Wed Jan 8 2020 09:50:12

Asterisk developer's documentation


fixedjitterbuf.h File Reference

Jitterbuffering algorithm. More...

Go to the source code of this file.

Data Structures

struct  fixed_jb_conf
 
struct  fixed_jb_frame
 

Macros

#define FIXED_JB_RESYNCH_THRESHOLD_DEFAULT   1000
 
#define FIXED_JB_SIZE_DEFAULT   200
 

Enumerations

enum  { FIXED_JB_OK, FIXED_JB_DROP, FIXED_JB_INTERP, FIXED_JB_NOFRAME }
 

Functions

void fixed_jb_destroy (struct fixed_jb *jb)
 
int fixed_jb_get (struct fixed_jb *jb, struct fixed_jb_frame *frame, long now, long interpl)
 
struct fixed_jbfixed_jb_new (struct fixed_jb_conf *conf)
 
long fixed_jb_next (struct fixed_jb *jb)
 
int fixed_jb_put (struct fixed_jb *jb, void *data, long ms, long ts, long now)
 
int fixed_jb_put_first (struct fixed_jb *jb, void *data, long ms, long ts, long now)
 
int fixed_jb_remove (struct fixed_jb *jb, struct fixed_jb_frame *frameout)
 
void fixed_jb_set_force_resynch (struct fixed_jb *jb)
 

Detailed Description

Jitterbuffering algorithm.

Definition in file fixedjitterbuf.h.

Macro Definition Documentation

#define FIXED_JB_RESYNCH_THRESHOLD_DEFAULT   1000

Definition at line 45 of file fixedjitterbuf.h.

Referenced by fixed_jb_new().

#define FIXED_JB_SIZE_DEFAULT   200

Definition at line 44 of file fixedjitterbuf.h.

Referenced by fixed_jb_new().

Enumeration Type Documentation

anonymous enum
Enumerator
FIXED_JB_OK 
FIXED_JB_DROP 
FIXED_JB_INTERP 
FIXED_JB_NOFRAME 

Definition at line 35 of file fixedjitterbuf.h.

Function Documentation

void fixed_jb_destroy ( struct fixed_jb jb)

Definition at line 129 of file fixedjitterbuf.c.

References ASSERT, ast_free, and fixed_jb::frames.

Referenced by jb_destroy_fixed().

130 {
131  /* jitterbuf MUST be empty before it can be destroyed */
132  ASSERT(jb->frames == NULL);
133 
134  ast_free(jb);
135 }
#define ASSERT(a)
struct fixed_jb_frame * frames
#define ast_free(a)
Definition: astmm.h:97
int fixed_jb_get ( struct fixed_jb jb,
struct fixed_jb_frame frame,
long  now,
long  interpl 
)

Definition at line 294 of file fixedjitterbuf.c.

References ASSERT, fixed_jb_frame::delivery, FIXED_JB_DROP, FIXED_JB_INTERP, FIXED_JB_NOFRAME, FIXED_JB_OK, frames, fixed_jb::frames, get_jb_head(), fixed_jb_frame::ms, and fixed_jb::next_delivery.

Referenced by jb_get_fixed().

295 {
296  ASSERT(now >= 0);
297  ASSERT(interpl >= 2);
298 
299  if (now < jb->next_delivery) {
300  /* too early for the next frame */
301  return FIXED_JB_NOFRAME;
302  }
303 
304  /* Is the jb empty? */
305  if (!jb->frames) {
306  /* should interpolate a frame */
307  /* update next */
308  jb->next_delivery += interpl;
309 
310  return FIXED_JB_INTERP;
311  }
312 
313  /* Isn't it too late for the first frame available in the jb? */
314  if (now > jb->frames->delivery + jb->frames->ms) {
315  /* yes - should drop this frame and update next to point the next frame (get_jb_head() does it) */
316  get_jb_head(jb, frame);
317 
318  return FIXED_JB_DROP;
319  }
320 
321  /* isn't it too early to play the first frame available? */
322  if (now < jb->frames->delivery) {
323  /* yes - should interpolate one frame */
324  /* update next */
325  jb->next_delivery += interpl;
326 
327  return FIXED_JB_INTERP;
328  }
329 
330  /* we have a frame for playing now (get_jb_head() updates next) */
331  get_jb_head(jb, frame);
332 
333  return FIXED_JB_OK;
334 }
#define ASSERT(a)
static void get_jb_head(struct fixed_jb *jb, struct fixed_jb_frame *frame)
static int frames
Definition: iax2-parser.c:49
struct fixed_jb_frame * frames
long next_delivery
struct fixed_jb* fixed_jb_new ( struct fixed_jb_conf conf)

Definition at line 102 of file fixedjitterbuf.c.

References ast_calloc, fixed_jb::conf, fixed_jb::delay, FIXED_JB_RESYNCH_THRESHOLD_DEFAULT, FIXED_JB_SIZE_DEFAULT, fixed_jb_conf::jbsize, and fixed_jb_conf::resync_threshold.

Referenced by jb_create_fixed().

103 {
104  struct fixed_jb *jb;
105 
106  if (!(jb = ast_calloc(1, sizeof(*jb))))
107  return NULL;
108 
109  /* First copy our config */
110  memcpy(&jb->conf, conf, sizeof(struct fixed_jb_conf));
111 
112  /* we don't need the passed config anymore - continue working with the saved one */
113  conf = &jb->conf;
114 
115  /* validate the configuration */
116  if (conf->jbsize < 1)
118 
119  if (conf->resync_threshold < 1)
121 
122  /* Set the constant delay to the jitterbuf */
123  jb->delay = conf->jbsize;
124 
125  return jb;
126 }
private fixed_jb structure
#define FIXED_JB_SIZE_DEFAULT
struct fixed_jb_conf conf
#define FIXED_JB_RESYNCH_THRESHOLD_DEFAULT
#define ast_calloc(a, b)
Definition: astmm.h:82
long fixed_jb_next ( struct fixed_jb jb)

Definition at line 337 of file fixedjitterbuf.c.

References fixed_jb::next_delivery.

Referenced by jb_next_fixed().

338 {
339  return jb->next_delivery;
340 }
long next_delivery
int fixed_jb_put ( struct fixed_jb jb,
void *  data,
long  ms,
long  ts,
long  now 
)

Definition at line 200 of file fixedjitterbuf.c.

References alloc_jb_frame(), ASSERT, fixed_jb::conf, fixed_jb_frame::data, fixed_jb::delay, fixed_jb_frame::delivery, FIXED_JB_OK, fixed_jb::force_resynch, fixed_jb::frames, fixed_jb_frame::ms, fixed_jb_frame::next, fixed_jb::next_delivery, fixed_jb_frame::prev, fixed_jb_conf::resync_threshold, resynch_jb(), fixed_jb::rxcore, fixed_jb::tail, and fixed_jb_frame::ts.

Referenced by fixed_jb_put_first(), jb_put_fixed(), and resynch_jb().

201 {
202  struct fixed_jb_frame *frame, *next, *newframe;
203  long delivery;
204 
205  /* debug check the validity of the input params */
206  ASSERT(data != NULL);
207  /* do not allow frames shorter than 2 ms */
208  ASSERT(ms >= 2);
209  ASSERT(ts >= 0);
210  ASSERT(now >= 0);
211 
212  delivery = jb->rxcore + jb->delay + ts;
213 
214  /* check if the new frame is not too late */
215  if (delivery < jb->next_delivery) {
216  /* should drop the frame, but let first resynch_jb() check if this is not a jump in ts, or
217  the force resynch flag was not set. */
218  return resynch_jb(jb, data, ms, ts, now);
219  }
220 
221  /* what if the delivery time is bigger than next + delay? Seems like a frame for the future.
222  However, allow more resync_threshold ms in advance */
223  if (delivery > jb->next_delivery + jb->delay + jb->conf.resync_threshold) {
224  /* should drop the frame, but let first resynch_jb() check if this is not a jump in ts, or
225  the force resynch flag was not set. */
226  return resynch_jb(jb, data, ms, ts, now);
227  }
228 
229  /* find the right place in the frames list, sorted by delivery time */
230  frame = jb->tail;
231  while (frame && frame->delivery > delivery) {
232  frame = frame->prev;
233  }
234 
235  /* Check if the new delivery time is not covered already by the chosen frame */
236  if (frame && (frame->delivery == delivery ||
237  delivery < frame->delivery + frame->ms ||
238  (frame->next && delivery + ms > frame->next->delivery)))
239  {
240  /* TODO: Should we check for resynch here? Be careful to do not allow threshold smaller than
241  the size of the jb */
242 
243  /* should drop the frame, but let first resynch_jb() check if this is not a jump in ts, or
244  the force resynch flag was not set. */
245  return resynch_jb(jb, data, ms, ts, now);
246  }
247 
248  /* Reset the force resynch flag */
249  jb->force_resynch = 0;
250 
251  /* Get a new frame */
252  newframe = alloc_jb_frame(jb);
253  newframe->data = data;
254  newframe->ts = ts;
255  newframe->ms = ms;
256  newframe->delivery = delivery;
257 
258  /* and insert it right on place */
259  if (frame) {
260  next = frame->next;
261  frame->next = newframe;
262  if (next) {
263  newframe->next = next;
264  next->prev = newframe;
265  } else {
266  /* insert after the last frame - should update tail */
267  jb->tail = newframe;
268  newframe->next = NULL;
269  }
270  newframe->prev = frame;
271 
272  return FIXED_JB_OK;
273  } else if (!jb->frames) {
274  /* the frame list is empty or thats just the first frame ever */
275  /* tail should also be NULL is that case */
276  ASSERT(jb->tail == NULL);
277  jb->frames = jb->tail = newframe;
278  newframe->next = NULL;
279  newframe->prev = NULL;
280 
281  return FIXED_JB_OK;
282  } else {
283  /* insert on a first position - should update frames head */
284  newframe->next = jb->frames;
285  newframe->prev = NULL;
286  jb->frames->prev = newframe;
287  jb->frames = newframe;
288 
289  return FIXED_JB_OK;
290  }
291 }
#define ASSERT(a)
static int resynch_jb(struct fixed_jb *jb, void *data, long ms, long ts, long now)
static struct fixed_jb_frame * alloc_jb_frame(struct fixed_jb *jb)
struct fixed_jb_frame * next
struct fixed_jb_frame * frames
struct fixed_jb_conf conf
struct fixed_jb_frame * tail
int force_resynch
struct fixed_jb_frame * prev
long next_delivery
int fixed_jb_put_first ( struct fixed_jb jb,
void *  data,
long  ms,
long  ts,
long  now 
)

Definition at line 187 of file fixedjitterbuf.c.

References fixed_jb::delay, fixed_jb_put(), fixed_jb::next_delivery, fixed_jb::rxcore, and fixed_jb_frame::ts.

Referenced by jb_put_first_fixed(), and resynch_jb().

188 {
189  /* this is our first frame - set the base of the receivers time */
190  jb->rxcore = now - ts;
191 
192  /* init next for a first time - it should be the time the first frame should be played */
193  jb->next_delivery = now + jb->delay;
194 
195  /* put the frame */
196  return fixed_jb_put(jb, data, ms, ts, now);
197 }
int fixed_jb_put(struct fixed_jb *jb, void *data, long ms, long ts, long now)
long next_delivery
int fixed_jb_remove ( struct fixed_jb jb,
struct fixed_jb_frame frameout 
)

Definition at line 343 of file fixedjitterbuf.c.

References FIXED_JB_NOFRAME, FIXED_JB_OK, fixed_jb::frames, and get_jb_head().

Referenced by jb_empty_and_reset_fixed(), and jb_remove_fixed().

344 {
345  if (!jb->frames)
346  return FIXED_JB_NOFRAME;
347 
348  get_jb_head(jb, frameout);
349 
350  return FIXED_JB_OK;
351 }
static void get_jb_head(struct fixed_jb *jb, struct fixed_jb_frame *frame)
struct fixed_jb_frame * frames
void fixed_jb_set_force_resynch ( struct fixed_jb jb)

Definition at line 181 of file fixedjitterbuf.c.

References fixed_jb::force_resynch.

Referenced by jb_force_resynch_fixed().

182 {
183  jb->force_resynch = 1;
184 }
int force_resynch