[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]; |
---|
| 138 | char * qs, *str; |
---|
| 139 | ssize_t len; |
---|
| 140 | |
---|
| 141 | if ( (len = read(ROAR_STDIN, buf, 1023)) == -1 ) |
---|
| 142 | return -1; |
---|
| 143 | |
---|
| 144 | buf[len] = 0; |
---|
| 145 | |
---|
| 146 | if ( strncmp(buf, "GET /", 5) ) |
---|
| 147 | return -1; |
---|
| 148 | |
---|
| 149 | qs = buf+5; |
---|
| 150 | |
---|
| 151 | if ( (str = strstr(qs, " ")) == NULL ) |
---|
| 152 | return -1; |
---|
| 153 | |
---|
| 154 | *str = 0; |
---|
| 155 | |
---|
| 156 | for (; *qs != '?'; qs++) |
---|
| 157 | if ( !*qs ) |
---|
| 158 | break; |
---|
| 159 | |
---|
| 160 | if ( *qs == '?' ) |
---|
| 161 | qs++; |
---|
| 162 | |
---|
| 163 | printf("HTTP/1.0 200 OK\r\n"); |
---|
| 164 | // printf("QS: %s\r\n", qs); |
---|
| 165 | |
---|
| 166 | fflush(stdout); |
---|
| 167 | |
---|
| 168 | setenv("QUERY_STRING", qs, 1); |
---|
| 169 | |
---|
| 170 | return 0; |
---|
| 171 | } |
---|
| 172 | |
---|
[1016] | 173 | int main (int argc, char * argv[]) { |
---|
| 174 | int rate = 44100; |
---|
| 175 | int bits = 16; |
---|
| 176 | int channels = 2; |
---|
[1018] | 177 | int codec = ROAR_CODEC_OGG_VORBIS; |
---|
| 178 | // int codec = ROAR_CODEC_DEFAULT; |
---|
[1016] | 179 | char * server = NULL; |
---|
| 180 | int fh; |
---|
[1018] | 181 | char * c, * k, * v; |
---|
| 182 | char * sp0, * sp1; |
---|
[1023] | 183 | |
---|
| 184 | alarm(0); // reset alarm timers from httpd |
---|
[1018] | 185 | |
---|
[1025] | 186 | if ( argc > 1 ) |
---|
| 187 | if ( ! strcmp(argv[1], "--inetd") ) |
---|
| 188 | if ( parse_http() == -1 ) |
---|
| 189 | return 1; |
---|
| 190 | |
---|
[1018] | 191 | c = strtok_r(getenv("QUERY_STRING"), "&", &sp0); |
---|
| 192 | |
---|
| 193 | while (c != NULL) { |
---|
| 194 | k = strtok_r(c, "=", &sp1); |
---|
| 195 | v = strtok_r(NULL, "=", &sp1); |
---|
| 196 | |
---|
| 197 | if ( !strcmp(k, "codec") ) { |
---|
| 198 | if ( (codec = roar_str2codec(v)) == -1 ) |
---|
| 199 | return 1; |
---|
[1024] | 200 | } else if ( !strcmp(k, "channels") ) { |
---|
| 201 | channels = atoi(v); |
---|
| 202 | } else if ( !strcmp(k, "rate") ) { |
---|
| 203 | rate = atoi(v); |
---|
| 204 | } else if ( !strcmp(k, "bits") ) { |
---|
| 205 | bits = atoi(v); |
---|
[1018] | 206 | } else { |
---|
| 207 | return 1; |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | c = strtok_r(NULL, "&", &sp0); |
---|
| 211 | } |
---|
| 212 | |
---|
[1016] | 213 | |
---|
| 214 | if ( (fh = roar_simple_monitor(rate, channels, bits, codec, server, "roarmon")) == -1 ) { |
---|
[1018] | 215 | // fprintf(stderr, "Error: can not start monitoring\n"); |
---|
[1016] | 216 | return 1; |
---|
| 217 | } |
---|
| 218 | |
---|
[1027] | 219 | print_header(codec, rate, channels); |
---|
[1017] | 220 | |
---|
| 221 | /* |
---|
[1016] | 222 | while((i = read(fh, buf, BUFSIZE))) |
---|
| 223 | if (write(out, buf, i) != i) |
---|
| 224 | break; |
---|
[1017] | 225 | */ |
---|
| 226 | |
---|
| 227 | stream(ROAR_STDOUT, fh); |
---|
[1016] | 228 | |
---|
| 229 | roar_simple_close(fh); |
---|
| 230 | |
---|
| 231 | return 0; |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | //ll |
---|