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