source: roaraudio/roarclients/roarradio.c @ 3121:0ed0acfadd4f

Last change on this file since 3121:0ed0acfadd4f was 2225:0c8e399f1041, checked in by phi, 15 years ago

corrected return codes

File size: 5.5 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  } else {
102   fprintf(stderr, "Error: unknown protocol: %s\n", file);
103   return 20;
104  }
105
106  if ( proto == P_HTTP || proto == P_GOPHER ) {
107   for (i = 0; host[i] != 0; i++) {
108    if ( host[i] == ':' ) {
109     port    = atoi(host+i+1); // atoi() ignores the rest after '/'
110     host[i] = 0;
111    } else if ( host[i] == '/' ) {
112     file    = host+i;
113     break;
114    }
115   }
116
117   if ( !*file ) {
118    file = "/";
119
120    if ( (in = roar_socket_connect(host, port)) == -1 ) {
121     ROAR_ERR("can not connect to remote server %s (port %i): %s", host, port, strerror(errno));
122     return 0;
123    }
124   } else {
125    *file = 0;
126
127    if ( (in = roar_socket_connect(host, port)) == -1 ) {
128     ROAR_ERR("can not connect to remote server %s (port %i): %s", host, port, strerror(errno));
129     return 0;
130    }
131
132    *file = '/';
133   }
134
135   switch (proto) {
136    case P_HTTP:
137      if ( (http = fdopen(in, "r+")) == NULL ) {
138       ROAR_ERR("can not create FILE* object: %s", strerror(errno));
139       return 0;
140      }
141
142      fprintf(http, "GET %s HTTP/1.1\r\n", file);
143      fprintf(http, "Host: %s\r\n", host);
144      fprintf(http, "User-Agent: roarradio $Revision$\r\n");
145      fprintf(http, "Connection: close\r\n");
146      fprintf(http, "\r\n");
147      fflush(http);
148
149      if ( fscanf(http, "%79s %i %79s\n", buf0, &port, buf1) != 3 ) {
150       ROAR_ERR("HTTP protocoll error!, no initial HTTP/1.x-line!");
151       return 1;
152      }
153      if ( port != 200 ) { // 200 = HTTP OK
154       ROAR_ERR("HTTP Error: %i - %s", port, buf1);
155       return 1;
156      }
157
158      *buf0 = 0;
159
160      while (*buf0 != '\r' && *buf0 != '\n') {
161       fgets(buf0, 80, http);
162      }
163
164      fflush(http);
165     break;
166    case P_GOPHER:
167      if ( file[0] == 0 ) {
168       file[0] = '/';
169       file[1] = 0;
170      } else if ( file[0] == '/' && file[1] != 0 && file[2] == '/' ) { // strip the type prefix
171       file += 2;
172      }
173      // TODO: do some error checks here
174      write(in, file, strlen(file));
175      write(in, "\r\n", 2);
176      ROAR_SHUTDOWN(in, SHUT_WR);
177     break;
178   }
179  } else {
180   if ( (in = open(file, O_RDONLY, 0644)) == -1 ) {
181    ROAR_ERR("can not open file: %s: %s", file, strerror(errno));
182    return 1;
183   }
184  }
185 }
186
187 if ( in == -1 ) {
188  ROAR_ERR("No open file, bad. This should not happen");
189  return 1;
190 }
191
192 if ( roar_simple_connect(con, server, "roarradio") == -1 ) {
193  ROAR_ERR("Can not connect to server");
194  return 1;
195 }
196
197 if ( roar_stream_new(stream, rate, channels, bits, codec) == -1 ) {
198  roar_disconnect(con);
199  return 1;
200 }
201
202 if ( roar_stream_connect(con, stream, ROAR_DIR_PLAY) == -1 ) {
203  roar_disconnect(con);
204  return 1;
205 }
206
207 if ( roar_stream_passfh(con, stream, in) == -1 ) {
208  roar_disconnect(con);
209  return 1;
210 }
211
212 close(in);
213
214 if ( roar_stream_attach_simple(con, stream, 0) == -1 ) {
215  ROAR_ERR("Can not attach stream to server");
216 }
217
218 roar_disconnect(con);
219
220 return 0;
221}
222
223//ll
Note: See TracBrowser for help on using the repository browser.