blob: 3f0820fef9e570363725a87bf1246367ab100824 [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/mediachannel.h" // For VideoOptions
31#include "talk/media/base/streamparams.h"
32#include "talk/media/webrtc/simulcast.h"
33#include "webrtc/base/common.h"
34#include "webrtc/base/logging.h"
35#include "webrtc/common_types.h" // For webrtc::VideoCodec
sprang@webrtc.org46d4d292014-12-23 15:19:35 +000036#include "webrtc/system_wrappers/interface/field_trial.h"
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000037namespace cricket {
38
39struct SimulcastFormat {
40 int width;
41 int height;
42 // The maximum number of simulcast layers can be used for
43 // resolutions at |widthxheigh|.
44 size_t max_layers;
45 // The maximum bitrate for encoding stream at |widthxheight|, when we are
46 // not sending the next higher spatial stream.
47 int max_bitrate_kbps[SBM_COUNT];
48 // The target bitrate for encoding stream at |widthxheight|, when this layer
49 // is not the highest layer (i.e., when we are sending another higher spatial
50 // stream).
51 int target_bitrate_kbps[SBM_COUNT];
52 // The minimum bitrate needed for encoding stream at |widthxheight|.
53 int min_bitrate_kbps[SBM_COUNT];
54};
55
56// These tables describe from which resolution we can use how many
57// simulcast layers at what bitrates (maximum, target, and minimum).
58// Important!! Keep this table from high resolution to low resolution.
59const SimulcastFormat kSimulcastFormats[] = {
Niklas Enbom07a4ba52015-03-24 07:47:55 -070060 {1920, 1080, 3, {5000, 5000, 5000}, {4000, 4000, 4000}, {800, 800, 800}},
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000061 {1280, 720, 3, {1200, 1200, 2500}, {1200, 1200, 2500}, {500, 600, 600}},
62 {960, 540, 3, {900, 900, 900}, {900, 900, 900}, {350, 450, 450}},
63 {640, 360, 2, {500, 700, 700}, {500, 500, 500}, {100, 150, 150}},
64 {480, 270, 2, {350, 450, 450}, {350, 350, 350}, {100, 150, 150}},
65 {320, 180, 1, {100, 200, 200}, {100, 150, 150}, {30, 30, 30}},
66 {0, 0, 1, {100, 200, 200}, {100, 150, 150}, {30, 30, 30}}
67};
68
69// Multiway: Number of temporal layers for each simulcast stream, for maximum
70// possible number of simulcast streams |kMaxSimulcastStreams|. The array
71// goes from lowest resolution at position 0 to highest resolution.
72// For example, first three elements correspond to say: QVGA, VGA, WHD.
73static const int
74 kDefaultConferenceNumberOfTemporalLayers[webrtc::kMaxSimulcastStreams] =
75 {3, 3, 3, 3};
76
Peter Boström0c4e06b2015-10-07 12:23:21 +020077void GetSimulcastSsrcs(const StreamParams& sp, std::vector<uint32_t>* ssrcs) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000078 const SsrcGroup* sim_group = sp.get_ssrc_group(kSimSsrcGroupSemantics);
79 if (sim_group) {
80 ssrcs->insert(
81 ssrcs->end(), sim_group->ssrcs.begin(), sim_group->ssrcs.end());
82 }
83}
84
85SimulcastBitrateMode GetSimulcastBitrateMode(
86 const VideoOptions& options) {
87 VideoOptions::HighestBitrate bitrate_mode;
88 if (options.video_highest_bitrate.Get(&bitrate_mode)) {
89 switch (bitrate_mode) {
90 case VideoOptions::HIGH:
91 return SBM_HIGH;
92 case VideoOptions::VERY_HIGH:
93 return SBM_VERY_HIGH;
94 default:
95 break;
96 }
97 }
98 return SBM_NORMAL;
99}
100
101void MaybeExchangeWidthHeight(int* width, int* height) {
102 // |kSimulcastFormats| assumes |width| >= |height|. If not, exchange them
103 // before comparing.
104 if (*width < *height) {
105 int temp = *width;
106 *width = *height;
107 *height = temp;
108 }
109}
110
111int FindSimulcastFormatIndex(int width, int height) {
112 MaybeExchangeWidthHeight(&width, &height);
113
114 for (int i = 0; i < ARRAY_SIZE(kSimulcastFormats); ++i) {
115 if (width >= kSimulcastFormats[i].width &&
116 height >= kSimulcastFormats[i].height) {
117 return i;
118 }
119 }
120 return -1;
121}
122
123int FindSimulcastFormatIndex(int width, int height, size_t max_layers) {
124 MaybeExchangeWidthHeight(&width, &height);
125
126 for (int i = 0; i < ARRAY_SIZE(kSimulcastFormats); ++i) {
127 if (width >= kSimulcastFormats[i].width &&
128 height >= kSimulcastFormats[i].height &&
129 max_layers == kSimulcastFormats[i].max_layers) {
130 return i;
131 }
132 }
133 return -1;
134}
135
136SimulcastBitrateMode FindSimulcastBitrateMode(
137 size_t max_layers,
138 int stream_idx,
139 SimulcastBitrateMode highest_enabled) {
140
141 if (highest_enabled > SBM_NORMAL) {
142 // We want high or very high for all layers if enabled.
143 return highest_enabled;
144 }
145 if (kSimulcastFormats[stream_idx].max_layers == max_layers) {
146 // We want high for the top layer.
147 return SBM_HIGH;
148 }
149 // And normal for everything else.
150 return SBM_NORMAL;
151}
152
153// Simulcast stream width and height must both be dividable by
154// |2 ^ simulcast_layers - 1|.
155int NormalizeSimulcastSize(int size, size_t simulcast_layers) {
156 const int base2_exponent = static_cast<int>(simulcast_layers) - 1;
157 return ((size >> base2_exponent) << base2_exponent);
158}
159
160size_t FindSimulcastMaxLayers(int width, int height) {
161 int index = FindSimulcastFormatIndex(width, height);
162 if (index == -1) {
163 return -1;
164 }
165 return kSimulcastFormats[index].max_layers;
166}
167
168// TODO(marpan): Investigate if we should return 0 instead of -1 in
169// FindSimulcast[Max/Target/Min]Bitrate functions below, since the
170// codec struct max/min/targeBitrates are unsigned.
171int FindSimulcastMaxBitrateBps(int width,
172 int height,
173 size_t max_layers,
174 SimulcastBitrateMode highest_enabled) {
175 const int format_index = FindSimulcastFormatIndex(width, height);
176 if (format_index == -1) {
177 return -1;
178 }
179 const SimulcastBitrateMode bitrate_mode = FindSimulcastBitrateMode(
180 max_layers, format_index, highest_enabled);
181 return kSimulcastFormats[format_index].max_bitrate_kbps[bitrate_mode] * 1000;
182}
183
184int FindSimulcastTargetBitrateBps(int width,
185 int height,
186 size_t max_layers,
187 SimulcastBitrateMode highest_enabled) {
188 const int format_index = FindSimulcastFormatIndex(width, height);
189 if (format_index == -1) {
190 return -1;
191 }
192 const SimulcastBitrateMode bitrate_mode = FindSimulcastBitrateMode(
193 max_layers, format_index, highest_enabled);
194 return kSimulcastFormats[format_index].target_bitrate_kbps[bitrate_mode] *
195 1000;
196}
197
198int FindSimulcastMinBitrateBps(int width,
199 int height,
200 size_t max_layers,
201 SimulcastBitrateMode highest_enabled) {
202 const int format_index = FindSimulcastFormatIndex(width, height);
203 if (format_index == -1) {
204 return -1;
205 }
206 const SimulcastBitrateMode bitrate_mode = FindSimulcastBitrateMode(
207 max_layers, format_index, highest_enabled);
208 return kSimulcastFormats[format_index].min_bitrate_kbps[bitrate_mode] * 1000;
209}
210
211bool SlotSimulcastMaxResolution(size_t max_layers, int* width, int* height) {
212 int index = FindSimulcastFormatIndex(*width, *height, max_layers);
213 if (index == -1) {
214 LOG(LS_ERROR) << "SlotSimulcastMaxResolution";
215 return false;
216 }
217
218 *width = kSimulcastFormats[index].width;
219 *height = kSimulcastFormats[index].height;
220 LOG(LS_INFO) << "SlotSimulcastMaxResolution to width:" << *width
221 << " height:" << *height;
222 return true;
223}
224
225int GetTotalMaxBitrateBps(const std::vector<webrtc::VideoStream>& streams) {
226 int total_max_bitrate_bps = 0;
227 for (size_t s = 0; s < streams.size() - 1; ++s) {
228 total_max_bitrate_bps += streams[s].target_bitrate_bps;
229 }
230 total_max_bitrate_bps += streams.back().max_bitrate_bps;
231 return total_max_bitrate_bps;
232}
233
234std::vector<webrtc::VideoStream> GetSimulcastConfig(
235 size_t max_streams,
236 SimulcastBitrateMode bitrate_mode,
237 int width,
238 int height,
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000239 int max_bitrate_bps,
240 int max_qp,
241 int max_framerate) {
242 size_t simulcast_layers = FindSimulcastMaxLayers(width, height);
243 if (simulcast_layers > max_streams) {
244 // If the number of SSRCs in the group differs from our target
245 // number of simulcast streams for current resolution, switch down
246 // to a resolution that matches our number of SSRCs.
247 if (!SlotSimulcastMaxResolution(max_streams, &width, &height)) {
248 return std::vector<webrtc::VideoStream>();
249 }
250 simulcast_layers = max_streams;
251 }
252 std::vector<webrtc::VideoStream> streams;
253 streams.resize(simulcast_layers);
254
255 // Format width and height has to be divisible by |2 ^ number_streams - 1|.
256 width = NormalizeSimulcastSize(width, simulcast_layers);
257 height = NormalizeSimulcastSize(height, simulcast_layers);
258
259 // Add simulcast sub-streams from lower resolution to higher resolutions.
260 // Add simulcast streams, from highest resolution (|s| = number_streams -1)
261 // to lowest resolution at |s| = 0.
262 for (size_t s = simulcast_layers - 1;; --s) {
263 streams[s].width = width;
264 streams[s].height = height;
265 // TODO(pbos): Fill actual temporal-layer bitrate thresholds.
266 streams[s].temporal_layer_thresholds_bps.resize(
267 kDefaultConferenceNumberOfTemporalLayers[s] - 1);
268 streams[s].max_bitrate_bps = FindSimulcastMaxBitrateBps(
269 width, height, simulcast_layers, bitrate_mode);
270 streams[s].target_bitrate_bps = FindSimulcastTargetBitrateBps(
271 width, height, simulcast_layers, bitrate_mode);
272 streams[s].min_bitrate_bps = FindSimulcastMinBitrateBps(
273 width, height, simulcast_layers, bitrate_mode);
274 streams[s].max_qp = max_qp;
275 streams[s].max_framerate = max_framerate;
276 width /= 2;
277 height /= 2;
278 if (s == 0) {
279 break;
280 }
281 }
282
283 // Spend additional bits to boost the max stream.
284 int bitrate_left_bps = max_bitrate_bps - GetTotalMaxBitrateBps(streams);
285 if (bitrate_left_bps > 0) {
286 streams.back().max_bitrate_bps += bitrate_left_bps;
287 }
288
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000289 return streams;
290}
291
292bool ConfigureSimulcastCodec(
293 int number_ssrcs,
294 SimulcastBitrateMode bitrate_mode,
295 webrtc::VideoCodec* codec) {
296 std::vector<webrtc::VideoStream> streams =
297 GetSimulcastConfig(static_cast<size_t>(number_ssrcs),
298 bitrate_mode,
299 static_cast<int>(codec->width),
300 static_cast<int>(codec->height),
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000301 codec->maxBitrate * 1000,
302 codec->qpMax,
303 codec->maxFramerate);
304 // Add simulcast sub-streams from lower resolution to higher resolutions.
305 codec->numberOfSimulcastStreams = static_cast<unsigned int>(streams.size());
306 codec->width = static_cast<unsigned short>(streams.back().width);
307 codec->height = static_cast<unsigned short>(streams.back().height);
308 // When using simulcast, |codec->maxBitrate| is set to the sum of the max
309 // bitrates over all streams. For a given stream |s|, the max bitrate for that
310 // stream is set by |simulcastStream[s].targetBitrate|, if it is not the
311 // highest resolution stream, otherwise it is set by
312 // |simulcastStream[s].maxBitrate|.
313
314 for (size_t s = 0; s < streams.size(); ++s) {
315 codec->simulcastStream[s].width =
316 static_cast<unsigned short>(streams[s].width);
317 codec->simulcastStream[s].height =
318 static_cast<unsigned short>(streams[s].height);
319 codec->simulcastStream[s].numberOfTemporalLayers =
320 static_cast<unsigned int>(
321 streams[s].temporal_layer_thresholds_bps.size() + 1);
322 codec->simulcastStream[s].minBitrate = streams[s].min_bitrate_bps / 1000;
323 codec->simulcastStream[s].targetBitrate =
324 streams[s].target_bitrate_bps / 1000;
325 codec->simulcastStream[s].maxBitrate = streams[s].max_bitrate_bps / 1000;
326 codec->simulcastStream[s].qpMax = streams[s].max_qp;
327 }
328
329 codec->maxBitrate =
330 static_cast<unsigned int>(GetTotalMaxBitrateBps(streams) / 1000);
331
332 codec->codecSpecific.VP8.numberOfTemporalLayers =
333 kDefaultConferenceNumberOfTemporalLayers[0];
334
335 return true;
336}
337
338bool ConfigureSimulcastCodec(
339 const StreamParams& sp,
340 const VideoOptions& options,
341 webrtc::VideoCodec* codec) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200342 std::vector<uint32_t> ssrcs;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000343 GetSimulcastSsrcs(sp, &ssrcs);
344 SimulcastBitrateMode bitrate_mode = GetSimulcastBitrateMode(options);
345 return ConfigureSimulcastCodec(static_cast<int>(ssrcs.size()), bitrate_mode,
346 codec);
347}
348
349void ConfigureSimulcastTemporalLayers(
350 int num_temporal_layers, webrtc::VideoCodec* codec) {
351 for (size_t i = 0; i < codec->numberOfSimulcastStreams; ++i) {
352 codec->simulcastStream[i].numberOfTemporalLayers = num_temporal_layers;
353 }
354}
355
356void DisableSimulcastCodec(webrtc::VideoCodec* codec) {
357 // TODO(hellner): the proper solution is to uncomment the next code line
358 // and remove the lines following it in this condition. This is pending
359 // b/7012070 being fixed.
360 // codec->numberOfSimulcastStreams = 0;
361 // It is possible to set non simulcast without the above line. However,
362 // the max bitrate for every simulcast layer must be set to 0. Further,
363 // there is a sanity check making sure that the aspect ratio is the same
364 // for all simulcast layers. The for-loop makes sure that the sanity check
365 // does not fail.
366 if (codec->numberOfSimulcastStreams > 0) {
367 const int ratio = codec->width / codec->height;
368 for (int i = 0; i < codec->numberOfSimulcastStreams - 1; ++i) {
369 // Min/target bitrate has to be zero not to influence padding
370 // calculations in VideoEngine.
371 codec->simulcastStream[i].minBitrate = 0;
372 codec->simulcastStream[i].targetBitrate = 0;
373 codec->simulcastStream[i].maxBitrate = 0;
374 codec->simulcastStream[i].width =
375 codec->simulcastStream[i].height * ratio;
376 codec->simulcastStream[i].numberOfTemporalLayers = 1;
377 }
378 // The for loop above did not set the bitrate of the highest layer.
379 codec->simulcastStream[codec->numberOfSimulcastStreams - 1]
380 .minBitrate = 0;
381 codec->simulcastStream[codec->numberOfSimulcastStreams - 1]
382 .targetBitrate = 0;
383 codec->simulcastStream[codec->numberOfSimulcastStreams - 1].
384 maxBitrate = 0;
385 // The highest layer has to correspond to the non-simulcast resolution.
386 codec->simulcastStream[codec->numberOfSimulcastStreams - 1].
387 width = codec->width;
388 codec->simulcastStream[codec->numberOfSimulcastStreams - 1].
389 height = codec->height;
390 codec->simulcastStream[codec->numberOfSimulcastStreams - 1].
391 numberOfTemporalLayers = 1;
392 // TODO(hellner): the maxFramerate should also be set here according to
393 // the screencasts framerate. Doing so will break some
394 // unittests.
395 }
396}
397
398void LogSimulcastSubstreams(const webrtc::VideoCodec& codec) {
399 for (size_t i = 0; i < codec.numberOfSimulcastStreams; ++i) {
400 LOG(LS_INFO) << "Simulcast substream " << i << ": "
401 << codec.simulcastStream[i].width << "x"
402 << codec.simulcastStream[i].height << "@"
403 << codec.simulcastStream[i].minBitrate << "-"
404 << codec.simulcastStream[i].maxBitrate << "kbps"
decurtis@webrtc.org8c5ea8a2015-03-11 19:59:27 +0000405 << " with "
406 << static_cast<int>(
407 codec.simulcastStream[i].numberOfTemporalLayers)
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000408 << " temporal layers";
409 }
410}
411
sprang@webrtc.org46d4d292014-12-23 15:19:35 +0000412static const int kScreenshareMinBitrateKbps = 50;
413static const int kScreenshareMaxBitrateKbps = 6000;
Erik Språng2c4c9142015-06-24 11:24:44 +0200414static const int kScreenshareDefaultTl0BitrateKbps = 200;
sprang@webrtc.org46d4d292014-12-23 15:19:35 +0000415static const int kScreenshareDefaultTl1BitrateKbps = 1000;
416
417static const char* kScreencastLayerFieldTrialName =
418 "WebRTC-ScreenshareLayerRates";
419
420ScreenshareLayerConfig::ScreenshareLayerConfig(int tl0_bitrate, int tl1_bitrate)
421 : tl0_bitrate_kbps(tl0_bitrate), tl1_bitrate_kbps(tl1_bitrate) {
422}
423
424ScreenshareLayerConfig ScreenshareLayerConfig::GetDefault() {
425 std::string group =
426 webrtc::field_trial::FindFullName(kScreencastLayerFieldTrialName);
427
428 ScreenshareLayerConfig config(kScreenshareDefaultTl0BitrateKbps,
429 kScreenshareDefaultTl1BitrateKbps);
430 if (!group.empty() && !FromFieldTrialGroup(group, &config)) {
431 LOG(LS_WARNING) << "Unable to parse WebRTC-ScreenshareLayerRates"
432 " field trial group: '" << group << "'.";
433 }
434 return config;
435}
436
437bool ScreenshareLayerConfig::FromFieldTrialGroup(
438 const std::string& group,
439 ScreenshareLayerConfig* config) {
440 // Parse field trial group name, containing bitrates for tl0 and tl1.
441 int tl0_bitrate;
442 int tl1_bitrate;
443 if (sscanf(group.c_str(), "%d-%d", &tl0_bitrate, &tl1_bitrate) != 2) {
444 return false;
445 }
446
447 // Sanity check.
448 if (tl0_bitrate < kScreenshareMinBitrateKbps ||
449 tl0_bitrate > kScreenshareMaxBitrateKbps ||
450 tl1_bitrate < kScreenshareMinBitrateKbps ||
451 tl1_bitrate > kScreenshareMaxBitrateKbps || tl0_bitrate > tl1_bitrate) {
452 return false;
453 }
454
455 config->tl0_bitrate_kbps = tl0_bitrate;
456 config->tl1_bitrate_kbps = tl1_bitrate;
457
458 return true;
459}
460
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000461void ConfigureConferenceModeScreencastCodec(webrtc::VideoCodec* codec) {
462 codec->codecSpecific.VP8.numberOfTemporalLayers = 2;
Erik Språng143cec12015-04-28 10:01:41 +0200463 codec->codecSpecific.VP8.automaticResizeOn = false;
sprang@webrtc.org46d4d292014-12-23 15:19:35 +0000464 ScreenshareLayerConfig config = ScreenshareLayerConfig::GetDefault();
465
466 // For screenshare in conference mode, tl0 and tl1 bitrates are piggybacked
467 // on the VideoCodec struct as target and max bitrates, respectively.
468 // See eg. webrtc::VP8EncoderImpl::SetRates().
469 codec->targetBitrate = config.tl0_bitrate_kbps;
470 codec->maxBitrate = config.tl1_bitrate_kbps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000471}
472
473} // namespace cricket