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>
39 #include "iochannel.h"
44 #include "sample-util.h"
51 struct pa_source
*source
;
53 struct pa_sample_spec sample_spec
;
55 size_t in_fragment_size
, out_fragment_size
, in_fragments
, out_fragments
, out_fill
;
59 void *in_mmap
, *out_mmap
;
60 size_t in_mmap_length
, out_mmap_length
;
62 struct pa_io_event
*io_event
;
64 struct pa_memblock
**in_memblocks
, **out_memblocks
;
65 unsigned out_current
, in_current
;
66 struct pa_module
*module
;
69 static const char* const valid_modargs
[] = {
83 #define DEFAULT_SINK_NAME "oss_output"
84 #define DEFAULT_SOURCE_NAME "oss_input"
85 #define DEFAULT_DEVICE "/dev/dsp"
87 static void update_usage(struct userdata
*u
) {
88 pa_module_set_used(u
->module
,
89 (u
->sink
? pa_idxset_ncontents(u
->sink
->inputs
) : 0) +
90 (u
->sink
? pa_idxset_ncontents(u
->sink
->monitor_source
->outputs
) : 0) +
91 (u
->source
? pa_idxset_ncontents(u
->source
->outputs
) : 0));
94 static void out_fill_memblocks(struct userdata
*u
, unsigned n
) {
95 assert(u
&& u
->out_memblocks
);
98 struct pa_memchunk chunk
;
100 if (u
->out_memblocks
[u
->out_current
])
101 pa_memblock_unref_fixed(u
->out_memblocks
[u
->out_current
]);
103 chunk
.memblock
= u
->out_memblocks
[u
->out_current
] = pa_memblock_new_fixed(u
->out_mmap
+u
->out_fragment_size
*u
->out_current
, u
->out_fragment_size
, u
->core
->memblock_stat
);
104 assert(chunk
.memblock
);
105 chunk
.length
= chunk
.memblock
->length
;
108 pa_sink_render_into_full(u
->sink
, &chunk
);
111 while (u
->out_current
>= u
->out_fragments
)
112 u
->out_current
-= u
->out_fragments
;
118 static void do_write(struct userdata
*u
) {
119 struct count_info info
;
120 assert(u
&& u
->sink
);
124 if (ioctl(u
->fd
, SNDCTL_DSP_GETOPTR
, &info
) < 0) {
125 fprintf(stderr
, "SNDCTL_DSP_GETOPTR: %s\n", strerror(errno
));
129 u
->out_fill
= (u
->out_fragment_size
* u
->out_fragments
) - (info
.ptr
% u
->out_fragment_size
);
134 out_fill_memblocks(u
, info
.blocks
);
137 static void in_post_memblocks(struct userdata
*u
, unsigned n
) {
138 assert(u
&& u
->in_memblocks
);
141 struct pa_memchunk chunk
;
143 if (!u
->in_memblocks
[u
->in_current
]) {
144 chunk
.memblock
= u
->in_memblocks
[u
->in_current
] = pa_memblock_new_fixed(u
->in_mmap
+u
->in_fragment_size
*u
->in_current
, u
->in_fragment_size
, u
->core
->memblock_stat
);
145 chunk
.length
= chunk
.memblock
->length
;
148 pa_source_post(u
->source
, &chunk
);
152 while (u
->in_current
>= u
->in_fragments
)
153 u
->in_current
-= u
->in_fragments
;
159 static void in_clear_memblocks(struct userdata
*u
, unsigned n
) {
160 unsigned i
= u
->in_current
;
161 assert(u
&& u
->in_memblocks
);
163 if (n
> u
->in_fragments
)
167 if (u
->in_memblocks
[i
]) {
168 pa_memblock_unref_fixed(u
->in_memblocks
[i
]);
169 u
->in_memblocks
[i
] = NULL
;
173 while (i
>= u
->in_fragments
)
174 i
-= u
->in_fragments
;
180 static void do_read(struct userdata
*u
) {
181 struct count_info info
;
182 assert(u
&& u
->source
);
186 if (ioctl(u
->fd
, SNDCTL_DSP_GETIPTR
, &info
) < 0) {
187 fprintf(stderr
, "SNDCTL_DSP_GETIPTR: %s\n", strerror(errno
));
194 in_post_memblocks(u
, info
.blocks
);
195 in_clear_memblocks(u
, u
->in_fragments
/2);
198 static void io_callback(struct pa_mainloop_api
*m
, struct pa_io_event
*e
, int fd
, enum pa_io_event_flags f
, void *userdata
) {
199 struct userdata
*u
= userdata
;
200 assert (u
&& u
->core
->mainloop
== m
&& u
->io_event
== e
);
202 if (f
& PA_IO_EVENT_INPUT
)
204 if (f
& PA_IO_EVENT_OUTPUT
)
208 static uint32_t sink_get_latency_cb(struct pa_sink
*s
) {
209 struct userdata
*u
= s
->userdata
;
213 return pa_bytes_to_usec(u
->out_fill
, &s
->sample_spec
);
216 int pa_module_init(struct pa_core
*c
, struct pa_module
*m
) {
217 struct audio_buf_info info
;
218 struct userdata
*u
= NULL
;
220 int nfrags
, frag_size
;
222 int enable_bits
= 0, zero
= 0;
223 int playback
= 1, record
= 1;
224 struct pa_modargs
*ma
= NULL
;
227 m
->userdata
= u
= pa_xmalloc0(sizeof(struct userdata
));
232 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
233 fprintf(stderr
, __FILE__
": failed to parse module arguments.\n");
237 if (pa_modargs_get_value_u32(ma
, "record", &record
) < 0 || pa_modargs_get_value_u32(ma
, "playback", &playback
) < 0) {
238 fprintf(stderr
, __FILE__
": record= and playback= expect numeric arguments.\n");
242 mode
= (playback
&&record
) ? O_RDWR
: (playback
? O_WRONLY
: (record
? O_RDONLY
: 0));
244 fprintf(stderr
, __FILE__
": neither playback nor record enabled for device.\n");
250 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) {
251 fprintf(stderr
, __FILE__
": failed to parse fragments arguments\n");
255 u
->sample_spec
= c
->default_sample_spec
;
256 if (pa_modargs_get_sample_spec(ma
, &u
->sample_spec
) < 0) {
257 fprintf(stderr
, __FILE__
": failed to parse sample specification\n");
261 if ((u
->fd
= pa_oss_open(p
= pa_modargs_get_value(ma
, "device", DEFAULT_DEVICE
), &mode
, &caps
)) < 0)
264 if (!(caps
& DSP_CAP_MMAP
) || !(caps
& DSP_CAP_REALTIME
) || !(caps
& DSP_CAP_TRIGGER
)) {
265 fprintf(stderr
, "OSS device not mmap capable.\n");
269 fprintf(stderr
, "module-oss: device opened in %s mode.\n", mode
== O_WRONLY
? "O_WRONLY" : (mode
== O_RDONLY
? "O_RDONLY" : "O_RDWR"));
271 if (pa_oss_set_fragments(u
->fd
, nfrags
, frag_size
) < 0)
274 if (pa_oss_auto_format(u
->fd
, &u
->sample_spec
) < 0)
277 if (mode
!= O_WRONLY
) {
278 if (ioctl(u
->fd
, SNDCTL_DSP_GETISPACE
, &info
) < 0) {
279 fprintf(stderr
, "SNDCTL_DSP_GETISPACE: %s\n", strerror(errno
));
283 fprintf(stderr
, "module-oss-mmap: input -- %u fragments of size %u.\n", info
.fragstotal
, info
.fragsize
);
284 u
->in_mmap_length
= (u
->in_fragment_size
= info
.fragsize
) * (u
->in_fragments
= info
.fragstotal
);
286 if ((u
->in_mmap
= mmap(NULL
, u
->in_mmap_length
, PROT_READ
, MAP_SHARED
, u
->fd
, 0)) == MAP_FAILED
) {
287 if (mode
== O_RDWR
) {
288 fprintf(stderr
, "module-oss-mmap: mmap failed for input. Changing to O_WRONLY mode.\n");
291 fprintf(stderr
, "modeule-oss-mmap: mmap(): %s\n", strerror(errno
));
296 u
->source
= pa_source_new(c
, pa_modargs_get_value(ma
, "source_name", DEFAULT_SOURCE_NAME
), 0, &u
->sample_spec
);
298 u
->source
->userdata
= u
;
299 pa_source_set_owner(u
->source
, m
);
300 u
->source
->description
= pa_sprintf_malloc("Open Sound System PCM/mmap() on '%s'", p
);
302 u
->in_memblocks
= pa_xmalloc0(sizeof(struct pa_memblock
*)*u
->in_fragments
);
304 enable_bits
|= PCM_ENABLE_INPUT
;
308 if (mode
!= O_RDONLY
) {
309 if (ioctl(u
->fd
, SNDCTL_DSP_GETOSPACE
, &info
) < 0) {
310 fprintf(stderr
, "SNDCTL_DSP_GETOSPACE: %s\n", strerror(errno
));
314 fprintf(stderr
, "module-oss: output -- %u fragments of size %u.\n", info
.fragstotal
, info
.fragsize
);
315 u
->out_mmap_length
= (u
->out_fragment_size
= info
.fragsize
) * (u
->out_fragments
= info
.fragstotal
);
317 if ((u
->out_mmap
= mmap(NULL
, u
->out_mmap_length
, PROT_WRITE
, MAP_SHARED
, u
->fd
, 0)) == MAP_FAILED
) {
318 if (mode
== O_RDWR
) {
319 fprintf(stderr
, "module-oss-mmap: mmap filed for output. Changing to O_RDONLY mode.\n");
322 fprintf(stderr
, "module-oss-mmap: mmap(): %s\n", strerror(errno
));
326 pa_silence_memory(u
->out_mmap
, u
->out_mmap_length
, &u
->sample_spec
);
328 u
->sink
= pa_sink_new(c
, pa_modargs_get_value(ma
, "sink_name", DEFAULT_SINK_NAME
), 0, &u
->sample_spec
);
330 u
->sink
->get_latency
= sink_get_latency_cb
;
331 u
->sink
->userdata
= u
;
332 pa_sink_set_owner(u
->sink
, m
);
333 u
->sink
->description
= pa_sprintf_malloc("Open Sound System PCM/mmap() on '%s'", p
);
335 u
->out_memblocks
= pa_xmalloc0(sizeof(struct memblock
*)*u
->out_fragments
);
337 enable_bits
|= PCM_ENABLE_OUTPUT
;
342 if (ioctl(u
->fd
, SNDCTL_DSP_SETTRIGGER
, &zero
) < 0) {
343 fprintf(stderr
, "SNDCTL_DSP_SETTRIGGER: %s\n", strerror(errno
));
347 if (ioctl(u
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable_bits
) < 0) {
348 fprintf(stderr
, "SNDCTL_DSP_SETTRIGGER: %s\n", strerror(errno
));
352 assert(u
->source
|| u
->sink
);
354 u
->io_event
= c
->mainloop
->io_new(c
->mainloop
, u
->fd
, (u
->source
? PA_IO_EVENT_INPUT
: 0) | (u
->sink
? PA_IO_EVENT_OUTPUT
: 0), io_callback
, u
);
362 pa_module_done(c
, m
);
370 void pa_module_done(struct pa_core
*c
, struct pa_module
*m
) {
374 if (!(u
= m
->userdata
))
377 if (u
->out_memblocks
) {
379 for (i
= 0; i
< u
->out_fragments
; i
++)
380 if (u
->out_memblocks
[i
])
381 pa_memblock_unref_fixed(u
->out_memblocks
[i
]);
382 pa_xfree(u
->out_memblocks
);
385 if (u
->in_memblocks
) {
387 for (i
= 0; i
< u
->in_fragments
; i
++)
388 if (u
->in_memblocks
[i
])
389 pa_memblock_unref_fixed(u
->in_memblocks
[i
]);
390 pa_xfree(u
->in_memblocks
);
393 if (u
->in_mmap
&& u
->in_mmap
!= MAP_FAILED
)
394 munmap(u
->in_mmap
, u
->in_mmap_length
);
396 if (u
->out_mmap
&& u
->out_mmap
!= MAP_FAILED
)
397 munmap(u
->out_mmap
, u
->out_mmap_length
);
400 pa_sink_free(u
->sink
);
403 pa_source_free(u
->source
);
406 u
->core
->mainloop
->io_free(u
->io_event
);