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 #define LOAD_IOCTL_FUNC() \
107 pthread_mutex_lock(&func_mutex); \
109 _ioctl = (int (*)(int, int, void*)) dlsym(RTLD_NEXT, "ioctl"); \
110 pthread_mutex_unlock(&func_mutex); \
113 #define LOAD_OPEN_FUNC() \
115 pthread_mutex_lock(&func_mutex); \
117 _open = (int (*)(const char *, int, mode_t)) dlsym(RTLD_NEXT, "open"); \
118 pthread_mutex_unlock(&func_mutex); \
121 #define LOAD_OPEN64_FUNC() \
123 pthread_mutex_lock(&func_mutex); \
125 _open64 = (int (*)(const char *, int, mode_t)) dlsym(RTLD_NEXT, "open64"); \
126 pthread_mutex_unlock(&func_mutex); \
129 #define LOAD_CLOSE_FUNC() \
131 pthread_mutex_lock(&func_mutex); \
133 _close = (int (*)(int)) dlsym(RTLD_NEXT, "close"); \
134 pthread_mutex_unlock(&func_mutex); \
137 #define LOAD_FOPEN_FUNC() \
139 pthread_mutex_lock(&func_mutex); \
141 _fopen = (FILE* (*)(const char *, const char*)) dlsym(RTLD_NEXT, "fopen"); \
142 pthread_mutex_unlock(&func_mutex); \
145 #define LOAD_FOPEN64_FUNC() \
147 pthread_mutex_lock(&func_mutex); \
149 _fopen64 = (FILE* (*)(const char *, const char*)) dlsym(RTLD_NEXT, "fopen64"); \
150 pthread_mutex_unlock(&func_mutex); \
153 #define LOAD_FCLOSE_FUNC() \
155 pthread_mutex_lock(&func_mutex); \
157 _fclose = (int (*)(FILE *)) dlsym(RTLD_NEXT, "fclose"); \
158 pthread_mutex_unlock(&func_mutex); \
161 #define CONTEXT_CHECK_DEAD_GOTO(i, label) do { \
162 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY) { \
163 debug(__FILE__": Not connected: %s", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
168 #define STREAM_CHECK_DEAD_GOTO(i, label) do { \
169 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY || \
170 !(i)->stream || pa_stream_get_state((i)->stream) != PA_STREAM_READY) { \
171 debug(__FILE__": Not connected: %s", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
176 static void debug(const char *format
, ...) PA_GCC_PRINTF_ATTR(1,2);
178 static void debug(const char *format
, ...) {
180 if (getenv("PADSP_DEBUG")) {
181 va_start(ap
, format
);
182 vfprintf(stderr
, format
, ap
);
187 static pthread_key_t recursion_key
;
189 static void recursion_key_alloc(void) {
190 pthread_key_create(&recursion_key
, NULL
);
193 static int function_enter(void) {
194 /* Avoid recursive calls */
195 static pthread_once_t recursion_key_once
= PTHREAD_ONCE_INIT
;
196 pthread_once(&recursion_key_once
, recursion_key_alloc
);
198 if (pthread_getspecific(recursion_key
))
201 pthread_setspecific(recursion_key
, (void*) 1);
205 static void function_exit(void) {
206 pthread_setspecific(recursion_key
, NULL
);
209 static void fd_info_free(fd_info
*i
) {
212 debug(__FILE__
": freeing fd info (fd=%i)\n", i
->app_fd
);
217 pa_threaded_mainloop_stop(i
->mainloop
);
220 pa_stream_disconnect(i
->stream
);
221 pa_stream_unref(i
->stream
);
225 pa_context_disconnect(i
->context
);
226 pa_context_unref(i
->context
);
230 pa_threaded_mainloop_free(i
->mainloop
);
232 if (i
->app_fd
>= 0) {
237 if (i
->thread_fd
>= 0) {
239 _close(i
->thread_fd
);
244 pthread_mutex_destroy(&i
->mutex
);
248 static fd_info
*fd_info_ref(fd_info
*i
) {
251 pthread_mutex_lock(&i
->mutex
);
255 /* debug(__FILE__": ref++, now %i\n", i->ref); */
256 pthread_mutex_unlock(&i
->mutex
);
261 static void fd_info_unref(fd_info
*i
) {
263 pthread_mutex_lock(&i
->mutex
);
266 /* debug(__FILE__": ref--, now %i\n", i->ref); */
267 pthread_mutex_unlock(&i
->mutex
);
273 static void context_state_cb(pa_context
*c
, void *userdata
) {
274 fd_info
*i
= userdata
;
277 switch (pa_context_get_state(c
)) {
278 case PA_CONTEXT_READY
:
279 case PA_CONTEXT_TERMINATED
:
280 case PA_CONTEXT_FAILED
:
281 pa_threaded_mainloop_signal(i
->mainloop
, 0);
284 case PA_CONTEXT_UNCONNECTED
:
285 case PA_CONTEXT_CONNECTING
:
286 case PA_CONTEXT_AUTHORIZING
:
287 case PA_CONTEXT_SETTING_NAME
:
292 static void reset_params(fd_info
*i
) {
295 i
->sample_spec
.format
= PA_SAMPLE_ULAW
;
296 i
->sample_spec
.channels
= 1;
297 i
->sample_spec
.rate
= 8000;
298 i
->fragment_size
= 1024;
302 static char *client_name(char *buf
, size_t n
) {
305 if (pa_get_binary_name(p
, sizeof(p
)))
306 snprintf(buf
, n
, "oss[%s]", pa_path_get_filename(p
));
308 snprintf(buf
, n
, "oss");
313 static void atfork_prepare(void) {
316 debug(__FILE__
": atfork_prepare() enter\n");
320 pthread_mutex_lock(&fd_infos_mutex
);
322 for (i
= fd_infos
; i
; i
= i
->next
) {
323 pthread_mutex_lock(&i
->mutex
);
324 pa_threaded_mainloop_lock(i
->mainloop
);
327 pthread_mutex_lock(&func_mutex
);
330 debug(__FILE__
": atfork_prepare() exit\n");
333 static void atfork_parent(void) {
336 debug(__FILE__
": atfork_parent() enter\n");
338 pthread_mutex_unlock(&func_mutex
);
340 for (i
= fd_infos
; i
; i
= i
->next
) {
341 pa_threaded_mainloop_unlock(i
->mainloop
);
342 pthread_mutex_unlock(&i
->mutex
);
345 pthread_mutex_unlock(&fd_infos_mutex
);
349 debug(__FILE__
": atfork_parent() exit\n");
352 static void atfork_child(void) {
355 debug(__FILE__
": atfork_child() enter\n");
357 /* We do only the bare minimum to get all fds closed */
358 pthread_mutex_init(&func_mutex
, NULL
);
359 pthread_mutex_init(&fd_infos_mutex
, NULL
);
361 for (i
= fd_infos
; i
; i
= i
->next
) {
362 pthread_mutex_init(&i
->mutex
, NULL
);
365 pa_context_disconnect(i
->context
);
366 pa_context_unref(i
->context
);
371 pa_stream_unref(i
->stream
);
375 if (i
->app_fd
>= 0) {
380 if (i
->thread_fd
>= 0) {
390 debug(__FILE__
": atfork_child() exit\n");
393 static void install_atfork(void) {
394 pthread_atfork(atfork_prepare
, atfork_parent
, atfork_child
);
397 static void stream_success_cb(pa_stream
*s
, int success
, void *userdata
) {
398 fd_info
*i
= userdata
;
403 i
->operation_success
= success
;
404 pa_threaded_mainloop_signal(i
->mainloop
, 0);
407 static void context_success_cb(pa_context
*c
, int success
, void *userdata
) {
408 fd_info
*i
= userdata
;
413 i
->operation_success
= success
;
414 pa_threaded_mainloop_signal(i
->mainloop
, 0);
417 static fd_info
* fd_info_new(fd_info_type_t type
, int *_errno
) {
419 int sfds
[2] = { -1, -1 };
421 static pthread_once_t install_atfork_once
= PTHREAD_ONCE_INIT
;
423 debug(__FILE__
": fd_info_new()\n");
425 signal(SIGPIPE
, SIG_IGN
); /* Yes, ugly as hell */
427 pthread_once(&install_atfork_once
, install_atfork
);
429 if (!(i
= malloc(sizeof(fd_info
)))) {
434 i
->app_fd
= i
->thread_fd
= -1;
441 pthread_mutex_init(&i
->mutex
, NULL
);
445 pa_cvolume_reset(&i
->volume
, 2);
446 i
->volume_modify_count
= 0;
447 i
->sink_index
= (uint32_t) -1;
448 PA_LLIST_INIT(fd_info
, i
);
452 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, sfds
) < 0) {
454 debug(__FILE__
": socket() failed: %s\n", strerror(errno
));
459 i
->thread_fd
= sfds
[1];
461 if (!(i
->mainloop
= pa_threaded_mainloop_new())) {
463 debug(__FILE__
": pa_threaded_mainloop_new() failed\n");
467 if (!(i
->context
= pa_context_new(pa_threaded_mainloop_get_api(i
->mainloop
), client_name(name
, sizeof(name
))))) {
469 debug(__FILE__
": pa_context_new() failed\n");
473 pa_context_set_state_callback(i
->context
, context_state_cb
, i
);
475 if (pa_context_connect(i
->context
, NULL
, 0, NULL
) < 0) {
476 *_errno
= ECONNREFUSED
;
477 debug(__FILE__
": pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
481 pa_threaded_mainloop_lock(i
->mainloop
);
483 if (pa_threaded_mainloop_start(i
->mainloop
) < 0) {
485 debug(__FILE__
": pa_threaded_mainloop_start() failed\n");
486 goto unlock_and_fail
;
489 /* Wait until the context is ready */
490 pa_threaded_mainloop_wait(i
->mainloop
);
492 if (pa_context_get_state(i
->context
) != PA_CONTEXT_READY
) {
493 *_errno
= ECONNREFUSED
;
494 debug(__FILE__
": pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
495 goto unlock_and_fail
;
498 pa_threaded_mainloop_unlock(i
->mainloop
);
503 pa_threaded_mainloop_unlock(i
->mainloop
);
513 static void fd_info_add_to_list(fd_info
*i
) {
516 pthread_mutex_lock(&fd_infos_mutex
);
517 PA_LLIST_PREPEND(fd_info
, fd_infos
, i
);
518 pthread_mutex_unlock(&fd_infos_mutex
);
523 static void fd_info_remove_from_list(fd_info
*i
) {
526 pthread_mutex_lock(&fd_infos_mutex
);
527 PA_LLIST_REMOVE(fd_info
, fd_infos
, i
);
528 pthread_mutex_unlock(&fd_infos_mutex
);
533 static fd_info
* fd_info_find(int fd
) {
536 pthread_mutex_lock(&fd_infos_mutex
);
538 for (i
= fd_infos
; i
; i
= i
->next
)
539 if (i
->app_fd
== fd
&& !i
->unusable
) {
544 pthread_mutex_unlock(&fd_infos_mutex
);
549 static void fix_metrics(fd_info
*i
) {
551 char t
[PA_SAMPLE_SPEC_SNPRINT_MAX
];
553 fs
= pa_frame_size(&i
->sample_spec
);
554 i
->fragment_size
= (i
->fragment_size
/fs
)*fs
;
556 if (i
->n_fragments
< 2)
559 if (i
->fragment_size
<= 0)
560 if ((i
->fragment_size
= pa_bytes_per_second(&i
->sample_spec
) / 2 / i
->n_fragments
) <= 0)
561 i
->fragment_size
= 1024;
563 debug(__FILE__
": sample spec: %s\n", pa_sample_spec_snprint(t
, sizeof(t
), &i
->sample_spec
));
564 debug(__FILE__
": fixated metrics to %i fragments, %li bytes each.\n", i
->n_fragments
, (long)i
->fragment_size
);
567 static void stream_request_cb(pa_stream
*s
, size_t length
, void *userdata
) {
568 fd_info
*i
= userdata
;
572 pa_mainloop_api
*api
;
573 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
574 api
->io_enable(i
->io_event
, PA_IO_EVENT_INPUT
);
578 static void stream_latency_update_cb(pa_stream
*s
, void *userdata
) {
579 fd_info
*i
= userdata
;
582 pa_threaded_mainloop_signal(i
->mainloop
, 0);
585 static void fd_info_shutdown(fd_info
*i
) {
589 pa_mainloop_api
*api
;
590 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
591 api
->io_free(i
->io_event
);
595 if (i
->thread_fd
>= 0) {
601 static int fd_info_copy_data(fd_info
*i
, int force
) {
607 if ((n
= pa_stream_writable_size(i
->stream
)) == (size_t) -1) {
608 debug(__FILE__
": pa_stream_writable_size(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
612 while (n
>= i
->fragment_size
|| force
) {
616 if (!(i
->buf
= malloc(i
->fragment_size
))) {
617 debug(__FILE__
": malloc() failed.\n");
622 if ((r
= read(i
->thread_fd
, i
->buf
, i
->fragment_size
)) <= 0) {
627 debug(__FILE__
": read(): %s\n", r
== 0 ? "EOF" : strerror(errno
));
631 if (pa_stream_write(i
->stream
, i
->buf
, r
, free
, 0, PA_SEEK_RELATIVE
) < 0) {
632 debug(__FILE__
": pa_stream_write(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
638 assert(n
>= (size_t) r
);
643 pa_mainloop_api
*api
;
644 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
645 api
->io_enable(i
->io_event
, n
>= i
->fragment_size
? PA_IO_EVENT_INPUT
: 0);
651 static void stream_state_cb(pa_stream
*s
, void * userdata
) {
652 fd_info
*i
= userdata
;
655 switch (pa_stream_get_state(s
)) {
657 case PA_STREAM_READY
:
658 debug(__FILE__
": stream established.\n");
661 case PA_STREAM_FAILED
:
662 debug(__FILE__
": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
666 case PA_STREAM_TERMINATED
:
667 case PA_STREAM_UNCONNECTED
:
668 case PA_STREAM_CREATING
:
673 static int create_stream(fd_info
*i
) {
681 if (!(i
->stream
= pa_stream_new(i
->context
, "Audio Stream", &i
->sample_spec
, NULL
))) {
682 debug(__FILE__
": pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
686 pa_stream_set_state_callback(i
->stream
, stream_state_cb
, i
);
687 pa_stream_set_write_callback(i
->stream
, stream_request_cb
, i
);
688 pa_stream_set_latency_update_callback(i
->stream
, stream_latency_update_cb
, i
);
690 memset(&attr
, 0, sizeof(attr
));
691 attr
.maxlength
= i
->fragment_size
* (i
->n_fragments
+1);
692 attr
.tlength
= i
->fragment_size
* i
->n_fragments
;
693 attr
.prebuf
= i
->fragment_size
;
694 attr
.minreq
= i
->fragment_size
;
696 if (pa_stream_connect_playback(i
->stream
, NULL
, &attr
, PA_STREAM_INTERPOLATE_TIMING
|PA_STREAM_AUTO_TIMING_UPDATE
, NULL
, NULL
) < 0) {
697 debug(__FILE__
": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i
->context
)));
701 n
= i
->fragment_size
;
702 setsockopt(i
->app_fd
, SOL_SOCKET
, SO_SNDBUF
, &n
, sizeof(n
));
703 n
= i
->fragment_size
;
704 setsockopt(i
->thread_fd
, SOL_SOCKET
, SO_RCVBUF
, &n
, sizeof(n
));
712 static void free_stream(fd_info
*i
) {
716 pa_stream_disconnect(i
->stream
);
717 pa_stream_unref(i
->stream
);
722 static void io_event_cb(pa_mainloop_api
*api
, pa_io_event
*e
, int fd
, pa_io_event_flags_t flags
, void *userdata
) {
723 fd_info
*i
= userdata
;
725 pa_threaded_mainloop_signal(i
->mainloop
, 0);
727 if (flags
& PA_IO_EVENT_INPUT
) {
730 api
->io_enable(e
, 0);
732 if (create_stream(i
) < 0)
736 if (fd_info_copy_data(i
, 0) < 0)
740 } else if (flags
& (PA_IO_EVENT_HANGUP
|PA_IO_EVENT_ERROR
))
746 /* We can't do anything better than removing the event source */
750 static int dsp_open(int flags
, int *_errno
) {
752 pa_mainloop_api
*api
;
756 if ((flags
!= O_WRONLY
) && (flags
!= (O_WRONLY
|O_NONBLOCK
))) {
761 if (!(i
= fd_info_new(FD_INFO_PLAYBACK
, _errno
)))
764 shutdown(i
->thread_fd
, SHUT_WR
);
765 shutdown(i
->app_fd
, SHUT_RD
);
767 if ((flags
& O_NONBLOCK
) == O_NONBLOCK
) {
768 if ((f
= fcntl(i
->app_fd
, F_GETFL
)) >= 0)
769 fcntl(i
->app_fd
, F_SETFL
, f
|O_NONBLOCK
);
771 if ((f
= fcntl(i
->thread_fd
, F_GETFL
)) >= 0)
772 fcntl(i
->thread_fd
, F_SETFL
, f
|O_NONBLOCK
);
774 fcntl(i
->app_fd
, F_SETFD
, FD_CLOEXEC
);
775 fcntl(i
->thread_fd
, F_SETFD
, FD_CLOEXEC
);
777 pa_threaded_mainloop_lock(i
->mainloop
);
778 api
= pa_threaded_mainloop_get_api(i
->mainloop
);
779 if (!(i
->io_event
= api
->io_new(api
, i
->thread_fd
, PA_IO_EVENT_INPUT
, io_event_cb
, i
)))
782 pa_threaded_mainloop_unlock(i
->mainloop
);
784 debug(__FILE__
": dsp_open() succeeded, fd=%i\n", i
->app_fd
);
786 fd_info_add_to_list(i
);
793 pa_threaded_mainloop_unlock(i
->mainloop
);
800 debug(__FILE__
": dsp_open() failed\n");
805 static void sink_info_cb(pa_context
*context
, const pa_sink_info
*si
, int eol
, void *userdata
) {
806 fd_info
*i
= userdata
;
808 if (!si
&& eol
< 0) {
809 i
->operation_success
= 0;
810 pa_threaded_mainloop_signal(i
->mainloop
, 0);
817 if (!pa_cvolume_equal(&i
->volume
, &si
->volume
))
818 i
->volume_modify_count
++;
820 i
->volume
= si
->volume
;
821 i
->sink_index
= si
->index
;
823 i
->operation_success
= 1;
824 pa_threaded_mainloop_signal(i
->mainloop
, 0);
827 static void subscribe_cb(pa_context
*context
, pa_subscription_event_type_t t
, uint32_t idx
, void *userdata
) {
828 fd_info
*i
= userdata
;
829 pa_operation
*o
= NULL
;
831 if (i
->sink_index
!= idx
)
834 if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) != PA_SUBSCRIPTION_EVENT_CHANGE
)
837 if (!(o
= pa_context_get_sink_info_by_index(i
->context
, i
->sink_index
, sink_info_cb
, i
))) {
838 debug(__FILE__
": Failed to get sink info: %s", pa_strerror(pa_context_errno(i
->context
)));
842 pa_operation_unref(o
);
845 static int mixer_open(int flags
, int *_errno
) {
850 if (!(i
= fd_info_new(FD_INFO_MIXER
, _errno
)))
853 pa_threaded_mainloop_lock(i
->mainloop
);
855 pa_context_set_subscribe_callback(i
->context
, subscribe_cb
, i
);
857 if (!(o
= pa_context_subscribe(i
->context
, PA_SUBSCRIPTION_MASK_SINK
, context_success_cb
, i
))) {
858 debug(__FILE__
": Failed to subscribe to events: %s", pa_strerror(pa_context_errno(i
->context
)));
863 i
->operation_success
= 0;
864 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
865 pa_threaded_mainloop_wait(i
->mainloop
);
866 CONTEXT_CHECK_DEAD_GOTO(i
, fail
);
869 if (!i
->operation_success
) {
870 debug(__FILE__
":Failed to subscribe to events: %s", pa_strerror(pa_context_errno(i
->context
)));
877 pa_operation_unref(o
);
878 if (!(o
= pa_context_get_sink_info_by_name(i
->context
, NULL
, sink_info_cb
, i
))) {
879 debug(__FILE__
": Failed to get sink info: %s", pa_strerror(pa_context_errno(i
->context
)));
884 i
->operation_success
= 0;
885 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
886 pa_threaded_mainloop_wait(i
->mainloop
);
887 CONTEXT_CHECK_DEAD_GOTO(i
, fail
);
890 if (!i
->operation_success
) {
891 debug(__FILE__
": Failed to get sink info: %s", pa_strerror(pa_context_errno(i
->context
)));
896 pa_threaded_mainloop_unlock(i
->mainloop
);
898 debug(__FILE__
": mixer_open() succeeded, fd=%i\n", i
->app_fd
);
900 fd_info_add_to_list(i
);
907 pa_threaded_mainloop_unlock(i
->mainloop
);
914 debug(__FILE__
": mixer_open() failed\n");
919 static int sndstat_open(int flags
, int *_errno
) {
920 static const char sndstat
[] =
921 "Sound Driver:3.8.1a-980706 (Polypaudio Virtual OSS)\n"
923 "Config options: 0\n"
925 "Installed drivers:\n"
926 "Type 255: Polypaudio Virtual OSS\n"
929 "Polypaudio Virtual OSS\n"
932 "0: Polypaudio Virtual OSS\n"
934 "Synth devices: NOT ENABLED IN CONFIG\n"
941 "0: Polypaudio Virtual OSS\n";
943 char fn
[] = "/tmp/padsp-sndstat-XXXXXX";
948 debug(__FILE__
": sndstat_open()\n");
950 if (flags
!= O_RDONLY
&& flags
!= (O_RDONLY
|O_LARGEFILE
)) {
952 debug(__FILE__
": bad access!\n");
963 debug(__FILE__
": mkstemp() failed: %s\n", strerror(errno
));
969 if (write(fd
, sndstat
, sizeof(sndstat
) -1) != sizeof(sndstat
)-1) {
971 debug(__FILE__
": write() failed: %s\n", strerror(errno
));
975 if (lseek(fd
, SEEK_SET
, 0) < 0) {
977 debug(__FILE__
": lseek() failed: %s\n", strerror(errno
));
989 int open(const char *filename
, int flags
, ...) {
994 debug(__FILE__
": open(%s)\n", filename
);
996 va_start(args
, flags
);
998 mode
= va_arg(args
, mode_t
);
1001 if (!function_enter()) {
1003 return _open(filename
, flags
, mode
);
1006 if (strcmp(filename
, "/dev/dsp") == 0 || strcmp(filename
, "/dev/adsp") == 0) {
1007 r
= dsp_open(flags
, &_errno
);
1008 } else if (strcmp(filename
, "/dev/mixer") == 0) {
1009 r
= mixer_open(flags
, &_errno
);
1010 } else if (strcmp(filename
, "/dev/sndstat") == 0) {
1011 r
= sndstat_open(flags
, &_errno
);
1015 return _open(filename
, flags
, mode
);
1026 static int mixer_ioctl(fd_info
*i
, unsigned long request
, void*argp
, int *_errno
) {
1030 case SOUND_MIXER_READ_DEVMASK
:
1031 debug(__FILE__
": SOUND_MIXER_READ_DEVMASK\n");
1033 *(int*) argp
= SOUND_MASK_PCM
;
1036 case SOUND_MIXER_READ_RECMASK
:
1037 debug(__FILE__
": SOUND_MIXER_READ_RECMASK\n");
1042 case SOUND_MIXER_READ_STEREODEVS
:
1043 debug(__FILE__
": SOUND_MIXER_READ_STEREODEVS\n");
1045 pa_threaded_mainloop_lock(i
->mainloop
);
1046 *(int*) argp
= i
->volume
.channels
> 1 ? SOUND_MASK_PCM
: 0;
1047 pa_threaded_mainloop_unlock(i
->mainloop
);
1051 case SOUND_MIXER_READ_RECSRC
:
1052 debug(__FILE__
": SOUND_MIXER_READ_RECSRC\n");
1057 case SOUND_MIXER_CAPS
:
1058 debug(__FILE__
": SOUND_MIXER_CAPS\n");
1063 case SOUND_MIXER_READ_PCM
:
1065 debug(__FILE__
": SOUND_MIXER_READ_PCM\n");
1067 pa_threaded_mainloop_lock(i
->mainloop
);
1070 ((i
->volume
.values
[0]*100/PA_VOLUME_NORM
) << 8) |
1071 ((i
->volume
.values
[i
->volume
.channels
> 1 ? 1 : 0]*100/PA_VOLUME_NORM
));
1073 pa_threaded_mainloop_unlock(i
->mainloop
);
1077 case SOUND_MIXER_WRITE_PCM
: {
1080 debug(__FILE__
": SOUND_MIXER_WRITE_PCM\n");
1082 pa_threaded_mainloop_lock(i
->mainloop
);
1086 i
->volume
.values
[0] = ((*(int*) argp
>> 8)*PA_VOLUME_NORM
)/100;
1087 i
->volume
.values
[1] = ((*(int*) argp
& 0xFF)*PA_VOLUME_NORM
)/100;
1089 if (!pa_cvolume_equal(&i
->volume
, &v
)) {
1092 if (!(o
= pa_context_set_sink_volume_by_index(i
->context
, i
->sink_index
, &i
->volume
, NULL
, NULL
)))
1093 debug(__FILE__
":Failed set volume: %s", pa_strerror(pa_context_errno(i
->context
)));
1095 pa_operation_unref(o
);
1097 /* We don't wait for completion here */
1098 i
->volume_modify_count
++;
1101 pa_threaded_mainloop_unlock(i
->mainloop
);
1106 case SOUND_MIXER_INFO
: {
1107 mixer_info
*mi
= argp
;
1109 memset(mi
, 0, sizeof(mixer_info
));
1110 strncpy(mi
->id
, "POLYPAUDIO", sizeof(mi
->id
));
1111 strncpy(mi
->name
, "Polypaudio Virtual OSS", sizeof(mi
->name
));
1112 pa_threaded_mainloop_lock(i
->mainloop
);
1113 mi
->modify_counter
= i
->volume_modify_count
;
1114 pa_threaded_mainloop_unlock(i
->mainloop
);
1119 debug(__FILE__
": unknwon ioctl 0x%08lx\n", request
);
1132 static int map_format(int *fmt
, pa_sample_spec
*ss
) {
1136 ss
->format
= PA_SAMPLE_ULAW
;
1140 ss
->format
= PA_SAMPLE_ALAW
;
1147 ss
->format
= PA_SAMPLE_U8
;
1154 ss
->format
= PA_SAMPLE_S16BE
;
1161 ss
->format
= PA_SAMPLE_S16LE
;
1165 ss
->format
= PA_SAMPLE_S16NE
;
1173 static int map_format_back(pa_sample_format_t format
) {
1175 case PA_SAMPLE_S16LE
: return AFMT_S16_LE
;
1176 case PA_SAMPLE_S16BE
: return AFMT_S16_BE
;
1177 case PA_SAMPLE_ULAW
: return AFMT_MU_LAW
;
1178 case PA_SAMPLE_ALAW
: return AFMT_A_LAW
;
1179 case PA_SAMPLE_U8
: return AFMT_U8
;
1185 static int dsp_flush_socket(fd_info
*i
) {
1188 if (i
->thread_fd
< 0)
1191 if (ioctl(i
->thread_fd
, SIOCINQ
, &l
) < 0) {
1192 debug(__FILE__
": SIOCINQ: %s\n", strerror(errno
));
1200 k
= (size_t) l
> sizeof(buf
) ? sizeof(buf
) : (size_t) l
;
1201 if (read(i
->thread_fd
, buf
, k
) < 0)
1202 debug(__FILE__
": read(): %s\n", strerror(errno
));
1209 static int dsp_empty_socket(fd_info
*i
) {
1212 /* Empty the socket */
1216 if (i
->thread_fd
< 0)
1219 if (ioctl(i
->thread_fd
, SIOCINQ
, &l
) < 0) {
1220 debug(__FILE__
": SIOCINQ: %s\n", strerror(errno
));
1229 pa_threaded_mainloop_wait(i
->mainloop
);
1235 static int dsp_drain(fd_info
*i
) {
1236 pa_operation
*o
= NULL
;
1242 debug(__FILE__
": Draining.\n");
1244 pa_threaded_mainloop_lock(i
->mainloop
);
1246 if (dsp_empty_socket(i
) < 0)
1252 debug(__FILE__
": Really draining.\n");
1254 if (!(o
= pa_stream_drain(i
->stream
, stream_success_cb
, i
))) {
1255 debug(__FILE__
": pa_stream_drain(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1259 i
->operation_success
= 0;
1260 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1261 STREAM_CHECK_DEAD_GOTO(i
, fail
);
1263 pa_threaded_mainloop_wait(i
->mainloop
);
1266 if (!i
->operation_success
) {
1267 debug(__FILE__
": pa_stream_drain() 2: %s\n", pa_strerror(pa_context_errno(i
->context
)));
1276 pa_operation_unref(o
);
1278 pa_threaded_mainloop_unlock(i
->mainloop
);
1283 static int dsp_trigger(fd_info
*i
) {
1284 pa_operation
*o
= NULL
;
1290 pa_threaded_mainloop_lock(i
->mainloop
);
1292 if (dsp_empty_socket(i
) < 0)
1295 debug(__FILE__
": Triggering.\n");
1297 if (!(o
= pa_stream_trigger(i
->stream
, stream_success_cb
, i
))) {
1298 debug(__FILE__
": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1302 i
->operation_success
= 0;
1303 while (!pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
1304 STREAM_CHECK_DEAD_GOTO(i
, fail
);
1306 pa_threaded_mainloop_wait(i
->mainloop
);
1309 if (!i
->operation_success
) {
1310 debug(__FILE__
": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1319 pa_operation_unref(o
);
1321 pa_threaded_mainloop_unlock(i
->mainloop
);
1326 static int dsp_ioctl(fd_info
*i
, unsigned long request
, void*argp
, int *_errno
) {
1330 case SNDCTL_DSP_SETFMT
: {
1331 debug(__FILE__
": SNDCTL_DSP_SETFMT: %i\n", *(int*) argp
);
1333 pa_threaded_mainloop_lock(i
->mainloop
);
1335 if (*(int*) argp
== AFMT_QUERY
)
1336 *(int*) argp
= map_format_back(i
->sample_spec
.format
);
1338 map_format((int*) argp
, &i
->sample_spec
);
1342 pa_threaded_mainloop_unlock(i
->mainloop
);
1346 case SNDCTL_DSP_SPEED
: {
1351 debug(__FILE__
": SNDCTL_DSP_SPEED: %i\n", *(int*) argp
);
1353 pa_threaded_mainloop_lock(i
->mainloop
);
1355 ss
= i
->sample_spec
;
1356 ss
.rate
= *(int*) argp
;
1358 if ((valid
= pa_sample_spec_valid(&ss
))) {
1359 i
->sample_spec
= ss
;
1363 debug(__FILE__
": ss: %s\n", pa_sample_spec_snprint(t
, sizeof(t
), &i
->sample_spec
));
1365 pa_threaded_mainloop_unlock(i
->mainloop
);
1375 case SNDCTL_DSP_STEREO
:
1376 debug(__FILE__
": SNDCTL_DSP_STEREO: %i\n", *(int*) argp
);
1378 pa_threaded_mainloop_lock(i
->mainloop
);
1380 i
->sample_spec
.channels
= *(int*) argp
? 2 : 1;
1383 pa_threaded_mainloop_unlock(i
->mainloop
);
1386 case SNDCTL_DSP_CHANNELS
: {
1390 debug(__FILE__
": SNDCTL_DSP_CHANNELS: %i\n", *(int*) argp
);
1392 pa_threaded_mainloop_lock(i
->mainloop
);
1394 ss
= i
->sample_spec
;
1395 ss
.channels
= *(int*) argp
;
1397 if ((valid
= pa_sample_spec_valid(&ss
))) {
1398 i
->sample_spec
= ss
;
1402 pa_threaded_mainloop_unlock(i
->mainloop
);
1412 case SNDCTL_DSP_GETBLKSIZE
:
1413 debug(__FILE__
": SNDCTL_DSP_GETBLKSIZE\n");
1415 pa_threaded_mainloop_lock(i
->mainloop
);
1418 *(int*) argp
= i
->fragment_size
;
1420 pa_threaded_mainloop_unlock(i
->mainloop
);
1424 case SNDCTL_DSP_SETFRAGMENT
:
1425 debug(__FILE__
": SNDCTL_DSP_SETFRAGMENT: 0x%8x\n", *(int*) argp
);
1427 pa_threaded_mainloop_lock(i
->mainloop
);
1429 i
->fragment_size
= 1 << (*(int*) argp
);
1430 i
->n_fragments
= (*(int*) argp
) >> 16;
1434 pa_threaded_mainloop_unlock(i
->mainloop
);
1438 case SNDCTL_DSP_GETCAPS
:
1439 debug(__FILE__
": SNDCTL_DSP_CAPS\n");
1441 *(int*) argp
= DSP_CAP_MULTI
;
1444 case SNDCTL_DSP_GETODELAY
: {
1447 debug(__FILE__
": SNDCTL_DSP_GETODELAY\n");
1449 pa_threaded_mainloop_lock(i
->mainloop
);
1456 STREAM_CHECK_DEAD_GOTO(i
, exit_loop
);
1458 if (pa_stream_get_latency(i
->stream
, &usec
, NULL
) >= 0) {
1459 *(int*) argp
= pa_usec_to_bytes(usec
, &i
->sample_spec
);
1463 if (pa_context_errno(i
->context
) != PA_ERR_NODATA
) {
1464 debug(__FILE__
": pa_stream_get_latency(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1468 pa_threaded_mainloop_wait(i
->mainloop
);
1473 if (ioctl(i
->thread_fd
, SIOCINQ
, &l
) < 0)
1474 debug(__FILE__
": SIOCINQ failed: %s\n", strerror(errno
));
1478 pa_threaded_mainloop_unlock(i
->mainloop
);
1480 debug(__FILE__
": ODELAY: %i\n", *(int*) argp
);
1485 case SNDCTL_DSP_RESET
: {
1486 debug(__FILE__
": SNDCTL_DSP_RESET\n");
1488 pa_threaded_mainloop_lock(i
->mainloop
);
1491 dsp_flush_socket(i
);
1494 pa_threaded_mainloop_unlock(i
->mainloop
);
1498 case SNDCTL_DSP_GETFMTS
: {
1499 debug(__FILE__
": SNDCTL_DSP_GETFMTS\n");
1501 *(int*) argp
= AFMT_MU_LAW
|AFMT_A_LAW
|AFMT_U8
|AFMT_S16_LE
|AFMT_S16_BE
;
1505 case SNDCTL_DSP_POST
:
1506 debug(__FILE__
": SNDCTL_DSP_POST\n");
1508 if (dsp_trigger(i
) < 0)
1512 case SNDCTL_DSP_SYNC
:
1513 debug(__FILE__
": SNDCTL_DSP_SYNC\n");
1515 if (dsp_drain(i
) < 0)
1520 case SNDCTL_DSP_GETOSPACE
: {
1521 audio_buf_info
*bi
= (audio_buf_info
*) argp
;
1525 debug(__FILE__
": SNDCTL_DSP_GETOSPACE\n");
1527 pa_threaded_mainloop_lock(i
->mainloop
);
1532 if ((k
= pa_stream_writable_size(i
->stream
)) == (size_t) -1)
1533 debug(__FILE__
": pa_stream_writable_size(): %s\n", pa_strerror(pa_context_errno(i
->context
)));
1535 k
= i
->fragment_size
* i
->n_fragments
;
1537 if (ioctl(i
->thread_fd
, SIOCINQ
, &l
) < 0) {
1538 debug(__FILE__
": SIOCINQ failed: %s\n", strerror(errno
));
1542 bi
->fragsize
= i
->fragment_size
;
1543 bi
->fragstotal
= i
->n_fragments
;
1544 bi
->bytes
= k
> (size_t) l
? k
- l
: 0;
1545 bi
->fragments
= bi
->bytes
/ bi
->fragsize
;
1547 pa_threaded_mainloop_unlock(i
->mainloop
);
1549 debug(__FILE__
": fragsize=%i, fragstotal=%i, bytes=%i, fragments=%i\n", bi
->fragsize
, bi
->fragstotal
, bi
->bytes
, bi
->fragments
);
1555 debug(__FILE__
": unknwon ioctl 0x%08lx\n", request
);
1568 int ioctl(int fd
, unsigned long request
, ...) {
1574 debug(__FILE__
": ioctl()\n");
1576 va_start(args
, request
);
1577 argp
= va_arg(args
, void *);
1580 if (!function_enter()) {
1582 return _ioctl(fd
, request
, argp
);
1585 if (!(i
= fd_info_find(fd
))) {
1588 return _ioctl(fd
, request
, argp
);
1591 if (i
->type
== FD_INFO_MIXER
)
1592 r
= mixer_ioctl(i
, request
, argp
, &_errno
);
1594 r
= dsp_ioctl(i
, request
, argp
, &_errno
);
1609 debug(__FILE__
": close()\n");
1611 if (!function_enter()) {
1616 if (!(i
= fd_info_find(fd
))) {
1622 fd_info_remove_from_list(i
);
1630 int open64(const char *filename
, int flags
, ...) {
1634 debug(__FILE__
": open64(%s)\n", filename
);
1636 va_start(args
, flags
);
1637 if (flags
& O_CREAT
)
1638 mode
= va_arg(args
, mode_t
);
1641 if (strcmp(filename
, "/dev/dsp") != 0 &&
1642 strcmp(filename
, "/dev/adsp") != 0 &&
1643 strcmp(filename
, "/dev/sndstat") != 0 &&
1644 strcmp(filename
, "/dev/mixer") != 0) {
1646 return _open64(filename
, flags
, mode
);
1649 return open(filename
, flags
, mode
);
1652 FILE* fopen(const char *filename
, const char *mode
) {
1657 debug(__FILE__
": fopen(%s)\n", filename
);
1659 if (strcmp(filename
, "/dev/dsp") == 0 ||
1660 strcmp(filename
, "/dev/adsp") == 0) {
1662 if (strcmp(mode
, "wb") != 0) {
1668 } else if (strcmp(filename
, "/dev/sndstat") == 0) {
1670 if (strcmp(mode
, "r") != 0) {
1676 } else if (strcmp(filename
, "/dev/mixer") != 0)
1680 return _fopen(filename
, mode
);
1683 if ((fd
= open(filename
, m
)) < 0)
1686 if (!(f
= fdopen(fd
, mode
))) {
1694 FILE *fopen64(const char *filename
, const char *mode
) {
1696 debug(__FILE__
": fopen64(%s)\n", filename
);
1698 if (strcmp(filename
, "/dev/dsp") != 0 &&
1699 strcmp(filename
, "/dev/adsp") != 0 &&
1700 strcmp(filename
, "/dev/sndstat") != 0 &&
1701 strcmp(filename
, "/dev/mixer") != 0) {
1702 LOAD_FOPEN64_FUNC();
1703 return _fopen64(filename
, mode
);
1706 return fopen(filename
, mode
);
1709 int fclose(FILE *f
) {
1712 debug(__FILE__
": fclose()\n");
1714 if (!function_enter()) {
1719 if (!(i
= fd_info_find(fileno(f
)))) {
1725 fd_info_remove_from_list(i
);
1727 /* Dirty trick to avoid that the fd is not freed twice, once by us
1728 * and once by the real fclose() */