1 /*
2 * Copyright (c) 2012 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 "TwoWayCommunication.h"
12
13 #include <stdio.h>
14 #include <string.h>
15
16 #include <memory>
17
18 #include "api/audio_codecs/builtin_audio_decoder_factory.h"
19 #include "api/audio_codecs/builtin_audio_encoder_factory.h"
20 #include "modules/audio_coding/test/PCMFile.h"
21 #include "test/gtest.h"
22 #include "test/testsupport/file_utils.h"
23
24 namespace webrtc {
25
26 #define MAX_FILE_NAME_LENGTH_BYTE 500
27
TwoWayCommunication()28 TwoWayCommunication::TwoWayCommunication()
29 : _acmA(AudioCodingModule::Create(
30 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))),
31 _acmRefA(AudioCodingModule::Create(
32 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))) {
33 AudioCodingModule::Config config;
34 // The clicks will be more obvious if time-stretching is not allowed.
35 // TODO(henrik.lundin) Really?
36 config.neteq_config.for_test_no_time_stretching = true;
37 config.decoder_factory = CreateBuiltinAudioDecoderFactory();
38 _acmB.reset(AudioCodingModule::Create(config));
39 _acmRefB.reset(AudioCodingModule::Create(config));
40 }
41
~TwoWayCommunication()42 TwoWayCommunication::~TwoWayCommunication() {
43 delete _channel_A2B;
44 delete _channel_B2A;
45 delete _channelRef_A2B;
46 delete _channelRef_B2A;
47 _inFileA.Close();
48 _inFileB.Close();
49 _outFileA.Close();
50 _outFileB.Close();
51 _outFileRefA.Close();
52 _outFileRefB.Close();
53 }
54
SetUpAutotest(AudioEncoderFactory * const encoder_factory,const SdpAudioFormat & format1,const int payload_type1,const SdpAudioFormat & format2,const int payload_type2)55 void TwoWayCommunication::SetUpAutotest(
56 AudioEncoderFactory* const encoder_factory,
57 const SdpAudioFormat& format1,
58 const int payload_type1,
59 const SdpAudioFormat& format2,
60 const int payload_type2) {
61 //--- Set A codecs
62 _acmA->SetEncoder(
63 encoder_factory->MakeAudioEncoder(payload_type1, format1, absl::nullopt));
64 _acmA->SetReceiveCodecs({{payload_type2, format2}});
65
66 //--- Set ref-A codecs
67 _acmRefA->SetEncoder(
68 encoder_factory->MakeAudioEncoder(payload_type1, format1, absl::nullopt));
69 _acmRefA->SetReceiveCodecs({{payload_type2, format2}});
70
71 //--- Set B codecs
72 _acmB->SetEncoder(
73 encoder_factory->MakeAudioEncoder(payload_type2, format2, absl::nullopt));
74 _acmB->SetReceiveCodecs({{payload_type1, format1}});
75
76 //--- Set ref-B codecs
77 _acmRefB->SetEncoder(
78 encoder_factory->MakeAudioEncoder(payload_type2, format2, absl::nullopt));
79 _acmRefB->SetReceiveCodecs({{payload_type1, format1}});
80
81 uint16_t frequencyHz;
82
83 //--- Input A and B
84 std::string in_file_name =
85 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
86 frequencyHz = 16000;
87 _inFileA.Open(in_file_name, frequencyHz, "rb");
88 _inFileB.Open(in_file_name, frequencyHz, "rb");
89
90 //--- Output A
91 std::string output_file_a = webrtc::test::OutputPath() + "outAutotestA.pcm";
92 frequencyHz = 16000;
93 _outFileA.Open(output_file_a, frequencyHz, "wb");
94 std::string output_ref_file_a =
95 webrtc::test::OutputPath() + "ref_outAutotestA.pcm";
96 _outFileRefA.Open(output_ref_file_a, frequencyHz, "wb");
97
98 //--- Output B
99 std::string output_file_b = webrtc::test::OutputPath() + "outAutotestB.pcm";
100 frequencyHz = 16000;
101 _outFileB.Open(output_file_b, frequencyHz, "wb");
102 std::string output_ref_file_b =
103 webrtc::test::OutputPath() + "ref_outAutotestB.pcm";
104 _outFileRefB.Open(output_ref_file_b, frequencyHz, "wb");
105
106 //--- Set A-to-B channel
107 _channel_A2B = new Channel;
108 _acmA->RegisterTransportCallback(_channel_A2B);
109 _channel_A2B->RegisterReceiverACM(_acmB.get());
110 //--- Do the same for the reference
111 _channelRef_A2B = new Channel;
112 _acmRefA->RegisterTransportCallback(_channelRef_A2B);
113 _channelRef_A2B->RegisterReceiverACM(_acmRefB.get());
114
115 //--- Set B-to-A channel
116 _channel_B2A = new Channel;
117 _acmB->RegisterTransportCallback(_channel_B2A);
118 _channel_B2A->RegisterReceiverACM(_acmA.get());
119 //--- Do the same for reference
120 _channelRef_B2A = new Channel;
121 _acmRefB->RegisterTransportCallback(_channelRef_B2A);
122 _channelRef_B2A->RegisterReceiverACM(_acmRefA.get());
123 }
124
Perform()125 void TwoWayCommunication::Perform() {
126 const SdpAudioFormat format1("ISAC", 16000, 1);
127 const SdpAudioFormat format2("L16", 8000, 1);
128 constexpr int payload_type1 = 17, payload_type2 = 18;
129
130 auto encoder_factory = CreateBuiltinAudioEncoderFactory();
131
132 SetUpAutotest(encoder_factory.get(), format1, payload_type1, format2,
133 payload_type2);
134
135 unsigned int msecPassed = 0;
136 unsigned int secPassed = 0;
137
138 int32_t outFreqHzA = _outFileA.SamplingFrequency();
139 int32_t outFreqHzB = _outFileB.SamplingFrequency();
140
141 AudioFrame audioFrame;
142
143 // In the following loop we tests that the code can handle misuse of the APIs.
144 // In the middle of a session with data flowing between two sides, called A
145 // and B, APIs will be called, and the code should continue to run, and be
146 // able to recover.
147 while (!_inFileA.EndOfFile() && !_inFileB.EndOfFile()) {
148 msecPassed += 10;
149 EXPECT_GT(_inFileA.Read10MsData(audioFrame), 0);
150 EXPECT_GE(_acmA->Add10MsData(audioFrame), 0);
151 EXPECT_GE(_acmRefA->Add10MsData(audioFrame), 0);
152
153 EXPECT_GT(_inFileB.Read10MsData(audioFrame), 0);
154
155 EXPECT_GE(_acmB->Add10MsData(audioFrame), 0);
156 EXPECT_GE(_acmRefB->Add10MsData(audioFrame), 0);
157 bool muted;
158 EXPECT_EQ(0, _acmA->PlayoutData10Ms(outFreqHzA, &audioFrame, &muted));
159 ASSERT_FALSE(muted);
160 _outFileA.Write10MsData(audioFrame);
161 EXPECT_EQ(0, _acmRefA->PlayoutData10Ms(outFreqHzA, &audioFrame, &muted));
162 ASSERT_FALSE(muted);
163 _outFileRefA.Write10MsData(audioFrame);
164 EXPECT_EQ(0, _acmB->PlayoutData10Ms(outFreqHzB, &audioFrame, &muted));
165 ASSERT_FALSE(muted);
166 _outFileB.Write10MsData(audioFrame);
167 EXPECT_EQ(0, _acmRefB->PlayoutData10Ms(outFreqHzB, &audioFrame, &muted));
168 ASSERT_FALSE(muted);
169 _outFileRefB.Write10MsData(audioFrame);
170
171 // Update time counters each time a second of data has passed.
172 if (msecPassed >= 1000) {
173 msecPassed = 0;
174 secPassed++;
175 }
176 // Re-register send codec on side B.
177 if (((secPassed % 5) == 4) && (msecPassed >= 990)) {
178 _acmB->SetEncoder(encoder_factory->MakeAudioEncoder(
179 payload_type2, format2, absl::nullopt));
180 }
181 // Initialize receiver on side A.
182 if (((secPassed % 7) == 6) && (msecPassed == 0))
183 EXPECT_EQ(0, _acmA->InitializeReceiver());
184 // Re-register codec on side A.
185 if (((secPassed % 7) == 6) && (msecPassed >= 990)) {
186 _acmA->SetReceiveCodecs({{payload_type2, format2}});
187 }
188 }
189 }
190
191 } // namespace webrtc
192