4 This file is part of PulseAudio.
6 Copyright 2006 Lennart Poettering
7 Copyright 2006-2007 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
;
90 pa_io_event
*io_event
;
91 pa_io_event_flags_t io_flags
;
96 int operation_success
;
98 pa_cvolume sink_volume
, source_volume
;
99 uint32_t sink_index
, source_index
;
100 int volume_modify_count
;
104 PA_LLIST_FIELDS(fd_info
);
107 static int dsp_drain(fd_info
*i
);
108 static void fd_info_remove_from_list(fd_info
*i
);
110 static pthread_mutex_t fd_infos_mutex
= PTHREAD_MUTEX_INITIALIZER
;
111 static pthread_mutex_t func_mutex
= PTHREAD_MUTEX_INITIALIZER
;
113 static PA_LLIST_HEAD(fd_info
, fd_infos
) = NULL
;
115 static int (*_ioctl
)(int, int, void*) = NULL
;
116 static int (*_close
)(int) = NULL
;
117 static int (*_open
)(const char *, int, mode_t
) = NULL
;
118 static FILE* (*_fopen
)(const char *path
, const char *mode
) = NULL
;
120 static int (*_open64
)(const char *, int, mode_t
) = NULL
;
121 static FILE* (*_fopen64
)(const char *path
, const char *mode
) = NULL
;
123 static int (*_fclose
)(FILE *f
) = NULL
;
124 static int (*_access
)(const char *, int) = NULL
;
126 /* dlsym() violates ISO C, so confide the breakage into this function to
128 typedef void (*fnptr
)(void);
129 static inline fnptr
dlsym_fn(void *handle
, const char *symbol
) {
130 return (fnptr
) (long) dlsym(handle
, symbol
);
133 #define LOAD_IOCTL_FUNC() \
135 pthread_mutex_lock(&func_mutex); \
137 _ioctl = (int (*)(int, int, void*)) dlsym_fn(RTLD_NEXT, "ioctl"); \
138 pthread_mutex_unlock(&func_mutex); \
141 #define LOAD_OPEN_FUNC() \
143 pthread_mutex_lock(&func_mutex); \
145 _open = (int (*)(const char *, int, mode_t)) dlsym_fn(RTLD_NEXT, "open"); \
146 pthread_mutex_unlock(&func_mutex); \
149 #define LOAD_OPEN64_FUNC() \
151 pthread_mutex_lock(&func_mutex); \
153 _open64 = (int (*)(const char *, int, mode_t)) dlsym_fn(RTLD_NEXT, "open64"); \
154 pthread_mutex_unlock(&func_mutex); \
157 #define LOAD_CLOSE_FUNC() \
159 pthread_mutex_lock(&func_mutex); \
161 _close = (int (*)(int)) dlsym_fn(RTLD_NEXT, "close"); \
162 pthread_mutex_unlock(&func_mutex); \
165 #define LOAD_ACCESS_FUNC() \
167 pthread_mutex_lock(&func_mutex); \
169 _access = (int (*)(const char*, int)) dlsym_fn(RTLD_NEXT, "access"); \
170 pthread_mutex_unlock(&func_mutex); \
173 #define LOAD_FOPEN_FUNC() \
175 pthread_mutex_lock(&func_mutex); \
177 _fopen = (FILE* (*)(const char *, const char*)) dlsym_fn(RTLD_NEXT, "fopen"); \
178 pthread_mutex_unlock(&func_mutex); \
181 #define LOAD_FOPEN64_FUNC() \
183 pthread_mutex_lock(&func_mutex); \
185 _fopen64 = (FILE* (*)(const char *, const char*)) dlsym_fn(RTLD_NEXT, "fopen64"); \
186 pthread_mutex_unlock(&func_mutex); \
189 #define LOAD_FCLOSE_FUNC() \
191 pthread_mutex_lock(&func_mutex); \
193 _fclose = (int (*)(FILE *)) dlsym_fn(RTLD_NEXT, "fclose"); \
194 pthread_mutex_unlock(&func_mutex); \
197 #define CONTEXT_CHECK_DEAD_GOTO(i, label) do { \
198 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY) { \
199 debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s\n", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
204 #define PLAYBACK_STREAM_CHECK_DEAD_GOTO(i, label) do { \
205 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY || \
206 !(i)->play_stream || pa_stream_get_state((i)->play_stream) != PA_STREAM_READY) { \
207 debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s\n", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
212 #define RECORD_STREAM_CHECK_DEAD_GOTO(i, label) do { \
213 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY || \
214 !(i)->rec_stream || pa_stream_get_state((i)->rec_stream) != PA_STREAM_READY) { \
215 debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s\n", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
220 static void debug(int level
, const char *format
, ...) PA_GCC_PRINTF_ATTR(2,3);
222 #define DEBUG_LEVEL_ALWAYS 0
223 #define DEBUG_LEVEL_NORMAL 1
224 #define DEBUG_LEVEL_VERBOSE 2
226 static void debug(int level
, const char *format
, ...) {
228 const char *dlevel_s
;
231 dlevel_s
= getenv("PADSP_DEBUG");
235 dlevel
= atoi(dlevel_s
);
240 va_start(ap
, format
);
241 vfprintf(stderr
, format
, ap
);
245 static int padsp_disabled(void) {
247 static int sym_resolved
= 0;
249 /* If the current process has a symbol __padsp_disabled__ we use
250 * it to detect whether we should enable our stuff or not. A
251 * program needs to be compiled with -rdynamic for this to work!
252 * The symbol must be an int containing a three bit bitmask: bit 1
253 * -> disable /dev/dsp emulation, bit 2 -> disable /dev/sndstat
254 * emulation, bit 3 -> disable /dev/mixer emulation. Hence a value
255 * of 7 disables padsp entirely. */
257 pthread_mutex_lock(&func_mutex
);
259 sym
= (int*) dlsym(RTLD_DEFAULT
, "__padsp_disabled__");
263 pthread_mutex_unlock(&func_mutex
);
271 static int dsp_cloak_enable(void) {
272 if (padsp_disabled() & 1)
275 if (getenv("PADSP_NO_DSP"))
281 static int sndstat_cloak_enable(void) {
282 if (padsp_disabled() & 2)
285 if (getenv("PADSP_NO_SNDSTAT"))
291 static int mixer_cloak_enable(void) {
292 if (padsp_disabled() & 4)
295 if (getenv("PADSP_NO_MIXER"))
300 static pthread_key_t recursion_key
;
302 static void recursion_key_alloc(void) {
303 pthread_key_create(&recursion_key
, NULL
);
306 static int function_enter(void) {
307 /* Avoid recursive calls */
308 static pthread_once_t recursion_key_once
= PTHREAD_ONCE_INIT
;
309 pthread_once(&recursion_key_once
, recursion_key_alloc
);
311 if (pthread_getspecific(recursion_key
))
314 pthread_setspecific(recursion_key
, (void*) 1);
318 static void function_exit(void) {
319 pthread_setspecific(recursion_key
, NULL
);
322 static void fd_info_free(fd_info
*i
) {
325 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": freeing fd info (fd=%i)\n", i
->app_fd
);
330 pa_threaded_mainloop_stop(i
->mainloop
);
332 if (i
->play_stream
) {
333 pa_stream_disconnect(i
->play_stream
);
334 pa_stream_unref(i
->play_stream
);
338 pa_stream_disconnect(i
->rec_stream
);
339 pa_stream_unref(i
->rec_stream
);
343 pa_context_disconnect(i
->context
);
344 pa_context_unref(i
->context
);
348 pa_threaded_mainloop_free(i
->mainloop
);
350 if (i
->app_fd
>= 0) {
355 if (i
->thread_fd
>= 0) {
357 _close(i
->thread_fd
);
362 pthread_mutex_destroy(&i
->mutex
);
366 static fd_info
*fd_info_ref(fd_info
*i
) {
369 pthread_mutex_lock(&i
->mutex
);
373 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": ref++, now %i\n", i
->ref
);
374 pthread_mutex_unlock(&i
->mutex
);
379 static void fd_info_unref(fd_info
*i
) {
381 pthread_mutex_lock(&i
->mutex
);
384 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": ref--, now %i\n", i
->ref
);
385 pthread_mutex_unlock(&i
->mutex
);
391 static void context_state_cb(pa_context
*c
, void *userdata
) {
392 fd_info
*i
= userdata
;
395 switch (pa_context_get_state(c
)) {
396 case PA_CONTEXT_READY
:
397 case PA_CONTEXT_TERMINATED
:
398 case PA_CONTEXT_FAILED
:
399 pa_threaded_mainloop_signal(i
->mainloop
, 0);
402 case PA_CONTEXT_UNCONNECTED
:
403 case PA_CONTEXT_CONNECTING
:
404 case PA_CONTEXT_AUTHORIZING
:
405 case PA_CONTEXT_SETTING_NAME
:
410 static void reset_params(fd_info
*i
) {
413 i
->sample_spec
.format
= PA_SAMPLE_U8
;
414 i
->sample_spec
.channels
= 1;
415 i
->sample_spec
.rate
= 8000;
416 i
->fragment_size
= 0;
420 static const char *client_name(char *buf
, size_t n
) {
424 if ((e
= getenv("PADSP_CLIENT_NAME")))
427 if (pa_get_binary_name(p
, sizeof(p
)))
428 snprintf(buf
, n
, "OSS Emulation[%s]", p
);
430 snprintf(buf
, n
, "OSS");
435 static const char *stream_name(void) {
438 if ((e
= getenv("PADSP_STREAM_NAME")))
441 return "Audio Stream";
444 static void atfork_prepare(void) {
447 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": atfork_prepare() enter\n");
451 pthread_mutex_lock(&fd_infos_mutex
);
453 for (i
= fd_infos
; i
; i
= i
->next
) {
454 pthread_mutex_lock(&i
->mutex
);
455 pa_threaded_mainloop_lock(i
->mainloop
);
458 pthread_mutex_lock(&func_mutex
);
461 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": atfork_prepare() exit\n");
464 static void atfork_parent(void) {
467 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": atfork_parent() enter\n");
469 pthread_mutex_unlock(&func_mutex
);
471 for (i
= fd_infos
; i
; i
= i
->next
) {
472 pa_threaded_mainloop_unlock(i
->mainloop
);
473 pthread_mutex_unlock(&i
->mutex
);
476 pthread_mutex_unlock(&fd_infos_mutex
);
480 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": atfork_parent() exit\n");
483 static void atfork_child(void) {
486 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": atfork_child() enter\n");
488 /* We do only the bare minimum to get all fds closed */
489 pthread_mutex_init(&func_mutex
, NULL
);
490 pthread_mutex_init(&fd_infos_mutex
, NULL
);
492 for (i
= fd_infos
; i
; i
= i
->next
) {
493 pthread_mutex_init(&i
->mutex
, NULL
);
496 pa_context_disconnect(i
->context
);
497 pa_context_unref(i
->context
);
501 if (i
->play_stream
) {
502 pa_stream_unref(i
->play_stream
);
503 i
->play_stream
= NULL
;
507 pa_stream_unref(i
->rec_stream
);
508 i
->rec_stream
= NULL
;
511 if (i
->app_fd
>= 0) {
516 if (i
->thread_fd
>= 0) {
526 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": atfork_child() exit\n");
529 static void install_atfork(void) {
530 pthread_atfork(atfork_prepare
, atfork_parent
, atfork_child
);
533 static void stream_success_cb(pa_stream
*s
, int success
, void *userdata
) {
534 fd_info
*i
= userdata
;
539 i
->operation_success
= success
;
540 pa_threaded_mainloop_signal(i
->mainloop
, 0);
543 static void context_success_cb(pa_context
*c
, int success
, void *userdata
) {
544 fd_info
*i
= userdata
;
549 i
->operation_success
= success
;
550 pa_threaded_mainloop_signal(i
->mainloop
, 0);
553 static fd_info
* fd_info_new(fd_info_type_t type
, int *_errno
) {
555 int sfds
[2] = { -1, -1 };
557 static pthread_once_t install_atfork_once
= PTHREAD_ONCE_INIT
;
559 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": fd_info_new()\n");
561 signal(SIGPIPE
, SIG_IGN
); /* Yes, ugly as hell */
563 pthread_once(&install_atfork_once
, install_atfork
);
565 if (!(i
= malloc(sizeof(fd_info
)))) {
570 i
->app_fd
= i
->thread_fd
= -1;
575 i
->play_stream
= NULL
;
576 i
->rec_stream
= NULL
;
581 pthread_mutex_init(&i
->mutex
, NULL
);
586 pa_cvolume_reset(&i
->sink_volume
, 2);
587 pa_cvolume_reset(&i
->source_volume
, 2);
588 i
->volume_modify_count
= 0;
589 i
->sink_index
= (uint32_t) -1;
590 i
->source_index
= (uint32_t) -1;
591 i
->optr_n_blocks
= 0;
592 PA_LLIST_INIT(fd_info
, i
);
596 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, sfds
) < 0) {
598 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": socket() failed: %s\n", strerror(errno
));
603 i
->thread_fd
= sfds
[1];
605 if (!(i
->mainloop
= pa_threaded_mainloop_new())) {
607 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_threaded_mainloop_new() failed\n");
611 if (!(i
->context
= pa_context_new(pa_threaded_mainloop_get_api(i
->mainloop
), client_name(name
, sizeof(name
))))) {
613 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_context_new() failed\n");
617 pa_context_set_state_callback(i
->context
, context_state_cb
, i
);
619 if (pa_context_connect(i
->context
, NULL
, 0, NULL
) < 0) {
620 *_errno
= ECONNREFUSED
;
621 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
625 pa_threaded_mainloop_lock(i
->mainloop
);
627 if (pa_threaded_mainloop_start(i
->mainloop
) < 0) {
629 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_threaded_mainloop_start() failed\n");
630 goto unlock_and_fail
;
633 /* Wait until the context is ready */
634 pa_threaded_mainloop_wait(i
->mainloop
);
636 if (pa_context_get_state(i
->context
) != PA_CONTEXT_READY
) {
637 *_errno
= ECONNREFUSED
;
638 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
639 goto unlock_and_fail
;
642 pa_threaded_mainloop_unlock(i
->mainloop
);
647 pa_threaded_mainloop_unlock(i
->mainloop
);
657 static void fd_info_add_to_list(fd_info
*i
) {
660 pthread_mutex_lock(&fd_infos_mutex
);
661 PA_LLIST_PREPEND(fd_info
, fd_infos
, i
);
662 pthread_mutex_unlock(&fd_infos_mutex
);
667 static void fd_info_remove_from_list(fd_info
*i
) {
670 pthread_mutex_lock(&fd_infos_mutex
);
671 PA_LLIST_REMOVE(fd_info
, fd_infos
, i
);
672 pthread_mutex_unlock(&fd_infos_mutex
);
677 static fd_info
* fd_info_find(int fd
) {
680 pthread_mutex_lock(&fd_infos_mutex
);
682 for (i
= fd_infos
; i
; i
= i
->next
)
683 if (i
->app_fd
== fd
&& !i
->unusable
) {
688 pthread_mutex_unlock(&fd_infos_mutex
);
693 static void fix_metrics(fd_info
*i
) {
695 char t
[PA_SAMPLE_SPEC_SNPRINT_MAX
];
697 fs
= pa_frame_size(&i
->sample_spec
);
699 /* Don't fix things more than necessary */
700 if ((i
->fragment_size
% fs
) == 0 &&
701 i
->n_fragments
>= 2 &&
702 i
->fragment_size
> 0)
705 i
->fragment_size
= (i
->fragment_size
/fs
)*fs
;
707 /* Number of fragments set? */
708 if (i
->n_fragments
< 2) {
709 if (i
->fragment_size
> 0) {
710 i
->n_fragments
= pa_bytes_per_second(&i
->sample_spec
) / 2 / i
->fragment_size
;
711 if (i
->n_fragments
< 2)
717 /* Fragment size set? */
718 if (i
->fragment_size
<= 0) {
719 i
->fragment_size
= pa_bytes_per_second(&i
->sample_spec
) / 2 / i
->n_fragments
;
720 if (i
->fragment_size
< 1024)
721 i
->fragment_size
= 1024;
724 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": sample spec: %s\n", pa_sample_spec_snprint(t
, sizeof(t
), &i
->sample_spec
));
725 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": fixated metrics to %i fragments, %li bytes each.\n", i
->n_fragments
, (long)i
->fragment_size
);
728 static void stream_request_cb(pa_stream
*s
, size_t length
, void *userdata
) {
729 fd_info
*i
= userdata
;
733 pa_mainloop_api
*api
;
736 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
738 if (s
== i
->play_stream
) {
739 n
= pa_stream_writable_size(i
->play_stream
);
740 if (n
== (size_t)-1) {
741 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_writable_size(): %s\n",
742 pa_strerror(pa_context_errno(i
->context
)));
745 if (n
>= i
->fragment_size
)
746 i
->io_flags
|= PA_IO_EVENT_INPUT
;
748 i
->io_flags
&= ~PA_IO_EVENT_INPUT
;
751 if (s
== i
->rec_stream
) {
752 n
= pa_stream_readable_size(i
->rec_stream
);
753 if (n
== (size_t)-1) {
754 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_readable_size(): %s\n",
755 pa_strerror(pa_context_errno(i
->context
)));
758 if (n
>= i
->fragment_size
)
759 i
->io_flags
|= PA_IO_EVENT_OUTPUT
;
761 i
->io_flags
&= ~PA_IO_EVENT_OUTPUT
;
764 api
->io_enable(i
->io_event
, i
->io_flags
);
768 static void stream_latency_update_cb(pa_stream
*s
, void *userdata
) {
769 fd_info
*i
= userdata
;
772 pa_threaded_mainloop_signal(i
->mainloop
, 0);
775 static void fd_info_shutdown(fd_info
*i
) {
779 pa_mainloop_api
*api
;
780 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
781 api
->io_free(i
->io_event
);
786 if (i
->thread_fd
>= 0) {
792 static int fd_info_copy_data(fd_info
*i
, int force
) {
795 if (!i
->play_stream
&& !i
->rec_stream
)
798 if ((i
->play_stream
) && (pa_stream_get_state(i
->play_stream
) == PA_STREAM_READY
)) {
799 n
= pa_stream_writable_size(i
->play_stream
);
801 if (n
== (size_t)-1) {
802 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_writable_size(): %s\n",
803 pa_strerror(pa_context_errno(i
->context
)));
807 while (n
>= i
->fragment_size
|| force
) {
811 if (!(i
->buf
= malloc(i
->fragment_size
))) {
812 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": malloc() failed.\n");
817 if ((r
= read(i
->thread_fd
, i
->buf
, i
->fragment_size
)) <= 0) {
822 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": read(): %s\n", r
== 0 ? "EOF" : strerror(errno
));
826 if (pa_stream_write(i
->play_stream
, i
->buf
, r
, free
, 0, PA_SEEK_RELATIVE
) < 0) {
827 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_write(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
833 assert(n
>= (size_t) r
);
837 if (n
>= i
->fragment_size
)
838 i
->io_flags
|= PA_IO_EVENT_INPUT
;
840 i
->io_flags
&= ~PA_IO_EVENT_INPUT
;
843 if ((i
->rec_stream
) && (pa_stream_get_state(i
->rec_stream
) == PA_STREAM_READY
)) {
844 n
= pa_stream_readable_size(i
->rec_stream
);
846 if (n
== (size_t)-1) {
847 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_readable_size(): %s\n",
848 pa_strerror(pa_context_errno(i
->context
)));
852 while (n
>= i
->fragment_size
|| force
) {
858 if (pa_stream_peek(i
->rec_stream
, &data
, &len
) < 0) {
859 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_peek(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
866 buf
= (const char*)data
+ i
->rec_offset
;
868 if ((r
= write(i
->thread_fd
, buf
, len
- i
->rec_offset
)) <= 0) {
873 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": write(): %s\n", strerror(errno
));
877 assert((size_t)r
<= len
- i
->rec_offset
);
880 if (i
->rec_offset
== len
) {
881 if (pa_stream_drop(i
->rec_stream
) < 0) {
882 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_drop(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
888 assert(n
>= (size_t) r
);
892 if (n
>= i
->fragment_size
)
893 i
->io_flags
|= PA_IO_EVENT_OUTPUT
;
895 i
->io_flags
&= ~PA_IO_EVENT_OUTPUT
;
899 pa_mainloop_api
*api
;
901 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
902 api
->io_enable(i
->io_event
, i
->io_flags
);
908 static void stream_state_cb(pa_stream
*s
, void * userdata
) {
909 fd_info
*i
= userdata
;
912 switch (pa_stream_get_state(s
)) {
914 case PA_STREAM_READY
:
915 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": stream established.\n");
918 case PA_STREAM_FAILED
:
919 if (s
== i
->play_stream
) {
920 debug(DEBUG_LEVEL_NORMAL
,
921 __FILE__
": pa_stream_connect_playback() failed: %s\n",
922 pa_strerror(pa_context_errno(i
->context
)));
923 pa_stream_unref(i
->play_stream
);
924 i
->play_stream
= NULL
;
925 } else if (s
== i
->rec_stream
) {
926 debug(DEBUG_LEVEL_NORMAL
,
927 __FILE__
": pa_stream_connect_record() failed: %s\n",
928 pa_strerror(pa_context_errno(i
->context
)));
929 pa_stream_unref(i
->rec_stream
);
930 i
->rec_stream
= NULL
;
935 case PA_STREAM_TERMINATED
:
936 case PA_STREAM_UNCONNECTED
:
937 case PA_STREAM_CREATING
:
942 static int create_playback_stream(fd_info
*i
) {
950 if (!(i
->play_stream
= pa_stream_new(i
->context
, stream_name(), &i
->sample_spec
, NULL
))) {
951 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
955 pa_stream_set_state_callback(i
->play_stream
, stream_state_cb
, i
);
956 pa_stream_set_write_callback(i
->play_stream
, stream_request_cb
, i
);
957 pa_stream_set_latency_update_callback(i
->play_stream
, stream_latency_update_cb
, i
);
959 memset(&attr
, 0, sizeof(attr
));
960 attr
.maxlength
= i
->fragment_size
* (i
->n_fragments
+1);
961 attr
.tlength
= i
->fragment_size
* i
->n_fragments
;
962 attr
.prebuf
= i
->fragment_size
;
963 attr
.minreq
= i
->fragment_size
;
965 flags
= PA_STREAM_INTERPOLATE_TIMING
|PA_STREAM_AUTO_TIMING_UPDATE
;
966 if (i
->play_precork
) {
967 flags
|= PA_STREAM_START_CORKED
;
968 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": creating stream corked\n");
970 if (pa_stream_connect_playback(i
->play_stream
, NULL
, &attr
, flags
, NULL
, NULL
) < 0) {
971 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
975 n
= i
->fragment_size
;
976 setsockopt(i
->app_fd
, SOL_SOCKET
, SO_SNDBUF
, &n
, sizeof(n
));
977 n
= i
->fragment_size
;
978 setsockopt(i
->thread_fd
, SOL_SOCKET
, SO_RCVBUF
, &n
, sizeof(n
));
986 static int create_record_stream(fd_info
*i
) {
994 if (!(i
->rec_stream
= pa_stream_new(i
->context
, stream_name(), &i
->sample_spec
, NULL
))) {
995 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
999 pa_stream_set_state_callback(i
->rec_stream
, stream_state_cb
, i
);
1000 pa_stream_set_read_callback(i
->rec_stream
, stream_request_cb
, i
);
1001 pa_stream_set_latency_update_callback(i
->rec_stream
, stream_latency_update_cb
, i
);
1003 memset(&attr
, 0, sizeof(attr
));
1004 attr
.maxlength
= i
->fragment_size
* (i
->n_fragments
+1);
1005 attr
.fragsize
= i
->fragment_size
;
1007 flags
= PA_STREAM_INTERPOLATE_TIMING
|PA_STREAM_AUTO_TIMING_UPDATE
;
1008 if (i
->rec_precork
) {
1009 flags
|= PA_STREAM_START_CORKED
;
1010 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": creating stream corked\n");
1012 if (pa_stream_connect_record(i
->rec_stream
, NULL
, &attr
, flags
) < 0) {
1013 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_connect_record() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
1017 n
= i
->fragment_size
;
1018 setsockopt(i
->app_fd
, SOL_SOCKET
, SO_RCVBUF
, &n
, sizeof(n
));
1019 n
= i
->fragment_size
;
1020 setsockopt(i
->thread_fd
, SOL_SOCKET
, SO_SNDBUF
, &n
, sizeof(n
));
1028 static void free_streams(fd_info
*i
) {
1031 if (i
->play_stream
) {
1032 pa_stream_disconnect(i
->play_stream
);
1033 pa_stream_unref(i
->play_stream
);
1034 i
->play_stream
= NULL
;
1035 i
->io_flags
|= PA_IO_EVENT_INPUT
;
1038 if (i
->rec_stream
) {
1039 pa_stream_disconnect(i
->rec_stream
);
1040 pa_stream_unref(i
->rec_stream
);
1041 i
->rec_stream
= NULL
;
1042 i
->io_flags
|= PA_IO_EVENT_OUTPUT
;
1046 pa_mainloop_api
*api
;
1048 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
1049 api
->io_enable(i
->io_event
, i
->io_flags
);
1053 static void io_event_cb(pa_mainloop_api
*api
, pa_io_event
*e
, int fd
, pa_io_event_flags_t flags
, void *userdata
) {
1054 fd_info
*i
= userdata
;
1056 pa_threaded_mainloop_signal(i
->mainloop
, 0);
1058 if (flags
& PA_IO_EVENT_INPUT
) {
1060 if (!i
->play_stream
) {
1061 if (create_playback_stream(i
) < 0)
1064 if (fd_info_copy_data(i
, 0) < 0)
1068 } else if (flags
& PA_IO_EVENT_OUTPUT
) {
1070 if (!i
->rec_stream
) {
1071 if (create_record_stream(i
) < 0)
1074 if (fd_info_copy_data(i
, 0) < 0)
1078 } else if (flags
& (PA_IO_EVENT_HANGUP
|PA_IO_EVENT_ERROR
))
1084 /* We can't do anything better than removing the event source */
1085 fd_info_shutdown(i
);
1088 static int dsp_open(int flags
, int *_errno
) {
1090 pa_mainloop_api
*api
;
1094 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": dsp_open()\n");
1096 if (!(i
= fd_info_new(FD_INFO_STREAM
, _errno
)))
1099 if ((flags
& O_NONBLOCK
) == O_NONBLOCK
) {
1100 if ((f
= fcntl(i
->app_fd
, F_GETFL
)) >= 0)
1101 fcntl(i
->app_fd
, F_SETFL
, f
|O_NONBLOCK
);
1103 if ((f
= fcntl(i
->thread_fd
, F_GETFL
)) >= 0)
1104 fcntl(i
->thread_fd
, F_SETFL
, f
|O_NONBLOCK
);
1106 fcntl(i
->app_fd
, F_SETFD
, FD_CLOEXEC
);
1107 fcntl(i
->thread_fd
, F_SETFD
, FD_CLOEXEC
);
1109 pa_threaded_mainloop_lock(i
->mainloop
);
1110 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
1112 switch (flags
& O_ACCMODE
) {
1114 i
->io_flags
= PA_IO_EVENT_OUTPUT
;
1115 shutdown(i
->thread_fd
, SHUT_RD
);
1116 shutdown(i
->app_fd
, SHUT_WR
);
1119 i
->io_flags
= PA_IO_EVENT_INPUT
;
1120 shutdown(i
->thread_fd
, SHUT_WR
);
1121 shutdown(i
->app_fd
, SHUT_RD
);
1124 i
->io_flags
= PA_IO_EVENT_INPUT
| PA_IO_EVENT_OUTPUT
;
1130 if (!(i
->io_event
= api
->io_new(api
, i
->thread_fd
, i
->io_flags
, io_event_cb
, i
)))
1133 pa_threaded_mainloop_unlock(i
->mainloop
);
1135 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": dsp_open() succeeded, fd=%i\n", i
->app_fd
);
1137 fd_info_add_to_list(i
);
1144 pa_threaded_mainloop_unlock(i
->mainloop
);
1151 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": dsp_open() failed\n");
1156 static void sink_info_cb(pa_context
*context
, const pa_sink_info
*si
, int eol
, void *userdata
) {
1157 fd_info
*i
= userdata
;
1159 if (!si
&& eol
< 0) {
1160 i
->operation_success
= 0;
1161 pa_threaded_mainloop_signal(i
->mainloop
, 0);
1168 if (!pa_cvolume_equal(&i
->sink_volume
, &si
->volume
))
1169 i
->volume_modify_count
++;
1171 i
->sink_volume
= si
->volume
;
1172 i
->sink_index
= si
->index
;
1174 i
->operation_success
= 1;
1175 pa_threaded_mainloop_signal(i
->mainloop
, 0);
1178 static void source_info_cb(pa_context
*context
, const pa_source_info
*si
, int eol
, void *userdata
) {
1179 fd_info
*i
= userdata
;
1181 if (!si
&& eol
< 0) {
1182 i
->operation_success
= 0;
1183 pa_threaded_mainloop_signal(i
->mainloop
, 0);
1190 if (!pa_cvolume_equal(&i
->source_volume
, &si
->volume
))
1191 i
->volume_modify_count
++;
1193 i
->source_volume
= si
->volume
;
1194 i
->source_index
= si
->index
;
1196 i
->operation_success
= 1;
1197 pa_threaded_mainloop_signal(i
->mainloop
, 0);
1200 static void subscribe_cb(pa_context
*context
, pa_subscription_event_type_t t
, uint32_t idx
, void *userdata
) {
1201 fd_info
*i
= userdata
;
1202 pa_operation
*o
= NULL
;
1204 if (i
->sink_index
!= idx
)
1207 if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) != PA_SUBSCRIPTION_EVENT_CHANGE
)
1210 if (!(o
= pa_context_get_sink_info_by_index(i
->context
, i
->sink_index
, sink_info_cb
, i
))) {
1211 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to get sink info: %s", pa_strerror(pa_context_errno(i
->context
)));
1215 pa_operation_unref(o
);
1218 static int mixer_open(int flags
, int *_errno
) {
1220 pa_operation
*o
= NULL
;
1223 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": mixer_open()\n");
1225 if (!(i
= fd_info_new(FD_INFO_MIXER
, _errno
)))
1228 pa_threaded_mainloop_lock(i
->mainloop
);
1230 pa_context_set_subscribe_callback(i
->context
, subscribe_cb
, i
);
1232 if (!(o
= pa_context_subscribe(i
->context
, PA_SUBSCRIPTION_MASK_SINK
| PA_SUBSCRIPTION_MASK_SOURCE
, context_success_cb
, i
))) {
1233 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to subscribe to events: %s", pa_strerror(pa_context_errno(i
->context
)));
1238 i
->operation_success
= 0;
1239 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1240 pa_threaded_mainloop_wait(i
->mainloop
);
1241 CONTEXT_CHECK_DEAD_GOTO(i
, fail
);
1244 pa_operation_unref(o
);
1247 if (!i
->operation_success
) {
1248 debug(DEBUG_LEVEL_NORMAL
, __FILE__
":Failed to subscribe to events: %s", pa_strerror(pa_context_errno(i
->context
)));
1255 if (!(o
= pa_context_get_sink_info_by_name(i
->context
, NULL
, sink_info_cb
, i
))) {
1256 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to get sink info: %s", pa_strerror(pa_context_errno(i
->context
)));
1261 i
->operation_success
= 0;
1262 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1263 pa_threaded_mainloop_wait(i
->mainloop
);
1264 CONTEXT_CHECK_DEAD_GOTO(i
, fail
);
1267 pa_operation_unref(o
);
1270 if (!i
->operation_success
) {
1271 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to get sink info: %s", pa_strerror(pa_context_errno(i
->context
)));
1276 /* Get source info */
1278 if (!(o
= pa_context_get_source_info_by_name(i
->context
, NULL
, source_info_cb
, i
))) {
1279 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to get source info: %s", pa_strerror(pa_context_errno(i
->context
)));
1284 i
->operation_success
= 0;
1285 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1286 pa_threaded_mainloop_wait(i
->mainloop
);
1287 CONTEXT_CHECK_DEAD_GOTO(i
, fail
);
1290 pa_operation_unref(o
);
1293 if (!i
->operation_success
) {
1294 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to get source info: %s", pa_strerror(pa_context_errno(i
->context
)));
1299 pa_threaded_mainloop_unlock(i
->mainloop
);
1301 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": mixer_open() succeeded, fd=%i\n", i
->app_fd
);
1303 fd_info_add_to_list(i
);
1311 pa_operation_unref(o
);
1313 pa_threaded_mainloop_unlock(i
->mainloop
);
1320 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": mixer_open() failed\n");
1325 static int sndstat_open(int flags
, int *_errno
) {
1326 static const char sndstat
[] =
1327 "Sound Driver:3.8.1a-980706 (PulseAudio Virtual OSS)\n"
1329 "Config options: 0\n"
1331 "Installed drivers:\n"
1332 "Type 255: PulseAudio Virtual OSS\n"
1335 "PulseAudio Virtual OSS\n"
1338 "0: PulseAudio Virtual OSS\n"
1340 "Synth devices: NOT ENABLED IN CONFIG\n"
1347 "0: PulseAudio Virtual OSS\n";
1349 char fn
[] = "/tmp/padsp-sndstat-XXXXXX";
1354 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": sndstat_open()\n");
1356 if (flags
!= O_RDONLY
1358 && flags
!= (O_RDONLY
|O_LARGEFILE
)
1362 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": bad access!\n");
1373 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": mkstemp() failed: %s\n", strerror(errno
));
1379 if (write(fd
, sndstat
, sizeof(sndstat
) -1) != sizeof(sndstat
)-1) {
1381 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": write() failed: %s\n", strerror(errno
));
1385 if (lseek(fd
, SEEK_SET
, 0) < 0) {
1387 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": lseek() failed: %s\n", strerror(errno
));
1399 int open(const char *filename
, int flags
, ...) {
1404 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": open(%s)\n", filename
);
1406 va_start(args
, flags
);
1407 if (flags
& O_CREAT
) {
1408 if (sizeof(mode_t
) < sizeof(int))
1409 mode
= va_arg(args
, int);
1411 mode
= va_arg(args
, mode_t
);
1415 if (!function_enter()) {
1417 return _open(filename
, flags
, mode
);
1420 if (dsp_cloak_enable() && (strcmp(filename
, "/dev/dsp") == 0 || strcmp(filename
, "/dev/adsp") == 0)) {
1421 r
= dsp_open(flags
, &_errno
);
1422 } else if (mixer_cloak_enable() && strcmp(filename
, "/dev/mixer") == 0) {
1423 r
= mixer_open(flags
, &_errno
);
1424 } else if (sndstat_cloak_enable() && strcmp(filename
, "/dev/sndstat") == 0) {
1425 r
= sndstat_open(flags
, &_errno
);
1429 return _open(filename
, flags
, mode
);
1440 static int mixer_ioctl(fd_info
*i
, unsigned long request
, void*argp
, int *_errno
) {
1444 case SOUND_MIXER_READ_DEVMASK
:
1445 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_DEVMASK\n");
1447 *(int*) argp
= SOUND_MASK_PCM
| SOUND_MASK_IGAIN
;
1450 case SOUND_MIXER_READ_RECMASK
:
1451 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_RECMASK\n");
1453 *(int*) argp
= SOUND_MASK_IGAIN
;
1456 case SOUND_MIXER_READ_STEREODEVS
:
1457 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_STEREODEVS\n");
1459 pa_threaded_mainloop_lock(i
->mainloop
);
1461 if (i
->sink_volume
.channels
> 1)
1462 *(int*) argp
|= SOUND_MASK_PCM
;
1463 if (i
->source_volume
.channels
> 1)
1464 *(int*) argp
|= SOUND_MASK_IGAIN
;
1465 pa_threaded_mainloop_unlock(i
->mainloop
);
1469 case SOUND_MIXER_READ_RECSRC
:
1470 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_RECSRC\n");
1472 *(int*) argp
= SOUND_MASK_IGAIN
;
1475 case SOUND_MIXER_WRITE_RECSRC
:
1476 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_WRITE_RECSRC\n");
1479 case SOUND_MIXER_READ_CAPS
:
1480 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_CAPS\n");
1485 case SOUND_MIXER_READ_PCM
:
1486 case SOUND_MIXER_READ_IGAIN
: {
1489 if (request
== SOUND_MIXER_READ_PCM
)
1490 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_PCM\n");
1492 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_IGAIN\n");
1494 pa_threaded_mainloop_lock(i
->mainloop
);
1496 if (request
== SOUND_MIXER_READ_PCM
)
1497 v
= &i
->sink_volume
;
1499 v
= &i
->source_volume
;
1502 ((v
->values
[0]*100/PA_VOLUME_NORM
)) |
1503 ((v
->values
[v
->channels
> 1 ? 1 : 0]*100/PA_VOLUME_NORM
) << 8);
1505 pa_threaded_mainloop_unlock(i
->mainloop
);
1510 case SOUND_MIXER_WRITE_PCM
:
1511 case SOUND_MIXER_WRITE_IGAIN
: {
1514 if (request
== SOUND_MIXER_WRITE_PCM
)
1515 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_WRITE_PCM\n");
1517 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_WRITE_IGAIN\n");
1519 pa_threaded_mainloop_lock(i
->mainloop
);
1521 if (request
== SOUND_MIXER_WRITE_PCM
) {
1523 pv
= &i
->sink_volume
;
1525 v
= i
->source_volume
;
1526 pv
= &i
->source_volume
;
1529 pv
->values
[0] = ((*(int*) argp
& 0xFF)*PA_VOLUME_NORM
)/100;
1530 pv
->values
[1] = ((*(int*) argp
>> 8)*PA_VOLUME_NORM
)/100;
1532 if (!pa_cvolume_equal(pv
, &v
)) {
1535 if (request
== SOUND_MIXER_WRITE_PCM
)
1536 o
= pa_context_set_sink_volume_by_index(i
->context
, i
->sink_index
, pv
, context_success_cb
, i
);
1538 o
= pa_context_set_source_volume_by_index(i
->context
, i
->source_index
, pv
, context_success_cb
, i
);
1541 debug(DEBUG_LEVEL_NORMAL
, __FILE__
":Failed set volume: %s", pa_strerror(pa_context_errno(i
->context
)));
1544 i
->operation_success
= 0;
1545 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1546 CONTEXT_CHECK_DEAD_GOTO(i
, exit_loop
);
1548 pa_threaded_mainloop_wait(i
->mainloop
);
1552 if (!i
->operation_success
)
1553 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to set volume: %s\n", pa_strerror(pa_context_errno(i
->context
)));
1555 pa_operation_unref(o
);
1558 /* We don't wait for completion here */
1559 i
->volume_modify_count
++;
1562 pa_threaded_mainloop_unlock(i
->mainloop
);
1567 case SOUND_MIXER_INFO
: {
1568 mixer_info
*mi
= argp
;
1570 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_INFO\n");
1572 memset(mi
, 0, sizeof(mixer_info
));
1573 strncpy(mi
->id
, "PULSEAUDIO", sizeof(mi
->id
));
1574 strncpy(mi
->name
, "PulseAudio Virtual OSS", sizeof(mi
->name
));
1575 pa_threaded_mainloop_lock(i
->mainloop
);
1576 mi
->modify_counter
= i
->volume_modify_count
;
1577 pa_threaded_mainloop_unlock(i
->mainloop
);
1582 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": unknown ioctl 0x%08lx\n", request
);
1595 static int map_format(int *fmt
, pa_sample_spec
*ss
) {
1599 ss
->format
= PA_SAMPLE_ULAW
;
1603 ss
->format
= PA_SAMPLE_ALAW
;
1610 ss
->format
= PA_SAMPLE_U8
;
1617 ss
->format
= PA_SAMPLE_S16BE
;
1624 ss
->format
= PA_SAMPLE_S16LE
;
1628 ss
->format
= PA_SAMPLE_S16NE
;
1636 static int map_format_back(pa_sample_format_t format
) {
1638 case PA_SAMPLE_S16LE
: return AFMT_S16_LE
;
1639 case PA_SAMPLE_S16BE
: return AFMT_S16_BE
;
1640 case PA_SAMPLE_ULAW
: return AFMT_MU_LAW
;
1641 case PA_SAMPLE_ALAW
: return AFMT_A_LAW
;
1642 case PA_SAMPLE_U8
: return AFMT_U8
;
1648 static int dsp_flush_fd(int fd
) {
1652 if (ioctl(fd
, SIOCINQ
, &l
) < 0) {
1653 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SIOCINQ: %s\n", strerror(errno
));
1661 k
= (size_t) l
> sizeof(buf
) ? sizeof(buf
) : (size_t) l
;
1662 if (read(fd
, buf
, k
) < 0)
1663 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": read(): %s\n", strerror(errno
));
1669 # warning "Your platform does not support SIOCINQ, something might not work as intended."
1674 static int dsp_flush_socket(fd_info
*i
) {
1677 if ((i
->thread_fd
< 0) && (i
->app_fd
< 0))
1680 if (i
->thread_fd
>= 0)
1681 res
= dsp_flush_fd(i
->thread_fd
);
1687 res
= dsp_flush_fd(i
->app_fd
);
1695 static int dsp_empty_socket(fd_info
*i
) {
1699 /* Empty the socket */
1703 if (i
->thread_fd
< 0)
1706 if (ioctl(i
->thread_fd
, SIOCINQ
, &l
) < 0) {
1707 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SIOCINQ: %s\n", strerror(errno
));
1716 pa_threaded_mainloop_wait(i
->mainloop
);
1721 # warning "Your platform does not support SIOCINQ, something might not work as intended."
1726 static int dsp_drain(fd_info
*i
) {
1727 pa_operation
*o
= NULL
;
1733 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Draining.\n");
1735 pa_threaded_mainloop_lock(i
->mainloop
);
1737 if (dsp_empty_socket(i
) < 0)
1740 if (!i
->play_stream
)
1743 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Really draining.\n");
1745 if (!(o
= pa_stream_drain(i
->play_stream
, stream_success_cb
, i
))) {
1746 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_drain(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1750 i
->operation_success
= 0;
1751 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1752 PLAYBACK_STREAM_CHECK_DEAD_GOTO(i
, fail
);
1754 pa_threaded_mainloop_wait(i
->mainloop
);
1757 if (!i
->operation_success
) {
1758 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_drain() 2: %s\n", pa_strerror(pa_context_errno(i
->context
)));
1767 pa_operation_unref(o
);
1769 pa_threaded_mainloop_unlock(i
->mainloop
);
1774 static int dsp_trigger(fd_info
*i
) {
1775 pa_operation
*o
= NULL
;
1778 if (!i
->play_stream
)
1781 pa_threaded_mainloop_lock(i
->mainloop
);
1783 if (dsp_empty_socket(i
) < 0)
1786 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Triggering.\n");
1788 if (!(o
= pa_stream_trigger(i
->play_stream
, stream_success_cb
, i
))) {
1789 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1793 i
->operation_success
= 0;
1794 while (!pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1795 PLAYBACK_STREAM_CHECK_DEAD_GOTO(i
, fail
);
1797 pa_threaded_mainloop_wait(i
->mainloop
);
1800 if (!i
->operation_success
) {
1801 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1810 pa_operation_unref(o
);
1812 pa_threaded_mainloop_unlock(i
->mainloop
);
1817 static int dsp_cork(fd_info
*i
, pa_stream
*s
, int b
) {
1818 pa_operation
*o
= NULL
;
1821 pa_threaded_mainloop_lock(i
->mainloop
);
1823 if (!(o
= pa_stream_cork(s
, b
, stream_success_cb
, i
))) {
1824 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_cork(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1828 i
->operation_success
= 0;
1829 while (!pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1830 if (s
== i
->play_stream
)
1831 PLAYBACK_STREAM_CHECK_DEAD_GOTO(i
, fail
);
1832 else if (s
== i
->rec_stream
)
1833 RECORD_STREAM_CHECK_DEAD_GOTO(i
, fail
);
1835 pa_threaded_mainloop_wait(i
->mainloop
);
1838 if (!i
->operation_success
) {
1839 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_cork(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1848 pa_operation_unref(o
);
1850 pa_threaded_mainloop_unlock(i
->mainloop
);
1855 static int dsp_ioctl(fd_info
*i
, unsigned long request
, void*argp
, int *_errno
) {
1858 if (i
->thread_fd
== -1) {
1860 * We've encountered some fatal error and are just waiting
1863 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": got ioctl 0x%08lx in fatal error state\n", request
);
1869 case SNDCTL_DSP_SETFMT
: {
1870 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_SETFMT: %i\n", *(int*) argp
);
1872 pa_threaded_mainloop_lock(i
->mainloop
);
1874 if (*(int*) argp
== AFMT_QUERY
)
1875 *(int*) argp
= map_format_back(i
->sample_spec
.format
);
1877 map_format((int*) argp
, &i
->sample_spec
);
1881 pa_threaded_mainloop_unlock(i
->mainloop
);
1885 case SNDCTL_DSP_SPEED
: {
1890 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_SPEED: %i\n", *(int*) argp
);
1892 pa_threaded_mainloop_lock(i
->mainloop
);
1894 ss
= i
->sample_spec
;
1895 ss
.rate
= *(int*) argp
;
1897 if ((valid
= pa_sample_spec_valid(&ss
))) {
1898 i
->sample_spec
= ss
;
1902 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": ss: %s\n", pa_sample_spec_snprint(t
, sizeof(t
), &i
->sample_spec
));
1904 pa_threaded_mainloop_unlock(i
->mainloop
);
1914 case SNDCTL_DSP_STEREO
:
1915 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_STEREO: %i\n", *(int*) argp
);
1917 pa_threaded_mainloop_lock(i
->mainloop
);
1919 i
->sample_spec
.channels
= *(int*) argp
? 2 : 1;
1922 pa_threaded_mainloop_unlock(i
->mainloop
);
1925 case SNDCTL_DSP_CHANNELS
: {
1929 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_CHANNELS: %i\n", *(int*) argp
);
1931 pa_threaded_mainloop_lock(i
->mainloop
);
1933 ss
= i
->sample_spec
;
1934 ss
.channels
= *(int*) argp
;
1936 if ((valid
= pa_sample_spec_valid(&ss
))) {
1937 i
->sample_spec
= ss
;
1941 pa_threaded_mainloop_unlock(i
->mainloop
);
1951 case SNDCTL_DSP_GETBLKSIZE
:
1952 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_GETBLKSIZE\n");
1954 pa_threaded_mainloop_lock(i
->mainloop
);
1957 *(int*) argp
= i
->fragment_size
;
1959 pa_threaded_mainloop_unlock(i
->mainloop
);
1963 case SNDCTL_DSP_SETFRAGMENT
:
1964 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_SETFRAGMENT: 0x%08x\n", *(int*) argp
);
1966 pa_threaded_mainloop_lock(i
->mainloop
);
1968 i
->fragment_size
= 1 << ((*(int*) argp
) & 31);
1969 i
->n_fragments
= (*(int*) argp
) >> 16;
1971 /* 0x7FFF means that we can set whatever we like */
1972 if (i
->n_fragments
== 0x7FFF)
1973 i
->n_fragments
= 12;
1977 pa_threaded_mainloop_unlock(i
->mainloop
);
1981 case SNDCTL_DSP_GETCAPS
:
1982 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_CAPS\n");
1984 *(int*) argp
= DSP_CAP_DUPLEX
| DSP_CAP_TRIGGER
1985 #ifdef DSP_CAP_MULTI
1991 case SNDCTL_DSP_GETODELAY
: {
1994 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_GETODELAY\n");
1996 pa_threaded_mainloop_lock(i
->mainloop
);
2003 PLAYBACK_STREAM_CHECK_DEAD_GOTO(i
, exit_loop
);
2005 if (pa_stream_get_latency(i
->play_stream
, &usec
, NULL
) >= 0) {
2006 *(int*) argp
= pa_usec_to_bytes(usec
, &i
->sample_spec
);
2010 if (pa_context_errno(i
->context
) != PA_ERR_NODATA
) {
2011 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_get_latency(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
2015 pa_threaded_mainloop_wait(i
->mainloop
);
2021 if (ioctl(i
->thread_fd
, SIOCINQ
, &l
) < 0)
2022 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SIOCINQ failed: %s\n", strerror(errno
));
2026 # warning "Your platform does not support SIOCINQ, something might not work as intended."
2029 pa_threaded_mainloop_unlock(i
->mainloop
);
2031 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": ODELAY: %i\n", *(int*) argp
);
2036 case SNDCTL_DSP_RESET
: {
2037 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_RESET\n");
2039 pa_threaded_mainloop_lock(i
->mainloop
);
2042 dsp_flush_socket(i
);
2044 i
->optr_n_blocks
= 0;
2046 pa_threaded_mainloop_unlock(i
->mainloop
);
2050 case SNDCTL_DSP_GETFMTS
: {
2051 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_GETFMTS\n");
2053 *(int*) argp
= AFMT_MU_LAW
|AFMT_A_LAW
|AFMT_U8
|AFMT_S16_LE
|AFMT_S16_BE
;
2057 case SNDCTL_DSP_POST
:
2058 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_POST\n");
2060 if (dsp_trigger(i
) < 0)
2064 case SNDCTL_DSP_SETTRIGGER
:
2065 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_SETTRIGGER: 0x%08x\n", *(int*) argp
);
2072 i
->play_precork
= !((*(int*) argp
) & PCM_ENABLE_OUTPUT
);
2074 if (i
->play_stream
) {
2075 if (dsp_cork(i
, i
->play_stream
, !((*(int*) argp
) & PCM_ENABLE_OUTPUT
)) < 0)
2077 if (dsp_trigger(i
) < 0)
2081 i
->rec_precork
= !((*(int*) argp
) & PCM_ENABLE_INPUT
);
2083 if (i
->rec_stream
) {
2084 if (dsp_cork(i
, i
->rec_stream
, !((*(int*) argp
) & PCM_ENABLE_INPUT
)) < 0)
2090 case SNDCTL_DSP_SYNC
:
2091 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_SYNC\n");
2093 if (dsp_drain(i
) < 0)
2098 case SNDCTL_DSP_GETOSPACE
:
2099 case SNDCTL_DSP_GETISPACE
: {
2100 audio_buf_info
*bi
= (audio_buf_info
*) argp
;
2104 if (request
== SNDCTL_DSP_GETOSPACE
)
2105 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_GETOSPACE\n");
2107 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_GETISPACE\n");
2109 pa_threaded_mainloop_lock(i
->mainloop
);
2113 if (request
== SNDCTL_DSP_GETOSPACE
) {
2114 if (i
->play_stream
) {
2115 if ((k
= pa_stream_writable_size(i
->play_stream
)) == (size_t) -1)
2116 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_writable_size(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
2118 k
= i
->fragment_size
* i
->n_fragments
;
2121 if (ioctl(i
->thread_fd
, SIOCINQ
, &l
) < 0) {
2122 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SIOCINQ failed: %s\n", strerror(errno
));
2126 # warning "Your platform does not dsp_flush_fd, something might not work as intended."
2129 bi
->bytes
= k
> (size_t) l
? k
- l
: 0;
2131 if (i
->rec_stream
) {
2132 if ((k
= pa_stream_readable_size(i
->rec_stream
)) == (size_t) -1)
2133 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_readable_size(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
2138 if (ioctl(i
->app_fd
, SIOCINQ
, &l
) < 0) {
2139 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SIOCINQ failed: %s\n", strerror(errno
));
2143 # warning "Your platform does not dsp_flush_fd, something might not work as intended."
2148 bi
->fragsize
= i
->fragment_size
;
2149 bi
->fragstotal
= i
->n_fragments
;
2150 bi
->fragments
= bi
->bytes
/ bi
->fragsize
;
2152 pa_threaded_mainloop_unlock(i
->mainloop
);
2154 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": fragsize=%i, fragstotal=%i, bytes=%i, fragments=%i\n", bi
->fragsize
, bi
->fragstotal
, bi
->bytes
, bi
->fragments
);
2159 case SOUND_PCM_READ_RATE
:
2160 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_PCM_READ_RATE\n");
2162 pa_threaded_mainloop_lock(i
->mainloop
);
2163 *(int*) argp
= i
->sample_spec
.rate
;
2164 pa_threaded_mainloop_unlock(i
->mainloop
);
2167 case SOUND_PCM_READ_CHANNELS
:
2168 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_PCM_READ_CHANNELS\n");
2170 pa_threaded_mainloop_lock(i
->mainloop
);
2171 *(int*) argp
= i
->sample_spec
.channels
;
2172 pa_threaded_mainloop_unlock(i
->mainloop
);
2175 case SOUND_PCM_READ_BITS
:
2176 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_PCM_READ_BITS\n");
2178 pa_threaded_mainloop_lock(i
->mainloop
);
2179 *(int*) argp
= pa_sample_size(&i
->sample_spec
)*8;
2180 pa_threaded_mainloop_unlock(i
->mainloop
);
2183 case SNDCTL_DSP_GETOPTR
: {
2186 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_GETOPTR\n");
2188 info
= (count_info
*) argp
;
2189 memset(info
, 0, sizeof(*info
));
2191 pa_threaded_mainloop_lock(i
->mainloop
);
2196 PLAYBACK_STREAM_CHECK_DEAD_GOTO(i
, exit_loop
);
2198 if (pa_stream_get_time(i
->play_stream
, &usec
) >= 0) {
2199 size_t k
= pa_usec_to_bytes(usec
, &i
->sample_spec
);
2202 info
->bytes
= (int) k
;
2203 m
= k
/ i
->fragment_size
;
2204 info
->blocks
= m
- i
->optr_n_blocks
;
2205 i
->optr_n_blocks
= m
;
2210 if (pa_context_errno(i
->context
) != PA_ERR_NODATA
) {
2211 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_get_latency(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
2215 pa_threaded_mainloop_wait(i
->mainloop
);
2218 pa_threaded_mainloop_unlock(i
->mainloop
);
2220 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": GETOPTR bytes=%i, blocks=%i, ptr=%i\n", info
->bytes
, info
->blocks
, info
->ptr
);
2225 case SNDCTL_DSP_GETIPTR
:
2226 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": invalid ioctl SNDCTL_DSP_GETIPTR\n");
2229 case SNDCTL_DSP_SETDUPLEX
:
2230 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_SETDUPLEX\n");
2231 /* this is a no-op */
2235 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": unknown ioctl 0x%08lx\n", request
);
2249 int ioctl(int fd
, unsigned long request
, ...) {
2255 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": ioctl()\n");
2257 va_start(args
, request
);
2258 argp
= va_arg(args
, void *);
2261 if (!function_enter()) {
2263 return _ioctl(fd
, request
, argp
);
2266 if (!(i
= fd_info_find(fd
))) {
2269 return _ioctl(fd
, request
, argp
);
2272 if (i
->type
== FD_INFO_MIXER
)
2273 r
= mixer_ioctl(i
, request
, argp
, &_errno
);
2275 r
= dsp_ioctl(i
, request
, argp
, &_errno
);
2290 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": close()\n");
2292 if (!function_enter()) {
2297 if (!(i
= fd_info_find(fd
))) {
2303 fd_info_remove_from_list(i
);
2311 int access(const char *pathname
, int mode
) {
2314 /* Firefox needs this. See #27 */
2319 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": access(%s)\n", pathname
);
2321 if (strcmp(pathname
, "/dev/dsp") != 0 &&
2322 strcmp(pathname
, "/dev/adsp") != 0 &&
2323 strcmp(pathname
, "/dev/sndstat") != 0 &&
2324 strcmp(pathname
, "/dev/mixer") != 0) {
2326 return _access(pathname
, mode
);
2329 if (mode
& (W_OK
| X_OK
)) {
2330 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": access(%s, %x) = EACCESS\n", pathname
, mode
);
2335 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": access(%s, %x) = OK\n", pathname
, mode
);
2342 int open64(const char *filename
, int flags
, ...) {
2346 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": open64(%s)\n", filename
);
2348 va_start(args
, flags
);
2349 if (flags
& O_CREAT
)
2350 mode
= va_arg(args
, mode_t
);
2353 if (strcmp(filename
, "/dev/dsp") != 0 &&
2354 strcmp(filename
, "/dev/adsp") != 0 &&
2355 strcmp(filename
, "/dev/sndstat") != 0 &&
2356 strcmp(filename
, "/dev/mixer") != 0) {
2358 return _open64(filename
, flags
, mode
);
2361 return open(filename
, flags
, mode
);
2366 FILE* fopen(const char *filename
, const char *mode
) {
2371 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": fopen(%s)\n", filename
);
2373 if (strcmp(filename
, "/dev/dsp") != 0 &&
2374 strcmp(filename
, "/dev/adsp") != 0 &&
2375 strcmp(filename
, "/dev/sndstat") != 0 &&
2376 strcmp(filename
, "/dev/mixer") != 0) {
2378 return _fopen(filename
, mode
);
2394 if ((((mode
[1] == 'b') || (mode
[1] == 't')) && (mode
[2] == '+')) || (mode
[1] == '+'))
2397 if ((fd
= open(filename
, m
)) < 0)
2400 if (!(f
= fdopen(fd
, mode
))) {
2410 FILE *fopen64(const char *filename
, const char *mode
) {
2412 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": fopen64(%s)\n", filename
);
2414 if (strcmp(filename
, "/dev/dsp") != 0 &&
2415 strcmp(filename
, "/dev/adsp") != 0 &&
2416 strcmp(filename
, "/dev/sndstat") != 0 &&
2417 strcmp(filename
, "/dev/mixer") != 0) {
2418 LOAD_FOPEN64_FUNC();
2419 return _fopen64(filename
, mode
);
2422 return fopen(filename
, mode
);
2427 int fclose(FILE *f
) {
2430 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": fclose()\n");
2432 if (!function_enter()) {
2437 if (!(i
= fd_info_find(fileno(f
)))) {
2443 fd_info_remove_from_list(i
);
2445 /* Dirty trick to avoid that the fd is not freed twice, once by us
2446 * and once by the real fclose() */