Asterisk data retrieval API.
[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         int oreadformat;
115         struct timeval last;
116         struct ast_frame *f;
117         char filename[256]="";
118         char *c;
119
120         if (ast_strlen_zero(data)) {
121                 ast_log(LOG_WARNING, "ICES requires an argument (configfile.xml)\n");
122                 return -1;
123         }
124         
125         last = ast_tv(0, 0);
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 (chan->_state != 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 = chan->readformat;
147         res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
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                 return -1;
153         }
154         if (((char *)data)[0] == '/')
155                 ast_copy_string(filename, (char *) data, sizeof(filename));
156         else
157                 snprintf(filename, sizeof(filename), "%s/%s", ast_config_AST_CONFIG_DIR, (char *)data);
158         /* Placeholder for options */           
159         c = strchr(filename, '|');
160         if (c)
161                 *c = '\0';      
162         res = icesencode(filename, fds[0]);
163         if (res >= 0) {
164                 pid = res;
165                 for (;;) {
166                         /* Wait for audio, and stream */
167                         ms = ast_waitfor(chan, -1);
168                         if (ms < 0) {
169                                 ast_debug(1, "Hangup detected\n");
170                                 res = -1;
171                                 break;
172                         }
173                         f = ast_read(chan);
174                         if (!f) {
175                                 ast_debug(1, "Null frame == hangup() detected\n");
176                                 res = -1;
177                                 break;
178                         }
179                         if (f->frametype == AST_FRAME_VOICE) {
180                                 res = write(fds[1], f->data.ptr, f->datalen);
181                                 if (res < 0) {
182                                         if (errno != EAGAIN) {
183                                                 ast_log(LOG_WARNING, "Write failed to pipe: %s\n", strerror(errno));
184                                                 res = -1;
185                                                 ast_frfree(f);
186                                                 break;
187                                         }
188                                 }
189                         }
190                         ast_frfree(f);
191                 }
192         }
193         close(fds[0]);
194         close(fds[1]);
195
196         if (pid > -1)
197                 kill(pid, SIGKILL);
198         if (!res && oreadformat)
199                 ast_set_read_format(chan, oreadformat);
200
201         return res;
202 }
203
204 static int unload_module(void)
205 {
206         return ast_unregister_application(app);
207 }
208
209 static int load_module(void)
210 {
211         return ast_register_application_xml(app, ices_exec);
212 }
213
214 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Encode and Stream via icecast and ices");