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;
74 pa_stream *play_stream;
75 pa_stream *rec_stream;
77 pa_io_event *io_event;
82 int operation_success;
84 pa_cvolume sink_volume, source_volume;
85 uint32_t sink_index, source_index;
86 int volume_modify_count;
88 PA_LLIST_FIELDS(fd_info);
91 static int dsp_drain(fd_info *i);
92 static void fd_info_remove_from_list(fd_info *i);
94 static pthread_mutex_t fd_infos_mutex = PTHREAD_MUTEX_INITIALIZER;
95 static pthread_mutex_t func_mutex = PTHREAD_MUTEX_INITIALIZER;
97 static PA_LLIST_HEAD(fd_info, fd_infos) = NULL;
99 static int (*_ioctl)(int, int, void*) = NULL;
100 static int (*_close)(int) = NULL;
101 static int (*_open)(const char *, int, mode_t) = NULL;
102 static FILE* (*_fopen)(const char *path, const char *mode) = NULL;
103 static int (*_open64)(const char *, int, mode_t) = NULL;
104 static FILE* (*_fopen64)(const char *path, const char *mode) = NULL;
105 static int (*_fclose)(FILE *f) = NULL;
106 static int (*_access)(const char *, int) = NULL;
108 /* dlsym() violates ISO C, so confide the breakage into this function to
110 typedef void (*fnptr)(void);
111 static inline fnptr dlsym_fn(void *handle, const char *symbol) {
112 return (fnptr) (long) dlsym(handle, symbol);
115 #define LOAD_IOCTL_FUNC() \
117 pthread_mutex_lock(&func_mutex); \
119 _ioctl = (int (*)(int, int, void*)) dlsym_fn(RTLD_NEXT, "ioctl"); \
120 pthread_mutex_unlock(&func_mutex); \
123 #define LOAD_OPEN_FUNC() \
125 pthread_mutex_lock(&func_mutex); \
127 _open = (int (*)(const char *, int, mode_t)) dlsym_fn(RTLD_NEXT, "open"); \
128 pthread_mutex_unlock(&func_mutex); \
131 #define LOAD_OPEN64_FUNC() \
133 pthread_mutex_lock(&func_mutex); \
135 _open64 = (int (*)(const char *, int, mode_t)) dlsym_fn(RTLD_NEXT, "open64"); \
136 pthread_mutex_unlock(&func_mutex); \
139 #define LOAD_CLOSE_FUNC() \
141 pthread_mutex_lock(&func_mutex); \
143 _close = (int (*)(int)) dlsym_fn(RTLD_NEXT, "close"); \
144 pthread_mutex_unlock(&func_mutex); \
147 #define LOAD_ACCESS_FUNC() \
149 pthread_mutex_lock(&func_mutex); \
151 _access = (int (*)(const char*, int)) dlsym_fn(RTLD_NEXT, "access"); \
152 pthread_mutex_unlock(&func_mutex); \
155 #define LOAD_FOPEN_FUNC() \
157 pthread_mutex_lock(&func_mutex); \
159 _fopen = (FILE* (*)(const char *, const char*)) dlsym_fn(RTLD_NEXT, "fopen"); \
160 pthread_mutex_unlock(&func_mutex); \
163 #define LOAD_FOPEN64_FUNC() \
165 pthread_mutex_lock(&func_mutex); \
167 _fopen64 = (FILE* (*)(const char *, const char*)) dlsym_fn(RTLD_NEXT, "fopen64"); \
168 pthread_mutex_unlock(&func_mutex); \
171 #define LOAD_FCLOSE_FUNC() \
173 pthread_mutex_lock(&func_mutex); \
175 _fclose = (int (*)(FILE *)) dlsym_fn(RTLD_NEXT, "fclose"); \
176 pthread_mutex_unlock(&func_mutex); \
179 #define CONTEXT_CHECK_DEAD_GOTO(i, label) do { \
180 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY) { \
181 debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
186 #define PLAYBACK_STREAM_CHECK_DEAD_GOTO(i, label) do { \
187 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY || \
188 !(i)->play_stream || pa_stream_get_state((i)->play_stream) != PA_STREAM_READY) { \
189 debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
194 #define RECORD_STREAM_CHECK_DEAD_GOTO(i, label) do { \
195 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY || \
196 !(i)->rec_stream || pa_stream_get_state((i)->rec_stream) != PA_STREAM_READY) { \
197 debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
202 static void debug(int level, const char *format, ...) PA_GCC_PRINTF_ATTR(2,3);
204 #define DEBUG_LEVEL_ALWAYS 0
205 #define DEBUG_LEVEL_NORMAL 1
206 #define DEBUG_LEVEL_VERBOSE 2
208 static void debug(int level, const char *format, ...) {
210 const char *dlevel_s;
213 dlevel_s = getenv("PADSP_DEBUG");
217 dlevel = atoi(dlevel_s);
222 va_start(ap, format);
223 vfprintf(stderr, format, ap);
227 static int padsp_disabled(void) {
229 static int sym_resolved = 0;
231 /* If the current process has a symbol __padsp_disabled__ we use
232 * it to detect whether we should enable our stuff or not. A
233 * program needs to be compiled with -rdynamic for this to work!
234 * The symbol must be an int containing a three bit bitmask: bit 1
235 * -> disable /dev/dsp emulation, bit 2 -> disable /dev/sndstat
236 * emulation, bit 3 -> disable /dev/mixer emulation. Hence a value
237 * of 7 disables padsp entirely. */
239 pthread_mutex_lock(&func_mutex);
241 sym = (int*) dlsym(RTLD_DEFAULT, "__padsp_disabled__");
245 pthread_mutex_unlock(&func_mutex);
253 static int dsp_cloak_enable(void) {
254 if (padsp_disabled() & 1)
257 if (getenv("PADSP_NO_DSP"))
263 static int sndstat_cloak_enable(void) {
264 if (padsp_disabled() & 2)
267 if (getenv("PADSP_NO_SNDSTAT"))
273 static int mixer_cloak_enable(void) {
274 if (padsp_disabled() & 4)
277 if (getenv("PADSP_NO_MIXER"))
282 static pthread_key_t recursion_key;
284 static void recursion_key_alloc(void) {
285 pthread_key_create(&recursion_key, NULL);
288 static int function_enter(void) {
289 /* Avoid recursive calls */
290 static pthread_once_t recursion_key_once = PTHREAD_ONCE_INIT;
291 pthread_once(&recursion_key_once, recursion_key_alloc);
293 if (pthread_getspecific(recursion_key))
296 pthread_setspecific(recursion_key, (void*) 1);
300 static void function_exit(void) {
301 pthread_setspecific(recursion_key, NULL);
304 static void fd_info_free(fd_info *i) {
307 debug(DEBUG_LEVEL_NORMAL, __FILE__": freeing fd info (fd=%i)\n", i->app_fd);
312 pa_threaded_mainloop_stop(i->mainloop);
314 if (i->play_stream) {
315 pa_stream_disconnect(i->play_stream);
316 pa_stream_unref(i->play_stream);
320 pa_stream_disconnect(i->rec_stream);
321 pa_stream_unref(i->rec_stream);
325 pa_context_disconnect(i->context);
326 pa_context_unref(i->context);
330 pa_threaded_mainloop_free(i->mainloop);
332 if (i->app_fd >= 0) {
337 if (i->thread_fd >= 0) {
339 _close(i->thread_fd);
344 pthread_mutex_destroy(&i->mutex);
348 static fd_info *fd_info_ref(fd_info *i) {
351 pthread_mutex_lock(&i->mutex);
355 debug(DEBUG_LEVEL_VERBOSE, __FILE__": ref++, now %i\n", i->ref);
356 pthread_mutex_unlock(&i->mutex);
361 static void fd_info_unref(fd_info *i) {
363 pthread_mutex_lock(&i->mutex);
366 debug(DEBUG_LEVEL_VERBOSE, __FILE__": ref--, now %i\n", i->ref);
367 pthread_mutex_unlock(&i->mutex);
373 static void context_state_cb(pa_context *c, void *userdata) {
374 fd_info *i = userdata;
377 switch (pa_context_get_state(c)) {
378 case PA_CONTEXT_READY:
379 case PA_CONTEXT_TERMINATED:
380 case PA_CONTEXT_FAILED:
381 pa_threaded_mainloop_signal(i->mainloop, 0);
384 case PA_CONTEXT_UNCONNECTED:
385 case PA_CONTEXT_CONNECTING:
386 case PA_CONTEXT_AUTHORIZING:
387 case PA_CONTEXT_SETTING_NAME:
392 static void reset_params(fd_info *i) {
395 i->sample_spec.format = PA_SAMPLE_ULAW;
396 i->sample_spec.channels = 1;
397 i->sample_spec.rate = 8000;
398 i->fragment_size = 0;
402 static const char *client_name(char *buf, size_t n) {
406 if ((e = getenv("PADSP_CLIENT_NAME")))
409 if (pa_get_binary_name(p, sizeof(p)))
410 snprintf(buf, n, "OSS Emulation[%s]", pa_path_get_filename(p));
412 snprintf(buf, n, "OSS");
417 static const char *stream_name(void) {
420 if ((e = getenv("PADSP_STREAM_NAME")))
423 return "Audio Stream";
426 static void atfork_prepare(void) {
429 debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_prepare() enter\n");
433 pthread_mutex_lock(&fd_infos_mutex);
435 for (i = fd_infos; i; i = i->next) {
436 pthread_mutex_lock(&i->mutex);
437 pa_threaded_mainloop_lock(i->mainloop);
440 pthread_mutex_lock(&func_mutex);
443 debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_prepare() exit\n");
446 static void atfork_parent(void) {
449 debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_parent() enter\n");
451 pthread_mutex_unlock(&func_mutex);
453 for (i = fd_infos; i; i = i->next) {
454 pa_threaded_mainloop_unlock(i->mainloop);
455 pthread_mutex_unlock(&i->mutex);
458 pthread_mutex_unlock(&fd_infos_mutex);
462 debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_parent() exit\n");
465 static void atfork_child(void) {
468 debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_child() enter\n");
470 /* We do only the bare minimum to get all fds closed */
471 pthread_mutex_init(&func_mutex, NULL);
472 pthread_mutex_init(&fd_infos_mutex, NULL);
474 for (i = fd_infos; i; i = i->next) {
475 pthread_mutex_init(&i->mutex, NULL);
478 pa_context_disconnect(i->context);
479 pa_context_unref(i->context);
483 if (i->play_stream) {
484 pa_stream_unref(i->play_stream);
485 i->play_stream = NULL;
489 pa_stream_unref(i->rec_stream);
490 i->rec_stream = NULL;
493 if (i->app_fd >= 0) {
498 if (i->thread_fd >= 0) {
508 debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_child() exit\n");
511 static void install_atfork(void) {
512 pthread_atfork(atfork_prepare, atfork_parent, atfork_child);
515 static void stream_success_cb(pa_stream *s, int success, void *userdata) {
516 fd_info *i = userdata;
521 i->operation_success = success;
522 pa_threaded_mainloop_signal(i->mainloop, 0);
525 static void context_success_cb(pa_context *c, int success, void *userdata) {
526 fd_info *i = userdata;
531 i->operation_success = success;
532 pa_threaded_mainloop_signal(i->mainloop, 0);
535 static fd_info* fd_info_new(fd_info_type_t type, int *_errno) {
537 int sfds[2] = { -1, -1 };
539 static pthread_once_t install_atfork_once = PTHREAD_ONCE_INIT;
541 debug(DEBUG_LEVEL_NORMAL, __FILE__": fd_info_new()\n");
543 signal(SIGPIPE, SIG_IGN); /* Yes, ugly as hell */
545 pthread_once(&install_atfork_once, install_atfork);
547 if (!(i = malloc(sizeof(fd_info)))) {
552 i->app_fd = i->thread_fd = -1;
557 i->play_stream = NULL;
558 i->rec_stream = NULL;
560 pthread_mutex_init(&i->mutex, NULL);
565 pa_cvolume_reset(&i->sink_volume, 2);
566 pa_cvolume_reset(&i->source_volume, 2);
567 i->volume_modify_count = 0;
568 i->sink_index = (uint32_t) -1;
569 i->source_index = (uint32_t) -1;
570 PA_LLIST_INIT(fd_info, i);
574 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sfds) < 0) {
576 debug(DEBUG_LEVEL_NORMAL, __FILE__": socket() failed: %s\n", strerror(errno));
581 i->thread_fd = sfds[1];
583 if (!(i->mainloop = pa_threaded_mainloop_new())) {
585 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_threaded_mainloop_new() failed\n");
589 if (!(i->context = pa_context_new(pa_threaded_mainloop_get_api(i->mainloop), client_name(name, sizeof(name))))) {
591 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_context_new() failed\n");
595 pa_context_set_state_callback(i->context, context_state_cb, i);
597 if (pa_context_connect(i->context, NULL, 0, NULL) < 0) {
598 *_errno = ECONNREFUSED;
599 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
603 pa_threaded_mainloop_lock(i->mainloop);
605 if (pa_threaded_mainloop_start(i->mainloop) < 0) {
607 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_threaded_mainloop_start() failed\n");
608 goto unlock_and_fail;
611 /* Wait until the context is ready */
612 pa_threaded_mainloop_wait(i->mainloop);
614 if (pa_context_get_state(i->context) != PA_CONTEXT_READY) {
615 *_errno = ECONNREFUSED;
616 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
617 goto unlock_and_fail;
620 pa_threaded_mainloop_unlock(i->mainloop);
625 pa_threaded_mainloop_unlock(i->mainloop);
635 static void fd_info_add_to_list(fd_info *i) {
638 pthread_mutex_lock(&fd_infos_mutex);
639 PA_LLIST_PREPEND(fd_info, fd_infos, i);
640 pthread_mutex_unlock(&fd_infos_mutex);
645 static void fd_info_remove_from_list(fd_info *i) {
648 pthread_mutex_lock(&fd_infos_mutex);
649 PA_LLIST_REMOVE(fd_info, fd_infos, i);
650 pthread_mutex_unlock(&fd_infos_mutex);
655 static fd_info* fd_info_find(int fd) {
658 pthread_mutex_lock(&fd_infos_mutex);
660 for (i = fd_infos; i; i = i->next)
661 if (i->app_fd == fd && !i->unusable) {
666 pthread_mutex_unlock(&fd_infos_mutex);
671 static void fix_metrics(fd_info *i) {
673 char t[PA_SAMPLE_SPEC_SNPRINT_MAX];
675 fs = pa_frame_size(&i->sample_spec);
676 i->fragment_size = (i->fragment_size/fs)*fs;
678 /* Number of fragments set? */
679 if (i->n_fragments < 2) {
680 if (i->fragment_size > 0) {
681 i->n_fragments = pa_bytes_per_second(&i->sample_spec) / 2 / i->fragment_size;
682 if (i->n_fragments < 2)
688 /* Fragment size set? */
689 if (i->fragment_size <= 0) {
690 i->fragment_size = pa_bytes_per_second(&i->sample_spec) / 2 / i->n_fragments;
691 if (i->fragment_size < 1024)
692 i->fragment_size = 1024;
695 debug(DEBUG_LEVEL_NORMAL, __FILE__": sample spec: %s\n", pa_sample_spec_snprint(t, sizeof(t), &i->sample_spec));
696 debug(DEBUG_LEVEL_NORMAL, __FILE__": fixated metrics to %i fragments, %li bytes each.\n", i->n_fragments, (long)i->fragment_size);
699 static void stream_request_cb(pa_stream *s, size_t length, void *userdata) {
700 fd_info *i = userdata;
704 pa_mainloop_api *api;
705 pa_io_event_flags_t flags;
708 api = pa_threaded_mainloop_get_api(i->mainloop);
712 if (s == i->play_stream) {
713 n = pa_stream_writable_size(i->play_stream);
714 if (n == (size_t)-1) {
715 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_writable_size(): %s\n",
716 pa_strerror(pa_context_errno(i->context)));
719 if (n >= i->fragment_size)
720 flags |= PA_IO_EVENT_INPUT;
723 if (s == i->rec_stream) {
724 n = pa_stream_readable_size(i->rec_stream);
725 if (n == (size_t)-1) {
726 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_readable_size(): %s\n",
727 pa_strerror(pa_context_errno(i->context)));
730 if (n >= i->fragment_size)
731 flags |= PA_IO_EVENT_OUTPUT;
734 api->io_enable(i->io_event, flags);
738 static void stream_latency_update_cb(pa_stream *s, void *userdata) {
739 fd_info *i = userdata;
742 pa_threaded_mainloop_signal(i->mainloop, 0);
745 static void fd_info_shutdown(fd_info *i) {
749 pa_mainloop_api *api;
750 api = pa_threaded_mainloop_get_api(i->mainloop);
751 api->io_free(i->io_event);
755 if (i->thread_fd >= 0) {
761 static int fd_info_copy_data(fd_info *i, int force) {
763 pa_io_event_flags_t flags;
765 if (!i->play_stream && !i->rec_stream)
770 if (i->play_stream) {
771 n = pa_stream_writable_size(i->play_stream);
773 if (n == (size_t)-1) {
774 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_writable_size(): %s\n",
775 pa_strerror(pa_context_errno(i->context)));
779 while (n >= i->fragment_size || force) {
783 if (!(i->buf = malloc(i->fragment_size))) {
784 debug(DEBUG_LEVEL_NORMAL, __FILE__": malloc() failed.\n");
789 if ((r = read(i->thread_fd, i->buf, i->fragment_size)) <= 0) {
794 debug(DEBUG_LEVEL_NORMAL, __FILE__": read(): %s\n", r == 0 ? "EOF" : strerror(errno));
798 if (pa_stream_write(i->play_stream, i->buf, r, free, 0, PA_SEEK_RELATIVE) < 0) {
799 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_write(): %s\n", pa_strerror(pa_context_errno(i->context)));
805 assert(n >= (size_t) r);
809 if (n >= i->fragment_size)
810 flags |= PA_IO_EVENT_INPUT;
814 n = pa_stream_readable_size(i->rec_stream);
816 if (n == (size_t)-1) {
817 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_readable_size(): %s\n",
818 pa_strerror(pa_context_errno(i->context)));
822 while (n >= i->fragment_size || force) {
828 if (pa_stream_peek(i->rec_stream, &data, &len) < 0) {
829 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_peek(): %s\n", pa_strerror(pa_context_errno(i->context)));
836 buf = (const char*)data + i->rec_offset;
838 if ((r = write(i->thread_fd, buf, len - i->rec_offset)) <= 0) {
843 debug(DEBUG_LEVEL_NORMAL, __FILE__": write(): %s\n", strerror(errno));
847 assert((size_t)r <= len - i->rec_offset);
850 if (i->rec_offset == len) {
851 if (pa_stream_drop(i->rec_stream) < 0) {
852 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_drop(): %s\n", pa_strerror(pa_context_errno(i->context)));
858 assert(n >= (size_t) r);
862 if (n >= i->fragment_size)
863 flags |= PA_IO_EVENT_OUTPUT;
867 pa_mainloop_api *api;
869 api = pa_threaded_mainloop_get_api(i->mainloop);
870 api->io_enable(i->io_event, flags);
876 static void stream_state_cb(pa_stream *s, void * userdata) {
877 fd_info *i = userdata;
880 switch (pa_stream_get_state(s)) {
882 case PA_STREAM_READY:
883 debug(DEBUG_LEVEL_NORMAL, __FILE__": stream established.\n");
886 case PA_STREAM_FAILED:
887 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
891 case PA_STREAM_TERMINATED:
892 case PA_STREAM_UNCONNECTED:
893 case PA_STREAM_CREATING:
898 static int create_playback_stream(fd_info *i) {
906 if (!(i->play_stream = pa_stream_new(i->context, stream_name(), &i->sample_spec, NULL))) {
907 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
911 pa_stream_set_state_callback(i->play_stream, stream_state_cb, i);
912 pa_stream_set_write_callback(i->play_stream, stream_request_cb, i);
913 pa_stream_set_latency_update_callback(i->play_stream, stream_latency_update_cb, i);
915 memset(&attr, 0, sizeof(attr));
916 attr.maxlength = i->fragment_size * (i->n_fragments+1);
917 attr.tlength = i->fragment_size * i->n_fragments;
918 attr.prebuf = i->fragment_size;
919 attr.minreq = i->fragment_size;
921 if (pa_stream_connect_playback(i->play_stream, NULL, &attr, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE, NULL, NULL) < 0) {
922 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
926 n = i->fragment_size;
927 setsockopt(i->app_fd, SOL_SOCKET, SO_SNDBUF, &n, sizeof(n));
928 n = i->fragment_size;
929 setsockopt(i->thread_fd, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n));
937 static int create_record_stream(fd_info *i) {
945 if (!(i->rec_stream = pa_stream_new(i->context, stream_name(), &i->sample_spec, NULL))) {
946 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
950 pa_stream_set_state_callback(i->rec_stream, stream_state_cb, i);
951 pa_stream_set_read_callback(i->rec_stream, stream_request_cb, i);
952 pa_stream_set_latency_update_callback(i->rec_stream, stream_latency_update_cb, i);
954 memset(&attr, 0, sizeof(attr));
955 attr.maxlength = i->fragment_size * (i->n_fragments+1);
956 attr.fragsize = i->fragment_size;
958 if (pa_stream_connect_record(i->rec_stream, NULL, &attr, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE) < 0) {
959 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
963 n = i->fragment_size;
964 setsockopt(i->app_fd, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n));
965 n = i->fragment_size;
966 setsockopt(i->thread_fd, SOL_SOCKET, SO_SNDBUF, &n, sizeof(n));
974 static void free_streams(fd_info *i) {
977 if (i->play_stream) {
978 pa_stream_disconnect(i->play_stream);
979 pa_stream_unref(i->play_stream);
980 i->play_stream = NULL;
984 pa_stream_disconnect(i->rec_stream);
985 pa_stream_unref(i->rec_stream);
986 i->rec_stream = NULL;
990 static void io_event_cb(pa_mainloop_api *api, pa_io_event *e, int fd, pa_io_event_flags_t flags, void *userdata) {
991 fd_info *i = userdata;
993 pa_threaded_mainloop_signal(i->mainloop, 0);
995 if (flags & PA_IO_EVENT_INPUT) {
997 if (!i->play_stream) {
998 if (create_playback_stream(i) < 0)
1001 if (fd_info_copy_data(i, 0) < 0)
1005 } else if (flags & PA_IO_EVENT_OUTPUT) {
1007 if (!i->rec_stream) {
1008 if (create_record_stream(i) < 0)
1011 if (fd_info_copy_data(i, 0) < 0)
1015 } else if (flags & (PA_IO_EVENT_HANGUP|PA_IO_EVENT_ERROR))
1021 /* We can't do anything better than removing the event source */
1022 fd_info_shutdown(i);
1025 static int dsp_open(int flags, int *_errno) {
1027 pa_mainloop_api *api;
1030 pa_io_event_flags_t ioflags;
1032 debug(DEBUG_LEVEL_NORMAL, __FILE__": dsp_open()\n");
1034 if (!(i = fd_info_new(FD_INFO_STREAM, _errno)))
1037 if ((flags & O_NONBLOCK) == O_NONBLOCK) {
1038 if ((f = fcntl(i->app_fd, F_GETFL)) >= 0)
1039 fcntl(i->app_fd, F_SETFL, f|O_NONBLOCK);
1041 if ((f = fcntl(i->thread_fd, F_GETFL)) >= 0)
1042 fcntl(i->thread_fd, F_SETFL, f|O_NONBLOCK);
1044 fcntl(i->app_fd, F_SETFD, FD_CLOEXEC);
1045 fcntl(i->thread_fd, F_SETFD, FD_CLOEXEC);
1047 pa_threaded_mainloop_lock(i->mainloop);
1048 api = pa_threaded_mainloop_get_api(i->mainloop);
1050 switch (flags & O_ACCMODE) {
1052 ioflags = PA_IO_EVENT_OUTPUT;
1053 shutdown(i->thread_fd, SHUT_RD);
1054 shutdown(i->app_fd, SHUT_WR);
1057 ioflags = PA_IO_EVENT_INPUT;
1058 shutdown(i->thread_fd, SHUT_WR);
1059 shutdown(i->app_fd, SHUT_RD);
1062 ioflags = PA_IO_EVENT_INPUT | PA_IO_EVENT_OUTPUT;
1068 if (!(i->io_event = api->io_new(api, i->thread_fd, ioflags, io_event_cb, i)))
1071 pa_threaded_mainloop_unlock(i->mainloop);
1073 debug(DEBUG_LEVEL_NORMAL, __FILE__": dsp_open() succeeded, fd=%i\n", i->app_fd);
1075 fd_info_add_to_list(i);
1082 pa_threaded_mainloop_unlock(i->mainloop);
1089 debug(DEBUG_LEVEL_NORMAL, __FILE__": dsp_open() failed\n");
1094 static void sink_info_cb(pa_context *context, const pa_sink_info *si, int eol, void *userdata) {
1095 fd_info *i = userdata;
1097 if (!si && eol < 0) {
1098 i->operation_success = 0;
1099 pa_threaded_mainloop_signal(i->mainloop, 0);
1106 if (!pa_cvolume_equal(&i->sink_volume, &si->volume))
1107 i->volume_modify_count++;
1109 i->sink_volume = si->volume;
1110 i->sink_index = si->index;
1112 i->operation_success = 1;
1113 pa_threaded_mainloop_signal(i->mainloop, 0);
1116 static void source_info_cb(pa_context *context, const pa_source_info *si, int eol, void *userdata) {
1117 fd_info *i = userdata;
1119 if (!si && eol < 0) {
1120 i->operation_success = 0;
1121 pa_threaded_mainloop_signal(i->mainloop, 0);
1128 if (!pa_cvolume_equal(&i->source_volume, &si->volume))
1129 i->volume_modify_count++;
1131 i->source_volume = si->volume;
1132 i->source_index = si->index;
1134 i->operation_success = 1;
1135 pa_threaded_mainloop_signal(i->mainloop, 0);
1138 static void subscribe_cb(pa_context *context, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
1139 fd_info *i = userdata;
1140 pa_operation *o = NULL;
1142 if (i->sink_index != idx)
1145 if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) != PA_SUBSCRIPTION_EVENT_CHANGE)
1148 if (!(o = pa_context_get_sink_info_by_index(i->context, i->sink_index, sink_info_cb, i))) {
1149 debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to get sink info: %s", pa_strerror(pa_context_errno(i->context)));
1153 pa_operation_unref(o);
1156 static int mixer_open(int flags, int *_errno) {
1158 pa_operation *o = NULL;
1161 debug(DEBUG_LEVEL_NORMAL, __FILE__": mixer_open()\n");
1163 if (!(i = fd_info_new(FD_INFO_MIXER, _errno)))
1166 pa_threaded_mainloop_lock(i->mainloop);
1168 pa_context_set_subscribe_callback(i->context, subscribe_cb, i);
1170 if (!(o = pa_context_subscribe(i->context, PA_SUBSCRIPTION_MASK_SINK | PA_SUBSCRIPTION_MASK_SOURCE, context_success_cb, i))) {
1171 debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to subscribe to events: %s", pa_strerror(pa_context_errno(i->context)));
1176 i->operation_success = 0;
1177 while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
1178 pa_threaded_mainloop_wait(i->mainloop);
1179 CONTEXT_CHECK_DEAD_GOTO(i, fail);
1182 pa_operation_unref(o);
1185 if (!i->operation_success) {
1186 debug(DEBUG_LEVEL_NORMAL, __FILE__":Failed to subscribe to events: %s", pa_strerror(pa_context_errno(i->context)));
1193 if (!(o = pa_context_get_sink_info_by_name(i->context, NULL, sink_info_cb, i))) {
1194 debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to get sink info: %s", pa_strerror(pa_context_errno(i->context)));
1199 i->operation_success = 0;
1200 while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
1201 pa_threaded_mainloop_wait(i->mainloop);
1202 CONTEXT_CHECK_DEAD_GOTO(i, fail);
1205 pa_operation_unref(o);
1208 if (!i->operation_success) {
1209 debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to get sink info: %s", pa_strerror(pa_context_errno(i->context)));
1214 /* Get source info */
1216 if (!(o = pa_context_get_source_info_by_name(i->context, NULL, source_info_cb, i))) {
1217 debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to get source info: %s", pa_strerror(pa_context_errno(i->context)));
1222 i->operation_success = 0;
1223 while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
1224 pa_threaded_mainloop_wait(i->mainloop);
1225 CONTEXT_CHECK_DEAD_GOTO(i, fail);
1228 pa_operation_unref(o);
1231 if (!i->operation_success) {
1232 debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to get source info: %s", pa_strerror(pa_context_errno(i->context)));
1237 pa_threaded_mainloop_unlock(i->mainloop);
1239 debug(DEBUG_LEVEL_NORMAL, __FILE__": mixer_open() succeeded, fd=%i\n", i->app_fd);
1241 fd_info_add_to_list(i);
1249 pa_operation_unref(o);
1251 pa_threaded_mainloop_unlock(i->mainloop);
1258 debug(DEBUG_LEVEL_NORMAL, __FILE__": mixer_open() failed\n");
1263 static int sndstat_open(int flags, int *_errno) {
1264 static const char sndstat[] =
1265 "Sound Driver:3.8.1a-980706 (Polypaudio Virtual OSS)\n"
1267 "Config options: 0\n"
1269 "Installed drivers:\n"
1270 "Type 255: Polypaudio Virtual OSS\n"
1273 "Polypaudio Virtual OSS\n"
1276 "0: Polypaudio Virtual OSS\n"
1278 "Synth devices: NOT ENABLED IN CONFIG\n"
1285 "0: Polypaudio Virtual OSS\n";
1287 char fn[] = "/tmp/padsp-sndstat-XXXXXX";
1292 debug(DEBUG_LEVEL_NORMAL, __FILE__": sndstat_open()\n");
1294 if (flags != O_RDONLY && flags != (O_RDONLY|O_LARGEFILE)) {
1296 debug(DEBUG_LEVEL_NORMAL, __FILE__": bad access!\n");
1307 debug(DEBUG_LEVEL_NORMAL, __FILE__": mkstemp() failed: %s\n", strerror(errno));
1313 if (write(fd, sndstat, sizeof(sndstat) -1) != sizeof(sndstat)-1) {
1315 debug(DEBUG_LEVEL_NORMAL, __FILE__": write() failed: %s\n", strerror(errno));
1319 if (lseek(fd, SEEK_SET, 0) < 0) {
1321 debug(DEBUG_LEVEL_NORMAL, __FILE__": lseek() failed: %s\n", strerror(errno));
1333 int open(const char *filename, int flags, ...) {
1338 debug(DEBUG_LEVEL_VERBOSE, __FILE__": open(%s)\n", filename);
1340 va_start(args, flags);
1341 if (flags & O_CREAT)
1342 mode = va_arg(args, mode_t);
1345 if (!function_enter()) {
1347 return _open(filename, flags, mode);
1350 if (dsp_cloak_enable() && (strcmp(filename, "/dev/dsp") == 0 || strcmp(filename, "/dev/adsp") == 0)) {
1351 r = dsp_open(flags, &_errno);
1352 } else if (mixer_cloak_enable() && strcmp(filename, "/dev/mixer") == 0) {
1353 r = mixer_open(flags, &_errno);
1354 } else if (sndstat_cloak_enable() && strcmp(filename, "/dev/sndstat") == 0) {
1355 r = sndstat_open(flags, &_errno);
1359 return _open(filename, flags, mode);
1370 static int mixer_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno) {
1374 case SOUND_MIXER_READ_DEVMASK :
1375 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_DEVMASK\n");
1377 *(int*) argp = SOUND_MASK_PCM | SOUND_MASK_IGAIN;
1380 case SOUND_MIXER_READ_RECMASK :
1381 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_RECMASK\n");
1383 *(int*) argp = SOUND_MASK_IGAIN;
1386 case SOUND_MIXER_READ_STEREODEVS:
1387 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_STEREODEVS\n");
1389 pa_threaded_mainloop_lock(i->mainloop);
1391 if (i->sink_volume.channels > 1)
1392 *(int*) argp |= SOUND_MASK_PCM;
1393 if (i->source_volume.channels > 1)
1394 *(int*) argp |= SOUND_MASK_IGAIN;
1395 pa_threaded_mainloop_unlock(i->mainloop);
1399 case SOUND_MIXER_READ_RECSRC:
1400 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_RECSRC\n");
1402 *(int*) argp = SOUND_MASK_IGAIN;
1405 case SOUND_MIXER_WRITE_RECSRC:
1406 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_WRITE_RECSRC\n");
1409 case SOUND_MIXER_READ_CAPS:
1410 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_CAPS\n");
1415 case SOUND_MIXER_READ_PCM:
1416 case SOUND_MIXER_READ_IGAIN: {
1419 if (request == SOUND_MIXER_READ_PCM)
1420 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_PCM\n");
1422 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_IGAIN\n");
1424 pa_threaded_mainloop_lock(i->mainloop);
1426 if (request == SOUND_MIXER_READ_PCM)
1427 v = &i->sink_volume;
1429 v = &i->source_volume;
1432 ((v->values[0]*100/PA_VOLUME_NORM)) |
1433 ((v->values[v->channels > 1 ? 1 : 0]*100/PA_VOLUME_NORM) << 8);
1435 pa_threaded_mainloop_unlock(i->mainloop);
1440 case SOUND_MIXER_WRITE_PCM:
1441 case SOUND_MIXER_WRITE_IGAIN: {
1444 if (request == SOUND_MIXER_READ_PCM)
1445 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_WRITE_PCM\n");
1447 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_WRITE_IGAIN\n");
1449 pa_threaded_mainloop_lock(i->mainloop);
1451 if (request == SOUND_MIXER_READ_PCM) {
1453 pv = &i->sink_volume;
1455 v = i->source_volume;
1456 pv = &i->source_volume;
1459 pv->values[0] = ((*(int*) argp & 0xFF)*PA_VOLUME_NORM)/100;
1460 pv->values[1] = ((*(int*) argp >> 8)*PA_VOLUME_NORM)/100;
1462 if (!pa_cvolume_equal(pv, &v)) {
1465 if (request == SOUND_MIXER_READ_PCM)
1466 o = pa_context_set_sink_volume_by_index(i->context, i->sink_index, pv, NULL, NULL);
1468 o = pa_context_set_source_volume_by_index(i->context, i->source_index, pv, NULL, NULL);
1471 debug(DEBUG_LEVEL_NORMAL, __FILE__":Failed set volume: %s", pa_strerror(pa_context_errno(i->context)));
1474 i->operation_success = 0;
1475 while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
1476 CONTEXT_CHECK_DEAD_GOTO(i, exit_loop);
1478 pa_threaded_mainloop_wait(i->mainloop);
1482 if (!i->operation_success)
1483 debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to set volume: %s\n", pa_strerror(pa_context_errno(i->context)));
1485 pa_operation_unref(o);
1488 /* We don't wait for completion here */
1489 i->volume_modify_count++;
1492 pa_threaded_mainloop_unlock(i->mainloop);
1497 case SOUND_MIXER_INFO: {
1498 mixer_info *mi = argp;
1500 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_INFO\n");
1502 memset(mi, 0, sizeof(mixer_info));
1503 strncpy(mi->id, "POLYPAUDIO", sizeof(mi->id));
1504 strncpy(mi->name, "Polypaudio Virtual OSS", sizeof(mi->name));
1505 pa_threaded_mainloop_lock(i->mainloop);
1506 mi->modify_counter = i->volume_modify_count;
1507 pa_threaded_mainloop_unlock(i->mainloop);
1512 debug(DEBUG_LEVEL_NORMAL, __FILE__": unknown ioctl 0x%08lx\n", request);
1525 static int map_format(int *fmt, pa_sample_spec *ss) {
1529 ss->format = PA_SAMPLE_ULAW;
1533 ss->format = PA_SAMPLE_ALAW;
1540 ss->format = PA_SAMPLE_U8;
1547 ss->format = PA_SAMPLE_S16BE;
1554 ss->format = PA_SAMPLE_S16LE;
1558 ss->format = PA_SAMPLE_S16NE;
1566 static int map_format_back(pa_sample_format_t format) {
1568 case PA_SAMPLE_S16LE: return AFMT_S16_LE;
1569 case PA_SAMPLE_S16BE: return AFMT_S16_BE;
1570 case PA_SAMPLE_ULAW: return AFMT_MU_LAW;
1571 case PA_SAMPLE_ALAW: return AFMT_A_LAW;
1572 case PA_SAMPLE_U8: return AFMT_U8;
1578 static int dsp_flush_fd(int fd) {
1581 if (ioctl(fd, SIOCINQ, &l) < 0) {
1582 debug(DEBUG_LEVEL_NORMAL, __FILE__": SIOCINQ: %s\n", strerror(errno));
1590 k = (size_t) l > sizeof(buf) ? sizeof(buf) : (size_t) l;
1591 if (read(fd, buf, k) < 0)
1592 debug(DEBUG_LEVEL_NORMAL, __FILE__": read(): %s\n", strerror(errno));
1599 static int dsp_flush_socket(fd_info *i) {
1602 if ((i->thread_fd < 0) && (i->app_fd < 0))
1605 if (i->thread_fd >= 0)
1606 res = dsp_flush_fd(i->thread_fd);
1612 res = dsp_flush_fd(i->app_fd);
1620 static int dsp_empty_socket(fd_info *i) {
1623 /* Empty the socket */
1627 if (i->thread_fd < 0)
1630 if (ioctl(i->thread_fd, SIOCINQ, &l) < 0) {
1631 debug(DEBUG_LEVEL_NORMAL, __FILE__": SIOCINQ: %s\n", strerror(errno));
1640 pa_threaded_mainloop_wait(i->mainloop);
1646 static int dsp_drain(fd_info *i) {
1647 pa_operation *o = NULL;
1653 debug(DEBUG_LEVEL_NORMAL, __FILE__": Draining.\n");
1655 pa_threaded_mainloop_lock(i->mainloop);
1657 if (dsp_empty_socket(i) < 0)
1660 if (!i->play_stream)
1663 debug(DEBUG_LEVEL_NORMAL, __FILE__": Really draining.\n");
1665 if (!(o = pa_stream_drain(i->play_stream, stream_success_cb, i))) {
1666 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_drain(): %s\n", pa_strerror(pa_context_errno(i->context)));
1670 i->operation_success = 0;
1671 while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
1672 PLAYBACK_STREAM_CHECK_DEAD_GOTO(i, fail);
1674 pa_threaded_mainloop_wait(i->mainloop);
1677 if (!i->operation_success) {
1678 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_drain() 2: %s\n", pa_strerror(pa_context_errno(i->context)));
1687 pa_operation_unref(o);
1689 pa_threaded_mainloop_unlock(i->mainloop);
1694 static int dsp_trigger(fd_info *i) {
1695 pa_operation *o = NULL;
1698 if (!i->play_stream)
1701 pa_threaded_mainloop_lock(i->mainloop);
1703 if (dsp_empty_socket(i) < 0)
1706 debug(DEBUG_LEVEL_NORMAL, __FILE__": Triggering.\n");
1708 if (!(o = pa_stream_trigger(i->play_stream, stream_success_cb, i))) {
1709 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i->context)));
1713 i->operation_success = 0;
1714 while (!pa_operation_get_state(o) != PA_OPERATION_DONE) {
1715 PLAYBACK_STREAM_CHECK_DEAD_GOTO(i, fail);
1717 pa_threaded_mainloop_wait(i->mainloop);
1720 if (!i->operation_success) {
1721 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i->context)));
1730 pa_operation_unref(o);
1732 pa_threaded_mainloop_unlock(i->mainloop);
1737 static int dsp_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno) {
1741 case SNDCTL_DSP_SETFMT: {
1742 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_SETFMT: %i\n", *(int*) argp);
1744 pa_threaded_mainloop_lock(i->mainloop);
1746 if (*(int*) argp == AFMT_QUERY)
1747 *(int*) argp = map_format_back(i->sample_spec.format);
1749 map_format((int*) argp, &i->sample_spec);
1753 pa_threaded_mainloop_unlock(i->mainloop);
1757 case SNDCTL_DSP_SPEED: {
1762 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_SPEED: %i\n", *(int*) argp);
1764 pa_threaded_mainloop_lock(i->mainloop);
1766 ss = i->sample_spec;
1767 ss.rate = *(int*) argp;
1769 if ((valid = pa_sample_spec_valid(&ss))) {
1770 i->sample_spec = ss;
1774 debug(DEBUG_LEVEL_NORMAL, __FILE__": ss: %s\n", pa_sample_spec_snprint(t, sizeof(t), &i->sample_spec));
1776 pa_threaded_mainloop_unlock(i->mainloop);
1786 case SNDCTL_DSP_STEREO:
1787 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_STEREO: %i\n", *(int*) argp);
1789 pa_threaded_mainloop_lock(i->mainloop);
1791 i->sample_spec.channels = *(int*) argp ? 2 : 1;
1794 pa_threaded_mainloop_unlock(i->mainloop);
1797 case SNDCTL_DSP_CHANNELS: {
1801 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_CHANNELS: %i\n", *(int*) argp);
1803 pa_threaded_mainloop_lock(i->mainloop);
1805 ss = i->sample_spec;
1806 ss.channels = *(int*) argp;
1808 if ((valid = pa_sample_spec_valid(&ss))) {
1809 i->sample_spec = ss;
1813 pa_threaded_mainloop_unlock(i->mainloop);
1823 case SNDCTL_DSP_GETBLKSIZE:
1824 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_GETBLKSIZE\n");
1826 pa_threaded_mainloop_lock(i->mainloop);
1829 *(int*) argp = i->fragment_size;
1831 pa_threaded_mainloop_unlock(i->mainloop);
1835 case SNDCTL_DSP_SETFRAGMENT:
1836 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_SETFRAGMENT: 0x%8x\n", *(int*) argp);
1838 pa_threaded_mainloop_lock(i->mainloop);
1840 i->fragment_size = 1 << (*(int*) argp);
1841 i->n_fragments = (*(int*) argp) >> 16;
1845 pa_threaded_mainloop_unlock(i->mainloop);
1849 case SNDCTL_DSP_GETCAPS:
1850 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_CAPS\n");
1852 *(int*) argp = DSP_CAP_DUPLEX | DSP_CAP_MULTI;
1855 case SNDCTL_DSP_GETODELAY: {
1858 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_GETODELAY\n");
1860 pa_threaded_mainloop_lock(i->mainloop);
1867 PLAYBACK_STREAM_CHECK_DEAD_GOTO(i, exit_loop);
1869 if (pa_stream_get_latency(i->play_stream, &usec, NULL) >= 0) {
1870 *(int*) argp = pa_usec_to_bytes(usec, &i->sample_spec);
1874 if (pa_context_errno(i->context) != PA_ERR_NODATA) {
1875 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_get_latency(): %s\n", pa_strerror(pa_context_errno(i->context)));
1879 pa_threaded_mainloop_wait(i->mainloop);
1884 if (ioctl(i->thread_fd, SIOCINQ, &l) < 0)
1885 debug(DEBUG_LEVEL_NORMAL, __FILE__": SIOCINQ failed: %s\n", strerror(errno));
1889 pa_threaded_mainloop_unlock(i->mainloop);
1891 debug(DEBUG_LEVEL_NORMAL, __FILE__": ODELAY: %i\n", *(int*) argp);
1896 case SNDCTL_DSP_RESET: {
1897 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_RESET\n");
1899 pa_threaded_mainloop_lock(i->mainloop);
1902 dsp_flush_socket(i);
1905 pa_threaded_mainloop_unlock(i->mainloop);
1909 case SNDCTL_DSP_GETFMTS: {
1910 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_GETFMTS\n");
1912 *(int*) argp = AFMT_MU_LAW|AFMT_A_LAW|AFMT_U8|AFMT_S16_LE|AFMT_S16_BE;
1916 case SNDCTL_DSP_POST:
1917 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_POST\n");
1919 if (dsp_trigger(i) < 0)
1923 case SNDCTL_DSP_SYNC:
1924 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_SYNC\n");
1926 if (dsp_drain(i) < 0)
1931 case SNDCTL_DSP_GETOSPACE:
1932 case SNDCTL_DSP_GETISPACE: {
1933 audio_buf_info *bi = (audio_buf_info*) argp;
1937 if (request == SNDCTL_DSP_GETOSPACE)
1938 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_GETOSPACE\n");
1940 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_GETISPACE\n");
1942 pa_threaded_mainloop_lock(i->mainloop);
1946 if (request == SNDCTL_DSP_GETOSPACE) {
1947 if (i->play_stream) {
1948 if ((k = pa_stream_writable_size(i->play_stream)) == (size_t) -1)
1949 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_writable_size(): %s\n", pa_strerror(pa_context_errno(i->context)));
1951 k = i->fragment_size * i->n_fragments;
1953 if (ioctl(i->thread_fd, SIOCINQ, &l) < 0) {
1954 debug(DEBUG_LEVEL_NORMAL, __FILE__": SIOCINQ failed: %s\n", strerror(errno));
1958 bi->bytes = k > (size_t) l ? k - l : 0;
1960 if (i->rec_stream) {
1961 if ((k = pa_stream_readable_size(i->rec_stream)) == (size_t) -1)
1962 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_readable_size(): %s\n", pa_strerror(pa_context_errno(i->context)));
1966 if (ioctl(i->app_fd, SIOCINQ, &l) < 0) {
1967 debug(DEBUG_LEVEL_NORMAL, __FILE__": SIOCINQ failed: %s\n", strerror(errno));
1974 bi->fragsize = i->fragment_size;
1975 bi->fragstotal = i->n_fragments;
1976 bi->fragments = bi->bytes / bi->fragsize;
1978 pa_threaded_mainloop_unlock(i->mainloop);
1980 debug(DEBUG_LEVEL_NORMAL, __FILE__": fragsize=%i, fragstotal=%i, bytes=%i, fragments=%i\n", bi->fragsize, bi->fragstotal, bi->bytes, bi->fragments);
1986 debug(DEBUG_LEVEL_NORMAL, __FILE__": unknown ioctl 0x%08lx\n", request);
1999 int ioctl(int fd, unsigned long request, ...) {
2005 debug(DEBUG_LEVEL_VERBOSE, __FILE__": ioctl()\n");
2007 va_start(args, request);
2008 argp = va_arg(args, void *);
2011 if (!function_enter()) {
2013 return _ioctl(fd, request, argp);
2016 if (!(i = fd_info_find(fd))) {
2019 return _ioctl(fd, request, argp);
2022 if (i->type == FD_INFO_MIXER)
2023 r = mixer_ioctl(i, request, argp, &_errno);
2025 r = dsp_ioctl(i, request, argp, &_errno);
2040 debug(DEBUG_LEVEL_VERBOSE, __FILE__": close()\n");
2042 if (!function_enter()) {
2047 if (!(i = fd_info_find(fd))) {
2053 fd_info_remove_from_list(i);
2061 int access(const char *pathname, int mode) {
2062 debug(DEBUG_LEVEL_VERBOSE, __FILE__": access(%s)\n", pathname);
2064 if (strcmp(pathname, "/dev/dsp") != 0 &&
2065 strcmp(pathname, "/dev/adsp") != 0 &&
2066 strcmp(pathname, "/dev/sndstat") != 0 &&
2067 strcmp(pathname, "/dev/mixer") != 0) {
2069 return _access(pathname, mode);
2072 if (mode & (W_OK | X_OK)) {
2073 debug(DEBUG_LEVEL_NORMAL, __FILE__": access(%s, %x) = EACCESS\n", pathname, mode);
2078 debug(DEBUG_LEVEL_NORMAL, __FILE__": access(%s, %x) = OK\n", pathname, mode);
2083 int open64(const char *filename, int flags, ...) {
2087 debug(DEBUG_LEVEL_VERBOSE, __FILE__": open64(%s)\n", filename);
2089 va_start(args, flags);
2090 if (flags & O_CREAT)
2091 mode = va_arg(args, mode_t);
2094 if (strcmp(filename, "/dev/dsp") != 0 &&
2095 strcmp(filename, "/dev/adsp") != 0 &&
2096 strcmp(filename, "/dev/sndstat") != 0 &&
2097 strcmp(filename, "/dev/mixer") != 0) {
2099 return _open64(filename, flags, mode);
2102 return open(filename, flags, mode);
2105 FILE* fopen(const char *filename, const char *mode) {
2110 debug(DEBUG_LEVEL_VERBOSE, __FILE__": fopen(%s)\n", filename);
2112 if (strcmp(filename, "/dev/dsp") != 0 &&
2113 strcmp(filename, "/dev/adsp") != 0 &&
2114 strcmp(filename, "/dev/sndstat") != 0 &&
2115 strcmp(filename, "/dev/mixer") != 0) {
2117 return _fopen(filename, mode);
2133 if ((((mode[1] == 'b') || (mode[1] == 't')) && (mode[2] == '+')) || (mode[1] == '+'))
2136 if ((fd = open(filename, m)) < 0)
2139 if (!(f = fdopen(fd, mode))) {
2147 FILE *fopen64(const char *filename, const char *mode) {
2149 debug(DEBUG_LEVEL_VERBOSE, __FILE__": fopen64(%s)\n", filename);
2151 if (strcmp(filename, "/dev/dsp") != 0 &&
2152 strcmp(filename, "/dev/adsp") != 0 &&
2153 strcmp(filename, "/dev/sndstat") != 0 &&
2154 strcmp(filename, "/dev/mixer") != 0) {
2155 LOAD_FOPEN64_FUNC();
2156 return _fopen64(filename, mode);
2159 return fopen(filename, mode);
2162 int fclose(FILE *f) {
2165 debug(DEBUG_LEVEL_VERBOSE, __FILE__": fclose()\n");
2167 if (!function_enter()) {
2172 if (!(i = fd_info_find(fileno(f)))) {
2178 fd_info_remove_from_list(i);
2180 /* Dirty trick to avoid that the fd is not freed twice, once by us
2181 * and once by the real fclose() */