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