]> code.delx.au - pulseaudio/blob - src/utils/padsp.c
fix playback of small sound files
[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 static int sndstat_open(int flags, int *_errno) {
775 static const char sndstat[] =
776 "Sound Driver:3.8.1a-980706 (Polypaudio Virtual OSS)\n"
777 "Kernel: POSIX\n"
778 "Config options: 0\n"
779 "\n"
780 "Installed drivers:\n"
781 "Type 255: Polypaudio Virtual OSS\n"
782 "\n"
783 "Card config:\n"
784 "Polypaudio Virtual OSS\n"
785 "\n"
786 "Audio devices:\n"
787 "0: Polypaudio Virtual OSS\n"
788 "\n"
789 "Synth devices: NOT ENABLED IN CONFIG\n"
790 "\n"
791 "Midi devices:\n"
792 "\n"
793 "Timers:\n"
794 "\n"
795 "\Mixers:\n"
796 "0: Polypaudio Virtual OSS\n";
797
798 char fn[] = "/tmp/padsp-sndstat-XXXXXX";
799 mode_t u;
800 int fd = -1;
801 int e;
802
803 debug(__FILE__": sndstat_open()\n");
804
805 if (flags != O_RDONLY && flags != (O_RDONLY|O_LARGEFILE)) {
806 *_errno = EACCES;
807 debug(__FILE__": bad access!\n");
808 goto fail;
809 }
810
811 u = umask(0077);
812 fd = mkstemp(fn);
813 e = errno;
814 umask(u);
815
816 if (fd < 0) {
817 *_errno = e;
818 debug(__FILE__": mkstemp() failed: %s\n", strerror(errno));
819 goto fail;
820 }
821
822 unlink(fn);
823
824 if (write(fd, sndstat, sizeof(sndstat) -1) != sizeof(sndstat)-1) {
825 *_errno = errno;
826 debug(__FILE__": write() failed: %s\n", strerror(errno));
827 goto fail;
828 }
829
830 if (lseek(fd, SEEK_SET, 0) < 0) {
831 *_errno = errno;
832 debug(__FILE__": lseek() failed: %s\n", strerror(errno));
833 goto fail;
834 }
835
836 return fd;
837
838 fail:
839 if (fd >= 0)
840 close(fd);
841 return -1;
842 }
843
844 int open(const char *filename, int flags, ...) {
845 va_list args;
846 mode_t mode = 0;
847 int r, _errno = 0;
848
849 debug(__FILE__": open(%s)\n", filename);
850
851 va_start(args, flags);
852 if (flags & O_CREAT)
853 mode = va_arg(args, mode_t);
854 va_end(args);
855
856 if (!function_enter()) {
857 LOAD_OPEN_FUNC();
858 return _open(filename, flags, mode);
859 }
860
861 if (strcmp(filename, "/dev/dsp") == 0 || strcmp(filename, "/dev/adsp") == 0) {
862 r = dsp_open(flags, &_errno);
863 } else if (strcmp(filename, "/dev/mixer") == 0) {
864 r = mixer_open(flags, &_errno);
865 } else if (strcmp(filename, "/dev/sndstat") == 0) {
866 r = sndstat_open(flags, &_errno);
867 } else {
868 function_exit();
869 LOAD_OPEN_FUNC();
870 return _open(filename, flags, mode);
871 }
872
873 function_exit();
874
875 if (_errno)
876 errno = _errno;
877
878 return r;
879 }
880
881 static int mixer_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno) {
882 *_errno = ENOSYS;
883 return -1;
884 }
885
886 static int map_format(int *fmt, pa_sample_spec *ss) {
887
888 switch (*fmt) {
889 case AFMT_MU_LAW:
890 ss->format = PA_SAMPLE_ULAW;
891 break;
892
893 case AFMT_A_LAW:
894 ss->format = PA_SAMPLE_ALAW;
895 break;
896
897 case AFMT_S8:
898 *fmt = AFMT_U8;
899 /* fall through */
900 case AFMT_U8:
901 ss->format = PA_SAMPLE_U8;
902 break;
903
904 case AFMT_U16_BE:
905 *fmt = AFMT_S16_BE;
906 /* fall through */
907 case AFMT_S16_BE:
908 ss->format = PA_SAMPLE_S16BE;
909 break;
910
911 case AFMT_U16_LE:
912 *fmt = AFMT_S16_LE;
913 /* fall through */
914 case AFMT_S16_LE:
915 ss->format = PA_SAMPLE_S16LE;
916 break;
917
918 default:
919 ss->format = PA_SAMPLE_S16NE;
920 *fmt = AFMT_S16_NE;
921 break;
922 }
923
924 return 0;
925 }
926
927 static int map_format_back(pa_sample_format_t format) {
928 switch (format) {
929 case PA_SAMPLE_S16LE: return AFMT_S16_LE;
930 case PA_SAMPLE_S16BE: return AFMT_S16_BE;
931 case PA_SAMPLE_ULAW: return AFMT_MU_LAW;
932 case PA_SAMPLE_ALAW: return AFMT_A_LAW;
933 case PA_SAMPLE_U8: return AFMT_U8;
934 default:
935 abort();
936 }
937 }
938
939 static void success_cb(pa_stream *s, int success, void *userdata) {
940 fd_info *i = userdata;
941
942 assert(s);
943 assert(i);
944
945 i->operation_success = success;
946 pa_threaded_mainloop_signal(i->mainloop, 0);
947 }
948
949 static int dsp_flush_socket(fd_info *i) {
950 int l;
951
952 if (i->thread_fd < 0)
953 return -1;
954
955 if (ioctl(i->thread_fd, SIOCINQ, &l) < 0) {
956 debug(__FILE__": SIOCINQ: %s\n", strerror(errno));
957 return -1;
958 }
959
960 while (l > 0) {
961 char buf[1024];
962 size_t k;
963
964 k = (size_t) l > sizeof(buf) ? sizeof(buf) : (size_t) l;
965 if (read(i->thread_fd, buf, k) < 0)
966 debug(__FILE__": read(): %s\n", strerror(errno));
967 l -= k;
968 }
969
970 return 0;
971 }
972
973 static int dsp_empty_socket(fd_info *i) {
974 int ret = -1;
975
976 /* Empty the socket */
977 for (;;) {
978 int l;
979
980 if (i->thread_fd < 0)
981 break;
982
983 if (ioctl(i->thread_fd, SIOCINQ, &l) < 0) {
984 debug(__FILE__": SIOCINQ: %s\n", strerror(errno));
985 break;
986 }
987
988 if (!l) {
989 ret = 0;
990 break;
991 }
992
993 pa_threaded_mainloop_wait(i->mainloop);
994 }
995
996 return ret;
997 }
998
999 static int dsp_drain(fd_info *i) {
1000 pa_operation *o = NULL;
1001 int r = -1;
1002
1003 if (!i->mainloop)
1004 return 0;
1005
1006 debug(__FILE__": Draining.\n");
1007
1008 pa_threaded_mainloop_lock(i->mainloop);
1009
1010 if (dsp_empty_socket(i) < 0)
1011 goto fail;
1012
1013 if (!i->stream)
1014 goto fail;
1015
1016 debug(__FILE__": Really draining.\n");
1017
1018 if (!(o = pa_stream_drain(i->stream, success_cb, i))) {
1019 debug(__FILE__": pa_stream_drain(): %s\n", pa_strerror(pa_context_errno(i->context)));
1020 goto fail;
1021 }
1022
1023 i->operation_success = 0;
1024 while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
1025 if (!i->stream || pa_stream_get_state(i->stream) != PA_STREAM_READY)
1026 goto fail;
1027
1028 pa_threaded_mainloop_wait(i->mainloop);
1029 }
1030
1031 if (!i->operation_success) {
1032 debug(__FILE__": pa_stream_drain() 2: %s\n", pa_strerror(pa_context_errno(i->context)));
1033 goto fail;
1034 }
1035
1036 r = 0;
1037
1038 fail:
1039
1040 if (o)
1041 pa_operation_unref(o);
1042
1043 pa_threaded_mainloop_unlock(i->mainloop);
1044
1045 return 0;
1046 }
1047
1048 static int dsp_trigger(fd_info *i) {
1049 pa_operation *o = NULL;
1050 int r = -1;
1051
1052 if (!i->stream)
1053 return 0;
1054
1055 pa_threaded_mainloop_lock(i->mainloop);
1056
1057 if (dsp_empty_socket(i) < 0)
1058 goto fail;
1059
1060 debug(__FILE__": Triggering.\n");
1061
1062 if (!(o = pa_stream_trigger(i->stream, success_cb, i))) {
1063 debug(__FILE__": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i->context)));
1064 goto fail;
1065 }
1066
1067 i->operation_success = 0;
1068 while (!pa_operation_get_state(o) != PA_OPERATION_DONE) {
1069 if (!i->stream || pa_stream_get_state(i->stream) != PA_STREAM_READY)
1070 goto fail;
1071
1072 pa_threaded_mainloop_wait(i->mainloop);
1073 }
1074
1075 if (!i->operation_success) {
1076 debug(__FILE__": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i->context)));
1077 goto fail;
1078 }
1079
1080 r = 0;
1081
1082 fail:
1083
1084 if (o)
1085 pa_operation_unref(o);
1086
1087 pa_threaded_mainloop_unlock(i->mainloop);
1088
1089 return 0;
1090 }
1091
1092 static int dsp_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno) {
1093 int ret = -1;
1094
1095 switch (request) {
1096 case SNDCTL_DSP_SETFMT: {
1097 debug(__FILE__": SNDCTL_DSP_SETFMT: %i\n", *(int*) argp);
1098
1099 pa_threaded_mainloop_lock(i->mainloop);
1100
1101 if (*(int*) argp == AFMT_QUERY)
1102 *(int*) argp = map_format_back(i->sample_spec.format);
1103 else {
1104 map_format((int*) argp, &i->sample_spec);
1105 free_stream(i);
1106 }
1107
1108 pa_threaded_mainloop_unlock(i->mainloop);
1109 break;
1110 }
1111
1112 case SNDCTL_DSP_SPEED: {
1113 pa_sample_spec ss;
1114 int valid;
1115 char t[256];
1116
1117 debug(__FILE__": SNDCTL_DSP_SPEED: %i\n", *(int*) argp);
1118
1119 pa_threaded_mainloop_lock(i->mainloop);
1120
1121 ss = i->sample_spec;
1122 ss.rate = *(int*) argp;
1123
1124 if ((valid = pa_sample_spec_valid(&ss))) {
1125 i->sample_spec = ss;
1126 free_stream(i);
1127 }
1128
1129 debug(__FILE__": ss: %s\n", pa_sample_spec_snprint(t, sizeof(t), &i->sample_spec));
1130
1131 pa_threaded_mainloop_unlock(i->mainloop);
1132
1133 if (!valid) {
1134 *_errno = EINVAL;
1135 goto fail;
1136 }
1137
1138 break;
1139 }
1140
1141 case SNDCTL_DSP_STEREO:
1142 debug(__FILE__": SNDCTL_DSP_STEREO: %i\n", *(int*) argp);
1143
1144 pa_threaded_mainloop_lock(i->mainloop);
1145
1146 i->sample_spec.channels = *(int*) argp ? 2 : 1;
1147 free_stream(i);
1148
1149 pa_threaded_mainloop_unlock(i->mainloop);
1150 return 0;
1151
1152 case SNDCTL_DSP_CHANNELS: {
1153 pa_sample_spec ss;
1154 int valid;
1155
1156 debug(__FILE__": SNDCTL_DSP_CHANNELS: %i\n", *(int*) argp);
1157
1158 pa_threaded_mainloop_lock(i->mainloop);
1159
1160 ss = i->sample_spec;
1161 ss.channels = *(int*) argp;
1162
1163 if ((valid = pa_sample_spec_valid(&ss))) {
1164 i->sample_spec = ss;
1165 free_stream(i);
1166 }
1167
1168 pa_threaded_mainloop_unlock(i->mainloop);
1169
1170 if (!valid) {
1171 *_errno = EINVAL;
1172 goto fail;
1173 }
1174
1175 break;
1176 }
1177
1178 case SNDCTL_DSP_GETBLKSIZE:
1179 debug(__FILE__": SNDCTL_DSP_GETBLKSIZE\n");
1180
1181 pa_threaded_mainloop_lock(i->mainloop);
1182
1183 fix_metrics(i);
1184 *(int*) argp = i->fragment_size;
1185
1186 pa_threaded_mainloop_unlock(i->mainloop);
1187
1188 break;
1189
1190 case SNDCTL_DSP_SETFRAGMENT:
1191 debug(__FILE__": SNDCTL_DSP_SETFRAGMENT: 0x%8x\n", *(int*) argp);
1192
1193 pa_threaded_mainloop_lock(i->mainloop);
1194
1195 i->fragment_size = 1 << (*(int*) argp);
1196 i->n_fragments = (*(int*) argp) >> 16;
1197
1198 free_stream(i);
1199
1200 pa_threaded_mainloop_unlock(i->mainloop);
1201
1202 break;
1203
1204 case SNDCTL_DSP_GETCAPS:
1205 debug(__FILE__": SNDCTL_DSP_CAPS\n");
1206
1207 *(int*) argp = DSP_CAP_MULTI;
1208 break;
1209
1210 case SNDCTL_DSP_GETODELAY: {
1211 int l;
1212
1213 debug(__FILE__": SNDCTL_DSP_GETODELAY\n");
1214
1215 pa_threaded_mainloop_lock(i->mainloop);
1216
1217 *(int*) argp = 0;
1218
1219 for (;;) {
1220 pa_usec_t usec;
1221 if (!i->stream || pa_stream_get_state(i->stream) != PA_STREAM_READY)
1222 break;
1223
1224 if (pa_stream_get_latency(i->stream, &usec, NULL) >= 0) {
1225 *(int*) argp = pa_usec_to_bytes(usec, &i->sample_spec);
1226 break;
1227 }
1228
1229 if (pa_context_errno(i->context) != PA_ERR_NODATA) {
1230 debug(__FILE__": pa_stream_get_latency(): %s\n", pa_strerror(pa_context_errno(i->context)));
1231 break;
1232 }
1233
1234 pa_threaded_mainloop_wait(i->mainloop);
1235 }
1236
1237 if (ioctl(i->thread_fd, SIOCINQ, &l) < 0)
1238 debug(__FILE__": SIOCINQ failed: %s\n", strerror(errno));
1239 else
1240 *(int*) argp += l;
1241
1242 pa_threaded_mainloop_unlock(i->mainloop);
1243
1244 debug(__FILE__": ODELAY: %i\n", *(int*) argp);
1245
1246 break;
1247 }
1248
1249 case SNDCTL_DSP_RESET: {
1250 debug(__FILE__": SNDCTL_DSP_RESET\n");
1251
1252 pa_threaded_mainloop_lock(i->mainloop);
1253
1254 free_stream(i);
1255 dsp_flush_socket(i);
1256 reset_params(i);
1257
1258 pa_threaded_mainloop_unlock(i->mainloop);
1259 break;
1260 }
1261
1262 case SNDCTL_DSP_GETFMTS: {
1263 debug(__FILE__": SNDCTL_DSP_GETFMTS\n");
1264
1265 *(int*) argp = AFMT_MU_LAW|AFMT_A_LAW|AFMT_U8|AFMT_S16_LE|AFMT_S16_BE;
1266 break;
1267 }
1268
1269 case SNDCTL_DSP_POST:
1270 debug(__FILE__": SNDCTL_DSP_POST\n");
1271
1272 if (dsp_trigger(i) < 0)
1273 *_errno = EIO;
1274 break;
1275
1276 case SNDCTL_DSP_SYNC:
1277 debug(__FILE__": SNDCTL_DSP_SYNC\n");
1278
1279 if (dsp_drain(i) < 0)
1280 *_errno = EIO;
1281
1282 break;
1283
1284 case SNDCTL_DSP_GETOSPACE: {
1285 audio_buf_info *bi = (audio_buf_info*) argp;
1286 int l;
1287 size_t k = 0;
1288
1289 debug(__FILE__": SNDCTL_DSP_GETOSPACE\n");
1290
1291 pa_threaded_mainloop_lock(i->mainloop);
1292
1293 fix_metrics(i);
1294
1295 if (i->stream) {
1296 if ((k = pa_stream_writable_size(i->stream)) == (size_t) -1)
1297 debug(__FILE__": pa_stream_writable_size(): %s\n", pa_strerror(pa_context_errno(i->context)));
1298 } else
1299 k = i->fragment_size * i->n_fragments;
1300
1301 if (ioctl(i->thread_fd, SIOCINQ, &l) < 0) {
1302 debug(__FILE__": SIOCINQ failed: %s\n", strerror(errno));
1303 l = 0;
1304 }
1305
1306 bi->fragsize = i->fragment_size;
1307 bi->fragstotal = i->n_fragments;
1308 bi->bytes = k > (size_t) l ? k - l : 0;
1309 bi->fragments = bi->bytes / bi->fragsize;
1310
1311 pa_threaded_mainloop_unlock(i->mainloop);
1312
1313 debug(__FILE__": fragsize=%i, fragstotal=%i, bytes=%i, fragments=%i\n", bi->fragsize, bi->fragstotal, bi->bytes, bi->fragments);
1314
1315 break;
1316 }
1317
1318 default:
1319 debug(__FILE__": unknwon ioctl 0x%08lx\n", request);
1320
1321 *_errno = EINVAL;
1322 goto fail;
1323 }
1324
1325 ret = 0;
1326
1327 fail:
1328
1329 return ret;
1330 }
1331
1332 int ioctl(int fd, unsigned long request, ...) {
1333 fd_info *i;
1334 va_list args;
1335 void *argp;
1336 int r, _errno = 0;
1337
1338 debug(__FILE__": ioctl()\n");
1339
1340 va_start(args, request);
1341 argp = va_arg(args, void *);
1342 va_end(args);
1343
1344 if (!function_enter()) {
1345 LOAD_IOCTL_FUNC();
1346 return _ioctl(fd, request, argp);
1347 }
1348
1349 if (!(i = fd_info_find(fd))) {
1350 function_exit();
1351 LOAD_IOCTL_FUNC();
1352 return _ioctl(fd, request, argp);
1353 }
1354
1355 if (i->type == FD_INFO_MIXER)
1356 r = mixer_ioctl(i, request, argp, &_errno);
1357 else
1358 r = dsp_ioctl(i, request, argp, &_errno);
1359
1360 fd_info_unref(i);
1361
1362 if (_errno)
1363 errno = _errno;
1364
1365 function_exit();
1366
1367 return r;
1368 }
1369
1370 int close(int fd) {
1371 fd_info *i;
1372
1373 debug(__FILE__": close()\n");
1374
1375 if (!function_enter()) {
1376 LOAD_CLOSE_FUNC();
1377 return _close(fd);
1378 }
1379
1380 if (!(i = fd_info_find(fd))) {
1381 function_exit();
1382 LOAD_CLOSE_FUNC();
1383 return _close(fd);
1384 }
1385
1386 fd_info_remove_from_list(i);
1387 fd_info_unref(i);
1388
1389 function_exit();
1390
1391 return 0;
1392 }
1393
1394 int open64(const char *filename, int flags, ...) {
1395 va_list args;
1396 mode_t mode = 0;
1397
1398 debug(__FILE__": open64(%s)\n", filename);
1399
1400 va_start(args, flags);
1401 if (flags & O_CREAT)
1402 mode = va_arg(args, mode_t);
1403 va_end(args);
1404
1405 if (strcmp(filename, "/dev/dsp") != 0 &&
1406 strcmp(filename, "/dev/adsp") != 0 &&
1407 strcmp(filename, "/dev/sndstat") != 0 &&
1408 strcmp(filename, "/dev/mixer") != 0) {
1409 LOAD_OPEN64_FUNC();
1410 return _open64(filename, flags, mode);
1411 }
1412
1413 return open(filename, flags, mode);
1414 }
1415
1416 FILE* fopen(const char *filename, const char *mode) {
1417 FILE *f = NULL;
1418 int fd;
1419 mode_t m;
1420
1421 debug(__FILE__": fopen(%s)\n", filename);
1422
1423 if (strcmp(filename, "/dev/dsp") == 0 ||
1424 strcmp(filename, "/dev/adsp") == 0) {
1425
1426 if (strcmp(mode, "wb") != 0) {
1427 errno = EACCES;
1428 return NULL;
1429 }
1430
1431 m = O_WRONLY;
1432 } else if (strcmp(filename, "/dev/sndstat") == 0) {
1433
1434 if (strcmp(mode, "r") != 0) {
1435 errno = EACCES;
1436 return NULL;
1437 }
1438
1439 m = O_RDONLY;
1440 } else if (strcmp(filename, "/dev/mixer") != 0)
1441 m = O_RDWR;
1442 else {
1443 LOAD_FOPEN_FUNC();
1444 return _fopen(filename, mode);
1445 }
1446
1447 if ((fd = open(filename, m)) < 0)
1448 return NULL;
1449
1450 if (!(f = fdopen(fd, mode))) {
1451 close(fd);
1452 return NULL;
1453 }
1454
1455 return f;
1456 }
1457
1458 FILE *fopen64(const char *filename, const char *mode) {
1459
1460 debug(__FILE__": fopen64(%s)\n", filename);
1461
1462 if (strcmp(filename, "/dev/dsp") != 0 &&
1463 strcmp(filename, "/dev/adsp") != 0 &&
1464 strcmp(filename, "/dev/sndstat") != 0 &&
1465 strcmp(filename, "/dev/mixer") != 0) {
1466 LOAD_FOPEN64_FUNC();
1467 return _fopen64(filename, mode);
1468 }
1469
1470 return fopen(filename, mode);
1471 }
1472
1473 int fclose(FILE *f) {
1474 fd_info *i;
1475
1476 debug(__FILE__": fclose()\n");
1477
1478 if (!function_enter()) {
1479 LOAD_FCLOSE_FUNC();
1480 return _fclose(f);
1481 }
1482
1483 if (!(i = fd_info_find(fileno(f)))) {
1484 function_exit();
1485 LOAD_FCLOSE_FUNC();
1486 return _fclose(f);
1487 }
1488
1489 fd_info_remove_from_list(i);
1490
1491 /* Dirty trick to avoid that the fd is not freed twice, once by us
1492 * and once by the real fclose() */
1493 i->app_fd = -1;
1494
1495 fd_info_unref(i);
1496
1497 function_exit();
1498
1499 LOAD_FCLOSE_FUNC();
1500 return _fclose(f);
1501 }