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 <polyp/error.h>
40 #include <polyp/xmalloc.h>
42 #include <polypcore/iochannel.h>
43 #include <polypcore/sink.h>
44 #include <polypcore/source.h>
45 #include <polypcore/module.h>
46 #include <polypcore/sample-util.h>
47 #include <polypcore/core-util.h>
48 #include <polypcore/modargs.h>
49 #include <polypcore/log.h>
52 #include "module-oss-mmap-symdef.h"
54 PA_MODULE_AUTHOR("Lennart Poettering")
55 PA_MODULE_DESCRIPTION("OSS Sink/Source (mmap)")
56 PA_MODULE_VERSION(PACKAGE_VERSION
)
58 "sink_name=<name for the sink> "
59 "source_name=<name for the source> "
60 "device=<OSS device> "
61 "record=<enable source?> "
62 "playback=<enable sink?> "
63 "format=<sample format> "
64 "channels=<number of channels> "
66 "fragments=<number of fragments> "
67 "fragment_size=<fragment size> "
68 "channel_map=<channel map>")
74 pa_sample_spec sample_spec
;
76 size_t in_fragment_size
, out_fragment_size
;
77 unsigned in_fragments
, out_fragments
;
78 unsigned out_blocks_saved
, in_blocks_saved
;
82 void *in_mmap
, *out_mmap
;
83 size_t in_mmap_length
, out_mmap_length
;
85 pa_io_event
*io_event
;
87 pa_memblock
**in_memblocks
, **out_memblocks
;
88 unsigned out_current
, in_current
;
92 static const char* const valid_modargs
[] = {
107 #define DEFAULT_SINK_NAME "oss_output"
108 #define DEFAULT_SOURCE_NAME "oss_input"
109 #define DEFAULT_DEVICE "/dev/dsp"
110 #define DEFAULT_NFRAGS 12
111 #define DEFAULT_FRAGSIZE 1024
113 static void update_usage(struct userdata
*u
) {
114 pa_module_set_used(u
->module
,
115 (u
->sink
? pa_idxset_size(u
->sink
->inputs
) : 0) +
116 (u
->sink
? pa_idxset_size(u
->sink
->monitor_source
->outputs
) : 0) +
117 (u
->source
? pa_idxset_size(u
->source
->outputs
) : 0));
120 static void out_fill_memblocks(struct userdata
*u
, unsigned n
) {
121 assert(u
&& u
->out_memblocks
);
126 if (u
->out_memblocks
[u
->out_current
])
127 pa_memblock_unref_fixed(u
->out_memblocks
[u
->out_current
]);
129 chunk
.memblock
= u
->out_memblocks
[u
->out_current
] =
130 pa_memblock_new_fixed(
131 (uint8_t*) u
->out_mmap
+u
->out_fragment_size
*u
->out_current
,
132 u
->out_fragment_size
,
134 u
->core
->memblock_stat
);
135 assert(chunk
.memblock
);
136 chunk
.length
= chunk
.memblock
->length
;
139 pa_sink_render_into_full(u
->sink
, &chunk
);
142 while (u
->out_current
>= u
->out_fragments
)
143 u
->out_current
-= u
->out_fragments
;
149 static void do_write(struct userdata
*u
) {
150 struct count_info info
;
151 assert(u
&& u
->sink
);
155 if (ioctl(u
->fd
, SNDCTL_DSP_GETOPTR
, &info
) < 0) {
156 pa_log(__FILE__
": SNDCTL_DSP_GETOPTR: %s", pa_cstrerror(errno
));
160 info
.blocks
+= u
->out_blocks_saved
;
161 u
->out_blocks_saved
= 0;
166 out_fill_memblocks(u
, info
.blocks
);
169 static void in_post_memblocks(struct userdata
*u
, unsigned n
) {
170 assert(u
&& u
->in_memblocks
);
175 if (!u
->in_memblocks
[u
->in_current
]) {
176 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
);
177 chunk
.length
= chunk
.memblock
->length
;
180 pa_source_post(u
->source
, &chunk
);
184 while (u
->in_current
>= u
->in_fragments
)
185 u
->in_current
-= u
->in_fragments
;
191 static void in_clear_memblocks(struct userdata
*u
, unsigned n
) {
192 unsigned i
= u
->in_current
;
193 assert(u
&& u
->in_memblocks
);
195 if (n
> u
->in_fragments
)
199 if (u
->in_memblocks
[i
]) {
200 pa_memblock_unref_fixed(u
->in_memblocks
[i
]);
201 u
->in_memblocks
[i
] = NULL
;
205 while (i
>= u
->in_fragments
)
206 i
-= u
->in_fragments
;
212 static void do_read(struct userdata
*u
) {
213 struct count_info info
;
214 assert(u
&& u
->source
);
218 if (ioctl(u
->fd
, SNDCTL_DSP_GETIPTR
, &info
) < 0) {
219 pa_log(__FILE__
": SNDCTL_DSP_GETIPTR: %s", pa_cstrerror(errno
));
223 info
.blocks
+= u
->in_blocks_saved
;
224 u
->in_blocks_saved
= 0;
229 in_post_memblocks(u
, info
.blocks
);
230 in_clear_memblocks(u
, u
->in_fragments
/2);
233 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
) {
234 struct userdata
*u
= userdata
;
235 assert (u
&& u
->core
->mainloop
== m
&& u
->io_event
== e
);
237 if (f
& PA_IO_EVENT_INPUT
)
239 if (f
& PA_IO_EVENT_OUTPUT
)
243 static pa_usec_t
sink_get_latency_cb(pa_sink
*s
) {
244 struct userdata
*u
= s
->userdata
;
245 struct count_info info
;
246 size_t bpos
, n
, total
;
249 if (ioctl(u
->fd
, SNDCTL_DSP_GETOPTR
, &info
) < 0) {
250 pa_log(__FILE__
": SNDCTL_DSP_GETOPTR: %s", pa_cstrerror(errno
));
254 u
->out_blocks_saved
+= info
.blocks
;
256 total
= u
->out_fragments
* u
->out_fragment_size
;
257 bpos
= ((u
->out_current
+ u
->out_blocks_saved
) * u
->out_fragment_size
) % total
;
259 if (bpos
<= (size_t) info
.ptr
)
260 n
= total
- (info
.ptr
- bpos
);
264 /* 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); */
266 return pa_bytes_to_usec(n
, &s
->sample_spec
);
269 static pa_usec_t
source_get_latency_cb(pa_source
*s
) {
270 struct userdata
*u
= s
->userdata
;
271 struct count_info info
;
272 size_t bpos
, n
, total
;
275 if (ioctl(u
->fd
, SNDCTL_DSP_GETIPTR
, &info
) < 0) {
276 pa_log(__FILE__
": SNDCTL_DSP_GETIPTR: %s", pa_cstrerror(errno
));
280 u
->in_blocks_saved
+= info
.blocks
;
282 total
= u
->in_fragments
* u
->in_fragment_size
;
283 bpos
= ((u
->in_current
+ u
->in_blocks_saved
) * u
->in_fragment_size
) % total
;
285 if (bpos
<= (size_t) info
.ptr
)
288 n
= (u
->in_fragments
* u
->in_fragment_size
) - bpos
+ info
.ptr
;
290 /* 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); */
292 return pa_bytes_to_usec(n
, &s
->sample_spec
);
295 static int sink_get_hw_volume(pa_sink
*s
) {
296 struct userdata
*u
= s
->userdata
;
298 if (pa_oss_get_pcm_volume(u
->fd
, &s
->sample_spec
, &s
->hw_volume
) < 0) {
299 pa_log_info(__FILE__
": device doesn't support reading mixer settings: %s", pa_cstrerror(errno
));
300 s
->get_hw_volume
= NULL
;
307 static int sink_set_hw_volume(pa_sink
*s
) {
308 struct userdata
*u
= s
->userdata
;
310 if (pa_oss_set_pcm_volume(u
->fd
, &s
->sample_spec
, &s
->hw_volume
) < 0) {
311 pa_log_info(__FILE__
": device doesn't support writing mixer settings: %s", pa_cstrerror(errno
));
312 s
->set_hw_volume
= NULL
;
319 static int source_get_hw_volume(pa_source
*s
) {
320 struct userdata
*u
= s
->userdata
;
322 if (pa_oss_get_input_volume(u
->fd
, &s
->sample_spec
, &s
->hw_volume
) < 0) {
323 pa_log_info(__FILE__
": device doesn't support reading mixer settings: %s", pa_cstrerror(errno
));
324 s
->get_hw_volume
= NULL
;
331 static int source_set_hw_volume(pa_source
*s
) {
332 struct userdata
*u
= s
->userdata
;
334 if (pa_oss_set_input_volume(u
->fd
, &s
->sample_spec
, &s
->hw_volume
) < 0) {
335 pa_log_info(__FILE__
": device doesn't support writing mixer settings: %s", pa_cstrerror(errno
));
336 s
->set_hw_volume
= NULL
;
343 int pa__init(pa_core
*c
, pa_module
*m
) {
344 struct audio_buf_info info
;
345 struct userdata
*u
= NULL
;
347 int nfrags
, frag_size
;
349 int enable_bits
= 0, zero
= 0;
350 int playback
= 1, record
= 1;
351 pa_modargs
*ma
= NULL
;
358 m
->userdata
= u
= pa_xnew0(struct userdata
, 1);
363 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
364 pa_log(__FILE__
": failed to parse module arguments.");
368 if (pa_modargs_get_value_boolean(ma
, "record", &record
) < 0 || pa_modargs_get_value_boolean(ma
, "playback", &playback
) < 0) {
369 pa_log(__FILE__
": record= and playback= expect numeric arguments.");
373 if (!playback
&& !record
) {
374 pa_log(__FILE__
": neither playback nor record enabled for device.");
378 mode
= (playback
&&record
) ? O_RDWR
: (playback
? O_WRONLY
: (record
? O_RDONLY
: 0));
380 nfrags
= DEFAULT_NFRAGS
;
381 frag_size
= DEFAULT_FRAGSIZE
;
382 if (pa_modargs_get_value_s32(ma
, "fragments", &nfrags
) < 0 || pa_modargs_get_value_s32(ma
, "fragment_size", &frag_size
) < 0) {
383 pa_log(__FILE__
": failed to parse fragments arguments");
387 u
->sample_spec
= c
->default_sample_spec
;
388 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &u
->sample_spec
, &map
, PA_CHANNEL_MAP_OSS
) < 0) {
389 pa_log(__FILE__
": failed to parse sample specification or channel map");
393 if ((u
->fd
= pa_oss_open(p
= pa_modargs_get_value(ma
, "device", DEFAULT_DEVICE
), &mode
, &caps
)) < 0)
396 if (!(caps
& DSP_CAP_MMAP
) || !(caps
& DSP_CAP_REALTIME
) || !(caps
& DSP_CAP_TRIGGER
)) {
397 pa_log(__FILE__
": OSS device not mmap capable.");
401 pa_log_info(__FILE__
": device opened in %s mode.", mode
== O_WRONLY
? "O_WRONLY" : (mode
== O_RDONLY
? "O_RDONLY" : "O_RDWR"));
403 if (pa_oss_get_hw_description(p
, hwdesc
, sizeof(hwdesc
)) >= 0)
404 pa_log_info(__FILE__
": hardware name is '%s'.", hwdesc
);
408 if (nfrags
>= 2 && frag_size
>= 1)
409 if (pa_oss_set_fragments(u
->fd
, nfrags
, frag_size
) < 0)
412 if (pa_oss_auto_format(u
->fd
, &u
->sample_spec
) < 0)
415 if (mode
!= O_WRONLY
) {
416 if (ioctl(u
->fd
, SNDCTL_DSP_GETISPACE
, &info
) < 0) {
417 pa_log(__FILE__
": SNDCTL_DSP_GETISPACE: %s", pa_cstrerror(errno
));
421 pa_log_info(__FILE__
": input -- %u fragments of size %u.", info
.fragstotal
, info
.fragsize
);
422 u
->in_mmap_length
= (u
->in_fragment_size
= info
.fragsize
) * (u
->in_fragments
= info
.fragstotal
);
424 if ((u
->in_mmap
= mmap(NULL
, u
->in_mmap_length
, PROT_READ
, MAP_SHARED
, u
->fd
, 0)) == MAP_FAILED
) {
425 if (mode
== O_RDWR
) {
426 pa_log(__FILE__
": mmap failed for input. Changing to O_WRONLY mode.");
429 pa_log(__FILE__
": mmap(): %s", pa_cstrerror(errno
));
434 if (!(u
->source
= pa_source_new(c
, __FILE__
, pa_modargs_get_value(ma
, "source_name", DEFAULT_SOURCE_NAME
), 0, &u
->sample_spec
, &map
)))
437 u
->source
->userdata
= u
;
438 u
->source
->get_latency
= source_get_latency_cb
;
439 u
->source
->get_hw_volume
= source_get_hw_volume
;
440 u
->source
->set_hw_volume
= source_set_hw_volume
;
441 pa_source_set_owner(u
->source
, m
);
442 u
->source
->description
= pa_sprintf_malloc("Open Sound System PCM/mmap() on '%s'%s%s%s",
444 hwdesc
[0] ? " (" : "",
445 hwdesc
[0] ? hwdesc
: "",
446 hwdesc
[0] ? ")" : "");
448 u
->in_memblocks
= pa_xnew0(pa_memblock
*, u
->in_fragments
);
450 enable_bits
|= PCM_ENABLE_INPUT
;
454 if (mode
!= O_RDONLY
) {
455 if (ioctl(u
->fd
, SNDCTL_DSP_GETOSPACE
, &info
) < 0) {
456 pa_log(__FILE__
": SNDCTL_DSP_GETOSPACE: %s", pa_cstrerror(errno
));
460 pa_log_info(__FILE__
": output -- %u fragments of size %u.", info
.fragstotal
, info
.fragsize
);
461 u
->out_mmap_length
= (u
->out_fragment_size
= info
.fragsize
) * (u
->out_fragments
= info
.fragstotal
);
463 if ((u
->out_mmap
= mmap(NULL
, u
->out_mmap_length
, PROT_WRITE
, MAP_SHARED
, u
->fd
, 0)) == MAP_FAILED
) {
464 if (mode
== O_RDWR
) {
465 pa_log(__FILE__
": mmap filed for output. Changing to O_RDONLY mode.");
468 pa_log(__FILE__
": mmap(): %s", pa_cstrerror(errno
));
472 pa_silence_memory(u
->out_mmap
, u
->out_mmap_length
, &u
->sample_spec
);
474 if (!(u
->sink
= pa_sink_new(c
, __FILE__
, pa_modargs_get_value(ma
, "sink_name", DEFAULT_SINK_NAME
), 0, &u
->sample_spec
, &map
)))
477 u
->sink
->get_latency
= sink_get_latency_cb
;
478 u
->sink
->get_hw_volume
= sink_get_hw_volume
;
479 u
->sink
->set_hw_volume
= sink_set_hw_volume
;
480 u
->sink
->userdata
= u
;
481 pa_sink_set_owner(u
->sink
, m
);
482 u
->sink
->description
= pa_sprintf_malloc("Open Sound System PCM/mmap() on '%s'%s%s%s",
484 hwdesc
[0] ? " (" : "",
485 hwdesc
[0] ? hwdesc
: "",
486 hwdesc
[0] ? ")" : "");
488 u
->out_memblocks
= pa_xmalloc0(sizeof(struct memblock
*)*u
->out_fragments
);
490 enable_bits
|= PCM_ENABLE_OUTPUT
;
495 if (ioctl(u
->fd
, SNDCTL_DSP_SETTRIGGER
, &zero
) < 0) {
496 pa_log(__FILE__
": SNDCTL_DSP_SETTRIGGER: %s", pa_cstrerror(errno
));
500 if (ioctl(u
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable_bits
) < 0) {
501 pa_log(__FILE__
": SNDCTL_DSP_SETTRIGGER: %s", pa_cstrerror(errno
));
505 assert(u
->source
|| u
->sink
);
507 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
);
512 /* Read mixer settings */
514 source_get_hw_volume(u
->source
);
516 sink_get_hw_volume(u
->sink
);
529 void pa__done(pa_core
*c
, pa_module
*m
) {
535 if (!(u
= m
->userdata
))
538 if (u
->out_memblocks
) {
540 for (i
= 0; i
< u
->out_fragments
; i
++)
541 if (u
->out_memblocks
[i
])
542 pa_memblock_unref_fixed(u
->out_memblocks
[i
]);
543 pa_xfree(u
->out_memblocks
);
546 if (u
->in_memblocks
) {
548 for (i
= 0; i
< u
->in_fragments
; i
++)
549 if (u
->in_memblocks
[i
])
550 pa_memblock_unref_fixed(u
->in_memblocks
[i
]);
551 pa_xfree(u
->in_memblocks
);
554 if (u
->in_mmap
&& u
->in_mmap
!= MAP_FAILED
)
555 munmap(u
->in_mmap
, u
->in_mmap_length
);
557 if (u
->out_mmap
&& u
->out_mmap
!= MAP_FAILED
)
558 munmap(u
->out_mmap
, u
->out_mmap_length
);
561 pa_sink_disconnect(u
->sink
);
562 pa_sink_unref(u
->sink
);
566 pa_source_disconnect(u
->source
);
567 pa_source_unref(u
->source
);
571 u
->core
->mainloop
->io_free(u
->io_event
);