2 This file is part of PulseAudio.
4 Copyright 2008 Joao Paulo Rechi Vita
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.
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.
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
29 #include <sys/ioctl.h>
30 #include <linux/sockios.h>
31 #include <arpa/inet.h>
33 #include <pulse/xmalloc.h>
34 #include <pulse/timeval.h>
35 #include <pulse/sample.h>
36 #include <pulse/i18n.h>
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>
50 #include <modules/dbus-util.h>
52 #include "module-bluetooth-device-symdef.h"
56 #include "bluetooth-util.h"
58 #define MAX_BITPOOL 64
59 #define MIN_BITPOOL 2U
61 #define SCO_TXBUFS 0x03
62 #define SCO_RXBUFS 0x04
64 PA_MODULE_AUTHOR("Joao Paulo Rechi Vita");
65 PA_MODULE_DESCRIPTION("Bluetooth audio sink and source");
66 PA_MODULE_VERSION(PACKAGE_VERSION
);
67 PA_MODULE_LOAD_ONCE(FALSE
);
69 "name=<name for the card/sink/source, to be prefixed> "
70 "card_name=<name for the card> "
71 "sink_name=<name for the sink> "
72 "source_name=<name for the source> "
73 "address=<address of the device> "
76 "channels=<number of channels> "
77 "path=<device object path> "
78 "sco_sink=<SCO over PCM sink name> "
79 "sco_source=<SCO over PCM source name>");
81 static const char* const valid_modargs
[] = {
97 sbc_capabilities_t sbc_capabilities
;
98 sbc_t sbc
; /* Codec data */
99 pa_bool_t sbc_initialized
; /* Keep track if the encoder is initialized */
100 size_t codesize
; /* SBC codesize */
102 void* buffer
; /* Codec transfer buffer */
103 size_t buffer_size
; /* Size of the buffer */
105 uint16_t seq_num
; /* Cumulative packet sequence */
109 pcm_capabilities_t pcm_capabilities
;
111 pa_source
*sco_source
;
112 pa_hook_slot
*sink_state_changed_slot
;
113 pa_hook_slot
*source_state_changed_slot
;
130 pa_thread_mq thread_mq
;
132 pa_rtpoll_item
*rtpoll_item
;
135 uint64_t read_index
, write_index
;
136 pa_usec_t started_at
;
137 pa_smoother
*read_smoother
;
139 pa_memchunk write_memchunk
;
141 pa_sample_spec sample_spec
, requested_sample_spec
;
149 struct a2dp_info a2dp
;
151 pa_dbus_connection
*connection
;
153 enum profile profile
;
157 pa_bluetooth_device
*device
;
159 int stream_write_type
, stream_read_type
;
160 int service_write_type
, service_read_type
;
163 static int init_bt(struct userdata
*u
);
164 static int init_profile(struct userdata
*u
);
166 static int service_send(struct userdata
*u
, const bt_audio_msg_header_t
*msg
) {
170 pa_assert(u
->service_fd
>= 0);
172 pa_assert(msg
->length
> 0);
174 pa_log_debug("Sending %s -> %s",
175 pa_strnull(bt_audio_strtype(msg
->type
)),
176 pa_strnull(bt_audio_strname(msg
->name
)));
178 if ((r
= pa_loop_write(u
->service_fd
, msg
, msg
->length
, &u
->service_write_type
)) == (ssize_t
) msg
->length
)
182 pa_log_error("Error sending data to audio service: %s", pa_cstrerror(errno
));
184 pa_log_error("Short write()");
189 static int service_recv(struct userdata
*u
, bt_audio_msg_header_t
*msg
, size_t room
) {
193 pa_assert(u
->service_fd
>= 0);
197 room
= BT_SUGGESTED_BUFFER_SIZE
;
199 pa_log_debug("Trying to receive message from audio service...");
201 /* First, read the header */
202 if ((r
= pa_loop_read(u
->service_fd
, msg
, sizeof(*msg
), &u
->service_read_type
)) != sizeof(*msg
))
205 if (msg
->length
< sizeof(*msg
)) {
206 pa_log_error("Invalid message size.");
210 /* Secondly, read the payload */
211 if (msg
->length
> sizeof(*msg
)) {
213 size_t remains
= msg
->length
- sizeof(*msg
);
215 if ((r
= pa_loop_read(u
->service_fd
,
216 (uint8_t*) msg
+ sizeof(*msg
),
218 &u
->service_read_type
)) != (ssize_t
) remains
)
222 pa_log_debug("Received %s <- %s",
223 pa_strnull(bt_audio_strtype(msg
->type
)),
224 pa_strnull(bt_audio_strname(msg
->name
)));
231 pa_log_error("Error receiving data from audio service: %s", pa_cstrerror(errno
));
233 pa_log_error("Short read()");
238 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
) {
242 pa_assert(u
->service_fd
>= 0);
245 if ((r
= service_recv(u
, rsp
, room
)) < 0)
248 if ((rsp
->type
!= BT_INDICATION
&& rsp
->type
!= BT_RESPONSE
) ||
249 rsp
->name
!= expected_name
||
250 (expected_size
> 0 && rsp
->length
!= expected_size
)) {
252 if (rsp
->type
== BT_ERROR
&& rsp
->length
== sizeof(bt_audio_error_t
))
253 pa_log_error("Received error condition: %s", pa_cstrerror(((bt_audio_error_t
*) rsp
)->posix_errno
));
255 pa_log_error("Bogus message %s received while %s was expected",
256 pa_strnull(bt_audio_strname(rsp
->name
)),
257 pa_strnull(bt_audio_strname(expected_name
)));
264 static int parse_caps(struct userdata
*u
, const struct bt_get_capabilities_rsp
*rsp
) {
266 const codec_capabilities_t
*codec
;
271 bytes_left
= rsp
->h
.length
- sizeof(*rsp
);
273 if (bytes_left
< sizeof(codec_capabilities_t
)) {
274 pa_log_error("Packet too small to store codec information.");
278 codec
= (codec_capabilities_t
*) rsp
->data
; /** ALIGNMENT? **/
280 pa_log_debug("Payload size is %lu %lu", (unsigned long) bytes_left
, (unsigned long) sizeof(*codec
));
282 if ((u
->profile
== PROFILE_A2DP
&& codec
->transport
!= BT_CAPABILITIES_TRANSPORT_A2DP
) ||
283 (u
->profile
== PROFILE_HSP
&& codec
->transport
!= BT_CAPABILITIES_TRANSPORT_SCO
)) {
284 pa_log_error("Got capabilities for wrong codec.");
288 if (u
->profile
== PROFILE_HSP
) {
290 if (bytes_left
<= 0 || codec
->length
!= sizeof(u
->hsp
.pcm_capabilities
))
293 pa_assert(codec
->type
== BT_HFP_CODEC_PCM
);
295 memcpy(&u
->hsp
.pcm_capabilities
, codec
, sizeof(u
->hsp
.pcm_capabilities
));
297 } else if (u
->profile
== PROFILE_A2DP
) {
299 while (bytes_left
> 0) {
300 if (codec
->type
== BT_A2DP_CODEC_SBC
)
303 bytes_left
-= codec
->length
;
304 codec
= (const codec_capabilities_t
*) ((const uint8_t*) codec
+ codec
->length
);
307 if (bytes_left
<= 0 || codec
->length
!= sizeof(u
->a2dp
.sbc_capabilities
))
310 pa_assert(codec
->type
== BT_A2DP_CODEC_SBC
);
312 memcpy(&u
->a2dp
.sbc_capabilities
, codec
, sizeof(u
->a2dp
.sbc_capabilities
));
318 static int get_caps(struct userdata
*u
) {
320 struct bt_get_capabilities_req getcaps_req
;
321 struct bt_get_capabilities_rsp getcaps_rsp
;
322 bt_audio_error_t error
;
323 uint8_t buf
[BT_SUGGESTED_BUFFER_SIZE
];
328 memset(&msg
, 0, sizeof(msg
));
329 msg
.getcaps_req
.h
.type
= BT_REQUEST
;
330 msg
.getcaps_req
.h
.name
= BT_GET_CAPABILITIES
;
331 msg
.getcaps_req
.h
.length
= sizeof(msg
.getcaps_req
);
333 pa_strlcpy(msg
.getcaps_req
.device
, u
->device
->address
, sizeof(msg
.getcaps_req
.device
));
334 if (u
->profile
== PROFILE_A2DP
)
335 msg
.getcaps_req
.transport
= BT_CAPABILITIES_TRANSPORT_A2DP
;
337 pa_assert(u
->profile
== PROFILE_HSP
);
338 msg
.getcaps_req
.transport
= BT_CAPABILITIES_TRANSPORT_SCO
;
340 msg
.getcaps_req
.flags
= BT_FLAG_AUTOCONNECT
;
342 if (service_send(u
, &msg
.getcaps_req
.h
) < 0)
345 if (service_expect(u
, &msg
.getcaps_rsp
.h
, sizeof(msg
), BT_GET_CAPABILITIES
, 0) < 0)
348 return parse_caps(u
, &msg
.getcaps_rsp
);
351 static uint8_t a2dp_default_bitpool(uint8_t freq
, uint8_t mode
) {
354 case BT_SBC_SAMPLING_FREQ_16000
:
355 case BT_SBC_SAMPLING_FREQ_32000
:
358 case BT_SBC_SAMPLING_FREQ_44100
:
361 case BT_A2DP_CHANNEL_MODE_MONO
:
362 case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL
:
365 case BT_A2DP_CHANNEL_MODE_STEREO
:
366 case BT_A2DP_CHANNEL_MODE_JOINT_STEREO
:
370 pa_log_warn("Invalid channel mode %u", mode
);
374 case BT_SBC_SAMPLING_FREQ_48000
:
377 case BT_A2DP_CHANNEL_MODE_MONO
:
378 case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL
:
381 case BT_A2DP_CHANNEL_MODE_STEREO
:
382 case BT_A2DP_CHANNEL_MODE_JOINT_STEREO
:
386 pa_log_warn("Invalid channel mode %u", mode
);
391 pa_log_warn("Invalid sampling freq %u", freq
);
396 static int setup_a2dp(struct userdata
*u
) {
397 sbc_capabilities_t
*cap
;
400 static const struct {
404 { 16000U, BT_SBC_SAMPLING_FREQ_16000
},
405 { 32000U, BT_SBC_SAMPLING_FREQ_32000
},
406 { 44100U, BT_SBC_SAMPLING_FREQ_44100
},
407 { 48000U, BT_SBC_SAMPLING_FREQ_48000
}
411 pa_assert(u
->profile
== PROFILE_A2DP
);
413 cap
= &u
->a2dp
.sbc_capabilities
;
415 /* Find the lowest freq that is at least as high as the requested
417 for (i
= 0; (unsigned) i
< PA_ELEMENTSOF(freq_table
); i
++)
418 if (freq_table
[i
].rate
>= u
->sample_spec
.rate
&& (cap
->frequency
& freq_table
[i
].cap
)) {
419 u
->sample_spec
.rate
= freq_table
[i
].rate
;
420 cap
->frequency
= freq_table
[i
].cap
;
424 if ((unsigned) i
>= PA_ELEMENTSOF(freq_table
)) {
425 for (; i
>= 0; i
--) {
426 if (cap
->frequency
& freq_table
[i
].cap
) {
427 u
->sample_spec
.rate
= freq_table
[i
].rate
;
428 cap
->frequency
= freq_table
[i
].cap
;
434 pa_log("Not suitable sample rate");
439 if (u
->sample_spec
.channels
<= 1) {
440 if (cap
->channel_mode
& BT_A2DP_CHANNEL_MODE_MONO
) {
441 cap
->channel_mode
= BT_A2DP_CHANNEL_MODE_MONO
;
442 u
->sample_spec
.channels
= 1;
444 u
->sample_spec
.channels
= 2;
447 if (u
->sample_spec
.channels
>= 2) {
448 u
->sample_spec
.channels
= 2;
450 if (cap
->channel_mode
& BT_A2DP_CHANNEL_MODE_JOINT_STEREO
)
451 cap
->channel_mode
= BT_A2DP_CHANNEL_MODE_JOINT_STEREO
;
452 else if (cap
->channel_mode
& BT_A2DP_CHANNEL_MODE_STEREO
)
453 cap
->channel_mode
= BT_A2DP_CHANNEL_MODE_STEREO
;
454 else if (cap
->channel_mode
& BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL
)
455 cap
->channel_mode
= BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL
;
456 else if (cap
->channel_mode
& BT_A2DP_CHANNEL_MODE_MONO
) {
457 cap
->channel_mode
= BT_A2DP_CHANNEL_MODE_MONO
;
458 u
->sample_spec
.channels
= 1;
460 pa_log("No supported channel modes");
465 if (cap
->block_length
& BT_A2DP_BLOCK_LENGTH_16
)
466 cap
->block_length
= BT_A2DP_BLOCK_LENGTH_16
;
467 else if (cap
->block_length
& BT_A2DP_BLOCK_LENGTH_12
)
468 cap
->block_length
= BT_A2DP_BLOCK_LENGTH_12
;
469 else if (cap
->block_length
& BT_A2DP_BLOCK_LENGTH_8
)
470 cap
->block_length
= BT_A2DP_BLOCK_LENGTH_8
;
471 else if (cap
->block_length
& BT_A2DP_BLOCK_LENGTH_4
)
472 cap
->block_length
= BT_A2DP_BLOCK_LENGTH_4
;
474 pa_log_error("No supported block lengths");
478 if (cap
->subbands
& BT_A2DP_SUBBANDS_8
)
479 cap
->subbands
= BT_A2DP_SUBBANDS_8
;
480 else if (cap
->subbands
& BT_A2DP_SUBBANDS_4
)
481 cap
->subbands
= BT_A2DP_SUBBANDS_4
;
483 pa_log_error("No supported subbands");
487 if (cap
->allocation_method
& BT_A2DP_ALLOCATION_LOUDNESS
)
488 cap
->allocation_method
= BT_A2DP_ALLOCATION_LOUDNESS
;
489 else if (cap
->allocation_method
& BT_A2DP_ALLOCATION_SNR
)
490 cap
->allocation_method
= BT_A2DP_ALLOCATION_SNR
;
492 cap
->min_bitpool
= (uint8_t) PA_MAX(MIN_BITPOOL
, cap
->min_bitpool
);
493 cap
->max_bitpool
= (uint8_t) PA_MIN(a2dp_default_bitpool(cap
->frequency
, cap
->channel_mode
), cap
->max_bitpool
);
498 static void setup_sbc(struct a2dp_info
*a2dp
) {
499 sbc_capabilities_t
*active_capabilities
;
503 active_capabilities
= &a2dp
->sbc_capabilities
;
505 if (a2dp
->sbc_initialized
)
506 sbc_reinit(&a2dp
->sbc
, 0);
508 sbc_init(&a2dp
->sbc
, 0);
509 a2dp
->sbc_initialized
= TRUE
;
511 switch (active_capabilities
->frequency
) {
512 case BT_SBC_SAMPLING_FREQ_16000
:
513 a2dp
->sbc
.frequency
= SBC_FREQ_16000
;
515 case BT_SBC_SAMPLING_FREQ_32000
:
516 a2dp
->sbc
.frequency
= SBC_FREQ_32000
;
518 case BT_SBC_SAMPLING_FREQ_44100
:
519 a2dp
->sbc
.frequency
= SBC_FREQ_44100
;
521 case BT_SBC_SAMPLING_FREQ_48000
:
522 a2dp
->sbc
.frequency
= SBC_FREQ_48000
;
525 pa_assert_not_reached();
528 switch (active_capabilities
->channel_mode
) {
529 case BT_A2DP_CHANNEL_MODE_MONO
:
530 a2dp
->sbc
.mode
= SBC_MODE_MONO
;
532 case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL
:
533 a2dp
->sbc
.mode
= SBC_MODE_DUAL_CHANNEL
;
535 case BT_A2DP_CHANNEL_MODE_STEREO
:
536 a2dp
->sbc
.mode
= SBC_MODE_STEREO
;
538 case BT_A2DP_CHANNEL_MODE_JOINT_STEREO
:
539 a2dp
->sbc
.mode
= SBC_MODE_JOINT_STEREO
;
542 pa_assert_not_reached();
545 switch (active_capabilities
->allocation_method
) {
546 case BT_A2DP_ALLOCATION_SNR
:
547 a2dp
->sbc
.allocation
= SBC_AM_SNR
;
549 case BT_A2DP_ALLOCATION_LOUDNESS
:
550 a2dp
->sbc
.allocation
= SBC_AM_LOUDNESS
;
553 pa_assert_not_reached();
556 switch (active_capabilities
->subbands
) {
557 case BT_A2DP_SUBBANDS_4
:
558 a2dp
->sbc
.subbands
= SBC_SB_4
;
560 case BT_A2DP_SUBBANDS_8
:
561 a2dp
->sbc
.subbands
= SBC_SB_8
;
564 pa_assert_not_reached();
567 switch (active_capabilities
->block_length
) {
568 case BT_A2DP_BLOCK_LENGTH_4
:
569 a2dp
->sbc
.blocks
= SBC_BLK_4
;
571 case BT_A2DP_BLOCK_LENGTH_8
:
572 a2dp
->sbc
.blocks
= SBC_BLK_8
;
574 case BT_A2DP_BLOCK_LENGTH_12
:
575 a2dp
->sbc
.blocks
= SBC_BLK_12
;
577 case BT_A2DP_BLOCK_LENGTH_16
:
578 a2dp
->sbc
.blocks
= SBC_BLK_16
;
581 pa_assert_not_reached();
584 a2dp
->sbc
.bitpool
= active_capabilities
->max_bitpool
;
585 a2dp
->codesize
= (uint16_t) sbc_get_codesize(&a2dp
->sbc
);
588 static int set_conf(struct userdata
*u
) {
590 struct bt_set_configuration_req setconf_req
;
591 struct bt_set_configuration_rsp setconf_rsp
;
592 bt_audio_error_t error
;
593 uint8_t buf
[BT_SUGGESTED_BUFFER_SIZE
];
596 if (u
->profile
== PROFILE_A2DP
) {
597 u
->sample_spec
.format
= PA_SAMPLE_S16LE
;
599 if (setup_a2dp(u
) < 0)
602 pa_assert(u
->profile
== PROFILE_HSP
);
604 u
->sample_spec
.format
= PA_SAMPLE_S16LE
;
605 u
->sample_spec
.channels
= 1;
606 u
->sample_spec
.rate
= 8000;
609 memset(&msg
, 0, sizeof(msg
));
610 msg
.setconf_req
.h
.type
= BT_REQUEST
;
611 msg
.setconf_req
.h
.name
= BT_SET_CONFIGURATION
;
612 msg
.setconf_req
.h
.length
= sizeof(msg
.setconf_req
);
614 pa_strlcpy(msg
.setconf_req
.device
, u
->device
->address
, sizeof(msg
.setconf_req
.device
));
615 msg
.setconf_req
.access_mode
= u
->profile
== PROFILE_A2DP
? BT_CAPABILITIES_ACCESS_MODE_WRITE
: BT_CAPABILITIES_ACCESS_MODE_READWRITE
;
617 msg
.setconf_req
.codec
.transport
= u
->profile
== PROFILE_A2DP
? BT_CAPABILITIES_TRANSPORT_A2DP
: BT_CAPABILITIES_TRANSPORT_SCO
;
619 if (u
->profile
== PROFILE_A2DP
) {
620 memcpy(&msg
.setconf_req
.codec
, &u
->a2dp
.sbc_capabilities
, sizeof(u
->a2dp
.sbc_capabilities
));
621 msg
.setconf_req
.h
.length
+= msg
.setconf_req
.codec
.length
- sizeof(msg
.setconf_req
.codec
);
624 if (service_send(u
, &msg
.setconf_req
.h
) < 0)
627 if (service_expect(u
, &msg
.setconf_rsp
.h
, sizeof(msg
), BT_SET_CONFIGURATION
, sizeof(msg
.setconf_rsp
)) < 0)
630 if ((u
->profile
== PROFILE_A2DP
&& msg
.setconf_rsp
.transport
!= BT_CAPABILITIES_TRANSPORT_A2DP
) ||
631 (u
->profile
== PROFILE_HSP
&& msg
.setconf_rsp
.transport
!= BT_CAPABILITIES_TRANSPORT_SCO
)) {
632 pa_log("Transport doesn't match what we requested.");
636 if ((u
->profile
== PROFILE_A2DP
&& msg
.setconf_rsp
.access_mode
!= BT_CAPABILITIES_ACCESS_MODE_WRITE
) ||
637 (u
->profile
== PROFILE_HSP
&& msg
.setconf_rsp
.access_mode
!= BT_CAPABILITIES_ACCESS_MODE_READWRITE
)) {
638 pa_log("Access mode doesn't match what we requested.");
642 u
->link_mtu
= msg
.setconf_rsp
.link_mtu
;
644 /* setup SBC encoder now we agree on parameters */
645 if (u
->profile
== PROFILE_A2DP
) {
647 u
->block_size
= u
->a2dp
.codesize
;
648 pa_log_info("SBC parameters:\n\tallocation=%u\n\tsubbands=%u\n\tblocks=%u\n\tbitpool=%u\n",
649 u
->a2dp
.sbc
.allocation
, u
->a2dp
.sbc
.subbands
, u
->a2dp
.sbc
.blocks
, u
->a2dp
.sbc
.bitpool
);
651 u
->block_size
= u
->link_mtu
;
657 static int start_stream_fd(struct userdata
*u
) {
659 bt_audio_msg_header_t rsp
;
660 struct bt_start_stream_req start_req
;
661 struct bt_start_stream_rsp start_rsp
;
662 struct bt_new_stream_ind streamfd_ind
;
663 bt_audio_error_t error
;
664 uint8_t buf
[BT_SUGGESTED_BUFFER_SIZE
];
666 struct pollfd
*pollfd
;
669 pa_assert(u
->rtpoll
);
670 pa_assert(!u
->rtpoll_item
);
671 pa_assert(u
->stream_fd
< 0);
673 memset(msg
.buf
, 0, BT_SUGGESTED_BUFFER_SIZE
);
674 msg
.start_req
.h
.type
= BT_REQUEST
;
675 msg
.start_req
.h
.name
= BT_START_STREAM
;
676 msg
.start_req
.h
.length
= sizeof(msg
.start_req
);
678 if (service_send(u
, &msg
.start_req
.h
) < 0)
681 if (service_expect(u
, &msg
.rsp
, sizeof(msg
), BT_START_STREAM
, sizeof(msg
.start_rsp
)) < 0)
684 if (service_expect(u
, &msg
.rsp
, sizeof(msg
), BT_NEW_STREAM
, sizeof(msg
.streamfd_ind
)) < 0)
687 if ((u
->stream_fd
= bt_audio_service_get_data_fd(u
->service_fd
)) < 0) {
688 pa_log("Failed to get stream fd from audio service.");
692 /* setsockopt(u->stream_fd, SOL_SCO, SCO_TXBUFS, &period_count, sizeof(period_count)); */
693 /* setsockopt(u->stream_fd, SOL_SCO, SCO_SNDBUF, &period_count, sizeof(period_count)); */
695 pa_make_fd_nonblock(u
->stream_fd
);
696 pa_make_socket_low_delay(u
->stream_fd
);
698 u
->rtpoll_item
= pa_rtpoll_item_new(u
->rtpoll
, PA_RTPOLL_NEVER
, 1);
699 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
700 pollfd
->fd
= u
->stream_fd
;
701 pollfd
->events
= pollfd
->revents
= 0;
710 static int stop_stream_fd(struct userdata
*u
) {
712 bt_audio_msg_header_t rsp
;
713 struct bt_stop_stream_req start_req
;
714 struct bt_stop_stream_rsp start_rsp
;
715 bt_audio_error_t error
;
716 uint8_t buf
[BT_SUGGESTED_BUFFER_SIZE
];
721 pa_assert(u
->rtpoll
);
722 pa_assert(u
->rtpoll_item
);
723 pa_assert(u
->stream_fd
>= 0);
725 pa_rtpoll_item_free(u
->rtpoll_item
);
726 u
->rtpoll_item
= NULL
;
728 memset(msg
.buf
, 0, BT_SUGGESTED_BUFFER_SIZE
);
729 msg
.start_req
.h
.type
= BT_REQUEST
;
730 msg
.start_req
.h
.name
= BT_STOP_STREAM
;
731 msg
.start_req
.h
.length
= sizeof(msg
.start_req
);
733 if (service_send(u
, &msg
.start_req
.h
) < 0 ||
734 service_expect(u
, &msg
.rsp
, sizeof(msg
), BT_STOP_STREAM
, sizeof(msg
.start_rsp
)) < 0)
737 pa_close(u
->stream_fd
);
743 static int sink_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
744 struct userdata
*u
= PA_SINK(o
)->userdata
;
745 pa_bool_t failed
= FALSE
;
748 pa_assert(u
->sink
== PA_SINK(o
));
750 pa_log_debug("got message: %d", code
);
753 case PA_SINK_MESSAGE_SET_STATE
:
755 switch ((pa_sink_state_t
) PA_PTR_TO_UINT(data
)) {
757 case PA_SINK_SUSPENDED
:
758 pa_assert(PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
));
760 /* Stop the device if the source is suspended as well */
761 if (!u
->source
|| u
->source
->state
== PA_SOURCE_SUSPENDED
)
762 /* We deliberately ignore whether stopping
763 * actually worked. Since the stream_fd is
764 * closed it doesn't really matter */
770 case PA_SINK_RUNNING
:
771 if (u
->sink
->thread_info
.state
!= PA_SINK_SUSPENDED
)
774 /* Resume the device if the source was suspended as well */
775 if (!u
->source
|| u
->source
->state
== PA_SOURCE_SUSPENDED
)
776 if (start_stream_fd(u
) < 0)
779 u
->started_at
= pa_rtclock_usec();
782 case PA_SINK_UNLINKED
:
784 case PA_SINK_INVALID_STATE
:
789 case PA_SINK_MESSAGE_GET_LATENCY
: {
790 *((pa_usec_t
*) data
) = 0;
795 r
= pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
797 return (r
< 0 || !failed
) ? r
: -1;
800 static int source_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
801 struct userdata
*u
= PA_SOURCE(o
)->userdata
;
802 pa_bool_t failed
= FALSE
;
805 pa_assert(u
->source
== PA_SOURCE(o
));
807 pa_log_debug("got message: %d", code
);
810 case PA_SOURCE_MESSAGE_SET_STATE
:
812 switch ((pa_source_state_t
) PA_PTR_TO_UINT(data
)) {
814 case PA_SOURCE_SUSPENDED
:
815 pa_assert(PA_SOURCE_IS_OPENED(u
->source
->thread_info
.state
));
817 /* Stop the device if the sink is suspended as well */
818 if (!u
->sink
|| u
->sink
->state
== PA_SINK_SUSPENDED
)
821 pa_smoother_pause(u
->read_smoother
, pa_rtclock_usec());
825 case PA_SOURCE_RUNNING
:
826 if (u
->source
->thread_info
.state
!= PA_SOURCE_SUSPENDED
)
829 /* Resume the device if the sink was suspended as well */
830 if (!u
->sink
|| u
->sink
->thread_info
.state
== PA_SINK_SUSPENDED
)
831 if (start_stream_fd(u
) < 0)
834 pa_smoother_resume(u
->read_smoother
, pa_rtclock_usec());
837 case PA_SOURCE_UNLINKED
:
839 case PA_SOURCE_INVALID_STATE
:
844 case PA_SOURCE_MESSAGE_GET_LATENCY
: {
845 *((pa_usec_t
*) data
) = 0;
851 r
= pa_source_process_msg(o
, code
, data
, offset
, chunk
);
853 return (r
< 0 || !failed
) ? r
: -1;
856 static int hsp_process_render(struct userdata
*u
) {
858 pa_memchunk memchunk
;
861 pa_assert(u
->profile
== PROFILE_HSP
);
864 pa_sink_render_full(u
->sink
, u
->block_size
, &memchunk
);
870 p
= (const uint8_t*) pa_memblock_acquire(memchunk
.memblock
) + memchunk
.index
;
871 l
= pa_write(u
->stream_fd
, p
, memchunk
.length
, &u
->stream_write_type
);
872 pa_memblock_release(memchunk
.memblock
);
874 pa_log_debug("Memblock written to socket: %lli bytes", (long long) l
);
879 if (errno
== EINTR
|| errno
== EAGAIN
)
882 pa_log_error("Failed to write data to SCO socket: %s", pa_cstrerror(errno
));
887 pa_assert((size_t) l
<= memchunk
.length
);
889 memchunk
.index
+= (size_t) l
;
890 memchunk
.length
-= (size_t) l
;
892 u
->write_index
+= (uint64_t) l
;
894 if (memchunk
.length
<= 0)
899 pa_memblock_unref(memchunk
.memblock
);
904 static int hsp_process_push(struct userdata
*u
) {
906 pa_memchunk memchunk
;
909 pa_assert(u
->profile
== PROFILE_HSP
);
910 pa_assert(u
->source
);
912 memchunk
.memblock
= pa_memblock_new(u
->core
->mempool
, u
->block_size
);
913 memchunk
.index
= memchunk
.length
= 0;
919 p
= pa_memblock_acquire(memchunk
.memblock
);
920 l
= pa_read(u
->stream_fd
, p
, pa_memblock_get_length(memchunk
.memblock
), &u
->stream_read_type
);
921 pa_memblock_release(memchunk
.memblock
);
924 if (l
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
927 pa_log_error("Failed to read data from SCO socket: %s", l
< 0 ? pa_cstrerror(errno
) : "EOF");
932 memchunk
.length
= (size_t) l
;
933 u
->read_index
+= (uint64_t) l
;
935 pa_source_post(u
->source
, &memchunk
);
940 pa_memblock_unref(memchunk
.memblock
);
945 static int a2dp_process_render(struct userdata
*u
) {
947 struct a2dp_info
*a2dp
;
948 struct rtp_header
*header
;
949 struct rtp_payload
*payload
;
953 unsigned frame_count
;
958 pa_assert(u
->profile
== PROFILE_A2DP
);
963 if (a2dp
->buffer_size
< u
->link_mtu
) {
964 a2dp
->buffer_size
= 2*u
->link_mtu
;
965 pa_xfree(a2dp
->buffer
);
966 a2dp
->buffer
= pa_xmalloc(a2dp
->buffer_size
);
969 header
= (struct rtp_header
*) a2dp
->buffer
;
970 payload
= (struct rtp_payload
*) ((uint8_t*) a2dp
->buffer
+ sizeof(*header
));
971 d
= (uint8_t*) a2dp
->buffer
+ sizeof(*header
) + sizeof(*payload
);
972 left
= a2dp
->buffer_size
- sizeof(*header
) - sizeof(*payload
);
974 frame_size
= sbc_get_frame_length(&a2dp
->sbc
);
977 writing_at
= u
->write_index
;
982 if (!u
->write_memchunk
.memblock
)
983 pa_sink_render_full(u
->sink
, u
->block_size
, &u
->write_memchunk
);
985 p
= (const uint8_t*) pa_memblock_acquire(u
->write_memchunk
.memblock
) + u
->write_memchunk
.index
;
986 encoded
= sbc_encode(&a2dp
->sbc
,
987 (void*) p
, u
->write_memchunk
.length
,
992 pa_log_debug("Using SBC encoder implementation: %s", pa_strnull(sbc_get_implementation_info(&a2dp
->sbc
)));
995 pa_memblock_release(u
->write_memchunk
.memblock
);
998 pa_log_error("SBC encoding error (%d)", encoded
);
1002 pa_assert(written
>= 0);
1004 pa_assert((size_t) encoded
<= u
->write_memchunk
.length
);
1005 pa_assert((size_t) written
<= left
);
1007 /* pa_log_debug("SBC: encoded: %d; written: %d", encoded, written); */
1009 u
->write_memchunk
.index
+= encoded
;
1010 u
->write_memchunk
.length
-= encoded
;
1012 if (u
->write_memchunk
.length
<= 0) {
1013 pa_memblock_unref(u
->write_memchunk
.memblock
);
1014 pa_memchunk_reset(&u
->write_memchunk
);
1017 u
->write_index
+= encoded
;
1019 d
= (uint8_t*) d
+ written
;
1024 } while ((uint8_t*) d
- (uint8_t*) a2dp
->buffer
+ written
< (ptrdiff_t) u
->link_mtu
);
1026 /* write it to the fifo */
1027 memset(a2dp
->buffer
, 0, sizeof(*header
) + sizeof(*payload
));
1028 payload
->frame_count
= frame_count
;
1031 header
->sequence_number
= htons(a2dp
->seq_num
++);
1032 header
->timestamp
= htonl(writing_at
/ frame_size
);
1033 header
->ssrc
= htonl(1);
1036 left
= (uint8_t*) d
- (uint8_t*) a2dp
->buffer
;
1041 l
= pa_write(u
->stream_fd
, p
, left
, &u
->stream_write_type
);
1042 /* pa_log_debug("write: requested %lu bytes; written %li bytes; mtu=%li", (unsigned long) left, (long) l, (unsigned long) u->link_mtu); */
1047 if (errno
== EINTR
|| errno
== EAGAIN
)
1050 pa_log_error("Failed to write data to socket: %s", pa_cstrerror(errno
));
1054 pa_assert((size_t) l
<= left
);
1056 d
= (uint8_t*) d
+ l
;
1067 static void thread_func(void *userdata
) {
1068 struct userdata
*u
= userdata
;
1069 pa_bool_t do_write
= FALSE
, writable
= FALSE
;
1073 pa_log_debug("IO Thread starting up");
1075 if (u
->core
->realtime_scheduling
)
1076 pa_make_realtime(u
->core
->realtime_priority
);
1078 if (start_stream_fd(u
) < 0)
1081 pa_thread_mq_install(&u
->thread_mq
);
1082 pa_rtpoll_install(u
->rtpoll
);
1084 pa_smoother_set_time_offset(u
->read_smoother
, pa_rtclock_usec());
1087 struct pollfd
*pollfd
;
1089 pa_bool_t disable_timer
= TRUE
;
1091 pollfd
= u
->rtpoll_item
? pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
) : NULL
;
1093 if (u
->source
&& PA_SOURCE_IS_LINKED(u
->source
->thread_info
.state
)) {
1095 if (pollfd
&& (pollfd
->revents
& POLLIN
)) {
1097 if (hsp_process_push(u
) < 0)
1100 /* We just read something, so we are supposed to write something, too */
1105 if (u
->sink
&& PA_SINK_IS_LINKED(u
->sink
->thread_info
.state
)) {
1107 if (u
->sink
->thread_info
.rewind_requested
)
1108 pa_sink_process_rewind(u
->sink
, 0);
1111 if (pollfd
->revents
& POLLOUT
)
1114 if ((!u
->source
|| !PA_SOURCE_IS_LINKED(u
->source
->thread_info
.state
)) && !do_write
&& writable
) {
1115 pa_usec_t time_passed
;
1116 uint64_t should_have_written
;
1118 /* Hmm, there is no input stream we could synchronize
1119 * to. So let's do things by time */
1121 time_passed
= pa_rtclock_usec() - u
->started_at
;
1122 should_have_written
= pa_usec_to_bytes(time_passed
, &u
->sink
->sample_spec
);
1124 do_write
= u
->write_index
<= should_have_written
;
1125 /* pa_log_debug("Time has come: %s", pa_yes_no(do_write)); */
1128 if (writable
&& do_write
) {
1130 if (u
->profile
== PROFILE_A2DP
) {
1131 if (a2dp_process_render(u
) < 0)
1134 if (hsp_process_render(u
) < 0)
1142 if ((!u
->source
|| !PA_SOURCE_IS_LINKED(u
->source
->thread_info
.state
)) && !do_write
) {
1143 pa_usec_t time_passed
, next_write_at
, sleep_for
;
1145 /* Hmm, there is no input stream we could synchronize
1146 * to. So let's estimate when we need to wake up the latest */
1148 time_passed
= pa_rtclock_usec() - u
->started_at
;
1149 next_write_at
= pa_bytes_to_usec(u
->write_index
, &u
->sink
->sample_spec
);
1150 sleep_for
= time_passed
< next_write_at
? next_write_at
- time_passed
: 0;
1152 /* pa_log("Sleeping for %lu; time passed %lu, next write at %lu", (unsigned long) sleep_for, (unsigned long) time_passed, (unsigned long)next_write_at); */
1154 pa_rtpoll_set_timer_relative(u
->rtpoll
, sleep_for
);
1155 disable_timer
= FALSE
;
1161 pa_rtpoll_set_timer_disabled(u
->rtpoll
);
1163 /* Hmm, nothing to do. Let's sleep */
1165 pollfd
->events
= (short) (((u
->sink
&& PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
) && !writable
) ? POLLOUT
: 0) |
1166 (u
->source
&& PA_SOURCE_IS_OPENED(u
->source
->thread_info
.state
) ? POLLIN
: 0));
1168 if ((ret
= pa_rtpoll_run(u
->rtpoll
, TRUE
)) < 0)
1174 pollfd
= u
->rtpoll_item
? pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
) : NULL
;
1176 if (pollfd
&& (pollfd
->revents
& ~(POLLOUT
|POLLIN
))) {
1177 pa_log_error("FD error.");
1183 /* If this was no regular exit from the loop we have to continue processing messages until we receive PA_MESSAGE_SHUTDOWN */
1184 pa_log_debug("IO thread failed");
1185 pa_asyncmsgq_post(u
->thread_mq
.outq
, PA_MSGOBJECT(u
->core
), PA_CORE_MESSAGE_UNLOAD_MODULE
, u
->module
, 0, NULL
, NULL
);
1186 pa_asyncmsgq_wait_for(u
->thread_mq
.inq
, PA_MESSAGE_SHUTDOWN
);
1189 pa_log_debug("IO thread shutting down");
1192 /* static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *msg, void *userdata) { */
1193 /* DBusMessageIter arg_i; */
1194 /* DBusError err; */
1195 /* const char *value; */
1196 /* struct userdata *u; */
1198 /* pa_assert(bus); */
1199 /* pa_assert(msg); */
1200 /* pa_assert(userdata); */
1203 /* pa_log_debug("dbus: interface=%s, path=%s, member=%s\n", */
1204 /* dbus_message_get_interface(msg), */
1205 /* dbus_message_get_path(msg), */
1206 /* dbus_message_get_member(msg)); */
1208 /* dbus_error_init(&err); */
1210 /* if (!dbus_message_has_path(msg, u->path)) */
1213 /* if (dbus_message_is_signal(msg, "org.bluez.Headset", "PropertyChanged") || */
1214 /* dbus_message_is_signal(msg, "org.bluez.AudioSink", "PropertyChanged")) { */
1216 /* struct device *d; */
1217 /* const char *profile; */
1218 /* DBusMessageIter variant_i; */
1219 /* dbus_uint16_t gain; */
1221 /* if (!dbus_message_iter_init(msg, &arg_i)) { */
1222 /* pa_log("dbus: message has no parameters"); */
1226 /* if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_STRING) { */
1227 /* pa_log("Property name not a string."); */
1231 /* dbus_message_iter_get_basic(&arg_i, &value); */
1233 /* if (!dbus_message_iter_next(&arg_i)) { */
1234 /* pa_log("Property value missing"); */
1238 /* if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_VARIANT) { */
1239 /* pa_log("Property value not a variant."); */
1243 /* dbus_message_iter_recurse(&arg_i, &variant_i); */
1245 /* if (dbus_message_iter_get_arg_type(&variant_i) != DBUS_TYPE_UINT16) { */
1246 /* dbus_message_iter_get_basic(&variant_i, &gain); */
1248 /* if (pa_streq(value, "SpeakerGain")) { */
1249 /* pa_log("spk gain: %d", gain); */
1250 /* pa_cvolume_set(&u->sink->virtual_volume, 1, (pa_volume_t) (gain * PA_VOLUME_NORM / 15)); */
1251 /* pa_subscription_post(u->sink->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, u->sink->index); */
1253 /* pa_log("mic gain: %d", gain); */
1254 /* if (!u->source) */
1257 /* pa_cvolume_set(&u->source->virtual_volume, 1, (pa_volume_t) (gain * PA_VOLUME_NORM / 15)); */
1258 /* pa_subscription_post(u->source->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, u->source->index); */
1264 /* dbus_error_free(&err); */
1265 /* return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; */
1268 /* static int sink_get_volume_cb(pa_sink *s) { */
1269 /* struct userdata *u = s->userdata; */
1272 /* /\* refresh? *\/ */
1277 /* static int source_get_volume_cb(pa_source *s) { */
1278 /* struct userdata *u = s->userdata; */
1281 /* /\* refresh? *\/ */
1286 /* static int sink_set_volume_cb(pa_sink *s) { */
1288 /* DBusMessage *m, *r; */
1289 /* DBusMessageIter it, itvar; */
1290 /* dbus_uint16_t vol; */
1291 /* const char *spkgain = "SpeakerGain"; */
1292 /* struct userdata *u = s->userdata; */
1295 /* dbus_error_init(&e); */
1297 /* vol = ((float) pa_cvolume_max(&s->virtual_volume) / PA_VOLUME_NORM) * 15; */
1298 /* pa_log_debug("set headset volume: %d", vol); */
1300 /* pa_assert_se(m = dbus_message_new_method_call("org.bluez", u->path, "org.bluez.Headset", "SetProperty")); */
1301 /* dbus_message_iter_init_append(m, &it); */
1302 /* dbus_message_iter_append_basic(&it, DBUS_TYPE_STRING, &spkgain); */
1303 /* dbus_message_iter_open_container(&it, DBUS_TYPE_VARIANT, DBUS_TYPE_UINT16_AS_STRING, &itvar); */
1304 /* dbus_message_iter_append_basic(&itvar, DBUS_TYPE_UINT16, &vol); */
1305 /* dbus_message_iter_close_container(&it, &itvar); */
1307 /* r = dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(u->conn), m, -1, &e); */
1311 /* dbus_message_unref(m); */
1313 /* dbus_message_unref(r); */
1315 /* dbus_error_free(&e); */
1320 /* static int source_set_volume_cb(pa_source *s) { */
1321 /* dbus_uint16_t vol; */
1322 /* struct userdata *u = s->userdata; */
1325 /* vol = ((float)pa_cvolume_max(&s->virtual_volume) / PA_VOLUME_NORM) * 15; */
1327 /* pa_log_debug("set headset mic volume: %d (not implemented yet)", vol); */
1332 static char *get_name(const char *type
, pa_modargs
*ma
, const char *device_id
, pa_bool_t
*namereg_fail
) {
1338 pa_assert(device_id
);
1339 pa_assert(namereg_fail
);
1341 t
= pa_sprintf_malloc("%s_name", type
);
1342 n
= pa_modargs_get_value(ma
, t
, NULL
);
1346 *namereg_fail
= TRUE
;
1347 return pa_xstrdup(n
);
1350 if ((n
= pa_modargs_get_value(ma
, "name", NULL
)))
1351 *namereg_fail
= TRUE
;
1354 *namereg_fail
= FALSE
;
1357 return pa_sprintf_malloc("bluez_%s.%s", type
, n
);
1360 #define USE_SCO_OVER_PCM(u) (u->profile == PROFILE_HSP && (u->hsp.sco_sink && u->hsp.sco_source))
1362 static void sco_over_pcm_state_update(struct userdata
*u
) {
1364 pa_assert(USE_SCO_OVER_PCM(u
));
1366 if (PA_SINK_IS_OPENED(pa_sink_get_state(u
->hsp
.sco_sink
)) ||
1367 PA_SOURCE_IS_OPENED(pa_source_get_state(u
->hsp
.sco_source
))) {
1369 if (u
->service_fd
>= 0)
1372 pa_log_debug("Resuming SCO over PCM");
1373 if ((init_bt(u
) < 0) || (init_profile(u
) < 0))
1374 pa_log("Can't resume SCO over PCM");
1378 if (u
->service_fd
< 0)
1381 pa_log_debug("Closing SCO over PCM");
1382 pa_close(u
->service_fd
);
1387 static pa_hook_result_t
sink_state_changed_cb(pa_core
*c
, pa_sink
*s
, struct userdata
*u
) {
1389 pa_sink_assert_ref(s
);
1392 if (s
!= u
->hsp
.sco_sink
)
1395 sco_over_pcm_state_update(u
);
1400 static pa_hook_result_t
source_state_changed_cb(pa_core
*c
, pa_source
*s
, struct userdata
*u
) {
1402 pa_source_assert_ref(s
);
1405 if (s
!= u
->hsp
.sco_source
)
1408 sco_over_pcm_state_update(u
);
1413 static int add_sink(struct userdata
*u
) {
1415 if (USE_SCO_OVER_PCM(u
)) {
1418 u
->sink
= u
->hsp
.sco_sink
;
1419 p
= pa_proplist_new();
1420 pa_proplist_sets(p
, "bluetooth.protocol", "sco");
1421 pa_proplist_update(u
->sink
->proplist
, PA_UPDATE_MERGE
, p
);
1422 pa_proplist_free(p
);
1424 if (!u
->hsp
.sink_state_changed_slot
)
1425 u
->hsp
.sink_state_changed_slot
= pa_hook_connect(&u
->core
->hooks
[PA_CORE_HOOK_SINK_STATE_CHANGED
], PA_HOOK_NORMAL
, (pa_hook_cb_t
) sink_state_changed_cb
, u
);
1428 pa_sink_new_data data
;
1431 pa_sink_new_data_init(&data
);
1432 data
.driver
= __FILE__
;
1433 data
.module
= u
->module
;
1434 pa_sink_new_data_set_sample_spec(&data
, &u
->sample_spec
);
1435 pa_proplist_sets(data
.proplist
, "bluetooth.protocol", u
->profile
== PROFILE_A2DP
? "a2dp" : "sco");
1436 data
.card
= u
->card
;
1437 data
.name
= get_name("sink", u
->modargs
, u
->device
->address
, &b
);
1438 data
.namereg_fail
= b
;
1440 u
->sink
= pa_sink_new(u
->core
, &data
, PA_SINK_HARDWARE
|PA_SINK_LATENCY
);
1441 pa_sink_new_data_done(&data
);
1444 pa_log_error("Failed to create sink");
1448 u
->sink
->userdata
= u
;
1449 u
->sink
->parent
.process_msg
= sink_process_msg
;
1452 /* u->sink->get_volume = sink_get_volume_cb; */
1453 /* u->sink->set_volume = sink_set_volume_cb; */
1458 static int add_source(struct userdata
*u
) {
1461 if (USE_SCO_OVER_PCM(u
)) {
1462 u
->source
= u
->hsp
.sco_source
;
1463 p
= pa_proplist_new();
1464 pa_proplist_sets(p
, "bluetooth.protocol", "sco");
1465 pa_proplist_update(u
->source
->proplist
, PA_UPDATE_MERGE
, p
);
1466 pa_proplist_free(p
);
1468 if (!u
->hsp
.source_state_changed_slot
)
1469 u
->hsp
.source_state_changed_slot
= pa_hook_connect(&u
->core
->hooks
[PA_CORE_HOOK_SOURCE_STATE_CHANGED
], PA_HOOK_NORMAL
, (pa_hook_cb_t
) source_state_changed_cb
, u
);
1472 pa_source_new_data data
;
1475 pa_source_new_data_init(&data
);
1476 data
.driver
= __FILE__
;
1477 data
.module
= u
->module
;
1478 pa_source_new_data_set_sample_spec(&data
, &u
->sample_spec
);
1479 pa_proplist_sets(data
.proplist
, "bluetooth.protocol", u
->profile
== PROFILE_A2DP
? "a2dp" : "sco");
1480 data
.card
= u
->card
;
1481 data
.name
= get_name("source", u
->modargs
, u
->device
->address
, &b
);
1482 data
.namereg_fail
= b
;
1484 u
->source
= pa_source_new(u
->core
, &data
, PA_SOURCE_HARDWARE
|PA_SOURCE_LATENCY
);
1485 pa_source_new_data_done(&data
);
1488 pa_log_error("Failed to create source");
1492 u
->source
->userdata
= u
;
1493 u
->source
->parent
.process_msg
= source_process_msg
;
1496 /* u->source->get_volume = source_get_volume_cb; */
1497 /* u->source->set_volume = source_set_volume_cb; */
1499 p
= pa_proplist_new();
1500 pa_proplist_sets(p
, "bluetooth.nrec", pa_yes_no(u
->hsp
.pcm_capabilities
.flags
& BT_PCM_FLAG_NREC
));
1501 pa_proplist_update(u
->source
->proplist
, PA_UPDATE_MERGE
, p
);
1502 pa_proplist_free(p
);
1507 static void shutdown_bt(struct userdata
*u
) {
1510 if (u
->stream_fd
>= 0) {
1511 pa_close(u
->stream_fd
);
1515 if (u
->service_fd
>= 0) {
1516 pa_close(u
->service_fd
);
1521 static int init_bt(struct userdata
*u
) {
1526 u
->stream_write_type
= u
->stream_read_type
= 0;
1527 u
->service_write_type
= u
->service_write_type
= 0;
1529 if ((u
->service_fd
= bt_audio_service_open()) < 0) {
1530 pa_log_error("Couldn't connect to bluetooth audio service");
1534 pa_log_debug("Connected to the bluetooth audio service");
1539 static int setup_bt(struct userdata
*u
) {
1542 if (get_caps(u
) < 0)
1545 pa_log_debug("Got device capabilities");
1547 if (set_conf(u
) < 0)
1550 pa_log_debug("Connection to the device configured");
1552 if (USE_SCO_OVER_PCM(u
)) {
1553 pa_log_debug("Configured to use SCO over PCM");
1557 pa_log_debug("Got the stream socket");
1562 static int init_profile(struct userdata
*u
) {
1565 pa_assert(u
->profile
!= PROFILE_OFF
);
1567 if (setup_bt(u
) < 0)
1570 if (u
->profile
== PROFILE_A2DP
||
1571 u
->profile
== PROFILE_HSP
)
1572 if (add_sink(u
) < 0)
1575 if (u
->profile
== PROFILE_HSP
)
1576 if (add_source(u
) < 0)
1582 static void stop_thread(struct userdata
*u
) {
1586 pa_asyncmsgq_send(u
->thread_mq
.inq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, 0, NULL
);
1587 pa_thread_free(u
->thread
);
1591 if (u
->rtpoll_item
) {
1592 pa_rtpoll_item_free(u
->rtpoll_item
);
1593 u
->rtpoll_item
= NULL
;
1596 if (u
->hsp
.sink_state_changed_slot
) {
1597 pa_hook_slot_free(u
->hsp
.sink_state_changed_slot
);
1598 u
->hsp
.sink_state_changed_slot
= NULL
;
1601 if (u
->hsp
.source_state_changed_slot
) {
1602 pa_hook_slot_free(u
->hsp
.source_state_changed_slot
);
1603 u
->hsp
.source_state_changed_slot
= NULL
;
1607 pa_sink_unref(u
->sink
);
1612 pa_source_unref(u
->source
);
1617 pa_thread_mq_done(&u
->thread_mq
);
1619 pa_rtpoll_free(u
->rtpoll
);
1624 static int start_thread(struct userdata
*u
) {
1626 pa_assert(!u
->thread
);
1627 pa_assert(!u
->rtpoll
);
1628 pa_assert(!u
->rtpoll_item
);
1630 if (USE_SCO_OVER_PCM(u
)) {
1631 pa_sink_ref(u
->sink
);
1632 pa_source_ref(u
->source
);
1636 u
->rtpoll
= pa_rtpoll_new();
1637 pa_thread_mq_init(&u
->thread_mq
, u
->core
->mainloop
, u
->rtpoll
);
1639 if (!(u
->thread
= pa_thread_new(thread_func
, u
))) {
1640 pa_log_error("Failed to create IO thread");
1646 pa_sink_set_asyncmsgq(u
->sink
, u
->thread_mq
.inq
);
1647 pa_sink_set_rtpoll(u
->sink
, u
->rtpoll
);
1648 pa_sink_put(u
->sink
);
1652 pa_source_set_asyncmsgq(u
->source
, u
->thread_mq
.inq
);
1653 pa_source_set_rtpoll(u
->source
, u
->rtpoll
);
1654 pa_source_put(u
->source
);
1660 static int card_set_profile(pa_card
*c
, pa_card_profile
*new_profile
) {
1663 pa_queue
*inputs
= NULL
, *outputs
= NULL
;
1666 pa_assert(new_profile
);
1667 pa_assert_se(u
= c
->userdata
);
1669 d
= PA_CARD_PROFILE_DATA(new_profile
);
1672 inputs
= pa_sink_move_all_start(u
->sink
);
1673 if (!USE_SCO_OVER_PCM(u
))
1674 pa_sink_unlink(u
->sink
);
1678 outputs
= pa_source_move_all_start(u
->source
);
1679 if (!USE_SCO_OVER_PCM(u
))
1680 pa_source_unlink(u
->source
);
1686 if (u
->write_memchunk
.memblock
) {
1687 pa_memblock_unref(u
->write_memchunk
.memblock
);
1688 pa_memchunk_reset(&u
->write_memchunk
);
1692 u
->sample_spec
= u
->requested_sample_spec
;
1696 if (u
->profile
!= PROFILE_OFF
)
1699 if (u
->sink
|| u
->source
)
1704 pa_sink_move_all_finish(u
->sink
, inputs
, FALSE
);
1706 pa_sink_move_all_fail(inputs
);
1711 pa_source_move_all_finish(u
->source
, outputs
, FALSE
);
1713 pa_source_move_all_fail(outputs
);
1719 static int add_card(struct userdata
*u
, const char * default_profile
) {
1720 pa_card_new_data data
;
1727 pa_card_new_data_init(&data
);
1728 data
.driver
= __FILE__
;
1729 data
.module
= u
->module
;
1731 n
= pa_bluetooth_cleanup_name(u
->device
->name
);
1732 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, n
);
1734 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_STRING
, u
->device
->address
);
1735 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_API
, "bluez");
1736 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_CLASS
, "sound");
1737 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_BUS
, "bluetooth");
1738 if ((ff
= pa_bluetooth_get_form_factor(u
->device
->class)))
1739 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_FORM_FACTOR
, ff
);
1740 pa_proplist_sets(data
.proplist
, "bluez.path", u
->device
->path
);
1741 pa_proplist_setf(data
.proplist
, "bluez.class", "0x%06x", (unsigned) u
->device
->class);
1742 pa_proplist_sets(data
.proplist
, "bluez.name", u
->device
->name
);
1743 data
.name
= get_name("card", u
->modargs
, u
->device
->address
, &b
);
1744 data
.namereg_fail
= b
;
1746 data
.profiles
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
1748 if (u
->device
->audio_sink_info_valid
> 0) {
1749 p
= pa_card_profile_new("a2dp", _("High Fidelity Playback (A2DP)"), sizeof(enum profile
));
1753 p
->max_sink_channels
= 2;
1754 p
->max_source_channels
= 0;
1756 d
= PA_CARD_PROFILE_DATA(p
);
1759 pa_hashmap_put(data
.profiles
, p
->name
, p
);
1762 if (u
->device
->headset_info_valid
> 0) {
1763 p
= pa_card_profile_new("hsp", _("Telephony Duplex (HSP/HFP)"), sizeof(enum profile
));
1767 p
->max_sink_channels
= 1;
1768 p
->max_source_channels
= 1;
1770 d
= PA_CARD_PROFILE_DATA(p
);
1773 pa_hashmap_put(data
.profiles
, p
->name
, p
);
1776 pa_assert(!pa_hashmap_isempty(data
.profiles
));
1778 p
= pa_card_profile_new("off", _("Off"), sizeof(enum profile
));
1779 d
= PA_CARD_PROFILE_DATA(p
);
1781 pa_hashmap_put(data
.profiles
, p
->name
, p
);
1783 if (default_profile
) {
1784 if (pa_hashmap_get(data
.profiles
, default_profile
))
1785 pa_card_new_data_set_profile(&data
, default_profile
);
1787 pa_log_warn("Profile '%s' not valid or not supported by device.", default_profile
);
1790 u
->card
= pa_card_new(u
->core
, &data
);
1791 pa_card_new_data_done(&data
);
1794 pa_log("Failed to allocate card.");
1798 u
->card
->userdata
= u
;
1799 u
->card
->set_profile
= card_set_profile
;
1801 d
= PA_CARD_PROFILE_DATA(u
->card
->active_profile
);
1807 static int setup_dbus(struct userdata
*u
) {
1810 dbus_error_init(&error
);
1812 u
->connection
= pa_dbus_bus_get(u
->core
, DBUS_BUS_SYSTEM
, &error
);
1813 if (dbus_error_is_set(&error
) || (!u
->connection
)) {
1814 pa_log("Failed to get D-Bus connection: %s", error
.message
);
1815 dbus_error_free(&error
);
1822 static int find_device(struct userdata
*u
, const char *address
, const char *path
) {
1825 if (!address
&& !path
) {
1826 pa_log_error("Failed to get device address/path from module arguments.");
1831 if (!(u
->device
= pa_bluetooth_get_device(pa_dbus_connection_get(u
->connection
), path
))) {
1832 pa_log_error("%s is not a valid BlueZ audio device.", path
);
1836 if (address
&& !(pa_streq(u
->device
->address
, address
))) {
1837 pa_log_error("Passed path %s and address %s don't match.", path
, address
);
1841 if (!(u
->device
= pa_bluetooth_find_device(pa_dbus_connection_get(u
->connection
), address
))) {
1842 pa_log_error("%s is not known.", address
);
1850 int pa__init(pa_module
* m
) {
1854 const char *address
, *path
;
1858 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
1859 pa_log_error("Failed to parse module arguments");
1863 m
->userdata
= u
= pa_xnew0(struct userdata
, 1);
1868 u
->read_smoother
= pa_smoother_new(PA_USEC_PER_SEC
, PA_USEC_PER_SEC
*2, TRUE
, 10);
1869 u
->sample_spec
= m
->core
->default_sample_spec
;
1872 if (pa_modargs_get_value(ma
, "sco_sink", NULL
) &&
1873 !(u
->hsp
.sco_sink
= pa_namereg_get(m
->core
, pa_modargs_get_value(ma
, "sco_sink", NULL
), PA_NAMEREG_SINK
))) {
1874 pa_log("SCO sink not found");
1878 if (pa_modargs_get_value(ma
, "sco_source", NULL
) &&
1879 !(u
->hsp
.sco_source
= pa_namereg_get(m
->core
, pa_modargs_get_value(ma
, "sco_source", NULL
), PA_NAMEREG_SOURCE
))) {
1880 pa_log("SCO source not found");
1884 if (pa_modargs_get_value_u32(ma
, "rate", &u
->sample_spec
.rate
) < 0 ||
1885 u
->sample_spec
.rate
<= 0 || u
->sample_spec
.rate
> PA_RATE_MAX
) {
1886 pa_log_error("Failed to get rate from module arguments");
1890 channels
= u
->sample_spec
.channels
;
1891 if (pa_modargs_get_value_u32(ma
, "channels", &channels
) < 0 ||
1892 channels
<= 0 || channels
> PA_CHANNELS_MAX
) {
1893 pa_log_error("Failed to get channels from module arguments");
1896 u
->sample_spec
.channels
= (uint8_t) channels
;
1897 u
->requested_sample_spec
= u
->sample_spec
;
1899 if (setup_dbus(u
) < 0)
1902 address
= pa_modargs_get_value(ma
, "address", NULL
);
1903 path
= pa_modargs_get_value(ma
, "path", NULL
);
1905 if (find_device(u
, address
, path
) < 0)
1908 pa_assert(u
->device
);
1910 /* Add the card structure. This will also initialize the default profile */
1911 if (add_card(u
, pa_modargs_get_value(ma
, "profile", NULL
)) < 0)
1914 /* Connect to the BT service and query capabilities */
1918 if (u
->profile
!= PROFILE_OFF
)
1919 if (init_profile(u
) < 0)
1922 /* if (u->path) { */
1923 /* DBusError err; */
1924 /* dbus_error_init(&err); */
1928 /* if (!dbus_connection_add_filter(pa_dbus_connection_get(u->conn), filter_cb, u, NULL)) { */
1929 /* pa_log_error("Failed to add filter function"); */
1933 /* if (u->transport == BT_CAPABILITIES_TRANSPORT_SCO || */
1934 /* u->transport == BT_CAPABILITIES_TRANSPORT_ANY) { */
1935 /* t = pa_sprintf_malloc("type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged',path='%s'", u->path); */
1936 /* dbus_bus_add_match(pa_dbus_connection_get(u->conn), t, &err); */
1939 /* if (dbus_error_is_set(&err)) { */
1940 /* pa_log_error("Unable to subscribe to org.bluez.Headset signals: %s: %s", err.name, err.message); */
1945 /* if (u->transport == BT_CAPABILITIES_TRANSPORT_A2DP || */
1946 /* u->transport == BT_CAPABILITIES_TRANSPORT_ANY) { */
1947 /* t = pa_sprintf_malloc("type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged',path='%s'", u->path); */
1948 /* dbus_bus_add_match(pa_dbus_connection_get(u->conn), t, &err); */
1951 /* if (dbus_error_is_set(&err)) { */
1952 /* pa_log_error("Unable to subscribe to org.bluez.AudioSink signals: %s: %s", err.name, err.message); */
1958 if (u
->sink
|| u
->source
)
1959 if (start_thread(u
) < 0)
1969 int pa__get_n_used(pa_module
*m
) {
1973 pa_assert_se(u
= m
->userdata
);
1976 (u
->sink
? pa_sink_linked_by(u
->sink
) : 0) +
1977 (u
->source
? pa_source_linked_by(u
->source
) : 0);
1980 void pa__done(pa_module
*m
) {
1984 if (!(u
= m
->userdata
))
1987 if (u
->sink
&& !USE_SCO_OVER_PCM(u
))
1988 pa_sink_unlink(u
->sink
);
1990 if (u
->source
&& !USE_SCO_OVER_PCM(u
))
1991 pa_source_unlink(u
->source
);
1995 if (u
->connection
) {
1996 /* DBusError error; */
1999 /* if (u->transport == BT_CAPABILITIES_TRANSPORT_SCO || */
2000 /* u->transport == BT_CAPABILITIES_TRANSPORT_ANY) { */
2002 /* t = pa_sprintf_malloc("type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged',path='%s'", u->path); */
2003 /* dbus_error_init(&error); */
2004 /* dbus_bus_remove_match(pa_dbus_connection_get(u->conn), t, &error); */
2005 /* dbus_error_free(&error); */
2009 /* if (u->transport == BT_CAPABILITIES_TRANSPORT_A2DP || */
2010 /* u->transport == BT_CAPABILITIES_TRANSPORT_ANY) { */
2012 /* t = pa_sprintf_malloc("type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged',path='%s'", u->path); */
2013 /* dbus_error_init(&error); */
2014 /* dbus_bus_remove_match(pa_dbus_connection_get(u->conn), t, &error); */
2015 /* dbus_error_free(&error); */
2019 /* dbus_connection_remove_filter(pa_dbus_connection_get(u->conn), filter_cb, u); */
2020 pa_dbus_connection_unref(u
->connection
);
2024 pa_card_free(u
->card
);
2026 if (u
->read_smoother
)
2027 pa_smoother_free(u
->read_smoother
);
2032 pa_bluetooth_device_free(u
->device
);
2034 if (u
->write_memchunk
.memblock
)
2035 pa_memblock_unref(u
->write_memchunk
.memblock
);
2038 pa_xfree(u
->a2dp
.buffer
);
2040 sbc_finish(&u
->a2dp
.sbc
);
2043 pa_modargs_free(u
->modargs
);