4 This file is part of PulseAudio.
6 PulseAudio 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 PulseAudio 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 PulseAudio; 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 <pulse/xmalloc.h>
40 #include <pulse/util.h>
42 #include <pulsecore/core-error.h>
43 #include <pulsecore/iochannel.h>
44 #include <pulsecore/sink.h>
45 #include <pulsecore/source.h>
46 #include <pulsecore/module.h>
47 #include <pulsecore/sample-util.h>
48 #include <pulsecore/core-util.h>
49 #include <pulsecore/modargs.h>
50 #include <pulsecore/log.h>
53 #include "module-oss-mmap-symdef.h"
55 PA_MODULE_AUTHOR("Lennart Poettering")
56 PA_MODULE_DESCRIPTION("OSS Sink/Source (mmap)")
57 PA_MODULE_VERSION(PACKAGE_VERSION
)
59 "sink_name=<name for the sink> "
60 "source_name=<name for the source> "
61 "device=<OSS device> "
62 "record=<enable source?> "
63 "playback=<enable sink?> "
64 "format=<sample format> "
65 "channels=<number of channels> "
67 "fragments=<number of fragments> "
68 "fragment_size=<fragment size> "
69 "channel_map=<channel map>")
75 pa_sample_spec sample_spec
;
77 size_t in_fragment_size
, out_fragment_size
;
78 unsigned in_fragments
, out_fragments
;
79 unsigned out_blocks_saved
, in_blocks_saved
;
83 void *in_mmap
, *out_mmap
;
84 size_t in_mmap_length
, out_mmap_length
;
86 pa_io_event
*io_event
;
88 pa_memblock
**in_memblocks
, **out_memblocks
;
89 unsigned out_current
, in_current
;
93 static const char* const valid_modargs
[] = {
108 #define DEFAULT_DEVICE "/dev/dsp"
109 #define DEFAULT_NFRAGS 12
110 #define DEFAULT_FRAGSIZE 1024
112 static void update_usage(struct userdata
*u
) {
113 pa_module_set_used(u
->module
,
114 (u
->sink
? pa_idxset_size(u
->sink
->inputs
) : 0) +
115 (u
->sink
? pa_idxset_size(u
->sink
->monitor_source
->outputs
) : 0) +
116 (u
->source
? pa_idxset_size(u
->source
->outputs
) : 0));
119 static void clear_up(struct userdata
*u
) {
123 pa_sink_disconnect(u
->sink
);
124 pa_sink_unref(u
->sink
);
129 pa_source_disconnect(u
->source
);
130 pa_source_unref(u
->source
);
134 if (u
->in_mmap
&& u
->in_mmap
!= MAP_FAILED
) {
135 munmap(u
->in_mmap
, u
->in_mmap_length
);
139 if (u
->out_mmap
&& u
->out_mmap
!= MAP_FAILED
) {
140 munmap(u
->out_mmap
, u
->out_mmap_length
);
145 u
->core
->mainloop
->io_free(u
->io_event
);
155 static void out_fill_memblocks(struct userdata
*u
, unsigned n
) {
156 assert(u
&& u
->out_memblocks
);
161 if (u
->out_memblocks
[u
->out_current
])
162 pa_memblock_unref_fixed(u
->out_memblocks
[u
->out_current
]);
164 chunk
.memblock
= u
->out_memblocks
[u
->out_current
] =
165 pa_memblock_new_fixed(
166 (uint8_t*) u
->out_mmap
+u
->out_fragment_size
*u
->out_current
,
167 u
->out_fragment_size
,
169 u
->core
->memblock_stat
);
170 assert(chunk
.memblock
);
171 chunk
.length
= chunk
.memblock
->length
;
174 pa_sink_render_into_full(u
->sink
, &chunk
);
177 while (u
->out_current
>= u
->out_fragments
)
178 u
->out_current
-= u
->out_fragments
;
184 static void do_write(struct userdata
*u
) {
185 struct count_info info
;
186 assert(u
&& u
->sink
);
190 if (ioctl(u
->fd
, SNDCTL_DSP_GETOPTR
, &info
) < 0) {
191 pa_log(__FILE__
": SNDCTL_DSP_GETOPTR: %s", pa_cstrerror(errno
));
194 pa_module_unload_request(u
->module
);
198 info
.blocks
+= u
->out_blocks_saved
;
199 u
->out_blocks_saved
= 0;
204 out_fill_memblocks(u
, info
.blocks
);
207 static void in_post_memblocks(struct userdata
*u
, unsigned n
) {
208 assert(u
&& u
->in_memblocks
);
213 if (!u
->in_memblocks
[u
->in_current
]) {
214 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
);
215 chunk
.length
= chunk
.memblock
->length
;
218 pa_source_post(u
->source
, &chunk
);
222 while (u
->in_current
>= u
->in_fragments
)
223 u
->in_current
-= u
->in_fragments
;
229 static void in_clear_memblocks(struct userdata
*u
, unsigned n
) {
230 unsigned i
= u
->in_current
;
231 assert(u
&& u
->in_memblocks
);
233 if (n
> u
->in_fragments
)
237 if (u
->in_memblocks
[i
]) {
238 pa_memblock_unref_fixed(u
->in_memblocks
[i
]);
239 u
->in_memblocks
[i
] = NULL
;
243 while (i
>= u
->in_fragments
)
244 i
-= u
->in_fragments
;
250 static void do_read(struct userdata
*u
) {
251 struct count_info info
;
252 assert(u
&& u
->source
);
256 if (ioctl(u
->fd
, SNDCTL_DSP_GETIPTR
, &info
) < 0) {
257 pa_log(__FILE__
": SNDCTL_DSP_GETIPTR: %s", pa_cstrerror(errno
));
260 pa_module_unload_request(u
->module
);
264 info
.blocks
+= u
->in_blocks_saved
;
265 u
->in_blocks_saved
= 0;
270 in_post_memblocks(u
, info
.blocks
);
271 in_clear_memblocks(u
, u
->in_fragments
/2);
274 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
) {
275 struct userdata
*u
= userdata
;
276 assert (u
&& u
->core
->mainloop
== m
&& u
->io_event
== e
);
278 if (f
& PA_IO_EVENT_ERROR
) {
280 pa_module_unload_request(u
->module
);
284 if (f
& PA_IO_EVENT_INPUT
)
286 if (f
& PA_IO_EVENT_OUTPUT
)
290 static pa_usec_t
sink_get_latency_cb(pa_sink
*s
) {
291 struct userdata
*u
= s
->userdata
;
292 struct count_info info
;
293 size_t bpos
, n
, total
;
296 if (ioctl(u
->fd
, SNDCTL_DSP_GETOPTR
, &info
) < 0) {
297 pa_log(__FILE__
": SNDCTL_DSP_GETOPTR: %s", pa_cstrerror(errno
));
301 u
->out_blocks_saved
+= info
.blocks
;
303 total
= u
->out_fragments
* u
->out_fragment_size
;
304 bpos
= ((u
->out_current
+ u
->out_blocks_saved
) * u
->out_fragment_size
) % total
;
306 if (bpos
<= (size_t) info
.ptr
)
307 n
= total
- (info
.ptr
- bpos
);
311 /* 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); */
313 return pa_bytes_to_usec(n
, &s
->sample_spec
);
316 static pa_usec_t
source_get_latency_cb(pa_source
*s
) {
317 struct userdata
*u
= s
->userdata
;
318 struct count_info info
;
319 size_t bpos
, n
, total
;
322 if (ioctl(u
->fd
, SNDCTL_DSP_GETIPTR
, &info
) < 0) {
323 pa_log(__FILE__
": SNDCTL_DSP_GETIPTR: %s", pa_cstrerror(errno
));
327 u
->in_blocks_saved
+= info
.blocks
;
329 total
= u
->in_fragments
* u
->in_fragment_size
;
330 bpos
= ((u
->in_current
+ u
->in_blocks_saved
) * u
->in_fragment_size
) % total
;
332 if (bpos
<= (size_t) info
.ptr
)
335 n
= (u
->in_fragments
* u
->in_fragment_size
) - bpos
+ info
.ptr
;
337 /* 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); */
339 return pa_bytes_to_usec(n
, &s
->sample_spec
);
342 static int sink_get_hw_volume(pa_sink
*s
) {
343 struct userdata
*u
= s
->userdata
;
345 if (pa_oss_get_pcm_volume(u
->fd
, &s
->sample_spec
, &s
->hw_volume
) < 0) {
346 pa_log_info(__FILE__
": device doesn't support reading mixer settings: %s", pa_cstrerror(errno
));
347 s
->get_hw_volume
= NULL
;
354 static int sink_set_hw_volume(pa_sink
*s
) {
355 struct userdata
*u
= s
->userdata
;
357 if (pa_oss_set_pcm_volume(u
->fd
, &s
->sample_spec
, &s
->hw_volume
) < 0) {
358 pa_log_info(__FILE__
": device doesn't support writing mixer settings: %s", pa_cstrerror(errno
));
359 s
->set_hw_volume
= NULL
;
366 static int source_get_hw_volume(pa_source
*s
) {
367 struct userdata
*u
= s
->userdata
;
369 if (pa_oss_get_input_volume(u
->fd
, &s
->sample_spec
, &s
->hw_volume
) < 0) {
370 pa_log_info(__FILE__
": device doesn't support reading mixer settings: %s", pa_cstrerror(errno
));
371 s
->get_hw_volume
= NULL
;
378 static int source_set_hw_volume(pa_source
*s
) {
379 struct userdata
*u
= s
->userdata
;
381 if (pa_oss_set_input_volume(u
->fd
, &s
->sample_spec
, &s
->hw_volume
) < 0) {
382 pa_log_info(__FILE__
": device doesn't support writing mixer settings: %s", pa_cstrerror(errno
));
383 s
->set_hw_volume
= NULL
;
390 int pa__init(pa_core
*c
, pa_module
*m
) {
391 struct audio_buf_info info
;
392 struct userdata
*u
= NULL
;
394 int nfrags
, frag_size
;
396 int enable_bits
= 0, zero
= 0;
397 int playback
= 1, record
= 1;
398 pa_modargs
*ma
= NULL
;
402 char *name_buf
= NULL
;
408 m
->userdata
= u
= pa_xnew0(struct userdata
, 1);
413 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
414 pa_log(__FILE__
": failed to parse module arguments.");
418 if (pa_modargs_get_value_boolean(ma
, "record", &record
) < 0 || pa_modargs_get_value_boolean(ma
, "playback", &playback
) < 0) {
419 pa_log(__FILE__
": record= and playback= expect numeric arguments.");
423 if (!playback
&& !record
) {
424 pa_log(__FILE__
": neither playback nor record enabled for device.");
428 mode
= (playback
&&record
) ? O_RDWR
: (playback
? O_WRONLY
: (record
? O_RDONLY
: 0));
430 nfrags
= DEFAULT_NFRAGS
;
431 frag_size
= DEFAULT_FRAGSIZE
;
432 if (pa_modargs_get_value_s32(ma
, "fragments", &nfrags
) < 0 || pa_modargs_get_value_s32(ma
, "fragment_size", &frag_size
) < 0) {
433 pa_log(__FILE__
": failed to parse fragments arguments");
437 u
->sample_spec
= c
->default_sample_spec
;
438 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &u
->sample_spec
, &map
, PA_CHANNEL_MAP_OSS
) < 0) {
439 pa_log(__FILE__
": failed to parse sample specification or channel map");
443 if ((u
->fd
= pa_oss_open(p
= pa_modargs_get_value(ma
, "device", DEFAULT_DEVICE
), &mode
, &caps
)) < 0)
446 if (!(caps
& DSP_CAP_MMAP
) || !(caps
& DSP_CAP_TRIGGER
)) {
447 pa_log(__FILE__
": OSS device not mmap capable.");
451 pa_log_info(__FILE__
": device opened in %s mode.", mode
== O_WRONLY
? "O_WRONLY" : (mode
== O_RDONLY
? "O_RDONLY" : "O_RDWR"));
453 if (pa_oss_get_hw_description(p
, hwdesc
, sizeof(hwdesc
)) >= 0)
454 pa_log_info(__FILE__
": hardware name is '%s'.", hwdesc
);
458 if (nfrags
>= 2 && frag_size
>= 1)
459 if (pa_oss_set_fragments(u
->fd
, nfrags
, frag_size
) < 0)
462 if (pa_oss_auto_format(u
->fd
, &u
->sample_spec
) < 0)
465 if (mode
!= O_WRONLY
) {
466 if (ioctl(u
->fd
, SNDCTL_DSP_GETISPACE
, &info
) < 0) {
467 pa_log(__FILE__
": SNDCTL_DSP_GETISPACE: %s", pa_cstrerror(errno
));
471 pa_log_info(__FILE__
": input -- %u fragments of size %u.", info
.fragstotal
, info
.fragsize
);
472 u
->in_mmap_length
= (u
->in_fragment_size
= info
.fragsize
) * (u
->in_fragments
= info
.fragstotal
);
474 if ((u
->in_mmap
= mmap(NULL
, u
->in_mmap_length
, PROT_READ
, MAP_SHARED
, u
->fd
, 0)) == MAP_FAILED
) {
475 if (mode
== O_RDWR
) {
476 pa_log(__FILE__
": mmap failed for input. Changing to O_WRONLY mode.");
479 pa_log(__FILE__
": mmap(): %s", pa_cstrerror(errno
));
483 if ((name
= pa_modargs_get_value(ma
, "source_name", NULL
)))
486 name
= name_buf
= pa_sprintf_malloc("oss_input.%s", pa_path_get_filename(p
));
490 if (!(u
->source
= pa_source_new(c
, __FILE__
, name
, namereg_fail
, &u
->sample_spec
, &map
)))
493 u
->source
->userdata
= u
;
494 u
->source
->get_latency
= source_get_latency_cb
;
495 u
->source
->get_hw_volume
= source_get_hw_volume
;
496 u
->source
->set_hw_volume
= source_set_hw_volume
;
497 pa_source_set_owner(u
->source
, m
);
498 pa_source_set_description(u
->source
, t
= pa_sprintf_malloc("OSS PCM/mmap() on %s%s%s%s",
500 hwdesc
[0] ? " (" : "",
501 hwdesc
[0] ? hwdesc
: "",
502 hwdesc
[0] ? ")" : ""));
504 u
->source
->is_hardware
= 1;
506 u
->in_memblocks
= pa_xnew0(pa_memblock
*, u
->in_fragments
);
508 enable_bits
|= PCM_ENABLE_INPUT
;
515 if (mode
!= O_RDONLY
) {
516 if (ioctl(u
->fd
, SNDCTL_DSP_GETOSPACE
, &info
) < 0) {
517 pa_log(__FILE__
": SNDCTL_DSP_GETOSPACE: %s", pa_cstrerror(errno
));
521 pa_log_info(__FILE__
": output -- %u fragments of size %u.", info
.fragstotal
, info
.fragsize
);
522 u
->out_mmap_length
= (u
->out_fragment_size
= info
.fragsize
) * (u
->out_fragments
= info
.fragstotal
);
524 if ((u
->out_mmap
= mmap(NULL
, u
->out_mmap_length
, PROT_WRITE
, MAP_SHARED
, u
->fd
, 0)) == MAP_FAILED
) {
525 if (mode
== O_RDWR
) {
526 pa_log(__FILE__
": mmap filed for output. Changing to O_RDONLY mode.");
529 pa_log(__FILE__
": mmap(): %s", pa_cstrerror(errno
));
533 pa_silence_memory(u
->out_mmap
, u
->out_mmap_length
, &u
->sample_spec
);
535 if ((name
= pa_modargs_get_value(ma
, "sink_name", NULL
)))
538 name
= name_buf
= pa_sprintf_malloc("oss_output.%s", pa_path_get_filename(p
));
542 if (!(u
->sink
= pa_sink_new(c
, __FILE__
, name
, namereg_fail
, &u
->sample_spec
, &map
)))
545 u
->sink
->get_latency
= sink_get_latency_cb
;
546 u
->sink
->get_hw_volume
= sink_get_hw_volume
;
547 u
->sink
->set_hw_volume
= sink_set_hw_volume
;
548 u
->sink
->userdata
= u
;
549 pa_sink_set_owner(u
->sink
, m
);
550 pa_sink_set_description(u
->sink
, t
= pa_sprintf_malloc("OSS PCM/mmap() on %s%s%s%s",
552 hwdesc
[0] ? " (" : "",
553 hwdesc
[0] ? hwdesc
: "",
554 hwdesc
[0] ? ")" : ""));
557 u
->sink
->is_hardware
= 1;
558 u
->out_memblocks
= pa_xmalloc0(sizeof(struct memblock
*)*u
->out_fragments
);
560 enable_bits
|= PCM_ENABLE_OUTPUT
;
568 if (ioctl(u
->fd
, SNDCTL_DSP_SETTRIGGER
, &zero
) < 0) {
569 pa_log(__FILE__
": SNDCTL_DSP_SETTRIGGER: %s", pa_cstrerror(errno
));
573 if (ioctl(u
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable_bits
) < 0) {
574 pa_log(__FILE__
": SNDCTL_DSP_SETTRIGGER: %s", pa_cstrerror(errno
));
578 assert(u
->source
|| u
->sink
);
580 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
);
585 /* Read mixer settings */
587 source_get_hw_volume(u
->source
);
589 sink_get_hw_volume(u
->sink
);
604 void pa__done(pa_core
*c
, pa_module
*m
) {
610 if (!(u
= m
->userdata
))
615 if (u
->out_memblocks
) {
617 for (i
= 0; i
< u
->out_fragments
; i
++)
618 if (u
->out_memblocks
[i
])
619 pa_memblock_unref_fixed(u
->out_memblocks
[i
]);
620 pa_xfree(u
->out_memblocks
);
623 if (u
->in_memblocks
) {
625 for (i
= 0; i
< u
->in_fragments
; i
++)
626 if (u
->in_memblocks
[i
])
627 pa_memblock_unref_fixed(u
->in_memblocks
[i
]);
628 pa_xfree(u
->in_memblocks
);