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