blob: c42e1dcae290840a9b398cc830d12c81cf6161c6 [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"
21#include "rtc_base/logging.h"
22#include "system_wrappers/include/field_trial.h"
tfarina5237aaf2015-11-10 23:44:30 -080023
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000024namespace cricket {
25
Rasmus Brandt195d1d72018-05-09 11:28:01 +020026namespace {
27
Rasmus Brandt73d117f2018-10-02 11:12:52 +020028constexpr char kUseBaseHeavyVP8TL3RateAllocationFieldTrial[] =
29 "WebRTC-UseBaseHeavyVP8TL3RateAllocation";
30
Erik Språngb6b1cac2018-08-09 16:12:54 +020031// Limits for legacy conference screensharing mode. Currently used for the
32// lower of the two simulcast streams.
Rasmus Brandt195d1d72018-05-09 11:28:01 +020033constexpr int kScreenshareDefaultTl0BitrateKbps = 200;
34constexpr int kScreenshareDefaultTl1BitrateKbps = 1000;
35
Erik Språng661a0c62018-09-11 12:33:21 +020036// Min/max bitrate for the higher one of the two simulcast stream used for
37// screen content.
38constexpr int kScreenshareHighStreamMinBitrateBps = 600000;
Erik Språng58b22842018-08-21 13:15:28 +020039constexpr int kScreenshareHighStreamMaxBitrateBps = 1250000;
Ilya Nikolaevskiy3df1d5d2018-08-22 09:26:51 +020040static const char* kSimulcastScreenshareFieldTrialName =
41 "WebRTC-SimulcastScreenshare";
Erik Språngb6b1cac2018-08-09 16:12:54 +020042
Rasmus Brandt195d1d72018-05-09 11:28:01 +020043} // namespace
44
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000045struct SimulcastFormat {
46 int width;
47 int height;
48 // The maximum number of simulcast layers can be used for
49 // resolutions at |widthxheigh|.
50 size_t max_layers;
51 // The maximum bitrate for encoding stream at |widthxheight|, when we are
52 // not sending the next higher spatial stream.
pbosbe16f792015-10-16 12:49:39 -070053 int max_bitrate_kbps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000054 // The target bitrate for encoding stream at |widthxheight|, when this layer
55 // is not the highest layer (i.e., when we are sending another higher spatial
56 // stream).
pbosbe16f792015-10-16 12:49:39 -070057 int target_bitrate_kbps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000058 // The minimum bitrate needed for encoding stream at |widthxheight|.
pbosbe16f792015-10-16 12:49:39 -070059 int min_bitrate_kbps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000060};
61
62// These tables describe from which resolution we can use how many
63// simulcast layers at what bitrates (maximum, target, and minimum).
64// Important!! Keep this table from high resolution to low resolution.
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +020065// clang-format off
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000066const SimulcastFormat kSimulcastFormats[] = {
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +020067 {1920, 1080, 3, 5000, 4000, 800},
68 {1280, 720, 3, 2500, 2500, 600},
69 {960, 540, 3, 900, 900, 450},
70 {640, 360, 2, 700, 500, 150},
71 {480, 270, 2, 450, 350, 150},
72 {320, 180, 1, 200, 150, 30},
73 {0, 0, 1, 200, 150, 30}
74};
75// clang-format on
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000076
Seth Hampson1370e302018-02-07 08:50:36 -080077const int kMaxScreenshareSimulcastLayers = 2;
sprang429600d2017-01-26 06:12:26 -080078
Erik Språngbfe3d852018-05-15 09:54:14 +020079// Multiway: Number of temporal layers for each simulcast stream.
Erik Språng58b22842018-08-21 13:15:28 +020080int DefaultNumberOfTemporalLayers(int simulcast_id, bool screenshare) {
Erik Språngbfe3d852018-05-15 09:54:14 +020081 RTC_CHECK_GE(simulcast_id, 0);
82 RTC_CHECK_LT(simulcast_id, webrtc::kMaxSimulcastStreams);
83
84 const int kDefaultNumTemporalLayers = 3;
Erik Språng7461eff2018-09-10 09:43:56 +020085 const int kDefaultNumScreenshareTemporalLayers = 2;
86 int default_num_temporal_layers = screenshare
87 ? kDefaultNumScreenshareTemporalLayers
88 : kDefaultNumTemporalLayers;
Erik Språngbfe3d852018-05-15 09:54:14 +020089
90 const std::string group_name =
Erik Språng58b22842018-08-21 13:15:28 +020091 screenshare ? webrtc::field_trial::FindFullName(
92 "WebRTC-VP8ScreenshareTemporalLayers")
93 : webrtc::field_trial::FindFullName(
94 "WebRTC-VP8ConferenceTemporalLayers");
Erik Språngbfe3d852018-05-15 09:54:14 +020095 if (group_name.empty())
Erik Språng7461eff2018-09-10 09:43:56 +020096 return default_num_temporal_layers;
Erik Språngbfe3d852018-05-15 09:54:14 +020097
Erik Språng7461eff2018-09-10 09:43:56 +020098 int num_temporal_layers = default_num_temporal_layers;
Erik Språngbfe3d852018-05-15 09:54:14 +020099 if (sscanf(group_name.c_str(), "%d", &num_temporal_layers) == 1 &&
100 num_temporal_layers > 0 &&
101 num_temporal_layers <= webrtc::kMaxTemporalStreams) {
102 return num_temporal_layers;
103 }
104
105 RTC_LOG(LS_WARNING) << "Attempt to set number of temporal layers to "
106 "incorrect value: "
107 << group_name;
108
Erik Språng7461eff2018-09-10 09:43:56 +0200109 return default_num_temporal_layers;
Erik Språngbfe3d852018-05-15 09:54:14 +0200110}
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000111
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000112int FindSimulcastFormatIndex(int width, int height) {
Seth Hampson438663e2018-01-09 11:14:14 -0800113 RTC_DCHECK_GE(width, 0);
114 RTC_DCHECK_GE(height, 0);
kjellandera96e2d72016-02-04 23:52:28 -0800115 for (uint32_t i = 0; i < arraysize(kSimulcastFormats); ++i) {
sprang429600d2017-01-26 06:12:26 -0800116 if (width * height >=
117 kSimulcastFormats[i].width * kSimulcastFormats[i].height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000118 return i;
119 }
120 }
Seth Hampson438663e2018-01-09 11:14:14 -0800121 RTC_NOTREACHED();
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000122 return -1;
123}
124
125int FindSimulcastFormatIndex(int width, int height, size_t max_layers) {
Seth Hampson438663e2018-01-09 11:14:14 -0800126 RTC_DCHECK_GE(width, 0);
127 RTC_DCHECK_GE(height, 0);
128 RTC_DCHECK_GT(max_layers, 0);
kjellandera96e2d72016-02-04 23:52:28 -0800129 for (uint32_t i = 0; i < arraysize(kSimulcastFormats); ++i) {
sprang429600d2017-01-26 06:12:26 -0800130 if (width * height >=
131 kSimulcastFormats[i].width * kSimulcastFormats[i].height &&
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000132 max_layers == kSimulcastFormats[i].max_layers) {
133 return i;
134 }
135 }
Seth Hampson438663e2018-01-09 11:14:14 -0800136 RTC_NOTREACHED();
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000137 return -1;
138}
139
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000140// Simulcast stream width and height must both be dividable by
Rasmus Brandtefbdcb02018-04-26 17:47:42 +0200141// |2 ^ (simulcast_layers - 1)|.
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000142int NormalizeSimulcastSize(int size, size_t simulcast_layers) {
143 const int base2_exponent = static_cast<int>(simulcast_layers) - 1;
144 return ((size >> base2_exponent) << base2_exponent);
145}
146
147size_t FindSimulcastMaxLayers(int width, int height) {
148 int index = FindSimulcastFormatIndex(width, height);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000149 return kSimulcastFormats[index].max_layers;
150}
151
sprang429600d2017-01-26 06:12:26 -0800152int FindSimulcastMaxBitrateBps(int width, int height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000153 const int format_index = FindSimulcastFormatIndex(width, height);
pbosbe16f792015-10-16 12:49:39 -0700154 return kSimulcastFormats[format_index].max_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000155}
156
sprang429600d2017-01-26 06:12:26 -0800157int FindSimulcastTargetBitrateBps(int width, int height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000158 const int format_index = FindSimulcastFormatIndex(width, height);
pbosbe16f792015-10-16 12:49:39 -0700159 return kSimulcastFormats[format_index].target_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000160}
161
sprang429600d2017-01-26 06:12:26 -0800162int FindSimulcastMinBitrateBps(int width, int height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000163 const int format_index = FindSimulcastFormatIndex(width, height);
pbosbe16f792015-10-16 12:49:39 -0700164 return kSimulcastFormats[format_index].min_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000165}
166
Seth Hampson438663e2018-01-09 11:14:14 -0800167void SlotSimulcastMaxResolution(size_t max_layers, int* width, int* height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000168 int index = FindSimulcastFormatIndex(*width, *height, max_layers);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000169 *width = kSimulcastFormats[index].width;
170 *height = kSimulcastFormats[index].height;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100171 RTC_LOG(LS_INFO) << "SlotSimulcastMaxResolution to width:" << *width
172 << " height:" << *height;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000173}
174
Seth Hampson1370e302018-02-07 08:50:36 -0800175void BoostMaxSimulcastLayer(int max_bitrate_bps,
176 std::vector<webrtc::VideoStream>* layers) {
Åsa Persson55659812018-06-18 17:51:32 +0200177 if (layers->empty())
178 return;
179
Seth Hampson1370e302018-02-07 08:50:36 -0800180 // Spend additional bits to boost the max layer.
181 int bitrate_left_bps = max_bitrate_bps - GetTotalMaxBitrateBps(*layers);
182 if (bitrate_left_bps > 0) {
183 layers->back().max_bitrate_bps += bitrate_left_bps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000184 }
Seth Hampson1370e302018-02-07 08:50:36 -0800185}
186
187int GetTotalMaxBitrateBps(const std::vector<webrtc::VideoStream>& layers) {
Åsa Persson55659812018-06-18 17:51:32 +0200188 if (layers.empty())
189 return 0;
190
Seth Hampson1370e302018-02-07 08:50:36 -0800191 int total_max_bitrate_bps = 0;
192 for (size_t s = 0; s < layers.size() - 1; ++s) {
193 total_max_bitrate_bps += layers[s].target_bitrate_bps;
194 }
195 total_max_bitrate_bps += layers.back().max_bitrate_bps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000196 return total_max_bitrate_bps;
197}
198
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200199std::vector<webrtc::VideoStream> GetSimulcastConfig(
200 size_t max_layers,
201 int width,
202 int height,
203 int /*max_bitrate_bps*/,
204 double bitrate_priority,
205 int max_qp,
Åsa Persson8c1bf952018-09-13 10:42:19 +0200206 int /*max_framerate*/,
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200207 bool is_screenshare,
208 bool temporal_layers_supported) {
Seth Hampson1370e302018-02-07 08:50:36 -0800209 if (is_screenshare) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200210 return GetScreenshareLayers(max_layers, width, height, bitrate_priority,
211 max_qp, ScreenshareSimulcastFieldTrialEnabled(),
212 temporal_layers_supported);
sprang429600d2017-01-26 06:12:26 -0800213 } else {
Åsa Persson55659812018-06-18 17:51:32 +0200214 return GetNormalSimulcastLayers(max_layers, width, height, bitrate_priority,
Åsa Persson8c1bf952018-09-13 10:42:19 +0200215 max_qp, temporal_layers_supported);
sprang429600d2017-01-26 06:12:26 -0800216 }
Seth Hampson1370e302018-02-07 08:50:36 -0800217}
sprang429600d2017-01-26 06:12:26 -0800218
Seth Hampson1370e302018-02-07 08:50:36 -0800219std::vector<webrtc::VideoStream> GetNormalSimulcastLayers(
220 size_t max_layers,
221 int width,
222 int height,
Seth Hampson1370e302018-02-07 08:50:36 -0800223 double bitrate_priority,
224 int max_qp,
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200225 bool temporal_layers_supported) {
Seth Hampson1370e302018-02-07 08:50:36 -0800226 // TODO(bugs.webrtc.org/8785): Currently if the resolution isn't large enough
227 // (defined in kSimulcastFormats) we scale down the number of simulcast
228 // layers. Consider changing this so that the application can have more
229 // control over exactly how many simulcast layers are used.
230 size_t num_simulcast_layers = FindSimulcastMaxLayers(width, height);
Åsa Persson645512b2018-09-14 16:42:58 +0200231 if (webrtc::field_trial::IsEnabled("WebRTC-SimulcastMaxLayers")) {
232 num_simulcast_layers = max_layers;
233 }
Seth Hampson1370e302018-02-07 08:50:36 -0800234 if (num_simulcast_layers > max_layers) {
235 // TODO(bugs.webrtc.org/8486): This scales down the resolution if the
236 // number of simulcast layers created by the application isn't sufficient
237 // (defined in kSimulcastFormats). For example if the input frame's
238 // resolution is HD, but there are only 2 simulcast layers, the
239 // resolution gets scaled down to VGA. Consider taking this logic out to
240 // allow the application more control over the resolutions.
241 SlotSimulcastMaxResolution(max_layers, &width, &height);
242 num_simulcast_layers = max_layers;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000243 }
Seth Hampson1370e302018-02-07 08:50:36 -0800244 std::vector<webrtc::VideoStream> layers(num_simulcast_layers);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000245
Seth Hampson1370e302018-02-07 08:50:36 -0800246 // Format width and height has to be divisible by |2 ^ num_simulcast_layers -
247 // 1|.
248 width = NormalizeSimulcastSize(width, num_simulcast_layers);
249 height = NormalizeSimulcastSize(height, num_simulcast_layers);
250 // Add simulcast streams, from highest resolution (|s| = num_simulcast_layers
251 // -1) to lowest resolution at |s| = 0.
252 for (size_t s = num_simulcast_layers - 1;; --s) {
253 layers[s].width = width;
254 layers[s].height = height;
255 // TODO(pbos): Fill actual temporal-layer bitrate thresholds.
256 layers[s].max_qp = max_qp;
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200257 layers[s].num_temporal_layers =
Erik Språng58b22842018-08-21 13:15:28 +0200258 temporal_layers_supported ? DefaultNumberOfTemporalLayers(s, false) : 0;
Seth Hampson1370e302018-02-07 08:50:36 -0800259 layers[s].max_bitrate_bps = FindSimulcastMaxBitrateBps(width, height);
260 layers[s].target_bitrate_bps = FindSimulcastTargetBitrateBps(width, height);
Erik Språng58b22842018-08-21 13:15:28 +0200261 int num_temporal_layers = DefaultNumberOfTemporalLayers(s, false);
Erik Språngd497e6b2018-07-06 17:17:10 +0200262 if (s == 0) {
Rasmus Brandt73d117f2018-10-02 11:12:52 +0200263 // If alternative temporal rate allocation is selected, adjust the
Erik Språngd92288f2018-07-04 10:07:40 +0200264 // bitrate of the lowest simulcast stream so that absolute bitrate for
265 // the base temporal layer matches the bitrate for the base temporal
266 // layer with the default 3 simulcast streams. Otherwise we risk a
267 // higher threshold for receiving a feed at all.
Erik Språngd497e6b2018-07-06 17:17:10 +0200268 float rate_factor = 1.0;
269 if (num_temporal_layers == 3) {
Rasmus Brandt73d117f2018-10-02 11:12:52 +0200270 if (webrtc::field_trial::IsEnabled(
271 kUseBaseHeavyVP8TL3RateAllocationFieldTrial)) {
272 // Base heavy allocation increases TL0 bitrate from 40% to 60%.
Erik Språngd497e6b2018-07-06 17:17:10 +0200273 rate_factor = 0.4 / 0.6;
274 }
275 } else {
276 rate_factor =
277 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(3, 0) /
278 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
279 num_temporal_layers, 0);
280 }
281
Erik Språng79478ad2018-05-18 09:39:55 +0200282 layers[s].max_bitrate_bps =
283 static_cast<int>(layers[s].max_bitrate_bps * rate_factor);
284 layers[s].target_bitrate_bps =
285 static_cast<int>(layers[s].target_bitrate_bps * rate_factor);
286 }
Seth Hampson1370e302018-02-07 08:50:36 -0800287 layers[s].min_bitrate_bps = FindSimulcastMinBitrateBps(width, height);
Åsa Persson8c1bf952018-09-13 10:42:19 +0200288 layers[s].max_framerate = kDefaultVideoMaxFramerate;
sprang02569ad2017-07-06 05:05:50 -0700289
Seth Hampson1370e302018-02-07 08:50:36 -0800290 width /= 2;
291 height /= 2;
sprang02569ad2017-07-06 05:05:50 -0700292
Seth Hampson1370e302018-02-07 08:50:36 -0800293 if (s == 0) {
294 break;
sprang02569ad2017-07-06 05:05:50 -0700295 }
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000296 }
Seth Hampson1370e302018-02-07 08:50:36 -0800297 // Currently the relative bitrate priority of the sender is controlled by
298 // the value of the lowest VideoStream.
299 // TODO(bugs.webrtc.org/8630): The web specification describes being able to
300 // control relative bitrate for each individual simulcast layer, but this
301 // is currently just implemented per rtp sender.
302 layers[0].bitrate_priority = bitrate_priority;
303 return layers;
304}
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000305
Seth Hampson1370e302018-02-07 08:50:36 -0800306std::vector<webrtc::VideoStream> GetScreenshareLayers(
307 size_t max_layers,
308 int width,
309 int height,
Seth Hampson1370e302018-02-07 08:50:36 -0800310 double bitrate_priority,
311 int max_qp,
Ilya Nikolaevskiy3df1d5d2018-08-22 09:26:51 +0200312 bool screenshare_simulcast_enabled,
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200313 bool temporal_layers_supported) {
Ilya Nikolaevskiy3df1d5d2018-08-22 09:26:51 +0200314 auto max_screenshare_layers =
315 screenshare_simulcast_enabled ? kMaxScreenshareSimulcastLayers : 1;
Seth Hampson1370e302018-02-07 08:50:36 -0800316 size_t num_simulcast_layers =
Ilya Nikolaevskiy3df1d5d2018-08-22 09:26:51 +0200317 std::min<int>(max_layers, max_screenshare_layers);
Seth Hampson1370e302018-02-07 08:50:36 -0800318
319 std::vector<webrtc::VideoStream> layers(num_simulcast_layers);
Seth Hampson1370e302018-02-07 08:50:36 -0800320 // For legacy screenshare in conference mode, tl0 and tl1 bitrates are
321 // piggybacked on the VideoCodec struct as target and max bitrates,
Erik Språngcc681cc2018-03-14 17:52:55 +0100322 // respectively. See eg. webrtc::LibvpxVp8Encoder::SetRates().
Seth Hampson1370e302018-02-07 08:50:36 -0800323 layers[0].width = width;
324 layers[0].height = height;
325 layers[0].max_qp = max_qp;
326 layers[0].max_framerate = 5;
327 layers[0].min_bitrate_bps = kMinVideoBitrateBps;
Rasmus Brandt195d1d72018-05-09 11:28:01 +0200328 layers[0].target_bitrate_bps = kScreenshareDefaultTl0BitrateKbps * 1000;
329 layers[0].max_bitrate_bps = kScreenshareDefaultTl1BitrateKbps * 1000;
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200330 layers[0].num_temporal_layers = temporal_layers_supported ? 2 : 0;
Seth Hampson1370e302018-02-07 08:50:36 -0800331
332 // With simulcast enabled, add another spatial layer. This one will have a
333 // more normal layout, with the regular 3 temporal layer pattern and no fps
334 // restrictions. The base simulcast layer will still use legacy setup.
335 if (num_simulcast_layers == kMaxScreenshareSimulcastLayers) {
336 // Add optional upper simulcast layer.
Erik Språng58b22842018-08-21 13:15:28 +0200337 const int num_temporal_layers = DefaultNumberOfTemporalLayers(1, true);
Erik Språngb6b1cac2018-08-09 16:12:54 +0200338 int max_bitrate_bps;
Erik Språngcf919422018-09-17 16:18:57 +0200339 bool using_boosted_bitrate = false;
Erik Språngb6b1cac2018-08-09 16:12:54 +0200340 if (!temporal_layers_supported) {
341 // Set the max bitrate to where the base layer would have been if temporal
Erik Språng58b22842018-08-21 13:15:28 +0200342 // layers were enabled.
Erik Språngb6b1cac2018-08-09 16:12:54 +0200343 max_bitrate_bps = static_cast<int>(
344 kScreenshareHighStreamMaxBitrateBps *
345 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
346 num_temporal_layers, 0));
Erik Språng58b22842018-08-21 13:15:28 +0200347 } else if (DefaultNumberOfTemporalLayers(1, true) != 3 ||
Rasmus Brandt73d117f2018-10-02 11:12:52 +0200348 webrtc::field_trial::IsEnabled(
349 kUseBaseHeavyVP8TL3RateAllocationFieldTrial)) {
Erik Språngb6b1cac2018-08-09 16:12:54 +0200350 // Experimental temporal layer mode used, use increased max bitrate.
351 max_bitrate_bps = kScreenshareHighStreamMaxBitrateBps;
Erik Språngcf919422018-09-17 16:18:57 +0200352 using_boosted_bitrate = true;
Erik Språngb6b1cac2018-08-09 16:12:54 +0200353 } else {
354 // Keep current bitrates with default 3tl/8 frame settings.
355 // Lowest temporal layers of a 3 layer setup will have 40% of the total
356 // bitrate allocation for that simulcast layer. Make sure the gap between
357 // the target of the lower simulcast layer and first temporal layer of the
358 // higher one is at most 2x the bitrate, so that upswitching is not
359 // hampered by stalled bitrate estimates.
360 max_bitrate_bps = 2 * ((layers[0].target_bitrate_bps * 10) / 4);
361 }
362
Seth Hampson1370e302018-02-07 08:50:36 -0800363 layers[1].width = width;
364 layers[1].height = height;
365 layers[1].max_qp = max_qp;
Åsa Persson8c1bf952018-09-13 10:42:19 +0200366 layers[1].max_framerate = kDefaultVideoMaxFramerate;
Erik Språngb6b1cac2018-08-09 16:12:54 +0200367 layers[1].num_temporal_layers =
Erik Språng58b22842018-08-21 13:15:28 +0200368 temporal_layers_supported ? DefaultNumberOfTemporalLayers(1, true) : 0;
Erik Språngcf919422018-09-17 16:18:57 +0200369 layers[1].min_bitrate_bps = using_boosted_bitrate
370 ? kScreenshareHighStreamMinBitrateBps
371 : layers[0].target_bitrate_bps * 2;
372
373 // Cap max bitrate so it isn't overly high for the given resolution.
374 int resolution_limited_bitrate = std::max(
375 FindSimulcastMaxBitrateBps(width, height), layers[1].min_bitrate_bps);
376 max_bitrate_bps =
377 std::min<int>(max_bitrate_bps, resolution_limited_bitrate);
378
Seth Hampson1370e302018-02-07 08:50:36 -0800379 layers[1].target_bitrate_bps = max_bitrate_bps;
380 layers[1].max_bitrate_bps = max_bitrate_bps;
381 }
382
Seth Hampson24722b32017-12-22 09:36:42 -0800383 // The bitrate priority currently implemented on a per-sender level, so we
Seth Hampson1370e302018-02-07 08:50:36 -0800384 // just set it for the first simulcast layer.
385 layers[0].bitrate_priority = bitrate_priority;
386 return layers;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000387}
388
Ilya Nikolaevskiy3df1d5d2018-08-22 09:26:51 +0200389bool ScreenshareSimulcastFieldTrialEnabled() {
Erik Språngb65aa012018-09-24 11:35:19 +0200390 return webrtc::field_trial::IsEnabled(kSimulcastScreenshareFieldTrialName);
Ilya Nikolaevskiy3df1d5d2018-08-22 09:26:51 +0200391}
392
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000393} // namespace cricket