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