OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2010 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2010 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 // Declaration of abstract class VideoCapturer | 11 // Declaration of abstract class VideoCapturer |
12 | 12 |
13 #ifndef WEBRTC_MEDIA_BASE_VIDEOCAPTURER_H_ | 13 #ifndef WEBRTC_MEDIA_BASE_VIDEOCAPTURER_H_ |
14 #define WEBRTC_MEDIA_BASE_VIDEOCAPTURER_H_ | 14 #define WEBRTC_MEDIA_BASE_VIDEOCAPTURER_H_ |
15 | 15 |
16 #include <algorithm> | 16 #include <algorithm> |
17 #include <string> | 17 #include <string> |
18 #include <vector> | 18 #include <vector> |
19 | 19 |
20 #include "webrtc/base/basictypes.h" | 20 #include "webrtc/base/basictypes.h" |
21 #include "webrtc/base/criticalsection.h" | 21 #include "webrtc/base/criticalsection.h" |
22 #include "webrtc/media/base/videosourceinterface.h" | 22 #include "webrtc/media/base/videosourceinterface.h" |
23 #include "webrtc/base/messagehandler.h" | |
24 #include "webrtc/base/rollingaccumulator.h" | 23 #include "webrtc/base/rollingaccumulator.h" |
25 #include "webrtc/base/scoped_ptr.h" | 24 #include "webrtc/base/scoped_ptr.h" |
26 #include "webrtc/base/sigslot.h" | 25 #include "webrtc/base/sigslot.h" |
27 #include "webrtc/base/thread.h" | |
28 #include "webrtc/base/timing.h" | 26 #include "webrtc/base/timing.h" |
| 27 #include "webrtc/base/thread_checker.h" |
29 #include "webrtc/media/base/mediachannel.h" | 28 #include "webrtc/media/base/mediachannel.h" |
30 #include "webrtc/media/base/videoadapter.h" | 29 #include "webrtc/media/base/videoadapter.h" |
31 #include "webrtc/media/base/videobroadcaster.h" | 30 #include "webrtc/media/base/videobroadcaster.h" |
32 #include "webrtc/media/base/videocommon.h" | 31 #include "webrtc/media/base/videocommon.h" |
33 #include "webrtc/media/base/videoframefactory.h" | 32 #include "webrtc/media/base/videoframefactory.h" |
34 #include "webrtc/media/devices/devicemanager.h" | 33 #include "webrtc/media/devices/devicemanager.h" |
35 | 34 |
36 | 35 |
37 namespace cricket { | 36 namespace cricket { |
38 | 37 |
39 // Current state of the capturer. | 38 // Current state of the capturer. |
40 // TODO(hellner): CS_NO_DEVICE is an error code not a capture state. Separate | |
41 // error codes and states. | |
42 enum CaptureState { | 39 enum CaptureState { |
43 CS_STOPPED, // The capturer has been stopped or hasn't started yet. | 40 CS_STOPPED, // The capturer has been stopped or hasn't started yet. |
44 CS_STARTING, // The capturer is in the process of starting. Note, it may | 41 CS_STARTING, // The capturer is in the process of starting. Note, it may |
45 // still fail to start. | 42 // still fail to start. |
46 CS_RUNNING, // The capturer has been started successfully and is now | 43 CS_RUNNING, // The capturer has been started successfully and is now |
47 // capturing. | 44 // capturing. |
48 CS_PAUSED, // The capturer has been paused. | 45 CS_PAUSED, // The capturer has been paused. |
49 CS_FAILED, // The capturer failed to start. | 46 CS_FAILED, // The capturer failed to start. |
50 CS_NO_DEVICE, // The capturer has no device and consequently failed to start. | |
51 }; | 47 }; |
52 | 48 |
53 class VideoFrame; | 49 class VideoFrame; |
54 | 50 |
55 struct CapturedFrame { | 51 struct CapturedFrame { |
56 static const uint32_t kFrameHeaderSize = 40; // Size from width to data_size. | 52 static const uint32_t kFrameHeaderSize = 40; // Size from width to data_size. |
57 static const uint32_t kUnknownDataSize = 0xFFFFFFFF; | 53 static const uint32_t kUnknownDataSize = 0xFFFFFFFF; |
58 | 54 |
59 CapturedFrame(); | 55 CapturedFrame(); |
60 | 56 |
(...skipping 23 matching lines...) Expand all Loading... |
84 RTC_DISALLOW_COPY_AND_ASSIGN(CapturedFrame); | 80 RTC_DISALLOW_COPY_AND_ASSIGN(CapturedFrame); |
85 }; | 81 }; |
86 | 82 |
87 // VideoCapturer is an abstract class that defines the interfaces for video | 83 // VideoCapturer is an abstract class that defines the interfaces for video |
88 // capturing. The subclasses implement the video capturer for various types of | 84 // capturing. The subclasses implement the video capturer for various types of |
89 // capturers and various platforms. | 85 // capturers and various platforms. |
90 // | 86 // |
91 // The captured frames may need to be adapted (for example, cropping). | 87 // The captured frames may need to be adapted (for example, cropping). |
92 // Video adaptation is built into and enabled by default. After a frame has | 88 // Video adaptation is built into and enabled by default. After a frame has |
93 // been captured from the device, it is sent to the video adapter, then out to | 89 // been captured from the device, it is sent to the video adapter, then out to |
94 // the encoder. | 90 // the sinks. |
95 // | 91 // |
96 // Programming model: | 92 // Programming model: |
97 // Create an object of a subclass of VideoCapturer | 93 // Create an object of a subclass of VideoCapturer |
98 // Initialize | 94 // Initialize |
99 // SignalStateChange.connect() | 95 // SignalStateChange.connect() |
100 // SignalFrameCaptured.connect() | 96 // AddOrUpdateSink() |
101 // Find the capture format for Start() by either calling GetSupportedFormats() | 97 // Find the capture format for Start() by either calling GetSupportedFormats() |
102 // and selecting one of the supported or calling GetBestCaptureFormat(). | 98 // and selecting one of the supported or calling GetBestCaptureFormat(). |
103 // video_adapter()->OnOutputFormatRequest(desired_encoding_format) | 99 // video_adapter()->OnOutputFormatRequest(desired_encoding_format) |
104 // Start() | 100 // Start() |
105 // GetCaptureFormat() optionally | 101 // GetCaptureFormat() optionally |
106 // Stop() | 102 // Stop() |
107 // | 103 // |
108 // Assumption: | 104 // Assumption: |
109 // The Start() and Stop() methods are called by a single thread (E.g., the | 105 // The Start() and Stop() methods are called by a single thread (E.g., the |
110 // media engine thread). Hence, the VideoCapture subclasses dont need to be | 106 // media engine thread). Hence, the VideoCapture subclasses dont need to be |
111 // thread safe. | 107 // thread safe. |
112 // | 108 // |
113 class VideoCapturer : public sigslot::has_slots<>, | 109 class VideoCapturer : public sigslot::has_slots<>, |
114 public rtc::MessageHandler, | |
115 public rtc::VideoSourceInterface<cricket::VideoFrame> { | 110 public rtc::VideoSourceInterface<cricket::VideoFrame> { |
116 public: | 111 public: |
117 // All signals are marshalled to |thread| or the creating thread if | |
118 // none is provided. | |
119 VideoCapturer(); | 112 VideoCapturer(); |
120 explicit VideoCapturer(rtc::Thread* thread); | 113 |
121 virtual ~VideoCapturer() {} | 114 virtual ~VideoCapturer() {} |
122 | 115 |
123 // Gets the id of the underlying device, which is available after the capturer | 116 // Gets the id of the underlying device, which is available after the capturer |
124 // is initialized. Can be used to determine if two capturers reference the | 117 // is initialized. Can be used to determine if two capturers reference the |
125 // same device. | 118 // same device. |
126 const std::string& GetId() const { return id_; } | 119 const std::string& GetId() const { return id_; } |
127 | 120 |
128 // Get the capture formats supported by the video capturer. The supported | 121 // Get the capture formats supported by the video capturer. The supported |
129 // formats are non empty after the device has been opened successfully. | 122 // formats are non empty after the device has been opened successfully. |
130 const std::vector<VideoFormat>* GetSupportedFormats() const; | 123 const std::vector<VideoFormat>* GetSupportedFormats() const; |
(...skipping 25 matching lines...) Expand all Loading... |
156 // GetSupportedFormats() and selecting one of the supported | 149 // GetSupportedFormats() and selecting one of the supported |
157 // or calling GetBestCaptureFormat(). | 150 // or calling GetBestCaptureFormat(). |
158 // Return | 151 // Return |
159 // CS_STARTING: The capturer is trying to start. Success or failure will | 152 // CS_STARTING: The capturer is trying to start. Success or failure will |
160 // be notified via the |SignalStateChange| callback. | 153 // be notified via the |SignalStateChange| callback. |
161 // CS_RUNNING: if the capturer is started and capturing. | 154 // CS_RUNNING: if the capturer is started and capturing. |
162 // CS_PAUSED: Will never be returned. | 155 // CS_PAUSED: Will never be returned. |
163 // CS_FAILED: if the capturer failes to start.. | 156 // CS_FAILED: if the capturer failes to start.. |
164 // CS_NO_DEVICE: if the capturer has no device and fails to start. | 157 // CS_NO_DEVICE: if the capturer has no device and fails to start. |
165 virtual CaptureState Start(const VideoFormat& capture_format) = 0; | 158 virtual CaptureState Start(const VideoFormat& capture_format) = 0; |
166 // Sets the desired aspect ratio. If the capturer is capturing at another | |
167 // aspect ratio it will crop the width or the height so that asked for | |
168 // aspect ratio is acheived. Note that ratio_w and ratio_h do not need to be | |
169 // relatively prime. | |
170 void UpdateAspectRatio(int ratio_w, int ratio_h); | |
171 void ClearAspectRatio(); | |
172 | 159 |
173 // Get the current capture format, which is set by the Start() call. | 160 // Get the current capture format, which is set by the Start() call. |
174 // Note that the width and height of the captured frames may differ from the | 161 // Note that the width and height of the captured frames may differ from the |
175 // capture format. For example, the capture format is HD but the captured | 162 // capture format. For example, the capture format is HD but the captured |
176 // frames may be smaller than HD. | 163 // frames may be smaller than HD. |
177 const VideoFormat* GetCaptureFormat() const { | 164 const VideoFormat* GetCaptureFormat() const { |
178 return capture_format_.get(); | 165 return capture_format_.get(); |
179 } | 166 } |
180 | 167 |
181 // Pause the video capturer. | |
182 virtual bool Pause(bool paused); | |
183 // Stop the video capturer. | 168 // Stop the video capturer. |
184 virtual void Stop() = 0; | 169 virtual void Stop() = 0; |
185 // Check if the video capturer is running. | 170 // Check if the video capturer is running. |
186 virtual bool IsRunning() = 0; | 171 virtual bool IsRunning() = 0; |
187 // Restart the video capturer with the new |capture_format|. | |
188 // Default implementation stops and starts the capturer. | |
189 virtual bool Restart(const VideoFormat& capture_format); | |
190 // TODO(thorcarpenter): This behavior of keeping the camera open just to emit | |
191 // black frames is a total hack and should be fixed. | |
192 // When muting, produce black frames then pause the camera. | |
193 // When unmuting, start the camera. Camera starts unmuted. | |
194 virtual bool MuteToBlackThenPause(bool muted); | |
195 virtual bool IsMuted() const { | |
196 return muted_; | |
197 } | |
198 CaptureState capture_state() const { | 172 CaptureState capture_state() const { |
199 return capture_state_; | 173 return capture_state_; |
200 } | 174 } |
201 | 175 |
202 virtual bool GetApplyRotation() { return apply_rotation_; } | 176 virtual bool GetApplyRotation() { return apply_rotation_; } |
203 | 177 |
204 // Returns true if the capturer is screencasting. This can be used to | 178 // Returns true if the capturer is screencasting. This can be used to |
205 // implement screencast specific behavior. | 179 // implement screencast specific behavior. |
206 virtual bool IsScreencast() const = 0; | 180 virtual bool IsScreencast() const = 0; |
207 | 181 |
208 // Caps the VideoCapturer's format according to max_format. It can e.g. be | 182 // Caps the VideoCapturer's format according to max_format. It can e.g. be |
209 // used to prevent cameras from capturing at a resolution or framerate that | 183 // used to prevent cameras from capturing at a resolution or framerate that |
210 // the capturer is capable of but not performing satisfactorily at. | 184 // the capturer is capable of but not performing satisfactorily at. |
211 // The capping is an upper bound for each component of the capturing format. | 185 // The capping is an upper bound for each component of the capturing format. |
212 // The fourcc component is ignored. | 186 // The fourcc component is ignored. |
213 void ConstrainSupportedFormats(const VideoFormat& max_format); | 187 void ConstrainSupportedFormats(const VideoFormat& max_format); |
214 | 188 |
215 void set_enable_camera_list(bool enable_camera_list) { | 189 void set_enable_camera_list(bool enable_camera_list) { |
216 enable_camera_list_ = enable_camera_list; | 190 enable_camera_list_ = enable_camera_list; |
217 } | 191 } |
218 bool enable_camera_list() { | 192 bool enable_camera_list() { |
219 return enable_camera_list_; | 193 return enable_camera_list_; |
220 } | 194 } |
221 | 195 |
222 // Enable scaling to ensure square pixels. | |
223 void set_square_pixel_aspect_ratio(bool square_pixel_aspect_ratio) { | |
224 square_pixel_aspect_ratio_ = square_pixel_aspect_ratio; | |
225 } | |
226 bool square_pixel_aspect_ratio() { | |
227 return square_pixel_aspect_ratio_; | |
228 } | |
229 | |
230 // Signal all capture state changes that are not a direct result of calling | 196 // Signal all capture state changes that are not a direct result of calling |
231 // Start(). | 197 // Start(). |
232 sigslot::signal2<VideoCapturer*, CaptureState> SignalStateChange; | 198 sigslot::signal2<VideoCapturer*, CaptureState> SignalStateChange; |
233 // Frame callbacks are multithreaded to allow disconnect and connect to be | 199 // Frame callbacks are multithreaded to allow disconnect and connect to be |
234 // called concurrently. It also ensures that it is safe to call disconnect | 200 // called concurrently. It also ensures that it is safe to call disconnect |
235 // at any time which is needed since the signal may be called from an | 201 // at any time which is needed since the signal may be called from an |
236 // unmarshalled thread owned by the VideoCapturer. | 202 // unmarshalled thread owned by the VideoCapturer. |
237 // Signal the captured frame to downstream. | 203 // Signal the captured frame to downstream. |
238 sigslot::signal2<VideoCapturer*, const CapturedFrame*, | 204 sigslot::signal2<VideoCapturer*, const CapturedFrame*, |
239 sigslot::multi_threaded_local> SignalFrameCaptured; | 205 sigslot::multi_threaded_local> SignalFrameCaptured; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 // Callback attached to SignalFrameCaptured where SignalVideoFrames is called. | 247 // Callback attached to SignalFrameCaptured where SignalVideoFrames is called. |
282 void OnFrameCaptured(VideoCapturer* video_capturer, | 248 void OnFrameCaptured(VideoCapturer* video_capturer, |
283 const CapturedFrame* captured_frame); | 249 const CapturedFrame* captured_frame); |
284 | 250 |
285 // Callback attached to SignalVideoFrame. | 251 // Callback attached to SignalVideoFrame. |
286 // TODO(perkj): Remove once SignalVideoFrame is removed. | 252 // TODO(perkj): Remove once SignalVideoFrame is removed. |
287 void OnFrame(VideoCapturer* capturer, const VideoFrame* frame); | 253 void OnFrame(VideoCapturer* capturer, const VideoFrame* frame); |
288 | 254 |
289 void SetCaptureState(CaptureState state); | 255 void SetCaptureState(CaptureState state); |
290 | 256 |
291 // Marshals SignalStateChange onto thread_. | |
292 void OnMessage(rtc::Message* message) override; | |
293 | |
294 // subclasses override this virtual method to provide a vector of fourccs, in | 257 // subclasses override this virtual method to provide a vector of fourccs, in |
295 // order of preference, that are expected by the media engine. | 258 // order of preference, that are expected by the media engine. |
296 virtual bool GetPreferredFourccs(std::vector<uint32_t>* fourccs) = 0; | 259 virtual bool GetPreferredFourccs(std::vector<uint32_t>* fourccs) = 0; |
297 | 260 |
298 // mutators to set private attributes | 261 // mutators to set private attributes |
299 void SetId(const std::string& id) { | 262 void SetId(const std::string& id) { |
300 id_ = id; | 263 id_ = id; |
301 } | 264 } |
302 | 265 |
303 void SetCaptureFormat(const VideoFormat* format) { | 266 void SetCaptureFormat(const VideoFormat* format) { |
(...skipping 28 matching lines...) Expand all Loading... |
332 | 295 |
333 void UpdateStats(const CapturedFrame* captured_frame); | 296 void UpdateStats(const CapturedFrame* captured_frame); |
334 | 297 |
335 // Helper function to save statistics on the current data from a | 298 // Helper function to save statistics on the current data from a |
336 // RollingAccumulator into stats. | 299 // RollingAccumulator into stats. |
337 template<class T> | 300 template<class T> |
338 static void GetVariableSnapshot( | 301 static void GetVariableSnapshot( |
339 const rtc::RollingAccumulator<T>& data, | 302 const rtc::RollingAccumulator<T>& data, |
340 VariableInfo<T>* stats); | 303 VariableInfo<T>* stats); |
341 | 304 |
342 rtc::Thread* thread_; | 305 rtc::ThreadChecker thread_checker_; |
343 std::string id_; | 306 std::string id_; |
344 CaptureState capture_state_; | 307 CaptureState capture_state_; |
345 rtc::scoped_ptr<VideoFrameFactory> frame_factory_; | 308 rtc::scoped_ptr<VideoFrameFactory> frame_factory_; |
346 rtc::scoped_ptr<VideoFormat> capture_format_; | 309 rtc::scoped_ptr<VideoFormat> capture_format_; |
347 std::vector<VideoFormat> supported_formats_; | 310 std::vector<VideoFormat> supported_formats_; |
348 rtc::scoped_ptr<VideoFormat> max_format_; | 311 rtc::scoped_ptr<VideoFormat> max_format_; |
349 std::vector<VideoFormat> filtered_supported_formats_; | 312 std::vector<VideoFormat> filtered_supported_formats_; |
350 | 313 |
351 int ratio_w_; // View resolution. e.g. 1280 x 720. | 314 int ratio_w_; // View resolution. e.g. 1280 x 720. |
352 int ratio_h_; | 315 int ratio_h_; |
353 bool enable_camera_list_; | 316 bool enable_camera_list_; |
354 bool square_pixel_aspect_ratio_; // Enable scaling to square pixels. | 317 bool square_pixel_aspect_ratio_; // Enable scaling to square pixels. |
355 int scaled_width_; // Current output size from ComputeScale. | 318 int scaled_width_; // Current output size from ComputeScale. |
356 int scaled_height_; | 319 int scaled_height_; |
357 bool muted_; | |
358 int black_frame_count_down_; | |
359 | 320 |
360 rtc::VideoBroadcaster broadcaster_; | 321 rtc::VideoBroadcaster broadcaster_; |
361 bool enable_video_adapter_; | 322 bool enable_video_adapter_; |
362 CoordinatedVideoAdapter video_adapter_; | 323 CoordinatedVideoAdapter video_adapter_; |
363 | 324 |
364 rtc::Timing frame_length_time_reporter_; | 325 rtc::Timing frame_length_time_reporter_; |
365 rtc::CriticalSection frame_stats_crit_; | 326 rtc::CriticalSection frame_stats_crit_; |
366 | 327 |
367 int adapt_frame_drops_; | 328 int adapt_frame_drops_; |
368 rtc::RollingAccumulator<int> adapt_frame_drops_data_; | 329 rtc::RollingAccumulator<int> adapt_frame_drops_data_; |
369 double previous_frame_time_; | 330 double previous_frame_time_; |
370 rtc::RollingAccumulator<double> frame_time_data_; | 331 rtc::RollingAccumulator<double> frame_time_data_; |
371 // The captured frame format before potential adapation. | 332 // The captured frame format before potential adapation. |
372 VideoFormat last_captured_frame_format_; | 333 VideoFormat last_captured_frame_format_; |
373 | 334 |
374 // Whether capturer should apply rotation to the frame before signaling it. | 335 // Whether capturer should apply rotation to the frame before signaling it. |
375 bool apply_rotation_; | 336 bool apply_rotation_; |
376 | 337 |
377 RTC_DISALLOW_COPY_AND_ASSIGN(VideoCapturer); | 338 RTC_DISALLOW_COPY_AND_ASSIGN(VideoCapturer); |
378 }; | 339 }; |
379 | 340 |
380 } // namespace cricket | 341 } // namespace cricket |
381 | 342 |
382 #endif // WEBRTC_MEDIA_BASE_VIDEOCAPTURER_H_ | 343 #endif // WEBRTC_MEDIA_BASE_VIDEOCAPTURER_H_ |
OLD | NEW |