4 This file is part of PulseAudio.
6 Copyright 2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #ifdef _FILE_OFFSET_BITS
30 #undef _FILE_OFFSET_BITS
33 #ifndef _LARGEFILE64_SOURCE
34 #define _LARGEFILE64_SOURCE 1
37 #include <sys/soundcard.h>
38 #include <sys/ioctl.h>
41 #include <sys/socket.h>
52 #include <linux/sockios.h>
55 #include <pulse/pulseaudio.h>
56 #include <pulsecore/llist.h>
57 #include <pulsecore/gccmacro.h>
59 /* On some systems SIOCINQ isn't defined, but FIONREAD is just an alias */
60 #if !defined(SIOCINQ) && defined(FIONREAD)
61 # define SIOCINQ FIONREAD
69 typedef struct fd_info fd_info
;
72 pthread_mutex_t mutex
;
77 int app_fd
, thread_fd
;
79 pa_sample_spec sample_spec
;
83 pa_threaded_mainloop
*mainloop
;
85 pa_stream
*play_stream
;
86 pa_stream
*rec_stream
;
88 pa_io_event
*io_event
;
89 pa_io_event_flags_t io_flags
;
94 int operation_success
;
96 pa_cvolume sink_volume
, source_volume
;
97 uint32_t sink_index
, source_index
;
98 int volume_modify_count
;
102 PA_LLIST_FIELDS(fd_info
);
105 static int dsp_drain(fd_info
*i
);
106 static void fd_info_remove_from_list(fd_info
*i
);
108 static pthread_mutex_t fd_infos_mutex
= PTHREAD_MUTEX_INITIALIZER
;
109 static pthread_mutex_t func_mutex
= PTHREAD_MUTEX_INITIALIZER
;
111 static PA_LLIST_HEAD(fd_info
, fd_infos
) = NULL
;
113 static int (*_ioctl
)(int, int, void*) = NULL
;
114 static int (*_close
)(int) = NULL
;
115 static int (*_open
)(const char *, int, mode_t
) = NULL
;
116 static FILE* (*_fopen
)(const char *path
, const char *mode
) = NULL
;
118 static int (*_open64
)(const char *, int, mode_t
) = NULL
;
119 static FILE* (*_fopen64
)(const char *path
, const char *mode
) = NULL
;
121 static int (*_fclose
)(FILE *f
) = NULL
;
122 static int (*_access
)(const char *, int) = NULL
;
124 /* dlsym() violates ISO C, so confide the breakage into this function to
126 typedef void (*fnptr
)(void);
127 static inline fnptr
dlsym_fn(void *handle
, const char *symbol
) {
128 return (fnptr
) (long) dlsym(handle
, symbol
);
131 #define LOAD_IOCTL_FUNC() \
133 pthread_mutex_lock(&func_mutex); \
135 _ioctl = (int (*)(int, int, void*)) dlsym_fn(RTLD_NEXT, "ioctl"); \
136 pthread_mutex_unlock(&func_mutex); \
139 #define LOAD_OPEN_FUNC() \
141 pthread_mutex_lock(&func_mutex); \
143 _open = (int (*)(const char *, int, mode_t)) dlsym_fn(RTLD_NEXT, "open"); \
144 pthread_mutex_unlock(&func_mutex); \
147 #define LOAD_OPEN64_FUNC() \
149 pthread_mutex_lock(&func_mutex); \
151 _open64 = (int (*)(const char *, int, mode_t)) dlsym_fn(RTLD_NEXT, "open64"); \
152 pthread_mutex_unlock(&func_mutex); \
155 #define LOAD_CLOSE_FUNC() \
157 pthread_mutex_lock(&func_mutex); \
159 _close = (int (*)(int)) dlsym_fn(RTLD_NEXT, "close"); \
160 pthread_mutex_unlock(&func_mutex); \
163 #define LOAD_ACCESS_FUNC() \
165 pthread_mutex_lock(&func_mutex); \
167 _access = (int (*)(const char*, int)) dlsym_fn(RTLD_NEXT, "access"); \
168 pthread_mutex_unlock(&func_mutex); \
171 #define LOAD_FOPEN_FUNC() \
173 pthread_mutex_lock(&func_mutex); \
175 _fopen = (FILE* (*)(const char *, const char*)) dlsym_fn(RTLD_NEXT, "fopen"); \
176 pthread_mutex_unlock(&func_mutex); \
179 #define LOAD_FOPEN64_FUNC() \
181 pthread_mutex_lock(&func_mutex); \
183 _fopen64 = (FILE* (*)(const char *, const char*)) dlsym_fn(RTLD_NEXT, "fopen64"); \
184 pthread_mutex_unlock(&func_mutex); \
187 #define LOAD_FCLOSE_FUNC() \
189 pthread_mutex_lock(&func_mutex); \
191 _fclose = (int (*)(FILE *)) dlsym_fn(RTLD_NEXT, "fclose"); \
192 pthread_mutex_unlock(&func_mutex); \
195 #define CONTEXT_CHECK_DEAD_GOTO(i, label) do { \
196 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY) { \
197 debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s\n", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
202 #define PLAYBACK_STREAM_CHECK_DEAD_GOTO(i, label) do { \
203 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY || \
204 !(i)->play_stream || pa_stream_get_state((i)->play_stream) != PA_STREAM_READY) { \
205 debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s\n", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
210 #define RECORD_STREAM_CHECK_DEAD_GOTO(i, label) do { \
211 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY || \
212 !(i)->rec_stream || pa_stream_get_state((i)->rec_stream) != PA_STREAM_READY) { \
213 debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s\n", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
218 static void debug(int level
, const char *format
, ...) PA_GCC_PRINTF_ATTR(2,3);
220 #define DEBUG_LEVEL_ALWAYS 0
221 #define DEBUG_LEVEL_NORMAL 1
222 #define DEBUG_LEVEL_VERBOSE 2
224 static void debug(int level
, const char *format
, ...) {
226 const char *dlevel_s
;
229 dlevel_s
= getenv("PADSP_DEBUG");
233 dlevel
= atoi(dlevel_s
);
238 va_start(ap
, format
);
239 vfprintf(stderr
, format
, ap
);
243 static int padsp_disabled(void) {
245 static int sym_resolved
= 0;
247 /* If the current process has a symbol __padsp_disabled__ we use
248 * it to detect whether we should enable our stuff or not. A
249 * program needs to be compiled with -rdynamic for this to work!
250 * The symbol must be an int containing a three bit bitmask: bit 1
251 * -> disable /dev/dsp emulation, bit 2 -> disable /dev/sndstat
252 * emulation, bit 3 -> disable /dev/mixer emulation. Hence a value
253 * of 7 disables padsp entirely. */
255 pthread_mutex_lock(&func_mutex
);
257 sym
= (int*) dlsym(RTLD_DEFAULT
, "__padsp_disabled__");
261 pthread_mutex_unlock(&func_mutex
);
269 static int dsp_cloak_enable(void) {
270 if (padsp_disabled() & 1)
273 if (getenv("PADSP_NO_DSP"))
279 static int sndstat_cloak_enable(void) {
280 if (padsp_disabled() & 2)
283 if (getenv("PADSP_NO_SNDSTAT"))
289 static int mixer_cloak_enable(void) {
290 if (padsp_disabled() & 4)
293 if (getenv("PADSP_NO_MIXER"))
298 static pthread_key_t recursion_key
;
300 static void recursion_key_alloc(void) {
301 pthread_key_create(&recursion_key
, NULL
);
304 static int function_enter(void) {
305 /* Avoid recursive calls */
306 static pthread_once_t recursion_key_once
= PTHREAD_ONCE_INIT
;
307 pthread_once(&recursion_key_once
, recursion_key_alloc
);
309 if (pthread_getspecific(recursion_key
))
312 pthread_setspecific(recursion_key
, (void*) 1);
316 static void function_exit(void) {
317 pthread_setspecific(recursion_key
, NULL
);
320 static void fd_info_free(fd_info
*i
) {
323 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": freeing fd info (fd=%i)\n", i
->app_fd
);
328 pa_threaded_mainloop_stop(i
->mainloop
);
330 if (i
->play_stream
) {
331 pa_stream_disconnect(i
->play_stream
);
332 pa_stream_unref(i
->play_stream
);
336 pa_stream_disconnect(i
->rec_stream
);
337 pa_stream_unref(i
->rec_stream
);
341 pa_context_disconnect(i
->context
);
342 pa_context_unref(i
->context
);
346 pa_threaded_mainloop_free(i
->mainloop
);
348 if (i
->app_fd
>= 0) {
353 if (i
->thread_fd
>= 0) {
355 _close(i
->thread_fd
);
360 pthread_mutex_destroy(&i
->mutex
);
364 static fd_info
*fd_info_ref(fd_info
*i
) {
367 pthread_mutex_lock(&i
->mutex
);
371 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": ref++, now %i\n", i
->ref
);
372 pthread_mutex_unlock(&i
->mutex
);
377 static void fd_info_unref(fd_info
*i
) {
379 pthread_mutex_lock(&i
->mutex
);
382 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": ref--, now %i\n", i
->ref
);
383 pthread_mutex_unlock(&i
->mutex
);
389 static void context_state_cb(pa_context
*c
, void *userdata
) {
390 fd_info
*i
= userdata
;
393 switch (pa_context_get_state(c
)) {
394 case PA_CONTEXT_READY
:
395 case PA_CONTEXT_TERMINATED
:
396 case PA_CONTEXT_FAILED
:
397 pa_threaded_mainloop_signal(i
->mainloop
, 0);
400 case PA_CONTEXT_UNCONNECTED
:
401 case PA_CONTEXT_CONNECTING
:
402 case PA_CONTEXT_AUTHORIZING
:
403 case PA_CONTEXT_SETTING_NAME
:
408 static void reset_params(fd_info
*i
) {
411 i
->sample_spec
.format
= PA_SAMPLE_U8
;
412 i
->sample_spec
.channels
= 1;
413 i
->sample_spec
.rate
= 8000;
414 i
->fragment_size
= 0;
418 static const char *client_name(char *buf
, size_t n
) {
422 if ((e
= getenv("PADSP_CLIENT_NAME")))
425 if (pa_get_binary_name(p
, sizeof(p
)))
426 snprintf(buf
, n
, "OSS Emulation[%s]", p
);
428 snprintf(buf
, n
, "OSS");
433 static const char *stream_name(void) {
436 if ((e
= getenv("PADSP_STREAM_NAME")))
439 return "Audio Stream";
442 static void atfork_prepare(void) {
445 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": atfork_prepare() enter\n");
449 pthread_mutex_lock(&fd_infos_mutex
);
451 for (i
= fd_infos
; i
; i
= i
->next
) {
452 pthread_mutex_lock(&i
->mutex
);
453 pa_threaded_mainloop_lock(i
->mainloop
);
456 pthread_mutex_lock(&func_mutex
);
459 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": atfork_prepare() exit\n");
462 static void atfork_parent(void) {
465 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": atfork_parent() enter\n");
467 pthread_mutex_unlock(&func_mutex
);
469 for (i
= fd_infos
; i
; i
= i
->next
) {
470 pa_threaded_mainloop_unlock(i
->mainloop
);
471 pthread_mutex_unlock(&i
->mutex
);
474 pthread_mutex_unlock(&fd_infos_mutex
);
478 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": atfork_parent() exit\n");
481 static void atfork_child(void) {
484 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": atfork_child() enter\n");
486 /* We do only the bare minimum to get all fds closed */
487 pthread_mutex_init(&func_mutex
, NULL
);
488 pthread_mutex_init(&fd_infos_mutex
, NULL
);
490 for (i
= fd_infos
; i
; i
= i
->next
) {
491 pthread_mutex_init(&i
->mutex
, NULL
);
494 pa_context_disconnect(i
->context
);
495 pa_context_unref(i
->context
);
499 if (i
->play_stream
) {
500 pa_stream_unref(i
->play_stream
);
501 i
->play_stream
= NULL
;
505 pa_stream_unref(i
->rec_stream
);
506 i
->rec_stream
= NULL
;
509 if (i
->app_fd
>= 0) {
514 if (i
->thread_fd
>= 0) {
524 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": atfork_child() exit\n");
527 static void install_atfork(void) {
528 pthread_atfork(atfork_prepare
, atfork_parent
, atfork_child
);
531 static void stream_success_cb(pa_stream
*s
, int success
, void *userdata
) {
532 fd_info
*i
= userdata
;
537 i
->operation_success
= success
;
538 pa_threaded_mainloop_signal(i
->mainloop
, 0);
541 static void context_success_cb(pa_context
*c
, int success
, void *userdata
) {
542 fd_info
*i
= userdata
;
547 i
->operation_success
= success
;
548 pa_threaded_mainloop_signal(i
->mainloop
, 0);
551 static fd_info
* fd_info_new(fd_info_type_t type
, int *_errno
) {
553 int sfds
[2] = { -1, -1 };
555 static pthread_once_t install_atfork_once
= PTHREAD_ONCE_INIT
;
557 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": fd_info_new()\n");
559 signal(SIGPIPE
, SIG_IGN
); /* Yes, ugly as hell */
561 pthread_once(&install_atfork_once
, install_atfork
);
563 if (!(i
= malloc(sizeof(fd_info
)))) {
568 i
->app_fd
= i
->thread_fd
= -1;
573 i
->play_stream
= NULL
;
574 i
->rec_stream
= NULL
;
577 pthread_mutex_init(&i
->mutex
, NULL
);
582 pa_cvolume_reset(&i
->sink_volume
, 2);
583 pa_cvolume_reset(&i
->source_volume
, 2);
584 i
->volume_modify_count
= 0;
585 i
->sink_index
= (uint32_t) -1;
586 i
->source_index
= (uint32_t) -1;
587 i
->optr_n_blocks
= 0;
588 PA_LLIST_INIT(fd_info
, i
);
592 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, sfds
) < 0) {
594 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": socket() failed: %s\n", strerror(errno
));
599 i
->thread_fd
= sfds
[1];
601 if (!(i
->mainloop
= pa_threaded_mainloop_new())) {
603 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_threaded_mainloop_new() failed\n");
607 if (!(i
->context
= pa_context_new(pa_threaded_mainloop_get_api(i
->mainloop
), client_name(name
, sizeof(name
))))) {
609 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_context_new() failed\n");
613 pa_context_set_state_callback(i
->context
, context_state_cb
, i
);
615 if (pa_context_connect(i
->context
, NULL
, 0, NULL
) < 0) {
616 *_errno
= ECONNREFUSED
;
617 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
621 pa_threaded_mainloop_lock(i
->mainloop
);
623 if (pa_threaded_mainloop_start(i
->mainloop
) < 0) {
625 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_threaded_mainloop_start() failed\n");
626 goto unlock_and_fail
;
629 /* Wait until the context is ready */
630 pa_threaded_mainloop_wait(i
->mainloop
);
632 if (pa_context_get_state(i
->context
) != PA_CONTEXT_READY
) {
633 *_errno
= ECONNREFUSED
;
634 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
635 goto unlock_and_fail
;
638 pa_threaded_mainloop_unlock(i
->mainloop
);
643 pa_threaded_mainloop_unlock(i
->mainloop
);
653 static void fd_info_add_to_list(fd_info
*i
) {
656 pthread_mutex_lock(&fd_infos_mutex
);
657 PA_LLIST_PREPEND(fd_info
, fd_infos
, i
);
658 pthread_mutex_unlock(&fd_infos_mutex
);
663 static void fd_info_remove_from_list(fd_info
*i
) {
666 pthread_mutex_lock(&fd_infos_mutex
);
667 PA_LLIST_REMOVE(fd_info
, fd_infos
, i
);
668 pthread_mutex_unlock(&fd_infos_mutex
);
673 static fd_info
* fd_info_find(int fd
) {
676 pthread_mutex_lock(&fd_infos_mutex
);
678 for (i
= fd_infos
; i
; i
= i
->next
)
679 if (i
->app_fd
== fd
&& !i
->unusable
) {
684 pthread_mutex_unlock(&fd_infos_mutex
);
689 static void fix_metrics(fd_info
*i
) {
691 char t
[PA_SAMPLE_SPEC_SNPRINT_MAX
];
693 fs
= pa_frame_size(&i
->sample_spec
);
695 /* Don't fix things more than necessary */
696 if ((i
->fragment_size
% fs
) == 0 &&
697 i
->n_fragments
>= 2 &&
698 i
->fragment_size
> 0)
701 i
->fragment_size
= (i
->fragment_size
/fs
)*fs
;
703 /* Number of fragments set? */
704 if (i
->n_fragments
< 2) {
705 if (i
->fragment_size
> 0) {
706 i
->n_fragments
= pa_bytes_per_second(&i
->sample_spec
) / 2 / i
->fragment_size
;
707 if (i
->n_fragments
< 2)
713 /* Fragment size set? */
714 if (i
->fragment_size
<= 0) {
715 i
->fragment_size
= pa_bytes_per_second(&i
->sample_spec
) / 2 / i
->n_fragments
;
716 if (i
->fragment_size
< 1024)
717 i
->fragment_size
= 1024;
720 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": sample spec: %s\n", pa_sample_spec_snprint(t
, sizeof(t
), &i
->sample_spec
));
721 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": fixated metrics to %i fragments, %li bytes each.\n", i
->n_fragments
, (long)i
->fragment_size
);
724 static void stream_request_cb(pa_stream
*s
, size_t length
, void *userdata
) {
725 fd_info
*i
= userdata
;
729 pa_mainloop_api
*api
;
732 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
734 if (s
== i
->play_stream
) {
735 n
= pa_stream_writable_size(i
->play_stream
);
736 if (n
== (size_t)-1) {
737 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_writable_size(): %s\n",
738 pa_strerror(pa_context_errno(i
->context
)));
741 if (n
>= i
->fragment_size
)
742 i
->io_flags
|= PA_IO_EVENT_INPUT
;
744 i
->io_flags
&= ~PA_IO_EVENT_INPUT
;
747 if (s
== i
->rec_stream
) {
748 n
= pa_stream_readable_size(i
->rec_stream
);
749 if (n
== (size_t)-1) {
750 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_readable_size(): %s\n",
751 pa_strerror(pa_context_errno(i
->context
)));
754 if (n
>= i
->fragment_size
)
755 i
->io_flags
|= PA_IO_EVENT_OUTPUT
;
757 i
->io_flags
&= ~PA_IO_EVENT_OUTPUT
;
760 api
->io_enable(i
->io_event
, i
->io_flags
);
764 static void stream_latency_update_cb(pa_stream
*s
, void *userdata
) {
765 fd_info
*i
= userdata
;
768 pa_threaded_mainloop_signal(i
->mainloop
, 0);
771 static void fd_info_shutdown(fd_info
*i
) {
775 pa_mainloop_api
*api
;
776 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
777 api
->io_free(i
->io_event
);
782 if (i
->thread_fd
>= 0) {
788 static int fd_info_copy_data(fd_info
*i
, int force
) {
791 if (!i
->play_stream
&& !i
->rec_stream
)
794 if ((i
->play_stream
) && (pa_stream_get_state(i
->play_stream
) == PA_STREAM_READY
)) {
795 n
= pa_stream_writable_size(i
->play_stream
);
797 if (n
== (size_t)-1) {
798 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_writable_size(): %s\n",
799 pa_strerror(pa_context_errno(i
->context
)));
803 while (n
>= i
->fragment_size
|| force
) {
807 if (!(i
->buf
= malloc(i
->fragment_size
))) {
808 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": malloc() failed.\n");
813 if ((r
= read(i
->thread_fd
, i
->buf
, i
->fragment_size
)) <= 0) {
818 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": read(): %s\n", r
== 0 ? "EOF" : strerror(errno
));
822 if (pa_stream_write(i
->play_stream
, i
->buf
, r
, free
, 0, PA_SEEK_RELATIVE
) < 0) {
823 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_write(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
829 assert(n
>= (size_t) r
);
833 if (n
>= i
->fragment_size
)
834 i
->io_flags
|= PA_IO_EVENT_INPUT
;
836 i
->io_flags
&= ~PA_IO_EVENT_INPUT
;
839 if ((i
->rec_stream
) && (pa_stream_get_state(i
->rec_stream
) == PA_STREAM_READY
)) {
840 n
= pa_stream_readable_size(i
->rec_stream
);
842 if (n
== (size_t)-1) {
843 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_readable_size(): %s\n",
844 pa_strerror(pa_context_errno(i
->context
)));
848 while (n
>= i
->fragment_size
|| force
) {
854 if (pa_stream_peek(i
->rec_stream
, &data
, &len
) < 0) {
855 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_peek(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
862 buf
= (const char*)data
+ i
->rec_offset
;
864 if ((r
= write(i
->thread_fd
, buf
, len
- i
->rec_offset
)) <= 0) {
869 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": write(): %s\n", strerror(errno
));
873 assert((size_t)r
<= len
- i
->rec_offset
);
876 if (i
->rec_offset
== len
) {
877 if (pa_stream_drop(i
->rec_stream
) < 0) {
878 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_drop(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
884 assert(n
>= (size_t) r
);
888 if (n
>= i
->fragment_size
)
889 i
->io_flags
|= PA_IO_EVENT_OUTPUT
;
891 i
->io_flags
&= ~PA_IO_EVENT_OUTPUT
;
895 pa_mainloop_api
*api
;
897 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
898 api
->io_enable(i
->io_event
, i
->io_flags
);
904 static void stream_state_cb(pa_stream
*s
, void * userdata
) {
905 fd_info
*i
= userdata
;
908 switch (pa_stream_get_state(s
)) {
910 case PA_STREAM_READY
:
911 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": stream established.\n");
914 case PA_STREAM_FAILED
:
915 if (s
== i
->play_stream
) {
916 debug(DEBUG_LEVEL_NORMAL
,
917 __FILE__
": pa_stream_connect_playback() failed: %s\n",
918 pa_strerror(pa_context_errno(i
->context
)));
919 pa_stream_unref(i
->play_stream
);
920 i
->play_stream
= NULL
;
921 } else if (s
== i
->rec_stream
) {
922 debug(DEBUG_LEVEL_NORMAL
,
923 __FILE__
": pa_stream_connect_record() failed: %s\n",
924 pa_strerror(pa_context_errno(i
->context
)));
925 pa_stream_unref(i
->rec_stream
);
926 i
->rec_stream
= NULL
;
931 case PA_STREAM_TERMINATED
:
932 case PA_STREAM_UNCONNECTED
:
933 case PA_STREAM_CREATING
:
938 static int create_playback_stream(fd_info
*i
) {
946 if (!(i
->play_stream
= pa_stream_new(i
->context
, stream_name(), &i
->sample_spec
, NULL
))) {
947 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
951 pa_stream_set_state_callback(i
->play_stream
, stream_state_cb
, i
);
952 pa_stream_set_write_callback(i
->play_stream
, stream_request_cb
, i
);
953 pa_stream_set_latency_update_callback(i
->play_stream
, stream_latency_update_cb
, i
);
955 memset(&attr
, 0, sizeof(attr
));
956 attr
.maxlength
= i
->fragment_size
* (i
->n_fragments
+1);
957 attr
.tlength
= i
->fragment_size
* i
->n_fragments
;
958 attr
.prebuf
= i
->fragment_size
;
959 attr
.minreq
= i
->fragment_size
;
961 if (pa_stream_connect_playback(i
->play_stream
, NULL
, &attr
, PA_STREAM_INTERPOLATE_TIMING
|PA_STREAM_AUTO_TIMING_UPDATE
, NULL
, NULL
) < 0) {
962 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
966 n
= i
->fragment_size
;
967 setsockopt(i
->app_fd
, SOL_SOCKET
, SO_SNDBUF
, &n
, sizeof(n
));
968 n
= i
->fragment_size
;
969 setsockopt(i
->thread_fd
, SOL_SOCKET
, SO_RCVBUF
, &n
, sizeof(n
));
977 static int create_record_stream(fd_info
*i
) {
985 if (!(i
->rec_stream
= pa_stream_new(i
->context
, stream_name(), &i
->sample_spec
, NULL
))) {
986 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
990 pa_stream_set_state_callback(i
->rec_stream
, stream_state_cb
, i
);
991 pa_stream_set_read_callback(i
->rec_stream
, stream_request_cb
, i
);
992 pa_stream_set_latency_update_callback(i
->rec_stream
, stream_latency_update_cb
, i
);
994 memset(&attr
, 0, sizeof(attr
));
995 attr
.maxlength
= i
->fragment_size
* (i
->n_fragments
+1);
996 attr
.fragsize
= i
->fragment_size
;
998 if (pa_stream_connect_record(i
->rec_stream
, NULL
, &attr
, PA_STREAM_INTERPOLATE_TIMING
|PA_STREAM_AUTO_TIMING_UPDATE
) < 0) {
999 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_connect_record() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
1003 n
= i
->fragment_size
;
1004 setsockopt(i
->app_fd
, SOL_SOCKET
, SO_RCVBUF
, &n
, sizeof(n
));
1005 n
= i
->fragment_size
;
1006 setsockopt(i
->thread_fd
, SOL_SOCKET
, SO_SNDBUF
, &n
, sizeof(n
));
1014 static void free_streams(fd_info
*i
) {
1017 if (i
->play_stream
) {
1018 pa_stream_disconnect(i
->play_stream
);
1019 pa_stream_unref(i
->play_stream
);
1020 i
->play_stream
= NULL
;
1021 i
->io_flags
|= PA_IO_EVENT_INPUT
;
1024 if (i
->rec_stream
) {
1025 pa_stream_disconnect(i
->rec_stream
);
1026 pa_stream_unref(i
->rec_stream
);
1027 i
->rec_stream
= NULL
;
1028 i
->io_flags
|= PA_IO_EVENT_OUTPUT
;
1032 pa_mainloop_api
*api
;
1034 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
1035 api
->io_enable(i
->io_event
, i
->io_flags
);
1039 static void io_event_cb(pa_mainloop_api
*api
, pa_io_event
*e
, int fd
, pa_io_event_flags_t flags
, void *userdata
) {
1040 fd_info
*i
= userdata
;
1042 pa_threaded_mainloop_signal(i
->mainloop
, 0);
1044 if (flags
& PA_IO_EVENT_INPUT
) {
1046 if (!i
->play_stream
) {
1047 if (create_playback_stream(i
) < 0)
1050 if (fd_info_copy_data(i
, 0) < 0)
1054 } else if (flags
& PA_IO_EVENT_OUTPUT
) {
1056 if (!i
->rec_stream
) {
1057 if (create_record_stream(i
) < 0)
1060 if (fd_info_copy_data(i
, 0) < 0)
1064 } else if (flags
& (PA_IO_EVENT_HANGUP
|PA_IO_EVENT_ERROR
))
1070 /* We can't do anything better than removing the event source */
1071 fd_info_shutdown(i
);
1074 static int dsp_open(int flags
, int *_errno
) {
1076 pa_mainloop_api
*api
;
1080 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": dsp_open()\n");
1082 if (!(i
= fd_info_new(FD_INFO_STREAM
, _errno
)))
1085 if ((flags
& O_NONBLOCK
) == O_NONBLOCK
) {
1086 if ((f
= fcntl(i
->app_fd
, F_GETFL
)) >= 0)
1087 fcntl(i
->app_fd
, F_SETFL
, f
|O_NONBLOCK
);
1089 if ((f
= fcntl(i
->thread_fd
, F_GETFL
)) >= 0)
1090 fcntl(i
->thread_fd
, F_SETFL
, f
|O_NONBLOCK
);
1092 fcntl(i
->app_fd
, F_SETFD
, FD_CLOEXEC
);
1093 fcntl(i
->thread_fd
, F_SETFD
, FD_CLOEXEC
);
1095 pa_threaded_mainloop_lock(i
->mainloop
);
1096 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
1098 switch (flags
& O_ACCMODE
) {
1100 i
->io_flags
= PA_IO_EVENT_OUTPUT
;
1101 shutdown(i
->thread_fd
, SHUT_RD
);
1102 shutdown(i
->app_fd
, SHUT_WR
);
1105 i
->io_flags
= PA_IO_EVENT_INPUT
;
1106 shutdown(i
->thread_fd
, SHUT_WR
);
1107 shutdown(i
->app_fd
, SHUT_RD
);
1110 i
->io_flags
= PA_IO_EVENT_INPUT
| PA_IO_EVENT_OUTPUT
;
1116 if (!(i
->io_event
= api
->io_new(api
, i
->thread_fd
, i
->io_flags
, io_event_cb
, i
)))
1119 pa_threaded_mainloop_unlock(i
->mainloop
);
1121 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": dsp_open() succeeded, fd=%i\n", i
->app_fd
);
1123 fd_info_add_to_list(i
);
1130 pa_threaded_mainloop_unlock(i
->mainloop
);
1137 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": dsp_open() failed\n");
1142 static void sink_info_cb(pa_context
*context
, const pa_sink_info
*si
, int eol
, void *userdata
) {
1143 fd_info
*i
= userdata
;
1145 if (!si
&& eol
< 0) {
1146 i
->operation_success
= 0;
1147 pa_threaded_mainloop_signal(i
->mainloop
, 0);
1154 if (!pa_cvolume_equal(&i
->sink_volume
, &si
->volume
))
1155 i
->volume_modify_count
++;
1157 i
->sink_volume
= si
->volume
;
1158 i
->sink_index
= si
->index
;
1160 i
->operation_success
= 1;
1161 pa_threaded_mainloop_signal(i
->mainloop
, 0);
1164 static void source_info_cb(pa_context
*context
, const pa_source_info
*si
, int eol
, void *userdata
) {
1165 fd_info
*i
= userdata
;
1167 if (!si
&& eol
< 0) {
1168 i
->operation_success
= 0;
1169 pa_threaded_mainloop_signal(i
->mainloop
, 0);
1176 if (!pa_cvolume_equal(&i
->source_volume
, &si
->volume
))
1177 i
->volume_modify_count
++;
1179 i
->source_volume
= si
->volume
;
1180 i
->source_index
= si
->index
;
1182 i
->operation_success
= 1;
1183 pa_threaded_mainloop_signal(i
->mainloop
, 0);
1186 static void subscribe_cb(pa_context
*context
, pa_subscription_event_type_t t
, uint32_t idx
, void *userdata
) {
1187 fd_info
*i
= userdata
;
1188 pa_operation
*o
= NULL
;
1190 if (i
->sink_index
!= idx
)
1193 if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) != PA_SUBSCRIPTION_EVENT_CHANGE
)
1196 if (!(o
= pa_context_get_sink_info_by_index(i
->context
, i
->sink_index
, sink_info_cb
, i
))) {
1197 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to get sink info: %s", pa_strerror(pa_context_errno(i
->context
)));
1201 pa_operation_unref(o
);
1204 static int mixer_open(int flags
, int *_errno
) {
1206 pa_operation
*o
= NULL
;
1209 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": mixer_open()\n");
1211 if (!(i
= fd_info_new(FD_INFO_MIXER
, _errno
)))
1214 pa_threaded_mainloop_lock(i
->mainloop
);
1216 pa_context_set_subscribe_callback(i
->context
, subscribe_cb
, i
);
1218 if (!(o
= pa_context_subscribe(i
->context
, PA_SUBSCRIPTION_MASK_SINK
| PA_SUBSCRIPTION_MASK_SOURCE
, context_success_cb
, i
))) {
1219 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to subscribe to events: %s", pa_strerror(pa_context_errno(i
->context
)));
1224 i
->operation_success
= 0;
1225 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1226 pa_threaded_mainloop_wait(i
->mainloop
);
1227 CONTEXT_CHECK_DEAD_GOTO(i
, fail
);
1230 pa_operation_unref(o
);
1233 if (!i
->operation_success
) {
1234 debug(DEBUG_LEVEL_NORMAL
, __FILE__
":Failed to subscribe to events: %s", pa_strerror(pa_context_errno(i
->context
)));
1241 if (!(o
= pa_context_get_sink_info_by_name(i
->context
, NULL
, sink_info_cb
, i
))) {
1242 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to get sink info: %s", pa_strerror(pa_context_errno(i
->context
)));
1247 i
->operation_success
= 0;
1248 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1249 pa_threaded_mainloop_wait(i
->mainloop
);
1250 CONTEXT_CHECK_DEAD_GOTO(i
, fail
);
1253 pa_operation_unref(o
);
1256 if (!i
->operation_success
) {
1257 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to get sink info: %s", pa_strerror(pa_context_errno(i
->context
)));
1262 /* Get source info */
1264 if (!(o
= pa_context_get_source_info_by_name(i
->context
, NULL
, source_info_cb
, i
))) {
1265 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to get source info: %s", pa_strerror(pa_context_errno(i
->context
)));
1270 i
->operation_success
= 0;
1271 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1272 pa_threaded_mainloop_wait(i
->mainloop
);
1273 CONTEXT_CHECK_DEAD_GOTO(i
, fail
);
1276 pa_operation_unref(o
);
1279 if (!i
->operation_success
) {
1280 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to get source info: %s", pa_strerror(pa_context_errno(i
->context
)));
1285 pa_threaded_mainloop_unlock(i
->mainloop
);
1287 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": mixer_open() succeeded, fd=%i\n", i
->app_fd
);
1289 fd_info_add_to_list(i
);
1297 pa_operation_unref(o
);
1299 pa_threaded_mainloop_unlock(i
->mainloop
);
1306 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": mixer_open() failed\n");
1311 static int sndstat_open(int flags
, int *_errno
) {
1312 static const char sndstat
[] =
1313 "Sound Driver:3.8.1a-980706 (PulseAudio Virtual OSS)\n"
1315 "Config options: 0\n"
1317 "Installed drivers:\n"
1318 "Type 255: PulseAudio Virtual OSS\n"
1321 "PulseAudio Virtual OSS\n"
1324 "0: PulseAudio Virtual OSS\n"
1326 "Synth devices: NOT ENABLED IN CONFIG\n"
1333 "0: PulseAudio Virtual OSS\n";
1335 char fn
[] = "/tmp/padsp-sndstat-XXXXXX";
1340 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": sndstat_open()\n");
1342 if (flags
!= O_RDONLY
1344 && flags
!= (O_RDONLY
|O_LARGEFILE
)
1348 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": bad access!\n");
1359 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": mkstemp() failed: %s\n", strerror(errno
));
1365 if (write(fd
, sndstat
, sizeof(sndstat
) -1) != sizeof(sndstat
)-1) {
1367 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": write() failed: %s\n", strerror(errno
));
1371 if (lseek(fd
, SEEK_SET
, 0) < 0) {
1373 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": lseek() failed: %s\n", strerror(errno
));
1385 int open(const char *filename
, int flags
, ...) {
1390 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": open(%s)\n", filename
);
1392 va_start(args
, flags
);
1393 if (flags
& O_CREAT
) {
1394 if (sizeof(mode_t
) < sizeof(int))
1395 mode
= va_arg(args
, int);
1397 mode
= va_arg(args
, mode_t
);
1401 if (!function_enter()) {
1403 return _open(filename
, flags
, mode
);
1406 if (dsp_cloak_enable() && (strcmp(filename
, "/dev/dsp") == 0 || strcmp(filename
, "/dev/adsp") == 0)) {
1407 r
= dsp_open(flags
, &_errno
);
1408 } else if (mixer_cloak_enable() && strcmp(filename
, "/dev/mixer") == 0) {
1409 r
= mixer_open(flags
, &_errno
);
1410 } else if (sndstat_cloak_enable() && strcmp(filename
, "/dev/sndstat") == 0) {
1411 r
= sndstat_open(flags
, &_errno
);
1415 return _open(filename
, flags
, mode
);
1426 static int mixer_ioctl(fd_info
*i
, unsigned long request
, void*argp
, int *_errno
) {
1430 case SOUND_MIXER_READ_DEVMASK
:
1431 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_DEVMASK\n");
1433 *(int*) argp
= SOUND_MASK_PCM
| SOUND_MASK_IGAIN
;
1436 case SOUND_MIXER_READ_RECMASK
:
1437 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_RECMASK\n");
1439 *(int*) argp
= SOUND_MASK_IGAIN
;
1442 case SOUND_MIXER_READ_STEREODEVS
:
1443 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_STEREODEVS\n");
1445 pa_threaded_mainloop_lock(i
->mainloop
);
1447 if (i
->sink_volume
.channels
> 1)
1448 *(int*) argp
|= SOUND_MASK_PCM
;
1449 if (i
->source_volume
.channels
> 1)
1450 *(int*) argp
|= SOUND_MASK_IGAIN
;
1451 pa_threaded_mainloop_unlock(i
->mainloop
);
1455 case SOUND_MIXER_READ_RECSRC
:
1456 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_RECSRC\n");
1458 *(int*) argp
= SOUND_MASK_IGAIN
;
1461 case SOUND_MIXER_WRITE_RECSRC
:
1462 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_WRITE_RECSRC\n");
1465 case SOUND_MIXER_READ_CAPS
:
1466 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_CAPS\n");
1471 case SOUND_MIXER_READ_PCM
:
1472 case SOUND_MIXER_READ_IGAIN
: {
1475 if (request
== SOUND_MIXER_READ_PCM
)
1476 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_PCM\n");
1478 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_IGAIN\n");
1480 pa_threaded_mainloop_lock(i
->mainloop
);
1482 if (request
== SOUND_MIXER_READ_PCM
)
1483 v
= &i
->sink_volume
;
1485 v
= &i
->source_volume
;
1488 ((v
->values
[0]*100/PA_VOLUME_NORM
)) |
1489 ((v
->values
[v
->channels
> 1 ? 1 : 0]*100/PA_VOLUME_NORM
) << 8);
1491 pa_threaded_mainloop_unlock(i
->mainloop
);
1496 case SOUND_MIXER_WRITE_PCM
:
1497 case SOUND_MIXER_WRITE_IGAIN
: {
1500 if (request
== SOUND_MIXER_READ_PCM
)
1501 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_WRITE_PCM\n");
1503 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_WRITE_IGAIN\n");
1505 pa_threaded_mainloop_lock(i
->mainloop
);
1507 if (request
== SOUND_MIXER_READ_PCM
) {
1509 pv
= &i
->sink_volume
;
1511 v
= i
->source_volume
;
1512 pv
= &i
->source_volume
;
1515 pv
->values
[0] = ((*(int*) argp
& 0xFF)*PA_VOLUME_NORM
)/100;
1516 pv
->values
[1] = ((*(int*) argp
>> 8)*PA_VOLUME_NORM
)/100;
1518 if (!pa_cvolume_equal(pv
, &v
)) {
1521 if (request
== SOUND_MIXER_READ_PCM
)
1522 o
= pa_context_set_sink_volume_by_index(i
->context
, i
->sink_index
, pv
, context_success_cb
, i
);
1524 o
= pa_context_set_source_volume_by_index(i
->context
, i
->source_index
, pv
, context_success_cb
, i
);
1527 debug(DEBUG_LEVEL_NORMAL
, __FILE__
":Failed set volume: %s", pa_strerror(pa_context_errno(i
->context
)));
1530 i
->operation_success
= 0;
1531 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1532 CONTEXT_CHECK_DEAD_GOTO(i
, exit_loop
);
1534 pa_threaded_mainloop_wait(i
->mainloop
);
1538 if (!i
->operation_success
)
1539 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to set volume: %s\n", pa_strerror(pa_context_errno(i
->context
)));
1541 pa_operation_unref(o
);
1544 /* We don't wait for completion here */
1545 i
->volume_modify_count
++;
1548 pa_threaded_mainloop_unlock(i
->mainloop
);
1553 case SOUND_MIXER_INFO
: {
1554 mixer_info
*mi
= argp
;
1556 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_INFO\n");
1558 memset(mi
, 0, sizeof(mixer_info
));
1559 strncpy(mi
->id
, "PULSEAUDIO", sizeof(mi
->id
));
1560 strncpy(mi
->name
, "PulseAudio Virtual OSS", sizeof(mi
->name
));
1561 pa_threaded_mainloop_lock(i
->mainloop
);
1562 mi
->modify_counter
= i
->volume_modify_count
;
1563 pa_threaded_mainloop_unlock(i
->mainloop
);
1568 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": unknown ioctl 0x%08lx\n", request
);
1581 static int map_format(int *fmt
, pa_sample_spec
*ss
) {
1585 ss
->format
= PA_SAMPLE_ULAW
;
1589 ss
->format
= PA_SAMPLE_ALAW
;
1596 ss
->format
= PA_SAMPLE_U8
;
1603 ss
->format
= PA_SAMPLE_S16BE
;
1610 ss
->format
= PA_SAMPLE_S16LE
;
1614 ss
->format
= PA_SAMPLE_S16NE
;
1622 static int map_format_back(pa_sample_format_t format
) {
1624 case PA_SAMPLE_S16LE
: return AFMT_S16_LE
;
1625 case PA_SAMPLE_S16BE
: return AFMT_S16_BE
;
1626 case PA_SAMPLE_ULAW
: return AFMT_MU_LAW
;
1627 case PA_SAMPLE_ALAW
: return AFMT_A_LAW
;
1628 case PA_SAMPLE_U8
: return AFMT_U8
;
1634 static int dsp_flush_fd(int fd
) {
1638 if (ioctl(fd
, SIOCINQ
, &l
) < 0) {
1639 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SIOCINQ: %s\n", strerror(errno
));
1647 k
= (size_t) l
> sizeof(buf
) ? sizeof(buf
) : (size_t) l
;
1648 if (read(fd
, buf
, k
) < 0)
1649 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": read(): %s\n", strerror(errno
));
1655 # warning "Your platform does not support SIOCINQ, something might not work as intended."
1660 static int dsp_flush_socket(fd_info
*i
) {
1663 if ((i
->thread_fd
< 0) && (i
->app_fd
< 0))
1666 if (i
->thread_fd
>= 0)
1667 res
= dsp_flush_fd(i
->thread_fd
);
1673 res
= dsp_flush_fd(i
->app_fd
);
1681 static int dsp_empty_socket(fd_info
*i
) {
1685 /* Empty the socket */
1689 if (i
->thread_fd
< 0)
1692 if (ioctl(i
->thread_fd
, SIOCINQ
, &l
) < 0) {
1693 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SIOCINQ: %s\n", strerror(errno
));
1702 pa_threaded_mainloop_wait(i
->mainloop
);
1707 # warning "Your platform does not support SIOCINQ, something might not work as intended."
1712 static int dsp_drain(fd_info
*i
) {
1713 pa_operation
*o
= NULL
;
1719 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Draining.\n");
1721 pa_threaded_mainloop_lock(i
->mainloop
);
1723 if (dsp_empty_socket(i
) < 0)
1726 if (!i
->play_stream
)
1729 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Really draining.\n");
1731 if (!(o
= pa_stream_drain(i
->play_stream
, stream_success_cb
, i
))) {
1732 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_drain(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1736 i
->operation_success
= 0;
1737 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1738 PLAYBACK_STREAM_CHECK_DEAD_GOTO(i
, fail
);
1740 pa_threaded_mainloop_wait(i
->mainloop
);
1743 if (!i
->operation_success
) {
1744 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_drain() 2: %s\n", pa_strerror(pa_context_errno(i
->context
)));
1753 pa_operation_unref(o
);
1755 pa_threaded_mainloop_unlock(i
->mainloop
);
1760 static int dsp_trigger(fd_info
*i
) {
1761 pa_operation
*o
= NULL
;
1764 if (!i
->play_stream
)
1767 pa_threaded_mainloop_lock(i
->mainloop
);
1769 if (dsp_empty_socket(i
) < 0)
1772 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Triggering.\n");
1774 if (!(o
= pa_stream_trigger(i
->play_stream
, stream_success_cb
, i
))) {
1775 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1779 i
->operation_success
= 0;
1780 while (!pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1781 PLAYBACK_STREAM_CHECK_DEAD_GOTO(i
, fail
);
1783 pa_threaded_mainloop_wait(i
->mainloop
);
1786 if (!i
->operation_success
) {
1787 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1796 pa_operation_unref(o
);
1798 pa_threaded_mainloop_unlock(i
->mainloop
);
1803 static int dsp_ioctl(fd_info
*i
, unsigned long request
, void*argp
, int *_errno
) {
1806 if (i
->thread_fd
== -1) {
1808 * We've encountered some fatal error and are just waiting
1811 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": got ioctl 0x%08lx in fatal error state\n", request
);
1817 case SNDCTL_DSP_SETFMT
: {
1818 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_SETFMT: %i\n", *(int*) argp
);
1820 pa_threaded_mainloop_lock(i
->mainloop
);
1822 if (*(int*) argp
== AFMT_QUERY
)
1823 *(int*) argp
= map_format_back(i
->sample_spec
.format
);
1825 map_format((int*) argp
, &i
->sample_spec
);
1829 pa_threaded_mainloop_unlock(i
->mainloop
);
1833 case SNDCTL_DSP_SPEED
: {
1838 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_SPEED: %i\n", *(int*) argp
);
1840 pa_threaded_mainloop_lock(i
->mainloop
);
1842 ss
= i
->sample_spec
;
1843 ss
.rate
= *(int*) argp
;
1845 if ((valid
= pa_sample_spec_valid(&ss
))) {
1846 i
->sample_spec
= ss
;
1850 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": ss: %s\n", pa_sample_spec_snprint(t
, sizeof(t
), &i
->sample_spec
));
1852 pa_threaded_mainloop_unlock(i
->mainloop
);
1862 case SNDCTL_DSP_STEREO
:
1863 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_STEREO: %i\n", *(int*) argp
);
1865 pa_threaded_mainloop_lock(i
->mainloop
);
1867 i
->sample_spec
.channels
= *(int*) argp
? 2 : 1;
1870 pa_threaded_mainloop_unlock(i
->mainloop
);
1873 case SNDCTL_DSP_CHANNELS
: {
1877 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_CHANNELS: %i\n", *(int*) argp
);
1879 pa_threaded_mainloop_lock(i
->mainloop
);
1881 ss
= i
->sample_spec
;
1882 ss
.channels
= *(int*) argp
;
1884 if ((valid
= pa_sample_spec_valid(&ss
))) {
1885 i
->sample_spec
= ss
;
1889 pa_threaded_mainloop_unlock(i
->mainloop
);
1899 case SNDCTL_DSP_GETBLKSIZE
:
1900 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_GETBLKSIZE\n");
1902 pa_threaded_mainloop_lock(i
->mainloop
);
1905 *(int*) argp
= i
->fragment_size
;
1907 pa_threaded_mainloop_unlock(i
->mainloop
);
1911 case SNDCTL_DSP_SETFRAGMENT
:
1912 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_SETFRAGMENT: 0x%08x\n", *(int*) argp
);
1914 pa_threaded_mainloop_lock(i
->mainloop
);
1916 i
->fragment_size
= 1 << ((*(int*) argp
) & 31);
1917 i
->n_fragments
= (*(int*) argp
) >> 16;
1919 /* 0x7FFF means that we can set whatever we like */
1920 if (i
->n_fragments
== 0x7FFF)
1921 i
->n_fragments
= 12;
1925 pa_threaded_mainloop_unlock(i
->mainloop
);
1929 case SNDCTL_DSP_GETCAPS
:
1930 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_CAPS\n");
1932 *(int*) argp
= DSP_CAP_DUPLEX
1933 #ifdef DSP_CAP_MULTI
1939 case SNDCTL_DSP_GETODELAY
: {
1942 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_GETODELAY\n");
1944 pa_threaded_mainloop_lock(i
->mainloop
);
1951 PLAYBACK_STREAM_CHECK_DEAD_GOTO(i
, exit_loop
);
1953 if (pa_stream_get_latency(i
->play_stream
, &usec
, NULL
) >= 0) {
1954 *(int*) argp
= pa_usec_to_bytes(usec
, &i
->sample_spec
);
1958 if (pa_context_errno(i
->context
) != PA_ERR_NODATA
) {
1959 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_get_latency(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1963 pa_threaded_mainloop_wait(i
->mainloop
);
1969 if (ioctl(i
->thread_fd
, SIOCINQ
, &l
) < 0)
1970 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SIOCINQ failed: %s\n", strerror(errno
));
1974 # warning "Your platform does not support SIOCINQ, something might not work as intended."
1977 pa_threaded_mainloop_unlock(i
->mainloop
);
1979 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": ODELAY: %i\n", *(int*) argp
);
1984 case SNDCTL_DSP_RESET
: {
1985 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_RESET\n");
1987 pa_threaded_mainloop_lock(i
->mainloop
);
1990 dsp_flush_socket(i
);
1992 i
->optr_n_blocks
= 0;
1994 pa_threaded_mainloop_unlock(i
->mainloop
);
1998 case SNDCTL_DSP_GETFMTS
: {
1999 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_GETFMTS\n");
2001 *(int*) argp
= AFMT_MU_LAW
|AFMT_A_LAW
|AFMT_U8
|AFMT_S16_LE
|AFMT_S16_BE
;
2005 case SNDCTL_DSP_POST
:
2006 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_POST\n");
2008 if (dsp_trigger(i
) < 0)
2012 case SNDCTL_DSP_SYNC
:
2013 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_SYNC\n");
2015 if (dsp_drain(i
) < 0)
2020 case SNDCTL_DSP_GETOSPACE
:
2021 case SNDCTL_DSP_GETISPACE
: {
2022 audio_buf_info
*bi
= (audio_buf_info
*) argp
;
2026 if (request
== SNDCTL_DSP_GETOSPACE
)
2027 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_GETOSPACE\n");
2029 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_GETISPACE\n");
2031 pa_threaded_mainloop_lock(i
->mainloop
);
2035 if (request
== SNDCTL_DSP_GETOSPACE
) {
2036 if (i
->play_stream
) {
2037 if ((k
= pa_stream_writable_size(i
->play_stream
)) == (size_t) -1)
2038 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_writable_size(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
2040 k
= i
->fragment_size
* i
->n_fragments
;
2043 if (ioctl(i
->thread_fd
, SIOCINQ
, &l
) < 0) {
2044 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SIOCINQ failed: %s\n", strerror(errno
));
2048 # warning "Your platform does not dsp_flush_fd, something might not work as intended."
2051 bi
->bytes
= k
> (size_t) l
? k
- l
: 0;
2053 if (i
->rec_stream
) {
2054 if ((k
= pa_stream_readable_size(i
->rec_stream
)) == (size_t) -1)
2055 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_readable_size(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
2060 if (ioctl(i
->app_fd
, SIOCINQ
, &l
) < 0) {
2061 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SIOCINQ failed: %s\n", strerror(errno
));
2065 # warning "Your platform does not dsp_flush_fd, something might not work as intended."
2070 bi
->fragsize
= i
->fragment_size
;
2071 bi
->fragstotal
= i
->n_fragments
;
2072 bi
->fragments
= bi
->bytes
/ bi
->fragsize
;
2074 pa_threaded_mainloop_unlock(i
->mainloop
);
2076 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": fragsize=%i, fragstotal=%i, bytes=%i, fragments=%i\n", bi
->fragsize
, bi
->fragstotal
, bi
->bytes
, bi
->fragments
);
2081 case SOUND_PCM_READ_RATE
:
2082 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_PCM_READ_RATE\n");
2084 pa_threaded_mainloop_lock(i
->mainloop
);
2085 *(int*) argp
= i
->sample_spec
.rate
;
2086 pa_threaded_mainloop_unlock(i
->mainloop
);
2089 case SOUND_PCM_READ_CHANNELS
:
2090 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_PCM_READ_CHANNELS\n");
2092 pa_threaded_mainloop_lock(i
->mainloop
);
2093 *(int*) argp
= i
->sample_spec
.channels
;
2094 pa_threaded_mainloop_unlock(i
->mainloop
);
2097 case SOUND_PCM_READ_BITS
:
2098 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_PCM_READ_BITS\n");
2100 pa_threaded_mainloop_lock(i
->mainloop
);
2101 *(int*) argp
= pa_sample_size(&i
->sample_spec
)*8;
2102 pa_threaded_mainloop_unlock(i
->mainloop
);
2105 case SNDCTL_DSP_GETOPTR
: {
2108 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_GETOPTR\n");
2110 info
= (count_info
*) argp
;
2111 memset(info
, 0, sizeof(*info
));
2113 pa_threaded_mainloop_lock(i
->mainloop
);
2118 PLAYBACK_STREAM_CHECK_DEAD_GOTO(i
, exit_loop
);
2120 if (pa_stream_get_time(i
->play_stream
, &usec
) >= 0) {
2121 size_t k
= pa_usec_to_bytes(usec
, &i
->sample_spec
);
2124 info
->bytes
= (int) k
;
2125 m
= k
/ i
->fragment_size
;
2126 info
->blocks
= m
- i
->optr_n_blocks
;
2127 i
->optr_n_blocks
= m
;
2132 if (pa_context_errno(i
->context
) != PA_ERR_NODATA
) {
2133 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_get_latency(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
2137 pa_threaded_mainloop_wait(i
->mainloop
);
2140 pa_threaded_mainloop_unlock(i
->mainloop
);
2142 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": GETOPTR bytes=%i, blocks=%i, ptr=%i\n", info
->bytes
, info
->blocks
, info
->ptr
);
2147 case SNDCTL_DSP_GETIPTR
:
2148 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": invalid ioctl SNDCTL_DSP_GETIPTR\n");
2151 case SNDCTL_DSP_SETDUPLEX
:
2152 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_SETDUPLEX\n");
2153 /* this is a no-op */
2157 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": unknown ioctl 0x%08lx\n", request
);
2171 int ioctl(int fd
, unsigned long request
, ...) {
2177 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": ioctl()\n");
2179 va_start(args
, request
);
2180 argp
= va_arg(args
, void *);
2183 if (!function_enter()) {
2185 return _ioctl(fd
, request
, argp
);
2188 if (!(i
= fd_info_find(fd
))) {
2191 return _ioctl(fd
, request
, argp
);
2194 if (i
->type
== FD_INFO_MIXER
)
2195 r
= mixer_ioctl(i
, request
, argp
, &_errno
);
2197 r
= dsp_ioctl(i
, request
, argp
, &_errno
);
2212 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": close()\n");
2214 if (!function_enter()) {
2219 if (!(i
= fd_info_find(fd
))) {
2225 fd_info_remove_from_list(i
);
2233 int access(const char *pathname
, int mode
) {
2236 /* Firefox needs this. See #27 */
2241 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": access(%s)\n", pathname
);
2243 if (strcmp(pathname
, "/dev/dsp") != 0 &&
2244 strcmp(pathname
, "/dev/adsp") != 0 &&
2245 strcmp(pathname
, "/dev/sndstat") != 0 &&
2246 strcmp(pathname
, "/dev/mixer") != 0) {
2248 return _access(pathname
, mode
);
2251 if (mode
& (W_OK
| X_OK
)) {
2252 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": access(%s, %x) = EACCESS\n", pathname
, mode
);
2257 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": access(%s, %x) = OK\n", pathname
, mode
);
2264 int open64(const char *filename
, int flags
, ...) {
2268 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": open64(%s)\n", filename
);
2270 va_start(args
, flags
);
2271 if (flags
& O_CREAT
)
2272 mode
= va_arg(args
, mode_t
);
2275 if (strcmp(filename
, "/dev/dsp") != 0 &&
2276 strcmp(filename
, "/dev/adsp") != 0 &&
2277 strcmp(filename
, "/dev/sndstat") != 0 &&
2278 strcmp(filename
, "/dev/mixer") != 0) {
2280 return _open64(filename
, flags
, mode
);
2283 return open(filename
, flags
, mode
);
2288 FILE* fopen(const char *filename
, const char *mode
) {
2293 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": fopen(%s)\n", filename
);
2295 if (strcmp(filename
, "/dev/dsp") != 0 &&
2296 strcmp(filename
, "/dev/adsp") != 0 &&
2297 strcmp(filename
, "/dev/sndstat") != 0 &&
2298 strcmp(filename
, "/dev/mixer") != 0) {
2300 return _fopen(filename
, mode
);
2316 if ((((mode
[1] == 'b') || (mode
[1] == 't')) && (mode
[2] == '+')) || (mode
[1] == '+'))
2319 if ((fd
= open(filename
, m
)) < 0)
2322 if (!(f
= fdopen(fd
, mode
))) {
2332 FILE *fopen64(const char *filename
, const char *mode
) {
2334 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": fopen64(%s)\n", filename
);
2336 if (strcmp(filename
, "/dev/dsp") != 0 &&
2337 strcmp(filename
, "/dev/adsp") != 0 &&
2338 strcmp(filename
, "/dev/sndstat") != 0 &&
2339 strcmp(filename
, "/dev/mixer") != 0) {
2340 LOAD_FOPEN64_FUNC();
2341 return _fopen64(filename
, mode
);
2344 return fopen(filename
, mode
);
2349 int fclose(FILE *f
) {
2352 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": fclose()\n");
2354 if (!function_enter()) {
2359 if (!(i
= fd_info_find(fileno(f
)))) {
2365 fd_info_remove_from_list(i
);
2367 /* Dirty trick to avoid that the fd is not freed twice, once by us
2368 * and once by the real fclose() */