Minor formatting change to test a mantis change ...
[asterisk-bristuff.git] / apps / app_mp3.c
blob23db94fbc58d00d6231bd86336b626f973d5756b
1 /*
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.
19 /*! \file
21 * \brief Silly application to play an MP3 file -- uses mpg123
23 * \author Mark Spencer <markster@digium.com>
25 * \ingroup applications
28 #include "asterisk.h"
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32 #include <sys/time.h>
33 #include <signal.h>
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)
59 int res;
61 res = ast_safe_fork(0);
62 if (res < 0)
63 ast_log(LOG_WARNING, "Fork failed\n");
64 if (res) {
65 return res;
67 if (ast_opt_high_priority)
68 ast_set_priority(0);
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);
82 else {
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");
92 _exit(0);
95 static int timed_read(int fd, void *data, int datalen, int timeout)
97 int res;
98 struct pollfd fds[1];
99 fds[0].fd = fd;
100 fds[0].events = POLLIN;
101 res = poll(fds, 1, timeout);
102 if (res < 1) {
103 ast_log(LOG_NOTICE, "Poll timed out/errored out with %d\n", res);
104 return -1;
106 return read(fd, data, datalen);
110 static int mp3_exec(struct ast_channel *chan, void *data)
112 int res=0;
113 int fds[2];
114 int ms = -1;
115 int pid = -1;
116 int owriteformat;
117 int timeout = 2000;
118 struct timeval next;
119 struct ast_frame *f;
120 struct myframe {
121 struct ast_frame f;
122 char offset[AST_FRIENDLY_OFFSET];
123 short frdata[160];
124 } myf;
126 if (ast_strlen_zero(data)) {
127 ast_log(LOG_WARNING, "MP3 Playback requires an argument (filename)\n");
128 return -1;
131 if (pipe(fds)) {
132 ast_log(LOG_WARNING, "Unable to create pipe\n");
133 return -1;
136 ast_stopstream(chan);
138 owriteformat = chan->writeformat;
139 res = ast_set_write_format(chan, AST_FORMAT_SLINEAR);
140 if (res < 0) {
141 ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
142 return -1;
145 res = mp3play((char *)data, fds[1]);
146 if (!strncasecmp((char *)data, "http://", 7)) {
147 timeout = 10000;
149 /* Wait 1000 ms first */
150 next = ast_tvnow();
151 next.tv_sec += 1;
152 if (res >= 0) {
153 pid = res;
154 /* Order is important -- there's almost always going to be mp3... we want to prioritize the
155 user */
156 for (;;) {
157 ms = ast_tvdiff_ms(next, ast_tvnow());
158 if (ms <= 0) {
159 res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata), timeout);
160 if (res > 0) {
161 myf.f.frametype = AST_FRAME_VOICE;
162 myf.f.subclass = AST_FORMAT_SLINEAR;
163 myf.f.datalen = res;
164 myf.f.samples = res / 2;
165 myf.f.mallocd = 0;
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) {
172 res = -1;
173 break;
175 } else {
176 ast_debug(1, "No more mp3\n");
177 res = 0;
178 break;
180 next = ast_tvadd(next, ast_samp2tv(myf.f.samples, 8000));
181 } else {
182 ms = ast_waitfor(chan, ms);
183 if (ms < 0) {
184 ast_debug(1, "Hangup detected\n");
185 res = -1;
186 break;
188 if (ms) {
189 f = ast_read(chan);
190 if (!f) {
191 ast_debug(1, "Null frame == hangup() detected\n");
192 res = -1;
193 break;
195 if (f->frametype == AST_FRAME_DTMF) {
196 ast_debug(1, "User pressed a key\n");
197 ast_frfree(f);
198 res = 0;
199 break;
201 ast_frfree(f);
206 close(fds[0]);
207 close(fds[1]);
209 if (pid > -1)
210 kill(pid, SIGKILL);
211 if (!res && owriteformat)
212 ast_set_write_format(chan, owriteformat);
214 return res;
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");