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"
48 #define DEFAULT_SINK_NAME "bt_sink"
49 #define BUFFER_SIZE 2048
50 #define MAX_BITPOOL 64
53 PA_MODULE_AUTHOR("Joao Paulo Rechi Vita");
54 PA_MODULE_DESCRIPTION("Bluetooth audio sink and source");
55 PA_MODULE_VERSION(PACKAGE_VERSION
);
56 PA_MODULE_LOAD_ONCE(FALSE
);
58 "name=<name of the device> "
59 "addr=<address of the device> "
60 "profile=<a2dp|hsp>");
63 sbc_capabilities_t sbc_capabilities
;
64 sbc_t sbc
; /* Codec data */
65 int sbc_initialized
; /* Keep track if the encoder is initialized */
66 int codesize
; /* SBC codesize */
67 int samples
; /* Number of encoded samples */
68 uint8_t buffer
[BUFFER_SIZE
]; /* Codec transfer buffer */
69 int count
; /* Codec transfer buffer counter */
71 int nsamples
; /* Cumulative number of codec samples */
72 uint16_t seq_num
; /* Cumulative packet sequence */
73 int frame_count
; /* Current frames in buffer*/
81 pa_thread_mq thread_mq
;
83 pa_rtpoll_item
*rtpoll_item
;
87 pa_smoother
*smoother
;
110 static const char* const valid_modargs
[] = {
119 static int bt_audioservice_send(int sk
, const bt_audio_msg_header_t
*msg
) {
121 pa_log
/*_debug*/("sending %s", bt_audio_strmsg(msg
->msg_type
));
122 if (send(sk
, msg
, BT_AUDIO_IPC_PACKET_SIZE
, 0) > 0)
126 pa_log_error("Error sending data to audio service: %s(%d)", pa_cstrerror(errno
), errno
);
131 static int bt_audioservice_recv(int sk
, bt_audio_msg_header_t
*inmsg
) {
135 pa_log
/*_debug*/("trying to receive msg from audio service...");
136 if (recv(sk
, inmsg
, BT_AUDIO_IPC_PACKET_SIZE
, 0) > 0) {
137 type
= bt_audio_strmsg(inmsg
->msg_type
);
139 pa_log
/*_debug*/("Received %s", type
);
144 pa_log_error("Bogus message type %d received from audio service", inmsg
->msg_type
);
149 pa_log_error("Error receiving data from audio service: %s(%d)", pa_cstrerror(errno
), errno
);
155 static int bt_audioservice_expect(int sk
, bt_audio_msg_header_t
*rsp_hdr
, int expected_type
) {
156 int e
= bt_audioservice_recv(sk
, rsp_hdr
);
158 if (rsp_hdr
->msg_type
!= expected_type
) {
160 pa_log_error("Bogus message %s received while %s was expected", bt_audio_strmsg(rsp_hdr
->msg_type
),
161 bt_audio_strmsg(expected_type
));
167 static int bt_getcaps(struct userdata
*u
) {
169 char buf
[BT_AUDIO_IPC_PACKET_SIZE
];
170 bt_audio_rsp_msg_header_t
*rsp_hdr
= (void*) buf
;
171 struct bt_getcapabilities_req
*getcaps_req
= (void*) buf
;
172 struct bt_getcapabilities_rsp
*getcaps_rsp
= (void*) buf
;
174 memset(getcaps_req
, 0, BT_AUDIO_IPC_PACKET_SIZE
);
175 getcaps_req
->h
.msg_type
= BT_GETCAPABILITIES_REQ
;
176 strncpy(getcaps_req
->device
, u
->addr
, 18);
177 if (strcasecmp(u
->profile
, "a2dp") == 0)
178 getcaps_req
->transport
= BT_CAPABILITIES_TRANSPORT_A2DP
;
179 else if (strcasecmp(u
->profile
, "hsp") == 0)
180 getcaps_req
->transport
= BT_CAPABILITIES_TRANSPORT_SCO
;
182 pa_log_error("invalid profile argument: %s", u
->profile
);
185 getcaps_req
->flags
= 0;
186 getcaps_req
->flags
|= BT_FLAG_AUTOCONNECT
;
188 e
= bt_audioservice_send(u
->audioservice_fd
, &getcaps_req
->h
);
190 pa_log_error("failed to send GETCAPABILITIES_REQ");
194 e
= bt_audioservice_expect(u
->audioservice_fd
, &rsp_hdr
->msg_h
, BT_GETCAPABILITIES_RSP
);
196 pa_log_error("failed to expect for GETCAPABILITIES_RSP");
199 if (rsp_hdr
->posix_errno
!= 0) {
200 pa_log_error("BT_GETCAPABILITIES failed : %s (%d)", pa_cstrerror(rsp_hdr
->posix_errno
), rsp_hdr
->posix_errno
);
201 return -rsp_hdr
->posix_errno
;
204 if ((u
->transport
= getcaps_rsp
->transport
) == BT_CAPABILITIES_TRANSPORT_A2DP
)
205 u
->a2dp
.sbc_capabilities
= getcaps_rsp
->sbc_capabilities
;
210 static uint8_t default_bitpool(uint8_t freq
, uint8_t mode
) {
212 case BT_SBC_SAMPLING_FREQ_16000
:
213 case BT_SBC_SAMPLING_FREQ_32000
:
215 case BT_SBC_SAMPLING_FREQ_44100
:
217 case BT_A2DP_CHANNEL_MODE_MONO
:
218 case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL
:
220 case BT_A2DP_CHANNEL_MODE_STEREO
:
221 case BT_A2DP_CHANNEL_MODE_JOINT_STEREO
:
224 pa_log_warn("Invalid channel mode %u", mode
);
227 case BT_SBC_SAMPLING_FREQ_48000
:
229 case BT_A2DP_CHANNEL_MODE_MONO
:
230 case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL
:
232 case BT_A2DP_CHANNEL_MODE_STEREO
:
233 case BT_A2DP_CHANNEL_MODE_JOINT_STEREO
:
236 pa_log_warn("Invalid channel mode %u", mode
);
240 pa_log_warn("Invalid sampling freq %u", freq
);
245 static int bt_a2dp_init(struct userdata
*u
) {
246 sbc_capabilities_t
*cap
= &u
->a2dp
.sbc_capabilities
;
247 unsigned int max_bitpool
, min_bitpool
, rate
, channels
;
251 cap
->frequency
= BT_SBC_SAMPLING_FREQ_48000
;
254 cap
->frequency
= BT_SBC_SAMPLING_FREQ_44100
;
257 cap
->frequency
= BT_SBC_SAMPLING_FREQ_32000
;
260 cap
->frequency
= BT_SBC_SAMPLING_FREQ_16000
;
263 pa_log_error("Rate %d not supported", rate
);
267 // if (cfg->has_channel_mode)
268 // cap->channel_mode = cfg->channel_mode;
271 if (cap
->channel_mode
& BT_A2DP_CHANNEL_MODE_JOINT_STEREO
)
272 cap
->channel_mode
= BT_A2DP_CHANNEL_MODE_JOINT_STEREO
;
273 else if (cap
->channel_mode
& BT_A2DP_CHANNEL_MODE_STEREO
)
274 cap
->channel_mode
= BT_A2DP_CHANNEL_MODE_STEREO
;
275 else if (cap
->channel_mode
& BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL
)
276 cap
->channel_mode
= BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL
;
278 if (cap
->channel_mode
& BT_A2DP_CHANNEL_MODE_MONO
)
279 cap
->channel_mode
= BT_A2DP_CHANNEL_MODE_MONO
;
282 if (!cap
->channel_mode
) {
283 pa_log_error("No supported channel modes");
287 // if (cfg->has_block_length)
288 // cap->block_length = cfg->block_length;
290 if (cap
->block_length
& BT_A2DP_BLOCK_LENGTH_16
)
291 cap
->block_length
= BT_A2DP_BLOCK_LENGTH_16
;
292 else if (cap
->block_length
& BT_A2DP_BLOCK_LENGTH_12
)
293 cap
->block_length
= BT_A2DP_BLOCK_LENGTH_12
;
294 else if (cap
->block_length
& BT_A2DP_BLOCK_LENGTH_8
)
295 cap
->block_length
= BT_A2DP_BLOCK_LENGTH_8
;
296 else if (cap
->block_length
& BT_A2DP_BLOCK_LENGTH_4
)
297 cap
->block_length
= BT_A2DP_BLOCK_LENGTH_4
;
299 pa_log_error("No supported block lengths");
303 // if (cfg->has_subbands)
304 // cap->subbands = cfg->subbands;
305 if (cap
->subbands
& BT_A2DP_SUBBANDS_8
)
306 cap
->subbands
= BT_A2DP_SUBBANDS_8
;
307 else if (cap
->subbands
& BT_A2DP_SUBBANDS_4
)
308 cap
->subbands
= BT_A2DP_SUBBANDS_4
;
310 pa_log_error("No supported subbands");
314 // if (cfg->has_allocation_method)
315 // cap->allocation_method = cfg->allocation_method;
316 if (cap
->allocation_method
& BT_A2DP_ALLOCATION_LOUDNESS
)
317 cap
->allocation_method
= BT_A2DP_ALLOCATION_LOUDNESS
;
318 else if (cap
->allocation_method
& BT_A2DP_ALLOCATION_SNR
)
319 cap
->allocation_method
= BT_A2DP_ALLOCATION_SNR
;
321 // if (cfg->has_bitpool)
322 // min_bitpool = max_bitpool = cfg->bitpool;
324 min_bitpool
= MAX(MIN_BITPOOL
, cap
->min_bitpool
);
325 max_bitpool
= MIN(default_bitpool(cap
->frequency
, cap
->channel_mode
), cap
->max_bitpool
);
328 cap
->min_bitpool
= min_bitpool
;
329 cap
->max_bitpool
= max_bitpool
;
334 static void bt_a2dp_setup(struct bt_a2dp
*a2dp
) {
335 sbc_capabilities_t active_capabilities
= a2dp
->sbc_capabilities
;
337 if (a2dp
->sbc_initialized
)
338 sbc_reinit(&a2dp
->sbc
, 0);
340 sbc_init(&a2dp
->sbc
, 0);
341 a2dp
->sbc_initialized
= 1;
343 if (active_capabilities
.frequency
& BT_SBC_SAMPLING_FREQ_16000
)
344 a2dp
->sbc
.frequency
= SBC_FREQ_16000
;
346 if (active_capabilities
.frequency
& BT_SBC_SAMPLING_FREQ_32000
)
347 a2dp
->sbc
.frequency
= SBC_FREQ_32000
;
349 if (active_capabilities
.frequency
& BT_SBC_SAMPLING_FREQ_44100
)
350 a2dp
->sbc
.frequency
= SBC_FREQ_44100
;
352 if (active_capabilities
.frequency
& BT_SBC_SAMPLING_FREQ_48000
)
353 a2dp
->sbc
.frequency
= SBC_FREQ_48000
;
355 if (active_capabilities
.channel_mode
& BT_A2DP_CHANNEL_MODE_MONO
)
356 a2dp
->sbc
.mode
= SBC_MODE_MONO
;
358 if (active_capabilities
.channel_mode
& BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL
)
359 a2dp
->sbc
.mode
= SBC_MODE_DUAL_CHANNEL
;
361 if (active_capabilities
.channel_mode
& BT_A2DP_CHANNEL_MODE_STEREO
)
362 a2dp
->sbc
.mode
= SBC_MODE_STEREO
;
364 if (active_capabilities
.channel_mode
& BT_A2DP_CHANNEL_MODE_JOINT_STEREO
)
365 a2dp
->sbc
.mode
= SBC_MODE_JOINT_STEREO
;
367 a2dp
->sbc
.allocation
= active_capabilities
.allocation_method
== BT_A2DP_ALLOCATION_SNR
? SBC_AM_SNR
: SBC_AM_LOUDNESS
;
369 switch (active_capabilities
.subbands
) {
370 case BT_A2DP_SUBBANDS_4
:
371 a2dp
->sbc
.subbands
= SBC_SB_4
;
373 case BT_A2DP_SUBBANDS_8
:
374 a2dp
->sbc
.subbands
= SBC_SB_8
;
378 switch (active_capabilities
.block_length
) {
379 case BT_A2DP_BLOCK_LENGTH_4
:
380 a2dp
->sbc
.blocks
= SBC_BLK_4
;
382 case BT_A2DP_BLOCK_LENGTH_8
:
383 a2dp
->sbc
.blocks
= SBC_BLK_8
;
385 case BT_A2DP_BLOCK_LENGTH_12
:
386 a2dp
->sbc
.blocks
= SBC_BLK_12
;
388 case BT_A2DP_BLOCK_LENGTH_16
:
389 a2dp
->sbc
.blocks
= SBC_BLK_16
;
393 a2dp
->sbc
.bitpool
= active_capabilities
.max_bitpool
;
394 a2dp
->codesize
= sbc_get_codesize(&a2dp
->sbc
);
395 // a2dp->count = sizeof(struct rtp_header) + sizeof(struct rtp_payload);
398 static int bt_setconf(struct userdata
*u
) {
400 char buf
[BT_AUDIO_IPC_PACKET_SIZE
];
401 bt_audio_rsp_msg_header_t
*rsp_hdr
= (void*) buf
;
402 struct bt_setconfiguration_req
*setconf_req
= (void*) buf
;
403 struct bt_setconfiguration_rsp
*setconf_rsp
= (void*) buf
;
405 if (u
->transport
== BT_CAPABILITIES_TRANSPORT_A2DP
) {
408 pa_log_error("a2dp_init error");
413 memset(setconf_req
, 0, BT_AUDIO_IPC_PACKET_SIZE
);
414 setconf_req
->h
.msg_type
= BT_SETCONFIGURATION_REQ
;
415 strncpy(setconf_req
->device
, u
->addr
, 18);
416 setconf_req
->transport
= u
->transport
;
417 if (u
->transport
== BT_CAPABILITIES_TRANSPORT_A2DP
)
418 setconf_req
->sbc_capabilities
= u
->a2dp
.sbc_capabilities
;
419 setconf_req
->access_mode
= BT_CAPABILITIES_ACCESS_MODE_WRITE
;
421 e
= bt_audioservice_send(u
->audioservice_fd
, &setconf_req
->h
);
423 pa_log_error("failed to send BT_SETCONFIGURATION_REQ");
427 e
= bt_audioservice_expect(u
->audioservice_fd
, &rsp_hdr
->msg_h
, BT_SETCONFIGURATION_RSP
);
429 pa_log_error("failed to expect BT_SETCONFIGURATION_RSP");
433 if (rsp_hdr
->posix_errno
!= 0) {
434 pa_log_error("BT_SETCONFIGURATION failed : %s(%d)", pa_cstrerror(rsp_hdr
->posix_errno
), rsp_hdr
->posix_errno
);
435 return -rsp_hdr
->posix_errno
;
438 u
->transport
= setconf_rsp
->transport
;
439 u
->link_mtu
= setconf_rsp
->link_mtu
;
440 if (u
->transport
== BT_CAPABILITIES_TRANSPORT_A2DP
)
441 /* TODO: Check the max possible size */ u
->block_size
= u
->link_mtu
/ 2;
443 u
->block_size
= u
->link_mtu
;
445 /* setup SBC encoder now we agree on parameters */
446 if (u
->transport
== BT_CAPABILITIES_TRANSPORT_A2DP
) {
447 bt_a2dp_setup(&u
->a2dp
);
448 pa_log
/*debug*/("\tallocation=%u\n\tsubbands=%u\n\tblocks=%u\n\tbitpool=%u\n",
449 u
->a2dp
.sbc
.allocation
, u
->a2dp
.sbc
.subbands
, u
->a2dp
.sbc
.blocks
, u
->a2dp
.sbc
.bitpool
);
455 static int bt_getstreamfd(struct userdata
*u
) {
457 char buf
[BT_AUDIO_IPC_PACKET_SIZE
];
458 struct bt_streamstart_req
*start_req
= (void*) buf
;
459 bt_audio_rsp_msg_header_t
*rsp_hdr
= (void*) buf
;
460 struct bt_streamfd_ind
*streamfd_ind
= (void*) buf
;
462 memset(start_req
, 0, BT_AUDIO_IPC_PACKET_SIZE
);
463 start_req
->h
.msg_type
= BT_STREAMSTART_REQ
;
465 e
= bt_audioservice_send(u
->audioservice_fd
, &start_req
->h
);
467 pa_log_error("failed to send BT_STREAMSTART_REQ");
471 e
= bt_audioservice_expect(u
->audioservice_fd
, &rsp_hdr
->msg_h
, BT_STREAMSTART_RSP
);
473 pa_log_error("failed to expect BT_STREAMSTART_RSP");
477 if (rsp_hdr
->posix_errno
!= 0) {
478 pa_log_error("BT_START failed : %s(%d)", pa_cstrerror(rsp_hdr
->posix_errno
), rsp_hdr
->posix_errno
);
479 return -rsp_hdr
->posix_errno
;
482 e
= bt_audioservice_expect(u
->audioservice_fd
, &streamfd_ind
->h
, BT_STREAMFD_IND
);
484 pa_log_error("failed to expect BT_STREAMFD_IND");
488 if (u
->stream_fd
>= 0)
489 pa_close(u
->stream_fd
);
491 u
->stream_fd
= bt_audio_service_get_data_fd(u
->audioservice_fd
);
492 if (u
->stream_fd
< 0) {
493 pa_log_error("failed to get data fd");
497 // if (u->transport == BT_CAPABILITIES_TRANSPORT_A2DP) {
498 // opt_name = SO_SNDTIMEO;
499 // if (setsockopt(u->stream_fd, SOL_SOCKET, opt_name, &t, sizeof(t)) < 0) {
500 // pa_log_error("failed to set socket options for A2DP");
505 // opt_name = SCO_TXBUFS;
506 // if (setsockopt(u->stream_fd, SOL_SCO, opt_name, &period_count, sizeof(period_count)) == 0)
508 // opt_name = SO_SNDBUF;
509 // if (setsockopt(u->stream_fd, SOL_SCO, opt_name, &period_count, sizeof(period_count)) == 0)
511 // /* FIXME : handle error codes */
513 pa_make_fd_nonblock(u
->stream_fd
);
518 static int bt_hw_constraint(struct userdata
*u
) {
520 u
->ss
.format
= PA_SAMPLE_S16LE
;
526 static int sink_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
527 struct userdata
*u
= PA_SINK(o
)->userdata
;
531 case PA_SINK_MESSAGE_SET_STATE
:
532 switch ((pa_sink_state_t
) PA_PTR_TO_UINT(data
)) {
533 case PA_SINK_SUSPENDED
:
534 pa_assert(PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
));
535 pa_smoother_pause(u
->smoother
, pa_rtclock_usec());
538 case PA_SINK_RUNNING
:
539 if (u
->sink
->thread_info
.state
== PA_SINK_SUSPENDED
)
540 pa_smoother_resume(u
->smoother
, pa_rtclock_usec());
542 case PA_SINK_UNLINKED
:
548 case PA_SINK_MESSAGE_GET_LATENCY
: {
550 r
= pa_smoother_get(u
->smoother
, pa_rtclock_usec());
551 w
= pa_bytes_to_usec(u
->offset
+ u
->memchunk
.length
, &u
->sink
->sample_spec
);
552 *((pa_usec_t
*) data
) = w
> r
? w
- r
: 0;
558 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
561 static void thread_func(void *userdata
) {
562 struct userdata
*u
= userdata
;
567 pa_log
/*_debug*/("Thread starting up");
569 pa_thread_mq_install(&u
->thread_mq
);
570 pa_rtpoll_install(u
->rtpoll
);
572 pa_smoother_set_time_offset(u
->smoother
, pa_rtclock_usec());
576 struct pollfd
*pollfd
;
578 if (PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
)) {
579 if (u
->sink
->thread_info
.rewind_requested
) {
580 pa_log("rewind_requested");
581 pa_sink_process_rewind(u
->sink
, 0);
582 pa_log("rewind_finished");
586 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
588 /* Render some data and write it to the fifo */
589 if (PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
) && pollfd
->revents
) {
592 pa_log("Render some data and write it to the fifo");
598 u
->memchunk
.memblock
= pa_memblock_new(u
->mempool
, u
->block_size
);
599 pa_log("memblock allocated");
600 u
->memchunk
.length
= pa_memblock_get_length(u
->memchunk
.memblock
);
601 pa_log("memchunk length");
602 pa_sink_render_into_full(u
->sink
, &u
->memchunk
);
605 pa_assert(u
->memchunk
.length
> 0);
607 p
= pa_memblock_acquire(u
->memchunk
.memblock
);
608 pa_log("memblock acquired");
609 l
= pa_write(u
->stream_fd
, (uint8_t*) p
, u
->memchunk
.length
, &write_type
);
610 pa_log("memblock written");
611 pa_memblock_release(u
->memchunk
.memblock
);
612 pa_log("memblock released");
617 pa_log("l = %d < 0", l
);
620 else if (errno
== EAGAIN
)
623 pa_log("Failed to write data to FIFO: %s", pa_cstrerror(errno
));
627 pa_log("l = %d >= 0", l
);
629 pa_memblock_unref(u
->memchunk
.memblock
);
630 pa_log("memblock unrefered");
631 pa_memchunk_reset(&u
->memchunk
);
632 pa_log("memchunk reseted");
640 /* At this spot we know that the socket buffers are fully filled up.
641 * This is the best time to estimate the playback position of the server */
647 // if (ioctl(u->fd, SIOCOUTQ, &l) >= 0 && l > 0)
652 usec
= pa_bytes_to_usec(n
, &u
->sink
->sample_spec
);
653 if (usec
> u
->latency
)
657 pa_smoother_put(u
->smoother
, pa_rtclock_usec(), usec
);
660 /* Hmm, nothing to do. Let's sleep */
661 pa_log("let's sleep");
662 pollfd
->events
= PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
) ? POLLOUT
: 0;
664 if ((ret
= pa_rtpoll_run(u
->rtpoll
, TRUE
)) < 0) {
675 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
676 if (pollfd
->revents
& ~POLLOUT
) {
677 pa_log("FIFO shutdown.");
683 /* If this was no regular exit from the loop we have to continue processing messages until we receive 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
;
697 struct pollfd
*pollfd
;
701 m
->userdata
= u
= pa_xnew0(struct userdata
, 1);
704 u
->audioservice_fd
= -1;
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
);
769 u
->rtpoll_item
= pa_rtpoll_item_new(u
->rtpoll
, PA_RTPOLL_NEVER
, 1);
770 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
771 pollfd
->fd
= u
->stream_fd
;
772 pollfd
->events
= pollfd
->revents
= 0;
774 /* configure hw supported sample specs */
775 e
= bt_hw_constraint(u
);
777 pa_log_error("failed to configure sample spec");
782 pa_sink_new_data_init(&data
);
783 data
.driver
= __FILE__
;
785 pa_sink_new_data_set_name(&data
, u
->name
);
786 pa_sink_new_data_set_sample_spec(&data
, &u
->ss
);
787 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_STRING
, u
->name
);
788 pa_proplist_setf(data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, "Bluetooth sink '%s' (%s)", u
->name
, u
->addr
);
789 u
->sink
= pa_sink_new(m
->core
, &data
, PA_SINK_HARDWARE
|PA_SINK_LATENCY
|PA_SINK_NETWORK
);
790 pa_sink_new_data_done(&data
);
792 pa_log_error("failed to create sink");
795 u
->sink
->userdata
= u
;
796 u
->sink
->parent
.process_msg
= sink_process_msg
;
797 pa_sink_set_asyncmsgq(u
->sink
, u
->thread_mq
.inq
);
798 pa_sink_set_rtpoll(u
->sink
, u
->rtpoll
);
800 /* start rt thread */
801 if (!(u
->thread
= pa_thread_new(thread_func
, u
))) {
802 pa_log_error("failed to create thread");
805 pa_sink_put(u
->sink
);
817 void pa__done(pa_module
*m
) {
818 pa_log("Unloading module-bt-device");