2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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.
11 #include "TwoWayCommunication.h"
21 #include "gtest/gtest.h"
22 #include "webrtc/engine_configurations.h"
23 #include "webrtc/common_types.h"
24 #include "webrtc/modules/audio_coding/main/test/PCMFile.h"
25 #include "webrtc/modules/audio_coding/main/test/utility.h"
26 #include "webrtc/system_wrappers/interface/trace.h"
27 #include "webrtc/test/testsupport/fileutils.h"
31 #define MAX_FILE_NAME_LENGTH_BYTE 500
33 TwoWayCommunication::TwoWayCommunication(int testMode)
34 : _acmA(AudioCodingModule::Create(1)),
35 _acmB(AudioCodingModule::Create(2)),
36 _acmRefA(AudioCodingModule::Create(3)),
37 _acmRefB(AudioCodingModule::Create(4)),
38 _testMode(testMode) {}
40 TwoWayCommunication::~TwoWayCommunication() {
43 delete _channelRef_A2B;
44 delete _channelRef_B2A;
45 #ifdef WEBRTC_DTMF_DETECTION
46 if (_dtmfDetectorA != NULL) {
47 delete _dtmfDetectorA;
49 if (_dtmfDetectorB != NULL) {
50 delete _dtmfDetectorB;
61 void TwoWayCommunication::ChooseCodec(uint8_t* codecID_A,
63 scoped_ptr<AudioCodingModule> tmpACM(AudioCodingModule::Create(0));
64 uint8_t noCodec = tmpACM->NumberOfCodecs();
66 printf("List of Supported Codecs\n");
67 printf("========================\n");
68 for (uint8_t codecCntr = 0; codecCntr < noCodec; codecCntr++) {
69 EXPECT_EQ(tmpACM->Codec(codecCntr, &codecInst), 0);
70 printf("%d- %s\n", codecCntr, codecInst.plname);
72 printf("\nChoose a send codec for side A [0]: ");
74 EXPECT_TRUE(fgets(myStr, 10, stdin) != NULL);
75 *codecID_A = (uint8_t) atoi(myStr);
77 printf("\nChoose a send codec for side B [0]: ");
78 EXPECT_TRUE(fgets(myStr, 10, stdin) != NULL);
79 *codecID_B = (uint8_t) atoi(myStr);
84 void TwoWayCommunication::SetUp() {
88 ChooseCodec(&codecID_A, &codecID_B);
89 CodecInst codecInst_A;
90 CodecInst codecInst_B;
92 EXPECT_EQ(0, _acmA->Codec(codecID_A, &codecInst_A));
93 EXPECT_EQ(0, _acmB->Codec(codecID_B, &codecInst_B));
94 EXPECT_EQ(0, _acmA->Codec(6, &dummyCodec));
97 EXPECT_EQ(0, _acmA->RegisterSendCodec(codecInst_A));
98 EXPECT_EQ(0, _acmA->RegisterReceiveCodec(codecInst_B));
99 #ifdef WEBRTC_DTMF_DETECTION
100 _dtmfDetectorA = new(DTMFDetector);
101 EXPECT_GT(_acmA->RegisterIncomingMessagesCallback(_dtmfDetectorA, ACMUSA),
104 //--- Set ref-A codecs
105 EXPECT_EQ(0, _acmRefA->RegisterSendCodec(codecInst_A));
106 EXPECT_EQ(0, _acmRefA->RegisterReceiveCodec(codecInst_B));
109 EXPECT_EQ(0, _acmB->RegisterSendCodec(codecInst_B));
110 EXPECT_EQ(0, _acmB->RegisterReceiveCodec(codecInst_A));
111 #ifdef WEBRTC_DTMF_DETECTION
112 _dtmfDetectorB = new(DTMFDetector);
113 EXPECT_GT(_acmB->RegisterIncomingMessagesCallback(_dtmfDetectorB, ACMUSA),
117 //--- Set ref-B codecs
118 EXPECT_EQ(0, _acmRefB->RegisterSendCodec(codecInst_B));
119 EXPECT_EQ(0, _acmRefB->RegisterReceiveCodec(codecInst_A));
121 uint16_t frequencyHz;
124 std::string in_file_name = webrtc::test::ResourcePath(
125 "audio_coding/testfile32kHz", "pcm");
127 printf("Enter input file at side A [%s]: ", in_file_name.c_str());
128 PCMFile::ChooseFile(&in_file_name, 499, &frequencyHz);
129 _inFileA.Open(in_file_name, frequencyHz, "rb");
132 std::string out_file_a = webrtc::test::OutputPath() + "outA.pcm";
133 printf("Output file at side A: %s\n", out_file_a.c_str());
134 printf("Sampling frequency (in Hz) of the above file: %u\n", frequencyHz);
135 _outFileA.Open(out_file_a, frequencyHz, "wb");
136 std::string ref_file_name = webrtc::test::OutputPath() + "ref_outA.pcm";
137 _outFileRefA.Open(ref_file_name, frequencyHz, "wb");
140 in_file_name = webrtc::test::ResourcePath("audio_coding/testfile32kHz",
143 printf("\n\nEnter input file at side B [%s]: ", in_file_name.c_str());
144 PCMFile::ChooseFile(&in_file_name, 499, &frequencyHz);
145 _inFileB.Open(in_file_name, frequencyHz, "rb");
148 std::string out_file_b = webrtc::test::OutputPath() + "outB.pcm";
149 printf("Output file at side B: %s\n", out_file_b.c_str());
150 printf("Sampling frequency (in Hz) of the above file: %u\n", frequencyHz);
151 _outFileB.Open(out_file_b, frequencyHz, "wb");
152 ref_file_name = webrtc::test::OutputPath() + "ref_outB.pcm";
153 _outFileRefB.Open(ref_file_name, frequencyHz, "wb");
155 //--- Set A-to-B channel
156 _channel_A2B = new Channel;
157 _acmA->RegisterTransportCallback(_channel_A2B);
158 _channel_A2B->RegisterReceiverACM(_acmB.get());
159 //--- Do the same for the reference
160 _channelRef_A2B = new Channel;
161 _acmRefA->RegisterTransportCallback(_channelRef_A2B);
162 _channelRef_A2B->RegisterReceiverACM(_acmRefB.get());
164 //--- Set B-to-A channel
165 _channel_B2A = new Channel;
166 _acmB->RegisterTransportCallback(_channel_B2A);
167 _channel_B2A->RegisterReceiverACM(_acmA.get());
168 //--- Do the same for reference
169 _channelRef_B2A = new Channel;
170 _acmRefB->RegisterTransportCallback(_channelRef_B2A);
171 _channelRef_B2A->RegisterReceiverACM(_acmRefA.get());
173 // The clicks will be more obvious when we
175 EXPECT_EQ(_acmB->SetPlayoutMode(fax), 0);
176 EXPECT_EQ(_acmRefB->SetPlayoutMode(fax), 0);
179 void TwoWayCommunication::SetUpAutotest() {
180 CodecInst codecInst_A;
181 CodecInst codecInst_B;
182 CodecInst dummyCodec;
184 EXPECT_EQ(0, _acmA->Codec("ISAC", &codecInst_A, 16000, 1));
185 EXPECT_EQ(0, _acmB->Codec("L16", &codecInst_B, 8000, 1));
186 EXPECT_EQ(0, _acmA->Codec(6, &dummyCodec));
189 EXPECT_EQ(0, _acmA->RegisterSendCodec(codecInst_A));
190 EXPECT_EQ(0, _acmA->RegisterReceiveCodec(codecInst_B));
191 #ifdef WEBRTC_DTMF_DETECTION
192 _dtmfDetectorA = new(DTMFDetector);
193 EXPECT_EQ(0, _acmA->RegisterIncomingMessagesCallback(_dtmfDetectorA, ACMUSA));
196 //--- Set ref-A codecs
197 EXPECT_GT(_acmRefA->RegisterSendCodec(codecInst_A), -1);
198 EXPECT_GT(_acmRefA->RegisterReceiveCodec(codecInst_B), -1);
201 EXPECT_GT(_acmB->RegisterSendCodec(codecInst_B), -1);
202 EXPECT_GT(_acmB->RegisterReceiveCodec(codecInst_A), -1);
203 #ifdef WEBRTC_DTMF_DETECTION
204 _dtmfDetectorB = new(DTMFDetector);
205 EXPECT_EQ(0, _acmB->RegisterIncomingMessagesCallback(_dtmfDetectorB, ACMUSA));
208 //--- Set ref-B codecs
209 EXPECT_EQ(0, _acmRefB->RegisterSendCodec(codecInst_B));
210 EXPECT_EQ(0, _acmRefB->RegisterReceiveCodec(codecInst_A));
212 uint16_t frequencyHz;
215 std::string in_file_name = webrtc::test::ResourcePath(
216 "audio_coding/testfile32kHz", "pcm");
218 _inFileA.Open(in_file_name, frequencyHz, "rb");
219 _inFileB.Open(in_file_name, frequencyHz, "rb");
222 std::string output_file_a = webrtc::test::OutputPath() + "outAutotestA.pcm";
224 _outFileA.Open(output_file_a, frequencyHz, "wb");
225 std::string output_ref_file_a = webrtc::test::OutputPath()
226 + "ref_outAutotestA.pcm";
227 _outFileRefA.Open(output_ref_file_a, frequencyHz, "wb");
230 std::string output_file_b = webrtc::test::OutputPath() + "outAutotestB.pcm";
232 _outFileB.Open(output_file_b, frequencyHz, "wb");
233 std::string output_ref_file_b = webrtc::test::OutputPath()
234 + "ref_outAutotestB.pcm";
235 _outFileRefB.Open(output_ref_file_b, frequencyHz, "wb");
237 //--- Set A-to-B channel
238 _channel_A2B = new Channel;
239 _acmA->RegisterTransportCallback(_channel_A2B);
240 _channel_A2B->RegisterReceiverACM(_acmB.get());
241 //--- Do the same for the reference
242 _channelRef_A2B = new Channel;
243 _acmRefA->RegisterTransportCallback(_channelRef_A2B);
244 _channelRef_A2B->RegisterReceiverACM(_acmRefB.get());
246 //--- Set B-to-A channel
247 _channel_B2A = new Channel;
248 _acmB->RegisterTransportCallback(_channel_B2A);
249 _channel_B2A->RegisterReceiverACM(_acmA.get());
250 //--- Do the same for reference
251 _channelRef_B2A = new Channel;
252 _acmRefB->RegisterTransportCallback(_channelRef_B2A);
253 _channelRef_B2A->RegisterReceiverACM(_acmRefA.get());
255 // The clicks will be more obvious when we
257 EXPECT_EQ(0, _acmB->SetPlayoutMode(fax));
258 EXPECT_EQ(0, _acmRefB->SetPlayoutMode(fax));
261 void TwoWayCommunication::Perform() {
262 if (_testMode == 0) {
267 unsigned int msecPassed = 0;
268 unsigned int secPassed = 0;
270 int32_t outFreqHzA = _outFileA.SamplingFrequency();
271 int32_t outFreqHzB = _outFileB.SamplingFrequency();
273 AudioFrame audioFrame;
275 CodecInst codecInst_B;
278 EXPECT_EQ(0, _acmB->SendCodec(&codecInst_B));
280 // In the following loop we tests that the code can handle misuse of the APIs.
281 // In the middle of a session with data flowing between two sides, called A
282 // and B, APIs will be called, like ResetEncoder(), and the code should
283 // continue to run, and be able to recover.
284 bool expect_error_add = false;
285 bool expect_error_process = false;
286 while (!_inFileA.EndOfFile() && !_inFileB.EndOfFile()) {
288 EXPECT_GT(_inFileA.Read10MsData(audioFrame), 0);
289 EXPECT_EQ(0, _acmA->Add10MsData(audioFrame));
290 EXPECT_EQ(0, _acmRefA->Add10MsData(audioFrame));
292 EXPECT_GT(_inFileB.Read10MsData(audioFrame), 0);
294 // Expect call to pass except for the time when no send codec is registered.
295 if (!expect_error_add) {
296 EXPECT_EQ(0, _acmB->Add10MsData(audioFrame));
298 EXPECT_EQ(-1, _acmB->Add10MsData(audioFrame));
300 // Expect to pass except for the time when there either is no send codec
301 // registered, or no receive codec.
302 if (!expect_error_process) {
303 EXPECT_GT(_acmB->Process(), -1);
305 EXPECT_EQ(_acmB->Process(), -1);
307 EXPECT_EQ(0, _acmRefB->Add10MsData(audioFrame));
308 EXPECT_GT(_acmA->Process(), -1);
309 EXPECT_GT(_acmRefA->Process(), -1);
310 EXPECT_GT(_acmRefB->Process(), -1);
311 EXPECT_EQ(0, _acmA->PlayoutData10Ms(outFreqHzA, &audioFrame));
312 _outFileA.Write10MsData(audioFrame);
313 EXPECT_EQ(0, _acmRefA->PlayoutData10Ms(outFreqHzA, &audioFrame));
314 _outFileRefA.Write10MsData(audioFrame);
315 EXPECT_EQ(0, _acmB->PlayoutData10Ms(outFreqHzB, &audioFrame));
316 _outFileB.Write10MsData(audioFrame);
317 EXPECT_EQ(0, _acmRefB->PlayoutData10Ms(outFreqHzB, &audioFrame));
318 _outFileRefB.Write10MsData(audioFrame);
320 // Update time counters each time a second of data has passed.
321 if (msecPassed >= 1000) {
325 // Call RestEncoder for ACM on side A, and InitializeSender for ACM on
327 if (((secPassed % 5) == 4) && (msecPassed == 0)) {
328 EXPECT_EQ(0, _acmA->ResetEncoder());
329 EXPECT_EQ(0, _acmB->InitializeSender());
330 expect_error_add = true;
331 expect_error_process = true;
333 // Re-register send codec on side B.
334 if (((secPassed % 5) == 4) && (msecPassed >= 990)) {
335 EXPECT_EQ(0, _acmB->RegisterSendCodec(codecInst_B));
336 EXPECT_EQ(0, _acmB->SendCodec(&dummy));
337 expect_error_add = false;
338 expect_error_process = false;
340 // Reset decoder on side B, and initialize receiver on side A.
341 if (((secPassed % 7) == 6) && (msecPassed == 0)) {
342 EXPECT_EQ(0, _acmB->ResetDecoder());
343 EXPECT_EQ(0, _acmA->InitializeReceiver());
345 // Re-register codec on side A.
346 if (((secPassed % 7) == 6) && (msecPassed >= 990)) {
347 EXPECT_EQ(0, _acmA->RegisterReceiveCodec(codecInst_B));
352 } // namespace webrtc