1 | //roarmonhttp.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2012 |
---|
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, 51 Franklin Street, Fifth Floor, |
---|
22 | * Boston, MA 02110-1301, USA. |
---|
23 | * |
---|
24 | */ |
---|
25 | |
---|
26 | /* ckport options: |
---|
27 | * ckport: ignore-symbol: alarm of target win32 |
---|
28 | * checked by configure. Needed to clear timeout some small HTTPds implement |
---|
29 | * to avoid problems with bad CGI scripts. |
---|
30 | */ |
---|
31 | |
---|
32 | #include <roaraudio.h> |
---|
33 | |
---|
34 | #if defined(ROAR_HAVE_SETENV) || defined(ROAR_HAVE_PUTENV) |
---|
35 | #define _CAN_SET_ENV |
---|
36 | #endif |
---|
37 | |
---|
38 | #define BUFSIZE 1024 |
---|
39 | |
---|
40 | void usage (void) { |
---|
41 | printf("roarmonhttp [OPTIONS]...\n"); |
---|
42 | |
---|
43 | printf("\nOptions:\n\n"); |
---|
44 | |
---|
45 | printf(" --server SERVER - Set server hostname\n" |
---|
46 | " --rate -R RATE - Set sample rate\n" |
---|
47 | " --bits -B BITS - Set bits per sample\n" |
---|
48 | " --chans -C CHANNELS - Set number of channels\n" |
---|
49 | " --codec CODEC - Set the codec\n" |
---|
50 | " --rel-id ID - Set ID of relative stream\n" |
---|
51 | " --inetd - Start in inetd mode (STDIN and STDOUT connected to socket)\n" |
---|
52 | " --help - Show this help\n" |
---|
53 | ); |
---|
54 | |
---|
55 | } |
---|
56 | |
---|
57 | void print_header (int codec, int rate, int channels) { |
---|
58 | const char * mime; |
---|
59 | |
---|
60 | mime = roar_codec2mime(codec); |
---|
61 | |
---|
62 | if ( mime == NULL ) |
---|
63 | mime = "application/octet-stream"; |
---|
64 | |
---|
65 | printf("Content-type: %s\r\n", mime); |
---|
66 | printf("ice-audio-info: ice-samplerate=%i;ice-channels=%i\r\n", rate, channels); |
---|
67 | printf("icy-pub:0\r\n"); |
---|
68 | printf("Server: RoarAudio (roarmonhttp $Revision$)\r\n"); |
---|
69 | printf("\r\n"); |
---|
70 | |
---|
71 | fflush(stdout); |
---|
72 | } |
---|
73 | |
---|
74 | int stream (struct roar_vio_calls * dest, struct roar_vio_calls * src) { |
---|
75 | struct roar_vio_select vios[2]; |
---|
76 | struct roar_buffer *ring = NULL, *cur; |
---|
77 | ssize_t len; |
---|
78 | size_t todo; |
---|
79 | int alive = 1; |
---|
80 | void * data; |
---|
81 | int ret; |
---|
82 | |
---|
83 | if ( roar_vio_nonblock(src, ROAR_SOCKET_NONBLOCK) == -1 ) { |
---|
84 | ROAR_WARN("stream(dest=%p, src=%p): Can not set source stream non-blocking: %s", |
---|
85 | dest, src, roar_error2str(roar_error)); |
---|
86 | } |
---|
87 | if ( roar_vio_nonblock(dest, ROAR_SOCKET_NONBLOCK) == -1 ) { |
---|
88 | ROAR_WARN("stream(dest=%p, src=%p): Can not set destination stream non-blocking: %s", |
---|
89 | dest, src, roar_error2str(roar_error)); |
---|
90 | } |
---|
91 | |
---|
92 | ROAR_VIO_SELECT_SETVIO(&(vios[0]), src, ROAR_VIO_SELECT_READ); |
---|
93 | ROAR_VIO_SELECT_SETVIO(&(vios[1]), dest, ROAR_VIO_SELECT_WRITE); |
---|
94 | |
---|
95 | while (alive) { |
---|
96 | ret = roar_vio_select(vios, ring != NULL ? 2 : 1, NULL, NULL); |
---|
97 | if ( ret == -1 ) { |
---|
98 | alive = 0; |
---|
99 | } else if ( ret == 0 ) { |
---|
100 | // nothing happend. |
---|
101 | } else { |
---|
102 | if ( vios[0].eventsa & ROAR_VIO_SELECT_READ ) { // we can read! |
---|
103 | if ( roar_buffer_new_data(&cur, BUFSIZE, &data) == -1 ) |
---|
104 | return -1; |
---|
105 | |
---|
106 | len = roar_vio_read(src, data, BUFSIZE); |
---|
107 | |
---|
108 | switch (len) { |
---|
109 | case 0: |
---|
110 | case -1: |
---|
111 | roar_buffer_free(cur); |
---|
112 | cur = NULL; |
---|
113 | |
---|
114 | if ( ring != NULL ) |
---|
115 | roar_buffer_free(ring); |
---|
116 | |
---|
117 | ring = NULL; |
---|
118 | |
---|
119 | return -1; |
---|
120 | break; |
---|
121 | } |
---|
122 | |
---|
123 | if ( cur != NULL ) { |
---|
124 | if ( roar_buffer_set_len(cur, len) == -1 ) |
---|
125 | return -1; |
---|
126 | |
---|
127 | if ( ring == NULL ) { |
---|
128 | ring = cur; |
---|
129 | } else { |
---|
130 | if ( roar_buffer_moveinto(ring, &cur) == -1 ) { |
---|
131 | ROAR_ERR("stream(dest=%p, src=%p): Can not append buffer to ring: %s", |
---|
132 | dest, src, roar_error2str(roar_error)); |
---|
133 | roar_buffer_free(ring); |
---|
134 | roar_buffer_free(cur); |
---|
135 | return -1; |
---|
136 | } |
---|
137 | } |
---|
138 | } |
---|
139 | } else if ( (vios[1].eventsa & ROAR_VIO_SELECT_WRITE) && ring != NULL ) { // we can write! |
---|
140 | if ( roar_buffer_get_data(ring, &data) == -1 ) |
---|
141 | return -1; |
---|
142 | |
---|
143 | if ( roar_buffer_get_len(ring, &todo) == -1 ) |
---|
144 | return -1; |
---|
145 | |
---|
146 | len = roar_vio_write(dest, data, todo); |
---|
147 | |
---|
148 | if ( len < 1 ) { |
---|
149 | if ( roar_error != ROAR_ERROR_AGAIN ) { |
---|
150 | roar_buffer_free(ring); |
---|
151 | return -1; |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | if ( (ssize_t)todo == len ) { // we wrote all of the pkg |
---|
156 | if ( roar_buffer_next(&ring) == -1 ) |
---|
157 | return -1; |
---|
158 | } else { |
---|
159 | if ( roar_buffer_set_offset(ring, len) == -1 ) |
---|
160 | return -1; |
---|
161 | } |
---|
162 | |
---|
163 | } |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | return 0; |
---|
168 | } |
---|
169 | |
---|
170 | #ifdef _CAN_SET_ENV |
---|
171 | int parse_http (int * gopher) { |
---|
172 | struct roar_keyval kv; |
---|
173 | char buf[1024]; |
---|
174 | char * qs = buf, *str; |
---|
175 | ssize_t len; |
---|
176 | int dir = ROAR_DIR_MONITOR; |
---|
177 | |
---|
178 | if ( (len = read(ROAR_STDIN, buf, 1023)) == -1 ) |
---|
179 | return -1; |
---|
180 | |
---|
181 | buf[len] = 0; |
---|
182 | |
---|
183 | if ( strncmp(buf, "GET /", 5) ) { |
---|
184 | if ( strncmp(buf, "SOURCE /", 8) ) { |
---|
185 | if ( buf[0] != '/' ) { |
---|
186 | return -1; |
---|
187 | } else { |
---|
188 | *gopher = 1; |
---|
189 | } |
---|
190 | } else { |
---|
191 | dir = ROAR_DIR_PLAY; |
---|
192 | qs += 3; |
---|
193 | } |
---|
194 | } |
---|
195 | |
---|
196 | if ( !*gopher ) { |
---|
197 | qs += 5; |
---|
198 | |
---|
199 | if ( (str = strstr(qs, " ")) == NULL ) |
---|
200 | return -1; |
---|
201 | |
---|
202 | *str = 0; |
---|
203 | } else { |
---|
204 | if ( (str = strstr(qs, "\r")) != NULL ) |
---|
205 | *str = 0; |
---|
206 | if ( (str = strstr(qs, "\n")) != NULL ) |
---|
207 | *str = 0; |
---|
208 | } |
---|
209 | |
---|
210 | for (; *qs != '?'; qs++) |
---|
211 | if ( !*qs ) |
---|
212 | break; |
---|
213 | |
---|
214 | if ( *qs == '?' ) |
---|
215 | qs++; |
---|
216 | |
---|
217 | if ( !*gopher ) |
---|
218 | printf("HTTP/1.0 200 OK\r\n"); |
---|
219 | // printf("QS: %s\r\n", qs); |
---|
220 | |
---|
221 | fflush(stdout); |
---|
222 | |
---|
223 | kv.key = "QUERY_STRING"; |
---|
224 | kv.value = qs; |
---|
225 | |
---|
226 | roar_env_set(&kv); |
---|
227 | |
---|
228 | return dir; |
---|
229 | } |
---|
230 | #endif |
---|
231 | |
---|
232 | int main (int argc, char * argv[]) { |
---|
233 | int rate = 44100; |
---|
234 | int bits = 16; |
---|
235 | int channels = 2; |
---|
236 | int codec = ROAR_CODEC_OGG_VORBIS; |
---|
237 | int rel_id = -1; |
---|
238 | int sflags = ROAR_FLAG_NONE; |
---|
239 | // int codec = ROAR_CODEC_DEFAULT; |
---|
240 | char * server = NULL; |
---|
241 | int i; |
---|
242 | char * c, * k, * v; |
---|
243 | char * sp0 = NULL, * sp1 = NULL; |
---|
244 | int dir = ROAR_DIR_MONITOR; |
---|
245 | int gopher = 0; |
---|
246 | struct roar_connection con; |
---|
247 | struct roar_stream s; |
---|
248 | struct roar_vio_calls * vio; |
---|
249 | |
---|
250 | #ifdef ROAR_HAVE_ALARM |
---|
251 | alarm(0); // reset alarm timers from httpd |
---|
252 | #endif |
---|
253 | |
---|
254 | for (i = 1; i < argc; i++) { |
---|
255 | k = argv[i]; |
---|
256 | if ( !strcmp(k, "--inetd") ) { |
---|
257 | #ifdef _CAN_SET_ENV |
---|
258 | if ( (dir = parse_http(&gopher)) == -1 ) |
---|
259 | return 1; |
---|
260 | #else |
---|
261 | return 1; |
---|
262 | #endif |
---|
263 | } else if ( !strcmp(k, "--server") ) { |
---|
264 | roar_libroar_set_server(argv[++i]); |
---|
265 | } else if ( !strcmp(k, "--codec") ) { |
---|
266 | codec = roar_str2codec(argv[++i]); |
---|
267 | } else if ( !strcmp(k, "--rate") || !strcmp(k, "-r") || !strcmp(k, "-R") ) { |
---|
268 | rate = roar_str2rate(argv[++i]); |
---|
269 | } else if ( !strcmp(k, "--bits") || !strcmp(k, "-B") ) { |
---|
270 | bits = roar_str2bits(argv[++i]); |
---|
271 | } else if ( !strcmp(k, "--channels") || !strcmp(k, "--chans") || !strcmp(k, "-C") ) { |
---|
272 | channels = roar_str2channels(argv[++i]); |
---|
273 | } else if ( !strcmp(k, "--rel-id") ) { |
---|
274 | rel_id = atoi(argv[++i]); |
---|
275 | } else if ( !strcmp(k, "--help") && !strcmp(k, "-h") ) { |
---|
276 | usage(); |
---|
277 | return 0; |
---|
278 | } else { |
---|
279 | ROAR_ERR("Unknown parameter: %s", k); |
---|
280 | usage(); |
---|
281 | return 1; |
---|
282 | } |
---|
283 | } |
---|
284 | |
---|
285 | c = getenv("QUERY_STRING"); |
---|
286 | if ( c == NULL ) |
---|
287 | c = ""; |
---|
288 | |
---|
289 | c = roar_mm_strtok_r(c, "&", &sp0); |
---|
290 | |
---|
291 | while (c != NULL) { |
---|
292 | k = roar_mm_strtok_r(c, "=", &sp1); |
---|
293 | v = roar_mm_strtok_r(NULL, "=", &sp1); |
---|
294 | |
---|
295 | if ( !strcmp(k, "codec") ) { |
---|
296 | if ( (codec = roar_str2codec(v)) == -1 ) |
---|
297 | return 1; |
---|
298 | } else if ( !strcmp(k, "channels") ) { |
---|
299 | channels = roar_str2channels(v); |
---|
300 | } else if ( !strcmp(k, "rate") ) { |
---|
301 | rate = roar_str2rate(v); |
---|
302 | } else if ( !strcmp(k, "bits") ) { |
---|
303 | bits = roar_str2bits(v); |
---|
304 | } else if ( !strcmp(k, "rel-id") || !strcmp(k, "relid") ) { |
---|
305 | rel_id = atoi(v); |
---|
306 | } else if ( !strcmp(k, "set-flag") ) { |
---|
307 | if ( !strcmp(v, "meta") ) { |
---|
308 | sflags |= ROAR_FLAG_META; |
---|
309 | } else if ( !strcmp(v, "cleanmeta") ) { |
---|
310 | sflags |= ROAR_FLAG_CLEANMETA; |
---|
311 | } else if ( !strcmp(v, "prethru") ) { |
---|
312 | sflags |= ROAR_FLAG_PRETHRU; |
---|
313 | } else { |
---|
314 | return 1; |
---|
315 | } |
---|
316 | } else if ( !strcmp(k, "dir") ) { |
---|
317 | if ( (dir = roar_str2dir(v)) == -1 ) |
---|
318 | return 1; |
---|
319 | } else { |
---|
320 | return 1; |
---|
321 | } |
---|
322 | |
---|
323 | c = roar_mm_strtok_r(NULL, "&", &sp0); |
---|
324 | } |
---|
325 | |
---|
326 | if ( roar_simple_connect(&con, server, "roarmonhttp") == -1 ) { |
---|
327 | return 10; |
---|
328 | } |
---|
329 | |
---|
330 | if ( roar_stream_new(&s, rate, channels, bits, codec) == -1 ) { |
---|
331 | roar_disconnect(&con); |
---|
332 | return 20; |
---|
333 | } |
---|
334 | |
---|
335 | if ( rel_id != -1 ) { |
---|
336 | if ( roar_stream_set_rel_id(&s, rel_id) ) { |
---|
337 | roar_disconnect(&con); |
---|
338 | return 21; |
---|
339 | } |
---|
340 | } |
---|
341 | |
---|
342 | if ( roar_stream_connect(&con, &s, dir, -1) == -1 ) { |
---|
343 | roar_disconnect(&con); |
---|
344 | return 11; |
---|
345 | } |
---|
346 | |
---|
347 | if ( sflags != ROAR_FLAG_NONE ) { |
---|
348 | if ( roar_stream_set_flags(&con, &s, sflags, ROAR_SET_FLAG) == -1 ) { |
---|
349 | roar_disconnect(&con); |
---|
350 | return 14; |
---|
351 | } |
---|
352 | } |
---|
353 | |
---|
354 | if ( roar_stream_exec(&con, &s) == -1 ) { |
---|
355 | roar_disconnect(&con); |
---|
356 | return 12; |
---|
357 | } |
---|
358 | |
---|
359 | if ( (vio = roar_get_connection_vio2(&con)) == NULL ) |
---|
360 | return 1; |
---|
361 | |
---|
362 | if ( !gopher ) |
---|
363 | print_header(codec, rate, channels); |
---|
364 | |
---|
365 | /* |
---|
366 | while((i = read(fh, buf, BUFSIZE))) |
---|
367 | if (write(out, buf, i) != i) |
---|
368 | break; |
---|
369 | */ |
---|
370 | |
---|
371 | switch (dir) { |
---|
372 | case ROAR_DIR_PLAY: |
---|
373 | stream(vio, roar_stdin); |
---|
374 | break; |
---|
375 | case ROAR_DIR_MONITOR: |
---|
376 | case ROAR_DIR_THRU: |
---|
377 | stream(roar_stdout, vio); |
---|
378 | break; |
---|
379 | } |
---|
380 | |
---|
381 | roar_vio_close(vio); |
---|
382 | |
---|
383 | return 0; |
---|
384 | } |
---|
385 | |
---|
386 | //ll |
---|