123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849 |
- #include "asterisk.h"
- ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
- #include "jitterbuf.h"
- #include "asterisk/utils.h"
- #define JB_LONGMAX 2147483647L
- #define JB_LONGMIN (-JB_LONGMAX - 1L)
- #define jb_warn(...) (warnf ? warnf(__VA_ARGS__) : (void)0)
- #define jb_err(...) (errf ? errf(__VA_ARGS__) : (void)0)
- #define jb_dbg(...) (dbgf ? dbgf(__VA_ARGS__) : (void)0)
- #ifdef DEEP_DEBUG
- #define jb_dbg2(...) (dbgf ? dbgf(__VA_ARGS__) : (void)0)
- #else
- #define jb_dbg2(...) ((void)0)
- #endif
- static jb_output_function_t warnf, errf, dbgf;
- void jb_setoutput(jb_output_function_t err, jb_output_function_t warn, jb_output_function_t dbg)
- {
- errf = err;
- warnf = warn;
- dbgf = dbg;
- }
- static void increment_losspct(jitterbuf *jb)
- {
- jb->info.losspct = (100000 + 499 * jb->info.losspct)/500;
- }
- static void decrement_losspct(jitterbuf *jb)
- {
- jb->info.losspct = (499 * jb->info.losspct)/500;
- }
- void jb_reset(jitterbuf *jb)
- {
-
- jb_conf s = jb->info.conf;
- jb_frame *fr = jb->free;
- memset(jb, 0, sizeof(*jb));
- jb->info.conf = s;
- jb->free = fr;
-
- jb->info.current = jb->info.target = jb->info.conf.target_extra = JB_TARGET_EXTRA;
- jb->info.silence_begin_ts = -1;
- }
- jitterbuf * jb_new()
- {
- jitterbuf *jb;
- if (!(jb = ast_calloc(1, sizeof(*jb))))
- return NULL;
- jb_reset(jb);
- jb_dbg2("jb_new() = %x\n", jb);
- return jb;
- }
- void jb_destroy(jitterbuf *jb)
- {
- jb_frame *frame;
- jb_dbg2("jb_destroy(%x)\n", jb);
-
- frame = jb->free;
- while (frame != NULL) {
- jb_frame *next = frame->next;
- ast_free(frame);
- frame = next;
- }
-
- ast_free(jb);
- }
- static int check_resync(jitterbuf *jb, long ts, long now, long ms, const enum jb_frame_type type, long *delay)
- {
- long numts = 0;
- long threshold = 2 * jb->info.jitter + jb->info.conf.resync_threshold;
-
- if (jb->frames) {
- numts = jb->frames->prev->ts - jb->frames->ts;
- }
- if (numts >= (jb->info.conf.max_jitterbuf)) {
- if (!jb->dropem) {
- ast_debug(1, "Attempting to exceed Jitterbuf max %ld timeslots\n",
- jb->info.conf.max_jitterbuf);
- jb->dropem = 1;
- }
- jb->info.frames_dropped++;
- return -1;
- } else {
- jb->dropem = 0;
- }
-
- if (jb->info.conf.resync_threshold != -1) {
- if (labs(*delay - jb->info.last_delay) > threshold) {
- jb->info.cnt_delay_discont++;
-
- if ((jb->info.cnt_delay_discont > 3) || (type == JB_TYPE_CONTROL)) {
- jb->info.cnt_delay_discont = 0;
- jb->hist_ptr = 0;
- jb->hist_maxbuf_valid = 0;
- 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);
- jb->info.resync_offset = ts - now;
- jb->info.last_delay = *delay = 0;
- } else {
- jb->info.frames_dropped++;
- return -1;
- }
- } else {
- jb->info.last_delay = *delay;
- jb->info.cnt_delay_discont = 0;
- }
- }
- return 0;
- }
- static int history_put(jitterbuf *jb, long ts, long now, long ms, long delay)
- {
- long kicked;
-
- if (ts <= 0)
- return 0;
- kicked = jb->history[jb->hist_ptr % JB_HISTORY_SZ];
- jb->history[(jb->hist_ptr++) % JB_HISTORY_SZ] = delay;
-
- if (!jb->hist_maxbuf_valid)
- return 0;
-
- if (jb->hist_ptr < JB_HISTORY_SZ)
- goto invalidate;
-
- if (delay < jb->hist_minbuf[JB_HISTORY_MAXBUF_SZ-1])
- goto invalidate;
-
- if (delay > jb->hist_maxbuf[JB_HISTORY_MAXBUF_SZ-1])
- goto invalidate;
-
- if (kicked <= jb->hist_minbuf[JB_HISTORY_MAXBUF_SZ-1])
- goto invalidate;
- if (kicked >= jb->hist_maxbuf[JB_HISTORY_MAXBUF_SZ-1])
- goto invalidate;
-
- return 0;
-
- invalidate:
- jb->hist_maxbuf_valid = 0;
- return 0;
- }
- static void history_calc_maxbuf(jitterbuf *jb)
- {
- int i,j;
- if (jb->hist_ptr == 0)
- return;
-
- for (i=0;i<JB_HISTORY_MAXBUF_SZ;i++) {
- jb->hist_maxbuf[i] = JB_LONGMIN;
- jb->hist_minbuf[i] = JB_LONGMAX;
- }
-
-
-
- i = (jb->hist_ptr > JB_HISTORY_SZ) ? (jb->hist_ptr - JB_HISTORY_SZ) : 0;
- for (;i<jb->hist_ptr;i++) {
- long toins = jb->history[i % JB_HISTORY_SZ];
-
- if (toins > jb->hist_maxbuf[JB_HISTORY_MAXBUF_SZ-1]) {
-
- for (j=0;j<JB_HISTORY_MAXBUF_SZ;j++) {
-
- if (toins > jb->hist_maxbuf[j]) {
-
- if (j != JB_HISTORY_MAXBUF_SZ - 1) {
- memmove(jb->hist_maxbuf + j + 1, jb->hist_maxbuf + j, (JB_HISTORY_MAXBUF_SZ - (j + 1)) * sizeof(jb->hist_maxbuf[0]));
- }
-
- jb->hist_maxbuf[j] = toins;
- break;
- }
- }
- }
-
- if (toins < jb->hist_minbuf[JB_HISTORY_MAXBUF_SZ-1]) {
-
- for (j=0;j<JB_HISTORY_MAXBUF_SZ;j++) {
-
- if (toins < jb->hist_minbuf[j]) {
-
- if (j != JB_HISTORY_MAXBUF_SZ - 1) {
- memmove(jb->hist_minbuf + j + 1, jb->hist_minbuf + j, (JB_HISTORY_MAXBUF_SZ - (j + 1)) * sizeof(jb->hist_minbuf[0]));
- }
-
- jb->hist_minbuf[j] = toins;
- break;
- }
- }
- }
- if (0) {
- int k;
- fprintf(stderr, "toins = %ld\n", toins);
- fprintf(stderr, "maxbuf =");
- for (k=0;k<JB_HISTORY_MAXBUF_SZ;k++)
- fprintf(stderr, "%ld ", jb->hist_maxbuf[k]);
- fprintf(stderr, "\nminbuf =");
- for (k=0;k<JB_HISTORY_MAXBUF_SZ;k++)
- fprintf(stderr, "%ld ", jb->hist_minbuf[k]);
- fprintf(stderr, "\n");
- }
- }
- jb->hist_maxbuf_valid = 1;
- }
- static void history_get(jitterbuf *jb)
- {
- long max, min, jitter;
- int idx;
- int count;
- if (!jb->hist_maxbuf_valid)
- history_calc_maxbuf(jb);
-
- count = (jb->hist_ptr < JB_HISTORY_SZ) ? jb->hist_ptr : JB_HISTORY_SZ;
-
- idx = count * JB_HISTORY_DROPPCT / 100;
-
- if (idx > (JB_HISTORY_MAXBUF_SZ - 1))
- idx = JB_HISTORY_MAXBUF_SZ - 1;
- if (idx < 0) {
- jb->info.min = 0;
- jb->info.jitter = 0;
- return;
- }
- max = jb->hist_maxbuf[idx];
- min = jb->hist_minbuf[idx];
- jitter = max - min;
-
-
- jb->info.min = min;
- jb->info.jitter = jitter;
- }
- static int queue_put(jitterbuf *jb, void *data, const enum jb_frame_type type, long ms, long ts)
- {
- jb_frame *frame;
- jb_frame *p;
- int head = 0;
- long resync_ts = ts - jb->info.resync_offset;
- if ((frame = jb->free)) {
- jb->free = frame->next;
- } else if (!(frame = ast_malloc(sizeof(*frame)))) {
- jb_err("cannot allocate frame\n");
- return 0;
- }
- jb->info.frames_cur++;
- frame->data = data;
- frame->ts = resync_ts;
- frame->ms = ms;
- frame->type = type;
-
- if (!jb->frames) {
- jb->frames = frame;
- frame->next = frame;
- frame->prev = frame;
- head = 1;
- } else if (resync_ts < jb->frames->ts) {
- frame->next = jb->frames;
- frame->prev = jb->frames->prev;
- frame->next->prev = frame;
- frame->prev->next = frame;
-
- jb->info.frames_ooo++;
- jb->frames = frame;
- head = 1;
- } else {
- p = jb->frames;
-
- if (resync_ts < p->prev->ts) jb->info.frames_ooo++;
- while (resync_ts < p->prev->ts && p->prev != jb->frames)
- p = p->prev;
- frame->next = p;
- frame->prev = p->prev;
- frame->next->prev = frame;
- frame->prev->next = frame;
- }
- return head;
- }
- static long queue_next(jitterbuf *jb)
- {
- if (jb->frames)
- return jb->frames->ts;
- else
- return -1;
- }
- static long queue_last(jitterbuf *jb)
- {
- if (jb->frames)
- return jb->frames->prev->ts;
- else
- return -1;
- }
- static jb_frame *_queue_get(jitterbuf *jb, long ts, int all)
- {
- jb_frame *frame;
- frame = jb->frames;
- if (!frame)
- return NULL;
-
- if (all || ts >= frame->ts) {
-
- frame->prev->next = frame->next;
- frame->next->prev = frame->prev;
- if (frame->next == frame)
- jb->frames = NULL;
- else
- jb->frames = frame->next;
-
- frame->next = jb->free;
- jb->free = frame;
- jb->info.frames_cur--;
-
- return frame;
- }
- return NULL;
- }
- static jb_frame *queue_get(jitterbuf *jb, long ts)
- {
- return _queue_get(jb,ts,0);
- }
- static jb_frame *queue_getall(jitterbuf *jb)
- {
- return _queue_get(jb,0,1);
- }
- #if 0
- static void jb_dbginfo(jitterbuf *jb)
- {
- if (dbgf == NULL)
- return;
- jb_dbg("\njb info: fin=%ld fout=%ld flate=%ld flost=%ld fdrop=%ld fcur=%ld\n",
- jb->info.frames_in, jb->info.frames_out, jb->info.frames_late, jb->info.frames_lost, jb->info.frames_dropped, jb->info.frames_cur);
- jb_dbg("jitter=%ld current=%ld target=%ld min=%ld sil=%d len=%d len/fcur=%ld\n",
- jb->info.jitter, jb->info.current, jb->info.target, jb->info.min, jb->info.silence_begin_ts, jb->info.current - jb->info.min,
- jb->info.frames_cur ? (jb->info.current - jb->info.min)/jb->info.frames_cur : -8);
- if (jb->info.frames_in > 0)
- jb_dbg("jb info: Loss PCT = %ld%%, Late PCT = %ld%%\n",
- jb->info.frames_lost * 100/(jb->info.frames_in + jb->info.frames_lost),
- jb->info.frames_late * 100/jb->info.frames_in);
- jb_dbg("jb info: queue %d -> %d. last_ts %d (queue len: %d) last_ms %d\n",
- queue_next(jb),
- queue_last(jb),
- jb->info.next_voice_ts,
- queue_last(jb) - queue_next(jb),
- jb->info.last_voice_ms);
- }
- #endif
- #ifdef DEEP_DEBUG
- static void jb_chkqueue(jitterbuf *jb)
- {
- int i=0;
- jb_frame *p = jb->frames;
- if (!p) {
- return;
- }
- do {
- if (p->next == NULL) {
- jb_err("Queue is BROKEN at item [%d]", i);
- }
- i++;
- p=p->next;
- } while (p->next != jb->frames);
- }
- static void jb_dbgqueue(jitterbuf *jb)
- {
- int i=0;
- jb_frame *p = jb->frames;
- jb_dbg("queue: ");
- if (!p) {
- jb_dbg("EMPTY\n");
- return;
- }
- do {
- jb_dbg("[%d]=%ld ", i++, p->ts);
- p=p->next;
- } while (p->next != jb->frames);
- jb_dbg("\n");
- }
- #endif
- enum jb_return_code jb_put(jitterbuf *jb, void *data, const enum jb_frame_type type, long ms, long ts, long now)
- {
- long delay = now - (ts - jb->info.resync_offset);
- jb_dbg2("jb_put(%x,%x,%ld,%ld,%ld)\n", jb, data, ms, ts, now);
- if (check_resync(jb, ts, now, ms, type, &delay)) {
- return JB_DROP;
- }
- if (type == JB_TYPE_VOICE) {
-
- history_put(jb, ts, now, ms, delay);
- }
- jb->info.frames_in++;
-
- if (queue_put(jb,data,type,ms,ts)) {
- return JB_SCHED;
- }
- return JB_OK;
- }
- static enum jb_return_code _jb_get(jitterbuf *jb, jb_frame *frameout, long now, long interpl)
- {
- jb_frame *frame;
- long diff;
- static int dbg_cnt = 0;
-
- history_get(jb);
- if (dbg_cnt && dbg_cnt % 50 == 0) {
- jb_dbg("\n");
- }
- dbg_cnt++;
-
- jb->info.target = jb->info.jitter + jb->info.min + jb->info.conf.target_extra;
-
- if ((jb->info.conf.max_jitterbuf) && ((jb->info.target - jb->info.min) > jb->info.conf.max_jitterbuf)) {
- jb_dbg("clamping target from %ld to %ld\n", (jb->info.target - jb->info.min), jb->info.conf.max_jitterbuf);
- jb->info.target = jb->info.min + jb->info.conf.max_jitterbuf;
- }
- diff = jb->info.target - jb->info.current;
-
-
-
- if (!jb->info.silence_begin_ts) {
-
- if ((diff > 0) &&
-
- (((jb->info.last_adjustment + JB_ADJUST_DELAY) < now) ||
-
- (diff > queue_last(jb) - queue_next(jb)) ) ) {
-
- jb->info.current += interpl;
- jb->info.next_voice_ts += interpl;
- jb->info.last_voice_ms = interpl;
- jb->info.last_adjustment = now;
- jb->info.cnt_contig_interp++;
- if (jb->info.conf.max_contig_interp && jb->info.cnt_contig_interp >= jb->info.conf.max_contig_interp) {
- jb->info.silence_begin_ts = jb->info.next_voice_ts - jb->info.current;
- }
- jb_dbg("G");
- return JB_INTERP;
- }
- frame = queue_get(jb, jb->info.next_voice_ts - jb->info.current);
-
- if (frame && frame->type != JB_TYPE_VOICE) {
- if (frame->type == JB_TYPE_SILENCE) {
- jb->info.silence_begin_ts = frame->ts;
- jb->info.cnt_contig_interp = 0;
- }
- *frameout = *frame;
- jb->info.frames_out++;
- jb_dbg("o");
- return JB_OK;
- }
-
- if (frame && frame->ts + jb->info.current < jb->info.next_voice_ts) {
- if (frame->ts + jb->info.current > jb->info.next_voice_ts - jb->info.last_voice_ms) {
-
-
- *frameout = *frame;
-
- jb->info.next_voice_ts = frame->ts + jb->info.current + frame->ms;
- jb->info.frames_out++;
- decrement_losspct(jb);
- jb->info.cnt_contig_interp = 0;
- jb_dbg("v");
- return JB_OK;
- } else {
-
- *frameout = *frame;
- jb->info.frames_out++;
- decrement_losspct(jb);
- jb->info.frames_late++;
- jb->info.frames_lost--;
- jb_dbg("l");
- return JB_DROP;
- }
- }
-
- if (frame && frame->ms > 0) {
- jb->info.last_voice_ms = frame->ms;
- }
-
-
-
-
- if (diff < -jb->info.conf.target_extra &&
- ((!frame && jb->info.last_adjustment + 80 < now) ||
- (jb->info.last_adjustment + 500 < now))) {
- jb->info.last_adjustment = now;
- jb->info.cnt_contig_interp = 0;
- if (frame) {
- *frameout = *frame;
-
- jb->info.current -= frame->ms;
- jb->info.frames_out++;
- decrement_losspct(jb);
- jb->info.frames_dropped++;
- jb_dbg("s");
- return JB_DROP;
- } else {
-
- jb->info.current -= jb->info.last_voice_ms;
- jb->info.frames_lost++;
- increment_losspct(jb);
- jb_dbg("S");
- return JB_NOFRAME;
- }
- }
-
- if (!frame) {
-
-
-
-
- jb->info.frames_lost++;
- increment_losspct(jb);
- jb->info.next_voice_ts += interpl;
- jb->info.last_voice_ms = interpl;
- jb->info.cnt_contig_interp++;
- if (jb->info.conf.max_contig_interp && jb->info.cnt_contig_interp >= jb->info.conf.max_contig_interp) {
- jb->info.silence_begin_ts = jb->info.next_voice_ts - jb->info.current;
- }
- jb_dbg("L");
- return JB_INTERP;
- }
-
- *frameout = *frame;
- jb->info.next_voice_ts += frame->ms;
- jb->info.frames_out++;
- jb->info.cnt_contig_interp = 0;
- decrement_losspct(jb);
- jb_dbg("v");
- return JB_OK;
- } else {
-
-
-
-
- if (diff < -jb->info.conf.target_extra &&
- jb->info.last_adjustment + 10 <= now) {
- jb->info.current -= interpl;
- jb->info.last_adjustment = now;
- }
- frame = queue_get(jb, now - jb->info.current);
- if (!frame) {
- return JB_NOFRAME;
- } else if (frame->type != JB_TYPE_VOICE) {
-
- *frameout = *frame;
- jb->info.frames_out++;
- return JB_OK;
- }
- if (frame->ts < jb->info.silence_begin_ts) {
-
- *frameout = *frame;
- jb->info.frames_out++;
- decrement_losspct(jb);
- jb->info.frames_late++;
- jb->info.frames_lost--;
- jb_dbg("l");
- return JB_DROP;
- } else {
-
-
- jb->info.current = jb->info.target;
- jb->info.silence_begin_ts = 0;
- jb->info.next_voice_ts = frame->ts + jb->info.current + frame->ms;
- jb->info.last_voice_ms = frame->ms;
- jb->info.frames_out++;
- decrement_losspct(jb);
- *frameout = *frame;
- jb_dbg("V");
- return JB_OK;
- }
- }
- }
- long jb_next(jitterbuf *jb)
- {
- if (jb->info.silence_begin_ts) {
- if (jb->frames) {
- long next = queue_next(jb);
- history_get(jb);
-
- if (jb->info.target - jb->info.current < -jb->info.conf.target_extra)
- return jb->info.last_adjustment + 10;
- return next + jb->info.target;
- }
- else
- return JB_LONGMAX;
- } else {
- return jb->info.next_voice_ts;
- }
- }
- enum jb_return_code jb_get(jitterbuf *jb, jb_frame *frameout, long now, long interpl)
- {
- enum jb_return_code ret = _jb_get(jb, frameout, now, interpl);
- #if 0
- static int lastts=0;
- int thists = ((ret == JB_OK) || (ret == JB_DROP)) ? frameout->ts : 0;
- jb_warn("jb_get(%x,%x,%ld) = %d (%d)\n", jb, frameout, now, ret, thists);
- if (thists && thists < lastts) jb_warn("XXXX timestamp roll-back!!!\n");
- lastts = thists;
- #endif
- if (ret == JB_INTERP)
- frameout->ms = jb->info.last_voice_ms;
- return ret;
- }
- enum jb_return_code jb_getall(jitterbuf *jb, jb_frame *frameout)
- {
- jb_frame *frame;
- frame = queue_getall(jb);
- if (!frame) {
- return JB_NOFRAME;
- }
- *frameout = *frame;
- return JB_OK;
- }
- enum jb_return_code jb_getinfo(jitterbuf *jb, jb_info *stats)
- {
- history_get(jb);
- *stats = jb->info;
- return JB_OK;
- }
- enum jb_return_code jb_setconf(jitterbuf *jb, jb_conf *conf)
- {
-
- jb->info.conf.max_jitterbuf = conf->max_jitterbuf;
- jb->info.conf.resync_threshold = conf->resync_threshold;
- jb->info.conf.max_contig_interp = conf->max_contig_interp;
-
- jb->info.conf.target_extra = ( conf->target_extra == -1 )
- ? JB_TARGET_EXTRA
- : conf->target_extra
- ;
-
- jb->info.current = jb->info.conf.target_extra;
- jb->info.target = jb->info.conf.target_extra;
- return JB_OK;
- }
|