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