source: roaraudio/roarclients/roarvorbis.c @ 3043:ddabf4ccc93e

Last change on this file since 3043:ddabf4ccc93e was 3043:ddabf4ccc93e, checked in by phi, 14 years ago

support for writing VCLT

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