blob: 6aedf4e1d2a199d198f6c4d83d2692c238109246 [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
11#include "webrtc/common_types.h"
12
henrik.lundin@webrtc.org1a3a6e52013-10-28 10:16:14 +000013#include <algorithm> // std::max
14
tommi@webrtc.orge07710c2015-02-19 17:43:25 +000015#include "webrtc/base/checks.h"
pbos854e84c2015-11-16 16:39:06 -080016#include "webrtc/base/logging.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"
20#include "webrtc/modules/video_coding/video_coding_impl.h"
jackychen61b4d512015-04-21 15:30:11 -070021#include "webrtc/modules/video_coding/utility/include/quality_scaler.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
pbos@webrtc.org32d640e2013-09-17 10:36:30 +0000129 _mediaOpt.SetEncodingData(sendCodec->codecType,
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000130 sendCodec->maxBitrate * 1000,
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000131 sendCodec->startBitrate * 1000,
132 sendCodec->width,
133 sendCodec->height,
Peter Boströmdf664532015-05-12 12:22:14 +0200134 sendCodec->maxFramerate,
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000135 numLayers,
136 maxPayloadSize);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000137 return VCM_OK;
138}
139
tommi@webrtc.orge07710c2015-02-19 17:43:25 +0000140const VideoCodec& VideoSender::GetSendCodec() const {
henrikg91d6ede2015-09-17 00:24:34 -0700141 RTC_DCHECK(main_thread_.CalledOnValidThread());
tommi@webrtc.orge07710c2015-02-19 17:43:25 +0000142 return current_codec_;
143}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000144
tommi@webrtc.orge07710c2015-02-19 17:43:25 +0000145int32_t VideoSender::SendCodecBlocking(VideoCodec* currentSendCodec) const {
Peter Boströmdcb89982015-09-15 14:43:47 +0200146 rtc::CritScope lock(&send_crit_);
mflodmanfcf54bd2015-04-14 21:28:08 +0200147 if (currentSendCodec == nullptr) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000148 return VCM_PARAMETER_ERROR;
149 }
150 return _codecDataBase.SendCodec(currentSendCodec) ? 0 : -1;
151}
152
tommi@webrtc.orge07710c2015-02-19 17:43:25 +0000153VideoCodecType VideoSender::SendCodecBlocking() const {
Peter Boströmdcb89982015-09-15 14:43:47 +0200154 rtc::CritScope lock(&send_crit_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000155 return _codecDataBase.SendCodec();
156}
157
158// Register an external decoder object.
159// This can not be used together with external decoder callbacks.
160int32_t VideoSender::RegisterExternalEncoder(VideoEncoder* externalEncoder,
161 uint8_t payloadType,
162 bool internalSource /*= false*/) {
henrikg91d6ede2015-09-17 00:24:34 -0700163 RTC_DCHECK(main_thread_.CalledOnValidThread());
tommi@webrtc.org558dc402015-03-07 20:55:56 +0000164
Peter Boströmdcb89982015-09-15 14:43:47 +0200165 rtc::CritScope lock(&send_crit_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000166
mflodmanfcf54bd2015-04-14 21:28:08 +0200167 if (externalEncoder == nullptr) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000168 bool wasSendCodec = false;
169 const bool ret =
170 _codecDataBase.DeregisterExternalEncoder(payloadType, &wasSendCodec);
171 if (wasSendCodec) {
172 // Make sure the VCM doesn't use the de-registered codec
mflodmanfcf54bd2015-04-14 21:28:08 +0200173 _encoder = nullptr;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000174 }
175 return ret ? 0 : -1;
176 }
177 _codecDataBase.RegisterExternalEncoder(
178 externalEncoder, payloadType, internalSource);
179 return 0;
180}
181
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000182// Get encode bitrate
183int VideoSender::Bitrate(unsigned int* bitrate) const {
henrikg91d6ede2015-09-17 00:24:34 -0700184 RTC_DCHECK(main_thread_.CalledOnValidThread());
tommi@webrtc.org558dc402015-03-07 20:55:56 +0000185 // Since we're running on the thread that's the only thread known to modify
186 // the value of _encoder, we don't need to grab the lock here.
187
Peter Boström69ccb332015-10-29 16:30:23 +0100188 if (!_encoder)
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000189 return VCM_UNINITIALIZED;
Peter Boström69ccb332015-10-29 16:30:23 +0100190 *bitrate = _encoder->GetEncoderParameters().target_bitrate;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000191 return 0;
192}
193
194// Get encode frame rate
195int VideoSender::FrameRate(unsigned int* framerate) const {
henrikg91d6ede2015-09-17 00:24:34 -0700196 RTC_DCHECK(main_thread_.CalledOnValidThread());
tommi@webrtc.org558dc402015-03-07 20:55:56 +0000197 // Since we're running on the thread that's the only thread known to modify
198 // the value of _encoder, we don't need to grab the lock here.
199
Peter Boström69ccb332015-10-29 16:30:23 +0100200 if (!_encoder)
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000201 return VCM_UNINITIALIZED;
Peter Boström69ccb332015-10-29 16:30:23 +0100202
203 *framerate = _encoder->GetEncoderParameters().input_frame_rate;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000204 return 0;
205}
206
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000207int32_t VideoSender::SetChannelParameters(uint32_t target_bitrate,
208 uint8_t lossRate,
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000209 int64_t rtt) {
Erik Språng66a641a2015-06-11 14:20:07 +0200210 uint32_t target_rate =
211 _mediaOpt.SetTargetRates(target_bitrate, lossRate, rtt,
212 protection_callback_, qm_settings_callback_);
tommi@webrtc.org558dc402015-03-07 20:55:56 +0000213
tommi@webrtc.org558dc402015-03-07 20:55:56 +0000214 uint32_t input_frame_rate = _mediaOpt.InputFrameRate();
215
Erik Språng66a641a2015-06-11 14:20:07 +0200216 rtc::CritScope cs(&params_lock_);
Peter Boström69ccb332015-10-29 16:30:23 +0100217 encoder_params_ = {target_rate, lossRate, rtt, input_frame_rate};
Erik Språng66a641a2015-06-11 14:20:07 +0200218
219 return VCM_OK;
220}
221
Peter Boströmdcb89982015-09-15 14:43:47 +0200222void VideoSender::SetEncoderParameters(EncoderParameters params) {
Peter Boström69ccb332015-10-29 16:30:23 +0100223 if (params.target_bitrate == 0)
Peter Boströmdcb89982015-09-15 14:43:47 +0200224 return;
tommi@webrtc.org558dc402015-03-07 20:55:56 +0000225
Erik Språng66a641a2015-06-11 14:20:07 +0200226 if (params.input_frame_rate == 0) {
227 // No frame rate estimate available, use default.
228 params.input_frame_rate = current_codec_.maxFramerate;
229 }
Peter Boström69ccb332015-10-29 16:30:23 +0100230 if (_encoder != nullptr)
231 _encoder->SetEncoderParameters(params);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000232}
233
234int32_t VideoSender::RegisterTransportCallback(
235 VCMPacketizationCallback* transport) {
Peter Boströmdcb89982015-09-15 14:43:47 +0200236 rtc::CritScope lock(&send_crit_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000237 _encodedFrameCallback.SetMediaOpt(&_mediaOpt);
238 _encodedFrameCallback.SetTransportCallback(transport);
239 return VCM_OK;
240}
241
242// Register video output information callback which will be called to deliver
243// information about the video stream produced by the encoder, for instance the
244// average frame rate and bit rate.
245int32_t VideoSender::RegisterSendStatisticsCallback(
246 VCMSendStatisticsCallback* sendStats) {
stefan@webrtc.org8db81c52013-09-18 11:57:34 +0000247 CriticalSectionScoped cs(process_crit_sect_.get());
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000248 _sendStatsCallback = sendStats;
249 return VCM_OK;
250}
251
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000252// Register a video protection callback which will be called to deliver the
253// requested FEC rate and NACK status (on/off).
mflodmanfcf54bd2015-04-14 21:28:08 +0200254// Note: this callback is assumed to only be registered once and before it is
255// used in this class.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000256int32_t VideoSender::RegisterProtectionCallback(
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000257 VCMProtectionCallback* protection_callback) {
henrikg91d6ede2015-09-17 00:24:34 -0700258 RTC_DCHECK(protection_callback == nullptr || protection_callback_ == nullptr);
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000259 protection_callback_ = protection_callback;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000260 return VCM_OK;
261}
262
263// Enable or disable a video protection method.
pbosba8c15b2015-07-14 09:36:34 -0700264void VideoSender::SetVideoProtection(VCMVideoProtection videoProtection) {
Peter Boströmdcb89982015-09-15 14:43:47 +0200265 rtc::CritScope lock(&send_crit_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000266 switch (videoProtection) {
pbos@webrtc.orgcade82c2015-03-12 10:39:24 +0000267 case kProtectionNone:
pbosba8c15b2015-07-14 09:36:34 -0700268 _mediaOpt.SetProtectionMethod(media_optimization::kNone);
pbos@webrtc.orgcade82c2015-03-12 10:39:24 +0000269 break;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000270 case kProtectionNack:
pbosba8c15b2015-07-14 09:36:34 -0700271 _mediaOpt.SetProtectionMethod(media_optimization::kNack);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000272 break;
pbos@webrtc.orgcade82c2015-03-12 10:39:24 +0000273 case kProtectionNackFEC:
pbosba8c15b2015-07-14 09:36:34 -0700274 _mediaOpt.SetProtectionMethod(media_optimization::kNackFec);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000275 break;
pbos@webrtc.orgcade82c2015-03-12 10:39:24 +0000276 case kProtectionFEC:
pbosba8c15b2015-07-14 09:36:34 -0700277 _mediaOpt.SetProtectionMethod(media_optimization::kFec);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000278 break;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000279 }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000280}
281// Add one raw video frame to the encoder, blocking.
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700282int32_t VideoSender::AddVideoFrame(const VideoFrame& videoFrame,
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000283 const VideoContentMetrics* contentMetrics,
284 const CodecSpecificInfo* codecSpecificInfo) {
Peter Boströmdcb89982015-09-15 14:43:47 +0200285 EncoderParameters encoder_params;
286 {
287 rtc::CritScope lock(&params_lock_);
288 encoder_params = encoder_params_;
Peter Boströmdcb89982015-09-15 14:43:47 +0200289 }
290 rtc::CritScope lock(&send_crit_);
Peter Boström69ccb332015-10-29 16:30:23 +0100291 if (_encoder == nullptr)
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000292 return VCM_UNINITIALIZED;
Peter Boström69ccb332015-10-29 16:30:23 +0100293 SetEncoderParameters(encoder_params);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000294 // TODO(holmer): Add support for dropping frames per stream. Currently we
295 // only have one frame dropper for all streams.
pbos22993e12015-10-19 02:39:06 -0700296 if (_nextFrameTypes[0] == kEmptyFrame) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000297 return VCM_OK;
298 }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000299 if (_mediaOpt.DropFrame()) {
jackychen61b4d512015-04-21 15:30:11 -0700300 _encoder->OnDroppedFrame();
stefan@webrtc.org34c5da62014-04-11 14:08:35 +0000301 return VCM_OK;
302 }
303 _mediaOpt.UpdateContentData(contentMetrics);
pbos@webrtc.org67a9e402015-03-05 13:57:37 +0000304 // TODO(pbos): Make sure setting send codec is synchronized with video
305 // processing so frame size always matches.
306 if (!_codecDataBase.MatchesCurrentResolution(videoFrame.width(),
307 videoFrame.height())) {
308 LOG(LS_ERROR) << "Incoming frame doesn't match set resolution. Dropping.";
309 return VCM_PARAMETER_ERROR;
310 }
Peter Boströmeb66e802015-06-05 11:08:03 +0200311 VideoFrame converted_frame = videoFrame;
312 if (converted_frame.native_handle() && !_encoder->SupportsNativeHandle()) {
313 // This module only supports software encoding.
314 // TODO(pbos): Offload conversion from the encoder thread.
315 converted_frame = converted_frame.ConvertNativeToI420Frame();
henrikg91d6ede2015-09-17 00:24:34 -0700316 RTC_CHECK(!converted_frame.IsZeroSize())
Peter Boströmeb66e802015-06-05 11:08:03 +0200317 << "Frame conversion failed, won't be able to encode frame.";
318 }
stefan@webrtc.org34c5da62014-04-11 14:08:35 +0000319 int32_t ret =
Peter Boströmeb66e802015-06-05 11:08:03 +0200320 _encoder->Encode(converted_frame, codecSpecificInfo, _nextFrameTypes);
stefan@webrtc.org34c5da62014-04-11 14:08:35 +0000321 if (ret < 0) {
322 LOG(LS_ERROR) << "Failed to encode frame. Error code: " << ret;
323 return ret;
324 }
325 for (size_t i = 0; i < _nextFrameTypes.size(); ++i) {
326 _nextFrameTypes[i] = kVideoFrameDelta; // Default frame type.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000327 }
jackychen6e2ce6e2015-07-13 16:26:33 -0700328 if (qm_settings_callback_)
329 qm_settings_callback_->SetTargetFramerate(_encoder->GetTargetFramerate());
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000330 return VCM_OK;
331}
332
333int32_t VideoSender::IntraFrameRequest(int stream_index) {
Peter Boströmdcb89982015-09-15 14:43:47 +0200334 rtc::CritScope lock(&send_crit_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000335 if (stream_index < 0 ||
336 static_cast<unsigned int>(stream_index) >= _nextFrameTypes.size()) {
337 return -1;
338 }
339 _nextFrameTypes[stream_index] = kVideoFrameKey;
mflodmanfcf54bd2015-04-14 21:28:08 +0200340 if (_encoder != nullptr && _encoder->InternalSource()) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000341 // Try to request the frame if we have an external encoder with
342 // internal source since AddVideoFrame never will be called.
343 if (_encoder->RequestFrame(_nextFrameTypes) == WEBRTC_VIDEO_CODEC_OK) {
344 _nextFrameTypes[stream_index] = kVideoFrameDelta;
345 }
346 }
347 return VCM_OK;
348}
349
350int32_t VideoSender::EnableFrameDropper(bool enable) {
Peter Boströmdcb89982015-09-15 14:43:47 +0200351 rtc::CritScope lock(&send_crit_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000352 frame_dropper_enabled_ = enable;
353 _mediaOpt.EnableFrameDropper(enable);
354 return VCM_OK;
355}
356
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000357void VideoSender::SuspendBelowMinBitrate() {
henrikg91d6ede2015-09-17 00:24:34 -0700358 RTC_DCHECK(main_thread_.CalledOnValidThread());
henrik.lundin@webrtc.org1a3a6e52013-10-28 10:16:14 +0000359 int threshold_bps;
tommi@webrtc.orge07710c2015-02-19 17:43:25 +0000360 if (current_codec_.numberOfSimulcastStreams == 0) {
361 threshold_bps = current_codec_.minBitrate * 1000;
henrik.lundin@webrtc.org1a3a6e52013-10-28 10:16:14 +0000362 } else {
tommi@webrtc.orge07710c2015-02-19 17:43:25 +0000363 threshold_bps = current_codec_.simulcastStream[0].minBitrate * 1000;
henrik.lundin@webrtc.org1a3a6e52013-10-28 10:16:14 +0000364 }
365 // Set the hysteresis window to be at 10% of the threshold, but at least
366 // 10 kbps.
367 int window_bps = std::max(threshold_bps / 10, 10000);
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000368 _mediaOpt.SuspendBelowMinBitrate(threshold_bps, window_bps);
henrik.lundin@webrtc.org572699d2013-09-30 12:16:08 +0000369}
370
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000371bool VideoSender::VideoSuspended() const {
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000372 return _mediaOpt.IsVideoSuspended();
henrik.lundin@webrtc.org572699d2013-09-30 12:16:08 +0000373}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000374} // namespace vcm
375} // namespace webrtc