/***
This file is part of PulseAudio.
- Copyright 2008 Joao Paulo Rechi Vita
+ Copyright 2008-2009 Joao Paulo Rechi Vita
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
#include <pulsecore/time-smoother.h>
#include <pulsecore/namereg.h>
#include <pulsecore/dbus-shared.h>
+#include <pulsecore/llist.h>
#include "module-bluetooth-device-symdef.h"
#include "ipc.h"
"source_name=<name for the source> "
"source_properties=<properties for the source> "
"address=<address of the device> "
- "profile=<a2dp|hsp> "
+ "profile=<a2dp|hsp|hfgw> "
"rate=<sample rate> "
"channels=<number of channels> "
- "path=<device object path>");
+ "path=<device object path> "
+ "auto_connect=<automatically connect?>");
/*
#ifdef NOKIA
"rate",
"channels",
"path",
+ "auto_connect",
#ifdef NOKIA
"sco_sink",
"sco_source",
pa_hook_slot *source_state_changed_slot;
};
-enum profile {
- PROFILE_A2DP,
- PROFILE_HSP,
- PROFILE_OFF
-};
-
struct userdata {
pa_core *core;
pa_module *module;
char *address;
char *path;
+ char *transport;
+ char *accesstype;
+
pa_bluetooth_discovery *discovery;
+ pa_bool_t auto_connect;
pa_dbus_connection *connection;
int stream_write_type;
int service_write_type, service_read_type;
+
+ pa_bool_t filter_added;
};
#define FIXED_LATENCY_PLAYBACK_A2DP (25*PA_USEC_PER_MSEC)
+#define FIXED_LATENCY_RECORD_A2DP (25*PA_USEC_PER_MSEC)
#define FIXED_LATENCY_PLAYBACK_HSP (125*PA_USEC_PER_MSEC)
#define FIXED_LATENCY_RECORD_HSP (25*PA_USEC_PER_MSEC)
ssize_t r;
pa_assert(u);
- pa_assert(u->service_fd >= 0);
pa_assert(msg);
pa_assert(msg->length > 0);
+ if (u->service_fd < 0) {
+ pa_log_warn("Service not connected");
+ return -1;
+ }
+
pa_log_debug("Sending %s -> %s",
pa_strnull(bt_audio_strtype(msg->type)),
pa_strnull(bt_audio_strname(msg->name)));
pa_assert(u);
pa_assert(u->service_fd >= 0);
pa_assert(msg);
-
- if (room <= 0)
- room = BT_SUGGESTED_BUFFER_SIZE;
+ pa_assert(room >= sizeof(*msg));
pa_log_debug("Trying to receive message from audio service...");
return -1;
}
+ if (msg->length > room) {
+ pa_log_error("Not enough room.");
+ return -1;
+ }
+
/* Secondly, read the payload */
if (msg->length > sizeof(*msg)) {
pa_log_debug("Payload size is %lu %lu", (unsigned long) bytes_left, (unsigned long) sizeof(*codec));
- if ((u->profile == PROFILE_A2DP && codec->transport != BT_CAPABILITIES_TRANSPORT_A2DP) ||
- (u->profile == PROFILE_HSP && codec->transport != BT_CAPABILITIES_TRANSPORT_SCO)) {
+ if (((u->profile == PROFILE_A2DP || u->profile == PROFILE_A2DP_SOURCE) && codec->transport != BT_CAPABILITIES_TRANSPORT_A2DP) ||
+ ((u->profile == PROFILE_HSP || u->profile == PROFILE_HFGW) && codec->transport != BT_CAPABILITIES_TRANSPORT_SCO)) {
pa_log_error("Got capabilities for wrong codec.");
return -1;
}
- if (u->profile == PROFILE_HSP) {
+ if (u->profile == PROFILE_HSP || u->profile == PROFILE_HFGW) {
if (bytes_left <= 0 || codec->length != sizeof(u->hsp.pcm_capabilities))
return -1;
return codec->seid;
memcpy(&u->a2dp.sbc_capabilities, codec, sizeof(u->a2dp.sbc_capabilities));
+
+ } else if (u->profile == PROFILE_A2DP_SOURCE) {
+
+ while (bytes_left > 0) {
+ if ((codec->type == BT_A2DP_SBC_SOURCE) && !codec->lock)
+ break;
+
+ bytes_left -= codec->length;
+ codec = (const codec_capabilities_t*) ((const uint8_t*) codec + codec->length);
+ }
+
+ if (bytes_left <= 0 || codec->length != sizeof(u->a2dp.sbc_capabilities))
+ return -1;
+
+ pa_assert(codec->type == BT_A2DP_SBC_SOURCE);
+
+ if (codec->configured && seid == 0)
+ return codec->seid;
+
+ memcpy(&u->a2dp.sbc_capabilities, codec, sizeof(u->a2dp.sbc_capabilities));
}
return 0;
msg.getcaps_req.seid = seid;
pa_strlcpy(msg.getcaps_req.object, u->path, sizeof(msg.getcaps_req.object));
- if (u->profile == PROFILE_A2DP)
+ if (u->profile == PROFILE_A2DP || u->profile == PROFILE_A2DP_SOURCE)
msg.getcaps_req.transport = BT_CAPABILITIES_TRANSPORT_A2DP;
else {
- pa_assert(u->profile == PROFILE_HSP);
+ pa_assert(u->profile == PROFILE_HSP || u->profile == PROFILE_HFGW);
msg.getcaps_req.transport = BT_CAPABILITIES_TRANSPORT_SCO;
}
- msg.getcaps_req.flags = BT_FLAG_AUTOCONNECT;
+ msg.getcaps_req.flags = u->auto_connect ? BT_FLAG_AUTOCONNECT : 0;
if (service_send(u, &msg.getcaps_req.h) < 0)
return -1;
};
pa_assert(u);
- pa_assert(u->profile == PROFILE_A2DP);
+ pa_assert(u->profile == PROFILE_A2DP || u->profile == PROFILE_A2DP_SOURCE);
cap = &u->a2dp.sbc_capabilities;
msg.open_req.h.length = sizeof(msg.open_req);
pa_strlcpy(msg.open_req.object, u->path, sizeof(msg.open_req.object));
- msg.open_req.seid = u->profile == PROFILE_A2DP ? u->a2dp.sbc_capabilities.capability.seid : BT_A2DP_SEID_RANGE + 1;
- msg.open_req.lock = u->profile == PROFILE_A2DP ? BT_WRITE_LOCK : BT_READ_LOCK | BT_WRITE_LOCK;
+ msg.open_req.seid = (u->profile == PROFILE_A2DP || u->profile == PROFILE_A2DP_SOURCE) ? u->a2dp.sbc_capabilities.capability.seid : BT_A2DP_SEID_RANGE + 1;
+ msg.open_req.lock = (u->profile == PROFILE_A2DP) ? BT_WRITE_LOCK : BT_READ_LOCK | BT_WRITE_LOCK;
if (service_send(u, &msg.open_req.h) < 0)
return -1;
if (service_expect(u, &msg.open_rsp.h, sizeof(msg), BT_OPEN, sizeof(msg.open_rsp)) < 0)
return -1;
- if (u->profile == PROFILE_A2DP ) {
+ if (u->profile == PROFILE_A2DP || u->profile == PROFILE_A2DP_SOURCE) {
u->sample_spec.format = PA_SAMPLE_S16LE;
if (setup_a2dp(u) < 0)
return -1;
} else {
- pa_assert(u->profile == PROFILE_HSP);
+ pa_assert(u->profile == PROFILE_HSP || u->profile == PROFILE_HFGW);
u->sample_spec.format = PA_SAMPLE_S16LE;
u->sample_spec.channels = 1;
msg.setconf_req.h.name = BT_SET_CONFIGURATION;
msg.setconf_req.h.length = sizeof(msg.setconf_req);
- if (u->profile == PROFILE_A2DP) {
+ if (u->profile == PROFILE_A2DP || u->profile == PROFILE_A2DP_SOURCE) {
memcpy(&msg.setconf_req.codec, &u->a2dp.sbc_capabilities, sizeof(u->a2dp.sbc_capabilities));
} else {
msg.setconf_req.codec.transport = BT_CAPABILITIES_TRANSPORT_SCO;
u->link_mtu = msg.setconf_rsp.link_mtu;
/* setup SBC encoder now we agree on parameters */
- if (u->profile == PROFILE_A2DP) {
+ if (u->profile == PROFILE_A2DP || u->profile == PROFILE_A2DP_SOURCE) {
setup_sbc(&u->a2dp);
u->block_size =
}
/* from IO thread, except in SCO over PCM */
+
+static int setup_stream(struct userdata *u) {
+ struct pollfd *pollfd;
+ int one;
+
+ pa_make_fd_nonblock(u->stream_fd);
+ pa_make_socket_low_delay(u->stream_fd);
+
+ one = 1;
+ if (setsockopt(u->stream_fd, SOL_SOCKET, SO_TIMESTAMP, &one, sizeof(one)) < 0)
+ pa_log_warn("Failed to enable SO_TIMESTAMP: %s", pa_cstrerror(errno));
+
+ pa_log_debug("Stream properly set up, we're ready to roll!");
+
+ u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
+ pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
+ pollfd->fd = u->stream_fd;
+ pollfd->events = pollfd->revents = 0;
+
+ u->read_index = u->write_index = 0;
+ u->started_at = 0;
+
+ if (u->source)
+ u->read_smoother = pa_smoother_new(
+ PA_USEC_PER_SEC,
+ PA_USEC_PER_SEC*2,
+ TRUE,
+ TRUE,
+ 10,
+ pa_rtclock_now(),
+ TRUE);
+
+ return 0;
+}
+
static int start_stream_fd(struct userdata *u) {
union {
bt_audio_msg_header_t rsp;
bt_audio_error_t error;
uint8_t buf[BT_SUGGESTED_BUFFER_SIZE];
} msg;
- struct pollfd *pollfd;
- int one;
pa_assert(u);
pa_assert(u->rtpoll);
return -1;
}
- pa_make_fd_nonblock(u->stream_fd);
- pa_make_socket_low_delay(u->stream_fd);
-
- one = 1;
- if (setsockopt(u->stream_fd, SOL_SOCKET, SO_TIMESTAMP, &one, sizeof(one)) < 0)
- pa_log_warn("Failed to enable SO_TIMESTAMP: %s", pa_cstrerror(errno));
-
- pa_log_debug("Stream properly set up, we're ready to roll!");
-
- u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
- pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
- pollfd->fd = u->stream_fd;
- pollfd->events = pollfd->revents = 0;
-
- u->read_index = u->write_index = 0;
- u->started_at = 0;
-
- if (u->source)
- u->read_smoother = pa_smoother_new(
- PA_USEC_PER_SEC,
- PA_USEC_PER_SEC*2,
- TRUE,
- TRUE,
- 10,
- pa_rtclock_now(),
- TRUE);
-
- return 0;
+ return setup_stream(u);
}
/* from IO thread */
pa_assert(u);
pa_assert(u->rtpoll);
- pa_assert(u->rtpoll_item);
- pa_assert(u->stream_fd >= 0);
- pa_rtpoll_item_free(u->rtpoll_item);
- u->rtpoll_item = NULL;
+ if (u->rtpoll_item) {
+ pa_rtpoll_item_free(u->rtpoll_item);
+ u->rtpoll_item = NULL;
+ }
- memset(msg.buf, 0, BT_SUGGESTED_BUFFER_SIZE);
- msg.start_req.h.type = BT_REQUEST;
- msg.start_req.h.name = BT_STOP_STREAM;
- msg.start_req.h.length = sizeof(msg.start_req);
+ if (u->stream_fd >= 0) {
+ memset(msg.buf, 0, BT_SUGGESTED_BUFFER_SIZE);
+ msg.start_req.h.type = BT_REQUEST;
+ msg.start_req.h.name = BT_STOP_STREAM;
+ msg.start_req.h.length = sizeof(msg.start_req);
- if (service_send(u, &msg.start_req.h) < 0 ||
- service_expect(u, &msg.rsp, sizeof(msg), BT_STOP_STREAM, sizeof(msg.start_rsp)) < 0)
- r = -1;
+ if (service_send(u, &msg.start_req.h) < 0 ||
+ service_expect(u, &msg.rsp, sizeof(msg), BT_STOP_STREAM, sizeof(msg.start_rsp)) < 0)
+ r = -1;
- pa_close(u->stream_fd);
- u->stream_fd = -1;
+ pa_close(u->stream_fd);
+ u->stream_fd = -1;
+ }
if (u->read_smoother) {
pa_smoother_free(u->read_smoother);
return r;
}
+static void bt_transport_release(struct userdata *u)
+{
+ const char *accesstype = "rw";
+ const pa_bluetooth_transport *t;
+
+ /* Ignore if already released */
+ if (!u->accesstype)
+ return;
+
+ pa_log_debug("Releasing transport %s", u->transport);
+
+ t = pa_bluetooth_discovery_get_transport(u->discovery, u->transport);
+ if (t)
+ pa_bluetooth_transport_release(t, accesstype);
+
+ pa_xfree(u->accesstype);
+ u->accesstype = NULL;
+
+ if (u->rtpoll_item) {
+ pa_rtpoll_item_free(u->rtpoll_item);
+ u->rtpoll_item = NULL;
+ }
+
+ if (u->stream_fd >= 0) {
+ pa_close(u->stream_fd);
+ u->stream_fd = -1;
+ }
+
+ if (u->read_smoother) {
+ pa_smoother_free(u->read_smoother);
+ u->read_smoother = NULL;
+ }
+}
+
+static int bt_transport_acquire(struct userdata *u, pa_bool_t start)
+{
+ const char *accesstype = "rw";
+ const pa_bluetooth_transport *t;
+
+ if (u->accesstype) {
+ if (start)
+ goto done;
+ return 0;
+ }
+
+ pa_log_debug("Acquiring transport %s", u->transport);
+
+ t = pa_bluetooth_discovery_get_transport(u->discovery, u->transport);
+ if (!t) {
+ pa_log("Transport %s no longer available", u->transport);
+ pa_xfree(u->transport);
+ u->transport = NULL;
+ return -1;
+ }
+
+ u->stream_fd = pa_bluetooth_transport_acquire(t, accesstype);
+ if (u->stream_fd < 0)
+ return -1;
+
+ u->accesstype = pa_xstrdup(accesstype);
+ pa_log_info("Transport %s acquired: fd %d", u->transport, u->stream_fd);
+
+ if (!start)
+ return 0;
+
+done:
+ pa_log_info("Transport %s resuming", u->transport);
+ return setup_stream(u);
+}
+
/* Run from IO thread */
static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
struct userdata *u = PA_SINK(o)->userdata;
pa_assert(PA_SINK_IS_OPENED(u->sink->thread_info.state));
/* Stop the device if the source is suspended as well */
- if (!u->source || u->source->state == PA_SOURCE_SUSPENDED)
+ if (!u->source || u->source->state == PA_SOURCE_SUSPENDED) {
/* We deliberately ignore whether stopping
* actually worked. Since the stream_fd is
* closed it doesn't really matter */
- stop_stream_fd(u);
+ if (u->transport)
+ bt_transport_release(u);
+ else
+ stop_stream_fd(u);
+ }
break;
break;
/* Resume the device if the source was suspended as well */
- if (!u->source || u->source->state == PA_SOURCE_SUSPENDED)
- if (start_stream_fd(u) < 0)
+ if (!u->source || u->source->state == PA_SOURCE_SUSPENDED) {
+ if (u->transport) {
+ if (bt_transport_acquire(u, TRUE) < 0)
+ failed = TRUE;
+ } else if (start_stream_fd(u) < 0)
failed = TRUE;
+ }
break;
case PA_SINK_UNLINKED:
pa_assert(PA_SOURCE_IS_OPENED(u->source->thread_info.state));
/* Stop the device if the sink is suspended as well */
- if (!u->sink || u->sink->state == PA_SINK_SUSPENDED)
- stop_stream_fd(u);
+ if (!u->sink || u->sink->state == PA_SINK_SUSPENDED) {
+ if (u->transport)
+ bt_transport_release(u);
+ else
+ stop_stream_fd(u);
+ }
if (u->read_smoother)
pa_smoother_pause(u->read_smoother, pa_rtclock_now());
break;
/* Resume the device if the sink was suspended as well */
- if (!u->sink || u->sink->thread_info.state == PA_SINK_SUSPENDED)
- if (start_stream_fd(u) < 0)
+ if (!u->sink || u->sink->thread_info.state == PA_SINK_SUSPENDED) {
+ if (u->transport) {
+ if (bt_transport_acquire(u, TRUE) < 0)
failed = TRUE;
-
+ } else if (start_stream_fd(u) < 0)
+ failed = TRUE;
+ }
/* We don't resume the smoother here. Instead we
* wait until the first packet arrives */
break;
case PA_SOURCE_MESSAGE_GET_LATENCY: {
pa_usec_t wi, ri;
- wi = pa_smoother_get(u->read_smoother, pa_rtclock_now());
- ri = pa_bytes_to_usec(u->read_index, &u->sample_spec);
+ if (u->read_smoother) {
+ wi = pa_smoother_get(u->read_smoother, pa_rtclock_now());
+ ri = pa_bytes_to_usec(u->read_index, &u->sample_spec);
+
+ *((pa_usec_t*) data) = (wi > ri ? wi - ri : 0) + u->source->thread_info.fixed_latency;
+ } else
+ *((pa_usec_t*) data) = 0;
- *((pa_usec_t*) data) = (wi > ri ? wi - ri : 0) + u->source->thread_info.fixed_latency;
return 0;
}
int ret = 0;
pa_assert(u);
- pa_assert(u->profile == PROFILE_HSP);
+ pa_assert(u->profile == PROFILE_HSP || u->profile == PROFILE_HFGW);
pa_assert(u->sink);
/* First, render some data */
pa_memchunk memchunk;
pa_assert(u);
- pa_assert(u->profile == PROFILE_HSP);
+ pa_assert(u->profile == PROFILE_HSP || u->profile == PROFILE_HFGW);
pa_assert(u->source);
pa_assert(u->read_smoother);
return ret;
}
+static int a2dp_process_push(struct userdata *u) {
+ int ret = 0;
+ pa_memchunk memchunk;
+
+ pa_assert(u);
+ pa_assert(u->profile == PROFILE_A2DP_SOURCE);
+ pa_assert(u->source);
+ pa_assert(u->read_smoother);
+
+ memchunk.memblock = pa_memblock_new(u->core->mempool, u->block_size);
+ memchunk.index = memchunk.length = 0;
+
+ for (;;) {
+ pa_bool_t found_tstamp = FALSE;
+ pa_usec_t tstamp;
+ struct a2dp_info *a2dp;
+ struct rtp_header *header;
+ struct rtp_payload *payload;
+ const void *p;
+ void *d;
+ ssize_t l;
+ size_t to_write, to_decode;
+ unsigned frame_count;
+
+ a2dp_prepare_buffer(u);
+
+ a2dp = &u->a2dp;
+ header = a2dp->buffer;
+ payload = (struct rtp_payload*) ((uint8_t*) a2dp->buffer + sizeof(*header));
+
+ l = pa_read(u->stream_fd, a2dp->buffer, a2dp->buffer_size, &u->stream_write_type);
+
+ if (l <= 0) {
+
+ if (l < 0 && errno == EINTR)
+ /* Retry right away if we got interrupted */
+ continue;
+
+ else if (l < 0 && errno == EAGAIN)
+ /* Hmm, apparently the socket was not readable, give up for now. */
+ break;
+
+ pa_log_error("Failed to read data from socket: %s", l < 0 ? pa_cstrerror(errno) : "EOF");
+ ret = -1;
+ break;
+ }
+
+ pa_assert((size_t) l <= a2dp->buffer_size);
+
+ u->read_index += (uint64_t) l;
+
+ /* TODO: get timestamp from rtp */
+ if (!found_tstamp) {
+ /* pa_log_warn("Couldn't find SO_TIMESTAMP data in auxiliary recvmsg() data!"); */
+ tstamp = pa_rtclock_now();
+ }
+
+ pa_smoother_put(u->read_smoother, tstamp, pa_bytes_to_usec(u->read_index, &u->sample_spec));
+ pa_smoother_resume(u->read_smoother, tstamp, TRUE);
+
+ p = (uint8_t*) a2dp->buffer + sizeof(*header) + sizeof(*payload);
+ to_decode = l - sizeof(*header) - sizeof(*payload);
+
+ d = pa_memblock_acquire(memchunk.memblock);
+ to_write = memchunk.length = pa_memblock_get_length(memchunk.memblock);
+
+ while (PA_LIKELY(to_decode > 0 && to_write > 0)) {
+ size_t written;
+ ssize_t decoded;
+
+ decoded = sbc_decode(&a2dp->sbc,
+ p, to_decode,
+ d, to_write,
+ &written);
+
+ if (PA_UNLIKELY(decoded <= 0)) {
+ pa_log_error("SBC decoding error (%li)", (long) decoded);
+ pa_memblock_release(memchunk.memblock);
+ pa_memblock_unref(memchunk.memblock);
+ return -1;
+ }
+
+/* pa_log_debug("SBC: decoded: %lu; written: %lu", (unsigned long) decoded, (unsigned long) written); */
+/* pa_log_debug("SBC: frame_length: %lu; codesize: %lu", (unsigned long) a2dp->frame_length, (unsigned long) a2dp->codesize); */
+
+ pa_assert_fp((size_t) decoded <= to_decode);
+ pa_assert_fp((size_t) decoded == a2dp->frame_length);
+
+ pa_assert_fp((size_t) written <= to_write);
+ pa_assert_fp((size_t) written == a2dp->codesize);
+
+ p = (const uint8_t*) p + decoded;
+ to_decode -= decoded;
+
+ d = (uint8_t*) d + written;
+ to_write -= written;
+
+ frame_count++;
+ }
+
+ pa_memblock_release(memchunk.memblock);
+
+ pa_source_post(u->source, &memchunk);
+
+ ret = 1;
+ break;
+ }
+
+ pa_memblock_unref(memchunk.memblock);
+
+ return ret;
+}
+
static void thread_func(void *userdata) {
struct userdata *u = userdata;
unsigned do_write = 0;
pa_thread_mq_install(&u->thread_mq);
- if (start_stream_fd(u) < 0)
+ if (u->transport) {
+ if (bt_transport_acquire(u, TRUE) < 0)
+ goto fail;
+ } else if (start_stream_fd(u) < 0)
goto fail;
for (;;) {
if (pollfd && (pollfd->revents & POLLIN)) {
int n_read;
- if ((n_read = hsp_process_push(u)) < 0)
+ if (u->profile == PROFILE_HSP || PROFILE_HFGW)
+ n_read = hsp_process_push(u);
+ else
+ n_read = a2dp_process_push(u);
+
+ if (n_read < 0)
goto fail;
/* We just read something, so we are supposed to write something, too */
if (u->service_fd >= 0)
return;
+ init_bt(u);
+
pa_log_debug("Resuming SCO over PCM");
- if ((init_bt(u) < 0) || (init_profile(u) < 0))
+ if (init_profile(u) < 0)
pa_log("Can't resume SCO over PCM");
- start_stream_fd(u);
+ if (u->transport)
+ bt_transport_acquire(u, TRUE);
+ else
+ start_stream_fd(u);
} else {
if (u->service_fd < 0)
return;
- stop_stream_fd(u);
+ if (u->transport)
+ bt_transport_release(u);
+ else
+ stop_stream_fd(u);
pa_log_debug("Closing SCO over PCM");
pa_close(u->service_fd);
data.driver = __FILE__;
data.module = u->module;
pa_source_new_data_set_sample_spec(&data, &u->sample_spec);
- pa_proplist_sets(data.proplist, "bluetooth.protocol", u->profile == PROFILE_A2DP ? "a2dp" : "hsp");
- if (u->profile == PROFILE_HSP)
+ pa_proplist_sets(data.proplist, "bluetooth.protocol", u->profile == PROFILE_A2DP_SOURCE ? "a2dp_source" : "hsp");
+ if ((u->profile == PROFILE_HSP) || (u->profile == PROFILE_HFGW))
pa_proplist_sets(data.proplist, PA_PROP_DEVICE_INTENDED_ROLES, "phone");
data.card = u->card;
data.name = get_name("source", u->modargs, u->address, &b);
u->source->parent.process_msg = source_process_msg;
pa_source_set_fixed_latency(u->source,
- (/* u->profile == PROFILE_A2DP ? FIXED_LATENCY_RECORD_A2DP : */ FIXED_LATENCY_RECORD_HSP) +
+ (u->profile == PROFILE_A2DP_SOURCE ? FIXED_LATENCY_RECORD_A2DP : FIXED_LATENCY_RECORD_HSP) +
pa_bytes_to_usec(u->block_size, &u->sample_spec));
}
- if (u->profile == PROFILE_HSP) {
+ if (u->profile == PROFILE_HSP || u->profile == PROFILE_HFGW)
pa_proplist_sets(u->source->proplist, "bluetooth.nrec", (u->hsp.pcm_capabilities.flags & BT_PCM_FLAG_NREC) ? "1" : "0");
+
+ if (u->profile == PROFILE_HSP) {
u->source->set_volume = source_set_volume_cb;
u->source->n_volume_steps = 16;
}
}
}
+static int bt_transport_config_a2dp(struct userdata *u)
+{
+ const pa_bluetooth_transport *t;
+ struct a2dp_info *a2dp = &u->a2dp;
+ sbc_capabilities_raw_t *config;
+
+ t = pa_bluetooth_discovery_get_transport(u->discovery, u->transport);
+ pa_assert(t);
+
+ config = (sbc_capabilities_raw_t *) t->config;
+
+ if (a2dp->sbc_initialized)
+ sbc_reinit(&a2dp->sbc, 0);
+ else
+ sbc_init(&a2dp->sbc, 0);
+ a2dp->sbc_initialized = TRUE;
+
+ switch (config->frequency) {
+ case BT_SBC_SAMPLING_FREQ_16000:
+ a2dp->sbc.frequency = SBC_FREQ_16000;
+ break;
+ case BT_SBC_SAMPLING_FREQ_32000:
+ a2dp->sbc.frequency = SBC_FREQ_32000;
+ break;
+ case BT_SBC_SAMPLING_FREQ_44100:
+ a2dp->sbc.frequency = SBC_FREQ_44100;
+ break;
+ case BT_SBC_SAMPLING_FREQ_48000:
+ a2dp->sbc.frequency = SBC_FREQ_48000;
+ break;
+ default:
+ pa_assert_not_reached();
+ }
+
+ switch (config->channel_mode) {
+ case BT_A2DP_CHANNEL_MODE_MONO:
+ a2dp->sbc.mode = SBC_MODE_MONO;
+ break;
+ case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL:
+ a2dp->sbc.mode = SBC_MODE_DUAL_CHANNEL;
+ break;
+ case BT_A2DP_CHANNEL_MODE_STEREO:
+ a2dp->sbc.mode = SBC_MODE_STEREO;
+ break;
+ case BT_A2DP_CHANNEL_MODE_JOINT_STEREO:
+ a2dp->sbc.mode = SBC_MODE_JOINT_STEREO;
+ break;
+ default:
+ pa_assert_not_reached();
+ }
+
+ switch (config->allocation_method) {
+ case BT_A2DP_ALLOCATION_SNR:
+ a2dp->sbc.allocation = SBC_AM_SNR;
+ break;
+ case BT_A2DP_ALLOCATION_LOUDNESS:
+ a2dp->sbc.allocation = SBC_AM_LOUDNESS;
+ break;
+ default:
+ pa_assert_not_reached();
+ }
+
+ switch (config->subbands) {
+ case BT_A2DP_SUBBANDS_4:
+ a2dp->sbc.subbands = SBC_SB_4;
+ break;
+ case BT_A2DP_SUBBANDS_8:
+ a2dp->sbc.subbands = SBC_SB_8;
+ break;
+ default:
+ pa_assert_not_reached();
+ }
+
+ switch (config->block_length) {
+ case BT_A2DP_BLOCK_LENGTH_4:
+ a2dp->sbc.blocks = SBC_BLK_4;
+ break;
+ case BT_A2DP_BLOCK_LENGTH_8:
+ a2dp->sbc.blocks = SBC_BLK_8;
+ break;
+ case BT_A2DP_BLOCK_LENGTH_12:
+ a2dp->sbc.blocks = SBC_BLK_12;
+ break;
+ case BT_A2DP_BLOCK_LENGTH_16:
+ a2dp->sbc.blocks = SBC_BLK_16;
+ break;
+ default:
+ pa_assert_not_reached();
+ }
+
+ a2dp->sbc.bitpool = config->max_bitpool;
+ a2dp->codesize = sbc_get_codesize(&a2dp->sbc);
+ a2dp->frame_length = sbc_get_frame_length(&a2dp->sbc);
+
+ u->block_size =
+ ((u->link_mtu - sizeof(struct rtp_header) - sizeof(struct rtp_payload))
+ / a2dp->frame_length
+ * a2dp->codesize);
+
+ pa_log_info("SBC parameters:\n\tallocation=%u\n\tsubbands=%u\n\tblocks=%u\n\tbitpool=%u\n",
+ a2dp->sbc.allocation, a2dp->sbc.subbands, a2dp->sbc.blocks, a2dp->sbc.bitpool);
+
+ return 0;
+}
+
+static int bt_transport_config(struct userdata *u)
+{
+ if (u->profile == PROFILE_HSP || u->profile == PROFILE_HFGW) {
+ u->block_size = u->link_mtu;
+ return 0;
+ }
+
+ return bt_transport_config_a2dp(u);
+}
+
+static int parse_transport_property(struct userdata *u, DBusMessageIter *i)
+{
+ const char *key;
+ DBusMessageIter variant_i;
+
+ pa_assert(u);
+ pa_assert(i);
+
+ if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_STRING) {
+ pa_log("Property name not a string.");
+ return -1;
+ }
+
+ dbus_message_iter_get_basic(i, &key);
+
+ if (!dbus_message_iter_next(i)) {
+ pa_log("Property value missing");
+ return -1;
+ }
+
+ if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_VARIANT) {
+ pa_log("Property value not a variant.");
+ return -1;
+ }
+
+ dbus_message_iter_recurse(i, &variant_i);
+
+ switch (dbus_message_iter_get_arg_type(&variant_i)) {
+
+ case DBUS_TYPE_UINT16: {
+
+ uint16_t value;
+ dbus_message_iter_get_basic(&variant_i, &value);
+
+ if (pa_streq(key, "OMTU"))
+ u->link_mtu = value;
+
+ break;
+ }
+
+ }
+
+ return 0;
+}
+
+/* Run from main thread */
+static int bt_transport_open(struct userdata *u)
+{
+ DBusMessage *m, *r;
+ DBusMessageIter arg_i, element_i;
+ DBusError err;
+
+ if (bt_transport_acquire(u, FALSE) < 0)
+ return -1;
+
+ dbus_error_init(&err);
+
+ pa_assert_se(m = dbus_message_new_method_call("org.bluez", u->transport, "org.bluez.MediaTransport", "GetProperties"));
+ r = dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(u->connection), m, -1, &err);
+
+ if (dbus_error_is_set(&err) || !r) {
+ pa_log("Failed to get transport properties: %s", err.message);
+ goto fail;
+ }
+
+ if (!dbus_message_iter_init(r, &arg_i)) {
+ pa_log("GetProperties reply has no arguments.");
+ goto fail;
+ }
+
+ if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_ARRAY) {
+ pa_log("GetProperties argument is not an array.");
+ goto fail;
+ }
+
+ dbus_message_iter_recurse(&arg_i, &element_i);
+ while (dbus_message_iter_get_arg_type(&element_i) != DBUS_TYPE_INVALID) {
+
+ if (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_DICT_ENTRY) {
+ DBusMessageIter dict_i;
+
+ dbus_message_iter_recurse(&element_i, &dict_i);
+
+ parse_transport_property(u, &dict_i);
+ }
+
+ if (!dbus_message_iter_next(&element_i))
+ break;
+ }
+
+ return bt_transport_config(u);
+
+fail:
+ dbus_message_unref(r);
+ return -1;
+}
+
/* Run from main thread */
static int init_bt(struct userdata *u) {
pa_assert(u);
u->service_read_type = 0;
if ((u->service_fd = bt_audio_service_open()) < 0) {
- pa_log_error("Couldn't connect to bluetooth audio service");
+ pa_log_warn("Bluetooth audio service not available");
return -1;
}
/* Run from main thread */
static int setup_bt(struct userdata *u) {
+ const pa_bluetooth_device *d;
+ const pa_bluetooth_transport *t;
+
pa_assert(u);
+ if (!(d = pa_bluetooth_discovery_get_by_path(u->discovery, u->path))) {
+ pa_log_error("Failed to get device object.");
+ return -1;
+ }
+
+ /* release transport if exist */
+ if (u->transport) {
+ bt_transport_release(u);
+ pa_xfree(u->transport);
+ u->transport = NULL;
+ }
+
+ /* check if profile has a transport */
+ t = pa_bluetooth_device_get_transport(d, u->profile);
+ if (t) {
+ u->transport = pa_xstrdup(t->path);
+ return bt_transport_open(u);
+ }
+
if (get_caps(u, 0) < 0)
return -1;
return -1;
if (u->profile == PROFILE_A2DP ||
- u->profile == PROFILE_HSP)
+ u->profile == PROFILE_HSP ||
+ u->profile == PROFILE_HFGW)
if (add_sink(u) < 0)
r = -1;
- if (u->profile == PROFILE_HSP)
+ if (u->profile == PROFILE_HSP ||
+ u->profile == PROFILE_A2DP_SOURCE ||
+ u->profile == PROFILE_HFGW)
if (add_source(u) < 0)
r = -1;
#ifdef NOKIA
if (USE_SCO_OVER_PCM(u)) {
- if (start_stream_fd(u) < 0)
+ if (u->transport) {
+ if (bt_transport_acquire(u, TRUE) < 0)
+ return -1;
+ } else if (start_stream_fd(u) < 0)
return -1;
pa_sink_ref(u->sink);
}
#endif
- if (!(u->thread = pa_thread_new(thread_func, u))) {
+ if (!(u->thread = pa_thread_new("bluetooth", thread_func, u))) {
pa_log_error("Failed to create IO thread");
stop_thread(u);
return -1;
pa_log_warn("A2DP is not connected, refused to switch profile");
return -PA_ERR_IO;
}
+ else if (device->hfgw_state <= PA_BT_AUDIO_STATE_CONNECTED && *d == PROFILE_HFGW) {
+ pa_log_warn("HandsfreeGateway is not connected, refused to switch profile");
+ return -PA_ERR_IO;
+ }
if (u->sink) {
inputs = pa_sink_move_all_start(u->sink, NULL);
pa_hashmap_put(data.profiles, p->name, p);
}
+ if (pa_bluetooth_uuid_has(device->uuids, A2DP_SOURCE_UUID)) {
+ p = pa_card_profile_new("a2dp_source", _("High Fidelity Capture (A2DP)"), sizeof(enum profile));
+ p->priority = 10;
+ p->n_sinks = 0;
+ p->n_sources = 1;
+ p->max_sink_channels = 0;
+ p->max_source_channels = 2;
+
+ d = PA_CARD_PROFILE_DATA(p);
+ *d = PROFILE_A2DP_SOURCE;
+
+ pa_hashmap_put(data.profiles, p->name, p);
+ }
+
if (pa_bluetooth_uuid_has(device->uuids, HSP_HS_UUID) ||
pa_bluetooth_uuid_has(device->uuids, HFP_HS_UUID)) {
p = pa_card_profile_new("hsp", _("Telephony Duplex (HSP/HFP)"), sizeof(enum profile));
pa_hashmap_put(data.profiles, p->name, p);
}
+ if (pa_bluetooth_uuid_has(device->uuids, HFP_AG_UUID)) {
+ p = pa_card_profile_new("hfgw", _("Handsfree Gateway"), sizeof(enum profile));
+ p->priority = 20;
+ p->n_sinks = 1;
+ p->n_sources = 1;
+ p->max_sink_channels = 1;
+ p->max_source_channels = 1;
+
+ d = PA_CARD_PROFILE_DATA(p);
+ *d = PROFILE_HFGW;
+
+ pa_hashmap_put(data.profiles, p->name, p);
+ }
+
pa_assert(!pa_hashmap_isempty(data.profiles));
p = pa_card_profile_new("off", _("Off"), sizeof(enum profile));
d = PA_CARD_PROFILE_DATA(u->card->active_profile);
if ((device->headset_state < PA_BT_AUDIO_STATE_CONNECTED && *d == PROFILE_HSP) ||
- (device->audio_sink_state < PA_BT_AUDIO_STATE_CONNECTED && *d == PROFILE_A2DP)) {
+ (device->audio_sink_state < PA_BT_AUDIO_STATE_CONNECTED && *d == PROFILE_A2DP) ||
+ (device->hfgw_state < PA_BT_AUDIO_STATE_CONNECTED && *d == PROFILE_HFGW)) {
pa_log_warn("Default profile not connected, selecting off profile");
u->card->active_profile = pa_hashmap_get(u->card->profiles, "off");
u->card->save_profile = FALSE;
}
if (address && !(pa_streq(d->address, address))) {
- pa_log_error("Passed path %s and address %s don't match.", path, address);
+ pa_log_error("Passed path %s address %s != %s don't match.", path, d->address, address);
return NULL;
}
goto fail;
}
+ u->auto_connect = TRUE;
+ if (pa_modargs_get_value_boolean(ma, "auto_connect", &u->auto_connect)) {
+ pa_log("Failed to parse auto_connect= argument");
+ goto fail;
+ }
+
channels = u->sample_spec.channels;
if (pa_modargs_get_value_u32(ma, "channels", &channels) < 0 ||
channels <= 0 || channels > PA_CHANNELS_MAX) {
if (add_card(u, device) < 0)
goto fail;
- /* Connect to the BT service and query capabilities */
- if (init_bt(u) < 0)
- goto fail;
-
if (!dbus_connection_add_filter(pa_dbus_connection_get(u->connection), filter_cb, u, NULL)) {
pa_log_error("Failed to add filter function");
goto fail;
}
+ u->filter_added = TRUE;
speaker = pa_sprintf_malloc("type='signal',sender='org.bluez',interface='org.bluez.Headset',member='SpeakerGainChanged',path='%s'", u->path);
mike = pa_sprintf_malloc("type='signal',sender='org.bluez',interface='org.bluez.Headset',member='MicrophoneGainChanged',path='%s'", u->path);
pa_xfree(speaker);
pa_xfree(mike);
+ /* Connect to the BT service */
+ init_bt(u);
+
if (u->profile != PROFILE_OFF)
if (init_profile(u) < 0)
goto fail;
pa_xfree(mike);
}
- dbus_connection_remove_filter(pa_dbus_connection_get(u->connection), filter_cb, u);
+ if (u->filter_added)
+ dbus_connection_remove_filter(pa_dbus_connection_get(u->connection), filter_cb, u);
+
pa_dbus_connection_unref(u->connection);
}
pa_xfree(u->address);
pa_xfree(u->path);
+ if (u->transport) {
+ bt_transport_release(u);
+ pa_xfree(u->transport);
+ }
+
if (u->discovery)
pa_bluetooth_discovery_unref(u->discovery);