app_nbscat.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, 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 Silly application to play an NBScat file -- uses nbscat8k
00022  *
00023  * \author Mark Spencer <markster@digium.com>
00024  *  
00025  * \ingroup applications
00026  */
00027 
00028 /*** MODULEINFO
00029    <support_level>extended</support_level>
00030  ***/
00031  
00032 #include "asterisk.h"
00033 
00034 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 419592 $")
00035 
00036 #include <fcntl.h>
00037 #include <sys/time.h>
00038 #include <sys/socket.h>
00039 #include <signal.h>
00040 
00041 #include "asterisk/lock.h"
00042 #include "asterisk/file.h"
00043 #include "asterisk/channel.h"
00044 #include "asterisk/frame.h"
00045 #include "asterisk/pbx.h"
00046 #include "asterisk/module.h"
00047 #include "asterisk/translate.h"
00048 #include "asterisk/app.h"
00049 #include "asterisk/format_cache.h"
00050 
00051 /*** DOCUMENTATION
00052    <application name="NBScat" language="en_US">
00053       <synopsis>
00054          Play an NBS local stream.
00055       </synopsis>
00056       <syntax />
00057       <description>
00058          <para>Executes nbscat to listen to the local NBS stream.
00059          User can exit by pressing any key.</para>
00060       </description>
00061    </application>
00062  ***/
00063 
00064 #define LOCAL_NBSCAT "/usr/local/bin/nbscat8k"
00065 #define NBSCAT "/usr/bin/nbscat8k"
00066 
00067 #ifndef AF_LOCAL
00068 #define AF_LOCAL AF_UNIX
00069 #endif
00070 
00071 static char *app = "NBScat";
00072 
00073 static int NBScatplay(int fd)
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 }
00097 
00098 static int timed_read(int fd, void *data, int datalen)
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 }
00112 
00113 static int NBScat_exec(struct ast_channel *chan, const char *data)
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 }
00214 
00215 static int unload_module(void)
00216 {
00217    return ast_unregister_application(app);
00218 }
00219 
00220 static int load_module(void)
00221 {
00222    return ast_register_application_xml(app, NBScat_exec);
00223 }
00224 
00225 AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Silly NBS Stream Application");
00226 

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