blob: fbf6ff8dcc0dd2939fcffce808ac69159729cba4 [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
Mirko Bonadei71207422017-09-15 13:58:09 +020014#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "common_video/include/video_bitrate_allocator.h"
16#include "common_video/libyuv/include/webrtc_libyuv.h"
17#include "modules/video_coding/codecs/vp8/temporal_layers.h"
18#include "modules/video_coding/encoded_frame.h"
19#include "modules/video_coding/include/video_codec_interface.h"
20#include "modules/video_coding/utility/default_video_bitrate_allocator.h"
21#include "modules/video_coding/utility/quality_scaler.h"
22#include "modules/video_coding/video_coding_impl.h"
23#include "rtc_base/checks.h"
24#include "rtc_base/logging.h"
25#include "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,
Niels Möllera0565992017-10-24 11:37:08 +020031 EncodedImageCallback* post_encode_callback)
32 : _encoder(nullptr),
33 _mediaOpt(clock),
perkj376b1922016-05-02 11:35:24 -070034 _encodedFrameCallback(post_encode_callback, &_mediaOpt),
kthelgason876222f2016-11-29 01:44:11 -080035 post_encode_callback_(post_encode_callback),
perkjf5b2e512016-07-05 08:34:04 -070036 _codecDataBase(&_encodedFrameCallback),
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000037 frame_dropper_enabled_(true),
tommi@webrtc.orge07710c2015-02-19 17:43:25 +000038 current_codec_(),
Erik Språng08127a92016-11-16 16:41:30 +010039 encoder_params_({BitrateAllocation(), 0, 0, 0}),
Peter Boström233bfd22016-01-18 20:23:40 +010040 encoder_has_internal_source_(false),
41 next_frame_types_(1, kVideoFrameDelta) {
Peter Boströmad6fc5a2016-05-12 03:01:31 +020042 _mediaOpt.Reset();
tommi@webrtc.org658d2012015-03-05 12:21:54 +000043 // Allow VideoSender to be created on one thread but used on another, post
44 // construction. This is currently how this class is being used by at least
45 // one external project (diffractor).
perkj4e417b22016-07-14 23:35:55 -070046 sequenced_checker_.Detach();
tommi@webrtc.orge07710c2015-02-19 17:43:25 +000047}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000048
Peter Boströmdcb89982015-09-15 14:43:47 +020049VideoSender::~VideoSender() {}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000050
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000051// Register the send codec to be used.
52int32_t VideoSender::RegisterSendCodec(const VideoCodec* sendCodec,
53 uint32_t numberOfCores,
54 uint32_t maxPayloadSize) {
perkj4e417b22016-07-14 23:35:55 -070055 RTC_DCHECK(sequenced_checker_.CalledSequentially());
Peter Boström233bfd22016-01-18 20:23:40 +010056 rtc::CritScope lock(&encoder_crit_);
mflodmanfcf54bd2015-04-14 21:28:08 +020057 if (sendCodec == nullptr) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000058 return VCM_PARAMETER_ERROR;
59 }
60
Peter Boström4f5db112015-10-29 16:53:59 +010061 bool ret =
62 _codecDataBase.SetSendCodec(sendCodec, numberOfCores, maxPayloadSize);
pbos@webrtc.orgda2c4ce2013-09-17 09:38:41 +000063
64 // Update encoder regardless of result to make sure that we're not holding on
65 // to a deleted instance.
66 _encoder = _codecDataBase.GetEncoder();
tommi@webrtc.orge07710c2015-02-19 17:43:25 +000067 // Cache the current codec here so they can be fetched from this thread
68 // without requiring the _sendCritSect lock.
69 current_codec_ = *sendCodec;
pbos@webrtc.orgda2c4ce2013-09-17 09:38:41 +000070
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000071 if (!ret) {
pbos@webrtc.org0b52ceb2015-03-24 11:20:54 +000072 LOG(LS_ERROR) << "Failed to initialize set encoder with payload name '"
73 << sendCodec->plName << "'.";
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000074 return VCM_CODEC_ERROR;
75 }
76
Peter Boström233bfd22016-01-18 20:23:40 +010077 // SetSendCodec succeeded, _encoder should be set.
78 RTC_DCHECK(_encoder);
79
ivicac7199c22015-10-07 06:43:33 -070080 int numLayers;
81 if (sendCodec->codecType == kVideoCodecVP8) {
hta257dc392016-10-25 09:05:06 -070082 numLayers = sendCodec->VP8().numberOfTemporalLayers;
ivicac7199c22015-10-07 06:43:33 -070083 } else if (sendCodec->codecType == kVideoCodecVP9) {
hta257dc392016-10-25 09:05:06 -070084 numLayers = sendCodec->VP9().numberOfTemporalLayers;
sprang4847ae62017-06-27 07:06:52 -070085 } else if (sendCodec->codecType == kVideoCodecGeneric &&
86 sendCodec->numberOfSimulcastStreams > 0) {
87 // This is mainly for unit testing, disabling frame dropping.
88 // TODO(sprang): Add a better way to disable frame dropping.
89 numLayers = sendCodec->simulcastStream[0].numberOfTemporalLayers;
ivicac7199c22015-10-07 06:43:33 -070090 } else {
91 numLayers = 1;
92 }
93
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000094 // If we have screensharing and we have layers, we disable frame dropper.
95 bool disable_frame_dropper =
96 numLayers > 1 && sendCodec->mode == kScreensharing;
97 if (disable_frame_dropper) {
98 _mediaOpt.EnableFrameDropper(false);
99 } else if (frame_dropper_enabled_) {
100 _mediaOpt.EnableFrameDropper(true);
101 }
sprang1a646ee2016-12-01 06:34:11 -0800102
Peter Boström233bfd22016-01-18 20:23:40 +0100103 {
104 rtc::CritScope cs(&params_crit_);
105 next_frame_types_.clear();
106 next_frame_types_.resize(VCM_MAX(sendCodec->numberOfSimulcastStreams, 1),
107 kVideoFrameKey);
108 // Cache InternalSource() to have this available from IntraFrameRequest()
109 // without having to acquire encoder_crit_ (avoid blocking on encoder use).
110 encoder_has_internal_source_ = _encoder->InternalSource();
111 }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000112
isheriff7620be82016-03-06 23:22:33 -0800113 LOG(LS_VERBOSE) << " max bitrate " << sendCodec->maxBitrate
114 << " start bitrate " << sendCodec->startBitrate
115 << " max frame rate " << sendCodec->maxFramerate
116 << " max payload size " << maxPayloadSize;
Per69b332d2016-06-02 15:45:42 +0200117 _mediaOpt.SetEncodingData(sendCodec->maxBitrate * 1000,
asapersson60dfbdb2017-08-07 00:03:03 -0700118 sendCodec->startBitrate * 1000,
119 sendCodec->maxFramerate);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000120 return VCM_OK;
121}
122
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000123// Register an external decoder object.
124// This can not be used together with external decoder callbacks.
Peter Boström795dbe42015-11-27 14:09:07 +0100125void VideoSender::RegisterExternalEncoder(VideoEncoder* externalEncoder,
philipel5908c712015-12-21 08:23:20 -0800126 uint8_t payloadType,
127 bool internalSource /*= false*/) {
perkj4e417b22016-07-14 23:35:55 -0700128 RTC_DCHECK(sequenced_checker_.CalledSequentially());
tommi@webrtc.org558dc402015-03-07 20:55:56 +0000129
Peter Boström233bfd22016-01-18 20:23:40 +0100130 rtc::CritScope lock(&encoder_crit_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000131
mflodmanfcf54bd2015-04-14 21:28:08 +0200132 if (externalEncoder == nullptr) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000133 bool wasSendCodec = false;
Peter Boström795dbe42015-11-27 14:09:07 +0100134 RTC_CHECK(
135 _codecDataBase.DeregisterExternalEncoder(payloadType, &wasSendCodec));
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000136 if (wasSendCodec) {
137 // Make sure the VCM doesn't use the de-registered codec
Peter Boström233bfd22016-01-18 20:23:40 +0100138 rtc::CritScope params_lock(&params_crit_);
mflodmanfcf54bd2015-04-14 21:28:08 +0200139 _encoder = nullptr;
Peter Boström233bfd22016-01-18 20:23:40 +0100140 encoder_has_internal_source_ = false;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000141 }
Peter Boström795dbe42015-11-27 14:09:07 +0100142 return;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000143 }
philipel5908c712015-12-21 08:23:20 -0800144 _codecDataBase.RegisterExternalEncoder(externalEncoder, payloadType,
145 internalSource);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000146}
147
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000148// Get encode bitrate
149int VideoSender::Bitrate(unsigned int* bitrate) const {
perkj4e417b22016-07-14 23:35:55 -0700150 RTC_DCHECK(sequenced_checker_.CalledSequentially());
tommi@webrtc.org558dc402015-03-07 20:55:56 +0000151 // Since we're running on the thread that's the only thread known to modify
152 // the value of _encoder, we don't need to grab the lock here.
153
Peter Boström69ccb332015-10-29 16:30:23 +0100154 if (!_encoder)
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000155 return VCM_UNINITIALIZED;
Erik Språng08127a92016-11-16 16:41:30 +0100156 *bitrate = _encoder->GetEncoderParameters().target_bitrate.get_sum_bps();
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000157 return 0;
158}
159
160// Get encode frame rate
161int VideoSender::FrameRate(unsigned int* framerate) const {
perkj4e417b22016-07-14 23:35:55 -0700162 RTC_DCHECK(sequenced_checker_.CalledSequentially());
tommi@webrtc.org558dc402015-03-07 20:55:56 +0000163 // Since we're running on the thread that's the only thread known to modify
164 // the value of _encoder, we don't need to grab the lock here.
165
Peter Boström69ccb332015-10-29 16:30:23 +0100166 if (!_encoder)
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000167 return VCM_UNINITIALIZED;
Peter Boström69ccb332015-10-29 16:30:23 +0100168
169 *framerate = _encoder->GetEncoderParameters().input_frame_rate;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000170 return 0;
171}
172
Erik Språng08127a92016-11-16 16:41:30 +0100173EncoderParameters VideoSender::UpdateEncoderParameters(
174 const EncoderParameters& params,
175 VideoBitrateAllocator* bitrate_allocator,
176 uint32_t target_bitrate_bps) {
asapersson9abd2752016-12-08 02:19:40 -0800177 uint32_t video_target_rate_bps = _mediaOpt.SetTargetRates(target_bitrate_bps);
tommi@webrtc.org558dc402015-03-07 20:55:56 +0000178 uint32_t input_frame_rate = _mediaOpt.InputFrameRate();
sprang40217c32016-11-21 05:41:52 -0800179 if (input_frame_rate == 0)
180 input_frame_rate = current_codec_.maxFramerate;
181
Erik Språng08127a92016-11-16 16:41:30 +0100182 BitrateAllocation bitrate_allocation;
sprang4847ae62017-06-27 07:06:52 -0700183 // Only call allocators if bitrate > 0 (ie, not suspended), otherwise they
184 // might cap the bitrate to the min bitrate configured.
185 if (target_bitrate_bps > 0) {
186 if (bitrate_allocator) {
187 bitrate_allocation = bitrate_allocator->GetAllocation(
188 video_target_rate_bps, input_frame_rate);
189 } else {
190 DefaultVideoBitrateAllocator default_allocator(current_codec_);
191 bitrate_allocation = default_allocator.GetAllocation(
192 video_target_rate_bps, input_frame_rate);
193 }
Erik Språng08127a92016-11-16 16:41:30 +0100194 }
Erik Språng08127a92016-11-16 16:41:30 +0100195 EncoderParameters new_encoder_params = {bitrate_allocation, params.loss_rate,
196 params.rtt, input_frame_rate};
197 return new_encoder_params;
198}
199
200void VideoSender::UpdateChannelParemeters(
sprang1a646ee2016-12-01 06:34:11 -0800201 VideoBitrateAllocator* bitrate_allocator,
202 VideoBitrateAllocationObserver* bitrate_updated_callback) {
203 BitrateAllocation target_rate;
204 {
205 rtc::CritScope cs(&params_crit_);
206 encoder_params_ =
207 UpdateEncoderParameters(encoder_params_, bitrate_allocator,
208 encoder_params_.target_bitrate.get_sum_bps());
209 target_rate = encoder_params_.target_bitrate;
210 }
sprang4847ae62017-06-27 07:06:52 -0700211 if (bitrate_updated_callback && target_rate.get_sum_bps() > 0)
sprang1a646ee2016-12-01 06:34:11 -0800212 bitrate_updated_callback->OnBitrateAllocationUpdated(target_rate);
Erik Språng08127a92016-11-16 16:41:30 +0100213}
214
215int32_t VideoSender::SetChannelParameters(
216 uint32_t target_bitrate_bps,
sprang1a646ee2016-12-01 06:34:11 -0800217 uint8_t loss_rate,
Erik Språng08127a92016-11-16 16:41:30 +0100218 int64_t rtt,
sprang1a646ee2016-12-01 06:34:11 -0800219 VideoBitrateAllocator* bitrate_allocator,
220 VideoBitrateAllocationObserver* bitrate_updated_callback) {
Erik Språng08127a92016-11-16 16:41:30 +0100221 EncoderParameters encoder_params;
sprang1a646ee2016-12-01 06:34:11 -0800222 encoder_params.loss_rate = loss_rate;
Erik Språng08127a92016-11-16 16:41:30 +0100223 encoder_params.rtt = rtt;
224 encoder_params = UpdateEncoderParameters(encoder_params, bitrate_allocator,
225 target_bitrate_bps);
sprang4847ae62017-06-27 07:06:52 -0700226 if (bitrate_updated_callback && target_bitrate_bps > 0) {
sprang1a646ee2016-12-01 06:34:11 -0800227 bitrate_updated_callback->OnBitrateAllocationUpdated(
228 encoder_params.target_bitrate);
229 }
Erik Språng08127a92016-11-16 16:41:30 +0100230
noahricd4badbc2016-04-13 14:59:48 -0700231 bool encoder_has_internal_source;
232 {
233 rtc::CritScope cs(&params_crit_);
234 encoder_params_ = encoder_params;
235 encoder_has_internal_source = encoder_has_internal_source_;
236 }
237
238 // For encoders with internal sources, we need to tell the encoder directly,
239 // instead of waiting for an AddVideoFrame that will never come (internal
240 // source encoders don't get input frames).
241 if (encoder_has_internal_source) {
242 rtc::CritScope cs(&encoder_crit_);
243 if (_encoder) {
perkj57c21f92016-06-17 07:27:16 -0700244 SetEncoderParameters(encoder_params, encoder_has_internal_source);
noahricd4badbc2016-04-13 14:59:48 -0700245 }
246 }
Erik Språng66a641a2015-06-11 14:20:07 +0200247
248 return VCM_OK;
249}
250
perkj57c21f92016-06-17 07:27:16 -0700251void VideoSender::SetEncoderParameters(EncoderParameters params,
252 bool has_internal_source) {
perkjfea93092016-05-14 00:58:48 -0700253 // |target_bitrate == 0 | means that the network is down or the send pacer is
perkj57c21f92016-06-17 07:27:16 -0700254 // full. We currently only report this if the encoder has an internal source.
255 // If the encoder does not have an internal source, higher levels are expected
256 // to not call AddVideoFrame. We do this since its unclear how current
257 // encoder implementations behave when given a zero target bitrate.
258 // TODO(perkj): Make sure all known encoder implementations handle zero
259 // target bitrate and remove this check.
Erik Språng08127a92016-11-16 16:41:30 +0100260 if (!has_internal_source && params.target_bitrate.get_sum_bps() == 0)
Peter Boströmdcb89982015-09-15 14:43:47 +0200261 return;
tommi@webrtc.org558dc402015-03-07 20:55:56 +0000262
Erik Språng66a641a2015-06-11 14:20:07 +0200263 if (params.input_frame_rate == 0) {
264 // No frame rate estimate available, use default.
265 params.input_frame_rate = current_codec_.maxFramerate;
266 }
Peter Boström69ccb332015-10-29 16:30:23 +0100267 if (_encoder != nullptr)
268 _encoder->SetEncoderParameters(params);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000269}
270
Per69b332d2016-06-02 15:45:42 +0200271// Deprecated:
272// TODO(perkj): Remove once no projects call this method. It currently do
273// nothing.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000274int32_t VideoSender::RegisterProtectionCallback(
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000275 VCMProtectionCallback* protection_callback) {
Per69b332d2016-06-02 15:45:42 +0200276 // Deprecated:
277 // TODO(perkj): Remove once no projects call this method. It currently do
278 // nothing.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000279 return VCM_OK;
280}
281
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000282// Add one raw video frame to the encoder, blocking.
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700283int32_t VideoSender::AddVideoFrame(const VideoFrame& videoFrame,
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000284 const CodecSpecificInfo* codecSpecificInfo) {
Peter Boströmdcb89982015-09-15 14:43:47 +0200285 EncoderParameters encoder_params;
Peter Boström233bfd22016-01-18 20:23:40 +0100286 std::vector<FrameType> next_frame_types;
perkj57c21f92016-06-17 07:27:16 -0700287 bool encoder_has_internal_source = false;
Peter Boströmdcb89982015-09-15 14:43:47 +0200288 {
Peter Boström233bfd22016-01-18 20:23:40 +0100289 rtc::CritScope lock(&params_crit_);
Peter Boströmdcb89982015-09-15 14:43:47 +0200290 encoder_params = encoder_params_;
Peter Boström233bfd22016-01-18 20:23:40 +0100291 next_frame_types = next_frame_types_;
perkj57c21f92016-06-17 07:27:16 -0700292 encoder_has_internal_source = encoder_has_internal_source_;
Peter Boströmdcb89982015-09-15 14:43:47 +0200293 }
Peter Boström233bfd22016-01-18 20:23:40 +0100294 rtc::CritScope lock(&encoder_crit_);
Peter Boström69ccb332015-10-29 16:30:23 +0100295 if (_encoder == nullptr)
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000296 return VCM_UNINITIALIZED;
perkj57c21f92016-06-17 07:27:16 -0700297 SetEncoderParameters(encoder_params, encoder_has_internal_source);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000298 if (_mediaOpt.DropFrame()) {
isheriff7620be82016-03-06 23:22:33 -0800299 LOG(LS_VERBOSE) << "Drop Frame "
Erik Språng08127a92016-11-16 16:41:30 +0100300 << "target bitrate "
301 << encoder_params.target_bitrate.get_sum_bps()
isheriff7620be82016-03-06 23:22:33 -0800302 << " loss rate " << encoder_params.loss_rate << " rtt "
303 << encoder_params.rtt << " input frame rate "
304 << encoder_params.input_frame_rate;
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200305 post_encode_callback_->OnDroppedFrame(
306 EncodedImageCallback::DropReason::kDroppedByMediaOptimizations);
stefan@webrtc.org34c5da62014-04-11 14:08:35 +0000307 return VCM_OK;
308 }
pbos@webrtc.org67a9e402015-03-05 13:57:37 +0000309 // TODO(pbos): Make sure setting send codec is synchronized with video
310 // processing so frame size always matches.
311 if (!_codecDataBase.MatchesCurrentResolution(videoFrame.width(),
312 videoFrame.height())) {
313 LOG(LS_ERROR) << "Incoming frame doesn't match set resolution. Dropping.";
314 return VCM_PARAMETER_ERROR;
315 }
Peter Boströmeb66e802015-06-05 11:08:03 +0200316 VideoFrame converted_frame = videoFrame;
Magnus Jedvert72dbe2a2017-06-10 17:03:37 +0000317 const VideoFrameBuffer::Type buffer_type =
318 converted_frame.video_frame_buffer()->type();
319 const bool is_buffer_type_supported =
320 buffer_type == VideoFrameBuffer::Type::kI420 ||
321 (buffer_type == VideoFrameBuffer::Type::kNative &&
322 _encoder->SupportsNativeHandle());
323 if (!is_buffer_type_supported) {
Peter Boströmeb66e802015-06-05 11:08:03 +0200324 // This module only supports software encoding.
325 // TODO(pbos): Offload conversion from the encoder thread.
Magnus Jedvert72dbe2a2017-06-10 17:03:37 +0000326 rtc::scoped_refptr<I420BufferInterface> converted_buffer(
327 converted_frame.video_frame_buffer()->ToI420());
nisseca6d5d12016-06-17 05:03:04 -0700328
329 if (!converted_buffer) {
330 LOG(LS_ERROR) << "Frame conversion failed, dropping frame.";
331 return VCM_PARAMETER_ERROR;
332 }
333 converted_frame = VideoFrame(converted_buffer,
334 converted_frame.timestamp(),
335 converted_frame.render_time_ms(),
336 converted_frame.rotation());
Peter Boströmeb66e802015-06-05 11:08:03 +0200337 }
stefan@webrtc.org34c5da62014-04-11 14:08:35 +0000338 int32_t ret =
Peter Boström233bfd22016-01-18 20:23:40 +0100339 _encoder->Encode(converted_frame, codecSpecificInfo, next_frame_types);
stefan@webrtc.org34c5da62014-04-11 14:08:35 +0000340 if (ret < 0) {
341 LOG(LS_ERROR) << "Failed to encode frame. Error code: " << ret;
342 return ret;
343 }
perkj376b1922016-05-02 11:35:24 -0700344
Peter Boström233bfd22016-01-18 20:23:40 +0100345 {
Peter Boström233bfd22016-01-18 20:23:40 +0100346 rtc::CritScope lock(&params_crit_);
perkj376b1922016-05-02 11:35:24 -0700347 // Change all keyframe requests to encode delta frames the next time.
Peter Boström233bfd22016-01-18 20:23:40 +0100348 for (size_t i = 0; i < next_frame_types_.size(); ++i) {
349 // Check for equality (same requested as before encoding) to not
350 // accidentally drop a keyframe request while encoding.
351 if (next_frame_types[i] == next_frame_types_[i])
352 next_frame_types_[i] = kVideoFrameDelta;
353 }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000354 }
355 return VCM_OK;
356}
357
perkj600246e2016-05-04 11:26:51 -0700358int32_t VideoSender::IntraFrameRequest(size_t stream_index) {
Peter Boström233bfd22016-01-18 20:23:40 +0100359 {
360 rtc::CritScope lock(&params_crit_);
perkj600246e2016-05-04 11:26:51 -0700361 if (stream_index >= next_frame_types_.size()) {
Peter Boström233bfd22016-01-18 20:23:40 +0100362 return -1;
363 }
364 next_frame_types_[stream_index] = kVideoFrameKey;
365 if (!encoder_has_internal_source_)
366 return VCM_OK;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000367 }
Peter Boström233bfd22016-01-18 20:23:40 +0100368 // TODO(pbos): Remove when InternalSource() is gone. Both locks have to be
369 // held here for internal consistency, since _encoder could be removed while
370 // not holding encoder_crit_. Checks have to be performed again since
371 // params_crit_ was dropped to not cause lock-order inversions with
372 // encoder_crit_.
373 rtc::CritScope lock(&encoder_crit_);
374 rtc::CritScope params_lock(&params_crit_);
perkj600246e2016-05-04 11:26:51 -0700375 if (stream_index >= next_frame_types_.size())
Peter Boström233bfd22016-01-18 20:23:40 +0100376 return -1;
mflodmanfcf54bd2015-04-14 21:28:08 +0200377 if (_encoder != nullptr && _encoder->InternalSource()) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000378 // Try to request the frame if we have an external encoder with
379 // internal source since AddVideoFrame never will be called.
Peter Boström233bfd22016-01-18 20:23:40 +0100380 if (_encoder->RequestFrame(next_frame_types_) == WEBRTC_VIDEO_CODEC_OK) {
381 // Try to remove just-performed keyframe request, if stream still exists.
382 next_frame_types_[stream_index] = kVideoFrameDelta;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000383 }
384 }
385 return VCM_OK;
386}
387
388int32_t VideoSender::EnableFrameDropper(bool enable) {
Peter Boström233bfd22016-01-18 20:23:40 +0100389 rtc::CritScope lock(&encoder_crit_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000390 frame_dropper_enabled_ = enable;
391 _mediaOpt.EnableFrameDropper(enable);
392 return VCM_OK;
393}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000394} // namespace vcm
395} // namespace webrtc