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>
47 #include <linux/sockios.h>
49 #include <pulse/pulseaudio.h>
50 #include <pulsecore/llist.h>
51 #include <pulsecore/gccmacro.h>
58 typedef struct fd_info fd_info
;
61 pthread_mutex_t mutex
;
66 int app_fd
, thread_fd
;
68 pa_sample_spec sample_spec
;
72 pa_threaded_mainloop
*mainloop
;
74 pa_stream
*play_stream
;
75 pa_stream
*rec_stream
;
77 pa_io_event
*io_event
;
82 int operation_success
;
84 pa_cvolume sink_volume
, source_volume
;
85 uint32_t sink_index
, source_index
;
86 int volume_modify_count
;
88 PA_LLIST_FIELDS(fd_info
);
91 static int dsp_drain(fd_info
*i
);
92 static void fd_info_remove_from_list(fd_info
*i
);
94 static pthread_mutex_t fd_infos_mutex
= PTHREAD_MUTEX_INITIALIZER
;
95 static pthread_mutex_t func_mutex
= PTHREAD_MUTEX_INITIALIZER
;
97 static PA_LLIST_HEAD(fd_info
, fd_infos
) = NULL
;
99 static int (*_ioctl
)(int, int, void*) = NULL
;
100 static int (*_close
)(int) = NULL
;
101 static int (*_open
)(const char *, int, mode_t
) = NULL
;
102 static FILE* (*_fopen
)(const char *path
, const char *mode
) = NULL
;
103 static int (*_open64
)(const char *, int, mode_t
) = NULL
;
104 static FILE* (*_fopen64
)(const char *path
, const char *mode
) = NULL
;
105 static int (*_fclose
)(FILE *f
) = NULL
;
106 static int (*_access
)(const char *, int) = NULL
;
108 /* dlsym() violates ISO C, so confide the breakage into this function to
110 typedef void (*fnptr
)(void);
111 static inline fnptr
dlsym_fn(void *handle
, const char *symbol
) {
112 return (fnptr
) (long) dlsym(handle
, symbol
);
115 #define LOAD_IOCTL_FUNC() \
117 pthread_mutex_lock(&func_mutex); \
119 _ioctl = (int (*)(int, int, void*)) dlsym_fn(RTLD_NEXT, "ioctl"); \
120 pthread_mutex_unlock(&func_mutex); \
123 #define LOAD_OPEN_FUNC() \
125 pthread_mutex_lock(&func_mutex); \
127 _open = (int (*)(const char *, int, mode_t)) dlsym_fn(RTLD_NEXT, "open"); \
128 pthread_mutex_unlock(&func_mutex); \
131 #define LOAD_OPEN64_FUNC() \
133 pthread_mutex_lock(&func_mutex); \
135 _open64 = (int (*)(const char *, int, mode_t)) dlsym_fn(RTLD_NEXT, "open64"); \
136 pthread_mutex_unlock(&func_mutex); \
139 #define LOAD_CLOSE_FUNC() \
141 pthread_mutex_lock(&func_mutex); \
143 _close = (int (*)(int)) dlsym_fn(RTLD_NEXT, "close"); \
144 pthread_mutex_unlock(&func_mutex); \
147 #define LOAD_ACCESS_FUNC() \
149 pthread_mutex_lock(&func_mutex); \
151 _access = (int (*)(const char*, int)) dlsym_fn(RTLD_NEXT, "access"); \
152 pthread_mutex_unlock(&func_mutex); \
155 #define LOAD_FOPEN_FUNC() \
157 pthread_mutex_lock(&func_mutex); \
159 _fopen = (FILE* (*)(const char *, const char*)) dlsym_fn(RTLD_NEXT, "fopen"); \
160 pthread_mutex_unlock(&func_mutex); \
163 #define LOAD_FOPEN64_FUNC() \
165 pthread_mutex_lock(&func_mutex); \
167 _fopen64 = (FILE* (*)(const char *, const char*)) dlsym_fn(RTLD_NEXT, "fopen64"); \
168 pthread_mutex_unlock(&func_mutex); \
171 #define LOAD_FCLOSE_FUNC() \
173 pthread_mutex_lock(&func_mutex); \
175 _fclose = (int (*)(FILE *)) dlsym_fn(RTLD_NEXT, "fclose"); \
176 pthread_mutex_unlock(&func_mutex); \
179 #define CONTEXT_CHECK_DEAD_GOTO(i, label) do { \
180 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY) { \
181 debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
186 #define PLAYBACK_STREAM_CHECK_DEAD_GOTO(i, label) do { \
187 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY || \
188 !(i)->play_stream || pa_stream_get_state((i)->play_stream) != PA_STREAM_READY) { \
189 debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
194 #define RECORD_STREAM_CHECK_DEAD_GOTO(i, label) do { \
195 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY || \
196 !(i)->rec_stream || pa_stream_get_state((i)->rec_stream) != PA_STREAM_READY) { \
197 debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
202 static void debug(int level
, const char *format
, ...) PA_GCC_PRINTF_ATTR(2,3);
204 #define DEBUG_LEVEL_ALWAYS 0
205 #define DEBUG_LEVEL_NORMAL 1
206 #define DEBUG_LEVEL_VERBOSE 2
208 static void debug(int level
, const char *format
, ...) {
210 const char *dlevel_s
;
213 dlevel_s
= getenv("PADSP_DEBUG");
217 dlevel
= atoi(dlevel_s
);
222 va_start(ap
, format
);
223 vfprintf(stderr
, format
, ap
);
227 static int padsp_disabled(void) {
229 static int sym_resolved
= 0;
231 /* If the current process has a symbol __padsp_disabled__ we use
232 * it to detect whether we should enable our stuff or not. A
233 * program needs to be compiled with -rdynamic for this to work!
234 * The symbol must be an int containing a three bit bitmask: bit 1
235 * -> disable /dev/dsp emulation, bit 2 -> disable /dev/sndstat
236 * emulation, bit 3 -> disable /dev/mixer emulation. Hence a value
237 * of 7 disables padsp entirely. */
239 pthread_mutex_lock(&func_mutex
);
241 sym
= (int*) dlsym(RTLD_DEFAULT
, "__padsp_disabled__");
245 pthread_mutex_unlock(&func_mutex
);
253 static int dsp_cloak_enable(void) {
254 if (padsp_disabled() & 1)
257 if (getenv("PADSP_NO_DSP"))
263 static int sndstat_cloak_enable(void) {
264 if (padsp_disabled() & 2)
267 if (getenv("PADSP_NO_SNDSTAT"))
273 static int mixer_cloak_enable(void) {
274 if (padsp_disabled() & 4)
277 if (getenv("PADSP_NO_MIXER"))
282 static pthread_key_t recursion_key
;
284 static void recursion_key_alloc(void) {
285 pthread_key_create(&recursion_key
, NULL
);
288 static int function_enter(void) {
289 /* Avoid recursive calls */
290 static pthread_once_t recursion_key_once
= PTHREAD_ONCE_INIT
;
291 pthread_once(&recursion_key_once
, recursion_key_alloc
);
293 if (pthread_getspecific(recursion_key
))
296 pthread_setspecific(recursion_key
, (void*) 1);
300 static void function_exit(void) {
301 pthread_setspecific(recursion_key
, NULL
);
304 static void fd_info_free(fd_info
*i
) {
307 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": freeing fd info (fd=%i)\n", i
->app_fd
);
312 pa_threaded_mainloop_stop(i
->mainloop
);
314 if (i
->play_stream
) {
315 pa_stream_disconnect(i
->play_stream
);
316 pa_stream_unref(i
->play_stream
);
320 pa_stream_disconnect(i
->rec_stream
);
321 pa_stream_unref(i
->rec_stream
);
325 pa_context_disconnect(i
->context
);
326 pa_context_unref(i
->context
);
330 pa_threaded_mainloop_free(i
->mainloop
);
332 if (i
->app_fd
>= 0) {
337 if (i
->thread_fd
>= 0) {
339 _close(i
->thread_fd
);
344 pthread_mutex_destroy(&i
->mutex
);
348 static fd_info
*fd_info_ref(fd_info
*i
) {
351 pthread_mutex_lock(&i
->mutex
);
355 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": ref++, now %i\n", i
->ref
);
356 pthread_mutex_unlock(&i
->mutex
);
361 static void fd_info_unref(fd_info
*i
) {
363 pthread_mutex_lock(&i
->mutex
);
366 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": ref--, now %i\n", i
->ref
);
367 pthread_mutex_unlock(&i
->mutex
);
373 static void context_state_cb(pa_context
*c
, void *userdata
) {
374 fd_info
*i
= userdata
;
377 switch (pa_context_get_state(c
)) {
378 case PA_CONTEXT_READY
:
379 case PA_CONTEXT_TERMINATED
:
380 case PA_CONTEXT_FAILED
:
381 pa_threaded_mainloop_signal(i
->mainloop
, 0);
384 case PA_CONTEXT_UNCONNECTED
:
385 case PA_CONTEXT_CONNECTING
:
386 case PA_CONTEXT_AUTHORIZING
:
387 case PA_CONTEXT_SETTING_NAME
:
392 static void reset_params(fd_info
*i
) {
395 i
->sample_spec
.format
= PA_SAMPLE_U8
;
396 i
->sample_spec
.channels
= 1;
397 i
->sample_spec
.rate
= 8000;
398 i
->fragment_size
= 0;
402 static const char *client_name(char *buf
, size_t n
) {
406 if ((e
= getenv("PADSP_CLIENT_NAME")))
409 if (pa_get_binary_name(p
, sizeof(p
)))
410 snprintf(buf
, n
, "OSS Emulation[%s]", pa_path_get_filename(p
));
412 snprintf(buf
, n
, "OSS");
417 static const char *stream_name(void) {
420 if ((e
= getenv("PADSP_STREAM_NAME")))
423 return "Audio Stream";
426 static void atfork_prepare(void) {
429 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": atfork_prepare() enter\n");
433 pthread_mutex_lock(&fd_infos_mutex
);
435 for (i
= fd_infos
; i
; i
= i
->next
) {
436 pthread_mutex_lock(&i
->mutex
);
437 pa_threaded_mainloop_lock(i
->mainloop
);
440 pthread_mutex_lock(&func_mutex
);
443 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": atfork_prepare() exit\n");
446 static void atfork_parent(void) {
449 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": atfork_parent() enter\n");
451 pthread_mutex_unlock(&func_mutex
);
453 for (i
= fd_infos
; i
; i
= i
->next
) {
454 pa_threaded_mainloop_unlock(i
->mainloop
);
455 pthread_mutex_unlock(&i
->mutex
);
458 pthread_mutex_unlock(&fd_infos_mutex
);
462 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": atfork_parent() exit\n");
465 static void atfork_child(void) {
468 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": atfork_child() enter\n");
470 /* We do only the bare minimum to get all fds closed */
471 pthread_mutex_init(&func_mutex
, NULL
);
472 pthread_mutex_init(&fd_infos_mutex
, NULL
);
474 for (i
= fd_infos
; i
; i
= i
->next
) {
475 pthread_mutex_init(&i
->mutex
, NULL
);
478 pa_context_disconnect(i
->context
);
479 pa_context_unref(i
->context
);
483 if (i
->play_stream
) {
484 pa_stream_unref(i
->play_stream
);
485 i
->play_stream
= NULL
;
489 pa_stream_unref(i
->rec_stream
);
490 i
->rec_stream
= NULL
;
493 if (i
->app_fd
>= 0) {
498 if (i
->thread_fd
>= 0) {
508 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": atfork_child() exit\n");
511 static void install_atfork(void) {
512 pthread_atfork(atfork_prepare
, atfork_parent
, atfork_child
);
515 static void stream_success_cb(pa_stream
*s
, int success
, void *userdata
) {
516 fd_info
*i
= userdata
;
521 i
->operation_success
= success
;
522 pa_threaded_mainloop_signal(i
->mainloop
, 0);
525 static void context_success_cb(pa_context
*c
, int success
, void *userdata
) {
526 fd_info
*i
= userdata
;
531 i
->operation_success
= success
;
532 pa_threaded_mainloop_signal(i
->mainloop
, 0);
535 static fd_info
* fd_info_new(fd_info_type_t type
, int *_errno
) {
537 int sfds
[2] = { -1, -1 };
539 static pthread_once_t install_atfork_once
= PTHREAD_ONCE_INIT
;
541 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": fd_info_new()\n");
543 signal(SIGPIPE
, SIG_IGN
); /* Yes, ugly as hell */
545 pthread_once(&install_atfork_once
, install_atfork
);
547 if (!(i
= malloc(sizeof(fd_info
)))) {
552 i
->app_fd
= i
->thread_fd
= -1;
557 i
->play_stream
= NULL
;
558 i
->rec_stream
= NULL
;
560 pthread_mutex_init(&i
->mutex
, NULL
);
565 pa_cvolume_reset(&i
->sink_volume
, 2);
566 pa_cvolume_reset(&i
->source_volume
, 2);
567 i
->volume_modify_count
= 0;
568 i
->sink_index
= (uint32_t) -1;
569 i
->source_index
= (uint32_t) -1;
570 PA_LLIST_INIT(fd_info
, i
);
574 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, sfds
) < 0) {
576 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": socket() failed: %s\n", strerror(errno
));
581 i
->thread_fd
= sfds
[1];
583 if (!(i
->mainloop
= pa_threaded_mainloop_new())) {
585 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_threaded_mainloop_new() failed\n");
589 if (!(i
->context
= pa_context_new(pa_threaded_mainloop_get_api(i
->mainloop
), client_name(name
, sizeof(name
))))) {
591 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_context_new() failed\n");
595 pa_context_set_state_callback(i
->context
, context_state_cb
, i
);
597 if (pa_context_connect(i
->context
, NULL
, 0, NULL
) < 0) {
598 *_errno
= ECONNREFUSED
;
599 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
603 pa_threaded_mainloop_lock(i
->mainloop
);
605 if (pa_threaded_mainloop_start(i
->mainloop
) < 0) {
607 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_threaded_mainloop_start() failed\n");
608 goto unlock_and_fail
;
611 /* Wait until the context is ready */
612 pa_threaded_mainloop_wait(i
->mainloop
);
614 if (pa_context_get_state(i
->context
) != PA_CONTEXT_READY
) {
615 *_errno
= ECONNREFUSED
;
616 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
617 goto unlock_and_fail
;
620 pa_threaded_mainloop_unlock(i
->mainloop
);
625 pa_threaded_mainloop_unlock(i
->mainloop
);
635 static void fd_info_add_to_list(fd_info
*i
) {
638 pthread_mutex_lock(&fd_infos_mutex
);
639 PA_LLIST_PREPEND(fd_info
, fd_infos
, i
);
640 pthread_mutex_unlock(&fd_infos_mutex
);
645 static void fd_info_remove_from_list(fd_info
*i
) {
648 pthread_mutex_lock(&fd_infos_mutex
);
649 PA_LLIST_REMOVE(fd_info
, fd_infos
, i
);
650 pthread_mutex_unlock(&fd_infos_mutex
);
655 static fd_info
* fd_info_find(int fd
) {
658 pthread_mutex_lock(&fd_infos_mutex
);
660 for (i
= fd_infos
; i
; i
= i
->next
)
661 if (i
->app_fd
== fd
&& !i
->unusable
) {
666 pthread_mutex_unlock(&fd_infos_mutex
);
671 static void fix_metrics(fd_info
*i
) {
673 char t
[PA_SAMPLE_SPEC_SNPRINT_MAX
];
675 fs
= pa_frame_size(&i
->sample_spec
);
677 /* Don't fix things more than necessary */
678 if ((i
->fragment_size
% fs
) == 0 &&
679 i
->n_fragments
>= 2 &&
680 i
->fragment_size
> 0)
683 i
->fragment_size
= (i
->fragment_size
/fs
)*fs
;
685 /* Number of fragments set? */
686 if (i
->n_fragments
< 2) {
687 if (i
->fragment_size
> 0) {
688 i
->n_fragments
= pa_bytes_per_second(&i
->sample_spec
) / 2 / i
->fragment_size
;
689 if (i
->n_fragments
< 2)
695 /* Fragment size set? */
696 if (i
->fragment_size
<= 0) {
697 i
->fragment_size
= pa_bytes_per_second(&i
->sample_spec
) / 2 / i
->n_fragments
;
698 if (i
->fragment_size
< 1024)
699 i
->fragment_size
= 1024;
702 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": sample spec: %s\n", pa_sample_spec_snprint(t
, sizeof(t
), &i
->sample_spec
));
703 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": fixated metrics to %i fragments, %li bytes each.\n", i
->n_fragments
, (long)i
->fragment_size
);
706 static void stream_request_cb(pa_stream
*s
, size_t length
, void *userdata
) {
707 fd_info
*i
= userdata
;
711 pa_mainloop_api
*api
;
712 pa_io_event_flags_t flags
;
715 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
719 if (s
== i
->play_stream
) {
720 n
= pa_stream_writable_size(i
->play_stream
);
721 if (n
== (size_t)-1) {
722 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_writable_size(): %s\n",
723 pa_strerror(pa_context_errno(i
->context
)));
726 if (n
>= i
->fragment_size
)
727 flags
|= PA_IO_EVENT_INPUT
;
730 if (s
== i
->rec_stream
) {
731 n
= pa_stream_readable_size(i
->rec_stream
);
732 if (n
== (size_t)-1) {
733 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_readable_size(): %s\n",
734 pa_strerror(pa_context_errno(i
->context
)));
737 if (n
>= i
->fragment_size
)
738 flags
|= PA_IO_EVENT_OUTPUT
;
741 api
->io_enable(i
->io_event
, flags
);
745 static void stream_latency_update_cb(pa_stream
*s
, void *userdata
) {
746 fd_info
*i
= userdata
;
749 pa_threaded_mainloop_signal(i
->mainloop
, 0);
752 static void fd_info_shutdown(fd_info
*i
) {
756 pa_mainloop_api
*api
;
757 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
758 api
->io_free(i
->io_event
);
762 if (i
->thread_fd
>= 0) {
768 static int fd_info_copy_data(fd_info
*i
, int force
) {
770 pa_io_event_flags_t flags
;
772 if (!i
->play_stream
&& !i
->rec_stream
)
777 if ((i
->play_stream
) && (pa_stream_get_state(i
->play_stream
) == PA_STREAM_READY
)) {
778 n
= pa_stream_writable_size(i
->play_stream
);
780 if (n
== (size_t)-1) {
781 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_writable_size(): %s\n",
782 pa_strerror(pa_context_errno(i
->context
)));
786 while (n
>= i
->fragment_size
|| force
) {
790 if (!(i
->buf
= malloc(i
->fragment_size
))) {
791 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": malloc() failed.\n");
796 if ((r
= read(i
->thread_fd
, i
->buf
, i
->fragment_size
)) <= 0) {
801 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": read(): %s\n", r
== 0 ? "EOF" : strerror(errno
));
805 if (pa_stream_write(i
->play_stream
, i
->buf
, r
, free
, 0, PA_SEEK_RELATIVE
) < 0) {
806 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_write(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
812 assert(n
>= (size_t) r
);
816 if (n
>= i
->fragment_size
)
817 flags
|= PA_IO_EVENT_INPUT
;
820 if ((i
->rec_stream
) && (pa_stream_get_state(i
->rec_stream
) == PA_STREAM_READY
)) {
821 n
= pa_stream_readable_size(i
->rec_stream
);
823 if (n
== (size_t)-1) {
824 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_readable_size(): %s\n",
825 pa_strerror(pa_context_errno(i
->context
)));
829 while (n
>= i
->fragment_size
|| force
) {
835 if (pa_stream_peek(i
->rec_stream
, &data
, &len
) < 0) {
836 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_peek(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
843 buf
= (const char*)data
+ i
->rec_offset
;
845 if ((r
= write(i
->thread_fd
, buf
, len
- i
->rec_offset
)) <= 0) {
850 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": write(): %s\n", strerror(errno
));
854 assert((size_t)r
<= len
- i
->rec_offset
);
857 if (i
->rec_offset
== len
) {
858 if (pa_stream_drop(i
->rec_stream
) < 0) {
859 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_drop(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
865 assert(n
>= (size_t) r
);
869 if (n
>= i
->fragment_size
)
870 flags
|= PA_IO_EVENT_OUTPUT
;
874 pa_mainloop_api
*api
;
876 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
877 api
->io_enable(i
->io_event
, flags
);
883 static void stream_state_cb(pa_stream
*s
, void * userdata
) {
884 fd_info
*i
= userdata
;
887 switch (pa_stream_get_state(s
)) {
889 case PA_STREAM_READY
:
890 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": stream established.\n");
893 case PA_STREAM_FAILED
:
894 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
898 case PA_STREAM_TERMINATED
:
899 case PA_STREAM_UNCONNECTED
:
900 case PA_STREAM_CREATING
:
905 static int create_playback_stream(fd_info
*i
) {
913 if (!(i
->play_stream
= pa_stream_new(i
->context
, stream_name(), &i
->sample_spec
, NULL
))) {
914 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
918 pa_stream_set_state_callback(i
->play_stream
, stream_state_cb
, i
);
919 pa_stream_set_write_callback(i
->play_stream
, stream_request_cb
, i
);
920 pa_stream_set_latency_update_callback(i
->play_stream
, stream_latency_update_cb
, i
);
922 memset(&attr
, 0, sizeof(attr
));
923 attr
.maxlength
= i
->fragment_size
* (i
->n_fragments
+1);
924 attr
.tlength
= i
->fragment_size
* i
->n_fragments
;
925 attr
.prebuf
= i
->fragment_size
;
926 attr
.minreq
= i
->fragment_size
;
928 if (pa_stream_connect_playback(i
->play_stream
, NULL
, &attr
, PA_STREAM_INTERPOLATE_TIMING
|PA_STREAM_AUTO_TIMING_UPDATE
, NULL
, NULL
) < 0) {
929 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
933 n
= i
->fragment_size
;
934 setsockopt(i
->app_fd
, SOL_SOCKET
, SO_SNDBUF
, &n
, sizeof(n
));
935 n
= i
->fragment_size
;
936 setsockopt(i
->thread_fd
, SOL_SOCKET
, SO_RCVBUF
, &n
, sizeof(n
));
944 static int create_record_stream(fd_info
*i
) {
952 if (!(i
->rec_stream
= pa_stream_new(i
->context
, stream_name(), &i
->sample_spec
, NULL
))) {
953 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
957 pa_stream_set_state_callback(i
->rec_stream
, stream_state_cb
, i
);
958 pa_stream_set_read_callback(i
->rec_stream
, stream_request_cb
, i
);
959 pa_stream_set_latency_update_callback(i
->rec_stream
, stream_latency_update_cb
, i
);
961 memset(&attr
, 0, sizeof(attr
));
962 attr
.maxlength
= i
->fragment_size
* (i
->n_fragments
+1);
963 attr
.fragsize
= i
->fragment_size
;
965 if (pa_stream_connect_record(i
->rec_stream
, NULL
, &attr
, PA_STREAM_INTERPOLATE_TIMING
|PA_STREAM_AUTO_TIMING_UPDATE
) < 0) {
966 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
970 n
= i
->fragment_size
;
971 setsockopt(i
->app_fd
, SOL_SOCKET
, SO_RCVBUF
, &n
, sizeof(n
));
972 n
= i
->fragment_size
;
973 setsockopt(i
->thread_fd
, SOL_SOCKET
, SO_SNDBUF
, &n
, sizeof(n
));
981 static void free_streams(fd_info
*i
) {
984 if (i
->play_stream
) {
985 pa_stream_disconnect(i
->play_stream
);
986 pa_stream_unref(i
->play_stream
);
987 i
->play_stream
= NULL
;
991 pa_stream_disconnect(i
->rec_stream
);
992 pa_stream_unref(i
->rec_stream
);
993 i
->rec_stream
= NULL
;
997 static void io_event_cb(pa_mainloop_api
*api
, pa_io_event
*e
, int fd
, pa_io_event_flags_t flags
, void *userdata
) {
998 fd_info
*i
= userdata
;
1000 pa_threaded_mainloop_signal(i
->mainloop
, 0);
1002 if (flags
& PA_IO_EVENT_INPUT
) {
1004 if (!i
->play_stream
) {
1005 if (create_playback_stream(i
) < 0)
1008 if (fd_info_copy_data(i
, 0) < 0)
1012 } else if (flags
& PA_IO_EVENT_OUTPUT
) {
1014 if (!i
->rec_stream
) {
1015 if (create_record_stream(i
) < 0)
1018 if (fd_info_copy_data(i
, 0) < 0)
1022 } else if (flags
& (PA_IO_EVENT_HANGUP
|PA_IO_EVENT_ERROR
))
1028 /* We can't do anything better than removing the event source */
1029 fd_info_shutdown(i
);
1032 static int dsp_open(int flags
, int *_errno
) {
1034 pa_mainloop_api
*api
;
1037 pa_io_event_flags_t ioflags
;
1039 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": dsp_open()\n");
1041 if (!(i
= fd_info_new(FD_INFO_STREAM
, _errno
)))
1044 if ((flags
& O_NONBLOCK
) == O_NONBLOCK
) {
1045 if ((f
= fcntl(i
->app_fd
, F_GETFL
)) >= 0)
1046 fcntl(i
->app_fd
, F_SETFL
, f
|O_NONBLOCK
);
1048 if ((f
= fcntl(i
->thread_fd
, F_GETFL
)) >= 0)
1049 fcntl(i
->thread_fd
, F_SETFL
, f
|O_NONBLOCK
);
1051 fcntl(i
->app_fd
, F_SETFD
, FD_CLOEXEC
);
1052 fcntl(i
->thread_fd
, F_SETFD
, FD_CLOEXEC
);
1054 pa_threaded_mainloop_lock(i
->mainloop
);
1055 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
1057 switch (flags
& O_ACCMODE
) {
1059 ioflags
= PA_IO_EVENT_OUTPUT
;
1060 shutdown(i
->thread_fd
, SHUT_RD
);
1061 shutdown(i
->app_fd
, SHUT_WR
);
1064 ioflags
= PA_IO_EVENT_INPUT
;
1065 shutdown(i
->thread_fd
, SHUT_WR
);
1066 shutdown(i
->app_fd
, SHUT_RD
);
1069 ioflags
= PA_IO_EVENT_INPUT
| PA_IO_EVENT_OUTPUT
;
1075 if (!(i
->io_event
= api
->io_new(api
, i
->thread_fd
, ioflags
, io_event_cb
, i
)))
1078 pa_threaded_mainloop_unlock(i
->mainloop
);
1080 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": dsp_open() succeeded, fd=%i\n", i
->app_fd
);
1082 fd_info_add_to_list(i
);
1089 pa_threaded_mainloop_unlock(i
->mainloop
);
1096 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": dsp_open() failed\n");
1101 static void sink_info_cb(pa_context
*context
, const pa_sink_info
*si
, int eol
, void *userdata
) {
1102 fd_info
*i
= userdata
;
1104 if (!si
&& eol
< 0) {
1105 i
->operation_success
= 0;
1106 pa_threaded_mainloop_signal(i
->mainloop
, 0);
1113 if (!pa_cvolume_equal(&i
->sink_volume
, &si
->volume
))
1114 i
->volume_modify_count
++;
1116 i
->sink_volume
= si
->volume
;
1117 i
->sink_index
= si
->index
;
1119 i
->operation_success
= 1;
1120 pa_threaded_mainloop_signal(i
->mainloop
, 0);
1123 static void source_info_cb(pa_context
*context
, const pa_source_info
*si
, int eol
, void *userdata
) {
1124 fd_info
*i
= userdata
;
1126 if (!si
&& eol
< 0) {
1127 i
->operation_success
= 0;
1128 pa_threaded_mainloop_signal(i
->mainloop
, 0);
1135 if (!pa_cvolume_equal(&i
->source_volume
, &si
->volume
))
1136 i
->volume_modify_count
++;
1138 i
->source_volume
= si
->volume
;
1139 i
->source_index
= si
->index
;
1141 i
->operation_success
= 1;
1142 pa_threaded_mainloop_signal(i
->mainloop
, 0);
1145 static void subscribe_cb(pa_context
*context
, pa_subscription_event_type_t t
, uint32_t idx
, void *userdata
) {
1146 fd_info
*i
= userdata
;
1147 pa_operation
*o
= NULL
;
1149 if (i
->sink_index
!= idx
)
1152 if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) != PA_SUBSCRIPTION_EVENT_CHANGE
)
1155 if (!(o
= pa_context_get_sink_info_by_index(i
->context
, i
->sink_index
, sink_info_cb
, i
))) {
1156 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to get sink info: %s", pa_strerror(pa_context_errno(i
->context
)));
1160 pa_operation_unref(o
);
1163 static int mixer_open(int flags
, int *_errno
) {
1165 pa_operation
*o
= NULL
;
1168 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": mixer_open()\n");
1170 if (!(i
= fd_info_new(FD_INFO_MIXER
, _errno
)))
1173 pa_threaded_mainloop_lock(i
->mainloop
);
1175 pa_context_set_subscribe_callback(i
->context
, subscribe_cb
, i
);
1177 if (!(o
= pa_context_subscribe(i
->context
, PA_SUBSCRIPTION_MASK_SINK
| PA_SUBSCRIPTION_MASK_SOURCE
, context_success_cb
, i
))) {
1178 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to subscribe to events: %s", pa_strerror(pa_context_errno(i
->context
)));
1183 i
->operation_success
= 0;
1184 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1185 pa_threaded_mainloop_wait(i
->mainloop
);
1186 CONTEXT_CHECK_DEAD_GOTO(i
, fail
);
1189 pa_operation_unref(o
);
1192 if (!i
->operation_success
) {
1193 debug(DEBUG_LEVEL_NORMAL
, __FILE__
":Failed to subscribe to events: %s", pa_strerror(pa_context_errno(i
->context
)));
1200 if (!(o
= pa_context_get_sink_info_by_name(i
->context
, NULL
, sink_info_cb
, i
))) {
1201 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to get sink info: %s", pa_strerror(pa_context_errno(i
->context
)));
1206 i
->operation_success
= 0;
1207 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1208 pa_threaded_mainloop_wait(i
->mainloop
);
1209 CONTEXT_CHECK_DEAD_GOTO(i
, fail
);
1212 pa_operation_unref(o
);
1215 if (!i
->operation_success
) {
1216 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to get sink info: %s", pa_strerror(pa_context_errno(i
->context
)));
1221 /* Get source info */
1223 if (!(o
= pa_context_get_source_info_by_name(i
->context
, NULL
, source_info_cb
, i
))) {
1224 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to get source info: %s", pa_strerror(pa_context_errno(i
->context
)));
1229 i
->operation_success
= 0;
1230 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1231 pa_threaded_mainloop_wait(i
->mainloop
);
1232 CONTEXT_CHECK_DEAD_GOTO(i
, fail
);
1235 pa_operation_unref(o
);
1238 if (!i
->operation_success
) {
1239 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to get source info: %s", pa_strerror(pa_context_errno(i
->context
)));
1244 pa_threaded_mainloop_unlock(i
->mainloop
);
1246 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": mixer_open() succeeded, fd=%i\n", i
->app_fd
);
1248 fd_info_add_to_list(i
);
1256 pa_operation_unref(o
);
1258 pa_threaded_mainloop_unlock(i
->mainloop
);
1265 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": mixer_open() failed\n");
1270 static int sndstat_open(int flags
, int *_errno
) {
1271 static const char sndstat
[] =
1272 "Sound Driver:3.8.1a-980706 (PulseAudio Virtual OSS)\n"
1274 "Config options: 0\n"
1276 "Installed drivers:\n"
1277 "Type 255: PulseAudio Virtual OSS\n"
1280 "PulseAudio Virtual OSS\n"
1283 "0: PulseAudio Virtual OSS\n"
1285 "Synth devices: NOT ENABLED IN CONFIG\n"
1292 "0: PulseAudio Virtual OSS\n";
1294 char fn
[] = "/tmp/padsp-sndstat-XXXXXX";
1299 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": sndstat_open()\n");
1301 if (flags
!= O_RDONLY
&& flags
!= (O_RDONLY
|O_LARGEFILE
)) {
1303 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": bad access!\n");
1314 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": mkstemp() failed: %s\n", strerror(errno
));
1320 if (write(fd
, sndstat
, sizeof(sndstat
) -1) != sizeof(sndstat
)-1) {
1322 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": write() failed: %s\n", strerror(errno
));
1326 if (lseek(fd
, SEEK_SET
, 0) < 0) {
1328 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": lseek() failed: %s\n", strerror(errno
));
1340 int open(const char *filename
, int flags
, ...) {
1345 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": open(%s)\n", filename
);
1347 va_start(args
, flags
);
1348 if (flags
& O_CREAT
)
1349 mode
= va_arg(args
, mode_t
);
1352 if (!function_enter()) {
1354 return _open(filename
, flags
, mode
);
1357 if (dsp_cloak_enable() && (strcmp(filename
, "/dev/dsp") == 0 || strcmp(filename
, "/dev/adsp") == 0)) {
1358 r
= dsp_open(flags
, &_errno
);
1359 } else if (mixer_cloak_enable() && strcmp(filename
, "/dev/mixer") == 0) {
1360 r
= mixer_open(flags
, &_errno
);
1361 } else if (sndstat_cloak_enable() && strcmp(filename
, "/dev/sndstat") == 0) {
1362 r
= sndstat_open(flags
, &_errno
);
1366 return _open(filename
, flags
, mode
);
1377 static int mixer_ioctl(fd_info
*i
, unsigned long request
, void*argp
, int *_errno
) {
1381 case SOUND_MIXER_READ_DEVMASK
:
1382 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_DEVMASK\n");
1384 *(int*) argp
= SOUND_MASK_PCM
| SOUND_MASK_IGAIN
;
1387 case SOUND_MIXER_READ_RECMASK
:
1388 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_RECMASK\n");
1390 *(int*) argp
= SOUND_MASK_IGAIN
;
1393 case SOUND_MIXER_READ_STEREODEVS
:
1394 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_STEREODEVS\n");
1396 pa_threaded_mainloop_lock(i
->mainloop
);
1398 if (i
->sink_volume
.channels
> 1)
1399 *(int*) argp
|= SOUND_MASK_PCM
;
1400 if (i
->source_volume
.channels
> 1)
1401 *(int*) argp
|= SOUND_MASK_IGAIN
;
1402 pa_threaded_mainloop_unlock(i
->mainloop
);
1406 case SOUND_MIXER_READ_RECSRC
:
1407 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_RECSRC\n");
1409 *(int*) argp
= SOUND_MASK_IGAIN
;
1412 case SOUND_MIXER_WRITE_RECSRC
:
1413 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_WRITE_RECSRC\n");
1416 case SOUND_MIXER_READ_CAPS
:
1417 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_CAPS\n");
1422 case SOUND_MIXER_READ_PCM
:
1423 case SOUND_MIXER_READ_IGAIN
: {
1426 if (request
== SOUND_MIXER_READ_PCM
)
1427 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_PCM\n");
1429 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_READ_IGAIN\n");
1431 pa_threaded_mainloop_lock(i
->mainloop
);
1433 if (request
== SOUND_MIXER_READ_PCM
)
1434 v
= &i
->sink_volume
;
1436 v
= &i
->source_volume
;
1439 ((v
->values
[0]*100/PA_VOLUME_NORM
)) |
1440 ((v
->values
[v
->channels
> 1 ? 1 : 0]*100/PA_VOLUME_NORM
) << 8);
1442 pa_threaded_mainloop_unlock(i
->mainloop
);
1447 case SOUND_MIXER_WRITE_PCM
:
1448 case SOUND_MIXER_WRITE_IGAIN
: {
1451 if (request
== SOUND_MIXER_READ_PCM
)
1452 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_WRITE_PCM\n");
1454 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_WRITE_IGAIN\n");
1456 pa_threaded_mainloop_lock(i
->mainloop
);
1458 if (request
== SOUND_MIXER_READ_PCM
) {
1460 pv
= &i
->sink_volume
;
1462 v
= i
->source_volume
;
1463 pv
= &i
->source_volume
;
1466 pv
->values
[0] = ((*(int*) argp
& 0xFF)*PA_VOLUME_NORM
)/100;
1467 pv
->values
[1] = ((*(int*) argp
>> 8)*PA_VOLUME_NORM
)/100;
1469 if (!pa_cvolume_equal(pv
, &v
)) {
1472 if (request
== SOUND_MIXER_READ_PCM
)
1473 o
= pa_context_set_sink_volume_by_index(i
->context
, i
->sink_index
, pv
, NULL
, NULL
);
1475 o
= pa_context_set_source_volume_by_index(i
->context
, i
->source_index
, pv
, NULL
, NULL
);
1478 debug(DEBUG_LEVEL_NORMAL
, __FILE__
":Failed set volume: %s", pa_strerror(pa_context_errno(i
->context
)));
1481 i
->operation_success
= 0;
1482 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1483 CONTEXT_CHECK_DEAD_GOTO(i
, exit_loop
);
1485 pa_threaded_mainloop_wait(i
->mainloop
);
1489 if (!i
->operation_success
)
1490 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Failed to set volume: %s\n", pa_strerror(pa_context_errno(i
->context
)));
1492 pa_operation_unref(o
);
1495 /* We don't wait for completion here */
1496 i
->volume_modify_count
++;
1499 pa_threaded_mainloop_unlock(i
->mainloop
);
1504 case SOUND_MIXER_INFO
: {
1505 mixer_info
*mi
= argp
;
1507 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SOUND_MIXER_INFO\n");
1509 memset(mi
, 0, sizeof(mixer_info
));
1510 strncpy(mi
->id
, "PULSEAUDIO", sizeof(mi
->id
));
1511 strncpy(mi
->name
, "PulseAudio Virtual OSS", sizeof(mi
->name
));
1512 pa_threaded_mainloop_lock(i
->mainloop
);
1513 mi
->modify_counter
= i
->volume_modify_count
;
1514 pa_threaded_mainloop_unlock(i
->mainloop
);
1519 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": unknown ioctl 0x%08lx\n", request
);
1532 static int map_format(int *fmt
, pa_sample_spec
*ss
) {
1536 ss
->format
= PA_SAMPLE_ULAW
;
1540 ss
->format
= PA_SAMPLE_ALAW
;
1547 ss
->format
= PA_SAMPLE_U8
;
1554 ss
->format
= PA_SAMPLE_S16BE
;
1561 ss
->format
= PA_SAMPLE_S16LE
;
1565 ss
->format
= PA_SAMPLE_S16NE
;
1573 static int map_format_back(pa_sample_format_t format
) {
1575 case PA_SAMPLE_S16LE
: return AFMT_S16_LE
;
1576 case PA_SAMPLE_S16BE
: return AFMT_S16_BE
;
1577 case PA_SAMPLE_ULAW
: return AFMT_MU_LAW
;
1578 case PA_SAMPLE_ALAW
: return AFMT_A_LAW
;
1579 case PA_SAMPLE_U8
: return AFMT_U8
;
1585 static int dsp_flush_fd(int fd
) {
1588 if (ioctl(fd
, SIOCINQ
, &l
) < 0) {
1589 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SIOCINQ: %s\n", strerror(errno
));
1597 k
= (size_t) l
> sizeof(buf
) ? sizeof(buf
) : (size_t) l
;
1598 if (read(fd
, buf
, k
) < 0)
1599 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": read(): %s\n", strerror(errno
));
1606 static int dsp_flush_socket(fd_info
*i
) {
1609 if ((i
->thread_fd
< 0) && (i
->app_fd
< 0))
1612 if (i
->thread_fd
>= 0)
1613 res
= dsp_flush_fd(i
->thread_fd
);
1619 res
= dsp_flush_fd(i
->app_fd
);
1627 static int dsp_empty_socket(fd_info
*i
) {
1630 /* Empty the socket */
1634 if (i
->thread_fd
< 0)
1637 if (ioctl(i
->thread_fd
, SIOCINQ
, &l
) < 0) {
1638 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SIOCINQ: %s\n", strerror(errno
));
1647 pa_threaded_mainloop_wait(i
->mainloop
);
1653 static int dsp_drain(fd_info
*i
) {
1654 pa_operation
*o
= NULL
;
1660 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Draining.\n");
1662 pa_threaded_mainloop_lock(i
->mainloop
);
1664 if (dsp_empty_socket(i
) < 0)
1667 if (!i
->play_stream
)
1670 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Really draining.\n");
1672 if (!(o
= pa_stream_drain(i
->play_stream
, stream_success_cb
, i
))) {
1673 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_drain(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1677 i
->operation_success
= 0;
1678 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1679 PLAYBACK_STREAM_CHECK_DEAD_GOTO(i
, fail
);
1681 pa_threaded_mainloop_wait(i
->mainloop
);
1684 if (!i
->operation_success
) {
1685 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_drain() 2: %s\n", pa_strerror(pa_context_errno(i
->context
)));
1694 pa_operation_unref(o
);
1696 pa_threaded_mainloop_unlock(i
->mainloop
);
1701 static int dsp_trigger(fd_info
*i
) {
1702 pa_operation
*o
= NULL
;
1705 if (!i
->play_stream
)
1708 pa_threaded_mainloop_lock(i
->mainloop
);
1710 if (dsp_empty_socket(i
) < 0)
1713 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": Triggering.\n");
1715 if (!(o
= pa_stream_trigger(i
->play_stream
, stream_success_cb
, i
))) {
1716 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1720 i
->operation_success
= 0;
1721 while (!pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1722 PLAYBACK_STREAM_CHECK_DEAD_GOTO(i
, fail
);
1724 pa_threaded_mainloop_wait(i
->mainloop
);
1727 if (!i
->operation_success
) {
1728 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1737 pa_operation_unref(o
);
1739 pa_threaded_mainloop_unlock(i
->mainloop
);
1744 static int dsp_ioctl(fd_info
*i
, unsigned long request
, void*argp
, int *_errno
) {
1748 case SNDCTL_DSP_SETFMT
: {
1749 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_SETFMT: %i\n", *(int*) argp
);
1751 pa_threaded_mainloop_lock(i
->mainloop
);
1753 if (*(int*) argp
== AFMT_QUERY
)
1754 *(int*) argp
= map_format_back(i
->sample_spec
.format
);
1756 map_format((int*) argp
, &i
->sample_spec
);
1760 pa_threaded_mainloop_unlock(i
->mainloop
);
1764 case SNDCTL_DSP_SPEED
: {
1769 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_SPEED: %i\n", *(int*) argp
);
1771 pa_threaded_mainloop_lock(i
->mainloop
);
1773 ss
= i
->sample_spec
;
1774 ss
.rate
= *(int*) argp
;
1776 if ((valid
= pa_sample_spec_valid(&ss
))) {
1777 i
->sample_spec
= ss
;
1781 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": ss: %s\n", pa_sample_spec_snprint(t
, sizeof(t
), &i
->sample_spec
));
1783 pa_threaded_mainloop_unlock(i
->mainloop
);
1793 case SNDCTL_DSP_STEREO
:
1794 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_STEREO: %i\n", *(int*) argp
);
1796 pa_threaded_mainloop_lock(i
->mainloop
);
1798 i
->sample_spec
.channels
= *(int*) argp
? 2 : 1;
1801 pa_threaded_mainloop_unlock(i
->mainloop
);
1804 case SNDCTL_DSP_CHANNELS
: {
1808 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_CHANNELS: %i\n", *(int*) argp
);
1810 pa_threaded_mainloop_lock(i
->mainloop
);
1812 ss
= i
->sample_spec
;
1813 ss
.channels
= *(int*) argp
;
1815 if ((valid
= pa_sample_spec_valid(&ss
))) {
1816 i
->sample_spec
= ss
;
1820 pa_threaded_mainloop_unlock(i
->mainloop
);
1830 case SNDCTL_DSP_GETBLKSIZE
:
1831 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_GETBLKSIZE\n");
1833 pa_threaded_mainloop_lock(i
->mainloop
);
1836 *(int*) argp
= i
->fragment_size
;
1838 pa_threaded_mainloop_unlock(i
->mainloop
);
1842 case SNDCTL_DSP_SETFRAGMENT
:
1843 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_SETFRAGMENT: 0x%8x\n", *(int*) argp
);
1845 pa_threaded_mainloop_lock(i
->mainloop
);
1847 i
->fragment_size
= 1 << (*(int*) argp
);
1848 i
->n_fragments
= (*(int*) argp
) >> 16;
1850 /* 0x7FFF means that we can set whatever we like */
1851 if (i
->n_fragments
== 0x7FFF)
1852 i
->n_fragments
= 12;
1856 pa_threaded_mainloop_unlock(i
->mainloop
);
1860 case SNDCTL_DSP_GETCAPS
:
1861 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_CAPS\n");
1863 *(int*) argp
= DSP_CAP_DUPLEX
| DSP_CAP_MULTI
;
1866 case SNDCTL_DSP_GETODELAY
: {
1869 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_GETODELAY\n");
1871 pa_threaded_mainloop_lock(i
->mainloop
);
1878 PLAYBACK_STREAM_CHECK_DEAD_GOTO(i
, exit_loop
);
1880 if (pa_stream_get_latency(i
->play_stream
, &usec
, NULL
) >= 0) {
1881 *(int*) argp
= pa_usec_to_bytes(usec
, &i
->sample_spec
);
1885 if (pa_context_errno(i
->context
) != PA_ERR_NODATA
) {
1886 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_get_latency(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1890 pa_threaded_mainloop_wait(i
->mainloop
);
1895 if (ioctl(i
->thread_fd
, SIOCINQ
, &l
) < 0)
1896 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SIOCINQ failed: %s\n", strerror(errno
));
1900 pa_threaded_mainloop_unlock(i
->mainloop
);
1902 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": ODELAY: %i\n", *(int*) argp
);
1907 case SNDCTL_DSP_RESET
: {
1908 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_RESET\n");
1910 pa_threaded_mainloop_lock(i
->mainloop
);
1913 dsp_flush_socket(i
);
1916 pa_threaded_mainloop_unlock(i
->mainloop
);
1920 case SNDCTL_DSP_GETFMTS
: {
1921 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_GETFMTS\n");
1923 *(int*) argp
= AFMT_MU_LAW
|AFMT_A_LAW
|AFMT_U8
|AFMT_S16_LE
|AFMT_S16_BE
;
1927 case SNDCTL_DSP_POST
:
1928 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_POST\n");
1930 if (dsp_trigger(i
) < 0)
1934 case SNDCTL_DSP_SYNC
:
1935 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_SYNC\n");
1937 if (dsp_drain(i
) < 0)
1942 case SNDCTL_DSP_GETOSPACE
:
1943 case SNDCTL_DSP_GETISPACE
: {
1944 audio_buf_info
*bi
= (audio_buf_info
*) argp
;
1948 if (request
== SNDCTL_DSP_GETOSPACE
)
1949 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_GETOSPACE\n");
1951 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SNDCTL_DSP_GETISPACE\n");
1953 pa_threaded_mainloop_lock(i
->mainloop
);
1957 if (request
== SNDCTL_DSP_GETOSPACE
) {
1958 if (i
->play_stream
) {
1959 if ((k
= pa_stream_writable_size(i
->play_stream
)) == (size_t) -1)
1960 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_writable_size(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1962 k
= i
->fragment_size
* i
->n_fragments
;
1964 if (ioctl(i
->thread_fd
, SIOCINQ
, &l
) < 0) {
1965 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SIOCINQ failed: %s\n", strerror(errno
));
1969 bi
->bytes
= k
> (size_t) l
? k
- l
: 0;
1971 if (i
->rec_stream
) {
1972 if ((k
= pa_stream_readable_size(i
->rec_stream
)) == (size_t) -1)
1973 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": pa_stream_readable_size(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1977 if (ioctl(i
->app_fd
, SIOCINQ
, &l
) < 0) {
1978 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": SIOCINQ failed: %s\n", strerror(errno
));
1985 bi
->fragsize
= i
->fragment_size
;
1986 bi
->fragstotal
= i
->n_fragments
;
1987 bi
->fragments
= bi
->bytes
/ bi
->fragsize
;
1989 pa_threaded_mainloop_unlock(i
->mainloop
);
1991 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": fragsize=%i, fragstotal=%i, bytes=%i, fragments=%i\n", bi
->fragsize
, bi
->fragstotal
, bi
->bytes
, bi
->fragments
);
1996 case SNDCTL_DSP_GETIPTR
:
1997 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": invalid ioctl SNDCTL_DSP_GETIPTR\n");
2000 case SNDCTL_DSP_GETOPTR
:
2001 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": invalid ioctl SNDCTL_DSP_GETOPTR\n");
2005 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": unknown ioctl 0x%08lx\n", request
);
2019 int ioctl(int fd
, unsigned long request
, ...) {
2025 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": ioctl()\n");
2027 va_start(args
, request
);
2028 argp
= va_arg(args
, void *);
2031 if (!function_enter()) {
2033 return _ioctl(fd
, request
, argp
);
2036 if (!(i
= fd_info_find(fd
))) {
2039 return _ioctl(fd
, request
, argp
);
2042 if (i
->type
== FD_INFO_MIXER
)
2043 r
= mixer_ioctl(i
, request
, argp
, &_errno
);
2045 r
= dsp_ioctl(i
, request
, argp
, &_errno
);
2060 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": close()\n");
2062 if (!function_enter()) {
2067 if (!(i
= fd_info_find(fd
))) {
2073 fd_info_remove_from_list(i
);
2081 int access(const char *pathname
, int mode
) {
2082 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": access(%s)\n", pathname
);
2084 if (strcmp(pathname
, "/dev/dsp") != 0 &&
2085 strcmp(pathname
, "/dev/adsp") != 0 &&
2086 strcmp(pathname
, "/dev/sndstat") != 0 &&
2087 strcmp(pathname
, "/dev/mixer") != 0) {
2089 return _access(pathname
, mode
);
2092 if (mode
& (W_OK
| X_OK
)) {
2093 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": access(%s, %x) = EACCESS\n", pathname
, mode
);
2098 debug(DEBUG_LEVEL_NORMAL
, __FILE__
": access(%s, %x) = OK\n", pathname
, mode
);
2103 int open64(const char *filename
, int flags
, ...) {
2107 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": open64(%s)\n", filename
);
2109 va_start(args
, flags
);
2110 if (flags
& O_CREAT
)
2111 mode
= va_arg(args
, mode_t
);
2114 if (strcmp(filename
, "/dev/dsp") != 0 &&
2115 strcmp(filename
, "/dev/adsp") != 0 &&
2116 strcmp(filename
, "/dev/sndstat") != 0 &&
2117 strcmp(filename
, "/dev/mixer") != 0) {
2119 return _open64(filename
, flags
, mode
);
2122 return open(filename
, flags
, mode
);
2125 FILE* fopen(const char *filename
, const char *mode
) {
2130 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": fopen(%s)\n", filename
);
2132 if (strcmp(filename
, "/dev/dsp") != 0 &&
2133 strcmp(filename
, "/dev/adsp") != 0 &&
2134 strcmp(filename
, "/dev/sndstat") != 0 &&
2135 strcmp(filename
, "/dev/mixer") != 0) {
2137 return _fopen(filename
, mode
);
2153 if ((((mode
[1] == 'b') || (mode
[1] == 't')) && (mode
[2] == '+')) || (mode
[1] == '+'))
2156 if ((fd
= open(filename
, m
)) < 0)
2159 if (!(f
= fdopen(fd
, mode
))) {
2167 FILE *fopen64(const char *filename
, const char *mode
) {
2169 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": fopen64(%s)\n", filename
);
2171 if (strcmp(filename
, "/dev/dsp") != 0 &&
2172 strcmp(filename
, "/dev/adsp") != 0 &&
2173 strcmp(filename
, "/dev/sndstat") != 0 &&
2174 strcmp(filename
, "/dev/mixer") != 0) {
2175 LOAD_FOPEN64_FUNC();
2176 return _fopen64(filename
, mode
);
2179 return fopen(filename
, mode
);
2182 int fclose(FILE *f
) {
2185 debug(DEBUG_LEVEL_VERBOSE
, __FILE__
": fclose()\n");
2187 if (!function_enter()) {
2192 if (!(i
= fd_info_find(fileno(f
)))) {
2198 fd_info_remove_from_list(i
);
2200 /* Dirty trick to avoid that the fd is not freed twice, once by us
2201 * and once by the real fclose() */