da017c128ca30539d138dff0cd65a63c068707de
[asterisk/asterisk.git] / jitterbuf.c
1 /*
2  * jitterbuf: an application-independent jitterbuffer
3  *
4  * Copyrights:
5  * Copyright (C) 2004-2005, Horizon Wimba, Inc.
6  *
7  * Contributors:
8  * Steve Kann <stevek@stevek.com>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU Lesser (Library) General Public License
12  *
13  * Copyright on this file is disclaimed to Digium for inclusion in Asterisk
14  */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19
20 #include "jitterbuf.h"
21
22 /* define these here, just for ancient compiler systems */
23 #define JB_LONGMAX 2147483647L
24 #define JB_LONGMIN (-JB_LONGMAX - 1L)
25
26 #define jb_warn(...) (warnf ? warnf(__VA_ARGS__) : (void)0)
27 #define jb_err(...) (errf ? errf(__VA_ARGS__) : (void)0)
28 #define jb_dbg(...) (dbgf ? dbgf(__VA_ARGS__) : (void)0)
29
30 #ifdef DEEP_DEBUG
31 #define jb_dbg2(...) (dbgf ? dbgf(__VA_ARGS__) : (void)0)
32 #else
33 #define jb_dbg2(...) ((void)0)
34 #endif
35
36 static jb_output_function_t warnf, errf, dbgf;
37
38 void jb_setoutput(jb_output_function_t warn, jb_output_function_t err, jb_output_function_t dbg) 
39 {
40         warnf = warn;
41         errf = err;
42         dbgf = dbg;
43 }
44
45 static void increment_losspct(jitterbuf *jb) 
46 {
47         jb->info.losspct = (100000 + 499 * jb->info.losspct)/500;    
48 }
49
50 static void decrement_losspct(jitterbuf *jb) 
51 {
52         jb->info.losspct = (499 * jb->info.losspct)/500;    
53 }
54
55 void jb_reset(jitterbuf *jb) 
56 {
57         memset(jb,0,sizeof(jitterbuf));
58
59         /* initialize length */
60         jb->info.current = jb->info.target = JB_TARGET_EXTRA; 
61         jb->info.silence_begin_ts = -1; 
62 }
63
64 jitterbuf * jb_new() 
65 {
66         jitterbuf *jb;
67
68
69         jb = malloc(sizeof(jitterbuf));
70         if (!jb) 
71                 return NULL;
72
73         jb_reset(jb);
74
75         jb_dbg2("jb_new() = %x\n", jb);
76         return jb;
77 }
78
79 void jb_destroy(jitterbuf *jb) 
80 {
81         jb_frame *frame; 
82         jb_dbg2("jb_destroy(%x)\n", jb);
83
84         /* free all the frames on the "free list" */
85         frame = jb->free;
86         while (frame != NULL) {
87                 jb_frame *next = frame->next;
88                 free(frame);
89                 frame = next;
90         }
91
92         /* free ourselves! */ 
93         free(jb);
94 }
95
96
97
98 /* simple history manipulation */
99 /* maybe later we can make the history buckets variable size, or something? */
100 /* drop parameter determines whether we will drop outliers to minimize
101  * delay */
102 #if 0
103 static int longcmp(const void *a, const void *b) 
104 {
105         return *(long *)a - *(long *)b;
106 }
107 #endif
108
109 static int history_put(jitterbuf *jb, long ts, long now, long ms) 
110 {
111         long delay = now - (ts - jb->info.resync_offset);
112         long threshold = 2 * jb->info.jitter + jb->info.resync_threshold;
113         long kicked;
114
115         /* don't add special/negative times to history */
116         if (ts <= 0) 
117                 return 0;
118
119         /* check for drastic change in delay */
120         if (jb->info.resync_threshold != -1) {
121                 if (abs(delay - jb->info.last_delay) > threshold) {
122                         jb->info.cnt_delay_discont++;
123                         if (jb->info.cnt_delay_discont > 3) {
124                                 /* resync the jitterbuffer */
125                                 jb->info.cnt_delay_discont = 0;
126                                 jb->hist_ptr = 0;
127                                 jb->hist_maxbuf_valid = 0;
128
129                                 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);
130                                 jb->info.resync_offset = ts - now;
131                                 jb->info.last_delay = 0; /* after resync, frame is right on time */
132                         } else {
133                                 return -1;
134                         }
135                 } else {
136                         jb->info.last_delay = delay;
137                         jb->info.cnt_delay_discont = 0;
138                 }
139         }
140
141         kicked = jb->history[jb->hist_ptr & JB_HISTORY_SZ];
142
143         jb->history[(jb->hist_ptr++) % JB_HISTORY_SZ] = delay;
144
145         /* optimization; the max/min buffers don't need to be recalculated, if this packet's
146          * entry doesn't change them.  This happens if this packet is not involved, _and_ any packet
147          * that got kicked out of the history is also not involved 
148          * We do a number of comparisons, but it's probably still worthwhile, because it will usually
149          * succeed, and should be a lot faster than going through all 500 packets in history */
150         if (!jb->hist_maxbuf_valid)
151                 return 0;
152
153         /* don't do this until we've filled history 
154          * (reduces some edge cases below) */
155         if (jb->hist_ptr < JB_HISTORY_SZ)
156                 goto invalidate;
157
158         /* if the new delay would go into min */
159         if (delay < jb->hist_minbuf[JB_HISTORY_MAXBUF_SZ-1])
160                 goto invalidate;
161     
162         /* or max.. */
163         if (delay > jb->hist_maxbuf[JB_HISTORY_MAXBUF_SZ-1])
164                 goto invalidate;
165
166         /* or the kicked delay would be in min */
167         if (kicked <= jb->hist_minbuf[JB_HISTORY_MAXBUF_SZ-1]) 
168                 goto invalidate;
169
170         if (kicked >= jb->hist_maxbuf[JB_HISTORY_MAXBUF_SZ-1]) 
171                 goto invalidate;
172
173         /* if we got here, we don't need to invalidate, 'cause this delay didn't 
174          * affect things */
175         return 0;
176         /* end optimization */
177
178
179 invalidate:
180         jb->hist_maxbuf_valid = 0;
181         return 0;
182 }
183
184 static void history_calc_maxbuf(jitterbuf *jb) 
185 {
186         int i,j;
187
188         if (jb->hist_ptr == 0) 
189                 return;
190
191
192         /* initialize maxbuf/minbuf to the latest value */
193         for (i=0;i<JB_HISTORY_MAXBUF_SZ;i++) {
194 /*
195  * jb->hist_maxbuf[i] = jb->history[(jb->hist_ptr-1) % JB_HISTORY_SZ];
196  * jb->hist_minbuf[i] = jb->history[(jb->hist_ptr-1) % JB_HISTORY_SZ];
197  */
198                 jb->hist_maxbuf[i] = JB_LONGMIN;
199                 jb->hist_minbuf[i] = JB_LONGMAX;
200         }
201
202         /* use insertion sort to populate maxbuf */
203         /* we want it to be the top "n" values, in order */
204
205         /* start at the beginning, or JB_HISTORY_SZ frames ago */
206         i = (jb->hist_ptr > JB_HISTORY_SZ) ? (jb->hist_ptr - JB_HISTORY_SZ) : 0; 
207
208         for (;i<jb->hist_ptr;i++) {
209                 long toins = jb->history[i % JB_HISTORY_SZ];
210
211                 /* if the maxbuf should get this */
212                 if (toins > jb->hist_maxbuf[JB_HISTORY_MAXBUF_SZ-1])  {
213
214                         /* insertion-sort it into the maxbuf */
215                         for (j=0;j<JB_HISTORY_MAXBUF_SZ;j++) {
216                                 /* found where it fits */
217                                 if (toins > jb->hist_maxbuf[j]) {
218                                         /* move over */
219                                         memmove(jb->hist_maxbuf+j+1,jb->hist_maxbuf+j, (JB_HISTORY_MAXBUF_SZ-(j+1)) * sizeof(long));
220                                         /* insert */
221                                         jb->hist_maxbuf[j] = toins;
222
223                                         break;
224                                 }
225                         }
226                 }
227
228                 /* if the minbuf should get this */
229                 if (toins < jb->hist_minbuf[JB_HISTORY_MAXBUF_SZ-1])  {
230
231                         /* insertion-sort it into the maxbuf */
232                         for (j=0;j<JB_HISTORY_MAXBUF_SZ;j++) {
233                                 /* found where it fits */
234                                 if (toins < jb->hist_minbuf[j]) {
235                                         /* move over */
236                                         memmove(jb->hist_minbuf+j+1,jb->hist_minbuf+j, (JB_HISTORY_MAXBUF_SZ-(j+1)) * sizeof(long));
237                                         /* insert */
238                                         jb->hist_minbuf[j] = toins;
239
240                                         break;
241                                 }
242                         }
243                 }
244
245                 if (0) { 
246                         int k;
247                         fprintf(stderr, "toins = %ld\n", toins);
248                         fprintf(stderr, "maxbuf =");
249                         for (k=0;k<JB_HISTORY_MAXBUF_SZ;k++) 
250                                 fprintf(stderr, "%ld ", jb->hist_maxbuf[k]);
251                         fprintf(stderr, "\nminbuf =");
252                         for (k=0;k<JB_HISTORY_MAXBUF_SZ;k++) 
253                                 fprintf(stderr, "%ld ", jb->hist_minbuf[k]);
254                         fprintf(stderr, "\n");
255                 }
256         }
257
258         jb->hist_maxbuf_valid = 1;
259 }
260
261 static void history_get(jitterbuf *jb) 
262 {
263         long max, min, jitter;
264         int index;
265         int count;
266
267         if (!jb->hist_maxbuf_valid) 
268                 history_calc_maxbuf(jb);
269
270         /* count is how many items in history we're examining */
271         count = (jb->hist_ptr < JB_HISTORY_SZ) ? jb->hist_ptr : JB_HISTORY_SZ;
272
273         /* index is the "n"ths highest/lowest that we'll look for */
274         index = count * JB_HISTORY_DROPPCT / 100;
275
276         /* sanity checks for index */
277         if (index > (JB_HISTORY_MAXBUF_SZ - 1)) 
278                 index = JB_HISTORY_MAXBUF_SZ - 1;
279
280
281         if (index < 0) {
282                 jb->info.min = 0;
283                 jb->info.jitter = 0;
284                 return;
285         }
286
287         max = jb->hist_maxbuf[index];
288         min = jb->hist_minbuf[index];
289
290         jitter = max - min;
291
292         /* these debug stmts compare the difference between looking at the absolute jitter, and the
293          * values we get by throwing away the outliers */
294         /*
295         fprintf(stderr, "[%d] min=%d, max=%d, jitter=%d\n", index, min, max, jitter);
296         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]);
297         */
298
299         jb->info.min = min;
300         jb->info.jitter = jitter;
301 }
302
303 /* returns 1 if frame was inserted into head of queue, 0 otherwise */
304 static int queue_put(jitterbuf *jb, void *data, int type, long ms, long ts) 
305 {
306         jb_frame *frame;
307         jb_frame *p;
308         int head = 0;
309         long resync_ts = ts - jb->info.resync_offset;
310
311         frame = jb->free;
312         if (frame) {
313                 jb->free = frame->next;
314         } else {
315                 frame = malloc(sizeof(jb_frame));
316         }
317
318         if (!frame) {
319                 jb_err("cannot allocate frame\n");
320                 return 0;
321         }
322
323         jb->info.frames_cur++;
324
325         frame->data = data;
326         frame->ts = resync_ts;
327         frame->ms = ms;
328         frame->type = type;
329
330         /* 
331          * frames are a circular list, jb-frames points to to the lowest ts, 
332          * jb->frames->prev points to the highest ts
333          */
334
335         if (!jb->frames) {  /* queue is empty */
336                 jb->frames = frame;
337                 frame->next = frame;
338                 frame->prev = frame;
339                 head = 1;
340         } else if (resync_ts < jb->frames->ts) {
341                 frame->next = jb->frames;
342                 frame->prev = jb->frames->prev;
343
344                 frame->next->prev = frame;
345                 frame->prev->next = frame;
346
347                 /* frame is out of order */
348                 jb->info.frames_ooo++;
349
350                 jb->frames = frame;
351                 head = 1;
352         } else { 
353                 p = jb->frames;
354
355                 /* frame is out of order */
356                 if (resync_ts < p->prev->ts) jb->info.frames_ooo++;
357
358                 while (resync_ts < p->prev->ts && p->prev != jb->frames) 
359                         p = p->prev;
360
361                 frame->next = p;
362                 frame->prev = p->prev;
363
364                 frame->next->prev = frame;
365                 frame->prev->next = frame;
366         }
367         return head;
368 }
369
370 static long queue_next(jitterbuf *jb) 
371 {
372         if (jb->frames) 
373                 return jb->frames->ts;
374         else 
375                 return -1;
376 }
377
378 static long queue_last(jitterbuf *jb) 
379 {
380         if (jb->frames) 
381                 return jb->frames->prev->ts;
382         else 
383                 return -1;
384 }
385
386 static jb_frame *_queue_get(jitterbuf *jb, long ts, int all) 
387 {
388         jb_frame *frame;
389         frame = jb->frames;
390
391         if (!frame)
392                 return NULL;
393
394         /*jb_warn("queue_get: ASK %ld FIRST %ld\n", ts, frame->ts); */
395
396         if (all || ts >= frame->ts) {
397                 /* remove this frame */
398                 frame->prev->next = frame->next;
399                 frame->next->prev = frame->prev;
400
401                 if (frame->next == frame)
402                         jb->frames = NULL;
403                 else
404                         jb->frames = frame->next;
405
406
407                 /* insert onto "free" single-linked list */
408                 frame->next = jb->free;
409                 jb->free = frame;
410
411                 jb->info.frames_cur--;
412
413                 /* we return the frame pointer, even though it's on free list, 
414                  * but caller must copy data */
415                 return frame;
416         } 
417
418         return NULL;
419 }
420
421 static jb_frame *queue_get(jitterbuf *jb, long ts) 
422 {
423         return _queue_get(jb,ts,0);
424 }
425
426 static jb_frame *queue_getall(jitterbuf *jb) 
427 {
428         return _queue_get(jb,0,1);
429 }
430
431 #if 0
432 /* some diagnostics */
433 static void jb_dbginfo(jitterbuf *jb) 
434 {
435         if (dbgf == NULL) 
436                 return;
437
438         jb_dbg("\njb info: fin=%ld fout=%ld flate=%ld flost=%ld fdrop=%ld fcur=%ld\n",
439                 jb->info.frames_in, jb->info.frames_out, jb->info.frames_late, jb->info.frames_lost, jb->info.frames_dropped, jb->info.frames_cur);
440         
441         jb_dbg("jitter=%ld current=%ld target=%ld min=%ld sil=%d len=%d len/fcur=%ld\n",
442                 jb->info.jitter, jb->info.current, jb->info.target, jb->info.min, jb->info.silence_begin_ts, jb->info.current - jb->info.min, 
443                 jb->info.frames_cur ? (jb->info.current - jb->info.min)/jb->info.frames_cur : -8);
444         if (jb->info.frames_in > 0) 
445                 jb_dbg("jb info: Loss PCT = %ld%%, Late PCT = %ld%%\n",
446                         jb->info.frames_lost * 100/(jb->info.frames_in + jb->info.frames_lost), 
447                         jb->info.frames_late * 100/jb->info.frames_in);
448         jb_dbg("jb info: queue %d -> %d.  last_ts %d (queue len: %d) last_ms %d\n",
449                 queue_next(jb), 
450                 queue_last(jb),
451                 jb->info.next_voice_ts, 
452                 queue_last(jb) - queue_next(jb),
453                 jb->info.last_voice_ms);
454 }
455 #endif
456
457 #ifdef DEEP_DEBUG
458 static void jb_chkqueue(jitterbuf *jb) 
459 {
460         int i=0;
461         jb_frame *p = jb->frames;
462
463         if (!p) {
464                 return;
465         }
466
467         do {
468                 if (p->next == NULL)  {
469                         jb_err("Queue is BROKEN at item [%d]", i);      
470                 }
471                 i++;
472                 p=p->next;
473         } while (p->next != jb->frames);
474 }
475
476 static void jb_dbgqueue(jitterbuf *jb) 
477 {
478         int i=0;
479         jb_frame *p = jb->frames;
480
481         jb_dbg("queue: ");
482
483         if (!p) {
484                 jb_dbg("EMPTY\n");
485                 return;
486         }
487
488         do {
489                 jb_dbg("[%d]=%ld ", i++, p->ts);
490                 p=p->next;
491         } while (p->next != jb->frames);
492
493         jb_dbg("\n");
494 }
495 #endif
496
497 int jb_put(jitterbuf *jb, void *data, int type, long ms, long ts, long now) 
498 {
499         jb_dbg2("jb_put(%x,%x,%ld,%ld,%ld)\n", jb, data, ms, ts, now);
500
501         jb->info.frames_in++;
502
503         if (type == JB_TYPE_VOICE) {
504                 /* presently, I'm only adding VOICE frames to history and drift calculations; mostly because with the
505                  * IAX integrations, I'm sending retransmitted control frames with their awkward timestamps through */
506                 if (history_put(jb,ts,now,ms))
507                         return JB_DROP;
508         }
509
510         /* if put into head of queue, caller needs to reschedule */
511         if (queue_put(jb,data,type,ms,ts)) {
512                 return JB_SCHED;
513         }
514         return JB_OK;
515 }
516
517
518 static int _jb_get(jitterbuf *jb, jb_frame *frameout, long now, long interpl) 
519 {
520         jb_frame *frame;
521         long diff;
522
523         /*if ((now - jb_next(jb)) > 2 * jb->info.last_voice_ms) jb_warn("SCHED: %ld", (now - jb_next(jb))); */
524         /* get jitter info */
525         history_get(jb);
526
527
528         /* target */
529         jb->info.target = jb->info.jitter + jb->info.min + JB_TARGET_EXTRA; 
530
531         /* if a hard clamp was requested, use it */
532         if ((jb->info.max_jitterbuf) && ((jb->info.target - jb->info.min) > jb->info.max_jitterbuf)) {
533                 jb_dbg("clamping target from %d to %d\n", (jb->info.target - jb->info.min), jb->info.max_jitterbuf);
534                 jb->info.target = jb->info.min + jb->info.max_jitterbuf;
535         }
536
537         diff = jb->info.target - jb->info.current;
538
539         /* jb_warn("diff = %d lms=%d last = %d now = %d\n", diff,  */
540         /*      jb->info.last_voice_ms, jb->info.last_adjustment, now); */
541
542         /* let's work on non-silent case first */
543         if (!jb->info.silence_begin_ts) { 
544                 /* we want to grow */
545                 if ((diff > 0) && 
546                         /* we haven't grown in the delay length */
547                         (((jb->info.last_adjustment + JB_ADJUST_DELAY) < now) || 
548                         /* we need to grow more than the "length" we have left */
549                         (diff > queue_last(jb)  - queue_next(jb)) ) ) {
550                         /* grow by interp frame length */
551                         jb->info.current += interpl;
552                         jb->info.next_voice_ts += interpl;
553                         jb->info.last_voice_ms = interpl;
554                         jb->info.last_adjustment = now;
555                         jb_dbg("G");
556                         return JB_INTERP;
557                 }
558
559                 frame = queue_get(jb, jb->info.next_voice_ts - jb->info.current);
560
561                 /* not a voice frame; just return it. */
562                 if (frame && frame->type != JB_TYPE_VOICE) {
563                         if (frame->type == JB_TYPE_SILENCE) 
564                                 jb->info.silence_begin_ts = frame->ts;
565
566                         *frameout = *frame;
567                         jb->info.frames_out++;
568                         jb_dbg("o");
569                         return JB_OK;
570                 }
571
572
573                 /* voice frame is later than expected */
574                 if (frame && frame->ts + jb->info.current < jb->info.next_voice_ts) {
575                         if (frame->ts + jb->info.current > jb->info.next_voice_ts - jb->info.last_voice_ms) {
576                                 /* either we interpolated past this frame in the last jb_get */
577                                 /* or the frame is still in order, but came a little too quick */ 
578                                 *frameout = *frame;
579                                 /* reset expectation for next frame */
580                                 jb->info.next_voice_ts = frame->ts + jb->info.current + frame->ms;
581                                 jb->info.frames_out++;
582                                 decrement_losspct(jb);
583                                 jb_dbg("v");
584                                 return JB_OK;
585                         } else {
586                                 /* voice frame is late */
587                                 *frameout = *frame;
588                                 jb->info.frames_out++;
589                                 decrement_losspct(jb);
590                                 jb->info.frames_late++;
591                                 jb->info.frames_lost--;
592                                 jb_dbg("l");
593                                 /*jb_warn("\nlate: wanted=%ld, this=%ld, next=%ld\n", jb->info.next_voice_ts - jb->info.current, frame->ts, queue_next(jb));
594                                 jb_warninfo(jb); */
595                                 return JB_DROP;
596                         }
597                 }
598
599                 /* keep track of frame sizes, to allow for variable sized-frames */
600                 if (frame && frame->ms > 0) {
601                         jb->info.last_voice_ms = frame->ms;
602                 }
603
604                 /* we want to shrink; shrink at 1 frame / 500ms */
605                 /* unless we don't have a frame, then shrink 1 frame */
606                 /* every 80ms (though perhaps we can shrink even faster */
607                 /* in this case) */
608                 if (diff < -JB_TARGET_EXTRA && 
609                         ((!frame && jb->info.last_adjustment + 80 < now) || 
610                         (jb->info.last_adjustment + 500 < now))) {
611
612                         jb->info.last_adjustment = now;
613
614                         if (frame) {
615                                 *frameout = *frame;
616                                 /* shrink by frame size we're throwing out */
617                                 jb->info.current -= frame->ms;
618                                 jb->info.frames_out++;
619                                 decrement_losspct(jb);
620                                 jb->info.frames_dropped++;
621                                 jb_dbg("s");
622                                 return JB_DROP;
623                         } else {
624                                 /* shrink by last_voice_ms */
625                                 jb->info.current -= jb->info.last_voice_ms;
626                                 jb->info.frames_lost++;
627                                 increment_losspct(jb);
628                                 jb_dbg("S");
629                                 return JB_NOFRAME;
630                         }
631                 }
632
633                 /* lost frame */
634                 if (!frame) {
635                         /* this is a bit of a hack for now, but if we're close to
636                          * target, and we find a missing frame, it makes sense to
637                          * grow, because the frame might just be a bit late;
638                          * otherwise, we presently get into a pattern where we return
639                          * INTERP for the lost frame, then it shows up next, and we
640                          * throw it away because it's late */
641                         /* I've recently only been able to replicate this using
642                          * iaxclient talking to app_echo on asterisk.  In this case,
643                          * my outgoing packets go through asterisk's (old)
644                          * jitterbuffer, and then might get an unusual increasing delay 
645                          * there if it decides to grow?? */
646                         /* Update: that might have been a different bug, that has been fixed..
647                          * But, this still seemed like a good idea, except that it ended up making a single actual
648                          * lost frame get interpolated two or more times, when there was "room" to grow, so it might
649                          * be a bit of a bad idea overall */
650                         /*if (diff > -1 * jb->info.last_voice_ms) { 
651                                 jb->info.current += jb->info.last_voice_ms;
652                                 jb->info.last_adjustment = now;
653                                 jb_warn("g");
654                                 return JB_INTERP;
655                         } */
656                         jb->info.frames_lost++;
657                         increment_losspct(jb);
658                         jb->info.next_voice_ts += interpl;
659                         jb->info.last_voice_ms = interpl;
660                         jb_dbg("L");
661                         return JB_INTERP;
662                 }
663
664                 /* normal case; return the frame, increment stuff */
665                 *frameout = *frame;
666                 jb->info.next_voice_ts += frame->ms;
667                 jb->info.frames_out++;
668                 decrement_losspct(jb);
669                 jb_dbg("v");
670                 return JB_OK;
671         } else {     
672                 /* TODO: after we get the non-silent case down, we'll make the
673                  * silent case -- basically, we'll just grow and shrink faster
674                  * here, plus handle next_voice_ts a bit differently */
675       
676                 /* to disable silent special case altogether, just uncomment this: */
677                 /* jb->info.silence_begin_ts = 0; */
678
679                 /* shrink interpl len every 10ms during silence */
680                 if (diff < -JB_TARGET_EXTRA &&
681                         jb->info.last_adjustment + 10 <= now) {
682                         jb->info.current -= interpl;
683                         jb->info.last_adjustment = now;
684                 }
685
686                 frame = queue_get(jb, now - jb->info.current);
687                 if (!frame) {
688                         return JB_NOFRAME;
689                 } else if (frame->type != JB_TYPE_VOICE) {
690                         /* normal case; in silent mode, got a non-voice frame */
691                         *frameout = *frame;
692                         jb->info.frames_out++;
693                         return JB_OK;
694                 }
695                 if (frame->ts < jb->info.silence_begin_ts) {
696                         /* voice frame is late */
697                         *frameout = *frame;
698                         jb->info.frames_out++;
699                         decrement_losspct(jb);
700                         jb->info.frames_late++;
701                         jb->info.frames_lost--;
702                         jb_dbg("l");
703                         /*jb_warn("\nlate: wanted=%ld, this=%ld, next=%ld\n", jb->info.next_voice_ts - jb->info.current, frame->ts, queue_next(jb));
704                         jb_warninfo(jb); */
705                         return JB_DROP;
706                 } else {
707                         /* voice frame */
708                         /* try setting current to target right away here */
709                         jb->info.current = jb->info.target;
710                         jb->info.silence_begin_ts = 0;
711                         jb->info.next_voice_ts = frame->ts + jb->info.current + frame->ms;
712                         jb->info.last_voice_ms = frame->ms;
713                         jb->info.frames_out++;
714                         decrement_losspct(jb);
715                         *frameout = *frame;
716                         jb_dbg("V");
717                         return JB_OK;
718                 }
719         }
720 }
721
722 long jb_next(jitterbuf *jb) 
723 {
724         if (jb->info.silence_begin_ts) {
725                 long next = queue_next(jb);
726                 if (next > 0) { 
727                         history_get(jb);
728                         /* shrink during silence */
729                         if (jb->info.target - jb->info.current < -JB_TARGET_EXTRA)
730                                 return jb->info.last_adjustment + 10;
731                         return next + jb->info.target;
732                 }
733                 else 
734                         return JB_LONGMAX;
735         } else {
736                 return jb->info.next_voice_ts;
737         }
738 }
739
740 int jb_get(jitterbuf *jb, jb_frame *frameout, long now, long interpl) 
741 {
742         int ret = _jb_get(jb,frameout,now,interpl);
743 #if 0
744         static int lastts=0;
745         int thists = ((ret == JB_OK) || (ret == JB_DROP)) ? frameout->ts : 0;
746         jb_warn("jb_get(%x,%x,%ld) = %d (%d)\n", jb, frameout, now, ret, thists);
747         if (thists && thists < lastts) jb_warn("XXXX timestamp roll-back!!!\n");
748         lastts = thists;
749 #endif
750         if(ret == JB_INTERP) 
751                 frameout->ms = jb->info.last_voice_ms;
752         
753         return ret;
754 }
755
756 int jb_getall(jitterbuf *jb, jb_frame *frameout) 
757 {
758         jb_frame *frame;
759         frame = queue_getall(jb);
760
761         if (!frame) {
762                 return JB_NOFRAME;
763         }
764
765         *frameout = *frame;
766         return JB_OK;
767 }
768
769
770 int jb_getinfo(jitterbuf *jb, jb_info *stats) 
771 {
772
773         history_get(jb);
774
775         *stats = jb->info;
776
777         return JB_OK;
778 }
779
780 int jb_setinfo(jitterbuf *jb, jb_info *settings) 
781 {
782         /* take selected settings from the struct */
783
784         jb->info.max_jitterbuf = settings->max_jitterbuf;
785         jb->info.resync_threshold = settings->resync_threshold;
786
787         return JB_OK;
788 }
789
790