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