blob: 3bc33812a23a17ea352e2c1f33080408cf3386bf [file] [log] [blame]
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +00001/*
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
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
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000011
henrik.lundin@webrtc.org1a3a6e52013-10-28 10:16:14 +000012#include <algorithm> // std::max
13
tommi@webrtc.orge07710c2015-02-19 17:43:25 +000014#include "webrtc/base/checks.h"
pbos854e84c2015-11-16 16:39:06 -080015#include "webrtc/base/logging.h"
philipel5908c712015-12-21 08:23:20 -080016#include "webrtc/common_types.h"
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000017#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010018#include "webrtc/modules/video_coding/include/video_codec_interface.h"
19#include "webrtc/modules/video_coding/encoded_frame.h"
kjellander@webrtc.orgb7ce9642015-11-18 23:04:10 +010020#include "webrtc/modules/video_coding/utility/quality_scaler.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010021#include "webrtc/modules/video_coding/video_coding_impl.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010022#include "webrtc/system_wrappers/include/clock.h"
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000023
24namespace webrtc {
25namespace vcm {
26
stefan@webrtc.org34c5da62014-04-11 14:08:35 +000027VideoSender::VideoSender(Clock* clock,
pbos@webrtc.org891d4832015-02-26 13:15:22 +000028 EncodedImageCallback* post_encode_callback,
mflodmanfcf54bd2015-04-14 21:28:08 +020029 VideoEncoderRateObserver* encoder_rate_observer,
30 VCMQMSettingsCallback* qm_settings_callback)
stefan@webrtc.org34c5da62014-04-11 14:08:35 +000031 : clock_(clock),
stefan@webrtc.org8db81c52013-09-18 11:57:34 +000032 process_crit_sect_(CriticalSectionWrapper::CreateCriticalSection()),
Peter Boström9dbbcfb2015-04-21 15:55:11 +020033 _encoder(nullptr),
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +000034 _encodedFrameCallback(post_encode_callback),
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000035 _nextFrameTypes(1, kVideoFrameDelta),
stefan@webrtc.org34c5da62014-04-11 14:08:35 +000036 _mediaOpt(clock_),
mflodmanfcf54bd2015-04-14 21:28:08 +020037 _sendStatsCallback(nullptr),
Peter Boström4f5db112015-10-29 16:53:59 +010038 _codecDataBase(encoder_rate_observer, &_encodedFrameCallback),
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000039 frame_dropper_enabled_(true),
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000040 _sendStatsTimer(1000, clock_),
tommi@webrtc.orge07710c2015-02-19 17:43:25 +000041 current_codec_(),
mflodmanfcf54bd2015-04-14 21:28:08 +020042 qm_settings_callback_(qm_settings_callback),
Peter Boström69ccb332015-10-29 16:30:23 +010043 protection_callback_(nullptr),
44 encoder_params_({0, 0, 0, 0}) {
tommi@webrtc.org658d2012015-03-05 12:21:54 +000045 // Allow VideoSender to be created on one thread but used on another, post
46 // construction. This is currently how this class is being used by at least
47 // one external project (diffractor).
mflodmanfcf54bd2015-04-14 21:28:08 +020048 _mediaOpt.EnableQM(qm_settings_callback_ != nullptr);
Peter Boström9dbbcfb2015-04-21 15:55:11 +020049 _mediaOpt.Reset();
tommi@webrtc.org658d2012015-03-05 12:21:54 +000050 main_thread_.DetachFromThread();
tommi@webrtc.orge07710c2015-02-19 17:43:25 +000051}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000052
Peter Boströmdcb89982015-09-15 14:43:47 +020053VideoSender::~VideoSender() {}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000054
55int32_t VideoSender::Process() {
56 int32_t returnValue = VCM_OK;
57
58 if (_sendStatsTimer.TimeUntilProcess() == 0) {
59 _sendStatsTimer.Processed();
stefan@webrtc.org8db81c52013-09-18 11:57:34 +000060 CriticalSectionScoped cs(process_crit_sect_.get());
mflodmanfcf54bd2015-04-14 21:28:08 +020061 if (_sendStatsCallback != nullptr) {
tommi@webrtc.orge07710c2015-02-19 17:43:25 +000062 uint32_t bitRate = _mediaOpt.SentBitRate();
63 uint32_t frameRate = _mediaOpt.SentFrameRate();
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000064 _sendStatsCallback->SendStatistics(bitRate, frameRate);
65 }
66 }
67
Erik Språng66a641a2015-06-11 14:20:07 +020068 {
69 rtc::CritScope cs(&params_lock_);
70 // Force an encoder parameters update, so that incoming frame rate is
71 // updated even if bandwidth hasn't changed.
72 encoder_params_.input_frame_rate = _mediaOpt.InputFrameRate();
Erik Språng66a641a2015-06-11 14:20:07 +020073 }
74
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000075 return returnValue;
76}
77
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000078int64_t VideoSender::TimeUntilNextProcess() {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000079 return _sendStatsTimer.TimeUntilProcess();
80}
81
82// Register the send codec to be used.
83int32_t VideoSender::RegisterSendCodec(const VideoCodec* sendCodec,
84 uint32_t numberOfCores,
85 uint32_t maxPayloadSize) {
henrikg91d6ede2015-09-17 00:24:34 -070086 RTC_DCHECK(main_thread_.CalledOnValidThread());
Peter Boströmdcb89982015-09-15 14:43:47 +020087 rtc::CritScope lock(&send_crit_);
mflodmanfcf54bd2015-04-14 21:28:08 +020088 if (sendCodec == nullptr) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000089 return VCM_PARAMETER_ERROR;
90 }
91
Peter Boström4f5db112015-10-29 16:53:59 +010092 bool ret =
93 _codecDataBase.SetSendCodec(sendCodec, numberOfCores, maxPayloadSize);
pbos@webrtc.orgda2c4ce2013-09-17 09:38:41 +000094
95 // Update encoder regardless of result to make sure that we're not holding on
96 // to a deleted instance.
97 _encoder = _codecDataBase.GetEncoder();
tommi@webrtc.orge07710c2015-02-19 17:43:25 +000098 // Cache the current codec here so they can be fetched from this thread
99 // without requiring the _sendCritSect lock.
100 current_codec_ = *sendCodec;
pbos@webrtc.orgda2c4ce2013-09-17 09:38:41 +0000101
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000102 if (!ret) {
pbos@webrtc.org0b52ceb2015-03-24 11:20:54 +0000103 LOG(LS_ERROR) << "Failed to initialize set encoder with payload name '"
104 << sendCodec->plName << "'.";
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000105 return VCM_CODEC_ERROR;
106 }
107
ivicac7199c22015-10-07 06:43:33 -0700108 int numLayers;
109 if (sendCodec->codecType == kVideoCodecVP8) {
110 numLayers = sendCodec->codecSpecific.VP8.numberOfTemporalLayers;
111 } else if (sendCodec->codecType == kVideoCodecVP9) {
112 numLayers = sendCodec->codecSpecific.VP9.numberOfTemporalLayers;
113 } else {
114 numLayers = 1;
115 }
116
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000117 // If we have screensharing and we have layers, we disable frame dropper.
118 bool disable_frame_dropper =
119 numLayers > 1 && sendCodec->mode == kScreensharing;
120 if (disable_frame_dropper) {
121 _mediaOpt.EnableFrameDropper(false);
122 } else if (frame_dropper_enabled_) {
123 _mediaOpt.EnableFrameDropper(true);
124 }
125 _nextFrameTypes.clear();
126 _nextFrameTypes.resize(VCM_MAX(sendCodec->numberOfSimulcastStreams, 1),
127 kVideoFrameDelta);
128
philipel5908c712015-12-21 08:23:20 -0800129 _mediaOpt.SetEncodingData(sendCodec->codecType, sendCodec->maxBitrate * 1000,
130 sendCodec->startBitrate * 1000, sendCodec->width,
131 sendCodec->height, sendCodec->maxFramerate,
132 numLayers, maxPayloadSize);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000133 return VCM_OK;
134}
135
tommi@webrtc.orge07710c2015-02-19 17:43:25 +0000136const VideoCodec& VideoSender::GetSendCodec() const {
henrikg91d6ede2015-09-17 00:24:34 -0700137 RTC_DCHECK(main_thread_.CalledOnValidThread());
tommi@webrtc.orge07710c2015-02-19 17:43:25 +0000138 return current_codec_;
139}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000140
tommi@webrtc.orge07710c2015-02-19 17:43:25 +0000141int32_t VideoSender::SendCodecBlocking(VideoCodec* currentSendCodec) const {
Peter Boströmdcb89982015-09-15 14:43:47 +0200142 rtc::CritScope lock(&send_crit_);
mflodmanfcf54bd2015-04-14 21:28:08 +0200143 if (currentSendCodec == nullptr) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000144 return VCM_PARAMETER_ERROR;
145 }
146 return _codecDataBase.SendCodec(currentSendCodec) ? 0 : -1;
147}
148
tommi@webrtc.orge07710c2015-02-19 17:43:25 +0000149VideoCodecType VideoSender::SendCodecBlocking() const {
Peter Boströmdcb89982015-09-15 14:43:47 +0200150 rtc::CritScope lock(&send_crit_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000151 return _codecDataBase.SendCodec();
152}
153
154// Register an external decoder object.
155// This can not be used together with external decoder callbacks.
Peter Boström795dbe42015-11-27 14:09:07 +0100156void VideoSender::RegisterExternalEncoder(VideoEncoder* externalEncoder,
philipel5908c712015-12-21 08:23:20 -0800157 uint8_t payloadType,
158 bool internalSource /*= false*/) {
henrikg91d6ede2015-09-17 00:24:34 -0700159 RTC_DCHECK(main_thread_.CalledOnValidThread());
tommi@webrtc.org558dc402015-03-07 20:55:56 +0000160
Peter Boströmdcb89982015-09-15 14:43:47 +0200161 rtc::CritScope lock(&send_crit_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000162
mflodmanfcf54bd2015-04-14 21:28:08 +0200163 if (externalEncoder == nullptr) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000164 bool wasSendCodec = false;
Peter Boström795dbe42015-11-27 14:09:07 +0100165 RTC_CHECK(
166 _codecDataBase.DeregisterExternalEncoder(payloadType, &wasSendCodec));
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000167 if (wasSendCodec) {
168 // Make sure the VCM doesn't use the de-registered codec
mflodmanfcf54bd2015-04-14 21:28:08 +0200169 _encoder = nullptr;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000170 }
Peter Boström795dbe42015-11-27 14:09:07 +0100171 return;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000172 }
philipel5908c712015-12-21 08:23:20 -0800173 _codecDataBase.RegisterExternalEncoder(externalEncoder, payloadType,
174 internalSource);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000175}
176
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000177// Get encode bitrate
178int VideoSender::Bitrate(unsigned int* bitrate) const {
henrikg91d6ede2015-09-17 00:24:34 -0700179 RTC_DCHECK(main_thread_.CalledOnValidThread());
tommi@webrtc.org558dc402015-03-07 20:55:56 +0000180 // Since we're running on the thread that's the only thread known to modify
181 // the value of _encoder, we don't need to grab the lock here.
182
Peter Boström69ccb332015-10-29 16:30:23 +0100183 if (!_encoder)
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000184 return VCM_UNINITIALIZED;
Peter Boström69ccb332015-10-29 16:30:23 +0100185 *bitrate = _encoder->GetEncoderParameters().target_bitrate;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000186 return 0;
187}
188
189// Get encode frame rate
190int VideoSender::FrameRate(unsigned int* framerate) const {
henrikg91d6ede2015-09-17 00:24:34 -0700191 RTC_DCHECK(main_thread_.CalledOnValidThread());
tommi@webrtc.org558dc402015-03-07 20:55:56 +0000192 // Since we're running on the thread that's the only thread known to modify
193 // the value of _encoder, we don't need to grab the lock here.
194
Peter Boström69ccb332015-10-29 16:30:23 +0100195 if (!_encoder)
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000196 return VCM_UNINITIALIZED;
Peter Boström69ccb332015-10-29 16:30:23 +0100197
198 *framerate = _encoder->GetEncoderParameters().input_frame_rate;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000199 return 0;
200}
201
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000202int32_t VideoSender::SetChannelParameters(uint32_t target_bitrate,
203 uint8_t lossRate,
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000204 int64_t rtt) {
Erik Språng66a641a2015-06-11 14:20:07 +0200205 uint32_t target_rate =
206 _mediaOpt.SetTargetRates(target_bitrate, lossRate, rtt,
207 protection_callback_, qm_settings_callback_);
tommi@webrtc.org558dc402015-03-07 20:55:56 +0000208
tommi@webrtc.org558dc402015-03-07 20:55:56 +0000209 uint32_t input_frame_rate = _mediaOpt.InputFrameRate();
210
Erik Språng66a641a2015-06-11 14:20:07 +0200211 rtc::CritScope cs(&params_lock_);
Peter Boström69ccb332015-10-29 16:30:23 +0100212 encoder_params_ = {target_rate, lossRate, rtt, input_frame_rate};
Erik Språng66a641a2015-06-11 14:20:07 +0200213
214 return VCM_OK;
215}
216
Peter Boströmdcb89982015-09-15 14:43:47 +0200217void VideoSender::SetEncoderParameters(EncoderParameters params) {
Peter Boström69ccb332015-10-29 16:30:23 +0100218 if (params.target_bitrate == 0)
Peter Boströmdcb89982015-09-15 14:43:47 +0200219 return;
tommi@webrtc.org558dc402015-03-07 20:55:56 +0000220
Erik Språng66a641a2015-06-11 14:20:07 +0200221 if (params.input_frame_rate == 0) {
222 // No frame rate estimate available, use default.
223 params.input_frame_rate = current_codec_.maxFramerate;
224 }
Peter Boström69ccb332015-10-29 16:30:23 +0100225 if (_encoder != nullptr)
226 _encoder->SetEncoderParameters(params);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000227}
228
229int32_t VideoSender::RegisterTransportCallback(
230 VCMPacketizationCallback* transport) {
Peter Boströmdcb89982015-09-15 14:43:47 +0200231 rtc::CritScope lock(&send_crit_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000232 _encodedFrameCallback.SetMediaOpt(&_mediaOpt);
233 _encodedFrameCallback.SetTransportCallback(transport);
234 return VCM_OK;
235}
236
237// Register video output information callback which will be called to deliver
238// information about the video stream produced by the encoder, for instance the
239// average frame rate and bit rate.
240int32_t VideoSender::RegisterSendStatisticsCallback(
241 VCMSendStatisticsCallback* sendStats) {
stefan@webrtc.org8db81c52013-09-18 11:57:34 +0000242 CriticalSectionScoped cs(process_crit_sect_.get());
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000243 _sendStatsCallback = sendStats;
244 return VCM_OK;
245}
246
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000247// Register a video protection callback which will be called to deliver the
248// requested FEC rate and NACK status (on/off).
mflodmanfcf54bd2015-04-14 21:28:08 +0200249// Note: this callback is assumed to only be registered once and before it is
250// used in this class.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000251int32_t VideoSender::RegisterProtectionCallback(
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000252 VCMProtectionCallback* protection_callback) {
henrikg91d6ede2015-09-17 00:24:34 -0700253 RTC_DCHECK(protection_callback == nullptr || protection_callback_ == nullptr);
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000254 protection_callback_ = protection_callback;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000255 return VCM_OK;
256}
257
258// Enable or disable a video protection method.
pbosba8c15b2015-07-14 09:36:34 -0700259void VideoSender::SetVideoProtection(VCMVideoProtection videoProtection) {
Peter Boströmdcb89982015-09-15 14:43:47 +0200260 rtc::CritScope lock(&send_crit_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000261 switch (videoProtection) {
pbos@webrtc.orgcade82c2015-03-12 10:39:24 +0000262 case kProtectionNone:
pbosba8c15b2015-07-14 09:36:34 -0700263 _mediaOpt.SetProtectionMethod(media_optimization::kNone);
pbos@webrtc.orgcade82c2015-03-12 10:39:24 +0000264 break;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000265 case kProtectionNack:
pbosba8c15b2015-07-14 09:36:34 -0700266 _mediaOpt.SetProtectionMethod(media_optimization::kNack);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000267 break;
pbos@webrtc.orgcade82c2015-03-12 10:39:24 +0000268 case kProtectionNackFEC:
pbosba8c15b2015-07-14 09:36:34 -0700269 _mediaOpt.SetProtectionMethod(media_optimization::kNackFec);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000270 break;
pbos@webrtc.orgcade82c2015-03-12 10:39:24 +0000271 case kProtectionFEC:
pbosba8c15b2015-07-14 09:36:34 -0700272 _mediaOpt.SetProtectionMethod(media_optimization::kFec);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000273 break;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000274 }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000275}
276// Add one raw video frame to the encoder, blocking.
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700277int32_t VideoSender::AddVideoFrame(const VideoFrame& videoFrame,
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000278 const VideoContentMetrics* contentMetrics,
279 const CodecSpecificInfo* codecSpecificInfo) {
Peter Boströmdcb89982015-09-15 14:43:47 +0200280 EncoderParameters encoder_params;
281 {
282 rtc::CritScope lock(&params_lock_);
283 encoder_params = encoder_params_;
Peter Boströmdcb89982015-09-15 14:43:47 +0200284 }
285 rtc::CritScope lock(&send_crit_);
Peter Boström69ccb332015-10-29 16:30:23 +0100286 if (_encoder == nullptr)
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000287 return VCM_UNINITIALIZED;
Peter Boström69ccb332015-10-29 16:30:23 +0100288 SetEncoderParameters(encoder_params);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000289 // TODO(holmer): Add support for dropping frames per stream. Currently we
290 // only have one frame dropper for all streams.
pbos22993e12015-10-19 02:39:06 -0700291 if (_nextFrameTypes[0] == kEmptyFrame) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000292 return VCM_OK;
293 }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000294 if (_mediaOpt.DropFrame()) {
jackychen61b4d512015-04-21 15:30:11 -0700295 _encoder->OnDroppedFrame();
stefan@webrtc.org34c5da62014-04-11 14:08:35 +0000296 return VCM_OK;
297 }
298 _mediaOpt.UpdateContentData(contentMetrics);
pbos@webrtc.org67a9e402015-03-05 13:57:37 +0000299 // TODO(pbos): Make sure setting send codec is synchronized with video
300 // processing so frame size always matches.
301 if (!_codecDataBase.MatchesCurrentResolution(videoFrame.width(),
302 videoFrame.height())) {
303 LOG(LS_ERROR) << "Incoming frame doesn't match set resolution. Dropping.";
304 return VCM_PARAMETER_ERROR;
305 }
Peter Boströmeb66e802015-06-05 11:08:03 +0200306 VideoFrame converted_frame = videoFrame;
307 if (converted_frame.native_handle() && !_encoder->SupportsNativeHandle()) {
308 // This module only supports software encoding.
309 // TODO(pbos): Offload conversion from the encoder thread.
310 converted_frame = converted_frame.ConvertNativeToI420Frame();
henrikg91d6ede2015-09-17 00:24:34 -0700311 RTC_CHECK(!converted_frame.IsZeroSize())
Peter Boströmeb66e802015-06-05 11:08:03 +0200312 << "Frame conversion failed, won't be able to encode frame.";
313 }
stefan@webrtc.org34c5da62014-04-11 14:08:35 +0000314 int32_t ret =
Peter Boströmeb66e802015-06-05 11:08:03 +0200315 _encoder->Encode(converted_frame, codecSpecificInfo, _nextFrameTypes);
stefan@webrtc.org34c5da62014-04-11 14:08:35 +0000316 if (ret < 0) {
317 LOG(LS_ERROR) << "Failed to encode frame. Error code: " << ret;
318 return ret;
319 }
320 for (size_t i = 0; i < _nextFrameTypes.size(); ++i) {
321 _nextFrameTypes[i] = kVideoFrameDelta; // Default frame type.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000322 }
jackychen6e2ce6e2015-07-13 16:26:33 -0700323 if (qm_settings_callback_)
324 qm_settings_callback_->SetTargetFramerate(_encoder->GetTargetFramerate());
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000325 return VCM_OK;
326}
327
328int32_t VideoSender::IntraFrameRequest(int stream_index) {
Peter Boströmdcb89982015-09-15 14:43:47 +0200329 rtc::CritScope lock(&send_crit_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000330 if (stream_index < 0 ||
331 static_cast<unsigned int>(stream_index) >= _nextFrameTypes.size()) {
332 return -1;
333 }
334 _nextFrameTypes[stream_index] = kVideoFrameKey;
mflodmanfcf54bd2015-04-14 21:28:08 +0200335 if (_encoder != nullptr && _encoder->InternalSource()) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000336 // Try to request the frame if we have an external encoder with
337 // internal source since AddVideoFrame never will be called.
338 if (_encoder->RequestFrame(_nextFrameTypes) == WEBRTC_VIDEO_CODEC_OK) {
339 _nextFrameTypes[stream_index] = kVideoFrameDelta;
340 }
341 }
342 return VCM_OK;
343}
344
345int32_t VideoSender::EnableFrameDropper(bool enable) {
Peter Boströmdcb89982015-09-15 14:43:47 +0200346 rtc::CritScope lock(&send_crit_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000347 frame_dropper_enabled_ = enable;
348 _mediaOpt.EnableFrameDropper(enable);
349 return VCM_OK;
350}
351
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000352void VideoSender::SuspendBelowMinBitrate() {
henrikg91d6ede2015-09-17 00:24:34 -0700353 RTC_DCHECK(main_thread_.CalledOnValidThread());
henrik.lundin@webrtc.org1a3a6e52013-10-28 10:16:14 +0000354 int threshold_bps;
tommi@webrtc.orge07710c2015-02-19 17:43:25 +0000355 if (current_codec_.numberOfSimulcastStreams == 0) {
356 threshold_bps = current_codec_.minBitrate * 1000;
henrik.lundin@webrtc.org1a3a6e52013-10-28 10:16:14 +0000357 } else {
tommi@webrtc.orge07710c2015-02-19 17:43:25 +0000358 threshold_bps = current_codec_.simulcastStream[0].minBitrate * 1000;
henrik.lundin@webrtc.org1a3a6e52013-10-28 10:16:14 +0000359 }
360 // Set the hysteresis window to be at 10% of the threshold, but at least
361 // 10 kbps.
362 int window_bps = std::max(threshold_bps / 10, 10000);
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000363 _mediaOpt.SuspendBelowMinBitrate(threshold_bps, window_bps);
henrik.lundin@webrtc.org572699d2013-09-30 12:16:08 +0000364}
365
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000366bool VideoSender::VideoSuspended() const {
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000367 return _mediaOpt.IsVideoSuspended();
henrik.lundin@webrtc.org572699d2013-09-30 12:16:08 +0000368}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000369} // namespace vcm
370} // namespace webrtc