OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/modules/video_capture/video_capture_impl.h" | 11 #include "webrtc/modules/video_capture/video_capture_impl.h" |
12 | 12 |
13 #include <stdlib.h> | 13 #include <stdlib.h> |
14 | 14 |
15 #include "webrtc/base/refcount.h" | 15 #include "webrtc/base/refcount.h" |
16 #include "webrtc/base/timeutils.h" | 16 #include "webrtc/base/timeutils.h" |
17 #include "webrtc/base/trace_event.h" | 17 #include "webrtc/base/trace_event.h" |
18 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" | 18 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" |
19 #include "webrtc/modules/include/module_common_types.h" | 19 #include "webrtc/modules/include/module_common_types.h" |
20 #include "webrtc/modules/video_capture/video_capture_config.h" | 20 #include "webrtc/modules/video_capture/video_capture_config.h" |
21 #include "webrtc/system_wrappers/include/clock.h" | 21 #include "webrtc/system_wrappers/include/clock.h" |
22 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" | 22 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" |
23 #include "webrtc/system_wrappers/include/logging.h" | 23 #include "webrtc/system_wrappers/include/logging.h" |
24 | 24 |
25 namespace webrtc { | 25 namespace webrtc { |
26 namespace videocapturemodule { | 26 namespace videocapturemodule { |
27 rtc::scoped_refptr<VideoCaptureModule> VideoCaptureImpl::Create( | 27 rtc::scoped_refptr<VideoCaptureModule> VideoCaptureImpl::Create( |
28 const int32_t id, | |
29 VideoCaptureExternal*& externalCapture) { | 28 VideoCaptureExternal*& externalCapture) { |
30 rtc::scoped_refptr<VideoCaptureImpl> implementation( | 29 rtc::scoped_refptr<VideoCaptureImpl> implementation( |
31 new rtc::RefCountedObject<VideoCaptureImpl>(id)); | 30 new rtc::RefCountedObject<VideoCaptureImpl>()); |
32 externalCapture = implementation.get(); | 31 externalCapture = implementation.get(); |
33 return implementation; | 32 return implementation; |
34 } | 33 } |
35 | 34 |
36 const char* VideoCaptureImpl::CurrentDeviceName() const | 35 const char* VideoCaptureImpl::CurrentDeviceName() const |
37 { | 36 { |
38 return _deviceUniqueId; | 37 return _deviceUniqueId; |
39 } | 38 } |
40 | 39 |
41 // static | 40 // static |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
83 int64_t VideoCaptureImpl::TimeUntilNextProcess() | 82 int64_t VideoCaptureImpl::TimeUntilNextProcess() |
84 { | 83 { |
85 CriticalSectionScoped cs(&_callBackCs); | 84 CriticalSectionScoped cs(&_callBackCs); |
86 const int64_t kProcessIntervalMs = 300; | 85 const int64_t kProcessIntervalMs = 300; |
87 return kProcessIntervalMs - | 86 return kProcessIntervalMs - |
88 (rtc::TimeNanos() - _lastProcessTimeNanos) / | 87 (rtc::TimeNanos() - _lastProcessTimeNanos) / |
89 rtc::kNumNanosecsPerMillisec; | 88 rtc::kNumNanosecsPerMillisec; |
90 } | 89 } |
91 | 90 |
92 // Process any pending tasks such as timeouts | 91 // Process any pending tasks such as timeouts |
93 void VideoCaptureImpl::Process() | 92 void VideoCaptureImpl::Process() |
perkj_webrtc
2016/11/29 12:55:56
VideoCaptureImpl no longer need ::Process and Vide
nisse-webrtc
2016/11/29 13:58:17
Nice!
And TimeUntilNextProcess should also be del
perkj_webrtc
2016/11/30 06:28:48
I don't think so. It does not have to be ref count
nisse-webrtc
2016/11/30 07:54:01
There's a method, WebRtcVcmFactory::Create, which
| |
94 { | 93 { |
95 CriticalSectionScoped cs(&_callBackCs); | 94 CriticalSectionScoped cs(&_callBackCs); |
nisse-webrtc
2016/11/29 13:58:17
And it seems _callBackCs can also be deleted? In a
perkj_webrtc
2016/11/30 06:28:48
You can use GUARDED_BY().. But yes, there is no re
| |
96 | 95 |
97 const int64_t now_ns = rtc::TimeNanos(); | |
98 _lastProcessTimeNanos = rtc::TimeNanos(); | 96 _lastProcessTimeNanos = rtc::TimeNanos(); |
99 | 97 |
100 // Handle No picture alarm | |
101 | |
102 if (_lastProcessFrameTimeNanos == _incomingFrameTimesNanos[0] && | |
103 _captureAlarm != Raised) | |
104 { | |
105 if (_noPictureAlarmCallBack && _captureCallBack) | |
106 { | |
107 _captureAlarm = Raised; | |
108 _captureCallBack->OnNoPictureAlarm(_id, _captureAlarm); | |
109 } | |
110 } | |
111 else if (_lastProcessFrameTimeNanos != _incomingFrameTimesNanos[0] && | |
112 _captureAlarm != Cleared) | |
113 { | |
114 if (_noPictureAlarmCallBack && _captureCallBack) | |
115 { | |
116 _captureAlarm = Cleared; | |
117 _captureCallBack->OnNoPictureAlarm(_id, _captureAlarm); | |
118 | |
119 } | |
120 } | |
121 | |
122 // Handle frame rate callback | |
123 if ((now_ns - _lastFrameRateCallbackTimeNanos) / | |
124 rtc::kNumNanosecsPerMillisec | |
125 > kFrameRateCallbackInterval) | |
126 { | |
127 if (_frameRateCallBack && _captureCallBack) | |
128 { | |
129 const uint32_t frameRate = CalculateFrameRate(now_ns); | |
130 _captureCallBack->OnCaptureFrameRate(_id, frameRate); | |
131 } | |
132 // Can be set by EnableFrameRateCallback | |
133 _lastFrameRateCallbackTimeNanos = now_ns; | |
134 | |
135 } | |
136 | |
137 _lastProcessFrameTimeNanos = _incomingFrameTimesNanos[0]; | 98 _lastProcessFrameTimeNanos = _incomingFrameTimesNanos[0]; |
138 } | 99 } |
139 | 100 |
140 VideoCaptureImpl::VideoCaptureImpl(const int32_t id) | 101 VideoCaptureImpl::VideoCaptureImpl() |
141 : _id(id), | 102 : _deviceUniqueId(NULL), |
142 _deviceUniqueId(NULL), | |
143 _apiCs(*CriticalSectionWrapper::CreateCriticalSection()), | 103 _apiCs(*CriticalSectionWrapper::CreateCriticalSection()), |
144 _captureDelay(0), | 104 _captureDelay(0), |
145 _requestedCapability(), | 105 _requestedCapability(), |
146 _callBackCs(*CriticalSectionWrapper::CreateCriticalSection()), | 106 _callBackCs(*CriticalSectionWrapper::CreateCriticalSection()), |
147 _lastProcessTimeNanos(rtc::TimeNanos()), | 107 _lastProcessTimeNanos(rtc::TimeNanos()), |
148 _lastFrameRateCallbackTimeNanos(rtc::TimeNanos()), | 108 _lastFrameRateCallbackTimeNanos(rtc::TimeNanos()), |
149 _frameRateCallBack(false), | |
150 _noPictureAlarmCallBack(false), | |
151 _captureAlarm(Cleared), | |
152 _setCaptureDelay(0), | |
153 _dataCallBack(NULL), | 109 _dataCallBack(NULL), |
154 _captureCallBack(NULL), | |
155 _lastProcessFrameTimeNanos(rtc::TimeNanos()), | 110 _lastProcessFrameTimeNanos(rtc::TimeNanos()), |
156 _rotateFrame(kVideoRotation_0), | 111 _rotateFrame(kVideoRotation_0), |
157 apply_rotation_(false) { | 112 apply_rotation_(false) { |
158 _requestedCapability.width = kDefaultWidth; | 113 _requestedCapability.width = kDefaultWidth; |
159 _requestedCapability.height = kDefaultHeight; | 114 _requestedCapability.height = kDefaultHeight; |
160 _requestedCapability.maxFPS = 30; | 115 _requestedCapability.maxFPS = 30; |
161 _requestedCapability.rawType = kVideoI420; | 116 _requestedCapability.rawType = kVideoI420; |
162 _requestedCapability.codecType = kVideoCodecUnknown; | 117 _requestedCapability.codecType = kVideoCodecUnknown; |
163 memset(_incomingFrameTimesNanos, 0, sizeof(_incomingFrameTimesNanos)); | 118 memset(_incomingFrameTimesNanos, 0, sizeof(_incomingFrameTimesNanos)); |
164 } | 119 } |
165 | 120 |
166 VideoCaptureImpl::~VideoCaptureImpl() | 121 VideoCaptureImpl::~VideoCaptureImpl() |
167 { | 122 { |
168 DeRegisterCaptureDataCallback(); | 123 DeRegisterCaptureDataCallback(); |
169 DeRegisterCaptureCallback(); | |
170 delete &_callBackCs; | 124 delete &_callBackCs; |
171 delete &_apiCs; | 125 delete &_apiCs; |
172 | 126 |
173 if (_deviceUniqueId) | 127 if (_deviceUniqueId) |
174 delete[] _deviceUniqueId; | 128 delete[] _deviceUniqueId; |
175 } | 129 } |
176 | 130 |
177 void VideoCaptureImpl::RegisterCaptureDataCallback( | 131 void VideoCaptureImpl::RegisterCaptureDataCallback( |
178 VideoCaptureDataCallback& dataCallBack) { | 132 rtc::VideoSinkInterface<VideoFrame>* dataCallBack) { |
179 CriticalSectionScoped cs(&_apiCs); | 133 CriticalSectionScoped cs(&_apiCs); |
180 CriticalSectionScoped cs2(&_callBackCs); | 134 CriticalSectionScoped cs2(&_callBackCs); |
181 _dataCallBack = &dataCallBack; | 135 _dataCallBack = dataCallBack; |
182 } | 136 } |
183 | 137 |
184 void VideoCaptureImpl::DeRegisterCaptureDataCallback() { | 138 void VideoCaptureImpl::DeRegisterCaptureDataCallback() { |
185 CriticalSectionScoped cs(&_apiCs); | 139 CriticalSectionScoped cs(&_apiCs); |
186 CriticalSectionScoped cs2(&_callBackCs); | 140 CriticalSectionScoped cs2(&_callBackCs); |
187 _dataCallBack = NULL; | 141 _dataCallBack = NULL; |
188 } | 142 } |
189 void VideoCaptureImpl::RegisterCaptureCallback(VideoCaptureFeedBack& callBack) { | |
190 | |
191 CriticalSectionScoped cs(&_apiCs); | |
192 CriticalSectionScoped cs2(&_callBackCs); | |
193 _captureCallBack = &callBack; | |
194 } | |
195 void VideoCaptureImpl::DeRegisterCaptureCallback() { | |
196 | |
197 CriticalSectionScoped cs(&_apiCs); | |
198 CriticalSectionScoped cs2(&_callBackCs); | |
199 _captureCallBack = NULL; | |
200 } | |
201 void VideoCaptureImpl::SetCaptureDelay(int32_t delayMS) { | |
202 CriticalSectionScoped cs(&_apiCs); | |
203 _captureDelay = delayMS; | |
204 } | |
205 int32_t VideoCaptureImpl::CaptureDelay() | |
206 { | |
207 CriticalSectionScoped cs(&_apiCs); | |
208 return _setCaptureDelay; | |
209 } | |
210 | |
211 int32_t VideoCaptureImpl::DeliverCapturedFrame(VideoFrame& captureFrame) { | 143 int32_t VideoCaptureImpl::DeliverCapturedFrame(VideoFrame& captureFrame) { |
212 UpdateFrameCount(); // frame count used for local frame rate callback. | 144 UpdateFrameCount(); // frame count used for local frame rate callback. |
213 | 145 |
214 const bool callOnCaptureDelayChanged = _setCaptureDelay != _captureDelay; | |
215 // Capture delay changed | |
216 if (_setCaptureDelay != _captureDelay) { | |
217 _setCaptureDelay = _captureDelay; | |
218 } | |
219 | |
220 if (_dataCallBack) { | 146 if (_dataCallBack) { |
221 if (callOnCaptureDelayChanged) { | 147 _dataCallBack->OnFrame(captureFrame); |
222 _dataCallBack->OnCaptureDelayChanged(_id, _captureDelay); | |
223 } | |
224 _dataCallBack->OnIncomingCapturedFrame(_id, captureFrame); | |
225 } | 148 } |
226 | 149 |
227 return 0; | 150 return 0; |
228 } | 151 } |
229 | 152 |
230 int32_t VideoCaptureImpl::IncomingFrame( | 153 int32_t VideoCaptureImpl::IncomingFrame( |
231 uint8_t* videoFrame, | 154 uint8_t* videoFrame, |
232 size_t videoFrameLength, | 155 size_t videoFrameLength, |
233 const VideoCaptureCapability& frameInfo, | 156 const VideoCaptureCapability& frameInfo, |
234 int64_t captureTime/*=0*/) | 157 int64_t captureTime/*=0*/) |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
306 return 0; | 229 return 0; |
307 } | 230 } |
308 | 231 |
309 int32_t VideoCaptureImpl::SetCaptureRotation(VideoRotation rotation) { | 232 int32_t VideoCaptureImpl::SetCaptureRotation(VideoRotation rotation) { |
310 CriticalSectionScoped cs(&_apiCs); | 233 CriticalSectionScoped cs(&_apiCs); |
311 CriticalSectionScoped cs2(&_callBackCs); | 234 CriticalSectionScoped cs2(&_callBackCs); |
312 _rotateFrame = rotation; | 235 _rotateFrame = rotation; |
313 return 0; | 236 return 0; |
314 } | 237 } |
315 | 238 |
316 void VideoCaptureImpl::EnableFrameRateCallback(const bool enable) { | |
317 CriticalSectionScoped cs(&_apiCs); | |
318 CriticalSectionScoped cs2(&_callBackCs); | |
319 _frameRateCallBack = enable; | |
320 if (enable) | |
321 { | |
322 _lastFrameRateCallbackTimeNanos = rtc::TimeNanos(); | |
323 } | |
324 } | |
325 | |
326 bool VideoCaptureImpl::SetApplyRotation(bool enable) { | 239 bool VideoCaptureImpl::SetApplyRotation(bool enable) { |
327 // We can't take any lock here as it'll cause deadlock with IncomingFrame. | 240 // We can't take any lock here as it'll cause deadlock with IncomingFrame. |
328 | 241 |
329 // The effect of this is the last caller wins. | 242 // The effect of this is the last caller wins. |
330 apply_rotation_ = enable; | 243 apply_rotation_ = enable; |
331 return true; | 244 return true; |
332 } | 245 } |
333 | 246 |
334 void VideoCaptureImpl::EnableNoPictureAlarm(const bool enable) { | |
335 CriticalSectionScoped cs(&_apiCs); | |
336 CriticalSectionScoped cs2(&_callBackCs); | |
337 _noPictureAlarmCallBack = enable; | |
338 } | |
339 | |
340 void VideoCaptureImpl::UpdateFrameCount() | 247 void VideoCaptureImpl::UpdateFrameCount() |
341 { | 248 { |
342 if (_incomingFrameTimesNanos[0] / rtc::kNumNanosecsPerMicrosec == 0) | 249 if (_incomingFrameTimesNanos[0] / rtc::kNumNanosecsPerMicrosec == 0) |
343 { | 250 { |
344 // first no shift | 251 // first no shift |
345 } | 252 } |
346 else | 253 else |
347 { | 254 { |
348 // shift | 255 // shift |
349 for (int i = (kFrameRateCountHistorySize - 2); i >= 0; i--) | 256 for (int i = (kFrameRateCountHistorySize - 2); i >= 0; i--) |
(...skipping 29 matching lines...) Expand all Loading... | |
379 if (diff > 0) | 286 if (diff > 0) |
380 { | 287 { |
381 return uint32_t((nrOfFrames * 1000.0f / diff) + 0.5f); | 288 return uint32_t((nrOfFrames * 1000.0f / diff) + 0.5f); |
382 } | 289 } |
383 } | 290 } |
384 | 291 |
385 return nrOfFrames; | 292 return nrOfFrames; |
386 } | 293 } |
387 } // namespace videocapturemodule | 294 } // namespace videocapturemodule |
388 } // namespace webrtc | 295 } // namespace webrtc |
OLD | NEW |