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