blob: 8b75c7b6d54a5715ed55658331ed89a57fb628b7 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
leozwang@webrtc.org91b359e2012-02-28 17:26:14 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
kjellander@webrtc.org543c3ea2011-11-23 12:20:35 +000011#include "TwoWayCommunication.h"
12
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <ctype.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000014#include <stdio.h>
15#include <string.h>
16
17#ifdef WIN32
18#include <Windows.h>
19#endif
20
kjellander@webrtc.org3c0aae12014-09-04 09:55:40 +000021#include "testing/gtest/include/gtest/gtest.h"
turaj@webrtc.org6ea3d1c2013-10-02 21:44:33 +000022#include "webrtc/engine_configurations.h"
turaj@webrtc.org6ea3d1c2013-10-02 21:44:33 +000023#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"
pbos@webrtc.org2ab209e2013-08-09 08:49:48 +000027#include "webrtc/test/testsupport/fileutils.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000028
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000029namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000030
31#define MAX_FILE_NAME_LENGTH_BYTE 500
32
henrik.lundin@webrtc.orgadaf8092014-04-17 08:29:10 +000033TwoWayCommunication::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) {}
niklase@google.com470e71d2011-07-07 08:21:25 +000039
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000040TwoWayCommunication::~TwoWayCommunication() {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000041 delete _channel_A2B;
42 delete _channel_B2A;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000043 delete _channelRef_A2B;
44 delete _channelRef_B2A;
niklase@google.com470e71d2011-07-07 08:21:25 +000045#ifdef WEBRTC_DTMF_DETECTION
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000046 if (_dtmfDetectorA != NULL) {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000047 delete _dtmfDetectorA;
48 }
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000049 if (_dtmfDetectorB != NULL) {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000050 delete _dtmfDetectorB;
51 }
niklase@google.com470e71d2011-07-07 08:21:25 +000052#endif
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000053 _inFileA.Close();
54 _inFileB.Close();
55 _outFileA.Close();
56 _outFileB.Close();
57 _outFileRefA.Close();
58 _outFileRefB.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +000059}
60
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000061void TwoWayCommunication::ChooseCodec(uint8_t* codecID_A,
62 uint8_t* codecID_B) {
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000063 rtc::scoped_ptr<AudioCodingModule> tmpACM(AudioCodingModule::Create(0));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000064 uint8_t noCodec = tmpACM->NumberOfCodecs();
65 CodecInst codecInst;
66 printf("List of Supported Codecs\n");
67 printf("========================\n");
68 for (uint8_t codecCntr = 0; codecCntr < noCodec; codecCntr++) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000069 EXPECT_EQ(tmpACM->Codec(codecCntr, &codecInst), 0);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000070 printf("%d- %s\n", codecCntr, codecInst.plname);
71 }
72 printf("\nChoose a send codec for side A [0]: ");
73 char myStr[15] = "";
74 EXPECT_TRUE(fgets(myStr, 10, stdin) != NULL);
75 *codecID_A = (uint8_t) atoi(myStr);
niklase@google.com470e71d2011-07-07 08:21:25 +000076
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000077 printf("\nChoose a send codec for side B [0]: ");
78 EXPECT_TRUE(fgets(myStr, 10, stdin) != NULL);
79 *codecID_B = (uint8_t) atoi(myStr);
niklase@google.com470e71d2011-07-07 08:21:25 +000080
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000081 printf("\n");
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000082}
niklase@google.com470e71d2011-07-07 08:21:25 +000083
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000084void TwoWayCommunication::SetUp() {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000085 uint8_t codecID_A;
86 uint8_t codecID_B;
87
88 ChooseCodec(&codecID_A, &codecID_B);
89 CodecInst codecInst_A;
90 CodecInst codecInst_B;
91 CodecInst dummyCodec;
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000092 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));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000095
96 //--- Set A codecs
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000097 EXPECT_EQ(0, _acmA->RegisterSendCodec(codecInst_A));
98 EXPECT_EQ(0, _acmA->RegisterReceiveCodec(codecInst_B));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000099 //--- Set ref-A codecs
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000100 EXPECT_EQ(0, _acmRefA->RegisterSendCodec(codecInst_A));
101 EXPECT_EQ(0, _acmRefA->RegisterReceiveCodec(codecInst_B));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000102
103 //--- Set B codecs
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000104 EXPECT_EQ(0, _acmB->RegisterSendCodec(codecInst_B));
105 EXPECT_EQ(0, _acmB->RegisterReceiveCodec(codecInst_A));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000106
107 //--- Set ref-B codecs
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000108 EXPECT_EQ(0, _acmRefB->RegisterSendCodec(codecInst_B));
109 EXPECT_EQ(0, _acmRefB->RegisterReceiveCodec(codecInst_A));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000110
111 uint16_t frequencyHz;
112
113 //--- Input A
114 std::string in_file_name = webrtc::test::ResourcePath(
115 "audio_coding/testfile32kHz", "pcm");
116 frequencyHz = 32000;
117 printf("Enter input file at side A [%s]: ", in_file_name.c_str());
118 PCMFile::ChooseFile(&in_file_name, 499, &frequencyHz);
119 _inFileA.Open(in_file_name, frequencyHz, "rb");
120
121 //--- Output A
122 std::string out_file_a = webrtc::test::OutputPath() + "outA.pcm";
123 printf("Output file at side A: %s\n", out_file_a.c_str());
124 printf("Sampling frequency (in Hz) of the above file: %u\n", frequencyHz);
125 _outFileA.Open(out_file_a, frequencyHz, "wb");
126 std::string ref_file_name = webrtc::test::OutputPath() + "ref_outA.pcm";
127 _outFileRefA.Open(ref_file_name, frequencyHz, "wb");
128
129 //--- Input B
130 in_file_name = webrtc::test::ResourcePath("audio_coding/testfile32kHz",
131 "pcm");
132 frequencyHz = 32000;
133 printf("\n\nEnter input file at side B [%s]: ", in_file_name.c_str());
134 PCMFile::ChooseFile(&in_file_name, 499, &frequencyHz);
135 _inFileB.Open(in_file_name, frequencyHz, "rb");
136
137 //--- Output B
138 std::string out_file_b = webrtc::test::OutputPath() + "outB.pcm";
139 printf("Output file at side B: %s\n", out_file_b.c_str());
140 printf("Sampling frequency (in Hz) of the above file: %u\n", frequencyHz);
141 _outFileB.Open(out_file_b, frequencyHz, "wb");
142 ref_file_name = webrtc::test::OutputPath() + "ref_outB.pcm";
143 _outFileRefB.Open(ref_file_name, frequencyHz, "wb");
144
145 //--- Set A-to-B channel
146 _channel_A2B = new Channel;
147 _acmA->RegisterTransportCallback(_channel_A2B);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000148 _channel_A2B->RegisterReceiverACM(_acmB.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000149 //--- Do the same for the reference
150 _channelRef_A2B = new Channel;
151 _acmRefA->RegisterTransportCallback(_channelRef_A2B);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000152 _channelRef_A2B->RegisterReceiverACM(_acmRefB.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000153
154 //--- Set B-to-A channel
155 _channel_B2A = new Channel;
156 _acmB->RegisterTransportCallback(_channel_B2A);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000157 _channel_B2A->RegisterReceiverACM(_acmA.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000158 //--- Do the same for reference
159 _channelRef_B2A = new Channel;
160 _acmRefB->RegisterTransportCallback(_channelRef_B2A);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000161 _channelRef_B2A->RegisterReceiverACM(_acmRefA.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000162
163 // The clicks will be more obvious when we
164 // are in FAX mode.
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000165 EXPECT_EQ(_acmB->SetPlayoutMode(fax), 0);
166 EXPECT_EQ(_acmRefB->SetPlayoutMode(fax), 0);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000167}
168
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000169void TwoWayCommunication::SetUpAutotest() {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000170 CodecInst codecInst_A;
171 CodecInst codecInst_B;
172 CodecInst dummyCodec;
173
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000174 EXPECT_EQ(0, _acmA->Codec("ISAC", &codecInst_A, 16000, 1));
175 EXPECT_EQ(0, _acmB->Codec("L16", &codecInst_B, 8000, 1));
176 EXPECT_EQ(0, _acmA->Codec(6, &dummyCodec));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000177
178 //--- Set A codecs
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000179 EXPECT_EQ(0, _acmA->RegisterSendCodec(codecInst_A));
180 EXPECT_EQ(0, _acmA->RegisterReceiveCodec(codecInst_B));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000181
182 //--- Set ref-A codecs
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000183 EXPECT_GT(_acmRefA->RegisterSendCodec(codecInst_A), -1);
184 EXPECT_GT(_acmRefA->RegisterReceiveCodec(codecInst_B), -1);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000185
186 //--- Set B codecs
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000187 EXPECT_GT(_acmB->RegisterSendCodec(codecInst_B), -1);
188 EXPECT_GT(_acmB->RegisterReceiveCodec(codecInst_A), -1);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000189
190 //--- Set ref-B codecs
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000191 EXPECT_EQ(0, _acmRefB->RegisterSendCodec(codecInst_B));
192 EXPECT_EQ(0, _acmRefB->RegisterReceiveCodec(codecInst_A));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000193
194 uint16_t frequencyHz;
195
196 //--- Input A and B
197 std::string in_file_name = webrtc::test::ResourcePath(
198 "audio_coding/testfile32kHz", "pcm");
199 frequencyHz = 16000;
200 _inFileA.Open(in_file_name, frequencyHz, "rb");
201 _inFileB.Open(in_file_name, frequencyHz, "rb");
202
203 //--- Output A
204 std::string output_file_a = webrtc::test::OutputPath() + "outAutotestA.pcm";
205 frequencyHz = 16000;
206 _outFileA.Open(output_file_a, frequencyHz, "wb");
207 std::string output_ref_file_a = webrtc::test::OutputPath()
208 + "ref_outAutotestA.pcm";
209 _outFileRefA.Open(output_ref_file_a, frequencyHz, "wb");
210
211 //--- Output B
212 std::string output_file_b = webrtc::test::OutputPath() + "outAutotestB.pcm";
213 frequencyHz = 16000;
214 _outFileB.Open(output_file_b, frequencyHz, "wb");
215 std::string output_ref_file_b = webrtc::test::OutputPath()
216 + "ref_outAutotestB.pcm";
217 _outFileRefB.Open(output_ref_file_b, frequencyHz, "wb");
218
219 //--- Set A-to-B channel
220 _channel_A2B = new Channel;
221 _acmA->RegisterTransportCallback(_channel_A2B);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000222 _channel_A2B->RegisterReceiverACM(_acmB.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000223 //--- Do the same for the reference
224 _channelRef_A2B = new Channel;
225 _acmRefA->RegisterTransportCallback(_channelRef_A2B);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000226 _channelRef_A2B->RegisterReceiverACM(_acmRefB.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000227
228 //--- Set B-to-A channel
229 _channel_B2A = new Channel;
230 _acmB->RegisterTransportCallback(_channel_B2A);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000231 _channel_B2A->RegisterReceiverACM(_acmA.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000232 //--- Do the same for reference
233 _channelRef_B2A = new Channel;
234 _acmRefB->RegisterTransportCallback(_channelRef_B2A);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000235 _channelRef_B2A->RegisterReceiverACM(_acmRefA.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000236
237 // The clicks will be more obvious when we
238 // are in FAX mode.
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000239 EXPECT_EQ(0, _acmB->SetPlayoutMode(fax));
240 EXPECT_EQ(0, _acmRefB->SetPlayoutMode(fax));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000241}
242
243void TwoWayCommunication::Perform() {
244 if (_testMode == 0) {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000245 SetUpAutotest();
246 } else {
247 SetUp();
248 }
249 unsigned int msecPassed = 0;
250 unsigned int secPassed = 0;
251
252 int32_t outFreqHzA = _outFileA.SamplingFrequency();
253 int32_t outFreqHzB = _outFileB.SamplingFrequency();
254
255 AudioFrame audioFrame;
256
257 CodecInst codecInst_B;
258 CodecInst dummy;
259
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000260 EXPECT_EQ(0, _acmB->SendCodec(&codecInst_B));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000261
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000262 // In the following loop we tests that the code can handle misuse of the APIs.
263 // In the middle of a session with data flowing between two sides, called A
Karl Wibergdd00f112015-08-25 09:37:04 +0200264 // and B, APIs will be called, and the code should continue to run, and be
265 // able to recover.
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000266 while (!_inFileA.EndOfFile() && !_inFileB.EndOfFile()) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000267 msecPassed += 10;
268 EXPECT_GT(_inFileA.Read10MsData(audioFrame), 0);
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +0000269 EXPECT_GE(_acmA->Add10MsData(audioFrame), 0);
270 EXPECT_GE(_acmRefA->Add10MsData(audioFrame), 0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000271
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000272 EXPECT_GT(_inFileB.Read10MsData(audioFrame), 0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000273
Henrik Lundin45c64492015-03-30 19:00:44 +0200274 EXPECT_GE(_acmB->Add10MsData(audioFrame), 0);
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +0000275 EXPECT_GE(_acmRefB->Add10MsData(audioFrame), 0);
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000276 EXPECT_EQ(0, _acmA->PlayoutData10Ms(outFreqHzA, &audioFrame));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000277 _outFileA.Write10MsData(audioFrame);
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000278 EXPECT_EQ(0, _acmRefA->PlayoutData10Ms(outFreqHzA, &audioFrame));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000279 _outFileRefA.Write10MsData(audioFrame);
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000280 EXPECT_EQ(0, _acmB->PlayoutData10Ms(outFreqHzB, &audioFrame));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000281 _outFileB.Write10MsData(audioFrame);
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000282 EXPECT_EQ(0, _acmRefB->PlayoutData10Ms(outFreqHzB, &audioFrame));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000283 _outFileRefB.Write10MsData(audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000284
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000285 // Update time counters each time a second of data has passed.
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000286 if (msecPassed >= 1000) {
287 msecPassed = 0;
288 secPassed++;
289 }
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000290 // Re-register send codec on side B.
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000291 if (((secPassed % 5) == 4) && (msecPassed >= 990)) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000292 EXPECT_EQ(0, _acmB->RegisterSendCodec(codecInst_B));
293 EXPECT_EQ(0, _acmB->SendCodec(&dummy));
niklase@google.com470e71d2011-07-07 08:21:25 +0000294 }
Karl Wibergdd00f112015-08-25 09:37:04 +0200295 // Initialize receiver on side A.
296 if (((secPassed % 7) == 6) && (msecPassed == 0))
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000297 EXPECT_EQ(0, _acmA->InitializeReceiver());
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000298 // Re-register codec on side A.
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000299 if (((secPassed % 7) == 6) && (msecPassed >= 990)) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000300 EXPECT_EQ(0, _acmA->RegisterReceiveCodec(codecInst_B));
niklase@google.com470e71d2011-07-07 08:21:25 +0000301 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000302 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000303}
304
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000305} // namespace webrtc