Wed Jan 8 2020 09:49:39

Asterisk developer's documentation


abstract_jb.c
Go to the documentation of this file.
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 
23 /*! \file
24  *
25  * \brief Common implementation-independent jitterbuffer stuff.
26  *
27  * \author Slav Klenov <slav@securax.org>
28  *
29  *
30  */
31 
32 /*** MODULEINFO
33  <support_level>core</support_level>
34  ***/
35 
36 #include "asterisk.h"
37 
38 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 413586 $")
39 
40 #include "asterisk/frame.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/term.h"
43 #include "asterisk/utils.h"
44 
45 #include "asterisk/abstract_jb.h"
46 #include "fixedjitterbuf.h"
47 #include "jitterbuf.h"
48 
49 /*! Internal jb flags */
50 enum {
51  JB_USE = (1 << 0),
53  JB_CREATED = (1 << 2)
54 };
55 
56 /* Hooks for the abstract jb implementation */
57 
58 /*! \brief Create */
59 typedef void * (*jb_create_impl)(struct ast_jb_conf *general_config, long resynch_threshold);
60 /*! \brief Destroy */
61 typedef void (*jb_destroy_impl)(void *jb);
62 /*! \brief Put first frame */
63 typedef int (*jb_put_first_impl)(void *jb, struct ast_frame *fin, long now);
64 /*! \brief Put frame */
65 typedef int (*jb_put_impl)(void *jb, struct ast_frame *fin, long now);
66 /*! \brief Get frame for now */
67 typedef int (*jb_get_impl)(void *jb, struct ast_frame **fout, long now, long interpl);
68 /*! \brief Get next */
69 typedef long (*jb_next_impl)(void *jb);
70 /*! \brief Remove first frame */
71 typedef int (*jb_remove_impl)(void *jb, struct ast_frame **fout);
72 /*! \brief Force resynch */
73 typedef void (*jb_force_resynch_impl)(void *jb);
74 /*! \brief Empty and reset jb */
75 typedef void (*jb_empty_and_reset_impl)(void *jb);
76 
77 /*!
78  * \brief Jitterbuffer implementation private struct.
79  */
81 {
92 };
93 
94 /* Implementation functions */
95 /* fixed */
96 static void *jb_create_fixed(struct ast_jb_conf *general_config, long resynch_threshold);
97 static void jb_destroy_fixed(void *jb);
98 static int jb_put_first_fixed(void *jb, struct ast_frame *fin, long now);
99 static int jb_put_fixed(void *jb, struct ast_frame *fin, long now);
100 static int jb_get_fixed(void *jb, struct ast_frame **fout, long now, long interpl);
101 static long jb_next_fixed(void *jb);
102 static int jb_remove_fixed(void *jb, struct ast_frame **fout);
103 static void jb_force_resynch_fixed(void *jb);
104 static void jb_empty_and_reset_fixed(void *jb);
105 /* adaptive */
106 static void * jb_create_adaptive(struct ast_jb_conf *general_config, long resynch_threshold);
107 static void jb_destroy_adaptive(void *jb);
108 static int jb_put_first_adaptive(void *jb, struct ast_frame *fin, long now);
109 static int jb_put_adaptive(void *jb, struct ast_frame *fin, long now);
110 static int jb_get_adaptive(void *jb, struct ast_frame **fout, long now, long interpl);
111 static long jb_next_adaptive(void *jb);
112 static int jb_remove_adaptive(void *jb, struct ast_frame **fout);
113 static void jb_force_resynch_adaptive(void *jb);
114 static void jb_empty_and_reset_adaptive(void *jb);
115 
116 /* Available jb implementations */
117 static const struct ast_jb_impl avail_impl[] = {
118  {
119  .name = "fixed",
120  .create = jb_create_fixed,
121  .destroy = jb_destroy_fixed,
122  .put_first = jb_put_first_fixed,
123  .put = jb_put_fixed,
124  .get = jb_get_fixed,
125  .next = jb_next_fixed,
126  .remove = jb_remove_fixed,
127  .force_resync = jb_force_resynch_fixed,
128  .empty_and_reset = jb_empty_and_reset_fixed,
129  },
130  {
131  .name = "adaptive",
132  .create = jb_create_adaptive,
133  .destroy = jb_destroy_adaptive,
134  .put_first = jb_put_first_adaptive,
135  .put = jb_put_adaptive,
136  .get = jb_get_adaptive,
137  .next = jb_next_adaptive,
138  .remove = jb_remove_adaptive,
139  .force_resync = jb_force_resynch_adaptive,
140  .empty_and_reset = jb_empty_and_reset_adaptive,
141  }
142 };
143 
144 static int default_impl = 0;
145 
146 
147 /*! Abstract return codes */
148 enum {
153 };
154 
155 /* Translations between impl and abstract return codes */
156 static const int fixed_to_abstract_code[] =
158 static const int adaptive_to_abstract_code[] =
160 
161 /* JB_GET actions (used only for the frames log) */
162 static const char * const jb_get_actions[] = {"Delivered", "Dropped", "Interpolated", "No"};
163 
164 /*! \brief Macros for the frame log files */
165 #define jb_framelog(...) do { \
166  if (jb->logfile) { \
167  fprintf(jb->logfile, __VA_ARGS__); \
168  fflush(jb->logfile); \
169  } \
170 } while (0)
171 
172 
173 /* Internal utility functions */
174 static void jb_choose_impl(struct ast_channel *chan);
175 static void jb_get_and_deliver(struct ast_channel *chan);
176 static int create_jb(struct ast_channel *chan, struct ast_frame *first_frame);
177 static long get_now(struct ast_jb *jb, struct timeval *tv);
178 
179 
180 /* Interface ast jb functions impl */
181 
182 
183 static void jb_choose_impl(struct ast_channel *chan)
184 {
185  struct ast_jb *jb = &chan->jb;
186  struct ast_jb_conf *jbconf = &jb->conf;
187  const struct ast_jb_impl *test_impl;
188  int i, avail_impl_count = ARRAY_LEN(avail_impl);
189 
190  jb->impl = &avail_impl[default_impl];
191 
192  if (ast_strlen_zero(jbconf->impl)) {
193  return;
194  }
195 
196  for (i = 0; i < avail_impl_count; i++) {
197  test_impl = &avail_impl[i];
198  if (!strcasecmp(jbconf->impl, test_impl->name)) {
199  jb->impl = test_impl;
200  return;
201  }
202  }
203 }
204 
205 int ast_jb_do_usecheck(struct ast_channel *c0, struct ast_channel *c1)
206 {
207  struct ast_jb *jb0 = &c0->jb;
208  struct ast_jb *jb1 = &c1->jb;
209  struct ast_jb_conf *conf0 = &jb0->conf;
210  struct ast_jb_conf *conf1 = &jb1->conf;
211  int c0_wants_jitter = c0->tech->properties & AST_CHAN_TP_WANTSJITTER;
212  int c0_creates_jitter = c0->tech->properties & AST_CHAN_TP_CREATESJITTER;
213  int c0_jb_enabled = ast_test_flag(conf0, AST_JB_ENABLED);
214  int c0_force_jb = ast_test_flag(conf0, AST_JB_FORCED);
215  int c0_jb_timebase_initialized = ast_test_flag(jb0, JB_TIMEBASE_INITIALIZED);
216  int c0_jb_created = ast_test_flag(jb0, JB_CREATED);
217  int c1_wants_jitter = c1->tech->properties & AST_CHAN_TP_WANTSJITTER;
218  int c1_creates_jitter = c1->tech->properties & AST_CHAN_TP_CREATESJITTER;
219  int c1_jb_enabled = ast_test_flag(conf1, AST_JB_ENABLED);
220  int c1_force_jb = ast_test_flag(conf1, AST_JB_FORCED);
221  int c1_jb_timebase_initialized = ast_test_flag(jb1, JB_TIMEBASE_INITIALIZED);
222  int c1_jb_created = ast_test_flag(jb1, JB_CREATED);
223  int inuse = 0;
224 
225  /* Determine whether audio going to c0 needs a jitter buffer */
226  if (((!c0_wants_jitter && c1_creates_jitter) || (c0_force_jb && c1_creates_jitter)) && c0_jb_enabled) {
227  ast_set_flag(jb0, JB_USE);
228  if (!c0_jb_timebase_initialized) {
229  if (c1_jb_timebase_initialized) {
230  memcpy(&jb0->timebase, &jb1->timebase, sizeof(struct timeval));
231  } else {
232  gettimeofday(&jb0->timebase, NULL);
233  }
235  }
236 
237  if (!c0_jb_created) {
238  jb_choose_impl(c0);
239  }
240 
241  inuse = 1;
242  }
243 
244  /* Determine whether audio going to c1 needs a jitter buffer */
245  if (((!c1_wants_jitter && c0_creates_jitter) || (c1_force_jb && c0_creates_jitter)) && c1_jb_enabled) {
246  ast_set_flag(jb1, JB_USE);
247  if (!c1_jb_timebase_initialized) {
248  if (c0_jb_timebase_initialized) {
249  memcpy(&jb1->timebase, &jb0->timebase, sizeof(struct timeval));
250  } else {
251  gettimeofday(&jb1->timebase, NULL);
252  }
254  }
255 
256  if (!c1_jb_created) {
257  jb_choose_impl(c1);
258  }
259 
260  inuse = 1;
261  }
262 
263  return inuse;
264 }
265 
266 int ast_jb_get_when_to_wakeup(struct ast_channel *c0, struct ast_channel *c1, int time_left)
267 {
268  struct ast_jb *jb0 = &c0->jb;
269  struct ast_jb *jb1 = &c1->jb;
270  int c0_use_jb = ast_test_flag(jb0, JB_USE);
271  int c0_jb_is_created = ast_test_flag(jb0, JB_CREATED);
272  int c1_use_jb = ast_test_flag(jb1, JB_USE);
273  int c1_jb_is_created = ast_test_flag(jb1, JB_CREATED);
274  int wait, wait0, wait1;
275  struct timeval tv_now;
276 
277  if (time_left == 0) {
278  /* No time left - the bridge will be retried */
279  /* TODO: Test disable this */
280  /*return 0;*/
281  }
282 
283  if (time_left < 0) {
284  time_left = INT_MAX;
285  }
286 
287  gettimeofday(&tv_now, NULL);
288 
289  wait0 = (c0_use_jb && c0_jb_is_created) ? jb0->next - get_now(jb0, &tv_now) : time_left;
290  wait1 = (c1_use_jb && c1_jb_is_created) ? jb1->next - get_now(jb1, &tv_now) : time_left;
291 
292  wait = wait0 < wait1 ? wait0 : wait1;
293  wait = wait < time_left ? wait : time_left;
294 
295  if (wait == INT_MAX) {
296  wait = -1;
297  } else if (wait < 1) {
298  /* don't let wait=0, because this can cause the pbx thread to loop without any sleeping at all */
299  wait = 1;
300  }
301 
302  return wait;
303 }
304 
305 
306 int ast_jb_put(struct ast_channel *chan, struct ast_frame *f)
307 {
308  struct ast_jb *jb = &chan->jb;
309  const struct ast_jb_impl *jbimpl = jb->impl;
310  void *jbobj = jb->jbobj;
311  struct ast_frame *frr;
312  long now = 0;
313 
314  if (!ast_test_flag(jb, JB_USE))
315  return -1;
316 
317  if (f->frametype != AST_FRAME_VOICE) {
318  if (f->frametype == AST_FRAME_DTMF && ast_test_flag(jb, JB_CREATED)) {
319  jb_framelog("JB_PUT {now=%ld}: Received DTMF frame. Force resynching jb...\n", now);
320  jbimpl->force_resync(jbobj);
321  }
322 
323  return -1;
324  }
325 
326  /* We consider an enabled jitterbuffer should receive frames with valid timing info. */
327  if (!ast_test_flag(f, AST_FRFLAG_HAS_TIMING_INFO) || f->len < 2 || f->ts < 0) {
328  ast_log(LOG_WARNING, "%s received frame with invalid timing info: "
329  "has_timing_info=%u, len=%ld, ts=%ld, src=%s\n",
330  chan->name, ast_test_flag(f, AST_FRFLAG_HAS_TIMING_INFO), f->len, f->ts, f->src);
331  return -1;
332  }
333 
334  frr = ast_frdup(f);
335 
336  if (!frr) {
337  ast_log(LOG_ERROR, "Failed to isolate frame for the jitterbuffer on channel '%s'\n", chan->name);
338  return -1;
339  }
340 
341  if (!ast_test_flag(jb, JB_CREATED)) {
342  if (create_jb(chan, frr)) {
343  ast_frfree(frr);
344  /* Disable the jitterbuffer */
345  ast_clear_flag(jb, JB_USE);
346  return -1;
347  }
348 
350  return 0;
351  } else {
352  now = get_now(jb, NULL);
353  if (jbimpl->put(jbobj, frr, now) != JB_IMPL_OK) {
354  jb_framelog("JB_PUT {now=%ld}: Dropped frame with ts=%ld and len=%ld\n", now, frr->ts, frr->len);
355  ast_frfree(frr);
356  /*return -1;*/
357  /* TODO: Check this fix - should return 0 here, because the dropped frame shouldn't
358  be delivered at all */
359  return 0;
360  }
361 
362  jb->next = jbimpl->next(jbobj);
363 
364  jb_framelog("JB_PUT {now=%ld}: Queued frame with ts=%ld and len=%ld\n", now, frr->ts, frr->len);
365 
366  return 0;
367  }
368 }
369 
370 
371 void ast_jb_get_and_deliver(struct ast_channel *c0, struct ast_channel *c1)
372 {
373  struct ast_jb *jb0 = &c0->jb;
374  struct ast_jb *jb1 = &c1->jb;
375  int c0_use_jb = ast_test_flag(jb0, JB_USE);
376  int c0_jb_is_created = ast_test_flag(jb0, JB_CREATED);
377  int c1_use_jb = ast_test_flag(jb1, JB_USE);
378  int c1_jb_is_created = ast_test_flag(jb1, JB_CREATED);
379 
380  if (c0_use_jb && c0_jb_is_created)
381  jb_get_and_deliver(c0);
382 
383  if (c1_use_jb && c1_jb_is_created)
384  jb_get_and_deliver(c1);
385 }
386 
387 
388 static void jb_get_and_deliver(struct ast_channel *chan)
389 {
390  struct ast_jb *jb = &chan->jb;
391  const struct ast_jb_impl *jbimpl = jb->impl;
392  void *jbobj = jb->jbobj;
393  struct ast_frame *f, finterp = { .frametype = AST_FRAME_VOICE, };
394  long now;
395  int interpolation_len, res;
396 
397  now = get_now(jb, NULL);
398  jb->next = jbimpl->next(jbobj);
399  if (now < jb->next) {
400  jb_framelog("\tJB_GET {now=%ld}: now < next=%ld\n", now, jb->next);
401  return;
402  }
403 
404  while (now >= jb->next) {
405  interpolation_len = ast_codec_interp_len(jb->last_format);
406 
407  res = jbimpl->get(jbobj, &f, now, interpolation_len);
408 
409  switch (res) {
410  case JB_IMPL_OK:
411  /* deliver the frame */
412  ast_write(chan, f);
413  /* Fall through intentionally */
414  case JB_IMPL_DROP:
415  jb_framelog("\tJB_GET {now=%ld}: %s frame with ts=%ld and len=%ld\n",
416  now, jb_get_actions[res], f->ts, f->len);
417  jb->last_format = f->subclass.codec;
418  ast_frfree(f);
419  break;
420  case JB_IMPL_INTERP:
421  /* interpolate a frame */
422  f = &finterp;
423  f->subclass.codec = jb->last_format;
424  f->samples = interpolation_len * 8;
425  f->src = "JB interpolation";
426  f->delivery = ast_tvadd(jb->timebase, ast_samp2tv(jb->next, 1000));
428  /* deliver the interpolated frame */
429  ast_write(chan, f);
430  jb_framelog("\tJB_GET {now=%ld}: Interpolated frame with len=%d\n", now, interpolation_len);
431  break;
432  case JB_IMPL_NOFRAME:
434  "JB_IMPL_NOFRAME is returned from the %s jb when now=%ld >= next=%ld, jbnext=%ld!\n",
435  jbimpl->name, now, jb->next, jbimpl->next(jbobj));
436  jb_framelog("\tJB_GET {now=%ld}: No frame for now!?\n", now);
437  return;
438  default:
439  ast_log(LOG_ERROR, "This should never happen!\n");
440  ast_assert("JB type unknown" == NULL);
441  break;
442  }
443 
444  jb->next = jbimpl->next(jbobj);
445  }
446 }
447 
448 
449 static int create_jb(struct ast_channel *chan, struct ast_frame *frr)
450 {
451  struct ast_jb *jb = &chan->jb;
452  struct ast_jb_conf *jbconf = &jb->conf;
453  const struct ast_jb_impl *jbimpl = jb->impl;
454  void *jbobj;
455  struct ast_channel *bridged;
456  long now;
457  char logfile_pathname[20 + AST_JB_IMPL_NAME_SIZE + 2*AST_CHANNEL_NAME + 1];
458  char name1[AST_CHANNEL_NAME], name2[AST_CHANNEL_NAME], *tmp;
459  int res;
460 
461  jbobj = jb->jbobj = jbimpl->create(jbconf, jbconf->resync_threshold);
462  if (!jbobj) {
463  ast_log(LOG_WARNING, "Failed to create jitterbuffer on channel '%s'\n", chan->name);
464  return -1;
465  }
466 
467  now = get_now(jb, NULL);
468  res = jbimpl->put_first(jbobj, frr, now);
469 
470  /* The result of putting the first frame should not differ from OK. However, its possible
471  some implementations (i.e. adaptive's when resynch_threshold is specified) to drop it. */
472  if (res != JB_IMPL_OK) {
473  ast_log(LOG_WARNING, "Failed to put first frame in the jitterbuffer on channel '%s'\n", chan->name);
474  /*
475  jbimpl->destroy(jbobj);
476  return -1;
477  */
478  }
479 
480  /* Init next */
481  jb->next = jbimpl->next(jbobj);
482 
483  /* Init last format for a first time. */
484  jb->last_format = frr->subclass.codec;
485 
486  /* Create a frame log file */
487  if (ast_test_flag(jbconf, AST_JB_LOG)) {
488  char safe_logfile[30] = "/tmp/logfile-XXXXXX";
489  int safe_fd;
490  snprintf(name2, sizeof(name2), "%s", chan->name);
491  while ((tmp = strchr(name2, '/'))) {
492  *tmp = '#';
493  }
494 
495  bridged = ast_bridged_channel(chan);
496  /* We should always have bridged chan if a jitterbuffer is in use */
497  ast_assert(bridged != NULL);
498 
499  snprintf(name1, sizeof(name1), "%s", bridged->name);
500  while ((tmp = strchr(name1, '/'))) {
501  *tmp = '#';
502  }
503 
504  snprintf(logfile_pathname, sizeof(logfile_pathname),
505  "/tmp/ast_%s_jb_%s--%s.log", jbimpl->name, name1, name2);
506  unlink(logfile_pathname);
507  safe_fd = mkstemp(safe_logfile);
508  if (safe_fd < 0 || link(safe_logfile, logfile_pathname) || unlink(safe_logfile) || !(jb->logfile = fdopen(safe_fd, "w+b"))) {
509  ast_log(LOG_ERROR, "Failed to create frame log file with pathname '%s': %s\n", logfile_pathname, strerror(errno));
510  jb->logfile = NULL;
511  if (safe_fd > -1) {
512  close(safe_fd);
513  }
514  }
515 
516  if (res == JB_IMPL_OK) {
517  jb_framelog("JB_PUT_FIRST {now=%ld}: Queued frame with ts=%ld and len=%ld\n",
518  now, frr->ts, frr->len);
519  } else {
520  jb_framelog("JB_PUT_FIRST {now=%ld}: Dropped frame with ts=%ld and len=%ld\n",
521  now, frr->ts, frr->len);
522  }
523  }
524 
525  ast_verb(3, "%s jitterbuffer created on channel %s\n", jbimpl->name, chan->name);
526 
527  /* Free the frame if it has not been queued in the jb */
528  if (res != JB_IMPL_OK) {
529  ast_frfree(frr);
530  }
531 
532  return 0;
533 }
534 
535 
536 void ast_jb_destroy(struct ast_channel *chan)
537 {
538  struct ast_jb *jb = &chan->jb;
539  const struct ast_jb_impl *jbimpl = jb->impl;
540  void *jbobj = jb->jbobj;
541  struct ast_frame *f;
542 
543  if (jb->logfile) {
544  fclose(jb->logfile);
545  jb->logfile = NULL;
546  }
547 
548  if (ast_test_flag(jb, JB_CREATED)) {
549  /* Remove and free all frames still queued in jb */
550  while (jbimpl->remove(jbobj, &f) == JB_IMPL_OK) {
551  ast_frfree(f);
552  }
553 
554  jbimpl->destroy(jbobj);
555  jb->jbobj = NULL;
556 
558 
559  ast_verb(3, "%s jitterbuffer destroyed on channel %s\n", jbimpl->name, chan->name);
560  }
561 }
562 
563 
564 static long get_now(struct ast_jb *jb, struct timeval *when)
565 {
566  struct timeval now;
567 
568  if (!when) {
569  when = &now;
570  gettimeofday(when, NULL);
571  }
572 
573  return ast_tvdiff_ms(*when, jb->timebase);
574 }
575 
576 
577 int ast_jb_read_conf(struct ast_jb_conf *conf, const char *varname, const char *value)
578 {
579  int prefixlen = sizeof(AST_JB_CONF_PREFIX) - 1;
580  const char *name;
581  int tmp;
582 
583  if (strncasecmp(AST_JB_CONF_PREFIX, varname, prefixlen)) {
584  return -1;
585  }
586 
587  name = varname + prefixlen;
588 
589  if (!strcasecmp(name, AST_JB_CONF_ENABLE)) {
590  ast_set2_flag(conf, ast_true(value), AST_JB_ENABLED);
591  } else if (!strcasecmp(name, AST_JB_CONF_FORCE)) {
592  ast_set2_flag(conf, ast_true(value), AST_JB_FORCED);
593  } else if (!strcasecmp(name, AST_JB_CONF_MAX_SIZE)) {
594  if ((tmp = atoi(value)) > 0)
595  conf->max_size = tmp;
596  } else if (!strcasecmp(name, AST_JB_CONF_RESYNCH_THRESHOLD)) {
597  if ((tmp = atoi(value)) > 0)
598  conf->resync_threshold = tmp;
599  } else if (!strcasecmp(name, AST_JB_CONF_IMPL)) {
600  if (!ast_strlen_zero(value))
601  snprintf(conf->impl, sizeof(conf->impl), "%s", value);
602  } else if (!strcasecmp(name, AST_JB_CONF_TARGET_EXTRA)) {
603  if (sscanf(value, "%30d", &tmp) == 1) {
604  conf->target_extra = tmp;
605  }
606  } else if (!strcasecmp(name, AST_JB_CONF_LOG)) {
607  ast_set2_flag(conf, ast_true(value), AST_JB_LOG);
608  } else {
609  return -1;
610  }
611 
612  return 0;
613 }
614 
615 
616 void ast_jb_configure(struct ast_channel *chan, const struct ast_jb_conf *conf)
617 {
618  memcpy(&chan->jb.conf, conf, sizeof(*conf));
619 }
620 
621 
622 void ast_jb_get_config(const struct ast_channel *chan, struct ast_jb_conf *conf)
623 {
624  memcpy(conf, &chan->jb.conf, sizeof(*conf));
625 }
626 
627 void ast_jb_empty_and_reset(struct ast_channel *c0, struct ast_channel *c1)
628 {
629  struct ast_jb *jb0 = &c0->jb;
630  struct ast_jb *jb1 = &c1->jb;
631  int c0_use_jb = ast_test_flag(jb0, JB_USE);
632  int c0_jb_is_created = ast_test_flag(jb0, JB_CREATED);
633  int c1_use_jb = ast_test_flag(jb1, JB_USE);
634  int c1_jb_is_created = ast_test_flag(jb1, JB_CREATED);
635 
636  if (c0_use_jb && c0_jb_is_created && jb0->impl->empty_and_reset) {
637  jb0->impl->empty_and_reset(jb0->jbobj);
638  }
639 
640  if (c1_use_jb && c1_jb_is_created && jb1->impl->empty_and_reset) {
641  jb1->impl->empty_and_reset(jb1->jbobj);
642  }
643 }
644 
645 /* Implementation functions */
646 
647 /* fixed */
648 static void * jb_create_fixed(struct ast_jb_conf *general_config, long resynch_threshold)
649 {
650  struct fixed_jb_conf conf;
651 
652  conf.jbsize = general_config->max_size;
653  conf.resync_threshold = resynch_threshold;
654 
655  return fixed_jb_new(&conf);
656 }
657 
658 static void jb_destroy_fixed(void *jb)
659 {
660  struct fixed_jb *fixedjb = (struct fixed_jb *) jb;
661 
662  /* destroy the jb */
663  fixed_jb_destroy(fixedjb);
664 }
665 
666 
667 static int jb_put_first_fixed(void *jb, struct ast_frame *fin, long now)
668 {
669  struct fixed_jb *fixedjb = (struct fixed_jb *) jb;
670  int res;
671 
672  res = fixed_jb_put_first(fixedjb, fin, fin->len, fin->ts, now);
673 
674  return fixed_to_abstract_code[res];
675 }
676 
677 
678 static int jb_put_fixed(void *jb, struct ast_frame *fin, long now)
679 {
680  struct fixed_jb *fixedjb = (struct fixed_jb *) jb;
681  int res;
682 
683  res = fixed_jb_put(fixedjb, fin, fin->len, fin->ts, now);
684 
685  return fixed_to_abstract_code[res];
686 }
687 
688 
689 static int jb_get_fixed(void *jb, struct ast_frame **fout, long now, long interpl)
690 {
691  struct fixed_jb *fixedjb = (struct fixed_jb *) jb;
692  struct fixed_jb_frame frame;
693  int res;
694 
695  res = fixed_jb_get(fixedjb, &frame, now, interpl);
696  *fout = frame.data;
697 
698  return fixed_to_abstract_code[res];
699 }
700 
701 
702 static long jb_next_fixed(void *jb)
703 {
704  struct fixed_jb *fixedjb = (struct fixed_jb *) jb;
705 
706  return fixed_jb_next(fixedjb);
707 }
708 
709 
710 static int jb_remove_fixed(void *jb, struct ast_frame **fout)
711 {
712  struct fixed_jb *fixedjb = (struct fixed_jb *) jb;
713  struct fixed_jb_frame frame;
714  int res;
715 
716  res = fixed_jb_remove(fixedjb, &frame);
717  *fout = frame.data;
718 
719  return fixed_to_abstract_code[res];
720 }
721 
722 
723 static void jb_force_resynch_fixed(void *jb)
724 {
725  struct fixed_jb *fixedjb = (struct fixed_jb *) jb;
726 
728 }
729 
730 static void jb_empty_and_reset_fixed(void *jb)
731 {
732  struct fixed_jb *fixedjb = jb;
733  struct fixed_jb_frame f;
734 
735  while (fixed_jb_remove(fixedjb, &f) == FIXED_JB_OK) {
736  ast_frfree(f.data);
737  }
738 }
739 
740 /* adaptive */
741 
742 static void *jb_create_adaptive(struct ast_jb_conf *general_config, long resynch_threshold)
743 {
744  jb_conf jbconf;
745  jitterbuf *adaptivejb;
746 
747  adaptivejb = jb_new();
748  if (adaptivejb) {
749  jbconf.max_jitterbuf = general_config->max_size;
750  jbconf.resync_threshold = general_config->resync_threshold;
751  jbconf.max_contig_interp = 10;
752  jbconf.target_extra = general_config->target_extra;
753  jb_setconf(adaptivejb, &jbconf);
754  }
755 
756  return adaptivejb;
757 }
758 
759 
760 static void jb_destroy_adaptive(void *jb)
761 {
762  jitterbuf *adaptivejb = (jitterbuf *) jb;
763 
764  jb_destroy(adaptivejb);
765 }
766 
767 
768 static int jb_put_first_adaptive(void *jb, struct ast_frame *fin, long now)
769 {
770  jitterbuf *adaptivejb = (jitterbuf *) jb;
771 
772  /* Initialize the offset to that of the first frame's timestamp */
773  adaptivejb->info.resync_offset = fin->ts;
774 
775  return jb_put_adaptive(jb, fin, now);
776 }
777 
778 
779 static int jb_put_adaptive(void *jb, struct ast_frame *fin, long now)
780 {
781  jitterbuf *adaptivejb = (jitterbuf *) jb;
782  int res;
783 
784  res = jb_put(adaptivejb, fin, JB_TYPE_VOICE, fin->len, fin->ts, now);
785 
786  return adaptive_to_abstract_code[res];
787 }
788 
789 
790 static int jb_get_adaptive(void *jb, struct ast_frame **fout, long now, long interpl)
791 {
792  jitterbuf *adaptivejb = (jitterbuf *) jb;
793  jb_frame frame;
794  int res;
795 
796  res = jb_get(adaptivejb, &frame, now, interpl);
797  *fout = frame.data;
798 
799  return adaptive_to_abstract_code[res];
800 }
801 
802 
803 static long jb_next_adaptive(void *jb)
804 {
805  jitterbuf *adaptivejb = (jitterbuf *) jb;
806 
807  return jb_next(adaptivejb);
808 }
809 
810 
811 static int jb_remove_adaptive(void *jb, struct ast_frame **fout)
812 {
813  jitterbuf *adaptivejb = (jitterbuf *) jb;
814  jb_frame frame;
815  int res;
816 
817  res = jb_getall(adaptivejb, &frame);
818  *fout = frame.data;
819 
820  return adaptive_to_abstract_code[res];
821 }
822 
823 
824 static void jb_force_resynch_adaptive(void *jb)
825 {
826 }
827 
828 static void jb_empty_and_reset_adaptive(void *jb)
829 {
830  jitterbuf *adaptivejb = jb;
831  jb_frame f;
832 
833  while (jb_getall(adaptivejb, &f) == JB_OK) {
834  ast_frfree(f.data);
835  }
836 
837  jb_reset(adaptivejb);
838 }
long max_jitterbuf
Definition: jitterbuf.h:67
union ast_frame_subclass subclass
Definition: frame.h:146
#define AST_JB_CONF_IMPL
Definition: abstract_jb.h:77
Main Channel structure associated with a channel.
Definition: channel.h:742
Asterisk main include file. File version handling, generic pbx functions.
static void * jb_create_adaptive(struct ast_jb_conf *general_config, long resynch_threshold)
Definition: abstract_jb.c:742
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
int offset
Definition: frame.h:156
private fixed_jb structure
jb_next_impl next
Definition: abstract_jb.c:88
int ast_jb_put(struct ast_channel *chan, struct ast_frame *f)
Puts a frame into a channel jitterbuffer.
Definition: abstract_jb.c:306
jb_create_impl create
Definition: abstract_jb.c:83
#define ast_set2_flag(p, value, flag)
Definition: utils.h:94
#define ast_test_flag(p, flag)
Definition: utils.h:63
static int ast_codec_interp_len(format_t format)
Gets duration in ms of interpolation frame for a format.
Definition: frame.h:782
void ast_jb_empty_and_reset(struct ast_channel *c0, struct ast_channel *c1)
drops all frames from a jitterbuffer and resets it
Definition: abstract_jb.c:627
#define AST_JB_CONF_ENABLE
Definition: abstract_jb.h:72
int fixed_jb_remove(struct fixed_jb *jb, struct fixed_jb_frame *frameout)
static void jb_force_resynch_fixed(void *jb)
Definition: abstract_jb.c:723
int(* jb_get_impl)(void *jb, struct ast_frame **fout, long now, long interpl)
Get frame for now.
Definition: abstract_jb.c:67
#define ast_set_flag(p, flag)
Definition: utils.h:70
#define LOG_WARNING
Definition: logger.h:144
void(* jb_force_resynch_impl)(void *jb)
Force resynch.
Definition: abstract_jb.c:73
int ast_jb_read_conf(struct ast_jb_conf *conf, const char *varname, const char *value)
Sets jitterbuffer configuration property.
Definition: abstract_jb.c:577
#define AST_FRAME_DTMF
Definition: frame.h:128
int ast_jb_get_when_to_wakeup(struct ast_channel *c0, struct ast_channel *c1, int time_left)
Calculates the time, left to the closest delivery moment in a bridge.
Definition: abstract_jb.c:266
enum jb_return_code jb_put(jitterbuf *jb, void *data, const enum jb_frame_type type, long ms, long ts, long now)
queue a frame
Definition: jitterbuf.c:527
#define AST_JB_CONF_LOG
Definition: abstract_jb.h:78
jb_destroy_impl destroy
Definition: abstract_jb.c:84
static int create_jb(struct ast_channel *chan, struct ast_frame *first_frame)
Definition: abstract_jb.c:449
static void jb_destroy_fixed(void *jb)
Definition: abstract_jb.c:658
long ts
Definition: frame.h:168
jb_get_impl get
Definition: abstract_jb.c:87
jb_remove_impl remove
Definition: abstract_jb.c:89
static const char *const jb_get_actions[]
Definition: abstract_jb.c:162
void(* jb_destroy_impl)(void *jb)
Destroy.
Definition: abstract_jb.c:61
#define ast_assert(a)
Definition: utils.h:738
static int jb_remove_adaptive(void *jb, struct ast_frame **fout)
Definition: abstract_jb.c:811
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:90
static long get_now(struct ast_jb *jb, struct timeval *tv)
Definition: abstract_jb.c:564
struct ast_jb_conf conf
Jitterbuffer configuration.
Definition: abstract_jb.h:90
void *(* jb_create_impl)(struct ast_jb_conf *general_config, long resynch_threshold)
Create.
Definition: abstract_jb.c:59
format_t codec
Definition: frame.h:137
#define AST_JB_IMPL_NAME_SIZE
Definition: abstract_jb.h:50
enum jb_return_code jb_get(jitterbuf *jb, jb_frame *frame, long now, long interpl)
get a frame for time now (receiver&#39;s time) return value is one of JB_OK: You&#39;ve got frame! JB_DROP: H...
Definition: jitterbuf.c:787
static long jb_next_fixed(void *jb)
Definition: abstract_jb.c:702
Common implementation-independent jitterbuffer stuff.
int value
Definition: syslog.c:39
static struct ast_jb_impl avail_impl[]
Definition: abstract_jb.c:117
#define ast_verb(level,...)
Definition: logger.h:243
static void jb_choose_impl(struct ast_channel *chan)
Definition: abstract_jb.c:183
Utility functions.
static int jb_put_fixed(void *jb, struct ast_frame *fin, long now)
Definition: abstract_jb.c:678
jitterbuf * jb_new(void)
new jitterbuf
Definition: jitterbuf.c:88
long resync_threshold
Resynchronization threshold of the jitterbuffer implementation.
Definition: abstract_jb.h:62
static void jb_force_resynch_adaptive(void *jb)
Definition: abstract_jb.c:824
#define jb_framelog(...)
Macros for the frame log files.
Definition: abstract_jb.c:165
long jb_next(jitterbuf *jb)
when is the next frame due out, in receiver&#39;s time (0=EMPTY) This value may change as frames are adde...
Definition: jitterbuf.c:769
long resync_threshold
Definition: jitterbuf.h:68
jb_put_impl put
Definition: abstract_jb.c:86
General Asterisk PBX channel definitions.
#define AST_FRIENDLY_OFFSET
Offset into a frame&#39;s data buffer.
Definition: frame.h:204
const char * src
Definition: frame.h:158
static void jb_destroy_adaptive(void *jb)
Definition: abstract_jb.c:760
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
#define AST_JB_CONF_PREFIX
Definition: abstract_jb.h:71
jb_force_resynch_impl force_resync
Definition: abstract_jb.c:90
void ast_jb_destroy(struct ast_channel *chan)
Destroys jitterbuffer on a channel.
Definition: abstract_jb.c:536
struct timeval ast_samp2tv(unsigned int _nsamp, unsigned int _rate)
Returns a timeval corresponding to the duration of n samples at rate r. Useful to convert samples to ...
Definition: time.h:191
static int jb_get_adaptive(void *jb, struct ast_frame **fout, long now, long interpl)
Definition: abstract_jb.c:790
void * jbobj
Jitterbuffer object, passed to the implementation.
Definition: abstract_jb.h:94
long target_extra
amount of additional jitterbuffer adjustment
Definition: abstract_jb.h:66
int fixed_jb_get(struct fixed_jb *jb, struct fixed_jb_frame *frame, long now, long interpl)
jb_info info
Definition: jitterbuf.h:108
#define AST_JB_CONF_TARGET_EXTRA
Definition: abstract_jb.h:76
char impl[AST_JB_IMPL_NAME_SIZE]
Name of the jitterbuffer implementation to be used.
Definition: abstract_jb.h:64
int(* jb_put_first_impl)(void *jb, struct ast_frame *fin, long now)
Put first frame.
Definition: abstract_jb.c:63
long target_extra
Definition: jitterbuf.h:70
jitterbuf: an application-independent jitterbuffer jitterbuf.c
static void jb_empty_and_reset_fixed(void *jb)
Definition: abstract_jb.c:730
static const int fixed_to_abstract_code[]
Definition: abstract_jb.c:156
void fixed_jb_destroy(struct fixed_jb *jb)
static int jb_put_first_fixed(void *jb, struct ast_frame *fin, long now)
Definition: abstract_jb.c:667
static int default_impl
Definition: abstract_jb.c:144
#define LOG_ERROR
Definition: logger.h:155
void ast_jb_get_config(const struct ast_channel *chan, struct ast_jb_conf *conf)
Copies a channel&#39;s jitterbuffer configuration.
Definition: abstract_jb.c:622
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is &quot;true&quot;. This function checks to see whether a string passed to it is an indication of an &quot;true&quot; value. It checks to see if the string is &quot;yes&quot;, &quot;true&quot;, &quot;y&quot;, &quot;t&quot;, &quot;on&quot; or &quot;1&quot;.
Definition: utils.c:1533
void * data
Definition: jitterbuf.h:100
enum jb_return_code jb_setconf(jitterbuf *jb, jb_conf *conf)
set jitterbuf conf
Definition: jitterbuf.c:827
int fixed_jb_put_first(struct fixed_jb *jb, void *data, long ms, long ts, long now)
struct ast_channel * ast_bridged_channel(struct ast_channel *chan)
Find bridged channel.
Definition: channel.c:7160
static const int adaptive_to_abstract_code[]
Definition: abstract_jb.c:158
struct timeval ast_tvadd(struct timeval a, struct timeval b)
Returns the sum of two timevals a + b.
Definition: utils.c:1587
const ast_string_field name
Definition: channel.h:787
static long jb_next_adaptive(void *jb)
Definition: abstract_jb.c:803
#define AST_JB_CONF_FORCE
Definition: abstract_jb.h:73
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
jb_put_first_impl put_first
Definition: abstract_jb.c:85
long(* jb_next_impl)(void *jb)
Get next.
Definition: abstract_jb.c:69
struct ast_jb_impl * impl
Jitterbuffer implementation to be used.
Definition: abstract_jb.h:92
General jitterbuffer state.
Definition: abstract_jb.h:87
static int jb_remove_fixed(void *jb, struct ast_frame **fout)
Definition: abstract_jb.c:710
long fixed_jb_next(struct fixed_jb *jb)
int errno
static const char name[]
#define AST_CHANNEL_NAME
Definition: channel.h:137
struct ast_jb jb
Definition: channel.h:821
FILE * logfile
File for frame timestamp tracing.
Definition: abstract_jb.h:102
static struct ast_format f[]
Definition: format_g726.c:181
int ast_write(struct ast_channel *chan, struct ast_frame *frame)
Write a frame to a channel This function writes the given frame to the indicated channel.
Definition: channel.c:4916
format_t last_format
Voice format of the last frame in.
Definition: abstract_jb.h:100
int(* jb_put_impl)(void *jb, struct ast_frame *fin, long now)
Put frame.
Definition: abstract_jb.c:65
Jitterbuffering algorithm.
#define ast_clear_flag(p, flag)
Definition: utils.h:77
struct timeval delivery
Definition: frame.h:162
void ast_jb_configure(struct ast_channel *chan, const struct ast_jb_conf *conf)
Configures a jitterbuffer on a channel.
Definition: abstract_jb.c:616
char name[AST_JB_IMPL_NAME_SIZE]
Definition: abstract_jb.c:82
void fixed_jb_set_force_resynch(struct fixed_jb *jb)
jb_empty_and_reset_impl empty_and_reset
Definition: abstract_jb.c:91
Channels have this property if they can create jitter; i.e. most VoIP channels.
Definition: channel.h:888
struct fixed_jb * fixed_jb_new(struct fixed_jb_conf *conf)
int fixed_jb_put(struct fixed_jb *jb, void *data, long ms, long ts, long now)
void ast_jb_get_and_deliver(struct ast_channel *c0, struct ast_channel *c1)
Deliver the queued frames that should be delivered now for both channels.
Definition: abstract_jb.c:371
struct ast_frame * next
Definition: frame.h:164
static void * jb_create_fixed(struct ast_jb_conf *general_config, long resynch_threshold)
Definition: abstract_jb.c:648
long resync_offset
Definition: jitterbuf.h:95
Data structure associated with a single frame of data.
Definition: frame.h:142
static int jb_put_adaptive(void *jb, struct ast_frame *fin, long now)
Definition: abstract_jb.c:779
Handy terminal functions for vt* terms.
struct timeval tv
Channels have this property if they can accept input with jitter; i.e. most VoIP channels.
Definition: channel.h:883
enum ast_frame_type frametype
Definition: frame.h:144
#define ast_frfree(fr)
Definition: frame.h:583
void jb_destroy(jitterbuf *jb)
destroy jitterbuf
Definition: jitterbuf.c:101
int(* jb_remove_impl)(void *jb, struct ast_frame **fout)
Remove first frame.
Definition: abstract_jb.c:71
#define AST_JB_CONF_MAX_SIZE
Definition: abstract_jb.h:74
struct timeval timebase
The time the jitterbuffer was created.
Definition: abstract_jb.h:96
enum jb_return_code jb_getall(jitterbuf *jb, jb_frame *frameout)
unconditionally get frames from jitterbuf until empty
Definition: jitterbuf.c:803
int ast_jb_do_usecheck(struct ast_channel *c0, struct ast_channel *c1)
Checks the need of a jb use in a generic bridge.
Definition: abstract_jb.c:205
#define AST_JB_CONF_RESYNCH_THRESHOLD
Definition: abstract_jb.h:75
struct ast_channel_tech * tech
Definition: channel.h:743
long max_size
Max size of the jitterbuffer implementation.
Definition: abstract_jb.h:60
void(* jb_empty_and_reset_impl)(void *jb)
Empty and reset jb.
Definition: abstract_jb.c:75
static void jb_get_and_deliver(struct ast_channel *chan)
Definition: abstract_jb.c:388
Jitterbuffer implementation private struct.
Definition: abstract_jb.c:80
long len
Definition: frame.h:170
General jitterbuffer configuration.
Definition: abstract_jb.h:55
static void jb_empty_and_reset_adaptive(void *jb)
Definition: abstract_jb.c:828
long max_contig_interp
Definition: jitterbuf.h:69
struct ast_frame * ast_frdup(const struct ast_frame *fr)
Copies a frame.
Definition: frame.c:474
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180
int samples
Definition: frame.h:150
static int jb_put_first_adaptive(void *jb, struct ast_frame *fin, long now)
Definition: abstract_jb.c:768
long next
The time the next frame should be played.
Definition: abstract_jb.h:98
void jb_reset(jitterbuf *jb)
reset jitterbuf
Definition: jitterbuf.c:74
static int jb_get_fixed(void *jb, struct ast_frame **fout, long now, long interpl)
Definition: abstract_jb.c:689