blob: 4585d8f6bafcffd2cbf12b38217cb25b78ad2a9e [file] [log] [blame]
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00001/*
kjellander1afca732016-02-07 20:46:45 -08002 * Copyright (c) 2010 The WebRTC project authors. All Rights Reserved.
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 *
kjellander1afca732016-02-07 20:46:45 -08004 * 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.org5f93d0a2015-01-20 21:36:13 +00009 */
10
henrike@webrtc.org28e20752013-07-10 00:45:36 +000011// Declaration of abstract class VideoCapturer
12
kjellandera96e2d72016-02-04 23:52:28 -080013#ifndef WEBRTC_MEDIA_BASE_VIDEOCAPTURER_H_
14#define WEBRTC_MEDIA_BASE_VIDEOCAPTURER_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000015
andresp@webrtc.orgff689be2015-02-12 11:54:26 +000016#include <algorithm>
kwiberg686a8ef2016-02-26 03:00:35 -080017#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000018#include <string>
19#include <vector>
20
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000021#include "webrtc/base/basictypes.h"
22#include "webrtc/base/criticalsection.h"
Pera5092412016-02-12 13:30:57 +010023#include "webrtc/media/base/videosourceinterface.h"
perkj74622e02016-02-26 02:54:38 -080024#include "webrtc/base/messagehandler.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000025#include "webrtc/base/rollingaccumulator.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000026#include "webrtc/base/sigslot.h"
perkj74622e02016-02-26 02:54:38 -080027#include "webrtc/base/thread.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000028#include "webrtc/base/timing.h"
kjellandera96e2d72016-02-04 23:52:28 -080029#include "webrtc/media/base/mediachannel.h"
30#include "webrtc/media/base/videoadapter.h"
Pera5092412016-02-12 13:30:57 +010031#include "webrtc/media/base/videobroadcaster.h"
kjellandera96e2d72016-02-04 23:52:28 -080032#include "webrtc/media/base/videocommon.h"
33#include "webrtc/media/base/videoframefactory.h"
34#include "webrtc/media/devices/devicemanager.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035
36
37namespace cricket {
38
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039// Current state of the capturer.
perkj74622e02016-02-26 02:54:38 -080040// TODO(hellner): CS_NO_DEVICE is an error code not a capture state. Separate
41// error codes and states.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042enum 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.
perkj74622e02016-02-26 02:54:38 -080050 CS_NO_DEVICE, // The capturer has no device and consequently failed to start.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051};
52
53class VideoFrame;
54
55struct CapturedFrame {
Peter Boström0c4e06b2015-10-07 12:23:21 +020056 static const uint32_t kFrameHeaderSize = 40; // Size from width to data_size.
57 static const uint32_t kUnknownDataSize = 0xFFFFFFFF;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058
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öm0c4e06b2015-10-07 12:23:21 +020064 bool GetDataSize(uint32_t* size) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065
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öm0c4e06b2015-10-07 12:23:21 +020069 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öm0c4e06b2015-10-07 12:23:21 +020074 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.org6c930c72015-02-09 01:28:12 +000077
Pera5092412016-02-12 13:30:57 +010078 webrtc::VideoRotation rotation; // rotation in degrees of the frame.
guoweis@webrtc.org6c930c72015-02-09 01:28:12 +000079
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080 void* data; // pointer to the frame data. This object allocates the
81 // memory or points to an existing memory.
82
83 private:
henrikg3c089d72015-09-16 05:37:44 -070084 RTC_DISALLOW_COPY_AND_ASSIGN(CapturedFrame);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085};
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.orga7b98182014-02-21 15:51:43 +000091// 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 Jedvertc2320962015-08-21 11:40:30 +020093// been captured from the device, it is sent to the video adapter, then out to
perkj74622e02016-02-26 02:54:38 -080094// the encoder.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095//
96// Programming model:
97// Create an object of a subclass of VideoCapturer
98// Initialize
99// SignalStateChange.connect()
perkj74622e02016-02-26 02:54:38 -0800100// SignalFrameCaptured.connect()
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101// Find the capture format for Start() by either calling GetSupportedFormats()
102// and selecting one of the supported or calling GetBestCaptureFormat().
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000103// video_adapter()->OnOutputFormatRequest(desired_encoding_format)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104// 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//
Pera5092412016-02-12 13:30:57 +0100113class VideoCapturer : public sigslot::has_slots<>,
perkj74622e02016-02-26 02:54:38 -0800114 public rtc::MessageHandler,
Pera5092412016-02-12 13:30:57 +0100115 public rtc::VideoSourceInterface<cricket::VideoFrame> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116 public:
perkj74622e02016-02-26 02:54:38 -0800117 // All signals are marshalled to |thread| or the creating thread if
118 // none is provided.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119 VideoCapturer();
perkj74622e02016-02-26 02:54:38 -0800120 explicit VideoCapturer(rtc::Thread* thread);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000121 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;
perkj74622e02016-02-26 02:54:38 -0800166 // 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.org28e20752013-07-10 00:45:36 +0000172
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
perkj74622e02016-02-26 02:54:38 -0800181 // Pause the video capturer.
182 virtual bool Pause(bool paused);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183 // Stop the video capturer.
184 virtual void Stop() = 0;
185 // Check if the video capturer is running.
186 virtual bool IsRunning() = 0;
perkj74622e02016-02-26 02:54:38 -0800187 // 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.org28e20752013-07-10 00:45:36 +0000198 CaptureState capture_state() const {
199 return capture_state_;
200 }
201
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000202 virtual bool GetApplyRotation() { return apply_rotation_; }
203
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000204 // 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.org1b15f422013-09-06 22:56:28 +0000221
perkj74622e02016-02-26 02:54:38 -0800222 // 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.org28e20752013-07-10 00:45:36 +0000230 // Signal all capture state changes that are not a direct result of calling
231 // Start().
232 sigslot::signal2<VideoCapturer*, CaptureState> SignalStateChange;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000233 // 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.org28e20752013-07-10 00:45:36 +0000240
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000241 // 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.org4f0d4012014-08-07 04:47:36 +0000249 // Takes ownership.
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000250 void set_frame_factory(VideoFrameFactory* frame_factory);
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000251
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000252 // 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.org0b53bd22014-05-06 17:12:36 +0000257 VariableInfo<double>* frame_time_stats,
258 VideoFormat* last_captured_frame_format);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000259
Pera5092412016-02-12 13:30:57 +0100260 // 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.org28e20752013-07-10 00:45:36 +0000265 protected:
Pera5092412016-02-12 13:30:57 +0100266 // 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.org28e20752013-07-10 00:45:36 +0000276 // Callback attached to SignalFrameCaptured where SignalVideoFrames is called.
277 void OnFrameCaptured(VideoCapturer* video_capturer,
278 const CapturedFrame* captured_frame);
Pera5092412016-02-12 13:30:57 +0100279
280 // Callback attached to SignalVideoFrame.
281 // TODO(perkj): Remove once SignalVideoFrame is removed.
282 void OnFrame(VideoCapturer* capturer, const VideoFrame* frame);
283
perkj2d5f0912016-02-29 00:04:41 -0800284 CoordinatedVideoAdapter* video_adapter() { return &video_adapter_; }
285
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000286 void SetCaptureState(CaptureState state);
287
perkj74622e02016-02-26 02:54:38 -0800288 // Marshals SignalStateChange onto thread_.
289 void OnMessage(rtc::Message* message) override;
290
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000291 // 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öm0c4e06b2015-10-07 12:23:21 +0200293 virtual bool GetPreferredFourccs(std::vector<uint32_t>* fourccs) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000294
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.orga7b98182014-02-21 15:51:43 +0000302 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.org28e20752013-07-10 00:45:36 +0000308 }
309
310 void SetSupportedFormats(const std::vector<VideoFormat>& formats);
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000311 VideoFrameFactory* frame_factory() { return frame_factory_.get(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000312
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öm0c4e06b2015-10-07 12:23:21 +0200318 int64_t GetFormatDistance(const VideoFormat& desired,
319 const VideoFormat& supported);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000320
321 // Convert captured frame to readable string for LOG messages.
322 std::string ToString(const CapturedFrame* frame) const;
323
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000324 // 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.org0b53bd22014-05-06 17:12:36 +0000330 void UpdateStats(const CapturedFrame* captured_frame);
331
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000332 // 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.orgd4e598d2014-07-29 17:36:52 +0000336 const rtc::RollingAccumulator<T>& data,
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000337 VariableInfo<T>* stats);
338
perkj74622e02016-02-26 02:54:38 -0800339 rtc::Thread* thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000340 std::string id_;
341 CaptureState capture_state_;
kwiberg686a8ef2016-02-26 03:00:35 -0800342 std::unique_ptr<VideoFrameFactory> frame_factory_;
343 std::unique_ptr<VideoFormat> capture_format_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000344 std::vector<VideoFormat> supported_formats_;
kwiberg686a8ef2016-02-26 03:00:35 -0800345 std::unique_ptr<VideoFormat> max_format_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000346 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.org1b15f422013-09-06 22:56:28 +0000351 bool square_pixel_aspect_ratio_; // Enable scaling to square pixels.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000352 int scaled_width_; // Current output size from ComputeScale.
353 int scaled_height_;
perkj74622e02016-02-26 02:54:38 -0800354 bool muted_;
355 int black_frame_count_down_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000356
Pera5092412016-02-12 13:30:57 +0100357 rtc::VideoBroadcaster broadcaster_;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000358 bool enable_video_adapter_;
359 CoordinatedVideoAdapter video_adapter_;
360
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000361 rtc::Timing frame_length_time_reporter_;
362 rtc::CriticalSection frame_stats_crit_;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000363
364 int adapt_frame_drops_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000365 rtc::RollingAccumulator<int> adapt_frame_drops_data_;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000366 double previous_frame_time_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000367 rtc::RollingAccumulator<double> frame_time_data_;
buildbot@webrtc.org0b53bd22014-05-06 17:12:36 +0000368 // The captured frame format before potential adapation.
369 VideoFormat last_captured_frame_format_;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000370
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000371 // Whether capturer should apply rotation to the frame before signaling it.
372 bool apply_rotation_;
373
henrikg3c089d72015-09-16 05:37:44 -0700374 RTC_DISALLOW_COPY_AND_ASSIGN(VideoCapturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000375};
376
377} // namespace cricket
378
kjellandera96e2d72016-02-04 23:52:28 -0800379#endif // WEBRTC_MEDIA_BASE_VIDEOCAPTURER_H_