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  * \author Mark Spencer <markster@digium.com>
00024  *
00025  * \note Add feature to play local M3U playlist file
00026  * Vincent Li <mchun.li@gmail.com>
00027  * 
00028  * \ingroup applications
00029  */
00030 
00031 /*** MODULEINFO
00032    <support_level>extended</support_level>
00033  ***/
00034  
00035 #include "asterisk.h"
00036 
00037 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 419592 $")
00038 
00039 #include <sys/time.h>
00040 #include <signal.h>
00041 
00042 #include "asterisk/lock.h"
00043 #include "asterisk/file.h"
00044 #include "asterisk/channel.h"
00045 #include "asterisk/frame.h"
00046 #include "asterisk/pbx.h"
00047 #include "asterisk/module.h"
00048 #include "asterisk/translate.h"
00049 #include "asterisk/app.h"
00050 #include "asterisk/format_cache.h"
00051 
00052 #define LOCAL_MPG_123 "/usr/local/bin/mpg123"
00053 #define MPG_123 "/usr/bin/mpg123"
00054 
00055 /*** DOCUMENTATION
00056    <application name="MP3Player" language="en_US">
00057       <synopsis>
00058          Play an MP3 file or M3U playlist file or stream.
00059       </synopsis>
00060       <syntax>
00061          <parameter name="Location" required="true">
00062             <para>Location of the file to be played.
00063             (argument passed to mpg123)</para>
00064          </parameter>
00065       </syntax>
00066       <description>
00067          <para>Executes mpg123 to play the given location, which typically would be a mp3 filename
00068          or m3u playlist filename or a URL. Please read http://en.wikipedia.org/wiki/M3U
00069          to see how M3U playlist file format is like, Example usage would be
00070          exten => 1234,1,MP3Player(/var/lib/asterisk/playlist.m3u)
00071          User can exit by pressing any key on the dialpad, or by hanging up.</para>
00072          <para>This application does not automatically answer and should be preceeded by an
00073          application such as Answer() or Progress().</para>
00074       </description>
00075    </application>
00076 
00077  ***/
00078 static char *app = "MP3Player";
00079 
00080 static int mp3play(const char *filename, int fd)
00081 {
00082    int res;
00083 
00084    res = ast_safe_fork(0);
00085    if (res < 0) 
00086       ast_log(LOG_WARNING, "Fork failed\n");
00087    if (res) {
00088       return res;
00089    }
00090    if (ast_opt_high_priority)
00091       ast_set_priority(0);
00092 
00093    dup2(fd, STDOUT_FILENO);
00094    ast_close_fds_above_n(STDERR_FILENO);
00095 
00096    /* Execute mpg123, but buffer if it's a net connection */
00097    if (!strncasecmp(filename, "http://", 7)) {
00098       /* Most commonly installed in /usr/local/bin */
00099        execl(LOCAL_MPG_123, "mpg123", "-q", "-s", "-b", "1024", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
00100       /* But many places has it in /usr/bin */
00101        execl(MPG_123, "mpg123", "-q", "-s", "-b", "1024","-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
00102       /* As a last-ditch effort, try to use PATH */
00103        execlp("mpg123", "mpg123", "-q", "-s", "-b", "1024",  "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
00104    }
00105    else if (strstr(filename, ".m3u")) {
00106       /* Most commonly installed in /usr/local/bin */
00107        execl(LOCAL_MPG_123, "mpg123", "-q", "-z", "-s", "-b", "1024", "-f", "8192", "--mono", "-r", "8000", "-@", filename, (char *)NULL);
00108       /* But many places has it in /usr/bin */
00109        execl(MPG_123, "mpg123", "-q", "-z", "-s", "-b", "1024","-f", "8192", "--mono", "-r", "8000", "-@", filename, (char *)NULL);
00110       /* As a last-ditch effort, try to use PATH */
00111        execlp("mpg123", "mpg123", "-q", "-z", "-s", "-b", "1024",  "-f", "8192", "--mono", "-r", "8000", "-@", filename, (char *)NULL);
00112    }
00113    else {
00114       /* Most commonly installed in /usr/local/bin */
00115        execl(MPG_123, "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
00116       /* But many places has it in /usr/bin */
00117        execl(LOCAL_MPG_123, "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
00118       /* As a last-ditch effort, try to use PATH */
00119        execlp("mpg123", "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
00120    }
00121    /* Can't use ast_log since FD's are closed */
00122    fprintf(stderr, "Execute of mpg123 failed\n");
00123    _exit(0);
00124 }
00125 
00126 static int timed_read(int fd, void *data, int datalen, int timeout)
00127 {
00128    int res;
00129    struct pollfd fds[1];
00130    fds[0].fd = fd;
00131    fds[0].events = POLLIN;
00132    res = ast_poll(fds, 1, timeout);
00133    if (res < 1) {
00134       ast_log(LOG_NOTICE, "Poll timed out/errored out with %d\n", res);
00135       return -1;
00136    }
00137    return read(fd, data, datalen);
00138    
00139 }
00140 
00141 static int mp3_exec(struct ast_channel *chan, const char *data)
00142 {
00143    int res=0;
00144    int fds[2];
00145    int ms = -1;
00146    int pid = -1;
00147    RAII_VAR(struct ast_format *, owriteformat, NULL, ao2_cleanup);
00148    int timeout = 2000;
00149    struct timeval next;
00150    struct ast_frame *f;
00151    struct myframe {
00152       struct ast_frame f;
00153       char offset[AST_FRIENDLY_OFFSET];
00154       short frdata[160];
00155    } myf = {
00156       .f = { 0, },
00157    };
00158 
00159    if (ast_strlen_zero(data)) {
00160       ast_log(LOG_WARNING, "MP3 Playback requires an argument (filename)\n");
00161       return -1;
00162    }
00163 
00164    if (pipe(fds)) {
00165       ast_log(LOG_WARNING, "Unable to create pipe\n");
00166       return -1;
00167    }
00168    
00169    ast_stopstream(chan);
00170 
00171    owriteformat = ao2_bump(ast_channel_writeformat(chan));
00172    res = ast_set_write_format(chan, ast_format_slin);
00173    if (res < 0) {
00174       ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
00175       return -1;
00176    }
00177 
00178    myf.f.frametype = AST_FRAME_VOICE;
00179    myf.f.subclass.format = ast_format_slin;
00180    myf.f.mallocd = 0;
00181    myf.f.offset = AST_FRIENDLY_OFFSET;
00182    myf.f.src = __PRETTY_FUNCTION__;
00183    myf.f.delivery.tv_sec = 0;
00184    myf.f.delivery.tv_usec = 0;
00185    myf.f.data.ptr = myf.frdata;
00186    
00187    res = mp3play(data, fds[1]);
00188    if (!strncasecmp(data, "http://", 7)) {
00189       timeout = 10000;
00190    }
00191    /* Wait 1000 ms first */
00192    next = ast_tvnow();
00193    next.tv_sec += 1;
00194    if (res >= 0) {
00195       pid = res;
00196       /* Order is important -- there's almost always going to be mp3...  we want to prioritize the
00197          user */
00198       for (;;) {
00199          ms = ast_tvdiff_ms(next, ast_tvnow());
00200          if (ms <= 0) {
00201             res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata), timeout);
00202             if (res > 0) {
00203                myf.f.datalen = res;
00204                myf.f.samples = res / 2;
00205                if (ast_write(chan, &myf.f) < 0) {
00206                   res = -1;
00207                   break;
00208                }
00209             } else {
00210                ast_debug(1, "No more mp3\n");
00211                res = 0;
00212                break;
00213             }
00214             next = ast_tvadd(next, ast_samp2tv(myf.f.samples, 8000));
00215          } else {
00216             ms = ast_waitfor(chan, ms);
00217             if (ms < 0) {
00218                ast_debug(1, "Hangup detected\n");
00219                res = -1;
00220                break;
00221             }
00222             if (ms) {
00223                f = ast_read(chan);
00224                if (!f) {
00225                   ast_debug(1, "Null frame == hangup() detected\n");
00226                   res = -1;
00227                   break;
00228                }
00229                if (f->frametype == AST_FRAME_DTMF) {
00230                   ast_debug(1, "User pressed a key\n");
00231                   ast_frfree(f);
00232                   res = 0;
00233                   break;
00234                }
00235                ast_frfree(f);
00236             } 
00237          }
00238       }
00239    }
00240    close(fds[0]);
00241    close(fds[1]);
00242    
00243    if (pid > -1)
00244       kill(pid, SIGKILL);
00245    if (!res && owriteformat)
00246       ast_set_write_format(chan, owriteformat);
00247 
00248    ast_frfree(&myf.f);
00249    
00250    return res;
00251 }
00252 
00253 static int unload_module(void)
00254 {
00255    return ast_unregister_application(app);
00256 }
00257 
00258 static int load_module(void)
00259 {
00260    return ast_register_application_xml(app, mp3_exec);
00261 }
00262 
00263 AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Silly MP3 Application");
00264 

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