blob: cbcf245efdde2ff2be6522518d6fa792b0bb09dc [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/common.h"
15#include "webrtc/base/logging.h"
kjellandera96e2d72016-02-04 23:52:28 -080016#include "webrtc/media/base/streamparams.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
52// Multiway: Number of temporal layers for each simulcast stream, for maximum
53// possible number of simulcast streams |kMaxSimulcastStreams|. The array
54// goes from lowest resolution at position 0 to highest resolution.
55// For example, first three elements correspond to say: QVGA, VGA, WHD.
56static const int
57 kDefaultConferenceNumberOfTemporalLayers[webrtc::kMaxSimulcastStreams] =
58 {3, 3, 3, 3};
59
Peter Boström0c4e06b2015-10-07 12:23:21 +020060void GetSimulcastSsrcs(const StreamParams& sp, std::vector<uint32_t>* ssrcs) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000061 const SsrcGroup* sim_group = sp.get_ssrc_group(kSimSsrcGroupSemantics);
62 if (sim_group) {
63 ssrcs->insert(
64 ssrcs->end(), sim_group->ssrcs.begin(), sim_group->ssrcs.end());
65 }
66}
67
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000068void MaybeExchangeWidthHeight(int* width, int* height) {
69 // |kSimulcastFormats| assumes |width| >= |height|. If not, exchange them
70 // before comparing.
71 if (*width < *height) {
72 int temp = *width;
73 *width = *height;
74 *height = temp;
75 }
76}
77
78int FindSimulcastFormatIndex(int width, int height) {
79 MaybeExchangeWidthHeight(&width, &height);
80
kjellandera96e2d72016-02-04 23:52:28 -080081 for (uint32_t i = 0; i < arraysize(kSimulcastFormats); ++i) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000082 if (width >= kSimulcastFormats[i].width &&
83 height >= kSimulcastFormats[i].height) {
84 return i;
85 }
86 }
87 return -1;
88}
89
90int FindSimulcastFormatIndex(int width, int height, size_t max_layers) {
91 MaybeExchangeWidthHeight(&width, &height);
92
kjellandera96e2d72016-02-04 23:52:28 -080093 for (uint32_t i = 0; i < arraysize(kSimulcastFormats); ++i) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000094 if (width >= kSimulcastFormats[i].width &&
95 height >= kSimulcastFormats[i].height &&
96 max_layers == kSimulcastFormats[i].max_layers) {
97 return i;
98 }
99 }
100 return -1;
101}
102
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000103// Simulcast stream width and height must both be dividable by
104// |2 ^ simulcast_layers - 1|.
105int NormalizeSimulcastSize(int size, size_t simulcast_layers) {
106 const int base2_exponent = static_cast<int>(simulcast_layers) - 1;
107 return ((size >> base2_exponent) << base2_exponent);
108}
109
110size_t FindSimulcastMaxLayers(int width, int height) {
111 int index = FindSimulcastFormatIndex(width, height);
112 if (index == -1) {
113 return -1;
114 }
115 return kSimulcastFormats[index].max_layers;
116}
117
118// TODO(marpan): Investigate if we should return 0 instead of -1 in
119// FindSimulcast[Max/Target/Min]Bitrate functions below, since the
120// codec struct max/min/targeBitrates are unsigned.
pbosbe16f792015-10-16 12:49:39 -0700121int FindSimulcastMaxBitrateBps(int width, int height, size_t max_layers) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000122 const int format_index = FindSimulcastFormatIndex(width, height);
123 if (format_index == -1) {
124 return -1;
125 }
pbosbe16f792015-10-16 12:49:39 -0700126 return kSimulcastFormats[format_index].max_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000127}
128
129int FindSimulcastTargetBitrateBps(int width,
130 int height,
pbosbe16f792015-10-16 12:49:39 -0700131 size_t max_layers) {
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
pbosbe16f792015-10-16 12:49:39 -0700139int FindSimulcastMinBitrateBps(int width, int height, size_t max_layers) {
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
170std::vector<webrtc::VideoStream> GetSimulcastConfig(
171 size_t max_streams,
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000172 int width,
173 int height,
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000174 int max_bitrate_bps,
175 int max_qp,
176 int max_framerate) {
177 size_t simulcast_layers = FindSimulcastMaxLayers(width, height);
178 if (simulcast_layers > max_streams) {
179 // If the number of SSRCs in the group differs from our target
180 // number of simulcast streams for current resolution, switch down
181 // to a resolution that matches our number of SSRCs.
182 if (!SlotSimulcastMaxResolution(max_streams, &width, &height)) {
183 return std::vector<webrtc::VideoStream>();
184 }
185 simulcast_layers = max_streams;
186 }
187 std::vector<webrtc::VideoStream> streams;
188 streams.resize(simulcast_layers);
189
190 // Format width and height has to be divisible by |2 ^ number_streams - 1|.
191 width = NormalizeSimulcastSize(width, simulcast_layers);
192 height = NormalizeSimulcastSize(height, simulcast_layers);
193
194 // Add simulcast sub-streams from lower resolution to higher resolutions.
195 // Add simulcast streams, from highest resolution (|s| = number_streams -1)
196 // to lowest resolution at |s| = 0.
197 for (size_t s = simulcast_layers - 1;; --s) {
198 streams[s].width = width;
199 streams[s].height = height;
200 // TODO(pbos): Fill actual temporal-layer bitrate thresholds.
201 streams[s].temporal_layer_thresholds_bps.resize(
202 kDefaultConferenceNumberOfTemporalLayers[s] - 1);
pbosbe16f792015-10-16 12:49:39 -0700203 streams[s].max_bitrate_bps =
204 FindSimulcastMaxBitrateBps(width, height, simulcast_layers);
205 streams[s].target_bitrate_bps =
206 FindSimulcastTargetBitrateBps(width, height, simulcast_layers);
207 streams[s].min_bitrate_bps =
208 FindSimulcastMinBitrateBps(width, height, simulcast_layers);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000209 streams[s].max_qp = max_qp;
210 streams[s].max_framerate = max_framerate;
211 width /= 2;
212 height /= 2;
213 if (s == 0) {
214 break;
215 }
216 }
217
218 // Spend additional bits to boost the max stream.
219 int bitrate_left_bps = max_bitrate_bps - GetTotalMaxBitrateBps(streams);
220 if (bitrate_left_bps > 0) {
221 streams.back().max_bitrate_bps += bitrate_left_bps;
222 }
223
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000224 return streams;
225}
226
sprang@webrtc.org46d4d292014-12-23 15:19:35 +0000227static const int kScreenshareMinBitrateKbps = 50;
228static const int kScreenshareMaxBitrateKbps = 6000;
Erik Språng2c4c9142015-06-24 11:24:44 +0200229static const int kScreenshareDefaultTl0BitrateKbps = 200;
sprang@webrtc.org46d4d292014-12-23 15:19:35 +0000230static const int kScreenshareDefaultTl1BitrateKbps = 1000;
231
232static const char* kScreencastLayerFieldTrialName =
233 "WebRTC-ScreenshareLayerRates";
234
235ScreenshareLayerConfig::ScreenshareLayerConfig(int tl0_bitrate, int tl1_bitrate)
236 : tl0_bitrate_kbps(tl0_bitrate), tl1_bitrate_kbps(tl1_bitrate) {
237}
238
239ScreenshareLayerConfig ScreenshareLayerConfig::GetDefault() {
240 std::string group =
241 webrtc::field_trial::FindFullName(kScreencastLayerFieldTrialName);
242
243 ScreenshareLayerConfig config(kScreenshareDefaultTl0BitrateKbps,
244 kScreenshareDefaultTl1BitrateKbps);
245 if (!group.empty() && !FromFieldTrialGroup(group, &config)) {
246 LOG(LS_WARNING) << "Unable to parse WebRTC-ScreenshareLayerRates"
247 " field trial group: '" << group << "'.";
248 }
249 return config;
250}
251
252bool ScreenshareLayerConfig::FromFieldTrialGroup(
253 const std::string& group,
254 ScreenshareLayerConfig* config) {
255 // Parse field trial group name, containing bitrates for tl0 and tl1.
256 int tl0_bitrate;
257 int tl1_bitrate;
258 if (sscanf(group.c_str(), "%d-%d", &tl0_bitrate, &tl1_bitrate) != 2) {
259 return false;
260 }
261
262 // Sanity check.
263 if (tl0_bitrate < kScreenshareMinBitrateKbps ||
264 tl0_bitrate > kScreenshareMaxBitrateKbps ||
265 tl1_bitrate < kScreenshareMinBitrateKbps ||
266 tl1_bitrate > kScreenshareMaxBitrateKbps || tl0_bitrate > tl1_bitrate) {
267 return false;
268 }
269
270 config->tl0_bitrate_kbps = tl0_bitrate;
271 config->tl1_bitrate_kbps = tl1_bitrate;
272
273 return true;
274}
275
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000276} // namespace cricket