source: roaraudio/roarclients/roarmonhttp.c @ 4926:94f6d50ce0bf

Last change on this file since 4926:94f6d50ce0bf was 4926:94f6d50ce0bf, checked in by phi, 13 years ago

added ckport ignore for alarm()

File size: 8.7 KB
Line 
1//roarmonhttp.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011
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/* ckport options:
27 * ckport: ignore-symbol: alarm of target win32
28 *           checked by configure. Needed to clear timeout some small HTTPds implement
29 *           to avoid problems with bad CGI scripts.
30 */
31
32#include <roaraudio.h>
33
34#if defined(ROAR_HAVE_SETENV) || defined(ROAR_HAVE_PUTENV)
35#define _CAN_SET_ENV
36#endif
37
38#define BUFSIZE 1024
39
40void usage (void) {
41 printf("roarmonhttp [OPTIONS]...\n");
42
43 printf("\nOptions:\n\n");
44
45 printf("  --server    SERVER    - Set server hostname\n"
46        "  --rate  -R  RATE      - Set sample rate\n"
47        "  --bits  -B  BITS      - Set bits per sample\n"
48        "  --chans -C  CHANNELS  - Set number of channels\n"
49        "  --codec     CODEC     - Set the codec\n"
50        "  --rel-id ID           - Set ID of relative stream\n"
51        "  --inetd               - Start in inetd mode (STDIN and STDOUT connected to socket)\n"
52        "  --help                - Show this help\n"
53       );
54
55}
56
57void print_header (int codec, int rate, int channels) {
58 const char * mime;
59
60 mime = roar_codec2mime(codec);
61
62 if ( mime == NULL )
63  mime = "application/octet-stream";
64
65 printf("Content-type: %s\r\n", mime);
66 printf("ice-audio-info: ice-samplerate=%i;ice-channels=%i\r\n", rate, channels);
67 printf("icy-pub:0\r\n");
68 printf("Server: RoarAudio (roarmonhttp $Revision$)\r\n");
69 printf("\r\n");
70
71 fflush(stdout);
72}
73
74int stream (struct roar_vio_calls * dest, struct roar_vio_calls * src) {
75 struct roar_vio_select vios[2];
76 struct roar_buffer *ring = NULL, *cur;
77 ssize_t len;
78 size_t  todo;
79 int alive = 1;
80 void * data;
81 int ret;
82
83 roar_vio_nonblock(src,  ROAR_SOCKET_NONBLOCK);
84 roar_vio_nonblock(dest, ROAR_SOCKET_NONBLOCK);
85
86 ROAR_VIO_SELECT_SETVIO(&(vios[0]), src,  ROAR_VIO_SELECT_READ);
87 ROAR_VIO_SELECT_SETVIO(&(vios[1]), dest, ROAR_VIO_SELECT_WRITE);
88
89 while (alive) {
90  ret = roar_vio_select(vios, ring != NULL ? 2 : 1, NULL, NULL);
91  if ( ret == -1 ) {
92   alive = 0;
93  } else if ( ret == 0 ) {
94   // nothing happend.
95  } else {
96   if ( vios[0].eventsa & ROAR_VIO_SELECT_READ ) { // we can read!
97    if ( roar_buffer_new(&cur, BUFSIZE) == -1 )
98     return -1;
99
100    if ( roar_buffer_get_data(cur, &data) == -1 )
101     return -1;
102
103    len = roar_vio_read(src, data, BUFSIZE);
104
105    switch (len) {
106     case  0:
107     case -1:
108       roar_buffer_free(cur);
109       cur = NULL;
110
111       if ( ring != NULL )
112        roar_buffer_free(ring);
113
114       ring = NULL;
115
116       return -1;
117      break;
118    }
119
120    if ( cur != NULL ) {
121     if ( roar_buffer_set_len(cur, len) == -1 )
122      return -1;
123
124     if ( ring == NULL ) {
125      ring = cur;
126     } else {
127      roar_buffer_add(ring, cur);
128     }
129    }
130   } else if ( (vios[1].eventsa & ROAR_VIO_SELECT_WRITE) && ring != NULL ) { // we can write!
131    if ( roar_buffer_get_data(ring, &data) == -1 )
132     return -1;
133
134    if ( roar_buffer_get_len(ring, &todo) == -1 )
135     return -1;
136
137    len = roar_vio_write(dest, data, todo);
138
139    if ( len < 1 ) {
140     if ( roar_error != ROAR_ERROR_AGAIN ) {
141      roar_buffer_free(ring);
142      return -1;
143     }
144    }
145
146    if ( (ssize_t)todo == len ) { // we wrote all of the pkg
147     if ( roar_buffer_next(&ring) == -1 )
148      return -1;
149    } else {
150     if ( roar_buffer_set_offset(ring, len) == -1 )
151      return -1;
152    }
153
154   }
155  }
156 }
157
158 return 0;
159}
160
161#ifdef _CAN_SET_ENV
162int parse_http (int * gopher) {
163 struct roar_keyval kv;
164 char buf[1024];
165 char * qs = buf, *str;
166 ssize_t len;
167 int dir = ROAR_DIR_MONITOR;
168
169 if ( (len = read(ROAR_STDIN, buf, 1023)) == -1 )
170  return -1;
171
172 buf[len] = 0;
173
174 if ( strncmp(buf, "GET /", 5) ) {
175  if ( strncmp(buf, "SOURCE /", 8) ) {
176   if ( buf[0] != '/' ) {
177    return -1;
178   } else {
179    *gopher = 1;
180   }
181  } else {
182   dir = ROAR_DIR_PLAY;
183   qs += 3;
184  }
185 }
186
187 if ( !*gopher ) {
188  qs += 5;
189
190  if ( (str = strstr(qs, " ")) == NULL )
191   return -1;
192
193  *str = 0;
194 } else {
195  if ( (str = strstr(qs, "\r")) != NULL )
196   *str = 0;
197  if ( (str = strstr(qs, "\n")) != NULL )
198   *str = 0;
199 }
200
201 for (; *qs != '?'; qs++)
202  if ( !*qs )
203   break;
204
205 if ( *qs == '?' )
206  qs++;
207
208 if ( !*gopher )
209  printf("HTTP/1.0 200 OK\r\n");
210// printf("QS: %s\r\n", qs);
211
212 fflush(stdout);
213
214 kv.key   = "QUERY_STRING";
215 kv.value = qs;
216
217 roar_env_set(&kv);
218
219 return dir;
220}
221#endif
222
223int main (int argc, char * argv[]) {
224 int    rate     = 44100;
225 int    bits     = 16;
226 int    channels = 2;
227 int    codec    = ROAR_CODEC_OGG_VORBIS;
228 int    rel_id   = -1;
229 int    sflags   = ROAR_FLAG_NONE;
230// int    codec    = ROAR_CODEC_DEFAULT;
231 char * server   = NULL;
232 int    i;
233 char * c, * k, * v;
234#ifdef ROAR_HAVE_STRTOK_R
235 char * sp0 = NULL, * sp1 = NULL;
236#endif
237 int dir = ROAR_DIR_MONITOR;
238 int gopher = 0;
239 struct roar_connection    con;
240 struct roar_stream        s;
241 struct roar_vio_calls   * vio;
242
243#ifdef ROAR_HAVE_ALARM
244 alarm(0); // reset alarm timers from httpd
245#endif
246
247 for (i = 1; i < argc; i++) {
248  k = argv[i];
249  if ( !strcmp(k, "--inetd") ) {
250#ifdef _CAN_SET_ENV
251   if ( (dir = parse_http(&gopher)) == -1 )
252    return 1;
253#else
254   return 1;
255#endif
256  } else if ( !strcmp(k, "--server") ) {
257   roar_libroar_set_server(argv[++i]);
258  } else if ( !strcmp(k, "--codec") ) {
259   codec = roar_str2codec(argv[++i]);
260  } else if ( !strcmp(k, "--rate") || !strcmp(k, "-r") || !strcmp(k, "-R") ) {
261   rate = roar_str2rate(argv[++i]);
262  } else if ( !strcmp(k, "--bits") || !strcmp(k, "-B") ) {
263   bits = roar_str2bits(argv[++i]);
264  } else if ( !strcmp(k, "--channels") || !strcmp(k, "--chans") || !strcmp(k, "-C") ) {
265   channels = roar_str2channels(argv[++i]);
266  } else if ( !strcmp(k, "--rel-id") ) {
267   rel_id = atoi(argv[++i]);
268  } else if ( !strcmp(k, "--help") && !strcmp(k, "-h") ) {
269   usage();
270   return 0;
271  } else {
272   ROAR_ERR("Unknown parameter: %s", k);
273   usage();
274   return 1;
275  }
276 }
277
278 c = getenv("QUERY_STRING");
279 if ( c == NULL )
280  c = "";
281
282#ifdef ROAR_HAVE_STRTOK_R
283 c = strtok_r(c, "&", &sp0);
284#else
285 c = strtok(c, "&");
286#endif
287
288 while (c != NULL) {
289#ifdef ROAR_HAVE_STRTOK_R
290  k = strtok_r(c,    "=", &sp1);
291  v = strtok_r(NULL, "=", &sp1);
292#else
293  k = c;
294  if ( (v = strstr(c, "=")) != NULL ) {
295   *v = 0;
296   v++;
297  }
298#endif
299
300  if ( !strcmp(k, "codec") ) {
301   if ( (codec = roar_str2codec(v)) == -1 )
302    return 1;
303  } else if ( !strcmp(k, "channels") ) {
304   channels = roar_str2channels(v);
305  } else if ( !strcmp(k, "rate") ) {
306   rate = roar_str2rate(v);
307  } else if ( !strcmp(k, "bits") ) {
308   bits = roar_str2bits(v);
309  } else if ( !strcmp(k, "rel-id") || !strcmp(k, "relid") ) {
310   rel_id = atoi(v);
311  } else if ( !strcmp(k, "set-flag") ) {
312   if ( !strcmp(v, "meta") ) {
313    sflags |= ROAR_FLAG_META;
314   } else if ( !strcmp(v, "cleanmeta") ) {
315    sflags |= ROAR_FLAG_CLEANMETA;
316   } else if ( !strcmp(v, "prethru") ) {
317    sflags |= ROAR_FLAG_PRETHRU;
318   } else {
319    return 1;
320   }
321  } else if ( !strcmp(k, "dir") ) {
322   if ( (dir = roar_str2dir(v)) == -1 )
323    return 1;
324  } else {
325   return 1;
326  }
327
328#ifdef ROAR_HAVE_STRTOK_R
329  c = strtok_r(NULL, "&", &sp0);
330#else
331  c = strtok(NULL, "&");
332#endif
333 }
334
335 if ( roar_simple_connect(&con, server, "roarmonhttp") == -1 ) {
336  return 10;
337 }
338
339 if ( roar_stream_new(&s, rate, channels, bits, codec) == -1 ) {
340  roar_disconnect(&con);
341  return 20;
342 }
343
344 if ( rel_id != -1 ) {
345  if ( roar_stream_set_rel_id(&s, rel_id) ) {
346   roar_disconnect(&con);
347   return 21;
348  }
349 }
350
351 if ( roar_stream_connect(&con, &s, dir) == -1 ) {
352  roar_disconnect(&con);
353  return 11;
354 }
355
356 if ( sflags != ROAR_FLAG_NONE ) {
357  if ( roar_stream_set_flags2(&con, &s, sflags, ROAR_SET_FLAG) == -1 ) {
358   roar_disconnect(&con);
359   return 14;
360  }
361 }
362
363 if ( roar_stream_exec(&con, &s) == -1 ) {
364  roar_disconnect(&con);
365  return 12;
366 }
367
368 if ( (vio = roar_get_connection_vio2(&con)) == NULL )
369  return 1;
370
371 if ( !gopher )
372  print_header(codec, rate, channels);
373
374/*
375 while((i = read(fh, buf, BUFSIZE)))
376  if (write(out, buf, i) != i)
377   break;
378*/
379
380 switch (dir) {
381  case ROAR_DIR_PLAY:
382    stream(vio, roar_stdin);
383   break;
384  case ROAR_DIR_MONITOR:
385  case ROAR_DIR_THRU:
386    stream(roar_stdout, vio);
387   break;
388 }
389
390 roar_vio_close(vio);
391
392 return 0;
393}
394
395//ll
Note: See TracBrowser for help on using the repository browser.