app_mp3.c File Reference

Silly application to play an MP3 file -- uses mpg123. More...

#include "asterisk.h"
#include <sys/time.h>
#include <signal.h>
#include "asterisk/lock.h"
#include "asterisk/file.h"
#include "asterisk/channel.h"
#include "asterisk/frame.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
#include "asterisk/translate.h"
#include "asterisk/app.h"
#include "asterisk/format_cache.h"

Include dependency graph for app_mp3.c:

Go to the source code of this file.

Defines

#define LOCAL_MPG_123   "/usr/local/bin/mpg123"
#define MPG_123   "/usr/bin/mpg123"

Functions

 AST_MODULE_INFO_STANDARD_EXTENDED (ASTERISK_GPL_KEY,"Silly MP3 Application")
static int load_module (void)
static int mp3_exec (struct ast_channel *chan, const char *data)
static int mp3play (const char *filename, int fd)
static int timed_read (int fd, void *data, int datalen, int timeout)
static int unload_module (void)

Variables

static char * app = "MP3Player"


Detailed Description

Silly application to play an MP3 file -- uses mpg123.

Author:
Mark Spencer <markster@digium.com>
Note:
Add feature to play local M3U playlist file Vincent Li <mchun.li@gmail.com>

Definition in file app_mp3.c.


Define Documentation

#define LOCAL_MPG_123   "/usr/local/bin/mpg123"

Definition at line 52 of file app_mp3.c.

Referenced by mp3play(), and spawn_mp3().

#define MPG_123   "/usr/bin/mpg123"

Definition at line 53 of file app_mp3.c.

Referenced by mp3play(), and spawn_mp3().


Function Documentation

AST_MODULE_INFO_STANDARD_EXTENDED ( ASTERISK_GPL_KEY  ,
"Silly MP3 Application"   
)

static int load_module ( void   )  [static]

Definition at line 258 of file app_mp3.c.

References ast_register_application_xml, and mp3_exec().

00259 {
00260    return ast_register_application_xml(app, mp3_exec);
00261 }

static int mp3_exec ( struct ast_channel chan,
const char *  data 
) [static]

Definition at line 141 of file app_mp3.c.

References ao2_bump, ao2_cleanup, ast_channel_writeformat(), ast_debug, ast_format_slin, AST_FRAME_DTMF, AST_FRAME_VOICE, ast_frfree, AST_FRIENDLY_OFFSET, ast_log, ast_read(), ast_samp2tv(), ast_set_write_format(), ast_stopstream(), ast_strlen_zero, ast_tvadd(), ast_tvdiff_ms(), ast_tvnow(), ast_waitfor(), ast_write(), f, ast_frame::frametype, LOG_WARNING, mp3play(), NULL, ast_frame::offset, RAII_VAR, timed_read(), and timeout.

Referenced by load_module().

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 }

static int mp3play ( const char *  filename,
int  fd 
) [static]

Definition at line 80 of file app_mp3.c.

References ast_close_fds_above_n(), ast_log, ast_opt_high_priority, ast_safe_fork(), ast_set_priority(), LOCAL_MPG_123, LOG_WARNING, MPG_123, and NULL.

Referenced by mp3_exec().

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 }

static int timed_read ( int  fd,
void *  data,
int  datalen,
int  timeout 
) [static]

Definition at line 126 of file app_mp3.c.

References ast_log, ast_poll, and LOG_NOTICE.

Referenced by mp3_exec(), and NBScat_exec().

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 }

static int unload_module ( void   )  [static]

Definition at line 253 of file app_mp3.c.

References ast_unregister_application().

00254 {
00255    return ast_unregister_application(app);
00256 }


Variable Documentation

char* app = "MP3Player" [static]

Definition at line 78 of file app_mp3.c.


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