blob: f2909d3f4c61bad2f6a8275e4d663220992a09d4 [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
sprang02569ad2017-07-06 05:05:50 -070054const int kMaxScreenshareSimulcastStreams = 2;
sprang429600d2017-01-26 06:12:26 -080055
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000056// Multiway: Number of temporal layers for each simulcast stream, for maximum
57// possible number of simulcast streams |kMaxSimulcastStreams|. The array
58// goes from lowest resolution at position 0 to highest resolution.
59// For example, first three elements correspond to say: QVGA, VGA, WHD.
60static const int
61 kDefaultConferenceNumberOfTemporalLayers[webrtc::kMaxSimulcastStreams] =
62 {3, 3, 3, 3};
63
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000064int FindSimulcastFormatIndex(int width, int height) {
Seth Hampson438663e2018-01-09 11:14:14 -080065 RTC_DCHECK_GE(width, 0);
66 RTC_DCHECK_GE(height, 0);
kjellandera96e2d72016-02-04 23:52:28 -080067 for (uint32_t i = 0; i < arraysize(kSimulcastFormats); ++i) {
sprang429600d2017-01-26 06:12:26 -080068 if (width * height >=
69 kSimulcastFormats[i].width * kSimulcastFormats[i].height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000070 return i;
71 }
72 }
Seth Hampson438663e2018-01-09 11:14:14 -080073 RTC_NOTREACHED();
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000074 return -1;
75}
76
77int FindSimulcastFormatIndex(int width, int height, size_t max_layers) {
Seth Hampson438663e2018-01-09 11:14:14 -080078 RTC_DCHECK_GE(width, 0);
79 RTC_DCHECK_GE(height, 0);
80 RTC_DCHECK_GT(max_layers, 0);
kjellandera96e2d72016-02-04 23:52:28 -080081 for (uint32_t i = 0; i < arraysize(kSimulcastFormats); ++i) {
sprang429600d2017-01-26 06:12:26 -080082 if (width * height >=
83 kSimulcastFormats[i].width * kSimulcastFormats[i].height &&
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000084 max_layers == kSimulcastFormats[i].max_layers) {
85 return i;
86 }
87 }
Seth Hampson438663e2018-01-09 11:14:14 -080088 RTC_NOTREACHED();
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000089 return -1;
90}
91
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000092// Simulcast stream width and height must both be dividable by
93// |2 ^ simulcast_layers - 1|.
94int NormalizeSimulcastSize(int size, size_t simulcast_layers) {
95 const int base2_exponent = static_cast<int>(simulcast_layers) - 1;
96 return ((size >> base2_exponent) << base2_exponent);
97}
98
99size_t FindSimulcastMaxLayers(int width, int height) {
100 int index = FindSimulcastFormatIndex(width, height);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000101 return kSimulcastFormats[index].max_layers;
102}
103
sprang429600d2017-01-26 06:12:26 -0800104int FindSimulcastMaxBitrateBps(int width, int height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000105 const int format_index = FindSimulcastFormatIndex(width, height);
pbosbe16f792015-10-16 12:49:39 -0700106 return kSimulcastFormats[format_index].max_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000107}
108
sprang429600d2017-01-26 06:12:26 -0800109int FindSimulcastTargetBitrateBps(int width, int height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000110 const int format_index = FindSimulcastFormatIndex(width, height);
pbosbe16f792015-10-16 12:49:39 -0700111 return kSimulcastFormats[format_index].target_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000112}
113
sprang429600d2017-01-26 06:12:26 -0800114int FindSimulcastMinBitrateBps(int width, int height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000115 const int format_index = FindSimulcastFormatIndex(width, height);
pbosbe16f792015-10-16 12:49:39 -0700116 return kSimulcastFormats[format_index].min_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000117}
118
Seth Hampson438663e2018-01-09 11:14:14 -0800119void SlotSimulcastMaxResolution(size_t max_layers, int* width, int* height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000120 int index = FindSimulcastFormatIndex(*width, *height, max_layers);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000121 *width = kSimulcastFormats[index].width;
122 *height = kSimulcastFormats[index].height;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100123 RTC_LOG(LS_INFO) << "SlotSimulcastMaxResolution to width:" << *width
124 << " height:" << *height;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000125}
126
127int GetTotalMaxBitrateBps(const std::vector<webrtc::VideoStream>& streams) {
128 int total_max_bitrate_bps = 0;
129 for (size_t s = 0; s < streams.size() - 1; ++s) {
130 total_max_bitrate_bps += streams[s].target_bitrate_bps;
131 }
132 total_max_bitrate_bps += streams.back().max_bitrate_bps;
133 return total_max_bitrate_bps;
134}
135
Seth Hampson24722b32017-12-22 09:36:42 -0800136std::vector<webrtc::VideoStream> GetSimulcastConfig(size_t max_streams,
137 int width,
138 int height,
139 int max_bitrate_bps,
140 double bitrate_priority,
141 int max_qp,
142 int max_framerate,
143 bool is_screencast) {
sprang429600d2017-01-26 06:12:26 -0800144 size_t num_simulcast_layers;
145 if (is_screencast) {
sprang02569ad2017-07-06 05:05:50 -0700146 if (UseSimulcastScreenshare()) {
147 num_simulcast_layers =
148 std::min<int>(max_streams, kMaxScreenshareSimulcastStreams);
149 } else {
150 num_simulcast_layers = 1;
151 }
sprang429600d2017-01-26 06:12:26 -0800152 } else {
153 num_simulcast_layers = FindSimulcastMaxLayers(width, height);
154 }
155
156 if (num_simulcast_layers > max_streams) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000157 // If the number of SSRCs in the group differs from our target
158 // number of simulcast streams for current resolution, switch down
159 // to a resolution that matches our number of SSRCs.
Seth Hampson438663e2018-01-09 11:14:14 -0800160 SlotSimulcastMaxResolution(max_streams, &width, &height);
sprang429600d2017-01-26 06:12:26 -0800161 num_simulcast_layers = max_streams;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000162 }
163 std::vector<webrtc::VideoStream> streams;
sprang429600d2017-01-26 06:12:26 -0800164 streams.resize(num_simulcast_layers);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000165
sprang02569ad2017-07-06 05:05:50 -0700166 if (is_screencast) {
167 ScreenshareLayerConfig config = ScreenshareLayerConfig::GetDefault();
168 // For legacy screenshare in conference mode, tl0 and tl1 bitrates are
169 // piggybacked on the VideoCodec struct as target and max bitrates,
170 // respectively. See eg. webrtc::VP8EncoderImpl::SetRates().
171 streams[0].width = width;
172 streams[0].height = height;
173 streams[0].max_qp = max_qp;
174 streams[0].max_framerate = 5;
Åsa Persson45bbc8a2017-11-13 10:16:47 +0100175 streams[0].min_bitrate_bps = kMinVideoBitrateBps;
sprang02569ad2017-07-06 05:05:50 -0700176 streams[0].target_bitrate_bps = config.tl0_bitrate_kbps * 1000;
177 streams[0].max_bitrate_bps = config.tl1_bitrate_kbps * 1000;
178 streams[0].temporal_layer_thresholds_bps.clear();
179 streams[0].temporal_layer_thresholds_bps.push_back(config.tl0_bitrate_kbps *
180 1000);
181
182 // With simulcast enabled, add another spatial layer. This one will have a
183 // more normal layout, with the regular 3 temporal layer pattern and no fps
184 // restrictions. The base simulcast stream will still use legacy setup.
185 if (num_simulcast_layers == kMaxScreenshareSimulcastStreams) {
186 // Add optional upper simulcast layer.
187 // Lowest temporal layers of a 3 layer setup will have 40% of the total
188 // bitrate allocation for that stream. Make sure the gap between the
189 // target of the lower stream and first temporal layer of the higher one
190 // is at most 2x the bitrate, so that upswitching is not hampered by
191 // stalled bitrate estimates.
192 int max_bitrate_bps = 2 * ((streams[0].target_bitrate_bps * 10) / 4);
193 // Cap max bitrate so it isn't overly high for the given resolution.
194 max_bitrate_bps = std::min<int>(
195 max_bitrate_bps, FindSimulcastMaxBitrateBps(width, height));
196
197 streams[1].width = width;
198 streams[1].height = height;
199 streams[1].max_qp = max_qp;
200 streams[1].max_framerate = max_framerate;
201 // Three temporal layers means two thresholds.
202 streams[1].temporal_layer_thresholds_bps.resize(2);
203 streams[1].min_bitrate_bps = streams[0].target_bitrate_bps * 2;
204 streams[1].target_bitrate_bps = max_bitrate_bps;
205 streams[1].max_bitrate_bps = max_bitrate_bps;
206 }
207 } else {
sprang429600d2017-01-26 06:12:26 -0800208 // Format width and height has to be divisible by |2 ^ number_streams - 1|.
209 width = NormalizeSimulcastSize(width, num_simulcast_layers);
210 height = NormalizeSimulcastSize(height, num_simulcast_layers);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000211
sprang02569ad2017-07-06 05:05:50 -0700212 // Add simulcast sub-streams from lower resolution to higher resolutions.
213 // Add simulcast streams, from highest resolution (|s| = number_streams -1)
214 // to lowest resolution at |s| = 0.
215 for (size_t s = num_simulcast_layers - 1;; --s) {
216 streams[s].width = width;
217 streams[s].height = height;
218 // TODO(pbos): Fill actual temporal-layer bitrate thresholds.
219 streams[s].max_qp = max_qp;
sprang429600d2017-01-26 06:12:26 -0800220 streams[s].temporal_layer_thresholds_bps.resize(
221 kDefaultConferenceNumberOfTemporalLayers[s] - 1);
222 streams[s].max_bitrate_bps = FindSimulcastMaxBitrateBps(width, height);
223 streams[s].target_bitrate_bps =
224 FindSimulcastTargetBitrateBps(width, height);
225 streams[s].min_bitrate_bps = FindSimulcastMinBitrateBps(width, height);
226 streams[s].max_framerate = max_framerate;
sprang429600d2017-01-26 06:12:26 -0800227
sprang3ebabf12017-02-16 07:35:22 -0800228 width /= 2;
229 height /= 2;
sprang317005a2017-06-08 07:12:17 -0700230
sprang02569ad2017-07-06 05:05:50 -0700231 if (s == 0)
232 break;
233 }
234
235 // Spend additional bits to boost the max stream.
236 int bitrate_left_bps = max_bitrate_bps - GetTotalMaxBitrateBps(streams);
237 if (bitrate_left_bps > 0) {
238 streams.back().max_bitrate_bps += bitrate_left_bps;
239 }
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000240 }
241
Seth Hampson24722b32017-12-22 09:36:42 -0800242 // The bitrate priority currently implemented on a per-sender level, so we
243 // just set it for the first video stream.
244 streams[0].bitrate_priority = bitrate_priority;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000245 return streams;
246}
247
sprang@webrtc.org46d4d292014-12-23 15:19:35 +0000248static const int kScreenshareMinBitrateKbps = 50;
249static const int kScreenshareMaxBitrateKbps = 6000;
Erik Språng2c4c9142015-06-24 11:24:44 +0200250static const int kScreenshareDefaultTl0BitrateKbps = 200;
sprang@webrtc.org46d4d292014-12-23 15:19:35 +0000251static const int kScreenshareDefaultTl1BitrateKbps = 1000;
252
253static const char* kScreencastLayerFieldTrialName =
254 "WebRTC-ScreenshareLayerRates";
sprang429600d2017-01-26 06:12:26 -0800255static const char* kSimulcastScreenshareFieldTrialName =
256 "WebRTC-SimulcastScreenshare";
sprang@webrtc.org46d4d292014-12-23 15:19:35 +0000257
258ScreenshareLayerConfig::ScreenshareLayerConfig(int tl0_bitrate, int tl1_bitrate)
259 : tl0_bitrate_kbps(tl0_bitrate), tl1_bitrate_kbps(tl1_bitrate) {
260}
261
262ScreenshareLayerConfig ScreenshareLayerConfig::GetDefault() {
263 std::string group =
264 webrtc::field_trial::FindFullName(kScreencastLayerFieldTrialName);
265
266 ScreenshareLayerConfig config(kScreenshareDefaultTl0BitrateKbps,
267 kScreenshareDefaultTl1BitrateKbps);
268 if (!group.empty() && !FromFieldTrialGroup(group, &config)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100269 RTC_LOG(LS_WARNING) << "Unable to parse WebRTC-ScreenshareLayerRates"
270 " field trial group: '"
271 << group << "'.";
sprang@webrtc.org46d4d292014-12-23 15:19:35 +0000272 }
273 return config;
274}
275
276bool ScreenshareLayerConfig::FromFieldTrialGroup(
277 const std::string& group,
278 ScreenshareLayerConfig* config) {
279 // Parse field trial group name, containing bitrates for tl0 and tl1.
280 int tl0_bitrate;
281 int tl1_bitrate;
282 if (sscanf(group.c_str(), "%d-%d", &tl0_bitrate, &tl1_bitrate) != 2) {
283 return false;
284 }
285
286 // Sanity check.
287 if (tl0_bitrate < kScreenshareMinBitrateKbps ||
288 tl0_bitrate > kScreenshareMaxBitrateKbps ||
289 tl1_bitrate < kScreenshareMinBitrateKbps ||
290 tl1_bitrate > kScreenshareMaxBitrateKbps || tl0_bitrate > tl1_bitrate) {
291 return false;
292 }
293
294 config->tl0_bitrate_kbps = tl0_bitrate;
295 config->tl1_bitrate_kbps = tl1_bitrate;
296
297 return true;
298}
299
sprang429600d2017-01-26 06:12:26 -0800300bool UseSimulcastScreenshare() {
sprangc1b57a12017-02-28 08:50:47 -0800301 return webrtc::field_trial::IsEnabled(kSimulcastScreenshareFieldTrialName);
sprang429600d2017-01-26 06:12:26 -0800302}
303
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000304} // namespace cricket