source: roaraudio/roarclients/roarvorbis.c @ 800:7d400de1c704

Last change on this file since 800:7d400de1c704 was 719:56a866eb4e91, checked in by phi, 16 years ago

use buffer of correct length (filename)

File size: 5.7 KB
RevLine 
[111]1//roarvorbis.c:
2
[669]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
[111]25#include <roaraudio.h>
26
27#include <stdio.h>
28#include <stdlib.h>
29#include <math.h>
[478]30#ifdef ROAR_HAVE_LIBVORBISFILE
[111]31#include <vorbis/codec.h>
32#include <vorbis/vorbisfile.h>
[478]33#endif
[111]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
[124]53
[478]54#ifdef ROAR_HAVE_LIBVORBISFILE
[124]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
[122]63int update_stream (struct roar_connection * con, struct roar_stream * s, int * out, OggVorbis_File * vf, char * file, struct roar_audio_info * info) {
[121]64 vorbis_info *vi = ov_info(vf, -1);
[111]65 int    bits     = 16;
66 int    codec    = ROAR_CODEC_DEFAULT;
[121]67 char **ptr = ov_comment(vf, -1)->user_comments;
[718]68 char key[ROAR_META_MAX_NAMELEN], value[LIBROAR_BUFFER_MSGDATA] = {0};
[121]69 int j, h = 0;
70 struct roar_meta   meta;
[122]71 int need_new_stream = 0;
[715]72 int meta_ok;
[121]73
74 fprintf(stderr, "\n");
75
[122]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 )
[121]84  close(*out);
85
[122]86  fprintf(stderr, "Audio: %i channel, %liHz\n\n", vi->channels, vi->rate);
87
88  info->rate     = vi->rate;
89  info->channels = vi->channels;
[121]90
[122]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  }
[121]95 }
96
97
98 meta.value = value;
99 meta.key[0] = 0;
[132]100 meta.type = ROAR_META_TYPE_NONE;
[121]101
102 roar_stream_meta_set(con, s, ROAR_META_MODE_CLEAR, &meta);
103
[124]104 if ( strncmp(file, "http:", 5) == 0 )
105  meta.type = ROAR_META_TYPE_FILEURL;
106 else
107  meta.type = ROAR_META_TYPE_FILENAME;
108
[715]109
[719]110 strncpy(value, file, LIBROAR_BUFFER_MSGDATA-1);
111 value[LIBROAR_BUFFER_MSGDATA-1] = 0;
[121]112 roar_stream_meta_set(con, s, ROAR_META_MODE_SET, &meta);
113
114 while(*ptr){
[715]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    }
[121]124    key[j] = (*ptr)[j];
[715]125   }
126   key[j] = 0;
[121]127
[715]128   if ( meta_ok ) {
129    for (j++, h = 0; (*ptr)[j] != 0 && (*ptr)[j] != '='; j++) {
[718]130     if ( h == LIBROAR_BUFFER_MSGDATA ) {
[715]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    }
[121]138    value[h]   = 0;
[715]139   }
[121]140
[715]141   if ( meta_ok ) {
142    fprintf(stderr, "Meta %-16s: %s\n", key, value);
[121]143
[715]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++;
[121]150 }
151
152 return 0;
153}
154
[478]155#endif
156
[121]157int main (int argc, char * argv[]) {
[478]158#ifndef ROAR_HAVE_LIBVORBISFILE
[479]159 fprintf(stderr, "Error: no Vorbis support!\n");
[478]160 return 1;
161#else
[111]162 char * server   = NULL;
163 char * file     = NULL;
164 char * k;
165 int    i;
166 FILE * in;
[120]167 int    out = -1;
[111]168 struct roar_connection con;
169 struct roar_stream     s;
170 OggVorbis_File vf;
171 int eof=0;
[132]172 int current_section = -1;
[120]173 int last_section = -1;
[122]174 struct roar_audio_info info;
[111]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
[115]195 if ( roar_simple_connect(&con, server, "roarvorbis") == -1 ) {
[111]196  ROAR_DBG("roar_simple_play(*): roar_simple_connect() faild!");
197  return -1;
198 }
199
[124]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 ) {
[111]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
[121]221// if ( update_stream(&con, &s, &out, &vf, file) == -1 )
222//  return -1;
[111]223
[117]224 while (!eof) {
225  long ret = ov_read(&vf, pcmout, sizeof(pcmout), 0, 2, 1, &current_section);
[120]226
[121]227  if ( last_section != current_section )
[122]228   if ( update_stream(&con, &s, &out, &vf, file, &info) == -1 )
[715]229    return 1;
[121]230
[120]231  last_section = current_section;
232
[111]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 */
[507]242//    write(out, pcmout, ret);
243   roar_stream_send_data(&con, &s, pcmout, ret);
[111]244  }
245 }
246
247  ov_clear(&vf);
248
249// fclose(in);
[120]250 close(out);
251 roar_disconnect(&con);
[111]252
253 return 0;
[478]254#endif
[111]255}
256
257//ll
Note: See TracBrowser for help on using the repository browser.