blob: 289aacad05d6e8a493c199810623000087698c67 [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
28#include "talk/media/base/mediachannel.h" // For VideoOptions
29#include "talk/media/base/streamparams.h"
30#include "talk/media/webrtc/simulcast.h"
31#include "webrtc/base/common.h"
32#include "webrtc/base/logging.h"
33#include "webrtc/common_types.h" // For webrtc::VideoCodec
34
35namespace 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.
45 int max_bitrate_kbps[SBM_COUNT];
46 // 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).
49 int target_bitrate_kbps[SBM_COUNT];
50 // The minimum bitrate needed for encoding stream at |widthxheight|.
51 int min_bitrate_kbps[SBM_COUNT];
52};
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[] = {
58 {1280, 720, 3, {1200, 1200, 2500}, {1200, 1200, 2500}, {500, 600, 600}},
59 {960, 540, 3, {900, 900, 900}, {900, 900, 900}, {350, 450, 450}},
60 {640, 360, 2, {500, 700, 700}, {500, 500, 500}, {100, 150, 150}},
61 {480, 270, 2, {350, 450, 450}, {350, 350, 350}, {100, 150, 150}},
62 {320, 180, 1, {100, 200, 200}, {100, 150, 150}, {30, 30, 30}},
63 {0, 0, 1, {100, 200, 200}, {100, 150, 150}, {30, 30, 30}}
64};
65
66// Multiway: Number of temporal layers for each simulcast stream, for maximum
67// possible number of simulcast streams |kMaxSimulcastStreams|. The array
68// goes from lowest resolution at position 0 to highest resolution.
69// For example, first three elements correspond to say: QVGA, VGA, WHD.
70static const int
71 kDefaultConferenceNumberOfTemporalLayers[webrtc::kMaxSimulcastStreams] =
72 {3, 3, 3, 3};
73
74static const int kScreencastWithTemporalLayerTargetVideoBitrate = 100;
75static const int kScreencastWithTemporalLayerMaxVideoBitrate = 1000;
76
77void GetSimulcastSsrcs(const StreamParams& sp, std::vector<uint32>* ssrcs) {
78 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,
239 int min_bitrate_bps,
240 int max_bitrate_bps,
241 int max_qp,
242 int max_framerate) {
243 size_t simulcast_layers = FindSimulcastMaxLayers(width, height);
244 if (simulcast_layers > max_streams) {
245 // If the number of SSRCs in the group differs from our target
246 // number of simulcast streams for current resolution, switch down
247 // to a resolution that matches our number of SSRCs.
248 if (!SlotSimulcastMaxResolution(max_streams, &width, &height)) {
249 return std::vector<webrtc::VideoStream>();
250 }
251 simulcast_layers = max_streams;
252 }
253 std::vector<webrtc::VideoStream> streams;
254 streams.resize(simulcast_layers);
255
256 // Format width and height has to be divisible by |2 ^ number_streams - 1|.
257 width = NormalizeSimulcastSize(width, simulcast_layers);
258 height = NormalizeSimulcastSize(height, simulcast_layers);
259
260 // Add simulcast sub-streams from lower resolution to higher resolutions.
261 // Add simulcast streams, from highest resolution (|s| = number_streams -1)
262 // to lowest resolution at |s| = 0.
263 for (size_t s = simulcast_layers - 1;; --s) {
264 streams[s].width = width;
265 streams[s].height = height;
266 // TODO(pbos): Fill actual temporal-layer bitrate thresholds.
267 streams[s].temporal_layer_thresholds_bps.resize(
268 kDefaultConferenceNumberOfTemporalLayers[s] - 1);
269 streams[s].max_bitrate_bps = FindSimulcastMaxBitrateBps(
270 width, height, simulcast_layers, bitrate_mode);
271 streams[s].target_bitrate_bps = FindSimulcastTargetBitrateBps(
272 width, height, simulcast_layers, bitrate_mode);
273 streams[s].min_bitrate_bps = FindSimulcastMinBitrateBps(
274 width, height, simulcast_layers, bitrate_mode);
275 streams[s].max_qp = max_qp;
276 streams[s].max_framerate = max_framerate;
277 width /= 2;
278 height /= 2;
279 if (s == 0) {
280 break;
281 }
282 }
283
284 // Spend additional bits to boost the max stream.
285 int bitrate_left_bps = max_bitrate_bps - GetTotalMaxBitrateBps(streams);
286 if (bitrate_left_bps > 0) {
287 streams.back().max_bitrate_bps += bitrate_left_bps;
288 }
289
290 // Make sure the first stream respects the bitrate minimum.
291 if (streams[0].min_bitrate_bps < min_bitrate_bps) {
292 streams[0].min_bitrate_bps = min_bitrate_bps;
293 }
294
295 return streams;
296}
297
298bool ConfigureSimulcastCodec(
299 int number_ssrcs,
300 SimulcastBitrateMode bitrate_mode,
301 webrtc::VideoCodec* codec) {
302 std::vector<webrtc::VideoStream> streams =
303 GetSimulcastConfig(static_cast<size_t>(number_ssrcs),
304 bitrate_mode,
305 static_cast<int>(codec->width),
306 static_cast<int>(codec->height),
307 codec->minBitrate * 1000,
308 codec->maxBitrate * 1000,
309 codec->qpMax,
310 codec->maxFramerate);
311 // Add simulcast sub-streams from lower resolution to higher resolutions.
312 codec->numberOfSimulcastStreams = static_cast<unsigned int>(streams.size());
313 codec->width = static_cast<unsigned short>(streams.back().width);
314 codec->height = static_cast<unsigned short>(streams.back().height);
315 // When using simulcast, |codec->maxBitrate| is set to the sum of the max
316 // bitrates over all streams. For a given stream |s|, the max bitrate for that
317 // stream is set by |simulcastStream[s].targetBitrate|, if it is not the
318 // highest resolution stream, otherwise it is set by
319 // |simulcastStream[s].maxBitrate|.
320
321 for (size_t s = 0; s < streams.size(); ++s) {
322 codec->simulcastStream[s].width =
323 static_cast<unsigned short>(streams[s].width);
324 codec->simulcastStream[s].height =
325 static_cast<unsigned short>(streams[s].height);
326 codec->simulcastStream[s].numberOfTemporalLayers =
327 static_cast<unsigned int>(
328 streams[s].temporal_layer_thresholds_bps.size() + 1);
329 codec->simulcastStream[s].minBitrate = streams[s].min_bitrate_bps / 1000;
330 codec->simulcastStream[s].targetBitrate =
331 streams[s].target_bitrate_bps / 1000;
332 codec->simulcastStream[s].maxBitrate = streams[s].max_bitrate_bps / 1000;
333 codec->simulcastStream[s].qpMax = streams[s].max_qp;
334 }
335
336 codec->maxBitrate =
337 static_cast<unsigned int>(GetTotalMaxBitrateBps(streams) / 1000);
338
339 codec->codecSpecific.VP8.numberOfTemporalLayers =
340 kDefaultConferenceNumberOfTemporalLayers[0];
341
342 return true;
343}
344
345bool ConfigureSimulcastCodec(
346 const StreamParams& sp,
347 const VideoOptions& options,
348 webrtc::VideoCodec* codec) {
349 std::vector<uint32> ssrcs;
350 GetSimulcastSsrcs(sp, &ssrcs);
351 SimulcastBitrateMode bitrate_mode = GetSimulcastBitrateMode(options);
352 return ConfigureSimulcastCodec(static_cast<int>(ssrcs.size()), bitrate_mode,
353 codec);
354}
355
356void ConfigureSimulcastTemporalLayers(
357 int num_temporal_layers, webrtc::VideoCodec* codec) {
358 for (size_t i = 0; i < codec->numberOfSimulcastStreams; ++i) {
359 codec->simulcastStream[i].numberOfTemporalLayers = num_temporal_layers;
360 }
361}
362
363void DisableSimulcastCodec(webrtc::VideoCodec* codec) {
364 // TODO(hellner): the proper solution is to uncomment the next code line
365 // and remove the lines following it in this condition. This is pending
366 // b/7012070 being fixed.
367 // codec->numberOfSimulcastStreams = 0;
368 // It is possible to set non simulcast without the above line. However,
369 // the max bitrate for every simulcast layer must be set to 0. Further,
370 // there is a sanity check making sure that the aspect ratio is the same
371 // for all simulcast layers. The for-loop makes sure that the sanity check
372 // does not fail.
373 if (codec->numberOfSimulcastStreams > 0) {
374 const int ratio = codec->width / codec->height;
375 for (int i = 0; i < codec->numberOfSimulcastStreams - 1; ++i) {
376 // Min/target bitrate has to be zero not to influence padding
377 // calculations in VideoEngine.
378 codec->simulcastStream[i].minBitrate = 0;
379 codec->simulcastStream[i].targetBitrate = 0;
380 codec->simulcastStream[i].maxBitrate = 0;
381 codec->simulcastStream[i].width =
382 codec->simulcastStream[i].height * ratio;
383 codec->simulcastStream[i].numberOfTemporalLayers = 1;
384 }
385 // The for loop above did not set the bitrate of the highest layer.
386 codec->simulcastStream[codec->numberOfSimulcastStreams - 1]
387 .minBitrate = 0;
388 codec->simulcastStream[codec->numberOfSimulcastStreams - 1]
389 .targetBitrate = 0;
390 codec->simulcastStream[codec->numberOfSimulcastStreams - 1].
391 maxBitrate = 0;
392 // The highest layer has to correspond to the non-simulcast resolution.
393 codec->simulcastStream[codec->numberOfSimulcastStreams - 1].
394 width = codec->width;
395 codec->simulcastStream[codec->numberOfSimulcastStreams - 1].
396 height = codec->height;
397 codec->simulcastStream[codec->numberOfSimulcastStreams - 1].
398 numberOfTemporalLayers = 1;
399 // TODO(hellner): the maxFramerate should also be set here according to
400 // the screencasts framerate. Doing so will break some
401 // unittests.
402 }
403}
404
405void LogSimulcastSubstreams(const webrtc::VideoCodec& codec) {
406 for (size_t i = 0; i < codec.numberOfSimulcastStreams; ++i) {
407 LOG(LS_INFO) << "Simulcast substream " << i << ": "
408 << codec.simulcastStream[i].width << "x"
409 << codec.simulcastStream[i].height << "@"
410 << codec.simulcastStream[i].minBitrate << "-"
411 << codec.simulcastStream[i].maxBitrate << "kbps"
412 << " with " << codec.simulcastStream[i].numberOfTemporalLayers
413 << " temporal layers";
414 }
415}
416
417void ConfigureConferenceModeScreencastCodec(webrtc::VideoCodec* codec) {
418 codec->codecSpecific.VP8.numberOfTemporalLayers = 2;
419 codec->maxBitrate = kScreencastWithTemporalLayerMaxVideoBitrate;
420 codec->targetBitrate = kScreencastWithTemporalLayerTargetVideoBitrate;
421}
422
423} // namespace cricket