Adding reference to ices home page. Anyone that has tested with ices2 ?
[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 <string.h>
35 #include <stdio.h>
36 #include <signal.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include <sys/time.h>
41 #include <errno.h>
42
43 #include "asterisk/lock.h"
44 #include "asterisk/file.h"
45 #include "asterisk/logger.h"
46 #include "asterisk/channel.h"
47 #include "asterisk/frame.h"
48 #include "asterisk/pbx.h"
49 #include "asterisk/module.h"
50 #include "asterisk/translate.h"
51 #include "asterisk/options.h"
52
53 #define ICES "/usr/bin/ices"
54 #define LOCAL_ICES "/usr/local/bin/ices"
55
56 static char *app = "ICES";
57
58 static char *synopsis = "Encode and stream using 'ices'";
59
60 static char *descrip = 
61 "  ICES(config.xml) Streams to an icecast server using ices\n"
62 "(available separately).  A configuration file must be supplied\n"
63 "for ices (see examples/asterisk-ices.conf). \n";
64
65
66 static int icesencode(char *filename, int fd)
67 {
68         int res;
69         int x;
70         sigset_t fullset, oldset;
71
72         sigfillset(&fullset);
73         pthread_sigmask(SIG_BLOCK, &fullset, &oldset);
74
75         res = fork();
76         if (res < 0) 
77                 ast_log(LOG_WARNING, "Fork failed\n");
78         if (res) {
79                 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
80                 return res;
81         }
82
83         /* Stop ignoring PIPE */
84         signal(SIGPIPE, SIG_DFL);
85         pthread_sigmask(SIG_UNBLOCK, &fullset, NULL);
86
87         if (ast_opt_high_priority)
88                 ast_set_priority(0);
89         dup2(fd, STDIN_FILENO);
90         for (x=STDERR_FILENO + 1;x<1024;x++) {
91                 if ((x != STDIN_FILENO) && (x != STDOUT_FILENO))
92                         close(x);
93         }
94         /* Most commonly installed in /usr/local/bin */
95         execl(ICES, "ices", filename, (char *)NULL);
96         /* But many places has it in /usr/bin */
97         execl(LOCAL_ICES, "ices", filename, (char *)NULL);
98         /* As a last-ditch effort, try to use PATH */
99         execlp("ices", "ices", filename, (char *)NULL);
100         ast_log(LOG_WARNING, "Execute of ices failed\n");
101         _exit(0);
102 }
103
104 static int ices_exec(struct ast_channel *chan, void *data)
105 {
106         int res=0;
107         struct ast_module_user *u;
108         int fds[2];
109         int ms = -1;
110         int pid = -1;
111         int flags;
112         int oreadformat;
113         struct timeval last;
114         struct ast_frame *f;
115         char filename[256]="";
116         char *c;
117
118         if (ast_strlen_zero(data)) {
119                 ast_log(LOG_WARNING, "ICES requires an argument (configfile.xml)\n");
120                 return -1;
121         }
122
123         u = ast_module_user_add(chan);
124         
125         last = ast_tv(0, 0);
126         
127         if (pipe(fds)) {
128                 ast_log(LOG_WARNING, "Unable to create pipe\n");
129                 ast_module_user_remove(u);
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                 ast_module_user_remove(u);
145                 return -1;
146         }
147
148         oreadformat = chan->readformat;
149         res = ast_set_read_format(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                 ast_module_user_remove(u);
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", (char *)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         close(fds[0]);
167         if (res >= 0) {
168                 pid = res;
169                 for (;;) {
170                         /* Wait for audio, and stream */
171                         ms = ast_waitfor(chan, -1);
172                         if (ms < 0) {
173                                 if (option_debug)
174                                         ast_log(LOG_DEBUG, "Hangup detected\n");
175                                 res = -1;
176                                 break;
177                         }
178                         f = ast_read(chan);
179                         if (!f) {
180                                 if (option_debug)
181                                         ast_log(LOG_DEBUG, "Null frame == hangup() detected\n");
182                                 res = -1;
183                                 break;
184                         }
185                         if (f->frametype == AST_FRAME_VOICE) {
186                                 res = write(fds[1], f->data, f->datalen);
187                                 if (res < 0) {
188                                         if (errno != EAGAIN) {
189                                                 ast_log(LOG_WARNING, "Write failed to pipe: %s\n", strerror(errno));
190                                                 res = -1;
191                                                 ast_frfree(f);
192                                                 break;
193                                         }
194                                 }
195                         }
196                         ast_frfree(f);
197                 }
198         }
199         close(fds[1]);
200         
201         if (pid > -1)
202                 kill(pid, SIGKILL);
203         if (!res && oreadformat)
204                 ast_set_read_format(chan, oreadformat);
205
206         ast_module_user_remove(u);
207
208         return res;
209 }
210
211 static int unload_module(void)
212 {
213         int res;
214
215         res = ast_unregister_application(app);
216
217         ast_module_user_hangup_all();
218
219         return res;
220 }
221
222 static int load_module(void)
223 {
224         return ast_register_application(app, ices_exec, synopsis, descrip);
225 }
226
227 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Encode and Stream via icecast and ices");