Fri May 26 01:45:27 2006

Asterisk developer's documentation


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  * \ingroup applications
00024  */
00025  
00026 #include <string.h>
00027 #include <stdio.h>
00028 #include <signal.h>
00029 #include <stdlib.h>
00030 #include <unistd.h>
00031 #include <fcntl.h>
00032 #include <sys/time.h>
00033 #include <sys/socket.h>
00034 
00035 #include "asterisk.h"
00036 
00037 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 7221 $")
00038 
00039 #include "asterisk/lock.h"
00040 #include "asterisk/file.h"
00041 #include "asterisk/logger.h"
00042 #include "asterisk/channel.h"
00043 #include "asterisk/frame.h"
00044 #include "asterisk/pbx.h"
00045 #include "asterisk/module.h"
00046 #include "asterisk/translate.h"
00047 
00048 #define LOCAL_NBSCAT "/usr/local/bin/nbscat8k"
00049 #define NBSCAT "/usr/bin/nbscat8k"
00050 
00051 #ifndef AF_LOCAL
00052 #define AF_LOCAL AF_UNIX
00053 #endif
00054 
00055 static char *tdesc = "Silly NBS Stream Application";
00056 
00057 static char *app = "NBScat";
00058 
00059 static char *synopsis = "Play an NBS local stream";
00060 
00061 static char *descrip = 
00062 "  NBScat: Executes nbscat to listen to the local NBS stream.\n"
00063 "User can exit by pressing any key\n.";
00064 
00065 STANDARD_LOCAL_USER;
00066 
00067 LOCAL_USER_DECL;
00068 
00069 static int NBScatplay(int fd)
00070 {
00071    int res;
00072    int x;
00073    res = fork();
00074    if (res < 0) 
00075       ast_log(LOG_WARNING, "Fork failed\n");
00076    if (res)
00077       return res;
00078    dup2(fd, STDOUT_FILENO);
00079    for (x=0;x<256;x++) {
00080       if (x != STDOUT_FILENO)
00081          close(x);
00082    }
00083    /* Most commonly installed in /usr/local/bin */
00084    execl(NBSCAT, "nbscat8k", "-d", (char *)NULL);
00085    execl(LOCAL_NBSCAT, "nbscat8k", "-d", (char *)NULL);
00086    ast_log(LOG_WARNING, "Execute of nbscat8k failed\n");
00087    return -1;
00088 }
00089 
00090 static int timed_read(int fd, void *data, int datalen)
00091 {
00092    int res;
00093    struct pollfd fds[1];
00094    fds[0].fd = fd;
00095    fds[0].events = POLLIN;
00096    res = poll(fds, 1, 2000);
00097    if (res < 1) {
00098       ast_log(LOG_NOTICE, "Selected timed out/errored out with %d\n", res);
00099       return -1;
00100    }
00101    return read(fd, data, datalen);
00102    
00103 }
00104 
00105 static int NBScat_exec(struct ast_channel *chan, void *data)
00106 {
00107    int res=0;
00108    struct localuser *u;
00109    int fds[2];
00110    int ms = -1;
00111    int pid = -1;
00112    int owriteformat;
00113    struct timeval next;
00114    struct ast_frame *f;
00115    struct myframe {
00116       struct ast_frame f;
00117       char offset[AST_FRIENDLY_OFFSET];
00118       short frdata[160];
00119    } myf;
00120    
00121    LOCAL_USER_ADD(u);
00122 
00123    if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds)) {
00124       ast_log(LOG_WARNING, "Unable to create socketpair\n");
00125       LOCAL_USER_REMOVE(u);
00126       return -1;
00127    }
00128    
00129    ast_stopstream(chan);
00130 
00131    owriteformat = chan->writeformat;
00132    res = ast_set_write_format(chan, AST_FORMAT_SLINEAR);
00133    if (res < 0) {
00134       ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
00135       LOCAL_USER_REMOVE(u);
00136       return -1;
00137    }
00138    
00139    res = NBScatplay(fds[1]);
00140    /* Wait 1000 ms first */
00141    next = ast_tvnow();
00142    next.tv_sec += 1;
00143    if (res >= 0) {
00144       pid = res;
00145       /* Order is important -- there's almost always going to be mp3...  we want to prioritize the
00146          user */
00147       for (;;) {
00148          ms = ast_tvdiff_ms(next, ast_tvnow());
00149          if (ms <= 0) {
00150             res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata));
00151             if (res > 0) {
00152                myf.f.frametype = AST_FRAME_VOICE;
00153                myf.f.subclass = AST_FORMAT_SLINEAR;
00154                myf.f.datalen = res;
00155                myf.f.samples = res / 2;
00156                myf.f.mallocd = 0;
00157                myf.f.offset = AST_FRIENDLY_OFFSET;
00158                myf.f.src = __PRETTY_FUNCTION__;
00159                myf.f.delivery.tv_sec = 0;
00160                myf.f.delivery.tv_usec = 0;
00161                myf.f.data = myf.frdata;
00162                if (ast_write(chan, &myf.f) < 0) {
00163                   res = -1;
00164                   break;
00165                }
00166             } else {
00167                ast_log(LOG_DEBUG, "No more mp3\n");
00168                res = 0;
00169                break;
00170             }
00171             next = ast_tvadd(next, ast_samp2tv(myf.f.samples, 8000));
00172          } else {
00173             ms = ast_waitfor(chan, ms);
00174             if (ms < 0) {
00175                ast_log(LOG_DEBUG, "Hangup detected\n");
00176                res = -1;
00177                break;
00178             }
00179             if (ms) {
00180                f = ast_read(chan);
00181                if (!f) {
00182                   ast_log(LOG_DEBUG, "Null frame == hangup() detected\n");
00183                   res = -1;
00184                   break;
00185                }
00186                if (f->frametype == AST_FRAME_DTMF) {
00187                   ast_log(LOG_DEBUG, "User pressed a key\n");
00188                   ast_frfree(f);
00189                   res = 0;
00190                   break;
00191                }
00192                ast_frfree(f);
00193             } 
00194          }
00195       }
00196    }
00197    close(fds[0]);
00198    close(fds[1]);
00199    
00200    if (pid > -1)
00201       kill(pid, SIGKILL);
00202    if (!res && owriteformat)
00203       ast_set_write_format(chan, owriteformat);
00204 
00205    LOCAL_USER_REMOVE(u);
00206 
00207    return res;
00208 }
00209 
00210 int unload_module(void)
00211 {
00212    int res;
00213 
00214    res = ast_unregister_application(app);
00215 
00216    STANDARD_HANGUP_LOCALUSERS;
00217 
00218    return res;
00219 }
00220 
00221 int load_module(void)
00222 {
00223    return ast_register_application(app, NBScat_exec, synopsis, descrip);
00224 }
00225 
00226 char *description(void)
00227 {
00228    return tdesc;
00229 }
00230 
00231 int usecount(void)
00232 {
00233    int res;
00234    STANDARD_USECOUNT(res);
00235    return res;
00236 }
00237 
00238 char *key()
00239 {
00240    return ASTERISK_GPL_KEY;
00241 }

Generated on Fri May 26 01:45:27 2006 for Asterisk - the Open Source PBX by  doxygen 1.4.6