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(__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(__FILE__": Not connected: %s", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
192 static void debug(const char *format, ...) PA_GCC_PRINTF_ATTR(1,2);
194 static void debug(const char *format, ...) {
196 if (getenv("PADSP_DEBUG")) {
197 va_start(ap, format);
198 vfprintf(stderr, format, ap);
203 static int padsp_disabled(void) {
205 static int sym_resolved = 0;
207 /* If the current process has a symbol __padsp_disabled__ we use
208 * it to detect whether we should enable our stuff or not. A
209 * program needs to be compiled with -rdynamic for this to work!
210 * The symbol must be an int containing a three bit bitmask: bit 1
211 * -> disable /dev/dsp emulation, bit 2 -> disable /dev/sndstat
212 * emulation, bit 3 -> disable /dev/mixer emulation. Hence a value
213 * of 7 disables padsp entirely. */
215 pthread_mutex_lock(&func_mutex);
217 sym = (int*) dlsym(RTLD_DEFAULT, "__padsp_disabled__");
221 pthread_mutex_unlock(&func_mutex);
229 static int dsp_cloak_enable(void) {
230 if (padsp_disabled() & 1)
233 if (getenv("PADSP_NO_DSP"))
239 static int sndstat_cloak_enable(void) {
240 if (padsp_disabled() & 2)
243 if (getenv("PADSP_NO_SNDSTAT"))
249 static int mixer_cloak_enable(void) {
250 if (padsp_disabled() & 4)
253 if (getenv("PADSP_NO_MIXER"))
258 static pthread_key_t recursion_key;
260 static void recursion_key_alloc(void) {
261 pthread_key_create(&recursion_key, NULL);
264 static int function_enter(void) {
265 /* Avoid recursive calls */
266 static pthread_once_t recursion_key_once = PTHREAD_ONCE_INIT;
267 pthread_once(&recursion_key_once, recursion_key_alloc);
269 if (pthread_getspecific(recursion_key))
272 pthread_setspecific(recursion_key, (void*) 1);
276 static void function_exit(void) {
277 pthread_setspecific(recursion_key, NULL);
280 static void fd_info_free(fd_info *i) {
283 debug(__FILE__": freeing fd info (fd=%i)\n", i->app_fd);
288 pa_threaded_mainloop_stop(i->mainloop);
291 pa_stream_disconnect(i->stream);
292 pa_stream_unref(i->stream);
296 pa_context_disconnect(i->context);
297 pa_context_unref(i->context);
301 pa_threaded_mainloop_free(i->mainloop);
303 if (i->app_fd >= 0) {
308 if (i->thread_fd >= 0) {
310 _close(i->thread_fd);
315 pthread_mutex_destroy(&i->mutex);
319 static fd_info *fd_info_ref(fd_info *i) {
322 pthread_mutex_lock(&i->mutex);
326 /* debug(__FILE__": ref++, now %i\n", i->ref); */
327 pthread_mutex_unlock(&i->mutex);
332 static void fd_info_unref(fd_info *i) {
334 pthread_mutex_lock(&i->mutex);
337 /* debug(__FILE__": ref--, now %i\n", i->ref); */
338 pthread_mutex_unlock(&i->mutex);
344 static void context_state_cb(pa_context *c, void *userdata) {
345 fd_info *i = userdata;
348 switch (pa_context_get_state(c)) {
349 case PA_CONTEXT_READY:
350 case PA_CONTEXT_TERMINATED:
351 case PA_CONTEXT_FAILED:
352 pa_threaded_mainloop_signal(i->mainloop, 0);
355 case PA_CONTEXT_UNCONNECTED:
356 case PA_CONTEXT_CONNECTING:
357 case PA_CONTEXT_AUTHORIZING:
358 case PA_CONTEXT_SETTING_NAME:
363 static void reset_params(fd_info *i) {
366 i->sample_spec.format = PA_SAMPLE_ULAW;
367 i->sample_spec.channels = 1;
368 i->sample_spec.rate = 8000;
369 i->fragment_size = 0;
373 static const char *client_name(char *buf, size_t n) {
377 if ((e = getenv("PADSP_CLIENT_NAME")))
380 if (pa_get_binary_name(p, sizeof(p)))
381 snprintf(buf, n, "OSS Emulation[%s]", pa_path_get_filename(p));
383 snprintf(buf, n, "OSS");
388 static const char *stream_name(void) {
391 if ((e = getenv("PADSP_STREAM_NAME")))
394 return "Audio Stream";
397 static void atfork_prepare(void) {
400 debug(__FILE__": atfork_prepare() enter\n");
404 pthread_mutex_lock(&fd_infos_mutex);
406 for (i = fd_infos; i; i = i->next) {
407 pthread_mutex_lock(&i->mutex);
408 pa_threaded_mainloop_lock(i->mainloop);
411 pthread_mutex_lock(&func_mutex);
414 debug(__FILE__": atfork_prepare() exit\n");
417 static void atfork_parent(void) {
420 debug(__FILE__": atfork_parent() enter\n");
422 pthread_mutex_unlock(&func_mutex);
424 for (i = fd_infos; i; i = i->next) {
425 pa_threaded_mainloop_unlock(i->mainloop);
426 pthread_mutex_unlock(&i->mutex);
429 pthread_mutex_unlock(&fd_infos_mutex);
433 debug(__FILE__": atfork_parent() exit\n");
436 static void atfork_child(void) {
439 debug(__FILE__": atfork_child() enter\n");
441 /* We do only the bare minimum to get all fds closed */
442 pthread_mutex_init(&func_mutex, NULL);
443 pthread_mutex_init(&fd_infos_mutex, NULL);
445 for (i = fd_infos; i; i = i->next) {
446 pthread_mutex_init(&i->mutex, NULL);
449 pa_context_disconnect(i->context);
450 pa_context_unref(i->context);
455 pa_stream_unref(i->stream);
459 if (i->app_fd >= 0) {
464 if (i->thread_fd >= 0) {
474 debug(__FILE__": atfork_child() exit\n");
477 static void install_atfork(void) {
478 pthread_atfork(atfork_prepare, atfork_parent, atfork_child);
481 static void stream_success_cb(pa_stream *s, int success, void *userdata) {
482 fd_info *i = userdata;
487 i->operation_success = success;
488 pa_threaded_mainloop_signal(i->mainloop, 0);
491 static void context_success_cb(pa_context *c, int success, void *userdata) {
492 fd_info *i = userdata;
497 i->operation_success = success;
498 pa_threaded_mainloop_signal(i->mainloop, 0);
501 static fd_info* fd_info_new(fd_info_type_t type, int *_errno) {
503 int sfds[2] = { -1, -1 };
505 static pthread_once_t install_atfork_once = PTHREAD_ONCE_INIT;
507 debug(__FILE__": fd_info_new()\n");
509 signal(SIGPIPE, SIG_IGN); /* Yes, ugly as hell */
511 pthread_once(&install_atfork_once, install_atfork);
513 if (!(i = malloc(sizeof(fd_info)))) {
518 i->app_fd = i->thread_fd = -1;
525 pthread_mutex_init(&i->mutex, NULL);
529 pa_cvolume_reset(&i->volume, 2);
530 i->volume_modify_count = 0;
531 i->sink_index = (uint32_t) -1;
532 PA_LLIST_INIT(fd_info, i);
536 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sfds) < 0) {
538 debug(__FILE__": socket() failed: %s\n", strerror(errno));
543 i->thread_fd = sfds[1];
545 if (!(i->mainloop = pa_threaded_mainloop_new())) {
547 debug(__FILE__": pa_threaded_mainloop_new() failed\n");
551 if (!(i->context = pa_context_new(pa_threaded_mainloop_get_api(i->mainloop), client_name(name, sizeof(name))))) {
553 debug(__FILE__": pa_context_new() failed\n");
557 pa_context_set_state_callback(i->context, context_state_cb, i);
559 if (pa_context_connect(i->context, NULL, 0, NULL) < 0) {
560 *_errno = ECONNREFUSED;
561 debug(__FILE__": pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
565 pa_threaded_mainloop_lock(i->mainloop);
567 if (pa_threaded_mainloop_start(i->mainloop) < 0) {
569 debug(__FILE__": pa_threaded_mainloop_start() failed\n");
570 goto unlock_and_fail;
573 /* Wait until the context is ready */
574 pa_threaded_mainloop_wait(i->mainloop);
576 if (pa_context_get_state(i->context) != PA_CONTEXT_READY) {
577 *_errno = ECONNREFUSED;
578 debug(__FILE__": pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
579 goto unlock_and_fail;
582 pa_threaded_mainloop_unlock(i->mainloop);
587 pa_threaded_mainloop_unlock(i->mainloop);
597 static void fd_info_add_to_list(fd_info *i) {
600 pthread_mutex_lock(&fd_infos_mutex);
601 PA_LLIST_PREPEND(fd_info, fd_infos, i);
602 pthread_mutex_unlock(&fd_infos_mutex);
607 static void fd_info_remove_from_list(fd_info *i) {
610 pthread_mutex_lock(&fd_infos_mutex);
611 PA_LLIST_REMOVE(fd_info, fd_infos, i);
612 pthread_mutex_unlock(&fd_infos_mutex);
617 static fd_info* fd_info_find(int fd) {
620 pthread_mutex_lock(&fd_infos_mutex);
622 for (i = fd_infos; i; i = i->next)
623 if (i->app_fd == fd && !i->unusable) {
628 pthread_mutex_unlock(&fd_infos_mutex);
633 static void fix_metrics(fd_info *i) {
635 char t[PA_SAMPLE_SPEC_SNPRINT_MAX];
637 fs = pa_frame_size(&i->sample_spec);
638 i->fragment_size = (i->fragment_size/fs)*fs;
640 /* Number of fragments set? */
641 if (i->n_fragments < 2) {
642 if (i->fragment_size > 0) {
643 i->n_fragments = pa_bytes_per_second(&i->sample_spec) / 2 / i->fragment_size;
644 if (i->n_fragments < 2)
650 /* Fragment size set? */
651 if (i->fragment_size <= 0) {
652 i->fragment_size = pa_bytes_per_second(&i->sample_spec) / 2 / i->n_fragments;
653 if (i->fragment_size < 1024)
654 i->fragment_size = 1024;
657 debug(__FILE__": sample spec: %s\n", pa_sample_spec_snprint(t, sizeof(t), &i->sample_spec));
658 debug(__FILE__": fixated metrics to %i fragments, %li bytes each.\n", i->n_fragments, (long)i->fragment_size);
661 static void stream_request_cb(pa_stream *s, size_t length, void *userdata) {
662 fd_info *i = userdata;
666 pa_mainloop_api *api;
667 api = pa_threaded_mainloop_get_api(i->mainloop);
668 api->io_enable(i->io_event, PA_IO_EVENT_INPUT);
672 static void stream_latency_update_cb(pa_stream *s, void *userdata) {
673 fd_info *i = userdata;
676 pa_threaded_mainloop_signal(i->mainloop, 0);
679 static void fd_info_shutdown(fd_info *i) {
683 pa_mainloop_api *api;
684 api = pa_threaded_mainloop_get_api(i->mainloop);
685 api->io_free(i->io_event);
689 if (i->thread_fd >= 0) {
695 static int fd_info_copy_data(fd_info *i, int force) {
701 if ((n = pa_stream_writable_size(i->stream)) == (size_t) -1) {
702 debug(__FILE__": pa_stream_writable_size(): %s\n", pa_strerror(pa_context_errno(i->context)));
706 while (n >= i->fragment_size || force) {
710 if (!(i->buf = malloc(i->fragment_size))) {
711 debug(__FILE__": malloc() failed.\n");
716 if ((r = read(i->thread_fd, i->buf, i->fragment_size)) <= 0) {
721 debug(__FILE__": read(): %s\n", r == 0 ? "EOF" : strerror(errno));
725 if (pa_stream_write(i->stream, i->buf, r, free, 0, PA_SEEK_RELATIVE) < 0) {
726 debug(__FILE__": pa_stream_write(): %s\n", pa_strerror(pa_context_errno(i->context)));
732 assert(n >= (size_t) r);
737 pa_mainloop_api *api;
738 api = pa_threaded_mainloop_get_api(i->mainloop);
739 api->io_enable(i->io_event, n >= i->fragment_size ? PA_IO_EVENT_INPUT : 0);
745 static void stream_state_cb(pa_stream *s, void * userdata) {
746 fd_info *i = userdata;
749 switch (pa_stream_get_state(s)) {
751 case PA_STREAM_READY:
752 debug(__FILE__": stream established.\n");
755 case PA_STREAM_FAILED:
756 debug(__FILE__": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
760 case PA_STREAM_TERMINATED:
761 case PA_STREAM_UNCONNECTED:
762 case PA_STREAM_CREATING:
767 static int create_stream(fd_info *i) {
775 if (!(i->stream = pa_stream_new(i->context, stream_name(), &i->sample_spec, NULL))) {
776 debug(__FILE__": pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
780 pa_stream_set_state_callback(i->stream, stream_state_cb, i);
781 pa_stream_set_write_callback(i->stream, stream_request_cb, i);
782 pa_stream_set_latency_update_callback(i->stream, stream_latency_update_cb, i);
784 memset(&attr, 0, sizeof(attr));
785 attr.maxlength = i->fragment_size * (i->n_fragments+1);
786 attr.tlength = i->fragment_size * i->n_fragments;
787 attr.prebuf = i->fragment_size;
788 attr.minreq = i->fragment_size;
790 if (pa_stream_connect_playback(i->stream, NULL, &attr, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE, NULL, NULL) < 0) {
791 debug(__FILE__": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
795 n = i->fragment_size;
796 setsockopt(i->app_fd, SOL_SOCKET, SO_SNDBUF, &n, sizeof(n));
797 n = i->fragment_size;
798 setsockopt(i->thread_fd, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n));
806 static void free_stream(fd_info *i) {
810 pa_stream_disconnect(i->stream);
811 pa_stream_unref(i->stream);
816 static void io_event_cb(pa_mainloop_api *api, pa_io_event *e, int fd, pa_io_event_flags_t flags, void *userdata) {
817 fd_info *i = userdata;
819 pa_threaded_mainloop_signal(i->mainloop, 0);
821 if (flags & PA_IO_EVENT_INPUT) {
824 api->io_enable(e, 0);
826 if (create_stream(i) < 0)
830 if (fd_info_copy_data(i, 0) < 0)
834 } else if (flags & (PA_IO_EVENT_HANGUP|PA_IO_EVENT_ERROR))
840 /* We can't do anything better than removing the event source */
844 static int dsp_open(int flags, int *_errno) {
846 pa_mainloop_api *api;
850 if ((flags != O_WRONLY) && (flags != (O_WRONLY|O_NONBLOCK))) {
855 if (!(i = fd_info_new(FD_INFO_PLAYBACK, _errno)))
858 shutdown(i->thread_fd, SHUT_WR);
859 shutdown(i->app_fd, SHUT_RD);
861 if ((flags & O_NONBLOCK) == O_NONBLOCK) {
862 if ((f = fcntl(i->app_fd, F_GETFL)) >= 0)
863 fcntl(i->app_fd, F_SETFL, f|O_NONBLOCK);
865 if ((f = fcntl(i->thread_fd, F_GETFL)) >= 0)
866 fcntl(i->thread_fd, F_SETFL, f|O_NONBLOCK);
868 fcntl(i->app_fd, F_SETFD, FD_CLOEXEC);
869 fcntl(i->thread_fd, F_SETFD, FD_CLOEXEC);
871 pa_threaded_mainloop_lock(i->mainloop);
872 api = pa_threaded_mainloop_get_api(i->mainloop);
873 if (!(i->io_event = api->io_new(api, i->thread_fd, PA_IO_EVENT_INPUT, io_event_cb, i)))
876 pa_threaded_mainloop_unlock(i->mainloop);
878 debug(__FILE__": dsp_open() succeeded, fd=%i\n", i->app_fd);
880 fd_info_add_to_list(i);
887 pa_threaded_mainloop_unlock(i->mainloop);
894 debug(__FILE__": dsp_open() failed\n");
899 static void sink_info_cb(pa_context *context, const pa_sink_info *si, int eol, void *userdata) {
900 fd_info *i = userdata;
902 if (!si && eol < 0) {
903 i->operation_success = 0;
904 pa_threaded_mainloop_signal(i->mainloop, 0);
911 if (!pa_cvolume_equal(&i->volume, &si->volume))
912 i->volume_modify_count++;
914 i->volume = si->volume;
915 i->sink_index = si->index;
917 i->operation_success = 1;
918 pa_threaded_mainloop_signal(i->mainloop, 0);
921 static void subscribe_cb(pa_context *context, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
922 fd_info *i = userdata;
923 pa_operation *o = NULL;
925 if (i->sink_index != idx)
928 if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) != PA_SUBSCRIPTION_EVENT_CHANGE)
931 if (!(o = pa_context_get_sink_info_by_index(i->context, i->sink_index, sink_info_cb, i))) {
932 debug(__FILE__": Failed to get sink info: %s", pa_strerror(pa_context_errno(i->context)));
936 pa_operation_unref(o);
939 static int mixer_open(int flags, int *_errno) {
944 if (!(i = fd_info_new(FD_INFO_MIXER, _errno)))
947 pa_threaded_mainloop_lock(i->mainloop);
949 pa_context_set_subscribe_callback(i->context, subscribe_cb, i);
951 if (!(o = pa_context_subscribe(i->context, PA_SUBSCRIPTION_MASK_SINK, context_success_cb, i))) {
952 debug(__FILE__": Failed to subscribe to events: %s", pa_strerror(pa_context_errno(i->context)));
957 i->operation_success = 0;
958 while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
959 pa_threaded_mainloop_wait(i->mainloop);
960 CONTEXT_CHECK_DEAD_GOTO(i, fail);
963 if (!i->operation_success) {
964 debug(__FILE__":Failed to subscribe to events: %s", pa_strerror(pa_context_errno(i->context)));
971 pa_operation_unref(o);
972 if (!(o = pa_context_get_sink_info_by_name(i->context, NULL, sink_info_cb, i))) {
973 debug(__FILE__": Failed to get sink info: %s", pa_strerror(pa_context_errno(i->context)));
978 i->operation_success = 0;
979 while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
980 pa_threaded_mainloop_wait(i->mainloop);
981 CONTEXT_CHECK_DEAD_GOTO(i, fail);
984 if (!i->operation_success) {
985 debug(__FILE__": Failed to get sink info: %s", pa_strerror(pa_context_errno(i->context)));
990 pa_threaded_mainloop_unlock(i->mainloop);
992 debug(__FILE__": mixer_open() succeeded, fd=%i\n", i->app_fd);
994 fd_info_add_to_list(i);
1001 pa_threaded_mainloop_unlock(i->mainloop);
1008 debug(__FILE__": mixer_open() failed\n");
1013 static int sndstat_open(int flags, int *_errno) {
1014 static const char sndstat[] =
1015 "Sound Driver:3.8.1a-980706 (Polypaudio Virtual OSS)\n"
1017 "Config options: 0\n"
1019 "Installed drivers:\n"
1020 "Type 255: Polypaudio Virtual OSS\n"
1023 "Polypaudio Virtual OSS\n"
1026 "0: Polypaudio Virtual OSS\n"
1028 "Synth devices: NOT ENABLED IN CONFIG\n"
1035 "0: Polypaudio Virtual OSS\n";
1037 char fn[] = "/tmp/padsp-sndstat-XXXXXX";
1042 debug(__FILE__": sndstat_open()\n");
1044 if (flags != O_RDONLY && flags != (O_RDONLY|O_LARGEFILE)) {
1046 debug(__FILE__": bad access!\n");
1057 debug(__FILE__": mkstemp() failed: %s\n", strerror(errno));
1063 if (write(fd, sndstat, sizeof(sndstat) -1) != sizeof(sndstat)-1) {
1065 debug(__FILE__": write() failed: %s\n", strerror(errno));
1069 if (lseek(fd, SEEK_SET, 0) < 0) {
1071 debug(__FILE__": lseek() failed: %s\n", strerror(errno));
1083 int open(const char *filename, int flags, ...) {
1088 debug(__FILE__": open(%s)\n", filename);
1090 va_start(args, flags);
1091 if (flags & O_CREAT)
1092 mode = va_arg(args, mode_t);
1095 if (!function_enter()) {
1097 return _open(filename, flags, mode);
1100 if (dsp_cloak_enable() && (strcmp(filename, "/dev/dsp") == 0 || strcmp(filename, "/dev/adsp") == 0)) {
1101 r = dsp_open(flags, &_errno);
1102 } else if (mixer_cloak_enable() && strcmp(filename, "/dev/mixer") == 0) {
1103 r = mixer_open(flags, &_errno);
1104 } else if (sndstat_cloak_enable() && strcmp(filename, "/dev/sndstat") == 0) {
1105 r = sndstat_open(flags, &_errno);
1109 return _open(filename, flags, mode);
1120 static int mixer_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno) {
1124 case SOUND_MIXER_READ_DEVMASK :
1125 debug(__FILE__": SOUND_MIXER_READ_DEVMASK\n");
1127 *(int*) argp = SOUND_MASK_PCM;
1130 case SOUND_MIXER_READ_RECMASK :
1131 debug(__FILE__": SOUND_MIXER_READ_RECMASK\n");
1136 case SOUND_MIXER_READ_STEREODEVS:
1137 debug(__FILE__": SOUND_MIXER_READ_STEREODEVS\n");
1139 pa_threaded_mainloop_lock(i->mainloop);
1140 *(int*) argp = i->volume.channels > 1 ? SOUND_MASK_PCM : 0;
1141 pa_threaded_mainloop_unlock(i->mainloop);
1145 case SOUND_MIXER_READ_RECSRC:
1146 debug(__FILE__": SOUND_MIXER_READ_RECSRC\n");
1151 case SOUND_MIXER_READ_CAPS:
1152 debug(__FILE__": SOUND_MIXER_READ_CAPS\n");
1157 case SOUND_MIXER_READ_PCM:
1159 debug(__FILE__": SOUND_MIXER_READ_PCM\n");
1161 pa_threaded_mainloop_lock(i->mainloop);
1164 ((i->volume.values[0]*100/PA_VOLUME_NORM)) |
1165 ((i->volume.values[i->volume.channels > 1 ? 1 : 0]*100/PA_VOLUME_NORM) << 8);
1167 pa_threaded_mainloop_unlock(i->mainloop);
1171 case SOUND_MIXER_WRITE_PCM: {
1174 debug(__FILE__": SOUND_MIXER_WRITE_PCM\n");
1176 pa_threaded_mainloop_lock(i->mainloop);
1180 i->volume.values[0] = ((*(int*) argp & 0xFF)*PA_VOLUME_NORM)/100;
1181 i->volume.values[1] = ((*(int*) argp >> 8)*PA_VOLUME_NORM)/100;
1183 if (!pa_cvolume_equal(&i->volume, &v)) {
1186 if (!(o = pa_context_set_sink_volume_by_index(i->context, i->sink_index, &i->volume, NULL, NULL)))
1187 debug(__FILE__":Failed set volume: %s", pa_strerror(pa_context_errno(i->context)));
1190 i->operation_success = 0;
1191 while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
1192 CONTEXT_CHECK_DEAD_GOTO(i, exit_loop);
1194 pa_threaded_mainloop_wait(i->mainloop);
1198 if (!i->operation_success)
1199 debug(__FILE__": Failed to set volume: %s\n", pa_strerror(pa_context_errno(i->context)));
1201 pa_operation_unref(o);
1204 /* We don't wait for completion here */
1205 i->volume_modify_count++;
1208 pa_threaded_mainloop_unlock(i->mainloop);
1213 case SOUND_MIXER_INFO: {
1214 mixer_info *mi = argp;
1216 memset(mi, 0, sizeof(mixer_info));
1217 strncpy(mi->id, "POLYPAUDIO", sizeof(mi->id));
1218 strncpy(mi->name, "Polypaudio Virtual OSS", sizeof(mi->name));
1219 pa_threaded_mainloop_lock(i->mainloop);
1220 mi->modify_counter = i->volume_modify_count;
1221 pa_threaded_mainloop_unlock(i->mainloop);
1226 debug(__FILE__": unknown ioctl 0x%08lx\n", request);
1239 static int map_format(int *fmt, pa_sample_spec *ss) {
1243 ss->format = PA_SAMPLE_ULAW;
1247 ss->format = PA_SAMPLE_ALAW;
1254 ss->format = PA_SAMPLE_U8;
1261 ss->format = PA_SAMPLE_S16BE;
1268 ss->format = PA_SAMPLE_S16LE;
1272 ss->format = PA_SAMPLE_S16NE;
1280 static int map_format_back(pa_sample_format_t format) {
1282 case PA_SAMPLE_S16LE: return AFMT_S16_LE;
1283 case PA_SAMPLE_S16BE: return AFMT_S16_BE;
1284 case PA_SAMPLE_ULAW: return AFMT_MU_LAW;
1285 case PA_SAMPLE_ALAW: return AFMT_A_LAW;
1286 case PA_SAMPLE_U8: return AFMT_U8;
1292 static int dsp_flush_socket(fd_info *i) {
1295 if (i->thread_fd < 0)
1298 if (ioctl(i->thread_fd, SIOCINQ, &l) < 0) {
1299 debug(__FILE__": SIOCINQ: %s\n", strerror(errno));
1307 k = (size_t) l > sizeof(buf) ? sizeof(buf) : (size_t) l;
1308 if (read(i->thread_fd, buf, k) < 0)
1309 debug(__FILE__": read(): %s\n", strerror(errno));
1316 static int dsp_empty_socket(fd_info *i) {
1319 /* Empty the socket */
1323 if (i->thread_fd < 0)
1326 if (ioctl(i->thread_fd, SIOCINQ, &l) < 0) {
1327 debug(__FILE__": SIOCINQ: %s\n", strerror(errno));
1336 pa_threaded_mainloop_wait(i->mainloop);
1342 static int dsp_drain(fd_info *i) {
1343 pa_operation *o = NULL;
1349 debug(__FILE__": Draining.\n");
1351 pa_threaded_mainloop_lock(i->mainloop);
1353 if (dsp_empty_socket(i) < 0)
1359 debug(__FILE__": Really draining.\n");
1361 if (!(o = pa_stream_drain(i->stream, stream_success_cb, i))) {
1362 debug(__FILE__": pa_stream_drain(): %s\n", pa_strerror(pa_context_errno(i->context)));
1366 i->operation_success = 0;
1367 while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
1368 STREAM_CHECK_DEAD_GOTO(i, fail);
1370 pa_threaded_mainloop_wait(i->mainloop);
1373 if (!i->operation_success) {
1374 debug(__FILE__": pa_stream_drain() 2: %s\n", pa_strerror(pa_context_errno(i->context)));
1383 pa_operation_unref(o);
1385 pa_threaded_mainloop_unlock(i->mainloop);
1390 static int dsp_trigger(fd_info *i) {
1391 pa_operation *o = NULL;
1397 pa_threaded_mainloop_lock(i->mainloop);
1399 if (dsp_empty_socket(i) < 0)
1402 debug(__FILE__": Triggering.\n");
1404 if (!(o = pa_stream_trigger(i->stream, stream_success_cb, i))) {
1405 debug(__FILE__": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i->context)));
1409 i->operation_success = 0;
1410 while (!pa_operation_get_state(o) != PA_OPERATION_DONE) {
1411 STREAM_CHECK_DEAD_GOTO(i, fail);
1413 pa_threaded_mainloop_wait(i->mainloop);
1416 if (!i->operation_success) {
1417 debug(__FILE__": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i->context)));
1426 pa_operation_unref(o);
1428 pa_threaded_mainloop_unlock(i->mainloop);
1433 static int dsp_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno) {
1437 case SNDCTL_DSP_SETFMT: {
1438 debug(__FILE__": SNDCTL_DSP_SETFMT: %i\n", *(int*) argp);
1440 pa_threaded_mainloop_lock(i->mainloop);
1442 if (*(int*) argp == AFMT_QUERY)
1443 *(int*) argp = map_format_back(i->sample_spec.format);
1445 map_format((int*) argp, &i->sample_spec);
1449 pa_threaded_mainloop_unlock(i->mainloop);
1453 case SNDCTL_DSP_SPEED: {
1458 debug(__FILE__": SNDCTL_DSP_SPEED: %i\n", *(int*) argp);
1460 pa_threaded_mainloop_lock(i->mainloop);
1462 ss = i->sample_spec;
1463 ss.rate = *(int*) argp;
1465 if ((valid = pa_sample_spec_valid(&ss))) {
1466 i->sample_spec = ss;
1470 debug(__FILE__": ss: %s\n", pa_sample_spec_snprint(t, sizeof(t), &i->sample_spec));
1472 pa_threaded_mainloop_unlock(i->mainloop);
1482 case SNDCTL_DSP_STEREO:
1483 debug(__FILE__": SNDCTL_DSP_STEREO: %i\n", *(int*) argp);
1485 pa_threaded_mainloop_lock(i->mainloop);
1487 i->sample_spec.channels = *(int*) argp ? 2 : 1;
1490 pa_threaded_mainloop_unlock(i->mainloop);
1493 case SNDCTL_DSP_CHANNELS: {
1497 debug(__FILE__": SNDCTL_DSP_CHANNELS: %i\n", *(int*) argp);
1499 pa_threaded_mainloop_lock(i->mainloop);
1501 ss = i->sample_spec;
1502 ss.channels = *(int*) argp;
1504 if ((valid = pa_sample_spec_valid(&ss))) {
1505 i->sample_spec = ss;
1509 pa_threaded_mainloop_unlock(i->mainloop);
1519 case SNDCTL_DSP_GETBLKSIZE:
1520 debug(__FILE__": SNDCTL_DSP_GETBLKSIZE\n");
1522 pa_threaded_mainloop_lock(i->mainloop);
1525 *(int*) argp = i->fragment_size;
1527 pa_threaded_mainloop_unlock(i->mainloop);
1531 case SNDCTL_DSP_SETFRAGMENT:
1532 debug(__FILE__": SNDCTL_DSP_SETFRAGMENT: 0x%8x\n", *(int*) argp);
1534 pa_threaded_mainloop_lock(i->mainloop);
1536 i->fragment_size = 1 << (*(int*) argp);
1537 i->n_fragments = (*(int*) argp) >> 16;
1541 pa_threaded_mainloop_unlock(i->mainloop);
1545 case SNDCTL_DSP_GETCAPS:
1546 debug(__FILE__": SNDCTL_DSP_CAPS\n");
1548 *(int*) argp = DSP_CAP_MULTI;
1551 case SNDCTL_DSP_GETODELAY: {
1554 debug(__FILE__": SNDCTL_DSP_GETODELAY\n");
1556 pa_threaded_mainloop_lock(i->mainloop);
1563 STREAM_CHECK_DEAD_GOTO(i, exit_loop);
1565 if (pa_stream_get_latency(i->stream, &usec, NULL) >= 0) {
1566 *(int*) argp = pa_usec_to_bytes(usec, &i->sample_spec);
1570 if (pa_context_errno(i->context) != PA_ERR_NODATA) {
1571 debug(__FILE__": pa_stream_get_latency(): %s\n", pa_strerror(pa_context_errno(i->context)));
1575 pa_threaded_mainloop_wait(i->mainloop);
1580 if (ioctl(i->thread_fd, SIOCINQ, &l) < 0)
1581 debug(__FILE__": SIOCINQ failed: %s\n", strerror(errno));
1585 pa_threaded_mainloop_unlock(i->mainloop);
1587 debug(__FILE__": ODELAY: %i\n", *(int*) argp);
1592 case SNDCTL_DSP_RESET: {
1593 debug(__FILE__": SNDCTL_DSP_RESET\n");
1595 pa_threaded_mainloop_lock(i->mainloop);
1598 dsp_flush_socket(i);
1601 pa_threaded_mainloop_unlock(i->mainloop);
1605 case SNDCTL_DSP_GETFMTS: {
1606 debug(__FILE__": SNDCTL_DSP_GETFMTS\n");
1608 *(int*) argp = AFMT_MU_LAW|AFMT_A_LAW|AFMT_U8|AFMT_S16_LE|AFMT_S16_BE;
1612 case SNDCTL_DSP_POST:
1613 debug(__FILE__": SNDCTL_DSP_POST\n");
1615 if (dsp_trigger(i) < 0)
1619 case SNDCTL_DSP_SYNC:
1620 debug(__FILE__": SNDCTL_DSP_SYNC\n");
1622 if (dsp_drain(i) < 0)
1627 case SNDCTL_DSP_GETOSPACE: {
1628 audio_buf_info *bi = (audio_buf_info*) argp;
1632 debug(__FILE__": SNDCTL_DSP_GETOSPACE\n");
1634 pa_threaded_mainloop_lock(i->mainloop);
1639 if ((k = pa_stream_writable_size(i->stream)) == (size_t) -1)
1640 debug(__FILE__": pa_stream_writable_size(): %s\n", pa_strerror(pa_context_errno(i->context)));
1642 k = i->fragment_size * i->n_fragments;
1644 if (ioctl(i->thread_fd, SIOCINQ, &l) < 0) {
1645 debug(__FILE__": SIOCINQ failed: %s\n", strerror(errno));
1649 bi->fragsize = i->fragment_size;
1650 bi->fragstotal = i->n_fragments;
1651 bi->bytes = k > (size_t) l ? k - l : 0;
1652 bi->fragments = bi->bytes / bi->fragsize;
1654 pa_threaded_mainloop_unlock(i->mainloop);
1656 debug(__FILE__": fragsize=%i, fragstotal=%i, bytes=%i, fragments=%i\n", bi->fragsize, bi->fragstotal, bi->bytes, bi->fragments);
1662 debug(__FILE__": unknwon ioctl 0x%08lx\n", request);
1675 int ioctl(int fd, unsigned long request, ...) {
1681 debug(__FILE__": ioctl()\n");
1683 va_start(args, request);
1684 argp = va_arg(args, void *);
1687 if (!function_enter()) {
1689 return _ioctl(fd, request, argp);
1692 if (!(i = fd_info_find(fd))) {
1695 return _ioctl(fd, request, argp);
1698 if (i->type == FD_INFO_MIXER)
1699 r = mixer_ioctl(i, request, argp, &_errno);
1701 r = dsp_ioctl(i, request, argp, &_errno);
1716 debug(__FILE__": close()\n");
1718 if (!function_enter()) {
1723 if (!(i = fd_info_find(fd))) {
1729 fd_info_remove_from_list(i);
1737 int access(const char *pathname, int mode) {
1738 debug(__FILE__": access()\n");
1740 if (strcmp(pathname, "/dev/dsp") != 0 &&
1741 strcmp(pathname, "/dev/adsp") != 0 &&
1742 strcmp(pathname, "/dev/sndstat") != 0 &&
1743 strcmp(pathname, "/dev/mixer") != 0) {
1745 return _access(pathname, mode);
1748 if (mode & (W_OK | X_OK)) {
1756 int open64(const char *filename, int flags, ...) {
1760 debug(__FILE__": open64(%s)\n", filename);
1762 va_start(args, flags);
1763 if (flags & O_CREAT)
1764 mode = va_arg(args, mode_t);
1767 if (strcmp(filename, "/dev/dsp") != 0 &&
1768 strcmp(filename, "/dev/adsp") != 0 &&
1769 strcmp(filename, "/dev/sndstat") != 0 &&
1770 strcmp(filename, "/dev/mixer") != 0) {
1772 return _open64(filename, flags, mode);
1775 return open(filename, flags, mode);
1778 FILE* fopen(const char *filename, const char *mode) {
1783 debug(__FILE__": fopen(%s)\n", filename);
1785 if (strcmp(filename, "/dev/dsp") == 0 ||
1786 strcmp(filename, "/dev/adsp") == 0) {
1788 if (strcmp(mode, "wb") != 0) {
1794 } else if (strcmp(filename, "/dev/sndstat") == 0) {
1796 if (strcmp(mode, "r") != 0) {
1802 } else if (strcmp(filename, "/dev/mixer") == 0)
1806 return _fopen(filename, mode);
1809 if ((fd = open(filename, m)) < 0)
1812 if (!(f = fdopen(fd, mode))) {
1820 FILE *fopen64(const char *filename, const char *mode) {
1822 debug(__FILE__": fopen64(%s)\n", filename);
1824 if (strcmp(filename, "/dev/dsp") != 0 &&
1825 strcmp(filename, "/dev/adsp") != 0 &&
1826 strcmp(filename, "/dev/sndstat") != 0 &&
1827 strcmp(filename, "/dev/mixer") != 0) {
1828 LOAD_FOPEN64_FUNC();
1829 return _fopen64(filename, mode);
1832 return fopen(filename, mode);
1835 int fclose(FILE *f) {
1838 debug(__FILE__": fclose()\n");
1840 if (!function_enter()) {
1845 if (!(i = fd_info_find(fileno(f)))) {
1851 fd_info_remove_from_list(i);
1853 /* Dirty trick to avoid that the fd is not freed twice, once by us
1854 * and once by the real fclose() */