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