]> code.delx.au - pulseaudio/blob - src/utils/padsp.c
- use pthread_atfork() to disable open sound streams in the child after a fork.
[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_LLIST_FIELDS(fd_info);
83 };
84
85 static int dsp_drain(fd_info *i);
86 static void fd_info_remove_from_list(fd_info *i);
87
88 static pthread_mutex_t fd_infos_mutex = PTHREAD_MUTEX_INITIALIZER;
89 static pthread_mutex_t func_mutex = PTHREAD_MUTEX_INITIALIZER;
90
91 static PA_LLIST_HEAD(fd_info, fd_infos) = NULL;
92
93 static int (*_ioctl)(int, int, void*) = NULL;
94 static int (*_close)(int) = NULL;
95 static int (*_open)(const char *, int, mode_t) = NULL;
96 static FILE* (*_fopen)(const char *path, const char *mode) = NULL;
97 static int (*_open64)(const char *, int, mode_t) = NULL;
98 static FILE* (*_fopen64)(const char *path, const char *mode) = NULL;
99 static int (*_fclose)(FILE *f) = NULL;
100
101 #define LOAD_IOCTL_FUNC() \
102 do { \
103 pthread_mutex_lock(&func_mutex); \
104 if (!_ioctl) \
105 _ioctl = (int (*)(int, int, void*)) dlsym(RTLD_NEXT, "ioctl"); \
106 pthread_mutex_unlock(&func_mutex); \
107 } while(0)
108
109 #define LOAD_OPEN_FUNC() \
110 do { \
111 pthread_mutex_lock(&func_mutex); \
112 if (!_open) \
113 _open = (int (*)(const char *, int, mode_t)) dlsym(RTLD_NEXT, "open"); \
114 pthread_mutex_unlock(&func_mutex); \
115 } while(0)
116
117 #define LOAD_OPEN64_FUNC() \
118 do { \
119 pthread_mutex_lock(&func_mutex); \
120 if (!_open64) \
121 _open64 = (int (*)(const char *, int, mode_t)) dlsym(RTLD_NEXT, "open64"); \
122 pthread_mutex_unlock(&func_mutex); \
123 } while(0)
124
125 #define LOAD_CLOSE_FUNC() \
126 do { \
127 pthread_mutex_lock(&func_mutex); \
128 if (!_close) \
129 _close = (int (*)(int)) dlsym(RTLD_NEXT, "close"); \
130 pthread_mutex_unlock(&func_mutex); \
131 } while(0)
132
133 #define LOAD_FOPEN_FUNC() \
134 do { \
135 pthread_mutex_lock(&func_mutex); \
136 if (!_fopen) \
137 _fopen = (FILE* (*)(const char *, const char*)) dlsym(RTLD_NEXT, "fopen"); \
138 pthread_mutex_unlock(&func_mutex); \
139 } while(0)
140
141 #define LOAD_FOPEN64_FUNC() \
142 do { \
143 pthread_mutex_lock(&func_mutex); \
144 if (!_fopen64) \
145 _fopen64 = (FILE* (*)(const char *, const char*)) dlsym(RTLD_NEXT, "fopen64"); \
146 pthread_mutex_unlock(&func_mutex); \
147 } while(0)
148
149 #define LOAD_FCLOSE_FUNC() \
150 do { \
151 pthread_mutex_lock(&func_mutex); \
152 if (!_fclose) \
153 _fclose = (int (*)(FILE *)) dlsym(RTLD_NEXT, "fclose"); \
154 pthread_mutex_unlock(&func_mutex); \
155 } while(0)
156
157 static void debug(const char *format, ...) PA_GCC_PRINTF_ATTR(1,2);
158
159 static void debug(const char *format, ...) {
160 va_list ap;
161 if (getenv("PADSP_DEBUG")) {
162 va_start(ap, format);
163 vfprintf(stderr, format, ap);
164 va_end(ap);
165 }
166 }
167
168 static pthread_key_t recursion_key;
169
170 static void recursion_key_alloc(void) {
171 pthread_key_create(&recursion_key, NULL);
172 }
173
174 static int function_enter(void) {
175 /* Avoid recursive calls */
176 static pthread_once_t recursion_key_once = PTHREAD_ONCE_INIT;
177 pthread_once(&recursion_key_once, recursion_key_alloc);
178
179 if (pthread_getspecific(recursion_key))
180 return 0;
181
182 pthread_setspecific(recursion_key, (void*) 1);
183 return 1;
184 }
185
186 static void function_exit(void) {
187 pthread_setspecific(recursion_key, NULL);
188 }
189
190 static void fd_info_free(fd_info *i) {
191 assert(i);
192
193 debug(__FILE__": freeing fd info (fd=%i)\n", i->app_fd);
194
195 dsp_drain(i);
196
197 if (i->mainloop)
198 pa_threaded_mainloop_stop(i->mainloop);
199
200 if (i->stream) {
201 pa_stream_disconnect(i->stream);
202 pa_stream_unref(i->stream);
203 }
204
205 if (i->context) {
206 pa_context_disconnect(i->context);
207 pa_context_unref(i->context);
208 }
209
210 if (i->mainloop)
211 pa_threaded_mainloop_free(i->mainloop);
212
213 if (i->app_fd >= 0) {
214 LOAD_CLOSE_FUNC();
215 _close(i->app_fd);
216 }
217
218 if (i->thread_fd >= 0) {
219 LOAD_CLOSE_FUNC();
220 _close(i->thread_fd);
221 }
222
223 free(i->buf);
224
225 pthread_mutex_destroy(&i->mutex);
226 free(i);
227 }
228
229 static fd_info *fd_info_ref(fd_info *i) {
230 assert(i);
231
232 pthread_mutex_lock(&i->mutex);
233 assert(i->ref >= 1);
234 i->ref++;
235
236 /* debug(__FILE__": ref++, now %i\n", i->ref); */
237 pthread_mutex_unlock(&i->mutex);
238
239 return i;
240 }
241
242 static void fd_info_unref(fd_info *i) {
243 int r;
244 pthread_mutex_lock(&i->mutex);
245 assert(i->ref >= 1);
246 r = --i->ref;
247 /* debug(__FILE__": ref--, now %i\n", i->ref); */
248 pthread_mutex_unlock(&i->mutex);
249
250 if (r <= 0)
251 fd_info_free(i);
252 }
253
254 static void context_state_cb(pa_context *c, void *userdata) {
255 fd_info *i = userdata;
256 assert(c);
257
258 switch (pa_context_get_state(c)) {
259 case PA_CONTEXT_READY:
260 case PA_CONTEXT_TERMINATED:
261 case PA_CONTEXT_FAILED:
262 pa_threaded_mainloop_signal(i->mainloop, 0);
263 break;
264
265 case PA_CONTEXT_UNCONNECTED:
266 case PA_CONTEXT_CONNECTING:
267 case PA_CONTEXT_AUTHORIZING:
268 case PA_CONTEXT_SETTING_NAME:
269 break;
270 }
271 }
272
273 static void reset_params(fd_info *i) {
274 assert(i);
275
276 i->sample_spec.format = PA_SAMPLE_ULAW;
277 i->sample_spec.channels = 1;
278 i->sample_spec.rate = 8000;
279 i->fragment_size = 1024;
280 i->n_fragments = 0;
281 }
282
283 static char *client_name(char *buf, size_t n) {
284 char p[PATH_MAX];
285
286 if (pa_get_binary_name(p, sizeof(p)))
287 snprintf(buf, n, "oss[%s]", pa_path_get_filename(p));
288 else
289 snprintf(buf, n, "oss");
290
291 return buf;
292 }
293
294 static void atfork_prepare(void) {
295 fd_info *i;
296
297 debug(__FILE__": atfork_prepare() enter\n");
298
299 function_enter();
300
301 pthread_mutex_lock(&fd_infos_mutex);
302
303 for (i = fd_infos; i; i = i->next) {
304 pthread_mutex_lock(&i->mutex);
305 pa_threaded_mainloop_lock(i->mainloop);
306 }
307
308 pthread_mutex_lock(&func_mutex);
309
310
311 debug(__FILE__": atfork_prepare() exit\n");
312 }
313
314 static void atfork_parent(void) {
315 fd_info *i;
316
317 debug(__FILE__": atfork_parent() enter\n");
318
319 pthread_mutex_unlock(&func_mutex);
320
321 for (i = fd_infos; i; i = i->next) {
322 pa_threaded_mainloop_unlock(i->mainloop);
323 pthread_mutex_unlock(&i->mutex);
324 }
325
326 pthread_mutex_unlock(&fd_infos_mutex);
327
328 function_exit();
329
330 debug(__FILE__": atfork_parent() exit\n");
331 }
332
333 static void atfork_child(void) {
334 fd_info *i;
335
336 debug(__FILE__": atfork_child() enter\n");
337
338 /* We do only the bare minimum to get all fds closed */
339 pthread_mutex_init(&func_mutex, NULL);
340 pthread_mutex_init(&fd_infos_mutex, NULL);
341
342 for (i = fd_infos; i; i = i->next) {
343 pthread_mutex_init(&i->mutex, NULL);
344
345 if (i->context) {
346 pa_context_disconnect(i->context);
347 pa_context_unref(i->context);
348 i->context = NULL;
349 }
350
351 if (i->stream) {
352 pa_stream_unref(i->stream);
353 i->stream = NULL;
354 }
355
356 if (i->app_fd >= 0) {
357 close(i->app_fd);
358 i->app_fd = -1;
359 }
360
361 if (i->thread_fd >= 0) {
362 close(i->thread_fd);
363 i->thread_fd = -1;
364 }
365
366 i->unusable = 1;
367 }
368
369 function_exit();
370
371 debug(__FILE__": atfork_child() exit\n");
372 }
373
374 static void install_atfork(void) {
375 pthread_atfork(atfork_prepare, atfork_parent, atfork_child);
376 }
377
378 static fd_info* fd_info_new(fd_info_type_t type, int *_errno) {
379 fd_info *i;
380 int sfds[2] = { -1, -1 };
381 char name[64];
382 static pthread_once_t install_atfork_once = PTHREAD_ONCE_INIT;
383
384 debug(__FILE__": fd_info_new()\n");
385
386 signal(SIGPIPE, SIG_IGN); /* Yes, ugly as hell */
387
388 pthread_once(&install_atfork_once, install_atfork);
389
390 if (!(i = malloc(sizeof(fd_info)))) {
391 *_errno = ENOMEM;
392 goto fail;
393 }
394
395 i->app_fd = i->thread_fd = -1;
396 i->type = type;
397
398 i->mainloop = NULL;
399 i->context = NULL;
400 i->stream = NULL;
401 i->io_event = NULL;
402 pthread_mutex_init(&i->mutex, NULL);
403 i->ref = 1;
404 i->buf = NULL;
405 i->unusable = 0;
406 PA_LLIST_INIT(fd_info, i);
407
408 reset_params(i);
409
410 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sfds) < 0) {
411 *_errno = errno;
412 debug(__FILE__": socket() failed: %s\n", strerror(errno));
413 goto fail;
414 }
415
416 i->app_fd = sfds[0];
417 i->thread_fd = sfds[1];
418
419 if (!(i->mainloop = pa_threaded_mainloop_new())) {
420 *_errno = EIO;
421 debug(__FILE__": pa_threaded_mainloop_new() failed\n");
422 goto fail;
423 }
424
425 if (!(i->context = pa_context_new(pa_threaded_mainloop_get_api(i->mainloop), client_name(name, sizeof(name))))) {
426 *_errno = EIO;
427 debug(__FILE__": pa_context_new() failed\n");
428 goto fail;
429 }
430
431 pa_context_set_state_callback(i->context, context_state_cb, i);
432
433 if (pa_context_connect(i->context, NULL, 0, NULL) < 0) {
434 *_errno = ECONNREFUSED;
435 debug(__FILE__": pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
436 goto fail;
437 }
438
439 pa_threaded_mainloop_lock(i->mainloop);
440
441 if (pa_threaded_mainloop_start(i->mainloop) < 0) {
442 *_errno = EIO;
443 debug(__FILE__": pa_threaded_mainloop_start() failed\n");
444 goto unlock_and_fail;
445 }
446
447 /* Wait until the context is ready */
448 pa_threaded_mainloop_wait(i->mainloop);
449
450 if (pa_context_get_state(i->context) != PA_CONTEXT_READY) {
451 *_errno = ECONNREFUSED;
452 debug(__FILE__": pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
453 goto unlock_and_fail;
454 }
455
456 pa_threaded_mainloop_unlock(i->mainloop);
457 return i;
458
459 unlock_and_fail:
460
461 pa_threaded_mainloop_unlock(i->mainloop);
462
463 fail:
464
465 if (i)
466 fd_info_unref(i);
467
468 return NULL;
469 }
470
471 static void fd_info_add_to_list(fd_info *i) {
472 assert(i);
473
474 pthread_mutex_lock(&fd_infos_mutex);
475 PA_LLIST_PREPEND(fd_info, fd_infos, i);
476 pthread_mutex_unlock(&fd_infos_mutex);
477
478 fd_info_ref(i);
479 }
480
481 static void fd_info_remove_from_list(fd_info *i) {
482 assert(i);
483
484 pthread_mutex_lock(&fd_infos_mutex);
485 PA_LLIST_REMOVE(fd_info, fd_infos, i);
486 pthread_mutex_unlock(&fd_infos_mutex);
487
488 fd_info_unref(i);
489 }
490
491 static fd_info* fd_info_find(int fd) {
492 fd_info *i;
493
494 pthread_mutex_lock(&fd_infos_mutex);
495
496 for (i = fd_infos; i; i = i->next)
497 if (i->app_fd == fd && !i->unusable) {
498 fd_info_ref(i);
499 break;
500 }
501
502 pthread_mutex_unlock(&fd_infos_mutex);
503
504 return i;
505 }
506
507 static void fix_metrics(fd_info *i) {
508 size_t fs;
509 char t[PA_SAMPLE_SPEC_SNPRINT_MAX];
510
511 fs = pa_frame_size(&i->sample_spec);
512 i->fragment_size = (i->fragment_size/fs)*fs;
513
514 if (i->n_fragments < 2)
515 i->n_fragments = 12;
516
517 if (i->fragment_size <= 0)
518 if ((i->fragment_size = pa_bytes_per_second(&i->sample_spec) / 2 / i->n_fragments) <= 0)
519 i->fragment_size = 1024;
520
521 debug(__FILE__": sample spec: %s\n", pa_sample_spec_snprint(t, sizeof(t), &i->sample_spec));
522 debug(__FILE__": fixated metrics to %i fragments, %li bytes each.\n", i->n_fragments, (long)i->fragment_size);
523 }
524
525 static void stream_request_cb(pa_stream *s, size_t length, void *userdata) {
526 fd_info *i = userdata;
527 assert(s);
528
529 if (i->io_event) {
530 pa_mainloop_api *api;
531 api = pa_threaded_mainloop_get_api(i->mainloop);
532 api->io_enable(i->io_event, PA_IO_EVENT_INPUT);
533 }
534 }
535
536 static void stream_latency_update_cb(pa_stream *s, void *userdata) {
537 fd_info *i = userdata;
538 assert(s);
539
540 pa_threaded_mainloop_signal(i->mainloop, 0);
541 }
542
543 static void fd_info_shutdown(fd_info *i) {
544 assert(i);
545
546 if (i->io_event) {
547 pa_mainloop_api *api;
548 api = pa_threaded_mainloop_get_api(i->mainloop);
549 api->io_free(i->io_event);
550 i->io_event = NULL;
551 }
552
553 if (i->thread_fd >= 0) {
554 close(i->thread_fd);
555 i->thread_fd = -1;
556 }
557 }
558
559 static int fd_info_copy_data(fd_info *i, int force) {
560 size_t n;
561
562 if (!i->stream)
563 return -1;
564
565 if ((n = pa_stream_writable_size(i->stream)) == (size_t) -1) {
566 debug(__FILE__": pa_stream_writable_size(): %s\n", pa_strerror(pa_context_errno(i->context)));
567 return -1;
568 }
569
570 while (n >= i->fragment_size || force) {
571 ssize_t r;
572
573 if (!i->buf) {
574 if (!(i->buf = malloc(i->fragment_size))) {
575 debug(__FILE__": malloc() failed.\n");
576 return -1;
577 }
578 }
579
580 if ((r = read(i->thread_fd, i->buf, i->fragment_size)) <= 0) {
581
582 if (errno == EAGAIN)
583 break;
584
585 debug(__FILE__": read(): %s\n", r == 0 ? "EOF" : strerror(errno));
586 return -1;
587 }
588
589 if (pa_stream_write(i->stream, i->buf, r, free, 0, PA_SEEK_RELATIVE) < 0) {
590 debug(__FILE__": pa_stream_write(): %s\n", pa_strerror(pa_context_errno(i->context)));
591 return -1;
592 }
593
594 i->buf = NULL;
595
596 assert(n >= (size_t) r);
597 n -= r;
598 }
599
600 if (i->io_event) {
601 pa_mainloop_api *api;
602 api = pa_threaded_mainloop_get_api(i->mainloop);
603 api->io_enable(i->io_event, n >= i->fragment_size ? PA_IO_EVENT_INPUT : 0);
604 }
605
606 return 0;
607 }
608
609 static void stream_state_cb(pa_stream *s, void * userdata) {
610 fd_info *i = userdata;
611 assert(s);
612
613 switch (pa_stream_get_state(s)) {
614
615 case PA_STREAM_READY:
616 debug(__FILE__": stream established.\n");
617 break;
618
619 case PA_STREAM_FAILED:
620 debug(__FILE__": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
621 fd_info_shutdown(i);
622 break;
623
624 case PA_STREAM_TERMINATED:
625 case PA_STREAM_UNCONNECTED:
626 case PA_STREAM_CREATING:
627 break;
628 }
629 }
630
631 static int create_stream(fd_info *i) {
632 pa_buffer_attr attr;
633 int n;
634
635 assert(i);
636
637 fix_metrics(i);
638
639 if (!(i->stream = pa_stream_new(i->context, "Audio Stream", &i->sample_spec, NULL))) {
640 debug(__FILE__": pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
641 goto fail;
642 }
643
644 pa_stream_set_state_callback(i->stream, stream_state_cb, i);
645 pa_stream_set_write_callback(i->stream, stream_request_cb, i);
646 pa_stream_set_latency_update_callback(i->stream, stream_latency_update_cb, i);
647
648 memset(&attr, 0, sizeof(attr));
649 attr.maxlength = i->fragment_size * (i->n_fragments+1);
650 attr.tlength = i->fragment_size * i->n_fragments;
651 attr.prebuf = i->fragment_size;
652 attr.minreq = i->fragment_size;
653
654 if (pa_stream_connect_playback(i->stream, NULL, &attr, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE, NULL, NULL) < 0) {
655 debug(__FILE__": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
656 goto fail;
657 }
658
659 n = i->fragment_size;
660 setsockopt(i->app_fd, SOL_SOCKET, SO_SNDBUF, &n, sizeof(n));
661 n = i->fragment_size;
662 setsockopt(i->thread_fd, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n));
663
664 return 0;
665
666 fail:
667 return -1;
668 }
669
670 static void free_stream(fd_info *i) {
671 assert(i);
672
673 if (i->stream) {
674 pa_stream_disconnect(i->stream);
675 pa_stream_unref(i->stream);
676 i->stream = NULL;
677 }
678 }
679
680 static void io_event_cb(pa_mainloop_api *api, pa_io_event *e, int fd, pa_io_event_flags_t flags, void *userdata) {
681 fd_info *i = userdata;
682
683 pa_threaded_mainloop_signal(i->mainloop, 0);
684
685 if (flags & PA_IO_EVENT_INPUT) {
686
687 if (!i->stream) {
688 api->io_enable(e, 0);
689
690 if (create_stream(i) < 0)
691 goto fail;
692
693 } else {
694 if (fd_info_copy_data(i, 0) < 0)
695 goto fail;
696 }
697
698 } else if (flags & (PA_IO_EVENT_HANGUP|PA_IO_EVENT_ERROR))
699 goto fail;
700
701 return;
702
703 fail:
704 /* We can't do anything better than removing the event source */
705 fd_info_shutdown(i);
706 }
707
708 static int dsp_open(int flags, int *_errno) {
709 fd_info *i;
710 pa_mainloop_api *api;
711 int ret;
712 int f;
713
714 if ((flags != O_WRONLY) && (flags != (O_WRONLY|O_NONBLOCK))) {
715 *_errno = EACCES;
716 return -1;
717 }
718
719 if (!(i = fd_info_new(FD_INFO_PLAYBACK, _errno)))
720 return -1;
721
722 shutdown(i->thread_fd, SHUT_WR);
723 shutdown(i->app_fd, SHUT_RD);
724
725 if ((flags & O_NONBLOCK) == O_NONBLOCK) {
726 if ((f = fcntl(i->app_fd, F_GETFL)) >= 0)
727 fcntl(i->app_fd, F_SETFL, f|O_NONBLOCK);
728 }
729 if ((f = fcntl(i->thread_fd, F_GETFL)) >= 0)
730 fcntl(i->thread_fd, F_SETFL, f|O_NONBLOCK);
731
732 fcntl(i->app_fd, F_SETFD, FD_CLOEXEC);
733 fcntl(i->thread_fd, F_SETFD, FD_CLOEXEC);
734
735 pa_threaded_mainloop_lock(i->mainloop);
736 api = pa_threaded_mainloop_get_api(i->mainloop);
737 if (!(i->io_event = api->io_new(api, i->thread_fd, PA_IO_EVENT_INPUT, io_event_cb, i)))
738 goto fail;
739
740 pa_threaded_mainloop_unlock(i->mainloop);
741
742 debug(__FILE__": dsp_open() succeeded, fd=%i\n", i->app_fd);
743
744 fd_info_add_to_list(i);
745 ret = i->app_fd;
746 fd_info_unref(i);
747
748 return ret;
749
750 fail:
751 pa_threaded_mainloop_unlock(i->mainloop);
752
753 if (i)
754 fd_info_unref(i);
755
756 *_errno = EIO;
757
758 debug(__FILE__": dsp_open() failed\n");
759
760 return -1;
761 }
762
763 static int mixer_open(int flags, int *_errno) {
764 /* fd_info *i; */
765
766 *_errno = ENOSYS;
767 return -1;
768
769 /* if (!(i = fd_info_new(FD_INFO_MIXER))) */
770 /* return -1; */
771
772 }
773
774 int open(const char *filename, int flags, ...) {
775 va_list args;
776 mode_t mode = 0;
777 int r, _errno = 0;
778
779 va_start(args, flags);
780 if (flags & O_CREAT)
781 mode = va_arg(args, mode_t);
782 va_end(args);
783
784 if (!function_enter()) {
785 LOAD_OPEN_FUNC();
786 return _open(filename, flags, mode);
787 }
788
789 debug(__FILE__": open()\n");
790
791 if (strcmp(filename, "/dev/dsp") == 0 || strcmp(filename, "/dev/adsp") == 0) {
792 r = dsp_open(flags, &_errno);
793 } else if (strcmp(filename, "/dev/mixer") == 0) {
794 r = mixer_open(flags, &_errno);
795 } else {
796 function_exit();
797 LOAD_OPEN_FUNC();
798 return _open(filename, flags, mode);
799 }
800
801 function_exit();
802
803 if (_errno)
804 errno = _errno;
805
806 return r;
807 }
808
809 static int mixer_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno) {
810 *_errno = ENOSYS;
811 return -1;
812 }
813
814 static int map_format(int *fmt, pa_sample_spec *ss) {
815
816 switch (*fmt) {
817 case AFMT_MU_LAW:
818 ss->format = PA_SAMPLE_ULAW;
819 break;
820
821 case AFMT_A_LAW:
822 ss->format = PA_SAMPLE_ALAW;
823 break;
824
825 case AFMT_S8:
826 *fmt = AFMT_U8;
827 /* fall through */
828 case AFMT_U8:
829 ss->format = PA_SAMPLE_U8;
830 break;
831
832 case AFMT_U16_BE:
833 *fmt = AFMT_S16_BE;
834 /* fall through */
835 case AFMT_S16_BE:
836 ss->format = PA_SAMPLE_S16BE;
837 break;
838
839 case AFMT_U16_LE:
840 *fmt = AFMT_S16_LE;
841 /* fall through */
842 case AFMT_S16_LE:
843 ss->format = PA_SAMPLE_S16LE;
844 break;
845
846 default:
847 ss->format = PA_SAMPLE_S16NE;
848 *fmt = AFMT_S16_NE;
849 break;
850 }
851
852 return 0;
853 }
854
855 static int map_format_back(pa_sample_format_t format) {
856 switch (format) {
857 case PA_SAMPLE_S16LE: return AFMT_S16_LE;
858 case PA_SAMPLE_S16BE: return AFMT_S16_BE;
859 case PA_SAMPLE_ULAW: return AFMT_MU_LAW;
860 case PA_SAMPLE_ALAW: return AFMT_A_LAW;
861 case PA_SAMPLE_U8: return AFMT_U8;
862 default:
863 abort();
864 }
865 }
866
867 static void success_cb(pa_stream *s, int success, void *userdata) {
868 fd_info *i = userdata;
869
870 assert(s);
871 assert(i);
872
873 i->operation_success = success;
874 pa_threaded_mainloop_signal(i->mainloop, 0);
875 }
876
877 static int dsp_flush_socket(fd_info *i) {
878 int l;
879
880 if (i->thread_fd < 0)
881 return -1;
882
883 if (ioctl(i->thread_fd, SIOCINQ, &l) < 0) {
884 debug(__FILE__": SIOCINQ: %s\n", strerror(errno));
885 return -1;
886 }
887
888 while (l > 0) {
889 char buf[1024];
890 size_t k;
891
892 k = (size_t) l > sizeof(buf) ? sizeof(buf) : (size_t) l;
893 if (read(i->thread_fd, buf, k) < 0)
894 debug(__FILE__": read(): %s\n", strerror(errno));
895 l -= k;
896 }
897
898 return 0;
899 }
900
901 static int dsp_empty_socket(fd_info *i) {
902 int ret = -1;
903
904 /* Empty the socket */
905 for (;;) {
906 int l;
907
908 if (i->thread_fd < 0 || !i->stream)
909 break;
910
911 if (ioctl(i->thread_fd, SIOCINQ, &l) < 0) {
912 debug(__FILE__": SIOCINQ: %s\n", strerror(errno));
913 break;
914 }
915
916 if (!l)
917 break;
918
919 pa_threaded_mainloop_wait(i->mainloop);
920 }
921
922 return ret;
923 }
924
925 static int dsp_drain(fd_info *i) {
926 pa_operation *o = NULL;
927 int r = -1;
928
929 if (!i->mainloop)
930 return 0;
931
932 debug(__FILE__": Draining.\n");
933
934 pa_threaded_mainloop_lock(i->mainloop);
935
936 if (dsp_empty_socket(i) < 0)
937 goto fail;
938
939 if (!i->stream)
940 goto fail;
941
942 debug(__FILE__": Really draining.\n");
943
944 if (!(o = pa_stream_drain(i->stream, success_cb, i))) {
945 debug(__FILE__": pa_stream_drain(): %s\n", pa_strerror(pa_context_errno(i->context)));
946 goto fail;
947 }
948
949 i->operation_success = 0;
950 while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
951 if (!i->stream || pa_stream_get_state(i->stream) != PA_STREAM_READY)
952 goto fail;
953
954 pa_threaded_mainloop_wait(i->mainloop);
955 }
956
957 if (!i->operation_success) {
958 debug(__FILE__": pa_stream_drain() 2: %s\n", pa_strerror(pa_context_errno(i->context)));
959 goto fail;
960 }
961
962 r = 0;
963
964 fail:
965
966 if (o)
967 pa_operation_unref(o);
968
969 pa_threaded_mainloop_unlock(i->mainloop);
970
971 return 0;
972 }
973
974 static int dsp_trigger(fd_info *i) {
975 pa_operation *o = NULL;
976 int r = -1;
977
978 if (!i->stream)
979 return 0;
980
981 pa_threaded_mainloop_lock(i->mainloop);
982
983 if (dsp_empty_socket(i) < 0)
984 goto fail;
985
986 debug(__FILE__": Triggering.\n");
987
988 if (!(o = pa_stream_trigger(i->stream, success_cb, i))) {
989 debug(__FILE__": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i->context)));
990 goto fail;
991 }
992
993 i->operation_success = 0;
994 while (!pa_operation_get_state(o) != PA_OPERATION_DONE) {
995 if (!i->stream || pa_stream_get_state(i->stream) != PA_STREAM_READY)
996 goto fail;
997
998 pa_threaded_mainloop_wait(i->mainloop);
999 }
1000
1001 if (!i->operation_success) {
1002 debug(__FILE__": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i->context)));
1003 goto fail;
1004 }
1005
1006 r = 0;
1007
1008 fail:
1009
1010 if (o)
1011 pa_operation_unref(o);
1012
1013 pa_threaded_mainloop_unlock(i->mainloop);
1014
1015 return 0;
1016 }
1017
1018 static int dsp_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno) {
1019 int ret = -1;
1020
1021 switch (request) {
1022 case SNDCTL_DSP_SETFMT: {
1023 debug(__FILE__": SNDCTL_DSP_SETFMT: %i\n", *(int*) argp);
1024
1025 pa_threaded_mainloop_lock(i->mainloop);
1026
1027 if (*(int*) argp == AFMT_QUERY)
1028 *(int*) argp = map_format_back(i->sample_spec.format);
1029 else {
1030 map_format((int*) argp, &i->sample_spec);
1031 free_stream(i);
1032 }
1033
1034 pa_threaded_mainloop_unlock(i->mainloop);
1035 break;
1036 }
1037
1038 case SNDCTL_DSP_SPEED: {
1039 pa_sample_spec ss;
1040 int valid;
1041 char t[256];
1042
1043 debug(__FILE__": SNDCTL_DSP_SPEED: %i\n", *(int*) argp);
1044
1045 pa_threaded_mainloop_lock(i->mainloop);
1046
1047 ss = i->sample_spec;
1048 ss.rate = *(int*) argp;
1049
1050 if ((valid = pa_sample_spec_valid(&ss))) {
1051 i->sample_spec = ss;
1052 free_stream(i);
1053 }
1054
1055 debug(__FILE__": ss: %s\n", pa_sample_spec_snprint(t, sizeof(t), &i->sample_spec));
1056
1057 pa_threaded_mainloop_unlock(i->mainloop);
1058
1059 if (!valid) {
1060 *_errno = EINVAL;
1061 goto fail;
1062 }
1063
1064 break;
1065 }
1066
1067 case SNDCTL_DSP_STEREO:
1068 debug(__FILE__": SNDCTL_DSP_STEREO: %i\n", *(int*) argp);
1069
1070 pa_threaded_mainloop_lock(i->mainloop);
1071
1072 i->sample_spec.channels = *(int*) argp ? 2 : 1;
1073 free_stream(i);
1074
1075 pa_threaded_mainloop_unlock(i->mainloop);
1076 return 0;
1077
1078 case SNDCTL_DSP_CHANNELS: {
1079 pa_sample_spec ss;
1080 int valid;
1081
1082 debug(__FILE__": SNDCTL_DSP_CHANNELS: %i\n", *(int*) argp);
1083
1084 pa_threaded_mainloop_lock(i->mainloop);
1085
1086 ss = i->sample_spec;
1087 ss.channels = *(int*) argp;
1088
1089 if ((valid = pa_sample_spec_valid(&ss))) {
1090 i->sample_spec = ss;
1091 free_stream(i);
1092 }
1093
1094 pa_threaded_mainloop_unlock(i->mainloop);
1095
1096 if (!valid) {
1097 *_errno = EINVAL;
1098 goto fail;
1099 }
1100
1101 break;
1102 }
1103
1104 case SNDCTL_DSP_GETBLKSIZE:
1105 debug(__FILE__": SNDCTL_DSP_GETBLKSIZE\n");
1106
1107 pa_threaded_mainloop_lock(i->mainloop);
1108
1109 fix_metrics(i);
1110 *(int*) argp = i->fragment_size;
1111
1112 pa_threaded_mainloop_unlock(i->mainloop);
1113
1114 break;
1115
1116 case SNDCTL_DSP_SETFRAGMENT:
1117 debug(__FILE__": SNDCTL_DSP_SETFRAGMENT: 0x%8x\n", *(int*) argp);
1118
1119 pa_threaded_mainloop_lock(i->mainloop);
1120
1121 i->fragment_size = 1 << (*(int*) argp);
1122 i->n_fragments = (*(int*) argp) >> 16;
1123
1124 free_stream(i);
1125
1126 pa_threaded_mainloop_unlock(i->mainloop);
1127
1128 break;
1129
1130 case SNDCTL_DSP_GETCAPS:
1131 debug(__FILE__": SNDCTL_DSP_CAPS\n");
1132
1133 *(int*) argp = DSP_CAP_MULTI;
1134 break;
1135
1136 case SNDCTL_DSP_GETODELAY: {
1137 int l;
1138
1139 debug(__FILE__": SNDCTL_DSP_GETODELAY\n");
1140
1141 pa_threaded_mainloop_lock(i->mainloop);
1142
1143 *(int*) argp = 0;
1144
1145 for (;;) {
1146 pa_usec_t usec;
1147 if (!i->stream || pa_stream_get_state(i->stream) != PA_STREAM_READY)
1148 break;
1149
1150 if (pa_stream_get_latency(i->stream, &usec, NULL) >= 0) {
1151 *(int*) argp = pa_usec_to_bytes(usec, &i->sample_spec);
1152 break;
1153 }
1154
1155 if (pa_context_errno(i->context) != PA_ERR_NODATA) {
1156 debug(__FILE__": pa_stream_get_latency(): %s\n", pa_strerror(pa_context_errno(i->context)));
1157 break;
1158 }
1159
1160 pa_threaded_mainloop_wait(i->mainloop);
1161 }
1162
1163 if (ioctl(i->thread_fd, SIOCINQ, &l) < 0)
1164 debug(__FILE__": SIOCINQ failed: %s\n", strerror(errno));
1165 else
1166 *(int*) argp += l;
1167
1168 pa_threaded_mainloop_unlock(i->mainloop);
1169
1170 debug(__FILE__": ODELAY: %i\n", *(int*) argp);
1171
1172 break;
1173 }
1174
1175 case SNDCTL_DSP_RESET: {
1176 debug(__FILE__": SNDCTL_DSP_RESET\n");
1177
1178 pa_threaded_mainloop_lock(i->mainloop);
1179
1180 free_stream(i);
1181 dsp_flush_socket(i);
1182 reset_params(i);
1183
1184 pa_threaded_mainloop_unlock(i->mainloop);
1185 break;
1186 }
1187
1188 case SNDCTL_DSP_GETFMTS: {
1189 debug(__FILE__": SNDCTL_DSP_GETFMTS\n");
1190
1191 *(int*) argp = AFMT_MU_LAW|AFMT_A_LAW|AFMT_U8|AFMT_S16_LE|AFMT_S16_BE;
1192 break;
1193 }
1194
1195 case SNDCTL_DSP_POST:
1196 debug(__FILE__": SNDCTL_DSP_POST\n");
1197
1198 if (dsp_trigger(i) < 0)
1199 *_errno = EIO;
1200 break;
1201
1202 case SNDCTL_DSP_SYNC:
1203 debug(__FILE__": SNDCTL_DSP_SYNC\n");
1204
1205 if (dsp_drain(i) < 0)
1206 *_errno = EIO;
1207
1208 break;
1209
1210 case SNDCTL_DSP_GETOSPACE: {
1211 audio_buf_info *bi = (audio_buf_info*) argp;
1212 int l;
1213 size_t k = 0;
1214
1215 debug(__FILE__": SNDCTL_DSP_GETOSPACE\n");
1216
1217 pa_threaded_mainloop_lock(i->mainloop);
1218
1219 fix_metrics(i);
1220
1221 if (i->stream) {
1222 if ((k = pa_stream_writable_size(i->stream)) == (size_t) -1)
1223 debug(__FILE__": pa_stream_writable_size(): %s\n", pa_strerror(pa_context_errno(i->context)));
1224 } else
1225 k = i->fragment_size * i->n_fragments;
1226
1227 if (ioctl(i->thread_fd, SIOCINQ, &l) < 0) {
1228 debug(__FILE__": SIOCINQ failed: %s\n", strerror(errno));
1229 l = 0;
1230 }
1231
1232 bi->fragsize = i->fragment_size;
1233 bi->fragstotal = i->n_fragments;
1234 bi->bytes = k > (size_t) l ? k - l : 0;
1235 bi->fragments = bi->bytes / bi->fragsize;
1236
1237 pa_threaded_mainloop_unlock(i->mainloop);
1238
1239 debug(__FILE__": fragsize=%i, fragstotal=%i, bytes=%i, fragments=%i\n", bi->fragsize, bi->fragstotal, bi->bytes, bi->fragments);
1240
1241 break;
1242 }
1243
1244 default:
1245 debug(__FILE__": unknwon ioctl 0x%08lx\n", request);
1246
1247 *_errno = EINVAL;
1248 goto fail;
1249 }
1250
1251 ret = 0;
1252
1253 fail:
1254
1255 return ret;
1256 }
1257
1258 int ioctl(int fd, unsigned long request, ...) {
1259 fd_info *i;
1260 va_list args;
1261 void *argp;
1262 int r, _errno = 0;
1263
1264 debug(__FILE__": ioctl()\n");
1265
1266 va_start(args, request);
1267 argp = va_arg(args, void *);
1268 va_end(args);
1269
1270 if (!function_enter()) {
1271 LOAD_IOCTL_FUNC();
1272 return _ioctl(fd, request, argp);
1273 }
1274
1275 if (!(i = fd_info_find(fd))) {
1276 function_exit();
1277 LOAD_IOCTL_FUNC();
1278 return _ioctl(fd, request, argp);
1279 }
1280
1281 if (i->type == FD_INFO_MIXER)
1282 r = mixer_ioctl(i, request, argp, &_errno);
1283 else
1284 r = dsp_ioctl(i, request, argp, &_errno);
1285
1286 fd_info_unref(i);
1287
1288 if (_errno)
1289 errno = _errno;
1290
1291 function_exit();
1292
1293 return r;
1294 }
1295
1296 int close(int fd) {
1297 fd_info *i;
1298
1299 debug(__FILE__": close()\n");
1300
1301 if (!function_enter()) {
1302 LOAD_CLOSE_FUNC();
1303 return _close(fd);
1304 }
1305
1306 if (!(i = fd_info_find(fd))) {
1307 function_exit();
1308 LOAD_CLOSE_FUNC();
1309 return _close(fd);
1310 }
1311
1312 fd_info_remove_from_list(i);
1313 fd_info_unref(i);
1314
1315 function_exit();
1316
1317 return 0;
1318 }
1319
1320 int open64(const char *filename, int flags, ...) {
1321 va_list args;
1322 mode_t mode = 0;
1323
1324 debug(__FILE__": open64()\n");
1325
1326 va_start(args, flags);
1327 if (flags & O_CREAT)
1328 mode = va_arg(args, mode_t);
1329 va_end(args);
1330
1331 if (strcmp(filename, "/dev/dsp") != 0 &&
1332 strcmp(filename, "/dev/adsp") != 0 &&
1333 strcmp(filename, "/dev/mixer") != 0) {
1334 LOAD_OPEN64_FUNC();
1335 return _open64(filename, flags, mode);
1336 }
1337
1338 return open(filename, flags, mode);
1339 }
1340
1341 FILE* fopen(const char *filename, const char *mode) {
1342 FILE *f = NULL;
1343 int fd;
1344
1345 debug(__FILE__": fopen()\n");
1346
1347 if (strcmp(filename, "/dev/dsp") != 0 &&
1348 strcmp(filename, "/dev/adsp") != 0 &&
1349 strcmp(filename, "/dev/mixer") != 0) {
1350 LOAD_FOPEN_FUNC();
1351 return _fopen(filename, mode);
1352 }
1353
1354 if (strcmp(mode, "wb") != 0) {
1355 errno = EACCES;
1356 return NULL;
1357 }
1358
1359 if ((fd = open(filename, O_WRONLY)) < 0)
1360 return NULL;
1361
1362 if (!(f = fdopen(fd, "wb"))) {
1363 close(fd);
1364 return NULL;
1365 }
1366
1367 return f;
1368 }
1369
1370 FILE *fopen64(const char *filename, const char *mode) {
1371
1372 debug(__FILE__": fopen64()\n");
1373
1374 if (strcmp(filename, "/dev/dsp") != 0 &&
1375 strcmp(filename, "/dev/adsp") != 0 &&
1376 strcmp(filename, "/dev/mixer") != 0) {
1377 LOAD_FOPEN64_FUNC();
1378 return _fopen64(filename, mode);
1379 }
1380
1381 return fopen(filename, mode);
1382 }
1383
1384 int fclose(FILE *f) {
1385 fd_info *i;
1386
1387 debug(__FILE__": fclose()\n");
1388
1389 if (!function_enter()) {
1390 LOAD_FCLOSE_FUNC();
1391 return _fclose(f);
1392 }
1393
1394 if (!(i = fd_info_find(fileno(f)))) {
1395 function_exit();
1396 LOAD_FCLOSE_FUNC();
1397 return _fclose(f);
1398 }
1399
1400 fd_info_remove_from_list(i);
1401
1402 /* Dirty trick to avoid that the fd is not freed twice, once by us
1403 * and once by the real fclose() */
1404 i->app_fd = -1;
1405
1406 fd_info_unref(i);
1407
1408 function_exit();
1409
1410 LOAD_FCLOSE_FUNC();
1411 return _fclose(f);
1412 }