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