Thu Jul 9 13:41:22 2009

Asterisk developer's documentation


jitterbuf.c File Reference

jitterbuf: an application-independent jitterbuffer More...

#include "asterisk.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)
 destroy jitterbuf
enum jb_return_code jb_get (jitterbuf *jb, jb_frame *frameout, long now, long interpl)
 get a frame for time now (receiver's time) return value is one of JB_OK: You've got frame! JB_DROP: Here's an audio frame you should just drop. Ask me again for this time.. JB_NOFRAME: There's no frame scheduled for this time. JB_INTERP: Please interpolate an interpl-length frame for this time (either we need to grow, or there was a lost frame) JB_EMPTY: The jb is empty.
enum jb_return_code jb_getall (jitterbuf *jb, jb_frame *frameout)
 unconditionally get frames from jitterbuf until empty
enum jb_return_code jb_getinfo (jitterbuf *jb, jb_info *stats)
 get jitterbuf info: only "statistics" may be valid
jitterbufjb_new ()
 new jitterbuf
long jb_next (jitterbuf *jb)
 when is the next frame due out, in receiver's time (0=EMPTY) This value may change as frames are added (esp non-audio frames)
enum jb_return_code jb_put (jitterbuf *jb, void *data, const enum jb_frame_type type, long ms, long ts, long now)
 queue a frame
void jb_reset (jitterbuf *jb)
 reset jitterbuf
enum jb_return_code jb_setconf (jitterbuf *jb, jb_conf *conf)
 set jitterbuf 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 43 of file jitterbuf.c.

Referenced by _jb_get().

 
#define jb_dbg2 (  )     ((void)0)

Definition at line 48 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 42 of file jitterbuf.c.

Referenced by queue_put().

#define JB_LONGMAX   2147483647L

define these here, just for ancient compiler systems

Definition at line 38 of file jitterbuf.c.

Referenced by history_calc_maxbuf(), and jb_next().

#define JB_LONGMIN   (-JB_LONGMAX - 1L)

Definition at line 39 of file jitterbuf.c.

Referenced by history_calc_maxbuf().

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

Definition at line 41 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 549 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_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_conf::target_extra, jb_frame::ts, and jb_frame::type.

Referenced by jb_get().

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

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

Definition at line 397 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().

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

static void decrement_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 = (499 * jb->info.losspct)/500;    
00068 }

static void history_calc_maxbuf ( jitterbuf jb  )  [static]

Definition at line 200 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().

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

static void history_get ( jitterbuf jb  )  [static]

Definition at line 277 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().

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

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 125 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().

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

static void increment_losspct ( jitterbuf jb  )  [static]

Definition at line 60 of file jitterbuf.c.

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

Referenced by _jb_get().

00061 {
00062    jb->info.losspct = (100000 + 499 * jb->info.losspct)/500;    
00063 }

void jb_destroy ( jitterbuf jb  ) 

destroy jitterbuf

Definition at line 95 of file jitterbuf.c.

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

Referenced by jb_destroy_adaptive(), and pvt_destructor().

00096 {
00097    jb_frame *frame; 
00098    jb_dbg2("jb_destroy(%x)\n", jb);
00099 
00100    /* free all the frames on the "free list" */
00101    frame = jb->free;
00102    while (frame != NULL) {
00103       jb_frame *next = frame->next;
00104       ast_free(frame);
00105       frame = next;
00106    }
00107 
00108    /* free ourselves! */ 
00109    ast_free(jb);
00110 }

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

get a frame for time now (receiver's time) return value is one of JB_OK: You've got frame! JB_DROP: Here's an audio frame you should just drop. Ask me again for this time.. JB_NOFRAME: There's no frame scheduled for this time. JB_INTERP: Please interpolate an interpl-length frame for this time (either we need to grow, or there was a lost frame) JB_EMPTY: The jb is empty.

Definition at line 789 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().

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

enum jb_return_code jb_getall ( jitterbuf jb,
jb_frame frameout 
)

unconditionally get frames from jitterbuf until empty

Definition at line 805 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().

00806 {
00807    jb_frame *frame;
00808    frame = queue_getall(jb);
00809 
00810    if (!frame) {
00811       return JB_NOFRAME;
00812    }
00813 
00814    *frameout = *frame;
00815    return JB_OK;
00816 }

enum jb_return_code jb_getinfo ( jitterbuf jb,
jb_info stats 
)

get jitterbuf info: only "statistics" may be valid

Definition at line 819 of file jitterbuf.c.

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

Referenced by ast_cli_netstats(), construct_rr(), handle_cli_iax2_show_channels(), and log_jitterstats().

00820 {
00821 
00822    history_get(jb);
00823 
00824    *stats = jb->info;
00825 
00826    return JB_OK;
00827 }

jitterbuf* jb_new ( void   ) 

new jitterbuf

Definition at line 82 of file jitterbuf.c.

References ast_malloc, jb_dbg2, and jb_reset().

Referenced by jb_create_adaptive(), and new_iax().

00083 {
00084    jitterbuf *jb;
00085 
00086    if (!(jb = ast_malloc(sizeof(*jb)))) 
00087       return NULL;
00088 
00089    jb_reset(jb);
00090 
00091    jb_dbg2("jb_new() = %x\n", jb);
00092    return jb;
00093 }

long jb_next ( jitterbuf jb  ) 

when is the next frame due out, in receiver's time (0=EMPTY) This value may change as frames are added (esp non-audio frames)

Definition at line 771 of file jitterbuf.c.

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

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

00772 {
00773    if (jb->info.silence_begin_ts) {
00774       if (jb->frames) {
00775          long next = queue_next(jb);
00776          history_get(jb);
00777          /* shrink during silence */
00778          if (jb->info.target - jb->info.current < -jb->info.conf.target_extra)
00779             return jb->info.last_adjustment + 10;
00780          return next + jb->info.target;
00781       }
00782       else 
00783          return JB_LONGMAX;
00784    } else {
00785       return jb->info.next_voice_ts;
00786    }
00787 }

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

queue a frame

data=frame data, timings (in ms): ms=length of frame (for voice), ts=ts (sender's time) now=now (in receiver's time) return value is one of JB_OK: Frame added. Last call to jb_next() still valid JB_DROP: Drop this frame immediately JB_SCHED: Frame added. Call jb_next() to get a new time for the next frame

Definition at line 508 of file jitterbuf.c.

References ast_debug, 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, jb_conf::max_jitterbuf, jb_frame::prev, queue_put(), and jb_frame::ts.

Referenced by jb_put_adaptive(), and schedule_delivery().

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

void jb_reset ( jitterbuf jb  ) 

reset jitterbuf

Note:
The jitterbuffer should be empty before you call this, otherwise you will leak queued frames, and some internal structures

Definition at line 70 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().

00071 {
00072    /* only save settings */
00073    jb_conf s = jb->info.conf;
00074    memset(jb, 0, sizeof(*jb));
00075    jb->info.conf = s;
00076 
00077    /* initialize length, using the default value */
00078    jb->info.current = jb->info.target = jb->info.conf.target_extra = JB_TARGET_EXTRA;
00079    jb->info.silence_begin_ts = -1; 
00080 }

enum jb_return_code jb_setconf ( jitterbuf jb,
jb_conf conf 
)

set jitterbuf conf

Definition at line 829 of file jitterbuf.c.

References jb_info::conf, jb_info::current, jitterbuf::info, JB_OK, JB_TARGET_EXTRA, jb_conf::max_contig_interp, jb_conf::max_jitterbuf, jb_conf::resync_threshold, jb_info::target, and jb_conf::target_extra.

Referenced by jb_create_adaptive(), and new_iax().

00830 {
00831    /* take selected settings from the struct */
00832 
00833    jb->info.conf.max_jitterbuf = conf->max_jitterbuf;
00834    jb->info.conf.resync_threshold = conf->resync_threshold;
00835    jb->info.conf.max_contig_interp = conf->max_contig_interp;
00836 
00837    /* -1 indicates use of the default JB_TARGET_EXTRA value */
00838    jb->info.conf.target_extra = ( conf->target_extra == -1 )
00839       ? JB_TARGET_EXTRA
00840       : conf->target_extra
00841       ;
00842       
00843    /* update these to match new target_extra setting */
00844    jb->info.current = jb->info.conf.target_extra;
00845    jb->info.target = jb->info.conf.target_extra;
00846 
00847    return JB_OK;
00848 }

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

Definition at line 53 of file jitterbuf.c.

Referenced by handle_cli_iax2_set_debug_jb(), handle_cli_iax2_set_debug_jb_deprecated(), and load_module().

00054 {
00055    errf = err;
00056    warnf = warn;
00057    dbgf = dbg;
00058 }

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

Definition at line 432 of file jitterbuf.c.

References _queue_get().

Referenced by _jb_get().

00433 {
00434    return _queue_get(jb,ts,0);
00435 }

static jb_frame* queue_getall ( jitterbuf jb  )  [static]

Definition at line 437 of file jitterbuf.c.

References _queue_get().

Referenced by jb_getall().

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

static long queue_last ( jitterbuf jb  )  [static]

Definition at line 389 of file jitterbuf.c.

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

Referenced by _jb_get().

00390 {
00391    if (jb->frames) 
00392       return jb->frames->prev->ts;
00393    else 
00394       return -1;
00395 }

static long queue_next ( jitterbuf jb  )  [static]

Definition at line 381 of file jitterbuf.c.

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

Referenced by _jb_get(), and jb_next().

00382 {
00383    if (jb->frames) 
00384       return jb->frames->ts;
00385    else 
00386       return -1;
00387 }

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

Definition at line 320 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().

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


Variable Documentation

jb_output_function_t dbgf [static]

Definition at line 51 of file jitterbuf.c.

jb_output_function_t errf [static]

Definition at line 51 of file jitterbuf.c.

jb_output_function_t warnf [static]

Definition at line 51 of file jitterbuf.c.


Generated on Thu Jul 9 13:41:22 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7