source: roaraudio/roarclients/roarvorbis.c @ 2875:5c14d2001848

Last change on this file since 2875:5c14d2001848 was 2568:271a25312484, checked in by phi, 15 years ago

use roar_libroar_set_server()

File size: 6.0 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) {
[861]56#ifdef ROAR_HAVE_BIN_WGET
[124]57 char cmd[1024];
58
[861]59 snprintf(cmd, 1023, ROAR_HAVE_BIN_WGET " -qO - '%s'", file);
[124]60
61 return popen(cmd, "r");
[861]62#else
63 return NULL;
64#endif
[124]65}
66
[122]67int update_stream (struct roar_connection * con, struct roar_stream * s, int * out, OggVorbis_File * vf, char * file, struct roar_audio_info * info) {
[121]68 vorbis_info *vi = ov_info(vf, -1);
[111]69 int    bits     = 16;
70 int    codec    = ROAR_CODEC_DEFAULT;
[121]71 char **ptr = ov_comment(vf, -1)->user_comments;
[718]72 char key[ROAR_META_MAX_NAMELEN], value[LIBROAR_BUFFER_MSGDATA] = {0};
[121]73 int j, h = 0;
74 struct roar_meta   meta;
[122]75 int need_new_stream = 0;
[715]76 int meta_ok;
[121]77
78 fprintf(stderr, "\n");
79
[122]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 )
[121]88  close(*out);
89
[122]90  fprintf(stderr, "Audio: %i channel, %liHz\n\n", vi->channels, vi->rate);
91
92  info->rate     = vi->rate;
93  info->channels = vi->channels;
[121]94
[122]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  }
[121]99 }
100
101
102 meta.value = value;
103 meta.key[0] = 0;
[132]104 meta.type = ROAR_META_TYPE_NONE;
[121]105
106 roar_stream_meta_set(con, s, ROAR_META_MODE_CLEAR, &meta);
107
[124]108 if ( strncmp(file, "http:", 5) == 0 )
109  meta.type = ROAR_META_TYPE_FILEURL;
110 else
111  meta.type = ROAR_META_TYPE_FILENAME;
112
[715]113
[719]114 strncpy(value, file, LIBROAR_BUFFER_MSGDATA-1);
115 value[LIBROAR_BUFFER_MSGDATA-1] = 0;
[121]116 roar_stream_meta_set(con, s, ROAR_META_MODE_SET, &meta);
117
118 while(*ptr){
[715]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    }
[121]128    key[j] = (*ptr)[j];
[715]129   }
130   key[j] = 0;
[121]131
[715]132   if ( meta_ok ) {
133    for (j++, h = 0; (*ptr)[j] != 0 && (*ptr)[j] != '='; j++) {
[718]134     if ( h == LIBROAR_BUFFER_MSGDATA ) {
[715]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    }
[121]142    value[h]   = 0;
[715]143   }
[121]144
[715]145   if ( meta_ok ) {
146    fprintf(stderr, "Meta %-16s: %s\n", key, value);
[121]147
[715]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++;
[121]154 }
155
[1238]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
[121]161 return 0;
162}
163
[478]164#endif
165
[121]166int main (int argc, char * argv[]) {
[478]167#ifndef ROAR_HAVE_LIBVORBISFILE
[479]168 fprintf(stderr, "Error: no Vorbis support!\n");
[478]169 return 1;
170#else
[111]171 char * server   = NULL;
172 char * file     = NULL;
173 char * k;
174 int    i;
175 FILE * in;
[120]176 int    out = -1;
[111]177 struct roar_connection con;
178 struct roar_stream     s;
179 OggVorbis_File vf;
180 int eof=0;
[132]181 int current_section = -1;
[120]182 int last_section = -1;
[122]183 struct roar_audio_info info;
[111]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
[2568]204 roar_libroar_set_server(server);
205
[1510]206 if ( file == NULL ) {
207  ROAR_ERR("No filename given.");
208  return 1;
209 }
210
[115]211 if ( roar_simple_connect(&con, server, "roarvorbis") == -1 ) {
[111]212  ROAR_DBG("roar_simple_play(*): roar_simple_connect() faild!");
[1406]213  return 1;
[111]214 }
215
[124]216 if ( strncmp(file, "http:", 5) == 0 ) {
217  in = open_http(file);
218 } else {
219  in = fopen(file, "rb");
220 }
221
222 if ( in == NULL ) {
[111]223  roar_disconnect(&con);
[1406]224  return 1;
[111]225 }
226
227#ifdef _WIN32
228  _setmode(_fileno(in), _O_BINARY);
229#endif
230
231 if(ov_open(in, &vf, NULL, 0) < 0) {
232  fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n");
233  roar_disconnect(&con);
[1406]234  return 1;
[111]235 }
236
[121]237// if ( update_stream(&con, &s, &out, &vf, file) == -1 )
[1406]238//  return 1;
[111]239
[117]240 while (!eof) {
241  long ret = ov_read(&vf, pcmout, sizeof(pcmout), 0, 2, 1, &current_section);
[120]242
[121]243  if ( last_section != current_section )
[122]244   if ( update_stream(&con, &s, &out, &vf, file, &info) == -1 )
[715]245    return 1;
[121]246
[120]247  last_section = current_section;
248
[111]249  if (ret == 0) {
250   /* EOF */
251   eof=1;
252  } else if (ret < 0) {
253   /* error in the stream.  Not a problem, just reporting it in
254      case we (the app) cares.  In this case, we don't. */
255  } else {
256     /* we don't bother dealing with sample rate changes, etc, but
257        you'll have to */
[507]258//    write(out, pcmout, ret);
259   roar_stream_send_data(&con, &s, pcmout, ret);
[111]260  }
261 }
262
263  ov_clear(&vf);
264
265// fclose(in);
[120]266 close(out);
267 roar_disconnect(&con);
[111]268
269 return 0;
[478]270#endif
[111]271}
272
273//ll
Note: See TracBrowser for help on using the repository browser.