blob: fe8af72570c762111256aee2b965859d733fd198 [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,
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),
kthelgason876222f2016-11-29 01:44:11 -080037 post_encode_callback_(post_encode_callback),
perkj376b1922016-05-02 11:35:24 -070038 send_stats_callback_(send_stats_callback),
perkjf5b2e512016-07-05 08:34:04 -070039 _codecDataBase(&_encodedFrameCallback),
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000040 frame_dropper_enabled_(true),
sprang40217c32016-11-21 05:41:52 -080041 _sendStatsTimer(VCMProcessTimer::kDefaultProcessIntervalMs, clock_),
tommi@webrtc.orge07710c2015-02-19 17:43:25 +000042 current_codec_(),
Erik Språng08127a92016-11-16 16:41:30 +010043 encoder_params_({BitrateAllocation(), 0, 0, 0}),
Peter Boström233bfd22016-01-18 20:23:40 +010044 encoder_has_internal_source_(false),
45 next_frame_types_(1, kVideoFrameDelta) {
Peter Boströmad6fc5a2016-05-12 03:01:31 +020046 _mediaOpt.Reset();
tommi@webrtc.org658d2012015-03-05 12:21:54 +000047 // Allow VideoSender to be created on one thread but used on another, post
48 // construction. This is currently how this class is being used by at least
49 // one external project (diffractor).
perkj4e417b22016-07-14 23:35:55 -070050 sequenced_checker_.Detach();
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
pbosa26ac922016-02-25 04:50:01 -080055void VideoSender::Process() {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000056 if (_sendStatsTimer.TimeUntilProcess() == 0) {
perkj376b1922016-05-02 11:35:24 -070057 // |_sendStatsTimer.Processed()| must be called. Otherwise
58 // VideoSender::Process() will be called in an infinite loop.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000059 _sendStatsTimer.Processed();
perkj376b1922016-05-02 11:35:24 -070060 if (send_stats_callback_) {
Åsa Perssonca0ed632017-10-11 12:59:01 +000061 uint32_t bitRate = _mediaOpt.SentBitRate();
62 uint32_t frameRate = _mediaOpt.SentFrameRate();
perkj275afc52016-09-01 00:21:16 -070063 send_stats_callback_->SendStatistics(bitRate, frameRate);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000064 }
65 }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000066}
67
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000068int64_t VideoSender::TimeUntilNextProcess() {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000069 return _sendStatsTimer.TimeUntilProcess();
70}
71
72// Register the send codec to be used.
73int32_t VideoSender::RegisterSendCodec(const VideoCodec* sendCodec,
74 uint32_t numberOfCores,
75 uint32_t maxPayloadSize) {
perkj4e417b22016-07-14 23:35:55 -070076 RTC_DCHECK(sequenced_checker_.CalledSequentially());
Peter Boström233bfd22016-01-18 20:23:40 +010077 rtc::CritScope lock(&encoder_crit_);
mflodmanfcf54bd2015-04-14 21:28:08 +020078 if (sendCodec == nullptr) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000079 return VCM_PARAMETER_ERROR;
80 }
81
Peter Boström4f5db112015-10-29 16:53:59 +010082 bool ret =
83 _codecDataBase.SetSendCodec(sendCodec, numberOfCores, maxPayloadSize);
pbos@webrtc.orgda2c4ce2013-09-17 09:38:41 +000084
85 // Update encoder regardless of result to make sure that we're not holding on
86 // to a deleted instance.
87 _encoder = _codecDataBase.GetEncoder();
tommi@webrtc.orge07710c2015-02-19 17:43:25 +000088 // Cache the current codec here so they can be fetched from this thread
89 // without requiring the _sendCritSect lock.
90 current_codec_ = *sendCodec;
pbos@webrtc.orgda2c4ce2013-09-17 09:38:41 +000091
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000092 if (!ret) {
pbos@webrtc.org0b52ceb2015-03-24 11:20:54 +000093 LOG(LS_ERROR) << "Failed to initialize set encoder with payload name '"
94 << sendCodec->plName << "'.";
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000095 return VCM_CODEC_ERROR;
96 }
97
Peter Boström233bfd22016-01-18 20:23:40 +010098 // SetSendCodec succeeded, _encoder should be set.
99 RTC_DCHECK(_encoder);
100
ivicac7199c22015-10-07 06:43:33 -0700101 int numLayers;
102 if (sendCodec->codecType == kVideoCodecVP8) {
hta257dc392016-10-25 09:05:06 -0700103 numLayers = sendCodec->VP8().numberOfTemporalLayers;
ivicac7199c22015-10-07 06:43:33 -0700104 } else if (sendCodec->codecType == kVideoCodecVP9) {
hta257dc392016-10-25 09:05:06 -0700105 numLayers = sendCodec->VP9().numberOfTemporalLayers;
sprang4847ae62017-06-27 07:06:52 -0700106 } else if (sendCodec->codecType == kVideoCodecGeneric &&
107 sendCodec->numberOfSimulcastStreams > 0) {
108 // This is mainly for unit testing, disabling frame dropping.
109 // TODO(sprang): Add a better way to disable frame dropping.
110 numLayers = sendCodec->simulcastStream[0].numberOfTemporalLayers;
ivicac7199c22015-10-07 06:43:33 -0700111 } else {
112 numLayers = 1;
113 }
114
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000115 // If we have screensharing and we have layers, we disable frame dropper.
116 bool disable_frame_dropper =
117 numLayers > 1 && sendCodec->mode == kScreensharing;
118 if (disable_frame_dropper) {
119 _mediaOpt.EnableFrameDropper(false);
120 } else if (frame_dropper_enabled_) {
121 _mediaOpt.EnableFrameDropper(true);
122 }
sprang1a646ee2016-12-01 06:34:11 -0800123
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,
asapersson60dfbdb2017-08-07 00:03:03 -0700139 sendCodec->startBitrate * 1000,
140 sendCodec->maxFramerate);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000141 return VCM_OK;
142}
143
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000144// Register an external decoder object.
145// This can not be used together with external decoder callbacks.
Peter Boström795dbe42015-11-27 14:09:07 +0100146void VideoSender::RegisterExternalEncoder(VideoEncoder* externalEncoder,
philipel5908c712015-12-21 08:23:20 -0800147 uint8_t payloadType,
148 bool internalSource /*= false*/) {
perkj4e417b22016-07-14 23:35:55 -0700149 RTC_DCHECK(sequenced_checker_.CalledSequentially());
tommi@webrtc.org558dc402015-03-07 20:55:56 +0000150
Peter Boström233bfd22016-01-18 20:23:40 +0100151 rtc::CritScope lock(&encoder_crit_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000152
mflodmanfcf54bd2015-04-14 21:28:08 +0200153 if (externalEncoder == nullptr) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000154 bool wasSendCodec = false;
Peter Boström795dbe42015-11-27 14:09:07 +0100155 RTC_CHECK(
156 _codecDataBase.DeregisterExternalEncoder(payloadType, &wasSendCodec));
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000157 if (wasSendCodec) {
158 // Make sure the VCM doesn't use the de-registered codec
Peter Boström233bfd22016-01-18 20:23:40 +0100159 rtc::CritScope params_lock(&params_crit_);
mflodmanfcf54bd2015-04-14 21:28:08 +0200160 _encoder = nullptr;
Peter Boström233bfd22016-01-18 20:23:40 +0100161 encoder_has_internal_source_ = false;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000162 }
Peter Boström795dbe42015-11-27 14:09:07 +0100163 return;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000164 }
philipel5908c712015-12-21 08:23:20 -0800165 _codecDataBase.RegisterExternalEncoder(externalEncoder, payloadType,
166 internalSource);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000167}
168
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000169// Get encode bitrate
170int VideoSender::Bitrate(unsigned int* bitrate) const {
perkj4e417b22016-07-14 23:35:55 -0700171 RTC_DCHECK(sequenced_checker_.CalledSequentially());
tommi@webrtc.org558dc402015-03-07 20:55:56 +0000172 // Since we're running on the thread that's the only thread known to modify
173 // the value of _encoder, we don't need to grab the lock here.
174
Peter Boström69ccb332015-10-29 16:30:23 +0100175 if (!_encoder)
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000176 return VCM_UNINITIALIZED;
Erik Språng08127a92016-11-16 16:41:30 +0100177 *bitrate = _encoder->GetEncoderParameters().target_bitrate.get_sum_bps();
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000178 return 0;
179}
180
181// Get encode frame rate
182int VideoSender::FrameRate(unsigned int* framerate) const {
perkj4e417b22016-07-14 23:35:55 -0700183 RTC_DCHECK(sequenced_checker_.CalledSequentially());
tommi@webrtc.org558dc402015-03-07 20:55:56 +0000184 // Since we're running on the thread that's the only thread known to modify
185 // the value of _encoder, we don't need to grab the lock here.
186
Peter Boström69ccb332015-10-29 16:30:23 +0100187 if (!_encoder)
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000188 return VCM_UNINITIALIZED;
Peter Boström69ccb332015-10-29 16:30:23 +0100189
190 *framerate = _encoder->GetEncoderParameters().input_frame_rate;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000191 return 0;
192}
193
Erik Språng08127a92016-11-16 16:41:30 +0100194EncoderParameters VideoSender::UpdateEncoderParameters(
195 const EncoderParameters& params,
196 VideoBitrateAllocator* bitrate_allocator,
197 uint32_t target_bitrate_bps) {
asapersson9abd2752016-12-08 02:19:40 -0800198 uint32_t video_target_rate_bps = _mediaOpt.SetTargetRates(target_bitrate_bps);
tommi@webrtc.org558dc402015-03-07 20:55:56 +0000199 uint32_t input_frame_rate = _mediaOpt.InputFrameRate();
sprang40217c32016-11-21 05:41:52 -0800200 if (input_frame_rate == 0)
201 input_frame_rate = current_codec_.maxFramerate;
202
Erik Språng08127a92016-11-16 16:41:30 +0100203 BitrateAllocation bitrate_allocation;
sprang4847ae62017-06-27 07:06:52 -0700204 // Only call allocators if bitrate > 0 (ie, not suspended), otherwise they
205 // might cap the bitrate to the min bitrate configured.
206 if (target_bitrate_bps > 0) {
207 if (bitrate_allocator) {
208 bitrate_allocation = bitrate_allocator->GetAllocation(
209 video_target_rate_bps, input_frame_rate);
210 } else {
211 DefaultVideoBitrateAllocator default_allocator(current_codec_);
212 bitrate_allocation = default_allocator.GetAllocation(
213 video_target_rate_bps, input_frame_rate);
214 }
Erik Språng08127a92016-11-16 16:41:30 +0100215 }
Erik Språng08127a92016-11-16 16:41:30 +0100216 EncoderParameters new_encoder_params = {bitrate_allocation, params.loss_rate,
217 params.rtt, input_frame_rate};
218 return new_encoder_params;
219}
220
221void VideoSender::UpdateChannelParemeters(
sprang1a646ee2016-12-01 06:34:11 -0800222 VideoBitrateAllocator* bitrate_allocator,
223 VideoBitrateAllocationObserver* bitrate_updated_callback) {
224 BitrateAllocation target_rate;
225 {
226 rtc::CritScope cs(&params_crit_);
227 encoder_params_ =
228 UpdateEncoderParameters(encoder_params_, bitrate_allocator,
229 encoder_params_.target_bitrate.get_sum_bps());
230 target_rate = encoder_params_.target_bitrate;
231 }
sprang4847ae62017-06-27 07:06:52 -0700232 if (bitrate_updated_callback && target_rate.get_sum_bps() > 0)
sprang1a646ee2016-12-01 06:34:11 -0800233 bitrate_updated_callback->OnBitrateAllocationUpdated(target_rate);
Erik Språng08127a92016-11-16 16:41:30 +0100234}
235
236int32_t VideoSender::SetChannelParameters(
237 uint32_t target_bitrate_bps,
sprang1a646ee2016-12-01 06:34:11 -0800238 uint8_t loss_rate,
Erik Språng08127a92016-11-16 16:41:30 +0100239 int64_t rtt,
sprang1a646ee2016-12-01 06:34:11 -0800240 VideoBitrateAllocator* bitrate_allocator,
241 VideoBitrateAllocationObserver* bitrate_updated_callback) {
Erik Språng08127a92016-11-16 16:41:30 +0100242 EncoderParameters encoder_params;
sprang1a646ee2016-12-01 06:34:11 -0800243 encoder_params.loss_rate = loss_rate;
Erik Språng08127a92016-11-16 16:41:30 +0100244 encoder_params.rtt = rtt;
245 encoder_params = UpdateEncoderParameters(encoder_params, bitrate_allocator,
246 target_bitrate_bps);
sprang4847ae62017-06-27 07:06:52 -0700247 if (bitrate_updated_callback && target_bitrate_bps > 0) {
sprang1a646ee2016-12-01 06:34:11 -0800248 bitrate_updated_callback->OnBitrateAllocationUpdated(
249 encoder_params.target_bitrate);
250 }
Erik Språng08127a92016-11-16 16:41:30 +0100251
noahricd4badbc2016-04-13 14:59:48 -0700252 bool encoder_has_internal_source;
253 {
254 rtc::CritScope cs(&params_crit_);
255 encoder_params_ = encoder_params;
256 encoder_has_internal_source = encoder_has_internal_source_;
257 }
258
259 // For encoders with internal sources, we need to tell the encoder directly,
260 // instead of waiting for an AddVideoFrame that will never come (internal
261 // source encoders don't get input frames).
262 if (encoder_has_internal_source) {
263 rtc::CritScope cs(&encoder_crit_);
264 if (_encoder) {
perkj57c21f92016-06-17 07:27:16 -0700265 SetEncoderParameters(encoder_params, encoder_has_internal_source);
noahricd4badbc2016-04-13 14:59:48 -0700266 }
267 }
Erik Språng66a641a2015-06-11 14:20:07 +0200268
269 return VCM_OK;
270}
271
perkj57c21f92016-06-17 07:27:16 -0700272void VideoSender::SetEncoderParameters(EncoderParameters params,
273 bool has_internal_source) {
perkjfea93092016-05-14 00:58:48 -0700274 // |target_bitrate == 0 | means that the network is down or the send pacer is
perkj57c21f92016-06-17 07:27:16 -0700275 // full. We currently only report this if the encoder has an internal source.
276 // If the encoder does not have an internal source, higher levels are expected
277 // to not call AddVideoFrame. We do this since its unclear how current
278 // encoder implementations behave when given a zero target bitrate.
279 // TODO(perkj): Make sure all known encoder implementations handle zero
280 // target bitrate and remove this check.
Erik Språng08127a92016-11-16 16:41:30 +0100281 if (!has_internal_source && params.target_bitrate.get_sum_bps() == 0)
Peter Boströmdcb89982015-09-15 14:43:47 +0200282 return;
tommi@webrtc.org558dc402015-03-07 20:55:56 +0000283
Erik Språng66a641a2015-06-11 14:20:07 +0200284 if (params.input_frame_rate == 0) {
285 // No frame rate estimate available, use default.
286 params.input_frame_rate = current_codec_.maxFramerate;
287 }
Peter Boström69ccb332015-10-29 16:30:23 +0100288 if (_encoder != nullptr)
289 _encoder->SetEncoderParameters(params);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000290}
291
Per69b332d2016-06-02 15:45:42 +0200292// Deprecated:
293// TODO(perkj): Remove once no projects call this method. It currently do
294// nothing.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000295int32_t VideoSender::RegisterProtectionCallback(
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000296 VCMProtectionCallback* protection_callback) {
Per69b332d2016-06-02 15:45:42 +0200297 // Deprecated:
298 // TODO(perkj): Remove once no projects call this method. It currently do
299 // nothing.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000300 return VCM_OK;
301}
302
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000303// Add one raw video frame to the encoder, blocking.
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700304int32_t VideoSender::AddVideoFrame(const VideoFrame& videoFrame,
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000305 const CodecSpecificInfo* codecSpecificInfo) {
Peter Boströmdcb89982015-09-15 14:43:47 +0200306 EncoderParameters encoder_params;
Peter Boström233bfd22016-01-18 20:23:40 +0100307 std::vector<FrameType> next_frame_types;
perkj57c21f92016-06-17 07:27:16 -0700308 bool encoder_has_internal_source = false;
Peter Boströmdcb89982015-09-15 14:43:47 +0200309 {
Peter Boström233bfd22016-01-18 20:23:40 +0100310 rtc::CritScope lock(&params_crit_);
Peter Boströmdcb89982015-09-15 14:43:47 +0200311 encoder_params = encoder_params_;
Peter Boström233bfd22016-01-18 20:23:40 +0100312 next_frame_types = next_frame_types_;
perkj57c21f92016-06-17 07:27:16 -0700313 encoder_has_internal_source = encoder_has_internal_source_;
Peter Boströmdcb89982015-09-15 14:43:47 +0200314 }
Peter Boström233bfd22016-01-18 20:23:40 +0100315 rtc::CritScope lock(&encoder_crit_);
Peter Boström69ccb332015-10-29 16:30:23 +0100316 if (_encoder == nullptr)
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000317 return VCM_UNINITIALIZED;
perkj57c21f92016-06-17 07:27:16 -0700318 SetEncoderParameters(encoder_params, encoder_has_internal_source);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000319 if (_mediaOpt.DropFrame()) {
isheriff7620be82016-03-06 23:22:33 -0800320 LOG(LS_VERBOSE) << "Drop Frame "
Erik Språng08127a92016-11-16 16:41:30 +0100321 << "target bitrate "
322 << encoder_params.target_bitrate.get_sum_bps()
isheriff7620be82016-03-06 23:22:33 -0800323 << " loss rate " << encoder_params.loss_rate << " rtt "
324 << encoder_params.rtt << " input frame rate "
325 << encoder_params.input_frame_rate;
kthelgason876222f2016-11-29 01:44:11 -0800326 post_encode_callback_->OnDroppedFrame();
stefan@webrtc.org34c5da62014-04-11 14:08:35 +0000327 return VCM_OK;
328 }
pbos@webrtc.org67a9e402015-03-05 13:57:37 +0000329 // TODO(pbos): Make sure setting send codec is synchronized with video
330 // processing so frame size always matches.
331 if (!_codecDataBase.MatchesCurrentResolution(videoFrame.width(),
332 videoFrame.height())) {
333 LOG(LS_ERROR) << "Incoming frame doesn't match set resolution. Dropping.";
334 return VCM_PARAMETER_ERROR;
335 }
Peter Boströmeb66e802015-06-05 11:08:03 +0200336 VideoFrame converted_frame = videoFrame;
Magnus Jedvert72dbe2a2017-06-10 17:03:37 +0000337 const VideoFrameBuffer::Type buffer_type =
338 converted_frame.video_frame_buffer()->type();
339 const bool is_buffer_type_supported =
340 buffer_type == VideoFrameBuffer::Type::kI420 ||
341 (buffer_type == VideoFrameBuffer::Type::kNative &&
342 _encoder->SupportsNativeHandle());
343 if (!is_buffer_type_supported) {
Peter Boströmeb66e802015-06-05 11:08:03 +0200344 // This module only supports software encoding.
345 // TODO(pbos): Offload conversion from the encoder thread.
Magnus Jedvert72dbe2a2017-06-10 17:03:37 +0000346 rtc::scoped_refptr<I420BufferInterface> converted_buffer(
347 converted_frame.video_frame_buffer()->ToI420());
nisseca6d5d12016-06-17 05:03:04 -0700348
349 if (!converted_buffer) {
350 LOG(LS_ERROR) << "Frame conversion failed, dropping frame.";
351 return VCM_PARAMETER_ERROR;
352 }
353 converted_frame = VideoFrame(converted_buffer,
354 converted_frame.timestamp(),
355 converted_frame.render_time_ms(),
356 converted_frame.rotation());
Peter Boströmeb66e802015-06-05 11:08:03 +0200357 }
stefan@webrtc.org34c5da62014-04-11 14:08:35 +0000358 int32_t ret =
Peter Boström233bfd22016-01-18 20:23:40 +0100359 _encoder->Encode(converted_frame, codecSpecificInfo, next_frame_types);
stefan@webrtc.org34c5da62014-04-11 14:08:35 +0000360 if (ret < 0) {
361 LOG(LS_ERROR) << "Failed to encode frame. Error code: " << ret;
362 return ret;
363 }
perkj376b1922016-05-02 11:35:24 -0700364
Peter Boström233bfd22016-01-18 20:23:40 +0100365 {
Peter Boström233bfd22016-01-18 20:23:40 +0100366 rtc::CritScope lock(&params_crit_);
perkj376b1922016-05-02 11:35:24 -0700367 // Change all keyframe requests to encode delta frames the next time.
Peter Boström233bfd22016-01-18 20:23:40 +0100368 for (size_t i = 0; i < next_frame_types_.size(); ++i) {
369 // Check for equality (same requested as before encoding) to not
370 // accidentally drop a keyframe request while encoding.
371 if (next_frame_types[i] == next_frame_types_[i])
372 next_frame_types_[i] = kVideoFrameDelta;
373 }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000374 }
375 return VCM_OK;
376}
377
perkj600246e2016-05-04 11:26:51 -0700378int32_t VideoSender::IntraFrameRequest(size_t stream_index) {
Peter Boström233bfd22016-01-18 20:23:40 +0100379 {
380 rtc::CritScope lock(&params_crit_);
perkj600246e2016-05-04 11:26:51 -0700381 if (stream_index >= next_frame_types_.size()) {
Peter Boström233bfd22016-01-18 20:23:40 +0100382 return -1;
383 }
384 next_frame_types_[stream_index] = kVideoFrameKey;
385 if (!encoder_has_internal_source_)
386 return VCM_OK;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000387 }
Peter Boström233bfd22016-01-18 20:23:40 +0100388 // TODO(pbos): Remove when InternalSource() is gone. Both locks have to be
389 // held here for internal consistency, since _encoder could be removed while
390 // not holding encoder_crit_. Checks have to be performed again since
391 // params_crit_ was dropped to not cause lock-order inversions with
392 // encoder_crit_.
393 rtc::CritScope lock(&encoder_crit_);
394 rtc::CritScope params_lock(&params_crit_);
perkj600246e2016-05-04 11:26:51 -0700395 if (stream_index >= next_frame_types_.size())
Peter Boström233bfd22016-01-18 20:23:40 +0100396 return -1;
mflodmanfcf54bd2015-04-14 21:28:08 +0200397 if (_encoder != nullptr && _encoder->InternalSource()) {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000398 // Try to request the frame if we have an external encoder with
399 // internal source since AddVideoFrame never will be called.
Peter Boström233bfd22016-01-18 20:23:40 +0100400 if (_encoder->RequestFrame(next_frame_types_) == WEBRTC_VIDEO_CODEC_OK) {
401 // Try to remove just-performed keyframe request, if stream still exists.
402 next_frame_types_[stream_index] = kVideoFrameDelta;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000403 }
404 }
405 return VCM_OK;
406}
407
408int32_t VideoSender::EnableFrameDropper(bool enable) {
Peter Boström233bfd22016-01-18 20:23:40 +0100409 rtc::CritScope lock(&encoder_crit_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000410 frame_dropper_enabled_ = enable;
411 _mediaOpt.EnableFrameDropper(enable);
412 return VCM_OK;
413}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000414} // namespace vcm
415} // namespace webrtc