blob: 0c507b683aba89d6fa8e4935d25dbe8024db633c [file] [log] [blame]
pbos@webrtc.org9115cde2014-12-09 10:36:40 +00001/*
2 * Copyright (c) 2014 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "media/engine/simulcast_encoder_adapter.h"
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000012
13#include <algorithm>
Ilya Nikolaevskiyc30a1312018-08-27 11:07:48 +020014#include <string>
15#include <utility>
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/video/i420_buffer.h"
Erik Språng7fd0a282018-05-22 15:37:23 +020018#include "api/video/video_bitrate_allocation.h"
Magnus Jedvertdf4883d2017-11-17 14:44:55 +010019#include "api/video_codecs/video_encoder_factory.h"
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +020020#include "modules/video_coding/utility/simulcast_rate_allocator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/checks.h"
22#include "system_wrappers/include/clock.h"
Ilya Nikolaevskiyc30a1312018-08-27 11:07:48 +020023#include "system_wrappers/include/field_trial.h"
Mirko Bonadei65432062017-12-11 09:32:13 +010024#include "third_party/libyuv/include/libyuv/scale.h"
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000025
26namespace {
27
28const unsigned int kDefaultMinQp = 2;
29const unsigned int kDefaultMaxQp = 56;
30// Max qp for lowest spatial resolution when doing simulcast.
31const unsigned int kLowestResMaxQp = 45;
32
Ilya Nikolaevskiyc30a1312018-08-27 11:07:48 +020033absl::optional<unsigned int> GetScreenshareBoostedQpValue() {
34 std::string experiment_group =
35 webrtc::field_trial::FindFullName("WebRTC-BoostedScreenshareQp");
36 unsigned int qp;
37 if (sscanf(experiment_group.c_str(), "%u", &qp) != 1)
38 return absl::nullopt;
39 qp = std::min(qp, 63u);
40 qp = std::max(qp, 1u);
41 return qp;
42}
43
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000044uint32_t SumStreamMaxBitrate(int streams, const webrtc::VideoCodec& codec) {
45 uint32_t bitrate_sum = 0;
46 for (int i = 0; i < streams; ++i) {
47 bitrate_sum += codec.simulcastStream[i].maxBitrate;
48 }
49 return bitrate_sum;
50}
51
52int NumberOfStreams(const webrtc::VideoCodec& codec) {
53 int streams =
54 codec.numberOfSimulcastStreams < 1 ? 1 : codec.numberOfSimulcastStreams;
55 uint32_t simulcast_max_bitrate = SumStreamMaxBitrate(streams, codec);
56 if (simulcast_max_bitrate == 0) {
57 streams = 1;
58 }
59 return streams;
60}
61
62bool ValidSimulcastResolutions(const webrtc::VideoCodec& codec,
63 int num_streams) {
64 if (codec.width != codec.simulcastStream[num_streams - 1].width ||
65 codec.height != codec.simulcastStream[num_streams - 1].height) {
66 return false;
67 }
68 for (int i = 0; i < num_streams; ++i) {
69 if (codec.width * codec.simulcastStream[i].height !=
70 codec.height * codec.simulcastStream[i].width) {
71 return false;
72 }
73 }
74 return true;
75}
76
77int VerifyCodec(const webrtc::VideoCodec* inst) {
brandtr5e171752017-05-23 03:32:16 -070078 if (inst == nullptr) {
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000079 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
80 }
81 if (inst->maxFramerate < 1) {
82 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
83 }
84 // allow zero to represent an unspecified maxBitRate
85 if (inst->maxBitrate > 0 && inst->startBitrate > inst->maxBitrate) {
86 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
87 }
88 if (inst->width <= 1 || inst->height <= 1) {
89 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
90 }
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +020091 if (inst->codecType == webrtc::kVideoCodecVP8 &&
92 inst->VP8().automaticResizeOn && inst->numberOfSimulcastStreams > 1) {
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000093 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
94 }
95 return WEBRTC_VIDEO_CODEC_OK;
96}
97
Noah Richards41ee1ea2015-04-15 09:24:26 -070098// An EncodedImageCallback implementation that forwards on calls to a
99// SimulcastEncoderAdapter, but with the stream index it's registered with as
100// the first parameter to Encoded.
101class AdapterEncodedImageCallback : public webrtc::EncodedImageCallback {
102 public:
103 AdapterEncodedImageCallback(webrtc::SimulcastEncoderAdapter* adapter,
104 size_t stream_idx)
105 : adapter_(adapter), stream_idx_(stream_idx) {}
106
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700107 EncodedImageCallback::Result OnEncodedImage(
108 const webrtc::EncodedImage& encoded_image,
109 const webrtc::CodecSpecificInfo* codec_specific_info,
110 const webrtc::RTPFragmentationHeader* fragmentation) override {
111 return adapter_->OnEncodedImage(stream_idx_, encoded_image,
112 codec_specific_info, fragmentation);
Noah Richards41ee1ea2015-04-15 09:24:26 -0700113 }
114
115 private:
116 webrtc::SimulcastEncoderAdapter* const adapter_;
117 const size_t stream_idx_;
118};
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000119} // namespace
120
121namespace webrtc {
122
Ilya Nikolaevskiy97b4ee52018-05-28 10:24:22 +0200123SimulcastEncoderAdapter::SimulcastEncoderAdapter(VideoEncoderFactory* factory,
124 const SdpVideoFormat& format)
Magnus Jedvertdf4883d2017-11-17 14:44:55 +0100125 : inited_(0),
126 factory_(factory),
Ilya Nikolaevskiy97b4ee52018-05-28 10:24:22 +0200127 video_format_(format),
Magnus Jedvertdf4883d2017-11-17 14:44:55 +0100128 encoded_complete_callback_(nullptr),
Ilya Nikolaevskiyc30a1312018-08-27 11:07:48 +0200129 implementation_name_("SimulcastEncoderAdapter"),
Oleh Prypina1d9ca42018-10-11 14:33:39 +0000130 experimental_boosted_screenshare_qp_(GetScreenshareBoostedQpValue()) {
Rasmus Brandtc334ce92018-02-05 13:03:25 +0100131 RTC_DCHECK(factory_);
Magnus Jedvertdf4883d2017-11-17 14:44:55 +0100132
brandtr5e171752017-05-23 03:32:16 -0700133 // The adapter is typically created on the worker thread, but operated on
134 // the encoder task queue.
135 encoder_queue_.Detach();
136
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000137 memset(&codec_, 0, sizeof(webrtc::VideoCodec));
138}
139
140SimulcastEncoderAdapter::~SimulcastEncoderAdapter() {
brandtr5e171752017-05-23 03:32:16 -0700141 RTC_DCHECK(!Initialized());
142 DestroyStoredEncoders();
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000143}
144
145int SimulcastEncoderAdapter::Release() {
brandtr5e171752017-05-23 03:32:16 -0700146 RTC_DCHECK_CALLED_SEQUENTIALLY(&encoder_queue_);
147
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000148 while (!streaminfos_.empty()) {
magjed3f897582017-08-28 08:05:42 -0700149 std::unique_ptr<VideoEncoder> encoder =
150 std::move(streaminfos_.back().encoder);
brandtr5e171752017-05-23 03:32:16 -0700151 // Even though it seems very unlikely, there are no guarantees that the
Rasmus Brandt9cf9f752017-09-28 13:02:58 +0200152 // encoder will not call back after being Release()'d. Therefore, we first
153 // disable the callbacks here.
brandtr5e171752017-05-23 03:32:16 -0700154 encoder->RegisterEncodeCompleteCallback(nullptr);
Rasmus Brandt9cf9f752017-09-28 13:02:58 +0200155 encoder->Release();
brandtr5e171752017-05-23 03:32:16 -0700156 streaminfos_.pop_back(); // Deletes callback adapter.
magjed3f897582017-08-28 08:05:42 -0700157 stored_encoders_.push(std::move(encoder));
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000158 }
brandtr5e171752017-05-23 03:32:16 -0700159
160 // It's legal to move the encoder to another queue now.
161 encoder_queue_.Detach();
162
163 rtc::AtomicOps::ReleaseStore(&inited_, 0);
164
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000165 return WEBRTC_VIDEO_CODEC_OK;
166}
167
168int SimulcastEncoderAdapter::InitEncode(const VideoCodec* inst,
169 int number_of_cores,
170 size_t max_payload_size) {
brandtr5e171752017-05-23 03:32:16 -0700171 RTC_DCHECK_CALLED_SEQUENTIALLY(&encoder_queue_);
172
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000173 if (number_of_cores < 1) {
174 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
175 }
176
177 int ret = VerifyCodec(inst);
178 if (ret < 0) {
179 return ret;
180 }
181
182 ret = Release();
183 if (ret < 0) {
184 return ret;
185 }
186
187 int number_of_streams = NumberOfStreams(*inst);
brandtr5e171752017-05-23 03:32:16 -0700188 RTC_DCHECK_LE(number_of_streams, kMaxSimulcastStreams);
Peter Boströmd53c3892016-03-30 17:03:52 +0200189 const bool doing_simulcast = (number_of_streams > 1);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000190
191 if (doing_simulcast && !ValidSimulcastResolutions(*inst, number_of_streams)) {
192 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
193 }
194
195 codec_ = *inst;
Erik Språng82fad3d2018-03-21 09:57:23 +0100196 SimulcastRateAllocator rate_allocator(codec_);
Erik Språng566124a2018-04-23 12:32:22 +0200197 VideoBitrateAllocation allocation = rate_allocator.GetAllocation(
Erik Språng08127a92016-11-16 16:41:30 +0100198 codec_.startBitrate * 1000, codec_.maxFramerate);
199 std::vector<uint32_t> start_bitrates;
200 for (int i = 0; i < kMaxSimulcastStreams; ++i) {
201 uint32_t stream_bitrate = allocation.GetSpatialLayerSum(i) / 1000;
202 start_bitrates.push_back(stream_bitrate);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000203 }
204
Peter Boströma5dec162016-01-20 15:53:55 +0100205 std::string implementation_name;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000206 // Create |number_of_streams| of encoder instances and init them.
207 for (int i = 0; i < number_of_streams; ++i) {
208 VideoCodec stream_codec;
Erik Språng78ce6192016-09-12 16:04:43 +0200209 uint32_t start_bitrate_kbps = start_bitrates[i];
Erik Språng7d687b12018-09-12 17:04:10 +0200210 const bool send_stream = start_bitrate_kbps > 0;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000211 if (!doing_simulcast) {
212 stream_codec = codec_;
213 stream_codec.numberOfSimulcastStreams = 1;
214 } else {
Erik Språng78ce6192016-09-12 16:04:43 +0200215 // Cap start bitrate to the min bitrate in order to avoid strange codec
Erik Språng7d687b12018-09-12 17:04:10 +0200216 // behavior. Since sending will be false, this should not matter.
Erik Språng78ce6192016-09-12 16:04:43 +0200217 start_bitrate_kbps =
218 std::max(codec_.simulcastStream[i].minBitrate, start_bitrate_kbps);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000219 bool highest_resolution_stream = (i == (number_of_streams - 1));
brandtr5e171752017-05-23 03:32:16 -0700220 PopulateStreamCodec(codec_, i, start_bitrate_kbps,
Erik Språng78ce6192016-09-12 16:04:43 +0200221 highest_resolution_stream, &stream_codec);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000222 }
223
Erik Språngcc681cc2018-03-14 17:52:55 +0100224 // TODO(ronghuawu): Remove once this is handled in LibvpxVp8Encoder.
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000225 if (stream_codec.qpMax < kDefaultMinQp) {
226 stream_codec.qpMax = kDefaultMaxQp;
227 }
228
brandtr5e171752017-05-23 03:32:16 -0700229 // If an existing encoder instance exists, reuse it.
230 // TODO(brandtr): Set initial RTP state (e.g., picture_id/tl0_pic_idx) here,
231 // when we start storing that state outside the encoder wrappers.
magjed3f897582017-08-28 08:05:42 -0700232 std::unique_ptr<VideoEncoder> encoder;
brandtr5e171752017-05-23 03:32:16 -0700233 if (!stored_encoders_.empty()) {
magjed3f897582017-08-28 08:05:42 -0700234 encoder = std::move(stored_encoders_.top());
brandtr5e171752017-05-23 03:32:16 -0700235 stored_encoders_.pop();
236 } else {
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200237 encoder = factory_->CreateVideoEncoder(SdpVideoFormat(
238 codec_.codecType == webrtc::kVideoCodecVP8 ? "VP8" : "H264"));
brandtr5e171752017-05-23 03:32:16 -0700239 }
240
philipelcce46fc2015-12-21 03:04:49 -0800241 ret = encoder->InitEncode(&stream_codec, number_of_cores, max_payload_size);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000242 if (ret < 0) {
noahrice5ba75a2016-12-12 13:08:27 -0800243 // Explicitly destroy the current encoder; because we haven't registered a
244 // StreamInfo for it yet, Release won't do anything about it.
magjed3f897582017-08-28 08:05:42 -0700245 encoder.reset();
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000246 Release();
247 return ret;
248 }
brandtr5e171752017-05-23 03:32:16 -0700249 std::unique_ptr<EncodedImageCallback> callback(
250 new AdapterEncodedImageCallback(this, i));
251 encoder->RegisterEncodeCompleteCallback(callback.get());
magjed3f897582017-08-28 08:05:42 -0700252 streaminfos_.emplace_back(std::move(encoder), std::move(callback),
253 stream_codec.width, stream_codec.height,
Erik Språng7d687b12018-09-12 17:04:10 +0200254 send_stream);
brandtr5e171752017-05-23 03:32:16 -0700255
256 if (i != 0) {
Peter Boströma5dec162016-01-20 15:53:55 +0100257 implementation_name += ", ";
brandtr5e171752017-05-23 03:32:16 -0700258 }
Erik Språnge2fd86a2018-10-24 11:32:39 +0200259 implementation_name +=
260 streaminfos_[i].encoder->GetEncoderInfo().implementation_name;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000261 }
brandtr5e171752017-05-23 03:32:16 -0700262
Peter Boströmd53c3892016-03-30 17:03:52 +0200263 if (doing_simulcast) {
264 implementation_name_ =
265 "SimulcastEncoderAdapter (" + implementation_name + ")";
266 } else {
267 implementation_name_ = implementation_name;
268 }
brandtr5e171752017-05-23 03:32:16 -0700269
270 // To save memory, don't store encoders that we don't use.
271 DestroyStoredEncoders();
272
273 rtc::AtomicOps::ReleaseStore(&inited_, 1);
274
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000275 return WEBRTC_VIDEO_CODEC_OK;
276}
277
278int SimulcastEncoderAdapter::Encode(
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700279 const VideoFrame& input_image,
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000280 const CodecSpecificInfo* codec_specific_info,
pbos22993e12015-10-19 02:39:06 -0700281 const std::vector<FrameType>* frame_types) {
brandtr5e171752017-05-23 03:32:16 -0700282 RTC_DCHECK_CALLED_SEQUENTIALLY(&encoder_queue_);
283
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000284 if (!Initialized()) {
285 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
286 }
brandtr5e171752017-05-23 03:32:16 -0700287 if (encoded_complete_callback_ == nullptr) {
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000288 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
289 }
290
291 // All active streams should generate a key frame if
292 // a key frame is requested by any stream.
293 bool send_key_frame = false;
294 if (frame_types) {
295 for (size_t i = 0; i < frame_types->size(); ++i) {
Peter Boström49e196a2015-10-23 15:58:18 +0200296 if (frame_types->at(i) == kVideoFrameKey) {
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000297 send_key_frame = true;
298 break;
299 }
300 }
301 }
302 for (size_t stream_idx = 0; stream_idx < streaminfos_.size(); ++stream_idx) {
303 if (streaminfos_[stream_idx].key_frame_request &&
304 streaminfos_[stream_idx].send_stream) {
305 send_key_frame = true;
306 break;
307 }
308 }
309
310 int src_width = input_image.width();
311 int src_height = input_image.height();
312 for (size_t stream_idx = 0; stream_idx < streaminfos_.size(); ++stream_idx) {
Peter Boström5d0379d2015-10-06 14:04:51 +0200313 // Don't encode frames in resolutions that we don't intend to send.
brandtr5e171752017-05-23 03:32:16 -0700314 if (!streaminfos_[stream_idx].send_stream) {
Peter Boström5d0379d2015-10-06 14:04:51 +0200315 continue;
brandtr5e171752017-05-23 03:32:16 -0700316 }
Peter Boström5d0379d2015-10-06 14:04:51 +0200317
pbos22993e12015-10-19 02:39:06 -0700318 std::vector<FrameType> stream_frame_types;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000319 if (send_key_frame) {
Peter Boström49e196a2015-10-23 15:58:18 +0200320 stream_frame_types.push_back(kVideoFrameKey);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000321 streaminfos_[stream_idx].key_frame_request = false;
322 } else {
Peter Boström49e196a2015-10-23 15:58:18 +0200323 stream_frame_types.push_back(kVideoFrameDelta);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000324 }
325
326 int dst_width = streaminfos_[stream_idx].width;
327 int dst_height = streaminfos_[stream_idx].height;
328 // If scaling isn't required, because the input resolution
329 // matches the destination or the input image is empty (e.g.
330 // a keyframe request for encoders with internal camera
noahricfe3654d2016-07-01 09:05:54 -0700331 // sources) or the source image has a native handle, pass the image on
332 // directly. Otherwise, we'll scale it to match what the encoder expects
333 // (below).
334 // For texture frames, the underlying encoder is expected to be able to
335 // correctly sample/scale the source texture.
336 // TODO(perkj): ensure that works going forward, and figure out how this
337 // affects webrtc:5683.
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000338 if ((dst_width == src_width && dst_height == src_height) ||
Magnus Jedvert72dbe2a2017-06-10 17:03:37 +0000339 input_image.video_frame_buffer()->type() ==
340 VideoFrameBuffer::Type::kNative) {
noahric57779102016-05-25 06:48:46 -0700341 int ret = streaminfos_[stream_idx].encoder->Encode(
342 input_image, codec_specific_info, &stream_frame_types);
343 if (ret != WEBRTC_VIDEO_CODEC_OK) {
344 return ret;
345 }
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000346 } else {
nisse64ec8f82016-09-27 00:17:25 -0700347 rtc::scoped_refptr<I420Buffer> dst_buffer =
Magnus Jedvert72dbe2a2017-06-10 17:03:37 +0000348 I420Buffer::Create(dst_width, dst_height);
349 rtc::scoped_refptr<I420BufferInterface> src_buffer =
350 input_image.video_frame_buffer()->ToI420();
351 libyuv::I420Scale(src_buffer->DataY(), src_buffer->StrideY(),
352 src_buffer->DataU(), src_buffer->StrideU(),
353 src_buffer->DataV(), src_buffer->StrideV(), src_width,
354 src_height, dst_buffer->MutableDataY(),
355 dst_buffer->StrideY(), dst_buffer->MutableDataU(),
356 dst_buffer->StrideU(), dst_buffer->MutableDataV(),
357 dst_buffer->StrideV(), dst_width, dst_height,
nissec9c142f2016-05-17 04:05:47 -0700358 libyuv::kFilterBilinear);
nisse64ec8f82016-09-27 00:17:25 -0700359
noahric57779102016-05-25 06:48:46 -0700360 int ret = streaminfos_[stream_idx].encoder->Encode(
nisse64ec8f82016-09-27 00:17:25 -0700361 VideoFrame(dst_buffer, input_image.timestamp(),
362 input_image.render_time_ms(), webrtc::kVideoRotation_0),
363 codec_specific_info, &stream_frame_types);
noahric57779102016-05-25 06:48:46 -0700364 if (ret != WEBRTC_VIDEO_CODEC_OK) {
365 return ret;
366 }
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000367 }
368 }
369
370 return WEBRTC_VIDEO_CODEC_OK;
371}
372
373int SimulcastEncoderAdapter::RegisterEncodeCompleteCallback(
374 EncodedImageCallback* callback) {
brandtr5e171752017-05-23 03:32:16 -0700375 RTC_DCHECK_CALLED_SEQUENTIALLY(&encoder_queue_);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000376 encoded_complete_callback_ = callback;
377 return WEBRTC_VIDEO_CODEC_OK;
378}
379
380int SimulcastEncoderAdapter::SetChannelParameters(uint32_t packet_loss,
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000381 int64_t rtt) {
brandtr5e171752017-05-23 03:32:16 -0700382 RTC_DCHECK_CALLED_SEQUENTIALLY(&encoder_queue_);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000383 for (size_t stream_idx = 0; stream_idx < streaminfos_.size(); ++stream_idx) {
384 streaminfos_[stream_idx].encoder->SetChannelParameters(packet_loss, rtt);
385 }
386 return WEBRTC_VIDEO_CODEC_OK;
387}
388
Erik Språng566124a2018-04-23 12:32:22 +0200389int SimulcastEncoderAdapter::SetRateAllocation(
390 const VideoBitrateAllocation& bitrate,
391 uint32_t new_framerate) {
brandtr5e171752017-05-23 03:32:16 -0700392 RTC_DCHECK_CALLED_SEQUENTIALLY(&encoder_queue_);
393
394 if (!Initialized()) {
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000395 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
brandtr5e171752017-05-23 03:32:16 -0700396 }
sprang647bf432016-11-10 06:46:20 -0800397
brandtr5e171752017-05-23 03:32:16 -0700398 if (new_framerate < 1) {
Erik Språng08127a92016-11-16 16:41:30 +0100399 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
brandtr5e171752017-05-23 03:32:16 -0700400 }
Erik Språng08127a92016-11-16 16:41:30 +0100401
brandtr5e171752017-05-23 03:32:16 -0700402 if (codec_.maxBitrate > 0 && bitrate.get_sum_kbps() > codec_.maxBitrate) {
Erik Språng08127a92016-11-16 16:41:30 +0100403 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
brandtr5e171752017-05-23 03:32:16 -0700404 }
Erik Språng08127a92016-11-16 16:41:30 +0100405
406 if (bitrate.get_sum_bps() > 0) {
sprang1369c832016-11-10 08:30:33 -0800407 // Make sure the bitrate fits the configured min bitrates. 0 is a special
408 // value that means paused, though, so leave it alone.
brandtr5e171752017-05-23 03:32:16 -0700409 if (bitrate.get_sum_kbps() < codec_.minBitrate) {
Erik Språng08127a92016-11-16 16:41:30 +0100410 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
brandtr5e171752017-05-23 03:32:16 -0700411 }
Erik Språng08127a92016-11-16 16:41:30 +0100412
sprang1369c832016-11-10 08:30:33 -0800413 if (codec_.numberOfSimulcastStreams > 0 &&
Erik Språng08127a92016-11-16 16:41:30 +0100414 bitrate.get_sum_kbps() < codec_.simulcastStream[0].minBitrate) {
415 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
sprang1369c832016-11-10 08:30:33 -0800416 }
sprang1369c832016-11-10 08:30:33 -0800417 }
Erik Språng08127a92016-11-16 16:41:30 +0100418
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000419 codec_.maxFramerate = new_framerate;
420
sprang1369c832016-11-10 08:30:33 -0800421 for (size_t stream_idx = 0; stream_idx < streaminfos_.size(); ++stream_idx) {
Erik Språng08127a92016-11-16 16:41:30 +0100422 uint32_t stream_bitrate_kbps =
423 bitrate.GetSpatialLayerSum(stream_idx) / 1000;
424
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000425 // Need a key frame if we have not sent this stream before.
Erik Språng78ce6192016-09-12 16:04:43 +0200426 if (stream_bitrate_kbps > 0 && !streaminfos_[stream_idx].send_stream) {
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000427 streaminfos_[stream_idx].key_frame_request = true;
428 }
Erik Språng78ce6192016-09-12 16:04:43 +0200429 streaminfos_[stream_idx].send_stream = stream_bitrate_kbps > 0;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000430
Erik Språng08127a92016-11-16 16:41:30 +0100431 // Slice the temporal layers out of the full allocation and pass it on to
432 // the encoder handling the current simulcast stream.
Erik Språng566124a2018-04-23 12:32:22 +0200433 VideoBitrateAllocation stream_allocation;
brandtr5e171752017-05-23 03:32:16 -0700434 for (int i = 0; i < kMaxTemporalStreams; ++i) {
erikvarga@webrtc.org01f2ec32017-11-15 14:58:23 +0100435 if (bitrate.HasBitrate(stream_idx, i)) {
436 stream_allocation.SetBitrate(0, i, bitrate.GetBitrate(stream_idx, i));
437 }
brandtr5e171752017-05-23 03:32:16 -0700438 }
Erik Språng08127a92016-11-16 16:41:30 +0100439 streaminfos_[stream_idx].encoder->SetRateAllocation(stream_allocation,
440 new_framerate);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000441 }
442
443 return WEBRTC_VIDEO_CODEC_OK;
444}
445
brandtr5e171752017-05-23 03:32:16 -0700446// TODO(brandtr): Add task checker to this member function, when all encoder
447// callbacks are coming in on the encoder queue.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700448EncodedImageCallback::Result SimulcastEncoderAdapter::OnEncodedImage(
Noah Richards41ee1ea2015-04-15 09:24:26 -0700449 size_t stream_idx,
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000450 const EncodedImage& encodedImage,
451 const CodecSpecificInfo* codecSpecificInfo,
452 const RTPFragmentationHeader* fragmentation) {
Niels Möllerd3b8c632018-08-27 15:33:42 +0200453 EncodedImage stream_image(encodedImage);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000454 CodecSpecificInfo stream_codec_specific = *codecSpecificInfo;
Niels Möllerd3b8c632018-08-27 15:33:42 +0200455
456 stream_image.SetSpatialIndex(stream_idx);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000457
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700458 return encoded_complete_callback_->OnEncodedImage(
Niels Möllerd3b8c632018-08-27 15:33:42 +0200459 stream_image, &stream_codec_specific, fragmentation);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000460}
461
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000462void SimulcastEncoderAdapter::PopulateStreamCodec(
brandtr5e171752017-05-23 03:32:16 -0700463 const webrtc::VideoCodec& inst,
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000464 int stream_index,
Erik Språng78ce6192016-09-12 16:04:43 +0200465 uint32_t start_bitrate_kbps,
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000466 bool highest_resolution_stream,
Erik Språng78ce6192016-09-12 16:04:43 +0200467 webrtc::VideoCodec* stream_codec) {
brandtr5e171752017-05-23 03:32:16 -0700468 *stream_codec = inst;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000469
470 // Stream specific settings.
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000471 stream_codec->numberOfSimulcastStreams = 0;
brandtr5e171752017-05-23 03:32:16 -0700472 stream_codec->width = inst.simulcastStream[stream_index].width;
473 stream_codec->height = inst.simulcastStream[stream_index].height;
474 stream_codec->maxBitrate = inst.simulcastStream[stream_index].maxBitrate;
475 stream_codec->minBitrate = inst.simulcastStream[stream_index].minBitrate;
476 stream_codec->qpMax = inst.simulcastStream[stream_index].qpMax;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000477 // Settings that are based on stream/resolution.
brandtr5e171752017-05-23 03:32:16 -0700478 const bool lowest_resolution_stream = (stream_index == 0);
Ilya Nikolaevskiyc30a1312018-08-27 11:07:48 +0200479 if (lowest_resolution_stream) {
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000480 // Settings for lowest spatial resolutions.
Ilya Nikolaevskiyc30a1312018-08-27 11:07:48 +0200481 if (inst.mode == VideoCodecMode::kScreensharing) {
482 if (experimental_boosted_screenshare_qp_) {
483 stream_codec->qpMax = *experimental_boosted_screenshare_qp_;
484 }
485 } else {
486 stream_codec->qpMax = kLowestResMaxQp;
487 }
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000488 }
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200489 if (inst.codecType == webrtc::kVideoCodecVP8) {
490 stream_codec->VP8()->numberOfTemporalLayers =
491 inst.simulcastStream[stream_index].numberOfTemporalLayers;
492 if (!highest_resolution_stream) {
493 // For resolutions below CIF, set the codec |complexity| parameter to
494 // kComplexityHigher, which maps to cpu_used = -4.
495 int pixels_per_frame = stream_codec->width * stream_codec->height;
496 if (pixels_per_frame < 352 * 288) {
497 stream_codec->VP8()->complexity =
498 webrtc::VideoCodecComplexity::kComplexityHigher;
499 }
500 // Turn off denoising for all streams but the highest resolution.
501 stream_codec->VP8()->denoisingOn = false;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000502 }
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000503 }
504 // TODO(ronghuawu): what to do with targetBitrate.
505
Erik Språng78ce6192016-09-12 16:04:43 +0200506 stream_codec->startBitrate = start_bitrate_kbps;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000507}
508
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000509bool SimulcastEncoderAdapter::Initialized() const {
brandtr5e171752017-05-23 03:32:16 -0700510 return rtc::AtomicOps::AcquireLoad(&inited_) == 1;
511}
512
513void SimulcastEncoderAdapter::DestroyStoredEncoders() {
514 while (!stored_encoders_.empty()) {
brandtr5e171752017-05-23 03:32:16 -0700515 stored_encoders_.pop();
516 }
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000517}
518
pbos65e15ba2015-10-15 10:52:15 -0700519bool SimulcastEncoderAdapter::SupportsNativeHandle() const {
brandtr5e171752017-05-23 03:32:16 -0700520 RTC_DCHECK_CALLED_SEQUENTIALLY(&encoder_queue_);
pbos65e15ba2015-10-15 10:52:15 -0700521 // We should not be calling this method before streaminfos_ are configured.
noahricfe3654d2016-07-01 09:05:54 -0700522 for (const auto& streaminfo : streaminfos_) {
brandtr5e171752017-05-23 03:32:16 -0700523 if (!streaminfo.encoder->SupportsNativeHandle()) {
noahricfe3654d2016-07-01 09:05:54 -0700524 return false;
brandtr5e171752017-05-23 03:32:16 -0700525 }
noahricfe3654d2016-07-01 09:05:54 -0700526 }
527 return true;
pbos65e15ba2015-10-15 10:52:15 -0700528}
529
kthelgason876222f2016-11-29 01:44:11 -0800530VideoEncoder::ScalingSettings SimulcastEncoderAdapter::GetScalingSettings()
531 const {
brandtr5e171752017-05-23 03:32:16 -0700532 // TODO(brandtr): Investigate why the sequence checker below fails on mac.
533 // RTC_DCHECK_CALLED_SEQUENTIALLY(&encoder_queue_);
kthelgason876222f2016-11-29 01:44:11 -0800534 // Turn off quality scaling for simulcast.
brandtr5e171752017-05-23 03:32:16 -0700535 if (!Initialized() || NumberOfStreams(codec_) != 1) {
Niels Möller225c7872018-02-22 15:03:53 +0100536 return VideoEncoder::ScalingSettings::kOff;
brandtr5e171752017-05-23 03:32:16 -0700537 }
kthelgason876222f2016-11-29 01:44:11 -0800538 return streaminfos_[0].encoder->GetScalingSettings();
539}
540
pbosecd21b42016-01-07 08:03:05 -0800541const char* SimulcastEncoderAdapter::ImplementationName() const {
brandtr5e171752017-05-23 03:32:16 -0700542 RTC_DCHECK_CALLED_SEQUENTIALLY(&encoder_queue_);
Peter Boströma5dec162016-01-20 15:53:55 +0100543 return implementation_name_.c_str();
pbosecd21b42016-01-07 08:03:05 -0800544}
545
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000546} // namespace webrtc