4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #include <sys/soundcard.h>
30 #include <sys/ioctl.h>
41 #ifdef HAVE_SYS_MMAN_H
45 #include <pulse/xmalloc.h>
46 #include <pulse/util.h>
48 #include <pulsecore/core-error.h>
49 #include <pulsecore/iochannel.h>
50 #include <pulsecore/sink.h>
51 #include <pulsecore/source.h>
52 #include <pulsecore/module.h>
53 #include <pulsecore/sample-util.h>
54 #include <pulsecore/core-util.h>
55 #include <pulsecore/modargs.h>
56 #include <pulsecore/log.h>
59 #include "module-oss-mmap-symdef.h"
61 PA_MODULE_AUTHOR("Lennart Poettering")
62 PA_MODULE_DESCRIPTION("OSS Sink/Source (mmap)")
63 PA_MODULE_VERSION(PACKAGE_VERSION
)
65 "sink_name=<name for the sink> "
66 "source_name=<name for the source> "
67 "device=<OSS device> "
68 "record=<enable source?> "
69 "playback=<enable sink?> "
70 "format=<sample format> "
71 "channels=<number of channels> "
73 "fragments=<number of fragments> "
74 "fragment_size=<fragment size> "
75 "channel_map=<channel map>")
81 pa_sample_spec sample_spec
;
83 size_t in_fragment_size
, out_fragment_size
;
84 unsigned in_fragments
, out_fragments
;
85 unsigned out_blocks_saved
, in_blocks_saved
;
89 void *in_mmap
, *out_mmap
;
90 size_t in_mmap_length
, out_mmap_length
;
92 pa_io_event
*io_event
;
94 pa_memblock
**in_memblocks
, **out_memblocks
;
95 unsigned out_current
, in_current
;
99 static const char* const valid_modargs
[] = {
114 #define DEFAULT_DEVICE "/dev/dsp"
115 #define DEFAULT_NFRAGS 12
116 #define DEFAULT_FRAGSIZE 1024
118 static void update_usage(struct userdata
*u
) {
119 pa_module_set_used(u
->module
,
120 (u
->sink
? pa_sink_used_by(u
->sink
) : 0) +
121 (u
->source
? pa_source_used_by(u
->source
) : 0));
124 static void clear_up(struct userdata
*u
) {
128 pa_sink_disconnect(u
->sink
);
129 pa_sink_unref(u
->sink
);
134 pa_source_disconnect(u
->source
);
135 pa_source_unref(u
->source
);
139 if (u
->in_mmap
&& u
->in_mmap
!= MAP_FAILED
) {
140 munmap(u
->in_mmap
, u
->in_mmap_length
);
144 if (u
->out_mmap
&& u
->out_mmap
!= MAP_FAILED
) {
145 munmap(u
->out_mmap
, u
->out_mmap_length
);
150 u
->core
->mainloop
->io_free(u
->io_event
);
160 static void out_fill_memblocks(struct userdata
*u
, unsigned n
) {
161 assert(u
&& u
->out_memblocks
);
166 if (u
->out_memblocks
[u
->out_current
])
167 pa_memblock_unref_fixed(u
->out_memblocks
[u
->out_current
]);
169 chunk
.memblock
= u
->out_memblocks
[u
->out_current
] =
170 pa_memblock_new_fixed(
172 (uint8_t*) u
->out_mmap
+u
->out_fragment_size
*u
->out_current
,
173 u
->out_fragment_size
,
175 assert(chunk
.memblock
);
176 chunk
.length
= chunk
.memblock
->length
;
179 pa_sink_render_into_full(u
->sink
, &chunk
);
182 while (u
->out_current
>= u
->out_fragments
)
183 u
->out_current
-= u
->out_fragments
;
189 static void do_write(struct userdata
*u
) {
190 struct count_info info
;
191 assert(u
&& u
->sink
);
195 if (ioctl(u
->fd
, SNDCTL_DSP_GETOPTR
, &info
) < 0) {
196 pa_log("SNDCTL_DSP_GETOPTR: %s", pa_cstrerror(errno
));
199 pa_module_unload_request(u
->module
);
203 info
.blocks
+= u
->out_blocks_saved
;
204 u
->out_blocks_saved
= 0;
209 out_fill_memblocks(u
, info
.blocks
);
212 static void in_post_memblocks(struct userdata
*u
, unsigned n
) {
213 assert(u
&& u
->in_memblocks
);
218 if (!u
->in_memblocks
[u
->in_current
]) {
219 chunk
.memblock
= u
->in_memblocks
[u
->in_current
] = pa_memblock_new_fixed(u
->core
->mempool
, (uint8_t*) u
->in_mmap
+u
->in_fragment_size
*u
->in_current
, u
->in_fragment_size
, 1);
220 chunk
.length
= chunk
.memblock
->length
;
223 pa_source_post(u
->source
, &chunk
);
227 while (u
->in_current
>= u
->in_fragments
)
228 u
->in_current
-= u
->in_fragments
;
234 static void in_clear_memblocks(struct userdata
*u
, unsigned n
) {
235 unsigned i
= u
->in_current
;
236 assert(u
&& u
->in_memblocks
);
238 if (n
> u
->in_fragments
)
242 if (u
->in_memblocks
[i
]) {
243 pa_memblock_unref_fixed(u
->in_memblocks
[i
]);
244 u
->in_memblocks
[i
] = NULL
;
248 while (i
>= u
->in_fragments
)
249 i
-= u
->in_fragments
;
255 static void do_read(struct userdata
*u
) {
256 struct count_info info
;
257 assert(u
&& u
->source
);
261 if (ioctl(u
->fd
, SNDCTL_DSP_GETIPTR
, &info
) < 0) {
262 pa_log("SNDCTL_DSP_GETIPTR: %s", pa_cstrerror(errno
));
265 pa_module_unload_request(u
->module
);
269 info
.blocks
+= u
->in_blocks_saved
;
270 u
->in_blocks_saved
= 0;
275 in_post_memblocks(u
, info
.blocks
);
276 in_clear_memblocks(u
, u
->in_fragments
/2);
279 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
) {
280 struct userdata
*u
= userdata
;
281 assert (u
&& u
->core
->mainloop
== m
&& u
->io_event
== e
);
283 if (f
& PA_IO_EVENT_ERROR
) {
285 pa_module_unload_request(u
->module
);
289 if (f
& PA_IO_EVENT_INPUT
)
291 if (f
& PA_IO_EVENT_OUTPUT
)
295 static pa_usec_t
sink_get_latency_cb(pa_sink
*s
) {
296 struct userdata
*u
= s
->userdata
;
297 struct count_info info
;
298 size_t bpos
, n
, total
;
301 if (ioctl(u
->fd
, SNDCTL_DSP_GETOPTR
, &info
) < 0) {
302 pa_log("SNDCTL_DSP_GETOPTR: %s", pa_cstrerror(errno
));
306 u
->out_blocks_saved
+= info
.blocks
;
308 total
= u
->out_fragments
* u
->out_fragment_size
;
309 bpos
= ((u
->out_current
+ u
->out_blocks_saved
) * u
->out_fragment_size
) % total
;
311 if (bpos
<= (size_t) info
.ptr
)
312 n
= total
- (info
.ptr
- bpos
);
316 /* pa_log("n = %u, bpos = %u, ptr = %u, total=%u, fragsize = %u, n_frags = %u\n", n, bpos, (unsigned) info.ptr, total, u->out_fragment_size, u->out_fragments); */
318 return pa_bytes_to_usec(n
, &s
->sample_spec
);
321 static pa_usec_t
source_get_latency_cb(pa_source
*s
) {
322 struct userdata
*u
= s
->userdata
;
323 struct count_info info
;
324 size_t bpos
, n
, total
;
327 if (ioctl(u
->fd
, SNDCTL_DSP_GETIPTR
, &info
) < 0) {
328 pa_log("SNDCTL_DSP_GETIPTR: %s", pa_cstrerror(errno
));
332 u
->in_blocks_saved
+= info
.blocks
;
334 total
= u
->in_fragments
* u
->in_fragment_size
;
335 bpos
= ((u
->in_current
+ u
->in_blocks_saved
) * u
->in_fragment_size
) % total
;
337 if (bpos
<= (size_t) info
.ptr
)
340 n
= (u
->in_fragments
* u
->in_fragment_size
) - bpos
+ info
.ptr
;
342 /* pa_log("n = %u, bpos = %u, ptr = %u, total=%u, fragsize = %u, n_frags = %u\n", n, bpos, (unsigned) info.ptr, total, u->in_fragment_size, u->in_fragments); */
344 return pa_bytes_to_usec(n
, &s
->sample_spec
);
347 static int sink_get_hw_volume(pa_sink
*s
) {
348 struct userdata
*u
= s
->userdata
;
350 if (pa_oss_get_pcm_volume(u
->fd
, &s
->sample_spec
, &s
->hw_volume
) < 0) {
351 pa_log_info("device doesn't support reading mixer settings: %s", pa_cstrerror(errno
));
352 s
->get_hw_volume
= NULL
;
359 static int sink_set_hw_volume(pa_sink
*s
) {
360 struct userdata
*u
= s
->userdata
;
362 if (pa_oss_set_pcm_volume(u
->fd
, &s
->sample_spec
, &s
->hw_volume
) < 0) {
363 pa_log_info("device doesn't support writing mixer settings: %s", pa_cstrerror(errno
));
364 s
->set_hw_volume
= NULL
;
371 static int source_get_hw_volume(pa_source
*s
) {
372 struct userdata
*u
= s
->userdata
;
374 if (pa_oss_get_input_volume(u
->fd
, &s
->sample_spec
, &s
->hw_volume
) < 0) {
375 pa_log_info("device doesn't support reading mixer settings: %s", pa_cstrerror(errno
));
376 s
->get_hw_volume
= NULL
;
383 static int source_set_hw_volume(pa_source
*s
) {
384 struct userdata
*u
= s
->userdata
;
386 if (pa_oss_set_input_volume(u
->fd
, &s
->sample_spec
, &s
->hw_volume
) < 0) {
387 pa_log_info("device doesn't support writing mixer settings: %s", pa_cstrerror(errno
));
388 s
->set_hw_volume
= NULL
;
395 int pa__init(pa_core
*c
, pa_module
*m
) {
396 struct audio_buf_info info
;
397 struct userdata
*u
= NULL
;
399 int nfrags
, frag_size
;
401 int enable_bits
= 0, zero
= 0;
402 int playback
= 1, record
= 1;
403 pa_modargs
*ma
= NULL
;
407 char *name_buf
= NULL
;
413 m
->userdata
= u
= pa_xnew0(struct userdata
, 1);
418 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
419 pa_log("failed to parse module arguments.");
423 if (pa_modargs_get_value_boolean(ma
, "record", &record
) < 0 || pa_modargs_get_value_boolean(ma
, "playback", &playback
) < 0) {
424 pa_log("record= and playback= expect numeric arguments.");
428 if (!playback
&& !record
) {
429 pa_log("neither playback nor record enabled for device.");
433 mode
= (playback
&&record
) ? O_RDWR
: (playback
? O_WRONLY
: (record
? O_RDONLY
: 0));
435 nfrags
= DEFAULT_NFRAGS
;
436 frag_size
= DEFAULT_FRAGSIZE
;
437 if (pa_modargs_get_value_s32(ma
, "fragments", &nfrags
) < 0 || pa_modargs_get_value_s32(ma
, "fragment_size", &frag_size
) < 0) {
438 pa_log("failed to parse fragments arguments");
442 u
->sample_spec
= c
->default_sample_spec
;
443 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &u
->sample_spec
, &map
, PA_CHANNEL_MAP_OSS
) < 0) {
444 pa_log("failed to parse sample specification or channel map");
448 if ((u
->fd
= pa_oss_open(p
= pa_modargs_get_value(ma
, "device", DEFAULT_DEVICE
), &mode
, &caps
)) < 0)
451 if (!(caps
& DSP_CAP_MMAP
) || !(caps
& DSP_CAP_TRIGGER
)) {
452 pa_log("OSS device not mmap capable.");
456 pa_log_info("device opened in %s mode.", mode
== O_WRONLY
? "O_WRONLY" : (mode
== O_RDONLY
? "O_RDONLY" : "O_RDWR"));
458 if (pa_oss_get_hw_description(p
, hwdesc
, sizeof(hwdesc
)) >= 0)
459 pa_log_info("hardware name is '%s'.", hwdesc
);
463 if (nfrags
>= 2 && frag_size
>= 1)
464 if (pa_oss_set_fragments(u
->fd
, nfrags
, frag_size
) < 0)
467 if (pa_oss_auto_format(u
->fd
, &u
->sample_spec
) < 0)
470 if (mode
!= O_WRONLY
) {
471 if (ioctl(u
->fd
, SNDCTL_DSP_GETISPACE
, &info
) < 0) {
472 pa_log("SNDCTL_DSP_GETISPACE: %s", pa_cstrerror(errno
));
476 pa_log_info("input -- %u fragments of size %u.", info
.fragstotal
, info
.fragsize
);
477 u
->in_mmap_length
= (u
->in_fragment_size
= info
.fragsize
) * (u
->in_fragments
= info
.fragstotal
);
479 if ((u
->in_mmap
= mmap(NULL
, u
->in_mmap_length
, PROT_READ
, MAP_SHARED
, u
->fd
, 0)) == MAP_FAILED
) {
480 if (mode
== O_RDWR
) {
481 pa_log("mmap failed for input. Changing to O_WRONLY mode.");
484 pa_log("mmap(): %s", pa_cstrerror(errno
));
488 if ((name
= pa_modargs_get_value(ma
, "source_name", NULL
)))
491 name
= name_buf
= pa_sprintf_malloc("oss_input.%s", pa_path_get_filename(p
));
495 if (!(u
->source
= pa_source_new(c
, __FILE__
, name
, namereg_fail
, &u
->sample_spec
, &map
)))
498 u
->source
->userdata
= u
;
499 u
->source
->get_latency
= source_get_latency_cb
;
500 u
->source
->get_hw_volume
= source_get_hw_volume
;
501 u
->source
->set_hw_volume
= source_set_hw_volume
;
502 pa_source_set_owner(u
->source
, m
);
503 pa_source_set_description(u
->source
, t
= pa_sprintf_malloc("OSS PCM/mmap() on %s%s%s%s",
505 hwdesc
[0] ? " (" : "",
506 hwdesc
[0] ? hwdesc
: "",
507 hwdesc
[0] ? ")" : ""));
509 u
->source
->is_hardware
= 1;
511 u
->in_memblocks
= pa_xnew0(pa_memblock
*, u
->in_fragments
);
513 enable_bits
|= PCM_ENABLE_INPUT
;
520 if (mode
!= O_RDONLY
) {
521 if (ioctl(u
->fd
, SNDCTL_DSP_GETOSPACE
, &info
) < 0) {
522 pa_log("SNDCTL_DSP_GETOSPACE: %s", pa_cstrerror(errno
));
526 pa_log_info("output -- %u fragments of size %u.", info
.fragstotal
, info
.fragsize
);
527 u
->out_mmap_length
= (u
->out_fragment_size
= info
.fragsize
) * (u
->out_fragments
= info
.fragstotal
);
529 if ((u
->out_mmap
= mmap(NULL
, u
->out_mmap_length
, PROT_WRITE
, MAP_SHARED
, u
->fd
, 0)) == MAP_FAILED
) {
530 if (mode
== O_RDWR
) {
531 pa_log("mmap filed for output. Changing to O_RDONLY mode.");
534 pa_log("mmap(): %s", pa_cstrerror(errno
));
538 pa_silence_memory(u
->out_mmap
, u
->out_mmap_length
, &u
->sample_spec
);
540 if ((name
= pa_modargs_get_value(ma
, "sink_name", NULL
)))
543 name
= name_buf
= pa_sprintf_malloc("oss_output.%s", pa_path_get_filename(p
));
547 if (!(u
->sink
= pa_sink_new(c
, __FILE__
, name
, namereg_fail
, &u
->sample_spec
, &map
)))
550 u
->sink
->get_latency
= sink_get_latency_cb
;
551 u
->sink
->get_hw_volume
= sink_get_hw_volume
;
552 u
->sink
->set_hw_volume
= sink_set_hw_volume
;
553 u
->sink
->userdata
= u
;
554 pa_sink_set_owner(u
->sink
, m
);
555 pa_sink_set_description(u
->sink
, t
= pa_sprintf_malloc("OSS PCM/mmap() on %s%s%s%s",
557 hwdesc
[0] ? " (" : "",
558 hwdesc
[0] ? hwdesc
: "",
559 hwdesc
[0] ? ")" : ""));
562 u
->sink
->is_hardware
= 1;
563 u
->out_memblocks
= pa_xmalloc0(sizeof(struct memblock
*)*u
->out_fragments
);
565 enable_bits
|= PCM_ENABLE_OUTPUT
;
573 if (ioctl(u
->fd
, SNDCTL_DSP_SETTRIGGER
, &zero
) < 0) {
574 pa_log("SNDCTL_DSP_SETTRIGGER: %s", pa_cstrerror(errno
));
578 if (ioctl(u
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable_bits
) < 0) {
579 pa_log("SNDCTL_DSP_SETTRIGGER: %s", pa_cstrerror(errno
));
583 assert(u
->source
|| u
->sink
);
585 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
);
590 /* Read mixer settings */
592 source_get_hw_volume(u
->source
);
594 sink_get_hw_volume(u
->sink
);
609 void pa__done(pa_core
*c
, pa_module
*m
) {
615 if (!(u
= m
->userdata
))
620 if (u
->out_memblocks
) {
622 for (i
= 0; i
< u
->out_fragments
; i
++)
623 if (u
->out_memblocks
[i
])
624 pa_memblock_unref_fixed(u
->out_memblocks
[i
]);
625 pa_xfree(u
->out_memblocks
);
628 if (u
->in_memblocks
) {
630 for (i
= 0; i
< u
->in_fragments
; i
++)
631 if (u
->in_memblocks
[i
])
632 pa_memblock_unref_fixed(u
->in_memblocks
[i
]);
633 pa_xfree(u
->in_memblocks
);