2013-09-14 00:25:28 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2013 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/common_types.h"
|
|
|
|
|
|
2013-10-28 10:16:14 +00:00
|
|
|
#include <algorithm> // std::max
|
|
|
|
|
|
2015-02-19 17:43:25 +00:00
|
|
|
#include "webrtc/base/checks.h"
|
2013-09-14 00:25:28 +00:00
|
|
|
#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
|
|
|
|
|
#include "webrtc/modules/video_coding/codecs/interface/video_codec_interface.h"
|
|
|
|
|
#include "webrtc/modules/video_coding/main/source/encoded_frame.h"
|
|
|
|
|
#include "webrtc/modules/video_coding/main/source/video_coding_impl.h"
|
|
|
|
|
#include "webrtc/system_wrappers/interface/clock.h"
|
2014-04-11 14:08:35 +00:00
|
|
|
#include "webrtc/system_wrappers/interface/logging.h"
|
2013-09-14 00:25:28 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
namespace vcm {
|
|
|
|
|
|
2014-01-08 12:38:22 +00:00
|
|
|
class DebugRecorder {
|
|
|
|
|
public:
|
|
|
|
|
DebugRecorder()
|
|
|
|
|
: cs_(CriticalSectionWrapper::CreateCriticalSection()), file_(NULL) {}
|
|
|
|
|
|
|
|
|
|
~DebugRecorder() { Stop(); }
|
|
|
|
|
|
|
|
|
|
int Start(const char* file_name_utf8) {
|
|
|
|
|
CriticalSectionScoped cs(cs_.get());
|
|
|
|
|
if (file_)
|
|
|
|
|
fclose(file_);
|
|
|
|
|
file_ = fopen(file_name_utf8, "wb");
|
|
|
|
|
if (!file_)
|
|
|
|
|
return VCM_GENERAL_ERROR;
|
|
|
|
|
return VCM_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Stop() {
|
|
|
|
|
CriticalSectionScoped cs(cs_.get());
|
|
|
|
|
if (file_) {
|
|
|
|
|
fclose(file_);
|
|
|
|
|
file_ = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Add(const I420VideoFrame& frame) {
|
|
|
|
|
CriticalSectionScoped cs(cs_.get());
|
|
|
|
|
if (file_)
|
|
|
|
|
PrintI420VideoFrame(frame, file_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2015-02-26 14:34:55 +00:00
|
|
|
rtc::scoped_ptr<CriticalSectionWrapper> cs_;
|
2014-01-08 12:38:22 +00:00
|
|
|
FILE* file_ GUARDED_BY(cs_);
|
|
|
|
|
};
|
|
|
|
|
|
2014-04-11 14:08:35 +00:00
|
|
|
VideoSender::VideoSender(Clock* clock,
|
2015-02-26 13:15:22 +00:00
|
|
|
EncodedImageCallback* post_encode_callback,
|
|
|
|
|
VideoEncoderRateObserver* encoder_rate_observer)
|
2014-04-11 14:08:35 +00:00
|
|
|
: clock_(clock),
|
2014-01-08 12:38:22 +00:00
|
|
|
recorder_(new DebugRecorder()),
|
2013-09-18 11:57:34 +00:00
|
|
|
process_crit_sect_(CriticalSectionWrapper::CreateCriticalSection()),
|
2013-09-14 00:25:28 +00:00
|
|
|
_sendCritSect(CriticalSectionWrapper::CreateCriticalSection()),
|
|
|
|
|
_encoder(),
|
2014-01-09 08:01:57 +00:00
|
|
|
_encodedFrameCallback(post_encode_callback),
|
2013-09-14 00:25:28 +00:00
|
|
|
_nextFrameTypes(1, kVideoFrameDelta),
|
2014-04-11 14:08:35 +00:00
|
|
|
_mediaOpt(clock_),
|
2013-09-14 00:25:28 +00:00
|
|
|
_sendStatsCallback(NULL),
|
2015-02-26 13:15:22 +00:00
|
|
|
_codecDataBase(encoder_rate_observer),
|
2013-09-14 00:25:28 +00:00
|
|
|
frame_dropper_enabled_(true),
|
2013-12-19 10:59:48 +00:00
|
|
|
_sendStatsTimer(1000, clock_),
|
2015-02-19 17:43:25 +00:00
|
|
|
current_codec_(),
|
2013-12-19 10:59:48 +00:00
|
|
|
qm_settings_callback_(NULL),
|
2015-02-19 17:43:25 +00:00
|
|
|
protection_callback_(NULL) {
|
2015-03-05 12:21:54 +00:00
|
|
|
// Allow VideoSender to be created on one thread but used on another, post
|
|
|
|
|
// construction. This is currently how this class is being used by at least
|
|
|
|
|
// one external project (diffractor).
|
|
|
|
|
main_thread_.DetachFromThread();
|
2015-02-19 17:43:25 +00:00
|
|
|
}
|
2013-09-14 00:25:28 +00:00
|
|
|
|
|
|
|
|
VideoSender::~VideoSender() {
|
|
|
|
|
delete _sendCritSect;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t VideoSender::Process() {
|
|
|
|
|
int32_t returnValue = VCM_OK;
|
|
|
|
|
|
|
|
|
|
if (_sendStatsTimer.TimeUntilProcess() == 0) {
|
|
|
|
|
_sendStatsTimer.Processed();
|
2013-09-18 11:57:34 +00:00
|
|
|
CriticalSectionScoped cs(process_crit_sect_.get());
|
2013-09-14 00:25:28 +00:00
|
|
|
if (_sendStatsCallback != NULL) {
|
2015-02-19 17:43:25 +00:00
|
|
|
uint32_t bitRate = _mediaOpt.SentBitRate();
|
|
|
|
|
uint32_t frameRate = _mediaOpt.SentFrameRate();
|
2013-09-14 00:25:28 +00:00
|
|
|
_sendStatsCallback->SendStatistics(bitRate, frameRate);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return returnValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reset send side to initial state - all components
|
|
|
|
|
int32_t VideoSender::InitializeSender() {
|
2015-02-19 17:43:25 +00:00
|
|
|
DCHECK(main_thread_.CalledOnValidThread());
|
2013-09-14 00:25:28 +00:00
|
|
|
CriticalSectionScoped cs(_sendCritSect);
|
|
|
|
|
_codecDataBase.ResetSender();
|
|
|
|
|
_encoder = NULL;
|
|
|
|
|
_encodedFrameCallback.SetTransportCallback(NULL);
|
|
|
|
|
_mediaOpt.Reset(); // Resetting frame dropper
|
|
|
|
|
return VCM_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-15 22:09:40 +00:00
|
|
|
int64_t VideoSender::TimeUntilNextProcess() {
|
2013-09-14 00:25:28 +00:00
|
|
|
return _sendStatsTimer.TimeUntilProcess();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Register the send codec to be used.
|
|
|
|
|
int32_t VideoSender::RegisterSendCodec(const VideoCodec* sendCodec,
|
|
|
|
|
uint32_t numberOfCores,
|
|
|
|
|
uint32_t maxPayloadSize) {
|
2015-02-19 17:43:25 +00:00
|
|
|
DCHECK(main_thread_.CalledOnValidThread());
|
2013-09-14 00:25:28 +00:00
|
|
|
CriticalSectionScoped cs(_sendCritSect);
|
|
|
|
|
if (sendCodec == NULL) {
|
|
|
|
|
return VCM_PARAMETER_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ret = _codecDataBase.SetSendCodec(
|
|
|
|
|
sendCodec, numberOfCores, maxPayloadSize, &_encodedFrameCallback);
|
2013-09-17 09:38:41 +00:00
|
|
|
|
|
|
|
|
// Update encoder regardless of result to make sure that we're not holding on
|
|
|
|
|
// to a deleted instance.
|
|
|
|
|
_encoder = _codecDataBase.GetEncoder();
|
2015-02-19 17:43:25 +00:00
|
|
|
// Cache the current codec here so they can be fetched from this thread
|
|
|
|
|
// without requiring the _sendCritSect lock.
|
|
|
|
|
current_codec_ = *sendCodec;
|
2013-09-17 09:38:41 +00:00
|
|
|
|
2013-09-14 00:25:28 +00:00
|
|
|
if (!ret) {
|
2014-04-11 14:08:35 +00:00
|
|
|
LOG(LS_ERROR) << "Failed to initialize the encoder with payload name "
|
|
|
|
|
<< sendCodec->plName << ". Error code: " << ret;
|
2013-09-14 00:25:28 +00:00
|
|
|
return VCM_CODEC_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-17 09:38:41 +00:00
|
|
|
int numLayers = (sendCodec->codecType != kVideoCodecVP8)
|
2013-09-14 00:25:28 +00:00
|
|
|
? 1
|
|
|
|
|
: sendCodec->codecSpecific.VP8.numberOfTemporalLayers;
|
|
|
|
|
// If we have screensharing and we have layers, we disable frame dropper.
|
|
|
|
|
bool disable_frame_dropper =
|
|
|
|
|
numLayers > 1 && sendCodec->mode == kScreensharing;
|
|
|
|
|
if (disable_frame_dropper) {
|
|
|
|
|
_mediaOpt.EnableFrameDropper(false);
|
|
|
|
|
} else if (frame_dropper_enabled_) {
|
|
|
|
|
_mediaOpt.EnableFrameDropper(true);
|
|
|
|
|
}
|
|
|
|
|
_nextFrameTypes.clear();
|
|
|
|
|
_nextFrameTypes.resize(VCM_MAX(sendCodec->numberOfSimulcastStreams, 1),
|
|
|
|
|
kVideoFrameDelta);
|
|
|
|
|
|
2013-09-17 10:36:30 +00:00
|
|
|
_mediaOpt.SetEncodingData(sendCodec->codecType,
|
2013-09-14 00:25:28 +00:00
|
|
|
sendCodec->maxBitrate * 1000,
|
|
|
|
|
sendCodec->maxFramerate * 1000,
|
|
|
|
|
sendCodec->startBitrate * 1000,
|
|
|
|
|
sendCodec->width,
|
|
|
|
|
sendCodec->height,
|
2013-12-19 10:59:48 +00:00
|
|
|
numLayers,
|
|
|
|
|
maxPayloadSize);
|
2013-09-14 00:25:28 +00:00
|
|
|
return VCM_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-19 17:43:25 +00:00
|
|
|
const VideoCodec& VideoSender::GetSendCodec() const {
|
|
|
|
|
DCHECK(main_thread_.CalledOnValidThread());
|
|
|
|
|
return current_codec_;
|
|
|
|
|
}
|
2013-09-14 00:25:28 +00:00
|
|
|
|
2015-02-19 17:43:25 +00:00
|
|
|
int32_t VideoSender::SendCodecBlocking(VideoCodec* currentSendCodec) const {
|
|
|
|
|
CriticalSectionScoped cs(_sendCritSect);
|
2013-09-14 00:25:28 +00:00
|
|
|
if (currentSendCodec == NULL) {
|
|
|
|
|
return VCM_PARAMETER_ERROR;
|
|
|
|
|
}
|
|
|
|
|
return _codecDataBase.SendCodec(currentSendCodec) ? 0 : -1;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-19 17:43:25 +00:00
|
|
|
VideoCodecType VideoSender::SendCodecBlocking() const {
|
2013-09-14 00:25:28 +00:00
|
|
|
CriticalSectionScoped cs(_sendCritSect);
|
|
|
|
|
return _codecDataBase.SendCodec();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Register an external decoder object.
|
|
|
|
|
// This can not be used together with external decoder callbacks.
|
|
|
|
|
int32_t VideoSender::RegisterExternalEncoder(VideoEncoder* externalEncoder,
|
|
|
|
|
uint8_t payloadType,
|
|
|
|
|
bool internalSource /*= false*/) {
|
2015-03-07 20:55:56 +00:00
|
|
|
DCHECK(main_thread_.CalledOnValidThread());
|
|
|
|
|
|
2013-09-14 00:25:28 +00:00
|
|
|
CriticalSectionScoped cs(_sendCritSect);
|
|
|
|
|
|
|
|
|
|
if (externalEncoder == NULL) {
|
|
|
|
|
bool wasSendCodec = false;
|
|
|
|
|
const bool ret =
|
|
|
|
|
_codecDataBase.DeregisterExternalEncoder(payloadType, &wasSendCodec);
|
|
|
|
|
if (wasSendCodec) {
|
|
|
|
|
// Make sure the VCM doesn't use the de-registered codec
|
|
|
|
|
_encoder = NULL;
|
|
|
|
|
}
|
|
|
|
|
return ret ? 0 : -1;
|
|
|
|
|
}
|
|
|
|
|
_codecDataBase.RegisterExternalEncoder(
|
|
|
|
|
externalEncoder, payloadType, internalSource);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get codec config parameters
|
2013-12-19 10:59:48 +00:00
|
|
|
int32_t VideoSender::CodecConfigParameters(uint8_t* buffer,
|
|
|
|
|
int32_t size) const {
|
2013-09-14 00:25:28 +00:00
|
|
|
CriticalSectionScoped cs(_sendCritSect);
|
|
|
|
|
if (_encoder != NULL) {
|
|
|
|
|
return _encoder->CodecConfigParameters(buffer, size);
|
|
|
|
|
}
|
|
|
|
|
return VCM_UNINITIALIZED;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-19 10:59:48 +00:00
|
|
|
// TODO(andresp): Make const once media_opt is thread-safe and this has a
|
|
|
|
|
// pointer to it.
|
|
|
|
|
int32_t VideoSender::SentFrameCount(VCMFrameCount* frameCount) {
|
|
|
|
|
*frameCount = _mediaOpt.SentFrameCount();
|
|
|
|
|
return VCM_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-14 00:25:28 +00:00
|
|
|
// Get encode bitrate
|
|
|
|
|
int VideoSender::Bitrate(unsigned int* bitrate) const {
|
2015-03-07 20:55:56 +00:00
|
|
|
DCHECK(main_thread_.CalledOnValidThread());
|
|
|
|
|
// Since we're running on the thread that's the only thread known to modify
|
|
|
|
|
// the value of _encoder, we don't need to grab the lock here.
|
|
|
|
|
|
2013-09-14 00:25:28 +00:00
|
|
|
// return the bit rate which the encoder is set to
|
|
|
|
|
if (!_encoder) {
|
|
|
|
|
return VCM_UNINITIALIZED;
|
|
|
|
|
}
|
|
|
|
|
*bitrate = _encoder->BitRate();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get encode frame rate
|
|
|
|
|
int VideoSender::FrameRate(unsigned int* framerate) const {
|
2015-03-07 20:55:56 +00:00
|
|
|
DCHECK(main_thread_.CalledOnValidThread());
|
|
|
|
|
// Since we're running on the thread that's the only thread known to modify
|
|
|
|
|
// the value of _encoder, we don't need to grab the lock here.
|
|
|
|
|
|
2013-09-14 00:25:28 +00:00
|
|
|
// input frame rate, not compensated
|
|
|
|
|
if (!_encoder) {
|
|
|
|
|
return VCM_UNINITIALIZED;
|
|
|
|
|
}
|
|
|
|
|
*framerate = _encoder->FrameRate();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t VideoSender::SetChannelParameters(uint32_t target_bitrate,
|
|
|
|
|
uint8_t lossRate,
|
2015-01-12 21:51:21 +00:00
|
|
|
int64_t rtt) {
|
2015-03-07 20:55:56 +00:00
|
|
|
// TODO(tommi,mflodman): This method is called on the network thread via the
|
|
|
|
|
// OnNetworkChanged event (ViEEncoder::OnNetworkChanged). Could we instead
|
|
|
|
|
// post the updated information to the encoding thread and not grab a lock
|
|
|
|
|
// here? This effectively means that the network thread will be blocked for
|
|
|
|
|
// as much as frame encoding period.
|
|
|
|
|
|
|
|
|
|
CriticalSectionScoped sendCs(_sendCritSect);
|
|
|
|
|
uint32_t target_rate = _mediaOpt.SetTargetRates(target_bitrate,
|
|
|
|
|
lossRate,
|
|
|
|
|
rtt,
|
|
|
|
|
protection_callback_,
|
|
|
|
|
qm_settings_callback_);
|
|
|
|
|
uint32_t input_frame_rate = _mediaOpt.InputFrameRate();
|
|
|
|
|
|
|
|
|
|
int32_t ret = VCM_UNINITIALIZED;
|
|
|
|
|
static_assert(VCM_UNINITIALIZED < 0, "VCM_UNINITIALIZED must be negative.");
|
|
|
|
|
|
|
|
|
|
if (_encoder != NULL) {
|
|
|
|
|
ret = _encoder->SetChannelParameters(lossRate, rtt);
|
|
|
|
|
if (ret >= 0) {
|
|
|
|
|
ret = _encoder->SetRates(target_rate, input_frame_rate);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
2013-09-14 00:25:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t VideoSender::RegisterTransportCallback(
|
|
|
|
|
VCMPacketizationCallback* transport) {
|
|
|
|
|
CriticalSectionScoped cs(_sendCritSect);
|
|
|
|
|
_encodedFrameCallback.SetMediaOpt(&_mediaOpt);
|
|
|
|
|
_encodedFrameCallback.SetTransportCallback(transport);
|
|
|
|
|
return VCM_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Register video output information callback which will be called to deliver
|
|
|
|
|
// information about the video stream produced by the encoder, for instance the
|
|
|
|
|
// average frame rate and bit rate.
|
|
|
|
|
int32_t VideoSender::RegisterSendStatisticsCallback(
|
|
|
|
|
VCMSendStatisticsCallback* sendStats) {
|
2013-09-18 11:57:34 +00:00
|
|
|
CriticalSectionScoped cs(process_crit_sect_.get());
|
2013-09-14 00:25:28 +00:00
|
|
|
_sendStatsCallback = sendStats;
|
|
|
|
|
return VCM_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Register a video quality settings callback which will be called when frame
|
|
|
|
|
// rate/dimensions need to be updated for video quality optimization
|
|
|
|
|
int32_t VideoSender::RegisterVideoQMCallback(
|
2013-12-19 10:59:48 +00:00
|
|
|
VCMQMSettingsCallback* qm_settings_callback) {
|
2013-09-14 00:25:28 +00:00
|
|
|
CriticalSectionScoped cs(_sendCritSect);
|
2015-03-07 20:55:56 +00:00
|
|
|
DCHECK(qm_settings_callback_ == qm_settings_callback ||
|
|
|
|
|
!qm_settings_callback_ ||
|
|
|
|
|
!qm_settings_callback) << "Overwriting the previous callback?";
|
2013-12-19 10:59:48 +00:00
|
|
|
qm_settings_callback_ = qm_settings_callback;
|
|
|
|
|
_mediaOpt.EnableQM(qm_settings_callback_ != NULL);
|
|
|
|
|
return VCM_OK;
|
2013-09-14 00:25:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Register a video protection callback which will be called to deliver the
|
|
|
|
|
// requested FEC rate and NACK status (on/off).
|
|
|
|
|
int32_t VideoSender::RegisterProtectionCallback(
|
2013-12-19 10:59:48 +00:00
|
|
|
VCMProtectionCallback* protection_callback) {
|
2013-09-14 00:25:28 +00:00
|
|
|
CriticalSectionScoped cs(_sendCritSect);
|
2015-03-07 20:55:56 +00:00
|
|
|
DCHECK(protection_callback_ == protection_callback ||
|
|
|
|
|
!protection_callback_ ||
|
|
|
|
|
!protection_callback) << "Overwriting the previous callback?";
|
2013-12-19 10:59:48 +00:00
|
|
|
protection_callback_ = protection_callback;
|
2013-09-14 00:25:28 +00:00
|
|
|
return VCM_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Enable or disable a video protection method.
|
|
|
|
|
// Note: This API should be deprecated, as it does not offer a distinction
|
|
|
|
|
// between the protection method and decoding with or without errors. If such a
|
|
|
|
|
// behavior is desired, use the following API: SetReceiverRobustnessMode.
|
|
|
|
|
int32_t VideoSender::SetVideoProtection(VCMVideoProtection videoProtection,
|
|
|
|
|
bool enable) {
|
|
|
|
|
switch (videoProtection) {
|
|
|
|
|
case kProtectionNack:
|
|
|
|
|
case kProtectionNackSender: {
|
|
|
|
|
CriticalSectionScoped cs(_sendCritSect);
|
|
|
|
|
_mediaOpt.EnableProtectionMethod(enable, media_optimization::kNack);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case kProtectionNackFEC: {
|
|
|
|
|
CriticalSectionScoped cs(_sendCritSect);
|
|
|
|
|
_mediaOpt.EnableProtectionMethod(enable, media_optimization::kNackFec);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case kProtectionFEC: {
|
|
|
|
|
CriticalSectionScoped cs(_sendCritSect);
|
|
|
|
|
_mediaOpt.EnableProtectionMethod(enable, media_optimization::kFec);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case kProtectionPeriodicKeyFrames: {
|
|
|
|
|
CriticalSectionScoped cs(_sendCritSect);
|
|
|
|
|
return _codecDataBase.SetPeriodicKeyFrames(enable) ? 0 : -1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case kProtectionNackReceiver:
|
|
|
|
|
case kProtectionKeyOnLoss:
|
|
|
|
|
case kProtectionKeyOnKeyLoss:
|
|
|
|
|
// Ignore decoder modes.
|
|
|
|
|
return VCM_OK;
|
|
|
|
|
}
|
|
|
|
|
return VCM_OK;
|
|
|
|
|
}
|
|
|
|
|
// Add one raw video frame to the encoder, blocking.
|
|
|
|
|
int32_t VideoSender::AddVideoFrame(const I420VideoFrame& videoFrame,
|
|
|
|
|
const VideoContentMetrics* contentMetrics,
|
|
|
|
|
const CodecSpecificInfo* codecSpecificInfo) {
|
|
|
|
|
CriticalSectionScoped cs(_sendCritSect);
|
|
|
|
|
if (_encoder == NULL) {
|
|
|
|
|
return VCM_UNINITIALIZED;
|
|
|
|
|
}
|
|
|
|
|
// TODO(holmer): Add support for dropping frames per stream. Currently we
|
|
|
|
|
// only have one frame dropper for all streams.
|
|
|
|
|
if (_nextFrameTypes[0] == kFrameEmpty) {
|
|
|
|
|
return VCM_OK;
|
|
|
|
|
}
|
|
|
|
|
if (_mediaOpt.DropFrame()) {
|
2014-04-11 14:08:35 +00:00
|
|
|
return VCM_OK;
|
|
|
|
|
}
|
|
|
|
|
_mediaOpt.UpdateContentData(contentMetrics);
|
2015-03-05 13:57:37 +00:00
|
|
|
// TODO(pbos): Make sure setting send codec is synchronized with video
|
|
|
|
|
// processing so frame size always matches.
|
|
|
|
|
if (!_codecDataBase.MatchesCurrentResolution(videoFrame.width(),
|
|
|
|
|
videoFrame.height())) {
|
|
|
|
|
LOG(LS_ERROR) << "Incoming frame doesn't match set resolution. Dropping.";
|
|
|
|
|
return VCM_PARAMETER_ERROR;
|
|
|
|
|
}
|
2014-04-11 14:08:35 +00:00
|
|
|
int32_t ret =
|
|
|
|
|
_encoder->Encode(videoFrame, codecSpecificInfo, _nextFrameTypes);
|
|
|
|
|
recorder_->Add(videoFrame);
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
LOG(LS_ERROR) << "Failed to encode frame. Error code: " << ret;
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
for (size_t i = 0; i < _nextFrameTypes.size(); ++i) {
|
|
|
|
|
_nextFrameTypes[i] = kVideoFrameDelta; // Default frame type.
|
2013-09-14 00:25:28 +00:00
|
|
|
}
|
|
|
|
|
return VCM_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t VideoSender::IntraFrameRequest(int stream_index) {
|
|
|
|
|
CriticalSectionScoped cs(_sendCritSect);
|
|
|
|
|
if (stream_index < 0 ||
|
|
|
|
|
static_cast<unsigned int>(stream_index) >= _nextFrameTypes.size()) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
_nextFrameTypes[stream_index] = kVideoFrameKey;
|
|
|
|
|
if (_encoder != NULL && _encoder->InternalSource()) {
|
|
|
|
|
// Try to request the frame if we have an external encoder with
|
|
|
|
|
// internal source since AddVideoFrame never will be called.
|
|
|
|
|
if (_encoder->RequestFrame(_nextFrameTypes) == WEBRTC_VIDEO_CODEC_OK) {
|
|
|
|
|
_nextFrameTypes[stream_index] = kVideoFrameDelta;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return VCM_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t VideoSender::EnableFrameDropper(bool enable) {
|
|
|
|
|
CriticalSectionScoped cs(_sendCritSect);
|
|
|
|
|
frame_dropper_enabled_ = enable;
|
|
|
|
|
_mediaOpt.EnableFrameDropper(enable);
|
|
|
|
|
return VCM_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int VideoSender::SetSenderNackMode(SenderNackMode mode) {
|
|
|
|
|
switch (mode) {
|
|
|
|
|
case VideoCodingModule::kNackNone:
|
|
|
|
|
_mediaOpt.EnableProtectionMethod(false, media_optimization::kNack);
|
|
|
|
|
break;
|
|
|
|
|
case VideoCodingModule::kNackAll:
|
|
|
|
|
_mediaOpt.EnableProtectionMethod(true, media_optimization::kNack);
|
|
|
|
|
break;
|
|
|
|
|
case VideoCodingModule::kNackSelective:
|
|
|
|
|
return VCM_NOT_IMPLEMENTED;
|
|
|
|
|
}
|
|
|
|
|
return VCM_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int VideoSender::SetSenderReferenceSelection(bool enable) {
|
|
|
|
|
return VCM_NOT_IMPLEMENTED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int VideoSender::SetSenderFEC(bool enable) {
|
|
|
|
|
_mediaOpt.EnableProtectionMethod(enable, media_optimization::kFec);
|
|
|
|
|
return VCM_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int VideoSender::SetSenderKeyFramePeriod(int periodMs) {
|
|
|
|
|
return VCM_NOT_IMPLEMENTED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int VideoSender::StartDebugRecording(const char* file_name_utf8) {
|
2014-01-08 12:38:22 +00:00
|
|
|
return recorder_->Start(file_name_utf8);
|
2013-09-14 00:25:28 +00:00
|
|
|
}
|
|
|
|
|
|
2014-01-08 12:38:22 +00:00
|
|
|
void VideoSender::StopDebugRecording() {
|
|
|
|
|
recorder_->Stop();
|
2013-09-14 00:25:28 +00:00
|
|
|
}
|
|
|
|
|
|
2013-11-18 12:18:43 +00:00
|
|
|
void VideoSender::SuspendBelowMinBitrate() {
|
2015-02-19 17:43:25 +00:00
|
|
|
DCHECK(main_thread_.CalledOnValidThread());
|
2013-10-28 10:16:14 +00:00
|
|
|
int threshold_bps;
|
2015-02-19 17:43:25 +00:00
|
|
|
if (current_codec_.numberOfSimulcastStreams == 0) {
|
|
|
|
|
threshold_bps = current_codec_.minBitrate * 1000;
|
2013-10-28 10:16:14 +00:00
|
|
|
} else {
|
2015-02-19 17:43:25 +00:00
|
|
|
threshold_bps = current_codec_.simulcastStream[0].minBitrate * 1000;
|
2013-10-28 10:16:14 +00:00
|
|
|
}
|
|
|
|
|
// Set the hysteresis window to be at 10% of the threshold, but at least
|
|
|
|
|
// 10 kbps.
|
|
|
|
|
int window_bps = std::max(threshold_bps / 10, 10000);
|
2013-11-18 12:18:43 +00:00
|
|
|
_mediaOpt.SuspendBelowMinBitrate(threshold_bps, window_bps);
|
2013-09-30 12:16:08 +00:00
|
|
|
}
|
|
|
|
|
|
2013-11-18 12:18:43 +00:00
|
|
|
bool VideoSender::VideoSuspended() const {
|
2013-09-30 12:16:08 +00:00
|
|
|
CriticalSectionScoped cs(_sendCritSect);
|
2013-12-19 10:59:48 +00:00
|
|
|
return _mediaOpt.IsVideoSuspended();
|
2013-09-30 12:16:08 +00:00
|
|
|
}
|
2013-09-14 00:25:28 +00:00
|
|
|
} // namespace vcm
|
|
|
|
|
} // namespace webrtc
|