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