• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2019 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 <memory>
12 
13 #include "absl/flags/declare.h"
14 #include "absl/flags/flag.h"
15 #include "api/test/create_network_emulation_manager.h"
16 #include "api/test/create_peerconnection_quality_test_fixture.h"
17 #include "api/test/network_emulation_manager.h"
18 #include "api/test/peerconnection_quality_test_fixture.h"
19 #include "api/test/simulated_network.h"
20 #include "api/test/time_controller.h"
21 #include "call/simulated_network.h"
22 #include "test/gtest.h"
23 #include "test/pc/e2e/network_quality_metrics_reporter.h"
24 #include "test/testsupport/file_utils.h"
25 #include "test/testsupport/perf_test.h"
26 
27 ABSL_DECLARE_FLAG(std::string, test_case_prefix);
28 ABSL_DECLARE_FLAG(int, sample_rate_hz);
29 ABSL_DECLARE_FLAG(bool, quick);
30 
31 namespace webrtc {
32 namespace test {
33 
34 using PeerConfigurer =
35     webrtc_pc_e2e::PeerConnectionE2EQualityTestFixture::PeerConfigurer;
36 using RunParams = webrtc_pc_e2e::PeerConnectionE2EQualityTestFixture::RunParams;
37 using AudioConfig =
38     webrtc_pc_e2e::PeerConnectionE2EQualityTestFixture::AudioConfig;
39 
40 namespace {
41 
42 constexpr int kTestDurationMs = 5400;
43 constexpr int kQuickTestDurationMs = 100;
44 
GetMetricTestCaseName()45 std::string GetMetricTestCaseName() {
46   const ::testing::TestInfo* const test_info =
47       ::testing::UnitTest::GetInstance()->current_test_info();
48   std::string test_case_prefix(absl::GetFlag(FLAGS_test_case_prefix));
49   if (test_case_prefix.empty()) {
50     return test_info->name();
51   }
52   return test_case_prefix + "_" + test_info->name();
53 }
54 
55 std::pair<EmulatedNetworkManagerInterface*, EmulatedNetworkManagerInterface*>
CreateTwoNetworkLinks(NetworkEmulationManager * emulation,const BuiltInNetworkBehaviorConfig & config)56 CreateTwoNetworkLinks(NetworkEmulationManager* emulation,
57                       const BuiltInNetworkBehaviorConfig& config) {
58   auto* alice_node = emulation->CreateEmulatedNode(config);
59   auto* bob_node = emulation->CreateEmulatedNode(config);
60 
61   auto* alice_endpoint = emulation->CreateEndpoint(EmulatedEndpointConfig());
62   auto* bob_endpoint = emulation->CreateEndpoint(EmulatedEndpointConfig());
63 
64   emulation->CreateRoute(alice_endpoint, {alice_node}, bob_endpoint);
65   emulation->CreateRoute(bob_endpoint, {bob_node}, alice_endpoint);
66 
67   return {
68       emulation->CreateEmulatedNetworkManagerInterface({alice_endpoint}),
69       emulation->CreateEmulatedNetworkManagerInterface({bob_endpoint}),
70   };
71 }
72 
73 std::unique_ptr<webrtc_pc_e2e::PeerConnectionE2EQualityTestFixture>
CreateTestFixture(const std::string & test_case_name,TimeController & time_controller,std::pair<EmulatedNetworkManagerInterface *,EmulatedNetworkManagerInterface * > network_links,rtc::FunctionView<void (PeerConfigurer *)> alice_configurer,rtc::FunctionView<void (PeerConfigurer *)> bob_configurer)74 CreateTestFixture(const std::string& test_case_name,
75                   TimeController& time_controller,
76                   std::pair<EmulatedNetworkManagerInterface*,
77                             EmulatedNetworkManagerInterface*> network_links,
78                   rtc::FunctionView<void(PeerConfigurer*)> alice_configurer,
79                   rtc::FunctionView<void(PeerConfigurer*)> bob_configurer) {
80   auto fixture = webrtc_pc_e2e::CreatePeerConnectionE2EQualityTestFixture(
81       test_case_name, time_controller, /*audio_quality_analyzer=*/nullptr,
82       /*video_quality_analyzer=*/nullptr);
83   fixture->AddPeer(network_links.first->network_thread(),
84                    network_links.first->network_manager(), alice_configurer);
85   fixture->AddPeer(network_links.second->network_thread(),
86                    network_links.second->network_manager(), bob_configurer);
87   fixture->AddQualityMetricsReporter(
88       std::make_unique<webrtc_pc_e2e::NetworkQualityMetricsReporter>(
89           network_links.first, network_links.second));
90   return fixture;
91 }
92 
FileSampleRateSuffix()93 std::string FileSampleRateSuffix() {
94   return std::to_string(absl::GetFlag(FLAGS_sample_rate_hz) / 1000);
95 }
96 
AudioInputFile()97 std::string AudioInputFile() {
98   return test::ResourcePath("voice_engine/audio_tiny" + FileSampleRateSuffix(),
99                             "wav");
100 }
101 
AudioOutputFile()102 std::string AudioOutputFile() {
103   const ::testing::TestInfo* const test_info =
104       ::testing::UnitTest::GetInstance()->current_test_info();
105   return webrtc::test::OutputPath() + "PCLowBandwidth_" + test_info->name() +
106          "_" + FileSampleRateSuffix() + ".wav";
107 }
108 
PerfResultsOutputFile()109 std::string PerfResultsOutputFile() {
110   return webrtc::test::OutputPath() + "PCLowBandwidth_perf_" +
111          FileSampleRateSuffix() + ".pb";
112 }
113 
LogTestResults()114 void LogTestResults() {
115   std::string perf_results_output_file = PerfResultsOutputFile();
116   EXPECT_TRUE(webrtc::test::WritePerfResults(perf_results_output_file));
117 
118   const ::testing::TestInfo* const test_info =
119       ::testing::UnitTest::GetInstance()->current_test_info();
120 
121   // Output information about the input and output audio files so that further
122   // processing can be done by an external process.
123   printf("TEST %s %s %s %s\n", test_info->name(), AudioInputFile().c_str(),
124          AudioOutputFile().c_str(), perf_results_output_file.c_str());
125 }
126 
127 }  // namespace
128 
TEST(PCLowBandwidthAudioTest,PCGoodNetworkHighBitrate)129 TEST(PCLowBandwidthAudioTest, PCGoodNetworkHighBitrate) {
130   std::unique_ptr<NetworkEmulationManager> network_emulation_manager =
131       CreateNetworkEmulationManager();
132   auto fixture = CreateTestFixture(
133       GetMetricTestCaseName(), *network_emulation_manager->time_controller(),
134       CreateTwoNetworkLinks(network_emulation_manager.get(),
135                             BuiltInNetworkBehaviorConfig()),
136       [](PeerConfigurer* alice) {
137         AudioConfig audio;
138         audio.stream_label = "alice-audio";
139         audio.mode = AudioConfig::Mode::kFile;
140         audio.input_file_name = AudioInputFile();
141         audio.output_dump_file_name = AudioOutputFile();
142         audio.sampling_frequency_in_hz = absl::GetFlag(FLAGS_sample_rate_hz);
143         alice->SetAudioConfig(std::move(audio));
144       },
145       [](PeerConfigurer* bob) {});
146   fixture->Run(RunParams(TimeDelta::Millis(
147       absl::GetFlag(FLAGS_quick) ? kQuickTestDurationMs : kTestDurationMs)));
148   LogTestResults();
149 }
150 
TEST(PCLowBandwidthAudioTest,PC40kbpsNetwork)151 TEST(PCLowBandwidthAudioTest, PC40kbpsNetwork) {
152   std::unique_ptr<NetworkEmulationManager> network_emulation_manager =
153       CreateNetworkEmulationManager();
154   BuiltInNetworkBehaviorConfig config;
155   config.link_capacity_kbps = 40;
156   config.queue_length_packets = 1500;
157   config.queue_delay_ms = 400;
158   config.loss_percent = 1;
159   auto fixture = CreateTestFixture(
160       GetMetricTestCaseName(), *network_emulation_manager->time_controller(),
161       CreateTwoNetworkLinks(network_emulation_manager.get(), config),
162       [](PeerConfigurer* alice) {
163         AudioConfig audio;
164         audio.stream_label = "alice-audio";
165         audio.mode = AudioConfig::Mode::kFile;
166         audio.input_file_name = AudioInputFile();
167         audio.output_dump_file_name = AudioOutputFile();
168         audio.sampling_frequency_in_hz = absl::GetFlag(FLAGS_sample_rate_hz);
169         alice->SetAudioConfig(std::move(audio));
170       },
171       [](PeerConfigurer* bob) {});
172   fixture->Run(RunParams(TimeDelta::Millis(
173       absl::GetFlag(FLAGS_quick) ? kQuickTestDurationMs : kTestDurationMs)));
174   LogTestResults();
175 }
176 
177 }  // namespace test
178 }  // namespace webrtc
179