]> code.delx.au - pulseaudio/blob - src/utils/padsp.c
Fix the fix_metrics() function so that we don't get a tiny buffer by default.
[pulseaudio] / src / utils / padsp.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
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 <dlfcn.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <string.h>
43 #include <stdarg.h>
44 #include <stdio.h>
45 #include <signal.h>
46
47 #include <linux/sockios.h>
48
49 #include <polyp/polypaudio.h>
50 #include <polypcore/llist.h>
51 #include <polypcore/gccmacro.h>
52
53 typedef enum {
54 FD_INFO_MIXER,
55 FD_INFO_PLAYBACK
56 } fd_info_type_t;
57
58 typedef struct fd_info fd_info;
59
60 struct fd_info {
61 pthread_mutex_t mutex;
62 int ref;
63 int unusable;
64
65 fd_info_type_t type;
66 int app_fd, thread_fd;
67
68 pa_sample_spec sample_spec;
69 size_t fragment_size;
70 unsigned n_fragments;
71
72 pa_threaded_mainloop *mainloop;
73 pa_context *context;
74 pa_stream *stream;
75
76 pa_io_event *io_event;
77
78 void *buf;
79
80 int operation_success;
81
82 pa_cvolume volume;
83 uint32_t sink_index;
84 int volume_modify_count;
85
86 PA_LLIST_FIELDS(fd_info);
87 };
88
89 static int dsp_drain(fd_info *i);
90 static void fd_info_remove_from_list(fd_info *i);
91
92 static pthread_mutex_t fd_infos_mutex = PTHREAD_MUTEX_INITIALIZER;
93 static pthread_mutex_t func_mutex = PTHREAD_MUTEX_INITIALIZER;
94
95 static PA_LLIST_HEAD(fd_info, fd_infos) = NULL;
96
97 static int (*_ioctl)(int, int, void*) = NULL;
98 static int (*_close)(int) = NULL;
99 static int (*_open)(const char *, int, mode_t) = NULL;
100 static FILE* (*_fopen)(const char *path, const char *mode) = NULL;
101 static int (*_open64)(const char *, int, mode_t) = NULL;
102 static FILE* (*_fopen64)(const char *path, const char *mode) = NULL;
103 static int (*_fclose)(FILE *f) = NULL;
104
105 /* dlsym() violates ISO C, so confide the breakage into this function to
106 * avoid warnings. */
107 typedef void (*fnptr)(void);
108 static inline fnptr dlsym_fn(void *handle, const char *symbol) {
109 return (fnptr) (long) dlsym(handle, symbol);
110 }
111
112 #define LOAD_IOCTL_FUNC() \
113 do { \
114 pthread_mutex_lock(&func_mutex); \
115 if (!_ioctl) \
116 _ioctl = (int (*)(int, int, void*)) dlsym_fn(RTLD_NEXT, "ioctl"); \
117 pthread_mutex_unlock(&func_mutex); \
118 } while(0)
119
120 #define LOAD_OPEN_FUNC() \
121 do { \
122 pthread_mutex_lock(&func_mutex); \
123 if (!_open) \
124 _open = (int (*)(const char *, int, mode_t)) dlsym_fn(RTLD_NEXT, "open"); \
125 pthread_mutex_unlock(&func_mutex); \
126 } while(0)
127
128 #define LOAD_OPEN64_FUNC() \
129 do { \
130 pthread_mutex_lock(&func_mutex); \
131 if (!_open64) \
132 _open64 = (int (*)(const char *, int, mode_t)) dlsym_fn(RTLD_NEXT, "open64"); \
133 pthread_mutex_unlock(&func_mutex); \
134 } while(0)
135
136 #define LOAD_CLOSE_FUNC() \
137 do { \
138 pthread_mutex_lock(&func_mutex); \
139 if (!_close) \
140 _close = (int (*)(int)) dlsym_fn(RTLD_NEXT, "close"); \
141 pthread_mutex_unlock(&func_mutex); \
142 } while(0)
143
144 #define LOAD_FOPEN_FUNC() \
145 do { \
146 pthread_mutex_lock(&func_mutex); \
147 if (!_fopen) \
148 _fopen = (FILE* (*)(const char *, const char*)) dlsym_fn(RTLD_NEXT, "fopen"); \
149 pthread_mutex_unlock(&func_mutex); \
150 } while(0)
151
152 #define LOAD_FOPEN64_FUNC() \
153 do { \
154 pthread_mutex_lock(&func_mutex); \
155 if (!_fopen64) \
156 _fopen64 = (FILE* (*)(const char *, const char*)) dlsym_fn(RTLD_NEXT, "fopen64"); \
157 pthread_mutex_unlock(&func_mutex); \
158 } while(0)
159
160 #define LOAD_FCLOSE_FUNC() \
161 do { \
162 pthread_mutex_lock(&func_mutex); \
163 if (!_fclose) \
164 _fclose = (int (*)(FILE *)) dlsym_fn(RTLD_NEXT, "fclose"); \
165 pthread_mutex_unlock(&func_mutex); \
166 } while(0)
167
168 #define CONTEXT_CHECK_DEAD_GOTO(i, label) do { \
169 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY) { \
170 debug(__FILE__": Not connected: %s", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
171 goto label; \
172 } \
173 } while(0);
174
175 #define STREAM_CHECK_DEAD_GOTO(i, label) do { \
176 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY || \
177 !(i)->stream || pa_stream_get_state((i)->stream) != PA_STREAM_READY) { \
178 debug(__FILE__": Not connected: %s", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
179 goto label; \
180 } \
181 } while(0);
182
183 static void debug(const char *format, ...) PA_GCC_PRINTF_ATTR(1,2);
184
185 static void debug(const char *format, ...) {
186 va_list ap;
187 if (getenv("PADSP_DEBUG")) {
188 va_start(ap, format);
189 vfprintf(stderr, format, ap);
190 va_end(ap);
191 }
192 }
193
194 static int padsp_disabled(void) {
195 static int *sym;
196 static int sym_resolved = 0;
197
198 /* If the current process has a symbol __padsp_disabled__ we use
199 * it to detect whether we should enable our stuff or not. A
200 * program needs to be compiled with -rdynamic for this to work!
201 * The symbol must be an int containing a three bit bitmask: bit 1
202 * -> disable /dev/dsp emulation, bit 2 -> disable /dev/sndstat
203 * emulation, bit 3 -> disable /dev/mixer emulation. Hence a value
204 * of 7 disables padsp entirely. */
205
206 pthread_mutex_lock(&func_mutex);
207 if (!sym_resolved) {
208 sym = (int*) dlsym(RTLD_DEFAULT, "__padsp_disabled__");
209 sym_resolved = 1;
210
211 }
212 pthread_mutex_unlock(&func_mutex);
213
214 if (!sym)
215 return 0;
216
217 return *sym;
218 }
219
220 static int dsp_cloak_enable(void) {
221 if (padsp_disabled() & 1)
222 return 0;
223
224 if (getenv("PADSP_NO_DSP"))
225 return 0;
226
227 return 1;
228 }
229
230 static int sndstat_cloak_enable(void) {
231 if (padsp_disabled() & 2)
232 return 0;
233
234 if (getenv("PADSP_NO_SNDSTAT"))
235 return 0;
236
237 return 1;
238 }
239
240 static int mixer_cloak_enable(void) {
241 if (padsp_disabled() & 4)
242 return 0;
243
244 if (getenv("PADSP_NO_MIXER"))
245 return 0;
246
247 return 1;
248 }
249 static pthread_key_t recursion_key;
250
251 static void recursion_key_alloc(void) {
252 pthread_key_create(&recursion_key, NULL);
253 }
254
255 static int function_enter(void) {
256 /* Avoid recursive calls */
257 static pthread_once_t recursion_key_once = PTHREAD_ONCE_INIT;
258 pthread_once(&recursion_key_once, recursion_key_alloc);
259
260 if (pthread_getspecific(recursion_key))
261 return 0;
262
263 pthread_setspecific(recursion_key, (void*) 1);
264 return 1;
265 }
266
267 static void function_exit(void) {
268 pthread_setspecific(recursion_key, NULL);
269 }
270
271 static void fd_info_free(fd_info *i) {
272 assert(i);
273
274 debug(__FILE__": freeing fd info (fd=%i)\n", i->app_fd);
275
276 dsp_drain(i);
277
278 if (i->mainloop)
279 pa_threaded_mainloop_stop(i->mainloop);
280
281 if (i->stream) {
282 pa_stream_disconnect(i->stream);
283 pa_stream_unref(i->stream);
284 }
285
286 if (i->context) {
287 pa_context_disconnect(i->context);
288 pa_context_unref(i->context);
289 }
290
291 if (i->mainloop)
292 pa_threaded_mainloop_free(i->mainloop);
293
294 if (i->app_fd >= 0) {
295 LOAD_CLOSE_FUNC();
296 _close(i->app_fd);
297 }
298
299 if (i->thread_fd >= 0) {
300 LOAD_CLOSE_FUNC();
301 _close(i->thread_fd);
302 }
303
304 free(i->buf);
305
306 pthread_mutex_destroy(&i->mutex);
307 free(i);
308 }
309
310 static fd_info *fd_info_ref(fd_info *i) {
311 assert(i);
312
313 pthread_mutex_lock(&i->mutex);
314 assert(i->ref >= 1);
315 i->ref++;
316
317 /* debug(__FILE__": ref++, now %i\n", i->ref); */
318 pthread_mutex_unlock(&i->mutex);
319
320 return i;
321 }
322
323 static void fd_info_unref(fd_info *i) {
324 int r;
325 pthread_mutex_lock(&i->mutex);
326 assert(i->ref >= 1);
327 r = --i->ref;
328 /* debug(__FILE__": ref--, now %i\n", i->ref); */
329 pthread_mutex_unlock(&i->mutex);
330
331 if (r <= 0)
332 fd_info_free(i);
333 }
334
335 static void context_state_cb(pa_context *c, void *userdata) {
336 fd_info *i = userdata;
337 assert(c);
338
339 switch (pa_context_get_state(c)) {
340 case PA_CONTEXT_READY:
341 case PA_CONTEXT_TERMINATED:
342 case PA_CONTEXT_FAILED:
343 pa_threaded_mainloop_signal(i->mainloop, 0);
344 break;
345
346 case PA_CONTEXT_UNCONNECTED:
347 case PA_CONTEXT_CONNECTING:
348 case PA_CONTEXT_AUTHORIZING:
349 case PA_CONTEXT_SETTING_NAME:
350 break;
351 }
352 }
353
354 static void reset_params(fd_info *i) {
355 assert(i);
356
357 i->sample_spec.format = PA_SAMPLE_ULAW;
358 i->sample_spec.channels = 1;
359 i->sample_spec.rate = 8000;
360 i->fragment_size = 0;
361 i->n_fragments = 0;
362 }
363
364 static const char *client_name(char *buf, size_t n) {
365 char p[PATH_MAX];
366 const char *e;
367
368 if ((e = getenv("PADSP_CLIENT_NAME")))
369 return e;
370
371 if (pa_get_binary_name(p, sizeof(p)))
372 snprintf(buf, n, "OSS Emulation[%s]", pa_path_get_filename(p));
373 else
374 snprintf(buf, n, "OSS");
375
376 return buf;
377 }
378
379 static const char *stream_name(void) {
380 const char *e;
381
382 if ((e = getenv("PADSP_STREAM_NAME")))
383 return e;
384
385 return "Audio Stream";
386 }
387
388 static void atfork_prepare(void) {
389 fd_info *i;
390
391 debug(__FILE__": atfork_prepare() enter\n");
392
393 function_enter();
394
395 pthread_mutex_lock(&fd_infos_mutex);
396
397 for (i = fd_infos; i; i = i->next) {
398 pthread_mutex_lock(&i->mutex);
399 pa_threaded_mainloop_lock(i->mainloop);
400 }
401
402 pthread_mutex_lock(&func_mutex);
403
404
405 debug(__FILE__": atfork_prepare() exit\n");
406 }
407
408 static void atfork_parent(void) {
409 fd_info *i;
410
411 debug(__FILE__": atfork_parent() enter\n");
412
413 pthread_mutex_unlock(&func_mutex);
414
415 for (i = fd_infos; i; i = i->next) {
416 pa_threaded_mainloop_unlock(i->mainloop);
417 pthread_mutex_unlock(&i->mutex);
418 }
419
420 pthread_mutex_unlock(&fd_infos_mutex);
421
422 function_exit();
423
424 debug(__FILE__": atfork_parent() exit\n");
425 }
426
427 static void atfork_child(void) {
428 fd_info *i;
429
430 debug(__FILE__": atfork_child() enter\n");
431
432 /* We do only the bare minimum to get all fds closed */
433 pthread_mutex_init(&func_mutex, NULL);
434 pthread_mutex_init(&fd_infos_mutex, NULL);
435
436 for (i = fd_infos; i; i = i->next) {
437 pthread_mutex_init(&i->mutex, NULL);
438
439 if (i->context) {
440 pa_context_disconnect(i->context);
441 pa_context_unref(i->context);
442 i->context = NULL;
443 }
444
445 if (i->stream) {
446 pa_stream_unref(i->stream);
447 i->stream = NULL;
448 }
449
450 if (i->app_fd >= 0) {
451 close(i->app_fd);
452 i->app_fd = -1;
453 }
454
455 if (i->thread_fd >= 0) {
456 close(i->thread_fd);
457 i->thread_fd = -1;
458 }
459
460 i->unusable = 1;
461 }
462
463 function_exit();
464
465 debug(__FILE__": atfork_child() exit\n");
466 }
467
468 static void install_atfork(void) {
469 pthread_atfork(atfork_prepare, atfork_parent, atfork_child);
470 }
471
472 static void stream_success_cb(pa_stream *s, int success, void *userdata) {
473 fd_info *i = userdata;
474
475 assert(s);
476 assert(i);
477
478 i->operation_success = success;
479 pa_threaded_mainloop_signal(i->mainloop, 0);
480 }
481
482 static void context_success_cb(pa_context *c, int success, void *userdata) {
483 fd_info *i = userdata;
484
485 assert(c);
486 assert(i);
487
488 i->operation_success = success;
489 pa_threaded_mainloop_signal(i->mainloop, 0);
490 }
491
492 static fd_info* fd_info_new(fd_info_type_t type, int *_errno) {
493 fd_info *i;
494 int sfds[2] = { -1, -1 };
495 char name[64];
496 static pthread_once_t install_atfork_once = PTHREAD_ONCE_INIT;
497
498 debug(__FILE__": fd_info_new()\n");
499
500 signal(SIGPIPE, SIG_IGN); /* Yes, ugly as hell */
501
502 pthread_once(&install_atfork_once, install_atfork);
503
504 if (!(i = malloc(sizeof(fd_info)))) {
505 *_errno = ENOMEM;
506 goto fail;
507 }
508
509 i->app_fd = i->thread_fd = -1;
510 i->type = type;
511
512 i->mainloop = NULL;
513 i->context = NULL;
514 i->stream = NULL;
515 i->io_event = NULL;
516 pthread_mutex_init(&i->mutex, NULL);
517 i->ref = 1;
518 i->buf = NULL;
519 i->unusable = 0;
520 pa_cvolume_reset(&i->volume, 2);
521 i->volume_modify_count = 0;
522 i->sink_index = (uint32_t) -1;
523 PA_LLIST_INIT(fd_info, i);
524
525 reset_params(i);
526
527 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sfds) < 0) {
528 *_errno = errno;
529 debug(__FILE__": socket() failed: %s\n", strerror(errno));
530 goto fail;
531 }
532
533 i->app_fd = sfds[0];
534 i->thread_fd = sfds[1];
535
536 if (!(i->mainloop = pa_threaded_mainloop_new())) {
537 *_errno = EIO;
538 debug(__FILE__": pa_threaded_mainloop_new() failed\n");
539 goto fail;
540 }
541
542 if (!(i->context = pa_context_new(pa_threaded_mainloop_get_api(i->mainloop), client_name(name, sizeof(name))))) {
543 *_errno = EIO;
544 debug(__FILE__": pa_context_new() failed\n");
545 goto fail;
546 }
547
548 pa_context_set_state_callback(i->context, context_state_cb, i);
549
550 if (pa_context_connect(i->context, NULL, 0, NULL) < 0) {
551 *_errno = ECONNREFUSED;
552 debug(__FILE__": pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
553 goto fail;
554 }
555
556 pa_threaded_mainloop_lock(i->mainloop);
557
558 if (pa_threaded_mainloop_start(i->mainloop) < 0) {
559 *_errno = EIO;
560 debug(__FILE__": pa_threaded_mainloop_start() failed\n");
561 goto unlock_and_fail;
562 }
563
564 /* Wait until the context is ready */
565 pa_threaded_mainloop_wait(i->mainloop);
566
567 if (pa_context_get_state(i->context) != PA_CONTEXT_READY) {
568 *_errno = ECONNREFUSED;
569 debug(__FILE__": pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
570 goto unlock_and_fail;
571 }
572
573 pa_threaded_mainloop_unlock(i->mainloop);
574 return i;
575
576 unlock_and_fail:
577
578 pa_threaded_mainloop_unlock(i->mainloop);
579
580 fail:
581
582 if (i)
583 fd_info_unref(i);
584
585 return NULL;
586 }
587
588 static void fd_info_add_to_list(fd_info *i) {
589 assert(i);
590
591 pthread_mutex_lock(&fd_infos_mutex);
592 PA_LLIST_PREPEND(fd_info, fd_infos, i);
593 pthread_mutex_unlock(&fd_infos_mutex);
594
595 fd_info_ref(i);
596 }
597
598 static void fd_info_remove_from_list(fd_info *i) {
599 assert(i);
600
601 pthread_mutex_lock(&fd_infos_mutex);
602 PA_LLIST_REMOVE(fd_info, fd_infos, i);
603 pthread_mutex_unlock(&fd_infos_mutex);
604
605 fd_info_unref(i);
606 }
607
608 static fd_info* fd_info_find(int fd) {
609 fd_info *i;
610
611 pthread_mutex_lock(&fd_infos_mutex);
612
613 for (i = fd_infos; i; i = i->next)
614 if (i->app_fd == fd && !i->unusable) {
615 fd_info_ref(i);
616 break;
617 }
618
619 pthread_mutex_unlock(&fd_infos_mutex);
620
621 return i;
622 }
623
624 static void fix_metrics(fd_info *i) {
625 size_t fs;
626 char t[PA_SAMPLE_SPEC_SNPRINT_MAX];
627
628 fs = pa_frame_size(&i->sample_spec);
629 i->fragment_size = (i->fragment_size/fs)*fs;
630
631 /* Number of fragments set? */
632 if (i->n_fragments < 2) {
633 if (i->fragment_size > 0) {
634 i->n_fragments = pa_bytes_per_second(&i->sample_spec) / 2 / i->fragment_size;
635 if (i->n_fragments < 2)
636 i->n_fragments = 2;
637 } else
638 i->n_fragments = 12;
639 }
640
641 /* Fragment size set? */
642 if (i->fragment_size <= 0) {
643 i->fragment_size = pa_bytes_per_second(&i->sample_spec) / 2 / i->n_fragments;
644 if (i->fragment_size < 1024)
645 i->fragment_size = 1024;
646 }
647
648 debug(__FILE__": sample spec: %s\n", pa_sample_spec_snprint(t, sizeof(t), &i->sample_spec));
649 debug(__FILE__": fixated metrics to %i fragments, %li bytes each.\n", i->n_fragments, (long)i->fragment_size);
650 }
651
652 static void stream_request_cb(pa_stream *s, size_t length, void *userdata) {
653 fd_info *i = userdata;
654 assert(s);
655
656 if (i->io_event) {
657 pa_mainloop_api *api;
658 api = pa_threaded_mainloop_get_api(i->mainloop);
659 api->io_enable(i->io_event, PA_IO_EVENT_INPUT);
660 }
661 }
662
663 static void stream_latency_update_cb(pa_stream *s, void *userdata) {
664 fd_info *i = userdata;
665 assert(s);
666
667 pa_threaded_mainloop_signal(i->mainloop, 0);
668 }
669
670 static void fd_info_shutdown(fd_info *i) {
671 assert(i);
672
673 if (i->io_event) {
674 pa_mainloop_api *api;
675 api = pa_threaded_mainloop_get_api(i->mainloop);
676 api->io_free(i->io_event);
677 i->io_event = NULL;
678 }
679
680 if (i->thread_fd >= 0) {
681 close(i->thread_fd);
682 i->thread_fd = -1;
683 }
684 }
685
686 static int fd_info_copy_data(fd_info *i, int force) {
687 size_t n;
688
689 if (!i->stream)
690 return -1;
691
692 if ((n = pa_stream_writable_size(i->stream)) == (size_t) -1) {
693 debug(__FILE__": pa_stream_writable_size(): %s\n", pa_strerror(pa_context_errno(i->context)));
694 return -1;
695 }
696
697 while (n >= i->fragment_size || force) {
698 ssize_t r;
699
700 if (!i->buf) {
701 if (!(i->buf = malloc(i->fragment_size))) {
702 debug(__FILE__": malloc() failed.\n");
703 return -1;
704 }
705 }
706
707 if ((r = read(i->thread_fd, i->buf, i->fragment_size)) <= 0) {
708
709 if (errno == EAGAIN)
710 break;
711
712 debug(__FILE__": read(): %s\n", r == 0 ? "EOF" : strerror(errno));
713 return -1;
714 }
715
716 if (pa_stream_write(i->stream, i->buf, r, free, 0, PA_SEEK_RELATIVE) < 0) {
717 debug(__FILE__": pa_stream_write(): %s\n", pa_strerror(pa_context_errno(i->context)));
718 return -1;
719 }
720
721 i->buf = NULL;
722
723 assert(n >= (size_t) r);
724 n -= r;
725 }
726
727 if (i->io_event) {
728 pa_mainloop_api *api;
729 api = pa_threaded_mainloop_get_api(i->mainloop);
730 api->io_enable(i->io_event, n >= i->fragment_size ? PA_IO_EVENT_INPUT : 0);
731 }
732
733 return 0;
734 }
735
736 static void stream_state_cb(pa_stream *s, void * userdata) {
737 fd_info *i = userdata;
738 assert(s);
739
740 switch (pa_stream_get_state(s)) {
741
742 case PA_STREAM_READY:
743 debug(__FILE__": stream established.\n");
744 break;
745
746 case PA_STREAM_FAILED:
747 debug(__FILE__": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
748 fd_info_shutdown(i);
749 break;
750
751 case PA_STREAM_TERMINATED:
752 case PA_STREAM_UNCONNECTED:
753 case PA_STREAM_CREATING:
754 break;
755 }
756 }
757
758 static int create_stream(fd_info *i) {
759 pa_buffer_attr attr;
760 int n;
761
762 assert(i);
763
764 fix_metrics(i);
765
766 if (!(i->stream = pa_stream_new(i->context, stream_name(), &i->sample_spec, NULL))) {
767 debug(__FILE__": pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
768 goto fail;
769 }
770
771 pa_stream_set_state_callback(i->stream, stream_state_cb, i);
772 pa_stream_set_write_callback(i->stream, stream_request_cb, i);
773 pa_stream_set_latency_update_callback(i->stream, stream_latency_update_cb, i);
774
775 memset(&attr, 0, sizeof(attr));
776 attr.maxlength = i->fragment_size * (i->n_fragments+1);
777 attr.tlength = i->fragment_size * i->n_fragments;
778 attr.prebuf = i->fragment_size;
779 attr.minreq = i->fragment_size;
780
781 if (pa_stream_connect_playback(i->stream, NULL, &attr, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE, NULL, NULL) < 0) {
782 debug(__FILE__": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
783 goto fail;
784 }
785
786 n = i->fragment_size;
787 setsockopt(i->app_fd, SOL_SOCKET, SO_SNDBUF, &n, sizeof(n));
788 n = i->fragment_size;
789 setsockopt(i->thread_fd, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n));
790
791 return 0;
792
793 fail:
794 return -1;
795 }
796
797 static void free_stream(fd_info *i) {
798 assert(i);
799
800 if (i->stream) {
801 pa_stream_disconnect(i->stream);
802 pa_stream_unref(i->stream);
803 i->stream = NULL;
804 }
805 }
806
807 static void io_event_cb(pa_mainloop_api *api, pa_io_event *e, int fd, pa_io_event_flags_t flags, void *userdata) {
808 fd_info *i = userdata;
809
810 pa_threaded_mainloop_signal(i->mainloop, 0);
811
812 if (flags & PA_IO_EVENT_INPUT) {
813
814 if (!i->stream) {
815 api->io_enable(e, 0);
816
817 if (create_stream(i) < 0)
818 goto fail;
819
820 } else {
821 if (fd_info_copy_data(i, 0) < 0)
822 goto fail;
823 }
824
825 } else if (flags & (PA_IO_EVENT_HANGUP|PA_IO_EVENT_ERROR))
826 goto fail;
827
828 return;
829
830 fail:
831 /* We can't do anything better than removing the event source */
832 fd_info_shutdown(i);
833 }
834
835 static int dsp_open(int flags, int *_errno) {
836 fd_info *i;
837 pa_mainloop_api *api;
838 int ret;
839 int f;
840
841 if ((flags != O_WRONLY) && (flags != (O_WRONLY|O_NONBLOCK))) {
842 *_errno = EACCES;
843 return -1;
844 }
845
846 if (!(i = fd_info_new(FD_INFO_PLAYBACK, _errno)))
847 return -1;
848
849 shutdown(i->thread_fd, SHUT_WR);
850 shutdown(i->app_fd, SHUT_RD);
851
852 if ((flags & O_NONBLOCK) == O_NONBLOCK) {
853 if ((f = fcntl(i->app_fd, F_GETFL)) >= 0)
854 fcntl(i->app_fd, F_SETFL, f|O_NONBLOCK);
855 }
856 if ((f = fcntl(i->thread_fd, F_GETFL)) >= 0)
857 fcntl(i->thread_fd, F_SETFL, f|O_NONBLOCK);
858
859 fcntl(i->app_fd, F_SETFD, FD_CLOEXEC);
860 fcntl(i->thread_fd, F_SETFD, FD_CLOEXEC);
861
862 pa_threaded_mainloop_lock(i->mainloop);
863 api = pa_threaded_mainloop_get_api(i->mainloop);
864 if (!(i->io_event = api->io_new(api, i->thread_fd, PA_IO_EVENT_INPUT, io_event_cb, i)))
865 goto fail;
866
867 pa_threaded_mainloop_unlock(i->mainloop);
868
869 debug(__FILE__": dsp_open() succeeded, fd=%i\n", i->app_fd);
870
871 fd_info_add_to_list(i);
872 ret = i->app_fd;
873 fd_info_unref(i);
874
875 return ret;
876
877 fail:
878 pa_threaded_mainloop_unlock(i->mainloop);
879
880 if (i)
881 fd_info_unref(i);
882
883 *_errno = EIO;
884
885 debug(__FILE__": dsp_open() failed\n");
886
887 return -1;
888 }
889
890 static void sink_info_cb(pa_context *context, const pa_sink_info *si, int eol, void *userdata) {
891 fd_info *i = userdata;
892
893 if (!si && eol < 0) {
894 i->operation_success = 0;
895 pa_threaded_mainloop_signal(i->mainloop, 0);
896 return;
897 }
898
899 if (eol)
900 return;
901
902 if (!pa_cvolume_equal(&i->volume, &si->volume))
903 i->volume_modify_count++;
904
905 i->volume = si->volume;
906 i->sink_index = si->index;
907
908 i->operation_success = 1;
909 pa_threaded_mainloop_signal(i->mainloop, 0);
910 }
911
912 static void subscribe_cb(pa_context *context, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
913 fd_info *i = userdata;
914 pa_operation *o = NULL;
915
916 if (i->sink_index != idx)
917 return;
918
919 if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) != PA_SUBSCRIPTION_EVENT_CHANGE)
920 return;
921
922 if (!(o = pa_context_get_sink_info_by_index(i->context, i->sink_index, sink_info_cb, i))) {
923 debug(__FILE__": Failed to get sink info: %s", pa_strerror(pa_context_errno(i->context)));
924 return;
925 }
926
927 pa_operation_unref(o);
928 }
929
930 static int mixer_open(int flags, int *_errno) {
931 fd_info *i;
932 pa_operation *o;
933 int ret;
934
935 if (!(i = fd_info_new(FD_INFO_MIXER, _errno)))
936 return -1;
937
938 pa_threaded_mainloop_lock(i->mainloop);
939
940 pa_context_set_subscribe_callback(i->context, subscribe_cb, i);
941
942 if (!(o = pa_context_subscribe(i->context, PA_SUBSCRIPTION_MASK_SINK, context_success_cb, i))) {
943 debug(__FILE__": Failed to subscribe to events: %s", pa_strerror(pa_context_errno(i->context)));
944 *_errno = EIO;
945 goto fail;
946 }
947
948 i->operation_success = 0;
949 while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
950 pa_threaded_mainloop_wait(i->mainloop);
951 CONTEXT_CHECK_DEAD_GOTO(i, fail);
952 }
953
954 if (!i->operation_success) {
955 debug(__FILE__":Failed to subscribe to events: %s", pa_strerror(pa_context_errno(i->context)));
956 *_errno = EIO;
957 goto fail;
958 }
959
960 /* Get sink info */
961
962 pa_operation_unref(o);
963 if (!(o = pa_context_get_sink_info_by_name(i->context, NULL, sink_info_cb, i))) {
964 debug(__FILE__": Failed to get sink info: %s", pa_strerror(pa_context_errno(i->context)));
965 *_errno = EIO;
966 goto fail;
967 }
968
969 i->operation_success = 0;
970 while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
971 pa_threaded_mainloop_wait(i->mainloop);
972 CONTEXT_CHECK_DEAD_GOTO(i, fail);
973 }
974
975 if (!i->operation_success) {
976 debug(__FILE__": Failed to get sink info: %s", pa_strerror(pa_context_errno(i->context)));
977 *_errno = EIO;
978 goto fail;
979 }
980
981 pa_threaded_mainloop_unlock(i->mainloop);
982
983 debug(__FILE__": mixer_open() succeeded, fd=%i\n", i->app_fd);
984
985 fd_info_add_to_list(i);
986 ret = i->app_fd;
987 fd_info_unref(i);
988
989 return ret;
990
991 fail:
992 pa_threaded_mainloop_unlock(i->mainloop);
993
994 if (i)
995 fd_info_unref(i);
996
997 *_errno = EIO;
998
999 debug(__FILE__": mixer_open() failed\n");
1000
1001 return -1;
1002 }
1003
1004 static int sndstat_open(int flags, int *_errno) {
1005 static const char sndstat[] =
1006 "Sound Driver:3.8.1a-980706 (Polypaudio Virtual OSS)\n"
1007 "Kernel: POSIX\n"
1008 "Config options: 0\n"
1009 "\n"
1010 "Installed drivers:\n"
1011 "Type 255: Polypaudio Virtual OSS\n"
1012 "\n"
1013 "Card config:\n"
1014 "Polypaudio Virtual OSS\n"
1015 "\n"
1016 "Audio devices:\n"
1017 "0: Polypaudio Virtual OSS\n"
1018 "\n"
1019 "Synth devices: NOT ENABLED IN CONFIG\n"
1020 "\n"
1021 "Midi devices:\n"
1022 "\n"
1023 "Timers:\n"
1024 "\n"
1025 "Mixers:\n"
1026 "0: Polypaudio Virtual OSS\n";
1027
1028 char fn[] = "/tmp/padsp-sndstat-XXXXXX";
1029 mode_t u;
1030 int fd = -1;
1031 int e;
1032
1033 debug(__FILE__": sndstat_open()\n");
1034
1035 if (flags != O_RDONLY && flags != (O_RDONLY|O_LARGEFILE)) {
1036 *_errno = EACCES;
1037 debug(__FILE__": bad access!\n");
1038 goto fail;
1039 }
1040
1041 u = umask(0077);
1042 fd = mkstemp(fn);
1043 e = errno;
1044 umask(u);
1045
1046 if (fd < 0) {
1047 *_errno = e;
1048 debug(__FILE__": mkstemp() failed: %s\n", strerror(errno));
1049 goto fail;
1050 }
1051
1052 unlink(fn);
1053
1054 if (write(fd, sndstat, sizeof(sndstat) -1) != sizeof(sndstat)-1) {
1055 *_errno = errno;
1056 debug(__FILE__": write() failed: %s\n", strerror(errno));
1057 goto fail;
1058 }
1059
1060 if (lseek(fd, SEEK_SET, 0) < 0) {
1061 *_errno = errno;
1062 debug(__FILE__": lseek() failed: %s\n", strerror(errno));
1063 goto fail;
1064 }
1065
1066 return fd;
1067
1068 fail:
1069 if (fd >= 0)
1070 close(fd);
1071 return -1;
1072 }
1073
1074 int open(const char *filename, int flags, ...) {
1075 va_list args;
1076 mode_t mode = 0;
1077 int r, _errno = 0;
1078
1079 debug(__FILE__": open(%s)\n", filename);
1080
1081 va_start(args, flags);
1082 if (flags & O_CREAT)
1083 mode = va_arg(args, mode_t);
1084 va_end(args);
1085
1086 if (!function_enter()) {
1087 LOAD_OPEN_FUNC();
1088 return _open(filename, flags, mode);
1089 }
1090
1091 if (dsp_cloak_enable() && (strcmp(filename, "/dev/dsp") == 0 || strcmp(filename, "/dev/adsp") == 0)) {
1092 r = dsp_open(flags, &_errno);
1093 } else if (mixer_cloak_enable() && strcmp(filename, "/dev/mixer") == 0) {
1094 r = mixer_open(flags, &_errno);
1095 } else if (sndstat_cloak_enable() && strcmp(filename, "/dev/sndstat") == 0) {
1096 r = sndstat_open(flags, &_errno);
1097 } else {
1098 function_exit();
1099 LOAD_OPEN_FUNC();
1100 return _open(filename, flags, mode);
1101 }
1102
1103 function_exit();
1104
1105 if (_errno)
1106 errno = _errno;
1107
1108 return r;
1109 }
1110
1111 static int mixer_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno) {
1112 int ret = -1;
1113
1114 switch (request) {
1115 case SOUND_MIXER_READ_DEVMASK :
1116 debug(__FILE__": SOUND_MIXER_READ_DEVMASK\n");
1117
1118 *(int*) argp = SOUND_MASK_PCM;
1119 break;
1120
1121 case SOUND_MIXER_READ_RECMASK :
1122 debug(__FILE__": SOUND_MIXER_READ_RECMASK\n");
1123
1124 *(int*) argp = 0;
1125 break;
1126
1127 case SOUND_MIXER_READ_STEREODEVS:
1128 debug(__FILE__": SOUND_MIXER_READ_STEREODEVS\n");
1129
1130 pa_threaded_mainloop_lock(i->mainloop);
1131 *(int*) argp = i->volume.channels > 1 ? SOUND_MASK_PCM : 0;
1132 pa_threaded_mainloop_unlock(i->mainloop);
1133
1134 break;
1135
1136 case SOUND_MIXER_READ_RECSRC:
1137 debug(__FILE__": SOUND_MIXER_READ_RECSRC\n");
1138
1139 *(int*) argp = 0;
1140 break;
1141
1142 case SOUND_MIXER_READ_CAPS:
1143 debug(__FILE__": SOUND_MIXER_READ_CAPS\n");
1144
1145 *(int*) argp = 0;
1146 break;
1147
1148 case SOUND_MIXER_READ_PCM:
1149
1150 debug(__FILE__": SOUND_MIXER_READ_PCM\n");
1151
1152 pa_threaded_mainloop_lock(i->mainloop);
1153
1154 *(int*) argp =
1155 ((i->volume.values[0]*100/PA_VOLUME_NORM)) |
1156 ((i->volume.values[i->volume.channels > 1 ? 1 : 0]*100/PA_VOLUME_NORM) << 8);
1157
1158 pa_threaded_mainloop_unlock(i->mainloop);
1159
1160 break;
1161
1162 case SOUND_MIXER_WRITE_PCM: {
1163 pa_cvolume v;
1164
1165 debug(__FILE__": SOUND_MIXER_WRITE_PCM\n");
1166
1167 pa_threaded_mainloop_lock(i->mainloop);
1168
1169 v = i->volume;
1170
1171 i->volume.values[0] = ((*(int*) argp & 0xFF)*PA_VOLUME_NORM)/100;
1172 i->volume.values[1] = ((*(int*) argp >> 8)*PA_VOLUME_NORM)/100;
1173
1174 if (!pa_cvolume_equal(&i->volume, &v)) {
1175 pa_operation *o;
1176
1177 if (!(o = pa_context_set_sink_volume_by_index(i->context, i->sink_index, &i->volume, NULL, NULL)))
1178 debug(__FILE__":Failed set volume: %s", pa_strerror(pa_context_errno(i->context)));
1179 else {
1180
1181 i->operation_success = 0;
1182 while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
1183 CONTEXT_CHECK_DEAD_GOTO(i, exit_loop);
1184
1185 pa_threaded_mainloop_wait(i->mainloop);
1186 }
1187 exit_loop:
1188
1189 if (!i->operation_success)
1190 debug(__FILE__": Failed to set volume: %s\n", pa_strerror(pa_context_errno(i->context)));
1191
1192 pa_operation_unref(o);
1193 }
1194
1195 /* We don't wait for completion here */
1196 i->volume_modify_count++;
1197 }
1198
1199 pa_threaded_mainloop_unlock(i->mainloop);
1200
1201 break;
1202 }
1203
1204 case SOUND_MIXER_INFO: {
1205 mixer_info *mi = argp;
1206
1207 memset(mi, 0, sizeof(mixer_info));
1208 strncpy(mi->id, "POLYPAUDIO", sizeof(mi->id));
1209 strncpy(mi->name, "Polypaudio Virtual OSS", sizeof(mi->name));
1210 pa_threaded_mainloop_lock(i->mainloop);
1211 mi->modify_counter = i->volume_modify_count;
1212 pa_threaded_mainloop_unlock(i->mainloop);
1213 break;
1214 }
1215
1216 default:
1217 debug(__FILE__": unknown ioctl 0x%08lx\n", request);
1218
1219 *_errno = EINVAL;
1220 goto fail;
1221 }
1222
1223 ret = 0;
1224
1225 fail:
1226
1227 return ret;
1228 }
1229
1230 static int map_format(int *fmt, pa_sample_spec *ss) {
1231
1232 switch (*fmt) {
1233 case AFMT_MU_LAW:
1234 ss->format = PA_SAMPLE_ULAW;
1235 break;
1236
1237 case AFMT_A_LAW:
1238 ss->format = PA_SAMPLE_ALAW;
1239 break;
1240
1241 case AFMT_S8:
1242 *fmt = AFMT_U8;
1243 /* fall through */
1244 case AFMT_U8:
1245 ss->format = PA_SAMPLE_U8;
1246 break;
1247
1248 case AFMT_U16_BE:
1249 *fmt = AFMT_S16_BE;
1250 /* fall through */
1251 case AFMT_S16_BE:
1252 ss->format = PA_SAMPLE_S16BE;
1253 break;
1254
1255 case AFMT_U16_LE:
1256 *fmt = AFMT_S16_LE;
1257 /* fall through */
1258 case AFMT_S16_LE:
1259 ss->format = PA_SAMPLE_S16LE;
1260 break;
1261
1262 default:
1263 ss->format = PA_SAMPLE_S16NE;
1264 *fmt = AFMT_S16_NE;
1265 break;
1266 }
1267
1268 return 0;
1269 }
1270
1271 static int map_format_back(pa_sample_format_t format) {
1272 switch (format) {
1273 case PA_SAMPLE_S16LE: return AFMT_S16_LE;
1274 case PA_SAMPLE_S16BE: return AFMT_S16_BE;
1275 case PA_SAMPLE_ULAW: return AFMT_MU_LAW;
1276 case PA_SAMPLE_ALAW: return AFMT_A_LAW;
1277 case PA_SAMPLE_U8: return AFMT_U8;
1278 default:
1279 abort();
1280 }
1281 }
1282
1283 static int dsp_flush_socket(fd_info *i) {
1284 int l;
1285
1286 if (i->thread_fd < 0)
1287 return -1;
1288
1289 if (ioctl(i->thread_fd, SIOCINQ, &l) < 0) {
1290 debug(__FILE__": SIOCINQ: %s\n", strerror(errno));
1291 return -1;
1292 }
1293
1294 while (l > 0) {
1295 char buf[1024];
1296 size_t k;
1297
1298 k = (size_t) l > sizeof(buf) ? sizeof(buf) : (size_t) l;
1299 if (read(i->thread_fd, buf, k) < 0)
1300 debug(__FILE__": read(): %s\n", strerror(errno));
1301 l -= k;
1302 }
1303
1304 return 0;
1305 }
1306
1307 static int dsp_empty_socket(fd_info *i) {
1308 int ret = -1;
1309
1310 /* Empty the socket */
1311 for (;;) {
1312 int l;
1313
1314 if (i->thread_fd < 0)
1315 break;
1316
1317 if (ioctl(i->thread_fd, SIOCINQ, &l) < 0) {
1318 debug(__FILE__": SIOCINQ: %s\n", strerror(errno));
1319 break;
1320 }
1321
1322 if (!l) {
1323 ret = 0;
1324 break;
1325 }
1326
1327 pa_threaded_mainloop_wait(i->mainloop);
1328 }
1329
1330 return ret;
1331 }
1332
1333 static int dsp_drain(fd_info *i) {
1334 pa_operation *o = NULL;
1335 int r = -1;
1336
1337 if (!i->mainloop)
1338 return 0;
1339
1340 debug(__FILE__": Draining.\n");
1341
1342 pa_threaded_mainloop_lock(i->mainloop);
1343
1344 if (dsp_empty_socket(i) < 0)
1345 goto fail;
1346
1347 if (!i->stream)
1348 goto fail;
1349
1350 debug(__FILE__": Really draining.\n");
1351
1352 if (!(o = pa_stream_drain(i->stream, stream_success_cb, i))) {
1353 debug(__FILE__": pa_stream_drain(): %s\n", pa_strerror(pa_context_errno(i->context)));
1354 goto fail;
1355 }
1356
1357 i->operation_success = 0;
1358 while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
1359 STREAM_CHECK_DEAD_GOTO(i, fail);
1360
1361 pa_threaded_mainloop_wait(i->mainloop);
1362 }
1363
1364 if (!i->operation_success) {
1365 debug(__FILE__": pa_stream_drain() 2: %s\n", pa_strerror(pa_context_errno(i->context)));
1366 goto fail;
1367 }
1368
1369 r = 0;
1370
1371 fail:
1372
1373 if (o)
1374 pa_operation_unref(o);
1375
1376 pa_threaded_mainloop_unlock(i->mainloop);
1377
1378 return 0;
1379 }
1380
1381 static int dsp_trigger(fd_info *i) {
1382 pa_operation *o = NULL;
1383 int r = -1;
1384
1385 if (!i->stream)
1386 return 0;
1387
1388 pa_threaded_mainloop_lock(i->mainloop);
1389
1390 if (dsp_empty_socket(i) < 0)
1391 goto fail;
1392
1393 debug(__FILE__": Triggering.\n");
1394
1395 if (!(o = pa_stream_trigger(i->stream, stream_success_cb, i))) {
1396 debug(__FILE__": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i->context)));
1397 goto fail;
1398 }
1399
1400 i->operation_success = 0;
1401 while (!pa_operation_get_state(o) != PA_OPERATION_DONE) {
1402 STREAM_CHECK_DEAD_GOTO(i, fail);
1403
1404 pa_threaded_mainloop_wait(i->mainloop);
1405 }
1406
1407 if (!i->operation_success) {
1408 debug(__FILE__": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i->context)));
1409 goto fail;
1410 }
1411
1412 r = 0;
1413
1414 fail:
1415
1416 if (o)
1417 pa_operation_unref(o);
1418
1419 pa_threaded_mainloop_unlock(i->mainloop);
1420
1421 return 0;
1422 }
1423
1424 static int dsp_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno) {
1425 int ret = -1;
1426
1427 switch (request) {
1428 case SNDCTL_DSP_SETFMT: {
1429 debug(__FILE__": SNDCTL_DSP_SETFMT: %i\n", *(int*) argp);
1430
1431 pa_threaded_mainloop_lock(i->mainloop);
1432
1433 if (*(int*) argp == AFMT_QUERY)
1434 *(int*) argp = map_format_back(i->sample_spec.format);
1435 else {
1436 map_format((int*) argp, &i->sample_spec);
1437 free_stream(i);
1438 }
1439
1440 pa_threaded_mainloop_unlock(i->mainloop);
1441 break;
1442 }
1443
1444 case SNDCTL_DSP_SPEED: {
1445 pa_sample_spec ss;
1446 int valid;
1447 char t[256];
1448
1449 debug(__FILE__": SNDCTL_DSP_SPEED: %i\n", *(int*) argp);
1450
1451 pa_threaded_mainloop_lock(i->mainloop);
1452
1453 ss = i->sample_spec;
1454 ss.rate = *(int*) argp;
1455
1456 if ((valid = pa_sample_spec_valid(&ss))) {
1457 i->sample_spec = ss;
1458 free_stream(i);
1459 }
1460
1461 debug(__FILE__": ss: %s\n", pa_sample_spec_snprint(t, sizeof(t), &i->sample_spec));
1462
1463 pa_threaded_mainloop_unlock(i->mainloop);
1464
1465 if (!valid) {
1466 *_errno = EINVAL;
1467 goto fail;
1468 }
1469
1470 break;
1471 }
1472
1473 case SNDCTL_DSP_STEREO:
1474 debug(__FILE__": SNDCTL_DSP_STEREO: %i\n", *(int*) argp);
1475
1476 pa_threaded_mainloop_lock(i->mainloop);
1477
1478 i->sample_spec.channels = *(int*) argp ? 2 : 1;
1479 free_stream(i);
1480
1481 pa_threaded_mainloop_unlock(i->mainloop);
1482 return 0;
1483
1484 case SNDCTL_DSP_CHANNELS: {
1485 pa_sample_spec ss;
1486 int valid;
1487
1488 debug(__FILE__": SNDCTL_DSP_CHANNELS: %i\n", *(int*) argp);
1489
1490 pa_threaded_mainloop_lock(i->mainloop);
1491
1492 ss = i->sample_spec;
1493 ss.channels = *(int*) argp;
1494
1495 if ((valid = pa_sample_spec_valid(&ss))) {
1496 i->sample_spec = ss;
1497 free_stream(i);
1498 }
1499
1500 pa_threaded_mainloop_unlock(i->mainloop);
1501
1502 if (!valid) {
1503 *_errno = EINVAL;
1504 goto fail;
1505 }
1506
1507 break;
1508 }
1509
1510 case SNDCTL_DSP_GETBLKSIZE:
1511 debug(__FILE__": SNDCTL_DSP_GETBLKSIZE\n");
1512
1513 pa_threaded_mainloop_lock(i->mainloop);
1514
1515 fix_metrics(i);
1516 *(int*) argp = i->fragment_size;
1517
1518 pa_threaded_mainloop_unlock(i->mainloop);
1519
1520 break;
1521
1522 case SNDCTL_DSP_SETFRAGMENT:
1523 debug(__FILE__": SNDCTL_DSP_SETFRAGMENT: 0x%8x\n", *(int*) argp);
1524
1525 pa_threaded_mainloop_lock(i->mainloop);
1526
1527 i->fragment_size = 1 << (*(int*) argp);
1528 i->n_fragments = (*(int*) argp) >> 16;
1529
1530 free_stream(i);
1531
1532 pa_threaded_mainloop_unlock(i->mainloop);
1533
1534 break;
1535
1536 case SNDCTL_DSP_GETCAPS:
1537 debug(__FILE__": SNDCTL_DSP_CAPS\n");
1538
1539 *(int*) argp = DSP_CAP_MULTI;
1540 break;
1541
1542 case SNDCTL_DSP_GETODELAY: {
1543 int l;
1544
1545 debug(__FILE__": SNDCTL_DSP_GETODELAY\n");
1546
1547 pa_threaded_mainloop_lock(i->mainloop);
1548
1549 *(int*) argp = 0;
1550
1551 for (;;) {
1552 pa_usec_t usec;
1553
1554 STREAM_CHECK_DEAD_GOTO(i, exit_loop);
1555
1556 if (pa_stream_get_latency(i->stream, &usec, NULL) >= 0) {
1557 *(int*) argp = pa_usec_to_bytes(usec, &i->sample_spec);
1558 break;
1559 }
1560
1561 if (pa_context_errno(i->context) != PA_ERR_NODATA) {
1562 debug(__FILE__": pa_stream_get_latency(): %s\n", pa_strerror(pa_context_errno(i->context)));
1563 break;
1564 }
1565
1566 pa_threaded_mainloop_wait(i->mainloop);
1567 }
1568
1569 exit_loop:
1570
1571 if (ioctl(i->thread_fd, SIOCINQ, &l) < 0)
1572 debug(__FILE__": SIOCINQ failed: %s\n", strerror(errno));
1573 else
1574 *(int*) argp += l;
1575
1576 pa_threaded_mainloop_unlock(i->mainloop);
1577
1578 debug(__FILE__": ODELAY: %i\n", *(int*) argp);
1579
1580 break;
1581 }
1582
1583 case SNDCTL_DSP_RESET: {
1584 debug(__FILE__": SNDCTL_DSP_RESET\n");
1585
1586 pa_threaded_mainloop_lock(i->mainloop);
1587
1588 free_stream(i);
1589 dsp_flush_socket(i);
1590 reset_params(i);
1591
1592 pa_threaded_mainloop_unlock(i->mainloop);
1593 break;
1594 }
1595
1596 case SNDCTL_DSP_GETFMTS: {
1597 debug(__FILE__": SNDCTL_DSP_GETFMTS\n");
1598
1599 *(int*) argp = AFMT_MU_LAW|AFMT_A_LAW|AFMT_U8|AFMT_S16_LE|AFMT_S16_BE;
1600 break;
1601 }
1602
1603 case SNDCTL_DSP_POST:
1604 debug(__FILE__": SNDCTL_DSP_POST\n");
1605
1606 if (dsp_trigger(i) < 0)
1607 *_errno = EIO;
1608 break;
1609
1610 case SNDCTL_DSP_SYNC:
1611 debug(__FILE__": SNDCTL_DSP_SYNC\n");
1612
1613 if (dsp_drain(i) < 0)
1614 *_errno = EIO;
1615
1616 break;
1617
1618 case SNDCTL_DSP_GETOSPACE: {
1619 audio_buf_info *bi = (audio_buf_info*) argp;
1620 int l;
1621 size_t k = 0;
1622
1623 debug(__FILE__": SNDCTL_DSP_GETOSPACE\n");
1624
1625 pa_threaded_mainloop_lock(i->mainloop);
1626
1627 fix_metrics(i);
1628
1629 if (i->stream) {
1630 if ((k = pa_stream_writable_size(i->stream)) == (size_t) -1)
1631 debug(__FILE__": pa_stream_writable_size(): %s\n", pa_strerror(pa_context_errno(i->context)));
1632 } else
1633 k = i->fragment_size * i->n_fragments;
1634
1635 if (ioctl(i->thread_fd, SIOCINQ, &l) < 0) {
1636 debug(__FILE__": SIOCINQ failed: %s\n", strerror(errno));
1637 l = 0;
1638 }
1639
1640 bi->fragsize = i->fragment_size;
1641 bi->fragstotal = i->n_fragments;
1642 bi->bytes = k > (size_t) l ? k - l : 0;
1643 bi->fragments = bi->bytes / bi->fragsize;
1644
1645 pa_threaded_mainloop_unlock(i->mainloop);
1646
1647 debug(__FILE__": fragsize=%i, fragstotal=%i, bytes=%i, fragments=%i\n", bi->fragsize, bi->fragstotal, bi->bytes, bi->fragments);
1648
1649 break;
1650 }
1651
1652 default:
1653 debug(__FILE__": unknwon ioctl 0x%08lx\n", request);
1654
1655 *_errno = EINVAL;
1656 goto fail;
1657 }
1658
1659 ret = 0;
1660
1661 fail:
1662
1663 return ret;
1664 }
1665
1666 int ioctl(int fd, unsigned long request, ...) {
1667 fd_info *i;
1668 va_list args;
1669 void *argp;
1670 int r, _errno = 0;
1671
1672 debug(__FILE__": ioctl()\n");
1673
1674 va_start(args, request);
1675 argp = va_arg(args, void *);
1676 va_end(args);
1677
1678 if (!function_enter()) {
1679 LOAD_IOCTL_FUNC();
1680 return _ioctl(fd, request, argp);
1681 }
1682
1683 if (!(i = fd_info_find(fd))) {
1684 function_exit();
1685 LOAD_IOCTL_FUNC();
1686 return _ioctl(fd, request, argp);
1687 }
1688
1689 if (i->type == FD_INFO_MIXER)
1690 r = mixer_ioctl(i, request, argp, &_errno);
1691 else
1692 r = dsp_ioctl(i, request, argp, &_errno);
1693
1694 fd_info_unref(i);
1695
1696 if (_errno)
1697 errno = _errno;
1698
1699 function_exit();
1700
1701 return r;
1702 }
1703
1704 int close(int fd) {
1705 fd_info *i;
1706
1707 debug(__FILE__": close()\n");
1708
1709 if (!function_enter()) {
1710 LOAD_CLOSE_FUNC();
1711 return _close(fd);
1712 }
1713
1714 if (!(i = fd_info_find(fd))) {
1715 function_exit();
1716 LOAD_CLOSE_FUNC();
1717 return _close(fd);
1718 }
1719
1720 fd_info_remove_from_list(i);
1721 fd_info_unref(i);
1722
1723 function_exit();
1724
1725 return 0;
1726 }
1727
1728 int open64(const char *filename, int flags, ...) {
1729 va_list args;
1730 mode_t mode = 0;
1731
1732 debug(__FILE__": open64(%s)\n", filename);
1733
1734 va_start(args, flags);
1735 if (flags & O_CREAT)
1736 mode = va_arg(args, mode_t);
1737 va_end(args);
1738
1739 if (strcmp(filename, "/dev/dsp") != 0 &&
1740 strcmp(filename, "/dev/adsp") != 0 &&
1741 strcmp(filename, "/dev/sndstat") != 0 &&
1742 strcmp(filename, "/dev/mixer") != 0) {
1743 LOAD_OPEN64_FUNC();
1744 return _open64(filename, flags, mode);
1745 }
1746
1747 return open(filename, flags, mode);
1748 }
1749
1750 FILE* fopen(const char *filename, const char *mode) {
1751 FILE *f = NULL;
1752 int fd;
1753 mode_t m;
1754
1755 debug(__FILE__": fopen(%s)\n", filename);
1756
1757 if (strcmp(filename, "/dev/dsp") == 0 ||
1758 strcmp(filename, "/dev/adsp") == 0) {
1759
1760 if (strcmp(mode, "wb") != 0) {
1761 errno = EACCES;
1762 return NULL;
1763 }
1764
1765 m = O_WRONLY;
1766 } else if (strcmp(filename, "/dev/sndstat") == 0) {
1767
1768 if (strcmp(mode, "r") != 0) {
1769 errno = EACCES;
1770 return NULL;
1771 }
1772
1773 m = O_RDONLY;
1774 } else if (strcmp(filename, "/dev/mixer") == 0)
1775 m = O_RDWR;
1776 else {
1777 LOAD_FOPEN_FUNC();
1778 return _fopen(filename, mode);
1779 }
1780
1781 if ((fd = open(filename, m)) < 0)
1782 return NULL;
1783
1784 if (!(f = fdopen(fd, mode))) {
1785 close(fd);
1786 return NULL;
1787 }
1788
1789 return f;
1790 }
1791
1792 FILE *fopen64(const char *filename, const char *mode) {
1793
1794 debug(__FILE__": fopen64(%s)\n", filename);
1795
1796 if (strcmp(filename, "/dev/dsp") != 0 &&
1797 strcmp(filename, "/dev/adsp") != 0 &&
1798 strcmp(filename, "/dev/sndstat") != 0 &&
1799 strcmp(filename, "/dev/mixer") != 0) {
1800 LOAD_FOPEN64_FUNC();
1801 return _fopen64(filename, mode);
1802 }
1803
1804 return fopen(filename, mode);
1805 }
1806
1807 int fclose(FILE *f) {
1808 fd_info *i;
1809
1810 debug(__FILE__": fclose()\n");
1811
1812 if (!function_enter()) {
1813 LOAD_FCLOSE_FUNC();
1814 return _fclose(f);
1815 }
1816
1817 if (!(i = fd_info_find(fileno(f)))) {
1818 function_exit();
1819 LOAD_FCLOSE_FUNC();
1820 return _fclose(f);
1821 }
1822
1823 fd_info_remove_from_list(i);
1824
1825 /* Dirty trick to avoid that the fd is not freed twice, once by us
1826 * and once by the real fclose() */
1827 i->app_fd = -1;
1828
1829 fd_info_unref(i);
1830
1831 function_exit();
1832
1833 LOAD_FCLOSE_FUNC();
1834 return _fclose(f);
1835 }