source: roaraudio/roarclients/roarmonhttp.c @ 2813:f677c459d89d

Last change on this file since 2813:f677c459d89d was 2813:f677c459d89d, checked in by phi, 15 years ago

added prethru support

File size: 7.1 KB
Line 
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#if defined(ROAR_HAVE_SETENV) || defined(ROAR_HAVE_PUTENV)
28#define _CAN_SET_ENV
29#endif
30
31#define BUFSIZE 1024
32
33void print_header (int codec, int rate, int channels) {
34 char * mime = "application/octet-stream";
35
36 switch (codec) {
37  case ROAR_CODEC_OGG_VORBIS:
38    mime = "application/ogg";
39   break;
40  case ROAR_CODEC_RIFF_WAVE:
41    mime = "audio/x-wav";
42   break;
43 }
44
45 printf("Content-type: %s\r\n", mime);
46 printf("ice-audio-info: ice-samplerate=%i;ice-channels=%i\r\n", rate, channels);
47 printf("icy-pub:0\r\n");
48 printf("Server: RoarAudio (roarmonhttp $Revision$)\r\n");
49 printf("\r\n");
50
51 fflush(stdout);
52}
53
54int 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);
71  if ( ring != NULL ) {
72   FD_SET(dest, fso);
73  }
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
100    if ( roar_buffer_set_len(cur, len) == -1 )
101     return -1;
102
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
139#ifdef _CAN_SET_ENV
140int parse_http (int * gopher) {
141 char buf[1024];
142 char * qs = buf, *str;
143 ssize_t len;
144 int dir = ROAR_DIR_MONITOR;
145
146 if ( (len = read(ROAR_STDIN, buf, 1023)) == -1 )
147  return -1;
148
149 buf[len] = 0;
150
151 if ( strncmp(buf, "GET /", 5) ) {
152  if ( strncmp(buf, "SOURCE /", 8) ) {
153   if ( buf[0] != '/' ) {
154    return -1;
155   } else {
156    *gopher = 1;
157   }
158  } else {
159   dir = ROAR_DIR_PLAY;
160   qs += 3;
161  }
162 }
163
164 if ( !*gopher ) {
165  qs += 5;
166
167  if ( (str = strstr(qs, " ")) == NULL )
168   return -1;
169
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 }
177
178 for (; *qs != '?'; qs++)
179  if ( !*qs )
180   break;
181
182 if ( *qs == '?' )
183  qs++;
184
185 if ( !*gopher )
186  printf("HTTP/1.0 200 OK\r\n");
187// printf("QS: %s\r\n", qs);
188
189 fflush(stdout);
190
191#ifdef ROAR_HAVE_SETENV
192 setenv("QUERY_STRING", qs, 1);
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
203
204 return dir;
205}
206#endif
207
208int main (int argc, char * argv[]) {
209 int    rate     = 44100;
210 int    bits     = 16;
211 int    channels = 2;
212 int    codec    = ROAR_CODEC_OGG_VORBIS;
213 int    rel_id   = -1;
214 int    sflags   = ROAR_FLAG_NONE;
215// int    codec    = ROAR_CODEC_DEFAULT;
216 char * server   = NULL;
217 int    fh;
218 char * c, * k, * v;
219#ifdef ROAR_HAVE_STRTOK_R
220 char * sp0 = NULL, * sp1 = NULL;
221#endif
222 int dir = ROAR_DIR_MONITOR;
223 int gopher = 0;
224 struct roar_connection    con;
225 struct roar_stream        s;
226
227#ifdef ROAR_HAVE_ALARM
228 alarm(0); // reset alarm timers from httpd
229#endif
230
231 if ( argc > 1 )
232  if ( ! strcmp(argv[1], "--inetd") )
233#ifdef _CAN_SET_ENV
234   if ( (dir = parse_http(&gopher)) == -1 )
235    return 1;
236#else
237   return 1;
238#endif
239
240#ifdef ROAR_HAVE_STRTOK_R
241 c = strtok_r(getenv("QUERY_STRING"), "&", &sp0);
242#else
243 c = strtok(getenv("QUERY_STRING"), "&");
244#endif
245
246 while (c != NULL) {
247#ifdef ROAR_HAVE_STRTOK_R
248  k = strtok_r(c,    "=", &sp1);
249  v = strtok_r(NULL, "=", &sp1);
250#else
251  k = c;
252  if ( (v = strstr(c, "=")) != NULL ) {
253   *v = 0;
254   v++;
255  }
256#endif
257
258  if ( !strcmp(k, "codec") ) {
259   if ( (codec = roar_str2codec(v)) == -1 )
260    return 1;
261  } else if ( !strcmp(k, "channels") ) {
262   channels = atoi(v);
263  } else if ( !strcmp(k, "rate") ) {
264   rate = atoi(v);
265  } else if ( !strcmp(k, "bits") ) {
266   bits = atoi(v);
267  } else if ( !strcmp(k, "rel-id") || !strcmp(k, "relid") ) {
268   rel_id = atoi(v);
269  } else if ( !strcmp(k, "set-flag") ) {
270   if ( !strcmp(v, "meta") ) {
271    sflags |= ROAR_FLAG_META;
272   } else if ( !strcmp(v, "cleanmeta") ) {
273    sflags |= ROAR_FLAG_CLEANMETA;
274   } else if ( !strcmp(v, "prethru") ) {
275    sflags |= ROAR_FLAG_PRETHRU;
276   } else {
277    return 1;
278   }
279  } else if ( !strcmp(k, "dir") ) {
280   if ( (dir = roar_str2dir(v)) == -1 )
281    return 1;
282  } else {
283   return 1;
284  }
285
286#ifdef ROAR_HAVE_STRTOK_R
287  c = strtok_r(NULL, "&", &sp0);
288#else
289  c = strtok(NULL, "&");
290#endif
291 }
292
293 if ( roar_simple_connect(&con, server, "roarmonhttp") == -1 ) {
294  return 10;
295 }
296
297 if ( roar_stream_new(&s, rate, channels, bits, codec) == -1 ) {
298  roar_disconnect(&con);
299  return 20;
300 }
301
302 if ( rel_id != -1 ) {
303  if ( roar_stream_set_rel_id(&s, rel_id) ) {
304   roar_disconnect(&con);
305   return 21;
306  }
307 }
308
309 if ( roar_stream_connect(&con, &s, dir) == -1 ) {
310  roar_disconnect(&con);
311  return 11;
312 }
313
314 if ( sflags != ROAR_FLAG_NONE ) {
315  if ( roar_stream_set_flags(&con, &s, sflags, 0) == -1 ) {
316   roar_disconnect(&con);
317   return 14;
318  }
319 }
320
321 if ( roar_stream_exec(&con, &s) == -1 ) {
322  roar_disconnect(&con);
323  return 12;
324 }
325
326 if ( (fh = roar_get_connection_fh(&con)) == -1 )
327  return 1;
328
329 if ( !gopher )
330  print_header(codec, rate, channels);
331
332/*
333 while((i = read(fh, buf, BUFSIZE)))
334  if (write(out, buf, i) != i)
335   break;
336*/
337
338 switch (dir) {
339  case ROAR_DIR_PLAY:
340    stream(fh, ROAR_STDIN);
341   break;
342  case ROAR_DIR_MONITOR:
343  case ROAR_DIR_THRU:
344    stream(ROAR_STDOUT, fh);
345   break;
346 }
347
348 roar_simple_close(fh);
349
350 return 0;
351}
352
353//ll
Note: See TracBrowser for help on using the repository browser.