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