abstract_jb.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  1. /*
  2. * abstract_jb: common implementation-independent jitterbuffer stuff
  3. *
  4. * Copyright (C) 2005, Attractel OOD
  5. *
  6. * Contributors:
  7. * Slav Klenov <slav@securax.org>
  8. *
  9. * See http://www.asterisk.org for more information about
  10. * the Asterisk project. Please do not directly contact
  11. * any of the maintainers of this project for assistance;
  12. * the project provides a web site, mailing lists and IRC
  13. * channels for your use.
  14. *
  15. * This program is free software, distributed under the terms of
  16. * the GNU General Public License Version 2. See the LICENSE file
  17. * at the top of the source tree.
  18. *
  19. * A license has been granted to Digium (via disclaimer) for the use of
  20. * this code.
  21. */
  22. /*! \file
  23. *
  24. * \brief Common implementation-independent jitterbuffer stuff.
  25. *
  26. * \author Slav Klenov <slav@securax.org>
  27. *
  28. *
  29. */
  30. /*** MODULEINFO
  31. <support_level>core</support_level>
  32. ***/
  33. #include "asterisk.h"
  34. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  35. #include "asterisk/frame.h"
  36. #include "asterisk/channel.h"
  37. #include "asterisk/term.h"
  38. #include "asterisk/utils.h"
  39. #include "asterisk/pbx.h"
  40. #include "asterisk/timing.h"
  41. #include "asterisk/abstract_jb.h"
  42. #include "fixedjitterbuf.h"
  43. #include "jitterbuf.h"
  44. /*! Internal jb flags */
  45. enum {
  46. JB_USE = (1 << 0),
  47. JB_TIMEBASE_INITIALIZED = (1 << 1),
  48. JB_CREATED = (1 << 2)
  49. };
  50. /* Implementation functions */
  51. /* fixed */
  52. static void *jb_create_fixed(struct ast_jb_conf *general_config);
  53. static void jb_destroy_fixed(void *jb);
  54. static int jb_put_first_fixed(void *jb, struct ast_frame *fin, long now);
  55. static int jb_put_fixed(void *jb, struct ast_frame *fin, long now);
  56. static int jb_get_fixed(void *jb, struct ast_frame **fout, long now, long interpl);
  57. static long jb_next_fixed(void *jb);
  58. static int jb_remove_fixed(void *jb, struct ast_frame **fout);
  59. static void jb_force_resynch_fixed(void *jb);
  60. static void jb_empty_and_reset_fixed(void *jb);
  61. /* adaptive */
  62. static void * jb_create_adaptive(struct ast_jb_conf *general_config);
  63. static void jb_destroy_adaptive(void *jb);
  64. static int jb_put_first_adaptive(void *jb, struct ast_frame *fin, long now);
  65. static int jb_put_adaptive(void *jb, struct ast_frame *fin, long now);
  66. static int jb_get_adaptive(void *jb, struct ast_frame **fout, long now, long interpl);
  67. static long jb_next_adaptive(void *jb);
  68. static int jb_remove_adaptive(void *jb, struct ast_frame **fout);
  69. static void jb_force_resynch_adaptive(void *jb);
  70. static void jb_empty_and_reset_adaptive(void *jb);
  71. /* Available jb implementations */
  72. static const struct ast_jb_impl avail_impl[] = {
  73. {
  74. .name = "fixed",
  75. .type = AST_JB_FIXED,
  76. .create = jb_create_fixed,
  77. .destroy = jb_destroy_fixed,
  78. .put_first = jb_put_first_fixed,
  79. .put = jb_put_fixed,
  80. .get = jb_get_fixed,
  81. .next = jb_next_fixed,
  82. .remove = jb_remove_fixed,
  83. .force_resync = jb_force_resynch_fixed,
  84. .empty_and_reset = jb_empty_and_reset_fixed,
  85. },
  86. {
  87. .name = "adaptive",
  88. .type = AST_JB_ADAPTIVE,
  89. .create = jb_create_adaptive,
  90. .destroy = jb_destroy_adaptive,
  91. .put_first = jb_put_first_adaptive,
  92. .put = jb_put_adaptive,
  93. .get = jb_get_adaptive,
  94. .next = jb_next_adaptive,
  95. .remove = jb_remove_adaptive,
  96. .force_resync = jb_force_resynch_adaptive,
  97. .empty_and_reset = jb_empty_and_reset_adaptive,
  98. }
  99. };
  100. static int default_impl = 0;
  101. /* Translations between impl and abstract return codes */
  102. static const int fixed_to_abstract_code[] =
  103. {AST_JB_IMPL_OK, AST_JB_IMPL_DROP, AST_JB_IMPL_INTERP, AST_JB_IMPL_NOFRAME};
  104. static const int adaptive_to_abstract_code[] =
  105. {AST_JB_IMPL_OK, AST_JB_IMPL_NOFRAME, AST_JB_IMPL_NOFRAME, AST_JB_IMPL_INTERP, AST_JB_IMPL_DROP, AST_JB_IMPL_OK};
  106. /* JB_GET actions (used only for the frames log) */
  107. static const char * const jb_get_actions[] = {"Delivered", "Dropped", "Interpolated", "No"};
  108. /*! \brief Macros for the frame log files */
  109. #define jb_framelog(...) do { \
  110. if (jb->logfile) { \
  111. fprintf(jb->logfile, __VA_ARGS__); \
  112. fflush(jb->logfile); \
  113. } \
  114. } while (0)
  115. /* Internal utility functions */
  116. static void jb_choose_impl(struct ast_channel *chan);
  117. static void jb_get_and_deliver(struct ast_channel *chan);
  118. static int create_jb(struct ast_channel *chan, struct ast_frame *first_frame);
  119. static long get_now(struct ast_jb *jb, struct timeval *tv);
  120. /* Interface ast jb functions impl */
  121. static void jb_choose_impl(struct ast_channel *chan)
  122. {
  123. struct ast_jb *jb = ast_channel_jb(chan);
  124. struct ast_jb_conf *jbconf = &jb->conf;
  125. const struct ast_jb_impl *test_impl;
  126. int i, avail_impl_count = ARRAY_LEN(avail_impl);
  127. jb->impl = &avail_impl[default_impl];
  128. if (ast_strlen_zero(jbconf->impl)) {
  129. return;
  130. }
  131. for (i = 0; i < avail_impl_count; i++) {
  132. test_impl = &avail_impl[i];
  133. if (!strcasecmp(jbconf->impl, test_impl->name)) {
  134. jb->impl = test_impl;
  135. return;
  136. }
  137. }
  138. }
  139. int ast_jb_do_usecheck(struct ast_channel *c0, struct ast_channel *c1)
  140. {
  141. struct ast_jb *jb0 = ast_channel_jb(c0);
  142. struct ast_jb *jb1 = ast_channel_jb(c1);
  143. struct ast_jb_conf *conf0 = &jb0->conf;
  144. struct ast_jb_conf *conf1 = &jb1->conf;
  145. int c0_wants_jitter = ast_channel_tech(c0)->properties & AST_CHAN_TP_WANTSJITTER;
  146. int c0_creates_jitter = ast_channel_tech(c0)->properties & AST_CHAN_TP_CREATESJITTER;
  147. int c0_jb_enabled = ast_test_flag(conf0, AST_JB_ENABLED);
  148. int c0_force_jb = ast_test_flag(conf0, AST_JB_FORCED);
  149. int c0_jb_timebase_initialized = ast_test_flag(jb0, JB_TIMEBASE_INITIALIZED);
  150. int c0_jb_created = ast_test_flag(jb0, JB_CREATED);
  151. int c1_wants_jitter = ast_channel_tech(c1)->properties & AST_CHAN_TP_WANTSJITTER;
  152. int c1_creates_jitter = ast_channel_tech(c1)->properties & AST_CHAN_TP_CREATESJITTER;
  153. int c1_jb_enabled = ast_test_flag(conf1, AST_JB_ENABLED);
  154. int c1_force_jb = ast_test_flag(conf1, AST_JB_FORCED);
  155. int c1_jb_timebase_initialized = ast_test_flag(jb1, JB_TIMEBASE_INITIALIZED);
  156. int c1_jb_created = ast_test_flag(jb1, JB_CREATED);
  157. int inuse = 0;
  158. /* Determine whether audio going to c0 needs a jitter buffer */
  159. if (((!c0_wants_jitter && c1_creates_jitter) || (c0_force_jb && c1_creates_jitter)) && c0_jb_enabled) {
  160. ast_set_flag(jb0, JB_USE);
  161. if (!c0_jb_timebase_initialized) {
  162. if (c1_jb_timebase_initialized) {
  163. memcpy(&jb0->timebase, &jb1->timebase, sizeof(struct timeval));
  164. } else {
  165. gettimeofday(&jb0->timebase, NULL);
  166. }
  167. ast_set_flag(jb0, JB_TIMEBASE_INITIALIZED);
  168. }
  169. if (!c0_jb_created) {
  170. jb_choose_impl(c0);
  171. }
  172. inuse = 1;
  173. }
  174. /* Determine whether audio going to c1 needs a jitter buffer */
  175. if (((!c1_wants_jitter && c0_creates_jitter) || (c1_force_jb && c0_creates_jitter)) && c1_jb_enabled) {
  176. ast_set_flag(jb1, JB_USE);
  177. if (!c1_jb_timebase_initialized) {
  178. if (c0_jb_timebase_initialized) {
  179. memcpy(&jb1->timebase, &jb0->timebase, sizeof(struct timeval));
  180. } else {
  181. gettimeofday(&jb1->timebase, NULL);
  182. }
  183. ast_set_flag(jb1, JB_TIMEBASE_INITIALIZED);
  184. }
  185. if (!c1_jb_created) {
  186. jb_choose_impl(c1);
  187. }
  188. inuse = 1;
  189. }
  190. return inuse;
  191. }
  192. int ast_jb_get_when_to_wakeup(struct ast_channel *c0, struct ast_channel *c1, int time_left)
  193. {
  194. struct ast_jb *jb0 = ast_channel_jb(c0);
  195. struct ast_jb *jb1 = ast_channel_jb(c1);
  196. int c0_use_jb = ast_test_flag(jb0, JB_USE);
  197. int c0_jb_is_created = ast_test_flag(jb0, JB_CREATED);
  198. int c1_use_jb = ast_test_flag(jb1, JB_USE);
  199. int c1_jb_is_created = ast_test_flag(jb1, JB_CREATED);
  200. int wait, wait0, wait1;
  201. struct timeval tv_now;
  202. if (time_left == 0) {
  203. /* No time left - the bridge will be retried */
  204. /* TODO: Test disable this */
  205. /*return 0;*/
  206. }
  207. if (time_left < 0) {
  208. time_left = INT_MAX;
  209. }
  210. gettimeofday(&tv_now, NULL);
  211. wait0 = (c0_use_jb && c0_jb_is_created) ? jb0->next - get_now(jb0, &tv_now) : time_left;
  212. wait1 = (c1_use_jb && c1_jb_is_created) ? jb1->next - get_now(jb1, &tv_now) : time_left;
  213. wait = wait0 < wait1 ? wait0 : wait1;
  214. wait = wait < time_left ? wait : time_left;
  215. if (wait == INT_MAX) {
  216. wait = -1;
  217. } else if (wait < 1) {
  218. /* don't let wait=0, because this can cause the pbx thread to loop without any sleeping at all */
  219. wait = 1;
  220. }
  221. return wait;
  222. }
  223. int ast_jb_put(struct ast_channel *chan, struct ast_frame *f)
  224. {
  225. struct ast_jb *jb = ast_channel_jb(chan);
  226. const struct ast_jb_impl *jbimpl = jb->impl;
  227. void *jbobj = jb->jbobj;
  228. struct ast_frame *frr;
  229. long now = 0;
  230. if (!ast_test_flag(jb, JB_USE))
  231. return -1;
  232. if (f->frametype != AST_FRAME_VOICE) {
  233. if (f->frametype == AST_FRAME_DTMF && ast_test_flag(jb, JB_CREATED)) {
  234. jb_framelog("JB_PUT {now=%ld}: Received DTMF frame. Force resynching jb...\n", now);
  235. jbimpl->force_resync(jbobj);
  236. }
  237. return -1;
  238. }
  239. /* We consider an enabled jitterbuffer should receive frames with valid timing info. */
  240. if (!ast_test_flag(f, AST_FRFLAG_HAS_TIMING_INFO) || f->len < 2 || f->ts < 0) {
  241. ast_log(LOG_WARNING, "%s received frame with invalid timing info: "
  242. "has_timing_info=%u, len=%ld, ts=%ld, src=%s\n",
  243. ast_channel_name(chan), ast_test_flag(f, AST_FRFLAG_HAS_TIMING_INFO), f->len, f->ts, f->src);
  244. return -1;
  245. }
  246. frr = ast_frdup(f);
  247. if (!frr) {
  248. ast_log(LOG_ERROR, "Failed to isolate frame for the jitterbuffer on channel '%s'\n", ast_channel_name(chan));
  249. return -1;
  250. }
  251. if (!ast_test_flag(jb, JB_CREATED)) {
  252. if (create_jb(chan, frr)) {
  253. ast_frfree(frr);
  254. /* Disable the jitterbuffer */
  255. ast_clear_flag(jb, JB_USE);
  256. return -1;
  257. }
  258. ast_set_flag(jb, JB_CREATED);
  259. return 0;
  260. } else {
  261. now = get_now(jb, NULL);
  262. if (jbimpl->put(jbobj, frr, now) != AST_JB_IMPL_OK) {
  263. jb_framelog("JB_PUT {now=%ld}: Dropped frame with ts=%ld and len=%ld\n", now, frr->ts, frr->len);
  264. ast_frfree(frr);
  265. /*return -1;*/
  266. /* TODO: Check this fix - should return 0 here, because the dropped frame shouldn't
  267. be delivered at all */
  268. return 0;
  269. }
  270. jb->next = jbimpl->next(jbobj);
  271. jb_framelog("JB_PUT {now=%ld}: Queued frame with ts=%ld and len=%ld\n", now, frr->ts, frr->len);
  272. return 0;
  273. }
  274. }
  275. void ast_jb_get_and_deliver(struct ast_channel *c0, struct ast_channel *c1)
  276. {
  277. struct ast_jb *jb0 = ast_channel_jb(c0);
  278. struct ast_jb *jb1 = ast_channel_jb(c1);
  279. int c0_use_jb = ast_test_flag(jb0, JB_USE);
  280. int c0_jb_is_created = ast_test_flag(jb0, JB_CREATED);
  281. int c1_use_jb = ast_test_flag(jb1, JB_USE);
  282. int c1_jb_is_created = ast_test_flag(jb1, JB_CREATED);
  283. if (c0_use_jb && c0_jb_is_created)
  284. jb_get_and_deliver(c0);
  285. if (c1_use_jb && c1_jb_is_created)
  286. jb_get_and_deliver(c1);
  287. }
  288. static void jb_get_and_deliver(struct ast_channel *chan)
  289. {
  290. struct ast_jb *jb = ast_channel_jb(chan);
  291. const struct ast_jb_impl *jbimpl = jb->impl;
  292. void *jbobj = jb->jbobj;
  293. struct ast_frame *f, finterp = { .frametype = AST_FRAME_VOICE, };
  294. long now;
  295. int interpolation_len, res;
  296. now = get_now(jb, NULL);
  297. jb->next = jbimpl->next(jbobj);
  298. if (now < jb->next) {
  299. jb_framelog("\tJB_GET {now=%ld}: now < next=%ld\n", now, jb->next);
  300. return;
  301. }
  302. while (now >= jb->next) {
  303. interpolation_len = ast_codec_interp_len(&jb->last_format);
  304. res = jbimpl->get(jbobj, &f, now, interpolation_len);
  305. switch (res) {
  306. case AST_JB_IMPL_OK:
  307. /* deliver the frame */
  308. ast_write(chan, f);
  309. case AST_JB_IMPL_DROP:
  310. jb_framelog("\tJB_GET {now=%ld}: %s frame with ts=%ld and len=%ld\n",
  311. now, jb_get_actions[res], f->ts, f->len);
  312. ast_format_copy(&jb->last_format, &f->subclass.format);
  313. ast_frfree(f);
  314. break;
  315. case AST_JB_IMPL_INTERP:
  316. /* interpolate a frame */
  317. f = &finterp;
  318. ast_format_copy(&f->subclass.format, &jb->last_format);
  319. f->samples = interpolation_len * 8;
  320. f->src = "JB interpolation";
  321. f->delivery = ast_tvadd(jb->timebase, ast_samp2tv(jb->next, 1000));
  322. f->offset = AST_FRIENDLY_OFFSET;
  323. /* deliver the interpolated frame */
  324. ast_write(chan, f);
  325. jb_framelog("\tJB_GET {now=%ld}: Interpolated frame with len=%d\n", now, interpolation_len);
  326. break;
  327. case AST_JB_IMPL_NOFRAME:
  328. ast_log(LOG_WARNING,
  329. "AST_JB_IMPL_NOFRAME is returned from the %s jb when now=%ld >= next=%ld, jbnext=%ld!\n",
  330. jbimpl->name, now, jb->next, jbimpl->next(jbobj));
  331. jb_framelog("\tJB_GET {now=%ld}: No frame for now!?\n", now);
  332. return;
  333. default:
  334. ast_log(LOG_ERROR, "This should never happen!\n");
  335. ast_assert("JB type unknown" == NULL);
  336. break;
  337. }
  338. jb->next = jbimpl->next(jbobj);
  339. }
  340. }
  341. static int create_jb(struct ast_channel *chan, struct ast_frame *frr)
  342. {
  343. struct ast_jb *jb = ast_channel_jb(chan);
  344. struct ast_jb_conf *jbconf = &jb->conf;
  345. const struct ast_jb_impl *jbimpl = jb->impl;
  346. void *jbobj;
  347. long now;
  348. char logfile_pathname[20 + AST_JB_IMPL_NAME_SIZE + 2*AST_CHANNEL_NAME + 1];
  349. char name1[AST_CHANNEL_NAME], name2[AST_CHANNEL_NAME], *tmp;
  350. int res;
  351. jbobj = jb->jbobj = jbimpl->create(jbconf);
  352. if (!jbobj) {
  353. ast_log(LOG_WARNING, "Failed to create jitterbuffer on channel '%s'\n", ast_channel_name(chan));
  354. return -1;
  355. }
  356. now = get_now(jb, NULL);
  357. res = jbimpl->put_first(jbobj, frr, now);
  358. /* The result of putting the first frame should not differ from OK. However, its possible
  359. some implementations (i.e. adaptive's when resynch_threshold is specified) to drop it. */
  360. if (res != AST_JB_IMPL_OK) {
  361. ast_log(LOG_WARNING, "Failed to put first frame in the jitterbuffer on channel '%s'\n", ast_channel_name(chan));
  362. /*
  363. jbimpl->destroy(jbobj);
  364. return -1;
  365. */
  366. }
  367. /* Init next */
  368. jb->next = jbimpl->next(jbobj);
  369. /* Init last format for a first time. */
  370. ast_format_copy(&jb->last_format, &frr->subclass.format);
  371. /* Create a frame log file */
  372. if (ast_test_flag(jbconf, AST_JB_LOG)) {
  373. RAII_VAR(struct ast_channel *, bridged, ast_channel_bridge_peer(chan), ast_channel_cleanup);
  374. char safe_logfile[30] = "/tmp/logfile-XXXXXX";
  375. int safe_fd;
  376. snprintf(name2, sizeof(name2), "%s", ast_channel_name(chan));
  377. while ((tmp = strchr(name2, '/'))) {
  378. *tmp = '#';
  379. }
  380. /* We should always have bridged chan if a jitterbuffer is in use */
  381. ast_assert(bridged != NULL);
  382. snprintf(name1, sizeof(name1), "%s", ast_channel_name(bridged));
  383. while ((tmp = strchr(name1, '/'))) {
  384. *tmp = '#';
  385. }
  386. snprintf(logfile_pathname, sizeof(logfile_pathname),
  387. "/tmp/ast_%s_jb_%s--%s.log", jbimpl->name, name1, name2);
  388. unlink(logfile_pathname);
  389. safe_fd = mkstemp(safe_logfile);
  390. if (safe_fd < 0 || link(safe_logfile, logfile_pathname) || unlink(safe_logfile) || !(jb->logfile = fdopen(safe_fd, "w+b"))) {
  391. ast_log(LOG_ERROR, "Failed to create frame log file with pathname '%s': %s\n", logfile_pathname, strerror(errno));
  392. jb->logfile = NULL;
  393. if (safe_fd > -1) {
  394. close(safe_fd);
  395. }
  396. }
  397. if (res == AST_JB_IMPL_OK) {
  398. jb_framelog("JB_PUT_FIRST {now=%ld}: Queued frame with ts=%ld and len=%ld\n",
  399. now, frr->ts, frr->len);
  400. } else {
  401. jb_framelog("JB_PUT_FIRST {now=%ld}: Dropped frame with ts=%ld and len=%ld\n",
  402. now, frr->ts, frr->len);
  403. }
  404. }
  405. ast_verb(3, "%s jitterbuffer created on channel %s\n", jbimpl->name, ast_channel_name(chan));
  406. /* Free the frame if it has not been queued in the jb */
  407. if (res != AST_JB_IMPL_OK) {
  408. ast_frfree(frr);
  409. }
  410. return 0;
  411. }
  412. void ast_jb_destroy(struct ast_channel *chan)
  413. {
  414. struct ast_jb *jb = ast_channel_jb(chan);
  415. const struct ast_jb_impl *jbimpl = jb->impl;
  416. void *jbobj = jb->jbobj;
  417. struct ast_frame *f;
  418. if (jb->logfile) {
  419. fclose(jb->logfile);
  420. jb->logfile = NULL;
  421. }
  422. if (ast_test_flag(jb, JB_CREATED)) {
  423. /* Remove and free all frames still queued in jb */
  424. while (jbimpl->remove(jbobj, &f) == AST_JB_IMPL_OK) {
  425. ast_frfree(f);
  426. }
  427. jbimpl->destroy(jbobj);
  428. jb->jbobj = NULL;
  429. ast_clear_flag(jb, JB_CREATED);
  430. ast_verb(3, "%s jitterbuffer destroyed on channel %s\n", jbimpl->name, ast_channel_name(chan));
  431. }
  432. }
  433. static long get_now(struct ast_jb *jb, struct timeval *when)
  434. {
  435. struct timeval now;
  436. if (!when) {
  437. when = &now;
  438. gettimeofday(when, NULL);
  439. }
  440. return ast_tvdiff_ms(*when, jb->timebase);
  441. }
  442. int ast_jb_read_conf(struct ast_jb_conf *conf, const char *varname, const char *value)
  443. {
  444. int prefixlen = sizeof(AST_JB_CONF_PREFIX) - 1;
  445. const char *name;
  446. int tmp;
  447. if (strncasecmp(AST_JB_CONF_PREFIX, varname, prefixlen)) {
  448. return -1;
  449. }
  450. name = varname + prefixlen;
  451. if (!strcasecmp(name, AST_JB_CONF_ENABLE)) {
  452. ast_set2_flag(conf, ast_true(value), AST_JB_ENABLED);
  453. } else if (!strcasecmp(name, AST_JB_CONF_FORCE)) {
  454. ast_set2_flag(conf, ast_true(value), AST_JB_FORCED);
  455. } else if (!strcasecmp(name, AST_JB_CONF_MAX_SIZE)) {
  456. if ((tmp = atoi(value)) > 0)
  457. conf->max_size = tmp;
  458. } else if (!strcasecmp(name, AST_JB_CONF_RESYNCH_THRESHOLD)) {
  459. if ((tmp = atoi(value)) > 0)
  460. conf->resync_threshold = tmp;
  461. } else if (!strcasecmp(name, AST_JB_CONF_IMPL)) {
  462. if (!ast_strlen_zero(value))
  463. snprintf(conf->impl, sizeof(conf->impl), "%s", value);
  464. } else if (!strcasecmp(name, AST_JB_CONF_TARGET_EXTRA)) {
  465. if (sscanf(value, "%30d", &tmp) == 1) {
  466. conf->target_extra = tmp;
  467. }
  468. } else if (!strcasecmp(name, AST_JB_CONF_LOG)) {
  469. ast_set2_flag(conf, ast_true(value), AST_JB_LOG);
  470. } else {
  471. return -1;
  472. }
  473. return 0;
  474. }
  475. void ast_jb_enable_for_channel(struct ast_channel *chan)
  476. {
  477. struct ast_jb_conf conf = ast_channel_jb(chan)->conf;
  478. if (ast_test_flag(&conf, AST_JB_ENABLED)) {
  479. ast_jb_create_framehook(chan, &conf, 1);
  480. }
  481. }
  482. void ast_jb_configure(struct ast_channel *chan, const struct ast_jb_conf *conf)
  483. {
  484. memcpy(&ast_channel_jb(chan)->conf, conf, sizeof(*conf));
  485. }
  486. void ast_jb_get_config(const struct ast_channel *chan, struct ast_jb_conf *conf)
  487. {
  488. memcpy(conf, &ast_channel_jb((struct ast_channel *) chan)->conf, sizeof(*conf));
  489. }
  490. void ast_jb_empty_and_reset(struct ast_channel *c0, struct ast_channel *c1)
  491. {
  492. struct ast_jb *jb0 = ast_channel_jb(c0);
  493. struct ast_jb *jb1 = ast_channel_jb(c1);
  494. int c0_use_jb = ast_test_flag(jb0, JB_USE);
  495. int c0_jb_is_created = ast_test_flag(jb0, JB_CREATED);
  496. int c1_use_jb = ast_test_flag(jb1, JB_USE);
  497. int c1_jb_is_created = ast_test_flag(jb1, JB_CREATED);
  498. if (c0_use_jb && c0_jb_is_created && jb0->impl->empty_and_reset) {
  499. jb0->impl->empty_and_reset(jb0->jbobj);
  500. }
  501. if (c1_use_jb && c1_jb_is_created && jb1->impl->empty_and_reset) {
  502. jb1->impl->empty_and_reset(jb1->jbobj);
  503. }
  504. }
  505. /* Implementation functions */
  506. /* fixed */
  507. static void * jb_create_fixed(struct ast_jb_conf *general_config)
  508. {
  509. struct fixed_jb_conf conf;
  510. conf.jbsize = general_config->max_size;
  511. conf.resync_threshold = general_config->resync_threshold;
  512. return fixed_jb_new(&conf);
  513. }
  514. static void jb_destroy_fixed(void *jb)
  515. {
  516. struct fixed_jb *fixedjb = (struct fixed_jb *) jb;
  517. /* Ensure the fixed jb is empty - otherwise it will raise an ASSERT */
  518. jb_empty_and_reset_fixed(jb);
  519. /* destroy the jb */
  520. fixed_jb_destroy(fixedjb);
  521. }
  522. static int jb_put_first_fixed(void *jb, struct ast_frame *fin, long now)
  523. {
  524. struct fixed_jb *fixedjb = (struct fixed_jb *) jb;
  525. int res;
  526. res = fixed_jb_put_first(fixedjb, fin, fin->len, fin->ts, now);
  527. return fixed_to_abstract_code[res];
  528. }
  529. static int jb_put_fixed(void *jb, struct ast_frame *fin, long now)
  530. {
  531. struct fixed_jb *fixedjb = (struct fixed_jb *) jb;
  532. int res;
  533. res = fixed_jb_put(fixedjb, fin, fin->len, fin->ts, now);
  534. return fixed_to_abstract_code[res];
  535. }
  536. static int jb_get_fixed(void *jb, struct ast_frame **fout, long now, long interpl)
  537. {
  538. struct fixed_jb *fixedjb = (struct fixed_jb *) jb;
  539. struct fixed_jb_frame frame = { .data = &ast_null_frame };
  540. int res;
  541. res = fixed_jb_get(fixedjb, &frame, now, interpl);
  542. *fout = frame.data;
  543. return fixed_to_abstract_code[res];
  544. }
  545. static long jb_next_fixed(void *jb)
  546. {
  547. struct fixed_jb *fixedjb = (struct fixed_jb *) jb;
  548. return fixed_jb_next(fixedjb);
  549. }
  550. static int jb_remove_fixed(void *jb, struct ast_frame **fout)
  551. {
  552. struct fixed_jb *fixedjb = (struct fixed_jb *) jb;
  553. struct fixed_jb_frame frame;
  554. int res;
  555. res = fixed_jb_remove(fixedjb, &frame);
  556. *fout = frame.data;
  557. return fixed_to_abstract_code[res];
  558. }
  559. static void jb_force_resynch_fixed(void *jb)
  560. {
  561. struct fixed_jb *fixedjb = (struct fixed_jb *) jb;
  562. fixed_jb_set_force_resynch(fixedjb);
  563. }
  564. static void jb_empty_and_reset_fixed(void *jb)
  565. {
  566. struct fixed_jb *fixedjb = jb;
  567. struct fixed_jb_frame f;
  568. while (fixed_jb_remove(fixedjb, &f) == FIXED_JB_OK) {
  569. ast_frfree(f.data);
  570. }
  571. }
  572. /* adaptive */
  573. static void *jb_create_adaptive(struct ast_jb_conf *general_config)
  574. {
  575. jb_conf jbconf;
  576. jitterbuf *adaptivejb;
  577. adaptivejb = jb_new();
  578. if (adaptivejb) {
  579. jbconf.max_jitterbuf = general_config->max_size;
  580. jbconf.resync_threshold = general_config->resync_threshold;
  581. jbconf.max_contig_interp = 10;
  582. jbconf.target_extra = general_config->target_extra;
  583. jb_setconf(adaptivejb, &jbconf);
  584. }
  585. return adaptivejb;
  586. }
  587. static void jb_destroy_adaptive(void *jb)
  588. {
  589. jitterbuf *adaptivejb = (jitterbuf *) jb;
  590. jb_destroy(adaptivejb);
  591. }
  592. static int jb_put_first_adaptive(void *jb, struct ast_frame *fin, long now)
  593. {
  594. return jb_put_adaptive(jb, fin, now);
  595. }
  596. static int jb_put_adaptive(void *jb, struct ast_frame *fin, long now)
  597. {
  598. jitterbuf *adaptivejb = (jitterbuf *) jb;
  599. int res;
  600. res = jb_put(adaptivejb, fin, JB_TYPE_VOICE, fin->len, fin->ts, now);
  601. return adaptive_to_abstract_code[res];
  602. }
  603. static int jb_get_adaptive(void *jb, struct ast_frame **fout, long now, long interpl)
  604. {
  605. jitterbuf *adaptivejb = (jitterbuf *) jb;
  606. jb_frame frame = { .data = &ast_null_frame };
  607. int res;
  608. res = jb_get(adaptivejb, &frame, now, interpl);
  609. *fout = frame.data;
  610. return adaptive_to_abstract_code[res];
  611. }
  612. static long jb_next_adaptive(void *jb)
  613. {
  614. jitterbuf *adaptivejb = (jitterbuf *) jb;
  615. return jb_next(adaptivejb);
  616. }
  617. static int jb_remove_adaptive(void *jb, struct ast_frame **fout)
  618. {
  619. jitterbuf *adaptivejb = (jitterbuf *) jb;
  620. jb_frame frame;
  621. int res;
  622. res = jb_getall(adaptivejb, &frame);
  623. *fout = frame.data;
  624. return adaptive_to_abstract_code[res];
  625. }
  626. static void jb_force_resynch_adaptive(void *jb)
  627. {
  628. }
  629. static void jb_empty_and_reset_adaptive(void *jb)
  630. {
  631. jitterbuf *adaptivejb = jb;
  632. jb_frame f;
  633. while (jb_getall(adaptivejb, &f) == JB_OK) {
  634. ast_frfree(f.data);
  635. }
  636. jb_reset(adaptivejb);
  637. }
  638. const struct ast_jb_impl *ast_jb_get_impl(enum ast_jb_type type)
  639. {
  640. int i;
  641. for (i = 0; i < ARRAY_LEN(avail_impl); i++) {
  642. if (avail_impl[i].type == type) {
  643. return &avail_impl[i];
  644. }
  645. }
  646. return NULL;
  647. }
  648. #define DEFAULT_TIMER_INTERVAL 20
  649. #define DEFAULT_SIZE 200
  650. #define DEFAULT_TARGET_EXTRA 40
  651. #define DEFAULT_RESYNC 1000
  652. #define DEFAULT_TYPE AST_JB_FIXED
  653. struct jb_framedata {
  654. const struct ast_jb_impl *jb_impl;
  655. struct ast_jb_conf jb_conf;
  656. struct timeval start_tv;
  657. struct ast_format last_format;
  658. struct ast_timer *timer;
  659. int timer_interval; /* ms between deliveries */
  660. int timer_fd;
  661. int first;
  662. void *jb_obj;
  663. };
  664. static void jb_framedata_destroy(struct jb_framedata *framedata)
  665. {
  666. if (framedata->timer) {
  667. ast_timer_close(framedata->timer);
  668. framedata->timer = NULL;
  669. }
  670. if (framedata->jb_impl && framedata->jb_obj) {
  671. struct ast_frame *f;
  672. while (framedata->jb_impl->remove(framedata->jb_obj, &f) == AST_JB_IMPL_OK) {
  673. ast_frfree(f);
  674. }
  675. framedata->jb_impl->destroy(framedata->jb_obj);
  676. framedata->jb_obj = NULL;
  677. }
  678. ast_free(framedata);
  679. }
  680. void ast_jb_conf_default(struct ast_jb_conf *conf)
  681. {
  682. conf->max_size = DEFAULT_SIZE;
  683. conf->resync_threshold = DEFAULT_RESYNC;
  684. ast_copy_string(conf->impl, "fixed", sizeof(conf->impl));
  685. conf->target_extra = DEFAULT_TARGET_EXTRA;
  686. }
  687. static void datastore_destroy_cb(void *data) {
  688. ast_free(data);
  689. ast_debug(1, "JITTERBUFFER datastore destroyed\n");
  690. }
  691. static const struct ast_datastore_info jb_datastore = {
  692. .type = "jitterbuffer",
  693. .destroy = datastore_destroy_cb
  694. };
  695. static void hook_destroy_cb(void *framedata)
  696. {
  697. ast_debug(1, "JITTERBUFFER hook destroyed\n");
  698. jb_framedata_destroy((struct jb_framedata *) framedata);
  699. }
  700. static struct ast_frame *hook_event_cb(struct ast_channel *chan, struct ast_frame *frame, enum ast_framehook_event event, void *data)
  701. {
  702. struct jb_framedata *framedata = data;
  703. struct timeval now_tv;
  704. unsigned long now;
  705. int putframe = 0; /* signifies if audio frame was placed into the buffer or not */
  706. switch (event) {
  707. case AST_FRAMEHOOK_EVENT_READ:
  708. break;
  709. case AST_FRAMEHOOK_EVENT_ATTACHED:
  710. case AST_FRAMEHOOK_EVENT_DETACHED:
  711. case AST_FRAMEHOOK_EVENT_WRITE:
  712. return frame;
  713. }
  714. if (ast_channel_fdno(chan) == AST_JITTERBUFFER_FD && framedata->timer) {
  715. if (ast_timer_ack(framedata->timer, 1) < 0) {
  716. ast_log(LOG_ERROR, "Failed to acknowledge timer in jitter buffer\n");
  717. return frame;
  718. }
  719. }
  720. if (!frame) {
  721. return frame;
  722. }
  723. now_tv = ast_tvnow();
  724. now = ast_tvdiff_ms(now_tv, framedata->start_tv);
  725. if (frame->frametype == AST_FRAME_VOICE) {
  726. int res;
  727. struct ast_frame *jbframe;
  728. if (!ast_test_flag(frame, AST_FRFLAG_HAS_TIMING_INFO) || frame->len < 2 || frame->ts < 0) {
  729. /* only frames with timing info can enter the jitterbuffer */
  730. return frame;
  731. }
  732. jbframe = ast_frisolate(frame);
  733. ast_format_copy(&framedata->last_format, &frame->subclass.format);
  734. if (frame->len && (frame->len != framedata->timer_interval)) {
  735. framedata->timer_interval = frame->len;
  736. ast_timer_set_rate(framedata->timer, 1000 / framedata->timer_interval);
  737. }
  738. if (!framedata->first) {
  739. framedata->first = 1;
  740. res = framedata->jb_impl->put_first(framedata->jb_obj, jbframe, now);
  741. } else {
  742. res = framedata->jb_impl->put(framedata->jb_obj, jbframe, now);
  743. }
  744. if (res == AST_JB_IMPL_OK) {
  745. if (jbframe != frame) {
  746. ast_frfree(frame);
  747. }
  748. frame = &ast_null_frame;
  749. } else if (jbframe != frame) {
  750. ast_frfree(jbframe);
  751. }
  752. putframe = 1;
  753. }
  754. if (frame->frametype == AST_FRAME_NULL) {
  755. int res;
  756. long next = framedata->jb_impl->next(framedata->jb_obj);
  757. /* If now is earlier than the next expected output frame
  758. * from the jitterbuffer we may choose to pass on retrieving
  759. * a frame during this read iteration. The only exception
  760. * to this rule is when an audio frame is placed into the buffer
  761. * and the time for the next frame to come out of the buffer is
  762. * at least within the timer_interval of the next output frame. By
  763. * doing this we are able to feed off the timing of the input frames
  764. * and only rely on our jitterbuffer timer when frames are dropped.
  765. * During testing, this hybrid form of timing gave more reliable results. */
  766. if (now < next) {
  767. long int diff = next - now;
  768. if (!putframe) {
  769. return frame;
  770. } else if (diff >= framedata->timer_interval) {
  771. return frame;
  772. }
  773. }
  774. ast_frfree(frame);
  775. frame = &ast_null_frame;
  776. res = framedata->jb_impl->get(framedata->jb_obj, &frame, now, framedata->timer_interval);
  777. switch (res) {
  778. case AST_JB_IMPL_OK:
  779. /* got it, and pass it through */
  780. break;
  781. case AST_JB_IMPL_DROP:
  782. ast_frfree(frame);
  783. frame = &ast_null_frame;
  784. break;
  785. case AST_JB_IMPL_INTERP:
  786. if (framedata->last_format.id) {
  787. struct ast_frame tmp = { 0, };
  788. tmp.frametype = AST_FRAME_VOICE;
  789. ast_format_copy(&tmp.subclass.format, &framedata->last_format);
  790. /* example: 8000hz / (1000 / 20ms) = 160 samples */
  791. tmp.samples = ast_format_rate(&framedata->last_format) / (1000 / framedata->timer_interval);
  792. tmp.delivery = ast_tvadd(framedata->start_tv, ast_samp2tv(next, 1000));
  793. tmp.offset = AST_FRIENDLY_OFFSET;
  794. tmp.src = "func_jitterbuffer interpolation";
  795. ast_frfree(frame);
  796. frame = ast_frdup(&tmp);
  797. break;
  798. }
  799. /* else fall through */
  800. case AST_JB_IMPL_NOFRAME:
  801. ast_frfree(frame);
  802. frame = &ast_null_frame;
  803. break;
  804. }
  805. }
  806. if (frame->frametype == AST_FRAME_CONTROL) {
  807. switch(frame->subclass.integer) {
  808. case AST_CONTROL_HOLD:
  809. case AST_CONTROL_UNHOLD:
  810. case AST_CONTROL_T38_PARAMETERS:
  811. case AST_CONTROL_SRCUPDATE:
  812. case AST_CONTROL_SRCCHANGE:
  813. framedata->jb_impl->force_resync(framedata->jb_obj);
  814. break;
  815. default:
  816. break;
  817. }
  818. }
  819. return frame;
  820. }
  821. /* set defaults */
  822. static int jb_framedata_init(struct jb_framedata *framedata, struct ast_jb_conf *jb_conf)
  823. {
  824. int jb_impl_type = DEFAULT_TYPE;
  825. /* Initialize defaults */
  826. framedata->timer_fd = -1;
  827. memcpy(&framedata->jb_conf, jb_conf, sizeof(*jb_conf));
  828. /* Figure out implementation type from the configuration implementation string */
  829. if (!ast_strlen_zero(jb_conf->impl)) {
  830. if (!strcasecmp(jb_conf->impl, "fixed")) {
  831. jb_impl_type = AST_JB_FIXED;
  832. } else if (!strcasecmp(jb_conf->impl, "adaptive")) {
  833. jb_impl_type = AST_JB_ADAPTIVE;
  834. } else {
  835. ast_log(LOG_WARNING, "Unknown Jitterbuffer type %s. Failed to create jitterbuffer.\n", jb_conf->impl);
  836. return -1;
  837. }
  838. }
  839. if (!(framedata->jb_impl = ast_jb_get_impl(jb_impl_type))) {
  840. return -1;
  841. }
  842. if (!(framedata->timer = ast_timer_open())) {
  843. return -1;
  844. }
  845. framedata->timer_fd = ast_timer_fd(framedata->timer);
  846. framedata->timer_interval = DEFAULT_TIMER_INTERVAL;
  847. ast_timer_set_rate(framedata->timer, 1000 / framedata->timer_interval);
  848. framedata->start_tv = ast_tvnow();
  849. framedata->jb_obj = framedata->jb_impl->create(&framedata->jb_conf);
  850. return 0;
  851. }
  852. void ast_jb_create_framehook(struct ast_channel *chan, struct ast_jb_conf *jb_conf, int prefer_existing)
  853. {
  854. struct jb_framedata *framedata;
  855. struct ast_datastore *datastore = NULL;
  856. struct ast_framehook_interface interface = {
  857. .version = AST_FRAMEHOOK_INTERFACE_VERSION,
  858. .event_cb = hook_event_cb,
  859. .destroy_cb = hook_destroy_cb,
  860. };
  861. int i = 0;
  862. /* If disabled, strip any existing jitterbuffer and don't replace it. */
  863. if (!strcasecmp(jb_conf->impl, "disabled")) {
  864. int *id;
  865. ast_channel_lock(chan);
  866. if ((datastore = ast_channel_datastore_find(chan, &jb_datastore, NULL))) {
  867. id = datastore->data;
  868. ast_framehook_detach(chan, *id);
  869. ast_channel_datastore_remove(chan, datastore);
  870. ast_datastore_free(datastore);
  871. }
  872. ast_channel_unlock(chan);
  873. return;
  874. }
  875. if (!(framedata = ast_calloc(1, sizeof(*framedata)))) {
  876. return;
  877. }
  878. if (jb_framedata_init(framedata, jb_conf)) {
  879. jb_framedata_destroy(framedata);
  880. return;
  881. }
  882. interface.data = framedata;
  883. ast_channel_lock(chan);
  884. i = ast_framehook_attach(chan, &interface);
  885. if (i >= 0) {
  886. int *id;
  887. if ((datastore = ast_channel_datastore_find(chan, &jb_datastore, NULL))) {
  888. /* There is already a jitterbuffer on the channel. */
  889. if (prefer_existing) {
  890. /* We prefer the existing jitterbuffer, so remove the new one and keep the old one. */
  891. ast_framehook_detach(chan, i);
  892. ast_channel_unlock(chan);
  893. return;
  894. }
  895. /* We prefer the new jitterbuffer, so strip the old one. */
  896. id = datastore->data;
  897. ast_framehook_detach(chan, *id);
  898. ast_channel_datastore_remove(chan, datastore);
  899. ast_datastore_free(datastore);
  900. }
  901. if (!(datastore = ast_datastore_alloc(&jb_datastore, NULL))) {
  902. ast_framehook_detach(chan, i);
  903. ast_channel_unlock(chan);
  904. return;
  905. }
  906. if (!(id = ast_calloc(1, sizeof(int)))) {
  907. ast_datastore_free(datastore);
  908. ast_framehook_detach(chan, i);
  909. ast_channel_unlock(chan);
  910. return;
  911. }
  912. *id = i; /* Store off the id. The channel is still locked so it is safe to access this ptr. */
  913. datastore->data = id;
  914. ast_channel_datastore_add(chan, datastore);
  915. ast_channel_set_fd(chan, AST_JITTERBUFFER_FD, framedata->timer_fd);
  916. } else {
  917. jb_framedata_destroy(framedata);
  918. framedata = NULL;
  919. }
  920. ast_channel_unlock(chan);
  921. }