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