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
= 1024;
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 if (i
->n_fragments
< 2)
634 if (i
->fragment_size
<= 0)
635 if ((i
->fragment_size
= pa_bytes_per_second(&i
->sample_spec
) / 2 / i
->n_fragments
) <= 0)
636 i
->fragment_size
= 1024;
638 debug(__FILE__
": sample spec: %s\n", pa_sample_spec_snprint(t
, sizeof(t
), &i
->sample_spec
));
639 debug(__FILE__
": fixated metrics to %i fragments, %li bytes each.\n", i
->n_fragments
, (long)i
->fragment_size
);
642 static void stream_request_cb(pa_stream
*s
, size_t length
, void *userdata
) {
643 fd_info
*i
= userdata
;
647 pa_mainloop_api
*api
;
648 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
649 api
->io_enable(i
->io_event
, PA_IO_EVENT_INPUT
);
653 static void stream_latency_update_cb(pa_stream
*s
, void *userdata
) {
654 fd_info
*i
= userdata
;
657 pa_threaded_mainloop_signal(i
->mainloop
, 0);
660 static void fd_info_shutdown(fd_info
*i
) {
664 pa_mainloop_api
*api
;
665 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
666 api
->io_free(i
->io_event
);
670 if (i
->thread_fd
>= 0) {
676 static int fd_info_copy_data(fd_info
*i
, int force
) {
682 if ((n
= pa_stream_writable_size(i
->stream
)) == (size_t) -1) {
683 debug(__FILE__
": pa_stream_writable_size(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
687 while (n
>= i
->fragment_size
|| force
) {
691 if (!(i
->buf
= malloc(i
->fragment_size
))) {
692 debug(__FILE__
": malloc() failed.\n");
697 if ((r
= read(i
->thread_fd
, i
->buf
, i
->fragment_size
)) <= 0) {
702 debug(__FILE__
": read(): %s\n", r
== 0 ? "EOF" : strerror(errno
));
706 if (pa_stream_write(i
->stream
, i
->buf
, r
, free
, 0, PA_SEEK_RELATIVE
) < 0) {
707 debug(__FILE__
": pa_stream_write(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
713 assert(n
>= (size_t) r
);
718 pa_mainloop_api
*api
;
719 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
720 api
->io_enable(i
->io_event
, n
>= i
->fragment_size
? PA_IO_EVENT_INPUT
: 0);
726 static void stream_state_cb(pa_stream
*s
, void * userdata
) {
727 fd_info
*i
= userdata
;
730 switch (pa_stream_get_state(s
)) {
732 case PA_STREAM_READY
:
733 debug(__FILE__
": stream established.\n");
736 case PA_STREAM_FAILED
:
737 debug(__FILE__
": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
741 case PA_STREAM_TERMINATED
:
742 case PA_STREAM_UNCONNECTED
:
743 case PA_STREAM_CREATING
:
748 static int create_stream(fd_info
*i
) {
756 if (!(i
->stream
= pa_stream_new(i
->context
, stream_name(), &i
->sample_spec
, NULL
))) {
757 debug(__FILE__
": pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
761 pa_stream_set_state_callback(i
->stream
, stream_state_cb
, i
);
762 pa_stream_set_write_callback(i
->stream
, stream_request_cb
, i
);
763 pa_stream_set_latency_update_callback(i
->stream
, stream_latency_update_cb
, i
);
765 memset(&attr
, 0, sizeof(attr
));
766 attr
.maxlength
= i
->fragment_size
* (i
->n_fragments
+1);
767 attr
.tlength
= i
->fragment_size
* i
->n_fragments
;
768 attr
.prebuf
= i
->fragment_size
;
769 attr
.minreq
= i
->fragment_size
;
771 if (pa_stream_connect_playback(i
->stream
, NULL
, &attr
, PA_STREAM_INTERPOLATE_TIMING
|PA_STREAM_AUTO_TIMING_UPDATE
, NULL
, NULL
) < 0) {
772 debug(__FILE__
": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
776 n
= i
->fragment_size
;
777 setsockopt(i
->app_fd
, SOL_SOCKET
, SO_SNDBUF
, &n
, sizeof(n
));
778 n
= i
->fragment_size
;
779 setsockopt(i
->thread_fd
, SOL_SOCKET
, SO_RCVBUF
, &n
, sizeof(n
));
787 static void free_stream(fd_info
*i
) {
791 pa_stream_disconnect(i
->stream
);
792 pa_stream_unref(i
->stream
);
797 static void io_event_cb(pa_mainloop_api
*api
, pa_io_event
*e
, int fd
, pa_io_event_flags_t flags
, void *userdata
) {
798 fd_info
*i
= userdata
;
800 pa_threaded_mainloop_signal(i
->mainloop
, 0);
802 if (flags
& PA_IO_EVENT_INPUT
) {
805 api
->io_enable(e
, 0);
807 if (create_stream(i
) < 0)
811 if (fd_info_copy_data(i
, 0) < 0)
815 } else if (flags
& (PA_IO_EVENT_HANGUP
|PA_IO_EVENT_ERROR
))
821 /* We can't do anything better than removing the event source */
825 static int dsp_open(int flags
, int *_errno
) {
827 pa_mainloop_api
*api
;
831 if ((flags
!= O_WRONLY
) && (flags
!= (O_WRONLY
|O_NONBLOCK
))) {
836 if (!(i
= fd_info_new(FD_INFO_PLAYBACK
, _errno
)))
839 shutdown(i
->thread_fd
, SHUT_WR
);
840 shutdown(i
->app_fd
, SHUT_RD
);
842 if ((flags
& O_NONBLOCK
) == O_NONBLOCK
) {
843 if ((f
= fcntl(i
->app_fd
, F_GETFL
)) >= 0)
844 fcntl(i
->app_fd
, F_SETFL
, f
|O_NONBLOCK
);
846 if ((f
= fcntl(i
->thread_fd
, F_GETFL
)) >= 0)
847 fcntl(i
->thread_fd
, F_SETFL
, f
|O_NONBLOCK
);
849 fcntl(i
->app_fd
, F_SETFD
, FD_CLOEXEC
);
850 fcntl(i
->thread_fd
, F_SETFD
, FD_CLOEXEC
);
852 pa_threaded_mainloop_lock(i
->mainloop
);
853 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
854 if (!(i
->io_event
= api
->io_new(api
, i
->thread_fd
, PA_IO_EVENT_INPUT
, io_event_cb
, i
)))
857 pa_threaded_mainloop_unlock(i
->mainloop
);
859 debug(__FILE__
": dsp_open() succeeded, fd=%i\n", i
->app_fd
);
861 fd_info_add_to_list(i
);
868 pa_threaded_mainloop_unlock(i
->mainloop
);
875 debug(__FILE__
": dsp_open() failed\n");
880 static void sink_info_cb(pa_context
*context
, const pa_sink_info
*si
, int eol
, void *userdata
) {
881 fd_info
*i
= userdata
;
883 if (!si
&& eol
< 0) {
884 i
->operation_success
= 0;
885 pa_threaded_mainloop_signal(i
->mainloop
, 0);
892 if (!pa_cvolume_equal(&i
->volume
, &si
->volume
))
893 i
->volume_modify_count
++;
895 i
->volume
= si
->volume
;
896 i
->sink_index
= si
->index
;
898 i
->operation_success
= 1;
899 pa_threaded_mainloop_signal(i
->mainloop
, 0);
902 static void subscribe_cb(pa_context
*context
, pa_subscription_event_type_t t
, uint32_t idx
, void *userdata
) {
903 fd_info
*i
= userdata
;
904 pa_operation
*o
= NULL
;
906 if (i
->sink_index
!= idx
)
909 if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) != PA_SUBSCRIPTION_EVENT_CHANGE
)
912 if (!(o
= pa_context_get_sink_info_by_index(i
->context
, i
->sink_index
, sink_info_cb
, i
))) {
913 debug(__FILE__
": Failed to get sink info: %s", pa_strerror(pa_context_errno(i
->context
)));
917 pa_operation_unref(o
);
920 static int mixer_open(int flags
, int *_errno
) {
925 if (!(i
= fd_info_new(FD_INFO_MIXER
, _errno
)))
928 pa_threaded_mainloop_lock(i
->mainloop
);
930 pa_context_set_subscribe_callback(i
->context
, subscribe_cb
, i
);
932 if (!(o
= pa_context_subscribe(i
->context
, PA_SUBSCRIPTION_MASK_SINK
, context_success_cb
, i
))) {
933 debug(__FILE__
": Failed to subscribe to events: %s", pa_strerror(pa_context_errno(i
->context
)));
938 i
->operation_success
= 0;
939 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
940 pa_threaded_mainloop_wait(i
->mainloop
);
941 CONTEXT_CHECK_DEAD_GOTO(i
, fail
);
944 if (!i
->operation_success
) {
945 debug(__FILE__
":Failed to subscribe to events: %s", pa_strerror(pa_context_errno(i
->context
)));
952 pa_operation_unref(o
);
953 if (!(o
= pa_context_get_sink_info_by_name(i
->context
, NULL
, sink_info_cb
, i
))) {
954 debug(__FILE__
": Failed to get sink info: %s", pa_strerror(pa_context_errno(i
->context
)));
959 i
->operation_success
= 0;
960 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
961 pa_threaded_mainloop_wait(i
->mainloop
);
962 CONTEXT_CHECK_DEAD_GOTO(i
, fail
);
965 if (!i
->operation_success
) {
966 debug(__FILE__
": Failed to get sink info: %s", pa_strerror(pa_context_errno(i
->context
)));
971 pa_threaded_mainloop_unlock(i
->mainloop
);
973 debug(__FILE__
": mixer_open() succeeded, fd=%i\n", i
->app_fd
);
975 fd_info_add_to_list(i
);
982 pa_threaded_mainloop_unlock(i
->mainloop
);
989 debug(__FILE__
": mixer_open() failed\n");
994 static int sndstat_open(int flags
, int *_errno
) {
995 static const char sndstat
[] =
996 "Sound Driver:3.8.1a-980706 (Polypaudio Virtual OSS)\n"
998 "Config options: 0\n"
1000 "Installed drivers:\n"
1001 "Type 255: Polypaudio Virtual OSS\n"
1004 "Polypaudio Virtual OSS\n"
1007 "0: Polypaudio Virtual OSS\n"
1009 "Synth devices: NOT ENABLED IN CONFIG\n"
1016 "0: Polypaudio Virtual OSS\n";
1018 char fn
[] = "/tmp/padsp-sndstat-XXXXXX";
1023 debug(__FILE__
": sndstat_open()\n");
1025 if (flags
!= O_RDONLY
&& flags
!= (O_RDONLY
|O_LARGEFILE
)) {
1027 debug(__FILE__
": bad access!\n");
1038 debug(__FILE__
": mkstemp() failed: %s\n", strerror(errno
));
1044 if (write(fd
, sndstat
, sizeof(sndstat
) -1) != sizeof(sndstat
)-1) {
1046 debug(__FILE__
": write() failed: %s\n", strerror(errno
));
1050 if (lseek(fd
, SEEK_SET
, 0) < 0) {
1052 debug(__FILE__
": lseek() failed: %s\n", strerror(errno
));
1064 int open(const char *filename
, int flags
, ...) {
1069 debug(__FILE__
": open(%s)\n", filename
);
1071 va_start(args
, flags
);
1072 if (flags
& O_CREAT
)
1073 mode
= va_arg(args
, mode_t
);
1076 if (!function_enter()) {
1078 return _open(filename
, flags
, mode
);
1081 if (dsp_cloak_enable() && (strcmp(filename
, "/dev/dsp") == 0 || strcmp(filename
, "/dev/adsp") == 0)) {
1082 r
= dsp_open(flags
, &_errno
);
1083 } else if (mixer_cloak_enable() && strcmp(filename
, "/dev/mixer") == 0) {
1084 r
= mixer_open(flags
, &_errno
);
1085 } else if (sndstat_cloak_enable() && strcmp(filename
, "/dev/sndstat") == 0) {
1086 r
= sndstat_open(flags
, &_errno
);
1090 return _open(filename
, flags
, mode
);
1101 static int mixer_ioctl(fd_info
*i
, unsigned long request
, void*argp
, int *_errno
) {
1105 case SOUND_MIXER_READ_DEVMASK
:
1106 debug(__FILE__
": SOUND_MIXER_READ_DEVMASK\n");
1108 *(int*) argp
= SOUND_MASK_PCM
;
1111 case SOUND_MIXER_READ_RECMASK
:
1112 debug(__FILE__
": SOUND_MIXER_READ_RECMASK\n");
1117 case SOUND_MIXER_READ_STEREODEVS
:
1118 debug(__FILE__
": SOUND_MIXER_READ_STEREODEVS\n");
1120 pa_threaded_mainloop_lock(i
->mainloop
);
1121 *(int*) argp
= i
->volume
.channels
> 1 ? SOUND_MASK_PCM
: 0;
1122 pa_threaded_mainloop_unlock(i
->mainloop
);
1126 case SOUND_MIXER_READ_RECSRC
:
1127 debug(__FILE__
": SOUND_MIXER_READ_RECSRC\n");
1132 case SOUND_MIXER_CAPS
:
1133 debug(__FILE__
": SOUND_MIXER_CAPS\n");
1138 case SOUND_MIXER_READ_PCM
:
1140 debug(__FILE__
": SOUND_MIXER_READ_PCM\n");
1142 pa_threaded_mainloop_lock(i
->mainloop
);
1145 ((i
->volume
.values
[0]*100/PA_VOLUME_NORM
)) |
1146 ((i
->volume
.values
[i
->volume
.channels
> 1 ? 1 : 0]*100/PA_VOLUME_NORM
) << 8);
1148 pa_threaded_mainloop_unlock(i
->mainloop
);
1152 case SOUND_MIXER_WRITE_PCM
: {
1155 debug(__FILE__
": SOUND_MIXER_WRITE_PCM\n");
1157 pa_threaded_mainloop_lock(i
->mainloop
);
1161 i
->volume
.values
[0] = ((*(int*) argp
& 0xFF)*PA_VOLUME_NORM
)/100;
1162 i
->volume
.values
[1] = ((*(int*) argp
>> 8)*PA_VOLUME_NORM
)/100;
1164 if (!pa_cvolume_equal(&i
->volume
, &v
)) {
1167 if (!(o
= pa_context_set_sink_volume_by_index(i
->context
, i
->sink_index
, &i
->volume
, NULL
, NULL
)))
1168 debug(__FILE__
":Failed set volume: %s", pa_strerror(pa_context_errno(i
->context
)));
1171 i
->operation_success
= 0;
1172 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1173 CONTEXT_CHECK_DEAD_GOTO(i
, exit_loop
);
1175 pa_threaded_mainloop_wait(i
->mainloop
);
1179 if (!i
->operation_success
)
1180 debug(__FILE__
": Failed to set volume: %s\n", pa_strerror(pa_context_errno(i
->context
)));
1182 pa_operation_unref(o
);
1185 /* We don't wait for completion here */
1186 i
->volume_modify_count
++;
1189 pa_threaded_mainloop_unlock(i
->mainloop
);
1194 case SOUND_MIXER_INFO
: {
1195 mixer_info
*mi
= argp
;
1197 memset(mi
, 0, sizeof(mixer_info
));
1198 strncpy(mi
->id
, "POLYPAUDIO", sizeof(mi
->id
));
1199 strncpy(mi
->name
, "Polypaudio Virtual OSS", sizeof(mi
->name
));
1200 pa_threaded_mainloop_lock(i
->mainloop
);
1201 mi
->modify_counter
= i
->volume_modify_count
;
1202 pa_threaded_mainloop_unlock(i
->mainloop
);
1207 debug(__FILE__
": unknwon ioctl 0x%08lx\n", request
);
1220 static int map_format(int *fmt
, pa_sample_spec
*ss
) {
1224 ss
->format
= PA_SAMPLE_ULAW
;
1228 ss
->format
= PA_SAMPLE_ALAW
;
1235 ss
->format
= PA_SAMPLE_U8
;
1242 ss
->format
= PA_SAMPLE_S16BE
;
1249 ss
->format
= PA_SAMPLE_S16LE
;
1253 ss
->format
= PA_SAMPLE_S16NE
;
1261 static int map_format_back(pa_sample_format_t format
) {
1263 case PA_SAMPLE_S16LE
: return AFMT_S16_LE
;
1264 case PA_SAMPLE_S16BE
: return AFMT_S16_BE
;
1265 case PA_SAMPLE_ULAW
: return AFMT_MU_LAW
;
1266 case PA_SAMPLE_ALAW
: return AFMT_A_LAW
;
1267 case PA_SAMPLE_U8
: return AFMT_U8
;
1273 static int dsp_flush_socket(fd_info
*i
) {
1276 if (i
->thread_fd
< 0)
1279 if (ioctl(i
->thread_fd
, SIOCINQ
, &l
) < 0) {
1280 debug(__FILE__
": SIOCINQ: %s\n", strerror(errno
));
1288 k
= (size_t) l
> sizeof(buf
) ? sizeof(buf
) : (size_t) l
;
1289 if (read(i
->thread_fd
, buf
, k
) < 0)
1290 debug(__FILE__
": read(): %s\n", strerror(errno
));
1297 static int dsp_empty_socket(fd_info
*i
) {
1300 /* Empty the socket */
1304 if (i
->thread_fd
< 0)
1307 if (ioctl(i
->thread_fd
, SIOCINQ
, &l
) < 0) {
1308 debug(__FILE__
": SIOCINQ: %s\n", strerror(errno
));
1317 pa_threaded_mainloop_wait(i
->mainloop
);
1323 static int dsp_drain(fd_info
*i
) {
1324 pa_operation
*o
= NULL
;
1330 debug(__FILE__
": Draining.\n");
1332 pa_threaded_mainloop_lock(i
->mainloop
);
1334 if (dsp_empty_socket(i
) < 0)
1340 debug(__FILE__
": Really draining.\n");
1342 if (!(o
= pa_stream_drain(i
->stream
, stream_success_cb
, i
))) {
1343 debug(__FILE__
": pa_stream_drain(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1347 i
->operation_success
= 0;
1348 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1349 STREAM_CHECK_DEAD_GOTO(i
, fail
);
1351 pa_threaded_mainloop_wait(i
->mainloop
);
1354 if (!i
->operation_success
) {
1355 debug(__FILE__
": pa_stream_drain() 2: %s\n", pa_strerror(pa_context_errno(i
->context
)));
1364 pa_operation_unref(o
);
1366 pa_threaded_mainloop_unlock(i
->mainloop
);
1371 static int dsp_trigger(fd_info
*i
) {
1372 pa_operation
*o
= NULL
;
1378 pa_threaded_mainloop_lock(i
->mainloop
);
1380 if (dsp_empty_socket(i
) < 0)
1383 debug(__FILE__
": Triggering.\n");
1385 if (!(o
= pa_stream_trigger(i
->stream
, stream_success_cb
, i
))) {
1386 debug(__FILE__
": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1390 i
->operation_success
= 0;
1391 while (!pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1392 STREAM_CHECK_DEAD_GOTO(i
, fail
);
1394 pa_threaded_mainloop_wait(i
->mainloop
);
1397 if (!i
->operation_success
) {
1398 debug(__FILE__
": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1407 pa_operation_unref(o
);
1409 pa_threaded_mainloop_unlock(i
->mainloop
);
1414 static int dsp_ioctl(fd_info
*i
, unsigned long request
, void*argp
, int *_errno
) {
1418 case SNDCTL_DSP_SETFMT
: {
1419 debug(__FILE__
": SNDCTL_DSP_SETFMT: %i\n", *(int*) argp
);
1421 pa_threaded_mainloop_lock(i
->mainloop
);
1423 if (*(int*) argp
== AFMT_QUERY
)
1424 *(int*) argp
= map_format_back(i
->sample_spec
.format
);
1426 map_format((int*) argp
, &i
->sample_spec
);
1430 pa_threaded_mainloop_unlock(i
->mainloop
);
1434 case SNDCTL_DSP_SPEED
: {
1439 debug(__FILE__
": SNDCTL_DSP_SPEED: %i\n", *(int*) argp
);
1441 pa_threaded_mainloop_lock(i
->mainloop
);
1443 ss
= i
->sample_spec
;
1444 ss
.rate
= *(int*) argp
;
1446 if ((valid
= pa_sample_spec_valid(&ss
))) {
1447 i
->sample_spec
= ss
;
1451 debug(__FILE__
": ss: %s\n", pa_sample_spec_snprint(t
, sizeof(t
), &i
->sample_spec
));
1453 pa_threaded_mainloop_unlock(i
->mainloop
);
1463 case SNDCTL_DSP_STEREO
:
1464 debug(__FILE__
": SNDCTL_DSP_STEREO: %i\n", *(int*) argp
);
1466 pa_threaded_mainloop_lock(i
->mainloop
);
1468 i
->sample_spec
.channels
= *(int*) argp
? 2 : 1;
1471 pa_threaded_mainloop_unlock(i
->mainloop
);
1474 case SNDCTL_DSP_CHANNELS
: {
1478 debug(__FILE__
": SNDCTL_DSP_CHANNELS: %i\n", *(int*) argp
);
1480 pa_threaded_mainloop_lock(i
->mainloop
);
1482 ss
= i
->sample_spec
;
1483 ss
.channels
= *(int*) argp
;
1485 if ((valid
= pa_sample_spec_valid(&ss
))) {
1486 i
->sample_spec
= ss
;
1490 pa_threaded_mainloop_unlock(i
->mainloop
);
1500 case SNDCTL_DSP_GETBLKSIZE
:
1501 debug(__FILE__
": SNDCTL_DSP_GETBLKSIZE\n");
1503 pa_threaded_mainloop_lock(i
->mainloop
);
1506 *(int*) argp
= i
->fragment_size
;
1508 pa_threaded_mainloop_unlock(i
->mainloop
);
1512 case SNDCTL_DSP_SETFRAGMENT
:
1513 debug(__FILE__
": SNDCTL_DSP_SETFRAGMENT: 0x%8x\n", *(int*) argp
);
1515 pa_threaded_mainloop_lock(i
->mainloop
);
1517 i
->fragment_size
= 1 << (*(int*) argp
);
1518 i
->n_fragments
= (*(int*) argp
) >> 16;
1522 pa_threaded_mainloop_unlock(i
->mainloop
);
1526 case SNDCTL_DSP_GETCAPS
:
1527 debug(__FILE__
": SNDCTL_DSP_CAPS\n");
1529 *(int*) argp
= DSP_CAP_MULTI
;
1532 case SNDCTL_DSP_GETODELAY
: {
1535 debug(__FILE__
": SNDCTL_DSP_GETODELAY\n");
1537 pa_threaded_mainloop_lock(i
->mainloop
);
1544 STREAM_CHECK_DEAD_GOTO(i
, exit_loop
);
1546 if (pa_stream_get_latency(i
->stream
, &usec
, NULL
) >= 0) {
1547 *(int*) argp
= pa_usec_to_bytes(usec
, &i
->sample_spec
);
1551 if (pa_context_errno(i
->context
) != PA_ERR_NODATA
) {
1552 debug(__FILE__
": pa_stream_get_latency(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1556 pa_threaded_mainloop_wait(i
->mainloop
);
1561 if (ioctl(i
->thread_fd
, SIOCINQ
, &l
) < 0)
1562 debug(__FILE__
": SIOCINQ failed: %s\n", strerror(errno
));
1566 pa_threaded_mainloop_unlock(i
->mainloop
);
1568 debug(__FILE__
": ODELAY: %i\n", *(int*) argp
);
1573 case SNDCTL_DSP_RESET
: {
1574 debug(__FILE__
": SNDCTL_DSP_RESET\n");
1576 pa_threaded_mainloop_lock(i
->mainloop
);
1579 dsp_flush_socket(i
);
1582 pa_threaded_mainloop_unlock(i
->mainloop
);
1586 case SNDCTL_DSP_GETFMTS
: {
1587 debug(__FILE__
": SNDCTL_DSP_GETFMTS\n");
1589 *(int*) argp
= AFMT_MU_LAW
|AFMT_A_LAW
|AFMT_U8
|AFMT_S16_LE
|AFMT_S16_BE
;
1593 case SNDCTL_DSP_POST
:
1594 debug(__FILE__
": SNDCTL_DSP_POST\n");
1596 if (dsp_trigger(i
) < 0)
1600 case SNDCTL_DSP_SYNC
:
1601 debug(__FILE__
": SNDCTL_DSP_SYNC\n");
1603 if (dsp_drain(i
) < 0)
1608 case SNDCTL_DSP_GETOSPACE
: {
1609 audio_buf_info
*bi
= (audio_buf_info
*) argp
;
1613 debug(__FILE__
": SNDCTL_DSP_GETOSPACE\n");
1615 pa_threaded_mainloop_lock(i
->mainloop
);
1620 if ((k
= pa_stream_writable_size(i
->stream
)) == (size_t) -1)
1621 debug(__FILE__
": pa_stream_writable_size(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1623 k
= i
->fragment_size
* i
->n_fragments
;
1625 if (ioctl(i
->thread_fd
, SIOCINQ
, &l
) < 0) {
1626 debug(__FILE__
": SIOCINQ failed: %s\n", strerror(errno
));
1630 bi
->fragsize
= i
->fragment_size
;
1631 bi
->fragstotal
= i
->n_fragments
;
1632 bi
->bytes
= k
> (size_t) l
? k
- l
: 0;
1633 bi
->fragments
= bi
->bytes
/ bi
->fragsize
;
1635 pa_threaded_mainloop_unlock(i
->mainloop
);
1637 debug(__FILE__
": fragsize=%i, fragstotal=%i, bytes=%i, fragments=%i\n", bi
->fragsize
, bi
->fragstotal
, bi
->bytes
, bi
->fragments
);
1643 debug(__FILE__
": unknwon ioctl 0x%08lx\n", request
);
1656 int ioctl(int fd
, unsigned long request
, ...) {
1662 debug(__FILE__
": ioctl()\n");
1664 va_start(args
, request
);
1665 argp
= va_arg(args
, void *);
1668 if (!function_enter()) {
1670 return _ioctl(fd
, request
, argp
);
1673 if (!(i
= fd_info_find(fd
))) {
1676 return _ioctl(fd
, request
, argp
);
1679 if (i
->type
== FD_INFO_MIXER
)
1680 r
= mixer_ioctl(i
, request
, argp
, &_errno
);
1682 r
= dsp_ioctl(i
, request
, argp
, &_errno
);
1697 debug(__FILE__
": close()\n");
1699 if (!function_enter()) {
1704 if (!(i
= fd_info_find(fd
))) {
1710 fd_info_remove_from_list(i
);
1718 int open64(const char *filename
, int flags
, ...) {
1722 debug(__FILE__
": open64(%s)\n", filename
);
1724 va_start(args
, flags
);
1725 if (flags
& O_CREAT
)
1726 mode
= va_arg(args
, mode_t
);
1729 if (strcmp(filename
, "/dev/dsp") != 0 &&
1730 strcmp(filename
, "/dev/adsp") != 0 &&
1731 strcmp(filename
, "/dev/sndstat") != 0 &&
1732 strcmp(filename
, "/dev/mixer") != 0) {
1734 return _open64(filename
, flags
, mode
);
1737 return open(filename
, flags
, mode
);
1740 FILE* fopen(const char *filename
, const char *mode
) {
1745 debug(__FILE__
": fopen(%s)\n", filename
);
1747 if (strcmp(filename
, "/dev/dsp") == 0 ||
1748 strcmp(filename
, "/dev/adsp") == 0) {
1750 if (strcmp(mode
, "wb") != 0) {
1756 } else if (strcmp(filename
, "/dev/sndstat") == 0) {
1758 if (strcmp(mode
, "r") != 0) {
1764 } else if (strcmp(filename
, "/dev/mixer") == 0)
1768 return _fopen(filename
, mode
);
1771 if ((fd
= open(filename
, m
)) < 0)
1774 if (!(f
= fdopen(fd
, mode
))) {
1782 FILE *fopen64(const char *filename
, const char *mode
) {
1784 debug(__FILE__
": fopen64(%s)\n", filename
);
1786 if (strcmp(filename
, "/dev/dsp") != 0 &&
1787 strcmp(filename
, "/dev/adsp") != 0 &&
1788 strcmp(filename
, "/dev/sndstat") != 0 &&
1789 strcmp(filename
, "/dev/mixer") != 0) {
1790 LOAD_FOPEN64_FUNC();
1791 return _fopen64(filename
, mode
);
1794 return fopen(filename
, mode
);
1797 int fclose(FILE *f
) {
1800 debug(__FILE__
": fclose()\n");
1802 if (!function_enter()) {
1807 if (!(i
= fd_info_find(fileno(f
)))) {
1813 fd_info_remove_from_list(i
);
1815 /* Dirty trick to avoid that the fd is not freed twice, once by us
1816 * and once by the real fclose() */