blob: 49878c08a32e429a8f4177010cbeb6387b18314e [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/*
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
63typedef struct
64{
65
66 /* Filtered current buffer level */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000067 uint16_t levelFiltFact; /* filter forgetting factor in Q8 */
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000068 int buffLevelFilt; /* filtered buffer level in Q8 */
niklase@google.com470e71d2011-07-07 08:21:25 +000069
70 /* Inter-arrival time (iat) statistics */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000071 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.com470e71d2011-07-07 08:21:25 +000074 packet arrival, based on RecOut calls */
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000075 int optBufLevel; /* current optimal buffer level in Q8 */
niklase@google.com470e71d2011-07-07 08:21:25 +000076
77 /* Packet related information */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000078 int16_t packetSpeechLenSamp; /* speech samples per incoming packet */
79 int16_t lastPackCNGorDTMF; /* indicates that the last received packet
niklase@google.com470e71d2011-07-07 08:21:25 +000080 contained special information */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000081 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.com470e71d2011-07-07 08:21:25 +000084 samples we cut during expand */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000085 int16_t prevTimeScale; /* indicates that the last mode was an accelerate
niklase@google.com470e71d2011-07-07 08:21:25 +000086 or pre-emptive expand operation */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000087 uint32_t timescaleHoldOff; /* counter that is shifted one step right each
niklase@google.com470e71d2011-07-07 08:21:25 +000088 RecOut call; time-scaling allowed when it has
89 reached 0 */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000090 int16_t extraDelayMs; /* extra delay for sync with video */
niklase@google.com470e71d2011-07-07 08:21:25 +000091
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +000092 int minimum_delay_ms; /* Desired delay, NetEq maintains this amount of
93 delay unless jitter statistics suggests a higher value. */
94 int required_delay_q8; /* Smallest delay required. This is computed
95 according to inter-arrival time and playout mode. It has the same unit
96 as |optBufLevel|. */
97
niklase@google.com470e71d2011-07-07 08:21:25 +000098 /* Peak-detection */
99 /* vector with the latest peak periods (peak spacing in samples) */
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000100 uint32_t peakPeriodSamp[NUM_PEAKS];
niklase@google.com470e71d2011-07-07 08:21:25 +0000101 /* vector with the latest peak heights (in packets) */
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000102 int16_t peakHeightPkt[NUM_PEAKS];
103 int16_t peakIndex; /* index for the vectors peakPeriodSamp and peakHeightPkt;
niklase@google.com470e71d2011-07-07 08:21:25 +0000104 -1 if still waiting for first peak */
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000105 uint16_t peakThresholdPkt; /* definition of peak (in packets);
niklase@google.com470e71d2011-07-07 08:21:25 +0000106 calculated from PEAK_HEIGHT */
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000107 uint32_t peakIatCountSamp; /* samples elapsed since last peak was observed */
108 uint32_t curPeakPeriod; /* current maximum of peakPeriodSamp vector */
109 int16_t curPeakHeight; /* derived from peakHeightPkt vector;
niklase@google.com470e71d2011-07-07 08:21:25 +0000110 used as optimal buffer level in peak mode */
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000111 int16_t peakModeDisabled; /* ==0 if peak mode can be engaged; >0 if not */
henrik.lundin@webrtc.orgd4398702012-01-04 13:09:55 +0000112 uint16_t peakFound; /* 1 if peaks are detected and extra delay is applied;
113 * 0 otherwise. */
niklase@google.com470e71d2011-07-07 08:21:25 +0000114
115 /* Post-call statistics */
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000116 uint32_t countIAT500ms; /* number of times we got small network outage */
117 uint32_t countIAT1000ms; /* number of times we got medium network outage */
118 uint32_t countIAT2000ms; /* number of times we got large network outage */
119 uint32_t longestIATms; /* mSec duration of longest network outage */
niklase@google.com470e71d2011-07-07 08:21:25 +0000120
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000121 int16_t cSumIatQ8; /* cumulative sum of inter-arrival times */
122 int16_t maxCSumIatQ8; /* max cumulative sum IAT */
123 uint32_t maxCSumUpdateTimer;/* time elapsed since maximum was observed */
niklase@google.com470e71d2011-07-07 08:21:25 +0000124
125} AutomodeInst_t;
126
127/*************/
128/* Functions */
129/*************/
130
131/****************************************************************************
132 * WebRtcNetEQ_UpdateIatStatistics(...)
133 *
134 * Update the packet inter-arrival time statistics when a new packet arrives.
135 * This function should be called for every arriving packet, with some
136 * exceptions when using DTX/VAD and DTMF. A new optimal buffer level is
137 * calculated after the update.
138 *
139 * Input:
140 * - inst : Automode instance
141 * - maxBufLen : Maximum number of packets the buffer can hold
142 * - seqNumber : RTP sequence number of incoming packet
143 * - timeStamp : RTP timestamp of incoming packet
144 * - fsHz : Sample rate in Hz
145 * - mdCodec : Non-zero if the current codec is a multiple-
146 * description codec
147 * - streamingMode : A non-zero value will increase jitter robustness (and delay)
148 *
149 * Output:
150 * - inst : Updated automode instance
151 *
152 * Return value : 0 - Ok
153 * <0 - Error
154 */
155
156int WebRtcNetEQ_UpdateIatStatistics(AutomodeInst_t *inst, int maxBufLen,
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000157 uint16_t seqNumber, uint32_t timeStamp,
158 int32_t fsHz, int mdCodec, int streamingMode);
niklase@google.com470e71d2011-07-07 08:21:25 +0000159
160/****************************************************************************
161 * WebRtcNetEQ_CalcOptimalBufLvl(...)
162 *
163 * Calculate the optimal buffer level based on packet inter-arrival time
164 * statistics.
165 *
166 * Input:
167 * - inst : Automode instance
168 * - fsHz : Sample rate in Hz
169 * - mdCodec : Non-zero if the current codec is a multiple-
170 * description codec
171 * - timeIatPkts : Currently observed inter-arrival time in packets
172 * - streamingMode : A non-zero value will increase jitter robustness (and delay)
173 *
174 * Output:
175 * - inst : Updated automode instance
176 *
177 * Return value : >0 - Optimal buffer level
178 * <0 - Error
179 */
180
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000181int16_t WebRtcNetEQ_CalcOptimalBufLvl(AutomodeInst_t *inst, int32_t fsHz,
182 int mdCodec, uint32_t timeIatPkts,
183 int streamingMode);
niklase@google.com470e71d2011-07-07 08:21:25 +0000184
185/****************************************************************************
186 * WebRtcNetEQ_BufferLevelFilter(...)
187 *
188 * Update filtered buffer level. The function must be called once for each
189 * RecOut call, since the timing of automode hinges on counters that are
190 * updated by this function.
191 *
192 * Input:
193 * - curSizeMs8 : Total length of unused speech data in packet buffer
194 * and sync buffer, in ms * 8
195 * - inst : Automode instance
196 * - sampPerCall : Number of samples per RecOut call
197 * - fsMult : Sample rate in Hz divided by 8000
198 *
199 * Output:
200 * - inst : Updated automode instance
201 *
202 * Return value : 0 - Ok
203 * : <0 - Error
204 */
205
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000206int WebRtcNetEQ_BufferLevelFilter(int32_t curSizeMs8, AutomodeInst_t *inst,
207 int sampPerCall, int16_t fsMult);
niklase@google.com470e71d2011-07-07 08:21:25 +0000208
209/****************************************************************************
210 * WebRtcNetEQ_SetPacketSpeechLen(...)
211 *
212 * Provide the number of speech samples extracted from a packet to the
213 * automode instance. Several of the calculations within automode depend
214 * on knowing the packet size.
215 *
216 *
217 * Input:
218 * - inst : Automode instance
219 * - newLenSamp : Number of samples per RecOut call
220 * - fsHz : Sample rate in Hz
221 *
222 * Output:
223 * - inst : Updated automode instance
224 *
225 * Return value : 0 - Ok
226 * <0 - Error
227 */
228
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000229int WebRtcNetEQ_SetPacketSpeechLen(AutomodeInst_t *inst, int16_t newLenSamp,
230 int32_t fsHz);
niklase@google.com470e71d2011-07-07 08:21:25 +0000231
232/****************************************************************************
233 * WebRtcNetEQ_ResetAutomode(...)
234 *
235 * Reset the automode instance.
236 *
237 *
238 * Input:
239 * - inst : Automode instance
240 * - maxBufLenPackets : Maximum number of packets that the packet
241 * buffer can hold (>1)
242 *
243 * Output:
244 * - inst : Updated automode instance
245 *
246 * Return value : 0 - Ok
247 */
248
249int WebRtcNetEQ_ResetAutomode(AutomodeInst_t *inst, int maxBufLenPackets);
250
henrik.lundin@webrtc.orgd4398702012-01-04 13:09:55 +0000251/****************************************************************************
252 * WebRtcNetEQ_AverageIAT(...)
253 *
254 * Calculate the average inter-arrival time based on current statistics.
255 * The average is expressed in parts per million relative the nominal. That is,
256 * if the average inter-arrival time is equal to the nominal frame time,
257 * the return value is zero. A positive value corresponds to packet spacing
258 * being too large, while a negative value means that the packets arrive with
259 * less spacing than expected.
260 *
261 *
262 * Input:
263 * - inst : Automode instance.
264 *
265 * Return value : Average relative inter-arrival time in samples.
266 */
267
268int32_t WebRtcNetEQ_AverageIAT(const AutomodeInst_t *inst);
269
niklase@google.com470e71d2011-07-07 08:21:25 +0000270#endif /* AUTOMODE_H */