blob: 5c1fc3f968dc7e81af8fab9c954609a3794f533f [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
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000139CircularBuffer::CircularBuffer(uint32_t len)
140 : _buff(NULL),
141 _idx(0),
142 _buffIsFull(false),
143 _calcAvg(false),
144 _calcVar(false),
145 _sum(0),
146 _sumSqr(0) {
147 _buff = new double[len];
148 if (_buff == NULL) {
149 _buffLen = 0;
150 } else {
151 for (uint32_t n = 0; n < len; n++) {
152 _buff[n] = 0;
153 }
154 _buffLen = len;
155 }
156}
157
158CircularBuffer::~CircularBuffer() {
159 if (_buff != NULL) {
160 delete[] _buff;
161 _buff = NULL;
162 }
163}
164
165void CircularBuffer::Update(const double newVal) {
166 assert(_buffLen > 0);
167
168 // store the value that is going to be overwritten
169 double oldVal = _buff[_idx];
170 // record the new value
171 _buff[_idx] = newVal;
172 // increment the index, to point to where we would
173 // write next
174 _idx++;
175 // it is a circular buffer, if we are at the end
176 // we have to cycle to the beginning
177 if (_idx >= _buffLen) {
178 // flag that the buffer is filled up.
179 _buffIsFull = true;
180 _idx = 0;
181 }
182
183 // Update
184
185 if (_calcAvg) {
186 // for the average we have to update
187 // the sum
188 _sum += (newVal - oldVal);
189 }
190
191 if (_calcVar) {
192 // to calculate variance we have to update
193 // the sum of squares
194 _sumSqr += (double) (newVal - oldVal) * (double) (newVal + oldVal);
195 }
196}
197
198void CircularBuffer::SetArithMean(bool enable) {
199 assert(_buffLen > 0);
200
201 if (enable && !_calcAvg) {
202 uint32_t lim;
203 if (_buffIsFull) {
204 lim = _buffLen;
205 } else {
206 lim = _idx;
207 }
208 _sum = 0;
209 for (uint32_t n = 0; n < lim; n++) {
210 _sum += _buff[n];
211 }
212 }
213 _calcAvg = enable;
214}
215
216void CircularBuffer::SetVariance(bool enable) {
217 assert(_buffLen > 0);
218
219 if (enable && !_calcVar) {
220 uint32_t lim;
221 if (_buffIsFull) {
222 lim = _buffLen;
223 } else {
224 lim = _idx;
225 }
226 _sumSqr = 0;
227 for (uint32_t n = 0; n < lim; n++) {
228 _sumSqr += _buff[n] * _buff[n];
229 }
230 }
231 _calcAvg = enable;
232}
233
234int16_t CircularBuffer::ArithMean(double& mean) {
235 assert(_buffLen > 0);
236
237 if (_buffIsFull) {
238
239 mean = _sum / (double) _buffLen;
niklase@google.com470e71d2011-07-07 08:21:25 +0000240 return 0;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000241 } else {
242 if (_idx > 0) {
243 mean = _sum / (double) _idx;
244 return 0;
245 } else {
246 return -1;
247 }
248
249 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000250}
251
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000252int16_t CircularBuffer::Variance(double& var) {
253 assert(_buffLen > 0);
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000254
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000255 if (_buffIsFull) {
256 var = _sumSqr / (double) _buffLen;
niklase@google.com470e71d2011-07-07 08:21:25 +0000257 return 0;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000258 } else {
259 if (_idx > 0) {
260 var = _sumSqr / (double) _idx;
261 return 0;
262 } else {
263 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000264 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000265 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000266}
267
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000268bool FixedPayloadTypeCodec(const char* payloadName) {
269 char fixPayloadTypeCodecs[NUM_CODECS_WITH_FIXED_PAYLOAD_TYPE][32] = { "PCMU",
270 "PCMA", "GSM", "G723", "DVI4", "LPC", "PCMA", "G722", "QCELP", "CN",
271 "MPA", "G728", "G729" };
272
273 for (int n = 0; n < NUM_CODECS_WITH_FIXED_PAYLOAD_TYPE; n++) {
274 if (!STR_CASE_CMP(payloadName, fixPayloadTypeCodecs[n])) {
275 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000276 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000277 }
278 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000279}
280
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000281void VADCallback::Reset() {
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000282 memset(_numFrameTypes, 0, sizeof(_numFrameTypes));
niklase@google.com470e71d2011-07-07 08:21:25 +0000283}
284
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000285VADCallback::VADCallback() {
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000286 memset(_numFrameTypes, 0, sizeof(_numFrameTypes));
niklase@google.com470e71d2011-07-07 08:21:25 +0000287}
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000288
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000289void VADCallback::PrintFrameTypes() {
pbos22993e12015-10-19 02:39:06 -0700290 printf("kEmptyFrame......... %d\n", _numFrameTypes[kEmptyFrame]);
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000291 printf("kAudioFrameSpeech... %d\n", _numFrameTypes[kAudioFrameSpeech]);
292 printf("kAudioFrameCN....... %d\n", _numFrameTypes[kAudioFrameCN]);
293 printf("kVideoFrameKey...... %d\n", _numFrameTypes[kVideoFrameKey]);
294 printf("kVideoFrameDelta.... %d\n", _numFrameTypes[kVideoFrameDelta]);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000295}
296
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000297int32_t VADCallback::InFrameType(FrameType frame_type) {
298 _numFrameTypes[frame_type]++;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000299 return 0;
300}
301
302} // namespace webrtc