source: roaraudio/roarclients/roarmonhttp.c @ 5249:26fb6a2e20fc

Last change on this file since 5249:26fb6a2e20fc was 5249:26fb6a2e20fc, checked in by phi, 12 years ago

fixed warnings

File size: 9.0 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 if ( roar_vio_nonblock(src,  ROAR_SOCKET_NONBLOCK) == -1 ) {
84  ROAR_WARN("stream(dest=%p, src=%p): Can not set source stream non-blocking: %s",
85            dest, src, roar_error2str(roar_error));
86 }
87 if ( roar_vio_nonblock(dest, ROAR_SOCKET_NONBLOCK) == -1 ) {
88  ROAR_WARN("stream(dest=%p, src=%p): Can not set destination stream non-blocking: %s",
89            dest, src, roar_error2str(roar_error));
90 }
91
92 ROAR_VIO_SELECT_SETVIO(&(vios[0]), src,  ROAR_VIO_SELECT_READ);
93 ROAR_VIO_SELECT_SETVIO(&(vios[1]), dest, ROAR_VIO_SELECT_WRITE);
94
95 while (alive) {
96  ret = roar_vio_select(vios, ring != NULL ? 2 : 1, NULL, NULL);
97  if ( ret == -1 ) {
98   alive = 0;
99  } else if ( ret == 0 ) {
100   // nothing happend.
101  } else {
102   if ( vios[0].eventsa & ROAR_VIO_SELECT_READ ) { // we can read!
103    if ( roar_buffer_new(&cur, BUFSIZE) == -1 )
104     return -1;
105
106    if ( roar_buffer_get_data(cur, &data) == -1 )
107     return -1;
108
109    len = roar_vio_read(src, data, BUFSIZE);
110
111    switch (len) {
112     case  0:
113     case -1:
114       roar_buffer_free(cur);
115       cur = NULL;
116
117       if ( ring != NULL )
118        roar_buffer_free(ring);
119
120       ring = NULL;
121
122       return -1;
123      break;
124    }
125
126    if ( cur != NULL ) {
127     if ( roar_buffer_set_len(cur, len) == -1 )
128      return -1;
129
130     if ( ring == NULL ) {
131      ring = cur;
132     } else {
133      if ( roar_buffer_add(ring, cur) == -1 ) {
134       ROAR_ERR("stream(dest=%p, src=%p): Can not append buffer to ring: %s",
135                dest, src, roar_error2str(roar_error));
136       roar_buffer_free(ring);
137       roar_buffer_free(cur);
138       return -1;
139      }
140     }
141    }
142   } else if ( (vios[1].eventsa & ROAR_VIO_SELECT_WRITE) && ring != NULL ) { // we can write!
143    if ( roar_buffer_get_data(ring, &data) == -1 )
144     return -1;
145
146    if ( roar_buffer_get_len(ring, &todo) == -1 )
147     return -1;
148
149    len = roar_vio_write(dest, data, todo);
150
151    if ( len < 1 ) {
152     if ( roar_error != ROAR_ERROR_AGAIN ) {
153      roar_buffer_free(ring);
154      return -1;
155     }
156    }
157
158    if ( (ssize_t)todo == len ) { // we wrote all of the pkg
159     if ( roar_buffer_next(&ring) == -1 )
160      return -1;
161    } else {
162     if ( roar_buffer_set_offset(ring, len) == -1 )
163      return -1;
164    }
165
166   }
167  }
168 }
169
170 return 0;
171}
172
173#ifdef _CAN_SET_ENV
174int parse_http (int * gopher) {
175 struct roar_keyval kv;
176 char buf[1024];
177 char * qs = buf, *str;
178 ssize_t len;
179 int dir = ROAR_DIR_MONITOR;
180
181 if ( (len = read(ROAR_STDIN, buf, 1023)) == -1 )
182  return -1;
183
184 buf[len] = 0;
185
186 if ( strncmp(buf, "GET /", 5) ) {
187  if ( strncmp(buf, "SOURCE /", 8) ) {
188   if ( buf[0] != '/' ) {
189    return -1;
190   } else {
191    *gopher = 1;
192   }
193  } else {
194   dir = ROAR_DIR_PLAY;
195   qs += 3;
196  }
197 }
198
199 if ( !*gopher ) {
200  qs += 5;
201
202  if ( (str = strstr(qs, " ")) == NULL )
203   return -1;
204
205  *str = 0;
206 } else {
207  if ( (str = strstr(qs, "\r")) != NULL )
208   *str = 0;
209  if ( (str = strstr(qs, "\n")) != NULL )
210   *str = 0;
211 }
212
213 for (; *qs != '?'; qs++)
214  if ( !*qs )
215   break;
216
217 if ( *qs == '?' )
218  qs++;
219
220 if ( !*gopher )
221  printf("HTTP/1.0 200 OK\r\n");
222// printf("QS: %s\r\n", qs);
223
224 fflush(stdout);
225
226 kv.key   = "QUERY_STRING";
227 kv.value = qs;
228
229 roar_env_set(&kv);
230
231 return dir;
232}
233#endif
234
235int main (int argc, char * argv[]) {
236 int    rate     = 44100;
237 int    bits     = 16;
238 int    channels = 2;
239 int    codec    = ROAR_CODEC_OGG_VORBIS;
240 int    rel_id   = -1;
241 int    sflags   = ROAR_FLAG_NONE;
242// int    codec    = ROAR_CODEC_DEFAULT;
243 char * server   = NULL;
244 int    i;
245 char * c, * k, * v;
246 char * sp0 = NULL, * sp1 = NULL;
247 int dir = ROAR_DIR_MONITOR;
248 int gopher = 0;
249 struct roar_connection    con;
250 struct roar_stream        s;
251 struct roar_vio_calls   * vio;
252
253#ifdef ROAR_HAVE_ALARM
254 alarm(0); // reset alarm timers from httpd
255#endif
256
257 for (i = 1; i < argc; i++) {
258  k = argv[i];
259  if ( !strcmp(k, "--inetd") ) {
260#ifdef _CAN_SET_ENV
261   if ( (dir = parse_http(&gopher)) == -1 )
262    return 1;
263#else
264   return 1;
265#endif
266  } else if ( !strcmp(k, "--server") ) {
267   roar_libroar_set_server(argv[++i]);
268  } else if ( !strcmp(k, "--codec") ) {
269   codec = roar_str2codec(argv[++i]);
270  } else if ( !strcmp(k, "--rate") || !strcmp(k, "-r") || !strcmp(k, "-R") ) {
271   rate = roar_str2rate(argv[++i]);
272  } else if ( !strcmp(k, "--bits") || !strcmp(k, "-B") ) {
273   bits = roar_str2bits(argv[++i]);
274  } else if ( !strcmp(k, "--channels") || !strcmp(k, "--chans") || !strcmp(k, "-C") ) {
275   channels = roar_str2channels(argv[++i]);
276  } else if ( !strcmp(k, "--rel-id") ) {
277   rel_id = atoi(argv[++i]);
278  } else if ( !strcmp(k, "--help") && !strcmp(k, "-h") ) {
279   usage();
280   return 0;
281  } else {
282   ROAR_ERR("Unknown parameter: %s", k);
283   usage();
284   return 1;
285  }
286 }
287
288 c = getenv("QUERY_STRING");
289 if ( c == NULL )
290  c = "";
291
292 c = roar_mm_strtok_r(c, "&", &sp0);
293
294 while (c != NULL) {
295  k = roar_mm_strtok_r(c,    "=", &sp1);
296  v = roar_mm_strtok_r(NULL, "=", &sp1);
297
298  if ( !strcmp(k, "codec") ) {
299   if ( (codec = roar_str2codec(v)) == -1 )
300    return 1;
301  } else if ( !strcmp(k, "channels") ) {
302   channels = roar_str2channels(v);
303  } else if ( !strcmp(k, "rate") ) {
304   rate = roar_str2rate(v);
305  } else if ( !strcmp(k, "bits") ) {
306   bits = roar_str2bits(v);
307  } else if ( !strcmp(k, "rel-id") || !strcmp(k, "relid") ) {
308   rel_id = atoi(v);
309  } else if ( !strcmp(k, "set-flag") ) {
310   if ( !strcmp(v, "meta") ) {
311    sflags |= ROAR_FLAG_META;
312   } else if ( !strcmp(v, "cleanmeta") ) {
313    sflags |= ROAR_FLAG_CLEANMETA;
314   } else if ( !strcmp(v, "prethru") ) {
315    sflags |= ROAR_FLAG_PRETHRU;
316   } else {
317    return 1;
318   }
319  } else if ( !strcmp(k, "dir") ) {
320   if ( (dir = roar_str2dir(v)) == -1 )
321    return 1;
322  } else {
323   return 1;
324  }
325
326  c = roar_mm_strtok_r(NULL, "&", &sp0);
327 }
328
329 if ( roar_simple_connect(&con, server, "roarmonhttp") == -1 ) {
330  return 10;
331 }
332
333 if ( roar_stream_new(&s, rate, channels, bits, codec) == -1 ) {
334  roar_disconnect(&con);
335  return 20;
336 }
337
338 if ( rel_id != -1 ) {
339  if ( roar_stream_set_rel_id(&s, rel_id) ) {
340   roar_disconnect(&con);
341   return 21;
342  }
343 }
344
345 if ( roar_stream_connect(&con, &s, dir, -1) == -1 ) {
346  roar_disconnect(&con);
347  return 11;
348 }
349
350 if ( sflags != ROAR_FLAG_NONE ) {
351  if ( roar_stream_set_flags(&con, &s, sflags, ROAR_SET_FLAG) == -1 ) {
352   roar_disconnect(&con);
353   return 14;
354  }
355 }
356
357 if ( roar_stream_exec(&con, &s) == -1 ) {
358  roar_disconnect(&con);
359  return 12;
360 }
361
362 if ( (vio = roar_get_connection_vio2(&con)) == NULL )
363  return 1;
364
365 if ( !gopher )
366  print_header(codec, rate, channels);
367
368/*
369 while((i = read(fh, buf, BUFSIZE)))
370  if (write(out, buf, i) != i)
371   break;
372*/
373
374 switch (dir) {
375  case ROAR_DIR_PLAY:
376    stream(vio, roar_stdin);
377   break;
378  case ROAR_DIR_MONITOR:
379  case ROAR_DIR_THRU:
380    stream(roar_stdout, vio);
381   break;
382 }
383
384 roar_vio_close(vio);
385
386 return 0;
387}
388
389//ll
Note: See TracBrowser for help on using the repository browser.