source: roaraudio/roarclients/roarvorbis.c @ 5823:f9f70dbaa376

Last change on this file since 5823:f9f70dbaa376 was 5823:f9f70dbaa376, checked in by phi, 11 years ago

updated copyright

File size: 10.2 KB
Line 
1//roarvorbis.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2013
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
43void 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
57int _g_cf_vorbis_vfvio_return_err (void) {
58 return -1;
59}
60
61size_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, (unsigned long int)size, (unsigned long int)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, (unsigned long int)size, (unsigned long int)nmemb, datasource, r);
77 return r;
78}
79
80int cf_vorbis_vfvio_seek  (void *datasource, ogg_int64_t offset, int whence) {
81 return roar_vio_lseek(datasource, offset, whence);
82}
83
84long cf_vorbis_vfvio_tell (void *datasource) {
85 return roar_vio_lseek(datasource, 0, SEEK_CUR);
86}
87
88ov_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
96int update_stream (struct roar_connection *  con,
97                   roar_vs_t              ** vss,
98                   OggVorbis_File         *  vf,
99                   const 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:%liHz, channels:%li\n", (long int)vi->rate, (long int)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
230const 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
250void 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
282int 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 const char * file     = NULL;
291 const char * vcltfile = NULL;
292 const char * k;
293 int    i;
294 struct roar_connection con;
295 roar_vs_t * vss = NULL;
296 OggVorbis_File vf;
297 int eof=0;
298 int current_section = -1;
299 int last_section = -1;
300 struct roar_audio_info info;
301 char pcmout[4096];
302 double time_total;
303 ssize_t bits_per_sec = -1;
304 ssize_t bits_written = -1;
305
306
307 for (i = 1; i < argc; i++) {
308  k = argv[i];
309
310  if ( strcmp(k, "--server") == 0 ) {
311   roar_libroar_set_server(argv[++i]);
312  } else if ( strcmp(k, "--vclt-out") == 0 ) {
313   vcltfile = argv[++i];
314  } else if ( strcmp(k, "--help") == 0 ) {
315   usage();
316   return 0;
317  } else if ( file == NULL ) {
318   file = k;
319  } else {
320   fprintf(stderr, "Error: unknown argument: %s\n", k);
321   usage();
322   return 1;
323  }
324 }
325
326 if ( file == NULL ) {
327  ROAR_ERR("No filename given.");
328  return 1;
329 }
330
331 if ( roar_vio_dstr_init_defaults(&def, ROAR_VIO_DEF_TYPE_NONE, O_RDONLY, 0644) == -1 )
332  return 1;
333
334 if ( roar_vio_open_dstr(&in, file, &def, 1) == -1 ) {
335  fprintf(stderr, "Error: can not open file: %s: %s\n", file, strerror(errno));
336  return 1;
337 }
338
339 if ( roar_simple_connect(&con, NULL, "roarvorbis") == -1 ) {
340  ROAR_DBG("roar_simple_play(*): roar_simple_connect() faild!");
341  roar_vio_close(&in);
342  return 1;
343 }
344
345 if ( ov_open_callbacks((void*)&in, &vf, NULL, 0, _g_cf_vorbis_vfvio) < 0 ) {
346// if( ov_open(in, &vf, NULL, 0) < 0 ) {
347  fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n");
348  roar_disconnect(&con);
349  roar_vio_close(&in);
350  return 1;
351 }
352
353 time_total = ov_time_total(&vf, -1);
354
355 if ( vcltfile != NULL ) {
356  if ( roar_vio_dstr_init_defaults(&def, ROAR_VIO_DEF_TYPE_NONE, O_WRONLY|O_CREAT|O_APPEND, 0644) == -1 )
357   return 1;
358  if ( roar_vio_open_dstr(&vclt, vcltfile, &def, 1) == -1 ) {
359   fprintf(stderr, "Error: can not open file: %s: %s\n", vcltfile, strerror(errno));
360   roar_disconnect(&con);
361   roar_vio_close(&in);
362   return 1;
363  }
364 }
365
366// if ( update_stream(&con, &s, &out, &vf, file) == -1 )
367//  return 1;
368
369 while (!eof) {
370  long ret = ov_read(&vf, pcmout, sizeof(pcmout), 0, 2, 1, &current_section);
371
372  if ( last_section != current_section )
373   if ( update_stream(&con, &vss, &vf, file, &info, vcltfile == NULL ? NULL : &vclt) == -1 ) {
374    roar_vio_close(&in);
375    if ( vcltfile != NULL )
376     roar_vio_close(&vclt);
377    return 1;
378   }
379   bits_per_sec = roar_info2bitspersec(&info);
380
381  last_section = current_section;
382
383  if (ret == 0) {
384   /* EOF */
385   eof = 1;
386  } else if (ret < 0) {
387   /* error in the stream.  Not a problem, just reporting it in
388      case we (the app) cares.  In this case, we don't. */
389  } else {
390   if ( roar_vs_write(vss, pcmout, ret, NULL) != (ssize_t)ret ) {
391    fprintf(stderr, "\nError: Can not write to server.\n");
392    eof = 1;
393    continue;
394   }
395   bits_written += ret * 8;
396   if ( bits_written > bits_per_sec ) {
397    bits_written = 0;
398    print_time(&vf, vss, time_total, &info);
399   }
400  }
401 }
402
403 fprintf(stderr, "\n"); // end the lion of print_time().
404
405 ov_clear(&vf);
406
407// fclose(in);
408 roar_vs_close(vss, ROAR_VS_FALSE, NULL);
409 roar_disconnect(&con);
410
411 if ( vcltfile != NULL )
412  roar_vio_close(&vclt);
413
414 roar_vio_close(&in);
415
416 return 0;
417#endif
418}
419
420//ll
Note: See TracBrowser for help on using the repository browser.