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