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
25 /* General power management rules:
27 * When SUSPENDED we close the audio device.
29 * We make no difference between IDLE and RUNNING in our handling.
31 * As long as we are in RUNNING/IDLE state we will *always* write data to
32 * the device. If none is avilable from the inputs, we write silence
35 * If power should be saved on IDLE this should be implemented in a
36 * special suspend-on-idle module that will put us into SUSPEND mode
37 * as soon and we're idle for too long.
41 /* TODO: handle restoring of volume after suspend */
47 #ifdef HAVE_SYS_MMAN_H
51 #include <sys/soundcard.h>
52 #include <sys/ioctl.h>
64 #include <pulse/xmalloc.h>
65 #include <pulse/util.h>
67 #include <pulsecore/core-error.h>
68 #include <pulsecore/thread.h>
69 #include <pulsecore/sink.h>
70 #include <pulsecore/source.h>
71 #include <pulsecore/module.h>
72 #include <pulsecore/sample-util.h>
73 #include <pulsecore/core-util.h>
74 #include <pulsecore/modargs.h>
75 #include <pulsecore/log.h>
78 #include "module-oss-symdef.h"
80 PA_MODULE_AUTHOR("Lennart Poettering")
81 PA_MODULE_DESCRIPTION("OSS Sink/Source")
82 PA_MODULE_VERSION(PACKAGE_VERSION
)
84 "sink_name=<name for the sink> "
85 "source_name=<name for the source> "
86 "device=<OSS device> "
87 "record=<enable source?> "
88 "playback=<enable sink?> "
89 "format=<sample format> "
90 "channels=<number of channels> "
92 "fragments=<number of fragments> "
93 "fragment_size=<fragment size> "
94 "channel_map=<channel map> "
95 "mmap=<enable memory mapping?>")
97 #define DEFAULT_DEVICE "/dev/dsp"
99 #define DEFAULT_NFRAGS 4
100 #define DEFAULT_FRAGSIZE_MSEC 25
108 pa_asyncmsgq
*asyncmsgq
;
112 pa_memchunk memchunk
;
114 uint32_t in_fragment_size
, out_fragment_size
, in_nfrags
, out_nfrags
, in_hwbuf_size
, out_hwbuf_size
;
115 int use_getospace
, use_getispace
;
119 int use_input_volume
;
121 int sink_suspended
, source_suspended
;
126 int nfrags
, frag_size
;
129 unsigned out_mmap_current
, in_mmap_current
;
130 void *in_mmap
, *out_mmap
;
131 pa_memblock
**in_mmap_memblocks
, **out_mmap_memblocks
;
133 int in_mmap_saved_nfrags
, out_mmap_saved_nfrags
;
136 static const char* const valid_modargs
[] = {
152 static void trigger(struct userdata
*u
, int quick
) {
153 int enable_bits
= 0, zero
= 0;
155 /* pa_log_debug("trigger"); */
157 if (u
->source
&& u
->source
->thread_info
.state
!= PA_SOURCE_SUSPENDED
)
158 enable_bits
|= PCM_ENABLE_INPUT
;
160 if (u
->sink
&& u
->sink
->thread_info
.state
!= PA_SINK_SUSPENDED
)
161 enable_bits
|= PCM_ENABLE_OUTPUT
;
166 /* First, let's stop all playback, capturing */
167 ioctl(u
->fd
, SNDCTL_DSP_SETTRIGGER
, &zero
);
169 #ifdef SNDCTL_DSP_HALT
170 if (enable_bits
== 0)
171 if (ioctl(u
->fd
, SNDCTL_DSP_HALT
, NULL
) < 0)
172 pa_log_warn("SNDCTL_DSP_HALT: %s", pa_cstrerror(errno
));
175 if (ioctl(u
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable_bits
) < 0)
176 pa_log_warn("SNDCTL_DSP_SETTRIGGER: %s", pa_cstrerror(errno
));
178 if (u
->sink
&& !(enable_bits
& PCM_ENABLE_OUTPUT
)) {
179 pa_log_debug("clearing playback buffer");
180 pa_silence_memory(u
->out_mmap
, u
->out_hwbuf_size
, &u
->sink
->sample_spec
);
186 if (ioctl(u
->fd
, SNDCTL_DSP_POST
, NULL
) < 0)
187 pa_log_warn("SNDCTL_DSP_POST: %s", pa_cstrerror(errno
));
191 * Some crappy drivers do not start the recording until we
192 * read something. Without this snippet, poll will never
193 * register the fd as ready.
196 if (u
->source
&& u
->source
->thread_info
.state
!= PA_SOURCE_SUSPENDED
) {
197 uint8_t *buf
= pa_xnew(uint8_t, u
->in_fragment_size
);
198 pa_read(u
->fd
, buf
, u
->in_fragment_size
, NULL
);
205 static void mmap_fill_memblocks(struct userdata
*u
, unsigned n
) {
207 pa_assert(u
->out_mmap_memblocks
);
212 if (u
->out_mmap_memblocks
[u
->out_mmap_current
])
213 pa_memblock_unref_fixed(u
->out_mmap_memblocks
[u
->out_mmap_current
]);
215 chunk
.memblock
= u
->out_mmap_memblocks
[u
->out_mmap_current
] =
216 pa_memblock_new_fixed(
218 (uint8_t*) u
->out_mmap
+ u
->out_fragment_size
* u
->out_mmap_current
,
219 u
->out_fragment_size
,
222 chunk
.length
= pa_memblock_get_length(chunk
.memblock
);
225 pa_sink_render_into_full(u
->sink
, &chunk
);
227 u
->out_mmap_current
++;
228 while (u
->out_mmap_current
>= u
->out_nfrags
)
229 u
->out_mmap_current
-= u
->out_nfrags
;
235 static int mmap_write(struct userdata
*u
) {
236 struct count_info info
;
242 if (ioctl(u
->fd
, SNDCTL_DSP_GETOPTR
, &info
) < 0) {
243 pa_log("SNDCTL_DSP_GETOPTR: %s", pa_cstrerror(errno
));
247 info
.blocks
+= u
->out_mmap_saved_nfrags
;
248 u
->out_mmap_saved_nfrags
= 0;
251 mmap_fill_memblocks(u
, info
.blocks
);
256 static void mmap_post_memblocks(struct userdata
*u
, unsigned n
) {
258 pa_assert(u
->in_mmap_memblocks
);
263 if (!u
->in_mmap_memblocks
[u
->in_mmap_current
]) {
265 chunk
.memblock
= u
->in_mmap_memblocks
[u
->in_mmap_current
] =
266 pa_memblock_new_fixed(
268 (uint8_t*) u
->in_mmap
+ u
->in_fragment_size
*u
->in_mmap_current
,
272 chunk
.length
= pa_memblock_get_length(chunk
.memblock
);
275 pa_source_post(u
->source
, &chunk
);
278 u
->in_mmap_current
++;
279 while (u
->in_mmap_current
>= u
->in_nfrags
)
280 u
->in_mmap_current
-= u
->in_nfrags
;
286 static void mmap_clear_memblocks(struct userdata
*u
, unsigned n
) {
287 unsigned i
= u
->in_mmap_current
;
290 pa_assert(u
->in_mmap_memblocks
);
292 if (n
> u
->in_nfrags
)
296 if (u
->in_mmap_memblocks
[i
]) {
297 pa_memblock_unref_fixed(u
->in_mmap_memblocks
[i
]);
298 u
->in_mmap_memblocks
[i
] = NULL
;
302 while (i
>= u
->in_nfrags
)
309 static int mmap_read(struct userdata
*u
) {
310 struct count_info info
;
312 pa_assert(u
->source
);
314 if (ioctl(u
->fd
, SNDCTL_DSP_GETIPTR
, &info
) < 0) {
315 pa_log("SNDCTL_DSP_GETIPTR: %s", pa_cstrerror(errno
));
319 info
.blocks
+= u
->in_mmap_saved_nfrags
;
320 u
->in_mmap_saved_nfrags
= 0;
322 if (info
.blocks
> 0) {
323 mmap_post_memblocks(u
, info
.blocks
);
324 mmap_clear_memblocks(u
, u
->in_nfrags
/2);
330 static pa_usec_t
mmap_sink_get_latency(struct userdata
*u
) {
331 struct count_info info
;
336 if (ioctl(u
->fd
, SNDCTL_DSP_GETOPTR
, &info
) < 0) {
337 pa_log("SNDCTL_DSP_GETOPTR: %s", pa_cstrerror(errno
));
341 u
->out_mmap_saved_nfrags
+= info
.blocks
;
343 bpos
= ((u
->out_mmap_current
+ u
->out_mmap_saved_nfrags
) * u
->out_fragment_size
) % u
->out_hwbuf_size
;
345 if (bpos
<= (size_t) info
.ptr
)
346 n
= u
->out_hwbuf_size
- (info
.ptr
- bpos
);
350 /* 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); */
352 return pa_bytes_to_usec(n
, &u
->sink
->sample_spec
);
355 static pa_usec_t
mmap_source_get_latency(struct userdata
*u
) {
356 struct count_info info
;
361 if (ioctl(u
->fd
, SNDCTL_DSP_GETIPTR
, &info
) < 0) {
362 pa_log("SNDCTL_DSP_GETIPTR: %s", pa_cstrerror(errno
));
366 u
->in_mmap_saved_nfrags
+= info
.blocks
;
367 bpos
= ((u
->in_mmap_current
+ u
->in_mmap_saved_nfrags
) * u
->in_fragment_size
) % u
->in_hwbuf_size
;
369 if (bpos
<= (size_t) info
.ptr
)
372 n
= u
->in_hwbuf_size
- bpos
+ info
.ptr
;
374 /* 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); */
376 return pa_bytes_to_usec(n
, &u
->source
->sample_spec
);
379 static pa_usec_t
io_sink_get_latency(struct userdata
*u
) {
384 if (u
->use_getodelay
) {
387 if (ioctl(u
->fd
, SNDCTL_DSP_GETODELAY
, &arg
) < 0) {
388 pa_log_info("Device doesn't support SNDCTL_DSP_GETODELAY: %s", pa_cstrerror(errno
));
389 u
->use_getodelay
= 0;
391 r
= pa_bytes_to_usec(arg
, &u
->sink
->sample_spec
);
395 if (!u
->use_getodelay
&& u
->use_getospace
) {
396 struct audio_buf_info info
;
398 if (ioctl(u
->fd
, SNDCTL_DSP_GETOSPACE
, &info
) < 0) {
399 pa_log_info("Device doesn't support SNDCTL_DSP_GETOSPACE: %s", pa_cstrerror(errno
));
400 u
->use_getospace
= 0;
402 r
= pa_bytes_to_usec(info
.bytes
, &u
->sink
->sample_spec
);
405 if (u
->memchunk
.memblock
)
406 r
+= pa_bytes_to_usec(u
->memchunk
.length
, &u
->sink
->sample_spec
);
412 static pa_usec_t
io_source_get_latency(struct userdata
*u
) {
417 if (u
->use_getispace
) {
418 struct audio_buf_info info
;
420 if (ioctl(u
->fd
, SNDCTL_DSP_GETISPACE
, &info
) < 0) {
421 pa_log_info("Device doesn't support SNDCTL_DSP_GETISPACE: %s", pa_cstrerror(errno
));
422 u
->use_getispace
= 0;
424 r
= pa_bytes_to_usec(info
.bytes
, &u
->source
->sample_spec
);
430 static int suspend(struct userdata
*u
) {
432 pa_assert(u
->fd
>= 0);
434 if (u
->out_mmap_memblocks
) {
436 for (i
= 0; i
< u
->out_nfrags
; i
++)
437 if (u
->out_mmap_memblocks
[i
]) {
438 pa_memblock_unref_fixed(u
->out_mmap_memblocks
[i
]);
439 u
->out_mmap_memblocks
[i
] = NULL
;
443 if (u
->in_mmap_memblocks
) {
445 for (i
= 0; i
< u
->in_nfrags
; i
++)
446 if (u
->in_mmap_memblocks
[i
]) {
447 pa_memblock_unref_fixed(u
->in_mmap_memblocks
[i
]);
448 u
->in_mmap_memblocks
[i
] = NULL
;
452 if (u
->in_mmap
&& u
->in_mmap
!= MAP_FAILED
) {
453 munmap(u
->in_mmap
, u
->in_hwbuf_size
);
457 if (u
->out_mmap
&& u
->out_mmap
!= MAP_FAILED
) {
458 munmap(u
->out_mmap
, u
->out_hwbuf_size
);
463 ioctl(u
->fd
, SNDCTL_DSP_SYNC
, NULL
);
467 pa_log_debug("Device suspended...");
472 static int unsuspend(struct userdata
*u
) {
474 pa_sample_spec ss
, *ss_original
;
475 int frag_size
, in_frag_size
, out_frag_size
;
476 int in_nfrags
, out_nfrags
;
477 struct audio_buf_info info
;
480 pa_assert(u
->fd
< 0);
484 pa_log_debug("Trying resume...");
486 if ((u
->fd
= pa_oss_open(u
->device_name
, &m
, NULL
)) < 0) {
487 pa_log_warn("Resume failed, device busy (%s)", pa_cstrerror(errno
));
491 pa_log_warn("Resume failed, couldn't open device with original access mode.");
495 if (u
->nfrags
>= 2 && u
->frag_size
>= 1)
496 if (pa_oss_set_fragments(u
->fd
, u
->nfrags
, u
->frag_size
) < 0) {
497 pa_log_warn("Resume failed, couldn't set original fragment settings.");
501 ss
= *(ss_original
= u
->sink
? &u
->sink
->sample_spec
: &u
->source
->sample_spec
);
502 if (pa_oss_auto_format(u
->fd
, &ss
) < 0 || !pa_sample_spec_equal(&ss
, ss_original
)) {
503 pa_log_warn("Resume failed, couldn't set original sample format settings.");
507 if (ioctl(u
->fd
, SNDCTL_DSP_GETBLKSIZE
, &frag_size
) < 0) {
508 pa_log_warn("SNDCTL_DSP_GETBLKSIZE: %s", pa_cstrerror(errno
));
512 in_frag_size
= out_frag_size
= frag_size
;
513 in_nfrags
= out_nfrags
= u
->nfrags
;
515 if (ioctl(u
->fd
, SNDCTL_DSP_GETISPACE
, &info
) >= 0) {
516 in_frag_size
= info
.fragsize
;
517 in_nfrags
= info
.fragstotal
;
520 if (ioctl(u
->fd
, SNDCTL_DSP_GETOSPACE
, &info
) >= 0) {
521 out_frag_size
= info
.fragsize
;
522 out_nfrags
= info
.fragstotal
;
525 if ((u
->source
&& (in_frag_size
!= (int) u
->in_fragment_size
|| in_nfrags
!= (int) u
->in_nfrags
)) ||
526 (u
->sink
&& (out_frag_size
!= (int) u
->out_fragment_size
|| out_nfrags
!= (int) u
->out_nfrags
))) {
527 pa_log_warn("Resume failed, input fragment settings don't match.");
533 if ((u
->in_mmap
= mmap(NULL
, u
->in_hwbuf_size
, PROT_READ
, MAP_SHARED
, u
->fd
, 0)) == MAP_FAILED
) {
534 pa_log("Resume failed, mmap(): %s", pa_cstrerror(errno
));
540 if ((u
->out_mmap
= mmap(NULL
, u
->out_hwbuf_size
, PROT_WRITE
, MAP_SHARED
, u
->fd
, 0)) == MAP_FAILED
) {
541 pa_log("Resume failed, mmap(): %s", pa_cstrerror(errno
));
542 if (u
->in_mmap
&& u
->in_mmap
!= MAP_FAILED
) {
543 munmap(u
->in_mmap
, u
->in_hwbuf_size
);
550 pa_silence_memory(u
->out_mmap
, u
->out_hwbuf_size
, &ss
);
554 u
->out_mmap_current
= u
->in_mmap_current
= 0;
555 u
->out_mmap_saved_nfrags
= u
->in_mmap_saved_nfrags
= 0;
557 /* Now, start only what we need */
560 pa_log_debug("Resumed successfully...");
570 static int sink_process_msg(pa_msgobject
*o
, int code
, void *data
, pa_memchunk
*chunk
) {
571 struct userdata
*u
= PA_SINK(o
)->userdata
;
572 int do_trigger
= 0, ret
;
576 case PA_SINK_MESSAGE_GET_LATENCY
: {
581 r
= mmap_sink_get_latency(u
);
583 r
= io_sink_get_latency(u
);
586 *((pa_usec_t
*) data
) = r
;
591 case PA_SINK_MESSAGE_SET_STATE
:
593 if (PA_PTR_TO_UINT(data
) == PA_SINK_SUSPENDED
) {
594 pa_assert(u
->sink
->thread_info
.state
!= PA_SINK_SUSPENDED
);
596 if (u
->source_suspended
) {
602 u
->sink_suspended
= 1;
604 } else if (u
->sink
->thread_info
.state
== PA_SINK_SUSPENDED
) {
605 pa_assert(PA_PTR_TO_UINT(data
) != PA_SINK_SUSPENDED
);
607 if (u
->source_suspended
) {
608 if (unsuspend(u
) < 0)
613 u
->out_mmap_current
= 0;
614 u
->out_mmap_saved_nfrags
= 0;
616 u
->sink_suspended
= 0;
621 case PA_SINK_MESSAGE_SET_VOLUME
:
623 if (u
->use_pcm_volume
&& u
->fd
>= 0) {
625 if (pa_oss_set_pcm_volume(u
->fd
, &u
->sink
->sample_spec
, ((pa_cvolume
*) data
)) < 0) {
626 pa_log_info("Device doesn't support setting mixer settings: %s", pa_cstrerror(errno
));
627 u
->use_pcm_volume
= 0;
634 case PA_SINK_MESSAGE_GET_VOLUME
:
636 if (u
->use_pcm_volume
&& u
->fd
>= 0) {
638 if (pa_oss_get_pcm_volume(u
->fd
, &u
->sink
->sample_spec
, ((pa_cvolume
*) data
)) < 0) {
639 pa_log_info("Device doesn't support reading mixer settings: %s", pa_cstrerror(errno
));
640 u
->use_pcm_volume
= 0;
648 ret
= pa_sink_process_msg(o
, code
, data
, chunk
);
656 static int source_process_msg(pa_msgobject
*o
, int code
, void *data
, pa_memchunk
*chunk
) {
657 struct userdata
*u
= PA_SOURCE(o
)->userdata
;
658 int do_trigger
= 0, ret
;
662 case PA_SOURCE_MESSAGE_GET_LATENCY
: {
667 r
= mmap_source_get_latency(u
);
669 r
= io_source_get_latency(u
);
672 *((pa_usec_t
*) data
) = r
;
676 case PA_SOURCE_MESSAGE_SET_STATE
:
678 if (PA_PTR_TO_UINT(data
) == PA_SOURCE_SUSPENDED
) {
679 pa_assert(u
->source
->thread_info
.state
!= PA_SOURCE_SUSPENDED
);
681 if (u
->sink_suspended
) {
687 u
->source_suspended
= 1;
689 } else if (u
->source
->thread_info
.state
== PA_SOURCE_SUSPENDED
) {
690 pa_assert(PA_PTR_TO_UINT(data
) != PA_SOURCE_SUSPENDED
);
692 if (u
->sink_suspended
) {
693 if (unsuspend(u
) < 0)
698 u
->in_mmap_current
= 0;
699 u
->in_mmap_saved_nfrags
= 0;
701 u
->source_suspended
= 0;
706 case PA_SOURCE_MESSAGE_SET_VOLUME
:
708 if (u
->use_input_volume
&& u
->fd
>= 0) {
710 if (pa_oss_set_input_volume(u
->fd
, &u
->source
->sample_spec
, ((pa_cvolume
*) data
)) < 0) {
711 pa_log_info("Device doesn't support setting mixer settings: %s", pa_cstrerror(errno
));
712 u
->use_input_volume
= 0;
719 case PA_SOURCE_MESSAGE_GET_VOLUME
:
721 if (u
->use_input_volume
&& u
->fd
>= 0) {
723 if (pa_oss_get_input_volume(u
->fd
, &u
->source
->sample_spec
, ((pa_cvolume
*) data
)) < 0) {
724 pa_log_info("Device doesn't support reading mixer settings: %s", pa_cstrerror(errno
));
725 u
->use_input_volume
= 0;
733 ret
= pa_source_process_msg(o
, code
, data
, chunk
);
741 static void thread_func(void *userdata
) {
748 struct userdata
*u
= userdata
;
749 struct pollfd pollfd
[POLLFD_MAX
];
750 int write_type
= 0, read_type
= 0;
754 pa_log_debug("Thread starting up");
758 memset(&pollfd
, 0, sizeof(pollfd
));
760 pollfd
[POLLFD_ASYNCQ
].fd
= pa_asyncmsgq_get_fd(u
->asyncmsgq
);
761 pollfd
[POLLFD_ASYNCQ
].events
= POLLIN
;
762 pollfd
[POLLFD_DSP
].fd
= u
->fd
;
765 pa_msgobject
*object
;
771 /* pa_log("loop"); */
773 /* Check whether there is a message for us to process */
774 if (pa_asyncmsgq_get(u
->asyncmsgq
, &object
, &code
, &data
, &chunk
, 0) == 0) {
777 /* pa_log("processing msg"); */
779 if (!object
&& code
== PA_MESSAGE_SHUTDOWN
) {
780 pa_asyncmsgq_done(u
->asyncmsgq
, 0);
784 ret
= pa_asyncmsgq_dispatch(object
, code
, data
, &chunk
);
785 pa_asyncmsgq_done(u
->asyncmsgq
, ret
);
789 /* pa_log("loop2"); */
791 /* Render some data and write it to the dsp */
793 if (u
->sink
&& u
->sink
->thread_info
.state
!= PA_SINK_SUSPENDED
&& (pollfd
[POLLFD_DSP
].revents
& POLLOUT
)) {
798 if ((ret
= mmap_write(u
)) < 0)
801 pollfd
[POLLFD_DSP
].revents
&= ~POLLOUT
;
811 l
= u
->out_fragment_size
;
813 if (u
->use_getospace
) {
816 if (ioctl(u
->fd
, SNDCTL_DSP_GETOSPACE
, &info
) < 0) {
817 pa_log_info("Device doesn't support SNDCTL_DSP_GETOSPACE: %s", pa_cstrerror(errno
));
818 u
->use_getospace
= 0;
820 if (info
.bytes
>= l
) {
821 l
= (info
.bytes
/l
)*l
;
832 if (u
->memchunk
.length
<= 0)
833 pa_sink_render(u
->sink
, l
, &u
->memchunk
);
835 pa_assert(u
->memchunk
.length
> 0);
837 p
= pa_memblock_acquire(u
->memchunk
.memblock
);
838 t
= pa_write(u
->fd
, (uint8_t*) p
+ u
->memchunk
.index
, u
->memchunk
.length
, &write_type
);
839 pa_memblock_release(u
->memchunk
.memblock
);
841 /* pa_log("wrote %i bytes of %u", t, l); */
850 else if (errno
== EAGAIN
) {
851 pa_log_debug("EAGAIN");
853 pollfd
[POLLFD_DSP
].revents
&= ~POLLOUT
;
857 pa_log("Failed to write data to DSP: %s", pa_cstrerror(errno
));
863 u
->memchunk
.index
+= t
;
864 u
->memchunk
.length
-= t
;
866 if (u
->memchunk
.length
<= 0) {
867 pa_memblock_unref(u
->memchunk
.memblock
);
868 pa_memchunk_reset(&u
->memchunk
);
873 pollfd
[POLLFD_DSP
].revents
&= ~POLLOUT
;
876 } while (loop
&& l
> 0);
882 /* Try to read some data and pass it on to the source driver */
884 if (u
->source
&& u
->source
->thread_info
.state
!= PA_SOURCE_SUSPENDED
&& ((pollfd
[POLLFD_DSP
].revents
& POLLIN
))) {
889 if ((ret
= mmap_read(u
)) < 0)
892 pollfd
[POLLFD_DSP
].revents
&= ~POLLIN
;
901 pa_memchunk memchunk
;
904 l
= u
->in_fragment_size
;
906 if (u
->use_getispace
) {
909 if (ioctl(u
->fd
, SNDCTL_DSP_GETISPACE
, &info
) < 0) {
910 pa_log_info("Device doesn't support SNDCTL_DSP_GETISPACE: %s", pa_cstrerror(errno
));
911 u
->use_getispace
= 0;
913 if (info
.bytes
>= l
) {
914 l
= (info
.bytes
/l
)*l
;
925 memchunk
.memblock
= pa_memblock_new(u
->core
->mempool
, l
);
927 p
= pa_memblock_acquire(memchunk
.memblock
);
928 t
= pa_read(u
->fd
, p
, l
, &read_type
);
929 pa_memblock_release(memchunk
.memblock
);
931 pa_assert(t
!= 0); /* EOF cannot happen */
933 /* pa_log("read %i bytes of %u", t, l); */
936 pa_memblock_unref(memchunk
.memblock
);
941 else if (errno
== EAGAIN
) {
942 pa_log_debug("EAGAIN");
944 pollfd
[POLLFD_DSP
].revents
&= ~POLLIN
;
948 pa_log("Failed to read data from DSP: %s", pa_cstrerror(errno
));
956 pa_source_post(u
->source
, &memchunk
);
957 pa_memblock_unref(memchunk
.memblock
);
961 pollfd
[POLLFD_DSP
].revents
&= ~POLLIN
;
963 } while (loop
&& l
> 0);
970 pollfd
[POLLFD_DSP
].fd
= u
->fd
;
971 pollfd
[POLLFD_DSP
].events
=
972 ((u
->source
&& u
->source
->thread_info
.state
!= PA_SOURCE_SUSPENDED
) ? POLLIN
: 0) |
973 ((u
->sink
&& u
->sink
->thread_info
.state
!= PA_SINK_SUSPENDED
) ? POLLOUT
: 0);
976 /* Hmm, nothing to do. Let's sleep */
978 if (pa_asyncmsgq_before_poll(u
->asyncmsgq
) < 0)
981 /* pa_log("polling for %i (legend: %i=POLLIN, %i=POLLOUT)", u->fd >= 0 ? pollfd[POLLFD_DSP].events : -1, POLLIN, POLLOUT); */
982 r
= poll(pollfd
, u
->fd
>= 0 ? POLLFD_MAX
: POLLFD_DSP
, -1);
983 /* pa_log("polling got dsp=%i amq=%i (%i)", r > 0 ? pollfd[POLLFD_DSP].revents : 0, r > 0 ? pollfd[POLLFD_ASYNCQ].revents : 0, r); */
985 pa_asyncmsgq_after_poll(u
->asyncmsgq
);
988 pollfd
[POLLFD_DSP
].revents
= 0;
991 if (errno
== EINTR
) {
992 pollfd
[POLLFD_ASYNCQ
].revents
= 0;
993 pollfd
[POLLFD_DSP
].revents
= 0;
997 pa_log("poll() failed: %s", pa_cstrerror(errno
));
1003 if (pollfd
[POLLFD_DSP
].revents
& ~(POLLOUT
|POLLIN
)) {
1004 pa_log("DSP shutdown.");
1008 pa_assert((pollfd
[POLLFD_ASYNCQ
].revents
& ~POLLIN
) == 0);
1012 /* We have to continue processing messages until we receive the
1013 * SHUTDOWN message */
1014 pa_asyncmsgq_post(u
->core
->asyncmsgq
, PA_MSGOBJECT(u
->core
), PA_CORE_MESSAGE_UNLOAD_MODULE
, u
->module
, NULL
, NULL
);
1015 pa_asyncmsgq_wait_for(u
->asyncmsgq
, PA_MESSAGE_SHUTDOWN
);
1018 pa_log_debug("Thread shutting down");
1021 int pa__init(pa_core
*c
, pa_module
*m
) {
1022 struct audio_buf_info info
;
1023 struct userdata
*u
= NULL
;
1026 int nfrags
, frag_size
;
1028 int record
= 1, playback
= 1, use_mmap
= 1;
1031 pa_modargs
*ma
= NULL
;
1032 char hwdesc
[64], *t
;
1039 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
1040 pa_log("Failed to parse module arguments.");
1044 if (pa_modargs_get_value_boolean(ma
, "record", &record
) < 0 || pa_modargs_get_value_boolean(ma
, "playback", &playback
) < 0) {
1045 pa_log("record= and playback= expect numeric argument.");
1049 if (!playback
&& !record
) {
1050 pa_log("Neither playback nor record enabled for device.");
1054 mode
= (playback
&& record
) ? O_RDWR
: (playback
? O_WRONLY
: (record
? O_RDONLY
: 0));
1056 ss
= c
->default_sample_spec
;
1057 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &ss
, &map
, PA_CHANNEL_MAP_OSS
) < 0) {
1058 pa_log("Failed to parse sample specification or channel map");
1062 nfrags
= DEFAULT_NFRAGS
;
1063 frag_size
= pa_usec_to_bytes(DEFAULT_FRAGSIZE_MSEC
*1000, &ss
);
1065 frag_size
= pa_frame_size(&ss
);
1067 if (pa_modargs_get_value_s32(ma
, "fragments", &nfrags
) < 0 || pa_modargs_get_value_s32(ma
, "fragment_size", &frag_size
) < 0) {
1068 pa_log("Failed to parse fragments arguments");
1072 if (pa_modargs_get_value_boolean(ma
, "mmap", &use_mmap
) < 0) {
1073 pa_log("Failed to parse mmap argument.");
1077 if ((fd
= pa_oss_open(p
= pa_modargs_get_value(ma
, "device", DEFAULT_DEVICE
), &mode
, &caps
)) < 0)
1080 if (use_mmap
&& (!(caps
& DSP_CAP_MMAP
) || !(caps
& DSP_CAP_TRIGGER
))) {
1081 pa_log("OSS device not mmap capable, falling back to UNIX read/write mode");
1085 if (pa_oss_get_hw_description(p
, hwdesc
, sizeof(hwdesc
)) >= 0)
1086 pa_log_info("Hardware name is '%s'.", hwdesc
);
1090 pa_log_info("Device opened in %s mode.", mode
== O_WRONLY
? "O_WRONLY" : (mode
== O_RDONLY
? "O_RDONLY" : "O_RDWR"));
1092 if (nfrags
>= 2 && frag_size
>= 1)
1093 if (pa_oss_set_fragments(fd
, nfrags
, frag_size
) < 0)
1096 if (pa_oss_auto_format(fd
, &ss
) < 0)
1099 if (ioctl(fd
, SNDCTL_DSP_GETBLKSIZE
, &frag_size
) < 0) {
1100 pa_log("SNDCTL_DSP_GETBLKSIZE: %s", pa_cstrerror(errno
));
1103 pa_assert(frag_size
> 0);
1105 u
= pa_xnew0(struct userdata
, 1);
1109 u
->use_getospace
= u
->use_getispace
= 1;
1110 u
->use_getodelay
= 1;
1111 u
->use_input_volume
= u
->use_pcm_volume
= 1;
1113 u
->device_name
= pa_xstrdup(p
);
1114 u
->in_nfrags
= u
->out_nfrags
= u
->nfrags
= nfrags
;
1115 u
->out_fragment_size
= u
->in_fragment_size
= u
->frag_size
= frag_size
;
1116 u
->use_mmap
= use_mmap
;
1117 pa_assert_se(u
->asyncmsgq
= pa_asyncmsgq_new(0));
1119 if (ioctl(fd
, SNDCTL_DSP_GETISPACE
, &info
) >= 0) {
1120 pa_log_info("Input -- %u fragments of size %u.", info
.fragstotal
, info
.fragsize
);
1121 u
->in_fragment_size
= info
.fragsize
;
1122 u
->in_nfrags
= info
.fragstotal
;
1123 u
->use_getispace
= 1;
1126 if (ioctl(fd
, SNDCTL_DSP_GETOSPACE
, &info
) >= 0) {
1127 pa_log_info("Output -- %u fragments of size %u.", info
.fragstotal
, info
.fragsize
);
1128 u
->out_fragment_size
= info
.fragsize
;
1129 u
->out_nfrags
= info
.fragstotal
;
1130 u
->use_getospace
= 1;
1133 u
->in_hwbuf_size
= u
->in_nfrags
* u
->in_fragment_size
;
1134 u
->out_hwbuf_size
= u
->out_nfrags
* u
->out_fragment_size
;
1136 if (mode
!= O_WRONLY
) {
1137 char *name_buf
= NULL
;
1140 if ((u
->in_mmap
= mmap(NULL
, u
->in_hwbuf_size
, PROT_READ
, MAP_SHARED
, fd
, 0)) == MAP_FAILED
) {
1141 if (mode
== O_RDWR
) {
1142 pa_log_debug("mmap() failed for input. Changing to O_WRONLY mode.");
1146 pa_log("mmap(): %s", pa_cstrerror(errno
));
1151 pa_log_debug("Successfully mmap()ed input buffer.");
1154 if ((name
= pa_modargs_get_value(ma
, "source_name", NULL
)))
1157 name
= name_buf
= pa_sprintf_malloc("oss_input.%s", pa_path_get_filename(p
));
1161 u
->source
= pa_source_new(c
, __FILE__
, name
, namereg_fail
, &ss
, &map
);
1166 u
->source
->parent
.process_msg
= source_process_msg
;
1167 u
->source
->userdata
= u
;
1169 pa_source_set_module(u
->source
, m
);
1170 pa_source_set_asyncmsgq(u
->source
, u
->asyncmsgq
);
1171 pa_source_set_description(u
->source
, t
= pa_sprintf_malloc("OSS PCM on %s%s%s%s",
1173 hwdesc
[0] ? " (" : "",
1174 hwdesc
[0] ? hwdesc
: "",
1175 hwdesc
[0] ? ")" : ""));
1177 u
->source
->is_hardware
= 1;
1178 u
->source
->refresh_volume
= 1;
1181 u
->in_mmap_memblocks
= pa_xnew0(pa_memblock
*, u
->in_nfrags
);
1186 if (mode
!= O_RDONLY
) {
1187 char *name_buf
= NULL
;
1190 if ((u
->out_mmap
= mmap(NULL
, u
->out_hwbuf_size
, PROT_WRITE
, MAP_SHARED
, fd
, 0)) == MAP_FAILED
) {
1191 if (mode
== O_RDWR
) {
1192 pa_log_debug("mmap() failed for input. Changing to O_WRONLY mode.");
1196 pa_log("mmap(): %s", pa_cstrerror(errno
));
1201 pa_log_debug("Successfully mmap()ed output buffer.");
1202 pa_silence_memory(u
->out_mmap
, u
->out_hwbuf_size
, &ss
);
1205 if ((name
= pa_modargs_get_value(ma
, "sink_name", NULL
)))
1208 name
= name_buf
= pa_sprintf_malloc("oss_output.%s", pa_path_get_filename(p
));
1212 u
->sink
= pa_sink_new(c
, __FILE__
, name
, namereg_fail
, &ss
, &map
);
1217 u
->sink
->parent
.process_msg
= sink_process_msg
;
1218 u
->sink
->userdata
= u
;
1220 pa_sink_set_module(u
->sink
, m
);
1221 pa_sink_set_asyncmsgq(u
->sink
, u
->asyncmsgq
);
1222 pa_sink_set_description(u
->sink
, t
= pa_sprintf_malloc("OSS PCM on %s%s%s%s",
1224 hwdesc
[0] ? " (" : "",
1225 hwdesc
[0] ? hwdesc
: "",
1226 hwdesc
[0] ? ")" : ""));
1228 u
->sink
->is_hardware
= 1;
1229 u
->sink
->refresh_volume
= 1;
1232 u
->out_mmap_memblocks
= pa_xnew0(pa_memblock
*, u
->out_nfrags
);
1237 pa_assert(u
->source
|| u
->sink
);
1241 pa_memchunk_reset(&u
->memchunk
);
1243 if (!(u
->thread
= pa_thread_new(thread_func
, u
))) {
1244 pa_log("Failed to create thread.");
1248 pa_modargs_free(ma
);
1250 /* Read mixer settings */
1252 pa_asyncmsgq_post(u
->asyncmsgq
, PA_MSGOBJECT(u
->source
), PA_SOURCE_MESSAGE_GET_VOLUME
, &u
->source
->volume
, NULL
, NULL
);
1254 pa_asyncmsgq_post(u
->asyncmsgq
, PA_MSGOBJECT(u
->sink
), PA_SINK_MESSAGE_GET_VOLUME
, &u
->sink
->volume
, NULL
, NULL
);
1263 pa_modargs_free(ma
);
1268 void pa__done(pa_core
*c
, pa_module
*m
) {
1274 if (!(u
= m
->userdata
))
1278 pa_sink_disconnect(u
->sink
);
1281 pa_source_disconnect(u
->source
);
1284 pa_asyncmsgq_send(u
->asyncmsgq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, NULL
);
1285 pa_thread_free(u
->thread
);
1289 pa_asyncmsgq_free(u
->asyncmsgq
);
1292 pa_sink_unref(u
->sink
);
1295 pa_source_unref(u
->source
);
1297 if (u
->memchunk
.memblock
)
1298 pa_memblock_unref(u
->memchunk
.memblock
);
1300 if (u
->out_mmap_memblocks
) {
1302 for (i
= 0; i
< u
->out_nfrags
; i
++)
1303 if (u
->out_mmap_memblocks
[i
])
1304 pa_memblock_unref_fixed(u
->out_mmap_memblocks
[i
]);
1305 pa_xfree(u
->out_mmap_memblocks
);
1308 if (u
->in_mmap_memblocks
) {
1310 for (i
= 0; i
< u
->in_nfrags
; i
++)
1311 if (u
->in_mmap_memblocks
[i
])
1312 pa_memblock_unref_fixed(u
->in_mmap_memblocks
[i
]);
1313 pa_xfree(u
->in_mmap_memblocks
);
1316 if (u
->in_mmap
&& u
->in_mmap
!= MAP_FAILED
)
1317 munmap(u
->in_mmap
, u
->in_hwbuf_size
);
1319 if (u
->out_mmap
&& u
->out_mmap
!= MAP_FAILED
)
1320 munmap(u
->out_mmap
, u
->out_hwbuf_size
);
1325 pa_xfree(u
->device_name
);