abstract_jb.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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/abstract_jb.h"
  40. #include "fixedjitterbuf.h"
  41. #include "jitterbuf.h"
  42. /*! Internal jb flags */
  43. enum {
  44. JB_USE = (1 << 0),
  45. JB_TIMEBASE_INITIALIZED = (1 << 1),
  46. JB_CREATED = (1 << 2)
  47. };
  48. /* Implementation functions */
  49. /* fixed */
  50. static void *jb_create_fixed(struct ast_jb_conf *general_config);
  51. static void jb_destroy_fixed(void *jb);
  52. static int jb_put_first_fixed(void *jb, struct ast_frame *fin, long now);
  53. static int jb_put_fixed(void *jb, struct ast_frame *fin, long now);
  54. static int jb_get_fixed(void *jb, struct ast_frame **fout, long now, long interpl);
  55. static long jb_next_fixed(void *jb);
  56. static int jb_remove_fixed(void *jb, struct ast_frame **fout);
  57. static void jb_force_resynch_fixed(void *jb);
  58. static void jb_empty_and_reset_fixed(void *jb);
  59. /* adaptive */
  60. static void * jb_create_adaptive(struct ast_jb_conf *general_config);
  61. static void jb_destroy_adaptive(void *jb);
  62. static int jb_put_first_adaptive(void *jb, struct ast_frame *fin, long now);
  63. static int jb_put_adaptive(void *jb, struct ast_frame *fin, long now);
  64. static int jb_get_adaptive(void *jb, struct ast_frame **fout, long now, long interpl);
  65. static long jb_next_adaptive(void *jb);
  66. static int jb_remove_adaptive(void *jb, struct ast_frame **fout);
  67. static void jb_force_resynch_adaptive(void *jb);
  68. static void jb_empty_and_reset_adaptive(void *jb);
  69. /* Available jb implementations */
  70. static const struct ast_jb_impl avail_impl[] = {
  71. {
  72. .name = "fixed",
  73. .type = AST_JB_FIXED,
  74. .create = jb_create_fixed,
  75. .destroy = jb_destroy_fixed,
  76. .put_first = jb_put_first_fixed,
  77. .put = jb_put_fixed,
  78. .get = jb_get_fixed,
  79. .next = jb_next_fixed,
  80. .remove = jb_remove_fixed,
  81. .force_resync = jb_force_resynch_fixed,
  82. .empty_and_reset = jb_empty_and_reset_fixed,
  83. },
  84. {
  85. .name = "adaptive",
  86. .type = AST_JB_ADAPTIVE,
  87. .create = jb_create_adaptive,
  88. .destroy = jb_destroy_adaptive,
  89. .put_first = jb_put_first_adaptive,
  90. .put = jb_put_adaptive,
  91. .get = jb_get_adaptive,
  92. .next = jb_next_adaptive,
  93. .remove = jb_remove_adaptive,
  94. .force_resync = jb_force_resynch_adaptive,
  95. .empty_and_reset = jb_empty_and_reset_adaptive,
  96. }
  97. };
  98. static int default_impl = 0;
  99. /* Translations between impl and abstract return codes */
  100. static const int fixed_to_abstract_code[] =
  101. {AST_JB_IMPL_OK, AST_JB_IMPL_DROP, AST_JB_IMPL_INTERP, AST_JB_IMPL_NOFRAME};
  102. static const int adaptive_to_abstract_code[] =
  103. {AST_JB_IMPL_OK, AST_JB_IMPL_NOFRAME, AST_JB_IMPL_NOFRAME, AST_JB_IMPL_INTERP, AST_JB_IMPL_DROP, AST_JB_IMPL_OK};
  104. /* JB_GET actions (used only for the frames log) */
  105. static const char * const jb_get_actions[] = {"Delivered", "Dropped", "Interpolated", "No"};
  106. /*! \brief Macros for the frame log files */
  107. #define jb_framelog(...) do { \
  108. if (jb->logfile) { \
  109. fprintf(jb->logfile, __VA_ARGS__); \
  110. fflush(jb->logfile); \
  111. } \
  112. } while (0)
  113. /* Internal utility functions */
  114. static void jb_choose_impl(struct ast_channel *chan);
  115. static void jb_get_and_deliver(struct ast_channel *chan);
  116. static int create_jb(struct ast_channel *chan, struct ast_frame *first_frame);
  117. static long get_now(struct ast_jb *jb, struct timeval *tv);
  118. /* Interface ast jb functions impl */
  119. static void jb_choose_impl(struct ast_channel *chan)
  120. {
  121. struct ast_jb *jb = ast_channel_jb(chan);
  122. struct ast_jb_conf *jbconf = &jb->conf;
  123. const struct ast_jb_impl *test_impl;
  124. int i, avail_impl_count = ARRAY_LEN(avail_impl);
  125. jb->impl = &avail_impl[default_impl];
  126. if (ast_strlen_zero(jbconf->impl)) {
  127. return;
  128. }
  129. for (i = 0; i < avail_impl_count; i++) {
  130. test_impl = &avail_impl[i];
  131. if (!strcasecmp(jbconf->impl, test_impl->name)) {
  132. jb->impl = test_impl;
  133. return;
  134. }
  135. }
  136. }
  137. int ast_jb_do_usecheck(struct ast_channel *c0, struct ast_channel *c1)
  138. {
  139. struct ast_jb *jb0 = ast_channel_jb(c0);
  140. struct ast_jb *jb1 = ast_channel_jb(c1);
  141. struct ast_jb_conf *conf0 = &jb0->conf;
  142. struct ast_jb_conf *conf1 = &jb1->conf;
  143. int c0_wants_jitter = ast_channel_tech(c0)->properties & AST_CHAN_TP_WANTSJITTER;
  144. int c0_creates_jitter = ast_channel_tech(c0)->properties & AST_CHAN_TP_CREATESJITTER;
  145. int c0_jb_enabled = ast_test_flag(conf0, AST_JB_ENABLED);
  146. int c0_force_jb = ast_test_flag(conf0, AST_JB_FORCED);
  147. int c0_jb_timebase_initialized = ast_test_flag(jb0, JB_TIMEBASE_INITIALIZED);
  148. int c0_jb_created = ast_test_flag(jb0, JB_CREATED);
  149. int c1_wants_jitter = ast_channel_tech(c1)->properties & AST_CHAN_TP_WANTSJITTER;
  150. int c1_creates_jitter = ast_channel_tech(c1)->properties & AST_CHAN_TP_CREATESJITTER;
  151. int c1_jb_enabled = ast_test_flag(conf1, AST_JB_ENABLED);
  152. int c1_force_jb = ast_test_flag(conf1, AST_JB_FORCED);
  153. int c1_jb_timebase_initialized = ast_test_flag(jb1, JB_TIMEBASE_INITIALIZED);
  154. int c1_jb_created = ast_test_flag(jb1, JB_CREATED);
  155. int inuse = 0;
  156. /* Determine whether audio going to c0 needs a jitter buffer */
  157. if (((!c0_wants_jitter && c1_creates_jitter) || (c0_force_jb && c1_creates_jitter)) && c0_jb_enabled) {
  158. ast_set_flag(jb0, JB_USE);
  159. if (!c0_jb_timebase_initialized) {
  160. if (c1_jb_timebase_initialized) {
  161. memcpy(&jb0->timebase, &jb1->timebase, sizeof(struct timeval));
  162. } else {
  163. gettimeofday(&jb0->timebase, NULL);
  164. }
  165. ast_set_flag(jb0, JB_TIMEBASE_INITIALIZED);
  166. }
  167. if (!c0_jb_created) {
  168. jb_choose_impl(c0);
  169. }
  170. inuse = 1;
  171. }
  172. /* Determine whether audio going to c1 needs a jitter buffer */
  173. if (((!c1_wants_jitter && c0_creates_jitter) || (c1_force_jb && c0_creates_jitter)) && c1_jb_enabled) {
  174. ast_set_flag(jb1, JB_USE);
  175. if (!c1_jb_timebase_initialized) {
  176. if (c0_jb_timebase_initialized) {
  177. memcpy(&jb1->timebase, &jb0->timebase, sizeof(struct timeval));
  178. } else {
  179. gettimeofday(&jb1->timebase, NULL);
  180. }
  181. ast_set_flag(jb1, JB_TIMEBASE_INITIALIZED);
  182. }
  183. if (!c1_jb_created) {
  184. jb_choose_impl(c1);
  185. }
  186. inuse = 1;
  187. }
  188. return inuse;
  189. }
  190. int ast_jb_get_when_to_wakeup(struct ast_channel *c0, struct ast_channel *c1, int time_left)
  191. {
  192. struct ast_jb *jb0 = ast_channel_jb(c0);
  193. struct ast_jb *jb1 = ast_channel_jb(c1);
  194. int c0_use_jb = ast_test_flag(jb0, JB_USE);
  195. int c0_jb_is_created = ast_test_flag(jb0, JB_CREATED);
  196. int c1_use_jb = ast_test_flag(jb1, JB_USE);
  197. int c1_jb_is_created = ast_test_flag(jb1, JB_CREATED);
  198. int wait, wait0, wait1;
  199. struct timeval tv_now;
  200. if (time_left == 0) {
  201. /* No time left - the bridge will be retried */
  202. /* TODO: Test disable this */
  203. /*return 0;*/
  204. }
  205. if (time_left < 0) {
  206. time_left = INT_MAX;
  207. }
  208. gettimeofday(&tv_now, NULL);
  209. wait0 = (c0_use_jb && c0_jb_is_created) ? jb0->next - get_now(jb0, &tv_now) : time_left;
  210. wait1 = (c1_use_jb && c1_jb_is_created) ? jb1->next - get_now(jb1, &tv_now) : time_left;
  211. wait = wait0 < wait1 ? wait0 : wait1;
  212. wait = wait < time_left ? wait : time_left;
  213. if (wait == INT_MAX) {
  214. wait = -1;
  215. } else if (wait < 1) {
  216. /* don't let wait=0, because this can cause the pbx thread to loop without any sleeping at all */
  217. wait = 1;
  218. }
  219. return wait;
  220. }
  221. int ast_jb_put(struct ast_channel *chan, struct ast_frame *f)
  222. {
  223. struct ast_jb *jb = ast_channel_jb(chan);
  224. const struct ast_jb_impl *jbimpl = jb->impl;
  225. void *jbobj = jb->jbobj;
  226. struct ast_frame *frr;
  227. long now = 0;
  228. if (!ast_test_flag(jb, JB_USE))
  229. return -1;
  230. if (f->frametype != AST_FRAME_VOICE) {
  231. if (f->frametype == AST_FRAME_DTMF && ast_test_flag(jb, JB_CREATED)) {
  232. jb_framelog("JB_PUT {now=%ld}: Received DTMF frame. Force resynching jb...\n", now);
  233. jbimpl->force_resync(jbobj);
  234. }
  235. return -1;
  236. }
  237. /* We consider an enabled jitterbuffer should receive frames with valid timing info. */
  238. if (!ast_test_flag(f, AST_FRFLAG_HAS_TIMING_INFO) || f->len < 2 || f->ts < 0) {
  239. ast_log(LOG_WARNING, "%s received frame with invalid timing info: "
  240. "has_timing_info=%u, len=%ld, ts=%ld, src=%s\n",
  241. ast_channel_name(chan), ast_test_flag(f, AST_FRFLAG_HAS_TIMING_INFO), f->len, f->ts, f->src);
  242. return -1;
  243. }
  244. frr = ast_frdup(f);
  245. if (!frr) {
  246. ast_log(LOG_ERROR, "Failed to isolate frame for the jitterbuffer on channel '%s'\n", ast_channel_name(chan));
  247. return -1;
  248. }
  249. if (!ast_test_flag(jb, JB_CREATED)) {
  250. if (create_jb(chan, frr)) {
  251. ast_frfree(frr);
  252. /* Disable the jitterbuffer */
  253. ast_clear_flag(jb, JB_USE);
  254. return -1;
  255. }
  256. ast_set_flag(jb, JB_CREATED);
  257. return 0;
  258. } else {
  259. now = get_now(jb, NULL);
  260. if (jbimpl->put(jbobj, frr, now) != AST_JB_IMPL_OK) {
  261. jb_framelog("JB_PUT {now=%ld}: Dropped frame with ts=%ld and len=%ld\n", now, frr->ts, frr->len);
  262. ast_frfree(frr);
  263. /*return -1;*/
  264. /* TODO: Check this fix - should return 0 here, because the dropped frame shouldn't
  265. be delivered at all */
  266. return 0;
  267. }
  268. jb->next = jbimpl->next(jbobj);
  269. jb_framelog("JB_PUT {now=%ld}: Queued frame with ts=%ld and len=%ld\n", now, frr->ts, frr->len);
  270. return 0;
  271. }
  272. }
  273. void ast_jb_get_and_deliver(struct ast_channel *c0, struct ast_channel *c1)
  274. {
  275. struct ast_jb *jb0 = ast_channel_jb(c0);
  276. struct ast_jb *jb1 = ast_channel_jb(c1);
  277. int c0_use_jb = ast_test_flag(jb0, JB_USE);
  278. int c0_jb_is_created = ast_test_flag(jb0, JB_CREATED);
  279. int c1_use_jb = ast_test_flag(jb1, JB_USE);
  280. int c1_jb_is_created = ast_test_flag(jb1, JB_CREATED);
  281. if (c0_use_jb && c0_jb_is_created)
  282. jb_get_and_deliver(c0);
  283. if (c1_use_jb && c1_jb_is_created)
  284. jb_get_and_deliver(c1);
  285. }
  286. static void jb_get_and_deliver(struct ast_channel *chan)
  287. {
  288. struct ast_jb *jb = ast_channel_jb(chan);
  289. const struct ast_jb_impl *jbimpl = jb->impl;
  290. void *jbobj = jb->jbobj;
  291. struct ast_frame *f, finterp = { .frametype = AST_FRAME_VOICE, };
  292. long now;
  293. int interpolation_len, res;
  294. now = get_now(jb, NULL);
  295. jb->next = jbimpl->next(jbobj);
  296. if (now < jb->next) {
  297. jb_framelog("\tJB_GET {now=%ld}: now < next=%ld\n", now, jb->next);
  298. return;
  299. }
  300. while (now >= jb->next) {
  301. interpolation_len = ast_codec_interp_len(&jb->last_format);
  302. res = jbimpl->get(jbobj, &f, now, interpolation_len);
  303. switch (res) {
  304. case AST_JB_IMPL_OK:
  305. /* deliver the frame */
  306. ast_write(chan, f);
  307. case AST_JB_IMPL_DROP:
  308. jb_framelog("\tJB_GET {now=%ld}: %s frame with ts=%ld and len=%ld\n",
  309. now, jb_get_actions[res], f->ts, f->len);
  310. ast_format_copy(&jb->last_format, &f->subclass.format);
  311. ast_frfree(f);
  312. break;
  313. case AST_JB_IMPL_INTERP:
  314. /* interpolate a frame */
  315. f = &finterp;
  316. ast_format_copy(&f->subclass.format, &jb->last_format);
  317. f->samples = interpolation_len * 8;
  318. f->src = "JB interpolation";
  319. f->delivery = ast_tvadd(jb->timebase, ast_samp2tv(jb->next, 1000));
  320. f->offset = AST_FRIENDLY_OFFSET;
  321. /* deliver the interpolated frame */
  322. ast_write(chan, f);
  323. jb_framelog("\tJB_GET {now=%ld}: Interpolated frame with len=%d\n", now, interpolation_len);
  324. break;
  325. case AST_JB_IMPL_NOFRAME:
  326. ast_log(LOG_WARNING,
  327. "AST_JB_IMPL_NOFRAME is returned from the %s jb when now=%ld >= next=%ld, jbnext=%ld!\n",
  328. jbimpl->name, now, jb->next, jbimpl->next(jbobj));
  329. jb_framelog("\tJB_GET {now=%ld}: No frame for now!?\n", now);
  330. return;
  331. default:
  332. ast_log(LOG_ERROR, "This should never happen!\n");
  333. ast_assert("JB type unknown" == NULL);
  334. break;
  335. }
  336. jb->next = jbimpl->next(jbobj);
  337. }
  338. }
  339. static int create_jb(struct ast_channel *chan, struct ast_frame *frr)
  340. {
  341. struct ast_jb *jb = ast_channel_jb(chan);
  342. struct ast_jb_conf *jbconf = &jb->conf;
  343. const struct ast_jb_impl *jbimpl = jb->impl;
  344. void *jbobj;
  345. struct ast_channel *bridged;
  346. long now;
  347. char logfile_pathname[20 + AST_JB_IMPL_NAME_SIZE + 2*AST_CHANNEL_NAME + 1];
  348. char name1[AST_CHANNEL_NAME], name2[AST_CHANNEL_NAME], *tmp;
  349. int res;
  350. jbobj = jb->jbobj = jbimpl->create(jbconf);
  351. if (!jbobj) {
  352. ast_log(LOG_WARNING, "Failed to create jitterbuffer on channel '%s'\n", ast_channel_name(chan));
  353. return -1;
  354. }
  355. now = get_now(jb, NULL);
  356. res = jbimpl->put_first(jbobj, frr, now);
  357. /* The result of putting the first frame should not differ from OK. However, its possible
  358. some implementations (i.e. adaptive's when resynch_threshold is specified) to drop it. */
  359. if (res != AST_JB_IMPL_OK) {
  360. ast_log(LOG_WARNING, "Failed to put first frame in the jitterbuffer on channel '%s'\n", ast_channel_name(chan));
  361. /*
  362. jbimpl->destroy(jbobj);
  363. return -1;
  364. */
  365. }
  366. /* Init next */
  367. jb->next = jbimpl->next(jbobj);
  368. /* Init last format for a first time. */
  369. ast_format_copy(&jb->last_format, &frr->subclass.format);
  370. /* Create a frame log file */
  371. if (ast_test_flag(jbconf, AST_JB_LOG)) {
  372. char safe_logfile[30] = "/tmp/logfile-XXXXXX";
  373. int safe_fd;
  374. snprintf(name2, sizeof(name2), "%s", ast_channel_name(chan));
  375. while ((tmp = strchr(name2, '/'))) {
  376. *tmp = '#';
  377. }
  378. bridged = ast_bridged_channel(chan);
  379. /* We should always have bridged chan if a jitterbuffer is in use */
  380. ast_assert(bridged != NULL);
  381. snprintf(name1, sizeof(name1), "%s", ast_channel_name(bridged));
  382. while ((tmp = strchr(name1, '/'))) {
  383. *tmp = '#';
  384. }
  385. snprintf(logfile_pathname, sizeof(logfile_pathname),
  386. "/tmp/ast_%s_jb_%s--%s.log", jbimpl->name, name1, name2);
  387. unlink(logfile_pathname);
  388. safe_fd = mkstemp(safe_logfile);
  389. if (safe_fd < 0 || link(safe_logfile, logfile_pathname) || unlink(safe_logfile) || !(jb->logfile = fdopen(safe_fd, "w+b"))) {
  390. ast_log(LOG_ERROR, "Failed to create frame log file with pathname '%s': %s\n", logfile_pathname, strerror(errno));
  391. jb->logfile = NULL;
  392. if (safe_fd > -1) {
  393. close(safe_fd);
  394. }
  395. }
  396. if (res == AST_JB_IMPL_OK) {
  397. jb_framelog("JB_PUT_FIRST {now=%ld}: Queued frame with ts=%ld and len=%ld\n",
  398. now, frr->ts, frr->len);
  399. } else {
  400. jb_framelog("JB_PUT_FIRST {now=%ld}: Dropped frame with ts=%ld and len=%ld\n",
  401. now, frr->ts, frr->len);
  402. }
  403. }
  404. ast_verb(3, "%s jitterbuffer created on channel %s\n", jbimpl->name, ast_channel_name(chan));
  405. /* Free the frame if it has not been queued in the jb */
  406. if (res != AST_JB_IMPL_OK) {
  407. ast_frfree(frr);
  408. }
  409. return 0;
  410. }
  411. void ast_jb_destroy(struct ast_channel *chan)
  412. {
  413. struct ast_jb *jb = ast_channel_jb(chan);
  414. const struct ast_jb_impl *jbimpl = jb->impl;
  415. void *jbobj = jb->jbobj;
  416. struct ast_frame *f;
  417. if (jb->logfile) {
  418. fclose(jb->logfile);
  419. jb->logfile = NULL;
  420. }
  421. if (ast_test_flag(jb, JB_CREATED)) {
  422. /* Remove and free all frames still queued in jb */
  423. while (jbimpl->remove(jbobj, &f) == AST_JB_IMPL_OK) {
  424. ast_frfree(f);
  425. }
  426. jbimpl->destroy(jbobj);
  427. jb->jbobj = NULL;
  428. ast_clear_flag(jb, JB_CREATED);
  429. ast_verb(3, "%s jitterbuffer destroyed on channel %s\n", jbimpl->name, ast_channel_name(chan));
  430. }
  431. }
  432. static long get_now(struct ast_jb *jb, struct timeval *when)
  433. {
  434. struct timeval now;
  435. if (!when) {
  436. when = &now;
  437. gettimeofday(when, NULL);
  438. }
  439. return ast_tvdiff_ms(*when, jb->timebase);
  440. }
  441. int ast_jb_read_conf(struct ast_jb_conf *conf, const char *varname, const char *value)
  442. {
  443. int prefixlen = sizeof(AST_JB_CONF_PREFIX) - 1;
  444. const char *name;
  445. int tmp;
  446. if (strncasecmp(AST_JB_CONF_PREFIX, varname, prefixlen)) {
  447. return -1;
  448. }
  449. name = varname + prefixlen;
  450. if (!strcasecmp(name, AST_JB_CONF_ENABLE)) {
  451. ast_set2_flag(conf, ast_true(value), AST_JB_ENABLED);
  452. } else if (!strcasecmp(name, AST_JB_CONF_FORCE)) {
  453. ast_set2_flag(conf, ast_true(value), AST_JB_FORCED);
  454. } else if (!strcasecmp(name, AST_JB_CONF_MAX_SIZE)) {
  455. if ((tmp = atoi(value)) > 0)
  456. conf->max_size = tmp;
  457. } else if (!strcasecmp(name, AST_JB_CONF_RESYNCH_THRESHOLD)) {
  458. if ((tmp = atoi(value)) > 0)
  459. conf->resync_threshold = tmp;
  460. } else if (!strcasecmp(name, AST_JB_CONF_IMPL)) {
  461. if (!ast_strlen_zero(value))
  462. snprintf(conf->impl, sizeof(conf->impl), "%s", value);
  463. } else if (!strcasecmp(name, AST_JB_CONF_TARGET_EXTRA)) {
  464. if (sscanf(value, "%30d", &tmp) == 1) {
  465. conf->target_extra = tmp;
  466. }
  467. } else if (!strcasecmp(name, AST_JB_CONF_LOG)) {
  468. ast_set2_flag(conf, ast_true(value), AST_JB_LOG);
  469. } else {
  470. return -1;
  471. }
  472. return 0;
  473. }
  474. void ast_jb_configure(struct ast_channel *chan, const struct ast_jb_conf *conf)
  475. {
  476. memcpy(&ast_channel_jb(chan)->conf, conf, sizeof(*conf));
  477. }
  478. void ast_jb_get_config(const struct ast_channel *chan, struct ast_jb_conf *conf)
  479. {
  480. memcpy(conf, &ast_channel_jb((struct ast_channel *) chan)->conf, sizeof(*conf));
  481. }
  482. void ast_jb_empty_and_reset(struct ast_channel *c0, struct ast_channel *c1)
  483. {
  484. struct ast_jb *jb0 = ast_channel_jb(c0);
  485. struct ast_jb *jb1 = ast_channel_jb(c1);
  486. int c0_use_jb = ast_test_flag(jb0, JB_USE);
  487. int c0_jb_is_created = ast_test_flag(jb0, JB_CREATED);
  488. int c1_use_jb = ast_test_flag(jb1, JB_USE);
  489. int c1_jb_is_created = ast_test_flag(jb1, JB_CREATED);
  490. if (c0_use_jb && c0_jb_is_created && jb0->impl->empty_and_reset) {
  491. jb0->impl->empty_and_reset(jb0->jbobj);
  492. }
  493. if (c1_use_jb && c1_jb_is_created && jb1->impl->empty_and_reset) {
  494. jb1->impl->empty_and_reset(jb1->jbobj);
  495. }
  496. }
  497. /* Implementation functions */
  498. /* fixed */
  499. static void * jb_create_fixed(struct ast_jb_conf *general_config)
  500. {
  501. struct fixed_jb_conf conf;
  502. conf.jbsize = general_config->max_size;
  503. conf.resync_threshold = general_config->resync_threshold;
  504. return fixed_jb_new(&conf);
  505. }
  506. static void jb_destroy_fixed(void *jb)
  507. {
  508. struct fixed_jb *fixedjb = (struct fixed_jb *) jb;
  509. /* Ensure the fixed jb is empty - otherwise it will raise an ASSERT */
  510. jb_empty_and_reset_fixed(jb);
  511. /* destroy the jb */
  512. fixed_jb_destroy(fixedjb);
  513. }
  514. static int jb_put_first_fixed(void *jb, struct ast_frame *fin, long now)
  515. {
  516. struct fixed_jb *fixedjb = (struct fixed_jb *) jb;
  517. int res;
  518. res = fixed_jb_put_first(fixedjb, fin, fin->len, fin->ts, now);
  519. return fixed_to_abstract_code[res];
  520. }
  521. static int jb_put_fixed(void *jb, struct ast_frame *fin, long now)
  522. {
  523. struct fixed_jb *fixedjb = (struct fixed_jb *) jb;
  524. int res;
  525. res = fixed_jb_put(fixedjb, fin, fin->len, fin->ts, now);
  526. return fixed_to_abstract_code[res];
  527. }
  528. static int jb_get_fixed(void *jb, struct ast_frame **fout, long now, long interpl)
  529. {
  530. struct fixed_jb *fixedjb = (struct fixed_jb *) jb;
  531. struct fixed_jb_frame frame = { .data = &ast_null_frame };
  532. int res;
  533. res = fixed_jb_get(fixedjb, &frame, now, interpl);
  534. *fout = frame.data;
  535. return fixed_to_abstract_code[res];
  536. }
  537. static long jb_next_fixed(void *jb)
  538. {
  539. struct fixed_jb *fixedjb = (struct fixed_jb *) jb;
  540. return fixed_jb_next(fixedjb);
  541. }
  542. static int jb_remove_fixed(void *jb, struct ast_frame **fout)
  543. {
  544. struct fixed_jb *fixedjb = (struct fixed_jb *) jb;
  545. struct fixed_jb_frame frame;
  546. int res;
  547. res = fixed_jb_remove(fixedjb, &frame);
  548. *fout = frame.data;
  549. return fixed_to_abstract_code[res];
  550. }
  551. static void jb_force_resynch_fixed(void *jb)
  552. {
  553. struct fixed_jb *fixedjb = (struct fixed_jb *) jb;
  554. fixed_jb_set_force_resynch(fixedjb);
  555. }
  556. static void jb_empty_and_reset_fixed(void *jb)
  557. {
  558. struct fixed_jb *fixedjb = jb;
  559. struct fixed_jb_frame f;
  560. while (fixed_jb_remove(fixedjb, &f) == FIXED_JB_OK) {
  561. ast_frfree(f.data);
  562. }
  563. }
  564. /* adaptive */
  565. static void *jb_create_adaptive(struct ast_jb_conf *general_config)
  566. {
  567. jb_conf jbconf;
  568. jitterbuf *adaptivejb;
  569. adaptivejb = jb_new();
  570. if (adaptivejb) {
  571. jbconf.max_jitterbuf = general_config->max_size;
  572. jbconf.resync_threshold = general_config->resync_threshold;
  573. jbconf.max_contig_interp = 10;
  574. jbconf.target_extra = general_config->target_extra;
  575. jb_setconf(adaptivejb, &jbconf);
  576. }
  577. return adaptivejb;
  578. }
  579. static void jb_destroy_adaptive(void *jb)
  580. {
  581. jitterbuf *adaptivejb = (jitterbuf *) jb;
  582. jb_destroy(adaptivejb);
  583. }
  584. static int jb_put_first_adaptive(void *jb, struct ast_frame *fin, long now)
  585. {
  586. return jb_put_adaptive(jb, fin, now);
  587. }
  588. static int jb_put_adaptive(void *jb, struct ast_frame *fin, long now)
  589. {
  590. jitterbuf *adaptivejb = (jitterbuf *) jb;
  591. int res;
  592. res = jb_put(adaptivejb, fin, JB_TYPE_VOICE, fin->len, fin->ts, now);
  593. return adaptive_to_abstract_code[res];
  594. }
  595. static int jb_get_adaptive(void *jb, struct ast_frame **fout, long now, long interpl)
  596. {
  597. jitterbuf *adaptivejb = (jitterbuf *) jb;
  598. jb_frame frame = { .data = &ast_null_frame };
  599. int res;
  600. res = jb_get(adaptivejb, &frame, now, interpl);
  601. *fout = frame.data;
  602. return adaptive_to_abstract_code[res];
  603. }
  604. static long jb_next_adaptive(void *jb)
  605. {
  606. jitterbuf *adaptivejb = (jitterbuf *) jb;
  607. return jb_next(adaptivejb);
  608. }
  609. static int jb_remove_adaptive(void *jb, struct ast_frame **fout)
  610. {
  611. jitterbuf *adaptivejb = (jitterbuf *) jb;
  612. jb_frame frame;
  613. int res;
  614. res = jb_getall(adaptivejb, &frame);
  615. *fout = frame.data;
  616. return adaptive_to_abstract_code[res];
  617. }
  618. static void jb_force_resynch_adaptive(void *jb)
  619. {
  620. }
  621. static void jb_empty_and_reset_adaptive(void *jb)
  622. {
  623. jitterbuf *adaptivejb = jb;
  624. jb_frame f;
  625. while (jb_getall(adaptivejb, &f) == JB_OK) {
  626. ast_frfree(f.data);
  627. }
  628. jb_reset(adaptivejb);
  629. }
  630. const struct ast_jb_impl *ast_jb_get_impl(enum ast_jb_type type)
  631. {
  632. int i;
  633. for (i = 0; i < ARRAY_LEN(avail_impl); i++) {
  634. if (avail_impl[i].type == type) {
  635. return &avail_impl[i];
  636. }
  637. }
  638. return NULL;
  639. }