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 <pulse/sample.h>
33 #include <pulsecore/core-util.h>
34 #include <pulsecore/core-error.h>
35 #include <pulsecore/module.h>
36 #include <pulsecore/modargs.h>
37 #include <pulsecore/thread.h>
38 #include <pulsecore/thread-mq.h>
39 #include <pulsecore/rtpoll.h>
40 #include <pulsecore/time-smoother.h>
41 #include <pulsecore/rtclock.h>
43 #include "dbus-util.h"
44 #include "module-bt-device-symdef.h"
49 #define DEFAULT_SINK_NAME "bluetooth_sink"
50 #define BUFFER_SIZE 2048
51 #define MAX_BITPOOL 64
54 #define SCO_TXBUFS 0x03
55 #define SCO_RXBUFS 0x04
57 PA_MODULE_AUTHOR("Joao Paulo Rechi Vita");
58 PA_MODULE_DESCRIPTION("Bluetooth audio sink and source");
59 PA_MODULE_VERSION(PACKAGE_VERSION
);
60 PA_MODULE_LOAD_ONCE(FALSE
);
62 "name=<name of the device> "
63 "addr=<address of the device> "
64 "profile=<a2dp|hsp>");
67 sbc_capabilities_t sbc_capabilities
;
68 sbc_t sbc
; /* Codec data */
69 int sbc_initialized
; /* Keep track if the encoder is initialized */
70 int codesize
; /* SBC codesize */
71 int samples
; /* Number of encoded samples */
72 uint8_t buffer
[BUFFER_SIZE
]; /* Codec transfer buffer */
73 int count
; /* Codec transfer buffer counter */
75 int nsamples
; /* Cumulative number of codec samples */
76 uint16_t seq_num
; /* Cumulative packet sequence */
77 int frame_count
; /* Current frames in buffer*/
85 pa_thread_mq thread_mq
;
87 pa_rtpoll_item
*rtpoll_item
;
91 pa_smoother
*smoother
;
114 static const char* const valid_modargs
[] = {
123 static int bt_audioservice_send(int sk
, const bt_audio_msg_header_t
*msg
) {
125 pa_log
/*_debug*/("sending %s", bt_audio_strmsg(msg
->msg_type
));
126 if (send(sk
, msg
, BT_AUDIO_IPC_PACKET_SIZE
, 0) > 0)
130 pa_log_error("Error sending data to audio service: %s(%d)", pa_cstrerror(errno
), errno
);
135 static int bt_audioservice_recv(int sk
, bt_audio_msg_header_t
*inmsg
) {
139 pa_log
/*_debug*/("trying to receive msg from audio service...");
140 if (recv(sk
, inmsg
, BT_AUDIO_IPC_PACKET_SIZE
, 0) > 0) {
141 type
= bt_audio_strmsg(inmsg
->msg_type
);
143 pa_log
/*_debug*/("Received %s", type
);
148 pa_log_error("Bogus message type %d received from audio service", inmsg
->msg_type
);
153 pa_log_error("Error receiving data from audio service: %s(%d)", pa_cstrerror(errno
), errno
);
159 static int bt_audioservice_expect(int sk
, bt_audio_msg_header_t
*rsp_hdr
, int expected_type
) {
160 int e
= bt_audioservice_recv(sk
, rsp_hdr
);
162 if (rsp_hdr
->msg_type
!= expected_type
) {
164 pa_log_error("Bogus message %s received while %s was expected", bt_audio_strmsg(rsp_hdr
->msg_type
),
165 bt_audio_strmsg(expected_type
));
171 static int bt_getcaps(struct userdata
*u
) {
173 char buf
[BT_AUDIO_IPC_PACKET_SIZE
];
174 bt_audio_rsp_msg_header_t
*rsp_hdr
= (void*) buf
;
175 struct bt_getcapabilities_req
*getcaps_req
= (void*) buf
;
176 struct bt_getcapabilities_rsp
*getcaps_rsp
= (void*) buf
;
178 memset(getcaps_req
, 0, BT_AUDIO_IPC_PACKET_SIZE
);
179 getcaps_req
->h
.msg_type
= BT_GETCAPABILITIES_REQ
;
180 strncpy(getcaps_req
->device
, u
->addr
, 18);
181 if (strcasecmp(u
->profile
, "a2dp") == 0)
182 getcaps_req
->transport
= BT_CAPABILITIES_TRANSPORT_A2DP
;
183 else if (strcasecmp(u
->profile
, "hsp") == 0)
184 getcaps_req
->transport
= BT_CAPABILITIES_TRANSPORT_SCO
;
186 pa_log_error("invalid profile argument: %s", u
->profile
);
189 getcaps_req
->flags
= 0;
190 getcaps_req
->flags
|= BT_FLAG_AUTOCONNECT
;
192 e
= bt_audioservice_send(u
->audioservice_fd
, &getcaps_req
->h
);
194 pa_log_error("failed to send GETCAPABILITIES_REQ");
198 e
= bt_audioservice_expect(u
->audioservice_fd
, &rsp_hdr
->msg_h
, BT_GETCAPABILITIES_RSP
);
200 pa_log_error("failed to expect for GETCAPABILITIES_RSP");
203 if (rsp_hdr
->posix_errno
!= 0) {
204 pa_log_error("BT_GETCAPABILITIES failed : %s (%d)", pa_cstrerror(rsp_hdr
->posix_errno
), rsp_hdr
->posix_errno
);
205 return -rsp_hdr
->posix_errno
;
208 if ((u
->transport
= getcaps_rsp
->transport
) == BT_CAPABILITIES_TRANSPORT_A2DP
)
209 u
->a2dp
.sbc_capabilities
= getcaps_rsp
->sbc_capabilities
;
214 static uint8_t default_bitpool(uint8_t freq
, uint8_t mode
) {
216 case BT_SBC_SAMPLING_FREQ_16000
:
217 case BT_SBC_SAMPLING_FREQ_32000
:
219 case BT_SBC_SAMPLING_FREQ_44100
:
221 case BT_A2DP_CHANNEL_MODE_MONO
:
222 case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL
:
224 case BT_A2DP_CHANNEL_MODE_STEREO
:
225 case BT_A2DP_CHANNEL_MODE_JOINT_STEREO
:
228 pa_log_warn("Invalid channel mode %u", mode
);
231 case BT_SBC_SAMPLING_FREQ_48000
:
233 case BT_A2DP_CHANNEL_MODE_MONO
:
234 case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL
:
236 case BT_A2DP_CHANNEL_MODE_STEREO
:
237 case BT_A2DP_CHANNEL_MODE_JOINT_STEREO
:
240 pa_log_warn("Invalid channel mode %u", mode
);
244 pa_log_warn("Invalid sampling freq %u", freq
);
249 static int bt_a2dp_init(struct userdata
*u
) {
250 sbc_capabilities_t
*cap
= &u
->a2dp
.sbc_capabilities
;
251 unsigned int max_bitpool
, min_bitpool
;
255 cap
->frequency
= BT_SBC_SAMPLING_FREQ_48000
;
258 cap
->frequency
= BT_SBC_SAMPLING_FREQ_44100
;
261 cap
->frequency
= BT_SBC_SAMPLING_FREQ_32000
;
264 cap
->frequency
= BT_SBC_SAMPLING_FREQ_16000
;
267 pa_log_error("Rate %d not supported", u
->rate
);
271 // if (cfg->has_channel_mode)
272 // cap->channel_mode = cfg->channel_mode;
274 if (u
->channels
== 2) {
275 if (cap
->channel_mode
& BT_A2DP_CHANNEL_MODE_JOINT_STEREO
)
276 cap
->channel_mode
= BT_A2DP_CHANNEL_MODE_JOINT_STEREO
;
277 else if (cap
->channel_mode
& BT_A2DP_CHANNEL_MODE_STEREO
)
278 cap
->channel_mode
= BT_A2DP_CHANNEL_MODE_STEREO
;
279 else if (cap
->channel_mode
& BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL
)
280 cap
->channel_mode
= BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL
;
282 if (cap
->channel_mode
& BT_A2DP_CHANNEL_MODE_MONO
)
283 cap
->channel_mode
= BT_A2DP_CHANNEL_MODE_MONO
;
286 if (!cap
->channel_mode
) {
287 pa_log_error("No supported channel modes");
291 // if (cfg->has_block_length)
292 // cap->block_length = cfg->block_length;
294 if (cap
->block_length
& BT_A2DP_BLOCK_LENGTH_16
)
295 cap
->block_length
= BT_A2DP_BLOCK_LENGTH_16
;
296 else if (cap
->block_length
& BT_A2DP_BLOCK_LENGTH_12
)
297 cap
->block_length
= BT_A2DP_BLOCK_LENGTH_12
;
298 else if (cap
->block_length
& BT_A2DP_BLOCK_LENGTH_8
)
299 cap
->block_length
= BT_A2DP_BLOCK_LENGTH_8
;
300 else if (cap
->block_length
& BT_A2DP_BLOCK_LENGTH_4
)
301 cap
->block_length
= BT_A2DP_BLOCK_LENGTH_4
;
303 pa_log_error("No supported block lengths");
307 // if (cfg->has_subbands)
308 // cap->subbands = cfg->subbands;
309 if (cap
->subbands
& BT_A2DP_SUBBANDS_8
)
310 cap
->subbands
= BT_A2DP_SUBBANDS_8
;
311 else if (cap
->subbands
& BT_A2DP_SUBBANDS_4
)
312 cap
->subbands
= BT_A2DP_SUBBANDS_4
;
314 pa_log_error("No supported subbands");
318 // if (cfg->has_allocation_method)
319 // cap->allocation_method = cfg->allocation_method;
320 if (cap
->allocation_method
& BT_A2DP_ALLOCATION_LOUDNESS
)
321 cap
->allocation_method
= BT_A2DP_ALLOCATION_LOUDNESS
;
322 else if (cap
->allocation_method
& BT_A2DP_ALLOCATION_SNR
)
323 cap
->allocation_method
= BT_A2DP_ALLOCATION_SNR
;
325 // if (cfg->has_bitpool)
326 // min_bitpool = max_bitpool = cfg->bitpool;
328 min_bitpool
= MAX(MIN_BITPOOL
, cap
->min_bitpool
);
329 max_bitpool
= MIN(default_bitpool(cap
->frequency
, cap
->channel_mode
), cap
->max_bitpool
);
332 cap
->min_bitpool
= min_bitpool
;
333 cap
->max_bitpool
= max_bitpool
;
338 static void bt_a2dp_setup(struct bt_a2dp
*a2dp
) {
339 sbc_capabilities_t active_capabilities
= a2dp
->sbc_capabilities
;
341 if (a2dp
->sbc_initialized
)
342 sbc_reinit(&a2dp
->sbc
, 0);
344 sbc_init(&a2dp
->sbc
, 0);
345 a2dp
->sbc_initialized
= 1;
347 if (active_capabilities
.frequency
& BT_SBC_SAMPLING_FREQ_16000
)
348 a2dp
->sbc
.frequency
= SBC_FREQ_16000
;
350 if (active_capabilities
.frequency
& BT_SBC_SAMPLING_FREQ_32000
)
351 a2dp
->sbc
.frequency
= SBC_FREQ_32000
;
353 if (active_capabilities
.frequency
& BT_SBC_SAMPLING_FREQ_44100
)
354 a2dp
->sbc
.frequency
= SBC_FREQ_44100
;
356 if (active_capabilities
.frequency
& BT_SBC_SAMPLING_FREQ_48000
)
357 a2dp
->sbc
.frequency
= SBC_FREQ_48000
;
359 if (active_capabilities
.channel_mode
& BT_A2DP_CHANNEL_MODE_MONO
)
360 a2dp
->sbc
.mode
= SBC_MODE_MONO
;
362 if (active_capabilities
.channel_mode
& BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL
)
363 a2dp
->sbc
.mode
= SBC_MODE_DUAL_CHANNEL
;
365 if (active_capabilities
.channel_mode
& BT_A2DP_CHANNEL_MODE_STEREO
)
366 a2dp
->sbc
.mode
= SBC_MODE_STEREO
;
368 if (active_capabilities
.channel_mode
& BT_A2DP_CHANNEL_MODE_JOINT_STEREO
)
369 a2dp
->sbc
.mode
= SBC_MODE_JOINT_STEREO
;
371 a2dp
->sbc
.allocation
= (active_capabilities
.allocation_method
== BT_A2DP_ALLOCATION_SNR
? SBC_AM_SNR
: SBC_AM_LOUDNESS
);
373 switch (active_capabilities
.subbands
) {
374 case BT_A2DP_SUBBANDS_4
:
375 a2dp
->sbc
.subbands
= SBC_SB_4
;
377 case BT_A2DP_SUBBANDS_8
:
378 a2dp
->sbc
.subbands
= SBC_SB_8
;
382 switch (active_capabilities
.block_length
) {
383 case BT_A2DP_BLOCK_LENGTH_4
:
384 a2dp
->sbc
.blocks
= SBC_BLK_4
;
386 case BT_A2DP_BLOCK_LENGTH_8
:
387 a2dp
->sbc
.blocks
= SBC_BLK_8
;
389 case BT_A2DP_BLOCK_LENGTH_12
:
390 a2dp
->sbc
.blocks
= SBC_BLK_12
;
392 case BT_A2DP_BLOCK_LENGTH_16
:
393 a2dp
->sbc
.blocks
= SBC_BLK_16
;
397 a2dp
->sbc
.bitpool
= active_capabilities
.max_bitpool
;
398 a2dp
->codesize
= sbc_get_codesize(&a2dp
->sbc
);
399 a2dp
->count
= sizeof(struct rtp_header
) + sizeof(struct rtp_payload
);
402 static int bt_setconf(struct userdata
*u
) {
404 char buf
[BT_AUDIO_IPC_PACKET_SIZE
];
405 bt_audio_rsp_msg_header_t
*rsp_hdr
= (void*) buf
;
406 struct bt_setconfiguration_req
*setconf_req
= (void*) buf
;
407 struct bt_setconfiguration_rsp
*setconf_rsp
= (void*) buf
;
409 if (u
->transport
== BT_CAPABILITIES_TRANSPORT_A2DP
) {
412 pa_log_error("a2dp_init error");
417 u
->ss
.format
= PA_SAMPLE_S16LE
;
418 u
->ss
.rate
= u
->rate
;
419 u
->ss
.channels
= u
->channels
;
421 memset(setconf_req
, 0, BT_AUDIO_IPC_PACKET_SIZE
);
422 setconf_req
->h
.msg_type
= BT_SETCONFIGURATION_REQ
;
423 strncpy(setconf_req
->device
, u
->addr
, 18);
424 setconf_req
->transport
= u
->transport
;
425 if (u
->transport
== BT_CAPABILITIES_TRANSPORT_A2DP
)
426 setconf_req
->sbc_capabilities
= u
->a2dp
.sbc_capabilities
;
427 setconf_req
->access_mode
= BT_CAPABILITIES_ACCESS_MODE_WRITE
;
429 e
= bt_audioservice_send(u
->audioservice_fd
, &setconf_req
->h
);
431 pa_log_error("failed to send BT_SETCONFIGURATION_REQ");
435 e
= bt_audioservice_expect(u
->audioservice_fd
, &rsp_hdr
->msg_h
, BT_SETCONFIGURATION_RSP
);
437 pa_log_error("failed to expect BT_SETCONFIGURATION_RSP");
441 if (rsp_hdr
->posix_errno
!= 0) {
442 pa_log_error("BT_SETCONFIGURATION failed : %s(%d)", pa_cstrerror(rsp_hdr
->posix_errno
), rsp_hdr
->posix_errno
);
443 return -rsp_hdr
->posix_errno
;
446 u
->transport
= setconf_rsp
->transport
;
447 u
->link_mtu
= setconf_rsp
->link_mtu
;
449 /* setup SBC encoder now we agree on parameters */
450 if (u
->transport
== BT_CAPABILITIES_TRANSPORT_A2DP
) {
451 bt_a2dp_setup(&u
->a2dp
);
452 u
->block_size
= u
->a2dp
.codesize
;
453 pa_log
/*debug*/("\tallocation=%u\n\tsubbands=%u\n\tblocks=%u\n\tbitpool=%u\n",
454 u
->a2dp
.sbc
.allocation
, u
->a2dp
.sbc
.subbands
, u
->a2dp
.sbc
.blocks
, u
->a2dp
.sbc
.bitpool
);
457 u
->block_size
= u
->link_mtu
;
462 static int bt_getstreamfd(struct userdata
*u
) {
464 // uint32_t period_count = io->buffer_size / io->period_size;
465 // struct timeval t = { 0, period_count };
466 char buf
[BT_AUDIO_IPC_PACKET_SIZE
];
467 struct bt_streamstart_req
*start_req
= (void*) buf
;
468 bt_audio_rsp_msg_header_t
*rsp_hdr
= (void*) buf
;
469 struct bt_streamfd_ind
*streamfd_ind
= (void*) buf
;
471 memset(start_req
, 0, BT_AUDIO_IPC_PACKET_SIZE
);
472 start_req
->h
.msg_type
= BT_STREAMSTART_REQ
;
474 e
= bt_audioservice_send(u
->audioservice_fd
, &start_req
->h
);
476 pa_log_error("failed to send BT_STREAMSTART_REQ");
480 e
= bt_audioservice_expect(u
->audioservice_fd
, &rsp_hdr
->msg_h
, BT_STREAMSTART_RSP
);
482 pa_log_error("failed to expect BT_STREAMSTART_RSP");
486 if (rsp_hdr
->posix_errno
!= 0) {
487 pa_log_error("BT_START failed : %s(%d)", pa_cstrerror(rsp_hdr
->posix_errno
), rsp_hdr
->posix_errno
);
488 return -rsp_hdr
->posix_errno
;
491 e
= bt_audioservice_expect(u
->audioservice_fd
, &streamfd_ind
->h
, BT_STREAMFD_IND
);
493 pa_log_error("failed to expect BT_STREAMFD_IND");
497 if (u
->stream_fd
>= 0)
498 pa_close(u
->stream_fd
);
500 u
->stream_fd
= bt_audio_service_get_data_fd(u
->audioservice_fd
);
501 if (u
->stream_fd
< 0) {
502 pa_log_error("failed to get data fd: %s (%d)",pa_cstrerror(errno
), errno
);
506 // if (u->transport == BT_CAPABILITIES_TRANSPORT_A2DP) {
507 // if (setsockopt(u->stream_fd, SOL_SOCKET, SO_SNDTIMEO, &t, sizeof(t)) < 0) {
508 // pa_log_error("failed to set socket options for A2DP: %s (%d)",pa_cstrerror(errno), errno);
513 // if (setsockopt(u->stream_fd, SOL_SCO, SCO_TXBUFS, &period_count, sizeof(period_count)) == 0)
515 // if (setsockopt(u->stream_fd, SOL_SCO, SO_SNDBUF, &period_count, sizeof(period_count)) == 0)
517 // /* FIXME : handle error codes */
519 pa_make_fd_nonblock(u
->stream_fd
);
524 static int sink_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
525 struct userdata
*u
= PA_SINK(o
)->userdata
;
527 pa_log
/*_debug*/("got message: %d", code
);
530 case PA_SINK_MESSAGE_SET_STATE
:
531 switch ((pa_sink_state_t
) PA_PTR_TO_UINT(data
)) {
532 case PA_SINK_SUSPENDED
:
533 pa_assert(PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
));
534 pa_smoother_pause(u
->smoother
, pa_rtclock_usec());
537 case PA_SINK_RUNNING
:
538 if (u
->sink
->thread_info
.state
== PA_SINK_SUSPENDED
)
539 pa_smoother_resume(u
->smoother
, pa_rtclock_usec());
541 case PA_SINK_UNLINKED
:
547 case PA_SINK_MESSAGE_GET_LATENCY
: {
549 r
= pa_smoother_get(u
->smoother
, pa_rtclock_usec());
550 w
= pa_bytes_to_usec(u
->offset
+ u
->memchunk
.length
, &u
->sink
->sample_spec
);
551 *((pa_usec_t
*) data
) = w
> r
? w
- r
: 0;
557 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
560 static void thread_func(void *userdata
) {
561 struct userdata
*u
= userdata
;
566 pa_log
/*_debug*/("Thread starting up");
568 pa_thread_mq_install(&u
->thread_mq
);
569 pa_rtpoll_install(u
->rtpoll
);
571 pa_smoother_set_time_offset(u
->smoother
, pa_rtclock_usec());
575 struct pollfd
*pollfd
;
577 if (PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
)) {
578 if (u
->sink
->thread_info
.rewind_requested
) {
579 pa_log("rewind_requested");
580 pa_sink_process_rewind(u
->sink
, 0);
581 pa_log("rewind_finished");
585 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
587 /* Render some data and write it to the fifo */
588 if (PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
) && pollfd
->revents
) {
591 pa_log("Render some data and write it to the fifo");
597 u
->memchunk
.memblock
= pa_memblock_new(u
->mempool
, u
->block_size
);
598 pa_log("memblock allocated");
599 u
->memchunk
.length
= pa_memblock_get_length(u
->memchunk
.memblock
);
600 pa_log("memchunk length");
601 pa_sink_render_into_full(u
->sink
, &u
->memchunk
);
604 pa_assert(u
->memchunk
.length
> 0);
606 p
= pa_memblock_acquire(u
->memchunk
.memblock
);
607 pa_log("memblock acquired");
608 l
= pa_write(u
->stream_fd
, (uint8_t*) p
, u
->memchunk
.length
, &write_type
);
609 pa_log("memblock written");
610 pa_memblock_release(u
->memchunk
.memblock
);
611 pa_log("memblock released");
616 pa_log("l = %d < 0", l
);
619 else if (errno
== EAGAIN
)
622 pa_log("Failed to write data to FIFO: %s", pa_cstrerror(errno
));
626 pa_log("l = %d >= 0", l
);
628 pa_memblock_unref(u
->memchunk
.memblock
);
629 pa_log("memblock unrefered");
630 pa_memchunk_reset(&u
->memchunk
);
631 pa_log("memchunk reseted");
639 /* At this spot we know that the socket buffers are fully filled up.
640 * This is the best time to estimate the playback position of the server */
646 if (ioctl(u
->fd
, SIOCOUTQ
, &l
) >= 0 && l
> 0)
651 usec
= pa_bytes_to_usec(n
, &u
->sink
->sample_spec
);
652 if (usec
> u
->latency
)
656 pa_smoother_put(u
->smoother
, pa_rtclock_usec(), usec
);
659 /* Hmm, nothing to do. Let's sleep */
660 pa_log("let's sleep");
661 pollfd
->events
= PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
) ? POLLOUT
: 0;
663 if ((ret
= pa_rtpoll_run(u
->rtpoll
, TRUE
)) < 0) {
674 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
675 if (pollfd
->revents
& ~POLLOUT
) {
676 pa_log("FIFO shutdown.");
682 /* If this was no regular exit from the loop we have to continue processing messages until we receive PA_MESSAGE_SHUTDOWN */
684 pa_asyncmsgq_post(u
->thread_mq
.outq
, PA_MSGOBJECT(u
->core
), PA_CORE_MESSAGE_UNLOAD_MODULE
, u
->module
, 0, NULL
, NULL
);
685 pa_asyncmsgq_wait_for(u
->thread_mq
.inq
, PA_MESSAGE_SHUTDOWN
);
688 pa_log_debug("Thread shutting down");
691 int pa__init(pa_module
* m
) {
693 const char *rate
, *channels
;
695 pa_sink_new_data data
;
696 struct pollfd
*pollfd
;
700 m
->userdata
= u
= pa_xnew0(struct userdata
, 1);
703 u
->audioservice_fd
= -1;
708 u
->a2dp
.sbc_initialized
= 0;
709 u
->smoother
= pa_smoother_new(PA_USEC_PER_SEC
, PA_USEC_PER_SEC
*2, TRUE
, 10);
710 u
->mempool
= pa_mempool_new(FALSE
);
711 pa_memchunk_reset(&u
->memchunk
);
712 u
->rtpoll
= pa_rtpoll_new();
713 pa_thread_mq_init(&u
->thread_mq
, u
->core
->mainloop
, u
->rtpoll
);
714 u
->rtpoll_item
= NULL
;
716 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
717 pa_log_error("failed to parse module arguments");
720 if (!(u
->name
= pa_xstrdup(pa_modargs_get_value(ma
, "name", DEFAULT_SINK_NAME
)))) {
721 pa_log_error("failed to get device name from module arguments");
724 if (!(u
->addr
= pa_xstrdup(pa_modargs_get_value(ma
, "addr", NULL
)))) {
725 pa_log_error("failed to get device address from module arguments");
728 if (!(u
->profile
= pa_xstrdup(pa_modargs_get_value(ma
, "profile", NULL
)))) {
729 pa_log_error("failed to get profile from module arguments");
732 if (pa_modargs_get_value_u32(ma
, "rate", &u
->rate
) < 0) {
733 pa_log_error("failed to get rate from module arguments");
736 if (pa_modargs_get_value_u32(ma
, "channels", &u
->channels
) < 0) {
737 pa_log_error("failed to get channels from module arguments");
740 pa_log("Loading module-bt-device for %s (%s), profile %s", u
->name
, u
->addr
, u
->profile
);
742 /* connect to the bluez audio service */
743 u
->audioservice_fd
= bt_audio_service_open();
744 if (u
->audioservice_fd
<= 0) {
745 pa_log_error("couldn't connect to bluetooth audio service");
749 /* queries device capabilities */
752 pa_log_error("failed to get device capabilities");
756 /* configures the connection */
759 pa_log_error("failed to set config");
763 /* gets the device socket */
764 e
= bt_getstreamfd(u
);
766 pa_log_error("failed to get stream fd (%d)", e
);
771 pa_sink_new_data_init(&data
);
772 data
.driver
= __FILE__
;
774 pa_sink_new_data_set_name(&data
, u
->name
);
775 pa_sink_new_data_set_sample_spec(&data
, &u
->ss
);
776 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_STRING
, u
->name
);
777 pa_proplist_setf(data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, "Bluetooth sink '%s' (%s)", u
->name
, u
->addr
);
778 u
->sink
= pa_sink_new(m
->core
, &data
, PA_SINK_HARDWARE
|PA_SINK_LATENCY
);
779 pa_sink_new_data_done(&data
);
781 pa_log_error("failed to create sink");
784 u
->sink
->userdata
= u
;
785 u
->sink
->parent
.process_msg
= sink_process_msg
;
786 pa_sink_set_asyncmsgq(u
->sink
, u
->thread_mq
.inq
);
787 pa_sink_set_rtpoll(u
->sink
, u
->rtpoll
);
789 u
->rtpoll_item
= pa_rtpoll_item_new(u
->rtpoll
, PA_RTPOLL_NEVER
, 1);
790 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
791 pollfd
->fd
= u
->stream_fd
;
792 pollfd
->events
= pollfd
->revents
= 0;
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");