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 #ifdef _FILE_OFFSET_BITS
27 #undef _FILE_OFFSET_BITS
30 #ifndef _LARGEFILE64_SOURCE
31 #define _LARGEFILE64_SOURCE 1
34 #include <sys/soundcard.h>
35 #include <sys/ioctl.h>
38 #include <sys/socket.h>
47 #include <linux/sockios.h>
49 #include <polyp/polypaudio.h>
50 #include <polypcore/llist.h>
51 #include <polypcore/gccmacro.h>
58 typedef struct fd_info fd_info
;
61 pthread_mutex_t mutex
;
66 int app_fd
, thread_fd
;
68 pa_sample_spec sample_spec
;
72 pa_threaded_mainloop
*mainloop
;
76 pa_io_event
*io_event
;
80 int operation_success
;
84 int volume_modify_count
;
86 PA_LLIST_FIELDS(fd_info
);
89 static int dsp_drain(fd_info
*i
);
90 static void fd_info_remove_from_list(fd_info
*i
);
92 static pthread_mutex_t fd_infos_mutex
= PTHREAD_MUTEX_INITIALIZER
;
93 static pthread_mutex_t func_mutex
= PTHREAD_MUTEX_INITIALIZER
;
95 static PA_LLIST_HEAD(fd_info
, fd_infos
) = NULL
;
97 static int (*_ioctl
)(int, int, void*) = NULL
;
98 static int (*_close
)(int) = NULL
;
99 static int (*_open
)(const char *, int, mode_t
) = NULL
;
100 static FILE* (*_fopen
)(const char *path
, const char *mode
) = NULL
;
101 static int (*_open64
)(const char *, int, mode_t
) = NULL
;
102 static FILE* (*_fopen64
)(const char *path
, const char *mode
) = NULL
;
103 static int (*_fclose
)(FILE *f
) = NULL
;
105 /* dlsym() violates ISO C, so confide the breakage into this function to
107 typedef void (*fnptr
)(void);
108 static inline fnptr
dlsym_fn(void *handle
, const char *symbol
) {
109 return (fnptr
) (long) dlsym(handle
, symbol
);
112 #define LOAD_IOCTL_FUNC() \
114 pthread_mutex_lock(&func_mutex); \
116 _ioctl = (int (*)(int, int, void*)) dlsym_fn(RTLD_NEXT, "ioctl"); \
117 pthread_mutex_unlock(&func_mutex); \
120 #define LOAD_OPEN_FUNC() \
122 pthread_mutex_lock(&func_mutex); \
124 _open = (int (*)(const char *, int, mode_t)) dlsym_fn(RTLD_NEXT, "open"); \
125 pthread_mutex_unlock(&func_mutex); \
128 #define LOAD_OPEN64_FUNC() \
130 pthread_mutex_lock(&func_mutex); \
132 _open64 = (int (*)(const char *, int, mode_t)) dlsym_fn(RTLD_NEXT, "open64"); \
133 pthread_mutex_unlock(&func_mutex); \
136 #define LOAD_CLOSE_FUNC() \
138 pthread_mutex_lock(&func_mutex); \
140 _close = (int (*)(int)) dlsym_fn(RTLD_NEXT, "close"); \
141 pthread_mutex_unlock(&func_mutex); \
144 #define LOAD_FOPEN_FUNC() \
146 pthread_mutex_lock(&func_mutex); \
148 _fopen = (FILE* (*)(const char *, const char*)) dlsym_fn(RTLD_NEXT, "fopen"); \
149 pthread_mutex_unlock(&func_mutex); \
152 #define LOAD_FOPEN64_FUNC() \
154 pthread_mutex_lock(&func_mutex); \
156 _fopen64 = (FILE* (*)(const char *, const char*)) dlsym_fn(RTLD_NEXT, "fopen64"); \
157 pthread_mutex_unlock(&func_mutex); \
160 #define LOAD_FCLOSE_FUNC() \
162 pthread_mutex_lock(&func_mutex); \
164 _fclose = (int (*)(FILE *)) dlsym_fn(RTLD_NEXT, "fclose"); \
165 pthread_mutex_unlock(&func_mutex); \
168 #define CONTEXT_CHECK_DEAD_GOTO(i, label) do { \
169 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY) { \
170 debug(__FILE__": Not connected: %s", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
175 #define STREAM_CHECK_DEAD_GOTO(i, label) do { \
176 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY || \
177 !(i)->stream || pa_stream_get_state((i)->stream) != PA_STREAM_READY) { \
178 debug(__FILE__": Not connected: %s", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
183 static void debug(const char *format
, ...) PA_GCC_PRINTF_ATTR(1,2);
185 static void debug(const char *format
, ...) {
187 if (getenv("PADSP_DEBUG")) {
188 va_start(ap
, format
);
189 vfprintf(stderr
, format
, ap
);
194 static int padsp_disabled(void) {
196 static int sym_resolved
= 0;
198 /* If the current process has a symbol __padsp_disabled__ we use
199 * it to detect whether we should enable our stuff or not. A
200 * program needs to be compiled with -rdynamic for this to work!
201 * The symbol must be an int containing a three bit bitmask: bit 1
202 * -> disable /dev/dsp emulation, bit 2 -> disable /dev/sndstat
203 * emulation, bit 3 -> disable /dev/mixer emulation. Hence a value
204 * of 7 disables padsp entirely. */
206 pthread_mutex_lock(&func_mutex
);
208 sym
= (int*) dlsym(RTLD_DEFAULT
, "__padsp_disabled__");
212 pthread_mutex_unlock(&func_mutex
);
220 static int dsp_cloak_enable(void) {
221 if (padsp_disabled() & 1)
224 if (getenv("PADSP_NO_DSP"))
230 static int sndstat_cloak_enable(void) {
231 if (padsp_disabled() & 2)
234 if (getenv("PADSP_NO_SNDSTAT"))
240 static int mixer_cloak_enable(void) {
241 if (padsp_disabled() & 4)
244 if (getenv("PADSP_NO_MIXER"))
249 static pthread_key_t recursion_key
;
251 static void recursion_key_alloc(void) {
252 pthread_key_create(&recursion_key
, NULL
);
255 static int function_enter(void) {
256 /* Avoid recursive calls */
257 static pthread_once_t recursion_key_once
= PTHREAD_ONCE_INIT
;
258 pthread_once(&recursion_key_once
, recursion_key_alloc
);
260 if (pthread_getspecific(recursion_key
))
263 pthread_setspecific(recursion_key
, (void*) 1);
267 static void function_exit(void) {
268 pthread_setspecific(recursion_key
, NULL
);
271 static void fd_info_free(fd_info
*i
) {
274 debug(__FILE__
": freeing fd info (fd=%i)\n", i
->app_fd
);
279 pa_threaded_mainloop_stop(i
->mainloop
);
282 pa_stream_disconnect(i
->stream
);
283 pa_stream_unref(i
->stream
);
287 pa_context_disconnect(i
->context
);
288 pa_context_unref(i
->context
);
292 pa_threaded_mainloop_free(i
->mainloop
);
294 if (i
->app_fd
>= 0) {
299 if (i
->thread_fd
>= 0) {
301 _close(i
->thread_fd
);
306 pthread_mutex_destroy(&i
->mutex
);
310 static fd_info
*fd_info_ref(fd_info
*i
) {
313 pthread_mutex_lock(&i
->mutex
);
317 /* debug(__FILE__": ref++, now %i\n", i->ref); */
318 pthread_mutex_unlock(&i
->mutex
);
323 static void fd_info_unref(fd_info
*i
) {
325 pthread_mutex_lock(&i
->mutex
);
328 /* debug(__FILE__": ref--, now %i\n", i->ref); */
329 pthread_mutex_unlock(&i
->mutex
);
335 static void context_state_cb(pa_context
*c
, void *userdata
) {
336 fd_info
*i
= userdata
;
339 switch (pa_context_get_state(c
)) {
340 case PA_CONTEXT_READY
:
341 case PA_CONTEXT_TERMINATED
:
342 case PA_CONTEXT_FAILED
:
343 pa_threaded_mainloop_signal(i
->mainloop
, 0);
346 case PA_CONTEXT_UNCONNECTED
:
347 case PA_CONTEXT_CONNECTING
:
348 case PA_CONTEXT_AUTHORIZING
:
349 case PA_CONTEXT_SETTING_NAME
:
354 static void reset_params(fd_info
*i
) {
357 i
->sample_spec
.format
= PA_SAMPLE_ULAW
;
358 i
->sample_spec
.channels
= 1;
359 i
->sample_spec
.rate
= 8000;
360 i
->fragment_size
= 0;
364 static const char *client_name(char *buf
, size_t n
) {
368 if ((e
= getenv("PADSP_CLIENT_NAME")))
371 if (pa_get_binary_name(p
, sizeof(p
)))
372 snprintf(buf
, n
, "OSS Emulation[%s]", pa_path_get_filename(p
));
374 snprintf(buf
, n
, "OSS");
379 static const char *stream_name(void) {
382 if ((e
= getenv("PADSP_STREAM_NAME")))
385 return "Audio Stream";
388 static void atfork_prepare(void) {
391 debug(__FILE__
": atfork_prepare() enter\n");
395 pthread_mutex_lock(&fd_infos_mutex
);
397 for (i
= fd_infos
; i
; i
= i
->next
) {
398 pthread_mutex_lock(&i
->mutex
);
399 pa_threaded_mainloop_lock(i
->mainloop
);
402 pthread_mutex_lock(&func_mutex
);
405 debug(__FILE__
": atfork_prepare() exit\n");
408 static void atfork_parent(void) {
411 debug(__FILE__
": atfork_parent() enter\n");
413 pthread_mutex_unlock(&func_mutex
);
415 for (i
= fd_infos
; i
; i
= i
->next
) {
416 pa_threaded_mainloop_unlock(i
->mainloop
);
417 pthread_mutex_unlock(&i
->mutex
);
420 pthread_mutex_unlock(&fd_infos_mutex
);
424 debug(__FILE__
": atfork_parent() exit\n");
427 static void atfork_child(void) {
430 debug(__FILE__
": atfork_child() enter\n");
432 /* We do only the bare minimum to get all fds closed */
433 pthread_mutex_init(&func_mutex
, NULL
);
434 pthread_mutex_init(&fd_infos_mutex
, NULL
);
436 for (i
= fd_infos
; i
; i
= i
->next
) {
437 pthread_mutex_init(&i
->mutex
, NULL
);
440 pa_context_disconnect(i
->context
);
441 pa_context_unref(i
->context
);
446 pa_stream_unref(i
->stream
);
450 if (i
->app_fd
>= 0) {
455 if (i
->thread_fd
>= 0) {
465 debug(__FILE__
": atfork_child() exit\n");
468 static void install_atfork(void) {
469 pthread_atfork(atfork_prepare
, atfork_parent
, atfork_child
);
472 static void stream_success_cb(pa_stream
*s
, int success
, void *userdata
) {
473 fd_info
*i
= userdata
;
478 i
->operation_success
= success
;
479 pa_threaded_mainloop_signal(i
->mainloop
, 0);
482 static void context_success_cb(pa_context
*c
, int success
, void *userdata
) {
483 fd_info
*i
= userdata
;
488 i
->operation_success
= success
;
489 pa_threaded_mainloop_signal(i
->mainloop
, 0);
492 static fd_info
* fd_info_new(fd_info_type_t type
, int *_errno
) {
494 int sfds
[2] = { -1, -1 };
496 static pthread_once_t install_atfork_once
= PTHREAD_ONCE_INIT
;
498 debug(__FILE__
": fd_info_new()\n");
500 signal(SIGPIPE
, SIG_IGN
); /* Yes, ugly as hell */
502 pthread_once(&install_atfork_once
, install_atfork
);
504 if (!(i
= malloc(sizeof(fd_info
)))) {
509 i
->app_fd
= i
->thread_fd
= -1;
516 pthread_mutex_init(&i
->mutex
, NULL
);
520 pa_cvolume_reset(&i
->volume
, 2);
521 i
->volume_modify_count
= 0;
522 i
->sink_index
= (uint32_t) -1;
523 PA_LLIST_INIT(fd_info
, i
);
527 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, sfds
) < 0) {
529 debug(__FILE__
": socket() failed: %s\n", strerror(errno
));
534 i
->thread_fd
= sfds
[1];
536 if (!(i
->mainloop
= pa_threaded_mainloop_new())) {
538 debug(__FILE__
": pa_threaded_mainloop_new() failed\n");
542 if (!(i
->context
= pa_context_new(pa_threaded_mainloop_get_api(i
->mainloop
), client_name(name
, sizeof(name
))))) {
544 debug(__FILE__
": pa_context_new() failed\n");
548 pa_context_set_state_callback(i
->context
, context_state_cb
, i
);
550 if (pa_context_connect(i
->context
, NULL
, 0, NULL
) < 0) {
551 *_errno
= ECONNREFUSED
;
552 debug(__FILE__
": pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
556 pa_threaded_mainloop_lock(i
->mainloop
);
558 if (pa_threaded_mainloop_start(i
->mainloop
) < 0) {
560 debug(__FILE__
": pa_threaded_mainloop_start() failed\n");
561 goto unlock_and_fail
;
564 /* Wait until the context is ready */
565 pa_threaded_mainloop_wait(i
->mainloop
);
567 if (pa_context_get_state(i
->context
) != PA_CONTEXT_READY
) {
568 *_errno
= ECONNREFUSED
;
569 debug(__FILE__
": pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
570 goto unlock_and_fail
;
573 pa_threaded_mainloop_unlock(i
->mainloop
);
578 pa_threaded_mainloop_unlock(i
->mainloop
);
588 static void fd_info_add_to_list(fd_info
*i
) {
591 pthread_mutex_lock(&fd_infos_mutex
);
592 PA_LLIST_PREPEND(fd_info
, fd_infos
, i
);
593 pthread_mutex_unlock(&fd_infos_mutex
);
598 static void fd_info_remove_from_list(fd_info
*i
) {
601 pthread_mutex_lock(&fd_infos_mutex
);
602 PA_LLIST_REMOVE(fd_info
, fd_infos
, i
);
603 pthread_mutex_unlock(&fd_infos_mutex
);
608 static fd_info
* fd_info_find(int fd
) {
611 pthread_mutex_lock(&fd_infos_mutex
);
613 for (i
= fd_infos
; i
; i
= i
->next
)
614 if (i
->app_fd
== fd
&& !i
->unusable
) {
619 pthread_mutex_unlock(&fd_infos_mutex
);
624 static void fix_metrics(fd_info
*i
) {
626 char t
[PA_SAMPLE_SPEC_SNPRINT_MAX
];
628 fs
= pa_frame_size(&i
->sample_spec
);
629 i
->fragment_size
= (i
->fragment_size
/fs
)*fs
;
631 /* Number of fragments set? */
632 if (i
->n_fragments
< 2) {
633 if (i
->fragment_size
> 0) {
634 i
->n_fragments
= pa_bytes_per_second(&i
->sample_spec
) / 2 / i
->fragment_size
;
635 if (i
->n_fragments
< 2)
641 /* Fragment size set? */
642 if (i
->fragment_size
<= 0) {
643 i
->fragment_size
= pa_bytes_per_second(&i
->sample_spec
) / 2 / i
->n_fragments
;
644 if (i
->fragment_size
< 1024)
645 i
->fragment_size
= 1024;
648 debug(__FILE__
": sample spec: %s\n", pa_sample_spec_snprint(t
, sizeof(t
), &i
->sample_spec
));
649 debug(__FILE__
": fixated metrics to %i fragments, %li bytes each.\n", i
->n_fragments
, (long)i
->fragment_size
);
652 static void stream_request_cb(pa_stream
*s
, size_t length
, void *userdata
) {
653 fd_info
*i
= userdata
;
657 pa_mainloop_api
*api
;
658 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
659 api
->io_enable(i
->io_event
, PA_IO_EVENT_INPUT
);
663 static void stream_latency_update_cb(pa_stream
*s
, void *userdata
) {
664 fd_info
*i
= userdata
;
667 pa_threaded_mainloop_signal(i
->mainloop
, 0);
670 static void fd_info_shutdown(fd_info
*i
) {
674 pa_mainloop_api
*api
;
675 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
676 api
->io_free(i
->io_event
);
680 if (i
->thread_fd
>= 0) {
686 static int fd_info_copy_data(fd_info
*i
, int force
) {
692 if ((n
= pa_stream_writable_size(i
->stream
)) == (size_t) -1) {
693 debug(__FILE__
": pa_stream_writable_size(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
697 while (n
>= i
->fragment_size
|| force
) {
701 if (!(i
->buf
= malloc(i
->fragment_size
))) {
702 debug(__FILE__
": malloc() failed.\n");
707 if ((r
= read(i
->thread_fd
, i
->buf
, i
->fragment_size
)) <= 0) {
712 debug(__FILE__
": read(): %s\n", r
== 0 ? "EOF" : strerror(errno
));
716 if (pa_stream_write(i
->stream
, i
->buf
, r
, free
, 0, PA_SEEK_RELATIVE
) < 0) {
717 debug(__FILE__
": pa_stream_write(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
723 assert(n
>= (size_t) r
);
728 pa_mainloop_api
*api
;
729 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
730 api
->io_enable(i
->io_event
, n
>= i
->fragment_size
? PA_IO_EVENT_INPUT
: 0);
736 static void stream_state_cb(pa_stream
*s
, void * userdata
) {
737 fd_info
*i
= userdata
;
740 switch (pa_stream_get_state(s
)) {
742 case PA_STREAM_READY
:
743 debug(__FILE__
": stream established.\n");
746 case PA_STREAM_FAILED
:
747 debug(__FILE__
": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
751 case PA_STREAM_TERMINATED
:
752 case PA_STREAM_UNCONNECTED
:
753 case PA_STREAM_CREATING
:
758 static int create_stream(fd_info
*i
) {
766 if (!(i
->stream
= pa_stream_new(i
->context
, stream_name(), &i
->sample_spec
, NULL
))) {
767 debug(__FILE__
": pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
771 pa_stream_set_state_callback(i
->stream
, stream_state_cb
, i
);
772 pa_stream_set_write_callback(i
->stream
, stream_request_cb
, i
);
773 pa_stream_set_latency_update_callback(i
->stream
, stream_latency_update_cb
, i
);
775 memset(&attr
, 0, sizeof(attr
));
776 attr
.maxlength
= i
->fragment_size
* (i
->n_fragments
+1);
777 attr
.tlength
= i
->fragment_size
* i
->n_fragments
;
778 attr
.prebuf
= i
->fragment_size
;
779 attr
.minreq
= i
->fragment_size
;
781 if (pa_stream_connect_playback(i
->stream
, NULL
, &attr
, PA_STREAM_INTERPOLATE_TIMING
|PA_STREAM_AUTO_TIMING_UPDATE
, NULL
, NULL
) < 0) {
782 debug(__FILE__
": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
786 n
= i
->fragment_size
;
787 setsockopt(i
->app_fd
, SOL_SOCKET
, SO_SNDBUF
, &n
, sizeof(n
));
788 n
= i
->fragment_size
;
789 setsockopt(i
->thread_fd
, SOL_SOCKET
, SO_RCVBUF
, &n
, sizeof(n
));
797 static void free_stream(fd_info
*i
) {
801 pa_stream_disconnect(i
->stream
);
802 pa_stream_unref(i
->stream
);
807 static void io_event_cb(pa_mainloop_api
*api
, pa_io_event
*e
, int fd
, pa_io_event_flags_t flags
, void *userdata
) {
808 fd_info
*i
= userdata
;
810 pa_threaded_mainloop_signal(i
->mainloop
, 0);
812 if (flags
& PA_IO_EVENT_INPUT
) {
815 api
->io_enable(e
, 0);
817 if (create_stream(i
) < 0)
821 if (fd_info_copy_data(i
, 0) < 0)
825 } else if (flags
& (PA_IO_EVENT_HANGUP
|PA_IO_EVENT_ERROR
))
831 /* We can't do anything better than removing the event source */
835 static int dsp_open(int flags
, int *_errno
) {
837 pa_mainloop_api
*api
;
841 if ((flags
!= O_WRONLY
) && (flags
!= (O_WRONLY
|O_NONBLOCK
))) {
846 if (!(i
= fd_info_new(FD_INFO_PLAYBACK
, _errno
)))
849 shutdown(i
->thread_fd
, SHUT_WR
);
850 shutdown(i
->app_fd
, SHUT_RD
);
852 if ((flags
& O_NONBLOCK
) == O_NONBLOCK
) {
853 if ((f
= fcntl(i
->app_fd
, F_GETFL
)) >= 0)
854 fcntl(i
->app_fd
, F_SETFL
, f
|O_NONBLOCK
);
856 if ((f
= fcntl(i
->thread_fd
, F_GETFL
)) >= 0)
857 fcntl(i
->thread_fd
, F_SETFL
, f
|O_NONBLOCK
);
859 fcntl(i
->app_fd
, F_SETFD
, FD_CLOEXEC
);
860 fcntl(i
->thread_fd
, F_SETFD
, FD_CLOEXEC
);
862 pa_threaded_mainloop_lock(i
->mainloop
);
863 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
864 if (!(i
->io_event
= api
->io_new(api
, i
->thread_fd
, PA_IO_EVENT_INPUT
, io_event_cb
, i
)))
867 pa_threaded_mainloop_unlock(i
->mainloop
);
869 debug(__FILE__
": dsp_open() succeeded, fd=%i\n", i
->app_fd
);
871 fd_info_add_to_list(i
);
878 pa_threaded_mainloop_unlock(i
->mainloop
);
885 debug(__FILE__
": dsp_open() failed\n");
890 static void sink_info_cb(pa_context
*context
, const pa_sink_info
*si
, int eol
, void *userdata
) {
891 fd_info
*i
= userdata
;
893 if (!si
&& eol
< 0) {
894 i
->operation_success
= 0;
895 pa_threaded_mainloop_signal(i
->mainloop
, 0);
902 if (!pa_cvolume_equal(&i
->volume
, &si
->volume
))
903 i
->volume_modify_count
++;
905 i
->volume
= si
->volume
;
906 i
->sink_index
= si
->index
;
908 i
->operation_success
= 1;
909 pa_threaded_mainloop_signal(i
->mainloop
, 0);
912 static void subscribe_cb(pa_context
*context
, pa_subscription_event_type_t t
, uint32_t idx
, void *userdata
) {
913 fd_info
*i
= userdata
;
914 pa_operation
*o
= NULL
;
916 if (i
->sink_index
!= idx
)
919 if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) != PA_SUBSCRIPTION_EVENT_CHANGE
)
922 if (!(o
= pa_context_get_sink_info_by_index(i
->context
, i
->sink_index
, sink_info_cb
, i
))) {
923 debug(__FILE__
": Failed to get sink info: %s", pa_strerror(pa_context_errno(i
->context
)));
927 pa_operation_unref(o
);
930 static int mixer_open(int flags
, int *_errno
) {
935 if (!(i
= fd_info_new(FD_INFO_MIXER
, _errno
)))
938 pa_threaded_mainloop_lock(i
->mainloop
);
940 pa_context_set_subscribe_callback(i
->context
, subscribe_cb
, i
);
942 if (!(o
= pa_context_subscribe(i
->context
, PA_SUBSCRIPTION_MASK_SINK
, context_success_cb
, i
))) {
943 debug(__FILE__
": Failed to subscribe to events: %s", pa_strerror(pa_context_errno(i
->context
)));
948 i
->operation_success
= 0;
949 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
950 pa_threaded_mainloop_wait(i
->mainloop
);
951 CONTEXT_CHECK_DEAD_GOTO(i
, fail
);
954 if (!i
->operation_success
) {
955 debug(__FILE__
":Failed to subscribe to events: %s", pa_strerror(pa_context_errno(i
->context
)));
962 pa_operation_unref(o
);
963 if (!(o
= pa_context_get_sink_info_by_name(i
->context
, NULL
, sink_info_cb
, i
))) {
964 debug(__FILE__
": Failed to get sink info: %s", pa_strerror(pa_context_errno(i
->context
)));
969 i
->operation_success
= 0;
970 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
971 pa_threaded_mainloop_wait(i
->mainloop
);
972 CONTEXT_CHECK_DEAD_GOTO(i
, fail
);
975 if (!i
->operation_success
) {
976 debug(__FILE__
": Failed to get sink info: %s", pa_strerror(pa_context_errno(i
->context
)));
981 pa_threaded_mainloop_unlock(i
->mainloop
);
983 debug(__FILE__
": mixer_open() succeeded, fd=%i\n", i
->app_fd
);
985 fd_info_add_to_list(i
);
992 pa_threaded_mainloop_unlock(i
->mainloop
);
999 debug(__FILE__
": mixer_open() failed\n");
1004 static int sndstat_open(int flags
, int *_errno
) {
1005 static const char sndstat
[] =
1006 "Sound Driver:3.8.1a-980706 (Polypaudio Virtual OSS)\n"
1008 "Config options: 0\n"
1010 "Installed drivers:\n"
1011 "Type 255: Polypaudio Virtual OSS\n"
1014 "Polypaudio Virtual OSS\n"
1017 "0: Polypaudio Virtual OSS\n"
1019 "Synth devices: NOT ENABLED IN CONFIG\n"
1026 "0: Polypaudio Virtual OSS\n";
1028 char fn
[] = "/tmp/padsp-sndstat-XXXXXX";
1033 debug(__FILE__
": sndstat_open()\n");
1035 if (flags
!= O_RDONLY
&& flags
!= (O_RDONLY
|O_LARGEFILE
)) {
1037 debug(__FILE__
": bad access!\n");
1048 debug(__FILE__
": mkstemp() failed: %s\n", strerror(errno
));
1054 if (write(fd
, sndstat
, sizeof(sndstat
) -1) != sizeof(sndstat
)-1) {
1056 debug(__FILE__
": write() failed: %s\n", strerror(errno
));
1060 if (lseek(fd
, SEEK_SET
, 0) < 0) {
1062 debug(__FILE__
": lseek() failed: %s\n", strerror(errno
));
1074 int open(const char *filename
, int flags
, ...) {
1079 debug(__FILE__
": open(%s)\n", filename
);
1081 va_start(args
, flags
);
1082 if (flags
& O_CREAT
)
1083 mode
= va_arg(args
, mode_t
);
1086 if (!function_enter()) {
1088 return _open(filename
, flags
, mode
);
1091 if (dsp_cloak_enable() && (strcmp(filename
, "/dev/dsp") == 0 || strcmp(filename
, "/dev/adsp") == 0)) {
1092 r
= dsp_open(flags
, &_errno
);
1093 } else if (mixer_cloak_enable() && strcmp(filename
, "/dev/mixer") == 0) {
1094 r
= mixer_open(flags
, &_errno
);
1095 } else if (sndstat_cloak_enable() && strcmp(filename
, "/dev/sndstat") == 0) {
1096 r
= sndstat_open(flags
, &_errno
);
1100 return _open(filename
, flags
, mode
);
1111 static int mixer_ioctl(fd_info
*i
, unsigned long request
, void*argp
, int *_errno
) {
1115 case SOUND_MIXER_READ_DEVMASK
:
1116 debug(__FILE__
": SOUND_MIXER_READ_DEVMASK\n");
1118 *(int*) argp
= SOUND_MASK_PCM
;
1121 case SOUND_MIXER_READ_RECMASK
:
1122 debug(__FILE__
": SOUND_MIXER_READ_RECMASK\n");
1127 case SOUND_MIXER_READ_STEREODEVS
:
1128 debug(__FILE__
": SOUND_MIXER_READ_STEREODEVS\n");
1130 pa_threaded_mainloop_lock(i
->mainloop
);
1131 *(int*) argp
= i
->volume
.channels
> 1 ? SOUND_MASK_PCM
: 0;
1132 pa_threaded_mainloop_unlock(i
->mainloop
);
1136 case SOUND_MIXER_READ_RECSRC
:
1137 debug(__FILE__
": SOUND_MIXER_READ_RECSRC\n");
1142 case SOUND_MIXER_READ_CAPS
:
1143 debug(__FILE__
": SOUND_MIXER_READ_CAPS\n");
1148 case SOUND_MIXER_READ_PCM
:
1150 debug(__FILE__
": SOUND_MIXER_READ_PCM\n");
1152 pa_threaded_mainloop_lock(i
->mainloop
);
1155 ((i
->volume
.values
[0]*100/PA_VOLUME_NORM
)) |
1156 ((i
->volume
.values
[i
->volume
.channels
> 1 ? 1 : 0]*100/PA_VOLUME_NORM
) << 8);
1158 pa_threaded_mainloop_unlock(i
->mainloop
);
1162 case SOUND_MIXER_WRITE_PCM
: {
1165 debug(__FILE__
": SOUND_MIXER_WRITE_PCM\n");
1167 pa_threaded_mainloop_lock(i
->mainloop
);
1171 i
->volume
.values
[0] = ((*(int*) argp
& 0xFF)*PA_VOLUME_NORM
)/100;
1172 i
->volume
.values
[1] = ((*(int*) argp
>> 8)*PA_VOLUME_NORM
)/100;
1174 if (!pa_cvolume_equal(&i
->volume
, &v
)) {
1177 if (!(o
= pa_context_set_sink_volume_by_index(i
->context
, i
->sink_index
, &i
->volume
, NULL
, NULL
)))
1178 debug(__FILE__
":Failed set volume: %s", pa_strerror(pa_context_errno(i
->context
)));
1181 i
->operation_success
= 0;
1182 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1183 CONTEXT_CHECK_DEAD_GOTO(i
, exit_loop
);
1185 pa_threaded_mainloop_wait(i
->mainloop
);
1189 if (!i
->operation_success
)
1190 debug(__FILE__
": Failed to set volume: %s\n", pa_strerror(pa_context_errno(i
->context
)));
1192 pa_operation_unref(o
);
1195 /* We don't wait for completion here */
1196 i
->volume_modify_count
++;
1199 pa_threaded_mainloop_unlock(i
->mainloop
);
1204 case SOUND_MIXER_INFO
: {
1205 mixer_info
*mi
= argp
;
1207 memset(mi
, 0, sizeof(mixer_info
));
1208 strncpy(mi
->id
, "POLYPAUDIO", sizeof(mi
->id
));
1209 strncpy(mi
->name
, "Polypaudio Virtual OSS", sizeof(mi
->name
));
1210 pa_threaded_mainloop_lock(i
->mainloop
);
1211 mi
->modify_counter
= i
->volume_modify_count
;
1212 pa_threaded_mainloop_unlock(i
->mainloop
);
1217 debug(__FILE__
": unknown ioctl 0x%08lx\n", request
);
1230 static int map_format(int *fmt
, pa_sample_spec
*ss
) {
1234 ss
->format
= PA_SAMPLE_ULAW
;
1238 ss
->format
= PA_SAMPLE_ALAW
;
1245 ss
->format
= PA_SAMPLE_U8
;
1252 ss
->format
= PA_SAMPLE_S16BE
;
1259 ss
->format
= PA_SAMPLE_S16LE
;
1263 ss
->format
= PA_SAMPLE_S16NE
;
1271 static int map_format_back(pa_sample_format_t format
) {
1273 case PA_SAMPLE_S16LE
: return AFMT_S16_LE
;
1274 case PA_SAMPLE_S16BE
: return AFMT_S16_BE
;
1275 case PA_SAMPLE_ULAW
: return AFMT_MU_LAW
;
1276 case PA_SAMPLE_ALAW
: return AFMT_A_LAW
;
1277 case PA_SAMPLE_U8
: return AFMT_U8
;
1283 static int dsp_flush_socket(fd_info
*i
) {
1286 if (i
->thread_fd
< 0)
1289 if (ioctl(i
->thread_fd
, SIOCINQ
, &l
) < 0) {
1290 debug(__FILE__
": SIOCINQ: %s\n", strerror(errno
));
1298 k
= (size_t) l
> sizeof(buf
) ? sizeof(buf
) : (size_t) l
;
1299 if (read(i
->thread_fd
, buf
, k
) < 0)
1300 debug(__FILE__
": read(): %s\n", strerror(errno
));
1307 static int dsp_empty_socket(fd_info
*i
) {
1310 /* Empty the socket */
1314 if (i
->thread_fd
< 0)
1317 if (ioctl(i
->thread_fd
, SIOCINQ
, &l
) < 0) {
1318 debug(__FILE__
": SIOCINQ: %s\n", strerror(errno
));
1327 pa_threaded_mainloop_wait(i
->mainloop
);
1333 static int dsp_drain(fd_info
*i
) {
1334 pa_operation
*o
= NULL
;
1340 debug(__FILE__
": Draining.\n");
1342 pa_threaded_mainloop_lock(i
->mainloop
);
1344 if (dsp_empty_socket(i
) < 0)
1350 debug(__FILE__
": Really draining.\n");
1352 if (!(o
= pa_stream_drain(i
->stream
, stream_success_cb
, i
))) {
1353 debug(__FILE__
": pa_stream_drain(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1357 i
->operation_success
= 0;
1358 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1359 STREAM_CHECK_DEAD_GOTO(i
, fail
);
1361 pa_threaded_mainloop_wait(i
->mainloop
);
1364 if (!i
->operation_success
) {
1365 debug(__FILE__
": pa_stream_drain() 2: %s\n", pa_strerror(pa_context_errno(i
->context
)));
1374 pa_operation_unref(o
);
1376 pa_threaded_mainloop_unlock(i
->mainloop
);
1381 static int dsp_trigger(fd_info
*i
) {
1382 pa_operation
*o
= NULL
;
1388 pa_threaded_mainloop_lock(i
->mainloop
);
1390 if (dsp_empty_socket(i
) < 0)
1393 debug(__FILE__
": Triggering.\n");
1395 if (!(o
= pa_stream_trigger(i
->stream
, stream_success_cb
, i
))) {
1396 debug(__FILE__
": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1400 i
->operation_success
= 0;
1401 while (!pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1402 STREAM_CHECK_DEAD_GOTO(i
, fail
);
1404 pa_threaded_mainloop_wait(i
->mainloop
);
1407 if (!i
->operation_success
) {
1408 debug(__FILE__
": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1417 pa_operation_unref(o
);
1419 pa_threaded_mainloop_unlock(i
->mainloop
);
1424 static int dsp_ioctl(fd_info
*i
, unsigned long request
, void*argp
, int *_errno
) {
1428 case SNDCTL_DSP_SETFMT
: {
1429 debug(__FILE__
": SNDCTL_DSP_SETFMT: %i\n", *(int*) argp
);
1431 pa_threaded_mainloop_lock(i
->mainloop
);
1433 if (*(int*) argp
== AFMT_QUERY
)
1434 *(int*) argp
= map_format_back(i
->sample_spec
.format
);
1436 map_format((int*) argp
, &i
->sample_spec
);
1440 pa_threaded_mainloop_unlock(i
->mainloop
);
1444 case SNDCTL_DSP_SPEED
: {
1449 debug(__FILE__
": SNDCTL_DSP_SPEED: %i\n", *(int*) argp
);
1451 pa_threaded_mainloop_lock(i
->mainloop
);
1453 ss
= i
->sample_spec
;
1454 ss
.rate
= *(int*) argp
;
1456 if ((valid
= pa_sample_spec_valid(&ss
))) {
1457 i
->sample_spec
= ss
;
1461 debug(__FILE__
": ss: %s\n", pa_sample_spec_snprint(t
, sizeof(t
), &i
->sample_spec
));
1463 pa_threaded_mainloop_unlock(i
->mainloop
);
1473 case SNDCTL_DSP_STEREO
:
1474 debug(__FILE__
": SNDCTL_DSP_STEREO: %i\n", *(int*) argp
);
1476 pa_threaded_mainloop_lock(i
->mainloop
);
1478 i
->sample_spec
.channels
= *(int*) argp
? 2 : 1;
1481 pa_threaded_mainloop_unlock(i
->mainloop
);
1484 case SNDCTL_DSP_CHANNELS
: {
1488 debug(__FILE__
": SNDCTL_DSP_CHANNELS: %i\n", *(int*) argp
);
1490 pa_threaded_mainloop_lock(i
->mainloop
);
1492 ss
= i
->sample_spec
;
1493 ss
.channels
= *(int*) argp
;
1495 if ((valid
= pa_sample_spec_valid(&ss
))) {
1496 i
->sample_spec
= ss
;
1500 pa_threaded_mainloop_unlock(i
->mainloop
);
1510 case SNDCTL_DSP_GETBLKSIZE
:
1511 debug(__FILE__
": SNDCTL_DSP_GETBLKSIZE\n");
1513 pa_threaded_mainloop_lock(i
->mainloop
);
1516 *(int*) argp
= i
->fragment_size
;
1518 pa_threaded_mainloop_unlock(i
->mainloop
);
1522 case SNDCTL_DSP_SETFRAGMENT
:
1523 debug(__FILE__
": SNDCTL_DSP_SETFRAGMENT: 0x%8x\n", *(int*) argp
);
1525 pa_threaded_mainloop_lock(i
->mainloop
);
1527 i
->fragment_size
= 1 << (*(int*) argp
);
1528 i
->n_fragments
= (*(int*) argp
) >> 16;
1532 pa_threaded_mainloop_unlock(i
->mainloop
);
1536 case SNDCTL_DSP_GETCAPS
:
1537 debug(__FILE__
": SNDCTL_DSP_CAPS\n");
1539 *(int*) argp
= DSP_CAP_MULTI
;
1542 case SNDCTL_DSP_GETODELAY
: {
1545 debug(__FILE__
": SNDCTL_DSP_GETODELAY\n");
1547 pa_threaded_mainloop_lock(i
->mainloop
);
1554 STREAM_CHECK_DEAD_GOTO(i
, exit_loop
);
1556 if (pa_stream_get_latency(i
->stream
, &usec
, NULL
) >= 0) {
1557 *(int*) argp
= pa_usec_to_bytes(usec
, &i
->sample_spec
);
1561 if (pa_context_errno(i
->context
) != PA_ERR_NODATA
) {
1562 debug(__FILE__
": pa_stream_get_latency(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1566 pa_threaded_mainloop_wait(i
->mainloop
);
1571 if (ioctl(i
->thread_fd
, SIOCINQ
, &l
) < 0)
1572 debug(__FILE__
": SIOCINQ failed: %s\n", strerror(errno
));
1576 pa_threaded_mainloop_unlock(i
->mainloop
);
1578 debug(__FILE__
": ODELAY: %i\n", *(int*) argp
);
1583 case SNDCTL_DSP_RESET
: {
1584 debug(__FILE__
": SNDCTL_DSP_RESET\n");
1586 pa_threaded_mainloop_lock(i
->mainloop
);
1589 dsp_flush_socket(i
);
1592 pa_threaded_mainloop_unlock(i
->mainloop
);
1596 case SNDCTL_DSP_GETFMTS
: {
1597 debug(__FILE__
": SNDCTL_DSP_GETFMTS\n");
1599 *(int*) argp
= AFMT_MU_LAW
|AFMT_A_LAW
|AFMT_U8
|AFMT_S16_LE
|AFMT_S16_BE
;
1603 case SNDCTL_DSP_POST
:
1604 debug(__FILE__
": SNDCTL_DSP_POST\n");
1606 if (dsp_trigger(i
) < 0)
1610 case SNDCTL_DSP_SYNC
:
1611 debug(__FILE__
": SNDCTL_DSP_SYNC\n");
1613 if (dsp_drain(i
) < 0)
1618 case SNDCTL_DSP_GETOSPACE
: {
1619 audio_buf_info
*bi
= (audio_buf_info
*) argp
;
1623 debug(__FILE__
": SNDCTL_DSP_GETOSPACE\n");
1625 pa_threaded_mainloop_lock(i
->mainloop
);
1630 if ((k
= pa_stream_writable_size(i
->stream
)) == (size_t) -1)
1631 debug(__FILE__
": pa_stream_writable_size(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1633 k
= i
->fragment_size
* i
->n_fragments
;
1635 if (ioctl(i
->thread_fd
, SIOCINQ
, &l
) < 0) {
1636 debug(__FILE__
": SIOCINQ failed: %s\n", strerror(errno
));
1640 bi
->fragsize
= i
->fragment_size
;
1641 bi
->fragstotal
= i
->n_fragments
;
1642 bi
->bytes
= k
> (size_t) l
? k
- l
: 0;
1643 bi
->fragments
= bi
->bytes
/ bi
->fragsize
;
1645 pa_threaded_mainloop_unlock(i
->mainloop
);
1647 debug(__FILE__
": fragsize=%i, fragstotal=%i, bytes=%i, fragments=%i\n", bi
->fragsize
, bi
->fragstotal
, bi
->bytes
, bi
->fragments
);
1653 debug(__FILE__
": unknwon ioctl 0x%08lx\n", request
);
1666 int ioctl(int fd
, unsigned long request
, ...) {
1672 debug(__FILE__
": ioctl()\n");
1674 va_start(args
, request
);
1675 argp
= va_arg(args
, void *);
1678 if (!function_enter()) {
1680 return _ioctl(fd
, request
, argp
);
1683 if (!(i
= fd_info_find(fd
))) {
1686 return _ioctl(fd
, request
, argp
);
1689 if (i
->type
== FD_INFO_MIXER
)
1690 r
= mixer_ioctl(i
, request
, argp
, &_errno
);
1692 r
= dsp_ioctl(i
, request
, argp
, &_errno
);
1707 debug(__FILE__
": close()\n");
1709 if (!function_enter()) {
1714 if (!(i
= fd_info_find(fd
))) {
1720 fd_info_remove_from_list(i
);
1728 int open64(const char *filename
, int flags
, ...) {
1732 debug(__FILE__
": open64(%s)\n", filename
);
1734 va_start(args
, flags
);
1735 if (flags
& O_CREAT
)
1736 mode
= va_arg(args
, mode_t
);
1739 if (strcmp(filename
, "/dev/dsp") != 0 &&
1740 strcmp(filename
, "/dev/adsp") != 0 &&
1741 strcmp(filename
, "/dev/sndstat") != 0 &&
1742 strcmp(filename
, "/dev/mixer") != 0) {
1744 return _open64(filename
, flags
, mode
);
1747 return open(filename
, flags
, mode
);
1750 FILE* fopen(const char *filename
, const char *mode
) {
1755 debug(__FILE__
": fopen(%s)\n", filename
);
1757 if (strcmp(filename
, "/dev/dsp") == 0 ||
1758 strcmp(filename
, "/dev/adsp") == 0) {
1760 if (strcmp(mode
, "wb") != 0) {
1766 } else if (strcmp(filename
, "/dev/sndstat") == 0) {
1768 if (strcmp(mode
, "r") != 0) {
1774 } else if (strcmp(filename
, "/dev/mixer") == 0)
1778 return _fopen(filename
, mode
);
1781 if ((fd
= open(filename
, m
)) < 0)
1784 if (!(f
= fdopen(fd
, mode
))) {
1792 FILE *fopen64(const char *filename
, const char *mode
) {
1794 debug(__FILE__
": fopen64(%s)\n", filename
);
1796 if (strcmp(filename
, "/dev/dsp") != 0 &&
1797 strcmp(filename
, "/dev/adsp") != 0 &&
1798 strcmp(filename
, "/dev/sndstat") != 0 &&
1799 strcmp(filename
, "/dev/mixer") != 0) {
1800 LOAD_FOPEN64_FUNC();
1801 return _fopen64(filename
, mode
);
1804 return fopen(filename
, mode
);
1807 int fclose(FILE *f
) {
1810 debug(__FILE__
": fclose()\n");
1812 if (!function_enter()) {
1817 if (!(i
= fd_info_find(fileno(f
)))) {
1823 fd_info_remove_from_list(i
);
1825 /* Dirty trick to avoid that the fd is not freed twice, once by us
1826 * and once by the real fclose() */