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
29 #include <linux/sockios.h>
30 #include <arpa/inet.h>
32 #include <pulse/rtclock.h>
33 #include <pulse/sample.h>
34 #include <pulse/timeval.h>
35 #include <pulse/xmalloc.h>
37 #include <pulsecore/i18n.h>
38 #include <pulsecore/module.h>
39 #include <pulsecore/modargs.h>
40 #include <pulsecore/core-rtclock.h>
41 #include <pulsecore/core-util.h>
42 #include <pulsecore/core-error.h>
43 #include <pulsecore/shared.h>
44 #include <pulsecore/socket-util.h>
45 #include <pulsecore/thread.h>
46 #include <pulsecore/thread-mq.h>
47 #include <pulsecore/poll.h>
48 #include <pulsecore/rtpoll.h>
49 #include <pulsecore/time-smoother.h>
50 #include <pulsecore/namereg.h>
51 #include <pulsecore/dbus-shared.h>
53 #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
62 #define HSP_MAX_GAIN 15
64 PA_MODULE_AUTHOR("Joao Paulo Rechi Vita");
65 PA_MODULE_DESCRIPTION("Bluetooth audio sink and source");
66 PA_MODULE_VERSION(PACKAGE_VERSION
);
67 PA_MODULE_LOAD_ONCE(FALSE
);
69 "name=<name for the card/sink/source, to be prefixed> "
70 "card_name=<name for the card> "
71 "card_properties=<properties for the card> "
72 "sink_name=<name for the sink> "
73 "sink_properties=<properties for the sink> "
74 "source_name=<name for the source> "
75 "source_properties=<properties for the source> "
76 "address=<address of the device> "
77 "profile=<a2dp|hsp|hfgw> "
79 "channels=<number of channels> "
80 "path=<device object path> "
81 "auto_connect=<automatically connect?> "
82 "sco_sink=<SCO over PCM sink name> "
83 "sco_source=<SCO over PCM source name>");
85 /* TODO: not close fd when entering suspend mode in a2dp */
87 static const char* const valid_modargs
[] = {
107 sbc_capabilities_t sbc_capabilities
;
108 sbc_t sbc
; /* Codec data */
109 pa_bool_t sbc_initialized
; /* Keep track if the encoder is initialized */
110 size_t codesize
, frame_length
; /* SBC Codesize, frame_length. We simply cache those values here */
112 void* buffer
; /* Codec transfer buffer */
113 size_t buffer_size
; /* Size of the buffer */
115 uint16_t seq_num
; /* Cumulative packet sequence */
121 pcm_capabilities_t pcm_capabilities
;
123 void (*sco_sink_set_volume
)(pa_sink
*s
);
124 pa_source
*sco_source
;
125 void (*sco_source_set_volume
)(pa_source
*s
);
126 pa_hook_slot
*sink_state_changed_slot
;
127 pa_hook_slot
*source_state_changed_slot
;
128 pa_hook_slot
*nrec_changed_slot
;
131 struct bluetooth_msg
{
136 typedef struct bluetooth_msg bluetooth_msg
;
137 PA_DEFINE_PRIVATE_CLASS(bluetooth_msg
, pa_msgobject
);
138 #define BLUETOOTH_MSG(o) (bluetooth_msg_cast(o))
149 pa_bluetooth_discovery
*discovery
;
150 pa_bool_t auto_connect
;
152 pa_dbus_connection
*connection
;
158 pa_thread_mq thread_mq
;
160 pa_rtpoll_item
*rtpoll_item
;
164 uint64_t read_index
, write_index
;
165 pa_usec_t started_at
;
166 pa_smoother
*read_smoother
;
168 pa_memchunk write_memchunk
;
170 pa_sample_spec sample_spec
, requested_sample_spec
;
177 struct a2dp_info a2dp
;
180 enum profile profile
;
184 int stream_write_type
;
186 pa_bool_t filter_added
;
190 BLUETOOTH_MESSAGE_IO_THREAD_FAILED
,
191 BLUETOOTH_MESSAGE_MAX
194 #define FIXED_LATENCY_PLAYBACK_A2DP (25*PA_USEC_PER_MSEC)
195 #define FIXED_LATENCY_RECORD_A2DP (25*PA_USEC_PER_MSEC)
196 #define FIXED_LATENCY_PLAYBACK_HSP (125*PA_USEC_PER_MSEC)
197 #define FIXED_LATENCY_RECORD_HSP (25*PA_USEC_PER_MSEC)
199 #define MAX_PLAYBACK_CATCH_UP_USEC (100*PA_USEC_PER_MSEC)
201 #define USE_SCO_OVER_PCM(u) (u->profile == PROFILE_HSP && (u->hsp.sco_sink && u->hsp.sco_source))
203 static int init_profile(struct userdata
*u
);
206 static void a2dp_set_bitpool(struct userdata
*u
, uint8_t bitpool
)
208 struct a2dp_info
*a2dp
;
214 if (a2dp
->sbc
.bitpool
== bitpool
)
217 if (bitpool
> a2dp
->max_bitpool
)
218 bitpool
= a2dp
->max_bitpool
;
219 else if (bitpool
< a2dp
->min_bitpool
)
220 bitpool
= a2dp
->min_bitpool
;
222 a2dp
->sbc
.bitpool
= bitpool
;
224 a2dp
->codesize
= sbc_get_codesize(&a2dp
->sbc
);
225 a2dp
->frame_length
= sbc_get_frame_length(&a2dp
->sbc
);
227 pa_log_debug("Bitpool has changed to %u", a2dp
->sbc
.bitpool
);
230 (u
->link_mtu
- sizeof(struct rtp_header
) - sizeof(struct rtp_payload
))
231 / a2dp
->frame_length
* a2dp
->codesize
;
233 pa_sink_set_max_request_within_thread(u
->sink
, u
->block_size
);
234 pa_sink_set_fixed_latency_within_thread(u
->sink
,
235 FIXED_LATENCY_PLAYBACK_A2DP
+ pa_bytes_to_usec(u
->block_size
, &u
->sample_spec
));
238 /* from IO thread, except in SCO over PCM */
240 static int setup_stream(struct userdata
*u
) {
241 struct pollfd
*pollfd
;
244 pa_make_fd_nonblock(u
->stream_fd
);
245 pa_make_socket_low_delay(u
->stream_fd
);
248 if (setsockopt(u
->stream_fd
, SOL_SOCKET
, SO_TIMESTAMP
, &one
, sizeof(one
)) < 0)
249 pa_log_warn("Failed to enable SO_TIMESTAMP: %s", pa_cstrerror(errno
));
251 pa_log_debug("Stream properly set up, we're ready to roll!");
253 if (u
->profile
== PROFILE_A2DP
)
254 a2dp_set_bitpool(u
, u
->a2dp
.max_bitpool
);
256 u
->rtpoll_item
= pa_rtpoll_item_new(u
->rtpoll
, PA_RTPOLL_NEVER
, 1);
257 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
258 pollfd
->fd
= u
->stream_fd
;
259 pollfd
->events
= pollfd
->revents
= 0;
261 u
->read_index
= u
->write_index
= 0;
265 u
->read_smoother
= pa_smoother_new(
277 static void bt_transport_release(struct userdata
*u
) {
278 const char *accesstype
= "rw";
279 const pa_bluetooth_transport
*t
;
281 /* Ignore if already released */
285 pa_log_debug("Releasing transport %s", u
->transport
);
287 t
= pa_bluetooth_discovery_get_transport(u
->discovery
, u
->transport
);
289 pa_bluetooth_transport_release(t
, accesstype
);
291 pa_xfree(u
->accesstype
);
292 u
->accesstype
= NULL
;
294 if (u
->rtpoll_item
) {
295 pa_rtpoll_item_free(u
->rtpoll_item
);
296 u
->rtpoll_item
= NULL
;
299 if (u
->stream_fd
>= 0) {
300 pa_close(u
->stream_fd
);
304 if (u
->read_smoother
) {
305 pa_smoother_free(u
->read_smoother
);
306 u
->read_smoother
= NULL
;
310 static int bt_transport_acquire(struct userdata
*u
, pa_bool_t start
) {
311 const char *accesstype
= "rw";
312 const pa_bluetooth_transport
*t
;
320 pa_log_debug("Acquiring transport %s", u
->transport
);
322 t
= pa_bluetooth_discovery_get_transport(u
->discovery
, u
->transport
);
324 pa_log("Transport %s no longer available", u
->transport
);
325 pa_xfree(u
->transport
);
330 /* FIXME: Handle in/out MTU properly when unix socket is not longer supported */
331 u
->stream_fd
= pa_bluetooth_transport_acquire(t
, accesstype
, NULL
, &u
->link_mtu
);
332 if (u
->stream_fd
< 0)
335 u
->accesstype
= pa_xstrdup(accesstype
);
336 pa_log_info("Transport %s acquired: fd %d", u
->transport
, u
->stream_fd
);
342 pa_log_info("Transport %s resuming", u
->transport
);
343 return setup_stream(u
);
346 /* Run from IO thread */
347 static int sink_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
348 struct userdata
*u
= PA_SINK(o
)->userdata
;
349 pa_bool_t failed
= FALSE
;
352 pa_assert(u
->sink
== PA_SINK(o
));
353 pa_assert(u
->transport
);
357 case PA_SINK_MESSAGE_SET_STATE
:
359 switch ((pa_sink_state_t
) PA_PTR_TO_UINT(data
)) {
361 case PA_SINK_SUSPENDED
:
362 pa_assert(PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
));
364 /* Stop the device if the source is suspended as well */
365 if (!u
->source
|| u
->source
->state
== PA_SOURCE_SUSPENDED
)
366 /* We deliberately ignore whether stopping
367 * actually worked. Since the stream_fd is
368 * closed it doesn't really matter */
369 bt_transport_release(u
);
374 case PA_SINK_RUNNING
:
375 if (u
->sink
->thread_info
.state
!= PA_SINK_SUSPENDED
)
378 /* Resume the device if the source was suspended as well */
379 if (!u
->source
|| u
->source
->state
== PA_SOURCE_SUSPENDED
) {
380 if (bt_transport_acquire(u
, TRUE
) < 0)
385 case PA_SINK_UNLINKED
:
387 case PA_SINK_INVALID_STATE
:
392 case PA_SINK_MESSAGE_GET_LATENCY
: {
394 if (u
->read_smoother
) {
397 ri
= pa_smoother_get(u
->read_smoother
, pa_rtclock_now());
398 wi
= pa_bytes_to_usec(u
->write_index
+ u
->block_size
, &u
->sample_spec
);
400 *((pa_usec_t
*) data
) = wi
> ri
? wi
- ri
: 0;
404 ri
= pa_rtclock_now() - u
->started_at
;
405 wi
= pa_bytes_to_usec(u
->write_index
, &u
->sample_spec
);
407 *((pa_usec_t
*) data
) = wi
> ri
? wi
- ri
: 0;
410 *((pa_usec_t
*) data
) += u
->sink
->thread_info
.fixed_latency
;
415 r
= pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
417 return (r
< 0 || !failed
) ? r
: -1;
420 /* Run from IO thread */
421 static int source_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
422 struct userdata
*u
= PA_SOURCE(o
)->userdata
;
423 pa_bool_t failed
= FALSE
;
426 pa_assert(u
->source
== PA_SOURCE(o
));
427 pa_assert(u
->transport
);
431 case PA_SOURCE_MESSAGE_SET_STATE
:
433 switch ((pa_source_state_t
) PA_PTR_TO_UINT(data
)) {
435 case PA_SOURCE_SUSPENDED
:
436 pa_assert(PA_SOURCE_IS_OPENED(u
->source
->thread_info
.state
));
438 /* Stop the device if the sink is suspended as well */
439 if (!u
->sink
|| u
->sink
->state
== PA_SINK_SUSPENDED
)
440 bt_transport_release(u
);
442 if (u
->read_smoother
)
443 pa_smoother_pause(u
->read_smoother
, pa_rtclock_now());
447 case PA_SOURCE_RUNNING
:
448 if (u
->source
->thread_info
.state
!= PA_SOURCE_SUSPENDED
)
451 /* Resume the device if the sink was suspended as well */
452 if (!u
->sink
|| u
->sink
->thread_info
.state
== PA_SINK_SUSPENDED
) {
453 if (bt_transport_acquire(u
, TRUE
) < 0)
456 /* We don't resume the smoother here. Instead we
457 * wait until the first packet arrives */
460 case PA_SOURCE_UNLINKED
:
462 case PA_SOURCE_INVALID_STATE
:
467 case PA_SOURCE_MESSAGE_GET_LATENCY
: {
470 if (u
->read_smoother
) {
471 wi
= pa_smoother_get(u
->read_smoother
, pa_rtclock_now());
472 ri
= pa_bytes_to_usec(u
->read_index
, &u
->sample_spec
);
474 *((pa_usec_t
*) data
) = (wi
> ri
? wi
- ri
: 0) + u
->source
->thread_info
.fixed_latency
;
476 *((pa_usec_t
*) data
) = 0;
483 r
= pa_source_process_msg(o
, code
, data
, offset
, chunk
);
485 return (r
< 0 || !failed
) ? r
: -1;
488 /* Called from main thread context */
489 static int device_process_msg(pa_msgobject
*obj
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
490 struct bluetooth_msg
*u
= BLUETOOTH_MSG(obj
);
493 case BLUETOOTH_MESSAGE_IO_THREAD_FAILED
: {
494 if (u
->card
->module
->unload_requested
)
497 pa_log_debug("Switching the profile to off due to IO thread failure.");
499 if (pa_card_set_profile(u
->card
, "off", FALSE
) < 0)
500 pa_log_debug("Failed to switch profile to off");
507 /* Run from IO thread */
508 static int hsp_process_render(struct userdata
*u
) {
512 pa_assert(u
->profile
== PROFILE_HSP
|| u
->profile
== PROFILE_HFGW
);
515 /* First, render some data */
516 if (!u
->write_memchunk
.memblock
)
517 pa_sink_render_full(u
->sink
, u
->block_size
, &u
->write_memchunk
);
519 pa_assert(u
->write_memchunk
.length
== u
->block_size
);
525 /* Now write that data to the socket. The socket is of type
526 * SEQPACKET, and we generated the data of the MTU size, so this
527 * should just work. */
529 p
= (const uint8_t*) pa_memblock_acquire(u
->write_memchunk
.memblock
) + u
->write_memchunk
.index
;
530 l
= pa_write(u
->stream_fd
, p
, u
->write_memchunk
.length
, &u
->stream_write_type
);
531 pa_memblock_release(u
->write_memchunk
.memblock
);
538 /* Retry right away if we got interrupted */
541 else if (errno
== EAGAIN
)
542 /* Hmm, apparently the socket was not writable, give up for now */
545 pa_log_error("Failed to write data to SCO socket: %s", pa_cstrerror(errno
));
550 pa_assert((size_t) l
<= u
->write_memchunk
.length
);
552 if ((size_t) l
!= u
->write_memchunk
.length
) {
553 pa_log_error("Wrote memory block to socket only partially! %llu written, wanted to write %llu.",
554 (unsigned long long) l
,
555 (unsigned long long) u
->write_memchunk
.length
);
560 u
->write_index
+= (uint64_t) u
->write_memchunk
.length
;
561 pa_memblock_unref(u
->write_memchunk
.memblock
);
562 pa_memchunk_reset(&u
->write_memchunk
);
571 /* Run from IO thread */
572 static int hsp_process_push(struct userdata
*u
) {
574 pa_memchunk memchunk
;
577 pa_assert(u
->profile
== PROFILE_HSP
|| u
->profile
== PROFILE_HFGW
);
578 pa_assert(u
->source
);
579 pa_assert(u
->read_smoother
);
581 memchunk
.memblock
= pa_memblock_new(u
->core
->mempool
, u
->block_size
);
582 memchunk
.index
= memchunk
.length
= 0;
591 pa_bool_t found_tstamp
= FALSE
;
594 memset(&m
, 0, sizeof(m
));
595 memset(&aux
, 0, sizeof(aux
));
596 memset(&iov
, 0, sizeof(iov
));
601 m
.msg_controllen
= sizeof(aux
);
603 p
= pa_memblock_acquire(memchunk
.memblock
);
605 iov
.iov_len
= pa_memblock_get_length(memchunk
.memblock
);
606 l
= recvmsg(u
->stream_fd
, &m
, 0);
607 pa_memblock_release(memchunk
.memblock
);
611 if (l
< 0 && errno
== EINTR
)
612 /* Retry right away if we got interrupted */
615 else if (l
< 0 && errno
== EAGAIN
)
616 /* Hmm, apparently the socket was not readable, give up for now. */
619 pa_log_error("Failed to read data from SCO socket: %s", l
< 0 ? pa_cstrerror(errno
) : "EOF");
624 pa_assert((size_t) l
<= pa_memblock_get_length(memchunk
.memblock
));
626 memchunk
.length
= (size_t) l
;
627 u
->read_index
+= (uint64_t) l
;
629 for (cm
= CMSG_FIRSTHDR(&m
); cm
; cm
= CMSG_NXTHDR(&m
, cm
))
630 if (cm
->cmsg_level
== SOL_SOCKET
&& cm
->cmsg_type
== SO_TIMESTAMP
) {
631 struct timeval
*tv
= (struct timeval
*) CMSG_DATA(cm
);
632 pa_rtclock_from_wallclock(tv
);
633 tstamp
= pa_timeval_load(tv
);
639 pa_log_warn("Couldn't find SO_TIMESTAMP data in auxiliary recvmsg() data!");
640 tstamp
= pa_rtclock_now();
643 pa_smoother_put(u
->read_smoother
, tstamp
, pa_bytes_to_usec(u
->read_index
, &u
->sample_spec
));
644 pa_smoother_resume(u
->read_smoother
, tstamp
, TRUE
);
646 pa_source_post(u
->source
, &memchunk
);
652 pa_memblock_unref(memchunk
.memblock
);
657 /* Run from IO thread */
658 static void a2dp_prepare_buffer(struct userdata
*u
) {
661 if (u
->a2dp
.buffer_size
>= u
->link_mtu
)
664 u
->a2dp
.buffer_size
= 2 * u
->link_mtu
;
665 pa_xfree(u
->a2dp
.buffer
);
666 u
->a2dp
.buffer
= pa_xmalloc(u
->a2dp
.buffer_size
);
669 /* Run from IO thread */
670 static int a2dp_process_render(struct userdata
*u
) {
671 struct a2dp_info
*a2dp
;
672 struct rtp_header
*header
;
673 struct rtp_payload
*payload
;
677 size_t to_write
, to_encode
;
678 unsigned frame_count
;
682 pa_assert(u
->profile
== PROFILE_A2DP
);
685 /* First, render some data */
686 if (!u
->write_memchunk
.memblock
)
687 pa_sink_render_full(u
->sink
, u
->block_size
, &u
->write_memchunk
);
689 pa_assert(u
->write_memchunk
.length
== u
->block_size
);
691 a2dp_prepare_buffer(u
);
694 header
= a2dp
->buffer
;
695 payload
= (struct rtp_payload
*) ((uint8_t*) a2dp
->buffer
+ sizeof(*header
));
699 /* Try to create a packet of the full MTU */
701 p
= (const uint8_t*) pa_memblock_acquire(u
->write_memchunk
.memblock
) + u
->write_memchunk
.index
;
702 to_encode
= u
->write_memchunk
.length
;
704 d
= (uint8_t*) a2dp
->buffer
+ sizeof(*header
) + sizeof(*payload
);
705 to_write
= a2dp
->buffer_size
- sizeof(*header
) - sizeof(*payload
);
707 while (PA_LIKELY(to_encode
> 0 && to_write
> 0)) {
711 encoded
= sbc_encode(&a2dp
->sbc
,
716 if (PA_UNLIKELY(encoded
<= 0)) {
717 pa_log_error("SBC encoding error (%li)", (long) encoded
);
718 pa_memblock_release(u
->write_memchunk
.memblock
);
722 /* pa_log_debug("SBC: encoded: %lu; written: %lu", (unsigned long) encoded, (unsigned long) written); */
723 /* pa_log_debug("SBC: codesize: %lu; frame_length: %lu", (unsigned long) a2dp->codesize, (unsigned long) a2dp->frame_length); */
725 pa_assert_fp((size_t) encoded
<= to_encode
);
726 pa_assert_fp((size_t) encoded
== a2dp
->codesize
);
728 pa_assert_fp((size_t) written
<= to_write
);
729 pa_assert_fp((size_t) written
== a2dp
->frame_length
);
731 p
= (const uint8_t*) p
+ encoded
;
732 to_encode
-= encoded
;
734 d
= (uint8_t*) d
+ written
;
740 pa_memblock_release(u
->write_memchunk
.memblock
);
742 pa_assert(to_encode
== 0);
745 pa_log_debug("Using SBC encoder implementation: %s", pa_strnull(sbc_get_implementation_info(&a2dp
->sbc
)));
748 /* write it to the fifo */
749 memset(a2dp
->buffer
, 0, sizeof(*header
) + sizeof(*payload
));
752 header
->sequence_number
= htons(a2dp
->seq_num
++);
753 header
->timestamp
= htonl(u
->write_index
/ pa_frame_size(&u
->sample_spec
));
754 header
->ssrc
= htonl(1);
755 payload
->frame_count
= frame_count
;
757 nbytes
= (uint8_t*) d
- (uint8_t*) a2dp
->buffer
;
762 l
= pa_write(u
->stream_fd
, a2dp
->buffer
, nbytes
, &u
->stream_write_type
);
769 /* Retry right away if we got interrupted */
772 else if (errno
== EAGAIN
)
773 /* Hmm, apparently the socket was not writable, give up for now */
776 pa_log_error("Failed to write data to socket: %s", pa_cstrerror(errno
));
781 pa_assert((size_t) l
<= nbytes
);
783 if ((size_t) l
!= nbytes
) {
784 pa_log_warn("Wrote memory block to socket only partially! %llu written, wanted to write %llu.",
785 (unsigned long long) l
,
786 (unsigned long long) nbytes
);
791 u
->write_index
+= (uint64_t) u
->write_memchunk
.length
;
792 pa_memblock_unref(u
->write_memchunk
.memblock
);
793 pa_memchunk_reset(&u
->write_memchunk
);
803 static int a2dp_process_push(struct userdata
*u
) {
805 pa_memchunk memchunk
;
808 pa_assert(u
->profile
== PROFILE_A2DP_SOURCE
);
809 pa_assert(u
->source
);
810 pa_assert(u
->read_smoother
);
812 memchunk
.memblock
= pa_memblock_new(u
->core
->mempool
, u
->block_size
);
813 memchunk
.index
= memchunk
.length
= 0;
816 pa_bool_t found_tstamp
= FALSE
;
818 struct a2dp_info
*a2dp
;
819 struct rtp_header
*header
;
820 struct rtp_payload
*payload
;
824 size_t to_write
, to_decode
;
826 a2dp_prepare_buffer(u
);
829 header
= a2dp
->buffer
;
830 payload
= (struct rtp_payload
*) ((uint8_t*) a2dp
->buffer
+ sizeof(*header
));
832 l
= pa_read(u
->stream_fd
, a2dp
->buffer
, a2dp
->buffer_size
, &u
->stream_write_type
);
836 if (l
< 0 && errno
== EINTR
)
837 /* Retry right away if we got interrupted */
840 else if (l
< 0 && errno
== EAGAIN
)
841 /* Hmm, apparently the socket was not readable, give up for now. */
844 pa_log_error("Failed to read data from socket: %s", l
< 0 ? pa_cstrerror(errno
) : "EOF");
849 pa_assert((size_t) l
<= a2dp
->buffer_size
);
851 u
->read_index
+= (uint64_t) l
;
853 /* TODO: get timestamp from rtp */
855 /* pa_log_warn("Couldn't find SO_TIMESTAMP data in auxiliary recvmsg() data!"); */
856 tstamp
= pa_rtclock_now();
859 pa_smoother_put(u
->read_smoother
, tstamp
, pa_bytes_to_usec(u
->read_index
, &u
->sample_spec
));
860 pa_smoother_resume(u
->read_smoother
, tstamp
, TRUE
);
862 p
= (uint8_t*) a2dp
->buffer
+ sizeof(*header
) + sizeof(*payload
);
863 to_decode
= l
- sizeof(*header
) - sizeof(*payload
);
865 d
= pa_memblock_acquire(memchunk
.memblock
);
866 to_write
= memchunk
.length
= pa_memblock_get_length(memchunk
.memblock
);
868 while (PA_LIKELY(to_decode
> 0)) {
872 decoded
= sbc_decode(&a2dp
->sbc
,
877 if (PA_UNLIKELY(decoded
<= 0)) {
878 pa_log_error("SBC decoding error (%li)", (long) decoded
);
879 pa_memblock_release(memchunk
.memblock
);
880 pa_memblock_unref(memchunk
.memblock
);
884 /* pa_log_debug("SBC: decoded: %lu; written: %lu", (unsigned long) decoded, (unsigned long) written); */
885 /* pa_log_debug("SBC: frame_length: %lu; codesize: %lu", (unsigned long) a2dp->frame_length, (unsigned long) a2dp->codesize); */
887 /* Reset frame length, it can be changed due to bitpool change */
888 a2dp
->frame_length
= sbc_get_frame_length(&a2dp
->sbc
);
890 pa_assert_fp((size_t) decoded
<= to_decode
);
891 pa_assert_fp((size_t) decoded
== a2dp
->frame_length
);
893 pa_assert_fp((size_t) written
== a2dp
->codesize
);
895 p
= (const uint8_t*) p
+ decoded
;
896 to_decode
-= decoded
;
898 d
= (uint8_t*) d
+ written
;
902 memchunk
.length
-= to_write
;
904 pa_memblock_release(memchunk
.memblock
);
906 pa_source_post(u
->source
, &memchunk
);
912 pa_memblock_unref(memchunk
.memblock
);
917 static void a2dp_reduce_bitpool(struct userdata
*u
)
919 struct a2dp_info
*a2dp
;
926 /* Check if bitpool is already at its limit */
927 if (a2dp
->sbc
.bitpool
<= BITPOOL_DEC_LIMIT
)
930 bitpool
= a2dp
->sbc
.bitpool
- BITPOOL_DEC_STEP
;
932 if (bitpool
< BITPOOL_DEC_LIMIT
)
933 bitpool
= BITPOOL_DEC_LIMIT
;
935 a2dp_set_bitpool(u
, bitpool
);
938 static void thread_func(void *userdata
) {
939 struct userdata
*u
= userdata
;
940 unsigned do_write
= 0;
941 pa_bool_t writable
= FALSE
;
944 pa_assert(u
->transport
);
946 pa_log_debug("IO Thread starting up");
948 if (u
->core
->realtime_scheduling
)
949 pa_make_realtime(u
->core
->realtime_priority
);
951 pa_thread_mq_install(&u
->thread_mq
);
953 if (bt_transport_acquire(u
, TRUE
) < 0)
957 struct pollfd
*pollfd
;
959 pa_bool_t disable_timer
= TRUE
;
961 pollfd
= u
->rtpoll_item
? pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
) : NULL
;
963 if (u
->source
&& PA_SOURCE_IS_LINKED(u
->source
->thread_info
.state
)) {
965 /* We should send two blocks to the device before we expect
968 if (u
->write_index
== 0 && u
->read_index
<= 0)
971 if (pollfd
&& (pollfd
->revents
& POLLIN
)) {
974 if (u
->profile
== PROFILE_HSP
|| u
->profile
== PROFILE_HFGW
)
975 n_read
= hsp_process_push(u
);
977 n_read
= a2dp_process_push(u
);
982 /* We just read something, so we are supposed to write something, too */
987 if (u
->sink
&& PA_SINK_IS_LINKED(u
->sink
->thread_info
.state
)) {
989 if (u
->sink
->thread_info
.rewind_requested
)
990 pa_sink_process_rewind(u
->sink
, 0);
993 if (pollfd
->revents
& POLLOUT
)
996 if ((!u
->source
|| !PA_SOURCE_IS_LINKED(u
->source
->thread_info
.state
)) && do_write
<= 0 && writable
) {
997 pa_usec_t time_passed
;
998 pa_usec_t audio_sent
;
1000 /* Hmm, there is no input stream we could synchronize
1001 * to. So let's do things by time */
1003 time_passed
= pa_rtclock_now() - u
->started_at
;
1004 audio_sent
= pa_bytes_to_usec(u
->write_index
, &u
->sample_spec
);
1006 if (audio_sent
<= time_passed
) {
1007 pa_usec_t audio_to_send
= time_passed
- audio_sent
;
1009 /* Never try to catch up for more than 100ms */
1010 if (u
->write_index
> 0 && audio_to_send
> MAX_PLAYBACK_CATCH_UP_USEC
) {
1011 pa_usec_t skip_usec
;
1012 uint64_t skip_bytes
;
1014 skip_usec
= audio_to_send
- MAX_PLAYBACK_CATCH_UP_USEC
;
1015 skip_bytes
= pa_usec_to_bytes(skip_usec
, &u
->sample_spec
);
1017 if (skip_bytes
> 0) {
1020 pa_log_warn("Skipping %llu us (= %llu bytes) in audio stream",
1021 (unsigned long long) skip_usec
,
1022 (unsigned long long) skip_bytes
);
1024 pa_sink_render_full(u
->sink
, skip_bytes
, &tmp
);
1025 pa_memblock_unref(tmp
.memblock
);
1026 u
->write_index
+= skip_bytes
;
1028 if (u
->profile
== PROFILE_A2DP
)
1029 a2dp_reduce_bitpool(u
);
1037 if (writable
&& do_write
> 0) {
1040 if (u
->write_index
<= 0)
1041 u
->started_at
= pa_rtclock_now();
1043 if (u
->profile
== PROFILE_A2DP
) {
1044 if ((n_written
= a2dp_process_render(u
)) < 0)
1047 if ((n_written
= hsp_process_render(u
)) < 0)
1052 pa_log("Broken kernel: we got EAGAIN on write() after POLLOUT!");
1054 do_write
-= n_written
;
1058 if ((!u
->source
|| !PA_SOURCE_IS_LINKED(u
->source
->thread_info
.state
)) && do_write
<= 0) {
1059 pa_usec_t sleep_for
;
1060 pa_usec_t time_passed
, next_write_at
;
1063 /* Hmm, there is no input stream we could synchronize
1064 * to. So let's estimate when we need to wake up the latest */
1065 time_passed
= pa_rtclock_now() - u
->started_at
;
1066 next_write_at
= pa_bytes_to_usec(u
->write_index
, &u
->sample_spec
);
1067 sleep_for
= time_passed
< next_write_at
? next_write_at
- time_passed
: 0;
1068 /* 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); */
1070 /* drop stream every 500 ms */
1071 sleep_for
= PA_USEC_PER_MSEC
* 500;
1073 pa_rtpoll_set_timer_relative(u
->rtpoll
, sleep_for
);
1074 disable_timer
= FALSE
;
1080 pa_rtpoll_set_timer_disabled(u
->rtpoll
);
1082 /* Hmm, nothing to do. Let's sleep */
1084 pollfd
->events
= (short) (((u
->sink
&& PA_SINK_IS_LINKED(u
->sink
->thread_info
.state
) && !writable
) ? POLLOUT
: 0) |
1085 (u
->source
&& PA_SOURCE_IS_LINKED(u
->source
->thread_info
.state
) ? POLLIN
: 0));
1087 if ((ret
= pa_rtpoll_run(u
->rtpoll
, TRUE
)) < 0) {
1088 pa_log_debug("pa_rtpoll_run failed with: %d", ret
);
1092 pa_log_debug("IO thread shutdown requested, stopping cleanly");
1093 bt_transport_release(u
);
1097 pollfd
= u
->rtpoll_item
? pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
) : NULL
;
1099 if (pollfd
&& (pollfd
->revents
& ~(POLLOUT
|POLLIN
))) {
1100 pa_log_info("FD error: %s%s%s%s",
1101 pollfd
->revents
& POLLERR
? "POLLERR " :"",
1102 pollfd
->revents
& POLLHUP
? "POLLHUP " :"",
1103 pollfd
->revents
& POLLPRI
? "POLLPRI " :"",
1104 pollfd
->revents
& POLLNVAL
? "POLLNVAL " :"");
1110 /* If this was no regular exit from the loop we have to continue processing messages until we receive PA_MESSAGE_SHUTDOWN */
1111 pa_log_debug("IO thread failed");
1112 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(u
->msg
), BLUETOOTH_MESSAGE_IO_THREAD_FAILED
, NULL
, 0, NULL
, NULL
);
1113 pa_asyncmsgq_wait_for(u
->thread_mq
.inq
, PA_MESSAGE_SHUTDOWN
);
1116 pa_log_debug("IO thread shutting down");
1119 /* Run from main thread */
1120 static DBusHandlerResult
filter_cb(DBusConnection
*bus
, DBusMessage
*m
, void *userdata
) {
1126 pa_assert_se(u
= userdata
);
1128 dbus_error_init(&err
);
1130 pa_log_debug("dbus: interface=%s, path=%s, member=%s\n",
1131 dbus_message_get_interface(m
),
1132 dbus_message_get_path(m
),
1133 dbus_message_get_member(m
));
1135 if (!dbus_message_has_path(m
, u
->path
) && !dbus_message_has_path(m
, u
->transport
))
1138 if (dbus_message_is_signal(m
, "org.bluez.Headset", "SpeakerGainChanged") ||
1139 dbus_message_is_signal(m
, "org.bluez.Headset", "MicrophoneGainChanged")) {
1144 if (!dbus_message_get_args(m
, &err
, DBUS_TYPE_UINT16
, &gain
, DBUS_TYPE_INVALID
) || gain
> HSP_MAX_GAIN
) {
1145 pa_log("Failed to parse org.bluez.Headset.{Speaker|Microphone}GainChanged: %s", err
.message
);
1149 if (u
->profile
== PROFILE_HSP
) {
1150 if (u
->sink
&& dbus_message_is_signal(m
, "org.bluez.Headset", "SpeakerGainChanged")) {
1151 pa_volume_t volume
= (pa_volume_t
) (gain
* PA_VOLUME_NORM
/ HSP_MAX_GAIN
);
1153 /* increment volume by one to correct rounding errors */
1154 if (volume
< PA_VOLUME_NORM
)
1157 pa_cvolume_set(&v
, u
->sample_spec
.channels
, volume
);
1158 pa_sink_volume_changed(u
->sink
, &v
);
1160 } else if (u
->source
&& dbus_message_is_signal(m
, "org.bluez.Headset", "MicrophoneGainChanged")) {
1161 pa_volume_t volume
= (pa_volume_t
) (gain
* PA_VOLUME_NORM
/ HSP_MAX_GAIN
);
1163 /* increment volume by one to correct rounding errors */
1164 if (volume
< PA_VOLUME_NORM
)
1167 pa_cvolume_set(&v
, u
->sample_spec
.channels
, volume
);
1168 pa_source_volume_changed(u
->source
, &v
);
1171 } else if (dbus_message_is_signal(m
, "org.bluez.HandsfreeGateway", "PropertyChanged")) {
1173 DBusMessageIter iter
;
1174 DBusMessageIter variant
;
1175 pa_bt_audio_state_t state
= PA_BT_AUDIO_STATE_INVALID
;
1177 if (!dbus_message_iter_init(m
, &iter
)) {
1178 pa_log("Failed to parse PropertyChanged: %s", err
.message
);
1182 if (dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_STRING
) {
1183 pa_log("Property name not a string.");
1187 dbus_message_iter_get_basic(&iter
, &key
);
1189 if (!dbus_message_iter_next(&iter
)) {
1190 pa_log("Property value missing");
1194 dbus_message_iter_recurse(&iter
, &variant
);
1196 if (dbus_message_iter_get_arg_type(&variant
) == DBUS_TYPE_STRING
) {
1198 dbus_message_iter_get_basic(&variant
, &value
);
1200 if (pa_streq(key
, "State")) {
1201 pa_log_debug("dbus: HSHFAG property 'State' changed to value '%s'", value
);
1202 state
= pa_bt_audio_state_from_string(value
);
1207 case PA_BT_AUDIO_STATE_INVALID
:
1208 case PA_BT_AUDIO_STATE_DISCONNECTED
:
1209 case PA_BT_AUDIO_STATE_CONNECTED
:
1210 case PA_BT_AUDIO_STATE_CONNECTING
:
1213 case PA_BT_AUDIO_STATE_PLAYING
:
1215 pa_log_debug("Changing profile to hfgw");
1216 if (pa_card_set_profile(u
->card
, "hfgw", FALSE
) < 0)
1217 pa_log("Failed to change profile to hfgw");
1224 dbus_error_free(&err
);
1226 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
1229 /* Run from main thread */
1230 static void sink_set_volume_cb(pa_sink
*s
) {
1240 k
= pa_sprintf_malloc("bluetooth-device@%p", (void*) s
);
1241 u
= pa_shared_get(s
->core
, k
);
1245 pa_assert(u
->sink
== s
);
1246 pa_assert(u
->profile
== PROFILE_HSP
);
1248 gain
= (pa_cvolume_max(&s
->real_volume
) * HSP_MAX_GAIN
) / PA_VOLUME_NORM
;
1250 if (gain
> HSP_MAX_GAIN
)
1251 gain
= HSP_MAX_GAIN
;
1253 volume
= (pa_volume_t
) (gain
* PA_VOLUME_NORM
/ HSP_MAX_GAIN
);
1255 /* increment volume by one to correct rounding errors */
1256 if (volume
< PA_VOLUME_NORM
)
1259 pa_cvolume_set(&s
->real_volume
, u
->sample_spec
.channels
, volume
);
1261 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", u
->path
, "org.bluez.Headset", "SetSpeakerGain"));
1262 pa_assert_se(dbus_message_append_args(m
, DBUS_TYPE_UINT16
, &gain
, DBUS_TYPE_INVALID
));
1263 pa_assert_se(dbus_connection_send(pa_dbus_connection_get(u
->connection
), m
, NULL
));
1264 dbus_message_unref(m
);
1267 /* Run from main thread */
1268 static void source_set_volume_cb(pa_source
*s
) {
1278 k
= pa_sprintf_malloc("bluetooth-device@%p", (void*) s
);
1279 u
= pa_shared_get(s
->core
, k
);
1283 pa_assert(u
->source
== s
);
1284 pa_assert(u
->profile
== PROFILE_HSP
);
1286 gain
= (pa_cvolume_max(&s
->real_volume
) * HSP_MAX_GAIN
) / PA_VOLUME_NORM
;
1288 if (gain
> HSP_MAX_GAIN
)
1289 gain
= HSP_MAX_GAIN
;
1291 volume
= (pa_volume_t
) (gain
* PA_VOLUME_NORM
/ HSP_MAX_GAIN
);
1293 /* increment volume by one to correct rounding errors */
1294 if (volume
< PA_VOLUME_NORM
)
1297 pa_cvolume_set(&s
->real_volume
, u
->sample_spec
.channels
, volume
);
1299 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", u
->path
, "org.bluez.Headset", "SetMicrophoneGain"));
1300 pa_assert_se(dbus_message_append_args(m
, DBUS_TYPE_UINT16
, &gain
, DBUS_TYPE_INVALID
));
1301 pa_assert_se(dbus_connection_send(pa_dbus_connection_get(u
->connection
), m
, NULL
));
1302 dbus_message_unref(m
);
1305 /* Run from main thread */
1306 static char *get_name(const char *type
, pa_modargs
*ma
, const char *device_id
, pa_bool_t
*namereg_fail
) {
1312 pa_assert(device_id
);
1313 pa_assert(namereg_fail
);
1315 t
= pa_sprintf_malloc("%s_name", type
);
1316 n
= pa_modargs_get_value(ma
, t
, NULL
);
1320 *namereg_fail
= TRUE
;
1321 return pa_xstrdup(n
);
1324 if ((n
= pa_modargs_get_value(ma
, "name", NULL
)))
1325 *namereg_fail
= TRUE
;
1328 *namereg_fail
= FALSE
;
1331 return pa_sprintf_malloc("bluez_%s.%s", type
, n
);
1334 static int sco_over_pcm_state_update(struct userdata
*u
, pa_bool_t changed
) {
1336 pa_assert(USE_SCO_OVER_PCM(u
));
1338 if (PA_SINK_IS_OPENED(pa_sink_get_state(u
->hsp
.sco_sink
)) ||
1339 PA_SOURCE_IS_OPENED(pa_source_get_state(u
->hsp
.sco_source
))) {
1341 if (u
->stream_fd
>= 0)
1344 pa_log_debug("Resuming SCO over PCM");
1345 if (init_profile(u
) < 0) {
1346 pa_log("Can't resume SCO over PCM");
1350 return bt_transport_acquire(u
, TRUE
);
1354 if (u
->stream_fd
< 0)
1357 pa_log_debug("Closing SCO over PCM");
1359 bt_transport_release(u
);
1365 static pa_hook_result_t
sink_state_changed_cb(pa_core
*c
, pa_sink
*s
, struct userdata
*u
) {
1367 pa_sink_assert_ref(s
);
1370 if (s
!= u
->hsp
.sco_sink
)
1373 sco_over_pcm_state_update(u
, TRUE
);
1378 static pa_hook_result_t
source_state_changed_cb(pa_core
*c
, pa_source
*s
, struct userdata
*u
) {
1380 pa_source_assert_ref(s
);
1383 if (s
!= u
->hsp
.sco_source
)
1386 sco_over_pcm_state_update(u
, TRUE
);
1391 static pa_hook_result_t
nrec_changed_cb(pa_bluetooth_transport
*t
, void *call_data
, struct userdata
*u
) {
1397 p
= pa_proplist_new();
1398 pa_proplist_sets(p
, "bluetooth.nrec", t
->nrec
? "1" : "0");
1399 pa_source_update_proplist(u
->source
, PA_UPDATE_REPLACE
, p
);
1400 pa_proplist_free(p
);
1405 static void connect_ports(struct userdata
*u
, void *sink_or_source_new_data
, pa_direction_t direction
) {
1407 pa_sink_new_data
*sink_new_data
;
1408 pa_source_new_data
*source_new_data
;
1410 pa_device_port
*port
;
1412 if (direction
== PA_DIRECTION_OUTPUT
)
1413 data
.sink_new_data
= sink_or_source_new_data
;
1415 data
.source_new_data
= sink_or_source_new_data
;
1417 switch (u
->profile
) {
1419 pa_assert_se(port
= pa_hashmap_get(u
->card
->ports
, "a2dp-output"));
1420 pa_assert_se(pa_hashmap_put(data
.sink_new_data
->ports
, port
->name
, port
) >= 0);
1421 pa_device_port_ref(port
);
1424 case PROFILE_A2DP_SOURCE
:
1425 pa_assert_se(port
= pa_hashmap_get(u
->card
->ports
, "a2dp-input"));
1426 pa_assert_se(pa_hashmap_put(data
.source_new_data
->ports
, port
->name
, port
) >= 0);
1427 pa_device_port_ref(port
);
1431 if (direction
== PA_DIRECTION_OUTPUT
) {
1432 pa_assert_se(port
= pa_hashmap_get(u
->card
->ports
, "hsp-output"));
1433 pa_assert_se(pa_hashmap_put(data
.sink_new_data
->ports
, port
->name
, port
) >= 0);
1435 pa_assert_se(port
= pa_hashmap_get(u
->card
->ports
, "hsp-input"));
1436 pa_assert_se(pa_hashmap_put(data
.source_new_data
->ports
, port
->name
, port
) >= 0);
1438 pa_device_port_ref(port
);
1442 if (direction
== PA_DIRECTION_OUTPUT
) {
1443 pa_assert_se(port
= pa_hashmap_get(u
->card
->ports
, "hfgw-output"));
1444 pa_assert_se(pa_hashmap_put(data
.sink_new_data
->ports
, port
->name
, port
) >= 0);
1446 pa_assert_se(port
= pa_hashmap_get(u
->card
->ports
, "hfgw-input"));
1447 pa_assert_se(pa_hashmap_put(data
.source_new_data
->ports
, port
->name
, port
) >= 0);
1449 pa_device_port_ref(port
);
1453 pa_assert_not_reached();
1457 /* Run from main thread */
1458 static int add_sink(struct userdata
*u
) {
1461 if (USE_SCO_OVER_PCM(u
)) {
1464 u
->sink
= u
->hsp
.sco_sink
;
1465 p
= pa_proplist_new();
1466 pa_proplist_sets(p
, "bluetooth.protocol", "sco");
1467 pa_proplist_update(u
->sink
->proplist
, PA_UPDATE_MERGE
, p
);
1468 pa_proplist_free(p
);
1470 if (!u
->hsp
.sink_state_changed_slot
)
1471 u
->hsp
.sink_state_changed_slot
= pa_hook_connect(&u
->core
->hooks
[PA_CORE_HOOK_SINK_STATE_CHANGED
], PA_HOOK_NORMAL
, (pa_hook_cb_t
) sink_state_changed_cb
, u
);
1474 pa_sink_new_data data
;
1477 pa_sink_new_data_init(&data
);
1478 data
.driver
= __FILE__
;
1479 data
.module
= u
->module
;
1480 pa_sink_new_data_set_sample_spec(&data
, &u
->sample_spec
);
1481 pa_proplist_sets(data
.proplist
, "bluetooth.protocol", u
->profile
== PROFILE_A2DP
? "a2dp" : "sco");
1482 if (u
->profile
== PROFILE_HSP
)
1483 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_INTENDED_ROLES
, "phone");
1484 data
.card
= u
->card
;
1485 data
.name
= get_name("sink", u
->modargs
, u
->address
, &b
);
1486 data
.namereg_fail
= b
;
1488 if (pa_modargs_get_proplist(u
->modargs
, "sink_properties", data
.proplist
, PA_UPDATE_REPLACE
) < 0) {
1489 pa_log("Invalid properties");
1490 pa_sink_new_data_done(&data
);
1493 connect_ports(u
, &data
, PA_DIRECTION_OUTPUT
);
1495 u
->sink
= pa_sink_new(u
->core
, &data
, PA_SINK_HARDWARE
|PA_SINK_LATENCY
);
1496 pa_sink_new_data_done(&data
);
1499 pa_log_error("Failed to create sink");
1503 u
->sink
->userdata
= u
;
1504 u
->sink
->parent
.process_msg
= sink_process_msg
;
1506 pa_sink_set_max_request(u
->sink
, u
->block_size
);
1507 pa_sink_set_fixed_latency(u
->sink
,
1508 (u
->profile
== PROFILE_A2DP
? FIXED_LATENCY_PLAYBACK_A2DP
: FIXED_LATENCY_PLAYBACK_HSP
) +
1509 pa_bytes_to_usec(u
->block_size
, &u
->sample_spec
));
1512 if (u
->profile
== PROFILE_HSP
) {
1513 pa_sink_set_set_volume_callback(u
->sink
, sink_set_volume_cb
);
1514 u
->sink
->n_volume_steps
= 16;
1516 k
= pa_sprintf_malloc("bluetooth-device@%p", (void*) u
->sink
);
1517 pa_shared_set(u
->core
, k
, u
);
1524 /* Run from main thread */
1525 static int add_source(struct userdata
*u
) {
1528 if (USE_SCO_OVER_PCM(u
)) {
1529 u
->source
= u
->hsp
.sco_source
;
1530 pa_proplist_sets(u
->source
->proplist
, "bluetooth.protocol", "hsp");
1532 if (!u
->hsp
.source_state_changed_slot
)
1533 u
->hsp
.source_state_changed_slot
= pa_hook_connect(&u
->core
->hooks
[PA_CORE_HOOK_SOURCE_STATE_CHANGED
], PA_HOOK_NORMAL
, (pa_hook_cb_t
) source_state_changed_cb
, u
);
1536 pa_source_new_data data
;
1539 pa_source_new_data_init(&data
);
1540 data
.driver
= __FILE__
;
1541 data
.module
= u
->module
;
1542 pa_source_new_data_set_sample_spec(&data
, &u
->sample_spec
);
1543 pa_proplist_sets(data
.proplist
, "bluetooth.protocol", u
->profile
== PROFILE_A2DP_SOURCE
? "a2dp_source" : "hsp");
1544 if ((u
->profile
== PROFILE_HSP
) || (u
->profile
== PROFILE_HFGW
))
1545 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_INTENDED_ROLES
, "phone");
1547 data
.card
= u
->card
;
1548 data
.name
= get_name("source", u
->modargs
, u
->address
, &b
);
1549 data
.namereg_fail
= b
;
1551 if (pa_modargs_get_proplist(u
->modargs
, "source_properties", data
.proplist
, PA_UPDATE_REPLACE
) < 0) {
1552 pa_log("Invalid properties");
1553 pa_source_new_data_done(&data
);
1557 connect_ports(u
, &data
, PA_DIRECTION_INPUT
);
1558 u
->source
= pa_source_new(u
->core
, &data
, PA_SOURCE_HARDWARE
|PA_SOURCE_LATENCY
);
1559 pa_source_new_data_done(&data
);
1562 pa_log_error("Failed to create source");
1566 u
->source
->userdata
= u
;
1567 u
->source
->parent
.process_msg
= source_process_msg
;
1569 pa_source_set_fixed_latency(u
->source
,
1570 (u
->profile
== PROFILE_A2DP_SOURCE
? FIXED_LATENCY_RECORD_A2DP
: FIXED_LATENCY_RECORD_HSP
) +
1571 pa_bytes_to_usec(u
->block_size
, &u
->sample_spec
));
1574 if ((u
->profile
== PROFILE_HSP
) || (u
->profile
== PROFILE_HFGW
)) {
1575 pa_bluetooth_transport
*t
;
1576 t
= pa_bluetooth_discovery_get_transport(u
->discovery
, u
->transport
);
1578 pa_proplist_sets(u
->source
->proplist
, "bluetooth.nrec", t
->nrec
? "1" : "0");
1580 if (!u
->hsp
.nrec_changed_slot
)
1581 u
->hsp
.nrec_changed_slot
= pa_hook_connect(&t
->hooks
[PA_BLUETOOTH_TRANSPORT_HOOK_NREC_CHANGED
], PA_HOOK_NORMAL
, (pa_hook_cb_t
) nrec_changed_cb
, u
);
1584 if (u
->profile
== PROFILE_HSP
) {
1585 pa_source_set_set_volume_callback(u
->source
, source_set_volume_cb
);
1586 u
->source
->n_volume_steps
= 16;
1588 k
= pa_sprintf_malloc("bluetooth-device@%p", (void*) u
->source
);
1589 pa_shared_set(u
->core
, k
, u
);
1596 static int bt_transport_config_a2dp(struct userdata
*u
) {
1597 const pa_bluetooth_transport
*t
;
1598 struct a2dp_info
*a2dp
= &u
->a2dp
;
1601 t
= pa_bluetooth_discovery_get_transport(u
->discovery
, u
->transport
);
1604 config
= (a2dp_sbc_t
*) t
->config
;
1606 u
->sample_spec
.format
= PA_SAMPLE_S16LE
;
1608 if (a2dp
->sbc_initialized
)
1609 sbc_reinit(&a2dp
->sbc
, 0);
1611 sbc_init(&a2dp
->sbc
, 0);
1612 a2dp
->sbc_initialized
= TRUE
;
1614 switch (config
->frequency
) {
1615 case BT_SBC_SAMPLING_FREQ_16000
:
1616 a2dp
->sbc
.frequency
= SBC_FREQ_16000
;
1617 u
->sample_spec
.rate
= 16000U;
1619 case BT_SBC_SAMPLING_FREQ_32000
:
1620 a2dp
->sbc
.frequency
= SBC_FREQ_32000
;
1621 u
->sample_spec
.rate
= 32000U;
1623 case BT_SBC_SAMPLING_FREQ_44100
:
1624 a2dp
->sbc
.frequency
= SBC_FREQ_44100
;
1625 u
->sample_spec
.rate
= 44100U;
1627 case BT_SBC_SAMPLING_FREQ_48000
:
1628 a2dp
->sbc
.frequency
= SBC_FREQ_48000
;
1629 u
->sample_spec
.rate
= 48000U;
1632 pa_assert_not_reached();
1635 switch (config
->channel_mode
) {
1636 case BT_A2DP_CHANNEL_MODE_MONO
:
1637 a2dp
->sbc
.mode
= SBC_MODE_MONO
;
1638 u
->sample_spec
.channels
= 1;
1640 case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL
:
1641 a2dp
->sbc
.mode
= SBC_MODE_DUAL_CHANNEL
;
1642 u
->sample_spec
.channels
= 2;
1644 case BT_A2DP_CHANNEL_MODE_STEREO
:
1645 a2dp
->sbc
.mode
= SBC_MODE_STEREO
;
1646 u
->sample_spec
.channels
= 2;
1648 case BT_A2DP_CHANNEL_MODE_JOINT_STEREO
:
1649 a2dp
->sbc
.mode
= SBC_MODE_JOINT_STEREO
;
1650 u
->sample_spec
.channels
= 2;
1653 pa_assert_not_reached();
1656 switch (config
->allocation_method
) {
1657 case BT_A2DP_ALLOCATION_SNR
:
1658 a2dp
->sbc
.allocation
= SBC_AM_SNR
;
1660 case BT_A2DP_ALLOCATION_LOUDNESS
:
1661 a2dp
->sbc
.allocation
= SBC_AM_LOUDNESS
;
1664 pa_assert_not_reached();
1667 switch (config
->subbands
) {
1668 case BT_A2DP_SUBBANDS_4
:
1669 a2dp
->sbc
.subbands
= SBC_SB_4
;
1671 case BT_A2DP_SUBBANDS_8
:
1672 a2dp
->sbc
.subbands
= SBC_SB_8
;
1675 pa_assert_not_reached();
1678 switch (config
->block_length
) {
1679 case BT_A2DP_BLOCK_LENGTH_4
:
1680 a2dp
->sbc
.blocks
= SBC_BLK_4
;
1682 case BT_A2DP_BLOCK_LENGTH_8
:
1683 a2dp
->sbc
.blocks
= SBC_BLK_8
;
1685 case BT_A2DP_BLOCK_LENGTH_12
:
1686 a2dp
->sbc
.blocks
= SBC_BLK_12
;
1688 case BT_A2DP_BLOCK_LENGTH_16
:
1689 a2dp
->sbc
.blocks
= SBC_BLK_16
;
1692 pa_assert_not_reached();
1695 a2dp
->min_bitpool
= config
->min_bitpool
;
1696 a2dp
->max_bitpool
= config
->max_bitpool
;
1698 /* Set minimum bitpool for source to get the maximum possible block_size */
1699 a2dp
->sbc
.bitpool
= u
->profile
== PROFILE_A2DP
? a2dp
->max_bitpool
: a2dp
->min_bitpool
;
1700 a2dp
->codesize
= sbc_get_codesize(&a2dp
->sbc
);
1701 a2dp
->frame_length
= sbc_get_frame_length(&a2dp
->sbc
);
1704 ((u
->link_mtu
- sizeof(struct rtp_header
) - sizeof(struct rtp_payload
))
1705 / a2dp
->frame_length
1708 pa_log_info("SBC parameters:\n\tallocation=%u\n\tsubbands=%u\n\tblocks=%u\n\tbitpool=%u\n",
1709 a2dp
->sbc
.allocation
, a2dp
->sbc
.subbands
, a2dp
->sbc
.blocks
, a2dp
->sbc
.bitpool
);
1714 static int bt_transport_config(struct userdata
*u
) {
1715 if (u
->profile
== PROFILE_HSP
|| u
->profile
== PROFILE_HFGW
) {
1716 u
->block_size
= u
->link_mtu
;
1717 u
->sample_spec
.format
= PA_SAMPLE_S16LE
;
1718 u
->sample_spec
.channels
= 1;
1719 u
->sample_spec
.rate
= 8000;
1723 return bt_transport_config_a2dp(u
);
1726 /* Run from main thread */
1727 static int setup_bt(struct userdata
*u
) {
1728 const pa_bluetooth_device
*d
;
1729 const pa_bluetooth_transport
*t
;
1733 if (!(d
= pa_bluetooth_discovery_get_by_path(u
->discovery
, u
->path
))) {
1734 pa_log_error("Failed to get device object.");
1738 /* release transport if exist */
1740 bt_transport_release(u
);
1741 pa_xfree(u
->transport
);
1742 u
->transport
= NULL
;
1745 /* check if profile has a transport */
1746 t
= pa_bluetooth_device_get_transport(d
, u
->profile
);
1748 pa_log_warn("Profile has no transport");
1752 u
->transport
= pa_xstrdup(t
->path
);
1754 if (bt_transport_acquire(u
, FALSE
) < 0)
1757 return bt_transport_config(u
);
1760 /* Run from main thread */
1761 static int init_profile(struct userdata
*u
) {
1764 pa_assert(u
->profile
!= PROFILE_OFF
);
1766 if (setup_bt(u
) < 0)
1769 if (u
->profile
== PROFILE_A2DP
||
1770 u
->profile
== PROFILE_HSP
||
1771 u
->profile
== PROFILE_HFGW
)
1772 if (add_sink(u
) < 0)
1775 if (u
->profile
== PROFILE_HSP
||
1776 u
->profile
== PROFILE_A2DP_SOURCE
||
1777 u
->profile
== PROFILE_HFGW
)
1778 if (add_source(u
) < 0)
1784 /* Run from main thread */
1785 static void stop_thread(struct userdata
*u
) {
1791 pa_asyncmsgq_send(u
->thread_mq
.inq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, 0, NULL
);
1792 pa_thread_free(u
->thread
);
1796 if (u
->rtpoll_item
) {
1797 pa_rtpoll_item_free(u
->rtpoll_item
);
1798 u
->rtpoll_item
= NULL
;
1801 if (u
->hsp
.sink_state_changed_slot
) {
1802 pa_hook_slot_free(u
->hsp
.sink_state_changed_slot
);
1803 u
->hsp
.sink_state_changed_slot
= NULL
;
1806 if (u
->hsp
.source_state_changed_slot
) {
1807 pa_hook_slot_free(u
->hsp
.source_state_changed_slot
);
1808 u
->hsp
.source_state_changed_slot
= NULL
;
1811 if (u
->hsp
.nrec_changed_slot
) {
1812 pa_hook_slot_free(u
->hsp
.nrec_changed_slot
);
1813 u
->hsp
.nrec_changed_slot
= NULL
;
1817 if (u
->profile
== PROFILE_HSP
) {
1818 k
= pa_sprintf_malloc("bluetooth-device@%p", (void*) u
->sink
);
1819 pa_shared_remove(u
->core
, k
);
1823 pa_sink_unref(u
->sink
);
1828 if (u
->profile
== PROFILE_HSP
) {
1829 k
= pa_sprintf_malloc("bluetooth-device@%p", (void*) u
->source
);
1830 pa_shared_remove(u
->core
, k
);
1834 pa_source_unref(u
->source
);
1839 pa_thread_mq_done(&u
->thread_mq
);
1841 pa_rtpoll_free(u
->rtpoll
);
1845 if (u
->read_smoother
) {
1846 pa_smoother_free(u
->read_smoother
);
1847 u
->read_smoother
= NULL
;
1851 /* Run from main thread */
1852 static int start_thread(struct userdata
*u
) {
1854 pa_assert(!u
->thread
);
1855 pa_assert(!u
->rtpoll
);
1856 pa_assert(!u
->rtpoll_item
);
1858 u
->rtpoll
= pa_rtpoll_new();
1859 pa_thread_mq_init(&u
->thread_mq
, u
->core
->mainloop
, u
->rtpoll
);
1861 if (USE_SCO_OVER_PCM(u
)) {
1862 if (sco_over_pcm_state_update(u
, FALSE
) < 0) {
1866 k
= pa_sprintf_malloc("bluetooth-device@%p", (void*) u
->sink
);
1867 pa_shared_remove(u
->core
, k
);
1872 k
= pa_sprintf_malloc("bluetooth-device@%p", (void*) u
->source
);
1873 pa_shared_remove(u
->core
, k
);
1880 pa_sink_ref(u
->sink
);
1881 pa_source_ref(u
->source
);
1882 /* FIXME: monitor stream_fd error */
1886 if (!(u
->thread
= pa_thread_new("bluetooth", thread_func
, u
))) {
1887 pa_log_error("Failed to create IO thread");
1893 pa_sink_set_asyncmsgq(u
->sink
, u
->thread_mq
.inq
);
1894 pa_sink_set_rtpoll(u
->sink
, u
->rtpoll
);
1895 pa_sink_put(u
->sink
);
1897 if (u
->sink
->set_volume
)
1898 u
->sink
->set_volume(u
->sink
);
1902 pa_source_set_asyncmsgq(u
->source
, u
->thread_mq
.inq
);
1903 pa_source_set_rtpoll(u
->source
, u
->rtpoll
);
1904 pa_source_put(u
->source
);
1906 if (u
->source
->set_volume
)
1907 u
->source
->set_volume(u
->source
);
1913 static void save_sco_volume_callbacks(struct userdata
*u
) {
1915 pa_assert(USE_SCO_OVER_PCM(u
));
1917 u
->hsp
.sco_sink_set_volume
= u
->hsp
.sco_sink
->set_volume
;
1918 u
->hsp
.sco_source_set_volume
= u
->hsp
.sco_source
->set_volume
;
1921 static void restore_sco_volume_callbacks(struct userdata
*u
) {
1923 pa_assert(USE_SCO_OVER_PCM(u
));
1925 pa_sink_set_set_volume_callback(u
->hsp
.sco_sink
, u
->hsp
.sco_sink_set_volume
);
1926 pa_source_set_set_volume_callback(u
->hsp
.sco_source
, u
->hsp
.sco_source_set_volume
);
1929 /* Run from main thread */
1930 static int card_set_profile(pa_card
*c
, pa_card_profile
*new_profile
) {
1933 pa_queue
*inputs
= NULL
, *outputs
= NULL
;
1934 const pa_bluetooth_device
*device
;
1937 pa_assert(new_profile
);
1938 pa_assert_se(u
= c
->userdata
);
1940 d
= PA_CARD_PROFILE_DATA(new_profile
);
1942 if (!(device
= pa_bluetooth_discovery_get_by_path(u
->discovery
, u
->path
))) {
1943 pa_log_error("Failed to get device object.");
1947 /* The state signal is sent by bluez, so it is racy to check
1948 strictly for CONNECTED, we should also accept STREAMING state
1949 as being good enough. However, if the profile is used
1950 concurrently (which is unlikely), ipc will fail later on, and
1951 module will be unloaded. */
1952 if (device
->headset_state
< PA_BT_AUDIO_STATE_CONNECTED
&& *d
== PROFILE_HSP
) {
1953 pa_log_warn("HSP is not connected, refused to switch profile");
1956 else if (device
->audio_sink_state
< PA_BT_AUDIO_STATE_CONNECTED
&& *d
== PROFILE_A2DP
) {
1957 pa_log_warn("A2DP is not connected, refused to switch profile");
1960 else if (device
->hfgw_state
< PA_BT_AUDIO_STATE_CONNECTED
&& *d
== PROFILE_HFGW
) {
1961 pa_log_warn("HandsfreeGateway is not connected, refused to switch profile");
1966 inputs
= pa_sink_move_all_start(u
->sink
, NULL
);
1968 if (!USE_SCO_OVER_PCM(u
))
1969 pa_sink_unlink(u
->sink
);
1973 outputs
= pa_source_move_all_start(u
->source
, NULL
);
1975 if (!USE_SCO_OVER_PCM(u
))
1976 pa_source_unlink(u
->source
);
1981 if (u
->profile
!= PROFILE_OFF
&& u
->transport
) {
1982 bt_transport_release(u
);
1983 pa_xfree(u
->transport
);
1984 u
->transport
= NULL
;
1987 if (USE_SCO_OVER_PCM(u
))
1988 restore_sco_volume_callbacks(u
);
1991 u
->sample_spec
= u
->requested_sample_spec
;
1993 if (USE_SCO_OVER_PCM(u
))
1994 save_sco_volume_callbacks(u
);
1996 if (u
->profile
!= PROFILE_OFF
)
1999 if (u
->sink
|| u
->source
)
2004 pa_sink_move_all_finish(u
->sink
, inputs
, FALSE
);
2006 pa_sink_move_all_fail(inputs
);
2011 pa_source_move_all_finish(u
->source
, outputs
, FALSE
);
2013 pa_source_move_all_fail(outputs
);
2019 static void create_ports_for_profile(struct userdata
*u
, pa_card_new_data
*card_new_data
, pa_card_profile
*profile
) {
2020 pa_device_port
*port
;
2023 d
= PA_CARD_PROFILE_DATA(profile
);
2027 pa_assert_se(port
= pa_device_port_new(u
->core
, "a2dp-output", _("Bluetooth High Quality (A2DP)"), 0));
2028 pa_assert_se(pa_hashmap_put(card_new_data
->ports
, port
->name
, port
) >= 0);
2029 port
->is_output
= 1;
2031 port
->priority
= profile
->priority
* 100;
2032 pa_hashmap_put(port
->profiles
, profile
->name
, profile
);
2035 case PROFILE_A2DP_SOURCE
:
2036 pa_assert_se(port
= pa_device_port_new(u
->core
, "a2dp-input", _("Bluetooth High Quality (A2DP)"), 0));
2037 pa_assert_se(pa_hashmap_put(card_new_data
->ports
, port
->name
, port
) >= 0);
2038 port
->is_output
= 0;
2040 port
->priority
= profile
->priority
* 100;
2041 pa_hashmap_put(port
->profiles
, profile
->name
, profile
);
2045 pa_assert_se(port
= pa_device_port_new(u
->core
, "hsp-output", _("Bluetooth Telephony (HSP/HFP)"), 0));
2046 pa_assert_se(pa_hashmap_put(card_new_data
->ports
, port
->name
, port
) >= 0);
2047 port
->is_output
= 1;
2049 port
->priority
= profile
->priority
* 100;
2050 pa_hashmap_put(port
->profiles
, profile
->name
, profile
);
2052 pa_assert_se(port
= pa_device_port_new(u
->core
, "hsp-input", _("Bluetooth Telephony (HSP/HFP)"), 0));
2053 pa_assert_se(pa_hashmap_put(card_new_data
->ports
, port
->name
, port
) >= 0);
2054 port
->is_output
= 0;
2056 port
->priority
= profile
->priority
* 100;
2057 pa_hashmap_put(port
->profiles
, profile
->name
, profile
);
2061 pa_assert_se(port
= pa_device_port_new(u
->core
, "hfgw-output", _("Bluetooth Handsfree Gateway"), 0));
2062 pa_assert_se(pa_hashmap_put(card_new_data
->ports
, port
->name
, port
) >= 0);
2063 port
->is_output
= 1;
2065 port
->priority
= profile
->priority
* 100;
2066 pa_hashmap_put(port
->profiles
, profile
->name
, profile
);
2068 pa_assert_se(port
= pa_device_port_new(u
->core
, "hfgw-input", _("Bluetooth Handsfree Gateway"), 0));
2069 pa_assert_se(pa_hashmap_put(card_new_data
->ports
, port
->name
, port
) >= 0);
2070 port
->is_output
= 0;
2072 port
->priority
= profile
->priority
* 100;
2073 pa_hashmap_put(port
->profiles
, profile
->name
, profile
);
2077 pa_assert_not_reached();
2082 /* Run from main thread */
2083 static int add_card(struct userdata
*u
, const pa_bluetooth_device
*device
) {
2084 pa_card_new_data data
;
2090 const char *default_profile
;
2095 pa_card_new_data_init(&data
);
2096 data
.driver
= __FILE__
;
2097 data
.module
= u
->module
;
2099 n
= pa_bluetooth_cleanup_name(device
->name
);
2100 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, n
);
2102 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_STRING
, device
->address
);
2103 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_API
, "bluez");
2104 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_CLASS
, "sound");
2105 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_BUS
, "bluetooth");
2106 if ((ff
= pa_bluetooth_get_form_factor(device
->class)))
2107 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_FORM_FACTOR
, ff
);
2108 pa_proplist_sets(data
.proplist
, "bluez.path", device
->path
);
2109 pa_proplist_setf(data
.proplist
, "bluez.class", "0x%06x", (unsigned) device
->class);
2110 pa_proplist_sets(data
.proplist
, "bluez.name", device
->name
);
2111 data
.name
= get_name("card", u
->modargs
, device
->address
, &b
);
2112 data
.namereg_fail
= b
;
2114 if (pa_modargs_get_proplist(u
->modargs
, "card_properties", data
.proplist
, PA_UPDATE_REPLACE
) < 0) {
2115 pa_log("Invalid properties");
2116 pa_card_new_data_done(&data
);
2120 /* we base hsp/a2dp availability on UUIDs.
2121 Ideally, it would be based on "Connected" state, but
2122 we can't afford to wait for this information when
2123 we are loaded with profile="hsp", for instance */
2124 if (pa_bluetooth_uuid_has(device
->uuids
, A2DP_SINK_UUID
)) {
2125 p
= pa_card_profile_new("a2dp", _("High Fidelity Playback (A2DP)"), sizeof(enum profile
));
2129 p
->max_sink_channels
= 2;
2130 p
->max_source_channels
= 0;
2132 d
= PA_CARD_PROFILE_DATA(p
);
2134 create_ports_for_profile(u
, &data
, p
);
2136 pa_hashmap_put(data
.profiles
, p
->name
, p
);
2139 if (pa_bluetooth_uuid_has(device
->uuids
, A2DP_SOURCE_UUID
)) {
2140 p
= pa_card_profile_new("a2dp_source", _("High Fidelity Capture (A2DP)"), sizeof(enum profile
));
2144 p
->max_sink_channels
= 0;
2145 p
->max_source_channels
= 2;
2147 d
= PA_CARD_PROFILE_DATA(p
);
2148 *d
= PROFILE_A2DP_SOURCE
;
2149 create_ports_for_profile(u
, &data
, p
);
2151 pa_hashmap_put(data
.profiles
, p
->name
, p
);
2154 if (pa_bluetooth_uuid_has(device
->uuids
, HSP_HS_UUID
) ||
2155 pa_bluetooth_uuid_has(device
->uuids
, HFP_HS_UUID
)) {
2156 p
= pa_card_profile_new("hsp", _("Telephony Duplex (HSP/HFP)"), sizeof(enum profile
));
2160 p
->max_sink_channels
= 1;
2161 p
->max_source_channels
= 1;
2163 d
= PA_CARD_PROFILE_DATA(p
);
2165 create_ports_for_profile(u
, &data
, p
);
2167 pa_hashmap_put(data
.profiles
, p
->name
, p
);
2170 if (pa_bluetooth_uuid_has(device
->uuids
, HFP_AG_UUID
)) {
2171 p
= pa_card_profile_new("hfgw", _("Handsfree Gateway"), sizeof(enum profile
));
2175 p
->max_sink_channels
= 1;
2176 p
->max_source_channels
= 1;
2178 d
= PA_CARD_PROFILE_DATA(p
);
2180 create_ports_for_profile(u
, &data
, p
);
2182 pa_hashmap_put(data
.profiles
, p
->name
, p
);
2185 pa_assert(!pa_hashmap_isempty(data
.profiles
));
2187 p
= pa_card_profile_new("off", _("Off"), sizeof(enum profile
));
2188 d
= PA_CARD_PROFILE_DATA(p
);
2190 pa_hashmap_put(data
.profiles
, p
->name
, p
);
2192 if ((default_profile
= pa_modargs_get_value(u
->modargs
, "profile", NULL
))) {
2193 if (pa_hashmap_get(data
.profiles
, default_profile
))
2194 pa_card_new_data_set_profile(&data
, default_profile
);
2196 pa_log_warn("Profile '%s' not valid or not supported by device.", default_profile
);
2199 u
->card
= pa_card_new(u
->core
, &data
);
2200 pa_card_new_data_done(&data
);
2203 pa_log("Failed to allocate card.");
2207 u
->card
->userdata
= u
;
2208 u
->card
->set_profile
= card_set_profile
;
2210 d
= PA_CARD_PROFILE_DATA(u
->card
->active_profile
);
2212 if ((device
->headset_state
< PA_BT_AUDIO_STATE_CONNECTED
&& *d
== PROFILE_HSP
) ||
2213 (device
->audio_sink_state
< PA_BT_AUDIO_STATE_CONNECTED
&& *d
== PROFILE_A2DP
) ||
2214 (device
->hfgw_state
< PA_BT_AUDIO_STATE_CONNECTED
&& *d
== PROFILE_HFGW
)) {
2215 pa_log_warn("Default profile not connected, selecting off profile");
2216 u
->card
->active_profile
= pa_hashmap_get(u
->card
->profiles
, "off");
2217 u
->card
->save_profile
= FALSE
;
2220 d
= PA_CARD_PROFILE_DATA(u
->card
->active_profile
);
2223 if (USE_SCO_OVER_PCM(u
))
2224 save_sco_volume_callbacks(u
);
2229 /* Run from main thread */
2230 static const pa_bluetooth_device
* find_device(struct userdata
*u
, const char *address
, const char *path
) {
2231 const pa_bluetooth_device
*d
= NULL
;
2235 if (!address
&& !path
) {
2236 pa_log_error("Failed to get device address/path from module arguments.");
2241 if (!(d
= pa_bluetooth_discovery_get_by_path(u
->discovery
, path
))) {
2242 pa_log_error("%s is not a valid BlueZ audio device.", path
);
2246 if (address
&& !(pa_streq(d
->address
, address
))) {
2247 pa_log_error("Passed path %s address %s != %s don't match.", path
, d
->address
, address
);
2252 if (!(d
= pa_bluetooth_discovery_get_by_address(u
->discovery
, address
))) {
2253 pa_log_error("%s is not known.", address
);
2259 u
->address
= pa_xstrdup(d
->address
);
2260 u
->path
= pa_xstrdup(d
->path
);
2266 /* Run from main thread */
2267 static int setup_dbus(struct userdata
*u
) {
2270 dbus_error_init(&err
);
2272 u
->connection
= pa_dbus_bus_get(u
->core
, DBUS_BUS_SYSTEM
, &err
);
2274 if (dbus_error_is_set(&err
) || !u
->connection
) {
2275 pa_log("Failed to get D-Bus connection: %s", err
.message
);
2276 dbus_error_free(&err
);
2283 int pa__init(pa_module
* m
) {
2287 const char *address
, *path
;
2289 char *mike
, *speaker
;
2290 const pa_bluetooth_device
*device
;
2294 dbus_error_init(&err
);
2296 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
2297 pa_log_error("Failed to parse module arguments");
2301 m
->userdata
= u
= pa_xnew0(struct userdata
, 1);
2305 u
->sample_spec
= m
->core
->default_sample_spec
;
2308 if (pa_modargs_get_value(ma
, "sco_sink", NULL
) &&
2309 !(u
->hsp
.sco_sink
= pa_namereg_get(m
->core
, pa_modargs_get_value(ma
, "sco_sink", NULL
), PA_NAMEREG_SINK
))) {
2310 pa_log("SCO sink not found");
2314 if (pa_modargs_get_value(ma
, "sco_source", NULL
) &&
2315 !(u
->hsp
.sco_source
= pa_namereg_get(m
->core
, pa_modargs_get_value(ma
, "sco_source", NULL
), PA_NAMEREG_SOURCE
))) {
2316 pa_log("SCO source not found");
2320 if (pa_modargs_get_value_u32(ma
, "rate", &u
->sample_spec
.rate
) < 0 ||
2321 u
->sample_spec
.rate
<= 0 || u
->sample_spec
.rate
> PA_RATE_MAX
) {
2322 pa_log_error("Failed to get rate from module arguments");
2326 u
->auto_connect
= TRUE
;
2327 if (pa_modargs_get_value_boolean(ma
, "auto_connect", &u
->auto_connect
)) {
2328 pa_log("Failed to parse auto_connect= argument");
2332 channels
= u
->sample_spec
.channels
;
2333 if (pa_modargs_get_value_u32(ma
, "channels", &channels
) < 0 ||
2334 channels
<= 0 || channels
> PA_CHANNELS_MAX
) {
2335 pa_log_error("Failed to get channels from module arguments");
2338 u
->sample_spec
.channels
= (uint8_t) channels
;
2339 u
->requested_sample_spec
= u
->sample_spec
;
2341 address
= pa_modargs_get_value(ma
, "address", NULL
);
2342 path
= pa_modargs_get_value(ma
, "path", NULL
);
2344 if (setup_dbus(u
) < 0)
2347 if (!(u
->discovery
= pa_bluetooth_discovery_get(m
->core
)))
2350 if (!(device
= find_device(u
, address
, path
)))
2353 /* Add the card structure. This will also initialize the default profile */
2354 if (add_card(u
, device
) < 0)
2357 if (!(u
->msg
= pa_msgobject_new(bluetooth_msg
)))
2360 u
->msg
->parent
.process_msg
= device_process_msg
;
2361 u
->msg
->card
= u
->card
;
2363 if (!dbus_connection_add_filter(pa_dbus_connection_get(u
->connection
), filter_cb
, u
, NULL
)) {
2364 pa_log_error("Failed to add filter function");
2367 u
->filter_added
= TRUE
;
2369 speaker
= pa_sprintf_malloc("type='signal',sender='org.bluez',interface='org.bluez.Headset',member='SpeakerGainChanged',path='%s'", u
->path
);
2370 mike
= pa_sprintf_malloc("type='signal',sender='org.bluez',interface='org.bluez.Headset',member='MicrophoneGainChanged',path='%s'", u
->path
);
2372 if (pa_dbus_add_matches(
2373 pa_dbus_connection_get(u
->connection
), &err
,
2376 "type='signal',sender='org.bluez',interface='org.bluez.MediaTransport',member='PropertyChanged'",
2377 "type='signal',sender='org.bluez',interface='org.bluez.HandsfreeGateway',member='PropertyChanged'",
2383 pa_log("Failed to add D-Bus matches: %s", err
.message
);
2390 if (u
->profile
!= PROFILE_OFF
)
2391 if (init_profile(u
) < 0)
2394 if (u
->sink
|| u
->source
)
2395 if (start_thread(u
) < 0)
2404 dbus_error_free(&err
);
2409 int pa__get_n_used(pa_module
*m
) {
2413 pa_assert_se(u
= m
->userdata
);
2416 (u
->sink
? pa_sink_linked_by(u
->sink
) : 0) +
2417 (u
->source
? pa_source_linked_by(u
->source
) : 0);
2420 void pa__done(pa_module
*m
) {
2425 if (!(u
= m
->userdata
))
2428 if (u
->sink
&& !USE_SCO_OVER_PCM(u
))
2429 pa_sink_unlink(u
->sink
);
2431 if (u
->source
&& !USE_SCO_OVER_PCM(u
))
2432 pa_source_unlink(u
->source
);
2436 if (USE_SCO_OVER_PCM(u
))
2437 restore_sco_volume_callbacks(u
);
2439 if (u
->connection
) {
2442 char *speaker
, *mike
;
2443 speaker
= pa_sprintf_malloc("type='signal',sender='org.bluez',interface='org.bluez.Headset',member='SpeakerGainChanged',path='%s'", u
->path
);
2444 mike
= pa_sprintf_malloc("type='signal',sender='org.bluez',interface='org.bluez.Headset',member='MicrophoneGainChanged',path='%s'", u
->path
);
2446 pa_dbus_remove_matches(pa_dbus_connection_get(u
->connection
), speaker
, mike
,
2447 "type='signal',sender='org.bluez',interface='org.bluez.MediaTransport',member='PropertyChanged'",
2448 "type='signal',sender='org.bluez',interface='org.bluez.HandsfreeGateway',member='PropertyChanged'",
2455 if (u
->filter_added
)
2456 dbus_connection_remove_filter(pa_dbus_connection_get(u
->connection
), filter_cb
, u
);
2458 pa_dbus_connection_unref(u
->connection
);
2465 pa_card_free(u
->card
);
2467 if (u
->read_smoother
)
2468 pa_smoother_free(u
->read_smoother
);
2471 pa_xfree(u
->a2dp
.buffer
);
2473 sbc_finish(&u
->a2dp
.sbc
);
2476 pa_modargs_free(u
->modargs
);
2478 pa_xfree(u
->address
);
2482 bt_transport_release(u
);
2483 pa_xfree(u
->transport
);
2487 pa_bluetooth_discovery_unref(u
->discovery
);