1 /*
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
11 #include "modules/audio_coding/audio_network_adaptor/channel_controller.h"
12
13 #include <memory>
14
15 #include "test/gtest.h"
16
17 namespace webrtc {
18
19 namespace {
20
21 constexpr int kNumChannels = 2;
22 constexpr int kChannel1To2BandwidthBps = 31000;
23 constexpr int kChannel2To1BandwidthBps = 29000;
24 constexpr int kMediumBandwidthBps =
25 (kChannel1To2BandwidthBps + kChannel2To1BandwidthBps) / 2;
26
CreateChannelController(int init_channels)27 std::unique_ptr<ChannelController> CreateChannelController(int init_channels) {
28 std::unique_ptr<ChannelController> controller(
29 new ChannelController(ChannelController::Config(
30 kNumChannels, init_channels, kChannel1To2BandwidthBps,
31 kChannel2To1BandwidthBps)));
32 return controller;
33 }
34
CheckDecision(ChannelController * controller,const absl::optional<int> & uplink_bandwidth_bps,size_t expected_num_channels)35 void CheckDecision(ChannelController* controller,
36 const absl::optional<int>& uplink_bandwidth_bps,
37 size_t expected_num_channels) {
38 if (uplink_bandwidth_bps) {
39 Controller::NetworkMetrics network_metrics;
40 network_metrics.uplink_bandwidth_bps = uplink_bandwidth_bps;
41 controller->UpdateNetworkMetrics(network_metrics);
42 }
43 AudioEncoderRuntimeConfig config;
44 controller->MakeDecision(&config);
45 EXPECT_EQ(expected_num_channels, config.num_channels);
46 }
47
48 } // namespace
49
TEST(ChannelControllerTest,OutputInitValueWhenUplinkBandwidthUnknown)50 TEST(ChannelControllerTest, OutputInitValueWhenUplinkBandwidthUnknown) {
51 constexpr int kInitChannels = 2;
52 auto controller = CreateChannelController(kInitChannels);
53 CheckDecision(controller.get(), absl::nullopt, kInitChannels);
54 }
55
TEST(ChannelControllerTest,SwitchTo2ChannelsOnHighUplinkBandwidth)56 TEST(ChannelControllerTest, SwitchTo2ChannelsOnHighUplinkBandwidth) {
57 constexpr int kInitChannels = 1;
58 auto controller = CreateChannelController(kInitChannels);
59 // Use high bandwidth to check output switch to 2.
60 CheckDecision(controller.get(), kChannel1To2BandwidthBps, 2);
61 }
62
TEST(ChannelControllerTest,SwitchTo1ChannelOnLowUplinkBandwidth)63 TEST(ChannelControllerTest, SwitchTo1ChannelOnLowUplinkBandwidth) {
64 constexpr int kInitChannels = 2;
65 auto controller = CreateChannelController(kInitChannels);
66 // Use low bandwidth to check output switch to 1.
67 CheckDecision(controller.get(), kChannel2To1BandwidthBps, 1);
68 }
69
TEST(ChannelControllerTest,Maintain1ChannelOnMediumUplinkBandwidth)70 TEST(ChannelControllerTest, Maintain1ChannelOnMediumUplinkBandwidth) {
71 constexpr int kInitChannels = 1;
72 auto controller = CreateChannelController(kInitChannels);
73 // Use between-thresholds bandwidth to check output remains at 1.
74 CheckDecision(controller.get(), kMediumBandwidthBps, 1);
75 }
76
TEST(ChannelControllerTest,Maintain2ChannelsOnMediumUplinkBandwidth)77 TEST(ChannelControllerTest, Maintain2ChannelsOnMediumUplinkBandwidth) {
78 constexpr int kInitChannels = 2;
79 auto controller = CreateChannelController(kInitChannels);
80 // Use between-thresholds bandwidth to check output remains at 2.
81 CheckDecision(controller.get(), kMediumBandwidthBps, 2);
82 }
83
TEST(ChannelControllerTest,CheckBehaviorOnChangingUplinkBandwidth)84 TEST(ChannelControllerTest, CheckBehaviorOnChangingUplinkBandwidth) {
85 constexpr int kInitChannels = 1;
86 auto controller = CreateChannelController(kInitChannels);
87
88 // Use between-thresholds bandwidth to check output remains at 1.
89 CheckDecision(controller.get(), kMediumBandwidthBps, 1);
90
91 // Use high bandwidth to check output switch to 2.
92 CheckDecision(controller.get(), kChannel1To2BandwidthBps, 2);
93
94 // Use between-thresholds bandwidth to check output remains at 2.
95 CheckDecision(controller.get(), kMediumBandwidthBps, 2);
96
97 // Use low bandwidth to check output switch to 1.
98 CheckDecision(controller.get(), kChannel2To1BandwidthBps, 1);
99 }
100
101 } // namespace webrtc
102