2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2004-2005, Horizon Wimba, Inc.
7 * Steve Kann <stevek@stevek.com>
9 * A license has been granted to Digium (via disclaimer) for the use of
12 * See http://www.asterisk.org for more information about
13 * the Asterisk project. Please do not directly contact
14 * any of the maintainers of this project for assistance;
15 * the project provides a web site, mailing lists and IRC
16 * channels for your use.
18 * This program is free software, distributed under the terms of
19 * the GNU General Public License Version 2. See the LICENSE file
20 * at the top of the source tree.
25 * \brief jitterbuf: an application-independent jitterbuffer
26 * \author Steve Kann <stevek@stevek.com>
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37 #include <sys/types.h>
39 #include "jitterbuf.h"
40 #include "asterisk/utils.h"
41 #include "asterisk/options.h"
43 /*! define these here, just for ancient compiler systems */
44 #define JB_LONGMAX 2147483647L
45 #define JB_LONGMIN (-JB_LONGMAX - 1L)
47 #define jb_warn(...) (warnf ? warnf(__VA_ARGS__) : (void)0)
48 #define jb_err(...) (errf ? errf(__VA_ARGS__) : (void)0)
49 #define jb_dbg(...) (dbgf ? dbgf(__VA_ARGS__) : (void)0)
52 #define jb_dbg2(...) (dbgf ? dbgf(__VA_ARGS__) : (void)0)
54 #define jb_dbg2(...) ((void)0)
57 static jb_output_function_t warnf, errf, dbgf;
59 void jb_setoutput(jb_output_function_t err, jb_output_function_t warn, jb_output_function_t dbg)
66 static void increment_losspct(jitterbuf *jb)
68 jb->info.losspct = (100000 + 499 * jb->info.losspct)/500;
71 static void decrement_losspct(jitterbuf *jb)
73 jb->info.losspct = (499 * jb->info.losspct)/500;
76 void jb_reset(jitterbuf *jb)
78 /* only save settings */
79 jb_conf s = jb->info.conf;
80 memset(jb, 0, sizeof(*jb));
83 /* initialize length, using the default value */
84 jb->info.current = jb->info.target = jb->info.conf.target_extra = JB_TARGET_EXTRA;
85 jb->info.silence_begin_ts = -1;
92 if (!(jb = ast_malloc(sizeof(*jb))))
97 jb_dbg2("jb_new() = %x\n", jb);
101 void jb_destroy(jitterbuf *jb)
104 jb_dbg2("jb_destroy(%x)\n", jb);
106 /* free all the frames on the "free list" */
108 while (frame != NULL) {
109 jb_frame *next = frame->next;
114 /* free ourselves! */
121 static int longcmp(const void *a, const void *b)
123 return *(long *)a - *(long *)b;
127 /*! \brief simple history manipulation
128 \note maybe later we can make the history buckets variable size, or something? */
129 /* drop parameter determines whether we will drop outliers to minimize
131 static int history_put(jitterbuf *jb, long ts, long now, long ms)
133 long delay = now - (ts - jb->info.resync_offset);
134 long threshold = 2 * jb->info.jitter + jb->info.conf.resync_threshold;
137 /* don't add special/negative times to history */
141 /* check for drastic change in delay */
142 if (jb->info.conf.resync_threshold != -1) {
143 if (abs(delay - jb->info.last_delay) > threshold) {
144 jb->info.cnt_delay_discont++;
145 if (jb->info.cnt_delay_discont > 3) {
146 /* resync the jitterbuffer */
147 jb->info.cnt_delay_discont = 0;
149 jb->hist_maxbuf_valid = 0;
151 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);
152 jb->info.resync_offset = ts - now;
153 jb->info.last_delay = delay = 0; /* after resync, frame is right on time */
158 jb->info.last_delay = delay;
159 jb->info.cnt_delay_discont = 0;
163 kicked = jb->history[jb->hist_ptr % JB_HISTORY_SZ];
165 jb->history[(jb->hist_ptr++) % JB_HISTORY_SZ] = delay;
167 /* optimization; the max/min buffers don't need to be recalculated, if this packet's
168 * entry doesn't change them. This happens if this packet is not involved, _and_ any packet
169 * that got kicked out of the history is also not involved
170 * We do a number of comparisons, but it's probably still worthwhile, because it will usually
171 * succeed, and should be a lot faster than going through all 500 packets in history */
172 if (!jb->hist_maxbuf_valid)
175 /* don't do this until we've filled history
176 * (reduces some edge cases below) */
177 if (jb->hist_ptr < JB_HISTORY_SZ)
180 /* if the new delay would go into min */
181 if (delay < jb->hist_minbuf[JB_HISTORY_MAXBUF_SZ-1])
185 if (delay > jb->hist_maxbuf[JB_HISTORY_MAXBUF_SZ-1])
188 /* or the kicked delay would be in min */
189 if (kicked <= jb->hist_minbuf[JB_HISTORY_MAXBUF_SZ-1])
192 if (kicked >= jb->hist_maxbuf[JB_HISTORY_MAXBUF_SZ-1])
195 /* if we got here, we don't need to invalidate, 'cause this delay didn't
198 /* end optimization */
202 jb->hist_maxbuf_valid = 0;
206 static void history_calc_maxbuf(jitterbuf *jb)
210 if (jb->hist_ptr == 0)
214 /* initialize maxbuf/minbuf to the latest value */
215 for (i=0;i<JB_HISTORY_MAXBUF_SZ;i++) {
217 * jb->hist_maxbuf[i] = jb->history[(jb->hist_ptr-1) % JB_HISTORY_SZ];
218 * jb->hist_minbuf[i] = jb->history[(jb->hist_ptr-1) % JB_HISTORY_SZ];
220 jb->hist_maxbuf[i] = JB_LONGMIN;
221 jb->hist_minbuf[i] = JB_LONGMAX;
224 /* use insertion sort to populate maxbuf */
225 /* we want it to be the top "n" values, in order */
227 /* start at the beginning, or JB_HISTORY_SZ frames ago */
228 i = (jb->hist_ptr > JB_HISTORY_SZ) ? (jb->hist_ptr - JB_HISTORY_SZ) : 0;
230 for (;i<jb->hist_ptr;i++) {
231 long toins = jb->history[i % JB_HISTORY_SZ];
233 /* if the maxbuf should get this */
234 if (toins > jb->hist_maxbuf[JB_HISTORY_MAXBUF_SZ-1]) {
236 /* insertion-sort it into the maxbuf */
237 for (j=0;j<JB_HISTORY_MAXBUF_SZ;j++) {
238 /* found where it fits */
239 if (toins > jb->hist_maxbuf[j]) {
241 memmove(jb->hist_maxbuf + j + 1, jb->hist_maxbuf + j, (JB_HISTORY_MAXBUF_SZ - (j + 1)) * sizeof(jb->hist_maxbuf[0]));
243 jb->hist_maxbuf[j] = toins;
250 /* if the minbuf should get this */
251 if (toins < jb->hist_minbuf[JB_HISTORY_MAXBUF_SZ-1]) {
253 /* insertion-sort it into the maxbuf */
254 for (j=0;j<JB_HISTORY_MAXBUF_SZ;j++) {
255 /* found where it fits */
256 if (toins < jb->hist_minbuf[j]) {
258 memmove(jb->hist_minbuf + j + 1, jb->hist_minbuf + j, (JB_HISTORY_MAXBUF_SZ - (j + 1)) * sizeof(jb->hist_minbuf[0]));
260 jb->hist_minbuf[j] = toins;
269 fprintf(stderr, "toins = %ld\n", toins);
270 fprintf(stderr, "maxbuf =");
271 for (k=0;k<JB_HISTORY_MAXBUF_SZ;k++)
272 fprintf(stderr, "%ld ", jb->hist_maxbuf[k]);
273 fprintf(stderr, "\nminbuf =");
274 for (k=0;k<JB_HISTORY_MAXBUF_SZ;k++)
275 fprintf(stderr, "%ld ", jb->hist_minbuf[k]);
276 fprintf(stderr, "\n");
280 jb->hist_maxbuf_valid = 1;
283 static void history_get(jitterbuf *jb)
285 long max, min, jitter;
289 if (!jb->hist_maxbuf_valid)
290 history_calc_maxbuf(jb);
292 /* count is how many items in history we're examining */
293 count = (jb->hist_ptr < JB_HISTORY_SZ) ? jb->hist_ptr : JB_HISTORY_SZ;
295 /* index is the "n"ths highest/lowest that we'll look for */
296 index = count * JB_HISTORY_DROPPCT / 100;
298 /* sanity checks for index */
299 if (index > (JB_HISTORY_MAXBUF_SZ - 1))
300 index = JB_HISTORY_MAXBUF_SZ - 1;
309 max = jb->hist_maxbuf[index];
310 min = jb->hist_minbuf[index];
314 /* these debug stmts compare the difference between looking at the absolute jitter, and the
315 * values we get by throwing away the outliers */
317 fprintf(stderr, "[%d] min=%d, max=%d, jitter=%d\n", index, min, max, jitter);
318 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]);
322 jb->info.jitter = jitter;
325 /* returns 1 if frame was inserted into head of queue, 0 otherwise */
326 static int queue_put(jitterbuf *jb, void *data, const enum jb_frame_type type, long ms, long ts)
331 long resync_ts = ts - jb->info.resync_offset;
333 if ((frame = jb->free)) {
334 jb->free = frame->next;
335 } else if (!(frame = ast_malloc(sizeof(*frame)))) {
336 jb_err("cannot allocate frame\n");
340 jb->info.frames_cur++;
343 frame->ts = resync_ts;
348 * frames are a circular list, jb-frames points to to the lowest ts,
349 * jb->frames->prev points to the highest ts
352 if (!jb->frames) { /* queue is empty */
357 } else if (resync_ts < jb->frames->ts) {
358 frame->next = jb->frames;
359 frame->prev = jb->frames->prev;
361 frame->next->prev = frame;
362 frame->prev->next = frame;
364 /* frame is out of order */
365 jb->info.frames_ooo++;
372 /* frame is out of order */
373 if (resync_ts < p->prev->ts) jb->info.frames_ooo++;
375 while (resync_ts < p->prev->ts && p->prev != jb->frames)
379 frame->prev = p->prev;
381 frame->next->prev = frame;
382 frame->prev->next = frame;
387 static long queue_next(jitterbuf *jb)
390 return jb->frames->ts;
395 static long queue_last(jitterbuf *jb)
398 return jb->frames->prev->ts;
403 static jb_frame *_queue_get(jitterbuf *jb, long ts, int all)
411 /*jb_warn("queue_get: ASK %ld FIRST %ld\n", ts, frame->ts); */
413 if (all || ts >= frame->ts) {
414 /* remove this frame */
415 frame->prev->next = frame->next;
416 frame->next->prev = frame->prev;
418 if (frame->next == frame)
421 jb->frames = frame->next;
424 /* insert onto "free" single-linked list */
425 frame->next = jb->free;
428 jb->info.frames_cur--;
430 /* we return the frame pointer, even though it's on free list,
431 * but caller must copy data */
438 static jb_frame *queue_get(jitterbuf *jb, long ts)
440 return _queue_get(jb,ts,0);
443 static jb_frame *queue_getall(jitterbuf *jb)
445 return _queue_get(jb,0,1);
449 /* some diagnostics */
450 static void jb_dbginfo(jitterbuf *jb)
455 jb_dbg("\njb info: fin=%ld fout=%ld flate=%ld flost=%ld fdrop=%ld fcur=%ld\n",
456 jb->info.frames_in, jb->info.frames_out, jb->info.frames_late, jb->info.frames_lost, jb->info.frames_dropped, jb->info.frames_cur);
458 jb_dbg("jitter=%ld current=%ld target=%ld min=%ld sil=%d len=%d len/fcur=%ld\n",
459 jb->info.jitter, jb->info.current, jb->info.target, jb->info.min, jb->info.silence_begin_ts, jb->info.current - jb->info.min,
460 jb->info.frames_cur ? (jb->info.current - jb->info.min)/jb->info.frames_cur : -8);
461 if (jb->info.frames_in > 0)
462 jb_dbg("jb info: Loss PCT = %ld%%, Late PCT = %ld%%\n",
463 jb->info.frames_lost * 100/(jb->info.frames_in + jb->info.frames_lost),
464 jb->info.frames_late * 100/jb->info.frames_in);
465 jb_dbg("jb info: queue %d -> %d. last_ts %d (queue len: %d) last_ms %d\n",
468 jb->info.next_voice_ts,
469 queue_last(jb) - queue_next(jb),
470 jb->info.last_voice_ms);
475 static void jb_chkqueue(jitterbuf *jb)
478 jb_frame *p = jb->frames;
485 if (p->next == NULL) {
486 jb_err("Queue is BROKEN at item [%d]", i);
490 } while (p->next != jb->frames);
493 static void jb_dbgqueue(jitterbuf *jb)
496 jb_frame *p = jb->frames;
506 jb_dbg("[%d]=%ld ", i++, p->ts);
508 } while (p->next != jb->frames);
514 enum jb_return_code jb_put(jitterbuf *jb, void *data, const enum jb_frame_type type, long ms, long ts, long now)
518 jb_dbg2("jb_put(%x,%x,%ld,%ld,%ld)\n", jb, data, ms, ts, now);
520 jb->info.frames_in++;
522 if (jb->frames && jb->dropem)
526 if (type == JB_TYPE_VOICE) {
527 /* presently, I'm only adding VOICE frames to history and drift calculations; mostly because with the
528 * IAX integrations, I'm sending retransmitted control frames with their awkward timestamps through */
529 if (history_put(jb,ts,now,ms))
534 numts = jb->frames->prev->ts - jb->frames->ts;
535 if (numts >= jb->info.conf.max_jitterbuf) {
536 ast_debug(1, "Attempting to exceed Jitterbuf max %ld timeslots\n",
537 jb->info.conf.max_jitterbuf);
541 /* if put into head of queue, caller needs to reschedule */
542 if (queue_put(jb,data,type,ms,ts)) {
549 static enum jb_return_code _jb_get(jitterbuf *jb, jb_frame *frameout, long now, long interpl)
553 static int dbg_cnt = 0;
555 /*if ((now - jb_next(jb)) > 2 * jb->info.last_voice_ms) jb_warn("SCHED: %ld", (now - jb_next(jb))); */
556 /* get jitter info */
559 if (dbg_cnt && dbg_cnt % 50 == 0) {
565 jb->info.target = jb->info.jitter + jb->info.min + jb->info.conf.target_extra;
567 /* if a hard clamp was requested, use it */
568 if ((jb->info.conf.max_jitterbuf) && ((jb->info.target - jb->info.min) > jb->info.conf.max_jitterbuf)) {
569 jb_dbg("clamping target from %d to %d\n", (jb->info.target - jb->info.min), jb->info.conf.max_jitterbuf);
570 jb->info.target = jb->info.min + jb->info.conf.max_jitterbuf;
573 diff = jb->info.target - jb->info.current;
575 /* jb_warn("diff = %d lms=%d last = %d now = %d\n", diff, */
576 /* jb->info.last_voice_ms, jb->info.last_adjustment, now); */
578 /* let's work on non-silent case first */
579 if (!jb->info.silence_begin_ts) {
580 /* we want to grow */
582 /* we haven't grown in the delay length */
583 (((jb->info.last_adjustment + JB_ADJUST_DELAY) < now) ||
584 /* we need to grow more than the "length" we have left */
585 (diff > queue_last(jb) - queue_next(jb)) ) ) {
586 /* grow by interp frame length */
587 jb->info.current += interpl;
588 jb->info.next_voice_ts += interpl;
589 jb->info.last_voice_ms = interpl;
590 jb->info.last_adjustment = now;
591 jb->info.cnt_contig_interp++;
592 if (jb->info.conf.max_contig_interp && jb->info.cnt_contig_interp >= jb->info.conf.max_contig_interp) {
593 jb->info.silence_begin_ts = jb->info.next_voice_ts - jb->info.current;
599 frame = queue_get(jb, jb->info.next_voice_ts - jb->info.current);
601 /* not a voice frame; just return it. */
602 if (frame && frame->type != JB_TYPE_VOICE) {
603 if (frame->type == JB_TYPE_SILENCE) {
604 jb->info.silence_begin_ts = frame->ts;
605 jb->info.cnt_contig_interp = 0;
609 jb->info.frames_out++;
615 /* voice frame is later than expected */
616 if (frame && frame->ts + jb->info.current < jb->info.next_voice_ts) {
617 if (frame->ts + jb->info.current > jb->info.next_voice_ts - jb->info.last_voice_ms) {
618 /* either we interpolated past this frame in the last jb_get */
619 /* or the frame is still in order, but came a little too quick */
621 /* reset expectation for next frame */
622 jb->info.next_voice_ts = frame->ts + jb->info.current + frame->ms;
623 jb->info.frames_out++;
624 decrement_losspct(jb);
625 jb->info.cnt_contig_interp = 0;
629 /* voice frame is late */
631 jb->info.frames_out++;
632 decrement_losspct(jb);
633 jb->info.frames_late++;
634 jb->info.frames_lost--;
636 /*jb_warn("\nlate: wanted=%ld, this=%ld, next=%ld\n", jb->info.next_voice_ts - jb->info.current, frame->ts, queue_next(jb));
642 /* keep track of frame sizes, to allow for variable sized-frames */
643 if (frame && frame->ms > 0) {
644 jb->info.last_voice_ms = frame->ms;
647 /* we want to shrink; shrink at 1 frame / 500ms */
648 /* unless we don't have a frame, then shrink 1 frame */
649 /* every 80ms (though perhaps we can shrink even faster */
651 if (diff < -jb->info.conf.target_extra &&
652 ((!frame && jb->info.last_adjustment + 80 < now) ||
653 (jb->info.last_adjustment + 500 < now))) {
655 jb->info.last_adjustment = now;
656 jb->info.cnt_contig_interp = 0;
660 /* shrink by frame size we're throwing out */
661 jb->info.current -= frame->ms;
662 jb->info.frames_out++;
663 decrement_losspct(jb);
664 jb->info.frames_dropped++;
668 /* shrink by last_voice_ms */
669 jb->info.current -= jb->info.last_voice_ms;
670 jb->info.frames_lost++;
671 increment_losspct(jb);
679 /* this is a bit of a hack for now, but if we're close to
680 * target, and we find a missing frame, it makes sense to
681 * grow, because the frame might just be a bit late;
682 * otherwise, we presently get into a pattern where we return
683 * INTERP for the lost frame, then it shows up next, and we
684 * throw it away because it's late */
685 /* I've recently only been able to replicate this using
686 * iaxclient talking to app_echo on asterisk. In this case,
687 * my outgoing packets go through asterisk's (old)
688 * jitterbuffer, and then might get an unusual increasing delay
689 * there if it decides to grow?? */
690 /* Update: that might have been a different bug, that has been fixed..
691 * But, this still seemed like a good idea, except that it ended up making a single actual
692 * lost frame get interpolated two or more times, when there was "room" to grow, so it might
693 * be a bit of a bad idea overall */
694 /*if (diff > -1 * jb->info.last_voice_ms) {
695 jb->info.current += jb->info.last_voice_ms;
696 jb->info.last_adjustment = now;
700 jb->info.frames_lost++;
701 increment_losspct(jb);
702 jb->info.next_voice_ts += interpl;
703 jb->info.last_voice_ms = interpl;
704 jb->info.cnt_contig_interp++;
705 if (jb->info.conf.max_contig_interp && jb->info.cnt_contig_interp >= jb->info.conf.max_contig_interp) {
706 jb->info.silence_begin_ts = jb->info.next_voice_ts - jb->info.current;
712 /* normal case; return the frame, increment stuff */
714 jb->info.next_voice_ts += frame->ms;
715 jb->info.frames_out++;
716 jb->info.cnt_contig_interp = 0;
717 decrement_losspct(jb);
721 /* TODO: after we get the non-silent case down, we'll make the
722 * silent case -- basically, we'll just grow and shrink faster
723 * here, plus handle next_voice_ts a bit differently */
725 /* to disable silent special case altogether, just uncomment this: */
726 /* jb->info.silence_begin_ts = 0; */
728 /* shrink interpl len every 10ms during silence */
729 if (diff < -jb->info.conf.target_extra &&
730 jb->info.last_adjustment + 10 <= now) {
731 jb->info.current -= interpl;
732 jb->info.last_adjustment = now;
735 frame = queue_get(jb, now - jb->info.current);
738 } else if (frame->type != JB_TYPE_VOICE) {
739 /* normal case; in silent mode, got a non-voice frame */
741 jb->info.frames_out++;
744 if (frame->ts < jb->info.silence_begin_ts) {
745 /* voice frame is late */
747 jb->info.frames_out++;
748 decrement_losspct(jb);
749 jb->info.frames_late++;
750 jb->info.frames_lost--;
752 /*jb_warn("\nlate: wanted=%ld, this=%ld, next=%ld\n", jb->info.next_voice_ts - jb->info.current, frame->ts, queue_next(jb));
757 /* try setting current to target right away here */
758 jb->info.current = jb->info.target;
759 jb->info.silence_begin_ts = 0;
760 jb->info.next_voice_ts = frame->ts + jb->info.current + frame->ms;
761 jb->info.last_voice_ms = frame->ms;
762 jb->info.frames_out++;
763 decrement_losspct(jb);
771 long jb_next(jitterbuf *jb)
773 if (jb->info.silence_begin_ts) {
775 long next = queue_next(jb);
777 /* shrink during silence */
778 if (jb->info.target - jb->info.current < -jb->info.conf.target_extra)
779 return jb->info.last_adjustment + 10;
780 return next + jb->info.target;
785 return jb->info.next_voice_ts;
789 enum jb_return_code jb_get(jitterbuf *jb, jb_frame *frameout, long now, long interpl)
791 enum jb_return_code ret = _jb_get(jb, frameout, now, interpl);
794 int thists = ((ret == JB_OK) || (ret == JB_DROP)) ? frameout->ts : 0;
795 jb_warn("jb_get(%x,%x,%ld) = %d (%d)\n", jb, frameout, now, ret, thists);
796 if (thists && thists < lastts) jb_warn("XXXX timestamp roll-back!!!\n");
799 if (ret == JB_INTERP)
800 frameout->ms = jb->info.last_voice_ms;
805 enum jb_return_code jb_getall(jitterbuf *jb, jb_frame *frameout)
808 frame = queue_getall(jb);
819 enum jb_return_code jb_getinfo(jitterbuf *jb, jb_info *stats)
829 enum jb_return_code jb_setconf(jitterbuf *jb, jb_conf *conf)
831 /* take selected settings from the struct */
833 jb->info.conf.max_jitterbuf = conf->max_jitterbuf;
834 jb->info.conf.resync_threshold = conf->resync_threshold;
835 jb->info.conf.max_contig_interp = conf->max_contig_interp;
837 /* -1 indicates use of the default JB_TARGET_EXTRA value */
838 jb->info.conf.target_extra = ( conf->target_extra == -1 )
843 /* update these to match new target_extra setting */
844 jb->info.current = jb->info.conf.target_extra;
845 jb->info.target = jb->info.conf.target_extra;