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>
61 typedef struct fd_info fd_info
;
64 pthread_mutex_t mutex
;
69 int app_fd
, thread_fd
;
71 pa_sample_spec sample_spec
;
75 pa_threaded_mainloop
*mainloop
;
77 pa_stream
*play_stream
;
78 pa_stream
*rec_stream
;
80 pa_io_event
*io_event
;
81 pa_io_event_flags_t io_flags
;
86 int operation_success
;
88 pa_cvolume sink_volume
, source_volume
;
89 uint32_t sink_index
, source_index
;
90 int volume_modify_count
;
92 PA_LLIST_FIELDS(fd_info
);
95 static int dsp_drain(fd_info
*i
);
96 static void fd_info_remove_from_list(fd_info
*i
);
98 static pthread_mutex_t fd_infos_mutex
= PTHREAD_MUTEX_INITIALIZER
;
99 static pthread_mutex_t func_mutex
= PTHREAD_MUTEX_INITIALIZER
;
101 static PA_LLIST_HEAD(fd_info
, fd_infos
) = NULL
;
103 static int (*_ioctl
)(int, int, void*) = NULL
;
104 static int (*_close
)(int) = NULL
;
105 static int (*_open
)(const char *, int, mode_t
) = NULL
;
106 static FILE* (*_fopen
)(const char *path
, const char *mode
) = NULL
;
108 static int (*_open64
)(const char *, int, mode_t
) = NULL
;
109 static FILE* (*_fopen64
)(const char *path
, const char *mode
) = NULL
;
111 static int (*_fclose
)(FILE *f
) = NULL
;
112 static int (*_access
)(const char *, int) = NULL
;
114 /* dlsym() violates ISO C, so confide the breakage into this function to
116 typedef void (*fnptr
)(void);
117 static inline fnptr
dlsym_fn(void *handle
, const char *symbol
) {
118 return (fnptr
) (long) dlsym(handle
, symbol
);
121 #define LOAD_IOCTL_FUNC() \
123 pthread_mutex_lock(&func_mutex); \
125 _ioctl = (int (*)(int, int, void*)) dlsym_fn(RTLD_NEXT, "ioctl"); \
126 pthread_mutex_unlock(&func_mutex); \
129 #define LOAD_OPEN_FUNC() \
131 pthread_mutex_lock(&func_mutex); \
133 _open = (int (*)(const char *, int, mode_t)) dlsym_fn(RTLD_NEXT, "open"); \
134 pthread_mutex_unlock(&func_mutex); \
137 #define LOAD_OPEN64_FUNC() \
139 pthread_mutex_lock(&func_mutex); \
141 _open64 = (int (*)(const char *, int, mode_t)) dlsym_fn(RTLD_NEXT, "open64"); \
142 pthread_mutex_unlock(&func_mutex); \
145 #define LOAD_CLOSE_FUNC() \
147 pthread_mutex_lock(&func_mutex); \
149 _close = (int (*)(int)) dlsym_fn(RTLD_NEXT, "close"); \
150 pthread_mutex_unlock(&func_mutex); \
153 #define LOAD_ACCESS_FUNC() \
155 pthread_mutex_lock(&func_mutex); \
157 _access = (int (*)(const char*, int)) dlsym_fn(RTLD_NEXT, "access"); \
158 pthread_mutex_unlock(&func_mutex); \
161 #define LOAD_FOPEN_FUNC() \
163 pthread_mutex_lock(&func_mutex); \
165 _fopen = (FILE* (*)(const char *, const char*)) dlsym_fn(RTLD_NEXT, "fopen"); \
166 pthread_mutex_unlock(&func_mutex); \
169 #define LOAD_FOPEN64_FUNC() \
171 pthread_mutex_lock(&func_mutex); \
173 _fopen64 = (FILE* (*)(const char *, const char*)) dlsym_fn(RTLD_NEXT, "fopen64"); \
174 pthread_mutex_unlock(&func_mutex); \
177 #define LOAD_FCLOSE_FUNC() \
179 pthread_mutex_lock(&func_mutex); \
181 _fclose = (int (*)(FILE *)) dlsym_fn(RTLD_NEXT, "fclose"); \
182 pthread_mutex_unlock(&func_mutex); \
185 #define CONTEXT_CHECK_DEAD_GOTO(i, label) do { \
186 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY) { \
187 debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
192 #define PLAYBACK_STREAM_CHECK_DEAD_GOTO(i, label) do { \
193 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY || \
194 !(i)->play_stream || pa_stream_get_state((i)->play_stream) != PA_STREAM_READY) { \
195 debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
200 #define RECORD_STREAM_CHECK_DEAD_GOTO(i, label) do { \
201 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY || \
202 !(i)->rec_stream || pa_stream_get_state((i)->rec_stream) != PA_STREAM_READY) { \
203 debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
208 static void debug(int level
, const char *format
, ...) PA_GCC_PRINTF_ATTR(2,3);
210 #define DEBUG_LEVEL_ALWAYS 0
211 #define DEBUG_LEVEL_NORMAL 1
212 #define DEBUG_LEVEL_VERBOSE 2
214 static void debug(int level
, const char *format
, ...) {
216 const char *dlevel_s
;
219 dlevel_s
= getenv("PADSP_DEBUG");
223 dlevel
= atoi(dlevel_s
);
228 va_start(ap
, format
);
229 vfprintf(stderr
, format
, ap
);
233 static int padsp_disabled(void) {
235 static int sym_resolved
= 0;
237 /* If the current process has a symbol __padsp_disabled__ we use
238 * it to detect whether we should enable our stuff or not. A
239 * program needs to be compiled with -rdynamic for this to work!
240 * The symbol must be an int containing a three bit bitmask: bit 1
241 * -> disable /dev/dsp emulation, bit 2 -> disable /dev/sndstat
242 * emulation, bit 3 -> disable /dev/mixer emulation. Hence a value
243 * of 7 disables padsp entirely. */
245 pthread_mutex_lock(&func_mutex
);
247 sym
= (int*) dlsym(RTLD_DEFAULT
, "__padsp_disabled__");
251 pthread_mutex_unlock(&func_mutex
);
259 static int dsp_cloak_enable(void) {
260 if (padsp_disabled() & 1)
263 if (getenv("PADSP_NO_DSP"))
269 static int sndstat_cloak_enable(void) {
270 if (padsp_disabled() & 2)
273 if (getenv("PADSP_NO_SNDSTAT"))
279 static int mixer_cloak_enable(void) {
280 if (padsp_disabled() & 4)
283 if (getenv("PADSP_NO_MIXER"))
288 static pthread_key_t recursion_key
;
290 static void recursion_key_alloc(void) {
291 pthread_key_create(&recursion_key
, NULL
);
294 static int function_enter(void) {
295 /* Avoid recursive calls */
296 static pthread_once_t recursion_key_once
= PTHREAD_ONCE_INIT
;
297 pthread_once(&recursion_key_once
, recursion_key_alloc
);
299 if (pthread_getspecific(recursion_key
))
302 pthread_setspecific(recursion_key
, (void*) 1);
306 static void function_exit(void) {
307 pthread_setspecific(recursion_key
, NULL
);
310 static void fd_info_free(fd_info
*i
) {
313 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": freeing fd info (fd=%i)\n", i
->app_fd
);
318 pa_threaded_mainloop_stop(i
->mainloop
);
320 if (i
->play_stream
) {
321 pa_stream_disconnect(i
->play_stream
);
322 pa_stream_unref(i
->play_stream
);
326 pa_stream_disconnect(i
->rec_stream
);
327 pa_stream_unref(i
->rec_stream
);
331 pa_context_disconnect(i
->context
);
332 pa_context_unref(i
->context
);
336 pa_threaded_mainloop_free(i
->mainloop
);
338 if (i
->app_fd
>= 0) {
343 if (i
->thread_fd
>= 0) {
345 _close(i
->thread_fd
);
350 pthread_mutex_destroy(&i
->mutex
);
354 static fd_info
*fd_info_ref(fd_info
*i
) {
357 pthread_mutex_lock(&i
->mutex
);
361 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": ref++, now %i\n", i
->ref
);
362 pthread_mutex_unlock(&i
->mutex
);
367 static void fd_info_unref(fd_info
*i
) {
369 pthread_mutex_lock(&i
->mutex
);
372 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": ref--, now %i\n", i
->ref
);
373 pthread_mutex_unlock(&i
->mutex
);
379 static void context_state_cb(pa_context
*c
, void *userdata
) {
380 fd_info
*i
= userdata
;
383 switch (pa_context_get_state(c
)) {
384 case PA_CONTEXT_READY
:
385 case PA_CONTEXT_TERMINATED
:
386 case PA_CONTEXT_FAILED
:
387 pa_threaded_mainloop_signal(i
->mainloop
, 0);
390 case PA_CONTEXT_UNCONNECTED
:
391 case PA_CONTEXT_CONNECTING
:
392 case PA_CONTEXT_AUTHORIZING
:
393 case PA_CONTEXT_SETTING_NAME
:
398 static void reset_params(fd_info
*i
) {
401 i
->sample_spec
.format
= PA_SAMPLE_U8
;
402 i
->sample_spec
.channels
= 1;
403 i
->sample_spec
.rate
= 8000;
404 i
->fragment_size
= 0;
408 static const char *client_name(char *buf
, size_t n
) {
412 if ((e
= getenv("PADSP_CLIENT_NAME")))
415 if (pa_get_binary_name(p
, sizeof(p
)))
416 snprintf(buf
, n
, "OSS Emulation[%s]", p
);
418 snprintf(buf
, n
, "OSS");
423 static const char *stream_name(void) {
426 if ((e
= getenv("PADSP_STREAM_NAME")))
429 return "Audio Stream";
432 static void atfork_prepare(void) {
435 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": atfork_prepare() enter\n");
439 pthread_mutex_lock(&fd_infos_mutex
);
441 for (i
= fd_infos
; i
; i
= i
->next
) {
442 pthread_mutex_lock(&i
->mutex
);
443 pa_threaded_mainloop_lock(i
->mainloop
);
446 pthread_mutex_lock(&func_mutex
);
449 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": atfork_prepare() exit\n");
452 static void atfork_parent(void) {
455 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": atfork_parent() enter\n");
457 pthread_mutex_unlock(&func_mutex
);
459 for (i
= fd_infos
; i
; i
= i
->next
) {
460 pa_threaded_mainloop_unlock(i
->mainloop
);
461 pthread_mutex_unlock(&i
->mutex
);
464 pthread_mutex_unlock(&fd_infos_mutex
);
468 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": atfork_parent() exit\n");
471 static void atfork_child(void) {
474 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": atfork_child() enter\n");
476 /* We do only the bare minimum to get all fds closed */
477 pthread_mutex_init(&func_mutex
, NULL
);
478 pthread_mutex_init(&fd_infos_mutex
, NULL
);
480 for (i
= fd_infos
; i
; i
= i
->next
) {
481 pthread_mutex_init(&i
->mutex
, NULL
);
484 pa_context_disconnect(i
->context
);
485 pa_context_unref(i
->context
);
489 if (i
->play_stream
) {
490 pa_stream_unref(i
->play_stream
);
491 i
->play_stream
= NULL
;
495 pa_stream_unref(i
->rec_stream
);
496 i
->rec_stream
= NULL
;
499 if (i
->app_fd
>= 0) {
504 if (i
->thread_fd
>= 0) {
514 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": atfork_child() exit\n");
517 static void install_atfork(void) {
518 pthread_atfork(atfork_prepare
, atfork_parent
, atfork_child
);
521 static void stream_success_cb(pa_stream
*s
, int success
, void *userdata
) {
522 fd_info
*i
= userdata
;
527 i
->operation_success
= success
;
528 pa_threaded_mainloop_signal(i
->mainloop
, 0);
531 static void context_success_cb(pa_context
*c
, int success
, void *userdata
) {
532 fd_info
*i
= userdata
;
537 i
->operation_success
= success
;
538 pa_threaded_mainloop_signal(i
->mainloop
, 0);
541 static fd_info
* fd_info_new(fd_info_type_t type
, int *_errno
) {
543 int sfds
[2] = { -1, -1 };
545 static pthread_once_t install_atfork_once
= PTHREAD_ONCE_INIT
;
547 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": fd_info_new()\n");
549 signal(SIGPIPE
, SIG_IGN
); /* Yes, ugly as hell */
551 pthread_once(&install_atfork_once
, install_atfork
);
553 if (!(i
= malloc(sizeof(fd_info
)))) {
558 i
->app_fd
= i
->thread_fd
= -1;
563 i
->play_stream
= NULL
;
564 i
->rec_stream
= NULL
;
567 pthread_mutex_init(&i
->mutex
, NULL
);
572 pa_cvolume_reset(&i
->sink_volume
, 2);
573 pa_cvolume_reset(&i
->source_volume
, 2);
574 i
->volume_modify_count
= 0;
575 i
->sink_index
= (uint32_t) -1;
576 i
->source_index
= (uint32_t) -1;
577 PA_LLIST_INIT(fd_info
, i
);
581 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, sfds
) < 0) {
583 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": socket() failed: %s\n", strerror(errno
));
588 i
->thread_fd
= sfds
[1];
590 if (!(i
->mainloop
= pa_threaded_mainloop_new())) {
592 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_threaded_mainloop_new() failed\n");
596 if (!(i
->context
= pa_context_new(pa_threaded_mainloop_get_api(i
->mainloop
), client_name(name
, sizeof(name
))))) {
598 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_context_new() failed\n");
602 pa_context_set_state_callback(i
->context
, context_state_cb
, i
);
604 if (pa_context_connect(i
->context
, NULL
, 0, NULL
) < 0) {
605 *_errno
= ECONNREFUSED
;
606 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
610 pa_threaded_mainloop_lock(i
->mainloop
);
612 if (pa_threaded_mainloop_start(i
->mainloop
) < 0) {
614 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_threaded_mainloop_start() failed\n");
615 goto unlock_and_fail
;
618 /* Wait until the context is ready */
619 pa_threaded_mainloop_wait(i
->mainloop
);
621 if (pa_context_get_state(i
->context
) != PA_CONTEXT_READY
) {
622 *_errno
= ECONNREFUSED
;
623 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
624 goto unlock_and_fail
;
627 pa_threaded_mainloop_unlock(i
->mainloop
);
632 pa_threaded_mainloop_unlock(i
->mainloop
);
642 static void fd_info_add_to_list(fd_info
*i
) {
645 pthread_mutex_lock(&fd_infos_mutex
);
646 PA_LLIST_PREPEND(fd_info
, fd_infos
, i
);
647 pthread_mutex_unlock(&fd_infos_mutex
);
652 static void fd_info_remove_from_list(fd_info
*i
) {
655 pthread_mutex_lock(&fd_infos_mutex
);
656 PA_LLIST_REMOVE(fd_info
, fd_infos
, i
);
657 pthread_mutex_unlock(&fd_infos_mutex
);
662 static fd_info
* fd_info_find(int fd
) {
665 pthread_mutex_lock(&fd_infos_mutex
);
667 for (i
= fd_infos
; i
; i
= i
->next
)
668 if (i
->app_fd
== fd
&& !i
->unusable
) {
673 pthread_mutex_unlock(&fd_infos_mutex
);
678 static void fix_metrics(fd_info
*i
) {
680 char t
[PA_SAMPLE_SPEC_SNPRINT_MAX
];
682 fs
= pa_frame_size(&i
->sample_spec
);
684 /* Don't fix things more than necessary */
685 if ((i
->fragment_size
% fs
) == 0 &&
686 i
->n_fragments
>= 2 &&
687 i
->fragment_size
> 0)
690 i
->fragment_size
= (i
->fragment_size
/fs
)*fs
;
692 /* Number of fragments set? */
693 if (i
->n_fragments
< 2) {
694 if (i
->fragment_size
> 0) {
695 i
->n_fragments
= pa_bytes_per_second(&i
->sample_spec
) / 2 / i
->fragment_size
;
696 if (i
->n_fragments
< 2)
702 /* Fragment size set? */
703 if (i
->fragment_size
<= 0) {
704 i
->fragment_size
= pa_bytes_per_second(&i
->sample_spec
) / 2 / i
->n_fragments
;
705 if (i
->fragment_size
< 1024)
706 i
->fragment_size
= 1024;
709 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": sample spec: %s\n", pa_sample_spec_snprint(t
, sizeof(t
), &i
->sample_spec
));
710 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": fixated metrics to %i fragments, %li bytes each.\n", i
->n_fragments
, (long)i
->fragment_size
);
713 static void stream_request_cb(pa_stream
*s
, size_t length
, void *userdata
) {
714 fd_info
*i
= userdata
;
718 pa_mainloop_api
*api
;
721 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
723 if (s
== i
->play_stream
) {
724 n
= pa_stream_writable_size(i
->play_stream
);
725 if (n
== (size_t)-1) {
726 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_writable_size(): %s\n",
727 pa_strerror(pa_context_errno(i
->context
)));
730 if (n
>= i
->fragment_size
)
731 i
->io_flags
|= PA_IO_EVENT_INPUT
;
733 i
->io_flags
&= ~PA_IO_EVENT_INPUT
;
736 if (s
== i
->rec_stream
) {
737 n
= pa_stream_readable_size(i
->rec_stream
);
738 if (n
== (size_t)-1) {
739 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_readable_size(): %s\n",
740 pa_strerror(pa_context_errno(i
->context
)));
743 if (n
>= i
->fragment_size
)
744 i
->io_flags
|= PA_IO_EVENT_OUTPUT
;
746 i
->io_flags
&= ~PA_IO_EVENT_OUTPUT
;
749 api
->io_enable(i
->io_event
, i
->io_flags
);
753 static void stream_latency_update_cb(pa_stream
*s
, void *userdata
) {
754 fd_info
*i
= userdata
;
757 pa_threaded_mainloop_signal(i
->mainloop
, 0);
760 static void fd_info_shutdown(fd_info
*i
) {
764 pa_mainloop_api
*api
;
765 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
766 api
->io_free(i
->io_event
);
771 if (i
->thread_fd
>= 0) {
777 static int fd_info_copy_data(fd_info
*i
, int force
) {
780 if (!i
->play_stream
&& !i
->rec_stream
)
783 if ((i
->play_stream
) && (pa_stream_get_state(i
->play_stream
) == PA_STREAM_READY
)) {
784 n
= pa_stream_writable_size(i
->play_stream
);
786 if (n
== (size_t)-1) {
787 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_writable_size(): %s\n",
788 pa_strerror(pa_context_errno(i
->context
)));
792 while (n
>= i
->fragment_size
|| force
) {
796 if (!(i
->buf
= malloc(i
->fragment_size
))) {
797 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": malloc() failed.\n");
802 if ((r
= read(i
->thread_fd
, i
->buf
, i
->fragment_size
)) <= 0) {
807 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": read(): %s\n", r
== 0 ? "EOF" : strerror(errno
));
811 if (pa_stream_write(i
->play_stream
, i
->buf
, r
, free
, 0, PA_SEEK_RELATIVE
) < 0) {
812 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_write(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
818 assert(n
>= (size_t) r
);
822 if (n
>= i
->fragment_size
)
823 i
->io_flags
|= PA_IO_EVENT_INPUT
;
825 i
->io_flags
&= ~PA_IO_EVENT_INPUT
;
828 if ((i
->rec_stream
) && (pa_stream_get_state(i
->rec_stream
) == PA_STREAM_READY
)) {
829 n
= pa_stream_readable_size(i
->rec_stream
);
831 if (n
== (size_t)-1) {
832 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_readable_size(): %s\n",
833 pa_strerror(pa_context_errno(i
->context
)));
837 while (n
>= i
->fragment_size
|| force
) {
843 if (pa_stream_peek(i
->rec_stream
, &data
, &len
) < 0) {
844 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_peek(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
851 buf
= (const char*)data
+ i
->rec_offset
;
853 if ((r
= write(i
->thread_fd
, buf
, len
- i
->rec_offset
)) <= 0) {
858 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": write(): %s\n", strerror(errno
));
862 assert((size_t)r
<= len
- i
->rec_offset
);
865 if (i
->rec_offset
== len
) {
866 if (pa_stream_drop(i
->rec_stream
) < 0) {
867 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_drop(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
873 assert(n
>= (size_t) r
);
877 if (n
>= i
->fragment_size
)
878 i
->io_flags
|= PA_IO_EVENT_OUTPUT
;
880 i
->io_flags
&= ~PA_IO_EVENT_OUTPUT
;
884 pa_mainloop_api
*api
;
886 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
887 api
->io_enable(i
->io_event
, i
->io_flags
);
893 static void stream_state_cb(pa_stream
*s
, void * userdata
) {
894 fd_info
*i
= userdata
;
897 switch (pa_stream_get_state(s
)) {
899 case PA_STREAM_READY
:
900 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": stream established.\n");
903 case PA_STREAM_FAILED
:
904 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
908 case PA_STREAM_TERMINATED
:
909 case PA_STREAM_UNCONNECTED
:
910 case PA_STREAM_CREATING
:
915 static int create_playback_stream(fd_info
*i
) {
923 if (!(i
->play_stream
= pa_stream_new(i
->context
, stream_name(), &i
->sample_spec
, NULL
))) {
924 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
928 pa_stream_set_state_callback(i
->play_stream
, stream_state_cb
, i
);
929 pa_stream_set_write_callback(i
->play_stream
, stream_request_cb
, i
);
930 pa_stream_set_latency_update_callback(i
->play_stream
, stream_latency_update_cb
, i
);
932 memset(&attr
, 0, sizeof(attr
));
933 attr
.maxlength
= i
->fragment_size
* (i
->n_fragments
+1);
934 attr
.tlength
= i
->fragment_size
* i
->n_fragments
;
935 attr
.prebuf
= i
->fragment_size
;
936 attr
.minreq
= i
->fragment_size
;
938 if (pa_stream_connect_playback(i
->play_stream
, NULL
, &attr
, PA_STREAM_INTERPOLATE_TIMING
|PA_STREAM_AUTO_TIMING_UPDATE
, NULL
, NULL
) < 0) {
939 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
943 n
= i
->fragment_size
;
944 setsockopt(i
->app_fd
, SOL_SOCKET
, SO_SNDBUF
, &n
, sizeof(n
));
945 n
= i
->fragment_size
;
946 setsockopt(i
->thread_fd
, SOL_SOCKET
, SO_RCVBUF
, &n
, sizeof(n
));
954 static int create_record_stream(fd_info
*i
) {
962 if (!(i
->rec_stream
= pa_stream_new(i
->context
, stream_name(), &i
->sample_spec
, NULL
))) {
963 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
967 pa_stream_set_state_callback(i
->rec_stream
, stream_state_cb
, i
);
968 pa_stream_set_read_callback(i
->rec_stream
, stream_request_cb
, i
);
969 pa_stream_set_latency_update_callback(i
->rec_stream
, stream_latency_update_cb
, i
);
971 memset(&attr
, 0, sizeof(attr
));
972 attr
.maxlength
= i
->fragment_size
* (i
->n_fragments
+1);
973 attr
.fragsize
= i
->fragment_size
;
975 if (pa_stream_connect_record(i
->rec_stream
, NULL
, &attr
, PA_STREAM_INTERPOLATE_TIMING
|PA_STREAM_AUTO_TIMING_UPDATE
) < 0) {
976 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
980 n
= i
->fragment_size
;
981 setsockopt(i
->app_fd
, SOL_SOCKET
, SO_RCVBUF
, &n
, sizeof(n
));
982 n
= i
->fragment_size
;
983 setsockopt(i
->thread_fd
, SOL_SOCKET
, SO_SNDBUF
, &n
, sizeof(n
));
991 static void free_streams(fd_info
*i
) {
994 if (i
->play_stream
) {
995 pa_stream_disconnect(i
->play_stream
);
996 pa_stream_unref(i
->play_stream
);
997 i
->play_stream
= NULL
;
1000 if (i
->rec_stream
) {
1001 pa_stream_disconnect(i
->rec_stream
);
1002 pa_stream_unref(i
->rec_stream
);
1003 i
->rec_stream
= NULL
;
1007 static void io_event_cb(pa_mainloop_api
*api
, pa_io_event
*e
, int fd
, pa_io_event_flags_t flags
, void *userdata
) {
1008 fd_info
*i
= userdata
;
1010 pa_threaded_mainloop_signal(i
->mainloop
, 0);
1012 if (flags
& PA_IO_EVENT_INPUT
) {
1014 if (!i
->play_stream
) {
1015 if (create_playback_stream(i
) < 0)
1018 if (fd_info_copy_data(i
, 0) < 0)
1022 } else if (flags
& PA_IO_EVENT_OUTPUT
) {
1024 if (!i
->rec_stream
) {
1025 if (create_record_stream(i
) < 0)
1028 if (fd_info_copy_data(i
, 0) < 0)
1032 } else if (flags
& (PA_IO_EVENT_HANGUP
|PA_IO_EVENT_ERROR
))
1038 /* We can't do anything better than removing the event source */
1039 fd_info_shutdown(i
);
1042 static int dsp_open(int flags
, int *_errno
) {
1044 pa_mainloop_api
*api
;
1048 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": dsp_open()\n");
1050 if (!(i
= fd_info_new(FD_INFO_STREAM
, _errno
)))
1053 if ((flags
& O_NONBLOCK
) == O_NONBLOCK
) {
1054 if ((f
= fcntl(i
->app_fd
, F_GETFL
)) >= 0)
1055 fcntl(i
->app_fd
, F_SETFL
, f
|O_NONBLOCK
);
1057 if ((f
= fcntl(i
->thread_fd
, F_GETFL
)) >= 0)
1058 fcntl(i
->thread_fd
, F_SETFL
, f
|O_NONBLOCK
);
1060 fcntl(i
->app_fd
, F_SETFD
, FD_CLOEXEC
);
1061 fcntl(i
->thread_fd
, F_SETFD
, FD_CLOEXEC
);
1063 pa_threaded_mainloop_lock(i
->mainloop
);
1064 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
1066 switch (flags
& O_ACCMODE
) {
1068 i
->io_flags
= PA_IO_EVENT_OUTPUT
;
1069 shutdown(i
->thread_fd
, SHUT_RD
);
1070 shutdown(i
->app_fd
, SHUT_WR
);
1073 i
->io_flags
= PA_IO_EVENT_INPUT
;
1074 shutdown(i
->thread_fd
, SHUT_WR
);
1075 shutdown(i
->app_fd
, SHUT_RD
);
1078 i
->io_flags
= PA_IO_EVENT_INPUT
| PA_IO_EVENT_OUTPUT
;
1084 if (!(i
->io_event
= api
->io_new(api
, i
->thread_fd
, i
->io_flags
, io_event_cb
, i
)))
1087 pa_threaded_mainloop_unlock(i
->mainloop
);
1089 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": dsp_open() succeeded, fd=%i\n", i
->app_fd
);
1091 fd_info_add_to_list(i
);
1098 pa_threaded_mainloop_unlock(i
->mainloop
);
1105 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": dsp_open() failed\n");
1110 static void sink_info_cb(pa_context
*context
, const pa_sink_info
*si
, int eol
, void *userdata
) {
1111 fd_info
*i
= userdata
;
1113 if (!si
&& eol
< 0) {
1114 i
->operation_success
= 0;
1115 pa_threaded_mainloop_signal(i
->mainloop
, 0);
1122 if (!pa_cvolume_equal(&i
->sink_volume
, &si
->volume
))
1123 i
->volume_modify_count
++;
1125 i
->sink_volume
= si
->volume
;
1126 i
->sink_index
= si
->index
;
1128 i
->operation_success
= 1;
1129 pa_threaded_mainloop_signal(i
->mainloop
, 0);
1132 static void source_info_cb(pa_context
*context
, const pa_source_info
*si
, int eol
, void *userdata
) {
1133 fd_info
*i
= userdata
;
1135 if (!si
&& eol
< 0) {
1136 i
->operation_success
= 0;
1137 pa_threaded_mainloop_signal(i
->mainloop
, 0);
1144 if (!pa_cvolume_equal(&i
->source_volume
, &si
->volume
))
1145 i
->volume_modify_count
++;
1147 i
->source_volume
= si
->volume
;
1148 i
->source_index
= si
->index
;
1150 i
->operation_success
= 1;
1151 pa_threaded_mainloop_signal(i
->mainloop
, 0);
1154 static void subscribe_cb(pa_context
*context
, pa_subscription_event_type_t t
, uint32_t idx
, void *userdata
) {
1155 fd_info
*i
= userdata
;
1156 pa_operation
*o
= NULL
;
1158 if (i
->sink_index
!= idx
)
1161 if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) != PA_SUBSCRIPTION_EVENT_CHANGE
)
1164 if (!(o
= pa_context_get_sink_info_by_index(i
->context
, i
->sink_index
, sink_info_cb
, i
))) {
1165 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to get sink info: %s", pa_strerror(pa_context_errno(i
->context
)));
1169 pa_operation_unref(o
);
1172 static int mixer_open(int flags
, int *_errno
) {
1174 pa_operation
*o
= NULL
;
1177 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": mixer_open()\n");
1179 if (!(i
= fd_info_new(FD_INFO_MIXER
, _errno
)))
1182 pa_threaded_mainloop_lock(i
->mainloop
);
1184 pa_context_set_subscribe_callback(i
->context
, subscribe_cb
, i
);
1186 if (!(o
= pa_context_subscribe(i
->context
, PA_SUBSCRIPTION_MASK_SINK
| PA_SUBSCRIPTION_MASK_SOURCE
, context_success_cb
, i
))) {
1187 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to subscribe to events: %s", pa_strerror(pa_context_errno(i
->context
)));
1192 i
->operation_success
= 0;
1193 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1194 pa_threaded_mainloop_wait(i
->mainloop
);
1195 CONTEXT_CHECK_DEAD_GOTO(i
, fail
);
1198 pa_operation_unref(o
);
1201 if (!i
->operation_success
) {
1202 debug(DEBUG_LEVEL_NORMAL
, __FILE__
":Failed to subscribe to events: %s", pa_strerror(pa_context_errno(i
->context
)));
1209 if (!(o
= pa_context_get_sink_info_by_name(i
->context
, NULL
, sink_info_cb
, i
))) {
1210 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to get sink info: %s", pa_strerror(pa_context_errno(i
->context
)));
1215 i
->operation_success
= 0;
1216 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1217 pa_threaded_mainloop_wait(i
->mainloop
);
1218 CONTEXT_CHECK_DEAD_GOTO(i
, fail
);
1221 pa_operation_unref(o
);
1224 if (!i
->operation_success
) {
1225 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to get sink info: %s", pa_strerror(pa_context_errno(i
->context
)));
1230 /* Get source info */
1232 if (!(o
= pa_context_get_source_info_by_name(i
->context
, NULL
, source_info_cb
, i
))) {
1233 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to get source info: %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 get source info: %s", pa_strerror(pa_context_errno(i
->context
)));
1253 pa_threaded_mainloop_unlock(i
->mainloop
);
1255 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": mixer_open() succeeded, fd=%i\n", i
->app_fd
);
1257 fd_info_add_to_list(i
);
1265 pa_operation_unref(o
);
1267 pa_threaded_mainloop_unlock(i
->mainloop
);
1274 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": mixer_open() failed\n");
1279 static int sndstat_open(int flags
, int *_errno
) {
1280 static const char sndstat
[] =
1281 "Sound Driver:3.8.1a-980706 (PulseAudio Virtual OSS)\n"
1283 "Config options: 0\n"
1285 "Installed drivers:\n"
1286 "Type 255: PulseAudio Virtual OSS\n"
1289 "PulseAudio Virtual OSS\n"
1292 "0: PulseAudio Virtual OSS\n"
1294 "Synth devices: NOT ENABLED IN CONFIG\n"
1301 "0: PulseAudio Virtual OSS\n";
1303 char fn
[] = "/tmp/padsp-sndstat-XXXXXX";
1308 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": sndstat_open()\n");
1310 if (flags
!= O_RDONLY
1312 && flags
!= (O_RDONLY
|O_LARGEFILE
)
1316 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": bad access!\n");
1327 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": mkstemp() failed: %s\n", strerror(errno
));
1333 if (write(fd
, sndstat
, sizeof(sndstat
) -1) != sizeof(sndstat
)-1) {
1335 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": write() failed: %s\n", strerror(errno
));
1339 if (lseek(fd
, SEEK_SET
, 0) < 0) {
1341 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": lseek() failed: %s\n", strerror(errno
));
1353 int open(const char *filename
, int flags
, ...) {
1358 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": open(%s)\n", filename
);
1360 va_start(args
, flags
);
1361 if (flags
& O_CREAT
) {
1362 if (sizeof(mode_t
) < sizeof(int))
1363 mode
= va_arg(args
, int);
1365 mode
= va_arg(args
, mode_t
);
1369 if (!function_enter()) {
1371 return _open(filename
, flags
, mode
);
1374 if (dsp_cloak_enable() && (strcmp(filename
, "/dev/dsp") == 0 || strcmp(filename
, "/dev/adsp") == 0)) {
1375 r
= dsp_open(flags
, &_errno
);
1376 } else if (mixer_cloak_enable() && strcmp(filename
, "/dev/mixer") == 0) {
1377 r
= mixer_open(flags
, &_errno
);
1378 } else if (sndstat_cloak_enable() && strcmp(filename
, "/dev/sndstat") == 0) {
1379 r
= sndstat_open(flags
, &_errno
);
1383 return _open(filename
, flags
, mode
);
1394 static int mixer_ioctl(fd_info
*i
, unsigned long request
, void*argp
, int *_errno
) {
1398 case SOUND_MIXER_READ_DEVMASK
:
1399 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_DEVMASK\n");
1401 *(int*) argp
= SOUND_MASK_PCM
| SOUND_MASK_IGAIN
;
1404 case SOUND_MIXER_READ_RECMASK
:
1405 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_RECMASK\n");
1407 *(int*) argp
= SOUND_MASK_IGAIN
;
1410 case SOUND_MIXER_READ_STEREODEVS
:
1411 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_STEREODEVS\n");
1413 pa_threaded_mainloop_lock(i
->mainloop
);
1415 if (i
->sink_volume
.channels
> 1)
1416 *(int*) argp
|= SOUND_MASK_PCM
;
1417 if (i
->source_volume
.channels
> 1)
1418 *(int*) argp
|= SOUND_MASK_IGAIN
;
1419 pa_threaded_mainloop_unlock(i
->mainloop
);
1423 case SOUND_MIXER_READ_RECSRC
:
1424 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_RECSRC\n");
1426 *(int*) argp
= SOUND_MASK_IGAIN
;
1429 case SOUND_MIXER_WRITE_RECSRC
:
1430 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_WRITE_RECSRC\n");
1433 case SOUND_MIXER_READ_CAPS
:
1434 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_CAPS\n");
1439 case SOUND_MIXER_READ_PCM
:
1440 case SOUND_MIXER_READ_IGAIN
: {
1443 if (request
== SOUND_MIXER_READ_PCM
)
1444 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_PCM\n");
1446 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_IGAIN\n");
1448 pa_threaded_mainloop_lock(i
->mainloop
);
1450 if (request
== SOUND_MIXER_READ_PCM
)
1451 v
= &i
->sink_volume
;
1453 v
= &i
->source_volume
;
1456 ((v
->values
[0]*100/PA_VOLUME_NORM
)) |
1457 ((v
->values
[v
->channels
> 1 ? 1 : 0]*100/PA_VOLUME_NORM
) << 8);
1459 pa_threaded_mainloop_unlock(i
->mainloop
);
1464 case SOUND_MIXER_WRITE_PCM
:
1465 case SOUND_MIXER_WRITE_IGAIN
: {
1468 if (request
== SOUND_MIXER_READ_PCM
)
1469 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_WRITE_PCM\n");
1471 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_WRITE_IGAIN\n");
1473 pa_threaded_mainloop_lock(i
->mainloop
);
1475 if (request
== SOUND_MIXER_READ_PCM
) {
1477 pv
= &i
->sink_volume
;
1479 v
= i
->source_volume
;
1480 pv
= &i
->source_volume
;
1483 pv
->values
[0] = ((*(int*) argp
& 0xFF)*PA_VOLUME_NORM
)/100;
1484 pv
->values
[1] = ((*(int*) argp
>> 8)*PA_VOLUME_NORM
)/100;
1486 if (!pa_cvolume_equal(pv
, &v
)) {
1489 if (request
== SOUND_MIXER_READ_PCM
)
1490 o
= pa_context_set_sink_volume_by_index(i
->context
, i
->sink_index
, pv
, NULL
, NULL
);
1492 o
= pa_context_set_source_volume_by_index(i
->context
, i
->source_index
, pv
, NULL
, NULL
);
1495 debug(DEBUG_LEVEL_NORMAL
, __FILE__
":Failed set volume: %s", pa_strerror(pa_context_errno(i
->context
)));
1498 i
->operation_success
= 0;
1499 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1500 CONTEXT_CHECK_DEAD_GOTO(i
, exit_loop
);
1502 pa_threaded_mainloop_wait(i
->mainloop
);
1506 if (!i
->operation_success
)
1507 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to set volume: %s\n", pa_strerror(pa_context_errno(i
->context
)));
1509 pa_operation_unref(o
);
1512 /* We don't wait for completion here */
1513 i
->volume_modify_count
++;
1516 pa_threaded_mainloop_unlock(i
->mainloop
);
1521 case SOUND_MIXER_INFO
: {
1522 mixer_info
*mi
= argp
;
1524 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_INFO\n");
1526 memset(mi
, 0, sizeof(mixer_info
));
1527 strncpy(mi
->id
, "PULSEAUDIO", sizeof(mi
->id
));
1528 strncpy(mi
->name
, "PulseAudio Virtual OSS", sizeof(mi
->name
));
1529 pa_threaded_mainloop_lock(i
->mainloop
);
1530 mi
->modify_counter
= i
->volume_modify_count
;
1531 pa_threaded_mainloop_unlock(i
->mainloop
);
1536 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": unknown ioctl 0x%08lx\n", request
);
1549 static int map_format(int *fmt
, pa_sample_spec
*ss
) {
1553 ss
->format
= PA_SAMPLE_ULAW
;
1557 ss
->format
= PA_SAMPLE_ALAW
;
1564 ss
->format
= PA_SAMPLE_U8
;
1571 ss
->format
= PA_SAMPLE_S16BE
;
1578 ss
->format
= PA_SAMPLE_S16LE
;
1582 ss
->format
= PA_SAMPLE_S16NE
;
1590 static int map_format_back(pa_sample_format_t format
) {
1592 case PA_SAMPLE_S16LE
: return AFMT_S16_LE
;
1593 case PA_SAMPLE_S16BE
: return AFMT_S16_BE
;
1594 case PA_SAMPLE_ULAW
: return AFMT_MU_LAW
;
1595 case PA_SAMPLE_ALAW
: return AFMT_A_LAW
;
1596 case PA_SAMPLE_U8
: return AFMT_U8
;
1602 static int dsp_flush_fd(int fd
) {
1606 if (ioctl(fd
, SIOCINQ
, &l
) < 0) {
1607 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SIOCINQ: %s\n", strerror(errno
));
1615 k
= (size_t) l
> sizeof(buf
) ? sizeof(buf
) : (size_t) l
;
1616 if (read(fd
, buf
, k
) < 0)
1617 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": read(): %s\n", strerror(errno
));
1623 # warning "Your platform does not support SIOCINQ, something might not work as intended."
1628 static int dsp_flush_socket(fd_info
*i
) {
1631 if ((i
->thread_fd
< 0) && (i
->app_fd
< 0))
1634 if (i
->thread_fd
>= 0)
1635 res
= dsp_flush_fd(i
->thread_fd
);
1641 res
= dsp_flush_fd(i
->app_fd
);
1649 static int dsp_empty_socket(fd_info
*i
) {
1653 /* Empty the socket */
1657 if (i
->thread_fd
< 0)
1660 if (ioctl(i
->thread_fd
, SIOCINQ
, &l
) < 0) {
1661 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SIOCINQ: %s\n", strerror(errno
));
1670 pa_threaded_mainloop_wait(i
->mainloop
);
1675 # warning "Your platform does not support SIOCINQ, something might not work as intended."
1680 static int dsp_drain(fd_info
*i
) {
1681 pa_operation
*o
= NULL
;
1687 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Draining.\n");
1689 pa_threaded_mainloop_lock(i
->mainloop
);
1691 if (dsp_empty_socket(i
) < 0)
1694 if (!i
->play_stream
)
1697 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Really draining.\n");
1699 if (!(o
= pa_stream_drain(i
->play_stream
, stream_success_cb
, i
))) {
1700 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_drain(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1704 i
->operation_success
= 0;
1705 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1706 PLAYBACK_STREAM_CHECK_DEAD_GOTO(i
, fail
);
1708 pa_threaded_mainloop_wait(i
->mainloop
);
1711 if (!i
->operation_success
) {
1712 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_drain() 2: %s\n", pa_strerror(pa_context_errno(i
->context
)));
1721 pa_operation_unref(o
);
1723 pa_threaded_mainloop_unlock(i
->mainloop
);
1728 static int dsp_trigger(fd_info
*i
) {
1729 pa_operation
*o
= NULL
;
1732 if (!i
->play_stream
)
1735 pa_threaded_mainloop_lock(i
->mainloop
);
1737 if (dsp_empty_socket(i
) < 0)
1740 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Triggering.\n");
1742 if (!(o
= pa_stream_trigger(i
->play_stream
, stream_success_cb
, i
))) {
1743 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1747 i
->operation_success
= 0;
1748 while (!pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1749 PLAYBACK_STREAM_CHECK_DEAD_GOTO(i
, fail
);
1751 pa_threaded_mainloop_wait(i
->mainloop
);
1754 if (!i
->operation_success
) {
1755 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1764 pa_operation_unref(o
);
1766 pa_threaded_mainloop_unlock(i
->mainloop
);
1771 static int dsp_ioctl(fd_info
*i
, unsigned long request
, void*argp
, int *_errno
) {
1775 case SNDCTL_DSP_SETFMT
: {
1776 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_SETFMT: %i\n", *(int*) argp
);
1778 pa_threaded_mainloop_lock(i
->mainloop
);
1780 if (*(int*) argp
== AFMT_QUERY
)
1781 *(int*) argp
= map_format_back(i
->sample_spec
.format
);
1783 map_format((int*) argp
, &i
->sample_spec
);
1787 pa_threaded_mainloop_unlock(i
->mainloop
);
1791 case SNDCTL_DSP_SPEED
: {
1796 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_SPEED: %i\n", *(int*) argp
);
1798 pa_threaded_mainloop_lock(i
->mainloop
);
1800 ss
= i
->sample_spec
;
1801 ss
.rate
= *(int*) argp
;
1803 if ((valid
= pa_sample_spec_valid(&ss
))) {
1804 i
->sample_spec
= ss
;
1808 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": ss: %s\n", pa_sample_spec_snprint(t
, sizeof(t
), &i
->sample_spec
));
1810 pa_threaded_mainloop_unlock(i
->mainloop
);
1820 case SNDCTL_DSP_STEREO
:
1821 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_STEREO: %i\n", *(int*) argp
);
1823 pa_threaded_mainloop_lock(i
->mainloop
);
1825 i
->sample_spec
.channels
= *(int*) argp
? 2 : 1;
1828 pa_threaded_mainloop_unlock(i
->mainloop
);
1831 case SNDCTL_DSP_CHANNELS
: {
1835 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_CHANNELS: %i\n", *(int*) argp
);
1837 pa_threaded_mainloop_lock(i
->mainloop
);
1839 ss
= i
->sample_spec
;
1840 ss
.channels
= *(int*) argp
;
1842 if ((valid
= pa_sample_spec_valid(&ss
))) {
1843 i
->sample_spec
= ss
;
1847 pa_threaded_mainloop_unlock(i
->mainloop
);
1857 case SNDCTL_DSP_GETBLKSIZE
:
1858 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_GETBLKSIZE\n");
1860 pa_threaded_mainloop_lock(i
->mainloop
);
1863 *(int*) argp
= i
->fragment_size
;
1865 pa_threaded_mainloop_unlock(i
->mainloop
);
1869 case SNDCTL_DSP_SETFRAGMENT
:
1870 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_SETFRAGMENT: 0x%8x\n", *(int*) argp
);
1872 pa_threaded_mainloop_lock(i
->mainloop
);
1874 i
->fragment_size
= 1 << (*(int*) argp
);
1875 i
->n_fragments
= (*(int*) argp
) >> 16;
1877 /* 0x7FFF means that we can set whatever we like */
1878 if (i
->n_fragments
== 0x7FFF)
1879 i
->n_fragments
= 12;
1883 pa_threaded_mainloop_unlock(i
->mainloop
);
1887 case SNDCTL_DSP_GETCAPS
:
1888 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_CAPS\n");
1890 *(int*) argp
= DSP_CAP_DUPLEX
1891 #ifdef DSP_CAP_MULTI
1897 case SNDCTL_DSP_GETODELAY
: {
1900 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_GETODELAY\n");
1902 pa_threaded_mainloop_lock(i
->mainloop
);
1909 PLAYBACK_STREAM_CHECK_DEAD_GOTO(i
, exit_loop
);
1911 if (pa_stream_get_latency(i
->play_stream
, &usec
, NULL
) >= 0) {
1912 *(int*) argp
= pa_usec_to_bytes(usec
, &i
->sample_spec
);
1916 if (pa_context_errno(i
->context
) != PA_ERR_NODATA
) {
1917 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_get_latency(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1921 pa_threaded_mainloop_wait(i
->mainloop
);
1927 if (ioctl(i
->thread_fd
, SIOCINQ
, &l
) < 0)
1928 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SIOCINQ failed: %s\n", strerror(errno
));
1932 # warning "Your platform does not support SIOCINQ, something might not work as intended."
1935 pa_threaded_mainloop_unlock(i
->mainloop
);
1937 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": ODELAY: %i\n", *(int*) argp
);
1942 case SNDCTL_DSP_RESET
: {
1943 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_RESET\n");
1945 pa_threaded_mainloop_lock(i
->mainloop
);
1948 dsp_flush_socket(i
);
1951 pa_threaded_mainloop_unlock(i
->mainloop
);
1955 case SNDCTL_DSP_GETFMTS
: {
1956 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_GETFMTS\n");
1958 *(int*) argp
= AFMT_MU_LAW
|AFMT_A_LAW
|AFMT_U8
|AFMT_S16_LE
|AFMT_S16_BE
;
1962 case SNDCTL_DSP_POST
:
1963 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_POST\n");
1965 if (dsp_trigger(i
) < 0)
1969 case SNDCTL_DSP_SYNC
:
1970 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_SYNC\n");
1972 if (dsp_drain(i
) < 0)
1977 case SNDCTL_DSP_GETOSPACE
:
1978 case SNDCTL_DSP_GETISPACE
: {
1979 audio_buf_info
*bi
= (audio_buf_info
*) argp
;
1983 if (request
== SNDCTL_DSP_GETOSPACE
)
1984 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_GETOSPACE\n");
1986 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_GETISPACE\n");
1988 pa_threaded_mainloop_lock(i
->mainloop
);
1992 if (request
== SNDCTL_DSP_GETOSPACE
) {
1993 if (i
->play_stream
) {
1994 if ((k
= pa_stream_writable_size(i
->play_stream
)) == (size_t) -1)
1995 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_writable_size(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1997 k
= i
->fragment_size
* i
->n_fragments
;
2000 if (ioctl(i
->thread_fd
, SIOCINQ
, &l
) < 0) {
2001 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SIOCINQ failed: %s\n", strerror(errno
));
2005 # warning "Your platform does not dsp_flush_fd, something might not work as intended."
2008 bi
->bytes
= k
> (size_t) l
? k
- l
: 0;
2010 if (i
->rec_stream
) {
2011 if ((k
= pa_stream_readable_size(i
->rec_stream
)) == (size_t) -1)
2012 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_readable_size(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
2017 if (ioctl(i
->app_fd
, SIOCINQ
, &l
) < 0) {
2018 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SIOCINQ failed: %s\n", strerror(errno
));
2022 # warning "Your platform does not dsp_flush_fd, something might not work as intended."
2027 bi
->fragsize
= i
->fragment_size
;
2028 bi
->fragstotal
= i
->n_fragments
;
2029 bi
->fragments
= bi
->bytes
/ bi
->fragsize
;
2031 pa_threaded_mainloop_unlock(i
->mainloop
);
2033 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": fragsize=%i, fragstotal=%i, bytes=%i, fragments=%i\n", bi
->fragsize
, bi
->fragstotal
, bi
->bytes
, bi
->fragments
);
2038 case SNDCTL_DSP_GETIPTR
:
2039 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": invalid ioctl SNDCTL_DSP_GETIPTR\n");
2042 case SNDCTL_DSP_GETOPTR
:
2043 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": invalid ioctl SNDCTL_DSP_GETOPTR\n");
2047 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": unknown ioctl 0x%08lx\n", request
);
2061 int ioctl(int fd
, unsigned long request
, ...) {
2067 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": ioctl()\n");
2069 va_start(args
, request
);
2070 argp
= va_arg(args
, void *);
2073 if (!function_enter()) {
2075 return _ioctl(fd
, request
, argp
);
2078 if (!(i
= fd_info_find(fd
))) {
2081 return _ioctl(fd
, request
, argp
);
2084 if (i
->type
== FD_INFO_MIXER
)
2085 r
= mixer_ioctl(i
, request
, argp
, &_errno
);
2087 r
= dsp_ioctl(i
, request
, argp
, &_errno
);
2102 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": close()\n");
2104 if (!function_enter()) {
2109 if (!(i
= fd_info_find(fd
))) {
2115 fd_info_remove_from_list(i
);
2123 int access(const char *pathname
, int mode
) {
2124 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": access(%s)\n", pathname
);
2126 if (strcmp(pathname
, "/dev/dsp") != 0 &&
2127 strcmp(pathname
, "/dev/adsp") != 0 &&
2128 strcmp(pathname
, "/dev/sndstat") != 0 &&
2129 strcmp(pathname
, "/dev/mixer") != 0) {
2131 return _access(pathname
, mode
);
2134 if (mode
& (W_OK
| X_OK
)) {
2135 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": access(%s, %x) = EACCESS\n", pathname
, mode
);
2140 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": access(%s, %x) = OK\n", pathname
, mode
);
2147 int open64(const char *filename
, int flags
, ...) {
2151 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": open64(%s)\n", filename
);
2153 va_start(args
, flags
);
2154 if (flags
& O_CREAT
)
2155 mode
= va_arg(args
, mode_t
);
2158 if (strcmp(filename
, "/dev/dsp") != 0 &&
2159 strcmp(filename
, "/dev/adsp") != 0 &&
2160 strcmp(filename
, "/dev/sndstat") != 0 &&
2161 strcmp(filename
, "/dev/mixer") != 0) {
2163 return _open64(filename
, flags
, mode
);
2166 return open(filename
, flags
, mode
);
2171 FILE* fopen(const char *filename
, const char *mode
) {
2176 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": fopen(%s)\n", filename
);
2178 if (strcmp(filename
, "/dev/dsp") != 0 &&
2179 strcmp(filename
, "/dev/adsp") != 0 &&
2180 strcmp(filename
, "/dev/sndstat") != 0 &&
2181 strcmp(filename
, "/dev/mixer") != 0) {
2183 return _fopen(filename
, mode
);
2199 if ((((mode
[1] == 'b') || (mode
[1] == 't')) && (mode
[2] == '+')) || (mode
[1] == '+'))
2202 if ((fd
= open(filename
, m
)) < 0)
2205 if (!(f
= fdopen(fd
, mode
))) {
2215 FILE *fopen64(const char *filename
, const char *mode
) {
2217 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": fopen64(%s)\n", filename
);
2219 if (strcmp(filename
, "/dev/dsp") != 0 &&
2220 strcmp(filename
, "/dev/adsp") != 0 &&
2221 strcmp(filename
, "/dev/sndstat") != 0 &&
2222 strcmp(filename
, "/dev/mixer") != 0) {
2223 LOAD_FOPEN64_FUNC();
2224 return _fopen64(filename
, mode
);
2227 return fopen(filename
, mode
);
2232 int fclose(FILE *f
) {
2235 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": fclose()\n");
2237 if (!function_enter()) {
2242 if (!(i
= fd_info_find(fileno(f
)))) {
2248 fd_info_remove_from_list(i
);
2250 /* Dirty trick to avoid that the fd is not freed twice, once by us
2251 * and once by the real fclose() */