4 This file is part of PulseAudio.
6 PulseAudio 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 PulseAudio 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 PulseAudio; 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>
49 #include <linux/sockios.h>
52 #include <pulse/pulseaudio.h>
53 #include <pulsecore/llist.h>
54 #include <pulsecore/gccmacro.h>
56 /* On some systems SIOCINQ isn't defined, but FIONREAD is just an alias */
57 #if !defined(SIOCINQ) && defined(FIONREAD)
58 # define SIOCINQ FIONREAD
66 typedef struct fd_info fd_info
;
69 pthread_mutex_t mutex
;
74 int app_fd
, thread_fd
;
76 pa_sample_spec sample_spec
;
80 pa_threaded_mainloop
*mainloop
;
82 pa_stream
*play_stream
;
83 pa_stream
*rec_stream
;
85 pa_io_event
*io_event
;
86 pa_io_event_flags_t io_flags
;
91 int operation_success
;
93 pa_cvolume sink_volume
, source_volume
;
94 uint32_t sink_index
, source_index
;
95 int volume_modify_count
;
99 PA_LLIST_FIELDS(fd_info
);
102 static int dsp_drain(fd_info
*i
);
103 static void fd_info_remove_from_list(fd_info
*i
);
105 static pthread_mutex_t fd_infos_mutex
= PTHREAD_MUTEX_INITIALIZER
;
106 static pthread_mutex_t func_mutex
= PTHREAD_MUTEX_INITIALIZER
;
108 static PA_LLIST_HEAD(fd_info
, fd_infos
) = NULL
;
110 static int (*_ioctl
)(int, int, void*) = NULL
;
111 static int (*_close
)(int) = NULL
;
112 static int (*_open
)(const char *, int, mode_t
) = NULL
;
113 static FILE* (*_fopen
)(const char *path
, const char *mode
) = NULL
;
115 static int (*_open64
)(const char *, int, mode_t
) = NULL
;
116 static FILE* (*_fopen64
)(const char *path
, const char *mode
) = NULL
;
118 static int (*_fclose
)(FILE *f
) = NULL
;
119 static int (*_access
)(const char *, int) = NULL
;
121 /* dlsym() violates ISO C, so confide the breakage into this function to
123 typedef void (*fnptr
)(void);
124 static inline fnptr
dlsym_fn(void *handle
, const char *symbol
) {
125 return (fnptr
) (long) dlsym(handle
, symbol
);
128 #define LOAD_IOCTL_FUNC() \
130 pthread_mutex_lock(&func_mutex); \
132 _ioctl = (int (*)(int, int, void*)) dlsym_fn(RTLD_NEXT, "ioctl"); \
133 pthread_mutex_unlock(&func_mutex); \
136 #define LOAD_OPEN_FUNC() \
138 pthread_mutex_lock(&func_mutex); \
140 _open = (int (*)(const char *, int, mode_t)) dlsym_fn(RTLD_NEXT, "open"); \
141 pthread_mutex_unlock(&func_mutex); \
144 #define LOAD_OPEN64_FUNC() \
146 pthread_mutex_lock(&func_mutex); \
148 _open64 = (int (*)(const char *, int, mode_t)) dlsym_fn(RTLD_NEXT, "open64"); \
149 pthread_mutex_unlock(&func_mutex); \
152 #define LOAD_CLOSE_FUNC() \
154 pthread_mutex_lock(&func_mutex); \
156 _close = (int (*)(int)) dlsym_fn(RTLD_NEXT, "close"); \
157 pthread_mutex_unlock(&func_mutex); \
160 #define LOAD_ACCESS_FUNC() \
162 pthread_mutex_lock(&func_mutex); \
164 _access = (int (*)(const char*, int)) dlsym_fn(RTLD_NEXT, "access"); \
165 pthread_mutex_unlock(&func_mutex); \
168 #define LOAD_FOPEN_FUNC() \
170 pthread_mutex_lock(&func_mutex); \
172 _fopen = (FILE* (*)(const char *, const char*)) dlsym_fn(RTLD_NEXT, "fopen"); \
173 pthread_mutex_unlock(&func_mutex); \
176 #define LOAD_FOPEN64_FUNC() \
178 pthread_mutex_lock(&func_mutex); \
180 _fopen64 = (FILE* (*)(const char *, const char*)) dlsym_fn(RTLD_NEXT, "fopen64"); \
181 pthread_mutex_unlock(&func_mutex); \
184 #define LOAD_FCLOSE_FUNC() \
186 pthread_mutex_lock(&func_mutex); \
188 _fclose = (int (*)(FILE *)) dlsym_fn(RTLD_NEXT, "fclose"); \
189 pthread_mutex_unlock(&func_mutex); \
192 #define CONTEXT_CHECK_DEAD_GOTO(i, label) do { \
193 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY) { \
194 debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s\n", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
199 #define PLAYBACK_STREAM_CHECK_DEAD_GOTO(i, label) do { \
200 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY || \
201 !(i)->play_stream || pa_stream_get_state((i)->play_stream) != PA_STREAM_READY) { \
202 debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s\n", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
207 #define RECORD_STREAM_CHECK_DEAD_GOTO(i, label) do { \
208 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY || \
209 !(i)->rec_stream || pa_stream_get_state((i)->rec_stream) != PA_STREAM_READY) { \
210 debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s\n", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
215 static void debug(int level
, const char *format
, ...) PA_GCC_PRINTF_ATTR(2,3);
217 #define DEBUG_LEVEL_ALWAYS 0
218 #define DEBUG_LEVEL_NORMAL 1
219 #define DEBUG_LEVEL_VERBOSE 2
221 static void debug(int level
, const char *format
, ...) {
223 const char *dlevel_s
;
226 dlevel_s
= getenv("PADSP_DEBUG");
230 dlevel
= atoi(dlevel_s
);
235 va_start(ap
, format
);
236 vfprintf(stderr
, format
, ap
);
240 static int padsp_disabled(void) {
242 static int sym_resolved
= 0;
244 /* If the current process has a symbol __padsp_disabled__ we use
245 * it to detect whether we should enable our stuff or not. A
246 * program needs to be compiled with -rdynamic for this to work!
247 * The symbol must be an int containing a three bit bitmask: bit 1
248 * -> disable /dev/dsp emulation, bit 2 -> disable /dev/sndstat
249 * emulation, bit 3 -> disable /dev/mixer emulation. Hence a value
250 * of 7 disables padsp entirely. */
252 pthread_mutex_lock(&func_mutex
);
254 sym
= (int*) dlsym(RTLD_DEFAULT
, "__padsp_disabled__");
258 pthread_mutex_unlock(&func_mutex
);
266 static int dsp_cloak_enable(void) {
267 if (padsp_disabled() & 1)
270 if (getenv("PADSP_NO_DSP"))
276 static int sndstat_cloak_enable(void) {
277 if (padsp_disabled() & 2)
280 if (getenv("PADSP_NO_SNDSTAT"))
286 static int mixer_cloak_enable(void) {
287 if (padsp_disabled() & 4)
290 if (getenv("PADSP_NO_MIXER"))
295 static pthread_key_t recursion_key
;
297 static void recursion_key_alloc(void) {
298 pthread_key_create(&recursion_key
, NULL
);
301 static int function_enter(void) {
302 /* Avoid recursive calls */
303 static pthread_once_t recursion_key_once
= PTHREAD_ONCE_INIT
;
304 pthread_once(&recursion_key_once
, recursion_key_alloc
);
306 if (pthread_getspecific(recursion_key
))
309 pthread_setspecific(recursion_key
, (void*) 1);
313 static void function_exit(void) {
314 pthread_setspecific(recursion_key
, NULL
);
317 static void fd_info_free(fd_info
*i
) {
320 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": freeing fd info (fd=%i)\n", i
->app_fd
);
325 pa_threaded_mainloop_stop(i
->mainloop
);
327 if (i
->play_stream
) {
328 pa_stream_disconnect(i
->play_stream
);
329 pa_stream_unref(i
->play_stream
);
333 pa_stream_disconnect(i
->rec_stream
);
334 pa_stream_unref(i
->rec_stream
);
338 pa_context_disconnect(i
->context
);
339 pa_context_unref(i
->context
);
343 pa_threaded_mainloop_free(i
->mainloop
);
345 if (i
->app_fd
>= 0) {
350 if (i
->thread_fd
>= 0) {
352 _close(i
->thread_fd
);
357 pthread_mutex_destroy(&i
->mutex
);
361 static fd_info
*fd_info_ref(fd_info
*i
) {
364 pthread_mutex_lock(&i
->mutex
);
368 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": ref++, now %i\n", i
->ref
);
369 pthread_mutex_unlock(&i
->mutex
);
374 static void fd_info_unref(fd_info
*i
) {
376 pthread_mutex_lock(&i
->mutex
);
379 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": ref--, now %i\n", i
->ref
);
380 pthread_mutex_unlock(&i
->mutex
);
386 static void context_state_cb(pa_context
*c
, void *userdata
) {
387 fd_info
*i
= userdata
;
390 switch (pa_context_get_state(c
)) {
391 case PA_CONTEXT_READY
:
392 case PA_CONTEXT_TERMINATED
:
393 case PA_CONTEXT_FAILED
:
394 pa_threaded_mainloop_signal(i
->mainloop
, 0);
397 case PA_CONTEXT_UNCONNECTED
:
398 case PA_CONTEXT_CONNECTING
:
399 case PA_CONTEXT_AUTHORIZING
:
400 case PA_CONTEXT_SETTING_NAME
:
405 static void reset_params(fd_info
*i
) {
408 i
->sample_spec
.format
= PA_SAMPLE_U8
;
409 i
->sample_spec
.channels
= 1;
410 i
->sample_spec
.rate
= 8000;
411 i
->fragment_size
= 0;
415 static const char *client_name(char *buf
, size_t n
) {
419 if ((e
= getenv("PADSP_CLIENT_NAME")))
422 if (pa_get_binary_name(p
, sizeof(p
)))
423 snprintf(buf
, n
, "OSS Emulation[%s]", p
);
425 snprintf(buf
, n
, "OSS");
430 static const char *stream_name(void) {
433 if ((e
= getenv("PADSP_STREAM_NAME")))
436 return "Audio Stream";
439 static void atfork_prepare(void) {
442 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": atfork_prepare() enter\n");
446 pthread_mutex_lock(&fd_infos_mutex
);
448 for (i
= fd_infos
; i
; i
= i
->next
) {
449 pthread_mutex_lock(&i
->mutex
);
450 pa_threaded_mainloop_lock(i
->mainloop
);
453 pthread_mutex_lock(&func_mutex
);
456 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": atfork_prepare() exit\n");
459 static void atfork_parent(void) {
462 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": atfork_parent() enter\n");
464 pthread_mutex_unlock(&func_mutex
);
466 for (i
= fd_infos
; i
; i
= i
->next
) {
467 pa_threaded_mainloop_unlock(i
->mainloop
);
468 pthread_mutex_unlock(&i
->mutex
);
471 pthread_mutex_unlock(&fd_infos_mutex
);
475 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": atfork_parent() exit\n");
478 static void atfork_child(void) {
481 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": atfork_child() enter\n");
483 /* We do only the bare minimum to get all fds closed */
484 pthread_mutex_init(&func_mutex
, NULL
);
485 pthread_mutex_init(&fd_infos_mutex
, NULL
);
487 for (i
= fd_infos
; i
; i
= i
->next
) {
488 pthread_mutex_init(&i
->mutex
, NULL
);
491 pa_context_disconnect(i
->context
);
492 pa_context_unref(i
->context
);
496 if (i
->play_stream
) {
497 pa_stream_unref(i
->play_stream
);
498 i
->play_stream
= NULL
;
502 pa_stream_unref(i
->rec_stream
);
503 i
->rec_stream
= NULL
;
506 if (i
->app_fd
>= 0) {
511 if (i
->thread_fd
>= 0) {
521 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": atfork_child() exit\n");
524 static void install_atfork(void) {
525 pthread_atfork(atfork_prepare
, atfork_parent
, atfork_child
);
528 static void stream_success_cb(pa_stream
*s
, int success
, void *userdata
) {
529 fd_info
*i
= userdata
;
534 i
->operation_success
= success
;
535 pa_threaded_mainloop_signal(i
->mainloop
, 0);
538 static void context_success_cb(pa_context
*c
, int success
, void *userdata
) {
539 fd_info
*i
= userdata
;
544 i
->operation_success
= success
;
545 pa_threaded_mainloop_signal(i
->mainloop
, 0);
548 static fd_info
* fd_info_new(fd_info_type_t type
, int *_errno
) {
550 int sfds
[2] = { -1, -1 };
552 static pthread_once_t install_atfork_once
= PTHREAD_ONCE_INIT
;
554 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": fd_info_new()\n");
556 signal(SIGPIPE
, SIG_IGN
); /* Yes, ugly as hell */
558 pthread_once(&install_atfork_once
, install_atfork
);
560 if (!(i
= malloc(sizeof(fd_info
)))) {
565 i
->app_fd
= i
->thread_fd
= -1;
570 i
->play_stream
= NULL
;
571 i
->rec_stream
= NULL
;
574 pthread_mutex_init(&i
->mutex
, NULL
);
579 pa_cvolume_reset(&i
->sink_volume
, 2);
580 pa_cvolume_reset(&i
->source_volume
, 2);
581 i
->volume_modify_count
= 0;
582 i
->sink_index
= (uint32_t) -1;
583 i
->source_index
= (uint32_t) -1;
584 i
->optr_n_blocks
= 0;
585 PA_LLIST_INIT(fd_info
, i
);
589 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, sfds
) < 0) {
591 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": socket() failed: %s\n", strerror(errno
));
596 i
->thread_fd
= sfds
[1];
598 if (!(i
->mainloop
= pa_threaded_mainloop_new())) {
600 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_threaded_mainloop_new() failed\n");
604 if (!(i
->context
= pa_context_new(pa_threaded_mainloop_get_api(i
->mainloop
), client_name(name
, sizeof(name
))))) {
606 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_context_new() failed\n");
610 pa_context_set_state_callback(i
->context
, context_state_cb
, i
);
612 if (pa_context_connect(i
->context
, NULL
, 0, NULL
) < 0) {
613 *_errno
= ECONNREFUSED
;
614 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
618 pa_threaded_mainloop_lock(i
->mainloop
);
620 if (pa_threaded_mainloop_start(i
->mainloop
) < 0) {
622 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_threaded_mainloop_start() failed\n");
623 goto unlock_and_fail
;
626 /* Wait until the context is ready */
627 pa_threaded_mainloop_wait(i
->mainloop
);
629 if (pa_context_get_state(i
->context
) != PA_CONTEXT_READY
) {
630 *_errno
= ECONNREFUSED
;
631 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
632 goto unlock_and_fail
;
635 pa_threaded_mainloop_unlock(i
->mainloop
);
640 pa_threaded_mainloop_unlock(i
->mainloop
);
650 static void fd_info_add_to_list(fd_info
*i
) {
653 pthread_mutex_lock(&fd_infos_mutex
);
654 PA_LLIST_PREPEND(fd_info
, fd_infos
, i
);
655 pthread_mutex_unlock(&fd_infos_mutex
);
660 static void fd_info_remove_from_list(fd_info
*i
) {
663 pthread_mutex_lock(&fd_infos_mutex
);
664 PA_LLIST_REMOVE(fd_info
, fd_infos
, i
);
665 pthread_mutex_unlock(&fd_infos_mutex
);
670 static fd_info
* fd_info_find(int fd
) {
673 pthread_mutex_lock(&fd_infos_mutex
);
675 for (i
= fd_infos
; i
; i
= i
->next
)
676 if (i
->app_fd
== fd
&& !i
->unusable
) {
681 pthread_mutex_unlock(&fd_infos_mutex
);
686 static void fix_metrics(fd_info
*i
) {
688 char t
[PA_SAMPLE_SPEC_SNPRINT_MAX
];
690 fs
= pa_frame_size(&i
->sample_spec
);
692 /* Don't fix things more than necessary */
693 if ((i
->fragment_size
% fs
) == 0 &&
694 i
->n_fragments
>= 2 &&
695 i
->fragment_size
> 0)
698 i
->fragment_size
= (i
->fragment_size
/fs
)*fs
;
700 /* Number of fragments set? */
701 if (i
->n_fragments
< 2) {
702 if (i
->fragment_size
> 0) {
703 i
->n_fragments
= pa_bytes_per_second(&i
->sample_spec
) / 2 / i
->fragment_size
;
704 if (i
->n_fragments
< 2)
710 /* Fragment size set? */
711 if (i
->fragment_size
<= 0) {
712 i
->fragment_size
= pa_bytes_per_second(&i
->sample_spec
) / 2 / i
->n_fragments
;
713 if (i
->fragment_size
< 1024)
714 i
->fragment_size
= 1024;
717 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": sample spec: %s\n", pa_sample_spec_snprint(t
, sizeof(t
), &i
->sample_spec
));
718 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": fixated metrics to %i fragments, %li bytes each.\n", i
->n_fragments
, (long)i
->fragment_size
);
721 static void stream_request_cb(pa_stream
*s
, size_t length
, void *userdata
) {
722 fd_info
*i
= userdata
;
726 pa_mainloop_api
*api
;
729 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
731 if (s
== i
->play_stream
) {
732 n
= pa_stream_writable_size(i
->play_stream
);
733 if (n
== (size_t)-1) {
734 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_writable_size(): %s\n",
735 pa_strerror(pa_context_errno(i
->context
)));
738 if (n
>= i
->fragment_size
)
739 i
->io_flags
|= PA_IO_EVENT_INPUT
;
741 i
->io_flags
&= ~PA_IO_EVENT_INPUT
;
744 if (s
== i
->rec_stream
) {
745 n
= pa_stream_readable_size(i
->rec_stream
);
746 if (n
== (size_t)-1) {
747 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_readable_size(): %s\n",
748 pa_strerror(pa_context_errno(i
->context
)));
751 if (n
>= i
->fragment_size
)
752 i
->io_flags
|= PA_IO_EVENT_OUTPUT
;
754 i
->io_flags
&= ~PA_IO_EVENT_OUTPUT
;
757 api
->io_enable(i
->io_event
, i
->io_flags
);
761 static void stream_latency_update_cb(pa_stream
*s
, void *userdata
) {
762 fd_info
*i
= userdata
;
765 pa_threaded_mainloop_signal(i
->mainloop
, 0);
768 static void fd_info_shutdown(fd_info
*i
) {
772 pa_mainloop_api
*api
;
773 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
774 api
->io_free(i
->io_event
);
779 if (i
->thread_fd
>= 0) {
785 static int fd_info_copy_data(fd_info
*i
, int force
) {
788 if (!i
->play_stream
&& !i
->rec_stream
)
791 if ((i
->play_stream
) && (pa_stream_get_state(i
->play_stream
) == PA_STREAM_READY
)) {
792 n
= pa_stream_writable_size(i
->play_stream
);
794 if (n
== (size_t)-1) {
795 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_writable_size(): %s\n",
796 pa_strerror(pa_context_errno(i
->context
)));
800 while (n
>= i
->fragment_size
|| force
) {
804 if (!(i
->buf
= malloc(i
->fragment_size
))) {
805 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": malloc() failed.\n");
810 if ((r
= read(i
->thread_fd
, i
->buf
, i
->fragment_size
)) <= 0) {
815 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": read(): %s\n", r
== 0 ? "EOF" : strerror(errno
));
819 if (pa_stream_write(i
->play_stream
, i
->buf
, r
, free
, 0, PA_SEEK_RELATIVE
) < 0) {
820 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_write(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
826 assert(n
>= (size_t) r
);
830 if (n
>= i
->fragment_size
)
831 i
->io_flags
|= PA_IO_EVENT_INPUT
;
833 i
->io_flags
&= ~PA_IO_EVENT_INPUT
;
836 if ((i
->rec_stream
) && (pa_stream_get_state(i
->rec_stream
) == PA_STREAM_READY
)) {
837 n
= pa_stream_readable_size(i
->rec_stream
);
839 if (n
== (size_t)-1) {
840 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_readable_size(): %s\n",
841 pa_strerror(pa_context_errno(i
->context
)));
845 while (n
>= i
->fragment_size
|| force
) {
851 if (pa_stream_peek(i
->rec_stream
, &data
, &len
) < 0) {
852 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_peek(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
859 buf
= (const char*)data
+ i
->rec_offset
;
861 if ((r
= write(i
->thread_fd
, buf
, len
- i
->rec_offset
)) <= 0) {
866 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": write(): %s\n", strerror(errno
));
870 assert((size_t)r
<= len
- i
->rec_offset
);
873 if (i
->rec_offset
== len
) {
874 if (pa_stream_drop(i
->rec_stream
) < 0) {
875 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_drop(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
881 assert(n
>= (size_t) r
);
885 if (n
>= i
->fragment_size
)
886 i
->io_flags
|= PA_IO_EVENT_OUTPUT
;
888 i
->io_flags
&= ~PA_IO_EVENT_OUTPUT
;
892 pa_mainloop_api
*api
;
894 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
895 api
->io_enable(i
->io_event
, i
->io_flags
);
901 static void stream_state_cb(pa_stream
*s
, void * userdata
) {
902 fd_info
*i
= userdata
;
905 switch (pa_stream_get_state(s
)) {
907 case PA_STREAM_READY
:
908 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": stream established.\n");
911 case PA_STREAM_FAILED
:
912 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
916 case PA_STREAM_TERMINATED
:
917 case PA_STREAM_UNCONNECTED
:
918 case PA_STREAM_CREATING
:
923 static int create_playback_stream(fd_info
*i
) {
931 if (!(i
->play_stream
= pa_stream_new(i
->context
, stream_name(), &i
->sample_spec
, NULL
))) {
932 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
936 pa_stream_set_state_callback(i
->play_stream
, stream_state_cb
, i
);
937 pa_stream_set_write_callback(i
->play_stream
, stream_request_cb
, i
);
938 pa_stream_set_latency_update_callback(i
->play_stream
, stream_latency_update_cb
, i
);
940 memset(&attr
, 0, sizeof(attr
));
941 attr
.maxlength
= i
->fragment_size
* (i
->n_fragments
+1);
942 attr
.tlength
= i
->fragment_size
* i
->n_fragments
;
943 attr
.prebuf
= i
->fragment_size
;
944 attr
.minreq
= i
->fragment_size
;
946 if (pa_stream_connect_playback(i
->play_stream
, NULL
, &attr
, PA_STREAM_INTERPOLATE_TIMING
|PA_STREAM_AUTO_TIMING_UPDATE
, NULL
, NULL
) < 0) {
947 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
951 n
= i
->fragment_size
;
952 setsockopt(i
->app_fd
, SOL_SOCKET
, SO_SNDBUF
, &n
, sizeof(n
));
953 n
= i
->fragment_size
;
954 setsockopt(i
->thread_fd
, SOL_SOCKET
, SO_RCVBUF
, &n
, sizeof(n
));
962 static int create_record_stream(fd_info
*i
) {
970 if (!(i
->rec_stream
= pa_stream_new(i
->context
, stream_name(), &i
->sample_spec
, NULL
))) {
971 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
975 pa_stream_set_state_callback(i
->rec_stream
, stream_state_cb
, i
);
976 pa_stream_set_read_callback(i
->rec_stream
, stream_request_cb
, i
);
977 pa_stream_set_latency_update_callback(i
->rec_stream
, stream_latency_update_cb
, i
);
979 memset(&attr
, 0, sizeof(attr
));
980 attr
.maxlength
= i
->fragment_size
* (i
->n_fragments
+1);
981 attr
.fragsize
= i
->fragment_size
;
983 if (pa_stream_connect_record(i
->rec_stream
, NULL
, &attr
, PA_STREAM_INTERPOLATE_TIMING
|PA_STREAM_AUTO_TIMING_UPDATE
) < 0) {
984 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
988 n
= i
->fragment_size
;
989 setsockopt(i
->app_fd
, SOL_SOCKET
, SO_RCVBUF
, &n
, sizeof(n
));
990 n
= i
->fragment_size
;
991 setsockopt(i
->thread_fd
, SOL_SOCKET
, SO_SNDBUF
, &n
, sizeof(n
));
999 static void free_streams(fd_info
*i
) {
1002 if (i
->play_stream
) {
1003 pa_stream_disconnect(i
->play_stream
);
1004 pa_stream_unref(i
->play_stream
);
1005 i
->play_stream
= NULL
;
1006 i
->io_flags
|= PA_IO_EVENT_INPUT
;
1009 if (i
->rec_stream
) {
1010 pa_stream_disconnect(i
->rec_stream
);
1011 pa_stream_unref(i
->rec_stream
);
1012 i
->rec_stream
= NULL
;
1013 i
->io_flags
|= PA_IO_EVENT_OUTPUT
;
1017 pa_mainloop_api
*api
;
1019 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
1020 api
->io_enable(i
->io_event
, i
->io_flags
);
1024 static void io_event_cb(pa_mainloop_api
*api
, pa_io_event
*e
, int fd
, pa_io_event_flags_t flags
, void *userdata
) {
1025 fd_info
*i
= userdata
;
1027 pa_threaded_mainloop_signal(i
->mainloop
, 0);
1029 if (flags
& PA_IO_EVENT_INPUT
) {
1031 if (!i
->play_stream
) {
1032 if (create_playback_stream(i
) < 0)
1035 if (fd_info_copy_data(i
, 0) < 0)
1039 } else if (flags
& PA_IO_EVENT_OUTPUT
) {
1041 if (!i
->rec_stream
) {
1042 if (create_record_stream(i
) < 0)
1045 if (fd_info_copy_data(i
, 0) < 0)
1049 } else if (flags
& (PA_IO_EVENT_HANGUP
|PA_IO_EVENT_ERROR
))
1055 /* We can't do anything better than removing the event source */
1056 fd_info_shutdown(i
);
1059 static int dsp_open(int flags
, int *_errno
) {
1061 pa_mainloop_api
*api
;
1065 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": dsp_open()\n");
1067 if (!(i
= fd_info_new(FD_INFO_STREAM
, _errno
)))
1070 if ((flags
& O_NONBLOCK
) == O_NONBLOCK
) {
1071 if ((f
= fcntl(i
->app_fd
, F_GETFL
)) >= 0)
1072 fcntl(i
->app_fd
, F_SETFL
, f
|O_NONBLOCK
);
1074 if ((f
= fcntl(i
->thread_fd
, F_GETFL
)) >= 0)
1075 fcntl(i
->thread_fd
, F_SETFL
, f
|O_NONBLOCK
);
1077 fcntl(i
->app_fd
, F_SETFD
, FD_CLOEXEC
);
1078 fcntl(i
->thread_fd
, F_SETFD
, FD_CLOEXEC
);
1080 pa_threaded_mainloop_lock(i
->mainloop
);
1081 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
1083 switch (flags
& O_ACCMODE
) {
1085 i
->io_flags
= PA_IO_EVENT_OUTPUT
;
1086 shutdown(i
->thread_fd
, SHUT_RD
);
1087 shutdown(i
->app_fd
, SHUT_WR
);
1090 i
->io_flags
= PA_IO_EVENT_INPUT
;
1091 shutdown(i
->thread_fd
, SHUT_WR
);
1092 shutdown(i
->app_fd
, SHUT_RD
);
1095 i
->io_flags
= PA_IO_EVENT_INPUT
| PA_IO_EVENT_OUTPUT
;
1101 if (!(i
->io_event
= api
->io_new(api
, i
->thread_fd
, i
->io_flags
, io_event_cb
, i
)))
1104 pa_threaded_mainloop_unlock(i
->mainloop
);
1106 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": dsp_open() succeeded, fd=%i\n", i
->app_fd
);
1108 fd_info_add_to_list(i
);
1115 pa_threaded_mainloop_unlock(i
->mainloop
);
1122 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": dsp_open() failed\n");
1127 static void sink_info_cb(pa_context
*context
, const pa_sink_info
*si
, int eol
, void *userdata
) {
1128 fd_info
*i
= userdata
;
1130 if (!si
&& eol
< 0) {
1131 i
->operation_success
= 0;
1132 pa_threaded_mainloop_signal(i
->mainloop
, 0);
1139 if (!pa_cvolume_equal(&i
->sink_volume
, &si
->volume
))
1140 i
->volume_modify_count
++;
1142 i
->sink_volume
= si
->volume
;
1143 i
->sink_index
= si
->index
;
1145 i
->operation_success
= 1;
1146 pa_threaded_mainloop_signal(i
->mainloop
, 0);
1149 static void source_info_cb(pa_context
*context
, const pa_source_info
*si
, int eol
, void *userdata
) {
1150 fd_info
*i
= userdata
;
1152 if (!si
&& eol
< 0) {
1153 i
->operation_success
= 0;
1154 pa_threaded_mainloop_signal(i
->mainloop
, 0);
1161 if (!pa_cvolume_equal(&i
->source_volume
, &si
->volume
))
1162 i
->volume_modify_count
++;
1164 i
->source_volume
= si
->volume
;
1165 i
->source_index
= si
->index
;
1167 i
->operation_success
= 1;
1168 pa_threaded_mainloop_signal(i
->mainloop
, 0);
1171 static void subscribe_cb(pa_context
*context
, pa_subscription_event_type_t t
, uint32_t idx
, void *userdata
) {
1172 fd_info
*i
= userdata
;
1173 pa_operation
*o
= NULL
;
1175 if (i
->sink_index
!= idx
)
1178 if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) != PA_SUBSCRIPTION_EVENT_CHANGE
)
1181 if (!(o
= pa_context_get_sink_info_by_index(i
->context
, i
->sink_index
, sink_info_cb
, i
))) {
1182 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to get sink info: %s", pa_strerror(pa_context_errno(i
->context
)));
1186 pa_operation_unref(o
);
1189 static int mixer_open(int flags
, int *_errno
) {
1191 pa_operation
*o
= NULL
;
1194 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": mixer_open()\n");
1196 if (!(i
= fd_info_new(FD_INFO_MIXER
, _errno
)))
1199 pa_threaded_mainloop_lock(i
->mainloop
);
1201 pa_context_set_subscribe_callback(i
->context
, subscribe_cb
, i
);
1203 if (!(o
= pa_context_subscribe(i
->context
, PA_SUBSCRIPTION_MASK_SINK
| PA_SUBSCRIPTION_MASK_SOURCE
, context_success_cb
, i
))) {
1204 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to subscribe to events: %s", pa_strerror(pa_context_errno(i
->context
)));
1209 i
->operation_success
= 0;
1210 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1211 pa_threaded_mainloop_wait(i
->mainloop
);
1212 CONTEXT_CHECK_DEAD_GOTO(i
, fail
);
1215 pa_operation_unref(o
);
1218 if (!i
->operation_success
) {
1219 debug(DEBUG_LEVEL_NORMAL
, __FILE__
":Failed to subscribe to events: %s", pa_strerror(pa_context_errno(i
->context
)));
1226 if (!(o
= pa_context_get_sink_info_by_name(i
->context
, NULL
, sink_info_cb
, i
))) {
1227 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to get sink info: %s", pa_strerror(pa_context_errno(i
->context
)));
1232 i
->operation_success
= 0;
1233 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1234 pa_threaded_mainloop_wait(i
->mainloop
);
1235 CONTEXT_CHECK_DEAD_GOTO(i
, fail
);
1238 pa_operation_unref(o
);
1241 if (!i
->operation_success
) {
1242 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to get sink info: %s", pa_strerror(pa_context_errno(i
->context
)));
1247 /* Get source info */
1249 if (!(o
= pa_context_get_source_info_by_name(i
->context
, NULL
, source_info_cb
, i
))) {
1250 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to get source info: %s", pa_strerror(pa_context_errno(i
->context
)));
1255 i
->operation_success
= 0;
1256 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1257 pa_threaded_mainloop_wait(i
->mainloop
);
1258 CONTEXT_CHECK_DEAD_GOTO(i
, fail
);
1261 pa_operation_unref(o
);
1264 if (!i
->operation_success
) {
1265 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to get source info: %s", pa_strerror(pa_context_errno(i
->context
)));
1270 pa_threaded_mainloop_unlock(i
->mainloop
);
1272 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": mixer_open() succeeded, fd=%i\n", i
->app_fd
);
1274 fd_info_add_to_list(i
);
1282 pa_operation_unref(o
);
1284 pa_threaded_mainloop_unlock(i
->mainloop
);
1291 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": mixer_open() failed\n");
1296 static int sndstat_open(int flags
, int *_errno
) {
1297 static const char sndstat
[] =
1298 "Sound Driver:3.8.1a-980706 (PulseAudio Virtual OSS)\n"
1300 "Config options: 0\n"
1302 "Installed drivers:\n"
1303 "Type 255: PulseAudio Virtual OSS\n"
1306 "PulseAudio Virtual OSS\n"
1309 "0: PulseAudio Virtual OSS\n"
1311 "Synth devices: NOT ENABLED IN CONFIG\n"
1318 "0: PulseAudio Virtual OSS\n";
1320 char fn
[] = "/tmp/padsp-sndstat-XXXXXX";
1325 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": sndstat_open()\n");
1327 if (flags
!= O_RDONLY
1329 && flags
!= (O_RDONLY
|O_LARGEFILE
)
1333 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": bad access!\n");
1344 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": mkstemp() failed: %s\n", strerror(errno
));
1350 if (write(fd
, sndstat
, sizeof(sndstat
) -1) != sizeof(sndstat
)-1) {
1352 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": write() failed: %s\n", strerror(errno
));
1356 if (lseek(fd
, SEEK_SET
, 0) < 0) {
1358 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": lseek() failed: %s\n", strerror(errno
));
1370 int open(const char *filename
, int flags
, ...) {
1375 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": open(%s)\n", filename
);
1377 va_start(args
, flags
);
1378 if (flags
& O_CREAT
) {
1379 if (sizeof(mode_t
) < sizeof(int))
1380 mode
= va_arg(args
, int);
1382 mode
= va_arg(args
, mode_t
);
1386 if (!function_enter()) {
1388 return _open(filename
, flags
, mode
);
1391 if (dsp_cloak_enable() && (strcmp(filename
, "/dev/dsp") == 0 || strcmp(filename
, "/dev/adsp") == 0)) {
1392 r
= dsp_open(flags
, &_errno
);
1393 } else if (mixer_cloak_enable() && strcmp(filename
, "/dev/mixer") == 0) {
1394 r
= mixer_open(flags
, &_errno
);
1395 } else if (sndstat_cloak_enable() && strcmp(filename
, "/dev/sndstat") == 0) {
1396 r
= sndstat_open(flags
, &_errno
);
1400 return _open(filename
, flags
, mode
);
1411 static int mixer_ioctl(fd_info
*i
, unsigned long request
, void*argp
, int *_errno
) {
1415 case SOUND_MIXER_READ_DEVMASK
:
1416 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_DEVMASK\n");
1418 *(int*) argp
= SOUND_MASK_PCM
| SOUND_MASK_IGAIN
;
1421 case SOUND_MIXER_READ_RECMASK
:
1422 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_RECMASK\n");
1424 *(int*) argp
= SOUND_MASK_IGAIN
;
1427 case SOUND_MIXER_READ_STEREODEVS
:
1428 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_STEREODEVS\n");
1430 pa_threaded_mainloop_lock(i
->mainloop
);
1432 if (i
->sink_volume
.channels
> 1)
1433 *(int*) argp
|= SOUND_MASK_PCM
;
1434 if (i
->source_volume
.channels
> 1)
1435 *(int*) argp
|= SOUND_MASK_IGAIN
;
1436 pa_threaded_mainloop_unlock(i
->mainloop
);
1440 case SOUND_MIXER_READ_RECSRC
:
1441 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_RECSRC\n");
1443 *(int*) argp
= SOUND_MASK_IGAIN
;
1446 case SOUND_MIXER_WRITE_RECSRC
:
1447 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_WRITE_RECSRC\n");
1450 case SOUND_MIXER_READ_CAPS
:
1451 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_CAPS\n");
1456 case SOUND_MIXER_READ_PCM
:
1457 case SOUND_MIXER_READ_IGAIN
: {
1460 if (request
== SOUND_MIXER_READ_PCM
)
1461 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_PCM\n");
1463 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_IGAIN\n");
1465 pa_threaded_mainloop_lock(i
->mainloop
);
1467 if (request
== SOUND_MIXER_READ_PCM
)
1468 v
= &i
->sink_volume
;
1470 v
= &i
->source_volume
;
1473 ((v
->values
[0]*100/PA_VOLUME_NORM
)) |
1474 ((v
->values
[v
->channels
> 1 ? 1 : 0]*100/PA_VOLUME_NORM
) << 8);
1476 pa_threaded_mainloop_unlock(i
->mainloop
);
1481 case SOUND_MIXER_WRITE_PCM
:
1482 case SOUND_MIXER_WRITE_IGAIN
: {
1485 if (request
== SOUND_MIXER_READ_PCM
)
1486 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_WRITE_PCM\n");
1488 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_WRITE_IGAIN\n");
1490 pa_threaded_mainloop_lock(i
->mainloop
);
1492 if (request
== SOUND_MIXER_READ_PCM
) {
1494 pv
= &i
->sink_volume
;
1496 v
= i
->source_volume
;
1497 pv
= &i
->source_volume
;
1500 pv
->values
[0] = ((*(int*) argp
& 0xFF)*PA_VOLUME_NORM
)/100;
1501 pv
->values
[1] = ((*(int*) argp
>> 8)*PA_VOLUME_NORM
)/100;
1503 if (!pa_cvolume_equal(pv
, &v
)) {
1506 if (request
== SOUND_MIXER_READ_PCM
)
1507 o
= pa_context_set_sink_volume_by_index(i
->context
, i
->sink_index
, pv
, context_success_cb
, i
);
1509 o
= pa_context_set_source_volume_by_index(i
->context
, i
->source_index
, pv
, context_success_cb
, i
);
1512 debug(DEBUG_LEVEL_NORMAL
, __FILE__
":Failed set volume: %s", pa_strerror(pa_context_errno(i
->context
)));
1515 i
->operation_success
= 0;
1516 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1517 CONTEXT_CHECK_DEAD_GOTO(i
, exit_loop
);
1519 pa_threaded_mainloop_wait(i
->mainloop
);
1523 if (!i
->operation_success
)
1524 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to set volume: %s\n", pa_strerror(pa_context_errno(i
->context
)));
1526 pa_operation_unref(o
);
1529 /* We don't wait for completion here */
1530 i
->volume_modify_count
++;
1533 pa_threaded_mainloop_unlock(i
->mainloop
);
1538 case SOUND_MIXER_INFO
: {
1539 mixer_info
*mi
= argp
;
1541 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_INFO\n");
1543 memset(mi
, 0, sizeof(mixer_info
));
1544 strncpy(mi
->id
, "PULSEAUDIO", sizeof(mi
->id
));
1545 strncpy(mi
->name
, "PulseAudio Virtual OSS", sizeof(mi
->name
));
1546 pa_threaded_mainloop_lock(i
->mainloop
);
1547 mi
->modify_counter
= i
->volume_modify_count
;
1548 pa_threaded_mainloop_unlock(i
->mainloop
);
1553 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": unknown ioctl 0x%08lx\n", request
);
1566 static int map_format(int *fmt
, pa_sample_spec
*ss
) {
1570 ss
->format
= PA_SAMPLE_ULAW
;
1574 ss
->format
= PA_SAMPLE_ALAW
;
1581 ss
->format
= PA_SAMPLE_U8
;
1588 ss
->format
= PA_SAMPLE_S16BE
;
1595 ss
->format
= PA_SAMPLE_S16LE
;
1599 ss
->format
= PA_SAMPLE_S16NE
;
1607 static int map_format_back(pa_sample_format_t format
) {
1609 case PA_SAMPLE_S16LE
: return AFMT_S16_LE
;
1610 case PA_SAMPLE_S16BE
: return AFMT_S16_BE
;
1611 case PA_SAMPLE_ULAW
: return AFMT_MU_LAW
;
1612 case PA_SAMPLE_ALAW
: return AFMT_A_LAW
;
1613 case PA_SAMPLE_U8
: return AFMT_U8
;
1619 static int dsp_flush_fd(int fd
) {
1623 if (ioctl(fd
, SIOCINQ
, &l
) < 0) {
1624 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SIOCINQ: %s\n", strerror(errno
));
1632 k
= (size_t) l
> sizeof(buf
) ? sizeof(buf
) : (size_t) l
;
1633 if (read(fd
, buf
, k
) < 0)
1634 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": read(): %s\n", strerror(errno
));
1640 # warning "Your platform does not support SIOCINQ, something might not work as intended."
1645 static int dsp_flush_socket(fd_info
*i
) {
1648 if ((i
->thread_fd
< 0) && (i
->app_fd
< 0))
1651 if (i
->thread_fd
>= 0)
1652 res
= dsp_flush_fd(i
->thread_fd
);
1658 res
= dsp_flush_fd(i
->app_fd
);
1666 static int dsp_empty_socket(fd_info
*i
) {
1670 /* Empty the socket */
1674 if (i
->thread_fd
< 0)
1677 if (ioctl(i
->thread_fd
, SIOCINQ
, &l
) < 0) {
1678 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SIOCINQ: %s\n", strerror(errno
));
1687 pa_threaded_mainloop_wait(i
->mainloop
);
1692 # warning "Your platform does not support SIOCINQ, something might not work as intended."
1697 static int dsp_drain(fd_info
*i
) {
1698 pa_operation
*o
= NULL
;
1704 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Draining.\n");
1706 pa_threaded_mainloop_lock(i
->mainloop
);
1708 if (dsp_empty_socket(i
) < 0)
1711 if (!i
->play_stream
)
1714 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Really draining.\n");
1716 if (!(o
= pa_stream_drain(i
->play_stream
, stream_success_cb
, i
))) {
1717 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_drain(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1721 i
->operation_success
= 0;
1722 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1723 PLAYBACK_STREAM_CHECK_DEAD_GOTO(i
, fail
);
1725 pa_threaded_mainloop_wait(i
->mainloop
);
1728 if (!i
->operation_success
) {
1729 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_drain() 2: %s\n", pa_strerror(pa_context_errno(i
->context
)));
1738 pa_operation_unref(o
);
1740 pa_threaded_mainloop_unlock(i
->mainloop
);
1745 static int dsp_trigger(fd_info
*i
) {
1746 pa_operation
*o
= NULL
;
1749 if (!i
->play_stream
)
1752 pa_threaded_mainloop_lock(i
->mainloop
);
1754 if (dsp_empty_socket(i
) < 0)
1757 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Triggering.\n");
1759 if (!(o
= pa_stream_trigger(i
->play_stream
, stream_success_cb
, i
))) {
1760 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1764 i
->operation_success
= 0;
1765 while (!pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1766 PLAYBACK_STREAM_CHECK_DEAD_GOTO(i
, fail
);
1768 pa_threaded_mainloop_wait(i
->mainloop
);
1771 if (!i
->operation_success
) {
1772 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1781 pa_operation_unref(o
);
1783 pa_threaded_mainloop_unlock(i
->mainloop
);
1788 static int dsp_ioctl(fd_info
*i
, unsigned long request
, void*argp
, int *_errno
) {
1792 case SNDCTL_DSP_SETFMT
: {
1793 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_SETFMT: %i\n", *(int*) argp
);
1795 pa_threaded_mainloop_lock(i
->mainloop
);
1797 if (*(int*) argp
== AFMT_QUERY
)
1798 *(int*) argp
= map_format_back(i
->sample_spec
.format
);
1800 map_format((int*) argp
, &i
->sample_spec
);
1804 pa_threaded_mainloop_unlock(i
->mainloop
);
1808 case SNDCTL_DSP_SPEED
: {
1813 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_SPEED: %i\n", *(int*) argp
);
1815 pa_threaded_mainloop_lock(i
->mainloop
);
1817 ss
= i
->sample_spec
;
1818 ss
.rate
= *(int*) argp
;
1820 if ((valid
= pa_sample_spec_valid(&ss
))) {
1821 i
->sample_spec
= ss
;
1825 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": ss: %s\n", pa_sample_spec_snprint(t
, sizeof(t
), &i
->sample_spec
));
1827 pa_threaded_mainloop_unlock(i
->mainloop
);
1837 case SNDCTL_DSP_STEREO
:
1838 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_STEREO: %i\n", *(int*) argp
);
1840 pa_threaded_mainloop_lock(i
->mainloop
);
1842 i
->sample_spec
.channels
= *(int*) argp
? 2 : 1;
1845 pa_threaded_mainloop_unlock(i
->mainloop
);
1848 case SNDCTL_DSP_CHANNELS
: {
1852 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_CHANNELS: %i\n", *(int*) argp
);
1854 pa_threaded_mainloop_lock(i
->mainloop
);
1856 ss
= i
->sample_spec
;
1857 ss
.channels
= *(int*) argp
;
1859 if ((valid
= pa_sample_spec_valid(&ss
))) {
1860 i
->sample_spec
= ss
;
1864 pa_threaded_mainloop_unlock(i
->mainloop
);
1874 case SNDCTL_DSP_GETBLKSIZE
:
1875 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_GETBLKSIZE\n");
1877 pa_threaded_mainloop_lock(i
->mainloop
);
1880 *(int*) argp
= i
->fragment_size
;
1882 pa_threaded_mainloop_unlock(i
->mainloop
);
1886 case SNDCTL_DSP_SETFRAGMENT
:
1887 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_SETFRAGMENT: 0x%08x\n", *(int*) argp
);
1889 pa_threaded_mainloop_lock(i
->mainloop
);
1891 i
->fragment_size
= 1 << ((*(int*) argp
) & 31);
1892 i
->n_fragments
= (*(int*) argp
) >> 16;
1894 /* 0x7FFF means that we can set whatever we like */
1895 if (i
->n_fragments
== 0x7FFF)
1896 i
->n_fragments
= 12;
1900 pa_threaded_mainloop_unlock(i
->mainloop
);
1904 case SNDCTL_DSP_GETCAPS
:
1905 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_CAPS\n");
1907 *(int*) argp
= DSP_CAP_DUPLEX
1908 #ifdef DSP_CAP_MULTI
1914 case SNDCTL_DSP_GETODELAY
: {
1917 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_GETODELAY\n");
1919 pa_threaded_mainloop_lock(i
->mainloop
);
1926 PLAYBACK_STREAM_CHECK_DEAD_GOTO(i
, exit_loop
);
1928 if (pa_stream_get_latency(i
->play_stream
, &usec
, NULL
) >= 0) {
1929 *(int*) argp
= pa_usec_to_bytes(usec
, &i
->sample_spec
);
1933 if (pa_context_errno(i
->context
) != PA_ERR_NODATA
) {
1934 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_get_latency(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1938 pa_threaded_mainloop_wait(i
->mainloop
);
1944 if (ioctl(i
->thread_fd
, SIOCINQ
, &l
) < 0)
1945 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SIOCINQ failed: %s\n", strerror(errno
));
1949 # warning "Your platform does not support SIOCINQ, something might not work as intended."
1952 pa_threaded_mainloop_unlock(i
->mainloop
);
1954 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": ODELAY: %i\n", *(int*) argp
);
1959 case SNDCTL_DSP_RESET
: {
1960 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_RESET\n");
1962 pa_threaded_mainloop_lock(i
->mainloop
);
1965 dsp_flush_socket(i
);
1967 i
->optr_n_blocks
= 0;
1969 pa_threaded_mainloop_unlock(i
->mainloop
);
1973 case SNDCTL_DSP_GETFMTS
: {
1974 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_GETFMTS\n");
1976 *(int*) argp
= AFMT_MU_LAW
|AFMT_A_LAW
|AFMT_U8
|AFMT_S16_LE
|AFMT_S16_BE
;
1980 case SNDCTL_DSP_POST
:
1981 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_POST\n");
1983 if (dsp_trigger(i
) < 0)
1987 case SNDCTL_DSP_SYNC
:
1988 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_SYNC\n");
1990 if (dsp_drain(i
) < 0)
1995 case SNDCTL_DSP_GETOSPACE
:
1996 case SNDCTL_DSP_GETISPACE
: {
1997 audio_buf_info
*bi
= (audio_buf_info
*) argp
;
2001 if (request
== SNDCTL_DSP_GETOSPACE
)
2002 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_GETOSPACE\n");
2004 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_GETISPACE\n");
2006 pa_threaded_mainloop_lock(i
->mainloop
);
2010 if (request
== SNDCTL_DSP_GETOSPACE
) {
2011 if (i
->play_stream
) {
2012 if ((k
= pa_stream_writable_size(i
->play_stream
)) == (size_t) -1)
2013 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_writable_size(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
2015 k
= i
->fragment_size
* i
->n_fragments
;
2018 if (ioctl(i
->thread_fd
, SIOCINQ
, &l
) < 0) {
2019 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SIOCINQ failed: %s\n", strerror(errno
));
2023 # warning "Your platform does not dsp_flush_fd, something might not work as intended."
2026 bi
->bytes
= k
> (size_t) l
? k
- l
: 0;
2028 if (i
->rec_stream
) {
2029 if ((k
= pa_stream_readable_size(i
->rec_stream
)) == (size_t) -1)
2030 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_readable_size(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
2035 if (ioctl(i
->app_fd
, SIOCINQ
, &l
) < 0) {
2036 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SIOCINQ failed: %s\n", strerror(errno
));
2040 # warning "Your platform does not dsp_flush_fd, something might not work as intended."
2045 bi
->fragsize
= i
->fragment_size
;
2046 bi
->fragstotal
= i
->n_fragments
;
2047 bi
->fragments
= bi
->bytes
/ bi
->fragsize
;
2049 pa_threaded_mainloop_unlock(i
->mainloop
);
2051 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": fragsize=%i, fragstotal=%i, bytes=%i, fragments=%i\n", bi
->fragsize
, bi
->fragstotal
, bi
->bytes
, bi
->fragments
);
2056 case SOUND_PCM_READ_RATE
:
2057 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_PCM_READ_RATE\n");
2059 pa_threaded_mainloop_lock(i
->mainloop
);
2060 *(int*) argp
= i
->sample_spec
.rate
;
2061 pa_threaded_mainloop_unlock(i
->mainloop
);
2064 case SOUND_PCM_READ_CHANNELS
:
2065 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_PCM_READ_CHANNELS\n");
2067 pa_threaded_mainloop_lock(i
->mainloop
);
2068 *(int*) argp
= i
->sample_spec
.channels
;
2069 pa_threaded_mainloop_unlock(i
->mainloop
);
2072 case SOUND_PCM_READ_BITS
:
2073 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_PCM_READ_BITS\n");
2075 pa_threaded_mainloop_lock(i
->mainloop
);
2076 *(int*) argp
= pa_sample_size(&i
->sample_spec
)*8;
2077 pa_threaded_mainloop_unlock(i
->mainloop
);
2080 case SNDCTL_DSP_GETOPTR
: {
2083 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_GETOPTR\n");
2085 info
= (count_info
*) argp
;
2086 memset(info
, 0, sizeof(*info
));
2088 pa_threaded_mainloop_lock(i
->mainloop
);
2093 PLAYBACK_STREAM_CHECK_DEAD_GOTO(i
, exit_loop
);
2095 if (pa_stream_get_time(i
->play_stream
, &usec
) >= 0) {
2096 size_t k
= pa_usec_to_bytes(usec
, &i
->sample_spec
);
2099 info
->bytes
= (int) k
;
2100 m
= k
/ i
->fragment_size
;
2101 info
->blocks
= m
- i
->optr_n_blocks
;
2102 i
->optr_n_blocks
= m
;
2107 if (pa_context_errno(i
->context
) != PA_ERR_NODATA
) {
2108 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_get_latency(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
2112 pa_threaded_mainloop_wait(i
->mainloop
);
2115 pa_threaded_mainloop_unlock(i
->mainloop
);
2117 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": GETOPTR bytes=%i, blocks=%i, ptr=%i\n", info
->bytes
, info
->blocks
, info
->ptr
);
2122 case SNDCTL_DSP_GETIPTR
:
2123 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": invalid ioctl SNDCTL_DSP_GETIPTR\n");
2127 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": unknown ioctl 0x%08lx\n", request
);
2141 int ioctl(int fd
, unsigned long request
, ...) {
2147 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": ioctl()\n");
2149 va_start(args
, request
);
2150 argp
= va_arg(args
, void *);
2153 if (!function_enter()) {
2155 return _ioctl(fd
, request
, argp
);
2158 if (!(i
= fd_info_find(fd
))) {
2161 return _ioctl(fd
, request
, argp
);
2164 if (i
->type
== FD_INFO_MIXER
)
2165 r
= mixer_ioctl(i
, request
, argp
, &_errno
);
2167 r
= dsp_ioctl(i
, request
, argp
, &_errno
);
2182 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": close()\n");
2184 if (!function_enter()) {
2189 if (!(i
= fd_info_find(fd
))) {
2195 fd_info_remove_from_list(i
);
2203 int access(const char *pathname
, int mode
) {
2206 /* Firefox needs this. See #27 */
2211 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": access(%s)\n", pathname
);
2213 if (strcmp(pathname
, "/dev/dsp") != 0 &&
2214 strcmp(pathname
, "/dev/adsp") != 0 &&
2215 strcmp(pathname
, "/dev/sndstat") != 0 &&
2216 strcmp(pathname
, "/dev/mixer") != 0) {
2218 return _access(pathname
, mode
);
2221 if (mode
& (W_OK
| X_OK
)) {
2222 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": access(%s, %x) = EACCESS\n", pathname
, mode
);
2227 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": access(%s, %x) = OK\n", pathname
, mode
);
2234 int open64(const char *filename
, int flags
, ...) {
2238 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": open64(%s)\n", filename
);
2240 va_start(args
, flags
);
2241 if (flags
& O_CREAT
)
2242 mode
= va_arg(args
, mode_t
);
2245 if (strcmp(filename
, "/dev/dsp") != 0 &&
2246 strcmp(filename
, "/dev/adsp") != 0 &&
2247 strcmp(filename
, "/dev/sndstat") != 0 &&
2248 strcmp(filename
, "/dev/mixer") != 0) {
2250 return _open64(filename
, flags
, mode
);
2253 return open(filename
, flags
, mode
);
2258 FILE* fopen(const char *filename
, const char *mode
) {
2263 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": fopen(%s)\n", filename
);
2265 if (strcmp(filename
, "/dev/dsp") != 0 &&
2266 strcmp(filename
, "/dev/adsp") != 0 &&
2267 strcmp(filename
, "/dev/sndstat") != 0 &&
2268 strcmp(filename
, "/dev/mixer") != 0) {
2270 return _fopen(filename
, mode
);
2286 if ((((mode
[1] == 'b') || (mode
[1] == 't')) && (mode
[2] == '+')) || (mode
[1] == '+'))
2289 if ((fd
= open(filename
, m
)) < 0)
2292 if (!(f
= fdopen(fd
, mode
))) {
2302 FILE *fopen64(const char *filename
, const char *mode
) {
2304 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": fopen64(%s)\n", filename
);
2306 if (strcmp(filename
, "/dev/dsp") != 0 &&
2307 strcmp(filename
, "/dev/adsp") != 0 &&
2308 strcmp(filename
, "/dev/sndstat") != 0 &&
2309 strcmp(filename
, "/dev/mixer") != 0) {
2310 LOAD_FOPEN64_FUNC();
2311 return _fopen64(filename
, mode
);
2314 return fopen(filename
, mode
);
2319 int fclose(FILE *f
) {
2322 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": fclose()\n");
2324 if (!function_enter()) {
2329 if (!(i
= fd_info_find(fileno(f
)))) {
2335 fd_info_remove_from_list(i
);
2337 /* Dirty trick to avoid that the fd is not freed twice, once by us
2338 * and once by the real fclose() */