buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 1 | /* |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame] | 2 | * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 3 | * |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
sprang@webrtc.org | 46d4d29 | 2014-12-23 15:19:35 +0000 | [diff] [blame] | 11 | #include <stdio.h> |
| 12 | |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 13 | #include "webrtc/base/arraysize.h" |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 14 | #include "webrtc/base/common.h" |
| 15 | #include "webrtc/base/logging.h" |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 16 | #include "webrtc/media/base/streamparams.h" |
kjellander@webrtc.org | 5ad1297 | 2016-02-12 06:39:40 +0100 | [diff] [blame^] | 17 | #include "webrtc/media/engine/simulcast.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 18 | #include "webrtc/system_wrappers/include/field_trial.h" |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 19 | |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 20 | namespace cricket { |
| 21 | |
| 22 | struct SimulcastFormat { |
| 23 | int width; |
| 24 | int height; |
| 25 | // The maximum number of simulcast layers can be used for |
| 26 | // resolutions at |widthxheigh|. |
| 27 | size_t max_layers; |
| 28 | // The maximum bitrate for encoding stream at |widthxheight|, when we are |
| 29 | // not sending the next higher spatial stream. |
pbos | be16f79 | 2015-10-16 12:49:39 -0700 | [diff] [blame] | 30 | int max_bitrate_kbps; |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 31 | // The target bitrate for encoding stream at |widthxheight|, when this layer |
| 32 | // is not the highest layer (i.e., when we are sending another higher spatial |
| 33 | // stream). |
pbos | be16f79 | 2015-10-16 12:49:39 -0700 | [diff] [blame] | 34 | int target_bitrate_kbps; |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 35 | // The minimum bitrate needed for encoding stream at |widthxheight|. |
pbos | be16f79 | 2015-10-16 12:49:39 -0700 | [diff] [blame] | 36 | int min_bitrate_kbps; |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | // These tables describe from which resolution we can use how many |
| 40 | // simulcast layers at what bitrates (maximum, target, and minimum). |
| 41 | // Important!! Keep this table from high resolution to low resolution. |
| 42 | const SimulcastFormat kSimulcastFormats[] = { |
pbos | be16f79 | 2015-10-16 12:49:39 -0700 | [diff] [blame] | 43 | {1920, 1080, 3, 5000, 4000, 800}, |
| 44 | {1280, 720, 3, 2500, 2500, 600}, |
| 45 | {960, 540, 3, 900, 900, 450}, |
| 46 | {640, 360, 2, 700, 500, 150}, |
| 47 | {480, 270, 2, 450, 350, 150}, |
| 48 | {320, 180, 1, 200, 150, 30}, |
| 49 | {0, 0, 1, 200, 150, 30} |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | // Multiway: Number of temporal layers for each simulcast stream, for maximum |
| 53 | // possible number of simulcast streams |kMaxSimulcastStreams|. The array |
| 54 | // goes from lowest resolution at position 0 to highest resolution. |
| 55 | // For example, first three elements correspond to say: QVGA, VGA, WHD. |
| 56 | static const int |
| 57 | kDefaultConferenceNumberOfTemporalLayers[webrtc::kMaxSimulcastStreams] = |
| 58 | {3, 3, 3, 3}; |
| 59 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 60 | void GetSimulcastSsrcs(const StreamParams& sp, std::vector<uint32_t>* ssrcs) { |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 61 | const SsrcGroup* sim_group = sp.get_ssrc_group(kSimSsrcGroupSemantics); |
| 62 | if (sim_group) { |
| 63 | ssrcs->insert( |
| 64 | ssrcs->end(), sim_group->ssrcs.begin(), sim_group->ssrcs.end()); |
| 65 | } |
| 66 | } |
| 67 | |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 68 | void MaybeExchangeWidthHeight(int* width, int* height) { |
| 69 | // |kSimulcastFormats| assumes |width| >= |height|. If not, exchange them |
| 70 | // before comparing. |
| 71 | if (*width < *height) { |
| 72 | int temp = *width; |
| 73 | *width = *height; |
| 74 | *height = temp; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | int FindSimulcastFormatIndex(int width, int height) { |
| 79 | MaybeExchangeWidthHeight(&width, &height); |
| 80 | |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 81 | for (uint32_t i = 0; i < arraysize(kSimulcastFormats); ++i) { |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 82 | if (width >= kSimulcastFormats[i].width && |
| 83 | height >= kSimulcastFormats[i].height) { |
| 84 | return i; |
| 85 | } |
| 86 | } |
| 87 | return -1; |
| 88 | } |
| 89 | |
| 90 | int FindSimulcastFormatIndex(int width, int height, size_t max_layers) { |
| 91 | MaybeExchangeWidthHeight(&width, &height); |
| 92 | |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 93 | for (uint32_t i = 0; i < arraysize(kSimulcastFormats); ++i) { |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 94 | if (width >= kSimulcastFormats[i].width && |
| 95 | height >= kSimulcastFormats[i].height && |
| 96 | max_layers == kSimulcastFormats[i].max_layers) { |
| 97 | return i; |
| 98 | } |
| 99 | } |
| 100 | return -1; |
| 101 | } |
| 102 | |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 103 | // Simulcast stream width and height must both be dividable by |
| 104 | // |2 ^ simulcast_layers - 1|. |
| 105 | int NormalizeSimulcastSize(int size, size_t simulcast_layers) { |
| 106 | const int base2_exponent = static_cast<int>(simulcast_layers) - 1; |
| 107 | return ((size >> base2_exponent) << base2_exponent); |
| 108 | } |
| 109 | |
| 110 | size_t FindSimulcastMaxLayers(int width, int height) { |
| 111 | int index = FindSimulcastFormatIndex(width, height); |
| 112 | if (index == -1) { |
| 113 | return -1; |
| 114 | } |
| 115 | return kSimulcastFormats[index].max_layers; |
| 116 | } |
| 117 | |
| 118 | // TODO(marpan): Investigate if we should return 0 instead of -1 in |
| 119 | // FindSimulcast[Max/Target/Min]Bitrate functions below, since the |
| 120 | // codec struct max/min/targeBitrates are unsigned. |
pbos | be16f79 | 2015-10-16 12:49:39 -0700 | [diff] [blame] | 121 | int FindSimulcastMaxBitrateBps(int width, int height, size_t max_layers) { |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 122 | const int format_index = FindSimulcastFormatIndex(width, height); |
| 123 | if (format_index == -1) { |
| 124 | return -1; |
| 125 | } |
pbos | be16f79 | 2015-10-16 12:49:39 -0700 | [diff] [blame] | 126 | return kSimulcastFormats[format_index].max_bitrate_kbps * 1000; |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | int FindSimulcastTargetBitrateBps(int width, |
| 130 | int height, |
pbos | be16f79 | 2015-10-16 12:49:39 -0700 | [diff] [blame] | 131 | size_t max_layers) { |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 132 | const int format_index = FindSimulcastFormatIndex(width, height); |
| 133 | if (format_index == -1) { |
| 134 | return -1; |
| 135 | } |
pbos | be16f79 | 2015-10-16 12:49:39 -0700 | [diff] [blame] | 136 | return kSimulcastFormats[format_index].target_bitrate_kbps * 1000; |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 137 | } |
| 138 | |
pbos | be16f79 | 2015-10-16 12:49:39 -0700 | [diff] [blame] | 139 | int FindSimulcastMinBitrateBps(int width, int height, size_t max_layers) { |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 140 | const int format_index = FindSimulcastFormatIndex(width, height); |
| 141 | if (format_index == -1) { |
| 142 | return -1; |
| 143 | } |
pbos | be16f79 | 2015-10-16 12:49:39 -0700 | [diff] [blame] | 144 | return kSimulcastFormats[format_index].min_bitrate_kbps * 1000; |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | bool SlotSimulcastMaxResolution(size_t max_layers, int* width, int* height) { |
| 148 | int index = FindSimulcastFormatIndex(*width, *height, max_layers); |
| 149 | if (index == -1) { |
| 150 | LOG(LS_ERROR) << "SlotSimulcastMaxResolution"; |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | *width = kSimulcastFormats[index].width; |
| 155 | *height = kSimulcastFormats[index].height; |
| 156 | LOG(LS_INFO) << "SlotSimulcastMaxResolution to width:" << *width |
| 157 | << " height:" << *height; |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | int GetTotalMaxBitrateBps(const std::vector<webrtc::VideoStream>& streams) { |
| 162 | int total_max_bitrate_bps = 0; |
| 163 | for (size_t s = 0; s < streams.size() - 1; ++s) { |
| 164 | total_max_bitrate_bps += streams[s].target_bitrate_bps; |
| 165 | } |
| 166 | total_max_bitrate_bps += streams.back().max_bitrate_bps; |
| 167 | return total_max_bitrate_bps; |
| 168 | } |
| 169 | |
| 170 | std::vector<webrtc::VideoStream> GetSimulcastConfig( |
| 171 | size_t max_streams, |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 172 | int width, |
| 173 | int height, |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 174 | int max_bitrate_bps, |
| 175 | int max_qp, |
| 176 | int max_framerate) { |
| 177 | size_t simulcast_layers = FindSimulcastMaxLayers(width, height); |
| 178 | if (simulcast_layers > max_streams) { |
| 179 | // If the number of SSRCs in the group differs from our target |
| 180 | // number of simulcast streams for current resolution, switch down |
| 181 | // to a resolution that matches our number of SSRCs. |
| 182 | if (!SlotSimulcastMaxResolution(max_streams, &width, &height)) { |
| 183 | return std::vector<webrtc::VideoStream>(); |
| 184 | } |
| 185 | simulcast_layers = max_streams; |
| 186 | } |
| 187 | std::vector<webrtc::VideoStream> streams; |
| 188 | streams.resize(simulcast_layers); |
| 189 | |
| 190 | // Format width and height has to be divisible by |2 ^ number_streams - 1|. |
| 191 | width = NormalizeSimulcastSize(width, simulcast_layers); |
| 192 | height = NormalizeSimulcastSize(height, simulcast_layers); |
| 193 | |
| 194 | // Add simulcast sub-streams from lower resolution to higher resolutions. |
| 195 | // Add simulcast streams, from highest resolution (|s| = number_streams -1) |
| 196 | // to lowest resolution at |s| = 0. |
| 197 | for (size_t s = simulcast_layers - 1;; --s) { |
| 198 | streams[s].width = width; |
| 199 | streams[s].height = height; |
| 200 | // TODO(pbos): Fill actual temporal-layer bitrate thresholds. |
| 201 | streams[s].temporal_layer_thresholds_bps.resize( |
| 202 | kDefaultConferenceNumberOfTemporalLayers[s] - 1); |
pbos | be16f79 | 2015-10-16 12:49:39 -0700 | [diff] [blame] | 203 | streams[s].max_bitrate_bps = |
| 204 | FindSimulcastMaxBitrateBps(width, height, simulcast_layers); |
| 205 | streams[s].target_bitrate_bps = |
| 206 | FindSimulcastTargetBitrateBps(width, height, simulcast_layers); |
| 207 | streams[s].min_bitrate_bps = |
| 208 | FindSimulcastMinBitrateBps(width, height, simulcast_layers); |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 209 | streams[s].max_qp = max_qp; |
| 210 | streams[s].max_framerate = max_framerate; |
| 211 | width /= 2; |
| 212 | height /= 2; |
| 213 | if (s == 0) { |
| 214 | break; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | // Spend additional bits to boost the max stream. |
| 219 | int bitrate_left_bps = max_bitrate_bps - GetTotalMaxBitrateBps(streams); |
| 220 | if (bitrate_left_bps > 0) { |
| 221 | streams.back().max_bitrate_bps += bitrate_left_bps; |
| 222 | } |
| 223 | |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 224 | return streams; |
| 225 | } |
| 226 | |
sprang@webrtc.org | 46d4d29 | 2014-12-23 15:19:35 +0000 | [diff] [blame] | 227 | static const int kScreenshareMinBitrateKbps = 50; |
| 228 | static const int kScreenshareMaxBitrateKbps = 6000; |
Erik Språng | 2c4c914 | 2015-06-24 11:24:44 +0200 | [diff] [blame] | 229 | static const int kScreenshareDefaultTl0BitrateKbps = 200; |
sprang@webrtc.org | 46d4d29 | 2014-12-23 15:19:35 +0000 | [diff] [blame] | 230 | static const int kScreenshareDefaultTl1BitrateKbps = 1000; |
| 231 | |
| 232 | static const char* kScreencastLayerFieldTrialName = |
| 233 | "WebRTC-ScreenshareLayerRates"; |
| 234 | |
| 235 | ScreenshareLayerConfig::ScreenshareLayerConfig(int tl0_bitrate, int tl1_bitrate) |
| 236 | : tl0_bitrate_kbps(tl0_bitrate), tl1_bitrate_kbps(tl1_bitrate) { |
| 237 | } |
| 238 | |
| 239 | ScreenshareLayerConfig ScreenshareLayerConfig::GetDefault() { |
| 240 | std::string group = |
| 241 | webrtc::field_trial::FindFullName(kScreencastLayerFieldTrialName); |
| 242 | |
| 243 | ScreenshareLayerConfig config(kScreenshareDefaultTl0BitrateKbps, |
| 244 | kScreenshareDefaultTl1BitrateKbps); |
| 245 | if (!group.empty() && !FromFieldTrialGroup(group, &config)) { |
| 246 | LOG(LS_WARNING) << "Unable to parse WebRTC-ScreenshareLayerRates" |
| 247 | " field trial group: '" << group << "'."; |
| 248 | } |
| 249 | return config; |
| 250 | } |
| 251 | |
| 252 | bool ScreenshareLayerConfig::FromFieldTrialGroup( |
| 253 | const std::string& group, |
| 254 | ScreenshareLayerConfig* config) { |
| 255 | // Parse field trial group name, containing bitrates for tl0 and tl1. |
| 256 | int tl0_bitrate; |
| 257 | int tl1_bitrate; |
| 258 | if (sscanf(group.c_str(), "%d-%d", &tl0_bitrate, &tl1_bitrate) != 2) { |
| 259 | return false; |
| 260 | } |
| 261 | |
| 262 | // Sanity check. |
| 263 | if (tl0_bitrate < kScreenshareMinBitrateKbps || |
| 264 | tl0_bitrate > kScreenshareMaxBitrateKbps || |
| 265 | tl1_bitrate < kScreenshareMinBitrateKbps || |
| 266 | tl1_bitrate > kScreenshareMaxBitrateKbps || tl0_bitrate > tl1_bitrate) { |
| 267 | return false; |
| 268 | } |
| 269 | |
| 270 | config->tl0_bitrate_kbps = tl0_bitrate; |
| 271 | config->tl1_bitrate_kbps = tl1_bitrate; |
| 272 | |
| 273 | return true; |
| 274 | } |
| 275 | |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 276 | } // namespace cricket |