1 | //roarvorbis.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2012 |
---|
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, 51 Franklin Street, Fifth Floor, |
---|
22 | * Boston, MA 02110-1301, USA. |
---|
23 | * |
---|
24 | */ |
---|
25 | |
---|
26 | #include <roaraudio.h> |
---|
27 | |
---|
28 | #include <stdio.h> |
---|
29 | #include <stdlib.h> |
---|
30 | #include <math.h> |
---|
31 | #ifdef ROAR_HAVE_LIBVORBISFILE |
---|
32 | #include <vorbis/codec.h> |
---|
33 | #include <vorbis/vorbisfile.h> |
---|
34 | #endif |
---|
35 | |
---|
36 | #ifdef _WIN32 |
---|
37 | #include <io.h> |
---|
38 | #include <fcntl.h> |
---|
39 | #endif |
---|
40 | |
---|
41 | #define BUFSIZE 1024 |
---|
42 | |
---|
43 | void usage (void) { |
---|
44 | printf("roarvorbis [OPTIONS]... FILE\n"); |
---|
45 | |
---|
46 | printf("\nOptions:\n\n"); |
---|
47 | |
---|
48 | printf(" --server SERVER - Set server hostname\n" |
---|
49 | " --help - Show this help\n" |
---|
50 | " --vclt-out FILE - Writes VCLT file\n" |
---|
51 | ); |
---|
52 | |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
56 | #ifdef ROAR_HAVE_LIBVORBISFILE |
---|
57 | int _g_cf_vorbis_vfvio_return_err (void) { |
---|
58 | return -1; |
---|
59 | } |
---|
60 | |
---|
61 | size_t cf_vorbis_vfvio_read (void *ptr, size_t size, size_t nmemb, void *datasource) { |
---|
62 | ssize_t r; |
---|
63 | |
---|
64 | r = roar_vio_read(datasource, ptr, size*nmemb); |
---|
65 | |
---|
66 | ROAR_DBG("cf_vorbis_vfvio_read(ptr=%p, size=%lu, nmemb=%lu, datasource=%p): r=%i", ptr, size, nmemb, datasource, r); |
---|
67 | |
---|
68 | if ( r == -1 ) |
---|
69 | return 0; |
---|
70 | |
---|
71 | if ( r > 0 ) |
---|
72 | errno = 0; |
---|
73 | |
---|
74 | r /= size; |
---|
75 | |
---|
76 | ROAR_DBG("cf_vorbis_vfvio_read(ptr=%p, size=%lu, nmemb=%lu, datasource=%p) = %i", ptr, size, nmemb, datasource, r); |
---|
77 | return r; |
---|
78 | } |
---|
79 | |
---|
80 | int cf_vorbis_vfvio_seek (void *datasource, ogg_int64_t offset, int whence) { |
---|
81 | return roar_vio_lseek(datasource, offset, whence); |
---|
82 | } |
---|
83 | |
---|
84 | long cf_vorbis_vfvio_tell (void *datasource) { |
---|
85 | return roar_vio_lseek(datasource, 0, SEEK_CUR); |
---|
86 | } |
---|
87 | |
---|
88 | ov_callbacks _g_cf_vorbis_vfvio = { |
---|
89 | .read_func = cf_vorbis_vfvio_read, |
---|
90 | .seek_func = cf_vorbis_vfvio_seek, |
---|
91 | .close_func = (int (*)(void * )) _g_cf_vorbis_vfvio_return_err, |
---|
92 | .tell_func = cf_vorbis_vfvio_tell |
---|
93 | }; |
---|
94 | |
---|
95 | |
---|
96 | int update_stream (struct roar_connection * con, |
---|
97 | roar_vs_t ** vss, |
---|
98 | OggVorbis_File * vf, |
---|
99 | char * file, |
---|
100 | struct roar_audio_info * info, |
---|
101 | struct roar_vio_calls * vclt) { |
---|
102 | struct roar_stream * s; |
---|
103 | vorbis_info *vi = ov_info(vf, -1); |
---|
104 | int bits = 16; |
---|
105 | int codec = ROAR_CODEC_PCM_S_LE; |
---|
106 | char **ptr = ov_comment(vf, -1)->user_comments; |
---|
107 | char key[ROAR_META_MAX_NAMELEN], value[LIBROAR_BUFFER_MSGDATA] = {0}; |
---|
108 | int j, h = 0; |
---|
109 | struct roar_meta meta; |
---|
110 | static int need_new_stream = 1; |
---|
111 | int need_close = 0; |
---|
112 | int meta_ok; |
---|
113 | |
---|
114 | fprintf(stderr, "\n"); |
---|
115 | |
---|
116 | if ( vclt != NULL ) { |
---|
117 | roar_vio_printf(vclt, "AUDIOINFO=rate:%iHz, channels:%i\n", vi->rate, vi->channels); |
---|
118 | } |
---|
119 | |
---|
120 | if ( !need_new_stream ) { |
---|
121 | if ( info->rate != (uint16_t)vi->rate || info->channels != (uint16_t)vi->channels ) { |
---|
122 | need_close = 1; |
---|
123 | need_new_stream = 1; |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | if ( need_new_stream ) { |
---|
128 | if ( need_close ) { |
---|
129 | roar_vs_sync(*vss, ROAR_VS_WAIT, NULL); |
---|
130 | roar_vs_close(*vss, ROAR_VS_FALSE, NULL); |
---|
131 | } |
---|
132 | |
---|
133 | fprintf(stderr, "Audio: %i channel, %liHz\n\n", vi->channels, vi->rate); |
---|
134 | |
---|
135 | info->rate = vi->rate; |
---|
136 | info->channels = vi->channels; |
---|
137 | info->bits = bits; |
---|
138 | info->codec = codec; |
---|
139 | |
---|
140 | *vss = roar_vs_new_from_con(con, NULL); |
---|
141 | if ( *vss == NULL ) { |
---|
142 | roar_disconnect(con); |
---|
143 | return -1; |
---|
144 | } |
---|
145 | |
---|
146 | if ( roar_vs_stream(*vss, info, ROAR_DIR_PLAY, NULL) == -1 ) { |
---|
147 | roar_vs_close(*vss, ROAR_VS_TRUE, NULL); |
---|
148 | roar_disconnect(con); |
---|
149 | return -1; |
---|
150 | } |
---|
151 | |
---|
152 | need_new_stream = 0; |
---|
153 | } |
---|
154 | |
---|
155 | s = roar_vs_stream_obj(*vss, NULL); |
---|
156 | |
---|
157 | meta.value = value; |
---|
158 | meta.key[0] = 0; |
---|
159 | meta.type = ROAR_META_TYPE_NONE; |
---|
160 | |
---|
161 | roar_stream_meta_set(con, s, ROAR_META_MODE_CLEAR, &meta); |
---|
162 | |
---|
163 | if ( strncmp(file, "http:", 5) == 0 ) |
---|
164 | meta.type = ROAR_META_TYPE_FILEURL; |
---|
165 | else |
---|
166 | meta.type = ROAR_META_TYPE_FILENAME; |
---|
167 | |
---|
168 | |
---|
169 | strncpy(value, file, LIBROAR_BUFFER_MSGDATA-1); |
---|
170 | value[LIBROAR_BUFFER_MSGDATA-1] = 0; |
---|
171 | roar_stream_meta_set(con, s, ROAR_META_MODE_SET, &meta); |
---|
172 | |
---|
173 | while(*ptr){ |
---|
174 | meta_ok = 1; |
---|
175 | |
---|
176 | for (j = 0; (*ptr)[j] != 0 && (*ptr)[j] != '='; j++) { |
---|
177 | if ( j == ROAR_META_MAX_NAMELEN ) { |
---|
178 | ROAR_ERR("update_stream(*): invalid meta data: meta data key too long"); |
---|
179 | meta_ok = 0; |
---|
180 | j = 0; |
---|
181 | break; |
---|
182 | } |
---|
183 | key[j] = (*ptr)[j]; |
---|
184 | } |
---|
185 | key[j] = 0; |
---|
186 | |
---|
187 | if ( meta_ok ) { |
---|
188 | for (j++, h = 0; (*ptr)[j] != 0 && (*ptr)[j] != '='; j++) { |
---|
189 | if ( h == LIBROAR_BUFFER_MSGDATA ) { |
---|
190 | ROAR_ERR("update_stream(*): invalid meta data: meta data value for key '%s' too long", key); |
---|
191 | meta_ok = 0; |
---|
192 | h = 0; |
---|
193 | break; |
---|
194 | } |
---|
195 | value[h++] = (*ptr)[j]; |
---|
196 | } |
---|
197 | value[h] = 0; |
---|
198 | } |
---|
199 | |
---|
200 | if ( meta_ok ) { |
---|
201 | fprintf(stderr, "Meta %-16s: %s\n", key, value); |
---|
202 | |
---|
203 | if ( vclt != NULL ) { |
---|
204 | roar_vio_printf(vclt, "%s=%s\n", key, value); |
---|
205 | } |
---|
206 | |
---|
207 | meta.type = roar_meta_inttype(key); |
---|
208 | if ( meta.type != -1 ) |
---|
209 | roar_stream_meta_set(con, s, ROAR_META_MODE_SET, &meta); |
---|
210 | } |
---|
211 | |
---|
212 | ptr++; |
---|
213 | } |
---|
214 | |
---|
215 | fprintf(stderr, "\n"); |
---|
216 | |
---|
217 | *value = 0; |
---|
218 | meta.key[0] = 0; |
---|
219 | meta.type = ROAR_META_TYPE_NONE; |
---|
220 | roar_stream_meta_set(con, s, ROAR_META_MODE_FINALIZE, &meta); |
---|
221 | |
---|
222 | if ( vclt != NULL ) { |
---|
223 | roar_vio_printf(vclt, "==\n"); |
---|
224 | } |
---|
225 | |
---|
226 | return 0; |
---|
227 | } |
---|
228 | |
---|
229 | |
---|
230 | const char * time2str(double t, char * buf, size_t len) { |
---|
231 | int h, m; |
---|
232 | |
---|
233 | if ( t < 0 ) { |
---|
234 | // strncpy(buf, "unknown", len); |
---|
235 | // return buf; |
---|
236 | *buf++ = '-'; |
---|
237 | t *= -1; |
---|
238 | } |
---|
239 | |
---|
240 | h = t / 3600; |
---|
241 | t -= h * 3600; |
---|
242 | m = t / 60; |
---|
243 | t -= m * 60; |
---|
244 | |
---|
245 | snprintf(buf, len, "%.2i:%.2i:%.2i", h, m, (int)t); |
---|
246 | |
---|
247 | return buf; |
---|
248 | } |
---|
249 | |
---|
250 | void print_time (OggVorbis_File * vf, roar_vs_t * vss, double time_total, struct roar_audio_info * info) { |
---|
251 | ssize_t pos; |
---|
252 | double time_cur; |
---|
253 | long bitrate_cur = ov_bitrate_instant(vf); |
---|
254 | float bitrate = bitrate_cur / 1000.0; |
---|
255 | char time_buf[3][10]; |
---|
256 | |
---|
257 | pos = roar_vs_position(vss, ROAR_VS_BACKEND_DEFAULT, NULL); |
---|
258 | |
---|
259 | if ( pos == -1 ) { |
---|
260 | time_cur = ov_time_tell(vf); |
---|
261 | } else { |
---|
262 | time_cur = (double)pos/(double)(info->channels*info->rate); |
---|
263 | } |
---|
264 | |
---|
265 | time2str(time_cur, time_buf[0], sizeof(time_buf[0])); |
---|
266 | |
---|
267 | if ( time_total > 0 ) { |
---|
268 | time2str(time_total-time_cur, time_buf[1], sizeof(time_buf[1])); |
---|
269 | time2str(time_total, time_buf[2], sizeof(time_buf[2])); |
---|
270 | |
---|
271 | //Time: 00:02.53 [03:26.20] of 03:28.73 (122.7 kbps) Output Buffer 43.8% |
---|
272 | fprintf(stderr, "\rTime: %s [%s] of %s (%.1f kbps) ", time_buf[0], time_buf[1], time_buf[2], bitrate); |
---|
273 | } else { |
---|
274 | fprintf(stderr, "\rTime: %s (%.1f kbps) ", time_buf[0], bitrate); |
---|
275 | } |
---|
276 | |
---|
277 | fflush(stderr); |
---|
278 | } |
---|
279 | |
---|
280 | #endif |
---|
281 | |
---|
282 | int main (int argc, char * argv[]) { |
---|
283 | #ifndef ROAR_HAVE_LIBVORBISFILE |
---|
284 | (void)argc, (void)argv; |
---|
285 | fprintf(stderr, "Error: no Vorbis support!\n"); |
---|
286 | return 1; |
---|
287 | #else |
---|
288 | struct roar_vio_calls vclt, in; |
---|
289 | struct roar_vio_defaults def; |
---|
290 | char * server = NULL; |
---|
291 | char * file = NULL; |
---|
292 | char * vcltfile = NULL; |
---|
293 | char * k; |
---|
294 | int i; |
---|
295 | struct roar_connection con; |
---|
296 | roar_vs_t * vss = NULL; |
---|
297 | OggVorbis_File vf; |
---|
298 | int eof=0; |
---|
299 | int current_section = -1; |
---|
300 | int last_section = -1; |
---|
301 | struct roar_audio_info info; |
---|
302 | char pcmout[4096]; |
---|
303 | double time_total; |
---|
304 | ssize_t bits_per_sec = -1; |
---|
305 | ssize_t bits_written = -1; |
---|
306 | |
---|
307 | |
---|
308 | for (i = 1; i < argc; i++) { |
---|
309 | k = argv[i]; |
---|
310 | |
---|
311 | if ( strcmp(k, "--server") == 0 ) { |
---|
312 | server = argv[++i]; |
---|
313 | } else if ( strcmp(k, "--vclt-out") == 0 ) { |
---|
314 | vcltfile = argv[++i]; |
---|
315 | } else if ( strcmp(k, "--help") == 0 ) { |
---|
316 | usage(); |
---|
317 | return 0; |
---|
318 | } else if ( file == NULL ) { |
---|
319 | file = k; |
---|
320 | } else { |
---|
321 | fprintf(stderr, "Error: unknown argument: %s\n", k); |
---|
322 | usage(); |
---|
323 | return 1; |
---|
324 | } |
---|
325 | } |
---|
326 | |
---|
327 | roar_libroar_set_server(server); |
---|
328 | |
---|
329 | if ( file == NULL ) { |
---|
330 | ROAR_ERR("No filename given."); |
---|
331 | return 1; |
---|
332 | } |
---|
333 | |
---|
334 | if ( roar_vio_dstr_init_defaults(&def, ROAR_VIO_DEF_TYPE_NONE, O_RDONLY, 0644) == -1 ) |
---|
335 | return 1; |
---|
336 | |
---|
337 | if ( roar_vio_open_dstr(&in, file, &def, 1) == -1 ) { |
---|
338 | fprintf(stderr, "Error: can not open file: %s: %s\n", file, strerror(errno)); |
---|
339 | return 1; |
---|
340 | } |
---|
341 | |
---|
342 | if ( roar_simple_connect(&con, NULL, "roarvorbis") == -1 ) { |
---|
343 | ROAR_DBG("roar_simple_play(*): roar_simple_connect() faild!"); |
---|
344 | roar_vio_close(&in); |
---|
345 | return 1; |
---|
346 | } |
---|
347 | |
---|
348 | if ( ov_open_callbacks((void*)&in, &vf, NULL, 0, _g_cf_vorbis_vfvio) < 0 ) { |
---|
349 | // if( ov_open(in, &vf, NULL, 0) < 0 ) { |
---|
350 | fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n"); |
---|
351 | roar_disconnect(&con); |
---|
352 | roar_vio_close(&in); |
---|
353 | return 1; |
---|
354 | } |
---|
355 | |
---|
356 | time_total = ov_time_total(&vf, -1); |
---|
357 | |
---|
358 | if ( vcltfile != NULL ) { |
---|
359 | if ( roar_vio_dstr_init_defaults(&def, ROAR_VIO_DEF_TYPE_NONE, O_WRONLY|O_CREAT|O_APPEND, 0644) == -1 ) |
---|
360 | return 1; |
---|
361 | if ( roar_vio_open_dstr(&vclt, vcltfile, &def, 1) == -1 ) { |
---|
362 | fprintf(stderr, "Error: can not open file: %s: %s\n", vcltfile, strerror(errno)); |
---|
363 | roar_disconnect(&con); |
---|
364 | roar_vio_close(&in); |
---|
365 | return 1; |
---|
366 | } |
---|
367 | } |
---|
368 | |
---|
369 | // if ( update_stream(&con, &s, &out, &vf, file) == -1 ) |
---|
370 | // return 1; |
---|
371 | |
---|
372 | while (!eof) { |
---|
373 | long ret = ov_read(&vf, pcmout, sizeof(pcmout), 0, 2, 1, ¤t_section); |
---|
374 | |
---|
375 | if ( last_section != current_section ) |
---|
376 | if ( update_stream(&con, &vss, &vf, file, &info, vcltfile == NULL ? NULL : &vclt) == -1 ) { |
---|
377 | roar_vio_close(&in); |
---|
378 | if ( vcltfile != NULL ) |
---|
379 | roar_vio_close(&vclt); |
---|
380 | return 1; |
---|
381 | } |
---|
382 | bits_per_sec = roar_info2bitspersec(&info); |
---|
383 | |
---|
384 | last_section = current_section; |
---|
385 | |
---|
386 | if (ret == 0) { |
---|
387 | /* EOF */ |
---|
388 | eof = 1; |
---|
389 | } else if (ret < 0) { |
---|
390 | /* error in the stream. Not a problem, just reporting it in |
---|
391 | case we (the app) cares. In this case, we don't. */ |
---|
392 | } else { |
---|
393 | if ( roar_vs_write(vss, pcmout, ret, NULL) != (ssize_t)ret ) { |
---|
394 | fprintf(stderr, "\nError: Can not write to server.\n"); |
---|
395 | eof = 1; |
---|
396 | continue; |
---|
397 | } |
---|
398 | bits_written += ret * 8; |
---|
399 | if ( bits_written > bits_per_sec ) { |
---|
400 | bits_written = 0; |
---|
401 | print_time(&vf, vss, time_total, &info); |
---|
402 | } |
---|
403 | } |
---|
404 | } |
---|
405 | |
---|
406 | fprintf(stderr, "\n"); // end the lion of print_time(). |
---|
407 | |
---|
408 | ov_clear(&vf); |
---|
409 | |
---|
410 | // fclose(in); |
---|
411 | roar_vs_close(vss, ROAR_VS_FALSE, NULL); |
---|
412 | roar_disconnect(&con); |
---|
413 | |
---|
414 | if ( vcltfile != NULL ) |
---|
415 | roar_vio_close(&vclt); |
---|
416 | |
---|
417 | roar_vio_close(&in); |
---|
418 | |
---|
419 | return 0; |
---|
420 | #endif |
---|
421 | } |
---|
422 | |
---|
423 | //ll |
---|