]>
code.delx.au - pulseaudio/blob - polyp/module-oss.c
4 This file is part of polypaudio.
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include <sys/soundcard.h>
27 #include <sys/ioctl.h>
38 #include "iochannel.h"
43 #include "sample-util.h"
50 struct pa_source
*source
;
51 struct pa_iochannel
*io
;
54 struct pa_memchunk memchunk
, silence
;
56 uint32_t in_fragment_size
, out_fragment_size
, sample_size
;
59 struct pa_module
*module
;
62 static const char* const valid_modargs
[] = {
76 #define DEFAULT_SINK_NAME "oss_output"
77 #define DEFAULT_SOURCE_NAME "oss_input"
78 #define DEFAULT_DEVICE "/dev/dsp"
80 static void update_usage(struct userdata
*u
) {
81 pa_module_set_used(u
->module
,
82 (u
->sink
? pa_idxset_ncontents(u
->sink
->inputs
) : 0) +
83 (u
->sink
? pa_idxset_ncontents(u
->sink
->monitor_source
->outputs
) : 0) +
84 (u
->source
? pa_idxset_ncontents(u
->source
->outputs
) : 0));
87 static void do_write(struct userdata
*u
) {
88 struct pa_memchunk
*memchunk
;
92 if (!u
->sink
|| !pa_iochannel_is_writable(u
->io
))
97 memchunk
= &u
->memchunk
;
99 if (!memchunk
->length
)
100 if (pa_sink_render(u
->sink
, u
->out_fragment_size
, memchunk
) < 0)
101 memchunk
= &u
->silence
;
103 assert(memchunk
->memblock
);
104 assert(memchunk
->memblock
->data
);
105 assert(memchunk
->length
);
107 if ((r
= pa_iochannel_write(u
->io
, (uint8_t*) memchunk
->memblock
->data
+ memchunk
->index
, memchunk
->length
)) < 0) {
108 fprintf(stderr
, "write() failed: %s\n", strerror(errno
));
112 if (memchunk
== &u
->silence
)
113 assert(r
% u
->sample_size
== 0);
115 u
->memchunk
.index
+= r
;
116 u
->memchunk
.length
-= r
;
118 if (u
->memchunk
.length
<= 0) {
119 pa_memblock_unref(u
->memchunk
.memblock
);
120 u
->memchunk
.memblock
= NULL
;
125 static void do_read(struct userdata
*u
) {
126 struct pa_memchunk memchunk
;
130 if (!u
->source
|| !pa_iochannel_is_readable(u
->io
))
135 memchunk
.memblock
= pa_memblock_new(u
->in_fragment_size
, u
->core
->memblock_stat
);
136 assert(memchunk
.memblock
);
137 if ((r
= pa_iochannel_read(u
->io
, memchunk
.memblock
->data
, memchunk
.memblock
->length
)) < 0) {
138 pa_memblock_unref(memchunk
.memblock
);
140 fprintf(stderr
, "read() failed: %s\n", strerror(errno
));
144 assert(r
<= (ssize_t
) memchunk
.memblock
->length
);
145 memchunk
.length
= memchunk
.memblock
->length
= r
;
148 pa_source_post(u
->source
, &memchunk
);
149 pa_memblock_unref(memchunk
.memblock
);
152 static void io_callback(struct pa_iochannel
*io
, void*userdata
) {
153 struct userdata
*u
= userdata
;
159 static uint32_t sink_get_latency_cb(struct pa_sink
*s
) {
161 struct userdata
*u
= s
->userdata
;
162 assert(s
&& u
&& u
->sink
);
164 if (ioctl(u
->fd
, SNDCTL_DSP_GETODELAY
, &arg
) < 0) {
165 fprintf(stderr
, "module-oss: device doesn't support SNDCTL_DSP_GETODELAY.\n");
166 s
->get_latency
= NULL
;
170 return pa_bytes_to_usec(arg
, &s
->sample_spec
);
173 int pa_module_init(struct pa_core
*c
, struct pa_module
*m
) {
174 struct audio_buf_info info
;
175 struct userdata
*u
= NULL
;
178 int nfrags
, frag_size
, in_frag_size
, out_frag_size
;
180 uint32_t record
= 1, playback
= 1;
181 struct pa_sample_spec ss
;
182 struct pa_modargs
*ma
= NULL
;
185 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
186 fprintf(stderr
, __FILE__
": failed to parse module arguments.\n");
190 if (pa_modargs_get_value_u32(ma
, "record", &record
) < 0 || pa_modargs_get_value_u32(ma
, "playback", &playback
) < 0) {
191 fprintf(stderr
, __FILE__
": record= and playback= expect numeric argument.\n");
195 mode
= (playback
&&record
) ? O_RDWR
: (playback
? O_WRONLY
: (record
? O_RDONLY
: 0));
197 fprintf(stderr
, __FILE__
": neither playback nor record enabled for device.\n");
203 if (pa_modargs_get_value_u32(ma
, "fragments", &nfrags
) < 0 || nfrags
< 2 || pa_modargs_get_value_u32(ma
, "fragment_size", &frag_size
) < 0 || frag_size
< 1) {
204 fprintf(stderr
, __FILE__
": failed to parse fragments arguments\n");
208 ss
= c
->default_sample_spec
;
209 if (pa_modargs_get_sample_spec(ma
, &ss
) < 0) {
210 fprintf(stderr
, __FILE__
": failed to parse sample specification\n");
214 if ((fd
= pa_oss_open(p
= pa_modargs_get_value(ma
, "device", DEFAULT_DEVICE
), &mode
, NULL
)) < 0)
217 fprintf(stderr
, "module-oss: device opened in %s mode.\n", mode
== O_WRONLY
? "O_WRONLY" : (mode
== O_RDONLY
? "O_RDONLY" : "O_RDWR"));
219 if (pa_oss_set_fragments(fd
, nfrags
, frag_size
) < 0)
222 if (pa_oss_auto_format(fd
, &ss
) < 0)
225 if (ioctl(fd
, SNDCTL_DSP_GETBLKSIZE
, &frag_size
) < 0) {
226 fprintf(stderr
, "SNDCTL_DSP_GETBLKSIZE: %s\n", strerror(errno
));
230 in_frag_size
= out_frag_size
= frag_size
;
232 if (ioctl(fd
, SNDCTL_DSP_GETISPACE
, &info
) >= 0) {
233 fprintf(stderr
, "module-oss: input -- %u fragments of size %u.\n", info
.fragstotal
, info
.fragsize
);
234 in_frag_size
= info
.fragsize
;
237 if (ioctl(fd
, SNDCTL_DSP_GETOSPACE
, &info
) >= 0) {
238 fprintf(stderr
, "module-oss: output -- %u fragments of size %u.\n", info
.fragstotal
, info
.fragsize
);
239 out_frag_size
= info
.fragsize
;
242 u
= pa_xmalloc(sizeof(struct userdata
));
245 if (mode
!= O_WRONLY
) {
246 u
->source
= pa_source_new(c
, pa_modargs_get_value(ma
, "source_name", DEFAULT_SOURCE_NAME
), 0, &ss
);
248 u
->source
->userdata
= u
;
249 pa_source_set_owner(u
->source
, m
);
250 u
->source
->description
= pa_sprintf_malloc("Open Sound System PCM on '%s'", p
);
254 if (mode
!= O_RDONLY
) {
255 u
->sink
= pa_sink_new(c
, pa_modargs_get_value(ma
, "sink_name", DEFAULT_SINK_NAME
), 0, &ss
);
257 u
->sink
->get_latency
= sink_get_latency_cb
;
258 u
->sink
->userdata
= u
;
259 pa_sink_set_owner(u
->sink
, m
);
260 u
->sink
->description
= pa_sprintf_malloc("Open Sound System PCM on '%s'", p
);
264 assert(u
->source
|| u
->sink
);
266 u
->io
= pa_iochannel_new(c
->mainloop
, u
->source
? fd
: -1, u
->sink
? fd
: 0);
268 pa_iochannel_set_callback(u
->io
, io_callback
, u
);
271 u
->memchunk
.memblock
= NULL
;
272 u
->memchunk
.length
= 0;
273 u
->sample_size
= pa_frame_size(&ss
);
275 u
->out_fragment_size
= out_frag_size
;
276 u
->in_fragment_size
= in_frag_size
;
277 u
->silence
.memblock
= pa_memblock_new(u
->silence
.length
= u
->out_fragment_size
, u
->core
->memblock_stat
);
278 assert(u
->silence
.memblock
);
279 pa_silence_memblock(u
->silence
.memblock
, &ss
);
280 u
->silence
.index
= 0;
299 void pa_module_done(struct pa_core
*c
, struct pa_module
*m
) {
303 if (!(u
= m
->userdata
))
306 if (u
->memchunk
.memblock
)
307 pa_memblock_unref(u
->memchunk
.memblock
);
308 if (u
->silence
.memblock
)
309 pa_memblock_unref(u
->silence
.memblock
);
312 pa_sink_free(u
->sink
);
314 pa_source_free(u
->source
);
316 pa_iochannel_free(u
->io
);