source: roaraudio/roarclients/roarmonhttp.c @ 4885:01d694a7fdf7

Last change on this file since 4885:01d694a7fdf7 was 4885:01d694a7fdf7, checked in by phi, 13 years ago

Some cleanup of roarclients to fix ckport warnings

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