blob: 5a9ec4baccd9fc3c66d48a5934036c34409f95aa [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
Yves Gerey3e707812018-11-28 15:47:4911#include <stdint.h>
12#include <vector>
13
14#include "api/fec_controller.h"
15#include "modules/include/module_fec_types.h"
Ying Wang3b790f32018-01-19 16:58:5716#include "modules/video_coding/fec_controller_default.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3117#include "system_wrappers/include/clock.h"
18#include "test/gtest.h"
Per69b332d2016-06-02 13:45:4219
20namespace webrtc {
21
22static const int kCodecBitrateBps = 100000;
23
24class ProtectionBitrateCalculatorTest : public ::testing::Test {
25 protected:
26 enum {
27 kSampleRate = 90000 // RTP timestamps per second.
28 };
29
30 class ProtectionCallback : public VCMProtectionCallback {
31 public:
32 int ProtectionRequest(const FecProtectionParams* delta_params,
33 const FecProtectionParams* key_params,
34 uint32_t* sent_video_rate_bps,
35 uint32_t* sent_nack_rate_bps,
36 uint32_t* sent_fec_rate_bps) override {
37 *sent_video_rate_bps = kCodecBitrateBps;
38 *sent_nack_rate_bps = nack_rate_bps_;
39 *sent_fec_rate_bps = fec_rate_bps_;
40 return 0;
41 }
42
43 uint32_t fec_rate_bps_ = 0;
44 uint32_t nack_rate_bps_ = 0;
45 };
46
47 // Note: simulated clock starts at 1 seconds, since parts of webrtc use 0 as
48 // a special case (e.g. frame rate in media optimization).
49 ProtectionBitrateCalculatorTest()
Ying Wang3b790f32018-01-19 16:58:5750 : clock_(1000), fec_controller_(&clock_, &protection_callback_) {}
Per69b332d2016-06-02 13:45:4251
52 SimulatedClock clock_;
53 ProtectionCallback protection_callback_;
Ying Wang3b790f32018-01-19 16:58:5754 FecControllerDefault fec_controller_;
Per69b332d2016-06-02 13:45:4255};
56
57TEST_F(ProtectionBitrateCalculatorTest, ProtectsUsingFecBitrate) {
58 static const uint32_t kMaxBitrateBps = 130000;
59
Ying Wang3b790f32018-01-19 16:58:5760 fec_controller_.SetProtectionMethod(true /*enable_fec*/,
61 false /* enable_nack */);
62 fec_controller_.SetEncodingData(640, 480, 1, 1000);
Per69b332d2016-06-02 13:45:4263
64 // Using 10% of codec bitrate for FEC.
65 protection_callback_.fec_rate_bps_ = kCodecBitrateBps / 10;
Ying Wang3b790f32018-01-19 16:58:5766 uint32_t target_bitrate = fec_controller_.UpdateFecRates(
67 kMaxBitrateBps, 30, 0, std::vector<bool>(1, false), 0);
Per69b332d2016-06-02 13:45:4268
69 EXPECT_GT(target_bitrate, 0u);
70 EXPECT_GT(kMaxBitrateBps, target_bitrate);
71
72 // Using as much for codec bitrate as fec rate, new target rate should share
73 // both equally, but only be half of max (since that ceiling should be hit).
74 protection_callback_.fec_rate_bps_ = kCodecBitrateBps;
Ying Wang3b790f32018-01-19 16:58:5775 target_bitrate = fec_controller_.UpdateFecRates(
76 kMaxBitrateBps, 30, 128, std::vector<bool>(1, false), 100);
Per69b332d2016-06-02 13:45:4277 EXPECT_EQ(kMaxBitrateBps / 2, target_bitrate);
78}
79
80TEST_F(ProtectionBitrateCalculatorTest, ProtectsUsingNackBitrate) {
81 static const uint32_t kMaxBitrateBps = 130000;
82
Ying Wang3b790f32018-01-19 16:58:5783 fec_controller_.SetProtectionMethod(false /*enable_fec*/,
84 true /* enable_nack */);
85 fec_controller_.SetEncodingData(640, 480, 1, 1000);
Per69b332d2016-06-02 13:45:4286
Ying Wang3b790f32018-01-19 16:58:5787 uint32_t target_bitrate = fec_controller_.UpdateFecRates(
88 kMaxBitrateBps, 30, 0, std::vector<bool>(1, false), 0);
Per69b332d2016-06-02 13:45:4289
90 EXPECT_EQ(kMaxBitrateBps, target_bitrate);
91
92 // Using as much for codec bitrate as nack rate, new target rate should share
93 // both equally, but only be half of max (since that ceiling should be hit).
94 protection_callback_.nack_rate_bps_ = kMaxBitrateBps;
Ying Wang3b790f32018-01-19 16:58:5795 target_bitrate = fec_controller_.UpdateFecRates(
96 kMaxBitrateBps, 30, 128, std::vector<bool>(1, false), 100);
Per69b332d2016-06-02 13:45:4297 EXPECT_EQ(kMaxBitrateBps / 2, target_bitrate);
98}
99
perkj57c21f92016-06-17 14:27:16100TEST_F(ProtectionBitrateCalculatorTest, NoProtection) {
101 static const uint32_t kMaxBitrateBps = 130000;
102
Ying Wang3b790f32018-01-19 16:58:57103 fec_controller_.SetProtectionMethod(false /*enable_fec*/,
104 false /* enable_nack */);
105 fec_controller_.SetEncodingData(640, 480, 1, 1000);
perkj57c21f92016-06-17 14:27:16106
Ying Wang3b790f32018-01-19 16:58:57107 uint32_t target_bitrate = fec_controller_.UpdateFecRates(
108 kMaxBitrateBps, 30, 128, std::vector<bool>(1, false), 100);
perkj57c21f92016-06-17 14:27:16109 EXPECT_EQ(kMaxBitrateBps, target_bitrate);
110}
111
Per69b332d2016-06-02 13:45:42112} // namespace webrtc