source: roaraudio/roarclients/roarmonhttp.c @ 4925:ea0657842b8f

Last change on this file since 4925:ea0657842b8f was 4925:ea0657842b8f, checked in by phi, 13 years ago

updated API from sysio to VIO usage

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#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 usage (void) {
35 printf("roarmonhttp [OPTIONS]...\n");
36
37 printf("\nOptions:\n\n");
38
39 printf("  --server    SERVER    - Set server hostname\n"
40        "  --rate  -R  RATE      - Set sample rate\n"
41        "  --bits  -B  BITS      - Set bits per sample\n"
42        "  --chans -C  CHANNELS  - Set number of channels\n"
43        "  --codec     CODEC     - Set the codec\n"
44        "  --rel-id ID           - Set ID of relative stream\n"
45        "  --inetd               - Start in inetd mode (STDIN and STDOUT connected to socket)\n"
46        "  --help                - Show this help\n"
47       );
48
49}
50
51void print_header (int codec, int rate, int channels) {
52 const char * mime;
53
54 mime = roar_codec2mime(codec);
55
56 if ( mime == NULL )
57  mime = "application/octet-stream";
58
59 printf("Content-type: %s\r\n", mime);
60 printf("ice-audio-info: ice-samplerate=%i;ice-channels=%i\r\n", rate, channels);
61 printf("icy-pub:0\r\n");
62 printf("Server: RoarAudio (roarmonhttp $Revision$)\r\n");
63 printf("\r\n");
64
65 fflush(stdout);
66}
67
68int stream (struct roar_vio_calls * dest, struct roar_vio_calls * src) {
69 struct roar_vio_select vios[2];
70 struct roar_buffer *ring = NULL, *cur;
71 ssize_t len;
72 size_t  todo;
73 int alive = 1;
74 void * data;
75 int ret;
76
77 roar_vio_nonblock(src,  ROAR_SOCKET_NONBLOCK);
78 roar_vio_nonblock(dest, ROAR_SOCKET_NONBLOCK);
79
80 ROAR_VIO_SELECT_SETVIO(&(vios[0]), src,  ROAR_VIO_SELECT_READ);
81 ROAR_VIO_SELECT_SETVIO(&(vios[1]), dest, ROAR_VIO_SELECT_WRITE);
82
83 while (alive) {
84  ret = roar_vio_select(vios, ring != NULL ? 2 : 1, NULL, NULL);
85  if ( ret == -1 ) {
86   alive = 0;
87  } else if ( ret == 0 ) {
88   // nothing happend.
89  } else {
90   if ( vios[0].eventsa & ROAR_VIO_SELECT_READ ) { // we can read!
91    if ( roar_buffer_new(&cur, BUFSIZE) == -1 )
92     return -1;
93
94    if ( roar_buffer_get_data(cur, &data) == -1 )
95     return -1;
96
97    len = roar_vio_read(src, data, BUFSIZE);
98
99    switch (len) {
100     case  0:
101     case -1:
102       roar_buffer_free(cur);
103       cur = NULL;
104
105       if ( ring != NULL )
106        roar_buffer_free(ring);
107
108       ring = NULL;
109
110       return -1;
111      break;
112    }
113
114    if ( cur != NULL ) {
115     if ( roar_buffer_set_len(cur, len) == -1 )
116      return -1;
117
118     if ( ring == NULL ) {
119      ring = cur;
120     } else {
121      roar_buffer_add(ring, cur);
122     }
123    }
124   } else if ( (vios[1].eventsa & ROAR_VIO_SELECT_WRITE) && ring != NULL ) { // we can write!
125    if ( roar_buffer_get_data(ring, &data) == -1 )
126     return -1;
127
128    if ( roar_buffer_get_len(ring, &todo) == -1 )
129     return -1;
130
131    len = roar_vio_write(dest, data, todo);
132
133    if ( len < 1 ) {
134     if ( roar_error != ROAR_ERROR_AGAIN ) {
135      roar_buffer_free(ring);
136      return -1;
137     }
138    }
139
140    if ( (ssize_t)todo == len ) { // we wrote all of the pkg
141     if ( roar_buffer_next(&ring) == -1 )
142      return -1;
143    } else {
144     if ( roar_buffer_set_offset(ring, len) == -1 )
145      return -1;
146    }
147
148   }
149  }
150 }
151
152 return 0;
153}
154
155#ifdef _CAN_SET_ENV
156int parse_http (int * gopher) {
157 struct roar_keyval kv;
158 char buf[1024];
159 char * qs = buf, *str;
160 ssize_t len;
161 int dir = ROAR_DIR_MONITOR;
162
163 if ( (len = read(ROAR_STDIN, buf, 1023)) == -1 )
164  return -1;
165
166 buf[len] = 0;
167
168 if ( strncmp(buf, "GET /", 5) ) {
169  if ( strncmp(buf, "SOURCE /", 8) ) {
170   if ( buf[0] != '/' ) {
171    return -1;
172   } else {
173    *gopher = 1;
174   }
175  } else {
176   dir = ROAR_DIR_PLAY;
177   qs += 3;
178  }
179 }
180
181 if ( !*gopher ) {
182  qs += 5;
183
184  if ( (str = strstr(qs, " ")) == NULL )
185   return -1;
186
187  *str = 0;
188 } else {
189  if ( (str = strstr(qs, "\r")) != NULL )
190   *str = 0;
191  if ( (str = strstr(qs, "\n")) != NULL )
192   *str = 0;
193 }
194
195 for (; *qs != '?'; qs++)
196  if ( !*qs )
197   break;
198
199 if ( *qs == '?' )
200  qs++;
201
202 if ( !*gopher )
203  printf("HTTP/1.0 200 OK\r\n");
204// printf("QS: %s\r\n", qs);
205
206 fflush(stdout);
207
208 kv.key   = "QUERY_STRING";
209 kv.value = qs;
210
211 roar_env_set(&kv);
212
213 return dir;
214}
215#endif
216
217int main (int argc, char * argv[]) {
218 int    rate     = 44100;
219 int    bits     = 16;
220 int    channels = 2;
221 int    codec    = ROAR_CODEC_OGG_VORBIS;
222 int    rel_id   = -1;
223 int    sflags   = ROAR_FLAG_NONE;
224// int    codec    = ROAR_CODEC_DEFAULT;
225 char * server   = NULL;
226 int    i;
227 char * c, * k, * v;
228#ifdef ROAR_HAVE_STRTOK_R
229 char * sp0 = NULL, * sp1 = NULL;
230#endif
231 int dir = ROAR_DIR_MONITOR;
232 int gopher = 0;
233 struct roar_connection    con;
234 struct roar_stream        s;
235 struct roar_vio_calls   * vio;
236
237#ifdef ROAR_HAVE_ALARM
238 alarm(0); // reset alarm timers from httpd
239#endif
240
241 for (i = 1; i < argc; i++) {
242  k = argv[i];
243  if ( !strcmp(k, "--inetd") ) {
244#ifdef _CAN_SET_ENV
245   if ( (dir = parse_http(&gopher)) == -1 )
246    return 1;
247#else
248   return 1;
249#endif
250  } else if ( !strcmp(k, "--server") ) {
251   roar_libroar_set_server(argv[++i]);
252  } else if ( !strcmp(k, "--codec") ) {
253   codec = roar_str2codec(argv[++i]);
254  } else if ( !strcmp(k, "--rate") || !strcmp(k, "-r") || !strcmp(k, "-R") ) {
255   rate = roar_str2rate(argv[++i]);
256  } else if ( !strcmp(k, "--bits") || !strcmp(k, "-B") ) {
257   bits = roar_str2bits(argv[++i]);
258  } else if ( !strcmp(k, "--channels") || !strcmp(k, "--chans") || !strcmp(k, "-C") ) {
259   channels = roar_str2channels(argv[++i]);
260  } else if ( !strcmp(k, "--rel-id") ) {
261   rel_id = atoi(argv[++i]);
262  } else if ( !strcmp(k, "--help") && !strcmp(k, "-h") ) {
263   usage();
264   return 0;
265  } else {
266   ROAR_ERR("Unknown parameter: %s", k);
267   usage();
268   return 1;
269  }
270 }
271
272 c = getenv("QUERY_STRING");
273 if ( c == NULL )
274  c = "";
275
276#ifdef ROAR_HAVE_STRTOK_R
277 c = strtok_r(c, "&", &sp0);
278#else
279 c = strtok(c, "&");
280#endif
281
282 while (c != NULL) {
283#ifdef ROAR_HAVE_STRTOK_R
284  k = strtok_r(c,    "=", &sp1);
285  v = strtok_r(NULL, "=", &sp1);
286#else
287  k = c;
288  if ( (v = strstr(c, "=")) != NULL ) {
289   *v = 0;
290   v++;
291  }
292#endif
293
294  if ( !strcmp(k, "codec") ) {
295   if ( (codec = roar_str2codec(v)) == -1 )
296    return 1;
297  } else if ( !strcmp(k, "channels") ) {
298   channels = roar_str2channels(v);
299  } else if ( !strcmp(k, "rate") ) {
300   rate = roar_str2rate(v);
301  } else if ( !strcmp(k, "bits") ) {
302   bits = roar_str2bits(v);
303  } else if ( !strcmp(k, "rel-id") || !strcmp(k, "relid") ) {
304   rel_id = atoi(v);
305  } else if ( !strcmp(k, "set-flag") ) {
306   if ( !strcmp(v, "meta") ) {
307    sflags |= ROAR_FLAG_META;
308   } else if ( !strcmp(v, "cleanmeta") ) {
309    sflags |= ROAR_FLAG_CLEANMETA;
310   } else if ( !strcmp(v, "prethru") ) {
311    sflags |= ROAR_FLAG_PRETHRU;
312   } else {
313    return 1;
314   }
315  } else if ( !strcmp(k, "dir") ) {
316   if ( (dir = roar_str2dir(v)) == -1 )
317    return 1;
318  } else {
319   return 1;
320  }
321
322#ifdef ROAR_HAVE_STRTOK_R
323  c = strtok_r(NULL, "&", &sp0);
324#else
325  c = strtok(NULL, "&");
326#endif
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 ) {
346  roar_disconnect(&con);
347  return 11;
348 }
349
350 if ( sflags != ROAR_FLAG_NONE ) {
351  if ( roar_stream_set_flags2(&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.