blob: dbf5ad453418ad985a287dac12cf14cee3eb761d [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
andrew@webrtc.org63a50982012-05-02 23:56:37 +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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_coding/test/APITest.h"
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000012
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <ctype.h>
kjellander@webrtc.org543c3ea2011-11-23 12:20:35 +000014#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000017
tina.legrand@webrtc.org5e7ca602012-06-12 07:16:24 +000018#include <iostream>
19#include <ostream>
20#include <string>
21
Mirko Bonadei71207422017-09-15 13:58:09 +020022#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "modules/audio_coding/codecs/audio_format_conversion.h"
24#include "modules/audio_coding/test/utility.h"
25#include "rtc_base/platform_thread.h"
26#include "rtc_base/timeutils.h"
27#include "system_wrappers/include/event_wrapper.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#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 {
33
niklase@google.com470e71d2011-07-07 08:21:25 +000034#define TEST_DURATION_SEC 600
niklase@google.com470e71d2011-07-07 08:21:25 +000035#define NUMBER_OF_SENDER_TESTS 6
niklase@google.com470e71d2011-07-07 08:21:25 +000036#define MAX_FILE_NAME_LENGTH_BYTE 500
niklase@google.com470e71d2011-07-07 08:21:25 +000037
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000038void APITest::Wait(uint32_t waitLengthMs) {
39 if (_randomTest) {
40 return;
41 } else {
42 EventWrapper* myEvent = EventWrapper::Create();
43 myEvent->Wait(waitLengthMs);
44 delete myEvent;
45 return;
46 }
niklase@google.com470e71d2011-07-07 08:21:25 +000047}
48
solenberg88499ec2016-09-07 07:34:41 -070049APITest::APITest()
solenbergc7b4a452017-09-28 07:37:11 -070050 : _acmA(AudioCodingModule::Create()),
51 _acmB(AudioCodingModule::Create()),
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000052 _channel_A2B(NULL),
53 _channel_B2A(NULL),
54 _writeToFile(true),
55 _pullEventA(NULL),
56 _pushEventA(NULL),
57 _processEventA(NULL),
58 _apiEventA(NULL),
59 _pullEventB(NULL),
60 _pushEventB(NULL),
61 _processEventB(NULL),
62 _apiEventB(NULL),
63 _codecCntrA(0),
64 _codecCntrB(0),
65 _thereIsEncoderA(false),
66 _thereIsEncoderB(false),
67 _thereIsDecoderA(false),
68 _thereIsDecoderB(false),
69 _sendVADA(false),
70 _sendDTXA(false),
71 _sendVADModeA(VADNormal),
72 _sendVADB(false),
73 _sendDTXB(false),
74 _sendVADModeB(VADNormal),
75 _minDelayA(0),
76 _minDelayB(0),
77 _dotPositionA(0),
78 _dotMoveDirectionA(1),
79 _dotPositionB(39),
80 _dotMoveDirectionB(-1),
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000081 _vadCallbackA(NULL),
82 _vadCallbackB(NULL),
83 _apiTestRWLock(*RWLockWrapper::CreateRWLock()),
84 _randomTest(false),
85 _testNumA(0),
86 _testNumB(1) {
87 int n;
88 for (n = 0; n < 32; n++) {
89 _payloadUsed[n] = false;
90 }
niklase@google.com470e71d2011-07-07 08:21:25 +000091
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000092 _movingDot[40] = '\0';
niklase@google.com470e71d2011-07-07 08:21:25 +000093
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000094 for (int n = 0; n < 40; n++) {
95 _movingDot[n] = ' ';
96 }
niklase@google.com470e71d2011-07-07 08:21:25 +000097}
98
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000099APITest::~APITest() {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000100 DELETE_POINTER(_channel_A2B);
101 DELETE_POINTER(_channel_B2A);
niklase@google.com470e71d2011-07-07 08:21:25 +0000102
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000103 DELETE_POINTER(_pushEventA);
104 DELETE_POINTER(_pullEventA);
105 DELETE_POINTER(_processEventA);
106 DELETE_POINTER(_apiEventA);
niklase@google.com470e71d2011-07-07 08:21:25 +0000107
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000108 DELETE_POINTER(_pushEventB);
109 DELETE_POINTER(_pullEventB);
110 DELETE_POINTER(_processEventB);
111 DELETE_POINTER(_apiEventB);
niklase@google.com470e71d2011-07-07 08:21:25 +0000112
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000113 _inFileA.Close();
114 _outFileA.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +0000115
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000116 _inFileB.Close();
117 _outFileB.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +0000118
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000119 DELETE_POINTER(_vadCallbackA);
120 DELETE_POINTER(_vadCallbackB);
niklase@google.com470e71d2011-07-07 08:21:25 +0000121
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000122 delete &_apiTestRWLock;
niklase@google.com470e71d2011-07-07 08:21:25 +0000123}
124
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000125int16_t APITest::SetUp() {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000126 CodecInst dummyCodec;
127 int lastPayloadType = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000128
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000129 int16_t numCodecs = _acmA->NumberOfCodecs();
130 for (uint8_t n = 0; n < numCodecs; n++) {
131 AudioCodingModule::Codec(n, &dummyCodec);
132 if ((STR_CASE_CMP(dummyCodec.plname, "CN") == 0)
133 && (dummyCodec.plfreq == 32000)) {
134 continue;
niklase@google.com470e71d2011-07-07 08:21:25 +0000135 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000136
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000137 printf("Register Receive Codec %s ", dummyCodec.plname);
niklase@google.com470e71d2011-07-07 08:21:25 +0000138
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000139 if ((n != 0) && !FixedPayloadTypeCodec(dummyCodec.plname)) {
140 // Check registration with an already occupied payload type
141 int currentPayloadType = dummyCodec.pltype;
142 dummyCodec.pltype = 97; //lastPayloadType;
kwibergda2bf4e2016-10-24 13:47:09 -0700143 EXPECT_EQ(true, _acmB->RegisterReceiveCodec(dummyCodec.pltype,
144 CodecInstToSdp(dummyCodec)));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000145 dummyCodec.pltype = currentPayloadType;
146 }
andrew@webrtc.orgd7a71d02012-08-01 01:40:02 +0000147
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000148 if ((n < numCodecs - 1) && !FixedPayloadTypeCodec(dummyCodec.plname)) {
149 // test if re-registration works;
150 CodecInst nextCodec;
151 int currentPayloadType = dummyCodec.pltype;
152 AudioCodingModule::Codec(n + 1, &nextCodec);
153 dummyCodec.pltype = nextCodec.pltype;
154 if (!FixedPayloadTypeCodec(nextCodec.plname)) {
kwibergda2bf4e2016-10-24 13:47:09 -0700155 _acmB->RegisterReceiveCodec(dummyCodec.pltype,
156 CodecInstToSdp(dummyCodec));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000157 }
158 dummyCodec.pltype = currentPayloadType;
159 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000160
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000161 if ((n < numCodecs - 1) && !FixedPayloadTypeCodec(dummyCodec.plname)) {
162 // test if un-registration works;
163 CodecInst nextCodec;
164 AudioCodingModule::Codec(n + 1, &nextCodec);
165 nextCodec.pltype = dummyCodec.pltype;
166 if (!FixedPayloadTypeCodec(nextCodec.plname)) {
kwibergda2bf4e2016-10-24 13:47:09 -0700167 EXPECT_EQ(true, _acmA->RegisterReceiveCodec(nextCodec.pltype,
168 CodecInstToSdp(nextCodec)));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000169 CHECK_ERROR_MT(_acmA->UnregisterReceiveCodec(nextCodec.pltype));
170 }
171 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000172
kwibergda2bf4e2016-10-24 13:47:09 -0700173 EXPECT_EQ(true, _acmA->RegisterReceiveCodec(dummyCodec.pltype,
174 CodecInstToSdp(dummyCodec)));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000175 printf(" side A done!");
kwibergda2bf4e2016-10-24 13:47:09 -0700176 EXPECT_EQ(true, _acmB->RegisterReceiveCodec(dummyCodec.pltype,
177 CodecInstToSdp(dummyCodec)));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000178 printf(" side B done!\n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000179
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000180 if (!strcmp(dummyCodec.plname, "CN")) {
181 CHECK_ERROR_MT(_acmA->RegisterSendCodec(dummyCodec));
182 CHECK_ERROR_MT(_acmB->RegisterSendCodec(dummyCodec));
183 }
184 lastPayloadType = dummyCodec.pltype;
185 if ((lastPayloadType >= 96) && (lastPayloadType <= 127)) {
186 _payloadUsed[lastPayloadType - 96] = true;
187 }
188 }
189 _thereIsDecoderA = true;
190 _thereIsDecoderB = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000191
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000192 // Register Send Codec
193 AudioCodingModule::Codec((uint8_t) _codecCntrA, &dummyCodec);
194 CHECK_ERROR_MT(_acmA->RegisterSendCodec(dummyCodec));
195 _thereIsEncoderA = true;
196 //
197 AudioCodingModule::Codec((uint8_t) _codecCntrB, &dummyCodec);
198 CHECK_ERROR_MT(_acmB->RegisterSendCodec(dummyCodec));
199 _thereIsEncoderB = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000200
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000201 uint16_t frequencyHz;
niklase@google.com470e71d2011-07-07 08:21:25 +0000202
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000203 printf("\n\nAPI Test\n");
204 printf("========\n");
205 printf("Hit enter to accept the default values indicated in []\n\n");
andrew@webrtc.orgd7a71d02012-08-01 01:40:02 +0000206
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000207 //--- Input A
208 std::string file_name = webrtc::test::ResourcePath(
209 "audio_coding/testfile32kHz", "pcm");
210 frequencyHz = 32000;
211 printf("Enter input file at side A [%s]: ", file_name.c_str());
212 PCMFile::ChooseFile(&file_name, 499, &frequencyHz);
213 _inFileA.Open(file_name, frequencyHz, "rb", true);
niklase@google.com470e71d2011-07-07 08:21:25 +0000214
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000215 //--- Output A
216 std::string out_file_a = webrtc::test::OutputPath() + "outA.pcm";
217 printf("Enter output file at side A [%s]: ", out_file_a.c_str());
218 PCMFile::ChooseFile(&out_file_a, 499, &frequencyHz);
219 _outFileA.Open(out_file_a, frequencyHz, "wb");
niklase@google.com470e71d2011-07-07 08:21:25 +0000220
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000221 //--- Input B
222 file_name = webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
223 printf("\n\nEnter input file at side B [%s]: ", file_name.c_str());
224 PCMFile::ChooseFile(&file_name, 499, &frequencyHz);
225 _inFileB.Open(file_name, frequencyHz, "rb", true);
niklase@google.com470e71d2011-07-07 08:21:25 +0000226
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000227 //--- Output B
228 std::string out_file_b = webrtc::test::OutputPath() + "outB.pcm";
229 printf("Enter output file at side B [%s]: ", out_file_b.c_str());
230 PCMFile::ChooseFile(&out_file_b, 499, &frequencyHz);
231 _outFileB.Open(out_file_b, frequencyHz, "wb");
niklase@google.com470e71d2011-07-07 08:21:25 +0000232
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000233 //--- Set A-to-B channel
234 _channel_A2B = new Channel(2);
235 CHECK_ERROR_MT(_acmA->RegisterTransportCallback(_channel_A2B));
turaj@webrtc.org6ea3d1c2013-10-02 21:44:33 +0000236 _channel_A2B->RegisterReceiverACM(_acmB.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000237
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000238 //--- Set B-to-A channel
239 _channel_B2A = new Channel(1);
240 CHECK_ERROR_MT(_acmB->RegisterTransportCallback(_channel_B2A));
turaj@webrtc.org6ea3d1c2013-10-02 21:44:33 +0000241 _channel_B2A->RegisterReceiverACM(_acmA.get());
tina.legrand@webrtc.org5e7ca602012-06-12 07:16:24 +0000242
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000243 //--- EVENT TIMERS
244 // A
Peter Boström64c03662015-04-08 11:24:19 +0200245 _pullEventA = EventTimerWrapper::Create();
246 _pushEventA = EventTimerWrapper::Create();
247 _processEventA = EventTimerWrapper::Create();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000248 _apiEventA = EventWrapper::Create();
249 // B
Peter Boström64c03662015-04-08 11:24:19 +0200250 _pullEventB = EventTimerWrapper::Create();
251 _pushEventB = EventTimerWrapper::Create();
252 _processEventB = EventTimerWrapper::Create();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000253 _apiEventB = EventWrapper::Create();
254
255 //--- I/O params
256 // A
257 _outFreqHzA = _outFileA.SamplingFrequency();
258 // B
259 _outFreqHzB = _outFileB.SamplingFrequency();
260
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000261 char print[11];
262
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000263 printf("\nRandom Test (y/n)?");
264 EXPECT_TRUE(fgets(print, 10, stdin) != NULL);
265 print[10] = '\0';
266 if (strstr(print, "y") != NULL) {
267 _randomTest = true;
268 _verbose = false;
269 _writeToFile = false;
270 } else {
271 _randomTest = false;
272 printf("\nPrint Tests (y/n)? ");
kjellander@webrtc.org543c3ea2011-11-23 12:20:35 +0000273 EXPECT_TRUE(fgets(print, 10, stdin) != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000274 print[10] = '\0';
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000275 if (strstr(print, "y") == NULL) {
276 EXPECT_TRUE(freopen("APITest_log.txt", "w", stdout) != 0);
277 _verbose = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000278 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000279 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000280
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000281 _vadCallbackA = new VADCallback;
282 _vadCallbackB = new VADCallback;
andrew@webrtc.orgd7a71d02012-08-01 01:40:02 +0000283
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000284 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000285}
286
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000287bool APITest::PushAudioThreadA(void* obj) {
288 return static_cast<APITest*>(obj)->PushAudioRunA();
niklase@google.com470e71d2011-07-07 08:21:25 +0000289}
290
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000291bool APITest::PushAudioThreadB(void* obj) {
292 return static_cast<APITest*>(obj)->PushAudioRunB();
niklase@google.com470e71d2011-07-07 08:21:25 +0000293}
294
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000295bool APITest::PullAudioThreadA(void* obj) {
296 return static_cast<APITest*>(obj)->PullAudioRunA();
niklase@google.com470e71d2011-07-07 08:21:25 +0000297}
298
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000299bool APITest::PullAudioThreadB(void* obj) {
300 return static_cast<APITest*>(obj)->PullAudioRunB();
niklase@google.com470e71d2011-07-07 08:21:25 +0000301}
302
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000303bool APITest::ProcessThreadA(void* obj) {
304 return static_cast<APITest*>(obj)->ProcessRunA();
niklase@google.com470e71d2011-07-07 08:21:25 +0000305}
306
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000307bool APITest::ProcessThreadB(void* obj) {
308 return static_cast<APITest*>(obj)->ProcessRunB();
niklase@google.com470e71d2011-07-07 08:21:25 +0000309}
310
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000311bool APITest::APIThreadA(void* obj) {
312 return static_cast<APITest*>(obj)->APIRunA();
niklase@google.com470e71d2011-07-07 08:21:25 +0000313}
314
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000315bool APITest::APIThreadB(void* obj) {
316 return static_cast<APITest*>(obj)->APIRunB();
niklase@google.com470e71d2011-07-07 08:21:25 +0000317}
318
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000319bool APITest::PullAudioRunA() {
320 _pullEventA->Wait(100);
321 AudioFrame audioFrame;
henrik.lundind4ccb002016-05-17 12:21:55 -0700322 bool muted;
323 if (_acmA->PlayoutData10Ms(_outFreqHzA, &audioFrame, &muted) < 0) {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000324 bool thereIsDecoder;
niklase@google.com470e71d2011-07-07 08:21:25 +0000325 {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000326 ReadLockScoped rl(_apiTestRWLock);
327 thereIsDecoder = _thereIsDecoderA;
niklase@google.com470e71d2011-07-07 08:21:25 +0000328 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000329 if (thereIsDecoder) {
330 fprintf(stderr, "\n>>>>>> cannot pull audio A <<<<<<<< \n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000331 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000332 } else {
333 if (_writeToFile) {
334 _outFileA.Write10MsData(audioFrame);
335 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000336 }
337 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000338}
339
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000340bool APITest::PullAudioRunB() {
341 _pullEventB->Wait(100);
342 AudioFrame audioFrame;
henrik.lundind4ccb002016-05-17 12:21:55 -0700343 bool muted;
344 if (_acmB->PlayoutData10Ms(_outFreqHzB, &audioFrame, &muted) < 0) {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000345 bool thereIsDecoder;
niklase@google.com470e71d2011-07-07 08:21:25 +0000346 {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000347 ReadLockScoped rl(_apiTestRWLock);
348 thereIsDecoder = _thereIsDecoderB;
niklase@google.com470e71d2011-07-07 08:21:25 +0000349 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000350 if (thereIsDecoder) {
351 fprintf(stderr, "\n>>>>>> cannot pull audio B <<<<<<<< \n");
352 fprintf(stderr, "%d %d\n", _testNumA, _testNumB);
andrew@webrtc.orgd7a71d02012-08-01 01:40:02 +0000353 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000354 } else {
355 if (_writeToFile) {
356 _outFileB.Write10MsData(audioFrame);
357 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000358 }
359 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000360}
361
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000362bool APITest::PushAudioRunA() {
363 _pushEventA->Wait(100);
364 AudioFrame audioFrame;
365 _inFileA.Read10MsData(audioFrame);
366 if (_acmA->Add10MsData(audioFrame) < 0) {
367 bool thereIsEncoder;
niklase@google.com470e71d2011-07-07 08:21:25 +0000368 {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000369 ReadLockScoped rl(_apiTestRWLock);
370 thereIsEncoder = _thereIsEncoderA;
niklase@google.com470e71d2011-07-07 08:21:25 +0000371 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000372 if (thereIsEncoder) {
373 fprintf(stderr, "\n>>>> add10MsData at A failed <<<<\n");
374 }
375 }
376 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000377}
378
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000379bool APITest::PushAudioRunB() {
380 _pushEventB->Wait(100);
381 AudioFrame audioFrame;
382 _inFileB.Read10MsData(audioFrame);
383 if (_acmB->Add10MsData(audioFrame) < 0) {
384 bool thereIsEncoder;
niklase@google.com470e71d2011-07-07 08:21:25 +0000385 {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000386 ReadLockScoped rl(_apiTestRWLock);
387 thereIsEncoder = _thereIsEncoderB;
niklase@google.com470e71d2011-07-07 08:21:25 +0000388 }
389
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000390 if (thereIsEncoder) {
391 fprintf(stderr, "\n>>>> cannot add audio to B <<<<");
392 }
393 }
394
395 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000396}
397
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000398bool APITest::ProcessRunA() {
399 _processEventA->Wait(100);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000400 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000401}
402
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000403bool APITest::ProcessRunB() {
404 _processEventB->Wait(100);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000405 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000406}
407
408/*/
409 *
410 * In side A we test the APIs which are related to sender Side.
411 *
412/*/
413
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000414void APITest::RunTest(char thread) {
415 int testNum;
416 {
417 WriteLockScoped cs(_apiTestRWLock);
418 if (thread == 'A') {
henrik.lundin1bd0e032015-09-28 06:12:17 -0700419 _testNumA = (_testNumB + 1 + (rand() % 3)) % 4;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000420 testNum = _testNumA;
niklase@google.com470e71d2011-07-07 08:21:25 +0000421
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000422 _movingDot[_dotPositionA] = ' ';
423 if (_dotPositionA == 0) {
424 _dotMoveDirectionA = 1;
425 }
426 if (_dotPositionA == 19) {
427 _dotMoveDirectionA = -1;
428 }
429 _dotPositionA += _dotMoveDirectionA;
430 _movingDot[_dotPositionA] = (_dotMoveDirectionA > 0) ? '>' : '<';
431 } else {
henrik.lundin1bd0e032015-09-28 06:12:17 -0700432 _testNumB = (_testNumA + 1 + (rand() % 3)) % 4;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000433 testNum = _testNumB;
andrew@webrtc.orgd7a71d02012-08-01 01:40:02 +0000434
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000435 _movingDot[_dotPositionB] = ' ';
436 if (_dotPositionB == 20) {
437 _dotMoveDirectionB = 1;
438 }
439 if (_dotPositionB == 39) {
440 _dotMoveDirectionB = -1;
441 }
442 _dotPositionB += _dotMoveDirectionB;
443 _movingDot[_dotPositionB] = (_dotMoveDirectionB > 0) ? '>' : '<';
niklase@google.com470e71d2011-07-07 08:21:25 +0000444 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000445 //fprintf(stderr, "%c: %d \n", thread, testNum);
446 //fflush(stderr);
447 }
448 switch (testNum) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000449 case 0:
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000450 CurrentCodec('A');
451 ChangeCodec('A');
452 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000453 case 1:
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000454 if (!_randomTest) {
455 fprintf(stdout, "\nTesting Delay ...\n");
456 }
457 TestDelay('A');
458 break;
henrik.lundin1bd0e032015-09-28 06:12:17 -0700459 case 2:
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000460 TestSendVAD('A');
461 break;
henrik.lundin1bd0e032015-09-28 06:12:17 -0700462 case 3:
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000463 TestRegisteration('A');
464 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000465 default:
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000466 fprintf(stderr, "Wrong Test Number\n");
marpan@webrtc.org4765ca52014-11-03 20:10:26 +0000467 getc(stdin);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000468 exit(1);
469 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000470}
471
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000472bool APITest::APIRunA() {
473 _apiEventA->Wait(50);
niklase@google.com470e71d2011-07-07 08:21:25 +0000474
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000475 bool randomTest;
476 {
477 ReadLockScoped rl(_apiTestRWLock);
478 randomTest = _randomTest;
479 }
480 if (randomTest) {
481 RunTest('A');
482 } else {
483 CurrentCodec('A');
484 ChangeCodec('A');
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000485 if (_codecCntrA == 0) {
486 fprintf(stdout, "\nTesting Delay ...\n");
487 TestDelay('A');
niklase@google.com470e71d2011-07-07 08:21:25 +0000488 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000489 // VAD TEST
490 TestSendVAD('A');
491 TestRegisteration('A');
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000492 }
493 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000494}
495
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000496bool APITest::APIRunB() {
497 _apiEventB->Wait(50);
498 bool randomTest;
499 {
500 ReadLockScoped rl(_apiTestRWLock);
501 randomTest = _randomTest;
502 }
503 //_apiEventB->Wait(2000);
504 if (randomTest) {
505 RunTest('B');
506 }
andrew@webrtc.orgd7a71d02012-08-01 01:40:02 +0000507
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000508 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000509}
510
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000511void APITest::Perform() {
512 SetUp();
niklase@google.com470e71d2011-07-07 08:21:25 +0000513
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000514 //--- THREADS
515 // A
516 // PUSH
Peter Boström8c38e8b2015-11-26 17:45:47 +0100517 rtc::PlatformThread myPushAudioThreadA(PushAudioThreadA, this,
518 "PushAudioThreadA");
519 myPushAudioThreadA.Start();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000520 // PULL
Peter Boström8c38e8b2015-11-26 17:45:47 +0100521 rtc::PlatformThread myPullAudioThreadA(PullAudioThreadA, this,
522 "PullAudioThreadA");
523 myPullAudioThreadA.Start();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000524 // Process
Peter Boström8c38e8b2015-11-26 17:45:47 +0100525 rtc::PlatformThread myProcessThreadA(ProcessThreadA, this, "ProcessThreadA");
526 myProcessThreadA.Start();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000527 // API
Peter Boström8c38e8b2015-11-26 17:45:47 +0100528 rtc::PlatformThread myAPIThreadA(APIThreadA, this, "APIThreadA");
529 myAPIThreadA.Start();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000530 // B
531 // PUSH
Peter Boström8c38e8b2015-11-26 17:45:47 +0100532 rtc::PlatformThread myPushAudioThreadB(PushAudioThreadB, this,
533 "PushAudioThreadB");
534 myPushAudioThreadB.Start();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000535 // PULL
Peter Boström8c38e8b2015-11-26 17:45:47 +0100536 rtc::PlatformThread myPullAudioThreadB(PullAudioThreadB, this,
537 "PullAudioThreadB");
538 myPullAudioThreadB.Start();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000539 // Process
Peter Boström8c38e8b2015-11-26 17:45:47 +0100540 rtc::PlatformThread myProcessThreadB(ProcessThreadB, this, "ProcessThreadB");
541 myProcessThreadB.Start();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000542 // API
Peter Boström8c38e8b2015-11-26 17:45:47 +0100543 rtc::PlatformThread myAPIThreadB(APIThreadB, this, "APIThreadB");
544 myAPIThreadB.Start();
andrew@webrtc.orgd7a71d02012-08-01 01:40:02 +0000545
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000546 //_apiEventA->StartTimer(true, 5000);
547 //_apiEventB->StartTimer(true, 5000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000548
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000549 _processEventA->StartTimer(true, 10);
550 _processEventB->StartTimer(true, 10);
niklase@google.com470e71d2011-07-07 08:21:25 +0000551
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000552 _pullEventA->StartTimer(true, 10);
553 _pullEventB->StartTimer(true, 10);
andrew@webrtc.orgd7a71d02012-08-01 01:40:02 +0000554
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000555 _pushEventA->StartTimer(true, 10);
556 _pushEventB->StartTimer(true, 10);
niklase@google.com470e71d2011-07-07 08:21:25 +0000557
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000558 // Keep main thread waiting for sender/receiver
559 // threads to complete
560 EventWrapper* completeEvent = EventWrapper::Create();
Niels Möllerd28db7f2016-05-10 16:31:47 +0200561 uint64_t startTime = rtc::TimeMillis();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000562 uint64_t currentTime;
563 // Run test in 2 minutes (120000 ms).
564 do {
niklase@google.com470e71d2011-07-07 08:21:25 +0000565 {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000566 //ReadLockScoped rl(_apiTestRWLock);
567 //fprintf(stderr, "\r%s", _movingDot);
568 }
569 //fflush(stderr);
570 completeEvent->Wait(50);
Niels Möllerd28db7f2016-05-10 16:31:47 +0200571 currentTime = rtc::TimeMillis();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000572 } while ((currentTime - startTime) < 120000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000573
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000574 //completeEvent->Wait(0xFFFFFFFF);
575 //(unsigned long)((unsigned long)TEST_DURATION_SEC * (unsigned long)1000));
576 delete completeEvent;
andrew@webrtc.orgd7a71d02012-08-01 01:40:02 +0000577
Peter Boström8c38e8b2015-11-26 17:45:47 +0100578 myPushAudioThreadA.Stop();
579 myPullAudioThreadA.Stop();
580 myProcessThreadA.Stop();
581 myAPIThreadA.Stop();
niklase@google.com470e71d2011-07-07 08:21:25 +0000582
Peter Boström8c38e8b2015-11-26 17:45:47 +0100583 myPushAudioThreadB.Stop();
584 myPullAudioThreadB.Stop();
585 myProcessThreadB.Stop();
586 myAPIThreadB.Stop();
niklase@google.com470e71d2011-07-07 08:21:25 +0000587}
588
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000589void APITest::CheckVADStatus(char side) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000590
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000591 bool dtxEnabled;
592 bool vadEnabled;
593 ACMVADMode vadMode;
niklase@google.com470e71d2011-07-07 08:21:25 +0000594
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000595 if (side == 'A') {
596 _acmA->VAD(&dtxEnabled, &vadEnabled, &vadMode);
597 _acmA->RegisterVADCallback(NULL);
598 _vadCallbackA->Reset();
599 _acmA->RegisterVADCallback(_vadCallbackA);
tina.legrand@webrtc.org2e096922011-08-18 06:20:30 +0000600
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000601 if (!_randomTest) {
602 if (_verbose) {
603 fprintf(stdout, "DTX %3s, VAD %3s, Mode %d", dtxEnabled ? "ON" : "OFF",
604 vadEnabled ? "ON" : "OFF", (int) vadMode);
605 Wait(5000);
606 fprintf(stdout, " => bit-rate %3.0f kbps\n", _channel_A2B->BitRate());
607 } else {
608 Wait(5000);
609 fprintf(stdout, "DTX %3s, VAD %3s, Mode %d => bit-rate %3.0f kbps\n",
610 dtxEnabled ? "ON" : "OFF", vadEnabled ? "ON" : "OFF",
611 (int) vadMode, _channel_A2B->BitRate());
612 }
613 _vadCallbackA->PrintFrameTypes();
niklase@google.com470e71d2011-07-07 08:21:25 +0000614 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000615
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000616 if (dtxEnabled != _sendDTXA) {
617 fprintf(stderr, ">>> Error Enabling DTX <<<\n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000618 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000619 if ((vadEnabled != _sendVADA) && (!dtxEnabled)) {
620 fprintf(stderr, ">>> Error Enabling VAD <<<\n");
621 }
622 if ((vadMode != _sendVADModeA) && vadEnabled) {
623 fprintf(stderr, ">>> Error setting VAD-mode <<<\n");
624 }
625 } else {
626 _acmB->VAD(&dtxEnabled, &vadEnabled, &vadMode);
627
628 _acmB->RegisterVADCallback(NULL);
629 _vadCallbackB->Reset();
630 _acmB->RegisterVADCallback(_vadCallbackB);
631
632 if (!_randomTest) {
633 if (_verbose) {
634 fprintf(stdout, "DTX %3s, VAD %3s, Mode %d", dtxEnabled ? "ON" : "OFF",
635 vadEnabled ? "ON" : "OFF", (int) vadMode);
636 Wait(5000);
637 fprintf(stdout, " => bit-rate %3.0f kbps\n", _channel_B2A->BitRate());
638 } else {
639 Wait(5000);
640 fprintf(stdout, "DTX %3s, VAD %3s, Mode %d => bit-rate %3.0f kbps\n",
641 dtxEnabled ? "ON" : "OFF", vadEnabled ? "ON" : "OFF",
642 (int) vadMode, _channel_B2A->BitRate());
643 }
644 _vadCallbackB->PrintFrameTypes();
645 }
646
647 if (dtxEnabled != _sendDTXB) {
648 fprintf(stderr, ">>> Error Enabling DTX <<<\n");
649 }
650 if ((vadEnabled != _sendVADB) && (!dtxEnabled)) {
651 fprintf(stderr, ">>> Error Enabling VAD <<<\n");
652 }
653 if ((vadMode != _sendVADModeB) && vadEnabled) {
654 fprintf(stderr, ">>> Error setting VAD-mode <<<\n");
655 }
656 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000657}
658
659// Set Min delay, get delay, playout timestamp
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000660void APITest::TestDelay(char side) {
661 AudioCodingModule* myACM;
662 Channel* myChannel;
663 int32_t* myMinDelay;
Peter Boström64c03662015-04-08 11:24:19 +0200664 EventTimerWrapper* myEvent = EventTimerWrapper::Create();
niklase@google.com470e71d2011-07-07 08:21:25 +0000665
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000666 uint32_t inTimestamp = 0;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000667 double estimDelay = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000668
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000669 double averageEstimDelay = 0;
670 double averageDelay = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000671
brandtr6607d842017-02-11 00:24:10 -0800672 test::CircularBuffer estimDelayCB(100);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000673 estimDelayCB.SetArithMean(true);
niklase@google.com470e71d2011-07-07 08:21:25 +0000674
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000675 if (side == 'A') {
turaj@webrtc.org6ea3d1c2013-10-02 21:44:33 +0000676 myACM = _acmA.get();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000677 myChannel = _channel_B2A;
678 myMinDelay = &_minDelayA;
679 } else {
turaj@webrtc.org6ea3d1c2013-10-02 21:44:33 +0000680 myACM = _acmB.get();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000681 myChannel = _channel_A2B;
682 myMinDelay = &_minDelayB;
683 }
684
685 CHECK_ERROR_MT(myACM->SetMinimumPlayoutDelay(*myMinDelay));
686
687 inTimestamp = myChannel->LastInTimestamp();
henrik.lundin9a410dd2016-04-06 01:39:22 -0700688 rtc::Optional<uint32_t> outTimestamp = myACM->PlayoutTimestamp();
689 CHECK_ERROR_MT(outTimestamp ? 0 : -1);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000690
691 if (!_randomTest) {
692 myEvent->StartTimer(true, 30);
693 int n = 0;
694 int settlePoint = 5000;
695 while (n < settlePoint + 400) {
696 myEvent->Wait(1000);
697
698 inTimestamp = myChannel->LastInTimestamp();
henrik.lundin9a410dd2016-04-06 01:39:22 -0700699 outTimestamp = myACM->PlayoutTimestamp();
700 CHECK_ERROR_MT(outTimestamp ? 0 : -1);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000701
702 //std::cout << outTimestamp << std::endl << std::flush;
henrik.lundin9a410dd2016-04-06 01:39:22 -0700703 estimDelay = (double)((uint32_t)(inTimestamp - *outTimestamp)) /
704 ((double)myACM->ReceiveFrequency() / 1000.0);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000705
706 estimDelayCB.Update(estimDelay);
707
708 estimDelayCB.ArithMean(averageEstimDelay);
709 //printf("\n %6.1f \n", estimDelay);
710 //std::cout << " " << std::flush;
711
712 if (_verbose) {
713 fprintf(stdout,
714 "\rExpected: %4d, retreived: %6.1f, measured: %6.1f",
715 *myMinDelay, averageDelay, averageEstimDelay);
716 std::cout << " " << std::flush;
717 }
718 if ((averageDelay > *myMinDelay) && (n < settlePoint)) {
719 settlePoint = n;
720 }
721 n++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000722 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000723 myEvent->StopTimer();
724 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000725
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000726 if ((!_verbose) && (!_randomTest)) {
727 fprintf(stdout, "\nExpected: %4d, retreived: %6.1f, measured: %6.1f",
niklase@google.com470e71d2011-07-07 08:21:25 +0000728 *myMinDelay, averageDelay, averageEstimDelay);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000729 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000730
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000731 *myMinDelay = (rand() % 1000) + 1;
andrew@webrtc.orgd7a71d02012-08-01 01:40:02 +0000732
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000733 NetworkStatistics networkStat;
734 CHECK_ERROR_MT(myACM->GetNetworkStatistics(&networkStat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000735
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000736 if (!_randomTest) {
737 fprintf(stdout, "\n\nJitter Statistics at Side %c\n", side);
738 fprintf(stdout, "--------------------------------------\n");
739 fprintf(stdout, "buffer-size............. %d\n",
740 networkStat.currentBufferSize);
741 fprintf(stdout, "Preferred buffer-size... %d\n",
742 networkStat.preferredBufferSize);
743 fprintf(stdout, "Peaky jitter mode........%d\n",
744 networkStat.jitterPeaksFound);
745 fprintf(stdout, "packet-size rate........ %d\n",
746 networkStat.currentPacketLossRate);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000747 fprintf(stdout, "expand rate............. %d\n",
748 networkStat.currentExpandRate);
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000749 fprintf(stdout, "speech expand rate...... %d\n",
750 networkStat.currentSpeechExpandRate);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000751 fprintf(stdout, "Preemptive rate......... %d\n",
752 networkStat.currentPreemptiveRate);
753 fprintf(stdout, "Accelerate rate......... %d\n",
754 networkStat.currentAccelerateRate);
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000755 fprintf(stdout, "Secondary decoded rate.. %d\n",
756 networkStat.currentSecondaryDecodedRate);
minyue-webrtc0c3ca752017-08-23 15:59:38 +0200757 fprintf(stdout, "Secondary discarded rate.%d\n",
758 networkStat.currentSecondaryDiscardedRate);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000759 fprintf(stdout, "Clock-drift............. %d\n", networkStat.clockDriftPPM);
760 fprintf(stdout, "Mean waiting time....... %d\n",
761 networkStat.meanWaitingTimeMs);
762 fprintf(stdout, "Median waiting time..... %d\n",
763 networkStat.medianWaitingTimeMs);
764 fprintf(stdout, "Min waiting time........ %d\n",
765 networkStat.minWaitingTimeMs);
766 fprintf(stdout, "Max waiting time........ %d\n",
767 networkStat.maxWaitingTimeMs);
768 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000769
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000770 CHECK_ERROR_MT(myACM->SetMinimumPlayoutDelay(*myMinDelay));
niklase@google.com470e71d2011-07-07 08:21:25 +0000771
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000772 if (!_randomTest) {
773 myEvent->Wait(500);
774 fprintf(stdout, "\n");
775 fprintf(stdout, "\n");
776 }
777 delete myEvent;
niklase@google.com470e71d2011-07-07 08:21:25 +0000778}
779
780// Unregister a codec & register again.
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000781void APITest::TestRegisteration(char sendSide) {
782 AudioCodingModule* sendACM;
783 AudioCodingModule* receiveACM;
784 bool* thereIsDecoder;
785 EventWrapper* myEvent = EventWrapper::Create();
niklase@google.com470e71d2011-07-07 08:21:25 +0000786
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000787 if (!_randomTest) {
788 fprintf(stdout, "\n\n");
789 fprintf(stdout,
790 "---------------------------------------------------------\n");
791 fprintf(stdout, " Unregister/register Receive Codec\n");
792 fprintf(stdout,
793 "---------------------------------------------------------\n");
794 }
795
796 switch (sendSide) {
797 case 'A': {
turaj@webrtc.org6ea3d1c2013-10-02 21:44:33 +0000798 sendACM = _acmA.get();
799 receiveACM = _acmB.get();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000800 thereIsDecoder = &_thereIsDecoderB;
801 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000802 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000803 case 'B': {
turaj@webrtc.org6ea3d1c2013-10-02 21:44:33 +0000804 sendACM = _acmB.get();
805 receiveACM = _acmA.get();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000806 thereIsDecoder = &_thereIsDecoderA;
807 break;
808 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000809 default:
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000810 fprintf(stderr, "Invalid sender-side in TestRegistration(%c)\n",
811 sendSide);
812 exit(-1);
813 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000814
kwiberg1fd4a4a2015-11-03 11:20:50 -0800815 auto myCodec = sendACM->SendCodec();
816 if (!myCodec) {
817 CodecInst ci;
818 AudioCodingModule::Codec(_codecCntrA, &ci);
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100819 myCodec = ci;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000820 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000821
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000822 if (!_randomTest) {
823 fprintf(stdout, "Unregistering reveive codec, NO AUDIO.\n");
824 fflush (stdout);
825 }
826 {
827 WriteLockScoped wl(_apiTestRWLock);
828 *thereIsDecoder = false;
829 }
830 //myEvent->Wait(20);
kwiberg1fd4a4a2015-11-03 11:20:50 -0800831 CHECK_ERROR_MT(receiveACM->UnregisterReceiveCodec(myCodec->pltype));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000832 Wait(1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000833
kwiberg1fd4a4a2015-11-03 11:20:50 -0800834 int currentPayload = myCodec->pltype;
niklase@google.com470e71d2011-07-07 08:21:25 +0000835
kwiberg1fd4a4a2015-11-03 11:20:50 -0800836 if (!FixedPayloadTypeCodec(myCodec->plname)) {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000837 int32_t i;
838 for (i = 0; i < 32; i++) {
839 if (!_payloadUsed[i]) {
840 if (!_randomTest) {
841 fprintf(stdout,
842 "Register receive codec with new Payload, AUDIO BACK.\n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000843 }
kwiberg1fd4a4a2015-11-03 11:20:50 -0800844 //myCodec->pltype = i + 96;
845 //CHECK_ERROR_MT(receiveACM->RegisterReceiveCodec(*myCodec));
846 //CHECK_ERROR_MT(sendACM->RegisterSendCodec(*myCodec));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000847 //myEvent->Wait(20);
848 //{
849 // WriteLockScoped wl(_apiTestRWLock);
850 // *thereIsDecoder = true;
851 //}
852 Wait(1000);
853
854 if (!_randomTest) {
855 fprintf(stdout, "Unregistering reveive codec, NO AUDIO.\n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000856 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000857 //{
858 // WriteLockScoped wl(_apiTestRWLock);
859 // *thereIsDecoder = false;
860 //}
861 //myEvent->Wait(20);
kwiberg1fd4a4a2015-11-03 11:20:50 -0800862 //CHECK_ERROR_MT(receiveACM->UnregisterReceiveCodec(myCodec->pltype));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000863 Wait(1000);
864
kwiberg1fd4a4a2015-11-03 11:20:50 -0800865 myCodec->pltype = currentPayload;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000866 if (!_randomTest) {
867 fprintf(stdout,
868 "Register receive codec with default Payload, AUDIO BACK.\n");
869 fflush (stdout);
niklase@google.com470e71d2011-07-07 08:21:25 +0000870 }
kwibergda2bf4e2016-10-24 13:47:09 -0700871 EXPECT_EQ(true, receiveACM->RegisterReceiveCodec(
872 myCodec->pltype, CodecInstToSdp(*myCodec)));
kwiberg1fd4a4a2015-11-03 11:20:50 -0800873 //CHECK_ERROR_MT(sendACM->RegisterSendCodec(*myCodec));
niklase@google.com470e71d2011-07-07 08:21:25 +0000874 myEvent->Wait(20);
875 {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000876 WriteLockScoped wl(_apiTestRWLock);
877 *thereIsDecoder = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000878 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000879 Wait(1000);
880
881 break;
882 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000883 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000884 if (i == 32) {
kwibergda2bf4e2016-10-24 13:47:09 -0700885 EXPECT_EQ(true, receiveACM->RegisterReceiveCodec(
886 myCodec->pltype, CodecInstToSdp(*myCodec)));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000887 {
888 WriteLockScoped wl(_apiTestRWLock);
889 *thereIsDecoder = true;
890 }
891 }
892 } else {
893 if (!_randomTest) {
894 fprintf(stdout,
895 "Register receive codec with fixed Payload, AUDIO BACK.\n");
896 fflush (stdout);
897 }
kwibergda2bf4e2016-10-24 13:47:09 -0700898 EXPECT_EQ(true, receiveACM->RegisterReceiveCodec(myCodec->pltype,
899 CodecInstToSdp(*myCodec)));
kwiberg1fd4a4a2015-11-03 11:20:50 -0800900 //CHECK_ERROR_MT(receiveACM->UnregisterReceiveCodec(myCodec->pltype));
901 //CHECK_ERROR_MT(receiveACM->RegisterReceiveCodec(*myCodec));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000902 myEvent->Wait(20);
niklase@google.com470e71d2011-07-07 08:21:25 +0000903 {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000904 WriteLockScoped wl(_apiTestRWLock);
905 *thereIsDecoder = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000906 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000907 }
908 delete myEvent;
909 if (!_randomTest) {
910 fprintf(stdout,
911 "---------------------------------------------------------\n");
912 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000913}
914
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000915void APITest::TestSendVAD(char side) {
916 if (_randomTest) {
917 return;
918 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000919
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000920 bool* vad;
921 bool* dtx;
922 ACMVADMode* mode;
923 Channel* myChannel;
924 AudioCodingModule* myACM;
niklase@google.com470e71d2011-07-07 08:21:25 +0000925
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000926 CodecInst myCodec;
927 if (!_randomTest) {
928 fprintf(stdout, "\n\n");
929 fprintf(stdout, "-----------------------------------------------\n");
930 fprintf(stdout, " Test VAD API\n");
931 fprintf(stdout, "-----------------------------------------------\n");
932 }
andrew@webrtc.orgd7a71d02012-08-01 01:40:02 +0000933
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000934 if (side == 'A') {
935 AudioCodingModule::Codec(_codecCntrA, &myCodec);
936 vad = &_sendVADA;
937 dtx = &_sendDTXA;
938 mode = &_sendVADModeA;
939 myChannel = _channel_A2B;
turaj@webrtc.org6ea3d1c2013-10-02 21:44:33 +0000940 myACM = _acmA.get();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000941 } else {
942 AudioCodingModule::Codec(_codecCntrB, &myCodec);
943 vad = &_sendVADB;
944 dtx = &_sendDTXB;
945 mode = &_sendVADModeB;
946 myChannel = _channel_B2A;
turaj@webrtc.org6ea3d1c2013-10-02 21:44:33 +0000947 myACM = _acmB.get();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000948 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000949
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000950 CheckVADStatus(side);
951 if (!_randomTest) {
952 fprintf(stdout, "\n\n");
953 }
954
955 switch (*mode) {
956 case VADNormal:
957 *vad = true;
958 *dtx = true;
959 *mode = VADAggr;
960 break;
961 case VADLowBitrate:
962 *vad = true;
963 *dtx = true;
964 *mode = VADVeryAggr;
965 break;
966 case VADAggr:
967 *vad = true;
968 *dtx = true;
969 *mode = VADLowBitrate;
970 break;
971 case VADVeryAggr:
972 *vad = false;
973 *dtx = false;
974 *mode = VADNormal;
975 break;
976 default:
977 *mode = VADNormal;
978 }
979
980 *dtx = (myCodec.plfreq == 32000) ? false : *dtx;
981
982 CHECK_ERROR_MT(myACM->SetVAD(*dtx, *vad, *mode));
983 myChannel->ResetStats();
984
985 CheckVADStatus(side);
986 if (!_randomTest) {
987 fprintf(stdout, "\n");
988 fprintf(stdout, "-----------------------------------------------\n");
989 }
990
991 // Fault Test
992 CHECK_PROTECTED_MT(myACM->SetVAD(false, true, (ACMVADMode) - 1));
993 CHECK_PROTECTED_MT(myACM->SetVAD(false, true, (ACMVADMode) 4));
niklase@google.com470e71d2011-07-07 08:21:25 +0000994
995}
996
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000997void APITest::CurrentCodec(char side) {
kwiberg1fd4a4a2015-11-03 11:20:50 -0800998 auto myCodec = (side == 'A' ? _acmA : _acmB)->SendCodec();
niklase@google.com470e71d2011-07-07 08:21:25 +0000999
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001000 if (!_randomTest) {
1001 fprintf(stdout, "\n\n");
1002 fprintf(stdout, "Send codec in Side A\n");
1003 fprintf(stdout, "----------------------------\n");
kwiberg1fd4a4a2015-11-03 11:20:50 -08001004 fprintf(stdout, "Name................. %s\n", myCodec->plname);
1005 fprintf(stdout, "Sampling Frequency... %d\n", myCodec->plfreq);
1006 fprintf(stdout, "Rate................. %d\n", myCodec->rate);
1007 fprintf(stdout, "Payload-type......... %d\n", myCodec->pltype);
1008 fprintf(stdout, "Packet-size.......... %d\n", myCodec->pacsize);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001009 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001010
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001011 Wait(100);
niklase@google.com470e71d2011-07-07 08:21:25 +00001012}
1013
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001014void APITest::ChangeCodec(char side) {
1015 CodecInst myCodec;
1016 AudioCodingModule* myACM;
1017 uint8_t* codecCntr;
1018 bool* thereIsEncoder;
1019 bool* vad;
1020 bool* dtx;
1021 ACMVADMode* mode;
1022 Channel* myChannel;
1023 // Reset and Wait
1024 if (!_randomTest) {
1025 fprintf(stdout, "Reset Encoder Side A \n");
1026 }
1027 if (side == 'A') {
turaj@webrtc.org6ea3d1c2013-10-02 21:44:33 +00001028 myACM = _acmA.get();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001029 codecCntr = &_codecCntrA;
niklase@google.com470e71d2011-07-07 08:21:25 +00001030 {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001031 WriteLockScoped wl(_apiTestRWLock);
1032 thereIsEncoder = &_thereIsEncoderA;
niklase@google.com470e71d2011-07-07 08:21:25 +00001033 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001034 vad = &_sendVADA;
1035 dtx = &_sendDTXA;
1036 mode = &_sendVADModeA;
1037 myChannel = _channel_A2B;
1038 } else {
turaj@webrtc.org6ea3d1c2013-10-02 21:44:33 +00001039 myACM = _acmB.get();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001040 codecCntr = &_codecCntrB;
niklase@google.com470e71d2011-07-07 08:21:25 +00001041 {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001042 WriteLockScoped wl(_apiTestRWLock);
1043 thereIsEncoder = &_thereIsEncoderB;
niklase@google.com470e71d2011-07-07 08:21:25 +00001044 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001045 vad = &_sendVADB;
1046 dtx = &_sendDTXB;
1047 mode = &_sendVADModeB;
1048 myChannel = _channel_B2A;
1049 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001050
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001051 Wait(100);
niklase@google.com470e71d2011-07-07 08:21:25 +00001052
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001053 // Register the next codec
1054 do {
1055 *codecCntr =
1056 (*codecCntr < AudioCodingModule::NumberOfCodecs() - 1) ?
1057 (*codecCntr + 1) : 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001058
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001059 if (*codecCntr == 0) {
1060 //printf("Initialize Sender Side A \n");
1061 {
niklase@google.com470e71d2011-07-07 08:21:25 +00001062 WriteLockScoped wl(_apiTestRWLock);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001063 *thereIsEncoder = false;
1064 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001065 // After Initialization CN is lost, re-register them
1066 if (AudioCodingModule::Codec("CN", &myCodec, 8000, 1) >= 0) {
1067 CHECK_ERROR_MT(myACM->RegisterSendCodec(myCodec));
1068 }
1069 if (AudioCodingModule::Codec("CN", &myCodec, 16000, 1) >= 0) {
1070 CHECK_ERROR_MT(myACM->RegisterSendCodec(myCodec));
1071 }
1072 // VAD & DTX are disabled after initialization
1073 *vad = false;
1074 *dtx = false;
1075 _writeToFile = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001076 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001077
1078 AudioCodingModule::Codec(*codecCntr, &myCodec);
1079 } while (!STR_CASE_CMP(myCodec.plname, "CN")
1080 || !STR_CASE_CMP(myCodec.plname, "telephone-event")
1081 || !STR_CASE_CMP(myCodec.plname, "RED"));
1082
1083 if (!_randomTest) {
1084 fprintf(stdout,"\n=====================================================\n");
1085 fprintf(stdout, " Registering New Codec %s, %d kHz, %d kbps\n",
1086 myCodec.plname, myCodec.plfreq / 1000, myCodec.rate / 1000);
1087 }
1088 //std::cout<< std::flush;
1089
1090 // NO DTX for supe-wideband codec at this point
1091 if (myCodec.plfreq == 32000) {
1092 *dtx = false;
1093 CHECK_ERROR_MT(myACM->SetVAD(*dtx, *vad, *mode));
1094
1095 }
1096
1097 CHECK_ERROR_MT(myACM->RegisterSendCodec(myCodec));
1098 myChannel->ResetStats();
1099 {
1100 WriteLockScoped wl(_apiTestRWLock);
1101 *thereIsEncoder = true;
1102 }
1103 Wait(500);
niklase@google.com470e71d2011-07-07 08:21:25 +00001104}
1105
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001106} // namespace webrtc