niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
| 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.org | 543c3ea | 2011-11-23 12:20:35 +0000 | [diff] [blame] | 12 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | #include <assert.h> |
| 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
henrik.lundin@webrtc.org | e9217b4 | 2015-03-06 07:50:34 +0000 | [diff] [blame] | 16 | #include <string.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 17 | |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 18 | #include "common_types.h" // NOLINT(build/include) |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "modules/audio_coding/include/audio_coding_module.h" |
| 20 | #include "test/gtest.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 21 | |
| 22 | #define NUM_CODECS_WITH_FIXED_PAYLOAD_TYPE 13 |
| 23 | |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 24 | namespace webrtc { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 25 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 26 | ACMTestTimer::ACMTestTimer() : _msec(0), _sec(0), _min(0), _hour(0) { |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 27 | return; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 28 | } |
| 29 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 30 | ACMTestTimer::~ACMTestTimer() { |
| 31 | return; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 32 | } |
| 33 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 34 | void ACMTestTimer::Reset() { |
| 35 | _msec = 0; |
| 36 | _sec = 0; |
| 37 | _min = 0; |
| 38 | _hour = 0; |
| 39 | return; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 40 | } |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 41 | void ACMTestTimer::Tick10ms() { |
| 42 | _msec += 10; |
| 43 | Adjust(); |
| 44 | return; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 45 | } |
| 46 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 47 | void ACMTestTimer::Tick1ms() { |
| 48 | _msec++; |
| 49 | Adjust(); |
| 50 | return; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 51 | } |
| 52 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 53 | void ACMTestTimer::Tick100ms() { |
| 54 | _msec += 100; |
| 55 | Adjust(); |
| 56 | return; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 57 | } |
| 58 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 59 | void ACMTestTimer::Tick1sec() { |
| 60 | _sec++; |
| 61 | Adjust(); |
| 62 | return; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 63 | } |
| 64 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 65 | void ACMTestTimer::CurrentTimeHMS(char* currTime) { |
| 66 | sprintf(currTime, "%4lu:%02u:%06.3f", _hour, _min, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 67 | (double)_sec + (double)_msec / 1000.); |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 68 | return; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 71 | void ACMTestTimer::CurrentTime(unsigned long& h, |
| 72 | unsigned char& m, |
| 73 | unsigned char& s, |
| 74 | unsigned short& ms) { |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 75 | h = _hour; |
| 76 | m = _min; |
| 77 | s = _sec; |
| 78 | ms = _msec; |
| 79 | return; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 80 | } |
| 81 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 82 | void 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 | |
| 101 | int16_t ChooseCodec(CodecInst& codecInst) { |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 102 | PrintCodecs(); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 103 | // AudioCodingModule* tmpACM = AudioCodingModule::Create(0); |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 104 | 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 115 | } |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 116 | } while (outOfRange); |
| 117 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 118 | CHECK_ERROR(AudioCodingModule::Codec((uint8_t)codecID, &codecInst)); |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 119 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 120 | } |
| 121 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 122 | void PrintCodecs() { |
| 123 | uint8_t noCodec = AudioCodingModule::NumberOfCodecs(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 124 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 125 | 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.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 132 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 133 | |
brandtr | 6607d84 | 2017-02-11 00:24:10 -0800 | [diff] [blame] | 134 | namespace test { |
| 135 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 136 | CircularBuffer::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 | |
| 155 | CircularBuffer::~CircularBuffer() { |
| 156 | if (_buff != NULL) { |
| 157 | delete[] _buff; |
| 158 | _buff = NULL; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | void 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 191 | _sumSqr += (double)(newVal - oldVal) * (double)(newVal + oldVal); |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
| 195 | void 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 | |
| 213 | void 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 | |
| 231 | int16_t CircularBuffer::ArithMean(double& mean) { |
| 232 | assert(_buffLen > 0); |
| 233 | |
| 234 | if (_buffIsFull) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 235 | mean = _sum / (double)_buffLen; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 236 | return 0; |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 237 | } else { |
| 238 | if (_idx > 0) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 239 | mean = _sum / (double)_idx; |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 240 | return 0; |
| 241 | } else { |
| 242 | return -1; |
| 243 | } |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 244 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 245 | } |
| 246 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 247 | int16_t CircularBuffer::Variance(double& var) { |
| 248 | assert(_buffLen > 0); |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 249 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 250 | if (_buffIsFull) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 251 | var = _sumSqr / (double)_buffLen; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 252 | return 0; |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 253 | } else { |
| 254 | if (_idx > 0) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 255 | var = _sumSqr / (double)_idx; |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 256 | return 0; |
| 257 | } else { |
| 258 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 259 | } |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 260 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 261 | } |
| 262 | |
brandtr | 6607d84 | 2017-02-11 00:24:10 -0800 | [diff] [blame] | 263 | } // namespace test |
| 264 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 265 | bool FixedPayloadTypeCodec(const char* payloadName) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 266 | 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.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 269 | |
| 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 273 | } |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 274 | } |
| 275 | return false; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 276 | } |
| 277 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 278 | void VADCallback::Reset() { |
henrik.lundin@webrtc.org | e9217b4 | 2015-03-06 07:50:34 +0000 | [diff] [blame] | 279 | memset(_numFrameTypes, 0, sizeof(_numFrameTypes)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 280 | } |
| 281 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 282 | VADCallback::VADCallback() { |
henrik.lundin@webrtc.org | e9217b4 | 2015-03-06 07:50:34 +0000 | [diff] [blame] | 283 | memset(_numFrameTypes, 0, sizeof(_numFrameTypes)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 284 | } |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 285 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 286 | void VADCallback::PrintFrameTypes() { |
pbos | 22993e1 | 2015-10-19 02:39:06 -0700 | [diff] [blame] | 287 | printf("kEmptyFrame......... %d\n", _numFrameTypes[kEmptyFrame]); |
henrik.lundin@webrtc.org | e9217b4 | 2015-03-06 07:50:34 +0000 | [diff] [blame] | 288 | 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.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 292 | } |
| 293 | |
henrik.lundin@webrtc.org | e9217b4 | 2015-03-06 07:50:34 +0000 | [diff] [blame] | 294 | int32_t VADCallback::InFrameType(FrameType frame_type) { |
| 295 | _numFrameTypes[frame_type]++; |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | } // namespace webrtc |