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;
109 static const char* const valid_modargs[] = {
118 static int bt_audioservice_send(int sk, const bt_audio_msg_header_t *msg) {
120 pa_log/*_debug*/("sending %s", bt_audio_strmsg(msg->msg_type));
121 if (send(sk, msg, BT_AUDIO_IPC_PACKET_SIZE, 0) > 0)
125 pa_log_error("Error sending data to audio service: %s(%d)", pa_cstrerror(errno), errno);
130 static int bt_audioservice_recv(int sk, bt_audio_msg_header_t *inmsg) {
134 pa_log/*_debug*/("trying to receive msg from audio service...");
135 if (recv(sk, inmsg, BT_AUDIO_IPC_PACKET_SIZE, 0) > 0) {
136 type = bt_audio_strmsg(inmsg->msg_type);
138 pa_log/*_debug*/("Received %s", type);
143 pa_log_error("Bogus message type %d received from audio service", inmsg->msg_type);
148 pa_log_error("Error receiving data from audio service: %s(%d)", pa_cstrerror(errno), errno);
154 static int bt_audioservice_expect(int sk, bt_audio_msg_header_t *rsp_hdr, int expected_type) {
155 int e = bt_audioservice_recv(sk, rsp_hdr);
157 if (rsp_hdr->msg_type != expected_type) {
159 pa_log_error("Bogus message %s received while %s was expected", bt_audio_strmsg(rsp_hdr->msg_type),
160 bt_audio_strmsg(expected_type));
166 static int bt_getcaps(struct userdata *u) {
168 char buf[BT_AUDIO_IPC_PACKET_SIZE];
169 bt_audio_rsp_msg_header_t *rsp_hdr = (void*) buf;
170 struct bt_getcapabilities_req *getcaps_req = (void*) buf;
171 struct bt_getcapabilities_rsp *getcaps_rsp = (void*) buf;
173 memset(getcaps_req, 0, BT_AUDIO_IPC_PACKET_SIZE);
174 getcaps_req->h.msg_type = BT_GETCAPABILITIES_REQ;
175 strncpy(getcaps_req->device, u->addr, 18);
176 if (strcasecmp(u->profile, "a2dp") == 0)
177 getcaps_req->transport = BT_CAPABILITIES_TRANSPORT_A2DP;
178 else if (strcasecmp(u->profile, "hsp") == 0)
179 getcaps_req->transport = BT_CAPABILITIES_TRANSPORT_SCO;
181 pa_log_error("invalid profile argument: %s", u->profile);
184 getcaps_req->flags = 0;
185 getcaps_req->flags |= BT_FLAG_AUTOCONNECT;
187 e = bt_audioservice_send(u->audioservice_fd, &getcaps_req->h);
189 pa_log_error("failed to send GETCAPABILITIES_REQ");
193 e = bt_audioservice_expect(u->audioservice_fd, &rsp_hdr->msg_h, BT_GETCAPABILITIES_RSP);
195 pa_log_error("failed to expect for GETCAPABILITIES_RSP");
198 if (rsp_hdr->posix_errno != 0) {
199 pa_log_error("BT_GETCAPABILITIES failed : %s (%d)", pa_cstrerror(rsp_hdr->posix_errno), rsp_hdr->posix_errno);
200 return -rsp_hdr->posix_errno;
203 if ((u->transport = getcaps_rsp->transport) == BT_CAPABILITIES_TRANSPORT_A2DP)
204 u->a2dp.sbc_capabilities = getcaps_rsp->sbc_capabilities;
209 static uint8_t default_bitpool(uint8_t freq, uint8_t mode) {
211 case BT_SBC_SAMPLING_FREQ_16000:
212 case BT_SBC_SAMPLING_FREQ_32000:
214 case BT_SBC_SAMPLING_FREQ_44100:
216 case BT_A2DP_CHANNEL_MODE_MONO:
217 case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL:
219 case BT_A2DP_CHANNEL_MODE_STEREO:
220 case BT_A2DP_CHANNEL_MODE_JOINT_STEREO:
223 pa_log_warn("Invalid channel mode %u", mode);
226 case BT_SBC_SAMPLING_FREQ_48000:
228 case BT_A2DP_CHANNEL_MODE_MONO:
229 case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL:
231 case BT_A2DP_CHANNEL_MODE_STEREO:
232 case BT_A2DP_CHANNEL_MODE_JOINT_STEREO:
235 pa_log_warn("Invalid channel mode %u", mode);
239 pa_log_warn("Invalid sampling freq %u", freq);
244 static int bt_a2dp_init(struct userdata *u) {
245 sbc_capabilities_t *cap = &u->a2dp.sbc_capabilities;
246 unsigned int max_bitpool, min_bitpool, rate, channels;
250 cap->frequency = BT_SBC_SAMPLING_FREQ_48000;
253 cap->frequency = BT_SBC_SAMPLING_FREQ_44100;
256 cap->frequency = BT_SBC_SAMPLING_FREQ_32000;
259 cap->frequency = BT_SBC_SAMPLING_FREQ_16000;
262 pa_log_error("Rate %d not supported", rate);
266 // if (cfg->has_channel_mode)
267 // cap->channel_mode = cfg->channel_mode;
270 if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_JOINT_STEREO)
271 cap->channel_mode = BT_A2DP_CHANNEL_MODE_JOINT_STEREO;
272 else if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_STEREO)
273 cap->channel_mode = BT_A2DP_CHANNEL_MODE_STEREO;
274 else if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL)
275 cap->channel_mode = BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL;
277 if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_MONO)
278 cap->channel_mode = BT_A2DP_CHANNEL_MODE_MONO;
281 if (!cap->channel_mode) {
282 pa_log_error("No supported channel modes");
286 // if (cfg->has_block_length)
287 // cap->block_length = cfg->block_length;
289 if (cap->block_length & BT_A2DP_BLOCK_LENGTH_16)
290 cap->block_length = BT_A2DP_BLOCK_LENGTH_16;
291 else if (cap->block_length & BT_A2DP_BLOCK_LENGTH_12)
292 cap->block_length = BT_A2DP_BLOCK_LENGTH_12;
293 else if (cap->block_length & BT_A2DP_BLOCK_LENGTH_8)
294 cap->block_length = BT_A2DP_BLOCK_LENGTH_8;
295 else if (cap->block_length & BT_A2DP_BLOCK_LENGTH_4)
296 cap->block_length = BT_A2DP_BLOCK_LENGTH_4;
298 pa_log_error("No supported block lengths");
302 // if (cfg->has_subbands)
303 // cap->subbands = cfg->subbands;
304 if (cap->subbands & BT_A2DP_SUBBANDS_8)
305 cap->subbands = BT_A2DP_SUBBANDS_8;
306 else if (cap->subbands & BT_A2DP_SUBBANDS_4)
307 cap->subbands = BT_A2DP_SUBBANDS_4;
309 pa_log_error("No supported subbands");
313 // if (cfg->has_allocation_method)
314 // cap->allocation_method = cfg->allocation_method;
315 if (cap->allocation_method & BT_A2DP_ALLOCATION_LOUDNESS)
316 cap->allocation_method = BT_A2DP_ALLOCATION_LOUDNESS;
317 else if (cap->allocation_method & BT_A2DP_ALLOCATION_SNR)
318 cap->allocation_method = BT_A2DP_ALLOCATION_SNR;
320 // if (cfg->has_bitpool)
321 // min_bitpool = max_bitpool = cfg->bitpool;
323 min_bitpool = MAX(MIN_BITPOOL, cap->min_bitpool);
324 max_bitpool = MIN(default_bitpool(cap->frequency, cap->channel_mode), cap->max_bitpool);
327 cap->min_bitpool = min_bitpool;
328 cap->max_bitpool = max_bitpool;
333 static void bt_a2dp_setup(struct bt_a2dp *a2dp) {
334 sbc_capabilities_t active_capabilities = a2dp->sbc_capabilities;
336 if (a2dp->sbc_initialized)
337 sbc_reinit(&a2dp->sbc, 0);
339 sbc_init(&a2dp->sbc, 0);
340 a2dp->sbc_initialized = 1;
342 if (active_capabilities.frequency & BT_SBC_SAMPLING_FREQ_16000)
343 a2dp->sbc.frequency = SBC_FREQ_16000;
345 if (active_capabilities.frequency & BT_SBC_SAMPLING_FREQ_32000)
346 a2dp->sbc.frequency = SBC_FREQ_32000;
348 if (active_capabilities.frequency & BT_SBC_SAMPLING_FREQ_44100)
349 a2dp->sbc.frequency = SBC_FREQ_44100;
351 if (active_capabilities.frequency & BT_SBC_SAMPLING_FREQ_48000)
352 a2dp->sbc.frequency = SBC_FREQ_48000;
354 if (active_capabilities.channel_mode & BT_A2DP_CHANNEL_MODE_MONO)
355 a2dp->sbc.mode = SBC_MODE_MONO;
357 if (active_capabilities.channel_mode & BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL)
358 a2dp->sbc.mode = SBC_MODE_DUAL_CHANNEL;
360 if (active_capabilities.channel_mode & BT_A2DP_CHANNEL_MODE_STEREO)
361 a2dp->sbc.mode = SBC_MODE_STEREO;
363 if (active_capabilities.channel_mode & BT_A2DP_CHANNEL_MODE_JOINT_STEREO)
364 a2dp->sbc.mode = SBC_MODE_JOINT_STEREO;
366 a2dp->sbc.allocation = active_capabilities.allocation_method == BT_A2DP_ALLOCATION_SNR ? SBC_AM_SNR : SBC_AM_LOUDNESS;
368 switch (active_capabilities.subbands) {
369 case BT_A2DP_SUBBANDS_4:
370 a2dp->sbc.subbands = SBC_SB_4;
372 case BT_A2DP_SUBBANDS_8:
373 a2dp->sbc.subbands = SBC_SB_8;
377 switch (active_capabilities.block_length) {
378 case BT_A2DP_BLOCK_LENGTH_4:
379 a2dp->sbc.blocks = SBC_BLK_4;
381 case BT_A2DP_BLOCK_LENGTH_8:
382 a2dp->sbc.blocks = SBC_BLK_8;
384 case BT_A2DP_BLOCK_LENGTH_12:
385 a2dp->sbc.blocks = SBC_BLK_12;
387 case BT_A2DP_BLOCK_LENGTH_16:
388 a2dp->sbc.blocks = SBC_BLK_16;
392 a2dp->sbc.bitpool = active_capabilities.max_bitpool;
393 a2dp->codesize = sbc_get_codesize(&a2dp->sbc);
394 // a2dp->count = sizeof(struct rtp_header) + sizeof(struct rtp_payload);
397 static int bt_setconf(struct userdata *u) {
399 char buf[BT_AUDIO_IPC_PACKET_SIZE];
400 bt_audio_rsp_msg_header_t *rsp_hdr = (void*) buf;
401 struct bt_setconfiguration_req *setconf_req = (void*) buf;
402 struct bt_setconfiguration_rsp *setconf_rsp = (void*) buf;
404 if (u->transport == BT_CAPABILITIES_TRANSPORT_A2DP) {
407 pa_log_error("a2dp_init error");
412 memset(setconf_req, 0, BT_AUDIO_IPC_PACKET_SIZE);
413 setconf_req->h.msg_type = BT_SETCONFIGURATION_REQ;
414 strncpy(setconf_req->device, u->addr, 18);
415 setconf_req->transport = u->transport;
416 if (u->transport == BT_CAPABILITIES_TRANSPORT_A2DP)
417 setconf_req->sbc_capabilities = u->a2dp.sbc_capabilities;
418 setconf_req->access_mode = BT_CAPABILITIES_ACCESS_MODE_WRITE;
420 e = bt_audioservice_send(u->audioservice_fd, &setconf_req->h);
422 pa_log_error("failed to send BT_SETCONFIGURATION_REQ");
426 e = bt_audioservice_expect(u->audioservice_fd, &rsp_hdr->msg_h, BT_SETCONFIGURATION_RSP);
428 pa_log_error("failed to expect BT_SETCONFIGURATION_RSP");
432 if (rsp_hdr->posix_errno != 0) {
433 pa_log_error("BT_SETCONFIGURATION failed : %s(%d)", pa_cstrerror(rsp_hdr->posix_errno), rsp_hdr->posix_errno);
434 return -rsp_hdr->posix_errno;
437 u->transport = setconf_rsp->transport;
438 u->block_size = u->link_mtu = setconf_rsp->link_mtu;
440 /* setup SBC encoder now we agree on parameters */
441 if (u->transport == BT_CAPABILITIES_TRANSPORT_A2DP) {
442 bt_a2dp_setup(&u->a2dp);
443 pa_log/*debug*/("\tallocation=%u\n\tsubbands=%u\n\tblocks=%u\n\tbitpool=%u\n",
444 u->a2dp.sbc.allocation, u->a2dp.sbc.subbands, u->a2dp.sbc.blocks, u->a2dp.sbc.bitpool);
450 static int bt_getstreamfd(struct userdata *u) {
452 char buf[BT_AUDIO_IPC_PACKET_SIZE];
453 struct bt_streamstart_req *start_req = (void*) buf;
454 bt_audio_rsp_msg_header_t *rsp_hdr = (void*) buf;
455 struct bt_streamfd_ind *streamfd_ind = (void*) buf;
457 memset(start_req, 0, BT_AUDIO_IPC_PACKET_SIZE);
458 start_req->h.msg_type = BT_STREAMSTART_REQ;
460 e = bt_audioservice_send(u->audioservice_fd, &start_req->h);
462 pa_log_error("failed to send BT_STREAMSTART_REQ");
466 e = bt_audioservice_expect(u->audioservice_fd, &rsp_hdr->msg_h, BT_STREAMSTART_RSP);
468 pa_log_error("failed to expect BT_STREAMSTART_RSP");
472 if (rsp_hdr->posix_errno != 0) {
473 pa_log_error("BT_START failed : %s(%d)", pa_cstrerror(rsp_hdr->posix_errno), rsp_hdr->posix_errno);
474 return -rsp_hdr->posix_errno;
477 e = bt_audioservice_expect(u->audioservice_fd, &streamfd_ind->h, BT_STREAMFD_IND);
479 pa_log_error("failed to expect BT_STREAMFD_IND");
483 if (u->stream_fd >= 0)
484 pa_close(u->stream_fd);
486 u->stream_fd = bt_audio_service_get_data_fd(u->audioservice_fd);
487 if (u->stream_fd < 0) {
488 pa_log_error("failed to get data fd");
492 // if (u->transport == BT_CAPABILITIES_TRANSPORT_A2DP) {
493 // opt_name = SO_SNDTIMEO;
494 // if (setsockopt(u->stream_fd, SOL_SOCKET, opt_name, &t, sizeof(t)) < 0) {
495 // pa_log_error("failed to set socket options for A2DP");
500 // opt_name = SCO_TXBUFS;
501 // if (setsockopt(u->stream_fd, SOL_SCO, opt_name, &period_count, sizeof(period_count)) == 0)
503 // opt_name = SO_SNDBUF;
504 // if (setsockopt(u->stream_fd, SOL_SCO, opt_name, &period_count, sizeof(period_count)) == 0)
506 // /* FIXME : handle error codes */
512 static int bt_hw_constraint(struct userdata *u) {
514 u->ss.format = PA_SAMPLE_S16LE;
520 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
521 struct userdata *u = PA_SINK(o)->userdata;
525 case PA_SINK_MESSAGE_SET_STATE:
526 switch ((pa_sink_state_t) PA_PTR_TO_UINT(data)) {
527 case PA_SINK_SUSPENDED:
528 pa_assert(PA_SINK_IS_OPENED(u->sink->thread_info.state));
529 pa_smoother_pause(u->smoother, pa_rtclock_usec());
532 case PA_SINK_RUNNING:
533 if (u->sink->thread_info.state == PA_SINK_SUSPENDED)
534 pa_smoother_resume(u->smoother, pa_rtclock_usec());
536 case PA_SINK_UNLINKED:
542 case PA_SINK_MESSAGE_GET_LATENCY: {
544 r = pa_smoother_get(u->smoother, pa_rtclock_usec());
545 w = pa_bytes_to_usec(u->offset + u->memchunk.length, &u->sink->sample_spec);
546 *((pa_usec_t*) data) = w > r ? w - r : 0;
552 return pa_sink_process_msg(o, code, data, offset, chunk);
555 static void thread_func(void *userdata) {
556 struct userdata *u = userdata;
561 pa_log/*_debug*/("Thread starting up");
563 pa_thread_mq_install(&u->thread_mq);
564 pa_rtpoll_install(u->rtpoll);
566 pa_smoother_set_time_offset(u->smoother, pa_rtclock_usec());
571 if (PA_SINK_IS_OPENED(u->sink->thread_info.state))
572 if (u->sink->thread_info.rewind_requested)
573 pa_sink_process_rewind(u->sink, 0);
575 if (u->rtpoll_item) {
576 struct pollfd *pollfd;
577 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
579 /* Render some data and write it to the fifo */
580 if (PA_SINK_IS_OPENED(u->sink->thread_info.state) && pollfd->revents) {
588 if (u->memchunk.length <= 0)
589 pa_sink_render(u->sink, u->block_size, &u->memchunk);
591 pa_assert(u->memchunk.length > 0);
593 p = pa_memblock_acquire(u->memchunk.memblock);
594 l = pa_write(u->stream_fd, (uint8_t*) p + u->memchunk.index, u->memchunk.length, &write_type);
595 pa_memblock_release(u->memchunk.memblock);
603 else if (errno == EAGAIN) {
605 /* OK, we filled all socket buffers up
610 pa_log("Failed to write data to FIFO: %s", pa_cstrerror(errno));
617 u->memchunk.index += l;
618 u->memchunk.length -= l;
620 if (u->memchunk.length <= 0) {
621 pa_memblock_unref(u->memchunk.memblock);
622 pa_memchunk_reset(&u->memchunk);
627 if (u->memchunk.length > 0)
629 /* OK, we wrote less that we asked for,
630 * hence we can assume that the socket
631 * buffers are full now */
638 /* At this spot we know that the socket buffers are
639 * fully filled up. This is the best time to estimate
640 * 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);
654 if (usec > u->latency)
659 pa_smoother_put(u->smoother, pa_rtclock_usec(), usec);
662 /* Hmm, nothing to do. Let's sleep */
663 pollfd->events = PA_SINK_IS_OPENED(u->sink->thread_info.state) ? POLLOUT : 0;
666 if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
672 if (u->rtpoll_item) {
673 struct pollfd* pollfd;
675 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
677 if (pollfd->revents & ~POLLOUT) {
678 pa_log("FIFO shutdown.");
685 /* If this was no regular exit from the loop we have to continue
686 * processing messages until we received PA_MESSAGE_SHUTDOWN */
687 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
688 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
691 pa_log_debug("Thread shutting down");
694 int pa__init(pa_module* m) {
696 const char *rate, *channels;
698 pa_sink_new_data data;
699 struct pollfd *pollfd;
703 m->userdata = u = pa_xnew0(struct userdata, 1);
706 u->audioservice_fd = -1;
711 u->smoother = pa_smoother_new(PA_USEC_PER_SEC, PA_USEC_PER_SEC*2, TRUE, 10);
712 pa_memchunk_reset(&u->memchunk);
713 u->rtpoll = pa_rtpoll_new();
714 pa_thread_mq_init(&u->thread_mq, u->core->mainloop, u->rtpoll);
715 u->rtpoll_item = NULL;
717 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
718 pa_log_error("failed to parse module arguments");
721 if (!(u->name = pa_xstrdup(pa_modargs_get_value(ma, "name", DEFAULT_SINK_NAME)))) {
722 pa_log_error("failed to get device name from module arguments");
725 if (!(u->addr = pa_xstrdup(pa_modargs_get_value(ma, "addr", NULL)))) {
726 pa_log_error("failed to get device address from module arguments");
729 if (!(u->profile = pa_xstrdup(pa_modargs_get_value(ma, "profile", NULL)))) {
730 pa_log_error("failed to get profile from module arguments");
733 if (pa_modargs_get_value_u32(ma, "rate", &u->rate) < 0) {
734 pa_log_error("failed to get rate from module arguments");
737 if (pa_modargs_get_value_u32(ma, "channels", &u->channels) < 0) {
738 pa_log_error("failed to get channels from module arguments");
741 pa_log("Loading module-bt-device for %s (%s), profile %s", u->name, u->addr, u->profile);
743 /* connect to the bluez audio service */
744 u->audioservice_fd = bt_audio_service_open();
745 if (u->audioservice_fd <= 0) {
746 pa_log_error("couldn't connect to bluetooth audio service");
750 /* queries device capabilities */
753 pa_log_error("failed to get device capabilities");
757 /* configures the connection */
760 pa_log_error("failed to set config");
764 /* gets the device socket */
765 e = bt_getstreamfd(u);
767 pa_log_error("failed to get stream fd (%d)", e);
770 u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
771 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
772 pollfd->fd = u->stream_fd;
773 pollfd->events = pollfd->revents = 0;
775 /* configure hw supported sample specs */
776 e = bt_hw_constraint(u);
778 pa_log_error("failed to configure sample spec");
783 pa_sink_new_data_init(&data);
784 data.driver = __FILE__;
786 pa_sink_new_data_set_name(&data, u->name);
787 pa_sink_new_data_set_sample_spec(&data, &u->ss);
788 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->name);
789 pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Bluetooth sink '%s' (%s)", u->name, u->addr);
790 u->sink = pa_sink_new(m->core, &data, PA_SINK_HARDWARE|PA_SINK_LATENCY|PA_SINK_NETWORK);
791 pa_sink_new_data_done(&data);
793 pa_log_error("failed to create sink");
796 u->sink->userdata = u;
797 u->sink->parent.process_msg = sink_process_msg;
798 pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
799 pa_sink_set_rtpoll(u->sink, u->rtpoll);
801 /* start rt thread */
802 if (!(u->thread = pa_thread_new(thread_func, u))) {
803 pa_log_error("failed to create thread");
806 pa_sink_put(u->sink);
818 void pa__done(pa_module *m) {
819 pa_log("Unloading module-bt-device");