jitterbuf.c 21 KB

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