1 /*
2  *  Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3  *
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
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef MODULES_DESKTOP_CAPTURE_WIN_SCREEN_CAPTURER_WIN_DIRECTX_H_
12 #define MODULES_DESKTOP_CAPTURE_WIN_SCREEN_CAPTURER_WIN_DIRECTX_H_
13 
14 #include <d3dcommon.h>
15 
16 #include <memory>
17 #include <vector>
18 
19 #include "api/scoped_refptr.h"
20 #include "modules/desktop_capture/desktop_capture_options.h"
21 #include "modules/desktop_capture/desktop_capturer.h"
22 #include "modules/desktop_capture/desktop_region.h"
23 #include "modules/desktop_capture/screen_capture_frame_queue.h"
24 #include "modules/desktop_capture/win/dxgi_duplicator_controller.h"
25 #include "modules/desktop_capture/win/dxgi_frame.h"
26 #include "rtc_base/system/rtc_export.h"
27 
28 namespace webrtc {
29 
30 // ScreenCapturerWinDirectx captures 32bit RGBA using DirectX.
31 class RTC_EXPORT ScreenCapturerWinDirectx : public DesktopCapturer {
32  public:
33   using D3dInfo = DxgiDuplicatorController::D3dInfo;
34 
35   // Whether the system supports DirectX based capturing.
36   static bool IsSupported();
37 
38   // Returns a most recent D3dInfo composed by
39   // DxgiDuplicatorController::Initialize() function. This function implicitly
40   // calls DxgiDuplicatorController::Initialize() if it has not been
41   // initialized. This function returns false and output parameter is kept
42   // unchanged if DxgiDuplicatorController::Initialize() failed.
43   // The D3dInfo may change based on hardware configuration even without
44   // restarting the hardware and software. Refer to https://goo.gl/OOCppq. So
45   // consumers should not cache the result returned by this function.
46   static bool RetrieveD3dInfo(D3dInfo* info);
47 
48   // Whether current process is running in a Windows session which is supported
49   // by ScreenCapturerWinDirectx.
50   // Usually using ScreenCapturerWinDirectx in unsupported sessions will fail.
51   // But this behavior may vary on different Windows version. So consumers can
52   // always try IsSupported() function.
53   static bool IsCurrentSessionSupported();
54 
55   // Maps |device_names| with the result from GetScreenList() and creates a new
56   // SourceList to include only the ones in |device_names|. If this function
57   // returns true, consumers can always assume |device_names|.size() equals to
58   // |screens|->size(), meanwhile |device_names|[i] and |screens|[i] indicate
59   // the same monitor on the system.
60   // Public for test only.
61   static bool GetScreenListFromDeviceNames(
62       const std::vector<std::string>& device_names,
63       DesktopCapturer::SourceList* screens);
64 
65   // Maps |id| with the result from GetScreenListFromDeviceNames() and returns
66   // the index of the entity in |device_names|. This function returns -1 if |id|
67   // cannot be found.
68   // Public for test only.
69   static int GetIndexFromScreenId(ScreenId id,
70                                   const std::vector<std::string>& device_names);
71 
72   explicit ScreenCapturerWinDirectx();
73 
74   ~ScreenCapturerWinDirectx() override;
75 
76   // DesktopCapturer implementation.
77   void Start(Callback* callback) override;
78   void SetSharedMemoryFactory(
79       std::unique_ptr<SharedMemoryFactory> shared_memory_factory) override;
80   void CaptureFrame() override;
81   bool GetSourceList(SourceList* sources) override;
82   bool SelectSource(SourceId id) override;
83 
84  private:
85   const rtc::scoped_refptr<DxgiDuplicatorController> controller_;
86   ScreenCaptureFrameQueue<DxgiFrame> frames_;
87   std::unique_ptr<SharedMemoryFactory> shared_memory_factory_;
88   Callback* callback_ = nullptr;
89   SourceId current_screen_id_ = kFullDesktopScreenId;
90 
91   RTC_DISALLOW_COPY_AND_ASSIGN(ScreenCapturerWinDirectx);
92 };
93 
94 }  // namespace webrtc
95 
96 #endif  // MODULES_DESKTOP_CAPTURE_WIN_SCREEN_CAPTURER_WIN_DIRECTX_H_
97