blob: a837b7ccb72f305b11ad3465febedc49164f2127 [file] [log] [blame]
buildbot@webrtc.orga8530772014-12-10 09:01:18 +00001/*
2 * libjingle
3 * Copyright 2014 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
sprang@webrtc.org46d4d292014-12-23 15:19:35 +000028#include <stdio.h>
29
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000030#include "talk/media/base/streamparams.h"
31#include "talk/media/webrtc/simulcast.h"
32#include "webrtc/base/common.h"
33#include "webrtc/base/logging.h"
sprang@webrtc.org46d4d292014-12-23 15:19:35 +000034#include "webrtc/system_wrappers/interface/field_trial.h"
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000035namespace cricket {
36
37struct SimulcastFormat {
38 int width;
39 int height;
40 // The maximum number of simulcast layers can be used for
41 // resolutions at |widthxheigh|.
42 size_t max_layers;
43 // The maximum bitrate for encoding stream at |widthxheight|, when we are
44 // not sending the next higher spatial stream.
pbosbe16f792015-10-16 12:49:39 -070045 int max_bitrate_kbps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000046 // The target bitrate for encoding stream at |widthxheight|, when this layer
47 // is not the highest layer (i.e., when we are sending another higher spatial
48 // stream).
pbosbe16f792015-10-16 12:49:39 -070049 int target_bitrate_kbps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000050 // The minimum bitrate needed for encoding stream at |widthxheight|.
pbosbe16f792015-10-16 12:49:39 -070051 int min_bitrate_kbps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000052};
53
54// These tables describe from which resolution we can use how many
55// simulcast layers at what bitrates (maximum, target, and minimum).
56// Important!! Keep this table from high resolution to low resolution.
57const SimulcastFormat kSimulcastFormats[] = {
pbosbe16f792015-10-16 12:49:39 -070058 {1920, 1080, 3, 5000, 4000, 800},
59 {1280, 720, 3, 2500, 2500, 600},
60 {960, 540, 3, 900, 900, 450},
61 {640, 360, 2, 700, 500, 150},
62 {480, 270, 2, 450, 350, 150},
63 {320, 180, 1, 200, 150, 30},
64 {0, 0, 1, 200, 150, 30}
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000065};
66
67// Multiway: Number of temporal layers for each simulcast stream, for maximum
68// possible number of simulcast streams |kMaxSimulcastStreams|. The array
69// goes from lowest resolution at position 0 to highest resolution.
70// For example, first three elements correspond to say: QVGA, VGA, WHD.
71static const int
72 kDefaultConferenceNumberOfTemporalLayers[webrtc::kMaxSimulcastStreams] =
73 {3, 3, 3, 3};
74
Peter Boström0c4e06b2015-10-07 12:23:21 +020075void GetSimulcastSsrcs(const StreamParams& sp, std::vector<uint32_t>* ssrcs) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000076 const SsrcGroup* sim_group = sp.get_ssrc_group(kSimSsrcGroupSemantics);
77 if (sim_group) {
78 ssrcs->insert(
79 ssrcs->end(), sim_group->ssrcs.begin(), sim_group->ssrcs.end());
80 }
81}
82
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000083void MaybeExchangeWidthHeight(int* width, int* height) {
84 // |kSimulcastFormats| assumes |width| >= |height|. If not, exchange them
85 // before comparing.
86 if (*width < *height) {
87 int temp = *width;
88 *width = *height;
89 *height = temp;
90 }
91}
92
93int FindSimulcastFormatIndex(int width, int height) {
94 MaybeExchangeWidthHeight(&width, &height);
95
96 for (int i = 0; i < ARRAY_SIZE(kSimulcastFormats); ++i) {
97 if (width >= kSimulcastFormats[i].width &&
98 height >= kSimulcastFormats[i].height) {
99 return i;
100 }
101 }
102 return -1;
103}
104
105int FindSimulcastFormatIndex(int width, int height, size_t max_layers) {
106 MaybeExchangeWidthHeight(&width, &height);
107
108 for (int i = 0; i < ARRAY_SIZE(kSimulcastFormats); ++i) {
109 if (width >= kSimulcastFormats[i].width &&
110 height >= kSimulcastFormats[i].height &&
111 max_layers == kSimulcastFormats[i].max_layers) {
112 return i;
113 }
114 }
115 return -1;
116}
117
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000118// Simulcast stream width and height must both be dividable by
119// |2 ^ simulcast_layers - 1|.
120int NormalizeSimulcastSize(int size, size_t simulcast_layers) {
121 const int base2_exponent = static_cast<int>(simulcast_layers) - 1;
122 return ((size >> base2_exponent) << base2_exponent);
123}
124
125size_t FindSimulcastMaxLayers(int width, int height) {
126 int index = FindSimulcastFormatIndex(width, height);
127 if (index == -1) {
128 return -1;
129 }
130 return kSimulcastFormats[index].max_layers;
131}
132
133// TODO(marpan): Investigate if we should return 0 instead of -1 in
134// FindSimulcast[Max/Target/Min]Bitrate functions below, since the
135// codec struct max/min/targeBitrates are unsigned.
pbosbe16f792015-10-16 12:49:39 -0700136int FindSimulcastMaxBitrateBps(int width, int height, size_t max_layers) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000137 const int format_index = FindSimulcastFormatIndex(width, height);
138 if (format_index == -1) {
139 return -1;
140 }
pbosbe16f792015-10-16 12:49:39 -0700141 return kSimulcastFormats[format_index].max_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000142}
143
144int FindSimulcastTargetBitrateBps(int width,
145 int height,
pbosbe16f792015-10-16 12:49:39 -0700146 size_t max_layers) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000147 const int format_index = FindSimulcastFormatIndex(width, height);
148 if (format_index == -1) {
149 return -1;
150 }
pbosbe16f792015-10-16 12:49:39 -0700151 return kSimulcastFormats[format_index].target_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000152}
153
pbosbe16f792015-10-16 12:49:39 -0700154int FindSimulcastMinBitrateBps(int width, int height, size_t max_layers) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000155 const int format_index = FindSimulcastFormatIndex(width, height);
156 if (format_index == -1) {
157 return -1;
158 }
pbosbe16f792015-10-16 12:49:39 -0700159 return kSimulcastFormats[format_index].min_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000160}
161
162bool SlotSimulcastMaxResolution(size_t max_layers, int* width, int* height) {
163 int index = FindSimulcastFormatIndex(*width, *height, max_layers);
164 if (index == -1) {
165 LOG(LS_ERROR) << "SlotSimulcastMaxResolution";
166 return false;
167 }
168
169 *width = kSimulcastFormats[index].width;
170 *height = kSimulcastFormats[index].height;
171 LOG(LS_INFO) << "SlotSimulcastMaxResolution to width:" << *width
172 << " height:" << *height;
173 return true;
174}
175
176int GetTotalMaxBitrateBps(const std::vector<webrtc::VideoStream>& streams) {
177 int total_max_bitrate_bps = 0;
178 for (size_t s = 0; s < streams.size() - 1; ++s) {
179 total_max_bitrate_bps += streams[s].target_bitrate_bps;
180 }
181 total_max_bitrate_bps += streams.back().max_bitrate_bps;
182 return total_max_bitrate_bps;
183}
184
185std::vector<webrtc::VideoStream> GetSimulcastConfig(
186 size_t max_streams,
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000187 int width,
188 int height,
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000189 int max_bitrate_bps,
190 int max_qp,
191 int max_framerate) {
192 size_t simulcast_layers = FindSimulcastMaxLayers(width, height);
193 if (simulcast_layers > max_streams) {
194 // If the number of SSRCs in the group differs from our target
195 // number of simulcast streams for current resolution, switch down
196 // to a resolution that matches our number of SSRCs.
197 if (!SlotSimulcastMaxResolution(max_streams, &width, &height)) {
198 return std::vector<webrtc::VideoStream>();
199 }
200 simulcast_layers = max_streams;
201 }
202 std::vector<webrtc::VideoStream> streams;
203 streams.resize(simulcast_layers);
204
205 // Format width and height has to be divisible by |2 ^ number_streams - 1|.
206 width = NormalizeSimulcastSize(width, simulcast_layers);
207 height = NormalizeSimulcastSize(height, simulcast_layers);
208
209 // Add simulcast sub-streams from lower resolution to higher resolutions.
210 // Add simulcast streams, from highest resolution (|s| = number_streams -1)
211 // to lowest resolution at |s| = 0.
212 for (size_t s = simulcast_layers - 1;; --s) {
213 streams[s].width = width;
214 streams[s].height = height;
215 // TODO(pbos): Fill actual temporal-layer bitrate thresholds.
216 streams[s].temporal_layer_thresholds_bps.resize(
217 kDefaultConferenceNumberOfTemporalLayers[s] - 1);
pbosbe16f792015-10-16 12:49:39 -0700218 streams[s].max_bitrate_bps =
219 FindSimulcastMaxBitrateBps(width, height, simulcast_layers);
220 streams[s].target_bitrate_bps =
221 FindSimulcastTargetBitrateBps(width, height, simulcast_layers);
222 streams[s].min_bitrate_bps =
223 FindSimulcastMinBitrateBps(width, height, simulcast_layers);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000224 streams[s].max_qp = max_qp;
225 streams[s].max_framerate = max_framerate;
226 width /= 2;
227 height /= 2;
228 if (s == 0) {
229 break;
230 }
231 }
232
233 // Spend additional bits to boost the max stream.
234 int bitrate_left_bps = max_bitrate_bps - GetTotalMaxBitrateBps(streams);
235 if (bitrate_left_bps > 0) {
236 streams.back().max_bitrate_bps += bitrate_left_bps;
237 }
238
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000239 return streams;
240}
241
sprang@webrtc.org46d4d292014-12-23 15:19:35 +0000242static const int kScreenshareMinBitrateKbps = 50;
243static const int kScreenshareMaxBitrateKbps = 6000;
Erik Språng2c4c9142015-06-24 11:24:44 +0200244static const int kScreenshareDefaultTl0BitrateKbps = 200;
sprang@webrtc.org46d4d292014-12-23 15:19:35 +0000245static const int kScreenshareDefaultTl1BitrateKbps = 1000;
246
247static const char* kScreencastLayerFieldTrialName =
248 "WebRTC-ScreenshareLayerRates";
249
250ScreenshareLayerConfig::ScreenshareLayerConfig(int tl0_bitrate, int tl1_bitrate)
251 : tl0_bitrate_kbps(tl0_bitrate), tl1_bitrate_kbps(tl1_bitrate) {
252}
253
254ScreenshareLayerConfig ScreenshareLayerConfig::GetDefault() {
255 std::string group =
256 webrtc::field_trial::FindFullName(kScreencastLayerFieldTrialName);
257
258 ScreenshareLayerConfig config(kScreenshareDefaultTl0BitrateKbps,
259 kScreenshareDefaultTl1BitrateKbps);
260 if (!group.empty() && !FromFieldTrialGroup(group, &config)) {
261 LOG(LS_WARNING) << "Unable to parse WebRTC-ScreenshareLayerRates"
262 " field trial group: '" << group << "'.";
263 }
264 return config;
265}
266
267bool ScreenshareLayerConfig::FromFieldTrialGroup(
268 const std::string& group,
269 ScreenshareLayerConfig* config) {
270 // Parse field trial group name, containing bitrates for tl0 and tl1.
271 int tl0_bitrate;
272 int tl1_bitrate;
273 if (sscanf(group.c_str(), "%d-%d", &tl0_bitrate, &tl1_bitrate) != 2) {
274 return false;
275 }
276
277 // Sanity check.
278 if (tl0_bitrate < kScreenshareMinBitrateKbps ||
279 tl0_bitrate > kScreenshareMaxBitrateKbps ||
280 tl1_bitrate < kScreenshareMinBitrateKbps ||
281 tl1_bitrate > kScreenshareMaxBitrateKbps || tl0_bitrate > tl1_bitrate) {
282 return false;
283 }
284
285 config->tl0_bitrate_kbps = tl0_bitrate;
286 config->tl1_bitrate_kbps = tl1_bitrate;
287
288 return true;
289}
290
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000291} // namespace cricket