[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 | |
---|
| 27 | #define BUFSIZE 1024 |
---|
| 28 | |
---|
[1027] | 29 | void print_header (int codec, int rate, int channels) { |
---|
[1017] | 30 | char * mime = "application/octet-stream"; |
---|
| 31 | |
---|
| 32 | switch (codec) { |
---|
| 33 | case ROAR_CODEC_OGG_VORBIS: |
---|
| 34 | mime = "application/ogg"; |
---|
| 35 | break; |
---|
[1018] | 36 | case ROAR_CODEC_RIFF_WAVE: |
---|
| 37 | mime = "audio/x-wav"; |
---|
| 38 | break; |
---|
[1017] | 39 | } |
---|
| 40 | |
---|
| 41 | printf("Content-type: %s\r\n", mime); |
---|
[1027] | 42 | printf("ice-audio-info: ice-samplerate=%i;ice-channels=%i\r\n", rate, channels); |
---|
| 43 | printf("icy-pub:0\r\n"); |
---|
[1024] | 44 | printf("Server: RoarAudio (roarmonhttp $Revision$)\r\n"); |
---|
[1017] | 45 | printf("\r\n"); |
---|
| 46 | |
---|
| 47 | fflush(stdout); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | int stream (int dest, int src) { |
---|
| 51 | struct roar_buffer *ring = NULL, *cur; |
---|
| 52 | ssize_t len; |
---|
| 53 | size_t todo; |
---|
| 54 | fd_set fsi[1], fso[1]; |
---|
| 55 | struct timeval tv; |
---|
| 56 | int alive = 1; |
---|
| 57 | int maxfh = (dest > src ? dest : src) + 1; |
---|
| 58 | void * data; |
---|
| 59 | |
---|
| 60 | roar_socket_nonblock(src, ROAR_SOCKET_NONBLOCK); |
---|
| 61 | roar_socket_nonblock(dest, ROAR_SOCKET_NONBLOCK); |
---|
| 62 | |
---|
| 63 | while (alive) { |
---|
| 64 | FD_ZERO(fsi); |
---|
| 65 | FD_ZERO(fso); |
---|
| 66 | FD_SET(src, fsi); |
---|
[1018] | 67 | if ( ring != NULL ) { |
---|
| 68 | FD_SET(dest, fso); |
---|
| 69 | } |
---|
[1017] | 70 | |
---|
| 71 | tv.tv_sec = 0; |
---|
| 72 | tv.tv_usec = 100000; // 100ms |
---|
| 73 | |
---|
| 74 | if (select(maxfh, fsi, fso, NULL, &tv) > 0) { |
---|
| 75 | if ( FD_ISSET(src, fsi) ) { // we can read! |
---|
| 76 | if ( roar_buffer_new(&cur, BUFSIZE) == -1 ) |
---|
| 77 | return -1; |
---|
| 78 | |
---|
| 79 | if ( roar_buffer_get_data(cur, &data) == -1 ) |
---|
| 80 | return -1; |
---|
| 81 | |
---|
| 82 | len = read(src, data, BUFSIZE); |
---|
| 83 | |
---|
| 84 | switch (len) { |
---|
| 85 | case 0: |
---|
| 86 | case -1: |
---|
| 87 | roar_buffer_free(cur); |
---|
| 88 | |
---|
| 89 | if ( ring != NULL ) |
---|
| 90 | roar_buffer_free(ring); |
---|
| 91 | |
---|
| 92 | return -1; |
---|
| 93 | break; |
---|
| 94 | } |
---|
| 95 | |
---|
[1018] | 96 | if ( roar_buffer_set_len(cur, len) == -1 ) |
---|
| 97 | return -1; |
---|
| 98 | |
---|
[1017] | 99 | if ( ring == NULL ) { |
---|
| 100 | ring = cur; |
---|
| 101 | } else { |
---|
| 102 | roar_buffer_add(ring, cur); |
---|
| 103 | } |
---|
| 104 | } else if ( FD_ISSET(dest, fso) && ring != NULL ) { // we can write! |
---|
| 105 | if ( roar_buffer_get_data(ring, &data) == -1 ) |
---|
| 106 | return -1; |
---|
| 107 | |
---|
| 108 | if ( roar_buffer_get_len(ring, &todo) == -1 ) |
---|
| 109 | return -1; |
---|
| 110 | |
---|
| 111 | len = write(dest, data, todo); |
---|
| 112 | |
---|
| 113 | if ( len < 1 ) { |
---|
| 114 | if ( errno != EAGAIN ) { |
---|
| 115 | roar_buffer_free(ring); |
---|
| 116 | return -1; |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | if ( todo == len ) { // we wrote all of the pkg |
---|
| 121 | if ( roar_buffer_next(&ring) == -1 ) |
---|
| 122 | return -1; |
---|
| 123 | } else { |
---|
| 124 | if ( roar_buffer_set_offset(ring, len) == -1 ) |
---|
| 125 | return -1; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | } |
---|
| 129 | } |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | return 0; |
---|
| 133 | } |
---|
| 134 | |
---|
[1025] | 135 | |
---|
| 136 | int parse_http (void) { |
---|
| 137 | char buf[1024]; |
---|
[1130] | 138 | char * qs = buf, *str; |
---|
[1025] | 139 | ssize_t len; |
---|
[1130] | 140 | int dir = ROAR_DIR_MONITOR; |
---|
[1025] | 141 | |
---|
| 142 | if ( (len = read(ROAR_STDIN, buf, 1023)) == -1 ) |
---|
| 143 | return -1; |
---|
| 144 | |
---|
| 145 | buf[len] = 0; |
---|
| 146 | |
---|
[1130] | 147 | if ( strncmp(buf, "GET /", 5) ) { |
---|
| 148 | if ( strncmp(buf, "SOURCE /", 8) ) { |
---|
| 149 | return -1; |
---|
| 150 | } else { |
---|
| 151 | dir = ROAR_DIR_PLAY; |
---|
| 152 | qs += 3; |
---|
| 153 | } |
---|
| 154 | } |
---|
[1025] | 155 | |
---|
[1130] | 156 | qs += 5; |
---|
[1025] | 157 | |
---|
| 158 | if ( (str = strstr(qs, " ")) == NULL ) |
---|
| 159 | return -1; |
---|
| 160 | |
---|
| 161 | *str = 0; |
---|
| 162 | |
---|
| 163 | for (; *qs != '?'; qs++) |
---|
| 164 | if ( !*qs ) |
---|
| 165 | break; |
---|
| 166 | |
---|
| 167 | if ( *qs == '?' ) |
---|
| 168 | qs++; |
---|
| 169 | |
---|
| 170 | printf("HTTP/1.0 200 OK\r\n"); |
---|
| 171 | // printf("QS: %s\r\n", qs); |
---|
| 172 | |
---|
| 173 | fflush(stdout); |
---|
| 174 | |
---|
| 175 | setenv("QUERY_STRING", qs, 1); |
---|
| 176 | |
---|
[1130] | 177 | return dir; |
---|
[1025] | 178 | } |
---|
| 179 | |
---|
[1016] | 180 | int main (int argc, char * argv[]) { |
---|
| 181 | int rate = 44100; |
---|
| 182 | int bits = 16; |
---|
| 183 | int channels = 2; |
---|
[1018] | 184 | int codec = ROAR_CODEC_OGG_VORBIS; |
---|
| 185 | // int codec = ROAR_CODEC_DEFAULT; |
---|
[1016] | 186 | char * server = NULL; |
---|
| 187 | int fh; |
---|
[1018] | 188 | char * c, * k, * v; |
---|
| 189 | char * sp0, * sp1; |
---|
[1130] | 190 | int dir = ROAR_DIR_MONITOR; |
---|
[1023] | 191 | |
---|
| 192 | alarm(0); // reset alarm timers from httpd |
---|
[1018] | 193 | |
---|
[1025] | 194 | if ( argc > 1 ) |
---|
| 195 | if ( ! strcmp(argv[1], "--inetd") ) |
---|
[1130] | 196 | if ( (dir = parse_http()) == -1 ) |
---|
[1025] | 197 | return 1; |
---|
| 198 | |
---|
[1018] | 199 | c = strtok_r(getenv("QUERY_STRING"), "&", &sp0); |
---|
| 200 | |
---|
| 201 | while (c != NULL) { |
---|
| 202 | k = strtok_r(c, "=", &sp1); |
---|
| 203 | v = strtok_r(NULL, "=", &sp1); |
---|
| 204 | |
---|
| 205 | if ( !strcmp(k, "codec") ) { |
---|
| 206 | if ( (codec = roar_str2codec(v)) == -1 ) |
---|
| 207 | return 1; |
---|
[1024] | 208 | } else if ( !strcmp(k, "channels") ) { |
---|
| 209 | channels = atoi(v); |
---|
| 210 | } else if ( !strcmp(k, "rate") ) { |
---|
| 211 | rate = atoi(v); |
---|
| 212 | } else if ( !strcmp(k, "bits") ) { |
---|
| 213 | bits = atoi(v); |
---|
[1018] | 214 | } else { |
---|
| 215 | return 1; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | c = strtok_r(NULL, "&", &sp0); |
---|
| 219 | } |
---|
| 220 | |
---|
[1016] | 221 | |
---|
[1130] | 222 | if ( (fh = roar_simple_stream(rate, channels, bits, codec, server, dir, "roarmonhttp")) == -1 ) { |
---|
[1018] | 223 | // fprintf(stderr, "Error: can not start monitoring\n"); |
---|
[1016] | 224 | return 1; |
---|
| 225 | } |
---|
| 226 | |
---|
[1027] | 227 | print_header(codec, rate, channels); |
---|
[1017] | 228 | |
---|
| 229 | /* |
---|
[1016] | 230 | while((i = read(fh, buf, BUFSIZE))) |
---|
| 231 | if (write(out, buf, i) != i) |
---|
| 232 | break; |
---|
[1017] | 233 | */ |
---|
| 234 | |
---|
[1130] | 235 | switch (dir) { |
---|
| 236 | case ROAR_DIR_PLAY: |
---|
| 237 | stream(fh, ROAR_STDIN); |
---|
| 238 | break; |
---|
| 239 | case ROAR_DIR_MONITOR: |
---|
| 240 | stream(ROAR_STDOUT, fh); |
---|
| 241 | break; |
---|
| 242 | } |
---|
[1016] | 243 | |
---|
| 244 | roar_simple_close(fh); |
---|
| 245 | |
---|
| 246 | return 0; |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | //ll |
---|