blob: 6133f751566ffc040db573044582da5ffc6d52fa [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
18#include "video_capture.h"
19#include "video_capture_config.h"
20#include "tick_util.h"
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +000021#include "common_video/interface/i420_video_frame.h"
andrew@webrtc.orgc1354bd2012-07-27 18:21:16 +000022#include "common_video/libyuv/include/webrtc_libyuv.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
niklase@google.com470e71d2011-07-07 08:21:25 +000054 // Implements Module declared functions.
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000055 virtual int32_t ChangeUniqueId(const int32_t id);
niklase@google.com470e71d2011-07-07 08:21:25 +000056
niklase@google.com470e71d2011-07-07 08:21:25 +000057 //Call backs
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000058 virtual int32_t RegisterCaptureDataCallback(VideoCaptureDataCallback& dataCallback);
59 virtual int32_t DeRegisterCaptureDataCallback();
60 virtual int32_t RegisterCaptureCallback(VideoCaptureFeedBack& callBack);
61 virtual int32_t DeRegisterCaptureCallback();
niklase@google.com470e71d2011-07-07 08:21:25 +000062
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000063 virtual int32_t SetCaptureDelay(int32_t delayMS);
64 virtual int32_t CaptureDelay();
65 virtual int32_t SetCaptureRotation(VideoCaptureRotation rotation);
niklase@google.com470e71d2011-07-07 08:21:25 +000066
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000067 virtual int32_t EnableFrameRateCallback(const bool enable);
68 virtual int32_t EnableNoPictureAlarm(const bool enable);
niklase@google.com470e71d2011-07-07 08:21:25 +000069
leozwang@webrtc.org1745e932012-03-01 16:30:40 +000070 virtual const char* CurrentDeviceName() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000071
72 // Module handling
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000073 virtual int32_t TimeUntilNextProcess();
74 virtual int32_t Process();
niklase@google.com470e71d2011-07-07 08:21:25 +000075
76 // Implement VideoCaptureExternal
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000077 // |capture_time| must be specified in the NTP time format in milliseconds.
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000078 virtual int32_t IncomingFrame(uint8_t* videoFrame,
79 int32_t videoFrameLength,
80 const VideoCaptureCapability& frameInfo,
81 int64_t captureTime = 0);
82 virtual int32_t IncomingFrameI420(
wu@webrtc.orgf10ea312011-10-14 17:16:04 +000083 const VideoFrameI420& video_frame,
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000084 int64_t captureTime = 0);
wu@webrtc.orgf10ea312011-10-14 17:16:04 +000085
niklase@google.com470e71d2011-07-07 08:21:25 +000086 // Platform dependent
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000087 virtual int32_t StartCapture(const VideoCaptureCapability& capability)
niklase@google.com470e71d2011-07-07 08:21:25 +000088 {
89 _requestedCapability = capability;
90 return -1;
91 }
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000092 virtual int32_t StopCapture() { return -1; }
niklase@google.com470e71d2011-07-07 08:21:25 +000093 virtual bool CaptureStarted() {return false; }
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000094 virtual int32_t CaptureSettings(VideoCaptureCapability& /*settings*/)
niklase@google.com470e71d2011-07-07 08:21:25 +000095 { return -1; }
96 VideoCaptureEncodeInterface* GetEncodeInterface(const VideoCodec& /*codec*/)
97 { return NULL; }
98
99protected:
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000100 VideoCaptureImpl(const int32_t id);
perkj@webrtc.org0cc68dc2011-09-12 08:53:36 +0000101 virtual ~VideoCaptureImpl();
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000102 int32_t DeliverCapturedFrame(I420VideoFrame& captureFrame,
103 int64_t capture_time);
perkj@webrtc.org0cc68dc2011-09-12 08:53:36 +0000104
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000105 int32_t _id; // Module ID
leozwang@webrtc.org1745e932012-03-01 16:30:40 +0000106 char* _deviceUniqueId; // current Device unique name;
niklase@google.com470e71d2011-07-07 08:21:25 +0000107 CriticalSectionWrapper& _apiCs;
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000108 int32_t _captureDelay; // Current capture delay. May be changed of platform dependent parts.
niklase@google.com470e71d2011-07-07 08:21:25 +0000109 VideoCaptureCapability _requestedCapability; // Should be set by platform dependent code in StartCapture.
110private:
111 void UpdateFrameCount();
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000112 uint32_t CalculateFrameRate(const TickTime& now);
niklase@google.com470e71d2011-07-07 08:21:25 +0000113
114 CriticalSectionWrapper& _callBackCs;
115
116 TickTime _lastProcessTime; // last time the module process function was called.
117 TickTime _lastFrameRateCallbackTime; // last time the frame rate callback function was called.
118 bool _frameRateCallBack; // true if EnableFrameRateCallback
119 bool _noPictureAlarmCallBack; //true if EnableNoPictureAlarm
120 VideoCaptureAlarm _captureAlarm; // current value of the noPictureAlarm
121
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000122 int32_t _setCaptureDelay; // The currently used capture delay
niklase@google.com470e71d2011-07-07 08:21:25 +0000123 VideoCaptureDataCallback* _dataCallBack;
124 VideoCaptureFeedBack* _captureCallBack;
125
niklase@google.com470e71d2011-07-07 08:21:25 +0000126 TickTime _lastProcessFrameCount;
127 TickTime _incomingFrameTimes[kFrameRateCountHistorySize];// timestamp for local captured frames
128 VideoRotationMode _rotateFrame; //Set if the frame should be rotated by the capture module.
129
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +0000130 I420VideoFrame _captureFrame;
mikhal@webrtc.org80f14d22012-10-11 15:03:53 +0000131 VideoFrame _capture_encoded_frame;
mflodman@webrtc.org29d75b32011-11-01 17:10:49 +0000132
133 // Used to make sure incoming timestamp is increasing for every frame.
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000134 int64_t last_capture_time_;
pbos@webrtc.org504af452013-07-02 10:15:43 +0000135
136 // Delta used for translating between NTP and internal timestamps.
137 const int64_t delta_ntp_internal_ms_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000138};
perkj@webrtc.org0cc68dc2011-09-12 08:53:36 +0000139} // namespace videocapturemodule
pbos@webrtc.org504af452013-07-02 10:15:43 +0000140} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000141#endif // WEBRTC_MODULES_VIDEO_CAPTURE_MAIN_SOURCE_VIDEO_CAPTURE_IMPL_H_