blob: 25bacfd60bf1e108e348492a0c8cc5fff1a69459 [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
kjellander3e6db232015-11-26 04:44:54 -080011#include "webrtc/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
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000022#include "testing/gtest/include/gtest/gtest.h"
pbos12411ef2015-11-23 14:47:56 -080023#include "webrtc/base/platform_thread.h"
Niels Möllerd28db7f2016-05-10 16:31:47 +020024#include "webrtc/base/timeutils.h"
turaj@webrtc.org6ea3d1c2013-10-02 21:44:33 +000025#include "webrtc/common.h"
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000026#include "webrtc/common_types.h"
27#include "webrtc/engine_configurations.h"
kjellander3e6db232015-11-26 04:44:54 -080028#include "webrtc/modules/audio_coding/acm2/acm_common_defs.h"
29#include "webrtc/modules/audio_coding/test/utility.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010030#include "webrtc/system_wrappers/include/event_wrapper.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010031#include "webrtc/system_wrappers/include/trace.h"
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000032#include "webrtc/test/testsupport/fileutils.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000033
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000034namespace webrtc {
35
niklase@google.com470e71d2011-07-07 08:21:25 +000036#define TEST_DURATION_SEC 600
niklase@google.com470e71d2011-07-07 08:21:25 +000037#define NUMBER_OF_SENDER_TESTS 6
niklase@google.com470e71d2011-07-07 08:21:25 +000038#define MAX_FILE_NAME_LENGTH_BYTE 500
niklase@google.com470e71d2011-07-07 08:21:25 +000039
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000040void APITest::Wait(uint32_t waitLengthMs) {
41 if (_randomTest) {
42 return;
43 } else {
44 EventWrapper* myEvent = EventWrapper::Create();
45 myEvent->Wait(waitLengthMs);
46 delete myEvent;
47 return;
48 }
niklase@google.com470e71d2011-07-07 08:21:25 +000049}
50
turaj@webrtc.org6ea3d1c2013-10-02 21:44:33 +000051APITest::APITest(const Config& config)
henrik.lundin@webrtc.orgadaf8092014-04-17 08:29:10 +000052 : _acmA(AudioCodingModule::Create(1)),
53 _acmB(AudioCodingModule::Create(2)),
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000054 _channel_A2B(NULL),
55 _channel_B2A(NULL),
56 _writeToFile(true),
57 _pullEventA(NULL),
58 _pushEventA(NULL),
59 _processEventA(NULL),
60 _apiEventA(NULL),
61 _pullEventB(NULL),
62 _pushEventB(NULL),
63 _processEventB(NULL),
64 _apiEventB(NULL),
65 _codecCntrA(0),
66 _codecCntrB(0),
67 _thereIsEncoderA(false),
68 _thereIsEncoderB(false),
69 _thereIsDecoderA(false),
70 _thereIsDecoderB(false),
71 _sendVADA(false),
72 _sendDTXA(false),
73 _sendVADModeA(VADNormal),
74 _sendVADB(false),
75 _sendDTXB(false),
76 _sendVADModeB(VADNormal),
77 _minDelayA(0),
78 _minDelayB(0),
79 _dotPositionA(0),
80 _dotMoveDirectionA(1),
81 _dotPositionB(39),
82 _dotMoveDirectionB(-1),
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000083 _vadCallbackA(NULL),
84 _vadCallbackB(NULL),
85 _apiTestRWLock(*RWLockWrapper::CreateRWLock()),
86 _randomTest(false),
87 _testNumA(0),
88 _testNumB(1) {
89 int n;
90 for (n = 0; n < 32; n++) {
91 _payloadUsed[n] = false;
92 }
niklase@google.com470e71d2011-07-07 08:21:25 +000093
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000094 _movingDot[40] = '\0';
niklase@google.com470e71d2011-07-07 08:21:25 +000095
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000096 for (int n = 0; n < 40; n++) {
97 _movingDot[n] = ' ';
98 }
niklase@google.com470e71d2011-07-07 08:21:25 +000099}
100
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000101APITest::~APITest() {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000102 DELETE_POINTER(_channel_A2B);
103 DELETE_POINTER(_channel_B2A);
niklase@google.com470e71d2011-07-07 08:21:25 +0000104
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000105 DELETE_POINTER(_pushEventA);
106 DELETE_POINTER(_pullEventA);
107 DELETE_POINTER(_processEventA);
108 DELETE_POINTER(_apiEventA);
niklase@google.com470e71d2011-07-07 08:21:25 +0000109
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000110 DELETE_POINTER(_pushEventB);
111 DELETE_POINTER(_pullEventB);
112 DELETE_POINTER(_processEventB);
113 DELETE_POINTER(_apiEventB);
niklase@google.com470e71d2011-07-07 08:21:25 +0000114
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000115 _inFileA.Close();
116 _outFileA.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +0000117
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000118 _inFileB.Close();
119 _outFileB.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +0000120
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000121 DELETE_POINTER(_vadCallbackA);
122 DELETE_POINTER(_vadCallbackB);
niklase@google.com470e71d2011-07-07 08:21:25 +0000123
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000124 delete &_apiTestRWLock;
niklase@google.com470e71d2011-07-07 08:21:25 +0000125}
126
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000127int16_t APITest::SetUp() {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000128 CodecInst dummyCodec;
129 int lastPayloadType = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000130
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000131 int16_t numCodecs = _acmA->NumberOfCodecs();
132 for (uint8_t n = 0; n < numCodecs; n++) {
133 AudioCodingModule::Codec(n, &dummyCodec);
134 if ((STR_CASE_CMP(dummyCodec.plname, "CN") == 0)
135 && (dummyCodec.plfreq == 32000)) {
136 continue;
niklase@google.com470e71d2011-07-07 08:21:25 +0000137 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000138
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000139 printf("Register Receive Codec %s ", dummyCodec.plname);
niklase@google.com470e71d2011-07-07 08:21:25 +0000140
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000141 if ((n != 0) && !FixedPayloadTypeCodec(dummyCodec.plname)) {
142 // Check registration with an already occupied payload type
143 int currentPayloadType = dummyCodec.pltype;
144 dummyCodec.pltype = 97; //lastPayloadType;
145 CHECK_ERROR(_acmB->RegisterReceiveCodec(dummyCodec));
146 dummyCodec.pltype = currentPayloadType;
147 }
andrew@webrtc.orgd7a71d02012-08-01 01:40:02 +0000148
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000149 if ((n < numCodecs - 1) && !FixedPayloadTypeCodec(dummyCodec.plname)) {
150 // test if re-registration works;
151 CodecInst nextCodec;
152 int currentPayloadType = dummyCodec.pltype;
153 AudioCodingModule::Codec(n + 1, &nextCodec);
154 dummyCodec.pltype = nextCodec.pltype;
155 if (!FixedPayloadTypeCodec(nextCodec.plname)) {
156 _acmB->RegisterReceiveCodec(dummyCodec);
157 }
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)) {
167 CHECK_ERROR_MT(_acmA->RegisterReceiveCodec(nextCodec));
168 CHECK_ERROR_MT(_acmA->UnregisterReceiveCodec(nextCodec.pltype));
169 }
170 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000171
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000172 CHECK_ERROR_MT(_acmA->RegisterReceiveCodec(dummyCodec));
173 printf(" side A done!");
174 CHECK_ERROR_MT(_acmB->RegisterReceiveCodec(dummyCodec));
175 printf(" side B done!\n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000176
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000177 if (!strcmp(dummyCodec.plname, "CN")) {
178 CHECK_ERROR_MT(_acmA->RegisterSendCodec(dummyCodec));
179 CHECK_ERROR_MT(_acmB->RegisterSendCodec(dummyCodec));
180 }
181 lastPayloadType = dummyCodec.pltype;
182 if ((lastPayloadType >= 96) && (lastPayloadType <= 127)) {
183 _payloadUsed[lastPayloadType - 96] = true;
184 }
185 }
186 _thereIsDecoderA = true;
187 _thereIsDecoderB = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000188
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000189 // Register Send Codec
190 AudioCodingModule::Codec((uint8_t) _codecCntrA, &dummyCodec);
191 CHECK_ERROR_MT(_acmA->RegisterSendCodec(dummyCodec));
192 _thereIsEncoderA = true;
193 //
194 AudioCodingModule::Codec((uint8_t) _codecCntrB, &dummyCodec);
195 CHECK_ERROR_MT(_acmB->RegisterSendCodec(dummyCodec));
196 _thereIsEncoderB = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000197
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000198 uint16_t frequencyHz;
niklase@google.com470e71d2011-07-07 08:21:25 +0000199
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000200 printf("\n\nAPI Test\n");
201 printf("========\n");
202 printf("Hit enter to accept the default values indicated in []\n\n");
andrew@webrtc.orgd7a71d02012-08-01 01:40:02 +0000203
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000204 //--- Input A
205 std::string file_name = webrtc::test::ResourcePath(
206 "audio_coding/testfile32kHz", "pcm");
207 frequencyHz = 32000;
208 printf("Enter input file at side A [%s]: ", file_name.c_str());
209 PCMFile::ChooseFile(&file_name, 499, &frequencyHz);
210 _inFileA.Open(file_name, frequencyHz, "rb", true);
niklase@google.com470e71d2011-07-07 08:21:25 +0000211
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000212 //--- Output A
213 std::string out_file_a = webrtc::test::OutputPath() + "outA.pcm";
214 printf("Enter output file at side A [%s]: ", out_file_a.c_str());
215 PCMFile::ChooseFile(&out_file_a, 499, &frequencyHz);
216 _outFileA.Open(out_file_a, frequencyHz, "wb");
niklase@google.com470e71d2011-07-07 08:21:25 +0000217
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000218 //--- Input B
219 file_name = webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
220 printf("\n\nEnter input file at side B [%s]: ", file_name.c_str());
221 PCMFile::ChooseFile(&file_name, 499, &frequencyHz);
222 _inFileB.Open(file_name, frequencyHz, "rb", true);
niklase@google.com470e71d2011-07-07 08:21:25 +0000223
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000224 //--- Output B
225 std::string out_file_b = webrtc::test::OutputPath() + "outB.pcm";
226 printf("Enter output file at side B [%s]: ", out_file_b.c_str());
227 PCMFile::ChooseFile(&out_file_b, 499, &frequencyHz);
228 _outFileB.Open(out_file_b, frequencyHz, "wb");
niklase@google.com470e71d2011-07-07 08:21:25 +0000229
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000230 //--- Set A-to-B channel
231 _channel_A2B = new Channel(2);
232 CHECK_ERROR_MT(_acmA->RegisterTransportCallback(_channel_A2B));
turaj@webrtc.org6ea3d1c2013-10-02 21:44:33 +0000233 _channel_A2B->RegisterReceiverACM(_acmB.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000234
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000235 //--- Set B-to-A channel
236 _channel_B2A = new Channel(1);
237 CHECK_ERROR_MT(_acmB->RegisterTransportCallback(_channel_B2A));
turaj@webrtc.org6ea3d1c2013-10-02 21:44:33 +0000238 _channel_B2A->RegisterReceiverACM(_acmA.get());
tina.legrand@webrtc.org5e7ca602012-06-12 07:16:24 +0000239
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000240 //--- EVENT TIMERS
241 // A
Peter Boström64c03662015-04-08 11:24:19 +0200242 _pullEventA = EventTimerWrapper::Create();
243 _pushEventA = EventTimerWrapper::Create();
244 _processEventA = EventTimerWrapper::Create();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000245 _apiEventA = EventWrapper::Create();
246 // B
Peter Boström64c03662015-04-08 11:24:19 +0200247 _pullEventB = EventTimerWrapper::Create();
248 _pushEventB = EventTimerWrapper::Create();
249 _processEventB = EventTimerWrapper::Create();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000250 _apiEventB = EventWrapper::Create();
251
252 //--- I/O params
253 // A
254 _outFreqHzA = _outFileA.SamplingFrequency();
255 // B
256 _outFreqHzB = _outFileB.SamplingFrequency();
257
258 //Trace::SetEncryptedTraceFile("ACMAPITestEncrypted.txt");
259
260 char print[11];
261
262 // Create a trace file.
263 Trace::CreateTrace();
264 Trace::SetTraceFile(
265 (webrtc::test::OutputPath() + "acm_api_trace.txt").c_str());
266
267 printf("\nRandom Test (y/n)?");
268 EXPECT_TRUE(fgets(print, 10, stdin) != NULL);
269 print[10] = '\0';
270 if (strstr(print, "y") != NULL) {
271 _randomTest = true;
272 _verbose = false;
273 _writeToFile = false;
274 } else {
275 _randomTest = false;
276 printf("\nPrint Tests (y/n)? ");
kjellander@webrtc.org543c3ea2011-11-23 12:20:35 +0000277 EXPECT_TRUE(fgets(print, 10, stdin) != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000278 print[10] = '\0';
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000279 if (strstr(print, "y") == NULL) {
280 EXPECT_TRUE(freopen("APITest_log.txt", "w", stdout) != 0);
281 _verbose = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000282 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000283 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000284
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000285 _vadCallbackA = new VADCallback;
286 _vadCallbackB = new VADCallback;
andrew@webrtc.orgd7a71d02012-08-01 01:40:02 +0000287
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000288 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000289}
290
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000291bool APITest::PushAudioThreadA(void* obj) {
292 return static_cast<APITest*>(obj)->PushAudioRunA();
niklase@google.com470e71d2011-07-07 08:21:25 +0000293}
294
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000295bool APITest::PushAudioThreadB(void* obj) {
296 return static_cast<APITest*>(obj)->PushAudioRunB();
niklase@google.com470e71d2011-07-07 08:21:25 +0000297}
298
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000299bool APITest::PullAudioThreadA(void* obj) {
300 return static_cast<APITest*>(obj)->PullAudioRunA();
niklase@google.com470e71d2011-07-07 08:21:25 +0000301}
302
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000303bool APITest::PullAudioThreadB(void* obj) {
304 return static_cast<APITest*>(obj)->PullAudioRunB();
niklase@google.com470e71d2011-07-07 08:21:25 +0000305}
306
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000307bool APITest::ProcessThreadA(void* obj) {
308 return static_cast<APITest*>(obj)->ProcessRunA();
niklase@google.com470e71d2011-07-07 08:21:25 +0000309}
310
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000311bool APITest::ProcessThreadB(void* obj) {
312 return static_cast<APITest*>(obj)->ProcessRunB();
niklase@google.com470e71d2011-07-07 08:21:25 +0000313}
314
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000315bool APITest::APIThreadA(void* obj) {
316 return static_cast<APITest*>(obj)->APIRunA();
niklase@google.com470e71d2011-07-07 08:21:25 +0000317}
318
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000319bool APITest::APIThreadB(void* obj) {
320 return static_cast<APITest*>(obj)->APIRunB();
niklase@google.com470e71d2011-07-07 08:21:25 +0000321}
322
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000323bool APITest::PullAudioRunA() {
324 _pullEventA->Wait(100);
325 AudioFrame audioFrame;
326 if (_acmA->PlayoutData10Ms(_outFreqHzA, &audioFrame) < 0) {
327 bool thereIsDecoder;
niklase@google.com470e71d2011-07-07 08:21:25 +0000328 {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000329 ReadLockScoped rl(_apiTestRWLock);
330 thereIsDecoder = _thereIsDecoderA;
niklase@google.com470e71d2011-07-07 08:21:25 +0000331 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000332 if (thereIsDecoder) {
333 fprintf(stderr, "\n>>>>>> cannot pull audio A <<<<<<<< \n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000334 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000335 } else {
336 if (_writeToFile) {
337 _outFileA.Write10MsData(audioFrame);
338 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000339 }
340 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000341}
342
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000343bool APITest::PullAudioRunB() {
344 _pullEventB->Wait(100);
345 AudioFrame audioFrame;
346 if (_acmB->PlayoutData10Ms(_outFreqHzB, &audioFrame) < 0) {
347 bool thereIsDecoder;
niklase@google.com470e71d2011-07-07 08:21:25 +0000348 {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000349 ReadLockScoped rl(_apiTestRWLock);
350 thereIsDecoder = _thereIsDecoderB;
niklase@google.com470e71d2011-07-07 08:21:25 +0000351 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000352 if (thereIsDecoder) {
353 fprintf(stderr, "\n>>>>>> cannot pull audio B <<<<<<<< \n");
354 fprintf(stderr, "%d %d\n", _testNumA, _testNumB);
andrew@webrtc.orgd7a71d02012-08-01 01:40:02 +0000355 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000356 } else {
357 if (_writeToFile) {
358 _outFileB.Write10MsData(audioFrame);
359 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000360 }
361 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000362}
363
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000364bool APITest::PushAudioRunA() {
365 _pushEventA->Wait(100);
366 AudioFrame audioFrame;
367 _inFileA.Read10MsData(audioFrame);
368 if (_acmA->Add10MsData(audioFrame) < 0) {
369 bool thereIsEncoder;
niklase@google.com470e71d2011-07-07 08:21:25 +0000370 {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000371 ReadLockScoped rl(_apiTestRWLock);
372 thereIsEncoder = _thereIsEncoderA;
niklase@google.com470e71d2011-07-07 08:21:25 +0000373 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000374 if (thereIsEncoder) {
375 fprintf(stderr, "\n>>>> add10MsData at A failed <<<<\n");
376 }
377 }
378 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000379}
380
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000381bool APITest::PushAudioRunB() {
382 _pushEventB->Wait(100);
383 AudioFrame audioFrame;
384 _inFileB.Read10MsData(audioFrame);
385 if (_acmB->Add10MsData(audioFrame) < 0) {
386 bool thereIsEncoder;
niklase@google.com470e71d2011-07-07 08:21:25 +0000387 {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000388 ReadLockScoped rl(_apiTestRWLock);
389 thereIsEncoder = _thereIsEncoderB;
niklase@google.com470e71d2011-07-07 08:21:25 +0000390 }
391
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000392 if (thereIsEncoder) {
393 fprintf(stderr, "\n>>>> cannot add audio to B <<<<");
394 }
395 }
396
397 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000398}
399
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000400bool APITest::ProcessRunA() {
401 _processEventA->Wait(100);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000402 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000403}
404
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000405bool APITest::ProcessRunB() {
406 _processEventB->Wait(100);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000407 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000408}
409
410/*/
411 *
412 * In side A we test the APIs which are related to sender Side.
413 *
414/*/
415
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000416void APITest::RunTest(char thread) {
417 int testNum;
418 {
419 WriteLockScoped cs(_apiTestRWLock);
420 if (thread == 'A') {
henrik.lundin1bd0e032015-09-28 06:12:17 -0700421 _testNumA = (_testNumB + 1 + (rand() % 3)) % 4;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000422 testNum = _testNumA;
niklase@google.com470e71d2011-07-07 08:21:25 +0000423
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000424 _movingDot[_dotPositionA] = ' ';
425 if (_dotPositionA == 0) {
426 _dotMoveDirectionA = 1;
427 }
428 if (_dotPositionA == 19) {
429 _dotMoveDirectionA = -1;
430 }
431 _dotPositionA += _dotMoveDirectionA;
432 _movingDot[_dotPositionA] = (_dotMoveDirectionA > 0) ? '>' : '<';
433 } else {
henrik.lundin1bd0e032015-09-28 06:12:17 -0700434 _testNumB = (_testNumA + 1 + (rand() % 3)) % 4;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000435 testNum = _testNumB;
andrew@webrtc.orgd7a71d02012-08-01 01:40:02 +0000436
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000437 _movingDot[_dotPositionB] = ' ';
438 if (_dotPositionB == 20) {
439 _dotMoveDirectionB = 1;
440 }
441 if (_dotPositionB == 39) {
442 _dotMoveDirectionB = -1;
443 }
444 _dotPositionB += _dotMoveDirectionB;
445 _movingDot[_dotPositionB] = (_dotMoveDirectionB > 0) ? '>' : '<';
niklase@google.com470e71d2011-07-07 08:21:25 +0000446 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000447 //fprintf(stderr, "%c: %d \n", thread, testNum);
448 //fflush(stderr);
449 }
450 switch (testNum) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000451 case 0:
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000452 CurrentCodec('A');
453 ChangeCodec('A');
454 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000455 case 1:
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000456 if (!_randomTest) {
457 fprintf(stdout, "\nTesting Delay ...\n");
458 }
459 TestDelay('A');
460 break;
henrik.lundin1bd0e032015-09-28 06:12:17 -0700461 case 2:
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000462 TestSendVAD('A');
463 break;
henrik.lundin1bd0e032015-09-28 06:12:17 -0700464 case 3:
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000465 TestRegisteration('A');
466 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000467 default:
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000468 fprintf(stderr, "Wrong Test Number\n");
marpan@webrtc.org4765ca52014-11-03 20:10:26 +0000469 getc(stdin);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000470 exit(1);
471 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000472}
473
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000474bool APITest::APIRunA() {
475 _apiEventA->Wait(50);
niklase@google.com470e71d2011-07-07 08:21:25 +0000476
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000477 bool randomTest;
478 {
479 ReadLockScoped rl(_apiTestRWLock);
480 randomTest = _randomTest;
481 }
482 if (randomTest) {
483 RunTest('A');
484 } else {
485 CurrentCodec('A');
486 ChangeCodec('A');
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000487 if (_codecCntrA == 0) {
488 fprintf(stdout, "\nTesting Delay ...\n");
489 TestDelay('A');
niklase@google.com470e71d2011-07-07 08:21:25 +0000490 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000491 // VAD TEST
492 TestSendVAD('A');
493 TestRegisteration('A');
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000494 }
495 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000496}
497
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000498bool APITest::APIRunB() {
499 _apiEventB->Wait(50);
500 bool randomTest;
501 {
502 ReadLockScoped rl(_apiTestRWLock);
503 randomTest = _randomTest;
504 }
505 //_apiEventB->Wait(2000);
506 if (randomTest) {
507 RunTest('B');
508 }
andrew@webrtc.orgd7a71d02012-08-01 01:40:02 +0000509
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000510 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000511}
512
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000513void APITest::Perform() {
514 SetUp();
niklase@google.com470e71d2011-07-07 08:21:25 +0000515
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000516 //--- THREADS
517 // A
518 // PUSH
Peter Boström8c38e8b2015-11-26 17:45:47 +0100519 rtc::PlatformThread myPushAudioThreadA(PushAudioThreadA, this,
520 "PushAudioThreadA");
521 myPushAudioThreadA.Start();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000522 // PULL
Peter Boström8c38e8b2015-11-26 17:45:47 +0100523 rtc::PlatformThread myPullAudioThreadA(PullAudioThreadA, this,
524 "PullAudioThreadA");
525 myPullAudioThreadA.Start();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000526 // Process
Peter Boström8c38e8b2015-11-26 17:45:47 +0100527 rtc::PlatformThread myProcessThreadA(ProcessThreadA, this, "ProcessThreadA");
528 myProcessThreadA.Start();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000529 // API
Peter Boström8c38e8b2015-11-26 17:45:47 +0100530 rtc::PlatformThread myAPIThreadA(APIThreadA, this, "APIThreadA");
531 myAPIThreadA.Start();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000532 // B
533 // PUSH
Peter Boström8c38e8b2015-11-26 17:45:47 +0100534 rtc::PlatformThread myPushAudioThreadB(PushAudioThreadB, this,
535 "PushAudioThreadB");
536 myPushAudioThreadB.Start();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000537 // PULL
Peter Boström8c38e8b2015-11-26 17:45:47 +0100538 rtc::PlatformThread myPullAudioThreadB(PullAudioThreadB, this,
539 "PullAudioThreadB");
540 myPullAudioThreadB.Start();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000541 // Process
Peter Boström8c38e8b2015-11-26 17:45:47 +0100542 rtc::PlatformThread myProcessThreadB(ProcessThreadB, this, "ProcessThreadB");
543 myProcessThreadB.Start();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000544 // API
Peter Boström8c38e8b2015-11-26 17:45:47 +0100545 rtc::PlatformThread myAPIThreadB(APIThreadB, this, "APIThreadB");
546 myAPIThreadB.Start();
andrew@webrtc.orgd7a71d02012-08-01 01:40:02 +0000547
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000548 //_apiEventA->StartTimer(true, 5000);
549 //_apiEventB->StartTimer(true, 5000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000550
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000551 _processEventA->StartTimer(true, 10);
552 _processEventB->StartTimer(true, 10);
niklase@google.com470e71d2011-07-07 08:21:25 +0000553
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000554 _pullEventA->StartTimer(true, 10);
555 _pullEventB->StartTimer(true, 10);
andrew@webrtc.orgd7a71d02012-08-01 01:40:02 +0000556
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000557 _pushEventA->StartTimer(true, 10);
558 _pushEventB->StartTimer(true, 10);
niklase@google.com470e71d2011-07-07 08:21:25 +0000559
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000560 // Keep main thread waiting for sender/receiver
561 // threads to complete
562 EventWrapper* completeEvent = EventWrapper::Create();
Niels Möllerd28db7f2016-05-10 16:31:47 +0200563 uint64_t startTime = rtc::TimeMillis();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000564 uint64_t currentTime;
565 // Run test in 2 minutes (120000 ms).
566 do {
niklase@google.com470e71d2011-07-07 08:21:25 +0000567 {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000568 //ReadLockScoped rl(_apiTestRWLock);
569 //fprintf(stderr, "\r%s", _movingDot);
570 }
571 //fflush(stderr);
572 completeEvent->Wait(50);
Niels Möllerd28db7f2016-05-10 16:31:47 +0200573 currentTime = rtc::TimeMillis();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000574 } while ((currentTime - startTime) < 120000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000575
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000576 //completeEvent->Wait(0xFFFFFFFF);
577 //(unsigned long)((unsigned long)TEST_DURATION_SEC * (unsigned long)1000));
578 delete completeEvent;
andrew@webrtc.orgd7a71d02012-08-01 01:40:02 +0000579
Peter Boström8c38e8b2015-11-26 17:45:47 +0100580 myPushAudioThreadA.Stop();
581 myPullAudioThreadA.Stop();
582 myProcessThreadA.Stop();
583 myAPIThreadA.Stop();
niklase@google.com470e71d2011-07-07 08:21:25 +0000584
Peter Boström8c38e8b2015-11-26 17:45:47 +0100585 myPushAudioThreadB.Stop();
586 myPullAudioThreadB.Stop();
587 myProcessThreadB.Stop();
588 myAPIThreadB.Stop();
niklase@google.com470e71d2011-07-07 08:21:25 +0000589}
590
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000591void APITest::CheckVADStatus(char side) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000592
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000593 bool dtxEnabled;
594 bool vadEnabled;
595 ACMVADMode vadMode;
niklase@google.com470e71d2011-07-07 08:21:25 +0000596
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000597 if (side == 'A') {
598 _acmA->VAD(&dtxEnabled, &vadEnabled, &vadMode);
599 _acmA->RegisterVADCallback(NULL);
600 _vadCallbackA->Reset();
601 _acmA->RegisterVADCallback(_vadCallbackA);
tina.legrand@webrtc.org2e096922011-08-18 06:20:30 +0000602
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000603 if (!_randomTest) {
604 if (_verbose) {
605 fprintf(stdout, "DTX %3s, VAD %3s, Mode %d", dtxEnabled ? "ON" : "OFF",
606 vadEnabled ? "ON" : "OFF", (int) vadMode);
607 Wait(5000);
608 fprintf(stdout, " => bit-rate %3.0f kbps\n", _channel_A2B->BitRate());
609 } else {
610 Wait(5000);
611 fprintf(stdout, "DTX %3s, VAD %3s, Mode %d => bit-rate %3.0f kbps\n",
612 dtxEnabled ? "ON" : "OFF", vadEnabled ? "ON" : "OFF",
613 (int) vadMode, _channel_A2B->BitRate());
614 }
615 _vadCallbackA->PrintFrameTypes();
niklase@google.com470e71d2011-07-07 08:21:25 +0000616 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000617
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000618 if (dtxEnabled != _sendDTXA) {
619 fprintf(stderr, ">>> Error Enabling DTX <<<\n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000620 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000621 if ((vadEnabled != _sendVADA) && (!dtxEnabled)) {
622 fprintf(stderr, ">>> Error Enabling VAD <<<\n");
623 }
624 if ((vadMode != _sendVADModeA) && vadEnabled) {
625 fprintf(stderr, ">>> Error setting VAD-mode <<<\n");
626 }
627 } else {
628 _acmB->VAD(&dtxEnabled, &vadEnabled, &vadMode);
629
630 _acmB->RegisterVADCallback(NULL);
631 _vadCallbackB->Reset();
632 _acmB->RegisterVADCallback(_vadCallbackB);
633
634 if (!_randomTest) {
635 if (_verbose) {
636 fprintf(stdout, "DTX %3s, VAD %3s, Mode %d", dtxEnabled ? "ON" : "OFF",
637 vadEnabled ? "ON" : "OFF", (int) vadMode);
638 Wait(5000);
639 fprintf(stdout, " => bit-rate %3.0f kbps\n", _channel_B2A->BitRate());
640 } else {
641 Wait(5000);
642 fprintf(stdout, "DTX %3s, VAD %3s, Mode %d => bit-rate %3.0f kbps\n",
643 dtxEnabled ? "ON" : "OFF", vadEnabled ? "ON" : "OFF",
644 (int) vadMode, _channel_B2A->BitRate());
645 }
646 _vadCallbackB->PrintFrameTypes();
647 }
648
649 if (dtxEnabled != _sendDTXB) {
650 fprintf(stderr, ">>> Error Enabling DTX <<<\n");
651 }
652 if ((vadEnabled != _sendVADB) && (!dtxEnabled)) {
653 fprintf(stderr, ">>> Error Enabling VAD <<<\n");
654 }
655 if ((vadMode != _sendVADModeB) && vadEnabled) {
656 fprintf(stderr, ">>> Error setting VAD-mode <<<\n");
657 }
658 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000659}
660
661// Set Min delay, get delay, playout timestamp
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000662void APITest::TestDelay(char side) {
663 AudioCodingModule* myACM;
664 Channel* myChannel;
665 int32_t* myMinDelay;
Peter Boström64c03662015-04-08 11:24:19 +0200666 EventTimerWrapper* myEvent = EventTimerWrapper::Create();
niklase@google.com470e71d2011-07-07 08:21:25 +0000667
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000668 uint32_t inTimestamp = 0;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000669 double estimDelay = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000670
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000671 double averageEstimDelay = 0;
672 double averageDelay = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000673
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000674 CircularBuffer estimDelayCB(100);
675 estimDelayCB.SetArithMean(true);
niklase@google.com470e71d2011-07-07 08:21:25 +0000676
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000677 if (side == 'A') {
turaj@webrtc.org6ea3d1c2013-10-02 21:44:33 +0000678 myACM = _acmA.get();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000679 myChannel = _channel_B2A;
680 myMinDelay = &_minDelayA;
681 } else {
turaj@webrtc.org6ea3d1c2013-10-02 21:44:33 +0000682 myACM = _acmB.get();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000683 myChannel = _channel_A2B;
684 myMinDelay = &_minDelayB;
685 }
686
687 CHECK_ERROR_MT(myACM->SetMinimumPlayoutDelay(*myMinDelay));
688
689 inTimestamp = myChannel->LastInTimestamp();
henrik.lundin9a410dd2016-04-06 01:39:22 -0700690 rtc::Optional<uint32_t> outTimestamp = myACM->PlayoutTimestamp();
691 CHECK_ERROR_MT(outTimestamp ? 0 : -1);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000692
693 if (!_randomTest) {
694 myEvent->StartTimer(true, 30);
695 int n = 0;
696 int settlePoint = 5000;
697 while (n < settlePoint + 400) {
698 myEvent->Wait(1000);
699
700 inTimestamp = myChannel->LastInTimestamp();
henrik.lundin9a410dd2016-04-06 01:39:22 -0700701 outTimestamp = myACM->PlayoutTimestamp();
702 CHECK_ERROR_MT(outTimestamp ? 0 : -1);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000703
704 //std::cout << outTimestamp << std::endl << std::flush;
henrik.lundin9a410dd2016-04-06 01:39:22 -0700705 estimDelay = (double)((uint32_t)(inTimestamp - *outTimestamp)) /
706 ((double)myACM->ReceiveFrequency() / 1000.0);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000707
708 estimDelayCB.Update(estimDelay);
709
710 estimDelayCB.ArithMean(averageEstimDelay);
711 //printf("\n %6.1f \n", estimDelay);
712 //std::cout << " " << std::flush;
713
714 if (_verbose) {
715 fprintf(stdout,
716 "\rExpected: %4d, retreived: %6.1f, measured: %6.1f",
717 *myMinDelay, averageDelay, averageEstimDelay);
718 std::cout << " " << std::flush;
719 }
720 if ((averageDelay > *myMinDelay) && (n < settlePoint)) {
721 settlePoint = n;
722 }
723 n++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000724 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000725 myEvent->StopTimer();
726 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000727
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000728 if ((!_verbose) && (!_randomTest)) {
729 fprintf(stdout, "\nExpected: %4d, retreived: %6.1f, measured: %6.1f",
niklase@google.com470e71d2011-07-07 08:21:25 +0000730 *myMinDelay, averageDelay, averageEstimDelay);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000731 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000732
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000733 *myMinDelay = (rand() % 1000) + 1;
andrew@webrtc.orgd7a71d02012-08-01 01:40:02 +0000734
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000735 NetworkStatistics networkStat;
736 CHECK_ERROR_MT(myACM->GetNetworkStatistics(&networkStat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000737
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000738 if (!_randomTest) {
739 fprintf(stdout, "\n\nJitter Statistics at Side %c\n", side);
740 fprintf(stdout, "--------------------------------------\n");
741 fprintf(stdout, "buffer-size............. %d\n",
742 networkStat.currentBufferSize);
743 fprintf(stdout, "Preferred buffer-size... %d\n",
744 networkStat.preferredBufferSize);
745 fprintf(stdout, "Peaky jitter mode........%d\n",
746 networkStat.jitterPeaksFound);
747 fprintf(stdout, "packet-size rate........ %d\n",
748 networkStat.currentPacketLossRate);
749 fprintf(stdout, "discard rate............ %d\n",
750 networkStat.currentDiscardRate);
751 fprintf(stdout, "expand rate............. %d\n",
752 networkStat.currentExpandRate);
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000753 fprintf(stdout, "speech expand rate...... %d\n",
754 networkStat.currentSpeechExpandRate);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000755 fprintf(stdout, "Preemptive rate......... %d\n",
756 networkStat.currentPreemptiveRate);
757 fprintf(stdout, "Accelerate rate......... %d\n",
758 networkStat.currentAccelerateRate);
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000759 fprintf(stdout, "Secondary decoded rate.. %d\n",
760 networkStat.currentSecondaryDecodedRate);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000761 fprintf(stdout, "Clock-drift............. %d\n", networkStat.clockDriftPPM);
762 fprintf(stdout, "Mean waiting time....... %d\n",
763 networkStat.meanWaitingTimeMs);
764 fprintf(stdout, "Median waiting time..... %d\n",
765 networkStat.medianWaitingTimeMs);
766 fprintf(stdout, "Min waiting time........ %d\n",
767 networkStat.minWaitingTimeMs);
768 fprintf(stdout, "Max waiting time........ %d\n",
769 networkStat.maxWaitingTimeMs);
770 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000771
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000772 CHECK_ERROR_MT(myACM->SetMinimumPlayoutDelay(*myMinDelay));
niklase@google.com470e71d2011-07-07 08:21:25 +0000773
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000774 if (!_randomTest) {
775 myEvent->Wait(500);
776 fprintf(stdout, "\n");
777 fprintf(stdout, "\n");
778 }
779 delete myEvent;
niklase@google.com470e71d2011-07-07 08:21:25 +0000780}
781
782// Unregister a codec & register again.
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000783void APITest::TestRegisteration(char sendSide) {
784 AudioCodingModule* sendACM;
785 AudioCodingModule* receiveACM;
786 bool* thereIsDecoder;
787 EventWrapper* myEvent = EventWrapper::Create();
niklase@google.com470e71d2011-07-07 08:21:25 +0000788
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000789 if (!_randomTest) {
790 fprintf(stdout, "\n\n");
791 fprintf(stdout,
792 "---------------------------------------------------------\n");
793 fprintf(stdout, " Unregister/register Receive Codec\n");
794 fprintf(stdout,
795 "---------------------------------------------------------\n");
796 }
797
798 switch (sendSide) {
799 case 'A': {
turaj@webrtc.org6ea3d1c2013-10-02 21:44:33 +0000800 sendACM = _acmA.get();
801 receiveACM = _acmB.get();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000802 thereIsDecoder = &_thereIsDecoderB;
803 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000804 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000805 case 'B': {
turaj@webrtc.org6ea3d1c2013-10-02 21:44:33 +0000806 sendACM = _acmB.get();
807 receiveACM = _acmA.get();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000808 thereIsDecoder = &_thereIsDecoderA;
809 break;
810 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000811 default:
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000812 fprintf(stderr, "Invalid sender-side in TestRegistration(%c)\n",
813 sendSide);
814 exit(-1);
815 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000816
kwiberg1fd4a4a2015-11-03 11:20:50 -0800817 auto myCodec = sendACM->SendCodec();
818 if (!myCodec) {
819 CodecInst ci;
820 AudioCodingModule::Codec(_codecCntrA, &ci);
Karl Wibergbe579832015-11-10 22:34:18 +0100821 myCodec = rtc::Optional<CodecInst>(ci);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000822 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000823
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000824 if (!_randomTest) {
825 fprintf(stdout, "Unregistering reveive codec, NO AUDIO.\n");
826 fflush (stdout);
827 }
828 {
829 WriteLockScoped wl(_apiTestRWLock);
830 *thereIsDecoder = false;
831 }
832 //myEvent->Wait(20);
kwiberg1fd4a4a2015-11-03 11:20:50 -0800833 CHECK_ERROR_MT(receiveACM->UnregisterReceiveCodec(myCodec->pltype));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000834 Wait(1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000835
kwiberg1fd4a4a2015-11-03 11:20:50 -0800836 int currentPayload = myCodec->pltype;
niklase@google.com470e71d2011-07-07 08:21:25 +0000837
kwiberg1fd4a4a2015-11-03 11:20:50 -0800838 if (!FixedPayloadTypeCodec(myCodec->plname)) {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000839 int32_t i;
840 for (i = 0; i < 32; i++) {
841 if (!_payloadUsed[i]) {
842 if (!_randomTest) {
843 fprintf(stdout,
844 "Register receive codec with new Payload, AUDIO BACK.\n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000845 }
kwiberg1fd4a4a2015-11-03 11:20:50 -0800846 //myCodec->pltype = i + 96;
847 //CHECK_ERROR_MT(receiveACM->RegisterReceiveCodec(*myCodec));
848 //CHECK_ERROR_MT(sendACM->RegisterSendCodec(*myCodec));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000849 //myEvent->Wait(20);
850 //{
851 // WriteLockScoped wl(_apiTestRWLock);
852 // *thereIsDecoder = true;
853 //}
854 Wait(1000);
855
856 if (!_randomTest) {
857 fprintf(stdout, "Unregistering reveive codec, NO AUDIO.\n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000858 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000859 //{
860 // WriteLockScoped wl(_apiTestRWLock);
861 // *thereIsDecoder = false;
862 //}
863 //myEvent->Wait(20);
kwiberg1fd4a4a2015-11-03 11:20:50 -0800864 //CHECK_ERROR_MT(receiveACM->UnregisterReceiveCodec(myCodec->pltype));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000865 Wait(1000);
866
kwiberg1fd4a4a2015-11-03 11:20:50 -0800867 myCodec->pltype = currentPayload;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000868 if (!_randomTest) {
869 fprintf(stdout,
870 "Register receive codec with default Payload, AUDIO BACK.\n");
871 fflush (stdout);
niklase@google.com470e71d2011-07-07 08:21:25 +0000872 }
kwiberg1fd4a4a2015-11-03 11:20:50 -0800873 CHECK_ERROR_MT(receiveACM->RegisterReceiveCodec(*myCodec));
874 //CHECK_ERROR_MT(sendACM->RegisterSendCodec(*myCodec));
niklase@google.com470e71d2011-07-07 08:21:25 +0000875 myEvent->Wait(20);
876 {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000877 WriteLockScoped wl(_apiTestRWLock);
878 *thereIsDecoder = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000879 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000880 Wait(1000);
881
882 break;
883 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000884 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000885 if (i == 32) {
kwiberg1fd4a4a2015-11-03 11:20:50 -0800886 CHECK_ERROR_MT(receiveACM->RegisterReceiveCodec(*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 }
kwiberg1fd4a4a2015-11-03 11:20:50 -0800898 CHECK_ERROR_MT(receiveACM->RegisterReceiveCodec(*myCodec));
899 //CHECK_ERROR_MT(receiveACM->UnregisterReceiveCodec(myCodec->pltype));
900 //CHECK_ERROR_MT(receiveACM->RegisterReceiveCodec(*myCodec));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000901 myEvent->Wait(20);
niklase@google.com470e71d2011-07-07 08:21:25 +0000902 {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000903 WriteLockScoped wl(_apiTestRWLock);
904 *thereIsDecoder = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000905 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000906 }
907 delete myEvent;
908 if (!_randomTest) {
909 fprintf(stdout,
910 "---------------------------------------------------------\n");
911 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000912}
913
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000914void APITest::TestSendVAD(char side) {
915 if (_randomTest) {
916 return;
917 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000918
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000919 bool* vad;
920 bool* dtx;
921 ACMVADMode* mode;
922 Channel* myChannel;
923 AudioCodingModule* myACM;
niklase@google.com470e71d2011-07-07 08:21:25 +0000924
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000925 CodecInst myCodec;
926 if (!_randomTest) {
927 fprintf(stdout, "\n\n");
928 fprintf(stdout, "-----------------------------------------------\n");
929 fprintf(stdout, " Test VAD API\n");
930 fprintf(stdout, "-----------------------------------------------\n");
931 }
andrew@webrtc.orgd7a71d02012-08-01 01:40:02 +0000932
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000933 if (side == 'A') {
934 AudioCodingModule::Codec(_codecCntrA, &myCodec);
935 vad = &_sendVADA;
936 dtx = &_sendDTXA;
937 mode = &_sendVADModeA;
938 myChannel = _channel_A2B;
turaj@webrtc.org6ea3d1c2013-10-02 21:44:33 +0000939 myACM = _acmA.get();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000940 } else {
941 AudioCodingModule::Codec(_codecCntrB, &myCodec);
942 vad = &_sendVADB;
943 dtx = &_sendDTXB;
944 mode = &_sendVADModeB;
945 myChannel = _channel_B2A;
turaj@webrtc.org6ea3d1c2013-10-02 21:44:33 +0000946 myACM = _acmB.get();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000947 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000948
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000949 CheckVADStatus(side);
950 if (!_randomTest) {
951 fprintf(stdout, "\n\n");
952 }
953
954 switch (*mode) {
955 case VADNormal:
956 *vad = true;
957 *dtx = true;
958 *mode = VADAggr;
959 break;
960 case VADLowBitrate:
961 *vad = true;
962 *dtx = true;
963 *mode = VADVeryAggr;
964 break;
965 case VADAggr:
966 *vad = true;
967 *dtx = true;
968 *mode = VADLowBitrate;
969 break;
970 case VADVeryAggr:
971 *vad = false;
972 *dtx = false;
973 *mode = VADNormal;
974 break;
975 default:
976 *mode = VADNormal;
977 }
978
979 *dtx = (myCodec.plfreq == 32000) ? false : *dtx;
980
981 CHECK_ERROR_MT(myACM->SetVAD(*dtx, *vad, *mode));
982 myChannel->ResetStats();
983
984 CheckVADStatus(side);
985 if (!_randomTest) {
986 fprintf(stdout, "\n");
987 fprintf(stdout, "-----------------------------------------------\n");
988 }
989
990 // Fault Test
991 CHECK_PROTECTED_MT(myACM->SetVAD(false, true, (ACMVADMode) - 1));
992 CHECK_PROTECTED_MT(myACM->SetVAD(false, true, (ACMVADMode) 4));
niklase@google.com470e71d2011-07-07 08:21:25 +0000993
994}
995
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000996void APITest::CurrentCodec(char side) {
kwiberg1fd4a4a2015-11-03 11:20:50 -0800997 auto myCodec = (side == 'A' ? _acmA : _acmB)->SendCodec();
niklase@google.com470e71d2011-07-07 08:21:25 +0000998
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000999 if (!_randomTest) {
1000 fprintf(stdout, "\n\n");
1001 fprintf(stdout, "Send codec in Side A\n");
1002 fprintf(stdout, "----------------------------\n");
kwiberg1fd4a4a2015-11-03 11:20:50 -08001003 fprintf(stdout, "Name................. %s\n", myCodec->plname);
1004 fprintf(stdout, "Sampling Frequency... %d\n", myCodec->plfreq);
1005 fprintf(stdout, "Rate................. %d\n", myCodec->rate);
1006 fprintf(stdout, "Payload-type......... %d\n", myCodec->pltype);
1007 fprintf(stdout, "Packet-size.......... %d\n", myCodec->pacsize);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001008 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001009
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001010 Wait(100);
niklase@google.com470e71d2011-07-07 08:21:25 +00001011}
1012
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001013void APITest::ChangeCodec(char side) {
1014 CodecInst myCodec;
1015 AudioCodingModule* myACM;
1016 uint8_t* codecCntr;
1017 bool* thereIsEncoder;
1018 bool* vad;
1019 bool* dtx;
1020 ACMVADMode* mode;
1021 Channel* myChannel;
1022 // Reset and Wait
1023 if (!_randomTest) {
1024 fprintf(stdout, "Reset Encoder Side A \n");
1025 }
1026 if (side == 'A') {
turaj@webrtc.org6ea3d1c2013-10-02 21:44:33 +00001027 myACM = _acmA.get();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001028 codecCntr = &_codecCntrA;
niklase@google.com470e71d2011-07-07 08:21:25 +00001029 {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001030 WriteLockScoped wl(_apiTestRWLock);
1031 thereIsEncoder = &_thereIsEncoderA;
niklase@google.com470e71d2011-07-07 08:21:25 +00001032 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001033 vad = &_sendVADA;
1034 dtx = &_sendDTXA;
1035 mode = &_sendVADModeA;
1036 myChannel = _channel_A2B;
1037 } else {
turaj@webrtc.org6ea3d1c2013-10-02 21:44:33 +00001038 myACM = _acmB.get();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001039 codecCntr = &_codecCntrB;
niklase@google.com470e71d2011-07-07 08:21:25 +00001040 {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001041 WriteLockScoped wl(_apiTestRWLock);
1042 thereIsEncoder = &_thereIsEncoderB;
niklase@google.com470e71d2011-07-07 08:21:25 +00001043 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001044 vad = &_sendVADB;
1045 dtx = &_sendDTXB;
1046 mode = &_sendVADModeB;
1047 myChannel = _channel_B2A;
1048 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001049
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001050 Wait(100);
niklase@google.com470e71d2011-07-07 08:21:25 +00001051
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001052 // Register the next codec
1053 do {
1054 *codecCntr =
1055 (*codecCntr < AudioCodingModule::NumberOfCodecs() - 1) ?
1056 (*codecCntr + 1) : 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001057
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001058 if (*codecCntr == 0) {
1059 //printf("Initialize Sender Side A \n");
1060 {
niklase@google.com470e71d2011-07-07 08:21:25 +00001061 WriteLockScoped wl(_apiTestRWLock);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001062 *thereIsEncoder = false;
1063 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001064 // After Initialization CN is lost, re-register them
1065 if (AudioCodingModule::Codec("CN", &myCodec, 8000, 1) >= 0) {
1066 CHECK_ERROR_MT(myACM->RegisterSendCodec(myCodec));
1067 }
1068 if (AudioCodingModule::Codec("CN", &myCodec, 16000, 1) >= 0) {
1069 CHECK_ERROR_MT(myACM->RegisterSendCodec(myCodec));
1070 }
1071 // VAD & DTX are disabled after initialization
1072 *vad = false;
1073 *dtx = false;
1074 _writeToFile = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001075 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001076
1077 AudioCodingModule::Codec(*codecCntr, &myCodec);
1078 } while (!STR_CASE_CMP(myCodec.plname, "CN")
1079 || !STR_CASE_CMP(myCodec.plname, "telephone-event")
1080 || !STR_CASE_CMP(myCodec.plname, "RED"));
1081
1082 if (!_randomTest) {
1083 fprintf(stdout,"\n=====================================================\n");
1084 fprintf(stdout, " Registering New Codec %s, %d kHz, %d kbps\n",
1085 myCodec.plname, myCodec.plfreq / 1000, myCodec.rate / 1000);
1086 }
1087 //std::cout<< std::flush;
1088
1089 // NO DTX for supe-wideband codec at this point
1090 if (myCodec.plfreq == 32000) {
1091 *dtx = false;
1092 CHECK_ERROR_MT(myACM->SetVAD(*dtx, *vad, *mode));
1093
1094 }
1095
1096 CHECK_ERROR_MT(myACM->RegisterSendCodec(myCodec));
1097 myChannel->ResetStats();
1098 {
1099 WriteLockScoped wl(_apiTestRWLock);
1100 *thereIsEncoder = true;
1101 }
1102 Wait(500);
niklase@google.com470e71d2011-07-07 08:21:25 +00001103}
1104
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001105} // namespace webrtc