blob: 164421776c3f5b0abc44582ef9d53b7729ab49b4 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
leozwang@webrtc.org1745e932012-03-01 16:30:40 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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.
9 */
10
11#ifndef WEBRTC_MODULES_VIDEO_CAPTURE_MAIN_SOURCE_VIDEO_CAPTURE_IMPL_H_
12#define WEBRTC_MODULES_VIDEO_CAPTURE_MAIN_SOURCE_VIDEO_CAPTURE_IMPL_H_
13
14/*
15 * video_capture_impl.h
16 */
17
pbos@webrtc.orga9b74ad2013-07-12 10:03:52 +000018#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
guoweis@webrtc.org59140d62015-03-09 17:07:31 +000019#include "webrtc/common_video/rotation.h"
Henrik Kjellander5dda80a2015-11-12 12:46:09 +010020#include "webrtc/modules/video_capture/video_capture.h"
pbos@webrtc.orga9b74ad2013-07-12 10:03:52 +000021#include "webrtc/modules/video_capture/video_capture_config.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010022#include "webrtc/system_wrappers/include/tick_util.h"
Thiago Farina9bfe3da2015-04-10 12:52:13 +020023#include "webrtc/video_frame.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000024
25namespace webrtc
26{
27class CriticalSectionWrapper;
28
perkj@webrtc.org0cc68dc2011-09-12 08:53:36 +000029namespace videocapturemodule {
niklase@google.com470e71d2011-07-07 08:21:25 +000030// Class definitions
31class VideoCaptureImpl: public VideoCaptureModule, public VideoCaptureExternal
32{
33public:
34
perkj@webrtc.org0cc68dc2011-09-12 08:53:36 +000035 /*
36 * Create a video capture module object
37 *
38 * id - unique identifier of this video capture module object
39 * deviceUniqueIdUTF8 - name of the device. Available names can be found by using GetDeviceName
40 */
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000041 static VideoCaptureModule* Create(const int32_t id,
leozwang@webrtc.org1745e932012-03-01 16:30:40 +000042 const char* deviceUniqueIdUTF8);
niklase@google.com470e71d2011-07-07 08:21:25 +000043
perkj@webrtc.org0cc68dc2011-09-12 08:53:36 +000044 /*
45 * Create a video capture module object used for external capture.
46 *
47 * id - unique identifier of this video capture module object
48 * externalCapture - [out] interface to call when a new frame is captured.
49 */
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000050 static VideoCaptureModule* Create(const int32_t id,
niklase@google.com470e71d2011-07-07 08:21:25 +000051 VideoCaptureExternal*& externalCapture);
52
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000053 static DeviceInfo* CreateDeviceInfo(const int32_t id);
perkj@webrtc.org0cc68dc2011-09-12 08:53:36 +000054
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000055 // Helpers for converting between (integral) degrees and
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000056 // VideoRotation values. Return 0 on success.
57 static int32_t RotationFromDegrees(int degrees, VideoRotation* rotation);
58 static int32_t RotationInDegrees(VideoRotation rotation, int* degrees);
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000059
niklase@google.com470e71d2011-07-07 08:21:25 +000060 //Call backs
mallinath@webrtc.org7433a082014-01-29 00:56:02 +000061 virtual void RegisterCaptureDataCallback(
62 VideoCaptureDataCallback& dataCallback);
63 virtual void DeRegisterCaptureDataCallback();
64 virtual void RegisterCaptureCallback(VideoCaptureFeedBack& callBack);
65 virtual void DeRegisterCaptureCallback();
niklase@google.com470e71d2011-07-07 08:21:25 +000066
mallinath@webrtc.org7433a082014-01-29 00:56:02 +000067 virtual void SetCaptureDelay(int32_t delayMS);
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000068 virtual int32_t CaptureDelay();
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000069 virtual int32_t SetCaptureRotation(VideoRotation rotation);
guoweis@webrtc.org1226e922015-02-11 18:37:54 +000070 virtual bool SetApplyRotation(bool enable);
71 virtual bool GetApplyRotation() {
72 return apply_rotation_;
73 }
niklase@google.com470e71d2011-07-07 08:21:25 +000074
mallinath@webrtc.org7433a082014-01-29 00:56:02 +000075 virtual void EnableFrameRateCallback(const bool enable);
76 virtual void EnableNoPictureAlarm(const bool enable);
niklase@google.com470e71d2011-07-07 08:21:25 +000077
leozwang@webrtc.org1745e932012-03-01 16:30:40 +000078 virtual const char* CurrentDeviceName() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000079
80 // Module handling
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000081 virtual int64_t TimeUntilNextProcess();
torbjorngda33a8a2016-02-25 04:34:03 -080082 virtual int32_t Process();
niklase@google.com470e71d2011-07-07 08:21:25 +000083
84 // Implement VideoCaptureExternal
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +000085 // |capture_time| must be specified in NTP time format in milliseconds.
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000086 virtual int32_t IncomingFrame(uint8_t* videoFrame,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000087 size_t videoFrameLength,
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000088 const VideoCaptureCapability& frameInfo,
89 int64_t captureTime = 0);
pbos@webrtc.org2ffb1492013-11-22 13:10:13 +000090
niklase@google.com470e71d2011-07-07 08:21:25 +000091 // Platform dependent
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000092 virtual int32_t StartCapture(const VideoCaptureCapability& capability)
niklase@google.com470e71d2011-07-07 08:21:25 +000093 {
94 _requestedCapability = capability;
95 return -1;
96 }
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000097 virtual int32_t StopCapture() { return -1; }
niklase@google.com470e71d2011-07-07 08:21:25 +000098 virtual bool CaptureStarted() {return false; }
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000099 virtual int32_t CaptureSettings(VideoCaptureCapability& /*settings*/)
niklase@google.com470e71d2011-07-07 08:21:25 +0000100 { return -1; }
101 VideoCaptureEncodeInterface* GetEncodeInterface(const VideoCodec& /*codec*/)
102 { return NULL; }
103
104protected:
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000105 VideoCaptureImpl(const int32_t id);
perkj@webrtc.org0cc68dc2011-09-12 08:53:36 +0000106 virtual ~VideoCaptureImpl();
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700107 int32_t DeliverCapturedFrame(VideoFrame& captureFrame);
perkj@webrtc.org0cc68dc2011-09-12 08:53:36 +0000108
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000109 int32_t _id; // Module ID
leozwang@webrtc.org1745e932012-03-01 16:30:40 +0000110 char* _deviceUniqueId; // current Device unique name;
niklase@google.com470e71d2011-07-07 08:21:25 +0000111 CriticalSectionWrapper& _apiCs;
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000112 int32_t _captureDelay; // Current capture delay. May be changed of platform dependent parts.
niklase@google.com470e71d2011-07-07 08:21:25 +0000113 VideoCaptureCapability _requestedCapability; // Should be set by platform dependent code in StartCapture.
114private:
115 void UpdateFrameCount();
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000116 uint32_t CalculateFrameRate(const TickTime& now);
niklase@google.com470e71d2011-07-07 08:21:25 +0000117
118 CriticalSectionWrapper& _callBackCs;
119
120 TickTime _lastProcessTime; // last time the module process function was called.
121 TickTime _lastFrameRateCallbackTime; // last time the frame rate callback function was called.
122 bool _frameRateCallBack; // true if EnableFrameRateCallback
123 bool _noPictureAlarmCallBack; //true if EnableNoPictureAlarm
124 VideoCaptureAlarm _captureAlarm; // current value of the noPictureAlarm
125
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000126 int32_t _setCaptureDelay; // The currently used capture delay
niklase@google.com470e71d2011-07-07 08:21:25 +0000127 VideoCaptureDataCallback* _dataCallBack;
128 VideoCaptureFeedBack* _captureCallBack;
129
niklase@google.com470e71d2011-07-07 08:21:25 +0000130 TickTime _lastProcessFrameCount;
131 TickTime _incomingFrameTimes[kFrameRateCountHistorySize];// timestamp for local captured frames
guoweis@webrtc.org59140d62015-03-09 17:07:31 +0000132 VideoRotation _rotateFrame; // Set if the frame should be rotated by the
133 // capture module.
niklase@google.com470e71d2011-07-07 08:21:25 +0000134
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700135 VideoFrame _captureFrame;
mflodman@webrtc.org29d75b32011-11-01 17:10:49 +0000136
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000137 // Indicate whether rotation should be applied before delivered externally.
138 bool apply_rotation_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000139};
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000140} // namespace videocapturemodule
141} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000142#endif // WEBRTC_MODULES_VIDEO_CAPTURE_MAIN_SOURCE_VIDEO_CAPTURE_IMPL_H_