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