You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

408 lines
16 KiB

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