source: roaraudio/roarclients/roarvorbis.c @ 669:90348e6a785f

Last change on this file since 669:90348e6a785f was 669:90348e6a785f, checked in by phi, 16 years ago

added license statements

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