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