blob: 82191c3c25974a092f374e5494e49fcaba5773ab [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,
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000238 int max_bitrate_bps,
239 int max_qp,
240 int max_framerate) {
241 size_t simulcast_layers = FindSimulcastMaxLayers(width, height);
242 if (simulcast_layers > max_streams) {
243 // If the number of SSRCs in the group differs from our target
244 // number of simulcast streams for current resolution, switch down
245 // to a resolution that matches our number of SSRCs.
246 if (!SlotSimulcastMaxResolution(max_streams, &width, &height)) {
247 return std::vector<webrtc::VideoStream>();
248 }
249 simulcast_layers = max_streams;
250 }
251 std::vector<webrtc::VideoStream> streams;
252 streams.resize(simulcast_layers);
253
254 // Format width and height has to be divisible by |2 ^ number_streams - 1|.
255 width = NormalizeSimulcastSize(width, simulcast_layers);
256 height = NormalizeSimulcastSize(height, simulcast_layers);
257
258 // Add simulcast sub-streams from lower resolution to higher resolutions.
259 // Add simulcast streams, from highest resolution (|s| = number_streams -1)
260 // to lowest resolution at |s| = 0.
261 for (size_t s = simulcast_layers - 1;; --s) {
262 streams[s].width = width;
263 streams[s].height = height;
264 // TODO(pbos): Fill actual temporal-layer bitrate thresholds.
265 streams[s].temporal_layer_thresholds_bps.resize(
266 kDefaultConferenceNumberOfTemporalLayers[s] - 1);
267 streams[s].max_bitrate_bps = FindSimulcastMaxBitrateBps(
268 width, height, simulcast_layers, bitrate_mode);
269 streams[s].target_bitrate_bps = FindSimulcastTargetBitrateBps(
270 width, height, simulcast_layers, bitrate_mode);
271 streams[s].min_bitrate_bps = FindSimulcastMinBitrateBps(
272 width, height, simulcast_layers, bitrate_mode);
273 streams[s].max_qp = max_qp;
274 streams[s].max_framerate = max_framerate;
275 width /= 2;
276 height /= 2;
277 if (s == 0) {
278 break;
279 }
280 }
281
282 // Spend additional bits to boost the max stream.
283 int bitrate_left_bps = max_bitrate_bps - GetTotalMaxBitrateBps(streams);
284 if (bitrate_left_bps > 0) {
285 streams.back().max_bitrate_bps += bitrate_left_bps;
286 }
287
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000288 return streams;
289}
290
291bool ConfigureSimulcastCodec(
292 int number_ssrcs,
293 SimulcastBitrateMode bitrate_mode,
294 webrtc::VideoCodec* codec) {
295 std::vector<webrtc::VideoStream> streams =
296 GetSimulcastConfig(static_cast<size_t>(number_ssrcs),
297 bitrate_mode,
298 static_cast<int>(codec->width),
299 static_cast<int>(codec->height),
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000300 codec->maxBitrate * 1000,
301 codec->qpMax,
302 codec->maxFramerate);
303 // Add simulcast sub-streams from lower resolution to higher resolutions.
304 codec->numberOfSimulcastStreams = static_cast<unsigned int>(streams.size());
305 codec->width = static_cast<unsigned short>(streams.back().width);
306 codec->height = static_cast<unsigned short>(streams.back().height);
307 // When using simulcast, |codec->maxBitrate| is set to the sum of the max
308 // bitrates over all streams. For a given stream |s|, the max bitrate for that
309 // stream is set by |simulcastStream[s].targetBitrate|, if it is not the
310 // highest resolution stream, otherwise it is set by
311 // |simulcastStream[s].maxBitrate|.
312
313 for (size_t s = 0; s < streams.size(); ++s) {
314 codec->simulcastStream[s].width =
315 static_cast<unsigned short>(streams[s].width);
316 codec->simulcastStream[s].height =
317 static_cast<unsigned short>(streams[s].height);
318 codec->simulcastStream[s].numberOfTemporalLayers =
319 static_cast<unsigned int>(
320 streams[s].temporal_layer_thresholds_bps.size() + 1);
321 codec->simulcastStream[s].minBitrate = streams[s].min_bitrate_bps / 1000;
322 codec->simulcastStream[s].targetBitrate =
323 streams[s].target_bitrate_bps / 1000;
324 codec->simulcastStream[s].maxBitrate = streams[s].max_bitrate_bps / 1000;
325 codec->simulcastStream[s].qpMax = streams[s].max_qp;
326 }
327
328 codec->maxBitrate =
329 static_cast<unsigned int>(GetTotalMaxBitrateBps(streams) / 1000);
330
331 codec->codecSpecific.VP8.numberOfTemporalLayers =
332 kDefaultConferenceNumberOfTemporalLayers[0];
333
334 return true;
335}
336
337bool ConfigureSimulcastCodec(
338 const StreamParams& sp,
339 const VideoOptions& options,
340 webrtc::VideoCodec* codec) {
341 std::vector<uint32> ssrcs;
342 GetSimulcastSsrcs(sp, &ssrcs);
343 SimulcastBitrateMode bitrate_mode = GetSimulcastBitrateMode(options);
344 return ConfigureSimulcastCodec(static_cast<int>(ssrcs.size()), bitrate_mode,
345 codec);
346}
347
348void ConfigureSimulcastTemporalLayers(
349 int num_temporal_layers, webrtc::VideoCodec* codec) {
350 for (size_t i = 0; i < codec->numberOfSimulcastStreams; ++i) {
351 codec->simulcastStream[i].numberOfTemporalLayers = num_temporal_layers;
352 }
353}
354
355void DisableSimulcastCodec(webrtc::VideoCodec* codec) {
356 // TODO(hellner): the proper solution is to uncomment the next code line
357 // and remove the lines following it in this condition. This is pending
358 // b/7012070 being fixed.
359 // codec->numberOfSimulcastStreams = 0;
360 // It is possible to set non simulcast without the above line. However,
361 // the max bitrate for every simulcast layer must be set to 0. Further,
362 // there is a sanity check making sure that the aspect ratio is the same
363 // for all simulcast layers. The for-loop makes sure that the sanity check
364 // does not fail.
365 if (codec->numberOfSimulcastStreams > 0) {
366 const int ratio = codec->width / codec->height;
367 for (int i = 0; i < codec->numberOfSimulcastStreams - 1; ++i) {
368 // Min/target bitrate has to be zero not to influence padding
369 // calculations in VideoEngine.
370 codec->simulcastStream[i].minBitrate = 0;
371 codec->simulcastStream[i].targetBitrate = 0;
372 codec->simulcastStream[i].maxBitrate = 0;
373 codec->simulcastStream[i].width =
374 codec->simulcastStream[i].height * ratio;
375 codec->simulcastStream[i].numberOfTemporalLayers = 1;
376 }
377 // The for loop above did not set the bitrate of the highest layer.
378 codec->simulcastStream[codec->numberOfSimulcastStreams - 1]
379 .minBitrate = 0;
380 codec->simulcastStream[codec->numberOfSimulcastStreams - 1]
381 .targetBitrate = 0;
382 codec->simulcastStream[codec->numberOfSimulcastStreams - 1].
383 maxBitrate = 0;
384 // The highest layer has to correspond to the non-simulcast resolution.
385 codec->simulcastStream[codec->numberOfSimulcastStreams - 1].
386 width = codec->width;
387 codec->simulcastStream[codec->numberOfSimulcastStreams - 1].
388 height = codec->height;
389 codec->simulcastStream[codec->numberOfSimulcastStreams - 1].
390 numberOfTemporalLayers = 1;
391 // TODO(hellner): the maxFramerate should also be set here according to
392 // the screencasts framerate. Doing so will break some
393 // unittests.
394 }
395}
396
397void LogSimulcastSubstreams(const webrtc::VideoCodec& codec) {
398 for (size_t i = 0; i < codec.numberOfSimulcastStreams; ++i) {
399 LOG(LS_INFO) << "Simulcast substream " << i << ": "
400 << codec.simulcastStream[i].width << "x"
401 << codec.simulcastStream[i].height << "@"
402 << codec.simulcastStream[i].minBitrate << "-"
403 << codec.simulcastStream[i].maxBitrate << "kbps"
decurtis@webrtc.org8c5ea8a2015-03-11 19:59:27 +0000404 << " with "
405 << static_cast<int>(
406 codec.simulcastStream[i].numberOfTemporalLayers)
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000407 << " temporal layers";
408 }
409}
410
sprang@webrtc.org46d4d292014-12-23 15:19:35 +0000411static const int kScreenshareMinBitrateKbps = 50;
412static const int kScreenshareMaxBitrateKbps = 6000;
413static const int kScreenshareDefaultTl0BitrateKbps = 100;
414static const int kScreenshareDefaultTl1BitrateKbps = 1000;
415
416static const char* kScreencastLayerFieldTrialName =
417 "WebRTC-ScreenshareLayerRates";
418
419ScreenshareLayerConfig::ScreenshareLayerConfig(int tl0_bitrate, int tl1_bitrate)
420 : tl0_bitrate_kbps(tl0_bitrate), tl1_bitrate_kbps(tl1_bitrate) {
421}
422
423ScreenshareLayerConfig ScreenshareLayerConfig::GetDefault() {
424 std::string group =
425 webrtc::field_trial::FindFullName(kScreencastLayerFieldTrialName);
426
427 ScreenshareLayerConfig config(kScreenshareDefaultTl0BitrateKbps,
428 kScreenshareDefaultTl1BitrateKbps);
429 if (!group.empty() && !FromFieldTrialGroup(group, &config)) {
430 LOG(LS_WARNING) << "Unable to parse WebRTC-ScreenshareLayerRates"
431 " field trial group: '" << group << "'.";
432 }
433 return config;
434}
435
436bool ScreenshareLayerConfig::FromFieldTrialGroup(
437 const std::string& group,
438 ScreenshareLayerConfig* config) {
439 // Parse field trial group name, containing bitrates for tl0 and tl1.
440 int tl0_bitrate;
441 int tl1_bitrate;
442 if (sscanf(group.c_str(), "%d-%d", &tl0_bitrate, &tl1_bitrate) != 2) {
443 return false;
444 }
445
446 // Sanity check.
447 if (tl0_bitrate < kScreenshareMinBitrateKbps ||
448 tl0_bitrate > kScreenshareMaxBitrateKbps ||
449 tl1_bitrate < kScreenshareMinBitrateKbps ||
450 tl1_bitrate > kScreenshareMaxBitrateKbps || tl0_bitrate > tl1_bitrate) {
451 return false;
452 }
453
454 config->tl0_bitrate_kbps = tl0_bitrate;
455 config->tl1_bitrate_kbps = tl1_bitrate;
456
457 return true;
458}
459
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000460void ConfigureConferenceModeScreencastCodec(webrtc::VideoCodec* codec) {
461 codec->codecSpecific.VP8.numberOfTemporalLayers = 2;
sprang@webrtc.org46d4d292014-12-23 15:19:35 +0000462 ScreenshareLayerConfig config = ScreenshareLayerConfig::GetDefault();
463
464 // For screenshare in conference mode, tl0 and tl1 bitrates are piggybacked
465 // on the VideoCodec struct as target and max bitrates, respectively.
466 // See eg. webrtc::VP8EncoderImpl::SetRates().
467 codec->targetBitrate = config.tl0_bitrate_kbps;
468 codec->maxBitrate = config.tl1_bitrate_kbps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000469}
470
471} // namespace cricket