2 This file is part of PulseAudio.
4 Copyright 2008-2013 João Paulo Rechi Vita
5 Copyright 2011-2013 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-bluez4-device-symdef.h"
56 #include "a2dp-codecs.h"
58 #include "bluez4-util.h"
60 #define BITPOOL_DEC_LIMIT 32
61 #define BITPOOL_DEC_STEP 5
63 PA_MODULE_AUTHOR("João Paulo Rechi Vita");
64 PA_MODULE_DESCRIPTION("BlueZ 4 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_bluez4_device
*device
;
139 pa_hook_slot
*uuid_added_slot
;
142 pa_bluez4_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_bluez4_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 pa_bluez4_profile_t 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 == PA_BLUEZ4_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
== PA_BLUEZ4_PROFILE_HSP
|| u
->profile
== PA_BLUEZ4_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
== PA_BLUEZ4_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
== PA_BLUEZ4_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
== PA_BLUEZ4_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_bluez4_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_bluez4_transport_acquire(u
->transport
, optional
, &u
->read_link_mtu
, &u
->write_link_mtu
);
370 if (u
->stream_fd
< 0) {
372 pa_log("Failed to acquire transport %s", u
->transport
->path
);
374 pa_log_info("Failed optional acquire of transport %s", u
->transport
->path
);
379 u
->transport_acquired
= true;
380 pa_log_info("Transport %s acquired: fd %d", u
->transport
->path
, u
->stream_fd
);
385 /* Run from IO thread */
386 static int sink_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
387 struct userdata
*u
= PA_SINK(o
)->userdata
;
391 pa_assert(u
->sink
== PA_SINK(o
));
392 pa_assert(u
->transport
);
396 case PA_SINK_MESSAGE_SET_STATE
:
398 switch ((pa_sink_state_t
) PA_PTR_TO_UINT(data
)) {
400 case PA_SINK_SUSPENDED
:
401 /* Ignore if transition is PA_SINK_INIT->PA_SINK_SUSPENDED */
402 if (!PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
))
405 /* Stop the device if the source is suspended as well */
406 if (!u
->source
|| u
->source
->state
== PA_SOURCE_SUSPENDED
)
407 /* We deliberately ignore whether stopping
408 * actually worked. Since the stream_fd is
409 * closed it doesn't really matter */
410 bt_transport_release(u
);
415 case PA_SINK_RUNNING
:
416 if (u
->sink
->thread_info
.state
!= PA_SINK_SUSPENDED
)
419 /* Resume the device if the source was suspended as well */
420 if (!u
->source
|| !PA_SOURCE_IS_OPENED(u
->source
->thread_info
.state
)) {
421 if (bt_transport_acquire(u
, false) < 0)
428 case PA_SINK_UNLINKED
:
430 case PA_SINK_INVALID_STATE
:
435 case PA_SINK_MESSAGE_GET_LATENCY
: {
437 if (u
->read_smoother
) {
440 ri
= pa_smoother_get(u
->read_smoother
, pa_rtclock_now());
441 wi
= pa_bytes_to_usec(u
->write_index
+ u
->write_block_size
, &u
->sample_spec
);
443 *((pa_usec_t
*) data
) = wi
> ri
? wi
- ri
: 0;
447 ri
= pa_rtclock_now() - u
->started_at
;
448 wi
= pa_bytes_to_usec(u
->write_index
, &u
->sample_spec
);
450 *((pa_usec_t
*) data
) = wi
> ri
? wi
- ri
: 0;
453 *((pa_usec_t
*) data
) += u
->sink
->thread_info
.fixed_latency
;
458 r
= pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
460 return (r
< 0 || !failed
) ? r
: -1;
463 /* Run from IO thread */
464 static int source_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
465 struct userdata
*u
= PA_SOURCE(o
)->userdata
;
469 pa_assert(u
->source
== PA_SOURCE(o
));
470 pa_assert(u
->transport
);
474 case PA_SOURCE_MESSAGE_SET_STATE
:
476 switch ((pa_source_state_t
) PA_PTR_TO_UINT(data
)) {
478 case PA_SOURCE_SUSPENDED
:
479 /* Ignore if transition is PA_SOURCE_INIT->PA_SOURCE_SUSPENDED */
480 if (!PA_SOURCE_IS_OPENED(u
->source
->thread_info
.state
))
483 /* Stop the device if the sink is suspended as well */
484 if (!u
->sink
|| u
->sink
->state
== PA_SINK_SUSPENDED
)
485 bt_transport_release(u
);
487 if (u
->read_smoother
)
488 pa_smoother_pause(u
->read_smoother
, pa_rtclock_now());
492 case PA_SOURCE_RUNNING
:
493 if (u
->source
->thread_info
.state
!= PA_SOURCE_SUSPENDED
)
496 /* Resume the device if the sink was suspended as well */
497 if (!u
->sink
|| !PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
)) {
498 if (bt_transport_acquire(u
, false) < 0)
503 /* We don't resume the smoother here. Instead we
504 * wait until the first packet arrives */
507 case PA_SOURCE_UNLINKED
:
509 case PA_SOURCE_INVALID_STATE
:
514 case PA_SOURCE_MESSAGE_GET_LATENCY
: {
517 if (u
->read_smoother
) {
518 wi
= pa_smoother_get(u
->read_smoother
, pa_rtclock_now());
519 ri
= pa_bytes_to_usec(u
->read_index
, &u
->sample_spec
);
521 *((pa_usec_t
*) data
) = (wi
> ri
? wi
- ri
: 0) + u
->source
->thread_info
.fixed_latency
;
523 *((pa_usec_t
*) data
) = 0;
530 r
= pa_source_process_msg(o
, code
, data
, offset
, chunk
);
532 return (r
< 0 || !failed
) ? r
: -1;
535 /* Called from main thread context */
536 static int device_process_msg(pa_msgobject
*obj
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
537 struct bluetooth_msg
*u
= BLUETOOTH_MSG(obj
);
540 case BLUETOOTH_MESSAGE_IO_THREAD_FAILED
: {
541 if (u
->card
->module
->unload_requested
)
544 pa_log_debug("Switching the profile to off due to IO thread failure.");
546 pa_assert_se(pa_card_set_profile(u
->card
, pa_hashmap_get(u
->card
->profiles
, "off"), false) >= 0);
553 /* Run from IO thread */
554 static int hsp_process_render(struct userdata
*u
) {
558 pa_assert(u
->profile
== PA_BLUEZ4_PROFILE_HSP
|| u
->profile
== PA_BLUEZ4_PROFILE_HFGW
);
561 /* First, render some data */
562 if (!u
->write_memchunk
.memblock
)
563 pa_sink_render_full(u
->sink
, u
->write_block_size
, &u
->write_memchunk
);
565 pa_assert(u
->write_memchunk
.length
== u
->write_block_size
);
571 /* Now write that data to the socket. The socket is of type
572 * SEQPACKET, and we generated the data of the MTU size, so this
573 * should just work. */
575 p
= (const uint8_t *) pa_memblock_acquire_chunk(&u
->write_memchunk
);
576 l
= pa_write(u
->stream_fd
, p
, u
->write_memchunk
.length
, &u
->stream_write_type
);
577 pa_memblock_release(u
->write_memchunk
.memblock
);
584 /* Retry right away if we got interrupted */
587 else if (errno
== EAGAIN
)
588 /* Hmm, apparently the socket was not writable, give up for now */
591 pa_log_error("Failed to write data to SCO socket: %s", pa_cstrerror(errno
));
596 pa_assert((size_t) l
<= u
->write_memchunk
.length
);
598 if ((size_t) l
!= u
->write_memchunk
.length
) {
599 pa_log_error("Wrote memory block to socket only partially! %llu written, wanted to write %llu.",
600 (unsigned long long) l
,
601 (unsigned long long) u
->write_memchunk
.length
);
606 u
->write_index
+= (uint64_t) u
->write_memchunk
.length
;
607 pa_memblock_unref(u
->write_memchunk
.memblock
);
608 pa_memchunk_reset(&u
->write_memchunk
);
617 /* Run from IO thread */
618 static int hsp_process_push(struct userdata
*u
) {
620 pa_memchunk memchunk
;
623 pa_assert(u
->profile
== PA_BLUEZ4_PROFILE_HSP
|| u
->profile
== PA_BLUEZ4_PROFILE_HFGW
);
624 pa_assert(u
->source
);
625 pa_assert(u
->read_smoother
);
627 memchunk
.memblock
= pa_memblock_new(u
->core
->mempool
, u
->read_block_size
);
628 memchunk
.index
= memchunk
.length
= 0;
637 bool found_tstamp
= false;
640 memset(&m
, 0, sizeof(m
));
641 memset(&aux
, 0, sizeof(aux
));
642 memset(&iov
, 0, sizeof(iov
));
647 m
.msg_controllen
= sizeof(aux
);
649 p
= pa_memblock_acquire(memchunk
.memblock
);
651 iov
.iov_len
= pa_memblock_get_length(memchunk
.memblock
);
652 l
= recvmsg(u
->stream_fd
, &m
, 0);
653 pa_memblock_release(memchunk
.memblock
);
657 if (l
< 0 && errno
== EINTR
)
658 /* Retry right away if we got interrupted */
661 else if (l
< 0 && errno
== EAGAIN
)
662 /* Hmm, apparently the socket was not readable, give up for now. */
665 pa_log_error("Failed to read data from SCO socket: %s", l
< 0 ? pa_cstrerror(errno
) : "EOF");
670 pa_assert((size_t) l
<= pa_memblock_get_length(memchunk
.memblock
));
672 /* In some rare occasions, we might receive packets of a very strange
673 * size. This could potentially be possible if the SCO packet was
674 * received partially over-the-air, or more probably due to hardware
675 * issues in our Bluetooth adapter. In these cases, in order to avoid
676 * an assertion failure due to unaligned data, just discard the whole
678 if (!pa_frame_aligned(l
, &u
->sample_spec
)) {
679 pa_log_warn("SCO packet received of unaligned size: %zu", l
);
683 memchunk
.length
= (size_t) l
;
684 u
->read_index
+= (uint64_t) l
;
686 for (cm
= CMSG_FIRSTHDR(&m
); cm
; cm
= CMSG_NXTHDR(&m
, cm
))
687 if (cm
->cmsg_level
== SOL_SOCKET
&& cm
->cmsg_type
== SO_TIMESTAMP
) {
688 struct timeval
*tv
= (struct timeval
*) CMSG_DATA(cm
);
689 pa_rtclock_from_wallclock(tv
);
690 tstamp
= pa_timeval_load(tv
);
696 pa_log_warn("Couldn't find SO_TIMESTAMP data in auxiliary recvmsg() data!");
697 tstamp
= pa_rtclock_now();
700 pa_smoother_put(u
->read_smoother
, tstamp
, pa_bytes_to_usec(u
->read_index
, &u
->sample_spec
));
701 pa_smoother_resume(u
->read_smoother
, tstamp
, true);
703 pa_source_post(u
->source
, &memchunk
);
709 pa_memblock_unref(memchunk
.memblock
);
714 /* Run from IO thread */
715 static void a2dp_prepare_buffer(struct userdata
*u
) {
716 size_t min_buffer_size
= PA_MAX(u
->read_link_mtu
, u
->write_link_mtu
);
720 if (u
->a2dp
.buffer_size
>= min_buffer_size
)
723 u
->a2dp
.buffer_size
= 2 * min_buffer_size
;
724 pa_xfree(u
->a2dp
.buffer
);
725 u
->a2dp
.buffer
= pa_xmalloc(u
->a2dp
.buffer_size
);
728 /* Run from IO thread */
729 static int a2dp_process_render(struct userdata
*u
) {
730 struct a2dp_info
*a2dp
;
731 struct rtp_header
*header
;
732 struct rtp_payload
*payload
;
736 size_t to_write
, to_encode
;
737 unsigned frame_count
;
741 pa_assert(u
->profile
== PA_BLUEZ4_PROFILE_A2DP
);
744 /* First, render some data */
745 if (!u
->write_memchunk
.memblock
)
746 pa_sink_render_full(u
->sink
, u
->write_block_size
, &u
->write_memchunk
);
748 pa_assert(u
->write_memchunk
.length
== u
->write_block_size
);
750 a2dp_prepare_buffer(u
);
753 header
= a2dp
->buffer
;
754 payload
= (struct rtp_payload
*) ((uint8_t*) a2dp
->buffer
+ sizeof(*header
));
758 /* Try to create a packet of the full MTU */
760 p
= (const uint8_t *) pa_memblock_acquire_chunk(&u
->write_memchunk
);
761 to_encode
= u
->write_memchunk
.length
;
763 d
= (uint8_t*) a2dp
->buffer
+ sizeof(*header
) + sizeof(*payload
);
764 to_write
= a2dp
->buffer_size
- sizeof(*header
) - sizeof(*payload
);
766 while (PA_LIKELY(to_encode
> 0 && to_write
> 0)) {
770 encoded
= sbc_encode(&a2dp
->sbc
,
775 if (PA_UNLIKELY(encoded
<= 0)) {
776 pa_log_error("SBC encoding error (%li)", (long) encoded
);
777 pa_memblock_release(u
->write_memchunk
.memblock
);
781 /* pa_log_debug("SBC: encoded: %lu; written: %lu", (unsigned long) encoded, (unsigned long) written); */
782 /* pa_log_debug("SBC: codesize: %lu; frame_length: %lu", (unsigned long) a2dp->codesize, (unsigned long) a2dp->frame_length); */
784 pa_assert_fp((size_t) encoded
<= to_encode
);
785 pa_assert_fp((size_t) encoded
== a2dp
->codesize
);
787 pa_assert_fp((size_t) written
<= to_write
);
788 pa_assert_fp((size_t) written
== a2dp
->frame_length
);
790 p
= (const uint8_t*) p
+ encoded
;
791 to_encode
-= encoded
;
793 d
= (uint8_t*) d
+ written
;
799 pa_memblock_release(u
->write_memchunk
.memblock
);
801 pa_assert(to_encode
== 0);
804 pa_log_debug("Using SBC encoder implementation: %s", pa_strnull(sbc_get_implementation_info(&a2dp
->sbc
)));
807 /* write it to the fifo */
808 memset(a2dp
->buffer
, 0, sizeof(*header
) + sizeof(*payload
));
811 header
->sequence_number
= htons(a2dp
->seq_num
++);
812 header
->timestamp
= htonl(u
->write_index
/ pa_frame_size(&u
->sample_spec
));
813 header
->ssrc
= htonl(1);
814 payload
->frame_count
= frame_count
;
816 nbytes
= (uint8_t*) d
- (uint8_t*) a2dp
->buffer
;
821 l
= pa_write(u
->stream_fd
, a2dp
->buffer
, nbytes
, &u
->stream_write_type
);
828 /* Retry right away if we got interrupted */
831 else if (errno
== EAGAIN
)
832 /* Hmm, apparently the socket was not writable, give up for now */
835 pa_log_error("Failed to write data to socket: %s", pa_cstrerror(errno
));
840 pa_assert((size_t) l
<= nbytes
);
842 if ((size_t) l
!= nbytes
) {
843 pa_log_warn("Wrote memory block to socket only partially! %llu written, wanted to write %llu.",
844 (unsigned long long) l
,
845 (unsigned long long) nbytes
);
850 u
->write_index
+= (uint64_t) u
->write_memchunk
.length
;
851 pa_memblock_unref(u
->write_memchunk
.memblock
);
852 pa_memchunk_reset(&u
->write_memchunk
);
862 static int a2dp_process_push(struct userdata
*u
) {
864 pa_memchunk memchunk
;
867 pa_assert(u
->profile
== PA_BLUEZ4_PROFILE_A2DP_SOURCE
);
868 pa_assert(u
->source
);
869 pa_assert(u
->read_smoother
);
871 memchunk
.memblock
= pa_memblock_new(u
->core
->mempool
, u
->read_block_size
);
872 memchunk
.index
= memchunk
.length
= 0;
875 bool found_tstamp
= false;
877 struct a2dp_info
*a2dp
;
878 struct rtp_header
*header
;
879 struct rtp_payload
*payload
;
883 size_t to_write
, to_decode
;
885 a2dp_prepare_buffer(u
);
888 header
= a2dp
->buffer
;
889 payload
= (struct rtp_payload
*) ((uint8_t*) a2dp
->buffer
+ sizeof(*header
));
891 l
= pa_read(u
->stream_fd
, a2dp
->buffer
, a2dp
->buffer_size
, &u
->stream_write_type
);
895 if (l
< 0 && errno
== EINTR
)
896 /* Retry right away if we got interrupted */
899 else if (l
< 0 && errno
== EAGAIN
)
900 /* Hmm, apparently the socket was not readable, give up for now. */
903 pa_log_error("Failed to read data from socket: %s", l
< 0 ? pa_cstrerror(errno
) : "EOF");
908 pa_assert((size_t) l
<= a2dp
->buffer_size
);
910 u
->read_index
+= (uint64_t) l
;
912 /* TODO: get timestamp from rtp */
914 /* pa_log_warn("Couldn't find SO_TIMESTAMP data in auxiliary recvmsg() data!"); */
915 tstamp
= pa_rtclock_now();
918 pa_smoother_put(u
->read_smoother
, tstamp
, pa_bytes_to_usec(u
->read_index
, &u
->sample_spec
));
919 pa_smoother_resume(u
->read_smoother
, tstamp
, true);
921 p
= (uint8_t*) a2dp
->buffer
+ sizeof(*header
) + sizeof(*payload
);
922 to_decode
= l
- sizeof(*header
) - sizeof(*payload
);
924 d
= pa_memblock_acquire(memchunk
.memblock
);
925 to_write
= memchunk
.length
= pa_memblock_get_length(memchunk
.memblock
);
927 while (PA_LIKELY(to_decode
> 0)) {
931 decoded
= sbc_decode(&a2dp
->sbc
,
936 if (PA_UNLIKELY(decoded
<= 0)) {
937 pa_log_error("SBC decoding error (%li)", (long) decoded
);
938 pa_memblock_release(memchunk
.memblock
);
939 pa_memblock_unref(memchunk
.memblock
);
943 /* pa_log_debug("SBC: decoded: %lu; written: %lu", (unsigned long) decoded, (unsigned long) written); */
944 /* pa_log_debug("SBC: frame_length: %lu; codesize: %lu", (unsigned long) a2dp->frame_length, (unsigned long) a2dp->codesize); */
946 /* Reset frame length, it can be changed due to bitpool change */
947 a2dp
->frame_length
= sbc_get_frame_length(&a2dp
->sbc
);
949 pa_assert_fp((size_t) decoded
<= to_decode
);
950 pa_assert_fp((size_t) decoded
== a2dp
->frame_length
);
952 pa_assert_fp((size_t) written
== a2dp
->codesize
);
954 p
= (const uint8_t*) p
+ decoded
;
955 to_decode
-= decoded
;
957 d
= (uint8_t*) d
+ written
;
961 memchunk
.length
-= to_write
;
963 pa_memblock_release(memchunk
.memblock
);
965 pa_source_post(u
->source
, &memchunk
);
971 pa_memblock_unref(memchunk
.memblock
);
976 static void a2dp_reduce_bitpool(struct userdata
*u
) {
977 struct a2dp_info
*a2dp
;
984 /* Check if bitpool is already at its limit */
985 if (a2dp
->sbc
.bitpool
<= BITPOOL_DEC_LIMIT
)
988 bitpool
= a2dp
->sbc
.bitpool
- BITPOOL_DEC_STEP
;
990 if (bitpool
< BITPOOL_DEC_LIMIT
)
991 bitpool
= BITPOOL_DEC_LIMIT
;
993 a2dp_set_bitpool(u
, bitpool
);
996 static void thread_func(void *userdata
) {
997 struct userdata
*u
= userdata
;
998 unsigned do_write
= 0;
999 unsigned pending_read_bytes
= 0;
1000 bool writable
= false;
1003 pa_assert(u
->transport
);
1005 pa_log_debug("IO Thread starting up");
1007 if (u
->core
->realtime_scheduling
)
1008 pa_make_realtime(u
->core
->realtime_priority
);
1010 pa_thread_mq_install(&u
->thread_mq
);
1012 /* Setup the stream only if the transport was already acquired */
1013 if (u
->transport_acquired
)
1017 struct pollfd
*pollfd
;
1019 bool disable_timer
= true;
1021 pollfd
= u
->rtpoll_item
? pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
) : NULL
;
1023 if (u
->source
&& PA_SOURCE_IS_LINKED(u
->source
->thread_info
.state
)) {
1025 /* We should send two blocks to the device before we expect
1028 if (u
->write_index
== 0 && u
->read_index
<= 0)
1031 if (pollfd
&& (pollfd
->revents
& POLLIN
)) {
1034 if (u
->profile
== PA_BLUEZ4_PROFILE_HSP
|| u
->profile
== PA_BLUEZ4_PROFILE_HFGW
)
1035 n_read
= hsp_process_push(u
);
1037 n_read
= a2dp_process_push(u
);
1043 /* We just read something, so we are supposed to write something, too */
1044 pending_read_bytes
+= n_read
;
1045 do_write
+= pending_read_bytes
/ u
->write_block_size
;
1046 pending_read_bytes
= pending_read_bytes
% u
->write_block_size
;
1051 if (u
->sink
&& PA_SINK_IS_LINKED(u
->sink
->thread_info
.state
)) {
1053 if (PA_UNLIKELY(u
->sink
->thread_info
.rewind_requested
))
1054 pa_sink_process_rewind(u
->sink
, 0);
1057 if (pollfd
->revents
& POLLOUT
)
1060 if ((!u
->source
|| !PA_SOURCE_IS_LINKED(u
->source
->thread_info
.state
)) && do_write
<= 0 && writable
) {
1061 pa_usec_t time_passed
;
1062 pa_usec_t audio_sent
;
1064 /* Hmm, there is no input stream we could synchronize
1065 * to. So let's do things by time */
1067 time_passed
= pa_rtclock_now() - u
->started_at
;
1068 audio_sent
= pa_bytes_to_usec(u
->write_index
, &u
->sample_spec
);
1070 if (audio_sent
<= time_passed
) {
1071 pa_usec_t audio_to_send
= time_passed
- audio_sent
;
1073 /* Never try to catch up for more than 100ms */
1074 if (u
->write_index
> 0 && audio_to_send
> MAX_PLAYBACK_CATCH_UP_USEC
) {
1075 pa_usec_t skip_usec
;
1076 uint64_t skip_bytes
;
1078 skip_usec
= audio_to_send
- MAX_PLAYBACK_CATCH_UP_USEC
;
1079 skip_bytes
= pa_usec_to_bytes(skip_usec
, &u
->sample_spec
);
1081 if (skip_bytes
> 0) {
1084 pa_log_warn("Skipping %llu us (= %llu bytes) in audio stream",
1085 (unsigned long long) skip_usec
,
1086 (unsigned long long) skip_bytes
);
1088 pa_sink_render_full(u
->sink
, skip_bytes
, &tmp
);
1089 pa_memblock_unref(tmp
.memblock
);
1090 u
->write_index
+= skip_bytes
;
1092 if (u
->profile
== PA_BLUEZ4_PROFILE_A2DP
)
1093 a2dp_reduce_bitpool(u
);
1098 pending_read_bytes
= 0;
1102 if (writable
&& do_write
> 0) {
1105 if (u
->write_index
<= 0)
1106 u
->started_at
= pa_rtclock_now();
1108 if (u
->profile
== PA_BLUEZ4_PROFILE_A2DP
) {
1109 if ((n_written
= a2dp_process_render(u
)) < 0)
1112 if ((n_written
= hsp_process_render(u
)) < 0)
1117 pa_log("Broken kernel: we got EAGAIN on write() after POLLOUT!");
1119 do_write
-= n_written
;
1123 if ((!u
->source
|| !PA_SOURCE_IS_LINKED(u
->source
->thread_info
.state
)) && do_write
<= 0) {
1124 pa_usec_t sleep_for
;
1125 pa_usec_t time_passed
, next_write_at
;
1128 /* Hmm, there is no input stream we could synchronize
1129 * to. So let's estimate when we need to wake up the latest */
1130 time_passed
= pa_rtclock_now() - u
->started_at
;
1131 next_write_at
= pa_bytes_to_usec(u
->write_index
, &u
->sample_spec
);
1132 sleep_for
= time_passed
< next_write_at
? next_write_at
- time_passed
: 0;
1133 /* 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); */
1135 /* drop stream every 500 ms */
1136 sleep_for
= PA_USEC_PER_MSEC
* 500;
1138 pa_rtpoll_set_timer_relative(u
->rtpoll
, sleep_for
);
1139 disable_timer
= false;
1145 pa_rtpoll_set_timer_disabled(u
->rtpoll
);
1147 /* Hmm, nothing to do. Let's sleep */
1149 pollfd
->events
= (short) (((u
->sink
&& PA_SINK_IS_LINKED(u
->sink
->thread_info
.state
) && !writable
) ? POLLOUT
: 0) |
1150 (u
->source
&& PA_SOURCE_IS_LINKED(u
->source
->thread_info
.state
) ? POLLIN
: 0));
1152 if ((ret
= pa_rtpoll_run(u
->rtpoll
, true)) < 0) {
1153 pa_log_debug("pa_rtpoll_run failed with: %d", ret
);
1157 pa_log_debug("IO thread shutdown requested, stopping cleanly");
1158 bt_transport_release(u
);
1162 pollfd
= u
->rtpoll_item
? pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
) : NULL
;
1164 if (pollfd
&& (pollfd
->revents
& ~(POLLOUT
|POLLIN
))) {
1165 pa_log_info("FD error: %s%s%s%s",
1166 pollfd
->revents
& POLLERR
? "POLLERR " :"",
1167 pollfd
->revents
& POLLHUP
? "POLLHUP " :"",
1168 pollfd
->revents
& POLLPRI
? "POLLPRI " :"",
1169 pollfd
->revents
& POLLNVAL
? "POLLNVAL " :"");
1176 /* In case of HUP, just tear down the streams */
1177 if (!pollfd
|| (pollfd
->revents
& POLLHUP
) == 0)
1181 pending_read_bytes
= 0;
1188 /* If this was no regular exit from the loop we have to continue processing messages until we receive PA_MESSAGE_SHUTDOWN */
1189 pa_log_debug("IO thread failed");
1190 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(u
->msg
), BLUETOOTH_MESSAGE_IO_THREAD_FAILED
, NULL
, 0, NULL
, NULL
);
1191 pa_asyncmsgq_wait_for(u
->thread_mq
.inq
, PA_MESSAGE_SHUTDOWN
);
1194 pa_log_debug("IO thread shutting down");
1197 static pa_available_t
transport_state_to_availability(pa_bluez4_transport_state_t state
) {
1198 if (state
== PA_BLUEZ4_TRANSPORT_STATE_DISCONNECTED
)
1199 return PA_AVAILABLE_NO
;
1200 else if (state
>= PA_BLUEZ4_TRANSPORT_STATE_PLAYING
)
1201 return PA_AVAILABLE_YES
;
1203 return PA_AVAILABLE_UNKNOWN
;
1206 static pa_direction_t
get_profile_direction(pa_bluez4_profile_t p
) {
1207 static const pa_direction_t profile_direction
[] = {
1208 [PA_BLUEZ4_PROFILE_A2DP
] = PA_DIRECTION_OUTPUT
,
1209 [PA_BLUEZ4_PROFILE_A2DP_SOURCE
] = PA_DIRECTION_INPUT
,
1210 [PA_BLUEZ4_PROFILE_HSP
] = PA_DIRECTION_INPUT
| PA_DIRECTION_OUTPUT
,
1211 [PA_BLUEZ4_PROFILE_HFGW
] = PA_DIRECTION_INPUT
| PA_DIRECTION_OUTPUT
,
1212 [PA_BLUEZ4_PROFILE_OFF
] = 0
1215 return profile_direction
[p
];
1218 /* Run from main thread */
1219 static pa_available_t
get_port_availability(struct userdata
*u
, pa_direction_t direction
) {
1220 pa_available_t result
= PA_AVAILABLE_NO
;
1224 pa_assert(u
->device
);
1226 for (i
= 0; i
< PA_BLUEZ4_PROFILE_COUNT
; i
++) {
1227 pa_bluez4_transport
*transport
;
1229 if (!(get_profile_direction(i
) & direction
))
1232 if (!(transport
= u
->device
->transports
[i
]))
1235 switch(transport
->state
) {
1236 case PA_BLUEZ4_TRANSPORT_STATE_DISCONNECTED
:
1239 case PA_BLUEZ4_TRANSPORT_STATE_IDLE
:
1240 if (result
== PA_AVAILABLE_NO
)
1241 result
= PA_AVAILABLE_UNKNOWN
;
1245 case PA_BLUEZ4_TRANSPORT_STATE_PLAYING
:
1246 return PA_AVAILABLE_YES
;
1253 /* Run from main thread */
1254 static void handle_transport_state_change(struct userdata
*u
, struct pa_bluez4_transport
*transport
) {
1255 bool acquire
= false;
1256 bool release
= false;
1257 pa_bluez4_profile_t profile
;
1258 pa_card_profile
*cp
;
1259 pa_bluez4_transport_state_t state
;
1260 pa_device_port
*port
;
1263 pa_assert(transport
);
1265 profile
= transport
->profile
;
1266 state
= transport
->state
;
1268 /* Update profile availability */
1269 if (!(cp
= pa_hashmap_get(u
->card
->profiles
, pa_bluez4_profile_to_string(profile
))))
1272 pa_card_profile_set_available(cp
, transport_state_to_availability(state
));
1274 /* Update port availability */
1275 pa_assert_se(port
= pa_hashmap_get(u
->card
->ports
, u
->output_port_name
));
1276 pa_device_port_set_available(port
, get_port_availability(u
, PA_DIRECTION_OUTPUT
));
1278 pa_assert_se(port
= pa_hashmap_get(u
->card
->ports
, u
->input_port_name
));
1279 pa_device_port_set_available(port
, get_port_availability(u
, PA_DIRECTION_INPUT
));
1281 /* Acquire or release transport as needed */
1282 acquire
= (state
== PA_BLUEZ4_TRANSPORT_STATE_PLAYING
&& u
->profile
== profile
);
1283 release
= (state
!= PA_BLUEZ4_TRANSPORT_STATE_PLAYING
&& u
->profile
== profile
);
1286 if (bt_transport_acquire(u
, true) >= 0) {
1288 pa_log_debug("Resuming source %s, because the bluetooth audio state changed to 'playing'.", u
->source
->name
);
1289 pa_source_suspend(u
->source
, false, PA_SUSPEND_IDLE
|PA_SUSPEND_USER
);
1293 pa_log_debug("Resuming sink %s, because the bluetooth audio state changed to 'playing'.", u
->sink
->name
);
1294 pa_sink_suspend(u
->sink
, false, PA_SUSPEND_IDLE
|PA_SUSPEND_USER
);
1298 if (release
&& u
->transport_acquired
) {
1299 /* FIXME: this release is racy, since the audio stream might have
1300 been set up again in the meantime (but not processed yet by PA).
1301 BlueZ should probably release the transport automatically, and
1302 in that case we would just mark the transport as released */
1304 /* Remote side closed the stream so we consider it PA_SUSPEND_USER */
1306 pa_log_debug("Suspending source %s, because the remote end closed the stream.", u
->source
->name
);
1307 pa_source_suspend(u
->source
, true, PA_SUSPEND_USER
);
1311 pa_log_debug("Suspending sink %s, because the remote end closed the stream.", u
->sink
->name
);
1312 pa_sink_suspend(u
->sink
, true, PA_SUSPEND_USER
);
1317 /* Run from main thread */
1318 static void sink_set_volume_cb(pa_sink
*s
) {
1327 k
= pa_sprintf_malloc("bluetooth-device@%p", (void*) s
);
1328 u
= pa_shared_get(s
->core
, k
);
1332 pa_assert(u
->sink
== s
);
1333 pa_assert(u
->profile
== PA_BLUEZ4_PROFILE_HSP
);
1334 pa_assert(u
->transport
);
1336 gain
= (dbus_uint16_t
) round((double) pa_cvolume_max(&s
->real_volume
) * HSP_MAX_GAIN
/ PA_VOLUME_NORM
);
1337 volume
= (pa_volume_t
) round((double) gain
* PA_VOLUME_NORM
/ HSP_MAX_GAIN
);
1339 pa_cvolume_set(&s
->real_volume
, u
->sample_spec
.channels
, volume
);
1341 pa_bluez4_transport_set_speaker_gain(u
->transport
, gain
);
1344 /* Run from main thread */
1345 static void source_set_volume_cb(pa_source
*s
) {
1354 k
= pa_sprintf_malloc("bluetooth-device@%p", (void*) s
);
1355 u
= pa_shared_get(s
->core
, k
);
1359 pa_assert(u
->source
== s
);
1360 pa_assert(u
->profile
== PA_BLUEZ4_PROFILE_HSP
);
1361 pa_assert(u
->transport
);
1363 gain
= (dbus_uint16_t
) round((double) pa_cvolume_max(&s
->real_volume
) * HSP_MAX_GAIN
/ PA_VOLUME_NORM
);
1364 volume
= (pa_volume_t
) round((double) gain
* PA_VOLUME_NORM
/ HSP_MAX_GAIN
);
1366 pa_cvolume_set(&s
->real_volume
, u
->sample_spec
.channels
, volume
);
1368 pa_bluez4_transport_set_microphone_gain(u
->transport
, gain
);
1371 /* Run from main thread */
1372 static char *get_name(const char *type
, pa_modargs
*ma
, const char *device_id
, bool *namereg_fail
) {
1378 pa_assert(device_id
);
1379 pa_assert(namereg_fail
);
1381 t
= pa_sprintf_malloc("%s_name", type
);
1382 n
= pa_modargs_get_value(ma
, t
, NULL
);
1386 *namereg_fail
= true;
1387 return pa_xstrdup(n
);
1390 if ((n
= pa_modargs_get_value(ma
, "name", NULL
)))
1391 *namereg_fail
= true;
1394 *namereg_fail
= false;
1397 return pa_sprintf_malloc("bluez_%s.%s", type
, n
);
1400 static int sco_over_pcm_state_update(struct userdata
*u
, bool changed
) {
1402 pa_assert(USE_SCO_OVER_PCM(u
));
1404 if (PA_SINK_IS_OPENED(pa_sink_get_state(u
->hsp
.sco_sink
)) ||
1405 PA_SOURCE_IS_OPENED(pa_source_get_state(u
->hsp
.sco_source
))) {
1407 if (u
->stream_fd
>= 0)
1410 pa_log_debug("Resuming SCO over PCM");
1411 if (init_profile(u
) < 0) {
1412 pa_log("Can't resume SCO over PCM");
1416 if (bt_transport_acquire(u
, false) < 0)
1425 if (u
->stream_fd
< 0)
1428 pa_log_debug("Closing SCO over PCM");
1430 bt_transport_release(u
);
1436 static pa_hook_result_t
sink_state_changed_cb(pa_core
*c
, pa_sink
*s
, struct userdata
*u
) {
1438 pa_sink_assert_ref(s
);
1441 if (!USE_SCO_OVER_PCM(u
) || s
!= u
->hsp
.sco_sink
)
1444 sco_over_pcm_state_update(u
, true);
1449 static pa_hook_result_t
source_state_changed_cb(pa_core
*c
, pa_source
*s
, struct userdata
*u
) {
1451 pa_source_assert_ref(s
);
1454 if (!USE_SCO_OVER_PCM(u
) || s
!= u
->hsp
.sco_source
)
1457 sco_over_pcm_state_update(u
, true);
1462 static pa_hook_result_t
transport_nrec_changed_cb(pa_bluez4_discovery
*y
, pa_bluez4_transport
*t
, struct userdata
*u
) {
1468 if (t
!= u
->transport
)
1471 p
= pa_proplist_new();
1472 pa_proplist_sets(p
, "bluetooth.nrec", t
->nrec
? "1" : "0");
1473 pa_source_update_proplist(u
->source
, PA_UPDATE_REPLACE
, p
);
1474 pa_proplist_free(p
);
1479 static pa_hook_result_t
transport_microphone_gain_changed_cb(pa_bluez4_discovery
*y
, pa_bluez4_transport
*t
,
1480 struct userdata
*u
) {
1486 if (t
!= u
->transport
)
1489 pa_assert(u
->source
);
1491 pa_cvolume_set(&v
, u
->sample_spec
.channels
,
1492 (pa_volume_t
) round((double) t
->microphone_gain
* PA_VOLUME_NORM
/ HSP_MAX_GAIN
));
1493 pa_source_volume_changed(u
->source
, &v
);
1498 static pa_hook_result_t
transport_speaker_gain_changed_cb(pa_bluez4_discovery
*y
, pa_bluez4_transport
*t
,
1499 struct userdata
*u
) {
1505 if (t
!= u
->transport
)
1510 pa_cvolume_set(&v
, u
->sample_spec
.channels
, (pa_volume_t
) round((double) t
->speaker_gain
* PA_VOLUME_NORM
/ HSP_MAX_GAIN
));
1511 pa_sink_volume_changed(u
->sink
, &v
);
1516 static void connect_ports(struct userdata
*u
, void *sink_or_source_new_data
, pa_direction_t direction
) {
1517 pa_device_port
*port
;
1519 if (direction
== PA_DIRECTION_OUTPUT
) {
1520 pa_sink_new_data
*sink_new_data
= sink_or_source_new_data
;
1522 pa_assert_se(port
= pa_hashmap_get(u
->card
->ports
, u
->output_port_name
));
1523 pa_assert_se(pa_hashmap_put(sink_new_data
->ports
, port
->name
, port
) >= 0);
1524 pa_device_port_ref(port
);
1526 pa_source_new_data
*source_new_data
= sink_or_source_new_data
;
1528 pa_assert_se(port
= pa_hashmap_get(u
->card
->ports
, u
->input_port_name
));
1529 pa_assert_se(pa_hashmap_put(source_new_data
->ports
, port
->name
, port
) >= 0);
1530 pa_device_port_ref(port
);
1534 static int sink_set_port_cb(pa_sink
*s
, pa_device_port
*p
) {
1538 static int source_set_port_cb(pa_source
*s
, pa_device_port
*p
) {
1542 /* Run from main thread */
1543 static int add_sink(struct userdata
*u
) {
1546 pa_assert(u
->transport
);
1548 if (USE_SCO_OVER_PCM(u
)) {
1551 u
->sink
= u
->hsp
.sco_sink
;
1552 p
= pa_proplist_new();
1553 pa_proplist_sets(p
, "bluetooth.protocol", pa_bluez4_profile_to_string(u
->profile
));
1554 pa_proplist_update(u
->sink
->proplist
, PA_UPDATE_MERGE
, p
);
1555 pa_proplist_free(p
);
1557 pa_sink_new_data data
;
1560 pa_sink_new_data_init(&data
);
1561 data
.driver
= __FILE__
;
1562 data
.module
= u
->module
;
1563 pa_sink_new_data_set_sample_spec(&data
, &u
->sample_spec
);
1564 pa_proplist_sets(data
.proplist
, "bluetooth.protocol", pa_bluez4_profile_to_string(u
->profile
));
1565 if (u
->profile
== PA_BLUEZ4_PROFILE_HSP
)
1566 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_INTENDED_ROLES
, "phone");
1567 data
.card
= u
->card
;
1568 data
.name
= get_name("sink", u
->modargs
, u
->address
, &b
);
1569 data
.namereg_fail
= b
;
1571 if (pa_modargs_get_proplist(u
->modargs
, "sink_properties", data
.proplist
, PA_UPDATE_REPLACE
) < 0) {
1572 pa_log("Invalid properties");
1573 pa_sink_new_data_done(&data
);
1576 connect_ports(u
, &data
, PA_DIRECTION_OUTPUT
);
1578 if (!u
->transport_acquired
)
1579 switch (u
->profile
) {
1580 case PA_BLUEZ4_PROFILE_A2DP
:
1581 case PA_BLUEZ4_PROFILE_HSP
:
1582 pa_assert_not_reached(); /* Profile switch should have failed */
1584 case PA_BLUEZ4_PROFILE_HFGW
:
1585 data
.suspend_cause
= PA_SUSPEND_USER
;
1587 case PA_BLUEZ4_PROFILE_A2DP_SOURCE
:
1588 case PA_BLUEZ4_PROFILE_OFF
:
1589 pa_assert_not_reached();
1592 u
->sink
= pa_sink_new(u
->core
, &data
, PA_SINK_HARDWARE
|PA_SINK_LATENCY
);
1593 pa_sink_new_data_done(&data
);
1596 pa_log_error("Failed to create sink");
1600 u
->sink
->userdata
= u
;
1601 u
->sink
->parent
.process_msg
= sink_process_msg
;
1602 u
->sink
->set_port
= sink_set_port_cb
;
1605 if (u
->profile
== PA_BLUEZ4_PROFILE_HSP
) {
1606 pa_sink_set_set_volume_callback(u
->sink
, sink_set_volume_cb
);
1607 u
->sink
->n_volume_steps
= 16;
1609 k
= pa_sprintf_malloc("bluetooth-device@%p", (void*) u
->sink
);
1610 pa_shared_set(u
->core
, k
, u
);
1617 /* Run from main thread */
1618 static int add_source(struct userdata
*u
) {
1621 pa_assert(u
->transport
);
1623 if (USE_SCO_OVER_PCM(u
)) {
1624 u
->source
= u
->hsp
.sco_source
;
1625 pa_proplist_sets(u
->source
->proplist
, "bluetooth.protocol", pa_bluez4_profile_to_string(u
->profile
));
1627 pa_source_new_data data
;
1630 pa_source_new_data_init(&data
);
1631 data
.driver
= __FILE__
;
1632 data
.module
= u
->module
;
1633 pa_source_new_data_set_sample_spec(&data
, &u
->sample_spec
);
1634 pa_proplist_sets(data
.proplist
, "bluetooth.protocol", pa_bluez4_profile_to_string(u
->profile
));
1635 if (u
->profile
== PA_BLUEZ4_PROFILE_HSP
)
1636 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_INTENDED_ROLES
, "phone");
1638 data
.card
= u
->card
;
1639 data
.name
= get_name("source", u
->modargs
, u
->address
, &b
);
1640 data
.namereg_fail
= b
;
1642 if (pa_modargs_get_proplist(u
->modargs
, "source_properties", data
.proplist
, PA_UPDATE_REPLACE
) < 0) {
1643 pa_log("Invalid properties");
1644 pa_source_new_data_done(&data
);
1648 connect_ports(u
, &data
, PA_DIRECTION_INPUT
);
1650 if (!u
->transport_acquired
)
1651 switch (u
->profile
) {
1652 case PA_BLUEZ4_PROFILE_HSP
:
1653 pa_assert_not_reached(); /* Profile switch should have failed */
1655 case PA_BLUEZ4_PROFILE_A2DP_SOURCE
:
1656 case PA_BLUEZ4_PROFILE_HFGW
:
1657 data
.suspend_cause
= PA_SUSPEND_USER
;
1659 case PA_BLUEZ4_PROFILE_A2DP
:
1660 case PA_BLUEZ4_PROFILE_OFF
:
1661 pa_assert_not_reached();
1664 u
->source
= pa_source_new(u
->core
, &data
, PA_SOURCE_HARDWARE
|PA_SOURCE_LATENCY
);
1665 pa_source_new_data_done(&data
);
1668 pa_log_error("Failed to create source");
1672 u
->source
->userdata
= u
;
1673 u
->source
->parent
.process_msg
= source_process_msg
;
1674 u
->source
->set_port
= source_set_port_cb
;
1677 if ((u
->profile
== PA_BLUEZ4_PROFILE_HSP
) || (u
->profile
== PA_BLUEZ4_PROFILE_HFGW
)) {
1678 pa_bluez4_transport
*t
= u
->transport
;
1679 pa_proplist_sets(u
->source
->proplist
, "bluetooth.nrec", t
->nrec
? "1" : "0");
1682 if (u
->profile
== PA_BLUEZ4_PROFILE_HSP
) {
1683 pa_source_set_set_volume_callback(u
->source
, source_set_volume_cb
);
1684 u
->source
->n_volume_steps
= 16;
1686 k
= pa_sprintf_malloc("bluetooth-device@%p", (void*) u
->source
);
1687 pa_shared_set(u
->core
, k
, u
);
1694 static void bt_transport_config_a2dp(struct userdata
*u
) {
1695 const pa_bluez4_transport
*t
;
1696 struct a2dp_info
*a2dp
= &u
->a2dp
;
1702 config
= (a2dp_sbc_t
*) t
->config
;
1704 u
->sample_spec
.format
= PA_SAMPLE_S16LE
;
1706 if (a2dp
->sbc_initialized
)
1707 sbc_reinit(&a2dp
->sbc
, 0);
1709 sbc_init(&a2dp
->sbc
, 0);
1710 a2dp
->sbc_initialized
= true;
1712 switch (config
->frequency
) {
1713 case SBC_SAMPLING_FREQ_16000
:
1714 a2dp
->sbc
.frequency
= SBC_FREQ_16000
;
1715 u
->sample_spec
.rate
= 16000U;
1717 case SBC_SAMPLING_FREQ_32000
:
1718 a2dp
->sbc
.frequency
= SBC_FREQ_32000
;
1719 u
->sample_spec
.rate
= 32000U;
1721 case SBC_SAMPLING_FREQ_44100
:
1722 a2dp
->sbc
.frequency
= SBC_FREQ_44100
;
1723 u
->sample_spec
.rate
= 44100U;
1725 case SBC_SAMPLING_FREQ_48000
:
1726 a2dp
->sbc
.frequency
= SBC_FREQ_48000
;
1727 u
->sample_spec
.rate
= 48000U;
1730 pa_assert_not_reached();
1733 switch (config
->channel_mode
) {
1734 case SBC_CHANNEL_MODE_MONO
:
1735 a2dp
->sbc
.mode
= SBC_MODE_MONO
;
1736 u
->sample_spec
.channels
= 1;
1738 case SBC_CHANNEL_MODE_DUAL_CHANNEL
:
1739 a2dp
->sbc
.mode
= SBC_MODE_DUAL_CHANNEL
;
1740 u
->sample_spec
.channels
= 2;
1742 case SBC_CHANNEL_MODE_STEREO
:
1743 a2dp
->sbc
.mode
= SBC_MODE_STEREO
;
1744 u
->sample_spec
.channels
= 2;
1746 case SBC_CHANNEL_MODE_JOINT_STEREO
:
1747 a2dp
->sbc
.mode
= SBC_MODE_JOINT_STEREO
;
1748 u
->sample_spec
.channels
= 2;
1751 pa_assert_not_reached();
1754 switch (config
->allocation_method
) {
1755 case SBC_ALLOCATION_SNR
:
1756 a2dp
->sbc
.allocation
= SBC_AM_SNR
;
1758 case SBC_ALLOCATION_LOUDNESS
:
1759 a2dp
->sbc
.allocation
= SBC_AM_LOUDNESS
;
1762 pa_assert_not_reached();
1765 switch (config
->subbands
) {
1766 case SBC_SUBBANDS_4
:
1767 a2dp
->sbc
.subbands
= SBC_SB_4
;
1769 case SBC_SUBBANDS_8
:
1770 a2dp
->sbc
.subbands
= SBC_SB_8
;
1773 pa_assert_not_reached();
1776 switch (config
->block_length
) {
1777 case SBC_BLOCK_LENGTH_4
:
1778 a2dp
->sbc
.blocks
= SBC_BLK_4
;
1780 case SBC_BLOCK_LENGTH_8
:
1781 a2dp
->sbc
.blocks
= SBC_BLK_8
;
1783 case SBC_BLOCK_LENGTH_12
:
1784 a2dp
->sbc
.blocks
= SBC_BLK_12
;
1786 case SBC_BLOCK_LENGTH_16
:
1787 a2dp
->sbc
.blocks
= SBC_BLK_16
;
1790 pa_assert_not_reached();
1793 a2dp
->min_bitpool
= config
->min_bitpool
;
1794 a2dp
->max_bitpool
= config
->max_bitpool
;
1796 /* Set minimum bitpool for source to get the maximum possible block_size */
1797 a2dp
->sbc
.bitpool
= u
->profile
== PA_BLUEZ4_PROFILE_A2DP
? a2dp
->max_bitpool
: a2dp
->min_bitpool
;
1798 a2dp
->codesize
= sbc_get_codesize(&a2dp
->sbc
);
1799 a2dp
->frame_length
= sbc_get_frame_length(&a2dp
->sbc
);
1801 pa_log_info("SBC parameters:\n\tallocation=%u\n\tsubbands=%u\n\tblocks=%u\n\tbitpool=%u\n",
1802 a2dp
->sbc
.allocation
, a2dp
->sbc
.subbands
, a2dp
->sbc
.blocks
, a2dp
->sbc
.bitpool
);
1805 static void bt_transport_config(struct userdata
*u
) {
1806 if (u
->profile
== PA_BLUEZ4_PROFILE_HSP
|| u
->profile
== PA_BLUEZ4_PROFILE_HFGW
) {
1807 u
->sample_spec
.format
= PA_SAMPLE_S16LE
;
1808 u
->sample_spec
.channels
= 1;
1809 u
->sample_spec
.rate
= 8000;
1811 bt_transport_config_a2dp(u
);
1814 /* Run from main thread */
1815 static pa_hook_result_t
transport_state_changed_cb(pa_bluez4_discovery
*y
, pa_bluez4_transport
*t
, struct userdata
*u
) {
1819 if (t
== u
->transport
&& t
->state
== PA_BLUEZ4_TRANSPORT_STATE_DISCONNECTED
)
1820 pa_assert_se(pa_card_set_profile(u
->card
, pa_hashmap_get(u
->card
->profiles
, "off"), false) >= 0);
1822 if (t
->device
== u
->device
)
1823 handle_transport_state_change(u
, t
);
1828 /* Run from main thread */
1829 static int setup_transport(struct userdata
*u
) {
1830 pa_bluez4_transport
*t
;
1833 pa_assert(!u
->transport
);
1834 pa_assert(u
->profile
!= PA_BLUEZ4_PROFILE_OFF
);
1836 /* check if profile has a transport */
1837 t
= u
->device
->transports
[u
->profile
];
1838 if (!t
|| t
->state
== PA_BLUEZ4_TRANSPORT_STATE_DISCONNECTED
) {
1839 pa_log_warn("Profile has no transport");
1845 if (u
->profile
== PA_BLUEZ4_PROFILE_A2DP_SOURCE
|| u
->profile
== PA_BLUEZ4_PROFILE_HFGW
)
1846 bt_transport_acquire(u
, true); /* In case of error, the sink/sources will be created suspended */
1847 else if (bt_transport_acquire(u
, false) < 0)
1848 return -1; /* We need to fail here until the interactions with module-suspend-on-idle and alike get improved */
1850 bt_transport_config(u
);
1855 /* Run from main thread */
1856 static int init_profile(struct userdata
*u
) {
1859 pa_assert(u
->profile
!= PA_BLUEZ4_PROFILE_OFF
);
1861 if (setup_transport(u
) < 0)
1864 pa_assert(u
->transport
);
1866 if (u
->profile
== PA_BLUEZ4_PROFILE_A2DP
||
1867 u
->profile
== PA_BLUEZ4_PROFILE_HSP
||
1868 u
->profile
== PA_BLUEZ4_PROFILE_HFGW
)
1869 if (add_sink(u
) < 0)
1872 if (u
->profile
== PA_BLUEZ4_PROFILE_HSP
||
1873 u
->profile
== PA_BLUEZ4_PROFILE_A2DP_SOURCE
||
1874 u
->profile
== PA_BLUEZ4_PROFILE_HFGW
)
1875 if (add_source(u
) < 0)
1881 /* Run from main thread */
1882 static void stop_thread(struct userdata
*u
) {
1887 if (u
->sink
&& !USE_SCO_OVER_PCM(u
))
1888 pa_sink_unlink(u
->sink
);
1890 if (u
->source
&& !USE_SCO_OVER_PCM(u
))
1891 pa_source_unlink(u
->source
);
1894 pa_asyncmsgq_send(u
->thread_mq
.inq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, 0, NULL
);
1895 pa_thread_free(u
->thread
);
1899 if (u
->rtpoll_item
) {
1900 pa_rtpoll_item_free(u
->rtpoll_item
);
1901 u
->rtpoll_item
= NULL
;
1905 pa_thread_mq_done(&u
->thread_mq
);
1907 pa_rtpoll_free(u
->rtpoll
);
1912 bt_transport_release(u
);
1913 u
->transport
= NULL
;
1917 if (u
->profile
== PA_BLUEZ4_PROFILE_HSP
) {
1918 k
= pa_sprintf_malloc("bluetooth-device@%p", (void*) u
->sink
);
1919 pa_shared_remove(u
->core
, k
);
1923 pa_sink_unref(u
->sink
);
1928 if (u
->profile
== PA_BLUEZ4_PROFILE_HSP
) {
1929 k
= pa_sprintf_malloc("bluetooth-device@%p", (void*) u
->source
);
1930 pa_shared_remove(u
->core
, k
);
1934 pa_source_unref(u
->source
);
1938 if (u
->read_smoother
) {
1939 pa_smoother_free(u
->read_smoother
);
1940 u
->read_smoother
= NULL
;
1944 /* Run from main thread */
1945 static int start_thread(struct userdata
*u
) {
1947 pa_assert(!u
->thread
);
1948 pa_assert(!u
->rtpoll
);
1949 pa_assert(!u
->rtpoll_item
);
1951 u
->rtpoll
= pa_rtpoll_new();
1952 pa_thread_mq_init(&u
->thread_mq
, u
->core
->mainloop
, u
->rtpoll
);
1954 if (USE_SCO_OVER_PCM(u
)) {
1955 if (sco_over_pcm_state_update(u
, false) < 0) {
1959 k
= pa_sprintf_malloc("bluetooth-device@%p", (void*) u
->sink
);
1960 pa_shared_remove(u
->core
, k
);
1965 k
= pa_sprintf_malloc("bluetooth-device@%p", (void*) u
->source
);
1966 pa_shared_remove(u
->core
, k
);
1973 pa_sink_ref(u
->sink
);
1974 pa_source_ref(u
->source
);
1975 /* FIXME: monitor stream_fd error */
1979 if (!(u
->thread
= pa_thread_new("bluetooth", thread_func
, u
))) {
1980 pa_log_error("Failed to create IO thread");
1985 pa_sink_set_asyncmsgq(u
->sink
, u
->thread_mq
.inq
);
1986 pa_sink_set_rtpoll(u
->sink
, u
->rtpoll
);
1987 pa_sink_put(u
->sink
);
1989 if (u
->sink
->set_volume
)
1990 u
->sink
->set_volume(u
->sink
);
1994 pa_source_set_asyncmsgq(u
->source
, u
->thread_mq
.inq
);
1995 pa_source_set_rtpoll(u
->source
, u
->rtpoll
);
1996 pa_source_put(u
->source
);
1998 if (u
->source
->set_volume
)
1999 u
->source
->set_volume(u
->source
);
2005 static void save_sco_volume_callbacks(struct userdata
*u
) {
2007 pa_assert(USE_SCO_OVER_PCM(u
));
2009 u
->hsp
.sco_sink_set_volume
= u
->hsp
.sco_sink
->set_volume
;
2010 u
->hsp
.sco_source_set_volume
= u
->hsp
.sco_source
->set_volume
;
2013 static void restore_sco_volume_callbacks(struct userdata
*u
) {
2015 pa_assert(USE_SCO_OVER_PCM(u
));
2017 pa_sink_set_set_volume_callback(u
->hsp
.sco_sink
, u
->hsp
.sco_sink_set_volume
);
2018 pa_source_set_set_volume_callback(u
->hsp
.sco_source
, u
->hsp
.sco_source_set_volume
);
2021 /* Run from main thread */
2022 static int card_set_profile(pa_card
*c
, pa_card_profile
*new_profile
) {
2024 pa_bluez4_profile_t
*d
;
2027 pa_assert(new_profile
);
2028 pa_assert_se(u
= c
->userdata
);
2030 d
= PA_CARD_PROFILE_DATA(new_profile
);
2032 if (*d
!= PA_BLUEZ4_PROFILE_OFF
) {
2033 const pa_bluez4_device
*device
= u
->device
;
2035 if (!device
->transports
[*d
] || device
->transports
[*d
]->state
== PA_BLUEZ4_TRANSPORT_STATE_DISCONNECTED
) {
2036 pa_log_warn("Profile not connected, refused to switch profile to %s", new_profile
->name
);
2043 if (USE_SCO_OVER_PCM(u
))
2044 restore_sco_volume_callbacks(u
);
2047 u
->sample_spec
= u
->requested_sample_spec
;
2049 if (USE_SCO_OVER_PCM(u
))
2050 save_sco_volume_callbacks(u
);
2052 if (u
->profile
!= PA_BLUEZ4_PROFILE_OFF
)
2053 if (init_profile(u
) < 0)
2056 if (u
->sink
|| u
->source
)
2057 if (start_thread(u
) < 0)
2065 pa_assert_se(pa_card_set_profile(u
->card
, pa_hashmap_get(u
->card
->profiles
, "off"), false) >= 0);
2070 /* Run from main thread */
2071 static void create_card_ports(struct userdata
*u
, pa_hashmap
*ports
) {
2072 pa_device_port
*port
;
2073 pa_device_port_new_data port_data
;
2075 const char *name_prefix
= NULL
;
2076 const char *input_description
= NULL
;
2077 const char *output_description
= NULL
;
2081 pa_assert(u
->device
);
2083 switch (pa_bluez4_get_form_factor(u
->device
->class)) {
2084 case PA_BLUEZ4_FORM_FACTOR_UNKNOWN
:
2087 case PA_BLUEZ4_FORM_FACTOR_HEADSET
:
2088 name_prefix
= "headset";
2089 input_description
= output_description
= _("Headset");
2092 case PA_BLUEZ4_FORM_FACTOR_HANDSFREE
:
2093 name_prefix
= "handsfree";
2094 input_description
= output_description
= _("Handsfree");
2097 case PA_BLUEZ4_FORM_FACTOR_MICROPHONE
:
2098 name_prefix
= "microphone";
2099 input_description
= _("Microphone");
2102 case PA_BLUEZ4_FORM_FACTOR_SPEAKER
:
2103 name_prefix
= "speaker";
2104 output_description
= _("Speaker");
2107 case PA_BLUEZ4_FORM_FACTOR_HEADPHONE
:
2108 name_prefix
= "headphone";
2109 output_description
= _("Headphone");
2112 case PA_BLUEZ4_FORM_FACTOR_PORTABLE
:
2113 name_prefix
= "portable";
2114 input_description
= output_description
= _("Portable");
2117 case PA_BLUEZ4_FORM_FACTOR_CAR
:
2118 name_prefix
= "car";
2119 input_description
= output_description
= _("Car");
2122 case PA_BLUEZ4_FORM_FACTOR_HIFI
:
2123 name_prefix
= "hifi";
2124 input_description
= output_description
= _("HiFi");
2127 case PA_BLUEZ4_FORM_FACTOR_PHONE
:
2128 name_prefix
= "phone";
2129 input_description
= output_description
= _("Phone");
2134 name_prefix
= "unknown";
2136 if (!output_description
)
2137 output_description
= _("Bluetooth Output");
2139 if (!input_description
)
2140 input_description
= _("Bluetooth Input");
2142 u
->output_port_name
= pa_sprintf_malloc("%s-output", name_prefix
);
2143 u
->input_port_name
= pa_sprintf_malloc("%s-input", name_prefix
);
2145 pa_device_port_new_data_init(&port_data
);
2146 pa_device_port_new_data_set_name(&port_data
, u
->output_port_name
);
2147 pa_device_port_new_data_set_description(&port_data
, output_description
);
2148 pa_device_port_new_data_set_direction(&port_data
, PA_DIRECTION_OUTPUT
);
2149 pa_device_port_new_data_set_available(&port_data
, get_port_availability(u
, PA_DIRECTION_OUTPUT
));
2150 pa_assert_se(port
= pa_device_port_new(u
->core
, &port_data
, 0));
2151 pa_assert_se(pa_hashmap_put(ports
, port
->name
, port
) >= 0);
2152 pa_device_port_new_data_done(&port_data
);
2154 pa_device_port_new_data_init(&port_data
);
2155 pa_device_port_new_data_set_name(&port_data
, u
->input_port_name
);
2156 pa_device_port_new_data_set_description(&port_data
, input_description
);
2157 pa_device_port_new_data_set_direction(&port_data
, PA_DIRECTION_INPUT
);
2158 pa_device_port_new_data_set_available(&port_data
, get_port_availability(u
, PA_DIRECTION_INPUT
));
2159 pa_assert_se(port
= pa_device_port_new(u
->core
, &port_data
, 0));
2160 pa_assert_se(pa_hashmap_put(ports
, port
->name
, port
) >= 0);
2161 pa_device_port_new_data_done(&port_data
);
2164 /* Run from main thread */
2165 static pa_card_profile
*create_card_profile(struct userdata
*u
, const char *uuid
, pa_hashmap
*ports
) {
2166 pa_device_port
*input_port
, *output_port
;
2167 pa_card_profile
*p
= NULL
;
2168 pa_bluez4_profile_t
*d
;
2170 pa_assert(u
->input_port_name
);
2171 pa_assert(u
->output_port_name
);
2172 pa_assert_se(input_port
= pa_hashmap_get(ports
, u
->input_port_name
));
2173 pa_assert_se(output_port
= pa_hashmap_get(ports
, u
->output_port_name
));
2175 if (pa_streq(uuid
, A2DP_SINK_UUID
)) {
2176 p
= pa_card_profile_new("a2dp", _("High Fidelity Playback (A2DP)"), sizeof(pa_bluez4_profile_t
));
2180 p
->max_sink_channels
= 2;
2181 p
->max_source_channels
= 0;
2182 pa_hashmap_put(output_port
->profiles
, p
->name
, p
);
2184 d
= PA_CARD_PROFILE_DATA(p
);
2185 *d
= PA_BLUEZ4_PROFILE_A2DP
;
2186 } else if (pa_streq(uuid
, A2DP_SOURCE_UUID
)) {
2187 p
= pa_card_profile_new("a2dp_source", _("High Fidelity Capture (A2DP)"), sizeof(pa_bluez4_profile_t
));
2191 p
->max_sink_channels
= 0;
2192 p
->max_source_channels
= 2;
2193 pa_hashmap_put(input_port
->profiles
, p
->name
, p
);
2195 d
= PA_CARD_PROFILE_DATA(p
);
2196 *d
= PA_BLUEZ4_PROFILE_A2DP_SOURCE
;
2197 } else if (pa_streq(uuid
, HSP_HS_UUID
) || pa_streq(uuid
, HFP_HS_UUID
)) {
2198 p
= pa_card_profile_new("hsp", _("Telephony Duplex (HSP/HFP)"), sizeof(pa_bluez4_profile_t
));
2202 p
->max_sink_channels
= 1;
2203 p
->max_source_channels
= 1;
2204 pa_hashmap_put(input_port
->profiles
, p
->name
, p
);
2205 pa_hashmap_put(output_port
->profiles
, p
->name
, p
);
2207 d
= PA_CARD_PROFILE_DATA(p
);
2208 *d
= PA_BLUEZ4_PROFILE_HSP
;
2209 } else if (pa_streq(uuid
, HFP_AG_UUID
)) {
2210 p
= pa_card_profile_new("hfgw", _("Handsfree Gateway"), sizeof(pa_bluez4_profile_t
));
2214 p
->max_sink_channels
= 1;
2215 p
->max_source_channels
= 1;
2216 pa_hashmap_put(input_port
->profiles
, p
->name
, p
);
2217 pa_hashmap_put(output_port
->profiles
, p
->name
, p
);
2219 d
= PA_CARD_PROFILE_DATA(p
);
2220 *d
= PA_BLUEZ4_PROFILE_HFGW
;
2224 pa_bluez4_transport
*t
;
2226 if ((t
= u
->device
->transports
[*d
]))
2227 p
->available
= transport_state_to_availability(t
->state
);
2233 /* Run from main thread */
2234 static int add_card(struct userdata
*u
) {
2235 pa_card_new_data data
;
2238 pa_bluez4_profile_t
*d
;
2239 pa_bluez4_form_factor_t ff
;
2241 const char *default_profile
;
2242 const pa_bluez4_device
*device
;
2243 const pa_bluez4_uuid
*uuid
;
2246 pa_assert(u
->device
);
2250 pa_card_new_data_init(&data
);
2251 data
.driver
= __FILE__
;
2252 data
.module
= u
->module
;
2254 n
= pa_bluez4_cleanup_name(device
->alias
);
2255 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, n
);
2257 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_STRING
, device
->address
);
2258 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_API
, "bluez");
2259 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_CLASS
, "sound");
2260 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_BUS
, "bluetooth");
2262 if ((ff
= pa_bluez4_get_form_factor(device
->class)) != PA_BLUEZ4_FORM_FACTOR_UNKNOWN
)
2263 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_FORM_FACTOR
, pa_bluez4_form_factor_to_string(ff
));
2265 pa_proplist_sets(data
.proplist
, "bluez.path", device
->path
);
2266 pa_proplist_setf(data
.proplist
, "bluez.class", "0x%06x", (unsigned) device
->class);
2267 pa_proplist_sets(data
.proplist
, "bluez.alias", device
->alias
);
2268 data
.name
= get_name("card", u
->modargs
, device
->address
, &b
);
2269 data
.namereg_fail
= b
;
2271 if (pa_modargs_get_proplist(u
->modargs
, "card_properties", data
.proplist
, PA_UPDATE_REPLACE
) < 0) {
2272 pa_log("Invalid properties");
2273 pa_card_new_data_done(&data
);
2277 create_card_ports(u
, data
.ports
);
2279 PA_LLIST_FOREACH(uuid
, device
->uuids
) {
2280 p
= create_card_profile(u
, uuid
->uuid
, data
.ports
);
2285 if (pa_hashmap_get(data
.profiles
, p
->name
)) {
2286 pa_card_profile_free(p
);
2290 pa_hashmap_put(data
.profiles
, p
->name
, p
);
2293 pa_assert(!pa_hashmap_isempty(data
.profiles
));
2295 p
= pa_card_profile_new("off", _("Off"), sizeof(pa_bluez4_profile_t
));
2296 p
->available
= PA_AVAILABLE_YES
;
2297 d
= PA_CARD_PROFILE_DATA(p
);
2298 *d
= PA_BLUEZ4_PROFILE_OFF
;
2299 pa_hashmap_put(data
.profiles
, p
->name
, p
);
2301 if ((default_profile
= pa_modargs_get_value(u
->modargs
, "profile", NULL
))) {
2302 if (pa_hashmap_get(data
.profiles
, default_profile
))
2303 pa_card_new_data_set_profile(&data
, default_profile
);
2305 pa_log_warn("Profile '%s' not valid or not supported by device.", default_profile
);
2308 u
->card
= pa_card_new(u
->core
, &data
);
2309 pa_card_new_data_done(&data
);
2312 pa_log("Failed to allocate card.");
2316 u
->card
->userdata
= u
;
2317 u
->card
->set_profile
= card_set_profile
;
2319 d
= PA_CARD_PROFILE_DATA(u
->card
->active_profile
);
2321 if (*d
!= PA_BLUEZ4_PROFILE_OFF
&& (!device
->transports
[*d
] ||
2322 device
->transports
[*d
]->state
== PA_BLUEZ4_TRANSPORT_STATE_DISCONNECTED
)) {
2323 pa_log_warn("Default profile not connected, selecting off profile");
2324 u
->card
->active_profile
= pa_hashmap_get(u
->card
->profiles
, "off");
2325 u
->card
->save_profile
= false;
2328 d
= PA_CARD_PROFILE_DATA(u
->card
->active_profile
);
2331 if (USE_SCO_OVER_PCM(u
))
2332 save_sco_volume_callbacks(u
);
2337 /* Run from main thread */
2338 static pa_bluez4_device
* find_device(struct userdata
*u
, const char *address
, const char *path
) {
2339 pa_bluez4_device
*d
= NULL
;
2343 if (!address
&& !path
) {
2344 pa_log_error("Failed to get device address/path from module arguments.");
2349 if (!(d
= pa_bluez4_discovery_get_by_path(u
->discovery
, path
))) {
2350 pa_log_error("%s is not a valid BlueZ audio device.", path
);
2354 if (address
&& !(pa_streq(d
->address
, address
))) {
2355 pa_log_error("Passed path %s address %s != %s don't match.", path
, d
->address
, address
);
2360 if (!(d
= pa_bluez4_discovery_get_by_address(u
->discovery
, address
))) {
2361 pa_log_error("%s is not known.", address
);
2367 u
->address
= pa_xstrdup(d
->address
);
2368 u
->path
= pa_xstrdup(d
->path
);
2374 /* Run from main thread */
2375 static pa_hook_result_t
uuid_added_cb(pa_bluez4_discovery
*y
, const struct pa_bluez4_hook_uuid_data
*data
,
2376 struct userdata
*u
) {
2380 pa_assert(data
->device
);
2381 pa_assert(data
->uuid
);
2384 if (data
->device
!= u
->device
)
2387 p
= create_card_profile(u
, data
->uuid
, u
->card
->ports
);
2392 if (pa_hashmap_get(u
->card
->profiles
, p
->name
)) {
2393 pa_card_profile_free(p
);
2397 pa_card_add_profile(u
->card
, p
);
2402 /* Run from main thread */
2403 static pa_hook_result_t
discovery_hook_cb(pa_bluez4_discovery
*y
, const pa_bluez4_device
*d
, struct userdata
*u
) {
2411 pa_log_debug("Device %s removed: unloading module", d
->path
);
2412 else if (!pa_bluez4_device_any_audio_connected(d
))
2413 pa_log_debug("Unloading module, because device %s doesn't have any audio profiles connected anymore.", d
->path
);
2417 pa_module_unload(u
->core
, u
->module
, true);
2422 int pa__init(pa_module
*m
) {
2426 const char *address
, *path
;
2427 pa_bluez4_device
*device
;
2431 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
2432 pa_log_error("Failed to parse module arguments");
2436 m
->userdata
= u
= pa_xnew0(struct userdata
, 1);
2440 u
->sample_spec
= m
->core
->default_sample_spec
;
2443 if (pa_modargs_get_value(ma
, "sco_sink", NULL
) &&
2444 !(u
->hsp
.sco_sink
= pa_namereg_get(m
->core
, pa_modargs_get_value(ma
, "sco_sink", NULL
), PA_NAMEREG_SINK
))) {
2445 pa_log("SCO sink not found");
2449 if (pa_modargs_get_value(ma
, "sco_source", NULL
) &&
2450 !(u
->hsp
.sco_source
= pa_namereg_get(m
->core
, pa_modargs_get_value(ma
, "sco_source", NULL
), PA_NAMEREG_SOURCE
))) {
2451 pa_log("SCO source not found");
2455 if (pa_modargs_get_sample_rate(ma
, &u
->sample_spec
.rate
) < 0) {
2456 pa_log_error("Failed to get rate from module arguments");
2460 u
->auto_connect
= true;
2461 if (pa_modargs_get_value_boolean(ma
, "auto_connect", &u
->auto_connect
)) {
2462 pa_log("Failed to parse auto_connect= argument");
2466 channels
= u
->sample_spec
.channels
;
2467 if (pa_modargs_get_value_u32(ma
, "channels", &channels
) < 0 ||
2468 !pa_channels_valid(channels
)) {
2469 pa_log_error("Failed to get channels from module arguments");
2472 u
->sample_spec
.channels
= (uint8_t) channels
;
2473 u
->requested_sample_spec
= u
->sample_spec
;
2475 address
= pa_modargs_get_value(ma
, "address", NULL
);
2476 path
= pa_modargs_get_value(ma
, "path", NULL
);
2478 if (!(u
->discovery
= pa_bluez4_discovery_get(m
->core
)))
2481 if (!(device
= find_device(u
, address
, path
)))
2487 pa_hook_connect(pa_bluez4_discovery_hook(u
->discovery
, PA_BLUEZ4_HOOK_DEVICE_CONNECTION_CHANGED
),
2488 PA_HOOK_NORMAL
, (pa_hook_cb_t
) discovery_hook_cb
, u
);
2490 u
->uuid_added_slot
=
2491 pa_hook_connect(pa_bluez4_discovery_hook(u
->discovery
, PA_BLUEZ4_HOOK_DEVICE_UUID_ADDED
),
2492 PA_HOOK_NORMAL
, (pa_hook_cb_t
) uuid_added_cb
, u
);
2494 u
->sink_state_changed_slot
=
2495 pa_hook_connect(&u
->core
->hooks
[PA_CORE_HOOK_SINK_STATE_CHANGED
],
2496 PA_HOOK_NORMAL
, (pa_hook_cb_t
) sink_state_changed_cb
, u
);
2498 u
->source_state_changed_slot
=
2499 pa_hook_connect(&u
->core
->hooks
[PA_CORE_HOOK_SOURCE_STATE_CHANGED
],
2500 PA_HOOK_NORMAL
, (pa_hook_cb_t
) source_state_changed_cb
, u
);
2502 u
->transport_state_changed_slot
=
2503 pa_hook_connect(pa_bluez4_discovery_hook(u
->discovery
, PA_BLUEZ4_HOOK_TRANSPORT_STATE_CHANGED
),
2504 PA_HOOK_NORMAL
, (pa_hook_cb_t
) transport_state_changed_cb
, u
);
2506 u
->transport_nrec_changed_slot
=
2507 pa_hook_connect(pa_bluez4_discovery_hook(u
->discovery
, PA_BLUEZ4_HOOK_TRANSPORT_NREC_CHANGED
),
2508 PA_HOOK_NORMAL
, (pa_hook_cb_t
) transport_nrec_changed_cb
, u
);
2510 u
->transport_microphone_changed_slot
=
2511 pa_hook_connect(pa_bluez4_discovery_hook(u
->discovery
, PA_BLUEZ4_HOOK_TRANSPORT_MICROPHONE_GAIN_CHANGED
),
2512 PA_HOOK_NORMAL
, (pa_hook_cb_t
) transport_microphone_gain_changed_cb
, u
);
2514 u
->transport_speaker_changed_slot
=
2515 pa_hook_connect(pa_bluez4_discovery_hook(u
->discovery
, PA_BLUEZ4_HOOK_TRANSPORT_SPEAKER_GAIN_CHANGED
),
2516 PA_HOOK_NORMAL
, (pa_hook_cb_t
) transport_speaker_gain_changed_cb
, u
);
2518 /* Add the card structure. This will also initialize the default profile */
2519 if (add_card(u
) < 0)
2522 if (!(u
->msg
= pa_msgobject_new(bluetooth_msg
)))
2525 u
->msg
->parent
.process_msg
= device_process_msg
;
2526 u
->msg
->card
= u
->card
;
2528 if (u
->profile
!= PA_BLUEZ4_PROFILE_OFF
)
2529 if (init_profile(u
) < 0)
2532 if (u
->sink
|| u
->source
)
2533 if (start_thread(u
) < 0)
2541 pa_assert_se(pa_card_set_profile(u
->card
, pa_hashmap_get(u
->card
->profiles
, "off"), false) >= 0);
2552 int pa__get_n_used(pa_module
*m
) {
2556 pa_assert_se(u
= m
->userdata
);
2559 (u
->sink
? pa_sink_linked_by(u
->sink
) : 0) +
2560 (u
->source
? pa_source_linked_by(u
->source
) : 0);
2563 void pa__done(pa_module
*m
) {
2568 if (!(u
= m
->userdata
))
2573 if (u
->discovery_slot
)
2574 pa_hook_slot_free(u
->discovery_slot
);
2576 if (u
->uuid_added_slot
)
2577 pa_hook_slot_free(u
->uuid_added_slot
);
2579 if (u
->sink_state_changed_slot
)
2580 pa_hook_slot_free(u
->sink_state_changed_slot
);
2582 if (u
->source_state_changed_slot
)
2583 pa_hook_slot_free(u
->source_state_changed_slot
);
2585 if (u
->transport_state_changed_slot
)
2586 pa_hook_slot_free(u
->transport_state_changed_slot
);
2588 if (u
->transport_nrec_changed_slot
)
2589 pa_hook_slot_free(u
->transport_nrec_changed_slot
);
2591 if (u
->transport_microphone_changed_slot
)
2592 pa_hook_slot_free(u
->transport_microphone_changed_slot
);
2594 if (u
->transport_speaker_changed_slot
)
2595 pa_hook_slot_free(u
->transport_speaker_changed_slot
);
2597 if (USE_SCO_OVER_PCM(u
))
2598 restore_sco_volume_callbacks(u
);
2604 pa_card_free(u
->card
);
2607 pa_xfree(u
->a2dp
.buffer
);
2609 sbc_finish(&u
->a2dp
.sbc
);
2612 pa_modargs_free(u
->modargs
);
2614 pa_xfree(u
->output_port_name
);
2615 pa_xfree(u
->input_port_name
);
2617 pa_xfree(u
->address
);
2621 pa_bluez4_discovery_unref(u
->discovery
);