source: roaraudio/roarclients/roarradio.c @ 2114:c0e084678926

Last change on this file since 2114:c0e084678926 was 2114:c0e084678926, checked in by phi, 15 years ago

added gopher support *g*

File size: 5.4 KB
Line 
1//roarradio.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008
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, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 */
24
25#include <roaraudio.h>
26
27#define P_UNKNOWN 0
28#define P_HTTP    1
29#define P_GOPHER  2
30
31void usage (void) {
32 printf("roarradio [OPTIONS]... [FILE|URL]\n");
33
34 printf("\nOptions:\n\n");
35
36 printf("  --server SERVER    - Set server hostname\n"
37        "  --rate   RATE      - Set sample rate\n"
38        "  --bits   BITS      - Set bits per sample\n"
39        "  --chans  CHANNELS  - Set number of channels\n"
40        "  --codec  CODEC     - Set the codec\n"
41        "  --help             - Show this help\n"
42       );
43
44}
45
46int main (int argc, char * argv[]) {
47 int    rate     = 44100;
48 int    bits     = 16;
49 int    channels = 2;
50 int    codec    = ROAR_CODEC_OGG_VORBIS;
51 char * server   = NULL;
52 char * k;
53 int    i;
54 int    in = -1;
55 char * file = NULL;
56 char * host;
57 int    port;
58 FILE * http;
59 struct roar_connection con[1];
60 struct roar_stream     stream[1];
61 char buf0[80], buf1[80];
62 int proto = P_UNKNOWN;
63
64 for (i = 1; i < argc; i++) {
65  k = argv[i];
66
67  if ( strcmp(k, "--server") == 0 ) {
68   server = argv[++i];
69  } else if ( strcmp(k, "--rate") == 0 ) {
70   rate = atoi(argv[++i]);
71  } else if ( strcmp(k, "--bits") == 0 ) {
72   bits = atoi(argv[++i]);
73  } else if ( strcmp(k, "--channels") == 0 || strcmp(k, "--chans") == 0 ) {
74   channels = atoi(argv[++i]);
75  } else if ( strcmp(k, "--codec") == 0 ) {
76   codec = roar_str2codec(argv[++i]);
77  } else if ( strcmp(k, "--help") == 0 ) {
78   usage();
79   return 0;
80  } else if ( file == NULL ) {
81   file = argv[i];
82  } else {
83   fprintf(stderr, "Error: unknown argument: %s\n", k);
84   usage();
85   return 1;
86  }
87 }
88
89 if ( file == NULL ) {
90  file = "/dev/stdin";
91  in   = ROAR_STDIN;
92 } else {
93  if ( strncmp(file, "http://", 7) == 0 ) {
94   proto = P_HTTP;
95   host  = file+7;
96   port  = 80;
97  } else if ( strncmp(file, "gopher://", 9) == 0 ) {
98   proto = P_GOPHER;
99   host  = file+9;
100   port  = 70;
101  }
102
103  if ( proto == P_HTTP || proto == P_GOPHER ) {
104   for (i = 0; host[i] != 0; i++) {
105    if ( host[i] == ':' ) {
106     port    = atoi(host+i+1); // atoi() ignores the rest after '/'
107     host[i] = 0;
108    } else if ( host[i] == '/' ) {
109     file    = host+i;
110     break;
111    }
112   }
113
114   if ( !*file ) {
115    file = "/";
116
117    if ( (in = roar_socket_connect(host, port)) == -1 ) {
118     ROAR_ERR("can not connect to remote server %s (port %i): %s", host, port, strerror(errno));
119     return 0;
120    }
121   } else {
122    *file = 0;
123
124    if ( (in = roar_socket_connect(host, port)) == -1 ) {
125     ROAR_ERR("can not connect to remote server %s (port %i): %s", host, port, strerror(errno));
126     return 0;
127    }
128
129    *file = '/';
130   }
131
132   switch (proto) {
133    case P_HTTP:
134      if ( (http = fdopen(in, "r+")) == NULL ) {
135       ROAR_ERR("can not create FILE* object: %s", strerror(errno));
136       return 0;
137      }
138
139      fprintf(http, "GET %s HTTP/1.1\r\n", file);
140      fprintf(http, "Host: %s\r\n", host);
141      fprintf(http, "User-Agent: roarradio $Revision$\r\n");
142      fprintf(http, "Connection: close\r\n");
143      fprintf(http, "\r\n");
144      fflush(http);
145
146      if ( fscanf(http, "%79s %i %79s\n", buf0, &port, buf1) != 3 ) {
147       ROAR_ERR("HTTP protocoll error!, no initial HTTP/1.x-line!");
148       return 1;
149      }
150      if ( port != 200 ) { // 200 = HTTP OK
151       ROAR_ERR("HTTP Error: %i - %s", port, buf1);
152       return 1;
153      }
154
155      *buf0 = 0;
156
157      while (*buf0 != '\r' && *buf0 != '\n') {
158       fgets(buf0, 80, http);
159      }
160
161      fflush(http);
162     break;
163    case P_GOPHER:
164      if ( file[0] == 0 ) {
165       file[0] = '/';
166       file[1] = 0;
167      } else if ( file[0] == '/' && file[1] != 0 && file[2] == '/' ) { // strip the type prefix
168       file += 2;
169      }
170      // TODO: do some error checks here
171      write(in, file, strlen(file));
172      write(in, "\r\n", 2);
173      ROAR_SHUTDOWN(in, SHUT_WR);
174     break;
175   }
176  } else {
177   if ( (in = open(file, O_RDONLY, 0644)) == -1 ) {
178    ROAR_ERR("can not open file: %s: %s", file, strerror(errno));
179    return 1;
180   }
181  }
182 }
183
184 if ( in == -1 ) {
185  ROAR_ERR("No open file, bad. This should not happen");
186  return 1;
187 }
188
189 if ( roar_simple_connect(con, server, "roarradio") == -1 ) {
190  ROAR_ERR("Can not connect to server");
191  return 0;
192 }
193
194 if ( roar_stream_new(stream, rate, channels, bits, codec) == -1 ) {
195  roar_disconnect(con);
196  return -1;
197 }
198
199 if ( roar_stream_connect(con, stream, ROAR_DIR_PLAY) == -1 ) {
200  roar_disconnect(con);
201  return -1;
202 }
203
204 if ( roar_stream_passfh(con, stream, in) == -1 ) {
205  roar_disconnect(con);
206  return -1;
207 }
208
209 close(in);
210
211 if ( roar_stream_attach_simple(con, stream, 0) == -1 ) {
212  ROAR_ERR("Can not attach stream to server");
213 }
214
215 roar_disconnect(con);
216
217 return 0;
218}
219
220//ll
Note: See TracBrowser for help on using the repository browser.