blob: aba15f2ac06ba3d269fe1b554a65c544df212daf [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/interface/i420_video_frame.h"
19#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
20#include "webrtc/modules/video_capture/include/video_capture.h"
21#include "webrtc/modules/video_capture/video_capture_config.h"
22#include "webrtc/system_wrappers/interface/tick_util.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000023
24namespace webrtc
25{
26class CriticalSectionWrapper;
27
perkj@webrtc.org0cc68dc2011-09-12 08:53:36 +000028namespace videocapturemodule {
niklase@google.com470e71d2011-07-07 08:21:25 +000029// Class definitions
30class VideoCaptureImpl: public VideoCaptureModule, public VideoCaptureExternal
31{
32public:
33
perkj@webrtc.org0cc68dc2011-09-12 08:53:36 +000034 /*
35 * Create a video capture module object
36 *
37 * id - unique identifier of this video capture module object
38 * deviceUniqueIdUTF8 - name of the device. Available names can be found by using GetDeviceName
39 */
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000040 static VideoCaptureModule* Create(const int32_t id,
leozwang@webrtc.org1745e932012-03-01 16:30:40 +000041 const char* deviceUniqueIdUTF8);
niklase@google.com470e71d2011-07-07 08:21:25 +000042
perkj@webrtc.org0cc68dc2011-09-12 08:53:36 +000043 /*
44 * Create a video capture module object used for external capture.
45 *
46 * id - unique identifier of this video capture module object
47 * externalCapture - [out] interface to call when a new frame is captured.
48 */
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000049 static VideoCaptureModule* Create(const int32_t id,
niklase@google.com470e71d2011-07-07 08:21:25 +000050 VideoCaptureExternal*& externalCapture);
51
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000052 static DeviceInfo* CreateDeviceInfo(const int32_t id);
perkj@webrtc.org0cc68dc2011-09-12 08:53:36 +000053
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000054 // Helpers for converting between (integral) degrees and
55 // VideoCaptureRotation values. Return 0 on success.
56 static int32_t RotationFromDegrees(int degrees,
57 VideoCaptureRotation* rotation);
58 static int32_t RotationInDegrees(VideoCaptureRotation rotation,
59 int* degrees);
60
niklase@google.com470e71d2011-07-07 08:21:25 +000061 //Call backs
mallinath@webrtc.org7433a082014-01-29 00:56:02 +000062 virtual void RegisterCaptureDataCallback(
63 VideoCaptureDataCallback& dataCallback);
64 virtual void DeRegisterCaptureDataCallback();
65 virtual void RegisterCaptureCallback(VideoCaptureFeedBack& callBack);
66 virtual void DeRegisterCaptureCallback();
niklase@google.com470e71d2011-07-07 08:21:25 +000067
mallinath@webrtc.org7433a082014-01-29 00:56:02 +000068 virtual void SetCaptureDelay(int32_t delayMS);
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000069 virtual int32_t CaptureDelay();
70 virtual int32_t SetCaptureRotation(VideoCaptureRotation rotation);
guoweis@webrtc.org1226e922015-02-11 18:37:54 +000071 virtual bool SetApplyRotation(bool enable);
72 virtual bool GetApplyRotation() {
73 return apply_rotation_;
74 }
niklase@google.com470e71d2011-07-07 08:21:25 +000075
mallinath@webrtc.org7433a082014-01-29 00:56:02 +000076 virtual void EnableFrameRateCallback(const bool enable);
77 virtual void EnableNoPictureAlarm(const bool enable);
niklase@google.com470e71d2011-07-07 08:21:25 +000078
leozwang@webrtc.org1745e932012-03-01 16:30:40 +000079 virtual const char* CurrentDeviceName() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000080
81 // Module handling
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000082 virtual int64_t TimeUntilNextProcess();
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000083 virtual int32_t Process();
niklase@google.com470e71d2011-07-07 08:21:25 +000084
85 // Implement VideoCaptureExternal
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000086 // |capture_time| must be specified in the NTP time format in milliseconds.
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000087 virtual int32_t IncomingFrame(uint8_t* videoFrame,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000088 size_t videoFrameLength,
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000089 const VideoCaptureCapability& frameInfo,
90 int64_t captureTime = 0);
pbos@webrtc.org2ffb1492013-11-22 13:10:13 +000091
92 virtual int32_t IncomingI420VideoFrame(I420VideoFrame* video_frame,
93 int64_t captureTime = 0);
wu@webrtc.orgf10ea312011-10-14 17:16:04 +000094
niklase@google.com470e71d2011-07-07 08:21:25 +000095 // Platform dependent
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000096 virtual int32_t StartCapture(const VideoCaptureCapability& capability)
niklase@google.com470e71d2011-07-07 08:21:25 +000097 {
98 _requestedCapability = capability;
99 return -1;
100 }
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000101 virtual int32_t StopCapture() { return -1; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000102 virtual bool CaptureStarted() {return false; }
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000103 virtual int32_t CaptureSettings(VideoCaptureCapability& /*settings*/)
niklase@google.com470e71d2011-07-07 08:21:25 +0000104 { return -1; }
105 VideoCaptureEncodeInterface* GetEncodeInterface(const VideoCodec& /*codec*/)
106 { return NULL; }
107
108protected:
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000109 VideoCaptureImpl(const int32_t id);
perkj@webrtc.org0cc68dc2011-09-12 08:53:36 +0000110 virtual ~VideoCaptureImpl();
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000111 int32_t DeliverCapturedFrame(I420VideoFrame& captureFrame,
112 int64_t capture_time);
perkj@webrtc.org0cc68dc2011-09-12 08:53:36 +0000113
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000114 int32_t _id; // Module ID
leozwang@webrtc.org1745e932012-03-01 16:30:40 +0000115 char* _deviceUniqueId; // current Device unique name;
niklase@google.com470e71d2011-07-07 08:21:25 +0000116 CriticalSectionWrapper& _apiCs;
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000117 int32_t _captureDelay; // Current capture delay. May be changed of platform dependent parts.
niklase@google.com470e71d2011-07-07 08:21:25 +0000118 VideoCaptureCapability _requestedCapability; // Should be set by platform dependent code in StartCapture.
119private:
120 void UpdateFrameCount();
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000121 uint32_t CalculateFrameRate(const TickTime& now);
niklase@google.com470e71d2011-07-07 08:21:25 +0000122
123 CriticalSectionWrapper& _callBackCs;
124
125 TickTime _lastProcessTime; // last time the module process function was called.
126 TickTime _lastFrameRateCallbackTime; // last time the frame rate callback function was called.
127 bool _frameRateCallBack; // true if EnableFrameRateCallback
128 bool _noPictureAlarmCallBack; //true if EnableNoPictureAlarm
129 VideoCaptureAlarm _captureAlarm; // current value of the noPictureAlarm
130
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000131 int32_t _setCaptureDelay; // The currently used capture delay
niklase@google.com470e71d2011-07-07 08:21:25 +0000132 VideoCaptureDataCallback* _dataCallBack;
133 VideoCaptureFeedBack* _captureCallBack;
134
niklase@google.com470e71d2011-07-07 08:21:25 +0000135 TickTime _lastProcessFrameCount;
136 TickTime _incomingFrameTimes[kFrameRateCountHistorySize];// timestamp for local captured frames
137 VideoRotationMode _rotateFrame; //Set if the frame should be rotated by the capture module.
138
sheu@chromium.org5dd2ecb2013-10-31 23:41:04 +0000139 I420VideoFrame _captureFrame;
mikhal@webrtc.org80f14d22012-10-11 15:03:53 +0000140 VideoFrame _capture_encoded_frame;
mflodman@webrtc.org29d75b32011-11-01 17:10:49 +0000141
142 // Used to make sure incoming timestamp is increasing for every frame.
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000143 int64_t last_capture_time_;
pbos@webrtc.org504af452013-07-02 10:15:43 +0000144
145 // Delta used for translating between NTP and internal timestamps.
146 const int64_t delta_ntp_internal_ms_;
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000147
148 // Indicate whether rotation should be applied before delivered externally.
149 bool apply_rotation_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000150};
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000151} // namespace videocapturemodule
152} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000153#endif // WEBRTC_MODULES_VIDEO_CAPTURE_MAIN_SOURCE_VIDEO_CAPTURE_IMPL_H_