]> code.delx.au - pulseaudio/blob - src/modules/bluetooth/module-bluetooth-device.c
don't try to recycle rtpoll objects
[pulseaudio] / src / modules / bluetooth / module-bluetooth-device.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2008 Joao Paulo Rechi Vita
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
10
11 PulseAudio 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
17 License along with PulseAudio; 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 #include <string.h>
27 #include <errno.h>
28 #include <poll.h>
29 #include <sys/ioctl.h>
30 #include <linux/sockios.h>
31 #include <arpa/inet.h>
32
33 #include <pulse/xmalloc.h>
34 #include <pulse/timeval.h>
35 #include <pulse/sample.h>
36 #include <pulsecore/module.h>
37 #include <pulsecore/modargs.h>
38 #include <pulsecore/core-util.h>
39 #include <pulsecore/core-error.h>
40 #include <pulsecore/socket-util.h>
41 #include <pulsecore/thread.h>
42 #include <pulsecore/thread-mq.h>
43 #include <pulsecore/rtpoll.h>
44 #include <pulsecore/time-smoother.h>
45 #include <pulsecore/rtclock.h>
46 #include <pulsecore/namereg.h>
47
48 #include <modules/dbus-util.h>
49
50 #include "module-bluetooth-device-symdef.h"
51 #include "ipc.h"
52 #include "sbc.h"
53 #include "rtp.h"
54 #include "bluetooth-util.h"
55
56 #define MAX_BITPOOL 64
57 #define MIN_BITPOOL 2U
58 #define SOL_SCO 17
59 #define SCO_TXBUFS 0x03
60 #define SCO_RXBUFS 0x04
61
62 PA_MODULE_AUTHOR("Joao Paulo Rechi Vita");
63 PA_MODULE_DESCRIPTION("Bluetooth audio sink and source");
64 PA_MODULE_VERSION(PACKAGE_VERSION);
65 PA_MODULE_LOAD_ONCE(FALSE);
66 PA_MODULE_USAGE(
67 "name=<name for the card/sink/source, to be prefixed> "
68 "card_name=<name for the card> "
69 "sink_name=<name for the sink> "
70 "source_name=<name for the source> "
71 "address=<address of the device> "
72 "profile=<a2dp|hsp> "
73 "rate=<sample rate> "
74 "channels=<number of channels> "
75 "path=<device object path> "
76 "sco_sink=<SCO over PCM sink name> "
77 "sco_source=<SCO over PCM source name>");
78
79 static const char* const valid_modargs[] = {
80 "name",
81 "card_name",
82 "sink_name",
83 "source_name",
84 "address",
85 "profile",
86 "rate",
87 "channels",
88 "path",
89 "sco_sink",
90 "sco_source",
91 NULL
92 };
93
94 struct a2dp_info {
95 sbc_capabilities_t sbc_capabilities;
96 sbc_t sbc; /* Codec data */
97 pa_bool_t sbc_initialized; /* Keep track if the encoder is initialized */
98 size_t codesize; /* SBC codesize */
99
100 void* buffer; /* Codec transfer buffer */
101 size_t buffer_size; /* Size of the buffer */
102
103 uint16_t seq_num; /* Cumulative packet sequence */
104 };
105
106 struct hsp_info {
107 pcm_capabilities_t pcm_capabilities;
108 pa_sink *sco_sink;
109 pa_source *sco_source;
110 pa_hook_slot *sink_state_changed_slot;
111 pa_hook_slot *source_state_changed_slot;
112 };
113
114 enum profile {
115 PROFILE_A2DP,
116 PROFILE_HSP,
117 PROFILE_OFF
118 };
119
120 struct userdata {
121 pa_core *core;
122 pa_module *module;
123
124 pa_card *card;
125 pa_sink *sink;
126 pa_source *source;
127
128 pa_thread_mq thread_mq;
129 pa_rtpoll *rtpoll;
130 pa_rtpoll_item *rtpoll_item;
131 pa_thread *thread;
132
133 uint64_t read_index, write_index;
134 pa_usec_t started_at;
135 pa_smoother *read_smoother;
136
137 pa_memchunk write_memchunk;
138
139 pa_sample_spec sample_spec, requested_sample_spec;
140
141 int service_fd;
142 int stream_fd;
143
144 size_t link_mtu;
145 size_t block_size;
146
147 struct a2dp_info a2dp;
148 struct hsp_info hsp;
149 pa_dbus_connection *connection;
150
151 enum profile profile;
152
153 pa_modargs *modargs;
154
155 pa_bluetooth_device *device;
156
157 int stream_write_type, stream_read_type;
158 int service_write_type, service_read_type;
159 };
160
161 static int init_bt(struct userdata *u);
162 static int init_profile(struct userdata *u);
163
164 static int service_send(struct userdata *u, const bt_audio_msg_header_t *msg) {
165 ssize_t r;
166
167 pa_assert(u);
168 pa_assert(u->service_fd >= 0);
169 pa_assert(msg);
170 pa_assert(msg->length > 0);
171
172 pa_log_debug("Sending %s -> %s",
173 pa_strnull(bt_audio_strtype(msg->type)),
174 pa_strnull(bt_audio_strname(msg->name)));
175
176 if ((r = pa_loop_write(u->service_fd, msg, msg->length, &u->service_write_type)) == (ssize_t) msg->length)
177 return 0;
178
179 if (r < 0)
180 pa_log_error("Error sending data to audio service: %s", pa_cstrerror(errno));
181 else
182 pa_log_error("Short write()");
183
184 return -1;
185 }
186
187 static int service_recv(struct userdata *u, bt_audio_msg_header_t *msg, size_t room) {
188 ssize_t r;
189
190 pa_assert(u);
191 pa_assert(u->service_fd >= 0);
192 pa_assert(msg);
193
194 if (room <= 0)
195 room = BT_SUGGESTED_BUFFER_SIZE;
196
197 pa_log_debug("Trying to receive message from audio service...");
198
199 /* First, read the header */
200 if ((r = pa_loop_read(u->service_fd, msg, sizeof(*msg), &u->service_read_type)) != sizeof(*msg))
201 goto read_fail;
202
203 if (msg->length < sizeof(*msg)) {
204 pa_log_error("Invalid message size.");
205 return -1;
206 }
207
208 /* Secondly, read the payload */
209 if (msg->length > sizeof(*msg)) {
210
211 size_t remains = msg->length - sizeof(*msg);
212
213 if ((r = pa_loop_read(u->service_fd,
214 (uint8_t*) msg + sizeof(*msg),
215 remains,
216 &u->service_read_type)) != (ssize_t) remains)
217 goto read_fail;
218 }
219
220 pa_log_debug("Received %s <- %s",
221 pa_strnull(bt_audio_strtype(msg->type)),
222 pa_strnull(bt_audio_strname(msg->name)));
223
224 return 0;
225
226 read_fail:
227
228 if (r < 0)
229 pa_log_error("Error receiving data from audio service: %s", pa_cstrerror(errno));
230 else
231 pa_log_error("Short read()");
232
233 return -1;
234 }
235
236 static ssize_t service_expect(struct userdata*u, bt_audio_msg_header_t *rsp, size_t room, uint8_t expected_name, size_t expected_size) {
237 int r;
238
239 pa_assert(u);
240 pa_assert(u->service_fd >= 0);
241 pa_assert(rsp);
242
243 if ((r = service_recv(u, rsp, room)) < 0)
244 return r;
245
246 if ((rsp->type != BT_INDICATION && rsp->type != BT_RESPONSE) ||
247 rsp->name != expected_name ||
248 (expected_size > 0 && rsp->length != expected_size)) {
249
250 if (rsp->type == BT_ERROR && rsp->length == sizeof(bt_audio_error_t))
251 pa_log_error("Received error condition: %s", pa_cstrerror(((bt_audio_error_t*) rsp)->posix_errno));
252 else
253 pa_log_error("Bogus message %s received while %s was expected",
254 pa_strnull(bt_audio_strname(rsp->name)),
255 pa_strnull(bt_audio_strname(expected_name)));
256 return -1;
257 }
258
259 return 0;
260 }
261
262 static int parse_caps(struct userdata *u, const struct bt_get_capabilities_rsp *rsp) {
263 uint16_t bytes_left;
264 const codec_capabilities_t *codec;
265
266 pa_assert(u);
267 pa_assert(rsp);
268
269 bytes_left = rsp->h.length - sizeof(*rsp);
270
271 if (bytes_left < sizeof(codec_capabilities_t)) {
272 pa_log_error("Packet too small to store codec information.");
273 return -1;
274 }
275
276 codec = (codec_capabilities_t *) rsp->data; /** ALIGNMENT? **/
277
278 pa_log_debug("Payload size is %lu %lu", (unsigned long) bytes_left, (unsigned long) sizeof(*codec));
279
280 if ((u->profile == PROFILE_A2DP && codec->transport != BT_CAPABILITIES_TRANSPORT_A2DP) ||
281 (u->profile == PROFILE_HSP && codec->transport != BT_CAPABILITIES_TRANSPORT_SCO)) {
282 pa_log_error("Got capabilities for wrong codec.");
283 return -1;
284 }
285
286 if (u->profile == PROFILE_HSP) {
287
288 if (bytes_left <= 0 || codec->length != sizeof(u->hsp.pcm_capabilities))
289 return -1;
290
291 pa_assert(codec->type == BT_HFP_CODEC_PCM);
292
293 memcpy(&u->hsp.pcm_capabilities, codec, sizeof(u->hsp.pcm_capabilities));
294
295 } else if (u->profile == PROFILE_A2DP) {
296
297 while (bytes_left > 0) {
298 if (codec->type == BT_A2DP_CODEC_SBC)
299 break;
300
301 bytes_left -= codec->length;
302 codec = (const codec_capabilities_t*) ((const uint8_t*) codec + codec->length);
303 }
304
305 if (bytes_left <= 0 || codec->length != sizeof(u->a2dp.sbc_capabilities))
306 return -1;
307
308 pa_assert(codec->type == BT_A2DP_CODEC_SBC);
309
310 memcpy(&u->a2dp.sbc_capabilities, codec, sizeof(u->a2dp.sbc_capabilities));
311 }
312
313 return 0;
314 }
315
316 static int get_caps(struct userdata *u) {
317 union {
318 struct bt_get_capabilities_req getcaps_req;
319 struct bt_get_capabilities_rsp getcaps_rsp;
320 bt_audio_error_t error;
321 uint8_t buf[BT_SUGGESTED_BUFFER_SIZE];
322 } msg;
323
324 pa_assert(u);
325
326 memset(&msg, 0, sizeof(msg));
327 msg.getcaps_req.h.type = BT_REQUEST;
328 msg.getcaps_req.h.name = BT_GET_CAPABILITIES;
329 msg.getcaps_req.h.length = sizeof(msg.getcaps_req);
330
331 pa_strlcpy(msg.getcaps_req.device, u->device->address, sizeof(msg.getcaps_req.device));
332 if (u->profile == PROFILE_A2DP)
333 msg.getcaps_req.transport = BT_CAPABILITIES_TRANSPORT_A2DP;
334 else {
335 pa_assert(u->profile == PROFILE_HSP);
336 msg.getcaps_req.transport = BT_CAPABILITIES_TRANSPORT_SCO;
337 }
338 msg.getcaps_req.flags = BT_FLAG_AUTOCONNECT;
339
340 if (service_send(u, &msg.getcaps_req.h) < 0)
341 return -1;
342
343 if (service_expect(u, &msg.getcaps_rsp.h, sizeof(msg), BT_GET_CAPABILITIES, 0) < 0)
344 return -1;
345
346 return parse_caps(u, &msg.getcaps_rsp);
347 }
348
349 static uint8_t a2dp_default_bitpool(uint8_t freq, uint8_t mode) {
350
351 switch (freq) {
352 case BT_SBC_SAMPLING_FREQ_16000:
353 case BT_SBC_SAMPLING_FREQ_32000:
354 return 53;
355
356 case BT_SBC_SAMPLING_FREQ_44100:
357
358 switch (mode) {
359 case BT_A2DP_CHANNEL_MODE_MONO:
360 case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL:
361 return 31;
362
363 case BT_A2DP_CHANNEL_MODE_STEREO:
364 case BT_A2DP_CHANNEL_MODE_JOINT_STEREO:
365 return 53;
366
367 default:
368 pa_log_warn("Invalid channel mode %u", mode);
369 return 53;
370 }
371
372 case BT_SBC_SAMPLING_FREQ_48000:
373
374 switch (mode) {
375 case BT_A2DP_CHANNEL_MODE_MONO:
376 case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL:
377 return 29;
378
379 case BT_A2DP_CHANNEL_MODE_STEREO:
380 case BT_A2DP_CHANNEL_MODE_JOINT_STEREO:
381 return 51;
382
383 default:
384 pa_log_warn("Invalid channel mode %u", mode);
385 return 51;
386 }
387
388 default:
389 pa_log_warn("Invalid sampling freq %u", freq);
390 return 53;
391 }
392 }
393
394 static int setup_a2dp(struct userdata *u) {
395 sbc_capabilities_t *cap;
396 int i;
397
398 static const struct {
399 uint32_t rate;
400 uint8_t cap;
401 } freq_table[] = {
402 { 16000U, BT_SBC_SAMPLING_FREQ_16000 },
403 { 32000U, BT_SBC_SAMPLING_FREQ_32000 },
404 { 44100U, BT_SBC_SAMPLING_FREQ_44100 },
405 { 48000U, BT_SBC_SAMPLING_FREQ_48000 }
406 };
407
408 pa_assert(u);
409 pa_assert(u->profile == PROFILE_A2DP);
410
411 cap = &u->a2dp.sbc_capabilities;
412
413 /* Find the lowest freq that is at least as high as the requested
414 * sampling rate */
415 for (i = 0; (unsigned) i < PA_ELEMENTSOF(freq_table); i++)
416 if (freq_table[i].rate >= u->sample_spec.rate && (cap->frequency & freq_table[i].cap)) {
417 u->sample_spec.rate = freq_table[i].rate;
418 cap->frequency = freq_table[i].cap;
419 break;
420 }
421
422 if ((unsigned) i >= PA_ELEMENTSOF(freq_table)) {
423 for (; i >= 0; i--) {
424 if (cap->frequency & freq_table[i].cap) {
425 u->sample_spec.rate = freq_table[i].rate;
426 cap->frequency = freq_table[i].cap;
427 break;
428 }
429 }
430
431 if (i < 0) {
432 pa_log("Not suitable sample rate");
433 return -1;
434 }
435 }
436
437 if (u->sample_spec.channels <= 1) {
438 if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_MONO) {
439 cap->channel_mode = BT_A2DP_CHANNEL_MODE_MONO;
440 u->sample_spec.channels = 1;
441 } else
442 u->sample_spec.channels = 2;
443 }
444
445 if (u->sample_spec.channels >= 2) {
446 u->sample_spec.channels = 2;
447
448 if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_JOINT_STEREO)
449 cap->channel_mode = BT_A2DP_CHANNEL_MODE_JOINT_STEREO;
450 else if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_STEREO)
451 cap->channel_mode = BT_A2DP_CHANNEL_MODE_STEREO;
452 else if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL)
453 cap->channel_mode = BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL;
454 else if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_MONO) {
455 cap->channel_mode = BT_A2DP_CHANNEL_MODE_MONO;
456 u->sample_spec.channels = 1;
457 } else {
458 pa_log("No supported channel modes");
459 return -1;
460 }
461 }
462
463 if (cap->block_length & BT_A2DP_BLOCK_LENGTH_16)
464 cap->block_length = BT_A2DP_BLOCK_LENGTH_16;
465 else if (cap->block_length & BT_A2DP_BLOCK_LENGTH_12)
466 cap->block_length = BT_A2DP_BLOCK_LENGTH_12;
467 else if (cap->block_length & BT_A2DP_BLOCK_LENGTH_8)
468 cap->block_length = BT_A2DP_BLOCK_LENGTH_8;
469 else if (cap->block_length & BT_A2DP_BLOCK_LENGTH_4)
470 cap->block_length = BT_A2DP_BLOCK_LENGTH_4;
471 else {
472 pa_log_error("No supported block lengths");
473 return -1;
474 }
475
476 if (cap->subbands & BT_A2DP_SUBBANDS_8)
477 cap->subbands = BT_A2DP_SUBBANDS_8;
478 else if (cap->subbands & BT_A2DP_SUBBANDS_4)
479 cap->subbands = BT_A2DP_SUBBANDS_4;
480 else {
481 pa_log_error("No supported subbands");
482 return -1;
483 }
484
485 if (cap->allocation_method & BT_A2DP_ALLOCATION_LOUDNESS)
486 cap->allocation_method = BT_A2DP_ALLOCATION_LOUDNESS;
487 else if (cap->allocation_method & BT_A2DP_ALLOCATION_SNR)
488 cap->allocation_method = BT_A2DP_ALLOCATION_SNR;
489
490 cap->min_bitpool = (uint8_t) PA_MAX(MIN_BITPOOL, cap->min_bitpool);
491 cap->max_bitpool = (uint8_t) PA_MIN(a2dp_default_bitpool(cap->frequency, cap->channel_mode), cap->max_bitpool);
492
493 return 0;
494 }
495
496 static void setup_sbc(struct a2dp_info *a2dp) {
497 sbc_capabilities_t *active_capabilities;
498
499 pa_assert(a2dp);
500
501 active_capabilities = &a2dp->sbc_capabilities;
502
503 if (a2dp->sbc_initialized)
504 sbc_reinit(&a2dp->sbc, 0);
505 else
506 sbc_init(&a2dp->sbc, 0);
507 a2dp->sbc_initialized = TRUE;
508
509 switch (active_capabilities->frequency) {
510 case BT_SBC_SAMPLING_FREQ_16000:
511 a2dp->sbc.frequency = SBC_FREQ_16000;
512 break;
513 case BT_SBC_SAMPLING_FREQ_32000:
514 a2dp->sbc.frequency = SBC_FREQ_32000;
515 break;
516 case BT_SBC_SAMPLING_FREQ_44100:
517 a2dp->sbc.frequency = SBC_FREQ_44100;
518 break;
519 case BT_SBC_SAMPLING_FREQ_48000:
520 a2dp->sbc.frequency = SBC_FREQ_48000;
521 break;
522 default:
523 pa_assert_not_reached();
524 }
525
526 switch (active_capabilities->channel_mode) {
527 case BT_A2DP_CHANNEL_MODE_MONO:
528 a2dp->sbc.mode = SBC_MODE_MONO;
529 break;
530 case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL:
531 a2dp->sbc.mode = SBC_MODE_DUAL_CHANNEL;
532 break;
533 case BT_A2DP_CHANNEL_MODE_STEREO:
534 a2dp->sbc.mode = SBC_MODE_STEREO;
535 break;
536 case BT_A2DP_CHANNEL_MODE_JOINT_STEREO:
537 a2dp->sbc.mode = SBC_MODE_JOINT_STEREO;
538 break;
539 default:
540 pa_assert_not_reached();
541 }
542
543 switch (active_capabilities->allocation_method) {
544 case BT_A2DP_ALLOCATION_SNR:
545 a2dp->sbc.allocation = SBC_AM_SNR;
546 break;
547 case BT_A2DP_ALLOCATION_LOUDNESS:
548 a2dp->sbc.allocation = SBC_AM_LOUDNESS;
549 break;
550 default:
551 pa_assert_not_reached();
552 }
553
554 switch (active_capabilities->subbands) {
555 case BT_A2DP_SUBBANDS_4:
556 a2dp->sbc.subbands = SBC_SB_4;
557 break;
558 case BT_A2DP_SUBBANDS_8:
559 a2dp->sbc.subbands = SBC_SB_8;
560 break;
561 default:
562 pa_assert_not_reached();
563 }
564
565 switch (active_capabilities->block_length) {
566 case BT_A2DP_BLOCK_LENGTH_4:
567 a2dp->sbc.blocks = SBC_BLK_4;
568 break;
569 case BT_A2DP_BLOCK_LENGTH_8:
570 a2dp->sbc.blocks = SBC_BLK_8;
571 break;
572 case BT_A2DP_BLOCK_LENGTH_12:
573 a2dp->sbc.blocks = SBC_BLK_12;
574 break;
575 case BT_A2DP_BLOCK_LENGTH_16:
576 a2dp->sbc.blocks = SBC_BLK_16;
577 break;
578 default:
579 pa_assert_not_reached();
580 }
581
582 a2dp->sbc.bitpool = active_capabilities->max_bitpool;
583 a2dp->codesize = (uint16_t) sbc_get_codesize(&a2dp->sbc);
584 }
585
586 static int set_conf(struct userdata *u) {
587 union {
588 struct bt_set_configuration_req setconf_req;
589 struct bt_set_configuration_rsp setconf_rsp;
590 bt_audio_error_t error;
591 uint8_t buf[BT_SUGGESTED_BUFFER_SIZE];
592 } msg;
593
594 if (u->profile == PROFILE_A2DP ) {
595 u->sample_spec.format = PA_SAMPLE_S16LE;
596
597 if (setup_a2dp(u) < 0)
598 return -1;
599 } else {
600 pa_assert(u->profile == PROFILE_HSP);
601
602 u->sample_spec.format = PA_SAMPLE_S16LE;
603 u->sample_spec.channels = 1;
604 u->sample_spec.rate = 8000;
605 }
606
607 memset(&msg, 0, sizeof(msg));
608 msg.setconf_req.h.type = BT_REQUEST;
609 msg.setconf_req.h.name = BT_SET_CONFIGURATION;
610 msg.setconf_req.h.length = sizeof(msg.setconf_req);
611
612 pa_strlcpy(msg.setconf_req.device, u->device->address, sizeof(msg.setconf_req.device));
613 msg.setconf_req.access_mode = u->profile == PROFILE_A2DP ? BT_CAPABILITIES_ACCESS_MODE_WRITE : BT_CAPABILITIES_ACCESS_MODE_READWRITE;
614
615 msg.setconf_req.codec.transport = u->profile == PROFILE_A2DP ? BT_CAPABILITIES_TRANSPORT_A2DP : BT_CAPABILITIES_TRANSPORT_SCO;
616
617 if (u->profile == PROFILE_A2DP) {
618 memcpy(&msg.setconf_req.codec, &u->a2dp.sbc_capabilities, sizeof(u->a2dp.sbc_capabilities));
619 msg.setconf_req.h.length += msg.setconf_req.codec.length - sizeof(msg.setconf_req.codec);
620 }
621
622 if (service_send(u, &msg.setconf_req.h) < 0)
623 return -1;
624
625 if (service_expect(u, &msg.setconf_rsp.h, sizeof(msg), BT_SET_CONFIGURATION, sizeof(msg.setconf_rsp)) < 0)
626 return -1;
627
628 if ((u->profile == PROFILE_A2DP && msg.setconf_rsp.transport != BT_CAPABILITIES_TRANSPORT_A2DP) ||
629 (u->profile == PROFILE_HSP && msg.setconf_rsp.transport != BT_CAPABILITIES_TRANSPORT_SCO)) {
630 pa_log("Transport doesn't match what we requested.");
631 return -1;
632 }
633
634 if ((u->profile == PROFILE_A2DP && msg.setconf_rsp.access_mode != BT_CAPABILITIES_ACCESS_MODE_WRITE) ||
635 (u->profile == PROFILE_HSP && msg.setconf_rsp.access_mode != BT_CAPABILITIES_ACCESS_MODE_READWRITE)) {
636 pa_log("Access mode doesn't match what we requested.");
637 return -1;
638 }
639
640 u->link_mtu = msg.setconf_rsp.link_mtu;
641
642 /* setup SBC encoder now we agree on parameters */
643 if (u->profile == PROFILE_A2DP) {
644 setup_sbc(&u->a2dp);
645 u->block_size = u->a2dp.codesize;
646 pa_log_info("SBC parameters:\n\tallocation=%u\n\tsubbands=%u\n\tblocks=%u\n\tbitpool=%u\n",
647 u->a2dp.sbc.allocation, u->a2dp.sbc.subbands, u->a2dp.sbc.blocks, u->a2dp.sbc.bitpool);
648 } else
649 u->block_size = u->link_mtu;
650
651 return 0;
652 }
653
654 static int setup_stream_fd(struct userdata *u) {
655 union {
656 bt_audio_msg_header_t rsp;
657 struct bt_start_stream_req start_req;
658 struct bt_start_stream_rsp start_rsp;
659 struct bt_new_stream_ind streamfd_ind;
660 bt_audio_error_t error;
661 uint8_t buf[BT_SUGGESTED_BUFFER_SIZE];
662 } msg;
663
664 pa_assert(u);
665 pa_assert(u->stream_fd < 0);
666
667 memset(msg.buf, 0, BT_SUGGESTED_BUFFER_SIZE);
668 msg.start_req.h.type = BT_REQUEST;
669 msg.start_req.h.name = BT_START_STREAM;
670 msg.start_req.h.length = sizeof(msg.start_req);
671
672 if (service_send(u, &msg.start_req.h) < 0)
673 return -1;
674
675 if (service_expect(u, &msg.rsp, sizeof(msg), BT_START_STREAM, sizeof(msg.start_rsp)) < 0)
676 return -1;
677
678 if (service_expect(u, &msg.rsp, sizeof(msg), BT_NEW_STREAM, sizeof(msg.streamfd_ind)) < 0)
679 return -1;
680
681 if ((u->stream_fd = bt_audio_service_get_data_fd(u->service_fd)) < 0) {
682 pa_log("Failed to get stream fd from audio service.");
683 return -1;
684 }
685
686 /* setsockopt(u->stream_fd, SOL_SCO, SCO_TXBUFS, &period_count, sizeof(period_count)); */
687 /* setsockopt(u->stream_fd, SOL_SCO, SCO_SNDBUF, &period_count, sizeof(period_count)); */
688
689 pa_make_fd_nonblock(u->stream_fd);
690 pa_make_socket_low_delay(u->stream_fd);
691
692 return 0;
693 }
694
695 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
696 struct userdata *u = PA_SINK(o)->userdata;
697 pa_assert(u->sink == PA_SINK(o));
698
699 pa_log_debug("got message: %d", code);
700 switch (code) {
701
702 case PA_SINK_MESSAGE_SET_STATE:
703
704 switch ((pa_sink_state_t) PA_PTR_TO_UINT(data)) {
705
706 case PA_SINK_SUSPENDED:
707 pa_assert(PA_SINK_IS_OPENED(u->sink->thread_info.state));
708 break;
709
710 case PA_SINK_IDLE:
711 case PA_SINK_RUNNING:
712 if (!PA_SINK_IS_OPENED(u->sink->thread_info.state))
713 u->started_at = pa_rtclock_usec();
714
715 break;
716
717 case PA_SINK_UNLINKED:
718 case PA_SINK_INIT:
719 case PA_SINK_INVALID_STATE:
720 ;
721 }
722 break;
723
724 case PA_SINK_MESSAGE_GET_LATENCY: {
725 *((pa_usec_t*) data) = 0;
726 return 0;
727 }
728
729 }
730
731 return pa_sink_process_msg(o, code, data, offset, chunk);
732 }
733
734 static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
735 struct userdata *u = PA_SOURCE(o)->userdata;
736
737 pa_assert(u->source == PA_SOURCE(o));
738
739 pa_log_debug("got message: %d", code);
740 switch (code) {
741
742 case PA_SOURCE_MESSAGE_SET_STATE:
743
744 switch ((pa_source_state_t) PA_PTR_TO_UINT(data)) {
745
746 case PA_SOURCE_SUSPENDED:
747 pa_smoother_pause(u->read_smoother, pa_rtclock_usec());
748 break;
749
750 case PA_SOURCE_IDLE:
751 case PA_SOURCE_RUNNING:
752 if (u->source->thread_info.state == PA_SOURCE_SUSPENDED)
753 pa_smoother_resume(u->read_smoother, pa_rtclock_usec());
754 break;
755
756 case PA_SOURCE_UNLINKED:
757 case PA_SOURCE_INIT:
758 case PA_SOURCE_INVALID_STATE:
759 ;
760 }
761 break;
762
763 case PA_SOURCE_MESSAGE_GET_LATENCY: {
764 *((pa_usec_t*) data) = 0;
765 return 0;
766 }
767
768 }
769
770 return pa_source_process_msg(o, code, data, offset, chunk);
771 }
772
773 static int hsp_process_render(struct userdata *u) {
774 int ret = 0;
775 pa_memchunk memchunk;
776
777 pa_assert(u);
778 pa_assert(u->profile == PROFILE_HSP);
779 pa_assert(u->sink);
780
781 pa_sink_render_full(u->sink, u->block_size, &memchunk);
782
783 for (;;) {
784 ssize_t l;
785 const void *p;
786
787 p = (const uint8_t*) pa_memblock_acquire(memchunk.memblock) + memchunk.index;
788 l = pa_write(u->stream_fd, p, memchunk.length, &u->stream_write_type);
789 pa_memblock_release(memchunk.memblock);
790
791 pa_log_debug("Memblock written to socket: %lli bytes", (long long) l);
792
793 pa_assert(l != 0);
794
795 if (l < 0) {
796 if (errno == EINTR)
797 continue;
798 else {
799 pa_log_error("Failed to write data to SCO socket: %s", pa_cstrerror(errno));
800 ret = -1;
801 break;
802 }
803 } else {
804 pa_assert((size_t) l <= memchunk.length);
805
806 memchunk.index += (size_t) l;
807 memchunk.length -= (size_t) l;
808
809 u->write_index += (uint64_t) l;
810
811 if (memchunk.length <= 0)
812 break;
813 }
814 }
815
816 pa_memblock_unref(memchunk.memblock);
817
818 return ret;
819 }
820
821 static int hsp_process_push(struct userdata *u) {
822 int ret = 0;
823 pa_memchunk memchunk;
824
825 pa_assert(u);
826 pa_assert(u->profile == PROFILE_HSP);
827 pa_assert(u->source);
828
829 memchunk.memblock = pa_memblock_new(u->core->mempool, u->block_size);
830 memchunk.index = memchunk.length = 0;
831
832 for (;;) {
833 ssize_t l;
834 void *p;
835
836 p = pa_memblock_acquire(memchunk.memblock);
837 l = pa_read(u->stream_fd, p, pa_memblock_get_length(memchunk.memblock), &u->stream_read_type);
838 pa_memblock_release(memchunk.memblock);
839
840 if (l <= 0) {
841 if (l < 0 && errno == EINTR)
842 continue;
843 else {
844 pa_log_error("Failed to read data from SCO socket: %s", ret < 0 ? pa_cstrerror(errno) : "EOF");
845 ret = -1;
846 break;
847 }
848 } else {
849 memchunk.length = (size_t) l;
850 u->read_index += (uint64_t) l;
851
852 pa_source_post(u->source, &memchunk);
853 break;
854 }
855 }
856
857 pa_memblock_unref(memchunk.memblock);
858
859 return ret;
860 }
861
862 static int a2dp_process_render(struct userdata *u) {
863 size_t frame_size;
864 struct a2dp_info *a2dp;
865 struct rtp_header *header;
866 struct rtp_payload *payload;
867 size_t left;
868 void *d;
869 const void *p;
870 unsigned frame_count;
871 int written;
872 uint64_t writing_at;
873
874 pa_assert(u);
875 pa_assert(u->profile == PROFILE_A2DP);
876 pa_assert(u->sink);
877
878 a2dp = &u->a2dp;
879
880 if (a2dp->buffer_size < u->link_mtu) {
881 a2dp->buffer_size = 2*u->link_mtu;
882 pa_xfree(a2dp->buffer);
883 a2dp->buffer = pa_xmalloc(a2dp->buffer_size);
884 }
885
886 header = (struct rtp_header*) a2dp->buffer;
887 payload = (struct rtp_payload*) ((uint8_t*) a2dp->buffer + sizeof(*header));
888 d = (uint8_t*) a2dp->buffer + sizeof(*header) + sizeof(*payload);
889 left = a2dp->buffer_size - sizeof(*header) - sizeof(*payload);
890
891 frame_size = sbc_get_frame_length(&a2dp->sbc);
892 frame_count = 0;
893
894 writing_at = u->write_index;
895
896 do {
897 int encoded;
898
899 if (!u->write_memchunk.memblock)
900 pa_sink_render_full(u->sink, u->block_size, &u->write_memchunk);
901
902 p = (const uint8_t*) pa_memblock_acquire(u->write_memchunk.memblock) + u->write_memchunk.index;
903 encoded = sbc_encode(&a2dp->sbc,
904 (void*) p, u->write_memchunk.length,
905 d, left,
906 &written);
907 pa_memblock_release(u->write_memchunk.memblock);
908
909 if (encoded <= 0) {
910 pa_log_error("SBC encoding error (%d)", encoded);
911 return -1;
912 }
913
914 pa_assert(written >= 0);
915
916 pa_assert((size_t) encoded <= u->write_memchunk.length);
917 pa_assert((size_t) written <= left);
918
919 /* pa_log_debug("SBC: encoded: %d; written: %d", encoded, written); */
920
921 u->write_memchunk.index += encoded;
922 u->write_memchunk.length -= encoded;
923
924 if (u->write_memchunk.length <= 0) {
925 pa_memblock_unref(u->write_memchunk.memblock);
926 pa_memchunk_reset(&u->write_memchunk);
927 }
928
929 u->write_index += encoded;
930
931 d = (uint8_t*) d + written;
932 left -= written;
933
934 frame_count++;
935
936 } while ((uint8_t*) d - (uint8_t*) a2dp->buffer + written < (ptrdiff_t) u->link_mtu);
937
938 /* write it to the fifo */
939 memset(a2dp->buffer, 0, sizeof(*header) + sizeof(*payload));
940 payload->frame_count = frame_count;
941 header->v = 2;
942 header->pt = 1;
943 header->sequence_number = htons(a2dp->seq_num++);
944 header->timestamp = htonl(writing_at / frame_size);
945 header->ssrc = htonl(1);
946
947 p = a2dp->buffer;
948 left = (uint8_t*) d - (uint8_t*) a2dp->buffer;
949
950 for (;;) {
951 ssize_t l;
952
953 l = pa_write(u->stream_fd, p, left, &u->stream_write_type);
954 /* pa_log_debug("write: requested %lu bytes; written %li bytes; mtu=%li", (unsigned long) left, (long) l, (unsigned long) u->link_mtu); */
955
956 pa_assert(l != 0);
957
958 if (l < 0) {
959 if (errno == EINTR)
960 continue;
961 else {
962 pa_log_error("Failed to write data to socket: %s", pa_cstrerror(errno));
963 return -1;
964 }
965 } else {
966 pa_assert((size_t) l <= left);
967
968 d = (uint8_t*) d + l;
969 left -= l;
970
971 if (left <= 0)
972 break;
973 }
974 }
975
976 return 0;
977 }
978
979 static void thread_func(void *userdata) {
980 struct userdata *u = userdata;
981 pa_bool_t do_write = FALSE, writable = FALSE;
982
983 pa_assert(u);
984
985 pa_log_debug("IO Thread starting up");
986
987 if (u->core->realtime_scheduling)
988 pa_make_realtime(u->core->realtime_priority);
989
990 pa_thread_mq_install(&u->thread_mq);
991 pa_rtpoll_install(u->rtpoll);
992
993 pa_smoother_set_time_offset(u->read_smoother, pa_rtclock_usec());
994
995 for (;;) {
996 struct pollfd *pollfd;
997 int ret;
998
999 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
1000
1001 if (u->source && PA_SOURCE_IS_LINKED(u->source->thread_info.state)) {
1002
1003 if (pollfd->revents & POLLIN) {
1004
1005 if (hsp_process_push(u) < 0)
1006 goto fail;
1007
1008 /* We just read something, so we are supposed to write something, too */
1009 do_write = TRUE;
1010 }
1011 }
1012
1013 if (u->sink && PA_SINK_IS_LINKED(u->sink->thread_info.state)) {
1014
1015 if (u->sink->thread_info.rewind_requested)
1016 pa_sink_process_rewind(u->sink, 0);
1017
1018 if (pollfd->revents & POLLOUT)
1019 writable = TRUE;
1020
1021 if ((!u->source || !PA_SOURCE_IS_LINKED(u->source->thread_info.state)) && !do_write && writable) {
1022 pa_usec_t time_passed;
1023 uint64_t should_have_written;
1024
1025 /* Hmm, there is no input stream we could synchronize
1026 * to. So let's do things by time */
1027
1028 time_passed = pa_rtclock_usec() - u->started_at;
1029 should_have_written = pa_usec_to_bytes(time_passed, &u->sink->sample_spec);
1030
1031 do_write = u->write_index <= should_have_written ;
1032 /* pa_log_debug("Time has come: %s", pa_yes_no(do_write)); */
1033 }
1034
1035 if (writable && do_write) {
1036
1037 if (u->profile == PROFILE_A2DP) {
1038 if (a2dp_process_render(u) < 0)
1039 goto fail;
1040 } else {
1041 if (hsp_process_render(u) < 0)
1042 goto fail;
1043 }
1044
1045 do_write = FALSE;
1046 writable = FALSE;
1047 }
1048
1049 if ((!u->source || !PA_SOURCE_IS_LINKED(u->source->thread_info.state)) && !do_write) {
1050 pa_usec_t time_passed, next_write_at, sleep_for;
1051
1052 /* Hmm, there is no input stream we could synchronize
1053 * to. So let's estimate when we need to wake up the latest */
1054
1055 time_passed = pa_rtclock_usec() - u->started_at;
1056 next_write_at = pa_bytes_to_usec(u->write_index, &u->sink->sample_spec);
1057 sleep_for = time_passed < next_write_at ? next_write_at - time_passed : 0;
1058
1059 /* pa_log("Sleeping for %lu; time passed %lu, next write at %lu", (unsigned long) sleep_for, (unsigned long) time_passed, (unsigned long)next_write_at); */
1060
1061 pa_rtpoll_set_timer_relative(u->rtpoll, sleep_for);
1062 }
1063 } else
1064 pa_rtpoll_set_timer_disabled(u->rtpoll);
1065
1066 /* Hmm, nothing to do. Let's sleep */
1067 pollfd->events = (short) (((u->sink && PA_SINK_IS_OPENED(u->sink->thread_info.state) && !writable) ? POLLOUT : 0) |
1068 (u->source && PA_SOURCE_IS_OPENED(u->source->thread_info.state) ? POLLIN : 0));
1069
1070 if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
1071 goto fail;
1072
1073 if (ret == 0)
1074 goto finish;
1075
1076 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
1077
1078 if (pollfd->revents & ~(POLLOUT|POLLIN)) {
1079 pa_log_error("FD error.");
1080 goto fail;
1081 }
1082 }
1083
1084 fail:
1085 /* If this was no regular exit from the loop we have to continue processing messages until we receive PA_MESSAGE_SHUTDOWN */
1086 pa_log_debug("IO thread failed");
1087 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
1088 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
1089
1090 finish:
1091 pa_log_debug("IO thread shutting down");
1092 }
1093
1094 /* static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *msg, void *userdata) { */
1095 /* DBusMessageIter arg_i; */
1096 /* DBusError err; */
1097 /* const char *value; */
1098 /* struct userdata *u; */
1099
1100 /* pa_assert(bus); */
1101 /* pa_assert(msg); */
1102 /* pa_assert(userdata); */
1103 /* u = userdata; */
1104
1105 /* pa_log_debug("dbus: interface=%s, path=%s, member=%s\n", */
1106 /* dbus_message_get_interface(msg), */
1107 /* dbus_message_get_path(msg), */
1108 /* dbus_message_get_member(msg)); */
1109
1110 /* dbus_error_init(&err); */
1111
1112 /* if (!dbus_message_has_path(msg, u->path)) */
1113 /* goto done; */
1114
1115 /* if (dbus_message_is_signal(msg, "org.bluez.Headset", "PropertyChanged") || */
1116 /* dbus_message_is_signal(msg, "org.bluez.AudioSink", "PropertyChanged")) { */
1117
1118 /* struct device *d; */
1119 /* const char *profile; */
1120 /* DBusMessageIter variant_i; */
1121 /* dbus_uint16_t gain; */
1122
1123 /* if (!dbus_message_iter_init(msg, &arg_i)) { */
1124 /* pa_log("dbus: message has no parameters"); */
1125 /* goto done; */
1126 /* } */
1127
1128 /* if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_STRING) { */
1129 /* pa_log("Property name not a string."); */
1130 /* goto done; */
1131 /* } */
1132
1133 /* dbus_message_iter_get_basic(&arg_i, &value); */
1134
1135 /* if (!dbus_message_iter_next(&arg_i)) { */
1136 /* pa_log("Property value missing"); */
1137 /* goto done; */
1138 /* } */
1139
1140 /* if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_VARIANT) { */
1141 /* pa_log("Property value not a variant."); */
1142 /* goto done; */
1143 /* } */
1144
1145 /* dbus_message_iter_recurse(&arg_i, &variant_i); */
1146
1147 /* if (dbus_message_iter_get_arg_type(&variant_i) != DBUS_TYPE_UINT16) { */
1148 /* dbus_message_iter_get_basic(&variant_i, &gain); */
1149
1150 /* if (pa_streq(value, "SpeakerGain")) { */
1151 /* pa_log("spk gain: %d", gain); */
1152 /* pa_cvolume_set(&u->sink->virtual_volume, 1, (pa_volume_t) (gain * PA_VOLUME_NORM / 15)); */
1153 /* pa_subscription_post(u->sink->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, u->sink->index); */
1154 /* } else { */
1155 /* pa_log("mic gain: %d", gain); */
1156 /* if (!u->source) */
1157 /* goto done; */
1158
1159 /* pa_cvolume_set(&u->source->virtual_volume, 1, (pa_volume_t) (gain * PA_VOLUME_NORM / 15)); */
1160 /* pa_subscription_post(u->source->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, u->source->index); */
1161 /* } */
1162 /* } */
1163 /* } */
1164
1165 /* done: */
1166 /* dbus_error_free(&err); */
1167 /* return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; */
1168 /* } */
1169
1170 /* static int sink_get_volume_cb(pa_sink *s) { */
1171 /* struct userdata *u = s->userdata; */
1172 /* pa_assert(u); */
1173
1174 /* /\* refresh? *\/ */
1175
1176 /* return 0; */
1177 /* } */
1178
1179 /* static int source_get_volume_cb(pa_source *s) { */
1180 /* struct userdata *u = s->userdata; */
1181 /* pa_assert(u); */
1182
1183 /* /\* refresh? *\/ */
1184
1185 /* return 0; */
1186 /* } */
1187
1188 /* static int sink_set_volume_cb(pa_sink *s) { */
1189 /* DBusError e; */
1190 /* DBusMessage *m, *r; */
1191 /* DBusMessageIter it, itvar; */
1192 /* dbus_uint16_t vol; */
1193 /* const char *spkgain = "SpeakerGain"; */
1194 /* struct userdata *u = s->userdata; */
1195 /* pa_assert(u); */
1196
1197 /* dbus_error_init(&e); */
1198
1199 /* vol = ((float) pa_cvolume_max(&s->virtual_volume) / PA_VOLUME_NORM) * 15; */
1200 /* pa_log_debug("set headset volume: %d", vol); */
1201
1202 /* pa_assert_se(m = dbus_message_new_method_call("org.bluez", u->path, "org.bluez.Headset", "SetProperty")); */
1203 /* dbus_message_iter_init_append(m, &it); */
1204 /* dbus_message_iter_append_basic(&it, DBUS_TYPE_STRING, &spkgain); */
1205 /* dbus_message_iter_open_container(&it, DBUS_TYPE_VARIANT, DBUS_TYPE_UINT16_AS_STRING, &itvar); */
1206 /* dbus_message_iter_append_basic(&itvar, DBUS_TYPE_UINT16, &vol); */
1207 /* dbus_message_iter_close_container(&it, &itvar); */
1208
1209 /* r = dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(u->conn), m, -1, &e); */
1210
1211 /* finish: */
1212 /* if (m) */
1213 /* dbus_message_unref(m); */
1214 /* if (r) */
1215 /* dbus_message_unref(r); */
1216
1217 /* dbus_error_free(&e); */
1218
1219 /* return 0; */
1220 /* } */
1221
1222 /* static int source_set_volume_cb(pa_source *s) { */
1223 /* dbus_uint16_t vol; */
1224 /* struct userdata *u = s->userdata; */
1225 /* pa_assert(u); */
1226
1227 /* vol = ((float)pa_cvolume_max(&s->virtual_volume) / PA_VOLUME_NORM) * 15; */
1228
1229 /* pa_log_debug("set headset mic volume: %d (not implemented yet)", vol); */
1230
1231 /* return 0; */
1232 /* } */
1233
1234 static char *get_name(const char *type, pa_modargs *ma, const char *device_id, pa_bool_t *namereg_fail) {
1235 char *t;
1236 const char *n;
1237
1238 pa_assert(type);
1239 pa_assert(ma);
1240 pa_assert(device_id);
1241 pa_assert(namereg_fail);
1242
1243 t = pa_sprintf_malloc("%s_name", type);
1244 n = pa_modargs_get_value(ma, t, NULL);
1245 pa_xfree(t);
1246
1247 if (n) {
1248 *namereg_fail = TRUE;
1249 return pa_xstrdup(n);
1250 }
1251
1252 if ((n = pa_modargs_get_value(ma, "name", NULL)))
1253 *namereg_fail = TRUE;
1254 else {
1255 n = device_id;
1256 *namereg_fail = FALSE;
1257 }
1258
1259 return pa_sprintf_malloc("bluez_%s.%s", type, n);
1260 }
1261
1262 #define USE_SCO_OVER_PCM(u) (u->profile == PROFILE_HSP && (u->hsp.sco_sink && u->hsp.sco_source))
1263
1264 static void sco_over_pcm_state_update(struct userdata *u) {
1265 pa_assert(u);
1266 pa_assert(USE_SCO_OVER_PCM(u));
1267
1268 if (PA_SINK_IS_OPENED(pa_sink_get_state(u->hsp.sco_sink)) ||
1269 PA_SOURCE_IS_OPENED(pa_source_get_state(u->hsp.sco_source))) {
1270
1271 if (u->service_fd > 0)
1272 return;
1273
1274 pa_log_debug("Resuming SCO over PCM");
1275 if ((init_bt(u) < 0) || (init_profile(u) < 0))
1276 pa_log("Can't resume SCO over PCM");
1277
1278 } else {
1279
1280 if (u->service_fd <= 0)
1281 return;
1282
1283 pa_log_debug("Closing SCO over PCM");
1284 pa_close(u->service_fd);
1285 u->service_fd = -1;
1286 }
1287 }
1288
1289 static pa_hook_result_t sink_state_changed_cb(pa_core *c, pa_sink *s, struct userdata *u) {
1290 pa_assert(c);
1291 pa_sink_assert_ref(s);
1292 pa_assert(u);
1293
1294 if (s != u->hsp.sco_sink)
1295 return PA_HOOK_OK;
1296
1297 sco_over_pcm_state_update(u);
1298
1299 return PA_HOOK_OK;
1300 }
1301
1302 static pa_hook_result_t source_state_changed_cb(pa_core *c, pa_source *s, struct userdata *u) {
1303 pa_assert(c);
1304 pa_source_assert_ref(s);
1305 pa_assert(u);
1306
1307 if (s != u->hsp.sco_source)
1308 return PA_HOOK_OK;
1309
1310 sco_over_pcm_state_update(u);
1311
1312 return PA_HOOK_OK;
1313 }
1314
1315 static int add_sink(struct userdata *u) {
1316
1317 if (USE_SCO_OVER_PCM(u)) {
1318 pa_proplist *p;
1319
1320 u->sink = u->hsp.sco_sink;
1321 p = pa_proplist_new();
1322 pa_proplist_sets(p, "bluetooth.protocol", "sco");
1323 pa_proplist_update(u->sink->proplist, PA_UPDATE_MERGE, p);
1324 pa_proplist_free(p);
1325
1326 if (!u->hsp.sink_state_changed_slot)
1327 u->hsp.sink_state_changed_slot = pa_hook_connect(&u->core->hooks[PA_CORE_HOOK_SINK_STATE_CHANGED], PA_HOOK_NORMAL, (pa_hook_cb_t) sink_state_changed_cb, u);
1328
1329 } else {
1330 pa_sink_new_data data;
1331 pa_bool_t b;
1332
1333 pa_sink_new_data_init(&data);
1334 data.driver = __FILE__;
1335 data.module = u->module;
1336 pa_sink_new_data_set_sample_spec(&data, &u->sample_spec);
1337 pa_proplist_sets(data.proplist, "bluetooth.protocol", u->profile == PROFILE_A2DP ? "a2dp" : "sco");
1338 data.card = u->card;
1339 data.name = get_name("sink", u->modargs, u->device->address, &b);
1340 data.namereg_fail = b;
1341
1342 u->sink = pa_sink_new(u->core, &data, PA_SINK_HARDWARE|PA_SINK_LATENCY);
1343 pa_sink_new_data_done(&data);
1344
1345 if (!u->sink) {
1346 pa_log_error("Failed to create sink");
1347 return -1;
1348 }
1349
1350 u->sink->userdata = u;
1351 u->sink->parent.process_msg = sink_process_msg;
1352
1353 pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
1354 pa_sink_set_rtpoll(u->sink, u->rtpoll);
1355 }
1356
1357 /* u->sink->get_volume = sink_get_volume_cb; */
1358 /* u->sink->set_volume = sink_set_volume_cb; */
1359
1360 return 0;
1361 }
1362
1363 static int add_source(struct userdata *u) {
1364 pa_proplist *p;
1365
1366 if (USE_SCO_OVER_PCM(u)) {
1367 u->source = u->hsp.sco_source;
1368 p = pa_proplist_new();
1369 pa_proplist_sets(p, "bluetooth.protocol", "sco");
1370 pa_proplist_update(u->source->proplist, PA_UPDATE_MERGE, p);
1371 pa_proplist_free(p);
1372
1373 if (!u->hsp.source_state_changed_slot)
1374 u->hsp.source_state_changed_slot = pa_hook_connect(&u->core->hooks[PA_CORE_HOOK_SOURCE_STATE_CHANGED], PA_HOOK_NORMAL, (pa_hook_cb_t) source_state_changed_cb, u);
1375
1376 } else {
1377 pa_source_new_data data;
1378 pa_bool_t b;
1379
1380 pa_source_new_data_init(&data);
1381 data.driver = __FILE__;
1382 data.module = u->module;
1383 pa_source_new_data_set_sample_spec(&data, &u->sample_spec);
1384 pa_proplist_sets(data.proplist, "bluetooth.protocol", u->profile == PROFILE_A2DP ? "a2dp" : "sco");
1385 data.card = u->card;
1386 data.name = get_name("source", u->modargs, u->device->address, &b);
1387 data.namereg_fail = b;
1388
1389 u->source = pa_source_new(u->core, &data, PA_SOURCE_HARDWARE|PA_SOURCE_LATENCY);
1390 pa_source_new_data_done(&data);
1391
1392 if (!u->source) {
1393 pa_log_error("Failed to create source");
1394 return -1;
1395 }
1396
1397 u->source->userdata = u;
1398 u->source->parent.process_msg = source_process_msg;
1399
1400 pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
1401 pa_source_set_rtpoll(u->source, u->rtpoll);
1402 }
1403
1404 /* u->source->get_volume = source_get_volume_cb; */
1405 /* u->source->set_volume = source_set_volume_cb; */
1406
1407 p = pa_proplist_new();
1408 pa_proplist_sets(p, "bluetooth.nrec", pa_yes_no(u->hsp.pcm_capabilities.flags & BT_PCM_FLAG_NREC));
1409 pa_proplist_update(u->source->proplist, PA_UPDATE_MERGE, p);
1410 pa_proplist_free(p);
1411
1412 return 0;
1413 }
1414
1415 static void shutdown_bt(struct userdata *u) {
1416 pa_assert(u);
1417
1418 if (u->stream_fd >= 0) {
1419 pa_close(u->stream_fd);
1420 u->stream_fd = -1;
1421 }
1422
1423 if (u->service_fd >= 0) {
1424 pa_close(u->service_fd);
1425 u->service_fd = -1;
1426 }
1427 }
1428
1429 static int init_bt(struct userdata *u) {
1430 pa_assert(u);
1431
1432 shutdown_bt(u);
1433
1434 u->stream_write_type = u->stream_read_type = 0;
1435 u->service_write_type = u->service_write_type = 0;
1436
1437 if ((u->service_fd = bt_audio_service_open()) < 0) {
1438 pa_log_error("Couldn't connect to bluetooth audio service");
1439 return -1;
1440 }
1441
1442 pa_log_debug("Connected to the bluetooth audio service");
1443
1444 return 0;
1445 }
1446
1447 static int setup_bt(struct userdata *u) {
1448 pa_assert(u);
1449
1450 if (get_caps(u) < 0)
1451 return -1;
1452
1453 pa_log_debug("Got device capabilities");
1454
1455 if (set_conf(u) < 0)
1456 return -1;
1457
1458 pa_log_debug("Connection to the device configured");
1459
1460 if (USE_SCO_OVER_PCM(u)) {
1461 pa_log_debug("Configured to use SCO over PCM");
1462 return 0;
1463 }
1464
1465 if (setup_stream_fd(u) < 0)
1466 return -1;
1467
1468 pa_log_debug("Got the stream socket");
1469
1470 return 0;
1471 }
1472
1473 static int init_profile(struct userdata *u) {
1474 int r = 0;
1475 pa_assert(u);
1476
1477 if (setup_bt(u) < 0)
1478 return -1;
1479
1480 if (u->profile == PROFILE_A2DP ||
1481 u->profile == PROFILE_HSP)
1482 if (add_sink(u) < 0)
1483 r = -1;
1484
1485 if (u->profile == PROFILE_HSP)
1486 if (add_source(u) < 0)
1487 r = -1;
1488
1489 return r;
1490 }
1491
1492 static void stop_thread(struct userdata *u) {
1493 pa_assert(u);
1494
1495 if (u->thread) {
1496 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
1497 pa_thread_free(u->thread);
1498 u->thread = NULL;
1499 }
1500
1501 if (u->rtpoll_item) {
1502 pa_rtpoll_item_free(u->rtpoll_item);
1503 u->rtpoll_item = NULL;
1504 }
1505
1506 if (u->hsp.sink_state_changed_slot) {
1507 pa_hook_slot_free(u->hsp.sink_state_changed_slot);
1508 u->hsp.sink_state_changed_slot = NULL;
1509 }
1510
1511 if (u->hsp.source_state_changed_slot) {
1512 pa_hook_slot_free(u->hsp.source_state_changed_slot);
1513 u->hsp.source_state_changed_slot = NULL;
1514 }
1515
1516 if (u->sink) {
1517 pa_sink_unref(u->sink);
1518 u->sink = NULL;
1519 }
1520
1521 if (u->source) {
1522 pa_source_unref(u->source);
1523 u->source = NULL;
1524 }
1525
1526 pa_thread_mq_done(&u->thread_mq);
1527
1528 if (u->rtpoll) {
1529 pa_rtpoll_free(u->rtpoll);
1530 u->rtpoll = NULL;
1531 }
1532 }
1533
1534 static int start_thread(struct userdata *u) {
1535 struct pollfd *pollfd;
1536
1537 pa_assert(u);
1538 pa_assert(!u->thread);
1539 pa_assert(!u->rtpoll);
1540 pa_assert(!u->rtpoll_item);
1541
1542 if (USE_SCO_OVER_PCM(u)) {
1543 pa_sink_ref(u->sink);
1544 pa_source_ref(u->source);
1545 return 0;
1546 }
1547
1548 u->rtpoll = pa_rtpoll_new();
1549 pa_thread_mq_init(&u->thread_mq, u->core->mainloop, u->rtpoll);
1550
1551 u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
1552 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
1553 pollfd->fd = u->stream_fd;
1554 pollfd->events = pollfd->revents = 0;
1555
1556 if (!(u->thread = pa_thread_new(thread_func, u))) {
1557 pa_log_error("Failed to create IO thread");
1558 stop_thread(u);
1559 return -1;
1560 }
1561
1562 if (u->sink)
1563 pa_sink_put(u->sink);
1564
1565 if (u->source)
1566 pa_source_put(u->source);
1567
1568 return 0;
1569 }
1570
1571 static int card_set_profile(pa_card *c, pa_card_profile *new_profile) {
1572 struct userdata *u;
1573 enum profile *d;
1574 pa_queue *inputs = NULL, *outputs = NULL;
1575
1576 pa_assert(c);
1577 pa_assert(new_profile);
1578 pa_assert_se(u = c->userdata);
1579
1580 d = PA_CARD_PROFILE_DATA(new_profile);
1581
1582 if (u->sink) {
1583 inputs = pa_sink_move_all_start(u->sink);
1584 if (!USE_SCO_OVER_PCM(u))
1585 pa_sink_unlink(u->sink);
1586 }
1587
1588 if (u->source) {
1589 outputs = pa_source_move_all_start(u->source);
1590 if (!USE_SCO_OVER_PCM(u))
1591 pa_source_unlink(u->source);
1592 }
1593
1594 stop_thread(u);
1595 shutdown_bt(u);
1596
1597 if (u->write_memchunk.memblock) {
1598 pa_memblock_unref(u->write_memchunk.memblock);
1599 pa_memchunk_reset(&u->write_memchunk);
1600 }
1601
1602 u->profile = *d;
1603 u->sample_spec = u->requested_sample_spec;
1604
1605 init_bt(u);
1606 init_profile(u);
1607
1608 if (u->sink || u->source)
1609 start_thread(u);
1610
1611 if (inputs) {
1612 if (u->sink)
1613 pa_sink_move_all_finish(u->sink, inputs, FALSE);
1614 else
1615 pa_sink_move_all_fail(inputs);
1616 }
1617
1618 if (outputs) {
1619 if (u->source)
1620 pa_source_move_all_finish(u->source, outputs, FALSE);
1621 else
1622 pa_source_move_all_fail(outputs);
1623 }
1624
1625 return 0;
1626 }
1627
1628 static int add_card(struct userdata *u, const char * default_profile) {
1629 pa_card_new_data data;
1630 pa_bool_t b;
1631 pa_card_profile *p;
1632 enum profile *d;
1633 const char *ff;
1634 char *n;
1635
1636 pa_card_new_data_init(&data);
1637 data.driver = __FILE__;
1638 data.module = u->module;
1639
1640 n = pa_bluetooth_cleanup_name(u->device->name);
1641 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, n);
1642 pa_xfree(n);
1643 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->device->address);
1644 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_API, "bluez");
1645 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "sound");
1646 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CONNECTOR, "bluetooth");
1647 if ((ff = pa_bluetooth_get_form_factor(u->device->class)))
1648 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_FORM_FACTOR, ff);
1649 pa_proplist_sets(data.proplist, "bluez.path", u->device->path);
1650 pa_proplist_setf(data.proplist, "bluez.class", "0x%06x", (unsigned) u->device->class);
1651 pa_proplist_sets(data.proplist, "bluez.name", u->device->name);
1652 data.name = get_name("card", u->modargs, u->device->address, &b);
1653 data.namereg_fail = b;
1654
1655 data.profiles = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
1656
1657 if (u->device->audio_sink_info_valid > 0) {
1658 p = pa_card_profile_new("a2dp", "A2DP Advanced Audio Distribution Profile", sizeof(enum profile));
1659 p->priority = 10;
1660 p->n_sinks = 1;
1661 p->n_sources = 0;
1662 p->max_sink_channels = 2;
1663 p->max_source_channels = 0;
1664
1665 d = PA_CARD_PROFILE_DATA(p);
1666 *d = PROFILE_A2DP;
1667
1668 pa_hashmap_put(data.profiles, p->name, p);
1669 }
1670
1671 if (u->device->headset_info_valid > 0) {
1672 p = pa_card_profile_new("hsp", "HSP/HFP Headset/Hands-Free Profile", sizeof(enum profile));
1673 p->priority = 20;
1674 p->n_sinks = 1;
1675 p->n_sources = 1;
1676 p->max_sink_channels = 1;
1677 p->max_source_channels = 1;
1678
1679 d = PA_CARD_PROFILE_DATA(p);
1680 *d = PROFILE_HSP;
1681
1682 pa_hashmap_put(data.profiles, p->name, p);
1683 }
1684
1685 pa_assert(!pa_hashmap_isempty(data.profiles));
1686
1687 p = pa_card_profile_new("off", "Off", sizeof(enum profile));
1688 d = PA_CARD_PROFILE_DATA(p);
1689 *d = PROFILE_OFF;
1690 pa_hashmap_put(data.profiles, p->name, p);
1691
1692 if (default_profile) {
1693 if (pa_hashmap_get(data.profiles, default_profile))
1694 pa_card_new_data_set_profile(&data, default_profile);
1695 else
1696 pa_log_warn("Profile '%s' not valid or not supported by device.", default_profile);
1697 }
1698
1699 u->card = pa_card_new(u->core, &data);
1700 pa_card_new_data_done(&data);
1701
1702 if (!u->card) {
1703 pa_log("Failed to allocate card.");
1704 return -1;
1705 }
1706
1707 u->card->userdata = u;
1708 u->card->set_profile = card_set_profile;
1709
1710 d = PA_CARD_PROFILE_DATA(u->card->active_profile);
1711 u->profile = *d;
1712
1713 return 0;
1714 }
1715
1716 static int setup_dbus(struct userdata *u) {
1717 DBusError error;
1718
1719 dbus_error_init(&error);
1720
1721 u->connection = pa_dbus_bus_get(u->core, DBUS_BUS_SYSTEM, &error);
1722 if (dbus_error_is_set(&error) || (!u->connection)) {
1723 pa_log("Failed to get D-Bus connection: %s", error.message);
1724 dbus_error_free(&error);
1725 return -1;
1726 }
1727
1728 return 0;
1729 }
1730
1731 static int find_device(struct userdata *u, const char *address, const char *path) {
1732 pa_assert(u);
1733
1734 if (!address && !path) {
1735 pa_log_error("Failed to get device address/path from module arguments.");
1736 return -1;
1737 }
1738
1739 if (path) {
1740 if (!(u->device = pa_bluetooth_get_device(pa_dbus_connection_get(u->connection), path))) {
1741 pa_log_error("%s is not a valid BlueZ audio device.", path);
1742 return -1;
1743 }
1744
1745 if (address && !(pa_streq(u->device->address, address))) {
1746 pa_log_error("Passed path %s and address %s don't match.", path, address);
1747 return -1;
1748 }
1749 } else {
1750 if (!(u->device = pa_bluetooth_find_device(pa_dbus_connection_get(u->connection), address))) {
1751 pa_log_error("%s is not known.", address);
1752 return -1;
1753 }
1754 }
1755
1756 return 0;
1757 }
1758
1759 int pa__init(pa_module* m) {
1760 pa_modargs *ma;
1761 uint32_t channels;
1762 struct userdata *u;
1763 const char *address, *path;
1764
1765 pa_assert(m);
1766
1767 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
1768 pa_log_error("Failed to parse module arguments");
1769 goto fail;
1770 }
1771
1772 m->userdata = u = pa_xnew0(struct userdata, 1);
1773 u->module = m;
1774 u->core = m->core;
1775 u->service_fd = -1;
1776 u->stream_fd = -1;
1777 u->read_smoother = pa_smoother_new(PA_USEC_PER_SEC, PA_USEC_PER_SEC*2, TRUE, 10);
1778 u->sample_spec = m->core->default_sample_spec;
1779 u->modargs = ma;
1780
1781 if (pa_modargs_get_value(ma, "sco_sink", NULL) &&
1782 !(u->hsp.sco_sink = pa_namereg_get(m->core, pa_modargs_get_value(ma, "sco_sink", NULL), PA_NAMEREG_SINK))) {
1783 pa_log("SCO sink not found");
1784 goto fail;
1785 }
1786
1787 if (pa_modargs_get_value(ma, "sco_source", NULL) &&
1788 !(u->hsp.sco_source = pa_namereg_get(m->core, pa_modargs_get_value(ma, "sco_source", NULL), PA_NAMEREG_SOURCE))) {
1789 pa_log("SCO source not found");
1790 goto fail;
1791 }
1792
1793 if (pa_modargs_get_value_u32(ma, "rate", &u->sample_spec.rate) < 0 ||
1794 u->sample_spec.rate <= 0 || u->sample_spec.rate > PA_RATE_MAX) {
1795 pa_log_error("Failed to get rate from module arguments");
1796 goto fail;
1797 }
1798
1799 channels = u->sample_spec.channels;
1800 if (pa_modargs_get_value_u32(ma, "channels", &channels) < 0 ||
1801 channels <= 0 || channels > PA_CHANNELS_MAX) {
1802 pa_log_error("Failed to get channels from module arguments");
1803 goto fail;
1804 }
1805 u->sample_spec.channels = (uint8_t) channels;
1806 u->requested_sample_spec = u->sample_spec;
1807
1808 if (setup_dbus(u) < 0)
1809 goto fail;
1810
1811 address = pa_modargs_get_value(ma, "address", NULL);
1812 path = pa_modargs_get_value(ma, "path", NULL);
1813
1814 if (find_device(u, address, path) < 0)
1815 goto fail;
1816
1817 pa_assert(u->device);
1818
1819 /* Add the card structure. This will also initialize the default profile */
1820 if (add_card(u, pa_modargs_get_value(ma, "profile", NULL)) < 0)
1821 goto fail;
1822
1823 /* Connect to the BT service and query capabilities */
1824 if (init_bt(u) < 0)
1825 goto fail;
1826
1827 if (init_profile(u) < 0)
1828 goto fail;
1829
1830 /* if (u->path) { */
1831 /* DBusError err; */
1832 /* dbus_error_init(&err); */
1833 /* char *t; */
1834
1835
1836 /* if (!dbus_connection_add_filter(pa_dbus_connection_get(u->conn), filter_cb, u, NULL)) { */
1837 /* pa_log_error("Failed to add filter function"); */
1838 /* goto fail; */
1839 /* } */
1840
1841 /* if (u->transport == BT_CAPABILITIES_TRANSPORT_SCO || */
1842 /* u->transport == BT_CAPABILITIES_TRANSPORT_ANY) { */
1843 /* t = pa_sprintf_malloc("type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged',path='%s'", u->path); */
1844 /* dbus_bus_add_match(pa_dbus_connection_get(u->conn), t, &err); */
1845 /* pa_xfree(t); */
1846
1847 /* if (dbus_error_is_set(&err)) { */
1848 /* pa_log_error("Unable to subscribe to org.bluez.Headset signals: %s: %s", err.name, err.message); */
1849 /* goto fail; */
1850 /* } */
1851 /* } */
1852
1853 /* if (u->transport == BT_CAPABILITIES_TRANSPORT_A2DP || */
1854 /* u->transport == BT_CAPABILITIES_TRANSPORT_ANY) { */
1855 /* t = pa_sprintf_malloc("type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged',path='%s'", u->path); */
1856 /* dbus_bus_add_match(pa_dbus_connection_get(u->conn), t, &err); */
1857 /* pa_xfree(t); */
1858
1859 /* if (dbus_error_is_set(&err)) { */
1860 /* pa_log_error("Unable to subscribe to org.bluez.AudioSink signals: %s: %s", err.name, err.message); */
1861 /* goto fail; */
1862 /* } */
1863 /* } */
1864 /* } */
1865
1866 if (start_thread(u) < 0)
1867 goto fail;
1868
1869 return 0;
1870
1871 fail:
1872 pa__done(m);
1873 return -1;
1874 }
1875
1876 int pa__get_n_used(pa_module *m) {
1877 struct userdata *u;
1878
1879 pa_assert(m);
1880 pa_assert_se(u = m->userdata);
1881
1882 return
1883 (u->sink ? pa_sink_linked_by(u->sink) : 0) +
1884 (u->source ? pa_source_linked_by(u->source) : 0);
1885 }
1886
1887 void pa__done(pa_module *m) {
1888 struct userdata *u;
1889 pa_assert(m);
1890
1891 if (!(u = m->userdata))
1892 return;
1893
1894 if (u->sink && !USE_SCO_OVER_PCM(u))
1895 pa_sink_unlink(u->sink);
1896
1897 if (u->source && !USE_SCO_OVER_PCM(u))
1898 pa_source_unlink(u->source);
1899
1900 stop_thread(u);
1901
1902 if (u->connection) {
1903 /* DBusError error; */
1904 /* char *t; */
1905
1906 /* if (u->transport == BT_CAPABILITIES_TRANSPORT_SCO || */
1907 /* u->transport == BT_CAPABILITIES_TRANSPORT_ANY) { */
1908
1909 /* t = pa_sprintf_malloc("type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged',path='%s'", u->path); */
1910 /* dbus_error_init(&error); */
1911 /* dbus_bus_remove_match(pa_dbus_connection_get(u->conn), t, &error); */
1912 /* dbus_error_free(&error); */
1913 /* pa_xfree(t); */
1914 /* } */
1915
1916 /* if (u->transport == BT_CAPABILITIES_TRANSPORT_A2DP || */
1917 /* u->transport == BT_CAPABILITIES_TRANSPORT_ANY) { */
1918
1919 /* t = pa_sprintf_malloc("type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged',path='%s'", u->path); */
1920 /* dbus_error_init(&error); */
1921 /* dbus_bus_remove_match(pa_dbus_connection_get(u->conn), t, &error); */
1922 /* dbus_error_free(&error); */
1923 /* pa_xfree(t); */
1924 /* } */
1925
1926 /* dbus_connection_remove_filter(pa_dbus_connection_get(u->conn), filter_cb, u); */
1927 pa_dbus_connection_unref(u->connection);
1928 }
1929
1930 if (u->card)
1931 pa_card_free(u->card);
1932
1933 if (u->read_smoother)
1934 pa_smoother_free(u->read_smoother);
1935
1936 shutdown_bt(u);
1937
1938 if (u->device)
1939 pa_bluetooth_device_free(u->device);
1940
1941 if (u->write_memchunk.memblock)
1942 pa_memblock_unref(u->write_memchunk.memblock);
1943
1944 if (u->a2dp.buffer)
1945 pa_xfree(u->a2dp.buffer);
1946
1947 sbc_finish(&u->a2dp.sbc);
1948
1949 if (u->modargs)
1950 pa_modargs_free(u->modargs);
1951
1952 pa_xfree(u);
1953 }