1 | //codecfilter_vorbis.c: |
---|
2 | |
---|
3 | #include "roard.h" |
---|
4 | |
---|
5 | int 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 | |
---|
31 | int 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 | |
---|
43 | int 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 | todo -= r; |
---|
70 | done += r; |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | //printf("ov_read(*) = %i\n", done); |
---|
75 | |
---|
76 | if ( done == 0 ) { |
---|
77 | // do some EOF handling... |
---|
78 | return 0; |
---|
79 | } else { |
---|
80 | return len; |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | //ll |
---|