463a91906f849cf3ebeed1b773655aa07bf88acf
[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  * \note Add feature to play local M3U playlist file
26  * Vincent Li <mchun.li@gmail.com>
27  * 
28  * \ingroup applications
29  */
30
31 /*** MODULEINFO
32         <support_level>extended</support_level>
33  ***/
34  
35 #include "asterisk.h"
36
37 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
38
39 #include <sys/time.h>
40 #include <signal.h>
41
42 #include "asterisk/lock.h"
43 #include "asterisk/file.h"
44 #include "asterisk/channel.h"
45 #include "asterisk/frame.h"
46 #include "asterisk/pbx.h"
47 #include "asterisk/module.h"
48 #include "asterisk/translate.h"
49 #include "asterisk/app.h"
50
51 #define LOCAL_MPG_123 "/usr/local/bin/mpg123"
52 #define MPG_123 "/usr/bin/mpg123"
53
54 /*** DOCUMENTATION
55         <application name="MP3Player" language="en_US">
56                 <synopsis>
57                         Play an MP3 file or M3U playlist file or stream.
58                 </synopsis>
59                 <syntax>
60                         <parameter name="Location" required="true">
61                                 <para>Location of the file to be played.
62                                 (argument passed to mpg123)</para>
63                         </parameter>
64                 </syntax>
65                 <description>
66                         <para>Executes mpg123 to play the given location, which typically would be a mp3 filename
67                         or m3u playlist filename or a URL. Please read http://en.wikipedia.org/wiki/M3U
68                         to see how M3U playlist file format is like, Example usage would be
69                         exten => 1234,1,MP3Player(/var/lib/asterisk/playlist.m3u)
70                         User can exit by pressing any key on the dialpad, or by hanging up.</para>
71                         <para>This application does not automatically answer and should be preceeded by an
72                         application such as Answer() or Progress().</para>
73                 </description>
74         </application>
75
76  ***/
77 static char *app = "MP3Player";
78
79 static int mp3play(const char *filename, int fd)
80 {
81         int res;
82
83         res = ast_safe_fork(0);
84         if (res < 0) 
85                 ast_log(LOG_WARNING, "Fork failed\n");
86         if (res) {
87                 return res;
88         }
89         if (ast_opt_high_priority)
90                 ast_set_priority(0);
91
92         dup2(fd, STDOUT_FILENO);
93         ast_close_fds_above_n(STDERR_FILENO);
94
95         /* Execute mpg123, but buffer if it's a net connection */
96         if (!strncasecmp(filename, "http://", 7)) {
97                 /* Most commonly installed in /usr/local/bin */
98             execl(LOCAL_MPG_123, "mpg123", "-q", "-s", "-b", "1024", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
99                 /* But many places has it in /usr/bin */
100             execl(MPG_123, "mpg123", "-q", "-s", "-b", "1024","-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
101                 /* As a last-ditch effort, try to use PATH */
102             execlp("mpg123", "mpg123", "-q", "-s", "-b", "1024",  "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
103         }
104         else if (strstr(filename, ".m3u")) {
105                 /* Most commonly installed in /usr/local/bin */
106             execl(LOCAL_MPG_123, "mpg123", "-q", "-z", "-s", "-b", "1024", "-f", "8192", "--mono", "-r", "8000", "-@", filename, (char *)NULL);
107                 /* But many places has it in /usr/bin */
108             execl(MPG_123, "mpg123", "-q", "-z", "-s", "-b", "1024","-f", "8192", "--mono", "-r", "8000", "-@", filename, (char *)NULL);
109                 /* As a last-ditch effort, try to use PATH */
110             execlp("mpg123", "mpg123", "-q", "-z", "-s", "-b", "1024",  "-f", "8192", "--mono", "-r", "8000", "-@", filename, (char *)NULL);
111         }
112         else {
113                 /* Most commonly installed in /usr/local/bin */
114             execl(MPG_123, "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
115                 /* But many places has it in /usr/bin */
116             execl(LOCAL_MPG_123, "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
117                 /* As a last-ditch effort, try to use PATH */
118             execlp("mpg123", "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
119         }
120         /* Can't use ast_log since FD's are closed */
121         fprintf(stderr, "Execute of mpg123 failed\n");
122         _exit(0);
123 }
124
125 static int timed_read(int fd, void *data, int datalen, int timeout)
126 {
127         int res;
128         struct pollfd fds[1];
129         fds[0].fd = fd;
130         fds[0].events = POLLIN;
131         res = ast_poll(fds, 1, timeout);
132         if (res < 1) {
133                 ast_log(LOG_NOTICE, "Poll timed out/errored out with %d\n", res);
134                 return -1;
135         }
136         return read(fd, data, datalen);
137         
138 }
139
140 static int mp3_exec(struct ast_channel *chan, const char *data)
141 {
142         int res=0;
143         int fds[2];
144         int ms = -1;
145         int pid = -1;
146         struct ast_format owriteformat;
147         int timeout = 2000;
148         struct timeval next;
149         struct ast_frame *f;
150         struct myframe {
151                 struct ast_frame f;
152                 char offset[AST_FRIENDLY_OFFSET];
153                 short frdata[160];
154         } myf = {
155                 .f = { 0, },
156         };
157
158         ast_format_clear(&owriteformat);
159         if (ast_strlen_zero(data)) {
160                 ast_log(LOG_WARNING, "MP3 Playback requires an argument (filename)\n");
161                 return -1;
162         }
163
164         if (pipe(fds)) {
165                 ast_log(LOG_WARNING, "Unable to create pipe\n");
166                 return -1;
167         }
168         
169         ast_stopstream(chan);
170
171         ast_format_copy(&owriteformat, ast_channel_writeformat(chan));
172         res = ast_set_write_format_by_id(chan, AST_FORMAT_SLINEAR);
173         if (res < 0) {
174                 ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
175                 return -1;
176         }
177         
178         res = mp3play(data, fds[1]);
179         if (!strncasecmp(data, "http://", 7)) {
180                 timeout = 10000;
181         }
182         /* Wait 1000 ms first */
183         next = ast_tvnow();
184         next.tv_sec += 1;
185         if (res >= 0) {
186                 pid = res;
187                 /* Order is important -- there's almost always going to be mp3...  we want to prioritize the
188                    user */
189                 for (;;) {
190                         ms = ast_tvdiff_ms(next, ast_tvnow());
191                         if (ms <= 0) {
192                                 res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata), timeout);
193                                 if (res > 0) {
194                                         myf.f.frametype = AST_FRAME_VOICE;
195                                         ast_format_set(&myf.f.subclass.format, AST_FORMAT_SLINEAR, 0);
196                                         myf.f.datalen = res;
197                                         myf.f.samples = res / 2;
198                                         myf.f.mallocd = 0;
199                                         myf.f.offset = AST_FRIENDLY_OFFSET;
200                                         myf.f.src = __PRETTY_FUNCTION__;
201                                         myf.f.delivery.tv_sec = 0;
202                                         myf.f.delivery.tv_usec = 0;
203                                         myf.f.data.ptr = myf.frdata;
204                                         if (ast_write(chan, &myf.f) < 0) {
205                                                 res = -1;
206                                                 break;
207                                         }
208                                 } else {
209                                         ast_debug(1, "No more mp3\n");
210                                         res = 0;
211                                         break;
212                                 }
213                                 next = ast_tvadd(next, ast_samp2tv(myf.f.samples, 8000));
214                         } else {
215                                 ms = ast_waitfor(chan, ms);
216                                 if (ms < 0) {
217                                         ast_debug(1, "Hangup detected\n");
218                                         res = -1;
219                                         break;
220                                 }
221                                 if (ms) {
222                                         f = ast_read(chan);
223                                         if (!f) {
224                                                 ast_debug(1, "Null frame == hangup() detected\n");
225                                                 res = -1;
226                                                 break;
227                                         }
228                                         if (f->frametype == AST_FRAME_DTMF) {
229                                                 ast_debug(1, "User pressed a key\n");
230                                                 ast_frfree(f);
231                                                 res = 0;
232                                                 break;
233                                         }
234                                         ast_frfree(f);
235                                 } 
236                         }
237                 }
238         }
239         close(fds[0]);
240         close(fds[1]);
241         
242         if (pid > -1)
243                 kill(pid, SIGKILL);
244         if (!res && owriteformat.id)
245                 ast_set_write_format(chan, &owriteformat);
246         
247         return res;
248 }
249
250 static int unload_module(void)
251 {
252         return ast_unregister_application(app);
253 }
254
255 static int load_module(void)
256 {
257         return ast_register_application_xml(app, mp3_exec);
258 }
259
260 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Silly MP3 Application");