blob: ebd8d4adea56b089f10c4cf972e57f1821924f30 [file] [log] [blame]
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00001/*
2 * libjingle
3 * Copyright 2010 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
henrike@webrtc.org28e20752013-07-10 00:45:36 +000028// Declaration of abstract class VideoCapturer
29
30#ifndef TALK_MEDIA_BASE_VIDEOCAPTURER_H_
31#define TALK_MEDIA_BASE_VIDEOCAPTURER_H_
32
andresp@webrtc.orgff689be2015-02-12 11:54:26 +000033#include <algorithm>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034#include <string>
35#include <vector>
36
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000037#include "talk/media/base/mediachannel.h"
38#include "talk/media/base/videoadapter.h"
39#include "talk/media/base/videocommon.h"
40#include "talk/media/base/videoframefactory.h"
41#include "talk/media/devices/devicemanager.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000042#include "webrtc/base/basictypes.h"
43#include "webrtc/base/criticalsection.h"
44#include "webrtc/base/messagehandler.h"
45#include "webrtc/base/rollingaccumulator.h"
46#include "webrtc/base/scoped_ptr.h"
47#include "webrtc/base/sigslot.h"
48#include "webrtc/base/thread.h"
49#include "webrtc/base/timing.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000050
51
52namespace cricket {
53
54class VideoProcessor;
55
56// Current state of the capturer.
57// TODO(hellner): CS_NO_DEVICE is an error code not a capture state. Separate
58// error codes and states.
59enum CaptureState {
60 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
62 // still fail to start.
63 CS_RUNNING, // The capturer has been started successfully and is now
64 // capturing.
65 CS_PAUSED, // The capturer has been paused.
66 CS_FAILED, // The capturer failed to start.
67 CS_NO_DEVICE, // The capturer has no device and consequently failed to start.
68};
69
70class VideoFrame;
71
72struct CapturedFrame {
73 static const uint32 kFrameHeaderSize = 40; // Size from width to data_size.
74 static const uint32 kUnknownDataSize = 0xFFFFFFFF;
75
76 CapturedFrame();
77
78 // Get the number of bytes of the frame data. If data_size is known, return
79 // it directly. Otherwise, calculate the size based on width, height, and
80 // fourcc. Return true if succeeded.
81 bool GetDataSize(uint32* size) const;
82
guoweis@webrtc.org6c930c72015-02-09 01:28:12 +000083 // TODO(guoweis): Change the type of |rotation| from int to
84 // webrtc::VideoRotation once chromium gets the code.
85 webrtc::VideoRotation GetRotation() const;
86
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087 // The width and height of the captured frame could be different from those
88 // of VideoFormat. Once the first frame is captured, the width, height,
89 // fourcc, pixel_width, and pixel_height should keep the same over frames.
90 int width; // in number of pixels
91 int height; // in number of pixels
92 uint32 fourcc; // compression
93 uint32 pixel_width; // width of a pixel, default is 1
94 uint32 pixel_height; // height of a pixel, default is 1
95 int64 elapsed_time; // elapsed time since the creation of the frame
96 // source (that is, the camera), in nanoseconds.
97 int64 time_stamp; // timestamp of when the frame was captured, in unix
98 // time with nanosecond units.
99 uint32 data_size; // number of bytes of the frame data
guoweis@webrtc.org6c930c72015-02-09 01:28:12 +0000100
101 // TODO(guoweis): This can't be converted to VideoRotation yet as it's
102 // used by chrome now.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000103 int rotation; // rotation in degrees of the frame (0, 90, 180, 270)
guoweis@webrtc.org6c930c72015-02-09 01:28:12 +0000104
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105 void* data; // pointer to the frame data. This object allocates the
106 // memory or points to an existing memory.
107
108 private:
109 DISALLOW_COPY_AND_ASSIGN(CapturedFrame);
110};
111
112// VideoCapturer is an abstract class that defines the interfaces for video
113// capturing. The subclasses implement the video capturer for various types of
114// capturers and various platforms.
115//
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000116// 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
118// been captured from the device, it is sent to the video adapter, then video
119// processors, then out to the encoder.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000120//
121// Programming model:
122// Create an object of a subclass of VideoCapturer
123// Initialize
124// SignalStateChange.connect()
125// SignalFrameCaptured.connect()
126// Find the capture format for Start() by either calling GetSupportedFormats()
127// and selecting one of the supported or calling GetBestCaptureFormat().
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000128// video_adapter()->OnOutputFormatRequest(desired_encoding_format)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129// Start()
130// GetCaptureFormat() optionally
131// Stop()
132//
133// Assumption:
134// 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
136// thread safe.
137//
138class VideoCapturer
139 : public sigslot::has_slots<>,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000140 public rtc::MessageHandler {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141 public:
142 typedef std::vector<VideoProcessor*> VideoProcessors;
143
144 // All signals are marshalled to |thread| or the creating thread if
145 // none is provided.
146 VideoCapturer();
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000147 explicit VideoCapturer(rtc::Thread* thread);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148 virtual ~VideoCapturer() {}
149
150 // 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
152 // same device.
153 const std::string& GetId() const { return id_; }
154
155 // Get the capture formats supported by the video capturer. The supported
156 // formats are non empty after the device has been opened successfully.
157 const std::vector<VideoFormat>* GetSupportedFormats() const;
158
159 // Get the best capture format for the desired format. The best format is the
160 // same as one of the supported formats except that the frame interval may be
161 // different. If the application asks for 16x9 and the camera does not support
162 // 16x9 HD or the application asks for 16x10, we find the closest 4x3 and then
163 // crop; Otherwise, we find what the application asks for. Note that we assume
164 // that for HD, the desired format is always 16x9. The subclasses can override
165 // the default implementation.
166 // Parameters
167 // desired: the input desired format. If desired.fourcc is not kAnyFourcc,
168 // the best capture format has the exactly same fourcc. Otherwise,
169 // the best capture format uses a fourcc in GetPreferredFourccs().
170 // best_format: the output of the best capture format.
171 // Return false if there is no such a best format, that is, the desired format
172 // is not supported.
173 virtual bool GetBestCaptureFormat(const VideoFormat& desired,
174 VideoFormat* best_format);
175
176 // TODO(hellner): deprecate (make private) the Start API in favor of this one.
177 // Also remove CS_STARTING as it is implied by the return
178 // value of StartCapturing().
179 bool StartCapturing(const VideoFormat& capture_format);
180 // Start the video capturer with the specified capture format.
181 // Parameter
182 // capture_format: The caller got this parameter by either calling
183 // GetSupportedFormats() and selecting one of the supported
184 // or calling GetBestCaptureFormat().
185 // Return
186 // CS_STARTING: The capturer is trying to start. Success or failure will
187 // be notified via the |SignalStateChange| callback.
188 // CS_RUNNING: if the capturer is started and capturing.
189 // CS_PAUSED: Will never be returned.
190 // CS_FAILED: if the capturer failes to start..
191 // CS_NO_DEVICE: if the capturer has no device and fails to start.
192 virtual CaptureState Start(const VideoFormat& capture_format) = 0;
193 // Sets the desired aspect ratio. If the capturer is capturing at another
194 // aspect ratio it will crop the width or the height so that asked for
195 // aspect ratio is acheived. Note that ratio_w and ratio_h do not need to be
196 // relatively prime.
197 void UpdateAspectRatio(int ratio_w, int ratio_h);
198 void ClearAspectRatio();
199
200 // Get the current capture format, which is set by the Start() call.
201 // Note that the width and height of the captured frames may differ from the
202 // capture format. For example, the capture format is HD but the captured
203 // frames may be smaller than HD.
204 const VideoFormat* GetCaptureFormat() const {
205 return capture_format_.get();
206 }
207
208 // Pause the video capturer.
209 virtual bool Pause(bool paused);
210 // Stop the video capturer.
211 virtual void Stop() = 0;
212 // Check if the video capturer is running.
213 virtual bool IsRunning() = 0;
214 // Restart the video capturer with the new |capture_format|.
215 // Default implementation stops and starts the capturer.
216 virtual bool Restart(const VideoFormat& capture_format);
217 // TODO(thorcarpenter): This behavior of keeping the camera open just to emit
218 // black frames is a total hack and should be fixed.
219 // When muting, produce black frames then pause the camera.
220 // When unmuting, start the camera. Camera starts unmuted.
221 virtual bool MuteToBlackThenPause(bool muted);
222 virtual bool IsMuted() const {
223 return muted_;
224 }
225 CaptureState capture_state() const {
226 return capture_state_;
227 }
228
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000229 // 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,
231 // generated frames will carry the rotation information from
232 // SetCaptureRotation. Return value indicates whether this operation succeeds.
233 virtual bool SetApplyRotation(bool enable);
234 virtual bool GetApplyRotation() { return apply_rotation_; }
235
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000236 // 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
245 // implement screencast specific behavior.
246 virtual bool IsScreencast() const = 0;
247
248 // 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
250 // the capturer is capable of but not performing satisfactorily at.
251 // The capping is an upper bound for each component of the capturing format.
252 // The fourcc component is ignored.
253 void ConstrainSupportedFormats(const VideoFormat& max_format);
254
255 void set_enable_camera_list(bool enable_camera_list) {
256 enable_camera_list_ = enable_camera_list;
257 }
258 bool enable_camera_list() {
259 return enable_camera_list_;
260 }
mallinath@webrtc.org1b15f422013-09-06 22:56:28 +0000261
262 // Enable scaling to ensure square pixels.
263 void set_square_pixel_aspect_ratio(bool square_pixel_aspect_ratio) {
264 square_pixel_aspect_ratio_ = square_pixel_aspect_ratio;
265 }
266 bool square_pixel_aspect_ratio() {
267 return square_pixel_aspect_ratio_;
268 }
269
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000270 // Signal all capture state changes that are not a direct result of calling
271 // Start().
272 sigslot::signal2<VideoCapturer*, CaptureState> SignalStateChange;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000273 // Frame callbacks are multithreaded to allow disconnect and connect to be
274 // called concurrently. It also ensures that it is safe to call disconnect
275 // at any time which is needed since the signal may be called from an
276 // unmarshalled thread owned by the VideoCapturer.
277 // Signal the captured frame to downstream.
278 sigslot::signal2<VideoCapturer*, const CapturedFrame*,
279 sigslot::multi_threaded_local> SignalFrameCaptured;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000280 // Signal the captured and possibly adapted frame to downstream consumers
281 // such as the encoder.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000282 sigslot::signal2<VideoCapturer*, const VideoFrame*,
283 sigslot::multi_threaded_local> SignalVideoFrame;
284
285 const VideoProcessors& video_processors() const { return video_processors_; }
286
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000287 // If 'screencast_max_pixels' is set greater than zero, screencasts will be
288 // scaled to be no larger than this value.
289 // If set to zero, the max pixels will be limited to
290 // Retina MacBookPro 15" resolution of 2880 x 1800.
291 // For high fps, maximum pixels limit is set based on common 24" monitor
292 // resolution of 2048 x 1280.
293 int screencast_max_pixels() const { return screencast_max_pixels_; }
294 void set_screencast_max_pixels(int p) {
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000295 screencast_max_pixels_ = std::max(0, p);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000296 }
297
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000298 // If true, run video adaptation. By default, video adaptation is enabled
299 // and users must call video_adapter()->OnOutputFormatRequest()
300 // to receive frames.
301 bool enable_video_adapter() const { return enable_video_adapter_; }
302 void set_enable_video_adapter(bool enable_video_adapter) {
303 enable_video_adapter_ = enable_video_adapter;
304 }
305
306 CoordinatedVideoAdapter* video_adapter() { return &video_adapter_; }
307 const CoordinatedVideoAdapter* video_adapter() const {
308 return &video_adapter_;
309 }
310
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000311 // Takes ownership.
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000312 void set_frame_factory(VideoFrameFactory* frame_factory);
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000313
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000314 // Gets statistics for tracked variables recorded since the last call to
315 // GetStats. Note that calling GetStats resets any gathered data so it
316 // should be called only periodically to log statistics.
317 void GetStats(VariableInfo<int>* adapt_drop_stats,
318 VariableInfo<int>* effect_drop_stats,
buildbot@webrtc.org0b53bd22014-05-06 17:12:36 +0000319 VariableInfo<double>* frame_time_stats,
320 VideoFormat* last_captured_frame_format);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000321
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000322 protected:
323 // Callback attached to SignalFrameCaptured where SignalVideoFrames is called.
324 void OnFrameCaptured(VideoCapturer* video_capturer,
325 const CapturedFrame* captured_frame);
326 void SetCaptureState(CaptureState state);
327
328 // Marshals SignalStateChange onto thread_.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000329 void OnMessage(rtc::Message* message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000330
331 // subclasses override this virtual method to provide a vector of fourccs, in
332 // order of preference, that are expected by the media engine.
333 virtual bool GetPreferredFourccs(std::vector<uint32>* fourccs) = 0;
334
335 // mutators to set private attributes
336 void SetId(const std::string& id) {
337 id_ = id;
338 }
339
340 void SetCaptureFormat(const VideoFormat* format) {
341 capture_format_.reset(format ? new VideoFormat(*format) : NULL);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000342 if (capture_format_) {
343 ASSERT(capture_format_->interval > 0 &&
344 "Capture format expected to have positive interval.");
345 // Video adapter really only cares about capture format interval.
346 video_adapter_.SetInputFormat(*capture_format_);
347 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000348 }
349
350 void SetSupportedFormats(const std::vector<VideoFormat>& formats);
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000351 VideoFrameFactory* frame_factory() { return frame_factory_.get(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000352
353 private:
354 void Construct();
355 // Get the distance between the desired format and the supported format.
356 // Return the max distance if they mismatch. See the implementation for
357 // details.
358 int64 GetFormatDistance(const VideoFormat& desired,
359 const VideoFormat& supported);
360
361 // Convert captured frame to readable string for LOG messages.
362 std::string ToString(const CapturedFrame* frame) const;
363
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
370 // supported_formats_ that fulfill all applied restrictions.
371 void UpdateFilteredSupportedFormats();
372 // Returns true if format doesn't fulfill all applied restrictions.
373 bool ShouldFilterFormat(const VideoFormat& format) const;
374
buildbot@webrtc.org0b53bd22014-05-06 17:12:36 +0000375 void UpdateStats(const CapturedFrame* captured_frame);
376
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000377 // Helper function to save statistics on the current data from a
378 // RollingAccumulator into stats.
379 template<class T>
380 static void GetVariableSnapshot(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000381 const rtc::RollingAccumulator<T>& data,
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000382 VariableInfo<T>* stats);
383
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000384 rtc::Thread* thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000385 std::string id_;
386 CaptureState capture_state_;
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000387 rtc::scoped_ptr<VideoFrameFactory> frame_factory_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000388 rtc::scoped_ptr<VideoFormat> capture_format_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000389 std::vector<VideoFormat> supported_formats_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000390 rtc::scoped_ptr<VideoFormat> max_format_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000391 std::vector<VideoFormat> filtered_supported_formats_;
392
393 int ratio_w_; // View resolution. e.g. 1280 x 720.
394 int ratio_h_;
395 bool enable_camera_list_;
mallinath@webrtc.org1b15f422013-09-06 22:56:28 +0000396 bool square_pixel_aspect_ratio_; // Enable scaling to square pixels.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000397 int scaled_width_; // Current output size from ComputeScale.
398 int scaled_height_;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000399 int screencast_max_pixels_; // Downscale screencasts further if requested.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000400 bool muted_;
401 int black_frame_count_down_;
402
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000403 bool enable_video_adapter_;
404 CoordinatedVideoAdapter video_adapter_;
405
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000406 rtc::Timing frame_length_time_reporter_;
407 rtc::CriticalSection frame_stats_crit_;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000408
409 int adapt_frame_drops_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000410 rtc::RollingAccumulator<int> adapt_frame_drops_data_;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000411 int effect_frame_drops_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000412 rtc::RollingAccumulator<int> effect_frame_drops_data_;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000413 double previous_frame_time_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000414 rtc::RollingAccumulator<double> frame_time_data_;
buildbot@webrtc.org0b53bd22014-05-06 17:12:36 +0000415 // The captured frame format before potential adapation.
416 VideoFormat last_captured_frame_format_;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000417
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000418 rtc::CriticalSection crit_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000419 VideoProcessors video_processors_;
420
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000421 // Whether capturer should apply rotation to the frame before signaling it.
422 bool apply_rotation_;
423
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000424 DISALLOW_COPY_AND_ASSIGN(VideoCapturer);
425};
426
427} // namespace cricket
428
429#endif // TALK_MEDIA_BASE_VIDEOCAPTURER_H_