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 rel_id = -1; |
---|
214 | int sflags = ROAR_FLAG_NONE; |
---|
215 | // int codec = ROAR_CODEC_DEFAULT; |
---|
216 | char * server = NULL; |
---|
217 | int fh; |
---|
218 | char * c, * k, * v; |
---|
219 | #ifdef ROAR_HAVE_STRTOK_R |
---|
220 | char * sp0 = NULL, * sp1 = NULL; |
---|
221 | #endif |
---|
222 | int dir = ROAR_DIR_MONITOR; |
---|
223 | int gopher = 0; |
---|
224 | struct roar_connection con; |
---|
225 | struct roar_stream s; |
---|
226 | |
---|
227 | #ifdef ROAR_HAVE_ALARM |
---|
228 | alarm(0); // reset alarm timers from httpd |
---|
229 | #endif |
---|
230 | |
---|
231 | if ( argc > 1 ) |
---|
232 | if ( ! strcmp(argv[1], "--inetd") ) |
---|
233 | #ifdef _CAN_SET_ENV |
---|
234 | if ( (dir = parse_http(&gopher)) == -1 ) |
---|
235 | return 1; |
---|
236 | #else |
---|
237 | return 1; |
---|
238 | #endif |
---|
239 | |
---|
240 | c = getenv("QUERY_STRING"); |
---|
241 | if ( c == NULL ) |
---|
242 | c = ""; |
---|
243 | |
---|
244 | #ifdef ROAR_HAVE_STRTOK_R |
---|
245 | c = strtok_r(c, "&", &sp0); |
---|
246 | #else |
---|
247 | c = strtok(c, "&"); |
---|
248 | #endif |
---|
249 | |
---|
250 | while (c != NULL) { |
---|
251 | #ifdef ROAR_HAVE_STRTOK_R |
---|
252 | k = strtok_r(c, "=", &sp1); |
---|
253 | v = strtok_r(NULL, "=", &sp1); |
---|
254 | #else |
---|
255 | k = c; |
---|
256 | if ( (v = strstr(c, "=")) != NULL ) { |
---|
257 | *v = 0; |
---|
258 | v++; |
---|
259 | } |
---|
260 | #endif |
---|
261 | |
---|
262 | if ( !strcmp(k, "codec") ) { |
---|
263 | if ( (codec = roar_str2codec(v)) == -1 ) |
---|
264 | return 1; |
---|
265 | } else if ( !strcmp(k, "channels") ) { |
---|
266 | channels = atoi(v); |
---|
267 | } else if ( !strcmp(k, "rate") ) { |
---|
268 | rate = atoi(v); |
---|
269 | } else if ( !strcmp(k, "bits") ) { |
---|
270 | bits = atoi(v); |
---|
271 | } else if ( !strcmp(k, "rel-id") || !strcmp(k, "relid") ) { |
---|
272 | rel_id = atoi(v); |
---|
273 | } else if ( !strcmp(k, "set-flag") ) { |
---|
274 | if ( !strcmp(v, "meta") ) { |
---|
275 | sflags |= ROAR_FLAG_META; |
---|
276 | } else if ( !strcmp(v, "cleanmeta") ) { |
---|
277 | sflags |= ROAR_FLAG_CLEANMETA; |
---|
278 | } else if ( !strcmp(v, "prethru") ) { |
---|
279 | sflags |= ROAR_FLAG_PRETHRU; |
---|
280 | } else { |
---|
281 | return 1; |
---|
282 | } |
---|
283 | } else if ( !strcmp(k, "dir") ) { |
---|
284 | if ( (dir = roar_str2dir(v)) == -1 ) |
---|
285 | return 1; |
---|
286 | } else { |
---|
287 | return 1; |
---|
288 | } |
---|
289 | |
---|
290 | #ifdef ROAR_HAVE_STRTOK_R |
---|
291 | c = strtok_r(NULL, "&", &sp0); |
---|
292 | #else |
---|
293 | c = strtok(NULL, "&"); |
---|
294 | #endif |
---|
295 | } |
---|
296 | |
---|
297 | if ( roar_simple_connect(&con, server, "roarmonhttp") == -1 ) { |
---|
298 | return 10; |
---|
299 | } |
---|
300 | |
---|
301 | if ( roar_stream_new(&s, rate, channels, bits, codec) == -1 ) { |
---|
302 | roar_disconnect(&con); |
---|
303 | return 20; |
---|
304 | } |
---|
305 | |
---|
306 | if ( rel_id != -1 ) { |
---|
307 | if ( roar_stream_set_rel_id(&s, rel_id) ) { |
---|
308 | roar_disconnect(&con); |
---|
309 | return 21; |
---|
310 | } |
---|
311 | } |
---|
312 | |
---|
313 | if ( roar_stream_connect(&con, &s, dir) == -1 ) { |
---|
314 | roar_disconnect(&con); |
---|
315 | return 11; |
---|
316 | } |
---|
317 | |
---|
318 | if ( sflags != ROAR_FLAG_NONE ) { |
---|
319 | if ( roar_stream_set_flags(&con, &s, sflags, 0) == -1 ) { |
---|
320 | roar_disconnect(&con); |
---|
321 | return 14; |
---|
322 | } |
---|
323 | } |
---|
324 | |
---|
325 | if ( roar_stream_exec(&con, &s) == -1 ) { |
---|
326 | roar_disconnect(&con); |
---|
327 | return 12; |
---|
328 | } |
---|
329 | |
---|
330 | if ( (fh = roar_get_connection_fh(&con)) == -1 ) |
---|
331 | return 1; |
---|
332 | |
---|
333 | if ( !gopher ) |
---|
334 | print_header(codec, rate, channels); |
---|
335 | |
---|
336 | /* |
---|
337 | while((i = read(fh, buf, BUFSIZE))) |
---|
338 | if (write(out, buf, i) != i) |
---|
339 | break; |
---|
340 | */ |
---|
341 | |
---|
342 | switch (dir) { |
---|
343 | case ROAR_DIR_PLAY: |
---|
344 | stream(fh, ROAR_STDIN); |
---|
345 | break; |
---|
346 | case ROAR_DIR_MONITOR: |
---|
347 | case ROAR_DIR_THRU: |
---|
348 | stream(ROAR_STDOUT, fh); |
---|
349 | break; |
---|
350 | } |
---|
351 | |
---|
352 | roar_simple_close(fh); |
---|
353 | |
---|
354 | return 0; |
---|
355 | } |
---|
356 | |
---|
357 | //ll |
---|