blob: 35fd39cd054980ef8191d2688d4423dd0dad4bb5 [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"
sprang647bf432016-11-10 06:46:20 -080017#include "webrtc/common_video/include/video_bitrate_allocator.h"
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000018#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
sprang647bf432016-11-10 06:46:20 -080019#include "webrtc/modules/video_coding/codecs/vp8/temporal_layers.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010020#include "webrtc/modules/video_coding/include/video_codec_interface.h"
21#include "webrtc/modules/video_coding/encoded_frame.h"
sprang647bf432016-11-10 06:46:20 -080022#include "webrtc/modules/video_coding/utility/default_video_bitrate_allocator.h"
kjellander@webrtc.orgb7ce9642015-11-18 23:04:10 +010023#include "webrtc/modules/video_coding/utility/quality_scaler.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010024#include "webrtc/modules/video_coding/video_coding_impl.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010025#include "webrtc/system_wrappers/include/clock.h"
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000026
27namespace webrtc {
28namespace vcm {
29
stefan@webrtc.org34c5da62014-04-11 14:08:35 +000030VideoSender::VideoSender(Clock* clock,
pbos@webrtc.org891d4832015-02-26 13:15:22 +000031 EncodedImageCallback* post_encode_callback,
perkj376b1922016-05-02 11:35:24 -070032 VCMSendStatisticsCallback* send_stats_callback)
stefan@webrtc.org34c5da62014-04-11 14:08:35 +000033 : clock_(clock),
Peter Boström9dbbcfb2015-04-21 15:55:11 +020034 _encoder(nullptr),
stefan@webrtc.org34c5da62014-04-11 14:08:35 +000035 _mediaOpt(clock_),
perkj376b1922016-05-02 11:35:24 -070036 _encodedFrameCallback(post_encode_callback, &_mediaOpt),
37 send_stats_callback_(send_stats_callback),
perkjf5b2e512016-07-05 08:34:04 -070038 _codecDataBase(&_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_(),
sprang647bf432016-11-10 06:46:20 -080042 encoder_params_({BitrateAllocation(), 0, 0, 0}),
Peter Boström233bfd22016-01-18 20:23:40 +010043 encoder_has_internal_source_(false),
44 next_frame_types_(1, kVideoFrameDelta) {
Peter Boströmad6fc5a2016-05-12 03:01:31 +020045 _mediaOpt.Reset();
tommi@webrtc.org658d2012015-03-05 12:21:54 +000046 // Allow VideoSender to be created on one thread but used on another, post
47 // construction. This is currently how this class is being used by at least
48 // one external project (diffractor).
perkj4e417b22016-07-14 23:35:55 -070049 sequenced_checker_.Detach();
tommi@webrtc.orge07710c2015-02-19 17:43:25 +000050}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000051
Peter Boströmdcb89982015-09-15 14:43:47 +020052VideoSender::~VideoSender() {}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000053
pbosa26ac922016-02-25 04:50:01 -080054void VideoSender::Process() {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000055 if (_sendStatsTimer.TimeUntilProcess() == 0) {
perkj376b1922016-05-02 11:35:24 -070056 // |_sendStatsTimer.Processed()| must be called. Otherwise
57 // VideoSender::Process() will be called in an infinite loop.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000058 _sendStatsTimer.Processed();
perkj376b1922016-05-02 11:35:24 -070059 if (send_stats_callback_) {
tommi@webrtc.orge07710c2015-02-19 17:43:25 +000060 uint32_t bitRate = _mediaOpt.SentBitRate();
61 uint32_t frameRate = _mediaOpt.SentFrameRate();
perkj275afc52016-09-01 00:21:16 -070062 send_stats_callback_->SendStatistics(bitRate, frameRate);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000063 }
64 }
65
Erik Språng66a641a2015-06-11 14:20:07 +020066 {
Peter Boström233bfd22016-01-18 20:23:40 +010067 rtc::CritScope cs(&params_crit_);
Erik Språng66a641a2015-06-11 14:20:07 +020068 // Force an encoder parameters update, so that incoming frame rate is
69 // updated even if bandwidth hasn't changed.
70 encoder_params_.input_frame_rate = _mediaOpt.InputFrameRate();
Erik Språng66a641a2015-06-11 14:20:07 +020071 }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000072}
73
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000074int64_t VideoSender::TimeUntilNextProcess() {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000075 return _sendStatsTimer.TimeUntilProcess();
76}
77
78// Register the send codec to be used.
79int32_t VideoSender::RegisterSendCodec(const VideoCodec* sendCodec,
80 uint32_t numberOfCores,
81 uint32_t maxPayloadSize) {
perkj4e417b22016-07-14 23:35:55 -070082 RTC_DCHECK(sequenced_checker_.CalledSequentially());
Peter Boström233bfd22016-01-18 20:23:40 +010083 rtc::CritScope lock(&encoder_crit_);
mflodmanfcf54bd2015-04-14 21:28:08 +020084 if (sendCodec == nullptr) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000085 return VCM_PARAMETER_ERROR;
86 }
87
Peter Boström4f5db112015-10-29 16:53:59 +010088 bool ret =
89 _codecDataBase.SetSendCodec(sendCodec, numberOfCores, maxPayloadSize);
pbos@webrtc.orgda2c4ce2013-09-17 09:38:41 +000090
91 // Update encoder regardless of result to make sure that we're not holding on
92 // to a deleted instance.
93 _encoder = _codecDataBase.GetEncoder();
tommi@webrtc.orge07710c2015-02-19 17:43:25 +000094 // Cache the current codec here so they can be fetched from this thread
95 // without requiring the _sendCritSect lock.
96 current_codec_ = *sendCodec;
pbos@webrtc.orgda2c4ce2013-09-17 09:38:41 +000097
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000098 if (!ret) {
pbos@webrtc.org0b52ceb2015-03-24 11:20:54 +000099 LOG(LS_ERROR) << "Failed to initialize set encoder with payload name '"
100 << sendCodec->plName << "'.";
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000101 return VCM_CODEC_ERROR;
102 }
103
Peter Boström233bfd22016-01-18 20:23:40 +0100104 // SetSendCodec succeeded, _encoder should be set.
105 RTC_DCHECK(_encoder);
106
ivicac7199c22015-10-07 06:43:33 -0700107 int numLayers;
108 if (sendCodec->codecType == kVideoCodecVP8) {
hta257dc392016-10-25 09:05:06 -0700109 numLayers = sendCodec->VP8().numberOfTemporalLayers;
ivicac7199c22015-10-07 06:43:33 -0700110 } else if (sendCodec->codecType == kVideoCodecVP9) {
hta257dc392016-10-25 09:05:06 -0700111 numLayers = sendCodec->VP9().numberOfTemporalLayers;
ivicac7199c22015-10-07 06:43:33 -0700112 } else {
113 numLayers = 1;
114 }
115
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000116 // If we have screensharing and we have layers, we disable frame dropper.
117 bool disable_frame_dropper =
118 numLayers > 1 && sendCodec->mode == kScreensharing;
119 if (disable_frame_dropper) {
120 _mediaOpt.EnableFrameDropper(false);
121 } else if (frame_dropper_enabled_) {
122 _mediaOpt.EnableFrameDropper(true);
123 }
Peter Boström233bfd22016-01-18 20:23:40 +0100124 {
125 rtc::CritScope cs(&params_crit_);
126 next_frame_types_.clear();
127 next_frame_types_.resize(VCM_MAX(sendCodec->numberOfSimulcastStreams, 1),
128 kVideoFrameKey);
129 // Cache InternalSource() to have this available from IntraFrameRequest()
130 // without having to acquire encoder_crit_ (avoid blocking on encoder use).
131 encoder_has_internal_source_ = _encoder->InternalSource();
132 }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000133
isheriff7620be82016-03-06 23:22:33 -0800134 LOG(LS_VERBOSE) << " max bitrate " << sendCodec->maxBitrate
135 << " start bitrate " << sendCodec->startBitrate
136 << " max frame rate " << sendCodec->maxFramerate
137 << " max payload size " << maxPayloadSize;
Per69b332d2016-06-02 15:45:42 +0200138 _mediaOpt.SetEncodingData(sendCodec->maxBitrate * 1000,
philipel5908c712015-12-21 08:23:20 -0800139 sendCodec->startBitrate * 1000, sendCodec->width,
140 sendCodec->height, sendCodec->maxFramerate,
141 numLayers, maxPayloadSize);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000142 return VCM_OK;
143}
144
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000145// Register an external decoder object.
146// This can not be used together with external decoder callbacks.
Peter Boström795dbe42015-11-27 14:09:07 +0100147void VideoSender::RegisterExternalEncoder(VideoEncoder* externalEncoder,
philipel5908c712015-12-21 08:23:20 -0800148 uint8_t payloadType,
149 bool internalSource /*= false*/) {
perkj4e417b22016-07-14 23:35:55 -0700150 RTC_DCHECK(sequenced_checker_.CalledSequentially());
tommi@webrtc.org558dc402015-03-07 20:55:56 +0000151
Peter Boström233bfd22016-01-18 20:23:40 +0100152 rtc::CritScope lock(&encoder_crit_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000153
mflodmanfcf54bd2015-04-14 21:28:08 +0200154 if (externalEncoder == nullptr) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000155 bool wasSendCodec = false;
Peter Boström795dbe42015-11-27 14:09:07 +0100156 RTC_CHECK(
157 _codecDataBase.DeregisterExternalEncoder(payloadType, &wasSendCodec));
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000158 if (wasSendCodec) {
159 // Make sure the VCM doesn't use the de-registered codec
Peter Boström233bfd22016-01-18 20:23:40 +0100160 rtc::CritScope params_lock(&params_crit_);
mflodmanfcf54bd2015-04-14 21:28:08 +0200161 _encoder = nullptr;
Peter Boström233bfd22016-01-18 20:23:40 +0100162 encoder_has_internal_source_ = false;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000163 }
Peter Boström795dbe42015-11-27 14:09:07 +0100164 return;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000165 }
philipel5908c712015-12-21 08:23:20 -0800166 _codecDataBase.RegisterExternalEncoder(externalEncoder, payloadType,
167 internalSource);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000168}
169
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000170// Get encode bitrate
171int VideoSender::Bitrate(unsigned int* bitrate) const {
perkj4e417b22016-07-14 23:35:55 -0700172 RTC_DCHECK(sequenced_checker_.CalledSequentially());
tommi@webrtc.org558dc402015-03-07 20:55:56 +0000173 // Since we're running on the thread that's the only thread known to modify
174 // the value of _encoder, we don't need to grab the lock here.
175
Peter Boström69ccb332015-10-29 16:30:23 +0100176 if (!_encoder)
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000177 return VCM_UNINITIALIZED;
sprang647bf432016-11-10 06:46:20 -0800178 *bitrate = _encoder->GetEncoderParameters().target_bitrate.get_sum_bps();
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000179 return 0;
180}
181
182// Get encode frame rate
183int VideoSender::FrameRate(unsigned int* framerate) const {
perkj4e417b22016-07-14 23:35:55 -0700184 RTC_DCHECK(sequenced_checker_.CalledSequentially());
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
191 *framerate = _encoder->GetEncoderParameters().input_frame_rate;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000192 return 0;
193}
194
sprang647bf432016-11-10 06:46:20 -0800195int32_t VideoSender::UpdateChannelParemeters(
196 VideoBitrateAllocator* bitrate_allocator) {
197 EncoderParameters encoder_params;
198 {
199 rtc::CritScope cs(&params_crit_);
200 encoder_params = encoder_params_;
201 }
tommi@webrtc.org558dc402015-03-07 20:55:56 +0000202
sprang647bf432016-11-10 06:46:20 -0800203 return SetChannelParameters(encoder_params.target_bitrate.get_sum_bps(),
204 encoder_params.loss_rate, encoder_params.rtt,
205 bitrate_allocator);
206}
207
208int32_t VideoSender::SetChannelParameters(
209 uint32_t target_bitrate_bps,
210 uint8_t lossRate,
211 int64_t rtt,
212 VideoBitrateAllocator* bitrate_allocator) {
213 uint32_t video_target_rate_bps =
214 _mediaOpt.SetTargetRates(target_bitrate_bps, lossRate, rtt);
tommi@webrtc.org558dc402015-03-07 20:55:56 +0000215 uint32_t input_frame_rate = _mediaOpt.InputFrameRate();
sprang647bf432016-11-10 06:46:20 -0800216 BitrateAllocation bitrate_allocation;
217 if (bitrate_allocator) {
218 bitrate_allocation = bitrate_allocator->GetAllocation(video_target_rate_bps,
219 input_frame_rate);
220 } else {
221 DefaultVideoBitrateAllocator default_allocator(current_codec_);
222 bitrate_allocation = default_allocator.GetAllocation(video_target_rate_bps,
223 input_frame_rate);
224 }
tommi@webrtc.org558dc402015-03-07 20:55:56 +0000225
sprang647bf432016-11-10 06:46:20 -0800226 EncoderParameters encoder_params = {bitrate_allocation, lossRate, rtt,
noahricd4badbc2016-04-13 14:59:48 -0700227 input_frame_rate};
228 bool encoder_has_internal_source;
229 {
230 rtc::CritScope cs(&params_crit_);
231 encoder_params_ = encoder_params;
232 encoder_has_internal_source = encoder_has_internal_source_;
233 }
234
235 // For encoders with internal sources, we need to tell the encoder directly,
236 // instead of waiting for an AddVideoFrame that will never come (internal
237 // source encoders don't get input frames).
238 if (encoder_has_internal_source) {
239 rtc::CritScope cs(&encoder_crit_);
240 if (_encoder) {
perkj57c21f92016-06-17 07:27:16 -0700241 SetEncoderParameters(encoder_params, encoder_has_internal_source);
noahricd4badbc2016-04-13 14:59:48 -0700242 }
243 }
Erik Språng66a641a2015-06-11 14:20:07 +0200244
245 return VCM_OK;
246}
247
perkj57c21f92016-06-17 07:27:16 -0700248void VideoSender::SetEncoderParameters(EncoderParameters params,
249 bool has_internal_source) {
perkjfea93092016-05-14 00:58:48 -0700250 // |target_bitrate == 0 | means that the network is down or the send pacer is
perkj57c21f92016-06-17 07:27:16 -0700251 // full. We currently only report this if the encoder has an internal source.
252 // If the encoder does not have an internal source, higher levels are expected
253 // to not call AddVideoFrame. We do this since its unclear how current
254 // encoder implementations behave when given a zero target bitrate.
255 // TODO(perkj): Make sure all known encoder implementations handle zero
256 // target bitrate and remove this check.
sprang647bf432016-11-10 06:46:20 -0800257 if (!has_internal_source && params.target_bitrate.get_sum_bps() == 0)
Peter Boströmdcb89982015-09-15 14:43:47 +0200258 return;
tommi@webrtc.org558dc402015-03-07 20:55:56 +0000259
Erik Språng66a641a2015-06-11 14:20:07 +0200260 if (params.input_frame_rate == 0) {
261 // No frame rate estimate available, use default.
262 params.input_frame_rate = current_codec_.maxFramerate;
263 }
Peter Boström69ccb332015-10-29 16:30:23 +0100264 if (_encoder != nullptr)
265 _encoder->SetEncoderParameters(params);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000266}
267
Per69b332d2016-06-02 15:45:42 +0200268// Deprecated:
269// TODO(perkj): Remove once no projects call this method. It currently do
270// nothing.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000271int32_t VideoSender::RegisterProtectionCallback(
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000272 VCMProtectionCallback* protection_callback) {
Per69b332d2016-06-02 15:45:42 +0200273 // Deprecated:
274 // TODO(perkj): Remove once no projects call this method. It currently do
275 // nothing.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000276 return VCM_OK;
277}
278
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000279// Add one raw video frame to the encoder, blocking.
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700280int32_t VideoSender::AddVideoFrame(const VideoFrame& videoFrame,
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000281 const CodecSpecificInfo* codecSpecificInfo) {
Peter Boströmdcb89982015-09-15 14:43:47 +0200282 EncoderParameters encoder_params;
Peter Boström233bfd22016-01-18 20:23:40 +0100283 std::vector<FrameType> next_frame_types;
perkj57c21f92016-06-17 07:27:16 -0700284 bool encoder_has_internal_source = false;
Peter Boströmdcb89982015-09-15 14:43:47 +0200285 {
Peter Boström233bfd22016-01-18 20:23:40 +0100286 rtc::CritScope lock(&params_crit_);
Peter Boströmdcb89982015-09-15 14:43:47 +0200287 encoder_params = encoder_params_;
Peter Boström233bfd22016-01-18 20:23:40 +0100288 next_frame_types = next_frame_types_;
perkj57c21f92016-06-17 07:27:16 -0700289 encoder_has_internal_source = encoder_has_internal_source_;
Peter Boströmdcb89982015-09-15 14:43:47 +0200290 }
Peter Boström233bfd22016-01-18 20:23:40 +0100291 rtc::CritScope lock(&encoder_crit_);
Peter Boström69ccb332015-10-29 16:30:23 +0100292 if (_encoder == nullptr)
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000293 return VCM_UNINITIALIZED;
perkj57c21f92016-06-17 07:27:16 -0700294 SetEncoderParameters(encoder_params, encoder_has_internal_source);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000295 if (_mediaOpt.DropFrame()) {
isheriff7620be82016-03-06 23:22:33 -0800296 LOG(LS_VERBOSE) << "Drop Frame "
sprang647bf432016-11-10 06:46:20 -0800297 << "target bitrate "
298 << encoder_params.target_bitrate.get_sum_bps()
isheriff7620be82016-03-06 23:22:33 -0800299 << " loss rate " << encoder_params.loss_rate << " rtt "
300 << encoder_params.rtt << " input frame rate "
301 << encoder_params.input_frame_rate;
jackychen61b4d512015-04-21 15:30:11 -0700302 _encoder->OnDroppedFrame();
stefan@webrtc.org34c5da62014-04-11 14:08:35 +0000303 return VCM_OK;
304 }
pbos@webrtc.org67a9e402015-03-05 13:57:37 +0000305 // TODO(pbos): Make sure setting send codec is synchronized with video
306 // processing so frame size always matches.
307 if (!_codecDataBase.MatchesCurrentResolution(videoFrame.width(),
308 videoFrame.height())) {
309 LOG(LS_ERROR) << "Incoming frame doesn't match set resolution. Dropping.";
310 return VCM_PARAMETER_ERROR;
311 }
Peter Boströmeb66e802015-06-05 11:08:03 +0200312 VideoFrame converted_frame = videoFrame;
nisse26acec42016-04-15 03:43:39 -0700313 if (converted_frame.video_frame_buffer()->native_handle() &&
314 !_encoder->SupportsNativeHandle()) {
Peter Boströmeb66e802015-06-05 11:08:03 +0200315 // This module only supports software encoding.
316 // TODO(pbos): Offload conversion from the encoder thread.
nisseca6d5d12016-06-17 05:03:04 -0700317 rtc::scoped_refptr<VideoFrameBuffer> converted_buffer(
318 converted_frame.video_frame_buffer()->NativeToI420Buffer());
319
320 if (!converted_buffer) {
321 LOG(LS_ERROR) << "Frame conversion failed, dropping frame.";
322 return VCM_PARAMETER_ERROR;
323 }
324 converted_frame = VideoFrame(converted_buffer,
325 converted_frame.timestamp(),
326 converted_frame.render_time_ms(),
327 converted_frame.rotation());
Peter Boströmeb66e802015-06-05 11:08:03 +0200328 }
stefan@webrtc.org34c5da62014-04-11 14:08:35 +0000329 int32_t ret =
Peter Boström233bfd22016-01-18 20:23:40 +0100330 _encoder->Encode(converted_frame, codecSpecificInfo, next_frame_types);
stefan@webrtc.org34c5da62014-04-11 14:08:35 +0000331 if (ret < 0) {
332 LOG(LS_ERROR) << "Failed to encode frame. Error code: " << ret;
333 return ret;
334 }
perkj376b1922016-05-02 11:35:24 -0700335
Peter Boström233bfd22016-01-18 20:23:40 +0100336 {
Peter Boström233bfd22016-01-18 20:23:40 +0100337 rtc::CritScope lock(&params_crit_);
perkj376b1922016-05-02 11:35:24 -0700338 // Change all keyframe requests to encode delta frames the next time.
Peter Boström233bfd22016-01-18 20:23:40 +0100339 for (size_t i = 0; i < next_frame_types_.size(); ++i) {
340 // Check for equality (same requested as before encoding) to not
341 // accidentally drop a keyframe request while encoding.
342 if (next_frame_types[i] == next_frame_types_[i])
343 next_frame_types_[i] = kVideoFrameDelta;
344 }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000345 }
346 return VCM_OK;
347}
348
perkj600246e2016-05-04 11:26:51 -0700349int32_t VideoSender::IntraFrameRequest(size_t stream_index) {
Peter Boström233bfd22016-01-18 20:23:40 +0100350 {
351 rtc::CritScope lock(&params_crit_);
perkj600246e2016-05-04 11:26:51 -0700352 if (stream_index >= next_frame_types_.size()) {
Peter Boström233bfd22016-01-18 20:23:40 +0100353 return -1;
354 }
355 next_frame_types_[stream_index] = kVideoFrameKey;
356 if (!encoder_has_internal_source_)
357 return VCM_OK;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000358 }
Peter Boström233bfd22016-01-18 20:23:40 +0100359 // TODO(pbos): Remove when InternalSource() is gone. Both locks have to be
360 // held here for internal consistency, since _encoder could be removed while
361 // not holding encoder_crit_. Checks have to be performed again since
362 // params_crit_ was dropped to not cause lock-order inversions with
363 // encoder_crit_.
364 rtc::CritScope lock(&encoder_crit_);
365 rtc::CritScope params_lock(&params_crit_);
perkj600246e2016-05-04 11:26:51 -0700366 if (stream_index >= next_frame_types_.size())
Peter Boström233bfd22016-01-18 20:23:40 +0100367 return -1;
mflodmanfcf54bd2015-04-14 21:28:08 +0200368 if (_encoder != nullptr && _encoder->InternalSource()) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000369 // Try to request the frame if we have an external encoder with
370 // internal source since AddVideoFrame never will be called.
Peter Boström233bfd22016-01-18 20:23:40 +0100371 if (_encoder->RequestFrame(next_frame_types_) == WEBRTC_VIDEO_CODEC_OK) {
372 // Try to remove just-performed keyframe request, if stream still exists.
373 next_frame_types_[stream_index] = kVideoFrameDelta;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000374 }
375 }
376 return VCM_OK;
377}
378
379int32_t VideoSender::EnableFrameDropper(bool enable) {
Peter Boström233bfd22016-01-18 20:23:40 +0100380 rtc::CritScope lock(&encoder_crit_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000381 frame_dropper_enabled_ = enable;
382 _mediaOpt.EnableFrameDropper(enable);
383 return VCM_OK;
384}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000385} // namespace vcm
386} // namespace webrtc