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.

530 lines
22 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2019 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 "video/encoder_bitrate_adjuster.h"
  11. #include <memory>
  12. #include <vector>
  13. #include "api/units/data_rate.h"
  14. #include "rtc_base/fake_clock.h"
  15. #include "rtc_base/numerics/safe_conversions.h"
  16. #include "test/field_trial.h"
  17. #include "test/gtest.h"
  18. namespace webrtc {
  19. namespace test {
  20. class EncoderBitrateAdjusterTest : public ::testing::Test {
  21. public:
  22. static constexpr int64_t kWindowSizeMs = 3000;
  23. static constexpr int kDefaultBitrateBps = 300000;
  24. static constexpr int kDefaultFrameRateFps = 30;
  25. // For network utilization higher than media utilization, loop over a
  26. // sequence where the first half undershoots and the second half overshoots
  27. // by the same amount.
  28. static constexpr int kSequenceLength = 4;
  29. static_assert(kSequenceLength % 2 == 0, "Sequence length must be even.");
  30. EncoderBitrateAdjusterTest()
  31. : target_bitrate_(DataRate::bps(kDefaultBitrateBps)),
  32. target_framerate_fps_(kDefaultFrameRateFps),
  33. tl_pattern_idx_{},
  34. sequence_idx_{} {}
  35. protected:
  36. void SetUpAdjuster(size_t num_spatial_layers,
  37. size_t num_temporal_layers,
  38. bool vp9_svc) {
  39. // Initialize some default VideoCodec instance with the given number of
  40. // layers.
  41. if (vp9_svc) {
  42. codec_.codecType = VideoCodecType::kVideoCodecVP9;
  43. codec_.numberOfSimulcastStreams = 1;
  44. codec_.VP9()->numberOfSpatialLayers = num_spatial_layers;
  45. codec_.VP9()->numberOfTemporalLayers = num_temporal_layers;
  46. for (size_t si = 0; si < num_spatial_layers; ++si) {
  47. codec_.spatialLayers[si].minBitrate = 100 * (1 << si);
  48. codec_.spatialLayers[si].targetBitrate = 200 * (1 << si);
  49. codec_.spatialLayers[si].maxBitrate = 300 * (1 << si);
  50. codec_.spatialLayers[si].active = true;
  51. codec_.spatialLayers[si].numberOfTemporalLayers = num_temporal_layers;
  52. }
  53. } else {
  54. codec_.codecType = VideoCodecType::kVideoCodecVP8;
  55. codec_.numberOfSimulcastStreams = num_spatial_layers;
  56. codec_.VP8()->numberOfTemporalLayers = num_temporal_layers;
  57. for (size_t si = 0; si < num_spatial_layers; ++si) {
  58. codec_.simulcastStream[si].minBitrate = 100 * (1 << si);
  59. codec_.simulcastStream[si].targetBitrate = 200 * (1 << si);
  60. codec_.simulcastStream[si].maxBitrate = 300 * (1 << si);
  61. codec_.simulcastStream[si].active = true;
  62. codec_.simulcastStream[si].numberOfTemporalLayers = num_temporal_layers;
  63. }
  64. }
  65. for (size_t si = 0; si < num_spatial_layers; ++si) {
  66. encoder_info_.fps_allocation[si].resize(num_temporal_layers);
  67. double fraction = 1.0;
  68. for (int ti = num_temporal_layers - 1; ti >= 0; --ti) {
  69. encoder_info_.fps_allocation[si][ti] = static_cast<uint8_t>(
  70. VideoEncoder::EncoderInfo::kMaxFramerateFraction * fraction + 0.5);
  71. fraction /= 2.0;
  72. }
  73. }
  74. adjuster_ = std::make_unique<EncoderBitrateAdjuster>(codec_);
  75. adjuster_->OnEncoderInfo(encoder_info_);
  76. current_adjusted_allocation_ =
  77. adjuster_->AdjustRateAllocation(VideoEncoder::RateControlParameters(
  78. current_input_allocation_, target_framerate_fps_));
  79. }
  80. void InsertFrames(std::vector<std::vector<double>> media_utilization_factors,
  81. int64_t duration_ms) {
  82. InsertFrames(media_utilization_factors, media_utilization_factors,
  83. duration_ms);
  84. }
  85. void InsertFrames(
  86. std::vector<std::vector<double>> media_utilization_factors,
  87. std::vector<std::vector<double>> network_utilization_factors,
  88. int64_t duration_ms) {
  89. RTC_DCHECK_EQ(media_utilization_factors.size(),
  90. network_utilization_factors.size());
  91. constexpr size_t kMaxFrameSize = 100000;
  92. uint8_t buffer[kMaxFrameSize];
  93. const int64_t start_us = rtc::TimeMicros();
  94. while (rtc::TimeMicros() <
  95. start_us + (duration_ms * rtc::kNumMicrosecsPerMillisec)) {
  96. clock_.AdvanceTime(TimeDelta::seconds(1) / target_framerate_fps_);
  97. for (size_t si = 0; si < NumSpatialLayers(); ++si) {
  98. const std::vector<int>& tl_pattern =
  99. kTlPatterns[NumTemporalLayers(si) - 1];
  100. const size_t ti =
  101. tl_pattern[(tl_pattern_idx_[si]++) % tl_pattern.size()];
  102. uint32_t layer_bitrate_bps =
  103. current_adjusted_allocation_.GetBitrate(si, ti);
  104. double layer_framerate_fps = target_framerate_fps_;
  105. if (encoder_info_.fps_allocation[si].size() > ti) {
  106. uint8_t layer_fps_fraction = encoder_info_.fps_allocation[si][ti];
  107. if (ti > 0) {
  108. // We're interested in the frame rate for this layer only, not
  109. // cumulative frame rate.
  110. layer_fps_fraction -= encoder_info_.fps_allocation[si][ti - 1];
  111. }
  112. layer_framerate_fps =
  113. (target_framerate_fps_ * layer_fps_fraction) /
  114. VideoEncoder::EncoderInfo::kMaxFramerateFraction;
  115. }
  116. double media_utilization_factor = 1.0;
  117. double network_utilization_factor = 1.0;
  118. if (media_utilization_factors.size() > si) {
  119. RTC_DCHECK_EQ(media_utilization_factors[si].size(),
  120. network_utilization_factors[si].size());
  121. if (media_utilization_factors[si].size() > ti) {
  122. media_utilization_factor = media_utilization_factors[si][ti];
  123. network_utilization_factor = network_utilization_factors[si][ti];
  124. }
  125. }
  126. RTC_DCHECK_GE(network_utilization_factor, media_utilization_factor);
  127. // Frame size based on constant (media) overshoot.
  128. const size_t media_frame_size = media_utilization_factor *
  129. (layer_bitrate_bps / 8.0) /
  130. layer_framerate_fps;
  131. constexpr int kFramesWithPenalty = (kSequenceLength / 2) - 1;
  132. RTC_DCHECK_GT(kFramesWithPenalty, 0);
  133. // The positive/negative size diff needed to achieve network rate but
  134. // not media rate penalty is the difference between the utilization
  135. // factors times the media rate frame size, then scaled by the fraction
  136. // between total frames and penalized frames in the sequence.
  137. // Cap to media frame size to avoid negative size undershoot.
  138. const size_t network_frame_size_diff_bytes = std::min(
  139. media_frame_size,
  140. static_cast<size_t>(
  141. (((network_utilization_factor - media_utilization_factor) *
  142. media_frame_size) *
  143. kSequenceLength) /
  144. kFramesWithPenalty +
  145. 0.5));
  146. int sequence_idx = sequence_idx_[si][ti];
  147. sequence_idx_[si][ti] = (sequence_idx_[si][ti] + 1) % kSequenceLength;
  148. const size_t frame_size_bytes =
  149. (sequence_idx < kSequenceLength / 2)
  150. ? media_frame_size - network_frame_size_diff_bytes
  151. : media_frame_size + network_frame_size_diff_bytes;
  152. EncodedImage image(buffer, 0, kMaxFrameSize);
  153. image.set_size(frame_size_bytes);
  154. image.SetSpatialIndex(si);
  155. adjuster_->OnEncodedFrame(image, ti);
  156. sequence_idx = ++sequence_idx % kSequenceLength;
  157. }
  158. }
  159. }
  160. size_t NumSpatialLayers() const {
  161. if (codec_.codecType == VideoCodecType::kVideoCodecVP9) {
  162. return codec_.VP9().numberOfSpatialLayers;
  163. }
  164. return codec_.numberOfSimulcastStreams;
  165. }
  166. size_t NumTemporalLayers(int spatial_index) {
  167. if (codec_.codecType == VideoCodecType::kVideoCodecVP9) {
  168. return codec_.spatialLayers[spatial_index].numberOfTemporalLayers;
  169. }
  170. return codec_.simulcastStream[spatial_index].numberOfTemporalLayers;
  171. }
  172. void ExpectNear(const VideoBitrateAllocation& expected_allocation,
  173. const VideoBitrateAllocation& actual_allocation,
  174. double allowed_error_fraction) {
  175. for (size_t si = 0; si < kMaxSpatialLayers; ++si) {
  176. for (size_t ti = 0; ti < kMaxTemporalStreams; ++ti) {
  177. if (expected_allocation.HasBitrate(si, ti)) {
  178. EXPECT_TRUE(actual_allocation.HasBitrate(si, ti));
  179. uint32_t expected_layer_bitrate_bps =
  180. expected_allocation.GetBitrate(si, ti);
  181. EXPECT_NEAR(expected_layer_bitrate_bps,
  182. actual_allocation.GetBitrate(si, ti),
  183. static_cast<uint32_t>(expected_layer_bitrate_bps *
  184. allowed_error_fraction));
  185. } else {
  186. EXPECT_FALSE(actual_allocation.HasBitrate(si, ti));
  187. }
  188. }
  189. }
  190. }
  191. VideoBitrateAllocation MultiplyAllocation(
  192. const VideoBitrateAllocation& allocation,
  193. double factor) {
  194. VideoBitrateAllocation multiplied_allocation;
  195. for (size_t si = 0; si < kMaxSpatialLayers; ++si) {
  196. for (size_t ti = 0; ti < kMaxTemporalStreams; ++ti) {
  197. if (allocation.HasBitrate(si, ti)) {
  198. multiplied_allocation.SetBitrate(
  199. si, ti,
  200. static_cast<uint32_t>(factor * allocation.GetBitrate(si, ti) +
  201. 0.5));
  202. }
  203. }
  204. }
  205. return multiplied_allocation;
  206. }
  207. VideoCodec codec_;
  208. VideoEncoder::EncoderInfo encoder_info_;
  209. std::unique_ptr<EncoderBitrateAdjuster> adjuster_;
  210. VideoBitrateAllocation current_input_allocation_;
  211. VideoBitrateAllocation current_adjusted_allocation_;
  212. rtc::ScopedFakeClock clock_;
  213. DataRate target_bitrate_;
  214. double target_framerate_fps_;
  215. int tl_pattern_idx_[kMaxSpatialLayers];
  216. int sequence_idx_[kMaxSpatialLayers][kMaxTemporalStreams];
  217. const std::vector<int> kTlPatterns[kMaxTemporalStreams] = {
  218. {0},
  219. {0, 1},
  220. {0, 2, 1, 2},
  221. {0, 3, 2, 3, 1, 3, 2, 3}};
  222. };
  223. TEST_F(EncoderBitrateAdjusterTest, SingleLayerOptimal) {
  224. // Single layer, well behaved encoder.
  225. current_input_allocation_.SetBitrate(0, 0, 300000);
  226. target_framerate_fps_ = 30;
  227. SetUpAdjuster(1, 1, false);
  228. InsertFrames({{1.0}}, kWindowSizeMs);
  229. current_adjusted_allocation_ =
  230. adjuster_->AdjustRateAllocation(VideoEncoder::RateControlParameters(
  231. current_input_allocation_, target_framerate_fps_));
  232. // Adjusted allocation near input. Allow 1% error margin due to rounding
  233. // errors etc.
  234. ExpectNear(current_input_allocation_, current_adjusted_allocation_, 0.01);
  235. }
  236. TEST_F(EncoderBitrateAdjusterTest, SingleLayerOveruse) {
  237. // Single layer, well behaved encoder.
  238. current_input_allocation_.SetBitrate(0, 0, 300000);
  239. target_framerate_fps_ = 30;
  240. SetUpAdjuster(1, 1, false);
  241. InsertFrames({{1.2}}, kWindowSizeMs);
  242. current_adjusted_allocation_ =
  243. adjuster_->AdjustRateAllocation(VideoEncoder::RateControlParameters(
  244. current_input_allocation_, target_framerate_fps_));
  245. // Adjusted allocation lowered by 20%.
  246. ExpectNear(MultiplyAllocation(current_input_allocation_, 1 / 1.2),
  247. current_adjusted_allocation_, 0.01);
  248. }
  249. TEST_F(EncoderBitrateAdjusterTest, SingleLayerUnderuse) {
  250. // Single layer, well behaved encoder.
  251. current_input_allocation_.SetBitrate(0, 0, 300000);
  252. target_framerate_fps_ = 30;
  253. SetUpAdjuster(1, 1, false);
  254. InsertFrames({{0.5}}, kWindowSizeMs);
  255. current_adjusted_allocation_ =
  256. adjuster_->AdjustRateAllocation(VideoEncoder::RateControlParameters(
  257. current_input_allocation_, target_framerate_fps_));
  258. // Undershoot, adjusted should exactly match input.
  259. ExpectNear(current_input_allocation_, current_adjusted_allocation_, 0.00);
  260. }
  261. TEST_F(EncoderBitrateAdjusterTest, ThreeTemporalLayersOptimalSize) {
  262. // Three temporal layers, 60%/20%/20% bps distro, well behaved encoder.
  263. current_input_allocation_.SetBitrate(0, 0, 180000);
  264. current_input_allocation_.SetBitrate(0, 1, 60000);
  265. current_input_allocation_.SetBitrate(0, 2, 60000);
  266. target_framerate_fps_ = 30;
  267. SetUpAdjuster(1, 3, false);
  268. InsertFrames({{1.0, 1.0, 1.0}}, kWindowSizeMs);
  269. current_adjusted_allocation_ =
  270. adjuster_->AdjustRateAllocation(VideoEncoder::RateControlParameters(
  271. current_input_allocation_, target_framerate_fps_));
  272. ExpectNear(current_input_allocation_, current_adjusted_allocation_, 0.01);
  273. }
  274. TEST_F(EncoderBitrateAdjusterTest, ThreeTemporalLayersOvershoot) {
  275. // Three temporal layers, 60%/20%/20% bps distro.
  276. // 10% overshoot on all layers.
  277. current_input_allocation_.SetBitrate(0, 0, 180000);
  278. current_input_allocation_.SetBitrate(0, 1, 60000);
  279. current_input_allocation_.SetBitrate(0, 2, 60000);
  280. target_framerate_fps_ = 30;
  281. SetUpAdjuster(1, 3, false);
  282. InsertFrames({{1.1, 1.1, 1.1}}, kWindowSizeMs);
  283. current_adjusted_allocation_ =
  284. adjuster_->AdjustRateAllocation(VideoEncoder::RateControlParameters(
  285. current_input_allocation_, target_framerate_fps_));
  286. // Adjusted allocation lowered by 10%.
  287. ExpectNear(MultiplyAllocation(current_input_allocation_, 1 / 1.1),
  288. current_adjusted_allocation_, 0.01);
  289. }
  290. TEST_F(EncoderBitrateAdjusterTest, ThreeTemporalLayersUndershoot) {
  291. // Three temporal layers, 60%/20%/20% bps distro, undershoot all layers.
  292. current_input_allocation_.SetBitrate(0, 0, 180000);
  293. current_input_allocation_.SetBitrate(0, 1, 60000);
  294. current_input_allocation_.SetBitrate(0, 2, 60000);
  295. target_framerate_fps_ = 30;
  296. SetUpAdjuster(1, 3, false);
  297. InsertFrames({{0.8, 0.8, 0.8}}, kWindowSizeMs);
  298. current_adjusted_allocation_ =
  299. adjuster_->AdjustRateAllocation(VideoEncoder::RateControlParameters(
  300. current_input_allocation_, target_framerate_fps_));
  301. // Adjusted allocation identical since we don't boost bitrates.
  302. ExpectNear(current_input_allocation_, current_adjusted_allocation_, 0.0);
  303. }
  304. TEST_F(EncoderBitrateAdjusterTest, ThreeTemporalLayersSkewedOvershoot) {
  305. // Three temporal layers, 60%/20%/20% bps distro.
  306. // 10% overshoot on base layer, 20% on higher layers.
  307. current_input_allocation_.SetBitrate(0, 0, 180000);
  308. current_input_allocation_.SetBitrate(0, 1, 60000);
  309. current_input_allocation_.SetBitrate(0, 2, 60000);
  310. target_framerate_fps_ = 30;
  311. SetUpAdjuster(1, 3, false);
  312. InsertFrames({{1.1, 1.2, 1.2}}, kWindowSizeMs);
  313. current_adjusted_allocation_ =
  314. adjuster_->AdjustRateAllocation(VideoEncoder::RateControlParameters(
  315. current_input_allocation_, target_framerate_fps_));
  316. // Expected overshoot is weighted by bitrate:
  317. // (0.6 * 1.1 + 0.2 * 1.2 + 0.2 * 1.2) = 1.14
  318. ExpectNear(MultiplyAllocation(current_input_allocation_, 1 / 1.14),
  319. current_adjusted_allocation_, 0.01);
  320. }
  321. TEST_F(EncoderBitrateAdjusterTest, FourTemporalLayersSkewedOvershoot) {
  322. // Three temporal layers, 40%/30%/15%/15% bps distro.
  323. // 10% overshoot on base layer, 20% on higher layers.
  324. current_input_allocation_.SetBitrate(0, 0, 120000);
  325. current_input_allocation_.SetBitrate(0, 1, 90000);
  326. current_input_allocation_.SetBitrate(0, 2, 45000);
  327. current_input_allocation_.SetBitrate(0, 3, 45000);
  328. target_framerate_fps_ = 30;
  329. SetUpAdjuster(1, 4, false);
  330. InsertFrames({{1.1, 1.2, 1.2, 1.2}}, kWindowSizeMs);
  331. current_adjusted_allocation_ =
  332. adjuster_->AdjustRateAllocation(VideoEncoder::RateControlParameters(
  333. current_input_allocation_, target_framerate_fps_));
  334. // Expected overshoot is weighted by bitrate:
  335. // (0.4 * 1.1 + 0.3 * 1.2 + 0.15 * 1.2 + 0.15 * 1.2) = 1.16
  336. ExpectNear(MultiplyAllocation(current_input_allocation_, 1 / 1.16),
  337. current_adjusted_allocation_, 0.01);
  338. }
  339. TEST_F(EncoderBitrateAdjusterTest, ThreeTemporalLayersNonLayeredEncoder) {
  340. // Three temporal layers, 60%/20%/20% bps allocation, 10% overshoot,
  341. // encoder does not actually support temporal layers.
  342. current_input_allocation_.SetBitrate(0, 0, 180000);
  343. current_input_allocation_.SetBitrate(0, 1, 60000);
  344. current_input_allocation_.SetBitrate(0, 2, 60000);
  345. target_framerate_fps_ = 30;
  346. SetUpAdjuster(1, 1, false);
  347. InsertFrames({{1.1}}, kWindowSizeMs);
  348. current_adjusted_allocation_ =
  349. adjuster_->AdjustRateAllocation(VideoEncoder::RateControlParameters(
  350. current_input_allocation_, target_framerate_fps_));
  351. // Expect the actual 10% overuse to be detected and the allocation to
  352. // only contain the one entry.
  353. VideoBitrateAllocation expected_allocation;
  354. expected_allocation.SetBitrate(
  355. 0, 0,
  356. static_cast<uint32_t>(current_input_allocation_.get_sum_bps() / 1.10));
  357. ExpectNear(expected_allocation, current_adjusted_allocation_, 0.01);
  358. }
  359. TEST_F(EncoderBitrateAdjusterTest, IgnoredStream) {
  360. // Encoder with three temporal layers, but in a mode that does not support
  361. // deterministic frame rate. Those are ignored, even if bitrate overshoots.
  362. current_input_allocation_.SetBitrate(0, 0, 180000);
  363. current_input_allocation_.SetBitrate(0, 1, 60000);
  364. target_framerate_fps_ = 30;
  365. SetUpAdjuster(1, 1, false);
  366. encoder_info_.fps_allocation[0].clear();
  367. adjuster_->OnEncoderInfo(encoder_info_);
  368. InsertFrames({{1.1}}, kWindowSizeMs);
  369. current_adjusted_allocation_ =
  370. adjuster_->AdjustRateAllocation(VideoEncoder::RateControlParameters(
  371. current_input_allocation_, target_framerate_fps_));
  372. // Values passed through.
  373. ExpectNear(current_input_allocation_, current_adjusted_allocation_, 0.00);
  374. }
  375. TEST_F(EncoderBitrateAdjusterTest, DifferentSpatialOvershoots) {
  376. // Two streams, both with three temporal layers.
  377. // S0 has 5% overshoot, S1 has 25% overshoot.
  378. current_input_allocation_.SetBitrate(0, 0, 180000);
  379. current_input_allocation_.SetBitrate(0, 1, 60000);
  380. current_input_allocation_.SetBitrate(0, 2, 60000);
  381. current_input_allocation_.SetBitrate(1, 0, 400000);
  382. current_input_allocation_.SetBitrate(1, 1, 150000);
  383. current_input_allocation_.SetBitrate(1, 2, 150000);
  384. target_framerate_fps_ = 30;
  385. // Run twice, once configured as simulcast and once as VP9 SVC.
  386. for (int i = 0; i < 2; ++i) {
  387. SetUpAdjuster(2, 3, i == 0);
  388. InsertFrames({{1.05, 1.05, 1.05}, {1.25, 1.25, 1.25}}, kWindowSizeMs);
  389. current_adjusted_allocation_ =
  390. adjuster_->AdjustRateAllocation(VideoEncoder::RateControlParameters(
  391. current_input_allocation_, target_framerate_fps_));
  392. VideoBitrateAllocation expected_allocation;
  393. for (size_t ti = 0; ti < 3; ++ti) {
  394. expected_allocation.SetBitrate(
  395. 0, ti,
  396. static_cast<uint32_t>(current_input_allocation_.GetBitrate(0, ti) /
  397. 1.05));
  398. expected_allocation.SetBitrate(
  399. 1, ti,
  400. static_cast<uint32_t>(current_input_allocation_.GetBitrate(1, ti) /
  401. 1.25));
  402. }
  403. ExpectNear(expected_allocation, current_adjusted_allocation_, 0.01);
  404. }
  405. }
  406. TEST_F(EncoderBitrateAdjusterTest, HeadroomAllowsOvershootToMediaRate) {
  407. // Two streams, both with three temporal layers.
  408. // Media rate is 1.0, but network rate is higher.
  409. ScopedFieldTrials field_trial(
  410. "WebRTC-VideoRateControl/adjuster_use_headroom:true/");
  411. const uint32_t kS0Bitrate = 300000;
  412. const uint32_t kS1Bitrate = 900000;
  413. current_input_allocation_.SetBitrate(0, 0, kS0Bitrate / 3);
  414. current_input_allocation_.SetBitrate(0, 1, kS0Bitrate / 3);
  415. current_input_allocation_.SetBitrate(0, 2, kS0Bitrate / 3);
  416. current_input_allocation_.SetBitrate(1, 0, kS1Bitrate / 3);
  417. current_input_allocation_.SetBitrate(1, 1, kS1Bitrate / 3);
  418. current_input_allocation_.SetBitrate(1, 2, kS1Bitrate / 3);
  419. target_framerate_fps_ = 30;
  420. // Run twice, once configured as simulcast and once as VP9 SVC.
  421. for (int i = 0; i < 2; ++i) {
  422. SetUpAdjuster(2, 3, i == 0);
  423. // Network rate has 10% overshoot, but media rate is correct at 1.0.
  424. InsertFrames({{1.0, 1.0, 1.0}, {1.0, 1.0, 1.0}},
  425. {{1.1, 1.1, 1.1}, {1.1, 1.1, 1.1}},
  426. kWindowSizeMs * kSequenceLength);
  427. // Push back by 10%.
  428. current_adjusted_allocation_ =
  429. adjuster_->AdjustRateAllocation(VideoEncoder::RateControlParameters(
  430. current_input_allocation_, target_framerate_fps_));
  431. ExpectNear(MultiplyAllocation(current_input_allocation_, 1 / 1.1),
  432. current_adjusted_allocation_, 0.01);
  433. // Add 10% link headroom, overshoot is now allowed.
  434. current_adjusted_allocation_ =
  435. adjuster_->AdjustRateAllocation(VideoEncoder::RateControlParameters(
  436. current_input_allocation_, target_framerate_fps_,
  437. DataRate::bps(current_input_allocation_.get_sum_bps() * 1.1)));
  438. ExpectNear(current_input_allocation_, current_adjusted_allocation_, 0.01);
  439. }
  440. }
  441. TEST_F(EncoderBitrateAdjusterTest, DontExceedMediaRateEvenWithHeadroom) {
  442. // Two streams, both with three temporal layers.
  443. // Media rate is 1.1, but network rate is higher.
  444. ScopedFieldTrials field_trial(
  445. "WebRTC-VideoRateControl/adjuster_use_headroom:true/");
  446. const uint32_t kS0Bitrate = 300000;
  447. const uint32_t kS1Bitrate = 900000;
  448. current_input_allocation_.SetBitrate(0, 0, kS0Bitrate / 3);
  449. current_input_allocation_.SetBitrate(0, 1, kS0Bitrate / 3);
  450. current_input_allocation_.SetBitrate(0, 2, kS0Bitrate / 3);
  451. current_input_allocation_.SetBitrate(1, 0, kS1Bitrate / 3);
  452. current_input_allocation_.SetBitrate(1, 1, kS1Bitrate / 3);
  453. current_input_allocation_.SetBitrate(1, 2, kS1Bitrate / 3);
  454. target_framerate_fps_ = 30;
  455. // Run twice, once configured as simulcast and once as VP9 SVC.
  456. for (int i = 0; i < 2; ++i) {
  457. SetUpAdjuster(2, 3, i == 0);
  458. // Network rate has 30% overshoot, media rate has 10% overshoot.
  459. InsertFrames({{1.1, 1.1, 1.1}, {1.1, 1.1, 1.1}},
  460. {{1.3, 1.3, 1.3}, {1.3, 1.3, 1.3}},
  461. kWindowSizeMs * kSequenceLength);
  462. // Push back by 30%.
  463. current_adjusted_allocation_ =
  464. adjuster_->AdjustRateAllocation(VideoEncoder::RateControlParameters(
  465. current_input_allocation_, target_framerate_fps_));
  466. // The up-down causes a bit more noise, allow slightly more error margin.
  467. ExpectNear(MultiplyAllocation(current_input_allocation_, 1 / 1.3),
  468. current_adjusted_allocation_, 0.015);
  469. // Add 100% link headroom, overshoot from network to media rate is allowed.
  470. current_adjusted_allocation_ =
  471. adjuster_->AdjustRateAllocation(VideoEncoder::RateControlParameters(
  472. current_input_allocation_, target_framerate_fps_,
  473. DataRate::bps(current_input_allocation_.get_sum_bps() * 2)));
  474. ExpectNear(MultiplyAllocation(current_input_allocation_, 1 / 1.1),
  475. current_adjusted_allocation_, 0.015);
  476. }
  477. }
  478. } // namespace test
  479. } // namespace webrtc