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