blob: 9c26a32c91b9f7b6ecb30f4317d972bca9e770f3 [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
kwiberg37478382016-02-14 20:40:57 -080017#include <memory>
18
niklase@google.com470e71d2011-07-07 08:21:25 +000019#ifdef WIN32
20#include <Windows.h>
21#endif
22
turaj@webrtc.org6ea3d1c2013-10-02 21:44:33 +000023#include "webrtc/common_types.h"
kwibergac9f8762016-09-30 22:29:43 -070024#include "webrtc/engine_configurations.h"
ossue3525782016-05-25 07:37:43 -070025#include "webrtc/modules/audio_coding/codecs/builtin_audio_decoder_factory.h"
kjellander3e6db232015-11-26 04:44:54 -080026#include "webrtc/modules/audio_coding/test/PCMFile.h"
27#include "webrtc/modules/audio_coding/test/utility.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010028#include "webrtc/system_wrappers/include/trace.h"
kwibergac9f8762016-09-30 22:29:43 -070029#include "webrtc/test/gtest.h"
pbos@webrtc.org2ab209e2013-08-09 08:49:48 +000030#include "webrtc/test/testsupport/fileutils.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000031
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000032namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000033
34#define MAX_FILE_NAME_LENGTH_BYTE 500
35
henrik.lundin@webrtc.orgadaf8092014-04-17 08:29:10 +000036TwoWayCommunication::TwoWayCommunication(int testMode)
37 : _acmA(AudioCodingModule::Create(1)),
henrik.lundin@webrtc.orgadaf8092014-04-17 08:29:10 +000038 _acmRefA(AudioCodingModule::Create(3)),
henrik.lundin1bd0e032015-09-28 06:12:17 -070039 _testMode(testMode) {
40 AudioCodingModule::Config config;
41 // The clicks will be more obvious in FAX mode. TODO(henrik.lundin) Really?
42 config.neteq_config.playout_mode = kPlayoutFax;
43 config.id = 2;
ossue3525782016-05-25 07:37:43 -070044 config.decoder_factory = CreateBuiltinAudioDecoderFactory();
henrik.lundin1bd0e032015-09-28 06:12:17 -070045 _acmB.reset(AudioCodingModule::Create(config));
46 config.id = 4;
47 _acmRefB.reset(AudioCodingModule::Create(config));
48}
niklase@google.com470e71d2011-07-07 08:21:25 +000049
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000050TwoWayCommunication::~TwoWayCommunication() {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000051 delete _channel_A2B;
52 delete _channel_B2A;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000053 delete _channelRef_A2B;
54 delete _channelRef_B2A;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000055 _inFileA.Close();
56 _inFileB.Close();
57 _outFileA.Close();
58 _outFileB.Close();
59 _outFileRefA.Close();
60 _outFileRefB.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +000061}
62
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000063void TwoWayCommunication::ChooseCodec(uint8_t* codecID_A,
64 uint8_t* codecID_B) {
kwiberg37478382016-02-14 20:40:57 -080065 std::unique_ptr<AudioCodingModule> tmpACM(AudioCodingModule::Create(0));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000066 uint8_t noCodec = tmpACM->NumberOfCodecs();
67 CodecInst codecInst;
68 printf("List of Supported Codecs\n");
69 printf("========================\n");
70 for (uint8_t codecCntr = 0; codecCntr < noCodec; codecCntr++) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000071 EXPECT_EQ(tmpACM->Codec(codecCntr, &codecInst), 0);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000072 printf("%d- %s\n", codecCntr, codecInst.plname);
73 }
74 printf("\nChoose a send codec for side A [0]: ");
75 char myStr[15] = "";
76 EXPECT_TRUE(fgets(myStr, 10, stdin) != NULL);
77 *codecID_A = (uint8_t) atoi(myStr);
niklase@google.com470e71d2011-07-07 08:21:25 +000078
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000079 printf("\nChoose a send codec for side B [0]: ");
80 EXPECT_TRUE(fgets(myStr, 10, stdin) != NULL);
81 *codecID_B = (uint8_t) atoi(myStr);
niklase@google.com470e71d2011-07-07 08:21:25 +000082
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000083 printf("\n");
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000084}
niklase@google.com470e71d2011-07-07 08:21:25 +000085
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000086void TwoWayCommunication::SetUp() {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000087 uint8_t codecID_A;
88 uint8_t codecID_B;
89
90 ChooseCodec(&codecID_A, &codecID_B);
91 CodecInst codecInst_A;
92 CodecInst codecInst_B;
93 CodecInst dummyCodec;
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000094 EXPECT_EQ(0, _acmA->Codec(codecID_A, &codecInst_A));
95 EXPECT_EQ(0, _acmB->Codec(codecID_B, &codecInst_B));
96 EXPECT_EQ(0, _acmA->Codec(6, &dummyCodec));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000097
98 //--- Set A codecs
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000099 EXPECT_EQ(0, _acmA->RegisterSendCodec(codecInst_A));
100 EXPECT_EQ(0, _acmA->RegisterReceiveCodec(codecInst_B));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000101 //--- Set ref-A codecs
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000102 EXPECT_EQ(0, _acmRefA->RegisterSendCodec(codecInst_A));
103 EXPECT_EQ(0, _acmRefA->RegisterReceiveCodec(codecInst_B));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000104
105 //--- Set B codecs
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000106 EXPECT_EQ(0, _acmB->RegisterSendCodec(codecInst_B));
107 EXPECT_EQ(0, _acmB->RegisterReceiveCodec(codecInst_A));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000108
109 //--- Set ref-B codecs
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000110 EXPECT_EQ(0, _acmRefB->RegisterSendCodec(codecInst_B));
111 EXPECT_EQ(0, _acmRefB->RegisterReceiveCodec(codecInst_A));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000112
113 uint16_t frequencyHz;
114
115 //--- Input A
116 std::string in_file_name = webrtc::test::ResourcePath(
117 "audio_coding/testfile32kHz", "pcm");
118 frequencyHz = 32000;
119 printf("Enter input file at side A [%s]: ", in_file_name.c_str());
120 PCMFile::ChooseFile(&in_file_name, 499, &frequencyHz);
121 _inFileA.Open(in_file_name, frequencyHz, "rb");
122
123 //--- Output A
124 std::string out_file_a = webrtc::test::OutputPath() + "outA.pcm";
125 printf("Output file at side A: %s\n", out_file_a.c_str());
126 printf("Sampling frequency (in Hz) of the above file: %u\n", frequencyHz);
127 _outFileA.Open(out_file_a, frequencyHz, "wb");
128 std::string ref_file_name = webrtc::test::OutputPath() + "ref_outA.pcm";
129 _outFileRefA.Open(ref_file_name, frequencyHz, "wb");
130
131 //--- Input B
132 in_file_name = webrtc::test::ResourcePath("audio_coding/testfile32kHz",
133 "pcm");
134 frequencyHz = 32000;
135 printf("\n\nEnter input file at side B [%s]: ", in_file_name.c_str());
136 PCMFile::ChooseFile(&in_file_name, 499, &frequencyHz);
137 _inFileB.Open(in_file_name, frequencyHz, "rb");
138
139 //--- Output B
140 std::string out_file_b = webrtc::test::OutputPath() + "outB.pcm";
141 printf("Output file at side B: %s\n", out_file_b.c_str());
142 printf("Sampling frequency (in Hz) of the above file: %u\n", frequencyHz);
143 _outFileB.Open(out_file_b, frequencyHz, "wb");
144 ref_file_name = webrtc::test::OutputPath() + "ref_outB.pcm";
145 _outFileRefB.Open(ref_file_name, frequencyHz, "wb");
146
147 //--- Set A-to-B channel
148 _channel_A2B = new Channel;
149 _acmA->RegisterTransportCallback(_channel_A2B);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000150 _channel_A2B->RegisterReceiverACM(_acmB.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000151 //--- Do the same for the reference
152 _channelRef_A2B = new Channel;
153 _acmRefA->RegisterTransportCallback(_channelRef_A2B);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000154 _channelRef_A2B->RegisterReceiverACM(_acmRefB.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000155
156 //--- Set B-to-A channel
157 _channel_B2A = new Channel;
158 _acmB->RegisterTransportCallback(_channel_B2A);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000159 _channel_B2A->RegisterReceiverACM(_acmA.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000160 //--- Do the same for reference
161 _channelRef_B2A = new Channel;
162 _acmRefB->RegisterTransportCallback(_channelRef_B2A);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000163 _channelRef_B2A->RegisterReceiverACM(_acmRefA.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000164}
165
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000166void TwoWayCommunication::SetUpAutotest() {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000167 CodecInst codecInst_A;
168 CodecInst codecInst_B;
169 CodecInst dummyCodec;
170
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000171 EXPECT_EQ(0, _acmA->Codec("ISAC", &codecInst_A, 16000, 1));
172 EXPECT_EQ(0, _acmB->Codec("L16", &codecInst_B, 8000, 1));
173 EXPECT_EQ(0, _acmA->Codec(6, &dummyCodec));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000174
175 //--- Set A codecs
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000176 EXPECT_EQ(0, _acmA->RegisterSendCodec(codecInst_A));
177 EXPECT_EQ(0, _acmA->RegisterReceiveCodec(codecInst_B));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000178
179 //--- Set ref-A codecs
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000180 EXPECT_GT(_acmRefA->RegisterSendCodec(codecInst_A), -1);
181 EXPECT_GT(_acmRefA->RegisterReceiveCodec(codecInst_B), -1);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000182
183 //--- Set B codecs
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000184 EXPECT_GT(_acmB->RegisterSendCodec(codecInst_B), -1);
185 EXPECT_GT(_acmB->RegisterReceiveCodec(codecInst_A), -1);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000186
187 //--- Set ref-B codecs
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000188 EXPECT_EQ(0, _acmRefB->RegisterSendCodec(codecInst_B));
189 EXPECT_EQ(0, _acmRefB->RegisterReceiveCodec(codecInst_A));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000190
191 uint16_t frequencyHz;
192
193 //--- Input A and B
194 std::string in_file_name = webrtc::test::ResourcePath(
195 "audio_coding/testfile32kHz", "pcm");
196 frequencyHz = 16000;
197 _inFileA.Open(in_file_name, frequencyHz, "rb");
198 _inFileB.Open(in_file_name, frequencyHz, "rb");
199
200 //--- Output A
201 std::string output_file_a = webrtc::test::OutputPath() + "outAutotestA.pcm";
202 frequencyHz = 16000;
203 _outFileA.Open(output_file_a, frequencyHz, "wb");
204 std::string output_ref_file_a = webrtc::test::OutputPath()
205 + "ref_outAutotestA.pcm";
206 _outFileRefA.Open(output_ref_file_a, frequencyHz, "wb");
207
208 //--- Output B
209 std::string output_file_b = webrtc::test::OutputPath() + "outAutotestB.pcm";
210 frequencyHz = 16000;
211 _outFileB.Open(output_file_b, frequencyHz, "wb");
212 std::string output_ref_file_b = webrtc::test::OutputPath()
213 + "ref_outAutotestB.pcm";
214 _outFileRefB.Open(output_ref_file_b, frequencyHz, "wb");
215
216 //--- Set A-to-B channel
217 _channel_A2B = new Channel;
218 _acmA->RegisterTransportCallback(_channel_A2B);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000219 _channel_A2B->RegisterReceiverACM(_acmB.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000220 //--- Do the same for the reference
221 _channelRef_A2B = new Channel;
222 _acmRefA->RegisterTransportCallback(_channelRef_A2B);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000223 _channelRef_A2B->RegisterReceiverACM(_acmRefB.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000224
225 //--- Set B-to-A channel
226 _channel_B2A = new Channel;
227 _acmB->RegisterTransportCallback(_channel_B2A);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000228 _channel_B2A->RegisterReceiverACM(_acmA.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000229 //--- Do the same for reference
230 _channelRef_B2A = new Channel;
231 _acmRefB->RegisterTransportCallback(_channelRef_B2A);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000232 _channelRef_B2A->RegisterReceiverACM(_acmRefA.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000233}
234
235void TwoWayCommunication::Perform() {
236 if (_testMode == 0) {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000237 SetUpAutotest();
238 } else {
239 SetUp();
240 }
241 unsigned int msecPassed = 0;
242 unsigned int secPassed = 0;
243
244 int32_t outFreqHzA = _outFileA.SamplingFrequency();
245 int32_t outFreqHzB = _outFileB.SamplingFrequency();
246
247 AudioFrame audioFrame;
248
kwiberg1fd4a4a2015-11-03 11:20:50 -0800249 auto codecInst_B = _acmB->SendCodec();
250 ASSERT_TRUE(codecInst_B);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000251
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000252 // In the following loop we tests that the code can handle misuse of the APIs.
253 // In the middle of a session with data flowing between two sides, called A
Karl Wibergdd00f112015-08-25 09:37:04 +0200254 // and B, APIs will be called, and the code should continue to run, and be
255 // able to recover.
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000256 while (!_inFileA.EndOfFile() && !_inFileB.EndOfFile()) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000257 msecPassed += 10;
258 EXPECT_GT(_inFileA.Read10MsData(audioFrame), 0);
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +0000259 EXPECT_GE(_acmA->Add10MsData(audioFrame), 0);
260 EXPECT_GE(_acmRefA->Add10MsData(audioFrame), 0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000261
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000262 EXPECT_GT(_inFileB.Read10MsData(audioFrame), 0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000263
Henrik Lundin45c64492015-03-30 19:00:44 +0200264 EXPECT_GE(_acmB->Add10MsData(audioFrame), 0);
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +0000265 EXPECT_GE(_acmRefB->Add10MsData(audioFrame), 0);
henrik.lundind4ccb002016-05-17 12:21:55 -0700266 bool muted;
267 EXPECT_EQ(0, _acmA->PlayoutData10Ms(outFreqHzA, &audioFrame, &muted));
268 ASSERT_FALSE(muted);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000269 _outFileA.Write10MsData(audioFrame);
henrik.lundind4ccb002016-05-17 12:21:55 -0700270 EXPECT_EQ(0, _acmRefA->PlayoutData10Ms(outFreqHzA, &audioFrame, &muted));
271 ASSERT_FALSE(muted);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000272 _outFileRefA.Write10MsData(audioFrame);
henrik.lundind4ccb002016-05-17 12:21:55 -0700273 EXPECT_EQ(0, _acmB->PlayoutData10Ms(outFreqHzB, &audioFrame, &muted));
274 ASSERT_FALSE(muted);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000275 _outFileB.Write10MsData(audioFrame);
henrik.lundind4ccb002016-05-17 12:21:55 -0700276 EXPECT_EQ(0, _acmRefB->PlayoutData10Ms(outFreqHzB, &audioFrame, &muted));
277 ASSERT_FALSE(muted);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000278 _outFileRefB.Write10MsData(audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000279
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000280 // Update time counters each time a second of data has passed.
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000281 if (msecPassed >= 1000) {
282 msecPassed = 0;
283 secPassed++;
284 }
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000285 // Re-register send codec on side B.
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000286 if (((secPassed % 5) == 4) && (msecPassed >= 990)) {
kwiberg1fd4a4a2015-11-03 11:20:50 -0800287 EXPECT_EQ(0, _acmB->RegisterSendCodec(*codecInst_B));
288 EXPECT_TRUE(_acmB->SendCodec());
niklase@google.com470e71d2011-07-07 08:21:25 +0000289 }
Karl Wibergdd00f112015-08-25 09:37:04 +0200290 // Initialize receiver on side A.
291 if (((secPassed % 7) == 6) && (msecPassed == 0))
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000292 EXPECT_EQ(0, _acmA->InitializeReceiver());
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000293 // Re-register codec on side A.
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000294 if (((secPassed % 7) == 6) && (msecPassed >= 990)) {
kwiberg1fd4a4a2015-11-03 11:20:50 -0800295 EXPECT_EQ(0, _acmA->RegisterReceiveCodec(*codecInst_B));
niklase@google.com470e71d2011-07-07 08:21:25 +0000296 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000297 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000298}
299
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000300} // namespace webrtc