1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
|
/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "webrtc/modules/video_capture/video_capture_impl.h"
#include <stdlib.h>
#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
#include "webrtc/modules/interface/module_common_types.h"
#include "webrtc/modules/video_capture/video_capture_config.h"
#include "webrtc/system_wrappers/interface/clock.h"
#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
#include "webrtc/system_wrappers/interface/logging.h"
#include "webrtc/system_wrappers/interface/ref_count.h"
#include "webrtc/system_wrappers/interface/tick_util.h"
#include "webrtc/system_wrappers/interface/trace_event.h"
namespace webrtc
{
namespace videocapturemodule
{
VideoCaptureModule* VideoCaptureImpl::Create(
const int32_t id,
VideoCaptureExternal*& externalCapture)
{
RefCountImpl<VideoCaptureImpl>* implementation =
new RefCountImpl<VideoCaptureImpl>(id);
externalCapture = implementation;
return implementation;
}
const char* VideoCaptureImpl::CurrentDeviceName() const
{
return _deviceUniqueId;
}
// static
int32_t VideoCaptureImpl::RotationFromDegrees(int degrees,
VideoCaptureRotation* rotation) {
switch (degrees) {
case 0:
*rotation = kCameraRotate0;
return 0;
case 90:
*rotation = kCameraRotate90;
return 0;
case 180:
*rotation = kCameraRotate180;
return 0;
case 270:
*rotation = kCameraRotate270;
return 0;
default:
return -1;;
}
}
// static
int32_t VideoCaptureImpl::RotationInDegrees(VideoCaptureRotation rotation,
int* degrees) {
switch (rotation) {
case kCameraRotate0:
*degrees = 0;
return 0;
case kCameraRotate90:
*degrees = 90;
return 0;
case kCameraRotate180:
*degrees = 180;
return 0;
case kCameraRotate270:
*degrees = 270;
return 0;
}
return -1;
}
int32_t VideoCaptureImpl::ChangeUniqueId(const int32_t id)
{
_id = id;
return 0;
}
// returns the number of milliseconds until the module want a worker thread to call Process
int64_t VideoCaptureImpl::TimeUntilNextProcess()
{
CriticalSectionScoped cs(&_callBackCs);
const int64_t kProcessIntervalMs = 300;
return kProcessIntervalMs -
(TickTime::Now() - _lastProcessTime).Milliseconds();
}
// Process any pending tasks such as timeouts
int32_t VideoCaptureImpl::Process()
{
CriticalSectionScoped cs(&_callBackCs);
const TickTime now = TickTime::Now();
_lastProcessTime = TickTime::Now();
// Handle No picture alarm
if (_lastProcessFrameCount.Ticks() == _incomingFrameTimes[0].Ticks() &&
_captureAlarm != Raised)
{
if (_noPictureAlarmCallBack && _captureCallBack)
{
_captureAlarm = Raised;
_captureCallBack->OnNoPictureAlarm(_id, _captureAlarm);
}
}
else if (_lastProcessFrameCount.Ticks() != _incomingFrameTimes[0].Ticks() &&
_captureAlarm != Cleared)
{
if (_noPictureAlarmCallBack && _captureCallBack)
{
_captureAlarm = Cleared;
_captureCallBack->OnNoPictureAlarm(_id, _captureAlarm);
}
}
// Handle frame rate callback
if ((now - _lastFrameRateCallbackTime).Milliseconds()
> kFrameRateCallbackInterval)
{
if (_frameRateCallBack && _captureCallBack)
{
const uint32_t frameRate = CalculateFrameRate(now);
_captureCallBack->OnCaptureFrameRate(_id, frameRate);
}
_lastFrameRateCallbackTime = now; // Can be set by EnableFrameRateCallback
}
_lastProcessFrameCount = _incomingFrameTimes[0];
return 0;
}
VideoCaptureImpl::VideoCaptureImpl(const int32_t id)
: _id(id),
_deviceUniqueId(NULL),
_apiCs(*CriticalSectionWrapper::CreateCriticalSection()),
_captureDelay(0),
_requestedCapability(),
_callBackCs(*CriticalSectionWrapper::CreateCriticalSection()),
_lastProcessTime(TickTime::Now()),
_lastFrameRateCallbackTime(TickTime::Now()),
_frameRateCallBack(false),
_noPictureAlarmCallBack(false),
_captureAlarm(Cleared),
_setCaptureDelay(0),
_dataCallBack(NULL),
_captureCallBack(NULL),
_lastProcessFrameCount(TickTime::Now()),
_rotateFrame(kRotateNone),
last_capture_time_(0),
delta_ntp_internal_ms_(
Clock::GetRealTimeClock()->CurrentNtpInMilliseconds() -
TickTime::MillisecondTimestamp()) {
_requestedCapability.width = kDefaultWidth;
_requestedCapability.height = kDefaultHeight;
_requestedCapability.maxFPS = 30;
_requestedCapability.rawType = kVideoI420;
_requestedCapability.codecType = kVideoCodecUnknown;
memset(_incomingFrameTimes, 0, sizeof(_incomingFrameTimes));
}
VideoCaptureImpl::~VideoCaptureImpl()
{
DeRegisterCaptureDataCallback();
DeRegisterCaptureCallback();
delete &_callBackCs;
delete &_apiCs;
if (_deviceUniqueId)
delete[] _deviceUniqueId;
}
void VideoCaptureImpl::RegisterCaptureDataCallback(
VideoCaptureDataCallback& dataCallBack) {
CriticalSectionScoped cs(&_apiCs);
CriticalSectionScoped cs2(&_callBackCs);
_dataCallBack = &dataCallBack;
}
void VideoCaptureImpl::DeRegisterCaptureDataCallback() {
CriticalSectionScoped cs(&_apiCs);
CriticalSectionScoped cs2(&_callBackCs);
_dataCallBack = NULL;
}
void VideoCaptureImpl::RegisterCaptureCallback(VideoCaptureFeedBack& callBack) {
CriticalSectionScoped cs(&_apiCs);
CriticalSectionScoped cs2(&_callBackCs);
_captureCallBack = &callBack;
}
void VideoCaptureImpl::DeRegisterCaptureCallback() {
CriticalSectionScoped cs(&_apiCs);
CriticalSectionScoped cs2(&_callBackCs);
_captureCallBack = NULL;
}
void VideoCaptureImpl::SetCaptureDelay(int32_t delayMS) {
CriticalSectionScoped cs(&_apiCs);
_captureDelay = delayMS;
}
int32_t VideoCaptureImpl::CaptureDelay()
{
CriticalSectionScoped cs(&_apiCs);
return _setCaptureDelay;
}
int32_t VideoCaptureImpl::DeliverCapturedFrame(I420VideoFrame& captureFrame,
int64_t capture_time) {
UpdateFrameCount(); // frame count used for local frame rate callback.
const bool callOnCaptureDelayChanged = _setCaptureDelay != _captureDelay;
// Capture delay changed
if (_setCaptureDelay != _captureDelay) {
_setCaptureDelay = _captureDelay;
}
// Set the capture time
if (capture_time != 0) {
captureFrame.set_render_time_ms(capture_time - delta_ntp_internal_ms_);
} else {
captureFrame.set_render_time_ms(TickTime::MillisecondTimestamp());
}
if (captureFrame.render_time_ms() == last_capture_time_) {
// We don't allow the same capture time for two frames, drop this one.
return -1;
}
last_capture_time_ = captureFrame.render_time_ms();
if (_dataCallBack) {
if (callOnCaptureDelayChanged) {
_dataCallBack->OnCaptureDelayChanged(_id, _captureDelay);
}
_dataCallBack->OnIncomingCapturedFrame(_id, captureFrame);
}
return 0;
}
int32_t VideoCaptureImpl::IncomingFrame(
uint8_t* videoFrame,
size_t videoFrameLength,
const VideoCaptureCapability& frameInfo,
int64_t captureTime/*=0*/)
{
CriticalSectionScoped cs(&_apiCs);
CriticalSectionScoped cs2(&_callBackCs);
const int32_t width = frameInfo.width;
const int32_t height = frameInfo.height;
TRACE_EVENT1("webrtc", "VC::IncomingFrame", "capture_time", captureTime);
if (frameInfo.codecType == kVideoCodecUnknown)
{
// Not encoded, convert to I420.
const VideoType commonVideoType =
RawVideoTypeToCommonVideoVideoType(frameInfo.rawType);
if (frameInfo.rawType != kVideoMJPEG &&
CalcBufferSize(commonVideoType, width,
abs(height)) != videoFrameLength)
{
LOG(LS_ERROR) << "Wrong incoming frame length.";
return -1;
}
int stride_y = width;
int stride_uv = (width + 1) / 2;
int target_width = width;
int target_height = height;
// Rotating resolution when for 90/270 degree rotations.
if (_rotateFrame == kRotate90 || _rotateFrame == kRotate270) {
target_width = abs(height);
target_height = width;
}
// TODO(mikhal): Update correct aligned stride values.
//Calc16ByteAlignedStride(target_width, &stride_y, &stride_uv);
// Setting absolute height (in case it was negative).
// In Windows, the image starts bottom left, instead of top left.
// Setting a negative source height, inverts the image (within LibYuv).
int ret = _captureFrame.CreateEmptyFrame(target_width,
abs(target_height),
stride_y,
stride_uv, stride_uv);
if (ret < 0)
{
LOG(LS_ERROR) << "Failed to create empty frame, this should only "
"happen due to bad parameters.";
return -1;
}
const int conversionResult = ConvertToI420(commonVideoType,
videoFrame,
0, 0, // No cropping
width, height,
videoFrameLength,
_rotateFrame,
&_captureFrame);
if (conversionResult < 0)
{
LOG(LS_ERROR) << "Failed to convert capture frame from type "
<< frameInfo.rawType << "to I420.";
return -1;
}
DeliverCapturedFrame(_captureFrame, captureTime);
}
else // Encoded format
{
assert(false);
return -1;
}
return 0;
}
int32_t VideoCaptureImpl::IncomingI420VideoFrame(I420VideoFrame* video_frame,
int64_t captureTime) {
CriticalSectionScoped cs(&_apiCs);
CriticalSectionScoped cs2(&_callBackCs);
DeliverCapturedFrame(*video_frame, captureTime);
return 0;
}
int32_t VideoCaptureImpl::SetCaptureRotation(VideoCaptureRotation rotation) {
CriticalSectionScoped cs(&_apiCs);
CriticalSectionScoped cs2(&_callBackCs);
switch (rotation){
case kCameraRotate0:
_rotateFrame = kRotateNone;
break;
case kCameraRotate90:
_rotateFrame = kRotate90;
break;
case kCameraRotate180:
_rotateFrame = kRotate180;
break;
case kCameraRotate270:
_rotateFrame = kRotate270;
break;
default:
return -1;
}
return 0;
}
void VideoCaptureImpl::EnableFrameRateCallback(const bool enable) {
CriticalSectionScoped cs(&_apiCs);
CriticalSectionScoped cs2(&_callBackCs);
_frameRateCallBack = enable;
if (enable)
{
_lastFrameRateCallbackTime = TickTime::Now();
}
}
void VideoCaptureImpl::EnableNoPictureAlarm(const bool enable) {
CriticalSectionScoped cs(&_apiCs);
CriticalSectionScoped cs2(&_callBackCs);
_noPictureAlarmCallBack = enable;
}
void VideoCaptureImpl::UpdateFrameCount()
{
if (_incomingFrameTimes[0].MicrosecondTimestamp() == 0)
{
// first no shift
}
else
{
// shift
for (int i = (kFrameRateCountHistorySize - 2); i >= 0; i--)
{
_incomingFrameTimes[i + 1] = _incomingFrameTimes[i];
}
}
_incomingFrameTimes[0] = TickTime::Now();
}
uint32_t VideoCaptureImpl::CalculateFrameRate(const TickTime& now)
{
int32_t num = 0;
int32_t nrOfFrames = 0;
for (num = 1; num < (kFrameRateCountHistorySize - 1); num++)
{
if (_incomingFrameTimes[num].Ticks() <= 0
|| (now - _incomingFrameTimes[num]).Milliseconds() > kFrameRateHistoryWindowMs) // don't use data older than 2sec
{
break;
}
else
{
nrOfFrames++;
}
}
if (num > 1)
{
int64_t diff = (now - _incomingFrameTimes[num - 1]).Milliseconds();
if (diff > 0)
{
return uint32_t((nrOfFrames * 1000.0f / diff) + 0.5f);
}
}
return nrOfFrames;
}
} // namespace videocapturemodule
} // namespace webrtc
|