]> code.delx.au - pulseaudio/blob - src/modules/module-oss.c
drop the PA_SOURCE_CAN_SUSPEND and PA_SINK_CAN_SUSPEND flags, since they were a bad...
[pulseaudio] / src / modules / module-oss.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
13
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 /* General power management rules:
26 *
27 * When SUSPENDED we close the audio device.
28 *
29 * We make no difference between IDLE and RUNNING in our handling.
30 *
31 * As long as we are in RUNNING/IDLE state we will *always* write data to
32 * the device. If none is avilable from the inputs, we write silence
33 * instead.
34 *
35 * If power should be saved on IDLE module-suspend-on-idle should be used.
36 *
37 */
38
39 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 #endif
42
43 #ifdef HAVE_SYS_MMAN_H
44 #include <sys/mman.h>
45 #endif
46
47 #include <sys/soundcard.h>
48 #include <sys/ioctl.h>
49 #include <stdlib.h>
50 #include <sys/stat.h>
51 #include <stdio.h>
52 #include <errno.h>
53 #include <string.h>
54 #include <fcntl.h>
55 #include <unistd.h>
56 #include <limits.h>
57 #include <signal.h>
58
59 #include <pulse/xmalloc.h>
60 #include <pulse/util.h>
61
62 #include <pulsecore/core-error.h>
63 #include <pulsecore/thread.h>
64 #include <pulsecore/sink.h>
65 #include <pulsecore/source.h>
66 #include <pulsecore/module.h>
67 #include <pulsecore/sample-util.h>
68 #include <pulsecore/core-util.h>
69 #include <pulsecore/modargs.h>
70 #include <pulsecore/log.h>
71 #include <pulsecore/macro.h>
72 #include <pulsecore/thread-mq.h>
73 #include <pulsecore/rtpoll.h>
74
75 #include "oss-util.h"
76 #include "module-oss-symdef.h"
77
78 PA_MODULE_AUTHOR("Lennart Poettering")
79 PA_MODULE_DESCRIPTION("OSS Sink/Source")
80 PA_MODULE_VERSION(PACKAGE_VERSION)
81 PA_MODULE_USAGE(
82 "sink_name=<name for the sink> "
83 "source_name=<name for the source> "
84 "device=<OSS device> "
85 "record=<enable source?> "
86 "playback=<enable sink?> "
87 "format=<sample format> "
88 "channels=<number of channels> "
89 "rate=<sample rate> "
90 "fragments=<number of fragments> "
91 "fragment_size=<fragment size> "
92 "channel_map=<channel map> "
93 "mmap=<enable memory mapping?>")
94
95 #define DEFAULT_DEVICE "/dev/dsp"
96
97 struct userdata {
98 pa_core *core;
99 pa_module *module;
100 pa_sink *sink;
101 pa_source *source;
102
103 pa_thread *thread;
104 pa_thread_mq thread_mq;
105 pa_rtpoll *rtpoll;
106
107 char *device_name;
108
109 pa_memchunk memchunk;
110
111 size_t frame_size;
112 uint32_t in_fragment_size, out_fragment_size, in_nfrags, out_nfrags, in_hwbuf_size, out_hwbuf_size;
113 int use_getospace, use_getispace;
114 int use_getodelay;
115
116 int sink_suspended, source_suspended;
117
118 int fd;
119 int mode;
120
121 int mixer_fd;
122 int mixer_devmask;
123
124 int nfrags, frag_size;
125
126 int use_mmap;
127 unsigned out_mmap_current, in_mmap_current;
128 void *in_mmap, *out_mmap;
129 pa_memblock **in_mmap_memblocks, **out_mmap_memblocks;
130
131 int in_mmap_saved_nfrags, out_mmap_saved_nfrags;
132
133 pa_rtpoll_item *rtpoll_item;
134 };
135
136 static const char* const valid_modargs[] = {
137 "sink_name",
138 "source_name",
139 "device",
140 "record",
141 "playback",
142 "fragments",
143 "fragment_size",
144 "format",
145 "rate",
146 "channels",
147 "channel_map",
148 "mmap",
149 NULL
150 };
151
152 static void trigger(struct userdata *u, int quick) {
153 int enable_bits = 0, zero = 0;
154
155 pa_assert(u);
156
157 if (u->fd < 0)
158 return;
159
160 /* pa_log_debug("trigger"); */
161
162 if (u->source && PA_SOURCE_OPENED(u->source->thread_info.state))
163 enable_bits |= PCM_ENABLE_INPUT;
164
165 if (u->sink && PA_SINK_OPENED(u->sink->thread_info.state))
166 enable_bits |= PCM_ENABLE_OUTPUT;
167
168 if (u->use_mmap) {
169
170 if (!quick)
171 ioctl(u->fd, SNDCTL_DSP_SETTRIGGER, &zero);
172
173 #ifdef SNDCTL_DSP_HALT
174 if (enable_bits == 0)
175 if (ioctl(u->fd, SNDCTL_DSP_HALT, NULL) < 0)
176 pa_log_warn("SNDCTL_DSP_HALT: %s", pa_cstrerror(errno));
177 #endif
178
179 if (ioctl(u->fd, SNDCTL_DSP_SETTRIGGER, &enable_bits) < 0)
180 pa_log_warn("SNDCTL_DSP_SETTRIGGER: %s", pa_cstrerror(errno));
181
182 if (u->sink && !(enable_bits & PCM_ENABLE_OUTPUT)) {
183 pa_log_debug("clearing playback buffer");
184 pa_silence_memory(u->out_mmap, u->out_hwbuf_size, &u->sink->sample_spec);
185 }
186
187 } else {
188
189 if (enable_bits)
190 if (ioctl(u->fd, SNDCTL_DSP_POST, NULL) < 0)
191 pa_log_warn("SNDCTL_DSP_POST: %s", pa_cstrerror(errno));
192
193 if (!quick) {
194 /*
195 * Some crappy drivers do not start the recording until we
196 * read something. Without this snippet, poll will never
197 * register the fd as ready.
198 */
199
200 if (u->source && PA_SOURCE_OPENED(u->source->thread_info.state)) {
201 uint8_t *buf = pa_xnew(uint8_t, u->in_fragment_size);
202 pa_read(u->fd, buf, u->in_fragment_size, NULL);
203 pa_xfree(buf);
204 }
205 }
206 }
207 }
208
209 static void mmap_fill_memblocks(struct userdata *u, unsigned n) {
210 pa_assert(u);
211 pa_assert(u->out_mmap_memblocks);
212
213 /* pa_log("Mmmap writing %u blocks", n); */
214
215 while (n > 0) {
216 pa_memchunk chunk;
217
218 if (u->out_mmap_memblocks[u->out_mmap_current])
219 pa_memblock_unref_fixed(u->out_mmap_memblocks[u->out_mmap_current]);
220
221 chunk.memblock = u->out_mmap_memblocks[u->out_mmap_current] =
222 pa_memblock_new_fixed(
223 u->core->mempool,
224 (uint8_t*) u->out_mmap + u->out_fragment_size * u->out_mmap_current,
225 u->out_fragment_size,
226 1);
227
228 chunk.length = pa_memblock_get_length(chunk.memblock);
229 chunk.index = 0;
230
231 pa_sink_render_into_full(u->sink, &chunk);
232
233 u->out_mmap_current++;
234 while (u->out_mmap_current >= u->out_nfrags)
235 u->out_mmap_current -= u->out_nfrags;
236
237 n--;
238 }
239 }
240
241 static int mmap_write(struct userdata *u) {
242 struct count_info info;
243
244 pa_assert(u);
245 pa_assert(u->sink);
246
247 /* pa_log("Mmmap writing..."); */
248
249 if (ioctl(u->fd, SNDCTL_DSP_GETOPTR, &info) < 0) {
250 pa_log("SNDCTL_DSP_GETOPTR: %s", pa_cstrerror(errno));
251 return -1;
252 }
253
254 info.blocks += u->out_mmap_saved_nfrags;
255 u->out_mmap_saved_nfrags = 0;
256
257 if (info.blocks > 0)
258 mmap_fill_memblocks(u, info.blocks);
259
260 return info.blocks;
261 }
262
263 static void mmap_post_memblocks(struct userdata *u, unsigned n) {
264 pa_assert(u);
265 pa_assert(u->in_mmap_memblocks);
266
267 /* pa_log("Mmmap reading %u blocks", n); */
268
269 while (n > 0) {
270 pa_memchunk chunk;
271
272 if (!u->in_mmap_memblocks[u->in_mmap_current]) {
273
274 chunk.memblock = u->in_mmap_memblocks[u->in_mmap_current] =
275 pa_memblock_new_fixed(
276 u->core->mempool,
277 (uint8_t*) u->in_mmap + u->in_fragment_size*u->in_mmap_current,
278 u->in_fragment_size,
279 1);
280
281 chunk.length = pa_memblock_get_length(chunk.memblock);
282 chunk.index = 0;
283
284 pa_source_post(u->source, &chunk);
285 }
286
287 u->in_mmap_current++;
288 while (u->in_mmap_current >= u->in_nfrags)
289 u->in_mmap_current -= u->in_nfrags;
290
291 n--;
292 }
293 }
294
295 static void mmap_clear_memblocks(struct userdata*u, unsigned n) {
296 unsigned i = u->in_mmap_current;
297
298 pa_assert(u);
299 pa_assert(u->in_mmap_memblocks);
300
301 if (n > u->in_nfrags)
302 n = u->in_nfrags;
303
304 while (n > 0) {
305 if (u->in_mmap_memblocks[i]) {
306 pa_memblock_unref_fixed(u->in_mmap_memblocks[i]);
307 u->in_mmap_memblocks[i] = NULL;
308 }
309
310 i++;
311 while (i >= u->in_nfrags)
312 i -= u->in_nfrags;
313
314 n--;
315 }
316 }
317
318 static int mmap_read(struct userdata *u) {
319 struct count_info info;
320 pa_assert(u);
321 pa_assert(u->source);
322
323 /* pa_log("Mmmap reading..."); */
324
325 if (ioctl(u->fd, SNDCTL_DSP_GETIPTR, &info) < 0) {
326 pa_log("SNDCTL_DSP_GETIPTR: %s", pa_cstrerror(errno));
327 return -1;
328 }
329
330 info.blocks += u->in_mmap_saved_nfrags;
331 u->in_mmap_saved_nfrags = 0;
332
333 if (info.blocks > 0) {
334 mmap_post_memblocks(u, info.blocks);
335 mmap_clear_memblocks(u, u->in_nfrags/2);
336 }
337
338 return info.blocks;
339 }
340
341 static pa_usec_t mmap_sink_get_latency(struct userdata *u) {
342 struct count_info info;
343 size_t bpos, n;
344
345 pa_assert(u);
346
347 if (ioctl(u->fd, SNDCTL_DSP_GETOPTR, &info) < 0) {
348 pa_log("SNDCTL_DSP_GETOPTR: %s", pa_cstrerror(errno));
349 return 0;
350 }
351
352 u->out_mmap_saved_nfrags += info.blocks;
353
354 bpos = ((u->out_mmap_current + u->out_mmap_saved_nfrags) * u->out_fragment_size) % u->out_hwbuf_size;
355
356 if (bpos <= (size_t) info.ptr)
357 n = u->out_hwbuf_size - (info.ptr - bpos);
358 else
359 n = bpos - info.ptr;
360
361 /* pa_log("n = %u, bpos = %u, ptr = %u, total=%u, fragsize = %u, n_frags = %u\n", n, bpos, (unsigned) info.ptr, total, u->out_fragment_size, u->out_fragments); */
362
363 return pa_bytes_to_usec(n, &u->sink->sample_spec);
364 }
365
366 static pa_usec_t mmap_source_get_latency(struct userdata *u) {
367 struct count_info info;
368 size_t bpos, n;
369
370 pa_assert(u);
371
372 if (ioctl(u->fd, SNDCTL_DSP_GETIPTR, &info) < 0) {
373 pa_log("SNDCTL_DSP_GETIPTR: %s", pa_cstrerror(errno));
374 return 0;
375 }
376
377 u->in_mmap_saved_nfrags += info.blocks;
378 bpos = ((u->in_mmap_current + u->in_mmap_saved_nfrags) * u->in_fragment_size) % u->in_hwbuf_size;
379
380 if (bpos <= (size_t) info.ptr)
381 n = info.ptr - bpos;
382 else
383 n = u->in_hwbuf_size - bpos + info.ptr;
384
385 /* pa_log("n = %u, bpos = %u, ptr = %u, total=%u, fragsize = %u, n_frags = %u\n", n, bpos, (unsigned) info.ptr, total, u->in_fragment_size, u->in_fragments); */
386
387 return pa_bytes_to_usec(n, &u->source->sample_spec);
388 }
389
390 static pa_usec_t io_sink_get_latency(struct userdata *u) {
391 pa_usec_t r = 0;
392
393 pa_assert(u);
394
395 if (u->use_getodelay) {
396 int arg;
397
398 if (ioctl(u->fd, SNDCTL_DSP_GETODELAY, &arg) < 0) {
399 pa_log_info("Device doesn't support SNDCTL_DSP_GETODELAY: %s", pa_cstrerror(errno));
400 u->use_getodelay = 0;
401 } else
402 r = pa_bytes_to_usec(arg, &u->sink->sample_spec);
403
404 }
405
406 if (!u->use_getodelay && u->use_getospace) {
407 struct audio_buf_info info;
408
409 if (ioctl(u->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
410 pa_log_info("Device doesn't support SNDCTL_DSP_GETOSPACE: %s", pa_cstrerror(errno));
411 u->use_getospace = 0;
412 } else
413 r = pa_bytes_to_usec(info.bytes, &u->sink->sample_spec);
414 }
415
416 if (u->memchunk.memblock)
417 r += pa_bytes_to_usec(u->memchunk.length, &u->sink->sample_spec);
418
419 return r;
420 }
421
422
423 static pa_usec_t io_source_get_latency(struct userdata *u) {
424 pa_usec_t r = 0;
425
426 pa_assert(u);
427
428 if (u->use_getispace) {
429 struct audio_buf_info info;
430
431 if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) < 0) {
432 pa_log_info("Device doesn't support SNDCTL_DSP_GETISPACE: %s", pa_cstrerror(errno));
433 u->use_getispace = 0;
434 } else
435 r = pa_bytes_to_usec(info.bytes, &u->source->sample_spec);
436 }
437
438 return r;
439 }
440
441 static int suspend(struct userdata *u) {
442 pa_assert(u);
443 pa_assert(u->fd >= 0);
444
445 pa_log_info("Suspending...");
446
447 if (u->out_mmap_memblocks) {
448 unsigned i;
449 for (i = 0; i < u->out_nfrags; i++)
450 if (u->out_mmap_memblocks[i]) {
451 pa_memblock_unref_fixed(u->out_mmap_memblocks[i]);
452 u->out_mmap_memblocks[i] = NULL;
453 }
454 }
455
456 if (u->in_mmap_memblocks) {
457 unsigned i;
458 for (i = 0; i < u->in_nfrags; i++)
459 if (u->in_mmap_memblocks[i]) {
460 pa_memblock_unref_fixed(u->in_mmap_memblocks[i]);
461 u->in_mmap_memblocks[i] = NULL;
462 }
463 }
464
465 if (u->in_mmap && u->in_mmap != MAP_FAILED) {
466 munmap(u->in_mmap, u->in_hwbuf_size);
467 u->in_mmap = NULL;
468 }
469
470 if (u->out_mmap && u->out_mmap != MAP_FAILED) {
471 munmap(u->out_mmap, u->out_hwbuf_size);
472 u->out_mmap = NULL;
473 }
474
475 /* Let's suspend */
476 ioctl(u->fd, SNDCTL_DSP_SYNC, NULL);
477 pa_close(u->fd);
478 u->fd = -1;
479
480 if (u->rtpoll_item) {
481 pa_rtpoll_item_free(u->rtpoll_item);
482 u->rtpoll_item = NULL;
483 }
484
485 pa_log_info("Device suspended...");
486
487 return 0;
488 }
489
490 static int unsuspend(struct userdata *u) {
491 int m;
492 pa_sample_spec ss, *ss_original;
493 int frag_size, in_frag_size, out_frag_size;
494 int in_nfrags, out_nfrags;
495 struct audio_buf_info info;
496 struct pollfd *pollfd;
497
498 pa_assert(u);
499 pa_assert(u->fd < 0);
500
501 m = u->mode;
502
503 pa_log_info("Trying resume...");
504
505 if ((u->fd = pa_oss_open(u->device_name, &m, NULL)) < 0) {
506 pa_log_warn("Resume failed, device busy (%s)", pa_cstrerror(errno));
507 return -1;
508
509 if (m != u->mode)
510 pa_log_warn("Resume failed, couldn't open device with original access mode.");
511 goto fail;
512 }
513
514 if (u->nfrags >= 2 && u->frag_size >= 1)
515 if (pa_oss_set_fragments(u->fd, u->nfrags, u->frag_size) < 0) {
516 pa_log_warn("Resume failed, couldn't set original fragment settings.");
517 goto fail;
518 }
519
520 ss = *(ss_original = u->sink ? &u->sink->sample_spec : &u->source->sample_spec);
521 if (pa_oss_auto_format(u->fd, &ss) < 0 || !pa_sample_spec_equal(&ss, ss_original)) {
522 pa_log_warn("Resume failed, couldn't set original sample format settings.");
523 goto fail;
524 }
525
526 if (ioctl(u->fd, SNDCTL_DSP_GETBLKSIZE, &frag_size) < 0) {
527 pa_log_warn("SNDCTL_DSP_GETBLKSIZE: %s", pa_cstrerror(errno));
528 goto fail;
529 }
530
531 in_frag_size = out_frag_size = frag_size;
532 in_nfrags = out_nfrags = u->nfrags;
533
534 if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) >= 0) {
535 in_frag_size = info.fragsize;
536 in_nfrags = info.fragstotal;
537 }
538
539 if (ioctl(u->fd, SNDCTL_DSP_GETOSPACE, &info) >= 0) {
540 out_frag_size = info.fragsize;
541 out_nfrags = info.fragstotal;
542 }
543
544 if ((u->source && (in_frag_size != (int) u->in_fragment_size || in_nfrags != (int) u->in_nfrags)) ||
545 (u->sink && (out_frag_size != (int) u->out_fragment_size || out_nfrags != (int) u->out_nfrags))) {
546 pa_log_warn("Resume failed, input fragment settings don't match.");
547 goto fail;
548 }
549
550 if (u->use_mmap) {
551 if (u->source) {
552 if ((u->in_mmap = mmap(NULL, u->in_hwbuf_size, PROT_READ, MAP_SHARED, u->fd, 0)) == MAP_FAILED) {
553 pa_log("Resume failed, mmap(): %s", pa_cstrerror(errno));
554 goto fail;
555 }
556 }
557
558 if (u->sink) {
559 if ((u->out_mmap = mmap(NULL, u->out_hwbuf_size, PROT_WRITE, MAP_SHARED, u->fd, 0)) == MAP_FAILED) {
560 pa_log("Resume failed, mmap(): %s", pa_cstrerror(errno));
561 if (u->in_mmap && u->in_mmap != MAP_FAILED) {
562 munmap(u->in_mmap, u->in_hwbuf_size);
563 u->in_mmap = NULL;
564 }
565
566 goto fail;
567 }
568
569 pa_silence_memory(u->out_mmap, u->out_hwbuf_size, &ss);
570 }
571 }
572
573 u->out_mmap_current = u->in_mmap_current = 0;
574 u->out_mmap_saved_nfrags = u->in_mmap_saved_nfrags = 0;
575
576 pa_assert(!u->rtpoll_item);
577
578 u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
579 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
580 pollfd->fd = u->fd;
581 pollfd->events = 0;
582 pollfd->revents = 0;
583
584 if (u->sink)
585 pa_sink_get_volume(u->sink);
586 if (u->source)
587 pa_source_get_volume(u->source);
588
589 pa_log_info("Resumed successfully...");
590
591 return 0;
592
593 fail:
594 pa_close(u->fd);
595 u->fd = -1;
596 return -1;
597 }
598
599 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
600 struct userdata *u = PA_SINK(o)->userdata;
601 int do_trigger = 0, ret, quick = 1;
602
603 switch (code) {
604
605 case PA_SINK_MESSAGE_GET_LATENCY: {
606 pa_usec_t r = 0;
607
608 if (u->fd >= 0) {
609 if (u->use_mmap)
610 r = mmap_sink_get_latency(u);
611 else
612 r = io_sink_get_latency(u);
613 }
614
615 *((pa_usec_t*) data) = r;
616
617 return 0;
618 }
619
620 case PA_SINK_MESSAGE_SET_STATE:
621
622 switch ((pa_sink_state_t) PA_PTR_TO_UINT(data)) {
623
624 case PA_SINK_SUSPENDED:
625 pa_assert(PA_SINK_OPENED(u->sink->thread_info.state));
626
627 if (!u->source || u->source_suspended) {
628 if (suspend(u) < 0)
629 return -1;
630 }
631
632 do_trigger = 1;
633
634 u->sink_suspended = 1;
635 break;
636
637 case PA_SINK_IDLE:
638 case PA_SINK_RUNNING:
639
640 if (u->sink->thread_info.state == PA_SINK_SUSPENDED) {
641
642 if (!u->source || u->source_suspended) {
643 if (unsuspend(u) < 0)
644 return -1;
645 quick = 0;
646 }
647
648 do_trigger = 1;
649
650 u->out_mmap_current = 0;
651 u->out_mmap_saved_nfrags = 0;
652
653 u->sink_suspended = 0;
654 }
655
656 break;
657
658 case PA_SINK_UNLINKED:
659 case PA_SINK_INIT:
660 ;
661 }
662
663 break;
664
665 }
666
667 ret = pa_sink_process_msg(o, code, data, offset, chunk);
668
669 if (do_trigger)
670 trigger(u, quick);
671
672 return ret;
673 }
674
675 static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
676 struct userdata *u = PA_SOURCE(o)->userdata;
677 int do_trigger = 0, ret, quick = 1;
678
679 switch (code) {
680
681 case PA_SOURCE_MESSAGE_GET_LATENCY: {
682 pa_usec_t r = 0;
683
684 if (u->fd >= 0) {
685 if (u->use_mmap)
686 r = mmap_source_get_latency(u);
687 else
688 r = io_source_get_latency(u);
689 }
690
691 *((pa_usec_t*) data) = r;
692 return 0;
693 }
694
695 case PA_SOURCE_MESSAGE_SET_STATE:
696
697 switch ((pa_source_state_t) PA_PTR_TO_UINT(data)) {
698 case PA_SOURCE_SUSPENDED:
699 pa_assert(PA_SOURCE_OPENED(u->source->thread_info.state));
700
701 if (!u->sink || u->sink_suspended) {
702 if (suspend(u) < 0)
703 return -1;
704 }
705
706 do_trigger = 1;
707
708 u->source_suspended = 1;
709 break;
710
711 case PA_SOURCE_IDLE:
712 case PA_SOURCE_RUNNING:
713
714 if (u->source->thread_info.state == PA_SOURCE_SUSPENDED) {
715
716 if (!u->sink || u->sink_suspended) {
717 if (unsuspend(u) < 0)
718 return -1;
719 quick = 0;
720 }
721
722 do_trigger = 1;
723
724 u->in_mmap_current = 0;
725 u->in_mmap_saved_nfrags = 0;
726
727 u->source_suspended = 0;
728 }
729 break;
730
731 case PA_SOURCE_UNLINKED:
732 case PA_SOURCE_INIT:
733 ;
734
735 }
736 break;
737
738 }
739
740 ret = pa_source_process_msg(o, code, data, offset, chunk);
741
742 if (do_trigger)
743 trigger(u, quick);
744
745 return ret;
746 }
747
748 static int sink_get_volume(pa_sink *s) {
749 struct userdata *u;
750 int r;
751
752 pa_assert_se(u = s->userdata);
753
754 pa_assert(u->mixer_devmask & (SOUND_MASK_VOLUME|SOUND_MASK_PCM));
755
756 if (u->mixer_devmask & SOUND_MASK_VOLUME)
757 if ((r = pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_READ_VOLUME, &s->sample_spec, &s->volume)) >= 0)
758 return r;
759
760 if (u->mixer_devmask & SOUND_MASK_PCM)
761 if ((r = pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_READ_PCM, &s->sample_spec, &s->volume)) >= 0)
762 return r;
763
764 pa_log_info("Device doesn't support reading mixer settings: %s", pa_cstrerror(errno));
765 return -1;
766 }
767
768 static int sink_set_volume(pa_sink *s) {
769 struct userdata *u;
770 int r;
771
772 pa_assert_se(u = s->userdata);
773
774 pa_assert(u->mixer_devmask & (SOUND_MASK_VOLUME|SOUND_MASK_PCM));
775
776 if (u->mixer_devmask & SOUND_MASK_VOLUME)
777 if ((r = pa_oss_set_volume(u->mixer_fd, SOUND_MIXER_WRITE_VOLUME, &s->sample_spec, &s->volume)) >= 0)
778 return r;
779
780 if (u->mixer_devmask & SOUND_MASK_PCM)
781 if ((r = pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_WRITE_PCM, &s->sample_spec, &s->volume)) >= 0)
782 return r;
783
784 pa_log_info("Device doesn't support writing mixer settings: %s", pa_cstrerror(errno));
785 return -1;
786 }
787
788 static int source_get_volume(pa_source *s) {
789 struct userdata *u;
790 int r;
791
792 pa_assert_se(u = s->userdata);
793
794 pa_assert(u->mixer_devmask & (SOUND_MASK_IGAIN|SOUND_MASK_RECLEV));
795
796 if (u->mixer_devmask & SOUND_MASK_IGAIN)
797 if ((r = pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_READ_IGAIN, &s->sample_spec, &s->volume)) >= 0)
798 return r;
799
800 if (u->mixer_devmask & SOUND_MASK_RECLEV)
801 if ((r = pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_READ_RECLEV, &s->sample_spec, &s->volume)) >= 0)
802 return r;
803
804 pa_log_info("Device doesn't support reading mixer settings: %s", pa_cstrerror(errno));
805 return -1;
806 }
807
808 static int source_set_volume(pa_source *s) {
809 struct userdata *u;
810 int r;
811
812 pa_assert_se(u = s->userdata);
813
814 pa_assert(u->mixer_devmask & (SOUND_MASK_IGAIN|SOUND_MASK_RECLEV));
815
816 if (u->mixer_devmask & SOUND_MASK_IGAIN)
817 if ((r = pa_oss_set_volume(u->mixer_fd, SOUND_MIXER_WRITE_IGAIN, &s->sample_spec, &s->volume)) >= 0)
818 return r;
819
820 if (u->mixer_devmask & SOUND_MASK_RECLEV)
821 if ((r = pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_WRITE_RECLEV, &s->sample_spec, &s->volume)) >= 0)
822 return r;
823
824 pa_log_info("Device doesn't support writing mixer settings: %s", pa_cstrerror(errno));
825 return -1;
826 }
827
828 static void thread_func(void *userdata) {
829 struct userdata *u = userdata;
830 int write_type = 0, read_type = 0;
831 unsigned short revents = 0;
832
833 pa_assert(u);
834
835 pa_log_debug("Thread starting up");
836
837 if (u->core->high_priority)
838 pa_make_realtime();
839
840 pa_thread_mq_install(&u->thread_mq);
841 pa_rtpoll_install(u->rtpoll);
842
843 trigger(u, 0);
844
845 for (;;) {
846 int ret;
847
848 /* pa_log("loop"); */
849
850 /* Render some data and write it to the dsp */
851
852 if (u->sink && u->sink->thread_info.state != PA_SINK_UNLINKED && u->fd >= 0 && (revents & POLLOUT)) {
853
854 if (u->use_mmap) {
855
856 if ((ret = mmap_write(u)) < 0)
857 goto fail;
858
859 revents &= ~POLLOUT;
860
861 if (ret > 0)
862 continue;
863
864 } else {
865 ssize_t l;
866 int loop = 0;
867
868 l = u->out_fragment_size;
869
870 if (u->use_getospace) {
871 audio_buf_info info;
872
873 if (ioctl(u->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
874 pa_log_info("Device doesn't support SNDCTL_DSP_GETOSPACE: %s", pa_cstrerror(errno));
875 u->use_getospace = 0;
876 } else {
877 if (info.bytes >= l) {
878 l = (info.bytes/l)*l;
879 loop = 1;
880 }
881 }
882 }
883
884 do {
885 void *p;
886 ssize_t t;
887
888 pa_assert(l > 0);
889
890 if (u->memchunk.length <= 0)
891 pa_sink_render(u->sink, l, &u->memchunk);
892
893 pa_assert(u->memchunk.length > 0);
894
895 p = pa_memblock_acquire(u->memchunk.memblock);
896 t = pa_write(u->fd, (uint8_t*) p + u->memchunk.index, u->memchunk.length, &write_type);
897 pa_memblock_release(u->memchunk.memblock);
898
899 /* pa_log("wrote %i bytes of %u", t, l); */
900
901 pa_assert(t != 0);
902
903 if (t < 0) {
904
905 if (errno == EINTR)
906 continue;
907
908 else if (errno == EAGAIN) {
909 pa_log_debug("EAGAIN");
910
911 revents &= ~POLLOUT;
912 break;
913
914 } else {
915 pa_log("Failed to write data to DSP: %s", pa_cstrerror(errno));
916 goto fail;
917 }
918
919 } else {
920
921 u->memchunk.index += t;
922 u->memchunk.length -= t;
923
924 if (u->memchunk.length <= 0) {
925 pa_memblock_unref(u->memchunk.memblock);
926 pa_memchunk_reset(&u->memchunk);
927 }
928
929 l -= t;
930
931 revents &= ~POLLOUT;
932 }
933
934 } while (loop && l > 0);
935
936 continue;
937 }
938 }
939
940 /* Try to read some data and pass it on to the source driver */
941
942 if (u->source && u->source->thread_info.state != PA_SOURCE_UNLINKED && u->fd >= 0 && ((revents & POLLIN))) {
943
944 if (u->use_mmap) {
945
946 if ((ret = mmap_read(u)) < 0)
947 goto fail;
948
949 revents &= ~POLLIN;
950
951 if (ret > 0)
952 continue;
953
954 } else {
955
956 void *p;
957 ssize_t l;
958 pa_memchunk memchunk;
959 int loop = 0;
960
961 l = u->in_fragment_size;
962
963 if (u->use_getispace) {
964 audio_buf_info info;
965
966 if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) < 0) {
967 pa_log_info("Device doesn't support SNDCTL_DSP_GETISPACE: %s", pa_cstrerror(errno));
968 u->use_getispace = 0;
969 } else {
970 if (info.bytes >= l) {
971 l = (info.bytes/l)*l;
972 loop = 1;
973 }
974 }
975 }
976
977 do {
978 ssize_t t, k;
979
980 pa_assert(l > 0);
981
982 memchunk.memblock = pa_memblock_new(u->core->mempool, (size_t) -1);
983
984 k = pa_memblock_get_length(memchunk.memblock);
985
986 if (k > l)
987 k = l;
988
989 k = (k/u->frame_size)*u->frame_size;
990
991 p = pa_memblock_acquire(memchunk.memblock);
992 t = pa_read(u->fd, p, k, &read_type);
993 pa_memblock_release(memchunk.memblock);
994
995 pa_assert(t != 0); /* EOF cannot happen */
996
997 /* pa_log("read %i bytes of %u", t, l); */
998
999 if (t < 0) {
1000 pa_memblock_unref(memchunk.memblock);
1001
1002 if (errno == EINTR)
1003 continue;
1004
1005 else if (errno == EAGAIN) {
1006 pa_log_debug("EAGAIN");
1007
1008 revents &= ~POLLIN;
1009 break;
1010
1011 } else {
1012 pa_log("Failed to read data from DSP: %s", pa_cstrerror(errno));
1013 goto fail;
1014 }
1015
1016 } else {
1017 memchunk.index = 0;
1018 memchunk.length = t;
1019
1020 pa_source_post(u->source, &memchunk);
1021 pa_memblock_unref(memchunk.memblock);
1022
1023 l -= t;
1024
1025 revents &= ~POLLIN;
1026 }
1027 } while (loop && l > 0);
1028
1029 continue;
1030 }
1031 }
1032
1033 /* pa_log("loop2"); */
1034
1035 if (u->fd >= 0) {
1036 struct pollfd *pollfd;
1037
1038 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
1039 pollfd->events =
1040 ((u->source && PA_SOURCE_OPENED(u->source->thread_info.state)) ? POLLIN : 0) |
1041 ((u->sink && PA_SINK_OPENED(u->sink->thread_info.state)) ? POLLOUT : 0);
1042 }
1043
1044 /* Hmm, nothing to do. Let's sleep */
1045 if ((ret = pa_rtpoll_run(u->rtpoll, 1)) < 0)
1046 goto fail;
1047
1048 if (ret == 0)
1049 goto finish;
1050
1051 if (u->fd >= 0) {
1052 struct pollfd *pollfd;
1053
1054 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
1055
1056 if (pollfd->revents & ~(POLLOUT|POLLIN)) {
1057 pa_log("DSP shutdown.");
1058 goto fail;
1059 }
1060
1061 revents = pollfd->revents;
1062 } else
1063 revents = 0;
1064 }
1065
1066 fail:
1067 /* If this was no regular exit from the loop we have to continue
1068 * processing messages until we received PA_MESSAGE_SHUTDOWN */
1069 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
1070 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
1071
1072 finish:
1073 pa_log_debug("Thread shutting down");
1074 }
1075
1076 int pa__init(pa_module*m) {
1077
1078 struct audio_buf_info info;
1079 struct userdata *u = NULL;
1080 const char *dev;
1081 int fd = -1;
1082 int nfrags, frag_size;
1083 int mode, caps;
1084 int record = 1, playback = 1, use_mmap = 1;
1085 pa_sample_spec ss;
1086 pa_channel_map map;
1087 pa_modargs *ma = NULL;
1088 char hwdesc[64], *t;
1089 const char *name;
1090 int namereg_fail;
1091 struct pollfd *pollfd;
1092
1093 pa_assert(m);
1094
1095 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
1096 pa_log("Failed to parse module arguments.");
1097 goto fail;
1098 }
1099
1100 if (pa_modargs_get_value_boolean(ma, "record", &record) < 0 || pa_modargs_get_value_boolean(ma, "playback", &playback) < 0) {
1101 pa_log("record= and playback= expect numeric argument.");
1102 goto fail;
1103 }
1104
1105 if (!playback && !record) {
1106 pa_log("Neither playback nor record enabled for device.");
1107 goto fail;
1108 }
1109
1110 mode = (playback && record) ? O_RDWR : (playback ? O_WRONLY : (record ? O_RDONLY : 0));
1111
1112 ss = m->core->default_sample_spec;
1113 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_OSS) < 0) {
1114 pa_log("Failed to parse sample specification or channel map");
1115 goto fail;
1116 }
1117
1118 nfrags = m->core->default_n_fragments;
1119 frag_size = pa_usec_to_bytes(m->core->default_fragment_size_msec*1000, &ss);
1120 if (frag_size <= 0)
1121 frag_size = pa_frame_size(&ss);
1122
1123 if (pa_modargs_get_value_s32(ma, "fragments", &nfrags) < 0 || pa_modargs_get_value_s32(ma, "fragment_size", &frag_size) < 0) {
1124 pa_log("Failed to parse fragments arguments");
1125 goto fail;
1126 }
1127
1128 if (pa_modargs_get_value_boolean(ma, "mmap", &use_mmap) < 0) {
1129 pa_log("Failed to parse mmap argument.");
1130 goto fail;
1131 }
1132
1133 if ((fd = pa_oss_open(dev = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), &mode, &caps)) < 0)
1134 goto fail;
1135
1136 if (use_mmap && (!(caps & DSP_CAP_MMAP) || !(caps & DSP_CAP_TRIGGER))) {
1137 pa_log_info("OSS device not mmap capable, falling back to UNIX read/write mode.");
1138 use_mmap = 0;
1139 }
1140
1141 if (use_mmap && mode == O_WRONLY) {
1142 pa_log_info("Device opened for playback only, cannot do memory mapping, falling back to UNIX write() mode.");
1143 use_mmap = 0;
1144 }
1145
1146 if (pa_oss_get_hw_description(dev, hwdesc, sizeof(hwdesc)) >= 0)
1147 pa_log_info("Hardware name is '%s'.", hwdesc);
1148 else
1149 hwdesc[0] = 0;
1150
1151 pa_log_info("Device opened in %s mode.", mode == O_WRONLY ? "O_WRONLY" : (mode == O_RDONLY ? "O_RDONLY" : "O_RDWR"));
1152
1153 if (nfrags >= 2 && frag_size >= 1)
1154 if (pa_oss_set_fragments(fd, nfrags, frag_size) < 0)
1155 goto fail;
1156
1157 if (pa_oss_auto_format(fd, &ss) < 0)
1158 goto fail;
1159
1160 if (ioctl(fd, SNDCTL_DSP_GETBLKSIZE, &frag_size) < 0) {
1161 pa_log("SNDCTL_DSP_GETBLKSIZE: %s", pa_cstrerror(errno));
1162 goto fail;
1163 }
1164 pa_assert(frag_size > 0);
1165
1166 u = pa_xnew0(struct userdata, 1);
1167 u->core = m->core;
1168 u->module = m;
1169 m->userdata = u;
1170 u->fd = fd;
1171 u->mixer_fd = -1;
1172 u->use_getospace = u->use_getispace = 1;
1173 u->use_getodelay = 1;
1174 u->mode = mode;
1175 u->frame_size = pa_frame_size(&ss);
1176 u->device_name = pa_xstrdup(dev);
1177 u->in_nfrags = u->out_nfrags = u->nfrags = nfrags;
1178 u->out_fragment_size = u->in_fragment_size = u->frag_size = frag_size;
1179 u->use_mmap = use_mmap;
1180 pa_thread_mq_init(&u->thread_mq, m->core->mainloop);
1181 u->rtpoll = pa_rtpoll_new();
1182 pa_rtpoll_item_new_asyncmsgq(u->rtpoll, PA_RTPOLL_EARLY, u->thread_mq.inq);
1183 u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
1184 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
1185 pollfd->fd = fd;
1186 pollfd->events = 0;
1187 pollfd->revents = 0;
1188
1189 if (ioctl(fd, SNDCTL_DSP_GETISPACE, &info) >= 0) {
1190 pa_log_info("Input -- %u fragments of size %u.", info.fragstotal, info.fragsize);
1191 u->in_fragment_size = info.fragsize;
1192 u->in_nfrags = info.fragstotal;
1193 u->use_getispace = 1;
1194 }
1195
1196 if (ioctl(fd, SNDCTL_DSP_GETOSPACE, &info) >= 0) {
1197 pa_log_info("Output -- %u fragments of size %u.", info.fragstotal, info.fragsize);
1198 u->out_fragment_size = info.fragsize;
1199 u->out_nfrags = info.fragstotal;
1200 u->use_getospace = 1;
1201 }
1202
1203 u->in_hwbuf_size = u->in_nfrags * u->in_fragment_size;
1204 u->out_hwbuf_size = u->out_nfrags * u->out_fragment_size;
1205
1206 if (mode != O_WRONLY) {
1207 char *name_buf = NULL;
1208
1209 if (use_mmap) {
1210 if ((u->in_mmap = mmap(NULL, u->in_hwbuf_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) {
1211 pa_log_warn("mmap(PROT_READ) failed, reverting to non-mmap mode: %s", pa_cstrerror(errno));
1212 use_mmap = u->use_mmap = 0;
1213 u->in_mmap = NULL;
1214 } else
1215 pa_log_debug("Successfully mmap()ed input buffer.");
1216 }
1217
1218 if ((name = pa_modargs_get_value(ma, "source_name", NULL)))
1219 namereg_fail = 1;
1220 else {
1221 name = name_buf = pa_sprintf_malloc("oss_input.%s", pa_path_get_filename(dev));
1222 namereg_fail = 0;
1223 }
1224
1225 u->source = pa_source_new(m->core, __FILE__, name, namereg_fail, &ss, &map);
1226 pa_xfree(name_buf);
1227 if (!u->source) {
1228 pa_log("Failed to create source object");
1229 goto fail;
1230 }
1231
1232 u->source->parent.process_msg = source_process_msg;
1233 u->source->userdata = u;
1234
1235 pa_source_set_module(u->source, m);
1236 pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
1237 pa_source_set_rtpoll(u->source, u->rtpoll);
1238 pa_source_set_description(u->source, t = pa_sprintf_malloc(
1239 "OSS PCM on %s%s%s%s%s",
1240 dev,
1241 hwdesc[0] ? " (" : "",
1242 hwdesc[0] ? hwdesc : "",
1243 hwdesc[0] ? ")" : "",
1244 use_mmap ? " via DMA" : ""));
1245 pa_xfree(t);
1246 u->source->flags = PA_SOURCE_HARDWARE|PA_SOURCE_LATENCY;
1247 u->source->refresh_volume = 1;
1248
1249 if (use_mmap)
1250 u->in_mmap_memblocks = pa_xnew0(pa_memblock*, u->in_nfrags);
1251 }
1252
1253 if (mode != O_RDONLY) {
1254 char *name_buf = NULL;
1255
1256 if (use_mmap) {
1257 if ((u->out_mmap = mmap(NULL, u->out_hwbuf_size, PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) {
1258 if (mode == O_RDWR) {
1259 pa_log_debug("mmap() failed for input. Changing to O_WRONLY mode.");
1260 mode = O_WRONLY;
1261 goto go_on;
1262 } else {
1263 pa_log_warn("mmap(PROT_WRITE) failed, reverting to non-mmap mode: %s", pa_cstrerror(errno));
1264 u->use_mmap = use_mmap = 0;
1265 u->out_mmap = NULL;
1266 }
1267 } else {
1268 pa_log_debug("Successfully mmap()ed output buffer.");
1269 pa_silence_memory(u->out_mmap, u->out_hwbuf_size, &ss);
1270 }
1271 }
1272
1273 if ((name = pa_modargs_get_value(ma, "sink_name", NULL)))
1274 namereg_fail = 1;
1275 else {
1276 name = name_buf = pa_sprintf_malloc("oss_output.%s", pa_path_get_filename(dev));
1277 namereg_fail = 0;
1278 }
1279
1280 u->sink = pa_sink_new(m->core, __FILE__, name, namereg_fail, &ss, &map);
1281 pa_xfree(name_buf);
1282 if (!u->sink) {
1283 pa_log("Failed to create sink object");
1284 goto fail;
1285 }
1286
1287 u->sink->parent.process_msg = sink_process_msg;
1288 u->sink->userdata = u;
1289
1290 pa_sink_set_module(u->sink, m);
1291 pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
1292 pa_sink_set_rtpoll(u->sink, u->rtpoll);
1293 pa_sink_set_description(u->sink, t = pa_sprintf_malloc(
1294 "OSS PCM on %s%s%s%s%s",
1295 dev,
1296 hwdesc[0] ? " (" : "",
1297 hwdesc[0] ? hwdesc : "",
1298 hwdesc[0] ? ")" : "",
1299 use_mmap ? " via DMA" : ""));
1300 pa_xfree(t);
1301 u->sink->flags = PA_SINK_HARDWARE|PA_SINK_LATENCY;
1302 u->sink->refresh_volume = 1;
1303
1304 if (use_mmap)
1305 u->out_mmap_memblocks = pa_xnew0(pa_memblock*, u->out_nfrags);
1306 }
1307
1308 if ((u->mixer_fd = pa_oss_open_mixer_for_device(u->device_name)) >= 0) {
1309 int do_close = 1;
1310 u->mixer_devmask = 0;
1311
1312 if (ioctl(fd, SOUND_MIXER_READ_DEVMASK, &u->mixer_devmask) < 0)
1313 pa_log_warn("SOUND_MIXER_READ_DEVMASK failed: %s", pa_cstrerror(errno));
1314
1315 else {
1316 if (u->sink && (u->mixer_devmask & (SOUND_MASK_VOLUME|SOUND_MASK_PCM))) {
1317 pa_log_debug("Found hardware mixer track for playback.");
1318 u->sink->flags |= PA_SINK_HW_VOLUME_CTRL;
1319 u->sink->get_volume = sink_get_volume;
1320 u->sink->set_volume = sink_set_volume;
1321 do_close = 0;
1322 }
1323
1324 if (u->source && (u->mixer_devmask & (SOUND_MASK_RECLEV|SOUND_MASK_IGAIN))) {
1325 pa_log_debug("Found hardware mixer track for recording.");
1326 u->source->flags |= PA_SOURCE_HW_VOLUME_CTRL;
1327 u->source->get_volume = source_get_volume;
1328 u->source->set_volume = source_set_volume;
1329 do_close = 0;
1330 }
1331 }
1332
1333 if (do_close) {
1334 pa_close(u->mixer_fd);
1335 u->mixer_fd = -1;
1336 }
1337 }
1338
1339 go_on:
1340
1341 pa_assert(u->source || u->sink);
1342
1343 pa_memchunk_reset(&u->memchunk);
1344
1345 if (!(u->thread = pa_thread_new(thread_func, u))) {
1346 pa_log("Failed to create thread.");
1347 goto fail;
1348 }
1349
1350 /* Read mixer settings */
1351 if (u->sink && u->sink->get_volume)
1352 sink_get_volume(u->sink);
1353 if (u->source && u->source->get_volume)
1354 source_get_volume(u->source);
1355
1356 if (u->sink)
1357 pa_sink_put(u->sink);
1358 if (u->source)
1359 pa_source_put(u->source);
1360
1361 pa_modargs_free(ma);
1362
1363 return 0;
1364
1365 fail:
1366
1367 if (u)
1368 pa__done(m);
1369 else if (fd >= 0)
1370 pa_close(fd);
1371
1372 if (ma)
1373 pa_modargs_free(ma);
1374
1375 return -1;
1376 }
1377
1378 void pa__done(pa_module*m) {
1379 struct userdata *u;
1380
1381 pa_assert(m);
1382
1383 if (!(u = m->userdata))
1384 return;
1385
1386 if (u->sink)
1387 pa_sink_unlink(u->sink);
1388
1389 if (u->source)
1390 pa_source_unlink(u->source);
1391
1392 if (u->thread) {
1393 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
1394 pa_thread_free(u->thread);
1395 }
1396
1397 pa_thread_mq_done(&u->thread_mq);
1398
1399 if (u->sink)
1400 pa_sink_unref(u->sink);
1401
1402 if (u->source)
1403 pa_source_unref(u->source);
1404
1405 if (u->memchunk.memblock)
1406 pa_memblock_unref(u->memchunk.memblock);
1407
1408 if (u->rtpoll_item)
1409 pa_rtpoll_item_free(u->rtpoll_item);
1410
1411 if (u->rtpoll)
1412 pa_rtpoll_free(u->rtpoll);
1413
1414 if (u->out_mmap_memblocks) {
1415 unsigned i;
1416 for (i = 0; i < u->out_nfrags; i++)
1417 if (u->out_mmap_memblocks[i])
1418 pa_memblock_unref_fixed(u->out_mmap_memblocks[i]);
1419 pa_xfree(u->out_mmap_memblocks);
1420 }
1421
1422 if (u->in_mmap_memblocks) {
1423 unsigned i;
1424 for (i = 0; i < u->in_nfrags; i++)
1425 if (u->in_mmap_memblocks[i])
1426 pa_memblock_unref_fixed(u->in_mmap_memblocks[i]);
1427 pa_xfree(u->in_mmap_memblocks);
1428 }
1429
1430 if (u->in_mmap && u->in_mmap != MAP_FAILED)
1431 munmap(u->in_mmap, u->in_hwbuf_size);
1432
1433 if (u->out_mmap && u->out_mmap != MAP_FAILED)
1434 munmap(u->out_mmap, u->out_hwbuf_size);
1435
1436 if (u->fd >= 0)
1437 pa_close(u->fd);
1438
1439 if (u->mixer_fd >= 0)
1440 pa_close(u->mixer_fd);
1441
1442 pa_xfree(u->device_name);
1443
1444 pa_xfree(u);
1445 }