blob: 607d1412f99bcef8c59b81fd1ff46a43bd75ce80 [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"
Erik Språng79478ad2018-05-18 09:39:55 +020018#include "modules/video_coding/codecs/vp8/include/vp8_common_types.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/arraysize.h"
20#include "rtc_base/logging.h"
21#include "system_wrappers/include/field_trial.h"
tfarina5237aaf2015-11-10 23:44:30 -080022
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000023namespace cricket {
24
Rasmus Brandt195d1d72018-05-09 11:28:01 +020025namespace {
26
27constexpr int kScreenshareDefaultTl0BitrateKbps = 200;
28constexpr int kScreenshareDefaultTl1BitrateKbps = 1000;
29
30static const char* kSimulcastScreenshareFieldTrialName =
31 "WebRTC-SimulcastScreenshare";
32
33} // namespace
34
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000035struct SimulcastFormat {
36 int width;
37 int height;
38 // The maximum number of simulcast layers can be used for
39 // resolutions at |widthxheigh|.
40 size_t max_layers;
41 // The maximum bitrate for encoding stream at |widthxheight|, when we are
42 // not sending the next higher spatial stream.
pbosbe16f792015-10-16 12:49:39 -070043 int max_bitrate_kbps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000044 // The target bitrate for encoding stream at |widthxheight|, when this layer
45 // is not the highest layer (i.e., when we are sending another higher spatial
46 // stream).
pbosbe16f792015-10-16 12:49:39 -070047 int target_bitrate_kbps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000048 // The minimum bitrate needed for encoding stream at |widthxheight|.
pbosbe16f792015-10-16 12:49:39 -070049 int min_bitrate_kbps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000050};
51
52// These tables describe from which resolution we can use how many
53// simulcast layers at what bitrates (maximum, target, and minimum).
54// Important!! Keep this table from high resolution to low resolution.
55const SimulcastFormat kSimulcastFormats[] = {
Yves Gerey665174f2018-06-19 15:03:05 +020056 {1920, 1080, 3, 5000, 4000, 800}, {1280, 720, 3, 2500, 2500, 600},
57 {960, 540, 3, 900, 900, 450}, {640, 360, 2, 700, 500, 150},
58 {480, 270, 2, 450, 350, 150}, {320, 180, 1, 200, 150, 30},
59 {0, 0, 1, 200, 150, 30}};
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000060
Seth Hampson1370e302018-02-07 08:50:36 -080061const int kMaxScreenshareSimulcastLayers = 2;
sprang429600d2017-01-26 06:12:26 -080062
Erik Språngbfe3d852018-05-15 09:54:14 +020063// Multiway: Number of temporal layers for each simulcast stream.
64int DefaultNumberOfTemporalLayers(int simulcast_id) {
65 RTC_CHECK_GE(simulcast_id, 0);
66 RTC_CHECK_LT(simulcast_id, webrtc::kMaxSimulcastStreams);
67
68 const int kDefaultNumTemporalLayers = 3;
69
70 const std::string group_name =
71 webrtc::field_trial::FindFullName("WebRTC-VP8ConferenceTemporalLayers");
72 if (group_name.empty())
73 return kDefaultNumTemporalLayers;
74
75 int num_temporal_layers = kDefaultNumTemporalLayers;
76 if (sscanf(group_name.c_str(), "%d", &num_temporal_layers) == 1 &&
77 num_temporal_layers > 0 &&
78 num_temporal_layers <= webrtc::kMaxTemporalStreams) {
79 return num_temporal_layers;
80 }
81
82 RTC_LOG(LS_WARNING) << "Attempt to set number of temporal layers to "
83 "incorrect value: "
84 << group_name;
85
86 return kDefaultNumTemporalLayers;
87}
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000088
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000089int FindSimulcastFormatIndex(int width, int height) {
Seth Hampson438663e2018-01-09 11:14:14 -080090 RTC_DCHECK_GE(width, 0);
91 RTC_DCHECK_GE(height, 0);
kjellandera96e2d72016-02-04 23:52:28 -080092 for (uint32_t i = 0; i < arraysize(kSimulcastFormats); ++i) {
sprang429600d2017-01-26 06:12:26 -080093 if (width * height >=
94 kSimulcastFormats[i].width * kSimulcastFormats[i].height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000095 return i;
96 }
97 }
Seth Hampson438663e2018-01-09 11:14:14 -080098 RTC_NOTREACHED();
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000099 return -1;
100}
101
102int FindSimulcastFormatIndex(int width, int height, size_t max_layers) {
Seth Hampson438663e2018-01-09 11:14:14 -0800103 RTC_DCHECK_GE(width, 0);
104 RTC_DCHECK_GE(height, 0);
105 RTC_DCHECK_GT(max_layers, 0);
kjellandera96e2d72016-02-04 23:52:28 -0800106 for (uint32_t i = 0; i < arraysize(kSimulcastFormats); ++i) {
sprang429600d2017-01-26 06:12:26 -0800107 if (width * height >=
108 kSimulcastFormats[i].width * kSimulcastFormats[i].height &&
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000109 max_layers == kSimulcastFormats[i].max_layers) {
110 return i;
111 }
112 }
Seth Hampson438663e2018-01-09 11:14:14 -0800113 RTC_NOTREACHED();
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000114 return -1;
115}
116
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000117// Simulcast stream width and height must both be dividable by
Rasmus Brandtefbdcb02018-04-26 17:47:42 +0200118// |2 ^ (simulcast_layers - 1)|.
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000119int NormalizeSimulcastSize(int size, size_t simulcast_layers) {
120 const int base2_exponent = static_cast<int>(simulcast_layers) - 1;
121 return ((size >> base2_exponent) << base2_exponent);
122}
123
124size_t FindSimulcastMaxLayers(int width, int height) {
125 int index = FindSimulcastFormatIndex(width, height);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000126 return kSimulcastFormats[index].max_layers;
127}
128
sprang429600d2017-01-26 06:12:26 -0800129int FindSimulcastMaxBitrateBps(int width, int height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000130 const int format_index = FindSimulcastFormatIndex(width, height);
pbosbe16f792015-10-16 12:49:39 -0700131 return kSimulcastFormats[format_index].max_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000132}
133
sprang429600d2017-01-26 06:12:26 -0800134int FindSimulcastTargetBitrateBps(int width, int height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000135 const int format_index = FindSimulcastFormatIndex(width, height);
pbosbe16f792015-10-16 12:49:39 -0700136 return kSimulcastFormats[format_index].target_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000137}
138
sprang429600d2017-01-26 06:12:26 -0800139int FindSimulcastMinBitrateBps(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].min_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000142}
143
Seth Hampson438663e2018-01-09 11:14:14 -0800144void SlotSimulcastMaxResolution(size_t max_layers, int* width, int* height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000145 int index = FindSimulcastFormatIndex(*width, *height, max_layers);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000146 *width = kSimulcastFormats[index].width;
147 *height = kSimulcastFormats[index].height;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100148 RTC_LOG(LS_INFO) << "SlotSimulcastMaxResolution to width:" << *width
149 << " height:" << *height;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000150}
151
Seth Hampson1370e302018-02-07 08:50:36 -0800152void BoostMaxSimulcastLayer(int max_bitrate_bps,
153 std::vector<webrtc::VideoStream>* layers) {
Åsa Persson55659812018-06-18 17:51:32 +0200154 if (layers->empty())
155 return;
156
Seth Hampson1370e302018-02-07 08:50:36 -0800157 // Spend additional bits to boost the max layer.
158 int bitrate_left_bps = max_bitrate_bps - GetTotalMaxBitrateBps(*layers);
159 if (bitrate_left_bps > 0) {
160 layers->back().max_bitrate_bps += bitrate_left_bps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000161 }
Seth Hampson1370e302018-02-07 08:50:36 -0800162}
163
164int GetTotalMaxBitrateBps(const std::vector<webrtc::VideoStream>& layers) {
Åsa Persson55659812018-06-18 17:51:32 +0200165 if (layers.empty())
166 return 0;
167
Seth Hampson1370e302018-02-07 08:50:36 -0800168 int total_max_bitrate_bps = 0;
169 for (size_t s = 0; s < layers.size() - 1; ++s) {
170 total_max_bitrate_bps += layers[s].target_bitrate_bps;
171 }
172 total_max_bitrate_bps += layers.back().max_bitrate_bps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000173 return total_max_bitrate_bps;
174}
175
Seth Hampson1370e302018-02-07 08:50:36 -0800176std::vector<webrtc::VideoStream> GetSimulcastConfig(size_t max_layers,
Seth Hampson24722b32017-12-22 09:36:42 -0800177 int width,
178 int height,
Åsa Persson55659812018-06-18 17:51:32 +0200179 int /*max_bitrate_bps*/,
Seth Hampson24722b32017-12-22 09:36:42 -0800180 double bitrate_priority,
181 int max_qp,
182 int max_framerate,
Seth Hampson1370e302018-02-07 08:50:36 -0800183 bool is_screenshare) {
184 if (is_screenshare) {
Åsa Persson55659812018-06-18 17:51:32 +0200185 return GetScreenshareLayers(max_layers, width, height, bitrate_priority,
186 max_qp, max_framerate,
Seth Hampson1370e302018-02-07 08:50:36 -0800187 ScreenshareSimulcastFieldTrialEnabled());
sprang429600d2017-01-26 06:12:26 -0800188 } else {
Åsa Persson55659812018-06-18 17:51:32 +0200189 return GetNormalSimulcastLayers(max_layers, width, height, bitrate_priority,
190 max_qp, max_framerate);
sprang429600d2017-01-26 06:12:26 -0800191 }
Seth Hampson1370e302018-02-07 08:50:36 -0800192}
sprang429600d2017-01-26 06:12:26 -0800193
Seth Hampson1370e302018-02-07 08:50:36 -0800194std::vector<webrtc::VideoStream> GetNormalSimulcastLayers(
195 size_t max_layers,
196 int width,
197 int height,
Seth Hampson1370e302018-02-07 08:50:36 -0800198 double bitrate_priority,
199 int max_qp,
200 int max_framerate) {
201 // TODO(bugs.webrtc.org/8785): Currently if the resolution isn't large enough
202 // (defined in kSimulcastFormats) we scale down the number of simulcast
203 // layers. Consider changing this so that the application can have more
204 // control over exactly how many simulcast layers are used.
205 size_t num_simulcast_layers = FindSimulcastMaxLayers(width, height);
206 if (num_simulcast_layers > max_layers) {
207 // TODO(bugs.webrtc.org/8486): This scales down the resolution if the
208 // number of simulcast layers created by the application isn't sufficient
209 // (defined in kSimulcastFormats). For example if the input frame's
210 // resolution is HD, but there are only 2 simulcast layers, the
211 // resolution gets scaled down to VGA. Consider taking this logic out to
212 // allow the application more control over the resolutions.
213 SlotSimulcastMaxResolution(max_layers, &width, &height);
214 num_simulcast_layers = max_layers;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000215 }
Seth Hampson1370e302018-02-07 08:50:36 -0800216 std::vector<webrtc::VideoStream> layers(num_simulcast_layers);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000217
Seth Hampson1370e302018-02-07 08:50:36 -0800218 // Format width and height has to be divisible by |2 ^ num_simulcast_layers -
219 // 1|.
220 width = NormalizeSimulcastSize(width, num_simulcast_layers);
221 height = NormalizeSimulcastSize(height, num_simulcast_layers);
222 // Add simulcast streams, from highest resolution (|s| = num_simulcast_layers
223 // -1) to lowest resolution at |s| = 0.
224 for (size_t s = num_simulcast_layers - 1;; --s) {
225 layers[s].width = width;
226 layers[s].height = height;
227 // TODO(pbos): Fill actual temporal-layer bitrate thresholds.
228 layers[s].max_qp = max_qp;
Erik Språngbfe3d852018-05-15 09:54:14 +0200229 layers[s].num_temporal_layers = DefaultNumberOfTemporalLayers(s);
Seth Hampson1370e302018-02-07 08:50:36 -0800230 layers[s].max_bitrate_bps = FindSimulcastMaxBitrateBps(width, height);
231 layers[s].target_bitrate_bps = FindSimulcastTargetBitrateBps(width, height);
Erik Språng79478ad2018-05-18 09:39:55 +0200232 int num_temporal_layers = DefaultNumberOfTemporalLayers(s);
233 if (s == 0 && num_temporal_layers != 3) {
234 // If alternative number temporal layers is selected, adjust the
235 // bitrate of the lowest simulcast stream so that absolute bitrate for the
236 // base temporal layer matches the bitrate for the base temporal layer
237 // with the default 3 simulcast streams. Otherwise we risk a higher
238 // threshold for receiving a feed at all.
239 const float rate_factor =
240 webrtc::kVp8LayerRateAlloction[3][0] /
241 webrtc::kVp8LayerRateAlloction[num_temporal_layers][0];
242 layers[s].max_bitrate_bps =
243 static_cast<int>(layers[s].max_bitrate_bps * rate_factor);
244 layers[s].target_bitrate_bps =
245 static_cast<int>(layers[s].target_bitrate_bps * rate_factor);
246 }
Seth Hampson1370e302018-02-07 08:50:36 -0800247 layers[s].min_bitrate_bps = FindSimulcastMinBitrateBps(width, height);
248 layers[s].max_framerate = max_framerate;
sprang02569ad2017-07-06 05:05:50 -0700249
Seth Hampson1370e302018-02-07 08:50:36 -0800250 width /= 2;
251 height /= 2;
sprang02569ad2017-07-06 05:05:50 -0700252
Seth Hampson1370e302018-02-07 08:50:36 -0800253 if (s == 0) {
254 break;
sprang02569ad2017-07-06 05:05:50 -0700255 }
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000256 }
Seth Hampson1370e302018-02-07 08:50:36 -0800257 // Currently the relative bitrate priority of the sender is controlled by
258 // the value of the lowest VideoStream.
259 // TODO(bugs.webrtc.org/8630): The web specification describes being able to
260 // control relative bitrate for each individual simulcast layer, but this
261 // is currently just implemented per rtp sender.
262 layers[0].bitrate_priority = bitrate_priority;
263 return layers;
264}
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000265
Seth Hampson1370e302018-02-07 08:50:36 -0800266std::vector<webrtc::VideoStream> GetScreenshareLayers(
267 size_t max_layers,
268 int width,
269 int height,
Seth Hampson1370e302018-02-07 08:50:36 -0800270 double bitrate_priority,
271 int max_qp,
272 int max_framerate,
273 bool screenshare_simulcast_enabled) {
274 auto max_screenshare_layers =
275 screenshare_simulcast_enabled ? kMaxScreenshareSimulcastLayers : 1;
276 size_t num_simulcast_layers =
277 std::min<int>(max_layers, max_screenshare_layers);
278
279 std::vector<webrtc::VideoStream> layers(num_simulcast_layers);
Seth Hampson1370e302018-02-07 08:50:36 -0800280 // For legacy screenshare in conference mode, tl0 and tl1 bitrates are
281 // piggybacked on the VideoCodec struct as target and max bitrates,
Erik Språngcc681cc2018-03-14 17:52:55 +0100282 // respectively. See eg. webrtc::LibvpxVp8Encoder::SetRates().
Seth Hampson1370e302018-02-07 08:50:36 -0800283 layers[0].width = width;
284 layers[0].height = height;
285 layers[0].max_qp = max_qp;
286 layers[0].max_framerate = 5;
287 layers[0].min_bitrate_bps = kMinVideoBitrateBps;
Rasmus Brandt195d1d72018-05-09 11:28:01 +0200288 layers[0].target_bitrate_bps = kScreenshareDefaultTl0BitrateKbps * 1000;
289 layers[0].max_bitrate_bps = kScreenshareDefaultTl1BitrateKbps * 1000;
Sergey Silkina796a7e2018-03-01 15:11:29 +0100290 layers[0].num_temporal_layers = 2;
Seth Hampson1370e302018-02-07 08:50:36 -0800291
292 // With simulcast enabled, add another spatial layer. This one will have a
293 // more normal layout, with the regular 3 temporal layer pattern and no fps
294 // restrictions. The base simulcast layer will still use legacy setup.
295 if (num_simulcast_layers == kMaxScreenshareSimulcastLayers) {
296 // Add optional upper simulcast layer.
297 // Lowest temporal layers of a 3 layer setup will have 40% of the total
298 // bitrate allocation for that simulcast layer. Make sure the gap between
299 // the target of the lower simulcast layer and first temporal layer of the
300 // higher one is at most 2x the bitrate, so that upswitching is not hampered
301 // by stalled bitrate estimates.
302 int max_bitrate_bps = 2 * ((layers[0].target_bitrate_bps * 10) / 4);
303 // Cap max bitrate so it isn't overly high for the given resolution.
304 max_bitrate_bps = std::min<int>(max_bitrate_bps,
305 FindSimulcastMaxBitrateBps(width, height));
306
307 layers[1].width = width;
308 layers[1].height = height;
309 layers[1].max_qp = max_qp;
310 layers[1].max_framerate = max_framerate;
Sergey Silkinca091252018-03-14 17:00:50 +0100311 layers[1].num_temporal_layers = 3;
Seth Hampson1370e302018-02-07 08:50:36 -0800312 layers[1].min_bitrate_bps = layers[0].target_bitrate_bps * 2;
313 layers[1].target_bitrate_bps = max_bitrate_bps;
314 layers[1].max_bitrate_bps = max_bitrate_bps;
315 }
316
Seth Hampson24722b32017-12-22 09:36:42 -0800317 // The bitrate priority currently implemented on a per-sender level, so we
Seth Hampson1370e302018-02-07 08:50:36 -0800318 // just set it for the first simulcast layer.
319 layers[0].bitrate_priority = bitrate_priority;
320 return layers;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000321}
322
Seth Hampson1370e302018-02-07 08:50:36 -0800323bool ScreenshareSimulcastFieldTrialEnabled() {
sprangc1b57a12017-02-28 08:50:47 -0800324 return webrtc::field_trial::IsEnabled(kSimulcastScreenshareFieldTrialName);
sprang429600d2017-01-26 06:12:26 -0800325}
326
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000327} // namespace cricket