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 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 11 | #include <stdint.h> |
sprang@webrtc.org | 46d4d29 | 2014-12-23 15:19:35 +0000 | [diff] [blame] | 12 | #include <stdio.h> |
Steve Anton | e78bcb9 | 2017-10-31 09:53:08 -0700 | [diff] [blame] | 13 | #include <algorithm> |
| 14 | #include <string> |
sprang@webrtc.org | 46d4d29 | 2014-12-23 15:19:35 +0000 | [diff] [blame] | 15 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 16 | #include "absl/types/optional.h" |
Erik Språng | f93eda1 | 2019-01-16 17:10:57 +0100 | [diff] [blame] | 17 | #include "api/video/video_codec_constants.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 18 | #include "media/base/media_constants.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "media/engine/constants.h" |
| 20 | #include "media/engine/simulcast.h" |
Sergio Garcia Murillo | 43800f9 | 2018-06-21 16:16:38 +0200 | [diff] [blame] | 21 | #include "modules/video_coding/utility/simulcast_rate_allocator.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "rtc_base/arraysize.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 23 | #include "rtc_base/checks.h" |
Åsa Persson | 1a35fbd | 2018-10-12 17:36:57 +0200 | [diff] [blame] | 24 | #include "rtc_base/experiments/normalize_simulcast_size_experiment.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "rtc_base/logging.h" |
| 26 | #include "system_wrappers/include/field_trial.h" |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 27 | |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 28 | namespace cricket { |
| 29 | |
Rasmus Brandt | 195d1d7 | 2018-05-09 11:28:01 +0200 | [diff] [blame] | 30 | namespace { |
| 31 | |
Rasmus Brandt | 73d117f | 2018-10-02 11:12:52 +0200 | [diff] [blame] | 32 | constexpr char kUseBaseHeavyVP8TL3RateAllocationFieldTrial[] = |
| 33 | "WebRTC-UseBaseHeavyVP8TL3RateAllocation"; |
| 34 | |
Erik Språng | b6b1cac | 2018-08-09 16:12:54 +0200 | [diff] [blame] | 35 | // Limits for legacy conference screensharing mode. Currently used for the |
| 36 | // lower of the two simulcast streams. |
Rasmus Brandt | 195d1d7 | 2018-05-09 11:28:01 +0200 | [diff] [blame] | 37 | constexpr int kScreenshareDefaultTl0BitrateKbps = 200; |
| 38 | constexpr int kScreenshareDefaultTl1BitrateKbps = 1000; |
| 39 | |
Erik Språng | 661a0c6 | 2018-09-11 12:33:21 +0200 | [diff] [blame] | 40 | // Min/max bitrate for the higher one of the two simulcast stream used for |
| 41 | // screen content. |
| 42 | constexpr int kScreenshareHighStreamMinBitrateBps = 600000; |
Erik Språng | 58b2284 | 2018-08-21 13:15:28 +0200 | [diff] [blame] | 43 | constexpr int kScreenshareHighStreamMaxBitrateBps = 1250000; |
Ilya Nikolaevskiy | 3df1d5d | 2018-08-22 09:26:51 +0200 | [diff] [blame] | 44 | static const char* kSimulcastScreenshareFieldTrialName = |
| 45 | "WebRTC-SimulcastScreenshare"; |
Erik Språng | b6b1cac | 2018-08-09 16:12:54 +0200 | [diff] [blame] | 46 | |
Rasmus Brandt | 195d1d7 | 2018-05-09 11:28:01 +0200 | [diff] [blame] | 47 | } // namespace |
| 48 | |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 49 | struct SimulcastFormat { |
| 50 | int width; |
| 51 | int height; |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 52 | // The maximum bitrate for encoding stream at |widthxheight|, when we are |
| 53 | // not sending the next higher spatial stream. |
pbos | be16f79 | 2015-10-16 12:49:39 -0700 | [diff] [blame] | 54 | int max_bitrate_kbps; |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 55 | // The target bitrate for encoding stream at |widthxheight|, when this layer |
| 56 | // is not the highest layer (i.e., when we are sending another higher spatial |
| 57 | // stream). |
pbos | be16f79 | 2015-10-16 12:49:39 -0700 | [diff] [blame] | 58 | int target_bitrate_kbps; |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 59 | // The minimum bitrate needed for encoding stream at |widthxheight|. |
pbos | be16f79 | 2015-10-16 12:49:39 -0700 | [diff] [blame] | 60 | int min_bitrate_kbps; |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | // These tables describe from which resolution we can use how many |
| 64 | // simulcast layers at what bitrates (maximum, target, and minimum). |
| 65 | // Important!! Keep this table from high resolution to low resolution. |
Sergio Garcia Murillo | 43800f9 | 2018-06-21 16:16:38 +0200 | [diff] [blame] | 66 | // clang-format off |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 67 | const SimulcastFormat kSimulcastFormats[] = { |
Florent Castelli | 668ce0c | 2019-07-04 17:06:04 +0200 | [diff] [blame^] | 68 | {1920, 1080, 5000, 4000, 800}, |
| 69 | {1280, 720, 2500, 2500, 600}, |
| 70 | {960, 540, 900, 900, 450}, |
| 71 | {640, 360, 700, 500, 150}, |
| 72 | {480, 270, 450, 350, 150}, |
| 73 | {320, 180, 200, 150, 30}, |
| 74 | {0, 0, 200, 150, 30} |
Sergio Garcia Murillo | 43800f9 | 2018-06-21 16:16:38 +0200 | [diff] [blame] | 75 | }; |
| 76 | // clang-format on |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 77 | |
Seth Hampson | 1370e30 | 2018-02-07 08:50:36 -0800 | [diff] [blame] | 78 | const int kMaxScreenshareSimulcastLayers = 2; |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 79 | |
Erik Språng | bfe3d85 | 2018-05-15 09:54:14 +0200 | [diff] [blame] | 80 | // Multiway: Number of temporal layers for each simulcast stream. |
Erik Språng | 58b2284 | 2018-08-21 13:15:28 +0200 | [diff] [blame] | 81 | int DefaultNumberOfTemporalLayers(int simulcast_id, bool screenshare) { |
Erik Språng | bfe3d85 | 2018-05-15 09:54:14 +0200 | [diff] [blame] | 82 | RTC_CHECK_GE(simulcast_id, 0); |
| 83 | RTC_CHECK_LT(simulcast_id, webrtc::kMaxSimulcastStreams); |
| 84 | |
| 85 | const int kDefaultNumTemporalLayers = 3; |
Erik Språng | 7461eff | 2018-09-10 09:43:56 +0200 | [diff] [blame] | 86 | const int kDefaultNumScreenshareTemporalLayers = 2; |
| 87 | int default_num_temporal_layers = screenshare |
| 88 | ? kDefaultNumScreenshareTemporalLayers |
| 89 | : kDefaultNumTemporalLayers; |
Erik Språng | bfe3d85 | 2018-05-15 09:54:14 +0200 | [diff] [blame] | 90 | |
| 91 | const std::string group_name = |
Erik Språng | 58b2284 | 2018-08-21 13:15:28 +0200 | [diff] [blame] | 92 | screenshare ? webrtc::field_trial::FindFullName( |
| 93 | "WebRTC-VP8ScreenshareTemporalLayers") |
| 94 | : webrtc::field_trial::FindFullName( |
| 95 | "WebRTC-VP8ConferenceTemporalLayers"); |
Erik Språng | bfe3d85 | 2018-05-15 09:54:14 +0200 | [diff] [blame] | 96 | if (group_name.empty()) |
Erik Språng | 7461eff | 2018-09-10 09:43:56 +0200 | [diff] [blame] | 97 | return default_num_temporal_layers; |
Erik Språng | bfe3d85 | 2018-05-15 09:54:14 +0200 | [diff] [blame] | 98 | |
Erik Språng | 7461eff | 2018-09-10 09:43:56 +0200 | [diff] [blame] | 99 | int num_temporal_layers = default_num_temporal_layers; |
Erik Språng | bfe3d85 | 2018-05-15 09:54:14 +0200 | [diff] [blame] | 100 | if (sscanf(group_name.c_str(), "%d", &num_temporal_layers) == 1 && |
| 101 | num_temporal_layers > 0 && |
| 102 | num_temporal_layers <= webrtc::kMaxTemporalStreams) { |
| 103 | return num_temporal_layers; |
| 104 | } |
| 105 | |
| 106 | RTC_LOG(LS_WARNING) << "Attempt to set number of temporal layers to " |
| 107 | "incorrect value: " |
| 108 | << group_name; |
| 109 | |
Erik Språng | 7461eff | 2018-09-10 09:43:56 +0200 | [diff] [blame] | 110 | return default_num_temporal_layers; |
Erik Språng | bfe3d85 | 2018-05-15 09:54:14 +0200 | [diff] [blame] | 111 | } |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 112 | |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 113 | int FindSimulcastFormatIndex(int width, int height) { |
Seth Hampson | 438663e | 2018-01-09 11:14:14 -0800 | [diff] [blame] | 114 | RTC_DCHECK_GE(width, 0); |
| 115 | RTC_DCHECK_GE(height, 0); |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 116 | for (uint32_t i = 0; i < arraysize(kSimulcastFormats); ++i) { |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 117 | if (width * height >= |
| 118 | kSimulcastFormats[i].width * kSimulcastFormats[i].height) { |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 119 | return i; |
| 120 | } |
| 121 | } |
Seth Hampson | 438663e | 2018-01-09 11:14:14 -0800 | [diff] [blame] | 122 | RTC_NOTREACHED(); |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 123 | return -1; |
| 124 | } |
| 125 | |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 126 | // Simulcast stream width and height must both be dividable by |
Rasmus Brandt | efbdcb0 | 2018-04-26 17:47:42 +0200 | [diff] [blame] | 127 | // |2 ^ (simulcast_layers - 1)|. |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 128 | int NormalizeSimulcastSize(int size, size_t simulcast_layers) { |
Åsa Persson | 1a35fbd | 2018-10-12 17:36:57 +0200 | [diff] [blame] | 129 | int base2_exponent = static_cast<int>(simulcast_layers) - 1; |
| 130 | const absl::optional<int> experimental_base2_exponent = |
| 131 | webrtc::NormalizeSimulcastSizeExperiment::GetBase2Exponent(); |
| 132 | if (experimental_base2_exponent && |
| 133 | (size > (1 << *experimental_base2_exponent))) { |
| 134 | base2_exponent = *experimental_base2_exponent; |
| 135 | } |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 136 | return ((size >> base2_exponent) << base2_exponent); |
| 137 | } |
| 138 | |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 139 | int FindSimulcastMaxBitrateBps(int width, int height) { |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 140 | const int format_index = FindSimulcastFormatIndex(width, height); |
pbos | be16f79 | 2015-10-16 12:49:39 -0700 | [diff] [blame] | 141 | return kSimulcastFormats[format_index].max_bitrate_kbps * 1000; |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 142 | } |
| 143 | |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 144 | int FindSimulcastTargetBitrateBps(int width, int height) { |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 145 | const int format_index = FindSimulcastFormatIndex(width, height); |
pbos | be16f79 | 2015-10-16 12:49:39 -0700 | [diff] [blame] | 146 | return kSimulcastFormats[format_index].target_bitrate_kbps * 1000; |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 147 | } |
| 148 | |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 149 | int FindSimulcastMinBitrateBps(int width, int height) { |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 150 | const int format_index = FindSimulcastFormatIndex(width, height); |
pbos | be16f79 | 2015-10-16 12:49:39 -0700 | [diff] [blame] | 151 | return kSimulcastFormats[format_index].min_bitrate_kbps * 1000; |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Seth Hampson | 1370e30 | 2018-02-07 08:50:36 -0800 | [diff] [blame] | 154 | void BoostMaxSimulcastLayer(int max_bitrate_bps, |
| 155 | std::vector<webrtc::VideoStream>* layers) { |
Åsa Persson | 5565981 | 2018-06-18 17:51:32 +0200 | [diff] [blame] | 156 | if (layers->empty()) |
| 157 | return; |
| 158 | |
Seth Hampson | 1370e30 | 2018-02-07 08:50:36 -0800 | [diff] [blame] | 159 | // Spend additional bits to boost the max layer. |
| 160 | int bitrate_left_bps = max_bitrate_bps - GetTotalMaxBitrateBps(*layers); |
| 161 | if (bitrate_left_bps > 0) { |
| 162 | layers->back().max_bitrate_bps += bitrate_left_bps; |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 163 | } |
Seth Hampson | 1370e30 | 2018-02-07 08:50:36 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | int GetTotalMaxBitrateBps(const std::vector<webrtc::VideoStream>& layers) { |
Åsa Persson | 5565981 | 2018-06-18 17:51:32 +0200 | [diff] [blame] | 167 | if (layers.empty()) |
| 168 | return 0; |
| 169 | |
Seth Hampson | 1370e30 | 2018-02-07 08:50:36 -0800 | [diff] [blame] | 170 | int total_max_bitrate_bps = 0; |
| 171 | for (size_t s = 0; s < layers.size() - 1; ++s) { |
| 172 | total_max_bitrate_bps += layers[s].target_bitrate_bps; |
| 173 | } |
| 174 | total_max_bitrate_bps += layers.back().max_bitrate_bps; |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 175 | return total_max_bitrate_bps; |
| 176 | } |
| 177 | |
Sergio Garcia Murillo | 43800f9 | 2018-06-21 16:16:38 +0200 | [diff] [blame] | 178 | std::vector<webrtc::VideoStream> GetSimulcastConfig( |
| 179 | size_t max_layers, |
| 180 | int width, |
| 181 | int height, |
Sergio Garcia Murillo | 43800f9 | 2018-06-21 16:16:38 +0200 | [diff] [blame] | 182 | double bitrate_priority, |
| 183 | int max_qp, |
Sergio Garcia Murillo | 43800f9 | 2018-06-21 16:16:38 +0200 | [diff] [blame] | 184 | bool is_screenshare, |
| 185 | bool temporal_layers_supported) { |
Seth Hampson | 1370e30 | 2018-02-07 08:50:36 -0800 | [diff] [blame] | 186 | if (is_screenshare) { |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 187 | return GetScreenshareLayers(max_layers, width, height, bitrate_priority, |
| 188 | max_qp, ScreenshareSimulcastFieldTrialEnabled(), |
| 189 | temporal_layers_supported); |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 190 | } else { |
Åsa Persson | 5565981 | 2018-06-18 17:51:32 +0200 | [diff] [blame] | 191 | return GetNormalSimulcastLayers(max_layers, width, height, bitrate_priority, |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 192 | max_qp, temporal_layers_supported); |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 193 | } |
Seth Hampson | 1370e30 | 2018-02-07 08:50:36 -0800 | [diff] [blame] | 194 | } |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 195 | |
Seth Hampson | 1370e30 | 2018-02-07 08:50:36 -0800 | [diff] [blame] | 196 | std::vector<webrtc::VideoStream> GetNormalSimulcastLayers( |
Florent Castelli | 668ce0c | 2019-07-04 17:06:04 +0200 | [diff] [blame^] | 197 | size_t layer_count, |
Seth Hampson | 1370e30 | 2018-02-07 08:50:36 -0800 | [diff] [blame] | 198 | int width, |
| 199 | int height, |
Seth Hampson | 1370e30 | 2018-02-07 08:50:36 -0800 | [diff] [blame] | 200 | double bitrate_priority, |
| 201 | int max_qp, |
Sergio Garcia Murillo | 43800f9 | 2018-06-21 16:16:38 +0200 | [diff] [blame] | 202 | bool temporal_layers_supported) { |
Florent Castelli | 668ce0c | 2019-07-04 17:06:04 +0200 | [diff] [blame^] | 203 | std::vector<webrtc::VideoStream> layers(layer_count); |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 204 | |
Seth Hampson | 1370e30 | 2018-02-07 08:50:36 -0800 | [diff] [blame] | 205 | // Format width and height has to be divisible by |2 ^ num_simulcast_layers - |
| 206 | // 1|. |
Florent Castelli | 668ce0c | 2019-07-04 17:06:04 +0200 | [diff] [blame^] | 207 | width = NormalizeSimulcastSize(width, layer_count); |
| 208 | height = NormalizeSimulcastSize(height, layer_count); |
Seth Hampson | 1370e30 | 2018-02-07 08:50:36 -0800 | [diff] [blame] | 209 | // Add simulcast streams, from highest resolution (|s| = num_simulcast_layers |
| 210 | // -1) to lowest resolution at |s| = 0. |
Florent Castelli | 668ce0c | 2019-07-04 17:06:04 +0200 | [diff] [blame^] | 211 | for (size_t s = layer_count - 1;; --s) { |
Seth Hampson | 1370e30 | 2018-02-07 08:50:36 -0800 | [diff] [blame] | 212 | layers[s].width = width; |
| 213 | layers[s].height = height; |
| 214 | // TODO(pbos): Fill actual temporal-layer bitrate thresholds. |
| 215 | layers[s].max_qp = max_qp; |
Sergio Garcia Murillo | 43800f9 | 2018-06-21 16:16:38 +0200 | [diff] [blame] | 216 | layers[s].num_temporal_layers = |
Erik Språng | 58b2284 | 2018-08-21 13:15:28 +0200 | [diff] [blame] | 217 | temporal_layers_supported ? DefaultNumberOfTemporalLayers(s, false) : 0; |
Seth Hampson | 1370e30 | 2018-02-07 08:50:36 -0800 | [diff] [blame] | 218 | layers[s].max_bitrate_bps = FindSimulcastMaxBitrateBps(width, height); |
| 219 | layers[s].target_bitrate_bps = FindSimulcastTargetBitrateBps(width, height); |
Erik Språng | 58b2284 | 2018-08-21 13:15:28 +0200 | [diff] [blame] | 220 | int num_temporal_layers = DefaultNumberOfTemporalLayers(s, false); |
Erik Språng | d497e6b | 2018-07-06 17:17:10 +0200 | [diff] [blame] | 221 | if (s == 0) { |
Rasmus Brandt | 73d117f | 2018-10-02 11:12:52 +0200 | [diff] [blame] | 222 | // If alternative temporal rate allocation is selected, adjust the |
Erik Språng | d92288f | 2018-07-04 10:07:40 +0200 | [diff] [blame] | 223 | // bitrate of the lowest simulcast stream so that absolute bitrate for |
| 224 | // the base temporal layer matches the bitrate for the base temporal |
| 225 | // layer with the default 3 simulcast streams. Otherwise we risk a |
| 226 | // higher threshold for receiving a feed at all. |
Erik Språng | d497e6b | 2018-07-06 17:17:10 +0200 | [diff] [blame] | 227 | float rate_factor = 1.0; |
| 228 | if (num_temporal_layers == 3) { |
Rasmus Brandt | 73d117f | 2018-10-02 11:12:52 +0200 | [diff] [blame] | 229 | if (webrtc::field_trial::IsEnabled( |
| 230 | kUseBaseHeavyVP8TL3RateAllocationFieldTrial)) { |
| 231 | // Base heavy allocation increases TL0 bitrate from 40% to 60%. |
Erik Språng | d497e6b | 2018-07-06 17:17:10 +0200 | [diff] [blame] | 232 | rate_factor = 0.4 / 0.6; |
| 233 | } |
| 234 | } else { |
| 235 | rate_factor = |
| 236 | webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(3, 0) / |
| 237 | webrtc::SimulcastRateAllocator::GetTemporalRateAllocation( |
| 238 | num_temporal_layers, 0); |
| 239 | } |
| 240 | |
Erik Språng | 79478ad | 2018-05-18 09:39:55 +0200 | [diff] [blame] | 241 | layers[s].max_bitrate_bps = |
| 242 | static_cast<int>(layers[s].max_bitrate_bps * rate_factor); |
| 243 | layers[s].target_bitrate_bps = |
| 244 | static_cast<int>(layers[s].target_bitrate_bps * rate_factor); |
| 245 | } |
Seth Hampson | 1370e30 | 2018-02-07 08:50:36 -0800 | [diff] [blame] | 246 | layers[s].min_bitrate_bps = FindSimulcastMinBitrateBps(width, height); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 247 | layers[s].max_framerate = kDefaultVideoMaxFramerate; |
sprang | 02569ad | 2017-07-06 05:05:50 -0700 | [diff] [blame] | 248 | |
Seth Hampson | 1370e30 | 2018-02-07 08:50:36 -0800 | [diff] [blame] | 249 | width /= 2; |
| 250 | height /= 2; |
sprang | 02569ad | 2017-07-06 05:05:50 -0700 | [diff] [blame] | 251 | |
Seth Hampson | 1370e30 | 2018-02-07 08:50:36 -0800 | [diff] [blame] | 252 | if (s == 0) { |
| 253 | break; |
sprang | 02569ad | 2017-07-06 05:05:50 -0700 | [diff] [blame] | 254 | } |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 255 | } |
Seth Hampson | 1370e30 | 2018-02-07 08:50:36 -0800 | [diff] [blame] | 256 | // Currently the relative bitrate priority of the sender is controlled by |
| 257 | // the value of the lowest VideoStream. |
| 258 | // TODO(bugs.webrtc.org/8630): The web specification describes being able to |
| 259 | // control relative bitrate for each individual simulcast layer, but this |
| 260 | // is currently just implemented per rtp sender. |
| 261 | layers[0].bitrate_priority = bitrate_priority; |
| 262 | return layers; |
| 263 | } |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 264 | |
Seth Hampson | 1370e30 | 2018-02-07 08:50:36 -0800 | [diff] [blame] | 265 | std::vector<webrtc::VideoStream> GetScreenshareLayers( |
| 266 | size_t max_layers, |
| 267 | int width, |
| 268 | int height, |
Seth Hampson | 1370e30 | 2018-02-07 08:50:36 -0800 | [diff] [blame] | 269 | double bitrate_priority, |
| 270 | int max_qp, |
Ilya Nikolaevskiy | 3df1d5d | 2018-08-22 09:26:51 +0200 | [diff] [blame] | 271 | bool screenshare_simulcast_enabled, |
Sergio Garcia Murillo | 43800f9 | 2018-06-21 16:16:38 +0200 | [diff] [blame] | 272 | bool temporal_layers_supported) { |
Ilya Nikolaevskiy | 3df1d5d | 2018-08-22 09:26:51 +0200 | [diff] [blame] | 273 | auto max_screenshare_layers = |
| 274 | screenshare_simulcast_enabled ? kMaxScreenshareSimulcastLayers : 1; |
Seth Hampson | 1370e30 | 2018-02-07 08:50:36 -0800 | [diff] [blame] | 275 | size_t num_simulcast_layers = |
Ilya Nikolaevskiy | 3df1d5d | 2018-08-22 09:26:51 +0200 | [diff] [blame] | 276 | std::min<int>(max_layers, max_screenshare_layers); |
Seth Hampson | 1370e30 | 2018-02-07 08:50:36 -0800 | [diff] [blame] | 277 | |
| 278 | std::vector<webrtc::VideoStream> layers(num_simulcast_layers); |
Seth Hampson | 1370e30 | 2018-02-07 08:50:36 -0800 | [diff] [blame] | 279 | // For legacy screenshare in conference mode, tl0 and tl1 bitrates are |
| 280 | // piggybacked on the VideoCodec struct as target and max bitrates, |
Erik Språng | cc681cc | 2018-03-14 17:52:55 +0100 | [diff] [blame] | 281 | // respectively. See eg. webrtc::LibvpxVp8Encoder::SetRates(). |
Seth Hampson | 1370e30 | 2018-02-07 08:50:36 -0800 | [diff] [blame] | 282 | layers[0].width = width; |
| 283 | layers[0].height = height; |
| 284 | layers[0].max_qp = max_qp; |
| 285 | layers[0].max_framerate = 5; |
| 286 | layers[0].min_bitrate_bps = kMinVideoBitrateBps; |
Rasmus Brandt | 195d1d7 | 2018-05-09 11:28:01 +0200 | [diff] [blame] | 287 | layers[0].target_bitrate_bps = kScreenshareDefaultTl0BitrateKbps * 1000; |
| 288 | layers[0].max_bitrate_bps = kScreenshareDefaultTl1BitrateKbps * 1000; |
Sergio Garcia Murillo | 43800f9 | 2018-06-21 16:16:38 +0200 | [diff] [blame] | 289 | layers[0].num_temporal_layers = temporal_layers_supported ? 2 : 0; |
Seth Hampson | 1370e30 | 2018-02-07 08:50:36 -0800 | [diff] [blame] | 290 | |
| 291 | // With simulcast enabled, add another spatial layer. This one will have a |
| 292 | // more normal layout, with the regular 3 temporal layer pattern and no fps |
| 293 | // restrictions. The base simulcast layer will still use legacy setup. |
| 294 | if (num_simulcast_layers == kMaxScreenshareSimulcastLayers) { |
| 295 | // Add optional upper simulcast layer. |
Erik Språng | 58b2284 | 2018-08-21 13:15:28 +0200 | [diff] [blame] | 296 | const int num_temporal_layers = DefaultNumberOfTemporalLayers(1, true); |
Erik Språng | b6b1cac | 2018-08-09 16:12:54 +0200 | [diff] [blame] | 297 | int max_bitrate_bps; |
Erik Språng | cf91942 | 2018-09-17 16:18:57 +0200 | [diff] [blame] | 298 | bool using_boosted_bitrate = false; |
Erik Språng | b6b1cac | 2018-08-09 16:12:54 +0200 | [diff] [blame] | 299 | if (!temporal_layers_supported) { |
| 300 | // Set the max bitrate to where the base layer would have been if temporal |
Erik Språng | 58b2284 | 2018-08-21 13:15:28 +0200 | [diff] [blame] | 301 | // layers were enabled. |
Erik Språng | b6b1cac | 2018-08-09 16:12:54 +0200 | [diff] [blame] | 302 | max_bitrate_bps = static_cast<int>( |
| 303 | kScreenshareHighStreamMaxBitrateBps * |
| 304 | webrtc::SimulcastRateAllocator::GetTemporalRateAllocation( |
| 305 | num_temporal_layers, 0)); |
Erik Språng | 58b2284 | 2018-08-21 13:15:28 +0200 | [diff] [blame] | 306 | } else if (DefaultNumberOfTemporalLayers(1, true) != 3 || |
Rasmus Brandt | 73d117f | 2018-10-02 11:12:52 +0200 | [diff] [blame] | 307 | webrtc::field_trial::IsEnabled( |
| 308 | kUseBaseHeavyVP8TL3RateAllocationFieldTrial)) { |
Erik Språng | b6b1cac | 2018-08-09 16:12:54 +0200 | [diff] [blame] | 309 | // Experimental temporal layer mode used, use increased max bitrate. |
| 310 | max_bitrate_bps = kScreenshareHighStreamMaxBitrateBps; |
Erik Språng | cf91942 | 2018-09-17 16:18:57 +0200 | [diff] [blame] | 311 | using_boosted_bitrate = true; |
Erik Språng | b6b1cac | 2018-08-09 16:12:54 +0200 | [diff] [blame] | 312 | } else { |
| 313 | // Keep current bitrates with default 3tl/8 frame settings. |
| 314 | // Lowest temporal layers of a 3 layer setup will have 40% of the total |
| 315 | // bitrate allocation for that simulcast layer. Make sure the gap between |
| 316 | // the target of the lower simulcast layer and first temporal layer of the |
| 317 | // higher one is at most 2x the bitrate, so that upswitching is not |
| 318 | // hampered by stalled bitrate estimates. |
| 319 | max_bitrate_bps = 2 * ((layers[0].target_bitrate_bps * 10) / 4); |
| 320 | } |
| 321 | |
Seth Hampson | 1370e30 | 2018-02-07 08:50:36 -0800 | [diff] [blame] | 322 | layers[1].width = width; |
| 323 | layers[1].height = height; |
| 324 | layers[1].max_qp = max_qp; |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 325 | layers[1].max_framerate = kDefaultVideoMaxFramerate; |
Erik Språng | b6b1cac | 2018-08-09 16:12:54 +0200 | [diff] [blame] | 326 | layers[1].num_temporal_layers = |
Erik Språng | 58b2284 | 2018-08-21 13:15:28 +0200 | [diff] [blame] | 327 | temporal_layers_supported ? DefaultNumberOfTemporalLayers(1, true) : 0; |
Erik Språng | cf91942 | 2018-09-17 16:18:57 +0200 | [diff] [blame] | 328 | layers[1].min_bitrate_bps = using_boosted_bitrate |
| 329 | ? kScreenshareHighStreamMinBitrateBps |
| 330 | : layers[0].target_bitrate_bps * 2; |
| 331 | |
| 332 | // Cap max bitrate so it isn't overly high for the given resolution. |
| 333 | int resolution_limited_bitrate = std::max( |
| 334 | FindSimulcastMaxBitrateBps(width, height), layers[1].min_bitrate_bps); |
| 335 | max_bitrate_bps = |
| 336 | std::min<int>(max_bitrate_bps, resolution_limited_bitrate); |
| 337 | |
Seth Hampson | 1370e30 | 2018-02-07 08:50:36 -0800 | [diff] [blame] | 338 | layers[1].target_bitrate_bps = max_bitrate_bps; |
| 339 | layers[1].max_bitrate_bps = max_bitrate_bps; |
| 340 | } |
| 341 | |
Seth Hampson | 24722b3 | 2017-12-22 09:36:42 -0800 | [diff] [blame] | 342 | // The bitrate priority currently implemented on a per-sender level, so we |
Seth Hampson | 1370e30 | 2018-02-07 08:50:36 -0800 | [diff] [blame] | 343 | // just set it for the first simulcast layer. |
| 344 | layers[0].bitrate_priority = bitrate_priority; |
| 345 | return layers; |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Ilya Nikolaevskiy | 3df1d5d | 2018-08-22 09:26:51 +0200 | [diff] [blame] | 348 | bool ScreenshareSimulcastFieldTrialEnabled() { |
Erik Språng | 72e52ee | 2018-11-29 11:41:53 +0100 | [diff] [blame] | 349 | return !webrtc::field_trial::IsDisabled(kSimulcastScreenshareFieldTrialName); |
Ilya Nikolaevskiy | 3df1d5d | 2018-08-22 09:26:51 +0200 | [diff] [blame] | 350 | } |
| 351 | |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 352 | } // namespace cricket |