]> code.delx.au - pulseaudio/blob - src/modules/module-solaris.c
merge 'lennart' branch back into trunk.
[pulseaudio] / src / modules / module-solaris.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2006 Lennart Poettering
7 Copyright 2006-2007 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
13
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <assert.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <limits.h>
37 #include <sys/ioctl.h>
38 #include <sys/stat.h>
39 #include <sys/types.h>
40
41 #include <signal.h>
42 #include <stropts.h>
43 #include <sys/conf.h>
44 #include <sys/audio.h>
45
46 #include <pulse/error.h>
47 #include <pulse/mainloop-signal.h>
48 #include <pulse/xmalloc.h>
49 #include <pulse/timeval.h>
50
51 #include <pulsecore/iochannel.h>
52 #include <pulsecore/sink.h>
53 #include <pulsecore/source.h>
54 #include <pulsecore/module.h>
55 #include <pulsecore/sample-util.h>
56 #include <pulsecore/core-util.h>
57 #include <pulsecore/modargs.h>
58 #include <pulsecore/log.h>
59 #include <pulsecore/core-error.h>
60 #include <pulsecore/thread-mq.h>
61 #include <pulsecore/rtpoll.h>
62 #include <pulsecore/thread.h>
63
64 #include "module-solaris-symdef.h"
65
66 PA_MODULE_AUTHOR("Pierre Ossman")
67 PA_MODULE_DESCRIPTION("Solaris Sink/Source")
68 PA_MODULE_VERSION(PACKAGE_VERSION)
69 PA_MODULE_USAGE(
70 "sink_name=<name for the sink> "
71 "source_name=<name for the source> "
72 "device=<OSS device> record=<enable source?> "
73 "playback=<enable sink?> "
74 "format=<sample format> "
75 "channels=<number of channels> "
76 "rate=<sample rate> "
77 "buffer_size=<record buffer size> "
78 "channel_map=<channel map>")
79
80 struct userdata {
81 pa_core *core;
82 pa_sink *sink;
83 pa_source *source;
84
85 pa_thread *thread;
86 pa_thread_mq thread_mq;
87 pa_rtpoll *rtpoll;
88
89 pa_signal_event *sig;
90
91 pa_memchunk memchunk;
92
93 unsigned int page_size;
94
95 uint32_t frame_size;
96 uint32_t buffer_size;
97 unsigned int written_bytes, read_bytes;
98
99 int fd;
100 pa_rtpoll_item *rtpoll_item;
101 pa_module *module;
102 };
103
104 static const char* const valid_modargs[] = {
105 "sink_name",
106 "source_name",
107 "device",
108 "record",
109 "playback",
110 "buffer_size",
111 "format",
112 "rate",
113 "channels",
114 "channel_map",
115 NULL
116 };
117
118 #define DEFAULT_SINK_NAME "solaris_output"
119 #define DEFAULT_SOURCE_NAME "solaris_input"
120 #define DEFAULT_DEVICE "/dev/audio"
121
122 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
123 struct userdata *u = PA_SINK(o)->userdata;
124 int err;
125 audio_info_t info;
126
127 switch (code) {
128 case PA_SINK_MESSAGE_GET_LATENCY: {
129 pa_usec_t r = 0;
130
131 if (u->fd >= 0) {
132
133 err = ioctl(u->fd, AUDIO_GETINFO, &info);
134 pa_assert(err >= 0);
135
136 r += pa_bytes_to_usec(u->written_bytes, &PA_SINK(o)->sample_spec);
137 r -= pa_bytes_to_usec(info.play.samples * u->frame_size, &PA_SINK(o)->sample_spec);
138
139 if (u->memchunk.memblock)
140 r += pa_bytes_to_usec(u->memchunk.length, &PA_SINK(o)->sample_spec);
141 }
142
143 *((pa_usec_t*) data) = r;
144
145 return 0;
146 }
147
148 case PA_SINK_MESSAGE_SET_VOLUME:
149 if (u->fd >= 0) {
150 AUDIO_INITINFO(&info);
151
152 info.play.gain = pa_cvolume_avg((pa_cvolume*)data) * AUDIO_MAX_GAIN / PA_VOLUME_NORM;
153 assert(info.play.gain <= AUDIO_MAX_GAIN);
154
155 if (ioctl(u->fd, AUDIO_SETINFO, &info) < 0) {
156 if (errno == EINVAL)
157 pa_log("AUDIO_SETINFO: Unsupported volume.");
158 else
159 pa_log("AUDIO_SETINFO: %s", pa_cstrerror(errno));
160 } else {
161 return 0;
162 }
163 }
164 break;
165
166 case PA_SINK_MESSAGE_GET_VOLUME:
167 if (u->fd >= 0) {
168 err = ioctl(u->fd, AUDIO_GETINFO, &info);
169 assert(err >= 0);
170
171 pa_cvolume_set((pa_cvolume*) data, ((pa_cvolume*) data)->channels,
172 info.play.gain * PA_VOLUME_NORM / AUDIO_MAX_GAIN);
173
174 return 0;
175 }
176 break;
177
178 case PA_SINK_MESSAGE_SET_MUTE:
179 if (u->fd >= 0) {
180 AUDIO_INITINFO(&info);
181
182 info.output_muted = !!PA_PTR_TO_UINT(data);
183
184 if (ioctl(u->fd, AUDIO_SETINFO, &info) < 0)
185 pa_log("AUDIO_SETINFO: %s", pa_cstrerror(errno));
186 else
187 return 0;
188 }
189 break;
190
191 case PA_SINK_MESSAGE_GET_MUTE:
192 if (u->fd >= 0) {
193 err = ioctl(u->fd, AUDIO_GETINFO, &info);
194 pa_assert(err >= 0);
195
196 *(int*)data = !!info.output_muted;
197
198 return 0;
199 }
200 break;
201 }
202
203 return pa_sink_process_msg(o, code, data, offset, chunk);
204 }
205
206 static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
207 struct userdata *u = PA_SOURCE(o)->userdata;
208 int err;
209 audio_info_t info;
210
211 switch (code) {
212 case PA_SOURCE_MESSAGE_GET_LATENCY: {
213 pa_usec_t r = 0;
214
215 if (u->fd) {
216 err = ioctl(u->fd, AUDIO_GETINFO, &info);
217 pa_assert(err >= 0);
218
219 r += pa_bytes_to_usec(info.record.samples * u->frame_size, &PA_SOURCE(o)->sample_spec);
220 r -= pa_bytes_to_usec(u->read_bytes, &PA_SOURCE(o)->sample_spec);
221 }
222
223 *((pa_usec_t*) data) = r;
224
225 return 0;
226 }
227
228 case PA_SOURCE_MESSAGE_SET_VOLUME:
229 if (u->fd >= 0) {
230 AUDIO_INITINFO(&info);
231
232 info.record.gain = pa_cvolume_avg((pa_cvolume*) data) * AUDIO_MAX_GAIN / PA_VOLUME_NORM;
233 assert(info.record.gain <= AUDIO_MAX_GAIN);
234
235 if (ioctl(u->fd, AUDIO_SETINFO, &info) < 0) {
236 if (errno == EINVAL)
237 pa_log("AUDIO_SETINFO: Unsupported volume.");
238 else
239 pa_log("AUDIO_SETINFO: %s", pa_cstrerror(errno));
240 } else {
241 return 0;
242 }
243 }
244 break;
245
246 case PA_SOURCE_MESSAGE_GET_VOLUME:
247 if (u->fd >= 0) {
248 err = ioctl(u->fd, AUDIO_GETINFO, &info);
249 pa_assert(err >= 0);
250
251 pa_cvolume_set((pa_cvolume*) data, ((pa_cvolume*) data)->channels,
252 info.record.gain * PA_VOLUME_NORM / AUDIO_MAX_GAIN);
253
254 return 0;
255 }
256 break;
257 }
258
259 return pa_source_process_msg(o, code, data, offset, chunk);
260 }
261
262 static void clear_underflow(struct userdata *u)
263 {
264 audio_info_t info;
265
266 AUDIO_INITINFO(&info);
267
268 info.play.error = 0;
269
270 if (ioctl(u->fd, AUDIO_SETINFO, &info) < 0)
271 pa_log("AUDIO_SETINFO: %s", pa_cstrerror(errno));
272 }
273
274 static void clear_overflow(struct userdata *u)
275 {
276 audio_info_t info;
277
278 AUDIO_INITINFO(&info);
279
280 info.record.error = 0;
281
282 if (ioctl(u->fd, AUDIO_SETINFO, &info) < 0)
283 pa_log("AUDIO_SETINFO: %s", pa_cstrerror(errno));
284 }
285
286 static void thread_func(void *userdata) {
287 struct userdata *u = userdata;
288 unsigned short revents = 0;
289 int ret;
290
291 pa_assert(u);
292
293 pa_log_debug("Thread starting up");
294
295 if (u->core->high_priority)
296 pa_make_realtime();
297
298 pa_thread_mq_install(&u->thread_mq);
299 pa_rtpoll_install(u->rtpoll);
300
301 for (;;) {
302 /* Render some data and write it to the dsp */
303
304 if (u->sink && PA_SINK_OPENED(u->sink->thread_info.state)) {
305 audio_info_t info;
306 int err;
307 size_t len;
308
309 err = ioctl(u->fd, AUDIO_GETINFO, &info);
310 pa_assert(err >= 0);
311
312 /*
313 * Since we cannot modify the size of the output buffer we fake it
314 * by not filling it more than u->buffer_size.
315 */
316 len = u->buffer_size;
317 len -= u->written_bytes - (info.play.samples * u->frame_size);
318
319 /* The sample counter can sometimes go backwards :( */
320 if (len > u->buffer_size)
321 len = 0;
322
323 if (info.play.error) {
324 pa_log_debug("Solaris buffer underflow!");
325 clear_underflow(u);
326 }
327
328 len -= len % u->frame_size;
329
330 while (len) {
331 void *p;
332 ssize_t r;
333
334 if (!u->memchunk.length)
335 pa_sink_render(u->sink, len, &u->memchunk);
336
337 pa_assert(u->memchunk.length);
338
339 p = pa_memblock_acquire(u->memchunk.memblock);
340 r = pa_write(u->fd, (uint8_t*) p + u->memchunk.index, u->memchunk.length, NULL);
341 pa_memblock_release(u->memchunk.memblock);
342
343 if (r < 0) {
344 if (errno == EINTR)
345 continue;
346 else if (errno != EAGAIN) {
347 pa_log("Failed to read data from DSP: %s", pa_cstrerror(errno));
348 goto fail;
349 }
350 } else {
351 pa_assert(r % u->frame_size == 0);
352
353 u->memchunk.index += r;
354 u->memchunk.length -= r;
355
356 if (u->memchunk.length <= 0) {
357 pa_memblock_unref(u->memchunk.memblock);
358 pa_memchunk_reset(&u->memchunk);
359 }
360
361 len -= r;
362 u->written_bytes += r;
363 }
364 }
365 }
366
367 /* Try to read some data and pass it on to the source driver */
368
369 if (u->source && PA_SOURCE_OPENED(u->source->thread_info.state) && ((revents & POLLIN))) {
370 pa_memchunk memchunk;
371 int err;
372 size_t l;
373 void *p;
374 ssize_t r;
375 audio_info_t info;
376
377 err = ioctl(u->fd, AUDIO_GETINFO, &info);
378 pa_assert(err >= 0);
379
380 if (info.record.error) {
381 pa_log_debug("Solaris buffer overflow!");
382 clear_overflow(u);
383 }
384
385 err = ioctl(u->fd, I_NREAD, &l);
386 pa_assert(err >= 0);
387
388 if (l > 0) {
389 /* This is to make sure it fits in the memory pool. Also, a page
390 should be the most efficient transfer size. */
391 if (l > u->page_size)
392 l = u->page_size;
393
394 memchunk.memblock = pa_memblock_new(u->core->mempool, l);
395 pa_assert(memchunk.memblock);
396
397 p = pa_memblock_acquire(memchunk.memblock);
398 r = pa_read(u->fd, p, l, NULL);
399 pa_memblock_release(memchunk.memblock);
400
401 if (r < 0) {
402 pa_memblock_unref(memchunk.memblock);
403 if (errno != EAGAIN) {
404 pa_log("Failed to read data from DSP: %s", pa_cstrerror(errno));
405 goto fail;
406 }
407 } else {
408 memchunk.index = 0;
409 memchunk.length = r;
410
411 pa_source_post(u->source, &memchunk);
412 pa_memblock_unref(memchunk.memblock);
413
414 u->read_bytes += r;
415
416 revents &= ~POLLIN;
417 }
418 }
419 }
420
421 if (u->fd >= 0) {
422 struct pollfd *pollfd;
423
424 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
425 pollfd->events =
426 ((u->source && PA_SOURCE_OPENED(u->source->thread_info.state)) ? POLLIN : 0);
427 }
428
429 /* Hmm, nothing to do. Let's sleep */
430 if ((ret = pa_rtpoll_run(u->rtpoll, 1)) < 0)
431 goto fail;
432
433 if (ret == 0)
434 goto finish;
435
436 if (u->fd >= 0) {
437 struct pollfd *pollfd;
438
439 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
440
441 if (pollfd->revents & ~(POLLOUT|POLLIN)) {
442 pa_log("DSP shutdown.");
443 goto fail;
444 }
445
446 revents = pollfd->revents;
447 } else
448 revents = 0;
449 }
450
451 fail:
452 /* We have to continue processing messages until we receive the
453 * SHUTDOWN message */
454 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
455 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
456
457 finish:
458 pa_log_debug("Thread shutting down");
459 }
460
461 static void sig_callback(pa_mainloop_api *api, pa_signal_event*e, int sig, void *userdata) {
462 struct userdata *u = userdata;
463
464 assert(u);
465
466 if (u->sink) {
467 pa_sink_get_volume(u->sink);
468 pa_sink_get_mute(u->sink);
469 }
470
471 if (u->source)
472 pa_source_get_volume(u->source);
473 }
474
475 static int pa_solaris_auto_format(int fd, int mode, pa_sample_spec *ss) {
476 audio_info_t info;
477
478 AUDIO_INITINFO(&info);
479
480 if (mode != O_RDONLY) {
481 info.play.sample_rate = ss->rate;
482 info.play.channels = ss->channels;
483 switch (ss->format) {
484 case PA_SAMPLE_U8:
485 info.play.precision = 8;
486 info.play.encoding = AUDIO_ENCODING_LINEAR;
487 break;
488 case PA_SAMPLE_ALAW:
489 info.play.precision = 8;
490 info.play.encoding = AUDIO_ENCODING_ALAW;
491 break;
492 case PA_SAMPLE_ULAW:
493 info.play.precision = 8;
494 info.play.encoding = AUDIO_ENCODING_ULAW;
495 break;
496 case PA_SAMPLE_S16NE:
497 info.play.precision = 16;
498 info.play.encoding = AUDIO_ENCODING_LINEAR;
499 break;
500 default:
501 return -1;
502 }
503 }
504
505 if (mode != O_WRONLY) {
506 info.record.sample_rate = ss->rate;
507 info.record.channels = ss->channels;
508 switch (ss->format) {
509 case PA_SAMPLE_U8:
510 info.record.precision = 8;
511 info.record.encoding = AUDIO_ENCODING_LINEAR;
512 break;
513 case PA_SAMPLE_ALAW:
514 info.record.precision = 8;
515 info.record.encoding = AUDIO_ENCODING_ALAW;
516 break;
517 case PA_SAMPLE_ULAW:
518 info.record.precision = 8;
519 info.record.encoding = AUDIO_ENCODING_ULAW;
520 break;
521 case PA_SAMPLE_S16NE:
522 info.record.precision = 16;
523 info.record.encoding = AUDIO_ENCODING_LINEAR;
524 break;
525 default:
526 return -1;
527 }
528 }
529
530 if (ioctl(fd, AUDIO_SETINFO, &info) < 0) {
531 if (errno == EINVAL)
532 pa_log("AUDIO_SETINFO: Unsupported sample format.");
533 else
534 pa_log("AUDIO_SETINFO: %s", pa_cstrerror(errno));
535 return -1;
536 }
537
538 return 0;
539 }
540
541 static int pa_solaris_set_buffer(int fd, int buffer_size) {
542 audio_info_t info;
543
544 AUDIO_INITINFO(&info);
545
546 info.play.buffer_size = buffer_size;
547 info.record.buffer_size = buffer_size;
548
549 if (ioctl(fd, AUDIO_SETINFO, &info) < 0) {
550 if (errno == EINVAL)
551 pa_log("AUDIO_SETINFO: Unsupported buffer size.");
552 else
553 pa_log("AUDIO_SETINFO: %s", pa_cstrerror(errno));
554 return -1;
555 }
556
557 return 0;
558 }
559
560 int pa__init(pa_module *m) {
561 struct userdata *u = NULL;
562 const char *p;
563 int fd = -1;
564 int buffer_size;
565 int mode;
566 int record = 1, playback = 1;
567 pa_sample_spec ss;
568 pa_channel_map map;
569 pa_modargs *ma = NULL;
570 char *t;
571 struct pollfd *pollfd;
572
573 pa_assert(m);
574
575 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
576 pa_log("failed to parse module arguments.");
577 goto fail;
578 }
579
580 if (pa_modargs_get_value_boolean(ma, "record", &record) < 0 || pa_modargs_get_value_boolean(ma, "playback", &playback) < 0) {
581 pa_log("record= and playback= expect numeric argument.");
582 goto fail;
583 }
584
585 if (!playback && !record) {
586 pa_log("neither playback nor record enabled for device.");
587 goto fail;
588 }
589
590 mode = (playback&&record) ? O_RDWR : (playback ? O_WRONLY : (record ? O_RDONLY : 0));
591
592 buffer_size = 16384;
593 if (pa_modargs_get_value_s32(ma, "buffer_size", &buffer_size) < 0) {
594 pa_log("failed to parse buffer size argument");
595 goto fail;
596 }
597
598 ss = m->core->default_sample_spec;
599 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
600 pa_log("failed to parse sample specification");
601 goto fail;
602 }
603
604 if ((fd = open(p = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), mode | O_NONBLOCK)) < 0)
605 goto fail;
606
607 pa_log_info("device opened in %s mode.", mode == O_WRONLY ? "O_WRONLY" : (mode == O_RDONLY ? "O_RDONLY" : "O_RDWR"));
608
609 if (pa_solaris_auto_format(fd, mode, &ss) < 0)
610 goto fail;
611
612 if (pa_solaris_set_buffer(fd, buffer_size) < 0)
613 goto fail;
614
615 u = pa_xmalloc(sizeof(struct userdata));
616 u->core = m->core;
617
618 u->fd = fd;
619
620 pa_memchunk_reset(&u->memchunk);
621
622 /* We use this to get a reasonable chunk size */
623 u->page_size = PA_PAGE_SIZE;
624
625 u->frame_size = pa_frame_size(&ss);
626 u->buffer_size = buffer_size;
627
628 u->written_bytes = 0;
629 u->read_bytes = 0;
630
631 u->module = m;
632 m->userdata = u;
633
634 pa_thread_mq_init(&u->thread_mq, m->core->mainloop);
635
636 u->rtpoll = pa_rtpoll_new();
637 pa_rtpoll_item_new_asyncmsgq(u->rtpoll, PA_RTPOLL_EARLY, u->thread_mq.inq);
638
639 pa_rtpoll_set_timer_periodic(u->rtpoll, pa_bytes_to_usec(u->buffer_size / 10, &ss));
640
641 u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
642 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
643 pollfd->fd = fd;
644 pollfd->events = 0;
645 pollfd->revents = 0;
646
647 if (mode != O_WRONLY) {
648 u->source = pa_source_new(m->core, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss, &map);
649 pa_assert(u->source);
650
651 u->source->userdata = u;
652 u->source->parent.process_msg = source_process_msg;
653
654 pa_source_set_module(u->source, m);
655 pa_source_set_description(u->source, t = pa_sprintf_malloc("Solaris PCM on '%s'", p));
656 pa_xfree(t);
657 pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
658 pa_source_set_rtpoll(u->source, u->rtpoll);
659
660 u->source->flags = PA_SOURCE_HARDWARE|PA_SOURCE_LATENCY|PA_SOURCE_HW_VOLUME_CTRL;
661 u->source->refresh_volume = 1;
662 } else
663 u->source = NULL;
664
665 if (mode != O_RDONLY) {
666 u->sink = pa_sink_new(m->core, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map);
667 pa_assert(u->sink);
668
669 u->sink->userdata = u;
670 u->sink->parent.process_msg = sink_process_msg;
671
672 pa_sink_set_module(u->sink, m);
673 pa_sink_set_description(u->sink, t = pa_sprintf_malloc("Solaris PCM on '%s'", p));
674 pa_xfree(t);
675 pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
676 pa_sink_set_rtpoll(u->sink, u->rtpoll);
677
678 u->sink->flags = PA_SINK_HARDWARE|PA_SINK_LATENCY|PA_SINK_HW_VOLUME_CTRL;
679 u->sink->refresh_volume = 1;
680 u->sink->refresh_mute = 1;
681 } else
682 u->sink = NULL;
683
684 pa_assert(u->source || u->sink);
685
686 u->sig = pa_signal_new(SIGPOLL, sig_callback, u);
687 pa_assert(u->sig);
688 ioctl(u->fd, I_SETSIG, S_MSG);
689
690 if (!(u->thread = pa_thread_new(thread_func, u))) {
691 pa_log("Failed to create thread.");
692 goto fail;
693 }
694
695 /* Read mixer settings */
696 if (u->source)
697 pa_asyncmsgq_send(u->thread_mq.inq, PA_MSGOBJECT(u->source), PA_SOURCE_MESSAGE_GET_VOLUME, &u->source->volume, 0, NULL);
698 if (u->sink) {
699 pa_asyncmsgq_send(u->thread_mq.inq, PA_MSGOBJECT(u->sink), PA_SINK_MESSAGE_GET_VOLUME, &u->sink->volume, 0, NULL);
700 pa_asyncmsgq_send(u->thread_mq.inq, PA_MSGOBJECT(u->sink), PA_SINK_MESSAGE_GET_MUTE, &u->sink->muted, 0, NULL);
701 }
702
703 if (u->sink)
704 pa_sink_put(u->sink);
705 if (u->source)
706 pa_source_put(u->source);
707
708 pa_modargs_free(ma);
709
710 return 0;
711
712 fail:
713 if (u)
714 pa__done(m);
715 else if (fd >= 0)
716 close(fd);
717
718 if (ma)
719 pa_modargs_free(ma);
720
721 return -1;
722 }
723
724 void pa__done(pa_module *m) {
725 struct userdata *u;
726
727 pa_assert(m);
728
729 if (!(u = m->userdata))
730 return;
731
732 ioctl(u->fd, I_SETSIG, 0);
733 pa_signal_free(u->sig);
734
735 if (u->sink)
736 pa_sink_unlink(u->sink);
737
738 if (u->source)
739 pa_source_unlink(u->source);
740
741 if (u->thread) {
742 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
743 pa_thread_free(u->thread);
744 }
745
746 pa_thread_mq_done(&u->thread_mq);
747
748 if (u->sink)
749 pa_sink_unref(u->sink);
750
751 if (u->source)
752 pa_source_unref(u->source);
753
754 if (u->memchunk.memblock)
755 pa_memblock_unref(u->memchunk.memblock);
756
757 if (u->rtpoll_item)
758 pa_rtpoll_item_free(u->rtpoll_item);
759
760 if (u->rtpoll)
761 pa_rtpoll_free(u->rtpoll);
762
763 if (u->fd >= 0)
764 close(u->fd);
765
766 pa_xfree(u);
767 }