1 | //roarvorbis.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 | #include <stdio.h> |
---|
28 | #include <stdlib.h> |
---|
29 | #include <math.h> |
---|
30 | #ifdef ROAR_HAVE_LIBVORBISFILE |
---|
31 | #include <vorbis/codec.h> |
---|
32 | #include <vorbis/vorbisfile.h> |
---|
33 | #endif |
---|
34 | |
---|
35 | #ifdef _WIN32 |
---|
36 | #include <io.h> |
---|
37 | #include <fcntl.h> |
---|
38 | #endif |
---|
39 | |
---|
40 | #define BUFSIZE 1024 |
---|
41 | |
---|
42 | void usage (void) { |
---|
43 | printf("roarvorbis [OPTIONS]... FILE\n"); |
---|
44 | |
---|
45 | printf("\nOptions:\n\n"); |
---|
46 | |
---|
47 | printf(" --server SERVER - Set server hostname\n" |
---|
48 | " --help - Show this help\n" |
---|
49 | ); |
---|
50 | |
---|
51 | } |
---|
52 | |
---|
53 | |
---|
54 | #ifdef ROAR_HAVE_LIBVORBISFILE |
---|
55 | FILE * open_http (char * file) { |
---|
56 | #ifdef ROAR_HAVE_BIN_WGET |
---|
57 | char cmd[1024]; |
---|
58 | |
---|
59 | snprintf(cmd, 1023, ROAR_HAVE_BIN_WGET " -qO - '%s'", file); |
---|
60 | |
---|
61 | return popen(cmd, "r"); |
---|
62 | #else |
---|
63 | return NULL; |
---|
64 | #endif |
---|
65 | } |
---|
66 | |
---|
67 | int update_stream (struct roar_connection * con, struct roar_stream * s, int * out, OggVorbis_File * vf, char * file, struct roar_audio_info * info) { |
---|
68 | vorbis_info *vi = ov_info(vf, -1); |
---|
69 | int bits = 16; |
---|
70 | int codec = ROAR_CODEC_DEFAULT; |
---|
71 | char **ptr = ov_comment(vf, -1)->user_comments; |
---|
72 | char key[ROAR_META_MAX_NAMELEN], value[LIBROAR_BUFFER_MSGDATA] = {0}; |
---|
73 | int j, h = 0; |
---|
74 | struct roar_meta meta; |
---|
75 | int need_new_stream = 0; |
---|
76 | int meta_ok; |
---|
77 | |
---|
78 | fprintf(stderr, "\n"); |
---|
79 | |
---|
80 | if ( *out == -1 ) { |
---|
81 | need_new_stream = 1; |
---|
82 | } else if ( info->rate != vi->rate || info->channels != vi->channels ) { |
---|
83 | need_new_stream = 1; |
---|
84 | } |
---|
85 | |
---|
86 | if ( need_new_stream ) { |
---|
87 | if ( *out != -1 ) |
---|
88 | close(*out); |
---|
89 | |
---|
90 | fprintf(stderr, "Audio: %i channel, %liHz\n\n", vi->channels, vi->rate); |
---|
91 | |
---|
92 | info->rate = vi->rate; |
---|
93 | info->channels = vi->channels; |
---|
94 | |
---|
95 | if ( (*out = roar_simple_new_stream_obj(con, s, vi->rate, vi->channels, bits, codec, ROAR_DIR_PLAY)) == -1 ) { |
---|
96 | roar_disconnect(con); |
---|
97 | return -1; |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | |
---|
102 | meta.value = value; |
---|
103 | meta.key[0] = 0; |
---|
104 | meta.type = ROAR_META_TYPE_NONE; |
---|
105 | |
---|
106 | roar_stream_meta_set(con, s, ROAR_META_MODE_CLEAR, &meta); |
---|
107 | |
---|
108 | if ( strncmp(file, "http:", 5) == 0 ) |
---|
109 | meta.type = ROAR_META_TYPE_FILEURL; |
---|
110 | else |
---|
111 | meta.type = ROAR_META_TYPE_FILENAME; |
---|
112 | |
---|
113 | |
---|
114 | strncpy(value, file, LIBROAR_BUFFER_MSGDATA-1); |
---|
115 | value[LIBROAR_BUFFER_MSGDATA-1] = 0; |
---|
116 | roar_stream_meta_set(con, s, ROAR_META_MODE_SET, &meta); |
---|
117 | |
---|
118 | while(*ptr){ |
---|
119 | meta_ok = 1; |
---|
120 | |
---|
121 | for (j = 0; (*ptr)[j] != 0 && (*ptr)[j] != '='; j++) { |
---|
122 | if ( j == ROAR_META_MAX_NAMELEN ) { |
---|
123 | ROAR_ERR("update_stream(*): invalid meta data: meta data key too long"); |
---|
124 | meta_ok = 0; |
---|
125 | j = 0; |
---|
126 | break; |
---|
127 | } |
---|
128 | key[j] = (*ptr)[j]; |
---|
129 | } |
---|
130 | key[j] = 0; |
---|
131 | |
---|
132 | if ( meta_ok ) { |
---|
133 | for (j++, h = 0; (*ptr)[j] != 0 && (*ptr)[j] != '='; j++) { |
---|
134 | if ( h == LIBROAR_BUFFER_MSGDATA ) { |
---|
135 | ROAR_ERR("update_stream(*): invalid meta data: meta data value for key '%s' too long", key); |
---|
136 | meta_ok = 0; |
---|
137 | h = 0; |
---|
138 | break; |
---|
139 | } |
---|
140 | value[h++] = (*ptr)[j]; |
---|
141 | } |
---|
142 | value[h] = 0; |
---|
143 | } |
---|
144 | |
---|
145 | if ( meta_ok ) { |
---|
146 | fprintf(stderr, "Meta %-16s: %s\n", key, value); |
---|
147 | |
---|
148 | meta.type = roar_meta_inttype(key); |
---|
149 | if ( meta.type != -1 ) |
---|
150 | roar_stream_meta_set(con, s, ROAR_META_MODE_SET, &meta); |
---|
151 | } |
---|
152 | |
---|
153 | ptr++; |
---|
154 | } |
---|
155 | |
---|
156 | *value = 0; |
---|
157 | meta.key[0] = 0; |
---|
158 | meta.type = ROAR_META_TYPE_NONE; |
---|
159 | roar_stream_meta_set(con, s, ROAR_META_MODE_FINALIZE, &meta); |
---|
160 | |
---|
161 | return 0; |
---|
162 | } |
---|
163 | |
---|
164 | #endif |
---|
165 | |
---|
166 | int main (int argc, char * argv[]) { |
---|
167 | #ifndef ROAR_HAVE_LIBVORBISFILE |
---|
168 | fprintf(stderr, "Error: no Vorbis support!\n"); |
---|
169 | return 1; |
---|
170 | #else |
---|
171 | char * server = NULL; |
---|
172 | char * file = NULL; |
---|
173 | char * k; |
---|
174 | int i; |
---|
175 | FILE * in; |
---|
176 | int out = -1; |
---|
177 | struct roar_connection con; |
---|
178 | struct roar_stream s; |
---|
179 | OggVorbis_File vf; |
---|
180 | int eof=0; |
---|
181 | int current_section = -1; |
---|
182 | int last_section = -1; |
---|
183 | struct roar_audio_info info; |
---|
184 | char pcmout[4096]; |
---|
185 | |
---|
186 | |
---|
187 | for (i = 1; i < argc; i++) { |
---|
188 | k = argv[i]; |
---|
189 | |
---|
190 | if ( strcmp(k, "--server") == 0 ) { |
---|
191 | server = argv[++i]; |
---|
192 | } else if ( strcmp(k, "--help") == 0 ) { |
---|
193 | usage(); |
---|
194 | return 0; |
---|
195 | } else if ( file == NULL ) { |
---|
196 | file = k; |
---|
197 | } else { |
---|
198 | fprintf(stderr, "Error: unknown argument: %s\n", k); |
---|
199 | usage(); |
---|
200 | return 1; |
---|
201 | } |
---|
202 | } |
---|
203 | |
---|
204 | roar_libroar_set_server(server); |
---|
205 | |
---|
206 | if ( file == NULL ) { |
---|
207 | ROAR_ERR("No filename given."); |
---|
208 | return 1; |
---|
209 | } |
---|
210 | |
---|
211 | if ( roar_simple_connect(&con, server, "roarvorbis") == -1 ) { |
---|
212 | ROAR_DBG("roar_simple_play(*): roar_simple_connect() faild!"); |
---|
213 | return 1; |
---|
214 | } |
---|
215 | |
---|
216 | if ( strncmp(file, "http:", 5) == 0 ) { |
---|
217 | in = open_http(file); |
---|
218 | } else { |
---|
219 | in = fopen(file, "rb"); |
---|
220 | } |
---|
221 | |
---|
222 | if ( in == NULL ) { |
---|
223 | roar_disconnect(&con); |
---|
224 | return 1; |
---|
225 | } |
---|
226 | |
---|
227 | #ifdef _WIN32 |
---|
228 | _setmode(_fileno(in), _O_BINARY); |
---|
229 | #endif |
---|
230 | |
---|
231 | if(ov_open(in, &vf, NULL, 0) < 0) { |
---|
232 | fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n"); |
---|
233 | roar_disconnect(&con); |
---|
234 | return 1; |
---|
235 | } |
---|
236 | |
---|
237 | // if ( update_stream(&con, &s, &out, &vf, file) == -1 ) |
---|
238 | // return 1; |
---|
239 | |
---|
240 | while (!eof) { |
---|
241 | long ret = ov_read(&vf, pcmout, sizeof(pcmout), 0, 2, 1, ¤t_section); |
---|
242 | |
---|
243 | if ( last_section != current_section ) |
---|
244 | if ( update_stream(&con, &s, &out, &vf, file, &info) == -1 ) |
---|
245 | return 1; |
---|
246 | |
---|
247 | last_section = current_section; |
---|
248 | |
---|
249 | if (ret == 0) { |
---|
250 | /* EOF */ |
---|
251 | eof=1; |
---|
252 | } else if (ret < 0) { |
---|
253 | /* error in the stream. Not a problem, just reporting it in |
---|
254 | case we (the app) cares. In this case, we don't. */ |
---|
255 | } else { |
---|
256 | /* we don't bother dealing with sample rate changes, etc, but |
---|
257 | you'll have to */ |
---|
258 | // write(out, pcmout, ret); |
---|
259 | roar_stream_send_data(&con, &s, pcmout, ret); |
---|
260 | } |
---|
261 | } |
---|
262 | |
---|
263 | ov_clear(&vf); |
---|
264 | |
---|
265 | // fclose(in); |
---|
266 | close(out); |
---|
267 | roar_disconnect(&con); |
---|
268 | |
---|
269 | return 0; |
---|
270 | #endif |
---|
271 | } |
---|
272 | |
---|
273 | //ll |
---|