source: roaraudio/roarclients/roarvorbis.c @ 507:5c9574ef2183

Last change on this file since 507:5c9574ef2183 was 507:5c9574ef2183, checked in by phi, 16 years ago

use roar_stream_send_data() not write()

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