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