2011-07-07 08:21:25 +00:00
|
|
|
/*
|
2012-02-16 18:18:21 +00:00
|
|
|
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
2011-07-07 08:21:25 +00:00
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2013-07-12 10:03:52 +00:00
|
|
|
#include "webrtc/modules/video_capture/video_capture_impl.h"
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2012-04-23 21:24:02 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
2017-01-10 07:44:26 -08:00
|
|
|
#include "webrtc/api/video/i420_buffer.h"
|
2013-07-12 10:03:52 +00:00
|
|
|
#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
|
2015-11-04 08:31:52 +01:00
|
|
|
#include "webrtc/modules/include/module_common_types.h"
|
2013-07-12 10:03:52 +00:00
|
|
|
#include "webrtc/modules/video_capture/video_capture_config.h"
|
2017-07-06 19:44:34 +02:00
|
|
|
#include "webrtc/rtc_base/logging.h"
|
|
|
|
|
#include "webrtc/rtc_base/refcount.h"
|
|
|
|
|
#include "webrtc/rtc_base/timeutils.h"
|
|
|
|
|
#include "webrtc/rtc_base/trace_event.h"
|
2015-10-28 18:17:40 +01:00
|
|
|
#include "webrtc/system_wrappers/include/clock.h"
|
2013-07-12 10:03:52 +00:00
|
|
|
|
2016-03-21 16:44:31 +01:00
|
|
|
namespace webrtc {
|
|
|
|
|
namespace videocapturemodule {
|
|
|
|
|
rtc::scoped_refptr<VideoCaptureModule> VideoCaptureImpl::Create(
|
|
|
|
|
VideoCaptureExternal*& externalCapture) {
|
|
|
|
|
rtc::scoped_refptr<VideoCaptureImpl> implementation(
|
2016-12-12 00:22:56 -08:00
|
|
|
new rtc::RefCountedObject<VideoCaptureImpl>());
|
2016-03-21 16:44:31 +01:00
|
|
|
externalCapture = implementation.get();
|
|
|
|
|
return implementation;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2017-04-11 10:34:31 -07:00
|
|
|
const char* VideoCaptureImpl::CurrentDeviceName() const {
|
|
|
|
|
return _deviceUniqueId;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-10-03 18:23:13 +00:00
|
|
|
// static
|
|
|
|
|
int32_t VideoCaptureImpl::RotationFromDegrees(int degrees,
|
2015-02-13 14:31:26 +00:00
|
|
|
VideoRotation* rotation) {
|
2013-10-03 18:23:13 +00:00
|
|
|
switch (degrees) {
|
|
|
|
|
case 0:
|
2015-02-13 14:31:26 +00:00
|
|
|
*rotation = kVideoRotation_0;
|
2013-10-03 18:23:13 +00:00
|
|
|
return 0;
|
|
|
|
|
case 90:
|
2015-02-13 14:31:26 +00:00
|
|
|
*rotation = kVideoRotation_90;
|
2013-10-03 18:23:13 +00:00
|
|
|
return 0;
|
|
|
|
|
case 180:
|
2015-02-13 14:31:26 +00:00
|
|
|
*rotation = kVideoRotation_180;
|
2013-10-03 18:23:13 +00:00
|
|
|
return 0;
|
|
|
|
|
case 270:
|
2015-02-13 14:31:26 +00:00
|
|
|
*rotation = kVideoRotation_270;
|
2013-10-03 18:23:13 +00:00
|
|
|
return 0;
|
|
|
|
|
default:
|
|
|
|
|
return -1;;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// static
|
2015-02-13 14:31:26 +00:00
|
|
|
int32_t VideoCaptureImpl::RotationInDegrees(VideoRotation rotation,
|
2013-10-03 18:23:13 +00:00
|
|
|
int* degrees) {
|
|
|
|
|
switch (rotation) {
|
2015-02-13 14:31:26 +00:00
|
|
|
case kVideoRotation_0:
|
2013-10-03 18:23:13 +00:00
|
|
|
*degrees = 0;
|
|
|
|
|
return 0;
|
2015-02-13 14:31:26 +00:00
|
|
|
case kVideoRotation_90:
|
2013-10-03 18:23:13 +00:00
|
|
|
*degrees = 90;
|
|
|
|
|
return 0;
|
2015-02-13 14:31:26 +00:00
|
|
|
case kVideoRotation_180:
|
2013-10-03 18:23:13 +00:00
|
|
|
*degrees = 180;
|
|
|
|
|
return 0;
|
2015-02-13 14:31:26 +00:00
|
|
|
case kVideoRotation_270:
|
2013-10-03 18:23:13 +00:00
|
|
|
*degrees = 270;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-12 00:22:56 -08:00
|
|
|
VideoCaptureImpl::VideoCaptureImpl()
|
|
|
|
|
: _deviceUniqueId(NULL),
|
2013-07-02 10:15:43 +00:00
|
|
|
_requestedCapability(),
|
2016-05-10 16:31:47 +02:00
|
|
|
_lastProcessTimeNanos(rtc::TimeNanos()),
|
|
|
|
|
_lastFrameRateCallbackTimeNanos(rtc::TimeNanos()),
|
2013-07-02 10:15:43 +00:00
|
|
|
_dataCallBack(NULL),
|
2016-05-10 16:31:47 +02:00
|
|
|
_lastProcessFrameTimeNanos(rtc::TimeNanos()),
|
2015-03-09 17:07:31 +00:00
|
|
|
_rotateFrame(kVideoRotation_0),
|
2016-03-18 11:38:26 -07:00
|
|
|
apply_rotation_(false) {
|
2011-07-07 08:21:25 +00:00
|
|
|
_requestedCapability.width = kDefaultWidth;
|
|
|
|
|
_requestedCapability.height = kDefaultHeight;
|
|
|
|
|
_requestedCapability.maxFPS = 30;
|
2017-04-28 07:18:05 -07:00
|
|
|
_requestedCapability.videoType = VideoType::kI420;
|
2016-05-10 16:31:47 +02:00
|
|
|
memset(_incomingFrameTimesNanos, 0, sizeof(_incomingFrameTimesNanos));
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VideoCaptureImpl::~VideoCaptureImpl()
|
|
|
|
|
{
|
|
|
|
|
DeRegisterCaptureDataCallback();
|
|
|
|
|
if (_deviceUniqueId)
|
|
|
|
|
delete[] _deviceUniqueId;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-29 00:56:02 +00:00
|
|
|
void VideoCaptureImpl::RegisterCaptureDataCallback(
|
2016-12-12 00:22:56 -08:00
|
|
|
rtc::VideoSinkInterface<VideoFrame>* dataCallBack) {
|
2017-03-31 02:03:55 -07:00
|
|
|
rtc::CritScope cs(&_apiCs);
|
2016-12-12 00:22:56 -08:00
|
|
|
_dataCallBack = dataCallBack;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2014-01-29 00:56:02 +00:00
|
|
|
void VideoCaptureImpl::DeRegisterCaptureDataCallback() {
|
2017-03-31 02:03:55 -07:00
|
|
|
rtc::CritScope cs(&_apiCs);
|
2011-07-07 08:21:25 +00:00
|
|
|
_dataCallBack = NULL;
|
|
|
|
|
}
|
2015-05-29 17:21:40 -07:00
|
|
|
int32_t VideoCaptureImpl::DeliverCapturedFrame(VideoFrame& captureFrame) {
|
2012-10-11 15:03:53 +00:00
|
|
|
UpdateFrameCount(); // frame count used for local frame rate callback.
|
|
|
|
|
|
|
|
|
|
if (_dataCallBack) {
|
2016-12-12 00:22:56 -08:00
|
|
|
_dataCallBack->OnFrame(captureFrame);
|
2012-10-11 15:03:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-10 08:23:13 +00:00
|
|
|
int32_t VideoCaptureImpl::IncomingFrame(
|
|
|
|
|
uint8_t* videoFrame,
|
Use size_t more consistently for packet/payload lengths.
See design doc at https://docs.google.com/a/chromium.org/document/d/1I6nmE9D_BmCY-IoV6MDPY2V6WYpEI-dg2apWXTfZyUI/edit?usp=sharing for more information.
This CL was reviewed and approved in pieces in the following CLs:
https://webrtc-codereview.appspot.com/24209004/
https://webrtc-codereview.appspot.com/24229004/
https://webrtc-codereview.appspot.com/24259004/
https://webrtc-codereview.appspot.com/25109004/
https://webrtc-codereview.appspot.com/26099004/
https://webrtc-codereview.appspot.com/27069004/
https://webrtc-codereview.appspot.com/27969004/
https://webrtc-codereview.appspot.com/27989004/
https://webrtc-codereview.appspot.com/29009004/
https://webrtc-codereview.appspot.com/30929004/
https://webrtc-codereview.appspot.com/30939004/
https://webrtc-codereview.appspot.com/31999004/
Committing as TBR to the original reviewers.
BUG=chromium:81439
TEST=none
TBR=pthatcher,henrik.lundin,tina.legrand,stefan,tkchin,glaznev,kjellander,perkj,mflodman,henrika,asapersson,niklas.enbom
Review URL: https://webrtc-codereview.appspot.com/23129004
git-svn-id: http://webrtc.googlecode.com/svn/trunk@7726 4adac7df-926f-26a2-2b94-8c16560cd09d
2014-11-20 22:28:14 +00:00
|
|
|
size_t videoFrameLength,
|
2012-02-23 09:00:26 +00:00
|
|
|
const VideoCaptureCapability& frameInfo,
|
2013-04-10 08:23:13 +00:00
|
|
|
int64_t captureTime/*=0*/)
|
2011-07-07 08:21:25 +00:00
|
|
|
{
|
2017-03-31 02:03:55 -07:00
|
|
|
rtc::CritScope cs(&_apiCs);
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2013-04-10 08:23:13 +00:00
|
|
|
const int32_t width = frameInfo.width;
|
|
|
|
|
const int32_t height = frameInfo.height;
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2013-04-09 19:54:10 +00:00
|
|
|
TRACE_EVENT1("webrtc", "VC::IncomingFrame", "capture_time", captureTime);
|
|
|
|
|
|
2017-02-20 23:27:37 -08:00
|
|
|
// Not encoded, convert to I420.
|
2017-04-28 07:18:05 -07:00
|
|
|
if (frameInfo.videoType != VideoType::kMJPEG &&
|
|
|
|
|
CalcBufferSize(frameInfo.videoType, width, abs(height)) !=
|
2017-04-11 10:34:31 -07:00
|
|
|
videoFrameLength) {
|
|
|
|
|
LOG(LS_ERROR) << "Wrong incoming frame length.";
|
|
|
|
|
return -1;
|
2017-02-20 23:27:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int stride_y = width;
|
|
|
|
|
int stride_uv = (width + 1) / 2;
|
|
|
|
|
int target_width = width;
|
|
|
|
|
int target_height = height;
|
|
|
|
|
|
|
|
|
|
// SetApplyRotation doesn't take any lock. Make a local copy here.
|
|
|
|
|
bool apply_rotation = apply_rotation_;
|
|
|
|
|
|
|
|
|
|
if (apply_rotation) {
|
|
|
|
|
// Rotating resolution when for 90/270 degree rotations.
|
|
|
|
|
if (_rotateFrame == kVideoRotation_90 ||
|
|
|
|
|
_rotateFrame == kVideoRotation_270) {
|
|
|
|
|
target_width = abs(height);
|
|
|
|
|
target_height = width;
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2017-02-20 23:27:37 -08:00
|
|
|
|
|
|
|
|
// 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).
|
|
|
|
|
|
|
|
|
|
// TODO(nisse): Use a pool?
|
|
|
|
|
rtc::scoped_refptr<I420Buffer> buffer = I420Buffer::Create(
|
|
|
|
|
target_width, abs(target_height), stride_y, stride_uv, stride_uv);
|
|
|
|
|
const int conversionResult = ConvertToI420(
|
2017-04-28 07:18:05 -07:00
|
|
|
frameInfo.videoType, videoFrame, 0, 0, // No cropping
|
2017-02-20 23:27:37 -08:00
|
|
|
width, height, videoFrameLength,
|
|
|
|
|
apply_rotation ? _rotateFrame : kVideoRotation_0, buffer.get());
|
2017-04-11 10:34:31 -07:00
|
|
|
if (conversionResult < 0) {
|
2017-02-20 23:27:37 -08:00
|
|
|
LOG(LS_ERROR) << "Failed to convert capture frame from type "
|
2017-04-28 07:18:05 -07:00
|
|
|
<< static_cast<int>(frameInfo.videoType) << "to I420.";
|
2017-04-11 10:34:31 -07:00
|
|
|
return -1;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2017-04-11 10:34:31 -07:00
|
|
|
VideoFrame captureFrame(buffer, 0, rtc::TimeMillis(),
|
|
|
|
|
!apply_rotation ? _rotateFrame : kVideoRotation_0);
|
2017-02-20 23:27:37 -08:00
|
|
|
captureFrame.set_ntp_time_ms(captureTime);
|
|
|
|
|
|
|
|
|
|
DeliverCapturedFrame(captureFrame);
|
|
|
|
|
|
2011-07-07 08:21:25 +00:00
|
|
|
return 0;
|
2011-10-14 17:16:04 +00:00
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2015-02-13 14:31:26 +00:00
|
|
|
int32_t VideoCaptureImpl::SetCaptureRotation(VideoRotation rotation) {
|
2017-03-31 02:03:55 -07:00
|
|
|
rtc::CritScope cs(&_apiCs);
|
2015-03-09 17:07:31 +00:00
|
|
|
_rotateFrame = rotation;
|
2012-11-19 21:15:35 +00:00
|
|
|
return 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2015-02-11 18:37:54 +00:00
|
|
|
bool VideoCaptureImpl::SetApplyRotation(bool enable) {
|
2015-04-01 15:33:06 -07:00
|
|
|
// We can't take any lock here as it'll cause deadlock with IncomingFrame.
|
|
|
|
|
|
2015-02-11 18:37:54 +00:00
|
|
|
// The effect of this is the last caller wins.
|
|
|
|
|
apply_rotation_ = enable;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-11 10:34:31 -07:00
|
|
|
void VideoCaptureImpl::UpdateFrameCount() {
|
|
|
|
|
if (_incomingFrameTimesNanos[0] / rtc::kNumNanosecsPerMicrosec == 0) {
|
|
|
|
|
// first no shift
|
|
|
|
|
} else {
|
|
|
|
|
// shift
|
|
|
|
|
for (int i = (kFrameRateCountHistorySize - 2); i >= 0; --i) {
|
|
|
|
|
_incomingFrameTimesNanos[i + 1] = _incomingFrameTimesNanos[i];
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2017-04-11 10:34:31 -07:00
|
|
|
}
|
|
|
|
|
_incomingFrameTimesNanos[0] = rtc::TimeNanos();
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2017-04-11 10:34:31 -07:00
|
|
|
uint32_t VideoCaptureImpl::CalculateFrameRate(int64_t now_ns) {
|
|
|
|
|
int32_t num = 0;
|
|
|
|
|
int32_t nrOfFrames = 0;
|
|
|
|
|
for (num = 1; num < (kFrameRateCountHistorySize - 1); ++num) {
|
|
|
|
|
if (_incomingFrameTimesNanos[num] <= 0 ||
|
|
|
|
|
(now_ns - _incomingFrameTimesNanos[num]) /
|
|
|
|
|
rtc::kNumNanosecsPerMillisec >
|
|
|
|
|
kFrameRateHistoryWindowMs) { // don't use data older than 2sec
|
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
nrOfFrames++;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2017-04-11 10:34:31 -07:00
|
|
|
}
|
|
|
|
|
if (num > 1) {
|
|
|
|
|
int64_t diff = (now_ns - _incomingFrameTimesNanos[num - 1]) /
|
|
|
|
|
rtc::kNumNanosecsPerMillisec;
|
|
|
|
|
if (diff > 0) {
|
|
|
|
|
return uint32_t((nrOfFrames * 1000.0f / diff) + 0.5f);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2017-04-11 10:34:31 -07:00
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2017-04-11 10:34:31 -07:00
|
|
|
return nrOfFrames;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2013-07-03 15:12:26 +00:00
|
|
|
} // namespace videocapturemodule
|
|
|
|
|
} // namespace webrtc
|