source: roaraudio/roard/codecfilter_au.c @ 4004:f479c0c19c80

Last change on this file since 4004:f479c0c19c80 was 4004:f479c0c19c80, checked in by phi, 14 years ago

added codec filter which already can read AU files

File size: 7.4 KB
Line 
1//codecfilter_au.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010
5 *
6 *  This file is part of roard a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  RoarAudio is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 */
25
26#include "roard.h"
27
28#ifndef ROAR_WITHOUT_CF_AU
29
30int cf_au_open(CODECFILTER_USERDATA_T * inst, int codec,
31                                            struct roar_stream_server * info,
32                                            struct roar_codecfilter   * filter) {
33 struct codecfilter_au_inst * self = roar_mm_malloc(sizeof(struct codecfilter_au_inst));
34 struct roar_stream * s = ROAR_STREAM(info);
35
36 if ( !self )
37  return -1;
38
39 self->stream               = info;
40 self->vstream              = NULL;
41 self->opened               = 0;
42
43 *inst = (CODECFILTER_USERDATA_T) self;
44
45 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
46 return 0;
47}
48
49int cf_au_close(CODECFILTER_USERDATA_T   inst) {
50// struct codecfilter_au_inst * self = (struct codecfilter_au_inst *) inst;
51
52 if ( !inst )
53  return -1;
54
55 roar_mm_free(inst);
56 return 0;
57}
58
59int cf_au_read(CODECFILTER_USERDATA_T   inst, char * buf, int len) {
60 struct codecfilter_au_inst * self = (struct codecfilter_au_inst *) inst;
61 int r = -1;
62 char tbuf[ROAR_AU_MIN_HEADER_LEN];
63 uint32_t * header = (uint32_t*)tbuf;
64 struct roar_stream * ps = ROAR_STREAM(self->stream);
65 struct roar_stream *  s;
66 struct roar_audio_info info;
67 char * extra_header;
68 int codec = -1;
69 int vid, fh;
70 int i;
71
72 if ( self->opened ) {
73  return stream_vio_s_read(self->stream, buf, len);
74 } else {
75  if (stream_vio_s_read(self->stream, tbuf, ROAR_AU_MIN_HEADER_LEN) != ROAR_AU_MIN_HEADER_LEN) {
76   return -1;
77  }
78
79  for (i = 0; i < ROAR_AU_MIN_HEADER_LEN/4; i++) {
80   header[i] = ROAR_NET2HOST32(header[i]);
81   ROAR_DBG("cf_au_read(inst=%p, buf=%p, len=%i): header[%i] = 0x%.8x", inst, buf, len, i, header[i]);
82  }
83
84
85  // test the header, is this really a AU stream?
86  if ( header[0] != ROAR_AU_MAGIC )
87   return -1;
88
89  if ( header[1] != ROAR_AU_MIN_HEADER_LEN ) {
90   header[1] -= ROAR_AU_MIN_HEADER_LEN;
91
92   if ( header[1] > 32*1024 ) // do not allow more than 32KiB header...
93    return -1;
94
95   if ( (extra_header = roar_mm_malloc(header[1])) == NULL )
96    return -1;
97
98   if ( stream_vio_s_read(self->stream, extra_header, header[1]) != header[1] ) {
99    roar_mm_free(extra_header);
100    return -1;
101   }
102
103   roar_mm_free(extra_header);
104  }
105
106  // TODO: write better code here!
107
108  if ( (fh = streams_get_fh(ps->id)) == -1 ) {
109   return -1;
110  }
111
112  if ( (vid = streams_new_virtual(ps->id, &(self->vstream))) == -1 ) {
113   return -1;
114  }
115
116  ROAR_DBG("cf_au_read(*): self->vstream=%p", self->vstream);
117
118  s = ROAR_STREAM(self->vstream);
119
120  s->info.rate = header[4];
121
122  s->info.channels = header[5];
123
124  switch (header[3]) {
125   case ROAR_AU_CID_MULAW:
126     s->info.bits  =  8;
127     codec         =  ROAR_CODEC_MULAW;
128    break;
129   case ROAR_AU_CID_ALAW:
130     s->info.bits  =  8;
131     codec         =  ROAR_CODEC_ALAW;
132    break;
133   case ROAR_AU_CID_PCM_S_8:
134     s->info.bits  =  8;
135     codec         =  ROAR_CODEC_PCM_S_BE;
136    break;
137   case ROAR_AU_CID_PCM_S_16:
138     s->info.bits  = 16;
139     codec         =  ROAR_CODEC_PCM_S_BE;
140    break;
141   case ROAR_AU_CID_PCM_S_24:
142     s->info.bits  = 24;
143     codec         =  ROAR_CODEC_PCM_S_BE;
144    break;
145   case ROAR_AU_CID_PCM_S_32:
146     s->info.bits  = 32;
147     codec         =  ROAR_CODEC_PCM_S_BE;
148    break;
149   default:
150     return -1;
151  }
152
153  s->info.codec             = codec;
154  self->vstream->codec_orgi = codec;
155
156  memcpy(&info, &(s->info), sizeof(struct roar_audio_info));
157
158  if ( streams_set_fh(vid, fh) == -1 ) {
159   return -1;
160  }
161
162/*
163  if ( roar_vio_open_pass(&(self->vstream->vio), &(self->stream->vio)) == -1 ) {
164   return -1;
165  }
166*/
167
168  memcpy(&(self->vstream->vio), &(self->stream->vio), sizeof(struct roar_vio_calls));
169
170  if ( streams_set_null_io(ps->id) == -1 ) {
171   return -1;
172  }
173
174  memcpy(&(ps->info), &info, sizeof(struct roar_audio_info));
175
176  self->opened = 1;
177
178  errno = EAGAIN;
179  return -1;
180 }
181
182 return r;
183}
184
185int cf_au_write(CODECFILTER_USERDATA_T   inst, char * buf, int len) {
186 struct codecfilter_au_inst * self = (struct codecfilter_au_inst *) inst;
187 struct roar_stream         * s    = ROAR_STREAM(self->stream);
188 void     * headerdata;
189 uint32_t * header;
190 uint32_t codec;
191 int      sid;
192 int      i;
193
194 ROAR_DBG("cf_au_write(inst=%p, buf=%p, len=%i) = ?", inst, buf, len);
195 ROAR_DBG("cf_au_write(inst=%p, buf=%p, len=%i): self->opened=%i", inst, buf, len, self->opened);
196
197 if ( self->opened ) {
198  return stream_vio_s_write(self->stream, buf, len);
199 } else {
200
201  if ( s->fh == -1 ) {
202   errno = EAGAIN;
203   return -1;
204  }
205
206  sid = ROAR_STREAM(self->stream)->id;
207
208  if ( stream_prethru_destroy(sid) == -1 ) {
209   return -1;
210  }
211
212  if ( stream_prethru_add_data(sid, &headerdata, ROAR_AU_MIN_HEADER_LEN) == -1 ) {
213   return -1;
214  }
215
216  header = headerdata;
217
218  switch (s->info.codec) {
219   case ROAR_CODEC_PCM_S_BE:
220     switch (s->info.bits) {
221      case  8: codec = ROAR_AU_CID_PCM_S_8;  break;
222      case 16: codec = ROAR_AU_CID_PCM_S_16; break;
223      case 24: codec = ROAR_AU_CID_PCM_S_24; break;
224      case 32: codec = ROAR_AU_CID_PCM_S_32; break;
225      default:
226        ROAR_ERR("cf_au_write(*) bits per sample not supported!");
227        return -1;
228       break;
229     }
230    break;
231   default:
232     ROAR_ERR("cf_au_write(*) Codec not supported!: %s(%i)", roar_codec2str(s->info.codec), s->info.codec);
233     return -1;
234    break;
235  }
236
237  ROAR_DBG("cf_au_write(*) Codec supported!");
238
239  header[0] = ROAR_AU_MAGIC;
240  header[1] = ROAR_AU_MIN_HEADER_LEN;
241  header[2] = ROAR_AU_DATASIZE;
242  header[3] = codec;
243  header[4] = s->info.rate;
244  header[5] = s->info.channels;
245
246  for (i = 0; i < ROAR_AU_MIN_HEADER_LEN/4; i++)
247   header[i] = ROAR_HOST2NET32(header[i]);
248
249  if ( stream_vio_s_write(self->stream, header, ROAR_AU_MIN_HEADER_LEN) != ROAR_AU_MIN_HEADER_LEN )
250   return -1;
251
252  self->opened = 1;
253
254  errno = EAGAIN;
255//  return -1;
256
257  len = stream_vio_s_write(self->stream, buf, len);
258
259  cf_au_close(inst);
260  ROAR_STREAM_SERVER(s)->codecfilter = -1;
261
262  return len;
263
264//  return stream_vio_s_write(self->stream, buf, len);
265 }
266
267 return -1;
268}
269
270int cf_au_ctl(CODECFILTER_USERDATA_T   inst, int cmd, void * data) {
271 struct codecfilter_au_inst * self = (struct codecfilter_au_inst *) inst;
272 int_least32_t type = cmd & ROAR_STREAM_CTL_TYPEMASK;
273
274 cmd -= type;
275
276 ROAR_DBG("cf_au_ctl(*): command: cmd=0x%.8x, type=0x%.8x, pcmd=0x%.8x",
277                    cmd, type, ROAR_CODECFILTER_CTL2CMD(cmd));
278
279 switch (cmd) {
280  case ROAR_CODECFILTER_CTL2CMD(ROAR_CODECFILTER_CTL_VIRTUAL_DELETE):
281    streams_delete(ROAR_STREAM(self->stream)->id);
282    return 0;
283   break;
284  default:
285    ROAR_DBG("cf_au_ctl(*): Unknown command: cmd=0x%.8x, type=0x%.8x, pcmd=0x%.8x",
286                    cmd, type, ROAR_CODECFILTER_CTL2CMD(cmd));
287    return -1;
288 }
289
290 return -1;
291}
292
293#endif
294
295//ll
Note: See TracBrowser for help on using the repository browser.