blob: 8049436cd01fe754e2c9bb845d8a7da97711a251 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020024#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "modules/audio_coding/codecs/audio_format_conversion.h"
26#include "modules/audio_coding/test/PCMFile.h"
27#include "modules/audio_coding/test/utility.h"
28#include "test/gtest.h"
29#include "test/testsupport/fileutils.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020030#include "typedefs.h" // NOLINT(build/include)
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)
solenberg2d0f7752017-09-27 10:33:57 -070037 : _acmA(AudioCodingModule::Create()),
38 _acmRefA(AudioCodingModule::Create()),
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;
ossue3525782016-05-25 07:37:43 -070043 config.decoder_factory = CreateBuiltinAudioDecoderFactory();
henrik.lundin1bd0e032015-09-28 06:12:17 -070044 _acmB.reset(AudioCodingModule::Create(config));
henrik.lundin1bd0e032015-09-28 06:12:17 -070045 _acmRefB.reset(AudioCodingModule::Create(config));
46}
niklase@google.com470e71d2011-07-07 08:21:25 +000047
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000048TwoWayCommunication::~TwoWayCommunication() {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000049 delete _channel_A2B;
50 delete _channel_B2A;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000051 delete _channelRef_A2B;
52 delete _channelRef_B2A;
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) {
solenberg2d0f7752017-09-27 10:33:57 -070063 std::unique_ptr<AudioCodingModule> tmpACM(AudioCodingModule::Create());
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));
kwibergda2bf4e2016-10-24 13:47:09 -070098 EXPECT_EQ(true, _acmA->RegisterReceiveCodec(codecInst_B.pltype,
99 CodecInstToSdp(codecInst_B)));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000100 //--- Set ref-A codecs
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000101 EXPECT_EQ(0, _acmRefA->RegisterSendCodec(codecInst_A));
kwibergda2bf4e2016-10-24 13:47:09 -0700102 EXPECT_EQ(true, _acmRefA->RegisterReceiveCodec(codecInst_B.pltype,
103 CodecInstToSdp(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));
kwibergda2bf4e2016-10-24 13:47:09 -0700107 EXPECT_EQ(true, _acmB->RegisterReceiveCodec(codecInst_A.pltype,
108 CodecInstToSdp(codecInst_A)));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000109
110 //--- Set ref-B codecs
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000111 EXPECT_EQ(0, _acmRefB->RegisterSendCodec(codecInst_B));
kwibergda2bf4e2016-10-24 13:47:09 -0700112 EXPECT_EQ(true, _acmRefB->RegisterReceiveCodec(codecInst_A.pltype,
113 CodecInstToSdp(codecInst_A)));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000114
115 uint16_t frequencyHz;
116
117 //--- Input A
118 std::string in_file_name = webrtc::test::ResourcePath(
119 "audio_coding/testfile32kHz", "pcm");
120 frequencyHz = 32000;
121 printf("Enter input file at side A [%s]: ", in_file_name.c_str());
122 PCMFile::ChooseFile(&in_file_name, 499, &frequencyHz);
123 _inFileA.Open(in_file_name, frequencyHz, "rb");
124
125 //--- Output A
126 std::string out_file_a = webrtc::test::OutputPath() + "outA.pcm";
127 printf("Output file at side A: %s\n", out_file_a.c_str());
128 printf("Sampling frequency (in Hz) of the above file: %u\n", frequencyHz);
129 _outFileA.Open(out_file_a, frequencyHz, "wb");
130 std::string ref_file_name = webrtc::test::OutputPath() + "ref_outA.pcm";
131 _outFileRefA.Open(ref_file_name, frequencyHz, "wb");
132
133 //--- Input B
134 in_file_name = webrtc::test::ResourcePath("audio_coding/testfile32kHz",
135 "pcm");
136 frequencyHz = 32000;
137 printf("\n\nEnter input file at side B [%s]: ", in_file_name.c_str());
138 PCMFile::ChooseFile(&in_file_name, 499, &frequencyHz);
139 _inFileB.Open(in_file_name, frequencyHz, "rb");
140
141 //--- Output B
142 std::string out_file_b = webrtc::test::OutputPath() + "outB.pcm";
143 printf("Output file at side B: %s\n", out_file_b.c_str());
144 printf("Sampling frequency (in Hz) of the above file: %u\n", frequencyHz);
145 _outFileB.Open(out_file_b, frequencyHz, "wb");
146 ref_file_name = webrtc::test::OutputPath() + "ref_outB.pcm";
147 _outFileRefB.Open(ref_file_name, frequencyHz, "wb");
148
149 //--- Set A-to-B channel
150 _channel_A2B = new Channel;
151 _acmA->RegisterTransportCallback(_channel_A2B);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000152 _channel_A2B->RegisterReceiverACM(_acmB.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000153 //--- Do the same for the reference
154 _channelRef_A2B = new Channel;
155 _acmRefA->RegisterTransportCallback(_channelRef_A2B);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000156 _channelRef_A2B->RegisterReceiverACM(_acmRefB.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000157
158 //--- Set B-to-A channel
159 _channel_B2A = new Channel;
160 _acmB->RegisterTransportCallback(_channel_B2A);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000161 _channel_B2A->RegisterReceiverACM(_acmA.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000162 //--- Do the same for reference
163 _channelRef_B2A = new Channel;
164 _acmRefB->RegisterTransportCallback(_channelRef_B2A);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000165 _channelRef_B2A->RegisterReceiverACM(_acmRefA.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000166}
167
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000168void TwoWayCommunication::SetUpAutotest() {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000169 CodecInst codecInst_A;
170 CodecInst codecInst_B;
171 CodecInst dummyCodec;
172
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000173 EXPECT_EQ(0, _acmA->Codec("ISAC", &codecInst_A, 16000, 1));
174 EXPECT_EQ(0, _acmB->Codec("L16", &codecInst_B, 8000, 1));
175 EXPECT_EQ(0, _acmA->Codec(6, &dummyCodec));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000176
177 //--- Set A codecs
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000178 EXPECT_EQ(0, _acmA->RegisterSendCodec(codecInst_A));
kwibergda2bf4e2016-10-24 13:47:09 -0700179 EXPECT_EQ(true, _acmA->RegisterReceiveCodec(codecInst_B.pltype,
180 CodecInstToSdp(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);
kwibergda2bf4e2016-10-24 13:47:09 -0700184 EXPECT_EQ(true, _acmRefA->RegisterReceiveCodec(codecInst_B.pltype,
185 CodecInstToSdp(codecInst_B)));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000186
187 //--- Set B codecs
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000188 EXPECT_GT(_acmB->RegisterSendCodec(codecInst_B), -1);
kwibergda2bf4e2016-10-24 13:47:09 -0700189 EXPECT_EQ(true, _acmB->RegisterReceiveCodec(codecInst_A.pltype,
190 CodecInstToSdp(codecInst_A)));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000191
192 //--- Set ref-B codecs
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000193 EXPECT_EQ(0, _acmRefB->RegisterSendCodec(codecInst_B));
kwibergda2bf4e2016-10-24 13:47:09 -0700194 EXPECT_EQ(true, _acmRefB->RegisterReceiveCodec(codecInst_A.pltype,
195 CodecInstToSdp(codecInst_A)));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000196
197 uint16_t frequencyHz;
198
199 //--- Input A and B
200 std::string in_file_name = webrtc::test::ResourcePath(
201 "audio_coding/testfile32kHz", "pcm");
202 frequencyHz = 16000;
203 _inFileA.Open(in_file_name, frequencyHz, "rb");
204 _inFileB.Open(in_file_name, frequencyHz, "rb");
205
206 //--- Output A
207 std::string output_file_a = webrtc::test::OutputPath() + "outAutotestA.pcm";
208 frequencyHz = 16000;
209 _outFileA.Open(output_file_a, frequencyHz, "wb");
210 std::string output_ref_file_a = webrtc::test::OutputPath()
211 + "ref_outAutotestA.pcm";
212 _outFileRefA.Open(output_ref_file_a, frequencyHz, "wb");
213
214 //--- Output B
215 std::string output_file_b = webrtc::test::OutputPath() + "outAutotestB.pcm";
216 frequencyHz = 16000;
217 _outFileB.Open(output_file_b, frequencyHz, "wb");
218 std::string output_ref_file_b = webrtc::test::OutputPath()
219 + "ref_outAutotestB.pcm";
220 _outFileRefB.Open(output_ref_file_b, frequencyHz, "wb");
221
222 //--- Set A-to-B channel
223 _channel_A2B = new Channel;
224 _acmA->RegisterTransportCallback(_channel_A2B);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000225 _channel_A2B->RegisterReceiverACM(_acmB.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000226 //--- Do the same for the reference
227 _channelRef_A2B = new Channel;
228 _acmRefA->RegisterTransportCallback(_channelRef_A2B);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000229 _channelRef_A2B->RegisterReceiverACM(_acmRefB.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000230
231 //--- Set B-to-A channel
232 _channel_B2A = new Channel;
233 _acmB->RegisterTransportCallback(_channel_B2A);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000234 _channel_B2A->RegisterReceiverACM(_acmA.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000235 //--- Do the same for reference
236 _channelRef_B2A = new Channel;
237 _acmRefB->RegisterTransportCallback(_channelRef_B2A);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000238 _channelRef_B2A->RegisterReceiverACM(_acmRefA.get());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000239}
240
241void TwoWayCommunication::Perform() {
242 if (_testMode == 0) {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000243 SetUpAutotest();
244 } else {
245 SetUp();
246 }
247 unsigned int msecPassed = 0;
248 unsigned int secPassed = 0;
249
250 int32_t outFreqHzA = _outFileA.SamplingFrequency();
251 int32_t outFreqHzB = _outFileB.SamplingFrequency();
252
253 AudioFrame audioFrame;
254
kwiberg1fd4a4a2015-11-03 11:20:50 -0800255 auto codecInst_B = _acmB->SendCodec();
256 ASSERT_TRUE(codecInst_B);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000257
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000258 // In the following loop we tests that the code can handle misuse of the APIs.
259 // In the middle of a session with data flowing between two sides, called A
Karl Wibergdd00f112015-08-25 09:37:04 +0200260 // and B, APIs will be called, and the code should continue to run, and be
261 // able to recover.
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000262 while (!_inFileA.EndOfFile() && !_inFileB.EndOfFile()) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000263 msecPassed += 10;
264 EXPECT_GT(_inFileA.Read10MsData(audioFrame), 0);
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +0000265 EXPECT_GE(_acmA->Add10MsData(audioFrame), 0);
266 EXPECT_GE(_acmRefA->Add10MsData(audioFrame), 0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000267
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000268 EXPECT_GT(_inFileB.Read10MsData(audioFrame), 0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000269
Henrik Lundin45c64492015-03-30 19:00:44 +0200270 EXPECT_GE(_acmB->Add10MsData(audioFrame), 0);
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +0000271 EXPECT_GE(_acmRefB->Add10MsData(audioFrame), 0);
henrik.lundind4ccb002016-05-17 12:21:55 -0700272 bool muted;
273 EXPECT_EQ(0, _acmA->PlayoutData10Ms(outFreqHzA, &audioFrame, &muted));
274 ASSERT_FALSE(muted);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000275 _outFileA.Write10MsData(audioFrame);
henrik.lundind4ccb002016-05-17 12:21:55 -0700276 EXPECT_EQ(0, _acmRefA->PlayoutData10Ms(outFreqHzA, &audioFrame, &muted));
277 ASSERT_FALSE(muted);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000278 _outFileRefA.Write10MsData(audioFrame);
henrik.lundind4ccb002016-05-17 12:21:55 -0700279 EXPECT_EQ(0, _acmB->PlayoutData10Ms(outFreqHzB, &audioFrame, &muted));
280 ASSERT_FALSE(muted);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000281 _outFileB.Write10MsData(audioFrame);
henrik.lundind4ccb002016-05-17 12:21:55 -0700282 EXPECT_EQ(0, _acmRefB->PlayoutData10Ms(outFreqHzB, &audioFrame, &muted));
283 ASSERT_FALSE(muted);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000284 _outFileRefB.Write10MsData(audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000285
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000286 // Update time counters each time a second of data has passed.
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000287 if (msecPassed >= 1000) {
288 msecPassed = 0;
289 secPassed++;
290 }
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000291 // Re-register send codec on side B.
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000292 if (((secPassed % 5) == 4) && (msecPassed >= 990)) {
kwiberg1fd4a4a2015-11-03 11:20:50 -0800293 EXPECT_EQ(0, _acmB->RegisterSendCodec(*codecInst_B));
294 EXPECT_TRUE(_acmB->SendCodec());
niklase@google.com470e71d2011-07-07 08:21:25 +0000295 }
Karl Wibergdd00f112015-08-25 09:37:04 +0200296 // Initialize receiver on side A.
297 if (((secPassed % 7) == 6) && (msecPassed == 0))
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000298 EXPECT_EQ(0, _acmA->InitializeReceiver());
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000299 // Re-register codec on side A.
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000300 if (((secPassed % 7) == 6) && (msecPassed >= 990)) {
kwibergda2bf4e2016-10-24 13:47:09 -0700301 EXPECT_EQ(true, _acmA->RegisterReceiveCodec(
302 codecInst_B->pltype, CodecInstToSdp(*codecInst_B)));
niklase@google.com470e71d2011-07-07 08:21:25 +0000303 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000304 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000305}
306
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000307} // namespace webrtc