blob: f1cd2ced459810b03b1737632f947cf5d08a0b6d [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
tfarina5237aaf2015-11-10 23:44:30 -080013#include "webrtc/base/arraysize.h"
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000014#include "webrtc/base/logging.h"
kjellandera96e2d72016-02-04 23:52:28 -080015#include "webrtc/media/base/streamparams.h"
sprang429600d2017-01-26 06:12:26 -080016#include "webrtc/media/engine/constants.h"
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +010017#include "webrtc/media/engine/simulcast.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010018#include "webrtc/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
sprang429600d2017-01-26 06:12:26 -080052const int kDefaultScreenshareSimulcastStreams = 2;
53
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) {
179 num_simulcast_layers =
180 UseSimulcastScreenshare() ? kDefaultScreenshareSimulcastStreams : 1;
181 } else {
182 num_simulcast_layers = FindSimulcastMaxLayers(width, height);
183 }
184
185 if (num_simulcast_layers > max_streams) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000186 // If the number of SSRCs in the group differs from our target
187 // number of simulcast streams for current resolution, switch down
188 // to a resolution that matches our number of SSRCs.
189 if (!SlotSimulcastMaxResolution(max_streams, &width, &height)) {
190 return std::vector<webrtc::VideoStream>();
191 }
sprang429600d2017-01-26 06:12:26 -0800192 num_simulcast_layers = max_streams;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000193 }
194 std::vector<webrtc::VideoStream> streams;
sprang429600d2017-01-26 06:12:26 -0800195 streams.resize(num_simulcast_layers);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000196
sprang429600d2017-01-26 06:12:26 -0800197 if (!is_screencast) {
198 // Format width and height has to be divisible by |2 ^ number_streams - 1|.
199 width = NormalizeSimulcastSize(width, num_simulcast_layers);
200 height = NormalizeSimulcastSize(height, num_simulcast_layers);
201 }
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000202
203 // Add simulcast sub-streams from lower resolution to higher resolutions.
204 // Add simulcast streams, from highest resolution (|s| = number_streams -1)
205 // to lowest resolution at |s| = 0.
sprang429600d2017-01-26 06:12:26 -0800206 for (size_t s = num_simulcast_layers - 1;; --s) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000207 streams[s].width = width;
208 streams[s].height = height;
209 // TODO(pbos): Fill actual temporal-layer bitrate thresholds.
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000210 streams[s].max_qp = max_qp;
sprang429600d2017-01-26 06:12:26 -0800211 if (is_screencast && s == 0) {
212 ScreenshareLayerConfig config = ScreenshareLayerConfig::GetDefault();
213 // For legacy screenshare in conference mode, tl0 and tl1 bitrates are
214 // piggybacked on the VideoCodec struct as target and max bitrates,
215 // respectively. See eg. webrtc::VP8EncoderImpl::SetRates().
216 streams[s].min_bitrate_bps = kMinVideoBitrateKbps * 1000;
217 streams[s].target_bitrate_bps = config.tl0_bitrate_kbps * 1000;
218 streams[s].max_bitrate_bps = config.tl1_bitrate_kbps * 1000;
219 streams[s].temporal_layer_thresholds_bps.clear();
220 streams[s].temporal_layer_thresholds_bps.push_back(
221 config.tl0_bitrate_kbps * 1000);
222 streams[s].max_framerate = 5;
223 } else {
224 streams[s].temporal_layer_thresholds_bps.resize(
225 kDefaultConferenceNumberOfTemporalLayers[s] - 1);
226 streams[s].max_bitrate_bps = FindSimulcastMaxBitrateBps(width, height);
227 streams[s].target_bitrate_bps =
228 FindSimulcastTargetBitrateBps(width, height);
229 streams[s].min_bitrate_bps = FindSimulcastMinBitrateBps(width, height);
230 streams[s].max_framerate = max_framerate;
231 }
232
sprang3ebabf12017-02-16 07:35:22 -0800233 if (!is_screencast) {
234 width /= 2;
235 height /= 2;
236 }
sprang429600d2017-01-26 06:12:26 -0800237 if (s == 0)
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000238 break;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000239 }
240
241 // Spend additional bits to boost the max stream.
242 int bitrate_left_bps = max_bitrate_bps - GetTotalMaxBitrateBps(streams);
243 if (bitrate_left_bps > 0) {
244 streams.back().max_bitrate_bps += bitrate_left_bps;
245 }
246
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000247 return streams;
248}
249
sprang@webrtc.org46d4d292014-12-23 15:19:35 +0000250static const int kScreenshareMinBitrateKbps = 50;
251static const int kScreenshareMaxBitrateKbps = 6000;
Erik Språng2c4c9142015-06-24 11:24:44 +0200252static const int kScreenshareDefaultTl0BitrateKbps = 200;
sprang@webrtc.org46d4d292014-12-23 15:19:35 +0000253static const int kScreenshareDefaultTl1BitrateKbps = 1000;
254
255static const char* kScreencastLayerFieldTrialName =
256 "WebRTC-ScreenshareLayerRates";
sprang429600d2017-01-26 06:12:26 -0800257static const char* kSimulcastScreenshareFieldTrialName =
258 "WebRTC-SimulcastScreenshare";
sprang@webrtc.org46d4d292014-12-23 15:19:35 +0000259
260ScreenshareLayerConfig::ScreenshareLayerConfig(int tl0_bitrate, int tl1_bitrate)
261 : tl0_bitrate_kbps(tl0_bitrate), tl1_bitrate_kbps(tl1_bitrate) {
262}
263
264ScreenshareLayerConfig ScreenshareLayerConfig::GetDefault() {
265 std::string group =
266 webrtc::field_trial::FindFullName(kScreencastLayerFieldTrialName);
267
268 ScreenshareLayerConfig config(kScreenshareDefaultTl0BitrateKbps,
269 kScreenshareDefaultTl1BitrateKbps);
270 if (!group.empty() && !FromFieldTrialGroup(group, &config)) {
271 LOG(LS_WARNING) << "Unable to parse WebRTC-ScreenshareLayerRates"
272 " field trial group: '" << group << "'.";
273 }
274 return config;
275}
276
277bool ScreenshareLayerConfig::FromFieldTrialGroup(
278 const std::string& group,
279 ScreenshareLayerConfig* config) {
280 // Parse field trial group name, containing bitrates for tl0 and tl1.
281 int tl0_bitrate;
282 int tl1_bitrate;
283 if (sscanf(group.c_str(), "%d-%d", &tl0_bitrate, &tl1_bitrate) != 2) {
284 return false;
285 }
286
287 // Sanity check.
288 if (tl0_bitrate < kScreenshareMinBitrateKbps ||
289 tl0_bitrate > kScreenshareMaxBitrateKbps ||
290 tl1_bitrate < kScreenshareMinBitrateKbps ||
291 tl1_bitrate > kScreenshareMaxBitrateKbps || tl0_bitrate > tl1_bitrate) {
292 return false;
293 }
294
295 config->tl0_bitrate_kbps = tl0_bitrate;
296 config->tl1_bitrate_kbps = tl1_bitrate;
297
298 return true;
299}
300
sprang429600d2017-01-26 06:12:26 -0800301bool UseSimulcastScreenshare() {
sprangc1b57a12017-02-28 08:50:47 -0800302 return webrtc::field_trial::IsEnabled(kSimulcastScreenshareFieldTrialName);
sprang429600d2017-01-26 06:12:26 -0800303}
304
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000305} // namespace cricket