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 | void usage (void) { |
---|
28 | printf("roarradio [OPTIONS]... [FILE|URL]\n"); |
---|
29 | |
---|
30 | printf("\nOptions:\n\n"); |
---|
31 | |
---|
32 | printf(" --server SERVER - Set server hostname\n" |
---|
33 | " --rate RATE - Set sample rate\n" |
---|
34 | " --bits BITS - Set bits per sample\n" |
---|
35 | " --chans CHANNELS - Set number of channels\n" |
---|
36 | " --codec CODEC - Set the codec\n" |
---|
37 | " --help - Show this help\n" |
---|
38 | ); |
---|
39 | |
---|
40 | } |
---|
41 | |
---|
42 | int main (int argc, char * argv[]) { |
---|
43 | int rate = 44100; |
---|
44 | int bits = 16; |
---|
45 | int channels = 2; |
---|
46 | int codec = ROAR_CODEC_OGG_VORBIS; |
---|
47 | char * server = NULL; |
---|
48 | char * k; |
---|
49 | int i; |
---|
50 | int in = -1; |
---|
51 | char * file = NULL; |
---|
52 | char * host; |
---|
53 | int port = 80; |
---|
54 | FILE * http; |
---|
55 | struct roar_connection con[1]; |
---|
56 | struct roar_stream stream[1]; |
---|
57 | char buf0[80], buf1[80]; |
---|
58 | |
---|
59 | for (i = 1; i < argc; i++) { |
---|
60 | k = argv[i]; |
---|
61 | |
---|
62 | if ( strcmp(k, "--server") == 0 ) { |
---|
63 | server = argv[++i]; |
---|
64 | } else if ( strcmp(k, "--rate") == 0 ) { |
---|
65 | rate = atoi(argv[++i]); |
---|
66 | } else if ( strcmp(k, "--bits") == 0 ) { |
---|
67 | bits = atoi(argv[++i]); |
---|
68 | } else if ( strcmp(k, "--channels") == 0 || strcmp(k, "--chans") == 0 ) { |
---|
69 | channels = atoi(argv[++i]); |
---|
70 | } else if ( strcmp(k, "--codec") == 0 ) { |
---|
71 | codec = roar_str2codec(argv[++i]); |
---|
72 | } else if ( strcmp(k, "--help") == 0 ) { |
---|
73 | usage(); |
---|
74 | return 0; |
---|
75 | } else if ( file == NULL ) { |
---|
76 | file = argv[i]; |
---|
77 | } else { |
---|
78 | fprintf(stderr, "Error: unknown argument: %s\n", k); |
---|
79 | usage(); |
---|
80 | return 1; |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | if ( file == NULL ) { |
---|
85 | file = "/dev/stdin"; |
---|
86 | in = ROAR_STDIN; |
---|
87 | } else { |
---|
88 | if ( strncmp(file, "http://", 7) != 0 ) { |
---|
89 | if ( (in = open(file, O_RDONLY, 0644)) == -1 ) { |
---|
90 | ROAR_ERR("can not open file: %s: %s", file, strerror(errno)); |
---|
91 | return 1; |
---|
92 | } |
---|
93 | } else { |
---|
94 | host = file+7; |
---|
95 | |
---|
96 | for (i = 0; host[i] != 0; i++) { |
---|
97 | if ( host[i] == ':' ) { |
---|
98 | port = atoi(host+i+1); // atoi() ignores the rest after '/' |
---|
99 | host[i] = 0; |
---|
100 | } else if ( host[i] == '/' ) { |
---|
101 | file = host+i; |
---|
102 | break; |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | if ( (in = roar_socket_connect(host, port)) == -1 ) { |
---|
107 | ROAR_ERR("can not connect to remote server %s (port %i): %s", host, port, strerror(errno)); |
---|
108 | return 0; |
---|
109 | } |
---|
110 | |
---|
111 | if ( (http = fdopen(in, "r+")) == NULL ) { |
---|
112 | ROAR_ERR("can not create FILE* object: %s", strerror(errno)); |
---|
113 | return 0; |
---|
114 | } |
---|
115 | |
---|
116 | fprintf(http, "GET %s HTTP/1.1\r\n", file); |
---|
117 | fprintf(http, "Host: %s\r\n", host); |
---|
118 | fprintf(http, "User-Agent: roarradio $Revision$\r\n"); |
---|
119 | fprintf(http, "Connection: close\r\n"); |
---|
120 | fprintf(http, "\r\n"); |
---|
121 | fflush(http); |
---|
122 | |
---|
123 | if ( fscanf(http, "%79s %i %79s\n", buf0, &port, buf1) != 3 ) { |
---|
124 | ROAR_ERR("HTTP protocoll error!, no initial HTTP/1.x-line!"); |
---|
125 | return 1; |
---|
126 | } |
---|
127 | if ( port != 200 ) { // 200 = HTTP OK |
---|
128 | ROAR_ERR("HTTP Error: %i - %s", port, buf1); |
---|
129 | return 1; |
---|
130 | } |
---|
131 | |
---|
132 | *buf0 = 0; |
---|
133 | |
---|
134 | while (*buf0 != '\r' && *buf0 != '\n') { |
---|
135 | fgets(buf0, 80, http); |
---|
136 | } |
---|
137 | |
---|
138 | fflush(http); |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | if ( in == -1 ) { |
---|
143 | ROAR_ERR("No open file, bad. This should not happen"); |
---|
144 | return 1; |
---|
145 | } |
---|
146 | |
---|
147 | if ( roar_simple_connect(con, server, "roarradio") == -1 ) { |
---|
148 | ROAR_ERR("Can not connect to server"); |
---|
149 | return 0; |
---|
150 | } |
---|
151 | |
---|
152 | if ( roar_stream_new(stream, rate, channels, bits, codec) == -1 ) { |
---|
153 | roar_disconnect(con); |
---|
154 | return -1; |
---|
155 | } |
---|
156 | |
---|
157 | if ( roar_stream_connect(con, stream, ROAR_DIR_PLAY) == -1 ) { |
---|
158 | roar_disconnect(con); |
---|
159 | return -1; |
---|
160 | } |
---|
161 | |
---|
162 | if ( roar_stream_passfh(con, stream, in) == -1 ) { |
---|
163 | roar_disconnect(con); |
---|
164 | return -1; |
---|
165 | } |
---|
166 | |
---|
167 | close(in); |
---|
168 | |
---|
169 | if ( roar_stream_attach_simple(con, stream, 0) == -1 ) { |
---|
170 | ROAR_ERR("Can not attach stream to server"); |
---|
171 | } |
---|
172 | |
---|
173 | roar_disconnect(con); |
---|
174 | |
---|
175 | return 0; |
---|
176 | } |
---|
177 | |
---|
178 | //ll |
---|