bdccff43f700d33e7bd6c8a8577ab889db50cf1a
[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
52 /*** DOCUMENTATION
53         <application name="ICES" language="en_US">
54                 <synopsis>
55                         Encode and stream using 'ices'.
56                 </synopsis>
57                 <syntax>
58                         <parameter name="config" required="true">
59                                 <para>ICES configuration file.</para>
60                         </parameter>
61                 </syntax>
62                 <description>
63                         <para>Streams to an icecast server using ices (available separately).
64                         A configuration file must be supplied for ices (see contrib/asterisk-ices.xml).</para>
65                         <note><para>ICES version 2 client and server required.</para></note>
66                 </description>
67         </application>
68
69  ***/
70
71 #define path_BIN "/usr/bin/"
72 #define path_LOCAL "/usr/local/bin/"
73
74 static char *app = "ICES";
75
76 static int icesencode(char *filename, int fd)
77 {
78         int res;
79
80         res = ast_safe_fork(0);
81         if (res < 0) 
82                 ast_log(LOG_WARNING, "Fork failed\n");
83         if (res) {
84                 return res;
85         }
86
87         if (ast_opt_high_priority)
88                 ast_set_priority(0);
89         dup2(fd, STDIN_FILENO);
90         ast_close_fds_above_n(STDERR_FILENO);
91
92         /* Most commonly installed in /usr/local/bin 
93          * But many places has it in /usr/bin 
94          * As a last-ditch effort, try to use PATH
95          */
96         execl(path_LOCAL "ices2", "ices", filename, SENTINEL);
97         execl(path_BIN "ices2", "ices", filename, SENTINEL);
98         execlp("ices2", "ices", filename, SENTINEL);
99
100         ast_debug(1, "Couldn't find ices version 2, attempting to use ices version 1.\n");
101
102         execl(path_LOCAL "ices", "ices", filename, SENTINEL);
103         execl(path_BIN "ices", "ices", filename, SENTINEL);
104         execlp("ices", "ices", filename, SENTINEL);
105
106         ast_log(LOG_WARNING, "Execute of ices failed, could not find command.\n");
107         close(fd);
108         _exit(0);
109 }
110
111 static int ices_exec(struct ast_channel *chan, const char *data)
112 {
113         int res = 0;
114         int fds[2];
115         int ms = -1;
116         int pid = -1;
117         int flags;
118         struct ast_format oreadformat;
119         struct ast_frame *f;
120         char filename[256]="";
121         char *c;
122
123         ast_format_clear(&oreadformat);
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         ast_format_copy(&oreadformat, ast_channel_readformat(chan));
149         res = ast_set_read_format_by_id(chan, AST_FORMAT_SLINEAR);
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                 return -1;
155         }
156         if (((char *)data)[0] == '/')
157                 ast_copy_string(filename, (char *) data, sizeof(filename));
158         else
159                 snprintf(filename, sizeof(filename), "%s/%s", ast_config_AST_CONFIG_DIR, (char *)data);
160         /* Placeholder for options */           
161         c = strchr(filename, '|');
162         if (c)
163                 *c = '\0';      
164         res = icesencode(filename, fds[0]);
165         if (res >= 0) {
166                 pid = res;
167                 for (;;) {
168                         /* Wait for audio, and stream */
169                         ms = ast_waitfor(chan, -1);
170                         if (ms < 0) {
171                                 ast_debug(1, "Hangup detected\n");
172                                 res = -1;
173                                 break;
174                         }
175                         f = ast_read(chan);
176                         if (!f) {
177                                 ast_debug(1, "Null frame == hangup() detected\n");
178                                 res = -1;
179                                 break;
180                         }
181                         if (f->frametype == AST_FRAME_VOICE) {
182                                 res = write(fds[1], f->data.ptr, f->datalen);
183                                 if (res < 0) {
184                                         if (errno != EAGAIN) {
185                                                 ast_log(LOG_WARNING, "Write failed to pipe: %s\n", strerror(errno));
186                                                 res = -1;
187                                                 ast_frfree(f);
188                                                 break;
189                                         }
190                                 }
191                         }
192                         ast_frfree(f);
193                 }
194         }
195         close(fds[0]);
196         close(fds[1]);
197
198         if (pid > -1)
199                 kill(pid, SIGKILL);
200         if (!res && oreadformat.id)
201                 ast_set_read_format(chan, &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(ASTERISK_GPL_KEY, "Encode and Stream via icecast and ices");