OLD | NEW |
1 /* | 1 /* |
2 * libjingle | 2 * libjingle |
3 * Copyright 2014 Google Inc. | 3 * Copyright 2014 Google Inc. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions are met: | 6 * modification, are permitted provided that the following conditions are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright notice, | 8 * 1. Redistributions of source code must retain the above copyright notice, |
9 * this list of conditions and the following disclaimer. | 9 * this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright notice, | 10 * 2. Redistributions in binary form must reproduce the above copyright notice, |
11 * this list of conditions and the following disclaimer in the documentation | 11 * this list of conditions and the following disclaimer in the documentation |
12 * and/or other materials provided with the distribution. | 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 | 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. | 14 * derived from this software without specific prior written permission. |
15 * | 15 * |
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED | 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 | 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO | 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | 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, | 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | 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 | 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 */ | 26 */ |
27 | 27 |
28 #include <stdio.h> | 28 #include <stdio.h> |
29 | 29 |
30 #include "talk/media/base/mediachannel.h" // For VideoOptions | |
31 #include "talk/media/base/streamparams.h" | 30 #include "talk/media/base/streamparams.h" |
32 #include "talk/media/webrtc/simulcast.h" | 31 #include "talk/media/webrtc/simulcast.h" |
33 #include "webrtc/base/common.h" | 32 #include "webrtc/base/common.h" |
34 #include "webrtc/base/logging.h" | 33 #include "webrtc/base/logging.h" |
35 #include "webrtc/common_types.h" // For webrtc::VideoCodec | |
36 #include "webrtc/system_wrappers/interface/field_trial.h" | 34 #include "webrtc/system_wrappers/interface/field_trial.h" |
37 namespace cricket { | 35 namespace cricket { |
38 | 36 |
39 struct SimulcastFormat { | 37 struct SimulcastFormat { |
40 int width; | 38 int width; |
41 int height; | 39 int height; |
42 // The maximum number of simulcast layers can be used for | 40 // The maximum number of simulcast layers can be used for |
43 // resolutions at |widthxheigh|. | 41 // resolutions at |widthxheigh|. |
44 size_t max_layers; | 42 size_t max_layers; |
45 // The maximum bitrate for encoding stream at |widthxheight|, when we are | 43 // The maximum bitrate for encoding stream at |widthxheight|, when we are |
46 // not sending the next higher spatial stream. | 44 // not sending the next higher spatial stream. |
47 int max_bitrate_kbps[SBM_COUNT]; | 45 int max_bitrate_kbps; |
48 // The target bitrate for encoding stream at |widthxheight|, when this layer | 46 // 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 | 47 // is not the highest layer (i.e., when we are sending another higher spatial |
50 // stream). | 48 // stream). |
51 int target_bitrate_kbps[SBM_COUNT]; | 49 int target_bitrate_kbps; |
52 // The minimum bitrate needed for encoding stream at |widthxheight|. | 50 // The minimum bitrate needed for encoding stream at |widthxheight|. |
53 int min_bitrate_kbps[SBM_COUNT]; | 51 int min_bitrate_kbps; |
54 }; | 52 }; |
55 | 53 |
56 // These tables describe from which resolution we can use how many | 54 // These tables describe from which resolution we can use how many |
57 // simulcast layers at what bitrates (maximum, target, and minimum). | 55 // simulcast layers at what bitrates (maximum, target, and minimum). |
58 // Important!! Keep this table from high resolution to low resolution. | 56 // Important!! Keep this table from high resolution to low resolution. |
59 const SimulcastFormat kSimulcastFormats[] = { | 57 const SimulcastFormat kSimulcastFormats[] = { |
60 {1920, 1080, 3, {5000, 5000, 5000}, {4000, 4000, 4000}, {800, 800, 800}}, | 58 {1920, 1080, 3, 5000, 4000, 800}, |
61 {1280, 720, 3, {1200, 1200, 2500}, {1200, 1200, 2500}, {500, 600, 600}}, | 59 {1280, 720, 3, 2500, 2500, 600}, |
62 {960, 540, 3, {900, 900, 900}, {900, 900, 900}, {350, 450, 450}}, | 60 {960, 540, 3, 900, 900, 450}, |
63 {640, 360, 2, {500, 700, 700}, {500, 500, 500}, {100, 150, 150}}, | 61 {640, 360, 2, 700, 500, 150}, |
64 {480, 270, 2, {350, 450, 450}, {350, 350, 350}, {100, 150, 150}}, | 62 {480, 270, 2, 450, 350, 150}, |
65 {320, 180, 1, {100, 200, 200}, {100, 150, 150}, {30, 30, 30}}, | 63 {320, 180, 1, 200, 150, 30}, |
66 {0, 0, 1, {100, 200, 200}, {100, 150, 150}, {30, 30, 30}} | 64 {0, 0, 1, 200, 150, 30} |
67 }; | 65 }; |
68 | 66 |
69 // Multiway: Number of temporal layers for each simulcast stream, for maximum | 67 // Multiway: Number of temporal layers for each simulcast stream, for maximum |
70 // possible number of simulcast streams |kMaxSimulcastStreams|. The array | 68 // possible number of simulcast streams |kMaxSimulcastStreams|. The array |
71 // goes from lowest resolution at position 0 to highest resolution. | 69 // goes from lowest resolution at position 0 to highest resolution. |
72 // For example, first three elements correspond to say: QVGA, VGA, WHD. | 70 // For example, first three elements correspond to say: QVGA, VGA, WHD. |
73 static const int | 71 static const int |
74 kDefaultConferenceNumberOfTemporalLayers[webrtc::kMaxSimulcastStreams] = | 72 kDefaultConferenceNumberOfTemporalLayers[webrtc::kMaxSimulcastStreams] = |
75 {3, 3, 3, 3}; | 73 {3, 3, 3, 3}; |
76 | 74 |
77 void GetSimulcastSsrcs(const StreamParams& sp, std::vector<uint32_t>* ssrcs) { | 75 void GetSimulcastSsrcs(const StreamParams& sp, std::vector<uint32_t>* ssrcs) { |
78 const SsrcGroup* sim_group = sp.get_ssrc_group(kSimSsrcGroupSemantics); | 76 const SsrcGroup* sim_group = sp.get_ssrc_group(kSimSsrcGroupSemantics); |
79 if (sim_group) { | 77 if (sim_group) { |
80 ssrcs->insert( | 78 ssrcs->insert( |
81 ssrcs->end(), sim_group->ssrcs.begin(), sim_group->ssrcs.end()); | 79 ssrcs->end(), sim_group->ssrcs.begin(), sim_group->ssrcs.end()); |
82 } | 80 } |
83 } | 81 } |
84 | 82 |
85 SimulcastBitrateMode 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 | |
101 void MaybeExchangeWidthHeight(int* width, int* height) { | 83 void MaybeExchangeWidthHeight(int* width, int* height) { |
102 // |kSimulcastFormats| assumes |width| >= |height|. If not, exchange them | 84 // |kSimulcastFormats| assumes |width| >= |height|. If not, exchange them |
103 // before comparing. | 85 // before comparing. |
104 if (*width < *height) { | 86 if (*width < *height) { |
105 int temp = *width; | 87 int temp = *width; |
106 *width = *height; | 88 *width = *height; |
107 *height = temp; | 89 *height = temp; |
108 } | 90 } |
109 } | 91 } |
110 | 92 |
(...skipping 15 matching lines...) Expand all Loading... |
126 for (int i = 0; i < ARRAY_SIZE(kSimulcastFormats); ++i) { | 108 for (int i = 0; i < ARRAY_SIZE(kSimulcastFormats); ++i) { |
127 if (width >= kSimulcastFormats[i].width && | 109 if (width >= kSimulcastFormats[i].width && |
128 height >= kSimulcastFormats[i].height && | 110 height >= kSimulcastFormats[i].height && |
129 max_layers == kSimulcastFormats[i].max_layers) { | 111 max_layers == kSimulcastFormats[i].max_layers) { |
130 return i; | 112 return i; |
131 } | 113 } |
132 } | 114 } |
133 return -1; | 115 return -1; |
134 } | 116 } |
135 | 117 |
136 SimulcastBitrateMode 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 | 118 // Simulcast stream width and height must both be dividable by |
154 // |2 ^ simulcast_layers - 1|. | 119 // |2 ^ simulcast_layers - 1|. |
155 int NormalizeSimulcastSize(int size, size_t simulcast_layers) { | 120 int NormalizeSimulcastSize(int size, size_t simulcast_layers) { |
156 const int base2_exponent = static_cast<int>(simulcast_layers) - 1; | 121 const int base2_exponent = static_cast<int>(simulcast_layers) - 1; |
157 return ((size >> base2_exponent) << base2_exponent); | 122 return ((size >> base2_exponent) << base2_exponent); |
158 } | 123 } |
159 | 124 |
160 size_t FindSimulcastMaxLayers(int width, int height) { | 125 size_t FindSimulcastMaxLayers(int width, int height) { |
161 int index = FindSimulcastFormatIndex(width, height); | 126 int index = FindSimulcastFormatIndex(width, height); |
162 if (index == -1) { | 127 if (index == -1) { |
163 return -1; | 128 return -1; |
164 } | 129 } |
165 return kSimulcastFormats[index].max_layers; | 130 return kSimulcastFormats[index].max_layers; |
166 } | 131 } |
167 | 132 |
168 // TODO(marpan): Investigate if we should return 0 instead of -1 in | 133 // TODO(marpan): Investigate if we should return 0 instead of -1 in |
169 // FindSimulcast[Max/Target/Min]Bitrate functions below, since the | 134 // FindSimulcast[Max/Target/Min]Bitrate functions below, since the |
170 // codec struct max/min/targeBitrates are unsigned. | 135 // codec struct max/min/targeBitrates are unsigned. |
171 int FindSimulcastMaxBitrateBps(int width, | 136 int FindSimulcastMaxBitrateBps(int width, int height, size_t max_layers) { |
172 int height, | |
173 size_t max_layers, | |
174 SimulcastBitrateMode highest_enabled) { | |
175 const int format_index = FindSimulcastFormatIndex(width, height); | 137 const int format_index = FindSimulcastFormatIndex(width, height); |
176 if (format_index == -1) { | 138 if (format_index == -1) { |
177 return -1; | 139 return -1; |
178 } | 140 } |
179 const SimulcastBitrateMode bitrate_mode = FindSimulcastBitrateMode( | 141 return kSimulcastFormats[format_index].max_bitrate_kbps * 1000; |
180 max_layers, format_index, highest_enabled); | |
181 return kSimulcastFormats[format_index].max_bitrate_kbps[bitrate_mode] * 1000; | |
182 } | 142 } |
183 | 143 |
184 int FindSimulcastTargetBitrateBps(int width, | 144 int FindSimulcastTargetBitrateBps(int width, |
185 int height, | 145 int height, |
186 size_t max_layers, | 146 size_t max_layers) { |
187 SimulcastBitrateMode highest_enabled) { | |
188 const int format_index = FindSimulcastFormatIndex(width, height); | 147 const int format_index = FindSimulcastFormatIndex(width, height); |
189 if (format_index == -1) { | 148 if (format_index == -1) { |
190 return -1; | 149 return -1; |
191 } | 150 } |
192 const SimulcastBitrateMode bitrate_mode = FindSimulcastBitrateMode( | 151 return kSimulcastFormats[format_index].target_bitrate_kbps * 1000; |
193 max_layers, format_index, highest_enabled); | |
194 return kSimulcastFormats[format_index].target_bitrate_kbps[bitrate_mode] * | |
195 1000; | |
196 } | 152 } |
197 | 153 |
198 int FindSimulcastMinBitrateBps(int width, | 154 int FindSimulcastMinBitrateBps(int width, int height, size_t max_layers) { |
199 int height, | |
200 size_t max_layers, | |
201 SimulcastBitrateMode highest_enabled) { | |
202 const int format_index = FindSimulcastFormatIndex(width, height); | 155 const int format_index = FindSimulcastFormatIndex(width, height); |
203 if (format_index == -1) { | 156 if (format_index == -1) { |
204 return -1; | 157 return -1; |
205 } | 158 } |
206 const SimulcastBitrateMode bitrate_mode = FindSimulcastBitrateMode( | 159 return kSimulcastFormats[format_index].min_bitrate_kbps * 1000; |
207 max_layers, format_index, highest_enabled); | |
208 return kSimulcastFormats[format_index].min_bitrate_kbps[bitrate_mode] * 1000; | |
209 } | 160 } |
210 | 161 |
211 bool SlotSimulcastMaxResolution(size_t max_layers, int* width, int* height) { | 162 bool SlotSimulcastMaxResolution(size_t max_layers, int* width, int* height) { |
212 int index = FindSimulcastFormatIndex(*width, *height, max_layers); | 163 int index = FindSimulcastFormatIndex(*width, *height, max_layers); |
213 if (index == -1) { | 164 if (index == -1) { |
214 LOG(LS_ERROR) << "SlotSimulcastMaxResolution"; | 165 LOG(LS_ERROR) << "SlotSimulcastMaxResolution"; |
215 return false; | 166 return false; |
216 } | 167 } |
217 | 168 |
218 *width = kSimulcastFormats[index].width; | 169 *width = kSimulcastFormats[index].width; |
219 *height = kSimulcastFormats[index].height; | 170 *height = kSimulcastFormats[index].height; |
220 LOG(LS_INFO) << "SlotSimulcastMaxResolution to width:" << *width | 171 LOG(LS_INFO) << "SlotSimulcastMaxResolution to width:" << *width |
221 << " height:" << *height; | 172 << " height:" << *height; |
222 return true; | 173 return true; |
223 } | 174 } |
224 | 175 |
225 int GetTotalMaxBitrateBps(const std::vector<webrtc::VideoStream>& streams) { | 176 int GetTotalMaxBitrateBps(const std::vector<webrtc::VideoStream>& streams) { |
226 int total_max_bitrate_bps = 0; | 177 int total_max_bitrate_bps = 0; |
227 for (size_t s = 0; s < streams.size() - 1; ++s) { | 178 for (size_t s = 0; s < streams.size() - 1; ++s) { |
228 total_max_bitrate_bps += streams[s].target_bitrate_bps; | 179 total_max_bitrate_bps += streams[s].target_bitrate_bps; |
229 } | 180 } |
230 total_max_bitrate_bps += streams.back().max_bitrate_bps; | 181 total_max_bitrate_bps += streams.back().max_bitrate_bps; |
231 return total_max_bitrate_bps; | 182 return total_max_bitrate_bps; |
232 } | 183 } |
233 | 184 |
234 std::vector<webrtc::VideoStream> GetSimulcastConfig( | 185 std::vector<webrtc::VideoStream> GetSimulcastConfig( |
235 size_t max_streams, | 186 size_t max_streams, |
236 SimulcastBitrateMode bitrate_mode, | |
237 int width, | 187 int width, |
238 int height, | 188 int height, |
239 int max_bitrate_bps, | 189 int max_bitrate_bps, |
240 int max_qp, | 190 int max_qp, |
241 int max_framerate) { | 191 int max_framerate) { |
242 size_t simulcast_layers = FindSimulcastMaxLayers(width, height); | 192 size_t simulcast_layers = FindSimulcastMaxLayers(width, height); |
243 if (simulcast_layers > max_streams) { | 193 if (simulcast_layers > max_streams) { |
244 // If the number of SSRCs in the group differs from our target | 194 // If the number of SSRCs in the group differs from our target |
245 // number of simulcast streams for current resolution, switch down | 195 // number of simulcast streams for current resolution, switch down |
246 // to a resolution that matches our number of SSRCs. | 196 // to a resolution that matches our number of SSRCs. |
(...skipping 11 matching lines...) Expand all Loading... |
258 | 208 |
259 // Add simulcast sub-streams from lower resolution to higher resolutions. | 209 // Add simulcast sub-streams from lower resolution to higher resolutions. |
260 // Add simulcast streams, from highest resolution (|s| = number_streams -1) | 210 // Add simulcast streams, from highest resolution (|s| = number_streams -1) |
261 // to lowest resolution at |s| = 0. | 211 // to lowest resolution at |s| = 0. |
262 for (size_t s = simulcast_layers - 1;; --s) { | 212 for (size_t s = simulcast_layers - 1;; --s) { |
263 streams[s].width = width; | 213 streams[s].width = width; |
264 streams[s].height = height; | 214 streams[s].height = height; |
265 // TODO(pbos): Fill actual temporal-layer bitrate thresholds. | 215 // TODO(pbos): Fill actual temporal-layer bitrate thresholds. |
266 streams[s].temporal_layer_thresholds_bps.resize( | 216 streams[s].temporal_layer_thresholds_bps.resize( |
267 kDefaultConferenceNumberOfTemporalLayers[s] - 1); | 217 kDefaultConferenceNumberOfTemporalLayers[s] - 1); |
268 streams[s].max_bitrate_bps = FindSimulcastMaxBitrateBps( | 218 streams[s].max_bitrate_bps = |
269 width, height, simulcast_layers, bitrate_mode); | 219 FindSimulcastMaxBitrateBps(width, height, simulcast_layers); |
270 streams[s].target_bitrate_bps = FindSimulcastTargetBitrateBps( | 220 streams[s].target_bitrate_bps = |
271 width, height, simulcast_layers, bitrate_mode); | 221 FindSimulcastTargetBitrateBps(width, height, simulcast_layers); |
272 streams[s].min_bitrate_bps = FindSimulcastMinBitrateBps( | 222 streams[s].min_bitrate_bps = |
273 width, height, simulcast_layers, bitrate_mode); | 223 FindSimulcastMinBitrateBps(width, height, simulcast_layers); |
274 streams[s].max_qp = max_qp; | 224 streams[s].max_qp = max_qp; |
275 streams[s].max_framerate = max_framerate; | 225 streams[s].max_framerate = max_framerate; |
276 width /= 2; | 226 width /= 2; |
277 height /= 2; | 227 height /= 2; |
278 if (s == 0) { | 228 if (s == 0) { |
279 break; | 229 break; |
280 } | 230 } |
281 } | 231 } |
282 | 232 |
283 // Spend additional bits to boost the max stream. | 233 // Spend additional bits to boost the max stream. |
284 int bitrate_left_bps = max_bitrate_bps - GetTotalMaxBitrateBps(streams); | 234 int bitrate_left_bps = max_bitrate_bps - GetTotalMaxBitrateBps(streams); |
285 if (bitrate_left_bps > 0) { | 235 if (bitrate_left_bps > 0) { |
286 streams.back().max_bitrate_bps += bitrate_left_bps; | 236 streams.back().max_bitrate_bps += bitrate_left_bps; |
287 } | 237 } |
288 | 238 |
289 return streams; | 239 return streams; |
290 } | 240 } |
291 | 241 |
292 bool 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), | |
301 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 | |
338 bool ConfigureSimulcastCodec( | |
339 const StreamParams& sp, | |
340 const VideoOptions& options, | |
341 webrtc::VideoCodec* codec) { | |
342 std::vector<uint32_t> ssrcs; | |
343 GetSimulcastSsrcs(sp, &ssrcs); | |
344 SimulcastBitrateMode bitrate_mode = GetSimulcastBitrateMode(options); | |
345 return ConfigureSimulcastCodec(static_cast<int>(ssrcs.size()), bitrate_mode, | |
346 codec); | |
347 } | |
348 | |
349 void 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 | |
356 void 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 | |
398 void 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" | |
405 << " with " | |
406 << static_cast<int>( | |
407 codec.simulcastStream[i].numberOfTemporalLayers) | |
408 << " temporal layers"; | |
409 } | |
410 } | |
411 | |
412 static const int kScreenshareMinBitrateKbps = 50; | 242 static const int kScreenshareMinBitrateKbps = 50; |
413 static const int kScreenshareMaxBitrateKbps = 6000; | 243 static const int kScreenshareMaxBitrateKbps = 6000; |
414 static const int kScreenshareDefaultTl0BitrateKbps = 200; | 244 static const int kScreenshareDefaultTl0BitrateKbps = 200; |
415 static const int kScreenshareDefaultTl1BitrateKbps = 1000; | 245 static const int kScreenshareDefaultTl1BitrateKbps = 1000; |
416 | 246 |
417 static const char* kScreencastLayerFieldTrialName = | 247 static const char* kScreencastLayerFieldTrialName = |
418 "WebRTC-ScreenshareLayerRates"; | 248 "WebRTC-ScreenshareLayerRates"; |
419 | 249 |
420 ScreenshareLayerConfig::ScreenshareLayerConfig(int tl0_bitrate, int tl1_bitrate) | 250 ScreenshareLayerConfig::ScreenshareLayerConfig(int tl0_bitrate, int tl1_bitrate) |
421 : tl0_bitrate_kbps(tl0_bitrate), tl1_bitrate_kbps(tl1_bitrate) { | 251 : tl0_bitrate_kbps(tl0_bitrate), tl1_bitrate_kbps(tl1_bitrate) { |
(...skipping 29 matching lines...) Expand all Loading... |
451 tl1_bitrate > kScreenshareMaxBitrateKbps || tl0_bitrate > tl1_bitrate) { | 281 tl1_bitrate > kScreenshareMaxBitrateKbps || tl0_bitrate > tl1_bitrate) { |
452 return false; | 282 return false; |
453 } | 283 } |
454 | 284 |
455 config->tl0_bitrate_kbps = tl0_bitrate; | 285 config->tl0_bitrate_kbps = tl0_bitrate; |
456 config->tl1_bitrate_kbps = tl1_bitrate; | 286 config->tl1_bitrate_kbps = tl1_bitrate; |
457 | 287 |
458 return true; | 288 return true; |
459 } | 289 } |
460 | 290 |
461 void ConfigureConferenceModeScreencastCodec(webrtc::VideoCodec* codec) { | |
462 codec->codecSpecific.VP8.numberOfTemporalLayers = 2; | |
463 codec->codecSpecific.VP8.automaticResizeOn = false; | |
464 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; | |
471 } | |
472 | |
473 } // namespace cricket | 291 } // namespace cricket |
OLD | NEW |