Merged revisions 24019 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 <string.h>
29 #include <stdio.h>
30 #include <signal.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <sys/time.h>
35 #include <errno.h>
36
37 #include "asterisk.h"
38
39 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
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 LOCAL_USER_DECL;
64
65 static int icesencode(char *filename, int fd)
66 {
67         int res;
68         int x;
69         res = fork();
70         if (res < 0) 
71                 ast_log(LOG_WARNING, "Fork failed\n");
72         if (res)
73                 return res;
74         if (ast_opt_high_priority)
75                 ast_set_priority(0);
76         dup2(fd, STDIN_FILENO);
77         for (x=STDERR_FILENO + 1;x<256;x++) {
78                 if ((x != STDIN_FILENO) && (x != STDOUT_FILENO))
79                         close(x);
80         }
81         /* Most commonly installed in /usr/local/bin */
82         execl(ICES, "ices", filename, (char *)NULL);
83         /* But many places has it in /usr/bin */
84         execl(LOCAL_ICES, "ices", filename, (char *)NULL);
85         /* As a last-ditch effort, try to use PATH */
86         execlp("ices", "ices", filename, (char *)NULL);
87         ast_log(LOG_WARNING, "Execute of ices failed\n");
88         return -1;
89 }
90
91 static int ices_exec(struct ast_channel *chan, void *data)
92 {
93         int res=0;
94         struct localuser *u;
95         int fds[2];
96         int ms = -1;
97         int pid = -1;
98         int flags;
99         int oreadformat;
100         struct timeval last;
101         struct ast_frame *f;
102         char filename[256]="";
103         char *c;
104
105         if (ast_strlen_zero(data)) {
106                 ast_log(LOG_WARNING, "ICES requires an argument (configfile.xml)\n");
107                 return -1;
108         }
109
110         LOCAL_USER_ADD(u);
111         
112         last = ast_tv(0, 0);
113         
114         if (pipe(fds)) {
115                 ast_log(LOG_WARNING, "Unable to create pipe\n");
116                 LOCAL_USER_REMOVE(u);
117                 return -1;
118         }
119         flags = fcntl(fds[1], F_GETFL);
120         fcntl(fds[1], F_SETFL, flags | O_NONBLOCK);
121         
122         ast_stopstream(chan);
123
124         if (chan->_state != AST_STATE_UP)
125                 res = ast_answer(chan);
126                 
127         if (res) {
128                 close(fds[0]);
129                 close(fds[1]);
130                 ast_log(LOG_WARNING, "Answer failed!\n");
131                 LOCAL_USER_REMOVE(u);
132                 return -1;
133         }
134
135         oreadformat = chan->readformat;
136         res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
137         if (res < 0) {
138                 close(fds[0]);
139                 close(fds[1]);
140                 ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
141                 LOCAL_USER_REMOVE(u);
142                 return -1;
143         }
144         if (((char *)data)[0] == '/')
145                 strncpy(filename, (char *)data, sizeof(filename) - 1);
146         else
147                 snprintf(filename, sizeof(filename), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, (char *)data);
148         /* Placeholder for options */           
149         c = strchr(filename, '|');
150         if (c)
151                 *c = '\0';      
152         res = icesencode(filename, fds[0]);
153         close(fds[0]);
154         if (res >= 0) {
155                 pid = res;
156                 for (;;) {
157                         /* Wait for audio, and stream */
158                         ms = ast_waitfor(chan, -1);
159                         if (ms < 0) {
160                                 ast_log(LOG_DEBUG, "Hangup detected\n");
161                                 res = -1;
162                                 break;
163                         }
164                         f = ast_read(chan);
165                         if (!f) {
166                                 ast_log(LOG_DEBUG, "Null frame == hangup() detected\n");
167                                 res = -1;
168                                 break;
169                         }
170                         if (f->frametype == AST_FRAME_VOICE) {
171                                 res = write(fds[1], f->data, f->datalen);
172                                 if (res < 0) {
173                                         if (errno != EAGAIN) {
174                                                 ast_log(LOG_WARNING, "Write failed to pipe: %s\n", strerror(errno));
175                                                 res = -1;
176                                                 break;
177                                         }
178                                 }
179                         }
180                         ast_frfree(f);
181                 }
182         }
183         close(fds[1]);
184         
185         if (pid > -1)
186                 kill(pid, SIGKILL);
187         if (!res && oreadformat)
188                 ast_set_read_format(chan, oreadformat);
189
190         LOCAL_USER_REMOVE(u);
191
192         return res;
193 }
194
195 static int unload_module(void *mod)
196 {
197         int res;
198
199         res = ast_unregister_application(app);
200
201         STANDARD_HANGUP_LOCALUSERS;
202
203         return res;
204 }
205
206 static int load_module(void *mod)
207 {
208         return ast_register_application(app, ices_exec, synopsis, descrip);
209 }
210
211 static const char *description(void)
212 {
213         return "Encode and Stream via icecast and ices";
214 }
215
216 static const char *key(void)
217 {
218         return ASTERISK_GPL_KEY;
219 }
220
221 STD_MOD1;