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