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