From 108c94b1d4c8c43dfaeb78c74c32625c27a9e703 Mon Sep 17 00:00:00 2001 From: Sergey Silkin Date: Fri, 12 Jul 2024 15:15:23 +0200 Subject: [PATCH] Do not expose GetNormalSimulcastLayers and GetScreenshareLayers This is a cleanup of simulcast.cc. Remove GetNormalSimulcastLayers and GetScreenshareLayers from simulcast.h. Move the implementations to anonymous namespace in simulcast.cc. Bug: webrtc:351644568, b/352504711 Change-Id: Iff03161e5c44cb0e7faa60b16cfc2fc9b903d5ce Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/357103 Reviewed-by: Ilya Nikolaevskiy Commit-Queue: Sergey Silkin Cr-Commit-Position: refs/heads/main@{#42635} --- video/config/simulcast.cc | 218 +++++++++++++++++++------------------- video/config/simulcast.h | 19 ---- 2 files changed, 109 insertions(+), 128 deletions(-) diff --git a/video/config/simulcast.cc b/video/config/simulcast.cc index 14c0b94750..ce20cec4ad 100644 --- a/video/config/simulcast.cc +++ b/video/config/simulcast.cc @@ -216,24 +216,6 @@ int FindSimulcastFormatIndex(int width, return -1; } -} // namespace - -// Round size to nearest simulcast-friendly size. -// Simulcast stream width and height must both be dividable by -// |2 ^ (simulcast_layers - 1)|. -int NormalizeSimulcastSize(const FieldTrialsView& field_trials, - int size, - size_t simulcast_layers) { - int base2_exponent = static_cast(simulcast_layers) - 1; - const absl::optional experimental_base2_exponent = - webrtc::NormalizeSimulcastSizeExperiment::GetBase2Exponent(field_trials); - if (experimental_base2_exponent && - (size > (1 << *experimental_base2_exponent))) { - base2_exponent = *experimental_base2_exponent; - } - return ((size >> base2_exponent) << base2_exponent); -} - SimulcastFormat InterpolateSimulcastFormat( int width, int height, @@ -267,97 +249,6 @@ SimulcastFormat InterpolateSimulcastFormat( return {width, height, max_layers, max_bitrate, target_bitrate, min_bitrate}; } -void BoostMaxSimulcastLayer(webrtc::DataRate max_bitrate, - std::vector* layers) { - if (layers->empty()) - return; - - const webrtc::DataRate total_bitrate = GetTotalMaxBitrate(*layers); - - // We're still not using all available bits. - if (total_bitrate < max_bitrate) { - // Spend additional bits to boost the max layer. - const webrtc::DataRate bitrate_left = max_bitrate - total_bitrate; - layers->back().max_bitrate_bps += bitrate_left.bps(); - } -} - -webrtc::DataRate GetTotalMaxBitrate( - const std::vector& layers) { - if (layers.empty()) - return webrtc::DataRate::Zero(); - - int total_max_bitrate_bps = 0; - for (size_t s = 0; s < layers.size() - 1; ++s) { - total_max_bitrate_bps += layers[s].target_bitrate_bps; - } - total_max_bitrate_bps += layers.back().max_bitrate_bps; - return webrtc::DataRate::BitsPerSec(total_max_bitrate_bps); -} - -size_t LimitSimulcastLayerCount(int width, - int height, - size_t need_layers, - size_t layer_count, - const webrtc::FieldTrialsView& trials, - webrtc::VideoCodecType codec) { - if (!absl::StartsWith(trials.Lookup(kUseLegacySimulcastLayerLimitFieldTrial), - "Disabled")) { - // Max layers from one higher resolution in kSimulcastFormats will be used - // if the ratio (pixels_up - pixels) / (pixels_up - pixels_down) is less - // than configured `max_ratio`. pixels_down is the selected index in - // kSimulcastFormats based on pixels. - webrtc::FieldTrialOptional max_ratio("max_ratio"); - webrtc::ParseFieldTrial({&max_ratio}, - trials.Lookup("WebRTC-SimulcastLayerLimitRoundUp")); - - const bool enable_lowres_bitrate_interpolation = - EnableLowresBitrateInterpolation(trials); - size_t adaptive_layer_count = std::max( - need_layers, - InterpolateSimulcastFormat(width, height, max_ratio.GetOptional(), - enable_lowres_bitrate_interpolation, codec) - .max_layers); - if (layer_count > adaptive_layer_count) { - RTC_LOG(LS_WARNING) << "Reducing simulcast layer count from " - << layer_count << " to " << adaptive_layer_count; - layer_count = adaptive_layer_count; - } - } - return layer_count; -} - -std::vector GetSimulcastConfig( - size_t min_layers, - size_t max_layers, - int width, - int height, - bool is_screenshare_with_conference_mode, - bool temporal_layers_supported, - const webrtc::FieldTrialsView& trials, - webrtc::VideoCodecType codec) { - RTC_DCHECK_LE(min_layers, max_layers); - RTC_DCHECK(max_layers > 1 || is_screenshare_with_conference_mode); - - const bool base_heavy_tl3_rate_alloc = - webrtc::RateControlSettings(trials).Vp8BaseHeavyTl3RateAllocation(); - if (is_screenshare_with_conference_mode) { - return GetScreenshareLayers(max_layers, width, height, - temporal_layers_supported, - base_heavy_tl3_rate_alloc, trials); - } else { - // Some applications rely on the old behavior limiting the simulcast layer - // count based on the resolution automatically, which they can get through - // the WebRTC-LegacySimulcastLayerLimit field trial until they update. - max_layers = LimitSimulcastLayerCount(width, height, min_layers, max_layers, - trials, codec); - - return GetNormalSimulcastLayers(max_layers, width, height, - temporal_layers_supported, - base_heavy_tl3_rate_alloc, trials, codec); - } -} - std::vector GetNormalSimulcastLayers( size_t layer_count, int width, @@ -492,4 +383,113 @@ std::vector GetScreenshareLayers( return layers; } +size_t LimitSimulcastLayerCount(int width, + int height, + size_t need_layers, + size_t layer_count, + const webrtc::FieldTrialsView& trials, + webrtc::VideoCodecType codec) { + if (!absl::StartsWith(trials.Lookup(kUseLegacySimulcastLayerLimitFieldTrial), + "Disabled")) { + // Max layers from one higher resolution in kSimulcastFormats will be used + // if the ratio (pixels_up - pixels) / (pixels_up - pixels_down) is less + // than configured `max_ratio`. pixels_down is the selected index in + // kSimulcastFormats based on pixels. + webrtc::FieldTrialOptional max_ratio("max_ratio"); + webrtc::ParseFieldTrial({&max_ratio}, + trials.Lookup("WebRTC-SimulcastLayerLimitRoundUp")); + + const bool enable_lowres_bitrate_interpolation = + EnableLowresBitrateInterpolation(trials); + size_t adaptive_layer_count = std::max( + need_layers, + InterpolateSimulcastFormat(width, height, max_ratio.GetOptional(), + enable_lowres_bitrate_interpolation, codec) + .max_layers); + if (layer_count > adaptive_layer_count) { + RTC_LOG(LS_WARNING) << "Reducing simulcast layer count from " + << layer_count << " to " << adaptive_layer_count; + layer_count = adaptive_layer_count; + } + } + return layer_count; +} + +} // namespace + +// Round size to nearest simulcast-friendly size. +// Simulcast stream width and height must both be dividable by +// |2 ^ (simulcast_layers - 1)|. +int NormalizeSimulcastSize(const FieldTrialsView& field_trials, + int size, + size_t simulcast_layers) { + int base2_exponent = static_cast(simulcast_layers) - 1; + const absl::optional experimental_base2_exponent = + webrtc::NormalizeSimulcastSizeExperiment::GetBase2Exponent(field_trials); + if (experimental_base2_exponent && + (size > (1 << *experimental_base2_exponent))) { + base2_exponent = *experimental_base2_exponent; + } + return ((size >> base2_exponent) << base2_exponent); +} + +void BoostMaxSimulcastLayer(webrtc::DataRate max_bitrate, + std::vector* layers) { + if (layers->empty()) + return; + + const webrtc::DataRate total_bitrate = GetTotalMaxBitrate(*layers); + + // We're still not using all available bits. + if (total_bitrate < max_bitrate) { + // Spend additional bits to boost the max layer. + const webrtc::DataRate bitrate_left = max_bitrate - total_bitrate; + layers->back().max_bitrate_bps += bitrate_left.bps(); + } +} + +webrtc::DataRate GetTotalMaxBitrate( + const std::vector& layers) { + if (layers.empty()) + return webrtc::DataRate::Zero(); + + int total_max_bitrate_bps = 0; + for (size_t s = 0; s < layers.size() - 1; ++s) { + total_max_bitrate_bps += layers[s].target_bitrate_bps; + } + total_max_bitrate_bps += layers.back().max_bitrate_bps; + return webrtc::DataRate::BitsPerSec(total_max_bitrate_bps); +} + +std::vector GetSimulcastConfig( + size_t min_layers, + size_t max_layers, + int width, + int height, + bool is_screenshare_with_conference_mode, + bool temporal_layers_supported, + const webrtc::FieldTrialsView& trials, + webrtc::VideoCodecType codec) { + RTC_DCHECK_LE(min_layers, max_layers); + RTC_DCHECK(max_layers > 1 || is_screenshare_with_conference_mode); + + const bool base_heavy_tl3_rate_alloc = + webrtc::RateControlSettings(trials).Vp8BaseHeavyTl3RateAllocation(); + if (is_screenshare_with_conference_mode) { + return GetScreenshareLayers(max_layers, width, height, + temporal_layers_supported, + base_heavy_tl3_rate_alloc, trials); + } else { + // Some applications rely on the old behavior limiting the simulcast layer + // count based on the resolution automatically, which they can get through + // the WebRTC-LegacySimulcastLayerLimit field trial until they update. + max_layers = LimitSimulcastLayerCount(width, height, min_layers, max_layers, + trials, codec); + + return GetNormalSimulcastLayers(max_layers, width, height, + temporal_layers_supported, + base_heavy_tl3_rate_alloc, trials, codec); + } +} + } // namespace cricket diff --git a/video/config/simulcast.h b/video/config/simulcast.h index 2e9ab331a6..480cf4bb78 100644 --- a/video/config/simulcast.h +++ b/video/config/simulcast.h @@ -46,25 +46,6 @@ std::vector GetSimulcastConfig( const webrtc::FieldTrialsView& trials, webrtc::VideoCodecType codec); -// Gets the simulcast config layers for a non-screensharing case. -std::vector GetNormalSimulcastLayers( - size_t max_layers, - int width, - int height, - bool temporal_layers_supported, - bool base_heavy_tl3_rate_alloc, - const webrtc::FieldTrialsView& trials, - webrtc::VideoCodecType codec); - -// Gets simulcast config layers for screenshare settings. -std::vector GetScreenshareLayers( - size_t max_layers, - int width, - int height, - bool temporal_layers_supported, - bool base_heavy_tl3_rate_alloc, - const webrtc::FieldTrialsView& trials); - } // namespace cricket #endif // VIDEO_CONFIG_SIMULCAST_H_