blob: 58ca9e8ecd965ab37faf9478f13ddc1fe862a45c [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "media/base/streamparams.h"
16#include "media/engine/constants.h"
17#include "media/engine/simulcast.h"
18#include "rtc_base/arraysize.h"
19#include "rtc_base/logging.h"
20#include "system_wrappers/include/field_trial.h"
tfarina5237aaf2015-11-10 23:44:30 -080021
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000022namespace cricket {
23
24struct SimulcastFormat {
25 int width;
26 int height;
27 // The maximum number of simulcast layers can be used for
28 // resolutions at |widthxheigh|.
29 size_t max_layers;
30 // The maximum bitrate for encoding stream at |widthxheight|, when we are
31 // not sending the next higher spatial stream.
pbosbe16f792015-10-16 12:49:39 -070032 int max_bitrate_kbps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000033 // The target bitrate for encoding stream at |widthxheight|, when this layer
34 // is not the highest layer (i.e., when we are sending another higher spatial
35 // stream).
pbosbe16f792015-10-16 12:49:39 -070036 int target_bitrate_kbps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000037 // The minimum bitrate needed for encoding stream at |widthxheight|.
pbosbe16f792015-10-16 12:49:39 -070038 int min_bitrate_kbps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000039};
40
41// These tables describe from which resolution we can use how many
42// simulcast layers at what bitrates (maximum, target, and minimum).
43// Important!! Keep this table from high resolution to low resolution.
44const SimulcastFormat kSimulcastFormats[] = {
pbosbe16f792015-10-16 12:49:39 -070045 {1920, 1080, 3, 5000, 4000, 800},
46 {1280, 720, 3, 2500, 2500, 600},
47 {960, 540, 3, 900, 900, 450},
48 {640, 360, 2, 700, 500, 150},
49 {480, 270, 2, 450, 350, 150},
50 {320, 180, 1, 200, 150, 30},
51 {0, 0, 1, 200, 150, 30}
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000052};
53
Seth Hampson1370e302018-02-07 08:50:36 -080054const int kMaxScreenshareSimulcastLayers = 2;
sprang429600d2017-01-26 06:12:26 -080055
Erik Språngbfe3d852018-05-15 09:54:14 +020056// Multiway: Number of temporal layers for each simulcast stream.
57int DefaultNumberOfTemporalLayers(int simulcast_id) {
58 RTC_CHECK_GE(simulcast_id, 0);
59 RTC_CHECK_LT(simulcast_id, webrtc::kMaxSimulcastStreams);
60
61 const int kDefaultNumTemporalLayers = 3;
62
63 const std::string group_name =
64 webrtc::field_trial::FindFullName("WebRTC-VP8ConferenceTemporalLayers");
65 if (group_name.empty())
66 return kDefaultNumTemporalLayers;
67
68 int num_temporal_layers = kDefaultNumTemporalLayers;
69 if (sscanf(group_name.c_str(), "%d", &num_temporal_layers) == 1 &&
70 num_temporal_layers > 0 &&
71 num_temporal_layers <= webrtc::kMaxTemporalStreams) {
72 return num_temporal_layers;
73 }
74
75 RTC_LOG(LS_WARNING) << "Attempt to set number of temporal layers to "
76 "incorrect value: "
77 << group_name;
78
79 return kDefaultNumTemporalLayers;
80}
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000081
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000082int FindSimulcastFormatIndex(int width, int height) {
Seth Hampson438663e2018-01-09 11:14:14 -080083 RTC_DCHECK_GE(width, 0);
84 RTC_DCHECK_GE(height, 0);
kjellandera96e2d72016-02-04 23:52:28 -080085 for (uint32_t i = 0; i < arraysize(kSimulcastFormats); ++i) {
sprang429600d2017-01-26 06:12:26 -080086 if (width * height >=
87 kSimulcastFormats[i].width * kSimulcastFormats[i].height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000088 return i;
89 }
90 }
Seth Hampson438663e2018-01-09 11:14:14 -080091 RTC_NOTREACHED();
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000092 return -1;
93}
94
95int FindSimulcastFormatIndex(int width, int height, size_t max_layers) {
Seth Hampson438663e2018-01-09 11:14:14 -080096 RTC_DCHECK_GE(width, 0);
97 RTC_DCHECK_GE(height, 0);
98 RTC_DCHECK_GT(max_layers, 0);
kjellandera96e2d72016-02-04 23:52:28 -080099 for (uint32_t i = 0; i < arraysize(kSimulcastFormats); ++i) {
sprang429600d2017-01-26 06:12:26 -0800100 if (width * height >=
101 kSimulcastFormats[i].width * kSimulcastFormats[i].height &&
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000102 max_layers == kSimulcastFormats[i].max_layers) {
103 return i;
104 }
105 }
Seth Hampson438663e2018-01-09 11:14:14 -0800106 RTC_NOTREACHED();
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000107 return -1;
108}
109
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000110// Simulcast stream width and height must both be dividable by
Rasmus Brandtefbdcb02018-04-26 17:47:42 +0200111// |2 ^ (simulcast_layers - 1)|.
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000112int NormalizeSimulcastSize(int size, size_t simulcast_layers) {
113 const int base2_exponent = static_cast<int>(simulcast_layers) - 1;
114 return ((size >> base2_exponent) << base2_exponent);
115}
116
117size_t FindSimulcastMaxLayers(int width, int height) {
118 int index = FindSimulcastFormatIndex(width, height);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000119 return kSimulcastFormats[index].max_layers;
120}
121
sprang429600d2017-01-26 06:12:26 -0800122int FindSimulcastMaxBitrateBps(int width, int height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000123 const int format_index = FindSimulcastFormatIndex(width, height);
pbosbe16f792015-10-16 12:49:39 -0700124 return kSimulcastFormats[format_index].max_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000125}
126
sprang429600d2017-01-26 06:12:26 -0800127int FindSimulcastTargetBitrateBps(int width, int height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000128 const int format_index = FindSimulcastFormatIndex(width, height);
pbosbe16f792015-10-16 12:49:39 -0700129 return kSimulcastFormats[format_index].target_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000130}
131
sprang429600d2017-01-26 06:12:26 -0800132int FindSimulcastMinBitrateBps(int width, int height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000133 const int format_index = FindSimulcastFormatIndex(width, height);
pbosbe16f792015-10-16 12:49:39 -0700134 return kSimulcastFormats[format_index].min_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000135}
136
Seth Hampson438663e2018-01-09 11:14:14 -0800137void SlotSimulcastMaxResolution(size_t max_layers, int* width, int* height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000138 int index = FindSimulcastFormatIndex(*width, *height, max_layers);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000139 *width = kSimulcastFormats[index].width;
140 *height = kSimulcastFormats[index].height;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100141 RTC_LOG(LS_INFO) << "SlotSimulcastMaxResolution to width:" << *width
142 << " height:" << *height;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000143}
144
Seth Hampson1370e302018-02-07 08:50:36 -0800145void BoostMaxSimulcastLayer(int max_bitrate_bps,
146 std::vector<webrtc::VideoStream>* layers) {
147 // Spend additional bits to boost the max layer.
148 int bitrate_left_bps = max_bitrate_bps - GetTotalMaxBitrateBps(*layers);
149 if (bitrate_left_bps > 0) {
150 layers->back().max_bitrate_bps += bitrate_left_bps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000151 }
Seth Hampson1370e302018-02-07 08:50:36 -0800152}
153
154int GetTotalMaxBitrateBps(const std::vector<webrtc::VideoStream>& layers) {
155 int total_max_bitrate_bps = 0;
156 for (size_t s = 0; s < layers.size() - 1; ++s) {
157 total_max_bitrate_bps += layers[s].target_bitrate_bps;
158 }
159 total_max_bitrate_bps += layers.back().max_bitrate_bps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000160 return total_max_bitrate_bps;
161}
162
Seth Hampson1370e302018-02-07 08:50:36 -0800163std::vector<webrtc::VideoStream> GetSimulcastConfig(size_t max_layers,
Seth Hampson24722b32017-12-22 09:36:42 -0800164 int width,
165 int height,
166 int max_bitrate_bps,
167 double bitrate_priority,
168 int max_qp,
169 int max_framerate,
Seth Hampson1370e302018-02-07 08:50:36 -0800170 bool is_screenshare) {
171 if (is_screenshare) {
172 return GetScreenshareLayers(max_layers, width, height, max_bitrate_bps,
173 bitrate_priority, max_qp, max_framerate,
174 ScreenshareSimulcastFieldTrialEnabled());
sprang429600d2017-01-26 06:12:26 -0800175 } else {
Seth Hampson1370e302018-02-07 08:50:36 -0800176 return GetNormalSimulcastLayers(max_layers, width, height, max_bitrate_bps,
177 bitrate_priority, max_qp, max_framerate);
sprang429600d2017-01-26 06:12:26 -0800178 }
Seth Hampson1370e302018-02-07 08:50:36 -0800179}
sprang429600d2017-01-26 06:12:26 -0800180
Seth Hampson1370e302018-02-07 08:50:36 -0800181std::vector<webrtc::VideoStream> GetNormalSimulcastLayers(
182 size_t max_layers,
183 int width,
184 int height,
185 int max_bitrate_bps,
186 double bitrate_priority,
187 int max_qp,
188 int max_framerate) {
189 // TODO(bugs.webrtc.org/8785): Currently if the resolution isn't large enough
190 // (defined in kSimulcastFormats) we scale down the number of simulcast
191 // layers. Consider changing this so that the application can have more
192 // control over exactly how many simulcast layers are used.
193 size_t num_simulcast_layers = FindSimulcastMaxLayers(width, height);
194 if (num_simulcast_layers > max_layers) {
195 // TODO(bugs.webrtc.org/8486): This scales down the resolution if the
196 // number of simulcast layers created by the application isn't sufficient
197 // (defined in kSimulcastFormats). For example if the input frame's
198 // resolution is HD, but there are only 2 simulcast layers, the
199 // resolution gets scaled down to VGA. Consider taking this logic out to
200 // allow the application more control over the resolutions.
201 SlotSimulcastMaxResolution(max_layers, &width, &height);
202 num_simulcast_layers = max_layers;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000203 }
Seth Hampson1370e302018-02-07 08:50:36 -0800204 std::vector<webrtc::VideoStream> layers(num_simulcast_layers);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000205
Seth Hampson1370e302018-02-07 08:50:36 -0800206 // Format width and height has to be divisible by |2 ^ num_simulcast_layers -
207 // 1|.
208 width = NormalizeSimulcastSize(width, num_simulcast_layers);
209 height = NormalizeSimulcastSize(height, num_simulcast_layers);
210 // Add simulcast streams, from highest resolution (|s| = num_simulcast_layers
211 // -1) to lowest resolution at |s| = 0.
212 for (size_t s = num_simulcast_layers - 1;; --s) {
213 layers[s].width = width;
214 layers[s].height = height;
215 // TODO(pbos): Fill actual temporal-layer bitrate thresholds.
216 layers[s].max_qp = max_qp;
Erik Språngbfe3d852018-05-15 09:54:14 +0200217 layers[s].num_temporal_layers = DefaultNumberOfTemporalLayers(s);
Seth Hampson1370e302018-02-07 08:50:36 -0800218 layers[s].max_bitrate_bps = FindSimulcastMaxBitrateBps(width, height);
219 layers[s].target_bitrate_bps = FindSimulcastTargetBitrateBps(width, height);
220 layers[s].min_bitrate_bps = FindSimulcastMinBitrateBps(width, height);
221 layers[s].max_framerate = max_framerate;
sprang02569ad2017-07-06 05:05:50 -0700222
Seth Hampson1370e302018-02-07 08:50:36 -0800223 width /= 2;
224 height /= 2;
sprang02569ad2017-07-06 05:05:50 -0700225
Seth Hampson1370e302018-02-07 08:50:36 -0800226 if (s == 0) {
227 break;
sprang02569ad2017-07-06 05:05:50 -0700228 }
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000229 }
Seth Hampson1370e302018-02-07 08:50:36 -0800230 // If there is bitrate leftover, give it to the largest layer.
231 BoostMaxSimulcastLayer(max_bitrate_bps, &layers);
232 // Currently the relative bitrate priority of the sender is controlled by
233 // the value of the lowest VideoStream.
234 // TODO(bugs.webrtc.org/8630): The web specification describes being able to
235 // control relative bitrate for each individual simulcast layer, but this
236 // is currently just implemented per rtp sender.
237 layers[0].bitrate_priority = bitrate_priority;
238 return layers;
239}
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000240
Seth Hampson1370e302018-02-07 08:50:36 -0800241std::vector<webrtc::VideoStream> GetScreenshareLayers(
242 size_t max_layers,
243 int width,
244 int height,
245 int max_bitrate_bps,
246 double bitrate_priority,
247 int max_qp,
248 int max_framerate,
249 bool screenshare_simulcast_enabled) {
250 auto max_screenshare_layers =
251 screenshare_simulcast_enabled ? kMaxScreenshareSimulcastLayers : 1;
252 size_t num_simulcast_layers =
253 std::min<int>(max_layers, max_screenshare_layers);
254
255 std::vector<webrtc::VideoStream> layers(num_simulcast_layers);
256 ScreenshareLayerConfig config = ScreenshareLayerConfig::GetDefault();
257 // For legacy screenshare in conference mode, tl0 and tl1 bitrates are
258 // piggybacked on the VideoCodec struct as target and max bitrates,
Erik Språngcc681cc2018-03-14 17:52:55 +0100259 // respectively. See eg. webrtc::LibvpxVp8Encoder::SetRates().
Seth Hampson1370e302018-02-07 08:50:36 -0800260 layers[0].width = width;
261 layers[0].height = height;
262 layers[0].max_qp = max_qp;
263 layers[0].max_framerate = 5;
264 layers[0].min_bitrate_bps = kMinVideoBitrateBps;
265 layers[0].target_bitrate_bps = config.tl0_bitrate_kbps * 1000;
266 layers[0].max_bitrate_bps = config.tl1_bitrate_kbps * 1000;
Sergey Silkina796a7e2018-03-01 15:11:29 +0100267 layers[0].num_temporal_layers = 2;
Seth Hampson1370e302018-02-07 08:50:36 -0800268
269 // With simulcast enabled, add another spatial layer. This one will have a
270 // more normal layout, with the regular 3 temporal layer pattern and no fps
271 // restrictions. The base simulcast layer will still use legacy setup.
272 if (num_simulcast_layers == kMaxScreenshareSimulcastLayers) {
273 // Add optional upper simulcast layer.
274 // Lowest temporal layers of a 3 layer setup will have 40% of the total
275 // bitrate allocation for that simulcast layer. Make sure the gap between
276 // the target of the lower simulcast layer and first temporal layer of the
277 // higher one is at most 2x the bitrate, so that upswitching is not hampered
278 // by stalled bitrate estimates.
279 int max_bitrate_bps = 2 * ((layers[0].target_bitrate_bps * 10) / 4);
280 // Cap max bitrate so it isn't overly high for the given resolution.
281 max_bitrate_bps = std::min<int>(max_bitrate_bps,
282 FindSimulcastMaxBitrateBps(width, height));
283
284 layers[1].width = width;
285 layers[1].height = height;
286 layers[1].max_qp = max_qp;
287 layers[1].max_framerate = max_framerate;
Sergey Silkinca091252018-03-14 17:00:50 +0100288 layers[1].num_temporal_layers = 3;
Seth Hampson1370e302018-02-07 08:50:36 -0800289 layers[1].min_bitrate_bps = layers[0].target_bitrate_bps * 2;
290 layers[1].target_bitrate_bps = max_bitrate_bps;
291 layers[1].max_bitrate_bps = max_bitrate_bps;
292 }
293
Seth Hampson24722b32017-12-22 09:36:42 -0800294 // The bitrate priority currently implemented on a per-sender level, so we
Seth Hampson1370e302018-02-07 08:50:36 -0800295 // just set it for the first simulcast layer.
296 layers[0].bitrate_priority = bitrate_priority;
297 return layers;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000298}
299
sprang@webrtc.org46d4d292014-12-23 15:19:35 +0000300static const int kScreenshareMinBitrateKbps = 50;
301static const int kScreenshareMaxBitrateKbps = 6000;
Erik Språng2c4c9142015-06-24 11:24:44 +0200302static const int kScreenshareDefaultTl0BitrateKbps = 200;
sprang@webrtc.org46d4d292014-12-23 15:19:35 +0000303static const int kScreenshareDefaultTl1BitrateKbps = 1000;
304
Seth Hampson1370e302018-02-07 08:50:36 -0800305static const char* kScreenshareLayerFieldTrialName =
sprang@webrtc.org46d4d292014-12-23 15:19:35 +0000306 "WebRTC-ScreenshareLayerRates";
sprang429600d2017-01-26 06:12:26 -0800307static const char* kSimulcastScreenshareFieldTrialName =
308 "WebRTC-SimulcastScreenshare";
sprang@webrtc.org46d4d292014-12-23 15:19:35 +0000309
310ScreenshareLayerConfig::ScreenshareLayerConfig(int tl0_bitrate, int tl1_bitrate)
311 : tl0_bitrate_kbps(tl0_bitrate), tl1_bitrate_kbps(tl1_bitrate) {
312}
313
314ScreenshareLayerConfig ScreenshareLayerConfig::GetDefault() {
315 std::string group =
Seth Hampson1370e302018-02-07 08:50:36 -0800316 webrtc::field_trial::FindFullName(kScreenshareLayerFieldTrialName);
sprang@webrtc.org46d4d292014-12-23 15:19:35 +0000317
318 ScreenshareLayerConfig config(kScreenshareDefaultTl0BitrateKbps,
319 kScreenshareDefaultTl1BitrateKbps);
320 if (!group.empty() && !FromFieldTrialGroup(group, &config)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100321 RTC_LOG(LS_WARNING) << "Unable to parse WebRTC-ScreenshareLayerRates"
322 " field trial group: '"
323 << group << "'.";
sprang@webrtc.org46d4d292014-12-23 15:19:35 +0000324 }
325 return config;
326}
327
328bool ScreenshareLayerConfig::FromFieldTrialGroup(
329 const std::string& group,
330 ScreenshareLayerConfig* config) {
331 // Parse field trial group name, containing bitrates for tl0 and tl1.
332 int tl0_bitrate;
333 int tl1_bitrate;
334 if (sscanf(group.c_str(), "%d-%d", &tl0_bitrate, &tl1_bitrate) != 2) {
335 return false;
336 }
337
338 // Sanity check.
339 if (tl0_bitrate < kScreenshareMinBitrateKbps ||
340 tl0_bitrate > kScreenshareMaxBitrateKbps ||
341 tl1_bitrate < kScreenshareMinBitrateKbps ||
342 tl1_bitrate > kScreenshareMaxBitrateKbps || tl0_bitrate > tl1_bitrate) {
343 return false;
344 }
345
346 config->tl0_bitrate_kbps = tl0_bitrate;
347 config->tl1_bitrate_kbps = tl1_bitrate;
348
349 return true;
350}
351
Seth Hampson1370e302018-02-07 08:50:36 -0800352bool ScreenshareSimulcastFieldTrialEnabled() {
sprangc1b57a12017-02-28 08:50:47 -0800353 return webrtc::field_trial::IsEnabled(kSimulcastScreenshareFieldTrialName);
sprang429600d2017-01-26 06:12:26 -0800354}
355
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000356} // namespace cricket