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