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