Start untangling header inclusion in a way that does not affect
[asterisk/asterisk.git] / apps / app_ices.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 Stream to an icecast server via ICES (see contrib/asterisk-ices.xml)
22  *
23  * \author Mark Spencer <markster@digium.com>
24  * 
25  * \extref ICES - http://www.icecast.org/ices.php
26  *
27  * \ingroup applications
28  */
29  
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
34 #include <signal.h>
35 #include <fcntl.h>
36 #include <sys/time.h>
37 #include <errno.h>
38
39 #include "asterisk/lock.h"
40 #include "asterisk/file.h"
41 #include "asterisk/logger.h"
42 #include "asterisk/channel.h"
43 #include "asterisk/frame.h"
44 #include "asterisk/pbx.h"
45 #include "asterisk/module.h"
46 #include "asterisk/translate.h"
47 #include "asterisk/options.h"
48
49 #define ICES "/usr/bin/ices"
50 #define LOCAL_ICES "/usr/local/bin/ices"
51
52 static char *app = "ICES";
53
54 static char *synopsis = "Encode and stream using 'ices'";
55
56 static char *descrip = 
57 "  ICES(config.xml) Streams to an icecast server using ices\n"
58 "(available separately).  A configuration file must be supplied\n"
59 "for ices (see examples/asterisk-ices.conf). \n";
60
61
62 static int icesencode(char *filename, int fd)
63 {
64         int res;
65         int x;
66         sigset_t fullset, oldset;
67
68         sigfillset(&fullset);
69         pthread_sigmask(SIG_BLOCK, &fullset, &oldset);
70
71         res = fork();
72         if (res < 0) 
73                 ast_log(LOG_WARNING, "Fork failed\n");
74         if (res) {
75                 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
76                 return res;
77         }
78
79         /* Stop ignoring PIPE */
80         signal(SIGPIPE, SIG_DFL);
81         pthread_sigmask(SIG_UNBLOCK, &fullset, NULL);
82
83         if (ast_opt_high_priority)
84                 ast_set_priority(0);
85         dup2(fd, STDIN_FILENO);
86         for (x=STDERR_FILENO + 1;x<1024;x++) {
87                 if ((x != STDIN_FILENO) && (x != STDOUT_FILENO))
88                         close(x);
89         }
90         /* Most commonly installed in /usr/local/bin */
91         execl(ICES, "ices", filename, (char *)NULL);
92         /* But many places has it in /usr/bin */
93         execl(LOCAL_ICES, "ices", filename, (char *)NULL);
94         /* As a last-ditch effort, try to use PATH */
95         execlp("ices", "ices", filename, (char *)NULL);
96         ast_log(LOG_WARNING, "Execute of ices failed\n");
97         _exit(0);
98 }
99
100 static int ices_exec(struct ast_channel *chan, void *data)
101 {
102         int res = 0;
103         int fds[2];
104         int ms = -1;
105         int pid = -1;
106         int flags;
107         int oreadformat;
108         struct timeval last;
109         struct ast_frame *f;
110         char filename[256]="";
111         char *c;
112
113         if (ast_strlen_zero(data)) {
114                 ast_log(LOG_WARNING, "ICES requires an argument (configfile.xml)\n");
115                 return -1;
116         }
117         
118         last = ast_tv(0, 0);
119         
120         if (pipe(fds)) {
121                 ast_log(LOG_WARNING, "Unable to create pipe\n");
122                 return -1;
123         }
124         flags = fcntl(fds[1], F_GETFL);
125         fcntl(fds[1], F_SETFL, flags | O_NONBLOCK);
126         
127         ast_stopstream(chan);
128
129         if (chan->_state != AST_STATE_UP)
130                 res = ast_answer(chan);
131                 
132         if (res) {
133                 close(fds[0]);
134                 close(fds[1]);
135                 ast_log(LOG_WARNING, "Answer failed!\n");
136                 return -1;
137         }
138
139         oreadformat = chan->readformat;
140         res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
141         if (res < 0) {
142                 close(fds[0]);
143                 close(fds[1]);
144                 ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
145                 return -1;
146         }
147         if (((char *)data)[0] == '/')
148                 ast_copy_string(filename, (char *) data, sizeof(filename));
149         else
150                 snprintf(filename, sizeof(filename), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, (char *)data);
151         /* Placeholder for options */           
152         c = strchr(filename, '|');
153         if (c)
154                 *c = '\0';      
155         res = icesencode(filename, fds[0]);
156         close(fds[0]);
157         if (res >= 0) {
158                 pid = res;
159                 for (;;) {
160                         /* Wait for audio, and stream */
161                         ms = ast_waitfor(chan, -1);
162                         if (ms < 0) {
163                                 ast_debug(1, "Hangup detected\n");
164                                 res = -1;
165                                 break;
166                         }
167                         f = ast_read(chan);
168                         if (!f) {
169                                 ast_debug(1, "Null frame == hangup() detected\n");
170                                 res = -1;
171                                 break;
172                         }
173                         if (f->frametype == AST_FRAME_VOICE) {
174                                 res = write(fds[1], f->data, f->datalen);
175                                 if (res < 0) {
176                                         if (errno != EAGAIN) {
177                                                 ast_log(LOG_WARNING, "Write failed to pipe: %s\n", strerror(errno));
178                                                 res = -1;
179                                                 ast_frfree(f);
180                                                 break;
181                                         }
182                                 }
183                         }
184                         ast_frfree(f);
185                 }
186         }
187         close(fds[1]);
188         
189         if (pid > -1)
190                 kill(pid, SIGKILL);
191         if (!res && oreadformat)
192                 ast_set_read_format(chan, oreadformat);
193
194         return res;
195 }
196
197 static int unload_module(void)
198 {
199         return ast_unregister_application(app);
200 }
201
202 static int load_module(void)
203 {
204         return ast_register_application(app, ices_exec, synopsis, descrip);
205 }
206
207 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Encode and Stream via icecast and ices");