source: roaraudio/roarclients/roarvorbis.c @ 1510:3887b49c6d38

Last change on this file since 1510:3887b49c6d38 was 1510:3887b49c6d38, checked in by phi, 15 years ago

fixed bug: roarvorbis need to take a filename as argument

File size: 6.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, 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       );
50
51}
52
53
54#ifdef ROAR_HAVE_LIBVORBISFILE
55FILE * 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
67int 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
166int 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 if ( file == NULL ) {
205  ROAR_ERR("No filename given.");
206  return 1;
207 }
208
209 if ( roar_simple_connect(&con, server, "roarvorbis") == -1 ) {
210  ROAR_DBG("roar_simple_play(*): roar_simple_connect() faild!");
211  return 1;
212 }
213
214 if ( strncmp(file, "http:", 5) == 0 ) {
215  in = open_http(file);
216 } else {
217  in = fopen(file, "rb");
218 }
219
220 if ( in == NULL ) {
221  roar_disconnect(&con);
222  return 1;
223 }
224
225#ifdef _WIN32
226  _setmode(_fileno(in), _O_BINARY);
227#endif
228
229 if(ov_open(in, &vf, NULL, 0) < 0) {
230  fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n");
231  roar_disconnect(&con);
232  return 1;
233 }
234
235// if ( update_stream(&con, &s, &out, &vf, file) == -1 )
236//  return 1;
237
238 while (!eof) {
239  long ret = ov_read(&vf, pcmout, sizeof(pcmout), 0, 2, 1, &current_section);
240
241  if ( last_section != current_section )
242   if ( update_stream(&con, &s, &out, &vf, file, &info) == -1 )
243    return 1;
244
245  last_section = current_section;
246
247  if (ret == 0) {
248   /* EOF */
249   eof=1;
250  } else if (ret < 0) {
251   /* error in the stream.  Not a problem, just reporting it in
252      case we (the app) cares.  In this case, we don't. */
253  } else {
254     /* we don't bother dealing with sample rate changes, etc, but
255        you'll have to */
256//    write(out, pcmout, ret);
257   roar_stream_send_data(&con, &s, pcmout, ret);
258  }
259 }
260
261  ov_clear(&vf);
262
263// fclose(in);
264 close(out);
265 roar_disconnect(&con);
266
267 return 0;
268#endif
269}
270
271//ll
Note: See TracBrowser for help on using the repository browser.