ensure that the private structure for pseudo channels is created without 'leaking...
[asterisk-bristuff.git] / apps / app_mp3.c
blob55d50f01190d64cc6d3aef6f89490ff62f93906a
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
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.
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.
19 /*! \file
21 * \brief Silly application to play an MP3 file -- uses mpg123
23 * \author Mark Spencer <markster@digium.com>
25 * \ingroup applications
28 #include "asterisk.h"
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
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>
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"
50 #define LOCAL_MPG_123 "/usr/local/bin/mpg123"
51 #define MPG_123 "/usr/bin/mpg123"
53 static char *app = "MP3Player";
55 static char *synopsis = "Play an MP3 file or stream";
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.";
63 static int mp3play(char *filename, int fd)
65 int res;
66 int x;
67 sigset_t fullset, oldset;
69 sigfillset(&fullset);
70 pthread_sigmask(SIG_BLOCK, &fullset, &oldset);
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;
79 if (ast_opt_high_priority)
80 ast_set_priority(0);
81 signal(SIGPIPE, SIG_DFL);
82 pthread_sigmask(SIG_UNBLOCK, &fullset, NULL);
84 dup2(fd, STDOUT_FILENO);
85 for (x=STDERR_FILENO + 1;x<256;x++) {
86 if (x != STDOUT_FILENO)
87 close(x);
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);
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);
106 ast_log(LOG_WARNING, "Execute of mpg123 failed\n");
107 _exit(0);
110 static int timed_read(int fd, void *data, int datalen, int timeout)
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;
121 return read(fd, data, datalen);
125 static int mp3_exec(struct ast_channel *chan, void *data)
127 int res=0;
128 struct ast_module_user *u;
129 int fds[2];
130 int ms = -1;
131 int pid = -1;
132 int owriteformat;
133 int timeout = 2000;
134 struct timeval next;
135 struct ast_frame *f;
136 struct myframe {
137 struct ast_frame f;
138 char offset[AST_FRIENDLY_OFFSET];
139 short frdata[160];
140 } myf;
142 if (ast_strlen_zero(data)) {
143 ast_log(LOG_WARNING, "MP3 Playback requires an argument (filename)\n");
144 return -1;
147 u = ast_module_user_add(chan);
149 if (pipe(fds)) {
150 ast_log(LOG_WARNING, "Unable to create pipe\n");
151 ast_module_user_remove(u);
152 return -1;
155 ast_stopstream(chan);
157 owriteformat = chan->writeformat;
158 res = ast_set_write_format(chan, AST_FORMAT_SLINEAR);
159 if (res < 0) {
160 ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
161 ast_module_user_remove(u);
162 return -1;
165 res = mp3play((char *)data, fds[1]);
166 if (!strncasecmp((char *)data, "http://", 7)) {
167 timeout = 10000;
169 /* Wait 1000 ms first */
170 next = ast_tvnow();
171 next.tv_sec += 1;
172 if (res >= 0) {
173 pid = res;
174 /* Order is important -- there's almost always going to be mp3... we want to prioritize the
175 user */
176 for (;;) {
177 ms = ast_tvdiff_ms(next, ast_tvnow());
178 if (ms <= 0) {
179 res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata), timeout);
180 if (res > 0) {
181 myf.f.frametype = AST_FRAME_VOICE;
182 myf.f.subclass = AST_FORMAT_SLINEAR;
183 myf.f.datalen = res;
184 myf.f.samples = res / 2;
185 myf.f.mallocd = 0;
186 myf.f.offset = AST_FRIENDLY_OFFSET;
187 myf.f.src = __PRETTY_FUNCTION__;
188 myf.f.delivery.tv_sec = 0;
189 myf.f.delivery.tv_usec = 0;
190 myf.f.data = myf.frdata;
191 if (ast_write(chan, &myf.f) < 0) {
192 res = -1;
193 break;
195 } else {
196 ast_log(LOG_DEBUG, "No more mp3\n");
197 res = 0;
198 break;
200 next = ast_tvadd(next, ast_samp2tv(myf.f.samples, 8000));
201 } else {
202 ms = ast_waitfor(chan, ms);
203 if (ms < 0) {
204 ast_log(LOG_DEBUG, "Hangup detected\n");
205 res = -1;
206 break;
208 if (ms) {
209 f = ast_read(chan);
210 if (!f) {
211 ast_log(LOG_DEBUG, "Null frame == hangup() detected\n");
212 res = -1;
213 break;
215 if (f->frametype == AST_FRAME_DTMF) {
216 ast_log(LOG_DEBUG, "User pressed a key\n");
217 ast_frfree(f);
218 res = 0;
219 break;
221 ast_frfree(f);
226 close(fds[0]);
227 close(fds[1]);
229 if (pid > -1)
230 kill(pid, SIGKILL);
231 if (!res && owriteformat)
232 ast_set_write_format(chan, owriteformat);
234 ast_module_user_remove(u);
236 return res;
239 static int unload_module(void)
241 int res;
243 res = ast_unregister_application(app);
245 ast_module_user_hangup_all();
247 return res;
250 static int load_module(void)
252 return ast_register_application(app, mp3_exec, synopsis, descrip);
255 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Silly MP3 Application");