blob: bfc96154931403d26f90f384ed7cd6f99898781a [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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "modules/video_coding/encoded_frame.h"
18#include "modules/video_coding/include/video_codec_interface.h"
19#include "modules/video_coding/utility/default_video_bitrate_allocator.h"
20#include "modules/video_coding/utility/quality_scaler.h"
21#include "modules/video_coding/video_coding_impl.h"
22#include "rtc_base/checks.h"
23#include "rtc_base/logging.h"
24#include "system_wrappers/include/clock.h"
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000025
26namespace webrtc {
27namespace vcm {
28
stefan@webrtc.org34c5da62014-04-11 14:08:35 +000029VideoSender::VideoSender(Clock* clock,
Niels Möllera0565992017-10-24 11:37:08 +020030 EncodedImageCallback* post_encode_callback)
31 : _encoder(nullptr),
32 _mediaOpt(clock),
perkj376b1922016-05-02 11:35:24 -070033 _encodedFrameCallback(post_encode_callback, &_mediaOpt),
kthelgason876222f2016-11-29 01:44:11 -080034 post_encode_callback_(post_encode_callback),
perkjf5b2e512016-07-05 08:34:04 -070035 _codecDataBase(&_encodedFrameCallback),
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000036 frame_dropper_enabled_(true),
tommi@webrtc.orge07710c2015-02-19 17:43:25 +000037 current_codec_(),
Erik Språng08127a92016-11-16 16:41:30 +010038 encoder_params_({BitrateAllocation(), 0, 0, 0}),
Peter Boström233bfd22016-01-18 20:23:40 +010039 encoder_has_internal_source_(false),
40 next_frame_types_(1, kVideoFrameDelta) {
Peter Boströmad6fc5a2016-05-12 03:01:31 +020041 _mediaOpt.Reset();
tommi@webrtc.org658d2012015-03-05 12:21:54 +000042 // Allow VideoSender to be created on one thread but used on another, post
43 // construction. This is currently how this class is being used by at least
44 // one external project (diffractor).
perkj4e417b22016-07-14 23:35:55 -070045 sequenced_checker_.Detach();
tommi@webrtc.orge07710c2015-02-19 17:43:25 +000046}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000047
Peter Boströmdcb89982015-09-15 14:43:47 +020048VideoSender::~VideoSender() {}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000049
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000050// Register the send codec to be used.
51int32_t VideoSender::RegisterSendCodec(const VideoCodec* sendCodec,
52 uint32_t numberOfCores,
53 uint32_t maxPayloadSize) {
perkj4e417b22016-07-14 23:35:55 -070054 RTC_DCHECK(sequenced_checker_.CalledSequentially());
Peter Boström233bfd22016-01-18 20:23:40 +010055 rtc::CritScope lock(&encoder_crit_);
mflodmanfcf54bd2015-04-14 21:28:08 +020056 if (sendCodec == nullptr) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000057 return VCM_PARAMETER_ERROR;
58 }
59
Peter Boström4f5db112015-10-29 16:53:59 +010060 bool ret =
61 _codecDataBase.SetSendCodec(sendCodec, numberOfCores, maxPayloadSize);
pbos@webrtc.orgda2c4ce2013-09-17 09:38:41 +000062
63 // Update encoder regardless of result to make sure that we're not holding on
64 // to a deleted instance.
65 _encoder = _codecDataBase.GetEncoder();
tommi@webrtc.orge07710c2015-02-19 17:43:25 +000066 // Cache the current codec here so they can be fetched from this thread
67 // without requiring the _sendCritSect lock.
68 current_codec_ = *sendCodec;
pbos@webrtc.orgda2c4ce2013-09-17 09:38:41 +000069
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000070 if (!ret) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010071 RTC_LOG(LS_ERROR) << "Failed to initialize set encoder with payload name '"
72 << sendCodec->plName << "'.";
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000073 return VCM_CODEC_ERROR;
74 }
75
Peter Boström233bfd22016-01-18 20:23:40 +010076 // SetSendCodec succeeded, _encoder should be set.
77 RTC_DCHECK(_encoder);
78
ivicac7199c22015-10-07 06:43:33 -070079 int numLayers;
80 if (sendCodec->codecType == kVideoCodecVP8) {
hta257dc392016-10-25 09:05:06 -070081 numLayers = sendCodec->VP8().numberOfTemporalLayers;
ivicac7199c22015-10-07 06:43:33 -070082 } else if (sendCodec->codecType == kVideoCodecVP9) {
hta257dc392016-10-25 09:05:06 -070083 numLayers = sendCodec->VP9().numberOfTemporalLayers;
sprang4847ae62017-06-27 07:06:52 -070084 } else if (sendCodec->codecType == kVideoCodecGeneric &&
85 sendCodec->numberOfSimulcastStreams > 0) {
86 // This is mainly for unit testing, disabling frame dropping.
87 // TODO(sprang): Add a better way to disable frame dropping.
88 numLayers = sendCodec->simulcastStream[0].numberOfTemporalLayers;
ivicac7199c22015-10-07 06:43:33 -070089 } else {
90 numLayers = 1;
91 }
92
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000093 // If we have screensharing and we have layers, we disable frame dropper.
94 bool disable_frame_dropper =
95 numLayers > 1 && sendCodec->mode == kScreensharing;
96 if (disable_frame_dropper) {
97 _mediaOpt.EnableFrameDropper(false);
98 } else if (frame_dropper_enabled_) {
99 _mediaOpt.EnableFrameDropper(true);
100 }
sprang1a646ee2016-12-01 06:34:11 -0800101
Peter Boström233bfd22016-01-18 20:23:40 +0100102 {
103 rtc::CritScope cs(&params_crit_);
104 next_frame_types_.clear();
105 next_frame_types_.resize(VCM_MAX(sendCodec->numberOfSimulcastStreams, 1),
106 kVideoFrameKey);
107 // Cache InternalSource() to have this available from IntraFrameRequest()
108 // without having to acquire encoder_crit_ (avoid blocking on encoder use).
109 encoder_has_internal_source_ = _encoder->InternalSource();
110 }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000111
Mirko Bonadei675513b2017-11-09 11:09:25 +0100112 RTC_LOG(LS_VERBOSE) << " max bitrate " << sendCodec->maxBitrate
113 << " start bitrate " << sendCodec->startBitrate
114 << " max frame rate " << sendCodec->maxFramerate
115 << " max payload size " << maxPayloadSize;
Per69b332d2016-06-02 15:45:42 +0200116 _mediaOpt.SetEncodingData(sendCodec->maxBitrate * 1000,
asapersson60dfbdb2017-08-07 00:03:03 -0700117 sendCodec->startBitrate * 1000,
118 sendCodec->maxFramerate);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000119 return VCM_OK;
120}
121
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000122// Register an external decoder object.
123// This can not be used together with external decoder callbacks.
Peter Boström795dbe42015-11-27 14:09:07 +0100124void VideoSender::RegisterExternalEncoder(VideoEncoder* externalEncoder,
philipel5908c712015-12-21 08:23:20 -0800125 uint8_t payloadType,
126 bool internalSource /*= false*/) {
perkj4e417b22016-07-14 23:35:55 -0700127 RTC_DCHECK(sequenced_checker_.CalledSequentially());
tommi@webrtc.org558dc402015-03-07 20:55:56 +0000128
Peter Boström233bfd22016-01-18 20:23:40 +0100129 rtc::CritScope lock(&encoder_crit_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000130
mflodmanfcf54bd2015-04-14 21:28:08 +0200131 if (externalEncoder == nullptr) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000132 bool wasSendCodec = false;
Peter Boström795dbe42015-11-27 14:09:07 +0100133 RTC_CHECK(
134 _codecDataBase.DeregisterExternalEncoder(payloadType, &wasSendCodec));
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000135 if (wasSendCodec) {
136 // Make sure the VCM doesn't use the de-registered codec
Peter Boström233bfd22016-01-18 20:23:40 +0100137 rtc::CritScope params_lock(&params_crit_);
mflodmanfcf54bd2015-04-14 21:28:08 +0200138 _encoder = nullptr;
Peter Boström233bfd22016-01-18 20:23:40 +0100139 encoder_has_internal_source_ = false;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000140 }
Peter Boström795dbe42015-11-27 14:09:07 +0100141 return;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000142 }
philipel5908c712015-12-21 08:23:20 -0800143 _codecDataBase.RegisterExternalEncoder(externalEncoder, payloadType,
144 internalSource);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000145}
146
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000147// Get encode bitrate
148int VideoSender::Bitrate(unsigned int* bitrate) const {
perkj4e417b22016-07-14 23:35:55 -0700149 RTC_DCHECK(sequenced_checker_.CalledSequentially());
tommi@webrtc.org558dc402015-03-07 20:55:56 +0000150 // Since we're running on the thread that's the only thread known to modify
151 // the value of _encoder, we don't need to grab the lock here.
152
Peter Boström69ccb332015-10-29 16:30:23 +0100153 if (!_encoder)
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000154 return VCM_UNINITIALIZED;
Erik Språng08127a92016-11-16 16:41:30 +0100155 *bitrate = _encoder->GetEncoderParameters().target_bitrate.get_sum_bps();
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000156 return 0;
157}
158
159// Get encode frame rate
160int VideoSender::FrameRate(unsigned int* framerate) const {
perkj4e417b22016-07-14 23:35:55 -0700161 RTC_DCHECK(sequenced_checker_.CalledSequentially());
tommi@webrtc.org558dc402015-03-07 20:55:56 +0000162 // Since we're running on the thread that's the only thread known to modify
163 // the value of _encoder, we don't need to grab the lock here.
164
Peter Boström69ccb332015-10-29 16:30:23 +0100165 if (!_encoder)
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000166 return VCM_UNINITIALIZED;
Peter Boström69ccb332015-10-29 16:30:23 +0100167
168 *framerate = _encoder->GetEncoderParameters().input_frame_rate;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000169 return 0;
170}
171
Erik Språng08127a92016-11-16 16:41:30 +0100172EncoderParameters VideoSender::UpdateEncoderParameters(
173 const EncoderParameters& params,
174 VideoBitrateAllocator* bitrate_allocator,
175 uint32_t target_bitrate_bps) {
asapersson9abd2752016-12-08 02:19:40 -0800176 uint32_t video_target_rate_bps = _mediaOpt.SetTargetRates(target_bitrate_bps);
tommi@webrtc.org558dc402015-03-07 20:55:56 +0000177 uint32_t input_frame_rate = _mediaOpt.InputFrameRate();
sprang40217c32016-11-21 05:41:52 -0800178 if (input_frame_rate == 0)
179 input_frame_rate = current_codec_.maxFramerate;
180
Erik Språng08127a92016-11-16 16:41:30 +0100181 BitrateAllocation bitrate_allocation;
sprang4847ae62017-06-27 07:06:52 -0700182 // Only call allocators if bitrate > 0 (ie, not suspended), otherwise they
183 // might cap the bitrate to the min bitrate configured.
184 if (target_bitrate_bps > 0) {
185 if (bitrate_allocator) {
186 bitrate_allocation = bitrate_allocator->GetAllocation(
187 video_target_rate_bps, input_frame_rate);
188 } else {
189 DefaultVideoBitrateAllocator default_allocator(current_codec_);
190 bitrate_allocation = default_allocator.GetAllocation(
191 video_target_rate_bps, input_frame_rate);
192 }
Erik Språng08127a92016-11-16 16:41:30 +0100193 }
Erik Språng08127a92016-11-16 16:41:30 +0100194 EncoderParameters new_encoder_params = {bitrate_allocation, params.loss_rate,
195 params.rtt, input_frame_rate};
196 return new_encoder_params;
197}
198
199void VideoSender::UpdateChannelParemeters(
sprang1a646ee2016-12-01 06:34:11 -0800200 VideoBitrateAllocator* bitrate_allocator,
201 VideoBitrateAllocationObserver* bitrate_updated_callback) {
202 BitrateAllocation target_rate;
203 {
204 rtc::CritScope cs(&params_crit_);
205 encoder_params_ =
206 UpdateEncoderParameters(encoder_params_, bitrate_allocator,
207 encoder_params_.target_bitrate.get_sum_bps());
208 target_rate = encoder_params_.target_bitrate;
209 }
sprang4847ae62017-06-27 07:06:52 -0700210 if (bitrate_updated_callback && target_rate.get_sum_bps() > 0)
sprang1a646ee2016-12-01 06:34:11 -0800211 bitrate_updated_callback->OnBitrateAllocationUpdated(target_rate);
Erik Språng08127a92016-11-16 16:41:30 +0100212}
213
214int32_t VideoSender::SetChannelParameters(
215 uint32_t target_bitrate_bps,
sprang1a646ee2016-12-01 06:34:11 -0800216 uint8_t loss_rate,
Erik Språng08127a92016-11-16 16:41:30 +0100217 int64_t rtt,
sprang1a646ee2016-12-01 06:34:11 -0800218 VideoBitrateAllocator* bitrate_allocator,
219 VideoBitrateAllocationObserver* bitrate_updated_callback) {
Erik Språng08127a92016-11-16 16:41:30 +0100220 EncoderParameters encoder_params;
sprang1a646ee2016-12-01 06:34:11 -0800221 encoder_params.loss_rate = loss_rate;
Erik Språng08127a92016-11-16 16:41:30 +0100222 encoder_params.rtt = rtt;
223 encoder_params = UpdateEncoderParameters(encoder_params, bitrate_allocator,
224 target_bitrate_bps);
sprang4847ae62017-06-27 07:06:52 -0700225 if (bitrate_updated_callback && target_bitrate_bps > 0) {
sprang1a646ee2016-12-01 06:34:11 -0800226 bitrate_updated_callback->OnBitrateAllocationUpdated(
227 encoder_params.target_bitrate);
228 }
Erik Språng08127a92016-11-16 16:41:30 +0100229
noahricd4badbc2016-04-13 14:59:48 -0700230 bool encoder_has_internal_source;
231 {
232 rtc::CritScope cs(&params_crit_);
233 encoder_params_ = encoder_params;
234 encoder_has_internal_source = encoder_has_internal_source_;
235 }
236
237 // For encoders with internal sources, we need to tell the encoder directly,
238 // instead of waiting for an AddVideoFrame that will never come (internal
239 // source encoders don't get input frames).
240 if (encoder_has_internal_source) {
241 rtc::CritScope cs(&encoder_crit_);
242 if (_encoder) {
perkj57c21f92016-06-17 07:27:16 -0700243 SetEncoderParameters(encoder_params, encoder_has_internal_source);
noahricd4badbc2016-04-13 14:59:48 -0700244 }
245 }
Erik Språng66a641a2015-06-11 14:20:07 +0200246
247 return VCM_OK;
248}
249
perkj57c21f92016-06-17 07:27:16 -0700250void VideoSender::SetEncoderParameters(EncoderParameters params,
251 bool has_internal_source) {
perkjfea93092016-05-14 00:58:48 -0700252 // |target_bitrate == 0 | means that the network is down or the send pacer is
perkj57c21f92016-06-17 07:27:16 -0700253 // full. We currently only report this if the encoder has an internal source.
254 // If the encoder does not have an internal source, higher levels are expected
255 // to not call AddVideoFrame. We do this since its unclear how current
256 // encoder implementations behave when given a zero target bitrate.
257 // TODO(perkj): Make sure all known encoder implementations handle zero
258 // target bitrate and remove this check.
Erik Språng08127a92016-11-16 16:41:30 +0100259 if (!has_internal_source && params.target_bitrate.get_sum_bps() == 0)
Peter Boströmdcb89982015-09-15 14:43:47 +0200260 return;
tommi@webrtc.org558dc402015-03-07 20:55:56 +0000261
Erik Språng66a641a2015-06-11 14:20:07 +0200262 if (params.input_frame_rate == 0) {
263 // No frame rate estimate available, use default.
264 params.input_frame_rate = current_codec_.maxFramerate;
265 }
Peter Boström69ccb332015-10-29 16:30:23 +0100266 if (_encoder != nullptr)
267 _encoder->SetEncoderParameters(params);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000268}
269
Per69b332d2016-06-02 15:45:42 +0200270// Deprecated:
271// TODO(perkj): Remove once no projects call this method. It currently do
272// nothing.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000273int32_t VideoSender::RegisterProtectionCallback(
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000274 VCMProtectionCallback* protection_callback) {
Per69b332d2016-06-02 15:45:42 +0200275 // Deprecated:
276 // TODO(perkj): Remove once no projects call this method. It currently do
277 // nothing.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000278 return VCM_OK;
279}
280
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000281// 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 CodecSpecificInfo* codecSpecificInfo) {
Peter Boströmdcb89982015-09-15 14:43:47 +0200284 EncoderParameters encoder_params;
Peter Boström233bfd22016-01-18 20:23:40 +0100285 std::vector<FrameType> next_frame_types;
perkj57c21f92016-06-17 07:27:16 -0700286 bool encoder_has_internal_source = false;
Peter Boströmdcb89982015-09-15 14:43:47 +0200287 {
Peter Boström233bfd22016-01-18 20:23:40 +0100288 rtc::CritScope lock(&params_crit_);
Peter Boströmdcb89982015-09-15 14:43:47 +0200289 encoder_params = encoder_params_;
Peter Boström233bfd22016-01-18 20:23:40 +0100290 next_frame_types = next_frame_types_;
perkj57c21f92016-06-17 07:27:16 -0700291 encoder_has_internal_source = encoder_has_internal_source_;
Peter Boströmdcb89982015-09-15 14:43:47 +0200292 }
Peter Boström233bfd22016-01-18 20:23:40 +0100293 rtc::CritScope lock(&encoder_crit_);
Peter Boström69ccb332015-10-29 16:30:23 +0100294 if (_encoder == nullptr)
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000295 return VCM_UNINITIALIZED;
perkj57c21f92016-06-17 07:27:16 -0700296 SetEncoderParameters(encoder_params, encoder_has_internal_source);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000297 if (_mediaOpt.DropFrame()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100298 RTC_LOG(LS_VERBOSE) << "Drop Frame "
299 << "target bitrate "
300 << encoder_params.target_bitrate.get_sum_bps()
301 << " loss rate " << encoder_params.loss_rate << " rtt "
302 << encoder_params.rtt << " input frame rate "
303 << encoder_params.input_frame_rate;
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200304 post_encode_callback_->OnDroppedFrame(
305 EncodedImageCallback::DropReason::kDroppedByMediaOptimizations);
stefan@webrtc.org34c5da62014-04-11 14:08:35 +0000306 return VCM_OK;
307 }
pbos@webrtc.org67a9e402015-03-05 13:57:37 +0000308 // TODO(pbos): Make sure setting send codec is synchronized with video
309 // processing so frame size always matches.
310 if (!_codecDataBase.MatchesCurrentResolution(videoFrame.width(),
311 videoFrame.height())) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100312 RTC_LOG(LS_ERROR)
313 << "Incoming frame doesn't match set resolution. Dropping.";
pbos@webrtc.org67a9e402015-03-05 13:57:37 +0000314 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) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100330 RTC_LOG(LS_ERROR) << "Frame conversion failed, dropping frame.";
nisseca6d5d12016-06-17 05:03:04 -0700331 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) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100341 RTC_LOG(LS_ERROR) << "Failed to encode frame. Error code: " << ret;
stefan@webrtc.org34c5da62014-04-11 14:08:35 +0000342 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