Merged revisions 24019 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 <string.h>
29 #include <stdio.h>
30 #include <signal.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <sys/time.h>
35
36 #include "asterisk.h"
37
38 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
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 *tdesc = "Silly MP3 Application";
54
55 static char *app = "MP3Player";
56
57 static char *synopsis = "Play an MP3 file or stream";
58
59 static char *descrip = 
60 "  MP3Player(location) Executes mpg123 to play the given location,\n"
61 "which typically would be a filename or a URL. User can exit by pressing\n"
62 "any key on the dialpad, or by hanging up."; 
63
64 LOCAL_USER_DECL;
65
66 static int mp3play(char *filename, int fd)
67 {
68         int res;
69         int x;
70         res = fork();
71         if (res < 0) 
72                 ast_log(LOG_WARNING, "Fork failed\n");
73         if (res)
74                 return res;
75         if (ast_opt_high_priority)
76                 ast_set_priority(0);
77         dup2(fd, STDOUT_FILENO);
78         for (x=0;x<256;x++) {
79                 if (x != STDOUT_FILENO)
80                         close(x);
81         }
82         /* Execute mpg123, but buffer if it's a net connection */
83         if (!strncasecmp(filename, "http://", 7)) {
84                 /* Most commonly installed in /usr/local/bin */
85             execl(LOCAL_MPG_123, "mpg123", "-q", "-s", "-b", "1024", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
86                 /* But many places has it in /usr/bin */
87             execl(MPG_123, "mpg123", "-q", "-s", "-b", "1024","-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
88                 /* As a last-ditch effort, try to use PATH */
89             execlp("mpg123", "mpg123", "-q", "-s", "-b", "1024",  "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
90         }
91         else {
92                 /* Most commonly installed in /usr/local/bin */
93             execl(MPG_123, "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
94                 /* But many places has it in /usr/bin */
95             execl(LOCAL_MPG_123, "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
96                 /* As a last-ditch effort, try to use PATH */
97             execlp("mpg123", "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
98         }
99         ast_log(LOG_WARNING, "Execute of mpg123 failed\n");
100         return -1;
101 }
102
103 static int timed_read(int fd, void *data, int datalen, int timeout)
104 {
105         int res;
106         struct pollfd fds[1];
107         fds[0].fd = fd;
108         fds[0].events = POLLIN;
109         res = poll(fds, 1, timeout);
110         if (res < 1) {
111                 ast_log(LOG_NOTICE, "Poll timed out/errored out with %d\n", res);
112                 return -1;
113         }
114         return read(fd, data, datalen);
115         
116 }
117
118 static int mp3_exec(struct ast_channel *chan, void *data)
119 {
120         int res=0;
121         struct localuser *u;
122         int fds[2];
123         int ms = -1;
124         int pid = -1;
125         int owriteformat;
126         int timeout = 2000;
127         struct timeval next;
128         struct ast_frame *f;
129         struct myframe {
130                 struct ast_frame f;
131                 char offset[AST_FRIENDLY_OFFSET];
132                 short frdata[160];
133         } myf;
134         
135         if (ast_strlen_zero(data)) {
136                 ast_log(LOG_WARNING, "MP3 Playback requires an argument (filename)\n");
137                 return -1;
138         }
139
140         LOCAL_USER_ADD(u);
141
142         if (pipe(fds)) {
143                 ast_log(LOG_WARNING, "Unable to create pipe\n");
144                 LOCAL_USER_REMOVE(u);
145                 return -1;
146         }
147         
148         ast_stopstream(chan);
149
150         owriteformat = chan->writeformat;
151         res = ast_set_write_format(chan, AST_FORMAT_SLINEAR);
152         if (res < 0) {
153                 ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
154                 LOCAL_USER_REMOVE(u);
155                 return -1;
156         }
157         
158         res = mp3play((char *)data, fds[1]);
159         if (!strncasecmp((char *)data, "http://", 7)) {
160                 timeout = 10000;
161         }
162         /* Wait 1000 ms first */
163         next = ast_tvnow();
164         next.tv_sec += 1;
165         if (res >= 0) {
166                 pid = res;
167                 /* Order is important -- there's almost always going to be mp3...  we want to prioritize the
168                    user */
169                 for (;;) {
170                         ms = ast_tvdiff_ms(next, ast_tvnow());
171                         if (ms <= 0) {
172                                 res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata), timeout);
173                                 if (res > 0) {
174                                         myf.f.frametype = AST_FRAME_VOICE;
175                                         myf.f.subclass = AST_FORMAT_SLINEAR;
176                                         myf.f.datalen = res;
177                                         myf.f.samples = res / 2;
178                                         myf.f.mallocd = 0;
179                                         myf.f.offset = AST_FRIENDLY_OFFSET;
180                                         myf.f.src = __PRETTY_FUNCTION__;
181                                         myf.f.delivery.tv_sec = 0;
182                                         myf.f.delivery.tv_usec = 0;
183                                         myf.f.data = myf.frdata;
184                                         if (ast_write(chan, &myf.f) < 0) {
185                                                 res = -1;
186                                                 break;
187                                         }
188                                 } else {
189                                         ast_log(LOG_DEBUG, "No more mp3\n");
190                                         res = 0;
191                                         break;
192                                 }
193                                 next = ast_tvadd(next, ast_samp2tv(myf.f.samples, 8000));
194                         } else {
195                                 ms = ast_waitfor(chan, ms);
196                                 if (ms < 0) {
197                                         ast_log(LOG_DEBUG, "Hangup detected\n");
198                                         res = -1;
199                                         break;
200                                 }
201                                 if (ms) {
202                                         f = ast_read(chan);
203                                         if (!f) {
204                                                 ast_log(LOG_DEBUG, "Null frame == hangup() detected\n");
205                                                 res = -1;
206                                                 break;
207                                         }
208                                         if (f->frametype == AST_FRAME_DTMF) {
209                                                 ast_log(LOG_DEBUG, "User pressed a key\n");
210                                                 ast_frfree(f);
211                                                 res = 0;
212                                                 break;
213                                         }
214                                         ast_frfree(f);
215                                 } 
216                         }
217                 }
218         }
219         close(fds[0]);
220         close(fds[1]);
221         
222         if (pid > -1)
223                 kill(pid, SIGKILL);
224         if (!res && owriteformat)
225                 ast_set_write_format(chan, owriteformat);
226
227         LOCAL_USER_REMOVE(u);
228         
229         return res;
230 }
231
232 static int unload_module(void *mod)
233 {
234         int res;
235
236         res = ast_unregister_application(app);
237
238         STANDARD_HANGUP_LOCALUSERS;
239         
240         return res;
241 }
242
243 static int load_module(void *mod)
244 {
245         return ast_register_application(app, mp3_exec, synopsis, descrip);
246 }
247
248 static const char *description(void)
249 {
250         return tdesc;
251 }
252
253 static const char *key(void)
254 {
255         return ASTERISK_GPL_KEY;
256 }
257
258 STD_MOD1;