source: roaraudio/roarclients/roarvorbis.c @ 3787:81e536463f01

Last change on this file since 3787:81e536463f01 was 3787:81e536463f01, checked in by phi, 14 years ago

get some warnings away on win32

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