2 This file is part of PulseAudio.
4 Copyright 2008 Joao Paulo Rechi Vita
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 #include <pulse/xmalloc.h>
31 #include <pulse/timeval.h>
32 #include <pulsecore/core-error.h>
33 #include <pulsecore/module.h>
34 #include <pulsecore/modargs.h>
35 #include <pulsecore/thread.h>
36 #include <pulsecore/thread-mq.h>
37 #include <pulsecore/rtpoll.h>
38 #include <pulsecore/time-smoother.h>
39 #include <pulsecore/rtclock.h>
41 #include "dbus-util.h"
42 #include "module-bt-device-symdef.h"
46 #define DEFAULT_SINK_NAME "bt_sink"
47 #define BUFFER_SIZE 2048
48 #define MAX_BITPOOL 64
51 PA_MODULE_AUTHOR("Joao Paulo Rechi Vita");
52 PA_MODULE_DESCRIPTION("Bluetooth audio sink and source");
53 PA_MODULE_VERSION(PACKAGE_VERSION
);
54 PA_MODULE_LOAD_ONCE(FALSE
);
56 "name=<name of the device> "
57 "addr=<address of the device> "
58 "profile=<a2dp|hsp>");
61 sbc_capabilities_t sbc_capabilities
;
62 sbc_t sbc
; /* Codec data */
63 int sbc_initialized
; /* Keep track if the encoder is initialized */
64 int codesize
; /* SBC codesize */
65 int samples
; /* Number of encoded samples */
66 uint8_t buffer
[BUFFER_SIZE
]; /* Codec transfer buffer */
67 int count
; /* Codec transfer buffer counter */
69 int nsamples
; /* Cumulative number of codec samples */
70 uint16_t seq_num
; /* Cumulative packet sequence */
71 int frame_count
; /* Current frames in buffer*/
79 pa_thread_mq thread_mq
;
81 pa_rtpoll_item
*rtpoll_item
;
85 pa_smoother
*smoother
;
107 static const char* const valid_modargs
[] = {
116 static int bt_audioservice_send(int sk
, const bt_audio_msg_header_t
*msg
) {
118 pa_log
/*_debug*/("sending %s", bt_audio_strmsg(msg
->msg_type
));
119 if (send(sk
, msg
, BT_AUDIO_IPC_PACKET_SIZE
, 0) > 0)
123 pa_log_error("Error sending data to audio service: %s(%d)", strerror(errno
), errno
);
128 static int bt_audioservice_recv(int sk
, bt_audio_msg_header_t
*inmsg
) {
132 pa_log
/*_debug*/("trying to receive msg from audio service...");
133 if (recv(sk
, inmsg
, BT_AUDIO_IPC_PACKET_SIZE
, 0) > 0) {
134 type
= bt_audio_strmsg(inmsg
->msg_type
);
136 pa_log
/*_debug*/("Received %s", type
);
141 pa_log_error("Bogus message type %d received from audio service", inmsg
->msg_type
);
146 pa_log_error("Error receiving data from audio service: %s(%d)", strerror(errno
), errno
);
152 static int bt_audioservice_expect(int sk
, bt_audio_msg_header_t
*rsp_hdr
, int expected_type
) {
153 int e
= bt_audioservice_recv(sk
, rsp_hdr
);
155 if (rsp_hdr
->msg_type
!= expected_type
) {
157 pa_log_error("Bogus message %s received while %s was expected", bt_audio_strmsg(rsp_hdr
->msg_type
),
158 bt_audio_strmsg(expected_type
));
164 static int bt_getcaps(struct userdata
*u
) {
166 char buf
[BT_AUDIO_IPC_PACKET_SIZE
];
167 bt_audio_rsp_msg_header_t
*rsp_hdr
= (void*) buf
;
168 struct bt_getcapabilities_req
*getcaps_req
= (void*) buf
;
169 struct bt_getcapabilities_rsp
*getcaps_rsp
= (void*) buf
;
171 memset(getcaps_req
, 0, BT_AUDIO_IPC_PACKET_SIZE
);
172 getcaps_req
->h
.msg_type
= BT_GETCAPABILITIES_REQ
;
173 strncpy(getcaps_req
->device
, u
->addr
, 18);
174 if (strcasecmp(u
->profile
, "a2dp") == 0)
175 getcaps_req
->transport
= BT_CAPABILITIES_TRANSPORT_A2DP
;
176 else if (strcasecmp(u
->profile
, "hsp") == 0)
177 getcaps_req
->transport
= BT_CAPABILITIES_TRANSPORT_SCO
;
179 pa_log_error("invalid profile argument: %s", u
->profile
);
182 getcaps_req
->flags
= 0;
183 getcaps_req
->flags
|= BT_FLAG_AUTOCONNECT
;
185 e
= bt_audioservice_send(u
->audioservice_fd
, &getcaps_req
->h
);
187 pa_log_error("failed to send GETCAPABILITIES_REQ");
191 e
= bt_audioservice_expect(u
->audioservice_fd
, &rsp_hdr
->msg_h
, BT_GETCAPABILITIES_RSP
);
193 pa_log_error("failed to expect for GETCAPABILITIES_RSP");
196 if (rsp_hdr
->posix_errno
!= 0) {
197 pa_log_error("BT_GETCAPABILITIES failed : %s (%d)", strerror(rsp_hdr
->posix_errno
), rsp_hdr
->posix_errno
);
198 return -rsp_hdr
->posix_errno
;
201 if ((u
->transport
= getcaps_rsp
->transport
) == BT_CAPABILITIES_TRANSPORT_A2DP
)
202 u
->a2dp
.sbc_capabilities
= getcaps_rsp
->sbc_capabilities
;
207 static uint8_t default_bitpool(uint8_t freq
, uint8_t mode
) {
209 case BT_SBC_SAMPLING_FREQ_16000
:
210 case BT_SBC_SAMPLING_FREQ_32000
:
212 case BT_SBC_SAMPLING_FREQ_44100
:
214 case BT_A2DP_CHANNEL_MODE_MONO
:
215 case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL
:
217 case BT_A2DP_CHANNEL_MODE_STEREO
:
218 case BT_A2DP_CHANNEL_MODE_JOINT_STEREO
:
221 pa_log_warn("Invalid channel mode %u", mode
);
224 case BT_SBC_SAMPLING_FREQ_48000
:
226 case BT_A2DP_CHANNEL_MODE_MONO
:
227 case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL
:
229 case BT_A2DP_CHANNEL_MODE_STEREO
:
230 case BT_A2DP_CHANNEL_MODE_JOINT_STEREO
:
233 pa_log_warn("Invalid channel mode %u", mode
);
237 pa_log_warn("Invalid sampling freq %u", freq
);
242 static int bt_a2dp_init(struct userdata
*u
) {
243 sbc_capabilities_t
*cap
= &u
->a2dp
.sbc_capabilities
;
244 unsigned int max_bitpool
, min_bitpool
, rate
, channels
;
248 cap
->frequency
= BT_SBC_SAMPLING_FREQ_48000
;
251 cap
->frequency
= BT_SBC_SAMPLING_FREQ_44100
;
254 cap
->frequency
= BT_SBC_SAMPLING_FREQ_32000
;
257 cap
->frequency
= BT_SBC_SAMPLING_FREQ_16000
;
260 pa_log_error("Rate %d not supported", rate
);
264 // if (cfg->has_channel_mode)
265 // cap->channel_mode = cfg->channel_mode;
268 if (cap
->channel_mode
& BT_A2DP_CHANNEL_MODE_JOINT_STEREO
)
269 cap
->channel_mode
= BT_A2DP_CHANNEL_MODE_JOINT_STEREO
;
270 else if (cap
->channel_mode
& BT_A2DP_CHANNEL_MODE_STEREO
)
271 cap
->channel_mode
= BT_A2DP_CHANNEL_MODE_STEREO
;
272 else if (cap
->channel_mode
& BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL
)
273 cap
->channel_mode
= BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL
;
275 if (cap
->channel_mode
& BT_A2DP_CHANNEL_MODE_MONO
)
276 cap
->channel_mode
= BT_A2DP_CHANNEL_MODE_MONO
;
279 if (!cap
->channel_mode
) {
280 pa_log_error("No supported channel modes");
284 // if (cfg->has_block_length)
285 // cap->block_length = cfg->block_length;
287 if (cap
->block_length
& BT_A2DP_BLOCK_LENGTH_16
)
288 cap
->block_length
= BT_A2DP_BLOCK_LENGTH_16
;
289 else if (cap
->block_length
& BT_A2DP_BLOCK_LENGTH_12
)
290 cap
->block_length
= BT_A2DP_BLOCK_LENGTH_12
;
291 else if (cap
->block_length
& BT_A2DP_BLOCK_LENGTH_8
)
292 cap
->block_length
= BT_A2DP_BLOCK_LENGTH_8
;
293 else if (cap
->block_length
& BT_A2DP_BLOCK_LENGTH_4
)
294 cap
->block_length
= BT_A2DP_BLOCK_LENGTH_4
;
296 pa_log_error("No supported block lengths");
300 // if (cfg->has_subbands)
301 // cap->subbands = cfg->subbands;
302 if (cap
->subbands
& BT_A2DP_SUBBANDS_8
)
303 cap
->subbands
= BT_A2DP_SUBBANDS_8
;
304 else if (cap
->subbands
& BT_A2DP_SUBBANDS_4
)
305 cap
->subbands
= BT_A2DP_SUBBANDS_4
;
307 pa_log_error("No supported subbands");
311 // if (cfg->has_allocation_method)
312 // cap->allocation_method = cfg->allocation_method;
313 if (cap
->allocation_method
& BT_A2DP_ALLOCATION_LOUDNESS
)
314 cap
->allocation_method
= BT_A2DP_ALLOCATION_LOUDNESS
;
315 else if (cap
->allocation_method
& BT_A2DP_ALLOCATION_SNR
)
316 cap
->allocation_method
= BT_A2DP_ALLOCATION_SNR
;
318 // if (cfg->has_bitpool)
319 // min_bitpool = max_bitpool = cfg->bitpool;
321 min_bitpool
= MAX(MIN_BITPOOL
, cap
->min_bitpool
);
322 max_bitpool
= MIN(default_bitpool(cap
->frequency
, cap
->channel_mode
), cap
->max_bitpool
);
325 cap
->min_bitpool
= min_bitpool
;
326 cap
->max_bitpool
= max_bitpool
;
331 static void bt_a2dp_setup(struct bt_a2dp
*a2dp
) {
332 sbc_capabilities_t active_capabilities
= a2dp
->sbc_capabilities
;
334 if (a2dp
->sbc_initialized
)
335 sbc_reinit(&a2dp
->sbc
, 0);
337 sbc_init(&a2dp
->sbc
, 0);
338 a2dp
->sbc_initialized
= 1;
340 if (active_capabilities
.frequency
& BT_SBC_SAMPLING_FREQ_16000
)
341 a2dp
->sbc
.frequency
= SBC_FREQ_16000
;
343 if (active_capabilities
.frequency
& BT_SBC_SAMPLING_FREQ_32000
)
344 a2dp
->sbc
.frequency
= SBC_FREQ_32000
;
346 if (active_capabilities
.frequency
& BT_SBC_SAMPLING_FREQ_44100
)
347 a2dp
->sbc
.frequency
= SBC_FREQ_44100
;
349 if (active_capabilities
.frequency
& BT_SBC_SAMPLING_FREQ_48000
)
350 a2dp
->sbc
.frequency
= SBC_FREQ_48000
;
352 if (active_capabilities
.channel_mode
& BT_A2DP_CHANNEL_MODE_MONO
)
353 a2dp
->sbc
.mode
= SBC_MODE_MONO
;
355 if (active_capabilities
.channel_mode
& BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL
)
356 a2dp
->sbc
.mode
= SBC_MODE_DUAL_CHANNEL
;
358 if (active_capabilities
.channel_mode
& BT_A2DP_CHANNEL_MODE_STEREO
)
359 a2dp
->sbc
.mode
= SBC_MODE_STEREO
;
361 if (active_capabilities
.channel_mode
& BT_A2DP_CHANNEL_MODE_JOINT_STEREO
)
362 a2dp
->sbc
.mode
= SBC_MODE_JOINT_STEREO
;
364 a2dp
->sbc
.allocation
= active_capabilities
.allocation_method
== BT_A2DP_ALLOCATION_SNR
? SBC_AM_SNR
: SBC_AM_LOUDNESS
;
366 switch (active_capabilities
.subbands
) {
367 case BT_A2DP_SUBBANDS_4
:
368 a2dp
->sbc
.subbands
= SBC_SB_4
;
370 case BT_A2DP_SUBBANDS_8
:
371 a2dp
->sbc
.subbands
= SBC_SB_8
;
375 switch (active_capabilities
.block_length
) {
376 case BT_A2DP_BLOCK_LENGTH_4
:
377 a2dp
->sbc
.blocks
= SBC_BLK_4
;
379 case BT_A2DP_BLOCK_LENGTH_8
:
380 a2dp
->sbc
.blocks
= SBC_BLK_8
;
382 case BT_A2DP_BLOCK_LENGTH_12
:
383 a2dp
->sbc
.blocks
= SBC_BLK_12
;
385 case BT_A2DP_BLOCK_LENGTH_16
:
386 a2dp
->sbc
.blocks
= SBC_BLK_16
;
390 a2dp
->sbc
.bitpool
= active_capabilities
.max_bitpool
;
391 a2dp
->codesize
= sbc_get_codesize(&a2dp
->sbc
);
392 // a2dp->count = sizeof(struct rtp_header) + sizeof(struct rtp_payload);
395 static int bt_setconf(struct userdata
*u
) {
397 char buf
[BT_AUDIO_IPC_PACKET_SIZE
];
398 bt_audio_rsp_msg_header_t
*rsp_hdr
= (void*) buf
;
399 struct bt_setconfiguration_req
*setconf_req
= (void*) buf
;
400 struct bt_setconfiguration_rsp
*setconf_rsp
= (void*) buf
;
402 if (u
->transport
== BT_CAPABILITIES_TRANSPORT_A2DP
) {
405 pa_log_error("a2dp_init error");
410 memset(setconf_req
, 0, BT_AUDIO_IPC_PACKET_SIZE
);
411 setconf_req
->h
.msg_type
= BT_SETCONFIGURATION_REQ
;
412 strncpy(setconf_req
->device
, u
->addr
, 18);
413 setconf_req
->transport
= u
->transport
;
414 if (u
->transport
== BT_CAPABILITIES_TRANSPORT_A2DP
)
415 setconf_req
->sbc_capabilities
= u
->a2dp
.sbc_capabilities
;
416 setconf_req
->access_mode
= BT_CAPABILITIES_ACCESS_MODE_WRITE
;
418 e
= bt_audioservice_send(u
->audioservice_fd
, &setconf_req
->h
);
420 pa_log_error("failed to send BT_SETCONFIGURATION_REQ");
424 e
= bt_audioservice_expect(u
->audioservice_fd
, &rsp_hdr
->msg_h
, BT_SETCONFIGURATION_RSP
);
426 pa_log_error("failed to expect BT_SETCONFIGURATION_RSP");
430 if (rsp_hdr
->posix_errno
!= 0) {
431 pa_log_error("BT_SETCONFIGURATION failed : %s(%d)", strerror(rsp_hdr
->posix_errno
), rsp_hdr
->posix_errno
);
432 return -rsp_hdr
->posix_errno
;
435 u
->transport
= setconf_rsp
->transport
;
436 u
->block_size
= u
->link_mtu
= setconf_rsp
->link_mtu
;
438 /* setup SBC encoder now we agree on parameters */
439 if (u
->transport
== BT_CAPABILITIES_TRANSPORT_A2DP
) {
440 bt_a2dp_setup(&u
->a2dp
);
441 pa_log
/*debug*/("\tallocation=%u\n\tsubbands=%u\n\tblocks=%u\n\tbitpool=%u\n",
442 u
->a2dp
.sbc
.allocation
, u
->a2dp
.sbc
.subbands
, u
->a2dp
.sbc
.blocks
, u
->a2dp
.sbc
.bitpool
);
448 static int bt_getstreamfd(struct userdata
*u
) {
450 char buf
[BT_AUDIO_IPC_PACKET_SIZE
];
451 struct bt_streamstart_req
*start_req
= (void*) buf
;
452 bt_audio_rsp_msg_header_t
*rsp_hdr
= (void*) buf
;
453 struct bt_streamfd_ind
*streamfd_ind
= (void*) buf
;
455 memset(start_req
, 0, BT_AUDIO_IPC_PACKET_SIZE
);
456 start_req
->h
.msg_type
= BT_STREAMSTART_REQ
;
458 e
= bt_audioservice_send(u
->audioservice_fd
, &start_req
->h
);
460 pa_log_error("failed to send BT_STREAMSTART_REQ");
464 e
= bt_audioservice_expect(u
->audioservice_fd
, &rsp_hdr
->msg_h
, BT_STREAMSTART_RSP
);
466 pa_log_error("failed to expect BT_STREAMSTART_RSP");
470 if (rsp_hdr
->posix_errno
!= 0) {
471 pa_log_error("BT_START failed : %s(%d)", strerror(rsp_hdr
->posix_errno
), rsp_hdr
->posix_errno
);
472 return -rsp_hdr
->posix_errno
;
475 e
= bt_audioservice_expect(u
->audioservice_fd
, &streamfd_ind
->h
, BT_STREAMFD_IND
);
477 pa_log_error("failed to expect BT_STREAMFD_IND");
481 if (u
->stream_fd
>= 0)
484 u
->stream_fd
= bt_audio_service_get_data_fd(u
->audioservice_fd
);
485 if (u
->stream_fd
< 0) {
486 pa_log_error("failed to get data fd");
490 // if (u->transport == BT_CAPABILITIES_TRANSPORT_A2DP) {
491 // opt_name = SO_SNDTIMEO;
492 // if (setsockopt(u->stream_fd, SOL_SOCKET, opt_name, &t, sizeof(t)) < 0) {
493 // pa_log_error("failed to set socket options for A2DP");
498 // opt_name = SCO_TXBUFS;
499 // if (setsockopt(u->stream_fd, SOL_SCO, opt_name, &period_count, sizeof(period_count)) == 0)
501 // opt_name = SO_SNDBUF;
502 // if (setsockopt(u->stream_fd, SOL_SCO, opt_name, &period_count, sizeof(period_count)) == 0)
504 // /* FIXME : handle error codes */
510 static int bt_hw_constraint(struct userdata
*u
) {
512 u
->ss
.format
= PA_SAMPLE_S16LE
;
518 static int sink_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
519 struct userdata
*u
= PA_SINK(o
)->userdata
;
523 case PA_SINK_MESSAGE_SET_STATE
:
524 switch ((pa_sink_state_t
) PA_PTR_TO_UINT(data
)) {
525 case PA_SINK_SUSPENDED
:
526 pa_assert(PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
));
527 pa_smoother_pause(u
->smoother
, pa_rtclock_usec());
530 case PA_SINK_RUNNING
:
531 if (u
->sink
->thread_info
.state
== PA_SINK_SUSPENDED
)
532 pa_smoother_resume(u
->smoother
, pa_rtclock_usec());
534 case PA_SINK_UNLINKED
:
540 case PA_SINK_MESSAGE_GET_LATENCY
: {
542 r
= pa_smoother_get(u
->smoother
, pa_rtclock_usec());
543 w
= pa_bytes_to_usec(u
->offset
+ u
->memchunk
.length
, &u
->sink
->sample_spec
);
544 *((pa_usec_t
*) data
) = w
> r
? w
- r
: 0;
550 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
553 static void thread_func(void *userdata
) {
554 struct userdata
*u
= userdata
;
559 pa_log
/*_debug*/("Thread starting up");
561 pa_thread_mq_install(&u
->thread_mq
);
562 pa_rtpoll_install(u
->rtpoll
);
564 pa_smoother_set_time_offset(u
->smoother
, pa_rtclock_usec());
569 if (PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
))
570 if (u
->sink
->thread_info
.rewind_requested
)
571 pa_sink_process_rewind(u
->sink
, 0);
573 if (u
->rtpoll_item
) {
574 struct pollfd
*pollfd
;
575 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
577 /* Render some data and write it to the fifo */
578 if (PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
) && pollfd
->revents
) {
586 if (u
->memchunk
.length
<= 0)
587 pa_sink_render(u
->sink
, u
->block_size
, &u
->memchunk
);
589 pa_assert(u
->memchunk
.length
> 0);
591 p
= pa_memblock_acquire(u
->memchunk
.memblock
);
592 l
= pa_write(u
->stream_fd
, (uint8_t*) p
+ u
->memchunk
.index
, u
->memchunk
.length
, &write_type
);
593 pa_memblock_release(u
->memchunk
.memblock
);
601 else if (errno
== EAGAIN
) {
603 /* OK, we filled all socket buffers up
608 pa_log("Failed to write data to FIFO: %s", pa_cstrerror(errno
));
615 u
->memchunk
.index
+= l
;
616 u
->memchunk
.length
-= l
;
618 if (u
->memchunk
.length
<= 0) {
619 pa_memblock_unref(u
->memchunk
.memblock
);
620 pa_memchunk_reset(&u
->memchunk
);
625 if (u
->memchunk
.length
> 0)
627 /* OK, we wrote less that we asked for,
628 * hence we can assume that the socket
629 * buffers are full now */
636 /* At this spot we know that the socket buffers are
637 * fully filled up. This is the best time to estimate
638 * the playback position of the server */
645 // if (ioctl(u->fd, SIOCOUTQ, &l) >= 0 && l > 0)
650 usec
= pa_bytes_to_usec(n
, &u
->sink
->sample_spec
);
652 if (usec
> u
->latency
)
657 pa_smoother_put(u
->smoother
, pa_rtclock_usec(), usec
);
660 /* Hmm, nothing to do. Let's sleep */
661 pollfd
->events
= PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
) ? POLLOUT
: 0;
664 if ((ret
= pa_rtpoll_run(u
->rtpoll
, TRUE
)) < 0)
670 if (u
->rtpoll_item
) {
671 struct pollfd
* pollfd
;
673 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
675 if (pollfd
->revents
& ~POLLOUT
) {
676 pa_log("FIFO shutdown.");
683 /* If this was no regular exit from the loop we have to continue
684 * processing messages until we received PA_MESSAGE_SHUTDOWN */
685 pa_asyncmsgq_post(u
->thread_mq
.outq
, PA_MSGOBJECT(u
->core
), PA_CORE_MESSAGE_UNLOAD_MODULE
, u
->module
, 0, NULL
, NULL
);
686 pa_asyncmsgq_wait_for(u
->thread_mq
.inq
, PA_MESSAGE_SHUTDOWN
);
689 pa_log_debug("Thread shutting down");
692 int pa__init(pa_module
* m
) {
694 const char *rate
, *channels
;
696 pa_sink_new_data data
;
700 m
->userdata
= u
= pa_xnew0(struct userdata
, 1);
703 u
->audioservice_fd
= -1;
708 u
->smoother
= pa_smoother_new(PA_USEC_PER_SEC
, PA_USEC_PER_SEC
*2, TRUE
, 10);
709 pa_memchunk_reset(&u
->memchunk
);
710 u
->rtpoll
= pa_rtpoll_new();
711 pa_thread_mq_init(&u
->thread_mq
, u
->core
->mainloop
, u
->rtpoll
);
712 u
->rtpoll_item
= NULL
;
714 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
715 pa_log_error("failed to parse module arguments");
718 if (!(u
->name
= pa_xstrdup(pa_modargs_get_value(ma
, "name", DEFAULT_SINK_NAME
)))) {
719 pa_log_error("failed to get device name from module arguments");
722 if (!(u
->addr
= pa_xstrdup(pa_modargs_get_value(ma
, "addr", NULL
)))) {
723 pa_log_error("failed to get device address from module arguments");
726 if (!(u
->profile
= pa_xstrdup(pa_modargs_get_value(ma
, "profile", NULL
)))) {
727 pa_log_error("failed to get profile from module arguments");
730 if (pa_modargs_get_value_u32(ma
, "rate", &u
->rate
) < 0) {
731 pa_log_error("failed to get rate from module arguments");
734 if (pa_modargs_get_value_u32(ma
, "channels", &u
->channels
) < 0) {
735 pa_log_error("failed to get channels from module arguments");
738 pa_log("Loading module-bt-device for %s (%s), profile %s", u
->name
, u
->addr
, u
->profile
);
740 /* connect to the bluez audio service */
741 u
->audioservice_fd
= bt_audio_service_open();
742 if (u
->audioservice_fd
<= 0) {
743 pa_log_error("couldn't connect to bluetooth audio service");
747 /* queries device capabilities */
750 pa_log_error("failed to get device capabilities");
754 /* configures the connection */
757 pa_log_error("failed to set config");
761 /* gets the device socket */
762 e
= bt_getstreamfd(u
);
764 pa_log_error("failed to get stream fd (%d)", e
);
768 /* configure hw supported sample specs */
769 e
= bt_hw_constraint(u
);
771 pa_log_error("failed to configure sample spec");
776 pa_sink_new_data_init(&data
);
777 data
.driver
= __FILE__
;
779 pa_sink_new_data_set_name(&data
, u
->name
);
780 pa_sink_new_data_set_sample_spec(&data
, &u
->ss
);
781 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_STRING
, u
->name
);
782 pa_proplist_setf(data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, "Bluetooth sink '%s' (%s)", u
->name
, u
->addr
);
783 u
->sink
= pa_sink_new(m
->core
, &data
, PA_SINK_HARDWARE
|PA_SINK_LATENCY
|PA_SINK_NETWORK
);
784 pa_sink_new_data_done(&data
);
786 pa_log_error("failed to create sink");
789 u
->sink
->userdata
= u
;
790 u
->sink
->parent
.process_msg
= sink_process_msg
;
791 pa_sink_set_asyncmsgq(u
->sink
, u
->thread_mq
.inq
);
792 pa_sink_set_rtpoll(u
->sink
, u
->rtpoll
);
794 /* start rt thread */
795 if (!(u
->thread
= pa_thread_new(thread_func
, u
))) {
796 pa_log_error("failed to create thread");
799 pa_sink_put(u
->sink
);
811 void pa__done(pa_module
*m
) {
812 pa_log("Unloading module-bt-device");