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
;
97 PA_LLIST_FIELDS(fd_info
);
100 static int dsp_drain(fd_info
*i
);
101 static void fd_info_remove_from_list(fd_info
*i
);
103 static pthread_mutex_t fd_infos_mutex
= PTHREAD_MUTEX_INITIALIZER
;
104 static pthread_mutex_t func_mutex
= PTHREAD_MUTEX_INITIALIZER
;
106 static PA_LLIST_HEAD(fd_info
, fd_infos
) = NULL
;
108 static int (*_ioctl
)(int, int, void*) = NULL
;
109 static int (*_close
)(int) = NULL
;
110 static int (*_open
)(const char *, int, mode_t
) = NULL
;
111 static FILE* (*_fopen
)(const char *path
, const char *mode
) = NULL
;
113 static int (*_open64
)(const char *, int, mode_t
) = NULL
;
114 static FILE* (*_fopen64
)(const char *path
, const char *mode
) = NULL
;
116 static int (*_fclose
)(FILE *f
) = NULL
;
117 static int (*_access
)(const char *, int) = NULL
;
119 /* dlsym() violates ISO C, so confide the breakage into this function to
121 typedef void (*fnptr
)(void);
122 static inline fnptr
dlsym_fn(void *handle
, const char *symbol
) {
123 return (fnptr
) (long) dlsym(handle
, symbol
);
126 #define LOAD_IOCTL_FUNC() \
128 pthread_mutex_lock(&func_mutex); \
130 _ioctl = (int (*)(int, int, void*)) dlsym_fn(RTLD_NEXT, "ioctl"); \
131 pthread_mutex_unlock(&func_mutex); \
134 #define LOAD_OPEN_FUNC() \
136 pthread_mutex_lock(&func_mutex); \
138 _open = (int (*)(const char *, int, mode_t)) dlsym_fn(RTLD_NEXT, "open"); \
139 pthread_mutex_unlock(&func_mutex); \
142 #define LOAD_OPEN64_FUNC() \
144 pthread_mutex_lock(&func_mutex); \
146 _open64 = (int (*)(const char *, int, mode_t)) dlsym_fn(RTLD_NEXT, "open64"); \
147 pthread_mutex_unlock(&func_mutex); \
150 #define LOAD_CLOSE_FUNC() \
152 pthread_mutex_lock(&func_mutex); \
154 _close = (int (*)(int)) dlsym_fn(RTLD_NEXT, "close"); \
155 pthread_mutex_unlock(&func_mutex); \
158 #define LOAD_ACCESS_FUNC() \
160 pthread_mutex_lock(&func_mutex); \
162 _access = (int (*)(const char*, int)) dlsym_fn(RTLD_NEXT, "access"); \
163 pthread_mutex_unlock(&func_mutex); \
166 #define LOAD_FOPEN_FUNC() \
168 pthread_mutex_lock(&func_mutex); \
170 _fopen = (FILE* (*)(const char *, const char*)) dlsym_fn(RTLD_NEXT, "fopen"); \
171 pthread_mutex_unlock(&func_mutex); \
174 #define LOAD_FOPEN64_FUNC() \
176 pthread_mutex_lock(&func_mutex); \
178 _fopen64 = (FILE* (*)(const char *, const char*)) dlsym_fn(RTLD_NEXT, "fopen64"); \
179 pthread_mutex_unlock(&func_mutex); \
182 #define LOAD_FCLOSE_FUNC() \
184 pthread_mutex_lock(&func_mutex); \
186 _fclose = (int (*)(FILE *)) dlsym_fn(RTLD_NEXT, "fclose"); \
187 pthread_mutex_unlock(&func_mutex); \
190 #define CONTEXT_CHECK_DEAD_GOTO(i, label) do { \
191 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY) { \
192 debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
197 #define PLAYBACK_STREAM_CHECK_DEAD_GOTO(i, label) do { \
198 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY || \
199 !(i)->play_stream || pa_stream_get_state((i)->play_stream) != PA_STREAM_READY) { \
200 debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
205 #define RECORD_STREAM_CHECK_DEAD_GOTO(i, label) do { \
206 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY || \
207 !(i)->rec_stream || pa_stream_get_state((i)->rec_stream) != PA_STREAM_READY) { \
208 debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
213 static void debug(int level
, const char *format
, ...) PA_GCC_PRINTF_ATTR(2,3);
215 #define DEBUG_LEVEL_ALWAYS 0
216 #define DEBUG_LEVEL_NORMAL 1
217 #define DEBUG_LEVEL_VERBOSE 2
219 static void debug(int level
, const char *format
, ...) {
221 const char *dlevel_s
;
224 dlevel_s
= getenv("PADSP_DEBUG");
228 dlevel
= atoi(dlevel_s
);
233 va_start(ap
, format
);
234 vfprintf(stderr
, format
, ap
);
238 static int padsp_disabled(void) {
240 static int sym_resolved
= 0;
242 /* If the current process has a symbol __padsp_disabled__ we use
243 * it to detect whether we should enable our stuff or not. A
244 * program needs to be compiled with -rdynamic for this to work!
245 * The symbol must be an int containing a three bit bitmask: bit 1
246 * -> disable /dev/dsp emulation, bit 2 -> disable /dev/sndstat
247 * emulation, bit 3 -> disable /dev/mixer emulation. Hence a value
248 * of 7 disables padsp entirely. */
250 pthread_mutex_lock(&func_mutex
);
252 sym
= (int*) dlsym(RTLD_DEFAULT
, "__padsp_disabled__");
256 pthread_mutex_unlock(&func_mutex
);
264 static int dsp_cloak_enable(void) {
265 if (padsp_disabled() & 1)
268 if (getenv("PADSP_NO_DSP"))
274 static int sndstat_cloak_enable(void) {
275 if (padsp_disabled() & 2)
278 if (getenv("PADSP_NO_SNDSTAT"))
284 static int mixer_cloak_enable(void) {
285 if (padsp_disabled() & 4)
288 if (getenv("PADSP_NO_MIXER"))
293 static pthread_key_t recursion_key
;
295 static void recursion_key_alloc(void) {
296 pthread_key_create(&recursion_key
, NULL
);
299 static int function_enter(void) {
300 /* Avoid recursive calls */
301 static pthread_once_t recursion_key_once
= PTHREAD_ONCE_INIT
;
302 pthread_once(&recursion_key_once
, recursion_key_alloc
);
304 if (pthread_getspecific(recursion_key
))
307 pthread_setspecific(recursion_key
, (void*) 1);
311 static void function_exit(void) {
312 pthread_setspecific(recursion_key
, NULL
);
315 static void fd_info_free(fd_info
*i
) {
318 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": freeing fd info (fd=%i)\n", i
->app_fd
);
323 pa_threaded_mainloop_stop(i
->mainloop
);
325 if (i
->play_stream
) {
326 pa_stream_disconnect(i
->play_stream
);
327 pa_stream_unref(i
->play_stream
);
331 pa_stream_disconnect(i
->rec_stream
);
332 pa_stream_unref(i
->rec_stream
);
336 pa_context_disconnect(i
->context
);
337 pa_context_unref(i
->context
);
341 pa_threaded_mainloop_free(i
->mainloop
);
343 if (i
->app_fd
>= 0) {
348 if (i
->thread_fd
>= 0) {
350 _close(i
->thread_fd
);
355 pthread_mutex_destroy(&i
->mutex
);
359 static fd_info
*fd_info_ref(fd_info
*i
) {
362 pthread_mutex_lock(&i
->mutex
);
366 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": ref++, now %i\n", i
->ref
);
367 pthread_mutex_unlock(&i
->mutex
);
372 static void fd_info_unref(fd_info
*i
) {
374 pthread_mutex_lock(&i
->mutex
);
377 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": ref--, now %i\n", i
->ref
);
378 pthread_mutex_unlock(&i
->mutex
);
384 static void context_state_cb(pa_context
*c
, void *userdata
) {
385 fd_info
*i
= userdata
;
388 switch (pa_context_get_state(c
)) {
389 case PA_CONTEXT_READY
:
390 case PA_CONTEXT_TERMINATED
:
391 case PA_CONTEXT_FAILED
:
392 pa_threaded_mainloop_signal(i
->mainloop
, 0);
395 case PA_CONTEXT_UNCONNECTED
:
396 case PA_CONTEXT_CONNECTING
:
397 case PA_CONTEXT_AUTHORIZING
:
398 case PA_CONTEXT_SETTING_NAME
:
403 static void reset_params(fd_info
*i
) {
406 i
->sample_spec
.format
= PA_SAMPLE_U8
;
407 i
->sample_spec
.channels
= 1;
408 i
->sample_spec
.rate
= 8000;
409 i
->fragment_size
= 0;
413 static const char *client_name(char *buf
, size_t n
) {
417 if ((e
= getenv("PADSP_CLIENT_NAME")))
420 if (pa_get_binary_name(p
, sizeof(p
)))
421 snprintf(buf
, n
, "OSS Emulation[%s]", p
);
423 snprintf(buf
, n
, "OSS");
428 static const char *stream_name(void) {
431 if ((e
= getenv("PADSP_STREAM_NAME")))
434 return "Audio Stream";
437 static void atfork_prepare(void) {
440 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": atfork_prepare() enter\n");
444 pthread_mutex_lock(&fd_infos_mutex
);
446 for (i
= fd_infos
; i
; i
= i
->next
) {
447 pthread_mutex_lock(&i
->mutex
);
448 pa_threaded_mainloop_lock(i
->mainloop
);
451 pthread_mutex_lock(&func_mutex
);
454 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": atfork_prepare() exit\n");
457 static void atfork_parent(void) {
460 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": atfork_parent() enter\n");
462 pthread_mutex_unlock(&func_mutex
);
464 for (i
= fd_infos
; i
; i
= i
->next
) {
465 pa_threaded_mainloop_unlock(i
->mainloop
);
466 pthread_mutex_unlock(&i
->mutex
);
469 pthread_mutex_unlock(&fd_infos_mutex
);
473 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": atfork_parent() exit\n");
476 static void atfork_child(void) {
479 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": atfork_child() enter\n");
481 /* We do only the bare minimum to get all fds closed */
482 pthread_mutex_init(&func_mutex
, NULL
);
483 pthread_mutex_init(&fd_infos_mutex
, NULL
);
485 for (i
= fd_infos
; i
; i
= i
->next
) {
486 pthread_mutex_init(&i
->mutex
, NULL
);
489 pa_context_disconnect(i
->context
);
490 pa_context_unref(i
->context
);
494 if (i
->play_stream
) {
495 pa_stream_unref(i
->play_stream
);
496 i
->play_stream
= NULL
;
500 pa_stream_unref(i
->rec_stream
);
501 i
->rec_stream
= NULL
;
504 if (i
->app_fd
>= 0) {
509 if (i
->thread_fd
>= 0) {
519 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": atfork_child() exit\n");
522 static void install_atfork(void) {
523 pthread_atfork(atfork_prepare
, atfork_parent
, atfork_child
);
526 static void stream_success_cb(pa_stream
*s
, int success
, void *userdata
) {
527 fd_info
*i
= userdata
;
532 i
->operation_success
= success
;
533 pa_threaded_mainloop_signal(i
->mainloop
, 0);
536 static void context_success_cb(pa_context
*c
, int success
, void *userdata
) {
537 fd_info
*i
= userdata
;
542 i
->operation_success
= success
;
543 pa_threaded_mainloop_signal(i
->mainloop
, 0);
546 static fd_info
* fd_info_new(fd_info_type_t type
, int *_errno
) {
548 int sfds
[2] = { -1, -1 };
550 static pthread_once_t install_atfork_once
= PTHREAD_ONCE_INIT
;
552 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": fd_info_new()\n");
554 signal(SIGPIPE
, SIG_IGN
); /* Yes, ugly as hell */
556 pthread_once(&install_atfork_once
, install_atfork
);
558 if (!(i
= malloc(sizeof(fd_info
)))) {
563 i
->app_fd
= i
->thread_fd
= -1;
568 i
->play_stream
= NULL
;
569 i
->rec_stream
= NULL
;
572 pthread_mutex_init(&i
->mutex
, NULL
);
577 pa_cvolume_reset(&i
->sink_volume
, 2);
578 pa_cvolume_reset(&i
->source_volume
, 2);
579 i
->volume_modify_count
= 0;
580 i
->sink_index
= (uint32_t) -1;
581 i
->source_index
= (uint32_t) -1;
582 PA_LLIST_INIT(fd_info
, i
);
586 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, sfds
) < 0) {
588 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": socket() failed: %s\n", strerror(errno
));
593 i
->thread_fd
= sfds
[1];
595 if (!(i
->mainloop
= pa_threaded_mainloop_new())) {
597 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_threaded_mainloop_new() failed\n");
601 if (!(i
->context
= pa_context_new(pa_threaded_mainloop_get_api(i
->mainloop
), client_name(name
, sizeof(name
))))) {
603 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_context_new() failed\n");
607 pa_context_set_state_callback(i
->context
, context_state_cb
, i
);
609 if (pa_context_connect(i
->context
, NULL
, 0, NULL
) < 0) {
610 *_errno
= ECONNREFUSED
;
611 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
615 pa_threaded_mainloop_lock(i
->mainloop
);
617 if (pa_threaded_mainloop_start(i
->mainloop
) < 0) {
619 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_threaded_mainloop_start() failed\n");
620 goto unlock_and_fail
;
623 /* Wait until the context is ready */
624 pa_threaded_mainloop_wait(i
->mainloop
);
626 if (pa_context_get_state(i
->context
) != PA_CONTEXT_READY
) {
627 *_errno
= ECONNREFUSED
;
628 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
629 goto unlock_and_fail
;
632 pa_threaded_mainloop_unlock(i
->mainloop
);
637 pa_threaded_mainloop_unlock(i
->mainloop
);
647 static void fd_info_add_to_list(fd_info
*i
) {
650 pthread_mutex_lock(&fd_infos_mutex
);
651 PA_LLIST_PREPEND(fd_info
, fd_infos
, i
);
652 pthread_mutex_unlock(&fd_infos_mutex
);
657 static void fd_info_remove_from_list(fd_info
*i
) {
660 pthread_mutex_lock(&fd_infos_mutex
);
661 PA_LLIST_REMOVE(fd_info
, fd_infos
, i
);
662 pthread_mutex_unlock(&fd_infos_mutex
);
667 static fd_info
* fd_info_find(int fd
) {
670 pthread_mutex_lock(&fd_infos_mutex
);
672 for (i
= fd_infos
; i
; i
= i
->next
)
673 if (i
->app_fd
== fd
&& !i
->unusable
) {
678 pthread_mutex_unlock(&fd_infos_mutex
);
683 static void fix_metrics(fd_info
*i
) {
685 char t
[PA_SAMPLE_SPEC_SNPRINT_MAX
];
687 fs
= pa_frame_size(&i
->sample_spec
);
689 /* Don't fix things more than necessary */
690 if ((i
->fragment_size
% fs
) == 0 &&
691 i
->n_fragments
>= 2 &&
692 i
->fragment_size
> 0)
695 i
->fragment_size
= (i
->fragment_size
/fs
)*fs
;
697 /* Number of fragments set? */
698 if (i
->n_fragments
< 2) {
699 if (i
->fragment_size
> 0) {
700 i
->n_fragments
= pa_bytes_per_second(&i
->sample_spec
) / 2 / i
->fragment_size
;
701 if (i
->n_fragments
< 2)
707 /* Fragment size set? */
708 if (i
->fragment_size
<= 0) {
709 i
->fragment_size
= pa_bytes_per_second(&i
->sample_spec
) / 2 / i
->n_fragments
;
710 if (i
->fragment_size
< 1024)
711 i
->fragment_size
= 1024;
714 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": sample spec: %s\n", pa_sample_spec_snprint(t
, sizeof(t
), &i
->sample_spec
));
715 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": fixated metrics to %i fragments, %li bytes each.\n", i
->n_fragments
, (long)i
->fragment_size
);
718 static void stream_request_cb(pa_stream
*s
, size_t length
, void *userdata
) {
719 fd_info
*i
= userdata
;
723 pa_mainloop_api
*api
;
726 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
728 if (s
== i
->play_stream
) {
729 n
= pa_stream_writable_size(i
->play_stream
);
730 if (n
== (size_t)-1) {
731 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_writable_size(): %s\n",
732 pa_strerror(pa_context_errno(i
->context
)));
735 if (n
>= i
->fragment_size
)
736 i
->io_flags
|= PA_IO_EVENT_INPUT
;
738 i
->io_flags
&= ~PA_IO_EVENT_INPUT
;
741 if (s
== i
->rec_stream
) {
742 n
= pa_stream_readable_size(i
->rec_stream
);
743 if (n
== (size_t)-1) {
744 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_readable_size(): %s\n",
745 pa_strerror(pa_context_errno(i
->context
)));
748 if (n
>= i
->fragment_size
)
749 i
->io_flags
|= PA_IO_EVENT_OUTPUT
;
751 i
->io_flags
&= ~PA_IO_EVENT_OUTPUT
;
754 api
->io_enable(i
->io_event
, i
->io_flags
);
758 static void stream_latency_update_cb(pa_stream
*s
, void *userdata
) {
759 fd_info
*i
= userdata
;
762 pa_threaded_mainloop_signal(i
->mainloop
, 0);
765 static void fd_info_shutdown(fd_info
*i
) {
769 pa_mainloop_api
*api
;
770 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
771 api
->io_free(i
->io_event
);
776 if (i
->thread_fd
>= 0) {
782 static int fd_info_copy_data(fd_info
*i
, int force
) {
785 if (!i
->play_stream
&& !i
->rec_stream
)
788 if ((i
->play_stream
) && (pa_stream_get_state(i
->play_stream
) == PA_STREAM_READY
)) {
789 n
= pa_stream_writable_size(i
->play_stream
);
791 if (n
== (size_t)-1) {
792 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_writable_size(): %s\n",
793 pa_strerror(pa_context_errno(i
->context
)));
797 while (n
>= i
->fragment_size
|| force
) {
801 if (!(i
->buf
= malloc(i
->fragment_size
))) {
802 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": malloc() failed.\n");
807 if ((r
= read(i
->thread_fd
, i
->buf
, i
->fragment_size
)) <= 0) {
812 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": read(): %s\n", r
== 0 ? "EOF" : strerror(errno
));
816 if (pa_stream_write(i
->play_stream
, i
->buf
, r
, free
, 0, PA_SEEK_RELATIVE
) < 0) {
817 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_write(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
823 assert(n
>= (size_t) r
);
827 if (n
>= i
->fragment_size
)
828 i
->io_flags
|= PA_IO_EVENT_INPUT
;
830 i
->io_flags
&= ~PA_IO_EVENT_INPUT
;
833 if ((i
->rec_stream
) && (pa_stream_get_state(i
->rec_stream
) == PA_STREAM_READY
)) {
834 n
= pa_stream_readable_size(i
->rec_stream
);
836 if (n
== (size_t)-1) {
837 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_readable_size(): %s\n",
838 pa_strerror(pa_context_errno(i
->context
)));
842 while (n
>= i
->fragment_size
|| force
) {
848 if (pa_stream_peek(i
->rec_stream
, &data
, &len
) < 0) {
849 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_peek(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
856 buf
= (const char*)data
+ i
->rec_offset
;
858 if ((r
= write(i
->thread_fd
, buf
, len
- i
->rec_offset
)) <= 0) {
863 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": write(): %s\n", strerror(errno
));
867 assert((size_t)r
<= len
- i
->rec_offset
);
870 if (i
->rec_offset
== len
) {
871 if (pa_stream_drop(i
->rec_stream
) < 0) {
872 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_drop(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
878 assert(n
>= (size_t) r
);
882 if (n
>= i
->fragment_size
)
883 i
->io_flags
|= PA_IO_EVENT_OUTPUT
;
885 i
->io_flags
&= ~PA_IO_EVENT_OUTPUT
;
889 pa_mainloop_api
*api
;
891 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
892 api
->io_enable(i
->io_event
, i
->io_flags
);
898 static void stream_state_cb(pa_stream
*s
, void * userdata
) {
899 fd_info
*i
= userdata
;
902 switch (pa_stream_get_state(s
)) {
904 case PA_STREAM_READY
:
905 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": stream established.\n");
908 case PA_STREAM_FAILED
:
909 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
913 case PA_STREAM_TERMINATED
:
914 case PA_STREAM_UNCONNECTED
:
915 case PA_STREAM_CREATING
:
920 static int create_playback_stream(fd_info
*i
) {
928 if (!(i
->play_stream
= pa_stream_new(i
->context
, stream_name(), &i
->sample_spec
, NULL
))) {
929 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
933 pa_stream_set_state_callback(i
->play_stream
, stream_state_cb
, i
);
934 pa_stream_set_write_callback(i
->play_stream
, stream_request_cb
, i
);
935 pa_stream_set_latency_update_callback(i
->play_stream
, stream_latency_update_cb
, i
);
937 memset(&attr
, 0, sizeof(attr
));
938 attr
.maxlength
= i
->fragment_size
* (i
->n_fragments
+1);
939 attr
.tlength
= i
->fragment_size
* i
->n_fragments
;
940 attr
.prebuf
= i
->fragment_size
;
941 attr
.minreq
= i
->fragment_size
;
943 if (pa_stream_connect_playback(i
->play_stream
, NULL
, &attr
, PA_STREAM_INTERPOLATE_TIMING
|PA_STREAM_AUTO_TIMING_UPDATE
, NULL
, NULL
) < 0) {
944 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
948 n
= i
->fragment_size
;
949 setsockopt(i
->app_fd
, SOL_SOCKET
, SO_SNDBUF
, &n
, sizeof(n
));
950 n
= i
->fragment_size
;
951 setsockopt(i
->thread_fd
, SOL_SOCKET
, SO_RCVBUF
, &n
, sizeof(n
));
959 static int create_record_stream(fd_info
*i
) {
967 if (!(i
->rec_stream
= pa_stream_new(i
->context
, stream_name(), &i
->sample_spec
, NULL
))) {
968 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
972 pa_stream_set_state_callback(i
->rec_stream
, stream_state_cb
, i
);
973 pa_stream_set_read_callback(i
->rec_stream
, stream_request_cb
, i
);
974 pa_stream_set_latency_update_callback(i
->rec_stream
, stream_latency_update_cb
, i
);
976 memset(&attr
, 0, sizeof(attr
));
977 attr
.maxlength
= i
->fragment_size
* (i
->n_fragments
+1);
978 attr
.fragsize
= i
->fragment_size
;
980 if (pa_stream_connect_record(i
->rec_stream
, NULL
, &attr
, PA_STREAM_INTERPOLATE_TIMING
|PA_STREAM_AUTO_TIMING_UPDATE
) < 0) {
981 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
985 n
= i
->fragment_size
;
986 setsockopt(i
->app_fd
, SOL_SOCKET
, SO_RCVBUF
, &n
, sizeof(n
));
987 n
= i
->fragment_size
;
988 setsockopt(i
->thread_fd
, SOL_SOCKET
, SO_SNDBUF
, &n
, sizeof(n
));
996 static void free_streams(fd_info
*i
) {
999 if (i
->play_stream
) {
1000 pa_stream_disconnect(i
->play_stream
);
1001 pa_stream_unref(i
->play_stream
);
1002 i
->play_stream
= NULL
;
1005 if (i
->rec_stream
) {
1006 pa_stream_disconnect(i
->rec_stream
);
1007 pa_stream_unref(i
->rec_stream
);
1008 i
->rec_stream
= NULL
;
1012 static void io_event_cb(pa_mainloop_api
*api
, pa_io_event
*e
, int fd
, pa_io_event_flags_t flags
, void *userdata
) {
1013 fd_info
*i
= userdata
;
1015 pa_threaded_mainloop_signal(i
->mainloop
, 0);
1017 if (flags
& PA_IO_EVENT_INPUT
) {
1019 if (!i
->play_stream
) {
1020 if (create_playback_stream(i
) < 0)
1023 if (fd_info_copy_data(i
, 0) < 0)
1027 } else if (flags
& PA_IO_EVENT_OUTPUT
) {
1029 if (!i
->rec_stream
) {
1030 if (create_record_stream(i
) < 0)
1033 if (fd_info_copy_data(i
, 0) < 0)
1037 } else if (flags
& (PA_IO_EVENT_HANGUP
|PA_IO_EVENT_ERROR
))
1043 /* We can't do anything better than removing the event source */
1044 fd_info_shutdown(i
);
1047 static int dsp_open(int flags
, int *_errno
) {
1049 pa_mainloop_api
*api
;
1053 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": dsp_open()\n");
1055 if (!(i
= fd_info_new(FD_INFO_STREAM
, _errno
)))
1058 if ((flags
& O_NONBLOCK
) == O_NONBLOCK
) {
1059 if ((f
= fcntl(i
->app_fd
, F_GETFL
)) >= 0)
1060 fcntl(i
->app_fd
, F_SETFL
, f
|O_NONBLOCK
);
1062 if ((f
= fcntl(i
->thread_fd
, F_GETFL
)) >= 0)
1063 fcntl(i
->thread_fd
, F_SETFL
, f
|O_NONBLOCK
);
1065 fcntl(i
->app_fd
, F_SETFD
, FD_CLOEXEC
);
1066 fcntl(i
->thread_fd
, F_SETFD
, FD_CLOEXEC
);
1068 pa_threaded_mainloop_lock(i
->mainloop
);
1069 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
1071 switch (flags
& O_ACCMODE
) {
1073 i
->io_flags
= PA_IO_EVENT_OUTPUT
;
1074 shutdown(i
->thread_fd
, SHUT_RD
);
1075 shutdown(i
->app_fd
, SHUT_WR
);
1078 i
->io_flags
= PA_IO_EVENT_INPUT
;
1079 shutdown(i
->thread_fd
, SHUT_WR
);
1080 shutdown(i
->app_fd
, SHUT_RD
);
1083 i
->io_flags
= PA_IO_EVENT_INPUT
| PA_IO_EVENT_OUTPUT
;
1089 if (!(i
->io_event
= api
->io_new(api
, i
->thread_fd
, i
->io_flags
, io_event_cb
, i
)))
1092 pa_threaded_mainloop_unlock(i
->mainloop
);
1094 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": dsp_open() succeeded, fd=%i\n", i
->app_fd
);
1096 fd_info_add_to_list(i
);
1103 pa_threaded_mainloop_unlock(i
->mainloop
);
1110 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": dsp_open() failed\n");
1115 static void sink_info_cb(pa_context
*context
, const pa_sink_info
*si
, int eol
, void *userdata
) {
1116 fd_info
*i
= userdata
;
1118 if (!si
&& eol
< 0) {
1119 i
->operation_success
= 0;
1120 pa_threaded_mainloop_signal(i
->mainloop
, 0);
1127 if (!pa_cvolume_equal(&i
->sink_volume
, &si
->volume
))
1128 i
->volume_modify_count
++;
1130 i
->sink_volume
= si
->volume
;
1131 i
->sink_index
= si
->index
;
1133 i
->operation_success
= 1;
1134 pa_threaded_mainloop_signal(i
->mainloop
, 0);
1137 static void source_info_cb(pa_context
*context
, const pa_source_info
*si
, int eol
, void *userdata
) {
1138 fd_info
*i
= userdata
;
1140 if (!si
&& eol
< 0) {
1141 i
->operation_success
= 0;
1142 pa_threaded_mainloop_signal(i
->mainloop
, 0);
1149 if (!pa_cvolume_equal(&i
->source_volume
, &si
->volume
))
1150 i
->volume_modify_count
++;
1152 i
->source_volume
= si
->volume
;
1153 i
->source_index
= si
->index
;
1155 i
->operation_success
= 1;
1156 pa_threaded_mainloop_signal(i
->mainloop
, 0);
1159 static void subscribe_cb(pa_context
*context
, pa_subscription_event_type_t t
, uint32_t idx
, void *userdata
) {
1160 fd_info
*i
= userdata
;
1161 pa_operation
*o
= NULL
;
1163 if (i
->sink_index
!= idx
)
1166 if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) != PA_SUBSCRIPTION_EVENT_CHANGE
)
1169 if (!(o
= pa_context_get_sink_info_by_index(i
->context
, i
->sink_index
, sink_info_cb
, i
))) {
1170 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to get sink info: %s", pa_strerror(pa_context_errno(i
->context
)));
1174 pa_operation_unref(o
);
1177 static int mixer_open(int flags
, int *_errno
) {
1179 pa_operation
*o
= NULL
;
1182 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": mixer_open()\n");
1184 if (!(i
= fd_info_new(FD_INFO_MIXER
, _errno
)))
1187 pa_threaded_mainloop_lock(i
->mainloop
);
1189 pa_context_set_subscribe_callback(i
->context
, subscribe_cb
, i
);
1191 if (!(o
= pa_context_subscribe(i
->context
, PA_SUBSCRIPTION_MASK_SINK
| PA_SUBSCRIPTION_MASK_SOURCE
, context_success_cb
, i
))) {
1192 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to subscribe to events: %s", pa_strerror(pa_context_errno(i
->context
)));
1197 i
->operation_success
= 0;
1198 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1199 pa_threaded_mainloop_wait(i
->mainloop
);
1200 CONTEXT_CHECK_DEAD_GOTO(i
, fail
);
1203 pa_operation_unref(o
);
1206 if (!i
->operation_success
) {
1207 debug(DEBUG_LEVEL_NORMAL
, __FILE__
":Failed to subscribe to events: %s", pa_strerror(pa_context_errno(i
->context
)));
1214 if (!(o
= pa_context_get_sink_info_by_name(i
->context
, NULL
, sink_info_cb
, i
))) {
1215 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to get sink info: %s", pa_strerror(pa_context_errno(i
->context
)));
1220 i
->operation_success
= 0;
1221 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1222 pa_threaded_mainloop_wait(i
->mainloop
);
1223 CONTEXT_CHECK_DEAD_GOTO(i
, fail
);
1226 pa_operation_unref(o
);
1229 if (!i
->operation_success
) {
1230 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to get sink info: %s", pa_strerror(pa_context_errno(i
->context
)));
1235 /* Get source info */
1237 if (!(o
= pa_context_get_source_info_by_name(i
->context
, NULL
, source_info_cb
, i
))) {
1238 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to get source info: %s", pa_strerror(pa_context_errno(i
->context
)));
1243 i
->operation_success
= 0;
1244 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1245 pa_threaded_mainloop_wait(i
->mainloop
);
1246 CONTEXT_CHECK_DEAD_GOTO(i
, fail
);
1249 pa_operation_unref(o
);
1252 if (!i
->operation_success
) {
1253 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to get source info: %s", pa_strerror(pa_context_errno(i
->context
)));
1258 pa_threaded_mainloop_unlock(i
->mainloop
);
1260 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": mixer_open() succeeded, fd=%i\n", i
->app_fd
);
1262 fd_info_add_to_list(i
);
1270 pa_operation_unref(o
);
1272 pa_threaded_mainloop_unlock(i
->mainloop
);
1279 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": mixer_open() failed\n");
1284 static int sndstat_open(int flags
, int *_errno
) {
1285 static const char sndstat
[] =
1286 "Sound Driver:3.8.1a-980706 (PulseAudio Virtual OSS)\n"
1288 "Config options: 0\n"
1290 "Installed drivers:\n"
1291 "Type 255: PulseAudio Virtual OSS\n"
1294 "PulseAudio Virtual OSS\n"
1297 "0: PulseAudio Virtual OSS\n"
1299 "Synth devices: NOT ENABLED IN CONFIG\n"
1306 "0: PulseAudio Virtual OSS\n";
1308 char fn
[] = "/tmp/padsp-sndstat-XXXXXX";
1313 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": sndstat_open()\n");
1315 if (flags
!= O_RDONLY
1317 && flags
!= (O_RDONLY
|O_LARGEFILE
)
1321 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": bad access!\n");
1332 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": mkstemp() failed: %s\n", strerror(errno
));
1338 if (write(fd
, sndstat
, sizeof(sndstat
) -1) != sizeof(sndstat
)-1) {
1340 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": write() failed: %s\n", strerror(errno
));
1344 if (lseek(fd
, SEEK_SET
, 0) < 0) {
1346 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": lseek() failed: %s\n", strerror(errno
));
1358 int open(const char *filename
, int flags
, ...) {
1363 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": open(%s)\n", filename
);
1365 va_start(args
, flags
);
1366 if (flags
& O_CREAT
) {
1367 if (sizeof(mode_t
) < sizeof(int))
1368 mode
= va_arg(args
, int);
1370 mode
= va_arg(args
, mode_t
);
1374 if (!function_enter()) {
1376 return _open(filename
, flags
, mode
);
1379 if (dsp_cloak_enable() && (strcmp(filename
, "/dev/dsp") == 0 || strcmp(filename
, "/dev/adsp") == 0)) {
1380 r
= dsp_open(flags
, &_errno
);
1381 } else if (mixer_cloak_enable() && strcmp(filename
, "/dev/mixer") == 0) {
1382 r
= mixer_open(flags
, &_errno
);
1383 } else if (sndstat_cloak_enable() && strcmp(filename
, "/dev/sndstat") == 0) {
1384 r
= sndstat_open(flags
, &_errno
);
1388 return _open(filename
, flags
, mode
);
1399 static int mixer_ioctl(fd_info
*i
, unsigned long request
, void*argp
, int *_errno
) {
1403 case SOUND_MIXER_READ_DEVMASK
:
1404 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_DEVMASK\n");
1406 *(int*) argp
= SOUND_MASK_PCM
| SOUND_MASK_IGAIN
;
1409 case SOUND_MIXER_READ_RECMASK
:
1410 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_RECMASK\n");
1412 *(int*) argp
= SOUND_MASK_IGAIN
;
1415 case SOUND_MIXER_READ_STEREODEVS
:
1416 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_STEREODEVS\n");
1418 pa_threaded_mainloop_lock(i
->mainloop
);
1420 if (i
->sink_volume
.channels
> 1)
1421 *(int*) argp
|= SOUND_MASK_PCM
;
1422 if (i
->source_volume
.channels
> 1)
1423 *(int*) argp
|= SOUND_MASK_IGAIN
;
1424 pa_threaded_mainloop_unlock(i
->mainloop
);
1428 case SOUND_MIXER_READ_RECSRC
:
1429 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_RECSRC\n");
1431 *(int*) argp
= SOUND_MASK_IGAIN
;
1434 case SOUND_MIXER_WRITE_RECSRC
:
1435 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_WRITE_RECSRC\n");
1438 case SOUND_MIXER_READ_CAPS
:
1439 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_CAPS\n");
1444 case SOUND_MIXER_READ_PCM
:
1445 case SOUND_MIXER_READ_IGAIN
: {
1448 if (request
== SOUND_MIXER_READ_PCM
)
1449 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_PCM\n");
1451 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_IGAIN\n");
1453 pa_threaded_mainloop_lock(i
->mainloop
);
1455 if (request
== SOUND_MIXER_READ_PCM
)
1456 v
= &i
->sink_volume
;
1458 v
= &i
->source_volume
;
1461 ((v
->values
[0]*100/PA_VOLUME_NORM
)) |
1462 ((v
->values
[v
->channels
> 1 ? 1 : 0]*100/PA_VOLUME_NORM
) << 8);
1464 pa_threaded_mainloop_unlock(i
->mainloop
);
1469 case SOUND_MIXER_WRITE_PCM
:
1470 case SOUND_MIXER_WRITE_IGAIN
: {
1473 if (request
== SOUND_MIXER_READ_PCM
)
1474 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_WRITE_PCM\n");
1476 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_WRITE_IGAIN\n");
1478 pa_threaded_mainloop_lock(i
->mainloop
);
1480 if (request
== SOUND_MIXER_READ_PCM
) {
1482 pv
= &i
->sink_volume
;
1484 v
= i
->source_volume
;
1485 pv
= &i
->source_volume
;
1488 pv
->values
[0] = ((*(int*) argp
& 0xFF)*PA_VOLUME_NORM
)/100;
1489 pv
->values
[1] = ((*(int*) argp
>> 8)*PA_VOLUME_NORM
)/100;
1491 if (!pa_cvolume_equal(pv
, &v
)) {
1494 if (request
== SOUND_MIXER_READ_PCM
)
1495 o
= pa_context_set_sink_volume_by_index(i
->context
, i
->sink_index
, pv
, context_success_cb
, i
);
1497 o
= pa_context_set_source_volume_by_index(i
->context
, i
->source_index
, pv
, context_success_cb
, i
);
1500 debug(DEBUG_LEVEL_NORMAL
, __FILE__
":Failed set volume: %s", pa_strerror(pa_context_errno(i
->context
)));
1503 i
->operation_success
= 0;
1504 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1505 CONTEXT_CHECK_DEAD_GOTO(i
, exit_loop
);
1507 pa_threaded_mainloop_wait(i
->mainloop
);
1511 if (!i
->operation_success
)
1512 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to set volume: %s\n", pa_strerror(pa_context_errno(i
->context
)));
1514 pa_operation_unref(o
);
1517 /* We don't wait for completion here */
1518 i
->volume_modify_count
++;
1521 pa_threaded_mainloop_unlock(i
->mainloop
);
1526 case SOUND_MIXER_INFO
: {
1527 mixer_info
*mi
= argp
;
1529 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_INFO\n");
1531 memset(mi
, 0, sizeof(mixer_info
));
1532 strncpy(mi
->id
, "PULSEAUDIO", sizeof(mi
->id
));
1533 strncpy(mi
->name
, "PulseAudio Virtual OSS", sizeof(mi
->name
));
1534 pa_threaded_mainloop_lock(i
->mainloop
);
1535 mi
->modify_counter
= i
->volume_modify_count
;
1536 pa_threaded_mainloop_unlock(i
->mainloop
);
1541 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": unknown ioctl 0x%08lx\n", request
);
1554 static int map_format(int *fmt
, pa_sample_spec
*ss
) {
1558 ss
->format
= PA_SAMPLE_ULAW
;
1562 ss
->format
= PA_SAMPLE_ALAW
;
1569 ss
->format
= PA_SAMPLE_U8
;
1576 ss
->format
= PA_SAMPLE_S16BE
;
1583 ss
->format
= PA_SAMPLE_S16LE
;
1587 ss
->format
= PA_SAMPLE_S16NE
;
1595 static int map_format_back(pa_sample_format_t format
) {
1597 case PA_SAMPLE_S16LE
: return AFMT_S16_LE
;
1598 case PA_SAMPLE_S16BE
: return AFMT_S16_BE
;
1599 case PA_SAMPLE_ULAW
: return AFMT_MU_LAW
;
1600 case PA_SAMPLE_ALAW
: return AFMT_A_LAW
;
1601 case PA_SAMPLE_U8
: return AFMT_U8
;
1607 static int dsp_flush_fd(int fd
) {
1611 if (ioctl(fd
, SIOCINQ
, &l
) < 0) {
1612 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SIOCINQ: %s\n", strerror(errno
));
1620 k
= (size_t) l
> sizeof(buf
) ? sizeof(buf
) : (size_t) l
;
1621 if (read(fd
, buf
, k
) < 0)
1622 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": read(): %s\n", strerror(errno
));
1628 # warning "Your platform does not support SIOCINQ, something might not work as intended."
1633 static int dsp_flush_socket(fd_info
*i
) {
1636 if ((i
->thread_fd
< 0) && (i
->app_fd
< 0))
1639 if (i
->thread_fd
>= 0)
1640 res
= dsp_flush_fd(i
->thread_fd
);
1646 res
= dsp_flush_fd(i
->app_fd
);
1654 static int dsp_empty_socket(fd_info
*i
) {
1658 /* Empty the socket */
1662 if (i
->thread_fd
< 0)
1665 if (ioctl(i
->thread_fd
, SIOCINQ
, &l
) < 0) {
1666 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SIOCINQ: %s\n", strerror(errno
));
1675 pa_threaded_mainloop_wait(i
->mainloop
);
1680 # warning "Your platform does not support SIOCINQ, something might not work as intended."
1685 static int dsp_drain(fd_info
*i
) {
1686 pa_operation
*o
= NULL
;
1692 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Draining.\n");
1694 pa_threaded_mainloop_lock(i
->mainloop
);
1696 if (dsp_empty_socket(i
) < 0)
1699 if (!i
->play_stream
)
1702 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Really draining.\n");
1704 if (!(o
= pa_stream_drain(i
->play_stream
, stream_success_cb
, i
))) {
1705 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_drain(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1709 i
->operation_success
= 0;
1710 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1711 PLAYBACK_STREAM_CHECK_DEAD_GOTO(i
, fail
);
1713 pa_threaded_mainloop_wait(i
->mainloop
);
1716 if (!i
->operation_success
) {
1717 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_drain() 2: %s\n", pa_strerror(pa_context_errno(i
->context
)));
1726 pa_operation_unref(o
);
1728 pa_threaded_mainloop_unlock(i
->mainloop
);
1733 static int dsp_trigger(fd_info
*i
) {
1734 pa_operation
*o
= NULL
;
1737 if (!i
->play_stream
)
1740 pa_threaded_mainloop_lock(i
->mainloop
);
1742 if (dsp_empty_socket(i
) < 0)
1745 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Triggering.\n");
1747 if (!(o
= pa_stream_trigger(i
->play_stream
, stream_success_cb
, i
))) {
1748 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1752 i
->operation_success
= 0;
1753 while (!pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1754 PLAYBACK_STREAM_CHECK_DEAD_GOTO(i
, fail
);
1756 pa_threaded_mainloop_wait(i
->mainloop
);
1759 if (!i
->operation_success
) {
1760 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1769 pa_operation_unref(o
);
1771 pa_threaded_mainloop_unlock(i
->mainloop
);
1776 static int dsp_ioctl(fd_info
*i
, unsigned long request
, void*argp
, int *_errno
) {
1780 case SNDCTL_DSP_SETFMT
: {
1781 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_SETFMT: %i\n", *(int*) argp
);
1783 pa_threaded_mainloop_lock(i
->mainloop
);
1785 if (*(int*) argp
== AFMT_QUERY
)
1786 *(int*) argp
= map_format_back(i
->sample_spec
.format
);
1788 map_format((int*) argp
, &i
->sample_spec
);
1792 pa_threaded_mainloop_unlock(i
->mainloop
);
1796 case SNDCTL_DSP_SPEED
: {
1801 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_SPEED: %i\n", *(int*) argp
);
1803 pa_threaded_mainloop_lock(i
->mainloop
);
1805 ss
= i
->sample_spec
;
1806 ss
.rate
= *(int*) argp
;
1808 if ((valid
= pa_sample_spec_valid(&ss
))) {
1809 i
->sample_spec
= ss
;
1813 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": ss: %s\n", pa_sample_spec_snprint(t
, sizeof(t
), &i
->sample_spec
));
1815 pa_threaded_mainloop_unlock(i
->mainloop
);
1825 case SNDCTL_DSP_STEREO
:
1826 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_STEREO: %i\n", *(int*) argp
);
1828 pa_threaded_mainloop_lock(i
->mainloop
);
1830 i
->sample_spec
.channels
= *(int*) argp
? 2 : 1;
1833 pa_threaded_mainloop_unlock(i
->mainloop
);
1836 case SNDCTL_DSP_CHANNELS
: {
1840 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_CHANNELS: %i\n", *(int*) argp
);
1842 pa_threaded_mainloop_lock(i
->mainloop
);
1844 ss
= i
->sample_spec
;
1845 ss
.channels
= *(int*) argp
;
1847 if ((valid
= pa_sample_spec_valid(&ss
))) {
1848 i
->sample_spec
= ss
;
1852 pa_threaded_mainloop_unlock(i
->mainloop
);
1862 case SNDCTL_DSP_GETBLKSIZE
:
1863 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_GETBLKSIZE\n");
1865 pa_threaded_mainloop_lock(i
->mainloop
);
1868 *(int*) argp
= i
->fragment_size
;
1870 pa_threaded_mainloop_unlock(i
->mainloop
);
1874 case SNDCTL_DSP_SETFRAGMENT
:
1875 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_SETFRAGMENT: 0x%8x\n", *(int*) argp
);
1877 pa_threaded_mainloop_lock(i
->mainloop
);
1879 i
->fragment_size
= 1 << (*(int*) argp
);
1880 i
->n_fragments
= (*(int*) argp
) >> 16;
1882 /* 0x7FFF means that we can set whatever we like */
1883 if (i
->n_fragments
== 0x7FFF)
1884 i
->n_fragments
= 12;
1888 pa_threaded_mainloop_unlock(i
->mainloop
);
1892 case SNDCTL_DSP_GETCAPS
:
1893 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_CAPS\n");
1895 *(int*) argp
= DSP_CAP_DUPLEX
1896 #ifdef DSP_CAP_MULTI
1902 case SNDCTL_DSP_GETODELAY
: {
1905 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_GETODELAY\n");
1907 pa_threaded_mainloop_lock(i
->mainloop
);
1914 PLAYBACK_STREAM_CHECK_DEAD_GOTO(i
, exit_loop
);
1916 if (pa_stream_get_latency(i
->play_stream
, &usec
, NULL
) >= 0) {
1917 *(int*) argp
= pa_usec_to_bytes(usec
, &i
->sample_spec
);
1921 if (pa_context_errno(i
->context
) != PA_ERR_NODATA
) {
1922 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_get_latency(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1926 pa_threaded_mainloop_wait(i
->mainloop
);
1932 if (ioctl(i
->thread_fd
, SIOCINQ
, &l
) < 0)
1933 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SIOCINQ failed: %s\n", strerror(errno
));
1937 # warning "Your platform does not support SIOCINQ, something might not work as intended."
1940 pa_threaded_mainloop_unlock(i
->mainloop
);
1942 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": ODELAY: %i\n", *(int*) argp
);
1947 case SNDCTL_DSP_RESET
: {
1948 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_RESET\n");
1950 pa_threaded_mainloop_lock(i
->mainloop
);
1953 dsp_flush_socket(i
);
1956 pa_threaded_mainloop_unlock(i
->mainloop
);
1960 case SNDCTL_DSP_GETFMTS
: {
1961 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_GETFMTS\n");
1963 *(int*) argp
= AFMT_MU_LAW
|AFMT_A_LAW
|AFMT_U8
|AFMT_S16_LE
|AFMT_S16_BE
;
1967 case SNDCTL_DSP_POST
:
1968 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_POST\n");
1970 if (dsp_trigger(i
) < 0)
1974 case SNDCTL_DSP_SYNC
:
1975 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_SYNC\n");
1977 if (dsp_drain(i
) < 0)
1982 case SNDCTL_DSP_GETOSPACE
:
1983 case SNDCTL_DSP_GETISPACE
: {
1984 audio_buf_info
*bi
= (audio_buf_info
*) argp
;
1988 if (request
== SNDCTL_DSP_GETOSPACE
)
1989 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_GETOSPACE\n");
1991 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_GETISPACE\n");
1993 pa_threaded_mainloop_lock(i
->mainloop
);
1997 if (request
== SNDCTL_DSP_GETOSPACE
) {
1998 if (i
->play_stream
) {
1999 if ((k
= pa_stream_writable_size(i
->play_stream
)) == (size_t) -1)
2000 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_writable_size(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
2002 k
= i
->fragment_size
* i
->n_fragments
;
2005 if (ioctl(i
->thread_fd
, SIOCINQ
, &l
) < 0) {
2006 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SIOCINQ failed: %s\n", strerror(errno
));
2010 # warning "Your platform does not dsp_flush_fd, something might not work as intended."
2013 bi
->bytes
= k
> (size_t) l
? k
- l
: 0;
2015 if (i
->rec_stream
) {
2016 if ((k
= pa_stream_readable_size(i
->rec_stream
)) == (size_t) -1)
2017 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_readable_size(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
2022 if (ioctl(i
->app_fd
, SIOCINQ
, &l
) < 0) {
2023 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SIOCINQ failed: %s\n", strerror(errno
));
2027 # warning "Your platform does not dsp_flush_fd, something might not work as intended."
2032 bi
->fragsize
= i
->fragment_size
;
2033 bi
->fragstotal
= i
->n_fragments
;
2034 bi
->fragments
= bi
->bytes
/ bi
->fragsize
;
2036 pa_threaded_mainloop_unlock(i
->mainloop
);
2038 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": fragsize=%i, fragstotal=%i, bytes=%i, fragments=%i\n", bi
->fragsize
, bi
->fragstotal
, bi
->bytes
, bi
->fragments
);
2043 case SNDCTL_DSP_GETIPTR
:
2044 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": invalid ioctl SNDCTL_DSP_GETIPTR\n");
2047 case SNDCTL_DSP_GETOPTR
:
2048 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": invalid ioctl SNDCTL_DSP_GETOPTR\n");
2052 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": unknown ioctl 0x%08lx\n", request
);
2066 int ioctl(int fd
, unsigned long request
, ...) {
2072 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": ioctl()\n");
2074 va_start(args
, request
);
2075 argp
= va_arg(args
, void *);
2078 if (!function_enter()) {
2080 return _ioctl(fd
, request
, argp
);
2083 if (!(i
= fd_info_find(fd
))) {
2086 return _ioctl(fd
, request
, argp
);
2089 if (i
->type
== FD_INFO_MIXER
)
2090 r
= mixer_ioctl(i
, request
, argp
, &_errno
);
2092 r
= dsp_ioctl(i
, request
, argp
, &_errno
);
2107 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": close()\n");
2109 if (!function_enter()) {
2114 if (!(i
= fd_info_find(fd
))) {
2120 fd_info_remove_from_list(i
);
2128 int access(const char *pathname
, int mode
) {
2131 /* Firefox needs this. See #27 */
2136 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": access(%s)\n", pathname
);
2138 if (strcmp(pathname
, "/dev/dsp") != 0 &&
2139 strcmp(pathname
, "/dev/adsp") != 0 &&
2140 strcmp(pathname
, "/dev/sndstat") != 0 &&
2141 strcmp(pathname
, "/dev/mixer") != 0) {
2143 return _access(pathname
, mode
);
2146 if (mode
& (W_OK
| X_OK
)) {
2147 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": access(%s, %x) = EACCESS\n", pathname
, mode
);
2152 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": access(%s, %x) = OK\n", pathname
, mode
);
2159 int open64(const char *filename
, int flags
, ...) {
2163 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": open64(%s)\n", filename
);
2165 va_start(args
, flags
);
2166 if (flags
& O_CREAT
)
2167 mode
= va_arg(args
, mode_t
);
2170 if (strcmp(filename
, "/dev/dsp") != 0 &&
2171 strcmp(filename
, "/dev/adsp") != 0 &&
2172 strcmp(filename
, "/dev/sndstat") != 0 &&
2173 strcmp(filename
, "/dev/mixer") != 0) {
2175 return _open64(filename
, flags
, mode
);
2178 return open(filename
, flags
, mode
);
2183 FILE* fopen(const char *filename
, const char *mode
) {
2188 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": fopen(%s)\n", filename
);
2190 if (strcmp(filename
, "/dev/dsp") != 0 &&
2191 strcmp(filename
, "/dev/adsp") != 0 &&
2192 strcmp(filename
, "/dev/sndstat") != 0 &&
2193 strcmp(filename
, "/dev/mixer") != 0) {
2195 return _fopen(filename
, mode
);
2211 if ((((mode
[1] == 'b') || (mode
[1] == 't')) && (mode
[2] == '+')) || (mode
[1] == '+'))
2214 if ((fd
= open(filename
, m
)) < 0)
2217 if (!(f
= fdopen(fd
, mode
))) {
2227 FILE *fopen64(const char *filename
, const char *mode
) {
2229 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": fopen64(%s)\n", filename
);
2231 if (strcmp(filename
, "/dev/dsp") != 0 &&
2232 strcmp(filename
, "/dev/adsp") != 0 &&
2233 strcmp(filename
, "/dev/sndstat") != 0 &&
2234 strcmp(filename
, "/dev/mixer") != 0) {
2235 LOAD_FOPEN64_FUNC();
2236 return _fopen64(filename
, mode
);
2239 return fopen(filename
, mode
);
2244 int fclose(FILE *f
) {
2247 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": fclose()\n");
2249 if (!function_enter()) {
2254 if (!(i
= fd_info_find(fileno(f
)))) {
2260 fd_info_remove_from_list(i
);
2262 /* Dirty trick to avoid that the fd is not freed twice, once by us
2263 * and once by the real fclose() */