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