├── SampleApp
├── icon.ico
├── Resource.rc
├── packages.config
├── resource.h
├── pch.cpp
├── SampleApp.vcxproj.filters
├── pch.h
├── SampleApp.vcxproj
└── WindowFactory.h
├── VideoLibrary
├── packages.config
├── pch.cpp
├── RecordingStep.cpp
├── RecordingStep.h
├── SourceState.h
├── Vertex.h
├── DxResource.h
├── DisplayAdapter.cpp
├── CaptureFrameStep.cpp
├── DxMultithread.h
├── DisplayAdapter.h
├── VertexShader.hlsl
├── PixelShader.hlsl
├── CaptureFrameStep.h
├── AudioMedia.h
├── EncodingContext.h
├── VirtualDesktop.h
├── TextureToMediaSampleStep.h
├── RenderMoveRectsStep.h
├── pch.h
├── DesktopMonitor.cpp
├── ShaderCache.h
├── SharedSurface.h
├── DesktopMonitor.h
├── ScreenDuplicator.cpp
├── Frame.h
├── RenderDirtyRectsStep.h
├── ScreenDuplicator.h
├── ScreenMediaSinkWriter.h
├── TexturePool.h
├── Pipeline.h
├── DesktopPointer.h
├── TextureToMediaSampleStep.cpp
├── Errors.cpp
├── Errors.h
├── RenderPointerTextureStep.h
├── KeyedMutexLock.h
├── TexturePool.cpp
├── SharedSurface.cpp
├── DxResource.cpp
├── VirtualDesktop.cpp
├── AsyncMediaSourceReader.h
├── AudioMedia.cpp
├── DesktopPointer.cpp
├── AsyncMediaSourceReader.cpp
├── ShaderCache.cpp
├── Pipeline.cpp
├── Frame.cpp
├── VideoLibrary.vcxproj.filters
├── RenderMoveRectsStep.cpp
├── ScreenMediaSinkWriter.cpp
└── RenderDirtyRectsStep.cpp
├── VideoLibraryTests
├── packages.config
├── stdafx.cpp
├── targetver.h
├── DesktopMonitorTests.cpp
├── VideoLibraryTests.vcxproj.filters
├── stdafx.h
├── RecordingStepsTests.cpp
├── AudioTests.cpp
└── VideoLibraryTests.vcxproj
├── .gitattributes
├── readme.md
├── DesktopRecorderLibrary.sln
└── .gitignore
/SampleApp/icon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jgcoded/DesktopRecorderLibrary/HEAD/SampleApp/icon.ico
--------------------------------------------------------------------------------
/SampleApp/Resource.rc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jgcoded/DesktopRecorderLibrary/HEAD/SampleApp/Resource.rc
--------------------------------------------------------------------------------
/SampleApp/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/VideoLibrary/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/VideoLibraryTests/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/SampleApp/resource.h:
--------------------------------------------------------------------------------
1 | //{{NO_DEPENDENCIES}}
2 | // Microsoft Visual C++ generated include file.
3 | // Used by Resource.rc
4 | //
5 | #define IDI_ICON1 102
6 |
7 | // Next default values for new objects
8 | //
9 | #ifdef APSTUDIO_INVOKED
10 | #ifndef APSTUDIO_READONLY_SYMBOLS
11 | #define _APS_NEXT_RESOURCE_VALUE 106
12 | #define _APS_NEXT_COMMAND_VALUE 40001
13 | #define _APS_NEXT_CONTROL_VALUE 1002
14 | #define _APS_NEXT_SYMED_VALUE 101
15 | #endif
16 | #endif
17 |
--------------------------------------------------------------------------------
/SampleApp/pch.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | #include "pch.h"
21 |
--------------------------------------------------------------------------------
/VideoLibrary/pch.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | #include "pch.h"
21 |
--------------------------------------------------------------------------------
/VideoLibrary/RecordingStep.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | #include "pch.h"
21 | #include "RecordingStep.h"
22 |
23 |
24 | RecordingStep::RecordingStep()
25 | {
26 | }
27 |
28 | RecordingStep::~RecordingStep()
29 | {
30 | }
31 |
--------------------------------------------------------------------------------
/VideoLibrary/RecordingStep.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | #pragma once
21 |
22 | class RecordingStep
23 | {
24 | public:
25 | RecordingStep();
26 | virtual ~RecordingStep();
27 |
28 | virtual void Perform() = 0;
29 | };
30 |
--------------------------------------------------------------------------------
/VideoLibrary/SourceState.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | #pragma once
21 |
22 | enum class SourceState
23 | {
24 | Invalid, // Initial state. Have not started opening the stream.
25 | Opening, // BeginOpen is in progress.
26 | Stopped,
27 | Started,
28 | Recovering,
29 | Shutdown
30 | };
31 |
--------------------------------------------------------------------------------
/VideoLibrary/Vertex.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | #pragma once
21 |
22 | struct Position
23 | {
24 | float x, y, z;
25 | };
26 |
27 | struct TexCoord
28 | {
29 | float u, v;
30 | };
31 |
32 | struct Vertex
33 | {
34 | Position pos;
35 | TexCoord texCoord;
36 | };
37 |
38 | constexpr int g_VerticesPerRect = 6;
39 |
--------------------------------------------------------------------------------
/VideoLibraryTests/stdafx.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | // stdafx.cpp : source file that includes just the standard includes
21 | // VideoLibraryTests.pch will be the pre-compiled header
22 | // stdafx.obj will contain the pre-compiled type information
23 |
24 | #include "stdafx.h"
25 |
26 | // TODO: reference any additional headers you need in STDAFX.H
27 | // and not in this file
28 |
--------------------------------------------------------------------------------
/VideoLibraryTests/targetver.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | #pragma once
21 |
22 | // Including SDKDDKVer.h defines the highest available Windows platform.
23 |
24 | // If you wish to build your application for a previous Windows platform, include WinSDKVer.h and
25 | // set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h.
26 |
27 | #include
28 |
--------------------------------------------------------------------------------
/VideoLibrary/DxResource.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | #pragma once
21 |
22 | class DxResource
23 | {
24 | public:
25 |
26 | static winrt::com_ptr MakeDevice();
27 |
28 | static winrt::com_ptr MakeVideoEnabledDevice(winrt::com_ptr const& adapter);
29 |
30 | static winrt::com_ptr MakeDxgiFactory();
31 |
32 | private:
33 | DxResource();
34 | };
35 |
--------------------------------------------------------------------------------
/VideoLibrary/DisplayAdapter.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | #include "pch.h"
21 | #include "DxResource.h"
22 | #include "DisplayAdapter.h"
23 |
24 | DisplayAdapter::DisplayAdapter(winrt::com_ptr const& adapter)
25 | : mAdapter{ adapter }
26 | {
27 | DXGI_ADAPTER_DESC1 adapterDesc;
28 | winrt::check_hresult(mAdapter->GetDesc1(&adapterDesc));
29 |
30 | mAdapterName = adapterDesc.Description;
31 | mDevice = DxResource::MakeVideoEnabledDevice(mAdapter);
32 | }
33 |
--------------------------------------------------------------------------------
/VideoLibrary/CaptureFrameStep.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | #include "pch.h"
21 | #include "CaptureFrameStep.h"
22 |
23 | CaptureFrameStep::CaptureFrameStep(ScreenDuplicator& duplicator)
24 | : mDupl{ duplicator }
25 | {
26 | }
27 |
28 | CaptureFrameStep::~CaptureFrameStep()
29 | {
30 | }
31 |
32 | void CaptureFrameStep::Perform()
33 | {
34 | mFrame = std::make_shared(mDupl);
35 | }
36 |
37 | std::shared_ptr CaptureFrameStep::Result()
38 | {
39 | return mFrame;
40 | }
41 |
--------------------------------------------------------------------------------
/VideoLibrary/DxMultithread.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | #pragma once
21 |
22 | #include
23 | #include
24 |
25 | class DxMultithread
26 | {
27 | public:
28 | DxMultithread(winrt::com_ptr multithread)
29 | : mMultithread{multithread}
30 | {
31 | mMultithread->Enter();
32 | }
33 |
34 | ~DxMultithread()
35 | {
36 | mMultithread->Leave();
37 | }
38 |
39 | private:
40 | winrt::com_ptr mMultithread;
41 | };
--------------------------------------------------------------------------------
/VideoLibrary/DisplayAdapter.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | #pragma once
21 |
22 | class DisplayAdapter
23 | {
24 | public:
25 |
26 | DisplayAdapter(winrt::com_ptr const& adapter);
27 |
28 | winrt::com_ptr Device() const { return mDevice; }
29 |
30 | std::wstring const& Name() const { return mAdapterName; }
31 |
32 | private:
33 |
34 | winrt::com_ptr mDevice;
35 | winrt::com_ptr mAdapter;
36 |
37 | std::wstring mAdapterName;
38 | };
39 |
--------------------------------------------------------------------------------
/VideoLibrary/VertexShader.hlsl:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | struct VS_INPUT
21 | {
22 | float4 Pos : POSITION;
23 | float2 Tex : TEXCOORD;
24 | };
25 |
26 | struct VS_OUTPUT
27 | {
28 | float4 Pos : SV_POSITION;
29 | float2 Tex : TEXCOORD;
30 | };
31 |
32 |
33 | //--------------------------------------------------------------------------------------
34 | // Vertex Shader
35 | //--------------------------------------------------------------------------------------
36 | VS_OUTPUT main(VS_INPUT input)
37 | {
38 | return input;
39 | }
--------------------------------------------------------------------------------
/VideoLibrary/PixelShader.hlsl:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | Texture2D tx : register(t0);
21 | SamplerState samLinear : register(s0);
22 |
23 | struct PS_INPUT
24 | {
25 | float4 Pos : SV_POSITION;
26 | float2 Tex : TEXCOORD;
27 | };
28 |
29 | //--------------------------------------------------------------------------------------
30 | // Pixel Shader
31 | //--------------------------------------------------------------------------------------
32 | float4 main(PS_INPUT input) : SV_Target
33 | {
34 | return tx.Sample(samLinear, input.Tex);
35 | }
--------------------------------------------------------------------------------
/VideoLibrary/CaptureFrameStep.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | #pragma once
21 |
22 | #include "RecordingStep.h"
23 | #include "DesktopMonitor.h"
24 | #include "ScreenDuplicator.h"
25 | #include "Frame.h"
26 |
27 | class CaptureFrameStep :
28 | public RecordingStep
29 | {
30 | public:
31 | CaptureFrameStep(ScreenDuplicator& duplicator);
32 | virtual ~CaptureFrameStep();
33 |
34 | virtual void Perform() override;
35 |
36 | std::shared_ptr Result();
37 |
38 | private:
39 | ScreenDuplicator& mDupl;
40 | std::shared_ptr mFrame;
41 | };
42 |
--------------------------------------------------------------------------------
/VideoLibrary/AudioMedia.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | #pragma once
21 |
22 | struct AudioDevice
23 | {
24 | std::wstring friendlyName;
25 | std::wstring endpoint;
26 | };
27 |
28 | class AudioMedia
29 | {
30 |
31 | public:
32 |
33 | static std::vector GetAudioRecordingDevices();
34 |
35 | static winrt::com_ptr GetAudioMediaSourceFromEndpoint(std::wstring endpoint);
36 |
37 | static bool IsAudioRecordingDeviceAvailable(std::wstring endpoint);
38 |
39 | private:
40 |
41 | static AudioDevice GetAudioRecordingDeviceFromActivator(IMFActivate* activationObject);
42 | };
43 |
--------------------------------------------------------------------------------
/VideoLibrary/EncodingContext.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | #pragma once
21 |
22 | #include
23 | #include
24 |
25 | using ResolutionOption = winrt::Windows::Media::MediaProperties::VideoEncodingQuality;
26 | using AudioQuality = winrt::Windows::Media::MediaProperties::AudioEncodingQuality;
27 |
28 | struct EncodingContext
29 | {
30 | std::wstring fileName;
31 | ResolutionOption resolutionOption;
32 | AudioQuality audioQuality;
33 | int frameRate;
34 | int bitRate;
35 | winrt::com_ptr videoInputMediaType;
36 | winrt::com_ptr audioInputMediaType;
37 | winrt::com_ptr device;
38 | };
39 |
--------------------------------------------------------------------------------
/VideoLibrary/VirtualDesktop.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | #pragma once
21 |
22 | #include "DesktopMonitor.h"
23 | #include "ScreenDuplicator.h"
24 | #include "KeyedMutexLock.h"
25 |
26 | class VirtualDesktop
27 | {
28 | public:
29 | VirtualDesktop();
30 |
31 | ~VirtualDesktop();
32 |
33 | static std::vector GetAllDesktopMonitors();
34 |
35 | std::vector DesktopMonitors() const;
36 | RECT VirtualDesktopBounds() const;
37 |
38 | static RECT CalculateDesktopMonitorBounds(const std::vector& desktopMonitors);
39 |
40 | private:
41 |
42 | std::vector mDesktopMonitors;
43 | RECT mVirtualDesktopBounds;
44 | };
45 |
46 |
--------------------------------------------------------------------------------
/VideoLibrary/TextureToMediaSampleStep.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | #pragma once
21 |
22 | #include "RecordingStep.h"
23 |
24 | class TextureToMediaSampleStep : public RecordingStep
25 | {
26 | public:
27 | TextureToMediaSampleStep(
28 | winrt::com_ptr sourceTexture,
29 | winrt::com_ptr texturePool);
30 |
31 | virtual ~TextureToMediaSampleStep();
32 |
33 | // Inherited via RecordingStep
34 | virtual void Perform() override;
35 |
36 | winrt::com_ptr Result();
37 |
38 | private:
39 | winrt::com_ptr mSourceTexture;
40 | winrt::com_ptr mTexturePool;
41 | winrt::com_ptr mSample;
42 | };
43 |
--------------------------------------------------------------------------------
/VideoLibraryTests/DesktopMonitorTests.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | #include "stdafx.h"
21 | #include "CppUnitTest.h"
22 |
23 | using namespace Microsoft::VisualStudio::CppUnitTestFramework;
24 |
25 | #include "..\VideoLibrary\VirtualDesktop.h"
26 | #include "..\VideoLibrary\DesktopMonitor.h"
27 |
28 | namespace VideoLibraryTests
29 | {
30 | TEST_CLASS(DesktopMonitorTests)
31 | {
32 | public:
33 | TEST_METHOD(MonitorsCanBeFound)
34 | {
35 | auto virtualDesktop = std::make_shared();
36 | std::vector monitors = virtualDesktop->GetAllDesktopMonitors();
37 | Assert::IsFalse(monitors.empty());
38 | }
39 | };
40 | }
41 |
--------------------------------------------------------------------------------
/VideoLibrary/RenderMoveRectsStep.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | #pragma once
21 |
22 | #include "Frame.h"
23 | #include "RecordingStep.h"
24 |
25 | class RenderMoveRectsStep : public RecordingStep
26 | {
27 | public:
28 | RenderMoveRectsStep(
29 | std::shared_ptr frame,
30 | RECT virtualDesktopBounds,
31 | winrt::com_ptr stagingTexture,
32 | ID3D11Texture2D* sharedSurfacePtr);
33 |
34 | ~RenderMoveRectsStep();
35 |
36 | // Inherited via RecordingStep
37 | virtual void Perform() override;
38 |
39 | private:
40 | winrt::com_ptr mStagingTexture;
41 | ID3D11Texture2D* mSharedSurfacePtr;
42 | std::shared_ptr mFrame;
43 | RECT mVirtualDesktopBounds;
44 | };
45 |
46 |
--------------------------------------------------------------------------------
/VideoLibrary/pch.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | //
21 | // pch.h
22 | // Precompiled header for commonly included header files
23 | //
24 |
25 | #pragma once
26 |
27 | #define NOMINMAX // Don't let windows headers define min and max macros
28 |
29 | #include
30 |
31 | #include
32 | #include
33 | #include
34 | #include
35 | #include
36 |
37 | #include
38 | #include
39 | #include
40 | #include
41 | #include
42 | #include
43 |
44 | #include
45 | #include
46 | #include
47 | #include
48 | #include
49 |
50 | #include "VertexShader.h"
51 | #include "PixelShader.h"
52 | #include "Vertex.h"
53 |
--------------------------------------------------------------------------------
/VideoLibrary/DesktopMonitor.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | #include "pch.h"
21 | #include "DesktopMonitor.h"
22 |
23 | DesktopMonitor::DesktopMonitor(
24 | std::shared_ptr displayAdapter,
25 | winrt::com_ptr output,
26 | int outputIndex)
27 | : mDisplayAdapter{ displayAdapter }
28 | , mOutput{ output }
29 | , mOutputIndex{ outputIndex }
30 | {
31 | DXGI_OUTPUT_DESC outputDesc;
32 | winrt::check_hresult(mOutput->GetDesc(&outputDesc));
33 |
34 | DISPLAY_DEVICE displayDevice;
35 | displayDevice.cb = sizeof(DISPLAY_DEVICE);
36 | winrt::check_bool(EnumDisplayDevices(outputDesc.DeviceName, 0, &displayDevice, 0));
37 |
38 | mOutputName = displayDevice.DeviceString;
39 | mDesktopMonitorBounds = outputDesc.DesktopCoordinates;
40 | mRotation = outputDesc.Rotation;
41 | }
42 |
--------------------------------------------------------------------------------
/VideoLibraryTests/VideoLibraryTests.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | Header Files
20 |
21 |
22 | Header Files
23 |
24 |
25 |
26 |
27 | Source Files
28 |
29 |
30 | Source Files
31 |
32 |
33 | Source Files
34 |
35 |
36 | Source Files
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/VideoLibraryTests/stdafx.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | // stdafx.h : include file for standard system include files,
21 | // or project specific include files that are used frequently, but
22 | // are changed infrequently
23 | //
24 |
25 | #pragma once
26 |
27 | #include "targetver.h"
28 |
29 | // Headers for CppUnitTest
30 | #include "CppUnitTest.h"
31 |
32 | // TODO: reference additional headers your program requires here
33 | #include
34 | #include
35 | #include
36 | #include
37 | #include
38 | #include
39 |
40 | #include
41 | #include
42 | #include
43 |
44 | #include
45 | #include
46 | #include
47 |
48 | #include
49 | #include
50 | #include
51 |
--------------------------------------------------------------------------------
/VideoLibrary/ShaderCache.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | #pragma once
21 | class ShaderCache
22 | {
23 | public:
24 |
25 | ShaderCache(winrt::com_ptr device);
26 |
27 | virtual ~ShaderCache();
28 |
29 | winrt::com_ptr VertexShader();
30 |
31 | winrt::com_ptr VertexShaderInputLayout();
32 |
33 | winrt::com_ptr PixelShader();
34 |
35 | winrt::com_ptr LinearSampler();
36 |
37 | winrt::com_ptr BlendState();
38 |
39 | private:
40 |
41 | void Initialize(winrt::com_ptr device);
42 |
43 | winrt::com_ptr mVertexShader;
44 | winrt::com_ptr mVertexShaderInputLayout;
45 | winrt::com_ptr mPixelShader;
46 | winrt::com_ptr mLinearSampler;
47 | winrt::com_ptr mBlendState;
48 | };
49 |
--------------------------------------------------------------------------------
/SampleApp/SampleApp.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;hm;inl;inc;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | Header Files
20 |
21 |
22 | Header Files
23 |
24 |
25 | Header Files
26 |
27 |
28 |
29 |
30 | Source Files
31 |
32 |
33 | Source Files
34 |
35 |
36 |
37 |
38 | Resource Files
39 |
40 |
41 |
42 |
43 | Resource Files
44 |
45 |
46 |
47 |
48 |
49 |
--------------------------------------------------------------------------------
/VideoLibrary/SharedSurface.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | #pragma once
21 |
22 | #include "KeyedMutexLock.h"
23 |
24 | class SharedSurface
25 | {
26 | public:
27 | SharedSurface(winrt::com_ptr device, int width, int height);
28 | SharedSurface(winrt::com_ptr device, winrt::com_ptr texture, std::shared_ptr rotatingKeys, int width, int height);
29 |
30 | winrt::com_ptr Device() const { return mDevice; }
31 | std::unique_ptr Lock() const;
32 | std::shared_ptr OpenSharedSurfaceWithDevice(winrt::com_ptr device) const;
33 | D3D11_TEXTURE2D_DESC Desc() const { return mDesc; }
34 | private:
35 |
36 | winrt::com_ptr mSharedSurface;
37 | std::shared_ptr mRotatingKeys;
38 | winrt::com_ptr mMutex;
39 | winrt::com_ptr mDevice;
40 | D3D11_TEXTURE2D_DESC mDesc;
41 | int mWidth;
42 | int mHeight;
43 | };
44 |
45 |
--------------------------------------------------------------------------------
/VideoLibrary/DesktopMonitor.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | #pragma once
21 | #include "DisplayAdapter.h"
22 | #include "DesktopPointer.h"
23 |
24 | class DesktopMonitor
25 | {
26 | public:
27 |
28 | DesktopMonitor(
29 | std::shared_ptr displayAdapter,
30 | winrt::com_ptr output,
31 | int outputIndex);
32 |
33 | std::wstring OutputName() const { return mOutputName; }
34 |
35 | RECT DesktopMonitorBounds() const { return mDesktopMonitorBounds; }
36 |
37 | DXGI_MODE_ROTATION Rotation() const { return mRotation; }
38 |
39 | DisplayAdapter const& Adapter() const { return *mDisplayAdapter; }
40 |
41 | winrt::com_ptr Output() const { return mOutput; }
42 |
43 | int OutputIndex() const { return mOutputIndex; }
44 |
45 | private:
46 |
47 | winrt::com_ptr mOutput;
48 | RECT mDesktopMonitorBounds;
49 | std::wstring mOutputName;
50 | int mOutputIndex;
51 | DXGI_MODE_ROTATION mRotation;
52 |
53 | std::shared_ptr mDisplayAdapter;
54 | };
55 |
--------------------------------------------------------------------------------
/VideoLibrary/ScreenDuplicator.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | #include "pch.h"
21 | #include "Errors.h"
22 | #include "ScreenDuplicator.h"
23 |
24 | ScreenDuplicator::ScreenDuplicator(
25 | DesktopMonitor const& monitor,
26 | std::shared_ptr desktopPointer)
27 | : mDevice {monitor.Adapter().Device() }
28 | , mOutput{ monitor.Output()}
29 | , mDesktopPointer{ desktopPointer }
30 | , mRectBuffer{ std::make_shared>() }
31 | , mOutputIndex{ monitor.OutputIndex() }
32 | {
33 | HRESULT hr = mOutput->DuplicateOutput(mDevice.get(), mDupl.put());
34 | if (FAILED(hr))
35 | {
36 | ThrowExceptionCheckRecoverable(mDevice, CreateDuplicationExpectedErrors, hr);
37 | }
38 | }
39 |
40 | std::shared_ptr ScreenDuplicator::DesktopPointerPtr()
41 | {
42 | return mDesktopPointer;
43 | }
44 |
45 | ScreenDuplicator::~ScreenDuplicator()
46 | {
47 | if (mDupl)
48 | {
49 | // ignore hr, just release in case this object went out of scope
50 | (void)mDupl->ReleaseFrame();
51 | mDupl = nullptr;
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/VideoLibrary/Frame.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | #pragma once
21 |
22 | #include "ScreenDuplicator.h"
23 |
24 | class Frame
25 | {
26 | public:
27 | Frame(ScreenDuplicator& duplicator);
28 | ~Frame();
29 |
30 | winrt::com_ptr DesktopImage() const;
31 |
32 | RECT DesktopMonitorBounds() const;
33 |
34 | int64_t PresentationTime() const;
35 |
36 | bool Captured() const;
37 |
38 | DXGI_MODE_ROTATION Rotation() const;
39 |
40 | DXGI_OUTDUPL_MOVE_RECT* MoveRects() const;
41 |
42 | RECT* DirtyRects() const;
43 |
44 | size_t MoveRectsCount() const;
45 |
46 | size_t DirtyRectsCount() const;
47 |
48 | private:
49 | RECT mDesktopMonitorBounds;
50 | winrt::com_ptr mFrameTexture;
51 | DXGI_OUTDUPL_FRAME_INFO mFrameInfo;
52 | winrt::com_ptr mDupl;
53 | std::shared_ptr> mRectBuffer;
54 |
55 | bool mCaptured;
56 |
57 | // move/dirty rects data
58 | DXGI_OUTDUPL_MOVE_RECT* mMoveRects;
59 | size_t mNumMoveRects;
60 |
61 | RECT* mDirtyRects;
62 | size_t mNumDirtyRects;
63 |
64 | DXGI_MODE_ROTATION mRotation;
65 | };
66 |
--------------------------------------------------------------------------------
/VideoLibrary/RenderDirtyRectsStep.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | #pragma once
21 | #include "RecordingStep.h"
22 | #include "DesktopMonitor.h"
23 | #include "ShaderCache.h"
24 | #include "Frame.h"
25 | #include "Vertex.h"
26 |
27 | class RenderDirtyRectsStep : public RecordingStep
28 | {
29 | public:
30 | RenderDirtyRectsStep(
31 | std::shared_ptr frame,
32 | RECT virtualDesktopBounds,
33 | std::shared_ptr> vertexBuffer,
34 | std::shared_ptr shaderCache,
35 | ID3D11Texture2D* sharedSurfacePtr,
36 | winrt::com_ptr renderTargetView
37 | );
38 | ~RenderDirtyRectsStep();
39 |
40 | // Inherited via RecordingStep
41 | virtual void Perform() override;
42 |
43 | private:
44 |
45 | void UpdateDirtyRects();
46 |
47 | void RenderDirtyRects();
48 |
49 | std::shared_ptr mFrame;
50 | RECT mVirtualDesktopBounds;
51 | std::shared_ptr> mVertexBuffer;
52 | std::shared_ptr mShaderCache;
53 | ID3D11Texture2D* mSharedSurfacePtr;
54 | winrt::com_ptr mRenderTargetView;
55 | };
56 |
--------------------------------------------------------------------------------
/SampleApp/pch.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | //
21 | // pch.h
22 | // Precompiled header for commonly included header files
23 | //
24 |
25 | #pragma once
26 |
27 | #include
28 | #include
29 | #include
30 | #include
31 | #include
32 | #include
33 |
34 | #include
35 | #include
36 | #include
37 |
38 | #include
39 | #include
40 | #include
41 | #include
42 | #include
43 | #include
44 | #include
45 |
46 | #include
47 | #include
48 | #include
49 | #include
50 | #include
51 | #include
52 | #include
53 | #include
54 |
55 | #include "VideoLibrary\VirtualDesktop.h"
56 | #include "VideoLibrary\Pipeline.h"
57 | #include "VideoLibrary\AsyncMediaSourceReader.h"
58 | #include "VideoLibrary\ScreenMediaSinkWriter.h"
59 | #include "VideoLibrary\AudioMedia.h"
60 | #include "VideoLibrary\Errors.h"
61 |
62 | #include "WindowFactory.h"
63 |
--------------------------------------------------------------------------------
/VideoLibrary/ScreenDuplicator.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | #pragma once
21 |
22 | #include "DesktopPointer.h"
23 | #include "DisplayAdapter.h"
24 | #include "DesktopMonitor.h"
25 |
26 | class ScreenDuplicator
27 | {
28 | public:
29 |
30 | ScreenDuplicator(
31 | DesktopMonitor const& monitor,
32 | std::shared_ptr desktopPointer
33 | );
34 |
35 | winrt::com_ptr Device() const { return mDevice; }
36 |
37 | std::shared_ptr DesktopPointerPtr();
38 |
39 | winrt::com_ptr Output() const { return mOutput; }
40 |
41 | int OutputIndex() const { return mOutputIndex; }
42 |
43 | winrt::com_ptr Duplication() const { return mDupl; }
44 |
45 | std::shared_ptr> Buffer() const { return mRectBuffer; };
46 |
47 | ~ScreenDuplicator();
48 |
49 | private:
50 |
51 | winrt::com_ptr mDevice;
52 | winrt::com_ptr mOutput;
53 | winrt::com_ptr mDupl;
54 | std::wstring mOutputName;
55 | int mOutputIndex;
56 |
57 | // Holds move and dirty rects
58 | std::shared_ptr> mRectBuffer;
59 |
60 | std::shared_ptr mDesktopPointer;
61 | };
--------------------------------------------------------------------------------
/VideoLibrary/ScreenMediaSinkWriter.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | #pragma once
21 |
22 | #include
23 | #include "EncodingContext.h"
24 |
25 | class ScreenMediaSinkWriter
26 | {
27 | public:
28 |
29 | ScreenMediaSinkWriter(const EncodingContext& encodingContext);
30 |
31 | void Begin();
32 |
33 | void SignalGap();
34 |
35 | void ResetDevice(winrt::com_ptr device);
36 |
37 | void WriteSample(IMFSample* sample);
38 |
39 | void End();
40 |
41 | virtual ~ScreenMediaSinkWriter();
42 |
43 | private:
44 | winrt::com_ptr mSinkWriter;
45 | UINT mManagerResetToken;
46 | winrt::com_ptr mDeviceManager;
47 | winrt::com_ptr mSinkWriterAttributes;
48 | winrt::com_ptr mVideoInputMediaType;
49 | winrt::com_ptr mVideoOutputMediaType;
50 | winrt::com_ptr mAudioInputMediaType;
51 | winrt::com_ptr mAudioOutputMediaType;
52 | winrt::com_ptr mDevice;
53 | DWORD mVideoStreamIndex;
54 | DWORD mAudioStreamIndex;
55 | bool mIsWriting;
56 | std::chrono::high_resolution_clock::time_point mWriteStartTime;
57 | UINT32 mVideoFrameDuration;
58 |
59 | std::mutex mMutex;
60 | };
61 |
--------------------------------------------------------------------------------
/VideoLibrary/TexturePool.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | #pragma once
21 |
22 | #include "DesktopMonitor.h"
23 |
24 | class TexturePool : public IMFAsyncCallback
25 | {
26 | public:
27 |
28 | TexturePool(winrt::com_ptr device, D3D11_TEXTURE2D_DESC desc);
29 |
30 | winrt::com_ptr Acquire();
31 |
32 | virtual HRESULT STDMETHODCALLTYPE GetParameters(DWORD* pdwFlags, DWORD* pdwQueue) override;
33 |
34 | virtual HRESULT STDMETHODCALLTYPE Invoke(IMFAsyncResult* pAsyncResult) override;
35 |
36 | STDMETHODIMP_(ULONG) AddRef() { return InterlockedIncrement(&m_refCount); }
37 | STDMETHODIMP_(ULONG) Release()
38 | {
39 | assert(m_refCount > 0);
40 | ULONG uCount = InterlockedDecrement(&m_refCount);
41 | if (uCount == 0)
42 | {
43 | delete this;
44 | }
45 | return uCount;
46 | }
47 | virtual HRESULT QueryInterface(REFIID riid, void** ppv) noexcept override;
48 |
49 | virtual ~TexturePool();
50 |
51 | private:
52 |
53 | winrt::com_ptr CreateTexture();
54 |
55 | winrt::com_ptr mDevice;
56 | const D3D11_TEXTURE2D_DESC mTextureDesc;
57 | std::queue> mTexturePool;
58 | std::mutex mMutex;
59 | volatile long m_refCount;
60 |
61 | };
62 |
--------------------------------------------------------------------------------
/VideoLibrary/Pipeline.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | #pragma once
21 | #include "RecordingStep.h"
22 | #include "TexturePool.h"
23 | #include "DesktopMonitor.h"
24 | #include "DesktopPointer.h"
25 | #include "ScreenDuplicator.h"
26 | #include "Vertex.h"
27 | #include "ShaderCache.h"
28 | #include "SharedSurface.h"
29 |
30 | class Pipeline : public RecordingStep
31 | {
32 | public:
33 | Pipeline(
34 | std::shared_ptr duplicator,
35 | std::shared_ptr sharedSurface,
36 | RECT virtualDesktopBounds
37 | );
38 |
39 | virtual ~Pipeline();
40 |
41 | // Inherited via RecordingStep
42 | virtual void Perform() override;
43 |
44 | winrt::com_ptr Sample() const;
45 |
46 | private:
47 |
48 | void AllocateTexturePool();
49 | void AllocateStagingTexture(winrt::com_ptr device, const D3D11_TEXTURE2D_DESC& desc);
50 |
51 | std::shared_ptr mDuplicator;
52 | std::shared_ptr mSharedSurface;
53 | std::shared_ptr mShaderCache;
54 | std::shared_ptr> mVertexBuffer;
55 | winrt::com_ptr mTexturePool;
56 | winrt::com_ptr mStagingTexture;
57 | winrt::com_ptr mSample;
58 | winrt::com_ptr mRenderTargetView;
59 | RECT mVirtualDesktopBounds;
60 | RECT mDesktopMonitorBounds;
61 | };
62 |
--------------------------------------------------------------------------------
/VideoLibrary/DesktopPointer.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | #pragma once
21 |
22 | class DesktopPointer
23 | {
24 | public:
25 | DesktopPointer(RECT virtualDesktopBounds);
26 | virtual ~DesktopPointer();
27 |
28 | std::size_t BufferSize() const;
29 |
30 | byte* PutBuffer(std::size_t requiredSize = 0);
31 |
32 | /*
33 | TODO in a multi monitor recording scenario, need to offset the Position based on entire virtual desktop
34 | The returned position will work when recording a single monitor because it is
35 | relative to the monitor that currently owns the pointer
36 | */
37 | DXGI_OUTDUPL_POINTER_POSITION Position() const;
38 | void UpdatePosition(DXGI_OUTDUPL_POINTER_POSITION newPosition, LARGE_INTEGER updateTime, UINT outputIndex, RECT desktopMonitorBounds);
39 |
40 | DXGI_OUTDUPL_POINTER_SHAPE_INFO ShapeInfo() const;
41 | void ShapeInfo(DXGI_OUTDUPL_POINTER_SHAPE_INFO newShapeInfo);
42 |
43 | void UpdateTexture(winrt::com_ptr const& newImage);
44 | winrt::com_ptr Texture() const;
45 |
46 | bool Visible() const;
47 |
48 | private:
49 | std::vector mBuffer;
50 | DXGI_OUTDUPL_POINTER_SHAPE_INFO mShapeInfo;
51 | bool mIsPointerTextureStale;
52 | winrt::com_ptr mPointerTexture;
53 | DXGI_OUTDUPL_POINTER_POSITION mPosition;
54 | LARGE_INTEGER mLastUpdateTime;
55 | UINT mPointerOwnerIndex;
56 | bool mVisible;
57 | RECT mVirtualDesktopBounds;
58 | };
59 |
60 |
--------------------------------------------------------------------------------
/VideoLibrary/TextureToMediaSampleStep.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | #include "pch.h"
21 | #include "TexturePool.h"
22 | #include "TextureToMediaSampleStep.h"
23 |
24 | TextureToMediaSampleStep::TextureToMediaSampleStep(
25 | winrt::com_ptr sourceTexture,
26 | winrt::com_ptr texturePool)
27 | : mSourceTexture{ sourceTexture }
28 | , mTexturePool{ texturePool }
29 | {
30 | winrt::check_pointer(sourceTexture.get());
31 | winrt::check_pointer(texturePool.get());
32 | }
33 |
34 | TextureToMediaSampleStep::~TextureToMediaSampleStep()
35 | {
36 | }
37 |
38 | void TextureToMediaSampleStep::Perform()
39 | {
40 | winrt::com_ptr mediaBuffer;
41 | winrt::check_hresult(MFCreateDXGISurfaceBuffer(
42 | __uuidof(ID3D11Texture2D), mSourceTexture.get(), 0, true, mediaBuffer.put()));
43 |
44 | auto mediaBuffer2D{ mediaBuffer.as() };
45 |
46 | DWORD length = 0;
47 |
48 | winrt::check_hresult(mediaBuffer2D->GetContiguousLength(&length));
49 | winrt::check_hresult(mediaBuffer->SetCurrentLength(length));
50 |
51 | winrt::check_hresult(MFCreateVideoSampleFromSurface(nullptr, mSample.put()));
52 |
53 | auto trackedSample = mSample.as();
54 |
55 | winrt::check_hresult(trackedSample->SetAllocator(mTexturePool.get(), trackedSample.get()));
56 |
57 | winrt::check_hresult(mSample->AddBuffer(mediaBuffer.get()));
58 | }
59 |
60 | winrt::com_ptr TextureToMediaSampleStep::Result()
61 | {
62 | return mSample;
63 | }
64 |
--------------------------------------------------------------------------------
/VideoLibrary/Errors.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | #include "pch.h"
21 | #include "Errors.h"
22 |
23 | HRESULT TranslateHresultFailureWithDevice(winrt::com_ptr device, HRESULT hr)
24 | {
25 | HRESULT translatedHr = hr;
26 | HRESULT deviceRemovedReason = device->GetDeviceRemovedReason();
27 |
28 | if (device)
29 | {
30 | switch (deviceRemovedReason)
31 | {
32 | case DXGI_ERROR_DEVICE_REMOVED:
33 | case DXGI_ERROR_DEVICE_RESET:
34 | case static_cast(E_OUTOFMEMORY) :
35 | {
36 | // Our device has been stopped due to an external event on the GPU so map them all to
37 | // device removed and continue processing the condition
38 | translatedHr = DXGI_ERROR_DEVICE_REMOVED;
39 | break;
40 | }
41 |
42 | case S_OK:
43 | // Device is not removed
44 | break;
45 |
46 | default:
47 | // Device is removed, but we don't want to lose this removal reason
48 | translatedHr = deviceRemovedReason;
49 | break;
50 | }
51 | }
52 |
53 | return translatedHr;
54 | }
55 |
56 | void ThrowExceptionCheckRecoverable(winrt::com_ptr device, const std::vector expectedErrors, HRESULT error)
57 | {
58 | HRESULT translatedHr = TranslateHresultFailureWithDevice(device, error);
59 |
60 | if (std::find(expectedErrors.begin(), expectedErrors.end(), translatedHr) != expectedErrors.end())
61 | {
62 | throw RecoverableVideoException(translatedHr);
63 | }
64 |
65 | winrt::throw_hresult(error);
66 | }
67 |
68 | inline RecoverableVideoException::RecoverableVideoException(HRESULT hr)
69 | {
70 | mHresult = hr;
71 | }
72 |
73 | inline HRESULT RecoverableVideoException::Hresult() const
74 | {
75 | return mHresult;
76 | }
77 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | ###############################################################################
2 | # Set default behavior to automatically normalize line endings.
3 | ###############################################################################
4 | * text=auto
5 |
6 | ###############################################################################
7 | # Set default behavior for command prompt diff.
8 | #
9 | # This is need for earlier builds of msysgit that does not have it on by
10 | # default for csharp files.
11 | # Note: This is only used by command line
12 | ###############################################################################
13 | #*.cs diff=csharp
14 |
15 | ###############################################################################
16 | # Set the merge driver for project and solution files
17 | #
18 | # Merging from the command prompt will add diff markers to the files if there
19 | # are conflicts (Merging from VS is not affected by the settings below, in VS
20 | # the diff markers are never inserted). Diff markers may cause the following
21 | # file extensions to fail to load in VS. An alternative would be to treat
22 | # these files as binary and thus will always conflict and require user
23 | # intervention with every merge. To do so, just uncomment the entries below
24 | ###############################################################################
25 | #*.sln merge=binary
26 | #*.csproj merge=binary
27 | #*.vbproj merge=binary
28 | #*.vcxproj merge=binary
29 | #*.vcproj merge=binary
30 | #*.dbproj merge=binary
31 | #*.fsproj merge=binary
32 | #*.lsproj merge=binary
33 | #*.wixproj merge=binary
34 | #*.modelproj merge=binary
35 | #*.sqlproj merge=binary
36 | #*.wwaproj merge=binary
37 |
38 | ###############################################################################
39 | # behavior for image files
40 | #
41 | # image files are treated as binary by default.
42 | ###############################################################################
43 | #*.jpg binary
44 | #*.png binary
45 | #*.gif binary
46 |
47 | ###############################################################################
48 | # diff behavior for common document formats
49 | #
50 | # Convert binary document formats to text before diffing them. This feature
51 | # is only available from the command line. Turn it on by uncommenting the
52 | # entries below.
53 | ###############################################################################
54 | #*.doc diff=astextplain
55 | #*.DOC diff=astextplain
56 | #*.docx diff=astextplain
57 | #*.DOCX diff=astextplain
58 | #*.dot diff=astextplain
59 | #*.DOT diff=astextplain
60 | #*.pdf diff=astextplain
61 | #*.PDF diff=astextplain
62 | #*.rtf diff=astextplain
63 | #*.RTF diff=astextplain
64 |
--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
1 | # Desktop Recorder Library
2 |
3 | This is the C++ library for a desktop recorder app I worked on for a couple of
4 | years as a hobby project. The app reached 10K monthly active users on the Windows Store.
5 | Here is a demo of what the app looked like, powered by this library:
6 |
7 | [](https://youtu.be/GGDT2mmUgYg)
8 |
9 | ## Features
10 |
11 | * GPU Accelerated video encoding to MP4
12 | * On-the-fly, GPU-based texture resizing
13 | * Multiple monitor support
14 | * Monitor rotation support
15 | * Microphone Audio Capture
16 | * Mouse capture and rendering
17 |
18 | ## Dependencies
19 |
20 | * Windows Desktop Duplication
21 | * Windows Media Foundation
22 | * DirectX
23 | * Win32
24 |
25 | ## Minimal example
26 |
27 | ```cpp
28 | winrt::check_hresult(MFStartup(MF_VERSION));
29 | winrt::init_apartment();
30 |
31 | // VirtualDesktop is an abstraction for multimonitor support
32 | auto virtualDesktop = std::make_shared();
33 |
34 | // Pick a monitor to record
35 | std::vector desktopMonitors = virtualDesktop->GetAllDesktopMonitors();
36 | DesktopMonitor monitor = desktopMonitors[0];
37 |
38 | // Create a duplicator for the monitor
39 | std::unique_ptr duplicator =
40 | virtualDesktop->RecordMonitor(monitor);
41 |
42 | // The pipeline encapsulates texture rendering
43 | std::unique_ptr duplicationPipeline = std::make_unique(duplicator);
44 | duplicationPipeline->Perform();
45 |
46 | // The IMFSample that can be used with the ScreenMediaSinkWriter class
47 | winrt::com_ptr = duplicationPipeline->Sample();
48 |
49 | // Get the media type - redacted for brevity
50 | winrt::com_ptr videoMediaType = GetMediaTypeFromDuplicator(*duplicator);
51 |
52 | // Encoding options for the video
53 | EncodingContext encodingContext{};
54 | encodingContext.fileName = L"test.mp4";
55 | encodingContext.resolutionOption = ResolutionOption::Auto;
56 | encodingContext.audioQuality = AudioQuality::Auto;
57 | encodingContext.frameRate = 30;
58 | encodingContext.bitRate = 9000000;
59 | encodingContext.videoInputMediaType = videoMediaType;
60 | // See SampleApp for audio recording
61 | encodingContext.audioInputMediaType = nullptr;
62 | encodingContext.device = duplicator->Device();
63 |
64 | // Encapsulates the Media Foundation sink writer
65 | auto writer = std::make_unique(encodingContext);
66 | writer->Begin();
67 |
68 | // Write the sample
69 | sample->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video);
70 | writer->WriteSample(sample);
71 |
72 | // End finishes recording
73 | writer->End();
74 | writer.reset(nullptr);
75 |
76 | winrt::check_hresult(MFShutdown());
77 | ```
78 |
79 |
--------------------------------------------------------------------------------
/VideoLibrary/Errors.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | #pragma once
21 |
22 | #include
23 |
24 | // The below is from the DesktopDuplication sample from Microsoft in the DesktopDuplication.cpp file
25 |
26 | // Below are lists of errors expect from Dxgi API calls when a transition event like mode change, PnpStop, PnpStart
27 | // desktop switch, TDR or session disconnect/reconnect. In all these cases we want the application to clean up the threads that process
28 | // the desktop updates and attempt to recreate them.
29 | // If we get an error that is not on the appropriate list then we exit the application
30 |
31 | // These are the errors we expect from general Dxgi API due to a transition
32 | const std::vector SystemTransitionsExpectedErrors = {
33 | DXGI_ERROR_DEVICE_REMOVED,
34 | DXGI_ERROR_ACCESS_LOST,
35 | static_cast(WAIT_ABANDONED),
36 | };
37 |
38 | // These are the errors we expect from IDXGIOutput1::DuplicateOutput due to a transition
39 | const std::vector CreateDuplicationExpectedErrors = {
40 | DXGI_ERROR_DEVICE_REMOVED,
41 | static_cast(E_ACCESSDENIED),
42 | DXGI_ERROR_UNSUPPORTED,
43 | DXGI_ERROR_SESSION_DISCONNECTED
44 | };
45 |
46 | // These are the errors we expect from IDXGIOutputDuplication methods due to a transition
47 | const std::vector FrameInfoExpectedErrors = {
48 | DXGI_ERROR_DEVICE_REMOVED,
49 | DXGI_ERROR_ACCESS_LOST,
50 | DXGI_ERROR_INVALID_CALL
51 | };
52 |
53 | class RecoverableVideoException : public std::exception
54 | {
55 | public:
56 |
57 | RecoverableVideoException(HRESULT hr);
58 |
59 | HRESULT Hresult() const;
60 |
61 | private:
62 | HRESULT mHresult;
63 | };
64 |
65 | HRESULT TranslateHresultFailureWithDevice(winrt::com_ptr device, HRESULT hr);
66 |
67 | void ThrowExceptionCheckRecoverable(winrt::com_ptr device, const std::vector expectedErrors, HRESULT error);
68 |
--------------------------------------------------------------------------------
/VideoLibrary/RenderPointerTextureStep.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | #pragma once
21 |
22 | #include "RecordingStep.h"
23 | #include "ShaderCache.h"
24 | #include "TexturePool.h"
25 | #include "SharedSurface.h"
26 |
27 | class RenderPointerTextureStep : public RecordingStep
28 | {
29 | public:
30 | RenderPointerTextureStep(
31 | std::shared_ptr desktopPointer,
32 | std::shared_ptr sharedSurface,
33 | winrt::com_ptr device,
34 | std::shared_ptr shaderCache,
35 | winrt::com_ptr texturePool,
36 | RECT virtualDesktopBounds,
37 | RECT desktopMonitorBounds);
38 |
39 | virtual ~RenderPointerTextureStep();
40 |
41 | virtual void Perform() override;
42 |
43 | winrt::com_ptr Result();
44 |
45 | private:
46 | winrt::com_ptr MakePointerTexture();
47 | winrt::com_ptr MakeColorPointerTexture();
48 | winrt::com_ptr MakeMaskedPointerTexture();
49 |
50 | void MakeMonochromePointerBuffer(UINT* dest, const UINT * colorData, UINT colorPitch, byte* maskData, UINT maskPitch, UINT width, UINT height, UINT maskX, UINT maskY, UINT maskHeight);
51 | void MakeMaskedColorPointerBuffer(UINT * dest, const UINT * colorData, UINT colorPitch, UINT * maskData, UINT maskPitch, UINT width, UINT height, UINT maskX, UINT maskY);
52 | winrt::com_ptr MakeColorPointer(byte* data, int width, int height);
53 |
54 | winrt::com_ptr mDevice;
55 | std::shared_ptr mDesktopPointer;
56 | std::shared_ptr mShaderCache;
57 | std::shared_ptr mSharedSurface;
58 | winrt::com_ptr mTexturePool;
59 |
60 | ID3D11Texture2D* mSharedSurfacePtr;
61 |
62 | winrt::com_ptr mResult;
63 |
64 | RECT mVirtualDesktopBounds;
65 | RECT mDesktopMonitorBounds;
66 | };
67 |
--------------------------------------------------------------------------------
/VideoLibrary/KeyedMutexLock.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | #pragma once
21 |
22 | class RotatingKeys
23 | {
24 | public:
25 |
26 | RotatingKeys()
27 | : RotatingKeys(0, 1)
28 | {
29 | }
30 |
31 | RotatingKeys(int acquireKey, int releaseKey)
32 | : mAcquireKey{ acquireKey }
33 | , mReleaseKey{ releaseKey }
34 | {
35 | }
36 |
37 | int AcquireKey() const
38 | {
39 | return mAcquireKey;
40 | }
41 |
42 | int ReleaseKey() const
43 | {
44 | return mReleaseKey;
45 | }
46 |
47 | void Rotate()
48 | {
49 | auto previousRelease = mReleaseKey;
50 | mReleaseKey = mAcquireKey;
51 | mAcquireKey = previousRelease;
52 | }
53 |
54 | private:
55 | int mAcquireKey;
56 | int mReleaseKey;
57 | };
58 |
59 | class KeyedMutexLock
60 | {
61 | public:
62 | KeyedMutexLock(
63 | winrt::com_ptr texture,
64 | winrt::com_ptr mutex,
65 | std::shared_ptr rotatingKeys)
66 | : mMutex{ mutex }
67 | , mRotatingKeys{ rotatingKeys }
68 | , mLocked{ false }
69 | {
70 | if (mMutex == nullptr)
71 | {
72 | throw std::exception("null mutex");
73 | }
74 |
75 | if (mRotatingKeys == nullptr)
76 | {
77 | throw std::exception("null rotating keys keyed mutex lock");
78 | }
79 |
80 | HRESULT hr = mutex->AcquireSync(mRotatingKeys->AcquireKey(), 10);
81 | if (hr == static_cast(WAIT_TIMEOUT))
82 | {
83 | return;
84 | }
85 |
86 | winrt::check_hresult(hr);
87 | mLocked = true;
88 | mTexture = texture;
89 | }
90 |
91 | ~KeyedMutexLock()
92 | {
93 | // only release and rotate if mLocked is true?
94 | auto releaseKey = mRotatingKeys->ReleaseKey();
95 | mRotatingKeys->Rotate();
96 | winrt::check_hresult(mMutex->ReleaseSync(releaseKey));
97 | }
98 |
99 | bool Locked() const { return mLocked; }
100 |
101 | ID3D11Texture2D* TexturePtr() const
102 | {
103 | return mTexture.get();
104 | }
105 |
106 | private:
107 | winrt::com_ptr mMutex;
108 | bool mLocked;
109 | std::shared_ptr mRotatingKeys;
110 | winrt::com_ptr mTexture;
111 | };
112 |
--------------------------------------------------------------------------------
/VideoLibraryTests/RecordingStepsTests.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | #include "stdafx.h"
21 | #include "CppUnitTest.h"
22 |
23 | using namespace Microsoft::VisualStudio::CppUnitTestFramework;
24 |
25 | #include "..\VideoLibrary\CaptureFrameStep.h"
26 | #include "..\VideoLibrary\RenderPointerTextureStep.h"
27 | #include "..\VideoLibrary\VirtualDesktop.h"
28 | #include
29 | #include
30 |
31 | namespace VideoLibraryTests
32 | {
33 | TEST_CLASS(RecordingStepsTests)
34 | {
35 | public:
36 | TEST_METHOD(RecordDesktop)
37 | {
38 | auto virtualDesktop = std::make_shared();
39 |
40 | auto monitors = virtualDesktop->DesktopMonitors();
41 | RotatingKeys keys{ 0, 1 };
42 | std::shared_ptr desktopPointer = std::make_shared(virtualDesktop->VirtualDesktopBounds());
43 | std::vector> duplicators;
44 | for (auto monitor : monitors)
45 | {
46 | std::shared_ptr duplicator = std::make_shared(
47 | monitor,
48 | desktopPointer
49 | );
50 | duplicators.push_back(duplicator);
51 | }
52 |
53 | std::unique_ptr recordingStep;
54 | int i = 0;
55 | for (auto& duplicator : duplicators) {
56 |
57 | for (int j = 0; j < 100; ++i, ++j) {
58 | recordingStep.reset(new CaptureFrameStep{ *duplicator });
59 | recordingStep->Perform();
60 | }
61 | }
62 |
63 | // TODO fix
64 | /*
65 | auto step = dynamic_cast(recordingStep.get());
66 | Assert::IsNotNull(step);
67 | step->Perform();
68 | auto texture = step->Result();
69 | winrt::com_ptr device;
70 | winrt::com_ptr context;
71 | texture->GetDevice(device.put());
72 | device->GetImmediateContext(context.put());
73 |
74 | //DirectX::SaveWICTextureToFile(context.get(), virtualDesktop->DesktopPointer().Texture().get(), GUID_ContainerFormatPng, L"pointer.jpg");
75 | DirectX::SaveWICTextureToFile(context.get(), texture.get(), GUID_ContainerFormatJpeg, L"test.jpg");
76 | */
77 | }
78 |
79 |
80 | };
81 | }
82 |
--------------------------------------------------------------------------------
/DesktopRecorderLibrary.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.2.32630.192
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SampleApp", "SampleApp\SampleApp.vcxproj", "{9EDDC606-86CE-49A2-9174-7CEA60F26405}"
7 | EndProject
8 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "VideoLibrary", "VideoLibrary\VideoLibrary.vcxproj", "{3CE469F1-0BE6-4AAD-ABD3-E49A9E270CD0}"
9 | EndProject
10 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "VideoLibraryTests", "VideoLibraryTests\VideoLibraryTests.vcxproj", "{348EF9C9-BBF4-4985-8784-4B47AC4E9F0A}"
11 | EndProject
12 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{B4B5EDA1-4563-4501-8F0A-BDA59882F9EF}"
13 | ProjectSection(SolutionItems) = preProject
14 | readme.md = readme.md
15 | EndProjectSection
16 | EndProject
17 | Global
18 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
19 | Debug|x64 = Debug|x64
20 | Debug|x86 = Debug|x86
21 | Release|x64 = Release|x64
22 | Release|x86 = Release|x86
23 | EndGlobalSection
24 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
25 | {9EDDC606-86CE-49A2-9174-7CEA60F26405}.Debug|x64.ActiveCfg = Debug|x64
26 | {9EDDC606-86CE-49A2-9174-7CEA60F26405}.Debug|x64.Build.0 = Debug|x64
27 | {9EDDC606-86CE-49A2-9174-7CEA60F26405}.Debug|x86.ActiveCfg = Debug|Win32
28 | {9EDDC606-86CE-49A2-9174-7CEA60F26405}.Debug|x86.Build.0 = Debug|Win32
29 | {9EDDC606-86CE-49A2-9174-7CEA60F26405}.Release|x64.ActiveCfg = Release|x64
30 | {9EDDC606-86CE-49A2-9174-7CEA60F26405}.Release|x64.Build.0 = Release|x64
31 | {9EDDC606-86CE-49A2-9174-7CEA60F26405}.Release|x86.ActiveCfg = Release|Win32
32 | {9EDDC606-86CE-49A2-9174-7CEA60F26405}.Release|x86.Build.0 = Release|Win32
33 | {3CE469F1-0BE6-4AAD-ABD3-E49A9E270CD0}.Debug|x64.ActiveCfg = Debug|x64
34 | {3CE469F1-0BE6-4AAD-ABD3-E49A9E270CD0}.Debug|x64.Build.0 = Debug|x64
35 | {3CE469F1-0BE6-4AAD-ABD3-E49A9E270CD0}.Debug|x86.ActiveCfg = Debug|Win32
36 | {3CE469F1-0BE6-4AAD-ABD3-E49A9E270CD0}.Debug|x86.Build.0 = Debug|Win32
37 | {3CE469F1-0BE6-4AAD-ABD3-E49A9E270CD0}.Release|x64.ActiveCfg = Release|x64
38 | {3CE469F1-0BE6-4AAD-ABD3-E49A9E270CD0}.Release|x64.Build.0 = Release|x64
39 | {3CE469F1-0BE6-4AAD-ABD3-E49A9E270CD0}.Release|x86.ActiveCfg = Release|Win32
40 | {3CE469F1-0BE6-4AAD-ABD3-E49A9E270CD0}.Release|x86.Build.0 = Release|Win32
41 | {348EF9C9-BBF4-4985-8784-4B47AC4E9F0A}.Debug|x64.ActiveCfg = Debug|x64
42 | {348EF9C9-BBF4-4985-8784-4B47AC4E9F0A}.Debug|x64.Build.0 = Debug|x64
43 | {348EF9C9-BBF4-4985-8784-4B47AC4E9F0A}.Debug|x86.ActiveCfg = Debug|Win32
44 | {348EF9C9-BBF4-4985-8784-4B47AC4E9F0A}.Debug|x86.Build.0 = Debug|Win32
45 | {348EF9C9-BBF4-4985-8784-4B47AC4E9F0A}.Release|x64.ActiveCfg = Release|x64
46 | {348EF9C9-BBF4-4985-8784-4B47AC4E9F0A}.Release|x64.Build.0 = Release|x64
47 | {348EF9C9-BBF4-4985-8784-4B47AC4E9F0A}.Release|x86.ActiveCfg = Release|Win32
48 | {348EF9C9-BBF4-4985-8784-4B47AC4E9F0A}.Release|x86.Build.0 = Release|Win32
49 | EndGlobalSection
50 | GlobalSection(SolutionProperties) = preSolution
51 | HideSolutionNode = FALSE
52 | EndGlobalSection
53 | GlobalSection(ExtensibilityGlobals) = postSolution
54 | SolutionGuid = {E1FCAAB2-181D-4A98-9DBA-33441BD5A07F}
55 | EndGlobalSection
56 | EndGlobal
57 |
--------------------------------------------------------------------------------
/VideoLibrary/TexturePool.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | #include "pch.h"
21 | #include "DesktopMonitor.h"
22 | #include "RecordingStep.h"
23 | #include "CaptureFrameStep.h"
24 | #include "VirtualDesktop.h"
25 | #include "TexturePool.h"
26 |
27 | TexturePool::TexturePool(winrt::com_ptr device, const D3D11_TEXTURE2D_DESC desc)
28 | : mDevice{ device }
29 | , mTextureDesc{ desc }
30 | , m_refCount{ 1 }
31 | {
32 | }
33 |
34 | winrt::com_ptr TexturePool::Acquire()
35 | {
36 | std::lock_guard lock{ mMutex };
37 | if (mTexturePool.empty()) {
38 | return CreateTexture();
39 | }
40 |
41 | auto texture = mTexturePool.front();
42 | mTexturePool.pop();
43 | return texture;
44 | }
45 |
46 | HRESULT __stdcall TexturePool::GetParameters(DWORD * pdwFlags, DWORD * pdwQueue)
47 | {
48 | UNREFERENCED_PARAMETER(pdwFlags);
49 | UNREFERENCED_PARAMETER(pdwQueue);
50 | return E_NOTIMPL;
51 | }
52 |
53 | HRESULT __stdcall TexturePool::Invoke(IMFAsyncResult * pAsyncResult)
54 | {
55 | winrt::com_ptr unknown;
56 | winrt::check_hresult(pAsyncResult->GetObjectW(unknown.put()));
57 |
58 | auto sample = unknown.as();
59 |
60 | winrt::com_ptr mediaBuffer;
61 | winrt::check_hresult(sample->GetBufferByIndex(0, mediaBuffer.put()));
62 |
63 | auto dxgiBuffer = mediaBuffer.as();
64 |
65 | winrt::com_ptr texture;
66 | winrt::check_hresult(dxgiBuffer->GetResource(IID_PPV_ARGS(texture.put())));
67 |
68 | {
69 | std::lock_guard lock{ mMutex };
70 | mTexturePool.push(texture);
71 | }
72 |
73 | return S_OK;
74 | }
75 |
76 | winrt::com_ptr TexturePool::CreateTexture()
77 | {
78 | D3D11_TEXTURE2D_DESC moveDesc = mTextureDesc;
79 | moveDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
80 | moveDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED;
81 | winrt::com_ptr texture;
82 | mDevice->CreateTexture2D(&moveDesc, nullptr, texture.put());
83 | return texture;
84 | }
85 |
86 | HRESULT TexturePool::QueryInterface(REFIID riid, void** ppv) noexcept
87 | {
88 | static const QITAB qit[] =
89 | {
90 | QITABENT(TexturePool, IMFAsyncCallback),
91 | { 0 }
92 | };
93 | return QISearch(this, qit, riid, ppv);
94 | }
95 |
96 | TexturePool::~TexturePool()
97 | {
98 | assert(m_refCount == 0);
99 | }
100 |
--------------------------------------------------------------------------------
/VideoLibrary/SharedSurface.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2022 by Julio Gutierrez (desktoprecorderapp@gmail.com)
3 |
4 | This file is part of DesktopRecorderLibrary.
5 |
6 | DesktopRecorderLibrary is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by the
8 | Free Software Foundation, either version 3 of the License,
9 | or (at your option) any later version.
10 |
11 | DesktopRecorderLibrary is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with DesktopRecorderLibrary. If not, see .
18 | */
19 |
20 | #include "pch.h"
21 | #include "SharedSurface.h"
22 |
23 | SharedSurface::SharedSurface(winrt::com_ptr device, int width, int height)
24 | : mDevice{ device }
25 | , mSharedSurface{ nullptr }
26 | , mMutex { nullptr }
27 | , mRotatingKeys { new RotatingKeys }
28 | , mWidth{ width }
29 | , mHeight{ height }
30 | {
31 | D3D11_TEXTURE2D_DESC desc;
32 | RtlZeroMemory(&desc, sizeof(D3D11_TEXTURE2D_DESC));
33 | desc.Width = static_cast(width);
34 | desc.Height = static_cast(height);
35 | desc.MipLevels = 1;
36 | desc.ArraySize = 1;
37 | desc.SampleDesc.Count = 1;
38 | desc.CPUAccessFlags = 0;
39 | desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
40 | desc.Usage = D3D11_USAGE_DEFAULT;
41 | desc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
42 | desc.MiscFlags = D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;
43 |
44 | winrt::check_hresult(mDevice->CreateTexture2D(&desc, nullptr, mSharedSurface.put()));
45 | mMutex = mSharedSurface.as();
46 | winrt::check_pointer(mMutex.get());
47 | mSharedSurface->GetDesc(&mDesc);
48 | }
49 |
50 | SharedSurface::SharedSurface(
51 | winrt::com_ptr