1 | //roarmonhttp.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010 |
---|
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, 51 Franklin Street, Fifth Floor, |
---|
22 | * Boston, MA 02110-1301, USA. |
---|
23 | * |
---|
24 | */ |
---|
25 | |
---|
26 | #include <roaraudio.h> |
---|
27 | |
---|
28 | #if defined(ROAR_HAVE_SETENV) || defined(ROAR_HAVE_PUTENV) |
---|
29 | #define _CAN_SET_ENV |
---|
30 | #endif |
---|
31 | |
---|
32 | #define BUFSIZE 1024 |
---|
33 | |
---|
34 | void print_header (int codec, int rate, int channels) { |
---|
35 | char * mime = "application/octet-stream"; |
---|
36 | |
---|
37 | switch (codec) { |
---|
38 | case ROAR_CODEC_OGG_VORBIS: |
---|
39 | mime = "application/ogg"; |
---|
40 | break; |
---|
41 | case ROAR_CODEC_RIFF_WAVE: |
---|
42 | mime = "audio/x-wav"; |
---|
43 | break; |
---|
44 | } |
---|
45 | |
---|
46 | printf("Content-type: %s\r\n", mime); |
---|
47 | printf("ice-audio-info: ice-samplerate=%i;ice-channels=%i\r\n", rate, channels); |
---|
48 | printf("icy-pub:0\r\n"); |
---|
49 | printf("Server: RoarAudio (roarmonhttp $Revision$)\r\n"); |
---|
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); |
---|
72 | if ( ring != NULL ) { |
---|
73 | FD_SET(dest, fso); |
---|
74 | } |
---|
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 | |
---|
101 | if ( roar_buffer_set_len(cur, len) == -1 ) |
---|
102 | return -1; |
---|
103 | |
---|
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 | |
---|
125 | if ( (ssize_t)todo == len ) { // we wrote all of the pkg |
---|
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 | |
---|
140 | #ifdef _CAN_SET_ENV |
---|
141 | int parse_http (int * gopher) { |
---|
142 | struct roar_keyval kv; |
---|
143 | char buf[1024]; |
---|
144 | char * qs = buf, *str; |
---|
145 | ssize_t len; |
---|
146 | int dir = ROAR_DIR_MONITOR; |
---|
147 | |
---|
148 | if ( (len = read(ROAR_STDIN, buf, 1023)) == -1 ) |
---|
149 | return -1; |
---|
150 | |
---|
151 | buf[len] = 0; |
---|
152 | |
---|
153 | if ( strncmp(buf, "GET /", 5) ) { |
---|
154 | if ( strncmp(buf, "SOURCE /", 8) ) { |
---|
155 | if ( buf[0] != '/' ) { |
---|
156 | return -1; |
---|
157 | } else { |
---|
158 | *gopher = 1; |
---|
159 | } |
---|
160 | } else { |
---|
161 | dir = ROAR_DIR_PLAY; |
---|
162 | qs += 3; |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | if ( !*gopher ) { |
---|
167 | qs += 5; |
---|
168 | |
---|
169 | if ( (str = strstr(qs, " ")) == NULL ) |
---|
170 | return -1; |
---|
171 | |
---|
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 | } |
---|
179 | |
---|
180 | for (; *qs != '?'; qs++) |
---|
181 | if ( !*qs ) |
---|
182 | break; |
---|
183 | |
---|
184 | if ( *qs == '?' ) |
---|
185 | qs++; |
---|
186 | |
---|
187 | if ( !*gopher ) |
---|
188 | printf("HTTP/1.0 200 OK\r\n"); |
---|
189 | // printf("QS: %s\r\n", qs); |
---|
190 | |
---|
191 | fflush(stdout); |
---|
192 | |
---|
193 | kv.key = "QUERY_STRING"; |
---|
194 | kv.value = qs; |
---|
195 | |
---|
196 | roar_env_set(&kv); |
---|
197 | |
---|
198 | return dir; |
---|
199 | } |
---|
200 | #endif |
---|
201 | |
---|
202 | int main (int argc, char * argv[]) { |
---|
203 | int rate = 44100; |
---|
204 | int bits = 16; |
---|
205 | int channels = 2; |
---|
206 | int codec = ROAR_CODEC_OGG_VORBIS; |
---|
207 | int rel_id = -1; |
---|
208 | int sflags = ROAR_FLAG_NONE; |
---|
209 | // int codec = ROAR_CODEC_DEFAULT; |
---|
210 | char * server = NULL; |
---|
211 | int fh; |
---|
212 | char * c, * k, * v; |
---|
213 | #ifdef ROAR_HAVE_STRTOK_R |
---|
214 | char * sp0 = NULL, * sp1 = NULL; |
---|
215 | #endif |
---|
216 | int dir = ROAR_DIR_MONITOR; |
---|
217 | int gopher = 0; |
---|
218 | struct roar_connection con; |
---|
219 | struct roar_stream s; |
---|
220 | |
---|
221 | #ifdef ROAR_HAVE_ALARM |
---|
222 | alarm(0); // reset alarm timers from httpd |
---|
223 | #endif |
---|
224 | |
---|
225 | if ( argc > 1 ) |
---|
226 | if ( ! strcmp(argv[1], "--inetd") ) |
---|
227 | #ifdef _CAN_SET_ENV |
---|
228 | if ( (dir = parse_http(&gopher)) == -1 ) |
---|
229 | return 1; |
---|
230 | #else |
---|
231 | return 1; |
---|
232 | #endif |
---|
233 | |
---|
234 | c = getenv("QUERY_STRING"); |
---|
235 | if ( c == NULL ) |
---|
236 | c = ""; |
---|
237 | |
---|
238 | #ifdef ROAR_HAVE_STRTOK_R |
---|
239 | c = strtok_r(c, "&", &sp0); |
---|
240 | #else |
---|
241 | c = strtok(c, "&"); |
---|
242 | #endif |
---|
243 | |
---|
244 | while (c != NULL) { |
---|
245 | #ifdef ROAR_HAVE_STRTOK_R |
---|
246 | k = strtok_r(c, "=", &sp1); |
---|
247 | v = strtok_r(NULL, "=", &sp1); |
---|
248 | #else |
---|
249 | k = c; |
---|
250 | if ( (v = strstr(c, "=")) != NULL ) { |
---|
251 | *v = 0; |
---|
252 | v++; |
---|
253 | } |
---|
254 | #endif |
---|
255 | |
---|
256 | if ( !strcmp(k, "codec") ) { |
---|
257 | if ( (codec = roar_str2codec(v)) == -1 ) |
---|
258 | return 1; |
---|
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); |
---|
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; |
---|
280 | } else { |
---|
281 | return 1; |
---|
282 | } |
---|
283 | |
---|
284 | #ifdef ROAR_HAVE_STRTOK_R |
---|
285 | c = strtok_r(NULL, "&", &sp0); |
---|
286 | #else |
---|
287 | c = strtok(NULL, "&"); |
---|
288 | #endif |
---|
289 | } |
---|
290 | |
---|
291 | if ( roar_simple_connect(&con, server, "roarmonhttp") == -1 ) { |
---|
292 | return 10; |
---|
293 | } |
---|
294 | |
---|
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 ) |
---|
325 | return 1; |
---|
326 | |
---|
327 | if ( !gopher ) |
---|
328 | print_header(codec, rate, channels); |
---|
329 | |
---|
330 | /* |
---|
331 | while((i = read(fh, buf, BUFSIZE))) |
---|
332 | if (write(out, buf, i) != i) |
---|
333 | break; |
---|
334 | */ |
---|
335 | |
---|
336 | switch (dir) { |
---|
337 | case ROAR_DIR_PLAY: |
---|
338 | stream(fh, ROAR_STDIN); |
---|
339 | break; |
---|
340 | case ROAR_DIR_MONITOR: |
---|
341 | case ROAR_DIR_THRU: |
---|
342 | stream(ROAR_STDOUT, fh); |
---|
343 | break; |
---|
344 | } |
---|
345 | |
---|
346 | roar_simple_close(fh); |
---|
347 | |
---|
348 | return 0; |
---|
349 | } |
---|
350 | |
---|
351 | //ll |
---|