source: roaraudio/roarclients/roarvorbis.c @ 122:150a6d2fdfbc

Last change on this file since 122:150a6d2fdfbc was 122:150a6d2fdfbc, checked in by phi, 16 years ago

only open a new stream if decoder infos changed, not every time we have a new logical bitstream

File size: 3.8 KB
Line 
1//roarvorbis.c:
2
3#include <roaraudio.h>
4
5#include <stdio.h>
6#include <stdlib.h>
7#include <math.h>
8#include <vorbis/codec.h>
9#include <vorbis/vorbisfile.h>
10
11#ifdef _WIN32
12#include <io.h>
13#include <fcntl.h>
14#endif
15
16#define BUFSIZE 1024
17
18void usage (void) {
19 printf("roarvorbis [OPTIONS]... FILE\n");
20
21 printf("\nOptions:\n\n");
22
23 printf("  --server SERVER    - Set server hostname\n"
24        "  --help             - Show this help\n"
25       );
26
27}
28
29int update_stream (struct roar_connection * con, struct roar_stream * s, int * out, OggVorbis_File * vf, char * file, struct roar_audio_info * info) {
30 vorbis_info *vi = ov_info(vf, -1);
31 int    bits     = 16;
32 int    codec    = ROAR_CODEC_DEFAULT;
33 char **ptr = ov_comment(vf, -1)->user_comments;
34 char key[80], value[80];
35 int j, h = 0;
36 struct roar_meta   meta;
37 int need_new_stream = 0;
38
39 fprintf(stderr, "\n");
40
41 if ( *out == -1 ) {
42  need_new_stream = 1;
43 } else if ( info->rate != vi->rate || info->channels != vi->channels ) {
44  need_new_stream = 1;
45 }
46
47 if ( need_new_stream ) {
48  if ( *out != -1 )
49  close(*out);
50
51  fprintf(stderr, "Audio: %i channel, %liHz\n\n", vi->channels, vi->rate);
52
53  info->rate     = vi->rate;
54  info->channels = vi->channels;
55
56  if ( (*out = roar_simple_new_stream_obj(con, s, vi->rate, vi->channels, bits, codec, ROAR_DIR_PLAY)) == -1 ) {
57   roar_disconnect(con);
58   return -1;
59  }
60 }
61
62
63 meta.value = value;
64 meta.key[0] = 0;
65
66 roar_stream_meta_set(con, s, ROAR_META_MODE_CLEAR, &meta);
67
68 meta.type = ROAR_META_TYPE_FILENAME;
69 strncpy(value, file, 79);
70 roar_stream_meta_set(con, s, ROAR_META_MODE_SET, &meta);
71
72 while(*ptr){
73   for (j = 0; (*ptr)[j] != 0 && (*ptr)[j] != '='; j++)
74    key[j] = (*ptr)[j];
75    key[j] = 0;
76
77   for (j++, h = 0; (*ptr)[j] != 0 && (*ptr)[j] != '='; j++)
78    value[h++] = (*ptr)[j];
79    value[h]   = 0;
80
81   meta.type = roar_meta_inttype(key);
82   if ( meta.type != -1 )
83    roar_stream_meta_set(con, s, ROAR_META_MODE_SET, &meta);
84
85   fprintf(stderr, "Meta %-16s: %s\n", key, value);
86   ++ptr;
87 }
88
89 return 0;
90}
91
92int main (int argc, char * argv[]) {
93 char * server   = NULL;
94 char * file     = NULL;
95 char * k;
96 int    i;
97 FILE * in;
98 int    out = -1;
99 struct roar_connection con;
100 struct roar_stream     s;
101 OggVorbis_File vf;
102 int eof=0;
103 int current_section;
104 int last_section = -1;
105 struct roar_audio_info info;
106 char pcmout[4096];
107
108
109 for (i = 1; i < argc; i++) {
110  k = argv[i];
111
112  if ( strcmp(k, "--server") == 0 ) {
113   server = argv[++i];
114  } else if ( strcmp(k, "--help") == 0 ) {
115   usage();
116   return 0;
117  } else if ( file == NULL ) {
118   file = k;
119  } else {
120   fprintf(stderr, "Error: unknown argument: %s\n", k);
121   usage();
122   return 1;
123  }
124 }
125
126 if ( roar_simple_connect(&con, server, "roarvorbis") == -1 ) {
127  ROAR_DBG("roar_simple_play(*): roar_simple_connect() faild!");
128  return -1;
129 }
130
131 if ( (in = fopen(file, "rb")) == NULL ) {
132  roar_disconnect(&con);
133  return -1;
134 }
135
136#ifdef _WIN32
137  _setmode(_fileno(in), _O_BINARY);
138#endif
139
140 if(ov_open(in, &vf, NULL, 0) < 0) {
141  fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n");
142  roar_disconnect(&con);
143  return -1;
144 }
145
146// if ( update_stream(&con, &s, &out, &vf, file) == -1 )
147//  return -1;
148
149 while (!eof) {
150  long ret = ov_read(&vf, pcmout, sizeof(pcmout), 0, 2, 1, &current_section);
151
152  if ( last_section != current_section )
153   if ( update_stream(&con, &s, &out, &vf, file, &info) == -1 )
154    return -1;
155
156  last_section = current_section;
157
158  if (ret == 0) {
159   /* EOF */
160   eof=1;
161  } else if (ret < 0) {
162   /* error in the stream.  Not a problem, just reporting it in
163      case we (the app) cares.  In this case, we don't. */
164  } else {
165     /* we don't bother dealing with sample rate changes, etc, but
166        you'll have to */
167    write(out, pcmout, ret);
168  }
169 }
170
171  ov_clear(&vf);
172
173// fclose(in);
174 close(out);
175 roar_disconnect(&con);
176
177 return 0;
178}
179
180//ll
Note: See TracBrowser for help on using the repository browser.