Sat Mar 24 23:25:59 2007

Asterisk developer's documentation


app_mp3.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 MP3 file -- uses mpg123
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 
00034 #include "asterisk.h"
00035 
00036 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 7221 $")
00037 
00038 #include "asterisk/lock.h"
00039 #include "asterisk/file.h"
00040 #include "asterisk/logger.h"
00041 #include "asterisk/channel.h"
00042 #include "asterisk/frame.h"
00043 #include "asterisk/pbx.h"
00044 #include "asterisk/module.h"
00045 #include "asterisk/translate.h"
00046 
00047 #define LOCAL_MPG_123 "/usr/local/bin/mpg123"
00048 #define MPG_123 "/usr/bin/mpg123"
00049 
00050 static char *tdesc = "Silly MP3 Application";
00051 
00052 static char *app = "MP3Player";
00053 
00054 static char *synopsis = "Play an MP3 file or stream";
00055 
00056 static char *descrip = 
00057 "  MP3Player(location) Executes mpg123 to play the given location,\n"
00058 "which typically would be a filename or a URL. User can exit by pressing\n"
00059 "any key on the dialpad, or by hanging up."; 
00060 
00061 STANDARD_LOCAL_USER;
00062 
00063 LOCAL_USER_DECL;
00064 
00065 static int mp3play(char *filename, int fd)
00066 {
00067    int res;
00068    int x;
00069    res = fork();
00070    if (res < 0) 
00071       ast_log(LOG_WARNING, "Fork failed\n");
00072    if (res)
00073       return res;
00074    dup2(fd, STDOUT_FILENO);
00075    for (x=0;x<256;x++) {
00076       if (x != STDOUT_FILENO)
00077          close(x);
00078    }
00079    /* Execute mpg123, but buffer if it's a net connection */
00080    if (!strncasecmp(filename, "http://", 7)) {
00081       /* Most commonly installed in /usr/local/bin */
00082        execl(LOCAL_MPG_123, "mpg123", "-q", "-s", "-b", "1024", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
00083       /* But many places has it in /usr/bin */
00084        execl(MPG_123, "mpg123", "-q", "-s", "-b", "1024","-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
00085       /* As a last-ditch effort, try to use PATH */
00086        execlp("mpg123", "mpg123", "-q", "-s", "-b", "1024",  "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
00087    }
00088    else {
00089       /* Most commonly installed in /usr/local/bin */
00090        execl(MPG_123, "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
00091       /* But many places has it in /usr/bin */
00092        execl(LOCAL_MPG_123, "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
00093       /* As a last-ditch effort, try to use PATH */
00094        execlp("mpg123", "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
00095    }
00096    ast_log(LOG_WARNING, "Execute of mpg123 failed\n");
00097    return -1;
00098 }
00099 
00100 static int timed_read(int fd, void *data, int datalen, int timeout)
00101 {
00102    int res;
00103    struct pollfd fds[1];
00104    fds[0].fd = fd;
00105    fds[0].events = POLLIN;
00106    res = poll(fds, 1, timeout);
00107    if (res < 1) {
00108       ast_log(LOG_NOTICE, "Poll timed out/errored out with %d\n", res);
00109       return -1;
00110    }
00111    return read(fd, data, datalen);
00112    
00113 }
00114 
00115 static int mp3_exec(struct ast_channel *chan, void *data)
00116 {
00117    int res=0;
00118    struct localuser *u;
00119    int fds[2];
00120    int ms = -1;
00121    int pid = -1;
00122    int owriteformat;
00123    int timeout = 2000;
00124    struct timeval next;
00125    struct ast_frame *f;
00126    struct myframe {
00127       struct ast_frame f;
00128       char offset[AST_FRIENDLY_OFFSET];
00129       short frdata[160];
00130    } myf;
00131    
00132    if (ast_strlen_zero(data)) {
00133       ast_log(LOG_WARNING, "MP3 Playback requires an argument (filename)\n");
00134       return -1;
00135    }
00136 
00137    LOCAL_USER_ADD(u);
00138 
00139    if (pipe(fds)) {
00140       ast_log(LOG_WARNING, "Unable to create pipe\n");
00141       LOCAL_USER_REMOVE(u);
00142       return -1;
00143    }
00144    
00145    ast_stopstream(chan);
00146 
00147    owriteformat = chan->writeformat;
00148    res = ast_set_write_format(chan, AST_FORMAT_SLINEAR);
00149    if (res < 0) {
00150       ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
00151       LOCAL_USER_REMOVE(u);
00152       return -1;
00153    }
00154    
00155    res = mp3play((char *)data, fds[1]);
00156    if (!strncasecmp((char *)data, "http://", 7)) {
00157       timeout = 10000;
00158    }
00159    /* Wait 1000 ms first */
00160    next = ast_tvnow();
00161    next.tv_sec += 1;
00162    if (res >= 0) {
00163       pid = res;
00164       /* Order is important -- there's almost always going to be mp3...  we want to prioritize the
00165          user */
00166       for (;;) {
00167          ms = ast_tvdiff_ms(next, ast_tvnow());
00168          if (ms <= 0) {
00169             res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata), timeout);
00170             if (res > 0) {
00171                myf.f.frametype = AST_FRAME_VOICE;
00172                myf.f.subclass = AST_FORMAT_SLINEAR;
00173                myf.f.datalen = res;
00174                myf.f.samples = res / 2;
00175                myf.f.mallocd = 0;
00176                myf.f.offset = AST_FRIENDLY_OFFSET;
00177                myf.f.src = __PRETTY_FUNCTION__;
00178                myf.f.delivery.tv_sec = 0;
00179                myf.f.delivery.tv_usec = 0;
00180                myf.f.data = myf.frdata;
00181                if (ast_write(chan, &myf.f) < 0) {
00182                   res = -1;
00183                   break;
00184                }
00185             } else {
00186                ast_log(LOG_DEBUG, "No more mp3\n");
00187                res = 0;
00188                break;
00189             }
00190             next = ast_tvadd(next, ast_samp2tv(myf.f.samples, 8000));
00191          } else {
00192             ms = ast_waitfor(chan, ms);
00193             if (ms < 0) {
00194                ast_log(LOG_DEBUG, "Hangup detected\n");
00195                res = -1;
00196                break;
00197             }
00198             if (ms) {
00199                f = ast_read(chan);
00200                if (!f) {
00201                   ast_log(LOG_DEBUG, "Null frame == hangup() detected\n");
00202                   res = -1;
00203                   break;
00204                }
00205                if (f->frametype == AST_FRAME_DTMF) {
00206                   ast_log(LOG_DEBUG, "User pressed a key\n");
00207                   ast_frfree(f);
00208                   res = 0;
00209                   break;
00210                }
00211                ast_frfree(f);
00212             } 
00213          }
00214       }
00215    }
00216    close(fds[0]);
00217    close(fds[1]);
00218    
00219    if (pid > -1)
00220       kill(pid, SIGKILL);
00221    if (!res && owriteformat)
00222       ast_set_write_format(chan, owriteformat);
00223 
00224    LOCAL_USER_REMOVE(u);
00225    
00226    return res;
00227 }
00228 
00229 int unload_module(void)
00230 {
00231    int res;
00232 
00233    res = ast_unregister_application(app);
00234 
00235    STANDARD_HANGUP_LOCALUSERS;
00236    
00237    return res;
00238 }
00239 
00240 int load_module(void)
00241 {
00242    return ast_register_application(app, mp3_exec, synopsis, descrip);
00243 }
00244 
00245 char *description(void)
00246 {
00247    return tdesc;
00248 }
00249 
00250 int usecount(void)
00251 {
00252    int res;
00253    STANDARD_USECOUNT(res);
00254    return res;
00255 }
00256 
00257 char *key()
00258 {
00259    return ASTERISK_GPL_KEY;
00260 }

Generated on Sat Mar 24 23:25:59 2007 for Asterisk - the Open Source PBX by  doxygen 1.4.6