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$")
40 #include "asterisk/lock.h"
41 #include "asterisk/file.h"
42 #include "asterisk/logger.h"
43 #include "asterisk/channel.h"
44 #include "asterisk/frame.h"
45 #include "asterisk/pbx.h"
46 #include "asterisk/module.h"
47 #include "asterisk/translate.h"
48 #include "asterisk/options.h"
50 #define LOCAL_MPG_123 "/usr/local/bin/mpg123"
51 #define MPG_123 "/usr/bin/mpg123"
53 static char *app
= "MP3Player";
55 static char *synopsis
= "Play an MP3 file or stream";
57 static char *descrip
=
58 " MP3Player(location) Executes mpg123 to play the given location,\n"
59 "which typically would be a filename or a URL. User can exit by pressing\n"
60 "any key on the dialpad, or by hanging up.";
63 static int mp3play(char *filename
, int fd
)
67 sigset_t fullset
, oldset
;
70 pthread_sigmask(SIG_BLOCK
, &fullset
, &oldset
);
74 ast_log(LOG_WARNING
, "Fork failed\n");
76 pthread_sigmask(SIG_SETMASK
, &oldset
, NULL
);
79 if (ast_opt_high_priority
)
81 signal(SIGPIPE
, SIG_DFL
);
82 pthread_sigmask(SIG_UNBLOCK
, &fullset
, NULL
);
84 dup2(fd
, STDOUT_FILENO
);
85 for (x
=STDERR_FILENO
+ 1;x
<256;x
++) {
86 if (x
!= STDOUT_FILENO
)
89 /* Execute mpg123, but buffer if it's a net connection */
90 if (!strncasecmp(filename
, "http://", 7)) {
91 /* Most commonly installed in /usr/local/bin */
92 execl(LOCAL_MPG_123
, "mpg123", "-q", "-s", "-b", "1024", "-f", "8192", "--mono", "-r", "8000", filename
, (char *)NULL
);
93 /* But many places has it in /usr/bin */
94 execl(MPG_123
, "mpg123", "-q", "-s", "-b", "1024","-f", "8192", "--mono", "-r", "8000", filename
, (char *)NULL
);
95 /* As a last-ditch effort, try to use PATH */
96 execlp("mpg123", "mpg123", "-q", "-s", "-b", "1024", "-f", "8192", "--mono", "-r", "8000", filename
, (char *)NULL
);
99 /* Most commonly installed in /usr/local/bin */
100 execl(MPG_123
, "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename
, (char *)NULL
);
101 /* But many places has it in /usr/bin */
102 execl(LOCAL_MPG_123
, "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename
, (char *)NULL
);
103 /* As a last-ditch effort, try to use PATH */
104 execlp("mpg123", "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename
, (char *)NULL
);
106 ast_log(LOG_WARNING
, "Execute of mpg123 failed\n");
110 static int timed_read(int fd
, void *data
, int datalen
, int timeout
)
113 struct pollfd fds
[1];
115 fds
[0].events
= POLLIN
;
116 res
= poll(fds
, 1, timeout
);
118 ast_log(LOG_NOTICE
, "Poll timed out/errored out with %d\n", res
);
121 return read(fd
, data
, datalen
);
125 static int mp3_exec(struct ast_channel
*chan
, void *data
)
128 struct ast_module_user
*u
;
138 char offset
[AST_FRIENDLY_OFFSET
];
142 if (ast_strlen_zero(data
)) {
143 ast_log(LOG_WARNING
, "MP3 Playback requires an argument (filename)\n");
147 u
= ast_module_user_add(chan
);
150 ast_log(LOG_WARNING
, "Unable to create pipe\n");
151 ast_module_user_remove(u
);
155 ast_stopstream(chan
);
157 owriteformat
= chan
->writeformat
;
158 res
= ast_set_write_format(chan
, AST_FORMAT_SLINEAR
);
160 ast_log(LOG_WARNING
, "Unable to set write format to signed linear\n");
161 ast_module_user_remove(u
);
165 res
= mp3play((char *)data
, fds
[1]);
166 if (!strncasecmp((char *)data
, "http://", 7)) {
169 /* Wait 1000 ms first */
174 /* Order is important -- there's almost always going to be mp3... we want to prioritize the
177 ms
= ast_tvdiff_ms(next
, ast_tvnow());
179 res
= timed_read(fds
[0], myf
.frdata
, sizeof(myf
.frdata
), timeout
);
181 myf
.f
.frametype
= AST_FRAME_VOICE
;
182 myf
.f
.subclass
= AST_FORMAT_SLINEAR
;
184 myf
.f
.samples
= res
/ 2;
186 myf
.f
.offset
= AST_FRIENDLY_OFFSET
;
187 myf
.f
.src
= __PRETTY_FUNCTION__
;
188 myf
.f
.delivery
.tv_sec
= 0;
189 myf
.f
.delivery
.tv_usec
= 0;
190 myf
.f
.data
= myf
.frdata
;
191 if (ast_write(chan
, &myf
.f
) < 0) {
196 ast_log(LOG_DEBUG
, "No more mp3\n");
200 next
= ast_tvadd(next
, ast_samp2tv(myf
.f
.samples
, 8000));
202 ms
= ast_waitfor(chan
, ms
);
204 ast_log(LOG_DEBUG
, "Hangup detected\n");
211 ast_log(LOG_DEBUG
, "Null frame == hangup() detected\n");
215 if (f
->frametype
== AST_FRAME_DTMF
) {
216 ast_log(LOG_DEBUG
, "User pressed a key\n");
231 if (!res
&& owriteformat
)
232 ast_set_write_format(chan
, owriteformat
);
234 ast_module_user_remove(u
);
239 static int unload_module(void)
243 res
= ast_unregister_application(app
);
245 ast_module_user_hangup_all();
250 static int load_module(void)
252 return ast_register_application(app
, mp3_exec
, synopsis
, descrip
);
255 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY
, "Silly MP3 Application");