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_sink_used_by(u
->sink
) : 0) +
115 (u
->source
? pa_source_used_by(u
->source
) : 0));
118 static void clear_up(struct userdata
*u
) {
122 pa_sink_disconnect(u
->sink
);
123 pa_sink_unref(u
->sink
);
128 pa_source_disconnect(u
->source
);
129 pa_source_unref(u
->source
);
133 if (u
->in_mmap
&& u
->in_mmap
!= MAP_FAILED
) {
134 munmap(u
->in_mmap
, u
->in_mmap_length
);
138 if (u
->out_mmap
&& u
->out_mmap
!= MAP_FAILED
) {
139 munmap(u
->out_mmap
, u
->out_mmap_length
);
144 u
->core
->mainloop
->io_free(u
->io_event
);
154 static void out_fill_memblocks(struct userdata
*u
, unsigned n
) {
155 assert(u
&& u
->out_memblocks
);
160 if (u
->out_memblocks
[u
->out_current
])
161 pa_memblock_unref_fixed(u
->out_memblocks
[u
->out_current
]);
163 chunk
.memblock
= u
->out_memblocks
[u
->out_current
] =
164 pa_memblock_new_fixed(
166 (uint8_t*) u
->out_mmap
+u
->out_fragment_size
*u
->out_current
,
167 u
->out_fragment_size
,
169 assert(chunk
.memblock
);
170 chunk
.length
= chunk
.memblock
->length
;
173 pa_sink_render_into_full(u
->sink
, &chunk
);
176 while (u
->out_current
>= u
->out_fragments
)
177 u
->out_current
-= u
->out_fragments
;
183 static void do_write(struct userdata
*u
) {
184 struct count_info info
;
185 assert(u
&& u
->sink
);
189 if (ioctl(u
->fd
, SNDCTL_DSP_GETOPTR
, &info
) < 0) {
190 pa_log(__FILE__
": SNDCTL_DSP_GETOPTR: %s", pa_cstrerror(errno
));
193 pa_module_unload_request(u
->module
);
197 info
.blocks
+= u
->out_blocks_saved
;
198 u
->out_blocks_saved
= 0;
203 out_fill_memblocks(u
, info
.blocks
);
206 static void in_post_memblocks(struct userdata
*u
, unsigned n
) {
207 assert(u
&& u
->in_memblocks
);
212 if (!u
->in_memblocks
[u
->in_current
]) {
213 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);
214 chunk
.length
= chunk
.memblock
->length
;
217 pa_source_post(u
->source
, &chunk
);
221 while (u
->in_current
>= u
->in_fragments
)
222 u
->in_current
-= u
->in_fragments
;
228 static void in_clear_memblocks(struct userdata
*u
, unsigned n
) {
229 unsigned i
= u
->in_current
;
230 assert(u
&& u
->in_memblocks
);
232 if (n
> u
->in_fragments
)
236 if (u
->in_memblocks
[i
]) {
237 pa_memblock_unref_fixed(u
->in_memblocks
[i
]);
238 u
->in_memblocks
[i
] = NULL
;
242 while (i
>= u
->in_fragments
)
243 i
-= u
->in_fragments
;
249 static void do_read(struct userdata
*u
) {
250 struct count_info info
;
251 assert(u
&& u
->source
);
255 if (ioctl(u
->fd
, SNDCTL_DSP_GETIPTR
, &info
) < 0) {
256 pa_log(__FILE__
": SNDCTL_DSP_GETIPTR: %s", pa_cstrerror(errno
));
259 pa_module_unload_request(u
->module
);
263 info
.blocks
+= u
->in_blocks_saved
;
264 u
->in_blocks_saved
= 0;
269 in_post_memblocks(u
, info
.blocks
);
270 in_clear_memblocks(u
, u
->in_fragments
/2);
273 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
) {
274 struct userdata
*u
= userdata
;
275 assert (u
&& u
->core
->mainloop
== m
&& u
->io_event
== e
);
277 if (f
& PA_IO_EVENT_ERROR
) {
279 pa_module_unload_request(u
->module
);
283 if (f
& PA_IO_EVENT_INPUT
)
285 if (f
& PA_IO_EVENT_OUTPUT
)
289 static pa_usec_t
sink_get_latency_cb(pa_sink
*s
) {
290 struct userdata
*u
= s
->userdata
;
291 struct count_info info
;
292 size_t bpos
, n
, total
;
295 if (ioctl(u
->fd
, SNDCTL_DSP_GETOPTR
, &info
) < 0) {
296 pa_log(__FILE__
": SNDCTL_DSP_GETOPTR: %s", pa_cstrerror(errno
));
300 u
->out_blocks_saved
+= info
.blocks
;
302 total
= u
->out_fragments
* u
->out_fragment_size
;
303 bpos
= ((u
->out_current
+ u
->out_blocks_saved
) * u
->out_fragment_size
) % total
;
305 if (bpos
<= (size_t) info
.ptr
)
306 n
= total
- (info
.ptr
- bpos
);
310 /* 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); */
312 return pa_bytes_to_usec(n
, &s
->sample_spec
);
315 static pa_usec_t
source_get_latency_cb(pa_source
*s
) {
316 struct userdata
*u
= s
->userdata
;
317 struct count_info info
;
318 size_t bpos
, n
, total
;
321 if (ioctl(u
->fd
, SNDCTL_DSP_GETIPTR
, &info
) < 0) {
322 pa_log(__FILE__
": SNDCTL_DSP_GETIPTR: %s", pa_cstrerror(errno
));
326 u
->in_blocks_saved
+= info
.blocks
;
328 total
= u
->in_fragments
* u
->in_fragment_size
;
329 bpos
= ((u
->in_current
+ u
->in_blocks_saved
) * u
->in_fragment_size
) % total
;
331 if (bpos
<= (size_t) info
.ptr
)
334 n
= (u
->in_fragments
* u
->in_fragment_size
) - bpos
+ info
.ptr
;
336 /* 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); */
338 return pa_bytes_to_usec(n
, &s
->sample_spec
);
341 static int sink_get_hw_volume(pa_sink
*s
) {
342 struct userdata
*u
= s
->userdata
;
344 if (pa_oss_get_pcm_volume(u
->fd
, &s
->sample_spec
, &s
->hw_volume
) < 0) {
345 pa_log_info(__FILE__
": device doesn't support reading mixer settings: %s", pa_cstrerror(errno
));
346 s
->get_hw_volume
= NULL
;
353 static int sink_set_hw_volume(pa_sink
*s
) {
354 struct userdata
*u
= s
->userdata
;
356 if (pa_oss_set_pcm_volume(u
->fd
, &s
->sample_spec
, &s
->hw_volume
) < 0) {
357 pa_log_info(__FILE__
": device doesn't support writing mixer settings: %s", pa_cstrerror(errno
));
358 s
->set_hw_volume
= NULL
;
365 static int source_get_hw_volume(pa_source
*s
) {
366 struct userdata
*u
= s
->userdata
;
368 if (pa_oss_get_input_volume(u
->fd
, &s
->sample_spec
, &s
->hw_volume
) < 0) {
369 pa_log_info(__FILE__
": device doesn't support reading mixer settings: %s", pa_cstrerror(errno
));
370 s
->get_hw_volume
= NULL
;
377 static int source_set_hw_volume(pa_source
*s
) {
378 struct userdata
*u
= s
->userdata
;
380 if (pa_oss_set_input_volume(u
->fd
, &s
->sample_spec
, &s
->hw_volume
) < 0) {
381 pa_log_info(__FILE__
": device doesn't support writing mixer settings: %s", pa_cstrerror(errno
));
382 s
->set_hw_volume
= NULL
;
389 int pa__init(pa_core
*c
, pa_module
*m
) {
390 struct audio_buf_info info
;
391 struct userdata
*u
= NULL
;
393 int nfrags
, frag_size
;
395 int enable_bits
= 0, zero
= 0;
396 int playback
= 1, record
= 1;
397 pa_modargs
*ma
= NULL
;
401 char *name_buf
= NULL
;
407 m
->userdata
= u
= pa_xnew0(struct userdata
, 1);
412 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
413 pa_log(__FILE__
": failed to parse module arguments.");
417 if (pa_modargs_get_value_boolean(ma
, "record", &record
) < 0 || pa_modargs_get_value_boolean(ma
, "playback", &playback
) < 0) {
418 pa_log(__FILE__
": record= and playback= expect numeric arguments.");
422 if (!playback
&& !record
) {
423 pa_log(__FILE__
": neither playback nor record enabled for device.");
427 mode
= (playback
&&record
) ? O_RDWR
: (playback
? O_WRONLY
: (record
? O_RDONLY
: 0));
429 nfrags
= DEFAULT_NFRAGS
;
430 frag_size
= DEFAULT_FRAGSIZE
;
431 if (pa_modargs_get_value_s32(ma
, "fragments", &nfrags
) < 0 || pa_modargs_get_value_s32(ma
, "fragment_size", &frag_size
) < 0) {
432 pa_log(__FILE__
": failed to parse fragments arguments");
436 u
->sample_spec
= c
->default_sample_spec
;
437 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &u
->sample_spec
, &map
, PA_CHANNEL_MAP_OSS
) < 0) {
438 pa_log(__FILE__
": failed to parse sample specification or channel map");
442 if ((u
->fd
= pa_oss_open(p
= pa_modargs_get_value(ma
, "device", DEFAULT_DEVICE
), &mode
, &caps
)) < 0)
445 if (!(caps
& DSP_CAP_MMAP
) || !(caps
& DSP_CAP_TRIGGER
)) {
446 pa_log(__FILE__
": OSS device not mmap capable.");
450 pa_log_info(__FILE__
": device opened in %s mode.", mode
== O_WRONLY
? "O_WRONLY" : (mode
== O_RDONLY
? "O_RDONLY" : "O_RDWR"));
452 if (pa_oss_get_hw_description(p
, hwdesc
, sizeof(hwdesc
)) >= 0)
453 pa_log_info(__FILE__
": hardware name is '%s'.", hwdesc
);
457 if (nfrags
>= 2 && frag_size
>= 1)
458 if (pa_oss_set_fragments(u
->fd
, nfrags
, frag_size
) < 0)
461 if (pa_oss_auto_format(u
->fd
, &u
->sample_spec
) < 0)
464 if (mode
!= O_WRONLY
) {
465 if (ioctl(u
->fd
, SNDCTL_DSP_GETISPACE
, &info
) < 0) {
466 pa_log(__FILE__
": SNDCTL_DSP_GETISPACE: %s", pa_cstrerror(errno
));
470 pa_log_info(__FILE__
": input -- %u fragments of size %u.", info
.fragstotal
, info
.fragsize
);
471 u
->in_mmap_length
= (u
->in_fragment_size
= info
.fragsize
) * (u
->in_fragments
= info
.fragstotal
);
473 if ((u
->in_mmap
= mmap(NULL
, u
->in_mmap_length
, PROT_READ
, MAP_SHARED
, u
->fd
, 0)) == MAP_FAILED
) {
474 if (mode
== O_RDWR
) {
475 pa_log(__FILE__
": mmap failed for input. Changing to O_WRONLY mode.");
478 pa_log(__FILE__
": mmap(): %s", pa_cstrerror(errno
));
482 if ((name
= pa_modargs_get_value(ma
, "source_name", NULL
)))
485 name
= name_buf
= pa_sprintf_malloc("oss_input.%s", pa_path_get_filename(p
));
489 if (!(u
->source
= pa_source_new(c
, __FILE__
, name
, namereg_fail
, &u
->sample_spec
, &map
)))
492 u
->source
->userdata
= u
;
493 u
->source
->get_latency
= source_get_latency_cb
;
494 u
->source
->get_hw_volume
= source_get_hw_volume
;
495 u
->source
->set_hw_volume
= source_set_hw_volume
;
496 pa_source_set_owner(u
->source
, m
);
497 pa_source_set_description(u
->source
, t
= pa_sprintf_malloc("OSS PCM/mmap() on %s%s%s%s",
499 hwdesc
[0] ? " (" : "",
500 hwdesc
[0] ? hwdesc
: "",
501 hwdesc
[0] ? ")" : ""));
503 u
->source
->is_hardware
= 1;
505 u
->in_memblocks
= pa_xnew0(pa_memblock
*, u
->in_fragments
);
507 enable_bits
|= PCM_ENABLE_INPUT
;
514 if (mode
!= O_RDONLY
) {
515 if (ioctl(u
->fd
, SNDCTL_DSP_GETOSPACE
, &info
) < 0) {
516 pa_log(__FILE__
": SNDCTL_DSP_GETOSPACE: %s", pa_cstrerror(errno
));
520 pa_log_info(__FILE__
": output -- %u fragments of size %u.", info
.fragstotal
, info
.fragsize
);
521 u
->out_mmap_length
= (u
->out_fragment_size
= info
.fragsize
) * (u
->out_fragments
= info
.fragstotal
);
523 if ((u
->out_mmap
= mmap(NULL
, u
->out_mmap_length
, PROT_WRITE
, MAP_SHARED
, u
->fd
, 0)) == MAP_FAILED
) {
524 if (mode
== O_RDWR
) {
525 pa_log(__FILE__
": mmap filed for output. Changing to O_RDONLY mode.");
528 pa_log(__FILE__
": mmap(): %s", pa_cstrerror(errno
));
532 pa_silence_memory(u
->out_mmap
, u
->out_mmap_length
, &u
->sample_spec
);
534 if ((name
= pa_modargs_get_value(ma
, "sink_name", NULL
)))
537 name
= name_buf
= pa_sprintf_malloc("oss_output.%s", pa_path_get_filename(p
));
541 if (!(u
->sink
= pa_sink_new(c
, __FILE__
, name
, namereg_fail
, &u
->sample_spec
, &map
)))
544 u
->sink
->get_latency
= sink_get_latency_cb
;
545 u
->sink
->get_hw_volume
= sink_get_hw_volume
;
546 u
->sink
->set_hw_volume
= sink_set_hw_volume
;
547 u
->sink
->userdata
= u
;
548 pa_sink_set_owner(u
->sink
, m
);
549 pa_sink_set_description(u
->sink
, t
= pa_sprintf_malloc("OSS PCM/mmap() on %s%s%s%s",
551 hwdesc
[0] ? " (" : "",
552 hwdesc
[0] ? hwdesc
: "",
553 hwdesc
[0] ? ")" : ""));
556 u
->sink
->is_hardware
= 1;
557 u
->out_memblocks
= pa_xmalloc0(sizeof(struct memblock
*)*u
->out_fragments
);
559 enable_bits
|= PCM_ENABLE_OUTPUT
;
567 if (ioctl(u
->fd
, SNDCTL_DSP_SETTRIGGER
, &zero
) < 0) {
568 pa_log(__FILE__
": SNDCTL_DSP_SETTRIGGER: %s", pa_cstrerror(errno
));
572 if (ioctl(u
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable_bits
) < 0) {
573 pa_log(__FILE__
": SNDCTL_DSP_SETTRIGGER: %s", pa_cstrerror(errno
));
577 assert(u
->source
|| u
->sink
);
579 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
);
584 /* Read mixer settings */
586 source_get_hw_volume(u
->source
);
588 sink_get_hw_volume(u
->sink
);
603 void pa__done(pa_core
*c
, pa_module
*m
) {
609 if (!(u
= m
->userdata
))
614 if (u
->out_memblocks
) {
616 for (i
= 0; i
< u
->out_fragments
; i
++)
617 if (u
->out_memblocks
[i
])
618 pa_memblock_unref_fixed(u
->out_memblocks
[i
]);
619 pa_xfree(u
->out_memblocks
);
622 if (u
->in_memblocks
) {
624 for (i
= 0; i
< u
->in_fragments
; i
++)
625 if (u
->in_memblocks
[i
])
626 pa_memblock_unref_fixed(u
->in_memblocks
[i
]);
627 pa_xfree(u
->in_memblocks
);