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
29 #include <sys/ioctl.h>
30 #include <linux/sockios.h>
32 #include <pulse/xmalloc.h>
33 #include <pulse/timeval.h>
34 #include <pulse/sample.h>
35 #include <pulsecore/module.h>
36 #include <pulsecore/modargs.h>
37 #include <pulsecore/core-util.h>
38 #include <pulsecore/core-error.h>
39 #include <pulsecore/socket-util.h>
40 #include <pulsecore/thread.h>
41 #include <pulsecore/thread-mq.h>
42 #include <pulsecore/rtpoll.h>
43 #include <pulsecore/time-smoother.h>
44 #include <pulsecore/rtclock.h>
46 #include "dbus-util.h"
47 #include "module-bt-device-symdef.h"
52 #define DEFAULT_SINK_NAME "bluetooth_sink"
53 #define BUFFER_SIZE 2048
54 #define MAX_BITPOOL 64
57 #define SCO_TXBUFS 0x03
58 #define SCO_RXBUFS 0x04
60 PA_MODULE_AUTHOR("Joao Paulo Rechi Vita");
61 PA_MODULE_DESCRIPTION("Bluetooth audio sink and source");
62 PA_MODULE_VERSION(PACKAGE_VERSION
);
63 PA_MODULE_LOAD_ONCE(FALSE
);
65 "name=<name of the device> "
66 "addr=<address of the device> "
67 "profile=<a2dp|hsp>");
70 sbc_capabilities_t sbc_capabilities
;
71 sbc_t sbc
; /* Codec data */
72 pa_bool_t sbc_initialized
; /* Keep track if the encoder is initialized */
73 int codesize
; /* SBC codesize */
74 int samples
; /* Number of encoded samples */
75 uint8_t buffer
[BUFFER_SIZE
]; /* Codec transfer buffer */
76 int count
; /* Codec transfer buffer counter */
78 int nsamples
; /* Cumulative number of codec samples */
79 uint16_t seq_num
; /* Cumulative packet sequence */
80 int frame_count
; /* Current frames in buffer*/
88 pa_thread_mq thread_mq
;
90 pa_rtpoll_item
*rtpoll_item
;
94 pa_smoother
*smoother
;
116 static const char* const valid_modargs
[] = {
125 static int bt_audioservice_send(int sk
, const bt_audio_msg_header_t
*msg
) {
127 pa_log_debug("sending %s", bt_audio_strmsg(msg
->msg_type
));
128 if (send(sk
, msg
, BT_AUDIO_IPC_PACKET_SIZE
, 0) > 0)
132 pa_log_error("Error sending data to audio service: %s(%d)", pa_cstrerror(errno
), errno
);
137 static int bt_audioservice_recv(int sk
, bt_audio_msg_header_t
*inmsg
) {
141 pa_log_debug("trying to receive msg from audio service...");
142 if (recv(sk
, inmsg
, BT_AUDIO_IPC_PACKET_SIZE
, 0) > 0) {
143 type
= bt_audio_strmsg(inmsg
->msg_type
);
145 pa_log_debug("Received %s", type
);
150 pa_log_error("Bogus message type %d received from audio service", inmsg
->msg_type
);
155 pa_log_error("Error receiving data from audio service: %s(%d)", pa_cstrerror(errno
), errno
);
161 static int bt_audioservice_expect(int sk
, bt_audio_msg_header_t
*rsp_hdr
, int expected_type
) {
162 int e
= bt_audioservice_recv(sk
, rsp_hdr
);
164 if (rsp_hdr
->msg_type
!= expected_type
) {
166 pa_log_error("Bogus message %s received while %s was expected", bt_audio_strmsg(rsp_hdr
->msg_type
),
167 bt_audio_strmsg(expected_type
));
173 static int bt_getcaps(struct userdata
*u
) {
176 bt_audio_rsp_msg_header_t rsp_hdr
;
177 struct bt_getcapabilities_req getcaps_req
;
178 struct bt_getcapabilities_rsp getcaps_rsp
;
179 uint8_t buf
[BT_AUDIO_IPC_PACKET_SIZE
];
182 memset(msg
.buf
, 0, BT_AUDIO_IPC_PACKET_SIZE
);
183 msg
.getcaps_req
.h
.msg_type
= BT_GETCAPABILITIES_REQ
;
184 strncpy(msg
.getcaps_req
.device
, u
->addr
, 18);
185 if (strcasecmp(u
->profile
, "a2dp") == 0)
186 msg
.getcaps_req
.transport
= BT_CAPABILITIES_TRANSPORT_A2DP
;
187 else if (strcasecmp(u
->profile
, "hsp") == 0)
188 msg
.getcaps_req
.transport
= BT_CAPABILITIES_TRANSPORT_SCO
;
190 pa_log_error("invalid profile argument: %s", u
->profile
);
193 msg
.getcaps_req
.flags
= BT_FLAG_AUTOCONNECT
;
195 e
= bt_audioservice_send(u
->audioservice_fd
, &msg
.getcaps_req
.h
);
197 pa_log_error("failed to send GETCAPABILITIES_REQ");
201 e
= bt_audioservice_expect(u
->audioservice_fd
, &msg
.rsp_hdr
.msg_h
, BT_GETCAPABILITIES_RSP
);
203 pa_log_error("failed to expect for GETCAPABILITIES_RSP");
206 if (msg
.rsp_hdr
.posix_errno
!= 0) {
207 pa_log_error("BT_GETCAPABILITIES failed : %s (%d)", pa_cstrerror(msg
.rsp_hdr
.posix_errno
), msg
.rsp_hdr
.posix_errno
);
208 return -msg
.rsp_hdr
.posix_errno
;
211 if ((u
->transport
= msg
.getcaps_rsp
.transport
) == BT_CAPABILITIES_TRANSPORT_A2DP
)
212 u
->a2dp
.sbc_capabilities
= msg
.getcaps_rsp
.sbc_capabilities
;
217 static uint8_t default_bitpool(uint8_t freq
, uint8_t mode
) {
219 case BT_SBC_SAMPLING_FREQ_16000
:
220 case BT_SBC_SAMPLING_FREQ_32000
:
222 case BT_SBC_SAMPLING_FREQ_44100
:
224 case BT_A2DP_CHANNEL_MODE_MONO
:
225 case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL
:
227 case BT_A2DP_CHANNEL_MODE_STEREO
:
228 case BT_A2DP_CHANNEL_MODE_JOINT_STEREO
:
231 pa_log_warn("Invalid channel mode %u", mode
);
234 case BT_SBC_SAMPLING_FREQ_48000
:
236 case BT_A2DP_CHANNEL_MODE_MONO
:
237 case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL
:
239 case BT_A2DP_CHANNEL_MODE_STEREO
:
240 case BT_A2DP_CHANNEL_MODE_JOINT_STEREO
:
243 pa_log_warn("Invalid channel mode %u", mode
);
247 pa_log_warn("Invalid sampling freq %u", freq
);
252 static int bt_a2dp_init(struct userdata
*u
) {
253 sbc_capabilities_t
*cap
= &u
->a2dp
.sbc_capabilities
;
254 unsigned int max_bitpool
, min_bitpool
;
256 switch (u
->ss
.rate
) {
258 cap
->frequency
= BT_SBC_SAMPLING_FREQ_48000
;
261 cap
->frequency
= BT_SBC_SAMPLING_FREQ_44100
;
264 cap
->frequency
= BT_SBC_SAMPLING_FREQ_32000
;
267 cap
->frequency
= BT_SBC_SAMPLING_FREQ_16000
;
270 pa_log_error("Rate %d not supported", u
->ss
.rate
);
274 // if (cfg->has_channel_mode)
275 // cap->channel_mode = cfg->channel_mode;
277 if (u
->ss
.channels
== 2) {
278 if (cap
->channel_mode
& BT_A2DP_CHANNEL_MODE_JOINT_STEREO
)
279 cap
->channel_mode
= BT_A2DP_CHANNEL_MODE_JOINT_STEREO
;
280 else if (cap
->channel_mode
& BT_A2DP_CHANNEL_MODE_STEREO
)
281 cap
->channel_mode
= BT_A2DP_CHANNEL_MODE_STEREO
;
282 else if (cap
->channel_mode
& BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL
)
283 cap
->channel_mode
= BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL
;
285 if (cap
->channel_mode
& BT_A2DP_CHANNEL_MODE_MONO
)
286 cap
->channel_mode
= BT_A2DP_CHANNEL_MODE_MONO
;
289 if (!cap
->channel_mode
) {
290 pa_log_error("No supported channel modes");
294 // if (cfg->has_block_length)
295 // cap->block_length = cfg->block_length;
297 if (cap
->block_length
& BT_A2DP_BLOCK_LENGTH_16
)
298 cap
->block_length
= BT_A2DP_BLOCK_LENGTH_16
;
299 else if (cap
->block_length
& BT_A2DP_BLOCK_LENGTH_12
)
300 cap
->block_length
= BT_A2DP_BLOCK_LENGTH_12
;
301 else if (cap
->block_length
& BT_A2DP_BLOCK_LENGTH_8
)
302 cap
->block_length
= BT_A2DP_BLOCK_LENGTH_8
;
303 else if (cap
->block_length
& BT_A2DP_BLOCK_LENGTH_4
)
304 cap
->block_length
= BT_A2DP_BLOCK_LENGTH_4
;
306 pa_log_error("No supported block lengths");
310 // if (cfg->has_subbands)
311 // cap->subbands = cfg->subbands;
312 if (cap
->subbands
& BT_A2DP_SUBBANDS_8
)
313 cap
->subbands
= BT_A2DP_SUBBANDS_8
;
314 else if (cap
->subbands
& BT_A2DP_SUBBANDS_4
)
315 cap
->subbands
= BT_A2DP_SUBBANDS_4
;
317 pa_log_error("No supported subbands");
321 // if (cfg->has_allocation_method)
322 // cap->allocation_method = cfg->allocation_method;
323 if (cap
->allocation_method
& BT_A2DP_ALLOCATION_LOUDNESS
)
324 cap
->allocation_method
= BT_A2DP_ALLOCATION_LOUDNESS
;
325 else if (cap
->allocation_method
& BT_A2DP_ALLOCATION_SNR
)
326 cap
->allocation_method
= BT_A2DP_ALLOCATION_SNR
;
328 // if (cfg->has_bitpool)
329 // min_bitpool = max_bitpool = cfg->bitpool;
331 min_bitpool
= PA_MAX(MIN_BITPOOL
, cap
->min_bitpool
);
332 max_bitpool
= PA_MIN(default_bitpool(cap
->frequency
, cap
->channel_mode
), cap
->max_bitpool
);
335 cap
->min_bitpool
= min_bitpool
;
336 cap
->max_bitpool
= max_bitpool
;
341 static void bt_a2dp_setup(struct bt_a2dp
*a2dp
) {
342 sbc_capabilities_t active_capabilities
= a2dp
->sbc_capabilities
;
344 if (a2dp
->sbc_initialized
)
345 sbc_reinit(&a2dp
->sbc
, 0);
347 sbc_init(&a2dp
->sbc
, 0);
348 a2dp
->sbc_initialized
= TRUE
;
350 if (active_capabilities
.frequency
& BT_SBC_SAMPLING_FREQ_16000
)
351 a2dp
->sbc
.frequency
= SBC_FREQ_16000
;
353 if (active_capabilities
.frequency
& BT_SBC_SAMPLING_FREQ_32000
)
354 a2dp
->sbc
.frequency
= SBC_FREQ_32000
;
356 if (active_capabilities
.frequency
& BT_SBC_SAMPLING_FREQ_44100
)
357 a2dp
->sbc
.frequency
= SBC_FREQ_44100
;
359 if (active_capabilities
.frequency
& BT_SBC_SAMPLING_FREQ_48000
)
360 a2dp
->sbc
.frequency
= SBC_FREQ_48000
;
362 if (active_capabilities
.channel_mode
& BT_A2DP_CHANNEL_MODE_MONO
)
363 a2dp
->sbc
.mode
= SBC_MODE_MONO
;
365 if (active_capabilities
.channel_mode
& BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL
)
366 a2dp
->sbc
.mode
= SBC_MODE_DUAL_CHANNEL
;
368 if (active_capabilities
.channel_mode
& BT_A2DP_CHANNEL_MODE_STEREO
)
369 a2dp
->sbc
.mode
= SBC_MODE_STEREO
;
371 if (active_capabilities
.channel_mode
& BT_A2DP_CHANNEL_MODE_JOINT_STEREO
)
372 a2dp
->sbc
.mode
= SBC_MODE_JOINT_STEREO
;
374 a2dp
->sbc
.allocation
= (active_capabilities
.allocation_method
== BT_A2DP_ALLOCATION_SNR
? SBC_AM_SNR
: SBC_AM_LOUDNESS
);
376 switch (active_capabilities
.subbands
) {
377 case BT_A2DP_SUBBANDS_4
:
378 a2dp
->sbc
.subbands
= SBC_SB_4
;
380 case BT_A2DP_SUBBANDS_8
:
381 a2dp
->sbc
.subbands
= SBC_SB_8
;
385 switch (active_capabilities
.block_length
) {
386 case BT_A2DP_BLOCK_LENGTH_4
:
387 a2dp
->sbc
.blocks
= SBC_BLK_4
;
389 case BT_A2DP_BLOCK_LENGTH_8
:
390 a2dp
->sbc
.blocks
= SBC_BLK_8
;
392 case BT_A2DP_BLOCK_LENGTH_12
:
393 a2dp
->sbc
.blocks
= SBC_BLK_12
;
395 case BT_A2DP_BLOCK_LENGTH_16
:
396 a2dp
->sbc
.blocks
= SBC_BLK_16
;
400 a2dp
->sbc
.bitpool
= active_capabilities
.max_bitpool
;
401 a2dp
->codesize
= sbc_get_codesize(&a2dp
->sbc
);
402 a2dp
->count
= sizeof(struct rtp_header
) + sizeof(struct rtp_payload
);
405 static int bt_setconf(struct userdata
*u
) {
408 bt_audio_rsp_msg_header_t rsp_hdr
;
409 struct bt_setconfiguration_req setconf_req
;
410 struct bt_setconfiguration_rsp setconf_rsp
;
411 uint8_t buf
[BT_AUDIO_IPC_PACKET_SIZE
];
414 if (u
->transport
== BT_CAPABILITIES_TRANSPORT_A2DP
) {
417 pa_log_error("a2dp_init error");
420 u
->ss
.format
= PA_SAMPLE_S16LE
;
423 u
->ss
.format
= PA_SAMPLE_U8
;
425 memset(msg
.buf
, 0, BT_AUDIO_IPC_PACKET_SIZE
);
426 msg
.setconf_req
.h
.msg_type
= BT_SETCONFIGURATION_REQ
;
427 strncpy(msg
.setconf_req
.device
, u
->addr
, 18);
428 msg
.setconf_req
.transport
= u
->transport
;
429 if (u
->transport
== BT_CAPABILITIES_TRANSPORT_A2DP
)
430 msg
.setconf_req
.sbc_capabilities
= u
->a2dp
.sbc_capabilities
;
431 msg
.setconf_req
.access_mode
= BT_CAPABILITIES_ACCESS_MODE_WRITE
;
433 e
= bt_audioservice_send(u
->audioservice_fd
, &msg
.setconf_req
.h
);
435 pa_log_error("failed to send BT_SETCONFIGURATION_REQ");
439 e
= bt_audioservice_expect(u
->audioservice_fd
, &msg
.rsp_hdr
.msg_h
, BT_SETCONFIGURATION_RSP
);
441 pa_log_error("failed to expect BT_SETCONFIGURATION_RSP");
445 if (msg
.rsp_hdr
.posix_errno
!= 0) {
446 pa_log_error("BT_SETCONFIGURATION failed : %s(%d)", pa_cstrerror(msg
.rsp_hdr
.posix_errno
), msg
.rsp_hdr
.posix_errno
);
447 return -msg
.rsp_hdr
.posix_errno
;
450 u
->transport
= msg
.setconf_rsp
.transport
;
451 u
->strtransport
= (u
->transport
== BT_CAPABILITIES_TRANSPORT_A2DP
? pa_xstrdup("A2DP") : pa_xstrdup("SCO"));
452 u
->link_mtu
= msg
.setconf_rsp
.link_mtu
;
454 /* setup SBC encoder now we agree on parameters */
455 if (u
->transport
== BT_CAPABILITIES_TRANSPORT_A2DP
) {
456 bt_a2dp_setup(&u
->a2dp
);
457 u
->block_size
= u
->a2dp
.codesize
;
458 pa_log_info("sbc parameters:\n\tallocation=%u\n\tsubbands=%u\n\tblocks=%u\n\tbitpool=%u\n",
459 u
->a2dp
.sbc
.allocation
, u
->a2dp
.sbc
.subbands
, u
->a2dp
.sbc
.blocks
, u
->a2dp
.sbc
.bitpool
);
462 u
->block_size
= u
->link_mtu
;
467 static int bt_getstreamfd(struct userdata
*u
) {
469 // uint32_t period_count = io->buffer_size / io->period_size;
471 bt_audio_rsp_msg_header_t rsp_hdr
;
472 struct bt_streamstart_req start_req
;
473 struct bt_streamfd_ind streamfd_ind
;
474 uint8_t buf
[BT_AUDIO_IPC_PACKET_SIZE
];
477 memset(msg
.buf
, 0, BT_AUDIO_IPC_PACKET_SIZE
);
478 msg
.start_req
.h
.msg_type
= BT_STREAMSTART_REQ
;
480 e
= bt_audioservice_send(u
->audioservice_fd
, &msg
.start_req
.h
);
482 pa_log_error("failed to send BT_STREAMSTART_REQ");
486 e
= bt_audioservice_expect(u
->audioservice_fd
, &msg
.rsp_hdr
.msg_h
, BT_STREAMSTART_RSP
);
488 pa_log_error("failed to expect BT_STREAMSTART_RSP");
492 if (msg
.rsp_hdr
.posix_errno
!= 0) {
493 pa_log_error("BT_START failed : %s(%d)", pa_cstrerror(msg
.rsp_hdr
.posix_errno
), msg
.rsp_hdr
.posix_errno
);
494 return -msg
.rsp_hdr
.posix_errno
;
497 e
= bt_audioservice_expect(u
->audioservice_fd
, &msg
.streamfd_ind
.h
, BT_STREAMFD_IND
);
499 pa_log_error("failed to expect BT_STREAMFD_IND");
503 if (u
->stream_fd
>= 0)
504 pa_close(u
->stream_fd
);
506 u
->stream_fd
= bt_audio_service_get_data_fd(u
->audioservice_fd
);
507 if (u
->stream_fd
< 0) {
508 pa_log_error("failed to get data fd: %s (%d)",pa_cstrerror(errno
), errno
);
512 if (u
->transport
== BT_CAPABILITIES_TRANSPORT_A2DP
) {
513 if (pa_socket_set_sndbuf(u
->stream_fd
, 10*u
->link_mtu
) < 0) {
514 pa_log_error("failed to set socket options for A2DP: %s (%d)",pa_cstrerror(errno
), errno
);
519 // if (setsockopt(u->stream_fd, SOL_SCO, SCO_TXBUFS, &period_count, sizeof(period_count)) == 0)
521 // if (setsockopt(u->stream_fd, SOL_SCO, SO_SNDBUF, &period_count, sizeof(period_count)) == 0)
523 // /* FIXME : handle error codes */
524 pa_make_fd_nonblock(u
->stream_fd
);
525 // pa_make_socket_low_delay(u->stream_fd);
530 static int sink_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
531 struct userdata
*u
= PA_SINK(o
)->userdata
;
533 pa_log_debug("got message: %d", code
);
536 case PA_SINK_MESSAGE_SET_STATE
:
537 switch ((pa_sink_state_t
) PA_PTR_TO_UINT(data
)) {
538 case PA_SINK_SUSPENDED
:
539 pa_assert(PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
));
540 pa_smoother_pause(u
->smoother
, pa_rtclock_usec());
543 case PA_SINK_RUNNING
:
544 if (u
->sink
->thread_info
.state
== PA_SINK_SUSPENDED
)
545 pa_smoother_resume(u
->smoother
, pa_rtclock_usec());
547 case PA_SINK_UNLINKED
:
553 case PA_SINK_MESSAGE_GET_LATENCY
: {
555 r
= pa_smoother_get(u
->smoother
, pa_rtclock_usec());
556 w
= pa_bytes_to_usec(u
->offset
+ u
->memchunk
.length
, &u
->sink
->sample_spec
);
557 *((pa_usec_t
*) data
) = w
> r
? w
- r
: 0;
563 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
566 static void sco_thread_func(void *userdata
) {
567 struct userdata
*u
= userdata
;
572 pa_log_debug("SCO thread starting up");
574 pa_thread_mq_install(&u
->thread_mq
);
575 pa_rtpoll_install(u
->rtpoll
);
577 pa_smoother_set_time_offset(u
->smoother
, pa_rtclock_usec());
581 struct pollfd
*pollfd
;
583 if (PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
)) {
584 if (u
->sink
->thread_info
.rewind_requested
) {
585 pa_sink_process_rewind(u
->sink
, 0);
589 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
591 if (PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
) && pollfd
->revents
) {
597 /* Render some data and write it to the fifo */
600 u
->memchunk
.memblock
= pa_memblock_new(u
->mempool
, u
->block_size
);
601 pa_log_debug("memblock asked size %d", u
->block_size
);
602 u
->memchunk
.length
= pa_memblock_get_length(u
->memchunk
.memblock
);
603 pa_log_debug("memchunk length %d", u
->memchunk
.length
);
604 pa_sink_render_into_full(u
->sink
, &u
->memchunk
);
606 pa_assert(u
->memchunk
.length
> 0);
608 p
= pa_memblock_acquire(u
->memchunk
.memblock
);
610 l
= pa_write(u
->stream_fd
, (uint8_t*) p
, u
->memchunk
.length
, &write_type
);
611 pa_log_debug("memblock written to socket: %d bytes", l
);
612 pa_memblock_release(u
->memchunk
.memblock
);
613 pa_memblock_unref(u
->memchunk
.memblock
);
614 pa_memchunk_reset(&u
->memchunk
);
615 pa_log("memchunk reseted");
620 if (errno
== EINTR
) {
621 pa_log_debug("EINTR");
624 else if (errno
== EAGAIN
) {
625 pa_log_debug("EAGAIN");
629 pa_log("Failed to write data to FIFO: %s", pa_cstrerror(errno
));
640 if (ioctl(u
->stream_fd
, SIOCOUTQ
, &l
) >= 0 && l
> 0)
642 usec
= pa_bytes_to_usec(n
, &u
->sink
->sample_spec
);
643 if (usec
> u
->latency
)
647 pa_smoother_put(u
->smoother
, pa_rtclock_usec(), usec
);
650 /* Hmm, nothing to do. Let's sleep */
651 pa_log_debug("SCO thread going to sleep");
652 pollfd
->events
= PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
) ? POLLOUT
: 0;
653 if ((ret
= pa_rtpoll_run(u
->rtpoll
, TRUE
)) < 0) {
654 pa_log("rtpoll_run < 0");
657 pa_log_debug("SCO thread waking up");
660 pa_log_debug("rtpoll_run == 0");
664 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
665 if (pollfd
->revents
& ~POLLOUT
) {
666 pa_log_error("FIFO shutdown.");
672 /* If this was no regular exit from the loop we have to continue processing messages until we receive PA_MESSAGE_SHUTDOWN */
673 pa_log_debug("SCO thread failed");
674 pa_asyncmsgq_post(u
->thread_mq
.outq
, PA_MSGOBJECT(u
->core
), PA_CORE_MESSAGE_UNLOAD_MODULE
, u
->module
, 0, NULL
, NULL
);
675 pa_asyncmsgq_wait_for(u
->thread_mq
.inq
, PA_MESSAGE_SHUTDOWN
);
678 pa_log_debug("SCO thread shutting down");
681 static int a2dp_process_render(struct userdata
*u
) {
683 int write_type
= 0, written
;
684 struct bt_a2dp
*a2dp
= &u
->a2dp
;
685 struct rtp_header
*header
= (void *) a2dp
->buffer
;
686 struct rtp_payload
*payload
= (void *) (a2dp
->buffer
+ sizeof(*header
));
691 /* Render some data */
692 int frame_size
, encoded
;
695 u
->memchunk
.memblock
= pa_memblock_new(u
->mempool
, u
->block_size
);
696 pa_log_debug("memblock asked size %d", u
->block_size
);
697 u
->memchunk
.length
= pa_memblock_get_length(u
->memchunk
.memblock
);
698 pa_log_debug("memchunk length %d", u
->memchunk
.length
);
699 pa_sink_render_into_full(u
->sink
, &u
->memchunk
);
701 pa_assert(u
->memchunk
.length
> 0);
703 p
= pa_memblock_acquire(u
->memchunk
.memblock
);
704 frame_size
= sbc_get_frame_length(&a2dp
->sbc
);
705 pa_log_debug("SBC frame_size: %d", frame_size
);
707 encoded
= sbc_encode(&a2dp
->sbc
, (uint8_t*) p
, a2dp
->codesize
, a2dp
->buffer
+ a2dp
->count
,
708 sizeof(a2dp
->buffer
) - a2dp
->count
, &written
);
709 pa_log_debug("SBC: encoded: %d; written: %d", encoded
, written
);
711 pa_log_error("SBC encoding error (%d)", encoded
);
714 pa_memblock_release(u
->memchunk
.memblock
);
715 pa_memblock_unref(u
->memchunk
.memblock
);
716 pa_memchunk_reset(&u
->memchunk
);
717 pa_log_debug("memchunk reseted");
719 a2dp
->count
+= written
;
721 a2dp
->samples
+= encoded
/ frame_size
;
722 a2dp
->nsamples
+= encoded
/ frame_size
;
724 } while (a2dp
->count
+ written
<= u
->link_mtu
);
726 /* write it to the fifo */
727 memset(a2dp
->buffer
, 0, sizeof(*header
) + sizeof(*payload
));
728 payload
->frame_count
= a2dp
->frame_count
;
731 header
->sequence_number
= htons(a2dp
->seq_num
);
732 header
->timestamp
= htonl(a2dp
->nsamples
);
733 header
->ssrc
= htonl(1);
736 l
= pa_write(u
->stream_fd
, a2dp
->buffer
, a2dp
->count
, write_type
);
737 pa_log_debug("avdtp_write: requested %d bytes; written %d bytes", a2dp
->count
, l
);
742 if (errno
== EINTR
) {
743 pa_log_debug("EINTR");
746 else if (errno
== EAGAIN
) {
747 pa_log_debug("EAGAIN");
751 pa_log_error("Failed to write data to FIFO: %s", pa_cstrerror(errno
));
756 u
->offset
+= a2dp
->codesize
*a2dp
->frame_count
;
758 /* Reset buffer of data to send */
759 a2dp
->count
= sizeof(struct rtp_header
) + sizeof(struct rtp_payload
);
760 a2dp
->frame_count
= 0;
767 static void a2dp_thread_func(void *userdata
) {
768 struct userdata
*u
= userdata
;
772 pa_log_debug("A2DP Thread starting up");
774 pa_thread_mq_install(&u
->thread_mq
);
775 pa_rtpoll_install(u
->rtpoll
);
777 pa_smoother_set_time_offset(u
->smoother
, pa_rtclock_usec());
781 struct pollfd
*pollfd
;
785 if (PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
)) {
786 if (u
->sink
->thread_info
.rewind_requested
) {
787 pa_sink_process_rewind(u
->sink
, 0);
791 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
793 if (PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
) && pollfd
->revents
) {
794 if (l
= a2dp_process_render(u
) < 0)
798 /* feed the time smoother */
800 if (ioctl(u
->stream_fd
, SIOCOUTQ
, &l
) >= 0 && l
> 0)
802 usec
= pa_bytes_to_usec(n
, &u
->sink
->sample_spec
);
803 if (usec
> u
->latency
)
807 pa_smoother_put(u
->smoother
, pa_rtclock_usec(), usec
);
810 /* Hmm, nothing to do. Let's sleep */
811 pa_log_debug("A2DP thread going to sleep");
812 pollfd
->events
= PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
) ? POLLOUT
: 0;
813 if ((ret
= pa_rtpoll_run(u
->rtpoll
, TRUE
)) < 0) {
814 pa_log_error("rtpoll_run < 0");
817 pa_log_debug("A2DP thread waking up");
820 pa_log_debug("rtpoll_run == 0");
824 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
825 if (pollfd
->revents
& ~POLLOUT
) {
826 pa_log_error("FIFO shutdown.");
832 /* If this was no regular exit from the loop we have to continue processing messages until we receive PA_MESSAGE_SHUTDOWN */
833 pa_log_debug("A2DP thread failed");
834 pa_asyncmsgq_post(u
->thread_mq
.outq
, PA_MSGOBJECT(u
->core
), PA_CORE_MESSAGE_UNLOAD_MODULE
, u
->module
, 0, NULL
, NULL
);
835 pa_asyncmsgq_wait_for(u
->thread_mq
.inq
, PA_MESSAGE_SHUTDOWN
);
838 pa_log_debug("A2DP thread shutting down");
841 int pa__init(pa_module
* m
) {
844 pa_sink_new_data data
;
845 struct pollfd
*pollfd
;
849 m
->userdata
= u
= pa_xnew0(struct userdata
, 1);
852 u
->audioservice_fd
= -1;
857 u
->a2dp
.sbc_initialized
= FALSE
;
858 u
->smoother
= pa_smoother_new(PA_USEC_PER_SEC
, PA_USEC_PER_SEC
*2, TRUE
, 10);
859 u
->mempool
= pa_mempool_new(FALSE
);
860 pa_memchunk_reset(&u
->memchunk
);
861 u
->rtpoll
= pa_rtpoll_new();
862 pa_thread_mq_init(&u
->thread_mq
, u
->core
->mainloop
, u
->rtpoll
);
863 u
->rtpoll_item
= NULL
;
865 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
866 pa_log_error("failed to parse module arguments");
869 if (!(u
->name
= pa_xstrdup(pa_modargs_get_value(ma
, "name", DEFAULT_SINK_NAME
)))) {
870 pa_log_error("failed to get device name from module arguments");
873 if (!(u
->addr
= pa_xstrdup(pa_modargs_get_value(ma
, "addr", NULL
)))) {
874 pa_log_error("failed to get device address from module arguments");
877 if (!(u
->profile
= pa_xstrdup(pa_modargs_get_value(ma
, "profile", NULL
)))) {
878 pa_log_error("failed to get profile from module arguments");
881 if (pa_modargs_get_value_u32(ma
, "rate", &u
->ss
.rate
) < 0) {
882 pa_log_error("failed to get rate from module arguments");
885 if (pa_modargs_get_value_u32(ma
, "channels", &u
->ss
.channels
) < 0) {
886 pa_log_error("failed to get channels from module arguments");
890 /* connect to the bluez audio service */
891 u
->audioservice_fd
= bt_audio_service_open();
892 if (u
->audioservice_fd
<= 0) {
893 pa_log_error("couldn't connect to bluetooth audio service");
896 pa_log_debug("connected to the bluetooth audio service");
898 /* queries device capabilities */
901 pa_log_error("failed to get device capabilities");
904 pa_log_debug("got device capabilities");
906 /* configures the connection */
909 pa_log_error("failed to set config");
912 pa_log_debug("connection to the device configured");
914 /* gets the device socket */
915 e
= bt_getstreamfd(u
);
917 pa_log_error("failed to get stream fd (%d)", e
);
920 pa_log_debug("got the device socket");
923 pa_sink_new_data_init(&data
);
924 data
.driver
= __FILE__
;
926 pa_sink_new_data_set_name(&data
, u
->name
);
927 pa_sink_new_data_set_sample_spec(&data
, &u
->ss
);
928 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_STRING
, u
->name
);
929 pa_proplist_setf(data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, "Bluetooth %s '%s' (%s)", u
->strtransport
, u
->name
, u
->addr
);
930 pa_proplist_setf(data
.proplist
, "bluetooth.protocol", u
->profile
);
931 pa_proplist_setf(data
.proplist
, PA_PROP_DEVICE_API
, "bluez");
932 pa_proplist_setf(data
.proplist
, PA_PROP_DEVICE_CLASS
, "sound");
933 pa_proplist_setf(data
.proplist
, PA_PROP_DEVICE_CONNECTOR
, "bluetooth");
934 pa_proplist_setf(data
.proplist
, PA_PROP_DEVICE_FORM_FACTOR
, "headset"); /*FIXME*/
935 pa_proplist_setf(data
.proplist
, PA_PROP_DEVICE_VENDOR_PRODUCT_ID
, "product_id"); /*FIXME*/
936 pa_proplist_setf(data
.proplist
, PA_PROP_DEVICE_SERIAL
, "serial"); /*FIXME*/
937 u
->sink
= pa_sink_new(m
->core
, &data
, PA_SINK_HARDWARE
|PA_SINK_LATENCY
);
938 pa_sink_new_data_done(&data
);
940 pa_log_error("failed to create sink");
943 u
->sink
->userdata
= u
;
944 u
->sink
->parent
.process_msg
= sink_process_msg
;
945 pa_sink_set_asyncmsgq(u
->sink
, u
->thread_mq
.inq
);
946 pa_sink_set_rtpoll(u
->sink
, u
->rtpoll
);
948 u
->rtpoll_item
= pa_rtpoll_item_new(u
->rtpoll
, PA_RTPOLL_NEVER
, 1);
949 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
950 pollfd
->fd
= u
->stream_fd
;
951 pollfd
->events
= pollfd
->revents
= 0;
953 /* start rt thread */
954 if (u
->transport
== BT_CAPABILITIES_TRANSPORT_A2DP
) {
955 if (!(u
->thread
= pa_thread_new(a2dp_thread_func
, u
))) {
956 pa_log_error("failed to create A2DP thread");
961 if (!(u
->thread
= pa_thread_new(sco_thread_func
, u
))) {
962 pa_log_error("failed to create SCO thread");
966 pa_sink_put(u
->sink
);
978 void pa__done(pa_module
*m
) {
982 if (!(u
= m
->userdata
))
986 pa_sink_unlink(u
->sink
);
989 pa_asyncmsgq_send(u
->thread_mq
.inq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, 0, NULL
);
990 pa_thread_free(u
->thread
);
993 pa_thread_mq_done(&u
->thread_mq
);
996 pa_sink_unref(u
->sink
);
999 pa_rtpoll_item_free(u
->rtpoll_item
);
1002 pa_rtpoll_free(u
->rtpoll
);
1004 if (u
->memchunk
.memblock
)
1005 pa_memblock_unref(u
->memchunk
.memblock
);
1006 /* TODO: free mempool */
1009 pa_smoother_free(u
->smoother
);
1018 pa_xfree(u
->profile
);
1020 if (u
->stream_fd
>= 0)
1021 pa_close(u
->stream_fd
);
1023 if (u
->audioservice_fd
>= 0)
1024 pa_close(u
->audioservice_fd
);