704666d8f6da141472ddd2404aca41184b1f8a86
[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 <string.h>
33 #include <stdio.h>
34 #include <signal.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <sys/time.h>
39
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"
49
50 #define LOCAL_MPG_123 "/usr/local/bin/mpg123"
51 #define MPG_123 "/usr/bin/mpg123"
52
53 static char *app = "MP3Player";
54
55 static char *synopsis = "Play an MP3 file or stream";
56
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."; 
61
62
63 static int mp3play(char *filename, int fd)
64 {
65         int res;
66         int x;
67         res = fork();
68         if (res < 0) 
69                 ast_log(LOG_WARNING, "Fork failed\n");
70         if (res)
71                 return res;
72         if (ast_opt_high_priority)
73                 ast_set_priority(0);
74         dup2(fd, STDOUT_FILENO);
75         for (x=0;x<256;x++) {
76                 if (x != STDOUT_FILENO)
77                         close(x);
78         }
79         /* Execute mpg123, but buffer if it's a net connection */
80         if (!strncasecmp(filename, "http://", 7)) {
81                 /* Most commonly installed in /usr/local/bin */
82             execl(LOCAL_MPG_123, "mpg123", "-q", "-s", "-b", "1024", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
83                 /* But many places has it in /usr/bin */
84             execl(MPG_123, "mpg123", "-q", "-s", "-b", "1024","-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
85                 /* As a last-ditch effort, try to use PATH */
86             execlp("mpg123", "mpg123", "-q", "-s", "-b", "1024",  "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
87         }
88         else {
89                 /* Most commonly installed in /usr/local/bin */
90             execl(MPG_123, "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
91                 /* But many places has it in /usr/bin */
92             execl(LOCAL_MPG_123, "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
93                 /* As a last-ditch effort, try to use PATH */
94             execlp("mpg123", "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
95         }
96         ast_log(LOG_WARNING, "Execute of mpg123 failed\n");
97         return -1;
98 }
99
100 static int timed_read(int fd, void *data, int datalen, int timeout)
101 {
102         int res;
103         struct pollfd fds[1];
104         fds[0].fd = fd;
105         fds[0].events = POLLIN;
106         res = poll(fds, 1, timeout);
107         if (res < 1) {
108                 ast_log(LOG_NOTICE, "Poll timed out/errored out with %d\n", res);
109                 return -1;
110         }
111         return read(fd, data, datalen);
112         
113 }
114
115 static int mp3_exec(struct ast_channel *chan, void *data)
116 {
117         int res=0;
118         struct ast_module_user *u;
119         int fds[2];
120         int ms = -1;
121         int pid = -1;
122         int owriteformat;
123         int timeout = 2000;
124         struct timeval next;
125         struct ast_frame *f;
126         struct myframe {
127                 struct ast_frame f;
128                 char offset[AST_FRIENDLY_OFFSET];
129                 short frdata[160];
130         } myf;
131         
132         if (ast_strlen_zero(data)) {
133                 ast_log(LOG_WARNING, "MP3 Playback requires an argument (filename)\n");
134                 return -1;
135         }
136
137         u = ast_module_user_add(chan);
138
139         if (pipe(fds)) {
140                 ast_log(LOG_WARNING, "Unable to create pipe\n");
141                 ast_module_user_remove(u);
142                 return -1;
143         }
144         
145         ast_stopstream(chan);
146
147         owriteformat = chan->writeformat;
148         res = ast_set_write_format(chan, AST_FORMAT_SLINEAR);
149         if (res < 0) {
150                 ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
151                 ast_module_user_remove(u);
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 = myf.frdata;
181                                         if (ast_write(chan, &myf.f) < 0) {
182                                                 res = -1;
183                                                 break;
184                                         }
185                                 } else {
186                                         if (option_debug)
187                                                 ast_log(LOG_DEBUG, "No more mp3\n");
188                                         res = 0;
189                                         break;
190                                 }
191                                 next = ast_tvadd(next, ast_samp2tv(myf.f.samples, 8000));
192                         } else {
193                                 ms = ast_waitfor(chan, ms);
194                                 if (ms < 0) {
195                                         if (option_debug)
196                                                 ast_log(LOG_DEBUG, "Hangup detected\n");
197                                         res = -1;
198                                         break;
199                                 }
200                                 if (ms) {
201                                         f = ast_read(chan);
202                                         if (!f) {
203                                                 if (option_debug)
204                                                         ast_log(LOG_DEBUG, "Null frame == hangup() detected\n");
205                                                 res = -1;
206                                                 break;
207                                         }
208                                         if (f->frametype == AST_FRAME_DTMF) {
209                                                 if (option_debug)
210                                                         ast_log(LOG_DEBUG, "User pressed a key\n");
211                                                 ast_frfree(f);
212                                                 res = 0;
213                                                 break;
214                                         }
215                                         ast_frfree(f);
216                                 } 
217                         }
218                 }
219         }
220         close(fds[0]);
221         close(fds[1]);
222         
223         if (pid > -1)
224                 kill(pid, SIGKILL);
225         if (!res && owriteformat)
226                 ast_set_write_format(chan, owriteformat);
227
228         ast_module_user_remove(u);
229         
230         return res;
231 }
232
233 static int unload_module(void)
234 {
235         int res;
236
237         res = ast_unregister_application(app);
238
239         ast_module_user_hangup_all();
240         
241         return res;
242 }
243
244 static int load_module(void)
245 {
246         return ast_register_application(app, mp3_exec, synopsis, descrip);
247 }
248
249 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Silly MP3 Application");