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