Get rid of SINK_MESSAGE_PASS_SOCKET, since we don't really need it
[pulseaudio-mirror.git] / src / modules / module-bt-device.c
bloba92cc559af1e13fd4a75d8ae76202a180c2434c5
1 /***
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
19 USA.
20 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <string.h>
27 #include <errno.h>
28 #include <poll.h>
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"
43 #include "bt-ipc.h"
44 #include "bt-sbc.h"
46 #define DEFAULT_SINK_NAME "bt_sink"
47 #define BUFFER_SIZE 2048
48 #define MAX_BITPOOL 64
49 #define MIN_BITPOOL 2
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);
55 PA_MODULE_USAGE(
56 "name=<name of the device> "
57 "addr=<address of the device> "
58 "profile=<a2dp|hsp>");
60 struct bt_a2dp {
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*/
74 struct userdata {
75 pa_core *core;
76 pa_module *module;
77 pa_sink *sink;
79 pa_thread_mq thread_mq;
80 pa_rtpoll *rtpoll;
81 pa_rtpoll_item *rtpoll_item;
82 pa_thread *thread;
84 int64_t offset;
85 pa_smoother *smoother;
87 pa_memchunk memchunk;
89 const char *name;
90 const char *addr;
91 const char *profile;
92 int rate;
93 int channels;
94 pa_sample_spec ss;
96 int audioservice_fd;
97 int stream_fd;
99 int transport;
100 int link_mtu;
101 size_t block_size;
102 pa_usec_t latency;
104 struct bt_a2dp a2dp;
107 static const char* const valid_modargs[] = {
108 "name",
109 "addr",
110 "profile",
111 "rate",
112 "channels",
113 NULL
116 static int bt_audioservice_send(int sk, const bt_audio_msg_header_t *msg) {
117 int e;
118 pa_log/*_debug*/("sending %s", bt_audio_strmsg(msg->msg_type));
119 if (send(sk, msg, BT_AUDIO_IPC_PACKET_SIZE, 0) > 0)
120 e = 0;
121 else {
122 e = -errno;
123 pa_log_error("Error sending data to audio service: %s(%d)", strerror(errno), errno);
125 return e;
128 static int bt_audioservice_recv(int sk, bt_audio_msg_header_t *inmsg) {
129 int e;
130 const char *type;
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);
135 if (type) {
136 pa_log/*_debug*/("Received %s", type);
137 e = 0;
139 else {
140 e = -EINVAL;
141 pa_log_error("Bogus message type %d received from audio service", inmsg->msg_type);
144 else {
145 e = -errno;
146 pa_log_error("Error receiving data from audio service: %s(%d)", strerror(errno), errno);
149 return e;
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);
154 if (e == 0) {
155 if (rsp_hdr->msg_type != expected_type) {
156 e = -EINVAL;
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));
161 return e;
164 static int bt_getcaps(struct userdata *u) {
165 int e;
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;
178 else {
179 pa_log_error("invalid profile argument: %s", u->profile);
180 return -1;
182 getcaps_req->flags = 0;
183 getcaps_req->flags |= BT_FLAG_AUTOCONNECT;
185 e = bt_audioservice_send(u->audioservice_fd, &getcaps_req->h);
186 if (e < 0) {
187 pa_log_error("failed to send GETCAPABILITIES_REQ");
188 return e;
191 e = bt_audioservice_expect(u->audioservice_fd, &rsp_hdr->msg_h, BT_GETCAPABILITIES_RSP);
192 if (e < 0) {
193 pa_log_error("failed to expect for GETCAPABILITIES_RSP");
194 return e;
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;
204 return 0;
207 static uint8_t default_bitpool(uint8_t freq, uint8_t mode) {
208 switch (freq) {
209 case BT_SBC_SAMPLING_FREQ_16000:
210 case BT_SBC_SAMPLING_FREQ_32000:
211 return 53;
212 case BT_SBC_SAMPLING_FREQ_44100:
213 switch (mode) {
214 case BT_A2DP_CHANNEL_MODE_MONO:
215 case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL:
216 return 31;
217 case BT_A2DP_CHANNEL_MODE_STEREO:
218 case BT_A2DP_CHANNEL_MODE_JOINT_STEREO:
219 return 53;
220 default:
221 pa_log_warn("Invalid channel mode %u", mode);
222 return 53;
224 case BT_SBC_SAMPLING_FREQ_48000:
225 switch (mode) {
226 case BT_A2DP_CHANNEL_MODE_MONO:
227 case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL:
228 return 29;
229 case BT_A2DP_CHANNEL_MODE_STEREO:
230 case BT_A2DP_CHANNEL_MODE_JOINT_STEREO:
231 return 51;
232 default:
233 pa_log_warn("Invalid channel mode %u", mode);
234 return 51;
236 default:
237 pa_log_warn("Invalid sampling freq %u", freq);
238 return 53;
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;
246 switch (u->rate) {
247 case 48000:
248 cap->frequency = BT_SBC_SAMPLING_FREQ_48000;
249 break;
250 case 44100:
251 cap->frequency = BT_SBC_SAMPLING_FREQ_44100;
252 break;
253 case 32000:
254 cap->frequency = BT_SBC_SAMPLING_FREQ_32000;
255 break;
256 case 16000:
257 cap->frequency = BT_SBC_SAMPLING_FREQ_16000;
258 break;
259 default:
260 pa_log_error("Rate %d not supported", rate);
261 return -1;
264 // if (cfg->has_channel_mode)
265 // cap->channel_mode = cfg->channel_mode;
266 // else
267 if (channels == 2) {
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;
274 } else {
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");
281 return -1;
284 // if (cfg->has_block_length)
285 // cap->block_length = cfg->block_length;
286 // else
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;
295 else {
296 pa_log_error("No supported block lengths");
297 return -1;
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;
306 else {
307 pa_log_error("No supported subbands");
308 return -1;
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;
320 // else {
321 min_bitpool = MAX(MIN_BITPOOL, cap->min_bitpool);
322 max_bitpool = MIN(default_bitpool(cap->frequency, cap->channel_mode), cap->max_bitpool);
323 // }
325 cap->min_bitpool = min_bitpool;
326 cap->max_bitpool = max_bitpool;
328 return 0;
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);
336 else
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;
369 break;
370 case BT_A2DP_SUBBANDS_8:
371 a2dp->sbc.subbands = SBC_SB_8;
372 break;
375 switch (active_capabilities.block_length) {
376 case BT_A2DP_BLOCK_LENGTH_4:
377 a2dp->sbc.blocks = SBC_BLK_4;
378 break;
379 case BT_A2DP_BLOCK_LENGTH_8:
380 a2dp->sbc.blocks = SBC_BLK_8;
381 break;
382 case BT_A2DP_BLOCK_LENGTH_12:
383 a2dp->sbc.blocks = SBC_BLK_12;
384 break;
385 case BT_A2DP_BLOCK_LENGTH_16:
386 a2dp->sbc.blocks = SBC_BLK_16;
387 break;
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) {
396 int e;
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) {
403 e = bt_a2dp_init(u);
404 if (e < 0) {
405 pa_log_error("a2dp_init error");
406 return e;
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);
419 if (e < 0) {
420 pa_log_error("failed to send BT_SETCONFIGURATION_REQ");
421 return e;
424 e = bt_audioservice_expect(u->audioservice_fd, &rsp_hdr->msg_h, BT_SETCONFIGURATION_RSP);
425 if (e < 0) {
426 pa_log_error("failed to expect BT_SETCONFIGURATION_RSP");
427 return e;
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);
445 return 0;
448 static int bt_getstreamfd(struct userdata *u) {
449 int e/*, opt_name*/;
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);
459 if (e < 0) {
460 pa_log_error("failed to send BT_STREAMSTART_REQ");
461 return e;
464 e = bt_audioservice_expect(u->audioservice_fd, &rsp_hdr->msg_h, BT_STREAMSTART_RSP);
465 if (e < 0) {
466 pa_log_error("failed to expect BT_STREAMSTART_RSP");
467 return e;
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);
476 if (e < 0) {
477 pa_log_error("failed to expect BT_STREAMFD_IND");
478 return e;
481 if (u->stream_fd >= 0)
482 close(u->stream_fd);
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");
487 return -errno;
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");
494 // return -errno;
495 // }
496 // }
497 // else {
498 // opt_name = SCO_TXBUFS;
499 // if (setsockopt(u->stream_fd, SOL_SCO, opt_name, &period_count, sizeof(period_count)) == 0)
500 // return 0;
501 // opt_name = SO_SNDBUF;
502 // if (setsockopt(u->stream_fd, SOL_SCO, opt_name, &period_count, sizeof(period_count)) == 0)
503 // return 0;
504 // /* FIXME : handle error codes */
505 // }
507 return 0;
510 static int bt_hw_constraint(struct userdata *u) {
511 /*TODO: A2DP */
512 u->ss.format = PA_SAMPLE_S16LE;
513 u->ss.rate = 8000;
514 u->ss.channels = 1;
515 return 0;
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;
521 switch (code) {
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());
528 break;
529 case PA_SINK_IDLE:
530 case PA_SINK_RUNNING:
531 if (u->sink->thread_info.state == PA_SINK_SUSPENDED)
532 pa_smoother_resume(u->smoother, pa_rtclock_usec());
533 break;
534 case PA_SINK_UNLINKED:
535 case PA_SINK_INIT:
538 break;
540 case PA_SINK_MESSAGE_GET_LATENCY: {
541 pa_usec_t w, r;
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;
545 break;
550 return pa_sink_process_msg(o, code, data, offset, chunk);
553 static void thread_func(void *userdata) {
554 struct userdata *u = userdata;
555 int write_type = 0;
557 pa_assert(u);
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());
566 for (;;) {
567 int ret;
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) {
579 pa_usec_t usec;
580 int64_t n;
582 for (;;) {
583 ssize_t l;
584 void *p;
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);
595 pa_assert(l != 0);
597 if (l < 0) {
599 if (errno == EINTR)
600 continue;
601 else if (errno == EAGAIN) {
603 /* OK, we filled all socket buffers up
604 * now. */
605 goto filled_up;
607 } else {
608 pa_log("Failed to write data to FIFO: %s", pa_cstrerror(errno));
609 goto fail;
612 } else {
613 u->offset += l;
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);
623 pollfd->revents = 0;
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 */
630 goto filled_up;
634 filled_up:
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 */
640 n = u->offset;
642 //#ifdef SIOCOUTQ
643 // {
644 // int l;
645 // if (ioctl(u->fd, SIOCOUTQ, &l) >= 0 && l > 0)
646 // n -= l;
647 // }
648 //#endif
650 usec = pa_bytes_to_usec(n, &u->sink->sample_spec);
652 if (usec > u->latency)
653 usec -= u->latency;
654 else
655 usec = 0;
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)
665 goto fail;
667 if (ret == 0)
668 goto finish;
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.");
677 goto fail;
682 fail:
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);
688 finish:
689 pa_log_debug("Thread shutting down");
692 int pa__init(pa_module* m) {
693 int e;
694 const char *rate, *channels;
695 pa_modargs *ma;
696 pa_sink_new_data data;
697 struct userdata *u;
699 pa_assert(m);
700 m->userdata = u = pa_xnew0(struct userdata, 1);
701 u->module = m;
702 u->core = m->core;
703 u->audioservice_fd = -1;
704 u->stream_fd = -1;
705 u->transport = -1;
706 u->offset = 0;
707 u->latency = 0;
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");
716 goto fail;
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");
720 goto fail;
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");
724 goto fail;
726 if (!(u->profile = pa_xstrdup(pa_modargs_get_value(ma, "profile", NULL)))) {
727 pa_log_error("failed to get profile from module arguments");
728 goto fail;
730 if (pa_modargs_get_value_u32(ma, "rate", &u->rate) < 0) {
731 pa_log_error("failed to get rate from module arguments");
732 goto fail;
734 if (pa_modargs_get_value_u32(ma, "channels", &u->channels) < 0) {
735 pa_log_error("failed to get channels from module arguments");
736 goto fail;
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");
744 goto fail;
747 /* queries device capabilities */
748 e = bt_getcaps(u);
749 if (e < 0) {
750 pa_log_error("failed to get device capabilities");
751 goto fail;
754 /* configures the connection */
755 e = bt_setconf(u);
756 if (e < 0) {
757 pa_log_error("failed to set config");
758 goto fail;
761 /* gets the device socket */
762 e = bt_getstreamfd(u);
763 if (e < 0) {
764 pa_log_error("failed to get stream fd (%d)", e);
765 goto fail;
768 /* configure hw supported sample specs */
769 e = bt_hw_constraint(u);
770 if (e < 0) {
771 pa_log_error("failed to configure sample spec");
772 goto fail;
775 /* create sink */
776 pa_sink_new_data_init(&data);
777 data.driver = __FILE__;
778 data.module = m;
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);
785 if (!u->sink) {
786 pa_log_error("failed to create sink");
787 goto fail;
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");
797 goto fail;
799 pa_sink_put(u->sink);
801 pa_modargs_free(ma);
802 return 0;
804 fail:
805 if (ma)
806 pa_modargs_free(ma);
807 pa__done(m);
808 return -1;
811 void pa__done(pa_module *m) {
812 pa_log("Unloading module-bt-device");