blob: de3d8df097f1505d01f64ce30fe7a6e022dc0fe4 [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
sprang@webrtc.org46d4d292014-12-23 15:19:35 +000011#include <stdio.h>
Steve Antone78bcb92017-10-31 09:53:08 -070012#include <algorithm>
13#include <string>
sprang@webrtc.org46d4d292014-12-23 15:19:35 +000014
Åsa Persson8c1bf952018-09-13 10:42:19 +020015#include "media/base/mediaconstants.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "media/base/streamparams.h"
17#include "media/engine/constants.h"
18#include "media/engine/simulcast.h"
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +020019#include "modules/video_coding/utility/simulcast_rate_allocator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/arraysize.h"
Åsa Persson1a35fbd2018-10-12 17:36:57 +020021#include "rtc_base/experiments/normalize_simulcast_size_experiment.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/logging.h"
23#include "system_wrappers/include/field_trial.h"
tfarina5237aaf2015-11-10 23:44:30 -080024
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000025namespace cricket {
26
Rasmus Brandt195d1d72018-05-09 11:28:01 +020027namespace {
28
Rasmus Brandt73d117f2018-10-02 11:12:52 +020029constexpr char kUseBaseHeavyVP8TL3RateAllocationFieldTrial[] =
30 "WebRTC-UseBaseHeavyVP8TL3RateAllocation";
31
Erik Språngb6b1cac2018-08-09 16:12:54 +020032// Limits for legacy conference screensharing mode. Currently used for the
33// lower of the two simulcast streams.
Rasmus Brandt195d1d72018-05-09 11:28:01 +020034constexpr int kScreenshareDefaultTl0BitrateKbps = 200;
35constexpr int kScreenshareDefaultTl1BitrateKbps = 1000;
36
Erik Språng661a0c62018-09-11 12:33:21 +020037// Min/max bitrate for the higher one of the two simulcast stream used for
38// screen content.
39constexpr int kScreenshareHighStreamMinBitrateBps = 600000;
Erik Språng58b22842018-08-21 13:15:28 +020040constexpr int kScreenshareHighStreamMaxBitrateBps = 1250000;
Ilya Nikolaevskiy3df1d5d2018-08-22 09:26:51 +020041static const char* kSimulcastScreenshareFieldTrialName =
42 "WebRTC-SimulcastScreenshare";
Erik Språngb6b1cac2018-08-09 16:12:54 +020043
Rasmus Brandt195d1d72018-05-09 11:28:01 +020044} // namespace
45
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000046struct SimulcastFormat {
47 int width;
48 int height;
49 // The maximum number of simulcast layers can be used for
50 // resolutions at |widthxheigh|.
51 size_t max_layers;
52 // The maximum bitrate for encoding stream at |widthxheight|, when we are
53 // not sending the next higher spatial stream.
pbosbe16f792015-10-16 12:49:39 -070054 int max_bitrate_kbps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000055 // The target bitrate for encoding stream at |widthxheight|, when this layer
56 // is not the highest layer (i.e., when we are sending another higher spatial
57 // stream).
pbosbe16f792015-10-16 12:49:39 -070058 int target_bitrate_kbps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000059 // The minimum bitrate needed for encoding stream at |widthxheight|.
pbosbe16f792015-10-16 12:49:39 -070060 int min_bitrate_kbps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000061};
62
63// These tables describe from which resolution we can use how many
64// simulcast layers at what bitrates (maximum, target, and minimum).
65// Important!! Keep this table from high resolution to low resolution.
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +020066// clang-format off
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000067const SimulcastFormat kSimulcastFormats[] = {
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +020068 {1920, 1080, 3, 5000, 4000, 800},
69 {1280, 720, 3, 2500, 2500, 600},
70 {960, 540, 3, 900, 900, 450},
71 {640, 360, 2, 700, 500, 150},
72 {480, 270, 2, 450, 350, 150},
73 {320, 180, 1, 200, 150, 30},
74 {0, 0, 1, 200, 150, 30}
75};
76// clang-format on
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000077
Seth Hampson1370e302018-02-07 08:50:36 -080078const int kMaxScreenshareSimulcastLayers = 2;
sprang429600d2017-01-26 06:12:26 -080079
Erik Språngbfe3d852018-05-15 09:54:14 +020080// Multiway: Number of temporal layers for each simulcast stream.
Erik Språng58b22842018-08-21 13:15:28 +020081int DefaultNumberOfTemporalLayers(int simulcast_id, bool screenshare) {
Erik Språngbfe3d852018-05-15 09:54:14 +020082 RTC_CHECK_GE(simulcast_id, 0);
83 RTC_CHECK_LT(simulcast_id, webrtc::kMaxSimulcastStreams);
84
85 const int kDefaultNumTemporalLayers = 3;
Erik Språng7461eff2018-09-10 09:43:56 +020086 const int kDefaultNumScreenshareTemporalLayers = 2;
87 int default_num_temporal_layers = screenshare
88 ? kDefaultNumScreenshareTemporalLayers
89 : kDefaultNumTemporalLayers;
Erik Språngbfe3d852018-05-15 09:54:14 +020090
91 const std::string group_name =
Erik Språng58b22842018-08-21 13:15:28 +020092 screenshare ? webrtc::field_trial::FindFullName(
93 "WebRTC-VP8ScreenshareTemporalLayers")
94 : webrtc::field_trial::FindFullName(
95 "WebRTC-VP8ConferenceTemporalLayers");
Erik Språngbfe3d852018-05-15 09:54:14 +020096 if (group_name.empty())
Erik Språng7461eff2018-09-10 09:43:56 +020097 return default_num_temporal_layers;
Erik Språngbfe3d852018-05-15 09:54:14 +020098
Erik Språng7461eff2018-09-10 09:43:56 +020099 int num_temporal_layers = default_num_temporal_layers;
Erik Språngbfe3d852018-05-15 09:54:14 +0200100 if (sscanf(group_name.c_str(), "%d", &num_temporal_layers) == 1 &&
101 num_temporal_layers > 0 &&
102 num_temporal_layers <= webrtc::kMaxTemporalStreams) {
103 return num_temporal_layers;
104 }
105
106 RTC_LOG(LS_WARNING) << "Attempt to set number of temporal layers to "
107 "incorrect value: "
108 << group_name;
109
Erik Språng7461eff2018-09-10 09:43:56 +0200110 return default_num_temporal_layers;
Erik Språngbfe3d852018-05-15 09:54:14 +0200111}
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000112
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000113int FindSimulcastFormatIndex(int width, int height) {
Seth Hampson438663e2018-01-09 11:14:14 -0800114 RTC_DCHECK_GE(width, 0);
115 RTC_DCHECK_GE(height, 0);
kjellandera96e2d72016-02-04 23:52:28 -0800116 for (uint32_t i = 0; i < arraysize(kSimulcastFormats); ++i) {
sprang429600d2017-01-26 06:12:26 -0800117 if (width * height >=
118 kSimulcastFormats[i].width * kSimulcastFormats[i].height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000119 return i;
120 }
121 }
Seth Hampson438663e2018-01-09 11:14:14 -0800122 RTC_NOTREACHED();
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000123 return -1;
124}
125
126int FindSimulcastFormatIndex(int width, int height, size_t max_layers) {
Seth Hampson438663e2018-01-09 11:14:14 -0800127 RTC_DCHECK_GE(width, 0);
128 RTC_DCHECK_GE(height, 0);
129 RTC_DCHECK_GT(max_layers, 0);
kjellandera96e2d72016-02-04 23:52:28 -0800130 for (uint32_t i = 0; i < arraysize(kSimulcastFormats); ++i) {
sprang429600d2017-01-26 06:12:26 -0800131 if (width * height >=
132 kSimulcastFormats[i].width * kSimulcastFormats[i].height &&
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000133 max_layers == kSimulcastFormats[i].max_layers) {
134 return i;
135 }
136 }
Seth Hampson438663e2018-01-09 11:14:14 -0800137 RTC_NOTREACHED();
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000138 return -1;
139}
140
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000141// Simulcast stream width and height must both be dividable by
Rasmus Brandtefbdcb02018-04-26 17:47:42 +0200142// |2 ^ (simulcast_layers - 1)|.
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000143int NormalizeSimulcastSize(int size, size_t simulcast_layers) {
Åsa Persson1a35fbd2018-10-12 17:36:57 +0200144 int base2_exponent = static_cast<int>(simulcast_layers) - 1;
145 const absl::optional<int> experimental_base2_exponent =
146 webrtc::NormalizeSimulcastSizeExperiment::GetBase2Exponent();
147 if (experimental_base2_exponent &&
148 (size > (1 << *experimental_base2_exponent))) {
149 base2_exponent = *experimental_base2_exponent;
150 }
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000151 return ((size >> base2_exponent) << base2_exponent);
152}
153
154size_t FindSimulcastMaxLayers(int width, int height) {
155 int index = FindSimulcastFormatIndex(width, height);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000156 return kSimulcastFormats[index].max_layers;
157}
158
sprang429600d2017-01-26 06:12:26 -0800159int FindSimulcastMaxBitrateBps(int width, int height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000160 const int format_index = FindSimulcastFormatIndex(width, height);
pbosbe16f792015-10-16 12:49:39 -0700161 return kSimulcastFormats[format_index].max_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000162}
163
sprang429600d2017-01-26 06:12:26 -0800164int FindSimulcastTargetBitrateBps(int width, int height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000165 const int format_index = FindSimulcastFormatIndex(width, height);
pbosbe16f792015-10-16 12:49:39 -0700166 return kSimulcastFormats[format_index].target_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000167}
168
sprang429600d2017-01-26 06:12:26 -0800169int FindSimulcastMinBitrateBps(int width, int height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000170 const int format_index = FindSimulcastFormatIndex(width, height);
pbosbe16f792015-10-16 12:49:39 -0700171 return kSimulcastFormats[format_index].min_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000172}
173
Seth Hampson438663e2018-01-09 11:14:14 -0800174void SlotSimulcastMaxResolution(size_t max_layers, int* width, int* height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000175 int index = FindSimulcastFormatIndex(*width, *height, max_layers);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000176 *width = kSimulcastFormats[index].width;
177 *height = kSimulcastFormats[index].height;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100178 RTC_LOG(LS_INFO) << "SlotSimulcastMaxResolution to width:" << *width
179 << " height:" << *height;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000180}
181
Seth Hampson1370e302018-02-07 08:50:36 -0800182void BoostMaxSimulcastLayer(int max_bitrate_bps,
183 std::vector<webrtc::VideoStream>* layers) {
Åsa Persson55659812018-06-18 17:51:32 +0200184 if (layers->empty())
185 return;
186
Seth Hampson1370e302018-02-07 08:50:36 -0800187 // Spend additional bits to boost the max layer.
188 int bitrate_left_bps = max_bitrate_bps - GetTotalMaxBitrateBps(*layers);
189 if (bitrate_left_bps > 0) {
190 layers->back().max_bitrate_bps += bitrate_left_bps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000191 }
Seth Hampson1370e302018-02-07 08:50:36 -0800192}
193
194int GetTotalMaxBitrateBps(const std::vector<webrtc::VideoStream>& layers) {
Åsa Persson55659812018-06-18 17:51:32 +0200195 if (layers.empty())
196 return 0;
197
Seth Hampson1370e302018-02-07 08:50:36 -0800198 int total_max_bitrate_bps = 0;
199 for (size_t s = 0; s < layers.size() - 1; ++s) {
200 total_max_bitrate_bps += layers[s].target_bitrate_bps;
201 }
202 total_max_bitrate_bps += layers.back().max_bitrate_bps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000203 return total_max_bitrate_bps;
204}
205
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200206std::vector<webrtc::VideoStream> GetSimulcastConfig(
207 size_t max_layers,
208 int width,
209 int height,
210 int /*max_bitrate_bps*/,
211 double bitrate_priority,
212 int max_qp,
Åsa Persson8c1bf952018-09-13 10:42:19 +0200213 int /*max_framerate*/,
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200214 bool is_screenshare,
215 bool temporal_layers_supported) {
Seth Hampson1370e302018-02-07 08:50:36 -0800216 if (is_screenshare) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200217 return GetScreenshareLayers(max_layers, width, height, bitrate_priority,
218 max_qp, ScreenshareSimulcastFieldTrialEnabled(),
219 temporal_layers_supported);
sprang429600d2017-01-26 06:12:26 -0800220 } else {
Åsa Persson55659812018-06-18 17:51:32 +0200221 return GetNormalSimulcastLayers(max_layers, width, height, bitrate_priority,
Åsa Persson8c1bf952018-09-13 10:42:19 +0200222 max_qp, temporal_layers_supported);
sprang429600d2017-01-26 06:12:26 -0800223 }
Seth Hampson1370e302018-02-07 08:50:36 -0800224}
sprang429600d2017-01-26 06:12:26 -0800225
Seth Hampson1370e302018-02-07 08:50:36 -0800226std::vector<webrtc::VideoStream> GetNormalSimulcastLayers(
227 size_t max_layers,
228 int width,
229 int height,
Seth Hampson1370e302018-02-07 08:50:36 -0800230 double bitrate_priority,
231 int max_qp,
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200232 bool temporal_layers_supported) {
Seth Hampson1370e302018-02-07 08:50:36 -0800233 // TODO(bugs.webrtc.org/8785): Currently if the resolution isn't large enough
234 // (defined in kSimulcastFormats) we scale down the number of simulcast
235 // layers. Consider changing this so that the application can have more
236 // control over exactly how many simulcast layers are used.
237 size_t num_simulcast_layers = FindSimulcastMaxLayers(width, height);
Åsa Persson645512b2018-09-14 16:42:58 +0200238 if (webrtc::field_trial::IsEnabled("WebRTC-SimulcastMaxLayers")) {
239 num_simulcast_layers = max_layers;
240 }
Seth Hampson1370e302018-02-07 08:50:36 -0800241 if (num_simulcast_layers > max_layers) {
242 // TODO(bugs.webrtc.org/8486): This scales down the resolution if the
243 // number of simulcast layers created by the application isn't sufficient
244 // (defined in kSimulcastFormats). For example if the input frame's
245 // resolution is HD, but there are only 2 simulcast layers, the
246 // resolution gets scaled down to VGA. Consider taking this logic out to
247 // allow the application more control over the resolutions.
248 SlotSimulcastMaxResolution(max_layers, &width, &height);
249 num_simulcast_layers = max_layers;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000250 }
Seth Hampson1370e302018-02-07 08:50:36 -0800251 std::vector<webrtc::VideoStream> layers(num_simulcast_layers);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000252
Seth Hampson1370e302018-02-07 08:50:36 -0800253 // Format width and height has to be divisible by |2 ^ num_simulcast_layers -
254 // 1|.
255 width = NormalizeSimulcastSize(width, num_simulcast_layers);
256 height = NormalizeSimulcastSize(height, num_simulcast_layers);
257 // Add simulcast streams, from highest resolution (|s| = num_simulcast_layers
258 // -1) to lowest resolution at |s| = 0.
259 for (size_t s = num_simulcast_layers - 1;; --s) {
260 layers[s].width = width;
261 layers[s].height = height;
262 // TODO(pbos): Fill actual temporal-layer bitrate thresholds.
263 layers[s].max_qp = max_qp;
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200264 layers[s].num_temporal_layers =
Erik Språng58b22842018-08-21 13:15:28 +0200265 temporal_layers_supported ? DefaultNumberOfTemporalLayers(s, false) : 0;
Seth Hampson1370e302018-02-07 08:50:36 -0800266 layers[s].max_bitrate_bps = FindSimulcastMaxBitrateBps(width, height);
267 layers[s].target_bitrate_bps = FindSimulcastTargetBitrateBps(width, height);
Erik Språng58b22842018-08-21 13:15:28 +0200268 int num_temporal_layers = DefaultNumberOfTemporalLayers(s, false);
Erik Språngd497e6b2018-07-06 17:17:10 +0200269 if (s == 0) {
Rasmus Brandt73d117f2018-10-02 11:12:52 +0200270 // If alternative temporal rate allocation is selected, adjust the
Erik Språngd92288f2018-07-04 10:07:40 +0200271 // bitrate of the lowest simulcast stream so that absolute bitrate for
272 // the base temporal layer matches the bitrate for the base temporal
273 // layer with the default 3 simulcast streams. Otherwise we risk a
274 // higher threshold for receiving a feed at all.
Erik Språngd497e6b2018-07-06 17:17:10 +0200275 float rate_factor = 1.0;
276 if (num_temporal_layers == 3) {
Rasmus Brandt73d117f2018-10-02 11:12:52 +0200277 if (webrtc::field_trial::IsEnabled(
278 kUseBaseHeavyVP8TL3RateAllocationFieldTrial)) {
279 // Base heavy allocation increases TL0 bitrate from 40% to 60%.
Erik Språngd497e6b2018-07-06 17:17:10 +0200280 rate_factor = 0.4 / 0.6;
281 }
282 } else {
283 rate_factor =
284 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(3, 0) /
285 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
286 num_temporal_layers, 0);
287 }
288
Erik Språng79478ad2018-05-18 09:39:55 +0200289 layers[s].max_bitrate_bps =
290 static_cast<int>(layers[s].max_bitrate_bps * rate_factor);
291 layers[s].target_bitrate_bps =
292 static_cast<int>(layers[s].target_bitrate_bps * rate_factor);
293 }
Seth Hampson1370e302018-02-07 08:50:36 -0800294 layers[s].min_bitrate_bps = FindSimulcastMinBitrateBps(width, height);
Åsa Persson8c1bf952018-09-13 10:42:19 +0200295 layers[s].max_framerate = kDefaultVideoMaxFramerate;
sprang02569ad2017-07-06 05:05:50 -0700296
Seth Hampson1370e302018-02-07 08:50:36 -0800297 width /= 2;
298 height /= 2;
sprang02569ad2017-07-06 05:05:50 -0700299
Seth Hampson1370e302018-02-07 08:50:36 -0800300 if (s == 0) {
301 break;
sprang02569ad2017-07-06 05:05:50 -0700302 }
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000303 }
Seth Hampson1370e302018-02-07 08:50:36 -0800304 // Currently the relative bitrate priority of the sender is controlled by
305 // the value of the lowest VideoStream.
306 // TODO(bugs.webrtc.org/8630): The web specification describes being able to
307 // control relative bitrate for each individual simulcast layer, but this
308 // is currently just implemented per rtp sender.
309 layers[0].bitrate_priority = bitrate_priority;
310 return layers;
311}
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000312
Seth Hampson1370e302018-02-07 08:50:36 -0800313std::vector<webrtc::VideoStream> GetScreenshareLayers(
314 size_t max_layers,
315 int width,
316 int height,
Seth Hampson1370e302018-02-07 08:50:36 -0800317 double bitrate_priority,
318 int max_qp,
Ilya Nikolaevskiy3df1d5d2018-08-22 09:26:51 +0200319 bool screenshare_simulcast_enabled,
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200320 bool temporal_layers_supported) {
Ilya Nikolaevskiy3df1d5d2018-08-22 09:26:51 +0200321 auto max_screenshare_layers =
322 screenshare_simulcast_enabled ? kMaxScreenshareSimulcastLayers : 1;
Seth Hampson1370e302018-02-07 08:50:36 -0800323 size_t num_simulcast_layers =
Ilya Nikolaevskiy3df1d5d2018-08-22 09:26:51 +0200324 std::min<int>(max_layers, max_screenshare_layers);
Seth Hampson1370e302018-02-07 08:50:36 -0800325
326 std::vector<webrtc::VideoStream> layers(num_simulcast_layers);
Seth Hampson1370e302018-02-07 08:50:36 -0800327 // For legacy screenshare in conference mode, tl0 and tl1 bitrates are
328 // piggybacked on the VideoCodec struct as target and max bitrates,
Erik Språngcc681cc2018-03-14 17:52:55 +0100329 // respectively. See eg. webrtc::LibvpxVp8Encoder::SetRates().
Seth Hampson1370e302018-02-07 08:50:36 -0800330 layers[0].width = width;
331 layers[0].height = height;
332 layers[0].max_qp = max_qp;
333 layers[0].max_framerate = 5;
334 layers[0].min_bitrate_bps = kMinVideoBitrateBps;
Rasmus Brandt195d1d72018-05-09 11:28:01 +0200335 layers[0].target_bitrate_bps = kScreenshareDefaultTl0BitrateKbps * 1000;
336 layers[0].max_bitrate_bps = kScreenshareDefaultTl1BitrateKbps * 1000;
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200337 layers[0].num_temporal_layers = temporal_layers_supported ? 2 : 0;
Seth Hampson1370e302018-02-07 08:50:36 -0800338
339 // With simulcast enabled, add another spatial layer. This one will have a
340 // more normal layout, with the regular 3 temporal layer pattern and no fps
341 // restrictions. The base simulcast layer will still use legacy setup.
342 if (num_simulcast_layers == kMaxScreenshareSimulcastLayers) {
343 // Add optional upper simulcast layer.
Erik Språng58b22842018-08-21 13:15:28 +0200344 const int num_temporal_layers = DefaultNumberOfTemporalLayers(1, true);
Erik Språngb6b1cac2018-08-09 16:12:54 +0200345 int max_bitrate_bps;
Erik Språngcf919422018-09-17 16:18:57 +0200346 bool using_boosted_bitrate = false;
Erik Språngb6b1cac2018-08-09 16:12:54 +0200347 if (!temporal_layers_supported) {
348 // Set the max bitrate to where the base layer would have been if temporal
Erik Språng58b22842018-08-21 13:15:28 +0200349 // layers were enabled.
Erik Språngb6b1cac2018-08-09 16:12:54 +0200350 max_bitrate_bps = static_cast<int>(
351 kScreenshareHighStreamMaxBitrateBps *
352 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
353 num_temporal_layers, 0));
Erik Språng58b22842018-08-21 13:15:28 +0200354 } else if (DefaultNumberOfTemporalLayers(1, true) != 3 ||
Rasmus Brandt73d117f2018-10-02 11:12:52 +0200355 webrtc::field_trial::IsEnabled(
356 kUseBaseHeavyVP8TL3RateAllocationFieldTrial)) {
Erik Språngb6b1cac2018-08-09 16:12:54 +0200357 // Experimental temporal layer mode used, use increased max bitrate.
358 max_bitrate_bps = kScreenshareHighStreamMaxBitrateBps;
Erik Språngcf919422018-09-17 16:18:57 +0200359 using_boosted_bitrate = true;
Erik Språngb6b1cac2018-08-09 16:12:54 +0200360 } else {
361 // Keep current bitrates with default 3tl/8 frame settings.
362 // Lowest temporal layers of a 3 layer setup will have 40% of the total
363 // bitrate allocation for that simulcast layer. Make sure the gap between
364 // the target of the lower simulcast layer and first temporal layer of the
365 // higher one is at most 2x the bitrate, so that upswitching is not
366 // hampered by stalled bitrate estimates.
367 max_bitrate_bps = 2 * ((layers[0].target_bitrate_bps * 10) / 4);
368 }
369
Seth Hampson1370e302018-02-07 08:50:36 -0800370 layers[1].width = width;
371 layers[1].height = height;
372 layers[1].max_qp = max_qp;
Åsa Persson8c1bf952018-09-13 10:42:19 +0200373 layers[1].max_framerate = kDefaultVideoMaxFramerate;
Erik Språngb6b1cac2018-08-09 16:12:54 +0200374 layers[1].num_temporal_layers =
Erik Språng58b22842018-08-21 13:15:28 +0200375 temporal_layers_supported ? DefaultNumberOfTemporalLayers(1, true) : 0;
Erik Språngcf919422018-09-17 16:18:57 +0200376 layers[1].min_bitrate_bps = using_boosted_bitrate
377 ? kScreenshareHighStreamMinBitrateBps
378 : layers[0].target_bitrate_bps * 2;
379
380 // Cap max bitrate so it isn't overly high for the given resolution.
381 int resolution_limited_bitrate = std::max(
382 FindSimulcastMaxBitrateBps(width, height), layers[1].min_bitrate_bps);
383 max_bitrate_bps =
384 std::min<int>(max_bitrate_bps, resolution_limited_bitrate);
385
Seth Hampson1370e302018-02-07 08:50:36 -0800386 layers[1].target_bitrate_bps = max_bitrate_bps;
387 layers[1].max_bitrate_bps = max_bitrate_bps;
388 }
389
Seth Hampson24722b32017-12-22 09:36:42 -0800390 // The bitrate priority currently implemented on a per-sender level, so we
Seth Hampson1370e302018-02-07 08:50:36 -0800391 // just set it for the first simulcast layer.
392 layers[0].bitrate_priority = bitrate_priority;
393 return layers;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000394}
395
Ilya Nikolaevskiy3df1d5d2018-08-22 09:26:51 +0200396bool ScreenshareSimulcastFieldTrialEnabled() {
Erik Språngb65aa012018-09-24 11:35:19 +0200397 return webrtc::field_trial::IsEnabled(kSimulcastScreenshareFieldTrialName);
Ilya Nikolaevskiy3df1d5d2018-08-22 09:26:51 +0200398}
399
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000400} // namespace cricket