source: roaraudio/roarclients/roarvorbis.c @ 121:1ed1307ca42e

Last change on this file since 121:1ed1307ca42e was 121:1ed1307ca42e, checked in by phi, 16 years ago

now using a extra function to update meta data and open the stream so we support chained Ogg :)

File size: 3.4 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) {
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
38 fprintf(stderr, "\n");
39
40 if ( *out != -1 )
41  close(*out);
42
43 fprintf(stderr, "Audio: %i channel, %liHz\n\n", vi->channels, vi->rate);
44
45 if ( (*out = roar_simple_new_stream_obj(con, s, vi->rate, vi->channels, bits, codec, ROAR_DIR_PLAY)) == -1 ) {
46  roar_disconnect(con);
47  return -1;
48 }
49
50
51 meta.value = value;
52 meta.key[0] = 0;
53
54 roar_stream_meta_set(con, s, ROAR_META_MODE_CLEAR, &meta);
55
56 meta.type = ROAR_META_TYPE_FILENAME;
57 strncpy(value, file, 79);
58 roar_stream_meta_set(con, s, ROAR_META_MODE_SET, &meta);
59
60 while(*ptr){
61   for (j = 0; (*ptr)[j] != 0 && (*ptr)[j] != '='; j++)
62    key[j] = (*ptr)[j];
63    key[j] = 0;
64
65   for (j++, h = 0; (*ptr)[j] != 0 && (*ptr)[j] != '='; j++)
66    value[h++] = (*ptr)[j];
67    value[h]   = 0;
68
69   meta.type = roar_meta_inttype(key);
70   if ( meta.type != -1 )
71    roar_stream_meta_set(con, s, ROAR_META_MODE_SET, &meta);
72
73   fprintf(stderr, "Meta %-16s: %s\n", key, value);
74   ++ptr;
75 }
76
77 return 0;
78}
79
80int main (int argc, char * argv[]) {
81 char * server   = NULL;
82 char * file     = NULL;
83 char * k;
84 int    i;
85 FILE * in;
86 int    out = -1;
87 struct roar_connection con;
88 struct roar_stream     s;
89 OggVorbis_File vf;
90 int eof=0;
91 int current_section;
92 int last_section = -1;
93 char pcmout[4096];
94
95
96 for (i = 1; i < argc; i++) {
97  k = argv[i];
98
99  if ( strcmp(k, "--server") == 0 ) {
100   server = argv[++i];
101  } else if ( strcmp(k, "--help") == 0 ) {
102   usage();
103   return 0;
104  } else if ( file == NULL ) {
105   file = k;
106  } else {
107   fprintf(stderr, "Error: unknown argument: %s\n", k);
108   usage();
109   return 1;
110  }
111 }
112
113 if ( roar_simple_connect(&con, server, "roarvorbis") == -1 ) {
114  ROAR_DBG("roar_simple_play(*): roar_simple_connect() faild!");
115  return -1;
116 }
117
118 if ( (in = fopen(file, "rb")) == NULL ) {
119  roar_disconnect(&con);
120  return -1;
121 }
122
123#ifdef _WIN32
124  _setmode(_fileno(in), _O_BINARY);
125#endif
126
127 if(ov_open(in, &vf, NULL, 0) < 0) {
128  fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n");
129  roar_disconnect(&con);
130  return -1;
131 }
132
133// if ( update_stream(&con, &s, &out, &vf, file) == -1 )
134//  return -1;
135
136 while (!eof) {
137  long ret = ov_read(&vf, pcmout, sizeof(pcmout), 0, 2, 1, &current_section);
138
139  if ( last_section != current_section )
140   if ( update_stream(&con, &s, &out, &vf, file) == -1 )
141    return -1;
142
143  last_section = current_section;
144
145  if (ret == 0) {
146   /* EOF */
147   eof=1;
148  } else if (ret < 0) {
149   /* error in the stream.  Not a problem, just reporting it in
150      case we (the app) cares.  In this case, we don't. */
151  } else {
152     /* we don't bother dealing with sample rate changes, etc, but
153        you'll have to */
154    write(out, pcmout, ret);
155  }
156 }
157
158  ov_clear(&vf);
159
160// fclose(in);
161 close(out);
162 roar_disconnect(&con);
163
164 return 0;
165}
166
167//ll
Note: See TracBrowser for help on using the repository browser.