More select/poll updates for various applications
[asterisk/asterisk.git] / apps / app_mp3.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Silly application to play an MP3 file -- uses mpg123
5  * 
6  * Copyright (C) 1999, Mark Spencer
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13  
14 #include <asterisk/lock.h>
15 #include <asterisk/file.h>
16 #include <asterisk/logger.h>
17 #include <asterisk/channel.h>
18 #include <asterisk/frame.h>
19 #include <asterisk/pbx.h>
20 #include <asterisk/module.h>
21 #include <asterisk/translate.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <signal.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <pthread.h>
29 #include <sys/time.h>
30
31 #define LOCAL_MPG_123 "/usr/local/bin/mpg123"
32 #define MPG_123 "/usr/bin/mpg123"
33
34 static char *tdesc = "Silly MP3 Application";
35
36 static char *app = "MP3Player";
37
38 static char *synopsis = "Play an MP3 file or stream";
39
40 static char *descrip = 
41 "  MP3Player(location) Executes mpg123 to play the given location\n"
42 "which typically would be a  filename  or  a URL. Returns  -1  on\n"
43 "hangup or 0 otherwise. User can exit by pressing any key\n.";
44
45 STANDARD_LOCAL_USER;
46
47 LOCAL_USER_DECL;
48
49 static int mp3play(char *filename, int fd)
50 {
51         int res;
52         int x;
53         res = fork();
54         if (res < 0) 
55                 ast_log(LOG_WARNING, "Fork failed\n");
56         if (res)
57                 return res;
58         dup2(fd, STDOUT_FILENO);
59         for (x=0;x<256;x++) {
60                 if (x != STDOUT_FILENO)
61                         close(x);
62         }
63         /* Execute mpg123, but buffer if it's a net connection */
64         if (strncmp(filename, "http://", 7)) {
65                 /* Most commonly installed in /usr/local/bin */
66             execl(LOCAL_MPG_123, "mpg123", "-q", "-s", "-b", "1024", "--mono", "-r", "8000", filename, (char *)NULL);
67                 /* But many places has it in /usr/bin */
68             execl(MPG_123, "mpg123", "-q", "-s", "-b", "1024", "--mono", "-r", "8000", filename, (char *)NULL);
69                 /* As a last-ditch effort, try to use PATH */
70             execlp("mpg123", "mpg123", "-q", "-s", "-b", "1024", "--mono", "-r", "8000", filename, (char *)NULL);
71         }
72         else {
73                 /* Most commonly installed in /usr/local/bin */
74             execl(MPG_123, "mpg123", "-q", "-s", "--mono", "-r", "8000", filename, (char *)NULL);
75                 /* But many places has it in /usr/bin */
76             execl(LOCAL_MPG_123, "mpg123", "-q", "-s", "--mono", "-r", "8000", filename, (char *)NULL);
77                 /* As a last-ditch effort, try to use PATH */
78             execlp("mpg123", "mpg123", "-q", "-s", "--mono", "-r", "8000", filename, (char *)NULL);
79         }
80         ast_log(LOG_WARNING, "Execute of mpg123 failed\n");
81         return -1;
82 }
83
84 static int timed_read(int fd, void *data, int datalen)
85 {
86         int res;
87         struct pollfd fds[1];
88         fds[0].fd = fd;
89         fds[0].events = POLLIN;
90         res = poll(fds, 1, 2000);
91         if (res < 1) {
92                 ast_log(LOG_NOTICE, "Selected timed out/errored out with %d\n", res);
93                 return -1;
94         }
95         return read(fd, data, datalen);
96         
97 }
98
99 static int mp3_exec(struct ast_channel *chan, void *data)
100 {
101         int res=0;
102         struct localuser *u;
103         int fds[2];
104         int ms = -1;
105         int pid = -1;
106         int owriteformat;
107         struct timeval last;
108         struct ast_frame *f;
109         struct myframe {
110                 struct ast_frame f;
111                 char offset[AST_FRIENDLY_OFFSET];
112                 short frdata[160];
113         } myf;
114         last.tv_usec = 0;
115         last.tv_sec = 0;
116         if (!data) {
117                 ast_log(LOG_WARNING, "MP3 Playback requires an argument (filename)\n");
118                 return -1;
119         }
120         if (pipe(fds)) {
121                 ast_log(LOG_WARNING, "Unable to create pipe\n");
122                 return -1;
123         }
124         LOCAL_USER_ADD(u);
125         ast_stopstream(chan);
126
127         owriteformat = chan->writeformat;
128         res = ast_set_write_format(chan, AST_FORMAT_SLINEAR);
129         if (res < 0) {
130                 ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
131                 return -1;
132         }
133         
134         res = mp3play((char *)data, fds[1]);
135         /* Wait 1000 ms first */
136         ms = 1000;
137         if (res >= 0) {
138                 pid = res;
139                 /* Order is important -- there's almost always going to be mp3...  we want to prioritize the
140                    user */
141                 for (;;) {
142                         ms = ast_waitfor(chan, ms);
143                         if (ms < 0) {
144                                 ast_log(LOG_DEBUG, "Hangup detected\n");
145                                 res = -1;
146                                 break;
147                         }
148                         if (ms > 40) {
149                                 f = ast_read(chan);
150                                 if (!f) {
151                                         ast_log(LOG_DEBUG, "Null frame == hangup() detected\n");
152                                         res = -1;
153                                         break;
154                                 }
155                                 if (f->frametype == AST_FRAME_DTMF) {
156                                         ast_log(LOG_DEBUG, "User pressed a key\n");
157                                         ast_frfree(f);
158                                         res = 0;
159                                         break;
160                                 }
161                                 ast_frfree(f);
162                         } else  {
163 #if 0
164                                 {
165                                         static struct timeval last;
166                                         struct timeval tv;
167                                         gettimeofday(&tv, NULL);
168                                         printf("Since last: %ld\n", (tv.tv_sec - last.tv_sec) * 1000 + (tv.tv_usec - last.tv_usec) / 1000);
169                                         last = tv;
170                                 }
171 #endif
172                                 res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata));
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                                 ms += res / 16;
194 #if 0
195                                 printf("Next: %d\n", ms);
196 #endif                          
197                         }
198                 }
199         }
200         close(fds[0]);
201         close(fds[1]);
202         LOCAL_USER_REMOVE(u);
203         if (pid > -1)
204                 kill(pid, SIGKILL);
205         if (!res && owriteformat)
206                 ast_set_write_format(chan, owriteformat);
207         return res;
208 }
209
210 int unload_module(void)
211 {
212         STANDARD_HANGUP_LOCALUSERS;
213         return ast_unregister_application(app);
214 }
215
216 int load_module(void)
217 {
218         return ast_register_application(app, mp3_exec, synopsis, descrip);
219 }
220
221 char *description(void)
222 {
223         return tdesc;
224 }
225
226 int usecount(void)
227 {
228         int res;
229         STANDARD_USECOUNT(res);
230         return res;
231 }
232
233 char *key()
234 {
235         return ASTERISK_GPL_KEY;
236 }