blob: 6ebf66dd50efebc8af6313e8daa96c5b4b436e20 [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>
12
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "media/base/streamparams.h"
14#include "media/engine/constants.h"
15#include "media/engine/simulcast.h"
16#include "rtc_base/arraysize.h"
17#include "rtc_base/logging.h"
18#include "system_wrappers/include/field_trial.h"
tfarina5237aaf2015-11-10 23:44:30 -080019
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000020namespace cricket {
21
22struct SimulcastFormat {
23 int width;
24 int height;
25 // The maximum number of simulcast layers can be used for
26 // resolutions at |widthxheigh|.
27 size_t max_layers;
28 // The maximum bitrate for encoding stream at |widthxheight|, when we are
29 // not sending the next higher spatial stream.
pbosbe16f792015-10-16 12:49:39 -070030 int max_bitrate_kbps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000031 // The target bitrate for encoding stream at |widthxheight|, when this layer
32 // is not the highest layer (i.e., when we are sending another higher spatial
33 // stream).
pbosbe16f792015-10-16 12:49:39 -070034 int target_bitrate_kbps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000035 // The minimum bitrate needed for encoding stream at |widthxheight|.
pbosbe16f792015-10-16 12:49:39 -070036 int min_bitrate_kbps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000037};
38
39// These tables describe from which resolution we can use how many
40// simulcast layers at what bitrates (maximum, target, and minimum).
41// Important!! Keep this table from high resolution to low resolution.
42const SimulcastFormat kSimulcastFormats[] = {
pbosbe16f792015-10-16 12:49:39 -070043 {1920, 1080, 3, 5000, 4000, 800},
44 {1280, 720, 3, 2500, 2500, 600},
45 {960, 540, 3, 900, 900, 450},
46 {640, 360, 2, 700, 500, 150},
47 {480, 270, 2, 450, 350, 150},
48 {320, 180, 1, 200, 150, 30},
49 {0, 0, 1, 200, 150, 30}
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000050};
51
sprang02569ad2017-07-06 05:05:50 -070052const int kMaxScreenshareSimulcastStreams = 2;
sprang429600d2017-01-26 06:12:26 -080053
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000054// Multiway: Number of temporal layers for each simulcast stream, for maximum
55// possible number of simulcast streams |kMaxSimulcastStreams|. The array
56// goes from lowest resolution at position 0 to highest resolution.
57// For example, first three elements correspond to say: QVGA, VGA, WHD.
58static const int
59 kDefaultConferenceNumberOfTemporalLayers[webrtc::kMaxSimulcastStreams] =
60 {3, 3, 3, 3};
61
Peter Boström0c4e06b2015-10-07 12:23:21 +020062void GetSimulcastSsrcs(const StreamParams& sp, std::vector<uint32_t>* ssrcs) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000063 const SsrcGroup* sim_group = sp.get_ssrc_group(kSimSsrcGroupSemantics);
64 if (sim_group) {
65 ssrcs->insert(
66 ssrcs->end(), sim_group->ssrcs.begin(), sim_group->ssrcs.end());
67 }
68}
69
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000070void MaybeExchangeWidthHeight(int* width, int* height) {
71 // |kSimulcastFormats| assumes |width| >= |height|. If not, exchange them
72 // before comparing.
73 if (*width < *height) {
74 int temp = *width;
75 *width = *height;
76 *height = temp;
77 }
78}
79
80int FindSimulcastFormatIndex(int width, int height) {
81 MaybeExchangeWidthHeight(&width, &height);
82
kjellandera96e2d72016-02-04 23:52:28 -080083 for (uint32_t i = 0; i < arraysize(kSimulcastFormats); ++i) {
sprang429600d2017-01-26 06:12:26 -080084 if (width * height >=
85 kSimulcastFormats[i].width * kSimulcastFormats[i].height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000086 return i;
87 }
88 }
89 return -1;
90}
91
92int FindSimulcastFormatIndex(int width, int height, size_t max_layers) {
93 MaybeExchangeWidthHeight(&width, &height);
94
kjellandera96e2d72016-02-04 23:52:28 -080095 for (uint32_t i = 0; i < arraysize(kSimulcastFormats); ++i) {
sprang429600d2017-01-26 06:12:26 -080096 if (width * height >=
97 kSimulcastFormats[i].width * kSimulcastFormats[i].height &&
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000098 max_layers == kSimulcastFormats[i].max_layers) {
99 return i;
100 }
101 }
102 return -1;
103}
104
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000105// Simulcast stream width and height must both be dividable by
106// |2 ^ simulcast_layers - 1|.
107int NormalizeSimulcastSize(int size, size_t simulcast_layers) {
108 const int base2_exponent = static_cast<int>(simulcast_layers) - 1;
109 return ((size >> base2_exponent) << base2_exponent);
110}
111
112size_t FindSimulcastMaxLayers(int width, int height) {
113 int index = FindSimulcastFormatIndex(width, height);
114 if (index == -1) {
115 return -1;
116 }
117 return kSimulcastFormats[index].max_layers;
118}
119
120// TODO(marpan): Investigate if we should return 0 instead of -1 in
121// FindSimulcast[Max/Target/Min]Bitrate functions below, since the
122// codec struct max/min/targeBitrates are unsigned.
sprang429600d2017-01-26 06:12:26 -0800123int FindSimulcastMaxBitrateBps(int width, int height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000124 const int format_index = FindSimulcastFormatIndex(width, height);
125 if (format_index == -1) {
126 return -1;
127 }
pbosbe16f792015-10-16 12:49:39 -0700128 return kSimulcastFormats[format_index].max_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000129}
130
sprang429600d2017-01-26 06:12:26 -0800131int FindSimulcastTargetBitrateBps(int width, int height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000132 const int format_index = FindSimulcastFormatIndex(width, height);
133 if (format_index == -1) {
134 return -1;
135 }
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);
141 if (format_index == -1) {
142 return -1;
143 }
pbosbe16f792015-10-16 12:49:39 -0700144 return kSimulcastFormats[format_index].min_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000145}
146
147bool SlotSimulcastMaxResolution(size_t max_layers, int* width, int* height) {
148 int index = FindSimulcastFormatIndex(*width, *height, max_layers);
149 if (index == -1) {
150 LOG(LS_ERROR) << "SlotSimulcastMaxResolution";
151 return false;
152 }
153
154 *width = kSimulcastFormats[index].width;
155 *height = kSimulcastFormats[index].height;
156 LOG(LS_INFO) << "SlotSimulcastMaxResolution to width:" << *width
157 << " height:" << *height;
158 return true;
159}
160
161int GetTotalMaxBitrateBps(const std::vector<webrtc::VideoStream>& streams) {
162 int total_max_bitrate_bps = 0;
163 for (size_t s = 0; s < streams.size() - 1; ++s) {
164 total_max_bitrate_bps += streams[s].target_bitrate_bps;
165 }
166 total_max_bitrate_bps += streams.back().max_bitrate_bps;
167 return total_max_bitrate_bps;
168}
169
sprang429600d2017-01-26 06:12:26 -0800170std::vector<webrtc::VideoStream> GetSimulcastConfig(size_t max_streams,
171 int width,
172 int height,
173 int max_bitrate_bps,
174 int max_qp,
175 int max_framerate,
176 bool is_screencast) {
177 size_t num_simulcast_layers;
178 if (is_screencast) {
sprang02569ad2017-07-06 05:05:50 -0700179 if (UseSimulcastScreenshare()) {
180 num_simulcast_layers =
181 std::min<int>(max_streams, kMaxScreenshareSimulcastStreams);
182 } else {
183 num_simulcast_layers = 1;
184 }
sprang429600d2017-01-26 06:12:26 -0800185 } else {
186 num_simulcast_layers = FindSimulcastMaxLayers(width, height);
187 }
188
189 if (num_simulcast_layers > max_streams) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000190 // If the number of SSRCs in the group differs from our target
191 // number of simulcast streams for current resolution, switch down
192 // to a resolution that matches our number of SSRCs.
193 if (!SlotSimulcastMaxResolution(max_streams, &width, &height)) {
194 return std::vector<webrtc::VideoStream>();
195 }
sprang429600d2017-01-26 06:12:26 -0800196 num_simulcast_layers = max_streams;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000197 }
198 std::vector<webrtc::VideoStream> streams;
sprang429600d2017-01-26 06:12:26 -0800199 streams.resize(num_simulcast_layers);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000200
sprang02569ad2017-07-06 05:05:50 -0700201 if (is_screencast) {
202 ScreenshareLayerConfig config = ScreenshareLayerConfig::GetDefault();
203 // For legacy screenshare in conference mode, tl0 and tl1 bitrates are
204 // piggybacked on the VideoCodec struct as target and max bitrates,
205 // respectively. See eg. webrtc::VP8EncoderImpl::SetRates().
206 streams[0].width = width;
207 streams[0].height = height;
208 streams[0].max_qp = max_qp;
209 streams[0].max_framerate = 5;
210 streams[0].min_bitrate_bps = kMinVideoBitrateKbps * 1000;
211 streams[0].target_bitrate_bps = config.tl0_bitrate_kbps * 1000;
212 streams[0].max_bitrate_bps = config.tl1_bitrate_kbps * 1000;
213 streams[0].temporal_layer_thresholds_bps.clear();
214 streams[0].temporal_layer_thresholds_bps.push_back(config.tl0_bitrate_kbps *
215 1000);
216
217 // With simulcast enabled, add another spatial layer. This one will have a
218 // more normal layout, with the regular 3 temporal layer pattern and no fps
219 // restrictions. The base simulcast stream will still use legacy setup.
220 if (num_simulcast_layers == kMaxScreenshareSimulcastStreams) {
221 // Add optional upper simulcast layer.
222 // Lowest temporal layers of a 3 layer setup will have 40% of the total
223 // bitrate allocation for that stream. Make sure the gap between the
224 // target of the lower stream and first temporal layer of the higher one
225 // is at most 2x the bitrate, so that upswitching is not hampered by
226 // stalled bitrate estimates.
227 int max_bitrate_bps = 2 * ((streams[0].target_bitrate_bps * 10) / 4);
228 // Cap max bitrate so it isn't overly high for the given resolution.
229 max_bitrate_bps = std::min<int>(
230 max_bitrate_bps, FindSimulcastMaxBitrateBps(width, height));
231
232 streams[1].width = width;
233 streams[1].height = height;
234 streams[1].max_qp = max_qp;
235 streams[1].max_framerate = max_framerate;
236 // Three temporal layers means two thresholds.
237 streams[1].temporal_layer_thresholds_bps.resize(2);
238 streams[1].min_bitrate_bps = streams[0].target_bitrate_bps * 2;
239 streams[1].target_bitrate_bps = max_bitrate_bps;
240 streams[1].max_bitrate_bps = max_bitrate_bps;
241 }
242 } else {
sprang429600d2017-01-26 06:12:26 -0800243 // Format width and height has to be divisible by |2 ^ number_streams - 1|.
244 width = NormalizeSimulcastSize(width, num_simulcast_layers);
245 height = NormalizeSimulcastSize(height, num_simulcast_layers);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000246
sprang02569ad2017-07-06 05:05:50 -0700247 // Add simulcast sub-streams from lower resolution to higher resolutions.
248 // Add simulcast streams, from highest resolution (|s| = number_streams -1)
249 // to lowest resolution at |s| = 0.
250 for (size_t s = num_simulcast_layers - 1;; --s) {
251 streams[s].width = width;
252 streams[s].height = height;
253 // TODO(pbos): Fill actual temporal-layer bitrate thresholds.
254 streams[s].max_qp = max_qp;
sprang429600d2017-01-26 06:12:26 -0800255 streams[s].temporal_layer_thresholds_bps.resize(
256 kDefaultConferenceNumberOfTemporalLayers[s] - 1);
257 streams[s].max_bitrate_bps = FindSimulcastMaxBitrateBps(width, height);
258 streams[s].target_bitrate_bps =
259 FindSimulcastTargetBitrateBps(width, height);
260 streams[s].min_bitrate_bps = FindSimulcastMinBitrateBps(width, height);
261 streams[s].max_framerate = max_framerate;
sprang429600d2017-01-26 06:12:26 -0800262
sprang3ebabf12017-02-16 07:35:22 -0800263 width /= 2;
264 height /= 2;
sprang317005a2017-06-08 07:12:17 -0700265
sprang02569ad2017-07-06 05:05:50 -0700266 if (s == 0)
267 break;
268 }
269
270 // Spend additional bits to boost the max stream.
271 int bitrate_left_bps = max_bitrate_bps - GetTotalMaxBitrateBps(streams);
272 if (bitrate_left_bps > 0) {
273 streams.back().max_bitrate_bps += bitrate_left_bps;
274 }
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000275 }
276
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000277 return streams;
278}
279
sprang@webrtc.org46d4d292014-12-23 15:19:35 +0000280static const int kScreenshareMinBitrateKbps = 50;
281static const int kScreenshareMaxBitrateKbps = 6000;
Erik Språng2c4c9142015-06-24 11:24:44 +0200282static const int kScreenshareDefaultTl0BitrateKbps = 200;
sprang@webrtc.org46d4d292014-12-23 15:19:35 +0000283static const int kScreenshareDefaultTl1BitrateKbps = 1000;
284
285static const char* kScreencastLayerFieldTrialName =
286 "WebRTC-ScreenshareLayerRates";
sprang429600d2017-01-26 06:12:26 -0800287static const char* kSimulcastScreenshareFieldTrialName =
288 "WebRTC-SimulcastScreenshare";
sprang@webrtc.org46d4d292014-12-23 15:19:35 +0000289
290ScreenshareLayerConfig::ScreenshareLayerConfig(int tl0_bitrate, int tl1_bitrate)
291 : tl0_bitrate_kbps(tl0_bitrate), tl1_bitrate_kbps(tl1_bitrate) {
292}
293
294ScreenshareLayerConfig ScreenshareLayerConfig::GetDefault() {
295 std::string group =
296 webrtc::field_trial::FindFullName(kScreencastLayerFieldTrialName);
297
298 ScreenshareLayerConfig config(kScreenshareDefaultTl0BitrateKbps,
299 kScreenshareDefaultTl1BitrateKbps);
300 if (!group.empty() && !FromFieldTrialGroup(group, &config)) {
301 LOG(LS_WARNING) << "Unable to parse WebRTC-ScreenshareLayerRates"
302 " field trial group: '" << group << "'.";
303 }
304 return config;
305}
306
307bool ScreenshareLayerConfig::FromFieldTrialGroup(
308 const std::string& group,
309 ScreenshareLayerConfig* config) {
310 // Parse field trial group name, containing bitrates for tl0 and tl1.
311 int tl0_bitrate;
312 int tl1_bitrate;
313 if (sscanf(group.c_str(), "%d-%d", &tl0_bitrate, &tl1_bitrate) != 2) {
314 return false;
315 }
316
317 // Sanity check.
318 if (tl0_bitrate < kScreenshareMinBitrateKbps ||
319 tl0_bitrate > kScreenshareMaxBitrateKbps ||
320 tl1_bitrate < kScreenshareMinBitrateKbps ||
321 tl1_bitrate > kScreenshareMaxBitrateKbps || tl0_bitrate > tl1_bitrate) {
322 return false;
323 }
324
325 config->tl0_bitrate_kbps = tl0_bitrate;
326 config->tl1_bitrate_kbps = tl1_bitrate;
327
328 return true;
329}
330
sprang429600d2017-01-26 06:12:26 -0800331bool UseSimulcastScreenshare() {
sprangc1b57a12017-02-28 08:50:47 -0800332 return webrtc::field_trial::IsEnabled(kSimulcastScreenshareFieldTrialName);
sprang429600d2017-01-26 06:12:26 -0800333}
334
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000335} // namespace cricket