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