Const-ify the world (or at least a good part of it)
[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;
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                         jb->last_format = f->subclass;
413                         ast_frfree(f);
414                         break;
415                 case JB_IMPL_INTERP:
416                         /* interpolate a frame */
417                         f = &finterp;
418                         f->frametype = AST_FRAME_VOICE;
419                         f->subclass = jb->last_format;
420                         f->datalen  = 0;
421                         f->samples  = interpolation_len * 8;
422                         f->mallocd  = 0;
423                         f->src  = "JB interpolation";
424                         f->data.ptr  = NULL;
425                         f->delivery = ast_tvadd(jb->timebase, ast_samp2tv(jb->next, 1000));
426                         f->offset = AST_FRIENDLY_OFFSET;
427                         /* deliver the interpolated frame */
428                         ast_write(chan, f);
429                         jb_framelog("\tJB_GET {now=%ld}: Interpolated frame with len=%d\n", now, interpolation_len);
430                         break;
431                 case JB_IMPL_NOFRAME:
432                         ast_log(LOG_WARNING,
433                                 "JB_IMPL_NOFRAME is returned from the %s jb when now=%ld >= next=%ld, jbnext=%ld!\n",
434                                 jbimpl->name, now, jb->next, jbimpl->next(jbobj));
435                         jb_framelog("\tJB_GET {now=%ld}: No frame for now!?\n", now);
436                         return;
437                 default:
438                         ast_log(LOG_ERROR, "This should never happen!\n");
439                         ast_assert("JB type unknown" == NULL);
440                         break;
441                 }
442
443                 jb->next = jbimpl->next(jbobj);
444         }
445 }
446
447
448 static int create_jb(struct ast_channel *chan, struct ast_frame *frr)
449 {
450         struct ast_jb *jb = &chan->jb;
451         struct ast_jb_conf *jbconf = &jb->conf;
452         const struct ast_jb_impl *jbimpl = jb->impl;
453         void *jbobj;
454         struct ast_channel *bridged;
455         long now;
456         char logfile_pathname[20 + AST_JB_IMPL_NAME_SIZE + 2*AST_CHANNEL_NAME + 1];
457         char name1[AST_CHANNEL_NAME], name2[AST_CHANNEL_NAME], *tmp;
458         int res;
459
460         jbobj = jb->jbobj = jbimpl->create(jbconf, jbconf->resync_threshold);
461         if (!jbobj) {
462                 ast_log(LOG_WARNING, "Failed to create jitterbuffer on channel '%s'\n", chan->name);
463                 return -1;
464         }
465
466         now = get_now(jb, NULL);
467         res = jbimpl->put_first(jbobj, frr, now);
468
469         /* The result of putting the first frame should not differ from OK. However, its possible
470            some implementations (i.e. adaptive's when resynch_threshold is specified) to drop it. */
471         if (res != JB_IMPL_OK) {
472                 ast_log(LOG_WARNING, "Failed to put first frame in the jitterbuffer on channel '%s'\n", chan->name);
473                 /*
474                 jbimpl->destroy(jbobj);
475                 return -1;
476                 */
477         }
478
479         /* Init next */
480         jb->next = jbimpl->next(jbobj);
481
482         /* Init last format for a first time. */
483         jb->last_format = frr->subclass;
484
485         /* Create a frame log file */
486         if (ast_test_flag(jbconf, AST_JB_LOG)) {
487                 char safe_logfile[30] = "/tmp/logfile-XXXXXX";
488                 int safe_fd;
489                 snprintf(name2, sizeof(name2), "%s", chan->name);
490                 if ((tmp = strchr(name2, '/'))) {
491                         *tmp = '#';
492                 }
493
494                 bridged = ast_bridged_channel(chan);
495                 /* We should always have bridged chan if a jitterbuffer is in use */
496                 ast_assert(bridged != NULL);
497
498                 snprintf(name1, sizeof(name1), "%s", bridged->name);
499                 if ((tmp = strchr(name1, '/'))) {
500                         *tmp = '#';
501                 }
502
503                 snprintf(logfile_pathname, sizeof(logfile_pathname),
504                         "/tmp/ast_%s_jb_%s--%s.log", jbimpl->name, name1, name2);
505                 if (!(safe_fd = mkstemp(safe_logfile)) > -1 || unlink(logfile_pathname) || link(safe_logfile, logfile_pathname) || unlink(safe_logfile) || !(jb->logfile = fdopen(safe_fd, "w+b"))) {
506                         jb->logfile = NULL;
507                         if (safe_fd > -1) {
508                                 close(safe_fd);
509                         }
510                         ast_log(LOG_ERROR, "Failed to create frame log file with pathname '%s': %s\n", logfile_pathname, strerror(errno));
511                 }
512
513                 if (res == JB_IMPL_OK) {
514                         jb_framelog("JB_PUT_FIRST {now=%ld}: Queued frame with ts=%ld and len=%ld\n",
515                                 now, frr->ts, frr->len);
516                 } else {
517                         jb_framelog("JB_PUT_FIRST {now=%ld}: Dropped frame with ts=%ld and len=%ld\n",
518                                 now, frr->ts, frr->len);
519                 }
520         }
521
522         ast_verb(3, "%s jitterbuffer created on channel %s\n", jbimpl->name, chan->name);
523
524         /* Free the frame if it has not been queued in the jb */
525         if (res != JB_IMPL_OK) {
526                 ast_frfree(frr);
527         }
528
529         return 0;
530 }
531
532
533 void ast_jb_destroy(struct ast_channel *chan)
534 {
535         struct ast_jb *jb = &chan->jb;
536         const struct ast_jb_impl *jbimpl = jb->impl;
537         void *jbobj = jb->jbobj;
538         struct ast_frame *f;
539
540         if (jb->logfile) {
541                 fclose(jb->logfile);
542                 jb->logfile = NULL;
543         }
544
545         if (ast_test_flag(jb, JB_CREATED)) {
546                 /* Remove and free all frames still queued in jb */
547                 while (jbimpl->remove(jbobj, &f) == JB_IMPL_OK) {
548                         ast_frfree(f);
549                 }
550
551                 jbimpl->destroy(jbobj);
552                 jb->jbobj = NULL;
553
554                 ast_clear_flag(jb, JB_CREATED);
555
556                 ast_verb(3, "%s jitterbuffer destroyed on channel %s\n", jbimpl->name, chan->name);
557         }
558 }
559
560
561 static long get_now(struct ast_jb *jb, struct timeval *when)
562 {
563         struct timeval now;
564
565         if (!when) {
566                 when = &now;
567                 gettimeofday(when, NULL);
568         }
569
570         return ast_tvdiff_ms(*when, jb->timebase);
571 }
572
573
574 int ast_jb_read_conf(struct ast_jb_conf *conf, const char *varname, const char *value)
575 {
576         int prefixlen = sizeof(AST_JB_CONF_PREFIX) - 1;
577         const char *name;
578         int tmp;
579
580         if (strncasecmp(AST_JB_CONF_PREFIX, varname, prefixlen)) {
581                 return -1;
582         }
583
584         name = varname + prefixlen;
585
586         if (!strcasecmp(name, AST_JB_CONF_ENABLE)) {
587                 ast_set2_flag(conf, ast_true(value), AST_JB_ENABLED);
588         } else if (!strcasecmp(name, AST_JB_CONF_FORCE)) {
589                 ast_set2_flag(conf, ast_true(value), AST_JB_FORCED);
590         } else if (!strcasecmp(name, AST_JB_CONF_MAX_SIZE)) {
591                 if ((tmp = atoi(value)) > 0)
592                         conf->max_size = tmp;
593         } else if (!strcasecmp(name, AST_JB_CONF_RESYNCH_THRESHOLD)) {
594                 if ((tmp = atoi(value)) > 0)
595                         conf->resync_threshold = tmp;
596         } else if (!strcasecmp(name, AST_JB_CONF_IMPL)) {
597                 if (!ast_strlen_zero(value))
598                         snprintf(conf->impl, sizeof(conf->impl), "%s", value);
599         } else if (!strcasecmp(name, AST_JB_CONF_LOG)) {
600                 ast_set2_flag(conf, ast_true(value), AST_JB_LOG);
601         } else {
602                 return -1;
603         }
604
605         return 0;
606 }
607
608
609 void ast_jb_configure(struct ast_channel *chan, const struct ast_jb_conf *conf)
610 {
611         memcpy(&chan->jb.conf, conf, sizeof(*conf));
612 }
613
614
615 void ast_jb_get_config(const struct ast_channel *chan, struct ast_jb_conf *conf)
616 {
617         memcpy(conf, &chan->jb.conf, sizeof(*conf));
618 }
619
620 void ast_jb_empty_and_reset(struct ast_channel *c0, struct ast_channel *c1)
621 {
622         struct ast_jb *jb0 = &c0->jb;
623         struct ast_jb *jb1 = &c1->jb;
624         int c0_use_jb = ast_test_flag(jb0, JB_USE);
625         int c0_jb_is_created = ast_test_flag(jb0, JB_CREATED);
626         int c1_use_jb = ast_test_flag(jb1, JB_USE);
627         int c1_jb_is_created = ast_test_flag(jb1, JB_CREATED);
628
629         if (c0_use_jb && c0_jb_is_created && jb0->impl->empty_and_reset) {
630                 jb0->impl->empty_and_reset(jb0->jbobj);
631         }
632
633         if (c1_use_jb && c1_jb_is_created && jb1->impl->empty_and_reset) {
634                 jb1->impl->empty_and_reset(jb1->jbobj);
635         }
636 }
637
638 /* Implementation functions */
639
640 /* fixed */
641 static void * jb_create_fixed(struct ast_jb_conf *general_config, long resynch_threshold)
642 {
643         struct fixed_jb_conf conf;
644
645         conf.jbsize = general_config->max_size;
646         conf.resync_threshold = resynch_threshold;
647
648         return fixed_jb_new(&conf);
649 }
650
651 static void jb_destroy_fixed(void *jb)
652 {
653         struct fixed_jb *fixedjb = (struct fixed_jb *) jb;
654
655         /* destroy the jb */
656         fixed_jb_destroy(fixedjb);
657 }
658
659
660 static int jb_put_first_fixed(void *jb, struct ast_frame *fin, long now)
661 {
662         struct fixed_jb *fixedjb = (struct fixed_jb *) jb;
663         int res;
664
665         res = fixed_jb_put_first(fixedjb, fin, fin->len, fin->ts, now);
666
667         return fixed_to_abstract_code[res];
668 }
669
670
671 static int jb_put_fixed(void *jb, struct ast_frame *fin, long now)
672 {
673         struct fixed_jb *fixedjb = (struct fixed_jb *) jb;
674         int res;
675
676         res = fixed_jb_put(fixedjb, fin, fin->len, fin->ts, now);
677
678         return fixed_to_abstract_code[res];
679 }
680
681
682 static int jb_get_fixed(void *jb, struct ast_frame **fout, long now, long interpl)
683 {
684         struct fixed_jb *fixedjb = (struct fixed_jb *) jb;
685         struct fixed_jb_frame frame;
686         int res;
687
688         res = fixed_jb_get(fixedjb, &frame, now, interpl);
689         *fout = frame.data;
690
691         return fixed_to_abstract_code[res];
692 }
693
694
695 static long jb_next_fixed(void *jb)
696 {
697         struct fixed_jb *fixedjb = (struct fixed_jb *) jb;
698
699         return fixed_jb_next(fixedjb);
700 }
701
702
703 static int jb_remove_fixed(void *jb, struct ast_frame **fout)
704 {
705         struct fixed_jb *fixedjb = (struct fixed_jb *) jb;
706         struct fixed_jb_frame frame;
707         int res;
708
709         res = fixed_jb_remove(fixedjb, &frame);
710         *fout = frame.data;
711
712         return fixed_to_abstract_code[res];
713 }
714
715
716 static void jb_force_resynch_fixed(void *jb)
717 {
718         struct fixed_jb *fixedjb = (struct fixed_jb *) jb;
719
720         fixed_jb_set_force_resynch(fixedjb);
721 }
722
723 static void jb_empty_and_reset_fixed(void *jb)
724 {
725         struct fixed_jb *fixedjb = jb;
726         struct fixed_jb_frame f;
727
728         while (fixed_jb_remove(fixedjb, &f) == FIXED_JB_OK) {
729                 ast_frfree(f.data);
730         }
731 }
732
733 /* adaptive */
734
735 static void *jb_create_adaptive(struct ast_jb_conf *general_config, long resynch_threshold)
736 {
737         jb_conf jbconf;
738         jitterbuf *adaptivejb;
739
740         adaptivejb = jb_new();
741         if (adaptivejb) {
742                 jbconf.max_jitterbuf = general_config->max_size;
743                 jbconf.resync_threshold = general_config->resync_threshold;
744                 jbconf.max_contig_interp = 10;
745                 jb_setconf(adaptivejb, &jbconf);
746         }
747
748         return adaptivejb;
749 }
750
751
752 static void jb_destroy_adaptive(void *jb)
753 {
754         jitterbuf *adaptivejb = (jitterbuf *) jb;
755
756         jb_destroy(adaptivejb);
757 }
758
759
760 static int jb_put_first_adaptive(void *jb, struct ast_frame *fin, long now)
761 {
762         return jb_put_adaptive(jb, fin, now);
763 }
764
765
766 static int jb_put_adaptive(void *jb, struct ast_frame *fin, long now)
767 {
768         jitterbuf *adaptivejb = (jitterbuf *) jb;
769         int res;
770
771         res = jb_put(adaptivejb, fin, JB_TYPE_VOICE, fin->len, fin->ts, now);
772
773         return adaptive_to_abstract_code[res];
774 }
775
776
777 static int jb_get_adaptive(void *jb, struct ast_frame **fout, long now, long interpl)
778 {
779         jitterbuf *adaptivejb = (jitterbuf *) jb;
780         jb_frame frame;
781         int res;
782
783         res = jb_get(adaptivejb, &frame, now, interpl);
784         *fout = frame.data;
785
786         return adaptive_to_abstract_code[res];
787 }
788
789
790 static long jb_next_adaptive(void *jb)
791 {
792         jitterbuf *adaptivejb = (jitterbuf *) jb;
793
794         return jb_next(adaptivejb);
795 }
796
797
798 static int jb_remove_adaptive(void *jb, struct ast_frame **fout)
799 {
800         jitterbuf *adaptivejb = (jitterbuf *) jb;
801         jb_frame frame;
802         int res;
803
804         res = jb_getall(adaptivejb, &frame);
805         *fout = frame.data;
806
807         return adaptive_to_abstract_code[res];
808 }
809
810
811 static void jb_force_resynch_adaptive(void *jb)
812 {
813 }
814
815 static void jb_empty_and_reset_adaptive(void *jb)
816 {
817         jitterbuf *adaptivejb = jb;
818         jb_frame f;
819
820         while (jb_getall(adaptivejb, &f) == JB_OK) {
821                 ast_frfree(f.data);
822         }
823
824         jb_reset(adaptivejb);
825 }