Fri Sep 25 19:28:16 2009

Asterisk developer's documentation


abstract_jb.h File Reference

Common implementation-independent jitterbuffer stuff. More...

#include <stdio.h>
#include <sys/time.h>

Include dependency graph for abstract_jb.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ast_jb
 General jitterbuffer state. More...
struct  ast_jb_conf
 General jitterbuffer configuration. More...

Defines

#define AST_JB_CONF_ENABLE   "enable"
#define AST_JB_CONF_FORCE   "force"
#define AST_JB_CONF_IMPL   "impl"
#define AST_JB_CONF_LOG   "log"
#define AST_JB_CONF_MAX_SIZE   "maxsize"
#define AST_JB_CONF_PREFIX   "jb"
#define AST_JB_CONF_RESYNCH_THRESHOLD   "resyncthreshold"
#define AST_JB_IMPL_NAME_SIZE   12

Enumerations

enum  { AST_JB_ENABLED = (1 << 0), AST_JB_FORCED = (1 << 1), AST_JB_LOG = (1 << 2) }

Functions

void ast_jb_configure (struct ast_channel *chan, const struct ast_jb_conf *conf)
 Configures a jitterbuffer on a channel.
void ast_jb_destroy (struct ast_channel *chan)
 Destroys jitterbuffer on a channel.
int ast_jb_do_usecheck (struct ast_channel *c0, struct ast_channel *c1)
 Checks the need of a jb use in a generic bridge.
void ast_jb_get_and_deliver (struct ast_channel *c0, struct ast_channel *c1)
 Deliver the queued frames that should be delivered now for both channels.
void ast_jb_get_config (const struct ast_channel *chan, struct ast_jb_conf *conf)
 Copies a channel's jitterbuffer configuration.
int ast_jb_get_when_to_wakeup (struct ast_channel *c0, struct ast_channel *c1, int time_left)
 Calculates the time, left to the closest delivery moment in a bridge.
int ast_jb_put (struct ast_channel *chan, struct ast_frame *f)
 Puts a frame into a channel jitterbuffer.
int ast_jb_read_conf (struct ast_jb_conf *conf, char *varname, char *value)
 Sets jitterbuffer configuration property.


Detailed Description

Common implementation-independent jitterbuffer stuff.

Author:
Slav Klenov <slav@securax.org>

Definition in file abstract_jb.h.


Define Documentation

#define AST_JB_CONF_ENABLE   "enable"

Definition at line 70 of file abstract_jb.h.

Referenced by ast_jb_read_conf().

#define AST_JB_CONF_FORCE   "force"

Definition at line 71 of file abstract_jb.h.

Referenced by ast_jb_read_conf().

#define AST_JB_CONF_IMPL   "impl"

Definition at line 74 of file abstract_jb.h.

Referenced by ast_jb_read_conf().

#define AST_JB_CONF_LOG   "log"

Definition at line 75 of file abstract_jb.h.

Referenced by ast_jb_read_conf().

#define AST_JB_CONF_MAX_SIZE   "maxsize"

Definition at line 72 of file abstract_jb.h.

Referenced by ast_jb_read_conf().

#define AST_JB_CONF_PREFIX   "jb"

Definition at line 69 of file abstract_jb.h.

Referenced by ast_jb_read_conf().

#define AST_JB_CONF_RESYNCH_THRESHOLD   "resyncthreshold"

Definition at line 73 of file abstract_jb.h.

Referenced by ast_jb_read_conf().

#define AST_JB_IMPL_NAME_SIZE   12

Definition at line 50 of file abstract_jb.h.

Referenced by create_jb().


Enumeration Type Documentation

anonymous enum

Enumerator:
AST_JB_ENABLED 
AST_JB_FORCED 
AST_JB_LOG 

Definition at line 44 of file abstract_jb.h.

00044      {
00045    AST_JB_ENABLED = (1 << 0),
00046    AST_JB_FORCED =  (1 << 1),
00047    AST_JB_LOG =     (1 << 2)
00048 };


Function Documentation

void ast_jb_configure ( struct ast_channel chan,
const struct ast_jb_conf conf 
)

Configures a jitterbuffer on a channel.

Parameters:
chan channel to configure.
conf configuration to apply.
Called from a channel driver when a channel is created and its jitterbuffer needs to be configured.

Definition at line 601 of file abstract_jb.c.

References ast_jb::conf, and ast_channel::jb.

Referenced by __oh323_rtp_create(), __oh323_update_info(), alsa_new(), gtalk_new(), mgcp_new(), oss_new(), sip_new(), skinny_new(), and zt_new().

00602 {
00603    memcpy(&chan->jb.conf, conf, sizeof(*conf));
00604 }

void ast_jb_destroy ( struct ast_channel chan  ) 

Destroys jitterbuffer on a channel.

Parameters:
chan channel.
Called from ast_channel_free() when a channel is destroyed.

Definition at line 525 of file abstract_jb.c.

References ast_clear_flag, ast_frfree, ast_test_flag, ast_verbose(), ast_jb_impl::destroy, f, ast_jb::impl, ast_channel::jb, JB_CREATED, JB_IMPL_OK, ast_jb::jbobj, ast_jb::logfile, ast_jb_impl::name, option_verbose, ast_jb_impl::remove, and VERBOSE_PREFIX_3.

Referenced by ast_channel_free().

00526 {
00527    struct ast_jb *jb = &chan->jb;
00528    struct ast_jb_impl *jbimpl = jb->impl;
00529    void *jbobj = jb->jbobj;
00530    struct ast_frame *f;
00531 
00532    if (jb->logfile) {
00533       fclose(jb->logfile);
00534       jb->logfile = NULL;
00535    }
00536    
00537    if (ast_test_flag(jb, JB_CREATED)) {
00538       /* Remove and free all frames still queued in jb */
00539       while (jbimpl->remove(jbobj, &f) == JB_IMPL_OK) {
00540          ast_frfree(f);
00541       }
00542       
00543       jbimpl->destroy(jbobj);
00544       jb->jbobj = NULL;
00545       
00546       ast_clear_flag(jb, JB_CREATED);
00547 
00548       if (option_verbose > 2)
00549          ast_verbose(VERBOSE_PREFIX_3 "%s jitterbuffer destroyed on channel %s\n", jbimpl->name, chan->name);
00550    }
00551 }

int ast_jb_do_usecheck ( struct ast_channel c0,
struct ast_channel c1 
)

Checks the need of a jb use in a generic bridge.

Parameters:
c0 first bridged channel.
c1 second bridged channel.
Called from ast_generic_bridge() when two channels are entering in a bridge. The function checks the need of a jitterbuffer, depending on both channel's configuration and technology properties. As a result, this function sets appropriate internal jb flags to the channels, determining further behaviour of the bridged jitterbuffers.

Returns:
zero if there are no jitter buffers in use, non-zero if there are

Definition at line 198 of file abstract_jb.c.

References AST_CHAN_TP_CREATESJITTER, AST_CHAN_TP_WANTSJITTER, AST_JB_ENABLED, AST_JB_FORCED, ast_set_flag, ast_test_flag, ast_jb::conf, inuse, ast_channel::jb, jb_choose_impl(), JB_CREATED, JB_TIMEBASE_INITIALIZED, JB_USE, ast_channel_tech::properties, ast_channel::tech, and ast_jb::timebase.

Referenced by ast_generic_bridge().

00199 {
00200    struct ast_jb *jb0 = &c0->jb;
00201    struct ast_jb *jb1 = &c1->jb;
00202    struct ast_jb_conf *conf0 = &jb0->conf;
00203    struct ast_jb_conf *conf1 = &jb1->conf;
00204    int c0_wants_jitter = c0->tech->properties & AST_CHAN_TP_WANTSJITTER;
00205    int c0_creates_jitter = c0->tech->properties & AST_CHAN_TP_CREATESJITTER;
00206    int c0_jb_enabled = ast_test_flag(conf0, AST_JB_ENABLED);
00207    int c0_force_jb = ast_test_flag(conf0, AST_JB_FORCED);
00208    int c0_jb_timebase_initialized = ast_test_flag(jb0, JB_TIMEBASE_INITIALIZED);
00209    int c0_jb_created = ast_test_flag(jb0, JB_CREATED);
00210    int c1_wants_jitter = c1->tech->properties & AST_CHAN_TP_WANTSJITTER;
00211    int c1_creates_jitter = c1->tech->properties & AST_CHAN_TP_CREATESJITTER;
00212    int c1_jb_enabled = ast_test_flag(conf1, AST_JB_ENABLED);
00213    int c1_force_jb = ast_test_flag(conf1, AST_JB_FORCED);
00214    int c1_jb_timebase_initialized = ast_test_flag(jb1, JB_TIMEBASE_INITIALIZED);
00215    int c1_jb_created = ast_test_flag(jb1, JB_CREATED);
00216    int inuse = 0;
00217 
00218    /* Determine whether audio going to c0 needs a jitter buffer */
00219    if (((!c0_wants_jitter && c1_creates_jitter) || (c0_force_jb && c1_creates_jitter)) && c0_jb_enabled) {
00220       ast_set_flag(jb0, JB_USE);
00221       if (!c0_jb_timebase_initialized) {
00222          if (c1_jb_timebase_initialized) {
00223             memcpy(&jb0->timebase, &jb1->timebase, sizeof(struct timeval));
00224          } else {
00225             gettimeofday(&jb0->timebase, NULL);
00226          }
00227          ast_set_flag(jb0, JB_TIMEBASE_INITIALIZED);
00228       }
00229       
00230       if (!c0_jb_created) {
00231          jb_choose_impl(c0);
00232       }
00233 
00234       inuse = 1;
00235    }
00236    
00237    /* Determine whether audio going to c1 needs a jitter buffer */
00238    if (((!c1_wants_jitter && c0_creates_jitter) || (c1_force_jb && c0_creates_jitter)) && c1_jb_enabled) {
00239       ast_set_flag(jb1, JB_USE);
00240       if (!c1_jb_timebase_initialized) {
00241          if (c0_jb_timebase_initialized) {
00242             memcpy(&jb1->timebase, &jb0->timebase, sizeof(struct timeval));
00243          } else {
00244             gettimeofday(&jb1->timebase, NULL);
00245          }
00246          ast_set_flag(jb1, JB_TIMEBASE_INITIALIZED);
00247       }
00248       
00249       if (!c1_jb_created) {
00250          jb_choose_impl(c1);
00251       }
00252 
00253       inuse = 1;
00254    }
00255 
00256    return inuse;
00257 }

void ast_jb_get_and_deliver ( struct ast_channel c0,
struct ast_channel c1 
)

Deliver the queued frames that should be delivered now for both channels.

Parameters:
c0 first bridged channel.
c1 second bridged channel.
Called from ast_generic_bridge() to deliver any frames, that should be delivered for the moment of invocation. Does nothing if neihter of the channels is using jb or has any frames currently queued in. The function delivers frames usig ast_write() each of the channels.

Definition at line 364 of file abstract_jb.c.

References ast_test_flag, ast_channel::jb, JB_CREATED, jb_get_and_deliver(), and JB_USE.

Referenced by ast_generic_bridge().

00365 {
00366    struct ast_jb *jb0 = &c0->jb;
00367    struct ast_jb *jb1 = &c1->jb;
00368    int c0_use_jb = ast_test_flag(jb0, JB_USE);
00369    int c0_jb_is_created = ast_test_flag(jb0, JB_CREATED);
00370    int c1_use_jb = ast_test_flag(jb1, JB_USE);
00371    int c1_jb_is_created = ast_test_flag(jb1, JB_CREATED);
00372    
00373    if (c0_use_jb && c0_jb_is_created)
00374       jb_get_and_deliver(c0);
00375    
00376    if (c1_use_jb && c1_jb_is_created)
00377       jb_get_and_deliver(c1);
00378 }

void ast_jb_get_config ( const struct ast_channel chan,
struct ast_jb_conf conf 
)

Copies a channel's jitterbuffer configuration.

Parameters:
chan channel.
conf destination.

Definition at line 607 of file abstract_jb.c.

References ast_jb::conf, and ast_channel::jb.

00608 {
00609    memcpy(conf, &chan->jb.conf, sizeof(*conf));
00610 }

int ast_jb_get_when_to_wakeup ( struct ast_channel c0,
struct ast_channel c1,
int  time_left 
)

Calculates the time, left to the closest delivery moment in a bridge.

Parameters:
c0 first bridged channel.
c1 second bridged channel.
time_left bridge time limit, or -1 if not set.
Called from ast_generic_bridge() to determine the maximum time to wait for activity in ast_waitfor_n() call. If neihter of the channels is using jb, this function returns the time limit passed.

Returns:
maximum time to wait.

Definition at line 259 of file abstract_jb.c.

References ast_test_flag, get_now(), ast_channel::jb, JB_CREATED, JB_USE, and ast_jb::next.

Referenced by ast_generic_bridge().

00260 {
00261    struct ast_jb *jb0 = &c0->jb;
00262    struct ast_jb *jb1 = &c1->jb;
00263    int c0_use_jb = ast_test_flag(jb0, JB_USE);
00264    int c0_jb_is_created = ast_test_flag(jb0, JB_CREATED);
00265    int c1_use_jb = ast_test_flag(jb1, JB_USE);
00266    int c1_jb_is_created = ast_test_flag(jb1, JB_CREATED);
00267    int wait, wait0, wait1;
00268    struct timeval tv_now;
00269    
00270    if (time_left == 0) {
00271       /* No time left - the bridge will be retried */
00272       /* TODO: Test disable this */
00273       /*return 0;*/
00274    }
00275    
00276    if (time_left < 0) {
00277       time_left = INT_MAX;
00278    }
00279    
00280    gettimeofday(&tv_now, NULL);
00281    
00282    wait0 = (c0_use_jb && c0_jb_is_created) ? jb0->next - get_now(jb0, &tv_now) : time_left;
00283    wait1 = (c1_use_jb && c1_jb_is_created) ? jb1->next - get_now(jb1, &tv_now) : time_left;
00284    
00285    wait = wait0 < wait1 ? wait0 : wait1;
00286    wait = wait < time_left ? wait : time_left;
00287    
00288    if (wait == INT_MAX) {
00289       wait = -1;
00290    } else if (wait < 1) {
00291       /* don't let wait=0, because this can cause the pbx thread to loop without any sleeping at all */
00292       wait = 1;
00293    }
00294    
00295    return wait;
00296 }

int ast_jb_put ( struct ast_channel chan,
struct ast_frame f 
)

Puts a frame into a channel jitterbuffer.

Parameters:
chan channel.
f frame.
Called from ast_generic_bridge() to put a frame into a channel's jitterbuffer. The function will successfuly enqueue a frame if and only if: 1. the channel is using a jitterbuffer (as determined by ast_jb_do_usecheck()), 2. the frame's type is AST_FRAME_VOICE, 3. the frame has timing info set and has length >= 2 ms, 4. there is no some internal error happened (like failed memory allocation). Frames, successfuly queued, should be delivered by the channel's jitterbuffer, when their delivery time has came. Frames, not successfuly queued, should be delivered immediately. Dropped by the jb implementation frames are considered successfuly enqueued as far as they should not be delivered at all.

Returns:
zero if the frame was queued, -1 if not.

Definition at line 299 of file abstract_jb.c.

References ast_clear_flag, AST_FRAME_DTMF, AST_FRAME_VOICE, ast_frdup(), ast_frfree, ast_log(), ast_set_flag, ast_test_flag, create_jb(), ast_jb_impl::force_resync, ast_frame::frametype, get_now(), ast_frame::has_timing_info, ast_jb::impl, ast_channel::jb, JB_CREATED, jb_framelog, JB_IMPL_OK, JB_USE, ast_jb::jbobj, ast_frame::len, LOG_ERROR, LOG_WARNING, ast_jb_impl::next, ast_jb::next, ast_jb_impl::put, ast_frame::src, and ast_frame::ts.

Referenced by ast_generic_bridge().

00300 {
00301    struct ast_jb *jb = &chan->jb;
00302    struct ast_jb_impl *jbimpl = jb->impl;
00303    void *jbobj = jb->jbobj;
00304    struct ast_frame *frr;
00305    long now = 0;
00306    
00307    if (!ast_test_flag(jb, JB_USE))
00308       return -1;
00309 
00310    if (f->frametype != AST_FRAME_VOICE) {
00311       if (f->frametype == AST_FRAME_DTMF && ast_test_flag(jb, JB_CREATED)) {
00312          jb_framelog("JB_PUT {now=%ld}: Received DTMF frame. Force resynching jb...\n", now);
00313          jbimpl->force_resync(jbobj);
00314       }
00315       
00316       return -1;
00317    }
00318 
00319    /* We consider an enabled jitterbuffer should receive frames with valid timing info. */
00320    if (!f->has_timing_info || f->len < 2 || f->ts < 0) {
00321       ast_log(LOG_WARNING, "%s recieved frame with invalid timing info: "
00322          "has_timing_info=%d, len=%ld, ts=%ld, src=%s\n",
00323          chan->name, f->has_timing_info, f->len, f->ts, f->src);
00324       return -1;
00325    }
00326 
00327    frr = ast_frdup(f);
00328 
00329    if (!frr) {
00330       ast_log(LOG_ERROR, "Failed to isolate frame for the jitterbuffer on channel '%s'\n", chan->name);
00331       return -1;
00332    }
00333 
00334    if (!ast_test_flag(jb, JB_CREATED)) {
00335       if (create_jb(chan, frr)) {
00336          ast_frfree(frr);
00337          /* Disable the jitterbuffer */
00338          ast_clear_flag(jb, JB_USE);
00339          return -1;
00340       }
00341 
00342       ast_set_flag(jb, JB_CREATED);
00343       return 0;
00344    } else {
00345       now = get_now(jb, NULL);
00346       if (jbimpl->put(jbobj, frr, now) != JB_IMPL_OK) {
00347          jb_framelog("JB_PUT {now=%ld}: Dropped frame with ts=%ld and len=%ld\n", now, frr->ts, frr->len);
00348          ast_frfree(frr);
00349          /*return -1;*/
00350          /* TODO: Check this fix - should return 0 here, because the dropped frame shouldn't 
00351             be delivered at all */
00352          return 0;
00353       }
00354 
00355       jb->next = jbimpl->next(jbobj);
00356 
00357       jb_framelog("JB_PUT {now=%ld}: Queued frame with ts=%ld and len=%ld\n", now, frr->ts, frr->len);
00358 
00359       return 0;
00360    }
00361 }

int ast_jb_read_conf ( struct ast_jb_conf conf,
char *  varname,
char *  value 
)

Sets jitterbuffer configuration property.

Parameters:
conf configuration to store the property in.
varname property name.
value property value.
Called from a channel driver to build a jitterbuffer configuration tipically when reading a configuration file. It is not neccessary for a channel driver to know each of the jb configuration property names. The jitterbuffer itself knows them. The channel driver can pass each config var it reads through this function. It will return 0 if the variable was consumed from the jb conf.

Returns:
zero if the property was set to the configuration, -1 if not.

Definition at line 567 of file abstract_jb.c.

References AST_JB_CONF_ENABLE, AST_JB_CONF_FORCE, AST_JB_CONF_IMPL, AST_JB_CONF_LOG, AST_JB_CONF_MAX_SIZE, AST_JB_CONF_PREFIX, AST_JB_CONF_RESYNCH_THRESHOLD, AST_JB_ENABLED, AST_JB_FORCED, AST_JB_LOG, ast_set2_flag, ast_strlen_zero(), ast_true(), ast_jb_conf::impl, ast_jb_conf::max_size, name, and ast_jb_conf::resync_threshold.

Referenced by gtalk_load_config(), load_module(), process_zap(), reload_config(), and store_config().

00568 {
00569    int prefixlen = sizeof(AST_JB_CONF_PREFIX) - 1;
00570    char *name;
00571    int tmp;
00572    
00573    if (strncasecmp(AST_JB_CONF_PREFIX, varname, prefixlen))
00574       return -1;
00575    
00576    name = varname + prefixlen;
00577    
00578    if (!strcasecmp(name, AST_JB_CONF_ENABLE)) {
00579       ast_set2_flag(conf, ast_true(value), AST_JB_ENABLED);
00580    } else if (!strcasecmp(name, AST_JB_CONF_FORCE)) {
00581       ast_set2_flag(conf, ast_true(value), AST_JB_FORCED);
00582    } else if (!strcasecmp(name, AST_JB_CONF_MAX_SIZE)) {
00583       if ((tmp = atoi(value)) > 0)
00584          conf->max_size = tmp;
00585    } else if (!strcasecmp(name, AST_JB_CONF_RESYNCH_THRESHOLD)) {
00586       if ((tmp = atoi(value)) > 0)
00587          conf->resync_threshold = tmp;
00588    } else if (!strcasecmp(name, AST_JB_CONF_IMPL)) {
00589       if (!ast_strlen_zero(value))
00590          snprintf(conf->impl, sizeof(conf->impl), "%s", value);
00591    } else if (!strcasecmp(name, AST_JB_CONF_LOG)) {
00592       ast_set2_flag(conf, ast_true(value), AST_JB_LOG);
00593    } else {
00594       return -1;
00595    }
00596    
00597    return 0;
00598 }


Generated on Fri Sep 25 19:28:16 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.5