blob: f2b3c3addfc3099a4913f8639e6e8d2755e348fe [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 +000072void MaybeExchangeWidthHeight(int* width, int* height) {
73 // |kSimulcastFormats| assumes |width| >= |height|. If not, exchange them
74 // before comparing.
75 if (*width < *height) {
76 int temp = *width;
77 *width = *height;
78 *height = temp;
79 }
80}
81
82int FindSimulcastFormatIndex(int width, int height) {
83 MaybeExchangeWidthHeight(&width, &height);
84
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 }
91 return -1;
92}
93
94int FindSimulcastFormatIndex(int width, int height, size_t max_layers) {
95 MaybeExchangeWidthHeight(&width, &height);
96
kjellandera96e2d72016-02-04 23:52:28 -080097 for (uint32_t i = 0; i < arraysize(kSimulcastFormats); ++i) {
sprang429600d2017-01-26 06:12:26 -080098 if (width * height >=
99 kSimulcastFormats[i].width * kSimulcastFormats[i].height &&
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000100 max_layers == kSimulcastFormats[i].max_layers) {
101 return i;
102 }
103 }
104 return -1;
105}
106
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000107// Simulcast stream width and height must both be dividable by
108// |2 ^ simulcast_layers - 1|.
109int NormalizeSimulcastSize(int size, size_t simulcast_layers) {
110 const int base2_exponent = static_cast<int>(simulcast_layers) - 1;
111 return ((size >> base2_exponent) << base2_exponent);
112}
113
114size_t FindSimulcastMaxLayers(int width, int height) {
115 int index = FindSimulcastFormatIndex(width, height);
116 if (index == -1) {
117 return -1;
118 }
119 return kSimulcastFormats[index].max_layers;
120}
121
122// TODO(marpan): Investigate if we should return 0 instead of -1 in
123// FindSimulcast[Max/Target/Min]Bitrate functions below, since the
124// codec struct max/min/targeBitrates are unsigned.
sprang429600d2017-01-26 06:12:26 -0800125int FindSimulcastMaxBitrateBps(int width, int height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000126 const int format_index = FindSimulcastFormatIndex(width, height);
127 if (format_index == -1) {
128 return -1;
129 }
pbosbe16f792015-10-16 12:49:39 -0700130 return kSimulcastFormats[format_index].max_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000131}
132
sprang429600d2017-01-26 06:12:26 -0800133int FindSimulcastTargetBitrateBps(int width, int height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000134 const int format_index = FindSimulcastFormatIndex(width, height);
135 if (format_index == -1) {
136 return -1;
137 }
pbosbe16f792015-10-16 12:49:39 -0700138 return kSimulcastFormats[format_index].target_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000139}
140
sprang429600d2017-01-26 06:12:26 -0800141int FindSimulcastMinBitrateBps(int width, int height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000142 const int format_index = FindSimulcastFormatIndex(width, height);
143 if (format_index == -1) {
144 return -1;
145 }
pbosbe16f792015-10-16 12:49:39 -0700146 return kSimulcastFormats[format_index].min_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000147}
148
149bool SlotSimulcastMaxResolution(size_t max_layers, int* width, int* height) {
150 int index = FindSimulcastFormatIndex(*width, *height, max_layers);
151 if (index == -1) {
152 LOG(LS_ERROR) << "SlotSimulcastMaxResolution";
153 return false;
154 }
155
156 *width = kSimulcastFormats[index].width;
157 *height = kSimulcastFormats[index].height;
158 LOG(LS_INFO) << "SlotSimulcastMaxResolution to width:" << *width
159 << " height:" << *height;
160 return true;
161}
162
163int GetTotalMaxBitrateBps(const std::vector<webrtc::VideoStream>& streams) {
164 int total_max_bitrate_bps = 0;
165 for (size_t s = 0; s < streams.size() - 1; ++s) {
166 total_max_bitrate_bps += streams[s].target_bitrate_bps;
167 }
168 total_max_bitrate_bps += streams.back().max_bitrate_bps;
169 return total_max_bitrate_bps;
170}
171
sprang429600d2017-01-26 06:12:26 -0800172std::vector<webrtc::VideoStream> GetSimulcastConfig(size_t max_streams,
173 int width,
174 int height,
175 int max_bitrate_bps,
176 int max_qp,
177 int max_framerate,
178 bool is_screencast) {
179 size_t num_simulcast_layers;
180 if (is_screencast) {
sprang02569ad2017-07-06 05:05:50 -0700181 if (UseSimulcastScreenshare()) {
182 num_simulcast_layers =
183 std::min<int>(max_streams, kMaxScreenshareSimulcastStreams);
184 } else {
185 num_simulcast_layers = 1;
186 }
sprang429600d2017-01-26 06:12:26 -0800187 } else {
188 num_simulcast_layers = FindSimulcastMaxLayers(width, height);
189 }
190
191 if (num_simulcast_layers > max_streams) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000192 // If the number of SSRCs in the group differs from our target
193 // number of simulcast streams for current resolution, switch down
194 // to a resolution that matches our number of SSRCs.
195 if (!SlotSimulcastMaxResolution(max_streams, &width, &height)) {
196 return std::vector<webrtc::VideoStream>();
197 }
sprang429600d2017-01-26 06:12:26 -0800198 num_simulcast_layers = max_streams;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000199 }
200 std::vector<webrtc::VideoStream> streams;
sprang429600d2017-01-26 06:12:26 -0800201 streams.resize(num_simulcast_layers);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000202
sprang02569ad2017-07-06 05:05:50 -0700203 if (is_screencast) {
204 ScreenshareLayerConfig config = ScreenshareLayerConfig::GetDefault();
205 // For legacy screenshare in conference mode, tl0 and tl1 bitrates are
206 // piggybacked on the VideoCodec struct as target and max bitrates,
207 // respectively. See eg. webrtc::VP8EncoderImpl::SetRates().
208 streams[0].width = width;
209 streams[0].height = height;
210 streams[0].max_qp = max_qp;
211 streams[0].max_framerate = 5;
212 streams[0].min_bitrate_bps = kMinVideoBitrateKbps * 1000;
213 streams[0].target_bitrate_bps = config.tl0_bitrate_kbps * 1000;
214 streams[0].max_bitrate_bps = config.tl1_bitrate_kbps * 1000;
215 streams[0].temporal_layer_thresholds_bps.clear();
216 streams[0].temporal_layer_thresholds_bps.push_back(config.tl0_bitrate_kbps *
217 1000);
218
219 // With simulcast enabled, add another spatial layer. This one will have a
220 // more normal layout, with the regular 3 temporal layer pattern and no fps
221 // restrictions. The base simulcast stream will still use legacy setup.
222 if (num_simulcast_layers == kMaxScreenshareSimulcastStreams) {
223 // Add optional upper simulcast layer.
224 // Lowest temporal layers of a 3 layer setup will have 40% of the total
225 // bitrate allocation for that stream. Make sure the gap between the
226 // target of the lower stream and first temporal layer of the higher one
227 // is at most 2x the bitrate, so that upswitching is not hampered by
228 // stalled bitrate estimates.
229 int max_bitrate_bps = 2 * ((streams[0].target_bitrate_bps * 10) / 4);
230 // Cap max bitrate so it isn't overly high for the given resolution.
231 max_bitrate_bps = std::min<int>(
232 max_bitrate_bps, FindSimulcastMaxBitrateBps(width, height));
233
234 streams[1].width = width;
235 streams[1].height = height;
236 streams[1].max_qp = max_qp;
237 streams[1].max_framerate = max_framerate;
238 // Three temporal layers means two thresholds.
239 streams[1].temporal_layer_thresholds_bps.resize(2);
240 streams[1].min_bitrate_bps = streams[0].target_bitrate_bps * 2;
241 streams[1].target_bitrate_bps = max_bitrate_bps;
242 streams[1].max_bitrate_bps = max_bitrate_bps;
243 }
244 } else {
sprang429600d2017-01-26 06:12:26 -0800245 // Format width and height has to be divisible by |2 ^ number_streams - 1|.
246 width = NormalizeSimulcastSize(width, num_simulcast_layers);
247 height = NormalizeSimulcastSize(height, num_simulcast_layers);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000248
sprang02569ad2017-07-06 05:05:50 -0700249 // Add simulcast sub-streams from lower resolution to higher resolutions.
250 // Add simulcast streams, from highest resolution (|s| = number_streams -1)
251 // to lowest resolution at |s| = 0.
252 for (size_t s = num_simulcast_layers - 1;; --s) {
253 streams[s].width = width;
254 streams[s].height = height;
255 // TODO(pbos): Fill actual temporal-layer bitrate thresholds.
256 streams[s].max_qp = max_qp;
sprang429600d2017-01-26 06:12:26 -0800257 streams[s].temporal_layer_thresholds_bps.resize(
258 kDefaultConferenceNumberOfTemporalLayers[s] - 1);
259 streams[s].max_bitrate_bps = FindSimulcastMaxBitrateBps(width, height);
260 streams[s].target_bitrate_bps =
261 FindSimulcastTargetBitrateBps(width, height);
262 streams[s].min_bitrate_bps = FindSimulcastMinBitrateBps(width, height);
263 streams[s].max_framerate = max_framerate;
sprang429600d2017-01-26 06:12:26 -0800264
sprang3ebabf12017-02-16 07:35:22 -0800265 width /= 2;
266 height /= 2;
sprang317005a2017-06-08 07:12:17 -0700267
sprang02569ad2017-07-06 05:05:50 -0700268 if (s == 0)
269 break;
270 }
271
272 // Spend additional bits to boost the max stream.
273 int bitrate_left_bps = max_bitrate_bps - GetTotalMaxBitrateBps(streams);
274 if (bitrate_left_bps > 0) {
275 streams.back().max_bitrate_bps += bitrate_left_bps;
276 }
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000277 }
278
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000279 return streams;
280}
281
sprang@webrtc.org46d4d292014-12-23 15:19:35 +0000282static const int kScreenshareMinBitrateKbps = 50;
283static const int kScreenshareMaxBitrateKbps = 6000;
Erik Språng2c4c9142015-06-24 11:24:44 +0200284static const int kScreenshareDefaultTl0BitrateKbps = 200;
sprang@webrtc.org46d4d292014-12-23 15:19:35 +0000285static const int kScreenshareDefaultTl1BitrateKbps = 1000;
286
287static const char* kScreencastLayerFieldTrialName =
288 "WebRTC-ScreenshareLayerRates";
sprang429600d2017-01-26 06:12:26 -0800289static const char* kSimulcastScreenshareFieldTrialName =
290 "WebRTC-SimulcastScreenshare";
sprang@webrtc.org46d4d292014-12-23 15:19:35 +0000291
292ScreenshareLayerConfig::ScreenshareLayerConfig(int tl0_bitrate, int tl1_bitrate)
293 : tl0_bitrate_kbps(tl0_bitrate), tl1_bitrate_kbps(tl1_bitrate) {
294}
295
296ScreenshareLayerConfig ScreenshareLayerConfig::GetDefault() {
297 std::string group =
298 webrtc::field_trial::FindFullName(kScreencastLayerFieldTrialName);
299
300 ScreenshareLayerConfig config(kScreenshareDefaultTl0BitrateKbps,
301 kScreenshareDefaultTl1BitrateKbps);
302 if (!group.empty() && !FromFieldTrialGroup(group, &config)) {
303 LOG(LS_WARNING) << "Unable to parse WebRTC-ScreenshareLayerRates"
304 " field trial group: '" << group << "'.";
305 }
306 return config;
307}
308
309bool ScreenshareLayerConfig::FromFieldTrialGroup(
310 const std::string& group,
311 ScreenshareLayerConfig* config) {
312 // Parse field trial group name, containing bitrates for tl0 and tl1.
313 int tl0_bitrate;
314 int tl1_bitrate;
315 if (sscanf(group.c_str(), "%d-%d", &tl0_bitrate, &tl1_bitrate) != 2) {
316 return false;
317 }
318
319 // Sanity check.
320 if (tl0_bitrate < kScreenshareMinBitrateKbps ||
321 tl0_bitrate > kScreenshareMaxBitrateKbps ||
322 tl1_bitrate < kScreenshareMinBitrateKbps ||
323 tl1_bitrate > kScreenshareMaxBitrateKbps || tl0_bitrate > tl1_bitrate) {
324 return false;
325 }
326
327 config->tl0_bitrate_kbps = tl0_bitrate;
328 config->tl1_bitrate_kbps = tl1_bitrate;
329
330 return true;
331}
332
sprang429600d2017-01-26 06:12:26 -0800333bool UseSimulcastScreenshare() {
sprangc1b57a12017-02-28 08:50:47 -0800334 return webrtc::field_trial::IsEnabled(kSimulcastScreenshareFieldTrialName);
sprang429600d2017-01-26 06:12:26 -0800335}
336
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000337} // namespace cricket