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;
104 static int (*_access)(const char *, int) = NULL;
106 /* dlsym() violates ISO C, so confide the breakage into this function to
108 typedef void (*fnptr)(void);
109 static inline fnptr dlsym_fn(void *handle, const char *symbol) {
110 return (fnptr) (long) dlsym(handle, symbol);
113 #define LOAD_IOCTL_FUNC() \
115 pthread_mutex_lock(&func_mutex); \
117 _ioctl = (int (*)(int, int, void*)) dlsym_fn(RTLD_NEXT, "ioctl"); \
118 pthread_mutex_unlock(&func_mutex); \
121 #define LOAD_OPEN_FUNC() \
123 pthread_mutex_lock(&func_mutex); \
125 _open = (int (*)(const char *, int, mode_t)) dlsym_fn(RTLD_NEXT, "open"); \
126 pthread_mutex_unlock(&func_mutex); \
129 #define LOAD_OPEN64_FUNC() \
131 pthread_mutex_lock(&func_mutex); \
133 _open64 = (int (*)(const char *, int, mode_t)) dlsym_fn(RTLD_NEXT, "open64"); \
134 pthread_mutex_unlock(&func_mutex); \
137 #define LOAD_CLOSE_FUNC() \
139 pthread_mutex_lock(&func_mutex); \
141 _close = (int (*)(int)) dlsym_fn(RTLD_NEXT, "close"); \
142 pthread_mutex_unlock(&func_mutex); \
145 #define LOAD_ACCESS_FUNC() \
147 pthread_mutex_lock(&func_mutex); \
149 _access = (int (*)(const char*, int)) dlsym_fn(RTLD_NEXT, "access"); \
150 pthread_mutex_unlock(&func_mutex); \
153 #define LOAD_FOPEN_FUNC() \
155 pthread_mutex_lock(&func_mutex); \
157 _fopen = (FILE* (*)(const char *, const char*)) dlsym_fn(RTLD_NEXT, "fopen"); \
158 pthread_mutex_unlock(&func_mutex); \
161 #define LOAD_FOPEN64_FUNC() \
163 pthread_mutex_lock(&func_mutex); \
165 _fopen64 = (FILE* (*)(const char *, const char*)) dlsym_fn(RTLD_NEXT, "fopen64"); \
166 pthread_mutex_unlock(&func_mutex); \
169 #define LOAD_FCLOSE_FUNC() \
171 pthread_mutex_lock(&func_mutex); \
173 _fclose = (int (*)(FILE *)) dlsym_fn(RTLD_NEXT, "fclose"); \
174 pthread_mutex_unlock(&func_mutex); \
177 #define CONTEXT_CHECK_DEAD_GOTO(i, label) do { \
178 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY) { \
179 debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
184 #define STREAM_CHECK_DEAD_GOTO(i, label) do { \
185 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY || \
186 !(i)->stream || pa_stream_get_state((i)->stream) != PA_STREAM_READY) { \
187 debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
192 static void debug(int level, const char *format, ...) PA_GCC_PRINTF_ATTR(2,3);
194 #define DEBUG_LEVEL_ALWAYS 0
195 #define DEBUG_LEVEL_NORMAL 1
196 #define DEBUG_LEVEL_VERBOSE 2
198 static void debug(int level, const char *format, ...) {
200 const char *dlevel_s;
203 dlevel_s = getenv("PADSP_DEBUG");
207 dlevel = atoi(dlevel_s);
212 va_start(ap, format);
213 vfprintf(stderr, format, ap);
217 static int padsp_disabled(void) {
219 static int sym_resolved = 0;
221 /* If the current process has a symbol __padsp_disabled__ we use
222 * it to detect whether we should enable our stuff or not. A
223 * program needs to be compiled with -rdynamic for this to work!
224 * The symbol must be an int containing a three bit bitmask: bit 1
225 * -> disable /dev/dsp emulation, bit 2 -> disable /dev/sndstat
226 * emulation, bit 3 -> disable /dev/mixer emulation. Hence a value
227 * of 7 disables padsp entirely. */
229 pthread_mutex_lock(&func_mutex);
231 sym = (int*) dlsym(RTLD_DEFAULT, "__padsp_disabled__");
235 pthread_mutex_unlock(&func_mutex);
243 static int dsp_cloak_enable(void) {
244 if (padsp_disabled() & 1)
247 if (getenv("PADSP_NO_DSP"))
253 static int sndstat_cloak_enable(void) {
254 if (padsp_disabled() & 2)
257 if (getenv("PADSP_NO_SNDSTAT"))
263 static int mixer_cloak_enable(void) {
264 if (padsp_disabled() & 4)
267 if (getenv("PADSP_NO_MIXER"))
272 static pthread_key_t recursion_key;
274 static void recursion_key_alloc(void) {
275 pthread_key_create(&recursion_key, NULL);
278 static int function_enter(void) {
279 /* Avoid recursive calls */
280 static pthread_once_t recursion_key_once = PTHREAD_ONCE_INIT;
281 pthread_once(&recursion_key_once, recursion_key_alloc);
283 if (pthread_getspecific(recursion_key))
286 pthread_setspecific(recursion_key, (void*) 1);
290 static void function_exit(void) {
291 pthread_setspecific(recursion_key, NULL);
294 static void fd_info_free(fd_info *i) {
297 debug(DEBUG_LEVEL_NORMAL, __FILE__": freeing fd info (fd=%i)\n", i->app_fd);
302 pa_threaded_mainloop_stop(i->mainloop);
305 pa_stream_disconnect(i->stream);
306 pa_stream_unref(i->stream);
310 pa_context_disconnect(i->context);
311 pa_context_unref(i->context);
315 pa_threaded_mainloop_free(i->mainloop);
317 if (i->app_fd >= 0) {
322 if (i->thread_fd >= 0) {
324 _close(i->thread_fd);
329 pthread_mutex_destroy(&i->mutex);
333 static fd_info *fd_info_ref(fd_info *i) {
336 pthread_mutex_lock(&i->mutex);
340 debug(DEBUG_LEVEL_VERBOSE, __FILE__": ref++, now %i\n", i->ref);
341 pthread_mutex_unlock(&i->mutex);
346 static void fd_info_unref(fd_info *i) {
348 pthread_mutex_lock(&i->mutex);
351 debug(DEBUG_LEVEL_VERBOSE, __FILE__": ref--, now %i\n", i->ref);
352 pthread_mutex_unlock(&i->mutex);
358 static void context_state_cb(pa_context *c, void *userdata) {
359 fd_info *i = userdata;
362 switch (pa_context_get_state(c)) {
363 case PA_CONTEXT_READY:
364 case PA_CONTEXT_TERMINATED:
365 case PA_CONTEXT_FAILED:
366 pa_threaded_mainloop_signal(i->mainloop, 0);
369 case PA_CONTEXT_UNCONNECTED:
370 case PA_CONTEXT_CONNECTING:
371 case PA_CONTEXT_AUTHORIZING:
372 case PA_CONTEXT_SETTING_NAME:
377 static void reset_params(fd_info *i) {
380 i->sample_spec.format = PA_SAMPLE_ULAW;
381 i->sample_spec.channels = 1;
382 i->sample_spec.rate = 8000;
383 i->fragment_size = 0;
387 static const char *client_name(char *buf, size_t n) {
391 if ((e = getenv("PADSP_CLIENT_NAME")))
394 if (pa_get_binary_name(p, sizeof(p)))
395 snprintf(buf, n, "OSS Emulation[%s]", pa_path_get_filename(p));
397 snprintf(buf, n, "OSS");
402 static const char *stream_name(void) {
405 if ((e = getenv("PADSP_STREAM_NAME")))
408 return "Audio Stream";
411 static void atfork_prepare(void) {
414 debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_prepare() enter\n");
418 pthread_mutex_lock(&fd_infos_mutex);
420 for (i = fd_infos; i; i = i->next) {
421 pthread_mutex_lock(&i->mutex);
422 pa_threaded_mainloop_lock(i->mainloop);
425 pthread_mutex_lock(&func_mutex);
428 debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_prepare() exit\n");
431 static void atfork_parent(void) {
434 debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_parent() enter\n");
436 pthread_mutex_unlock(&func_mutex);
438 for (i = fd_infos; i; i = i->next) {
439 pa_threaded_mainloop_unlock(i->mainloop);
440 pthread_mutex_unlock(&i->mutex);
443 pthread_mutex_unlock(&fd_infos_mutex);
447 debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_parent() exit\n");
450 static void atfork_child(void) {
453 debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_child() enter\n");
455 /* We do only the bare minimum to get all fds closed */
456 pthread_mutex_init(&func_mutex, NULL);
457 pthread_mutex_init(&fd_infos_mutex, NULL);
459 for (i = fd_infos; i; i = i->next) {
460 pthread_mutex_init(&i->mutex, NULL);
463 pa_context_disconnect(i->context);
464 pa_context_unref(i->context);
469 pa_stream_unref(i->stream);
473 if (i->app_fd >= 0) {
478 if (i->thread_fd >= 0) {
488 debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_child() exit\n");
491 static void install_atfork(void) {
492 pthread_atfork(atfork_prepare, atfork_parent, atfork_child);
495 static void stream_success_cb(pa_stream *s, int success, void *userdata) {
496 fd_info *i = userdata;
501 i->operation_success = success;
502 pa_threaded_mainloop_signal(i->mainloop, 0);
505 static void context_success_cb(pa_context *c, int success, void *userdata) {
506 fd_info *i = userdata;
511 i->operation_success = success;
512 pa_threaded_mainloop_signal(i->mainloop, 0);
515 static fd_info* fd_info_new(fd_info_type_t type, int *_errno) {
517 int sfds[2] = { -1, -1 };
519 static pthread_once_t install_atfork_once = PTHREAD_ONCE_INIT;
521 debug(DEBUG_LEVEL_NORMAL, __FILE__": fd_info_new()\n");
523 signal(SIGPIPE, SIG_IGN); /* Yes, ugly as hell */
525 pthread_once(&install_atfork_once, install_atfork);
527 if (!(i = malloc(sizeof(fd_info)))) {
532 i->app_fd = i->thread_fd = -1;
539 pthread_mutex_init(&i->mutex, NULL);
543 pa_cvolume_reset(&i->volume, 2);
544 i->volume_modify_count = 0;
545 i->sink_index = (uint32_t) -1;
546 PA_LLIST_INIT(fd_info, i);
550 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sfds) < 0) {
552 debug(DEBUG_LEVEL_NORMAL, __FILE__": socket() failed: %s\n", strerror(errno));
557 i->thread_fd = sfds[1];
559 if (!(i->mainloop = pa_threaded_mainloop_new())) {
561 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_threaded_mainloop_new() failed\n");
565 if (!(i->context = pa_context_new(pa_threaded_mainloop_get_api(i->mainloop), client_name(name, sizeof(name))))) {
567 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_context_new() failed\n");
571 pa_context_set_state_callback(i->context, context_state_cb, i);
573 if (pa_context_connect(i->context, NULL, 0, NULL) < 0) {
574 *_errno = ECONNREFUSED;
575 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
579 pa_threaded_mainloop_lock(i->mainloop);
581 if (pa_threaded_mainloop_start(i->mainloop) < 0) {
583 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_threaded_mainloop_start() failed\n");
584 goto unlock_and_fail;
587 /* Wait until the context is ready */
588 pa_threaded_mainloop_wait(i->mainloop);
590 if (pa_context_get_state(i->context) != PA_CONTEXT_READY) {
591 *_errno = ECONNREFUSED;
592 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
593 goto unlock_and_fail;
596 pa_threaded_mainloop_unlock(i->mainloop);
601 pa_threaded_mainloop_unlock(i->mainloop);
611 static void fd_info_add_to_list(fd_info *i) {
614 pthread_mutex_lock(&fd_infos_mutex);
615 PA_LLIST_PREPEND(fd_info, fd_infos, i);
616 pthread_mutex_unlock(&fd_infos_mutex);
621 static void fd_info_remove_from_list(fd_info *i) {
624 pthread_mutex_lock(&fd_infos_mutex);
625 PA_LLIST_REMOVE(fd_info, fd_infos, i);
626 pthread_mutex_unlock(&fd_infos_mutex);
631 static fd_info* fd_info_find(int fd) {
634 pthread_mutex_lock(&fd_infos_mutex);
636 for (i = fd_infos; i; i = i->next)
637 if (i->app_fd == fd && !i->unusable) {
642 pthread_mutex_unlock(&fd_infos_mutex);
647 static void fix_metrics(fd_info *i) {
649 char t[PA_SAMPLE_SPEC_SNPRINT_MAX];
651 fs = pa_frame_size(&i->sample_spec);
652 i->fragment_size = (i->fragment_size/fs)*fs;
654 /* Number of fragments set? */
655 if (i->n_fragments < 2) {
656 if (i->fragment_size > 0) {
657 i->n_fragments = pa_bytes_per_second(&i->sample_spec) / 2 / i->fragment_size;
658 if (i->n_fragments < 2)
664 /* Fragment size set? */
665 if (i->fragment_size <= 0) {
666 i->fragment_size = pa_bytes_per_second(&i->sample_spec) / 2 / i->n_fragments;
667 if (i->fragment_size < 1024)
668 i->fragment_size = 1024;
671 debug(DEBUG_LEVEL_NORMAL, __FILE__": sample spec: %s\n", pa_sample_spec_snprint(t, sizeof(t), &i->sample_spec));
672 debug(DEBUG_LEVEL_NORMAL, __FILE__": fixated metrics to %i fragments, %li bytes each.\n", i->n_fragments, (long)i->fragment_size);
675 static void stream_request_cb(pa_stream *s, size_t length, void *userdata) {
676 fd_info *i = userdata;
680 pa_mainloop_api *api;
681 api = pa_threaded_mainloop_get_api(i->mainloop);
682 api->io_enable(i->io_event, PA_IO_EVENT_INPUT);
686 static void stream_latency_update_cb(pa_stream *s, void *userdata) {
687 fd_info *i = userdata;
690 pa_threaded_mainloop_signal(i->mainloop, 0);
693 static void fd_info_shutdown(fd_info *i) {
697 pa_mainloop_api *api;
698 api = pa_threaded_mainloop_get_api(i->mainloop);
699 api->io_free(i->io_event);
703 if (i->thread_fd >= 0) {
709 static int fd_info_copy_data(fd_info *i, int force) {
715 if ((n = pa_stream_writable_size(i->stream)) == (size_t) -1) {
716 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_writable_size(): %s\n", pa_strerror(pa_context_errno(i->context)));
720 while (n >= i->fragment_size || force) {
724 if (!(i->buf = malloc(i->fragment_size))) {
725 debug(DEBUG_LEVEL_NORMAL, __FILE__": malloc() failed.\n");
730 if ((r = read(i->thread_fd, i->buf, i->fragment_size)) <= 0) {
735 debug(DEBUG_LEVEL_NORMAL, __FILE__": read(): %s\n", r == 0 ? "EOF" : strerror(errno));
739 if (pa_stream_write(i->stream, i->buf, r, free, 0, PA_SEEK_RELATIVE) < 0) {
740 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_write(): %s\n", pa_strerror(pa_context_errno(i->context)));
746 assert(n >= (size_t) r);
751 pa_mainloop_api *api;
752 api = pa_threaded_mainloop_get_api(i->mainloop);
753 api->io_enable(i->io_event, n >= i->fragment_size ? PA_IO_EVENT_INPUT : 0);
759 static void stream_state_cb(pa_stream *s, void * userdata) {
760 fd_info *i = userdata;
763 switch (pa_stream_get_state(s)) {
765 case PA_STREAM_READY:
766 debug(DEBUG_LEVEL_NORMAL, __FILE__": stream established.\n");
769 case PA_STREAM_FAILED:
770 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
774 case PA_STREAM_TERMINATED:
775 case PA_STREAM_UNCONNECTED:
776 case PA_STREAM_CREATING:
781 static int create_stream(fd_info *i) {
789 if (!(i->stream = pa_stream_new(i->context, stream_name(), &i->sample_spec, NULL))) {
790 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
794 pa_stream_set_state_callback(i->stream, stream_state_cb, i);
795 pa_stream_set_write_callback(i->stream, stream_request_cb, i);
796 pa_stream_set_latency_update_callback(i->stream, stream_latency_update_cb, i);
798 memset(&attr, 0, sizeof(attr));
799 attr.maxlength = i->fragment_size * (i->n_fragments+1);
800 attr.tlength = i->fragment_size * i->n_fragments;
801 attr.prebuf = i->fragment_size;
802 attr.minreq = i->fragment_size;
804 if (pa_stream_connect_playback(i->stream, NULL, &attr, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE, NULL, NULL) < 0) {
805 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
809 n = i->fragment_size;
810 setsockopt(i->app_fd, SOL_SOCKET, SO_SNDBUF, &n, sizeof(n));
811 n = i->fragment_size;
812 setsockopt(i->thread_fd, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n));
820 static void free_stream(fd_info *i) {
824 pa_stream_disconnect(i->stream);
825 pa_stream_unref(i->stream);
830 static void io_event_cb(pa_mainloop_api *api, pa_io_event *e, int fd, pa_io_event_flags_t flags, void *userdata) {
831 fd_info *i = userdata;
833 pa_threaded_mainloop_signal(i->mainloop, 0);
835 if (flags & PA_IO_EVENT_INPUT) {
838 api->io_enable(e, 0);
840 if (create_stream(i) < 0)
844 if (fd_info_copy_data(i, 0) < 0)
848 } else if (flags & (PA_IO_EVENT_HANGUP|PA_IO_EVENT_ERROR))
854 /* We can't do anything better than removing the event source */
858 static int dsp_open(int flags, int *_errno) {
860 pa_mainloop_api *api;
864 debug(DEBUG_LEVEL_NORMAL, __FILE__": dsp_open()\n");
866 if ((flags != O_WRONLY) && (flags != (O_WRONLY|O_NONBLOCK))) {
867 debug(DEBUG_LEVEL_NORMAL, __FILE__": bad access flags: %x\n", flags);
872 if (!(i = fd_info_new(FD_INFO_PLAYBACK, _errno)))
875 shutdown(i->thread_fd, SHUT_WR);
876 shutdown(i->app_fd, SHUT_RD);
878 if ((flags & O_NONBLOCK) == O_NONBLOCK) {
879 if ((f = fcntl(i->app_fd, F_GETFL)) >= 0)
880 fcntl(i->app_fd, F_SETFL, f|O_NONBLOCK);
882 if ((f = fcntl(i->thread_fd, F_GETFL)) >= 0)
883 fcntl(i->thread_fd, F_SETFL, f|O_NONBLOCK);
885 fcntl(i->app_fd, F_SETFD, FD_CLOEXEC);
886 fcntl(i->thread_fd, F_SETFD, FD_CLOEXEC);
888 pa_threaded_mainloop_lock(i->mainloop);
889 api = pa_threaded_mainloop_get_api(i->mainloop);
890 if (!(i->io_event = api->io_new(api, i->thread_fd, PA_IO_EVENT_INPUT, io_event_cb, i)))
893 pa_threaded_mainloop_unlock(i->mainloop);
895 debug(DEBUG_LEVEL_NORMAL, __FILE__": dsp_open() succeeded, fd=%i\n", i->app_fd);
897 fd_info_add_to_list(i);
904 pa_threaded_mainloop_unlock(i->mainloop);
911 debug(DEBUG_LEVEL_NORMAL, __FILE__": dsp_open() failed\n");
916 static void sink_info_cb(pa_context *context, const pa_sink_info *si, int eol, void *userdata) {
917 fd_info *i = userdata;
919 if (!si && eol < 0) {
920 i->operation_success = 0;
921 pa_threaded_mainloop_signal(i->mainloop, 0);
928 if (!pa_cvolume_equal(&i->volume, &si->volume))
929 i->volume_modify_count++;
931 i->volume = si->volume;
932 i->sink_index = si->index;
934 i->operation_success = 1;
935 pa_threaded_mainloop_signal(i->mainloop, 0);
938 static void subscribe_cb(pa_context *context, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
939 fd_info *i = userdata;
940 pa_operation *o = NULL;
942 if (i->sink_index != idx)
945 if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) != PA_SUBSCRIPTION_EVENT_CHANGE)
948 if (!(o = pa_context_get_sink_info_by_index(i->context, i->sink_index, sink_info_cb, i))) {
949 debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to get sink info: %s", pa_strerror(pa_context_errno(i->context)));
953 pa_operation_unref(o);
956 static int mixer_open(int flags, int *_errno) {
961 debug(DEBUG_LEVEL_NORMAL, __FILE__": mixer_open()\n");
963 if (!(i = fd_info_new(FD_INFO_MIXER, _errno)))
966 pa_threaded_mainloop_lock(i->mainloop);
968 pa_context_set_subscribe_callback(i->context, subscribe_cb, i);
970 if (!(o = pa_context_subscribe(i->context, PA_SUBSCRIPTION_MASK_SINK, context_success_cb, i))) {
971 debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to subscribe to events: %s", pa_strerror(pa_context_errno(i->context)));
976 i->operation_success = 0;
977 while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
978 pa_threaded_mainloop_wait(i->mainloop);
979 CONTEXT_CHECK_DEAD_GOTO(i, fail);
982 if (!i->operation_success) {
983 debug(DEBUG_LEVEL_NORMAL, __FILE__":Failed to subscribe to events: %s", pa_strerror(pa_context_errno(i->context)));
990 pa_operation_unref(o);
991 if (!(o = pa_context_get_sink_info_by_name(i->context, NULL, sink_info_cb, i))) {
992 debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to get sink info: %s", pa_strerror(pa_context_errno(i->context)));
997 i->operation_success = 0;
998 while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
999 pa_threaded_mainloop_wait(i->mainloop);
1000 CONTEXT_CHECK_DEAD_GOTO(i, fail);
1003 if (!i->operation_success) {
1004 debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to get sink info: %s", pa_strerror(pa_context_errno(i->context)));
1009 pa_threaded_mainloop_unlock(i->mainloop);
1011 debug(DEBUG_LEVEL_NORMAL, __FILE__": mixer_open() succeeded, fd=%i\n", i->app_fd);
1013 fd_info_add_to_list(i);
1020 pa_threaded_mainloop_unlock(i->mainloop);
1027 debug(DEBUG_LEVEL_NORMAL, __FILE__": mixer_open() failed\n");
1032 static int sndstat_open(int flags, int *_errno) {
1033 static const char sndstat[] =
1034 "Sound Driver:3.8.1a-980706 (Polypaudio Virtual OSS)\n"
1036 "Config options: 0\n"
1038 "Installed drivers:\n"
1039 "Type 255: Polypaudio Virtual OSS\n"
1042 "Polypaudio Virtual OSS\n"
1045 "0: Polypaudio Virtual OSS\n"
1047 "Synth devices: NOT ENABLED IN CONFIG\n"
1054 "0: Polypaudio Virtual OSS\n";
1056 char fn[] = "/tmp/padsp-sndstat-XXXXXX";
1061 debug(DEBUG_LEVEL_NORMAL, __FILE__": sndstat_open()\n");
1063 if (flags != O_RDONLY && flags != (O_RDONLY|O_LARGEFILE)) {
1065 debug(DEBUG_LEVEL_NORMAL, __FILE__": bad access!\n");
1076 debug(DEBUG_LEVEL_NORMAL, __FILE__": mkstemp() failed: %s\n", strerror(errno));
1082 if (write(fd, sndstat, sizeof(sndstat) -1) != sizeof(sndstat)-1) {
1084 debug(DEBUG_LEVEL_NORMAL, __FILE__": write() failed: %s\n", strerror(errno));
1088 if (lseek(fd, SEEK_SET, 0) < 0) {
1090 debug(DEBUG_LEVEL_NORMAL, __FILE__": lseek() failed: %s\n", strerror(errno));
1102 int open(const char *filename, int flags, ...) {
1107 debug(DEBUG_LEVEL_VERBOSE, __FILE__": open(%s)\n", filename);
1109 va_start(args, flags);
1110 if (flags & O_CREAT)
1111 mode = va_arg(args, mode_t);
1114 if (!function_enter()) {
1116 return _open(filename, flags, mode);
1119 if (dsp_cloak_enable() && (strcmp(filename, "/dev/dsp") == 0 || strcmp(filename, "/dev/adsp") == 0)) {
1120 r = dsp_open(flags, &_errno);
1121 } else if (mixer_cloak_enable() && strcmp(filename, "/dev/mixer") == 0) {
1122 r = mixer_open(flags, &_errno);
1123 } else if (sndstat_cloak_enable() && strcmp(filename, "/dev/sndstat") == 0) {
1124 r = sndstat_open(flags, &_errno);
1128 return _open(filename, flags, mode);
1139 static int mixer_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno) {
1143 case SOUND_MIXER_READ_DEVMASK :
1144 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_DEVMASK\n");
1146 *(int*) argp = SOUND_MASK_PCM;
1149 case SOUND_MIXER_READ_RECMASK :
1150 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_RECMASK\n");
1155 case SOUND_MIXER_READ_STEREODEVS:
1156 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_STEREODEVS\n");
1158 pa_threaded_mainloop_lock(i->mainloop);
1159 *(int*) argp = i->volume.channels > 1 ? SOUND_MASK_PCM : 0;
1160 pa_threaded_mainloop_unlock(i->mainloop);
1164 case SOUND_MIXER_READ_RECSRC:
1165 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_RECSRC\n");
1170 case SOUND_MIXER_READ_CAPS:
1171 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_CAPS\n");
1176 case SOUND_MIXER_READ_PCM:
1178 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_PCM\n");
1180 pa_threaded_mainloop_lock(i->mainloop);
1183 ((i->volume.values[0]*100/PA_VOLUME_NORM)) |
1184 ((i->volume.values[i->volume.channels > 1 ? 1 : 0]*100/PA_VOLUME_NORM) << 8);
1186 pa_threaded_mainloop_unlock(i->mainloop);
1190 case SOUND_MIXER_WRITE_PCM: {
1193 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_WRITE_PCM\n");
1195 pa_threaded_mainloop_lock(i->mainloop);
1199 i->volume.values[0] = ((*(int*) argp & 0xFF)*PA_VOLUME_NORM)/100;
1200 i->volume.values[1] = ((*(int*) argp >> 8)*PA_VOLUME_NORM)/100;
1202 if (!pa_cvolume_equal(&i->volume, &v)) {
1205 if (!(o = pa_context_set_sink_volume_by_index(i->context, i->sink_index, &i->volume, NULL, NULL)))
1206 debug(DEBUG_LEVEL_NORMAL, __FILE__":Failed set volume: %s", pa_strerror(pa_context_errno(i->context)));
1209 i->operation_success = 0;
1210 while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
1211 CONTEXT_CHECK_DEAD_GOTO(i, exit_loop);
1213 pa_threaded_mainloop_wait(i->mainloop);
1217 if (!i->operation_success)
1218 debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to set volume: %s\n", pa_strerror(pa_context_errno(i->context)));
1220 pa_operation_unref(o);
1223 /* We don't wait for completion here */
1224 i->volume_modify_count++;
1227 pa_threaded_mainloop_unlock(i->mainloop);
1232 case SOUND_MIXER_INFO: {
1233 mixer_info *mi = argp;
1235 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_INFO\n");
1237 memset(mi, 0, sizeof(mixer_info));
1238 strncpy(mi->id, "POLYPAUDIO", sizeof(mi->id));
1239 strncpy(mi->name, "Polypaudio Virtual OSS", sizeof(mi->name));
1240 pa_threaded_mainloop_lock(i->mainloop);
1241 mi->modify_counter = i->volume_modify_count;
1242 pa_threaded_mainloop_unlock(i->mainloop);
1247 debug(DEBUG_LEVEL_NORMAL, __FILE__": unknown ioctl 0x%08lx\n", request);
1260 static int map_format(int *fmt, pa_sample_spec *ss) {
1264 ss->format = PA_SAMPLE_ULAW;
1268 ss->format = PA_SAMPLE_ALAW;
1275 ss->format = PA_SAMPLE_U8;
1282 ss->format = PA_SAMPLE_S16BE;
1289 ss->format = PA_SAMPLE_S16LE;
1293 ss->format = PA_SAMPLE_S16NE;
1301 static int map_format_back(pa_sample_format_t format) {
1303 case PA_SAMPLE_S16LE: return AFMT_S16_LE;
1304 case PA_SAMPLE_S16BE: return AFMT_S16_BE;
1305 case PA_SAMPLE_ULAW: return AFMT_MU_LAW;
1306 case PA_SAMPLE_ALAW: return AFMT_A_LAW;
1307 case PA_SAMPLE_U8: return AFMT_U8;
1313 static int dsp_flush_socket(fd_info *i) {
1316 if (i->thread_fd < 0)
1319 if (ioctl(i->thread_fd, SIOCINQ, &l) < 0) {
1320 debug(DEBUG_LEVEL_NORMAL, __FILE__": SIOCINQ: %s\n", strerror(errno));
1328 k = (size_t) l > sizeof(buf) ? sizeof(buf) : (size_t) l;
1329 if (read(i->thread_fd, buf, k) < 0)
1330 debug(DEBUG_LEVEL_NORMAL, __FILE__": read(): %s\n", strerror(errno));
1337 static int dsp_empty_socket(fd_info *i) {
1340 /* Empty the socket */
1344 if (i->thread_fd < 0)
1347 if (ioctl(i->thread_fd, SIOCINQ, &l) < 0) {
1348 debug(DEBUG_LEVEL_NORMAL, __FILE__": SIOCINQ: %s\n", strerror(errno));
1357 pa_threaded_mainloop_wait(i->mainloop);
1363 static int dsp_drain(fd_info *i) {
1364 pa_operation *o = NULL;
1370 debug(DEBUG_LEVEL_NORMAL, __FILE__": Draining.\n");
1372 pa_threaded_mainloop_lock(i->mainloop);
1374 if (dsp_empty_socket(i) < 0)
1380 debug(DEBUG_LEVEL_NORMAL, __FILE__": Really draining.\n");
1382 if (!(o = pa_stream_drain(i->stream, stream_success_cb, i))) {
1383 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_drain(): %s\n", pa_strerror(pa_context_errno(i->context)));
1387 i->operation_success = 0;
1388 while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
1389 STREAM_CHECK_DEAD_GOTO(i, fail);
1391 pa_threaded_mainloop_wait(i->mainloop);
1394 if (!i->operation_success) {
1395 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_drain() 2: %s\n", pa_strerror(pa_context_errno(i->context)));
1404 pa_operation_unref(o);
1406 pa_threaded_mainloop_unlock(i->mainloop);
1411 static int dsp_trigger(fd_info *i) {
1412 pa_operation *o = NULL;
1418 pa_threaded_mainloop_lock(i->mainloop);
1420 if (dsp_empty_socket(i) < 0)
1423 debug(DEBUG_LEVEL_NORMAL, __FILE__": Triggering.\n");
1425 if (!(o = pa_stream_trigger(i->stream, stream_success_cb, i))) {
1426 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i->context)));
1430 i->operation_success = 0;
1431 while (!pa_operation_get_state(o) != PA_OPERATION_DONE) {
1432 STREAM_CHECK_DEAD_GOTO(i, fail);
1434 pa_threaded_mainloop_wait(i->mainloop);
1437 if (!i->operation_success) {
1438 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i->context)));
1447 pa_operation_unref(o);
1449 pa_threaded_mainloop_unlock(i->mainloop);
1454 static int dsp_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno) {
1458 case SNDCTL_DSP_SETFMT: {
1459 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_SETFMT: %i\n", *(int*) argp);
1461 pa_threaded_mainloop_lock(i->mainloop);
1463 if (*(int*) argp == AFMT_QUERY)
1464 *(int*) argp = map_format_back(i->sample_spec.format);
1466 map_format((int*) argp, &i->sample_spec);
1470 pa_threaded_mainloop_unlock(i->mainloop);
1474 case SNDCTL_DSP_SPEED: {
1479 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_SPEED: %i\n", *(int*) argp);
1481 pa_threaded_mainloop_lock(i->mainloop);
1483 ss = i->sample_spec;
1484 ss.rate = *(int*) argp;
1486 if ((valid = pa_sample_spec_valid(&ss))) {
1487 i->sample_spec = ss;
1491 debug(DEBUG_LEVEL_NORMAL, __FILE__": ss: %s\n", pa_sample_spec_snprint(t, sizeof(t), &i->sample_spec));
1493 pa_threaded_mainloop_unlock(i->mainloop);
1503 case SNDCTL_DSP_STEREO:
1504 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_STEREO: %i\n", *(int*) argp);
1506 pa_threaded_mainloop_lock(i->mainloop);
1508 i->sample_spec.channels = *(int*) argp ? 2 : 1;
1511 pa_threaded_mainloop_unlock(i->mainloop);
1514 case SNDCTL_DSP_CHANNELS: {
1518 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_CHANNELS: %i\n", *(int*) argp);
1520 pa_threaded_mainloop_lock(i->mainloop);
1522 ss = i->sample_spec;
1523 ss.channels = *(int*) argp;
1525 if ((valid = pa_sample_spec_valid(&ss))) {
1526 i->sample_spec = ss;
1530 pa_threaded_mainloop_unlock(i->mainloop);
1540 case SNDCTL_DSP_GETBLKSIZE:
1541 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_GETBLKSIZE\n");
1543 pa_threaded_mainloop_lock(i->mainloop);
1546 *(int*) argp = i->fragment_size;
1548 pa_threaded_mainloop_unlock(i->mainloop);
1552 case SNDCTL_DSP_SETFRAGMENT:
1553 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_SETFRAGMENT: 0x%8x\n", *(int*) argp);
1555 pa_threaded_mainloop_lock(i->mainloop);
1557 i->fragment_size = 1 << (*(int*) argp);
1558 i->n_fragments = (*(int*) argp) >> 16;
1562 pa_threaded_mainloop_unlock(i->mainloop);
1566 case SNDCTL_DSP_GETCAPS:
1567 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_CAPS\n");
1569 *(int*) argp = DSP_CAP_MULTI;
1572 case SNDCTL_DSP_GETODELAY: {
1575 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_GETODELAY\n");
1577 pa_threaded_mainloop_lock(i->mainloop);
1584 STREAM_CHECK_DEAD_GOTO(i, exit_loop);
1586 if (pa_stream_get_latency(i->stream, &usec, NULL) >= 0) {
1587 *(int*) argp = pa_usec_to_bytes(usec, &i->sample_spec);
1591 if (pa_context_errno(i->context) != PA_ERR_NODATA) {
1592 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_get_latency(): %s\n", pa_strerror(pa_context_errno(i->context)));
1596 pa_threaded_mainloop_wait(i->mainloop);
1601 if (ioctl(i->thread_fd, SIOCINQ, &l) < 0)
1602 debug(DEBUG_LEVEL_NORMAL, __FILE__": SIOCINQ failed: %s\n", strerror(errno));
1606 pa_threaded_mainloop_unlock(i->mainloop);
1608 debug(DEBUG_LEVEL_NORMAL, __FILE__": ODELAY: %i\n", *(int*) argp);
1613 case SNDCTL_DSP_RESET: {
1614 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_RESET\n");
1616 pa_threaded_mainloop_lock(i->mainloop);
1619 dsp_flush_socket(i);
1622 pa_threaded_mainloop_unlock(i->mainloop);
1626 case SNDCTL_DSP_GETFMTS: {
1627 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_GETFMTS\n");
1629 *(int*) argp = AFMT_MU_LAW|AFMT_A_LAW|AFMT_U8|AFMT_S16_LE|AFMT_S16_BE;
1633 case SNDCTL_DSP_POST:
1634 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_POST\n");
1636 if (dsp_trigger(i) < 0)
1640 case SNDCTL_DSP_SYNC:
1641 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_SYNC\n");
1643 if (dsp_drain(i) < 0)
1648 case SNDCTL_DSP_GETOSPACE: {
1649 audio_buf_info *bi = (audio_buf_info*) argp;
1653 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_GETOSPACE\n");
1655 pa_threaded_mainloop_lock(i->mainloop);
1660 if ((k = pa_stream_writable_size(i->stream)) == (size_t) -1)
1661 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_writable_size(): %s\n", pa_strerror(pa_context_errno(i->context)));
1663 k = i->fragment_size * i->n_fragments;
1665 if (ioctl(i->thread_fd, SIOCINQ, &l) < 0) {
1666 debug(DEBUG_LEVEL_NORMAL, __FILE__": SIOCINQ failed: %s\n", strerror(errno));
1670 bi->fragsize = i->fragment_size;
1671 bi->fragstotal = i->n_fragments;
1672 bi->bytes = k > (size_t) l ? k - l : 0;
1673 bi->fragments = bi->bytes / bi->fragsize;
1675 pa_threaded_mainloop_unlock(i->mainloop);
1677 debug(DEBUG_LEVEL_NORMAL, __FILE__": fragsize=%i, fragstotal=%i, bytes=%i, fragments=%i\n", bi->fragsize, bi->fragstotal, bi->bytes, bi->fragments);
1683 debug(DEBUG_LEVEL_NORMAL, __FILE__": unknown ioctl 0x%08lx\n", request);
1696 int ioctl(int fd, unsigned long request, ...) {
1702 debug(DEBUG_LEVEL_VERBOSE, __FILE__": ioctl()\n");
1704 va_start(args, request);
1705 argp = va_arg(args, void *);
1708 if (!function_enter()) {
1710 return _ioctl(fd, request, argp);
1713 if (!(i = fd_info_find(fd))) {
1716 return _ioctl(fd, request, argp);
1719 if (i->type == FD_INFO_MIXER)
1720 r = mixer_ioctl(i, request, argp, &_errno);
1722 r = dsp_ioctl(i, request, argp, &_errno);
1737 debug(DEBUG_LEVEL_VERBOSE, __FILE__": close()\n");
1739 if (!function_enter()) {
1744 if (!(i = fd_info_find(fd))) {
1750 fd_info_remove_from_list(i);
1758 int access(const char *pathname, int mode) {
1759 debug(DEBUG_LEVEL_VERBOSE, __FILE__": access(%s)\n", pathname);
1761 if (strcmp(pathname, "/dev/dsp") != 0 &&
1762 strcmp(pathname, "/dev/adsp") != 0 &&
1763 strcmp(pathname, "/dev/sndstat") != 0 &&
1764 strcmp(pathname, "/dev/mixer") != 0) {
1766 return _access(pathname, mode);
1769 if (mode & (W_OK | X_OK)) {
1770 debug(DEBUG_LEVEL_NORMAL, __FILE__": access(%s, %x) = EACCESS\n", pathname, mode);
1775 debug(DEBUG_LEVEL_NORMAL, __FILE__": access(%s, %x) = OK\n", pathname, mode);
1780 int open64(const char *filename, int flags, ...) {
1784 debug(DEBUG_LEVEL_VERBOSE, __FILE__": open64(%s)\n", filename);
1786 va_start(args, flags);
1787 if (flags & O_CREAT)
1788 mode = va_arg(args, mode_t);
1791 if (strcmp(filename, "/dev/dsp") != 0 &&
1792 strcmp(filename, "/dev/adsp") != 0 &&
1793 strcmp(filename, "/dev/sndstat") != 0 &&
1794 strcmp(filename, "/dev/mixer") != 0) {
1796 return _open64(filename, flags, mode);
1799 return open(filename, flags, mode);
1802 FILE* fopen(const char *filename, const char *mode) {
1807 debug(DEBUG_LEVEL_VERBOSE, __FILE__": fopen(%s)\n", filename);
1809 if (strcmp(filename, "/dev/dsp") != 0 &&
1810 strcmp(filename, "/dev/adsp") != 0 &&
1811 strcmp(filename, "/dev/sndstat") != 0 &&
1812 strcmp(filename, "/dev/mixer") != 0) {
1814 return _fopen(filename, mode);
1830 if ((((mode[1] == 'b') || (mode[1] == 't')) && (mode[2] == '+')) || (mode[1] == '+'))
1833 if ((fd = open(filename, m)) < 0)
1836 if (!(f = fdopen(fd, mode))) {
1844 FILE *fopen64(const char *filename, const char *mode) {
1846 debug(DEBUG_LEVEL_VERBOSE, __FILE__": fopen64(%s)\n", filename);
1848 if (strcmp(filename, "/dev/dsp") != 0 &&
1849 strcmp(filename, "/dev/adsp") != 0 &&
1850 strcmp(filename, "/dev/sndstat") != 0 &&
1851 strcmp(filename, "/dev/mixer") != 0) {
1852 LOAD_FOPEN64_FUNC();
1853 return _fopen64(filename, mode);
1856 return fopen(filename, mode);
1859 int fclose(FILE *f) {
1862 debug(DEBUG_LEVEL_VERBOSE, __FILE__": fclose()\n");
1864 if (!function_enter()) {
1869 if (!(i = fd_info_find(fileno(f)))) {
1875 fd_info_remove_from_list(i);
1877 /* Dirty trick to avoid that the fd is not freed twice, once by us
1878 * and once by the real fclose() */