Asterisk media architecture conversion - no more format bitfields
[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 timeval last;
116         struct ast_frame *f;
117         char filename[256]="";
118         char *c;
119
120         ast_format_clear(&oreadformat);
121         if (ast_strlen_zero(data)) {
122                 ast_log(LOG_WARNING, "ICES requires an argument (configfile.xml)\n");
123                 return -1;
124         }
125         
126         last = ast_tv(0, 0);
127         
128         if (pipe(fds)) {
129                 ast_log(LOG_WARNING, "Unable to create pipe\n");
130                 return -1;
131         }
132         flags = fcntl(fds[1], F_GETFL);
133         fcntl(fds[1], F_SETFL, flags | O_NONBLOCK);
134         
135         ast_stopstream(chan);
136
137         if (chan->_state != AST_STATE_UP)
138                 res = ast_answer(chan);
139                 
140         if (res) {
141                 close(fds[0]);
142                 close(fds[1]);
143                 ast_log(LOG_WARNING, "Answer failed!\n");
144                 return -1;
145         }
146
147         ast_format_copy(&oreadformat, &chan->readformat);
148         res = ast_set_read_format_by_id(chan, AST_FORMAT_SLINEAR);
149         if (res < 0) {
150                 close(fds[0]);
151                 close(fds[1]);
152                 ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
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.id)
200                 ast_set_read_format(chan, &oreadformat);
201
202         return res;
203 }
204
205 static int unload_module(void)
206 {
207         return ast_unregister_application(app);
208 }
209
210 static int load_module(void)
211 {
212         return ast_register_application_xml(app, ices_exec);
213 }
214
215 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Encode and Stream via icecast and ices");