blob: 0103051bcc051645d37042073dd6b304d69d21a0 [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
Yves Gerey3e707812018-11-28 16:47:49 +010011#include <stdint.h>
sprang@webrtc.org46d4d292014-12-23 15:19:35 +000012#include <stdio.h>
Steve Antone78bcb92017-10-31 09:53:08 -070013#include <algorithm>
14#include <string>
sprang@webrtc.org46d4d292014-12-23 15:19:35 +000015
Yves Gerey3e707812018-11-28 16:47:49 +010016#include "absl/types/optional.h"
17#include "api/video/video_bitrate_allocation.h"
Åsa Persson8c1bf952018-09-13 10:42:19 +020018#include "media/base/mediaconstants.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "media/engine/constants.h"
20#include "media/engine/simulcast.h"
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +020021#include "modules/video_coding/utility/simulcast_rate_allocator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/arraysize.h"
Yves Gerey3e707812018-11-28 16:47:49 +010023#include "rtc_base/checks.h"
Åsa Persson1a35fbd2018-10-12 17:36:57 +020024#include "rtc_base/experiments/normalize_simulcast_size_experiment.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "rtc_base/logging.h"
26#include "system_wrappers/include/field_trial.h"
tfarina5237aaf2015-11-10 23:44:30 -080027
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000028namespace cricket {
29
Rasmus Brandt195d1d72018-05-09 11:28:01 +020030namespace {
31
Rasmus Brandt73d117f2018-10-02 11:12:52 +020032constexpr char kUseBaseHeavyVP8TL3RateAllocationFieldTrial[] =
33 "WebRTC-UseBaseHeavyVP8TL3RateAllocation";
34
Erik Språngb6b1cac2018-08-09 16:12:54 +020035// Limits for legacy conference screensharing mode. Currently used for the
36// lower of the two simulcast streams.
Rasmus Brandt195d1d72018-05-09 11:28:01 +020037constexpr int kScreenshareDefaultTl0BitrateKbps = 200;
38constexpr int kScreenshareDefaultTl1BitrateKbps = 1000;
39
Erik Språng661a0c62018-09-11 12:33:21 +020040// Min/max bitrate for the higher one of the two simulcast stream used for
41// screen content.
42constexpr int kScreenshareHighStreamMinBitrateBps = 600000;
Erik Språng58b22842018-08-21 13:15:28 +020043constexpr int kScreenshareHighStreamMaxBitrateBps = 1250000;
Ilya Nikolaevskiy3df1d5d2018-08-22 09:26:51 +020044static const char* kSimulcastScreenshareFieldTrialName =
45 "WebRTC-SimulcastScreenshare";
Erik Språngb6b1cac2018-08-09 16:12:54 +020046
Rasmus Brandt195d1d72018-05-09 11:28:01 +020047} // namespace
48
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000049struct SimulcastFormat {
50 int width;
51 int height;
52 // The maximum number of simulcast layers can be used for
53 // resolutions at |widthxheigh|.
54 size_t max_layers;
55 // The maximum bitrate for encoding stream at |widthxheight|, when we are
56 // not sending the next higher spatial stream.
pbosbe16f792015-10-16 12:49:39 -070057 int max_bitrate_kbps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000058 // The target bitrate for encoding stream at |widthxheight|, when this layer
59 // is not the highest layer (i.e., when we are sending another higher spatial
60 // stream).
pbosbe16f792015-10-16 12:49:39 -070061 int target_bitrate_kbps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000062 // The minimum bitrate needed for encoding stream at |widthxheight|.
pbosbe16f792015-10-16 12:49:39 -070063 int min_bitrate_kbps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000064};
65
66// These tables describe from which resolution we can use how many
67// simulcast layers at what bitrates (maximum, target, and minimum).
68// Important!! Keep this table from high resolution to low resolution.
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +020069// clang-format off
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000070const SimulcastFormat kSimulcastFormats[] = {
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +020071 {1920, 1080, 3, 5000, 4000, 800},
72 {1280, 720, 3, 2500, 2500, 600},
73 {960, 540, 3, 900, 900, 450},
74 {640, 360, 2, 700, 500, 150},
75 {480, 270, 2, 450, 350, 150},
76 {320, 180, 1, 200, 150, 30},
77 {0, 0, 1, 200, 150, 30}
78};
79// clang-format on
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000080
Seth Hampson1370e302018-02-07 08:50:36 -080081const int kMaxScreenshareSimulcastLayers = 2;
sprang429600d2017-01-26 06:12:26 -080082
Erik Språngbfe3d852018-05-15 09:54:14 +020083// Multiway: Number of temporal layers for each simulcast stream.
Erik Språng58b22842018-08-21 13:15:28 +020084int DefaultNumberOfTemporalLayers(int simulcast_id, bool screenshare) {
Erik Språngbfe3d852018-05-15 09:54:14 +020085 RTC_CHECK_GE(simulcast_id, 0);
86 RTC_CHECK_LT(simulcast_id, webrtc::kMaxSimulcastStreams);
87
88 const int kDefaultNumTemporalLayers = 3;
Erik Språng7461eff2018-09-10 09:43:56 +020089 const int kDefaultNumScreenshareTemporalLayers = 2;
90 int default_num_temporal_layers = screenshare
91 ? kDefaultNumScreenshareTemporalLayers
92 : kDefaultNumTemporalLayers;
Erik Språngbfe3d852018-05-15 09:54:14 +020093
94 const std::string group_name =
Erik Språng58b22842018-08-21 13:15:28 +020095 screenshare ? webrtc::field_trial::FindFullName(
96 "WebRTC-VP8ScreenshareTemporalLayers")
97 : webrtc::field_trial::FindFullName(
98 "WebRTC-VP8ConferenceTemporalLayers");
Erik Språngbfe3d852018-05-15 09:54:14 +020099 if (group_name.empty())
Erik Språng7461eff2018-09-10 09:43:56 +0200100 return default_num_temporal_layers;
Erik Språngbfe3d852018-05-15 09:54:14 +0200101
Erik Språng7461eff2018-09-10 09:43:56 +0200102 int num_temporal_layers = default_num_temporal_layers;
Erik Språngbfe3d852018-05-15 09:54:14 +0200103 if (sscanf(group_name.c_str(), "%d", &num_temporal_layers) == 1 &&
104 num_temporal_layers > 0 &&
105 num_temporal_layers <= webrtc::kMaxTemporalStreams) {
106 return num_temporal_layers;
107 }
108
109 RTC_LOG(LS_WARNING) << "Attempt to set number of temporal layers to "
110 "incorrect value: "
111 << group_name;
112
Erik Språng7461eff2018-09-10 09:43:56 +0200113 return default_num_temporal_layers;
Erik Språngbfe3d852018-05-15 09:54:14 +0200114}
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000115
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000116int FindSimulcastFormatIndex(int width, int height) {
Seth Hampson438663e2018-01-09 11:14:14 -0800117 RTC_DCHECK_GE(width, 0);
118 RTC_DCHECK_GE(height, 0);
kjellandera96e2d72016-02-04 23:52:28 -0800119 for (uint32_t i = 0; i < arraysize(kSimulcastFormats); ++i) {
sprang429600d2017-01-26 06:12:26 -0800120 if (width * height >=
121 kSimulcastFormats[i].width * kSimulcastFormats[i].height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000122 return i;
123 }
124 }
Seth Hampson438663e2018-01-09 11:14:14 -0800125 RTC_NOTREACHED();
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000126 return -1;
127}
128
129int FindSimulcastFormatIndex(int width, int height, size_t max_layers) {
Seth Hampson438663e2018-01-09 11:14:14 -0800130 RTC_DCHECK_GE(width, 0);
131 RTC_DCHECK_GE(height, 0);
132 RTC_DCHECK_GT(max_layers, 0);
kjellandera96e2d72016-02-04 23:52:28 -0800133 for (uint32_t i = 0; i < arraysize(kSimulcastFormats); ++i) {
sprang429600d2017-01-26 06:12:26 -0800134 if (width * height >=
135 kSimulcastFormats[i].width * kSimulcastFormats[i].height &&
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000136 max_layers == kSimulcastFormats[i].max_layers) {
137 return i;
138 }
139 }
Seth Hampson438663e2018-01-09 11:14:14 -0800140 RTC_NOTREACHED();
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000141 return -1;
142}
143
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000144// Simulcast stream width and height must both be dividable by
Rasmus Brandtefbdcb02018-04-26 17:47:42 +0200145// |2 ^ (simulcast_layers - 1)|.
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000146int NormalizeSimulcastSize(int size, size_t simulcast_layers) {
Åsa Persson1a35fbd2018-10-12 17:36:57 +0200147 int base2_exponent = static_cast<int>(simulcast_layers) - 1;
148 const absl::optional<int> experimental_base2_exponent =
149 webrtc::NormalizeSimulcastSizeExperiment::GetBase2Exponent();
150 if (experimental_base2_exponent &&
151 (size > (1 << *experimental_base2_exponent))) {
152 base2_exponent = *experimental_base2_exponent;
153 }
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000154 return ((size >> base2_exponent) << base2_exponent);
155}
156
157size_t FindSimulcastMaxLayers(int width, int height) {
158 int index = FindSimulcastFormatIndex(width, height);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000159 return kSimulcastFormats[index].max_layers;
160}
161
sprang429600d2017-01-26 06:12:26 -0800162int FindSimulcastMaxBitrateBps(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].max_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000165}
166
sprang429600d2017-01-26 06:12:26 -0800167int FindSimulcastTargetBitrateBps(int width, int height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000168 const int format_index = FindSimulcastFormatIndex(width, height);
pbosbe16f792015-10-16 12:49:39 -0700169 return kSimulcastFormats[format_index].target_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000170}
171
sprang429600d2017-01-26 06:12:26 -0800172int FindSimulcastMinBitrateBps(int width, int height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000173 const int format_index = FindSimulcastFormatIndex(width, height);
pbosbe16f792015-10-16 12:49:39 -0700174 return kSimulcastFormats[format_index].min_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000175}
176
Seth Hampson438663e2018-01-09 11:14:14 -0800177void SlotSimulcastMaxResolution(size_t max_layers, int* width, int* height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000178 int index = FindSimulcastFormatIndex(*width, *height, max_layers);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000179 *width = kSimulcastFormats[index].width;
180 *height = kSimulcastFormats[index].height;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100181 RTC_LOG(LS_INFO) << "SlotSimulcastMaxResolution to width:" << *width
182 << " height:" << *height;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000183}
184
Seth Hampson1370e302018-02-07 08:50:36 -0800185void BoostMaxSimulcastLayer(int max_bitrate_bps,
186 std::vector<webrtc::VideoStream>* layers) {
Åsa Persson55659812018-06-18 17:51:32 +0200187 if (layers->empty())
188 return;
189
Seth Hampson1370e302018-02-07 08:50:36 -0800190 // Spend additional bits to boost the max layer.
191 int bitrate_left_bps = max_bitrate_bps - GetTotalMaxBitrateBps(*layers);
192 if (bitrate_left_bps > 0) {
193 layers->back().max_bitrate_bps += bitrate_left_bps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000194 }
Seth Hampson1370e302018-02-07 08:50:36 -0800195}
196
197int GetTotalMaxBitrateBps(const std::vector<webrtc::VideoStream>& layers) {
Åsa Persson55659812018-06-18 17:51:32 +0200198 if (layers.empty())
199 return 0;
200
Seth Hampson1370e302018-02-07 08:50:36 -0800201 int total_max_bitrate_bps = 0;
202 for (size_t s = 0; s < layers.size() - 1; ++s) {
203 total_max_bitrate_bps += layers[s].target_bitrate_bps;
204 }
205 total_max_bitrate_bps += layers.back().max_bitrate_bps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000206 return total_max_bitrate_bps;
207}
208
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200209std::vector<webrtc::VideoStream> GetSimulcastConfig(
210 size_t max_layers,
211 int width,
212 int height,
213 int /*max_bitrate_bps*/,
214 double bitrate_priority,
215 int max_qp,
Åsa Persson8c1bf952018-09-13 10:42:19 +0200216 int /*max_framerate*/,
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200217 bool is_screenshare,
218 bool temporal_layers_supported) {
Seth Hampson1370e302018-02-07 08:50:36 -0800219 if (is_screenshare) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200220 return GetScreenshareLayers(max_layers, width, height, bitrate_priority,
221 max_qp, ScreenshareSimulcastFieldTrialEnabled(),
222 temporal_layers_supported);
sprang429600d2017-01-26 06:12:26 -0800223 } else {
Åsa Persson55659812018-06-18 17:51:32 +0200224 return GetNormalSimulcastLayers(max_layers, width, height, bitrate_priority,
Åsa Persson8c1bf952018-09-13 10:42:19 +0200225 max_qp, temporal_layers_supported);
sprang429600d2017-01-26 06:12:26 -0800226 }
Seth Hampson1370e302018-02-07 08:50:36 -0800227}
sprang429600d2017-01-26 06:12:26 -0800228
Seth Hampson1370e302018-02-07 08:50:36 -0800229std::vector<webrtc::VideoStream> GetNormalSimulcastLayers(
230 size_t max_layers,
231 int width,
232 int height,
Seth Hampson1370e302018-02-07 08:50:36 -0800233 double bitrate_priority,
234 int max_qp,
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200235 bool temporal_layers_supported) {
Seth Hampson1370e302018-02-07 08:50:36 -0800236 // TODO(bugs.webrtc.org/8785): Currently if the resolution isn't large enough
237 // (defined in kSimulcastFormats) we scale down the number of simulcast
238 // layers. Consider changing this so that the application can have more
239 // control over exactly how many simulcast layers are used.
240 size_t num_simulcast_layers = FindSimulcastMaxLayers(width, height);
Åsa Persson645512b2018-09-14 16:42:58 +0200241 if (webrtc::field_trial::IsEnabled("WebRTC-SimulcastMaxLayers")) {
242 num_simulcast_layers = max_layers;
243 }
Seth Hampson1370e302018-02-07 08:50:36 -0800244 if (num_simulcast_layers > max_layers) {
245 // TODO(bugs.webrtc.org/8486): This scales down the resolution if the
246 // number of simulcast layers created by the application isn't sufficient
247 // (defined in kSimulcastFormats). For example if the input frame's
248 // resolution is HD, but there are only 2 simulcast layers, the
249 // resolution gets scaled down to VGA. Consider taking this logic out to
250 // allow the application more control over the resolutions.
251 SlotSimulcastMaxResolution(max_layers, &width, &height);
252 num_simulcast_layers = max_layers;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000253 }
Seth Hampson1370e302018-02-07 08:50:36 -0800254 std::vector<webrtc::VideoStream> layers(num_simulcast_layers);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000255
Seth Hampson1370e302018-02-07 08:50:36 -0800256 // Format width and height has to be divisible by |2 ^ num_simulcast_layers -
257 // 1|.
258 width = NormalizeSimulcastSize(width, num_simulcast_layers);
259 height = NormalizeSimulcastSize(height, num_simulcast_layers);
260 // Add simulcast streams, from highest resolution (|s| = num_simulcast_layers
261 // -1) to lowest resolution at |s| = 0.
262 for (size_t s = num_simulcast_layers - 1;; --s) {
263 layers[s].width = width;
264 layers[s].height = height;
265 // TODO(pbos): Fill actual temporal-layer bitrate thresholds.
266 layers[s].max_qp = max_qp;
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200267 layers[s].num_temporal_layers =
Erik Språng58b22842018-08-21 13:15:28 +0200268 temporal_layers_supported ? DefaultNumberOfTemporalLayers(s, false) : 0;
Seth Hampson1370e302018-02-07 08:50:36 -0800269 layers[s].max_bitrate_bps = FindSimulcastMaxBitrateBps(width, height);
270 layers[s].target_bitrate_bps = FindSimulcastTargetBitrateBps(width, height);
Erik Språng58b22842018-08-21 13:15:28 +0200271 int num_temporal_layers = DefaultNumberOfTemporalLayers(s, false);
Erik Språngd497e6b2018-07-06 17:17:10 +0200272 if (s == 0) {
Rasmus Brandt73d117f2018-10-02 11:12:52 +0200273 // If alternative temporal rate allocation is selected, adjust the
Erik Språngd92288f2018-07-04 10:07:40 +0200274 // bitrate of the lowest simulcast stream so that absolute bitrate for
275 // the base temporal layer matches the bitrate for the base temporal
276 // layer with the default 3 simulcast streams. Otherwise we risk a
277 // higher threshold for receiving a feed at all.
Erik Språngd497e6b2018-07-06 17:17:10 +0200278 float rate_factor = 1.0;
279 if (num_temporal_layers == 3) {
Rasmus Brandt73d117f2018-10-02 11:12:52 +0200280 if (webrtc::field_trial::IsEnabled(
281 kUseBaseHeavyVP8TL3RateAllocationFieldTrial)) {
282 // Base heavy allocation increases TL0 bitrate from 40% to 60%.
Erik Språngd497e6b2018-07-06 17:17:10 +0200283 rate_factor = 0.4 / 0.6;
284 }
285 } else {
286 rate_factor =
287 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(3, 0) /
288 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
289 num_temporal_layers, 0);
290 }
291
Erik Språng79478ad2018-05-18 09:39:55 +0200292 layers[s].max_bitrate_bps =
293 static_cast<int>(layers[s].max_bitrate_bps * rate_factor);
294 layers[s].target_bitrate_bps =
295 static_cast<int>(layers[s].target_bitrate_bps * rate_factor);
296 }
Seth Hampson1370e302018-02-07 08:50:36 -0800297 layers[s].min_bitrate_bps = FindSimulcastMinBitrateBps(width, height);
Åsa Persson8c1bf952018-09-13 10:42:19 +0200298 layers[s].max_framerate = kDefaultVideoMaxFramerate;
sprang02569ad2017-07-06 05:05:50 -0700299
Seth Hampson1370e302018-02-07 08:50:36 -0800300 width /= 2;
301 height /= 2;
sprang02569ad2017-07-06 05:05:50 -0700302
Seth Hampson1370e302018-02-07 08:50:36 -0800303 if (s == 0) {
304 break;
sprang02569ad2017-07-06 05:05:50 -0700305 }
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000306 }
Seth Hampson1370e302018-02-07 08:50:36 -0800307 // Currently the relative bitrate priority of the sender is controlled by
308 // the value of the lowest VideoStream.
309 // TODO(bugs.webrtc.org/8630): The web specification describes being able to
310 // control relative bitrate for each individual simulcast layer, but this
311 // is currently just implemented per rtp sender.
312 layers[0].bitrate_priority = bitrate_priority;
313 return layers;
314}
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000315
Seth Hampson1370e302018-02-07 08:50:36 -0800316std::vector<webrtc::VideoStream> GetScreenshareLayers(
317 size_t max_layers,
318 int width,
319 int height,
Seth Hampson1370e302018-02-07 08:50:36 -0800320 double bitrate_priority,
321 int max_qp,
Ilya Nikolaevskiy3df1d5d2018-08-22 09:26:51 +0200322 bool screenshare_simulcast_enabled,
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200323 bool temporal_layers_supported) {
Ilya Nikolaevskiy3df1d5d2018-08-22 09:26:51 +0200324 auto max_screenshare_layers =
325 screenshare_simulcast_enabled ? kMaxScreenshareSimulcastLayers : 1;
Seth Hampson1370e302018-02-07 08:50:36 -0800326 size_t num_simulcast_layers =
Ilya Nikolaevskiy3df1d5d2018-08-22 09:26:51 +0200327 std::min<int>(max_layers, max_screenshare_layers);
Seth Hampson1370e302018-02-07 08:50:36 -0800328
329 std::vector<webrtc::VideoStream> layers(num_simulcast_layers);
Seth Hampson1370e302018-02-07 08:50:36 -0800330 // For legacy screenshare in conference mode, tl0 and tl1 bitrates are
331 // piggybacked on the VideoCodec struct as target and max bitrates,
Erik Språngcc681cc2018-03-14 17:52:55 +0100332 // respectively. See eg. webrtc::LibvpxVp8Encoder::SetRates().
Seth Hampson1370e302018-02-07 08:50:36 -0800333 layers[0].width = width;
334 layers[0].height = height;
335 layers[0].max_qp = max_qp;
336 layers[0].max_framerate = 5;
337 layers[0].min_bitrate_bps = kMinVideoBitrateBps;
Rasmus Brandt195d1d72018-05-09 11:28:01 +0200338 layers[0].target_bitrate_bps = kScreenshareDefaultTl0BitrateKbps * 1000;
339 layers[0].max_bitrate_bps = kScreenshareDefaultTl1BitrateKbps * 1000;
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200340 layers[0].num_temporal_layers = temporal_layers_supported ? 2 : 0;
Seth Hampson1370e302018-02-07 08:50:36 -0800341
342 // With simulcast enabled, add another spatial layer. This one will have a
343 // more normal layout, with the regular 3 temporal layer pattern and no fps
344 // restrictions. The base simulcast layer will still use legacy setup.
345 if (num_simulcast_layers == kMaxScreenshareSimulcastLayers) {
346 // Add optional upper simulcast layer.
Erik Språng58b22842018-08-21 13:15:28 +0200347 const int num_temporal_layers = DefaultNumberOfTemporalLayers(1, true);
Erik Språngb6b1cac2018-08-09 16:12:54 +0200348 int max_bitrate_bps;
Erik Språngcf919422018-09-17 16:18:57 +0200349 bool using_boosted_bitrate = false;
Erik Språngb6b1cac2018-08-09 16:12:54 +0200350 if (!temporal_layers_supported) {
351 // Set the max bitrate to where the base layer would have been if temporal
Erik Språng58b22842018-08-21 13:15:28 +0200352 // layers were enabled.
Erik Språngb6b1cac2018-08-09 16:12:54 +0200353 max_bitrate_bps = static_cast<int>(
354 kScreenshareHighStreamMaxBitrateBps *
355 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
356 num_temporal_layers, 0));
Erik Språng58b22842018-08-21 13:15:28 +0200357 } else if (DefaultNumberOfTemporalLayers(1, true) != 3 ||
Rasmus Brandt73d117f2018-10-02 11:12:52 +0200358 webrtc::field_trial::IsEnabled(
359 kUseBaseHeavyVP8TL3RateAllocationFieldTrial)) {
Erik Språngb6b1cac2018-08-09 16:12:54 +0200360 // Experimental temporal layer mode used, use increased max bitrate.
361 max_bitrate_bps = kScreenshareHighStreamMaxBitrateBps;
Erik Språngcf919422018-09-17 16:18:57 +0200362 using_boosted_bitrate = true;
Erik Språngb6b1cac2018-08-09 16:12:54 +0200363 } else {
364 // Keep current bitrates with default 3tl/8 frame settings.
365 // Lowest temporal layers of a 3 layer setup will have 40% of the total
366 // bitrate allocation for that simulcast layer. Make sure the gap between
367 // the target of the lower simulcast layer and first temporal layer of the
368 // higher one is at most 2x the bitrate, so that upswitching is not
369 // hampered by stalled bitrate estimates.
370 max_bitrate_bps = 2 * ((layers[0].target_bitrate_bps * 10) / 4);
371 }
372
Seth Hampson1370e302018-02-07 08:50:36 -0800373 layers[1].width = width;
374 layers[1].height = height;
375 layers[1].max_qp = max_qp;
Åsa Persson8c1bf952018-09-13 10:42:19 +0200376 layers[1].max_framerate = kDefaultVideoMaxFramerate;
Erik Språngb6b1cac2018-08-09 16:12:54 +0200377 layers[1].num_temporal_layers =
Erik Språng58b22842018-08-21 13:15:28 +0200378 temporal_layers_supported ? DefaultNumberOfTemporalLayers(1, true) : 0;
Erik Språngcf919422018-09-17 16:18:57 +0200379 layers[1].min_bitrate_bps = using_boosted_bitrate
380 ? kScreenshareHighStreamMinBitrateBps
381 : layers[0].target_bitrate_bps * 2;
382
383 // Cap max bitrate so it isn't overly high for the given resolution.
384 int resolution_limited_bitrate = std::max(
385 FindSimulcastMaxBitrateBps(width, height), layers[1].min_bitrate_bps);
386 max_bitrate_bps =
387 std::min<int>(max_bitrate_bps, resolution_limited_bitrate);
388
Seth Hampson1370e302018-02-07 08:50:36 -0800389 layers[1].target_bitrate_bps = max_bitrate_bps;
390 layers[1].max_bitrate_bps = max_bitrate_bps;
391 }
392
Seth Hampson24722b32017-12-22 09:36:42 -0800393 // The bitrate priority currently implemented on a per-sender level, so we
Seth Hampson1370e302018-02-07 08:50:36 -0800394 // just set it for the first simulcast layer.
395 layers[0].bitrate_priority = bitrate_priority;
396 return layers;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000397}
398
Ilya Nikolaevskiy3df1d5d2018-08-22 09:26:51 +0200399bool ScreenshareSimulcastFieldTrialEnabled() {
Erik Språng72e52ee2018-11-29 11:41:53 +0100400 return !webrtc::field_trial::IsDisabled(kSimulcastScreenshareFieldTrialName);
Ilya Nikolaevskiy3df1d5d2018-08-22 09:26:51 +0200401}
402
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000403} // namespace cricket