1 | //roarmonhttp.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008 |
---|
5 | * |
---|
6 | * This file is part of roarclients a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * RoarAudio is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
22 | * |
---|
23 | */ |
---|
24 | |
---|
25 | #include <roaraudio.h> |
---|
26 | |
---|
27 | #if defined(ROAR_HAVE_SETENV) || defined(ROAR_HAVE_PUTENV) |
---|
28 | #define _CAN_SET_ENV |
---|
29 | #endif |
---|
30 | |
---|
31 | #define BUFSIZE 1024 |
---|
32 | |
---|
33 | void print_header (int codec, int rate, int channels) { |
---|
34 | char * mime = "application/octet-stream"; |
---|
35 | |
---|
36 | switch (codec) { |
---|
37 | case ROAR_CODEC_OGG_VORBIS: |
---|
38 | mime = "application/ogg"; |
---|
39 | break; |
---|
40 | case ROAR_CODEC_RIFF_WAVE: |
---|
41 | mime = "audio/x-wav"; |
---|
42 | break; |
---|
43 | } |
---|
44 | |
---|
45 | printf("Content-type: %s\r\n", mime); |
---|
46 | printf("ice-audio-info: ice-samplerate=%i;ice-channels=%i\r\n", rate, channels); |
---|
47 | printf("icy-pub:0\r\n"); |
---|
48 | printf("Server: RoarAudio (roarmonhttp $Revision$)\r\n"); |
---|
49 | printf("\r\n"); |
---|
50 | |
---|
51 | fflush(stdout); |
---|
52 | } |
---|
53 | |
---|
54 | int stream (int dest, int src) { |
---|
55 | struct roar_buffer *ring = NULL, *cur; |
---|
56 | ssize_t len; |
---|
57 | size_t todo; |
---|
58 | fd_set fsi[1], fso[1]; |
---|
59 | struct timeval tv; |
---|
60 | int alive = 1; |
---|
61 | int maxfh = (dest > src ? dest : src) + 1; |
---|
62 | void * data; |
---|
63 | |
---|
64 | roar_socket_nonblock(src, ROAR_SOCKET_NONBLOCK); |
---|
65 | roar_socket_nonblock(dest, ROAR_SOCKET_NONBLOCK); |
---|
66 | |
---|
67 | while (alive) { |
---|
68 | FD_ZERO(fsi); |
---|
69 | FD_ZERO(fso); |
---|
70 | FD_SET(src, fsi); |
---|
71 | if ( ring != NULL ) { |
---|
72 | FD_SET(dest, fso); |
---|
73 | } |
---|
74 | |
---|
75 | tv.tv_sec = 0; |
---|
76 | tv.tv_usec = 100000; // 100ms |
---|
77 | |
---|
78 | if (select(maxfh, fsi, fso, NULL, &tv) > 0) { |
---|
79 | if ( FD_ISSET(src, fsi) ) { // we can read! |
---|
80 | if ( roar_buffer_new(&cur, BUFSIZE) == -1 ) |
---|
81 | return -1; |
---|
82 | |
---|
83 | if ( roar_buffer_get_data(cur, &data) == -1 ) |
---|
84 | return -1; |
---|
85 | |
---|
86 | len = read(src, data, BUFSIZE); |
---|
87 | |
---|
88 | switch (len) { |
---|
89 | case 0: |
---|
90 | case -1: |
---|
91 | roar_buffer_free(cur); |
---|
92 | |
---|
93 | if ( ring != NULL ) |
---|
94 | roar_buffer_free(ring); |
---|
95 | |
---|
96 | return -1; |
---|
97 | break; |
---|
98 | } |
---|
99 | |
---|
100 | if ( roar_buffer_set_len(cur, len) == -1 ) |
---|
101 | return -1; |
---|
102 | |
---|
103 | if ( ring == NULL ) { |
---|
104 | ring = cur; |
---|
105 | } else { |
---|
106 | roar_buffer_add(ring, cur); |
---|
107 | } |
---|
108 | } else if ( FD_ISSET(dest, fso) && ring != NULL ) { // we can write! |
---|
109 | if ( roar_buffer_get_data(ring, &data) == -1 ) |
---|
110 | return -1; |
---|
111 | |
---|
112 | if ( roar_buffer_get_len(ring, &todo) == -1 ) |
---|
113 | return -1; |
---|
114 | |
---|
115 | len = write(dest, data, todo); |
---|
116 | |
---|
117 | if ( len < 1 ) { |
---|
118 | if ( errno != EAGAIN ) { |
---|
119 | roar_buffer_free(ring); |
---|
120 | return -1; |
---|
121 | } |
---|
122 | } |
---|
123 | |
---|
124 | if ( todo == len ) { // we wrote all of the pkg |
---|
125 | if ( roar_buffer_next(&ring) == -1 ) |
---|
126 | return -1; |
---|
127 | } else { |
---|
128 | if ( roar_buffer_set_offset(ring, len) == -1 ) |
---|
129 | return -1; |
---|
130 | } |
---|
131 | |
---|
132 | } |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | return 0; |
---|
137 | } |
---|
138 | |
---|
139 | #ifdef _CAN_SET_ENV |
---|
140 | int parse_http (int * gopher) { |
---|
141 | char buf[1024]; |
---|
142 | char * qs = buf, *str; |
---|
143 | ssize_t len; |
---|
144 | int dir = ROAR_DIR_MONITOR; |
---|
145 | |
---|
146 | if ( (len = read(ROAR_STDIN, buf, 1023)) == -1 ) |
---|
147 | return -1; |
---|
148 | |
---|
149 | buf[len] = 0; |
---|
150 | |
---|
151 | if ( strncmp(buf, "GET /", 5) ) { |
---|
152 | if ( strncmp(buf, "SOURCE /", 8) ) { |
---|
153 | if ( buf[0] != '/' ) { |
---|
154 | return -1; |
---|
155 | } else { |
---|
156 | *gopher = 1; |
---|
157 | } |
---|
158 | } else { |
---|
159 | dir = ROAR_DIR_PLAY; |
---|
160 | qs += 3; |
---|
161 | } |
---|
162 | } |
---|
163 | |
---|
164 | if ( !*gopher ) { |
---|
165 | qs += 5; |
---|
166 | |
---|
167 | if ( (str = strstr(qs, " ")) == NULL ) |
---|
168 | return -1; |
---|
169 | |
---|
170 | *str = 0; |
---|
171 | } else { |
---|
172 | if ( (str = strstr(qs, "\r")) != NULL ) |
---|
173 | *str = 0; |
---|
174 | if ( (str = strstr(qs, "\n")) != NULL ) |
---|
175 | *str = 0; |
---|
176 | } |
---|
177 | |
---|
178 | for (; *qs != '?'; qs++) |
---|
179 | if ( !*qs ) |
---|
180 | break; |
---|
181 | |
---|
182 | if ( *qs == '?' ) |
---|
183 | qs++; |
---|
184 | |
---|
185 | if ( !*gopher ) |
---|
186 | printf("HTTP/1.0 200 OK\r\n"); |
---|
187 | // printf("QS: %s\r\n", qs); |
---|
188 | |
---|
189 | fflush(stdout); |
---|
190 | |
---|
191 | #ifdef ROAR_HAVE_SETENV |
---|
192 | setenv("QUERY_STRING", qs, 1); |
---|
193 | #else |
---|
194 | // TODO: does this leak memory? |
---|
195 | if ( (str = malloc(strlen(qs) + strlen("QUERY_STRING=") + 1)) == NULL ) { |
---|
196 | return -1; |
---|
197 | } |
---|
198 | |
---|
199 | sprintf(str, "QUERY_STRING=%s", qs); |
---|
200 | |
---|
201 | putenv(str); |
---|
202 | #endif |
---|
203 | |
---|
204 | return dir; |
---|
205 | } |
---|
206 | #endif |
---|
207 | |
---|
208 | int main (int argc, char * argv[]) { |
---|
209 | int rate = 44100; |
---|
210 | int bits = 16; |
---|
211 | int channels = 2; |
---|
212 | int codec = ROAR_CODEC_OGG_VORBIS; |
---|
213 | // int codec = ROAR_CODEC_DEFAULT; |
---|
214 | char * server = NULL; |
---|
215 | int fh; |
---|
216 | char * c, * k, * v; |
---|
217 | #ifdef ROAR_HAVE_STRTOK_R |
---|
218 | char * sp0 = NULL, * sp1 = NULL; |
---|
219 | #endif |
---|
220 | int dir = ROAR_DIR_MONITOR; |
---|
221 | int gopher = 0; |
---|
222 | |
---|
223 | #ifdef ROAR_HAVE_ALARM |
---|
224 | alarm(0); // reset alarm timers from httpd |
---|
225 | #endif |
---|
226 | |
---|
227 | if ( argc > 1 ) |
---|
228 | if ( ! strcmp(argv[1], "--inetd") ) |
---|
229 | #ifdef _CAN_SET_ENV |
---|
230 | if ( (dir = parse_http(&gopher)) == -1 ) |
---|
231 | return 1; |
---|
232 | #else |
---|
233 | return 1; |
---|
234 | #endif |
---|
235 | |
---|
236 | #ifdef ROAR_HAVE_STRTOK_R |
---|
237 | c = strtok_r(getenv("QUERY_STRING"), "&", &sp0); |
---|
238 | #else |
---|
239 | c = strtok(getenv("QUERY_STRING"), "&"); |
---|
240 | #endif |
---|
241 | |
---|
242 | while (c != NULL) { |
---|
243 | #ifdef ROAR_HAVE_STRTOK_R |
---|
244 | k = strtok_r(c, "=", &sp1); |
---|
245 | v = strtok_r(NULL, "=", &sp1); |
---|
246 | #else |
---|
247 | k = c; |
---|
248 | if ( (v = strstr(c, "=")) != NULL ) { |
---|
249 | *v = 0; |
---|
250 | v++; |
---|
251 | } |
---|
252 | #endif |
---|
253 | |
---|
254 | if ( !strcmp(k, "codec") ) { |
---|
255 | if ( (codec = roar_str2codec(v)) == -1 ) |
---|
256 | return 1; |
---|
257 | } else if ( !strcmp(k, "channels") ) { |
---|
258 | channels = atoi(v); |
---|
259 | } else if ( !strcmp(k, "rate") ) { |
---|
260 | rate = atoi(v); |
---|
261 | } else if ( !strcmp(k, "bits") ) { |
---|
262 | bits = atoi(v); |
---|
263 | } else { |
---|
264 | return 1; |
---|
265 | } |
---|
266 | |
---|
267 | #ifdef ROAR_HAVE_STRTOK_R |
---|
268 | c = strtok_r(NULL, "&", &sp0); |
---|
269 | #else |
---|
270 | c = strtok(NULL, "&"); |
---|
271 | #endif |
---|
272 | } |
---|
273 | |
---|
274 | |
---|
275 | if ( (fh = roar_simple_stream(rate, channels, bits, codec, server, dir, "roarmonhttp")) == -1 ) { |
---|
276 | // fprintf(stderr, "Error: can not start monitoring\n"); |
---|
277 | return 1; |
---|
278 | } |
---|
279 | |
---|
280 | if ( !gopher ) |
---|
281 | print_header(codec, rate, channels); |
---|
282 | |
---|
283 | /* |
---|
284 | while((i = read(fh, buf, BUFSIZE))) |
---|
285 | if (write(out, buf, i) != i) |
---|
286 | break; |
---|
287 | */ |
---|
288 | |
---|
289 | switch (dir) { |
---|
290 | case ROAR_DIR_PLAY: |
---|
291 | stream(fh, ROAR_STDIN); |
---|
292 | break; |
---|
293 | case ROAR_DIR_MONITOR: |
---|
294 | stream(ROAR_STDOUT, fh); |
---|
295 | break; |
---|
296 | } |
---|
297 | |
---|
298 | roar_simple_close(fh); |
---|
299 | |
---|
300 | return 0; |
---|
301 | } |
---|
302 | |
---|
303 | //ll |
---|