| 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 "media/engine/simulcast.h" |
| 12 |
| 13 | #include <stdint.h> |
| 14 | #include <stdio.h> |
| 15 |
| 16 | #include <algorithm> |
| 17 | #include <string> |
| 18 |
| 19 | #include "absl/types/optional.h" |
| 20 | #include "api/video/video_codec_constants.h" |
| 21 | #include "media/base/media_constants.h" |
| 22 | #include "modules/video_coding/utility/simulcast_rate_allocator.h" |
| 23 | #include "rtc_base/arraysize.h" |
| 24 | #include "rtc_base/checks.h" |
| 25 | #include "rtc_base/experiments/min_video_bitrate_experiment.h" |
| 26 | #include "rtc_base/experiments/normalize_simulcast_size_experiment.h" |
| 27 | #include "rtc_base/experiments/rate_control_settings.h" |
| 28 | #include "rtc_base/logging.h" |
| 29 | #include "system_wrappers/include/field_trial.h" |
| 30 |
| 31 | namespace cricket { |
| 32 |
| 33 | namespace { |
| 34 |
| 35 | constexpr webrtc::DataRate Interpolate(const webrtc::DataRate& a, |
| 36 | const webrtc::DataRate& b, |
| 37 | float rate) { |
| 38 | return a * (1.0 - rate) + b * rate; |
| 39 | } |
| 40 |
| 41 | constexpr char kUseLegacySimulcastLayerLimitFieldTrial[] = |
| 42 | "WebRTC-LegacySimulcastLayerLimit"; |
| 43 |
| 44 | // Limits for legacy conference screensharing mode. Currently used for the |
| 45 | // lower of the two simulcast streams. |
| 46 | constexpr webrtc::DataRate kScreenshareDefaultTl0Bitrate = |
| 47 | webrtc::DataRate::KilobitsPerSec(200); |
| 48 | constexpr webrtc::DataRate kScreenshareDefaultTl1Bitrate = |
| 49 | webrtc::DataRate::KilobitsPerSec(1000); |
| 50 |
| 51 | // Min/max bitrate for the higher one of the two simulcast stream used for |
| 52 | // screen content. |
| 53 | constexpr webrtc::DataRate kScreenshareHighStreamMinBitrate = |
| 54 | webrtc::DataRate::KilobitsPerSec(600); |
| 55 | constexpr webrtc::DataRate kScreenshareHighStreamMaxBitrate = |
| 56 | webrtc::DataRate::KilobitsPerSec(1250); |
| 57 |
| 58 | } // namespace |
| 59 |
| 60 | struct SimulcastFormat { |
| 61 | int width; |
| 62 | int height; |
| 63 | // The maximum number of simulcast layers can be used for |
| 64 | // resolutions at |widthxheigh| for legacy applications. |
| 65 | size_t max_layers; |
| 66 | // The maximum bitrate for encoding stream at |widthxheight|, when we are |
| 67 | // not sending the next higher spatial stream. |
| 68 | webrtc::DataRate max_bitrate; |
| 69 | // The target bitrate for encoding stream at |widthxheight|, when this layer |
| 70 | // is not the highest layer (i.e., when we are sending another higher spatial |
| 71 | // stream). |
| 72 | webrtc::DataRate target_bitrate; |
| 73 | // The minimum bitrate needed for encoding stream at |widthxheight|. |
| 74 | webrtc::DataRate min_bitrate; |
| 75 | }; |
| 76 |
| 77 | // These tables describe from which resolution we can use how many |
| 78 | // simulcast layers at what bitrates (maximum, target, and minimum). |
| 79 | // Important!! Keep this table from high resolution to low resolution. |
| 80 | constexpr const SimulcastFormat kSimulcastFormats[] = { |
| 81 | {1920, 1080, 3, webrtc::DataRate::KilobitsPerSec(5000), |
| 82 | webrtc::DataRate::KilobitsPerSec(4000), |
| 83 | webrtc::DataRate::KilobitsPerSec(800)}, |
| 84 | {1280, 720, 3, webrtc::DataRate::KilobitsPerSec(2500), |
| 85 | webrtc::DataRate::KilobitsPerSec(2500), |
| 86 | webrtc::DataRate::KilobitsPerSec(600)}, |
| 87 | {960, 540, 3, webrtc::DataRate::KilobitsPerSec(1200), |
| 88 | webrtc::DataRate::KilobitsPerSec(1200), |
| 89 | webrtc::DataRate::KilobitsPerSec(350)}, |
| 90 | {640, 360, 2, webrtc::DataRate::KilobitsPerSec(700), |
| 91 | webrtc::DataRate::KilobitsPerSec(500), |
| 92 | webrtc::DataRate::KilobitsPerSec(150)}, |
| 93 | {480, 270, 2, webrtc::DataRate::KilobitsPerSec(450), |
| 94 | webrtc::DataRate::KilobitsPerSec(350), |
| 95 | webrtc::DataRate::KilobitsPerSec(150)}, |
| 96 | {320, 180, 1, webrtc::DataRate::KilobitsPerSec(200), |
| 97 | webrtc::DataRate::KilobitsPerSec(150), |
| 98 | webrtc::DataRate::KilobitsPerSec(30)}, |
| 99 | {0, 0, 1, webrtc::DataRate::KilobitsPerSec(200), |
| 100 | webrtc::DataRate::KilobitsPerSec(150), |
| 101 | webrtc::DataRate::KilobitsPerSec(30)}}; |
| 102 |
| 103 | const int kMaxScreenshareSimulcastLayers = 2; |
| 104 |
| 105 | // Multiway: Number of temporal layers for each simulcast stream. |
| 106 | int DefaultNumberOfTemporalLayers(int simulcast_id, bool screenshare) { |
| 107 | RTC_CHECK_GE(simulcast_id, 0); |
| 108 | RTC_CHECK_LT(simulcast_id, webrtc::kMaxSimulcastStreams); |
| 109 |
| 110 | const int kDefaultNumTemporalLayers = 3; |
| 111 | const int kDefaultNumScreenshareTemporalLayers = 2; |
| 112 | int default_num_temporal_layers = screenshare |
| 113 | ? kDefaultNumScreenshareTemporalLayers |
| 114 | : kDefaultNumTemporalLayers; |
| 115 |
| 116 | const std::string group_name = |
| 117 | screenshare ? webrtc::field_trial::FindFullName( |
| 118 | "WebRTC-VP8ScreenshareTemporalLayers") |
| 119 | : webrtc::field_trial::FindFullName( |
| 120 | "WebRTC-VP8ConferenceTemporalLayers"); |
| 121 | if (group_name.empty()) |
| 122 | return default_num_temporal_layers; |
| 123 |
| 124 | int num_temporal_layers = default_num_temporal_layers; |
| 125 | if (sscanf(group_name.c_str(), "%d", &num_temporal_layers) == 1 && |
| 126 | num_temporal_layers > 0 && |
| 127 | num_temporal_layers <= webrtc::kMaxTemporalStreams) { |
| 128 | return num_temporal_layers; |
| 129 | } |
| 130 |
| 131 | RTC_LOG(LS_WARNING) << "Attempt to set number of temporal layers to " |
| 132 | "incorrect value: " |
| 133 | << group_name; |
| 134 |
| 135 | return default_num_temporal_layers; |
| 136 | } |
| 137 |
| 138 | int FindSimulcastFormatIndex(int width, int height) { |
| 139 | RTC_DCHECK_GE(width, 0); |
| 140 | RTC_DCHECK_GE(height, 0); |
| 141 | for (uint32_t i = 0; i < arraysize(kSimulcastFormats); ++i) { |
| 142 | if (width * height >= |
| 143 | kSimulcastFormats[i].width * kSimulcastFormats[i].height) { |
| 144 | return i; |
| 145 | } |
| 146 | } |
| 147 | RTC_NOTREACHED(); |
| 148 | return -1; |
| 149 | } |
| 150 |
| 151 | // Round size to nearest simulcast-friendly size. |
| 152 | // Simulcast stream width and height must both be dividable by |
| 153 | // |2 ^ (simulcast_layers - 1)|. |
| 154 | int NormalizeSimulcastSize(int size, size_t simulcast_layers) { |
| 155 | int base2_exponent = static_cast<int>(simulcast_layers) - 1; |
| 156 | const absl::optional<int> experimental_base2_exponent = |
| 157 | webrtc::NormalizeSimulcastSizeExperiment::GetBase2Exponent(); |
| 158 | if (experimental_base2_exponent && |
| 159 | (size > (1 << *experimental_base2_exponent))) { |
| 160 | base2_exponent = *experimental_base2_exponent; |
| 161 | } |
| 162 | return ((size >> base2_exponent) << base2_exponent); |
| 163 | } |
| 164 |
| 165 | SimulcastFormat InterpolateSimulcastFormat(int width, int height) { |
| 166 | const int index = FindSimulcastFormatIndex(width, height); |
| 167 | if (index == 0) |
| 168 | return kSimulcastFormats[index]; |
| 169 | const int total_pixels_up = |
| 170 | kSimulcastFormats[index - 1].width * kSimulcastFormats[index - 1].height; |
| 171 | const int total_pixels_down = |
| 172 | kSimulcastFormats[index].width * kSimulcastFormats[index].height; |
| 173 | const int total_pixels = width * height; |
| 174 | const float rate = (total_pixels_up - total_pixels) / |
| 175 | static_cast<float>(total_pixels_up - total_pixels_down); |
| 176 |
| 177 | size_t max_layers = kSimulcastFormats[index].max_layers; |
| 178 | webrtc::DataRate max_bitrate = |
| 179 | Interpolate(kSimulcastFormats[index - 1].max_bitrate, |
| 180 | kSimulcastFormats[index].max_bitrate, rate); |
| 181 | webrtc::DataRate target_bitrate = |
| 182 | Interpolate(kSimulcastFormats[index - 1].target_bitrate, |
| 183 | kSimulcastFormats[index].target_bitrate, rate); |
| 184 | webrtc::DataRate min_bitrate = |
| 185 | Interpolate(kSimulcastFormats[index - 1].min_bitrate, |
| 186 | kSimulcastFormats[index].min_bitrate, rate); |
| 187 |
| 188 | return {width, height, max_layers, max_bitrate, target_bitrate, min_bitrate}; |
| 189 | } |
| 190 |
| 191 | webrtc::DataRate FindSimulcastMaxBitrate(int width, int height) { |
| 192 | return InterpolateSimulcastFormat(width, height).max_bitrate; |
| 193 | } |
| 194 |
| 195 | webrtc::DataRate FindSimulcastTargetBitrate(int width, int height) { |
| 196 | return InterpolateSimulcastFormat(width, height).target_bitrate; |
| 197 | } |
| 198 |
| 199 | webrtc::DataRate FindSimulcastMinBitrate(int width, int height) { |
| 200 | return InterpolateSimulcastFormat(width, height).min_bitrate; |
| 201 | } |
| 202 |
| 203 | void BoostMaxSimulcastLayer(webrtc::DataRate max_bitrate, |
| 204 | std::vector<webrtc::VideoStream>* layers) { |
| 205 | if (layers->empty()) |
| 206 | return; |
| 207 |
| 208 | const webrtc::DataRate total_bitrate = GetTotalMaxBitrate(*layers); |
| 209 |
| 210 | // We're still not using all available bits. |
| 211 | if (total_bitrate < max_bitrate) { |
| 212 | // Spend additional bits to boost the max layer. |
| 213 | const webrtc::DataRate bitrate_left = max_bitrate - total_bitrate; |
| 214 | layers->back().max_bitrate_bps += bitrate_left.bps(); |
| 215 | } |
| 216 | } |
| 217 |
| 218 | webrtc::DataRate GetTotalMaxBitrate( |
| 219 | const std::vector<webrtc::VideoStream>& layers) { |
| 220 | if (layers.empty()) |
| 221 | return webrtc::DataRate::Zero(); |
| 222 |
| 223 | int total_max_bitrate_bps = 0; |
| 224 | for (size_t s = 0; s < layers.size() - 1; ++s) { |
| 225 | total_max_bitrate_bps += layers[s].target_bitrate_bps; |
| 226 | } |
| 227 | total_max_bitrate_bps += layers.back().max_bitrate_bps; |
| 228 | return webrtc::DataRate::BitsPerSec(total_max_bitrate_bps); |
| 229 | } |
| 230 |
| 231 | size_t LimitSimulcastLayerCount(int width, |
| 232 | int height, |
| 233 | size_t need_layers, |
| 234 | size_t layer_count) { |
| 235 | if (!webrtc::field_trial::IsDisabled( |
| 236 | kUseLegacySimulcastLayerLimitFieldTrial)) { |
| 237 | size_t adaptive_layer_count = std::max( |
| 238 | need_layers, |
| 239 | kSimulcastFormats[FindSimulcastFormatIndex(width, height)].max_layers); |
| 240 | if (layer_count > adaptive_layer_count) { |
| 241 | RTC_LOG(LS_WARNING) << "Reducing simulcast layer count from " |
| 242 | << layer_count << " to " << adaptive_layer_count; |
| 243 | layer_count = adaptive_layer_count; |
| 244 | } |
| 245 | } |
| 246 | return layer_count; |
| 247 | } |
| 248 |
| 249 | std::vector<webrtc::VideoStream> GetSimulcastConfig( |
| 250 | size_t min_layers, |
| 251 | size_t max_layers, |
| 252 | int width, |
| 253 | int height, |
| 254 | double bitrate_priority, |
| 255 | int max_qp, |
| 256 | bool is_screenshare_with_conference_mode, |
| 257 | bool temporal_layers_supported) { |
| 258 | RTC_DCHECK_LE(min_layers, max_layers); |
| 259 | RTC_DCHECK(max_layers > 1 || is_screenshare_with_conference_mode); |
| 260 |
| 261 | const bool base_heavy_tl3_rate_alloc = |
| 262 | webrtc::RateControlSettings::ParseFromFieldTrials() |
| 263 | .Vp8BaseHeavyTl3RateAllocation(); |
| 264 | if (is_screenshare_with_conference_mode) { |
| 265 | return GetScreenshareLayers(max_layers, width, height, bitrate_priority, |
| 266 | max_qp, temporal_layers_supported, |
| 267 | base_heavy_tl3_rate_alloc); |
| 268 | } else { |
| 269 | // Some applications rely on the old behavior limiting the simulcast layer |
| 270 | // count based on the resolution automatically, which they can get through |
| 271 | // the WebRTC-LegacySimulcastLayerLimit field trial until they update. |
| 272 | max_layers = |
| 273 | LimitSimulcastLayerCount(width, height, min_layers, max_layers); |
| 274 |
| 275 | return GetNormalSimulcastLayers(max_layers, width, height, bitrate_priority, |
| 276 | max_qp, temporal_layers_supported, |
| 277 | base_heavy_tl3_rate_alloc); |
| 278 | } |
| 279 | } |
| 280 |
| 281 | std::vector<webrtc::VideoStream> GetNormalSimulcastLayers( |
| 282 | size_t layer_count, |
| 283 | int width, |
| 284 | int height, |
| 285 | double bitrate_priority, |
| 286 | int max_qp, |
| 287 | bool temporal_layers_supported, |
| 288 | bool base_heavy_tl3_rate_alloc) { |
| 289 | std::vector<webrtc::VideoStream> layers(layer_count); |
| 290 |
| 291 | // Format width and height has to be divisible by |2 ^ num_simulcast_layers - |
| 292 | // 1|. |
| 293 | width = NormalizeSimulcastSize(width, layer_count); |
| 294 | height = NormalizeSimulcastSize(height, layer_count); |
| 295 | // Add simulcast streams, from highest resolution (|s| = num_simulcast_layers |
| 296 | // -1) to lowest resolution at |s| = 0. |
| 297 | for (size_t s = layer_count - 1;; --s) { |
| 298 | layers[s].width = width; |
| 299 | layers[s].height = height; |
| 300 | // TODO(pbos): Fill actual temporal-layer bitrate thresholds. |
| 301 | layers[s].max_qp = max_qp; |
| 302 | layers[s].num_temporal_layers = |
| 303 | temporal_layers_supported ? DefaultNumberOfTemporalLayers(s, false) : 1; |
| 304 | layers[s].max_bitrate_bps = FindSimulcastMaxBitrate(width, height).bps(); |
| 305 | layers[s].target_bitrate_bps = |
| 306 | FindSimulcastTargetBitrate(width, height).bps(); |
| 307 | int num_temporal_layers = DefaultNumberOfTemporalLayers(s, false); |
| 308 | if (s == 0) { |
| 309 | // If alternative temporal rate allocation is selected, adjust the |
| 310 | // bitrate of the lowest simulcast stream so that absolute bitrate for |
| 311 | // the base temporal layer matches the bitrate for the base temporal |
| 312 | // layer with the default 3 simulcast streams. Otherwise we risk a |
| 313 | // higher threshold for receiving a feed at all. |
| 314 | float rate_factor = 1.0; |
| 315 | if (num_temporal_layers == 3) { |
| 316 | if (base_heavy_tl3_rate_alloc) { |
| 317 | // Base heavy allocation increases TL0 bitrate from 40% to 60%. |
| 318 | rate_factor = 0.4 / 0.6; |
| 319 | } |
| 320 | } else { |
| 321 | rate_factor = |
| 322 | webrtc::SimulcastRateAllocator::GetTemporalRateAllocation( |
| 323 | 3, 0, /*base_heavy_tl3_rate_alloc=*/false) / |
| 324 | webrtc::SimulcastRateAllocator::GetTemporalRateAllocation( |
| 325 | num_temporal_layers, 0, /*base_heavy_tl3_rate_alloc=*/false); |
| 326 | } |
| 327 |
| 328 | layers[s].max_bitrate_bps = |
| 329 | static_cast<int>(layers[s].max_bitrate_bps * rate_factor); |
| 330 | layers[s].target_bitrate_bps = |
| 331 | static_cast<int>(layers[s].target_bitrate_bps * rate_factor); |
| 332 | } |
| 333 | layers[s].min_bitrate_bps = FindSimulcastMinBitrate(width, height).bps(); |
| 334 | layers[s].max_framerate = kDefaultVideoMaxFramerate; |
| 335 |
| 336 | width /= 2; |
| 337 | height /= 2; |
| 338 |
| 339 | if (s == 0) { |
| 340 | break; |
| 341 | } |
| 342 | } |
| 343 | // Currently the relative bitrate priority of the sender is controlled by |
| 344 | // the value of the lowest VideoStream. |
| 345 | // TODO(bugs.webrtc.org/8630): The web specification describes being able to |
| 346 | // control relative bitrate for each individual simulcast layer, but this |
| 347 | // is currently just implemented per rtp sender. |
| 348 | layers[0].bitrate_priority = bitrate_priority; |
| 349 | return layers; |
| 350 | } |
| 351 |
| 352 | std::vector<webrtc::VideoStream> GetScreenshareLayers( |
| 353 | size_t max_layers, |
| 354 | int width, |
| 355 | int height, |
| 356 | double bitrate_priority, |
| 357 | int max_qp, |
| 358 | bool temporal_layers_supported, |
| 359 | bool base_heavy_tl3_rate_alloc) { |
| 360 | auto max_screenshare_layers = kMaxScreenshareSimulcastLayers; |
| 361 | size_t num_simulcast_layers = |
| 362 | std::min<int>(max_layers, max_screenshare_layers); |
| 363 |
| 364 | std::vector<webrtc::VideoStream> layers(num_simulcast_layers); |
| 365 | // For legacy screenshare in conference mode, tl0 and tl1 bitrates are |
| 366 | // piggybacked on the VideoCodec struct as target and max bitrates, |
| 367 | // respectively. See eg. webrtc::LibvpxVp8Encoder::SetRates(). |
| 368 | layers[0].width = width; |
| 369 | layers[0].height = height; |
| 370 | layers[0].max_qp = max_qp; |
| 371 | layers[0].max_framerate = 5; |
| 372 | layers[0].min_bitrate_bps = webrtc::kDefaultMinVideoBitrateBps; |
| 373 | layers[0].target_bitrate_bps = kScreenshareDefaultTl0Bitrate.bps(); |
| 374 | layers[0].max_bitrate_bps = kScreenshareDefaultTl1Bitrate.bps(); |
| 375 | layers[0].num_temporal_layers = temporal_layers_supported ? 2 : 1; |
| 376 |
| 377 | // With simulcast enabled, add another spatial layer. This one will have a |
| 378 | // more normal layout, with the regular 3 temporal layer pattern and no fps |
| 379 | // restrictions. The base simulcast layer will still use legacy setup. |
| 380 | if (num_simulcast_layers == kMaxScreenshareSimulcastLayers) { |
| 381 | // Add optional upper simulcast layer. |
| 382 | const int num_temporal_layers = DefaultNumberOfTemporalLayers(1, true); |
| 383 | int max_bitrate_bps; |
| 384 | bool using_boosted_bitrate = false; |
| 385 | if (!temporal_layers_supported) { |
| 386 | // Set the max bitrate to where the base layer would have been if temporal |
| 387 | // layers were enabled. |
| 388 | max_bitrate_bps = static_cast<int>( |
| 389 | kScreenshareHighStreamMaxBitrate.bps() * |
| 390 | webrtc::SimulcastRateAllocator::GetTemporalRateAllocation( |
| 391 | num_temporal_layers, 0, base_heavy_tl3_rate_alloc)); |
| 392 | } else if (DefaultNumberOfTemporalLayers(1, true) != 3 || |
| 393 | base_heavy_tl3_rate_alloc) { |
| 394 | // Experimental temporal layer mode used, use increased max bitrate. |
| 395 | max_bitrate_bps = kScreenshareHighStreamMaxBitrate.bps(); |
| 396 | using_boosted_bitrate = true; |
| 397 | } else { |
| 398 | // Keep current bitrates with default 3tl/8 frame settings. |
| 399 | // Lowest temporal layers of a 3 layer setup will have 40% of the total |
| 400 | // bitrate allocation for that simulcast layer. Make sure the gap between |
| 401 | // the target of the lower simulcast layer and first temporal layer of the |
| 402 | // higher one is at most 2x the bitrate, so that upswitching is not |
| 403 | // hampered by stalled bitrate estimates. |
| 404 | max_bitrate_bps = 2 * ((layers[0].target_bitrate_bps * 10) / 4); |
| 405 | } |
| 406 |
| 407 | layers[1].width = width; |
| 408 | layers[1].height = height; |
| 409 | layers[1].max_qp = max_qp; |
| 410 | layers[1].max_framerate = kDefaultVideoMaxFramerate; |
| 411 | layers[1].num_temporal_layers = |
| 412 | temporal_layers_supported ? DefaultNumberOfTemporalLayers(1, true) : 1; |
| 413 | layers[1].min_bitrate_bps = using_boosted_bitrate |
| 414 | ? kScreenshareHighStreamMinBitrate.bps() |
| 415 | : layers[0].target_bitrate_bps * 2; |
| 416 |
| 417 | // Cap max bitrate so it isn't overly high for the given resolution. |
| 418 | int resolution_limited_bitrate = |
| 419 | std::max<int>(FindSimulcastMaxBitrate(width, height).bps(), |
| 420 | layers[1].min_bitrate_bps); |
| 421 | max_bitrate_bps = |
| 422 | std::min<int>(max_bitrate_bps, resolution_limited_bitrate); |
| 423 |
| 424 | layers[1].target_bitrate_bps = max_bitrate_bps; |
| 425 | layers[1].max_bitrate_bps = max_bitrate_bps; |
| 426 | } |
| 427 |
| 428 | // The bitrate priority currently implemented on a per-sender level, so we |
| 429 | // just set it for the first simulcast layer. |
| 430 | layers[0].bitrate_priority = bitrate_priority; |
| 431 | return layers; |
| 432 | } |
| 433 |
| 434 | } // namespace cricket |
| 435 |