Wed Aug 18 22:33:59 2010

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, void *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_DEFAULT , .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 = "a9c98e5d177805051735cb5b0b16b0a0" , .load = load_module, .unload = unload_module, }
static char * app = "NBScat"
static struct ast_module_infoast_module_info = &__mod_info
static char * descrip
static char * synopsis = "Play an NBS local stream"


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 50 of file app_nbscat.c.

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

Definition at line 46 of file app_nbscat.c.

Referenced by NBScatplay().

#define NBSCAT   "/usr/bin/nbscat8k"

Definition at line 47 of file app_nbscat.c.

Referenced by NBScatplay().


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 210 of file app_nbscat.c.

static void __unreg_module ( void   )  [static]

Definition at line 210 of file app_nbscat.c.

static int load_module ( void   )  [static]

Definition at line 205 of file app_nbscat.c.

References ast_register_application, and NBScat_exec().

00206 {
00207    return ast_register_application(app, NBScat_exec, synopsis, descrip);
00208 }

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

Definition at line 102 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(), chan, f, LOG_WARNING, NBScatplay(), ast_frame::offset, timed_read(), and ast_channel::writeformat.

Referenced by load_module().

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

static int NBScatplay ( int  fd  )  [static]

Definition at line 62 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().

00063 {
00064    int res;
00065 
00066    res = ast_safe_fork(0);
00067    if (res < 0) {
00068       ast_log(LOG_WARNING, "Fork failed\n");
00069    }
00070 
00071    if (res) {
00072       return res;
00073    }
00074 
00075    if (ast_opt_high_priority)
00076       ast_set_priority(0);
00077 
00078    dup2(fd, STDOUT_FILENO);
00079    ast_close_fds_above_n(STDERR_FILENO);
00080    /* Most commonly installed in /usr/local/bin */
00081    execl(NBSCAT, "nbscat8k", "-d", (char *)NULL);
00082    execl(LOCAL_NBSCAT, "nbscat8k", "-d", (char *)NULL);
00083    fprintf(stderr, "Execute of nbscat8k failed\n");
00084    _exit(0);
00085 }

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

Definition at line 87 of file app_nbscat.c.

References ast_log(), ast_poll, and LOG_NOTICE.

00088 {
00089    int res;
00090    struct pollfd fds[1];
00091    fds[0].fd = fd;
00092    fds[0].events = POLLIN;
00093    res = ast_poll(fds, 1, 2000);
00094    if (res < 1) {
00095       ast_log(LOG_NOTICE, "Selected timed out/errored out with %d\n", res);
00096       return -1;
00097    }
00098    return read(fd, data, datalen);
00099    
00100 }

static int unload_module ( void   )  [static]

Definition at line 200 of file app_nbscat.c.

References ast_unregister_application().

00201 {
00202    return ast_unregister_application(app);
00203 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .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 = "a9c98e5d177805051735cb5b0b16b0a0" , .load = load_module, .unload = unload_module, } [static]

Definition at line 210 of file app_nbscat.c.

char* app = "NBScat" [static]

Definition at line 53 of file app_nbscat.c.

struct ast_module_info* ast_module_info = &__mod_info [static]

Definition at line 210 of file app_nbscat.c.

char* descrip [static]

Initial value:

 
"  NBScat(): Executes nbscat to listen to the local NBS stream.\n"
"User can exit by pressing any key.\n"

Definition at line 57 of file app_nbscat.c.

char* synopsis = "Play an NBS local stream" [static]

Definition at line 55 of file app_nbscat.c.


Generated on Wed Aug 18 22:33:59 2010 for Asterisk - the Open Source PBX by  doxygen 1.4.7