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"
#include "asterisk/format_cache.h"

Include dependency graph for app_nbscat.c:

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

 AST_MODULE_INFO_STANDARD_EXTENDED (ASTERISK_GPL_KEY,"Silly NBS Stream Application")
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 char * app = "NBScat"


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

Referenced by NBScatplay().

#define NBSCAT   "/usr/bin/nbscat8k"

Definition at line 65 of file app_nbscat.c.

Referenced by NBScatplay().


Function Documentation

AST_MODULE_INFO_STANDARD_EXTENDED ( ASTERISK_GPL_KEY  ,
"Silly NBS Stream Application"   
)

static int load_module ( void   )  [static]

Definition at line 220 of file app_nbscat.c.

References ast_register_application_xml, and NBScat_exec().

00221 {
00222    return ast_register_application_xml(app, NBScat_exec);
00223 }

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

Definition at line 113 of file app_nbscat.c.

References AF_LOCAL, ao2_bump, ao2_cleanup, ast_channel_writeformat(), ast_debug, ast_format_slin, 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, ast_frame::frametype, LOG_WARNING, NBScatplay(), ast_frame::offset, and timed_read().

Referenced by load_module().

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

static int NBScatplay ( int  fd  )  [static]

Definition at line 73 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, NBSCAT, and NULL.

Referenced by NBScat_exec().

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

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

Definition at line 98 of file app_nbscat.c.

References ast_log, ast_poll, and LOG_NOTICE.

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

static int unload_module ( void   )  [static]

Definition at line 215 of file app_nbscat.c.

References ast_unregister_application().

00216 {
00217    return ast_unregister_application(app);
00218 }


Variable Documentation

char* app = "NBScat" [static]

Definition at line 71 of file app_nbscat.c.


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