| OLD | NEW |
| 1 /* | 1 /* |
| 2 * libjingle | 2 * libjingle |
| 3 * Copyright 2010 Google Inc. | 3 * Copyright 2010 Google Inc. |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions are met: | 6 * modification, are permitted provided that the following conditions are met: |
| 7 * | 7 * |
| 8 * 1. Redistributions of source code must retain the above copyright notice, | 8 * 1. Redistributions of source code must retain the above copyright notice, |
| 9 * this list of conditions and the following disclaimer. | 9 * this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright notice, | 10 * 2. Redistributions in binary form must reproduce the above copyright notice, |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 #include "webrtc/base/messagehandler.h" | 44 #include "webrtc/base/messagehandler.h" |
| 45 #include "webrtc/base/rollingaccumulator.h" | 45 #include "webrtc/base/rollingaccumulator.h" |
| 46 #include "webrtc/base/scoped_ptr.h" | 46 #include "webrtc/base/scoped_ptr.h" |
| 47 #include "webrtc/base/sigslot.h" | 47 #include "webrtc/base/sigslot.h" |
| 48 #include "webrtc/base/thread.h" | 48 #include "webrtc/base/thread.h" |
| 49 #include "webrtc/base/timing.h" | 49 #include "webrtc/base/timing.h" |
| 50 | 50 |
| 51 | 51 |
| 52 namespace cricket { | 52 namespace cricket { |
| 53 | 53 |
| 54 class VideoProcessor; | |
| 55 | |
| 56 // Current state of the capturer. | 54 // Current state of the capturer. |
| 57 // TODO(hellner): CS_NO_DEVICE is an error code not a capture state. Separate | 55 // TODO(hellner): CS_NO_DEVICE is an error code not a capture state. Separate |
| 58 // error codes and states. | 56 // error codes and states. |
| 59 enum CaptureState { | 57 enum CaptureState { |
| 60 CS_STOPPED, // The capturer has been stopped or hasn't started yet. | 58 CS_STOPPED, // The capturer has been stopped or hasn't started yet. |
| 61 CS_STARTING, // The capturer is in the process of starting. Note, it may | 59 CS_STARTING, // The capturer is in the process of starting. Note, it may |
| 62 // still fail to start. | 60 // still fail to start. |
| 63 CS_RUNNING, // The capturer has been started successfully and is now | 61 CS_RUNNING, // The capturer has been started successfully and is now |
| 64 // capturing. | 62 // capturing. |
| 65 CS_PAUSED, // The capturer has been paused. | 63 CS_PAUSED, // The capturer has been paused. |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 private: | 106 private: |
| 109 DISALLOW_COPY_AND_ASSIGN(CapturedFrame); | 107 DISALLOW_COPY_AND_ASSIGN(CapturedFrame); |
| 110 }; | 108 }; |
| 111 | 109 |
| 112 // VideoCapturer is an abstract class that defines the interfaces for video | 110 // VideoCapturer is an abstract class that defines the interfaces for video |
| 113 // capturing. The subclasses implement the video capturer for various types of | 111 // capturing. The subclasses implement the video capturer for various types of |
| 114 // capturers and various platforms. | 112 // capturers and various platforms. |
| 115 // | 113 // |
| 116 // The captured frames may need to be adapted (for example, cropping). | 114 // The captured frames may need to be adapted (for example, cropping). |
| 117 // Video adaptation is built into and enabled by default. After a frame has | 115 // Video adaptation is built into and enabled by default. After a frame has |
| 118 // been captured from the device, it is sent to the video adapter, then video | 116 // been captured from the device, it is sent to the video adapter, then out to |
| 119 // processors, then out to the encoder. | 117 // the encoder. |
| 120 // | 118 // |
| 121 // Programming model: | 119 // Programming model: |
| 122 // Create an object of a subclass of VideoCapturer | 120 // Create an object of a subclass of VideoCapturer |
| 123 // Initialize | 121 // Initialize |
| 124 // SignalStateChange.connect() | 122 // SignalStateChange.connect() |
| 125 // SignalFrameCaptured.connect() | 123 // SignalFrameCaptured.connect() |
| 126 // Find the capture format for Start() by either calling GetSupportedFormats() | 124 // Find the capture format for Start() by either calling GetSupportedFormats() |
| 127 // and selecting one of the supported or calling GetBestCaptureFormat(). | 125 // and selecting one of the supported or calling GetBestCaptureFormat(). |
| 128 // video_adapter()->OnOutputFormatRequest(desired_encoding_format) | 126 // video_adapter()->OnOutputFormatRequest(desired_encoding_format) |
| 129 // Start() | 127 // Start() |
| 130 // GetCaptureFormat() optionally | 128 // GetCaptureFormat() optionally |
| 131 // Stop() | 129 // Stop() |
| 132 // | 130 // |
| 133 // Assumption: | 131 // Assumption: |
| 134 // The Start() and Stop() methods are called by a single thread (E.g., the | 132 // The Start() and Stop() methods are called by a single thread (E.g., the |
| 135 // media engine thread). Hence, the VideoCapture subclasses dont need to be | 133 // media engine thread). Hence, the VideoCapture subclasses dont need to be |
| 136 // thread safe. | 134 // thread safe. |
| 137 // | 135 // |
| 138 class VideoCapturer | 136 class VideoCapturer |
| 139 : public sigslot::has_slots<>, | 137 : public sigslot::has_slots<>, |
| 140 public rtc::MessageHandler { | 138 public rtc::MessageHandler { |
| 141 public: | 139 public: |
| 142 typedef std::vector<VideoProcessor*> VideoProcessors; | |
| 143 | |
| 144 // All signals are marshalled to |thread| or the creating thread if | 140 // All signals are marshalled to |thread| or the creating thread if |
| 145 // none is provided. | 141 // none is provided. |
| 146 VideoCapturer(); | 142 VideoCapturer(); |
| 147 explicit VideoCapturer(rtc::Thread* thread); | 143 explicit VideoCapturer(rtc::Thread* thread); |
| 148 virtual ~VideoCapturer() {} | 144 virtual ~VideoCapturer() {} |
| 149 | 145 |
| 150 // Gets the id of the underlying device, which is available after the capturer | 146 // Gets the id of the underlying device, which is available after the capturer |
| 151 // is initialized. Can be used to determine if two capturers reference the | 147 // is initialized. Can be used to determine if two capturers reference the |
| 152 // same device. | 148 // same device. |
| 153 const std::string& GetId() const { return id_; } | 149 const std::string& GetId() const { return id_; } |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 return capture_state_; | 222 return capture_state_; |
| 227 } | 223 } |
| 228 | 224 |
| 229 // Tells videocapturer whether to apply the pending rotation. By default, the | 225 // Tells videocapturer whether to apply the pending rotation. By default, the |
| 230 // rotation is applied and the generated frame is up right. When set to false, | 226 // rotation is applied and the generated frame is up right. When set to false, |
| 231 // generated frames will carry the rotation information from | 227 // generated frames will carry the rotation information from |
| 232 // SetCaptureRotation. Return value indicates whether this operation succeeds. | 228 // SetCaptureRotation. Return value indicates whether this operation succeeds. |
| 233 virtual bool SetApplyRotation(bool enable); | 229 virtual bool SetApplyRotation(bool enable); |
| 234 virtual bool GetApplyRotation() { return apply_rotation_; } | 230 virtual bool GetApplyRotation() { return apply_rotation_; } |
| 235 | 231 |
| 236 // Adds a video processor that will be applied on VideoFrames returned by | |
| 237 // |SignalVideoFrame|. Multiple video processors can be added. The video | |
| 238 // processors will be applied in the order they were added. | |
| 239 void AddVideoProcessor(VideoProcessor* video_processor); | |
| 240 // Removes the |video_processor| from the list of video processors or | |
| 241 // returns false. | |
| 242 bool RemoveVideoProcessor(VideoProcessor* video_processor); | |
| 243 | |
| 244 // Returns true if the capturer is screencasting. This can be used to | 232 // Returns true if the capturer is screencasting. This can be used to |
| 245 // implement screencast specific behavior. | 233 // implement screencast specific behavior. |
| 246 virtual bool IsScreencast() const = 0; | 234 virtual bool IsScreencast() const = 0; |
| 247 | 235 |
| 248 // Caps the VideoCapturer's format according to max_format. It can e.g. be | 236 // Caps the VideoCapturer's format according to max_format. It can e.g. be |
| 249 // used to prevent cameras from capturing at a resolution or framerate that | 237 // used to prevent cameras from capturing at a resolution or framerate that |
| 250 // the capturer is capable of but not performing satisfactorily at. | 238 // the capturer is capable of but not performing satisfactorily at. |
| 251 // The capping is an upper bound for each component of the capturing format. | 239 // The capping is an upper bound for each component of the capturing format. |
| 252 // The fourcc component is ignored. | 240 // The fourcc component is ignored. |
| 253 void ConstrainSupportedFormats(const VideoFormat& max_format); | 241 void ConstrainSupportedFormats(const VideoFormat& max_format); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 275 // at any time which is needed since the signal may be called from an | 263 // at any time which is needed since the signal may be called from an |
| 276 // unmarshalled thread owned by the VideoCapturer. | 264 // unmarshalled thread owned by the VideoCapturer. |
| 277 // Signal the captured frame to downstream. | 265 // Signal the captured frame to downstream. |
| 278 sigslot::signal2<VideoCapturer*, const CapturedFrame*, | 266 sigslot::signal2<VideoCapturer*, const CapturedFrame*, |
| 279 sigslot::multi_threaded_local> SignalFrameCaptured; | 267 sigslot::multi_threaded_local> SignalFrameCaptured; |
| 280 // Signal the captured and possibly adapted frame to downstream consumers | 268 // Signal the captured and possibly adapted frame to downstream consumers |
| 281 // such as the encoder. | 269 // such as the encoder. |
| 282 sigslot::signal2<VideoCapturer*, const VideoFrame*, | 270 sigslot::signal2<VideoCapturer*, const VideoFrame*, |
| 283 sigslot::multi_threaded_local> SignalVideoFrame; | 271 sigslot::multi_threaded_local> SignalVideoFrame; |
| 284 | 272 |
| 285 const VideoProcessors& video_processors() const { return video_processors_; } | |
| 286 | |
| 287 // If 'screencast_max_pixels' is set greater than zero, screencasts will be | 273 // If 'screencast_max_pixels' is set greater than zero, screencasts will be |
| 288 // scaled to be no larger than this value. | 274 // scaled to be no larger than this value. |
| 289 // If set to zero, the max pixels will be limited to | 275 // If set to zero, the max pixels will be limited to |
| 290 // Retina MacBookPro 15" resolution of 2880 x 1800. | 276 // Retina MacBookPro 15" resolution of 2880 x 1800. |
| 291 // For high fps, maximum pixels limit is set based on common 24" monitor | 277 // For high fps, maximum pixels limit is set based on common 24" monitor |
| 292 // resolution of 2048 x 1280. | 278 // resolution of 2048 x 1280. |
| 293 int screencast_max_pixels() const { return screencast_max_pixels_; } | 279 int screencast_max_pixels() const { return screencast_max_pixels_; } |
| 294 void set_screencast_max_pixels(int p) { | 280 void set_screencast_max_pixels(int p) { |
| 295 screencast_max_pixels_ = std::max(0, p); | 281 screencast_max_pixels_ = std::max(0, p); |
| 296 } | 282 } |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 354 void Construct(); | 340 void Construct(); |
| 355 // Get the distance between the desired format and the supported format. | 341 // Get the distance between the desired format and the supported format. |
| 356 // Return the max distance if they mismatch. See the implementation for | 342 // Return the max distance if they mismatch. See the implementation for |
| 357 // details. | 343 // details. |
| 358 int64 GetFormatDistance(const VideoFormat& desired, | 344 int64 GetFormatDistance(const VideoFormat& desired, |
| 359 const VideoFormat& supported); | 345 const VideoFormat& supported); |
| 360 | 346 |
| 361 // Convert captured frame to readable string for LOG messages. | 347 // Convert captured frame to readable string for LOG messages. |
| 362 std::string ToString(const CapturedFrame* frame) const; | 348 std::string ToString(const CapturedFrame* frame) const; |
| 363 | 349 |
| 364 // Applies all registered processors. If any of the processors signal that | |
| 365 // the frame should be dropped the return value will be false. Note that | |
| 366 // this frame should be dropped as it has not applied all processors. | |
| 367 bool ApplyProcessors(VideoFrame* video_frame); | |
| 368 | |
| 369 // Updates filtered_supported_formats_ so that it contains the formats in | 350 // Updates filtered_supported_formats_ so that it contains the formats in |
| 370 // supported_formats_ that fulfill all applied restrictions. | 351 // supported_formats_ that fulfill all applied restrictions. |
| 371 void UpdateFilteredSupportedFormats(); | 352 void UpdateFilteredSupportedFormats(); |
| 372 // Returns true if format doesn't fulfill all applied restrictions. | 353 // Returns true if format doesn't fulfill all applied restrictions. |
| 373 bool ShouldFilterFormat(const VideoFormat& format) const; | 354 bool ShouldFilterFormat(const VideoFormat& format) const; |
| 374 | 355 |
| 375 void UpdateStats(const CapturedFrame* captured_frame); | 356 void UpdateStats(const CapturedFrame* captured_frame); |
| 376 | 357 |
| 377 // Helper function to save statistics on the current data from a | 358 // Helper function to save statistics on the current data from a |
| 378 // RollingAccumulator into stats. | 359 // RollingAccumulator into stats. |
| (...skipping 22 matching lines...) Expand all Loading... |
| 401 int black_frame_count_down_; | 382 int black_frame_count_down_; |
| 402 | 383 |
| 403 bool enable_video_adapter_; | 384 bool enable_video_adapter_; |
| 404 CoordinatedVideoAdapter video_adapter_; | 385 CoordinatedVideoAdapter video_adapter_; |
| 405 | 386 |
| 406 rtc::Timing frame_length_time_reporter_; | 387 rtc::Timing frame_length_time_reporter_; |
| 407 rtc::CriticalSection frame_stats_crit_; | 388 rtc::CriticalSection frame_stats_crit_; |
| 408 | 389 |
| 409 int adapt_frame_drops_; | 390 int adapt_frame_drops_; |
| 410 rtc::RollingAccumulator<int> adapt_frame_drops_data_; | 391 rtc::RollingAccumulator<int> adapt_frame_drops_data_; |
| 411 int effect_frame_drops_; | |
| 412 rtc::RollingAccumulator<int> effect_frame_drops_data_; | |
| 413 double previous_frame_time_; | 392 double previous_frame_time_; |
| 414 rtc::RollingAccumulator<double> frame_time_data_; | 393 rtc::RollingAccumulator<double> frame_time_data_; |
| 415 // The captured frame format before potential adapation. | 394 // The captured frame format before potential adapation. |
| 416 VideoFormat last_captured_frame_format_; | 395 VideoFormat last_captured_frame_format_; |
| 417 | 396 |
| 418 rtc::CriticalSection crit_; | |
| 419 VideoProcessors video_processors_; | |
| 420 | |
| 421 // Whether capturer should apply rotation to the frame before signaling it. | 397 // Whether capturer should apply rotation to the frame before signaling it. |
| 422 bool apply_rotation_; | 398 bool apply_rotation_; |
| 423 | 399 |
| 424 DISALLOW_COPY_AND_ASSIGN(VideoCapturer); | 400 DISALLOW_COPY_AND_ASSIGN(VideoCapturer); |
| 425 }; | 401 }; |
| 426 | 402 |
| 427 } // namespace cricket | 403 } // namespace cricket |
| 428 | 404 |
| 429 #endif // TALK_MEDIA_BASE_VIDEOCAPTURER_H_ | 405 #endif // TALK_MEDIA_BASE_VIDEOCAPTURER_H_ |
| OLD | NEW |