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 Lesser 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 Lesser 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 <polypcore/iochannel.h>
40 #include <polypcore/sink.h>
41 #include <polypcore/source.h>
42 #include <polypcore/module.h>
43 #include <polypcore/sample-util.h>
44 #include <polypcore/util.h>
45 #include <polypcore/modargs.h>
46 #include <polypcore/xmalloc.h>
47 #include <polypcore/log.h>
50 #include "module-oss-mmap-symdef.h"
52 PA_MODULE_AUTHOR("Lennart Poettering")
53 PA_MODULE_DESCRIPTION("OSS Sink/Source (mmap)")
54 PA_MODULE_VERSION(PACKAGE_VERSION
)
56 "sink_name=<name for the sink> "
57 "source_name=<name for the source> "
58 "device=<OSS device> "
59 "record=<enable source?> "
60 "playback=<enable sink?> "
61 "format=<sample format> "
62 "channels=<number of channels> "
64 "fragments=<number of fragments> "
65 "fragment_size=<fragment size>")
71 pa_sample_spec sample_spec
;
73 size_t in_fragment_size
, out_fragment_size
, in_fragments
, out_fragments
;
74 int out_blocks_saved
, in_blocks_saved
;
78 void *in_mmap
, *out_mmap
;
79 size_t in_mmap_length
, out_mmap_length
;
81 pa_io_event
*io_event
;
83 pa_memblock
**in_memblocks
, **out_memblocks
;
84 unsigned out_current
, in_current
;
88 static const char* const valid_modargs
[] = {
102 #define DEFAULT_SINK_NAME "oss_output"
103 #define DEFAULT_SOURCE_NAME "oss_input"
104 #define DEFAULT_DEVICE "/dev/dsp"
105 #define DEFAULT_NFRAGS 12
106 #define DEFAULT_FRAGSIZE 1024
108 static void update_usage(struct userdata
*u
) {
109 pa_module_set_used(u
->module
,
110 (u
->sink
? pa_idxset_size(u
->sink
->inputs
) : 0) +
111 (u
->sink
? pa_idxset_size(u
->sink
->monitor_source
->outputs
) : 0) +
112 (u
->source
? pa_idxset_size(u
->source
->outputs
) : 0));
115 static void out_fill_memblocks(struct userdata
*u
, unsigned n
) {
116 assert(u
&& u
->out_memblocks
);
121 if (u
->out_memblocks
[u
->out_current
])
122 pa_memblock_unref_fixed(u
->out_memblocks
[u
->out_current
]);
124 chunk
.memblock
= u
->out_memblocks
[u
->out_current
] = pa_memblock_new_fixed((uint8_t*)u
->out_mmap
+u
->out_fragment_size
*u
->out_current
, u
->out_fragment_size
, 1, u
->core
->memblock_stat
);
125 assert(chunk
.memblock
);
126 chunk
.length
= chunk
.memblock
->length
;
129 pa_sink_render_into_full(u
->sink
, &chunk
);
132 while (u
->out_current
>= u
->out_fragments
)
133 u
->out_current
-= u
->out_fragments
;
139 static void do_write(struct userdata
*u
) {
140 struct count_info info
;
141 assert(u
&& u
->sink
);
145 if (ioctl(u
->fd
, SNDCTL_DSP_GETOPTR
, &info
) < 0) {
146 pa_log(__FILE__
": SNDCTL_DSP_GETOPTR: %s", strerror(errno
));
150 info
.blocks
+= u
->out_blocks_saved
;
151 u
->out_blocks_saved
= 0;
156 out_fill_memblocks(u
, info
.blocks
);
159 static void in_post_memblocks(struct userdata
*u
, unsigned n
) {
160 assert(u
&& u
->in_memblocks
);
165 if (!u
->in_memblocks
[u
->in_current
]) {
166 chunk
.memblock
= u
->in_memblocks
[u
->in_current
] = pa_memblock_new_fixed((uint8_t*) u
->in_mmap
+u
->in_fragment_size
*u
->in_current
, u
->in_fragment_size
, 1, u
->core
->memblock_stat
);
167 chunk
.length
= chunk
.memblock
->length
;
170 pa_source_post(u
->source
, &chunk
);
174 while (u
->in_current
>= u
->in_fragments
)
175 u
->in_current
-= u
->in_fragments
;
181 static void in_clear_memblocks(struct userdata
*u
, unsigned n
) {
182 unsigned i
= u
->in_current
;
183 assert(u
&& u
->in_memblocks
);
185 if (n
> u
->in_fragments
)
189 if (u
->in_memblocks
[i
]) {
190 pa_memblock_unref_fixed(u
->in_memblocks
[i
]);
191 u
->in_memblocks
[i
] = NULL
;
195 while (i
>= u
->in_fragments
)
196 i
-= u
->in_fragments
;
202 static void do_read(struct userdata
*u
) {
203 struct count_info info
;
204 assert(u
&& u
->source
);
208 if (ioctl(u
->fd
, SNDCTL_DSP_GETIPTR
, &info
) < 0) {
209 pa_log(__FILE__
": SNDCTL_DSP_GETIPTR: %s", strerror(errno
));
213 info
.blocks
+= u
->in_blocks_saved
;
214 u
->in_blocks_saved
= 0;
219 in_post_memblocks(u
, info
.blocks
);
220 in_clear_memblocks(u
, u
->in_fragments
/2);
223 static void io_callback(pa_mainloop_api
*m
, pa_io_event
*e
, PA_GCC_UNUSED
int fd
, pa_io_event_flags_t f
, void *userdata
) {
224 struct userdata
*u
= userdata
;
225 assert (u
&& u
->core
->mainloop
== m
&& u
->io_event
== e
);
227 if (f
& PA_IO_EVENT_INPUT
)
229 if (f
& PA_IO_EVENT_OUTPUT
)
233 static pa_usec_t
sink_get_latency_cb(pa_sink
*s
) {
234 struct userdata
*u
= s
->userdata
;
235 struct count_info info
;
239 if (ioctl(u
->fd
, SNDCTL_DSP_GETOPTR
, &info
) < 0) {
240 pa_log(__FILE__
": SNDCTL_DSP_GETOPTR: %s", strerror(errno
));
244 u
->out_blocks_saved
+= info
.blocks
;
246 bpos
= ((u
->out_current
+ u
->out_blocks_saved
) % u
->out_fragments
) * u
->out_fragment_size
;
248 if (bpos
< (size_t) info
.ptr
)
249 n
= (u
->out_fragments
* u
->out_fragment_size
) - (info
.ptr
- bpos
);
253 return pa_bytes_to_usec(n
, &s
->sample_spec
);
256 static pa_usec_t
source_get_latency_cb(pa_source
*s
) {
257 struct userdata
*u
= s
->userdata
;
258 struct count_info info
;
262 if (ioctl(u
->fd
, SNDCTL_DSP_GETIPTR
, &info
) < 0) {
263 pa_log(__FILE__
": SNDCTL_DSP_GETIPTR: %s", strerror(errno
));
267 u
->in_blocks_saved
+= info
.blocks
;
269 bpos
= ((u
->in_current
+ u
->in_blocks_saved
) % u
->in_fragments
) * u
->in_fragment_size
;
271 if (bpos
< (size_t) info
.ptr
)
274 n
= (u
->in_fragments
* u
->in_fragment_size
) - bpos
+ info
.ptr
;
276 return pa_bytes_to_usec(n
, &s
->sample_spec
);
279 static int sink_get_hw_volume(pa_sink
*s
) {
280 struct userdata
*u
= s
->userdata
;
282 if (pa_oss_get_pcm_volume(u
->fd
, &s
->sample_spec
, &s
->hw_volume
) < 0) {
283 pa_log_info(__FILE__
": device doesn't support reading mixer settings: %s", strerror(errno
));
284 s
->get_hw_volume
= NULL
;
291 static int sink_set_hw_volume(pa_sink
*s
) {
292 struct userdata
*u
= s
->userdata
;
294 if (pa_oss_set_pcm_volume(u
->fd
, &s
->sample_spec
, &s
->hw_volume
) < 0) {
295 pa_log_info(__FILE__
": device doesn't support writing mixer settings: %s", strerror(errno
));
296 s
->set_hw_volume
= NULL
;
303 static int source_get_hw_volume(pa_source
*s
) {
304 struct userdata
*u
= s
->userdata
;
306 if (pa_oss_get_input_volume(u
->fd
, &s
->sample_spec
, &s
->hw_volume
) < 0) {
307 pa_log_info(__FILE__
": device doesn't support reading mixer settings: %s", strerror(errno
));
308 s
->get_hw_volume
= NULL
;
315 static int source_set_hw_volume(pa_source
*s
) {
316 struct userdata
*u
= s
->userdata
;
318 if (pa_oss_set_input_volume(u
->fd
, &s
->sample_spec
, &s
->hw_volume
) < 0) {
319 pa_log_info(__FILE__
": device doesn't support writing mixer settings: %s", strerror(errno
));
320 s
->set_hw_volume
= NULL
;
327 int pa__init(pa_core
*c
, pa_module
*m
) {
328 struct audio_buf_info info
;
329 struct userdata
*u
= NULL
;
331 int nfrags
, frag_size
;
333 int enable_bits
= 0, zero
= 0;
334 int playback
= 1, record
= 1;
335 pa_modargs
*ma
= NULL
;
341 m
->userdata
= u
= pa_xnew0(struct userdata
, 1);
346 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
347 pa_log(__FILE__
": failed to parse module arguments.");
351 if (pa_modargs_get_value_boolean(ma
, "record", &record
) < 0 || pa_modargs_get_value_boolean(ma
, "playback", &playback
) < 0) {
352 pa_log(__FILE__
": record= and playback= expect numeric arguments.");
356 if (!playback
&& !record
) {
357 pa_log(__FILE__
": neither playback nor record enabled for device.");
361 mode
= (playback
&&record
) ? O_RDWR
: (playback
? O_WRONLY
: (record
? O_RDONLY
: 0));
363 nfrags
= DEFAULT_NFRAGS
;
364 frag_size
= DEFAULT_FRAGSIZE
;
365 if (pa_modargs_get_value_s32(ma
, "fragments", &nfrags
) < 0 || pa_modargs_get_value_s32(ma
, "fragment_size", &frag_size
) < 0) {
366 pa_log(__FILE__
": failed to parse fragments arguments");
370 u
->sample_spec
= c
->default_sample_spec
;
371 if (pa_modargs_get_sample_spec(ma
, &u
->sample_spec
) < 0) {
372 pa_log(__FILE__
": failed to parse sample specification");
376 if ((u
->fd
= pa_oss_open(p
= pa_modargs_get_value(ma
, "device", DEFAULT_DEVICE
), &mode
, &caps
)) < 0)
379 if (!(caps
& DSP_CAP_MMAP
) || !(caps
& DSP_CAP_REALTIME
) || !(caps
& DSP_CAP_TRIGGER
)) {
380 pa_log(__FILE__
": OSS device not mmap capable.");
384 pa_log_info(__FILE__
": device opened in %s mode.", mode
== O_WRONLY
? "O_WRONLY" : (mode
== O_RDONLY
? "O_RDONLY" : "O_RDWR"));
386 if (pa_oss_get_hw_description(p
, hwdesc
, sizeof(hwdesc
)) >= 0)
387 pa_log_info(__FILE__
": hardware name is '%s'.", hwdesc
);
391 if (nfrags
>= 2 && frag_size
>= 1)
392 if (pa_oss_set_fragments(u
->fd
, nfrags
, frag_size
) < 0)
395 if (pa_oss_auto_format(u
->fd
, &u
->sample_spec
) < 0)
398 if (mode
!= O_WRONLY
) {
399 if (ioctl(u
->fd
, SNDCTL_DSP_GETISPACE
, &info
) < 0) {
400 pa_log(__FILE__
": SNDCTL_DSP_GETISPACE: %s", strerror(errno
));
404 pa_log_info(__FILE__
": input -- %u fragments of size %u.", info
.fragstotal
, info
.fragsize
);
405 u
->in_mmap_length
= (u
->in_fragment_size
= info
.fragsize
) * (u
->in_fragments
= info
.fragstotal
);
407 if ((u
->in_mmap
= mmap(NULL
, u
->in_mmap_length
, PROT_READ
, MAP_SHARED
, u
->fd
, 0)) == MAP_FAILED
) {
408 if (mode
== O_RDWR
) {
409 pa_log(__FILE__
": mmap failed for input. Changing to O_WRONLY mode.");
412 pa_log(__FILE__
": mmap(): %s", strerror(errno
));
417 if (!(u
->source
= pa_source_new(c
, __FILE__
, pa_modargs_get_value(ma
, "source_name", DEFAULT_SOURCE_NAME
), 0, &u
->sample_spec
, NULL
)))
420 u
->source
->userdata
= u
;
421 u
->source
->get_latency
= source_get_latency_cb
;
422 u
->source
->get_hw_volume
= source_get_hw_volume
;
423 u
->source
->set_hw_volume
= source_set_hw_volume
;
424 pa_source_set_owner(u
->source
, m
);
425 u
->source
->description
= pa_sprintf_malloc("Open Sound System PCM/mmap() on '%s'%s%s%s",
427 hwdesc
[0] ? " (" : "",
428 hwdesc
[0] ? hwdesc
: "",
429 hwdesc
[0] ? ")" : "");
431 u
->in_memblocks
= pa_xnew0(pa_memblock
*, u
->in_fragments
);
433 enable_bits
|= PCM_ENABLE_INPUT
;
437 if (mode
!= O_RDONLY
) {
438 if (ioctl(u
->fd
, SNDCTL_DSP_GETOSPACE
, &info
) < 0) {
439 pa_log(__FILE__
": SNDCTL_DSP_GETOSPACE: %s", strerror(errno
));
443 pa_log_info(__FILE__
": output -- %u fragments of size %u.", info
.fragstotal
, info
.fragsize
);
444 u
->out_mmap_length
= (u
->out_fragment_size
= info
.fragsize
) * (u
->out_fragments
= info
.fragstotal
);
446 if ((u
->out_mmap
= mmap(NULL
, u
->out_mmap_length
, PROT_WRITE
, MAP_SHARED
, u
->fd
, 0)) == MAP_FAILED
) {
447 if (mode
== O_RDWR
) {
448 pa_log(__FILE__
": mmap filed for output. Changing to O_RDONLY mode.");
451 pa_log(__FILE__
": mmap(): %s", strerror(errno
));
455 pa_silence_memory(u
->out_mmap
, u
->out_mmap_length
, &u
->sample_spec
);
457 if (!(u
->sink
= pa_sink_new(c
, __FILE__
, pa_modargs_get_value(ma
, "sink_name", DEFAULT_SINK_NAME
), 0, &u
->sample_spec
, NULL
)))
460 u
->sink
->get_latency
= sink_get_latency_cb
;
461 u
->sink
->get_hw_volume
= sink_get_hw_volume
;
462 u
->sink
->set_hw_volume
= sink_set_hw_volume
;
463 u
->sink
->userdata
= u
;
464 pa_sink_set_owner(u
->sink
, m
);
465 u
->sink
->description
= pa_sprintf_malloc("Open Sound System PCM/mmap() on '%s'%s%s%s",
467 hwdesc
[0] ? " (" : "",
468 hwdesc
[0] ? hwdesc
: "",
469 hwdesc
[0] ? ")" : "");
471 u
->out_memblocks
= pa_xmalloc0(sizeof(struct memblock
*)*u
->out_fragments
);
473 enable_bits
|= PCM_ENABLE_OUTPUT
;
478 if (ioctl(u
->fd
, SNDCTL_DSP_SETTRIGGER
, &zero
) < 0) {
479 pa_log(__FILE__
": SNDCTL_DSP_SETTRIGGER: %s", strerror(errno
));
483 if (ioctl(u
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable_bits
) < 0) {
484 pa_log(__FILE__
": SNDCTL_DSP_SETTRIGGER: %s", strerror(errno
));
488 assert(u
->source
|| u
->sink
);
490 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
);
495 /* Read mixer settings */
497 source_get_hw_volume(u
->source
);
499 sink_get_hw_volume(u
->sink
);
512 void pa__done(pa_core
*c
, pa_module
*m
) {
518 if (!(u
= m
->userdata
))
521 if (u
->out_memblocks
) {
523 for (i
= 0; i
< u
->out_fragments
; i
++)
524 if (u
->out_memblocks
[i
])
525 pa_memblock_unref_fixed(u
->out_memblocks
[i
]);
526 pa_xfree(u
->out_memblocks
);
529 if (u
->in_memblocks
) {
531 for (i
= 0; i
< u
->in_fragments
; i
++)
532 if (u
->in_memblocks
[i
])
533 pa_memblock_unref_fixed(u
->in_memblocks
[i
]);
534 pa_xfree(u
->in_memblocks
);
537 if (u
->in_mmap
&& u
->in_mmap
!= MAP_FAILED
)
538 munmap(u
->in_mmap
, u
->in_mmap_length
);
540 if (u
->out_mmap
&& u
->out_mmap
!= MAP_FAILED
)
541 munmap(u
->out_mmap
, u
->out_mmap_length
);
544 pa_sink_disconnect(u
->sink
);
545 pa_sink_unref(u
->sink
);
549 pa_source_disconnect(u
->source
);
550 pa_source_unref(u
->source
);
554 u
->core
->mainloop
->io_free(u
->io_event
);