Line data Source code
1 : /*
2 : * Copyright (c) 2014 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 WEBRTC_MODULES_DESKTOP_CAPTURE_MAIN_SOURCE_DESKTOP_CAPTURE_IMPL_H_
12 : #define WEBRTC_MODULES_DESKTOP_CAPTURE_MAIN_SOURCE_DESKTOP_CAPTURE_IMPL_H_
13 :
14 : /*
15 : * video_capture_impl.h
16 : */
17 :
18 : #include <string>
19 : #include <memory>
20 :
21 : #include "webrtc/video_frame.h"
22 : #include "webrtc/base/scoped_ref_ptr.h"
23 : #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
24 : #include "webrtc/modules/video_capture/video_capture_config.h"
25 : #include "webrtc/modules/desktop_capture/shared_memory.h"
26 : #include "webrtc/base/platform_thread.h"
27 : #include "webrtc/system_wrappers/include/event_wrapper.h"
28 : #include "webrtc/modules/desktop_capture/desktop_device_info.h"
29 : #include "webrtc/modules/desktop_capture/desktop_and_cursor_composer.h"
30 :
31 : using namespace webrtc::videocapturemodule;
32 :
33 : namespace webrtc {
34 :
35 : class CriticalSectionWrapper;
36 : class VideoCaptureEncodeInterface;
37 :
38 :
39 : //simulate deviceInfo interface for video engine, bridge screen/application and real screen/application device info
40 :
41 : class ScreenDeviceInfoImpl : public VideoCaptureModule::DeviceInfo {
42 : public:
43 : ScreenDeviceInfoImpl(const int32_t id);
44 : virtual ~ScreenDeviceInfoImpl(void);
45 :
46 : int32_t Init();
47 : int32_t Refresh();
48 :
49 : virtual uint32_t NumberOfDevices();
50 : virtual int32_t GetDeviceName(uint32_t deviceNumber,
51 : char* deviceNameUTF8,
52 : uint32_t deviceNameLength,
53 : char* deviceUniqueIdUTF8,
54 : uint32_t deviceUniqueIdUTF8Length,
55 : char* productUniqueIdUTF8,
56 : uint32_t productUniqueIdUTF8Length,
57 : pid_t* pid);
58 :
59 : virtual int32_t DisplayCaptureSettingsDialogBox(const char* deviceUniqueIdUTF8,
60 : const char* dialogTitleUTF8,
61 : void* parentWindow,
62 : uint32_t positionX,
63 : uint32_t positionY);
64 : virtual int32_t NumberOfCapabilities(const char* deviceUniqueIdUTF8);
65 : virtual int32_t GetCapability(const char* deviceUniqueIdUTF8,
66 : const uint32_t deviceCapabilityNumber,
67 : VideoCaptureCapability& capability);
68 :
69 : virtual int32_t GetBestMatchedCapability(const char* deviceUniqueIdUTF8,
70 : const VideoCaptureCapability& requested,
71 : VideoCaptureCapability& resulting);
72 : virtual int32_t GetOrientation(const char* deviceUniqueIdUTF8,
73 : VideoRotation& orientation);
74 : protected:
75 : int32_t _id;
76 : std::unique_ptr<DesktopDeviceInfo> desktop_device_info_;
77 :
78 : };
79 :
80 : class AppDeviceInfoImpl : public VideoCaptureModule::DeviceInfo {
81 : public:
82 : AppDeviceInfoImpl(const int32_t id);
83 : virtual ~AppDeviceInfoImpl(void);
84 :
85 : int32_t Init();
86 : int32_t Refresh();
87 :
88 : virtual uint32_t NumberOfDevices();
89 : virtual int32_t GetDeviceName(uint32_t deviceNumber,
90 : char* deviceNameUTF8,
91 : uint32_t deviceNameLength,
92 : char* deviceUniqueIdUTF8,
93 : uint32_t deviceUniqueIdUTF8Length,
94 : char* productUniqueIdUTF8,
95 : uint32_t productUniqueIdUTF8Length,
96 : pid_t* pid);
97 :
98 : virtual int32_t DisplayCaptureSettingsDialogBox(const char* deviceUniqueIdUTF8,
99 : const char* dialogTitleUTF8,
100 : void* parentWindow,
101 : uint32_t positionX,
102 : uint32_t positionY);
103 : virtual int32_t NumberOfCapabilities(const char* deviceUniqueIdUTF8);
104 : virtual int32_t GetCapability(const char* deviceUniqueIdUTF8,
105 : const uint32_t deviceCapabilityNumber,
106 : VideoCaptureCapability& capability);
107 :
108 : virtual int32_t GetBestMatchedCapability(const char* deviceUniqueIdUTF8,
109 : const VideoCaptureCapability& requested,
110 : VideoCaptureCapability& resulting);
111 : virtual int32_t GetOrientation(const char* deviceUniqueIdUTF8,
112 : VideoRotation& orientation);
113 : protected:
114 : int32_t _id;
115 : std::unique_ptr<DesktopDeviceInfo> desktop_device_info_;
116 : };
117 :
118 : class WindowDeviceInfoImpl : public VideoCaptureModule::DeviceInfo {
119 : public:
120 0 : WindowDeviceInfoImpl(const int32_t id) : _id(id) {};
121 0 : virtual ~WindowDeviceInfoImpl(void) {};
122 :
123 : int32_t Init();
124 : int32_t Refresh();
125 :
126 : virtual uint32_t NumberOfDevices();
127 : virtual int32_t GetDeviceName(uint32_t deviceNumber,
128 : char* deviceNameUTF8,
129 : uint32_t deviceNameLength,
130 : char* deviceUniqueIdUTF8,
131 : uint32_t deviceUniqueIdUTF8Length,
132 : char* productUniqueIdUTF8,
133 : uint32_t productUniqueIdUTF8Length,
134 : pid_t* pid);
135 :
136 : virtual int32_t DisplayCaptureSettingsDialogBox(const char* deviceUniqueIdUTF8,
137 : const char* dialogTitleUTF8,
138 : void* parentWindow,
139 : uint32_t positionX,
140 : uint32_t positionY);
141 : virtual int32_t NumberOfCapabilities(const char* deviceUniqueIdUTF8);
142 : virtual int32_t GetCapability(const char* deviceUniqueIdUTF8,
143 : const uint32_t deviceCapabilityNumber,
144 : VideoCaptureCapability& capability);
145 :
146 : virtual int32_t GetBestMatchedCapability(const char* deviceUniqueIdUTF8,
147 : const VideoCaptureCapability& requested,
148 : VideoCaptureCapability& resulting);
149 : virtual int32_t GetOrientation(const char* deviceUniqueIdUTF8,
150 : VideoRotation& orientation);
151 : protected:
152 : int32_t _id;
153 : std::unique_ptr<DesktopDeviceInfo> desktop_device_info_;
154 :
155 : };
156 :
157 : // Reuses the video engine pipeline for screen sharing.
158 : // As with video, DesktopCaptureImpl is a proxy for screen sharing
159 : // and follows the video pipeline design
160 : class DesktopCaptureImpl: public DesktopCapturer::Callback,
161 : public VideoCaptureModule,
162 : public VideoCaptureExternal
163 : {
164 : public:
165 : /* Create a screen capture modules object
166 : */
167 : static VideoCaptureModule* Create(const int32_t id, const char* uniqueId, const CaptureDeviceType type);
168 : static VideoCaptureModule::DeviceInfo* CreateDeviceInfo(const int32_t id, const CaptureDeviceType type);
169 :
170 : int32_t Init(const char* uniqueId, const CaptureDeviceType type);
171 : //RefCounting for RefCountedModule
172 : virtual int32_t AddRef() const override;
173 : virtual int32_t Release() const override;
174 : //Call backs
175 : virtual void RegisterCaptureDataCallback(rtc::VideoSinkInterface<VideoFrame> *dataCallback) override;
176 : virtual void DeRegisterCaptureDataCallback() override;
177 :
178 : virtual int32_t SetCaptureRotation(VideoRotation rotation) override;
179 : virtual bool SetApplyRotation(bool enable) override;
180 0 : virtual bool GetApplyRotation() override { return true; }
181 :
182 : virtual const char* CurrentDeviceName() const override;
183 :
184 : // Implement VideoCaptureExternal
185 : // |capture_time| must be specified in the NTP time format in milliseconds.
186 : virtual int32_t IncomingFrame(uint8_t* videoFrame,
187 : size_t videoFrameLength,
188 : const VideoCaptureCapability& frameInfo,
189 : int64_t captureTime = 0) override;
190 :
191 : // Platform dependent
192 : virtual int32_t StartCapture(const VideoCaptureCapability& capability) override;
193 : virtual int32_t StopCapture() override;
194 : virtual bool CaptureStarted() override;
195 : virtual int32_t CaptureSettings(VideoCaptureCapability& settings) override;
196 :
197 : protected:
198 : DesktopCaptureImpl(const int32_t id);
199 : virtual ~DesktopCaptureImpl();
200 : int32_t DeliverCapturedFrame(webrtc::VideoFrame& captureFrame,
201 : int64_t capture_time);
202 :
203 : static const uint32_t kMaxDesktopCaptureCpuUsage = 50; // maximum CPU usage in %
204 :
205 : int32_t _id; // Module ID
206 : std::string _deviceUniqueId; // current Device unique name;
207 : CriticalSectionWrapper& _apiCs;
208 : VideoCaptureCapability _requestedCapability; // Should be set by platform dependent code in StartCapture.
209 :
210 : private:
211 : void UpdateFrameCount();
212 : uint32_t CalculateFrameRate(int64_t now_ns);
213 :
214 : CriticalSectionWrapper& _callBackCs;
215 :
216 : rtc::VideoSinkInterface<VideoFrame>* _dataCallBack;
217 :
218 : int64_t _incomingFrameTimesNanos[kFrameRateCountHistorySize];// timestamp for local captured frames
219 : VideoRotation _rotateFrame; //Set if the frame should be rotated by the capture module.
220 :
221 : // Used to make sure incoming timestamp is increasing for every frame.
222 : int64_t last_capture_time_;
223 :
224 : // Delta used for translating between NTP and internal timestamps.
225 : const int64_t delta_ntp_internal_ms_;
226 :
227 : // DesktopCapturer::Callback interface.
228 : void OnCaptureResult(DesktopCapturer::Result result,
229 : std::unique_ptr<DesktopFrame> frame) override;
230 :
231 : public:
232 0 : static bool Run(void*obj) {
233 0 : static_cast<DesktopCaptureImpl*>(obj)->process();
234 0 : return true;
235 : };
236 : void process();
237 :
238 : private:
239 : std::unique_ptr<DesktopAndCursorComposer> desktop_capturer_cursor_composer_;
240 : std::unique_ptr<EventWrapper> time_event_;
241 : #if defined(_WIN32)
242 : std::unique_ptr<rtc::PlatformUIThread> capturer_thread_;
243 : #else
244 : std::unique_ptr<rtc::PlatformThread> capturer_thread_;
245 : #endif
246 : mutable uint32_t mRefCount;
247 : bool started_;
248 : };
249 :
250 : } // namespace webrtc
251 :
252 : #endif // WEBRTC_MODULES_DESKTOP_CAPTURE_MAIN_SOURCE_DESKTOP_CAPTURE_IMPL_H_
|