[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) { |
---|
[3967] | 142 | struct roar_keyval kv; |
---|
[1025] | 143 | char buf[1024]; |
---|
[1130] | 144 | char * qs = buf, *str; |
---|
[1025] | 145 | ssize_t len; |
---|
[1130] | 146 | int dir = ROAR_DIR_MONITOR; |
---|
[1025] | 147 | |
---|
| 148 | if ( (len = read(ROAR_STDIN, buf, 1023)) == -1 ) |
---|
| 149 | return -1; |
---|
| 150 | |
---|
| 151 | buf[len] = 0; |
---|
| 152 | |
---|
[1130] | 153 | if ( strncmp(buf, "GET /", 5) ) { |
---|
| 154 | if ( strncmp(buf, "SOURCE /", 8) ) { |
---|
[1632] | 155 | if ( buf[0] != '/' ) { |
---|
| 156 | return -1; |
---|
| 157 | } else { |
---|
| 158 | *gopher = 1; |
---|
| 159 | } |
---|
[1130] | 160 | } else { |
---|
| 161 | dir = ROAR_DIR_PLAY; |
---|
| 162 | qs += 3; |
---|
| 163 | } |
---|
| 164 | } |
---|
[1025] | 165 | |
---|
[1632] | 166 | if ( !*gopher ) { |
---|
| 167 | qs += 5; |
---|
| 168 | |
---|
| 169 | if ( (str = strstr(qs, " ")) == NULL ) |
---|
| 170 | return -1; |
---|
[1025] | 171 | |
---|
[1632] | 172 | *str = 0; |
---|
| 173 | } else { |
---|
| 174 | if ( (str = strstr(qs, "\r")) != NULL ) |
---|
| 175 | *str = 0; |
---|
| 176 | if ( (str = strstr(qs, "\n")) != NULL ) |
---|
| 177 | *str = 0; |
---|
| 178 | } |
---|
[1025] | 179 | |
---|
| 180 | for (; *qs != '?'; qs++) |
---|
| 181 | if ( !*qs ) |
---|
| 182 | break; |
---|
| 183 | |
---|
| 184 | if ( *qs == '?' ) |
---|
| 185 | qs++; |
---|
| 186 | |
---|
[1632] | 187 | if ( !*gopher ) |
---|
| 188 | printf("HTTP/1.0 200 OK\r\n"); |
---|
[1025] | 189 | // printf("QS: %s\r\n", qs); |
---|
| 190 | |
---|
| 191 | fflush(stdout); |
---|
| 192 | |
---|
[3967] | 193 | kv.key = "QUERY_STRING"; |
---|
| 194 | kv.value = qs; |
---|
[1785] | 195 | |
---|
[3967] | 196 | roar_env_set(&kv); |
---|
[1025] | 197 | |
---|
[1130] | 198 | return dir; |
---|
[1025] | 199 | } |
---|
[1785] | 200 | #endif |
---|
[1025] | 201 | |
---|
[1016] | 202 | int main (int argc, char * argv[]) { |
---|
| 203 | int rate = 44100; |
---|
| 204 | int bits = 16; |
---|
| 205 | int channels = 2; |
---|
[1018] | 206 | int codec = ROAR_CODEC_OGG_VORBIS; |
---|
[2813] | 207 | int rel_id = -1; |
---|
| 208 | int sflags = ROAR_FLAG_NONE; |
---|
[1018] | 209 | // int codec = ROAR_CODEC_DEFAULT; |
---|
[1016] | 210 | char * server = NULL; |
---|
| 211 | int fh; |
---|
[1018] | 212 | char * c, * k, * v; |
---|
[2220] | 213 | #ifdef ROAR_HAVE_STRTOK_R |
---|
[1605] | 214 | char * sp0 = NULL, * sp1 = NULL; |
---|
[2220] | 215 | #endif |
---|
[1130] | 216 | int dir = ROAR_DIR_MONITOR; |
---|
[1632] | 217 | int gopher = 0; |
---|
[2813] | 218 | struct roar_connection con; |
---|
| 219 | struct roar_stream s; |
---|
[1023] | 220 | |
---|
[1783] | 221 | #ifdef ROAR_HAVE_ALARM |
---|
[1023] | 222 | alarm(0); // reset alarm timers from httpd |
---|
[1783] | 223 | #endif |
---|
[1018] | 224 | |
---|
[1025] | 225 | if ( argc > 1 ) |
---|
| 226 | if ( ! strcmp(argv[1], "--inetd") ) |
---|
[1785] | 227 | #ifdef _CAN_SET_ENV |
---|
[1632] | 228 | if ( (dir = parse_http(&gopher)) == -1 ) |
---|
[1025] | 229 | return 1; |
---|
[1785] | 230 | #else |
---|
| 231 | return 1; |
---|
| 232 | #endif |
---|
[1025] | 233 | |
---|
[3062] | 234 | c = getenv("QUERY_STRING"); |
---|
| 235 | if ( c == NULL ) |
---|
| 236 | c = ""; |
---|
| 237 | |
---|
[1797] | 238 | #ifdef ROAR_HAVE_STRTOK_R |
---|
[3062] | 239 | c = strtok_r(c, "&", &sp0); |
---|
[1797] | 240 | #else |
---|
[3062] | 241 | c = strtok(c, "&"); |
---|
[1797] | 242 | #endif |
---|
[1018] | 243 | |
---|
| 244 | while (c != NULL) { |
---|
[1797] | 245 | #ifdef ROAR_HAVE_STRTOK_R |
---|
[1018] | 246 | k = strtok_r(c, "=", &sp1); |
---|
| 247 | v = strtok_r(NULL, "=", &sp1); |
---|
[1797] | 248 | #else |
---|
| 249 | k = c; |
---|
| 250 | if ( (v = strstr(c, "=")) != NULL ) { |
---|
| 251 | *v = 0; |
---|
| 252 | v++; |
---|
| 253 | } |
---|
| 254 | #endif |
---|
[1018] | 255 | |
---|
| 256 | if ( !strcmp(k, "codec") ) { |
---|
| 257 | if ( (codec = roar_str2codec(v)) == -1 ) |
---|
| 258 | return 1; |
---|
[1024] | 259 | } else if ( !strcmp(k, "channels") ) { |
---|
| 260 | channels = atoi(v); |
---|
| 261 | } else if ( !strcmp(k, "rate") ) { |
---|
| 262 | rate = atoi(v); |
---|
| 263 | } else if ( !strcmp(k, "bits") ) { |
---|
| 264 | bits = atoi(v); |
---|
[2813] | 265 | } else if ( !strcmp(k, "rel-id") || !strcmp(k, "relid") ) { |
---|
| 266 | rel_id = atoi(v); |
---|
| 267 | } else if ( !strcmp(k, "set-flag") ) { |
---|
| 268 | if ( !strcmp(v, "meta") ) { |
---|
| 269 | sflags |= ROAR_FLAG_META; |
---|
| 270 | } else if ( !strcmp(v, "cleanmeta") ) { |
---|
| 271 | sflags |= ROAR_FLAG_CLEANMETA; |
---|
| 272 | } else if ( !strcmp(v, "prethru") ) { |
---|
| 273 | sflags |= ROAR_FLAG_PRETHRU; |
---|
| 274 | } else { |
---|
| 275 | return 1; |
---|
| 276 | } |
---|
| 277 | } else if ( !strcmp(k, "dir") ) { |
---|
| 278 | if ( (dir = roar_str2dir(v)) == -1 ) |
---|
| 279 | return 1; |
---|
[1018] | 280 | } else { |
---|
| 281 | return 1; |
---|
| 282 | } |
---|
| 283 | |
---|
[1797] | 284 | #ifdef ROAR_HAVE_STRTOK_R |
---|
[1018] | 285 | c = strtok_r(NULL, "&", &sp0); |
---|
[1797] | 286 | #else |
---|
| 287 | c = strtok(NULL, "&"); |
---|
| 288 | #endif |
---|
[1018] | 289 | } |
---|
| 290 | |
---|
[2813] | 291 | if ( roar_simple_connect(&con, server, "roarmonhttp") == -1 ) { |
---|
| 292 | return 10; |
---|
| 293 | } |
---|
[1016] | 294 | |
---|
[2813] | 295 | if ( roar_stream_new(&s, rate, channels, bits, codec) == -1 ) { |
---|
| 296 | roar_disconnect(&con); |
---|
| 297 | return 20; |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | if ( rel_id != -1 ) { |
---|
| 301 | if ( roar_stream_set_rel_id(&s, rel_id) ) { |
---|
| 302 | roar_disconnect(&con); |
---|
| 303 | return 21; |
---|
| 304 | } |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | if ( roar_stream_connect(&con, &s, dir) == -1 ) { |
---|
| 308 | roar_disconnect(&con); |
---|
| 309 | return 11; |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | if ( sflags != ROAR_FLAG_NONE ) { |
---|
| 313 | if ( roar_stream_set_flags(&con, &s, sflags, 0) == -1 ) { |
---|
| 314 | roar_disconnect(&con); |
---|
| 315 | return 14; |
---|
| 316 | } |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | if ( roar_stream_exec(&con, &s) == -1 ) { |
---|
| 320 | roar_disconnect(&con); |
---|
| 321 | return 12; |
---|
| 322 | } |
---|
| 323 | |
---|
| 324 | if ( (fh = roar_get_connection_fh(&con)) == -1 ) |
---|
[1016] | 325 | return 1; |
---|
| 326 | |
---|
[1632] | 327 | if ( !gopher ) |
---|
| 328 | print_header(codec, rate, channels); |
---|
[1017] | 329 | |
---|
| 330 | /* |
---|
[1016] | 331 | while((i = read(fh, buf, BUFSIZE))) |
---|
| 332 | if (write(out, buf, i) != i) |
---|
| 333 | break; |
---|
[1017] | 334 | */ |
---|
| 335 | |
---|
[1130] | 336 | switch (dir) { |
---|
| 337 | case ROAR_DIR_PLAY: |
---|
| 338 | stream(fh, ROAR_STDIN); |
---|
| 339 | break; |
---|
| 340 | case ROAR_DIR_MONITOR: |
---|
[2813] | 341 | case ROAR_DIR_THRU: |
---|
[1130] | 342 | stream(ROAR_STDOUT, fh); |
---|
| 343 | break; |
---|
| 344 | } |
---|
[1016] | 345 | |
---|
| 346 | roar_simple_close(fh); |
---|
| 347 | |
---|
| 348 | return 0; |
---|
| 349 | } |
---|
| 350 | |
---|
| 351 | //ll |
---|