blob: 30bafd4a4913457c5eb06c5314a0e7f66dc28e5c [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>
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +000016#include <string.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000017
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000018#include "webrtc/common_types.h"
kjellander3e6db232015-11-26 04:44:54 -080019#include "webrtc/modules/audio_coding/acm2/acm_common_defs.h"
kwibergac9f8762016-09-30 22:29:43 -070020#include "webrtc/modules/audio_coding/include/audio_coding_module.h"
21#include "webrtc/test/gtest.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000022
23#define NUM_CODECS_WITH_FIXED_PAYLOAD_TYPE 13
24
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000025namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000026
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000027ACMTestTimer::ACMTestTimer()
28 : _msec(0),
29 _sec(0),
30 _min(0),
31 _hour(0) {
32 return;
niklase@google.com470e71d2011-07-07 08:21:25 +000033}
34
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000035ACMTestTimer::~ACMTestTimer() {
36 return;
niklase@google.com470e71d2011-07-07 08:21:25 +000037}
38
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000039void ACMTestTimer::Reset() {
40 _msec = 0;
41 _sec = 0;
42 _min = 0;
43 _hour = 0;
44 return;
niklase@google.com470e71d2011-07-07 08:21:25 +000045}
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000046void ACMTestTimer::Tick10ms() {
47 _msec += 10;
48 Adjust();
49 return;
niklase@google.com470e71d2011-07-07 08:21:25 +000050}
51
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000052void ACMTestTimer::Tick1ms() {
53 _msec++;
54 Adjust();
55 return;
niklase@google.com470e71d2011-07-07 08:21:25 +000056}
57
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000058void ACMTestTimer::Tick100ms() {
59 _msec += 100;
60 Adjust();
61 return;
niklase@google.com470e71d2011-07-07 08:21:25 +000062}
63
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000064void ACMTestTimer::Tick1sec() {
65 _sec++;
66 Adjust();
67 return;
niklase@google.com470e71d2011-07-07 08:21:25 +000068}
69
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000070void ACMTestTimer::CurrentTimeHMS(char* currTime) {
71 sprintf(currTime, "%4lu:%02u:%06.3f", _hour, _min,
72 (double) _sec + (double) _msec / 1000.);
73 return;
niklase@google.com470e71d2011-07-07 08:21:25 +000074}
75
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000076void ACMTestTimer::CurrentTime(unsigned long& h, unsigned char& m,
77 unsigned char& s, unsigned short& ms) {
78 h = _hour;
79 m = _min;
80 s = _sec;
81 ms = _msec;
82 return;
niklase@google.com470e71d2011-07-07 08:21:25 +000083}
84
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000085void ACMTestTimer::Adjust() {
86 unsigned int n;
87 if (_msec >= 1000) {
88 n = _msec / 1000;
89 _msec -= (1000 * n);
90 _sec += n;
91 }
92 if (_sec >= 60) {
93 n = _sec / 60;
94 _sec -= (n * 60);
95 _min += n;
96 }
97 if (_min >= 60) {
98 n = _min / 60;
99 _min -= (n * 60);
100 _hour += n;
101 }
102}
103
104int16_t ChooseCodec(CodecInst& codecInst) {
105
106 PrintCodecs();
107 //AudioCodingModule* tmpACM = AudioCodingModule::Create(0);
108 uint8_t noCodec = AudioCodingModule::NumberOfCodecs();
109 int8_t codecID;
110 bool outOfRange = false;
111 char myStr[15] = "";
112 do {
113 printf("\nChoose a codec [0]: ");
114 EXPECT_TRUE(fgets(myStr, 10, stdin) != NULL);
115 codecID = atoi(myStr);
116 if ((codecID < 0) || (codecID >= noCodec)) {
117 printf("\nOut of range.\n");
118 outOfRange = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000119 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000120 } while (outOfRange);
121
122 CHECK_ERROR(AudioCodingModule::Codec((uint8_t )codecID, &codecInst));
123 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000124}
125
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000126void PrintCodecs() {
127 uint8_t noCodec = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +0000128
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000129 CodecInst codecInst;
130 printf("No Name [Hz] [bps]\n");
131 for (uint8_t codecCntr = 0; codecCntr < noCodec; codecCntr++) {
132 AudioCodingModule::Codec(codecCntr, &codecInst);
133 printf("%2d- %-18s %5d %6d\n", codecCntr, codecInst.plname,
134 codecInst.plfreq, codecInst.rate);
135 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000136
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000137}
niklase@google.com470e71d2011-07-07 08:21:25 +0000138
brandtr6607d842017-02-11 00:24:10 -0800139namespace test {
140
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000141CircularBuffer::CircularBuffer(uint32_t len)
142 : _buff(NULL),
143 _idx(0),
144 _buffIsFull(false),
145 _calcAvg(false),
146 _calcVar(false),
147 _sum(0),
148 _sumSqr(0) {
149 _buff = new double[len];
150 if (_buff == NULL) {
151 _buffLen = 0;
152 } else {
153 for (uint32_t n = 0; n < len; n++) {
154 _buff[n] = 0;
155 }
156 _buffLen = len;
157 }
158}
159
160CircularBuffer::~CircularBuffer() {
161 if (_buff != NULL) {
162 delete[] _buff;
163 _buff = NULL;
164 }
165}
166
167void CircularBuffer::Update(const double newVal) {
168 assert(_buffLen > 0);
169
170 // store the value that is going to be overwritten
171 double oldVal = _buff[_idx];
172 // record the new value
173 _buff[_idx] = newVal;
174 // increment the index, to point to where we would
175 // write next
176 _idx++;
177 // it is a circular buffer, if we are at the end
178 // we have to cycle to the beginning
179 if (_idx >= _buffLen) {
180 // flag that the buffer is filled up.
181 _buffIsFull = true;
182 _idx = 0;
183 }
184
185 // Update
186
187 if (_calcAvg) {
188 // for the average we have to update
189 // the sum
190 _sum += (newVal - oldVal);
191 }
192
193 if (_calcVar) {
194 // to calculate variance we have to update
195 // the sum of squares
196 _sumSqr += (double) (newVal - oldVal) * (double) (newVal + oldVal);
197 }
198}
199
200void CircularBuffer::SetArithMean(bool enable) {
201 assert(_buffLen > 0);
202
203 if (enable && !_calcAvg) {
204 uint32_t lim;
205 if (_buffIsFull) {
206 lim = _buffLen;
207 } else {
208 lim = _idx;
209 }
210 _sum = 0;
211 for (uint32_t n = 0; n < lim; n++) {
212 _sum += _buff[n];
213 }
214 }
215 _calcAvg = enable;
216}
217
218void CircularBuffer::SetVariance(bool enable) {
219 assert(_buffLen > 0);
220
221 if (enable && !_calcVar) {
222 uint32_t lim;
223 if (_buffIsFull) {
224 lim = _buffLen;
225 } else {
226 lim = _idx;
227 }
228 _sumSqr = 0;
229 for (uint32_t n = 0; n < lim; n++) {
230 _sumSqr += _buff[n] * _buff[n];
231 }
232 }
233 _calcAvg = enable;
234}
235
236int16_t CircularBuffer::ArithMean(double& mean) {
237 assert(_buffLen > 0);
238
239 if (_buffIsFull) {
240
241 mean = _sum / (double) _buffLen;
niklase@google.com470e71d2011-07-07 08:21:25 +0000242 return 0;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000243 } else {
244 if (_idx > 0) {
245 mean = _sum / (double) _idx;
246 return 0;
247 } else {
248 return -1;
249 }
250
251 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000252}
253
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000254int16_t CircularBuffer::Variance(double& var) {
255 assert(_buffLen > 0);
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000256
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000257 if (_buffIsFull) {
258 var = _sumSqr / (double) _buffLen;
niklase@google.com470e71d2011-07-07 08:21:25 +0000259 return 0;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000260 } else {
261 if (_idx > 0) {
262 var = _sumSqr / (double) _idx;
263 return 0;
264 } else {
265 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000266 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000267 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000268}
269
brandtr6607d842017-02-11 00:24:10 -0800270} // namespace test
271
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000272bool FixedPayloadTypeCodec(const char* payloadName) {
273 char fixPayloadTypeCodecs[NUM_CODECS_WITH_FIXED_PAYLOAD_TYPE][32] = { "PCMU",
274 "PCMA", "GSM", "G723", "DVI4", "LPC", "PCMA", "G722", "QCELP", "CN",
275 "MPA", "G728", "G729" };
276
277 for (int n = 0; n < NUM_CODECS_WITH_FIXED_PAYLOAD_TYPE; n++) {
278 if (!STR_CASE_CMP(payloadName, fixPayloadTypeCodecs[n])) {
279 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000280 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000281 }
282 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000283}
284
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000285void VADCallback::Reset() {
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000286 memset(_numFrameTypes, 0, sizeof(_numFrameTypes));
niklase@google.com470e71d2011-07-07 08:21:25 +0000287}
288
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000289VADCallback::VADCallback() {
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000290 memset(_numFrameTypes, 0, sizeof(_numFrameTypes));
niklase@google.com470e71d2011-07-07 08:21:25 +0000291}
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000292
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000293void VADCallback::PrintFrameTypes() {
pbos22993e12015-10-19 02:39:06 -0700294 printf("kEmptyFrame......... %d\n", _numFrameTypes[kEmptyFrame]);
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000295 printf("kAudioFrameSpeech... %d\n", _numFrameTypes[kAudioFrameSpeech]);
296 printf("kAudioFrameCN....... %d\n", _numFrameTypes[kAudioFrameCN]);
297 printf("kVideoFrameKey...... %d\n", _numFrameTypes[kVideoFrameKey]);
298 printf("kVideoFrameDelta.... %d\n", _numFrameTypes[kVideoFrameDelta]);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000299}
300
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000301int32_t VADCallback::InFrameType(FrameType frame_type) {
302 _numFrameTypes[frame_type]++;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000303 return 0;
304}
305
306} // namespace webrtc