00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <string.h>
00033
00034 #include "asterisk.h"
00035
00036 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 34087 $")
00037
00038 #include "jitterbuf.h"
00039
00040
00041 #define JB_LONGMAX 2147483647L
00042 #define JB_LONGMIN (-JB_LONGMAX - 1L)
00043
00044 #define jb_warn(...) (warnf ? warnf(__VA_ARGS__) : (void)0)
00045 #define jb_err(...) (errf ? errf(__VA_ARGS__) : (void)0)
00046 #define jb_dbg(...) (dbgf ? dbgf(__VA_ARGS__) : (void)0)
00047
00048 #ifdef DEEP_DEBUG
00049 #define jb_dbg2(...) (dbgf ? dbgf(__VA_ARGS__) : (void)0)
00050 #else
00051 #define jb_dbg2(...) ((void)0)
00052 #endif
00053
00054 static jb_output_function_t warnf, errf, dbgf;
00055
00056 void jb_setoutput(jb_output_function_t err, jb_output_function_t warn, jb_output_function_t dbg)
00057 {
00058 errf = err;
00059 warnf = warn;
00060 dbgf = dbg;
00061 }
00062
00063 static void increment_losspct(jitterbuf *jb)
00064 {
00065 jb->info.losspct = (100000 + 499 * jb->info.losspct)/500;
00066 }
00067
00068 static void decrement_losspct(jitterbuf *jb)
00069 {
00070 jb->info.losspct = (499 * jb->info.losspct)/500;
00071 }
00072
00073 void jb_reset(jitterbuf *jb)
00074 {
00075
00076 jb_conf s = jb->info.conf;
00077 memset(jb,0,sizeof(jitterbuf));
00078 jb->info.conf = s;
00079
00080
00081 jb->info.current = jb->info.target = JB_TARGET_EXTRA;
00082 jb->info.silence_begin_ts = -1;
00083 }
00084
00085 jitterbuf * jb_new()
00086 {
00087 jitterbuf *jb;
00088
00089
00090 jb = malloc(sizeof(jitterbuf));
00091 if (!jb)
00092 return NULL;
00093
00094 jb_reset(jb);
00095
00096 jb_dbg2("jb_new() = %x\n", jb);
00097 return jb;
00098 }
00099
00100 void jb_destroy(jitterbuf *jb)
00101 {
00102 jb_frame *frame;
00103 jb_dbg2("jb_destroy(%x)\n", jb);
00104
00105
00106 frame = jb->free;
00107 while (frame != NULL) {
00108 jb_frame *next = frame->next;
00109 free(frame);
00110 frame = next;
00111 }
00112
00113
00114 free(jb);
00115 }
00116
00117
00118
00119 #if 0
00120 static int longcmp(const void *a, const void *b)
00121 {
00122 return *(long *)a - *(long *)b;
00123 }
00124 #endif
00125
00126
00127
00128
00129
00130 static int history_put(jitterbuf *jb, long ts, long now, long ms)
00131 {
00132 long delay = now - (ts - jb->info.resync_offset);
00133 long threshold = 2 * jb->info.jitter + jb->info.conf.resync_threshold;
00134 long kicked;
00135
00136
00137 if (ts <= 0)
00138 return 0;
00139
00140
00141 if (jb->info.conf.resync_threshold != -1) {
00142 if (abs(delay - jb->info.last_delay) > threshold) {
00143 jb->info.cnt_delay_discont++;
00144 if (jb->info.cnt_delay_discont > 3) {
00145
00146 jb->info.cnt_delay_discont = 0;
00147 jb->hist_ptr = 0;
00148 jb->hist_maxbuf_valid = 0;
00149
00150 jb_warn("Resyncing the jb. last_delay %ld, this delay %ld, threshold %ld, new offset %ld\n", jb->info.last_delay, delay, threshold, ts - now);
00151 jb->info.resync_offset = ts - now;
00152 jb->info.last_delay = delay = 0;
00153 } else {
00154 return -1;
00155 }
00156 } else {
00157 jb->info.last_delay = delay;
00158 jb->info.cnt_delay_discont = 0;
00159 }
00160 }
00161
00162 kicked = jb->history[jb->hist_ptr % JB_HISTORY_SZ];
00163
00164 jb->history[(jb->hist_ptr++) % JB_HISTORY_SZ] = delay;
00165
00166
00167
00168
00169
00170
00171 if (!jb->hist_maxbuf_valid)
00172 return 0;
00173
00174
00175
00176 if (jb->hist_ptr < JB_HISTORY_SZ)
00177 goto invalidate;
00178
00179
00180 if (delay < jb->hist_minbuf[JB_HISTORY_MAXBUF_SZ-1])
00181 goto invalidate;
00182
00183
00184 if (delay > jb->hist_maxbuf[JB_HISTORY_MAXBUF_SZ-1])
00185 goto invalidate;
00186
00187
00188 if (kicked <= jb->hist_minbuf[JB_HISTORY_MAXBUF_SZ-1])
00189 goto invalidate;
00190
00191 if (kicked >= jb->hist_maxbuf[JB_HISTORY_MAXBUF_SZ-1])
00192 goto invalidate;
00193
00194
00195
00196 return 0;
00197
00198
00199
00200 invalidate:
00201 jb->hist_maxbuf_valid = 0;
00202 return 0;
00203 }
00204
00205 static void history_calc_maxbuf(jitterbuf *jb)
00206 {
00207 int i,j;
00208
00209 if (jb->hist_ptr == 0)
00210 return;
00211
00212
00213
00214 for (i=0;i<JB_HISTORY_MAXBUF_SZ;i++) {
00215
00216
00217
00218
00219 jb->hist_maxbuf[i] = JB_LONGMIN;
00220 jb->hist_minbuf[i] = JB_LONGMAX;
00221 }
00222
00223
00224
00225
00226
00227 i = (jb->hist_ptr > JB_HISTORY_SZ) ? (jb->hist_ptr - JB_HISTORY_SZ) : 0;
00228
00229 for (;i<jb->hist_ptr;i++) {
00230 long toins = jb->history[i % JB_HISTORY_SZ];
00231
00232
00233 if (toins > jb->hist_maxbuf[JB_HISTORY_MAXBUF_SZ-1]) {
00234
00235
00236 for (j=0;j<JB_HISTORY_MAXBUF_SZ;j++) {
00237
00238 if (toins > jb->hist_maxbuf[j]) {
00239
00240 memmove(jb->hist_maxbuf+j+1,jb->hist_maxbuf+j, (JB_HISTORY_MAXBUF_SZ-(j+1)) * sizeof(long));
00241
00242 jb->hist_maxbuf[j] = toins;
00243
00244 break;
00245 }
00246 }
00247 }
00248
00249
00250 if (toins < jb->hist_minbuf[JB_HISTORY_MAXBUF_SZ-1]) {
00251
00252
00253 for (j=0;j<JB_HISTORY_MAXBUF_SZ;j++) {
00254
00255 if (toins < jb->hist_minbuf[j]) {
00256
00257 memmove(jb->hist_minbuf+j+1,jb->hist_minbuf+j, (JB_HISTORY_MAXBUF_SZ-(j+1)) * sizeof(long));
00258
00259 jb->hist_minbuf[j] = toins;
00260
00261 break;
00262 }
00263 }
00264 }
00265
00266 if (0) {
00267 int k;
00268 fprintf(stderr, "toins = %ld\n", toins);
00269 fprintf(stderr, "maxbuf =");
00270 for (k=0;k<JB_HISTORY_MAXBUF_SZ;k++)
00271 fprintf(stderr, "%ld ", jb->hist_maxbuf[k]);
00272 fprintf(stderr, "\nminbuf =");
00273 for (k=0;k<JB_HISTORY_MAXBUF_SZ;k++)
00274 fprintf(stderr, "%ld ", jb->hist_minbuf[k]);
00275 fprintf(stderr, "\n");
00276 }
00277 }
00278
00279 jb->hist_maxbuf_valid = 1;
00280 }
00281
00282 static void history_get(jitterbuf *jb)
00283 {
00284 long max, min, jitter;
00285 int index;
00286 int count;
00287
00288 if (!jb->hist_maxbuf_valid)
00289 history_calc_maxbuf(jb);
00290
00291
00292 count = (jb->hist_ptr < JB_HISTORY_SZ) ? jb->hist_ptr : JB_HISTORY_SZ;
00293
00294
00295 index = count * JB_HISTORY_DROPPCT / 100;
00296
00297
00298 if (index > (JB_HISTORY_MAXBUF_SZ - 1))
00299 index = JB_HISTORY_MAXBUF_SZ - 1;
00300
00301
00302 if (index < 0) {
00303 jb->info.min = 0;
00304 jb->info.jitter = 0;
00305 return;
00306 }
00307
00308 max = jb->hist_maxbuf[index];
00309 min = jb->hist_minbuf[index];
00310
00311 jitter = max - min;
00312
00313
00314
00315
00316
00317
00318
00319
00320 jb->info.min = min;
00321 jb->info.jitter = jitter;
00322 }
00323
00324
00325 static int queue_put(jitterbuf *jb, void *data, int type, long ms, long ts)
00326 {
00327 jb_frame *frame;
00328 jb_frame *p;
00329 int head = 0;
00330 long resync_ts = ts - jb->info.resync_offset;
00331
00332 frame = jb->free;
00333 if (frame) {
00334 jb->free = frame->next;
00335 } else {
00336 frame = malloc(sizeof(jb_frame));
00337 }
00338
00339 if (!frame) {
00340 jb_err("cannot allocate frame\n");
00341 return 0;
00342 }
00343
00344 jb->info.frames_cur++;
00345
00346 frame->data = data;
00347 frame->ts = resync_ts;
00348 frame->ms = ms;
00349 frame->type = type;
00350
00351
00352
00353
00354
00355
00356 if (!jb->frames) {
00357 jb->frames = frame;
00358 frame->next = frame;
00359 frame->prev = frame;
00360 head = 1;
00361 } else if (resync_ts < jb->frames->ts) {
00362 frame->next = jb->frames;
00363 frame->prev = jb->frames->prev;
00364
00365 frame->next->prev = frame;
00366 frame->prev->next = frame;
00367
00368
00369 jb->info.frames_ooo++;
00370
00371 jb->frames = frame;
00372 head = 1;
00373 } else {
00374 p = jb->frames;
00375
00376
00377 if (resync_ts < p->prev->ts) jb->info.frames_ooo++;
00378
00379 while (resync_ts < p->prev->ts && p->prev != jb->frames)
00380 p = p->prev;
00381
00382 frame->next = p;
00383 frame->prev = p->prev;
00384
00385 frame->next->prev = frame;
00386 frame->prev->next = frame;
00387 }
00388 return head;
00389 }
00390
00391 static long queue_next(jitterbuf *jb)
00392 {
00393 if (jb->frames)
00394 return jb->frames->ts;
00395 else
00396 return -1;
00397 }
00398
00399 static long queue_last(jitterbuf *jb)
00400 {
00401 if (jb->frames)
00402 return jb->frames->prev->ts;
00403 else
00404 return -1;
00405 }
00406
00407 static jb_frame *_queue_get(jitterbuf *jb, long ts, int all)
00408 {
00409 jb_frame *frame;
00410 frame = jb->frames;
00411
00412 if (!frame)
00413 return NULL;
00414
00415
00416
00417 if (all || ts >= frame->ts) {
00418
00419 frame->prev->next = frame->next;
00420 frame->next->prev = frame->prev;
00421
00422 if (frame->next == frame)
00423 jb->frames = NULL;
00424 else
00425 jb->frames = frame->next;
00426
00427
00428
00429 frame->next = jb->free;
00430 jb->free = frame;
00431
00432 jb->info.frames_cur--;
00433
00434
00435
00436 return frame;
00437 }
00438
00439 return NULL;
00440 }
00441
00442 static jb_frame *queue_get(jitterbuf *jb, long ts)
00443 {
00444 return _queue_get(jb,ts,0);
00445 }
00446
00447 static jb_frame *queue_getall(jitterbuf *jb)
00448 {
00449 return _queue_get(jb,0,1);
00450 }
00451
00452 #if 0
00453
00454 static void jb_dbginfo(jitterbuf *jb)
00455 {
00456 if (dbgf == NULL)
00457 return;
00458
00459 jb_dbg("\njb info: fin=%ld fout=%ld flate=%ld flost=%ld fdrop=%ld fcur=%ld\n",
00460 jb->info.frames_in, jb->info.frames_out, jb->info.frames_late, jb->info.frames_lost, jb->info.frames_dropped, jb->info.frames_cur);
00461
00462 jb_dbg("jitter=%ld current=%ld target=%ld min=%ld sil=%d len=%d len/fcur=%ld\n",
00463 jb->info.jitter, jb->info.current, jb->info.target, jb->info.min, jb->info.silence_begin_ts, jb->info.current - jb->info.min,
00464 jb->info.frames_cur ? (jb->info.current - jb->info.min)/jb->info.frames_cur : -8);
00465 if (jb->info.frames_in > 0)
00466 jb_dbg("jb info: Loss PCT = %ld%%, Late PCT = %ld%%\n",
00467 jb->info.frames_lost * 100/(jb->info.frames_in + jb->info.frames_lost),
00468 jb->info.frames_late * 100/jb->info.frames_in);
00469 jb_dbg("jb info: queue %d -> %d. last_ts %d (queue len: %d) last_ms %d\n",
00470 queue_next(jb),
00471 queue_last(jb),
00472 jb->info.next_voice_ts,
00473 queue_last(jb) - queue_next(jb),
00474 jb->info.last_voice_ms);
00475 }
00476 #endif
00477
00478 #ifdef DEEP_DEBUG
00479 static void jb_chkqueue(jitterbuf *jb)
00480 {
00481 int i=0;
00482 jb_frame *p = jb->frames;
00483
00484 if (!p) {
00485 return;
00486 }
00487
00488 do {
00489 if (p->next == NULL) {
00490 jb_err("Queue is BROKEN at item [%d]", i);
00491 }
00492 i++;
00493 p=p->next;
00494 } while (p->next != jb->frames);
00495 }
00496
00497 static void jb_dbgqueue(jitterbuf *jb)
00498 {
00499 int i=0;
00500 jb_frame *p = jb->frames;
00501
00502 jb_dbg("queue: ");
00503
00504 if (!p) {
00505 jb_dbg("EMPTY\n");
00506 return;
00507 }
00508
00509 do {
00510 jb_dbg("[%d]=%ld ", i++, p->ts);
00511 p=p->next;
00512 } while (p->next != jb->frames);
00513
00514 jb_dbg("\n");
00515 }
00516 #endif
00517
00518 int jb_put(jitterbuf *jb, void *data, int type, long ms, long ts, long now)
00519 {
00520 jb_dbg2("jb_put(%x,%x,%ld,%ld,%ld)\n", jb, data, ms, ts, now);
00521
00522 jb->info.frames_in++;
00523
00524 if (type == JB_TYPE_VOICE) {
00525
00526
00527 if (history_put(jb,ts,now,ms))
00528 return JB_DROP;
00529 }
00530
00531
00532 if (queue_put(jb,data,type,ms,ts)) {
00533 return JB_SCHED;
00534 }
00535 return JB_OK;
00536 }
00537
00538
00539 static int _jb_get(jitterbuf *jb, jb_frame *frameout, long now, long interpl)
00540 {
00541 jb_frame *frame;
00542 long diff;
00543 static int dbg_cnt = 0;
00544
00545
00546
00547 history_get(jb);
00548
00549 if (dbg_cnt && dbg_cnt % 50 == 0) {
00550 jb_dbg("\n");
00551 }
00552 dbg_cnt++;
00553
00554
00555 jb->info.target = jb->info.jitter + jb->info.min + JB_TARGET_EXTRA;
00556
00557
00558 if ((jb->info.conf.max_jitterbuf) && ((jb->info.target - jb->info.min) > jb->info.conf.max_jitterbuf)) {
00559 jb_dbg("clamping target from %d to %d\n", (jb->info.target - jb->info.min), jb->info.conf.max_jitterbuf);
00560 jb->info.target = jb->info.min + jb->info.conf.max_jitterbuf;
00561 }
00562
00563 diff = jb->info.target - jb->info.current;
00564
00565
00566
00567
00568
00569 if (!jb->info.silence_begin_ts) {
00570
00571 if ((diff > 0) &&
00572
00573 (((jb->info.last_adjustment + JB_ADJUST_DELAY) < now) ||
00574
00575 (diff > queue_last(jb) - queue_next(jb)) ) ) {
00576
00577 jb->info.current += interpl;
00578 jb->info.next_voice_ts += interpl;
00579 jb->info.last_voice_ms = interpl;
00580 jb->info.last_adjustment = now;
00581 jb->info.cnt_contig_interp++;
00582 if (jb->info.conf.max_contig_interp && jb->info.cnt_contig_interp >= jb->info.conf.max_contig_interp) {
00583 jb->info.silence_begin_ts = jb->info.next_voice_ts - jb->info.current;
00584 }
00585 jb_dbg("G");
00586 return JB_INTERP;
00587 }
00588
00589 frame = queue_get(jb, jb->info.next_voice_ts - jb->info.current);
00590
00591
00592 if (frame && frame->type != JB_TYPE_VOICE) {
00593 if (frame->type == JB_TYPE_SILENCE) {
00594 jb->info.silence_begin_ts = frame->ts;
00595 jb->info.cnt_contig_interp = 0;
00596 }
00597
00598 *frameout = *frame;
00599 jb->info.frames_out++;
00600 jb_dbg("o");
00601 return JB_OK;
00602 }
00603
00604
00605
00606 if (frame && frame->ts + jb->info.current < jb->info.next_voice_ts) {
00607 if (frame->ts + jb->info.current > jb->info.next_voice_ts - jb->info.last_voice_ms) {
00608
00609
00610 *frameout = *frame;
00611
00612 jb->info.next_voice_ts = frame->ts + jb->info.current + frame->ms;
00613 jb->info.frames_out++;
00614 decrement_losspct(jb);
00615 jb->info.cnt_contig_interp = 0;
00616 jb_dbg("v");
00617 return JB_OK;
00618 } else {
00619
00620 *frameout = *frame;
00621 jb->info.frames_out++;
00622 decrement_losspct(jb);
00623 jb->info.frames_late++;
00624 jb->info.frames_lost--;
00625 jb_dbg("l");
00626
00627
00628 return JB_DROP;
00629 }
00630 }
00631
00632
00633 if (frame && frame->ms > 0) {
00634 jb->info.last_voice_ms = frame->ms;
00635 }
00636
00637
00638
00639
00640
00641 if (diff < -JB_TARGET_EXTRA &&
00642 ((!frame && jb->info.last_adjustment + 80 < now) ||
00643 (jb->info.last_adjustment + 500 < now))) {
00644
00645 jb->info.last_adjustment = now;
00646 jb->info.cnt_contig_interp = 0;
00647
00648 if (frame) {
00649 *frameout = *frame;
00650
00651 jb->info.current -= frame->ms;
00652 jb->info.frames_out++;
00653 decrement_losspct(jb);
00654 jb->info.frames_dropped++;
00655 jb_dbg("s");
00656 return JB_DROP;
00657 } else {
00658
00659 jb->info.current -= jb->info.last_voice_ms;
00660 jb->info.frames_lost++;
00661 increment_losspct(jb);
00662 jb_dbg("S");
00663 return JB_NOFRAME;
00664 }
00665 }
00666
00667
00668 if (!frame) {
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690 jb->info.frames_lost++;
00691 increment_losspct(jb);
00692 jb->info.next_voice_ts += interpl;
00693 jb->info.last_voice_ms = interpl;
00694 jb->info.cnt_contig_interp++;
00695 if (jb->info.conf.max_contig_interp && jb->info.cnt_contig_interp >= jb->info.conf.max_contig_interp) {
00696 jb->info.silence_begin_ts = jb->info.next_voice_ts - jb->info.current;
00697 }
00698 jb_dbg("L");
00699 return JB_INTERP;
00700 }
00701
00702
00703 *frameout = *frame;
00704 jb->info.next_voice_ts += frame->ms;
00705 jb->info.frames_out++;
00706 jb->info.cnt_contig_interp = 0;
00707 decrement_losspct(jb);
00708 jb_dbg("v");
00709 return JB_OK;
00710 } else {
00711
00712
00713
00714
00715
00716
00717
00718
00719 if (diff < -JB_TARGET_EXTRA &&
00720 jb->info.last_adjustment + 10 <= now) {
00721 jb->info.current -= interpl;
00722 jb->info.last_adjustment = now;
00723 }
00724
00725 frame = queue_get(jb, now - jb->info.current);
00726 if (!frame) {
00727 return JB_NOFRAME;
00728 } else if (frame->type != JB_TYPE_VOICE) {
00729
00730 *frameout = *frame;
00731 jb->info.frames_out++;
00732 return JB_OK;
00733 }
00734 if (frame->ts < jb->info.silence_begin_ts) {
00735
00736 *frameout = *frame;
00737 jb->info.frames_out++;
00738 decrement_losspct(jb);
00739 jb->info.frames_late++;
00740 jb->info.frames_lost--;
00741 jb_dbg("l");
00742
00743
00744 return JB_DROP;
00745 } else {
00746
00747
00748 jb->info.current = jb->info.target;
00749 jb->info.silence_begin_ts = 0;
00750 jb->info.next_voice_ts = frame->ts + jb->info.current + frame->ms;
00751 jb->info.last_voice_ms = frame->ms;
00752 jb->info.frames_out++;
00753 decrement_losspct(jb);
00754 *frameout = *frame;
00755 jb_dbg("V");
00756 return JB_OK;
00757 }
00758 }
00759 }
00760
00761 long jb_next(jitterbuf *jb)
00762 {
00763 if (jb->info.silence_begin_ts) {
00764 long next = queue_next(jb);
00765 if (next > 0) {
00766 history_get(jb);
00767
00768 if (jb->info.target - jb->info.current < -JB_TARGET_EXTRA)
00769 return jb->info.last_adjustment + 10;
00770 return next + jb->info.target;
00771 }
00772 else
00773 return JB_LONGMAX;
00774 } else {
00775 return jb->info.next_voice_ts;
00776 }
00777 }
00778
00779 int jb_get(jitterbuf *jb, jb_frame *frameout, long now, long interpl)
00780 {
00781 int ret = _jb_get(jb,frameout,now,interpl);
00782 #if 0
00783 static int lastts=0;
00784 int thists = ((ret == JB_OK) || (ret == JB_DROP)) ? frameout->ts : 0;
00785 jb_warn("jb_get(%x,%x,%ld) = %d (%d)\n", jb, frameout, now, ret, thists);
00786 if (thists && thists < lastts) jb_warn("XXXX timestamp roll-back!!!\n");
00787 lastts = thists;
00788 #endif
00789 if(ret == JB_INTERP)
00790 frameout->ms = jb->info.last_voice_ms;
00791
00792 return ret;
00793 }
00794
00795 int jb_getall(jitterbuf *jb, jb_frame *frameout)
00796 {
00797 jb_frame *frame;
00798 frame = queue_getall(jb);
00799
00800 if (!frame) {
00801 return JB_NOFRAME;
00802 }
00803
00804 *frameout = *frame;
00805 return JB_OK;
00806 }
00807
00808
00809 int jb_getinfo(jitterbuf *jb, jb_info *stats)
00810 {
00811
00812 history_get(jb);
00813
00814 *stats = jb->info;
00815
00816 return JB_OK;
00817 }
00818
00819 int jb_setconf(jitterbuf *jb, jb_conf *conf)
00820 {
00821
00822
00823 jb->info.conf.max_jitterbuf = conf->max_jitterbuf;
00824 jb->info.conf.resync_threshold = conf->resync_threshold;
00825 jb->info.conf.max_contig_interp = conf->max_contig_interp;
00826
00827 return JB_OK;
00828 }
00829
00830