blob: 74d99fce9ce18163fd26b286b79255e7cb255b74 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3 *
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
11#include "utility.h"
kjellander@webrtc.org543c3ea2011-11-23 12:20:35 +000012
niklase@google.com470e71d2011-07-07 08:21:25 +000013#include <assert.h>
14#include <stdio.h>
15#include <stdlib.h>
16
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000017#include "testing/gtest/include/gtest/gtest.h"
18#include "webrtc/common_types.h"
19#include "webrtc/modules/audio_coding/main/interface/audio_coding_module.h"
20#include "webrtc/modules/audio_coding/main/source/acm_common_defs.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000021
22#define NUM_CODECS_WITH_FIXED_PAYLOAD_TYPE 13
23
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000024namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000025
26ACMTestTimer::ACMTestTimer() :
27_msec(0),
28_sec(0),
29_min(0),
30_hour(0)
31{
32 return;
33}
34
35ACMTestTimer::~ACMTestTimer()
36{
37 return;
38}
39
40void ACMTestTimer::Reset()
41{
42 _msec = 0;
43 _sec = 0;
44 _min = 0;
45 _hour = 0;
46 return;
47}
48void ACMTestTimer::Tick10ms()
49{
50 _msec += 10;
51 Adjust();
52 return;
53}
54
55void ACMTestTimer::Tick1ms()
56{
57 _msec++;
58 Adjust();
59 return;
60}
61
62void ACMTestTimer::Tick100ms()
63{
64 _msec += 100;
65 Adjust();
66 return;
67}
68
69void ACMTestTimer::Tick1sec()
70{
71 _sec++;
72 Adjust();
73 return;
74}
75
76void ACMTestTimer::CurrentTimeHMS(char* currTime)
77{
78 sprintf(currTime, "%4lu:%02u:%06.3f", _hour, _min, (double)_sec + (double)_msec / 1000.);
79 return;
80}
81
82void ACMTestTimer::CurrentTime(
83 unsigned long& h,
84 unsigned char& m,
85 unsigned char& s,
86 unsigned short& ms)
87{
88 h = _hour;
89 m = _min;
90 s = _sec;
91 ms = _msec;
92 return;
93}
94
95void ACMTestTimer::Adjust()
96{
97 unsigned int n;
98 if(_msec >= 1000)
99 {
100 n = _msec / 1000;
101 _msec -= (1000 * n);
102 _sec += n;
103 }
104 if(_sec >= 60)
105 {
106 n = _sec / 60;
107 _sec -= (n * 60);
108 _min += n;
109 }
110 if(_min >= 60)
111 {
112 n = _min / 60;
113 _min -= (n * 60);
114 _hour += n;
115 }
116}
117
118
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000119int16_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000120ChooseCodec(
121 CodecInst& codecInst)
122{
123
124 PrintCodecs();
125 //AudioCodingModule* tmpACM = AudioCodingModule::Create(0);
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000126 uint8_t noCodec = AudioCodingModule::NumberOfCodecs();
127 int8_t codecID;
niklase@google.com470e71d2011-07-07 08:21:25 +0000128 bool outOfRange = false;
129 char myStr[15] = "";
130 do
131 {
132 printf("\nChoose a codec [0]: ");
kjellander@webrtc.org543c3ea2011-11-23 12:20:35 +0000133 EXPECT_TRUE(fgets(myStr, 10, stdin) != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000134 codecID = atoi(myStr);
135 if((codecID < 0) || (codecID >= noCodec))
136 {
137 printf("\nOut of range.\n");
138 outOfRange = true;
139 }
140 } while(outOfRange);
141
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000142 CHECK_ERROR(AudioCodingModule::Codec((uint8_t)codecID, &codecInst));
niklase@google.com470e71d2011-07-07 08:21:25 +0000143 return 0;
144}
145
146void
147PrintCodecs()
148{
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000149 uint8_t noCodec = AudioCodingModule::NumberOfCodecs();
150
niklase@google.com470e71d2011-07-07 08:21:25 +0000151 CodecInst codecInst;
152 printf("No Name [Hz] [bps]\n");
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000153 for(uint8_t codecCntr = 0; codecCntr < noCodec; codecCntr++)
niklase@google.com470e71d2011-07-07 08:21:25 +0000154 {
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +0000155 AudioCodingModule::Codec(codecCntr, &codecInst);
niklase@google.com470e71d2011-07-07 08:21:25 +0000156 printf("%2d- %-18s %5d %6d\n",
157 codecCntr, codecInst.plname, codecInst.plfreq, codecInst.rate);
158 }
159
160}
161
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000162CircularBuffer::CircularBuffer(uint32_t len):
tina.legrand@webrtc.org2e096922011-08-18 06:20:30 +0000163_buff(NULL),
164_idx(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000165_buffIsFull(false),
166_calcAvg(false),
167_calcVar(false),
168_sum(0),
tina.legrand@webrtc.org2e096922011-08-18 06:20:30 +0000169_sumSqr(0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000170{
tina.legrand@webrtc.org5efcad12011-12-20 09:05:55 +0000171 _buff = new double[len];
niklase@google.com470e71d2011-07-07 08:21:25 +0000172 if(_buff == NULL)
173 {
174 _buffLen = 0;
175 }
176 else
177 {
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000178 for(uint32_t n = 0; n < len; n++)
niklase@google.com470e71d2011-07-07 08:21:25 +0000179 {
180 _buff[n] = 0;
181 }
182 _buffLen = len;
183 }
184}
185
186CircularBuffer::~CircularBuffer()
187{
188 if(_buff != NULL)
189 {
190 delete [] _buff;
191 _buff = NULL;
192 }
193}
194
195void
196CircularBuffer::Update(
197 const double newVal)
198{
199 assert(_buffLen > 0);
200
201 // store the value that is going to be overwritten
202 double oldVal = _buff[_idx];
203 // record the new value
204 _buff[_idx] = newVal;
205 // increment the index, to point to where we would
206 // write next
207 _idx++;
208 // it is a circular buffer, if we are at the end
209 // we have to cycle to the beginning
210 if(_idx >= _buffLen)
211 {
212 // flag that the buffer is filled up.
213 _buffIsFull = true;
214 _idx = 0;
215 }
216
217 // Update
218
219 if(_calcAvg)
220 {
221 // for the average we have to update
222 // the sum
223 _sum += (newVal - oldVal);
224 }
225
226 if(_calcVar)
227 {
228 // to calculate variance we have to update
229 // the sum of squares
230 _sumSqr += (double)(newVal - oldVal) * (double)(newVal + oldVal);
231 }
232}
233
234void
235CircularBuffer::SetArithMean(
236 bool enable)
237{
238 assert(_buffLen > 0);
239
240 if(enable && !_calcAvg)
241 {
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000242 uint32_t lim;
niklase@google.com470e71d2011-07-07 08:21:25 +0000243 if(_buffIsFull)
244 {
245 lim = _buffLen;
246 }
247 else
248 {
249 lim = _idx;
250 }
251 _sum = 0;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000252 for(uint32_t n = 0; n < lim; n++)
niklase@google.com470e71d2011-07-07 08:21:25 +0000253 {
254 _sum += _buff[n];
255 }
256 }
257 _calcAvg = enable;
258}
259
260void
261CircularBuffer::SetVariance(
262 bool enable)
263{
264 assert(_buffLen > 0);
265
266 if(enable && !_calcVar)
267 {
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000268 uint32_t lim;
niklase@google.com470e71d2011-07-07 08:21:25 +0000269 if(_buffIsFull)
270 {
271 lim = _buffLen;
272 }
273 else
274 {
275 lim = _idx;
276 }
277 _sumSqr = 0;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000278 for(uint32_t n = 0; n < lim; n++)
niklase@google.com470e71d2011-07-07 08:21:25 +0000279 {
280 _sumSqr += _buff[n] * _buff[n];
281 }
282 }
283 _calcAvg = enable;
284}
285
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000286int16_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000287CircularBuffer::ArithMean(double& mean)
288{
289 assert(_buffLen > 0);
290
291 if(_buffIsFull)
292 {
293
294 mean = _sum / (double)_buffLen;
295 return 0;
296 }
297 else
298 {
299 if(_idx > 0)
300 {
301 mean = _sum / (double)_idx;
302 return 0;
303 }
304 else
305 {
306 return -1;
307 }
308
309 }
310}
311
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000312int16_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000313CircularBuffer::Variance(double& var)
314{
315 assert(_buffLen > 0);
316
317 if(_buffIsFull)
318 {
319 var = _sumSqr / (double)_buffLen;
320 return 0;
321 }
322 else
323 {
324 if(_idx > 0)
325 {
326 var = _sumSqr / (double)_idx;
327 return 0;
328 }
329 else
330 {
331 return -1;
332 }
333 }
334}
335
336
337
338bool
339FixedPayloadTypeCodec(const char* payloadName)
340{
341 char fixPayloadTypeCodecs[NUM_CODECS_WITH_FIXED_PAYLOAD_TYPE][32] = {
342 "PCMU",
343 "PCMA",
344 "GSM",
345 "G723",
346 "DVI4",
347 "LPC",
348 "PCMA",
349 "G722",
350 "QCELP",
351 "CN",
352 "MPA",
353 "G728",
354 "G729"
355 };
356
357 for(int n = 0; n < NUM_CODECS_WITH_FIXED_PAYLOAD_TYPE; n++)
358 {
359 if(!STR_CASE_CMP(payloadName, fixPayloadTypeCodecs[n]))
360 {
361 return true;
362 }
363 }
364 return false;
365}
366
367DTMFDetector::DTMFDetector()
368{
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000369 for(int16_t n = 0; n < 1000; n++)
niklase@google.com470e71d2011-07-07 08:21:25 +0000370 {
371 _toneCntr[n] = 0;
372 }
373}
374
375DTMFDetector::~DTMFDetector()
376{
377}
378
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000379int32_t DTMFDetector::IncomingDtmf(const uint8_t digitDtmf, const bool /* toneEnded */)
niklase@google.com470e71d2011-07-07 08:21:25 +0000380{
381 fprintf(stdout, "%d-",digitDtmf);
382 _toneCntr[digitDtmf]++;
383 return 0;
384}
385
386void DTMFDetector::PrintDetectedDigits()
387{
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000388 for(int16_t n = 0; n < 1000; n++)
niklase@google.com470e71d2011-07-07 08:21:25 +0000389 {
390 if(_toneCntr[n] > 0)
391 {
392 fprintf(stdout, "%d %u msec, \n", n, _toneCntr[n]*10);
393 }
394 }
395 fprintf(stdout, "\n");
396 return;
397}
398
399void
400VADCallback::Reset()
401{
402 for(int n = 0; n < 6; n++)
403 {
404 _numFrameTypes[n] = 0;
405 }
406}
407
408VADCallback::VADCallback()
409{
410 for(int n = 0; n < 6; n++)
411 {
412 _numFrameTypes[n] = 0;
413 }
414}
415
416void
417VADCallback::PrintFrameTypes()
418{
419 fprintf(stdout, "No encoding.................. %d\n", _numFrameTypes[0]);
420 fprintf(stdout, "Active normal encoded........ %d\n", _numFrameTypes[1]);
421 fprintf(stdout, "Passive normal encoded....... %d\n", _numFrameTypes[2]);
422 fprintf(stdout, "Passive DTX wideband......... %d\n", _numFrameTypes[3]);
423 fprintf(stdout, "Passive DTX narrowband....... %d\n", _numFrameTypes[4]);
424 fprintf(stdout, "Passive DTX super-wideband... %d\n", _numFrameTypes[5]);
425}
426
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000427int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000428VADCallback::InFrameType(
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000429 int16_t frameType)
niklase@google.com470e71d2011-07-07 08:21:25 +0000430{
431 _numFrameTypes[frameType]++;
432 return 0;
433}
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000434
435} // namespace webrtc