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
)
213 struct a2dp_info
*a2dp
;
219 if (a2dp
->sbc
.bitpool
== bitpool
)
222 if (bitpool
> a2dp
->max_bitpool
)
223 bitpool
= a2dp
->max_bitpool
;
224 else if (bitpool
< a2dp
->min_bitpool
)
225 bitpool
= a2dp
->min_bitpool
;
227 a2dp
->sbc
.bitpool
= bitpool
;
229 a2dp
->codesize
= sbc_get_codesize(&a2dp
->sbc
);
230 a2dp
->frame_length
= sbc_get_frame_length(&a2dp
->sbc
);
232 pa_log_debug("Bitpool has changed to %u", a2dp
->sbc
.bitpool
);
235 (u
->read_link_mtu
- sizeof(struct rtp_header
) - sizeof(struct rtp_payload
))
236 / a2dp
->frame_length
* a2dp
->codesize
;
238 u
->write_block_size
=
239 (u
->write_link_mtu
- sizeof(struct rtp_header
) - sizeof(struct rtp_payload
))
240 / a2dp
->frame_length
* a2dp
->codesize
;
242 pa_sink_set_max_request_within_thread(u
->sink
, u
->write_block_size
);
243 pa_sink_set_fixed_latency_within_thread(u
->sink
,
244 FIXED_LATENCY_PLAYBACK_A2DP
+ pa_bytes_to_usec(u
->write_block_size
, &u
->sample_spec
));
247 /* from IO thread, except in SCO over PCM */
248 static void bt_transport_config_mtu(struct userdata
*u
) {
249 /* Calculate block sizes */
250 if (u
->profile
== PROFILE_HSP
|| u
->profile
== PROFILE_HFGW
) {
251 u
->read_block_size
= u
->read_link_mtu
;
252 u
->write_block_size
= u
->write_link_mtu
;
255 (u
->read_link_mtu
- sizeof(struct rtp_header
) - sizeof(struct rtp_payload
))
256 / u
->a2dp
.frame_length
* u
->a2dp
.codesize
;
258 u
->write_block_size
=
259 (u
->write_link_mtu
- sizeof(struct rtp_header
) - sizeof(struct rtp_payload
))
260 / u
->a2dp
.frame_length
* u
->a2dp
.codesize
;
263 if (USE_SCO_OVER_PCM(u
))
267 pa_sink_set_max_request_within_thread(u
->sink
, u
->write_block_size
);
268 pa_sink_set_fixed_latency_within_thread(u
->sink
,
269 (u
->profile
== PROFILE_A2DP
?
270 FIXED_LATENCY_PLAYBACK_A2DP
: FIXED_LATENCY_PLAYBACK_HSP
) +
271 pa_bytes_to_usec(u
->write_block_size
, &u
->sample_spec
));
275 pa_source_set_fixed_latency_within_thread(u
->source
,
276 (u
->profile
== PROFILE_A2DP_SOURCE
?
277 FIXED_LATENCY_RECORD_A2DP
: FIXED_LATENCY_RECORD_HSP
) +
278 pa_bytes_to_usec(u
->read_block_size
, &u
->sample_spec
));
281 /* from IO thread, except in SCO over PCM */
283 static void setup_stream(struct userdata
*u
) {
284 struct pollfd
*pollfd
;
287 pa_log_info("Transport %s resuming", u
->transport
->path
);
289 bt_transport_config_mtu(u
);
291 pa_make_fd_nonblock(u
->stream_fd
);
292 pa_make_socket_low_delay(u
->stream_fd
);
295 if (setsockopt(u
->stream_fd
, SOL_SOCKET
, SO_TIMESTAMP
, &one
, sizeof(one
)) < 0)
296 pa_log_warn("Failed to enable SO_TIMESTAMP: %s", pa_cstrerror(errno
));
298 pa_log_debug("Stream properly set up, we're ready to roll!");
300 if (u
->profile
== PROFILE_A2DP
)
301 a2dp_set_bitpool(u
, u
->a2dp
.max_bitpool
);
303 u
->rtpoll_item
= pa_rtpoll_item_new(u
->rtpoll
, PA_RTPOLL_NEVER
, 1);
304 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
305 pollfd
->fd
= u
->stream_fd
;
306 pollfd
->events
= pollfd
->revents
= 0;
308 u
->read_index
= u
->write_index
= 0;
312 u
->read_smoother
= pa_smoother_new(
322 static void teardown_stream(struct userdata
*u
) {
323 if (u
->rtpoll_item
) {
324 pa_rtpoll_item_free(u
->rtpoll_item
);
325 u
->rtpoll_item
= NULL
;
328 if (u
->stream_fd
>= 0) {
329 pa_close(u
->stream_fd
);
333 if (u
->read_smoother
) {
334 pa_smoother_free(u
->read_smoother
);
335 u
->read_smoother
= NULL
;
338 if (u
->write_memchunk
.memblock
) {
339 pa_memblock_unref(u
->write_memchunk
.memblock
);
340 pa_memchunk_reset(&u
->write_memchunk
);
343 pa_log_debug("Audio stream torn down");
346 static void bt_transport_release(struct userdata
*u
) {
347 pa_assert(u
->transport
);
349 /* Ignore if already released */
350 if (!u
->transport_acquired
)
353 pa_log_debug("Releasing transport %s", u
->transport
->path
);
355 pa_bluetooth_transport_release(u
->transport
);
357 u
->transport_acquired
= false;
362 static int bt_transport_acquire(struct userdata
*u
, bool optional
) {
363 pa_assert(u
->transport
);
365 if (u
->transport_acquired
)
368 pa_log_debug("Acquiring transport %s", u
->transport
->path
);
370 u
->stream_fd
= pa_bluetooth_transport_acquire(u
->transport
, optional
, &u
->read_link_mtu
, &u
->write_link_mtu
);
371 if (u
->stream_fd
< 0)
374 u
->transport_acquired
= true;
375 pa_log_info("Transport %s acquired: fd %d", u
->transport
->path
, u
->stream_fd
);
380 /* Run from IO thread */
381 static int sink_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
382 struct userdata
*u
= PA_SINK(o
)->userdata
;
386 pa_assert(u
->sink
== PA_SINK(o
));
387 pa_assert(u
->transport
);
391 case PA_SINK_MESSAGE_SET_STATE
:
393 switch ((pa_sink_state_t
) PA_PTR_TO_UINT(data
)) {
395 case PA_SINK_SUSPENDED
:
396 /* Ignore if transition is PA_SINK_INIT->PA_SINK_SUSPENDED */
397 if (!PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
))
400 /* Stop the device if the source is suspended as well */
401 if (!u
->source
|| u
->source
->state
== PA_SOURCE_SUSPENDED
)
402 /* We deliberately ignore whether stopping
403 * actually worked. Since the stream_fd is
404 * closed it doesn't really matter */
405 bt_transport_release(u
);
410 case PA_SINK_RUNNING
:
411 if (u
->sink
->thread_info
.state
!= PA_SINK_SUSPENDED
)
414 /* Resume the device if the source was suspended as well */
415 if (!u
->source
|| !PA_SOURCE_IS_OPENED(u
->source
->thread_info
.state
)) {
416 if (bt_transport_acquire(u
, false) < 0)
423 case PA_SINK_UNLINKED
:
425 case PA_SINK_INVALID_STATE
:
430 case PA_SINK_MESSAGE_GET_LATENCY
: {
432 if (u
->read_smoother
) {
435 ri
= pa_smoother_get(u
->read_smoother
, pa_rtclock_now());
436 wi
= pa_bytes_to_usec(u
->write_index
+ u
->write_block_size
, &u
->sample_spec
);
438 *((pa_usec_t
*) data
) = wi
> ri
? wi
- ri
: 0;
442 ri
= pa_rtclock_now() - u
->started_at
;
443 wi
= pa_bytes_to_usec(u
->write_index
, &u
->sample_spec
);
445 *((pa_usec_t
*) data
) = wi
> ri
? wi
- ri
: 0;
448 *((pa_usec_t
*) data
) += u
->sink
->thread_info
.fixed_latency
;
453 r
= pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
455 return (r
< 0 || !failed
) ? r
: -1;
458 /* Run from IO thread */
459 static int source_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
460 struct userdata
*u
= PA_SOURCE(o
)->userdata
;
464 pa_assert(u
->source
== PA_SOURCE(o
));
465 pa_assert(u
->transport
);
469 case PA_SOURCE_MESSAGE_SET_STATE
:
471 switch ((pa_source_state_t
) PA_PTR_TO_UINT(data
)) {
473 case PA_SOURCE_SUSPENDED
:
474 /* Ignore if transition is PA_SOURCE_INIT->PA_SOURCE_SUSPENDED */
475 if (!PA_SOURCE_IS_OPENED(u
->source
->thread_info
.state
))
478 /* Stop the device if the sink is suspended as well */
479 if (!u
->sink
|| u
->sink
->state
== PA_SINK_SUSPENDED
)
480 bt_transport_release(u
);
482 if (u
->read_smoother
)
483 pa_smoother_pause(u
->read_smoother
, pa_rtclock_now());
487 case PA_SOURCE_RUNNING
:
488 if (u
->source
->thread_info
.state
!= PA_SOURCE_SUSPENDED
)
491 /* Resume the device if the sink was suspended as well */
492 if (!u
->sink
|| !PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
)) {
493 if (bt_transport_acquire(u
, false) < 0)
498 /* We don't resume the smoother here. Instead we
499 * wait until the first packet arrives */
502 case PA_SOURCE_UNLINKED
:
504 case PA_SOURCE_INVALID_STATE
:
509 case PA_SOURCE_MESSAGE_GET_LATENCY
: {
512 if (u
->read_smoother
) {
513 wi
= pa_smoother_get(u
->read_smoother
, pa_rtclock_now());
514 ri
= pa_bytes_to_usec(u
->read_index
, &u
->sample_spec
);
516 *((pa_usec_t
*) data
) = (wi
> ri
? wi
- ri
: 0) + u
->source
->thread_info
.fixed_latency
;
518 *((pa_usec_t
*) data
) = 0;
525 r
= pa_source_process_msg(o
, code
, data
, offset
, chunk
);
527 return (r
< 0 || !failed
) ? r
: -1;
530 /* Called from main thread context */
531 static int device_process_msg(pa_msgobject
*obj
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
532 struct bluetooth_msg
*u
= BLUETOOTH_MSG(obj
);
535 case BLUETOOTH_MESSAGE_IO_THREAD_FAILED
: {
536 if (u
->card
->module
->unload_requested
)
539 pa_log_debug("Switching the profile to off due to IO thread failure.");
541 pa_assert_se(pa_card_set_profile(u
->card
, "off", false) >= 0);
548 /* Run from IO thread */
549 static int hsp_process_render(struct userdata
*u
) {
553 pa_assert(u
->profile
== PROFILE_HSP
|| u
->profile
== PROFILE_HFGW
);
556 /* First, render some data */
557 if (!u
->write_memchunk
.memblock
)
558 pa_sink_render_full(u
->sink
, u
->write_block_size
, &u
->write_memchunk
);
560 pa_assert(u
->write_memchunk
.length
== u
->write_block_size
);
566 /* Now write that data to the socket. The socket is of type
567 * SEQPACKET, and we generated the data of the MTU size, so this
568 * should just work. */
570 p
= (const uint8_t *) pa_memblock_acquire_chunk(&u
->write_memchunk
);
571 l
= pa_write(u
->stream_fd
, p
, u
->write_memchunk
.length
, &u
->stream_write_type
);
572 pa_memblock_release(u
->write_memchunk
.memblock
);
579 /* Retry right away if we got interrupted */
582 else if (errno
== EAGAIN
)
583 /* Hmm, apparently the socket was not writable, give up for now */
586 pa_log_error("Failed to write data to SCO socket: %s", pa_cstrerror(errno
));
591 pa_assert((size_t) l
<= u
->write_memchunk
.length
);
593 if ((size_t) l
!= u
->write_memchunk
.length
) {
594 pa_log_error("Wrote memory block to socket only partially! %llu written, wanted to write %llu.",
595 (unsigned long long) l
,
596 (unsigned long long) u
->write_memchunk
.length
);
601 u
->write_index
+= (uint64_t) u
->write_memchunk
.length
;
602 pa_memblock_unref(u
->write_memchunk
.memblock
);
603 pa_memchunk_reset(&u
->write_memchunk
);
612 /* Run from IO thread */
613 static int hsp_process_push(struct userdata
*u
) {
615 pa_memchunk memchunk
;
618 pa_assert(u
->profile
== PROFILE_HSP
|| u
->profile
== PROFILE_HFGW
);
619 pa_assert(u
->source
);
620 pa_assert(u
->read_smoother
);
622 memchunk
.memblock
= pa_memblock_new(u
->core
->mempool
, u
->read_block_size
);
623 memchunk
.index
= memchunk
.length
= 0;
632 bool found_tstamp
= false;
635 memset(&m
, 0, sizeof(m
));
636 memset(&aux
, 0, sizeof(aux
));
637 memset(&iov
, 0, sizeof(iov
));
642 m
.msg_controllen
= sizeof(aux
);
644 p
= pa_memblock_acquire(memchunk
.memblock
);
646 iov
.iov_len
= pa_memblock_get_length(memchunk
.memblock
);
647 l
= recvmsg(u
->stream_fd
, &m
, 0);
648 pa_memblock_release(memchunk
.memblock
);
652 if (l
< 0 && errno
== EINTR
)
653 /* Retry right away if we got interrupted */
656 else if (l
< 0 && errno
== EAGAIN
)
657 /* Hmm, apparently the socket was not readable, give up for now. */
660 pa_log_error("Failed to read data from SCO socket: %s", l
< 0 ? pa_cstrerror(errno
) : "EOF");
665 pa_assert((size_t) l
<= pa_memblock_get_length(memchunk
.memblock
));
667 /* In some rare occasions, we might receive packets of a very strange
668 * size. This could potentially be possible if the SCO packet was
669 * received partially over-the-air, or more probably due to hardware
670 * issues in our Bluetooth adapter. In these cases, in order to avoid
671 * an assertion failure due to unaligned data, just discard the whole
673 if (!pa_frame_aligned(l
, &u
->sample_spec
)) {
674 pa_log_warn("SCO packet received of unaligned size: %zu", l
);
678 memchunk
.length
= (size_t) l
;
679 u
->read_index
+= (uint64_t) l
;
681 for (cm
= CMSG_FIRSTHDR(&m
); cm
; cm
= CMSG_NXTHDR(&m
, cm
))
682 if (cm
->cmsg_level
== SOL_SOCKET
&& cm
->cmsg_type
== SO_TIMESTAMP
) {
683 struct timeval
*tv
= (struct timeval
*) CMSG_DATA(cm
);
684 pa_rtclock_from_wallclock(tv
);
685 tstamp
= pa_timeval_load(tv
);
691 pa_log_warn("Couldn't find SO_TIMESTAMP data in auxiliary recvmsg() data!");
692 tstamp
= pa_rtclock_now();
695 pa_smoother_put(u
->read_smoother
, tstamp
, pa_bytes_to_usec(u
->read_index
, &u
->sample_spec
));
696 pa_smoother_resume(u
->read_smoother
, tstamp
, true);
698 pa_source_post(u
->source
, &memchunk
);
704 pa_memblock_unref(memchunk
.memblock
);
709 /* Run from IO thread */
710 static void a2dp_prepare_buffer(struct userdata
*u
) {
711 size_t min_buffer_size
= PA_MAX(u
->read_link_mtu
, u
->write_link_mtu
);
715 if (u
->a2dp
.buffer_size
>= min_buffer_size
)
718 u
->a2dp
.buffer_size
= 2 * min_buffer_size
;
719 pa_xfree(u
->a2dp
.buffer
);
720 u
->a2dp
.buffer
= pa_xmalloc(u
->a2dp
.buffer_size
);
723 /* Run from IO thread */
724 static int a2dp_process_render(struct userdata
*u
) {
725 struct a2dp_info
*a2dp
;
726 struct rtp_header
*header
;
727 struct rtp_payload
*payload
;
731 size_t to_write
, to_encode
;
732 unsigned frame_count
;
736 pa_assert(u
->profile
== PROFILE_A2DP
);
739 /* First, render some data */
740 if (!u
->write_memchunk
.memblock
)
741 pa_sink_render_full(u
->sink
, u
->write_block_size
, &u
->write_memchunk
);
743 pa_assert(u
->write_memchunk
.length
== u
->write_block_size
);
745 a2dp_prepare_buffer(u
);
748 header
= a2dp
->buffer
;
749 payload
= (struct rtp_payload
*) ((uint8_t*) a2dp
->buffer
+ sizeof(*header
));
753 /* Try to create a packet of the full MTU */
755 p
= (const uint8_t *) pa_memblock_acquire_chunk(&u
->write_memchunk
);
756 to_encode
= u
->write_memchunk
.length
;
758 d
= (uint8_t*) a2dp
->buffer
+ sizeof(*header
) + sizeof(*payload
);
759 to_write
= a2dp
->buffer_size
- sizeof(*header
) - sizeof(*payload
);
761 while (PA_LIKELY(to_encode
> 0 && to_write
> 0)) {
765 encoded
= sbc_encode(&a2dp
->sbc
,
770 if (PA_UNLIKELY(encoded
<= 0)) {
771 pa_log_error("SBC encoding error (%li)", (long) encoded
);
772 pa_memblock_release(u
->write_memchunk
.memblock
);
776 /* pa_log_debug("SBC: encoded: %lu; written: %lu", (unsigned long) encoded, (unsigned long) written); */
777 /* pa_log_debug("SBC: codesize: %lu; frame_length: %lu", (unsigned long) a2dp->codesize, (unsigned long) a2dp->frame_length); */
779 pa_assert_fp((size_t) encoded
<= to_encode
);
780 pa_assert_fp((size_t) encoded
== a2dp
->codesize
);
782 pa_assert_fp((size_t) written
<= to_write
);
783 pa_assert_fp((size_t) written
== a2dp
->frame_length
);
785 p
= (const uint8_t*) p
+ encoded
;
786 to_encode
-= encoded
;
788 d
= (uint8_t*) d
+ written
;
794 pa_memblock_release(u
->write_memchunk
.memblock
);
796 pa_assert(to_encode
== 0);
799 pa_log_debug("Using SBC encoder implementation: %s", pa_strnull(sbc_get_implementation_info(&a2dp
->sbc
)));
802 /* write it to the fifo */
803 memset(a2dp
->buffer
, 0, sizeof(*header
) + sizeof(*payload
));
806 header
->sequence_number
= htons(a2dp
->seq_num
++);
807 header
->timestamp
= htonl(u
->write_index
/ pa_frame_size(&u
->sample_spec
));
808 header
->ssrc
= htonl(1);
809 payload
->frame_count
= frame_count
;
811 nbytes
= (uint8_t*) d
- (uint8_t*) a2dp
->buffer
;
816 l
= pa_write(u
->stream_fd
, a2dp
->buffer
, nbytes
, &u
->stream_write_type
);
823 /* Retry right away if we got interrupted */
826 else if (errno
== EAGAIN
)
827 /* Hmm, apparently the socket was not writable, give up for now */
830 pa_log_error("Failed to write data to socket: %s", pa_cstrerror(errno
));
835 pa_assert((size_t) l
<= nbytes
);
837 if ((size_t) l
!= nbytes
) {
838 pa_log_warn("Wrote memory block to socket only partially! %llu written, wanted to write %llu.",
839 (unsigned long long) l
,
840 (unsigned long long) nbytes
);
845 u
->write_index
+= (uint64_t) u
->write_memchunk
.length
;
846 pa_memblock_unref(u
->write_memchunk
.memblock
);
847 pa_memchunk_reset(&u
->write_memchunk
);
857 static int a2dp_process_push(struct userdata
*u
) {
859 pa_memchunk memchunk
;
862 pa_assert(u
->profile
== PROFILE_A2DP_SOURCE
);
863 pa_assert(u
->source
);
864 pa_assert(u
->read_smoother
);
866 memchunk
.memblock
= pa_memblock_new(u
->core
->mempool
, u
->read_block_size
);
867 memchunk
.index
= memchunk
.length
= 0;
870 bool found_tstamp
= false;
872 struct a2dp_info
*a2dp
;
873 struct rtp_header
*header
;
874 struct rtp_payload
*payload
;
878 size_t to_write
, to_decode
;
880 a2dp_prepare_buffer(u
);
883 header
= a2dp
->buffer
;
884 payload
= (struct rtp_payload
*) ((uint8_t*) a2dp
->buffer
+ sizeof(*header
));
886 l
= pa_read(u
->stream_fd
, a2dp
->buffer
, a2dp
->buffer_size
, &u
->stream_write_type
);
890 if (l
< 0 && errno
== EINTR
)
891 /* Retry right away if we got interrupted */
894 else if (l
< 0 && errno
== EAGAIN
)
895 /* Hmm, apparently the socket was not readable, give up for now. */
898 pa_log_error("Failed to read data from socket: %s", l
< 0 ? pa_cstrerror(errno
) : "EOF");
903 pa_assert((size_t) l
<= a2dp
->buffer_size
);
905 u
->read_index
+= (uint64_t) l
;
907 /* TODO: get timestamp from rtp */
909 /* pa_log_warn("Couldn't find SO_TIMESTAMP data in auxiliary recvmsg() data!"); */
910 tstamp
= pa_rtclock_now();
913 pa_smoother_put(u
->read_smoother
, tstamp
, pa_bytes_to_usec(u
->read_index
, &u
->sample_spec
));
914 pa_smoother_resume(u
->read_smoother
, tstamp
, true);
916 p
= (uint8_t*) a2dp
->buffer
+ sizeof(*header
) + sizeof(*payload
);
917 to_decode
= l
- sizeof(*header
) - sizeof(*payload
);
919 d
= pa_memblock_acquire(memchunk
.memblock
);
920 to_write
= memchunk
.length
= pa_memblock_get_length(memchunk
.memblock
);
922 while (PA_LIKELY(to_decode
> 0)) {
926 decoded
= sbc_decode(&a2dp
->sbc
,
931 if (PA_UNLIKELY(decoded
<= 0)) {
932 pa_log_error("SBC decoding error (%li)", (long) decoded
);
933 pa_memblock_release(memchunk
.memblock
);
934 pa_memblock_unref(memchunk
.memblock
);
938 /* pa_log_debug("SBC: decoded: %lu; written: %lu", (unsigned long) decoded, (unsigned long) written); */
939 /* pa_log_debug("SBC: frame_length: %lu; codesize: %lu", (unsigned long) a2dp->frame_length, (unsigned long) a2dp->codesize); */
941 /* Reset frame length, it can be changed due to bitpool change */
942 a2dp
->frame_length
= sbc_get_frame_length(&a2dp
->sbc
);
944 pa_assert_fp((size_t) decoded
<= to_decode
);
945 pa_assert_fp((size_t) decoded
== a2dp
->frame_length
);
947 pa_assert_fp((size_t) written
== a2dp
->codesize
);
949 p
= (const uint8_t*) p
+ decoded
;
950 to_decode
-= decoded
;
952 d
= (uint8_t*) d
+ written
;
956 memchunk
.length
-= to_write
;
958 pa_memblock_release(memchunk
.memblock
);
960 pa_source_post(u
->source
, &memchunk
);
966 pa_memblock_unref(memchunk
.memblock
);
971 static void a2dp_reduce_bitpool(struct userdata
*u
)
973 struct a2dp_info
*a2dp
;
980 /* Check if bitpool is already at its limit */
981 if (a2dp
->sbc
.bitpool
<= BITPOOL_DEC_LIMIT
)
984 bitpool
= a2dp
->sbc
.bitpool
- BITPOOL_DEC_STEP
;
986 if (bitpool
< BITPOOL_DEC_LIMIT
)
987 bitpool
= BITPOOL_DEC_LIMIT
;
989 a2dp_set_bitpool(u
, bitpool
);
992 static void thread_func(void *userdata
) {
993 struct userdata
*u
= userdata
;
994 unsigned do_write
= 0;
995 unsigned pending_read_bytes
= 0;
996 bool writable
= false;
999 pa_assert(u
->transport
);
1001 pa_log_debug("IO Thread starting up");
1003 if (u
->core
->realtime_scheduling
)
1004 pa_make_realtime(u
->core
->realtime_priority
);
1006 pa_thread_mq_install(&u
->thread_mq
);
1008 /* Setup the stream only if the transport was already acquired */
1009 if (u
->transport_acquired
)
1013 struct pollfd
*pollfd
;
1015 bool disable_timer
= true;
1017 pollfd
= u
->rtpoll_item
? pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
) : NULL
;
1019 if (u
->source
&& PA_SOURCE_IS_LINKED(u
->source
->thread_info
.state
)) {
1021 /* We should send two blocks to the device before we expect
1024 if (u
->write_index
== 0 && u
->read_index
<= 0)
1027 if (pollfd
&& (pollfd
->revents
& POLLIN
)) {
1030 if (u
->profile
== PROFILE_HSP
|| u
->profile
== PROFILE_HFGW
)
1031 n_read
= hsp_process_push(u
);
1033 n_read
= a2dp_process_push(u
);
1038 /* We just read something, so we are supposed to write something, too */
1039 pending_read_bytes
+= n_read
;
1040 do_write
+= pending_read_bytes
/ u
->write_block_size
;
1041 pending_read_bytes
= pending_read_bytes
% u
->write_block_size
;
1045 if (u
->sink
&& PA_SINK_IS_LINKED(u
->sink
->thread_info
.state
)) {
1047 if (PA_UNLIKELY(u
->sink
->thread_info
.rewind_requested
))
1048 pa_sink_process_rewind(u
->sink
, 0);
1051 if (pollfd
->revents
& POLLOUT
)
1054 if ((!u
->source
|| !PA_SOURCE_IS_LINKED(u
->source
->thread_info
.state
)) && do_write
<= 0 && writable
) {
1055 pa_usec_t time_passed
;
1056 pa_usec_t audio_sent
;
1058 /* Hmm, there is no input stream we could synchronize
1059 * to. So let's do things by time */
1061 time_passed
= pa_rtclock_now() - u
->started_at
;
1062 audio_sent
= pa_bytes_to_usec(u
->write_index
, &u
->sample_spec
);
1064 if (audio_sent
<= time_passed
) {
1065 pa_usec_t audio_to_send
= time_passed
- audio_sent
;
1067 /* Never try to catch up for more than 100ms */
1068 if (u
->write_index
> 0 && audio_to_send
> MAX_PLAYBACK_CATCH_UP_USEC
) {
1069 pa_usec_t skip_usec
;
1070 uint64_t skip_bytes
;
1072 skip_usec
= audio_to_send
- MAX_PLAYBACK_CATCH_UP_USEC
;
1073 skip_bytes
= pa_usec_to_bytes(skip_usec
, &u
->sample_spec
);
1075 if (skip_bytes
> 0) {
1078 pa_log_warn("Skipping %llu us (= %llu bytes) in audio stream",
1079 (unsigned long long) skip_usec
,
1080 (unsigned long long) skip_bytes
);
1082 pa_sink_render_full(u
->sink
, skip_bytes
, &tmp
);
1083 pa_memblock_unref(tmp
.memblock
);
1084 u
->write_index
+= skip_bytes
;
1086 if (u
->profile
== PROFILE_A2DP
)
1087 a2dp_reduce_bitpool(u
);
1092 pending_read_bytes
= 0;
1096 if (writable
&& do_write
> 0) {
1099 if (u
->write_index
<= 0)
1100 u
->started_at
= pa_rtclock_now();
1102 if (u
->profile
== PROFILE_A2DP
) {
1103 if ((n_written
= a2dp_process_render(u
)) < 0)
1106 if ((n_written
= hsp_process_render(u
)) < 0)
1111 pa_log("Broken kernel: we got EAGAIN on write() after POLLOUT!");
1113 do_write
-= n_written
;
1117 if ((!u
->source
|| !PA_SOURCE_IS_LINKED(u
->source
->thread_info
.state
)) && do_write
<= 0) {
1118 pa_usec_t sleep_for
;
1119 pa_usec_t time_passed
, next_write_at
;
1122 /* Hmm, there is no input stream we could synchronize
1123 * to. So let's estimate when we need to wake up the latest */
1124 time_passed
= pa_rtclock_now() - u
->started_at
;
1125 next_write_at
= pa_bytes_to_usec(u
->write_index
, &u
->sample_spec
);
1126 sleep_for
= time_passed
< next_write_at
? next_write_at
- time_passed
: 0;
1127 /* 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); */
1129 /* drop stream every 500 ms */
1130 sleep_for
= PA_USEC_PER_MSEC
* 500;
1132 pa_rtpoll_set_timer_relative(u
->rtpoll
, sleep_for
);
1133 disable_timer
= false;
1139 pa_rtpoll_set_timer_disabled(u
->rtpoll
);
1141 /* Hmm, nothing to do. Let's sleep */
1143 pollfd
->events
= (short) (((u
->sink
&& PA_SINK_IS_LINKED(u
->sink
->thread_info
.state
) && !writable
) ? POLLOUT
: 0) |
1144 (u
->source
&& PA_SOURCE_IS_LINKED(u
->source
->thread_info
.state
) ? POLLIN
: 0));
1146 if ((ret
= pa_rtpoll_run(u
->rtpoll
, true)) < 0) {
1147 pa_log_debug("pa_rtpoll_run failed with: %d", ret
);
1151 pa_log_debug("IO thread shutdown requested, stopping cleanly");
1152 bt_transport_release(u
);
1156 pollfd
= u
->rtpoll_item
? pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
) : NULL
;
1158 if (pollfd
&& (pollfd
->revents
& ~(POLLOUT
|POLLIN
))) {
1159 pa_log_info("FD error: %s%s%s%s",
1160 pollfd
->revents
& POLLERR
? "POLLERR " :"",
1161 pollfd
->revents
& POLLHUP
? "POLLHUP " :"",
1162 pollfd
->revents
& POLLPRI
? "POLLPRI " :"",
1163 pollfd
->revents
& POLLNVAL
? "POLLNVAL " :"");
1170 /* In case of HUP, just tear down the streams */
1171 if (!pollfd
|| (pollfd
->revents
& POLLHUP
) == 0)
1175 pending_read_bytes
= 0;
1182 /* If this was no regular exit from the loop we have to continue processing messages until we receive PA_MESSAGE_SHUTDOWN */
1183 pa_log_debug("IO thread failed");
1184 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(u
->msg
), BLUETOOTH_MESSAGE_IO_THREAD_FAILED
, NULL
, 0, NULL
, NULL
);
1185 pa_asyncmsgq_wait_for(u
->thread_mq
.inq
, PA_MESSAGE_SHUTDOWN
);
1188 pa_log_debug("IO thread shutting down");
1191 static pa_available_t
transport_state_to_availability(pa_bluetooth_transport_state_t state
) {
1192 if (state
== PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED
)
1193 return PA_AVAILABLE_NO
;
1194 else if (state
>= PA_BLUETOOTH_TRANSPORT_STATE_PLAYING
)
1195 return PA_AVAILABLE_YES
;
1197 return PA_AVAILABLE_UNKNOWN
;
1200 static pa_direction_t
get_profile_direction(enum profile p
) {
1201 static const pa_direction_t profile_direction
[] = {
1202 [PROFILE_A2DP
] = PA_DIRECTION_OUTPUT
,
1203 [PROFILE_A2DP_SOURCE
] = PA_DIRECTION_INPUT
,
1204 [PROFILE_HSP
] = PA_DIRECTION_INPUT
| PA_DIRECTION_OUTPUT
,
1205 [PROFILE_HFGW
] = PA_DIRECTION_INPUT
| PA_DIRECTION_OUTPUT
,
1209 return profile_direction
[p
];
1212 /* Run from main thread */
1213 static pa_available_t
get_port_availability(struct userdata
*u
, pa_direction_t direction
) {
1214 pa_available_t result
= PA_AVAILABLE_NO
;
1218 pa_assert(u
->device
);
1220 for (i
= 0; i
< PA_BLUETOOTH_PROFILE_COUNT
; i
++) {
1221 pa_bluetooth_transport
*transport
;
1223 if (!(get_profile_direction(i
) & direction
))
1226 if (!(transport
= u
->device
->transports
[i
]))
1229 switch(transport
->state
) {
1230 case PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED
:
1233 case PA_BLUETOOTH_TRANSPORT_STATE_IDLE
:
1234 if (result
== PA_AVAILABLE_NO
)
1235 result
= PA_AVAILABLE_UNKNOWN
;
1239 case PA_BLUETOOTH_TRANSPORT_STATE_PLAYING
:
1240 return PA_AVAILABLE_YES
;
1247 /* Run from main thread */
1248 static void handle_transport_state_change(struct userdata
*u
, struct pa_bluetooth_transport
*transport
) {
1249 bool acquire
= false;
1250 bool release
= false;
1251 enum profile profile
;
1252 pa_card_profile
*cp
;
1253 pa_bluetooth_transport_state_t state
;
1254 pa_device_port
*port
;
1257 pa_assert(transport
);
1259 profile
= transport
->profile
;
1260 state
= transport
->state
;
1262 /* Update profile availability */
1263 if (!(cp
= pa_hashmap_get(u
->card
->profiles
, pa_bt_profile_to_string(profile
))))
1266 pa_card_profile_set_available(cp
, transport_state_to_availability(state
));
1268 /* Update port availability */
1269 pa_assert_se(port
= pa_hashmap_get(u
->card
->ports
, u
->output_port_name
));
1270 pa_device_port_set_available(port
, get_port_availability(u
, PA_DIRECTION_OUTPUT
));
1272 pa_assert_se(port
= pa_hashmap_get(u
->card
->ports
, u
->input_port_name
));
1273 pa_device_port_set_available(port
, get_port_availability(u
, PA_DIRECTION_INPUT
));
1275 /* Acquire or release transport as needed */
1276 acquire
= (state
== PA_BLUETOOTH_TRANSPORT_STATE_PLAYING
&& u
->profile
== profile
);
1277 release
= (state
!= PA_BLUETOOTH_TRANSPORT_STATE_PLAYING
&& u
->profile
== profile
);
1280 if (bt_transport_acquire(u
, true) >= 0) {
1282 pa_log_debug("Resuming source %s, because the bluetooth audio state changed to 'playing'.", u
->source
->name
);
1283 pa_source_suspend(u
->source
, false, PA_SUSPEND_IDLE
|PA_SUSPEND_USER
);
1287 pa_log_debug("Resuming sink %s, because the bluetooth audio state changed to 'playing'.", u
->sink
->name
);
1288 pa_sink_suspend(u
->sink
, false, PA_SUSPEND_IDLE
|PA_SUSPEND_USER
);
1292 if (release
&& u
->transport_acquired
) {
1293 /* FIXME: this release is racy, since the audio stream might have
1294 been set up again in the meantime (but not processed yet by PA).
1295 BlueZ should probably release the transport automatically, and
1296 in that case we would just mark the transport as released */
1298 /* Remote side closed the stream so we consider it PA_SUSPEND_USER */
1300 pa_log_debug("Suspending source %s, because the remote end closed the stream.", u
->source
->name
);
1301 pa_source_suspend(u
->source
, true, PA_SUSPEND_USER
);
1305 pa_log_debug("Suspending sink %s, because the remote end closed the stream.", u
->sink
->name
);
1306 pa_sink_suspend(u
->sink
, true, PA_SUSPEND_USER
);
1311 /* Run from main thread */
1312 static void sink_set_volume_cb(pa_sink
*s
) {
1321 k
= pa_sprintf_malloc("bluetooth-device@%p", (void*) s
);
1322 u
= pa_shared_get(s
->core
, k
);
1326 pa_assert(u
->sink
== s
);
1327 pa_assert(u
->profile
== PROFILE_HSP
);
1328 pa_assert(u
->transport
);
1330 gain
= (dbus_uint16_t
) round((double) pa_cvolume_max(&s
->real_volume
) * HSP_MAX_GAIN
/ PA_VOLUME_NORM
);
1331 volume
= (pa_volume_t
) round((double) gain
* PA_VOLUME_NORM
/ HSP_MAX_GAIN
);
1333 pa_cvolume_set(&s
->real_volume
, u
->sample_spec
.channels
, volume
);
1335 pa_bluetooth_transport_set_speaker_gain(u
->transport
, gain
);
1338 /* Run from main thread */
1339 static void source_set_volume_cb(pa_source
*s
) {
1348 k
= pa_sprintf_malloc("bluetooth-device@%p", (void*) s
);
1349 u
= pa_shared_get(s
->core
, k
);
1353 pa_assert(u
->source
== s
);
1354 pa_assert(u
->profile
== PROFILE_HSP
);
1355 pa_assert(u
->transport
);
1357 gain
= (dbus_uint16_t
) round((double) pa_cvolume_max(&s
->real_volume
) * HSP_MAX_GAIN
/ PA_VOLUME_NORM
);
1358 volume
= (pa_volume_t
) round((double) gain
* PA_VOLUME_NORM
/ HSP_MAX_GAIN
);
1360 pa_cvolume_set(&s
->real_volume
, u
->sample_spec
.channels
, volume
);
1362 pa_bluetooth_transport_set_microphone_gain(u
->transport
, gain
);
1365 /* Run from main thread */
1366 static char *get_name(const char *type
, pa_modargs
*ma
, const char *device_id
, bool *namereg_fail
) {
1372 pa_assert(device_id
);
1373 pa_assert(namereg_fail
);
1375 t
= pa_sprintf_malloc("%s_name", type
);
1376 n
= pa_modargs_get_value(ma
, t
, NULL
);
1380 *namereg_fail
= true;
1381 return pa_xstrdup(n
);
1384 if ((n
= pa_modargs_get_value(ma
, "name", NULL
)))
1385 *namereg_fail
= true;
1388 *namereg_fail
= false;
1391 return pa_sprintf_malloc("bluez_%s.%s", type
, n
);
1394 static int sco_over_pcm_state_update(struct userdata
*u
, bool changed
) {
1396 pa_assert(USE_SCO_OVER_PCM(u
));
1398 if (PA_SINK_IS_OPENED(pa_sink_get_state(u
->hsp
.sco_sink
)) ||
1399 PA_SOURCE_IS_OPENED(pa_source_get_state(u
->hsp
.sco_source
))) {
1401 if (u
->stream_fd
>= 0)
1404 pa_log_debug("Resuming SCO over PCM");
1405 if (init_profile(u
) < 0) {
1406 pa_log("Can't resume SCO over PCM");
1410 if (bt_transport_acquire(u
, false) < 0)
1419 if (u
->stream_fd
< 0)
1422 pa_log_debug("Closing SCO over PCM");
1424 bt_transport_release(u
);
1430 static pa_hook_result_t
sink_state_changed_cb(pa_core
*c
, pa_sink
*s
, struct userdata
*u
) {
1432 pa_sink_assert_ref(s
);
1435 if (!USE_SCO_OVER_PCM(u
) || s
!= u
->hsp
.sco_sink
)
1438 sco_over_pcm_state_update(u
, true);
1443 static pa_hook_result_t
source_state_changed_cb(pa_core
*c
, pa_source
*s
, struct userdata
*u
) {
1445 pa_source_assert_ref(s
);
1448 if (!USE_SCO_OVER_PCM(u
) || s
!= u
->hsp
.sco_source
)
1451 sco_over_pcm_state_update(u
, true);
1456 static pa_hook_result_t
transport_nrec_changed_cb(pa_bluetooth_discovery
*y
, pa_bluetooth_transport
*t
, struct userdata
*u
) {
1462 if (t
!= u
->transport
)
1465 p
= pa_proplist_new();
1466 pa_proplist_sets(p
, "bluetooth.nrec", t
->nrec
? "1" : "0");
1467 pa_source_update_proplist(u
->source
, PA_UPDATE_REPLACE
, p
);
1468 pa_proplist_free(p
);
1473 static pa_hook_result_t
transport_microphone_gain_changed_cb(pa_bluetooth_discovery
*y
, pa_bluetooth_transport
*t
,
1474 struct userdata
*u
) {
1480 if (t
!= u
->transport
)
1483 pa_assert(u
->source
);
1485 pa_cvolume_set(&v
, u
->sample_spec
.channels
,
1486 (pa_volume_t
) round((double) t
->microphone_gain
* PA_VOLUME_NORM
/ HSP_MAX_GAIN
));
1487 pa_source_volume_changed(u
->source
, &v
);
1492 static pa_hook_result_t
transport_speaker_gain_changed_cb(pa_bluetooth_discovery
*y
, pa_bluetooth_transport
*t
,
1493 struct userdata
*u
) {
1499 if (t
!= u
->transport
)
1504 pa_cvolume_set(&v
, u
->sample_spec
.channels
, (pa_volume_t
) round((double) t
->speaker_gain
* PA_VOLUME_NORM
/ HSP_MAX_GAIN
));
1505 pa_sink_volume_changed(u
->sink
, &v
);
1510 static void connect_ports(struct userdata
*u
, void *sink_or_source_new_data
, pa_direction_t direction
) {
1511 pa_device_port
*port
;
1513 if (direction
== PA_DIRECTION_OUTPUT
) {
1514 pa_sink_new_data
*sink_new_data
= sink_or_source_new_data
;
1516 pa_assert_se(port
= pa_hashmap_get(u
->card
->ports
, u
->output_port_name
));
1517 pa_assert_se(pa_hashmap_put(sink_new_data
->ports
, port
->name
, port
) >= 0);
1518 pa_device_port_ref(port
);
1520 pa_source_new_data
*source_new_data
= sink_or_source_new_data
;
1522 pa_assert_se(port
= pa_hashmap_get(u
->card
->ports
, u
->input_port_name
));
1523 pa_assert_se(pa_hashmap_put(source_new_data
->ports
, port
->name
, port
) >= 0);
1524 pa_device_port_ref(port
);
1528 static int sink_set_port_cb(pa_sink
*s
, pa_device_port
*p
) {
1532 static int source_set_port_cb(pa_source
*s
, pa_device_port
*p
) {
1536 /* Run from main thread */
1537 static int add_sink(struct userdata
*u
) {
1540 pa_assert(u
->transport
);
1542 if (USE_SCO_OVER_PCM(u
)) {
1545 u
->sink
= u
->hsp
.sco_sink
;
1546 p
= pa_proplist_new();
1547 pa_proplist_sets(p
, "bluetooth.protocol", pa_bt_profile_to_string(u
->profile
));
1548 pa_proplist_update(u
->sink
->proplist
, PA_UPDATE_MERGE
, p
);
1549 pa_proplist_free(p
);
1551 pa_sink_new_data data
;
1554 pa_sink_new_data_init(&data
);
1555 data
.driver
= __FILE__
;
1556 data
.module
= u
->module
;
1557 pa_sink_new_data_set_sample_spec(&data
, &u
->sample_spec
);
1558 pa_proplist_sets(data
.proplist
, "bluetooth.protocol", pa_bt_profile_to_string(u
->profile
));
1559 if (u
->profile
== PROFILE_HSP
)
1560 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_INTENDED_ROLES
, "phone");
1561 data
.card
= u
->card
;
1562 data
.name
= get_name("sink", u
->modargs
, u
->address
, &b
);
1563 data
.namereg_fail
= b
;
1565 if (pa_modargs_get_proplist(u
->modargs
, "sink_properties", data
.proplist
, PA_UPDATE_REPLACE
) < 0) {
1566 pa_log("Invalid properties");
1567 pa_sink_new_data_done(&data
);
1570 connect_ports(u
, &data
, PA_DIRECTION_OUTPUT
);
1572 if (!u
->transport_acquired
)
1573 switch (u
->profile
) {
1576 pa_assert_not_reached(); /* Profile switch should have failed */
1579 data
.suspend_cause
= PA_SUSPEND_USER
;
1581 case PROFILE_A2DP_SOURCE
:
1583 pa_assert_not_reached();
1586 u
->sink
= pa_sink_new(u
->core
, &data
, PA_SINK_HARDWARE
|PA_SINK_LATENCY
);
1587 pa_sink_new_data_done(&data
);
1590 pa_log_error("Failed to create sink");
1594 u
->sink
->userdata
= u
;
1595 u
->sink
->parent
.process_msg
= sink_process_msg
;
1596 u
->sink
->set_port
= sink_set_port_cb
;
1599 if (u
->profile
== PROFILE_HSP
) {
1600 pa_sink_set_set_volume_callback(u
->sink
, sink_set_volume_cb
);
1601 u
->sink
->n_volume_steps
= 16;
1603 k
= pa_sprintf_malloc("bluetooth-device@%p", (void*) u
->sink
);
1604 pa_shared_set(u
->core
, k
, u
);
1611 /* Run from main thread */
1612 static int add_source(struct userdata
*u
) {
1615 pa_assert(u
->transport
);
1617 if (USE_SCO_OVER_PCM(u
)) {
1618 u
->source
= u
->hsp
.sco_source
;
1619 pa_proplist_sets(u
->source
->proplist
, "bluetooth.protocol", pa_bt_profile_to_string(u
->profile
));
1621 pa_source_new_data data
;
1624 pa_source_new_data_init(&data
);
1625 data
.driver
= __FILE__
;
1626 data
.module
= u
->module
;
1627 pa_source_new_data_set_sample_spec(&data
, &u
->sample_spec
);
1628 pa_proplist_sets(data
.proplist
, "bluetooth.protocol", pa_bt_profile_to_string(u
->profile
));
1629 if (u
->profile
== PROFILE_HSP
)
1630 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_INTENDED_ROLES
, "phone");
1632 data
.card
= u
->card
;
1633 data
.name
= get_name("source", u
->modargs
, u
->address
, &b
);
1634 data
.namereg_fail
= b
;
1636 if (pa_modargs_get_proplist(u
->modargs
, "source_properties", data
.proplist
, PA_UPDATE_REPLACE
) < 0) {
1637 pa_log("Invalid properties");
1638 pa_source_new_data_done(&data
);
1642 connect_ports(u
, &data
, PA_DIRECTION_INPUT
);
1644 if (!u
->transport_acquired
)
1645 switch (u
->profile
) {
1647 pa_assert_not_reached(); /* Profile switch should have failed */
1649 case PROFILE_A2DP_SOURCE
:
1651 data
.suspend_cause
= PA_SUSPEND_USER
;
1655 pa_assert_not_reached();
1658 u
->source
= pa_source_new(u
->core
, &data
, PA_SOURCE_HARDWARE
|PA_SOURCE_LATENCY
);
1659 pa_source_new_data_done(&data
);
1662 pa_log_error("Failed to create source");
1666 u
->source
->userdata
= u
;
1667 u
->source
->parent
.process_msg
= source_process_msg
;
1668 u
->source
->set_port
= source_set_port_cb
;
1671 if ((u
->profile
== PROFILE_HSP
) || (u
->profile
== PROFILE_HFGW
)) {
1672 pa_bluetooth_transport
*t
= u
->transport
;
1673 pa_proplist_sets(u
->source
->proplist
, "bluetooth.nrec", t
->nrec
? "1" : "0");
1676 if (u
->profile
== PROFILE_HSP
) {
1677 pa_source_set_set_volume_callback(u
->source
, source_set_volume_cb
);
1678 u
->source
->n_volume_steps
= 16;
1680 k
= pa_sprintf_malloc("bluetooth-device@%p", (void*) u
->source
);
1681 pa_shared_set(u
->core
, k
, u
);
1688 static void bt_transport_config_a2dp(struct userdata
*u
) {
1689 const pa_bluetooth_transport
*t
;
1690 struct a2dp_info
*a2dp
= &u
->a2dp
;
1696 config
= (a2dp_sbc_t
*) t
->config
;
1698 u
->sample_spec
.format
= PA_SAMPLE_S16LE
;
1700 if (a2dp
->sbc_initialized
)
1701 sbc_reinit(&a2dp
->sbc
, 0);
1703 sbc_init(&a2dp
->sbc
, 0);
1704 a2dp
->sbc_initialized
= true;
1706 switch (config
->frequency
) {
1707 case SBC_SAMPLING_FREQ_16000
:
1708 a2dp
->sbc
.frequency
= SBC_FREQ_16000
;
1709 u
->sample_spec
.rate
= 16000U;
1711 case SBC_SAMPLING_FREQ_32000
:
1712 a2dp
->sbc
.frequency
= SBC_FREQ_32000
;
1713 u
->sample_spec
.rate
= 32000U;
1715 case SBC_SAMPLING_FREQ_44100
:
1716 a2dp
->sbc
.frequency
= SBC_FREQ_44100
;
1717 u
->sample_spec
.rate
= 44100U;
1719 case SBC_SAMPLING_FREQ_48000
:
1720 a2dp
->sbc
.frequency
= SBC_FREQ_48000
;
1721 u
->sample_spec
.rate
= 48000U;
1724 pa_assert_not_reached();
1727 switch (config
->channel_mode
) {
1728 case SBC_CHANNEL_MODE_MONO
:
1729 a2dp
->sbc
.mode
= SBC_MODE_MONO
;
1730 u
->sample_spec
.channels
= 1;
1732 case SBC_CHANNEL_MODE_DUAL_CHANNEL
:
1733 a2dp
->sbc
.mode
= SBC_MODE_DUAL_CHANNEL
;
1734 u
->sample_spec
.channels
= 2;
1736 case SBC_CHANNEL_MODE_STEREO
:
1737 a2dp
->sbc
.mode
= SBC_MODE_STEREO
;
1738 u
->sample_spec
.channels
= 2;
1740 case SBC_CHANNEL_MODE_JOINT_STEREO
:
1741 a2dp
->sbc
.mode
= SBC_MODE_JOINT_STEREO
;
1742 u
->sample_spec
.channels
= 2;
1745 pa_assert_not_reached();
1748 switch (config
->allocation_method
) {
1749 case SBC_ALLOCATION_SNR
:
1750 a2dp
->sbc
.allocation
= SBC_AM_SNR
;
1752 case SBC_ALLOCATION_LOUDNESS
:
1753 a2dp
->sbc
.allocation
= SBC_AM_LOUDNESS
;
1756 pa_assert_not_reached();
1759 switch (config
->subbands
) {
1760 case SBC_SUBBANDS_4
:
1761 a2dp
->sbc
.subbands
= SBC_SB_4
;
1763 case SBC_SUBBANDS_8
:
1764 a2dp
->sbc
.subbands
= SBC_SB_8
;
1767 pa_assert_not_reached();
1770 switch (config
->block_length
) {
1771 case SBC_BLOCK_LENGTH_4
:
1772 a2dp
->sbc
.blocks
= SBC_BLK_4
;
1774 case SBC_BLOCK_LENGTH_8
:
1775 a2dp
->sbc
.blocks
= SBC_BLK_8
;
1777 case SBC_BLOCK_LENGTH_12
:
1778 a2dp
->sbc
.blocks
= SBC_BLK_12
;
1780 case SBC_BLOCK_LENGTH_16
:
1781 a2dp
->sbc
.blocks
= SBC_BLK_16
;
1784 pa_assert_not_reached();
1787 a2dp
->min_bitpool
= config
->min_bitpool
;
1788 a2dp
->max_bitpool
= config
->max_bitpool
;
1790 /* Set minimum bitpool for source to get the maximum possible block_size */
1791 a2dp
->sbc
.bitpool
= u
->profile
== PROFILE_A2DP
? a2dp
->max_bitpool
: a2dp
->min_bitpool
;
1792 a2dp
->codesize
= sbc_get_codesize(&a2dp
->sbc
);
1793 a2dp
->frame_length
= sbc_get_frame_length(&a2dp
->sbc
);
1795 pa_log_info("SBC parameters:\n\tallocation=%u\n\tsubbands=%u\n\tblocks=%u\n\tbitpool=%u\n",
1796 a2dp
->sbc
.allocation
, a2dp
->sbc
.subbands
, a2dp
->sbc
.blocks
, a2dp
->sbc
.bitpool
);
1799 static void bt_transport_config(struct userdata
*u
) {
1800 if (u
->profile
== PROFILE_HSP
|| u
->profile
== PROFILE_HFGW
) {
1801 u
->sample_spec
.format
= PA_SAMPLE_S16LE
;
1802 u
->sample_spec
.channels
= 1;
1803 u
->sample_spec
.rate
= 8000;
1805 bt_transport_config_a2dp(u
);
1808 /* Run from main thread */
1809 static pa_hook_result_t
transport_state_changed_cb(pa_bluetooth_discovery
*y
, pa_bluetooth_transport
*t
, struct userdata
*u
) {
1813 if (t
== u
->transport
&& t
->state
== PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED
)
1814 pa_assert_se(pa_card_set_profile(u
->card
, "off", false) >= 0);
1816 if (t
->device
== u
->device
)
1817 handle_transport_state_change(u
, t
);
1822 /* Run from main thread */
1823 static int setup_transport(struct userdata
*u
) {
1824 pa_bluetooth_transport
*t
;
1827 pa_assert(!u
->transport
);
1828 pa_assert(u
->profile
!= PROFILE_OFF
);
1830 /* check if profile has a transport */
1831 t
= u
->device
->transports
[u
->profile
];
1832 if (!t
|| t
->state
== PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED
) {
1833 pa_log_warn("Profile has no transport");
1839 if (u
->profile
== PROFILE_A2DP_SOURCE
|| u
->profile
== PROFILE_HFGW
)
1840 bt_transport_acquire(u
, true); /* In case of error, the sink/sources will be created suspended */
1841 else if (bt_transport_acquire(u
, false) < 0)
1842 return -1; /* We need to fail here until the interactions with module-suspend-on-idle and alike get improved */
1844 bt_transport_config(u
);
1849 /* Run from main thread */
1850 static int init_profile(struct userdata
*u
) {
1853 pa_assert(u
->profile
!= PROFILE_OFF
);
1855 if (setup_transport(u
) < 0)
1858 pa_assert(u
->transport
);
1860 if (u
->profile
== PROFILE_A2DP
||
1861 u
->profile
== PROFILE_HSP
||
1862 u
->profile
== PROFILE_HFGW
)
1863 if (add_sink(u
) < 0)
1866 if (u
->profile
== PROFILE_HSP
||
1867 u
->profile
== PROFILE_A2DP_SOURCE
||
1868 u
->profile
== PROFILE_HFGW
)
1869 if (add_source(u
) < 0)
1875 /* Run from main thread */
1876 static void stop_thread(struct userdata
*u
) {
1881 if (u
->sink
&& !USE_SCO_OVER_PCM(u
))
1882 pa_sink_unlink(u
->sink
);
1884 if (u
->source
&& !USE_SCO_OVER_PCM(u
))
1885 pa_source_unlink(u
->source
);
1888 pa_asyncmsgq_send(u
->thread_mq
.inq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, 0, NULL
);
1889 pa_thread_free(u
->thread
);
1893 if (u
->rtpoll_item
) {
1894 pa_rtpoll_item_free(u
->rtpoll_item
);
1895 u
->rtpoll_item
= NULL
;
1899 pa_thread_mq_done(&u
->thread_mq
);
1901 pa_rtpoll_free(u
->rtpoll
);
1906 bt_transport_release(u
);
1907 u
->transport
= NULL
;
1911 if (u
->profile
== PROFILE_HSP
) {
1912 k
= pa_sprintf_malloc("bluetooth-device@%p", (void*) u
->sink
);
1913 pa_shared_remove(u
->core
, k
);
1917 pa_sink_unref(u
->sink
);
1922 if (u
->profile
== PROFILE_HSP
) {
1923 k
= pa_sprintf_malloc("bluetooth-device@%p", (void*) u
->source
);
1924 pa_shared_remove(u
->core
, k
);
1928 pa_source_unref(u
->source
);
1932 if (u
->read_smoother
) {
1933 pa_smoother_free(u
->read_smoother
);
1934 u
->read_smoother
= NULL
;
1938 /* Run from main thread */
1939 static int start_thread(struct userdata
*u
) {
1941 pa_assert(!u
->thread
);
1942 pa_assert(!u
->rtpoll
);
1943 pa_assert(!u
->rtpoll_item
);
1945 u
->rtpoll
= pa_rtpoll_new();
1946 pa_thread_mq_init(&u
->thread_mq
, u
->core
->mainloop
, u
->rtpoll
);
1948 if (USE_SCO_OVER_PCM(u
)) {
1949 if (sco_over_pcm_state_update(u
, false) < 0) {
1953 k
= pa_sprintf_malloc("bluetooth-device@%p", (void*) u
->sink
);
1954 pa_shared_remove(u
->core
, k
);
1959 k
= pa_sprintf_malloc("bluetooth-device@%p", (void*) u
->source
);
1960 pa_shared_remove(u
->core
, k
);
1967 pa_sink_ref(u
->sink
);
1968 pa_source_ref(u
->source
);
1969 /* FIXME: monitor stream_fd error */
1973 if (!(u
->thread
= pa_thread_new("bluetooth", thread_func
, u
))) {
1974 pa_log_error("Failed to create IO thread");
1979 pa_sink_set_asyncmsgq(u
->sink
, u
->thread_mq
.inq
);
1980 pa_sink_set_rtpoll(u
->sink
, u
->rtpoll
);
1981 pa_sink_put(u
->sink
);
1983 if (u
->sink
->set_volume
)
1984 u
->sink
->set_volume(u
->sink
);
1988 pa_source_set_asyncmsgq(u
->source
, u
->thread_mq
.inq
);
1989 pa_source_set_rtpoll(u
->source
, u
->rtpoll
);
1990 pa_source_put(u
->source
);
1992 if (u
->source
->set_volume
)
1993 u
->source
->set_volume(u
->source
);
1999 static void save_sco_volume_callbacks(struct userdata
*u
) {
2001 pa_assert(USE_SCO_OVER_PCM(u
));
2003 u
->hsp
.sco_sink_set_volume
= u
->hsp
.sco_sink
->set_volume
;
2004 u
->hsp
.sco_source_set_volume
= u
->hsp
.sco_source
->set_volume
;
2007 static void restore_sco_volume_callbacks(struct userdata
*u
) {
2009 pa_assert(USE_SCO_OVER_PCM(u
));
2011 pa_sink_set_set_volume_callback(u
->hsp
.sco_sink
, u
->hsp
.sco_sink_set_volume
);
2012 pa_source_set_set_volume_callback(u
->hsp
.sco_source
, u
->hsp
.sco_source_set_volume
);
2015 /* Run from main thread */
2016 static int card_set_profile(pa_card
*c
, pa_card_profile
*new_profile
) {
2021 pa_assert(new_profile
);
2022 pa_assert_se(u
= c
->userdata
);
2024 d
= PA_CARD_PROFILE_DATA(new_profile
);
2026 if (*d
!= PROFILE_OFF
) {
2027 const pa_bluetooth_device
*device
= u
->device
;
2029 if (!device
->transports
[*d
] || device
->transports
[*d
]->state
== PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED
) {
2030 pa_log_warn("Profile not connected, refused to switch profile to %s", new_profile
->name
);
2037 if (USE_SCO_OVER_PCM(u
))
2038 restore_sco_volume_callbacks(u
);
2041 u
->sample_spec
= u
->requested_sample_spec
;
2043 if (USE_SCO_OVER_PCM(u
))
2044 save_sco_volume_callbacks(u
);
2046 if (u
->profile
!= PROFILE_OFF
)
2047 if (init_profile(u
) < 0)
2050 if (u
->sink
|| u
->source
)
2051 if (start_thread(u
) < 0)
2059 pa_assert_se(pa_card_set_profile(u
->card
, "off", false) >= 0);
2064 /* Run from main thread */
2065 static void create_card_ports(struct userdata
*u
, pa_hashmap
*ports
) {
2066 pa_device_port
*port
;
2067 pa_device_port_new_data port_data
;
2069 const char *name_prefix
= NULL
;
2070 const char *input_description
= NULL
;
2071 const char *output_description
= NULL
;
2075 pa_assert(u
->device
);
2077 switch (pa_bluetooth_get_form_factor(u
->device
->class)) {
2078 case PA_BT_FORM_FACTOR_UNKNOWN
:
2081 case PA_BT_FORM_FACTOR_HEADSET
:
2082 name_prefix
= "headset";
2083 input_description
= output_description
= _("Headset");
2086 case PA_BT_FORM_FACTOR_HANDSFREE
:
2087 name_prefix
= "handsfree";
2088 input_description
= output_description
= _("Handsfree");
2091 case PA_BT_FORM_FACTOR_MICROPHONE
:
2092 name_prefix
= "microphone";
2093 input_description
= _("Microphone");
2096 case PA_BT_FORM_FACTOR_SPEAKER
:
2097 name_prefix
= "speaker";
2098 output_description
= _("Speaker");
2101 case PA_BT_FORM_FACTOR_HEADPHONE
:
2102 name_prefix
= "headphone";
2103 output_description
= _("Headphone");
2106 case PA_BT_FORM_FACTOR_PORTABLE
:
2107 name_prefix
= "portable";
2108 input_description
= output_description
= _("Portable");
2111 case PA_BT_FORM_FACTOR_CAR
:
2112 name_prefix
= "car";
2113 input_description
= output_description
= _("Car");
2116 case PA_BT_FORM_FACTOR_HIFI
:
2117 name_prefix
= "hifi";
2118 input_description
= output_description
= _("HiFi");
2121 case PA_BT_FORM_FACTOR_PHONE
:
2122 name_prefix
= "phone";
2123 input_description
= output_description
= _("Phone");
2128 name_prefix
= "unknown";
2130 if (!output_description
)
2131 output_description
= _("Bluetooth Output");
2133 if (!input_description
)
2134 input_description
= _("Bluetooth Input");
2136 u
->output_port_name
= pa_sprintf_malloc("%s-output", name_prefix
);
2137 u
->input_port_name
= pa_sprintf_malloc("%s-input", name_prefix
);
2139 pa_device_port_new_data_init(&port_data
);
2140 pa_device_port_new_data_set_name(&port_data
, u
->output_port_name
);
2141 pa_device_port_new_data_set_description(&port_data
, output_description
);
2142 pa_device_port_new_data_set_direction(&port_data
, PA_DIRECTION_OUTPUT
);
2143 pa_device_port_new_data_set_available(&port_data
, get_port_availability(u
, PA_DIRECTION_OUTPUT
));
2144 pa_assert_se(port
= pa_device_port_new(u
->core
, &port_data
, 0));
2145 pa_assert_se(pa_hashmap_put(ports
, port
->name
, port
) >= 0);
2146 pa_device_port_new_data_done(&port_data
);
2148 pa_device_port_new_data_init(&port_data
);
2149 pa_device_port_new_data_set_name(&port_data
, u
->input_port_name
);
2150 pa_device_port_new_data_set_description(&port_data
, input_description
);
2151 pa_device_port_new_data_set_direction(&port_data
, PA_DIRECTION_INPUT
);
2152 pa_device_port_new_data_set_available(&port_data
, get_port_availability(u
, PA_DIRECTION_INPUT
));
2153 pa_assert_se(port
= pa_device_port_new(u
->core
, &port_data
, 0));
2154 pa_assert_se(pa_hashmap_put(ports
, port
->name
, port
) >= 0);
2155 pa_device_port_new_data_done(&port_data
);
2158 /* Run from main thread */
2159 static pa_card_profile
*create_card_profile(struct userdata
*u
, const char *uuid
, pa_hashmap
*ports
) {
2160 pa_device_port
*input_port
, *output_port
;
2161 pa_card_profile
*p
= NULL
;
2164 pa_assert(u
->input_port_name
);
2165 pa_assert(u
->output_port_name
);
2166 pa_assert_se(input_port
= pa_hashmap_get(ports
, u
->input_port_name
));
2167 pa_assert_se(output_port
= pa_hashmap_get(ports
, u
->output_port_name
));
2169 if (pa_streq(uuid
, A2DP_SINK_UUID
)) {
2170 p
= pa_card_profile_new("a2dp", _("High Fidelity Playback (A2DP)"), sizeof(enum profile
));
2174 p
->max_sink_channels
= 2;
2175 p
->max_source_channels
= 0;
2176 pa_hashmap_put(output_port
->profiles
, p
->name
, p
);
2178 d
= PA_CARD_PROFILE_DATA(p
);
2180 } else if (pa_streq(uuid
, A2DP_SOURCE_UUID
)) {
2181 p
= pa_card_profile_new("a2dp_source", _("High Fidelity Capture (A2DP)"), sizeof(enum profile
));
2185 p
->max_sink_channels
= 0;
2186 p
->max_source_channels
= 2;
2187 pa_hashmap_put(input_port
->profiles
, p
->name
, p
);
2189 d
= PA_CARD_PROFILE_DATA(p
);
2190 *d
= PROFILE_A2DP_SOURCE
;
2191 } else if (pa_streq(uuid
, HSP_HS_UUID
) || pa_streq(uuid
, HFP_HS_UUID
)) {
2192 p
= pa_card_profile_new("hsp", _("Telephony Duplex (HSP/HFP)"), sizeof(enum profile
));
2196 p
->max_sink_channels
= 1;
2197 p
->max_source_channels
= 1;
2198 pa_hashmap_put(input_port
->profiles
, p
->name
, p
);
2199 pa_hashmap_put(output_port
->profiles
, p
->name
, p
);
2201 d
= PA_CARD_PROFILE_DATA(p
);
2203 } else if (pa_streq(uuid
, HFP_AG_UUID
)) {
2204 p
= pa_card_profile_new("hfgw", _("Handsfree Gateway"), sizeof(enum profile
));
2208 p
->max_sink_channels
= 1;
2209 p
->max_source_channels
= 1;
2210 pa_hashmap_put(input_port
->profiles
, p
->name
, p
);
2211 pa_hashmap_put(output_port
->profiles
, p
->name
, p
);
2213 d
= PA_CARD_PROFILE_DATA(p
);
2218 pa_bluetooth_transport
*t
;
2220 if ((t
= u
->device
->transports
[*d
]))
2221 p
->available
= transport_state_to_availability(t
->state
);
2227 /* Run from main thread */
2228 static int add_card(struct userdata
*u
) {
2229 pa_card_new_data data
;
2233 pa_bt_form_factor_t ff
;
2235 const char *default_profile
;
2236 const pa_bluetooth_device
*device
;
2237 const pa_bluetooth_uuid
*uuid
;
2240 pa_assert(u
->device
);
2244 pa_card_new_data_init(&data
);
2245 data
.driver
= __FILE__
;
2246 data
.module
= u
->module
;
2248 n
= pa_bluetooth_cleanup_name(device
->alias
);
2249 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, n
);
2251 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_STRING
, device
->address
);
2252 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_API
, "bluez");
2253 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_CLASS
, "sound");
2254 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_BUS
, "bluetooth");
2256 if ((ff
= pa_bluetooth_get_form_factor(device
->class)) != PA_BT_FORM_FACTOR_UNKNOWN
)
2257 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_FORM_FACTOR
, pa_bt_form_factor_to_string(ff
));
2259 pa_proplist_sets(data
.proplist
, "bluez.path", device
->path
);
2260 pa_proplist_setf(data
.proplist
, "bluez.class", "0x%06x", (unsigned) device
->class);
2261 pa_proplist_sets(data
.proplist
, "bluez.alias", device
->alias
);
2262 data
.name
= get_name("card", u
->modargs
, device
->address
, &b
);
2263 data
.namereg_fail
= b
;
2265 if (pa_modargs_get_proplist(u
->modargs
, "card_properties", data
.proplist
, PA_UPDATE_REPLACE
) < 0) {
2266 pa_log("Invalid properties");
2267 pa_card_new_data_done(&data
);
2271 create_card_ports(u
, data
.ports
);
2273 PA_LLIST_FOREACH(uuid
, device
->uuids
) {
2274 p
= create_card_profile(u
, uuid
->uuid
, data
.ports
);
2279 if (pa_hashmap_get(data
.profiles
, p
->name
)) {
2280 pa_card_profile_free(p
);
2284 pa_hashmap_put(data
.profiles
, p
->name
, p
);
2287 pa_assert(!pa_hashmap_isempty(data
.profiles
));
2289 p
= pa_card_profile_new("off", _("Off"), sizeof(enum profile
));
2290 p
->available
= PA_AVAILABLE_YES
;
2291 d
= PA_CARD_PROFILE_DATA(p
);
2293 pa_hashmap_put(data
.profiles
, p
->name
, p
);
2295 if ((default_profile
= pa_modargs_get_value(u
->modargs
, "profile", NULL
))) {
2296 if (pa_hashmap_get(data
.profiles
, default_profile
))
2297 pa_card_new_data_set_profile(&data
, default_profile
);
2299 pa_log_warn("Profile '%s' not valid or not supported by device.", default_profile
);
2302 u
->card
= pa_card_new(u
->core
, &data
);
2303 pa_card_new_data_done(&data
);
2306 pa_log("Failed to allocate card.");
2310 u
->card
->userdata
= u
;
2311 u
->card
->set_profile
= card_set_profile
;
2313 d
= PA_CARD_PROFILE_DATA(u
->card
->active_profile
);
2315 if (*d
!= PROFILE_OFF
&& (!device
->transports
[*d
] ||
2316 device
->transports
[*d
]->state
== PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED
)) {
2317 pa_log_warn("Default profile not connected, selecting off profile");
2318 u
->card
->active_profile
= pa_hashmap_get(u
->card
->profiles
, "off");
2319 u
->card
->save_profile
= false;
2322 d
= PA_CARD_PROFILE_DATA(u
->card
->active_profile
);
2325 if (USE_SCO_OVER_PCM(u
))
2326 save_sco_volume_callbacks(u
);
2331 /* Run from main thread */
2332 static pa_bluetooth_device
* find_device(struct userdata
*u
, const char *address
, const char *path
) {
2333 pa_bluetooth_device
*d
= NULL
;
2337 if (!address
&& !path
) {
2338 pa_log_error("Failed to get device address/path from module arguments.");
2343 if (!(d
= pa_bluetooth_discovery_get_by_path(u
->discovery
, path
))) {
2344 pa_log_error("%s is not a valid BlueZ audio device.", path
);
2348 if (address
&& !(pa_streq(d
->address
, address
))) {
2349 pa_log_error("Passed path %s address %s != %s don't match.", path
, d
->address
, address
);
2354 if (!(d
= pa_bluetooth_discovery_get_by_address(u
->discovery
, address
))) {
2355 pa_log_error("%s is not known.", address
);
2361 u
->address
= pa_xstrdup(d
->address
);
2362 u
->path
= pa_xstrdup(d
->path
);
2368 /* Run from main thread */
2369 static pa_hook_result_t
uuid_added_cb(pa_bluetooth_discovery
*y
, const struct pa_bluetooth_hook_uuid_data
*data
,
2370 struct userdata
*u
) {
2374 pa_assert(data
->device
);
2375 pa_assert(data
->uuid
);
2378 if (data
->device
!= u
->device
)
2381 p
= create_card_profile(u
, data
->uuid
, u
->card
->ports
);
2386 if (pa_hashmap_get(u
->card
->profiles
, p
->name
)) {
2387 pa_card_profile_free(p
);
2391 pa_card_add_profile(u
->card
, p
);
2396 /* Run from main thread */
2397 static pa_hook_result_t
discovery_hook_cb(pa_bluetooth_discovery
*y
, const pa_bluetooth_device
*d
, struct userdata
*u
) {
2405 pa_log_debug("Device %s removed: unloading module", d
->path
);
2406 else if (!pa_bluetooth_device_any_audio_connected(d
))
2407 pa_log_debug("Unloading module, because device %s doesn't have any audio profiles connected anymore.", d
->path
);
2411 pa_module_unload(u
->core
, u
->module
, true);
2416 int pa__init(pa_module
* m
) {
2420 const char *address
, *path
;
2421 pa_bluetooth_device
*device
;
2425 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
2426 pa_log_error("Failed to parse module arguments");
2430 m
->userdata
= u
= pa_xnew0(struct userdata
, 1);
2434 u
->sample_spec
= m
->core
->default_sample_spec
;
2437 if (pa_modargs_get_value(ma
, "sco_sink", NULL
) &&
2438 !(u
->hsp
.sco_sink
= pa_namereg_get(m
->core
, pa_modargs_get_value(ma
, "sco_sink", NULL
), PA_NAMEREG_SINK
))) {
2439 pa_log("SCO sink not found");
2443 if (pa_modargs_get_value(ma
, "sco_source", NULL
) &&
2444 !(u
->hsp
.sco_source
= pa_namereg_get(m
->core
, pa_modargs_get_value(ma
, "sco_source", NULL
), PA_NAMEREG_SOURCE
))) {
2445 pa_log("SCO source not found");
2449 if (pa_modargs_get_value_u32(ma
, "rate", &u
->sample_spec
.rate
) < 0 ||
2450 u
->sample_spec
.rate
<= 0 || u
->sample_spec
.rate
> PA_RATE_MAX
) {
2451 pa_log_error("Failed to get rate from module arguments");
2455 u
->auto_connect
= true;
2456 if (pa_modargs_get_value_boolean(ma
, "auto_connect", &u
->auto_connect
)) {
2457 pa_log("Failed to parse auto_connect= argument");
2461 channels
= u
->sample_spec
.channels
;
2462 if (pa_modargs_get_value_u32(ma
, "channels", &channels
) < 0 ||
2463 channels
<= 0 || channels
> PA_CHANNELS_MAX
) {
2464 pa_log_error("Failed to get channels from module arguments");
2467 u
->sample_spec
.channels
= (uint8_t) channels
;
2468 u
->requested_sample_spec
= u
->sample_spec
;
2470 address
= pa_modargs_get_value(ma
, "address", NULL
);
2471 path
= pa_modargs_get_value(ma
, "path", NULL
);
2473 if (!(u
->discovery
= pa_bluetooth_discovery_get(m
->core
)))
2476 if (!(device
= find_device(u
, address
, path
)))
2482 pa_hook_connect(pa_bluetooth_discovery_hook(u
->discovery
, PA_BLUETOOTH_HOOK_DEVICE_CONNECTION_CHANGED
),
2483 PA_HOOK_NORMAL
, (pa_hook_cb_t
) discovery_hook_cb
, u
);
2485 u
->uuid_added_slot
=
2486 pa_hook_connect(pa_bluetooth_discovery_hook(u
->discovery
, PA_BLUETOOTH_HOOK_DEVICE_UUID_ADDED
),
2487 PA_HOOK_NORMAL
, (pa_hook_cb_t
) uuid_added_cb
, u
);
2489 u
->sink_state_changed_slot
=
2490 pa_hook_connect(&u
->core
->hooks
[PA_CORE_HOOK_SINK_STATE_CHANGED
],
2491 PA_HOOK_NORMAL
, (pa_hook_cb_t
) sink_state_changed_cb
, u
);
2493 u
->source_state_changed_slot
=
2494 pa_hook_connect(&u
->core
->hooks
[PA_CORE_HOOK_SOURCE_STATE_CHANGED
],
2495 PA_HOOK_NORMAL
, (pa_hook_cb_t
) source_state_changed_cb
, u
);
2497 u
->transport_state_changed_slot
=
2498 pa_hook_connect(pa_bluetooth_discovery_hook(u
->discovery
, PA_BLUETOOTH_HOOK_TRANSPORT_STATE_CHANGED
),
2499 PA_HOOK_NORMAL
, (pa_hook_cb_t
) transport_state_changed_cb
, u
);
2501 u
->transport_nrec_changed_slot
=
2502 pa_hook_connect(pa_bluetooth_discovery_hook(u
->discovery
, PA_BLUETOOTH_HOOK_TRANSPORT_NREC_CHANGED
),
2503 PA_HOOK_NORMAL
, (pa_hook_cb_t
) transport_nrec_changed_cb
, u
);
2505 u
->transport_microphone_changed_slot
=
2506 pa_hook_connect(pa_bluetooth_discovery_hook(u
->discovery
, PA_BLUETOOTH_HOOK_TRANSPORT_MICROPHONE_GAIN_CHANGED
),
2507 PA_HOOK_NORMAL
, (pa_hook_cb_t
) transport_microphone_gain_changed_cb
, u
);
2509 u
->transport_speaker_changed_slot
=
2510 pa_hook_connect(pa_bluetooth_discovery_hook(u
->discovery
, PA_BLUETOOTH_HOOK_TRANSPORT_SPEAKER_GAIN_CHANGED
),
2511 PA_HOOK_NORMAL
, (pa_hook_cb_t
) transport_speaker_gain_changed_cb
, u
);
2513 /* Add the card structure. This will also initialize the default profile */
2514 if (add_card(u
) < 0)
2517 if (!(u
->msg
= pa_msgobject_new(bluetooth_msg
)))
2520 u
->msg
->parent
.process_msg
= device_process_msg
;
2521 u
->msg
->card
= u
->card
;
2523 if (u
->profile
!= PROFILE_OFF
)
2524 if (init_profile(u
) < 0)
2527 if (u
->sink
|| u
->source
)
2528 if (start_thread(u
) < 0)
2536 pa_assert_se(pa_card_set_profile(u
->card
, "off", false) >= 0);
2547 int pa__get_n_used(pa_module
*m
) {
2551 pa_assert_se(u
= m
->userdata
);
2554 (u
->sink
? pa_sink_linked_by(u
->sink
) : 0) +
2555 (u
->source
? pa_source_linked_by(u
->source
) : 0);
2558 void pa__done(pa_module
*m
) {
2563 if (!(u
= m
->userdata
))
2568 if (u
->discovery_slot
)
2569 pa_hook_slot_free(u
->discovery_slot
);
2571 if (u
->uuid_added_slot
)
2572 pa_hook_slot_free(u
->uuid_added_slot
);
2574 if (u
->sink_state_changed_slot
)
2575 pa_hook_slot_free(u
->sink_state_changed_slot
);
2577 if (u
->source_state_changed_slot
)
2578 pa_hook_slot_free(u
->source_state_changed_slot
);
2580 if (u
->transport_state_changed_slot
)
2581 pa_hook_slot_free(u
->transport_state_changed_slot
);
2583 if (u
->transport_nrec_changed_slot
)
2584 pa_hook_slot_free(u
->transport_nrec_changed_slot
);
2586 if (u
->transport_microphone_changed_slot
)
2587 pa_hook_slot_free(u
->transport_microphone_changed_slot
);
2589 if (u
->transport_speaker_changed_slot
)
2590 pa_hook_slot_free(u
->transport_speaker_changed_slot
);
2592 if (USE_SCO_OVER_PCM(u
))
2593 restore_sco_volume_callbacks(u
);
2599 pa_card_free(u
->card
);
2602 pa_xfree(u
->a2dp
.buffer
);
2604 sbc_finish(&u
->a2dp
.sbc
);
2607 pa_modargs_free(u
->modargs
);
2609 pa_xfree(u
->output_port_name
);
2610 pa_xfree(u
->input_port_name
);
2612 pa_xfree(u
->address
);
2616 pa_bluetooth_discovery_unref(u
->discovery
);