Another big chunk of changes from the RSW branch. Bunch of stuff from main/
[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 struct ast_jb_impl avail_impl[] = 
114 {
115         {
116                 .name = "fixed",
117                 .create = jb_create_fixed,
118                 .destroy = jb_destroy_fixed,
119                 .put_first = jb_put_first_fixed,
120                 .put = jb_put_fixed,
121                 .get = jb_get_fixed,
122                 .next = jb_next_fixed,
123                 .remove = jb_remove_fixed,
124                 .force_resync = jb_force_resynch_fixed,
125                 .empty_and_reset = jb_empty_and_reset_fixed,
126         },
127         {
128                 .name = "adaptive",
129                 .create = jb_create_adaptive,
130                 .destroy = jb_destroy_adaptive,
131                 .put_first = jb_put_first_adaptive,
132                 .put = jb_put_adaptive,
133                 .get = jb_get_adaptive,
134                 .next = jb_next_adaptive,
135                 .remove = jb_remove_adaptive,
136                 .force_resync = jb_force_resynch_adaptive,
137                 .empty_and_reset = jb_empty_and_reset_adaptive,
138         }
139 };
140
141 static int default_impl = 0;
142
143
144 /*! Abstract return codes */
145 enum {
146         JB_IMPL_OK,
147         JB_IMPL_DROP,
148         JB_IMPL_INTERP,
149         JB_IMPL_NOFRAME
150 };
151
152 /* Translations between impl and abstract return codes */
153 static int fixed_to_abstract_code[] =
154         {JB_IMPL_OK, JB_IMPL_DROP, JB_IMPL_INTERP, JB_IMPL_NOFRAME};
155 static int adaptive_to_abstract_code[] =
156         {JB_IMPL_OK, JB_IMPL_NOFRAME, JB_IMPL_NOFRAME, JB_IMPL_INTERP, JB_IMPL_DROP, JB_IMPL_OK};
157
158 /* JB_GET actions (used only for the frames log) */
159 static char *jb_get_actions[] = {"Delivered", "Dropped", "Interpolated", "No"};
160
161 /*! \brief Macros for the frame log files */
162 #define jb_framelog(...) do { \
163         if (jb->logfile) { \
164                 fprintf(jb->logfile, __VA_ARGS__); \
165                 fflush(jb->logfile); \
166         } \
167 } while (0)
168
169
170 /* Internal utility functions */
171 static void jb_choose_impl(struct ast_channel *chan);
172 static void jb_get_and_deliver(struct ast_channel *chan);
173 static int create_jb(struct ast_channel *chan, struct ast_frame *first_frame);
174 static long get_now(struct ast_jb *jb, struct timeval *tv);
175
176
177 /* Interface ast jb functions impl */
178
179
180 static void jb_choose_impl(struct ast_channel *chan)
181 {
182         struct ast_jb *jb = &chan->jb;
183         struct ast_jb_conf *jbconf = &jb->conf;
184         struct ast_jb_impl *test_impl;
185         int i, avail_impl_count = ARRAY_LEN(avail_impl);
186         
187         jb->impl = &avail_impl[default_impl];
188         
189         if (ast_strlen_zero(jbconf->impl))
190                 return;
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         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         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         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                 snprintf(name2, sizeof(name2), "%s", chan->name);
488                 tmp = strchr(name2, '/');
489                 if (tmp)
490                         *tmp = '#';
491                 
492                 bridged = ast_bridged_channel(chan);
493                 /* We should always have bridged chan if a jitterbuffer is in use */
494                 ast_assert(bridged != NULL);
495
496                 snprintf(name1, sizeof(name1), "%s", bridged->name);
497                 tmp = strchr(name1, '/');
498                 if (tmp)
499                         *tmp = '#';
500                 
501                 snprintf(logfile_pathname, sizeof(logfile_pathname),
502                         "/tmp/ast_%s_jb_%s--%s.log", jbimpl->name, name1, name2);
503                 jb->logfile = fopen(logfile_pathname, "w+b");
504                 
505                 if (!jb->logfile)
506                         ast_log(LOG_ERROR, "Failed to create frame log file with pathname '%s'\n", logfile_pathname);
507                 
508                 if (res == JB_IMPL_OK)
509                         jb_framelog("JB_PUT_FIRST {now=%ld}: Queued frame with ts=%ld and len=%ld\n",
510                                 now, frr->ts, frr->len);
511                 else
512                         jb_framelog("JB_PUT_FIRST {now=%ld}: Dropped frame with ts=%ld and len=%ld\n",
513                                 now, frr->ts, frr->len);
514         }
515
516         ast_verb(3, "%s jitterbuffer created on channel %s\n", jbimpl->name, chan->name);
517         
518         /* Free the frame if it has not been queued in the jb */
519         if (res != JB_IMPL_OK)
520                 ast_frfree(frr);
521         
522         return 0;
523 }
524
525
526 void ast_jb_destroy(struct ast_channel *chan)
527 {
528         struct ast_jb *jb = &chan->jb;
529         struct ast_jb_impl *jbimpl = jb->impl;
530         void *jbobj = jb->jbobj;
531         struct ast_frame *f;
532
533         if (jb->logfile) {
534                 fclose(jb->logfile);
535                 jb->logfile = NULL;
536         }
537         
538         if (ast_test_flag(jb, JB_CREATED)) {
539                 /* Remove and free all frames still queued in jb */
540                 while (jbimpl->remove(jbobj, &f) == JB_IMPL_OK) {
541                         ast_frfree(f);
542                 }
543                 
544                 jbimpl->destroy(jbobj);
545                 jb->jbobj = NULL;
546                 
547                 ast_clear_flag(jb, JB_CREATED);
548
549                 ast_verb(3, "%s jitterbuffer destroyed on channel %s\n", jbimpl->name, chan->name);
550         }
551 }
552
553
554 static long get_now(struct ast_jb *jb, struct timeval *when)
555 {
556         struct timeval now;
557
558         if (!when) {
559                 when = &now;
560                 gettimeofday(when, NULL);
561         }
562
563         return ast_tvdiff_ms(*when, jb->timebase);
564 }
565
566
567 int ast_jb_read_conf(struct ast_jb_conf *conf, const char *varname, const char *value)
568 {
569         int prefixlen = sizeof(AST_JB_CONF_PREFIX) - 1;
570         const char *name;
571         int tmp;
572         
573         if (strncasecmp(AST_JB_CONF_PREFIX, varname, prefixlen))
574                 return -1;
575         
576         name = varname + prefixlen;
577         
578         if (!strcasecmp(name, AST_JB_CONF_ENABLE)) {
579                 ast_set2_flag(conf, ast_true(value), AST_JB_ENABLED);
580         } else if (!strcasecmp(name, AST_JB_CONF_FORCE)) {
581                 ast_set2_flag(conf, ast_true(value), AST_JB_FORCED);
582         } else if (!strcasecmp(name, AST_JB_CONF_MAX_SIZE)) {
583                 if ((tmp = atoi(value)) > 0)
584                         conf->max_size = tmp;
585         } else if (!strcasecmp(name, AST_JB_CONF_RESYNCH_THRESHOLD)) {
586                 if ((tmp = atoi(value)) > 0)
587                         conf->resync_threshold = tmp;
588         } else if (!strcasecmp(name, AST_JB_CONF_IMPL)) {
589                 if (!ast_strlen_zero(value))
590                         snprintf(conf->impl, sizeof(conf->impl), "%s", value);
591         } else if (!strcasecmp(name, AST_JB_CONF_LOG)) {
592                 ast_set2_flag(conf, ast_true(value), AST_JB_LOG);
593         } else {
594                 return -1;
595         }
596         
597         return 0;
598 }
599
600
601 void ast_jb_configure(struct ast_channel *chan, const struct ast_jb_conf *conf)
602 {
603         memcpy(&chan->jb.conf, conf, sizeof(*conf));
604 }
605
606
607 void ast_jb_get_config(const struct ast_channel *chan, struct ast_jb_conf *conf)
608 {
609         memcpy(conf, &chan->jb.conf, sizeof(*conf));
610 }
611
612 void ast_jb_empty_and_reset(struct ast_channel *c0, struct ast_channel *c1)
613 {
614         struct ast_jb *jb0 = &c0->jb;
615         struct ast_jb *jb1 = &c1->jb;
616         int c0_use_jb = ast_test_flag(jb0, JB_USE);
617         int c0_jb_is_created = ast_test_flag(jb0, JB_CREATED);
618         int c1_use_jb = ast_test_flag(jb1, JB_USE);
619         int c1_jb_is_created = ast_test_flag(jb1, JB_CREATED);
620
621         if (c0_use_jb && c0_jb_is_created && jb0->impl->empty_and_reset) {
622                 jb0->impl->empty_and_reset(jb0->jbobj);
623         }
624
625         if (c1_use_jb && c1_jb_is_created && jb1->impl->empty_and_reset) {
626                 jb1->impl->empty_and_reset(jb1->jbobj);
627         }
628 }
629
630 /* Implementation functions */
631
632 /* fixed */
633 static void * jb_create_fixed(struct ast_jb_conf *general_config, long resynch_threshold)
634 {
635         struct fixed_jb_conf conf;
636
637         conf.jbsize = general_config->max_size;
638         conf.resync_threshold = resynch_threshold;
639
640         return fixed_jb_new(&conf);
641 }
642
643 static void jb_destroy_fixed(void *jb)
644 {
645         struct fixed_jb *fixedjb = (struct fixed_jb *) jb;
646         
647         /* destroy the jb */
648         fixed_jb_destroy(fixedjb);
649 }
650
651
652 static int jb_put_first_fixed(void *jb, struct ast_frame *fin, long now)
653 {
654         struct fixed_jb *fixedjb = (struct fixed_jb *) jb;
655         int res;
656         
657         res = fixed_jb_put_first(fixedjb, fin, fin->len, fin->ts, now);
658         
659         return fixed_to_abstract_code[res];
660 }
661
662
663 static int jb_put_fixed(void *jb, struct ast_frame *fin, long now)
664 {
665         struct fixed_jb *fixedjb = (struct fixed_jb *) jb;
666         int res;
667         
668         res = fixed_jb_put(fixedjb, fin, fin->len, fin->ts, now);
669         
670         return fixed_to_abstract_code[res];
671 }
672
673
674 static int jb_get_fixed(void *jb, struct ast_frame **fout, long now, long interpl)
675 {
676         struct fixed_jb *fixedjb = (struct fixed_jb *) jb;
677         struct fixed_jb_frame frame;
678         int res;
679         
680         res = fixed_jb_get(fixedjb, &frame, now, interpl);
681         *fout = frame.data;
682         
683         return fixed_to_abstract_code[res];
684 }
685
686
687 static long jb_next_fixed(void *jb)
688 {
689         struct fixed_jb *fixedjb = (struct fixed_jb *) jb;
690         
691         return fixed_jb_next(fixedjb);
692 }
693
694
695 static int jb_remove_fixed(void *jb, struct ast_frame **fout)
696 {
697         struct fixed_jb *fixedjb = (struct fixed_jb *) jb;
698         struct fixed_jb_frame frame;
699         int res;
700         
701         res = fixed_jb_remove(fixedjb, &frame);
702         *fout = frame.data;
703         
704         return fixed_to_abstract_code[res];
705 }
706
707
708 static void jb_force_resynch_fixed(void *jb)
709 {
710         struct fixed_jb *fixedjb = (struct fixed_jb *) jb;
711         
712         fixed_jb_set_force_resynch(fixedjb);
713 }
714
715 static void jb_empty_and_reset_fixed(void *jb)
716 {
717         struct fixed_jb *fixedjb = jb;
718         struct fixed_jb_frame f;
719
720         while (fixed_jb_remove(fixedjb, &f) == FIXED_JB_OK) {
721                 ast_frfree(f.data);
722         }
723 }
724
725 /* adaptive */
726
727 static void *jb_create_adaptive(struct ast_jb_conf *general_config, long resynch_threshold)
728 {
729         jb_conf jbconf;
730         jitterbuf *adaptivejb;
731
732         adaptivejb = jb_new();
733         if (adaptivejb) {
734                 jbconf.max_jitterbuf = general_config->max_size;
735                 jbconf.resync_threshold = general_config->resync_threshold;
736                 jbconf.max_contig_interp = 10;
737                 jb_setconf(adaptivejb, &jbconf);
738         }
739         
740         return adaptivejb;
741 }
742
743
744 static void jb_destroy_adaptive(void *jb)
745 {
746         jitterbuf *adaptivejb = (jitterbuf *) jb;
747         
748         jb_destroy(adaptivejb);
749 }
750
751
752 static int jb_put_first_adaptive(void *jb, struct ast_frame *fin, long now)
753 {
754         return jb_put_adaptive(jb, fin, now);
755 }
756
757
758 static int jb_put_adaptive(void *jb, struct ast_frame *fin, long now)
759 {
760         jitterbuf *adaptivejb = (jitterbuf *) jb;
761         int res;
762         
763         res = jb_put(adaptivejb, fin, JB_TYPE_VOICE, fin->len, fin->ts, now);
764         
765         return adaptive_to_abstract_code[res];
766 }
767
768
769 static int jb_get_adaptive(void *jb, struct ast_frame **fout, long now, long interpl)
770 {
771         jitterbuf *adaptivejb = (jitterbuf *) jb;
772         jb_frame frame;
773         int res;
774         
775         res = jb_get(adaptivejb, &frame, now, interpl);
776         *fout = frame.data;
777         
778         return adaptive_to_abstract_code[res];
779 }
780
781
782 static long jb_next_adaptive(void *jb)
783 {
784         jitterbuf *adaptivejb = (jitterbuf *) jb;
785         
786         return jb_next(adaptivejb);
787 }
788
789
790 static int jb_remove_adaptive(void *jb, struct ast_frame **fout)
791 {
792         jitterbuf *adaptivejb = (jitterbuf *) jb;
793         jb_frame frame;
794         int res;
795         
796         res = jb_getall(adaptivejb, &frame);
797         *fout = frame.data;
798         
799         return adaptive_to_abstract_code[res];
800 }
801
802
803 static void jb_force_resynch_adaptive(void *jb)
804 {
805 }
806
807 static void jb_empty_and_reset_adaptive(void *jb)
808 {
809         jitterbuf *adaptivejb = jb;
810         jb_frame f;
811
812         while (jb_getall(adaptivejb, &f) == JB_OK) {
813                 ast_frfree(f.data);
814         }
815
816         jb_reset(adaptivejb);
817 }