source: roaraudio/roarclients/roarvorbis.c @ 124:70992549a17a

Last change on this file since 124:70992549a17a was 124:70992549a17a, checked in by phi, 16 years ago

added simple http support using wget

File size: 4.0 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
29
30FILE * 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
38int 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];
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
75 roar_stream_meta_set(con, s, ROAR_META_MODE_CLEAR, &meta);
76
77 if ( strncmp(file, "http:", 5) == 0 )
78  meta.type = ROAR_META_TYPE_FILEURL;
79 else
80  meta.type = ROAR_META_TYPE_FILENAME;
81
82 strncpy(value, file, 79);
83 roar_stream_meta_set(con, s, ROAR_META_MODE_SET, &meta);
84
85 while(*ptr){
86   for (j = 0; (*ptr)[j] != 0 && (*ptr)[j] != '='; j++)
87    key[j] = (*ptr)[j];
88    key[j] = 0;
89
90   for (j++, h = 0; (*ptr)[j] != 0 && (*ptr)[j] != '='; j++)
91    value[h++] = (*ptr)[j];
92    value[h]   = 0;
93
94   meta.type = roar_meta_inttype(key);
95   if ( meta.type != -1 )
96    roar_stream_meta_set(con, s, ROAR_META_MODE_SET, &meta);
97
98   fprintf(stderr, "Meta %-16s: %s\n", key, value);
99   ++ptr;
100 }
101
102 return 0;
103}
104
105int main (int argc, char * argv[]) {
106 char * server   = NULL;
107 char * file     = NULL;
108 char * k;
109 int    i;
110 FILE * in;
111 int    out = -1;
112 struct roar_connection con;
113 struct roar_stream     s;
114 OggVorbis_File vf;
115 int eof=0;
116 int current_section;
117 int last_section = -1;
118 struct roar_audio_info info;
119 char pcmout[4096];
120
121
122 for (i = 1; i < argc; i++) {
123  k = argv[i];
124
125  if ( strcmp(k, "--server") == 0 ) {
126   server = argv[++i];
127  } else if ( strcmp(k, "--help") == 0 ) {
128   usage();
129   return 0;
130  } else if ( file == NULL ) {
131   file = k;
132  } else {
133   fprintf(stderr, "Error: unknown argument: %s\n", k);
134   usage();
135   return 1;
136  }
137 }
138
139 if ( roar_simple_connect(&con, server, "roarvorbis") == -1 ) {
140  ROAR_DBG("roar_simple_play(*): roar_simple_connect() faild!");
141  return -1;
142 }
143
144 if ( strncmp(file, "http:", 5) == 0 ) {
145  in = open_http(file);
146 } else {
147  in = fopen(file, "rb");
148 }
149
150 if ( in == NULL ) {
151  roar_disconnect(&con);
152  return -1;
153 }
154
155#ifdef _WIN32
156  _setmode(_fileno(in), _O_BINARY);
157#endif
158
159 if(ov_open(in, &vf, NULL, 0) < 0) {
160  fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n");
161  roar_disconnect(&con);
162  return -1;
163 }
164
165// if ( update_stream(&con, &s, &out, &vf, file) == -1 )
166//  return -1;
167
168 while (!eof) {
169  long ret = ov_read(&vf, pcmout, sizeof(pcmout), 0, 2, 1, &current_section);
170
171  if ( last_section != current_section )
172   if ( update_stream(&con, &s, &out, &vf, file, &info) == -1 )
173    return -1;
174
175  last_section = current_section;
176
177  if (ret == 0) {
178   /* EOF */
179   eof=1;
180  } else if (ret < 0) {
181   /* error in the stream.  Not a problem, just reporting it in
182      case we (the app) cares.  In this case, we don't. */
183  } else {
184     /* we don't bother dealing with sample rate changes, etc, but
185        you'll have to */
186    write(out, pcmout, ret);
187  }
188 }
189
190  ov_clear(&vf);
191
192// fclose(in);
193 close(out);
194 roar_disconnect(&con);
195
196 return 0;
197}
198
199//ll
Note: See TracBrowser for help on using the repository browser.