source: roaraudio/roarclients/roarvorbis.c @ 718:3808018a1b99

Last change on this file since 718:3808018a1b99 was 718:3808018a1b99, checked in by phi, 16 years ago

use buffer of correct length

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