1 /*
2 * Copyright (c) 2020 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
11 #include "modules/audio_coding/audio_network_adaptor/frame_length_controller_v2.h"
12
13 #include <algorithm>
14 #include <memory>
15
16 #include "modules/audio_coding/audio_network_adaptor/controller.h"
17 #include "modules/audio_coding/audio_network_adaptor/include/audio_network_adaptor_config.h"
18 #include "test/gtest.h"
19
20 namespace webrtc {
21 namespace {
22
23 constexpr int kANASupportedFrameLengths[] = {20, 40, 60, 120};
24 constexpr int kMinPayloadBitrateBps = 16000;
25
26 } // namespace
27
28 class FrameLengthControllerV2Test : public testing::Test {
29 protected:
GetDecision()30 AudioEncoderRuntimeConfig GetDecision() {
31 AudioEncoderRuntimeConfig config;
32 controller_->MakeDecision(&config);
33 return config;
34 }
35
SetOverhead(int overhead_bytes_per_packet)36 void SetOverhead(int overhead_bytes_per_packet) {
37 overhead_bytes_per_packet_ = overhead_bytes_per_packet;
38 Controller::NetworkMetrics metrics;
39 metrics.overhead_bytes_per_packet = overhead_bytes_per_packet;
40 controller_->UpdateNetworkMetrics(metrics);
41 }
42
SetTargetBitrate(int target_audio_bitrate_bps)43 void SetTargetBitrate(int target_audio_bitrate_bps) {
44 target_audio_bitrate_bps_ = target_audio_bitrate_bps;
45 Controller::NetworkMetrics metrics;
46 metrics.target_audio_bitrate_bps = target_audio_bitrate_bps;
47 controller_->UpdateNetworkMetrics(metrics);
48 }
49
SetUplinkBandwidth(int uplink_bandwidth_bps)50 void SetUplinkBandwidth(int uplink_bandwidth_bps) {
51 Controller::NetworkMetrics metrics;
52 metrics.uplink_bandwidth_bps = uplink_bandwidth_bps;
53 controller_->UpdateNetworkMetrics(metrics);
54 }
55
ExpectFrameLengthDecision(int expected_frame_length_ms)56 void ExpectFrameLengthDecision(int expected_frame_length_ms) {
57 auto config = GetDecision();
58 EXPECT_EQ(*config.frame_length_ms, expected_frame_length_ms);
59 }
60
61 std::unique_ptr<FrameLengthControllerV2> controller_ =
62 std::make_unique<FrameLengthControllerV2>(kANASupportedFrameLengths,
63 kMinPayloadBitrateBps,
64 /*use_slow_adaptation=*/false);
65 absl::optional<int> target_audio_bitrate_bps_;
66 absl::optional<int> overhead_bytes_per_packet_;
67 };
68
69 // Don't return any decision if we haven't received all required network
70 // metrics.
TEST_F(FrameLengthControllerV2Test,RequireNetworkMetrics)71 TEST_F(FrameLengthControllerV2Test, RequireNetworkMetrics) {
72 auto config = GetDecision();
73 EXPECT_FALSE(config.bitrate_bps);
74 EXPECT_FALSE(config.frame_length_ms);
75
76 SetOverhead(30);
77 config = GetDecision();
78 EXPECT_FALSE(config.frame_length_ms);
79
80 SetTargetBitrate(32000);
81 config = GetDecision();
82 EXPECT_FALSE(config.frame_length_ms);
83
84 SetUplinkBandwidth(32000);
85 config = GetDecision();
86 EXPECT_TRUE(config.frame_length_ms);
87 }
88
TEST_F(FrameLengthControllerV2Test,UseFastAdaptation)89 TEST_F(FrameLengthControllerV2Test, UseFastAdaptation) {
90 SetOverhead(50);
91 SetTargetBitrate(50000);
92 SetUplinkBandwidth(50000);
93 ExpectFrameLengthDecision(20);
94
95 SetTargetBitrate(20000);
96 ExpectFrameLengthDecision(120);
97
98 SetTargetBitrate(30000);
99 ExpectFrameLengthDecision(40);
100
101 SetTargetBitrate(25000);
102 ExpectFrameLengthDecision(60);
103 }
104
TEST_F(FrameLengthControllerV2Test,UseSlowAdaptation)105 TEST_F(FrameLengthControllerV2Test, UseSlowAdaptation) {
106 controller_ = std::make_unique<FrameLengthControllerV2>(
107 kANASupportedFrameLengths, kMinPayloadBitrateBps,
108 /*use_slow_adaptation=*/true);
109 SetOverhead(50);
110 SetTargetBitrate(50000);
111 SetUplinkBandwidth(20000);
112 ExpectFrameLengthDecision(120);
113
114 SetUplinkBandwidth(30000);
115 ExpectFrameLengthDecision(40);
116
117 SetUplinkBandwidth(40000);
118 ExpectFrameLengthDecision(20);
119 }
120
121 } // namespace webrtc
122