[1016] | 1 | //roarmonhttp.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[5381] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2012 |
---|
[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! |
---|
[5300] | 103 | if ( roar_buffer_new_data(&cur, BUFSIZE, &data) == -1 ) |
---|
[1017] | 104 | return -1; |
---|
| 105 | |
---|
[4925] | 106 | len = roar_vio_read(src, data, BUFSIZE); |
---|
[1017] | 107 | |
---|
| 108 | switch (len) { |
---|
| 109 | case 0: |
---|
| 110 | case -1: |
---|
| 111 | roar_buffer_free(cur); |
---|
[4925] | 112 | cur = NULL; |
---|
[1017] | 113 | |
---|
| 114 | if ( ring != NULL ) |
---|
| 115 | roar_buffer_free(ring); |
---|
| 116 | |
---|
[4925] | 117 | ring = NULL; |
---|
| 118 | |
---|
[1017] | 119 | return -1; |
---|
| 120 | break; |
---|
| 121 | } |
---|
| 122 | |
---|
[4925] | 123 | if ( cur != NULL ) { |
---|
| 124 | if ( roar_buffer_set_len(cur, len) == -1 ) |
---|
| 125 | return -1; |
---|
[1018] | 126 | |
---|
[4925] | 127 | if ( ring == NULL ) { |
---|
| 128 | ring = cur; |
---|
| 129 | } else { |
---|
[5300] | 130 | if ( roar_buffer_moveinto(ring, &cur) == -1 ) { |
---|
[5249] | 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 | } |
---|
[4925] | 137 | } |
---|
[1017] | 138 | } |
---|
[4925] | 139 | } else if ( (vios[1].eventsa & ROAR_VIO_SELECT_WRITE) && ring != NULL ) { // we can write! |
---|
[1017] | 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 | |
---|
[4925] | 146 | len = roar_vio_write(dest, data, todo); |
---|
[1017] | 147 | |
---|
| 148 | if ( len < 1 ) { |
---|
[4925] | 149 | if ( roar_error != ROAR_ERROR_AGAIN ) { |
---|
[1017] | 150 | roar_buffer_free(ring); |
---|
| 151 | return -1; |
---|
| 152 | } |
---|
| 153 | } |
---|
| 154 | |
---|
[3641] | 155 | if ( (ssize_t)todo == len ) { // we wrote all of the pkg |
---|
[1017] | 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 | |
---|
[1785] | 170 | #ifdef _CAN_SET_ENV |
---|
[1632] | 171 | int parse_http (int * gopher) { |
---|
[3967] | 172 | struct roar_keyval kv; |
---|
[1025] | 173 | char buf[1024]; |
---|
[1130] | 174 | char * qs = buf, *str; |
---|
[1025] | 175 | ssize_t len; |
---|
[1130] | 176 | int dir = ROAR_DIR_MONITOR; |
---|
[1025] | 177 | |
---|
| 178 | if ( (len = read(ROAR_STDIN, buf, 1023)) == -1 ) |
---|
| 179 | return -1; |
---|
| 180 | |
---|
| 181 | buf[len] = 0; |
---|
| 182 | |
---|
[1130] | 183 | if ( strncmp(buf, "GET /", 5) ) { |
---|
| 184 | if ( strncmp(buf, "SOURCE /", 8) ) { |
---|
[1632] | 185 | if ( buf[0] != '/' ) { |
---|
| 186 | return -1; |
---|
| 187 | } else { |
---|
| 188 | *gopher = 1; |
---|
| 189 | } |
---|
[1130] | 190 | } else { |
---|
| 191 | dir = ROAR_DIR_PLAY; |
---|
| 192 | qs += 3; |
---|
| 193 | } |
---|
| 194 | } |
---|
[1025] | 195 | |
---|
[1632] | 196 | if ( !*gopher ) { |
---|
| 197 | qs += 5; |
---|
| 198 | |
---|
| 199 | if ( (str = strstr(qs, " ")) == NULL ) |
---|
| 200 | return -1; |
---|
[1025] | 201 | |
---|
[1632] | 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 | } |
---|
[1025] | 209 | |
---|
| 210 | for (; *qs != '?'; qs++) |
---|
| 211 | if ( !*qs ) |
---|
| 212 | break; |
---|
| 213 | |
---|
| 214 | if ( *qs == '?' ) |
---|
| 215 | qs++; |
---|
| 216 | |
---|
[1632] | 217 | if ( !*gopher ) |
---|
| 218 | printf("HTTP/1.0 200 OK\r\n"); |
---|
[1025] | 219 | // printf("QS: %s\r\n", qs); |
---|
| 220 | |
---|
| 221 | fflush(stdout); |
---|
| 222 | |
---|
[3967] | 223 | kv.key = "QUERY_STRING"; |
---|
| 224 | kv.value = qs; |
---|
[1785] | 225 | |
---|
[3967] | 226 | roar_env_set(&kv); |
---|
[1025] | 227 | |
---|
[1130] | 228 | return dir; |
---|
[1025] | 229 | } |
---|
[1785] | 230 | #endif |
---|
[1025] | 231 | |
---|
[1016] | 232 | int main (int argc, char * argv[]) { |
---|
| 233 | int rate = 44100; |
---|
| 234 | int bits = 16; |
---|
| 235 | int channels = 2; |
---|
[1018] | 236 | int codec = ROAR_CODEC_OGG_VORBIS; |
---|
[2813] | 237 | int rel_id = -1; |
---|
| 238 | int sflags = ROAR_FLAG_NONE; |
---|
[1018] | 239 | // int codec = ROAR_CODEC_DEFAULT; |
---|
[1016] | 240 | char * server = NULL; |
---|
[4132] | 241 | int i; |
---|
[1018] | 242 | char * c, * k, * v; |
---|
[1605] | 243 | char * sp0 = NULL, * sp1 = NULL; |
---|
[1130] | 244 | int dir = ROAR_DIR_MONITOR; |
---|
[1632] | 245 | int gopher = 0; |
---|
[2813] | 246 | struct roar_connection con; |
---|
| 247 | struct roar_stream s; |
---|
[4925] | 248 | struct roar_vio_calls * vio; |
---|
[1023] | 249 | |
---|
[1783] | 250 | #ifdef ROAR_HAVE_ALARM |
---|
[1023] | 251 | alarm(0); // reset alarm timers from httpd |
---|
[1783] | 252 | #endif |
---|
[1018] | 253 | |
---|
[4132] | 254 | for (i = 1; i < argc; i++) { |
---|
| 255 | k = argv[i]; |
---|
| 256 | if ( !strcmp(k, "--inetd") ) { |
---|
[1785] | 257 | #ifdef _CAN_SET_ENV |
---|
[1632] | 258 | if ( (dir = parse_http(&gopher)) == -1 ) |
---|
[1025] | 259 | return 1; |
---|
[1785] | 260 | #else |
---|
| 261 | return 1; |
---|
| 262 | #endif |
---|
[4132] | 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") ) { |
---|
[4883] | 268 | rate = roar_str2rate(argv[++i]); |
---|
[4132] | 269 | } else if ( !strcmp(k, "--bits") || !strcmp(k, "-B") ) { |
---|
[4883] | 270 | bits = roar_str2bits(argv[++i]); |
---|
[4132] | 271 | } else if ( !strcmp(k, "--channels") || !strcmp(k, "--chans") || !strcmp(k, "-C") ) { |
---|
[4883] | 272 | channels = roar_str2channels(argv[++i]); |
---|
[4132] | 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 { |
---|
[4133] | 279 | ROAR_ERR("Unknown parameter: %s", k); |
---|
[4132] | 280 | usage(); |
---|
| 281 | return 1; |
---|
| 282 | } |
---|
| 283 | } |
---|
[1025] | 284 | |
---|
[3062] | 285 | c = getenv("QUERY_STRING"); |
---|
| 286 | if ( c == NULL ) |
---|
| 287 | c = ""; |
---|
| 288 | |
---|
[5009] | 289 | c = roar_mm_strtok_r(c, "&", &sp0); |
---|
[1018] | 290 | |
---|
| 291 | while (c != NULL) { |
---|
[5009] | 292 | k = roar_mm_strtok_r(c, "=", &sp1); |
---|
| 293 | v = roar_mm_strtok_r(NULL, "=", &sp1); |
---|
[1018] | 294 | |
---|
| 295 | if ( !strcmp(k, "codec") ) { |
---|
| 296 | if ( (codec = roar_str2codec(v)) == -1 ) |
---|
| 297 | return 1; |
---|
[1024] | 298 | } else if ( !strcmp(k, "channels") ) { |
---|
[4883] | 299 | channels = roar_str2channels(v); |
---|
[1024] | 300 | } else if ( !strcmp(k, "rate") ) { |
---|
[4883] | 301 | rate = roar_str2rate(v); |
---|
[1024] | 302 | } else if ( !strcmp(k, "bits") ) { |
---|
[4883] | 303 | bits = roar_str2bits(v); |
---|
[2813] | 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; |
---|
[1018] | 319 | } else { |
---|
| 320 | return 1; |
---|
| 321 | } |
---|
| 322 | |
---|
[5009] | 323 | c = roar_mm_strtok_r(NULL, "&", &sp0); |
---|
[1018] | 324 | } |
---|
| 325 | |
---|
[2813] | 326 | if ( roar_simple_connect(&con, server, "roarmonhttp") == -1 ) { |
---|
| 327 | return 10; |
---|
| 328 | } |
---|
[1016] | 329 | |
---|
[2813] | 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 | |
---|
[5238] | 342 | if ( roar_stream_connect(&con, &s, dir, -1) == -1 ) { |
---|
[2813] | 343 | roar_disconnect(&con); |
---|
| 344 | return 11; |
---|
| 345 | } |
---|
| 346 | |
---|
| 347 | if ( sflags != ROAR_FLAG_NONE ) { |
---|
[5238] | 348 | if ( roar_stream_set_flags(&con, &s, sflags, ROAR_SET_FLAG) == -1 ) { |
---|
[2813] | 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 | |
---|
[4925] | 359 | if ( (vio = roar_get_connection_vio2(&con)) == NULL ) |
---|
[1016] | 360 | return 1; |
---|
| 361 | |
---|
[1632] | 362 | if ( !gopher ) |
---|
| 363 | print_header(codec, rate, channels); |
---|
[1017] | 364 | |
---|
| 365 | /* |
---|
[1016] | 366 | while((i = read(fh, buf, BUFSIZE))) |
---|
| 367 | if (write(out, buf, i) != i) |
---|
| 368 | break; |
---|
[1017] | 369 | */ |
---|
| 370 | |
---|
[1130] | 371 | switch (dir) { |
---|
| 372 | case ROAR_DIR_PLAY: |
---|
[4925] | 373 | stream(vio, roar_stdin); |
---|
[1130] | 374 | break; |
---|
| 375 | case ROAR_DIR_MONITOR: |
---|
[2813] | 376 | case ROAR_DIR_THRU: |
---|
[4925] | 377 | stream(roar_stdout, vio); |
---|
[1130] | 378 | break; |
---|
| 379 | } |
---|
[1016] | 380 | |
---|
[4925] | 381 | roar_vio_close(vio); |
---|
[1016] | 382 | |
---|
| 383 | return 0; |
---|
| 384 | } |
---|
| 385 | |
---|
| 386 | //ll |
---|