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