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