blob: d58a7a7d2a2965a3c2244f21f3fb3d2bfdf3a015 [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>
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "api/video/i420_buffer.h"
Erik Språng7fd0a282018-05-22 15:37:23 +020016#include "api/video/video_bitrate_allocation.h"
Magnus Jedvertdf4883d2017-11-17 14:44:55 +010017#include "api/video_codecs/video_encoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "media/engine/scopedvideoencoder.h"
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +020019#include "modules/video_coding/utility/simulcast_rate_allocator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/checks.h"
21#include "system_wrappers/include/clock.h"
Mirko Bonadei65432062017-12-11 09:32:13 +010022#include "third_party/libyuv/include/libyuv/scale.h"
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000023
24namespace {
25
26const unsigned int kDefaultMinQp = 2;
27const unsigned int kDefaultMaxQp = 56;
28// Max qp for lowest spatial resolution when doing simulcast.
29const unsigned int kLowestResMaxQp = 45;
30
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000031uint32_t SumStreamMaxBitrate(int streams, const webrtc::VideoCodec& codec) {
32 uint32_t bitrate_sum = 0;
33 for (int i = 0; i < streams; ++i) {
34 bitrate_sum += codec.simulcastStream[i].maxBitrate;
35 }
36 return bitrate_sum;
37}
38
39int NumberOfStreams(const webrtc::VideoCodec& codec) {
40 int streams =
41 codec.numberOfSimulcastStreams < 1 ? 1 : codec.numberOfSimulcastStreams;
42 uint32_t simulcast_max_bitrate = SumStreamMaxBitrate(streams, codec);
43 if (simulcast_max_bitrate == 0) {
44 streams = 1;
45 }
46 return streams;
47}
48
49bool ValidSimulcastResolutions(const webrtc::VideoCodec& codec,
50 int num_streams) {
51 if (codec.width != codec.simulcastStream[num_streams - 1].width ||
52 codec.height != codec.simulcastStream[num_streams - 1].height) {
53 return false;
54 }
55 for (int i = 0; i < num_streams; ++i) {
56 if (codec.width * codec.simulcastStream[i].height !=
57 codec.height * codec.simulcastStream[i].width) {
58 return false;
59 }
60 }
61 return true;
62}
63
64int VerifyCodec(const webrtc::VideoCodec* inst) {
brandtr5e171752017-05-23 03:32:16 -070065 if (inst == nullptr) {
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000066 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
67 }
68 if (inst->maxFramerate < 1) {
69 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
70 }
71 // allow zero to represent an unspecified maxBitRate
72 if (inst->maxBitrate > 0 && inst->startBitrate > inst->maxBitrate) {
73 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
74 }
75 if (inst->width <= 1 || inst->height <= 1) {
76 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
77 }
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +020078 if (inst->codecType == webrtc::kVideoCodecVP8 &&
79 inst->VP8().automaticResizeOn && inst->numberOfSimulcastStreams > 1) {
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000080 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
81 }
82 return WEBRTC_VIDEO_CODEC_OK;
83}
84
Noah Richards41ee1ea2015-04-15 09:24:26 -070085// An EncodedImageCallback implementation that forwards on calls to a
86// SimulcastEncoderAdapter, but with the stream index it's registered with as
87// the first parameter to Encoded.
88class AdapterEncodedImageCallback : public webrtc::EncodedImageCallback {
89 public:
90 AdapterEncodedImageCallback(webrtc::SimulcastEncoderAdapter* adapter,
91 size_t stream_idx)
92 : adapter_(adapter), stream_idx_(stream_idx) {}
93
Sergey Ulanov525df3f2016-08-02 17:46:41 -070094 EncodedImageCallback::Result OnEncodedImage(
95 const webrtc::EncodedImage& encoded_image,
96 const webrtc::CodecSpecificInfo* codec_specific_info,
97 const webrtc::RTPFragmentationHeader* fragmentation) override {
98 return adapter_->OnEncodedImage(stream_idx_, encoded_image,
99 codec_specific_info, fragmentation);
Noah Richards41ee1ea2015-04-15 09:24:26 -0700100 }
101
102 private:
103 webrtc::SimulcastEncoderAdapter* const adapter_;
104 const size_t stream_idx_;
105};
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000106} // namespace
107
108namespace webrtc {
109
Ilya Nikolaevskiy97b4ee52018-05-28 10:24:22 +0200110SimulcastEncoderAdapter::SimulcastEncoderAdapter(VideoEncoderFactory* factory,
111 const SdpVideoFormat& format)
Magnus Jedvertdf4883d2017-11-17 14:44:55 +0100112 : inited_(0),
113 factory_(factory),
Ilya Nikolaevskiy97b4ee52018-05-28 10:24:22 +0200114 video_format_(format),
Magnus Jedvertdf4883d2017-11-17 14:44:55 +0100115 encoded_complete_callback_(nullptr),
116 implementation_name_("SimulcastEncoderAdapter") {
Rasmus Brandtc334ce92018-02-05 13:03:25 +0100117 RTC_DCHECK(factory_);
Magnus Jedvertdf4883d2017-11-17 14:44:55 +0100118
brandtr5e171752017-05-23 03:32:16 -0700119 // The adapter is typically created on the worker thread, but operated on
120 // the encoder task queue.
121 encoder_queue_.Detach();
122
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000123 memset(&codec_, 0, sizeof(webrtc::VideoCodec));
124}
125
126SimulcastEncoderAdapter::~SimulcastEncoderAdapter() {
brandtr5e171752017-05-23 03:32:16 -0700127 RTC_DCHECK(!Initialized());
128 DestroyStoredEncoders();
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000129}
130
131int SimulcastEncoderAdapter::Release() {
brandtr5e171752017-05-23 03:32:16 -0700132 RTC_DCHECK_CALLED_SEQUENTIALLY(&encoder_queue_);
133
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000134 while (!streaminfos_.empty()) {
magjed3f897582017-08-28 08:05:42 -0700135 std::unique_ptr<VideoEncoder> encoder =
136 std::move(streaminfos_.back().encoder);
brandtr5e171752017-05-23 03:32:16 -0700137 // Even though it seems very unlikely, there are no guarantees that the
Rasmus Brandt9cf9f752017-09-28 13:02:58 +0200138 // encoder will not call back after being Release()'d. Therefore, we first
139 // disable the callbacks here.
brandtr5e171752017-05-23 03:32:16 -0700140 encoder->RegisterEncodeCompleteCallback(nullptr);
Rasmus Brandt9cf9f752017-09-28 13:02:58 +0200141 encoder->Release();
brandtr5e171752017-05-23 03:32:16 -0700142 streaminfos_.pop_back(); // Deletes callback adapter.
magjed3f897582017-08-28 08:05:42 -0700143 stored_encoders_.push(std::move(encoder));
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000144 }
brandtr5e171752017-05-23 03:32:16 -0700145
146 // It's legal to move the encoder to another queue now.
147 encoder_queue_.Detach();
148
149 rtc::AtomicOps::ReleaseStore(&inited_, 0);
150
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000151 return WEBRTC_VIDEO_CODEC_OK;
152}
153
154int SimulcastEncoderAdapter::InitEncode(const VideoCodec* inst,
155 int number_of_cores,
156 size_t max_payload_size) {
brandtr5e171752017-05-23 03:32:16 -0700157 RTC_DCHECK_CALLED_SEQUENTIALLY(&encoder_queue_);
158
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000159 if (number_of_cores < 1) {
160 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
161 }
162
163 int ret = VerifyCodec(inst);
164 if (ret < 0) {
165 return ret;
166 }
167
168 ret = Release();
169 if (ret < 0) {
170 return ret;
171 }
172
173 int number_of_streams = NumberOfStreams(*inst);
brandtr5e171752017-05-23 03:32:16 -0700174 RTC_DCHECK_LE(number_of_streams, kMaxSimulcastStreams);
Peter Boströmd53c3892016-03-30 17:03:52 +0200175 const bool doing_simulcast = (number_of_streams > 1);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000176
177 if (doing_simulcast && !ValidSimulcastResolutions(*inst, number_of_streams)) {
178 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
179 }
180
181 codec_ = *inst;
Erik Språng82fad3d2018-03-21 09:57:23 +0100182 SimulcastRateAllocator rate_allocator(codec_);
Erik Språng566124a2018-04-23 12:32:22 +0200183 VideoBitrateAllocation allocation = rate_allocator.GetAllocation(
Erik Språng08127a92016-11-16 16:41:30 +0100184 codec_.startBitrate * 1000, codec_.maxFramerate);
185 std::vector<uint32_t> start_bitrates;
186 for (int i = 0; i < kMaxSimulcastStreams; ++i) {
187 uint32_t stream_bitrate = allocation.GetSpatialLayerSum(i) / 1000;
188 start_bitrates.push_back(stream_bitrate);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000189 }
190
Peter Boströma5dec162016-01-20 15:53:55 +0100191 std::string implementation_name;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000192 // Create |number_of_streams| of encoder instances and init them.
193 for (int i = 0; i < number_of_streams; ++i) {
194 VideoCodec stream_codec;
Erik Språng78ce6192016-09-12 16:04:43 +0200195 uint32_t start_bitrate_kbps = start_bitrates[i];
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000196 if (!doing_simulcast) {
197 stream_codec = codec_;
198 stream_codec.numberOfSimulcastStreams = 1;
199 } else {
Erik Språng78ce6192016-09-12 16:04:43 +0200200 // Cap start bitrate to the min bitrate in order to avoid strange codec
201 // behavior. Since sending sending will be false, this should not matter.
202 start_bitrate_kbps =
203 std::max(codec_.simulcastStream[i].minBitrate, start_bitrate_kbps);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000204 bool highest_resolution_stream = (i == (number_of_streams - 1));
brandtr5e171752017-05-23 03:32:16 -0700205 PopulateStreamCodec(codec_, i, start_bitrate_kbps,
Erik Språng78ce6192016-09-12 16:04:43 +0200206 highest_resolution_stream, &stream_codec);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000207 }
208
Erik Språngcc681cc2018-03-14 17:52:55 +0100209 // TODO(ronghuawu): Remove once this is handled in LibvpxVp8Encoder.
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000210 if (stream_codec.qpMax < kDefaultMinQp) {
211 stream_codec.qpMax = kDefaultMaxQp;
212 }
213
brandtr5e171752017-05-23 03:32:16 -0700214 // If an existing encoder instance exists, reuse it.
215 // TODO(brandtr): Set initial RTP state (e.g., picture_id/tl0_pic_idx) here,
216 // when we start storing that state outside the encoder wrappers.
magjed3f897582017-08-28 08:05:42 -0700217 std::unique_ptr<VideoEncoder> encoder;
brandtr5e171752017-05-23 03:32:16 -0700218 if (!stored_encoders_.empty()) {
magjed3f897582017-08-28 08:05:42 -0700219 encoder = std::move(stored_encoders_.top());
brandtr5e171752017-05-23 03:32:16 -0700220 stored_encoders_.pop();
221 } else {
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200222 encoder = factory_->CreateVideoEncoder(SdpVideoFormat(
223 codec_.codecType == webrtc::kVideoCodecVP8 ? "VP8" : "H264"));
brandtr5e171752017-05-23 03:32:16 -0700224 }
225
philipelcce46fc2015-12-21 03:04:49 -0800226 ret = encoder->InitEncode(&stream_codec, number_of_cores, max_payload_size);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000227 if (ret < 0) {
noahrice5ba75a2016-12-12 13:08:27 -0800228 // Explicitly destroy the current encoder; because we haven't registered a
229 // StreamInfo for it yet, Release won't do anything about it.
magjed3f897582017-08-28 08:05:42 -0700230 encoder.reset();
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000231 Release();
232 return ret;
233 }
brandtr5e171752017-05-23 03:32:16 -0700234 std::unique_ptr<EncodedImageCallback> callback(
235 new AdapterEncodedImageCallback(this, i));
236 encoder->RegisterEncodeCompleteCallback(callback.get());
magjed3f897582017-08-28 08:05:42 -0700237 streaminfos_.emplace_back(std::move(encoder), std::move(callback),
238 stream_codec.width, stream_codec.height,
239 start_bitrate_kbps > 0);
brandtr5e171752017-05-23 03:32:16 -0700240
241 if (i != 0) {
Peter Boströma5dec162016-01-20 15:53:55 +0100242 implementation_name += ", ";
brandtr5e171752017-05-23 03:32:16 -0700243 }
Peter Boströma5dec162016-01-20 15:53:55 +0100244 implementation_name += streaminfos_[i].encoder->ImplementationName();
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000245 }
brandtr5e171752017-05-23 03:32:16 -0700246
Peter Boströmd53c3892016-03-30 17:03:52 +0200247 if (doing_simulcast) {
248 implementation_name_ =
249 "SimulcastEncoderAdapter (" + implementation_name + ")";
250 } else {
251 implementation_name_ = implementation_name;
252 }
brandtr5e171752017-05-23 03:32:16 -0700253
254 // To save memory, don't store encoders that we don't use.
255 DestroyStoredEncoders();
256
257 rtc::AtomicOps::ReleaseStore(&inited_, 1);
258
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000259 return WEBRTC_VIDEO_CODEC_OK;
260}
261
262int SimulcastEncoderAdapter::Encode(
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700263 const VideoFrame& input_image,
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000264 const CodecSpecificInfo* codec_specific_info,
pbos22993e12015-10-19 02:39:06 -0700265 const std::vector<FrameType>* frame_types) {
brandtr5e171752017-05-23 03:32:16 -0700266 RTC_DCHECK_CALLED_SEQUENTIALLY(&encoder_queue_);
267
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000268 if (!Initialized()) {
269 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
270 }
brandtr5e171752017-05-23 03:32:16 -0700271 if (encoded_complete_callback_ == nullptr) {
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000272 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
273 }
274
275 // All active streams should generate a key frame if
276 // a key frame is requested by any stream.
277 bool send_key_frame = false;
278 if (frame_types) {
279 for (size_t i = 0; i < frame_types->size(); ++i) {
Peter Boström49e196a2015-10-23 15:58:18 +0200280 if (frame_types->at(i) == kVideoFrameKey) {
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000281 send_key_frame = true;
282 break;
283 }
284 }
285 }
286 for (size_t stream_idx = 0; stream_idx < streaminfos_.size(); ++stream_idx) {
287 if (streaminfos_[stream_idx].key_frame_request &&
288 streaminfos_[stream_idx].send_stream) {
289 send_key_frame = true;
290 break;
291 }
292 }
293
294 int src_width = input_image.width();
295 int src_height = input_image.height();
296 for (size_t stream_idx = 0; stream_idx < streaminfos_.size(); ++stream_idx) {
Peter Boström5d0379d2015-10-06 14:04:51 +0200297 // Don't encode frames in resolutions that we don't intend to send.
brandtr5e171752017-05-23 03:32:16 -0700298 if (!streaminfos_[stream_idx].send_stream) {
Peter Boström5d0379d2015-10-06 14:04:51 +0200299 continue;
brandtr5e171752017-05-23 03:32:16 -0700300 }
Peter Boström5d0379d2015-10-06 14:04:51 +0200301
pbos22993e12015-10-19 02:39:06 -0700302 std::vector<FrameType> stream_frame_types;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000303 if (send_key_frame) {
Peter Boström49e196a2015-10-23 15:58:18 +0200304 stream_frame_types.push_back(kVideoFrameKey);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000305 streaminfos_[stream_idx].key_frame_request = false;
306 } else {
Peter Boström49e196a2015-10-23 15:58:18 +0200307 stream_frame_types.push_back(kVideoFrameDelta);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000308 }
309
310 int dst_width = streaminfos_[stream_idx].width;
311 int dst_height = streaminfos_[stream_idx].height;
312 // If scaling isn't required, because the input resolution
313 // matches the destination or the input image is empty (e.g.
314 // a keyframe request for encoders with internal camera
noahricfe3654d2016-07-01 09:05:54 -0700315 // sources) or the source image has a native handle, pass the image on
316 // directly. Otherwise, we'll scale it to match what the encoder expects
317 // (below).
318 // For texture frames, the underlying encoder is expected to be able to
319 // correctly sample/scale the source texture.
320 // TODO(perkj): ensure that works going forward, and figure out how this
321 // affects webrtc:5683.
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000322 if ((dst_width == src_width && dst_height == src_height) ||
Magnus Jedvert72dbe2a2017-06-10 17:03:37 +0000323 input_image.video_frame_buffer()->type() ==
324 VideoFrameBuffer::Type::kNative) {
noahric57779102016-05-25 06:48:46 -0700325 int ret = streaminfos_[stream_idx].encoder->Encode(
326 input_image, codec_specific_info, &stream_frame_types);
327 if (ret != WEBRTC_VIDEO_CODEC_OK) {
328 return ret;
329 }
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000330 } else {
nisse64ec8f82016-09-27 00:17:25 -0700331 rtc::scoped_refptr<I420Buffer> dst_buffer =
Magnus Jedvert72dbe2a2017-06-10 17:03:37 +0000332 I420Buffer::Create(dst_width, dst_height);
333 rtc::scoped_refptr<I420BufferInterface> src_buffer =
334 input_image.video_frame_buffer()->ToI420();
335 libyuv::I420Scale(src_buffer->DataY(), src_buffer->StrideY(),
336 src_buffer->DataU(), src_buffer->StrideU(),
337 src_buffer->DataV(), src_buffer->StrideV(), src_width,
338 src_height, dst_buffer->MutableDataY(),
339 dst_buffer->StrideY(), dst_buffer->MutableDataU(),
340 dst_buffer->StrideU(), dst_buffer->MutableDataV(),
341 dst_buffer->StrideV(), dst_width, dst_height,
nissec9c142f2016-05-17 04:05:47 -0700342 libyuv::kFilterBilinear);
nisse64ec8f82016-09-27 00:17:25 -0700343
noahric57779102016-05-25 06:48:46 -0700344 int ret = streaminfos_[stream_idx].encoder->Encode(
nisse64ec8f82016-09-27 00:17:25 -0700345 VideoFrame(dst_buffer, input_image.timestamp(),
346 input_image.render_time_ms(), webrtc::kVideoRotation_0),
347 codec_specific_info, &stream_frame_types);
noahric57779102016-05-25 06:48:46 -0700348 if (ret != WEBRTC_VIDEO_CODEC_OK) {
349 return ret;
350 }
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000351 }
352 }
353
354 return WEBRTC_VIDEO_CODEC_OK;
355}
356
357int SimulcastEncoderAdapter::RegisterEncodeCompleteCallback(
358 EncodedImageCallback* callback) {
brandtr5e171752017-05-23 03:32:16 -0700359 RTC_DCHECK_CALLED_SEQUENTIALLY(&encoder_queue_);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000360 encoded_complete_callback_ = callback;
361 return WEBRTC_VIDEO_CODEC_OK;
362}
363
364int SimulcastEncoderAdapter::SetChannelParameters(uint32_t packet_loss,
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000365 int64_t rtt) {
brandtr5e171752017-05-23 03:32:16 -0700366 RTC_DCHECK_CALLED_SEQUENTIALLY(&encoder_queue_);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000367 for (size_t stream_idx = 0; stream_idx < streaminfos_.size(); ++stream_idx) {
368 streaminfos_[stream_idx].encoder->SetChannelParameters(packet_loss, rtt);
369 }
370 return WEBRTC_VIDEO_CODEC_OK;
371}
372
Erik Språng566124a2018-04-23 12:32:22 +0200373int SimulcastEncoderAdapter::SetRateAllocation(
374 const VideoBitrateAllocation& bitrate,
375 uint32_t new_framerate) {
brandtr5e171752017-05-23 03:32:16 -0700376 RTC_DCHECK_CALLED_SEQUENTIALLY(&encoder_queue_);
377
378 if (!Initialized()) {
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000379 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
brandtr5e171752017-05-23 03:32:16 -0700380 }
sprang647bf432016-11-10 06:46:20 -0800381
brandtr5e171752017-05-23 03:32:16 -0700382 if (new_framerate < 1) {
Erik Språng08127a92016-11-16 16:41:30 +0100383 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
brandtr5e171752017-05-23 03:32:16 -0700384 }
Erik Språng08127a92016-11-16 16:41:30 +0100385
brandtr5e171752017-05-23 03:32:16 -0700386 if (codec_.maxBitrate > 0 && bitrate.get_sum_kbps() > codec_.maxBitrate) {
Erik Språng08127a92016-11-16 16:41:30 +0100387 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
brandtr5e171752017-05-23 03:32:16 -0700388 }
Erik Språng08127a92016-11-16 16:41:30 +0100389
390 if (bitrate.get_sum_bps() > 0) {
sprang1369c832016-11-10 08:30:33 -0800391 // Make sure the bitrate fits the configured min bitrates. 0 is a special
392 // value that means paused, though, so leave it alone.
brandtr5e171752017-05-23 03:32:16 -0700393 if (bitrate.get_sum_kbps() < codec_.minBitrate) {
Erik Språng08127a92016-11-16 16:41:30 +0100394 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
brandtr5e171752017-05-23 03:32:16 -0700395 }
Erik Språng08127a92016-11-16 16:41:30 +0100396
sprang1369c832016-11-10 08:30:33 -0800397 if (codec_.numberOfSimulcastStreams > 0 &&
Erik Språng08127a92016-11-16 16:41:30 +0100398 bitrate.get_sum_kbps() < codec_.simulcastStream[0].minBitrate) {
399 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
sprang1369c832016-11-10 08:30:33 -0800400 }
sprang1369c832016-11-10 08:30:33 -0800401 }
Erik Språng08127a92016-11-16 16:41:30 +0100402
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000403 codec_.maxFramerate = new_framerate;
404
sprang1369c832016-11-10 08:30:33 -0800405 for (size_t stream_idx = 0; stream_idx < streaminfos_.size(); ++stream_idx) {
Erik Språng08127a92016-11-16 16:41:30 +0100406 uint32_t stream_bitrate_kbps =
407 bitrate.GetSpatialLayerSum(stream_idx) / 1000;
408
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000409 // Need a key frame if we have not sent this stream before.
Erik Språng78ce6192016-09-12 16:04:43 +0200410 if (stream_bitrate_kbps > 0 && !streaminfos_[stream_idx].send_stream) {
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000411 streaminfos_[stream_idx].key_frame_request = true;
412 }
Erik Språng78ce6192016-09-12 16:04:43 +0200413 streaminfos_[stream_idx].send_stream = stream_bitrate_kbps > 0;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000414
Erik Språng08127a92016-11-16 16:41:30 +0100415 // Slice the temporal layers out of the full allocation and pass it on to
416 // the encoder handling the current simulcast stream.
Erik Språng566124a2018-04-23 12:32:22 +0200417 VideoBitrateAllocation stream_allocation;
brandtr5e171752017-05-23 03:32:16 -0700418 for (int i = 0; i < kMaxTemporalStreams; ++i) {
erikvarga@webrtc.org01f2ec32017-11-15 14:58:23 +0100419 if (bitrate.HasBitrate(stream_idx, i)) {
420 stream_allocation.SetBitrate(0, i, bitrate.GetBitrate(stream_idx, i));
421 }
brandtr5e171752017-05-23 03:32:16 -0700422 }
Erik Språng08127a92016-11-16 16:41:30 +0100423 streaminfos_[stream_idx].encoder->SetRateAllocation(stream_allocation,
424 new_framerate);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000425 }
426
427 return WEBRTC_VIDEO_CODEC_OK;
428}
429
brandtr5e171752017-05-23 03:32:16 -0700430// TODO(brandtr): Add task checker to this member function, when all encoder
431// callbacks are coming in on the encoder queue.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700432EncodedImageCallback::Result SimulcastEncoderAdapter::OnEncodedImage(
Noah Richards41ee1ea2015-04-15 09:24:26 -0700433 size_t stream_idx,
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000434 const EncodedImage& encodedImage,
435 const CodecSpecificInfo* codecSpecificInfo,
436 const RTPFragmentationHeader* fragmentation) {
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000437 CodecSpecificInfo stream_codec_specific = *codecSpecificInfo;
perkj275afc52016-09-01 00:21:16 -0700438 stream_codec_specific.codec_name = implementation_name_.c_str();
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200439 if (stream_codec_specific.codecType == webrtc::kVideoCodecVP8) {
440 stream_codec_specific.codecSpecific.VP8.simulcastIdx = stream_idx;
441 } else if (stream_codec_specific.codecType == webrtc::kVideoCodecH264) {
442 stream_codec_specific.codecSpecific.H264.simulcast_idx = stream_idx;
443 }
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000444
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700445 return encoded_complete_callback_->OnEncodedImage(
Peter Boström5d0379d2015-10-06 14:04:51 +0200446 encodedImage, &stream_codec_specific, fragmentation);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000447}
448
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000449void SimulcastEncoderAdapter::PopulateStreamCodec(
brandtr5e171752017-05-23 03:32:16 -0700450 const webrtc::VideoCodec& inst,
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000451 int stream_index,
Erik Språng78ce6192016-09-12 16:04:43 +0200452 uint32_t start_bitrate_kbps,
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000453 bool highest_resolution_stream,
Erik Språng78ce6192016-09-12 16:04:43 +0200454 webrtc::VideoCodec* stream_codec) {
brandtr5e171752017-05-23 03:32:16 -0700455 *stream_codec = inst;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000456
457 // Stream specific settings.
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000458 stream_codec->numberOfSimulcastStreams = 0;
brandtr5e171752017-05-23 03:32:16 -0700459 stream_codec->width = inst.simulcastStream[stream_index].width;
460 stream_codec->height = inst.simulcastStream[stream_index].height;
461 stream_codec->maxBitrate = inst.simulcastStream[stream_index].maxBitrate;
462 stream_codec->minBitrate = inst.simulcastStream[stream_index].minBitrate;
463 stream_codec->qpMax = inst.simulcastStream[stream_index].qpMax;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000464 // Settings that are based on stream/resolution.
brandtr5e171752017-05-23 03:32:16 -0700465 const bool lowest_resolution_stream = (stream_index == 0);
Erik Språng8d2995b2018-08-09 11:18:17 +0200466 if (lowest_resolution_stream && inst.mode != VideoCodecMode::kScreensharing) {
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000467 // Settings for lowest spatial resolutions.
468 stream_codec->qpMax = kLowestResMaxQp;
469 }
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200470 if (inst.codecType == webrtc::kVideoCodecVP8) {
471 stream_codec->VP8()->numberOfTemporalLayers =
472 inst.simulcastStream[stream_index].numberOfTemporalLayers;
473 if (!highest_resolution_stream) {
474 // For resolutions below CIF, set the codec |complexity| parameter to
475 // kComplexityHigher, which maps to cpu_used = -4.
476 int pixels_per_frame = stream_codec->width * stream_codec->height;
477 if (pixels_per_frame < 352 * 288) {
478 stream_codec->VP8()->complexity =
479 webrtc::VideoCodecComplexity::kComplexityHigher;
480 }
481 // Turn off denoising for all streams but the highest resolution.
482 stream_codec->VP8()->denoisingOn = false;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000483 }
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000484 }
485 // TODO(ronghuawu): what to do with targetBitrate.
486
Erik Språng78ce6192016-09-12 16:04:43 +0200487 stream_codec->startBitrate = start_bitrate_kbps;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000488}
489
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000490bool SimulcastEncoderAdapter::Initialized() const {
brandtr5e171752017-05-23 03:32:16 -0700491 return rtc::AtomicOps::AcquireLoad(&inited_) == 1;
492}
493
494void SimulcastEncoderAdapter::DestroyStoredEncoders() {
495 while (!stored_encoders_.empty()) {
brandtr5e171752017-05-23 03:32:16 -0700496 stored_encoders_.pop();
497 }
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000498}
499
pbos65e15ba2015-10-15 10:52:15 -0700500bool SimulcastEncoderAdapter::SupportsNativeHandle() const {
brandtr5e171752017-05-23 03:32:16 -0700501 RTC_DCHECK_CALLED_SEQUENTIALLY(&encoder_queue_);
pbos65e15ba2015-10-15 10:52:15 -0700502 // We should not be calling this method before streaminfos_ are configured.
503 RTC_DCHECK(!streaminfos_.empty());
noahricfe3654d2016-07-01 09:05:54 -0700504 for (const auto& streaminfo : streaminfos_) {
brandtr5e171752017-05-23 03:32:16 -0700505 if (!streaminfo.encoder->SupportsNativeHandle()) {
noahricfe3654d2016-07-01 09:05:54 -0700506 return false;
brandtr5e171752017-05-23 03:32:16 -0700507 }
noahricfe3654d2016-07-01 09:05:54 -0700508 }
509 return true;
pbos65e15ba2015-10-15 10:52:15 -0700510}
511
kthelgason876222f2016-11-29 01:44:11 -0800512VideoEncoder::ScalingSettings SimulcastEncoderAdapter::GetScalingSettings()
513 const {
brandtr5e171752017-05-23 03:32:16 -0700514 // TODO(brandtr): Investigate why the sequence checker below fails on mac.
515 // RTC_DCHECK_CALLED_SEQUENTIALLY(&encoder_queue_);
kthelgason876222f2016-11-29 01:44:11 -0800516 // Turn off quality scaling for simulcast.
brandtr5e171752017-05-23 03:32:16 -0700517 if (!Initialized() || NumberOfStreams(codec_) != 1) {
Niels Möller225c7872018-02-22 15:03:53 +0100518 return VideoEncoder::ScalingSettings::kOff;
brandtr5e171752017-05-23 03:32:16 -0700519 }
kthelgason876222f2016-11-29 01:44:11 -0800520 return streaminfos_[0].encoder->GetScalingSettings();
521}
522
pbosecd21b42016-01-07 08:03:05 -0800523const char* SimulcastEncoderAdapter::ImplementationName() const {
brandtr5e171752017-05-23 03:32:16 -0700524 RTC_DCHECK_CALLED_SEQUENTIALLY(&encoder_queue_);
Peter Boströma5dec162016-01-20 15:53:55 +0100525 return implementation_name_.c_str();
pbosecd21b42016-01-07 08:03:05 -0800526}
527
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000528} // namespace webrtc