Version 0.1.7 from FTP
[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, Adtran Inc. and Linux Support Services, LLC
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/file.h>
15 #include <asterisk/logger.h>
16 #include <asterisk/channel.h>
17 #include <asterisk/frame.h>
18 #include <asterisk/pbx.h>
19 #include <asterisk/module.h>
20 #include <asterisk/translate.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <signal.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <pthread.h>
28 #include <sys/time.h>
29
30 #define MPG_123 "/usr/bin/mpg123"
31
32 static char *tdesc = "Silly MP3 Application";
33
34 static char *app = "MP3Player";
35
36 STANDARD_LOCAL_USER;
37
38 LOCAL_USER_DECL;
39
40 static int mp3play(char *filename, int fd)
41 {
42         int res;
43         res = fork();
44         if (res < 0) 
45                 ast_log(LOG_WARNING, "Fork failed\n");
46         if (res)
47                 return res;
48         dup2(fd, STDOUT_FILENO);
49         /* Execute mpg123, but buffer if it's a net connection */
50         if (strncmp(filename, "http://", 7)) 
51             execl(MPG_123, MPG_123, "-q", "-s", "-b", "1024", "--mono", "-r", "8000", filename, NULL);
52         else
53             execl(MPG_123, MPG_123, "-q", "-s", "--mono", "-r", "8000", filename, NULL);
54         ast_log(LOG_WARNING, "Execute of mpg123 failed\n");
55         return -1;
56 }
57
58 static int mp3_exec(struct ast_channel *chan, void *data)
59 {
60         int res=0;
61         struct localuser *u;
62         struct ast_channel *trans;
63         int fds[2];
64         int rfds[2];
65         int ms = -1;
66         int pid;
67         int us;
68         struct timeval tv;
69         struct timeval last;
70         struct ast_frame *f;
71         struct myframe {
72                 struct ast_frame f;
73                 char offset[AST_FRIENDLY_OFFSET];
74                 char frdata[160];
75         } myf;
76         last.tv_usec = 0;
77         last.tv_sec = 0;
78         if (!data) {
79                 ast_log(LOG_WARNING, "MP3 Playback requires an argument (filename)\n");
80                 return -1;
81         }
82         if (pipe(fds)) {
83                 ast_log(LOG_WARNING, "Unable to create pipe\n");
84                 return -1;
85         }
86         LOCAL_USER_ADD(u);
87         ast_stopstream(chan);
88         if (chan->format & AST_FORMAT_SLINEAR)
89                 trans = chan;
90         else
91                 trans = ast_translator_create(chan, AST_FORMAT_SLINEAR, AST_DIRECTION_OUT);
92         if (trans) {
93                 res = mp3play((char *)data, fds[1]);
94                 if (res >= 0) {
95                         pid = res;
96                         /* Order is important -- there's almost always going to be mp3...  we want to prioritize the
97                            user */
98                         rfds[0] = trans->fd;
99                         rfds[1] = fds[0];
100                         for (;;) {
101                                 CHECK_BLOCKING(trans);
102                                 res = ast_waitfor_n_fd(rfds, 2, &ms);
103                                 trans->blocking = 0;
104                                 if (res < 1) {
105                                         ast_log(LOG_DEBUG, "Hangup detected\n");
106                                         res = -1;
107                                         break;
108                                 } else if (res == trans->fd) {
109                                         f = ast_read(trans);
110                                         if (!f) {
111                                                 ast_log(LOG_DEBUG, "Null frame == hangup() detected\n");
112                                                 res = -1;
113                                                 break;
114                                         }
115                                         if (f->frametype == AST_FRAME_DTMF) {
116                                                 ast_log(LOG_DEBUG, "User pressed a key\n");
117                                                 ast_frfree(f);
118                                                 res = 0;
119                                                 break;
120                                         }
121                                         ast_frfree(f);
122                                 } else if (res == fds[0]) {
123                                         gettimeofday(&tv, NULL);
124                                         if (last.tv_sec || last.tv_usec) {
125                                                 /* We should wait at least a frame length */
126                                                 us = sizeof(myf.frdata) / 16 * 1000;
127                                                 /* Subtract 1,000,000 us for each second late we've passed */
128                                                 us -= (tv.tv_sec - last.tv_sec) * 1000000;
129                                                 /* And one for each us late we've passed */
130                                                 us -= (tv.tv_usec - last.tv_usec);
131                                                 /* Sleep that long if needed */
132                                                 if (us > 0)
133                                                         usleep(us);
134                                         }
135                                         last = tv;
136                                         res = read(fds[0], myf.frdata, sizeof(myf.frdata));
137                                         if (res > 0) {
138                                                 myf.f.frametype = AST_FRAME_VOICE;
139                                                 myf.f.subclass = AST_FORMAT_SLINEAR;
140                                                 myf.f.datalen = res;
141                                                 myf.f.timelen = res / 16;
142                                                 myf.f.mallocd = 0;
143                                                 myf.f.offset = AST_FRIENDLY_OFFSET;
144                                                 myf.f.src = __PRETTY_FUNCTION__;
145                                                 myf.f.data = myf.frdata;
146                                                 if (ast_write(trans, &myf.f) < 0) {
147                                                         res = -1;
148                                                         break;
149                                                 }
150                                         } else {
151                                                 ast_log(LOG_DEBUG, "No more mp3\n");
152                                                 res = 0;
153                                         }
154                                 } else {
155                                         ast_log(LOG_DEBUG, "HuhHHH?\n");
156                                         res = -1;
157                                         break;
158                                 }
159                         }
160                         kill(pid, SIGTERM);
161                 }
162                 if (trans != chan) 
163                         ast_translator_destroy(trans);
164         } else 
165                 ast_log(LOG_WARNING, "No translator channel available\n");
166         close(fds[0]);
167         close(fds[1]);
168         LOCAL_USER_REMOVE(u);
169         return res;
170 }
171
172 int unload_module(void)
173 {
174         STANDARD_HANGUP_LOCALUSERS;
175         return ast_unregister_application(app);
176 }
177
178 int load_module(void)
179 {
180         return ast_register_application(app, mp3_exec);
181 }
182
183 char *description(void)
184 {
185         return tdesc;
186 }
187
188 int usecount(void)
189 {
190         int res;
191         STANDARD_USECOUNT(res);
192         return res;
193 }