blob: aa042468ad1e52757b1b59c8c025a2f2cd09d6e7 [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
Peter Boström0c4e06b2015-10-07 12:23:21 +020064void GetSimulcastSsrcs(const StreamParams& sp, std::vector<uint32_t>* ssrcs) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000065 const SsrcGroup* sim_group = sp.get_ssrc_group(kSimSsrcGroupSemantics);
66 if (sim_group) {
67 ssrcs->insert(
68 ssrcs->end(), sim_group->ssrcs.begin(), sim_group->ssrcs.end());
69 }
70}
71
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000072int FindSimulcastFormatIndex(int width, int height) {
Seth Hampson438663e2018-01-09 11:14:14 -080073 RTC_DCHECK_GE(width, 0);
74 RTC_DCHECK_GE(height, 0);
kjellandera96e2d72016-02-04 23:52:28 -080075 for (uint32_t i = 0; i < arraysize(kSimulcastFormats); ++i) {
sprang429600d2017-01-26 06:12:26 -080076 if (width * height >=
77 kSimulcastFormats[i].width * kSimulcastFormats[i].height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000078 return i;
79 }
80 }
Seth Hampson438663e2018-01-09 11:14:14 -080081 RTC_NOTREACHED();
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000082 return -1;
83}
84
85int FindSimulcastFormatIndex(int width, int height, size_t max_layers) {
Seth Hampson438663e2018-01-09 11:14:14 -080086 RTC_DCHECK_GE(width, 0);
87 RTC_DCHECK_GE(height, 0);
88 RTC_DCHECK_GT(max_layers, 0);
kjellandera96e2d72016-02-04 23:52:28 -080089 for (uint32_t i = 0; i < arraysize(kSimulcastFormats); ++i) {
sprang429600d2017-01-26 06:12:26 -080090 if (width * height >=
91 kSimulcastFormats[i].width * kSimulcastFormats[i].height &&
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000092 max_layers == kSimulcastFormats[i].max_layers) {
93 return i;
94 }
95 }
Seth Hampson438663e2018-01-09 11:14:14 -080096 RTC_NOTREACHED();
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000097 return -1;
98}
99
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000100// Simulcast stream width and height must both be dividable by
101// |2 ^ simulcast_layers - 1|.
102int NormalizeSimulcastSize(int size, size_t simulcast_layers) {
103 const int base2_exponent = static_cast<int>(simulcast_layers) - 1;
104 return ((size >> base2_exponent) << base2_exponent);
105}
106
107size_t FindSimulcastMaxLayers(int width, int height) {
108 int index = FindSimulcastFormatIndex(width, height);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000109 return kSimulcastFormats[index].max_layers;
110}
111
sprang429600d2017-01-26 06:12:26 -0800112int FindSimulcastMaxBitrateBps(int width, int height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000113 const int format_index = FindSimulcastFormatIndex(width, height);
pbosbe16f792015-10-16 12:49:39 -0700114 return kSimulcastFormats[format_index].max_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000115}
116
sprang429600d2017-01-26 06:12:26 -0800117int FindSimulcastTargetBitrateBps(int width, int height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000118 const int format_index = FindSimulcastFormatIndex(width, height);
pbosbe16f792015-10-16 12:49:39 -0700119 return kSimulcastFormats[format_index].target_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000120}
121
sprang429600d2017-01-26 06:12:26 -0800122int FindSimulcastMinBitrateBps(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].min_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000125}
126
Seth Hampson438663e2018-01-09 11:14:14 -0800127void SlotSimulcastMaxResolution(size_t max_layers, int* width, int* height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000128 int index = FindSimulcastFormatIndex(*width, *height, max_layers);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000129 *width = kSimulcastFormats[index].width;
130 *height = kSimulcastFormats[index].height;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100131 RTC_LOG(LS_INFO) << "SlotSimulcastMaxResolution to width:" << *width
132 << " height:" << *height;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000133}
134
135int GetTotalMaxBitrateBps(const std::vector<webrtc::VideoStream>& streams) {
136 int total_max_bitrate_bps = 0;
137 for (size_t s = 0; s < streams.size() - 1; ++s) {
138 total_max_bitrate_bps += streams[s].target_bitrate_bps;
139 }
140 total_max_bitrate_bps += streams.back().max_bitrate_bps;
141 return total_max_bitrate_bps;
142}
143
Seth Hampson24722b32017-12-22 09:36:42 -0800144// TODO(shampson): This is a fix to not break downstream tests. Remove this
145// after updating downstream tests.
sprang429600d2017-01-26 06:12:26 -0800146std::vector<webrtc::VideoStream> GetSimulcastConfig(size_t max_streams,
147 int width,
148 int height,
149 int max_bitrate_bps,
150 int max_qp,
151 int max_framerate,
152 bool is_screencast) {
Seth Hampson24722b32017-12-22 09:36:42 -0800153 return GetSimulcastConfig(max_streams, width, height, max_bitrate_bps, 1.0,
154 max_qp, max_framerate, is_screencast);
155}
156
157std::vector<webrtc::VideoStream> GetSimulcastConfig(size_t max_streams,
158 int width,
159 int height,
160 int max_bitrate_bps,
161 double bitrate_priority,
162 int max_qp,
163 int max_framerate,
164 bool is_screencast) {
sprang429600d2017-01-26 06:12:26 -0800165 size_t num_simulcast_layers;
166 if (is_screencast) {
sprang02569ad2017-07-06 05:05:50 -0700167 if (UseSimulcastScreenshare()) {
168 num_simulcast_layers =
169 std::min<int>(max_streams, kMaxScreenshareSimulcastStreams);
170 } else {
171 num_simulcast_layers = 1;
172 }
sprang429600d2017-01-26 06:12:26 -0800173 } else {
174 num_simulcast_layers = FindSimulcastMaxLayers(width, height);
175 }
176
177 if (num_simulcast_layers > max_streams) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000178 // If the number of SSRCs in the group differs from our target
179 // number of simulcast streams for current resolution, switch down
180 // to a resolution that matches our number of SSRCs.
Seth Hampson438663e2018-01-09 11:14:14 -0800181 SlotSimulcastMaxResolution(max_streams, &width, &height);
sprang429600d2017-01-26 06:12:26 -0800182 num_simulcast_layers = max_streams;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000183 }
184 std::vector<webrtc::VideoStream> streams;
sprang429600d2017-01-26 06:12:26 -0800185 streams.resize(num_simulcast_layers);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000186
sprang02569ad2017-07-06 05:05:50 -0700187 if (is_screencast) {
188 ScreenshareLayerConfig config = ScreenshareLayerConfig::GetDefault();
189 // For legacy screenshare in conference mode, tl0 and tl1 bitrates are
190 // piggybacked on the VideoCodec struct as target and max bitrates,
191 // respectively. See eg. webrtc::VP8EncoderImpl::SetRates().
192 streams[0].width = width;
193 streams[0].height = height;
194 streams[0].max_qp = max_qp;
195 streams[0].max_framerate = 5;
Åsa Persson45bbc8a2017-11-13 10:16:47 +0100196 streams[0].min_bitrate_bps = kMinVideoBitrateBps;
sprang02569ad2017-07-06 05:05:50 -0700197 streams[0].target_bitrate_bps = config.tl0_bitrate_kbps * 1000;
198 streams[0].max_bitrate_bps = config.tl1_bitrate_kbps * 1000;
199 streams[0].temporal_layer_thresholds_bps.clear();
200 streams[0].temporal_layer_thresholds_bps.push_back(config.tl0_bitrate_kbps *
201 1000);
202
203 // With simulcast enabled, add another spatial layer. This one will have a
204 // more normal layout, with the regular 3 temporal layer pattern and no fps
205 // restrictions. The base simulcast stream will still use legacy setup.
206 if (num_simulcast_layers == kMaxScreenshareSimulcastStreams) {
207 // Add optional upper simulcast layer.
208 // Lowest temporal layers of a 3 layer setup will have 40% of the total
209 // bitrate allocation for that stream. Make sure the gap between the
210 // target of the lower stream and first temporal layer of the higher one
211 // is at most 2x the bitrate, so that upswitching is not hampered by
212 // stalled bitrate estimates.
213 int max_bitrate_bps = 2 * ((streams[0].target_bitrate_bps * 10) / 4);
214 // Cap max bitrate so it isn't overly high for the given resolution.
215 max_bitrate_bps = std::min<int>(
216 max_bitrate_bps, FindSimulcastMaxBitrateBps(width, height));
217
218 streams[1].width = width;
219 streams[1].height = height;
220 streams[1].max_qp = max_qp;
221 streams[1].max_framerate = max_framerate;
222 // Three temporal layers means two thresholds.
223 streams[1].temporal_layer_thresholds_bps.resize(2);
224 streams[1].min_bitrate_bps = streams[0].target_bitrate_bps * 2;
225 streams[1].target_bitrate_bps = max_bitrate_bps;
226 streams[1].max_bitrate_bps = max_bitrate_bps;
227 }
228 } else {
sprang429600d2017-01-26 06:12:26 -0800229 // Format width and height has to be divisible by |2 ^ number_streams - 1|.
230 width = NormalizeSimulcastSize(width, num_simulcast_layers);
231 height = NormalizeSimulcastSize(height, num_simulcast_layers);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000232
sprang02569ad2017-07-06 05:05:50 -0700233 // Add simulcast sub-streams from lower resolution to higher resolutions.
234 // Add simulcast streams, from highest resolution (|s| = number_streams -1)
235 // to lowest resolution at |s| = 0.
236 for (size_t s = num_simulcast_layers - 1;; --s) {
237 streams[s].width = width;
238 streams[s].height = height;
239 // TODO(pbos): Fill actual temporal-layer bitrate thresholds.
240 streams[s].max_qp = max_qp;
sprang429600d2017-01-26 06:12:26 -0800241 streams[s].temporal_layer_thresholds_bps.resize(
242 kDefaultConferenceNumberOfTemporalLayers[s] - 1);
243 streams[s].max_bitrate_bps = FindSimulcastMaxBitrateBps(width, height);
244 streams[s].target_bitrate_bps =
245 FindSimulcastTargetBitrateBps(width, height);
246 streams[s].min_bitrate_bps = FindSimulcastMinBitrateBps(width, height);
247 streams[s].max_framerate = max_framerate;
sprang429600d2017-01-26 06:12:26 -0800248
sprang3ebabf12017-02-16 07:35:22 -0800249 width /= 2;
250 height /= 2;
sprang317005a2017-06-08 07:12:17 -0700251
sprang02569ad2017-07-06 05:05:50 -0700252 if (s == 0)
253 break;
254 }
255
256 // Spend additional bits to boost the max stream.
257 int bitrate_left_bps = max_bitrate_bps - GetTotalMaxBitrateBps(streams);
258 if (bitrate_left_bps > 0) {
259 streams.back().max_bitrate_bps += bitrate_left_bps;
260 }
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000261 }
262
Seth Hampson24722b32017-12-22 09:36:42 -0800263 // The bitrate priority currently implemented on a per-sender level, so we
264 // just set it for the first video stream.
265 streams[0].bitrate_priority = bitrate_priority;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000266 return streams;
267}
268
sprang@webrtc.org46d4d292014-12-23 15:19:35 +0000269static const int kScreenshareMinBitrateKbps = 50;
270static const int kScreenshareMaxBitrateKbps = 6000;
Erik Språng2c4c9142015-06-24 11:24:44 +0200271static const int kScreenshareDefaultTl0BitrateKbps = 200;
sprang@webrtc.org46d4d292014-12-23 15:19:35 +0000272static const int kScreenshareDefaultTl1BitrateKbps = 1000;
273
274static const char* kScreencastLayerFieldTrialName =
275 "WebRTC-ScreenshareLayerRates";
sprang429600d2017-01-26 06:12:26 -0800276static const char* kSimulcastScreenshareFieldTrialName =
277 "WebRTC-SimulcastScreenshare";
sprang@webrtc.org46d4d292014-12-23 15:19:35 +0000278
279ScreenshareLayerConfig::ScreenshareLayerConfig(int tl0_bitrate, int tl1_bitrate)
280 : tl0_bitrate_kbps(tl0_bitrate), tl1_bitrate_kbps(tl1_bitrate) {
281}
282
283ScreenshareLayerConfig ScreenshareLayerConfig::GetDefault() {
284 std::string group =
285 webrtc::field_trial::FindFullName(kScreencastLayerFieldTrialName);
286
287 ScreenshareLayerConfig config(kScreenshareDefaultTl0BitrateKbps,
288 kScreenshareDefaultTl1BitrateKbps);
289 if (!group.empty() && !FromFieldTrialGroup(group, &config)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100290 RTC_LOG(LS_WARNING) << "Unable to parse WebRTC-ScreenshareLayerRates"
291 " field trial group: '"
292 << group << "'.";
sprang@webrtc.org46d4d292014-12-23 15:19:35 +0000293 }
294 return config;
295}
296
297bool ScreenshareLayerConfig::FromFieldTrialGroup(
298 const std::string& group,
299 ScreenshareLayerConfig* config) {
300 // Parse field trial group name, containing bitrates for tl0 and tl1.
301 int tl0_bitrate;
302 int tl1_bitrate;
303 if (sscanf(group.c_str(), "%d-%d", &tl0_bitrate, &tl1_bitrate) != 2) {
304 return false;
305 }
306
307 // Sanity check.
308 if (tl0_bitrate < kScreenshareMinBitrateKbps ||
309 tl0_bitrate > kScreenshareMaxBitrateKbps ||
310 tl1_bitrate < kScreenshareMinBitrateKbps ||
311 tl1_bitrate > kScreenshareMaxBitrateKbps || tl0_bitrate > tl1_bitrate) {
312 return false;
313 }
314
315 config->tl0_bitrate_kbps = tl0_bitrate;
316 config->tl1_bitrate_kbps = tl1_bitrate;
317
318 return true;
319}
320
sprang429600d2017-01-26 06:12:26 -0800321bool UseSimulcastScreenshare() {
sprangc1b57a12017-02-28 08:50:47 -0800322 return webrtc::field_trial::IsEnabled(kSimulcastScreenshareFieldTrialName);
sprang429600d2017-01-26 06:12:26 -0800323}
324
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000325} // namespace cricket