| 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 |
| 11 | #include "CustomSimulcastEncoderAdapter.h" |
| 12 |
| 13 | #include <stdio.h> |
| 14 | #include <string.h> |
| 15 |
| 16 | #include <algorithm> |
| 17 | #include <cstdint> |
| 18 | #include <string> |
| 19 | #include <utility> |
| 20 |
| 21 | #include "absl/algorithm/container.h" |
| 22 | #include "api/scoped_refptr.h" |
| 23 | #include "api/video/i420_buffer.h" |
| 24 | #include "api/video/video_codec_constants.h" |
| 25 | #include "api/video/video_frame_buffer.h" |
| 26 | #include "api/video/video_rotation.h" |
| 27 | #include "api/video_codecs/video_encoder.h" |
| 28 | #include "api/video_codecs/video_encoder_factory.h" |
| 29 | #include "api/video_codecs/video_encoder_software_fallback_wrapper.h" |
| 30 | #include "media/base/video_common.h" |
| 31 | #include "modules/video_coding/include/video_error_codes.h" |
| 32 | #include "modules/video_coding/utility/simulcast_rate_allocator.h" |
| 33 | #include "rtc_base/checks.h" |
| 34 | #include "rtc_base/experiments/rate_control_settings.h" |
| 35 | #include "rtc_base/logging.h" |
| 36 | #include "system_wrappers/include/field_trial.h" |
| 37 |
| 38 | #include <stdio.h> |
| 39 | #include <string.h> |
| 40 |
| 41 | #include <algorithm> |
| 42 | #include <cstdint> |
| 43 | #include <string> |
| 44 | #include <utility> |
| 45 |
| 46 | #include "absl/algorithm/container.h" |
| 47 | #include "api/field_trials_view.h" |
| 48 | #include "api/scoped_refptr.h" |
| 49 | #include "api/transport/field_trial_based_config.h" |
| 50 | #include "api/video/i420_buffer.h" |
| 51 | #include "api/video/video_codec_constants.h" |
| 52 | #include "api/video/video_frame_buffer.h" |
| 53 | #include "api/video/video_rotation.h" |
| 54 | #include "api/video_codecs/video_encoder.h" |
| 55 | #include "api/video_codecs/video_encoder_factory.h" |
| 56 | #include "api/video_codecs/video_encoder_software_fallback_wrapper.h" |
| 57 | #include "media/base/media_constants.h" |
| 58 | #include "media/base/video_common.h" |
| 59 | #include "modules/video_coding/include/video_error_codes.h" |
| 60 | #include "modules/video_coding/include/video_error_codes_utils.h" |
| 61 | #include "modules/video_coding/utility/simulcast_rate_allocator.h" |
| 62 | #include "rtc_base/checks.h" |
| 63 | #include "rtc_base/experiments/rate_control_settings.h" |
| 64 | #include "rtc_base/logging.h" |
| 65 |
| 66 | namespace { |
| 67 |
| 68 | // Max qp for lowest spatial resolution when doing simulcast. |
| 69 | const unsigned int kLowestResMaxQp = 45; |
| 70 |
| 71 | absl::optional<unsigned int> GetScreenshareBoostedQpValue( |
| 72 | const webrtc::FieldTrialsView& field_trials) { |
| 73 | std::string experiment_group = |
| 74 | field_trials.Lookup("WebRTC-BoostedScreenshareQp"); |
| 75 | unsigned int qp; |
| 76 | if (sscanf(experiment_group.c_str(), "%u", &qp) != 1) |
| 77 | return absl::nullopt; |
| 78 | qp = std::min(qp, 63u); |
| 79 | qp = std::max(qp, 1u); |
| 80 | return qp; |
| 81 | } |
| 82 |
| 83 | uint32_t SumStreamMaxBitrate(int streams, const webrtc::VideoCodec& codec) { |
| 84 | uint32_t bitrate_sum = 0; |
| 85 | for (int i = 0; i < streams; ++i) { |
| 86 | bitrate_sum += codec.simulcastStream[i].maxBitrate; |
| 87 | } |
| 88 | return bitrate_sum; |
| 89 | } |
| 90 |
| 91 | int CountAllStreams(const webrtc::VideoCodec& codec) { |
| 92 | int total_streams_count = |
| 93 | codec.numberOfSimulcastStreams < 1 ? 1 : codec.numberOfSimulcastStreams; |
| 94 | uint32_t simulcast_max_bitrate = |
| 95 | SumStreamMaxBitrate(total_streams_count, codec); |
| 96 | if (simulcast_max_bitrate == 0) { |
| 97 | total_streams_count = 1; |
| 98 | } |
| 99 | return total_streams_count; |
| 100 | } |
| 101 |
| 102 | int CountActiveStreams(const webrtc::VideoCodec& codec) { |
| 103 | if (codec.numberOfSimulcastStreams < 1) { |
| 104 | return 1; |
| 105 | } |
| 106 | int total_streams_count = CountAllStreams(codec); |
| 107 | int active_streams_count = 0; |
| 108 | for (int i = 0; i < total_streams_count; ++i) { |
| 109 | if (codec.simulcastStream[i].active) { |
| 110 | ++active_streams_count; |
| 111 | } |
| 112 | } |
| 113 | return active_streams_count; |
| 114 | } |
| 115 |
| 116 | int VerifyCodec(const webrtc::VideoCodec* codec_settings) { |
| 117 | if (codec_settings == nullptr) { |
| 118 | return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
| 119 | } |
| 120 | if (codec_settings->maxFramerate < 1) { |
| 121 | return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
| 122 | } |
| 123 | // allow zero to represent an unspecified maxBitRate |
| 124 | if (codec_settings->maxBitrate > 0 && |
| 125 | codec_settings->startBitrate > codec_settings->maxBitrate) { |
| 126 | return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
| 127 | } |
| 128 | if (codec_settings->width <= 1 || codec_settings->height <= 1) { |
| 129 | return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
| 130 | } |
| 131 | if (codec_settings->codecType == webrtc::kVideoCodecVP8 && |
| 132 | codec_settings->VP8().automaticResizeOn && |
| 133 | CountActiveStreams(*codec_settings) > 1) { |
| 134 | return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
| 135 | } |
| 136 | return WEBRTC_VIDEO_CODEC_OK; |
| 137 | } |
| 138 |
| 139 | bool StreamQualityCompare(const webrtc::SimulcastStream& a, |
| 140 | const webrtc::SimulcastStream& b) { |
| 141 | return std::tie(a.height, a.width, a.maxBitrate, a.maxFramerate) < |
| 142 | std::tie(b.height, b.width, b.maxBitrate, b.maxFramerate); |
| 143 | } |
| 144 |
| 145 | void GetLowestAndHighestQualityStreamIndixes( |
| 146 | rtc::ArrayView<webrtc::SimulcastStream> streams, |
| 147 | int* lowest_quality_stream_idx, |
| 148 | int* highest_quality_stream_idx) { |
| 149 | const auto lowest_highest_quality_streams = |
| 150 | absl::c_minmax_element(streams, StreamQualityCompare); |
| 151 | *lowest_quality_stream_idx = |
| 152 | (int)std::distance(streams.begin(), lowest_highest_quality_streams.first); |
| 153 | *highest_quality_stream_idx = |
| 154 | (int)std::distance(streams.begin(), lowest_highest_quality_streams.second); |
| 155 | } |
| 156 |
| 157 | std::vector<uint32_t> GetStreamStartBitratesKbps( |
| 158 | const webrtc::VideoCodec& codec) { |
| 159 | std::vector<uint32_t> start_bitrates; |
| 160 | std::unique_ptr<webrtc::VideoBitrateAllocator> rate_allocator = |
| 161 | std::make_unique<webrtc::SimulcastRateAllocator>(codec); |
| 162 | webrtc::VideoBitrateAllocation allocation = |
| 163 | rate_allocator->Allocate(webrtc::VideoBitrateAllocationParameters( |
| 164 | codec.startBitrate * 1000, codec.maxFramerate)); |
| 165 |
| 166 | int total_streams_count = CountAllStreams(codec); |
| 167 | for (int i = 0; i < total_streams_count; ++i) { |
| 168 | uint32_t stream_bitrate = allocation.GetSpatialLayerSum(i) / 1000; |
| 169 | start_bitrates.push_back(stream_bitrate); |
| 170 | } |
| 171 | return start_bitrates; |
| 172 | } |
| 173 |
| 174 | } // namespace |
| 175 |
| 176 | namespace webrtc { |
| 177 |
| 178 | CustomSimulcastEncoderAdapter::EncoderContext::EncoderContext( |
| 179 | std::unique_ptr<VideoEncoder> encoder, |
| 180 | bool prefer_temporal_support, |
| 181 | VideoEncoder::EncoderInfo primary_info, |
| 182 | VideoEncoder::EncoderInfo fallback_info) |
| 183 | : encoder_(std::move(encoder)), |
| 184 | prefer_temporal_support_(prefer_temporal_support), |
| 185 | primary_info_(std::move(primary_info)), |
| 186 | fallback_info_(std::move(fallback_info)) {} |
| 187 |
| 188 | void CustomSimulcastEncoderAdapter::EncoderContext::Release() { |
| 189 | if (encoder_) { |
| 190 | encoder_->Release(); |
| 191 | encoder_->RegisterEncodeCompleteCallback(nullptr); |
| 192 | } |
| 193 | } |
| 194 |
| 195 | CustomSimulcastEncoderAdapter::StreamContext::StreamContext( |
| 196 | CustomSimulcastEncoderAdapter* parent, |
| 197 | std::unique_ptr<EncoderContext> encoder_context, |
| 198 | std::unique_ptr<FramerateController> framerate_controller, |
| 199 | int stream_idx, |
| 200 | uint16_t width, |
| 201 | uint16_t height, |
| 202 | bool is_paused) |
| 203 | : parent_(parent), |
| 204 | encoder_context_(std::move(encoder_context)), |
| 205 | framerate_controller_(std::move(framerate_controller)), |
| 206 | stream_idx_(stream_idx), |
| 207 | width_(width), |
| 208 | height_(height), |
| 209 | is_keyframe_needed_(false), |
| 210 | is_paused_(is_paused) { |
| 211 | if (parent_) { |
| 212 | encoder_context_->encoder().RegisterEncodeCompleteCallback(this); |
| 213 | } |
| 214 | } |
| 215 |
| 216 | CustomSimulcastEncoderAdapter::StreamContext::StreamContext(StreamContext&& rhs) |
| 217 | : parent_(rhs.parent_), |
| 218 | encoder_context_(std::move(rhs.encoder_context_)), |
| 219 | framerate_controller_(std::move(rhs.framerate_controller_)), |
| 220 | stream_idx_(rhs.stream_idx_), |
| 221 | width_(rhs.width_), |
| 222 | height_(rhs.height_), |
| 223 | is_keyframe_needed_(rhs.is_keyframe_needed_), |
| 224 | is_paused_(rhs.is_paused_) { |
| 225 | if (parent_) { |
| 226 | encoder_context_->encoder().RegisterEncodeCompleteCallback(this); |
| 227 | } |
| 228 | } |
| 229 |
| 230 | CustomSimulcastEncoderAdapter::StreamContext::~StreamContext() { |
| 231 | if (encoder_context_) { |
| 232 | encoder_context_->Release(); |
| 233 | } |
| 234 | } |
| 235 |
| 236 | std::unique_ptr<CustomSimulcastEncoderAdapter::EncoderContext> |
| 237 | CustomSimulcastEncoderAdapter::StreamContext::ReleaseEncoderContext() && { |
| 238 | encoder_context_->Release(); |
| 239 | return std::move(encoder_context_); |
| 240 | } |
| 241 |
| 242 | void CustomSimulcastEncoderAdapter::StreamContext::OnKeyframe(Timestamp timestamp) { |
| 243 | is_keyframe_needed_ = false; |
| 244 | if (framerate_controller_) { |
| 245 | framerate_controller_->KeepFrame(timestamp.us() * 1000); |
| 246 | } |
| 247 | } |
| 248 |
| 249 | bool CustomSimulcastEncoderAdapter::StreamContext::ShouldDropFrame( |
| 250 | Timestamp timestamp) { |
| 251 | if (!framerate_controller_) { |
| 252 | return false; |
| 253 | } |
| 254 | return framerate_controller_->ShouldDropFrame(timestamp.us() * 1000); |
| 255 | } |
| 256 |
| 257 | EncodedImageCallback::Result |
| 258 | CustomSimulcastEncoderAdapter::StreamContext::OnEncodedImage( |
| 259 | const EncodedImage& encoded_image, |
| 260 | const CodecSpecificInfo* codec_specific_info) { |
| 261 | RTC_CHECK(parent_); // If null, this method should never be called. |
| 262 | return parent_->OnEncodedImage(stream_idx_, encoded_image, |
| 263 | codec_specific_info); |
| 264 | } |
| 265 |
| 266 | void CustomSimulcastEncoderAdapter::StreamContext::OnDroppedFrame( |
| 267 | DropReason /*reason*/) { |
| 268 | RTC_CHECK(parent_); // If null, this method should never be called. |
| 269 | parent_->OnDroppedFrame(stream_idx_); |
| 270 | } |
| 271 |
| 272 | CustomSimulcastEncoderAdapter::CustomSimulcastEncoderAdapter(VideoEncoderFactory* factory, |
| 273 | const SdpVideoFormat& format) |
| 274 | : CustomSimulcastEncoderAdapter(factory, |
| 275 | nullptr, |
| 276 | format, |
| 277 | FieldTrialBasedConfig()) {} |
| 278 |
| 279 | CustomSimulcastEncoderAdapter::CustomSimulcastEncoderAdapter( |
| 280 | VideoEncoderFactory* primary_factory, |
| 281 | VideoEncoderFactory* hardware_factory, |
| 282 | const SdpVideoFormat& format, |
| 283 | const FieldTrialsView& field_trials) |
| 284 | : inited_(0), |
| 285 | primary_encoder_factory_(primary_factory), |
| 286 | hardware_encoder_factory_(hardware_factory), |
| 287 | video_format_(format), |
| 288 | total_streams_count_(0), |
| 289 | bypass_mode_(false), |
| 290 | encoded_complete_callback_(nullptr), |
| 291 | experimental_boosted_screenshare_qp_( |
| 292 | GetScreenshareBoostedQpValue(field_trials)), |
| 293 | boost_base_layer_quality_( |
| 294 | RateControlSettings::ParseFromKeyValueConfig(&field_trials) |
| 295 | .Vp8BoostBaseLayerQuality()), |
| 296 | prefer_temporal_support_on_base_layer_(field_trials.IsEnabled( |
| 297 | "WebRTC-Video-PreferTemporalSupportOnBaseLayer")) { |
| 298 | RTC_DCHECK(primary_factory); |
| 299 |
| 300 | // The adapter is typically created on the worker thread, but operated on |
| 301 | // the encoder task queue. |
| 302 | encoder_queue_.Detach(); |
| 303 | } |
| 304 |
| 305 | CustomSimulcastEncoderAdapter::~CustomSimulcastEncoderAdapter() { |
| 306 | RTC_DCHECK(!Initialized()); |
| 307 | DestroyStoredEncoders(); |
| 308 | } |
| 309 |
| 310 | void CustomSimulcastEncoderAdapter::SetFecControllerOverride( |
| 311 | FecControllerOverride* /*fec_controller_override*/) { |
| 312 | // Ignored. |
| 313 | } |
| 314 |
| 315 | int CustomSimulcastEncoderAdapter::Release() { |
| 316 | RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 317 |
| 318 | while (!stream_contexts_.empty()) { |
| 319 | // Move the encoder instances and put it on the `cached_encoder_contexts_` |
| 320 | // where it may possibly be reused from (ordering does not matter). |
| 321 | cached_encoder_contexts_.push_front( |
| 322 | std::move(stream_contexts_.back()).ReleaseEncoderContext()); |
| 323 | stream_contexts_.pop_back(); |
| 324 | } |
| 325 |
| 326 | bypass_mode_ = false; |
| 327 |
| 328 | // It's legal to move the encoder to another queue now. |
| 329 | encoder_queue_.Detach(); |
| 330 |
| 331 | inited_.store(0); |
| 332 |
| 333 | return WEBRTC_VIDEO_CODEC_OK; |
| 334 | } |
| 335 |
| 336 | int CustomSimulcastEncoderAdapter::InitEncode( |
| 337 | const VideoCodec* codec_settings, |
| 338 | const VideoEncoder::Settings& settings) { |
| 339 | RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 340 |
| 341 | if (settings.number_of_cores < 1) { |
| 342 | return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
| 343 | } |
| 344 |
| 345 | int ret = VerifyCodec(codec_settings); |
| 346 | if (ret < 0) { |
| 347 | return ret; |
| 348 | } |
| 349 |
| 350 | Release(); |
| 351 |
| 352 | codec_ = *codec_settings; |
| 353 | total_streams_count_ = CountAllStreams(*codec_settings); |
| 354 |
| 355 | bool is_legacy_singlecast = codec_.numberOfSimulcastStreams == 0; |
| 356 | int lowest_quality_stream_idx = 0; |
| 357 | int highest_quality_stream_idx = 0; |
| 358 | if (!is_legacy_singlecast) { |
| 359 | GetLowestAndHighestQualityStreamIndixes( |
| 360 | rtc::ArrayView<SimulcastStream>(codec_.simulcastStream, |
| 361 | total_streams_count_), |
| 362 | &lowest_quality_stream_idx, &highest_quality_stream_idx); |
| 363 | } |
| 364 |
| 365 | std::unique_ptr<EncoderContext> encoder_context = FetchOrCreateEncoderContext( |
| 366 | /*is_lowest_quality_stream=*/( |
| 367 | is_legacy_singlecast || |
| 368 | codec_.simulcastStream[lowest_quality_stream_idx].active), false); |
| 369 | if (encoder_context == nullptr) { |
| 370 | return WEBRTC_VIDEO_CODEC_MEMORY; |
| 371 | } |
| 372 |
| 373 | // Two distinct scenarios: |
| 374 | // * Singlecast (total_streams_count == 1) or simulcast with simulcast-capable |
| 375 | // underlaying encoder implementation if active_streams_count > 1. SEA |
| 376 | // operates in bypass mode: original settings are passed to the underlaying |
| 377 | // encoder, frame encode complete callback is not intercepted. |
| 378 | // * Multi-encoder simulcast or singlecast if layers are deactivated |
| 379 | // (active_streams_count >= 1). SEA creates N=active_streams_count encoders |
| 380 | // and configures each to produce a single stream. |
| 381 |
| 382 | int active_streams_count = CountActiveStreams(*codec_settings); |
| 383 | // If we only have a single active layer it is better to create an encoder |
| 384 | // with only one configured layer than creating it with all-but-one disabled |
| 385 | // layers because that way we control scaling. |
| 386 | bool separate_encoders_needed = true; |
| 387 | // Singlecast or simulcast with simulcast-capable underlaying encoder. |
| 388 | if (total_streams_count_ == 1 || !separate_encoders_needed) { |
| 389 | RTC_LOG(LS_INFO) << "[SEA] InitEncode: Single-encoder mode"; |
| 390 | int ret = encoder_context->encoder().InitEncode(&codec_, settings); |
| 391 | if (ret >= 0) { |
| 392 | stream_contexts_.emplace_back( |
| 393 | /*parent=*/nullptr, std::move(encoder_context), |
| 394 | /*framerate_controller=*/nullptr, /*stream_idx=*/0, codec_.width, |
| 395 | codec_.height, /*is_paused=*/active_streams_count == 0); |
| 396 | bypass_mode_ = true; |
| 397 |
| 398 | DestroyStoredEncoders(); |
| 399 | inited_.store(1); |
| 400 | return WEBRTC_VIDEO_CODEC_OK; |
| 401 | } |
| 402 |
| 403 | encoder_context->Release(); |
| 404 | if (total_streams_count_ == 1) { |
| 405 | RTC_LOG(LS_ERROR) << "[SEA] InitEncode: failed with error code: " |
| 406 | << WebRtcVideoCodecErrorToString(ret); |
| 407 | return ret; |
| 408 | } |
| 409 | } |
| 410 |
| 411 | // Multi-encoder simulcast or singlecast (deactivated layers). |
| 412 | std::vector<uint32_t> stream_start_bitrate_kbps = |
| 413 | GetStreamStartBitratesKbps(codec_); |
| 414 |
| 415 | for (int stream_idx = 0; stream_idx < total_streams_count_; ++stream_idx) { |
| 416 | if (!is_legacy_singlecast && !codec_.simulcastStream[stream_idx].active) { |
| 417 | continue; |
| 418 | } |
| 419 |
| 420 | if (encoder_context == nullptr) { |
| 421 | encoder_context = FetchOrCreateEncoderContext( |
| 422 | /*is_lowest_quality_stream=*/stream_idx == lowest_quality_stream_idx, /*is_highest_quality_stream=*/stream_idx == highest_quality_stream_idx); |
| 423 |
| 424 | } |
| 425 | if (encoder_context == nullptr) { |
| 426 | Release(); |
| 427 | return WEBRTC_VIDEO_CODEC_MEMORY; |
| 428 | } |
| 429 |
| 430 | VideoCodec stream_codec = MakeStreamCodec( |
| 431 | codec_, stream_idx, stream_start_bitrate_kbps[stream_idx], |
| 432 | /*is_lowest_quality_stream=*/stream_idx == lowest_quality_stream_idx, |
| 433 | /*is_highest_quality_stream=*/stream_idx == highest_quality_stream_idx); |
| 434 |
| 435 | RTC_LOG(LS_INFO) << "[SEA] Multi-encoder mode: initializing stream: " |
| 436 | << stream_idx << ", active: " |
| 437 | << (codec_.simulcastStream[stream_idx].active ? "true" |
| 438 | : "false"); |
| 439 | int ret = encoder_context->encoder().InitEncode(&stream_codec, settings); |
| 440 | if (ret < 0) { |
| 441 | encoder_context.reset(); |
| 442 | Release(); |
| 443 | RTC_LOG(LS_ERROR) << "[SEA] InitEncode: failed with error code: " |
| 444 | << WebRtcVideoCodecErrorToString(ret); |
| 445 | return ret; |
| 446 | } |
| 447 |
| 448 | // Intercept frame encode complete callback only for upper streams, where |
| 449 | // we need to set a correct stream index. Set `parent` to nullptr for the |
| 450 | // lowest stream to bypass the callback. |
| 451 | CustomSimulcastEncoderAdapter* parent = stream_idx > 0 ? this : nullptr; |
| 452 |
| 453 | bool is_paused = stream_start_bitrate_kbps[stream_idx] == 0; |
| 454 | stream_contexts_.emplace_back( |
| 455 | parent, std::move(encoder_context), |
| 456 | std::make_unique<FramerateController>(stream_codec.maxFramerate), |
| 457 | stream_idx, stream_codec.width, stream_codec.height, is_paused); |
| 458 | } |
| 459 |
| 460 | // To save memory, don't store encoders that we don't use. |
| 461 | DestroyStoredEncoders(); |
| 462 |
| 463 | inited_.store(1); |
| 464 | return WEBRTC_VIDEO_CODEC_OK; |
| 465 | } |
| 466 |
| 467 | int CustomSimulcastEncoderAdapter::Encode( |
| 468 | const VideoFrame& input_image, |
| 469 | const std::vector<VideoFrameType>* frame_types) { |
| 470 | RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 471 |
| 472 | if (!Initialized()) { |
| 473 | return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
| 474 | } |
| 475 | if (encoded_complete_callback_ == nullptr) { |
| 476 | return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
| 477 | } |
| 478 |
| 479 | if (encoder_info_override_.requested_resolution_alignment()) { |
| 480 | const int alignment = |
| 481 | *encoder_info_override_.requested_resolution_alignment(); |
| 482 | if (input_image.width() % alignment != 0 || |
| 483 | input_image.height() % alignment != 0) { |
| 484 | RTC_LOG(LS_WARNING) << "Frame " << input_image.width() << "x" |
| 485 | << input_image.height() << " not divisible by " |
| 486 | << alignment; |
| 487 | return WEBRTC_VIDEO_CODEC_ERROR; |
| 488 | } |
| 489 | if (encoder_info_override_.apply_alignment_to_all_simulcast_layers()) { |
| 490 | for (const auto& layer : stream_contexts_) { |
| 491 | if (layer.width() % alignment != 0 || layer.height() % alignment != 0) { |
| 492 | RTC_LOG(LS_WARNING) |
| 493 | << "Codec " << layer.width() << "x" << layer.height() |
| 494 | << " not divisible by " << alignment; |
| 495 | return WEBRTC_VIDEO_CODEC_ERROR; |
| 496 | } |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 |
| 501 | bool is_keyframe_needed = false; |
| 502 | for (const auto& layer : stream_contexts_) { |
| 503 | if (layer.is_keyframe_needed()) { |
| 504 | // This is legacy behavior, generating a keyframe on all layers |
| 505 | // when generating one for a layer that became active for the first time |
| 506 | // or after being disabled. |
| 507 | is_keyframe_needed = true; |
| 508 | break; |
| 509 | } |
| 510 | } |
| 511 |
| 512 | // Temporary thay may hold the result of texture to i420 buffer conversion. |
| 513 | rtc::scoped_refptr<VideoFrameBuffer> src_buffer; |
| 514 | int src_width = input_image.width(); |
| 515 | int src_height = input_image.height(); |
| 516 |
| 517 | for (auto& layer : stream_contexts_) { |
| 518 | // Don't encode frames in resolutions that we don't intend to send. |
| 519 | if (layer.is_paused()) { |
| 520 | continue; |
| 521 | } |
| 522 |
| 523 | // Convert timestamp from RTP 90kHz clock. |
| 524 | const Timestamp frame_timestamp = |
| 525 | Timestamp::Micros((1000 * input_image.timestamp()) / 90); |
| 526 |
| 527 | // If adapter is passed through and only one sw encoder does simulcast, |
| 528 | // frame types for all streams should be passed to the encoder unchanged. |
| 529 | // Otherwise a single per-encoder frame type is passed. |
| 530 | std::vector<VideoFrameType> stream_frame_types( |
| 531 | bypass_mode_ |
| 532 | ? std::max<unsigned char>(codec_.numberOfSimulcastStreams, 1) |
| 533 | : 1, |
| 534 | VideoFrameType::kVideoFrameDelta); |
| 535 |
| 536 | bool keyframe_requested = false; |
| 537 | if (is_keyframe_needed) { |
| 538 | std::fill(stream_frame_types.begin(), stream_frame_types.end(), |
| 539 | VideoFrameType::kVideoFrameKey); |
| 540 | keyframe_requested = true; |
| 541 | } else if (frame_types) { |
| 542 | if (bypass_mode_) { |
| 543 | // In bypass mode, we effectively pass on frame_types. |
| 544 | RTC_DCHECK_EQ(frame_types->size(), stream_frame_types.size()); |
| 545 | stream_frame_types = *frame_types; |
| 546 | keyframe_requested = |
| 547 | absl::c_any_of(*frame_types, [](const VideoFrameType frame_type) { |
| 548 | return frame_type == VideoFrameType::kVideoFrameKey; |
| 549 | }); |
| 550 | } else { |
| 551 | size_t stream_idx = static_cast<size_t>(layer.stream_idx()); |
| 552 | if (frame_types->size() >= stream_idx && |
| 553 | (*frame_types)[stream_idx] == VideoFrameType::kVideoFrameKey) { |
| 554 | stream_frame_types[0] = VideoFrameType::kVideoFrameKey; |
| 555 | keyframe_requested = true; |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | if (keyframe_requested) { |
| 560 | layer.OnKeyframe(frame_timestamp); |
| 561 | } else if (layer.ShouldDropFrame(frame_timestamp)) { |
| 562 | continue; |
| 563 | } |
| 564 |
| 565 | // If scaling isn't required, because the input resolution |
| 566 | // matches the destination or the input image is empty (e.g. |
| 567 | // a keyframe request for encoders with internal camera |
| 568 | // sources) or the source image has a native handle, pass the image on |
| 569 | // directly. Otherwise, we'll scale it to match what the encoder expects |
| 570 | // (below). |
| 571 | // For texture frames, the underlying encoder is expected to be able to |
| 572 | // correctly sample/scale the source texture. |
| 573 | // TODO(perkj): ensure that works going forward, and figure out how this |
| 574 | // affects webrtc:5683. |
| 575 | if (layer.width() == src_width && layer.height() == src_height) { |
| 576 | int ret = layer.encoder().Encode(input_image, &stream_frame_types); |
| 577 | if (ret != WEBRTC_VIDEO_CODEC_OK) { |
| 578 | return ret; |
| 579 | } |
| 580 | } else { |
| 581 | if (src_buffer == nullptr) { |
| 582 | src_buffer = input_image.video_frame_buffer(); |
| 583 | } |
| 584 | rtc::scoped_refptr<VideoFrameBuffer> dst_buffer = |
| 585 | src_buffer->Scale(layer.width(), layer.height()); |
| 586 | if (!dst_buffer) { |
| 587 | RTC_LOG(LS_ERROR) << "Failed to scale video frame"; |
| 588 | return WEBRTC_VIDEO_CODEC_ENCODER_FAILURE; |
| 589 | } |
| 590 |
| 591 | // UpdateRect is not propagated to lower simulcast layers currently. |
| 592 | // TODO(ilnik): Consider scaling UpdateRect together with the buffer. |
| 593 | VideoFrame frame(input_image); |
| 594 | frame.set_video_frame_buffer(dst_buffer); |
| 595 | frame.set_rotation(webrtc::kVideoRotation_0); |
| 596 | frame.set_update_rect( |
| 597 | VideoFrame::UpdateRect{0, 0, frame.width(), frame.height()}); |
| 598 | int ret = layer.encoder().Encode(frame, &stream_frame_types); |
| 599 | if (ret != WEBRTC_VIDEO_CODEC_OK) { |
| 600 | return ret; |
| 601 | } |
| 602 | } |
| 603 | } |
| 604 |
| 605 | return WEBRTC_VIDEO_CODEC_OK; |
| 606 | } |
| 607 |
| 608 | int CustomSimulcastEncoderAdapter::RegisterEncodeCompleteCallback( |
| 609 | EncodedImageCallback* callback) { |
| 610 | RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 611 | encoded_complete_callback_ = callback; |
| 612 | if (!stream_contexts_.empty() && stream_contexts_.front().stream_idx() == 0) { |
| 613 | // Bypass frame encode complete callback for the lowest layer since there is |
| 614 | // no need to override frame's spatial index. |
| 615 | stream_contexts_.front().encoder().RegisterEncodeCompleteCallback(callback); |
| 616 | } |
| 617 | return WEBRTC_VIDEO_CODEC_OK; |
| 618 | } |
| 619 |
| 620 | void CustomSimulcastEncoderAdapter::SetRates( |
| 621 | const RateControlParameters& parameters) { |
| 622 | RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 623 |
| 624 | if (!Initialized()) { |
| 625 | RTC_LOG(LS_WARNING) << "SetRates while not initialized"; |
| 626 | return; |
| 627 | } |
| 628 |
| 629 | if (parameters.framerate_fps < 1.0) { |
| 630 | RTC_LOG(LS_WARNING) << "Invalid framerate: " << parameters.framerate_fps; |
| 631 | return; |
| 632 | } |
| 633 |
| 634 | codec_.maxFramerate = static_cast<uint32_t>(parameters.framerate_fps + 0.5); |
| 635 |
| 636 | if (bypass_mode_) { |
| 637 | stream_contexts_.front().encoder().SetRates(parameters); |
| 638 | return; |
| 639 | } |
| 640 |
| 641 | for (StreamContext& layer_context : stream_contexts_) { |
| 642 | int stream_idx = layer_context.stream_idx(); |
| 643 | uint32_t stream_bitrate_kbps = |
| 644 | parameters.bitrate.GetSpatialLayerSum(stream_idx) / 1000; |
| 645 |
| 646 | // Need a key frame if we have not sent this stream before. |
| 647 | if (stream_bitrate_kbps > 0 && layer_context.is_paused()) { |
| 648 | layer_context.set_is_keyframe_needed(); |
| 649 | } |
| 650 | layer_context.set_is_paused(stream_bitrate_kbps == 0); |
| 651 |
| 652 | // Slice the temporal layers out of the full allocation and pass it on to |
| 653 | // the encoder handling the current simulcast stream. |
| 654 | RateControlParameters stream_parameters = parameters; |
| 655 | stream_parameters.bitrate = VideoBitrateAllocation(); |
| 656 | for (int i = 0; i < kMaxTemporalStreams; ++i) { |
| 657 | if (parameters.bitrate.HasBitrate(stream_idx, i)) { |
| 658 | stream_parameters.bitrate.SetBitrate( |
| 659 | 0, i, parameters.bitrate.GetBitrate(stream_idx, i)); |
| 660 | } |
| 661 | } |
| 662 |
| 663 | // Assign link allocation proportionally to spatial layer allocation. |
| 664 | if (!parameters.bandwidth_allocation.IsZero() && |
| 665 | parameters.bitrate.get_sum_bps() > 0) { |
| 666 | stream_parameters.bandwidth_allocation = |
| 667 | DataRate::BitsPerSec((parameters.bandwidth_allocation.bps() * |
| 668 | stream_parameters.bitrate.get_sum_bps()) / |
| 669 | parameters.bitrate.get_sum_bps()); |
| 670 | // Make sure we don't allocate bandwidth lower than target bitrate. |
| 671 | if (stream_parameters.bandwidth_allocation.bps() < |
| 672 | stream_parameters.bitrate.get_sum_bps()) { |
| 673 | stream_parameters.bandwidth_allocation = |
| 674 | DataRate::BitsPerSec(stream_parameters.bitrate.get_sum_bps()); |
| 675 | } |
| 676 | } |
| 677 |
| 678 | stream_parameters.framerate_fps = std::min<double>( |
| 679 | parameters.framerate_fps, |
| 680 | layer_context.target_fps().value_or(parameters.framerate_fps)); |
| 681 |
| 682 | layer_context.encoder().SetRates(stream_parameters); |
| 683 | } |
| 684 | } |
| 685 |
| 686 | void CustomSimulcastEncoderAdapter::OnPacketLossRateUpdate(float packet_loss_rate) { |
| 687 | for (auto& c : stream_contexts_) { |
| 688 | c.encoder().OnPacketLossRateUpdate(packet_loss_rate); |
| 689 | } |
| 690 | } |
| 691 |
| 692 | void CustomSimulcastEncoderAdapter::OnRttUpdate(int64_t rtt_ms) { |
| 693 | for (auto& c : stream_contexts_) { |
| 694 | c.encoder().OnRttUpdate(rtt_ms); |
| 695 | } |
| 696 | } |
| 697 |
| 698 | void CustomSimulcastEncoderAdapter::OnLossNotification( |
| 699 | const LossNotification& loss_notification) { |
| 700 | for (auto& c : stream_contexts_) { |
| 701 | c.encoder().OnLossNotification(loss_notification); |
| 702 | } |
| 703 | } |
| 704 |
| 705 | // TODO(brandtr): Add task checker to this member function, when all encoder |
| 706 | // callbacks are coming in on the encoder queue. |
| 707 | EncodedImageCallback::Result CustomSimulcastEncoderAdapter::OnEncodedImage( |
| 708 | size_t stream_idx, |
| 709 | const EncodedImage& encodedImage, |
| 710 | const CodecSpecificInfo* codecSpecificInfo) { |
| 711 | EncodedImage stream_image(encodedImage); |
| 712 | CodecSpecificInfo stream_codec_specific = *codecSpecificInfo; |
| 713 |
| 714 | stream_image.SetSimulcastIndex(stream_idx); |
| 715 |
| 716 | return encoded_complete_callback_->OnEncodedImage(stream_image, |
| 717 | &stream_codec_specific); |
| 718 | } |
| 719 |
| 720 | void CustomSimulcastEncoderAdapter::OnDroppedFrame(size_t stream_idx) { |
| 721 | // Not yet implemented. |
| 722 | } |
| 723 |
| 724 | bool CustomSimulcastEncoderAdapter::Initialized() const { |
| 725 | return inited_.load() == 1; |
| 726 | } |
| 727 |
| 728 | void CustomSimulcastEncoderAdapter::DestroyStoredEncoders() { |
| 729 | while (!cached_encoder_contexts_.empty()) { |
| 730 | cached_encoder_contexts_.pop_back(); |
| 731 | } |
| 732 | } |
| 733 |
| 734 | std::unique_ptr<CustomSimulcastEncoderAdapter::EncoderContext> |
| 735 | CustomSimulcastEncoderAdapter::FetchOrCreateEncoderContext( |
| 736 | bool is_lowest_quality_stream, bool is_highest_quality_stream) const { |
| 737 | bool prefer_temporal_support = false;/*fallback_encoder_factory_ != nullptr && |
| 738 | is_lowest_quality_stream && |
| 739 | prefer_temporal_support_on_base_layer_;*/ |
| 740 |
| 741 |
| 742 | // Toggling of `prefer_temporal_support` requires encoder recreation. Find |
| 743 | // and reuse encoder with desired `prefer_temporal_support`. Otherwise, if |
| 744 | // there is no such encoder in the cache, create a new instance. |
| 745 | auto encoder_context_iter = |
| 746 | std::find_if(cached_encoder_contexts_.begin(), |
| 747 | cached_encoder_contexts_.end(), [&](auto& encoder_context) { |
| 748 | return encoder_context->prefer_temporal_support() == |
| 749 | prefer_temporal_support; |
| 750 | }); |
| 751 |
| 752 | std::unique_ptr<CustomSimulcastEncoderAdapter::EncoderContext> encoder_context; |
| 753 | if (encoder_context_iter != cached_encoder_contexts_.end()) { |
| 754 | encoder_context = std::move(*encoder_context_iter); |
| 755 | cached_encoder_contexts_.erase(encoder_context_iter); |
| 756 | } else { |
| 757 | std::unique_ptr<VideoEncoder> primary_encoder; |
| 758 | if (!primary_encoder && is_highest_quality_stream && hardware_encoder_factory_) { |
| 759 | primary_encoder = hardware_encoder_factory_->CreateVideoEncoder(video_format_); |
| 760 | } |
| 761 | if (!primary_encoder) { |
| 762 | primary_encoder = primary_encoder_factory_->CreateVideoEncoder(video_format_); |
| 763 | } |
| 764 |
| 765 | std::unique_ptr<VideoEncoder> fallback_encoder; |
| 766 | /*if (fallback_encoder_factory_ != nullptr) { |
| 767 | fallback_encoder = |
| 768 | fallback_encoder_factory_->CreateVideoEncoder(video_format_); |
| 769 | }*/ |
| 770 |
| 771 | std::unique_ptr<VideoEncoder> encoder; |
| 772 | VideoEncoder::EncoderInfo primary_info; |
| 773 | VideoEncoder::EncoderInfo fallback_info; |
| 774 |
| 775 | if (primary_encoder != nullptr) { |
| 776 | primary_info = primary_encoder->GetEncoderInfo(); |
| 777 | fallback_info = primary_info; |
| 778 |
| 779 | if (fallback_encoder == nullptr) { |
| 780 | encoder = std::move(primary_encoder); |
| 781 | } else { |
| 782 | encoder = CreateVideoEncoderSoftwareFallbackWrapper( |
| 783 | std::move(fallback_encoder), std::move(primary_encoder), |
| 784 | prefer_temporal_support); |
| 785 | } |
| 786 | } else if (fallback_encoder != nullptr) { |
| 787 | RTC_LOG(LS_WARNING) << "Failed to create primary " << video_format_.name |
| 788 | << " encoder. Use fallback encoder."; |
| 789 | fallback_info = fallback_encoder->GetEncoderInfo(); |
| 790 | primary_info = fallback_info; |
| 791 | encoder = std::move(fallback_encoder); |
| 792 | } else { |
| 793 | RTC_LOG(LS_ERROR) << "Failed to create primary and fallback " |
| 794 | << video_format_.name << " encoders."; |
| 795 | return nullptr; |
| 796 | } |
| 797 |
| 798 | encoder_context = std::make_unique<CustomSimulcastEncoderAdapter::EncoderContext>( |
| 799 | std::move(encoder), prefer_temporal_support, primary_info, |
| 800 | fallback_info); |
| 801 | } |
| 802 |
| 803 | encoder_context->encoder().RegisterEncodeCompleteCallback( |
| 804 | encoded_complete_callback_); |
| 805 | return encoder_context; |
| 806 | } |
| 807 |
| 808 | webrtc::VideoCodec CustomSimulcastEncoderAdapter::MakeStreamCodec( |
| 809 | const webrtc::VideoCodec& codec, |
| 810 | int stream_idx, |
| 811 | uint32_t start_bitrate_kbps, |
| 812 | bool is_lowest_quality_stream, |
| 813 | bool is_highest_quality_stream) { |
| 814 | webrtc::VideoCodec codec_params = codec; |
| 815 | const SimulcastStream& stream_params = codec.simulcastStream[stream_idx]; |
| 816 |
| 817 | codec_params.numberOfSimulcastStreams = 0; |
| 818 | codec_params.width = stream_params.width; |
| 819 | codec_params.height = stream_params.height; |
| 820 | codec_params.maxBitrate = stream_params.maxBitrate; |
| 821 | codec_params.minBitrate = stream_params.minBitrate; |
| 822 | codec_params.maxFramerate = stream_params.maxFramerate; |
| 823 | codec_params.qpMax = stream_params.qpMax; |
| 824 | codec_params.active = stream_params.active; |
| 825 | // By default, `scalability_mode` comes from SimulcastStream when |
| 826 | // CustomSimulcastEncoderAdapter is used. This allows multiple encodings of L1Tx, |
| 827 | // but SimulcastStream currently does not support multiple spatial layers. |
| 828 | ScalabilityMode scalability_mode = stream_params.GetScalabilityMode(); |
| 829 | // To support the full set of scalability modes in the event that this is the |
| 830 | // only active encoding, prefer VideoCodec::GetScalabilityMode() if all other |
| 831 | // encodings are inactive. |
| 832 | if (codec.GetScalabilityMode().has_value()) { |
| 833 | bool only_active_stream = true; |
| 834 | for (int i = 0; i < codec.numberOfSimulcastStreams; ++i) { |
| 835 | if (i != stream_idx && codec.simulcastStream[i].active) { |
| 836 | only_active_stream = false; |
| 837 | break; |
| 838 | } |
| 839 | } |
| 840 | if (only_active_stream) { |
| 841 | scalability_mode = codec.GetScalabilityMode().value(); |
| 842 | } |
| 843 | } |
| 844 | codec_params.SetScalabilityMode(scalability_mode); |
| 845 | // Settings that are based on stream/resolution. |
| 846 | if (is_lowest_quality_stream) { |
| 847 | // Settings for lowest spatial resolutions. |
| 848 | if (codec.mode == VideoCodecMode::kScreensharing) { |
| 849 | if (experimental_boosted_screenshare_qp_) { |
| 850 | codec_params.qpMax = *experimental_boosted_screenshare_qp_; |
| 851 | } |
| 852 | } else if (boost_base_layer_quality_) { |
| 853 | codec_params.qpMax = kLowestResMaxQp; |
| 854 | } |
| 855 | } |
| 856 | if (codec.codecType == webrtc::kVideoCodecVP8) { |
| 857 | codec_params.VP8()->numberOfTemporalLayers = |
| 858 | stream_params.numberOfTemporalLayers; |
| 859 | if (!is_highest_quality_stream) { |
| 860 | // For resolutions below CIF, set the codec `complexity` parameter to |
| 861 | // kComplexityHigher, which maps to cpu_used = -4. |
| 862 | int pixels_per_frame = codec_params.width * codec_params.height; |
| 863 | if (pixels_per_frame < 352 * 288) { |
| 864 | codec_params.SetVideoEncoderComplexity( |
| 865 | webrtc::VideoCodecComplexity::kComplexityHigher); |
| 866 | } |
| 867 | // Turn off denoising for all streams but the highest resolution. |
| 868 | codec_params.VP8()->denoisingOn = false; |
| 869 | } |
| 870 | } else if (codec.codecType == webrtc::kVideoCodecH264) { |
| 871 | codec_params.H264()->numberOfTemporalLayers = |
| 872 | stream_params.numberOfTemporalLayers; |
| 873 | } |
| 874 |
| 875 | // Cap start bitrate to the min bitrate in order to avoid strange codec |
| 876 | // behavior. |
| 877 | codec_params.startBitrate = |
| 878 | std::max(stream_params.minBitrate, start_bitrate_kbps); |
| 879 |
| 880 | // Legacy screenshare mode is only enabled for the first simulcast layer |
| 881 | codec_params.legacy_conference_mode = |
| 882 | codec.legacy_conference_mode && stream_idx == 0; |
| 883 |
| 884 | return codec_params; |
| 885 | } |
| 886 |
| 887 | void CustomSimulcastEncoderAdapter::OverrideFromFieldTrial( |
| 888 | VideoEncoder::EncoderInfo* info) const { |
| 889 | if (encoder_info_override_.requested_resolution_alignment()) { |
| 890 | info->requested_resolution_alignment = cricket::LeastCommonMultiple( |
| 891 | info->requested_resolution_alignment, |
| 892 | *encoder_info_override_.requested_resolution_alignment()); |
| 893 | info->apply_alignment_to_all_simulcast_layers = |
| 894 | info->apply_alignment_to_all_simulcast_layers || |
| 895 | encoder_info_override_.apply_alignment_to_all_simulcast_layers(); |
| 896 | } |
| 897 | // Override resolution bitrate limits unless they're set already. |
| 898 | if (info->resolution_bitrate_limits.empty() && |
| 899 | !encoder_info_override_.resolution_bitrate_limits().empty()) { |
| 900 | info->resolution_bitrate_limits = |
| 901 | encoder_info_override_.resolution_bitrate_limits(); |
| 902 | } |
| 903 | } |
| 904 |
| 905 | VideoEncoder::EncoderInfo CustomSimulcastEncoderAdapter::GetEncoderInfo() const { |
| 906 | if (stream_contexts_.size() == 1) { |
| 907 | // Not using simulcast adapting functionality, just pass through. |
| 908 | VideoEncoder::EncoderInfo info = |
| 909 | stream_contexts_.front().encoder().GetEncoderInfo(); |
| 910 | OverrideFromFieldTrial(&info); |
| 911 | return info; |
| 912 | } |
| 913 |
| 914 | VideoEncoder::EncoderInfo encoder_info; |
| 915 | encoder_info.implementation_name = "CustomSimulcastEncoderAdapter"; |
| 916 | encoder_info.requested_resolution_alignment = 1; |
| 917 | encoder_info.apply_alignment_to_all_simulcast_layers = false; |
| 918 | encoder_info.supports_native_handle = true; |
| 919 | encoder_info.scaling_settings.thresholds = absl::nullopt; |
| 920 |
| 921 | if (stream_contexts_.empty()) { |
| 922 | // GetEncoderInfo queried before InitEncode. Only alignment info is needed |
| 923 | // to be filled. |
| 924 | // Create one encoder and query it. |
| 925 |
| 926 | std::unique_ptr<CustomSimulcastEncoderAdapter::EncoderContext> encoder_context = |
| 927 | FetchOrCreateEncoderContext(/*is_lowest_quality_stream=*/true, false); |
| 928 | if (encoder_context == nullptr) { |
| 929 | return encoder_info; |
| 930 | } |
| 931 |
| 932 | const VideoEncoder::EncoderInfo& primary_info = |
| 933 | encoder_context->PrimaryInfo(); |
| 934 | const VideoEncoder::EncoderInfo& fallback_info = |
| 935 | encoder_context->FallbackInfo(); |
| 936 |
| 937 | encoder_info.requested_resolution_alignment = cricket::LeastCommonMultiple( |
| 938 | primary_info.requested_resolution_alignment, |
| 939 | fallback_info.requested_resolution_alignment); |
| 940 |
| 941 | encoder_info.apply_alignment_to_all_simulcast_layers = |
| 942 | primary_info.apply_alignment_to_all_simulcast_layers || |
| 943 | fallback_info.apply_alignment_to_all_simulcast_layers; |
| 944 |
| 945 | if (!primary_info.supports_simulcast || !fallback_info.supports_simulcast) { |
| 946 | encoder_info.apply_alignment_to_all_simulcast_layers = true; |
| 947 | } |
| 948 |
| 949 | cached_encoder_contexts_.emplace_back(std::move(encoder_context)); |
| 950 |
| 951 | OverrideFromFieldTrial(&encoder_info); |
| 952 | return encoder_info; |
| 953 | } |
| 954 |
| 955 | encoder_info.scaling_settings = VideoEncoder::ScalingSettings::kOff; |
| 956 |
| 957 | for (size_t i = 0; i < stream_contexts_.size(); ++i) { |
| 958 | VideoEncoder::EncoderInfo encoder_impl_info = |
| 959 | stream_contexts_[i].encoder().GetEncoderInfo(); |
| 960 | if (i == 0) { |
| 961 | // Encoder name indicates names of all sub-encoders. |
| 962 | encoder_info.implementation_name += " ("; |
| 963 | encoder_info.implementation_name += encoder_impl_info.implementation_name; |
| 964 |
| 965 | encoder_info.supports_native_handle = |
| 966 | encoder_impl_info.supports_native_handle; |
| 967 | encoder_info.has_trusted_rate_controller = |
| 968 | encoder_impl_info.has_trusted_rate_controller; |
| 969 | encoder_info.is_hardware_accelerated = |
| 970 | encoder_impl_info.is_hardware_accelerated; |
| 971 | encoder_info.is_qp_trusted = encoder_impl_info.is_qp_trusted; |
| 972 | } else { |
| 973 | encoder_info.implementation_name += ", "; |
| 974 | encoder_info.implementation_name += encoder_impl_info.implementation_name; |
| 975 |
| 976 | // Native handle supported if any encoder supports it. |
| 977 | encoder_info.supports_native_handle |= |
| 978 | encoder_impl_info.supports_native_handle; |
| 979 |
| 980 | // Trusted rate controller only if all encoders have it. |
| 981 | encoder_info.has_trusted_rate_controller &= |
| 982 | encoder_impl_info.has_trusted_rate_controller; |
| 983 |
| 984 | // Uses hardware support if any of the encoders uses it. |
| 985 | // For example, if we are having issues with down-scaling due to |
| 986 | // pipelining delay in HW encoders we need higher encoder usage |
| 987 | // thresholds in CPU adaptation. |
| 988 | encoder_info.is_hardware_accelerated |= |
| 989 | encoder_impl_info.is_hardware_accelerated; |
| 990 |
| 991 | // Treat QP from frame/slice/tile header as average QP only if all |
| 992 | // encoders report it as average QP. |
| 993 | encoder_info.is_qp_trusted = |
| 994 | encoder_info.is_qp_trusted.value_or(true) && |
| 995 | encoder_impl_info.is_qp_trusted.value_or(true); |
| 996 | } |
| 997 | encoder_info.fps_allocation[i] = encoder_impl_info.fps_allocation[0]; |
| 998 | encoder_info.requested_resolution_alignment = cricket::LeastCommonMultiple( |
| 999 | encoder_info.requested_resolution_alignment, |
| 1000 | encoder_impl_info.requested_resolution_alignment); |
| 1001 | // request alignment on all layers if any of the encoders may need it, or |
| 1002 | // if any non-top layer encoder requests a non-trivial alignment. |
| 1003 | if (encoder_impl_info.apply_alignment_to_all_simulcast_layers || |
| 1004 | (encoder_impl_info.requested_resolution_alignment > 1 && |
| 1005 | (codec_.simulcastStream[i].height < codec_.height || |
| 1006 | codec_.simulcastStream[i].width < codec_.width))) { |
| 1007 | encoder_info.apply_alignment_to_all_simulcast_layers = true; |
| 1008 | } |
| 1009 | } |
| 1010 | encoder_info.implementation_name += ")"; |
| 1011 |
| 1012 | OverrideFromFieldTrial(&encoder_info); |
| 1013 |
| 1014 | return encoder_info; |
| 1015 | } |
| 1016 |
| 1017 | } // namespace webrtc |
| 1018 |