2 This file is part of PulseAudio.
4 Copyright 2008-2009 Joao Paulo Rechi Vita
5 Copyright 2011-2012 BMW Car IT GmbH.
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as
9 published by the Free Software Foundation; either version 2.1 of the
10 License, or (at your option) any later version.
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 #include <linux/sockios.h>
31 #include <arpa/inet.h>
33 #include <pulse/rtclock.h>
34 #include <pulse/sample.h>
35 #include <pulse/timeval.h>
36 #include <pulse/xmalloc.h>
38 #include <pulsecore/i18n.h>
39 #include <pulsecore/module.h>
40 #include <pulsecore/modargs.h>
41 #include <pulsecore/core-rtclock.h>
42 #include <pulsecore/core-util.h>
43 #include <pulsecore/core-error.h>
44 #include <pulsecore/shared.h>
45 #include <pulsecore/socket-util.h>
46 #include <pulsecore/thread.h>
47 #include <pulsecore/thread-mq.h>
48 #include <pulsecore/poll.h>
49 #include <pulsecore/rtpoll.h>
50 #include <pulsecore/time-smoother.h>
51 #include <pulsecore/namereg.h>
55 #include "module-bluetooth-device-symdef.h"
56 #include "a2dp-codecs.h"
58 #include "bluetooth-util.h"
60 #define BITPOOL_DEC_LIMIT 32
61 #define BITPOOL_DEC_STEP 5
63 PA_MODULE_AUTHOR("Joao Paulo Rechi Vita");
64 PA_MODULE_DESCRIPTION("Bluetooth audio sink and source");
65 PA_MODULE_VERSION(PACKAGE_VERSION
);
66 PA_MODULE_LOAD_ONCE(false);
68 "name=<name for the card/sink/source, to be prefixed> "
69 "card_name=<name for the card> "
70 "card_properties=<properties for the card> "
71 "sink_name=<name for the sink> "
72 "sink_properties=<properties for the sink> "
73 "source_name=<name for the source> "
74 "source_properties=<properties for the source> "
75 "address=<address of the device> "
76 "profile=<a2dp|hsp|hfgw> "
78 "channels=<number of channels> "
79 "path=<device object path> "
80 "auto_connect=<automatically connect?> "
81 "sco_sink=<SCO over PCM sink name> "
82 "sco_source=<SCO over PCM source name>");
84 /* TODO: not close fd when entering suspend mode in a2dp */
86 static const char* const valid_modargs
[] = {
106 sbc_t sbc
; /* Codec data */
107 bool sbc_initialized
; /* Keep track if the encoder is initialized */
108 size_t codesize
, frame_length
; /* SBC Codesize, frame_length. We simply cache those values here */
110 void* buffer
; /* Codec transfer buffer */
111 size_t buffer_size
; /* Size of the buffer */
113 uint16_t seq_num
; /* Cumulative packet sequence */
120 void (*sco_sink_set_volume
)(pa_sink
*s
);
121 pa_source
*sco_source
;
122 void (*sco_source_set_volume
)(pa_source
*s
);
125 struct bluetooth_msg
{
130 typedef struct bluetooth_msg bluetooth_msg
;
131 PA_DEFINE_PRIVATE_CLASS(bluetooth_msg
, pa_msgobject
);
132 #define BLUETOOTH_MSG(o) (bluetooth_msg_cast(o))
138 pa_bluetooth_device
*device
;
139 pa_hook_slot
*uuid_added_slot
;
142 pa_bluetooth_transport
*transport
;
143 bool transport_acquired
;
144 pa_hook_slot
*discovery_slot
;
145 pa_hook_slot
*sink_state_changed_slot
;
146 pa_hook_slot
*source_state_changed_slot
;
147 pa_hook_slot
*transport_state_changed_slot
;
148 pa_hook_slot
*transport_nrec_changed_slot
;
149 pa_hook_slot
*transport_microphone_changed_slot
;
150 pa_hook_slot
*transport_speaker_changed_slot
;
152 pa_bluetooth_discovery
*discovery
;
155 char *output_port_name
;
156 char *input_port_name
;
162 pa_thread_mq thread_mq
;
164 pa_rtpoll_item
*rtpoll_item
;
168 uint64_t read_index
, write_index
;
169 pa_usec_t started_at
;
170 pa_smoother
*read_smoother
;
172 pa_memchunk write_memchunk
;
174 pa_sample_spec sample_spec
, requested_sample_spec
;
178 size_t read_link_mtu
;
179 size_t read_block_size
;
181 size_t write_link_mtu
;
182 size_t write_block_size
;
184 struct a2dp_info a2dp
;
187 enum profile profile
;
191 int stream_write_type
;
195 BLUETOOTH_MESSAGE_IO_THREAD_FAILED
,
196 BLUETOOTH_MESSAGE_MAX
199 #define FIXED_LATENCY_PLAYBACK_A2DP (25*PA_USEC_PER_MSEC)
200 #define FIXED_LATENCY_RECORD_A2DP (25*PA_USEC_PER_MSEC)
201 #define FIXED_LATENCY_PLAYBACK_HSP (125*PA_USEC_PER_MSEC)
202 #define FIXED_LATENCY_RECORD_HSP (25*PA_USEC_PER_MSEC)
204 #define MAX_PLAYBACK_CATCH_UP_USEC (100*PA_USEC_PER_MSEC)
206 #define USE_SCO_OVER_PCM(u) (u->profile == PROFILE_HSP && (u->hsp.sco_sink && u->hsp.sco_source))
208 static int init_profile(struct userdata
*u
);
211 static void a2dp_set_bitpool(struct userdata
*u
, uint8_t bitpool
) {
212 struct a2dp_info
*a2dp
;
218 if (a2dp
->sbc
.bitpool
== bitpool
)
221 if (bitpool
> a2dp
->max_bitpool
)
222 bitpool
= a2dp
->max_bitpool
;
223 else if (bitpool
< a2dp
->min_bitpool
)
224 bitpool
= a2dp
->min_bitpool
;
226 a2dp
->sbc
.bitpool
= bitpool
;
228 a2dp
->codesize
= sbc_get_codesize(&a2dp
->sbc
);
229 a2dp
->frame_length
= sbc_get_frame_length(&a2dp
->sbc
);
231 pa_log_debug("Bitpool has changed to %u", a2dp
->sbc
.bitpool
);
234 (u
->read_link_mtu
- sizeof(struct rtp_header
) - sizeof(struct rtp_payload
))
235 / a2dp
->frame_length
* a2dp
->codesize
;
237 u
->write_block_size
=
238 (u
->write_link_mtu
- sizeof(struct rtp_header
) - sizeof(struct rtp_payload
))
239 / a2dp
->frame_length
* a2dp
->codesize
;
241 pa_sink_set_max_request_within_thread(u
->sink
, u
->write_block_size
);
242 pa_sink_set_fixed_latency_within_thread(u
->sink
,
243 FIXED_LATENCY_PLAYBACK_A2DP
+ pa_bytes_to_usec(u
->write_block_size
, &u
->sample_spec
));
246 /* from IO thread, except in SCO over PCM */
247 static void bt_transport_config_mtu(struct userdata
*u
) {
248 /* Calculate block sizes */
249 if (u
->profile
== PROFILE_HSP
|| u
->profile
== PROFILE_HFGW
) {
250 u
->read_block_size
= u
->read_link_mtu
;
251 u
->write_block_size
= u
->write_link_mtu
;
254 (u
->read_link_mtu
- sizeof(struct rtp_header
) - sizeof(struct rtp_payload
))
255 / u
->a2dp
.frame_length
* u
->a2dp
.codesize
;
257 u
->write_block_size
=
258 (u
->write_link_mtu
- sizeof(struct rtp_header
) - sizeof(struct rtp_payload
))
259 / u
->a2dp
.frame_length
* u
->a2dp
.codesize
;
262 if (USE_SCO_OVER_PCM(u
))
266 pa_sink_set_max_request_within_thread(u
->sink
, u
->write_block_size
);
267 pa_sink_set_fixed_latency_within_thread(u
->sink
,
268 (u
->profile
== PROFILE_A2DP
?
269 FIXED_LATENCY_PLAYBACK_A2DP
: FIXED_LATENCY_PLAYBACK_HSP
) +
270 pa_bytes_to_usec(u
->write_block_size
, &u
->sample_spec
));
274 pa_source_set_fixed_latency_within_thread(u
->source
,
275 (u
->profile
== PROFILE_A2DP_SOURCE
?
276 FIXED_LATENCY_RECORD_A2DP
: FIXED_LATENCY_RECORD_HSP
) +
277 pa_bytes_to_usec(u
->read_block_size
, &u
->sample_spec
));
280 /* from IO thread, except in SCO over PCM */
282 static void setup_stream(struct userdata
*u
) {
283 struct pollfd
*pollfd
;
286 pa_log_info("Transport %s resuming", u
->transport
->path
);
288 bt_transport_config_mtu(u
);
290 pa_make_fd_nonblock(u
->stream_fd
);
291 pa_make_socket_low_delay(u
->stream_fd
);
294 if (setsockopt(u
->stream_fd
, SOL_SOCKET
, SO_TIMESTAMP
, &one
, sizeof(one
)) < 0)
295 pa_log_warn("Failed to enable SO_TIMESTAMP: %s", pa_cstrerror(errno
));
297 pa_log_debug("Stream properly set up, we're ready to roll!");
299 if (u
->profile
== PROFILE_A2DP
)
300 a2dp_set_bitpool(u
, u
->a2dp
.max_bitpool
);
302 u
->rtpoll_item
= pa_rtpoll_item_new(u
->rtpoll
, PA_RTPOLL_NEVER
, 1);
303 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
304 pollfd
->fd
= u
->stream_fd
;
305 pollfd
->events
= pollfd
->revents
= 0;
307 u
->read_index
= u
->write_index
= 0;
311 u
->read_smoother
= pa_smoother_new(
321 static void teardown_stream(struct userdata
*u
) {
322 if (u
->rtpoll_item
) {
323 pa_rtpoll_item_free(u
->rtpoll_item
);
324 u
->rtpoll_item
= NULL
;
327 if (u
->stream_fd
>= 0) {
328 pa_close(u
->stream_fd
);
332 if (u
->read_smoother
) {
333 pa_smoother_free(u
->read_smoother
);
334 u
->read_smoother
= NULL
;
337 if (u
->write_memchunk
.memblock
) {
338 pa_memblock_unref(u
->write_memchunk
.memblock
);
339 pa_memchunk_reset(&u
->write_memchunk
);
342 pa_log_debug("Audio stream torn down");
345 static void bt_transport_release(struct userdata
*u
) {
346 pa_assert(u
->transport
);
348 /* Ignore if already released */
349 if (!u
->transport_acquired
)
352 pa_log_debug("Releasing transport %s", u
->transport
->path
);
354 pa_bluetooth_transport_release(u
->transport
);
356 u
->transport_acquired
= false;
361 static int bt_transport_acquire(struct userdata
*u
, bool optional
) {
362 pa_assert(u
->transport
);
364 if (u
->transport_acquired
)
367 pa_log_debug("Acquiring transport %s", u
->transport
->path
);
369 u
->stream_fd
= pa_bluetooth_transport_acquire(u
->transport
, optional
, &u
->read_link_mtu
, &u
->write_link_mtu
);
370 if (u
->stream_fd
< 0)
373 u
->transport_acquired
= true;
374 pa_log_info("Transport %s acquired: fd %d", u
->transport
->path
, u
->stream_fd
);
379 /* Run from IO thread */
380 static int sink_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
381 struct userdata
*u
= PA_SINK(o
)->userdata
;
385 pa_assert(u
->sink
== PA_SINK(o
));
386 pa_assert(u
->transport
);
390 case PA_SINK_MESSAGE_SET_STATE
:
392 switch ((pa_sink_state_t
) PA_PTR_TO_UINT(data
)) {
394 case PA_SINK_SUSPENDED
:
395 /* Ignore if transition is PA_SINK_INIT->PA_SINK_SUSPENDED */
396 if (!PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
))
399 /* Stop the device if the source is suspended as well */
400 if (!u
->source
|| u
->source
->state
== PA_SOURCE_SUSPENDED
)
401 /* We deliberately ignore whether stopping
402 * actually worked. Since the stream_fd is
403 * closed it doesn't really matter */
404 bt_transport_release(u
);
409 case PA_SINK_RUNNING
:
410 if (u
->sink
->thread_info
.state
!= PA_SINK_SUSPENDED
)
413 /* Resume the device if the source was suspended as well */
414 if (!u
->source
|| !PA_SOURCE_IS_OPENED(u
->source
->thread_info
.state
)) {
415 if (bt_transport_acquire(u
, false) < 0)
422 case PA_SINK_UNLINKED
:
424 case PA_SINK_INVALID_STATE
:
429 case PA_SINK_MESSAGE_GET_LATENCY
: {
431 if (u
->read_smoother
) {
434 ri
= pa_smoother_get(u
->read_smoother
, pa_rtclock_now());
435 wi
= pa_bytes_to_usec(u
->write_index
+ u
->write_block_size
, &u
->sample_spec
);
437 *((pa_usec_t
*) data
) = wi
> ri
? wi
- ri
: 0;
441 ri
= pa_rtclock_now() - u
->started_at
;
442 wi
= pa_bytes_to_usec(u
->write_index
, &u
->sample_spec
);
444 *((pa_usec_t
*) data
) = wi
> ri
? wi
- ri
: 0;
447 *((pa_usec_t
*) data
) += u
->sink
->thread_info
.fixed_latency
;
452 r
= pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
454 return (r
< 0 || !failed
) ? r
: -1;
457 /* Run from IO thread */
458 static int source_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
459 struct userdata
*u
= PA_SOURCE(o
)->userdata
;
463 pa_assert(u
->source
== PA_SOURCE(o
));
464 pa_assert(u
->transport
);
468 case PA_SOURCE_MESSAGE_SET_STATE
:
470 switch ((pa_source_state_t
) PA_PTR_TO_UINT(data
)) {
472 case PA_SOURCE_SUSPENDED
:
473 /* Ignore if transition is PA_SOURCE_INIT->PA_SOURCE_SUSPENDED */
474 if (!PA_SOURCE_IS_OPENED(u
->source
->thread_info
.state
))
477 /* Stop the device if the sink is suspended as well */
478 if (!u
->sink
|| u
->sink
->state
== PA_SINK_SUSPENDED
)
479 bt_transport_release(u
);
481 if (u
->read_smoother
)
482 pa_smoother_pause(u
->read_smoother
, pa_rtclock_now());
486 case PA_SOURCE_RUNNING
:
487 if (u
->source
->thread_info
.state
!= PA_SOURCE_SUSPENDED
)
490 /* Resume the device if the sink was suspended as well */
491 if (!u
->sink
|| !PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
)) {
492 if (bt_transport_acquire(u
, false) < 0)
497 /* We don't resume the smoother here. Instead we
498 * wait until the first packet arrives */
501 case PA_SOURCE_UNLINKED
:
503 case PA_SOURCE_INVALID_STATE
:
508 case PA_SOURCE_MESSAGE_GET_LATENCY
: {
511 if (u
->read_smoother
) {
512 wi
= pa_smoother_get(u
->read_smoother
, pa_rtclock_now());
513 ri
= pa_bytes_to_usec(u
->read_index
, &u
->sample_spec
);
515 *((pa_usec_t
*) data
) = (wi
> ri
? wi
- ri
: 0) + u
->source
->thread_info
.fixed_latency
;
517 *((pa_usec_t
*) data
) = 0;
524 r
= pa_source_process_msg(o
, code
, data
, offset
, chunk
);
526 return (r
< 0 || !failed
) ? r
: -1;
529 /* Called from main thread context */
530 static int device_process_msg(pa_msgobject
*obj
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
531 struct bluetooth_msg
*u
= BLUETOOTH_MSG(obj
);
534 case BLUETOOTH_MESSAGE_IO_THREAD_FAILED
: {
535 if (u
->card
->module
->unload_requested
)
538 pa_log_debug("Switching the profile to off due to IO thread failure.");
540 pa_assert_se(pa_card_set_profile(u
->card
, "off", false) >= 0);
547 /* Run from IO thread */
548 static int hsp_process_render(struct userdata
*u
) {
552 pa_assert(u
->profile
== PROFILE_HSP
|| u
->profile
== PROFILE_HFGW
);
555 /* First, render some data */
556 if (!u
->write_memchunk
.memblock
)
557 pa_sink_render_full(u
->sink
, u
->write_block_size
, &u
->write_memchunk
);
559 pa_assert(u
->write_memchunk
.length
== u
->write_block_size
);
565 /* Now write that data to the socket. The socket is of type
566 * SEQPACKET, and we generated the data of the MTU size, so this
567 * should just work. */
569 p
= (const uint8_t *) pa_memblock_acquire_chunk(&u
->write_memchunk
);
570 l
= pa_write(u
->stream_fd
, p
, u
->write_memchunk
.length
, &u
->stream_write_type
);
571 pa_memblock_release(u
->write_memchunk
.memblock
);
578 /* Retry right away if we got interrupted */
581 else if (errno
== EAGAIN
)
582 /* Hmm, apparently the socket was not writable, give up for now */
585 pa_log_error("Failed to write data to SCO socket: %s", pa_cstrerror(errno
));
590 pa_assert((size_t) l
<= u
->write_memchunk
.length
);
592 if ((size_t) l
!= u
->write_memchunk
.length
) {
593 pa_log_error("Wrote memory block to socket only partially! %llu written, wanted to write %llu.",
594 (unsigned long long) l
,
595 (unsigned long long) u
->write_memchunk
.length
);
600 u
->write_index
+= (uint64_t) u
->write_memchunk
.length
;
601 pa_memblock_unref(u
->write_memchunk
.memblock
);
602 pa_memchunk_reset(&u
->write_memchunk
);
611 /* Run from IO thread */
612 static int hsp_process_push(struct userdata
*u
) {
614 pa_memchunk memchunk
;
617 pa_assert(u
->profile
== PROFILE_HSP
|| u
->profile
== PROFILE_HFGW
);
618 pa_assert(u
->source
);
619 pa_assert(u
->read_smoother
);
621 memchunk
.memblock
= pa_memblock_new(u
->core
->mempool
, u
->read_block_size
);
622 memchunk
.index
= memchunk
.length
= 0;
631 bool found_tstamp
= false;
634 memset(&m
, 0, sizeof(m
));
635 memset(&aux
, 0, sizeof(aux
));
636 memset(&iov
, 0, sizeof(iov
));
641 m
.msg_controllen
= sizeof(aux
);
643 p
= pa_memblock_acquire(memchunk
.memblock
);
645 iov
.iov_len
= pa_memblock_get_length(memchunk
.memblock
);
646 l
= recvmsg(u
->stream_fd
, &m
, 0);
647 pa_memblock_release(memchunk
.memblock
);
651 if (l
< 0 && errno
== EINTR
)
652 /* Retry right away if we got interrupted */
655 else if (l
< 0 && errno
== EAGAIN
)
656 /* Hmm, apparently the socket was not readable, give up for now. */
659 pa_log_error("Failed to read data from SCO socket: %s", l
< 0 ? pa_cstrerror(errno
) : "EOF");
664 pa_assert((size_t) l
<= pa_memblock_get_length(memchunk
.memblock
));
666 /* In some rare occasions, we might receive packets of a very strange
667 * size. This could potentially be possible if the SCO packet was
668 * received partially over-the-air, or more probably due to hardware
669 * issues in our Bluetooth adapter. In these cases, in order to avoid
670 * an assertion failure due to unaligned data, just discard the whole
672 if (!pa_frame_aligned(l
, &u
->sample_spec
)) {
673 pa_log_warn("SCO packet received of unaligned size: %zu", l
);
677 memchunk
.length
= (size_t) l
;
678 u
->read_index
+= (uint64_t) l
;
680 for (cm
= CMSG_FIRSTHDR(&m
); cm
; cm
= CMSG_NXTHDR(&m
, cm
))
681 if (cm
->cmsg_level
== SOL_SOCKET
&& cm
->cmsg_type
== SO_TIMESTAMP
) {
682 struct timeval
*tv
= (struct timeval
*) CMSG_DATA(cm
);
683 pa_rtclock_from_wallclock(tv
);
684 tstamp
= pa_timeval_load(tv
);
690 pa_log_warn("Couldn't find SO_TIMESTAMP data in auxiliary recvmsg() data!");
691 tstamp
= pa_rtclock_now();
694 pa_smoother_put(u
->read_smoother
, tstamp
, pa_bytes_to_usec(u
->read_index
, &u
->sample_spec
));
695 pa_smoother_resume(u
->read_smoother
, tstamp
, true);
697 pa_source_post(u
->source
, &memchunk
);
703 pa_memblock_unref(memchunk
.memblock
);
708 /* Run from IO thread */
709 static void a2dp_prepare_buffer(struct userdata
*u
) {
710 size_t min_buffer_size
= PA_MAX(u
->read_link_mtu
, u
->write_link_mtu
);
714 if (u
->a2dp
.buffer_size
>= min_buffer_size
)
717 u
->a2dp
.buffer_size
= 2 * min_buffer_size
;
718 pa_xfree(u
->a2dp
.buffer
);
719 u
->a2dp
.buffer
= pa_xmalloc(u
->a2dp
.buffer_size
);
722 /* Run from IO thread */
723 static int a2dp_process_render(struct userdata
*u
) {
724 struct a2dp_info
*a2dp
;
725 struct rtp_header
*header
;
726 struct rtp_payload
*payload
;
730 size_t to_write
, to_encode
;
731 unsigned frame_count
;
735 pa_assert(u
->profile
== PROFILE_A2DP
);
738 /* First, render some data */
739 if (!u
->write_memchunk
.memblock
)
740 pa_sink_render_full(u
->sink
, u
->write_block_size
, &u
->write_memchunk
);
742 pa_assert(u
->write_memchunk
.length
== u
->write_block_size
);
744 a2dp_prepare_buffer(u
);
747 header
= a2dp
->buffer
;
748 payload
= (struct rtp_payload
*) ((uint8_t*) a2dp
->buffer
+ sizeof(*header
));
752 /* Try to create a packet of the full MTU */
754 p
= (const uint8_t *) pa_memblock_acquire_chunk(&u
->write_memchunk
);
755 to_encode
= u
->write_memchunk
.length
;
757 d
= (uint8_t*) a2dp
->buffer
+ sizeof(*header
) + sizeof(*payload
);
758 to_write
= a2dp
->buffer_size
- sizeof(*header
) - sizeof(*payload
);
760 while (PA_LIKELY(to_encode
> 0 && to_write
> 0)) {
764 encoded
= sbc_encode(&a2dp
->sbc
,
769 if (PA_UNLIKELY(encoded
<= 0)) {
770 pa_log_error("SBC encoding error (%li)", (long) encoded
);
771 pa_memblock_release(u
->write_memchunk
.memblock
);
775 /* pa_log_debug("SBC: encoded: %lu; written: %lu", (unsigned long) encoded, (unsigned long) written); */
776 /* pa_log_debug("SBC: codesize: %lu; frame_length: %lu", (unsigned long) a2dp->codesize, (unsigned long) a2dp->frame_length); */
778 pa_assert_fp((size_t) encoded
<= to_encode
);
779 pa_assert_fp((size_t) encoded
== a2dp
->codesize
);
781 pa_assert_fp((size_t) written
<= to_write
);
782 pa_assert_fp((size_t) written
== a2dp
->frame_length
);
784 p
= (const uint8_t*) p
+ encoded
;
785 to_encode
-= encoded
;
787 d
= (uint8_t*) d
+ written
;
793 pa_memblock_release(u
->write_memchunk
.memblock
);
795 pa_assert(to_encode
== 0);
798 pa_log_debug("Using SBC encoder implementation: %s", pa_strnull(sbc_get_implementation_info(&a2dp
->sbc
)));
801 /* write it to the fifo */
802 memset(a2dp
->buffer
, 0, sizeof(*header
) + sizeof(*payload
));
805 header
->sequence_number
= htons(a2dp
->seq_num
++);
806 header
->timestamp
= htonl(u
->write_index
/ pa_frame_size(&u
->sample_spec
));
807 header
->ssrc
= htonl(1);
808 payload
->frame_count
= frame_count
;
810 nbytes
= (uint8_t*) d
- (uint8_t*) a2dp
->buffer
;
815 l
= pa_write(u
->stream_fd
, a2dp
->buffer
, nbytes
, &u
->stream_write_type
);
822 /* Retry right away if we got interrupted */
825 else if (errno
== EAGAIN
)
826 /* Hmm, apparently the socket was not writable, give up for now */
829 pa_log_error("Failed to write data to socket: %s", pa_cstrerror(errno
));
834 pa_assert((size_t) l
<= nbytes
);
836 if ((size_t) l
!= nbytes
) {
837 pa_log_warn("Wrote memory block to socket only partially! %llu written, wanted to write %llu.",
838 (unsigned long long) l
,
839 (unsigned long long) nbytes
);
844 u
->write_index
+= (uint64_t) u
->write_memchunk
.length
;
845 pa_memblock_unref(u
->write_memchunk
.memblock
);
846 pa_memchunk_reset(&u
->write_memchunk
);
856 static int a2dp_process_push(struct userdata
*u
) {
858 pa_memchunk memchunk
;
861 pa_assert(u
->profile
== PROFILE_A2DP_SOURCE
);
862 pa_assert(u
->source
);
863 pa_assert(u
->read_smoother
);
865 memchunk
.memblock
= pa_memblock_new(u
->core
->mempool
, u
->read_block_size
);
866 memchunk
.index
= memchunk
.length
= 0;
869 bool found_tstamp
= false;
871 struct a2dp_info
*a2dp
;
872 struct rtp_header
*header
;
873 struct rtp_payload
*payload
;
877 size_t to_write
, to_decode
;
879 a2dp_prepare_buffer(u
);
882 header
= a2dp
->buffer
;
883 payload
= (struct rtp_payload
*) ((uint8_t*) a2dp
->buffer
+ sizeof(*header
));
885 l
= pa_read(u
->stream_fd
, a2dp
->buffer
, a2dp
->buffer_size
, &u
->stream_write_type
);
889 if (l
< 0 && errno
== EINTR
)
890 /* Retry right away if we got interrupted */
893 else if (l
< 0 && errno
== EAGAIN
)
894 /* Hmm, apparently the socket was not readable, give up for now. */
897 pa_log_error("Failed to read data from socket: %s", l
< 0 ? pa_cstrerror(errno
) : "EOF");
902 pa_assert((size_t) l
<= a2dp
->buffer_size
);
904 u
->read_index
+= (uint64_t) l
;
906 /* TODO: get timestamp from rtp */
908 /* pa_log_warn("Couldn't find SO_TIMESTAMP data in auxiliary recvmsg() data!"); */
909 tstamp
= pa_rtclock_now();
912 pa_smoother_put(u
->read_smoother
, tstamp
, pa_bytes_to_usec(u
->read_index
, &u
->sample_spec
));
913 pa_smoother_resume(u
->read_smoother
, tstamp
, true);
915 p
= (uint8_t*) a2dp
->buffer
+ sizeof(*header
) + sizeof(*payload
);
916 to_decode
= l
- sizeof(*header
) - sizeof(*payload
);
918 d
= pa_memblock_acquire(memchunk
.memblock
);
919 to_write
= memchunk
.length
= pa_memblock_get_length(memchunk
.memblock
);
921 while (PA_LIKELY(to_decode
> 0)) {
925 decoded
= sbc_decode(&a2dp
->sbc
,
930 if (PA_UNLIKELY(decoded
<= 0)) {
931 pa_log_error("SBC decoding error (%li)", (long) decoded
);
932 pa_memblock_release(memchunk
.memblock
);
933 pa_memblock_unref(memchunk
.memblock
);
937 /* pa_log_debug("SBC: decoded: %lu; written: %lu", (unsigned long) decoded, (unsigned long) written); */
938 /* pa_log_debug("SBC: frame_length: %lu; codesize: %lu", (unsigned long) a2dp->frame_length, (unsigned long) a2dp->codesize); */
940 /* Reset frame length, it can be changed due to bitpool change */
941 a2dp
->frame_length
= sbc_get_frame_length(&a2dp
->sbc
);
943 pa_assert_fp((size_t) decoded
<= to_decode
);
944 pa_assert_fp((size_t) decoded
== a2dp
->frame_length
);
946 pa_assert_fp((size_t) written
== a2dp
->codesize
);
948 p
= (const uint8_t*) p
+ decoded
;
949 to_decode
-= decoded
;
951 d
= (uint8_t*) d
+ written
;
955 memchunk
.length
-= to_write
;
957 pa_memblock_release(memchunk
.memblock
);
959 pa_source_post(u
->source
, &memchunk
);
965 pa_memblock_unref(memchunk
.memblock
);
970 static void a2dp_reduce_bitpool(struct userdata
*u
) {
971 struct a2dp_info
*a2dp
;
978 /* Check if bitpool is already at its limit */
979 if (a2dp
->sbc
.bitpool
<= BITPOOL_DEC_LIMIT
)
982 bitpool
= a2dp
->sbc
.bitpool
- BITPOOL_DEC_STEP
;
984 if (bitpool
< BITPOOL_DEC_LIMIT
)
985 bitpool
= BITPOOL_DEC_LIMIT
;
987 a2dp_set_bitpool(u
, bitpool
);
990 static void thread_func(void *userdata
) {
991 struct userdata
*u
= userdata
;
992 unsigned do_write
= 0;
993 unsigned pending_read_bytes
= 0;
994 bool writable
= false;
997 pa_assert(u
->transport
);
999 pa_log_debug("IO Thread starting up");
1001 if (u
->core
->realtime_scheduling
)
1002 pa_make_realtime(u
->core
->realtime_priority
);
1004 pa_thread_mq_install(&u
->thread_mq
);
1006 /* Setup the stream only if the transport was already acquired */
1007 if (u
->transport_acquired
)
1011 struct pollfd
*pollfd
;
1013 bool disable_timer
= true;
1015 pollfd
= u
->rtpoll_item
? pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
) : NULL
;
1017 if (u
->source
&& PA_SOURCE_IS_LINKED(u
->source
->thread_info
.state
)) {
1019 /* We should send two blocks to the device before we expect
1022 if (u
->write_index
== 0 && u
->read_index
<= 0)
1025 if (pollfd
&& (pollfd
->revents
& POLLIN
)) {
1028 if (u
->profile
== PROFILE_HSP
|| u
->profile
== PROFILE_HFGW
)
1029 n_read
= hsp_process_push(u
);
1031 n_read
= a2dp_process_push(u
);
1036 /* We just read something, so we are supposed to write something, too */
1037 pending_read_bytes
+= n_read
;
1038 do_write
+= pending_read_bytes
/ u
->write_block_size
;
1039 pending_read_bytes
= pending_read_bytes
% u
->write_block_size
;
1043 if (u
->sink
&& PA_SINK_IS_LINKED(u
->sink
->thread_info
.state
)) {
1045 if (PA_UNLIKELY(u
->sink
->thread_info
.rewind_requested
))
1046 pa_sink_process_rewind(u
->sink
, 0);
1049 if (pollfd
->revents
& POLLOUT
)
1052 if ((!u
->source
|| !PA_SOURCE_IS_LINKED(u
->source
->thread_info
.state
)) && do_write
<= 0 && writable
) {
1053 pa_usec_t time_passed
;
1054 pa_usec_t audio_sent
;
1056 /* Hmm, there is no input stream we could synchronize
1057 * to. So let's do things by time */
1059 time_passed
= pa_rtclock_now() - u
->started_at
;
1060 audio_sent
= pa_bytes_to_usec(u
->write_index
, &u
->sample_spec
);
1062 if (audio_sent
<= time_passed
) {
1063 pa_usec_t audio_to_send
= time_passed
- audio_sent
;
1065 /* Never try to catch up for more than 100ms */
1066 if (u
->write_index
> 0 && audio_to_send
> MAX_PLAYBACK_CATCH_UP_USEC
) {
1067 pa_usec_t skip_usec
;
1068 uint64_t skip_bytes
;
1070 skip_usec
= audio_to_send
- MAX_PLAYBACK_CATCH_UP_USEC
;
1071 skip_bytes
= pa_usec_to_bytes(skip_usec
, &u
->sample_spec
);
1073 if (skip_bytes
> 0) {
1076 pa_log_warn("Skipping %llu us (= %llu bytes) in audio stream",
1077 (unsigned long long) skip_usec
,
1078 (unsigned long long) skip_bytes
);
1080 pa_sink_render_full(u
->sink
, skip_bytes
, &tmp
);
1081 pa_memblock_unref(tmp
.memblock
);
1082 u
->write_index
+= skip_bytes
;
1084 if (u
->profile
== PROFILE_A2DP
)
1085 a2dp_reduce_bitpool(u
);
1090 pending_read_bytes
= 0;
1094 if (writable
&& do_write
> 0) {
1097 if (u
->write_index
<= 0)
1098 u
->started_at
= pa_rtclock_now();
1100 if (u
->profile
== PROFILE_A2DP
) {
1101 if ((n_written
= a2dp_process_render(u
)) < 0)
1104 if ((n_written
= hsp_process_render(u
)) < 0)
1109 pa_log("Broken kernel: we got EAGAIN on write() after POLLOUT!");
1111 do_write
-= n_written
;
1115 if ((!u
->source
|| !PA_SOURCE_IS_LINKED(u
->source
->thread_info
.state
)) && do_write
<= 0) {
1116 pa_usec_t sleep_for
;
1117 pa_usec_t time_passed
, next_write_at
;
1120 /* Hmm, there is no input stream we could synchronize
1121 * to. So let's estimate when we need to wake up the latest */
1122 time_passed
= pa_rtclock_now() - u
->started_at
;
1123 next_write_at
= pa_bytes_to_usec(u
->write_index
, &u
->sample_spec
);
1124 sleep_for
= time_passed
< next_write_at
? next_write_at
- time_passed
: 0;
1125 /* 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); */
1127 /* drop stream every 500 ms */
1128 sleep_for
= PA_USEC_PER_MSEC
* 500;
1130 pa_rtpoll_set_timer_relative(u
->rtpoll
, sleep_for
);
1131 disable_timer
= false;
1137 pa_rtpoll_set_timer_disabled(u
->rtpoll
);
1139 /* Hmm, nothing to do. Let's sleep */
1141 pollfd
->events
= (short) (((u
->sink
&& PA_SINK_IS_LINKED(u
->sink
->thread_info
.state
) && !writable
) ? POLLOUT
: 0) |
1142 (u
->source
&& PA_SOURCE_IS_LINKED(u
->source
->thread_info
.state
) ? POLLIN
: 0));
1144 if ((ret
= pa_rtpoll_run(u
->rtpoll
, true)) < 0) {
1145 pa_log_debug("pa_rtpoll_run failed with: %d", ret
);
1149 pa_log_debug("IO thread shutdown requested, stopping cleanly");
1150 bt_transport_release(u
);
1154 pollfd
= u
->rtpoll_item
? pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
) : NULL
;
1156 if (pollfd
&& (pollfd
->revents
& ~(POLLOUT
|POLLIN
))) {
1157 pa_log_info("FD error: %s%s%s%s",
1158 pollfd
->revents
& POLLERR
? "POLLERR " :"",
1159 pollfd
->revents
& POLLHUP
? "POLLHUP " :"",
1160 pollfd
->revents
& POLLPRI
? "POLLPRI " :"",
1161 pollfd
->revents
& POLLNVAL
? "POLLNVAL " :"");
1168 /* In case of HUP, just tear down the streams */
1169 if (!pollfd
|| (pollfd
->revents
& POLLHUP
) == 0)
1173 pending_read_bytes
= 0;
1180 /* If this was no regular exit from the loop we have to continue processing messages until we receive PA_MESSAGE_SHUTDOWN */
1181 pa_log_debug("IO thread failed");
1182 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(u
->msg
), BLUETOOTH_MESSAGE_IO_THREAD_FAILED
, NULL
, 0, NULL
, NULL
);
1183 pa_asyncmsgq_wait_for(u
->thread_mq
.inq
, PA_MESSAGE_SHUTDOWN
);
1186 pa_log_debug("IO thread shutting down");
1189 static pa_available_t
transport_state_to_availability(pa_bluetooth_transport_state_t state
) {
1190 if (state
== PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED
)
1191 return PA_AVAILABLE_NO
;
1192 else if (state
>= PA_BLUETOOTH_TRANSPORT_STATE_PLAYING
)
1193 return PA_AVAILABLE_YES
;
1195 return PA_AVAILABLE_UNKNOWN
;
1198 static pa_direction_t
get_profile_direction(enum profile p
) {
1199 static const pa_direction_t profile_direction
[] = {
1200 [PROFILE_A2DP
] = PA_DIRECTION_OUTPUT
,
1201 [PROFILE_A2DP_SOURCE
] = PA_DIRECTION_INPUT
,
1202 [PROFILE_HSP
] = PA_DIRECTION_INPUT
| PA_DIRECTION_OUTPUT
,
1203 [PROFILE_HFGW
] = PA_DIRECTION_INPUT
| PA_DIRECTION_OUTPUT
,
1207 return profile_direction
[p
];
1210 /* Run from main thread */
1211 static pa_available_t
get_port_availability(struct userdata
*u
, pa_direction_t direction
) {
1212 pa_available_t result
= PA_AVAILABLE_NO
;
1216 pa_assert(u
->device
);
1218 for (i
= 0; i
< PA_BLUETOOTH_PROFILE_COUNT
; i
++) {
1219 pa_bluetooth_transport
*transport
;
1221 if (!(get_profile_direction(i
) & direction
))
1224 if (!(transport
= u
->device
->transports
[i
]))
1227 switch(transport
->state
) {
1228 case PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED
:
1231 case PA_BLUETOOTH_TRANSPORT_STATE_IDLE
:
1232 if (result
== PA_AVAILABLE_NO
)
1233 result
= PA_AVAILABLE_UNKNOWN
;
1237 case PA_BLUETOOTH_TRANSPORT_STATE_PLAYING
:
1238 return PA_AVAILABLE_YES
;
1245 /* Run from main thread */
1246 static void handle_transport_state_change(struct userdata
*u
, struct pa_bluetooth_transport
*transport
) {
1247 bool acquire
= false;
1248 bool release
= false;
1249 enum profile profile
;
1250 pa_card_profile
*cp
;
1251 pa_bluetooth_transport_state_t state
;
1252 pa_device_port
*port
;
1255 pa_assert(transport
);
1257 profile
= transport
->profile
;
1258 state
= transport
->state
;
1260 /* Update profile availability */
1261 if (!(cp
= pa_hashmap_get(u
->card
->profiles
, pa_bt_profile_to_string(profile
))))
1264 pa_card_profile_set_available(cp
, transport_state_to_availability(state
));
1266 /* Update port availability */
1267 pa_assert_se(port
= pa_hashmap_get(u
->card
->ports
, u
->output_port_name
));
1268 pa_device_port_set_available(port
, get_port_availability(u
, PA_DIRECTION_OUTPUT
));
1270 pa_assert_se(port
= pa_hashmap_get(u
->card
->ports
, u
->input_port_name
));
1271 pa_device_port_set_available(port
, get_port_availability(u
, PA_DIRECTION_INPUT
));
1273 /* Acquire or release transport as needed */
1274 acquire
= (state
== PA_BLUETOOTH_TRANSPORT_STATE_PLAYING
&& u
->profile
== profile
);
1275 release
= (state
!= PA_BLUETOOTH_TRANSPORT_STATE_PLAYING
&& u
->profile
== profile
);
1278 if (bt_transport_acquire(u
, true) >= 0) {
1280 pa_log_debug("Resuming source %s, because the bluetooth audio state changed to 'playing'.", u
->source
->name
);
1281 pa_source_suspend(u
->source
, false, PA_SUSPEND_IDLE
|PA_SUSPEND_USER
);
1285 pa_log_debug("Resuming sink %s, because the bluetooth audio state changed to 'playing'.", u
->sink
->name
);
1286 pa_sink_suspend(u
->sink
, false, PA_SUSPEND_IDLE
|PA_SUSPEND_USER
);
1290 if (release
&& u
->transport_acquired
) {
1291 /* FIXME: this release is racy, since the audio stream might have
1292 been set up again in the meantime (but not processed yet by PA).
1293 BlueZ should probably release the transport automatically, and
1294 in that case we would just mark the transport as released */
1296 /* Remote side closed the stream so we consider it PA_SUSPEND_USER */
1298 pa_log_debug("Suspending source %s, because the remote end closed the stream.", u
->source
->name
);
1299 pa_source_suspend(u
->source
, true, PA_SUSPEND_USER
);
1303 pa_log_debug("Suspending sink %s, because the remote end closed the stream.", u
->sink
->name
);
1304 pa_sink_suspend(u
->sink
, true, PA_SUSPEND_USER
);
1309 /* Run from main thread */
1310 static void sink_set_volume_cb(pa_sink
*s
) {
1319 k
= pa_sprintf_malloc("bluetooth-device@%p", (void*) s
);
1320 u
= pa_shared_get(s
->core
, k
);
1324 pa_assert(u
->sink
== s
);
1325 pa_assert(u
->profile
== PROFILE_HSP
);
1326 pa_assert(u
->transport
);
1328 gain
= (dbus_uint16_t
) round((double) pa_cvolume_max(&s
->real_volume
) * HSP_MAX_GAIN
/ PA_VOLUME_NORM
);
1329 volume
= (pa_volume_t
) round((double) gain
* PA_VOLUME_NORM
/ HSP_MAX_GAIN
);
1331 pa_cvolume_set(&s
->real_volume
, u
->sample_spec
.channels
, volume
);
1333 pa_bluetooth_transport_set_speaker_gain(u
->transport
, gain
);
1336 /* Run from main thread */
1337 static void source_set_volume_cb(pa_source
*s
) {
1346 k
= pa_sprintf_malloc("bluetooth-device@%p", (void*) s
);
1347 u
= pa_shared_get(s
->core
, k
);
1351 pa_assert(u
->source
== s
);
1352 pa_assert(u
->profile
== PROFILE_HSP
);
1353 pa_assert(u
->transport
);
1355 gain
= (dbus_uint16_t
) round((double) pa_cvolume_max(&s
->real_volume
) * HSP_MAX_GAIN
/ PA_VOLUME_NORM
);
1356 volume
= (pa_volume_t
) round((double) gain
* PA_VOLUME_NORM
/ HSP_MAX_GAIN
);
1358 pa_cvolume_set(&s
->real_volume
, u
->sample_spec
.channels
, volume
);
1360 pa_bluetooth_transport_set_microphone_gain(u
->transport
, gain
);
1363 /* Run from main thread */
1364 static char *get_name(const char *type
, pa_modargs
*ma
, const char *device_id
, bool *namereg_fail
) {
1370 pa_assert(device_id
);
1371 pa_assert(namereg_fail
);
1373 t
= pa_sprintf_malloc("%s_name", type
);
1374 n
= pa_modargs_get_value(ma
, t
, NULL
);
1378 *namereg_fail
= true;
1379 return pa_xstrdup(n
);
1382 if ((n
= pa_modargs_get_value(ma
, "name", NULL
)))
1383 *namereg_fail
= true;
1386 *namereg_fail
= false;
1389 return pa_sprintf_malloc("bluez_%s.%s", type
, n
);
1392 static int sco_over_pcm_state_update(struct userdata
*u
, bool changed
) {
1394 pa_assert(USE_SCO_OVER_PCM(u
));
1396 if (PA_SINK_IS_OPENED(pa_sink_get_state(u
->hsp
.sco_sink
)) ||
1397 PA_SOURCE_IS_OPENED(pa_source_get_state(u
->hsp
.sco_source
))) {
1399 if (u
->stream_fd
>= 0)
1402 pa_log_debug("Resuming SCO over PCM");
1403 if (init_profile(u
) < 0) {
1404 pa_log("Can't resume SCO over PCM");
1408 if (bt_transport_acquire(u
, false) < 0)
1417 if (u
->stream_fd
< 0)
1420 pa_log_debug("Closing SCO over PCM");
1422 bt_transport_release(u
);
1428 static pa_hook_result_t
sink_state_changed_cb(pa_core
*c
, pa_sink
*s
, struct userdata
*u
) {
1430 pa_sink_assert_ref(s
);
1433 if (!USE_SCO_OVER_PCM(u
) || s
!= u
->hsp
.sco_sink
)
1436 sco_over_pcm_state_update(u
, true);
1441 static pa_hook_result_t
source_state_changed_cb(pa_core
*c
, pa_source
*s
, struct userdata
*u
) {
1443 pa_source_assert_ref(s
);
1446 if (!USE_SCO_OVER_PCM(u
) || s
!= u
->hsp
.sco_source
)
1449 sco_over_pcm_state_update(u
, true);
1454 static pa_hook_result_t
transport_nrec_changed_cb(pa_bluetooth_discovery
*y
, pa_bluetooth_transport
*t
, struct userdata
*u
) {
1460 if (t
!= u
->transport
)
1463 p
= pa_proplist_new();
1464 pa_proplist_sets(p
, "bluetooth.nrec", t
->nrec
? "1" : "0");
1465 pa_source_update_proplist(u
->source
, PA_UPDATE_REPLACE
, p
);
1466 pa_proplist_free(p
);
1471 static pa_hook_result_t
transport_microphone_gain_changed_cb(pa_bluetooth_discovery
*y
, pa_bluetooth_transport
*t
,
1472 struct userdata
*u
) {
1478 if (t
!= u
->transport
)
1481 pa_assert(u
->source
);
1483 pa_cvolume_set(&v
, u
->sample_spec
.channels
,
1484 (pa_volume_t
) round((double) t
->microphone_gain
* PA_VOLUME_NORM
/ HSP_MAX_GAIN
));
1485 pa_source_volume_changed(u
->source
, &v
);
1490 static pa_hook_result_t
transport_speaker_gain_changed_cb(pa_bluetooth_discovery
*y
, pa_bluetooth_transport
*t
,
1491 struct userdata
*u
) {
1497 if (t
!= u
->transport
)
1502 pa_cvolume_set(&v
, u
->sample_spec
.channels
, (pa_volume_t
) round((double) t
->speaker_gain
* PA_VOLUME_NORM
/ HSP_MAX_GAIN
));
1503 pa_sink_volume_changed(u
->sink
, &v
);
1508 static void connect_ports(struct userdata
*u
, void *sink_or_source_new_data
, pa_direction_t direction
) {
1509 pa_device_port
*port
;
1511 if (direction
== PA_DIRECTION_OUTPUT
) {
1512 pa_sink_new_data
*sink_new_data
= sink_or_source_new_data
;
1514 pa_assert_se(port
= pa_hashmap_get(u
->card
->ports
, u
->output_port_name
));
1515 pa_assert_se(pa_hashmap_put(sink_new_data
->ports
, port
->name
, port
) >= 0);
1516 pa_device_port_ref(port
);
1518 pa_source_new_data
*source_new_data
= sink_or_source_new_data
;
1520 pa_assert_se(port
= pa_hashmap_get(u
->card
->ports
, u
->input_port_name
));
1521 pa_assert_se(pa_hashmap_put(source_new_data
->ports
, port
->name
, port
) >= 0);
1522 pa_device_port_ref(port
);
1526 static int sink_set_port_cb(pa_sink
*s
, pa_device_port
*p
) {
1530 static int source_set_port_cb(pa_source
*s
, pa_device_port
*p
) {
1534 /* Run from main thread */
1535 static int add_sink(struct userdata
*u
) {
1538 pa_assert(u
->transport
);
1540 if (USE_SCO_OVER_PCM(u
)) {
1543 u
->sink
= u
->hsp
.sco_sink
;
1544 p
= pa_proplist_new();
1545 pa_proplist_sets(p
, "bluetooth.protocol", pa_bt_profile_to_string(u
->profile
));
1546 pa_proplist_update(u
->sink
->proplist
, PA_UPDATE_MERGE
, p
);
1547 pa_proplist_free(p
);
1549 pa_sink_new_data data
;
1552 pa_sink_new_data_init(&data
);
1553 data
.driver
= __FILE__
;
1554 data
.module
= u
->module
;
1555 pa_sink_new_data_set_sample_spec(&data
, &u
->sample_spec
);
1556 pa_proplist_sets(data
.proplist
, "bluetooth.protocol", pa_bt_profile_to_string(u
->profile
));
1557 if (u
->profile
== PROFILE_HSP
)
1558 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_INTENDED_ROLES
, "phone");
1559 data
.card
= u
->card
;
1560 data
.name
= get_name("sink", u
->modargs
, u
->address
, &b
);
1561 data
.namereg_fail
= b
;
1563 if (pa_modargs_get_proplist(u
->modargs
, "sink_properties", data
.proplist
, PA_UPDATE_REPLACE
) < 0) {
1564 pa_log("Invalid properties");
1565 pa_sink_new_data_done(&data
);
1568 connect_ports(u
, &data
, PA_DIRECTION_OUTPUT
);
1570 if (!u
->transport_acquired
)
1571 switch (u
->profile
) {
1574 pa_assert_not_reached(); /* Profile switch should have failed */
1577 data
.suspend_cause
= PA_SUSPEND_USER
;
1579 case PROFILE_A2DP_SOURCE
:
1581 pa_assert_not_reached();
1584 u
->sink
= pa_sink_new(u
->core
, &data
, PA_SINK_HARDWARE
|PA_SINK_LATENCY
);
1585 pa_sink_new_data_done(&data
);
1588 pa_log_error("Failed to create sink");
1592 u
->sink
->userdata
= u
;
1593 u
->sink
->parent
.process_msg
= sink_process_msg
;
1594 u
->sink
->set_port
= sink_set_port_cb
;
1597 if (u
->profile
== PROFILE_HSP
) {
1598 pa_sink_set_set_volume_callback(u
->sink
, sink_set_volume_cb
);
1599 u
->sink
->n_volume_steps
= 16;
1601 k
= pa_sprintf_malloc("bluetooth-device@%p", (void*) u
->sink
);
1602 pa_shared_set(u
->core
, k
, u
);
1609 /* Run from main thread */
1610 static int add_source(struct userdata
*u
) {
1613 pa_assert(u
->transport
);
1615 if (USE_SCO_OVER_PCM(u
)) {
1616 u
->source
= u
->hsp
.sco_source
;
1617 pa_proplist_sets(u
->source
->proplist
, "bluetooth.protocol", pa_bt_profile_to_string(u
->profile
));
1619 pa_source_new_data data
;
1622 pa_source_new_data_init(&data
);
1623 data
.driver
= __FILE__
;
1624 data
.module
= u
->module
;
1625 pa_source_new_data_set_sample_spec(&data
, &u
->sample_spec
);
1626 pa_proplist_sets(data
.proplist
, "bluetooth.protocol", pa_bt_profile_to_string(u
->profile
));
1627 if (u
->profile
== PROFILE_HSP
)
1628 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_INTENDED_ROLES
, "phone");
1630 data
.card
= u
->card
;
1631 data
.name
= get_name("source", u
->modargs
, u
->address
, &b
);
1632 data
.namereg_fail
= b
;
1634 if (pa_modargs_get_proplist(u
->modargs
, "source_properties", data
.proplist
, PA_UPDATE_REPLACE
) < 0) {
1635 pa_log("Invalid properties");
1636 pa_source_new_data_done(&data
);
1640 connect_ports(u
, &data
, PA_DIRECTION_INPUT
);
1642 if (!u
->transport_acquired
)
1643 switch (u
->profile
) {
1645 pa_assert_not_reached(); /* Profile switch should have failed */
1647 case PROFILE_A2DP_SOURCE
:
1649 data
.suspend_cause
= PA_SUSPEND_USER
;
1653 pa_assert_not_reached();
1656 u
->source
= pa_source_new(u
->core
, &data
, PA_SOURCE_HARDWARE
|PA_SOURCE_LATENCY
);
1657 pa_source_new_data_done(&data
);
1660 pa_log_error("Failed to create source");
1664 u
->source
->userdata
= u
;
1665 u
->source
->parent
.process_msg
= source_process_msg
;
1666 u
->source
->set_port
= source_set_port_cb
;
1669 if ((u
->profile
== PROFILE_HSP
) || (u
->profile
== PROFILE_HFGW
)) {
1670 pa_bluetooth_transport
*t
= u
->transport
;
1671 pa_proplist_sets(u
->source
->proplist
, "bluetooth.nrec", t
->nrec
? "1" : "0");
1674 if (u
->profile
== PROFILE_HSP
) {
1675 pa_source_set_set_volume_callback(u
->source
, source_set_volume_cb
);
1676 u
->source
->n_volume_steps
= 16;
1678 k
= pa_sprintf_malloc("bluetooth-device@%p", (void*) u
->source
);
1679 pa_shared_set(u
->core
, k
, u
);
1686 static void bt_transport_config_a2dp(struct userdata
*u
) {
1687 const pa_bluetooth_transport
*t
;
1688 struct a2dp_info
*a2dp
= &u
->a2dp
;
1694 config
= (a2dp_sbc_t
*) t
->config
;
1696 u
->sample_spec
.format
= PA_SAMPLE_S16LE
;
1698 if (a2dp
->sbc_initialized
)
1699 sbc_reinit(&a2dp
->sbc
, 0);
1701 sbc_init(&a2dp
->sbc
, 0);
1702 a2dp
->sbc_initialized
= true;
1704 switch (config
->frequency
) {
1705 case SBC_SAMPLING_FREQ_16000
:
1706 a2dp
->sbc
.frequency
= SBC_FREQ_16000
;
1707 u
->sample_spec
.rate
= 16000U;
1709 case SBC_SAMPLING_FREQ_32000
:
1710 a2dp
->sbc
.frequency
= SBC_FREQ_32000
;
1711 u
->sample_spec
.rate
= 32000U;
1713 case SBC_SAMPLING_FREQ_44100
:
1714 a2dp
->sbc
.frequency
= SBC_FREQ_44100
;
1715 u
->sample_spec
.rate
= 44100U;
1717 case SBC_SAMPLING_FREQ_48000
:
1718 a2dp
->sbc
.frequency
= SBC_FREQ_48000
;
1719 u
->sample_spec
.rate
= 48000U;
1722 pa_assert_not_reached();
1725 switch (config
->channel_mode
) {
1726 case SBC_CHANNEL_MODE_MONO
:
1727 a2dp
->sbc
.mode
= SBC_MODE_MONO
;
1728 u
->sample_spec
.channels
= 1;
1730 case SBC_CHANNEL_MODE_DUAL_CHANNEL
:
1731 a2dp
->sbc
.mode
= SBC_MODE_DUAL_CHANNEL
;
1732 u
->sample_spec
.channels
= 2;
1734 case SBC_CHANNEL_MODE_STEREO
:
1735 a2dp
->sbc
.mode
= SBC_MODE_STEREO
;
1736 u
->sample_spec
.channels
= 2;
1738 case SBC_CHANNEL_MODE_JOINT_STEREO
:
1739 a2dp
->sbc
.mode
= SBC_MODE_JOINT_STEREO
;
1740 u
->sample_spec
.channels
= 2;
1743 pa_assert_not_reached();
1746 switch (config
->allocation_method
) {
1747 case SBC_ALLOCATION_SNR
:
1748 a2dp
->sbc
.allocation
= SBC_AM_SNR
;
1750 case SBC_ALLOCATION_LOUDNESS
:
1751 a2dp
->sbc
.allocation
= SBC_AM_LOUDNESS
;
1754 pa_assert_not_reached();
1757 switch (config
->subbands
) {
1758 case SBC_SUBBANDS_4
:
1759 a2dp
->sbc
.subbands
= SBC_SB_4
;
1761 case SBC_SUBBANDS_8
:
1762 a2dp
->sbc
.subbands
= SBC_SB_8
;
1765 pa_assert_not_reached();
1768 switch (config
->block_length
) {
1769 case SBC_BLOCK_LENGTH_4
:
1770 a2dp
->sbc
.blocks
= SBC_BLK_4
;
1772 case SBC_BLOCK_LENGTH_8
:
1773 a2dp
->sbc
.blocks
= SBC_BLK_8
;
1775 case SBC_BLOCK_LENGTH_12
:
1776 a2dp
->sbc
.blocks
= SBC_BLK_12
;
1778 case SBC_BLOCK_LENGTH_16
:
1779 a2dp
->sbc
.blocks
= SBC_BLK_16
;
1782 pa_assert_not_reached();
1785 a2dp
->min_bitpool
= config
->min_bitpool
;
1786 a2dp
->max_bitpool
= config
->max_bitpool
;
1788 /* Set minimum bitpool for source to get the maximum possible block_size */
1789 a2dp
->sbc
.bitpool
= u
->profile
== PROFILE_A2DP
? a2dp
->max_bitpool
: a2dp
->min_bitpool
;
1790 a2dp
->codesize
= sbc_get_codesize(&a2dp
->sbc
);
1791 a2dp
->frame_length
= sbc_get_frame_length(&a2dp
->sbc
);
1793 pa_log_info("SBC parameters:\n\tallocation=%u\n\tsubbands=%u\n\tblocks=%u\n\tbitpool=%u\n",
1794 a2dp
->sbc
.allocation
, a2dp
->sbc
.subbands
, a2dp
->sbc
.blocks
, a2dp
->sbc
.bitpool
);
1797 static void bt_transport_config(struct userdata
*u
) {
1798 if (u
->profile
== PROFILE_HSP
|| u
->profile
== PROFILE_HFGW
) {
1799 u
->sample_spec
.format
= PA_SAMPLE_S16LE
;
1800 u
->sample_spec
.channels
= 1;
1801 u
->sample_spec
.rate
= 8000;
1803 bt_transport_config_a2dp(u
);
1806 /* Run from main thread */
1807 static pa_hook_result_t
transport_state_changed_cb(pa_bluetooth_discovery
*y
, pa_bluetooth_transport
*t
, struct userdata
*u
) {
1811 if (t
== u
->transport
&& t
->state
== PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED
)
1812 pa_assert_se(pa_card_set_profile(u
->card
, "off", false) >= 0);
1814 if (t
->device
== u
->device
)
1815 handle_transport_state_change(u
, t
);
1820 /* Run from main thread */
1821 static int setup_transport(struct userdata
*u
) {
1822 pa_bluetooth_transport
*t
;
1825 pa_assert(!u
->transport
);
1826 pa_assert(u
->profile
!= PROFILE_OFF
);
1828 /* check if profile has a transport */
1829 t
= u
->device
->transports
[u
->profile
];
1830 if (!t
|| t
->state
== PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED
) {
1831 pa_log_warn("Profile has no transport");
1837 if (u
->profile
== PROFILE_A2DP_SOURCE
|| u
->profile
== PROFILE_HFGW
)
1838 bt_transport_acquire(u
, true); /* In case of error, the sink/sources will be created suspended */
1839 else if (bt_transport_acquire(u
, false) < 0)
1840 return -1; /* We need to fail here until the interactions with module-suspend-on-idle and alike get improved */
1842 bt_transport_config(u
);
1847 /* Run from main thread */
1848 static int init_profile(struct userdata
*u
) {
1851 pa_assert(u
->profile
!= PROFILE_OFF
);
1853 if (setup_transport(u
) < 0)
1856 pa_assert(u
->transport
);
1858 if (u
->profile
== PROFILE_A2DP
||
1859 u
->profile
== PROFILE_HSP
||
1860 u
->profile
== PROFILE_HFGW
)
1861 if (add_sink(u
) < 0)
1864 if (u
->profile
== PROFILE_HSP
||
1865 u
->profile
== PROFILE_A2DP_SOURCE
||
1866 u
->profile
== PROFILE_HFGW
)
1867 if (add_source(u
) < 0)
1873 /* Run from main thread */
1874 static void stop_thread(struct userdata
*u
) {
1879 if (u
->sink
&& !USE_SCO_OVER_PCM(u
))
1880 pa_sink_unlink(u
->sink
);
1882 if (u
->source
&& !USE_SCO_OVER_PCM(u
))
1883 pa_source_unlink(u
->source
);
1886 pa_asyncmsgq_send(u
->thread_mq
.inq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, 0, NULL
);
1887 pa_thread_free(u
->thread
);
1891 if (u
->rtpoll_item
) {
1892 pa_rtpoll_item_free(u
->rtpoll_item
);
1893 u
->rtpoll_item
= NULL
;
1897 pa_thread_mq_done(&u
->thread_mq
);
1899 pa_rtpoll_free(u
->rtpoll
);
1904 bt_transport_release(u
);
1905 u
->transport
= NULL
;
1909 if (u
->profile
== PROFILE_HSP
) {
1910 k
= pa_sprintf_malloc("bluetooth-device@%p", (void*) u
->sink
);
1911 pa_shared_remove(u
->core
, k
);
1915 pa_sink_unref(u
->sink
);
1920 if (u
->profile
== PROFILE_HSP
) {
1921 k
= pa_sprintf_malloc("bluetooth-device@%p", (void*) u
->source
);
1922 pa_shared_remove(u
->core
, k
);
1926 pa_source_unref(u
->source
);
1930 if (u
->read_smoother
) {
1931 pa_smoother_free(u
->read_smoother
);
1932 u
->read_smoother
= NULL
;
1936 /* Run from main thread */
1937 static int start_thread(struct userdata
*u
) {
1939 pa_assert(!u
->thread
);
1940 pa_assert(!u
->rtpoll
);
1941 pa_assert(!u
->rtpoll_item
);
1943 u
->rtpoll
= pa_rtpoll_new();
1944 pa_thread_mq_init(&u
->thread_mq
, u
->core
->mainloop
, u
->rtpoll
);
1946 if (USE_SCO_OVER_PCM(u
)) {
1947 if (sco_over_pcm_state_update(u
, false) < 0) {
1951 k
= pa_sprintf_malloc("bluetooth-device@%p", (void*) u
->sink
);
1952 pa_shared_remove(u
->core
, k
);
1957 k
= pa_sprintf_malloc("bluetooth-device@%p", (void*) u
->source
);
1958 pa_shared_remove(u
->core
, k
);
1965 pa_sink_ref(u
->sink
);
1966 pa_source_ref(u
->source
);
1967 /* FIXME: monitor stream_fd error */
1971 if (!(u
->thread
= pa_thread_new("bluetooth", thread_func
, u
))) {
1972 pa_log_error("Failed to create IO thread");
1977 pa_sink_set_asyncmsgq(u
->sink
, u
->thread_mq
.inq
);
1978 pa_sink_set_rtpoll(u
->sink
, u
->rtpoll
);
1979 pa_sink_put(u
->sink
);
1981 if (u
->sink
->set_volume
)
1982 u
->sink
->set_volume(u
->sink
);
1986 pa_source_set_asyncmsgq(u
->source
, u
->thread_mq
.inq
);
1987 pa_source_set_rtpoll(u
->source
, u
->rtpoll
);
1988 pa_source_put(u
->source
);
1990 if (u
->source
->set_volume
)
1991 u
->source
->set_volume(u
->source
);
1997 static void save_sco_volume_callbacks(struct userdata
*u
) {
1999 pa_assert(USE_SCO_OVER_PCM(u
));
2001 u
->hsp
.sco_sink_set_volume
= u
->hsp
.sco_sink
->set_volume
;
2002 u
->hsp
.sco_source_set_volume
= u
->hsp
.sco_source
->set_volume
;
2005 static void restore_sco_volume_callbacks(struct userdata
*u
) {
2007 pa_assert(USE_SCO_OVER_PCM(u
));
2009 pa_sink_set_set_volume_callback(u
->hsp
.sco_sink
, u
->hsp
.sco_sink_set_volume
);
2010 pa_source_set_set_volume_callback(u
->hsp
.sco_source
, u
->hsp
.sco_source_set_volume
);
2013 /* Run from main thread */
2014 static int card_set_profile(pa_card
*c
, pa_card_profile
*new_profile
) {
2019 pa_assert(new_profile
);
2020 pa_assert_se(u
= c
->userdata
);
2022 d
= PA_CARD_PROFILE_DATA(new_profile
);
2024 if (*d
!= PROFILE_OFF
) {
2025 const pa_bluetooth_device
*device
= u
->device
;
2027 if (!device
->transports
[*d
] || device
->transports
[*d
]->state
== PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED
) {
2028 pa_log_warn("Profile not connected, refused to switch profile to %s", new_profile
->name
);
2035 if (USE_SCO_OVER_PCM(u
))
2036 restore_sco_volume_callbacks(u
);
2039 u
->sample_spec
= u
->requested_sample_spec
;
2041 if (USE_SCO_OVER_PCM(u
))
2042 save_sco_volume_callbacks(u
);
2044 if (u
->profile
!= PROFILE_OFF
)
2045 if (init_profile(u
) < 0)
2048 if (u
->sink
|| u
->source
)
2049 if (start_thread(u
) < 0)
2057 pa_assert_se(pa_card_set_profile(u
->card
, "off", false) >= 0);
2062 /* Run from main thread */
2063 static void create_card_ports(struct userdata
*u
, pa_hashmap
*ports
) {
2064 pa_device_port
*port
;
2065 pa_device_port_new_data port_data
;
2067 const char *name_prefix
= NULL
;
2068 const char *input_description
= NULL
;
2069 const char *output_description
= NULL
;
2073 pa_assert(u
->device
);
2075 switch (pa_bluetooth_get_form_factor(u
->device
->class)) {
2076 case PA_BT_FORM_FACTOR_UNKNOWN
:
2079 case PA_BT_FORM_FACTOR_HEADSET
:
2080 name_prefix
= "headset";
2081 input_description
= output_description
= _("Headset");
2084 case PA_BT_FORM_FACTOR_HANDSFREE
:
2085 name_prefix
= "handsfree";
2086 input_description
= output_description
= _("Handsfree");
2089 case PA_BT_FORM_FACTOR_MICROPHONE
:
2090 name_prefix
= "microphone";
2091 input_description
= _("Microphone");
2094 case PA_BT_FORM_FACTOR_SPEAKER
:
2095 name_prefix
= "speaker";
2096 output_description
= _("Speaker");
2099 case PA_BT_FORM_FACTOR_HEADPHONE
:
2100 name_prefix
= "headphone";
2101 output_description
= _("Headphone");
2104 case PA_BT_FORM_FACTOR_PORTABLE
:
2105 name_prefix
= "portable";
2106 input_description
= output_description
= _("Portable");
2109 case PA_BT_FORM_FACTOR_CAR
:
2110 name_prefix
= "car";
2111 input_description
= output_description
= _("Car");
2114 case PA_BT_FORM_FACTOR_HIFI
:
2115 name_prefix
= "hifi";
2116 input_description
= output_description
= _("HiFi");
2119 case PA_BT_FORM_FACTOR_PHONE
:
2120 name_prefix
= "phone";
2121 input_description
= output_description
= _("Phone");
2126 name_prefix
= "unknown";
2128 if (!output_description
)
2129 output_description
= _("Bluetooth Output");
2131 if (!input_description
)
2132 input_description
= _("Bluetooth Input");
2134 u
->output_port_name
= pa_sprintf_malloc("%s-output", name_prefix
);
2135 u
->input_port_name
= pa_sprintf_malloc("%s-input", name_prefix
);
2137 pa_device_port_new_data_init(&port_data
);
2138 pa_device_port_new_data_set_name(&port_data
, u
->output_port_name
);
2139 pa_device_port_new_data_set_description(&port_data
, output_description
);
2140 pa_device_port_new_data_set_direction(&port_data
, PA_DIRECTION_OUTPUT
);
2141 pa_device_port_new_data_set_available(&port_data
, get_port_availability(u
, PA_DIRECTION_OUTPUT
));
2142 pa_assert_se(port
= pa_device_port_new(u
->core
, &port_data
, 0));
2143 pa_assert_se(pa_hashmap_put(ports
, port
->name
, port
) >= 0);
2144 pa_device_port_new_data_done(&port_data
);
2146 pa_device_port_new_data_init(&port_data
);
2147 pa_device_port_new_data_set_name(&port_data
, u
->input_port_name
);
2148 pa_device_port_new_data_set_description(&port_data
, input_description
);
2149 pa_device_port_new_data_set_direction(&port_data
, PA_DIRECTION_INPUT
);
2150 pa_device_port_new_data_set_available(&port_data
, get_port_availability(u
, PA_DIRECTION_INPUT
));
2151 pa_assert_se(port
= pa_device_port_new(u
->core
, &port_data
, 0));
2152 pa_assert_se(pa_hashmap_put(ports
, port
->name
, port
) >= 0);
2153 pa_device_port_new_data_done(&port_data
);
2156 /* Run from main thread */
2157 static pa_card_profile
*create_card_profile(struct userdata
*u
, const char *uuid
, pa_hashmap
*ports
) {
2158 pa_device_port
*input_port
, *output_port
;
2159 pa_card_profile
*p
= NULL
;
2162 pa_assert(u
->input_port_name
);
2163 pa_assert(u
->output_port_name
);
2164 pa_assert_se(input_port
= pa_hashmap_get(ports
, u
->input_port_name
));
2165 pa_assert_se(output_port
= pa_hashmap_get(ports
, u
->output_port_name
));
2167 if (pa_streq(uuid
, A2DP_SINK_UUID
)) {
2168 p
= pa_card_profile_new("a2dp", _("High Fidelity Playback (A2DP)"), sizeof(enum profile
));
2172 p
->max_sink_channels
= 2;
2173 p
->max_source_channels
= 0;
2174 pa_hashmap_put(output_port
->profiles
, p
->name
, p
);
2176 d
= PA_CARD_PROFILE_DATA(p
);
2178 } else if (pa_streq(uuid
, A2DP_SOURCE_UUID
)) {
2179 p
= pa_card_profile_new("a2dp_source", _("High Fidelity Capture (A2DP)"), sizeof(enum profile
));
2183 p
->max_sink_channels
= 0;
2184 p
->max_source_channels
= 2;
2185 pa_hashmap_put(input_port
->profiles
, p
->name
, p
);
2187 d
= PA_CARD_PROFILE_DATA(p
);
2188 *d
= PROFILE_A2DP_SOURCE
;
2189 } else if (pa_streq(uuid
, HSP_HS_UUID
) || pa_streq(uuid
, HFP_HS_UUID
)) {
2190 p
= pa_card_profile_new("hsp", _("Telephony Duplex (HSP/HFP)"), sizeof(enum profile
));
2194 p
->max_sink_channels
= 1;
2195 p
->max_source_channels
= 1;
2196 pa_hashmap_put(input_port
->profiles
, p
->name
, p
);
2197 pa_hashmap_put(output_port
->profiles
, p
->name
, p
);
2199 d
= PA_CARD_PROFILE_DATA(p
);
2201 } else if (pa_streq(uuid
, HFP_AG_UUID
)) {
2202 p
= pa_card_profile_new("hfgw", _("Handsfree Gateway"), sizeof(enum profile
));
2206 p
->max_sink_channels
= 1;
2207 p
->max_source_channels
= 1;
2208 pa_hashmap_put(input_port
->profiles
, p
->name
, p
);
2209 pa_hashmap_put(output_port
->profiles
, p
->name
, p
);
2211 d
= PA_CARD_PROFILE_DATA(p
);
2216 pa_bluetooth_transport
*t
;
2218 if ((t
= u
->device
->transports
[*d
]))
2219 p
->available
= transport_state_to_availability(t
->state
);
2225 /* Run from main thread */
2226 static int add_card(struct userdata
*u
) {
2227 pa_card_new_data data
;
2231 pa_bt_form_factor_t ff
;
2233 const char *default_profile
;
2234 const pa_bluetooth_device
*device
;
2235 const pa_bluetooth_uuid
*uuid
;
2238 pa_assert(u
->device
);
2242 pa_card_new_data_init(&data
);
2243 data
.driver
= __FILE__
;
2244 data
.module
= u
->module
;
2246 n
= pa_bluetooth_cleanup_name(device
->alias
);
2247 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, n
);
2249 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_STRING
, device
->address
);
2250 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_API
, "bluez");
2251 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_CLASS
, "sound");
2252 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_BUS
, "bluetooth");
2254 if ((ff
= pa_bluetooth_get_form_factor(device
->class)) != PA_BT_FORM_FACTOR_UNKNOWN
)
2255 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_FORM_FACTOR
, pa_bt_form_factor_to_string(ff
));
2257 pa_proplist_sets(data
.proplist
, "bluez.path", device
->path
);
2258 pa_proplist_setf(data
.proplist
, "bluez.class", "0x%06x", (unsigned) device
->class);
2259 pa_proplist_sets(data
.proplist
, "bluez.alias", device
->alias
);
2260 data
.name
= get_name("card", u
->modargs
, device
->address
, &b
);
2261 data
.namereg_fail
= b
;
2263 if (pa_modargs_get_proplist(u
->modargs
, "card_properties", data
.proplist
, PA_UPDATE_REPLACE
) < 0) {
2264 pa_log("Invalid properties");
2265 pa_card_new_data_done(&data
);
2269 create_card_ports(u
, data
.ports
);
2271 PA_LLIST_FOREACH(uuid
, device
->uuids
) {
2272 p
= create_card_profile(u
, uuid
->uuid
, data
.ports
);
2277 if (pa_hashmap_get(data
.profiles
, p
->name
)) {
2278 pa_card_profile_free(p
);
2282 pa_hashmap_put(data
.profiles
, p
->name
, p
);
2285 pa_assert(!pa_hashmap_isempty(data
.profiles
));
2287 p
= pa_card_profile_new("off", _("Off"), sizeof(enum profile
));
2288 p
->available
= PA_AVAILABLE_YES
;
2289 d
= PA_CARD_PROFILE_DATA(p
);
2291 pa_hashmap_put(data
.profiles
, p
->name
, p
);
2293 if ((default_profile
= pa_modargs_get_value(u
->modargs
, "profile", NULL
))) {
2294 if (pa_hashmap_get(data
.profiles
, default_profile
))
2295 pa_card_new_data_set_profile(&data
, default_profile
);
2297 pa_log_warn("Profile '%s' not valid or not supported by device.", default_profile
);
2300 u
->card
= pa_card_new(u
->core
, &data
);
2301 pa_card_new_data_done(&data
);
2304 pa_log("Failed to allocate card.");
2308 u
->card
->userdata
= u
;
2309 u
->card
->set_profile
= card_set_profile
;
2311 d
= PA_CARD_PROFILE_DATA(u
->card
->active_profile
);
2313 if (*d
!= PROFILE_OFF
&& (!device
->transports
[*d
] ||
2314 device
->transports
[*d
]->state
== PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED
)) {
2315 pa_log_warn("Default profile not connected, selecting off profile");
2316 u
->card
->active_profile
= pa_hashmap_get(u
->card
->profiles
, "off");
2317 u
->card
->save_profile
= false;
2320 d
= PA_CARD_PROFILE_DATA(u
->card
->active_profile
);
2323 if (USE_SCO_OVER_PCM(u
))
2324 save_sco_volume_callbacks(u
);
2329 /* Run from main thread */
2330 static pa_bluetooth_device
* find_device(struct userdata
*u
, const char *address
, const char *path
) {
2331 pa_bluetooth_device
*d
= NULL
;
2335 if (!address
&& !path
) {
2336 pa_log_error("Failed to get device address/path from module arguments.");
2341 if (!(d
= pa_bluetooth_discovery_get_by_path(u
->discovery
, path
))) {
2342 pa_log_error("%s is not a valid BlueZ audio device.", path
);
2346 if (address
&& !(pa_streq(d
->address
, address
))) {
2347 pa_log_error("Passed path %s address %s != %s don't match.", path
, d
->address
, address
);
2352 if (!(d
= pa_bluetooth_discovery_get_by_address(u
->discovery
, address
))) {
2353 pa_log_error("%s is not known.", address
);
2359 u
->address
= pa_xstrdup(d
->address
);
2360 u
->path
= pa_xstrdup(d
->path
);
2366 /* Run from main thread */
2367 static pa_hook_result_t
uuid_added_cb(pa_bluetooth_discovery
*y
, const struct pa_bluetooth_hook_uuid_data
*data
,
2368 struct userdata
*u
) {
2372 pa_assert(data
->device
);
2373 pa_assert(data
->uuid
);
2376 if (data
->device
!= u
->device
)
2379 p
= create_card_profile(u
, data
->uuid
, u
->card
->ports
);
2384 if (pa_hashmap_get(u
->card
->profiles
, p
->name
)) {
2385 pa_card_profile_free(p
);
2389 pa_card_add_profile(u
->card
, p
);
2394 /* Run from main thread */
2395 static pa_hook_result_t
discovery_hook_cb(pa_bluetooth_discovery
*y
, const pa_bluetooth_device
*d
, struct userdata
*u
) {
2403 pa_log_debug("Device %s removed: unloading module", d
->path
);
2404 else if (!pa_bluetooth_device_any_audio_connected(d
))
2405 pa_log_debug("Unloading module, because device %s doesn't have any audio profiles connected anymore.", d
->path
);
2409 pa_module_unload(u
->core
, u
->module
, true);
2414 int pa__init(pa_module
* m
) {
2418 const char *address
, *path
;
2419 pa_bluetooth_device
*device
;
2423 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
2424 pa_log_error("Failed to parse module arguments");
2428 m
->userdata
= u
= pa_xnew0(struct userdata
, 1);
2432 u
->sample_spec
= m
->core
->default_sample_spec
;
2435 if (pa_modargs_get_value(ma
, "sco_sink", NULL
) &&
2436 !(u
->hsp
.sco_sink
= pa_namereg_get(m
->core
, pa_modargs_get_value(ma
, "sco_sink", NULL
), PA_NAMEREG_SINK
))) {
2437 pa_log("SCO sink not found");
2441 if (pa_modargs_get_value(ma
, "sco_source", NULL
) &&
2442 !(u
->hsp
.sco_source
= pa_namereg_get(m
->core
, pa_modargs_get_value(ma
, "sco_source", NULL
), PA_NAMEREG_SOURCE
))) {
2443 pa_log("SCO source not found");
2447 if (pa_modargs_get_value_u32(ma
, "rate", &u
->sample_spec
.rate
) < 0 ||
2448 u
->sample_spec
.rate
<= 0 || u
->sample_spec
.rate
> PA_RATE_MAX
) {
2449 pa_log_error("Failed to get rate from module arguments");
2453 u
->auto_connect
= true;
2454 if (pa_modargs_get_value_boolean(ma
, "auto_connect", &u
->auto_connect
)) {
2455 pa_log("Failed to parse auto_connect= argument");
2459 channels
= u
->sample_spec
.channels
;
2460 if (pa_modargs_get_value_u32(ma
, "channels", &channels
) < 0 ||
2461 channels
<= 0 || channels
> PA_CHANNELS_MAX
) {
2462 pa_log_error("Failed to get channels from module arguments");
2465 u
->sample_spec
.channels
= (uint8_t) channels
;
2466 u
->requested_sample_spec
= u
->sample_spec
;
2468 address
= pa_modargs_get_value(ma
, "address", NULL
);
2469 path
= pa_modargs_get_value(ma
, "path", NULL
);
2471 if (!(u
->discovery
= pa_bluetooth_discovery_get(m
->core
)))
2474 if (!(device
= find_device(u
, address
, path
)))
2480 pa_hook_connect(pa_bluetooth_discovery_hook(u
->discovery
, PA_BLUETOOTH_HOOK_DEVICE_CONNECTION_CHANGED
),
2481 PA_HOOK_NORMAL
, (pa_hook_cb_t
) discovery_hook_cb
, u
);
2483 u
->uuid_added_slot
=
2484 pa_hook_connect(pa_bluetooth_discovery_hook(u
->discovery
, PA_BLUETOOTH_HOOK_DEVICE_UUID_ADDED
),
2485 PA_HOOK_NORMAL
, (pa_hook_cb_t
) uuid_added_cb
, u
);
2487 u
->sink_state_changed_slot
=
2488 pa_hook_connect(&u
->core
->hooks
[PA_CORE_HOOK_SINK_STATE_CHANGED
],
2489 PA_HOOK_NORMAL
, (pa_hook_cb_t
) sink_state_changed_cb
, u
);
2491 u
->source_state_changed_slot
=
2492 pa_hook_connect(&u
->core
->hooks
[PA_CORE_HOOK_SOURCE_STATE_CHANGED
],
2493 PA_HOOK_NORMAL
, (pa_hook_cb_t
) source_state_changed_cb
, u
);
2495 u
->transport_state_changed_slot
=
2496 pa_hook_connect(pa_bluetooth_discovery_hook(u
->discovery
, PA_BLUETOOTH_HOOK_TRANSPORT_STATE_CHANGED
),
2497 PA_HOOK_NORMAL
, (pa_hook_cb_t
) transport_state_changed_cb
, u
);
2499 u
->transport_nrec_changed_slot
=
2500 pa_hook_connect(pa_bluetooth_discovery_hook(u
->discovery
, PA_BLUETOOTH_HOOK_TRANSPORT_NREC_CHANGED
),
2501 PA_HOOK_NORMAL
, (pa_hook_cb_t
) transport_nrec_changed_cb
, u
);
2503 u
->transport_microphone_changed_slot
=
2504 pa_hook_connect(pa_bluetooth_discovery_hook(u
->discovery
, PA_BLUETOOTH_HOOK_TRANSPORT_MICROPHONE_GAIN_CHANGED
),
2505 PA_HOOK_NORMAL
, (pa_hook_cb_t
) transport_microphone_gain_changed_cb
, u
);
2507 u
->transport_speaker_changed_slot
=
2508 pa_hook_connect(pa_bluetooth_discovery_hook(u
->discovery
, PA_BLUETOOTH_HOOK_TRANSPORT_SPEAKER_GAIN_CHANGED
),
2509 PA_HOOK_NORMAL
, (pa_hook_cb_t
) transport_speaker_gain_changed_cb
, u
);
2511 /* Add the card structure. This will also initialize the default profile */
2512 if (add_card(u
) < 0)
2515 if (!(u
->msg
= pa_msgobject_new(bluetooth_msg
)))
2518 u
->msg
->parent
.process_msg
= device_process_msg
;
2519 u
->msg
->card
= u
->card
;
2521 if (u
->profile
!= PROFILE_OFF
)
2522 if (init_profile(u
) < 0)
2525 if (u
->sink
|| u
->source
)
2526 if (start_thread(u
) < 0)
2534 pa_assert_se(pa_card_set_profile(u
->card
, "off", false) >= 0);
2545 int pa__get_n_used(pa_module
*m
) {
2549 pa_assert_se(u
= m
->userdata
);
2552 (u
->sink
? pa_sink_linked_by(u
->sink
) : 0) +
2553 (u
->source
? pa_source_linked_by(u
->source
) : 0);
2556 void pa__done(pa_module
*m
) {
2561 if (!(u
= m
->userdata
))
2566 if (u
->discovery_slot
)
2567 pa_hook_slot_free(u
->discovery_slot
);
2569 if (u
->uuid_added_slot
)
2570 pa_hook_slot_free(u
->uuid_added_slot
);
2572 if (u
->sink_state_changed_slot
)
2573 pa_hook_slot_free(u
->sink_state_changed_slot
);
2575 if (u
->source_state_changed_slot
)
2576 pa_hook_slot_free(u
->source_state_changed_slot
);
2578 if (u
->transport_state_changed_slot
)
2579 pa_hook_slot_free(u
->transport_state_changed_slot
);
2581 if (u
->transport_nrec_changed_slot
)
2582 pa_hook_slot_free(u
->transport_nrec_changed_slot
);
2584 if (u
->transport_microphone_changed_slot
)
2585 pa_hook_slot_free(u
->transport_microphone_changed_slot
);
2587 if (u
->transport_speaker_changed_slot
)
2588 pa_hook_slot_free(u
->transport_speaker_changed_slot
);
2590 if (USE_SCO_OVER_PCM(u
))
2591 restore_sco_volume_callbacks(u
);
2597 pa_card_free(u
->card
);
2600 pa_xfree(u
->a2dp
.buffer
);
2602 sbc_finish(&u
->a2dp
.sbc
);
2605 pa_modargs_free(u
->modargs
);
2607 pa_xfree(u
->output_port_name
);
2608 pa_xfree(u
->input_port_name
);
2610 pa_xfree(u
->address
);
2614 pa_bluetooth_discovery_unref(u
->discovery
);