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