source: roaraudio/roarclients/roarmonhttp.c @ 5109:4f9fc788fe91

Last change on this file since 5109:4f9fc788fe91 was 5109:4f9fc788fe91, checked in by phi, 13 years ago

Started to use compiler attributes (Also see: #130)

File size: 8.5 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 char * sp0 = NULL, * sp1 = NULL;
235 int dir = ROAR_DIR_MONITOR;
236 int gopher = 0;
237 struct roar_connection    con;
238 struct roar_stream        s;
239 struct roar_vio_calls   * vio;
240
241#ifdef ROAR_HAVE_ALARM
242 alarm(0); // reset alarm timers from httpd
243#endif
244
245 for (i = 1; i < argc; i++) {
246  k = argv[i];
247  if ( !strcmp(k, "--inetd") ) {
248#ifdef _CAN_SET_ENV
249   if ( (dir = parse_http(&gopher)) == -1 )
250    return 1;
251#else
252   return 1;
253#endif
254  } else if ( !strcmp(k, "--server") ) {
255   roar_libroar_set_server(argv[++i]);
256  } else if ( !strcmp(k, "--codec") ) {
257   codec = roar_str2codec(argv[++i]);
258  } else if ( !strcmp(k, "--rate") || !strcmp(k, "-r") || !strcmp(k, "-R") ) {
259   rate = roar_str2rate(argv[++i]);
260  } else if ( !strcmp(k, "--bits") || !strcmp(k, "-B") ) {
261   bits = roar_str2bits(argv[++i]);
262  } else if ( !strcmp(k, "--channels") || !strcmp(k, "--chans") || !strcmp(k, "-C") ) {
263   channels = roar_str2channels(argv[++i]);
264  } else if ( !strcmp(k, "--rel-id") ) {
265   rel_id = atoi(argv[++i]);
266  } else if ( !strcmp(k, "--help") && !strcmp(k, "-h") ) {
267   usage();
268   return 0;
269  } else {
270   ROAR_ERR("Unknown parameter: %s", k);
271   usage();
272   return 1;
273  }
274 }
275
276 c = getenv("QUERY_STRING");
277 if ( c == NULL )
278  c = "";
279
280 c = roar_mm_strtok_r(c, "&", &sp0);
281
282 while (c != NULL) {
283  k = roar_mm_strtok_r(c,    "=", &sp1);
284  v = roar_mm_strtok_r(NULL, "=", &sp1);
285
286  if ( !strcmp(k, "codec") ) {
287   if ( (codec = roar_str2codec(v)) == -1 )
288    return 1;
289  } else if ( !strcmp(k, "channels") ) {
290   channels = roar_str2channels(v);
291  } else if ( !strcmp(k, "rate") ) {
292   rate = roar_str2rate(v);
293  } else if ( !strcmp(k, "bits") ) {
294   bits = roar_str2bits(v);
295  } else if ( !strcmp(k, "rel-id") || !strcmp(k, "relid") ) {
296   rel_id = atoi(v);
297  } else if ( !strcmp(k, "set-flag") ) {
298   if ( !strcmp(v, "meta") ) {
299    sflags |= ROAR_FLAG_META;
300   } else if ( !strcmp(v, "cleanmeta") ) {
301    sflags |= ROAR_FLAG_CLEANMETA;
302   } else if ( !strcmp(v, "prethru") ) {
303    sflags |= ROAR_FLAG_PRETHRU;
304   } else {
305    return 1;
306   }
307  } else if ( !strcmp(k, "dir") ) {
308   if ( (dir = roar_str2dir(v)) == -1 )
309    return 1;
310  } else {
311   return 1;
312  }
313
314  c = roar_mm_strtok_r(NULL, "&", &sp0);
315 }
316
317 if ( roar_simple_connect(&con, server, "roarmonhttp") == -1 ) {
318  return 10;
319 }
320
321 if ( roar_stream_new(&s, rate, channels, bits, codec) == -1 ) {
322  roar_disconnect(&con);
323  return 20;
324 }
325
326 if ( rel_id != -1 ) {
327  if ( roar_stream_set_rel_id(&s, rel_id) ) {
328   roar_disconnect(&con);
329   return 21;
330  }
331 }
332
333 if ( roar_stream_connect2(&con, &s, dir, -1) == -1 ) {
334  roar_disconnect(&con);
335  return 11;
336 }
337
338 if ( sflags != ROAR_FLAG_NONE ) {
339  if ( roar_stream_set_flags2(&con, &s, sflags, ROAR_SET_FLAG) == -1 ) {
340   roar_disconnect(&con);
341   return 14;
342  }
343 }
344
345 if ( roar_stream_exec(&con, &s) == -1 ) {
346  roar_disconnect(&con);
347  return 12;
348 }
349
350 if ( (vio = roar_get_connection_vio2(&con)) == NULL )
351  return 1;
352
353 if ( !gopher )
354  print_header(codec, rate, channels);
355
356/*
357 while((i = read(fh, buf, BUFSIZE)))
358  if (write(out, buf, i) != i)
359   break;
360*/
361
362 switch (dir) {
363  case ROAR_DIR_PLAY:
364    stream(vio, roar_stdin);
365   break;
366  case ROAR_DIR_MONITOR:
367  case ROAR_DIR_THRU:
368    stream(roar_stdout, vio);
369   break;
370 }
371
372 roar_vio_close(vio);
373
374 return 0;
375}
376
377//ll
Note: See TracBrowser for help on using the repository browser.