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
RevLine 
[1016]1//roarmonhttp.c:
2
3/*
[4708]4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011
[1016]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
[3517]21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
[1016]23 *
24 */
25
[4926]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
[1016]32#include <roaraudio.h>
33
[1785]34#if defined(ROAR_HAVE_SETENV) || defined(ROAR_HAVE_PUTENV)
35#define _CAN_SET_ENV
36#endif
37
[1016]38#define BUFSIZE 1024
39
[4132]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
[1027]57void print_header (int codec, int rate, int channels) {
[4602]58 const char * mime;
[1017]59
[4602]60 mime = roar_codec2mime(codec);
61
62 if ( mime == NULL )
63  mime = "application/octet-stream";
[1017]64
65 printf("Content-type: %s\r\n", mime);
[1027]66 printf("ice-audio-info: ice-samplerate=%i;ice-channels=%i\r\n", rate, channels);
67 printf("icy-pub:0\r\n");
[1024]68 printf("Server: RoarAudio (roarmonhttp $Revision$)\r\n");
[1017]69 printf("\r\n");
70
71 fflush(stdout);
72}
73
[4925]74int stream (struct roar_vio_calls * dest, struct roar_vio_calls * src) {
75 struct roar_vio_select vios[2];
[1017]76 struct roar_buffer *ring = NULL, *cur;
77 ssize_t len;
78 size_t  todo;
79 int alive = 1;
80 void * data;
[4925]81 int ret;
[1017]82
[4925]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);
[1017]88
89 while (alive) {
[4925]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!
[1017]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
[4925]103    len = roar_vio_read(src, data, BUFSIZE);
[1017]104
105    switch (len) {
106     case  0:
107     case -1:
108       roar_buffer_free(cur);
[4925]109       cur = NULL;
[1017]110
111       if ( ring != NULL )
112        roar_buffer_free(ring);
113
[4925]114       ring = NULL;
115
[1017]116       return -1;
117      break;
118    }
119
[4925]120    if ( cur != NULL ) {
121     if ( roar_buffer_set_len(cur, len) == -1 )
122      return -1;
[1018]123
[4925]124     if ( ring == NULL ) {
125      ring = cur;
126     } else {
127      roar_buffer_add(ring, cur);
128     }
[1017]129    }
[4925]130   } else if ( (vios[1].eventsa & ROAR_VIO_SELECT_WRITE) && ring != NULL ) { // we can write!
[1017]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
[4925]137    len = roar_vio_write(dest, data, todo);
[1017]138
139    if ( len < 1 ) {
[4925]140     if ( roar_error != ROAR_ERROR_AGAIN ) {
[1017]141      roar_buffer_free(ring);
142      return -1;
143     }
144    }
145
[3641]146    if ( (ssize_t)todo == len ) { // we wrote all of the pkg
[1017]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
[1785]161#ifdef _CAN_SET_ENV
[1632]162int parse_http (int * gopher) {
[3967]163 struct roar_keyval kv;
[1025]164 char buf[1024];
[1130]165 char * qs = buf, *str;
[1025]166 ssize_t len;
[1130]167 int dir = ROAR_DIR_MONITOR;
[1025]168
169 if ( (len = read(ROAR_STDIN, buf, 1023)) == -1 )
170  return -1;
171
172 buf[len] = 0;
173
[1130]174 if ( strncmp(buf, "GET /", 5) ) {
175  if ( strncmp(buf, "SOURCE /", 8) ) {
[1632]176   if ( buf[0] != '/' ) {
177    return -1;
178   } else {
179    *gopher = 1;
180   }
[1130]181  } else {
182   dir = ROAR_DIR_PLAY;
183   qs += 3;
184  }
185 }
[1025]186
[1632]187 if ( !*gopher ) {
188  qs += 5;
189
190  if ( (str = strstr(qs, " ")) == NULL )
191   return -1;
[1025]192
[1632]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 }
[1025]200
201 for (; *qs != '?'; qs++)
202  if ( !*qs )
203   break;
204
205 if ( *qs == '?' )
206  qs++;
207
[1632]208 if ( !*gopher )
209  printf("HTTP/1.0 200 OK\r\n");
[1025]210// printf("QS: %s\r\n", qs);
211
212 fflush(stdout);
213
[3967]214 kv.key   = "QUERY_STRING";
215 kv.value = qs;
[1785]216
[3967]217 roar_env_set(&kv);
[1025]218
[1130]219 return dir;
[1025]220}
[1785]221#endif
[1025]222
[1016]223int main (int argc, char * argv[]) {
224 int    rate     = 44100;
225 int    bits     = 16;
226 int    channels = 2;
[1018]227 int    codec    = ROAR_CODEC_OGG_VORBIS;
[2813]228 int    rel_id   = -1;
229 int    sflags   = ROAR_FLAG_NONE;
[1018]230// int    codec    = ROAR_CODEC_DEFAULT;
[1016]231 char * server   = NULL;
[4132]232 int    i;
[1018]233 char * c, * k, * v;
[2220]234#ifdef ROAR_HAVE_STRTOK_R
[1605]235 char * sp0 = NULL, * sp1 = NULL;
[2220]236#endif
[1130]237 int dir = ROAR_DIR_MONITOR;
[1632]238 int gopher = 0;
[2813]239 struct roar_connection    con;
240 struct roar_stream        s;
[4925]241 struct roar_vio_calls   * vio;
[1023]242
[1783]243#ifdef ROAR_HAVE_ALARM
[1023]244 alarm(0); // reset alarm timers from httpd
[1783]245#endif
[1018]246
[4132]247 for (i = 1; i < argc; i++) {
248  k = argv[i];
249  if ( !strcmp(k, "--inetd") ) {
[1785]250#ifdef _CAN_SET_ENV
[1632]251   if ( (dir = parse_http(&gopher)) == -1 )
[1025]252    return 1;
[1785]253#else
254   return 1;
255#endif
[4132]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") ) {
[4883]261   rate = roar_str2rate(argv[++i]);
[4132]262  } else if ( !strcmp(k, "--bits") || !strcmp(k, "-B") ) {
[4883]263   bits = roar_str2bits(argv[++i]);
[4132]264  } else if ( !strcmp(k, "--channels") || !strcmp(k, "--chans") || !strcmp(k, "-C") ) {
[4883]265   channels = roar_str2channels(argv[++i]);
[4132]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 {
[4133]272   ROAR_ERR("Unknown parameter: %s", k);
[4132]273   usage();
274   return 1;
275  }
276 }
[1025]277
[3062]278 c = getenv("QUERY_STRING");
279 if ( c == NULL )
280  c = "";
281
[1797]282#ifdef ROAR_HAVE_STRTOK_R
[3062]283 c = strtok_r(c, "&", &sp0);
[1797]284#else
[3062]285 c = strtok(c, "&");
[1797]286#endif
[1018]287
288 while (c != NULL) {
[1797]289#ifdef ROAR_HAVE_STRTOK_R
[1018]290  k = strtok_r(c,    "=", &sp1);
291  v = strtok_r(NULL, "=", &sp1);
[1797]292#else
293  k = c;
294  if ( (v = strstr(c, "=")) != NULL ) {
295   *v = 0;
296   v++;
297  }
298#endif
[1018]299
300  if ( !strcmp(k, "codec") ) {
301   if ( (codec = roar_str2codec(v)) == -1 )
302    return 1;
[1024]303  } else if ( !strcmp(k, "channels") ) {
[4883]304   channels = roar_str2channels(v);
[1024]305  } else if ( !strcmp(k, "rate") ) {
[4883]306   rate = roar_str2rate(v);
[1024]307  } else if ( !strcmp(k, "bits") ) {
[4883]308   bits = roar_str2bits(v);
[2813]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;
[1018]324  } else {
325   return 1;
326  }
327
[1797]328#ifdef ROAR_HAVE_STRTOK_R
[1018]329  c = strtok_r(NULL, "&", &sp0);
[1797]330#else
331  c = strtok(NULL, "&");
332#endif
[1018]333 }
334
[2813]335 if ( roar_simple_connect(&con, server, "roarmonhttp") == -1 ) {
336  return 10;
337 }
[1016]338
[2813]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 ) {
[4885]357  if ( roar_stream_set_flags2(&con, &s, sflags, ROAR_SET_FLAG) == -1 ) {
[2813]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
[4925]368 if ( (vio = roar_get_connection_vio2(&con)) == NULL )
[1016]369  return 1;
370
[1632]371 if ( !gopher )
372  print_header(codec, rate, channels);
[1017]373
374/*
[1016]375 while((i = read(fh, buf, BUFSIZE)))
376  if (write(out, buf, i) != i)
377   break;
[1017]378*/
379
[1130]380 switch (dir) {
381  case ROAR_DIR_PLAY:
[4925]382    stream(vio, roar_stdin);
[1130]383   break;
384  case ROAR_DIR_MONITOR:
[2813]385  case ROAR_DIR_THRU:
[4925]386    stream(roar_stdout, vio);
[1130]387   break;
388 }
[1016]389
[4925]390 roar_vio_close(vio);
[1016]391
392 return 0;
393}
394
395//ll
Note: See TracBrowser for help on using the repository browser.