blob: c5dd829b834740ea31f5aac2793e6bd08330c17b [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 */
henrik.lundin@webrtc.org6f6ba6e2013-11-21 13:17:29 +000083 int firstPacketReceived; /* set to zero implicitly when the instance is
84 filled with zeros */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000085 int32_t sampleMemory; /* memory position for keeping track of how many
niklase@google.com470e71d2011-07-07 08:21:25 +000086 samples we cut during expand */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000087 int16_t prevTimeScale; /* indicates that the last mode was an accelerate
niklase@google.com470e71d2011-07-07 08:21:25 +000088 or pre-emptive expand operation */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000089 uint32_t timescaleHoldOff; /* counter that is shifted one step right each
niklase@google.com470e71d2011-07-07 08:21:25 +000090 RecOut call; time-scaling allowed when it has
91 reached 0 */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000092 int16_t extraDelayMs; /* extra delay for sync with video */
niklase@google.com470e71d2011-07-07 08:21:25 +000093
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +000094 int minimum_delay_ms; /* Desired delay, NetEq maintains this amount of
95 delay unless jitter statistics suggests a higher value. */
pwestin@webrtc.org401ef362013-08-06 21:01:36 +000096 int maximum_delay_ms; /* Max desired delay, NetEq will not go above this
97 amount of delay even if jitter statistics suggests a higher value. */
98
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +000099 int required_delay_q8; /* Smallest delay required. This is computed
100 according to inter-arrival time and playout mode. It has the same unit
101 as |optBufLevel|. */
102
niklase@google.com470e71d2011-07-07 08:21:25 +0000103 /* Peak-detection */
104 /* vector with the latest peak periods (peak spacing in samples) */
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000105 uint32_t peakPeriodSamp[NUM_PEAKS];
niklase@google.com470e71d2011-07-07 08:21:25 +0000106 /* vector with the latest peak heights (in packets) */
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000107 int16_t peakHeightPkt[NUM_PEAKS];
108 int16_t peakIndex; /* index for the vectors peakPeriodSamp and peakHeightPkt;
niklase@google.com470e71d2011-07-07 08:21:25 +0000109 -1 if still waiting for first peak */
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000110 uint16_t peakThresholdPkt; /* definition of peak (in packets);
niklase@google.com470e71d2011-07-07 08:21:25 +0000111 calculated from PEAK_HEIGHT */
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000112 uint32_t peakIatCountSamp; /* samples elapsed since last peak was observed */
113 uint32_t curPeakPeriod; /* current maximum of peakPeriodSamp vector */
114 int16_t curPeakHeight; /* derived from peakHeightPkt vector;
niklase@google.com470e71d2011-07-07 08:21:25 +0000115 used as optimal buffer level in peak mode */
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000116 int16_t peakModeDisabled; /* ==0 if peak mode can be engaged; >0 if not */
henrik.lundin@webrtc.orgd4398702012-01-04 13:09:55 +0000117 uint16_t peakFound; /* 1 if peaks are detected and extra delay is applied;
118 * 0 otherwise. */
niklase@google.com470e71d2011-07-07 08:21:25 +0000119
120 /* Post-call statistics */
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000121 uint32_t countIAT500ms; /* number of times we got small network outage */
122 uint32_t countIAT1000ms; /* number of times we got medium network outage */
123 uint32_t countIAT2000ms; /* number of times we got large network outage */
124 uint32_t longestIATms; /* mSec duration of longest network outage */
niklase@google.com470e71d2011-07-07 08:21:25 +0000125
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000126 int16_t cSumIatQ8; /* cumulative sum of inter-arrival times */
127 int16_t maxCSumIatQ8; /* max cumulative sum IAT */
128 uint32_t maxCSumUpdateTimer;/* time elapsed since maximum was observed */
niklase@google.com470e71d2011-07-07 08:21:25 +0000129} AutomodeInst_t;
130
131/*************/
132/* Functions */
133/*************/
134
135/****************************************************************************
136 * WebRtcNetEQ_UpdateIatStatistics(...)
137 *
138 * Update the packet inter-arrival time statistics when a new packet arrives.
139 * This function should be called for every arriving packet, with some
140 * exceptions when using DTX/VAD and DTMF. A new optimal buffer level is
141 * calculated after the update.
142 *
143 * Input:
144 * - inst : Automode instance
145 * - maxBufLen : Maximum number of packets the buffer can hold
146 * - seqNumber : RTP sequence number of incoming packet
147 * - timeStamp : RTP timestamp of incoming packet
148 * - fsHz : Sample rate in Hz
149 * - mdCodec : Non-zero if the current codec is a multiple-
150 * description codec
151 * - streamingMode : A non-zero value will increase jitter robustness (and delay)
152 *
153 * Output:
154 * - inst : Updated automode instance
155 *
156 * Return value : 0 - Ok
157 * <0 - Error
158 */
159
160int WebRtcNetEQ_UpdateIatStatistics(AutomodeInst_t *inst, int maxBufLen,
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000161 uint16_t seqNumber, uint32_t timeStamp,
162 int32_t fsHz, int mdCodec, int streamingMode);
niklase@google.com470e71d2011-07-07 08:21:25 +0000163
164/****************************************************************************
165 * WebRtcNetEQ_CalcOptimalBufLvl(...)
166 *
167 * Calculate the optimal buffer level based on packet inter-arrival time
168 * statistics.
169 *
170 * Input:
171 * - inst : Automode instance
172 * - fsHz : Sample rate in Hz
173 * - mdCodec : Non-zero if the current codec is a multiple-
174 * description codec
175 * - timeIatPkts : Currently observed inter-arrival time in packets
176 * - streamingMode : A non-zero value will increase jitter robustness (and delay)
177 *
178 * Output:
179 * - inst : Updated automode instance
180 *
181 * Return value : >0 - Optimal buffer level
182 * <0 - Error
183 */
184
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000185int16_t WebRtcNetEQ_CalcOptimalBufLvl(AutomodeInst_t *inst, int32_t fsHz,
186 int mdCodec, uint32_t timeIatPkts,
187 int streamingMode);
niklase@google.com470e71d2011-07-07 08:21:25 +0000188
189/****************************************************************************
190 * WebRtcNetEQ_BufferLevelFilter(...)
191 *
192 * Update filtered buffer level. The function must be called once for each
193 * RecOut call, since the timing of automode hinges on counters that are
194 * updated by this function.
195 *
196 * Input:
197 * - curSizeMs8 : Total length of unused speech data in packet buffer
198 * and sync buffer, in ms * 8
199 * - inst : Automode instance
200 * - sampPerCall : Number of samples per RecOut call
201 * - fsMult : Sample rate in Hz divided by 8000
202 *
203 * Output:
204 * - inst : Updated automode instance
205 *
206 * Return value : 0 - Ok
207 * : <0 - Error
208 */
209
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000210int WebRtcNetEQ_BufferLevelFilter(int32_t curSizeMs8, AutomodeInst_t *inst,
211 int sampPerCall, int16_t fsMult);
niklase@google.com470e71d2011-07-07 08:21:25 +0000212
213/****************************************************************************
214 * WebRtcNetEQ_SetPacketSpeechLen(...)
215 *
216 * Provide the number of speech samples extracted from a packet to the
217 * automode instance. Several of the calculations within automode depend
218 * on knowing the packet size.
219 *
220 *
221 * Input:
222 * - inst : Automode instance
223 * - newLenSamp : Number of samples per RecOut call
224 * - fsHz : Sample rate in Hz
225 *
226 * Output:
227 * - inst : Updated automode instance
228 *
229 * Return value : 0 - Ok
230 * <0 - Error
231 */
232
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000233int WebRtcNetEQ_SetPacketSpeechLen(AutomodeInst_t *inst, int16_t newLenSamp,
234 int32_t fsHz);
niklase@google.com470e71d2011-07-07 08:21:25 +0000235
236/****************************************************************************
237 * WebRtcNetEQ_ResetAutomode(...)
238 *
239 * Reset the automode instance.
240 *
241 *
242 * Input:
243 * - inst : Automode instance
244 * - maxBufLenPackets : Maximum number of packets that the packet
245 * buffer can hold (>1)
246 *
247 * Output:
248 * - inst : Updated automode instance
249 *
250 * Return value : 0 - Ok
251 */
252
253int WebRtcNetEQ_ResetAutomode(AutomodeInst_t *inst, int maxBufLenPackets);
254
henrik.lundin@webrtc.orgd4398702012-01-04 13:09:55 +0000255/****************************************************************************
256 * WebRtcNetEQ_AverageIAT(...)
257 *
258 * Calculate the average inter-arrival time based on current statistics.
259 * The average is expressed in parts per million relative the nominal. That is,
260 * if the average inter-arrival time is equal to the nominal frame time,
261 * the return value is zero. A positive value corresponds to packet spacing
262 * being too large, while a negative value means that the packets arrive with
263 * less spacing than expected.
264 *
265 *
266 * Input:
267 * - inst : Automode instance.
268 *
269 * Return value : Average relative inter-arrival time in samples.
270 */
271
272int32_t WebRtcNetEQ_AverageIAT(const AutomodeInst_t *inst);
273
niklase@google.com470e71d2011-07-07 08:21:25 +0000274#endif /* AUTOMODE_H */