Sat Aug 6 00:39:58 2011

Asterisk developer's documentation


jitterbuf.c File Reference

jitterbuf: an application-independent jitterbuffer More...

#include "asterisk.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "jitterbuf.h"
#include "asterisk/utils.h"

Go to the source code of this file.

Defines

#define jb_dbg()   (dbgf ? dbgf(__VA_ARGS__) : (void)0)
#define jb_dbg2()   ((void)0)
#define jb_err()   (errf ? errf(__VA_ARGS__) : (void)0)
#define JB_LONGMAX   2147483647L
#define JB_LONGMIN   (-JB_LONGMAX - 1L)
#define jb_warn()   (warnf ? warnf(__VA_ARGS__) : (void)0)

Functions

static enum jb_return_code _jb_get (jitterbuf *jb, jb_frame *frameout, long now, long interpl)
static jb_frame_queue_get (jitterbuf *jb, long ts, int all)
static void decrement_losspct (jitterbuf *jb)
static void history_calc_maxbuf (jitterbuf *jb)
static void history_get (jitterbuf *jb)
static int history_put (jitterbuf *jb, long ts, long now, long ms)
 simple history manipulation
static void increment_losspct (jitterbuf *jb)
void jb_destroy (jitterbuf *jb)
enum jb_return_code jb_get (jitterbuf *jb, jb_frame *frameout, long now, long interpl)
enum jb_return_code jb_getall (jitterbuf *jb, jb_frame *frameout)
enum jb_return_code jb_getinfo (jitterbuf *jb, jb_info *stats)
jitterbufjb_new ()
long jb_next (jitterbuf *jb)
enum jb_return_code jb_put (jitterbuf *jb, void *data, const enum jb_frame_type type, long ms, long ts, long now)
void jb_reset (jitterbuf *jb)
enum jb_return_code jb_setconf (jitterbuf *jb, jb_conf *conf)
void jb_setoutput (jb_output_function_t err, jb_output_function_t warn, jb_output_function_t dbg)
static jb_framequeue_get (jitterbuf *jb, long ts)
static jb_framequeue_getall (jitterbuf *jb)
static long queue_last (jitterbuf *jb)
static long queue_next (jitterbuf *jb)
static int queue_put (jitterbuf *jb, void *data, const enum jb_frame_type type, long ms, long ts)

Variables

static jb_output_function_t dbgf
static jb_output_function_t errf
static jb_output_function_t warnf


Detailed Description

jitterbuf: an application-independent jitterbuffer

Author:
Steve Kann <stevek@stevek.com>

Definition in file jitterbuf.c.


Define Documentation

 
#define jb_dbg (  )     (dbgf ? dbgf(__VA_ARGS__) : (void)0)

Definition at line 48 of file jitterbuf.c.

Referenced by _jb_get().

 
#define jb_dbg2 (  )     ((void)0)

Definition at line 53 of file jitterbuf.c.

Referenced by jb_destroy(), jb_new(), and jb_put().

 
#define jb_err (  )     (errf ? errf(__VA_ARGS__) : (void)0)

Definition at line 47 of file jitterbuf.c.

Referenced by queue_put().

#define JB_LONGMAX   2147483647L

define these here, just for ancient compiler systems

Definition at line 43 of file jitterbuf.c.

Referenced by history_calc_maxbuf(), and jb_next().

#define JB_LONGMIN   (-JB_LONGMAX - 1L)

Definition at line 44 of file jitterbuf.c.

Referenced by history_calc_maxbuf().

 
#define jb_warn (  )     (warnf ? warnf(__VA_ARGS__) : (void)0)

Definition at line 46 of file jitterbuf.c.

Referenced by history_put(), and jb_get().


Function Documentation

static enum jb_return_code _jb_get ( jitterbuf jb,
jb_frame frameout,
long  now,
long  interpl 
) [static]

Definition at line 554 of file jitterbuf.c.

References jb_info::cnt_contig_interp, jb_info::conf, jb_info::current, decrement_losspct(), jb_info::frames_dropped, jb_info::frames_late, jb_info::frames_lost, jb_info::frames_out, history_get(), increment_losspct(), jitterbuf::info, JB_ADJUST_DELAY, jb_dbg, JB_DROP, JB_INTERP, JB_NOFRAME, JB_OK, JB_TARGET_EXTRA, JB_TYPE_SILENCE, JB_TYPE_VOICE, jb_info::jitter, jb_info::last_adjustment, jb_info::last_voice_ms, jb_conf::max_contig_interp, jb_conf::max_jitterbuf, jb_info::min, jb_frame::ms, jb_info::next_voice_ts, queue_get(), queue_last(), queue_next(), jb_info::silence_begin_ts, jb_info::target, jb_frame::ts, and jb_frame::type.

Referenced by jb_get().

00555 {
00556    jb_frame *frame;
00557    long diff;
00558    static int dbg_cnt = 0;
00559 
00560    /*if ((now - jb_next(jb)) > 2 * jb->info.last_voice_ms) jb_warn("SCHED: %ld", (now - jb_next(jb))); */
00561    /* get jitter info */
00562    history_get(jb);
00563 
00564    if (dbg_cnt && dbg_cnt % 50 == 0) {
00565       jb_dbg("\n");
00566    }
00567    dbg_cnt++;
00568 
00569    /* target */
00570    jb->info.target = jb->info.jitter + jb->info.min + JB_TARGET_EXTRA; 
00571 
00572    /* if a hard clamp was requested, use it */
00573    if ((jb->info.conf.max_jitterbuf) && ((jb->info.target - jb->info.min) > jb->info.conf.max_jitterbuf)) {
00574       jb_dbg("clamping target from %ld to %ld\n", (jb->info.target - jb->info.min), jb->info.conf.max_jitterbuf);
00575       jb->info.target = jb->info.min + jb->info.conf.max_jitterbuf;
00576    }
00577 
00578    diff = jb->info.target - jb->info.current;
00579 
00580    /* jb_warn("diff = %d lms=%d last = %d now = %d\n", diff,  */
00581    /* jb->info.last_voice_ms, jb->info.last_adjustment, now); */
00582 
00583    /* let's work on non-silent case first */
00584    if (!jb->info.silence_begin_ts) { 
00585       /* we want to grow */
00586       if ((diff > 0) && 
00587          /* we haven't grown in the delay length */
00588          (((jb->info.last_adjustment + JB_ADJUST_DELAY) < now) || 
00589          /* we need to grow more than the "length" we have left */
00590          (diff > queue_last(jb)  - queue_next(jb)) ) ) {
00591          /* grow by interp frame length */
00592          jb->info.current += interpl;
00593          jb->info.next_voice_ts += interpl;
00594          jb->info.last_voice_ms = interpl;
00595          jb->info.last_adjustment = now;
00596          jb->info.cnt_contig_interp++;
00597          if (jb->info.conf.max_contig_interp && jb->info.cnt_contig_interp >= jb->info.conf.max_contig_interp) {
00598             jb->info.silence_begin_ts = jb->info.next_voice_ts - jb->info.current;
00599          }
00600          jb_dbg("G");
00601          return JB_INTERP;
00602       }
00603 
00604       frame = queue_get(jb, jb->info.next_voice_ts - jb->info.current);
00605 
00606       /* not a voice frame; just return it. */
00607       if (frame && frame->type != JB_TYPE_VOICE) {
00608          if (frame->type == JB_TYPE_SILENCE) {
00609             jb->info.silence_begin_ts = frame->ts;
00610             jb->info.cnt_contig_interp = 0;
00611          }
00612 
00613          *frameout = *frame;
00614          jb->info.frames_out++;
00615          jb_dbg("o");
00616          return JB_OK;
00617       }
00618 
00619 
00620       /* voice frame is later than expected */
00621       if (frame && frame->ts + jb->info.current < jb->info.next_voice_ts) {
00622          if (frame->ts + jb->info.current > jb->info.next_voice_ts - jb->info.last_voice_ms) {
00623             /* either we interpolated past this frame in the last jb_get */
00624             /* or the frame is still in order, but came a little too quick */ 
00625             *frameout = *frame;
00626             /* reset expectation for next frame */
00627             jb->info.next_voice_ts = frame->ts + jb->info.current + frame->ms;
00628             jb->info.frames_out++;
00629             decrement_losspct(jb);
00630             jb->info.cnt_contig_interp = 0;
00631             jb_dbg("v");
00632             return JB_OK;
00633          } else {
00634             /* voice frame is late */
00635             *frameout = *frame;
00636             jb->info.frames_out++;
00637             decrement_losspct(jb);
00638             jb->info.frames_late++;
00639             jb->info.frames_lost--;
00640             jb_dbg("l");
00641             /*jb_warn("\nlate: wanted=%ld, this=%ld, next=%ld\n", jb->info.next_voice_ts - jb->info.current, frame->ts, queue_next(jb));
00642             jb_warninfo(jb); */
00643             return JB_DROP;
00644          }
00645       }
00646 
00647       /* keep track of frame sizes, to allow for variable sized-frames */
00648       if (frame && frame->ms > 0) {
00649          jb->info.last_voice_ms = frame->ms;
00650       }
00651 
00652       /* we want to shrink; shrink at 1 frame / 500ms */
00653       /* unless we don't have a frame, then shrink 1 frame */
00654       /* every 80ms (though perhaps we can shrink even faster */
00655       /* in this case) */
00656       if (diff < -JB_TARGET_EXTRA && 
00657          ((!frame && jb->info.last_adjustment + 80 < now) || 
00658          (jb->info.last_adjustment + 500 < now))) {
00659 
00660          jb->info.last_adjustment = now;
00661          jb->info.cnt_contig_interp = 0;
00662 
00663          if (frame) {
00664             *frameout = *frame;
00665             /* shrink by frame size we're throwing out */
00666             jb->info.current -= frame->ms;
00667             jb->info.frames_out++;
00668             decrement_losspct(jb);
00669             jb->info.frames_dropped++;
00670             jb_dbg("s");
00671             return JB_DROP;
00672          } else {
00673             /* shrink by last_voice_ms */
00674             jb->info.current -= jb->info.last_voice_ms;
00675             jb->info.frames_lost++;
00676             increment_losspct(jb);
00677             jb_dbg("S");
00678             return JB_NOFRAME;
00679          }
00680       }
00681 
00682       /* lost frame */
00683       if (!frame) {
00684          /* this is a bit of a hack for now, but if we're close to
00685           * target, and we find a missing frame, it makes sense to
00686           * grow, because the frame might just be a bit late;
00687           * otherwise, we presently get into a pattern where we return
00688           * INTERP for the lost frame, then it shows up next, and we
00689           * throw it away because it's late */
00690          /* I've recently only been able to replicate this using
00691           * iaxclient talking to app_echo on asterisk.  In this case,
00692           * my outgoing packets go through asterisk's (old)
00693           * jitterbuffer, and then might get an unusual increasing delay 
00694           * there if it decides to grow?? */
00695          /* Update: that might have been a different bug, that has been fixed..
00696           * But, this still seemed like a good idea, except that it ended up making a single actual
00697           * lost frame get interpolated two or more times, when there was "room" to grow, so it might
00698           * be a bit of a bad idea overall */
00699          /*if (diff > -1 * jb->info.last_voice_ms) { 
00700             jb->info.current += jb->info.last_voice_ms;
00701             jb->info.last_adjustment = now;
00702             jb_warn("g");
00703             return JB_INTERP;
00704          } */
00705          jb->info.frames_lost++;
00706          increment_losspct(jb);
00707          jb->info.next_voice_ts += interpl;
00708          jb->info.last_voice_ms = interpl;
00709          jb->info.cnt_contig_interp++;
00710          if (jb->info.conf.max_contig_interp && jb->info.cnt_contig_interp >= jb->info.conf.max_contig_interp) {
00711             jb->info.silence_begin_ts = jb->info.next_voice_ts - jb->info.current;
00712          }
00713          jb_dbg("L");
00714          return JB_INTERP;
00715       }
00716 
00717       /* normal case; return the frame, increment stuff */
00718       *frameout = *frame;
00719       jb->info.next_voice_ts += frame->ms;
00720       jb->info.frames_out++;
00721       jb->info.cnt_contig_interp = 0;
00722       decrement_losspct(jb);
00723       jb_dbg("v");
00724       return JB_OK;
00725    } else {     
00726       /* TODO: after we get the non-silent case down, we'll make the
00727        * silent case -- basically, we'll just grow and shrink faster
00728        * here, plus handle next_voice_ts a bit differently */
00729       
00730       /* to disable silent special case altogether, just uncomment this: */
00731       /* jb->info.silence_begin_ts = 0; */
00732 
00733       /* shrink interpl len every 10ms during silence */
00734       if (diff < -JB_TARGET_EXTRA &&
00735          jb->info.last_adjustment + 10 <= now) {
00736          jb->info.current -= interpl;
00737          jb->info.last_adjustment = now;
00738       }
00739 
00740       frame = queue_get(jb, now - jb->info.current);
00741       if (!frame) {
00742          return JB_NOFRAME;
00743       } else if (frame->type != JB_TYPE_VOICE) {
00744          /* normal case; in silent mode, got a non-voice frame */
00745          *frameout = *frame;
00746          jb->info.frames_out++;
00747          return JB_OK;
00748       }
00749       if (frame->ts < jb->info.silence_begin_ts) {
00750          /* voice frame is late */
00751          *frameout = *frame;
00752          jb->info.frames_out++;
00753          decrement_losspct(jb);
00754          jb->info.frames_late++;
00755          jb->info.frames_lost--;
00756          jb_dbg("l");
00757          /*jb_warn("\nlate: wanted=%ld, this=%ld, next=%ld\n", jb->info.next_voice_ts - jb->info.current, frame->ts, queue_next(jb));
00758          jb_warninfo(jb); */
00759          return JB_DROP;
00760       } else {
00761          /* voice frame */
00762          /* try setting current to target right away here */
00763          jb->info.current = jb->info.target;
00764          jb->info.silence_begin_ts = 0;
00765          jb->info.next_voice_ts = frame->ts + jb->info.current + frame->ms;
00766          jb->info.last_voice_ms = frame->ms;
00767          jb->info.frames_out++;
00768          decrement_losspct(jb);
00769          *frameout = *frame;
00770          jb_dbg("V");
00771          return JB_OK;
00772       }
00773    }
00774 }

static jb_frame* _queue_get ( jitterbuf jb,
long  ts,
int  all 
) [static]

Definition at line 402 of file jitterbuf.c.

References jitterbuf::frames, jb_info::frames_cur, jitterbuf::free, jitterbuf::info, jb_frame::next, jb_frame::prev, and jb_frame::ts.

Referenced by queue_get(), and queue_getall().

00403 {
00404    jb_frame *frame;
00405    frame = jb->frames;
00406 
00407    if (!frame)
00408       return NULL;
00409 
00410    /*jb_warn("queue_get: ASK %ld FIRST %ld\n", ts, frame->ts); */
00411 
00412    if (all || ts >= frame->ts) {
00413       /* remove this frame */
00414       frame->prev->next = frame->next;
00415       frame->next->prev = frame->prev;
00416 
00417       if (frame->next == frame)
00418          jb->frames = NULL;
00419       else
00420          jb->frames = frame->next;
00421 
00422 
00423       /* insert onto "free" single-linked list */
00424       frame->next = jb->free;
00425       jb->free = frame;
00426 
00427       jb->info.frames_cur--;
00428 
00429       /* we return the frame pointer, even though it's on free list, 
00430        * but caller must copy data */
00431       return frame;
00432    } 
00433 
00434    return NULL;
00435 }

static void decrement_losspct ( jitterbuf jb  )  [static]

Definition at line 70 of file jitterbuf.c.

References jitterbuf::info, and jb_info::losspct.

Referenced by _jb_get().

00071 {
00072    jb->info.losspct = (499 * jb->info.losspct)/500;    
00073 }

static void history_calc_maxbuf ( jitterbuf jb  )  [static]

Definition at line 205 of file jitterbuf.c.

References jitterbuf::hist_maxbuf, jitterbuf::hist_minbuf, jitterbuf::hist_ptr, jitterbuf::history, JB_HISTORY_MAXBUF_SZ, JB_HISTORY_SZ, JB_LONGMAX, and JB_LONGMIN.

Referenced by history_get().

00206 {
00207    int i,j;
00208 
00209    if (jb->hist_ptr == 0) 
00210       return;
00211 
00212 
00213    /* initialize maxbuf/minbuf to the latest value */
00214    for (i=0;i<JB_HISTORY_MAXBUF_SZ;i++) {
00215 /*
00216  * jb->hist_maxbuf[i] = jb->history[(jb->hist_ptr-1) % JB_HISTORY_SZ];
00217  * jb->hist_minbuf[i] = jb->history[(jb->hist_ptr-1) % JB_HISTORY_SZ];
00218  */
00219       jb->hist_maxbuf[i] = JB_LONGMIN;
00220       jb->hist_minbuf[i] = JB_LONGMAX;
00221    }
00222 
00223    /* use insertion sort to populate maxbuf */
00224    /* we want it to be the top "n" values, in order */
00225 
00226    /* start at the beginning, or JB_HISTORY_SZ frames ago */
00227    i = (jb->hist_ptr > JB_HISTORY_SZ) ? (jb->hist_ptr - JB_HISTORY_SZ) : 0; 
00228 
00229    for (;i<jb->hist_ptr;i++) {
00230       long toins = jb->history[i % JB_HISTORY_SZ];
00231 
00232       /* if the maxbuf should get this */
00233       if (toins > jb->hist_maxbuf[JB_HISTORY_MAXBUF_SZ-1])  {
00234 
00235          /* insertion-sort it into the maxbuf */
00236          for (j=0;j<JB_HISTORY_MAXBUF_SZ;j++) {
00237             /* found where it fits */
00238             if (toins > jb->hist_maxbuf[j]) {
00239                /* move over */
00240                memmove(jb->hist_maxbuf + j + 1, jb->hist_maxbuf + j, (JB_HISTORY_MAXBUF_SZ - (j + 1)) * sizeof(jb->hist_maxbuf[0]));
00241                /* insert */
00242                jb->hist_maxbuf[j] = toins;
00243 
00244                break;
00245             }
00246          }
00247       }
00248 
00249       /* if the minbuf should get this */
00250       if (toins < jb->hist_minbuf[JB_HISTORY_MAXBUF_SZ-1])  {
00251 
00252          /* insertion-sort it into the maxbuf */
00253          for (j=0;j<JB_HISTORY_MAXBUF_SZ;j++) {
00254             /* found where it fits */
00255             if (toins < jb->hist_minbuf[j]) {
00256                /* move over */
00257                memmove(jb->hist_minbuf + j + 1, jb->hist_minbuf + j, (JB_HISTORY_MAXBUF_SZ - (j + 1)) * sizeof(jb->hist_minbuf[0]));
00258                /* insert */
00259                jb->hist_minbuf[j] = toins;
00260 
00261                break;
00262             }
00263          }
00264       }
00265 
00266       if (0) { 
00267          int k;
00268          fprintf(stderr, "toins = %ld\n", toins);
00269          fprintf(stderr, "maxbuf =");
00270          for (k=0;k<JB_HISTORY_MAXBUF_SZ;k++) 
00271             fprintf(stderr, "%ld ", jb->hist_maxbuf[k]);
00272          fprintf(stderr, "\nminbuf =");
00273          for (k=0;k<JB_HISTORY_MAXBUF_SZ;k++) 
00274             fprintf(stderr, "%ld ", jb->hist_minbuf[k]);
00275          fprintf(stderr, "\n");
00276       }
00277    }
00278 
00279    jb->hist_maxbuf_valid = 1;
00280 }

static void history_get ( jitterbuf jb  )  [static]

Definition at line 282 of file jitterbuf.c.

References jitterbuf::hist_maxbuf, jitterbuf::hist_maxbuf_valid, jitterbuf::hist_minbuf, jitterbuf::hist_ptr, history_calc_maxbuf(), jitterbuf::info, JB_HISTORY_DROPPCT, JB_HISTORY_SZ, jb_info::jitter, and jb_info::min.

Referenced by _jb_get(), jb_getinfo(), and jb_next().

00283 {
00284    long max, min, jitter;
00285    int index;
00286    int count;
00287 
00288    if (!jb->hist_maxbuf_valid) 
00289       history_calc_maxbuf(jb);
00290 
00291    /* count is how many items in history we're examining */
00292    count = (jb->hist_ptr < JB_HISTORY_SZ) ? jb->hist_ptr : JB_HISTORY_SZ;
00293 
00294    /* index is the "n"ths highest/lowest that we'll look for */
00295    index = count * JB_HISTORY_DROPPCT / 100;
00296 
00297    /* sanity checks for index */
00298    if (index > (JB_HISTORY_MAXBUF_SZ - 1)) 
00299       index = JB_HISTORY_MAXBUF_SZ - 1;
00300 
00301 
00302    if (index < 0) {
00303       jb->info.min = 0;
00304       jb->info.jitter = 0;
00305       return;
00306    }
00307 
00308    max = jb->hist_maxbuf[index];
00309    min = jb->hist_minbuf[index];
00310 
00311    jitter = max - min;
00312 
00313    /* these debug stmts compare the difference between looking at the absolute jitter, and the
00314     * values we get by throwing away the outliers */
00315    /*
00316    fprintf(stderr, "[%d] min=%d, max=%d, jitter=%d\n", index, min, max, jitter);
00317    fprintf(stderr, "[%d] min=%d, max=%d, jitter=%d\n", 0, jb->hist_minbuf[0], jb->hist_maxbuf[0], jb->hist_maxbuf[0]-jb->hist_minbuf[0]);
00318    */
00319 
00320    jb->info.min = min;
00321    jb->info.jitter = jitter;
00322 }

static int history_put ( jitterbuf jb,
long  ts,
long  now,
long  ms 
) [static]

simple history manipulation

Note:
maybe later we can make the history buckets variable size, or something?

Definition at line 130 of file jitterbuf.c.

References jb_info::cnt_delay_discont, jb_info::conf, jitterbuf::hist_maxbuf, jitterbuf::hist_maxbuf_valid, jitterbuf::hist_ptr, jitterbuf::history, jitterbuf::info, JB_HISTORY_MAXBUF_SZ, JB_HISTORY_SZ, jb_warn, jb_info::jitter, jb_info::last_delay, jb_info::resync_offset, and jb_conf::resync_threshold.

Referenced by jb_put().

00131 {
00132    long delay = now - (ts - jb->info.resync_offset);
00133    long threshold = 2 * jb->info.jitter + jb->info.conf.resync_threshold;
00134    long kicked;
00135 
00136    /* don't add special/negative times to history */
00137    if (ts <= 0) 
00138       return 0;
00139 
00140    /* check for drastic change in delay */
00141    if (jb->info.conf.resync_threshold != -1) {
00142       if (abs(delay - jb->info.last_delay) > threshold) {
00143          jb->info.cnt_delay_discont++;
00144          if (jb->info.cnt_delay_discont > 3) {
00145             /* resync the jitterbuffer */
00146             jb->info.cnt_delay_discont = 0;
00147             jb->hist_ptr = 0;
00148             jb->hist_maxbuf_valid = 0;
00149 
00150             jb_warn("Resyncing the jb. last_delay %ld, this delay %ld, threshold %ld, new offset %ld\n", jb->info.last_delay, delay, threshold, ts - now);
00151             jb->info.resync_offset = ts - now;
00152             jb->info.last_delay = delay = 0; /* after resync, frame is right on time */
00153          } else {
00154             return -1;
00155          }
00156       } else {
00157          jb->info.last_delay = delay;
00158          jb->info.cnt_delay_discont = 0;
00159       }
00160    }
00161 
00162    kicked = jb->history[jb->hist_ptr % JB_HISTORY_SZ];
00163 
00164    jb->history[(jb->hist_ptr++) % JB_HISTORY_SZ] = delay;
00165 
00166    /* optimization; the max/min buffers don't need to be recalculated, if this packet's
00167     * entry doesn't change them.  This happens if this packet is not involved, _and_ any packet
00168     * that got kicked out of the history is also not involved 
00169     * We do a number of comparisons, but it's probably still worthwhile, because it will usually
00170     * succeed, and should be a lot faster than going through all 500 packets in history */
00171    if (!jb->hist_maxbuf_valid)
00172       return 0;
00173 
00174    /* don't do this until we've filled history 
00175     * (reduces some edge cases below) */
00176    if (jb->hist_ptr < JB_HISTORY_SZ)
00177       goto invalidate;
00178 
00179    /* if the new delay would go into min */
00180    if (delay < jb->hist_minbuf[JB_HISTORY_MAXBUF_SZ-1])
00181       goto invalidate;
00182     
00183    /* or max.. */
00184    if (delay > jb->hist_maxbuf[JB_HISTORY_MAXBUF_SZ-1])
00185       goto invalidate;
00186 
00187    /* or the kicked delay would be in min */
00188    if (kicked <= jb->hist_minbuf[JB_HISTORY_MAXBUF_SZ-1]) 
00189       goto invalidate;
00190 
00191    if (kicked >= jb->hist_maxbuf[JB_HISTORY_MAXBUF_SZ-1]) 
00192       goto invalidate;
00193 
00194    /* if we got here, we don't need to invalidate, 'cause this delay didn't 
00195     * affect things */
00196    return 0;
00197    /* end optimization */
00198 
00199 
00200 invalidate:
00201    jb->hist_maxbuf_valid = 0;
00202    return 0;
00203 }

static void increment_losspct ( jitterbuf jb  )  [static]

Definition at line 65 of file jitterbuf.c.

References jitterbuf::info, and jb_info::losspct.

Referenced by _jb_get().

00066 {
00067    jb->info.losspct = (100000 + 499 * jb->info.losspct)/500;    
00068 }

void jb_destroy ( jitterbuf jb  ) 

Definition at line 100 of file jitterbuf.c.

References jitterbuf::free, free, jb_dbg2, and jb_frame::next.

Referenced by jb_destroy_adaptive(), and pvt_destructor().

00101 {
00102    jb_frame *frame; 
00103    jb_dbg2("jb_destroy(%x)\n", jb);
00104 
00105    /* free all the frames on the "free list" */
00106    frame = jb->free;
00107    while (frame != NULL) {
00108       jb_frame *next = frame->next;
00109       free(frame);
00110       frame = next;
00111    }
00112 
00113    /* free ourselves! */ 
00114    free(jb);
00115 }

enum jb_return_code jb_get ( jitterbuf jb,
jb_frame frameout,
long  now,
long  interpl 
)

Definition at line 794 of file jitterbuf.c.

References _jb_get(), jitterbuf::info, JB_DROP, JB_INTERP, JB_OK, jb_warn, jb_info::last_voice_ms, jb_frame::ms, and jb_frame::ts.

Referenced by __get_from_jb(), and jb_get_adaptive().

00795 {
00796    enum jb_return_code ret = _jb_get(jb, frameout, now, interpl);
00797 #if 0
00798    static int lastts=0;
00799    int thists = ((ret == JB_OK) || (ret == JB_DROP)) ? frameout->ts : 0;
00800    jb_warn("jb_get(%x,%x,%ld) = %d (%d)\n", jb, frameout, now, ret, thists);
00801    if (thists && thists < lastts) jb_warn("XXXX timestamp roll-back!!!\n");
00802    lastts = thists;
00803 #endif
00804    if(ret == JB_INTERP) 
00805       frameout->ms = jb->info.last_voice_ms;
00806    
00807    return ret;
00808 }

enum jb_return_code jb_getall ( jitterbuf jb,
jb_frame frameout 
)

Definition at line 810 of file jitterbuf.c.

References JB_NOFRAME, JB_OK, and queue_getall().

Referenced by complete_transfer(), jb_empty_and_reset_adaptive(), jb_remove_adaptive(), pvt_destructor(), and schedule_delivery().

00811 {
00812    jb_frame *frame;
00813    frame = queue_getall(jb);
00814 
00815    if (!frame) {
00816       return JB_NOFRAME;
00817    }
00818 
00819    *frameout = *frame;
00820    return JB_OK;
00821 }

enum jb_return_code jb_getinfo ( jitterbuf jb,
jb_info stats 
)

Definition at line 824 of file jitterbuf.c.

References history_get(), jitterbuf::info, and JB_OK.

Referenced by ast_cli_netstats(), construct_rr(), and iax2_show_channels().

00825 {
00826 
00827    history_get(jb);
00828 
00829    *stats = jb->info;
00830 
00831    return JB_OK;
00832 }

jitterbuf* jb_new ( void   ) 

Definition at line 87 of file jitterbuf.c.

References ast_malloc, jb_dbg2, and jb_reset().

Referenced by jb_create_adaptive(), and new_iax().

00088 {
00089    jitterbuf *jb;
00090 
00091    if (!(jb = ast_malloc(sizeof(*jb)))) 
00092       return NULL;
00093 
00094    jb_reset(jb);
00095 
00096    jb_dbg2("jb_new() = %x\n", jb);
00097    return jb;
00098 }

long jb_next ( jitterbuf jb  ) 

Definition at line 776 of file jitterbuf.c.

References jb_info::current, jitterbuf::frames, history_get(), jitterbuf::info, JB_LONGMAX, JB_TARGET_EXTRA, jb_info::last_adjustment, jb_info::next_voice_ts, queue_next(), jb_info::silence_begin_ts, and jb_info::target.

Referenced by __get_from_jb(), jb_next_adaptive(), and update_jbsched().

00777 {
00778    if (jb->info.silence_begin_ts) {
00779       if (jb->frames) {
00780          long next = queue_next(jb);
00781          history_get(jb);
00782          /* shrink during silence */
00783          if (jb->info.target - jb->info.current < -JB_TARGET_EXTRA)
00784             return jb->info.last_adjustment + 10;
00785          return next + jb->info.target;
00786       }
00787       else 
00788          return JB_LONGMAX;
00789    } else {
00790       return jb->info.next_voice_ts;
00791    }
00792 }

enum jb_return_code jb_put ( jitterbuf jb,
void *  data,
const enum jb_frame_type  type,
long  ms,
long  ts,
long  now 
)

Definition at line 513 of file jitterbuf.c.

References ast_log(), jb_info::conf, jitterbuf::dropem, jitterbuf::frames, jb_info::frames_dropped, jb_info::frames_in, history_put(), jitterbuf::info, jb_dbg2, JB_DROP, JB_OK, JB_SCHED, JB_TYPE_VOICE, LOG_DEBUG, jb_conf::max_jitterbuf, jb_frame::prev, queue_put(), and jb_frame::ts.

Referenced by jb_put_adaptive(), and schedule_delivery().

00514 {
00515    long numts;
00516 
00517    jb_dbg2("jb_put(%x,%x,%ld,%ld,%ld)\n", jb, data, ms, ts, now);
00518 
00519    numts = 0;
00520    if (jb->frames)
00521       numts = jb->frames->prev->ts - jb->frames->ts;
00522 
00523    if (numts >= jb->info.conf.max_jitterbuf) {
00524       if (!jb->dropem) {
00525          ast_log(LOG_DEBUG, "Attempting to exceed Jitterbuf max %ld timeslots\n",
00526             jb->info.conf.max_jitterbuf);
00527          jb->dropem = 1;
00528       }
00529       jb->info.frames_dropped++;
00530       return JB_DROP;
00531    } else {
00532       jb->dropem = 0;
00533    }
00534 
00535    if (type == JB_TYPE_VOICE) {
00536       /* presently, I'm only adding VOICE frames to history and drift calculations; mostly because with the
00537        * IAX integrations, I'm sending retransmitted control frames with their awkward timestamps through */
00538       if (history_put(jb,ts,now,ms)) {
00539          jb->info.frames_dropped++;
00540          return JB_DROP;
00541       }
00542    }
00543 
00544    jb->info.frames_in++;
00545 
00546    /* if put into head of queue, caller needs to reschedule */
00547    if (queue_put(jb,data,type,ms,ts)) {
00548       return JB_SCHED;
00549    }
00550    return JB_OK;
00551 }

void jb_reset ( jitterbuf jb  ) 

Definition at line 75 of file jitterbuf.c.

References jb_info::conf, jitterbuf::info, JB_TARGET_EXTRA, and s.

Referenced by complete_transfer(), jb_empty_and_reset_adaptive(), jb_new(), and schedule_delivery().

00076 {
00077    /* only save settings */
00078    jb_conf s = jb->info.conf;
00079    memset(jb, 0, sizeof(*jb));
00080    jb->info.conf = s;
00081 
00082    /* initialize length */
00083    jb->info.current = jb->info.target = JB_TARGET_EXTRA; 
00084    jb->info.silence_begin_ts = -1; 
00085 }

enum jb_return_code jb_setconf ( jitterbuf jb,
jb_conf conf 
)

Definition at line 834 of file jitterbuf.c.

References jb_info::conf, jitterbuf::info, JB_OK, jb_conf::max_contig_interp, jb_conf::max_jitterbuf, and jb_conf::resync_threshold.

Referenced by jb_create_adaptive(), and new_iax().

00835 {
00836    /* take selected settings from the struct */
00837 
00838    jb->info.conf.max_jitterbuf = conf->max_jitterbuf;
00839    jb->info.conf.resync_threshold = conf->resync_threshold;
00840    jb->info.conf.max_contig_interp = conf->max_contig_interp;
00841 
00842    return JB_OK;
00843 }

void jb_setoutput ( jb_output_function_t  err,
jb_output_function_t  warn,
jb_output_function_t  dbg 
)

Definition at line 58 of file jitterbuf.c.

Referenced by iax2_do_jb_debug(), iax2_no_jb_debug(), and load_module().

00059 {
00060    errf = err;
00061    warnf = warn;
00062    dbgf = dbg;
00063 }

static jb_frame* queue_get ( jitterbuf jb,
long  ts 
) [static]

Definition at line 437 of file jitterbuf.c.

References _queue_get().

Referenced by _jb_get().

00438 {
00439    return _queue_get(jb,ts,0);
00440 }

static jb_frame* queue_getall ( jitterbuf jb  )  [static]

Definition at line 442 of file jitterbuf.c.

References _queue_get().

Referenced by jb_getall().

00443 {
00444    return _queue_get(jb,0,1);
00445 }

static long queue_last ( jitterbuf jb  )  [static]

Definition at line 394 of file jitterbuf.c.

References jitterbuf::frames, jb_frame::prev, and jb_frame::ts.

Referenced by _jb_get().

00395 {
00396    if (jb->frames) 
00397       return jb->frames->prev->ts;
00398    else 
00399       return -1;
00400 }

static long queue_next ( jitterbuf jb  )  [static]

Definition at line 386 of file jitterbuf.c.

References jitterbuf::frames, and jb_frame::ts.

Referenced by _jb_get(), and jb_next().

00387 {
00388    if (jb->frames) 
00389       return jb->frames->ts;
00390    else 
00391       return -1;
00392 }

static int queue_put ( jitterbuf jb,
void *  data,
const enum jb_frame_type  type,
long  ms,
long  ts 
) [static]

Definition at line 325 of file jitterbuf.c.

References ast_malloc, frames, jitterbuf::frames, jb_info::frames_cur, jb_info::frames_ooo, jitterbuf::free, jitterbuf::info, jb_err, jb_frame::next, jb_frame::prev, and jb_info::resync_offset.

Referenced by jb_put().

00326 {
00327    jb_frame *frame;
00328    jb_frame *p;
00329    int head = 0;
00330    long resync_ts = ts - jb->info.resync_offset;
00331 
00332    if ((frame = jb->free)) {
00333       jb->free = frame->next;
00334    } else if (!(frame = ast_malloc(sizeof(*frame)))) {
00335       jb_err("cannot allocate frame\n");
00336       return 0;
00337    }
00338 
00339    jb->info.frames_cur++;
00340 
00341    frame->data = data;
00342    frame->ts = resync_ts;
00343    frame->ms = ms;
00344    frame->type = type;
00345 
00346    /* 
00347     * frames are a circular list, jb-frames points to to the lowest ts, 
00348     * jb->frames->prev points to the highest ts
00349     */
00350 
00351    if (!jb->frames) {  /* queue is empty */
00352       jb->frames = frame;
00353       frame->next = frame;
00354       frame->prev = frame;
00355       head = 1;
00356    } else if (resync_ts < jb->frames->ts) {
00357       frame->next = jb->frames;
00358       frame->prev = jb->frames->prev;
00359 
00360       frame->next->prev = frame;
00361       frame->prev->next = frame;
00362 
00363       /* frame is out of order */
00364       jb->info.frames_ooo++;
00365 
00366       jb->frames = frame;
00367       head = 1;
00368    } else { 
00369       p = jb->frames;
00370 
00371       /* frame is out of order */
00372       if (resync_ts < p->prev->ts) jb->info.frames_ooo++;
00373 
00374       while (resync_ts < p->prev->ts && p->prev != jb->frames) 
00375          p = p->prev;
00376 
00377       frame->next = p;
00378       frame->prev = p->prev;
00379 
00380       frame->next->prev = frame;
00381       frame->prev->next = frame;
00382    }
00383    return head;
00384 }


Variable Documentation

jb_output_function_t dbgf [static]

Definition at line 56 of file jitterbuf.c.

jb_output_function_t errf [static]

Definition at line 56 of file jitterbuf.c.

jb_output_function_t warnf [static]

Definition at line 56 of file jitterbuf.c.


Generated on Sat Aug 6 00:39:58 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7