| 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 <memory> | 17 #include <memory> |
| 18 #include <string> | 18 #include <string> |
| 19 #include <vector> | 19 #include <vector> |
| 20 | 20 |
| 21 #include "webrtc/base/basictypes.h" | 21 #include "webrtc/base/basictypes.h" |
| 22 #include "webrtc/base/criticalsection.h" | 22 #include "webrtc/base/criticalsection.h" |
| 23 #include "webrtc/media/base/videosourceinterface.h" | 23 #include "webrtc/media/base/videosourceinterface.h" |
| 24 #include "webrtc/base/messagehandler.h" | |
| 25 #include "webrtc/base/rollingaccumulator.h" | 24 #include "webrtc/base/rollingaccumulator.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. | 168 // Pause the video capturer. |
| 182 virtual bool Pause(bool paused); | 169 // DEPRECATED |
| 170 // TODO(perkj): Remove once Chrome remoting doesn't override this method. |
| 171 virtual bool Pause(bool paused) { |
| 172 RTC_NOTREACHED(); |
| 173 return false; |
| 174 } |
| 183 // Stop the video capturer. | 175 // Stop the video capturer. |
| 184 virtual void Stop() = 0; | 176 virtual void Stop() = 0; |
| 185 // Check if the video capturer is running. | 177 // Check if the video capturer is running. |
| 186 virtual bool IsRunning() = 0; | 178 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 { | 179 CaptureState capture_state() const { |
| 199 return capture_state_; | 180 return capture_state_; |
| 200 } | 181 } |
| 201 | 182 |
| 202 virtual bool GetApplyRotation() { return apply_rotation_; } | 183 virtual bool GetApplyRotation() { return apply_rotation_; } |
| 203 | 184 |
| 204 // Returns true if the capturer is screencasting. This can be used to | 185 // Returns true if the capturer is screencasting. This can be used to |
| 205 // implement screencast specific behavior. | 186 // implement screencast specific behavior. |
| 206 virtual bool IsScreencast() const = 0; | 187 virtual bool IsScreencast() const = 0; |
| 207 | 188 |
| 208 // Caps the VideoCapturer's format according to max_format. It can e.g. be | 189 // 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 | 190 // used to prevent cameras from capturing at a resolution or framerate that |
| 210 // the capturer is capable of but not performing satisfactorily at. | 191 // the capturer is capable of but not performing satisfactorily at. |
| 211 // The capping is an upper bound for each component of the capturing format. | 192 // The capping is an upper bound for each component of the capturing format. |
| 212 // The fourcc component is ignored. | 193 // The fourcc component is ignored. |
| 213 void ConstrainSupportedFormats(const VideoFormat& max_format); | 194 void ConstrainSupportedFormats(const VideoFormat& max_format); |
| 214 | 195 |
| 215 void set_enable_camera_list(bool enable_camera_list) { | 196 void set_enable_camera_list(bool enable_camera_list) { |
| 216 enable_camera_list_ = enable_camera_list; | 197 enable_camera_list_ = enable_camera_list; |
| 217 } | 198 } |
| 218 bool enable_camera_list() { | 199 bool enable_camera_list() { |
| 219 return enable_camera_list_; | 200 return enable_camera_list_; |
| 220 } | 201 } |
| 221 | 202 |
| 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 | 203 // Signal all capture state changes that are not a direct result of calling |
| 231 // Start(). | 204 // Start(). |
| 232 sigslot::signal2<VideoCapturer*, CaptureState> SignalStateChange; | 205 sigslot::signal2<VideoCapturer*, CaptureState> SignalStateChange; |
| 233 // Frame callbacks are multithreaded to allow disconnect and connect to be | 206 // Frame callbacks are multithreaded to allow disconnect and connect to be |
| 234 // called concurrently. It also ensures that it is safe to call disconnect | 207 // 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 | 208 // at any time which is needed since the signal may be called from an |
| 236 // unmarshalled thread owned by the VideoCapturer. | 209 // unmarshalled thread owned by the VideoCapturer. |
| 237 // Signal the captured frame to downstream. | 210 // Signal the captured frame to downstream. |
| 238 sigslot::signal2<VideoCapturer*, const CapturedFrame*, | 211 sigslot::signal2<VideoCapturer*, const CapturedFrame*, |
| 239 sigslot::multi_threaded_local> SignalFrameCaptured; | 212 sigslot::multi_threaded_local> SignalFrameCaptured; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 const CapturedFrame* captured_frame); | 251 const CapturedFrame* captured_frame); |
| 279 | 252 |
| 280 // Callback attached to SignalVideoFrame. | 253 // Callback attached to SignalVideoFrame. |
| 281 // TODO(perkj): Remove once SignalVideoFrame is removed. | 254 // TODO(perkj): Remove once SignalVideoFrame is removed. |
| 282 void OnFrame(VideoCapturer* capturer, const VideoFrame* frame); | 255 void OnFrame(VideoCapturer* capturer, const VideoFrame* frame); |
| 283 | 256 |
| 284 CoordinatedVideoAdapter* video_adapter() { return &video_adapter_; } | 257 CoordinatedVideoAdapter* video_adapter() { return &video_adapter_; } |
| 285 | 258 |
| 286 void SetCaptureState(CaptureState state); | 259 void SetCaptureState(CaptureState state); |
| 287 | 260 |
| 288 // Marshals SignalStateChange onto thread_. | |
| 289 void OnMessage(rtc::Message* message) override; | |
| 290 | |
| 291 // subclasses override this virtual method to provide a vector of fourccs, in | 261 // subclasses override this virtual method to provide a vector of fourccs, in |
| 292 // order of preference, that are expected by the media engine. | 262 // order of preference, that are expected by the media engine. |
| 293 virtual bool GetPreferredFourccs(std::vector<uint32_t>* fourccs) = 0; | 263 virtual bool GetPreferredFourccs(std::vector<uint32_t>* fourccs) = 0; |
| 294 | 264 |
| 295 // mutators to set private attributes | 265 // mutators to set private attributes |
| 296 void SetId(const std::string& id) { | 266 void SetId(const std::string& id) { |
| 297 id_ = id; | 267 id_ = id; |
| 298 } | 268 } |
| 299 | 269 |
| 300 void SetCaptureFormat(const VideoFormat* format) { | 270 void SetCaptureFormat(const VideoFormat* format) { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 329 | 299 |
| 330 void UpdateStats(const CapturedFrame* captured_frame); | 300 void UpdateStats(const CapturedFrame* captured_frame); |
| 331 | 301 |
| 332 // Helper function to save statistics on the current data from a | 302 // Helper function to save statistics on the current data from a |
| 333 // RollingAccumulator into stats. | 303 // RollingAccumulator into stats. |
| 334 template<class T> | 304 template<class T> |
| 335 static void GetVariableSnapshot( | 305 static void GetVariableSnapshot( |
| 336 const rtc::RollingAccumulator<T>& data, | 306 const rtc::RollingAccumulator<T>& data, |
| 337 VariableInfo<T>* stats); | 307 VariableInfo<T>* stats); |
| 338 | 308 |
| 339 rtc::Thread* thread_; | 309 rtc::ThreadChecker thread_checker_; |
| 340 std::string id_; | 310 std::string id_; |
| 341 CaptureState capture_state_; | 311 CaptureState capture_state_; |
| 342 std::unique_ptr<VideoFrameFactory> frame_factory_; | 312 std::unique_ptr<VideoFrameFactory> frame_factory_; |
| 343 std::unique_ptr<VideoFormat> capture_format_; | 313 std::unique_ptr<VideoFormat> capture_format_; |
| 344 std::vector<VideoFormat> supported_formats_; | 314 std::vector<VideoFormat> supported_formats_; |
| 345 std::unique_ptr<VideoFormat> max_format_; | 315 std::unique_ptr<VideoFormat> max_format_; |
| 346 std::vector<VideoFormat> filtered_supported_formats_; | 316 std::vector<VideoFormat> filtered_supported_formats_; |
| 347 | 317 |
| 348 int ratio_w_; // View resolution. e.g. 1280 x 720. | 318 int ratio_w_; // View resolution. e.g. 1280 x 720. |
| 349 int ratio_h_; | 319 int ratio_h_; |
| 350 bool enable_camera_list_; | 320 bool enable_camera_list_; |
| 351 bool square_pixel_aspect_ratio_; // Enable scaling to square pixels. | 321 bool square_pixel_aspect_ratio_; // Enable scaling to square pixels. |
| 352 int scaled_width_; // Current output size from ComputeScale. | 322 int scaled_width_; // Current output size from ComputeScale. |
| 353 int scaled_height_; | 323 int scaled_height_; |
| 354 bool muted_; | |
| 355 int black_frame_count_down_; | |
| 356 | 324 |
| 357 rtc::VideoBroadcaster broadcaster_; | 325 rtc::VideoBroadcaster broadcaster_; |
| 358 bool enable_video_adapter_; | 326 bool enable_video_adapter_; |
| 359 CoordinatedVideoAdapter video_adapter_; | 327 CoordinatedVideoAdapter video_adapter_; |
| 360 | 328 |
| 361 rtc::Timing frame_length_time_reporter_; | 329 rtc::Timing frame_length_time_reporter_; |
| 362 rtc::CriticalSection frame_stats_crit_; | 330 rtc::CriticalSection frame_stats_crit_; |
| 363 | 331 |
| 364 int adapt_frame_drops_; | 332 int adapt_frame_drops_; |
| 365 rtc::RollingAccumulator<int> adapt_frame_drops_data_; | 333 rtc::RollingAccumulator<int> adapt_frame_drops_data_; |
| 366 double previous_frame_time_; | 334 double previous_frame_time_; |
| 367 rtc::RollingAccumulator<double> frame_time_data_; | 335 rtc::RollingAccumulator<double> frame_time_data_; |
| 368 // The captured frame format before potential adapation. | 336 // The captured frame format before potential adapation. |
| 369 VideoFormat last_captured_frame_format_; | 337 VideoFormat last_captured_frame_format_; |
| 370 | 338 |
| 371 // Whether capturer should apply rotation to the frame before signaling it. | 339 // Whether capturer should apply rotation to the frame before signaling it. |
| 372 bool apply_rotation_; | 340 bool apply_rotation_; |
| 373 | 341 |
| 374 RTC_DISALLOW_COPY_AND_ASSIGN(VideoCapturer); | 342 RTC_DISALLOW_COPY_AND_ASSIGN(VideoCapturer); |
| 375 }; | 343 }; |
| 376 | 344 |
| 377 } // namespace cricket | 345 } // namespace cricket |
| 378 | 346 |
| 379 #endif // WEBRTC_MEDIA_BASE_VIDEOCAPTURER_H_ | 347 #endif // WEBRTC_MEDIA_BASE_VIDEOCAPTURER_H_ |
| OLD | NEW |