blob: b8e7a6caa573e7505e529319130ca53c2a11a7d8 [file] [log] [blame]
buildbot@webrtc.orga8530772014-12-10 09:01:18 +00001/*
kjellander1afca732016-02-07 20:46:45 -08002 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
buildbot@webrtc.orga8530772014-12-10 09:01:18 +00003 *
kjellander1afca732016-02-07 20:46:45 -08004 * 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.orga8530772014-12-10 09:01:18 +00009 */
10
Jonas Olssona4d87372019-07-05 19:08:33 +020011#include "media/engine/simulcast.h"
12
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <stdint.h>
sprang@webrtc.org46d4d292014-12-23 15:19:35 +000014#include <stdio.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
Steve Antone78bcb92017-10-31 09:53:08 -070016#include <algorithm>
17#include <string>
sprang@webrtc.org46d4d292014-12-23 15:19:35 +000018
Yves Gerey3e707812018-11-28 16:47:49 +010019#include "absl/types/optional.h"
Erik Språngf93eda12019-01-16 17:10:57 +010020#include "api/video/video_codec_constants.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "media/base/media_constants.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "media/engine/constants.h"
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +020023#include "modules/video_coding/utility/simulcast_rate_allocator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/arraysize.h"
Yves Gerey3e707812018-11-28 16:47:49 +010025#include "rtc_base/checks.h"
Erik Språngd7ee76c2019-08-02 16:32:24 +020026#include "rtc_base/experiments/experimental_screenshare_settings.h"
Åsa Persson1a35fbd2018-10-12 17:36:57 +020027#include "rtc_base/experiments/normalize_simulcast_size_experiment.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "rtc_base/logging.h"
29#include "system_wrappers/include/field_trial.h"
tfarina5237aaf2015-11-10 23:44:30 -080030
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000031namespace cricket {
32
Rasmus Brandt195d1d72018-05-09 11:28:01 +020033namespace {
34
Rasmus Brandt73d117f2018-10-02 11:12:52 +020035constexpr char kUseBaseHeavyVP8TL3RateAllocationFieldTrial[] =
36 "WebRTC-UseBaseHeavyVP8TL3RateAllocation";
37
Florent Castellia9fbb222019-07-24 16:06:45 +020038constexpr char kUseLegacySimulcastLayerLimitFieldTrial[] =
39 "WebRTC-LegacySimulcastLayerLimit";
40
Erik Språngb6b1cac2018-08-09 16:12:54 +020041// Limits for legacy conference screensharing mode. Currently used for the
42// lower of the two simulcast streams.
Rasmus Brandt195d1d72018-05-09 11:28:01 +020043constexpr int kScreenshareDefaultTl0BitrateKbps = 200;
44constexpr int kScreenshareDefaultTl1BitrateKbps = 1000;
45
Erik Språng661a0c62018-09-11 12:33:21 +020046// Min/max bitrate for the higher one of the two simulcast stream used for
47// screen content.
48constexpr int kScreenshareHighStreamMinBitrateBps = 600000;
Erik Språng58b22842018-08-21 13:15:28 +020049constexpr int kScreenshareHighStreamMaxBitrateBps = 1250000;
Erik Språngb6b1cac2018-08-09 16:12:54 +020050
Rasmus Brandt195d1d72018-05-09 11:28:01 +020051} // namespace
52
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000053struct SimulcastFormat {
54 int width;
55 int height;
Florent Castellia9fbb222019-07-24 16:06:45 +020056 // The maximum number of simulcast layers can be used for
57 // resolutions at |widthxheigh| for legacy applications.
58 size_t max_layers;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000059 // The maximum bitrate for encoding stream at |widthxheight|, when we are
60 // not sending the next higher spatial stream.
pbosbe16f792015-10-16 12:49:39 -070061 int max_bitrate_kbps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000062 // 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).
pbosbe16f792015-10-16 12:49:39 -070065 int target_bitrate_kbps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000066 // The minimum bitrate needed for encoding stream at |widthxheight|.
pbosbe16f792015-10-16 12:49:39 -070067 int min_bitrate_kbps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000068};
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 Murillo43800f92018-06-21 16:16:38 +020073// clang-format off
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000074const SimulcastFormat kSimulcastFormats[] = {
Florent Castellia9fbb222019-07-24 16:06:45 +020075 {1920, 1080, 3, 5000, 4000, 800},
76 {1280, 720, 3, 2500, 2500, 600},
Ilya Nikolaevskiy73ceed52019-09-16 14:53:29 +020077 {960, 540, 3, 1200, 1200, 350},
Florent Castellia9fbb222019-07-24 16:06:45 +020078 {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 Murillo43800f92018-06-21 16:16:38 +020082};
83// clang-format on
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000084
Seth Hampson1370e302018-02-07 08:50:36 -080085const int kMaxScreenshareSimulcastLayers = 2;
sprang429600d2017-01-26 06:12:26 -080086
Erik Språngbfe3d852018-05-15 09:54:14 +020087// Multiway: Number of temporal layers for each simulcast stream.
Erik Språng58b22842018-08-21 13:15:28 +020088int DefaultNumberOfTemporalLayers(int simulcast_id, bool screenshare) {
Erik Språngbfe3d852018-05-15 09:54:14 +020089 RTC_CHECK_GE(simulcast_id, 0);
90 RTC_CHECK_LT(simulcast_id, webrtc::kMaxSimulcastStreams);
91
92 const int kDefaultNumTemporalLayers = 3;
Erik Språng7461eff2018-09-10 09:43:56 +020093 const int kDefaultNumScreenshareTemporalLayers = 2;
94 int default_num_temporal_layers = screenshare
95 ? kDefaultNumScreenshareTemporalLayers
96 : kDefaultNumTemporalLayers;
Erik Språngbfe3d852018-05-15 09:54:14 +020097
98 const std::string group_name =
Erik Språng58b22842018-08-21 13:15:28 +020099 screenshare ? webrtc::field_trial::FindFullName(
100 "WebRTC-VP8ScreenshareTemporalLayers")
101 : webrtc::field_trial::FindFullName(
102 "WebRTC-VP8ConferenceTemporalLayers");
Erik Språngbfe3d852018-05-15 09:54:14 +0200103 if (group_name.empty())
Erik Språng7461eff2018-09-10 09:43:56 +0200104 return default_num_temporal_layers;
Erik Språngbfe3d852018-05-15 09:54:14 +0200105
Erik Språng7461eff2018-09-10 09:43:56 +0200106 int num_temporal_layers = default_num_temporal_layers;
Erik Språngbfe3d852018-05-15 09:54:14 +0200107 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ång7461eff2018-09-10 09:43:56 +0200117 return default_num_temporal_layers;
Erik Språngbfe3d852018-05-15 09:54:14 +0200118}
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000119
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000120int FindSimulcastFormatIndex(int width, int height) {
Seth Hampson438663e2018-01-09 11:14:14 -0800121 RTC_DCHECK_GE(width, 0);
122 RTC_DCHECK_GE(height, 0);
kjellandera96e2d72016-02-04 23:52:28 -0800123 for (uint32_t i = 0; i < arraysize(kSimulcastFormats); ++i) {
sprang429600d2017-01-26 06:12:26 -0800124 if (width * height >=
125 kSimulcastFormats[i].width * kSimulcastFormats[i].height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000126 return i;
127 }
128 }
Seth Hampson438663e2018-01-09 11:14:14 -0800129 RTC_NOTREACHED();
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000130 return -1;
131}
132
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000133// Simulcast stream width and height must both be dividable by
Rasmus Brandtefbdcb02018-04-26 17:47:42 +0200134// |2 ^ (simulcast_layers - 1)|.
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000135int NormalizeSimulcastSize(int size, size_t simulcast_layers) {
Åsa Persson1a35fbd2018-10-12 17:36:57 +0200136 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.orga8530772014-12-10 09:01:18 +0000143 return ((size >> base2_exponent) << base2_exponent);
144}
145
Ilya Nikolaevskiy73ceed52019-09-16 14:53:29 +0200146SimulcastFormat InterpolateSimulcastFormat(int width, int height) {
147 const int index = FindSimulcastFormatIndex(width, height);
148 if (index == 0)
149 return kSimulcastFormats[index];
150 const int total_pixels_up =
151 kSimulcastFormats[index - 1].width * kSimulcastFormats[index - 1].height;
152 const int total_pixels_down =
153 kSimulcastFormats[index].width * kSimulcastFormats[index].height;
154 const int total_pixels = width * height;
155 const float rate = (total_pixels_up - total_pixels) /
156 static_cast<float>(total_pixels_up - total_pixels_down);
157 SimulcastFormat res;
158 res.width = width;
159 res.height = height;
160 res.max_layers = kSimulcastFormats[index].max_layers;
161 res.max_bitrate_kbps =
162 kSimulcastFormats[index - 1].max_bitrate_kbps * (1.0 - rate) +
163 kSimulcastFormats[index].max_bitrate_kbps * rate;
164 res.target_bitrate_kbps =
165 kSimulcastFormats[index - 1].target_bitrate_kbps * (1.0 - rate) +
166 kSimulcastFormats[index].target_bitrate_kbps * rate;
167 res.min_bitrate_kbps =
168 kSimulcastFormats[index - 1].min_bitrate_kbps * (1.0 - rate) +
169 kSimulcastFormats[index].min_bitrate_kbps * rate;
170 return res;
171}
172
sprang429600d2017-01-26 06:12:26 -0800173int FindSimulcastMaxBitrateBps(int width, int height) {
Ilya Nikolaevskiy73ceed52019-09-16 14:53:29 +0200174 const SimulcastFormat format = InterpolateSimulcastFormat(width, height);
175 return format.max_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000176}
177
sprang429600d2017-01-26 06:12:26 -0800178int FindSimulcastTargetBitrateBps(int width, int height) {
Ilya Nikolaevskiy73ceed52019-09-16 14:53:29 +0200179 const SimulcastFormat format = InterpolateSimulcastFormat(width, height);
180 return format.target_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000181}
182
sprang429600d2017-01-26 06:12:26 -0800183int FindSimulcastMinBitrateBps(int width, int height) {
Ilya Nikolaevskiy73ceed52019-09-16 14:53:29 +0200184 const SimulcastFormat format = InterpolateSimulcastFormat(width, height);
185 return format.min_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000186}
187
Seth Hampson1370e302018-02-07 08:50:36 -0800188void BoostMaxSimulcastLayer(int max_bitrate_bps,
189 std::vector<webrtc::VideoStream>* layers) {
Åsa Persson55659812018-06-18 17:51:32 +0200190 if (layers->empty())
191 return;
192
Seth Hampson1370e302018-02-07 08:50:36 -0800193 // Spend additional bits to boost the max layer.
194 int bitrate_left_bps = max_bitrate_bps - GetTotalMaxBitrateBps(*layers);
195 if (bitrate_left_bps > 0) {
196 layers->back().max_bitrate_bps += bitrate_left_bps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000197 }
Seth Hampson1370e302018-02-07 08:50:36 -0800198}
199
200int GetTotalMaxBitrateBps(const std::vector<webrtc::VideoStream>& layers) {
Åsa Persson55659812018-06-18 17:51:32 +0200201 if (layers.empty())
202 return 0;
203
Seth Hampson1370e302018-02-07 08:50:36 -0800204 int total_max_bitrate_bps = 0;
205 for (size_t s = 0; s < layers.size() - 1; ++s) {
206 total_max_bitrate_bps += layers[s].target_bitrate_bps;
207 }
208 total_max_bitrate_bps += layers.back().max_bitrate_bps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000209 return total_max_bitrate_bps;
210}
211
Florent Castellia9fbb222019-07-24 16:06:45 +0200212int LimitSimulcastLayerCount(int width, int height, int layer_count) {
Florent Castelli9b1700c2019-08-05 17:12:20 +0200213 if (!webrtc::field_trial::IsDisabled(
214 kUseLegacySimulcastLayerLimitFieldTrial)) {
Florent Castellia9fbb222019-07-24 16:06:45 +0200215 int adaptive_layer_count =
216 kSimulcastFormats[FindSimulcastFormatIndex(width, height)].max_layers;
217 if (layer_count > adaptive_layer_count) {
218 RTC_LOG(LS_WARNING) << "Reducing simulcast layer count from "
219 << layer_count << " to " << adaptive_layer_count;
220 layer_count = adaptive_layer_count;
221 }
222 }
223 return layer_count;
224}
225
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200226std::vector<webrtc::VideoStream> GetSimulcastConfig(
227 size_t max_layers,
228 int width,
229 int height,
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200230 double bitrate_priority,
231 int max_qp,
Florent Castelli66b38602019-07-10 16:57:57 +0200232 bool is_screenshare_with_conference_mode,
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200233 bool temporal_layers_supported) {
Florent Castelli66b38602019-07-10 16:57:57 +0200234 RTC_DCHECK(max_layers > 1 || is_screenshare_with_conference_mode);
Florent Castellia9fbb222019-07-24 16:06:45 +0200235
Florent Castelli66b38602019-07-10 16:57:57 +0200236 if (is_screenshare_with_conference_mode) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200237 return GetScreenshareLayers(max_layers, width, height, bitrate_priority,
Florent Castelli66b38602019-07-10 16:57:57 +0200238 max_qp, temporal_layers_supported);
sprang429600d2017-01-26 06:12:26 -0800239 } else {
Ilya Nikolaevskiy9d7eb282019-10-01 15:43:14 +0200240 // Some applications rely on the old behavior limiting the simulcast layer
241 // count based on the resolution automatically, which they can get through
242 // the WebRTC-LegacySimulcastLayerLimit field trial until they update.
243 max_layers = LimitSimulcastLayerCount(width, height, max_layers);
244
Åsa Persson55659812018-06-18 17:51:32 +0200245 return GetNormalSimulcastLayers(max_layers, width, height, bitrate_priority,
Åsa Persson8c1bf952018-09-13 10:42:19 +0200246 max_qp, temporal_layers_supported);
sprang429600d2017-01-26 06:12:26 -0800247 }
Seth Hampson1370e302018-02-07 08:50:36 -0800248}
sprang429600d2017-01-26 06:12:26 -0800249
Seth Hampson1370e302018-02-07 08:50:36 -0800250std::vector<webrtc::VideoStream> GetNormalSimulcastLayers(
Florent Castelli668ce0c2019-07-04 17:06:04 +0200251 size_t layer_count,
Seth Hampson1370e302018-02-07 08:50:36 -0800252 int width,
253 int height,
Seth Hampson1370e302018-02-07 08:50:36 -0800254 double bitrate_priority,
255 int max_qp,
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200256 bool temporal_layers_supported) {
Florent Castelli668ce0c2019-07-04 17:06:04 +0200257 std::vector<webrtc::VideoStream> layers(layer_count);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000258
Seth Hampson1370e302018-02-07 08:50:36 -0800259 // Format width and height has to be divisible by |2 ^ num_simulcast_layers -
260 // 1|.
Florent Castelli668ce0c2019-07-04 17:06:04 +0200261 width = NormalizeSimulcastSize(width, layer_count);
262 height = NormalizeSimulcastSize(height, layer_count);
Seth Hampson1370e302018-02-07 08:50:36 -0800263 // Add simulcast streams, from highest resolution (|s| = num_simulcast_layers
264 // -1) to lowest resolution at |s| = 0.
Florent Castelli668ce0c2019-07-04 17:06:04 +0200265 for (size_t s = layer_count - 1;; --s) {
Seth Hampson1370e302018-02-07 08:50:36 -0800266 layers[s].width = width;
267 layers[s].height = height;
268 // TODO(pbos): Fill actual temporal-layer bitrate thresholds.
269 layers[s].max_qp = max_qp;
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200270 layers[s].num_temporal_layers =
Erik Språng58b22842018-08-21 13:15:28 +0200271 temporal_layers_supported ? DefaultNumberOfTemporalLayers(s, false) : 0;
Seth Hampson1370e302018-02-07 08:50:36 -0800272 layers[s].max_bitrate_bps = FindSimulcastMaxBitrateBps(width, height);
273 layers[s].target_bitrate_bps = FindSimulcastTargetBitrateBps(width, height);
Erik Språng58b22842018-08-21 13:15:28 +0200274 int num_temporal_layers = DefaultNumberOfTemporalLayers(s, false);
Erik Språngd497e6b2018-07-06 17:17:10 +0200275 if (s == 0) {
Rasmus Brandt73d117f2018-10-02 11:12:52 +0200276 // If alternative temporal rate allocation is selected, adjust the
Erik Språngd92288f2018-07-04 10:07:40 +0200277 // bitrate of the lowest simulcast stream so that absolute bitrate for
278 // the base temporal layer matches the bitrate for the base temporal
279 // layer with the default 3 simulcast streams. Otherwise we risk a
280 // higher threshold for receiving a feed at all.
Erik Språngd497e6b2018-07-06 17:17:10 +0200281 float rate_factor = 1.0;
282 if (num_temporal_layers == 3) {
Rasmus Brandt73d117f2018-10-02 11:12:52 +0200283 if (webrtc::field_trial::IsEnabled(
284 kUseBaseHeavyVP8TL3RateAllocationFieldTrial)) {
285 // Base heavy allocation increases TL0 bitrate from 40% to 60%.
Erik Språngd497e6b2018-07-06 17:17:10 +0200286 rate_factor = 0.4 / 0.6;
287 }
288 } else {
289 rate_factor =
290 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(3, 0) /
291 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
292 num_temporal_layers, 0);
293 }
294
Erik Språng79478ad2018-05-18 09:39:55 +0200295 layers[s].max_bitrate_bps =
296 static_cast<int>(layers[s].max_bitrate_bps * rate_factor);
297 layers[s].target_bitrate_bps =
298 static_cast<int>(layers[s].target_bitrate_bps * rate_factor);
299 }
Seth Hampson1370e302018-02-07 08:50:36 -0800300 layers[s].min_bitrate_bps = FindSimulcastMinBitrateBps(width, height);
Åsa Persson8c1bf952018-09-13 10:42:19 +0200301 layers[s].max_framerate = kDefaultVideoMaxFramerate;
sprang02569ad2017-07-06 05:05:50 -0700302
Seth Hampson1370e302018-02-07 08:50:36 -0800303 width /= 2;
304 height /= 2;
sprang02569ad2017-07-06 05:05:50 -0700305
Seth Hampson1370e302018-02-07 08:50:36 -0800306 if (s == 0) {
307 break;
sprang02569ad2017-07-06 05:05:50 -0700308 }
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000309 }
Seth Hampson1370e302018-02-07 08:50:36 -0800310 // Currently the relative bitrate priority of the sender is controlled by
311 // the value of the lowest VideoStream.
312 // TODO(bugs.webrtc.org/8630): The web specification describes being able to
313 // control relative bitrate for each individual simulcast layer, but this
314 // is currently just implemented per rtp sender.
315 layers[0].bitrate_priority = bitrate_priority;
316 return layers;
317}
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000318
Seth Hampson1370e302018-02-07 08:50:36 -0800319std::vector<webrtc::VideoStream> GetScreenshareLayers(
320 size_t max_layers,
321 int width,
322 int height,
Seth Hampson1370e302018-02-07 08:50:36 -0800323 double bitrate_priority,
324 int max_qp,
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200325 bool temporal_layers_supported) {
Florent Castelli66b38602019-07-10 16:57:57 +0200326 auto max_screenshare_layers = kMaxScreenshareSimulcastLayers;
Seth Hampson1370e302018-02-07 08:50:36 -0800327 size_t num_simulcast_layers =
Ilya Nikolaevskiy3df1d5d2018-08-22 09:26:51 +0200328 std::min<int>(max_layers, max_screenshare_layers);
Seth Hampson1370e302018-02-07 08:50:36 -0800329
330 std::vector<webrtc::VideoStream> layers(num_simulcast_layers);
Seth Hampson1370e302018-02-07 08:50:36 -0800331 // For legacy screenshare in conference mode, tl0 and tl1 bitrates are
332 // piggybacked on the VideoCodec struct as target and max bitrates,
Erik Språngcc681cc2018-03-14 17:52:55 +0100333 // respectively. See eg. webrtc::LibvpxVp8Encoder::SetRates().
Seth Hampson1370e302018-02-07 08:50:36 -0800334 layers[0].width = width;
335 layers[0].height = height;
336 layers[0].max_qp = max_qp;
337 layers[0].max_framerate = 5;
338 layers[0].min_bitrate_bps = kMinVideoBitrateBps;
Rasmus Brandt195d1d72018-05-09 11:28:01 +0200339 layers[0].target_bitrate_bps = kScreenshareDefaultTl0BitrateKbps * 1000;
340 layers[0].max_bitrate_bps = kScreenshareDefaultTl1BitrateKbps * 1000;
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200341 layers[0].num_temporal_layers = temporal_layers_supported ? 2 : 0;
Seth Hampson1370e302018-02-07 08:50:36 -0800342
343 // With simulcast enabled, add another spatial layer. This one will have a
344 // more normal layout, with the regular 3 temporal layer pattern and no fps
345 // restrictions. The base simulcast layer will still use legacy setup.
346 if (num_simulcast_layers == kMaxScreenshareSimulcastLayers) {
Erik Språngd7ee76c2019-08-02 16:32:24 +0200347 auto experimental_settings =
348 webrtc::ExperimentalScreenshareSettings::ParseFromFieldTrials();
349 if (temporal_layers_supported &&
350 experimental_settings.BaseLayerMaxBitrate().has_value()) {
351 layers[0].max_bitrate_bps = *experimental_settings.BaseLayerMaxBitrate();
352 }
353
Seth Hampson1370e302018-02-07 08:50:36 -0800354 // Add optional upper simulcast layer.
Erik Språng58b22842018-08-21 13:15:28 +0200355 const int num_temporal_layers = DefaultNumberOfTemporalLayers(1, true);
Erik Språngb6b1cac2018-08-09 16:12:54 +0200356 int max_bitrate_bps;
Erik Språngcf919422018-09-17 16:18:57 +0200357 bool using_boosted_bitrate = false;
Erik Språngb6b1cac2018-08-09 16:12:54 +0200358 if (!temporal_layers_supported) {
359 // Set the max bitrate to where the base layer would have been if temporal
Erik Språng58b22842018-08-21 13:15:28 +0200360 // layers were enabled.
Erik Språngb6b1cac2018-08-09 16:12:54 +0200361 max_bitrate_bps = static_cast<int>(
362 kScreenshareHighStreamMaxBitrateBps *
363 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
364 num_temporal_layers, 0));
Erik Språng58b22842018-08-21 13:15:28 +0200365 } else if (DefaultNumberOfTemporalLayers(1, true) != 3 ||
Rasmus Brandt73d117f2018-10-02 11:12:52 +0200366 webrtc::field_trial::IsEnabled(
367 kUseBaseHeavyVP8TL3RateAllocationFieldTrial)) {
Erik Språngb6b1cac2018-08-09 16:12:54 +0200368 // Experimental temporal layer mode used, use increased max bitrate.
Erik Språngd7ee76c2019-08-02 16:32:24 +0200369 max_bitrate_bps = experimental_settings.TopLayerMaxBitrate().value_or(
370 kScreenshareHighStreamMaxBitrateBps);
Erik Språngcf919422018-09-17 16:18:57 +0200371 using_boosted_bitrate = true;
Erik Språngb6b1cac2018-08-09 16:12:54 +0200372 } else {
373 // Keep current bitrates with default 3tl/8 frame settings.
374 // Lowest temporal layers of a 3 layer setup will have 40% of the total
375 // bitrate allocation for that simulcast layer. Make sure the gap between
376 // the target of the lower simulcast layer and first temporal layer of the
377 // higher one is at most 2x the bitrate, so that upswitching is not
378 // hampered by stalled bitrate estimates.
379 max_bitrate_bps = 2 * ((layers[0].target_bitrate_bps * 10) / 4);
380 }
381
Seth Hampson1370e302018-02-07 08:50:36 -0800382 layers[1].width = width;
383 layers[1].height = height;
384 layers[1].max_qp = max_qp;
Åsa Persson8c1bf952018-09-13 10:42:19 +0200385 layers[1].max_framerate = kDefaultVideoMaxFramerate;
Erik Språngb6b1cac2018-08-09 16:12:54 +0200386 layers[1].num_temporal_layers =
Erik Språng58b22842018-08-21 13:15:28 +0200387 temporal_layers_supported ? DefaultNumberOfTemporalLayers(1, true) : 0;
Erik Språngcf919422018-09-17 16:18:57 +0200388 layers[1].min_bitrate_bps = using_boosted_bitrate
389 ? kScreenshareHighStreamMinBitrateBps
390 : layers[0].target_bitrate_bps * 2;
391
392 // Cap max bitrate so it isn't overly high for the given resolution.
393 int resolution_limited_bitrate = std::max(
394 FindSimulcastMaxBitrateBps(width, height), layers[1].min_bitrate_bps);
395 max_bitrate_bps =
396 std::min<int>(max_bitrate_bps, resolution_limited_bitrate);
397
Seth Hampson1370e302018-02-07 08:50:36 -0800398 layers[1].target_bitrate_bps = max_bitrate_bps;
399 layers[1].max_bitrate_bps = max_bitrate_bps;
400 }
401
Seth Hampson24722b32017-12-22 09:36:42 -0800402 // The bitrate priority currently implemented on a per-sender level, so we
Seth Hampson1370e302018-02-07 08:50:36 -0800403 // just set it for the first simulcast layer.
404 layers[0].bitrate_priority = bitrate_priority;
405 return layers;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000406}
407
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000408} // namespace cricket