| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #ifndef WEBRTC_MODULES_DESKTOP_CAPTURE_WIN_SCREEN_CAPTURER_WIN_DIRECTX_H_ | 11 #ifndef WEBRTC_MODULES_DESKTOP_CAPTURE_WIN_SCREEN_CAPTURER_WIN_DIRECTX_H_ |
| 12 #define WEBRTC_MODULES_DESKTOP_CAPTURE_WIN_SCREEN_CAPTURER_WIN_DIRECTX_H_ | 12 #define WEBRTC_MODULES_DESKTOP_CAPTURE_WIN_SCREEN_CAPTURER_WIN_DIRECTX_H_ |
| 13 | 13 |
| 14 #include "webrtc/modules/desktop_capture/screen_capturer.h" | 14 #include "webrtc/modules/desktop_capture/screen_capturer.h" |
| 15 | 15 |
| 16 #include <comdef.h> | |
| 17 #include <D3DCommon.h> | |
| 18 #include <D3D11.h> | |
| 19 #include <DXGI.h> | |
| 20 #include <DXGI1_2.h> | |
| 21 #include <windows.h> | |
| 22 #include <wrl/client.h> | |
| 23 | |
| 24 #include <memory> | 16 #include <memory> |
| 25 #include <vector> | 17 #include <vector> |
| 26 | 18 |
| 27 #include "webrtc/base/thread_annotations.h" | |
| 28 #include "webrtc/modules/desktop_capture/desktop_capture_options.h" | 19 #include "webrtc/modules/desktop_capture/desktop_capture_options.h" |
| 29 #include "webrtc/modules/desktop_capture/desktop_geometry.h" | |
| 30 #include "webrtc/modules/desktop_capture/desktop_region.h" | 20 #include "webrtc/modules/desktop_capture/desktop_region.h" |
| 31 #include "webrtc/modules/desktop_capture/screen_capture_frame_queue.h" | 21 #include "webrtc/modules/desktop_capture/screen_capture_frame_queue.h" |
| 32 #include "webrtc/modules/desktop_capture/shared_desktop_frame.h" | 22 #include "webrtc/modules/desktop_capture/shared_desktop_frame.h" |
| 23 #include "webrtc/modules/desktop_capture/win/dxgi_context.h" |
| 33 | 24 |
| 34 namespace webrtc { | 25 namespace webrtc { |
| 35 | 26 |
| 36 // ScreenCapturerWinDirectx captures 32bit RGBA using DirectX. This | 27 // ScreenCapturerWinDirectx captures 32bit RGBA using DirectX. This |
| 37 // implementation won't work when ScreenCaptureFrameQueue.kQueueLength is not 2. | 28 // implementation won't work when ScreenCaptureFrameQueue.kQueueLength is not 2. |
| 38 class ScreenCapturerWinDirectx : public ScreenCapturer { | 29 class ScreenCapturerWinDirectx : public ScreenCapturer { |
| 39 public: | 30 public: |
| 40 // Initializes DirectX related components. Returns false if any error | 31 // Initializes DirectX related components. Returns false if any error |
| 41 // happened, any instance of this class won't be able to work in such status. | 32 // happened, any instance of this class won't be able to work in such status. |
| 42 // Thread safe, guarded by initialize_lock. | 33 // Thread safe, guarded by initialize_lock. |
| 43 static bool Initialize(); | 34 static bool Initialize(); |
| 44 | 35 |
| 45 explicit ScreenCapturerWinDirectx(const DesktopCaptureOptions& options); | 36 explicit ScreenCapturerWinDirectx(const DesktopCaptureOptions& options); |
| 46 virtual ~ScreenCapturerWinDirectx(); | 37 virtual ~ScreenCapturerWinDirectx(); |
| 47 | 38 |
| 48 void Start(Callback* callback) override; | 39 void Start(Callback* callback) override; |
| 49 void SetSharedMemoryFactory( | 40 void SetSharedMemoryFactory( |
| 50 std::unique_ptr<SharedMemoryFactory> shared_memory_factory) override; | 41 std::unique_ptr<SharedMemoryFactory> shared_memory_factory) override; |
| 51 void Capture(const DesktopRegion& region) override; | 42 void Capture(const DesktopRegion& region) override; |
| 52 bool GetScreenList(ScreenList* screens) override; | 43 bool GetScreenList(ScreenList* screens) override; |
| 53 bool SelectScreen(ScreenId id) override; | 44 bool SelectScreen(ScreenId id) override; |
| 54 | 45 |
| 55 private: | 46 private: |
| 56 // Texture is a pair of an ID3D11Texture2D and an IDXGISurface. Refer to its | 47 // Returns desktop size of selected screen. |
| 57 // implementation in source code for details. | 48 DesktopSize SelectedDesktopSize() const; |
| 58 class Texture; | |
| 59 | 49 |
| 60 // An implementation of DesktopFrame to return data from a Texture instance. | |
| 61 class DxgiDesktopFrame; | |
| 62 | |
| 63 static bool DoInitialize(); | |
| 64 | |
| 65 // Initializes DxgiOutputDuplication. If current DxgiOutputDuplication | |
| 66 // instance is existing, this function takes no-op and returns true. Returns | |
| 67 // false if it fails to execute windows api. | |
| 68 static bool DuplicateOutput(); | |
| 69 | |
| 70 // Deprecates current DxgiOutputDuplication instance and calls DuplicateOutput | |
| 71 // to reinitialize it. | |
| 72 static bool ForceDuplicateOutput(); | |
| 73 | |
| 74 // Detects update regions in last frame, if anything wrong, returns false. | |
| 75 // ProcessFrame will insert a whole desktop size as updated region instead. | |
| 76 static bool DetectUpdatedRegion(const DXGI_OUTDUPL_FRAME_INFO& frame_info, | |
| 77 DesktopRegion* updated_region); | |
| 78 | |
| 79 // A helper function to handle _com_error result in DetectUpdatedRegion. | |
| 80 // Returns false if the _com_error shows an error. | |
| 81 static bool HandleDetectUpdatedRegionError(const _com_error& error, | |
| 82 const char* stage); | |
| 83 | |
| 84 // Processes one frame received from AcquireNextFrame function, returns a | |
| 85 // nullptr if anything wrong. | |
| 86 std::unique_ptr<DesktopFrame> ProcessFrame( | |
| 87 const DXGI_OUTDUPL_FRAME_INFO& frame_info, | |
| 88 IDXGIResource* resource); | |
| 89 | |
| 90 // A shortcut to execute callback with current frame in frames. | |
| 91 void EmitCurrentFrame(); | |
| 92 | |
| 93 ScreenCaptureFrameQueue<rtc::scoped_refptr<Texture>> surfaces_; | |
| 94 ScreenCaptureFrameQueue<SharedDesktopFrame> frames_; | 50 ScreenCaptureFrameQueue<SharedDesktopFrame> frames_; |
| 95 std::unique_ptr<SharedMemoryFactory> shared_memory_factory_; | 51 std::unique_ptr<SharedMemoryFactory> shared_memory_factory_; |
| 96 Callback* callback_ = nullptr; | 52 Callback* callback_ = nullptr; |
| 97 | 53 |
| 54 DxgiContext context_; |
| 55 |
| 56 ScreenId current_screen_id = kFullDesktopScreenId; |
| 57 |
| 98 RTC_DISALLOW_COPY_AND_ASSIGN(ScreenCapturerWinDirectx); | 58 RTC_DISALLOW_COPY_AND_ASSIGN(ScreenCapturerWinDirectx); |
| 99 }; | 59 }; |
| 100 | 60 |
| 101 } // namespace webrtc | 61 } // namespace webrtc |
| 102 | 62 |
| 103 #endif // WEBRTC_MODULES_DESKTOP_CAPTURE_WIN_SCREEN_CAPTURER_WIN_DIRECTX_H_ | 63 #endif // WEBRTC_MODULES_DESKTOP_CAPTURE_WIN_SCREEN_CAPTURER_WIN_DIRECTX_H_ |
| OLD | NEW |