blob: 1809c32deef3f4f466e9021556727b0f89fada78 [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 Perssonced5cfd2018-08-10 16:16:43 +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
Erik Språngb6b1cac2018-08-09 16:12:54 +020028// Limits for legacy conference screensharing mode. Currently used for the
29// lower of the two simulcast streams.
Rasmus Brandt195d1d72018-05-09 11:28:01 +020030constexpr int kScreenshareDefaultTl0BitrateKbps = 200;
31constexpr int kScreenshareDefaultTl1BitrateKbps = 1000;
32
Erik Språngb6b1cac2018-08-09 16:12:54 +020033// Max bitrate for the higher one of the two simulcast stream used for screen
34// content.
35constexpr int kScreenshareHighStreamMaxBitrateBps = 1600000;
36
Rasmus Brandt195d1d72018-05-09 11:28:01 +020037} // namespace
38
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000039struct SimulcastFormat {
40 int width;
41 int height;
42 // The maximum number of simulcast layers can be used for
43 // resolutions at |widthxheigh|.
44 size_t max_layers;
45 // The maximum bitrate for encoding stream at |widthxheight|, when we are
46 // not sending the next higher spatial stream.
pbosbe16f792015-10-16 12:49:39 -070047 int max_bitrate_kbps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000048 // The target bitrate for encoding stream at |widthxheight|, when this layer
49 // is not the highest layer (i.e., when we are sending another higher spatial
50 // stream).
pbosbe16f792015-10-16 12:49:39 -070051 int target_bitrate_kbps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000052 // The minimum bitrate needed for encoding stream at |widthxheight|.
pbosbe16f792015-10-16 12:49:39 -070053 int min_bitrate_kbps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000054};
55
56// These tables describe from which resolution we can use how many
57// simulcast layers at what bitrates (maximum, target, and minimum).
58// Important!! Keep this table from high resolution to low resolution.
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +020059// clang-format off
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000060const SimulcastFormat kSimulcastFormats[] = {
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +020061 {1920, 1080, 3, 5000, 4000, 800},
62 {1280, 720, 3, 2500, 2500, 600},
63 {960, 540, 3, 900, 900, 450},
64 {640, 360, 2, 700, 500, 150},
65 {480, 270, 2, 450, 350, 150},
66 {320, 180, 1, 200, 150, 30},
67 {0, 0, 1, 200, 150, 30}
68};
69// clang-format on
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000070
Seth Hampson1370e302018-02-07 08:50:36 -080071const int kMaxScreenshareSimulcastLayers = 2;
sprang429600d2017-01-26 06:12:26 -080072
Erik Språngbfe3d852018-05-15 09:54:14 +020073// Multiway: Number of temporal layers for each simulcast stream.
74int DefaultNumberOfTemporalLayers(int simulcast_id) {
75 RTC_CHECK_GE(simulcast_id, 0);
76 RTC_CHECK_LT(simulcast_id, webrtc::kMaxSimulcastStreams);
77
78 const int kDefaultNumTemporalLayers = 3;
79
80 const std::string group_name =
81 webrtc::field_trial::FindFullName("WebRTC-VP8ConferenceTemporalLayers");
82 if (group_name.empty())
83 return kDefaultNumTemporalLayers;
84
85 int num_temporal_layers = kDefaultNumTemporalLayers;
86 if (sscanf(group_name.c_str(), "%d", &num_temporal_layers) == 1 &&
87 num_temporal_layers > 0 &&
88 num_temporal_layers <= webrtc::kMaxTemporalStreams) {
89 return num_temporal_layers;
90 }
91
92 RTC_LOG(LS_WARNING) << "Attempt to set number of temporal layers to "
93 "incorrect value: "
94 << group_name;
95
96 return kDefaultNumTemporalLayers;
97}
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000098
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000099int FindSimulcastFormatIndex(int width, int height) {
Seth Hampson438663e2018-01-09 11:14:14 -0800100 RTC_DCHECK_GE(width, 0);
101 RTC_DCHECK_GE(height, 0);
kjellandera96e2d72016-02-04 23:52:28 -0800102 for (uint32_t i = 0; i < arraysize(kSimulcastFormats); ++i) {
sprang429600d2017-01-26 06:12:26 -0800103 if (width * height >=
104 kSimulcastFormats[i].width * kSimulcastFormats[i].height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000105 return i;
106 }
107 }
Seth Hampson438663e2018-01-09 11:14:14 -0800108 RTC_NOTREACHED();
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000109 return -1;
110}
111
112int FindSimulcastFormatIndex(int width, int height, size_t max_layers) {
Seth Hampson438663e2018-01-09 11:14:14 -0800113 RTC_DCHECK_GE(width, 0);
114 RTC_DCHECK_GE(height, 0);
115 RTC_DCHECK_GT(max_layers, 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 max_layers == kSimulcastFormats[i].max_layers) {
120 return i;
121 }
122 }
Seth Hampson438663e2018-01-09 11:14:14 -0800123 RTC_NOTREACHED();
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000124 return -1;
125}
126
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000127// Simulcast stream width and height must both be dividable by
Rasmus Brandtefbdcb02018-04-26 17:47:42 +0200128// |2 ^ (simulcast_layers - 1)|.
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000129int NormalizeSimulcastSize(int size, size_t simulcast_layers) {
130 const int base2_exponent = static_cast<int>(simulcast_layers) - 1;
131 return ((size >> base2_exponent) << base2_exponent);
132}
133
134size_t FindSimulcastMaxLayers(int width, int height) {
135 int index = FindSimulcastFormatIndex(width, height);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000136 return kSimulcastFormats[index].max_layers;
137}
138
sprang429600d2017-01-26 06:12:26 -0800139int FindSimulcastMaxBitrateBps(int width, int height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000140 const int format_index = FindSimulcastFormatIndex(width, height);
pbosbe16f792015-10-16 12:49:39 -0700141 return kSimulcastFormats[format_index].max_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000142}
143
sprang429600d2017-01-26 06:12:26 -0800144int FindSimulcastTargetBitrateBps(int width, int height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000145 const int format_index = FindSimulcastFormatIndex(width, height);
pbosbe16f792015-10-16 12:49:39 -0700146 return kSimulcastFormats[format_index].target_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000147}
148
sprang429600d2017-01-26 06:12:26 -0800149int FindSimulcastMinBitrateBps(int width, int height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000150 const int format_index = FindSimulcastFormatIndex(width, height);
pbosbe16f792015-10-16 12:49:39 -0700151 return kSimulcastFormats[format_index].min_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000152}
153
Seth Hampson438663e2018-01-09 11:14:14 -0800154void SlotSimulcastMaxResolution(size_t max_layers, int* width, int* height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000155 int index = FindSimulcastFormatIndex(*width, *height, max_layers);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000156 *width = kSimulcastFormats[index].width;
157 *height = kSimulcastFormats[index].height;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100158 RTC_LOG(LS_INFO) << "SlotSimulcastMaxResolution to width:" << *width
159 << " height:" << *height;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000160}
161
Seth Hampson1370e302018-02-07 08:50:36 -0800162void BoostMaxSimulcastLayer(int max_bitrate_bps,
163 std::vector<webrtc::VideoStream>* layers) {
Åsa Persson55659812018-06-18 17:51:32 +0200164 if (layers->empty())
165 return;
166
Seth Hampson1370e302018-02-07 08:50:36 -0800167 // Spend additional bits to boost the max layer.
168 int bitrate_left_bps = max_bitrate_bps - GetTotalMaxBitrateBps(*layers);
169 if (bitrate_left_bps > 0) {
170 layers->back().max_bitrate_bps += bitrate_left_bps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000171 }
Seth Hampson1370e302018-02-07 08:50:36 -0800172}
173
174int GetTotalMaxBitrateBps(const std::vector<webrtc::VideoStream>& layers) {
Åsa Persson55659812018-06-18 17:51:32 +0200175 if (layers.empty())
176 return 0;
177
Seth Hampson1370e302018-02-07 08:50:36 -0800178 int total_max_bitrate_bps = 0;
179 for (size_t s = 0; s < layers.size() - 1; ++s) {
180 total_max_bitrate_bps += layers[s].target_bitrate_bps;
181 }
182 total_max_bitrate_bps += layers.back().max_bitrate_bps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000183 return total_max_bitrate_bps;
184}
185
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200186std::vector<webrtc::VideoStream> GetSimulcastConfig(
187 size_t max_layers,
188 int width,
189 int height,
190 int /*max_bitrate_bps*/,
191 double bitrate_priority,
192 int max_qp,
Åsa Perssonced5cfd2018-08-10 16:16:43 +0200193 int /*max_framerate*/,
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200194 bool is_screenshare,
195 bool temporal_layers_supported) {
Seth Hampson1370e302018-02-07 08:50:36 -0800196 if (is_screenshare) {
Åsa Persson55659812018-06-18 17:51:32 +0200197 return GetScreenshareLayers(max_layers, width, height, bitrate_priority,
Åsa Perssonced5cfd2018-08-10 16:16:43 +0200198 max_qp, temporal_layers_supported);
sprang429600d2017-01-26 06:12:26 -0800199 } else {
Åsa Persson55659812018-06-18 17:51:32 +0200200 return GetNormalSimulcastLayers(max_layers, width, height, bitrate_priority,
Åsa Perssonced5cfd2018-08-10 16:16:43 +0200201 max_qp, temporal_layers_supported);
sprang429600d2017-01-26 06:12:26 -0800202 }
Seth Hampson1370e302018-02-07 08:50:36 -0800203}
sprang429600d2017-01-26 06:12:26 -0800204
Seth Hampson1370e302018-02-07 08:50:36 -0800205std::vector<webrtc::VideoStream> GetNormalSimulcastLayers(
206 size_t max_layers,
207 int width,
208 int height,
Seth Hampson1370e302018-02-07 08:50:36 -0800209 double bitrate_priority,
210 int max_qp,
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200211 bool temporal_layers_supported) {
Seth Hampson1370e302018-02-07 08:50:36 -0800212 // TODO(bugs.webrtc.org/8785): Currently if the resolution isn't large enough
213 // (defined in kSimulcastFormats) we scale down the number of simulcast
214 // layers. Consider changing this so that the application can have more
215 // control over exactly how many simulcast layers are used.
216 size_t num_simulcast_layers = FindSimulcastMaxLayers(width, height);
217 if (num_simulcast_layers > max_layers) {
218 // TODO(bugs.webrtc.org/8486): This scales down the resolution if the
219 // number of simulcast layers created by the application isn't sufficient
220 // (defined in kSimulcastFormats). For example if the input frame's
221 // resolution is HD, but there are only 2 simulcast layers, the
222 // resolution gets scaled down to VGA. Consider taking this logic out to
223 // allow the application more control over the resolutions.
224 SlotSimulcastMaxResolution(max_layers, &width, &height);
225 num_simulcast_layers = max_layers;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000226 }
Seth Hampson1370e302018-02-07 08:50:36 -0800227 std::vector<webrtc::VideoStream> layers(num_simulcast_layers);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000228
Seth Hampson1370e302018-02-07 08:50:36 -0800229 // Format width and height has to be divisible by |2 ^ num_simulcast_layers -
230 // 1|.
231 width = NormalizeSimulcastSize(width, num_simulcast_layers);
232 height = NormalizeSimulcastSize(height, num_simulcast_layers);
233 // Add simulcast streams, from highest resolution (|s| = num_simulcast_layers
234 // -1) to lowest resolution at |s| = 0.
235 for (size_t s = num_simulcast_layers - 1;; --s) {
236 layers[s].width = width;
237 layers[s].height = height;
238 // TODO(pbos): Fill actual temporal-layer bitrate thresholds.
239 layers[s].max_qp = max_qp;
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200240 layers[s].num_temporal_layers =
241 temporal_layers_supported ? DefaultNumberOfTemporalLayers(s)
242 : 0;
Seth Hampson1370e302018-02-07 08:50:36 -0800243 layers[s].max_bitrate_bps = FindSimulcastMaxBitrateBps(width, height);
244 layers[s].target_bitrate_bps = FindSimulcastTargetBitrateBps(width, height);
Erik Språng79478ad2018-05-18 09:39:55 +0200245 int num_temporal_layers = DefaultNumberOfTemporalLayers(s);
Erik Språngd497e6b2018-07-06 17:17:10 +0200246 if (s == 0) {
Erik Språng79478ad2018-05-18 09:39:55 +0200247 // If alternative number temporal layers is selected, adjust the
Erik Språngd92288f2018-07-04 10:07:40 +0200248 // bitrate of the lowest simulcast stream so that absolute bitrate for
249 // the base temporal layer matches the bitrate for the base temporal
250 // layer with the default 3 simulcast streams. Otherwise we risk a
251 // higher threshold for receiving a feed at all.
Erik Språngd497e6b2018-07-06 17:17:10 +0200252 float rate_factor = 1.0;
253 if (num_temporal_layers == 3) {
254 if (webrtc::field_trial::IsEnabled("WebRTC-UseShortVP8TL3Pattern")) {
255 // Shortened pattern increases TL0 bitrate from 40% to 60%.
256 rate_factor = 0.4 / 0.6;
257 }
258 } else {
259 rate_factor =
260 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(3, 0) /
261 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
262 num_temporal_layers, 0);
263 }
264
Erik Språng79478ad2018-05-18 09:39:55 +0200265 layers[s].max_bitrate_bps =
266 static_cast<int>(layers[s].max_bitrate_bps * rate_factor);
267 layers[s].target_bitrate_bps =
268 static_cast<int>(layers[s].target_bitrate_bps * rate_factor);
269 }
Seth Hampson1370e302018-02-07 08:50:36 -0800270 layers[s].min_bitrate_bps = FindSimulcastMinBitrateBps(width, height);
Åsa Perssonced5cfd2018-08-10 16:16:43 +0200271 layers[s].max_framerate = kDefaultVideoMaxFramerate;
sprang02569ad2017-07-06 05:05:50 -0700272
Seth Hampson1370e302018-02-07 08:50:36 -0800273 width /= 2;
274 height /= 2;
sprang02569ad2017-07-06 05:05:50 -0700275
Seth Hampson1370e302018-02-07 08:50:36 -0800276 if (s == 0) {
277 break;
sprang02569ad2017-07-06 05:05:50 -0700278 }
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000279 }
Seth Hampson1370e302018-02-07 08:50:36 -0800280 // Currently the relative bitrate priority of the sender is controlled by
281 // the value of the lowest VideoStream.
282 // TODO(bugs.webrtc.org/8630): The web specification describes being able to
283 // control relative bitrate for each individual simulcast layer, but this
284 // is currently just implemented per rtp sender.
285 layers[0].bitrate_priority = bitrate_priority;
286 return layers;
287}
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000288
Seth Hampson1370e302018-02-07 08:50:36 -0800289std::vector<webrtc::VideoStream> GetScreenshareLayers(
290 size_t max_layers,
291 int width,
292 int height,
Seth Hampson1370e302018-02-07 08:50:36 -0800293 double bitrate_priority,
294 int max_qp,
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200295 bool temporal_layers_supported) {
Seth Hampson1370e302018-02-07 08:50:36 -0800296 size_t num_simulcast_layers =
Ilya Nikolaevskiya3df0f22018-07-24 17:02:07 +0200297 std::min<int>(max_layers, kMaxScreenshareSimulcastLayers);
Seth Hampson1370e302018-02-07 08:50:36 -0800298
299 std::vector<webrtc::VideoStream> layers(num_simulcast_layers);
Seth Hampson1370e302018-02-07 08:50:36 -0800300 // For legacy screenshare in conference mode, tl0 and tl1 bitrates are
301 // piggybacked on the VideoCodec struct as target and max bitrates,
Erik Språngcc681cc2018-03-14 17:52:55 +0100302 // respectively. See eg. webrtc::LibvpxVp8Encoder::SetRates().
Seth Hampson1370e302018-02-07 08:50:36 -0800303 layers[0].width = width;
304 layers[0].height = height;
305 layers[0].max_qp = max_qp;
306 layers[0].max_framerate = 5;
307 layers[0].min_bitrate_bps = kMinVideoBitrateBps;
Rasmus Brandt195d1d72018-05-09 11:28:01 +0200308 layers[0].target_bitrate_bps = kScreenshareDefaultTl0BitrateKbps * 1000;
309 layers[0].max_bitrate_bps = kScreenshareDefaultTl1BitrateKbps * 1000;
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200310 layers[0].num_temporal_layers = temporal_layers_supported ? 2 : 0;
Seth Hampson1370e302018-02-07 08:50:36 -0800311
312 // With simulcast enabled, add another spatial layer. This one will have a
313 // more normal layout, with the regular 3 temporal layer pattern and no fps
314 // restrictions. The base simulcast layer will still use legacy setup.
315 if (num_simulcast_layers == kMaxScreenshareSimulcastLayers) {
316 // Add optional upper simulcast layer.
Erik Språngb6b1cac2018-08-09 16:12:54 +0200317 const int num_temporal_layers = DefaultNumberOfTemporalLayers(1);
318 int max_bitrate_bps;
319 if (!temporal_layers_supported) {
320 // Set the max bitrate to where the base layer would have been if temporal
321 // layer were enabled.
322 max_bitrate_bps = static_cast<int>(
323 kScreenshareHighStreamMaxBitrateBps *
324 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
325 num_temporal_layers, 0));
326 } else if (DefaultNumberOfTemporalLayers(1) != 3 ||
327 webrtc::field_trial::IsEnabled("WebRTC-UseShortVP8TL3Pattern")) {
328 // Experimental temporal layer mode used, use increased max bitrate.
329 max_bitrate_bps = kScreenshareHighStreamMaxBitrateBps;
330 } else {
331 // Keep current bitrates with default 3tl/8 frame settings.
332 // Lowest temporal layers of a 3 layer setup will have 40% of the total
333 // bitrate allocation for that simulcast layer. Make sure the gap between
334 // the target of the lower simulcast layer and first temporal layer of the
335 // higher one is at most 2x the bitrate, so that upswitching is not
336 // hampered by stalled bitrate estimates.
337 max_bitrate_bps = 2 * ((layers[0].target_bitrate_bps * 10) / 4);
338 }
339
Seth Hampson1370e302018-02-07 08:50:36 -0800340 // Cap max bitrate so it isn't overly high for the given resolution.
341 max_bitrate_bps = std::min<int>(max_bitrate_bps,
342 FindSimulcastMaxBitrateBps(width, height));
Seth Hampson1370e302018-02-07 08:50:36 -0800343 layers[1].width = width;
344 layers[1].height = height;
345 layers[1].max_qp = max_qp;
Åsa Perssonced5cfd2018-08-10 16:16:43 +0200346 layers[1].max_framerate = kDefaultVideoMaxFramerate;
Erik Språngb6b1cac2018-08-09 16:12:54 +0200347 layers[1].num_temporal_layers =
348 temporal_layers_supported ? DefaultNumberOfTemporalLayers(1) : 0;
Seth Hampson1370e302018-02-07 08:50:36 -0800349 layers[1].min_bitrate_bps = layers[0].target_bitrate_bps * 2;
350 layers[1].target_bitrate_bps = max_bitrate_bps;
351 layers[1].max_bitrate_bps = max_bitrate_bps;
352 }
353
Seth Hampson24722b32017-12-22 09:36:42 -0800354 // The bitrate priority currently implemented on a per-sender level, so we
Seth Hampson1370e302018-02-07 08:50:36 -0800355 // just set it for the first simulcast layer.
356 layers[0].bitrate_priority = bitrate_priority;
357 return layers;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000358}
359
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000360} // namespace cricket