4 This file is part of polypaudio.
6 polypaudio 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 polypaudio 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 polypaudio; 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 <polyp/polypaudio.h>
50 #include <polypcore/llist.h>
51 #include <polypcore/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
;
76 pa_io_event
*io_event
;
80 int operation_success
;
84 int volume_modify_count
;
86 PA_LLIST_FIELDS(fd_info
);
89 static int dsp_drain(fd_info
*i
);
90 static void fd_info_remove_from_list(fd_info
*i
);
92 static pthread_mutex_t fd_infos_mutex
= PTHREAD_MUTEX_INITIALIZER
;
93 static pthread_mutex_t func_mutex
= PTHREAD_MUTEX_INITIALIZER
;
95 static PA_LLIST_HEAD(fd_info
, fd_infos
) = NULL
;
97 static int (*_ioctl
)(int, int, void*) = NULL
;
98 static int (*_close
)(int) = NULL
;
99 static int (*_open
)(const char *, int, mode_t
) = NULL
;
100 static FILE* (*_fopen
)(const char *path
, const char *mode
) = NULL
;
101 static int (*_open64
)(const char *, int, mode_t
) = NULL
;
102 static FILE* (*_fopen64
)(const char *path
, const char *mode
) = NULL
;
103 static int (*_fclose
)(FILE *f
) = NULL
;
105 /* dlsym() violates ISO C, so confide the breakage into this function to
107 typedef void (*fnptr
)(void);
108 static inline fnptr
dlsym_fn(void *handle
, const char *symbol
) {
109 return (fnptr
) (long) dlsym(handle
, symbol
);
112 #define LOAD_IOCTL_FUNC() \
114 pthread_mutex_lock(&func_mutex); \
116 _ioctl = (int (*)(int, int, void*)) dlsym_fn(RTLD_NEXT, "ioctl"); \
117 pthread_mutex_unlock(&func_mutex); \
120 #define LOAD_OPEN_FUNC() \
122 pthread_mutex_lock(&func_mutex); \
124 _open = (int (*)(const char *, int, mode_t)) dlsym_fn(RTLD_NEXT, "open"); \
125 pthread_mutex_unlock(&func_mutex); \
128 #define LOAD_OPEN64_FUNC() \
130 pthread_mutex_lock(&func_mutex); \
132 _open64 = (int (*)(const char *, int, mode_t)) dlsym_fn(RTLD_NEXT, "open64"); \
133 pthread_mutex_unlock(&func_mutex); \
136 #define LOAD_CLOSE_FUNC() \
138 pthread_mutex_lock(&func_mutex); \
140 _close = (int (*)(int)) dlsym_fn(RTLD_NEXT, "close"); \
141 pthread_mutex_unlock(&func_mutex); \
144 #define LOAD_FOPEN_FUNC() \
146 pthread_mutex_lock(&func_mutex); \
148 _fopen = (FILE* (*)(const char *, const char*)) dlsym_fn(RTLD_NEXT, "fopen"); \
149 pthread_mutex_unlock(&func_mutex); \
152 #define LOAD_FOPEN64_FUNC() \
154 pthread_mutex_lock(&func_mutex); \
156 _fopen64 = (FILE* (*)(const char *, const char*)) dlsym_fn(RTLD_NEXT, "fopen64"); \
157 pthread_mutex_unlock(&func_mutex); \
160 #define LOAD_FCLOSE_FUNC() \
162 pthread_mutex_lock(&func_mutex); \
164 _fclose = (int (*)(FILE *)) dlsym_fn(RTLD_NEXT, "fclose"); \
165 pthread_mutex_unlock(&func_mutex); \
168 #define CONTEXT_CHECK_DEAD_GOTO(i, label) do { \
169 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY) { \
170 debug(__FILE__": Not connected: %s", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
175 #define STREAM_CHECK_DEAD_GOTO(i, label) do { \
176 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY || \
177 !(i)->stream || pa_stream_get_state((i)->stream) != PA_STREAM_READY) { \
178 debug(__FILE__": Not connected: %s", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
183 static void debug(const char *format
, ...) PA_GCC_PRINTF_ATTR(1,2);
185 static void debug(const char *format
, ...) {
187 if (getenv("PADSP_DEBUG")) {
188 va_start(ap
, format
);
189 vfprintf(stderr
, format
, ap
);
194 static pthread_key_t recursion_key
;
196 static void recursion_key_alloc(void) {
197 pthread_key_create(&recursion_key
, NULL
);
200 static int function_enter(void) {
201 /* Avoid recursive calls */
202 static pthread_once_t recursion_key_once
= PTHREAD_ONCE_INIT
;
203 pthread_once(&recursion_key_once
, recursion_key_alloc
);
205 if (pthread_getspecific(recursion_key
))
208 pthread_setspecific(recursion_key
, (void*) 1);
212 static void function_exit(void) {
213 pthread_setspecific(recursion_key
, NULL
);
216 static void fd_info_free(fd_info
*i
) {
219 debug(__FILE__
": freeing fd info (fd=%i)\n", i
->app_fd
);
224 pa_threaded_mainloop_stop(i
->mainloop
);
227 pa_stream_disconnect(i
->stream
);
228 pa_stream_unref(i
->stream
);
232 pa_context_disconnect(i
->context
);
233 pa_context_unref(i
->context
);
237 pa_threaded_mainloop_free(i
->mainloop
);
239 if (i
->app_fd
>= 0) {
244 if (i
->thread_fd
>= 0) {
246 _close(i
->thread_fd
);
251 pthread_mutex_destroy(&i
->mutex
);
255 static fd_info
*fd_info_ref(fd_info
*i
) {
258 pthread_mutex_lock(&i
->mutex
);
262 /* debug(__FILE__": ref++, now %i\n", i->ref); */
263 pthread_mutex_unlock(&i
->mutex
);
268 static void fd_info_unref(fd_info
*i
) {
270 pthread_mutex_lock(&i
->mutex
);
273 /* debug(__FILE__": ref--, now %i\n", i->ref); */
274 pthread_mutex_unlock(&i
->mutex
);
280 static void context_state_cb(pa_context
*c
, void *userdata
) {
281 fd_info
*i
= userdata
;
284 switch (pa_context_get_state(c
)) {
285 case PA_CONTEXT_READY
:
286 case PA_CONTEXT_TERMINATED
:
287 case PA_CONTEXT_FAILED
:
288 pa_threaded_mainloop_signal(i
->mainloop
, 0);
291 case PA_CONTEXT_UNCONNECTED
:
292 case PA_CONTEXT_CONNECTING
:
293 case PA_CONTEXT_AUTHORIZING
:
294 case PA_CONTEXT_SETTING_NAME
:
299 static void reset_params(fd_info
*i
) {
302 i
->sample_spec
.format
= PA_SAMPLE_ULAW
;
303 i
->sample_spec
.channels
= 1;
304 i
->sample_spec
.rate
= 8000;
305 i
->fragment_size
= 1024;
309 static char *client_name(char *buf
, size_t n
) {
312 if (pa_get_binary_name(p
, sizeof(p
)))
313 snprintf(buf
, n
, "oss[%s]", pa_path_get_filename(p
));
315 snprintf(buf
, n
, "oss");
320 static void atfork_prepare(void) {
323 debug(__FILE__
": atfork_prepare() enter\n");
327 pthread_mutex_lock(&fd_infos_mutex
);
329 for (i
= fd_infos
; i
; i
= i
->next
) {
330 pthread_mutex_lock(&i
->mutex
);
331 pa_threaded_mainloop_lock(i
->mainloop
);
334 pthread_mutex_lock(&func_mutex
);
337 debug(__FILE__
": atfork_prepare() exit\n");
340 static void atfork_parent(void) {
343 debug(__FILE__
": atfork_parent() enter\n");
345 pthread_mutex_unlock(&func_mutex
);
347 for (i
= fd_infos
; i
; i
= i
->next
) {
348 pa_threaded_mainloop_unlock(i
->mainloop
);
349 pthread_mutex_unlock(&i
->mutex
);
352 pthread_mutex_unlock(&fd_infos_mutex
);
356 debug(__FILE__
": atfork_parent() exit\n");
359 static void atfork_child(void) {
362 debug(__FILE__
": atfork_child() enter\n");
364 /* We do only the bare minimum to get all fds closed */
365 pthread_mutex_init(&func_mutex
, NULL
);
366 pthread_mutex_init(&fd_infos_mutex
, NULL
);
368 for (i
= fd_infos
; i
; i
= i
->next
) {
369 pthread_mutex_init(&i
->mutex
, NULL
);
372 pa_context_disconnect(i
->context
);
373 pa_context_unref(i
->context
);
378 pa_stream_unref(i
->stream
);
382 if (i
->app_fd
>= 0) {
387 if (i
->thread_fd
>= 0) {
397 debug(__FILE__
": atfork_child() exit\n");
400 static void install_atfork(void) {
401 pthread_atfork(atfork_prepare
, atfork_parent
, atfork_child
);
404 static void stream_success_cb(pa_stream
*s
, int success
, void *userdata
) {
405 fd_info
*i
= userdata
;
410 i
->operation_success
= success
;
411 pa_threaded_mainloop_signal(i
->mainloop
, 0);
414 static void context_success_cb(pa_context
*c
, int success
, void *userdata
) {
415 fd_info
*i
= userdata
;
420 i
->operation_success
= success
;
421 pa_threaded_mainloop_signal(i
->mainloop
, 0);
424 static fd_info
* fd_info_new(fd_info_type_t type
, int *_errno
) {
426 int sfds
[2] = { -1, -1 };
428 static pthread_once_t install_atfork_once
= PTHREAD_ONCE_INIT
;
430 debug(__FILE__
": fd_info_new()\n");
432 signal(SIGPIPE
, SIG_IGN
); /* Yes, ugly as hell */
434 pthread_once(&install_atfork_once
, install_atfork
);
436 if (!(i
= malloc(sizeof(fd_info
)))) {
441 i
->app_fd
= i
->thread_fd
= -1;
448 pthread_mutex_init(&i
->mutex
, NULL
);
452 pa_cvolume_reset(&i
->volume
, 2);
453 i
->volume_modify_count
= 0;
454 i
->sink_index
= (uint32_t) -1;
455 PA_LLIST_INIT(fd_info
, i
);
459 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, sfds
) < 0) {
461 debug(__FILE__
": socket() failed: %s\n", strerror(errno
));
466 i
->thread_fd
= sfds
[1];
468 if (!(i
->mainloop
= pa_threaded_mainloop_new())) {
470 debug(__FILE__
": pa_threaded_mainloop_new() failed\n");
474 if (!(i
->context
= pa_context_new(pa_threaded_mainloop_get_api(i
->mainloop
), client_name(name
, sizeof(name
))))) {
476 debug(__FILE__
": pa_context_new() failed\n");
480 pa_context_set_state_callback(i
->context
, context_state_cb
, i
);
482 if (pa_context_connect(i
->context
, NULL
, 0, NULL
) < 0) {
483 *_errno
= ECONNREFUSED
;
484 debug(__FILE__
": pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
488 pa_threaded_mainloop_lock(i
->mainloop
);
490 if (pa_threaded_mainloop_start(i
->mainloop
) < 0) {
492 debug(__FILE__
": pa_threaded_mainloop_start() failed\n");
493 goto unlock_and_fail
;
496 /* Wait until the context is ready */
497 pa_threaded_mainloop_wait(i
->mainloop
);
499 if (pa_context_get_state(i
->context
) != PA_CONTEXT_READY
) {
500 *_errno
= ECONNREFUSED
;
501 debug(__FILE__
": pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
502 goto unlock_and_fail
;
505 pa_threaded_mainloop_unlock(i
->mainloop
);
510 pa_threaded_mainloop_unlock(i
->mainloop
);
520 static void fd_info_add_to_list(fd_info
*i
) {
523 pthread_mutex_lock(&fd_infos_mutex
);
524 PA_LLIST_PREPEND(fd_info
, fd_infos
, i
);
525 pthread_mutex_unlock(&fd_infos_mutex
);
530 static void fd_info_remove_from_list(fd_info
*i
) {
533 pthread_mutex_lock(&fd_infos_mutex
);
534 PA_LLIST_REMOVE(fd_info
, fd_infos
, i
);
535 pthread_mutex_unlock(&fd_infos_mutex
);
540 static fd_info
* fd_info_find(int fd
) {
543 pthread_mutex_lock(&fd_infos_mutex
);
545 for (i
= fd_infos
; i
; i
= i
->next
)
546 if (i
->app_fd
== fd
&& !i
->unusable
) {
551 pthread_mutex_unlock(&fd_infos_mutex
);
556 static void fix_metrics(fd_info
*i
) {
558 char t
[PA_SAMPLE_SPEC_SNPRINT_MAX
];
560 fs
= pa_frame_size(&i
->sample_spec
);
561 i
->fragment_size
= (i
->fragment_size
/fs
)*fs
;
563 if (i
->n_fragments
< 2)
566 if (i
->fragment_size
<= 0)
567 if ((i
->fragment_size
= pa_bytes_per_second(&i
->sample_spec
) / 2 / i
->n_fragments
) <= 0)
568 i
->fragment_size
= 1024;
570 debug(__FILE__
": sample spec: %s\n", pa_sample_spec_snprint(t
, sizeof(t
), &i
->sample_spec
));
571 debug(__FILE__
": fixated metrics to %i fragments, %li bytes each.\n", i
->n_fragments
, (long)i
->fragment_size
);
574 static void stream_request_cb(pa_stream
*s
, size_t length
, void *userdata
) {
575 fd_info
*i
= userdata
;
579 pa_mainloop_api
*api
;
580 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
581 api
->io_enable(i
->io_event
, PA_IO_EVENT_INPUT
);
585 static void stream_latency_update_cb(pa_stream
*s
, void *userdata
) {
586 fd_info
*i
= userdata
;
589 pa_threaded_mainloop_signal(i
->mainloop
, 0);
592 static void fd_info_shutdown(fd_info
*i
) {
596 pa_mainloop_api
*api
;
597 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
598 api
->io_free(i
->io_event
);
602 if (i
->thread_fd
>= 0) {
608 static int fd_info_copy_data(fd_info
*i
, int force
) {
614 if ((n
= pa_stream_writable_size(i
->stream
)) == (size_t) -1) {
615 debug(__FILE__
": pa_stream_writable_size(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
619 while (n
>= i
->fragment_size
|| force
) {
623 if (!(i
->buf
= malloc(i
->fragment_size
))) {
624 debug(__FILE__
": malloc() failed.\n");
629 if ((r
= read(i
->thread_fd
, i
->buf
, i
->fragment_size
)) <= 0) {
634 debug(__FILE__
": read(): %s\n", r
== 0 ? "EOF" : strerror(errno
));
638 if (pa_stream_write(i
->stream
, i
->buf
, r
, free
, 0, PA_SEEK_RELATIVE
) < 0) {
639 debug(__FILE__
": pa_stream_write(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
645 assert(n
>= (size_t) r
);
650 pa_mainloop_api
*api
;
651 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
652 api
->io_enable(i
->io_event
, n
>= i
->fragment_size
? PA_IO_EVENT_INPUT
: 0);
658 static void stream_state_cb(pa_stream
*s
, void * userdata
) {
659 fd_info
*i
= userdata
;
662 switch (pa_stream_get_state(s
)) {
664 case PA_STREAM_READY
:
665 debug(__FILE__
": stream established.\n");
668 case PA_STREAM_FAILED
:
669 debug(__FILE__
": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
673 case PA_STREAM_TERMINATED
:
674 case PA_STREAM_UNCONNECTED
:
675 case PA_STREAM_CREATING
:
680 static int create_stream(fd_info
*i
) {
688 if (!(i
->stream
= pa_stream_new(i
->context
, "Audio Stream", &i
->sample_spec
, NULL
))) {
689 debug(__FILE__
": pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
693 pa_stream_set_state_callback(i
->stream
, stream_state_cb
, i
);
694 pa_stream_set_write_callback(i
->stream
, stream_request_cb
, i
);
695 pa_stream_set_latency_update_callback(i
->stream
, stream_latency_update_cb
, i
);
697 memset(&attr
, 0, sizeof(attr
));
698 attr
.maxlength
= i
->fragment_size
* (i
->n_fragments
+1);
699 attr
.tlength
= i
->fragment_size
* i
->n_fragments
;
700 attr
.prebuf
= i
->fragment_size
;
701 attr
.minreq
= i
->fragment_size
;
703 if (pa_stream_connect_playback(i
->stream
, NULL
, &attr
, PA_STREAM_INTERPOLATE_TIMING
|PA_STREAM_AUTO_TIMING_UPDATE
, NULL
, NULL
) < 0) {
704 debug(__FILE__
": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
708 n
= i
->fragment_size
;
709 setsockopt(i
->app_fd
, SOL_SOCKET
, SO_SNDBUF
, &n
, sizeof(n
));
710 n
= i
->fragment_size
;
711 setsockopt(i
->thread_fd
, SOL_SOCKET
, SO_RCVBUF
, &n
, sizeof(n
));
719 static void free_stream(fd_info
*i
) {
723 pa_stream_disconnect(i
->stream
);
724 pa_stream_unref(i
->stream
);
729 static void io_event_cb(pa_mainloop_api
*api
, pa_io_event
*e
, int fd
, pa_io_event_flags_t flags
, void *userdata
) {
730 fd_info
*i
= userdata
;
732 pa_threaded_mainloop_signal(i
->mainloop
, 0);
734 if (flags
& PA_IO_EVENT_INPUT
) {
737 api
->io_enable(e
, 0);
739 if (create_stream(i
) < 0)
743 if (fd_info_copy_data(i
, 0) < 0)
747 } else if (flags
& (PA_IO_EVENT_HANGUP
|PA_IO_EVENT_ERROR
))
753 /* We can't do anything better than removing the event source */
757 static int dsp_open(int flags
, int *_errno
) {
759 pa_mainloop_api
*api
;
763 if ((flags
!= O_WRONLY
) && (flags
!= (O_WRONLY
|O_NONBLOCK
))) {
768 if (!(i
= fd_info_new(FD_INFO_PLAYBACK
, _errno
)))
771 shutdown(i
->thread_fd
, SHUT_WR
);
772 shutdown(i
->app_fd
, SHUT_RD
);
774 if ((flags
& O_NONBLOCK
) == O_NONBLOCK
) {
775 if ((f
= fcntl(i
->app_fd
, F_GETFL
)) >= 0)
776 fcntl(i
->app_fd
, F_SETFL
, f
|O_NONBLOCK
);
778 if ((f
= fcntl(i
->thread_fd
, F_GETFL
)) >= 0)
779 fcntl(i
->thread_fd
, F_SETFL
, f
|O_NONBLOCK
);
781 fcntl(i
->app_fd
, F_SETFD
, FD_CLOEXEC
);
782 fcntl(i
->thread_fd
, F_SETFD
, FD_CLOEXEC
);
784 pa_threaded_mainloop_lock(i
->mainloop
);
785 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
786 if (!(i
->io_event
= api
->io_new(api
, i
->thread_fd
, PA_IO_EVENT_INPUT
, io_event_cb
, i
)))
789 pa_threaded_mainloop_unlock(i
->mainloop
);
791 debug(__FILE__
": dsp_open() succeeded, fd=%i\n", i
->app_fd
);
793 fd_info_add_to_list(i
);
800 pa_threaded_mainloop_unlock(i
->mainloop
);
807 debug(__FILE__
": dsp_open() failed\n");
812 static void sink_info_cb(pa_context
*context
, const pa_sink_info
*si
, int eol
, void *userdata
) {
813 fd_info
*i
= userdata
;
815 if (!si
&& eol
< 0) {
816 i
->operation_success
= 0;
817 pa_threaded_mainloop_signal(i
->mainloop
, 0);
824 if (!pa_cvolume_equal(&i
->volume
, &si
->volume
))
825 i
->volume_modify_count
++;
827 i
->volume
= si
->volume
;
828 i
->sink_index
= si
->index
;
830 i
->operation_success
= 1;
831 pa_threaded_mainloop_signal(i
->mainloop
, 0);
834 static void subscribe_cb(pa_context
*context
, pa_subscription_event_type_t t
, uint32_t idx
, void *userdata
) {
835 fd_info
*i
= userdata
;
836 pa_operation
*o
= NULL
;
838 if (i
->sink_index
!= idx
)
841 if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) != PA_SUBSCRIPTION_EVENT_CHANGE
)
844 if (!(o
= pa_context_get_sink_info_by_index(i
->context
, i
->sink_index
, sink_info_cb
, i
))) {
845 debug(__FILE__
": Failed to get sink info: %s", pa_strerror(pa_context_errno(i
->context
)));
849 pa_operation_unref(o
);
852 static int mixer_open(int flags
, int *_errno
) {
857 if (!(i
= fd_info_new(FD_INFO_MIXER
, _errno
)))
860 pa_threaded_mainloop_lock(i
->mainloop
);
862 pa_context_set_subscribe_callback(i
->context
, subscribe_cb
, i
);
864 if (!(o
= pa_context_subscribe(i
->context
, PA_SUBSCRIPTION_MASK_SINK
, context_success_cb
, i
))) {
865 debug(__FILE__
": Failed to subscribe to events: %s", pa_strerror(pa_context_errno(i
->context
)));
870 i
->operation_success
= 0;
871 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
872 pa_threaded_mainloop_wait(i
->mainloop
);
873 CONTEXT_CHECK_DEAD_GOTO(i
, fail
);
876 if (!i
->operation_success
) {
877 debug(__FILE__
":Failed to subscribe to events: %s", pa_strerror(pa_context_errno(i
->context
)));
884 pa_operation_unref(o
);
885 if (!(o
= pa_context_get_sink_info_by_name(i
->context
, NULL
, sink_info_cb
, i
))) {
886 debug(__FILE__
": Failed to get sink info: %s", pa_strerror(pa_context_errno(i
->context
)));
891 i
->operation_success
= 0;
892 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
893 pa_threaded_mainloop_wait(i
->mainloop
);
894 CONTEXT_CHECK_DEAD_GOTO(i
, fail
);
897 if (!i
->operation_success
) {
898 debug(__FILE__
": Failed to get sink info: %s", pa_strerror(pa_context_errno(i
->context
)));
903 pa_threaded_mainloop_unlock(i
->mainloop
);
905 debug(__FILE__
": mixer_open() succeeded, fd=%i\n", i
->app_fd
);
907 fd_info_add_to_list(i
);
914 pa_threaded_mainloop_unlock(i
->mainloop
);
921 debug(__FILE__
": mixer_open() failed\n");
926 static int sndstat_open(int flags
, int *_errno
) {
927 static const char sndstat
[] =
928 "Sound Driver:3.8.1a-980706 (Polypaudio Virtual OSS)\n"
930 "Config options: 0\n"
932 "Installed drivers:\n"
933 "Type 255: Polypaudio Virtual OSS\n"
936 "Polypaudio Virtual OSS\n"
939 "0: Polypaudio Virtual OSS\n"
941 "Synth devices: NOT ENABLED IN CONFIG\n"
948 "0: Polypaudio Virtual OSS\n";
950 char fn
[] = "/tmp/padsp-sndstat-XXXXXX";
955 debug(__FILE__
": sndstat_open()\n");
957 if (flags
!= O_RDONLY
&& flags
!= (O_RDONLY
|O_LARGEFILE
)) {
959 debug(__FILE__
": bad access!\n");
970 debug(__FILE__
": mkstemp() failed: %s\n", strerror(errno
));
976 if (write(fd
, sndstat
, sizeof(sndstat
) -1) != sizeof(sndstat
)-1) {
978 debug(__FILE__
": write() failed: %s\n", strerror(errno
));
982 if (lseek(fd
, SEEK_SET
, 0) < 0) {
984 debug(__FILE__
": lseek() failed: %s\n", strerror(errno
));
996 int open(const char *filename
, int flags
, ...) {
1001 debug(__FILE__
": open(%s)\n", filename
);
1003 va_start(args
, flags
);
1004 if (flags
& O_CREAT
)
1005 mode
= va_arg(args
, mode_t
);
1008 if (!function_enter()) {
1010 return _open(filename
, flags
, mode
);
1013 if (strcmp(filename
, "/dev/dsp") == 0 || strcmp(filename
, "/dev/adsp") == 0) {
1014 r
= dsp_open(flags
, &_errno
);
1015 } else if (strcmp(filename
, "/dev/mixer") == 0) {
1016 r
= mixer_open(flags
, &_errno
);
1017 } else if (strcmp(filename
, "/dev/sndstat") == 0) {
1018 r
= sndstat_open(flags
, &_errno
);
1022 return _open(filename
, flags
, mode
);
1033 static int mixer_ioctl(fd_info
*i
, unsigned long request
, void*argp
, int *_errno
) {
1037 case SOUND_MIXER_READ_DEVMASK
:
1038 debug(__FILE__
": SOUND_MIXER_READ_DEVMASK\n");
1040 *(int*) argp
= SOUND_MASK_PCM
;
1043 case SOUND_MIXER_READ_RECMASK
:
1044 debug(__FILE__
": SOUND_MIXER_READ_RECMASK\n");
1049 case SOUND_MIXER_READ_STEREODEVS
:
1050 debug(__FILE__
": SOUND_MIXER_READ_STEREODEVS\n");
1052 pa_threaded_mainloop_lock(i
->mainloop
);
1053 *(int*) argp
= i
->volume
.channels
> 1 ? SOUND_MASK_PCM
: 0;
1054 pa_threaded_mainloop_unlock(i
->mainloop
);
1058 case SOUND_MIXER_READ_RECSRC
:
1059 debug(__FILE__
": SOUND_MIXER_READ_RECSRC\n");
1064 case SOUND_MIXER_CAPS
:
1065 debug(__FILE__
": SOUND_MIXER_CAPS\n");
1070 case SOUND_MIXER_READ_PCM
:
1072 debug(__FILE__
": SOUND_MIXER_READ_PCM\n");
1074 pa_threaded_mainloop_lock(i
->mainloop
);
1077 ((i
->volume
.values
[0]*100/PA_VOLUME_NORM
)) |
1078 ((i
->volume
.values
[i
->volume
.channels
> 1 ? 1 : 0]*100/PA_VOLUME_NORM
) << 8);
1080 pa_threaded_mainloop_unlock(i
->mainloop
);
1084 case SOUND_MIXER_WRITE_PCM
: {
1087 debug(__FILE__
": SOUND_MIXER_WRITE_PCM\n");
1089 pa_threaded_mainloop_lock(i
->mainloop
);
1093 i
->volume
.values
[0] = ((*(int*) argp
& 0xFF)*PA_VOLUME_NORM
)/100;
1094 i
->volume
.values
[1] = ((*(int*) argp
>> 8)*PA_VOLUME_NORM
)/100;
1096 if (!pa_cvolume_equal(&i
->volume
, &v
)) {
1099 if (!(o
= pa_context_set_sink_volume_by_index(i
->context
, i
->sink_index
, &i
->volume
, NULL
, NULL
)))
1100 debug(__FILE__
":Failed set volume: %s", pa_strerror(pa_context_errno(i
->context
)));
1103 i
->operation_success
= 0;
1104 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1105 CONTEXT_CHECK_DEAD_GOTO(i
, exit_loop
);
1107 pa_threaded_mainloop_wait(i
->mainloop
);
1111 if (!i
->operation_success
)
1112 debug(__FILE__
": Failed to set volume: %s\n", pa_strerror(pa_context_errno(i
->context
)));
1114 pa_operation_unref(o
);
1117 /* We don't wait for completion here */
1118 i
->volume_modify_count
++;
1121 pa_threaded_mainloop_unlock(i
->mainloop
);
1126 case SOUND_MIXER_INFO
: {
1127 mixer_info
*mi
= argp
;
1129 memset(mi
, 0, sizeof(mixer_info
));
1130 strncpy(mi
->id
, "POLYPAUDIO", sizeof(mi
->id
));
1131 strncpy(mi
->name
, "Polypaudio Virtual OSS", sizeof(mi
->name
));
1132 pa_threaded_mainloop_lock(i
->mainloop
);
1133 mi
->modify_counter
= i
->volume_modify_count
;
1134 pa_threaded_mainloop_unlock(i
->mainloop
);
1139 debug(__FILE__
": unknwon ioctl 0x%08lx\n", request
);
1152 static int map_format(int *fmt
, pa_sample_spec
*ss
) {
1156 ss
->format
= PA_SAMPLE_ULAW
;
1160 ss
->format
= PA_SAMPLE_ALAW
;
1167 ss
->format
= PA_SAMPLE_U8
;
1174 ss
->format
= PA_SAMPLE_S16BE
;
1181 ss
->format
= PA_SAMPLE_S16LE
;
1185 ss
->format
= PA_SAMPLE_S16NE
;
1193 static int map_format_back(pa_sample_format_t format
) {
1195 case PA_SAMPLE_S16LE
: return AFMT_S16_LE
;
1196 case PA_SAMPLE_S16BE
: return AFMT_S16_BE
;
1197 case PA_SAMPLE_ULAW
: return AFMT_MU_LAW
;
1198 case PA_SAMPLE_ALAW
: return AFMT_A_LAW
;
1199 case PA_SAMPLE_U8
: return AFMT_U8
;
1205 static int dsp_flush_socket(fd_info
*i
) {
1208 if (i
->thread_fd
< 0)
1211 if (ioctl(i
->thread_fd
, SIOCINQ
, &l
) < 0) {
1212 debug(__FILE__
": SIOCINQ: %s\n", strerror(errno
));
1220 k
= (size_t) l
> sizeof(buf
) ? sizeof(buf
) : (size_t) l
;
1221 if (read(i
->thread_fd
, buf
, k
) < 0)
1222 debug(__FILE__
": read(): %s\n", strerror(errno
));
1229 static int dsp_empty_socket(fd_info
*i
) {
1232 /* Empty the socket */
1236 if (i
->thread_fd
< 0)
1239 if (ioctl(i
->thread_fd
, SIOCINQ
, &l
) < 0) {
1240 debug(__FILE__
": SIOCINQ: %s\n", strerror(errno
));
1249 pa_threaded_mainloop_wait(i
->mainloop
);
1255 static int dsp_drain(fd_info
*i
) {
1256 pa_operation
*o
= NULL
;
1262 debug(__FILE__
": Draining.\n");
1264 pa_threaded_mainloop_lock(i
->mainloop
);
1266 if (dsp_empty_socket(i
) < 0)
1272 debug(__FILE__
": Really draining.\n");
1274 if (!(o
= pa_stream_drain(i
->stream
, stream_success_cb
, i
))) {
1275 debug(__FILE__
": pa_stream_drain(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1279 i
->operation_success
= 0;
1280 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1281 STREAM_CHECK_DEAD_GOTO(i
, fail
);
1283 pa_threaded_mainloop_wait(i
->mainloop
);
1286 if (!i
->operation_success
) {
1287 debug(__FILE__
": pa_stream_drain() 2: %s\n", pa_strerror(pa_context_errno(i
->context
)));
1296 pa_operation_unref(o
);
1298 pa_threaded_mainloop_unlock(i
->mainloop
);
1303 static int dsp_trigger(fd_info
*i
) {
1304 pa_operation
*o
= NULL
;
1310 pa_threaded_mainloop_lock(i
->mainloop
);
1312 if (dsp_empty_socket(i
) < 0)
1315 debug(__FILE__
": Triggering.\n");
1317 if (!(o
= pa_stream_trigger(i
->stream
, stream_success_cb
, i
))) {
1318 debug(__FILE__
": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1322 i
->operation_success
= 0;
1323 while (!pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1324 STREAM_CHECK_DEAD_GOTO(i
, fail
);
1326 pa_threaded_mainloop_wait(i
->mainloop
);
1329 if (!i
->operation_success
) {
1330 debug(__FILE__
": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1339 pa_operation_unref(o
);
1341 pa_threaded_mainloop_unlock(i
->mainloop
);
1346 static int dsp_ioctl(fd_info
*i
, unsigned long request
, void*argp
, int *_errno
) {
1350 case SNDCTL_DSP_SETFMT
: {
1351 debug(__FILE__
": SNDCTL_DSP_SETFMT: %i\n", *(int*) argp
);
1353 pa_threaded_mainloop_lock(i
->mainloop
);
1355 if (*(int*) argp
== AFMT_QUERY
)
1356 *(int*) argp
= map_format_back(i
->sample_spec
.format
);
1358 map_format((int*) argp
, &i
->sample_spec
);
1362 pa_threaded_mainloop_unlock(i
->mainloop
);
1366 case SNDCTL_DSP_SPEED
: {
1371 debug(__FILE__
": SNDCTL_DSP_SPEED: %i\n", *(int*) argp
);
1373 pa_threaded_mainloop_lock(i
->mainloop
);
1375 ss
= i
->sample_spec
;
1376 ss
.rate
= *(int*) argp
;
1378 if ((valid
= pa_sample_spec_valid(&ss
))) {
1379 i
->sample_spec
= ss
;
1383 debug(__FILE__
": ss: %s\n", pa_sample_spec_snprint(t
, sizeof(t
), &i
->sample_spec
));
1385 pa_threaded_mainloop_unlock(i
->mainloop
);
1395 case SNDCTL_DSP_STEREO
:
1396 debug(__FILE__
": SNDCTL_DSP_STEREO: %i\n", *(int*) argp
);
1398 pa_threaded_mainloop_lock(i
->mainloop
);
1400 i
->sample_spec
.channels
= *(int*) argp
? 2 : 1;
1403 pa_threaded_mainloop_unlock(i
->mainloop
);
1406 case SNDCTL_DSP_CHANNELS
: {
1410 debug(__FILE__
": SNDCTL_DSP_CHANNELS: %i\n", *(int*) argp
);
1412 pa_threaded_mainloop_lock(i
->mainloop
);
1414 ss
= i
->sample_spec
;
1415 ss
.channels
= *(int*) argp
;
1417 if ((valid
= pa_sample_spec_valid(&ss
))) {
1418 i
->sample_spec
= ss
;
1422 pa_threaded_mainloop_unlock(i
->mainloop
);
1432 case SNDCTL_DSP_GETBLKSIZE
:
1433 debug(__FILE__
": SNDCTL_DSP_GETBLKSIZE\n");
1435 pa_threaded_mainloop_lock(i
->mainloop
);
1438 *(int*) argp
= i
->fragment_size
;
1440 pa_threaded_mainloop_unlock(i
->mainloop
);
1444 case SNDCTL_DSP_SETFRAGMENT
:
1445 debug(__FILE__
": SNDCTL_DSP_SETFRAGMENT: 0x%8x\n", *(int*) argp
);
1447 pa_threaded_mainloop_lock(i
->mainloop
);
1449 i
->fragment_size
= 1 << (*(int*) argp
);
1450 i
->n_fragments
= (*(int*) argp
) >> 16;
1454 pa_threaded_mainloop_unlock(i
->mainloop
);
1458 case SNDCTL_DSP_GETCAPS
:
1459 debug(__FILE__
": SNDCTL_DSP_CAPS\n");
1461 *(int*) argp
= DSP_CAP_MULTI
;
1464 case SNDCTL_DSP_GETODELAY
: {
1467 debug(__FILE__
": SNDCTL_DSP_GETODELAY\n");
1469 pa_threaded_mainloop_lock(i
->mainloop
);
1476 STREAM_CHECK_DEAD_GOTO(i
, exit_loop
);
1478 if (pa_stream_get_latency(i
->stream
, &usec
, NULL
) >= 0) {
1479 *(int*) argp
= pa_usec_to_bytes(usec
, &i
->sample_spec
);
1483 if (pa_context_errno(i
->context
) != PA_ERR_NODATA
) {
1484 debug(__FILE__
": pa_stream_get_latency(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1488 pa_threaded_mainloop_wait(i
->mainloop
);
1493 if (ioctl(i
->thread_fd
, SIOCINQ
, &l
) < 0)
1494 debug(__FILE__
": SIOCINQ failed: %s\n", strerror(errno
));
1498 pa_threaded_mainloop_unlock(i
->mainloop
);
1500 debug(__FILE__
": ODELAY: %i\n", *(int*) argp
);
1505 case SNDCTL_DSP_RESET
: {
1506 debug(__FILE__
": SNDCTL_DSP_RESET\n");
1508 pa_threaded_mainloop_lock(i
->mainloop
);
1511 dsp_flush_socket(i
);
1514 pa_threaded_mainloop_unlock(i
->mainloop
);
1518 case SNDCTL_DSP_GETFMTS
: {
1519 debug(__FILE__
": SNDCTL_DSP_GETFMTS\n");
1521 *(int*) argp
= AFMT_MU_LAW
|AFMT_A_LAW
|AFMT_U8
|AFMT_S16_LE
|AFMT_S16_BE
;
1525 case SNDCTL_DSP_POST
:
1526 debug(__FILE__
": SNDCTL_DSP_POST\n");
1528 if (dsp_trigger(i
) < 0)
1532 case SNDCTL_DSP_SYNC
:
1533 debug(__FILE__
": SNDCTL_DSP_SYNC\n");
1535 if (dsp_drain(i
) < 0)
1540 case SNDCTL_DSP_GETOSPACE
: {
1541 audio_buf_info
*bi
= (audio_buf_info
*) argp
;
1545 debug(__FILE__
": SNDCTL_DSP_GETOSPACE\n");
1547 pa_threaded_mainloop_lock(i
->mainloop
);
1552 if ((k
= pa_stream_writable_size(i
->stream
)) == (size_t) -1)
1553 debug(__FILE__
": pa_stream_writable_size(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1555 k
= i
->fragment_size
* i
->n_fragments
;
1557 if (ioctl(i
->thread_fd
, SIOCINQ
, &l
) < 0) {
1558 debug(__FILE__
": SIOCINQ failed: %s\n", strerror(errno
));
1562 bi
->fragsize
= i
->fragment_size
;
1563 bi
->fragstotal
= i
->n_fragments
;
1564 bi
->bytes
= k
> (size_t) l
? k
- l
: 0;
1565 bi
->fragments
= bi
->bytes
/ bi
->fragsize
;
1567 pa_threaded_mainloop_unlock(i
->mainloop
);
1569 debug(__FILE__
": fragsize=%i, fragstotal=%i, bytes=%i, fragments=%i\n", bi
->fragsize
, bi
->fragstotal
, bi
->bytes
, bi
->fragments
);
1575 debug(__FILE__
": unknwon ioctl 0x%08lx\n", request
);
1588 int ioctl(int fd
, unsigned long request
, ...) {
1594 debug(__FILE__
": ioctl()\n");
1596 va_start(args
, request
);
1597 argp
= va_arg(args
, void *);
1600 if (!function_enter()) {
1602 return _ioctl(fd
, request
, argp
);
1605 if (!(i
= fd_info_find(fd
))) {
1608 return _ioctl(fd
, request
, argp
);
1611 if (i
->type
== FD_INFO_MIXER
)
1612 r
= mixer_ioctl(i
, request
, argp
, &_errno
);
1614 r
= dsp_ioctl(i
, request
, argp
, &_errno
);
1629 debug(__FILE__
": close()\n");
1631 if (!function_enter()) {
1636 if (!(i
= fd_info_find(fd
))) {
1642 fd_info_remove_from_list(i
);
1650 int open64(const char *filename
, int flags
, ...) {
1654 debug(__FILE__
": open64(%s)\n", filename
);
1656 va_start(args
, flags
);
1657 if (flags
& O_CREAT
)
1658 mode
= va_arg(args
, mode_t
);
1661 if (strcmp(filename
, "/dev/dsp") != 0 &&
1662 strcmp(filename
, "/dev/adsp") != 0 &&
1663 strcmp(filename
, "/dev/sndstat") != 0 &&
1664 strcmp(filename
, "/dev/mixer") != 0) {
1666 return _open64(filename
, flags
, mode
);
1669 return open(filename
, flags
, mode
);
1672 FILE* fopen(const char *filename
, const char *mode
) {
1677 debug(__FILE__
": fopen(%s)\n", filename
);
1679 if (strcmp(filename
, "/dev/dsp") == 0 ||
1680 strcmp(filename
, "/dev/adsp") == 0) {
1682 if (strcmp(mode
, "wb") != 0) {
1688 } else if (strcmp(filename
, "/dev/sndstat") == 0) {
1690 if (strcmp(mode
, "r") != 0) {
1696 } else if (strcmp(filename
, "/dev/mixer") != 0)
1700 return _fopen(filename
, mode
);
1703 if ((fd
= open(filename
, m
)) < 0)
1706 if (!(f
= fdopen(fd
, mode
))) {
1714 FILE *fopen64(const char *filename
, const char *mode
) {
1716 debug(__FILE__
": fopen64(%s)\n", filename
);
1718 if (strcmp(filename
, "/dev/dsp") != 0 &&
1719 strcmp(filename
, "/dev/adsp") != 0 &&
1720 strcmp(filename
, "/dev/sndstat") != 0 &&
1721 strcmp(filename
, "/dev/mixer") != 0) {
1722 LOAD_FOPEN64_FUNC();
1723 return _fopen64(filename
, mode
);
1726 return fopen(filename
, mode
);
1729 int fclose(FILE *f
) {
1732 debug(__FILE__
": fclose()\n");
1734 if (!function_enter()) {
1739 if (!(i
= fd_info_find(fileno(f
)))) {
1745 fd_info_remove_from_list(i
);
1747 /* Dirty trick to avoid that the fd is not freed twice, once by us
1748 * and once by the real fclose() */