2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 /* General power management rules:
25 * When SUSPENDED we close the audio device.
27 * We make no difference between IDLE and RUNNING in our handling.
29 * As long as we are in RUNNING/IDLE state we will *always* write data to
30 * the device. If none is avilable from the inputs, we write silence
33 * If power should be saved on IDLE module-suspend-on-idle should be used.
41 #ifdef HAVE_SYS_MMAN_H
45 #include <sys/soundcard.h>
46 #include <sys/ioctl.h>
58 #include <pulse/xmalloc.h>
59 #include <pulse/util.h>
61 #include <pulsecore/core-error.h>
62 #include <pulsecore/thread.h>
63 #include <pulsecore/sink.h>
64 #include <pulsecore/source.h>
65 #include <pulsecore/module.h>
66 #include <pulsecore/sample-util.h>
67 #include <pulsecore/core-util.h>
68 #include <pulsecore/modargs.h>
69 #include <pulsecore/log.h>
70 #include <pulsecore/macro.h>
71 #include <pulsecore/thread-mq.h>
72 #include <pulsecore/rtpoll.h>
75 #include "module-oss-symdef.h"
77 PA_MODULE_AUTHOR("Lennart Poettering");
78 PA_MODULE_DESCRIPTION("OSS Sink/Source");
79 PA_MODULE_VERSION(PACKAGE_VERSION
);
80 PA_MODULE_LOAD_ONCE(FALSE
);
82 "sink_name=<name for the sink> "
83 "source_name=<name for the source> "
84 "device=<OSS device> "
85 "record=<enable source?> "
86 "playback=<enable sink?> "
87 "format=<sample format> "
88 "channels=<number of channels> "
90 "fragments=<number of fragments> "
91 "fragment_size=<fragment size> "
92 "channel_map=<channel map> "
93 "mmap=<enable memory mapping?>");
95 #define DEFAULT_DEVICE "/dev/dsp"
104 pa_thread_mq thread_mq
;
109 pa_memchunk memchunk
;
112 uint32_t in_fragment_size
, out_fragment_size
, in_nfrags
, out_nfrags
, in_hwbuf_size
, out_hwbuf_size
;
113 pa_bool_t use_getospace
, use_getispace
;
114 pa_bool_t use_getodelay
;
116 pa_bool_t sink_suspended
, source_suspended
;
124 int nfrags
, frag_size
;
127 unsigned out_mmap_current
, in_mmap_current
;
128 void *in_mmap
, *out_mmap
;
129 pa_memblock
**in_mmap_memblocks
, **out_mmap_memblocks
;
131 int in_mmap_saved_nfrags
, out_mmap_saved_nfrags
;
133 pa_rtpoll_item
*rtpoll_item
;
136 static const char* const valid_modargs
[] = {
152 static void trigger(struct userdata
*u
, pa_bool_t quick
) {
153 int enable_bits
= 0, zero
= 0;
160 pa_log_debug("trigger");
162 if (u
->source
&& PA_SOURCE_IS_OPENED(u
->source
->thread_info
.state
))
163 enable_bits
|= PCM_ENABLE_INPUT
;
165 if (u
->sink
&& PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
))
166 enable_bits
|= PCM_ENABLE_OUTPUT
;
168 pa_log_debug("trigger: %i", enable_bits
);
174 ioctl(u
->fd
, SNDCTL_DSP_SETTRIGGER
, &zero
);
176 #ifdef SNDCTL_DSP_HALT
177 if (enable_bits
== 0)
178 if (ioctl(u
->fd
, SNDCTL_DSP_HALT
, NULL
) < 0)
179 pa_log_warn("SNDCTL_DSP_HALT: %s", pa_cstrerror(errno
));
182 if (ioctl(u
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable_bits
) < 0)
183 pa_log_warn("SNDCTL_DSP_SETTRIGGER: %s", pa_cstrerror(errno
));
185 if (u
->sink
&& !(enable_bits
& PCM_ENABLE_OUTPUT
)) {
186 pa_log_debug("clearing playback buffer");
187 pa_silence_memory(u
->out_mmap
, u
->out_hwbuf_size
, &u
->sink
->sample_spec
);
193 if (ioctl(u
->fd
, SNDCTL_DSP_POST
, NULL
) < 0)
194 pa_log_warn("SNDCTL_DSP_POST: %s", pa_cstrerror(errno
));
198 * Some crappy drivers do not start the recording until we
199 * read something. Without this snippet, poll will never
200 * register the fd as ready.
203 if (u
->source
&& PA_SOURCE_IS_OPENED(u
->source
->thread_info
.state
)) {
204 uint8_t *buf
= pa_xnew(uint8_t, u
->in_fragment_size
);
205 pa_read(u
->fd
, buf
, u
->in_fragment_size
, NULL
);
212 static void mmap_fill_memblocks(struct userdata
*u
, unsigned n
) {
214 pa_assert(u
->out_mmap_memblocks
);
216 /* pa_log("Mmmap writing %u blocks", n); */
221 if (u
->out_mmap_memblocks
[u
->out_mmap_current
])
222 pa_memblock_unref_fixed(u
->out_mmap_memblocks
[u
->out_mmap_current
]);
224 chunk
.memblock
= u
->out_mmap_memblocks
[u
->out_mmap_current
] =
225 pa_memblock_new_fixed(
227 (uint8_t*) u
->out_mmap
+ u
->out_fragment_size
* u
->out_mmap_current
,
228 u
->out_fragment_size
,
231 chunk
.length
= pa_memblock_get_length(chunk
.memblock
);
234 pa_sink_render_into_full(u
->sink
, &chunk
);
236 u
->out_mmap_current
++;
237 while (u
->out_mmap_current
>= u
->out_nfrags
)
238 u
->out_mmap_current
-= u
->out_nfrags
;
244 static int mmap_write(struct userdata
*u
) {
245 struct count_info info
;
250 /* pa_log("Mmmap writing..."); */
252 if (ioctl(u
->fd
, SNDCTL_DSP_GETOPTR
, &info
) < 0) {
253 pa_log("SNDCTL_DSP_GETOPTR: %s", pa_cstrerror(errno
));
257 info
.blocks
+= u
->out_mmap_saved_nfrags
;
258 u
->out_mmap_saved_nfrags
= 0;
261 mmap_fill_memblocks(u
, (unsigned) info
.blocks
);
266 static void mmap_post_memblocks(struct userdata
*u
, unsigned n
) {
268 pa_assert(u
->in_mmap_memblocks
);
270 /* pa_log("Mmmap reading %u blocks", n); */
275 if (!u
->in_mmap_memblocks
[u
->in_mmap_current
]) {
277 chunk
.memblock
= u
->in_mmap_memblocks
[u
->in_mmap_current
] =
278 pa_memblock_new_fixed(
280 (uint8_t*) u
->in_mmap
+ u
->in_fragment_size
*u
->in_mmap_current
,
284 chunk
.length
= pa_memblock_get_length(chunk
.memblock
);
287 pa_source_post(u
->source
, &chunk
);
290 u
->in_mmap_current
++;
291 while (u
->in_mmap_current
>= u
->in_nfrags
)
292 u
->in_mmap_current
-= u
->in_nfrags
;
298 static void mmap_clear_memblocks(struct userdata
*u
, unsigned n
) {
299 unsigned i
= u
->in_mmap_current
;
302 pa_assert(u
->in_mmap_memblocks
);
304 if (n
> u
->in_nfrags
)
308 if (u
->in_mmap_memblocks
[i
]) {
309 pa_memblock_unref_fixed(u
->in_mmap_memblocks
[i
]);
310 u
->in_mmap_memblocks
[i
] = NULL
;
314 while (i
>= u
->in_nfrags
)
321 static int mmap_read(struct userdata
*u
) {
322 struct count_info info
;
324 pa_assert(u
->source
);
326 /* pa_log("Mmmap reading..."); */
328 if (ioctl(u
->fd
, SNDCTL_DSP_GETIPTR
, &info
) < 0) {
329 pa_log("SNDCTL_DSP_GETIPTR: %s", pa_cstrerror(errno
));
333 /* pa_log("... %i", info.blocks); */
335 info
.blocks
+= u
->in_mmap_saved_nfrags
;
336 u
->in_mmap_saved_nfrags
= 0;
338 if (info
.blocks
> 0) {
339 mmap_post_memblocks(u
, (unsigned) info
.blocks
);
340 mmap_clear_memblocks(u
, u
->in_nfrags
/2);
346 static pa_usec_t
mmap_sink_get_latency(struct userdata
*u
) {
347 struct count_info info
;
352 if (ioctl(u
->fd
, SNDCTL_DSP_GETOPTR
, &info
) < 0) {
353 pa_log("SNDCTL_DSP_GETOPTR: %s", pa_cstrerror(errno
));
357 u
->out_mmap_saved_nfrags
+= info
.blocks
;
359 bpos
= ((u
->out_mmap_current
+ (unsigned) u
->out_mmap_saved_nfrags
) * u
->out_fragment_size
) % u
->out_hwbuf_size
;
361 if (bpos
<= (size_t) info
.ptr
)
362 n
= u
->out_hwbuf_size
- ((size_t) info
.ptr
- bpos
);
364 n
= bpos
- (size_t) info
.ptr
;
366 /* 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); */
368 return pa_bytes_to_usec(n
, &u
->sink
->sample_spec
);
371 static pa_usec_t
mmap_source_get_latency(struct userdata
*u
) {
372 struct count_info info
;
377 if (ioctl(u
->fd
, SNDCTL_DSP_GETIPTR
, &info
) < 0) {
378 pa_log("SNDCTL_DSP_GETIPTR: %s", pa_cstrerror(errno
));
382 u
->in_mmap_saved_nfrags
+= info
.blocks
;
383 bpos
= ((u
->in_mmap_current
+ (unsigned) u
->in_mmap_saved_nfrags
) * u
->in_fragment_size
) % u
->in_hwbuf_size
;
385 if (bpos
<= (size_t) info
.ptr
)
386 n
= (size_t) info
.ptr
- bpos
;
388 n
= u
->in_hwbuf_size
- bpos
+ (size_t) info
.ptr
;
390 /* 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); */
392 return pa_bytes_to_usec(n
, &u
->source
->sample_spec
);
395 static pa_usec_t
io_sink_get_latency(struct userdata
*u
) {
400 if (u
->use_getodelay
) {
403 if (ioctl(u
->fd
, SNDCTL_DSP_GETODELAY
, &arg
) < 0) {
404 pa_log_info("Device doesn't support SNDCTL_DSP_GETODELAY: %s", pa_cstrerror(errno
));
405 u
->use_getodelay
= 0;
407 r
= pa_bytes_to_usec((size_t) arg
, &u
->sink
->sample_spec
);
411 if (!u
->use_getodelay
&& u
->use_getospace
) {
412 struct audio_buf_info info
;
414 if (ioctl(u
->fd
, SNDCTL_DSP_GETOSPACE
, &info
) < 0) {
415 pa_log_info("Device doesn't support SNDCTL_DSP_GETOSPACE: %s", pa_cstrerror(errno
));
416 u
->use_getospace
= 0;
418 r
= pa_bytes_to_usec((size_t) info
.bytes
, &u
->sink
->sample_spec
);
421 if (u
->memchunk
.memblock
)
422 r
+= pa_bytes_to_usec(u
->memchunk
.length
, &u
->sink
->sample_spec
);
428 static pa_usec_t
io_source_get_latency(struct userdata
*u
) {
433 if (u
->use_getispace
) {
434 struct audio_buf_info info
;
436 if (ioctl(u
->fd
, SNDCTL_DSP_GETISPACE
, &info
) < 0) {
437 pa_log_info("Device doesn't support SNDCTL_DSP_GETISPACE: %s", pa_cstrerror(errno
));
438 u
->use_getispace
= 0;
440 r
= pa_bytes_to_usec((size_t) info
.bytes
, &u
->source
->sample_spec
);
446 static void build_pollfd(struct userdata
*u
) {
447 struct pollfd
*pollfd
;
450 pa_assert(u
->fd
>= 0);
453 pa_rtpoll_item_free(u
->rtpoll_item
);
455 u
->rtpoll_item
= pa_rtpoll_item_new(u
->rtpoll
, PA_RTPOLL_NEVER
, 1);
456 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
462 static int suspend(struct userdata
*u
) {
464 pa_assert(u
->fd
>= 0);
466 pa_log_info("Suspending...");
468 if (u
->out_mmap_memblocks
) {
470 for (i
= 0; i
< u
->out_nfrags
; i
++)
471 if (u
->out_mmap_memblocks
[i
]) {
472 pa_memblock_unref_fixed(u
->out_mmap_memblocks
[i
]);
473 u
->out_mmap_memblocks
[i
] = NULL
;
477 if (u
->in_mmap_memblocks
) {
479 for (i
= 0; i
< u
->in_nfrags
; i
++)
480 if (u
->in_mmap_memblocks
[i
]) {
481 pa_memblock_unref_fixed(u
->in_mmap_memblocks
[i
]);
482 u
->in_mmap_memblocks
[i
] = NULL
;
486 if (u
->in_mmap
&& u
->in_mmap
!= MAP_FAILED
) {
487 munmap(u
->in_mmap
, u
->in_hwbuf_size
);
491 if (u
->out_mmap
&& u
->out_mmap
!= MAP_FAILED
) {
492 munmap(u
->out_mmap
, u
->out_hwbuf_size
);
497 ioctl(u
->fd
, SNDCTL_DSP_SYNC
, NULL
);
501 if (u
->rtpoll_item
) {
502 pa_rtpoll_item_free(u
->rtpoll_item
);
503 u
->rtpoll_item
= NULL
;
506 pa_log_info("Device suspended...");
511 static int sink_get_volume(pa_sink
*s
);
512 static int source_get_volume(pa_source
*s
);
514 static int unsuspend(struct userdata
*u
) {
516 pa_sample_spec ss
, *ss_original
;
517 int frag_size
, in_frag_size
, out_frag_size
;
518 int in_nfrags
, out_nfrags
;
519 struct audio_buf_info info
;
522 pa_assert(u
->fd
< 0);
526 pa_log_info("Trying resume...");
528 if ((u
->fd
= pa_oss_open(u
->device_name
, &m
, NULL
)) < 0) {
529 pa_log_warn("Resume failed, device busy (%s)", pa_cstrerror(errno
));
534 pa_log_warn("Resume failed, couldn't open device with original access mode.");
538 if (u
->nfrags
>= 2 && u
->frag_size
>= 1)
539 if (pa_oss_set_fragments(u
->fd
, u
->nfrags
, u
->frag_size
) < 0) {
540 pa_log_warn("Resume failed, couldn't set original fragment settings.");
544 ss
= *(ss_original
= u
->sink
? &u
->sink
->sample_spec
: &u
->source
->sample_spec
);
545 if (pa_oss_auto_format(u
->fd
, &ss
) < 0 || !pa_sample_spec_equal(&ss
, ss_original
)) {
546 pa_log_warn("Resume failed, couldn't set original sample format settings.");
550 if (ioctl(u
->fd
, SNDCTL_DSP_GETBLKSIZE
, &frag_size
) < 0) {
551 pa_log_warn("SNDCTL_DSP_GETBLKSIZE: %s", pa_cstrerror(errno
));
555 in_frag_size
= out_frag_size
= frag_size
;
556 in_nfrags
= out_nfrags
= u
->nfrags
;
558 if (ioctl(u
->fd
, SNDCTL_DSP_GETISPACE
, &info
) >= 0) {
559 in_frag_size
= info
.fragsize
;
560 in_nfrags
= info
.fragstotal
;
563 if (ioctl(u
->fd
, SNDCTL_DSP_GETOSPACE
, &info
) >= 0) {
564 out_frag_size
= info
.fragsize
;
565 out_nfrags
= info
.fragstotal
;
568 if ((u
->source
&& (in_frag_size
!= (int) u
->in_fragment_size
|| in_nfrags
!= (int) u
->in_nfrags
)) ||
569 (u
->sink
&& (out_frag_size
!= (int) u
->out_fragment_size
|| out_nfrags
!= (int) u
->out_nfrags
))) {
570 pa_log_warn("Resume failed, input fragment settings don't match.");
576 if ((u
->in_mmap
= mmap(NULL
, u
->in_hwbuf_size
, PROT_READ
, MAP_SHARED
, u
->fd
, 0)) == MAP_FAILED
) {
577 pa_log("Resume failed, mmap(): %s", pa_cstrerror(errno
));
583 if ((u
->out_mmap
= mmap(NULL
, u
->out_hwbuf_size
, PROT_WRITE
, MAP_SHARED
, u
->fd
, 0)) == MAP_FAILED
) {
584 pa_log("Resume failed, mmap(): %s", pa_cstrerror(errno
));
585 if (u
->in_mmap
&& u
->in_mmap
!= MAP_FAILED
) {
586 munmap(u
->in_mmap
, u
->in_hwbuf_size
);
593 pa_silence_memory(u
->out_mmap
, u
->out_hwbuf_size
, &ss
);
597 u
->out_mmap_current
= u
->in_mmap_current
= 0;
598 u
->out_mmap_saved_nfrags
= u
->in_mmap_saved_nfrags
= 0;
600 pa_assert(!u
->rtpoll_item
);
605 sink_get_volume(u
->sink
);
607 source_get_volume(u
->source
);
609 pa_log_info("Resumed successfully...");
619 static int sink_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
620 struct userdata
*u
= PA_SINK(o
)->userdata
;
622 pa_bool_t do_trigger
= FALSE
, quick
= TRUE
;
626 case PA_SINK_MESSAGE_GET_LATENCY
: {
631 r
= mmap_sink_get_latency(u
);
633 r
= io_sink_get_latency(u
);
636 *((pa_usec_t
*) data
) = r
;
641 case PA_SINK_MESSAGE_SET_STATE
:
643 switch ((pa_sink_state_t
) PA_PTR_TO_UINT(data
)) {
645 case PA_SINK_SUSPENDED
:
646 pa_assert(PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
));
648 if (!u
->source
|| u
->source_suspended
) {
655 u
->sink_suspended
= TRUE
;
659 case PA_SINK_RUNNING
:
661 if (u
->sink
->thread_info
.state
== PA_SINK_INIT
) {
663 quick
= u
->source
&& PA_SOURCE_IS_OPENED(u
->source
->thread_info
.state
);
666 if (u
->sink
->thread_info
.state
== PA_SINK_SUSPENDED
) {
668 if (!u
->source
|| u
->source_suspended
) {
669 if (unsuspend(u
) < 0)
676 u
->out_mmap_current
= 0;
677 u
->out_mmap_saved_nfrags
= 0;
679 u
->sink_suspended
= FALSE
;
684 case PA_SINK_UNLINKED
:
693 ret
= pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
701 static int source_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
702 struct userdata
*u
= PA_SOURCE(o
)->userdata
;
704 int do_trigger
= FALSE
, quick
= TRUE
;
708 case PA_SOURCE_MESSAGE_GET_LATENCY
: {
713 r
= mmap_source_get_latency(u
);
715 r
= io_source_get_latency(u
);
718 *((pa_usec_t
*) data
) = r
;
722 case PA_SOURCE_MESSAGE_SET_STATE
:
724 switch ((pa_source_state_t
) PA_PTR_TO_UINT(data
)) {
725 case PA_SOURCE_SUSPENDED
:
726 pa_assert(PA_SOURCE_IS_OPENED(u
->source
->thread_info
.state
));
728 if (!u
->sink
|| u
->sink_suspended
) {
735 u
->source_suspended
= TRUE
;
739 case PA_SOURCE_RUNNING
:
741 if (u
->source
->thread_info
.state
== PA_SOURCE_INIT
) {
743 quick
= u
->sink
&& PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
);
746 if (u
->source
->thread_info
.state
== PA_SOURCE_SUSPENDED
) {
748 if (!u
->sink
|| u
->sink_suspended
) {
749 if (unsuspend(u
) < 0)
756 u
->in_mmap_current
= 0;
757 u
->in_mmap_saved_nfrags
= 0;
759 u
->source_suspended
= FALSE
;
763 case PA_SOURCE_UNLINKED
:
772 ret
= pa_source_process_msg(o
, code
, data
, offset
, chunk
);
780 static int sink_get_volume(pa_sink
*s
) {
784 pa_assert_se(u
= s
->userdata
);
786 pa_assert(u
->mixer_devmask
& (SOUND_MASK_VOLUME
|SOUND_MASK_PCM
));
788 if (u
->mixer_devmask
& SOUND_MASK_VOLUME
)
789 if ((r
= pa_oss_get_volume(u
->mixer_fd
, SOUND_MIXER_READ_VOLUME
, &s
->sample_spec
, &s
->volume
)) >= 0)
792 if (u
->mixer_devmask
& SOUND_MASK_PCM
)
793 if ((r
= pa_oss_get_volume(u
->mixer_fd
, SOUND_MIXER_READ_PCM
, &s
->sample_spec
, &s
->volume
)) >= 0)
796 pa_log_info("Device doesn't support reading mixer settings: %s", pa_cstrerror(errno
));
800 static int sink_set_volume(pa_sink
*s
) {
804 pa_assert_se(u
= s
->userdata
);
806 pa_assert(u
->mixer_devmask
& (SOUND_MASK_VOLUME
|SOUND_MASK_PCM
));
808 if (u
->mixer_devmask
& SOUND_MASK_VOLUME
)
809 if ((r
= pa_oss_set_volume(u
->mixer_fd
, SOUND_MIXER_WRITE_VOLUME
, &s
->sample_spec
, &s
->volume
)) >= 0)
812 if (u
->mixer_devmask
& SOUND_MASK_PCM
)
813 if ((r
= pa_oss_get_volume(u
->mixer_fd
, SOUND_MIXER_WRITE_PCM
, &s
->sample_spec
, &s
->volume
)) >= 0)
816 pa_log_info("Device doesn't support writing mixer settings: %s", pa_cstrerror(errno
));
820 static int source_get_volume(pa_source
*s
) {
824 pa_assert_se(u
= s
->userdata
);
826 pa_assert(u
->mixer_devmask
& (SOUND_MASK_IGAIN
|SOUND_MASK_RECLEV
));
828 if (u
->mixer_devmask
& SOUND_MASK_IGAIN
)
829 if ((r
= pa_oss_get_volume(u
->mixer_fd
, SOUND_MIXER_READ_IGAIN
, &s
->sample_spec
, &s
->volume
)) >= 0)
832 if (u
->mixer_devmask
& SOUND_MASK_RECLEV
)
833 if ((r
= pa_oss_get_volume(u
->mixer_fd
, SOUND_MIXER_READ_RECLEV
, &s
->sample_spec
, &s
->volume
)) >= 0)
836 pa_log_info("Device doesn't support reading mixer settings: %s", pa_cstrerror(errno
));
840 static int source_set_volume(pa_source
*s
) {
844 pa_assert_se(u
= s
->userdata
);
846 pa_assert(u
->mixer_devmask
& (SOUND_MASK_IGAIN
|SOUND_MASK_RECLEV
));
848 if (u
->mixer_devmask
& SOUND_MASK_IGAIN
)
849 if ((r
= pa_oss_set_volume(u
->mixer_fd
, SOUND_MIXER_WRITE_IGAIN
, &s
->sample_spec
, &s
->volume
)) >= 0)
852 if (u
->mixer_devmask
& SOUND_MASK_RECLEV
)
853 if ((r
= pa_oss_get_volume(u
->mixer_fd
, SOUND_MIXER_WRITE_RECLEV
, &s
->sample_spec
, &s
->volume
)) >= 0)
856 pa_log_info("Device doesn't support writing mixer settings: %s", pa_cstrerror(errno
));
860 static void thread_func(void *userdata
) {
861 struct userdata
*u
= userdata
;
862 int write_type
= 0, read_type
= 0;
867 pa_log_debug("Thread starting up");
869 if (u
->core
->realtime_scheduling
)
870 pa_make_realtime(u
->core
->realtime_priority
);
872 pa_thread_mq_install(&u
->thread_mq
);
873 pa_rtpoll_install(u
->rtpoll
);
878 /* pa_log("loop"); */
880 if (PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
))
881 if (u
->sink
->thread_info
.rewind_requested
)
882 pa_sink_process_rewind(u
->sink
, 0);
884 /* Render some data and write it to the dsp */
886 if (u
->sink
&& PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
) && ((revents
& POLLOUT
) || u
->use_mmap
|| u
->use_getospace
)) {
890 if ((ret
= mmap_write(u
)) < 0)
900 pa_bool_t loop
= FALSE
, work_done
= FALSE
;
902 l
= (ssize_t
) u
->out_fragment_size
;
904 if (u
->use_getospace
) {
907 if (ioctl(u
->fd
, SNDCTL_DSP_GETOSPACE
, &info
) < 0) {
908 pa_log_info("Device doesn't support SNDCTL_DSP_GETOSPACE: %s", pa_cstrerror(errno
));
909 u
->use_getospace
= FALSE
;
913 /* We loop only if GETOSPACE worked and we
914 * actually *know* that we can write more than
915 * one fragment at a time */
920 /* Round down to multiples of the fragment size,
921 * because OSS needs that (at least some versions
923 l
= (l
/(ssize_t
) u
->out_fragment_size
) * (ssize_t
) u
->out_fragment_size
;
925 /* Hmm, so poll() signalled us that we can read
926 * something, but GETOSPACE told us there was nothing?
927 * Hmm, make the best of it, try to read some data, to
928 * avoid spinning forever. */
929 if (l
<= 0 && (revents
& POLLOUT
)) {
930 l
= (ssize_t
) u
->out_fragment_size
;
938 if (u
->memchunk
.length
<= 0)
939 pa_sink_render(u
->sink
, (size_t) l
, &u
->memchunk
);
941 pa_assert(u
->memchunk
.length
> 0);
943 p
= pa_memblock_acquire(u
->memchunk
.memblock
);
944 t
= pa_write(u
->fd
, (uint8_t*) p
+ u
->memchunk
.index
, u
->memchunk
.length
, &write_type
);
945 pa_memblock_release(u
->memchunk
.memblock
);
947 /* pa_log("wrote %i bytes of %u", t, l); */
956 else if (errno
== EAGAIN
) {
957 pa_log_debug("EAGAIN");
963 pa_log("Failed to write data to DSP: %s", pa_cstrerror(errno
));
969 u
->memchunk
.index
+= (size_t) t
;
970 u
->memchunk
.length
-= (size_t) t
;
972 if (u
->memchunk
.length
<= 0) {
973 pa_memblock_unref(u
->memchunk
.memblock
);
974 pa_memchunk_reset(&u
->memchunk
);
992 /* Try to read some data and pass it on to the source driver. */
994 if (u
->source
&& PA_SOURCE_IS_OPENED(u
->source
->thread_info
.state
) && ((revents
& POLLIN
) || u
->use_mmap
|| u
->use_getispace
)) {
998 if ((ret
= mmap_read(u
)) < 0)
1010 pa_memchunk memchunk
;
1011 pa_bool_t loop
= FALSE
, work_done
= FALSE
;
1013 l
= (ssize_t
) u
->in_fragment_size
;
1015 if (u
->use_getispace
) {
1016 audio_buf_info info
;
1018 if (ioctl(u
->fd
, SNDCTL_DSP_GETISPACE
, &info
) < 0) {
1019 pa_log_info("Device doesn't support SNDCTL_DSP_GETISPACE: %s", pa_cstrerror(errno
));
1020 u
->use_getispace
= FALSE
;
1027 l
= (l
/(ssize_t
) u
->in_fragment_size
) * (ssize_t
) u
->in_fragment_size
;
1029 if (l
<= 0 && (revents
& POLLIN
)) {
1030 l
= (ssize_t
) u
->in_fragment_size
;
1040 memchunk
.memblock
= pa_memblock_new(u
->core
->mempool
, (size_t) -1);
1042 k
= pa_memblock_get_length(memchunk
.memblock
);
1047 k
= (k
/u
->frame_size
)*u
->frame_size
;
1049 p
= pa_memblock_acquire(memchunk
.memblock
);
1050 t
= pa_read(u
->fd
, p
, k
, &read_type
);
1051 pa_memblock_release(memchunk
.memblock
);
1053 pa_assert(t
!= 0); /* EOF cannot happen */
1055 /* pa_log("read %i bytes of %u", t, l); */
1058 pa_memblock_unref(memchunk
.memblock
);
1063 else if (errno
== EAGAIN
) {
1064 pa_log_debug("EAGAIN");
1070 pa_log("Failed to read data from DSP: %s", pa_cstrerror(errno
));
1076 memchunk
.length
= (size_t) t
;
1078 pa_source_post(u
->source
, &memchunk
);
1079 pa_memblock_unref(memchunk
.memblock
);
1096 /* pa_log("loop2 revents=%i", revents); */
1098 if (u
->rtpoll_item
) {
1099 struct pollfd
*pollfd
;
1101 pa_assert(u
->fd
>= 0);
1103 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
1104 pollfd
->events
= (short)
1105 (((u
->source
&& PA_SOURCE_IS_OPENED(u
->source
->thread_info
.state
)) ? POLLIN
: 0) |
1106 ((u
->sink
&& PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
)) ? POLLOUT
: 0));
1109 /* Hmm, nothing to do. Let's sleep */
1110 if ((ret
= pa_rtpoll_run(u
->rtpoll
, TRUE
)) < 0)
1116 if (u
->rtpoll_item
) {
1117 struct pollfd
*pollfd
;
1119 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
1121 if (pollfd
->revents
& ~(POLLOUT
|POLLIN
)) {
1122 pa_log("DSP shutdown.");
1126 revents
= pollfd
->revents
;
1132 /* If this was no regular exit from the loop we have to continue
1133 * processing messages until we received PA_MESSAGE_SHUTDOWN */
1134 pa_asyncmsgq_post(u
->thread_mq
.outq
, PA_MSGOBJECT(u
->core
), PA_CORE_MESSAGE_UNLOAD_MODULE
, u
->module
, 0, NULL
, NULL
);
1135 pa_asyncmsgq_wait_for(u
->thread_mq
.inq
, PA_MESSAGE_SHUTDOWN
);
1138 pa_log_debug("Thread shutting down");
1141 int pa__init(pa_module
*m
) {
1143 struct audio_buf_info info
;
1144 struct userdata
*u
= NULL
;
1147 int nfrags
, frag_size
;
1149 pa_bool_t record
= TRUE
, playback
= TRUE
, use_mmap
= TRUE
;
1152 pa_modargs
*ma
= NULL
;
1155 pa_bool_t namereg_fail
;
1156 pa_sink_new_data sink_new_data
;
1157 pa_source_new_data source_new_data
;
1161 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
1162 pa_log("Failed to parse module arguments.");
1166 if (pa_modargs_get_value_boolean(ma
, "record", &record
) < 0 || pa_modargs_get_value_boolean(ma
, "playback", &playback
) < 0) {
1167 pa_log("record= and playback= expect boolean argument.");
1171 if (!playback
&& !record
) {
1172 pa_log("Neither playback nor record enabled for device.");
1176 mode
= (playback
&& record
) ? O_RDWR
: (playback
? O_WRONLY
: (record
? O_RDONLY
: 0));
1178 ss
= m
->core
->default_sample_spec
;
1179 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &ss
, &map
, PA_CHANNEL_MAP_OSS
) < 0) {
1180 pa_log("Failed to parse sample specification or channel map");
1184 nfrags
= (int) m
->core
->default_n_fragments
;
1185 frag_size
= (int) pa_usec_to_bytes(m
->core
->default_fragment_size_msec
*1000, &ss
);
1187 frag_size
= (int) pa_frame_size(&ss
);
1189 if (pa_modargs_get_value_s32(ma
, "fragments", &nfrags
) < 0 || pa_modargs_get_value_s32(ma
, "fragment_size", &frag_size
) < 0) {
1190 pa_log("Failed to parse fragments arguments");
1194 if (pa_modargs_get_value_boolean(ma
, "mmap", &use_mmap
) < 0) {
1195 pa_log("Failed to parse mmap argument.");
1199 if ((fd
= pa_oss_open(dev
= pa_modargs_get_value(ma
, "device", DEFAULT_DEVICE
), &mode
, &caps
)) < 0)
1202 if (use_mmap
&& (!(caps
& DSP_CAP_MMAP
) || !(caps
& DSP_CAP_TRIGGER
))) {
1203 pa_log_info("OSS device not mmap capable, falling back to UNIX read/write mode.");
1207 if (use_mmap
&& mode
== O_WRONLY
) {
1208 pa_log_info("Device opened for playback only, cannot do memory mapping, falling back to UNIX write() mode.");
1212 if (pa_oss_get_hw_description(dev
, hwdesc
, sizeof(hwdesc
)) >= 0)
1213 pa_log_info("Hardware name is '%s'.", hwdesc
);
1217 pa_log_info("Device opened in %s mode.", mode
== O_WRONLY
? "O_WRONLY" : (mode
== O_RDONLY
? "O_RDONLY" : "O_RDWR"));
1219 if (nfrags
>= 2 && frag_size
>= 1)
1220 if (pa_oss_set_fragments(fd
, nfrags
, frag_size
) < 0)
1223 if (pa_oss_auto_format(fd
, &ss
) < 0)
1226 if (ioctl(fd
, SNDCTL_DSP_GETBLKSIZE
, &frag_size
) < 0) {
1227 pa_log("SNDCTL_DSP_GETBLKSIZE: %s", pa_cstrerror(errno
));
1230 pa_assert(frag_size
> 0);
1232 u
= pa_xnew0(struct userdata
, 1);
1238 u
->use_getospace
= u
->use_getispace
= TRUE
;
1239 u
->use_getodelay
= TRUE
;
1241 u
->frame_size
= pa_frame_size(&ss
);
1242 u
->device_name
= pa_xstrdup(dev
);
1243 u
->in_nfrags
= u
->out_nfrags
= (uint32_t) (u
->nfrags
= nfrags
);
1244 u
->out_fragment_size
= u
->in_fragment_size
= (uint32_t) (u
->frag_size
= frag_size
);
1245 u
->use_mmap
= use_mmap
;
1246 u
->rtpoll
= pa_rtpoll_new();
1247 pa_thread_mq_init(&u
->thread_mq
, m
->core
->mainloop
, u
->rtpoll
);
1248 u
->rtpoll_item
= NULL
;
1251 if (ioctl(fd
, SNDCTL_DSP_GETISPACE
, &info
) >= 0) {
1252 pa_log_info("Input -- %u fragments of size %u.", info
.fragstotal
, info
.fragsize
);
1253 u
->in_fragment_size
= (uint32_t) info
.fragsize
;
1254 u
->in_nfrags
= (uint32_t) info
.fragstotal
;
1255 u
->use_getispace
= TRUE
;
1258 if (ioctl(fd
, SNDCTL_DSP_GETOSPACE
, &info
) >= 0) {
1259 pa_log_info("Output -- %u fragments of size %u.", info
.fragstotal
, info
.fragsize
);
1260 u
->out_fragment_size
= (uint32_t) info
.fragsize
;
1261 u
->out_nfrags
= (uint32_t) info
.fragstotal
;
1262 u
->use_getospace
= TRUE
;
1265 u
->in_hwbuf_size
= u
->in_nfrags
* u
->in_fragment_size
;
1266 u
->out_hwbuf_size
= u
->out_nfrags
* u
->out_fragment_size
;
1268 if (mode
!= O_WRONLY
) {
1269 char *name_buf
= NULL
;
1272 if ((u
->in_mmap
= mmap(NULL
, u
->in_hwbuf_size
, PROT_READ
, MAP_SHARED
, fd
, 0)) == MAP_FAILED
) {
1273 pa_log_warn("mmap(PROT_READ) failed, reverting to non-mmap mode: %s", pa_cstrerror(errno
));
1274 use_mmap
= u
->use_mmap
= FALSE
;
1277 pa_log_debug("Successfully mmap()ed input buffer.");
1280 if ((name
= pa_modargs_get_value(ma
, "source_name", NULL
)))
1281 namereg_fail
= TRUE
;
1283 name
= name_buf
= pa_sprintf_malloc("oss_input.%s", pa_path_get_filename(dev
));
1284 namereg_fail
= FALSE
;
1287 pa_source_new_data_init(&source_new_data
);
1288 source_new_data
.driver
= __FILE__
;
1289 source_new_data
.module
= m
;
1290 pa_source_new_data_set_name(&source_new_data
, name
);
1291 source_new_data
.namereg_fail
= namereg_fail
;
1292 pa_source_new_data_set_sample_spec(&source_new_data
, &ss
);
1293 pa_source_new_data_set_channel_map(&source_new_data
, &map
);
1294 pa_proplist_sets(source_new_data
.proplist
, PA_PROP_DEVICE_STRING
, dev
);
1295 pa_proplist_sets(source_new_data
.proplist
, PA_PROP_DEVICE_API
, "oss");
1296 pa_proplist_sets(source_new_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, hwdesc
[0] ? hwdesc
: dev
);
1297 pa_proplist_sets(source_new_data
.proplist
, PA_PROP_DEVICE_ACCESS_MODE
, use_mmap
? "mmap" : "serial");
1298 pa_proplist_setf(source_new_data
.proplist
, PA_PROP_DEVICE_BUFFERING_BUFFER_SIZE
, "%lu", (unsigned long) (u
->in_hwbuf_size
));
1299 pa_proplist_setf(source_new_data
.proplist
, PA_PROP_DEVICE_BUFFERING_FRAGMENT_SIZE
, "%lu", (unsigned long) (u
->in_fragment_size
));
1301 u
->source
= pa_source_new(m
->core
, &source_new_data
, PA_SOURCE_HARDWARE
|PA_SOURCE_LATENCY
);
1302 pa_source_new_data_done(&source_new_data
);
1306 pa_log("Failed to create source object");
1310 u
->source
->parent
.process_msg
= source_process_msg
;
1311 u
->source
->userdata
= u
;
1313 pa_source_set_asyncmsgq(u
->source
, u
->thread_mq
.inq
);
1314 pa_source_set_rtpoll(u
->source
, u
->rtpoll
);
1315 u
->source
->refresh_volume
= TRUE
;
1318 u
->in_mmap_memblocks
= pa_xnew0(pa_memblock
*, u
->in_nfrags
);
1321 if (mode
!= O_RDONLY
) {
1322 char *name_buf
= NULL
;
1325 if ((u
->out_mmap
= mmap(NULL
, u
->out_hwbuf_size
, PROT_WRITE
, MAP_SHARED
, fd
, 0)) == MAP_FAILED
) {
1326 if (mode
== O_RDWR
) {
1327 pa_log_debug("mmap() failed for input. Changing to O_WRONLY mode.");
1331 pa_log_warn("mmap(PROT_WRITE) failed, reverting to non-mmap mode: %s", pa_cstrerror(errno
));
1332 u
->use_mmap
= use_mmap
= FALSE
;
1336 pa_log_debug("Successfully mmap()ed output buffer.");
1337 pa_silence_memory(u
->out_mmap
, u
->out_hwbuf_size
, &ss
);
1341 if ((name
= pa_modargs_get_value(ma
, "sink_name", NULL
)))
1342 namereg_fail
= TRUE
;
1344 name
= name_buf
= pa_sprintf_malloc("oss_output.%s", pa_path_get_filename(dev
));
1345 namereg_fail
= FALSE
;
1348 pa_sink_new_data_init(&sink_new_data
);
1349 sink_new_data
.driver
= __FILE__
;
1350 sink_new_data
.module
= m
;
1351 pa_sink_new_data_set_name(&sink_new_data
, name
);
1352 sink_new_data
.namereg_fail
= namereg_fail
;
1353 pa_sink_new_data_set_sample_spec(&sink_new_data
, &ss
);
1354 pa_sink_new_data_set_channel_map(&sink_new_data
, &map
);
1355 pa_proplist_sets(sink_new_data
.proplist
, PA_PROP_DEVICE_STRING
, dev
);
1356 pa_proplist_sets(sink_new_data
.proplist
, PA_PROP_DEVICE_API
, "oss");
1357 pa_proplist_sets(sink_new_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, hwdesc
[0] ? hwdesc
: dev
);
1358 pa_proplist_sets(sink_new_data
.proplist
, PA_PROP_DEVICE_ACCESS_MODE
, use_mmap
? "mmap" : "serial");
1359 pa_proplist_setf(sink_new_data
.proplist
, PA_PROP_DEVICE_BUFFERING_BUFFER_SIZE
, "%lu", (unsigned long) (u
->out_hwbuf_size
));
1360 pa_proplist_setf(sink_new_data
.proplist
, PA_PROP_DEVICE_BUFFERING_FRAGMENT_SIZE
, "%lu", (unsigned long) (u
->out_fragment_size
));
1362 u
->sink
= pa_sink_new(m
->core
, &sink_new_data
, PA_SINK_HARDWARE
|PA_SINK_LATENCY
);
1363 pa_sink_new_data_done(&sink_new_data
);
1367 pa_log("Failed to create sink object");
1371 u
->sink
->parent
.process_msg
= sink_process_msg
;
1372 u
->sink
->userdata
= u
;
1374 pa_sink_set_asyncmsgq(u
->sink
, u
->thread_mq
.inq
);
1375 pa_sink_set_rtpoll(u
->sink
, u
->rtpoll
);
1376 u
->sink
->refresh_volume
= TRUE
;
1378 u
->sink
->thread_info
.max_request
= u
->out_hwbuf_size
;
1381 u
->out_mmap_memblocks
= pa_xnew0(pa_memblock
*, u
->out_nfrags
);
1384 if ((u
->mixer_fd
= pa_oss_open_mixer_for_device(u
->device_name
)) >= 0) {
1385 pa_bool_t do_close
= TRUE
;
1386 u
->mixer_devmask
= 0;
1388 if (ioctl(fd
, SOUND_MIXER_READ_DEVMASK
, &u
->mixer_devmask
) < 0)
1389 pa_log_warn("SOUND_MIXER_READ_DEVMASK failed: %s", pa_cstrerror(errno
));
1392 if (u
->sink
&& (u
->mixer_devmask
& (SOUND_MASK_VOLUME
|SOUND_MASK_PCM
))) {
1393 pa_log_debug("Found hardware mixer track for playback.");
1394 u
->sink
->flags
|= PA_SINK_HW_VOLUME_CTRL
;
1395 u
->sink
->get_volume
= sink_get_volume
;
1396 u
->sink
->set_volume
= sink_set_volume
;
1400 if (u
->source
&& (u
->mixer_devmask
& (SOUND_MASK_RECLEV
|SOUND_MASK_IGAIN
))) {
1401 pa_log_debug("Found hardware mixer track for recording.");
1402 u
->source
->flags
|= PA_SOURCE_HW_VOLUME_CTRL
;
1403 u
->source
->get_volume
= source_get_volume
;
1404 u
->source
->set_volume
= source_set_volume
;
1410 pa_close(u
->mixer_fd
);
1417 pa_assert(u
->source
|| u
->sink
);
1419 pa_memchunk_reset(&u
->memchunk
);
1421 if (!(u
->thread
= pa_thread_new(thread_func
, u
))) {
1422 pa_log("Failed to create thread.");
1426 /* Read mixer settings */
1428 if (sink_new_data
.volume_is_set
) {
1429 if (u
->sink
->set_volume
)
1430 u
->sink
->set_volume(u
->sink
);
1432 if (u
->sink
->get_volume
)
1433 u
->sink
->get_volume(u
->sink
);
1438 if (source_new_data
.volume_is_set
) {
1439 if (u
->source
->set_volume
)
1440 u
->source
->set_volume(u
->source
);
1442 if (u
->source
->get_volume
)
1443 u
->source
->get_volume(u
->source
);
1448 pa_sink_put(u
->sink
);
1450 pa_source_put(u
->source
);
1452 pa_modargs_free(ma
);
1464 pa_modargs_free(ma
);
1469 void pa__done(pa_module
*m
) {
1474 if (!(u
= m
->userdata
))
1478 pa_sink_unlink(u
->sink
);
1481 pa_source_unlink(u
->source
);
1484 pa_asyncmsgq_send(u
->thread_mq
.inq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, 0, NULL
);
1485 pa_thread_free(u
->thread
);
1488 pa_thread_mq_done(&u
->thread_mq
);
1491 pa_sink_unref(u
->sink
);
1494 pa_source_unref(u
->source
);
1496 if (u
->memchunk
.memblock
)
1497 pa_memblock_unref(u
->memchunk
.memblock
);
1500 pa_rtpoll_item_free(u
->rtpoll_item
);
1503 pa_rtpoll_free(u
->rtpoll
);
1505 if (u
->out_mmap_memblocks
) {
1507 for (i
= 0; i
< u
->out_nfrags
; i
++)
1508 if (u
->out_mmap_memblocks
[i
])
1509 pa_memblock_unref_fixed(u
->out_mmap_memblocks
[i
]);
1510 pa_xfree(u
->out_mmap_memblocks
);
1513 if (u
->in_mmap_memblocks
) {
1515 for (i
= 0; i
< u
->in_nfrags
; i
++)
1516 if (u
->in_mmap_memblocks
[i
])
1517 pa_memblock_unref_fixed(u
->in_mmap_memblocks
[i
]);
1518 pa_xfree(u
->in_mmap_memblocks
);
1521 if (u
->in_mmap
&& u
->in_mmap
!= MAP_FAILED
)
1522 munmap(u
->in_mmap
, u
->in_hwbuf_size
);
1524 if (u
->out_mmap
&& u
->out_mmap
!= MAP_FAILED
)
1525 munmap(u
->out_mmap
, u
->out_hwbuf_size
);
1530 if (u
->mixer_fd
>= 0)
1531 pa_close(u
->mixer_fd
);
1533 pa_xfree(u
->device_name
);