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 | /* |
| 12 | * This file contains the functionality for automatic buffer level optimization. |
| 13 | */ |
| 14 | |
| 15 | #ifndef AUTOMODE_H |
| 16 | #define AUTOMODE_H |
| 17 | |
| 18 | #include "typedefs.h" |
| 19 | |
| 20 | /*************/ |
| 21 | /* Constants */ |
| 22 | /*************/ |
| 23 | |
| 24 | /* The beta parameter defines the trade-off between delay and underrun probability. */ |
| 25 | /* It is defined through its inverse in Q30 */ |
| 26 | #define AUTOMODE_BETA_INV_Q30 53687091 /* 1/20 in Q30 */ |
| 27 | #define AUTOMODE_STREAMING_BETA_INV_Q30 536871 /* 1/2000 in Q30 */ |
| 28 | |
| 29 | /* Forgetting factor for the inter-arrival time statistics */ |
| 30 | #define IAT_PROB_FACT 32745 /* 0.9993 in Q15 */ |
| 31 | |
| 32 | /* Maximum inter-arrival time to register (in "packet-times") */ |
| 33 | #define MAX_IAT 64 |
| 34 | #define PEAK_HEIGHT 20 /* 0.08s in Q8 */ |
| 35 | |
| 36 | /* The value (1<<5) sets maximum accelerate "speed" to about 100 ms/s */ |
| 37 | #define AUTOMODE_TIMESCALE_LIMIT (1<<5) |
| 38 | |
| 39 | /* Peak mode related parameters */ |
| 40 | /* Number of peaks in peak vector; must be a power of 2 */ |
| 41 | #define NUM_PEAKS 8 |
| 42 | |
| 43 | /* Must be NUM_PEAKS-1 */ |
| 44 | #define PEAK_INDEX_MASK 0x0007 |
| 45 | |
| 46 | /* Longest accepted peak distance */ |
| 47 | #define MAX_PEAK_PERIOD 10 |
| 48 | #define MAX_STREAMING_PEAK_PERIOD 600 /* 10 minutes */ |
| 49 | |
| 50 | /* Number of peaks required before peak mode can be engaged */ |
| 51 | #define NUM_PEAKS_REQUIRED 3 |
| 52 | |
| 53 | /* Drift term for cumulative sum */ |
| 54 | #define CSUM_IAT_DRIFT 2 |
| 55 | |
| 56 | /*******************/ |
| 57 | /* Automode struct */ |
| 58 | /*******************/ |
| 59 | |
| 60 | /* The automode struct is a sub-struct of the |
| 61 | bufstats-struct (BufstatsInst_t). */ |
| 62 | |
| 63 | typedef struct |
| 64 | { |
| 65 | |
| 66 | /* Filtered current buffer level */ |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 67 | uint16_t levelFiltFact; /* filter forgetting factor in Q8 */ |
turaj@webrtc.org | 6388c3e | 2013-02-12 21:42:18 +0000 | [diff] [blame] | 68 | int buffLevelFilt; /* filtered buffer level in Q8 */ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 69 | |
| 70 | /* Inter-arrival time (iat) statistics */ |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 71 | int32_t iatProb[MAX_IAT + 1]; /* iat probabilities in Q30 */ |
| 72 | int16_t iatProbFact; /* iat forgetting factor in Q15 */ |
| 73 | uint32_t packetIatCountSamp; /* time (in timestamps) elapsed since last |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 74 | packet arrival, based on RecOut calls */ |
turaj@webrtc.org | 6388c3e | 2013-02-12 21:42:18 +0000 | [diff] [blame] | 75 | int optBufLevel; /* current optimal buffer level in Q8 */ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 76 | |
| 77 | /* Packet related information */ |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 78 | int16_t packetSpeechLenSamp; /* speech samples per incoming packet */ |
| 79 | int16_t lastPackCNGorDTMF; /* indicates that the last received packet |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 80 | contained special information */ |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 81 | uint16_t lastSeqNo; /* sequence number for last packet received */ |
| 82 | uint32_t lastTimeStamp; /* timestamp for the last packet received */ |
| 83 | int32_t sampleMemory; /* memory position for keeping track of how many |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 84 | samples we cut during expand */ |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 85 | int16_t prevTimeScale; /* indicates that the last mode was an accelerate |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 86 | or pre-emptive expand operation */ |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 87 | uint32_t timescaleHoldOff; /* counter that is shifted one step right each |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 88 | RecOut call; time-scaling allowed when it has |
| 89 | reached 0 */ |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 90 | int16_t extraDelayMs; /* extra delay for sync with video */ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 91 | |
| 92 | /* Peak-detection */ |
| 93 | /* vector with the latest peak periods (peak spacing in samples) */ |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 94 | uint32_t peakPeriodSamp[NUM_PEAKS]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 95 | /* vector with the latest peak heights (in packets) */ |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 96 | int16_t peakHeightPkt[NUM_PEAKS]; |
| 97 | int16_t peakIndex; /* index for the vectors peakPeriodSamp and peakHeightPkt; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 98 | -1 if still waiting for first peak */ |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 99 | uint16_t peakThresholdPkt; /* definition of peak (in packets); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 100 | calculated from PEAK_HEIGHT */ |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 101 | uint32_t peakIatCountSamp; /* samples elapsed since last peak was observed */ |
| 102 | uint32_t curPeakPeriod; /* current maximum of peakPeriodSamp vector */ |
| 103 | int16_t curPeakHeight; /* derived from peakHeightPkt vector; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 104 | used as optimal buffer level in peak mode */ |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 105 | int16_t peakModeDisabled; /* ==0 if peak mode can be engaged; >0 if not */ |
henrik.lundin@webrtc.org | d439870 | 2012-01-04 13:09:55 +0000 | [diff] [blame] | 106 | uint16_t peakFound; /* 1 if peaks are detected and extra delay is applied; |
| 107 | * 0 otherwise. */ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 108 | |
| 109 | /* Post-call statistics */ |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 110 | uint32_t countIAT500ms; /* number of times we got small network outage */ |
| 111 | uint32_t countIAT1000ms; /* number of times we got medium network outage */ |
| 112 | uint32_t countIAT2000ms; /* number of times we got large network outage */ |
| 113 | uint32_t longestIATms; /* mSec duration of longest network outage */ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 114 | |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 115 | int16_t cSumIatQ8; /* cumulative sum of inter-arrival times */ |
| 116 | int16_t maxCSumIatQ8; /* max cumulative sum IAT */ |
| 117 | uint32_t maxCSumUpdateTimer;/* time elapsed since maximum was observed */ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 118 | |
| 119 | } AutomodeInst_t; |
| 120 | |
| 121 | /*************/ |
| 122 | /* Functions */ |
| 123 | /*************/ |
| 124 | |
| 125 | /**************************************************************************** |
| 126 | * WebRtcNetEQ_UpdateIatStatistics(...) |
| 127 | * |
| 128 | * Update the packet inter-arrival time statistics when a new packet arrives. |
| 129 | * This function should be called for every arriving packet, with some |
| 130 | * exceptions when using DTX/VAD and DTMF. A new optimal buffer level is |
| 131 | * calculated after the update. |
| 132 | * |
| 133 | * Input: |
| 134 | * - inst : Automode instance |
| 135 | * - maxBufLen : Maximum number of packets the buffer can hold |
| 136 | * - seqNumber : RTP sequence number of incoming packet |
| 137 | * - timeStamp : RTP timestamp of incoming packet |
| 138 | * - fsHz : Sample rate in Hz |
| 139 | * - mdCodec : Non-zero if the current codec is a multiple- |
| 140 | * description codec |
| 141 | * - streamingMode : A non-zero value will increase jitter robustness (and delay) |
| 142 | * |
| 143 | * Output: |
| 144 | * - inst : Updated automode instance |
| 145 | * |
| 146 | * Return value : 0 - Ok |
| 147 | * <0 - Error |
| 148 | */ |
| 149 | |
| 150 | int WebRtcNetEQ_UpdateIatStatistics(AutomodeInst_t *inst, int maxBufLen, |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 151 | uint16_t seqNumber, uint32_t timeStamp, |
| 152 | int32_t fsHz, int mdCodec, int streamingMode); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 153 | |
| 154 | /**************************************************************************** |
| 155 | * WebRtcNetEQ_CalcOptimalBufLvl(...) |
| 156 | * |
| 157 | * Calculate the optimal buffer level based on packet inter-arrival time |
| 158 | * statistics. |
| 159 | * |
| 160 | * Input: |
| 161 | * - inst : Automode instance |
| 162 | * - fsHz : Sample rate in Hz |
| 163 | * - mdCodec : Non-zero if the current codec is a multiple- |
| 164 | * description codec |
| 165 | * - timeIatPkts : Currently observed inter-arrival time in packets |
| 166 | * - streamingMode : A non-zero value will increase jitter robustness (and delay) |
| 167 | * |
| 168 | * Output: |
| 169 | * - inst : Updated automode instance |
| 170 | * |
| 171 | * Return value : >0 - Optimal buffer level |
| 172 | * <0 - Error |
| 173 | */ |
| 174 | |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 175 | int16_t WebRtcNetEQ_CalcOptimalBufLvl(AutomodeInst_t *inst, int32_t fsHz, |
| 176 | int mdCodec, uint32_t timeIatPkts, |
| 177 | int streamingMode); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 178 | |
| 179 | /**************************************************************************** |
| 180 | * WebRtcNetEQ_BufferLevelFilter(...) |
| 181 | * |
| 182 | * Update filtered buffer level. The function must be called once for each |
| 183 | * RecOut call, since the timing of automode hinges on counters that are |
| 184 | * updated by this function. |
| 185 | * |
| 186 | * Input: |
| 187 | * - curSizeMs8 : Total length of unused speech data in packet buffer |
| 188 | * and sync buffer, in ms * 8 |
| 189 | * - inst : Automode instance |
| 190 | * - sampPerCall : Number of samples per RecOut call |
| 191 | * - fsMult : Sample rate in Hz divided by 8000 |
| 192 | * |
| 193 | * Output: |
| 194 | * - inst : Updated automode instance |
| 195 | * |
| 196 | * Return value : 0 - Ok |
| 197 | * : <0 - Error |
| 198 | */ |
| 199 | |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 200 | int WebRtcNetEQ_BufferLevelFilter(int32_t curSizeMs8, AutomodeInst_t *inst, |
| 201 | int sampPerCall, int16_t fsMult); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 202 | |
| 203 | /**************************************************************************** |
| 204 | * WebRtcNetEQ_SetPacketSpeechLen(...) |
| 205 | * |
| 206 | * Provide the number of speech samples extracted from a packet to the |
| 207 | * automode instance. Several of the calculations within automode depend |
| 208 | * on knowing the packet size. |
| 209 | * |
| 210 | * |
| 211 | * Input: |
| 212 | * - inst : Automode instance |
| 213 | * - newLenSamp : Number of samples per RecOut call |
| 214 | * - fsHz : Sample rate in Hz |
| 215 | * |
| 216 | * Output: |
| 217 | * - inst : Updated automode instance |
| 218 | * |
| 219 | * Return value : 0 - Ok |
| 220 | * <0 - Error |
| 221 | */ |
| 222 | |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 223 | int WebRtcNetEQ_SetPacketSpeechLen(AutomodeInst_t *inst, int16_t newLenSamp, |
| 224 | int32_t fsHz); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 225 | |
| 226 | /**************************************************************************** |
| 227 | * WebRtcNetEQ_ResetAutomode(...) |
| 228 | * |
| 229 | * Reset the automode instance. |
| 230 | * |
| 231 | * |
| 232 | * Input: |
| 233 | * - inst : Automode instance |
| 234 | * - maxBufLenPackets : Maximum number of packets that the packet |
| 235 | * buffer can hold (>1) |
| 236 | * |
| 237 | * Output: |
| 238 | * - inst : Updated automode instance |
| 239 | * |
| 240 | * Return value : 0 - Ok |
| 241 | */ |
| 242 | |
| 243 | int WebRtcNetEQ_ResetAutomode(AutomodeInst_t *inst, int maxBufLenPackets); |
| 244 | |
henrik.lundin@webrtc.org | d439870 | 2012-01-04 13:09:55 +0000 | [diff] [blame] | 245 | /**************************************************************************** |
| 246 | * WebRtcNetEQ_AverageIAT(...) |
| 247 | * |
| 248 | * Calculate the average inter-arrival time based on current statistics. |
| 249 | * The average is expressed in parts per million relative the nominal. That is, |
| 250 | * if the average inter-arrival time is equal to the nominal frame time, |
| 251 | * the return value is zero. A positive value corresponds to packet spacing |
| 252 | * being too large, while a negative value means that the packets arrive with |
| 253 | * less spacing than expected. |
| 254 | * |
| 255 | * |
| 256 | * Input: |
| 257 | * - inst : Automode instance. |
| 258 | * |
| 259 | * Return value : Average relative inter-arrival time in samples. |
| 260 | */ |
| 261 | |
| 262 | int32_t WebRtcNetEQ_AverageIAT(const AutomodeInst_t *inst); |
| 263 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 264 | #endif /* AUTOMODE_H */ |