source: roaraudio/roarclients/roarradio.c @ 5546:faff3e9677c4

Last change on this file since 5546:faff3e9677c4 was 5536:72763f8d8270, checked in by phi, 12 years ago

make use of new function roar_stream_new_by_info()

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