blob: 68a9d60aa7b879014d81b6d6db5be1b1a6d1c27e [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[] = {
60 {1280, 720, 3, {1200, 1200, 2500}, {1200, 1200, 2500}, {500, 600, 600}},
61 {960, 540, 3, {900, 900, 900}, {900, 900, 900}, {350, 450, 450}},
62 {640, 360, 2, {500, 700, 700}, {500, 500, 500}, {100, 150, 150}},
63 {480, 270, 2, {350, 450, 450}, {350, 350, 350}, {100, 150, 150}},
64 {320, 180, 1, {100, 200, 200}, {100, 150, 150}, {30, 30, 30}},
65 {0, 0, 1, {100, 200, 200}, {100, 150, 150}, {30, 30, 30}}
66};
67
68// Multiway: Number of temporal layers for each simulcast stream, for maximum
69// possible number of simulcast streams |kMaxSimulcastStreams|. The array
70// goes from lowest resolution at position 0 to highest resolution.
71// For example, first three elements correspond to say: QVGA, VGA, WHD.
72static const int
73 kDefaultConferenceNumberOfTemporalLayers[webrtc::kMaxSimulcastStreams] =
74 {3, 3, 3, 3};
75
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000076void GetSimulcastSsrcs(const StreamParams& sp, std::vector<uint32>* ssrcs) {
77 const SsrcGroup* sim_group = sp.get_ssrc_group(kSimSsrcGroupSemantics);
78 if (sim_group) {
79 ssrcs->insert(
80 ssrcs->end(), sim_group->ssrcs.begin(), sim_group->ssrcs.end());
81 }
82}
83
84SimulcastBitrateMode GetSimulcastBitrateMode(
85 const VideoOptions& options) {
86 VideoOptions::HighestBitrate bitrate_mode;
87 if (options.video_highest_bitrate.Get(&bitrate_mode)) {
88 switch (bitrate_mode) {
89 case VideoOptions::HIGH:
90 return SBM_HIGH;
91 case VideoOptions::VERY_HIGH:
92 return SBM_VERY_HIGH;
93 default:
94 break;
95 }
96 }
97 return SBM_NORMAL;
98}
99
100void MaybeExchangeWidthHeight(int* width, int* height) {
101 // |kSimulcastFormats| assumes |width| >= |height|. If not, exchange them
102 // before comparing.
103 if (*width < *height) {
104 int temp = *width;
105 *width = *height;
106 *height = temp;
107 }
108}
109
110int FindSimulcastFormatIndex(int width, int height) {
111 MaybeExchangeWidthHeight(&width, &height);
112
113 for (int i = 0; i < ARRAY_SIZE(kSimulcastFormats); ++i) {
114 if (width >= kSimulcastFormats[i].width &&
115 height >= kSimulcastFormats[i].height) {
116 return i;
117 }
118 }
119 return -1;
120}
121
122int FindSimulcastFormatIndex(int width, int height, size_t max_layers) {
123 MaybeExchangeWidthHeight(&width, &height);
124
125 for (int i = 0; i < ARRAY_SIZE(kSimulcastFormats); ++i) {
126 if (width >= kSimulcastFormats[i].width &&
127 height >= kSimulcastFormats[i].height &&
128 max_layers == kSimulcastFormats[i].max_layers) {
129 return i;
130 }
131 }
132 return -1;
133}
134
135SimulcastBitrateMode FindSimulcastBitrateMode(
136 size_t max_layers,
137 int stream_idx,
138 SimulcastBitrateMode highest_enabled) {
139
140 if (highest_enabled > SBM_NORMAL) {
141 // We want high or very high for all layers if enabled.
142 return highest_enabled;
143 }
144 if (kSimulcastFormats[stream_idx].max_layers == max_layers) {
145 // We want high for the top layer.
146 return SBM_HIGH;
147 }
148 // And normal for everything else.
149 return SBM_NORMAL;
150}
151
152// Simulcast stream width and height must both be dividable by
153// |2 ^ simulcast_layers - 1|.
154int NormalizeSimulcastSize(int size, size_t simulcast_layers) {
155 const int base2_exponent = static_cast<int>(simulcast_layers) - 1;
156 return ((size >> base2_exponent) << base2_exponent);
157}
158
159size_t FindSimulcastMaxLayers(int width, int height) {
160 int index = FindSimulcastFormatIndex(width, height);
161 if (index == -1) {
162 return -1;
163 }
164 return kSimulcastFormats[index].max_layers;
165}
166
167// TODO(marpan): Investigate if we should return 0 instead of -1 in
168// FindSimulcast[Max/Target/Min]Bitrate functions below, since the
169// codec struct max/min/targeBitrates are unsigned.
170int FindSimulcastMaxBitrateBps(int width,
171 int height,
172 size_t max_layers,
173 SimulcastBitrateMode highest_enabled) {
174 const int format_index = FindSimulcastFormatIndex(width, height);
175 if (format_index == -1) {
176 return -1;
177 }
178 const SimulcastBitrateMode bitrate_mode = FindSimulcastBitrateMode(
179 max_layers, format_index, highest_enabled);
180 return kSimulcastFormats[format_index].max_bitrate_kbps[bitrate_mode] * 1000;
181}
182
183int FindSimulcastTargetBitrateBps(int width,
184 int height,
185 size_t max_layers,
186 SimulcastBitrateMode highest_enabled) {
187 const int format_index = FindSimulcastFormatIndex(width, height);
188 if (format_index == -1) {
189 return -1;
190 }
191 const SimulcastBitrateMode bitrate_mode = FindSimulcastBitrateMode(
192 max_layers, format_index, highest_enabled);
193 return kSimulcastFormats[format_index].target_bitrate_kbps[bitrate_mode] *
194 1000;
195}
196
197int FindSimulcastMinBitrateBps(int width,
198 int height,
199 size_t max_layers,
200 SimulcastBitrateMode highest_enabled) {
201 const int format_index = FindSimulcastFormatIndex(width, height);
202 if (format_index == -1) {
203 return -1;
204 }
205 const SimulcastBitrateMode bitrate_mode = FindSimulcastBitrateMode(
206 max_layers, format_index, highest_enabled);
207 return kSimulcastFormats[format_index].min_bitrate_kbps[bitrate_mode] * 1000;
208}
209
210bool SlotSimulcastMaxResolution(size_t max_layers, int* width, int* height) {
211 int index = FindSimulcastFormatIndex(*width, *height, max_layers);
212 if (index == -1) {
213 LOG(LS_ERROR) << "SlotSimulcastMaxResolution";
214 return false;
215 }
216
217 *width = kSimulcastFormats[index].width;
218 *height = kSimulcastFormats[index].height;
219 LOG(LS_INFO) << "SlotSimulcastMaxResolution to width:" << *width
220 << " height:" << *height;
221 return true;
222}
223
224int GetTotalMaxBitrateBps(const std::vector<webrtc::VideoStream>& streams) {
225 int total_max_bitrate_bps = 0;
226 for (size_t s = 0; s < streams.size() - 1; ++s) {
227 total_max_bitrate_bps += streams[s].target_bitrate_bps;
228 }
229 total_max_bitrate_bps += streams.back().max_bitrate_bps;
230 return total_max_bitrate_bps;
231}
232
233std::vector<webrtc::VideoStream> GetSimulcastConfig(
234 size_t max_streams,
235 SimulcastBitrateMode bitrate_mode,
236 int width,
237 int height,
238 int min_bitrate_bps,
239 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
289 // Make sure the first stream respects the bitrate minimum.
290 if (streams[0].min_bitrate_bps < min_bitrate_bps) {
291 streams[0].min_bitrate_bps = min_bitrate_bps;
292 }
293
294 return streams;
295}
296
297bool ConfigureSimulcastCodec(
298 int number_ssrcs,
299 SimulcastBitrateMode bitrate_mode,
300 webrtc::VideoCodec* codec) {
301 std::vector<webrtc::VideoStream> streams =
302 GetSimulcastConfig(static_cast<size_t>(number_ssrcs),
303 bitrate_mode,
304 static_cast<int>(codec->width),
305 static_cast<int>(codec->height),
306 codec->minBitrate * 1000,
307 codec->maxBitrate * 1000,
308 codec->qpMax,
309 codec->maxFramerate);
310 // Add simulcast sub-streams from lower resolution to higher resolutions.
311 codec->numberOfSimulcastStreams = static_cast<unsigned int>(streams.size());
312 codec->width = static_cast<unsigned short>(streams.back().width);
313 codec->height = static_cast<unsigned short>(streams.back().height);
314 // When using simulcast, |codec->maxBitrate| is set to the sum of the max
315 // bitrates over all streams. For a given stream |s|, the max bitrate for that
316 // stream is set by |simulcastStream[s].targetBitrate|, if it is not the
317 // highest resolution stream, otherwise it is set by
318 // |simulcastStream[s].maxBitrate|.
319
320 for (size_t s = 0; s < streams.size(); ++s) {
321 codec->simulcastStream[s].width =
322 static_cast<unsigned short>(streams[s].width);
323 codec->simulcastStream[s].height =
324 static_cast<unsigned short>(streams[s].height);
325 codec->simulcastStream[s].numberOfTemporalLayers =
326 static_cast<unsigned int>(
327 streams[s].temporal_layer_thresholds_bps.size() + 1);
328 codec->simulcastStream[s].minBitrate = streams[s].min_bitrate_bps / 1000;
329 codec->simulcastStream[s].targetBitrate =
330 streams[s].target_bitrate_bps / 1000;
331 codec->simulcastStream[s].maxBitrate = streams[s].max_bitrate_bps / 1000;
332 codec->simulcastStream[s].qpMax = streams[s].max_qp;
333 }
334
335 codec->maxBitrate =
336 static_cast<unsigned int>(GetTotalMaxBitrateBps(streams) / 1000);
337
338 codec->codecSpecific.VP8.numberOfTemporalLayers =
339 kDefaultConferenceNumberOfTemporalLayers[0];
340
341 return true;
342}
343
344bool ConfigureSimulcastCodec(
345 const StreamParams& sp,
346 const VideoOptions& options,
347 webrtc::VideoCodec* codec) {
348 std::vector<uint32> ssrcs;
349 GetSimulcastSsrcs(sp, &ssrcs);
350 SimulcastBitrateMode bitrate_mode = GetSimulcastBitrateMode(options);
351 return ConfigureSimulcastCodec(static_cast<int>(ssrcs.size()), bitrate_mode,
352 codec);
353}
354
355void ConfigureSimulcastTemporalLayers(
356 int num_temporal_layers, webrtc::VideoCodec* codec) {
357 for (size_t i = 0; i < codec->numberOfSimulcastStreams; ++i) {
358 codec->simulcastStream[i].numberOfTemporalLayers = num_temporal_layers;
359 }
360}
361
362void DisableSimulcastCodec(webrtc::VideoCodec* codec) {
363 // TODO(hellner): the proper solution is to uncomment the next code line
364 // and remove the lines following it in this condition. This is pending
365 // b/7012070 being fixed.
366 // codec->numberOfSimulcastStreams = 0;
367 // It is possible to set non simulcast without the above line. However,
368 // the max bitrate for every simulcast layer must be set to 0. Further,
369 // there is a sanity check making sure that the aspect ratio is the same
370 // for all simulcast layers. The for-loop makes sure that the sanity check
371 // does not fail.
372 if (codec->numberOfSimulcastStreams > 0) {
373 const int ratio = codec->width / codec->height;
374 for (int i = 0; i < codec->numberOfSimulcastStreams - 1; ++i) {
375 // Min/target bitrate has to be zero not to influence padding
376 // calculations in VideoEngine.
377 codec->simulcastStream[i].minBitrate = 0;
378 codec->simulcastStream[i].targetBitrate = 0;
379 codec->simulcastStream[i].maxBitrate = 0;
380 codec->simulcastStream[i].width =
381 codec->simulcastStream[i].height * ratio;
382 codec->simulcastStream[i].numberOfTemporalLayers = 1;
383 }
384 // The for loop above did not set the bitrate of the highest layer.
385 codec->simulcastStream[codec->numberOfSimulcastStreams - 1]
386 .minBitrate = 0;
387 codec->simulcastStream[codec->numberOfSimulcastStreams - 1]
388 .targetBitrate = 0;
389 codec->simulcastStream[codec->numberOfSimulcastStreams - 1].
390 maxBitrate = 0;
391 // The highest layer has to correspond to the non-simulcast resolution.
392 codec->simulcastStream[codec->numberOfSimulcastStreams - 1].
393 width = codec->width;
394 codec->simulcastStream[codec->numberOfSimulcastStreams - 1].
395 height = codec->height;
396 codec->simulcastStream[codec->numberOfSimulcastStreams - 1].
397 numberOfTemporalLayers = 1;
398 // TODO(hellner): the maxFramerate should also be set here according to
399 // the screencasts framerate. Doing so will break some
400 // unittests.
401 }
402}
403
404void LogSimulcastSubstreams(const webrtc::VideoCodec& codec) {
405 for (size_t i = 0; i < codec.numberOfSimulcastStreams; ++i) {
406 LOG(LS_INFO) << "Simulcast substream " << i << ": "
407 << codec.simulcastStream[i].width << "x"
408 << codec.simulcastStream[i].height << "@"
409 << codec.simulcastStream[i].minBitrate << "-"
410 << codec.simulcastStream[i].maxBitrate << "kbps"
411 << " with " << codec.simulcastStream[i].numberOfTemporalLayers
412 << " temporal layers";
413 }
414}
415
sprang@webrtc.org46d4d292014-12-23 15:19:35 +0000416static const int kScreenshareMinBitrateKbps = 50;
417static const int kScreenshareMaxBitrateKbps = 6000;
418static const int kScreenshareDefaultTl0BitrateKbps = 100;
419static const int kScreenshareDefaultTl1BitrateKbps = 1000;
420
421static const char* kScreencastLayerFieldTrialName =
422 "WebRTC-ScreenshareLayerRates";
423
424ScreenshareLayerConfig::ScreenshareLayerConfig(int tl0_bitrate, int tl1_bitrate)
425 : tl0_bitrate_kbps(tl0_bitrate), tl1_bitrate_kbps(tl1_bitrate) {
426}
427
428ScreenshareLayerConfig ScreenshareLayerConfig::GetDefault() {
429 std::string group =
430 webrtc::field_trial::FindFullName(kScreencastLayerFieldTrialName);
431
432 ScreenshareLayerConfig config(kScreenshareDefaultTl0BitrateKbps,
433 kScreenshareDefaultTl1BitrateKbps);
434 if (!group.empty() && !FromFieldTrialGroup(group, &config)) {
435 LOG(LS_WARNING) << "Unable to parse WebRTC-ScreenshareLayerRates"
436 " field trial group: '" << group << "'.";
437 }
438 return config;
439}
440
441bool ScreenshareLayerConfig::FromFieldTrialGroup(
442 const std::string& group,
443 ScreenshareLayerConfig* config) {
444 // Parse field trial group name, containing bitrates for tl0 and tl1.
445 int tl0_bitrate;
446 int tl1_bitrate;
447 if (sscanf(group.c_str(), "%d-%d", &tl0_bitrate, &tl1_bitrate) != 2) {
448 return false;
449 }
450
451 // Sanity check.
452 if (tl0_bitrate < kScreenshareMinBitrateKbps ||
453 tl0_bitrate > kScreenshareMaxBitrateKbps ||
454 tl1_bitrate < kScreenshareMinBitrateKbps ||
455 tl1_bitrate > kScreenshareMaxBitrateKbps || tl0_bitrate > tl1_bitrate) {
456 return false;
457 }
458
459 config->tl0_bitrate_kbps = tl0_bitrate;
460 config->tl1_bitrate_kbps = tl1_bitrate;
461
462 return true;
463}
464
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000465void ConfigureConferenceModeScreencastCodec(webrtc::VideoCodec* codec) {
466 codec->codecSpecific.VP8.numberOfTemporalLayers = 2;
sprang@webrtc.org46d4d292014-12-23 15:19:35 +0000467 ScreenshareLayerConfig config = ScreenshareLayerConfig::GetDefault();
468
469 // For screenshare in conference mode, tl0 and tl1 bitrates are piggybacked
470 // on the VideoCodec struct as target and max bitrates, respectively.
471 // See eg. webrtc::VP8EncoderImpl::SetRates().
472 codec->targetBitrate = config.tl0_bitrate_kbps;
473 codec->maxBitrate = config.tl1_bitrate_kbps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000474}
475
476} // namespace cricket