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 | |
tina.legrand@webrtc.org | 73222cf | 2013-03-15 13:29:17 +0000 | [diff] [blame] | 18 | #include "webrtc/common_types.h" |
kjellander | 3e6db23 | 2015-11-26 04:44:54 -0800 | [diff] [blame] | 19 | #include "webrtc/modules/audio_coding/acm2/acm_common_defs.h" |
kwiberg | ac9f876 | 2016-09-30 22:29:43 -0700 | [diff] [blame] | 20 | #include "webrtc/modules/audio_coding/include/audio_coding_module.h" |
| 21 | #include "webrtc/test/gtest.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 22 | |
| 23 | #define NUM_CODECS_WITH_FIXED_PAYLOAD_TYPE 13 |
| 24 | |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 25 | namespace webrtc { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 26 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 27 | ACMTestTimer::ACMTestTimer() |
| 28 | : _msec(0), |
| 29 | _sec(0), |
| 30 | _min(0), |
| 31 | _hour(0) { |
| 32 | return; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 33 | } |
| 34 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 35 | ACMTestTimer::~ACMTestTimer() { |
| 36 | return; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 37 | } |
| 38 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 39 | void ACMTestTimer::Reset() { |
| 40 | _msec = 0; |
| 41 | _sec = 0; |
| 42 | _min = 0; |
| 43 | _hour = 0; |
| 44 | return; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 45 | } |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 46 | void ACMTestTimer::Tick10ms() { |
| 47 | _msec += 10; |
| 48 | Adjust(); |
| 49 | return; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 50 | } |
| 51 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 52 | void ACMTestTimer::Tick1ms() { |
| 53 | _msec++; |
| 54 | Adjust(); |
| 55 | return; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 56 | } |
| 57 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 58 | void ACMTestTimer::Tick100ms() { |
| 59 | _msec += 100; |
| 60 | Adjust(); |
| 61 | return; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 62 | } |
| 63 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 64 | void ACMTestTimer::Tick1sec() { |
| 65 | _sec++; |
| 66 | Adjust(); |
| 67 | return; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 68 | } |
| 69 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 70 | void ACMTestTimer::CurrentTimeHMS(char* currTime) { |
| 71 | sprintf(currTime, "%4lu:%02u:%06.3f", _hour, _min, |
| 72 | (double) _sec + (double) _msec / 1000.); |
| 73 | return; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 74 | } |
| 75 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 76 | void 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 83 | } |
| 84 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 85 | void 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 | |
| 104 | int16_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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 119 | } |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 120 | } while (outOfRange); |
| 121 | |
| 122 | CHECK_ERROR(AudioCodingModule::Codec((uint8_t )codecID, &codecInst)); |
| 123 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 124 | } |
| 125 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 126 | void PrintCodecs() { |
| 127 | uint8_t noCodec = AudioCodingModule::NumberOfCodecs(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 128 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 129 | 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 136 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 137 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 138 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 139 | CircularBuffer::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 | |
| 158 | CircularBuffer::~CircularBuffer() { |
| 159 | if (_buff != NULL) { |
| 160 | delete[] _buff; |
| 161 | _buff = NULL; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | void 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 | |
| 198 | void 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 | |
| 216 | void 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 | |
| 234 | int16_t CircularBuffer::ArithMean(double& mean) { |
| 235 | assert(_buffLen > 0); |
| 236 | |
| 237 | if (_buffIsFull) { |
| 238 | |
| 239 | mean = _sum / (double) _buffLen; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 240 | return 0; |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 241 | } else { |
| 242 | if (_idx > 0) { |
| 243 | mean = _sum / (double) _idx; |
| 244 | return 0; |
| 245 | } else { |
| 246 | return -1; |
| 247 | } |
| 248 | |
| 249 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 250 | } |
| 251 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 252 | int16_t CircularBuffer::Variance(double& var) { |
| 253 | assert(_buffLen > 0); |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 254 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 255 | if (_buffIsFull) { |
| 256 | var = _sumSqr / (double) _buffLen; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 257 | return 0; |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 258 | } else { |
| 259 | if (_idx > 0) { |
| 260 | var = _sumSqr / (double) _idx; |
| 261 | return 0; |
| 262 | } else { |
| 263 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 264 | } |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 265 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 266 | } |
| 267 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 268 | bool 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 276 | } |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 277 | } |
| 278 | return false; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 279 | } |
| 280 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 281 | void VADCallback::Reset() { |
henrik.lundin@webrtc.org | e9217b4 | 2015-03-06 07:50:34 +0000 | [diff] [blame] | 282 | memset(_numFrameTypes, 0, sizeof(_numFrameTypes)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 283 | } |
| 284 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 285 | VADCallback::VADCallback() { |
henrik.lundin@webrtc.org | e9217b4 | 2015-03-06 07:50:34 +0000 | [diff] [blame] | 286 | memset(_numFrameTypes, 0, sizeof(_numFrameTypes)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 287 | } |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 288 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 289 | void VADCallback::PrintFrameTypes() { |
pbos | 22993e1 | 2015-10-19 02:39:06 -0700 | [diff] [blame] | 290 | printf("kEmptyFrame......... %d\n", _numFrameTypes[kEmptyFrame]); |
henrik.lundin@webrtc.org | e9217b4 | 2015-03-06 07:50:34 +0000 | [diff] [blame] | 291 | 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.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 295 | } |
| 296 | |
henrik.lundin@webrtc.org | e9217b4 | 2015-03-06 07:50:34 +0000 | [diff] [blame] | 297 | int32_t VADCallback::InFrameType(FrameType frame_type) { |
| 298 | _numFrameTypes[frame_type]++; |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | } // namespace webrtc |