jitterbuf.c 22 KB

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