source: roaraudio/roard/codecfilter_vorbis.c @ 207:07ebcec64ab8

Last change on this file since 207:07ebcec64ab8 was 207:07ebcec64ab8, checked in by phi, 16 years ago

basic meta data support

File size: 2.9 KB
Line 
1//codecfilter_vorbis.c:
2
3#include "roard.h"
4
5int cf_vorbis_open(CODECFILTER_USERDATA_T * inst, int codec,
6                                            struct roar_stream_server * info,
7                                            struct roar_codecfilter   * filter) {
8 struct codecfilter_vorbis_inst * self = malloc(sizeof(struct codecfilter_vorbis_inst));
9
10 if ( !self )
11  return -1;
12
13 self->current_section      = -1;
14 self->last_section         = -1;
15 self->opened               = 0;
16 self->stream               = info;
17// self->outlen               = ROAR_OUTPUT_BUFFER_SAMPLES * s->info.channels * s->info.bits / 8; // optimal size
18
19 if ( (self->in = fdopen(((struct roar_stream*)info)->fh, "r")) == NULL ) {
20  free((void*)self);
21  return -1;
22 }
23
24 *inst = (CODECFILTER_USERDATA_T) self;
25
26 ((struct roar_stream*)info)->info.codec = ROAR_CODEC_DEFAULT;
27
28 return 0;
29}
30
31int cf_vorbis_close(CODECFILTER_USERDATA_T   inst) {
32 struct codecfilter_vorbis_inst * self = (struct codecfilter_vorbis_inst *) inst;
33
34 if ( !inst )
35  return -1;
36
37 ov_clear(&(self->vf));
38
39 free(inst);
40 return 0;
41}
42
43int cf_vorbis_read(CODECFILTER_USERDATA_T   inst, char * buf, int len) {
44 struct codecfilter_vorbis_inst * self = (struct codecfilter_vorbis_inst *) inst;
45 long r;
46 long todo = len;
47 long done = 0;
48
49// printf("cf_vorbis_read(inst=%p, buf=%p, len=%i) = ?\n", inst, buf, len);
50
51 self->opened++;
52 if ( self->opened == 16 ) {
53  //printf("cf_vorbis_read(*): opening...\n");
54  if ( ov_open(self->in, &(self->vf), NULL, 0) < 0 ) {
55   free((void*)self);
56   return 0;
57  }
58 }
59
60 if ( self->opened < 16 ) {
61  return -1;
62 }
63
64 while (todo) {
65  r = ov_read(&(self->vf), buf+done, todo, 0, 2, 1, &(self->current_section));
66  if ( r < 1 ) {
67   break;
68  } else {
69   if ( self->last_section != self->current_section )
70    if ( cf_vorbis_update_stream(self) == -1 )
71     return 0;
72
73   self->last_section = self->current_section;
74   todo -= r;
75   done += r;
76  }
77 }
78
79 //printf("ov_read(*) = %i\n", done);
80
81 if ( done == 0 ) {
82  // do some EOF handling...
83  return 0;
84 } else {
85  return len;
86 }
87}
88
89int cf_vorbis_update_stream (struct codecfilter_vorbis_inst * self) {
90 vorbis_info *vi = ov_info(&(self->vf), -1);
91 char **ptr = ov_comment(&(self->vf), -1)->user_comments;
92 char key[80] = {0}, value[80] = {0};
93 struct roar_stream * s = (struct roar_stream *) self->stream;
94 int type;
95 int j, h = 0;
96
97 s->info.channels = vi->channels;
98 s->info.rate     = vi->rate;
99 s->info.bits     = 16;
100 s->info.codec    = ROAR_CODEC_DEFAULT;
101
102 stream_meta_clear(s->id);
103
104 while(*ptr){
105   for (j = 0; (*ptr)[j] != 0 && (*ptr)[j] != '='; j++)
106    key[j] = (*ptr)[j];
107    key[j] = 0;
108
109   for (j++, h = 0; (*ptr)[j] != 0 && (*ptr)[j] != '='; j++)
110    value[h++] = (*ptr)[j];
111    value[h]   = 0;
112
113   type = roar_meta_inttype(key);
114   if ( type != -1 )
115    stream_meta_set(s->id, type, "", value);
116
117   ROAR_DBG("cf_vorbis_update_stream(*): Meta %-16s: %s", key, value);
118   ++ptr;
119 }
120
121 return 0;
122}
123
124//ll
Note: See TracBrowser for help on using the repository browser.