2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Silly application to play an MP3 file -- uses mpg123
23 * \author Mark Spencer <markster@digium.com>
25 * \ingroup applications
30 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
35 #include "asterisk/lock.h"
36 #include "asterisk/file.h"
37 #include "asterisk/channel.h"
38 #include "asterisk/frame.h"
39 #include "asterisk/pbx.h"
40 #include "asterisk/module.h"
41 #include "asterisk/translate.h"
42 #include "asterisk/app.h"
44 #define LOCAL_MPG_123 "/usr/local/bin/mpg123"
45 #define MPG_123 "/usr/bin/mpg123"
47 static char *app
= "MP3Player";
49 static char *synopsis
= "Play an MP3 file or stream";
51 static char *descrip
=
52 " MP3Player(location): Executes mpg123 to play the given location,\n"
53 "which typically would be a filename or a URL. User can exit by pressing\n"
54 "any key on the dialpad, or by hanging up.";
57 static int mp3play(char *filename
, int fd
)
61 res
= ast_safe_fork(0);
63 ast_log(LOG_WARNING
, "Fork failed\n");
67 if (ast_opt_high_priority
)
70 dup2(fd
, STDOUT_FILENO
);
71 ast_close_fds_above_n(STDERR_FILENO
);
73 /* Execute mpg123, but buffer if it's a net connection */
74 if (!strncasecmp(filename
, "http://", 7)) {
75 /* Most commonly installed in /usr/local/bin */
76 execl(LOCAL_MPG_123
, "mpg123", "-q", "-s", "-b", "1024", "-f", "8192", "--mono", "-r", "8000", filename
, (char *)NULL
);
77 /* But many places has it in /usr/bin */
78 execl(MPG_123
, "mpg123", "-q", "-s", "-b", "1024","-f", "8192", "--mono", "-r", "8000", filename
, (char *)NULL
);
79 /* As a last-ditch effort, try to use PATH */
80 execlp("mpg123", "mpg123", "-q", "-s", "-b", "1024", "-f", "8192", "--mono", "-r", "8000", filename
, (char *)NULL
);
83 /* Most commonly installed in /usr/local/bin */
84 execl(MPG_123
, "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename
, (char *)NULL
);
85 /* But many places has it in /usr/bin */
86 execl(LOCAL_MPG_123
, "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename
, (char *)NULL
);
87 /* As a last-ditch effort, try to use PATH */
88 execlp("mpg123", "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename
, (char *)NULL
);
90 /* Can't use ast_log since FD's are closed */
91 fprintf(stderr
, "Execute of mpg123 failed\n");
95 static int timed_read(int fd
, void *data
, int datalen
, int timeout
)
100 fds
[0].events
= POLLIN
;
101 res
= poll(fds
, 1, timeout
);
103 ast_log(LOG_NOTICE
, "Poll timed out/errored out with %d\n", res
);
106 return read(fd
, data
, datalen
);
110 static int mp3_exec(struct ast_channel
*chan
, void *data
)
122 char offset
[AST_FRIENDLY_OFFSET
];
126 if (ast_strlen_zero(data
)) {
127 ast_log(LOG_WARNING
, "MP3 Playback requires an argument (filename)\n");
132 ast_log(LOG_WARNING
, "Unable to create pipe\n");
136 ast_stopstream(chan
);
138 owriteformat
= chan
->writeformat
;
139 res
= ast_set_write_format(chan
, AST_FORMAT_SLINEAR
);
141 ast_log(LOG_WARNING
, "Unable to set write format to signed linear\n");
145 res
= mp3play((char *)data
, fds
[1]);
146 if (!strncasecmp((char *)data
, "http://", 7)) {
149 /* Wait 1000 ms first */
154 /* Order is important -- there's almost always going to be mp3... we want to prioritize the
157 ms
= ast_tvdiff_ms(next
, ast_tvnow());
159 res
= timed_read(fds
[0], myf
.frdata
, sizeof(myf
.frdata
), timeout
);
161 myf
.f
.frametype
= AST_FRAME_VOICE
;
162 myf
.f
.subclass
= AST_FORMAT_SLINEAR
;
164 myf
.f
.samples
= res
/ 2;
166 myf
.f
.offset
= AST_FRIENDLY_OFFSET
;
167 myf
.f
.src
= __PRETTY_FUNCTION__
;
168 myf
.f
.delivery
.tv_sec
= 0;
169 myf
.f
.delivery
.tv_usec
= 0;
170 myf
.f
.data
.ptr
= myf
.frdata
;
171 if (ast_write(chan
, &myf
.f
) < 0) {
176 ast_debug(1, "No more mp3\n");
180 next
= ast_tvadd(next
, ast_samp2tv(myf
.f
.samples
, 8000));
182 ms
= ast_waitfor(chan
, ms
);
184 ast_debug(1, "Hangup detected\n");
191 ast_debug(1, "Null frame == hangup() detected\n");
195 if (f
->frametype
== AST_FRAME_DTMF
) {
196 ast_debug(1, "User pressed a key\n");
211 if (!res
&& owriteformat
)
212 ast_set_write_format(chan
, owriteformat
);
217 static int unload_module(void)
219 return ast_unregister_application(app
);
222 static int load_module(void)
224 return ast_register_application(app
, mp3_exec
, synopsis
, descrip
);
227 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY
, "Silly MP3 Application");