]> code.delx.au - pulseaudio/blob - src/utils/padsp.c
check for $PULSE_INTERNAL before enabling padsp
[pulseaudio] / src / utils / padsp.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2006 Lennart Poettering
7 Copyright 2006-2007 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
13
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #ifdef _FILE_OFFSET_BITS
30 #undef _FILE_OFFSET_BITS
31 #endif
32
33 #ifndef _LARGEFILE64_SOURCE
34 #define _LARGEFILE64_SOURCE 1
35 #endif
36
37 #include <sys/soundcard.h>
38 #include <sys/ioctl.h>
39 #include <pthread.h>
40 #include <unistd.h>
41 #include <sys/socket.h>
42 #include <sys/stat.h>
43 #include <dlfcn.h>
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <string.h>
47 #include <stdarg.h>
48 #include <stdio.h>
49 #include <signal.h>
50
51 #ifdef __linux__
52 #include <linux/sockios.h>
53 #endif
54
55 #include <pulse/pulseaudio.h>
56 #include <pulse/gccmacro.h>
57 #include <pulsecore/llist.h>
58
59 /* On some systems SIOCINQ isn't defined, but FIONREAD is just an alias */
60 #if !defined(SIOCINQ) && defined(FIONREAD)
61 # define SIOCINQ FIONREAD
62 #endif
63
64 /* make sure gcc doesn't redefine open and friends as macros */
65 #undef open
66 #undef open64
67
68 typedef enum {
69 FD_INFO_MIXER,
70 FD_INFO_STREAM,
71 } fd_info_type_t;
72
73 typedef struct fd_info fd_info;
74
75 struct fd_info {
76 pthread_mutex_t mutex;
77 int ref;
78 int unusable;
79
80 fd_info_type_t type;
81 int app_fd, thread_fd;
82
83 pa_sample_spec sample_spec;
84 size_t fragment_size;
85 unsigned n_fragments;
86
87 pa_threaded_mainloop *mainloop;
88 pa_context *context;
89 pa_stream *play_stream;
90 pa_stream *rec_stream;
91 int play_precork;
92 int rec_precork;
93
94 pa_io_event *io_event;
95 pa_io_event_flags_t io_flags;
96
97 void *buf;
98 size_t rec_offset;
99
100 int operation_success;
101
102 pa_cvolume sink_volume, source_volume;
103 uint32_t sink_index, source_index;
104 int volume_modify_count;
105
106 int optr_n_blocks;
107
108 PA_LLIST_FIELDS(fd_info);
109 };
110
111 static int dsp_drain(fd_info *i);
112 static void fd_info_remove_from_list(fd_info *i);
113
114 static pthread_mutex_t fd_infos_mutex = PTHREAD_MUTEX_INITIALIZER;
115 static pthread_mutex_t func_mutex = PTHREAD_MUTEX_INITIALIZER;
116
117 static PA_LLIST_HEAD(fd_info, fd_infos) = NULL;
118
119 static int (*_ioctl)(int, int, void*) = NULL;
120 static int (*_close)(int) = NULL;
121 static int (*_open)(const char *, int, mode_t) = NULL;
122 static FILE* (*_fopen)(const char *path, const char *mode) = NULL;
123 static int (*_stat)(const char *, struct stat *) = NULL;
124 #ifdef _STAT_VER
125 static int (*___xstat)(int, const char *, struct stat *) = NULL;
126 #endif
127 #ifdef HAVE_OPEN64
128 static int (*_open64)(const char *, int, mode_t) = NULL;
129 static FILE* (*_fopen64)(const char *path, const char *mode) = NULL;
130 static int (*_stat64)(const char *, struct stat64 *) = NULL;
131 #ifdef _STAT_VER
132 static int (*___xstat64)(int, const char *, struct stat64 *) = NULL;
133 #endif
134 #endif
135 static int (*_fclose)(FILE *f) = NULL;
136 static int (*_access)(const char *, int) = NULL;
137
138 /* dlsym() violates ISO C, so confide the breakage into this function to
139 * avoid warnings. */
140 typedef void (*fnptr)(void);
141 static inline fnptr dlsym_fn(void *handle, const char *symbol) {
142 return (fnptr) (long) dlsym(handle, symbol);
143 }
144
145 #define LOAD_IOCTL_FUNC() \
146 do { \
147 pthread_mutex_lock(&func_mutex); \
148 if (!_ioctl) \
149 _ioctl = (int (*)(int, int, void*)) dlsym_fn(RTLD_NEXT, "ioctl"); \
150 pthread_mutex_unlock(&func_mutex); \
151 } while(0)
152
153 #define LOAD_OPEN_FUNC() \
154 do { \
155 pthread_mutex_lock(&func_mutex); \
156 if (!_open) \
157 _open = (int (*)(const char *, int, mode_t)) dlsym_fn(RTLD_NEXT, "open"); \
158 pthread_mutex_unlock(&func_mutex); \
159 } while(0)
160
161 #define LOAD_OPEN64_FUNC() \
162 do { \
163 pthread_mutex_lock(&func_mutex); \
164 if (!_open64) \
165 _open64 = (int (*)(const char *, int, mode_t)) dlsym_fn(RTLD_NEXT, "open64"); \
166 pthread_mutex_unlock(&func_mutex); \
167 } while(0)
168
169 #define LOAD_CLOSE_FUNC() \
170 do { \
171 pthread_mutex_lock(&func_mutex); \
172 if (!_close) \
173 _close = (int (*)(int)) dlsym_fn(RTLD_NEXT, "close"); \
174 pthread_mutex_unlock(&func_mutex); \
175 } while(0)
176
177 #define LOAD_ACCESS_FUNC() \
178 do { \
179 pthread_mutex_lock(&func_mutex); \
180 if (!_access) \
181 _access = (int (*)(const char*, int)) dlsym_fn(RTLD_NEXT, "access"); \
182 pthread_mutex_unlock(&func_mutex); \
183 } while(0)
184
185 #define LOAD_STAT_FUNC() \
186 do { \
187 pthread_mutex_lock(&func_mutex); \
188 if (!_stat) \
189 _stat = (int (*)(const char *, struct stat *)) dlsym_fn(RTLD_NEXT, "stat"); \
190 pthread_mutex_unlock(&func_mutex); \
191 } while(0)
192
193 #define LOAD_STAT64_FUNC() \
194 do { \
195 pthread_mutex_lock(&func_mutex); \
196 if (!_stat64) \
197 _stat64 = (int (*)(const char *, struct stat64 *)) dlsym_fn(RTLD_NEXT, "stat64"); \
198 pthread_mutex_unlock(&func_mutex); \
199 } while(0)
200
201 #define LOAD_XSTAT_FUNC() \
202 do { \
203 pthread_mutex_lock(&func_mutex); \
204 if (!___xstat) \
205 ___xstat = (int (*)(int, const char *, struct stat *)) dlsym_fn(RTLD_NEXT, "__xstat"); \
206 pthread_mutex_unlock(&func_mutex); \
207 } while(0)
208
209 #define LOAD_XSTAT64_FUNC() \
210 do { \
211 pthread_mutex_lock(&func_mutex); \
212 if (!___xstat64) \
213 ___xstat64 = (int (*)(int, const char *, struct stat64 *)) dlsym_fn(RTLD_NEXT, "__xstat64"); \
214 pthread_mutex_unlock(&func_mutex); \
215 } while(0)
216
217 #define LOAD_FOPEN_FUNC() \
218 do { \
219 pthread_mutex_lock(&func_mutex); \
220 if (!_fopen) \
221 _fopen = (FILE* (*)(const char *, const char*)) dlsym_fn(RTLD_NEXT, "fopen"); \
222 pthread_mutex_unlock(&func_mutex); \
223 } while(0)
224
225 #define LOAD_FOPEN64_FUNC() \
226 do { \
227 pthread_mutex_lock(&func_mutex); \
228 if (!_fopen64) \
229 _fopen64 = (FILE* (*)(const char *, const char*)) dlsym_fn(RTLD_NEXT, "fopen64"); \
230 pthread_mutex_unlock(&func_mutex); \
231 } while(0)
232
233 #define LOAD_FCLOSE_FUNC() \
234 do { \
235 pthread_mutex_lock(&func_mutex); \
236 if (!_fclose) \
237 _fclose = (int (*)(FILE *)) dlsym_fn(RTLD_NEXT, "fclose"); \
238 pthread_mutex_unlock(&func_mutex); \
239 } while(0)
240
241 #define CONTEXT_CHECK_DEAD_GOTO(i, label) do { \
242 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY) { \
243 debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s\n", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
244 goto label; \
245 } \
246 } while(0)
247
248 #define PLAYBACK_STREAM_CHECK_DEAD_GOTO(i, label) do { \
249 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY || \
250 !(i)->play_stream || pa_stream_get_state((i)->play_stream) != PA_STREAM_READY) { \
251 debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s\n", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
252 goto label; \
253 } \
254 } while(0)
255
256 #define RECORD_STREAM_CHECK_DEAD_GOTO(i, label) do { \
257 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY || \
258 !(i)->rec_stream || pa_stream_get_state((i)->rec_stream) != PA_STREAM_READY) { \
259 debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s\n", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
260 goto label; \
261 } \
262 } while(0)
263
264 static void debug(int level, const char *format, ...) PA_GCC_PRINTF_ATTR(2,3);
265
266 #define DEBUG_LEVEL_ALWAYS 0
267 #define DEBUG_LEVEL_NORMAL 1
268 #define DEBUG_LEVEL_VERBOSE 2
269
270 static void debug(int level, const char *format, ...) {
271 va_list ap;
272 const char *dlevel_s;
273 int dlevel;
274
275 dlevel_s = getenv("PADSP_DEBUG");
276 if (!dlevel_s)
277 return;
278
279 dlevel = atoi(dlevel_s);
280
281 if (dlevel < level)
282 return;
283
284 va_start(ap, format);
285 vfprintf(stderr, format, ap);
286 va_end(ap);
287 }
288
289 static int padsp_disabled(void) {
290 static int *sym;
291 static int sym_resolved = 0;
292
293 /* If the current process has a symbol __padsp_disabled__ we use
294 * it to detect whether we should enable our stuff or not. A
295 * program needs to be compiled with -rdynamic for this to work!
296 * The symbol must be an int containing a three bit bitmask: bit 1
297 * -> disable /dev/dsp emulation, bit 2 -> disable /dev/sndstat
298 * emulation, bit 3 -> disable /dev/mixer emulation. Hence a value
299 * of 7 disables padsp entirely. */
300
301 pthread_mutex_lock(&func_mutex);
302 if (!sym_resolved) {
303 sym = (int*) dlsym(RTLD_DEFAULT, "__padsp_disabled__");
304 sym_resolved = 1;
305 }
306 pthread_mutex_unlock(&func_mutex);
307
308 if (!sym)
309 return 0;
310
311 return *sym;
312 }
313
314 static int dsp_cloak_enable(void) {
315 if (padsp_disabled() & 1)
316 return 0;
317
318 if (getenv("PADSP_NO_DSP") || getenv("PULSE_INTERNAL"))
319 return 0;
320
321 return 1;
322 }
323
324 static int sndstat_cloak_enable(void) {
325 if (padsp_disabled() & 2)
326 return 0;
327
328 if (getenv("PADSP_NO_SNDSTAT") || getenv("PULSE_INTERNAL"))
329 return 0;
330
331 return 1;
332 }
333
334 static int mixer_cloak_enable(void) {
335 if (padsp_disabled() & 4)
336 return 0;
337
338 if (getenv("PADSP_NO_MIXER") || getenv("PULSE_INTERNAL"))
339 return 0;
340
341 return 1;
342 }
343 static pthread_key_t recursion_key;
344
345 static void recursion_key_alloc(void) {
346 pthread_key_create(&recursion_key, NULL);
347 }
348
349 static int function_enter(void) {
350 /* Avoid recursive calls */
351 static pthread_once_t recursion_key_once = PTHREAD_ONCE_INIT;
352 pthread_once(&recursion_key_once, recursion_key_alloc);
353
354 if (pthread_getspecific(recursion_key))
355 return 0;
356
357 pthread_setspecific(recursion_key, (void*) 1);
358 return 1;
359 }
360
361 static void function_exit(void) {
362 pthread_setspecific(recursion_key, NULL);
363 }
364
365 static void fd_info_free(fd_info *i) {
366 assert(i);
367
368 debug(DEBUG_LEVEL_NORMAL, __FILE__": freeing fd info (fd=%i)\n", i->app_fd);
369
370 dsp_drain(i);
371
372 if (i->mainloop)
373 pa_threaded_mainloop_stop(i->mainloop);
374
375 if (i->play_stream) {
376 pa_stream_disconnect(i->play_stream);
377 pa_stream_unref(i->play_stream);
378 }
379
380 if (i->rec_stream) {
381 pa_stream_disconnect(i->rec_stream);
382 pa_stream_unref(i->rec_stream);
383 }
384
385 if (i->context) {
386 pa_context_disconnect(i->context);
387 pa_context_unref(i->context);
388 }
389
390 if (i->mainloop)
391 pa_threaded_mainloop_free(i->mainloop);
392
393 if (i->app_fd >= 0) {
394 LOAD_CLOSE_FUNC();
395 _close(i->app_fd);
396 }
397
398 if (i->thread_fd >= 0) {
399 LOAD_CLOSE_FUNC();
400 _close(i->thread_fd);
401 }
402
403 free(i->buf);
404
405 pthread_mutex_destroy(&i->mutex);
406 free(i);
407 }
408
409 static fd_info *fd_info_ref(fd_info *i) {
410 assert(i);
411
412 pthread_mutex_lock(&i->mutex);
413 assert(i->ref >= 1);
414 i->ref++;
415
416 debug(DEBUG_LEVEL_VERBOSE, __FILE__": ref++, now %i\n", i->ref);
417 pthread_mutex_unlock(&i->mutex);
418
419 return i;
420 }
421
422 static void fd_info_unref(fd_info *i) {
423 int r;
424 pthread_mutex_lock(&i->mutex);
425 assert(i->ref >= 1);
426 r = --i->ref;
427 debug(DEBUG_LEVEL_VERBOSE, __FILE__": ref--, now %i\n", i->ref);
428 pthread_mutex_unlock(&i->mutex);
429
430 if (r <= 0)
431 fd_info_free(i);
432 }
433
434 static void context_state_cb(pa_context *c, void *userdata) {
435 fd_info *i = userdata;
436 assert(c);
437
438 switch (pa_context_get_state(c)) {
439 case PA_CONTEXT_READY:
440 case PA_CONTEXT_TERMINATED:
441 case PA_CONTEXT_FAILED:
442 pa_threaded_mainloop_signal(i->mainloop, 0);
443 break;
444
445 case PA_CONTEXT_UNCONNECTED:
446 case PA_CONTEXT_CONNECTING:
447 case PA_CONTEXT_AUTHORIZING:
448 case PA_CONTEXT_SETTING_NAME:
449 break;
450 }
451 }
452
453 static void reset_params(fd_info *i) {
454 assert(i);
455
456 i->sample_spec.format = PA_SAMPLE_U8;
457 i->sample_spec.channels = 1;
458 i->sample_spec.rate = 8000;
459 i->fragment_size = 0;
460 i->n_fragments = 0;
461 }
462
463 static const char *client_name(char *buf, size_t n) {
464 char p[PATH_MAX];
465 const char *e;
466
467 if ((e = getenv("PADSP_CLIENT_NAME")))
468 return e;
469
470 if (pa_get_binary_name(p, sizeof(p)))
471 snprintf(buf, n, "OSS Emulation[%s]", p);
472 else
473 snprintf(buf, n, "OSS");
474
475 return buf;
476 }
477
478 static const char *stream_name(void) {
479 const char *e;
480
481 if ((e = getenv("PADSP_STREAM_NAME")))
482 return e;
483
484 return "Audio Stream";
485 }
486
487 static void atfork_prepare(void) {
488 fd_info *i;
489
490 debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_prepare() enter\n");
491
492 function_enter();
493
494 pthread_mutex_lock(&fd_infos_mutex);
495
496 for (i = fd_infos; i; i = i->next) {
497 pthread_mutex_lock(&i->mutex);
498 pa_threaded_mainloop_lock(i->mainloop);
499 }
500
501 pthread_mutex_lock(&func_mutex);
502
503
504 debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_prepare() exit\n");
505 }
506
507 static void atfork_parent(void) {
508 fd_info *i;
509
510 debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_parent() enter\n");
511
512 pthread_mutex_unlock(&func_mutex);
513
514 for (i = fd_infos; i; i = i->next) {
515 pa_threaded_mainloop_unlock(i->mainloop);
516 pthread_mutex_unlock(&i->mutex);
517 }
518
519 pthread_mutex_unlock(&fd_infos_mutex);
520
521 function_exit();
522
523 debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_parent() exit\n");
524 }
525
526 static void atfork_child(void) {
527 fd_info *i;
528
529 debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_child() enter\n");
530
531 /* We do only the bare minimum to get all fds closed */
532 pthread_mutex_init(&func_mutex, NULL);
533 pthread_mutex_init(&fd_infos_mutex, NULL);
534
535 for (i = fd_infos; i; i = i->next) {
536 pthread_mutex_init(&i->mutex, NULL);
537
538 if (i->context) {
539 pa_context_disconnect(i->context);
540 pa_context_unref(i->context);
541 i->context = NULL;
542 }
543
544 if (i->play_stream) {
545 pa_stream_unref(i->play_stream);
546 i->play_stream = NULL;
547 }
548
549 if (i->rec_stream) {
550 pa_stream_unref(i->rec_stream);
551 i->rec_stream = NULL;
552 }
553
554 if (i->app_fd >= 0) {
555 close(i->app_fd);
556 i->app_fd = -1;
557 }
558
559 if (i->thread_fd >= 0) {
560 close(i->thread_fd);
561 i->thread_fd = -1;
562 }
563
564 i->unusable = 1;
565 }
566
567 function_exit();
568
569 debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_child() exit\n");
570 }
571
572 static void install_atfork(void) {
573 pthread_atfork(atfork_prepare, atfork_parent, atfork_child);
574 }
575
576 static void stream_success_cb(pa_stream *s, int success, void *userdata) {
577 fd_info *i = userdata;
578
579 assert(s);
580 assert(i);
581
582 i->operation_success = success;
583 pa_threaded_mainloop_signal(i->mainloop, 0);
584 }
585
586 static void context_success_cb(pa_context *c, int success, void *userdata) {
587 fd_info *i = userdata;
588
589 assert(c);
590 assert(i);
591
592 i->operation_success = success;
593 pa_threaded_mainloop_signal(i->mainloop, 0);
594 }
595
596 static fd_info* fd_info_new(fd_info_type_t type, int *_errno) {
597 fd_info *i;
598 int sfds[2] = { -1, -1 };
599 char name[64];
600 static pthread_once_t install_atfork_once = PTHREAD_ONCE_INIT;
601
602 debug(DEBUG_LEVEL_NORMAL, __FILE__": fd_info_new()\n");
603
604 signal(SIGPIPE, SIG_IGN); /* Yes, ugly as hell */
605
606 pthread_once(&install_atfork_once, install_atfork);
607
608 if (!(i = malloc(sizeof(fd_info)))) {
609 *_errno = ENOMEM;
610 goto fail;
611 }
612
613 i->app_fd = i->thread_fd = -1;
614 i->type = type;
615
616 i->mainloop = NULL;
617 i->context = NULL;
618 i->play_stream = NULL;
619 i->rec_stream = NULL;
620 i->play_precork = 0;
621 i->rec_precork = 0;
622 i->io_event = NULL;
623 i->io_flags = 0;
624 pthread_mutex_init(&i->mutex, NULL);
625 i->ref = 1;
626 i->buf = NULL;
627 i->rec_offset = 0;
628 i->unusable = 0;
629 pa_cvolume_reset(&i->sink_volume, 2);
630 pa_cvolume_reset(&i->source_volume, 2);
631 i->volume_modify_count = 0;
632 i->sink_index = (uint32_t) -1;
633 i->source_index = (uint32_t) -1;
634 i->optr_n_blocks = 0;
635 PA_LLIST_INIT(fd_info, i);
636
637 reset_params(i);
638
639 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sfds) < 0) {
640 *_errno = errno;
641 debug(DEBUG_LEVEL_NORMAL, __FILE__": socket() failed: %s\n", strerror(errno));
642 goto fail;
643 }
644
645 i->app_fd = sfds[0];
646 i->thread_fd = sfds[1];
647
648 if (!(i->mainloop = pa_threaded_mainloop_new())) {
649 *_errno = EIO;
650 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_threaded_mainloop_new() failed\n");
651 goto fail;
652 }
653
654 if (!(i->context = pa_context_new(pa_threaded_mainloop_get_api(i->mainloop), client_name(name, sizeof(name))))) {
655 *_errno = EIO;
656 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_context_new() failed\n");
657 goto fail;
658 }
659
660 pa_context_set_state_callback(i->context, context_state_cb, i);
661
662 if (pa_context_connect(i->context, NULL, 0, NULL) < 0) {
663 *_errno = ECONNREFUSED;
664 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
665 goto fail;
666 }
667
668 pa_threaded_mainloop_lock(i->mainloop);
669
670 if (pa_threaded_mainloop_start(i->mainloop) < 0) {
671 *_errno = EIO;
672 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_threaded_mainloop_start() failed\n");
673 goto unlock_and_fail;
674 }
675
676 /* Wait until the context is ready */
677 pa_threaded_mainloop_wait(i->mainloop);
678
679 if (pa_context_get_state(i->context) != PA_CONTEXT_READY) {
680 *_errno = ECONNREFUSED;
681 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
682 goto unlock_and_fail;
683 }
684
685 pa_threaded_mainloop_unlock(i->mainloop);
686 return i;
687
688 unlock_and_fail:
689
690 pa_threaded_mainloop_unlock(i->mainloop);
691
692 fail:
693
694 if (i)
695 fd_info_unref(i);
696
697 return NULL;
698 }
699
700 static void fd_info_add_to_list(fd_info *i) {
701 assert(i);
702
703 pthread_mutex_lock(&fd_infos_mutex);
704 PA_LLIST_PREPEND(fd_info, fd_infos, i);
705 pthread_mutex_unlock(&fd_infos_mutex);
706
707 fd_info_ref(i);
708 }
709
710 static void fd_info_remove_from_list(fd_info *i) {
711 assert(i);
712
713 pthread_mutex_lock(&fd_infos_mutex);
714 PA_LLIST_REMOVE(fd_info, fd_infos, i);
715 pthread_mutex_unlock(&fd_infos_mutex);
716
717 fd_info_unref(i);
718 }
719
720 static fd_info* fd_info_find(int fd) {
721 fd_info *i;
722
723 pthread_mutex_lock(&fd_infos_mutex);
724
725 for (i = fd_infos; i; i = i->next)
726 if (i->app_fd == fd && !i->unusable) {
727 fd_info_ref(i);
728 break;
729 }
730
731 pthread_mutex_unlock(&fd_infos_mutex);
732
733 return i;
734 }
735
736 static void fix_metrics(fd_info *i) {
737 size_t fs;
738 char t[PA_SAMPLE_SPEC_SNPRINT_MAX];
739
740 fs = pa_frame_size(&i->sample_spec);
741
742 /* Don't fix things more than necessary */
743 if ((i->fragment_size % fs) == 0 &&
744 i->n_fragments >= 2 &&
745 i->fragment_size > 0)
746 return;
747
748 i->fragment_size = (i->fragment_size/fs)*fs;
749
750 /* Number of fragments set? */
751 if (i->n_fragments < 2) {
752 if (i->fragment_size > 0) {
753 i->n_fragments = pa_bytes_per_second(&i->sample_spec) / 2 / i->fragment_size;
754 if (i->n_fragments < 2)
755 i->n_fragments = 2;
756 } else
757 i->n_fragments = 12;
758 }
759
760 /* Fragment size set? */
761 if (i->fragment_size <= 0) {
762 i->fragment_size = pa_bytes_per_second(&i->sample_spec) / 2 / i->n_fragments;
763 if (i->fragment_size < 1024)
764 i->fragment_size = 1024;
765 }
766
767 debug(DEBUG_LEVEL_NORMAL, __FILE__": sample spec: %s\n", pa_sample_spec_snprint(t, sizeof(t), &i->sample_spec));
768 debug(DEBUG_LEVEL_NORMAL, __FILE__": fixated metrics to %i fragments, %li bytes each.\n", i->n_fragments, (long)i->fragment_size);
769 }
770
771 static void stream_request_cb(pa_stream *s, size_t length, void *userdata) {
772 fd_info *i = userdata;
773 assert(s);
774
775 if (i->io_event) {
776 pa_mainloop_api *api;
777 size_t n;
778
779 api = pa_threaded_mainloop_get_api(i->mainloop);
780
781 if (s == i->play_stream) {
782 n = pa_stream_writable_size(i->play_stream);
783 if (n == (size_t)-1) {
784 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_writable_size(): %s\n",
785 pa_strerror(pa_context_errno(i->context)));
786 }
787
788 if (n >= i->fragment_size)
789 i->io_flags |= PA_IO_EVENT_INPUT;
790 else
791 i->io_flags &= ~PA_IO_EVENT_INPUT;
792 }
793
794 if (s == i->rec_stream) {
795 n = pa_stream_readable_size(i->rec_stream);
796 if (n == (size_t)-1) {
797 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_readable_size(): %s\n",
798 pa_strerror(pa_context_errno(i->context)));
799 }
800
801 if (n >= i->fragment_size)
802 i->io_flags |= PA_IO_EVENT_OUTPUT;
803 else
804 i->io_flags &= ~PA_IO_EVENT_OUTPUT;
805 }
806
807 api->io_enable(i->io_event, i->io_flags);
808 }
809 }
810
811 static void stream_latency_update_cb(pa_stream *s, void *userdata) {
812 fd_info *i = userdata;
813 assert(s);
814
815 pa_threaded_mainloop_signal(i->mainloop, 0);
816 }
817
818 static void fd_info_shutdown(fd_info *i) {
819 assert(i);
820
821 if (i->io_event) {
822 pa_mainloop_api *api;
823 api = pa_threaded_mainloop_get_api(i->mainloop);
824 api->io_free(i->io_event);
825 i->io_event = NULL;
826 i->io_flags = 0;
827 }
828
829 if (i->thread_fd >= 0) {
830 close(i->thread_fd);
831 i->thread_fd = -1;
832 }
833 }
834
835 static int fd_info_copy_data(fd_info *i, int force) {
836 size_t n;
837
838 if (!i->play_stream && !i->rec_stream)
839 return -1;
840
841 if ((i->play_stream) && (pa_stream_get_state(i->play_stream) == PA_STREAM_READY)) {
842 n = pa_stream_writable_size(i->play_stream);
843
844 if (n == (size_t)-1) {
845 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_writable_size(): %s\n",
846 pa_strerror(pa_context_errno(i->context)));
847 return -1;
848 }
849
850 while (n >= i->fragment_size || force) {
851 ssize_t r;
852
853 if (!i->buf) {
854 if (!(i->buf = malloc(i->fragment_size))) {
855 debug(DEBUG_LEVEL_NORMAL, __FILE__": malloc() failed.\n");
856 return -1;
857 }
858 }
859
860 if ((r = read(i->thread_fd, i->buf, i->fragment_size)) <= 0) {
861
862 if (errno == EAGAIN)
863 break;
864
865 debug(DEBUG_LEVEL_NORMAL, __FILE__": read(): %s\n", r == 0 ? "EOF" : strerror(errno));
866 return -1;
867 }
868
869 if (pa_stream_write(i->play_stream, i->buf, r, free, 0, PA_SEEK_RELATIVE) < 0) {
870 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_write(): %s\n", pa_strerror(pa_context_errno(i->context)));
871 return -1;
872 }
873
874 i->buf = NULL;
875
876 assert(n >= (size_t) r);
877 n -= r;
878 }
879
880 if (n >= i->fragment_size)
881 i->io_flags |= PA_IO_EVENT_INPUT;
882 else
883 i->io_flags &= ~PA_IO_EVENT_INPUT;
884 }
885
886 if ((i->rec_stream) && (pa_stream_get_state(i->rec_stream) == PA_STREAM_READY)) {
887 n = pa_stream_readable_size(i->rec_stream);
888
889 if (n == (size_t)-1) {
890 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_readable_size(): %s\n",
891 pa_strerror(pa_context_errno(i->context)));
892 return -1;
893 }
894
895 while (n >= i->fragment_size || force) {
896 ssize_t r;
897 const void *data;
898 const char *buf;
899 size_t len;
900
901 if (pa_stream_peek(i->rec_stream, &data, &len) < 0) {
902 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_peek(): %s\n", pa_strerror(pa_context_errno(i->context)));
903 return -1;
904 }
905
906 if (!data)
907 break;
908
909 buf = (const char*)data + i->rec_offset;
910
911 if ((r = write(i->thread_fd, buf, len - i->rec_offset)) <= 0) {
912
913 if (errno == EAGAIN)
914 break;
915
916 debug(DEBUG_LEVEL_NORMAL, __FILE__": write(): %s\n", strerror(errno));
917 return -1;
918 }
919
920 assert((size_t)r <= len - i->rec_offset);
921 i->rec_offset += r;
922
923 if (i->rec_offset == len) {
924 if (pa_stream_drop(i->rec_stream) < 0) {
925 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_drop(): %s\n", pa_strerror(pa_context_errno(i->context)));
926 return -1;
927 }
928 i->rec_offset = 0;
929 }
930
931 assert(n >= (size_t) r);
932 n -= r;
933 }
934
935 if (n >= i->fragment_size)
936 i->io_flags |= PA_IO_EVENT_OUTPUT;
937 else
938 i->io_flags &= ~PA_IO_EVENT_OUTPUT;
939 }
940
941 if (i->io_event) {
942 pa_mainloop_api *api;
943
944 api = pa_threaded_mainloop_get_api(i->mainloop);
945 api->io_enable(i->io_event, i->io_flags);
946 }
947
948 return 0;
949 }
950
951 static void stream_state_cb(pa_stream *s, void * userdata) {
952 fd_info *i = userdata;
953 assert(s);
954
955 switch (pa_stream_get_state(s)) {
956
957 case PA_STREAM_READY:
958 debug(DEBUG_LEVEL_NORMAL, __FILE__": stream established.\n");
959 break;
960
961 case PA_STREAM_FAILED:
962 if (s == i->play_stream) {
963 debug(DEBUG_LEVEL_NORMAL,
964 __FILE__": pa_stream_connect_playback() failed: %s\n",
965 pa_strerror(pa_context_errno(i->context)));
966 pa_stream_unref(i->play_stream);
967 i->play_stream = NULL;
968 } else if (s == i->rec_stream) {
969 debug(DEBUG_LEVEL_NORMAL,
970 __FILE__": pa_stream_connect_record() failed: %s\n",
971 pa_strerror(pa_context_errno(i->context)));
972 pa_stream_unref(i->rec_stream);
973 i->rec_stream = NULL;
974 }
975 fd_info_shutdown(i);
976 break;
977
978 case PA_STREAM_TERMINATED:
979 case PA_STREAM_UNCONNECTED:
980 case PA_STREAM_CREATING:
981 break;
982 }
983 }
984
985 static int create_playback_stream(fd_info *i) {
986 pa_buffer_attr attr;
987 int n, flags;
988
989 assert(i);
990
991 fix_metrics(i);
992
993 if (!(i->play_stream = pa_stream_new(i->context, stream_name(), &i->sample_spec, NULL))) {
994 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
995 goto fail;
996 }
997
998 pa_stream_set_state_callback(i->play_stream, stream_state_cb, i);
999 pa_stream_set_write_callback(i->play_stream, stream_request_cb, i);
1000 pa_stream_set_latency_update_callback(i->play_stream, stream_latency_update_cb, i);
1001
1002 memset(&attr, 0, sizeof(attr));
1003 attr.maxlength = i->fragment_size * (i->n_fragments+1);
1004 attr.tlength = i->fragment_size * i->n_fragments;
1005 attr.prebuf = i->fragment_size;
1006 attr.minreq = i->fragment_size;
1007
1008 flags = PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE;
1009 if (i->play_precork) {
1010 flags |= PA_STREAM_START_CORKED;
1011 debug(DEBUG_LEVEL_NORMAL, __FILE__": creating stream corked\n");
1012 }
1013 if (pa_stream_connect_playback(i->play_stream, NULL, &attr, flags, NULL, NULL) < 0) {
1014 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
1015 goto fail;
1016 }
1017
1018 n = i->fragment_size;
1019 setsockopt(i->app_fd, SOL_SOCKET, SO_SNDBUF, &n, sizeof(n));
1020 n = i->fragment_size;
1021 setsockopt(i->thread_fd, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n));
1022
1023 return 0;
1024
1025 fail:
1026 return -1;
1027 }
1028
1029 static int create_record_stream(fd_info *i) {
1030 pa_buffer_attr attr;
1031 int n, flags;
1032
1033 assert(i);
1034
1035 fix_metrics(i);
1036
1037 if (!(i->rec_stream = pa_stream_new(i->context, stream_name(), &i->sample_spec, NULL))) {
1038 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
1039 goto fail;
1040 }
1041
1042 pa_stream_set_state_callback(i->rec_stream, stream_state_cb, i);
1043 pa_stream_set_read_callback(i->rec_stream, stream_request_cb, i);
1044 pa_stream_set_latency_update_callback(i->rec_stream, stream_latency_update_cb, i);
1045
1046 memset(&attr, 0, sizeof(attr));
1047 attr.maxlength = i->fragment_size * (i->n_fragments+1);
1048 attr.fragsize = i->fragment_size;
1049
1050 flags = PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE;
1051 if (i->rec_precork) {
1052 flags |= PA_STREAM_START_CORKED;
1053 debug(DEBUG_LEVEL_NORMAL, __FILE__": creating stream corked\n");
1054 }
1055 if (pa_stream_connect_record(i->rec_stream, NULL, &attr, flags) < 0) {
1056 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_connect_record() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
1057 goto fail;
1058 }
1059
1060 n = i->fragment_size;
1061 setsockopt(i->app_fd, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n));
1062 n = i->fragment_size;
1063 setsockopt(i->thread_fd, SOL_SOCKET, SO_SNDBUF, &n, sizeof(n));
1064
1065 return 0;
1066
1067 fail:
1068 return -1;
1069 }
1070
1071 static void free_streams(fd_info *i) {
1072 assert(i);
1073
1074 if (i->play_stream) {
1075 pa_stream_disconnect(i->play_stream);
1076 pa_stream_unref(i->play_stream);
1077 i->play_stream = NULL;
1078 i->io_flags |= PA_IO_EVENT_INPUT;
1079 }
1080
1081 if (i->rec_stream) {
1082 pa_stream_disconnect(i->rec_stream);
1083 pa_stream_unref(i->rec_stream);
1084 i->rec_stream = NULL;
1085 i->io_flags |= PA_IO_EVENT_OUTPUT;
1086 }
1087
1088 if (i->io_event) {
1089 pa_mainloop_api *api;
1090
1091 api = pa_threaded_mainloop_get_api(i->mainloop);
1092 api->io_enable(i->io_event, i->io_flags);
1093 }
1094 }
1095
1096 static void io_event_cb(pa_mainloop_api *api, pa_io_event *e, int fd, pa_io_event_flags_t flags, void *userdata) {
1097 fd_info *i = userdata;
1098
1099 pa_threaded_mainloop_signal(i->mainloop, 0);
1100
1101 if (flags & PA_IO_EVENT_INPUT) {
1102
1103 if (!i->play_stream) {
1104 if (create_playback_stream(i) < 0)
1105 goto fail;
1106 } else {
1107 if (fd_info_copy_data(i, 0) < 0)
1108 goto fail;
1109 }
1110
1111 } else if (flags & PA_IO_EVENT_OUTPUT) {
1112
1113 if (!i->rec_stream) {
1114 if (create_record_stream(i) < 0)
1115 goto fail;
1116 } else {
1117 if (fd_info_copy_data(i, 0) < 0)
1118 goto fail;
1119 }
1120
1121 } else if (flags & (PA_IO_EVENT_HANGUP|PA_IO_EVENT_ERROR))
1122 goto fail;
1123
1124 return;
1125
1126 fail:
1127 /* We can't do anything better than removing the event source */
1128 fd_info_shutdown(i);
1129 }
1130
1131 static int dsp_open(int flags, int *_errno) {
1132 fd_info *i;
1133 pa_mainloop_api *api;
1134 int ret;
1135 int f;
1136
1137 debug(DEBUG_LEVEL_NORMAL, __FILE__": dsp_open()\n");
1138
1139 if (!(i = fd_info_new(FD_INFO_STREAM, _errno)))
1140 return -1;
1141
1142 if ((flags & O_NONBLOCK) == O_NONBLOCK) {
1143 if ((f = fcntl(i->app_fd, F_GETFL)) >= 0)
1144 fcntl(i->app_fd, F_SETFL, f|O_NONBLOCK);
1145 }
1146 if ((f = fcntl(i->thread_fd, F_GETFL)) >= 0)
1147 fcntl(i->thread_fd, F_SETFL, f|O_NONBLOCK);
1148
1149 fcntl(i->app_fd, F_SETFD, FD_CLOEXEC);
1150 fcntl(i->thread_fd, F_SETFD, FD_CLOEXEC);
1151
1152 pa_threaded_mainloop_lock(i->mainloop);
1153 api = pa_threaded_mainloop_get_api(i->mainloop);
1154
1155 switch (flags & O_ACCMODE) {
1156 case O_RDONLY:
1157 i->io_flags = PA_IO_EVENT_OUTPUT;
1158 shutdown(i->thread_fd, SHUT_RD);
1159 shutdown(i->app_fd, SHUT_WR);
1160 break;
1161 case O_WRONLY:
1162 i->io_flags = PA_IO_EVENT_INPUT;
1163 shutdown(i->thread_fd, SHUT_WR);
1164 shutdown(i->app_fd, SHUT_RD);
1165 break;
1166 case O_RDWR:
1167 i->io_flags = PA_IO_EVENT_INPUT | PA_IO_EVENT_OUTPUT;
1168 break;
1169 default:
1170 return -1;
1171 }
1172
1173 if (!(i->io_event = api->io_new(api, i->thread_fd, i->io_flags, io_event_cb, i)))
1174 goto fail;
1175
1176 pa_threaded_mainloop_unlock(i->mainloop);
1177
1178 debug(DEBUG_LEVEL_NORMAL, __FILE__": dsp_open() succeeded, fd=%i\n", i->app_fd);
1179
1180 fd_info_add_to_list(i);
1181 ret = i->app_fd;
1182 fd_info_unref(i);
1183
1184 return ret;
1185
1186 fail:
1187 pa_threaded_mainloop_unlock(i->mainloop);
1188
1189 if (i)
1190 fd_info_unref(i);
1191
1192 *_errno = EIO;
1193
1194 debug(DEBUG_LEVEL_NORMAL, __FILE__": dsp_open() failed\n");
1195
1196 return -1;
1197 }
1198
1199 static void sink_info_cb(pa_context *context, const pa_sink_info *si, int eol, void *userdata) {
1200 fd_info *i = userdata;
1201
1202 if (!si && eol < 0) {
1203 i->operation_success = 0;
1204 pa_threaded_mainloop_signal(i->mainloop, 0);
1205 return;
1206 }
1207
1208 if (eol)
1209 return;
1210
1211 if (!pa_cvolume_equal(&i->sink_volume, &si->volume))
1212 i->volume_modify_count++;
1213
1214 i->sink_volume = si->volume;
1215 i->sink_index = si->index;
1216
1217 i->operation_success = 1;
1218 pa_threaded_mainloop_signal(i->mainloop, 0);
1219 }
1220
1221 static void source_info_cb(pa_context *context, const pa_source_info *si, int eol, void *userdata) {
1222 fd_info *i = userdata;
1223
1224 if (!si && eol < 0) {
1225 i->operation_success = 0;
1226 pa_threaded_mainloop_signal(i->mainloop, 0);
1227 return;
1228 }
1229
1230 if (eol)
1231 return;
1232
1233 if (!pa_cvolume_equal(&i->source_volume, &si->volume))
1234 i->volume_modify_count++;
1235
1236 i->source_volume = si->volume;
1237 i->source_index = si->index;
1238
1239 i->operation_success = 1;
1240 pa_threaded_mainloop_signal(i->mainloop, 0);
1241 }
1242
1243 static void subscribe_cb(pa_context *context, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
1244 fd_info *i = userdata;
1245 pa_operation *o = NULL;
1246
1247 if (i->sink_index != idx)
1248 return;
1249
1250 if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) != PA_SUBSCRIPTION_EVENT_CHANGE)
1251 return;
1252
1253 if (!(o = pa_context_get_sink_info_by_index(i->context, i->sink_index, sink_info_cb, i))) {
1254 debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to get sink info: %s", pa_strerror(pa_context_errno(i->context)));
1255 return;
1256 }
1257
1258 pa_operation_unref(o);
1259 }
1260
1261 static int mixer_open(int flags, int *_errno) {
1262 fd_info *i;
1263 pa_operation *o = NULL;
1264 int ret;
1265
1266 debug(DEBUG_LEVEL_NORMAL, __FILE__": mixer_open()\n");
1267
1268 if (!(i = fd_info_new(FD_INFO_MIXER, _errno)))
1269 return -1;
1270
1271 pa_threaded_mainloop_lock(i->mainloop);
1272
1273 pa_context_set_subscribe_callback(i->context, subscribe_cb, i);
1274
1275 if (!(o = pa_context_subscribe(i->context, PA_SUBSCRIPTION_MASK_SINK | PA_SUBSCRIPTION_MASK_SOURCE, context_success_cb, i))) {
1276 debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to subscribe to events: %s", pa_strerror(pa_context_errno(i->context)));
1277 *_errno = EIO;
1278 goto fail;
1279 }
1280
1281 i->operation_success = 0;
1282 while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
1283 pa_threaded_mainloop_wait(i->mainloop);
1284 CONTEXT_CHECK_DEAD_GOTO(i, fail);
1285 }
1286
1287 pa_operation_unref(o);
1288 o = NULL;
1289
1290 if (!i->operation_success) {
1291 debug(DEBUG_LEVEL_NORMAL, __FILE__":Failed to subscribe to events: %s", pa_strerror(pa_context_errno(i->context)));
1292 *_errno = EIO;
1293 goto fail;
1294 }
1295
1296 /* Get sink info */
1297
1298 if (!(o = pa_context_get_sink_info_by_name(i->context, NULL, sink_info_cb, i))) {
1299 debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to get sink info: %s", pa_strerror(pa_context_errno(i->context)));
1300 *_errno = EIO;
1301 goto fail;
1302 }
1303
1304 i->operation_success = 0;
1305 while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
1306 pa_threaded_mainloop_wait(i->mainloop);
1307 CONTEXT_CHECK_DEAD_GOTO(i, fail);
1308 }
1309
1310 pa_operation_unref(o);
1311 o = NULL;
1312
1313 if (!i->operation_success) {
1314 debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to get sink info: %s", pa_strerror(pa_context_errno(i->context)));
1315 *_errno = EIO;
1316 goto fail;
1317 }
1318
1319 /* Get source info */
1320
1321 if (!(o = pa_context_get_source_info_by_name(i->context, NULL, source_info_cb, i))) {
1322 debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to get source info: %s", pa_strerror(pa_context_errno(i->context)));
1323 *_errno = EIO;
1324 goto fail;
1325 }
1326
1327 i->operation_success = 0;
1328 while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
1329 pa_threaded_mainloop_wait(i->mainloop);
1330 CONTEXT_CHECK_DEAD_GOTO(i, fail);
1331 }
1332
1333 pa_operation_unref(o);
1334 o = NULL;
1335
1336 if (!i->operation_success) {
1337 debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to get source info: %s", pa_strerror(pa_context_errno(i->context)));
1338 *_errno = EIO;
1339 goto fail;
1340 }
1341
1342 pa_threaded_mainloop_unlock(i->mainloop);
1343
1344 debug(DEBUG_LEVEL_NORMAL, __FILE__": mixer_open() succeeded, fd=%i\n", i->app_fd);
1345
1346 fd_info_add_to_list(i);
1347 ret = i->app_fd;
1348 fd_info_unref(i);
1349
1350 return ret;
1351
1352 fail:
1353 if (o)
1354 pa_operation_unref(o);
1355
1356 pa_threaded_mainloop_unlock(i->mainloop);
1357
1358 if (i)
1359 fd_info_unref(i);
1360
1361 *_errno = EIO;
1362
1363 debug(DEBUG_LEVEL_NORMAL, __FILE__": mixer_open() failed\n");
1364
1365 return -1;
1366 }
1367
1368 static int sndstat_open(int flags, int *_errno) {
1369 static const char sndstat[] =
1370 "Sound Driver:3.8.1a-980706 (PulseAudio Virtual OSS)\n"
1371 "Kernel: POSIX\n"
1372 "Config options: 0\n"
1373 "\n"
1374 "Installed drivers:\n"
1375 "Type 255: PulseAudio Virtual OSS\n"
1376 "\n"
1377 "Card config:\n"
1378 "PulseAudio Virtual OSS\n"
1379 "\n"
1380 "Audio devices:\n"
1381 "0: PulseAudio Virtual OSS\n"
1382 "\n"
1383 "Synth devices: NOT ENABLED IN CONFIG\n"
1384 "\n"
1385 "Midi devices:\n"
1386 "\n"
1387 "Timers:\n"
1388 "\n"
1389 "Mixers:\n"
1390 "0: PulseAudio Virtual OSS\n";
1391
1392 char fn[] = "/tmp/padsp-sndstat-XXXXXX";
1393 mode_t u;
1394 int fd = -1;
1395 int e;
1396
1397 debug(DEBUG_LEVEL_NORMAL, __FILE__": sndstat_open()\n");
1398
1399 if (flags != O_RDONLY
1400 #ifdef O_LARGEFILE
1401 && flags != (O_RDONLY|O_LARGEFILE)
1402 #endif
1403 ) {
1404 *_errno = EACCES;
1405 debug(DEBUG_LEVEL_NORMAL, __FILE__": bad access!\n");
1406 goto fail;
1407 }
1408
1409 u = umask(0077);
1410 fd = mkstemp(fn);
1411 e = errno;
1412 umask(u);
1413
1414 if (fd < 0) {
1415 *_errno = e;
1416 debug(DEBUG_LEVEL_NORMAL, __FILE__": mkstemp() failed: %s\n", strerror(errno));
1417 goto fail;
1418 }
1419
1420 unlink(fn);
1421
1422 if (write(fd, sndstat, sizeof(sndstat) -1) != sizeof(sndstat)-1) {
1423 *_errno = errno;
1424 debug(DEBUG_LEVEL_NORMAL, __FILE__": write() failed: %s\n", strerror(errno));
1425 goto fail;
1426 }
1427
1428 if (lseek(fd, SEEK_SET, 0) < 0) {
1429 *_errno = errno;
1430 debug(DEBUG_LEVEL_NORMAL, __FILE__": lseek() failed: %s\n", strerror(errno));
1431 goto fail;
1432 }
1433
1434 return fd;
1435
1436 fail:
1437 if (fd >= 0)
1438 close(fd);
1439 return -1;
1440 }
1441
1442 static int real_open(const char *filename, int flags, mode_t mode) {
1443 int r, _errno = 0;
1444
1445 debug(DEBUG_LEVEL_VERBOSE, __FILE__": open(%s)\n", filename?filename:"NULL");
1446
1447 if (!function_enter()) {
1448 LOAD_OPEN_FUNC();
1449 return _open(filename, flags, mode);
1450 }
1451
1452 if (filename && dsp_cloak_enable() && (strcmp(filename, "/dev/dsp") == 0 || strcmp(filename, "/dev/adsp") == 0))
1453 r = dsp_open(flags, &_errno);
1454 else if (filename && mixer_cloak_enable() && strcmp(filename, "/dev/mixer") == 0)
1455 r = mixer_open(flags, &_errno);
1456 else if (filename && sndstat_cloak_enable() && strcmp(filename, "/dev/sndstat") == 0)
1457 r = sndstat_open(flags, &_errno);
1458 else {
1459 function_exit();
1460 LOAD_OPEN_FUNC();
1461 return _open(filename, flags, mode);
1462 }
1463
1464 function_exit();
1465
1466 if (_errno)
1467 errno = _errno;
1468
1469 return r;
1470 }
1471
1472 int open(const char *filename, int flags, ...) {
1473 va_list args;
1474 mode_t mode = 0;
1475
1476 if (flags & O_CREAT) {
1477 va_start(args, flags);
1478 if (sizeof(mode_t) < sizeof(int))
1479 mode = va_arg(args, int);
1480 else
1481 mode = va_arg(args, mode_t);
1482 va_end(args);
1483 }
1484
1485 return real_open(filename, flags, mode);
1486 }
1487
1488 static int mixer_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno) {
1489 int ret = -1;
1490
1491 switch (request) {
1492 case SOUND_MIXER_READ_DEVMASK :
1493 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_DEVMASK\n");
1494
1495 *(int*) argp = SOUND_MASK_PCM | SOUND_MASK_IGAIN;
1496 break;
1497
1498 case SOUND_MIXER_READ_RECMASK :
1499 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_RECMASK\n");
1500
1501 *(int*) argp = SOUND_MASK_IGAIN;
1502 break;
1503
1504 case SOUND_MIXER_READ_STEREODEVS:
1505 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_STEREODEVS\n");
1506
1507 pa_threaded_mainloop_lock(i->mainloop);
1508 *(int*) argp = 0;
1509 if (i->sink_volume.channels > 1)
1510 *(int*) argp |= SOUND_MASK_PCM;
1511 if (i->source_volume.channels > 1)
1512 *(int*) argp |= SOUND_MASK_IGAIN;
1513 pa_threaded_mainloop_unlock(i->mainloop);
1514
1515 break;
1516
1517 case SOUND_MIXER_READ_RECSRC:
1518 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_RECSRC\n");
1519
1520 *(int*) argp = SOUND_MASK_IGAIN;
1521 break;
1522
1523 case SOUND_MIXER_WRITE_RECSRC:
1524 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_WRITE_RECSRC\n");
1525 break;
1526
1527 case SOUND_MIXER_READ_CAPS:
1528 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_CAPS\n");
1529
1530 *(int*) argp = 0;
1531 break;
1532
1533 case SOUND_MIXER_READ_PCM:
1534 case SOUND_MIXER_READ_IGAIN: {
1535 pa_cvolume *v;
1536
1537 if (request == SOUND_MIXER_READ_PCM)
1538 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_PCM\n");
1539 else
1540 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_IGAIN\n");
1541
1542 pa_threaded_mainloop_lock(i->mainloop);
1543
1544 if (request == SOUND_MIXER_READ_PCM)
1545 v = &i->sink_volume;
1546 else
1547 v = &i->source_volume;
1548
1549 *(int*) argp =
1550 ((v->values[0]*100/PA_VOLUME_NORM)) |
1551 ((v->values[v->channels > 1 ? 1 : 0]*100/PA_VOLUME_NORM) << 8);
1552
1553 pa_threaded_mainloop_unlock(i->mainloop);
1554
1555 break;
1556 }
1557
1558 case SOUND_MIXER_WRITE_PCM:
1559 case SOUND_MIXER_WRITE_IGAIN: {
1560 pa_cvolume v, *pv;
1561
1562 if (request == SOUND_MIXER_WRITE_PCM)
1563 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_WRITE_PCM\n");
1564 else
1565 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_WRITE_IGAIN\n");
1566
1567 pa_threaded_mainloop_lock(i->mainloop);
1568
1569 if (request == SOUND_MIXER_WRITE_PCM) {
1570 v = i->sink_volume;
1571 pv = &i->sink_volume;
1572 } else {
1573 v = i->source_volume;
1574 pv = &i->source_volume;
1575 }
1576
1577 pv->values[0] = ((*(int*) argp & 0xFF)*PA_VOLUME_NORM)/100;
1578 pv->values[1] = ((*(int*) argp >> 8)*PA_VOLUME_NORM)/100;
1579
1580 if (!pa_cvolume_equal(pv, &v)) {
1581 pa_operation *o;
1582
1583 if (request == SOUND_MIXER_WRITE_PCM)
1584 o = pa_context_set_sink_volume_by_index(i->context, i->sink_index, pv, context_success_cb, i);
1585 else
1586 o = pa_context_set_source_volume_by_index(i->context, i->source_index, pv, context_success_cb, i);
1587
1588 if (!o)
1589 debug(DEBUG_LEVEL_NORMAL, __FILE__":Failed set volume: %s", pa_strerror(pa_context_errno(i->context)));
1590 else {
1591
1592 i->operation_success = 0;
1593 while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
1594 CONTEXT_CHECK_DEAD_GOTO(i, exit_loop);
1595
1596 pa_threaded_mainloop_wait(i->mainloop);
1597 }
1598 exit_loop:
1599
1600 if (!i->operation_success)
1601 debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to set volume: %s\n", pa_strerror(pa_context_errno(i->context)));
1602
1603 pa_operation_unref(o);
1604 }
1605
1606 /* We don't wait for completion here */
1607 i->volume_modify_count++;
1608 }
1609
1610 pa_threaded_mainloop_unlock(i->mainloop);
1611
1612 break;
1613 }
1614
1615 case SOUND_MIXER_INFO: {
1616 mixer_info *mi = argp;
1617
1618 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_INFO\n");
1619
1620 memset(mi, 0, sizeof(mixer_info));
1621 strncpy(mi->id, "PULSEAUDIO", sizeof(mi->id));
1622 strncpy(mi->name, "PulseAudio Virtual OSS", sizeof(mi->name));
1623 pa_threaded_mainloop_lock(i->mainloop);
1624 mi->modify_counter = i->volume_modify_count;
1625 pa_threaded_mainloop_unlock(i->mainloop);
1626 break;
1627 }
1628
1629 default:
1630 debug(DEBUG_LEVEL_NORMAL, __FILE__": unknown ioctl 0x%08lx\n", request);
1631
1632 *_errno = EINVAL;
1633 goto fail;
1634 }
1635
1636 ret = 0;
1637
1638 fail:
1639
1640 return ret;
1641 }
1642
1643 static int map_format(int *fmt, pa_sample_spec *ss) {
1644
1645 switch (*fmt) {
1646 case AFMT_MU_LAW:
1647 ss->format = PA_SAMPLE_ULAW;
1648 break;
1649
1650 case AFMT_A_LAW:
1651 ss->format = PA_SAMPLE_ALAW;
1652 break;
1653
1654 case AFMT_S8:
1655 *fmt = AFMT_U8;
1656 /* fall through */
1657 case AFMT_U8:
1658 ss->format = PA_SAMPLE_U8;
1659 break;
1660
1661 case AFMT_U16_BE:
1662 *fmt = AFMT_S16_BE;
1663 /* fall through */
1664 case AFMT_S16_BE:
1665 ss->format = PA_SAMPLE_S16BE;
1666 break;
1667
1668 case AFMT_U16_LE:
1669 *fmt = AFMT_S16_LE;
1670 /* fall through */
1671 case AFMT_S16_LE:
1672 ss->format = PA_SAMPLE_S16LE;
1673 break;
1674
1675 default:
1676 ss->format = PA_SAMPLE_S16NE;
1677 *fmt = AFMT_S16_NE;
1678 break;
1679 }
1680
1681 return 0;
1682 }
1683
1684 static int map_format_back(pa_sample_format_t format) {
1685 switch (format) {
1686 case PA_SAMPLE_S16LE: return AFMT_S16_LE;
1687 case PA_SAMPLE_S16BE: return AFMT_S16_BE;
1688 case PA_SAMPLE_ULAW: return AFMT_MU_LAW;
1689 case PA_SAMPLE_ALAW: return AFMT_A_LAW;
1690 case PA_SAMPLE_U8: return AFMT_U8;
1691 default:
1692 abort();
1693 }
1694 }
1695
1696 static int dsp_flush_fd(int fd) {
1697 #ifdef SIOCINQ
1698 int l;
1699
1700 if (ioctl(fd, SIOCINQ, &l) < 0) {
1701 debug(DEBUG_LEVEL_NORMAL, __FILE__": SIOCINQ: %s\n", strerror(errno));
1702 return -1;
1703 }
1704
1705 while (l > 0) {
1706 char buf[1024];
1707 size_t k;
1708
1709 k = (size_t) l > sizeof(buf) ? sizeof(buf) : (size_t) l;
1710 if (read(fd, buf, k) < 0)
1711 debug(DEBUG_LEVEL_NORMAL, __FILE__": read(): %s\n", strerror(errno));
1712 l -= k;
1713 }
1714
1715 return 0;
1716 #else
1717 # warning "Your platform does not support SIOCINQ, something might not work as intended."
1718 return 0;
1719 #endif
1720 }
1721
1722 static int dsp_flush_socket(fd_info *i) {
1723 int res = 0;
1724
1725 if ((i->thread_fd < 0) && (i->app_fd < 0))
1726 return -1;
1727
1728 if (i->thread_fd >= 0)
1729 res = dsp_flush_fd(i->thread_fd);
1730
1731 if (res < 0)
1732 return res;
1733
1734 if (i->app_fd >= 0)
1735 res = dsp_flush_fd(i->app_fd);
1736
1737 if (res < 0)
1738 return res;
1739
1740 return 0;
1741 }
1742
1743 static int dsp_empty_socket(fd_info *i) {
1744 #ifdef SIOCINQ
1745 int ret = -1;
1746
1747 /* Empty the socket */
1748 for (;;) {
1749 int l;
1750
1751 if (i->thread_fd < 0)
1752 break;
1753
1754 if (ioctl(i->thread_fd, SIOCINQ, &l) < 0) {
1755 debug(DEBUG_LEVEL_NORMAL, __FILE__": SIOCINQ: %s\n", strerror(errno));
1756 break;
1757 }
1758
1759 if (!l) {
1760 ret = 0;
1761 break;
1762 }
1763
1764 pa_threaded_mainloop_wait(i->mainloop);
1765 }
1766
1767 return ret;
1768 #else
1769 # warning "Your platform does not support SIOCINQ, something might not work as intended."
1770 return 0;
1771 #endif
1772 }
1773
1774 static int dsp_drain(fd_info *i) {
1775 pa_operation *o = NULL;
1776 int r = -1;
1777
1778 if (!i->mainloop)
1779 return 0;
1780
1781 debug(DEBUG_LEVEL_NORMAL, __FILE__": Draining.\n");
1782
1783 pa_threaded_mainloop_lock(i->mainloop);
1784
1785 if (dsp_empty_socket(i) < 0)
1786 goto fail;
1787
1788 if (!i->play_stream)
1789 goto fail;
1790
1791 debug(DEBUG_LEVEL_NORMAL, __FILE__": Really draining.\n");
1792
1793 if (!(o = pa_stream_drain(i->play_stream, stream_success_cb, i))) {
1794 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_drain(): %s\n", pa_strerror(pa_context_errno(i->context)));
1795 goto fail;
1796 }
1797
1798 i->operation_success = 0;
1799 while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
1800 PLAYBACK_STREAM_CHECK_DEAD_GOTO(i, fail);
1801
1802 pa_threaded_mainloop_wait(i->mainloop);
1803 }
1804
1805 if (!i->operation_success) {
1806 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_drain() 2: %s\n", pa_strerror(pa_context_errno(i->context)));
1807 goto fail;
1808 }
1809
1810 r = 0;
1811
1812 fail:
1813
1814 if (o)
1815 pa_operation_unref(o);
1816
1817 pa_threaded_mainloop_unlock(i->mainloop);
1818
1819 return 0;
1820 }
1821
1822 static int dsp_trigger(fd_info *i) {
1823 pa_operation *o = NULL;
1824 int r = -1;
1825
1826 if (!i->play_stream)
1827 return 0;
1828
1829 pa_threaded_mainloop_lock(i->mainloop);
1830
1831 if (dsp_empty_socket(i) < 0)
1832 goto fail;
1833
1834 debug(DEBUG_LEVEL_NORMAL, __FILE__": Triggering.\n");
1835
1836 if (!(o = pa_stream_trigger(i->play_stream, stream_success_cb, i))) {
1837 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i->context)));
1838 goto fail;
1839 }
1840
1841 i->operation_success = 0;
1842 while (!pa_operation_get_state(o) != PA_OPERATION_DONE) {
1843 PLAYBACK_STREAM_CHECK_DEAD_GOTO(i, fail);
1844
1845 pa_threaded_mainloop_wait(i->mainloop);
1846 }
1847
1848 if (!i->operation_success) {
1849 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i->context)));
1850 goto fail;
1851 }
1852
1853 r = 0;
1854
1855 fail:
1856
1857 if (o)
1858 pa_operation_unref(o);
1859
1860 pa_threaded_mainloop_unlock(i->mainloop);
1861
1862 return 0;
1863 }
1864
1865 static int dsp_cork(fd_info *i, pa_stream *s, int b) {
1866 pa_operation *o = NULL;
1867 int r = -1;
1868
1869 pa_threaded_mainloop_lock(i->mainloop);
1870
1871 if (!(o = pa_stream_cork(s, b, stream_success_cb, i))) {
1872 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_cork(): %s\n", pa_strerror(pa_context_errno(i->context)));
1873 goto fail;
1874 }
1875
1876 i->operation_success = 0;
1877 while (!pa_operation_get_state(o) != PA_OPERATION_DONE) {
1878 if (s == i->play_stream)
1879 PLAYBACK_STREAM_CHECK_DEAD_GOTO(i, fail);
1880 else if (s == i->rec_stream)
1881 RECORD_STREAM_CHECK_DEAD_GOTO(i, fail);
1882
1883 pa_threaded_mainloop_wait(i->mainloop);
1884 }
1885
1886 if (!i->operation_success) {
1887 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_cork(): %s\n", pa_strerror(pa_context_errno(i->context)));
1888 goto fail;
1889 }
1890
1891 r = 0;
1892
1893 fail:
1894
1895 if (o)
1896 pa_operation_unref(o);
1897
1898 pa_threaded_mainloop_unlock(i->mainloop);
1899
1900 return 0;
1901 }
1902
1903 static int dsp_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno) {
1904 int ret = -1;
1905
1906 if (i->thread_fd == -1) {
1907 /*
1908 * We've encountered some fatal error and are just waiting
1909 * for a close.
1910 */
1911 debug(DEBUG_LEVEL_NORMAL, __FILE__": got ioctl 0x%08lx in fatal error state\n", request);
1912 *_errno = EIO;
1913 return -1;
1914 }
1915
1916 switch (request) {
1917 case SNDCTL_DSP_SETFMT: {
1918 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_SETFMT: %i\n", *(int*) argp);
1919
1920 pa_threaded_mainloop_lock(i->mainloop);
1921
1922 if (*(int*) argp == AFMT_QUERY)
1923 *(int*) argp = map_format_back(i->sample_spec.format);
1924 else {
1925 map_format((int*) argp, &i->sample_spec);
1926 free_streams(i);
1927 }
1928
1929 pa_threaded_mainloop_unlock(i->mainloop);
1930 break;
1931 }
1932
1933 case SNDCTL_DSP_SPEED: {
1934 pa_sample_spec ss;
1935 int valid;
1936 char t[256];
1937
1938 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_SPEED: %i\n", *(int*) argp);
1939
1940 pa_threaded_mainloop_lock(i->mainloop);
1941
1942 ss = i->sample_spec;
1943 ss.rate = *(int*) argp;
1944
1945 if ((valid = pa_sample_spec_valid(&ss))) {
1946 i->sample_spec = ss;
1947 free_streams(i);
1948 }
1949
1950 debug(DEBUG_LEVEL_NORMAL, __FILE__": ss: %s\n", pa_sample_spec_snprint(t, sizeof(t), &i->sample_spec));
1951
1952 pa_threaded_mainloop_unlock(i->mainloop);
1953
1954 if (!valid) {
1955 *_errno = EINVAL;
1956 goto fail;
1957 }
1958
1959 break;
1960 }
1961
1962 case SNDCTL_DSP_STEREO:
1963 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_STEREO: %i\n", *(int*) argp);
1964
1965 pa_threaded_mainloop_lock(i->mainloop);
1966
1967 i->sample_spec.channels = *(int*) argp ? 2 : 1;
1968 free_streams(i);
1969
1970 pa_threaded_mainloop_unlock(i->mainloop);
1971 return 0;
1972
1973 case SNDCTL_DSP_CHANNELS: {
1974 pa_sample_spec ss;
1975 int valid;
1976
1977 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_CHANNELS: %i\n", *(int*) argp);
1978
1979 pa_threaded_mainloop_lock(i->mainloop);
1980
1981 ss = i->sample_spec;
1982 ss.channels = *(int*) argp;
1983
1984 if ((valid = pa_sample_spec_valid(&ss))) {
1985 i->sample_spec = ss;
1986 free_streams(i);
1987 }
1988
1989 pa_threaded_mainloop_unlock(i->mainloop);
1990
1991 if (!valid) {
1992 *_errno = EINVAL;
1993 goto fail;
1994 }
1995
1996 break;
1997 }
1998
1999 case SNDCTL_DSP_GETBLKSIZE:
2000 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_GETBLKSIZE\n");
2001
2002 pa_threaded_mainloop_lock(i->mainloop);
2003
2004 fix_metrics(i);
2005 *(int*) argp = i->fragment_size;
2006
2007 pa_threaded_mainloop_unlock(i->mainloop);
2008
2009 break;
2010
2011 case SNDCTL_DSP_SETFRAGMENT:
2012 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_SETFRAGMENT: 0x%08x\n", *(int*) argp);
2013
2014 pa_threaded_mainloop_lock(i->mainloop);
2015
2016 i->fragment_size = 1 << ((*(int*) argp) & 31);
2017 i->n_fragments = (*(int*) argp) >> 16;
2018
2019 /* 0x7FFF means that we can set whatever we like */
2020 if (i->n_fragments == 0x7FFF)
2021 i->n_fragments = 12;
2022
2023 free_streams(i);
2024
2025 pa_threaded_mainloop_unlock(i->mainloop);
2026
2027 break;
2028
2029 case SNDCTL_DSP_GETCAPS:
2030 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_CAPS\n");
2031
2032 *(int*) argp = DSP_CAP_DUPLEX | DSP_CAP_TRIGGER
2033 #ifdef DSP_CAP_MULTI
2034 | DSP_CAP_MULTI
2035 #endif
2036 ;
2037 break;
2038
2039 case SNDCTL_DSP_GETODELAY: {
2040 int l;
2041
2042 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_GETODELAY\n");
2043
2044 pa_threaded_mainloop_lock(i->mainloop);
2045
2046 *(int*) argp = 0;
2047
2048 for (;;) {
2049 pa_usec_t usec;
2050
2051 PLAYBACK_STREAM_CHECK_DEAD_GOTO(i, exit_loop);
2052
2053 if (pa_stream_get_latency(i->play_stream, &usec, NULL) >= 0) {
2054 *(int*) argp = pa_usec_to_bytes(usec, &i->sample_spec);
2055 break;
2056 }
2057
2058 if (pa_context_errno(i->context) != PA_ERR_NODATA) {
2059 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_get_latency(): %s\n", pa_strerror(pa_context_errno(i->context)));
2060 break;
2061 }
2062
2063 pa_threaded_mainloop_wait(i->mainloop);
2064 }
2065
2066 exit_loop:
2067
2068 #ifdef SIOCINQ
2069 if (ioctl(i->thread_fd, SIOCINQ, &l) < 0)
2070 debug(DEBUG_LEVEL_NORMAL, __FILE__": SIOCINQ failed: %s\n", strerror(errno));
2071 else
2072 *(int*) argp += l;
2073 #else
2074 # warning "Your platform does not support SIOCINQ, something might not work as intended."
2075 #endif
2076
2077 pa_threaded_mainloop_unlock(i->mainloop);
2078
2079 debug(DEBUG_LEVEL_NORMAL, __FILE__": ODELAY: %i\n", *(int*) argp);
2080
2081 break;
2082 }
2083
2084 case SNDCTL_DSP_RESET: {
2085 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_RESET\n");
2086
2087 pa_threaded_mainloop_lock(i->mainloop);
2088
2089 free_streams(i);
2090 dsp_flush_socket(i);
2091
2092 i->optr_n_blocks = 0;
2093
2094 pa_threaded_mainloop_unlock(i->mainloop);
2095 break;
2096 }
2097
2098 case SNDCTL_DSP_GETFMTS: {
2099 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_GETFMTS\n");
2100
2101 *(int*) argp = AFMT_MU_LAW|AFMT_A_LAW|AFMT_U8|AFMT_S16_LE|AFMT_S16_BE;
2102 break;
2103 }
2104
2105 case SNDCTL_DSP_POST:
2106 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_POST\n");
2107
2108 if (dsp_trigger(i) < 0)
2109 *_errno = EIO;
2110 break;
2111
2112 case SNDCTL_DSP_GETTRIGGER:
2113 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_GETTRIGGER\n");
2114
2115 *(int*) argp = 0;
2116 if (!i->play_precork)
2117 *(int*) argp |= PCM_ENABLE_OUTPUT;
2118 if (!i->rec_precork)
2119 *(int*) argp |= PCM_ENABLE_INPUT;
2120
2121 break;
2122
2123 case SNDCTL_DSP_SETTRIGGER:
2124 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_SETTRIGGER: 0x%08x\n", *(int*) argp);
2125
2126 if (!i->io_event) {
2127 *_errno = EIO;
2128 break;
2129 }
2130
2131 i->play_precork = !((*(int*) argp) & PCM_ENABLE_OUTPUT);
2132
2133 if (i->play_stream) {
2134 if (dsp_cork(i, i->play_stream, !((*(int*) argp) & PCM_ENABLE_OUTPUT)) < 0)
2135 *_errno = EIO;
2136 if (dsp_trigger(i) < 0)
2137 *_errno = EIO;
2138 }
2139
2140 i->rec_precork = !((*(int*) argp) & PCM_ENABLE_INPUT);
2141
2142 if (i->rec_stream) {
2143 if (dsp_cork(i, i->rec_stream, !((*(int*) argp) & PCM_ENABLE_INPUT)) < 0)
2144 *_errno = EIO;
2145 }
2146
2147 break;
2148
2149 case SNDCTL_DSP_SYNC:
2150 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_SYNC\n");
2151
2152 if (dsp_drain(i) < 0)
2153 *_errno = EIO;
2154
2155 break;
2156
2157 case SNDCTL_DSP_GETOSPACE:
2158 case SNDCTL_DSP_GETISPACE: {
2159 audio_buf_info *bi = (audio_buf_info*) argp;
2160 int l = 0;
2161 size_t k = 0;
2162
2163 if (request == SNDCTL_DSP_GETOSPACE)
2164 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_GETOSPACE\n");
2165 else
2166 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_GETISPACE\n");
2167
2168 pa_threaded_mainloop_lock(i->mainloop);
2169
2170 fix_metrics(i);
2171
2172 if (request == SNDCTL_DSP_GETOSPACE) {
2173 if (i->play_stream) {
2174 if ((k = pa_stream_writable_size(i->play_stream)) == (size_t) -1)
2175 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_writable_size(): %s\n", pa_strerror(pa_context_errno(i->context)));
2176 } else
2177 k = i->fragment_size * i->n_fragments;
2178
2179 #ifdef SIOCINQ
2180 if (ioctl(i->thread_fd, SIOCINQ, &l) < 0) {
2181 debug(DEBUG_LEVEL_NORMAL, __FILE__": SIOCINQ failed: %s\n", strerror(errno));
2182 l = 0;
2183 }
2184 #else
2185 # warning "Your platform does not dsp_flush_fd, something might not work as intended."
2186 #endif
2187
2188 bi->bytes = k > (size_t) l ? k - l : 0;
2189 } else {
2190 if (i->rec_stream) {
2191 if ((k = pa_stream_readable_size(i->rec_stream)) == (size_t) -1)
2192 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_readable_size(): %s\n", pa_strerror(pa_context_errno(i->context)));
2193 } else
2194 k = 0;
2195
2196 #ifdef SIOCINQ
2197 if (ioctl(i->app_fd, SIOCINQ, &l) < 0) {
2198 debug(DEBUG_LEVEL_NORMAL, __FILE__": SIOCINQ failed: %s\n", strerror(errno));
2199 l = 0;
2200 }
2201 #else
2202 # warning "Your platform does not dsp_flush_fd, something might not work as intended."
2203 #endif
2204 bi->bytes = k + l;
2205 }
2206
2207 bi->fragsize = i->fragment_size;
2208 bi->fragstotal = i->n_fragments;
2209 bi->fragments = bi->bytes / bi->fragsize;
2210
2211 pa_threaded_mainloop_unlock(i->mainloop);
2212
2213 debug(DEBUG_LEVEL_NORMAL, __FILE__": fragsize=%i, fragstotal=%i, bytes=%i, fragments=%i\n", bi->fragsize, bi->fragstotal, bi->bytes, bi->fragments);
2214
2215 break;
2216 }
2217
2218 case SOUND_PCM_READ_RATE:
2219 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_PCM_READ_RATE\n");
2220
2221 pa_threaded_mainloop_lock(i->mainloop);
2222 *(int*) argp = i->sample_spec.rate;
2223 pa_threaded_mainloop_unlock(i->mainloop);
2224 break;
2225
2226 case SOUND_PCM_READ_CHANNELS:
2227 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_PCM_READ_CHANNELS\n");
2228
2229 pa_threaded_mainloop_lock(i->mainloop);
2230 *(int*) argp = i->sample_spec.channels;
2231 pa_threaded_mainloop_unlock(i->mainloop);
2232 break;
2233
2234 case SOUND_PCM_READ_BITS:
2235 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_PCM_READ_BITS\n");
2236
2237 pa_threaded_mainloop_lock(i->mainloop);
2238 *(int*) argp = pa_sample_size(&i->sample_spec)*8;
2239 pa_threaded_mainloop_unlock(i->mainloop);
2240 break;
2241
2242 case SNDCTL_DSP_GETOPTR: {
2243 count_info *info;
2244
2245 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_GETOPTR\n");
2246
2247 info = (count_info*) argp;
2248 memset(info, 0, sizeof(*info));
2249
2250 pa_threaded_mainloop_lock(i->mainloop);
2251
2252 for (;;) {
2253 pa_usec_t usec;
2254
2255 PLAYBACK_STREAM_CHECK_DEAD_GOTO(i, exit_loop);
2256
2257 if (pa_stream_get_time(i->play_stream, &usec) >= 0) {
2258 size_t k = pa_usec_to_bytes(usec, &i->sample_spec);
2259 int m;
2260
2261 info->bytes = (int) k;
2262 m = k / i->fragment_size;
2263 info->blocks = m - i->optr_n_blocks;
2264 i->optr_n_blocks = m;
2265
2266 break;
2267 }
2268
2269 if (pa_context_errno(i->context) != PA_ERR_NODATA) {
2270 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_get_latency(): %s\n", pa_strerror(pa_context_errno(i->context)));
2271 break;
2272 }
2273
2274 pa_threaded_mainloop_wait(i->mainloop);
2275 }
2276
2277 pa_threaded_mainloop_unlock(i->mainloop);
2278
2279 debug(DEBUG_LEVEL_NORMAL, __FILE__": GETOPTR bytes=%i, blocks=%i, ptr=%i\n", info->bytes, info->blocks, info->ptr);
2280
2281 break;
2282 }
2283
2284 case SNDCTL_DSP_GETIPTR:
2285 debug(DEBUG_LEVEL_NORMAL, __FILE__": invalid ioctl SNDCTL_DSP_GETIPTR\n");
2286 goto inval;
2287
2288 case SNDCTL_DSP_SETDUPLEX:
2289 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_SETDUPLEX\n");
2290 /* this is a no-op */
2291 break;
2292
2293 default:
2294 /* Mixer ioctls are valid on /dev/dsp aswell */
2295 return mixer_ioctl(i, request, argp, _errno);
2296
2297 inval:
2298 *_errno = EINVAL;
2299 goto fail;
2300 }
2301
2302 ret = 0;
2303
2304 fail:
2305
2306 return ret;
2307 }
2308
2309 #ifdef sun
2310 int ioctl(int fd, int request, ...) {
2311 #else
2312 int ioctl(int fd, unsigned long request, ...) {
2313 #endif
2314 fd_info *i;
2315 va_list args;
2316 void *argp;
2317 int r, _errno = 0;
2318
2319 debug(DEBUG_LEVEL_VERBOSE, __FILE__": ioctl()\n");
2320
2321 va_start(args, request);
2322 argp = va_arg(args, void *);
2323 va_end(args);
2324
2325 if (!function_enter()) {
2326 LOAD_IOCTL_FUNC();
2327 return _ioctl(fd, request, argp);
2328 }
2329
2330 if (!(i = fd_info_find(fd))) {
2331 function_exit();
2332 LOAD_IOCTL_FUNC();
2333 return _ioctl(fd, request, argp);
2334 }
2335
2336 if (i->type == FD_INFO_MIXER)
2337 r = mixer_ioctl(i, request, argp, &_errno);
2338 else
2339 r = dsp_ioctl(i, request, argp, &_errno);
2340
2341 fd_info_unref(i);
2342
2343 if (_errno)
2344 errno = _errno;
2345
2346 function_exit();
2347
2348 return r;
2349 }
2350
2351 int close(int fd) {
2352 fd_info *i;
2353
2354 debug(DEBUG_LEVEL_VERBOSE, __FILE__": close()\n");
2355
2356 if (!function_enter()) {
2357 LOAD_CLOSE_FUNC();
2358 return _close(fd);
2359 }
2360
2361 if (!(i = fd_info_find(fd))) {
2362 function_exit();
2363 LOAD_CLOSE_FUNC();
2364 return _close(fd);
2365 }
2366
2367 fd_info_remove_from_list(i);
2368 fd_info_unref(i);
2369
2370 function_exit();
2371
2372 return 0;
2373 }
2374
2375 int access(const char *pathname, int mode) {
2376
2377 debug(DEBUG_LEVEL_VERBOSE, __FILE__": access(%s)\n", pathname?pathname:"NULL");
2378
2379 if (!pathname ||
2380 ( strcmp(pathname, "/dev/dsp") != 0 &&
2381 strcmp(pathname, "/dev/adsp") != 0 &&
2382 strcmp(pathname, "/dev/sndstat") != 0 &&
2383 strcmp(pathname, "/dev/mixer") != 0 )) {
2384 LOAD_ACCESS_FUNC();
2385 return _access(pathname, mode);
2386 }
2387
2388 if (mode & (W_OK | X_OK)) {
2389 debug(DEBUG_LEVEL_NORMAL, __FILE__": access(%s, %x) = EACCESS\n", pathname, mode);
2390 errno = EACCES;
2391 return -1;
2392 }
2393
2394 debug(DEBUG_LEVEL_NORMAL, __FILE__": access(%s, %x) = OK\n", pathname, mode);
2395
2396 return 0;
2397 }
2398
2399 int stat(const char *pathname, struct stat *buf) {
2400 #ifdef HAVE_OPEN64
2401 struct stat64 parent;
2402 #else
2403 struct stat parent;
2404 #endif
2405 int ret;
2406
2407 if (!pathname ||
2408 !buf ||
2409 ( strcmp(pathname, "/dev/dsp") != 0 &&
2410 strcmp(pathname, "/dev/adsp") != 0 &&
2411 strcmp(pathname, "/dev/sndstat") != 0 &&
2412 strcmp(pathname, "/dev/mixer") != 0 )) {
2413 debug(DEBUG_LEVEL_VERBOSE, __FILE__": stat(%s)\n", pathname?pathname:"NULL");
2414 LOAD_STAT_FUNC();
2415 return _stat(pathname, buf);
2416 }
2417
2418 debug(DEBUG_LEVEL_NORMAL, __FILE__": stat(%s)\n", pathname);
2419
2420 #ifdef _STAT_VER
2421 #ifdef HAVE_OPEN64
2422 ret = __xstat64(_STAT_VER, "/dev", &parent);
2423 #else
2424 ret = __xstat(_STAT_VER, "/dev", &parent);
2425 #endif
2426 #else
2427 #ifdef HAVE_OPEN64
2428 ret = stat64("/dev", &parent);
2429 #else
2430 ret = stat("/dev", &parent);
2431 #endif
2432 #endif
2433
2434 if (ret) {
2435 debug(DEBUG_LEVEL_NORMAL, __FILE__": unable to stat \"/dev\"\n");
2436 return -1;
2437 }
2438
2439 buf->st_dev = parent.st_dev;
2440 buf->st_ino = 0xDEADBEEF; /* FIXME: Can we do this in a safe way? */
2441 buf->st_mode = S_IFCHR | S_IRUSR | S_IWUSR;
2442 buf->st_nlink = 1;
2443 buf->st_uid = getuid();
2444 buf->st_gid = getgid();
2445 buf->st_rdev = 0x0E03; /* FIXME: Linux specific */
2446 buf->st_size = 0;
2447 buf->st_atime = 1181557705;
2448 buf->st_mtime = 1181557705;
2449 buf->st_ctime = 1181557705;
2450 buf->st_blksize = 1;
2451 buf->st_blocks = 0;
2452
2453 return 0;
2454 }
2455
2456 #ifdef HAVE_OPEN64
2457
2458 int stat64(const char *pathname, struct stat64 *buf) {
2459 struct stat oldbuf;
2460 int ret;
2461
2462 debug(DEBUG_LEVEL_VERBOSE, __FILE__": stat64(%s)\n", pathname?pathname:"NULL");
2463
2464 if (!pathname ||
2465 !buf ||
2466 ( strcmp(pathname, "/dev/dsp") != 0 &&
2467 strcmp(pathname, "/dev/adsp") != 0 &&
2468 strcmp(pathname, "/dev/sndstat") != 0 &&
2469 strcmp(pathname, "/dev/mixer") != 0 )) {
2470 LOAD_STAT64_FUNC();
2471 return _stat64(pathname, buf);
2472 }
2473
2474 ret = stat(pathname, &oldbuf);
2475 if (ret)
2476 return ret;
2477
2478 buf->st_dev = oldbuf.st_dev;
2479 buf->st_ino = oldbuf.st_ino;
2480 buf->st_mode = oldbuf.st_mode;
2481 buf->st_nlink = oldbuf.st_nlink;
2482 buf->st_uid = oldbuf.st_uid;
2483 buf->st_gid = oldbuf.st_gid;
2484 buf->st_rdev = oldbuf.st_rdev;
2485 buf->st_size = oldbuf.st_size;
2486 buf->st_atime = oldbuf.st_atime;
2487 buf->st_mtime = oldbuf.st_mtime;
2488 buf->st_ctime = oldbuf.st_ctime;
2489 buf->st_blksize = oldbuf.st_blksize;
2490 buf->st_blocks = oldbuf.st_blocks;
2491
2492 return 0;
2493 }
2494
2495 int open64(const char *filename, int flags, ...) {
2496 va_list args;
2497 mode_t mode = 0;
2498
2499 debug(DEBUG_LEVEL_VERBOSE, __FILE__": open64(%s)\n", filename?filename:"NULL");
2500
2501 if (flags & O_CREAT) {
2502 va_start(args, flags);
2503 if (sizeof(mode_t) < sizeof(int))
2504 mode = va_arg(args, int);
2505 else
2506 mode = va_arg(args, mode_t);
2507 va_end(args);
2508 }
2509
2510 if (!filename ||
2511 ( strcmp(filename, "/dev/dsp") != 0 &&
2512 strcmp(filename, "/dev/adsp") != 0 &&
2513 strcmp(filename, "/dev/sndstat") != 0 &&
2514 strcmp(filename, "/dev/mixer") != 0 )) {
2515 LOAD_OPEN64_FUNC();
2516 return _open64(filename, flags, mode);
2517 }
2518
2519 return real_open(filename, flags, mode);
2520 }
2521
2522 #endif
2523
2524 #ifdef _STAT_VER
2525
2526 int __xstat(int ver, const char *pathname, struct stat *buf) {
2527 debug(DEBUG_LEVEL_VERBOSE, __FILE__": __xstat(%s)\n", pathname?pathname:"NULL");
2528
2529 if (!pathname ||
2530 !buf ||
2531 ( strcmp(pathname, "/dev/dsp") != 0 &&
2532 strcmp(pathname, "/dev/adsp") != 0 &&
2533 strcmp(pathname, "/dev/sndstat") != 0 &&
2534 strcmp(pathname, "/dev/mixer") != 0 )) {
2535 LOAD_XSTAT_FUNC();
2536 return ___xstat(ver, pathname, buf);
2537 }
2538
2539 if (ver != _STAT_VER) {
2540 errno = EINVAL;
2541 return -1;
2542 }
2543
2544 return stat(pathname, buf);
2545 }
2546
2547 #ifdef HAVE_OPEN64
2548
2549 int __xstat64(int ver, const char *pathname, struct stat64 *buf) {
2550 debug(DEBUG_LEVEL_VERBOSE, __FILE__": __xstat64(%s)\n", pathname?pathname:"NULL");
2551
2552 if (!pathname ||
2553 !buf ||
2554 ( strcmp(pathname, "/dev/dsp") != 0 &&
2555 strcmp(pathname, "/dev/adsp") != 0 &&
2556 strcmp(pathname, "/dev/sndstat") != 0 &&
2557 strcmp(pathname, "/dev/mixer") != 0 )) {
2558 LOAD_XSTAT64_FUNC();
2559 return ___xstat64(ver, pathname, buf);
2560 }
2561
2562 if (ver != _STAT_VER) {
2563 errno = EINVAL;
2564 return -1;
2565 }
2566
2567 return stat64(pathname, buf);
2568 }
2569
2570 #endif
2571
2572 #endif
2573
2574 FILE* fopen(const char *filename, const char *mode) {
2575 FILE *f = NULL;
2576 int fd;
2577 mode_t m;
2578
2579 debug(DEBUG_LEVEL_VERBOSE, __FILE__": fopen(%s)\n", filename?filename:"NULL");
2580
2581 if (!filename ||
2582 !mode ||
2583 ( strcmp(filename, "/dev/dsp") != 0 &&
2584 strcmp(filename, "/dev/adsp") != 0 &&
2585 strcmp(filename, "/dev/sndstat") != 0 &&
2586 strcmp(filename, "/dev/mixer") != 0 )) {
2587 LOAD_FOPEN_FUNC();
2588 return _fopen(filename, mode);
2589 }
2590
2591 switch (mode[0]) {
2592 case 'r':
2593 m = O_RDONLY;
2594 break;
2595 case 'w':
2596 case 'a':
2597 m = O_WRONLY;
2598 break;
2599 default:
2600 errno = EINVAL;
2601 return NULL;
2602 }
2603
2604 if ((((mode[1] == 'b') || (mode[1] == 't')) && (mode[2] == '+')) || (mode[1] == '+'))
2605 m = O_RDWR;
2606
2607 if ((fd = real_open(filename, m, 0)) < 0)
2608 return NULL;
2609
2610 if (!(f = fdopen(fd, mode))) {
2611 close(fd);
2612 return NULL;
2613 }
2614
2615 return f;
2616 }
2617
2618 #ifdef HAVE_OPEN64
2619
2620 FILE *fopen64(const char *filename, const char *mode) {
2621
2622 debug(DEBUG_LEVEL_VERBOSE, __FILE__": fopen64(%s)\n", filename?filename:"NULL");
2623
2624 if (!filename ||
2625 !mode ||
2626 ( strcmp(filename, "/dev/dsp") != 0 &&
2627 strcmp(filename, "/dev/adsp") != 0 &&
2628 strcmp(filename, "/dev/sndstat") != 0 &&
2629 strcmp(filename, "/dev/mixer") != 0 )) {
2630 LOAD_FOPEN64_FUNC();
2631 return _fopen64(filename, mode);
2632 }
2633
2634 return fopen(filename, mode);
2635 }
2636
2637 #endif
2638
2639 int fclose(FILE *f) {
2640 fd_info *i;
2641
2642 debug(DEBUG_LEVEL_VERBOSE, __FILE__": fclose()\n");
2643
2644 if (!function_enter()) {
2645 LOAD_FCLOSE_FUNC();
2646 return _fclose(f);
2647 }
2648
2649 if (!(i = fd_info_find(fileno(f)))) {
2650 function_exit();
2651 LOAD_FCLOSE_FUNC();
2652 return _fclose(f);
2653 }
2654
2655 fd_info_remove_from_list(i);
2656
2657 /* Dirty trick to avoid that the fd is not freed twice, once by us
2658 * and once by the real fclose() */
2659 i->app_fd = -1;
2660
2661 fd_info_unref(i);
2662
2663 function_exit();
2664
2665 LOAD_FCLOSE_FUNC();
2666 return _fclose(f);
2667 }