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