source: roaraudio/roarclients/roarvorbis.c @ 861:292a83484609

Last change on this file since 861:292a83484609 was 861:292a83484609, checked in by phi, 16 years ago

only support HTTP streams if we have wget

File size: 5.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       );
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 return 0;
157}
158
159#endif
160
161int main (int argc, char * argv[]) {
162#ifndef ROAR_HAVE_LIBVORBISFILE
163 fprintf(stderr, "Error: no Vorbis support!\n");
164 return 1;
165#else
166 char * server   = NULL;
167 char * file     = NULL;
168 char * k;
169 int    i;
170 FILE * in;
171 int    out = -1;
172 struct roar_connection con;
173 struct roar_stream     s;
174 OggVorbis_File vf;
175 int eof=0;
176 int current_section = -1;
177 int last_section = -1;
178 struct roar_audio_info info;
179 char pcmout[4096];
180
181
182 for (i = 1; i < argc; i++) {
183  k = argv[i];
184
185  if ( strcmp(k, "--server") == 0 ) {
186   server = argv[++i];
187  } else if ( strcmp(k, "--help") == 0 ) {
188   usage();
189   return 0;
190  } else if ( file == NULL ) {
191   file = k;
192  } else {
193   fprintf(stderr, "Error: unknown argument: %s\n", k);
194   usage();
195   return 1;
196  }
197 }
198
199 if ( roar_simple_connect(&con, server, "roarvorbis") == -1 ) {
200  ROAR_DBG("roar_simple_play(*): roar_simple_connect() faild!");
201  return -1;
202 }
203
204 if ( strncmp(file, "http:", 5) == 0 ) {
205  in = open_http(file);
206 } else {
207  in = fopen(file, "rb");
208 }
209
210 if ( in == NULL ) {
211  roar_disconnect(&con);
212  return -1;
213 }
214
215#ifdef _WIN32
216  _setmode(_fileno(in), _O_BINARY);
217#endif
218
219 if(ov_open(in, &vf, NULL, 0) < 0) {
220  fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n");
221  roar_disconnect(&con);
222  return -1;
223 }
224
225// if ( update_stream(&con, &s, &out, &vf, file) == -1 )
226//  return -1;
227
228 while (!eof) {
229  long ret = ov_read(&vf, pcmout, sizeof(pcmout), 0, 2, 1, &current_section);
230
231  if ( last_section != current_section )
232   if ( update_stream(&con, &s, &out, &vf, file, &info) == -1 )
233    return 1;
234
235  last_section = current_section;
236
237  if (ret == 0) {
238   /* EOF */
239   eof=1;
240  } else if (ret < 0) {
241   /* error in the stream.  Not a problem, just reporting it in
242      case we (the app) cares.  In this case, we don't. */
243  } else {
244     /* we don't bother dealing with sample rate changes, etc, but
245        you'll have to */
246//    write(out, pcmout, ret);
247   roar_stream_send_data(&con, &s, pcmout, ret);
248  }
249 }
250
251  ov_clear(&vf);
252
253// fclose(in);
254 close(out);
255 roar_disconnect(&con);
256
257 return 0;
258#endif
259}
260
261//ll
Note: See TracBrowser for help on using the repository browser.