source: roaraudio/roarclients/roarmonhttp.c @ 3790:1ba31162e041

Last change on this file since 3790:1ba31162e041 was 3790:1ba31162e041, checked in by phi, 14 years ago

fixed copyright

File size: 7.1 KB
Line 
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
34void 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
55int 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
141int parse_http (int * gopher) {
142 char buf[1024];
143 char * qs = buf, *str;
144 ssize_t len;
145 int dir = ROAR_DIR_MONITOR;
146
147 if ( (len = read(ROAR_STDIN, buf, 1023)) == -1 )
148  return -1;
149
150 buf[len] = 0;
151
152 if ( strncmp(buf, "GET /", 5) ) {
153  if ( strncmp(buf, "SOURCE /", 8) ) {
154   if ( buf[0] != '/' ) {
155    return -1;
156   } else {
157    *gopher = 1;
158   }
159  } else {
160   dir = ROAR_DIR_PLAY;
161   qs += 3;
162  }
163 }
164
165 if ( !*gopher ) {
166  qs += 5;
167
168  if ( (str = strstr(qs, " ")) == NULL )
169   return -1;
170
171  *str = 0;
172 } else {
173  if ( (str = strstr(qs, "\r")) != NULL )
174   *str = 0;
175  if ( (str = strstr(qs, "\n")) != NULL )
176   *str = 0;
177 }
178
179 for (; *qs != '?'; qs++)
180  if ( !*qs )
181   break;
182
183 if ( *qs == '?' )
184  qs++;
185
186 if ( !*gopher )
187  printf("HTTP/1.0 200 OK\r\n");
188// printf("QS: %s\r\n", qs);
189
190 fflush(stdout);
191
192#ifdef ROAR_HAVE_SETENV
193 setenv("QUERY_STRING", qs, 1);
194#else
195 // TODO: does this leak memory?
196 if ( (str = malloc(strlen(qs) + strlen("QUERY_STRING=") + 1)) == NULL ) {
197  return -1;
198 }
199
200 sprintf(str, "QUERY_STRING=%s", qs);
201
202 putenv(str);
203#endif
204
205 return dir;
206}
207#endif
208
209int main (int argc, char * argv[]) {
210 int    rate     = 44100;
211 int    bits     = 16;
212 int    channels = 2;
213 int    codec    = ROAR_CODEC_OGG_VORBIS;
214 int    rel_id   = -1;
215 int    sflags   = ROAR_FLAG_NONE;
216// int    codec    = ROAR_CODEC_DEFAULT;
217 char * server   = NULL;
218 int    fh;
219 char * c, * k, * v;
220#ifdef ROAR_HAVE_STRTOK_R
221 char * sp0 = NULL, * sp1 = NULL;
222#endif
223 int dir = ROAR_DIR_MONITOR;
224 int gopher = 0;
225 struct roar_connection    con;
226 struct roar_stream        s;
227
228#ifdef ROAR_HAVE_ALARM
229 alarm(0); // reset alarm timers from httpd
230#endif
231
232 if ( argc > 1 )
233  if ( ! strcmp(argv[1], "--inetd") )
234#ifdef _CAN_SET_ENV
235   if ( (dir = parse_http(&gopher)) == -1 )
236    return 1;
237#else
238   return 1;
239#endif
240
241 c = getenv("QUERY_STRING");
242 if ( c == NULL )
243  c = "";
244
245#ifdef ROAR_HAVE_STRTOK_R
246 c = strtok_r(c, "&", &sp0);
247#else
248 c = strtok(c, "&");
249#endif
250
251 while (c != NULL) {
252#ifdef ROAR_HAVE_STRTOK_R
253  k = strtok_r(c,    "=", &sp1);
254  v = strtok_r(NULL, "=", &sp1);
255#else
256  k = c;
257  if ( (v = strstr(c, "=")) != NULL ) {
258   *v = 0;
259   v++;
260  }
261#endif
262
263  if ( !strcmp(k, "codec") ) {
264   if ( (codec = roar_str2codec(v)) == -1 )
265    return 1;
266  } else if ( !strcmp(k, "channels") ) {
267   channels = atoi(v);
268  } else if ( !strcmp(k, "rate") ) {
269   rate = atoi(v);
270  } else if ( !strcmp(k, "bits") ) {
271   bits = atoi(v);
272  } else if ( !strcmp(k, "rel-id") || !strcmp(k, "relid") ) {
273   rel_id = atoi(v);
274  } else if ( !strcmp(k, "set-flag") ) {
275   if ( !strcmp(v, "meta") ) {
276    sflags |= ROAR_FLAG_META;
277   } else if ( !strcmp(v, "cleanmeta") ) {
278    sflags |= ROAR_FLAG_CLEANMETA;
279   } else if ( !strcmp(v, "prethru") ) {
280    sflags |= ROAR_FLAG_PRETHRU;
281   } else {
282    return 1;
283   }
284  } else if ( !strcmp(k, "dir") ) {
285   if ( (dir = roar_str2dir(v)) == -1 )
286    return 1;
287  } else {
288   return 1;
289  }
290
291#ifdef ROAR_HAVE_STRTOK_R
292  c = strtok_r(NULL, "&", &sp0);
293#else
294  c = strtok(NULL, "&");
295#endif
296 }
297
298 if ( roar_simple_connect(&con, server, "roarmonhttp") == -1 ) {
299  return 10;
300 }
301
302 if ( roar_stream_new(&s, rate, channels, bits, codec) == -1 ) {
303  roar_disconnect(&con);
304  return 20;
305 }
306
307 if ( rel_id != -1 ) {
308  if ( roar_stream_set_rel_id(&s, rel_id) ) {
309   roar_disconnect(&con);
310   return 21;
311  }
312 }
313
314 if ( roar_stream_connect(&con, &s, dir) == -1 ) {
315  roar_disconnect(&con);
316  return 11;
317 }
318
319 if ( sflags != ROAR_FLAG_NONE ) {
320  if ( roar_stream_set_flags(&con, &s, sflags, 0) == -1 ) {
321   roar_disconnect(&con);
322   return 14;
323  }
324 }
325
326 if ( roar_stream_exec(&con, &s) == -1 ) {
327  roar_disconnect(&con);
328  return 12;
329 }
330
331 if ( (fh = roar_get_connection_fh(&con)) == -1 )
332  return 1;
333
334 if ( !gopher )
335  print_header(codec, rate, channels);
336
337/*
338 while((i = read(fh, buf, BUFSIZE)))
339  if (write(out, buf, i) != i)
340   break;
341*/
342
343 switch (dir) {
344  case ROAR_DIR_PLAY:
345    stream(fh, ROAR_STDIN);
346   break;
347  case ROAR_DIR_MONITOR:
348  case ROAR_DIR_THRU:
349    stream(ROAR_STDOUT, fh);
350   break;
351 }
352
353 roar_simple_close(fh);
354
355 return 0;
356}
357
358//ll
Note: See TracBrowser for help on using the repository browser.