chan_nbs.c File Reference

Network broadcast sound support channel driver. More...

#include "asterisk.h"
#include <sys/socket.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <nbs.h>
#include "asterisk/lock.h"
#include "asterisk/channel.h"
#include "asterisk/config.h"
#include "asterisk/module.h"
#include "asterisk/pbx.h"
#include "asterisk/utils.h"
#include "asterisk/format_cache.h"

Include dependency graph for chan_nbs.c:

Go to the source code of this file.

Data Structures

struct  nbs_pvt

Functions

 AST_MODULE_INFO_STANDARD_EXTENDED (ASTERISK_GPL_KEY,"Network Broadcast Sound Support")
static int load_module (void)
static struct nbs_pvtnbs_alloc (const char *data)
static int nbs_call (struct ast_channel *ast, const char *dest, int timeout)
static void nbs_destroy (struct nbs_pvt *p)
static int nbs_hangup (struct ast_channel *ast)
static struct ast_channelnbs_new (struct nbs_pvt *i, int state, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor)
static struct ast_channelnbs_request (const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause)
static struct ast_framenbs_xread (struct ast_channel *ast)
static int nbs_xwrite (struct ast_channel *ast, struct ast_frame *frame)
static int unload_module (void)

Variables

static char context [AST_MAX_EXTENSION] = "default"
static struct ast_channel_tech nbs_tech
static const char tdesc [] = "Network Broadcast Sound Driver"
static const char type [] = "NBS"


Detailed Description

Network broadcast sound support channel driver.

Author:
Mark Spencer <markster@digium.com>

Definition in file chan_nbs.c.


Function Documentation

AST_MODULE_INFO_STANDARD_EXTENDED ( ASTERISK_GPL_KEY  ,
"Network Broadcast Sound Support"   
)

static int load_module ( void   )  [static]

static struct nbs_pvt* nbs_alloc ( const char *  data  )  [static, read]

Definition at line 117 of file chan_nbs.c.

References ast_calloc, ast_copy_string(), ast_free, ast_log, ast_strlen_zero, LOG_WARNING, nbs_pvt::nbs, NULL, and nbs_pvt::stream.

Referenced by nbs_request().

00118 {
00119    struct nbs_pvt *p;
00120    int flags = 0;
00121    char stream[256];
00122    char *opts;
00123 
00124    ast_copy_string(stream, data, sizeof(stream));
00125    if ((opts = strchr(stream, ':'))) {
00126       *opts = '\0';
00127       opts++;
00128    } else
00129       opts = "";
00130    p = ast_calloc(1, sizeof(*p));
00131    if (p) {
00132       if (!ast_strlen_zero(opts)) {
00133          if (strchr(opts, 'm'))
00134             flags |= NBS_FLAG_MUTE;
00135          if (strchr(opts, 'o'))
00136             flags |= NBS_FLAG_OVERSPEAK;
00137          if (strchr(opts, 'e'))
00138             flags |= NBS_FLAG_EMERGENCY;
00139          if (strchr(opts, 'O'))
00140             flags |= NBS_FLAG_OVERRIDE;
00141       } else
00142          flags = NBS_FLAG_OVERSPEAK;
00143       
00144       ast_copy_string(p->stream, stream, sizeof(p->stream));
00145       p->nbs = nbs_newstream("asterisk", stream, flags);
00146       if (!p->nbs) {
00147          ast_log(LOG_WARNING, "Unable to allocate new NBS stream '%s' with flags %d\n", stream, flags);
00148          ast_free(p);
00149          p = NULL;
00150       } else {
00151          /* Set for 8000 hz mono, 640 samples */
00152          nbs_setbitrate(p->nbs, 8000);
00153          nbs_setchannels(p->nbs, 1);
00154          nbs_setblocksize(p->nbs, 640);
00155          nbs_setblocking(p->nbs, 0);
00156       }
00157    }
00158    return p;
00159 }

static int nbs_call ( struct ast_channel ast,
const char *  dest,
int  timeout 
) [static]

Definition at line 83 of file chan_nbs.c.

References ast_channel_name(), ast_channel_tech_pvt(), AST_CONTROL_ANSWER, AST_CONTROL_CONGESTION, ast_debug, ast_log, ast_queue_control(), ast_setstate(), AST_STATE_DOWN, AST_STATE_RESERVED, AST_STATE_RINGING, LOG_WARNING, and nbs_pvt::nbs.

00084 {
00085    struct nbs_pvt *p;
00086 
00087    p = ast_channel_tech_pvt(ast);
00088 
00089    if ((ast_channel_state(ast) != AST_STATE_DOWN) && (ast_channel_state(ast) != AST_STATE_RESERVED)) {
00090       ast_log(LOG_WARNING, "nbs_call called on %s, neither down nor reserved\n", ast_channel_name(ast));
00091       return -1;
00092    }
00093    /* When we call, it just works, really, there's no destination...  Just
00094       ring the phone and wait for someone to answer */
00095    ast_debug(1, "Calling %s on %s\n", dest, ast_channel_name(ast));
00096 
00097    /* If we can't connect, return congestion */
00098    if (nbs_connect(p->nbs)) {
00099       ast_log(LOG_WARNING, "NBS Connection failed on %s\n", ast_channel_name(ast));
00100       ast_queue_control(ast, AST_CONTROL_CONGESTION);
00101    } else {
00102       ast_setstate(ast, AST_STATE_RINGING);
00103       ast_queue_control(ast, AST_CONTROL_ANSWER);
00104    }
00105 
00106    return 0;
00107 }

static void nbs_destroy ( struct nbs_pvt p  )  [static]

Definition at line 109 of file chan_nbs.c.

References ast_free, ast_module_user_remove, nbs_pvt::nbs, and nbs_pvt::u.

Referenced by nbs_hangup(), and nbs_request().

00110 {
00111    if (p->nbs)
00112       nbs_delstream(p->nbs);
00113    ast_module_user_remove(p->u);
00114    ast_free(p);
00115 }

static int nbs_hangup ( struct ast_channel ast  )  [static]

Definition at line 161 of file chan_nbs.c.

References ast_channel_name(), ast_channel_tech_pvt(), ast_channel_tech_pvt_set(), ast_debug, ast_log, ast_setstate(), AST_STATE_DOWN, LOG_WARNING, nbs_destroy(), and NULL.

00162 {
00163    struct nbs_pvt *p;
00164    p = ast_channel_tech_pvt(ast);
00165    ast_debug(1, "nbs_hangup(%s)\n", ast_channel_name(ast));
00166    if (!ast_channel_tech_pvt(ast)) {
00167       ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
00168       return 0;
00169    }
00170    nbs_destroy(p);
00171    ast_channel_tech_pvt_set(ast, NULL);
00172    ast_setstate(ast, AST_STATE_DOWN);
00173    return 0;
00174 }

static struct ast_channel* nbs_new ( struct nbs_pvt i,
int  state,
const struct ast_assigned_ids assignedids,
const struct ast_channel requestor 
) [static, read]

Definition at line 195 of file chan_nbs.c.

References ast_channel_alloc, ast_channel_context_set(), ast_channel_exten_set(), ast_channel_name(), ast_channel_nativeformats_set(), ast_channel_rings_set(), ast_channel_set_fd(), ast_channel_set_rawreadformat(), ast_channel_set_rawwriteformat(), ast_channel_set_readformat(), ast_channel_set_writeformat(), ast_channel_tech_pvt_set(), ast_channel_tech_set(), ast_channel_unlock, ast_format_slin, ast_hangup(), ast_log, ast_module_user_add, ast_pbx_start(), AST_STATE_DOWN, AST_STATE_RING, ast_channel_tech::capabilities, LOG_WARNING, nbs_pvt::nbs, nbs_pvt::owner, nbs_pvt::stream, tmp(), and nbs_pvt::u.

Referenced by nbs_request().

00196 {
00197    struct ast_channel *tmp;
00198    tmp = ast_channel_alloc(1, state, 0, 0, "", "s", context, assignedids, requestor, 0, "NBS/%s", i->stream);
00199    if (tmp) {
00200       ast_channel_tech_set(tmp, &nbs_tech);
00201       ast_channel_set_fd(tmp, 0, nbs_fd(i->nbs));
00202 
00203       ast_channel_nativeformats_set(tmp, nbs_tech.capabilities);
00204       ast_channel_set_rawreadformat(tmp, ast_format_slin);
00205       ast_channel_set_rawwriteformat(tmp, ast_format_slin);
00206       ast_channel_set_writeformat(tmp, ast_format_slin);
00207       ast_channel_set_readformat(tmp, ast_format_slin);
00208       if (state == AST_STATE_RING)
00209          ast_channel_rings_set(tmp, 1);
00210       ast_channel_tech_pvt_set(tmp, i);
00211       ast_channel_context_set(tmp, context);
00212       ast_channel_exten_set(tmp, "s");
00213       ast_channel_language_set(tmp, "");
00214       i->owner = tmp;
00215       i->u = ast_module_user_add(tmp);
00216       ast_channel_unlock(tmp);
00217       if (state != AST_STATE_DOWN) {
00218          if (ast_pbx_start(tmp)) {
00219             ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ast_channel_name(tmp));
00220             ast_hangup(tmp);
00221          }
00222       }
00223    } else
00224       ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
00225    return tmp;
00226 }

static struct ast_channel * nbs_request ( const char *  type,
struct ast_format_cap cap,
const struct ast_assigned_ids assignedids,
const struct ast_channel requestor,
const char *  data,
int *  cause 
) [static, read]

Definition at line 229 of file chan_nbs.c.

References ast_format_cap_get_names(), ast_format_cap_iscompatible_format(), AST_FORMAT_CMP_NOT_EQUAL, ast_format_slin, ast_log, AST_STATE_DOWN, ast_str_alloca, LOG_NOTICE, nbs_alloc(), nbs_destroy(), nbs_new(), NULL, and tmp().

00230 {
00231    struct nbs_pvt *p;
00232    struct ast_channel *tmp = NULL;
00233 
00234    if (ast_format_cap_iscompatible_format(cap, ast_format_slin) == AST_FORMAT_CMP_NOT_EQUAL) {
00235       struct ast_str *cap_buf = ast_str_alloca(64);
00236 
00237       ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%s'\n",
00238          ast_format_cap_get_names(cap, &cap_buf));
00239       return NULL;
00240    }
00241    p = nbs_alloc(data);
00242    if (p) {
00243       tmp = nbs_new(p, AST_STATE_DOWN, assignedids, requestor);
00244       if (!tmp)
00245          nbs_destroy(p);
00246    }
00247    return tmp;
00248 }

static struct ast_frame * nbs_xread ( struct ast_channel ast  )  [static, read]

Definition at line 176 of file chan_nbs.c.

References ast_channel_name(), ast_debug, and ast_null_frame.

00177 {
00178    ast_debug(1, "Returning null frame on %s\n", ast_channel_name(ast));
00179 
00180    return &ast_null_frame;
00181 }

static int nbs_xwrite ( struct ast_channel ast,
struct ast_frame frame 
) [static]

Definition at line 183 of file chan_nbs.c.

References ast_channel_tech_pvt(), AST_STATE_UP, ast_frame::data, ast_frame::datalen, nbs_pvt::nbs, and ast_frame::ptr.

00184 {
00185    struct nbs_pvt *p = ast_channel_tech_pvt(ast);
00186    if (ast_channel_state(ast) != AST_STATE_UP) {
00187       /* Don't try tos end audio on-hook */
00188       return 0;
00189    }
00190    if (nbs_write(p->nbs, frame->data.ptr, frame->datalen / 2) < 0) 
00191       return -1;
00192    return 0;
00193 }

static int unload_module ( void   )  [static]

Definition at line 250 of file chan_nbs.c.

References ao2_ref, ast_channel_unregister(), ast_channel_tech::capabilities, and NULL.

00251 {
00252    /* First, take us out of the channel loop */
00253    ast_channel_unregister(&nbs_tech);
00254    ao2_ref(nbs_tech.capabilities, -1);
00255    nbs_tech.capabilities = NULL;
00256    return 0;
00257 }


Variable Documentation

char context[AST_MAX_EXTENSION] = "default" [static]

Definition at line 54 of file chan_nbs.c.

struct ast_channel_tech nbs_tech [static]

Definition at line 73 of file chan_nbs.c.

const char tdesc[] = "Network Broadcast Sound Driver" [static]

Definition at line 52 of file chan_nbs.c.

const char type[] = "NBS" [static]

Definition at line 55 of file chan_nbs.c.


Generated on Thu Apr 16 06:29:45 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6