blob: 51ae15808f2013e8edf07d65b792b03a9a0cb5c8 [file] [log] [blame]
Per69b332d2016-06-02 15:45:42 +02001/*
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
Ying Wang3b790f32018-01-19 17:58:57 +010011#include "modules/video_coding/fec_controller_default.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020012#include "system_wrappers/include/clock.h"
13#include "test/gtest.h"
Per69b332d2016-06-02 15:45:42 +020014
15namespace webrtc {
16
17static const int kCodecBitrateBps = 100000;
18
19class ProtectionBitrateCalculatorTest : public ::testing::Test {
20 protected:
21 enum {
22 kSampleRate = 90000 // RTP timestamps per second.
23 };
24
25 class ProtectionCallback : public VCMProtectionCallback {
26 public:
27 int ProtectionRequest(const FecProtectionParams* delta_params,
28 const FecProtectionParams* key_params,
29 uint32_t* sent_video_rate_bps,
30 uint32_t* sent_nack_rate_bps,
31 uint32_t* sent_fec_rate_bps) override {
32 *sent_video_rate_bps = kCodecBitrateBps;
33 *sent_nack_rate_bps = nack_rate_bps_;
34 *sent_fec_rate_bps = fec_rate_bps_;
35 return 0;
36 }
37
38 uint32_t fec_rate_bps_ = 0;
39 uint32_t nack_rate_bps_ = 0;
40 };
41
42 // Note: simulated clock starts at 1 seconds, since parts of webrtc use 0 as
43 // a special case (e.g. frame rate in media optimization).
44 ProtectionBitrateCalculatorTest()
Ying Wang3b790f32018-01-19 17:58:57 +010045 : clock_(1000), fec_controller_(&clock_, &protection_callback_) {}
Per69b332d2016-06-02 15:45:42 +020046
47 SimulatedClock clock_;
48 ProtectionCallback protection_callback_;
Ying Wang3b790f32018-01-19 17:58:57 +010049 FecControllerDefault fec_controller_;
Per69b332d2016-06-02 15:45:42 +020050};
51
52TEST_F(ProtectionBitrateCalculatorTest, ProtectsUsingFecBitrate) {
53 static const uint32_t kMaxBitrateBps = 130000;
54
Ying Wang3b790f32018-01-19 17:58:57 +010055 fec_controller_.SetProtectionMethod(true /*enable_fec*/,
56 false /* enable_nack */);
57 fec_controller_.SetEncodingData(640, 480, 1, 1000);
Per69b332d2016-06-02 15:45:42 +020058
59 // Using 10% of codec bitrate for FEC.
60 protection_callback_.fec_rate_bps_ = kCodecBitrateBps / 10;
Ying Wang3b790f32018-01-19 17:58:57 +010061 uint32_t target_bitrate = fec_controller_.UpdateFecRates(
62 kMaxBitrateBps, 30, 0, std::vector<bool>(1, false), 0);
Per69b332d2016-06-02 15:45:42 +020063
64 EXPECT_GT(target_bitrate, 0u);
65 EXPECT_GT(kMaxBitrateBps, target_bitrate);
66
67 // Using as much for codec bitrate as fec rate, new target rate should share
68 // both equally, but only be half of max (since that ceiling should be hit).
69 protection_callback_.fec_rate_bps_ = kCodecBitrateBps;
Ying Wang3b790f32018-01-19 17:58:57 +010070 target_bitrate = fec_controller_.UpdateFecRates(
71 kMaxBitrateBps, 30, 128, std::vector<bool>(1, false), 100);
Per69b332d2016-06-02 15:45:42 +020072 EXPECT_EQ(kMaxBitrateBps / 2, target_bitrate);
73}
74
75TEST_F(ProtectionBitrateCalculatorTest, ProtectsUsingNackBitrate) {
76 static const uint32_t kMaxBitrateBps = 130000;
77
Ying Wang3b790f32018-01-19 17:58:57 +010078 fec_controller_.SetProtectionMethod(false /*enable_fec*/,
79 true /* enable_nack */);
80 fec_controller_.SetEncodingData(640, 480, 1, 1000);
Per69b332d2016-06-02 15:45:42 +020081
Ying Wang3b790f32018-01-19 17:58:57 +010082 uint32_t target_bitrate = fec_controller_.UpdateFecRates(
83 kMaxBitrateBps, 30, 0, std::vector<bool>(1, false), 0);
Per69b332d2016-06-02 15:45:42 +020084
85 EXPECT_EQ(kMaxBitrateBps, target_bitrate);
86
87 // Using as much for codec bitrate as nack rate, new target rate should share
88 // both equally, but only be half of max (since that ceiling should be hit).
89 protection_callback_.nack_rate_bps_ = kMaxBitrateBps;
Ying Wang3b790f32018-01-19 17:58:57 +010090 target_bitrate = fec_controller_.UpdateFecRates(
91 kMaxBitrateBps, 30, 128, std::vector<bool>(1, false), 100);
Per69b332d2016-06-02 15:45:42 +020092 EXPECT_EQ(kMaxBitrateBps / 2, target_bitrate);
93}
94
perkj57c21f92016-06-17 07:27:16 -070095TEST_F(ProtectionBitrateCalculatorTest, NoProtection) {
96 static const uint32_t kMaxBitrateBps = 130000;
97
Ying Wang3b790f32018-01-19 17:58:57 +010098 fec_controller_.SetProtectionMethod(false /*enable_fec*/,
99 false /* enable_nack */);
100 fec_controller_.SetEncodingData(640, 480, 1, 1000);
perkj57c21f92016-06-17 07:27:16 -0700101
Ying Wang3b790f32018-01-19 17:58:57 +0100102 uint32_t target_bitrate = fec_controller_.UpdateFecRates(
103 kMaxBitrateBps, 30, 128, std::vector<bool>(1, false), 100);
perkj57c21f92016-06-17 07:27:16 -0700104 EXPECT_EQ(kMaxBitrateBps, target_bitrate);
105}
106
Per69b332d2016-06-02 15:45:42 +0200107} // namespace webrtc