Wed Oct 28 11:45:33 2009

Asterisk developer's documentation


chan_nbs.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2006, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Network broadcast sound support channel driver
00022  * 
00023  * \author Mark Spencer <markster@digium.com>
00024  *
00025  * \ingroup channel_drivers
00026  */
00027 
00028 /*** MODULEINFO
00029    <depend>nbs</depend>
00030  ***/
00031 
00032 #include "asterisk.h"
00033 
00034 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 89512 $")
00035 
00036 #include <sys/socket.h>
00037 #include <sys/time.h>
00038 #include <arpa/inet.h>
00039 #include <fcntl.h>
00040 #include <sys/ioctl.h>
00041 #include <nbs.h>
00042 
00043 #include "asterisk/lock.h"
00044 #include "asterisk/channel.h"
00045 #include "asterisk/config.h"
00046 #include "asterisk/module.h"
00047 #include "asterisk/pbx.h"
00048 #include "asterisk/utils.h"
00049 
00050 static const char tdesc[] = "Network Broadcast Sound Driver";
00051 
00052 /* Only linear is allowed */
00053 static int prefformat = AST_FORMAT_SLINEAR;
00054 
00055 static char context[AST_MAX_EXTENSION] = "default";
00056 static char type[] = "NBS";
00057 
00058 /* NBS creates private structures on demand */
00059    
00060 struct nbs_pvt {
00061    NBS *nbs;
00062    struct ast_channel *owner;    /* Channel we belong to, possibly NULL */
00063    char app[16];              /* Our app */
00064    char stream[80];           /* Our stream */
00065    struct ast_frame fr;       /* "null" frame */
00066    struct ast_module_user *u;    /*! for holding a reference to this module */
00067 };
00068 
00069 static struct ast_channel *nbs_request(const char *type, int format, void *data, int *cause);
00070 static int nbs_call(struct ast_channel *ast, char *dest, int timeout);
00071 static int nbs_hangup(struct ast_channel *ast);
00072 static struct ast_frame *nbs_xread(struct ast_channel *ast);
00073 static int nbs_xwrite(struct ast_channel *ast, struct ast_frame *frame);
00074 
00075 static const struct ast_channel_tech nbs_tech = {
00076    .type = type,
00077    .description = tdesc,
00078    .capabilities = AST_FORMAT_SLINEAR,
00079    .requester = nbs_request,
00080    .call = nbs_call,
00081    .hangup = nbs_hangup,
00082    .read = nbs_xread,
00083    .write = nbs_xwrite,
00084 };
00085 
00086 static int nbs_call(struct ast_channel *ast, char *dest, int timeout)
00087 {
00088    struct nbs_pvt *p;
00089 
00090    p = ast->tech_pvt;
00091 
00092    if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
00093       ast_log(LOG_WARNING, "nbs_call called on %s, neither down nor reserved\n", ast->name);
00094       return -1;
00095    }
00096    /* When we call, it just works, really, there's no destination...  Just
00097       ring the phone and wait for someone to answer */
00098    ast_debug(1, "Calling %s on %s\n", dest, ast->name);
00099 
00100    /* If we can't connect, return congestion */
00101    if (nbs_connect(p->nbs)) {
00102       ast_log(LOG_WARNING, "NBS Connection failed on %s\n", ast->name);
00103       ast_queue_control(ast, AST_CONTROL_CONGESTION);
00104    } else {
00105       ast_setstate(ast, AST_STATE_RINGING);
00106       ast_queue_control(ast, AST_CONTROL_ANSWER);
00107    }
00108 
00109    return 0;
00110 }
00111 
00112 static void nbs_destroy(struct nbs_pvt *p)
00113 {
00114    if (p->nbs)
00115       nbs_delstream(p->nbs);
00116    ast_module_user_remove(p->u);
00117    ast_free(p);
00118 }
00119 
00120 static struct nbs_pvt *nbs_alloc(void *data)
00121 {
00122    struct nbs_pvt *p;
00123    int flags = 0;
00124    char stream[256];
00125    char *opts;
00126 
00127    ast_copy_string(stream, data, sizeof(stream));
00128    if ((opts = strchr(stream, ':'))) {
00129       *opts = '\0';
00130       opts++;
00131    } else
00132       opts = "";
00133    p = ast_calloc(1, sizeof(*p));
00134    if (p) {
00135       if (!ast_strlen_zero(opts)) {
00136          if (strchr(opts, 'm'))
00137             flags |= NBS_FLAG_MUTE;
00138          if (strchr(opts, 'o'))
00139             flags |= NBS_FLAG_OVERSPEAK;
00140          if (strchr(opts, 'e'))
00141             flags |= NBS_FLAG_EMERGENCY;
00142          if (strchr(opts, 'O'))
00143             flags |= NBS_FLAG_OVERRIDE;
00144       } else
00145          flags = NBS_FLAG_OVERSPEAK;
00146       
00147       ast_copy_string(p->stream, stream, sizeof(p->stream));
00148       p->nbs = nbs_newstream("asterisk", stream, flags);
00149       if (!p->nbs) {
00150          ast_log(LOG_WARNING, "Unable to allocate new NBS stream '%s' with flags %d\n", stream, flags);
00151          ast_free(p);
00152          p = NULL;
00153       } else {
00154          /* Set for 8000 hz mono, 640 samples */
00155          nbs_setbitrate(p->nbs, 8000);
00156          nbs_setchannels(p->nbs, 1);
00157          nbs_setblocksize(p->nbs, 640);
00158          nbs_setblocking(p->nbs, 0);
00159       }
00160    }
00161    return p;
00162 }
00163 
00164 static int nbs_hangup(struct ast_channel *ast)
00165 {
00166    struct nbs_pvt *p;
00167    p = ast->tech_pvt;
00168    ast_debug(1, "nbs_hangup(%s)\n", ast->name);
00169    if (!ast->tech_pvt) {
00170       ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
00171       return 0;
00172    }
00173    nbs_destroy(p);
00174    ast->tech_pvt = NULL;
00175    ast_setstate(ast, AST_STATE_DOWN);
00176    return 0;
00177 }
00178 
00179 static struct ast_frame  *nbs_xread(struct ast_channel *ast)
00180 {
00181    struct nbs_pvt *p = ast->tech_pvt;
00182    
00183 
00184    /* Some nice norms */
00185    p->fr.datalen = 0;
00186    p->fr.samples = 0;
00187    p->fr.data =  NULL;
00188    p->fr.src = type;
00189    p->fr.offset = 0;
00190    p->fr.mallocd=0;
00191    p->fr.delivery.tv_sec = 0;
00192    p->fr.delivery.tv_usec = 0;
00193 
00194    ast_debug(1, "Returning null frame on %s\n", ast->name);
00195 
00196    return &p->fr;
00197 }
00198 
00199 static int nbs_xwrite(struct ast_channel *ast, struct ast_frame *frame)
00200 {
00201    struct nbs_pvt *p = ast->tech_pvt;
00202    /* Write a frame of (presumably voice) data */
00203    if (frame->frametype != AST_FRAME_VOICE) {
00204       if (frame->frametype != AST_FRAME_IMAGE)
00205          ast_log(LOG_WARNING, "Don't know what to do with  frame type '%d'\n", frame->frametype);
00206       return 0;
00207    }
00208    if (!(frame->subclass &
00209       (AST_FORMAT_SLINEAR))) {
00210       ast_log(LOG_WARNING, "Cannot handle frames in %d format\n", frame->subclass);
00211       return 0;
00212    }
00213    if (ast->_state != AST_STATE_UP) {
00214       /* Don't try tos end audio on-hook */
00215       return 0;
00216    }
00217    if (nbs_write(p->nbs, frame->data, frame->datalen / 2) < 0) 
00218       return -1;
00219    return 0;
00220 }
00221 
00222 static struct ast_channel *nbs_new(struct nbs_pvt *i, int state)
00223 {
00224    struct ast_channel *tmp;
00225    tmp = ast_channel_alloc(1, state, 0, 0, "", "s", context, 0, "NBS/%s", i->stream);
00226    if (tmp) {
00227       tmp->tech = &nbs_tech;
00228       ast_channel_set_fd(tmp, 0, nbs_fd(i->nbs));
00229       tmp->nativeformats = prefformat;
00230       tmp->rawreadformat = prefformat;
00231       tmp->rawwriteformat = prefformat;
00232       tmp->writeformat = prefformat;
00233       tmp->readformat = prefformat;
00234       if (state == AST_STATE_RING)
00235          tmp->rings = 1;
00236       tmp->tech_pvt = i;
00237       ast_copy_string(tmp->context, context, sizeof(tmp->context));
00238       ast_copy_string(tmp->exten, "s",  sizeof(tmp->exten));
00239       ast_string_field_set(tmp, language, "");
00240       i->owner = tmp;
00241       i->u = ast_module_user_add(tmp);
00242       if (state != AST_STATE_DOWN) {
00243          if (ast_pbx_start(tmp)) {
00244             ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
00245             ast_hangup(tmp);
00246          }
00247       }
00248    } else
00249       ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
00250    return tmp;
00251 }
00252 
00253 
00254 static struct ast_channel *nbs_request(const char *type, int format, void *data, int *cause)
00255 {
00256    int oldformat;
00257    struct nbs_pvt *p;
00258    struct ast_channel *tmp = NULL;
00259    
00260    oldformat = format;
00261    format &= (AST_FORMAT_SLINEAR);
00262    if (!format) {
00263       ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", oldformat);
00264       return NULL;
00265    }
00266    p = nbs_alloc(data);
00267    if (p) {
00268       tmp = nbs_new(p, AST_STATE_DOWN);
00269       if (!tmp)
00270          nbs_destroy(p);
00271    }
00272    return tmp;
00273 }
00274 
00275 static int unload_module(void)
00276 {
00277    /* First, take us out of the channel loop */
00278    ast_channel_unregister(&nbs_tech);
00279    return 0;
00280 }
00281 
00282 static int load_module(void)
00283 {
00284    /* Make sure we can register our channel type */
00285    if (ast_channel_register(&nbs_tech)) {
00286       ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
00287       return -1;
00288    }
00289    return 0;
00290 }
00291 
00292 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Network Broadcast Sound Support");

Generated on Wed Oct 28 11:45:33 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.6