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