Merged revisions 182810 via svnmerge from
[asterisk/asterisk.git] / apps / app_mp3.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
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.
13  *
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.
17  */
18
19 /*! \file
20  *
21  * \brief Silly application to play an MP3 file -- uses mpg123
22  *
23  * \author Mark Spencer <markster@digium.com>
24  * 
25  * \ingroup applications
26  */
27  
28 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include <sys/time.h>
33 #include <signal.h>
34
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"
43
44 #define LOCAL_MPG_123 "/usr/local/bin/mpg123"
45 #define MPG_123 "/usr/bin/mpg123"
46
47 /*** DOCUMENTATION
48         <application name="MP3Player" language="en_US">
49                 <synopsis>
50                         Play an MP3 file or stream.
51                 </synopsis>
52                 <syntax>
53                         <parameter name="Location" required="true">
54                                 <para>Location of the file to be played.
55                                 (argument passed to mpg123)</para>
56                         </parameter>
57                 </syntax>
58                 <description>
59                         <para>Executes mpg123 to play the given location, which typically would be a filename or a URL.
60                         User can exit by pressing any key on the dialpad, or by hanging up.</para>
61                 </description>
62         </application>
63
64  ***/
65 static char *app = "MP3Player";
66
67 static int mp3play(char *filename, int fd)
68 {
69         int res;
70
71         res = ast_safe_fork(0);
72         if (res < 0) 
73                 ast_log(LOG_WARNING, "Fork failed\n");
74         if (res) {
75                 return res;
76         }
77         if (ast_opt_high_priority)
78                 ast_set_priority(0);
79
80         dup2(fd, STDOUT_FILENO);
81         ast_close_fds_above_n(STDERR_FILENO);
82
83         /* Execute mpg123, but buffer if it's a net connection */
84         if (!strncasecmp(filename, "http://", 7)) {
85                 /* Most commonly installed in /usr/local/bin */
86             execl(LOCAL_MPG_123, "mpg123", "-q", "-s", "-b", "1024", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
87                 /* But many places has it in /usr/bin */
88             execl(MPG_123, "mpg123", "-q", "-s", "-b", "1024","-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
89                 /* As a last-ditch effort, try to use PATH */
90             execlp("mpg123", "mpg123", "-q", "-s", "-b", "1024",  "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
91         }
92         else {
93                 /* Most commonly installed in /usr/local/bin */
94             execl(MPG_123, "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
95                 /* But many places has it in /usr/bin */
96             execl(LOCAL_MPG_123, "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
97                 /* As a last-ditch effort, try to use PATH */
98             execlp("mpg123", "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
99         }
100         /* Can't use ast_log since FD's are closed */
101         fprintf(stderr, "Execute of mpg123 failed\n");
102         _exit(0);
103 }
104
105 static int timed_read(int fd, void *data, int datalen, int timeout)
106 {
107         int res;
108         struct pollfd fds[1];
109         fds[0].fd = fd;
110         fds[0].events = POLLIN;
111         res = ast_poll(fds, 1, timeout);
112         if (res < 1) {
113                 ast_log(LOG_NOTICE, "Poll timed out/errored out with %d\n", res);
114                 return -1;
115         }
116         return read(fd, data, datalen);
117         
118 }
119
120 static int mp3_exec(struct ast_channel *chan, void *data)
121 {
122         int res=0;
123         int fds[2];
124         int ms = -1;
125         int pid = -1;
126         int owriteformat;
127         int timeout = 2000;
128         struct timeval next;
129         struct ast_frame *f;
130         struct myframe {
131                 struct ast_frame f;
132                 char offset[AST_FRIENDLY_OFFSET];
133                 short frdata[160];
134         } myf;
135         
136         if (ast_strlen_zero(data)) {
137                 ast_log(LOG_WARNING, "MP3 Playback requires an argument (filename)\n");
138                 return -1;
139         }
140
141         if (pipe(fds)) {
142                 ast_log(LOG_WARNING, "Unable to create pipe\n");
143                 return -1;
144         }
145         
146         ast_stopstream(chan);
147
148         owriteformat = chan->writeformat;
149         res = ast_set_write_format(chan, AST_FORMAT_SLINEAR);
150         if (res < 0) {
151                 ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
152                 return -1;
153         }
154         
155         res = mp3play((char *)data, fds[1]);
156         if (!strncasecmp((char *)data, "http://", 7)) {
157                 timeout = 10000;
158         }
159         /* Wait 1000 ms first */
160         next = ast_tvnow();
161         next.tv_sec += 1;
162         if (res >= 0) {
163                 pid = res;
164                 /* Order is important -- there's almost always going to be mp3...  we want to prioritize the
165                    user */
166                 for (;;) {
167                         ms = ast_tvdiff_ms(next, ast_tvnow());
168                         if (ms <= 0) {
169                                 res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata), timeout);
170                                 if (res > 0) {
171                                         myf.f.frametype = AST_FRAME_VOICE;
172                                         myf.f.subclass = AST_FORMAT_SLINEAR;
173                                         myf.f.datalen = res;
174                                         myf.f.samples = res / 2;
175                                         myf.f.mallocd = 0;
176                                         myf.f.offset = AST_FRIENDLY_OFFSET;
177                                         myf.f.src = __PRETTY_FUNCTION__;
178                                         myf.f.delivery.tv_sec = 0;
179                                         myf.f.delivery.tv_usec = 0;
180                                         myf.f.data.ptr = myf.frdata;
181                                         if (ast_write(chan, &myf.f) < 0) {
182                                                 res = -1;
183                                                 break;
184                                         }
185                                 } else {
186                                         ast_debug(1, "No more mp3\n");
187                                         res = 0;
188                                         break;
189                                 }
190                                 next = ast_tvadd(next, ast_samp2tv(myf.f.samples, 8000));
191                         } else {
192                                 ms = ast_waitfor(chan, ms);
193                                 if (ms < 0) {
194                                         ast_debug(1, "Hangup detected\n");
195                                         res = -1;
196                                         break;
197                                 }
198                                 if (ms) {
199                                         f = ast_read(chan);
200                                         if (!f) {
201                                                 ast_debug(1, "Null frame == hangup() detected\n");
202                                                 res = -1;
203                                                 break;
204                                         }
205                                         if (f->frametype == AST_FRAME_DTMF) {
206                                                 ast_debug(1, "User pressed a key\n");
207                                                 ast_frfree(f);
208                                                 res = 0;
209                                                 break;
210                                         }
211                                         ast_frfree(f);
212                                 } 
213                         }
214                 }
215         }
216         close(fds[0]);
217         close(fds[1]);
218         
219         if (pid > -1)
220                 kill(pid, SIGKILL);
221         if (!res && owriteformat)
222                 ast_set_write_format(chan, owriteformat);
223         
224         return res;
225 }
226
227 static int unload_module(void)
228 {
229         return ast_unregister_application(app);
230 }
231
232 static int load_module(void)
233 {
234         return ast_register_application_xml(app, mp3_exec);
235 }
236
237 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Silly MP3 Application");