[1016] | 1 | //roarmonhttp.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008 |
---|
| 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 |
---|
| 21 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 22 | * |
---|
| 23 | */ |
---|
| 24 | |
---|
| 25 | #include <roaraudio.h> |
---|
| 26 | |
---|
[1785] | 27 | #if defined(ROAR_HAVE_SETENV) || defined(ROAR_HAVE_PUTENV) |
---|
| 28 | #define _CAN_SET_ENV |
---|
| 29 | #endif |
---|
| 30 | |
---|
[1016] | 31 | #define BUFSIZE 1024 |
---|
| 32 | |
---|
[1027] | 33 | void print_header (int codec, int rate, int channels) { |
---|
[1017] | 34 | char * mime = "application/octet-stream"; |
---|
| 35 | |
---|
| 36 | switch (codec) { |
---|
| 37 | case ROAR_CODEC_OGG_VORBIS: |
---|
| 38 | mime = "application/ogg"; |
---|
| 39 | break; |
---|
[1018] | 40 | case ROAR_CODEC_RIFF_WAVE: |
---|
| 41 | mime = "audio/x-wav"; |
---|
| 42 | break; |
---|
[1017] | 43 | } |
---|
| 44 | |
---|
| 45 | printf("Content-type: %s\r\n", mime); |
---|
[1027] | 46 | printf("ice-audio-info: ice-samplerate=%i;ice-channels=%i\r\n", rate, channels); |
---|
| 47 | printf("icy-pub:0\r\n"); |
---|
[1024] | 48 | printf("Server: RoarAudio (roarmonhttp $Revision$)\r\n"); |
---|
[1017] | 49 | printf("\r\n"); |
---|
| 50 | |
---|
| 51 | fflush(stdout); |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | int stream (int dest, int src) { |
---|
| 55 | struct roar_buffer *ring = NULL, *cur; |
---|
| 56 | ssize_t len; |
---|
| 57 | size_t todo; |
---|
| 58 | fd_set fsi[1], fso[1]; |
---|
| 59 | struct timeval tv; |
---|
| 60 | int alive = 1; |
---|
| 61 | int maxfh = (dest > src ? dest : src) + 1; |
---|
| 62 | void * data; |
---|
| 63 | |
---|
| 64 | roar_socket_nonblock(src, ROAR_SOCKET_NONBLOCK); |
---|
| 65 | roar_socket_nonblock(dest, ROAR_SOCKET_NONBLOCK); |
---|
| 66 | |
---|
| 67 | while (alive) { |
---|
| 68 | FD_ZERO(fsi); |
---|
| 69 | FD_ZERO(fso); |
---|
| 70 | FD_SET(src, fsi); |
---|
[1018] | 71 | if ( ring != NULL ) { |
---|
| 72 | FD_SET(dest, fso); |
---|
| 73 | } |
---|
[1017] | 74 | |
---|
| 75 | tv.tv_sec = 0; |
---|
| 76 | tv.tv_usec = 100000; // 100ms |
---|
| 77 | |
---|
| 78 | if (select(maxfh, fsi, fso, NULL, &tv) > 0) { |
---|
| 79 | if ( FD_ISSET(src, fsi) ) { // we can read! |
---|
| 80 | if ( roar_buffer_new(&cur, BUFSIZE) == -1 ) |
---|
| 81 | return -1; |
---|
| 82 | |
---|
| 83 | if ( roar_buffer_get_data(cur, &data) == -1 ) |
---|
| 84 | return -1; |
---|
| 85 | |
---|
| 86 | len = read(src, data, BUFSIZE); |
---|
| 87 | |
---|
| 88 | switch (len) { |
---|
| 89 | case 0: |
---|
| 90 | case -1: |
---|
| 91 | roar_buffer_free(cur); |
---|
| 92 | |
---|
| 93 | if ( ring != NULL ) |
---|
| 94 | roar_buffer_free(ring); |
---|
| 95 | |
---|
| 96 | return -1; |
---|
| 97 | break; |
---|
| 98 | } |
---|
| 99 | |
---|
[1018] | 100 | if ( roar_buffer_set_len(cur, len) == -1 ) |
---|
| 101 | return -1; |
---|
| 102 | |
---|
[1017] | 103 | if ( ring == NULL ) { |
---|
| 104 | ring = cur; |
---|
| 105 | } else { |
---|
| 106 | roar_buffer_add(ring, cur); |
---|
| 107 | } |
---|
| 108 | } else if ( FD_ISSET(dest, fso) && ring != NULL ) { // we can write! |
---|
| 109 | if ( roar_buffer_get_data(ring, &data) == -1 ) |
---|
| 110 | return -1; |
---|
| 111 | |
---|
| 112 | if ( roar_buffer_get_len(ring, &todo) == -1 ) |
---|
| 113 | return -1; |
---|
| 114 | |
---|
| 115 | len = write(dest, data, todo); |
---|
| 116 | |
---|
| 117 | if ( len < 1 ) { |
---|
| 118 | if ( errno != EAGAIN ) { |
---|
| 119 | roar_buffer_free(ring); |
---|
| 120 | return -1; |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | if ( todo == len ) { // we wrote all of the pkg |
---|
| 125 | if ( roar_buffer_next(&ring) == -1 ) |
---|
| 126 | return -1; |
---|
| 127 | } else { |
---|
| 128 | if ( roar_buffer_set_offset(ring, len) == -1 ) |
---|
| 129 | return -1; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | } |
---|
| 133 | } |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | return 0; |
---|
| 137 | } |
---|
| 138 | |
---|
[1785] | 139 | #ifdef _CAN_SET_ENV |
---|
[1632] | 140 | int parse_http (int * gopher) { |
---|
[1025] | 141 | char buf[1024]; |
---|
[1130] | 142 | char * qs = buf, *str; |
---|
[1025] | 143 | ssize_t len; |
---|
[1130] | 144 | int dir = ROAR_DIR_MONITOR; |
---|
[1025] | 145 | |
---|
| 146 | if ( (len = read(ROAR_STDIN, buf, 1023)) == -1 ) |
---|
| 147 | return -1; |
---|
| 148 | |
---|
| 149 | buf[len] = 0; |
---|
| 150 | |
---|
[1130] | 151 | if ( strncmp(buf, "GET /", 5) ) { |
---|
| 152 | if ( strncmp(buf, "SOURCE /", 8) ) { |
---|
[1632] | 153 | if ( buf[0] != '/' ) { |
---|
| 154 | return -1; |
---|
| 155 | } else { |
---|
| 156 | *gopher = 1; |
---|
| 157 | } |
---|
[1130] | 158 | } else { |
---|
| 159 | dir = ROAR_DIR_PLAY; |
---|
| 160 | qs += 3; |
---|
| 161 | } |
---|
| 162 | } |
---|
[1025] | 163 | |
---|
[1632] | 164 | if ( !*gopher ) { |
---|
| 165 | qs += 5; |
---|
| 166 | |
---|
| 167 | if ( (str = strstr(qs, " ")) == NULL ) |
---|
| 168 | return -1; |
---|
[1025] | 169 | |
---|
[1632] | 170 | *str = 0; |
---|
| 171 | } else { |
---|
| 172 | if ( (str = strstr(qs, "\r")) != NULL ) |
---|
| 173 | *str = 0; |
---|
| 174 | if ( (str = strstr(qs, "\n")) != NULL ) |
---|
| 175 | *str = 0; |
---|
| 176 | } |
---|
[1025] | 177 | |
---|
| 178 | for (; *qs != '?'; qs++) |
---|
| 179 | if ( !*qs ) |
---|
| 180 | break; |
---|
| 181 | |
---|
| 182 | if ( *qs == '?' ) |
---|
| 183 | qs++; |
---|
| 184 | |
---|
[1632] | 185 | if ( !*gopher ) |
---|
| 186 | printf("HTTP/1.0 200 OK\r\n"); |
---|
[1025] | 187 | // printf("QS: %s\r\n", qs); |
---|
| 188 | |
---|
| 189 | fflush(stdout); |
---|
| 190 | |
---|
[1785] | 191 | #ifdef ROAR_HAVE_SETENV |
---|
[1025] | 192 | setenv("QUERY_STRING", qs, 1); |
---|
[1785] | 193 | #else |
---|
| 194 | // TODO: does this leak memory? |
---|
| 195 | if ( (str = malloc(strlen(qs) + strlen("QUERY_STRING=") + 1)) == NULL ) { |
---|
| 196 | return -1; |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | sprintf(str, "QUERY_STRING=%s", qs); |
---|
| 200 | |
---|
| 201 | putenv(str); |
---|
| 202 | #endif |
---|
[1025] | 203 | |
---|
[1130] | 204 | return dir; |
---|
[1025] | 205 | } |
---|
[1785] | 206 | #endif |
---|
[1025] | 207 | |
---|
[1016] | 208 | int main (int argc, char * argv[]) { |
---|
| 209 | int rate = 44100; |
---|
| 210 | int bits = 16; |
---|
| 211 | int channels = 2; |
---|
[1018] | 212 | int codec = ROAR_CODEC_OGG_VORBIS; |
---|
| 213 | // int codec = ROAR_CODEC_DEFAULT; |
---|
[1016] | 214 | char * server = NULL; |
---|
| 215 | int fh; |
---|
[1018] | 216 | char * c, * k, * v; |
---|
[1605] | 217 | char * sp0 = NULL, * sp1 = NULL; |
---|
[1130] | 218 | int dir = ROAR_DIR_MONITOR; |
---|
[1632] | 219 | int gopher = 0; |
---|
[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 | |
---|
[1797] | 234 | #ifdef ROAR_HAVE_STRTOK_R |
---|
[1018] | 235 | c = strtok_r(getenv("QUERY_STRING"), "&", &sp0); |
---|
[1797] | 236 | #else |
---|
| 237 | c = strtok(getenv("QUERY_STRING"), "&"); |
---|
| 238 | #endif |
---|
[1018] | 239 | |
---|
| 240 | while (c != NULL) { |
---|
[1797] | 241 | #ifdef ROAR_HAVE_STRTOK_R |
---|
[1018] | 242 | k = strtok_r(c, "=", &sp1); |
---|
| 243 | v = strtok_r(NULL, "=", &sp1); |
---|
[1797] | 244 | #else |
---|
| 245 | k = c; |
---|
| 246 | if ( (v = strstr(c, "=")) != NULL ) { |
---|
| 247 | *v = 0; |
---|
| 248 | v++; |
---|
| 249 | } |
---|
| 250 | #endif |
---|
[1018] | 251 | |
---|
| 252 | if ( !strcmp(k, "codec") ) { |
---|
| 253 | if ( (codec = roar_str2codec(v)) == -1 ) |
---|
| 254 | return 1; |
---|
[1024] | 255 | } else if ( !strcmp(k, "channels") ) { |
---|
| 256 | channels = atoi(v); |
---|
| 257 | } else if ( !strcmp(k, "rate") ) { |
---|
| 258 | rate = atoi(v); |
---|
| 259 | } else if ( !strcmp(k, "bits") ) { |
---|
| 260 | bits = atoi(v); |
---|
[1018] | 261 | } else { |
---|
| 262 | return 1; |
---|
| 263 | } |
---|
| 264 | |
---|
[1797] | 265 | #ifdef ROAR_HAVE_STRTOK_R |
---|
[1018] | 266 | c = strtok_r(NULL, "&", &sp0); |
---|
[1797] | 267 | #else |
---|
| 268 | c = strtok(NULL, "&"); |
---|
| 269 | #endif |
---|
[1018] | 270 | } |
---|
| 271 | |
---|
[1016] | 272 | |
---|
[1130] | 273 | if ( (fh = roar_simple_stream(rate, channels, bits, codec, server, dir, "roarmonhttp")) == -1 ) { |
---|
[1018] | 274 | // fprintf(stderr, "Error: can not start monitoring\n"); |
---|
[1016] | 275 | return 1; |
---|
| 276 | } |
---|
| 277 | |
---|
[1632] | 278 | if ( !gopher ) |
---|
| 279 | print_header(codec, rate, channels); |
---|
[1017] | 280 | |
---|
| 281 | /* |
---|
[1016] | 282 | while((i = read(fh, buf, BUFSIZE))) |
---|
| 283 | if (write(out, buf, i) != i) |
---|
| 284 | break; |
---|
[1017] | 285 | */ |
---|
| 286 | |
---|
[1130] | 287 | switch (dir) { |
---|
| 288 | case ROAR_DIR_PLAY: |
---|
| 289 | stream(fh, ROAR_STDIN); |
---|
| 290 | break; |
---|
| 291 | case ROAR_DIR_MONITOR: |
---|
| 292 | stream(ROAR_STDOUT, fh); |
---|
| 293 | break; |
---|
| 294 | } |
---|
[1016] | 295 | |
---|
| 296 | roar_simple_close(fh); |
---|
| 297 | |
---|
| 298 | return 0; |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | //ll |
---|