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