Sat Mar 10 01:54:34 2012

Asterisk developer's documentation


app_nbscat.c File Reference

Silly application to play an NBScat file -- uses nbscat8k. More...

#include "asterisk.h"
#include <fcntl.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <signal.h>
#include "asterisk/lock.h"
#include "asterisk/file.h"
#include "asterisk/channel.h"
#include "asterisk/frame.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
#include "asterisk/translate.h"
#include "asterisk/app.h"

Go to the source code of this file.

Defines

#define AF_LOCAL   AF_UNIX
#define LOCAL_NBSCAT   "/usr/local/bin/nbscat8k"
#define NBSCAT   "/usr/bin/nbscat8k"

Functions

static void __reg_module (void)
static void __unreg_module (void)
static int load_module (void)
static int NBScat_exec (struct ast_channel *chan, const char *data)
static int NBScatplay (int fd)
static int timed_read (int fd, void *data, int datalen)
static int unload_module (void)

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Silly NBS Stream Application" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "88eaa8f5c1bd988bedd71113385e0886" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, }
static char * app = "NBScat"
static struct ast_module_infoast_module_info = &__mod_info


Detailed Description

Silly application to play an NBScat file -- uses nbscat8k.

Author:
Mark Spencer <markster@digium.com>

Definition in file app_nbscat.c.


Define Documentation

#define AF_LOCAL   AF_UNIX

Definition at line 67 of file app_nbscat.c.

Referenced by ast_makesocket(), ast_tryconnect(), listener(), and NBScat_exec().

#define LOCAL_NBSCAT   "/usr/local/bin/nbscat8k"

Definition at line 63 of file app_nbscat.c.

Referenced by NBScatplay().

#define NBSCAT   "/usr/bin/nbscat8k"

Definition at line 64 of file app_nbscat.c.

Referenced by NBScatplay().


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 220 of file app_nbscat.c.

static void __unreg_module ( void   )  [static]

Definition at line 220 of file app_nbscat.c.

static int load_module ( void   )  [static]

Definition at line 215 of file app_nbscat.c.

References ast_register_application_xml, and NBScat_exec().

00216 {
00217    return ast_register_application_xml(app, NBScat_exec);
00218 }

static int NBScat_exec ( struct ast_channel chan,
const char *  data 
) [static]

Definition at line 112 of file app_nbscat.c.

References AF_LOCAL, ast_debug, AST_FORMAT_SLINEAR, AST_FRAME_DTMF, AST_FRAME_VOICE, ast_frfree, AST_FRIENDLY_OFFSET, ast_log(), ast_read(), ast_samp2tv(), ast_set_write_format(), ast_stopstream(), ast_tvadd(), ast_tvdiff_ms(), ast_tvnow(), ast_waitfor(), ast_write(), f, LOG_WARNING, NBScatplay(), ast_frame::offset, timed_read(), and ast_channel::writeformat.

Referenced by load_module().

00113 {
00114    int res=0;
00115    int fds[2];
00116    int ms = -1;
00117    int pid = -1;
00118    int owriteformat;
00119    struct timeval next;
00120    struct ast_frame *f;
00121    struct myframe {
00122       struct ast_frame f;
00123       char offset[AST_FRIENDLY_OFFSET];
00124       short frdata[160];
00125    } myf;
00126    
00127    if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds)) {
00128       ast_log(LOG_WARNING, "Unable to create socketpair\n");
00129       return -1;
00130    }
00131    
00132    ast_stopstream(chan);
00133 
00134    owriteformat = chan->writeformat;
00135    res = ast_set_write_format(chan, AST_FORMAT_SLINEAR);
00136    if (res < 0) {
00137       ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
00138       return -1;
00139    }
00140    
00141    res = NBScatplay(fds[1]);
00142    /* Wait 1000 ms first */
00143    next = ast_tvnow();
00144    next.tv_sec += 1;
00145    if (res >= 0) {
00146       pid = res;
00147       /* Order is important -- there's almost always going to be mp3...  we want to prioritize the
00148          user */
00149       for (;;) {
00150          ms = ast_tvdiff_ms(next, ast_tvnow());
00151          if (ms <= 0) {
00152             res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata));
00153             if (res > 0) {
00154                myf.f.frametype = AST_FRAME_VOICE;
00155                myf.f.subclass.codec = AST_FORMAT_SLINEAR;
00156                myf.f.datalen = res;
00157                myf.f.samples = res / 2;
00158                myf.f.mallocd = 0;
00159                myf.f.offset = AST_FRIENDLY_OFFSET;
00160                myf.f.src = __PRETTY_FUNCTION__;
00161                myf.f.delivery.tv_sec = 0;
00162                myf.f.delivery.tv_usec = 0;
00163                myf.f.data.ptr = myf.frdata;
00164                if (ast_write(chan, &myf.f) < 0) {
00165                   res = -1;
00166                   break;
00167                }
00168             } else {
00169                ast_debug(1, "No more mp3\n");
00170                res = 0;
00171                break;
00172             }
00173             next = ast_tvadd(next, ast_samp2tv(myf.f.samples, 8000));
00174          } else {
00175             ms = ast_waitfor(chan, ms);
00176             if (ms < 0) {
00177                ast_debug(1, "Hangup detected\n");
00178                res = -1;
00179                break;
00180             }
00181             if (ms) {
00182                f = ast_read(chan);
00183                if (!f) {
00184                   ast_debug(1, "Null frame == hangup() detected\n");
00185                   res = -1;
00186                   break;
00187                }
00188                if (f->frametype == AST_FRAME_DTMF) {
00189                   ast_debug(1, "User pressed a key\n");
00190                   ast_frfree(f);
00191                   res = 0;
00192                   break;
00193                }
00194                ast_frfree(f);
00195             } 
00196          }
00197       }
00198    }
00199    close(fds[0]);
00200    close(fds[1]);
00201    
00202    if (pid > -1)
00203       kill(pid, SIGKILL);
00204    if (!res && owriteformat)
00205       ast_set_write_format(chan, owriteformat);
00206 
00207    return res;
00208 }

static int NBScatplay ( int  fd  )  [static]

Definition at line 72 of file app_nbscat.c.

References ast_close_fds_above_n(), ast_log(), ast_opt_high_priority, ast_safe_fork(), ast_set_priority(), LOCAL_NBSCAT, LOG_WARNING, and NBSCAT.

Referenced by NBScat_exec().

00073 {
00074    int res;
00075 
00076    res = ast_safe_fork(0);
00077    if (res < 0) {
00078       ast_log(LOG_WARNING, "Fork failed\n");
00079    }
00080 
00081    if (res) {
00082       return res;
00083    }
00084 
00085    if (ast_opt_high_priority)
00086       ast_set_priority(0);
00087 
00088    dup2(fd, STDOUT_FILENO);
00089    ast_close_fds_above_n(STDERR_FILENO);
00090    /* Most commonly installed in /usr/local/bin */
00091    execl(NBSCAT, "nbscat8k", "-d", (char *)NULL);
00092    execl(LOCAL_NBSCAT, "nbscat8k", "-d", (char *)NULL);
00093    fprintf(stderr, "Execute of nbscat8k failed\n");
00094    _exit(0);
00095 }

static int timed_read ( int  fd,
void *  data,
int  datalen 
) [static]

Definition at line 97 of file app_nbscat.c.

References ast_log(), ast_poll, and LOG_NOTICE.

00098 {
00099    int res;
00100    struct pollfd fds[1];
00101    fds[0].fd = fd;
00102    fds[0].events = POLLIN;
00103    res = ast_poll(fds, 1, 2000);
00104    if (res < 1) {
00105       ast_log(LOG_NOTICE, "Selected timed out/errored out with %d\n", res);
00106       return -1;
00107    }
00108    return read(fd, data, datalen);
00109    
00110 }

static int unload_module ( void   )  [static]

Definition at line 210 of file app_nbscat.c.

References ast_unregister_application().

00211 {
00212    return ast_unregister_application(app);
00213 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Silly NBS Stream Application" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "88eaa8f5c1bd988bedd71113385e0886" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, } [static]

Definition at line 220 of file app_nbscat.c.

char* app = "NBScat" [static]

Definition at line 70 of file app_nbscat.c.

struct ast_module_info* ast_module_info = &__mod_info [static]

Definition at line 220 of file app_nbscat.c.


Generated on Sat Mar 10 01:54:34 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7