blob: 88ad7e2a76af2c79c9e4bc5f3ec393f2560d4752 [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
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000011#include "webrtc/modules/audio_coding/main/test/APITest.h"
12
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"
turaj@webrtc.org6ea3d1c2013-10-02 21:44:33 +000024#include "webrtc/common.h"
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000025#include "webrtc/common_types.h"
26#include "webrtc/engine_configurations.h"
turaj@webrtc.org532f3dc2013-09-19 00:12:23 +000027#include "webrtc/modules/audio_coding/main/acm2/acm_common_defs.h"
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000028#include "webrtc/modules/audio_coding/main/test/utility.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010029#include "webrtc/system_wrappers/include/event_wrapper.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010030#include "webrtc/system_wrappers/include/tick_util.h"
31#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
kjellander@webrtc.org5490c712011-12-21 13:34:18 +000039#define CHECK_THREAD_NULLITY(myThread, S) \
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000040 if(myThread != NULL) { \
pbos@webrtc.org86639732015-03-13 00:06:21 +000041 (myThread)->Start(); \
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000042 } else { \
43 ADD_FAILURE() << S; \
44 }
niklase@google.com470e71d2011-07-07 08:21:25 +000045
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000046void APITest::Wait(uint32_t waitLengthMs) {
47 if (_randomTest) {
48 return;
49 } else {
50 EventWrapper* myEvent = EventWrapper::Create();
51 myEvent->Wait(waitLengthMs);
52 delete myEvent;
53 return;
54 }
niklase@google.com470e71d2011-07-07 08:21:25 +000055}
56
turaj@webrtc.org6ea3d1c2013-10-02 21:44:33 +000057APITest::APITest(const Config& config)
henrik.lundin@webrtc.orgadaf8092014-04-17 08:29:10 +000058 : _acmA(AudioCodingModule::Create(1)),
59 _acmB(AudioCodingModule::Create(2)),
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000060 _channel_A2B(NULL),
61 _channel_B2A(NULL),
62 _writeToFile(true),
63 _pullEventA(NULL),
64 _pushEventA(NULL),
65 _processEventA(NULL),
66 _apiEventA(NULL),
67 _pullEventB(NULL),
68 _pushEventB(NULL),
69 _processEventB(NULL),
70 _apiEventB(NULL),
71 _codecCntrA(0),
72 _codecCntrB(0),
73 _thereIsEncoderA(false),
74 _thereIsEncoderB(false),
75 _thereIsDecoderA(false),
76 _thereIsDecoderB(false),
77 _sendVADA(false),
78 _sendDTXA(false),
79 _sendVADModeA(VADNormal),
80 _sendVADB(false),
81 _sendDTXB(false),
82 _sendVADModeB(VADNormal),
83 _minDelayA(0),
84 _minDelayB(0),
85 _dotPositionA(0),
86 _dotMoveDirectionA(1),
87 _dotPositionB(39),
88 _dotMoveDirectionB(-1),
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000089 _vadCallbackA(NULL),
90 _vadCallbackB(NULL),
91 _apiTestRWLock(*RWLockWrapper::CreateRWLock()),
92 _randomTest(false),
93 _testNumA(0),
94 _testNumB(1) {
95 int n;
96 for (n = 0; n < 32; n++) {
97 _payloadUsed[n] = false;
98 }
niklase@google.com470e71d2011-07-07 08:21:25 +000099
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000100 _movingDot[40] = '\0';
niklase@google.com470e71d2011-07-07 08:21:25 +0000101
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000102 for (int n = 0; n < 40; n++) {
103 _movingDot[n] = ' ';
104 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000105}
106
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000107APITest::~APITest() {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000108 DELETE_POINTER(_channel_A2B);
109 DELETE_POINTER(_channel_B2A);
niklase@google.com470e71d2011-07-07 08:21:25 +0000110
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000111 DELETE_POINTER(_pushEventA);
112 DELETE_POINTER(_pullEventA);
113 DELETE_POINTER(_processEventA);
114 DELETE_POINTER(_apiEventA);
niklase@google.com470e71d2011-07-07 08:21:25 +0000115
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000116 DELETE_POINTER(_pushEventB);
117 DELETE_POINTER(_pullEventB);
118 DELETE_POINTER(_processEventB);
119 DELETE_POINTER(_apiEventB);
niklase@google.com470e71d2011-07-07 08:21:25 +0000120
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000121 _inFileA.Close();
122 _outFileA.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +0000123
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000124 _inFileB.Close();
125 _outFileB.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +0000126
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000127 DELETE_POINTER(_vadCallbackA);
128 DELETE_POINTER(_vadCallbackB);
niklase@google.com470e71d2011-07-07 08:21:25 +0000129
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000130 delete &_apiTestRWLock;
niklase@google.com470e71d2011-07-07 08:21:25 +0000131}
132
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000133int16_t APITest::SetUp() {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000134 CodecInst dummyCodec;
135 int lastPayloadType = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000136
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000137 int16_t numCodecs = _acmA->NumberOfCodecs();
138 for (uint8_t n = 0; n < numCodecs; n++) {
139 AudioCodingModule::Codec(n, &dummyCodec);
140 if ((STR_CASE_CMP(dummyCodec.plname, "CN") == 0)
141 && (dummyCodec.plfreq == 32000)) {
142 continue;
niklase@google.com470e71d2011-07-07 08:21:25 +0000143 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000144
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000145 printf("Register Receive Codec %s ", dummyCodec.plname);
niklase@google.com470e71d2011-07-07 08:21:25 +0000146
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000147 if ((n != 0) && !FixedPayloadTypeCodec(dummyCodec.plname)) {
148 // Check registration with an already occupied payload type
149 int currentPayloadType = dummyCodec.pltype;
150 dummyCodec.pltype = 97; //lastPayloadType;
151 CHECK_ERROR(_acmB->RegisterReceiveCodec(dummyCodec));
152 dummyCodec.pltype = currentPayloadType;
153 }
andrew@webrtc.orgd7a71d02012-08-01 01:40:02 +0000154
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000155 if ((n < numCodecs - 1) && !FixedPayloadTypeCodec(dummyCodec.plname)) {
156 // test if re-registration works;
157 CodecInst nextCodec;
158 int currentPayloadType = dummyCodec.pltype;
159 AudioCodingModule::Codec(n + 1, &nextCodec);
160 dummyCodec.pltype = nextCodec.pltype;
161 if (!FixedPayloadTypeCodec(nextCodec.plname)) {
162 _acmB->RegisterReceiveCodec(dummyCodec);
163 }
164 dummyCodec.pltype = currentPayloadType;
165 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000166
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000167 if ((n < numCodecs - 1) && !FixedPayloadTypeCodec(dummyCodec.plname)) {
168 // test if un-registration works;
169 CodecInst nextCodec;
170 AudioCodingModule::Codec(n + 1, &nextCodec);
171 nextCodec.pltype = dummyCodec.pltype;
172 if (!FixedPayloadTypeCodec(nextCodec.plname)) {
173 CHECK_ERROR_MT(_acmA->RegisterReceiveCodec(nextCodec));
174 CHECK_ERROR_MT(_acmA->UnregisterReceiveCodec(nextCodec.pltype));
175 }
176 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000177
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000178 CHECK_ERROR_MT(_acmA->RegisterReceiveCodec(dummyCodec));
179 printf(" side A done!");
180 CHECK_ERROR_MT(_acmB->RegisterReceiveCodec(dummyCodec));
181 printf(" side B done!\n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000182
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000183 if (!strcmp(dummyCodec.plname, "CN")) {
184 CHECK_ERROR_MT(_acmA->RegisterSendCodec(dummyCodec));
185 CHECK_ERROR_MT(_acmB->RegisterSendCodec(dummyCodec));
186 }
187 lastPayloadType = dummyCodec.pltype;
188 if ((lastPayloadType >= 96) && (lastPayloadType <= 127)) {
189 _payloadUsed[lastPayloadType - 96] = true;
190 }
191 }
192 _thereIsDecoderA = true;
193 _thereIsDecoderB = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000194
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000195 // Register Send Codec
196 AudioCodingModule::Codec((uint8_t) _codecCntrA, &dummyCodec);
197 CHECK_ERROR_MT(_acmA->RegisterSendCodec(dummyCodec));
198 _thereIsEncoderA = true;
199 //
200 AudioCodingModule::Codec((uint8_t) _codecCntrB, &dummyCodec);
201 CHECK_ERROR_MT(_acmB->RegisterSendCodec(dummyCodec));
202 _thereIsEncoderB = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000203
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000204 uint16_t frequencyHz;
niklase@google.com470e71d2011-07-07 08:21:25 +0000205
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000206 printf("\n\nAPI Test\n");
207 printf("========\n");
208 printf("Hit enter to accept the default values indicated in []\n\n");
andrew@webrtc.orgd7a71d02012-08-01 01:40:02 +0000209
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000210 //--- Input A
211 std::string file_name = webrtc::test::ResourcePath(
212 "audio_coding/testfile32kHz", "pcm");
213 frequencyHz = 32000;
214 printf("Enter input file at side A [%s]: ", file_name.c_str());
215 PCMFile::ChooseFile(&file_name, 499, &frequencyHz);
216 _inFileA.Open(file_name, frequencyHz, "rb", true);
niklase@google.com470e71d2011-07-07 08:21:25 +0000217
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000218 //--- Output A
219 std::string out_file_a = webrtc::test::OutputPath() + "outA.pcm";
220 printf("Enter output file at side A [%s]: ", out_file_a.c_str());
221 PCMFile::ChooseFile(&out_file_a, 499, &frequencyHz);
222 _outFileA.Open(out_file_a, frequencyHz, "wb");
niklase@google.com470e71d2011-07-07 08:21:25 +0000223
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000224 //--- Input B
225 file_name = webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
226 printf("\n\nEnter input file at side B [%s]: ", file_name.c_str());
227 PCMFile::ChooseFile(&file_name, 499, &frequencyHz);
228 _inFileB.Open(file_name, frequencyHz, "rb", true);
niklase@google.com470e71d2011-07-07 08:21:25 +0000229
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000230 //--- Output B
231 std::string out_file_b = webrtc::test::OutputPath() + "outB.pcm";
232 printf("Enter output file at side B [%s]: ", out_file_b.c_str());
233 PCMFile::ChooseFile(&out_file_b, 499, &frequencyHz);
234 _outFileB.Open(out_file_b, frequencyHz, "wb");
niklase@google.com470e71d2011-07-07 08:21:25 +0000235
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000236 //--- Set A-to-B channel
237 _channel_A2B = new Channel(2);
238 CHECK_ERROR_MT(_acmA->RegisterTransportCallback(_channel_A2B));
turaj@webrtc.org6ea3d1c2013-10-02 21:44:33 +0000239 _channel_A2B->RegisterReceiverACM(_acmB.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000240
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000241 //--- Set B-to-A channel
242 _channel_B2A = new Channel(1);
243 CHECK_ERROR_MT(_acmB->RegisterTransportCallback(_channel_B2A));
turaj@webrtc.org6ea3d1c2013-10-02 21:44:33 +0000244 _channel_B2A->RegisterReceiverACM(_acmA.get());
tina.legrand@webrtc.org5e7ca602012-06-12 07:16:24 +0000245
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000246 //--- EVENT TIMERS
247 // A
Peter Boström64c03662015-04-08 11:24:19 +0200248 _pullEventA = EventTimerWrapper::Create();
249 _pushEventA = EventTimerWrapper::Create();
250 _processEventA = EventTimerWrapper::Create();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000251 _apiEventA = EventWrapper::Create();
252 // B
Peter Boström64c03662015-04-08 11:24:19 +0200253 _pullEventB = EventTimerWrapper::Create();
254 _pushEventB = EventTimerWrapper::Create();
255 _processEventB = EventTimerWrapper::Create();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000256 _apiEventB = EventWrapper::Create();
257
258 //--- I/O params
259 // A
260 _outFreqHzA = _outFileA.SamplingFrequency();
261 // B
262 _outFreqHzB = _outFileB.SamplingFrequency();
263
264 //Trace::SetEncryptedTraceFile("ACMAPITestEncrypted.txt");
265
266 char print[11];
267
268 // Create a trace file.
269 Trace::CreateTrace();
270 Trace::SetTraceFile(
271 (webrtc::test::OutputPath() + "acm_api_trace.txt").c_str());
272
273 printf("\nRandom Test (y/n)?");
274 EXPECT_TRUE(fgets(print, 10, stdin) != NULL);
275 print[10] = '\0';
276 if (strstr(print, "y") != NULL) {
277 _randomTest = true;
278 _verbose = false;
279 _writeToFile = false;
280 } else {
281 _randomTest = false;
282 printf("\nPrint Tests (y/n)? ");
kjellander@webrtc.org543c3ea2011-11-23 12:20:35 +0000283 EXPECT_TRUE(fgets(print, 10, stdin) != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000284 print[10] = '\0';
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000285 if (strstr(print, "y") == NULL) {
286 EXPECT_TRUE(freopen("APITest_log.txt", "w", stdout) != 0);
287 _verbose = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000288 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000289 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000290
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000291 _vadCallbackA = new VADCallback;
292 _vadCallbackB = new VADCallback;
andrew@webrtc.orgd7a71d02012-08-01 01:40:02 +0000293
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000294 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000295}
296
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000297bool APITest::PushAudioThreadA(void* obj) {
298 return static_cast<APITest*>(obj)->PushAudioRunA();
niklase@google.com470e71d2011-07-07 08:21:25 +0000299}
300
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000301bool APITest::PushAudioThreadB(void* obj) {
302 return static_cast<APITest*>(obj)->PushAudioRunB();
niklase@google.com470e71d2011-07-07 08:21:25 +0000303}
304
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000305bool APITest::PullAudioThreadA(void* obj) {
306 return static_cast<APITest*>(obj)->PullAudioRunA();
niklase@google.com470e71d2011-07-07 08:21:25 +0000307}
308
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000309bool APITest::PullAudioThreadB(void* obj) {
310 return static_cast<APITest*>(obj)->PullAudioRunB();
niklase@google.com470e71d2011-07-07 08:21:25 +0000311}
312
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000313bool APITest::ProcessThreadA(void* obj) {
314 return static_cast<APITest*>(obj)->ProcessRunA();
niklase@google.com470e71d2011-07-07 08:21:25 +0000315}
316
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000317bool APITest::ProcessThreadB(void* obj) {
318 return static_cast<APITest*>(obj)->ProcessRunB();
niklase@google.com470e71d2011-07-07 08:21:25 +0000319}
320
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000321bool APITest::APIThreadA(void* obj) {
322 return static_cast<APITest*>(obj)->APIRunA();
niklase@google.com470e71d2011-07-07 08:21:25 +0000323}
324
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000325bool APITest::APIThreadB(void* obj) {
326 return static_cast<APITest*>(obj)->APIRunB();
niklase@google.com470e71d2011-07-07 08:21:25 +0000327}
328
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000329bool APITest::PullAudioRunA() {
330 _pullEventA->Wait(100);
331 AudioFrame audioFrame;
332 if (_acmA->PlayoutData10Ms(_outFreqHzA, &audioFrame) < 0) {
333 bool thereIsDecoder;
niklase@google.com470e71d2011-07-07 08:21:25 +0000334 {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000335 ReadLockScoped rl(_apiTestRWLock);
336 thereIsDecoder = _thereIsDecoderA;
niklase@google.com470e71d2011-07-07 08:21:25 +0000337 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000338 if (thereIsDecoder) {
339 fprintf(stderr, "\n>>>>>> cannot pull audio A <<<<<<<< \n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000340 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000341 } else {
342 if (_writeToFile) {
343 _outFileA.Write10MsData(audioFrame);
344 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000345 }
346 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000347}
348
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000349bool APITest::PullAudioRunB() {
350 _pullEventB->Wait(100);
351 AudioFrame audioFrame;
352 if (_acmB->PlayoutData10Ms(_outFreqHzB, &audioFrame) < 0) {
353 bool thereIsDecoder;
niklase@google.com470e71d2011-07-07 08:21:25 +0000354 {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000355 ReadLockScoped rl(_apiTestRWLock);
356 thereIsDecoder = _thereIsDecoderB;
niklase@google.com470e71d2011-07-07 08:21:25 +0000357 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000358 if (thereIsDecoder) {
359 fprintf(stderr, "\n>>>>>> cannot pull audio B <<<<<<<< \n");
360 fprintf(stderr, "%d %d\n", _testNumA, _testNumB);
andrew@webrtc.orgd7a71d02012-08-01 01:40:02 +0000361 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000362 } else {
363 if (_writeToFile) {
364 _outFileB.Write10MsData(audioFrame);
365 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000366 }
367 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000368}
369
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000370bool APITest::PushAudioRunA() {
371 _pushEventA->Wait(100);
372 AudioFrame audioFrame;
373 _inFileA.Read10MsData(audioFrame);
374 if (_acmA->Add10MsData(audioFrame) < 0) {
375 bool thereIsEncoder;
niklase@google.com470e71d2011-07-07 08:21:25 +0000376 {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000377 ReadLockScoped rl(_apiTestRWLock);
378 thereIsEncoder = _thereIsEncoderA;
niklase@google.com470e71d2011-07-07 08:21:25 +0000379 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000380 if (thereIsEncoder) {
381 fprintf(stderr, "\n>>>> add10MsData at A failed <<<<\n");
382 }
383 }
384 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000385}
386
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000387bool APITest::PushAudioRunB() {
388 _pushEventB->Wait(100);
389 AudioFrame audioFrame;
390 _inFileB.Read10MsData(audioFrame);
391 if (_acmB->Add10MsData(audioFrame) < 0) {
392 bool thereIsEncoder;
niklase@google.com470e71d2011-07-07 08:21:25 +0000393 {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000394 ReadLockScoped rl(_apiTestRWLock);
395 thereIsEncoder = _thereIsEncoderB;
niklase@google.com470e71d2011-07-07 08:21:25 +0000396 }
397
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000398 if (thereIsEncoder) {
399 fprintf(stderr, "\n>>>> cannot add audio to B <<<<");
400 }
401 }
402
403 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000404}
405
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000406bool APITest::ProcessRunA() {
407 _processEventA->Wait(100);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000408 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000409}
410
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000411bool APITest::ProcessRunB() {
412 _processEventB->Wait(100);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000413 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000414}
415
416/*/
417 *
418 * In side A we test the APIs which are related to sender Side.
419 *
420/*/
421
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000422void APITest::RunTest(char thread) {
423 int testNum;
424 {
425 WriteLockScoped cs(_apiTestRWLock);
426 if (thread == 'A') {
henrik.lundin1bd0e032015-09-28 06:12:17 -0700427 _testNumA = (_testNumB + 1 + (rand() % 3)) % 4;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000428 testNum = _testNumA;
niklase@google.com470e71d2011-07-07 08:21:25 +0000429
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000430 _movingDot[_dotPositionA] = ' ';
431 if (_dotPositionA == 0) {
432 _dotMoveDirectionA = 1;
433 }
434 if (_dotPositionA == 19) {
435 _dotMoveDirectionA = -1;
436 }
437 _dotPositionA += _dotMoveDirectionA;
438 _movingDot[_dotPositionA] = (_dotMoveDirectionA > 0) ? '>' : '<';
439 } else {
henrik.lundin1bd0e032015-09-28 06:12:17 -0700440 _testNumB = (_testNumA + 1 + (rand() % 3)) % 4;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000441 testNum = _testNumB;
andrew@webrtc.orgd7a71d02012-08-01 01:40:02 +0000442
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000443 _movingDot[_dotPositionB] = ' ';
444 if (_dotPositionB == 20) {
445 _dotMoveDirectionB = 1;
446 }
447 if (_dotPositionB == 39) {
448 _dotMoveDirectionB = -1;
449 }
450 _dotPositionB += _dotMoveDirectionB;
451 _movingDot[_dotPositionB] = (_dotMoveDirectionB > 0) ? '>' : '<';
niklase@google.com470e71d2011-07-07 08:21:25 +0000452 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000453 //fprintf(stderr, "%c: %d \n", thread, testNum);
454 //fflush(stderr);
455 }
456 switch (testNum) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000457 case 0:
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000458 CurrentCodec('A');
459 ChangeCodec('A');
460 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000461 case 1:
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000462 if (!_randomTest) {
463 fprintf(stdout, "\nTesting Delay ...\n");
464 }
465 TestDelay('A');
466 break;
henrik.lundin1bd0e032015-09-28 06:12:17 -0700467 case 2:
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000468 TestSendVAD('A');
469 break;
henrik.lundin1bd0e032015-09-28 06:12:17 -0700470 case 3:
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000471 TestRegisteration('A');
472 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000473 default:
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000474 fprintf(stderr, "Wrong Test Number\n");
marpan@webrtc.org4765ca52014-11-03 20:10:26 +0000475 getc(stdin);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000476 exit(1);
477 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000478}
479
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000480bool APITest::APIRunA() {
481 _apiEventA->Wait(50);
niklase@google.com470e71d2011-07-07 08:21:25 +0000482
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000483 bool randomTest;
484 {
485 ReadLockScoped rl(_apiTestRWLock);
486 randomTest = _randomTest;
487 }
488 if (randomTest) {
489 RunTest('A');
490 } else {
491 CurrentCodec('A');
492 ChangeCodec('A');
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000493 if (_codecCntrA == 0) {
494 fprintf(stdout, "\nTesting Delay ...\n");
495 TestDelay('A');
niklase@google.com470e71d2011-07-07 08:21:25 +0000496 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000497 // VAD TEST
498 TestSendVAD('A');
499 TestRegisteration('A');
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000500 }
501 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000502}
503
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000504bool APITest::APIRunB() {
505 _apiEventB->Wait(50);
506 bool randomTest;
507 {
508 ReadLockScoped rl(_apiTestRWLock);
509 randomTest = _randomTest;
510 }
511 //_apiEventB->Wait(2000);
512 if (randomTest) {
513 RunTest('B');
514 }
andrew@webrtc.orgd7a71d02012-08-01 01:40:02 +0000515
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000516 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000517}
518
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000519void APITest::Perform() {
520 SetUp();
niklase@google.com470e71d2011-07-07 08:21:25 +0000521
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000522 //--- THREADS
523 // A
524 // PUSH
pbos12411ef2015-11-23 14:47:56 -0800525 rtc::scoped_ptr<PlatformThread> myPushAudioThreadA =
526 PlatformThread::CreateThread(PushAudioThreadA, this, "PushAudioThreadA");
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000527 CHECK_THREAD_NULLITY(myPushAudioThreadA, "Unable to start A::PUSH thread");
528 // PULL
pbos12411ef2015-11-23 14:47:56 -0800529 rtc::scoped_ptr<PlatformThread> myPullAudioThreadA =
530 PlatformThread::CreateThread(PullAudioThreadA, this, "PullAudioThreadA");
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000531 CHECK_THREAD_NULLITY(myPullAudioThreadA, "Unable to start A::PULL thread");
532 // Process
pbos12411ef2015-11-23 14:47:56 -0800533 rtc::scoped_ptr<PlatformThread> myProcessThreadA =
534 PlatformThread::CreateThread(ProcessThreadA, this, "ProcessThreadA");
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000535 CHECK_THREAD_NULLITY(myProcessThreadA, "Unable to start A::Process thread");
536 // API
pbos12411ef2015-11-23 14:47:56 -0800537 rtc::scoped_ptr<PlatformThread> myAPIThreadA =
538 PlatformThread::CreateThread(APIThreadA, this, "APIThreadA");
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000539 CHECK_THREAD_NULLITY(myAPIThreadA, "Unable to start A::API thread");
540 // B
541 // PUSH
pbos12411ef2015-11-23 14:47:56 -0800542 rtc::scoped_ptr<PlatformThread> myPushAudioThreadB =
543 PlatformThread::CreateThread(PushAudioThreadB, this, "PushAudioThreadB");
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000544 CHECK_THREAD_NULLITY(myPushAudioThreadB, "Unable to start B::PUSH thread");
545 // PULL
pbos12411ef2015-11-23 14:47:56 -0800546 rtc::scoped_ptr<PlatformThread> myPullAudioThreadB =
547 PlatformThread::CreateThread(PullAudioThreadB, this, "PullAudioThreadB");
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000548 CHECK_THREAD_NULLITY(myPullAudioThreadB, "Unable to start B::PULL thread");
549 // Process
pbos12411ef2015-11-23 14:47:56 -0800550 rtc::scoped_ptr<PlatformThread> myProcessThreadB =
551 PlatformThread::CreateThread(ProcessThreadB, this, "ProcessThreadB");
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000552 CHECK_THREAD_NULLITY(myProcessThreadB, "Unable to start B::Process thread");
553 // API
pbos12411ef2015-11-23 14:47:56 -0800554 rtc::scoped_ptr<PlatformThread> myAPIThreadB =
555 PlatformThread::CreateThread(APIThreadB, this, "APIThreadB");
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000556 CHECK_THREAD_NULLITY(myAPIThreadB, "Unable to start B::API thread");
andrew@webrtc.orgd7a71d02012-08-01 01:40:02 +0000557
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000558 //_apiEventA->StartTimer(true, 5000);
559 //_apiEventB->StartTimer(true, 5000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000560
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000561 _processEventA->StartTimer(true, 10);
562 _processEventB->StartTimer(true, 10);
niklase@google.com470e71d2011-07-07 08:21:25 +0000563
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000564 _pullEventA->StartTimer(true, 10);
565 _pullEventB->StartTimer(true, 10);
andrew@webrtc.orgd7a71d02012-08-01 01:40:02 +0000566
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000567 _pushEventA->StartTimer(true, 10);
568 _pushEventB->StartTimer(true, 10);
niklase@google.com470e71d2011-07-07 08:21:25 +0000569
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000570 // Keep main thread waiting for sender/receiver
571 // threads to complete
572 EventWrapper* completeEvent = EventWrapper::Create();
573 uint64_t startTime = TickTime::MillisecondTimestamp();
574 uint64_t currentTime;
575 // Run test in 2 minutes (120000 ms).
576 do {
niklase@google.com470e71d2011-07-07 08:21:25 +0000577 {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000578 //ReadLockScoped rl(_apiTestRWLock);
579 //fprintf(stderr, "\r%s", _movingDot);
580 }
581 //fflush(stderr);
582 completeEvent->Wait(50);
583 currentTime = TickTime::MillisecondTimestamp();
584 } while ((currentTime - startTime) < 120000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000585
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000586 //completeEvent->Wait(0xFFFFFFFF);
587 //(unsigned long)((unsigned long)TEST_DURATION_SEC * (unsigned long)1000));
588 delete completeEvent;
andrew@webrtc.orgd7a71d02012-08-01 01:40:02 +0000589
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000590 myPushAudioThreadA->Stop();
591 myPullAudioThreadA->Stop();
592 myProcessThreadA->Stop();
593 myAPIThreadA->Stop();
niklase@google.com470e71d2011-07-07 08:21:25 +0000594
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000595 myPushAudioThreadB->Stop();
596 myPullAudioThreadB->Stop();
597 myProcessThreadB->Stop();
598 myAPIThreadB->Stop();
niklase@google.com470e71d2011-07-07 08:21:25 +0000599}
600
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000601void APITest::CheckVADStatus(char side) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000602
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000603 bool dtxEnabled;
604 bool vadEnabled;
605 ACMVADMode vadMode;
niklase@google.com470e71d2011-07-07 08:21:25 +0000606
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000607 if (side == 'A') {
608 _acmA->VAD(&dtxEnabled, &vadEnabled, &vadMode);
609 _acmA->RegisterVADCallback(NULL);
610 _vadCallbackA->Reset();
611 _acmA->RegisterVADCallback(_vadCallbackA);
tina.legrand@webrtc.org2e096922011-08-18 06:20:30 +0000612
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000613 if (!_randomTest) {
614 if (_verbose) {
615 fprintf(stdout, "DTX %3s, VAD %3s, Mode %d", dtxEnabled ? "ON" : "OFF",
616 vadEnabled ? "ON" : "OFF", (int) vadMode);
617 Wait(5000);
618 fprintf(stdout, " => bit-rate %3.0f kbps\n", _channel_A2B->BitRate());
619 } else {
620 Wait(5000);
621 fprintf(stdout, "DTX %3s, VAD %3s, Mode %d => bit-rate %3.0f kbps\n",
622 dtxEnabled ? "ON" : "OFF", vadEnabled ? "ON" : "OFF",
623 (int) vadMode, _channel_A2B->BitRate());
624 }
625 _vadCallbackA->PrintFrameTypes();
niklase@google.com470e71d2011-07-07 08:21:25 +0000626 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000627
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000628 if (dtxEnabled != _sendDTXA) {
629 fprintf(stderr, ">>> Error Enabling DTX <<<\n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000630 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000631 if ((vadEnabled != _sendVADA) && (!dtxEnabled)) {
632 fprintf(stderr, ">>> Error Enabling VAD <<<\n");
633 }
634 if ((vadMode != _sendVADModeA) && vadEnabled) {
635 fprintf(stderr, ">>> Error setting VAD-mode <<<\n");
636 }
637 } else {
638 _acmB->VAD(&dtxEnabled, &vadEnabled, &vadMode);
639
640 _acmB->RegisterVADCallback(NULL);
641 _vadCallbackB->Reset();
642 _acmB->RegisterVADCallback(_vadCallbackB);
643
644 if (!_randomTest) {
645 if (_verbose) {
646 fprintf(stdout, "DTX %3s, VAD %3s, Mode %d", dtxEnabled ? "ON" : "OFF",
647 vadEnabled ? "ON" : "OFF", (int) vadMode);
648 Wait(5000);
649 fprintf(stdout, " => bit-rate %3.0f kbps\n", _channel_B2A->BitRate());
650 } else {
651 Wait(5000);
652 fprintf(stdout, "DTX %3s, VAD %3s, Mode %d => bit-rate %3.0f kbps\n",
653 dtxEnabled ? "ON" : "OFF", vadEnabled ? "ON" : "OFF",
654 (int) vadMode, _channel_B2A->BitRate());
655 }
656 _vadCallbackB->PrintFrameTypes();
657 }
658
659 if (dtxEnabled != _sendDTXB) {
660 fprintf(stderr, ">>> Error Enabling DTX <<<\n");
661 }
662 if ((vadEnabled != _sendVADB) && (!dtxEnabled)) {
663 fprintf(stderr, ">>> Error Enabling VAD <<<\n");
664 }
665 if ((vadMode != _sendVADModeB) && vadEnabled) {
666 fprintf(stderr, ">>> Error setting VAD-mode <<<\n");
667 }
668 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000669}
670
671// Set Min delay, get delay, playout timestamp
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000672void APITest::TestDelay(char side) {
673 AudioCodingModule* myACM;
674 Channel* myChannel;
675 int32_t* myMinDelay;
Peter Boström64c03662015-04-08 11:24:19 +0200676 EventTimerWrapper* myEvent = EventTimerWrapper::Create();
niklase@google.com470e71d2011-07-07 08:21:25 +0000677
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000678 uint32_t inTimestamp = 0;
679 uint32_t outTimestamp = 0;
680 double estimDelay = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000681
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000682 double averageEstimDelay = 0;
683 double averageDelay = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000684
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000685 CircularBuffer estimDelayCB(100);
686 estimDelayCB.SetArithMean(true);
niklase@google.com470e71d2011-07-07 08:21:25 +0000687
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000688 if (side == 'A') {
turaj@webrtc.org6ea3d1c2013-10-02 21:44:33 +0000689 myACM = _acmA.get();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000690 myChannel = _channel_B2A;
691 myMinDelay = &_minDelayA;
692 } else {
turaj@webrtc.org6ea3d1c2013-10-02 21:44:33 +0000693 myACM = _acmB.get();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000694 myChannel = _channel_A2B;
695 myMinDelay = &_minDelayB;
696 }
697
698 CHECK_ERROR_MT(myACM->SetMinimumPlayoutDelay(*myMinDelay));
699
700 inTimestamp = myChannel->LastInTimestamp();
701 CHECK_ERROR_MT(myACM->PlayoutTimestamp(&outTimestamp));
702
703 if (!_randomTest) {
704 myEvent->StartTimer(true, 30);
705 int n = 0;
706 int settlePoint = 5000;
707 while (n < settlePoint + 400) {
708 myEvent->Wait(1000);
709
710 inTimestamp = myChannel->LastInTimestamp();
711 CHECK_ERROR_MT(myACM->PlayoutTimestamp(&outTimestamp));
712
713 //std::cout << outTimestamp << std::endl << std::flush;
714 estimDelay = (double) ((uint32_t)(inTimestamp - outTimestamp))
715 / ((double) myACM->ReceiveFrequency() / 1000.0);
716
717 estimDelayCB.Update(estimDelay);
718
719 estimDelayCB.ArithMean(averageEstimDelay);
720 //printf("\n %6.1f \n", estimDelay);
721 //std::cout << " " << std::flush;
722
723 if (_verbose) {
724 fprintf(stdout,
725 "\rExpected: %4d, retreived: %6.1f, measured: %6.1f",
726 *myMinDelay, averageDelay, averageEstimDelay);
727 std::cout << " " << std::flush;
728 }
729 if ((averageDelay > *myMinDelay) && (n < settlePoint)) {
730 settlePoint = n;
731 }
732 n++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000733 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000734 myEvent->StopTimer();
735 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000736
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000737 if ((!_verbose) && (!_randomTest)) {
738 fprintf(stdout, "\nExpected: %4d, retreived: %6.1f, measured: %6.1f",
niklase@google.com470e71d2011-07-07 08:21:25 +0000739 *myMinDelay, averageDelay, averageEstimDelay);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000740 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000741
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000742 *myMinDelay = (rand() % 1000) + 1;
andrew@webrtc.orgd7a71d02012-08-01 01:40:02 +0000743
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000744 NetworkStatistics networkStat;
745 CHECK_ERROR_MT(myACM->GetNetworkStatistics(&networkStat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000746
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000747 if (!_randomTest) {
748 fprintf(stdout, "\n\nJitter Statistics at Side %c\n", side);
749 fprintf(stdout, "--------------------------------------\n");
750 fprintf(stdout, "buffer-size............. %d\n",
751 networkStat.currentBufferSize);
752 fprintf(stdout, "Preferred buffer-size... %d\n",
753 networkStat.preferredBufferSize);
754 fprintf(stdout, "Peaky jitter mode........%d\n",
755 networkStat.jitterPeaksFound);
756 fprintf(stdout, "packet-size rate........ %d\n",
757 networkStat.currentPacketLossRate);
758 fprintf(stdout, "discard rate............ %d\n",
759 networkStat.currentDiscardRate);
760 fprintf(stdout, "expand rate............. %d\n",
761 networkStat.currentExpandRate);
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000762 fprintf(stdout, "speech expand rate...... %d\n",
763 networkStat.currentSpeechExpandRate);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000764 fprintf(stdout, "Preemptive rate......... %d\n",
765 networkStat.currentPreemptiveRate);
766 fprintf(stdout, "Accelerate rate......... %d\n",
767 networkStat.currentAccelerateRate);
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000768 fprintf(stdout, "Secondary decoded rate.. %d\n",
769 networkStat.currentSecondaryDecodedRate);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000770 fprintf(stdout, "Clock-drift............. %d\n", networkStat.clockDriftPPM);
771 fprintf(stdout, "Mean waiting time....... %d\n",
772 networkStat.meanWaitingTimeMs);
773 fprintf(stdout, "Median waiting time..... %d\n",
774 networkStat.medianWaitingTimeMs);
775 fprintf(stdout, "Min waiting time........ %d\n",
776 networkStat.minWaitingTimeMs);
777 fprintf(stdout, "Max waiting time........ %d\n",
778 networkStat.maxWaitingTimeMs);
779 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000780
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000781 CHECK_ERROR_MT(myACM->SetMinimumPlayoutDelay(*myMinDelay));
niklase@google.com470e71d2011-07-07 08:21:25 +0000782
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000783 if (!_randomTest) {
784 myEvent->Wait(500);
785 fprintf(stdout, "\n");
786 fprintf(stdout, "\n");
787 }
788 delete myEvent;
niklase@google.com470e71d2011-07-07 08:21:25 +0000789}
790
791// Unregister a codec & register again.
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000792void APITest::TestRegisteration(char sendSide) {
793 AudioCodingModule* sendACM;
794 AudioCodingModule* receiveACM;
795 bool* thereIsDecoder;
796 EventWrapper* myEvent = EventWrapper::Create();
niklase@google.com470e71d2011-07-07 08:21:25 +0000797
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000798 if (!_randomTest) {
799 fprintf(stdout, "\n\n");
800 fprintf(stdout,
801 "---------------------------------------------------------\n");
802 fprintf(stdout, " Unregister/register Receive Codec\n");
803 fprintf(stdout,
804 "---------------------------------------------------------\n");
805 }
806
807 switch (sendSide) {
808 case 'A': {
turaj@webrtc.org6ea3d1c2013-10-02 21:44:33 +0000809 sendACM = _acmA.get();
810 receiveACM = _acmB.get();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000811 thereIsDecoder = &_thereIsDecoderB;
812 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000813 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000814 case 'B': {
turaj@webrtc.org6ea3d1c2013-10-02 21:44:33 +0000815 sendACM = _acmB.get();
816 receiveACM = _acmA.get();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000817 thereIsDecoder = &_thereIsDecoderA;
818 break;
819 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000820 default:
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000821 fprintf(stderr, "Invalid sender-side in TestRegistration(%c)\n",
822 sendSide);
823 exit(-1);
824 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000825
kwiberg1fd4a4a2015-11-03 11:20:50 -0800826 auto myCodec = sendACM->SendCodec();
827 if (!myCodec) {
828 CodecInst ci;
829 AudioCodingModule::Codec(_codecCntrA, &ci);
Karl Wibergbe579832015-11-10 22:34:18 +0100830 myCodec = rtc::Optional<CodecInst>(ci);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000831 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000832
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000833 if (!_randomTest) {
834 fprintf(stdout, "Unregistering reveive codec, NO AUDIO.\n");
835 fflush (stdout);
836 }
837 {
838 WriteLockScoped wl(_apiTestRWLock);
839 *thereIsDecoder = false;
840 }
841 //myEvent->Wait(20);
kwiberg1fd4a4a2015-11-03 11:20:50 -0800842 CHECK_ERROR_MT(receiveACM->UnregisterReceiveCodec(myCodec->pltype));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000843 Wait(1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000844
kwiberg1fd4a4a2015-11-03 11:20:50 -0800845 int currentPayload = myCodec->pltype;
niklase@google.com470e71d2011-07-07 08:21:25 +0000846
kwiberg1fd4a4a2015-11-03 11:20:50 -0800847 if (!FixedPayloadTypeCodec(myCodec->plname)) {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000848 int32_t i;
849 for (i = 0; i < 32; i++) {
850 if (!_payloadUsed[i]) {
851 if (!_randomTest) {
852 fprintf(stdout,
853 "Register receive codec with new Payload, AUDIO BACK.\n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000854 }
kwiberg1fd4a4a2015-11-03 11:20:50 -0800855 //myCodec->pltype = i + 96;
856 //CHECK_ERROR_MT(receiveACM->RegisterReceiveCodec(*myCodec));
857 //CHECK_ERROR_MT(sendACM->RegisterSendCodec(*myCodec));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000858 //myEvent->Wait(20);
859 //{
860 // WriteLockScoped wl(_apiTestRWLock);
861 // *thereIsDecoder = true;
862 //}
863 Wait(1000);
864
865 if (!_randomTest) {
866 fprintf(stdout, "Unregistering reveive codec, NO AUDIO.\n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000867 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000868 //{
869 // WriteLockScoped wl(_apiTestRWLock);
870 // *thereIsDecoder = false;
871 //}
872 //myEvent->Wait(20);
kwiberg1fd4a4a2015-11-03 11:20:50 -0800873 //CHECK_ERROR_MT(receiveACM->UnregisterReceiveCodec(myCodec->pltype));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000874 Wait(1000);
875
kwiberg1fd4a4a2015-11-03 11:20:50 -0800876 myCodec->pltype = currentPayload;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000877 if (!_randomTest) {
878 fprintf(stdout,
879 "Register receive codec with default Payload, AUDIO BACK.\n");
880 fflush (stdout);
niklase@google.com470e71d2011-07-07 08:21:25 +0000881 }
kwiberg1fd4a4a2015-11-03 11:20:50 -0800882 CHECK_ERROR_MT(receiveACM->RegisterReceiveCodec(*myCodec));
883 //CHECK_ERROR_MT(sendACM->RegisterSendCodec(*myCodec));
niklase@google.com470e71d2011-07-07 08:21:25 +0000884 myEvent->Wait(20);
885 {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000886 WriteLockScoped wl(_apiTestRWLock);
887 *thereIsDecoder = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000888 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000889 Wait(1000);
890
891 break;
892 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000893 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000894 if (i == 32) {
kwiberg1fd4a4a2015-11-03 11:20:50 -0800895 CHECK_ERROR_MT(receiveACM->RegisterReceiveCodec(*myCodec));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000896 {
897 WriteLockScoped wl(_apiTestRWLock);
898 *thereIsDecoder = true;
899 }
900 }
901 } else {
902 if (!_randomTest) {
903 fprintf(stdout,
904 "Register receive codec with fixed Payload, AUDIO BACK.\n");
905 fflush (stdout);
906 }
kwiberg1fd4a4a2015-11-03 11:20:50 -0800907 CHECK_ERROR_MT(receiveACM->RegisterReceiveCodec(*myCodec));
908 //CHECK_ERROR_MT(receiveACM->UnregisterReceiveCodec(myCodec->pltype));
909 //CHECK_ERROR_MT(receiveACM->RegisterReceiveCodec(*myCodec));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000910 myEvent->Wait(20);
niklase@google.com470e71d2011-07-07 08:21:25 +0000911 {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000912 WriteLockScoped wl(_apiTestRWLock);
913 *thereIsDecoder = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000914 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000915 }
916 delete myEvent;
917 if (!_randomTest) {
918 fprintf(stdout,
919 "---------------------------------------------------------\n");
920 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000921}
922
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000923void APITest::TestSendVAD(char side) {
924 if (_randomTest) {
925 return;
926 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000927
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000928 bool* vad;
929 bool* dtx;
930 ACMVADMode* mode;
931 Channel* myChannel;
932 AudioCodingModule* myACM;
niklase@google.com470e71d2011-07-07 08:21:25 +0000933
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000934 CodecInst myCodec;
935 if (!_randomTest) {
936 fprintf(stdout, "\n\n");
937 fprintf(stdout, "-----------------------------------------------\n");
938 fprintf(stdout, " Test VAD API\n");
939 fprintf(stdout, "-----------------------------------------------\n");
940 }
andrew@webrtc.orgd7a71d02012-08-01 01:40:02 +0000941
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000942 if (side == 'A') {
943 AudioCodingModule::Codec(_codecCntrA, &myCodec);
944 vad = &_sendVADA;
945 dtx = &_sendDTXA;
946 mode = &_sendVADModeA;
947 myChannel = _channel_A2B;
turaj@webrtc.org6ea3d1c2013-10-02 21:44:33 +0000948 myACM = _acmA.get();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000949 } else {
950 AudioCodingModule::Codec(_codecCntrB, &myCodec);
951 vad = &_sendVADB;
952 dtx = &_sendDTXB;
953 mode = &_sendVADModeB;
954 myChannel = _channel_B2A;
turaj@webrtc.org6ea3d1c2013-10-02 21:44:33 +0000955 myACM = _acmB.get();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000956 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000957
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000958 CheckVADStatus(side);
959 if (!_randomTest) {
960 fprintf(stdout, "\n\n");
961 }
962
963 switch (*mode) {
964 case VADNormal:
965 *vad = true;
966 *dtx = true;
967 *mode = VADAggr;
968 break;
969 case VADLowBitrate:
970 *vad = true;
971 *dtx = true;
972 *mode = VADVeryAggr;
973 break;
974 case VADAggr:
975 *vad = true;
976 *dtx = true;
977 *mode = VADLowBitrate;
978 break;
979 case VADVeryAggr:
980 *vad = false;
981 *dtx = false;
982 *mode = VADNormal;
983 break;
984 default:
985 *mode = VADNormal;
986 }
987
988 *dtx = (myCodec.plfreq == 32000) ? false : *dtx;
989
990 CHECK_ERROR_MT(myACM->SetVAD(*dtx, *vad, *mode));
991 myChannel->ResetStats();
992
993 CheckVADStatus(side);
994 if (!_randomTest) {
995 fprintf(stdout, "\n");
996 fprintf(stdout, "-----------------------------------------------\n");
997 }
998
999 // Fault Test
1000 CHECK_PROTECTED_MT(myACM->SetVAD(false, true, (ACMVADMode) - 1));
1001 CHECK_PROTECTED_MT(myACM->SetVAD(false, true, (ACMVADMode) 4));
niklase@google.com470e71d2011-07-07 08:21:25 +00001002
1003}
1004
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001005void APITest::CurrentCodec(char side) {
kwiberg1fd4a4a2015-11-03 11:20:50 -08001006 auto myCodec = (side == 'A' ? _acmA : _acmB)->SendCodec();
niklase@google.com470e71d2011-07-07 08:21:25 +00001007
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001008 if (!_randomTest) {
1009 fprintf(stdout, "\n\n");
1010 fprintf(stdout, "Send codec in Side A\n");
1011 fprintf(stdout, "----------------------------\n");
kwiberg1fd4a4a2015-11-03 11:20:50 -08001012 fprintf(stdout, "Name................. %s\n", myCodec->plname);
1013 fprintf(stdout, "Sampling Frequency... %d\n", myCodec->plfreq);
1014 fprintf(stdout, "Rate................. %d\n", myCodec->rate);
1015 fprintf(stdout, "Payload-type......... %d\n", myCodec->pltype);
1016 fprintf(stdout, "Packet-size.......... %d\n", myCodec->pacsize);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001017 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001018
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001019 Wait(100);
niklase@google.com470e71d2011-07-07 08:21:25 +00001020}
1021
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001022void APITest::ChangeCodec(char side) {
1023 CodecInst myCodec;
1024 AudioCodingModule* myACM;
1025 uint8_t* codecCntr;
1026 bool* thereIsEncoder;
1027 bool* vad;
1028 bool* dtx;
1029 ACMVADMode* mode;
1030 Channel* myChannel;
1031 // Reset and Wait
1032 if (!_randomTest) {
1033 fprintf(stdout, "Reset Encoder Side A \n");
1034 }
1035 if (side == 'A') {
turaj@webrtc.org6ea3d1c2013-10-02 21:44:33 +00001036 myACM = _acmA.get();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001037 codecCntr = &_codecCntrA;
niklase@google.com470e71d2011-07-07 08:21:25 +00001038 {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001039 WriteLockScoped wl(_apiTestRWLock);
1040 thereIsEncoder = &_thereIsEncoderA;
niklase@google.com470e71d2011-07-07 08:21:25 +00001041 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001042 vad = &_sendVADA;
1043 dtx = &_sendDTXA;
1044 mode = &_sendVADModeA;
1045 myChannel = _channel_A2B;
1046 } else {
turaj@webrtc.org6ea3d1c2013-10-02 21:44:33 +00001047 myACM = _acmB.get();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001048 codecCntr = &_codecCntrB;
niklase@google.com470e71d2011-07-07 08:21:25 +00001049 {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001050 WriteLockScoped wl(_apiTestRWLock);
1051 thereIsEncoder = &_thereIsEncoderB;
niklase@google.com470e71d2011-07-07 08:21:25 +00001052 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001053 vad = &_sendVADB;
1054 dtx = &_sendDTXB;
1055 mode = &_sendVADModeB;
1056 myChannel = _channel_B2A;
1057 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001058
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001059 Wait(100);
niklase@google.com470e71d2011-07-07 08:21:25 +00001060
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001061 // Register the next codec
1062 do {
1063 *codecCntr =
1064 (*codecCntr < AudioCodingModule::NumberOfCodecs() - 1) ?
1065 (*codecCntr + 1) : 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001066
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001067 if (*codecCntr == 0) {
1068 //printf("Initialize Sender Side A \n");
1069 {
niklase@google.com470e71d2011-07-07 08:21:25 +00001070 WriteLockScoped wl(_apiTestRWLock);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001071 *thereIsEncoder = false;
1072 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001073 // After Initialization CN is lost, re-register them
1074 if (AudioCodingModule::Codec("CN", &myCodec, 8000, 1) >= 0) {
1075 CHECK_ERROR_MT(myACM->RegisterSendCodec(myCodec));
1076 }
1077 if (AudioCodingModule::Codec("CN", &myCodec, 16000, 1) >= 0) {
1078 CHECK_ERROR_MT(myACM->RegisterSendCodec(myCodec));
1079 }
1080 // VAD & DTX are disabled after initialization
1081 *vad = false;
1082 *dtx = false;
1083 _writeToFile = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001084 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001085
1086 AudioCodingModule::Codec(*codecCntr, &myCodec);
1087 } while (!STR_CASE_CMP(myCodec.plname, "CN")
1088 || !STR_CASE_CMP(myCodec.plname, "telephone-event")
1089 || !STR_CASE_CMP(myCodec.plname, "RED"));
1090
1091 if (!_randomTest) {
1092 fprintf(stdout,"\n=====================================================\n");
1093 fprintf(stdout, " Registering New Codec %s, %d kHz, %d kbps\n",
1094 myCodec.plname, myCodec.plfreq / 1000, myCodec.rate / 1000);
1095 }
1096 //std::cout<< std::flush;
1097
1098 // NO DTX for supe-wideband codec at this point
1099 if (myCodec.plfreq == 32000) {
1100 *dtx = false;
1101 CHECK_ERROR_MT(myACM->SetVAD(*dtx, *vad, *mode));
1102
1103 }
1104
1105 CHECK_ERROR_MT(myACM->RegisterSendCodec(myCodec));
1106 myChannel->ResetStats();
1107 {
1108 WriteLockScoped wl(_apiTestRWLock);
1109 *thereIsEncoder = true;
1110 }
1111 Wait(500);
niklase@google.com470e71d2011-07-07 08:21:25 +00001112}
1113
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +00001114} // namespace webrtc