blob: 808bc0fc150a6f7dd60159daccc0bf513e58c262 [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
15// NOTE(ajm): Path provided by gyp.
16#include "libyuv/scale.h" // NOLINT
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/video/i420_buffer.h"
19#include "media/engine/scopedvideoencoder.h"
20#include "modules/video_coding/codecs/vp8/screenshare_layers.h"
21#include "modules/video_coding/codecs/vp8/simulcast_rate_allocator.h"
22#include "rtc_base/checks.h"
23#include "system_wrappers/include/clock.h"
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000024
25namespace {
26
27const unsigned int kDefaultMinQp = 2;
28const unsigned int kDefaultMaxQp = 56;
29// Max qp for lowest spatial resolution when doing simulcast.
30const unsigned int kLowestResMaxQp = 45;
31
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000032uint32_t SumStreamMaxBitrate(int streams, const webrtc::VideoCodec& codec) {
33 uint32_t bitrate_sum = 0;
34 for (int i = 0; i < streams; ++i) {
35 bitrate_sum += codec.simulcastStream[i].maxBitrate;
36 }
37 return bitrate_sum;
38}
39
40int NumberOfStreams(const webrtc::VideoCodec& codec) {
41 int streams =
42 codec.numberOfSimulcastStreams < 1 ? 1 : codec.numberOfSimulcastStreams;
43 uint32_t simulcast_max_bitrate = SumStreamMaxBitrate(streams, codec);
44 if (simulcast_max_bitrate == 0) {
45 streams = 1;
46 }
47 return streams;
48}
49
50bool ValidSimulcastResolutions(const webrtc::VideoCodec& codec,
51 int num_streams) {
52 if (codec.width != codec.simulcastStream[num_streams - 1].width ||
53 codec.height != codec.simulcastStream[num_streams - 1].height) {
54 return false;
55 }
56 for (int i = 0; i < num_streams; ++i) {
57 if (codec.width * codec.simulcastStream[i].height !=
58 codec.height * codec.simulcastStream[i].width) {
59 return false;
60 }
61 }
62 return true;
63}
64
65int VerifyCodec(const webrtc::VideoCodec* inst) {
brandtr5e171752017-05-23 03:32:16 -070066 if (inst == nullptr) {
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000067 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
68 }
69 if (inst->maxFramerate < 1) {
70 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
71 }
72 // allow zero to represent an unspecified maxBitRate
73 if (inst->maxBitrate > 0 && inst->startBitrate > inst->maxBitrate) {
74 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
75 }
76 if (inst->width <= 1 || inst->height <= 1) {
77 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
78 }
hta257dc392016-10-25 09:05:06 -070079 if (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};
106
Erik Språng08127a92016-11-16 16:41:30 +0100107// Utility class used to adapt the simulcast id as reported by the temporal
108// layers factory, since each sub-encoder will report stream 0.
109class TemporalLayersFactoryAdapter : public webrtc::TemporalLayersFactory {
110 public:
111 TemporalLayersFactoryAdapter(int adapted_simulcast_id,
112 const TemporalLayersFactory& tl_factory)
113 : adapted_simulcast_id_(adapted_simulcast_id), tl_factory_(tl_factory) {}
114 ~TemporalLayersFactoryAdapter() override {}
115 webrtc::TemporalLayers* Create(int simulcast_id,
116 int temporal_layers,
117 uint8_t initial_tl0_pic_idx) const override {
118 return tl_factory_.Create(adapted_simulcast_id_, temporal_layers,
119 initial_tl0_pic_idx);
120 }
Ilya Nikolaevskiybf352982017-10-02 10:08:25 +0200121 std::unique_ptr<webrtc::TemporalLayersChecker> CreateChecker(
122 int simulcast_id,
123 int temporal_layers,
124 uint8_t initial_tl0_pic_idx) const override {
125 return tl_factory_.CreateChecker(adapted_simulcast_id_, temporal_layers,
126 initial_tl0_pic_idx);
127 }
Erik Språng08127a92016-11-16 16:41:30 +0100128
129 const int adapted_simulcast_id_;
130 const TemporalLayersFactory& tl_factory_;
131};
132
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000133} // namespace
134
135namespace webrtc {
136
magjed6cc25612017-07-10 03:26:36 -0700137SimulcastEncoderAdapter::SimulcastEncoderAdapter(
138 cricket::WebRtcVideoEncoderFactory* factory)
brandtr5e171752017-05-23 03:32:16 -0700139 : inited_(0),
140 factory_(factory),
Erik Språng78ce6192016-09-12 16:04:43 +0200141 encoded_complete_callback_(nullptr),
Peter Boströma5dec162016-01-20 15:53:55 +0100142 implementation_name_("SimulcastEncoderAdapter") {
brandtr5e171752017-05-23 03:32:16 -0700143 // The adapter is typically created on the worker thread, but operated on
144 // the encoder task queue.
145 encoder_queue_.Detach();
146
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000147 memset(&codec_, 0, sizeof(webrtc::VideoCodec));
148}
149
150SimulcastEncoderAdapter::~SimulcastEncoderAdapter() {
brandtr5e171752017-05-23 03:32:16 -0700151 RTC_DCHECK(!Initialized());
152 DestroyStoredEncoders();
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000153}
154
155int SimulcastEncoderAdapter::Release() {
brandtr5e171752017-05-23 03:32:16 -0700156 RTC_DCHECK_CALLED_SEQUENTIALLY(&encoder_queue_);
157
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000158 while (!streaminfos_.empty()) {
magjed3f897582017-08-28 08:05:42 -0700159 std::unique_ptr<VideoEncoder> encoder =
160 std::move(streaminfos_.back().encoder);
brandtr5e171752017-05-23 03:32:16 -0700161 // Even though it seems very unlikely, there are no guarantees that the
Rasmus Brandt9cf9f752017-09-28 13:02:58 +0200162 // encoder will not call back after being Release()'d. Therefore, we first
163 // disable the callbacks here.
brandtr5e171752017-05-23 03:32:16 -0700164 encoder->RegisterEncodeCompleteCallback(nullptr);
Rasmus Brandt9cf9f752017-09-28 13:02:58 +0200165 encoder->Release();
brandtr5e171752017-05-23 03:32:16 -0700166 streaminfos_.pop_back(); // Deletes callback adapter.
magjed3f897582017-08-28 08:05:42 -0700167 stored_encoders_.push(std::move(encoder));
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000168 }
brandtr5e171752017-05-23 03:32:16 -0700169
170 // It's legal to move the encoder to another queue now.
171 encoder_queue_.Detach();
172
173 rtc::AtomicOps::ReleaseStore(&inited_, 0);
174
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000175 return WEBRTC_VIDEO_CODEC_OK;
176}
177
178int SimulcastEncoderAdapter::InitEncode(const VideoCodec* inst,
179 int number_of_cores,
180 size_t max_payload_size) {
brandtr5e171752017-05-23 03:32:16 -0700181 RTC_DCHECK_CALLED_SEQUENTIALLY(&encoder_queue_);
182
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000183 if (number_of_cores < 1) {
184 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
185 }
186
187 int ret = VerifyCodec(inst);
188 if (ret < 0) {
189 return ret;
190 }
191
192 ret = Release();
193 if (ret < 0) {
194 return ret;
195 }
196
197 int number_of_streams = NumberOfStreams(*inst);
brandtr5e171752017-05-23 03:32:16 -0700198 RTC_DCHECK_LE(number_of_streams, kMaxSimulcastStreams);
Peter Boströmd53c3892016-03-30 17:03:52 +0200199 const bool doing_simulcast = (number_of_streams > 1);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000200
201 if (doing_simulcast && !ValidSimulcastResolutions(*inst, number_of_streams)) {
202 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
203 }
204
205 codec_ = *inst;
Erik Språng08127a92016-11-16 16:41:30 +0100206 SimulcastRateAllocator rate_allocator(codec_, nullptr);
207 BitrateAllocation allocation = rate_allocator.GetAllocation(
208 codec_.startBitrate * 1000, codec_.maxFramerate);
209 std::vector<uint32_t> start_bitrates;
210 for (int i = 0; i < kMaxSimulcastStreams; ++i) {
211 uint32_t stream_bitrate = allocation.GetSpatialLayerSum(i) / 1000;
212 start_bitrates.push_back(stream_bitrate);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000213 }
214
Peter Boströma5dec162016-01-20 15:53:55 +0100215 std::string implementation_name;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000216 // Create |number_of_streams| of encoder instances and init them.
217 for (int i = 0; i < number_of_streams; ++i) {
218 VideoCodec stream_codec;
Erik Språng78ce6192016-09-12 16:04:43 +0200219 uint32_t start_bitrate_kbps = start_bitrates[i];
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000220 if (!doing_simulcast) {
221 stream_codec = codec_;
222 stream_codec.numberOfSimulcastStreams = 1;
223 } else {
Erik Språng78ce6192016-09-12 16:04:43 +0200224 // Cap start bitrate to the min bitrate in order to avoid strange codec
225 // behavior. Since sending sending will be false, this should not matter.
226 start_bitrate_kbps =
227 std::max(codec_.simulcastStream[i].minBitrate, start_bitrate_kbps);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000228 bool highest_resolution_stream = (i == (number_of_streams - 1));
brandtr5e171752017-05-23 03:32:16 -0700229 PopulateStreamCodec(codec_, i, start_bitrate_kbps,
Erik Språng78ce6192016-09-12 16:04:43 +0200230 highest_resolution_stream, &stream_codec);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000231 }
Erik Språng08127a92016-11-16 16:41:30 +0100232 TemporalLayersFactoryAdapter tl_factory_adapter(i,
233 *codec_.VP8()->tl_factory);
234 stream_codec.VP8()->tl_factory = &tl_factory_adapter;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000235
236 // TODO(ronghuawu): Remove once this is handled in VP8EncoderImpl.
237 if (stream_codec.qpMax < kDefaultMinQp) {
238 stream_codec.qpMax = kDefaultMaxQp;
239 }
240
brandtr5e171752017-05-23 03:32:16 -0700241 // If an existing encoder instance exists, reuse it.
242 // TODO(brandtr): Set initial RTP state (e.g., picture_id/tl0_pic_idx) here,
243 // when we start storing that state outside the encoder wrappers.
magjed3f897582017-08-28 08:05:42 -0700244 std::unique_ptr<VideoEncoder> encoder;
brandtr5e171752017-05-23 03:32:16 -0700245 if (!stored_encoders_.empty()) {
magjed3f897582017-08-28 08:05:42 -0700246 encoder = std::move(stored_encoders_.top());
brandtr5e171752017-05-23 03:32:16 -0700247 stored_encoders_.pop();
248 } else {
magjed3f897582017-08-28 08:05:42 -0700249 encoder = CreateScopedVideoEncoder(factory_, cricket::VideoCodec("VP8"));
brandtr5e171752017-05-23 03:32:16 -0700250 }
251
philipelcce46fc2015-12-21 03:04:49 -0800252 ret = encoder->InitEncode(&stream_codec, number_of_cores, max_payload_size);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000253 if (ret < 0) {
noahrice5ba75a2016-12-12 13:08:27 -0800254 // Explicitly destroy the current encoder; because we haven't registered a
255 // StreamInfo for it yet, Release won't do anything about it.
magjed3f897582017-08-28 08:05:42 -0700256 encoder.reset();
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000257 Release();
258 return ret;
259 }
brandtr5e171752017-05-23 03:32:16 -0700260 std::unique_ptr<EncodedImageCallback> callback(
261 new AdapterEncodedImageCallback(this, i));
262 encoder->RegisterEncodeCompleteCallback(callback.get());
magjed3f897582017-08-28 08:05:42 -0700263 streaminfos_.emplace_back(std::move(encoder), std::move(callback),
264 stream_codec.width, stream_codec.height,
265 start_bitrate_kbps > 0);
brandtr5e171752017-05-23 03:32:16 -0700266
267 if (i != 0) {
Peter Boströma5dec162016-01-20 15:53:55 +0100268 implementation_name += ", ";
brandtr5e171752017-05-23 03:32:16 -0700269 }
Peter Boströma5dec162016-01-20 15:53:55 +0100270 implementation_name += streaminfos_[i].encoder->ImplementationName();
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000271 }
brandtr5e171752017-05-23 03:32:16 -0700272
Peter Boströmd53c3892016-03-30 17:03:52 +0200273 if (doing_simulcast) {
274 implementation_name_ =
275 "SimulcastEncoderAdapter (" + implementation_name + ")";
276 } else {
277 implementation_name_ = implementation_name;
278 }
brandtr5e171752017-05-23 03:32:16 -0700279
280 // To save memory, don't store encoders that we don't use.
281 DestroyStoredEncoders();
282
283 rtc::AtomicOps::ReleaseStore(&inited_, 1);
284
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000285 return WEBRTC_VIDEO_CODEC_OK;
286}
287
288int SimulcastEncoderAdapter::Encode(
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700289 const VideoFrame& input_image,
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000290 const CodecSpecificInfo* codec_specific_info,
pbos22993e12015-10-19 02:39:06 -0700291 const std::vector<FrameType>* frame_types) {
brandtr5e171752017-05-23 03:32:16 -0700292 RTC_DCHECK_CALLED_SEQUENTIALLY(&encoder_queue_);
293
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000294 if (!Initialized()) {
295 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
296 }
brandtr5e171752017-05-23 03:32:16 -0700297 if (encoded_complete_callback_ == nullptr) {
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000298 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
299 }
300
301 // All active streams should generate a key frame if
302 // a key frame is requested by any stream.
303 bool send_key_frame = false;
304 if (frame_types) {
305 for (size_t i = 0; i < frame_types->size(); ++i) {
Peter Boström49e196a2015-10-23 15:58:18 +0200306 if (frame_types->at(i) == kVideoFrameKey) {
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000307 send_key_frame = true;
308 break;
309 }
310 }
311 }
312 for (size_t stream_idx = 0; stream_idx < streaminfos_.size(); ++stream_idx) {
313 if (streaminfos_[stream_idx].key_frame_request &&
314 streaminfos_[stream_idx].send_stream) {
315 send_key_frame = true;
316 break;
317 }
318 }
319
320 int src_width = input_image.width();
321 int src_height = input_image.height();
322 for (size_t stream_idx = 0; stream_idx < streaminfos_.size(); ++stream_idx) {
Peter Boström5d0379d2015-10-06 14:04:51 +0200323 // Don't encode frames in resolutions that we don't intend to send.
brandtr5e171752017-05-23 03:32:16 -0700324 if (!streaminfos_[stream_idx].send_stream) {
Peter Boström5d0379d2015-10-06 14:04:51 +0200325 continue;
brandtr5e171752017-05-23 03:32:16 -0700326 }
Peter Boström5d0379d2015-10-06 14:04:51 +0200327
pbos22993e12015-10-19 02:39:06 -0700328 std::vector<FrameType> stream_frame_types;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000329 if (send_key_frame) {
Peter Boström49e196a2015-10-23 15:58:18 +0200330 stream_frame_types.push_back(kVideoFrameKey);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000331 streaminfos_[stream_idx].key_frame_request = false;
332 } else {
Peter Boström49e196a2015-10-23 15:58:18 +0200333 stream_frame_types.push_back(kVideoFrameDelta);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000334 }
335
336 int dst_width = streaminfos_[stream_idx].width;
337 int dst_height = streaminfos_[stream_idx].height;
338 // If scaling isn't required, because the input resolution
339 // matches the destination or the input image is empty (e.g.
340 // a keyframe request for encoders with internal camera
noahricfe3654d2016-07-01 09:05:54 -0700341 // sources) or the source image has a native handle, pass the image on
342 // directly. Otherwise, we'll scale it to match what the encoder expects
343 // (below).
344 // For texture frames, the underlying encoder is expected to be able to
345 // correctly sample/scale the source texture.
346 // TODO(perkj): ensure that works going forward, and figure out how this
347 // affects webrtc:5683.
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000348 if ((dst_width == src_width && dst_height == src_height) ||
Magnus Jedvert72dbe2a2017-06-10 17:03:37 +0000349 input_image.video_frame_buffer()->type() ==
350 VideoFrameBuffer::Type::kNative) {
noahric57779102016-05-25 06:48:46 -0700351 int ret = streaminfos_[stream_idx].encoder->Encode(
352 input_image, codec_specific_info, &stream_frame_types);
353 if (ret != WEBRTC_VIDEO_CODEC_OK) {
354 return ret;
355 }
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000356 } else {
nisse64ec8f82016-09-27 00:17:25 -0700357 rtc::scoped_refptr<I420Buffer> dst_buffer =
Magnus Jedvert72dbe2a2017-06-10 17:03:37 +0000358 I420Buffer::Create(dst_width, dst_height);
359 rtc::scoped_refptr<I420BufferInterface> src_buffer =
360 input_image.video_frame_buffer()->ToI420();
361 libyuv::I420Scale(src_buffer->DataY(), src_buffer->StrideY(),
362 src_buffer->DataU(), src_buffer->StrideU(),
363 src_buffer->DataV(), src_buffer->StrideV(), src_width,
364 src_height, dst_buffer->MutableDataY(),
365 dst_buffer->StrideY(), dst_buffer->MutableDataU(),
366 dst_buffer->StrideU(), dst_buffer->MutableDataV(),
367 dst_buffer->StrideV(), dst_width, dst_height,
nissec9c142f2016-05-17 04:05:47 -0700368 libyuv::kFilterBilinear);
nisse64ec8f82016-09-27 00:17:25 -0700369
noahric57779102016-05-25 06:48:46 -0700370 int ret = streaminfos_[stream_idx].encoder->Encode(
nisse64ec8f82016-09-27 00:17:25 -0700371 VideoFrame(dst_buffer, input_image.timestamp(),
372 input_image.render_time_ms(), webrtc::kVideoRotation_0),
373 codec_specific_info, &stream_frame_types);
noahric57779102016-05-25 06:48:46 -0700374 if (ret != WEBRTC_VIDEO_CODEC_OK) {
375 return ret;
376 }
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000377 }
378 }
379
380 return WEBRTC_VIDEO_CODEC_OK;
381}
382
383int SimulcastEncoderAdapter::RegisterEncodeCompleteCallback(
384 EncodedImageCallback* callback) {
brandtr5e171752017-05-23 03:32:16 -0700385 RTC_DCHECK_CALLED_SEQUENTIALLY(&encoder_queue_);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000386 encoded_complete_callback_ = callback;
387 return WEBRTC_VIDEO_CODEC_OK;
388}
389
390int SimulcastEncoderAdapter::SetChannelParameters(uint32_t packet_loss,
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000391 int64_t rtt) {
brandtr5e171752017-05-23 03:32:16 -0700392 RTC_DCHECK_CALLED_SEQUENTIALLY(&encoder_queue_);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000393 for (size_t stream_idx = 0; stream_idx < streaminfos_.size(); ++stream_idx) {
394 streaminfos_[stream_idx].encoder->SetChannelParameters(packet_loss, rtt);
395 }
396 return WEBRTC_VIDEO_CODEC_OK;
397}
398
Erik Språng08127a92016-11-16 16:41:30 +0100399int SimulcastEncoderAdapter::SetRateAllocation(const BitrateAllocation& bitrate,
400 uint32_t new_framerate) {
brandtr5e171752017-05-23 03:32:16 -0700401 RTC_DCHECK_CALLED_SEQUENTIALLY(&encoder_queue_);
402
403 if (!Initialized()) {
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000404 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
brandtr5e171752017-05-23 03:32:16 -0700405 }
sprang647bf432016-11-10 06:46:20 -0800406
brandtr5e171752017-05-23 03:32:16 -0700407 if (new_framerate < 1) {
Erik Språng08127a92016-11-16 16:41:30 +0100408 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
brandtr5e171752017-05-23 03:32:16 -0700409 }
Erik Språng08127a92016-11-16 16:41:30 +0100410
brandtr5e171752017-05-23 03:32:16 -0700411 if (codec_.maxBitrate > 0 && bitrate.get_sum_kbps() > codec_.maxBitrate) {
Erik Språng08127a92016-11-16 16:41:30 +0100412 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
brandtr5e171752017-05-23 03:32:16 -0700413 }
Erik Språng08127a92016-11-16 16:41:30 +0100414
415 if (bitrate.get_sum_bps() > 0) {
sprang1369c832016-11-10 08:30:33 -0800416 // Make sure the bitrate fits the configured min bitrates. 0 is a special
417 // value that means paused, though, so leave it alone.
brandtr5e171752017-05-23 03:32:16 -0700418 if (bitrate.get_sum_kbps() < codec_.minBitrate) {
Erik Språng08127a92016-11-16 16:41:30 +0100419 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
brandtr5e171752017-05-23 03:32:16 -0700420 }
Erik Språng08127a92016-11-16 16:41:30 +0100421
sprang1369c832016-11-10 08:30:33 -0800422 if (codec_.numberOfSimulcastStreams > 0 &&
Erik Språng08127a92016-11-16 16:41:30 +0100423 bitrate.get_sum_kbps() < codec_.simulcastStream[0].minBitrate) {
424 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
sprang1369c832016-11-10 08:30:33 -0800425 }
sprang1369c832016-11-10 08:30:33 -0800426 }
Erik Språng08127a92016-11-16 16:41:30 +0100427
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000428 codec_.maxFramerate = new_framerate;
429
sprang1369c832016-11-10 08:30:33 -0800430 for (size_t stream_idx = 0; stream_idx < streaminfos_.size(); ++stream_idx) {
Erik Språng08127a92016-11-16 16:41:30 +0100431 uint32_t stream_bitrate_kbps =
432 bitrate.GetSpatialLayerSum(stream_idx) / 1000;
433
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000434 // Need a key frame if we have not sent this stream before.
Erik Språng78ce6192016-09-12 16:04:43 +0200435 if (stream_bitrate_kbps > 0 && !streaminfos_[stream_idx].send_stream) {
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000436 streaminfos_[stream_idx].key_frame_request = true;
437 }
Erik Språng78ce6192016-09-12 16:04:43 +0200438 streaminfos_[stream_idx].send_stream = stream_bitrate_kbps > 0;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000439
Erik Språng08127a92016-11-16 16:41:30 +0100440 // Slice the temporal layers out of the full allocation and pass it on to
441 // the encoder handling the current simulcast stream.
442 BitrateAllocation stream_allocation;
brandtr5e171752017-05-23 03:32:16 -0700443 for (int i = 0; i < kMaxTemporalStreams; ++i) {
Erik Språng08127a92016-11-16 16:41:30 +0100444 stream_allocation.SetBitrate(0, i, bitrate.GetBitrate(stream_idx, i));
brandtr5e171752017-05-23 03:32:16 -0700445 }
Erik Språng08127a92016-11-16 16:41:30 +0100446 streaminfos_[stream_idx].encoder->SetRateAllocation(stream_allocation,
447 new_framerate);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000448 }
449
450 return WEBRTC_VIDEO_CODEC_OK;
451}
452
brandtr5e171752017-05-23 03:32:16 -0700453// TODO(brandtr): Add task checker to this member function, when all encoder
454// callbacks are coming in on the encoder queue.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700455EncodedImageCallback::Result SimulcastEncoderAdapter::OnEncodedImage(
Noah Richards41ee1ea2015-04-15 09:24:26 -0700456 size_t stream_idx,
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000457 const EncodedImage& encodedImage,
458 const CodecSpecificInfo* codecSpecificInfo,
459 const RTPFragmentationHeader* fragmentation) {
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000460 CodecSpecificInfo stream_codec_specific = *codecSpecificInfo;
perkj275afc52016-09-01 00:21:16 -0700461 stream_codec_specific.codec_name = implementation_name_.c_str();
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000462 CodecSpecificInfoVP8* vp8Info = &(stream_codec_specific.codecSpecific.VP8);
463 vp8Info->simulcastIdx = stream_idx;
464
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700465 return encoded_complete_callback_->OnEncodedImage(
Peter Boström5d0379d2015-10-06 14:04:51 +0200466 encodedImage, &stream_codec_specific, fragmentation);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000467}
468
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000469void SimulcastEncoderAdapter::PopulateStreamCodec(
brandtr5e171752017-05-23 03:32:16 -0700470 const webrtc::VideoCodec& inst,
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000471 int stream_index,
Erik Språng78ce6192016-09-12 16:04:43 +0200472 uint32_t start_bitrate_kbps,
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000473 bool highest_resolution_stream,
Erik Språng78ce6192016-09-12 16:04:43 +0200474 webrtc::VideoCodec* stream_codec) {
brandtr5e171752017-05-23 03:32:16 -0700475 *stream_codec = inst;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000476
477 // Stream specific settings.
hta257dc392016-10-25 09:05:06 -0700478 stream_codec->VP8()->numberOfTemporalLayers =
brandtr5e171752017-05-23 03:32:16 -0700479 inst.simulcastStream[stream_index].numberOfTemporalLayers;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000480 stream_codec->numberOfSimulcastStreams = 0;
brandtr5e171752017-05-23 03:32:16 -0700481 stream_codec->width = inst.simulcastStream[stream_index].width;
482 stream_codec->height = inst.simulcastStream[stream_index].height;
483 stream_codec->maxBitrate = inst.simulcastStream[stream_index].maxBitrate;
484 stream_codec->minBitrate = inst.simulcastStream[stream_index].minBitrate;
485 stream_codec->qpMax = inst.simulcastStream[stream_index].qpMax;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000486 // Settings that are based on stream/resolution.
brandtr5e171752017-05-23 03:32:16 -0700487 const bool lowest_resolution_stream = (stream_index == 0);
488 if (lowest_resolution_stream) {
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000489 // Settings for lowest spatial resolutions.
490 stream_codec->qpMax = kLowestResMaxQp;
491 }
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) {
hta257dc392016-10-25 09:05:06 -0700497 stream_codec->VP8()->complexity = webrtc::kComplexityHigher;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000498 }
499 // Turn off denoising for all streams but the highest resolution.
hta257dc392016-10-25 09:05:06 -0700500 stream_codec->VP8()->denoisingOn = false;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000501 }
502 // TODO(ronghuawu): what to do with targetBitrate.
503
Erik Språng78ce6192016-09-12 16:04:43 +0200504 stream_codec->startBitrate = start_bitrate_kbps;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000505}
506
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000507bool SimulcastEncoderAdapter::Initialized() const {
brandtr5e171752017-05-23 03:32:16 -0700508 return rtc::AtomicOps::AcquireLoad(&inited_) == 1;
509}
510
511void SimulcastEncoderAdapter::DestroyStoredEncoders() {
512 while (!stored_encoders_.empty()) {
brandtr5e171752017-05-23 03:32:16 -0700513 stored_encoders_.pop();
514 }
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000515}
516
pbos65e15ba2015-10-15 10:52:15 -0700517bool SimulcastEncoderAdapter::SupportsNativeHandle() const {
brandtr5e171752017-05-23 03:32:16 -0700518 RTC_DCHECK_CALLED_SEQUENTIALLY(&encoder_queue_);
pbos65e15ba2015-10-15 10:52:15 -0700519 // We should not be calling this method before streaminfos_ are configured.
520 RTC_DCHECK(!streaminfos_.empty());
noahricfe3654d2016-07-01 09:05:54 -0700521 for (const auto& streaminfo : streaminfos_) {
brandtr5e171752017-05-23 03:32:16 -0700522 if (!streaminfo.encoder->SupportsNativeHandle()) {
noahricfe3654d2016-07-01 09:05:54 -0700523 return false;
brandtr5e171752017-05-23 03:32:16 -0700524 }
noahricfe3654d2016-07-01 09:05:54 -0700525 }
526 return true;
pbos65e15ba2015-10-15 10:52:15 -0700527}
528
kthelgason876222f2016-11-29 01:44:11 -0800529VideoEncoder::ScalingSettings SimulcastEncoderAdapter::GetScalingSettings()
530 const {
brandtr5e171752017-05-23 03:32:16 -0700531 // TODO(brandtr): Investigate why the sequence checker below fails on mac.
532 // RTC_DCHECK_CALLED_SEQUENTIALLY(&encoder_queue_);
kthelgason876222f2016-11-29 01:44:11 -0800533 // Turn off quality scaling for simulcast.
brandtr5e171752017-05-23 03:32:16 -0700534 if (!Initialized() || NumberOfStreams(codec_) != 1) {
kthelgason876222f2016-11-29 01:44:11 -0800535 return VideoEncoder::ScalingSettings(false);
brandtr5e171752017-05-23 03:32:16 -0700536 }
kthelgason876222f2016-11-29 01:44:11 -0800537 return streaminfos_[0].encoder->GetScalingSettings();
538}
539
pbosecd21b42016-01-07 08:03:05 -0800540const char* SimulcastEncoderAdapter::ImplementationName() const {
brandtr5e171752017-05-23 03:32:16 -0700541 RTC_DCHECK_CALLED_SEQUENTIALLY(&encoder_queue_);
Peter Boströma5dec162016-01-20 15:53:55 +0100542 return implementation_name_.c_str();
pbosecd21b42016-01-07 08:03:05 -0800543}
544
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000545} // namespace webrtc