[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 | |
---|
[4925] | 68 | int stream (struct roar_vio_calls * dest, struct roar_vio_calls * src) { |
---|
| 69 | struct roar_vio_select vios[2]; |
---|
[1017] | 70 | struct roar_buffer *ring = NULL, *cur; |
---|
| 71 | ssize_t len; |
---|
| 72 | size_t todo; |
---|
| 73 | int alive = 1; |
---|
| 74 | void * data; |
---|
[4925] | 75 | int ret; |
---|
[1017] | 76 | |
---|
[4925] | 77 | roar_vio_nonblock(src, ROAR_SOCKET_NONBLOCK); |
---|
| 78 | roar_vio_nonblock(dest, ROAR_SOCKET_NONBLOCK); |
---|
| 79 | |
---|
| 80 | ROAR_VIO_SELECT_SETVIO(&(vios[0]), src, ROAR_VIO_SELECT_READ); |
---|
| 81 | ROAR_VIO_SELECT_SETVIO(&(vios[1]), dest, ROAR_VIO_SELECT_WRITE); |
---|
[1017] | 82 | |
---|
| 83 | while (alive) { |
---|
[4925] | 84 | ret = roar_vio_select(vios, ring != NULL ? 2 : 1, NULL, NULL); |
---|
| 85 | if ( ret == -1 ) { |
---|
| 86 | alive = 0; |
---|
| 87 | } else if ( ret == 0 ) { |
---|
| 88 | // nothing happend. |
---|
| 89 | } else { |
---|
| 90 | if ( vios[0].eventsa & ROAR_VIO_SELECT_READ ) { // we can read! |
---|
[1017] | 91 | if ( roar_buffer_new(&cur, BUFSIZE) == -1 ) |
---|
| 92 | return -1; |
---|
| 93 | |
---|
| 94 | if ( roar_buffer_get_data(cur, &data) == -1 ) |
---|
| 95 | return -1; |
---|
| 96 | |
---|
[4925] | 97 | len = roar_vio_read(src, data, BUFSIZE); |
---|
[1017] | 98 | |
---|
| 99 | switch (len) { |
---|
| 100 | case 0: |
---|
| 101 | case -1: |
---|
| 102 | roar_buffer_free(cur); |
---|
[4925] | 103 | cur = NULL; |
---|
[1017] | 104 | |
---|
| 105 | if ( ring != NULL ) |
---|
| 106 | roar_buffer_free(ring); |
---|
| 107 | |
---|
[4925] | 108 | ring = NULL; |
---|
| 109 | |
---|
[1017] | 110 | return -1; |
---|
| 111 | break; |
---|
| 112 | } |
---|
| 113 | |
---|
[4925] | 114 | if ( cur != NULL ) { |
---|
| 115 | if ( roar_buffer_set_len(cur, len) == -1 ) |
---|
| 116 | return -1; |
---|
[1018] | 117 | |
---|
[4925] | 118 | if ( ring == NULL ) { |
---|
| 119 | ring = cur; |
---|
| 120 | } else { |
---|
| 121 | roar_buffer_add(ring, cur); |
---|
| 122 | } |
---|
[1017] | 123 | } |
---|
[4925] | 124 | } else if ( (vios[1].eventsa & ROAR_VIO_SELECT_WRITE) && ring != NULL ) { // we can write! |
---|
[1017] | 125 | if ( roar_buffer_get_data(ring, &data) == -1 ) |
---|
| 126 | return -1; |
---|
| 127 | |
---|
| 128 | if ( roar_buffer_get_len(ring, &todo) == -1 ) |
---|
| 129 | return -1; |
---|
| 130 | |
---|
[4925] | 131 | len = roar_vio_write(dest, data, todo); |
---|
[1017] | 132 | |
---|
| 133 | if ( len < 1 ) { |
---|
[4925] | 134 | if ( roar_error != ROAR_ERROR_AGAIN ) { |
---|
[1017] | 135 | roar_buffer_free(ring); |
---|
| 136 | return -1; |
---|
| 137 | } |
---|
| 138 | } |
---|
| 139 | |
---|
[3641] | 140 | if ( (ssize_t)todo == len ) { // we wrote all of the pkg |
---|
[1017] | 141 | if ( roar_buffer_next(&ring) == -1 ) |
---|
| 142 | return -1; |
---|
| 143 | } else { |
---|
| 144 | if ( roar_buffer_set_offset(ring, len) == -1 ) |
---|
| 145 | return -1; |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | } |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | return 0; |
---|
| 153 | } |
---|
| 154 | |
---|
[1785] | 155 | #ifdef _CAN_SET_ENV |
---|
[1632] | 156 | int parse_http (int * gopher) { |
---|
[3967] | 157 | struct roar_keyval kv; |
---|
[1025] | 158 | char buf[1024]; |
---|
[1130] | 159 | char * qs = buf, *str; |
---|
[1025] | 160 | ssize_t len; |
---|
[1130] | 161 | int dir = ROAR_DIR_MONITOR; |
---|
[1025] | 162 | |
---|
| 163 | if ( (len = read(ROAR_STDIN, buf, 1023)) == -1 ) |
---|
| 164 | return -1; |
---|
| 165 | |
---|
| 166 | buf[len] = 0; |
---|
| 167 | |
---|
[1130] | 168 | if ( strncmp(buf, "GET /", 5) ) { |
---|
| 169 | if ( strncmp(buf, "SOURCE /", 8) ) { |
---|
[1632] | 170 | if ( buf[0] != '/' ) { |
---|
| 171 | return -1; |
---|
| 172 | } else { |
---|
| 173 | *gopher = 1; |
---|
| 174 | } |
---|
[1130] | 175 | } else { |
---|
| 176 | dir = ROAR_DIR_PLAY; |
---|
| 177 | qs += 3; |
---|
| 178 | } |
---|
| 179 | } |
---|
[1025] | 180 | |
---|
[1632] | 181 | if ( !*gopher ) { |
---|
| 182 | qs += 5; |
---|
| 183 | |
---|
| 184 | if ( (str = strstr(qs, " ")) == NULL ) |
---|
| 185 | return -1; |
---|
[1025] | 186 | |
---|
[1632] | 187 | *str = 0; |
---|
| 188 | } else { |
---|
| 189 | if ( (str = strstr(qs, "\r")) != NULL ) |
---|
| 190 | *str = 0; |
---|
| 191 | if ( (str = strstr(qs, "\n")) != NULL ) |
---|
| 192 | *str = 0; |
---|
| 193 | } |
---|
[1025] | 194 | |
---|
| 195 | for (; *qs != '?'; qs++) |
---|
| 196 | if ( !*qs ) |
---|
| 197 | break; |
---|
| 198 | |
---|
| 199 | if ( *qs == '?' ) |
---|
| 200 | qs++; |
---|
| 201 | |
---|
[1632] | 202 | if ( !*gopher ) |
---|
| 203 | printf("HTTP/1.0 200 OK\r\n"); |
---|
[1025] | 204 | // printf("QS: %s\r\n", qs); |
---|
| 205 | |
---|
| 206 | fflush(stdout); |
---|
| 207 | |
---|
[3967] | 208 | kv.key = "QUERY_STRING"; |
---|
| 209 | kv.value = qs; |
---|
[1785] | 210 | |
---|
[3967] | 211 | roar_env_set(&kv); |
---|
[1025] | 212 | |
---|
[1130] | 213 | return dir; |
---|
[1025] | 214 | } |
---|
[1785] | 215 | #endif |
---|
[1025] | 216 | |
---|
[1016] | 217 | int main (int argc, char * argv[]) { |
---|
| 218 | int rate = 44100; |
---|
| 219 | int bits = 16; |
---|
| 220 | int channels = 2; |
---|
[1018] | 221 | int codec = ROAR_CODEC_OGG_VORBIS; |
---|
[2813] | 222 | int rel_id = -1; |
---|
| 223 | int sflags = ROAR_FLAG_NONE; |
---|
[1018] | 224 | // int codec = ROAR_CODEC_DEFAULT; |
---|
[1016] | 225 | char * server = NULL; |
---|
[4132] | 226 | int i; |
---|
[1018] | 227 | char * c, * k, * v; |
---|
[2220] | 228 | #ifdef ROAR_HAVE_STRTOK_R |
---|
[1605] | 229 | char * sp0 = NULL, * sp1 = NULL; |
---|
[2220] | 230 | #endif |
---|
[1130] | 231 | int dir = ROAR_DIR_MONITOR; |
---|
[1632] | 232 | int gopher = 0; |
---|
[2813] | 233 | struct roar_connection con; |
---|
| 234 | struct roar_stream s; |
---|
[4925] | 235 | struct roar_vio_calls * vio; |
---|
[1023] | 236 | |
---|
[1783] | 237 | #ifdef ROAR_HAVE_ALARM |
---|
[1023] | 238 | alarm(0); // reset alarm timers from httpd |
---|
[1783] | 239 | #endif |
---|
[1018] | 240 | |
---|
[4132] | 241 | for (i = 1; i < argc; i++) { |
---|
| 242 | k = argv[i]; |
---|
| 243 | if ( !strcmp(k, "--inetd") ) { |
---|
[1785] | 244 | #ifdef _CAN_SET_ENV |
---|
[1632] | 245 | if ( (dir = parse_http(&gopher)) == -1 ) |
---|
[1025] | 246 | return 1; |
---|
[1785] | 247 | #else |
---|
| 248 | return 1; |
---|
| 249 | #endif |
---|
[4132] | 250 | } else if ( !strcmp(k, "--server") ) { |
---|
| 251 | roar_libroar_set_server(argv[++i]); |
---|
| 252 | } else if ( !strcmp(k, "--codec") ) { |
---|
| 253 | codec = roar_str2codec(argv[++i]); |
---|
| 254 | } else if ( !strcmp(k, "--rate") || !strcmp(k, "-r") || !strcmp(k, "-R") ) { |
---|
[4883] | 255 | rate = roar_str2rate(argv[++i]); |
---|
[4132] | 256 | } else if ( !strcmp(k, "--bits") || !strcmp(k, "-B") ) { |
---|
[4883] | 257 | bits = roar_str2bits(argv[++i]); |
---|
[4132] | 258 | } else if ( !strcmp(k, "--channels") || !strcmp(k, "--chans") || !strcmp(k, "-C") ) { |
---|
[4883] | 259 | channels = roar_str2channels(argv[++i]); |
---|
[4132] | 260 | } else if ( !strcmp(k, "--rel-id") ) { |
---|
| 261 | rel_id = atoi(argv[++i]); |
---|
| 262 | } else if ( !strcmp(k, "--help") && !strcmp(k, "-h") ) { |
---|
| 263 | usage(); |
---|
| 264 | return 0; |
---|
| 265 | } else { |
---|
[4133] | 266 | ROAR_ERR("Unknown parameter: %s", k); |
---|
[4132] | 267 | usage(); |
---|
| 268 | return 1; |
---|
| 269 | } |
---|
| 270 | } |
---|
[1025] | 271 | |
---|
[3062] | 272 | c = getenv("QUERY_STRING"); |
---|
| 273 | if ( c == NULL ) |
---|
| 274 | c = ""; |
---|
| 275 | |
---|
[1797] | 276 | #ifdef ROAR_HAVE_STRTOK_R |
---|
[3062] | 277 | c = strtok_r(c, "&", &sp0); |
---|
[1797] | 278 | #else |
---|
[3062] | 279 | c = strtok(c, "&"); |
---|
[1797] | 280 | #endif |
---|
[1018] | 281 | |
---|
| 282 | while (c != NULL) { |
---|
[1797] | 283 | #ifdef ROAR_HAVE_STRTOK_R |
---|
[1018] | 284 | k = strtok_r(c, "=", &sp1); |
---|
| 285 | v = strtok_r(NULL, "=", &sp1); |
---|
[1797] | 286 | #else |
---|
| 287 | k = c; |
---|
| 288 | if ( (v = strstr(c, "=")) != NULL ) { |
---|
| 289 | *v = 0; |
---|
| 290 | v++; |
---|
| 291 | } |
---|
| 292 | #endif |
---|
[1018] | 293 | |
---|
| 294 | if ( !strcmp(k, "codec") ) { |
---|
| 295 | if ( (codec = roar_str2codec(v)) == -1 ) |
---|
| 296 | return 1; |
---|
[1024] | 297 | } else if ( !strcmp(k, "channels") ) { |
---|
[4883] | 298 | channels = roar_str2channels(v); |
---|
[1024] | 299 | } else if ( !strcmp(k, "rate") ) { |
---|
[4883] | 300 | rate = roar_str2rate(v); |
---|
[1024] | 301 | } else if ( !strcmp(k, "bits") ) { |
---|
[4883] | 302 | bits = roar_str2bits(v); |
---|
[2813] | 303 | } else if ( !strcmp(k, "rel-id") || !strcmp(k, "relid") ) { |
---|
| 304 | rel_id = atoi(v); |
---|
| 305 | } else if ( !strcmp(k, "set-flag") ) { |
---|
| 306 | if ( !strcmp(v, "meta") ) { |
---|
| 307 | sflags |= ROAR_FLAG_META; |
---|
| 308 | } else if ( !strcmp(v, "cleanmeta") ) { |
---|
| 309 | sflags |= ROAR_FLAG_CLEANMETA; |
---|
| 310 | } else if ( !strcmp(v, "prethru") ) { |
---|
| 311 | sflags |= ROAR_FLAG_PRETHRU; |
---|
| 312 | } else { |
---|
| 313 | return 1; |
---|
| 314 | } |
---|
| 315 | } else if ( !strcmp(k, "dir") ) { |
---|
| 316 | if ( (dir = roar_str2dir(v)) == -1 ) |
---|
| 317 | return 1; |
---|
[1018] | 318 | } else { |
---|
| 319 | return 1; |
---|
| 320 | } |
---|
| 321 | |
---|
[1797] | 322 | #ifdef ROAR_HAVE_STRTOK_R |
---|
[1018] | 323 | c = strtok_r(NULL, "&", &sp0); |
---|
[1797] | 324 | #else |
---|
| 325 | c = strtok(NULL, "&"); |
---|
| 326 | #endif |
---|
[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 | |
---|
| 345 | if ( roar_stream_connect(&con, &s, dir) == -1 ) { |
---|
| 346 | roar_disconnect(&con); |
---|
| 347 | return 11; |
---|
| 348 | } |
---|
| 349 | |
---|
| 350 | if ( sflags != ROAR_FLAG_NONE ) { |
---|
[4885] | 351 | if ( roar_stream_set_flags2(&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 |
---|