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