Sat Apr 26 2014 22:01:27

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