abstract_jb.c 32 KB

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