blob: a89ac5265ba7736bba10b192a2b3cdc7e440453e [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"
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +010016#include "webrtc/media/engine/simulcast.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010017#include "webrtc/system_wrappers/include/field_trial.h"
tfarina5237aaf2015-11-10 23:44:30 -080018
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000019namespace cricket {
20
21struct SimulcastFormat {
22 int width;
23 int height;
24 // The maximum number of simulcast layers can be used for
25 // resolutions at |widthxheigh|.
26 size_t max_layers;
27 // The maximum bitrate for encoding stream at |widthxheight|, when we are
28 // not sending the next higher spatial stream.
pbosbe16f792015-10-16 12:49:39 -070029 int max_bitrate_kbps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000030 // The target bitrate for encoding stream at |widthxheight|, when this layer
31 // is not the highest layer (i.e., when we are sending another higher spatial
32 // stream).
pbosbe16f792015-10-16 12:49:39 -070033 int target_bitrate_kbps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000034 // The minimum bitrate needed for encoding stream at |widthxheight|.
pbosbe16f792015-10-16 12:49:39 -070035 int min_bitrate_kbps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000036};
37
38// These tables describe from which resolution we can use how many
39// simulcast layers at what bitrates (maximum, target, and minimum).
40// Important!! Keep this table from high resolution to low resolution.
41const SimulcastFormat kSimulcastFormats[] = {
pbosbe16f792015-10-16 12:49:39 -070042 {1920, 1080, 3, 5000, 4000, 800},
43 {1280, 720, 3, 2500, 2500, 600},
44 {960, 540, 3, 900, 900, 450},
45 {640, 360, 2, 700, 500, 150},
46 {480, 270, 2, 450, 350, 150},
47 {320, 180, 1, 200, 150, 30},
48 {0, 0, 1, 200, 150, 30}
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000049};
50
51// Multiway: Number of temporal layers for each simulcast stream, for maximum
52// possible number of simulcast streams |kMaxSimulcastStreams|. The array
53// goes from lowest resolution at position 0 to highest resolution.
54// For example, first three elements correspond to say: QVGA, VGA, WHD.
55static const int
56 kDefaultConferenceNumberOfTemporalLayers[webrtc::kMaxSimulcastStreams] =
57 {3, 3, 3, 3};
58
Peter Boström0c4e06b2015-10-07 12:23:21 +020059void GetSimulcastSsrcs(const StreamParams& sp, std::vector<uint32_t>* ssrcs) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000060 const SsrcGroup* sim_group = sp.get_ssrc_group(kSimSsrcGroupSemantics);
61 if (sim_group) {
62 ssrcs->insert(
63 ssrcs->end(), sim_group->ssrcs.begin(), sim_group->ssrcs.end());
64 }
65}
66
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000067void MaybeExchangeWidthHeight(int* width, int* height) {
68 // |kSimulcastFormats| assumes |width| >= |height|. If not, exchange them
69 // before comparing.
70 if (*width < *height) {
71 int temp = *width;
72 *width = *height;
73 *height = temp;
74 }
75}
76
77int FindSimulcastFormatIndex(int width, int height) {
78 MaybeExchangeWidthHeight(&width, &height);
79
kjellandera96e2d72016-02-04 23:52:28 -080080 for (uint32_t i = 0; i < arraysize(kSimulcastFormats); ++i) {
sprang44303ea2017-01-18 05:19:13 -080081 if (width >= kSimulcastFormats[i].width &&
82 height >= kSimulcastFormats[i].height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000083 return i;
84 }
85 }
86 return -1;
87}
88
89int FindSimulcastFormatIndex(int width, int height, size_t max_layers) {
90 MaybeExchangeWidthHeight(&width, &height);
91
kjellandera96e2d72016-02-04 23:52:28 -080092 for (uint32_t i = 0; i < arraysize(kSimulcastFormats); ++i) {
sprang44303ea2017-01-18 05:19:13 -080093 if (width >= kSimulcastFormats[i].width &&
94 height >= kSimulcastFormats[i].height &&
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000095 max_layers == kSimulcastFormats[i].max_layers) {
96 return i;
97 }
98 }
99 return -1;
100}
101
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000102// Simulcast stream width and height must both be dividable by
103// |2 ^ simulcast_layers - 1|.
104int NormalizeSimulcastSize(int size, size_t simulcast_layers) {
105 const int base2_exponent = static_cast<int>(simulcast_layers) - 1;
106 return ((size >> base2_exponent) << base2_exponent);
107}
108
109size_t FindSimulcastMaxLayers(int width, int height) {
110 int index = FindSimulcastFormatIndex(width, height);
111 if (index == -1) {
112 return -1;
113 }
114 return kSimulcastFormats[index].max_layers;
115}
116
117// TODO(marpan): Investigate if we should return 0 instead of -1 in
118// FindSimulcast[Max/Target/Min]Bitrate functions below, since the
119// codec struct max/min/targeBitrates are unsigned.
sprang44303ea2017-01-18 05:19:13 -0800120int FindSimulcastMaxBitrateBps(int width, int height, size_t max_layers) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000121 const int format_index = FindSimulcastFormatIndex(width, height);
122 if (format_index == -1) {
123 return -1;
124 }
pbosbe16f792015-10-16 12:49:39 -0700125 return kSimulcastFormats[format_index].max_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000126}
127
sprang44303ea2017-01-18 05:19:13 -0800128int FindSimulcastTargetBitrateBps(int width,
129 int height,
130 size_t max_layers) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000131 const int format_index = FindSimulcastFormatIndex(width, height);
132 if (format_index == -1) {
133 return -1;
134 }
pbosbe16f792015-10-16 12:49:39 -0700135 return kSimulcastFormats[format_index].target_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000136}
137
sprang44303ea2017-01-18 05:19:13 -0800138int FindSimulcastMinBitrateBps(int width, int height, size_t max_layers) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000139 const int format_index = FindSimulcastFormatIndex(width, height);
140 if (format_index == -1) {
141 return -1;
142 }
pbosbe16f792015-10-16 12:49:39 -0700143 return kSimulcastFormats[format_index].min_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000144}
145
146bool SlotSimulcastMaxResolution(size_t max_layers, int* width, int* height) {
147 int index = FindSimulcastFormatIndex(*width, *height, max_layers);
148 if (index == -1) {
149 LOG(LS_ERROR) << "SlotSimulcastMaxResolution";
150 return false;
151 }
152
153 *width = kSimulcastFormats[index].width;
154 *height = kSimulcastFormats[index].height;
155 LOG(LS_INFO) << "SlotSimulcastMaxResolution to width:" << *width
156 << " height:" << *height;
157 return true;
158}
159
160int GetTotalMaxBitrateBps(const std::vector<webrtc::VideoStream>& streams) {
161 int total_max_bitrate_bps = 0;
162 for (size_t s = 0; s < streams.size() - 1; ++s) {
163 total_max_bitrate_bps += streams[s].target_bitrate_bps;
164 }
165 total_max_bitrate_bps += streams.back().max_bitrate_bps;
166 return total_max_bitrate_bps;
167}
168
sprang44303ea2017-01-18 05:19:13 -0800169std::vector<webrtc::VideoStream> GetSimulcastConfig(
170 size_t max_streams,
171 int width,
172 int height,
173 int max_bitrate_bps,
174 int max_qp,
175 int max_framerate) {
176 size_t simulcast_layers = FindSimulcastMaxLayers(width, height);
177 if (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.
181 if (!SlotSimulcastMaxResolution(max_streams, &width, &height)) {
182 return std::vector<webrtc::VideoStream>();
183 }
sprang44303ea2017-01-18 05:19:13 -0800184 simulcast_layers = max_streams;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000185 }
186 std::vector<webrtc::VideoStream> streams;
sprang44303ea2017-01-18 05:19:13 -0800187 streams.resize(simulcast_layers);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000188
sprang44303ea2017-01-18 05:19:13 -0800189 // Format width and height has to be divisible by |2 ^ number_streams - 1|.
190 width = NormalizeSimulcastSize(width, simulcast_layers);
191 height = NormalizeSimulcastSize(height, simulcast_layers);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000192
193 // Add simulcast sub-streams from lower resolution to higher resolutions.
194 // Add simulcast streams, from highest resolution (|s| = number_streams -1)
195 // to lowest resolution at |s| = 0.
sprang44303ea2017-01-18 05:19:13 -0800196 for (size_t s = simulcast_layers - 1;; --s) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000197 streams[s].width = width;
198 streams[s].height = height;
199 // TODO(pbos): Fill actual temporal-layer bitrate thresholds.
sprang44303ea2017-01-18 05:19:13 -0800200 streams[s].temporal_layer_thresholds_bps.resize(
201 kDefaultConferenceNumberOfTemporalLayers[s] - 1);
202 streams[s].max_bitrate_bps =
203 FindSimulcastMaxBitrateBps(width, height, simulcast_layers);
204 streams[s].target_bitrate_bps =
205 FindSimulcastTargetBitrateBps(width, height, simulcast_layers);
206 streams[s].min_bitrate_bps =
207 FindSimulcastMinBitrateBps(width, height, simulcast_layers);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000208 streams[s].max_qp = max_qp;
sprang44303ea2017-01-18 05:19:13 -0800209 streams[s].max_framerate = max_framerate;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000210 width /= 2;
211 height /= 2;
sprang44303ea2017-01-18 05:19:13 -0800212 if (s == 0) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000213 break;
sprang44303ea2017-01-18 05:19:13 -0800214 }
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000215 }
216
217 // Spend additional bits to boost the max stream.
218 int bitrate_left_bps = max_bitrate_bps - GetTotalMaxBitrateBps(streams);
219 if (bitrate_left_bps > 0) {
220 streams.back().max_bitrate_bps += bitrate_left_bps;
221 }
222
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000223 return streams;
224}
225
sprang@webrtc.org46d4d292014-12-23 15:19:35 +0000226static const int kScreenshareMinBitrateKbps = 50;
227static const int kScreenshareMaxBitrateKbps = 6000;
Erik Språng2c4c9142015-06-24 11:24:44 +0200228static const int kScreenshareDefaultTl0BitrateKbps = 200;
sprang@webrtc.org46d4d292014-12-23 15:19:35 +0000229static const int kScreenshareDefaultTl1BitrateKbps = 1000;
230
231static const char* kScreencastLayerFieldTrialName =
232 "WebRTC-ScreenshareLayerRates";
233
234ScreenshareLayerConfig::ScreenshareLayerConfig(int tl0_bitrate, int tl1_bitrate)
235 : tl0_bitrate_kbps(tl0_bitrate), tl1_bitrate_kbps(tl1_bitrate) {
236}
237
238ScreenshareLayerConfig ScreenshareLayerConfig::GetDefault() {
239 std::string group =
240 webrtc::field_trial::FindFullName(kScreencastLayerFieldTrialName);
241
242 ScreenshareLayerConfig config(kScreenshareDefaultTl0BitrateKbps,
243 kScreenshareDefaultTl1BitrateKbps);
244 if (!group.empty() && !FromFieldTrialGroup(group, &config)) {
245 LOG(LS_WARNING) << "Unable to parse WebRTC-ScreenshareLayerRates"
246 " field trial group: '" << group << "'.";
247 }
248 return config;
249}
250
251bool ScreenshareLayerConfig::FromFieldTrialGroup(
252 const std::string& group,
253 ScreenshareLayerConfig* config) {
254 // Parse field trial group name, containing bitrates for tl0 and tl1.
255 int tl0_bitrate;
256 int tl1_bitrate;
257 if (sscanf(group.c_str(), "%d-%d", &tl0_bitrate, &tl1_bitrate) != 2) {
258 return false;
259 }
260
261 // Sanity check.
262 if (tl0_bitrate < kScreenshareMinBitrateKbps ||
263 tl0_bitrate > kScreenshareMaxBitrateKbps ||
264 tl1_bitrate < kScreenshareMinBitrateKbps ||
265 tl1_bitrate > kScreenshareMaxBitrateKbps || tl0_bitrate > tl1_bitrate) {
266 return false;
267 }
268
269 config->tl0_bitrate_kbps = tl0_bitrate;
270 config->tl1_bitrate_kbps = tl1_bitrate;
271
272 return true;
273}
274
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000275} // namespace cricket