blob: c4a0a08fd19475f98f6f23a40c2fb3e80f9b894e [file] [log] [blame]
Per69b332d2016-06-02 13:45:421/*
2 * Copyright (c) 2016 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
Jonas Olssona4d87372019-07-05 17:08:3311#include "api/fec_controller.h"
12
Yves Gerey3e707812018-11-28 15:47:4913#include <stdint.h>
Jonas Olssona4d87372019-07-05 17:08:3314
Yves Gerey3e707812018-11-28 15:47:4915#include <vector>
16
Danil Chapovalov1d6bf312024-01-09 10:46:5917#include "api/environment/environment_factory.h"
Yves Gerey3e707812018-11-28 15:47:4918#include "modules/include/module_fec_types.h"
Ying Wang3b790f32018-01-19 16:58:5719#include "modules/video_coding/fec_controller_default.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3120#include "system_wrappers/include/clock.h"
21#include "test/gtest.h"
Per69b332d2016-06-02 13:45:4222
23namespace webrtc {
24
25static const int kCodecBitrateBps = 100000;
26
27class ProtectionBitrateCalculatorTest : public ::testing::Test {
28 protected:
29 enum {
30 kSampleRate = 90000 // RTP timestamps per second.
31 };
32
33 class ProtectionCallback : public VCMProtectionCallback {
34 public:
35 int ProtectionRequest(const FecProtectionParams* delta_params,
36 const FecProtectionParams* key_params,
37 uint32_t* sent_video_rate_bps,
38 uint32_t* sent_nack_rate_bps,
39 uint32_t* sent_fec_rate_bps) override {
40 *sent_video_rate_bps = kCodecBitrateBps;
41 *sent_nack_rate_bps = nack_rate_bps_;
42 *sent_fec_rate_bps = fec_rate_bps_;
43 return 0;
44 }
Ying Wang2d598532023-06-01 05:55:3645 void SetRetransmissionMode(int retransmission_mode) {}
Per69b332d2016-06-02 13:45:4246
47 uint32_t fec_rate_bps_ = 0;
48 uint32_t nack_rate_bps_ = 0;
49 };
50
51 // Note: simulated clock starts at 1 seconds, since parts of webrtc use 0 as
52 // a special case (e.g. frame rate in media optimization).
53 ProtectionBitrateCalculatorTest()
Danil Chapovalov1d6bf312024-01-09 10:46:5954 : clock_(1000),
55 fec_controller_(CreateEnvironment(&clock_), &protection_callback_) {}
Per69b332d2016-06-02 13:45:4256
57 SimulatedClock clock_;
58 ProtectionCallback protection_callback_;
Ying Wang3b790f32018-01-19 16:58:5759 FecControllerDefault fec_controller_;
Per69b332d2016-06-02 13:45:4260};
61
62TEST_F(ProtectionBitrateCalculatorTest, ProtectsUsingFecBitrate) {
63 static const uint32_t kMaxBitrateBps = 130000;
64
Ying Wang3b790f32018-01-19 16:58:5765 fec_controller_.SetProtectionMethod(true /*enable_fec*/,
66 false /* enable_nack */);
67 fec_controller_.SetEncodingData(640, 480, 1, 1000);
Per69b332d2016-06-02 13:45:4268
69 // Using 10% of codec bitrate for FEC.
70 protection_callback_.fec_rate_bps_ = kCodecBitrateBps / 10;
Ying Wang3b790f32018-01-19 16:58:5771 uint32_t target_bitrate = fec_controller_.UpdateFecRates(
72 kMaxBitrateBps, 30, 0, std::vector<bool>(1, false), 0);
Per69b332d2016-06-02 13:45:4273
74 EXPECT_GT(target_bitrate, 0u);
75 EXPECT_GT(kMaxBitrateBps, target_bitrate);
76
77 // Using as much for codec bitrate as fec rate, new target rate should share
78 // both equally, but only be half of max (since that ceiling should be hit).
79 protection_callback_.fec_rate_bps_ = kCodecBitrateBps;
Ying Wang3b790f32018-01-19 16:58:5780 target_bitrate = fec_controller_.UpdateFecRates(
81 kMaxBitrateBps, 30, 128, std::vector<bool>(1, false), 100);
Per69b332d2016-06-02 13:45:4282 EXPECT_EQ(kMaxBitrateBps / 2, target_bitrate);
83}
84
85TEST_F(ProtectionBitrateCalculatorTest, ProtectsUsingNackBitrate) {
86 static const uint32_t kMaxBitrateBps = 130000;
87
Ying Wang3b790f32018-01-19 16:58:5788 fec_controller_.SetProtectionMethod(false /*enable_fec*/,
89 true /* enable_nack */);
90 fec_controller_.SetEncodingData(640, 480, 1, 1000);
Per69b332d2016-06-02 13:45:4291
Ying Wang3b790f32018-01-19 16:58:5792 uint32_t target_bitrate = fec_controller_.UpdateFecRates(
93 kMaxBitrateBps, 30, 0, std::vector<bool>(1, false), 0);
Per69b332d2016-06-02 13:45:4294
95 EXPECT_EQ(kMaxBitrateBps, target_bitrate);
96
97 // Using as much for codec bitrate as nack rate, new target rate should share
98 // both equally, but only be half of max (since that ceiling should be hit).
99 protection_callback_.nack_rate_bps_ = kMaxBitrateBps;
Ying Wang3b790f32018-01-19 16:58:57100 target_bitrate = fec_controller_.UpdateFecRates(
101 kMaxBitrateBps, 30, 128, std::vector<bool>(1, false), 100);
Per69b332d2016-06-02 13:45:42102 EXPECT_EQ(kMaxBitrateBps / 2, target_bitrate);
103}
104
perkj57c21f92016-06-17 14:27:16105TEST_F(ProtectionBitrateCalculatorTest, NoProtection) {
106 static const uint32_t kMaxBitrateBps = 130000;
107
Ying Wang3b790f32018-01-19 16:58:57108 fec_controller_.SetProtectionMethod(false /*enable_fec*/,
109 false /* enable_nack */);
110 fec_controller_.SetEncodingData(640, 480, 1, 1000);
perkj57c21f92016-06-17 14:27:16111
Ying Wang3b790f32018-01-19 16:58:57112 uint32_t target_bitrate = fec_controller_.UpdateFecRates(
113 kMaxBitrateBps, 30, 128, std::vector<bool>(1, false), 100);
perkj57c21f92016-06-17 14:27:16114 EXPECT_EQ(kMaxBitrateBps, target_bitrate);
115}
116
Per69b332d2016-06-02 13:45:42117} // namespace webrtc