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 | |
---|
18 | void 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 | |
---|
29 | |
---|
30 | FILE * open_http (char * file) { |
---|
31 | char cmd[1024]; |
---|
32 | |
---|
33 | snprintf(cmd, 1023, "wget -qO - '%s'", file); |
---|
34 | |
---|
35 | return popen(cmd, "r"); |
---|
36 | } |
---|
37 | |
---|
38 | int update_stream (struct roar_connection * con, struct roar_stream * s, int * out, OggVorbis_File * vf, char * file, struct roar_audio_info * info) { |
---|
39 | vorbis_info *vi = ov_info(vf, -1); |
---|
40 | int bits = 16; |
---|
41 | int codec = ROAR_CODEC_DEFAULT; |
---|
42 | char **ptr = ov_comment(vf, -1)->user_comments; |
---|
43 | char key[80], value[80] = {0}; |
---|
44 | int j, h = 0; |
---|
45 | struct roar_meta meta; |
---|
46 | int need_new_stream = 0; |
---|
47 | |
---|
48 | fprintf(stderr, "\n"); |
---|
49 | |
---|
50 | if ( *out == -1 ) { |
---|
51 | need_new_stream = 1; |
---|
52 | } else if ( info->rate != vi->rate || info->channels != vi->channels ) { |
---|
53 | need_new_stream = 1; |
---|
54 | } |
---|
55 | |
---|
56 | if ( need_new_stream ) { |
---|
57 | if ( *out != -1 ) |
---|
58 | close(*out); |
---|
59 | |
---|
60 | fprintf(stderr, "Audio: %i channel, %liHz\n\n", vi->channels, vi->rate); |
---|
61 | |
---|
62 | info->rate = vi->rate; |
---|
63 | info->channels = vi->channels; |
---|
64 | |
---|
65 | if ( (*out = roar_simple_new_stream_obj(con, s, vi->rate, vi->channels, bits, codec, ROAR_DIR_PLAY)) == -1 ) { |
---|
66 | roar_disconnect(con); |
---|
67 | return -1; |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | |
---|
72 | meta.value = value; |
---|
73 | meta.key[0] = 0; |
---|
74 | meta.type = ROAR_META_TYPE_NONE; |
---|
75 | |
---|
76 | roar_stream_meta_set(con, s, ROAR_META_MODE_CLEAR, &meta); |
---|
77 | |
---|
78 | if ( strncmp(file, "http:", 5) == 0 ) |
---|
79 | meta.type = ROAR_META_TYPE_FILEURL; |
---|
80 | else |
---|
81 | meta.type = ROAR_META_TYPE_FILENAME; |
---|
82 | |
---|
83 | strncpy(value, file, 79); |
---|
84 | roar_stream_meta_set(con, s, ROAR_META_MODE_SET, &meta); |
---|
85 | |
---|
86 | while(*ptr){ |
---|
87 | for (j = 0; (*ptr)[j] != 0 && (*ptr)[j] != '='; j++) |
---|
88 | key[j] = (*ptr)[j]; |
---|
89 | key[j] = 0; |
---|
90 | |
---|
91 | for (j++, h = 0; (*ptr)[j] != 0 && (*ptr)[j] != '='; j++) |
---|
92 | value[h++] = (*ptr)[j]; |
---|
93 | value[h] = 0; |
---|
94 | |
---|
95 | meta.type = roar_meta_inttype(key); |
---|
96 | if ( meta.type != -1 ) |
---|
97 | roar_stream_meta_set(con, s, ROAR_META_MODE_SET, &meta); |
---|
98 | |
---|
99 | fprintf(stderr, "Meta %-16s: %s\n", key, value); |
---|
100 | ++ptr; |
---|
101 | } |
---|
102 | |
---|
103 | return 0; |
---|
104 | } |
---|
105 | |
---|
106 | int main (int argc, char * argv[]) { |
---|
107 | char * server = NULL; |
---|
108 | char * file = NULL; |
---|
109 | char * k; |
---|
110 | int i; |
---|
111 | FILE * in; |
---|
112 | int out = -1; |
---|
113 | struct roar_connection con; |
---|
114 | struct roar_stream s; |
---|
115 | OggVorbis_File vf; |
---|
116 | int eof=0; |
---|
117 | int current_section = -1; |
---|
118 | int last_section = -1; |
---|
119 | struct roar_audio_info info; |
---|
120 | char pcmout[4096]; |
---|
121 | |
---|
122 | |
---|
123 | for (i = 1; i < argc; i++) { |
---|
124 | k = argv[i]; |
---|
125 | |
---|
126 | if ( strcmp(k, "--server") == 0 ) { |
---|
127 | server = argv[++i]; |
---|
128 | } else if ( strcmp(k, "--help") == 0 ) { |
---|
129 | usage(); |
---|
130 | return 0; |
---|
131 | } else if ( file == NULL ) { |
---|
132 | file = k; |
---|
133 | } else { |
---|
134 | fprintf(stderr, "Error: unknown argument: %s\n", k); |
---|
135 | usage(); |
---|
136 | return 1; |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | if ( roar_simple_connect(&con, server, "roarvorbis") == -1 ) { |
---|
141 | ROAR_DBG("roar_simple_play(*): roar_simple_connect() faild!"); |
---|
142 | return -1; |
---|
143 | } |
---|
144 | |
---|
145 | if ( strncmp(file, "http:", 5) == 0 ) { |
---|
146 | in = open_http(file); |
---|
147 | } else { |
---|
148 | in = fopen(file, "rb"); |
---|
149 | } |
---|
150 | |
---|
151 | if ( in == NULL ) { |
---|
152 | roar_disconnect(&con); |
---|
153 | return -1; |
---|
154 | } |
---|
155 | |
---|
156 | #ifdef _WIN32 |
---|
157 | _setmode(_fileno(in), _O_BINARY); |
---|
158 | #endif |
---|
159 | |
---|
160 | if(ov_open(in, &vf, NULL, 0) < 0) { |
---|
161 | fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n"); |
---|
162 | roar_disconnect(&con); |
---|
163 | return -1; |
---|
164 | } |
---|
165 | |
---|
166 | // if ( update_stream(&con, &s, &out, &vf, file) == -1 ) |
---|
167 | // return -1; |
---|
168 | |
---|
169 | while (!eof) { |
---|
170 | long ret = ov_read(&vf, pcmout, sizeof(pcmout), 0, 2, 1, ¤t_section); |
---|
171 | |
---|
172 | if ( last_section != current_section ) |
---|
173 | if ( update_stream(&con, &s, &out, &vf, file, &info) == -1 ) |
---|
174 | return -1; |
---|
175 | |
---|
176 | last_section = current_section; |
---|
177 | |
---|
178 | if (ret == 0) { |
---|
179 | /* EOF */ |
---|
180 | eof=1; |
---|
181 | } else if (ret < 0) { |
---|
182 | /* error in the stream. Not a problem, just reporting it in |
---|
183 | case we (the app) cares. In this case, we don't. */ |
---|
184 | } else { |
---|
185 | /* we don't bother dealing with sample rate changes, etc, but |
---|
186 | you'll have to */ |
---|
187 | write(out, pcmout, ret); |
---|
188 | } |
---|
189 | } |
---|
190 | |
---|
191 | ov_clear(&vf); |
---|
192 | |
---|
193 | // fclose(in); |
---|
194 | close(out); |
---|
195 | roar_disconnect(&con); |
---|
196 | |
---|
197 | return 0; |
---|
198 | } |
---|
199 | |
---|
200 | //ll |
---|