forked from jamulussoftware/jamulus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer.cpp
More file actions
executable file
·386 lines (331 loc) · 12.9 KB
/
Copy pathbuffer.cpp
File metadata and controls
executable file
·386 lines (331 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/******************************************************************************\
* Copyright (c) 2004-2020
*
* Author(s):
* Volker Fischer
*
* Note: We are assuming here that put and get operations are secured by a mutex
* and accessing does not occur at the same time.
*
******************************************************************************
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
\******************************************************************************/
#include "buffer.h"
/* Network buffer implementation **********************************************/
void CNetBuf::Init ( const int iNewBlockSize,
const int iNewNumBlocks,
const bool bPreserve )
{
// store block size value
iBlockSize = iNewBlockSize;
// total size -> size of one block times the number of blocks
CBufferBase<uint8_t>::Init ( iNewBlockSize * iNewNumBlocks,
bPreserve );
// clear buffer if not preserved
if ( !bPreserve )
{
Clear();
}
}
bool CNetBuf::Put ( const CVector<uint8_t>& vecbyData,
const int iInSize )
{
bool bPutOK = true;
// check if there is not enough space available
if ( GetAvailSpace() < iInSize )
{
return false;
}
// copy new data in internal buffer (implemented in base class)
CBufferBase<uint8_t>::Put ( vecbyData, iInSize );
return bPutOK;
}
bool CNetBuf::Get ( CVector<uint8_t>& vecbyData,
const int iOutSize )
{
bool bGetOK = true; // init return value
// check size
if ( ( iOutSize == 0 ) || ( iOutSize != iBlockSize ) )
{
return false;
}
// check if there is not enough data available
if ( GetAvailData() < iOutSize )
{
return false;
}
// copy data from internal buffer in output buffer (implemented in base
// class)
CBufferBase<uint8_t>::Get ( vecbyData, iOutSize );
return bGetOK;
}
/* Network buffer with statistic calculations implementation ******************/
CNetBufWithStats::CNetBufWithStats() :
CNetBuf ( false ), // base class init: no simulation mode
iMaxStatisticCount ( MAX_STATISTIC_COUNT ),
bUseDoubleSystemFrameSize ( false ),
dAutoFilt_WightUpNormal ( IIR_WEIGTH_UP_NORMAL ),
dAutoFilt_WightDownNormal ( IIR_WEIGTH_DOWN_NORMAL ),
dAutoFilt_WightUpFast ( IIR_WEIGTH_UP_FAST ),
dAutoFilt_WightDownFast ( IIR_WEIGTH_DOWN_FAST ),
dErrorRateBound ( ERROR_RATE_BOUND ),
dUpMaxErrorBound ( UP_MAX_ERROR_BOUND )
{
// Define the sizes of the simulation buffers,
// must be NUM_STAT_SIMULATION_BUFFERS elements!
// Avoid the buffer length 1 because we do not have a solution for a
// sample rate offset correction. Caused by the jitter we usually get bad
// performance with just one buffer.
viBufSizesForSim[0] = 2;
viBufSizesForSim[1] = 3;
viBufSizesForSim[2] = 4;
viBufSizesForSim[3] = 5;
viBufSizesForSim[4] = 6;
viBufSizesForSim[5] = 7;
viBufSizesForSim[6] = 8;
viBufSizesForSim[7] = 9;
viBufSizesForSim[8] = 10;
viBufSizesForSim[9] = 11;
// set all simulation buffers in simulation mode
for ( int i = 0; i < NUM_STAT_SIMULATION_BUFFERS; i++ )
{
SimulationBuffer[i].SetIsSimulation ( true );
}
}
void CNetBufWithStats::GetErrorRates ( CVector<double>& vecErrRates,
double& dLimit,
double& dMaxUpLimit )
{
// get all the averages of the error statistic
vecErrRates.Init ( NUM_STAT_SIMULATION_BUFFERS );
for ( int i = 0; i < NUM_STAT_SIMULATION_BUFFERS; i++ )
{
vecErrRates[i] = ErrorRateStatistic[i].GetAverage();
}
// get the limits for the decisions
dLimit = dErrorRateBound;
dMaxUpLimit = dUpMaxErrorBound;
}
void CNetBufWithStats::Init ( const int iNewBlockSize,
const int iNewNumBlocks,
const bool bPreserve )
{
// call base class Init
CNetBuf::Init ( iNewBlockSize, iNewNumBlocks, bPreserve );
// inits for statistics calculation
if ( !bPreserve )
{
// set the auto filter weights and max statistic count
if ( bUseDoubleSystemFrameSize )
{
dAutoFilt_WightUpNormal = IIR_WEIGTH_UP_NORMAL_DOUBLE_FRAME_SIZE;
dAutoFilt_WightDownNormal = IIR_WEIGTH_DOWN_NORMAL_DOUBLE_FRAME_SIZE;
dAutoFilt_WightUpFast = IIR_WEIGTH_UP_FAST_DOUBLE_FRAME_SIZE;
dAutoFilt_WightDownFast = IIR_WEIGTH_DOWN_FAST_DOUBLE_FRAME_SIZE;
iMaxStatisticCount = MAX_STATISTIC_COUNT_DOUBLE_FRAME_SIZE;
dErrorRateBound = ERROR_RATE_BOUND_DOUBLE_FRAME_SIZE;
dUpMaxErrorBound = UP_MAX_ERROR_BOUND_DOUBLE_FRAME_SIZE;
}
else
{
dAutoFilt_WightUpNormal = IIR_WEIGTH_UP_NORMAL;
dAutoFilt_WightDownNormal = IIR_WEIGTH_DOWN_NORMAL;
dAutoFilt_WightUpFast = IIR_WEIGTH_UP_FAST;
dAutoFilt_WightDownFast = IIR_WEIGTH_DOWN_FAST;
iMaxStatisticCount = MAX_STATISTIC_COUNT;
dErrorRateBound = ERROR_RATE_BOUND;
dUpMaxErrorBound = UP_MAX_ERROR_BOUND;
}
for ( int i = 0; i < NUM_STAT_SIMULATION_BUFFERS; i++ )
{
// init simulation buffers with the correct size
SimulationBuffer[i].Init ( iNewBlockSize, viBufSizesForSim[i] );
// init statistics
ErrorRateStatistic[i].Init ( iMaxStatisticCount, true );
}
// reset the initialization counter which controls the initialization
// phase length
ResetInitCounter();
// init auto buffer setting with a meaningful value, also init the
// IIR parameter with this value
iCurAutoBufferSizeSetting = 6;
dCurIIRFilterResult = iCurAutoBufferSizeSetting;
iCurDecidedResult = iCurAutoBufferSizeSetting;
}
}
void CNetBufWithStats::ResetInitCounter()
{
// start initialization phase of IIR filtering, use a quarter the size
// of the error rate statistic buffers which should be ok for a good
// initialization value (initialization phase should be as short as
// possible)
iInitCounter = iMaxStatisticCount / 4;
}
bool CNetBufWithStats::Put ( const CVector<uint8_t>& vecbyData,
const int iInSize )
{
// call base class Put
const bool bPutOK = CNetBuf::Put ( vecbyData, iInSize );
// update statistics calculations
for ( int i = 0; i < NUM_STAT_SIMULATION_BUFFERS; i++ )
{
ErrorRateStatistic[i].Update (
!SimulationBuffer[i].Put ( vecbyData, iInSize ) );
}
return bPutOK;
}
bool CNetBufWithStats::Get ( CVector<uint8_t>& vecbyData,
const int iOutSize )
{
// call base class Get
const bool bGetOK = CNetBuf::Get ( vecbyData, iOutSize );
// update statistics calculations
for ( int i = 0; i < NUM_STAT_SIMULATION_BUFFERS; i++ )
{
ErrorRateStatistic[i].Update (
!SimulationBuffer[i].Get ( vecbyData, iOutSize ) );
}
// update auto setting
UpdateAutoSetting();
return bGetOK;
}
void CNetBufWithStats::UpdateAutoSetting()
{
int iCurDecision = 0; // dummy initialization
int iCurMaxUpDecision = 0; // dummy initialization
bool bDecisionFound;
// Get regular error rate decision -----------------------------------------
// Use a specified error bound to identify the best buffer size for the
// current network situation. Start with the smallest buffer and
// test for the error rate until the rate is below the bound.
bDecisionFound = false;
for ( int i = 0; i < NUM_STAT_SIMULATION_BUFFERS - 1; i++ )
{
if ( ( !bDecisionFound ) &&
( ErrorRateStatistic[i].GetAverage() <= dErrorRateBound ) )
{
iCurDecision = viBufSizesForSim[i];
bDecisionFound = true;
}
}
if ( !bDecisionFound )
{
// in case no buffer is below bound, use largest buffer size
iCurDecision = viBufSizesForSim[NUM_STAT_SIMULATION_BUFFERS - 1];
}
// Get maximum upper error rate decision -----------------------------------
// Use a specified error bound to identify the maximum upper error rate
// to identify if we have a too low buffer setting which gives a very
// bad performance constantly. Start with the smallest buffer and
// test for the error rate until the rate is below the bound.
bDecisionFound = false;
for ( int i = 0; i < NUM_STAT_SIMULATION_BUFFERS - 1; i++ )
{
if ( ( !bDecisionFound ) &&
( ErrorRateStatistic[i].GetAverage() <= dUpMaxErrorBound ) )
{
iCurMaxUpDecision = viBufSizesForSim[i];
bDecisionFound = true;
}
}
if ( !bDecisionFound )
{
// in case no buffer is below bound, use largest buffer size
iCurMaxUpDecision = viBufSizesForSim[NUM_STAT_SIMULATION_BUFFERS - 1];
// This is a worst case, something very bad had happened. Hopefully
// this was just temporary so that we initiate a new initialzation
// phase to get quickly back to normal buffer sizes (hopefully).
ResetInitCounter();
}
// Post calculation (filtering) --------------------------------------------
// Define different weigths for up and down direction. Up direction
// filtering shall be slower than for down direction since we assume
// that the lower value is the actual value which can be used for
// the current network condition. If the current error rate estimation
// is higher, it may be a temporary problem which should not change
// the current jitter buffer size significantly.
// For the initialization phase, use lower weight values to get faster
// adaptation.
double dWeightUp, dWeightDown;
const double dHysteresisValue = FILTER_DECISION_HYSTERESIS;
bool bUseFastAdaptation = false;
// check for initialization phase
if ( iInitCounter > 0 )
{
// decrease init counter
iInitCounter--;
// use the fast adaptation
bUseFastAdaptation = true;
}
// if the current detected buffer setting is below the maximum upper bound
// decision, then we enable a booster to go up to the minimum required
// number of buffer blocks (i.e. we use weights for fast adaptation)
if ( iCurAutoBufferSizeSetting < iCurMaxUpDecision )
{
bUseFastAdaptation = true;
}
if ( bUseFastAdaptation )
{
dWeightUp = dAutoFilt_WightUpFast;
dWeightDown = dAutoFilt_WightDownFast;
}
else
{
dWeightUp = dAutoFilt_WightUpNormal;
dWeightDown = dAutoFilt_WightDownNormal;
}
// apply non-linear IIR filter
MathUtils().UpDownIIR1 ( dCurIIRFilterResult,
static_cast<double> ( iCurDecision ),
dWeightUp,
dWeightDown );
/*
// TEST store important detection parameters in file for debugging
static FILE* pFile = fopen ( "test.dat", "w" );
static int icnt = 0;
if ( icnt == 50 )
{
fprintf ( pFile, "%d %e\n", iCurDecision, dCurIIRFilterResult );
fflush ( pFile );
icnt = 0;
}
else
{
icnt++;
}
*/
// apply a hysteresis
iCurAutoBufferSizeSetting =
MathUtils().DecideWithHysteresis ( dCurIIRFilterResult,
iCurDecidedResult,
dHysteresisValue );
// Initialization phase check and correction -------------------------------
// sometimes in the very first period after a connection we get a bad error
// rate result -> delete this from the initialization phase
if ( iInitCounter == iMaxStatisticCount / 8 )
{
// check error rate of the largest buffer as the indicator
if ( ErrorRateStatistic[NUM_STAT_SIMULATION_BUFFERS - 1].
GetAverage() > dErrorRateBound )
{
for ( int i = 0; i < NUM_STAT_SIMULATION_BUFFERS; i++ )
{
ErrorRateStatistic[i].Reset();
}
}
}
}