andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [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" |
| 12 | |
| 13 | #include <assert.h> |
| 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
henrik.lundin@webrtc.org | dd787cc | 2015-03-06 07:50:34 | [diff] [blame] | 16 | #include <string.h> |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 17 | |
tina.legrand@webrtc.org | cf9ab12 | 2013-03-15 13:29:17 | [diff] [blame] | 18 | #include "webrtc/common_types.h" |
kwiberg | 36a2479 | 2016-10-01 05:29:43 | [diff] [blame] | 19 | #include "webrtc/modules/audio_coding/include/audio_coding_module.h" |
| 20 | #include "webrtc/test/gtest.h" |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 21 | |
| 22 | #define NUM_CODECS_WITH_FIXED_PAYLOAD_TYPE 13 |
| 23 | |
| 24 | namespace webrtc { |
| 25 | |
tina.legrand@webrtc.org | b31332e | 2013-05-03 07:34:12 | [diff] [blame] | 26 | ACMTestTimer::ACMTestTimer() |
| 27 | : _msec(0), |
| 28 | _sec(0), |
| 29 | _min(0), |
| 30 | _hour(0) { |
| 31 | return; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 32 | } |
| 33 | |
tina.legrand@webrtc.org | b31332e | 2013-05-03 07:34:12 | [diff] [blame] | 34 | ACMTestTimer::~ACMTestTimer() { |
| 35 | return; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 36 | } |
| 37 | |
tina.legrand@webrtc.org | b31332e | 2013-05-03 07:34:12 | [diff] [blame] | 38 | void ACMTestTimer::Reset() { |
| 39 | _msec = 0; |
| 40 | _sec = 0; |
| 41 | _min = 0; |
| 42 | _hour = 0; |
| 43 | return; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 44 | } |
tina.legrand@webrtc.org | b31332e | 2013-05-03 07:34:12 | [diff] [blame] | 45 | void ACMTestTimer::Tick10ms() { |
| 46 | _msec += 10; |
| 47 | Adjust(); |
| 48 | return; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 49 | } |
| 50 | |
tina.legrand@webrtc.org | b31332e | 2013-05-03 07:34:12 | [diff] [blame] | 51 | void ACMTestTimer::Tick1ms() { |
| 52 | _msec++; |
| 53 | Adjust(); |
| 54 | return; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 55 | } |
| 56 | |
tina.legrand@webrtc.org | b31332e | 2013-05-03 07:34:12 | [diff] [blame] | 57 | void ACMTestTimer::Tick100ms() { |
| 58 | _msec += 100; |
| 59 | Adjust(); |
| 60 | return; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 61 | } |
| 62 | |
tina.legrand@webrtc.org | b31332e | 2013-05-03 07:34:12 | [diff] [blame] | 63 | void ACMTestTimer::Tick1sec() { |
| 64 | _sec++; |
| 65 | Adjust(); |
| 66 | return; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 67 | } |
| 68 | |
tina.legrand@webrtc.org | b31332e | 2013-05-03 07:34:12 | [diff] [blame] | 69 | void ACMTestTimer::CurrentTimeHMS(char* currTime) { |
| 70 | sprintf(currTime, "%4lu:%02u:%06.3f", _hour, _min, |
| 71 | (double) _sec + (double) _msec / 1000.); |
| 72 | return; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 73 | } |
| 74 | |
tina.legrand@webrtc.org | b31332e | 2013-05-03 07:34:12 | [diff] [blame] | 75 | void ACMTestTimer::CurrentTime(unsigned long& h, unsigned char& m, |
| 76 | unsigned char& s, unsigned short& ms) { |
| 77 | h = _hour; |
| 78 | m = _min; |
| 79 | s = _sec; |
| 80 | ms = _msec; |
| 81 | return; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 82 | } |
| 83 | |
tina.legrand@webrtc.org | b31332e | 2013-05-03 07:34:12 | [diff] [blame] | 84 | void ACMTestTimer::Adjust() { |
| 85 | unsigned int n; |
| 86 | if (_msec >= 1000) { |
| 87 | n = _msec / 1000; |
| 88 | _msec -= (1000 * n); |
| 89 | _sec += n; |
| 90 | } |
| 91 | if (_sec >= 60) { |
| 92 | n = _sec / 60; |
| 93 | _sec -= (n * 60); |
| 94 | _min += n; |
| 95 | } |
| 96 | if (_min >= 60) { |
| 97 | n = _min / 60; |
| 98 | _min -= (n * 60); |
| 99 | _hour += n; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | int16_t ChooseCodec(CodecInst& codecInst) { |
| 104 | |
| 105 | PrintCodecs(); |
| 106 | //AudioCodingModule* tmpACM = AudioCodingModule::Create(0); |
| 107 | uint8_t noCodec = AudioCodingModule::NumberOfCodecs(); |
| 108 | int8_t codecID; |
| 109 | bool outOfRange = false; |
| 110 | char myStr[15] = ""; |
| 111 | do { |
| 112 | printf("\nChoose a codec [0]: "); |
| 113 | EXPECT_TRUE(fgets(myStr, 10, stdin) != NULL); |
| 114 | codecID = atoi(myStr); |
| 115 | if ((codecID < 0) || (codecID >= noCodec)) { |
| 116 | printf("\nOut of range.\n"); |
| 117 | outOfRange = true; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 118 | } |
tina.legrand@webrtc.org | b31332e | 2013-05-03 07:34:12 | [diff] [blame] | 119 | } while (outOfRange); |
| 120 | |
| 121 | CHECK_ERROR(AudioCodingModule::Codec((uint8_t )codecID, &codecInst)); |
| 122 | return 0; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 123 | } |
| 124 | |
tina.legrand@webrtc.org | b31332e | 2013-05-03 07:34:12 | [diff] [blame] | 125 | void PrintCodecs() { |
| 126 | uint8_t noCodec = AudioCodingModule::NumberOfCodecs(); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 127 | |
tina.legrand@webrtc.org | b31332e | 2013-05-03 07:34:12 | [diff] [blame] | 128 | CodecInst codecInst; |
| 129 | printf("No Name [Hz] [bps]\n"); |
| 130 | for (uint8_t codecCntr = 0; codecCntr < noCodec; codecCntr++) { |
| 131 | AudioCodingModule::Codec(codecCntr, &codecInst); |
| 132 | printf("%2d- %-18s %5d %6d\n", codecCntr, codecInst.plname, |
| 133 | codecInst.plfreq, codecInst.rate); |
| 134 | } |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 135 | |
tina.legrand@webrtc.org | b31332e | 2013-05-03 07:34:12 | [diff] [blame] | 136 | } |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 137 | |
brandtr | 576a9b3 | 2017-02-11 08:24:10 | [diff] [blame] | 138 | namespace test { |
| 139 | |
tina.legrand@webrtc.org | b31332e | 2013-05-03 07:34:12 | [diff] [blame] | 140 | CircularBuffer::CircularBuffer(uint32_t len) |
| 141 | : _buff(NULL), |
| 142 | _idx(0), |
| 143 | _buffIsFull(false), |
| 144 | _calcAvg(false), |
| 145 | _calcVar(false), |
| 146 | _sum(0), |
| 147 | _sumSqr(0) { |
| 148 | _buff = new double[len]; |
| 149 | if (_buff == NULL) { |
| 150 | _buffLen = 0; |
| 151 | } else { |
| 152 | for (uint32_t n = 0; n < len; n++) { |
| 153 | _buff[n] = 0; |
| 154 | } |
| 155 | _buffLen = len; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | CircularBuffer::~CircularBuffer() { |
| 160 | if (_buff != NULL) { |
| 161 | delete[] _buff; |
| 162 | _buff = NULL; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | void CircularBuffer::Update(const double newVal) { |
| 167 | assert(_buffLen > 0); |
| 168 | |
| 169 | // store the value that is going to be overwritten |
| 170 | double oldVal = _buff[_idx]; |
| 171 | // record the new value |
| 172 | _buff[_idx] = newVal; |
| 173 | // increment the index, to point to where we would |
| 174 | // write next |
| 175 | _idx++; |
| 176 | // it is a circular buffer, if we are at the end |
| 177 | // we have to cycle to the beginning |
| 178 | if (_idx >= _buffLen) { |
| 179 | // flag that the buffer is filled up. |
| 180 | _buffIsFull = true; |
| 181 | _idx = 0; |
| 182 | } |
| 183 | |
| 184 | // Update |
| 185 | |
| 186 | if (_calcAvg) { |
| 187 | // for the average we have to update |
| 188 | // the sum |
| 189 | _sum += (newVal - oldVal); |
| 190 | } |
| 191 | |
| 192 | if (_calcVar) { |
| 193 | // to calculate variance we have to update |
| 194 | // the sum of squares |
| 195 | _sumSqr += (double) (newVal - oldVal) * (double) (newVal + oldVal); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | void CircularBuffer::SetArithMean(bool enable) { |
| 200 | assert(_buffLen > 0); |
| 201 | |
| 202 | if (enable && !_calcAvg) { |
| 203 | uint32_t lim; |
| 204 | if (_buffIsFull) { |
| 205 | lim = _buffLen; |
| 206 | } else { |
| 207 | lim = _idx; |
| 208 | } |
| 209 | _sum = 0; |
| 210 | for (uint32_t n = 0; n < lim; n++) { |
| 211 | _sum += _buff[n]; |
| 212 | } |
| 213 | } |
| 214 | _calcAvg = enable; |
| 215 | } |
| 216 | |
| 217 | void CircularBuffer::SetVariance(bool enable) { |
| 218 | assert(_buffLen > 0); |
| 219 | |
| 220 | if (enable && !_calcVar) { |
| 221 | uint32_t lim; |
| 222 | if (_buffIsFull) { |
| 223 | lim = _buffLen; |
| 224 | } else { |
| 225 | lim = _idx; |
| 226 | } |
| 227 | _sumSqr = 0; |
| 228 | for (uint32_t n = 0; n < lim; n++) { |
| 229 | _sumSqr += _buff[n] * _buff[n]; |
| 230 | } |
| 231 | } |
| 232 | _calcAvg = enable; |
| 233 | } |
| 234 | |
| 235 | int16_t CircularBuffer::ArithMean(double& mean) { |
| 236 | assert(_buffLen > 0); |
| 237 | |
| 238 | if (_buffIsFull) { |
| 239 | |
| 240 | mean = _sum / (double) _buffLen; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 241 | return 0; |
tina.legrand@webrtc.org | b31332e | 2013-05-03 07:34:12 | [diff] [blame] | 242 | } else { |
| 243 | if (_idx > 0) { |
| 244 | mean = _sum / (double) _idx; |
| 245 | return 0; |
| 246 | } else { |
| 247 | return -1; |
| 248 | } |
| 249 | |
| 250 | } |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 251 | } |
| 252 | |
tina.legrand@webrtc.org | b31332e | 2013-05-03 07:34:12 | [diff] [blame] | 253 | int16_t CircularBuffer::Variance(double& var) { |
| 254 | assert(_buffLen > 0); |
pbos@webrtc.org | fbda0fc | 2013-04-09 00:28:06 | [diff] [blame] | 255 | |
tina.legrand@webrtc.org | b31332e | 2013-05-03 07:34:12 | [diff] [blame] | 256 | if (_buffIsFull) { |
| 257 | var = _sumSqr / (double) _buffLen; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 258 | return 0; |
tina.legrand@webrtc.org | b31332e | 2013-05-03 07:34:12 | [diff] [blame] | 259 | } else { |
| 260 | if (_idx > 0) { |
| 261 | var = _sumSqr / (double) _idx; |
| 262 | return 0; |
| 263 | } else { |
| 264 | return -1; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 265 | } |
tina.legrand@webrtc.org | b31332e | 2013-05-03 07:34:12 | [diff] [blame] | 266 | } |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 267 | } |
| 268 | |
brandtr | 576a9b3 | 2017-02-11 08:24:10 | [diff] [blame] | 269 | } // namespace test |
| 270 | |
tina.legrand@webrtc.org | b31332e | 2013-05-03 07:34:12 | [diff] [blame] | 271 | bool FixedPayloadTypeCodec(const char* payloadName) { |
| 272 | char fixPayloadTypeCodecs[NUM_CODECS_WITH_FIXED_PAYLOAD_TYPE][32] = { "PCMU", |
| 273 | "PCMA", "GSM", "G723", "DVI4", "LPC", "PCMA", "G722", "QCELP", "CN", |
| 274 | "MPA", "G728", "G729" }; |
| 275 | |
| 276 | for (int n = 0; n < NUM_CODECS_WITH_FIXED_PAYLOAD_TYPE; n++) { |
| 277 | if (!STR_CASE_CMP(payloadName, fixPayloadTypeCodecs[n])) { |
| 278 | return true; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 279 | } |
tina.legrand@webrtc.org | b31332e | 2013-05-03 07:34:12 | [diff] [blame] | 280 | } |
| 281 | return false; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 282 | } |
| 283 | |
tina.legrand@webrtc.org | b31332e | 2013-05-03 07:34:12 | [diff] [blame] | 284 | void VADCallback::Reset() { |
henrik.lundin@webrtc.org | dd787cc | 2015-03-06 07:50:34 | [diff] [blame] | 285 | memset(_numFrameTypes, 0, sizeof(_numFrameTypes)); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 286 | } |
| 287 | |
tina.legrand@webrtc.org | b31332e | 2013-05-03 07:34:12 | [diff] [blame] | 288 | VADCallback::VADCallback() { |
henrik.lundin@webrtc.org | dd787cc | 2015-03-06 07:50:34 | [diff] [blame] | 289 | memset(_numFrameTypes, 0, sizeof(_numFrameTypes)); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 290 | } |
| 291 | |
tina.legrand@webrtc.org | b31332e | 2013-05-03 07:34:12 | [diff] [blame] | 292 | void VADCallback::PrintFrameTypes() { |
pbos | b63d8ad | 2015-10-19 09:39:06 | [diff] [blame] | 293 | printf("kEmptyFrame......... %d\n", _numFrameTypes[kEmptyFrame]); |
henrik.lundin@webrtc.org | dd787cc | 2015-03-06 07:50:34 | [diff] [blame] | 294 | printf("kAudioFrameSpeech... %d\n", _numFrameTypes[kAudioFrameSpeech]); |
| 295 | printf("kAudioFrameCN....... %d\n", _numFrameTypes[kAudioFrameCN]); |
| 296 | printf("kVideoFrameKey...... %d\n", _numFrameTypes[kVideoFrameKey]); |
| 297 | printf("kVideoFrameDelta.... %d\n", _numFrameTypes[kVideoFrameDelta]); |
tina.legrand@webrtc.org | b31332e | 2013-05-03 07:34:12 | [diff] [blame] | 298 | } |
| 299 | |
henrik.lundin@webrtc.org | dd787cc | 2015-03-06 07:50:34 | [diff] [blame] | 300 | int32_t VADCallback::InFrameType(FrameType frame_type) { |
| 301 | _numFrameTypes[frame_type]++; |
tina.legrand@webrtc.org | b31332e | 2013-05-03 07:34:12 | [diff] [blame] | 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | } // namespace webrtc |