niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
leozwang@webrtc.org | 1745e93 | 2012-03-01 16:30:40 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3 | * |
| 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 | |
Peter Boström | 1d19441 | 2016-03-21 16:44:31 +0100 | [diff] [blame] | 18 | #include "webrtc/base/scoped_ref_ptr.h" |
pbos@webrtc.org | a9b74ad | 2013-07-12 10:03:52 +0000 | [diff] [blame] | 19 | #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" |
guoweis@webrtc.org | 59140d6 | 2015-03-09 17:07:31 +0000 | [diff] [blame] | 20 | #include "webrtc/common_video/rotation.h" |
Henrik Kjellander | 5dda80a | 2015-11-12 12:46:09 +0100 | [diff] [blame] | 21 | #include "webrtc/modules/video_capture/video_capture.h" |
pbos@webrtc.org | a9b74ad | 2013-07-12 10:03:52 +0000 | [diff] [blame] | 22 | #include "webrtc/modules/video_capture/video_capture_config.h" |
Thiago Farina | 9bfe3da | 2015-04-10 12:52:13 +0200 | [diff] [blame] | 23 | #include "webrtc/video_frame.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 24 | |
| 25 | namespace webrtc |
| 26 | { |
| 27 | class CriticalSectionWrapper; |
| 28 | |
perkj@webrtc.org | 0cc68dc | 2011-09-12 08:53:36 +0000 | [diff] [blame] | 29 | namespace videocapturemodule { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 30 | // Class definitions |
| 31 | class VideoCaptureImpl: public VideoCaptureModule, public VideoCaptureExternal |
| 32 | { |
| 33 | public: |
| 34 | |
perkj@webrtc.org | 0cc68dc | 2011-09-12 08:53:36 +0000 | [diff] [blame] | 35 | /* |
| 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 | */ |
Peter Boström | 1d19441 | 2016-03-21 16:44:31 +0100 | [diff] [blame] | 41 | static rtc::scoped_refptr<VideoCaptureModule> Create( |
| 42 | const int32_t id, |
| 43 | const char* deviceUniqueIdUTF8); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 44 | |
perkj@webrtc.org | 0cc68dc | 2011-09-12 08:53:36 +0000 | [diff] [blame] | 45 | /* |
| 46 | * Create a video capture module object used for external capture. |
| 47 | * |
| 48 | * id - unique identifier of this video capture module object |
| 49 | * externalCapture - [out] interface to call when a new frame is captured. |
| 50 | */ |
Peter Boström | 1d19441 | 2016-03-21 16:44:31 +0100 | [diff] [blame] | 51 | static rtc::scoped_refptr<VideoCaptureModule> Create( |
| 52 | const int32_t id, |
| 53 | VideoCaptureExternal*& externalCapture); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 54 | |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 55 | static DeviceInfo* CreateDeviceInfo(const int32_t id); |
perkj@webrtc.org | 0cc68dc | 2011-09-12 08:53:36 +0000 | [diff] [blame] | 56 | |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 57 | // Helpers for converting between (integral) degrees and |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 +0000 | [diff] [blame] | 58 | // VideoRotation values. Return 0 on success. |
| 59 | static int32_t RotationFromDegrees(int degrees, VideoRotation* rotation); |
| 60 | static int32_t RotationInDegrees(VideoRotation rotation, int* degrees); |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 61 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 62 | //Call backs |
mallinath@webrtc.org | 7433a08 | 2014-01-29 00:56:02 +0000 | [diff] [blame] | 63 | virtual void RegisterCaptureDataCallback( |
| 64 | VideoCaptureDataCallback& dataCallback); |
| 65 | virtual void DeRegisterCaptureDataCallback(); |
| 66 | virtual void RegisterCaptureCallback(VideoCaptureFeedBack& callBack); |
| 67 | virtual void DeRegisterCaptureCallback(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 68 | |
mallinath@webrtc.org | 7433a08 | 2014-01-29 00:56:02 +0000 | [diff] [blame] | 69 | virtual void SetCaptureDelay(int32_t delayMS); |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 70 | virtual int32_t CaptureDelay(); |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 +0000 | [diff] [blame] | 71 | virtual int32_t SetCaptureRotation(VideoRotation rotation); |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 72 | virtual bool SetApplyRotation(bool enable); |
| 73 | virtual bool GetApplyRotation() { |
| 74 | return apply_rotation_; |
| 75 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 76 | |
mallinath@webrtc.org | 7433a08 | 2014-01-29 00:56:02 +0000 | [diff] [blame] | 77 | virtual void EnableFrameRateCallback(const bool enable); |
| 78 | virtual void EnableNoPictureAlarm(const bool enable); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 79 | |
leozwang@webrtc.org | 1745e93 | 2012-03-01 16:30:40 +0000 | [diff] [blame] | 80 | virtual const char* CurrentDeviceName() const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 81 | |
| 82 | // Module handling |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 83 | virtual int64_t TimeUntilNextProcess(); |
pbos | a26ac92 | 2016-02-25 04:50:01 -0800 | [diff] [blame] | 84 | virtual void Process(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 85 | |
| 86 | // Implement VideoCaptureExternal |
perkj@webrtc.org | af612d5 | 2015-03-18 09:51:05 +0000 | [diff] [blame] | 87 | // |capture_time| must be specified in NTP time format in milliseconds. |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 88 | virtual int32_t IncomingFrame(uint8_t* videoFrame, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 89 | size_t videoFrameLength, |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 90 | const VideoCaptureCapability& frameInfo, |
| 91 | int64_t captureTime = 0); |
pbos@webrtc.org | 2ffb149 | 2013-11-22 13:10:13 +0000 | [diff] [blame] | 92 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 93 | // Platform dependent |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 94 | virtual int32_t StartCapture(const VideoCaptureCapability& capability) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 95 | { |
| 96 | _requestedCapability = capability; |
| 97 | return -1; |
| 98 | } |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 99 | virtual int32_t StopCapture() { return -1; } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 100 | virtual bool CaptureStarted() {return false; } |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 101 | virtual int32_t CaptureSettings(VideoCaptureCapability& /*settings*/) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 102 | { return -1; } |
| 103 | VideoCaptureEncodeInterface* GetEncodeInterface(const VideoCodec& /*codec*/) |
| 104 | { return NULL; } |
| 105 | |
| 106 | protected: |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 107 | VideoCaptureImpl(const int32_t id); |
perkj@webrtc.org | 0cc68dc | 2011-09-12 08:53:36 +0000 | [diff] [blame] | 108 | virtual ~VideoCaptureImpl(); |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 109 | int32_t DeliverCapturedFrame(VideoFrame& captureFrame); |
perkj@webrtc.org | 0cc68dc | 2011-09-12 08:53:36 +0000 | [diff] [blame] | 110 | |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 111 | int32_t _id; // Module ID |
leozwang@webrtc.org | 1745e93 | 2012-03-01 16:30:40 +0000 | [diff] [blame] | 112 | char* _deviceUniqueId; // current Device unique name; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 113 | CriticalSectionWrapper& _apiCs; |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 114 | int32_t _captureDelay; // Current capture delay. May be changed of platform dependent parts. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 115 | VideoCaptureCapability _requestedCapability; // Should be set by platform dependent code in StartCapture. |
| 116 | private: |
| 117 | void UpdateFrameCount(); |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame^] | 118 | uint32_t CalculateFrameRate(int64_t now_ns); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 119 | |
| 120 | CriticalSectionWrapper& _callBackCs; |
| 121 | |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame^] | 122 | // last time the module process function was called. |
| 123 | int64_t _lastProcessTimeNanos; |
| 124 | // last time the frame rate callback function was called. |
| 125 | int64_t _lastFrameRateCallbackTimeNanos; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 126 | bool _frameRateCallBack; // true if EnableFrameRateCallback |
| 127 | bool _noPictureAlarmCallBack; //true if EnableNoPictureAlarm |
| 128 | VideoCaptureAlarm _captureAlarm; // current value of the noPictureAlarm |
| 129 | |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 130 | int32_t _setCaptureDelay; // The currently used capture delay |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 131 | VideoCaptureDataCallback* _dataCallBack; |
| 132 | VideoCaptureFeedBack* _captureCallBack; |
| 133 | |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame^] | 134 | int64_t _lastProcessFrameTimeNanos; |
| 135 | // timestamp for local captured frames |
| 136 | int64_t _incomingFrameTimesNanos[kFrameRateCountHistorySize]; |
guoweis@webrtc.org | 59140d6 | 2015-03-09 17:07:31 +0000 | [diff] [blame] | 137 | VideoRotation _rotateFrame; // Set if the frame should be rotated by the |
| 138 | // capture module. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 139 | |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 140 | VideoFrame _captureFrame; |
mflodman@webrtc.org | 29d75b3 | 2011-11-01 17:10:49 +0000 | [diff] [blame] | 141 | |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 142 | // Indicate whether rotation should be applied before delivered externally. |
| 143 | bool apply_rotation_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 144 | }; |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 145 | } // namespace videocapturemodule |
| 146 | } // namespace webrtc |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 147 | #endif // WEBRTC_MODULES_VIDEO_CAPTURE_MAIN_SOURCE_VIDEO_CAPTURE_IMPL_H_ |