blob: 83c25b537e45c18270cfc33e86947564257f3f5d [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
Mirko Bonadei71207422017-09-15 13:58:09 +020018#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/audio_coding/include/audio_coding_module.h"
20#include "test/gtest.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
Yves Gerey665174f2018-06-19 15:03:05 +020026ACMTestTimer::ACMTestTimer() : _msec(0), _sec(0), _min(0), _hour(0) {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000027 return;
niklase@google.com470e71d2011-07-07 08:21:25 +000028}
29
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000030ACMTestTimer::~ACMTestTimer() {
31 return;
niklase@google.com470e71d2011-07-07 08:21:25 +000032}
33
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000034void ACMTestTimer::Reset() {
35 _msec = 0;
36 _sec = 0;
37 _min = 0;
38 _hour = 0;
39 return;
niklase@google.com470e71d2011-07-07 08:21:25 +000040}
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000041void ACMTestTimer::Tick10ms() {
42 _msec += 10;
43 Adjust();
44 return;
niklase@google.com470e71d2011-07-07 08:21:25 +000045}
46
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000047void ACMTestTimer::Tick1ms() {
48 _msec++;
49 Adjust();
50 return;
niklase@google.com470e71d2011-07-07 08:21:25 +000051}
52
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000053void ACMTestTimer::Tick100ms() {
54 _msec += 100;
55 Adjust();
56 return;
niklase@google.com470e71d2011-07-07 08:21:25 +000057}
58
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000059void ACMTestTimer::Tick1sec() {
60 _sec++;
61 Adjust();
62 return;
niklase@google.com470e71d2011-07-07 08:21:25 +000063}
64
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000065void ACMTestTimer::CurrentTimeHMS(char* currTime) {
66 sprintf(currTime, "%4lu:%02u:%06.3f", _hour, _min,
Yves Gerey665174f2018-06-19 15:03:05 +020067 (double)_sec + (double)_msec / 1000.);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000068 return;
niklase@google.com470e71d2011-07-07 08:21:25 +000069}
70
Yves Gerey665174f2018-06-19 15:03:05 +020071void ACMTestTimer::CurrentTime(unsigned long& h,
72 unsigned char& m,
73 unsigned char& s,
74 unsigned short& ms) {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000075 h = _hour;
76 m = _min;
77 s = _sec;
78 ms = _msec;
79 return;
niklase@google.com470e71d2011-07-07 08:21:25 +000080}
81
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000082void ACMTestTimer::Adjust() {
83 unsigned int n;
84 if (_msec >= 1000) {
85 n = _msec / 1000;
86 _msec -= (1000 * n);
87 _sec += n;
88 }
89 if (_sec >= 60) {
90 n = _sec / 60;
91 _sec -= (n * 60);
92 _min += n;
93 }
94 if (_min >= 60) {
95 n = _min / 60;
96 _min -= (n * 60);
97 _hour += n;
98 }
99}
100
101int16_t ChooseCodec(CodecInst& codecInst) {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000102 PrintCodecs();
Yves Gerey665174f2018-06-19 15:03:05 +0200103 // AudioCodingModule* tmpACM = AudioCodingModule::Create(0);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000104 uint8_t noCodec = AudioCodingModule::NumberOfCodecs();
105 int8_t codecID;
106 bool outOfRange = false;
107 char myStr[15] = "";
108 do {
109 printf("\nChoose a codec [0]: ");
110 EXPECT_TRUE(fgets(myStr, 10, stdin) != NULL);
111 codecID = atoi(myStr);
112 if ((codecID < 0) || (codecID >= noCodec)) {
113 printf("\nOut of range.\n");
114 outOfRange = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000115 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000116 } while (outOfRange);
117
Yves Gerey665174f2018-06-19 15:03:05 +0200118 CHECK_ERROR(AudioCodingModule::Codec((uint8_t)codecID, &codecInst));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000119 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000120}
121
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000122void PrintCodecs() {
123 uint8_t noCodec = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +0000124
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000125 CodecInst codecInst;
126 printf("No Name [Hz] [bps]\n");
127 for (uint8_t codecCntr = 0; codecCntr < noCodec; codecCntr++) {
128 AudioCodingModule::Codec(codecCntr, &codecInst);
129 printf("%2d- %-18s %5d %6d\n", codecCntr, codecInst.plname,
130 codecInst.plfreq, codecInst.rate);
131 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000132}
niklase@google.com470e71d2011-07-07 08:21:25 +0000133
brandtr6607d842017-02-11 00:24:10 -0800134namespace test {
135
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000136CircularBuffer::CircularBuffer(uint32_t len)
137 : _buff(NULL),
138 _idx(0),
139 _buffIsFull(false),
140 _calcAvg(false),
141 _calcVar(false),
142 _sum(0),
143 _sumSqr(0) {
144 _buff = new double[len];
145 if (_buff == NULL) {
146 _buffLen = 0;
147 } else {
148 for (uint32_t n = 0; n < len; n++) {
149 _buff[n] = 0;
150 }
151 _buffLen = len;
152 }
153}
154
155CircularBuffer::~CircularBuffer() {
156 if (_buff != NULL) {
157 delete[] _buff;
158 _buff = NULL;
159 }
160}
161
162void CircularBuffer::Update(const double newVal) {
163 assert(_buffLen > 0);
164
165 // store the value that is going to be overwritten
166 double oldVal = _buff[_idx];
167 // record the new value
168 _buff[_idx] = newVal;
169 // increment the index, to point to where we would
170 // write next
171 _idx++;
172 // it is a circular buffer, if we are at the end
173 // we have to cycle to the beginning
174 if (_idx >= _buffLen) {
175 // flag that the buffer is filled up.
176 _buffIsFull = true;
177 _idx = 0;
178 }
179
180 // Update
181
182 if (_calcAvg) {
183 // for the average we have to update
184 // the sum
185 _sum += (newVal - oldVal);
186 }
187
188 if (_calcVar) {
189 // to calculate variance we have to update
190 // the sum of squares
Yves Gerey665174f2018-06-19 15:03:05 +0200191 _sumSqr += (double)(newVal - oldVal) * (double)(newVal + oldVal);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000192 }
193}
194
195void CircularBuffer::SetArithMean(bool enable) {
196 assert(_buffLen > 0);
197
198 if (enable && !_calcAvg) {
199 uint32_t lim;
200 if (_buffIsFull) {
201 lim = _buffLen;
202 } else {
203 lim = _idx;
204 }
205 _sum = 0;
206 for (uint32_t n = 0; n < lim; n++) {
207 _sum += _buff[n];
208 }
209 }
210 _calcAvg = enable;
211}
212
213void CircularBuffer::SetVariance(bool enable) {
214 assert(_buffLen > 0);
215
216 if (enable && !_calcVar) {
217 uint32_t lim;
218 if (_buffIsFull) {
219 lim = _buffLen;
220 } else {
221 lim = _idx;
222 }
223 _sumSqr = 0;
224 for (uint32_t n = 0; n < lim; n++) {
225 _sumSqr += _buff[n] * _buff[n];
226 }
227 }
228 _calcAvg = enable;
229}
230
231int16_t CircularBuffer::ArithMean(double& mean) {
232 assert(_buffLen > 0);
233
234 if (_buffIsFull) {
Yves Gerey665174f2018-06-19 15:03:05 +0200235 mean = _sum / (double)_buffLen;
niklase@google.com470e71d2011-07-07 08:21:25 +0000236 return 0;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000237 } else {
238 if (_idx > 0) {
Yves Gerey665174f2018-06-19 15:03:05 +0200239 mean = _sum / (double)_idx;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000240 return 0;
241 } else {
242 return -1;
243 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000244 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000245}
246
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000247int16_t CircularBuffer::Variance(double& var) {
248 assert(_buffLen > 0);
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000249
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000250 if (_buffIsFull) {
Yves Gerey665174f2018-06-19 15:03:05 +0200251 var = _sumSqr / (double)_buffLen;
niklase@google.com470e71d2011-07-07 08:21:25 +0000252 return 0;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000253 } else {
254 if (_idx > 0) {
Yves Gerey665174f2018-06-19 15:03:05 +0200255 var = _sumSqr / (double)_idx;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000256 return 0;
257 } else {
258 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000259 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000260 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000261}
262
brandtr6607d842017-02-11 00:24:10 -0800263} // namespace test
264
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000265bool FixedPayloadTypeCodec(const char* payloadName) {
Yves Gerey665174f2018-06-19 15:03:05 +0200266 char fixPayloadTypeCodecs[NUM_CODECS_WITH_FIXED_PAYLOAD_TYPE][32] = {
267 "PCMU", "PCMA", "GSM", "G723", "DVI4", "LPC", "PCMA",
268 "G722", "QCELP", "CN", "MPA", "G728", "G729"};
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000269
270 for (int n = 0; n < NUM_CODECS_WITH_FIXED_PAYLOAD_TYPE; n++) {
271 if (!STR_CASE_CMP(payloadName, fixPayloadTypeCodecs[n])) {
272 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000273 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000274 }
275 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000276}
277
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000278void VADCallback::Reset() {
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000279 memset(_numFrameTypes, 0, sizeof(_numFrameTypes));
niklase@google.com470e71d2011-07-07 08:21:25 +0000280}
281
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000282VADCallback::VADCallback() {
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000283 memset(_numFrameTypes, 0, sizeof(_numFrameTypes));
niklase@google.com470e71d2011-07-07 08:21:25 +0000284}
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000285
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000286void VADCallback::PrintFrameTypes() {
pbos22993e12015-10-19 02:39:06 -0700287 printf("kEmptyFrame......... %d\n", _numFrameTypes[kEmptyFrame]);
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000288 printf("kAudioFrameSpeech... %d\n", _numFrameTypes[kAudioFrameSpeech]);
289 printf("kAudioFrameCN....... %d\n", _numFrameTypes[kAudioFrameCN]);
290 printf("kVideoFrameKey...... %d\n", _numFrameTypes[kVideoFrameKey]);
291 printf("kVideoFrameDelta.... %d\n", _numFrameTypes[kVideoFrameDelta]);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000292}
293
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000294int32_t VADCallback::InFrameType(FrameType frame_type) {
295 _numFrameTypes[frame_type]++;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000296 return 0;
297}
298
299} // namespace webrtc