aboutsummaryrefslogtreecommitdiffstats
path: root/main/fixedjitterbuf.c
blob: 1d7a5cc301f71f0d0a78c61b1fdb8d33eef6c18a (plain)
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
/*
 * Copyright (C) 2005, Attractel OOD
 *
 * Contributors:
 * Slav Klenov <slav@securax.org>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 *
 * A license has been granted to Digium (via disclaimer) for the use of
 * this code.
 */

/*! \file 
 * 
 * \brief Jitterbuffering algorithm.
 * 
 * \author Slav Klenov <slav@securax.org>
 */

#include "asterisk.h"

ASTERISK_FILE_VERSION(__FILE__, "$Revision$")

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <unistd.h>

#include "asterisk/utils.h"
#include "fixedjitterbuf.h"

#undef FIXED_JB_DEBUG

#ifdef FIXED_JB_DEBUG
#define ASSERT(a)
#else
#define ASSERT(a) assert(a)
#endif

/*! \brief private fixed_jb structure */
struct fixed_jb
{
	struct fixed_jb_frame *frames;
	struct fixed_jb_frame *tail;
	struct fixed_jb_conf conf;
	long rxcore;
	long delay;
	long next_delivery;
	int force_resynch;
};


static struct fixed_jb_frame *alloc_jb_frame(struct fixed_jb *jb);
static void release_jb_frame(struct fixed_jb *jb, struct fixed_jb_frame *frame);
static void get_jb_head(struct fixed_jb *jb, struct fixed_jb_frame *frame);
static int resynch_jb(struct fixed_jb *jb, void *data, long ms, long ts, long now);

static inline struct fixed_jb_frame *alloc_jb_frame(struct fixed_jb *jb)
{
	return ast_calloc(1, sizeof(struct fixed_jb_frame));
}

static inline void release_jb_frame(struct fixed_jb *jb, struct fixed_jb_frame *frame)
{
	free(frame);
}

static void get_jb_head(struct fixed_jb *jb, struct fixed_jb_frame *frame)
{
	struct fixed_jb_frame *fr;
	
	/* unlink the frame */
	fr = jb->frames;
	jb->frames = fr->next;
	if (jb->frames) {
		jb->frames->prev = NULL;
	} else {
		/* the jb is empty - update tail */
		jb->tail = NULL;
	}
	
	/* update next */
	jb->next_delivery = fr->delivery + fr->ms;
	
	/* copy the destination */
	memcpy(frame, fr, sizeof(struct fixed_jb_frame));
	
	/* and release the frame */
	release_jb_frame(jb, fr);
}


struct fixed_jb *fixed_jb_new(struct fixed_jb_conf *conf)
{
	struct fixed_jb *jb;
	
	if (!(jb = ast_calloc(1, sizeof(*jb))))
		return NULL;
	
	/* First copy our config */
	memcpy(&jb->conf, conf, sizeof(struct fixed_jb_conf));

	/* we dont need the passed config anymore - continue working with the saved one */
	conf = &jb->conf;
	
	/* validate the configuration */
	if (conf->jbsize < 1)
		conf->jbsize = FIXED_JB_SIZE_DEFAULT;

	if (conf->resync_threshold < 1)
		conf->resync_threshold = FIXED_JB_RESYNCH_THRESHOLD_DEFAULT;
	
	/* Set the constant delay to the jitterbuf */
	jb->delay = conf->jbsize;
	
	return jb;
}


void fixed_jb_destroy(struct fixed_jb *jb)
{
	/* jitterbuf MUST be empty before it can be destroyed */
	ASSERT(jb->frames == NULL);
	
	free(jb);
}


static int resynch_jb(struct fixed_jb *jb, void *data, long ms, long ts, long now)
{
	long diff, offset;
	struct fixed_jb_frame *frame;
	
	/* If jb is empty, just reinitialize the jb */
	if (!jb->frames) {
		/* debug check: tail should also be NULL */
		ASSERT(jb->tail == NULL);
		
		return fixed_jb_put_first(jb, data, ms, ts, now);
	}
	
	/* Adjust all jb state just as the new frame is with delivery = the delivery of the last
	   frame (e.g. this one with max delivery) + the length of the last frame. */
	
	/* Get the diff in timestamps */
	diff = ts - jb->tail->ts;
	
	/* Ideally this should be just the length of the last frame. The deviation is the desired
	   offset */
	offset = diff - jb->tail->ms;
	
	/* Do we really need to resynch, or this is just a frame for dropping? */
	if (!jb->force_resynch && (offset < jb->conf.resync_threshold && offset > -jb->conf.resync_threshold))
		return FIXED_JB_DROP;
	
	/* Reset the force resynch flag */
	jb->force_resynch = 0;
	
	/* apply the offset to the jb state */
	jb->rxcore -= offset;
	frame = jb->frames;
	while (frame) {
		frame->ts += offset;
		frame = frame->next;
	}
	
	/* now jb_put() should add the frame at a last position */
	return fixed_jb_put(jb, data, ms, ts, now);
}


void fixed_jb_set_force_resynch(struct fixed_jb *jb)
{
	jb->force_resynch = 1;
}


int fixed_jb_put_first(struct fixed_jb *jb, void *data, long ms, long ts, long now)
{
	/* this is our first frame - set the base of the receivers time */
	jb->rxcore = now - ts;
	
	/* init next for a first time - it should be the time the first frame should be played */
	jb->next_delivery = now + jb->delay;
	
	/* put the frame */
	return fixed_jb_put(jb, data, ms, ts, now);
}


int fixed_jb_put(struct fixed_jb *jb, void *data, long ms, long ts, long now)
{
	struct fixed_jb_frame *frame, *next, *newframe;
	long delivery;
	
	/* debug check the validity of the input params */
	ASSERT(data != NULL);
	/* do not allow frames shorter than 2 ms */
	ASSERT(ms >= 2);
	ASSERT(ts >= 0);
	ASSERT(now >= 0);
	
	delivery = jb->rxcore + jb->delay + ts;
	
	/* check if the new frame is not too late */
	if (delivery < jb->next_delivery) {
		/* should drop the frame, but let first resynch_jb() check if this is not a jump in ts, or
		   the force resynch flag was not set. */
		return resynch_jb(jb, data, ms, ts, now);
	}
	
	/* what if the delivery time is bigger than next + delay? Seems like a frame for the future.
	   However, allow more resync_threshold ms in advance */
	if (delivery > jb->next_delivery + jb->delay + jb->conf.resync_threshold) {
		/* should drop the frame, but let first resynch_jb() check if this is not a jump in ts, or
		   the force resynch flag was not set. */
		return resynch_jb(jb, data, ms, ts, now);
	}

	/* find the right place in the frames list, sorted by delivery time */
	frame = jb->tail;
	while (frame && frame->delivery > delivery) {
		frame = frame->prev;
	}
	
	/* Check if the new delivery time is not covered already by the chosen frame */
	if (frame && (frame->delivery == delivery ||
		         delivery < frame->delivery + frame->ms ||
		         (frame->next && delivery + ms > frame->next->delivery)))
	{
		/* TODO: Should we check for resynch here? Be careful to do not allow threshold smaller than
		   the size of the jb */
		
		/* should drop the frame, but let first resynch_jb() check if this is not a jump in ts, or
		   the force resynch flag was not set. */
		return resynch_jb(jb, data, ms, ts, now);
	}
	
	/* Reset the force resynch flag */
	jb->force_resynch = 0;
	
	/* Get a new frame */
	newframe = alloc_jb_frame(jb);
	newframe->data = data;
	newframe->ts = ts;
	newframe->ms = ms;
	newframe->delivery = delivery;
	
	/* and insert it right on place */
	if (frame) {
		next = frame->next;
		frame->next = newframe;
		if (next) {
			newframe->next = next;
			next->prev = newframe;
		} else {
			/* insert after the last frame - should update tail */
			jb->tail = newframe;
			newframe->next = NULL;
		}
		newframe->prev = frame;
		
		return FIXED_JB_OK;
	} else if (!jb->frames) {
		/* the frame list is empty or thats just the first frame ever */
		/* tail should also be NULL is that case */
		ASSERT(jb->tail == NULL);
		jb->frames = jb->tail = newframe;
		newframe->next = NULL;
		newframe->prev = NULL;
		
		return FIXED_JB_OK;
	} else {
		/* insert on a first position - should update frames head */
		newframe->next = jb->frames;
		newframe->prev = NULL;
		jb->frames->prev = newframe;
		jb->frames = newframe;
		
		return FIXED_JB_OK;
	}
}


int fixed_jb_get(struct fixed_jb *jb, struct fixed_jb_frame *frame, long now, long interpl)
{
	ASSERT(now >= 0);
	ASSERT(interpl >= 2);
	
	if (now < jb->next_delivery) {
		/* too early for the next frame */
		return FIXED_JB_NOFRAME;
	}
	
	/* Is the jb empty? */
	if (!jb->frames) {
		/* should interpolate a frame */
		/* update next */
		jb->next_delivery += interpl;
		
		return FIXED_JB_INTERP;
	}
	
	/* Isn't it too late for the first frame available in the jb? */
	if (now > jb->frames->delivery + jb->frames->ms) {
		/* yes - should drop this frame and update next to point the next frame (get_jb_head() does it) */
		get_jb_head(jb, frame);
		
		return FIXED_JB_DROP;
	}
	
	/* isn't it too early to play the first frame available? */
	if (now < jb->frames->delivery) {
		/* yes - should interpolate one frame */
		/* update next */
		jb->next_delivery += interpl;
		
		return FIXED_JB_INTERP;
	}
	
	/* we have a frame for playing now (get_jb_head() updates next) */
	get_jb_head(jb, frame);
	
	return FIXED_JB_OK;
}


long fixed_jb_next(struct fixed_jb *jb)
{
	return jb->next_delivery;
}


int fixed_jb_remove(struct fixed_jb *jb, struct fixed_jb_frame *frameout)
{
	if (!jb->frames)
		return FIXED_JB_NOFRAME;
	
	get_jb_head(jb, frameout);
	
	return FIXED_JB_OK;
}