4ca4b67c575fabd9495a0ab374026a3ec8af0ad3
[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  * ICES - http://www.icecast.org/ices.php
26  *
27  * \ingroup applications
28  */
29
30 /*** MODULEINFO
31         <support_level>extended</support_level>
32  ***/
33  
34 #include "asterisk.h"
35
36 #include <signal.h>
37 #include <fcntl.h>
38 #include <sys/time.h>
39
40 #include "asterisk/paths.h"     /* use ast_config_AST_CONFIG_DIR */
41 #include "asterisk/lock.h"
42 #include "asterisk/file.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/app.h"
49 #include "asterisk/format_cache.h"
50
51 /*** DOCUMENTATION
52         <application name="ICES" language="en_US">
53                 <synopsis>
54                         Encode and stream using 'ices'.
55                 </synopsis>
56                 <syntax>
57                         <parameter name="config" required="true">
58                                 <para>ICES configuration file.</para>
59                         </parameter>
60                 </syntax>
61                 <description>
62                         <para>Streams to an icecast server using ices (available separately).
63                         A configuration file must be supplied for ices (see contrib/asterisk-ices.xml).</para>
64                         <note><para>ICES version 2 client and server required.</para></note>
65                 </description>
66         </application>
67
68  ***/
69
70 #define path_BIN "/usr/bin/"
71 #define path_LOCAL "/usr/local/bin/"
72
73 static char *app = "ICES";
74
75 static int icesencode(char *filename, int fd)
76 {
77         int res;
78
79         res = ast_safe_fork(0);
80         if (res < 0) 
81                 ast_log(LOG_WARNING, "Fork failed\n");
82         if (res) {
83                 return res;
84         }
85
86         if (ast_opt_high_priority)
87                 ast_set_priority(0);
88         dup2(fd, STDIN_FILENO);
89         ast_close_fds_above_n(STDERR_FILENO);
90
91         /* Most commonly installed in /usr/local/bin 
92          * But many places has it in /usr/bin 
93          * As a last-ditch effort, try to use PATH
94          */
95         execl(path_LOCAL "ices2", "ices", filename, SENTINEL);
96         execl(path_BIN "ices2", "ices", filename, SENTINEL);
97         execlp("ices2", "ices", filename, SENTINEL);
98
99         ast_debug(1, "Couldn't find ices version 2, attempting to use ices version 1.\n");
100
101         execl(path_LOCAL "ices", "ices", filename, SENTINEL);
102         execl(path_BIN "ices", "ices", filename, SENTINEL);
103         execlp("ices", "ices", filename, SENTINEL);
104
105         ast_log(LOG_WARNING, "Execute of ices failed, could not find command.\n");
106         close(fd);
107         _exit(0);
108 }
109
110 static int ices_exec(struct ast_channel *chan, const char *data)
111 {
112         int res = 0;
113         int fds[2];
114         int ms = -1;
115         int pid = -1;
116         int flags;
117         struct ast_format *oreadformat;
118         struct ast_frame *f;
119         char filename[256]="";
120         char *c;
121
122         if (ast_strlen_zero(data)) {
123                 ast_log(LOG_WARNING, "ICES requires an argument (configfile.xml)\n");
124                 return -1;
125         }
126         
127         if (pipe(fds)) {
128                 ast_log(LOG_WARNING, "Unable to create pipe\n");
129                 return -1;
130         }
131         flags = fcntl(fds[1], F_GETFL);
132         fcntl(fds[1], F_SETFL, flags | O_NONBLOCK);
133         
134         ast_stopstream(chan);
135
136         if (ast_channel_state(chan) != AST_STATE_UP)
137                 res = ast_answer(chan);
138                 
139         if (res) {
140                 close(fds[0]);
141                 close(fds[1]);
142                 ast_log(LOG_WARNING, "Answer failed!\n");
143                 return -1;
144         }
145
146         oreadformat = ao2_bump(ast_channel_readformat(chan));
147         res = ast_set_read_format(chan, ast_format_slin);
148         if (res < 0) {
149                 close(fds[0]);
150                 close(fds[1]);
151                 ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
152                 ao2_cleanup(oreadformat);
153                 return -1;
154         }
155         if (((char *)data)[0] == '/')
156                 ast_copy_string(filename, (char *) data, sizeof(filename));
157         else
158                 snprintf(filename, sizeof(filename), "%s/%s", ast_config_AST_CONFIG_DIR, (char *)data);
159         /* Placeholder for options */           
160         c = strchr(filename, '|');
161         if (c)
162                 *c = '\0';      
163         res = icesencode(filename, fds[0]);
164         if (res >= 0) {
165                 pid = res;
166                 for (;;) {
167                         /* Wait for audio, and stream */
168                         ms = ast_waitfor(chan, -1);
169                         if (ms < 0) {
170                                 ast_debug(1, "Hangup detected\n");
171                                 res = -1;
172                                 break;
173                         }
174                         f = ast_read(chan);
175                         if (!f) {
176                                 ast_debug(1, "Null frame == hangup() detected\n");
177                                 res = -1;
178                                 break;
179                         }
180                         if (f->frametype == AST_FRAME_VOICE) {
181                                 res = write(fds[1], f->data.ptr, f->datalen);
182                                 if (res < 0) {
183                                         if (errno != EAGAIN) {
184                                                 ast_log(LOG_WARNING, "Write failed to pipe: %s\n", strerror(errno));
185                                                 res = -1;
186                                                 ast_frfree(f);
187                                                 break;
188                                         }
189                                 }
190                         }
191                         ast_frfree(f);
192                 }
193         }
194         close(fds[0]);
195         close(fds[1]);
196
197         if (pid > -1)
198                 kill(pid, SIGKILL);
199         if (!res && oreadformat)
200                 ast_set_read_format(chan, oreadformat);
201         ao2_cleanup(oreadformat);
202
203         return res;
204 }
205
206 static int unload_module(void)
207 {
208         return ast_unregister_application(app);
209 }
210
211 static int load_module(void)
212 {
213         return ast_register_application_xml(app, ices_exec);
214 }
215
216 AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Encode and Stream via icecast and ices");
217