922d42a5b4dadba264dc8c6cf00f16d01f8346ad
[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                 </description>
72         </application>
73
74  ***/
75 static char *app = "MP3Player";
76
77 static int mp3play(const char *filename, int fd)
78 {
79         int res;
80
81         res = ast_safe_fork(0);
82         if (res < 0) 
83                 ast_log(LOG_WARNING, "Fork failed\n");
84         if (res) {
85                 return res;
86         }
87         if (ast_opt_high_priority)
88                 ast_set_priority(0);
89
90         dup2(fd, STDOUT_FILENO);
91         ast_close_fds_above_n(STDERR_FILENO);
92
93         /* Execute mpg123, but buffer if it's a net connection */
94         if (!strncasecmp(filename, "http://", 7)) {
95                 /* Most commonly installed in /usr/local/bin */
96             execl(LOCAL_MPG_123, "mpg123", "-q", "-s", "-b", "1024", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
97                 /* But many places has it in /usr/bin */
98             execl(MPG_123, "mpg123", "-q", "-s", "-b", "1024","-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
99                 /* As a last-ditch effort, try to use PATH */
100             execlp("mpg123", "mpg123", "-q", "-s", "-b", "1024",  "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
101         }
102         else if (strstr(filename, ".m3u")) {
103                 /* Most commonly installed in /usr/local/bin */
104             execl(LOCAL_MPG_123, "mpg123", "-q", "-z", "-s", "-b", "1024", "-f", "8192", "--mono", "-r", "8000", "-@", filename, (char *)NULL);
105                 /* But many places has it in /usr/bin */
106             execl(MPG_123, "mpg123", "-q", "-z", "-s", "-b", "1024","-f", "8192", "--mono", "-r", "8000", "-@", filename, (char *)NULL);
107                 /* As a last-ditch effort, try to use PATH */
108             execlp("mpg123", "mpg123", "-q", "-z", "-s", "-b", "1024",  "-f", "8192", "--mono", "-r", "8000", "-@", filename, (char *)NULL);
109         }
110         else {
111                 /* Most commonly installed in /usr/local/bin */
112             execl(MPG_123, "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
113                 /* But many places has it in /usr/bin */
114             execl(LOCAL_MPG_123, "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
115                 /* As a last-ditch effort, try to use PATH */
116             execlp("mpg123", "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
117         }
118         /* Can't use ast_log since FD's are closed */
119         fprintf(stderr, "Execute of mpg123 failed\n");
120         _exit(0);
121 }
122
123 static int timed_read(int fd, void *data, int datalen, int timeout)
124 {
125         int res;
126         struct pollfd fds[1];
127         fds[0].fd = fd;
128         fds[0].events = POLLIN;
129         res = ast_poll(fds, 1, timeout);
130         if (res < 1) {
131                 ast_log(LOG_NOTICE, "Poll timed out/errored out with %d\n", res);
132                 return -1;
133         }
134         return read(fd, data, datalen);
135         
136 }
137
138 static int mp3_exec(struct ast_channel *chan, const char *data)
139 {
140         int res=0;
141         int fds[2];
142         int ms = -1;
143         int pid = -1;
144         struct ast_format owriteformat;
145         int timeout = 2000;
146         struct timeval next;
147         struct ast_frame *f;
148         struct myframe {
149                 struct ast_frame f;
150                 char offset[AST_FRIENDLY_OFFSET];
151                 short frdata[160];
152         } myf = {
153                 .f = { 0, },
154         };
155
156         ast_format_clear(&owriteformat);
157         if (ast_strlen_zero(data)) {
158                 ast_log(LOG_WARNING, "MP3 Playback requires an argument (filename)\n");
159                 return -1;
160         }
161
162         if (pipe(fds)) {
163                 ast_log(LOG_WARNING, "Unable to create pipe\n");
164                 return -1;
165         }
166         
167         ast_stopstream(chan);
168
169         ast_format_copy(&owriteformat, &chan->writeformat);
170         res = ast_set_write_format_by_id(chan, AST_FORMAT_SLINEAR);
171         if (res < 0) {
172                 ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
173                 return -1;
174         }
175         
176         res = mp3play(data, fds[1]);
177         if (!strncasecmp(data, "http://", 7)) {
178                 timeout = 10000;
179         }
180         /* Wait 1000 ms first */
181         next = ast_tvnow();
182         next.tv_sec += 1;
183         if (res >= 0) {
184                 pid = res;
185                 /* Order is important -- there's almost always going to be mp3...  we want to prioritize the
186                    user */
187                 for (;;) {
188                         ms = ast_tvdiff_ms(next, ast_tvnow());
189                         if (ms <= 0) {
190                                 res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata), timeout);
191                                 if (res > 0) {
192                                         myf.f.frametype = AST_FRAME_VOICE;
193                                         ast_format_set(&myf.f.subclass.format, AST_FORMAT_SLINEAR, 0);
194                                         myf.f.datalen = res;
195                                         myf.f.samples = res / 2;
196                                         myf.f.mallocd = 0;
197                                         myf.f.offset = AST_FRIENDLY_OFFSET;
198                                         myf.f.src = __PRETTY_FUNCTION__;
199                                         myf.f.delivery.tv_sec = 0;
200                                         myf.f.delivery.tv_usec = 0;
201                                         myf.f.data.ptr = myf.frdata;
202                                         if (ast_write(chan, &myf.f) < 0) {
203                                                 res = -1;
204                                                 break;
205                                         }
206                                 } else {
207                                         ast_debug(1, "No more mp3\n");
208                                         res = 0;
209                                         break;
210                                 }
211                                 next = ast_tvadd(next, ast_samp2tv(myf.f.samples, 8000));
212                         } else {
213                                 ms = ast_waitfor(chan, ms);
214                                 if (ms < 0) {
215                                         ast_debug(1, "Hangup detected\n");
216                                         res = -1;
217                                         break;
218                                 }
219                                 if (ms) {
220                                         f = ast_read(chan);
221                                         if (!f) {
222                                                 ast_debug(1, "Null frame == hangup() detected\n");
223                                                 res = -1;
224                                                 break;
225                                         }
226                                         if (f->frametype == AST_FRAME_DTMF) {
227                                                 ast_debug(1, "User pressed a key\n");
228                                                 ast_frfree(f);
229                                                 res = 0;
230                                                 break;
231                                         }
232                                         ast_frfree(f);
233                                 } 
234                         }
235                 }
236         }
237         close(fds[0]);
238         close(fds[1]);
239         
240         if (pid > -1)
241                 kill(pid, SIGKILL);
242         if (!res && owriteformat.id)
243                 ast_set_write_format(chan, &owriteformat);
244         
245         return res;
246 }
247
248 static int unload_module(void)
249 {
250         return ast_unregister_application(app);
251 }
252
253 static int load_module(void)
254 {
255         return ast_register_application_xml(app, mp3_exec);
256 }
257
258 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Silly MP3 Application");