jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 1 | /* |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame] | 2 | * Copyright (c) 2010 The WebRTC project authors. All Rights Reserved. |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 3 | * |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame] | 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. |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 11 | // Declaration of abstract class VideoCapturer |
| 12 | |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 13 | #ifndef WEBRTC_MEDIA_BASE_VIDEOCAPTURER_H_ |
| 14 | #define WEBRTC_MEDIA_BASE_VIDEOCAPTURER_H_ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 15 | |
andresp@webrtc.org | ff689be | 2015-02-12 11:54:26 +0000 | [diff] [blame] | 16 | #include <algorithm> |
kwiberg | 686a8ef | 2016-02-26 03:00:35 -0800 | [diff] [blame] | 17 | #include <memory> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 18 | #include <string> |
| 19 | #include <vector> |
| 20 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 21 | #include "webrtc/base/basictypes.h" |
| 22 | #include "webrtc/base/criticalsection.h" |
Per | a509241 | 2016-02-12 13:30:57 +0100 | [diff] [blame] | 23 | #include "webrtc/media/base/videosourceinterface.h" |
perkj | 74622e0 | 2016-02-26 02:54:38 -0800 | [diff] [blame] | 24 | #include "webrtc/base/messagehandler.h" |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 25 | #include "webrtc/base/rollingaccumulator.h" |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 26 | #include "webrtc/base/sigslot.h" |
perkj | 74622e0 | 2016-02-26 02:54:38 -0800 | [diff] [blame] | 27 | #include "webrtc/base/thread.h" |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 28 | #include "webrtc/base/timing.h" |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 29 | #include "webrtc/media/base/mediachannel.h" |
| 30 | #include "webrtc/media/base/videoadapter.h" |
Per | a509241 | 2016-02-12 13:30:57 +0100 | [diff] [blame] | 31 | #include "webrtc/media/base/videobroadcaster.h" |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 32 | #include "webrtc/media/base/videocommon.h" |
| 33 | #include "webrtc/media/base/videoframefactory.h" |
| 34 | #include "webrtc/media/devices/devicemanager.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 35 | |
| 36 | |
| 37 | namespace cricket { |
| 38 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 39 | // Current state of the capturer. |
perkj | 74622e0 | 2016-02-26 02:54:38 -0800 | [diff] [blame] | 40 | // TODO(hellner): CS_NO_DEVICE is an error code not a capture state. Separate |
| 41 | // error codes and states. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 42 | enum CaptureState { |
| 43 | 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 |
| 45 | // still fail to start. |
| 46 | CS_RUNNING, // The capturer has been started successfully and is now |
| 47 | // capturing. |
| 48 | CS_PAUSED, // The capturer has been paused. |
| 49 | CS_FAILED, // The capturer failed to start. |
perkj | 74622e0 | 2016-02-26 02:54:38 -0800 | [diff] [blame] | 50 | CS_NO_DEVICE, // The capturer has no device and consequently failed to start. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | class VideoFrame; |
| 54 | |
| 55 | struct CapturedFrame { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 56 | static const uint32_t kFrameHeaderSize = 40; // Size from width to data_size. |
| 57 | static const uint32_t kUnknownDataSize = 0xFFFFFFFF; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 58 | |
| 59 | CapturedFrame(); |
| 60 | |
| 61 | // Get the number of bytes of the frame data. If data_size is known, return |
| 62 | // it directly. Otherwise, calculate the size based on width, height, and |
| 63 | // fourcc. Return true if succeeded. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 64 | bool GetDataSize(uint32_t* size) const; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 65 | |
| 66 | // The width and height of the captured frame could be different from those |
| 67 | // of VideoFormat. Once the first frame is captured, the width, height, |
| 68 | // fourcc, pixel_width, and pixel_height should keep the same over frames. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 69 | int width; // in number of pixels |
| 70 | int height; // in number of pixels |
| 71 | uint32_t fourcc; // compression |
| 72 | uint32_t pixel_width; // width of a pixel, default is 1 |
| 73 | uint32_t pixel_height; // height of a pixel, default is 1 |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 74 | int64_t time_stamp; // timestamp of when the frame was captured, in unix |
| 75 | // time with nanosecond units. |
| 76 | uint32_t data_size; // number of bytes of the frame data |
guoweis@webrtc.org | 6c930c7 | 2015-02-09 01:28:12 +0000 | [diff] [blame] | 77 | |
Per | a509241 | 2016-02-12 13:30:57 +0100 | [diff] [blame] | 78 | webrtc::VideoRotation rotation; // rotation in degrees of the frame. |
guoweis@webrtc.org | 6c930c7 | 2015-02-09 01:28:12 +0000 | [diff] [blame] | 79 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 80 | void* data; // pointer to the frame data. This object allocates the |
| 81 | // memory or points to an existing memory. |
| 82 | |
| 83 | private: |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 84 | RTC_DISALLOW_COPY_AND_ASSIGN(CapturedFrame); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | // VideoCapturer is an abstract class that defines the interfaces for video |
| 88 | // capturing. The subclasses implement the video capturer for various types of |
| 89 | // capturers and various platforms. |
| 90 | // |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 91 | // 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 |
Magnus Jedvert | c232096 | 2015-08-21 11:40:30 +0200 | [diff] [blame] | 93 | // been captured from the device, it is sent to the video adapter, then out to |
perkj | 74622e0 | 2016-02-26 02:54:38 -0800 | [diff] [blame] | 94 | // the encoder. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 95 | // |
| 96 | // Programming model: |
| 97 | // Create an object of a subclass of VideoCapturer |
| 98 | // Initialize |
| 99 | // SignalStateChange.connect() |
perkj | 74622e0 | 2016-02-26 02:54:38 -0800 | [diff] [blame] | 100 | // SignalFrameCaptured.connect() |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 101 | // Find the capture format for Start() by either calling GetSupportedFormats() |
| 102 | // and selecting one of the supported or calling GetBestCaptureFormat(). |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 103 | // video_adapter()->OnOutputFormatRequest(desired_encoding_format) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 104 | // Start() |
| 105 | // GetCaptureFormat() optionally |
| 106 | // Stop() |
| 107 | // |
| 108 | // Assumption: |
| 109 | // 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 |
| 111 | // thread safe. |
| 112 | // |
Per | a509241 | 2016-02-12 13:30:57 +0100 | [diff] [blame] | 113 | class VideoCapturer : public sigslot::has_slots<>, |
perkj | 74622e0 | 2016-02-26 02:54:38 -0800 | [diff] [blame] | 114 | public rtc::MessageHandler, |
Per | a509241 | 2016-02-12 13:30:57 +0100 | [diff] [blame] | 115 | public rtc::VideoSourceInterface<cricket::VideoFrame> { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 116 | public: |
perkj | 74622e0 | 2016-02-26 02:54:38 -0800 | [diff] [blame] | 117 | // All signals are marshalled to |thread| or the creating thread if |
| 118 | // none is provided. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 119 | VideoCapturer(); |
perkj | 74622e0 | 2016-02-26 02:54:38 -0800 | [diff] [blame] | 120 | explicit VideoCapturer(rtc::Thread* thread); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 121 | virtual ~VideoCapturer() {} |
| 122 | |
| 123 | // 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 |
| 125 | // same device. |
| 126 | const std::string& GetId() const { return id_; } |
| 127 | |
| 128 | // Get the capture formats supported by the video capturer. The supported |
| 129 | // formats are non empty after the device has been opened successfully. |
| 130 | const std::vector<VideoFormat>* GetSupportedFormats() const; |
| 131 | |
| 132 | // Get the best capture format for the desired format. The best format is the |
| 133 | // same as one of the supported formats except that the frame interval may be |
| 134 | // different. If the application asks for 16x9 and the camera does not support |
| 135 | // 16x9 HD or the application asks for 16x10, we find the closest 4x3 and then |
| 136 | // crop; Otherwise, we find what the application asks for. Note that we assume |
| 137 | // that for HD, the desired format is always 16x9. The subclasses can override |
| 138 | // the default implementation. |
| 139 | // Parameters |
| 140 | // desired: the input desired format. If desired.fourcc is not kAnyFourcc, |
| 141 | // the best capture format has the exactly same fourcc. Otherwise, |
| 142 | // the best capture format uses a fourcc in GetPreferredFourccs(). |
| 143 | // best_format: the output of the best capture format. |
| 144 | // Return false if there is no such a best format, that is, the desired format |
| 145 | // is not supported. |
| 146 | virtual bool GetBestCaptureFormat(const VideoFormat& desired, |
| 147 | VideoFormat* best_format); |
| 148 | |
| 149 | // TODO(hellner): deprecate (make private) the Start API in favor of this one. |
| 150 | // Also remove CS_STARTING as it is implied by the return |
| 151 | // value of StartCapturing(). |
| 152 | bool StartCapturing(const VideoFormat& capture_format); |
| 153 | // Start the video capturer with the specified capture format. |
| 154 | // Parameter |
| 155 | // capture_format: The caller got this parameter by either calling |
| 156 | // GetSupportedFormats() and selecting one of the supported |
| 157 | // or calling GetBestCaptureFormat(). |
| 158 | // Return |
| 159 | // CS_STARTING: The capturer is trying to start. Success or failure will |
| 160 | // be notified via the |SignalStateChange| callback. |
| 161 | // CS_RUNNING: if the capturer is started and capturing. |
| 162 | // CS_PAUSED: Will never be returned. |
| 163 | // CS_FAILED: if the capturer failes to start.. |
| 164 | // CS_NO_DEVICE: if the capturer has no device and fails to start. |
| 165 | virtual CaptureState Start(const VideoFormat& capture_format) = 0; |
perkj | 74622e0 | 2016-02-26 02:54:38 -0800 | [diff] [blame] | 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(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 172 | |
| 173 | // 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 |
| 175 | // capture format. For example, the capture format is HD but the captured |
| 176 | // frames may be smaller than HD. |
| 177 | const VideoFormat* GetCaptureFormat() const { |
| 178 | return capture_format_.get(); |
| 179 | } |
| 180 | |
perkj | 74622e0 | 2016-02-26 02:54:38 -0800 | [diff] [blame] | 181 | // Pause the video capturer. |
| 182 | virtual bool Pause(bool paused); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 183 | // Stop the video capturer. |
| 184 | virtual void Stop() = 0; |
| 185 | // Check if the video capturer is running. |
| 186 | virtual bool IsRunning() = 0; |
perkj | 74622e0 | 2016-02-26 02:54:38 -0800 | [diff] [blame] | 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 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 198 | CaptureState capture_state() const { |
| 199 | return capture_state_; |
| 200 | } |
| 201 | |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 202 | virtual bool GetApplyRotation() { return apply_rotation_; } |
| 203 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 204 | // Returns true if the capturer is screencasting. This can be used to |
| 205 | // implement screencast specific behavior. |
| 206 | virtual bool IsScreencast() const = 0; |
| 207 | |
| 208 | // 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 |
| 210 | // the capturer is capable of but not performing satisfactorily at. |
| 211 | // The capping is an upper bound for each component of the capturing format. |
| 212 | // The fourcc component is ignored. |
| 213 | void ConstrainSupportedFormats(const VideoFormat& max_format); |
| 214 | |
| 215 | void set_enable_camera_list(bool enable_camera_list) { |
| 216 | enable_camera_list_ = enable_camera_list; |
| 217 | } |
| 218 | bool enable_camera_list() { |
| 219 | return enable_camera_list_; |
| 220 | } |
mallinath@webrtc.org | 1b15f42 | 2013-09-06 22:56:28 +0000 | [diff] [blame] | 221 | |
perkj | 74622e0 | 2016-02-26 02:54:38 -0800 | [diff] [blame] | 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 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 230 | // Signal all capture state changes that are not a direct result of calling |
| 231 | // Start(). |
| 232 | sigslot::signal2<VideoCapturer*, CaptureState> SignalStateChange; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 233 | // Frame callbacks are multithreaded to allow disconnect and connect to be |
| 234 | // 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 |
| 236 | // unmarshalled thread owned by the VideoCapturer. |
| 237 | // Signal the captured frame to downstream. |
| 238 | sigslot::signal2<VideoCapturer*, const CapturedFrame*, |
| 239 | sigslot::multi_threaded_local> SignalFrameCaptured; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 240 | |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 241 | // If true, run video adaptation. By default, video adaptation is enabled |
| 242 | // and users must call video_adapter()->OnOutputFormatRequest() |
| 243 | // to receive frames. |
| 244 | bool enable_video_adapter() const { return enable_video_adapter_; } |
| 245 | void set_enable_video_adapter(bool enable_video_adapter) { |
| 246 | enable_video_adapter_ = enable_video_adapter; |
| 247 | } |
| 248 | |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 249 | // Takes ownership. |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 250 | void set_frame_factory(VideoFrameFactory* frame_factory); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 251 | |
henrike@webrtc.org | 704bf9e | 2014-02-27 17:52:04 +0000 | [diff] [blame] | 252 | // Gets statistics for tracked variables recorded since the last call to |
| 253 | // GetStats. Note that calling GetStats resets any gathered data so it |
| 254 | // should be called only periodically to log statistics. |
| 255 | void GetStats(VariableInfo<int>* adapt_drop_stats, |
| 256 | VariableInfo<int>* effect_drop_stats, |
buildbot@webrtc.org | 0b53bd2 | 2014-05-06 17:12:36 +0000 | [diff] [blame] | 257 | VariableInfo<double>* frame_time_stats, |
| 258 | VideoFormat* last_captured_frame_format); |
henrike@webrtc.org | 704bf9e | 2014-02-27 17:52:04 +0000 | [diff] [blame] | 259 | |
Per | a509241 | 2016-02-12 13:30:57 +0100 | [diff] [blame] | 260 | // Implements VideoSourceInterface |
| 261 | void AddOrUpdateSink(rtc::VideoSinkInterface<cricket::VideoFrame>* sink, |
| 262 | const rtc::VideoSinkWants& wants) override; |
| 263 | void RemoveSink(rtc::VideoSinkInterface<cricket::VideoFrame>* sink) override; |
| 264 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 265 | protected: |
Per | a509241 | 2016-02-12 13:30:57 +0100 | [diff] [blame] | 266 | // Signal the captured and possibly adapted frame to downstream consumers |
| 267 | // such as the encoder. |
| 268 | // TODO(perkj): Remove once it is not used by remoting in Chrome. |
| 269 | sigslot::signal2<VideoCapturer*, const VideoFrame*, |
| 270 | sigslot::multi_threaded_local> SignalVideoFrame; |
| 271 | |
| 272 | // OnSinkWantsChanged can be overridden to change the default behavior |
| 273 | // when a sink changes its VideoSinkWants by calling AddOrUpdateSink. |
| 274 | virtual void OnSinkWantsChanged(const rtc::VideoSinkWants& wants); |
| 275 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 276 | // Callback attached to SignalFrameCaptured where SignalVideoFrames is called. |
| 277 | void OnFrameCaptured(VideoCapturer* video_capturer, |
| 278 | const CapturedFrame* captured_frame); |
Per | a509241 | 2016-02-12 13:30:57 +0100 | [diff] [blame] | 279 | |
| 280 | // Callback attached to SignalVideoFrame. |
| 281 | // TODO(perkj): Remove once SignalVideoFrame is removed. |
| 282 | void OnFrame(VideoCapturer* capturer, const VideoFrame* frame); |
| 283 | |
perkj | 2d5f091 | 2016-02-29 00:04:41 -0800 | [diff] [blame^] | 284 | CoordinatedVideoAdapter* video_adapter() { return &video_adapter_; } |
| 285 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 286 | void SetCaptureState(CaptureState state); |
| 287 | |
perkj | 74622e0 | 2016-02-26 02:54:38 -0800 | [diff] [blame] | 288 | // Marshals SignalStateChange onto thread_. |
| 289 | void OnMessage(rtc::Message* message) override; |
| 290 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 291 | // subclasses override this virtual method to provide a vector of fourccs, in |
| 292 | // order of preference, that are expected by the media engine. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 293 | virtual bool GetPreferredFourccs(std::vector<uint32_t>* fourccs) = 0; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 294 | |
| 295 | // mutators to set private attributes |
| 296 | void SetId(const std::string& id) { |
| 297 | id_ = id; |
| 298 | } |
| 299 | |
| 300 | void SetCaptureFormat(const VideoFormat* format) { |
| 301 | capture_format_.reset(format ? new VideoFormat(*format) : NULL); |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 302 | if (capture_format_) { |
| 303 | ASSERT(capture_format_->interval > 0 && |
| 304 | "Capture format expected to have positive interval."); |
| 305 | // Video adapter really only cares about capture format interval. |
| 306 | video_adapter_.SetInputFormat(*capture_format_); |
| 307 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | void SetSupportedFormats(const std::vector<VideoFormat>& formats); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 311 | VideoFrameFactory* frame_factory() { return frame_factory_.get(); } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 312 | |
| 313 | private: |
| 314 | void Construct(); |
| 315 | // Get the distance between the desired format and the supported format. |
| 316 | // Return the max distance if they mismatch. See the implementation for |
| 317 | // details. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 318 | int64_t GetFormatDistance(const VideoFormat& desired, |
| 319 | const VideoFormat& supported); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 320 | |
| 321 | // Convert captured frame to readable string for LOG messages. |
| 322 | std::string ToString(const CapturedFrame* frame) const; |
| 323 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 324 | // Updates filtered_supported_formats_ so that it contains the formats in |
| 325 | // supported_formats_ that fulfill all applied restrictions. |
| 326 | void UpdateFilteredSupportedFormats(); |
| 327 | // Returns true if format doesn't fulfill all applied restrictions. |
| 328 | bool ShouldFilterFormat(const VideoFormat& format) const; |
| 329 | |
buildbot@webrtc.org | 0b53bd2 | 2014-05-06 17:12:36 +0000 | [diff] [blame] | 330 | void UpdateStats(const CapturedFrame* captured_frame); |
| 331 | |
henrike@webrtc.org | 704bf9e | 2014-02-27 17:52:04 +0000 | [diff] [blame] | 332 | // Helper function to save statistics on the current data from a |
| 333 | // RollingAccumulator into stats. |
| 334 | template<class T> |
| 335 | static void GetVariableSnapshot( |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 336 | const rtc::RollingAccumulator<T>& data, |
henrike@webrtc.org | 704bf9e | 2014-02-27 17:52:04 +0000 | [diff] [blame] | 337 | VariableInfo<T>* stats); |
| 338 | |
perkj | 74622e0 | 2016-02-26 02:54:38 -0800 | [diff] [blame] | 339 | rtc::Thread* thread_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 340 | std::string id_; |
| 341 | CaptureState capture_state_; |
kwiberg | 686a8ef | 2016-02-26 03:00:35 -0800 | [diff] [blame] | 342 | std::unique_ptr<VideoFrameFactory> frame_factory_; |
| 343 | std::unique_ptr<VideoFormat> capture_format_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 344 | std::vector<VideoFormat> supported_formats_; |
kwiberg | 686a8ef | 2016-02-26 03:00:35 -0800 | [diff] [blame] | 345 | std::unique_ptr<VideoFormat> max_format_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 346 | std::vector<VideoFormat> filtered_supported_formats_; |
| 347 | |
| 348 | int ratio_w_; // View resolution. e.g. 1280 x 720. |
| 349 | int ratio_h_; |
| 350 | bool enable_camera_list_; |
mallinath@webrtc.org | 1b15f42 | 2013-09-06 22:56:28 +0000 | [diff] [blame] | 351 | bool square_pixel_aspect_ratio_; // Enable scaling to square pixels. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 352 | int scaled_width_; // Current output size from ComputeScale. |
| 353 | int scaled_height_; |
perkj | 74622e0 | 2016-02-26 02:54:38 -0800 | [diff] [blame] | 354 | bool muted_; |
| 355 | int black_frame_count_down_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 356 | |
Per | a509241 | 2016-02-12 13:30:57 +0100 | [diff] [blame] | 357 | rtc::VideoBroadcaster broadcaster_; |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 358 | bool enable_video_adapter_; |
| 359 | CoordinatedVideoAdapter video_adapter_; |
| 360 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 361 | rtc::Timing frame_length_time_reporter_; |
| 362 | rtc::CriticalSection frame_stats_crit_; |
henrike@webrtc.org | 704bf9e | 2014-02-27 17:52:04 +0000 | [diff] [blame] | 363 | |
| 364 | int adapt_frame_drops_; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 365 | rtc::RollingAccumulator<int> adapt_frame_drops_data_; |
henrike@webrtc.org | 704bf9e | 2014-02-27 17:52:04 +0000 | [diff] [blame] | 366 | double previous_frame_time_; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 367 | rtc::RollingAccumulator<double> frame_time_data_; |
buildbot@webrtc.org | 0b53bd2 | 2014-05-06 17:12:36 +0000 | [diff] [blame] | 368 | // The captured frame format before potential adapation. |
| 369 | VideoFormat last_captured_frame_format_; |
henrike@webrtc.org | 704bf9e | 2014-02-27 17:52:04 +0000 | [diff] [blame] | 370 | |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 371 | // Whether capturer should apply rotation to the frame before signaling it. |
| 372 | bool apply_rotation_; |
| 373 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 374 | RTC_DISALLOW_COPY_AND_ASSIGN(VideoCapturer); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 375 | }; |
| 376 | |
| 377 | } // namespace cricket |
| 378 | |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 379 | #endif // WEBRTC_MEDIA_BASE_VIDEOCAPTURER_H_ |