This file is part of PulseAudio.
Copyright 2008-2009 Joao Paulo Rechi Vita
+ Copyright 2011-2012 BMW Car IT GmbH.
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
#include <string.h>
#include <errno.h>
+#include <math.h>
#include <linux/sockios.h>
#include <arpa/inet.h>
#include <pulsecore/rtpoll.h>
#include <pulsecore/time-smoother.h>
#include <pulsecore/namereg.h>
-#include <pulsecore/dbus-shared.h>
+
+#include <sbc/sbc.h>
#include "module-bluetooth-device-symdef.h"
-#include "ipc.h"
-#include "sbc.h"
#include "a2dp-codecs.h"
#include "rtp.h"
#include "bluetooth-util.h"
#define BITPOOL_DEC_LIMIT 32
#define BITPOOL_DEC_STEP 5
-#define HSP_MAX_GAIN 15
PA_MODULE_AUTHOR("Joao Paulo Rechi Vita");
PA_MODULE_DESCRIPTION("Bluetooth audio sink and source");
PA_MODULE_VERSION(PACKAGE_VERSION);
-PA_MODULE_LOAD_ONCE(FALSE);
+PA_MODULE_LOAD_ONCE(false);
PA_MODULE_USAGE(
"name=<name for the card/sink/source, to be prefixed> "
"card_name=<name for the card> "
};
struct a2dp_info {
- sbc_capabilities_t sbc_capabilities;
sbc_t sbc; /* Codec data */
- pa_bool_t sbc_initialized; /* Keep track if the encoder is initialized */
+ bool sbc_initialized; /* Keep track if the encoder is initialized */
size_t codesize, frame_length; /* SBC Codesize, frame_length. We simply cache those values here */
void* buffer; /* Codec transfer buffer */
};
struct hsp_info {
- pcm_capabilities_t pcm_capabilities;
pa_sink *sco_sink;
void (*sco_sink_set_volume)(pa_sink *s);
pa_source *sco_source;
void (*sco_source_set_volume)(pa_source *s);
- pa_hook_slot *sink_state_changed_slot;
- pa_hook_slot *source_state_changed_slot;
};
struct bluetooth_msg {
pa_core *core;
pa_module *module;
+ pa_bluetooth_device *device;
+ pa_hook_slot *uuid_added_slot;
char *address;
char *path;
- char *transport;
- char *accesstype;
+ pa_bluetooth_transport *transport;
+ bool transport_acquired;
+ pa_hook_slot *discovery_slot;
+ pa_hook_slot *sink_state_changed_slot;
+ pa_hook_slot *source_state_changed_slot;
+ pa_hook_slot *transport_state_changed_slot;
+ pa_hook_slot *transport_nrec_changed_slot;
+ pa_hook_slot *transport_microphone_changed_slot;
+ pa_hook_slot *transport_speaker_changed_slot;
pa_bluetooth_discovery *discovery;
- pa_bool_t auto_connect;
+ bool auto_connect;
- pa_dbus_connection *connection;
+ char *output_port_name;
+ char *input_port_name;
pa_card *card;
pa_sink *sink;
pa_sample_spec sample_spec, requested_sample_spec;
- int service_fd;
int stream_fd;
- size_t link_mtu;
- size_t block_size;
+ size_t read_link_mtu;
+ size_t read_block_size;
+
+ size_t write_link_mtu;
+ size_t write_block_size;
struct a2dp_info a2dp;
struct hsp_info hsp;
pa_modargs *modargs;
int stream_write_type;
- int service_write_type, service_read_type;
-
- pa_bool_t filter_added;
};
enum {
- BLUETOOTH_MESSAGE_SET_PROFILE,
+ BLUETOOTH_MESSAGE_IO_THREAD_FAILED,
BLUETOOTH_MESSAGE_MAX
};
#define USE_SCO_OVER_PCM(u) (u->profile == PROFILE_HSP && (u->hsp.sco_sink && u->hsp.sco_source))
-static int init_bt(struct userdata *u);
static int init_profile(struct userdata *u);
-static int service_send(struct userdata *u, const bt_audio_msg_header_t *msg) {
- ssize_t r;
-
- pa_assert(u);
- 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)));
-
- if ((r = pa_loop_write(u->service_fd, msg, msg->length, &u->service_write_type)) == (ssize_t) msg->length)
- return 0;
-
- if (r < 0)
- pa_log_error("Error sending data to audio service: %s", pa_cstrerror(errno));
- else
- pa_log_error("Short write()");
-
- return -1;
-}
-
-static int service_recv(struct userdata *u, bt_audio_msg_header_t *msg, size_t room) {
- ssize_t r;
-
- pa_assert(u);
- pa_assert(u->service_fd >= 0);
- pa_assert(msg);
- pa_assert(room >= sizeof(*msg));
-
- pa_log_debug("Trying to receive message from audio service...");
-
- /* First, read the header */
- if ((r = pa_loop_read(u->service_fd, msg, sizeof(*msg), &u->service_read_type)) != sizeof(*msg))
- goto read_fail;
-
- if (msg->length < sizeof(*msg)) {
- pa_log_error("Invalid message size.");
- return -1;
- }
-
- if (msg->length > room) {
- pa_log_error("Not enough room.");
- return -1;
- }
-
- /* Secondly, read the payload */
- if (msg->length > sizeof(*msg)) {
-
- size_t remains = msg->length - sizeof(*msg);
-
- if ((r = pa_loop_read(u->service_fd,
- (uint8_t*) msg + sizeof(*msg),
- remains,
- &u->service_read_type)) != (ssize_t) remains)
- goto read_fail;
- }
-
- pa_log_debug("Received %s <- %s",
- pa_strnull(bt_audio_strtype(msg->type)),
- pa_strnull(bt_audio_strname(msg->name)));
-
- return 0;
-
-read_fail:
-
- if (r < 0)
- pa_log_error("Error receiving data from audio service: %s", pa_cstrerror(errno));
- else
- pa_log_error("Short read()");
-
- return -1;
-}
-
-static ssize_t service_expect(struct userdata*u, bt_audio_msg_header_t *rsp, size_t room, uint8_t expected_name, size_t expected_size) {
- int r;
-
- pa_assert(u);
- pa_assert(u->service_fd >= 0);
- pa_assert(rsp);
-
- if ((r = service_recv(u, rsp, room)) < 0)
- return r;
-
- if ((rsp->type != BT_INDICATION && rsp->type != BT_RESPONSE) ||
- rsp->name != expected_name ||
- (expected_size > 0 && rsp->length != expected_size)) {
-
- if (rsp->type == BT_ERROR && rsp->length == sizeof(bt_audio_error_t))
- pa_log_error("Received error condition: %s", pa_cstrerror(((bt_audio_error_t*) rsp)->posix_errno));
- else
- pa_log_error("Bogus message %s received while %s was expected",
- pa_strnull(bt_audio_strname(rsp->name)),
- pa_strnull(bt_audio_strname(expected_name)));
- return -1;
- }
-
- return 0;
-}
-
-/* Run from main thread */
-static int parse_caps(struct userdata *u, uint8_t seid, const struct bt_get_capabilities_rsp *rsp) {
- uint16_t bytes_left;
- const codec_capabilities_t *codec;
-
- pa_assert(u);
- pa_assert(rsp);
-
- bytes_left = rsp->h.length - sizeof(*rsp);
-
- if (bytes_left < sizeof(codec_capabilities_t)) {
- pa_log_error("Packet too small to store codec information.");
- return -1;
- }
-
- codec = (codec_capabilities_t *) rsp->data; /** ALIGNMENT? **/
-
- pa_log_debug("Payload size is %lu %lu", (unsigned long) bytes_left, (unsigned long) sizeof(*codec));
-
- 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 || u->profile == PROFILE_HFGW) {
-
- if (bytes_left <= 0 || codec->length != sizeof(u->hsp.pcm_capabilities))
- return -1;
-
- pa_assert(codec->type == BT_HFP_CODEC_PCM);
-
- if (codec->configured && seid == 0)
- return codec->seid;
-
- memcpy(&u->hsp.pcm_capabilities, codec, sizeof(u->hsp.pcm_capabilities));
-
- } else if (u->profile == PROFILE_A2DP) {
-
- while (bytes_left > 0) {
- if ((codec->type == BT_A2DP_SBC_SINK) && !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_SINK);
-
- if (codec->configured && seid == 0)
- 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;
-}
-
-/* Run from main thread */
-static int get_caps(struct userdata *u, uint8_t seid) {
- union {
- struct bt_get_capabilities_req getcaps_req;
- struct bt_get_capabilities_rsp getcaps_rsp;
- bt_audio_error_t error;
- uint8_t buf[BT_SUGGESTED_BUFFER_SIZE];
- } msg;
- int ret;
-
- pa_assert(u);
-
- memset(&msg, 0, sizeof(msg));
- msg.getcaps_req.h.type = BT_REQUEST;
- msg.getcaps_req.h.name = BT_GET_CAPABILITIES;
- msg.getcaps_req.h.length = sizeof(msg.getcaps_req);
- msg.getcaps_req.seid = seid;
-
- pa_strlcpy(msg.getcaps_req.object, u->path, sizeof(msg.getcaps_req.object));
- 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 || u->profile == PROFILE_HFGW);
- msg.getcaps_req.transport = BT_CAPABILITIES_TRANSPORT_SCO;
- }
- msg.getcaps_req.flags = u->auto_connect ? BT_FLAG_AUTOCONNECT : 0;
-
- if (service_send(u, &msg.getcaps_req.h) < 0)
- return -1;
-
- if (service_expect(u, &msg.getcaps_rsp.h, sizeof(msg), BT_GET_CAPABILITIES, 0) < 0)
- return -1;
-
- ret = parse_caps(u, seid, &msg.getcaps_rsp);
- if (ret <= 0)
- return ret;
-
- return get_caps(u, ret);
-}
-
-/* Run from main thread */
-static uint8_t a2dp_default_bitpool(uint8_t freq, uint8_t mode) {
-
- switch (freq) {
- case BT_SBC_SAMPLING_FREQ_16000:
- case BT_SBC_SAMPLING_FREQ_32000:
- return 53;
-
- case BT_SBC_SAMPLING_FREQ_44100:
-
- switch (mode) {
- case BT_A2DP_CHANNEL_MODE_MONO:
- case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL:
- return 31;
-
- case BT_A2DP_CHANNEL_MODE_STEREO:
- case BT_A2DP_CHANNEL_MODE_JOINT_STEREO:
- return 53;
-
- default:
- pa_log_warn("Invalid channel mode %u", mode);
- return 53;
- }
-
- case BT_SBC_SAMPLING_FREQ_48000:
-
- switch (mode) {
- case BT_A2DP_CHANNEL_MODE_MONO:
- case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL:
- return 29;
-
- case BT_A2DP_CHANNEL_MODE_STEREO:
- case BT_A2DP_CHANNEL_MODE_JOINT_STEREO:
- return 51;
-
- default:
- pa_log_warn("Invalid channel mode %u", mode);
- return 51;
- }
-
- default:
- pa_log_warn("Invalid sampling freq %u", freq);
- return 53;
- }
-}
-
-/* Run from main thread */
-static int setup_a2dp(struct userdata *u) {
- sbc_capabilities_t *cap;
- int i;
-
- static const struct {
- uint32_t rate;
- uint8_t cap;
- } freq_table[] = {
- { 16000U, BT_SBC_SAMPLING_FREQ_16000 },
- { 32000U, BT_SBC_SAMPLING_FREQ_32000 },
- { 44100U, BT_SBC_SAMPLING_FREQ_44100 },
- { 48000U, BT_SBC_SAMPLING_FREQ_48000 }
- };
-
- pa_assert(u);
- pa_assert(u->profile == PROFILE_A2DP || u->profile == PROFILE_A2DP_SOURCE);
-
- cap = &u->a2dp.sbc_capabilities;
-
- /* Find the lowest freq that is at least as high as the requested
- * sampling rate */
- for (i = 0; (unsigned) i < PA_ELEMENTSOF(freq_table); i++)
- if (freq_table[i].rate >= u->sample_spec.rate && (cap->frequency & freq_table[i].cap)) {
- u->sample_spec.rate = freq_table[i].rate;
- cap->frequency = freq_table[i].cap;
- break;
- }
-
- if ((unsigned) i == PA_ELEMENTSOF(freq_table)) {
- for (--i; i >= 0; i--) {
- if (cap->frequency & freq_table[i].cap) {
- u->sample_spec.rate = freq_table[i].rate;
- cap->frequency = freq_table[i].cap;
- break;
- }
- }
-
- if (i < 0) {
- pa_log("Not suitable sample rate");
- return -1;
- }
- }
-
- pa_assert((unsigned) i < PA_ELEMENTSOF(freq_table));
-
- if (cap->capability.configured)
- return 0;
-
- if (u->sample_spec.channels <= 1) {
- if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_MONO) {
- cap->channel_mode = BT_A2DP_CHANNEL_MODE_MONO;
- u->sample_spec.channels = 1;
- } else
- u->sample_spec.channels = 2;
- }
-
- if (u->sample_spec.channels >= 2) {
- u->sample_spec.channels = 2;
-
- if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_JOINT_STEREO)
- cap->channel_mode = BT_A2DP_CHANNEL_MODE_JOINT_STEREO;
- else if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_STEREO)
- cap->channel_mode = BT_A2DP_CHANNEL_MODE_STEREO;
- else if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL)
- cap->channel_mode = BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL;
- else if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_MONO) {
- cap->channel_mode = BT_A2DP_CHANNEL_MODE_MONO;
- u->sample_spec.channels = 1;
- } else {
- pa_log("No supported channel modes");
- return -1;
- }
- }
-
- if (cap->block_length & BT_A2DP_BLOCK_LENGTH_16)
- cap->block_length = BT_A2DP_BLOCK_LENGTH_16;
- else if (cap->block_length & BT_A2DP_BLOCK_LENGTH_12)
- cap->block_length = BT_A2DP_BLOCK_LENGTH_12;
- else if (cap->block_length & BT_A2DP_BLOCK_LENGTH_8)
- cap->block_length = BT_A2DP_BLOCK_LENGTH_8;
- else if (cap->block_length & BT_A2DP_BLOCK_LENGTH_4)
- cap->block_length = BT_A2DP_BLOCK_LENGTH_4;
- else {
- pa_log_error("No supported block lengths");
- return -1;
- }
-
- if (cap->subbands & BT_A2DP_SUBBANDS_8)
- cap->subbands = BT_A2DP_SUBBANDS_8;
- else if (cap->subbands & BT_A2DP_SUBBANDS_4)
- cap->subbands = BT_A2DP_SUBBANDS_4;
- else {
- pa_log_error("No supported subbands");
- return -1;
- }
-
- if (cap->allocation_method & BT_A2DP_ALLOCATION_LOUDNESS)
- cap->allocation_method = BT_A2DP_ALLOCATION_LOUDNESS;
- else if (cap->allocation_method & BT_A2DP_ALLOCATION_SNR)
- cap->allocation_method = BT_A2DP_ALLOCATION_SNR;
-
- cap->min_bitpool = (uint8_t) PA_MAX(MIN_BITPOOL, cap->min_bitpool);
- cap->max_bitpool = (uint8_t) PA_MIN(a2dp_default_bitpool(cap->frequency, cap->channel_mode), cap->max_bitpool);
-
- return 0;
-}
-
-/* Run from main thread */
-static void setup_sbc(struct a2dp_info *a2dp, enum profile p) {
- sbc_capabilities_t *active_capabilities;
-
- pa_assert(a2dp);
-
- active_capabilities = &a2dp->sbc_capabilities;
-
- if (a2dp->sbc_initialized)
- sbc_reinit(&a2dp->sbc, 0);
- else
- sbc_init(&a2dp->sbc, 0);
- a2dp->sbc_initialized = TRUE;
-
- switch (active_capabilities->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 (active_capabilities->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 (active_capabilities->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 (active_capabilities->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 (active_capabilities->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->min_bitpool = active_capabilities->min_bitpool;
- a2dp->max_bitpool = active_capabilities->max_bitpool;
-
- /* Set minimum bitpool for source to get the maximum possible block_size */
- a2dp->sbc.bitpool = p == PROFILE_A2DP ? a2dp->max_bitpool : a2dp->min_bitpool;
- a2dp->codesize = sbc_get_codesize(&a2dp->sbc);
- a2dp->frame_length = sbc_get_frame_length(&a2dp->sbc);
-}
-
-/* Run from main thread */
-static int set_conf(struct userdata *u) {
- union {
- struct bt_open_req open_req;
- struct bt_open_rsp open_rsp;
- struct bt_set_configuration_req setconf_req;
- struct bt_set_configuration_rsp setconf_rsp;
- bt_audio_error_t error;
- uint8_t buf[BT_SUGGESTED_BUFFER_SIZE];
- } msg;
-
- memset(&msg, 0, sizeof(msg));
- msg.open_req.h.type = BT_REQUEST;
- msg.open_req.h.name = BT_OPEN;
- 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->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 || 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 || u->profile == PROFILE_HFGW);
-
- u->sample_spec.format = PA_SAMPLE_S16LE;
- u->sample_spec.channels = 1;
- u->sample_spec.rate = 8000;
- }
-
- memset(&msg, 0, sizeof(msg));
- msg.setconf_req.h.type = BT_REQUEST;
- msg.setconf_req.h.name = BT_SET_CONFIGURATION;
- msg.setconf_req.h.length = sizeof(msg.setconf_req);
-
- 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;
- msg.setconf_req.codec.seid = BT_A2DP_SEID_RANGE + 1;
- msg.setconf_req.codec.length = sizeof(pcm_capabilities_t);
- }
- msg.setconf_req.h.length += msg.setconf_req.codec.length - sizeof(msg.setconf_req.codec);
-
- if (service_send(u, &msg.setconf_req.h) < 0)
- return -1;
-
- if (service_expect(u, &msg.setconf_rsp.h, sizeof(msg), BT_SET_CONFIGURATION, sizeof(msg.setconf_rsp)) < 0)
- return -1;
-
- u->link_mtu = msg.setconf_rsp.link_mtu;
-
- /* setup SBC encoder now we agree on parameters */
- if (u->profile == PROFILE_A2DP || u->profile == PROFILE_A2DP_SOURCE) {
- setup_sbc(&u->a2dp, u->profile);
-
- u->block_size =
- ((u->link_mtu - sizeof(struct rtp_header) - sizeof(struct rtp_payload))
- / u->a2dp.frame_length
- * u->a2dp.codesize);
-
- pa_log_info("SBC parameters:\n\tallocation=%u\n\tsubbands=%u\n\tblocks=%u\n\tbitpool=%u\n",
- u->a2dp.sbc.allocation, u->a2dp.sbc.subbands, u->a2dp.sbc.blocks, u->a2dp.sbc.bitpool);
- } else
- u->block_size = u->link_mtu;
-
- return 0;
-}
-
/* from IO thread */
static void a2dp_set_bitpool(struct userdata *u, uint8_t bitpool)
{
a2dp->sbc.bitpool = bitpool;
- a2dp->codesize = sbc_get_codesize(&a2dp->sbc);
- a2dp->frame_length = sbc_get_frame_length(&a2dp->sbc);
+ a2dp->codesize = sbc_get_codesize(&a2dp->sbc);
+ a2dp->frame_length = sbc_get_frame_length(&a2dp->sbc);
+
+ pa_log_debug("Bitpool has changed to %u", a2dp->sbc.bitpool);
+
+ u->read_block_size =
+ (u->read_link_mtu - sizeof(struct rtp_header) - sizeof(struct rtp_payload))
+ / a2dp->frame_length * a2dp->codesize;
+
+ u->write_block_size =
+ (u->write_link_mtu - sizeof(struct rtp_header) - sizeof(struct rtp_payload))
+ / a2dp->frame_length * a2dp->codesize;
+
+ pa_sink_set_max_request_within_thread(u->sink, u->write_block_size);
+ pa_sink_set_fixed_latency_within_thread(u->sink,
+ FIXED_LATENCY_PLAYBACK_A2DP + pa_bytes_to_usec(u->write_block_size, &u->sample_spec));
+}
+
+/* from IO thread, except in SCO over PCM */
+static void bt_transport_config_mtu(struct userdata *u) {
+ /* Calculate block sizes */
+ if (u->profile == PROFILE_HSP || u->profile == PROFILE_HFGW) {
+ u->read_block_size = u->read_link_mtu;
+ u->write_block_size = u->write_link_mtu;
+ } else {
+ u->read_block_size =
+ (u->read_link_mtu - sizeof(struct rtp_header) - sizeof(struct rtp_payload))
+ / u->a2dp.frame_length * u->a2dp.codesize;
+
+ u->write_block_size =
+ (u->write_link_mtu - sizeof(struct rtp_header) - sizeof(struct rtp_payload))
+ / u->a2dp.frame_length * u->a2dp.codesize;
+ }
- pa_log_debug("Bitpool has changed to %u", a2dp->sbc.bitpool);
+ if (USE_SCO_OVER_PCM(u))
+ return;
- u->block_size =
- (u->link_mtu - sizeof(struct rtp_header) - sizeof(struct rtp_payload))
- / a2dp->frame_length * a2dp->codesize;
+ if (u->sink) {
+ pa_sink_set_max_request_within_thread(u->sink, u->write_block_size);
+ pa_sink_set_fixed_latency_within_thread(u->sink,
+ (u->profile == PROFILE_A2DP ?
+ FIXED_LATENCY_PLAYBACK_A2DP : FIXED_LATENCY_PLAYBACK_HSP) +
+ pa_bytes_to_usec(u->write_block_size, &u->sample_spec));
+ }
- pa_sink_set_max_request_within_thread(u->sink, u->block_size);
- pa_sink_set_fixed_latency_within_thread(u->sink,
- FIXED_LATENCY_PLAYBACK_A2DP + pa_bytes_to_usec(u->block_size, &u->sample_spec));
+ if (u->source)
+ pa_source_set_fixed_latency_within_thread(u->source,
+ (u->profile == PROFILE_A2DP_SOURCE ?
+ FIXED_LATENCY_RECORD_A2DP : FIXED_LATENCY_RECORD_HSP) +
+ pa_bytes_to_usec(u->read_block_size, &u->sample_spec));
}
/* from IO thread, except in SCO over PCM */
-static int setup_stream(struct userdata *u) {
+static void setup_stream(struct userdata *u) {
struct pollfd *pollfd;
int one;
+ pa_log_info("Transport %s resuming", u->transport->path);
+
+ bt_transport_config_mtu(u);
+
pa_make_fd_nonblock(u->stream_fd);
pa_make_socket_low_delay(u->stream_fd);
u->read_smoother = pa_smoother_new(
PA_USEC_PER_SEC,
PA_USEC_PER_SEC*2,
- TRUE,
- TRUE,
+ true,
+ true,
10,
pa_rtclock_now(),
- TRUE);
-
- return 0;
-}
-
-static int start_stream_fd(struct userdata *u) {
- union {
- bt_audio_msg_header_t rsp;
- struct bt_start_stream_req start_req;
- struct bt_start_stream_rsp start_rsp;
- struct bt_new_stream_ind streamfd_ind;
- bt_audio_error_t error;
- uint8_t buf[BT_SUGGESTED_BUFFER_SIZE];
- } msg;
-
- pa_assert(u);
- pa_assert(u->rtpoll);
- pa_assert(!u->rtpoll_item);
- pa_assert(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_START_STREAM;
- msg.start_req.h.length = sizeof(msg.start_req);
-
- if (service_send(u, &msg.start_req.h) < 0)
- return -1;
-
- if (service_expect(u, &msg.rsp, sizeof(msg), BT_START_STREAM, sizeof(msg.start_rsp)) < 0)
- return -1;
-
- if (service_expect(u, &msg.rsp, sizeof(msg), BT_NEW_STREAM, sizeof(msg.streamfd_ind)) < 0)
- return -1;
-
- if ((u->stream_fd = bt_audio_service_get_data_fd(u->service_fd)) < 0) {
- pa_log("Failed to get stream fd from audio service.");
- return -1;
- }
-
- return setup_stream(u);
+ true);
}
-/* from IO thread */
-static int stop_stream_fd(struct userdata *u) {
- union {
- bt_audio_msg_header_t rsp;
- struct bt_stop_stream_req start_req;
- struct bt_stop_stream_rsp start_rsp;
- bt_audio_error_t error;
- uint8_t buf[BT_SUGGESTED_BUFFER_SIZE];
- } msg;
- int r = 0;
-
- pa_assert(u);
- pa_assert(u->rtpoll);
-
+static void teardown_stream(struct userdata *u) {
if (u->rtpoll_item) {
pa_rtpoll_item_free(u->rtpoll_item);
u->rtpoll_item = NULL;
}
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;
-
pa_close(u->stream_fd);
u->stream_fd = -1;
}
u->read_smoother = NULL;
}
- return r;
+ if (u->write_memchunk.memblock) {
+ pa_memblock_unref(u->write_memchunk.memblock);
+ pa_memchunk_reset(&u->write_memchunk);
+ }
+
+ pa_log_debug("Audio stream torn down");
}
static void bt_transport_release(struct userdata *u) {
- const char *accesstype = "rw";
- const pa_bluetooth_transport *t;
+ pa_assert(u->transport);
/* Ignore if already released */
- if (!u->accesstype)
+ if (!u->transport_acquired)
return;
- pa_log_debug("Releasing transport %s", u->transport);
+ pa_log_debug("Releasing transport %s", u->transport->path);
- t = pa_bluetooth_discovery_get_transport(u->discovery, u->transport);
- if (t)
- pa_bluetooth_transport_release(t, accesstype);
+ pa_bluetooth_transport_release(u->transport);
- pa_xfree(u->accesstype);
- u->accesstype = NULL;
+ u->transport_acquired = false;
- 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;
- }
+ teardown_stream(u);
}
-static int bt_transport_acquire(struct userdata *u, pa_bool_t start) {
- const char *accesstype = "rw";
- const pa_bluetooth_transport *t;
+static int bt_transport_acquire(struct userdata *u, bool optional) {
+ pa_assert(u->transport);
- if (u->accesstype) {
- if (start)
- goto done;
+ if (u->transport_acquired)
return 0;
- }
- pa_log_debug("Acquiring transport %s", u->transport);
+ pa_log_debug("Acquiring transport %s", u->transport->path);
- 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(u->transport, optional, &u->read_link_mtu, &u->write_link_mtu);
+ if (u->stream_fd < 0) {
+ if (!optional)
+ pa_log("Failed to acquire transport %s", u->transport->path);
+ else
+ pa_log_info("Failed optional acquire of transport %s", u->transport->path);
- /* FIXME: Handle in/out MTU properly when unix socket is not longer supported */
- u->stream_fd = pa_bluetooth_transport_acquire(t, accesstype, NULL, &u->link_mtu);
- 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;
+ u->transport_acquired = true;
+ pa_log_info("Transport %s acquired: fd %d", u->transport->path, u->stream_fd);
-done:
- pa_log_info("Transport %s resuming", u->transport);
- return setup_stream(u);
+ return 0;
}
/* 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_bool_t failed = FALSE;
+ bool failed = false;
int r;
pa_assert(u->sink == PA_SINK(o));
+ pa_assert(u->transport);
switch (code) {
switch ((pa_sink_state_t) PA_PTR_TO_UINT(data)) {
case PA_SINK_SUSPENDED:
- pa_assert(PA_SINK_IS_OPENED(u->sink->thread_info.state));
+ /* Ignore if transition is PA_SINK_INIT->PA_SINK_SUSPENDED */
+ if (!PA_SINK_IS_OPENED(u->sink->thread_info.state))
+ break;
/* 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 */
- if (u->transport)
- bt_transport_release(u);
- else
- stop_stream_fd(u);
- }
+ bt_transport_release(u);
break;
break;
/* Resume the device if the source was suspended as well */
- 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;
+ if (!u->source || !PA_SOURCE_IS_OPENED(u->source->thread_info.state)) {
+ if (bt_transport_acquire(u, false) < 0)
+ failed = true;
+ else
+ setup_stream(u);
}
break;
pa_usec_t wi, ri;
ri = pa_smoother_get(u->read_smoother, pa_rtclock_now());
- wi = pa_bytes_to_usec(u->write_index + u->block_size, &u->sample_spec);
+ wi = pa_bytes_to_usec(u->write_index + u->write_block_size, &u->sample_spec);
*((pa_usec_t*) data) = wi > ri ? wi - ri : 0;
} else {
/* Run from IO thread */
static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
struct userdata *u = PA_SOURCE(o)->userdata;
- pa_bool_t failed = FALSE;
+ bool failed = false;
int r;
pa_assert(u->source == PA_SOURCE(o));
+ pa_assert(u->transport);
switch (code) {
switch ((pa_source_state_t) PA_PTR_TO_UINT(data)) {
case PA_SOURCE_SUSPENDED:
- pa_assert(PA_SOURCE_IS_OPENED(u->source->thread_info.state));
+ /* Ignore if transition is PA_SOURCE_INIT->PA_SOURCE_SUSPENDED */
+ if (!PA_SOURCE_IS_OPENED(u->source->thread_info.state))
+ break;
/* Stop the device if the sink is suspended as well */
- if (!u->sink || u->sink->state == PA_SINK_SUSPENDED) {
- if (u->transport)
- bt_transport_release(u);
- else
- stop_stream_fd(u);
- }
+ if (!u->sink || u->sink->state == PA_SINK_SUSPENDED)
+ bt_transport_release(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 (u->transport) {
- if (bt_transport_acquire(u, TRUE) < 0)
- failed = TRUE;
- } else if (start_stream_fd(u) < 0)
- failed = TRUE;
+ if (!u->sink || !PA_SINK_IS_OPENED(u->sink->thread_info.state)) {
+ if (bt_transport_acquire(u, false) < 0)
+ failed = true;
+ else
+ setup_stream(u);
}
/* We don't resume the smoother here. Instead we
* wait until the first packet arrives */
struct bluetooth_msg *u = BLUETOOTH_MSG(obj);
switch (code) {
- case BLUETOOTH_MESSAGE_SET_PROFILE: {
- const char *profile = data;
- pa_log_debug("Switch profile to %s requested", profile);
+ case BLUETOOTH_MESSAGE_IO_THREAD_FAILED: {
+ if (u->card->module->unload_requested)
+ break;
+
+ pa_log_debug("Switching the profile to off due to IO thread failure.");
- if (pa_card_set_profile(u->card, profile, FALSE) < 0)
- pa_log_debug("Failed to switch profile to %s", profile);
+ pa_assert_se(pa_card_set_profile(u->card, "off", false) >= 0);
break;
}
}
/* First, render some data */
if (!u->write_memchunk.memblock)
- pa_sink_render_full(u->sink, u->block_size, &u->write_memchunk);
+ pa_sink_render_full(u->sink, u->write_block_size, &u->write_memchunk);
- pa_assert(u->write_memchunk.length == u->block_size);
+ pa_assert(u->write_memchunk.length == u->write_block_size);
for (;;) {
ssize_t l;
* SEQPACKET, and we generated the data of the MTU size, so this
* should just work. */
- p = (const uint8_t*) pa_memblock_acquire(u->write_memchunk.memblock) + u->write_memchunk.index;
+ p = (const uint8_t *) pa_memblock_acquire_chunk(&u->write_memchunk);
l = pa_write(u->stream_fd, p, u->write_memchunk.length, &u->stream_write_type);
pa_memblock_release(u->write_memchunk.memblock);
pa_assert(u->source);
pa_assert(u->read_smoother);
- memchunk.memblock = pa_memblock_new(u->core->mempool, u->block_size);
+ memchunk.memblock = pa_memblock_new(u->core->mempool, u->read_block_size);
memchunk.index = memchunk.length = 0;
for (;;) {
struct cmsghdr *cm;
uint8_t aux[1024];
struct iovec iov;
- pa_bool_t found_tstamp = FALSE;
+ bool found_tstamp = false;
pa_usec_t tstamp;
memset(&m, 0, sizeof(m));
pa_assert((size_t) l <= pa_memblock_get_length(memchunk.memblock));
+ /* In some rare occasions, we might receive packets of a very strange
+ * size. This could potentially be possible if the SCO packet was
+ * received partially over-the-air, or more probably due to hardware
+ * issues in our Bluetooth adapter. In these cases, in order to avoid
+ * an assertion failure due to unaligned data, just discard the whole
+ * packet */
+ if (!pa_frame_aligned(l, &u->sample_spec)) {
+ pa_log_warn("SCO packet received of unaligned size: %zu", l);
+ break;
+ }
+
memchunk.length = (size_t) l;
u->read_index += (uint64_t) l;
struct timeval *tv = (struct timeval*) CMSG_DATA(cm);
pa_rtclock_from_wallclock(tv);
tstamp = pa_timeval_load(tv);
- found_tstamp = TRUE;
+ found_tstamp = true;
break;
}
}
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);
+ pa_smoother_resume(u->read_smoother, tstamp, true);
pa_source_post(u->source, &memchunk);
- ret = 1;
+ ret = l;
break;
}
/* Run from IO thread */
static void a2dp_prepare_buffer(struct userdata *u) {
+ size_t min_buffer_size = PA_MAX(u->read_link_mtu, u->write_link_mtu);
+
pa_assert(u);
- if (u->a2dp.buffer_size >= u->link_mtu)
+ if (u->a2dp.buffer_size >= min_buffer_size)
return;
- u->a2dp.buffer_size = 2 * u->link_mtu;
+ u->a2dp.buffer_size = 2 * min_buffer_size;
pa_xfree(u->a2dp.buffer);
u->a2dp.buffer = pa_xmalloc(u->a2dp.buffer_size);
}
/* First, render some data */
if (!u->write_memchunk.memblock)
- pa_sink_render_full(u->sink, u->block_size, &u->write_memchunk);
+ pa_sink_render_full(u->sink, u->write_block_size, &u->write_memchunk);
- pa_assert(u->write_memchunk.length == u->block_size);
+ pa_assert(u->write_memchunk.length == u->write_block_size);
a2dp_prepare_buffer(u);
/* Try to create a packet of the full MTU */
- p = (const uint8_t*) pa_memblock_acquire(u->write_memchunk.memblock) + u->write_memchunk.index;
+ p = (const uint8_t *) pa_memblock_acquire_chunk(&u->write_memchunk);
to_encode = u->write_memchunk.length;
d = (uint8_t*) a2dp->buffer + sizeof(*header) + sizeof(*payload);
pa_assert(u->source);
pa_assert(u->read_smoother);
- memchunk.memblock = pa_memblock_new(u->core->mempool, u->block_size);
+ memchunk.memblock = pa_memblock_new(u->core->mempool, u->read_block_size);
memchunk.index = memchunk.length = 0;
for (;;) {
- pa_bool_t found_tstamp = FALSE;
+ bool found_tstamp = false;
pa_usec_t tstamp;
struct a2dp_info *a2dp;
struct rtp_header *header;
void *d;
ssize_t l;
size_t to_write, to_decode;
- unsigned frame_count;
a2dp_prepare_buffer(u);
}
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);
+ 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 = (uint8_t*) d + written;
to_write -= written;
-
- frame_count++;
}
memchunk.length -= to_write;
pa_source_post(u->source, &memchunk);
- ret = 1;
+ ret = l;
break;
}
static void thread_func(void *userdata) {
struct userdata *u = userdata;
unsigned do_write = 0;
- pa_bool_t writable = FALSE;
+ unsigned pending_read_bytes = 0;
+ bool writable = false;
pa_assert(u);
+ pa_assert(u->transport);
pa_log_debug("IO Thread starting up");
pa_thread_mq_install(&u->thread_mq);
- if (u->transport) {
- if (bt_transport_acquire(u, TRUE) < 0)
- goto fail;
- } else if (start_stream_fd(u) < 0)
- goto fail;
+ /* Setup the stream only if the transport was already acquired */
+ if (u->transport_acquired)
+ setup_stream(u);
for (;;) {
struct pollfd *pollfd;
int ret;
- pa_bool_t disable_timer = TRUE;
+ bool disable_timer = true;
pollfd = u->rtpoll_item ? pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL) : NULL;
n_read = a2dp_process_push(u);
if (n_read < 0)
- goto fail;
+ goto io_fail;
/* We just read something, so we are supposed to write something, too */
- do_write += n_read;
+ pending_read_bytes += n_read;
+ do_write += pending_read_bytes / u->write_block_size;
+ pending_read_bytes = pending_read_bytes % u->write_block_size;
}
}
if (u->sink && PA_SINK_IS_LINKED(u->sink->thread_info.state)) {
- if (u->sink->thread_info.rewind_requested)
+ if (PA_UNLIKELY(u->sink->thread_info.rewind_requested))
pa_sink_process_rewind(u->sink, 0);
if (pollfd) {
if (pollfd->revents & POLLOUT)
- writable = TRUE;
+ writable = true;
if ((!u->source || !PA_SOURCE_IS_LINKED(u->source->thread_info.state)) && do_write <= 0 && writable) {
pa_usec_t time_passed;
}
do_write = 1;
+ pending_read_bytes = 0;
}
}
if (u->profile == PROFILE_A2DP) {
if ((n_written = a2dp_process_render(u)) < 0)
- goto fail;
+ goto io_fail;
} else {
if ((n_written = hsp_process_render(u)) < 0)
- goto fail;
+ goto io_fail;
}
if (n_written == 0)
pa_log("Broken kernel: we got EAGAIN on write() after POLLOUT!");
do_write -= n_written;
- writable = FALSE;
+ writable = false;
}
if ((!u->source || !PA_SOURCE_IS_LINKED(u->source->thread_info.state)) && do_write <= 0) {
sleep_for = PA_USEC_PER_MSEC * 500;
pa_rtpoll_set_timer_relative(u->rtpoll, sleep_for);
- disable_timer = FALSE;
+ disable_timer = false;
}
}
}
pollfd->events = (short) (((u->sink && PA_SINK_IS_LINKED(u->sink->thread_info.state) && !writable) ? POLLOUT : 0) |
(u->source && PA_SOURCE_IS_LINKED(u->source->thread_info.state) ? POLLIN : 0));
- if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
+ if ((ret = pa_rtpoll_run(u->rtpoll, true)) < 0) {
+ pa_log_debug("pa_rtpoll_run failed with: %d", ret);
goto fail;
-
- if (ret == 0)
+ }
+ if (ret == 0) {
+ pa_log_debug("IO thread shutdown requested, stopping cleanly");
+ bt_transport_release(u);
goto finish;
+ }
pollfd = u->rtpoll_item ? pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL) : NULL;
pollfd->revents & POLLHUP ? "POLLHUP " :"",
pollfd->revents & POLLPRI ? "POLLPRI " :"",
pollfd->revents & POLLNVAL ? "POLLNVAL " :"");
- goto fail;
+ goto io_fail;
}
+
+ continue;
+
+io_fail:
+ /* In case of HUP, just tear down the streams */
+ if (!pollfd || (pollfd->revents & POLLHUP) == 0)
+ goto fail;
+
+ do_write = 0;
+ pending_read_bytes = 0;
+ writable = false;
+
+ teardown_stream(u);
}
fail:
/* If this was no regular exit from the loop we have to continue processing messages until we receive PA_MESSAGE_SHUTDOWN */
pa_log_debug("IO thread failed");
- pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(u->msg), BLUETOOTH_MESSAGE_SET_PROFILE, "off", 0, NULL, NULL);
+ pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(u->msg), BLUETOOTH_MESSAGE_IO_THREAD_FAILED, NULL, 0, NULL, NULL);
pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
finish:
pa_log_debug("IO thread shutting down");
}
-/* Run from main thread */
-static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *m, void *userdata) {
- DBusError err;
- struct userdata *u;
-
- pa_assert(bus);
- pa_assert(m);
- pa_assert_se(u = userdata);
-
- dbus_error_init(&err);
+static pa_available_t transport_state_to_availability(pa_bluetooth_transport_state_t state) {
+ if (state == PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED)
+ return PA_AVAILABLE_NO;
+ else if (state >= PA_BLUETOOTH_TRANSPORT_STATE_PLAYING)
+ return PA_AVAILABLE_YES;
+ else
+ return PA_AVAILABLE_UNKNOWN;
+}
- pa_log_debug("dbus: interface=%s, path=%s, member=%s\n",
- dbus_message_get_interface(m),
- dbus_message_get_path(m),
- dbus_message_get_member(m));
+static pa_direction_t get_profile_direction(enum profile p) {
+ static const pa_direction_t profile_direction[] = {
+ [PROFILE_A2DP] = PA_DIRECTION_OUTPUT,
+ [PROFILE_A2DP_SOURCE] = PA_DIRECTION_INPUT,
+ [PROFILE_HSP] = PA_DIRECTION_INPUT | PA_DIRECTION_OUTPUT,
+ [PROFILE_HFGW] = PA_DIRECTION_INPUT | PA_DIRECTION_OUTPUT,
+ [PROFILE_OFF] = 0
+ };
- if (!dbus_message_has_path(m, u->path) && !dbus_message_has_path(m, u->transport))
- goto fail;
+ return profile_direction[p];
+}
- if (dbus_message_is_signal(m, "org.bluez.Headset", "SpeakerGainChanged") ||
- dbus_message_is_signal(m, "org.bluez.Headset", "MicrophoneGainChanged")) {
+/* Run from main thread */
+static pa_available_t get_port_availability(struct userdata *u, pa_direction_t direction) {
+ pa_available_t result = PA_AVAILABLE_NO;
+ unsigned i;
- dbus_uint16_t gain;
- pa_cvolume v;
+ pa_assert(u);
+ pa_assert(u->device);
- if (!dbus_message_get_args(m, &err, DBUS_TYPE_UINT16, &gain, DBUS_TYPE_INVALID) || gain > HSP_MAX_GAIN) {
- pa_log("Failed to parse org.bluez.Headset.{Speaker|Microphone}GainChanged: %s", err.message);
- goto fail;
- }
+ for (i = 0; i < PA_BLUETOOTH_PROFILE_COUNT; i++) {
+ pa_bluetooth_transport *transport;
- if (u->profile == PROFILE_HSP) {
- if (u->sink && dbus_message_is_signal(m, "org.bluez.Headset", "SpeakerGainChanged")) {
- pa_volume_t volume = (pa_volume_t) (gain * PA_VOLUME_NORM / HSP_MAX_GAIN);
+ if (!(get_profile_direction(i) & direction))
+ continue;
- /* increment volume by one to correct rounding errors */
- if (volume < PA_VOLUME_NORM)
- volume++;
+ if (!(transport = u->device->transports[i]))
+ continue;
- pa_cvolume_set(&v, u->sample_spec.channels, volume);
- pa_sink_volume_changed(u->sink, &v);
+ switch(transport->state) {
+ case PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED:
+ continue;
- } else if (u->source && dbus_message_is_signal(m, "org.bluez.Headset", "MicrophoneGainChanged")) {
- pa_volume_t volume = (pa_volume_t) (gain * PA_VOLUME_NORM / HSP_MAX_GAIN);
+ case PA_BLUETOOTH_TRANSPORT_STATE_IDLE:
+ if (result == PA_AVAILABLE_NO)
+ result = PA_AVAILABLE_UNKNOWN;
- /* increment volume by one to correct rounding errors */
- if (volume < PA_VOLUME_NORM)
- volume++;
+ break;
- pa_cvolume_set(&v, u->sample_spec.channels, volume);
- pa_source_volume_changed(u->source, &v);
- }
+ case PA_BLUETOOTH_TRANSPORT_STATE_PLAYING:
+ return PA_AVAILABLE_YES;
}
- } else if (dbus_message_is_signal(m, "org.bluez.MediaTransport", "PropertyChanged")) {
- DBusMessageIter arg_i;
- pa_bluetooth_transport *t;
- pa_bool_t nrec;
+ }
- t = (pa_bluetooth_transport *) pa_bluetooth_discovery_get_transport(u->discovery, u->transport);
- pa_assert(t);
+ return result;
+}
- if (!dbus_message_iter_init(m, &arg_i)) {
- pa_log("Failed to parse PropertyChanged: %s", err.message);
- goto fail;
- }
+/* Run from main thread */
+static void handle_transport_state_change(struct userdata *u, struct pa_bluetooth_transport *transport) {
+ bool acquire = false;
+ bool release = false;
+ enum profile profile;
+ pa_card_profile *cp;
+ pa_bluetooth_transport_state_t state;
+ pa_device_port *port;
- nrec = t->nrec;
+ pa_assert(u);
+ pa_assert(transport);
- if (pa_bluetooth_transport_parse_property(t, &arg_i) < 0)
- goto fail;
+ profile = transport->profile;
+ state = transport->state;
- if (nrec != t->nrec) {
- pa_log_debug("dbus: property 'NREC' changed to value '%s'", t->nrec ? "True" : "False");
- pa_proplist_sets(u->source->proplist, "bluetooth.nrec", t->nrec ? "1" : "0");
- }
- } else if (dbus_message_is_signal(m, "org.bluez.HandsfreeGateway", "PropertyChanged")) {
- const char *key;
- DBusMessageIter iter;
- DBusMessageIter variant;
- pa_bt_audio_state_t state = PA_BT_AUDIO_STATE_INVALID;
-
- if (!dbus_message_iter_init(m, &iter)) {
- pa_log("Failed to parse PropertyChanged: %s", err.message);
- goto fail;
- }
+ /* Update profile availability */
+ if (!(cp = pa_hashmap_get(u->card->profiles, pa_bt_profile_to_string(profile))))
+ return;
- if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) {
- pa_log("Property name not a string.");
- goto fail;
- }
+ pa_card_profile_set_available(cp, transport_state_to_availability(state));
- dbus_message_iter_get_basic(&iter, &key);
+ /* Update port availability */
+ pa_assert_se(port = pa_hashmap_get(u->card->ports, u->output_port_name));
+ pa_device_port_set_available(port, get_port_availability(u, PA_DIRECTION_OUTPUT));
- if (!dbus_message_iter_next(&iter)) {
- pa_log("Property value missing");
- goto fail;
- }
+ pa_assert_se(port = pa_hashmap_get(u->card->ports, u->input_port_name));
+ pa_device_port_set_available(port, get_port_availability(u, PA_DIRECTION_INPUT));
- dbus_message_iter_recurse(&iter, &variant);
+ /* Acquire or release transport as needed */
+ acquire = (state == PA_BLUETOOTH_TRANSPORT_STATE_PLAYING && u->profile == profile);
+ release = (state != PA_BLUETOOTH_TRANSPORT_STATE_PLAYING && u->profile == profile);
- if (dbus_message_iter_get_arg_type(&variant) == DBUS_TYPE_STRING) {
- const char *value;
- dbus_message_iter_get_basic(&variant, &value);
+ if (acquire)
+ if (bt_transport_acquire(u, true) >= 0) {
+ if (u->source) {
+ pa_log_debug("Resuming source %s, because the bluetooth audio state changed to 'playing'.", u->source->name);
+ pa_source_suspend(u->source, false, PA_SUSPEND_IDLE|PA_SUSPEND_USER);
+ }
- if (pa_streq(key, "State")) {
- pa_log_debug("dbus: HSHFAG property 'State' changed to value '%s'", value);
- state = pa_bt_audio_state_from_string(value);
+ if (u->sink) {
+ pa_log_debug("Resuming sink %s, because the bluetooth audio state changed to 'playing'.", u->sink->name);
+ pa_sink_suspend(u->sink, false, PA_SUSPEND_IDLE|PA_SUSPEND_USER);
}
}
- switch(state) {
- case PA_BT_AUDIO_STATE_INVALID:
- case PA_BT_AUDIO_STATE_DISCONNECTED:
- case PA_BT_AUDIO_STATE_CONNECTED:
- case PA_BT_AUDIO_STATE_CONNECTING:
- goto fail;
-
- case PA_BT_AUDIO_STATE_PLAYING:
- if (u->card) {
- pa_log_debug("Changing profile to hfgw");
- if (pa_card_set_profile(u->card, "hfgw", FALSE) < 0)
- pa_log("Failed to change profile to hfgw");
- }
- break;
- }
- }
+ if (release && u->transport_acquired) {
+ /* FIXME: this release is racy, since the audio stream might have
+ been set up again in the meantime (but not processed yet by PA).
+ BlueZ should probably release the transport automatically, and
+ in that case we would just mark the transport as released */
-fail:
- dbus_error_free(&err);
+ /* Remote side closed the stream so we consider it PA_SUSPEND_USER */
+ if (u->source) {
+ pa_log_debug("Suspending source %s, because the remote end closed the stream.", u->source->name);
+ pa_source_suspend(u->source, true, PA_SUSPEND_USER);
+ }
- return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+ if (u->sink) {
+ pa_log_debug("Suspending sink %s, because the remote end closed the stream.", u->sink->name);
+ pa_sink_suspend(u->sink, true, PA_SUSPEND_USER);
+ }
+ }
}
/* Run from main thread */
static void sink_set_volume_cb(pa_sink *s) {
- DBusMessage *m;
- dbus_uint16_t gain;
+ uint16_t gain;
pa_volume_t volume;
struct userdata *u;
char *k;
pa_assert(u);
pa_assert(u->sink == s);
pa_assert(u->profile == PROFILE_HSP);
+ pa_assert(u->transport);
- gain = (pa_cvolume_max(&s->real_volume) * HSP_MAX_GAIN) / PA_VOLUME_NORM;
-
- if (gain > HSP_MAX_GAIN)
- gain = HSP_MAX_GAIN;
-
- volume = (pa_volume_t) (gain * PA_VOLUME_NORM / HSP_MAX_GAIN);
-
- /* increment volume by one to correct rounding errors */
- if (volume < PA_VOLUME_NORM)
- volume++;
+ gain = (dbus_uint16_t) round((double) pa_cvolume_max(&s->real_volume) * HSP_MAX_GAIN / PA_VOLUME_NORM);
+ volume = (pa_volume_t) round((double) gain * PA_VOLUME_NORM / HSP_MAX_GAIN);
pa_cvolume_set(&s->real_volume, u->sample_spec.channels, volume);
- pa_assert_se(m = dbus_message_new_method_call("org.bluez", u->path, "org.bluez.Headset", "SetSpeakerGain"));
- pa_assert_se(dbus_message_append_args(m, DBUS_TYPE_UINT16, &gain, DBUS_TYPE_INVALID));
- pa_assert_se(dbus_connection_send(pa_dbus_connection_get(u->connection), m, NULL));
- dbus_message_unref(m);
+ pa_bluetooth_transport_set_speaker_gain(u->transport, gain);
}
/* Run from main thread */
static void source_set_volume_cb(pa_source *s) {
- DBusMessage *m;
- dbus_uint16_t gain;
+ uint16_t gain;
pa_volume_t volume;
struct userdata *u;
char *k;
pa_assert(u);
pa_assert(u->source == s);
pa_assert(u->profile == PROFILE_HSP);
+ pa_assert(u->transport);
- gain = (pa_cvolume_max(&s->real_volume) * HSP_MAX_GAIN) / PA_VOLUME_NORM;
-
- if (gain > HSP_MAX_GAIN)
- gain = HSP_MAX_GAIN;
-
- volume = (pa_volume_t) (gain * PA_VOLUME_NORM / HSP_MAX_GAIN);
-
- /* increment volume by one to correct rounding errors */
- if (volume < PA_VOLUME_NORM)
- volume++;
+ gain = (dbus_uint16_t) round((double) pa_cvolume_max(&s->real_volume) * HSP_MAX_GAIN / PA_VOLUME_NORM);
+ volume = (pa_volume_t) round((double) gain * PA_VOLUME_NORM / HSP_MAX_GAIN);
pa_cvolume_set(&s->real_volume, u->sample_spec.channels, volume);
- pa_assert_se(m = dbus_message_new_method_call("org.bluez", u->path, "org.bluez.Headset", "SetMicrophoneGain"));
- pa_assert_se(dbus_message_append_args(m, DBUS_TYPE_UINT16, &gain, DBUS_TYPE_INVALID));
- pa_assert_se(dbus_connection_send(pa_dbus_connection_get(u->connection), m, NULL));
- dbus_message_unref(m);
+ pa_bluetooth_transport_set_microphone_gain(u->transport, gain);
}
/* Run from main thread */
-static char *get_name(const char *type, pa_modargs *ma, const char *device_id, pa_bool_t *namereg_fail) {
+static char *get_name(const char *type, pa_modargs *ma, const char *device_id, bool *namereg_fail) {
char *t;
const char *n;
pa_xfree(t);
if (n) {
- *namereg_fail = TRUE;
+ *namereg_fail = true;
return pa_xstrdup(n);
}
if ((n = pa_modargs_get_value(ma, "name", NULL)))
- *namereg_fail = TRUE;
+ *namereg_fail = true;
else {
n = device_id;
- *namereg_fail = FALSE;
+ *namereg_fail = false;
}
return pa_sprintf_malloc("bluez_%s.%s", type, n);
}
-static int sco_over_pcm_state_update(struct userdata *u, pa_bool_t changed) {
+static int sco_over_pcm_state_update(struct userdata *u, bool changed) {
pa_assert(u);
pa_assert(USE_SCO_OVER_PCM(u));
if (PA_SINK_IS_OPENED(pa_sink_get_state(u->hsp.sco_sink)) ||
PA_SOURCE_IS_OPENED(pa_source_get_state(u->hsp.sco_source))) {
- if (u->service_fd >= 0 && u->stream_fd >= 0)
+ if (u->stream_fd >= 0)
return 0;
- init_bt(u);
-
pa_log_debug("Resuming SCO over PCM");
if (init_profile(u) < 0) {
pa_log("Can't resume SCO over PCM");
return -1;
}
- if (u->transport)
- return bt_transport_acquire(u, TRUE);
+ if (bt_transport_acquire(u, false) < 0)
+ return -1;
+
+ setup_stream(u);
- return start_stream_fd(u);
+ return 0;
}
if (changed) {
- if (u->service_fd < 0 && u->stream_fd < 0)
+ if (u->stream_fd < 0)
return 0;
pa_log_debug("Closing SCO over PCM");
- if (u->transport)
- bt_transport_release(u);
- else if (u->stream_fd >= 0)
- stop_stream_fd(u);
-
- if (u->service_fd >= 0) {
- pa_close(u->service_fd);
- u->service_fd = -1;
- }
+ bt_transport_release(u);
}
return 0;
pa_sink_assert_ref(s);
pa_assert(u);
- if (s != u->hsp.sco_sink)
+ if (!USE_SCO_OVER_PCM(u) || s != u->hsp.sco_sink)
return PA_HOOK_OK;
- sco_over_pcm_state_update(u, TRUE);
+ sco_over_pcm_state_update(u, true);
return PA_HOOK_OK;
}
pa_source_assert_ref(s);
pa_assert(u);
- if (s != u->hsp.sco_source)
+ if (!USE_SCO_OVER_PCM(u) || s != u->hsp.sco_source)
+ return PA_HOOK_OK;
+
+ sco_over_pcm_state_update(u, true);
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t transport_nrec_changed_cb(pa_bluetooth_discovery *y, pa_bluetooth_transport *t, struct userdata *u) {
+ pa_proplist *p;
+
+ pa_assert(t);
+ pa_assert(u);
+
+ if (t != u->transport)
+ return PA_HOOK_OK;
+
+ p = pa_proplist_new();
+ pa_proplist_sets(p, "bluetooth.nrec", t->nrec ? "1" : "0");
+ pa_source_update_proplist(u->source, PA_UPDATE_REPLACE, p);
+ pa_proplist_free(p);
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t transport_microphone_gain_changed_cb(pa_bluetooth_discovery *y, pa_bluetooth_transport *t,
+ struct userdata *u) {
+ pa_cvolume v;
+
+ pa_assert(t);
+ pa_assert(u);
+
+ if (t != u->transport)
+ return PA_HOOK_OK;
+
+ pa_assert(u->source);
+
+ pa_cvolume_set(&v, u->sample_spec.channels,
+ (pa_volume_t) round((double) t->microphone_gain * PA_VOLUME_NORM / HSP_MAX_GAIN));
+ pa_source_volume_changed(u->source, &v);
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t transport_speaker_gain_changed_cb(pa_bluetooth_discovery *y, pa_bluetooth_transport *t,
+ struct userdata *u) {
+ pa_cvolume v;
+
+ pa_assert(t);
+ pa_assert(u);
+
+ if (t != u->transport)
return PA_HOOK_OK;
- sco_over_pcm_state_update(u, TRUE);
+ pa_assert(u->sink);
+
+ pa_cvolume_set(&v, u->sample_spec.channels, (pa_volume_t) round((double) t->speaker_gain * PA_VOLUME_NORM / HSP_MAX_GAIN));
+ pa_sink_volume_changed(u->sink, &v);
return PA_HOOK_OK;
}
+static void connect_ports(struct userdata *u, void *sink_or_source_new_data, pa_direction_t direction) {
+ pa_device_port *port;
+
+ if (direction == PA_DIRECTION_OUTPUT) {
+ pa_sink_new_data *sink_new_data = sink_or_source_new_data;
+
+ pa_assert_se(port = pa_hashmap_get(u->card->ports, u->output_port_name));
+ pa_assert_se(pa_hashmap_put(sink_new_data->ports, port->name, port) >= 0);
+ pa_device_port_ref(port);
+ } else {
+ pa_source_new_data *source_new_data = sink_or_source_new_data;
+
+ pa_assert_se(port = pa_hashmap_get(u->card->ports, u->input_port_name));
+ pa_assert_se(pa_hashmap_put(source_new_data->ports, port->name, port) >= 0);
+ pa_device_port_ref(port);
+ }
+}
+
+static int sink_set_port_cb(pa_sink *s, pa_device_port *p) {
+ return 0;
+}
+
+static int source_set_port_cb(pa_source *s, pa_device_port *p) {
+ return 0;
+}
+
/* Run from main thread */
static int add_sink(struct userdata *u) {
char *k;
+ pa_assert(u->transport);
+
if (USE_SCO_OVER_PCM(u)) {
pa_proplist *p;
u->sink = u->hsp.sco_sink;
p = pa_proplist_new();
- pa_proplist_sets(p, "bluetooth.protocol", "sco");
+ pa_proplist_sets(p, "bluetooth.protocol", pa_bt_profile_to_string(u->profile));
pa_proplist_update(u->sink->proplist, PA_UPDATE_MERGE, p);
pa_proplist_free(p);
-
- if (!u->hsp.sink_state_changed_slot)
- u->hsp.sink_state_changed_slot = pa_hook_connect(&u->core->hooks[PA_CORE_HOOK_SINK_STATE_CHANGED], PA_HOOK_NORMAL, (pa_hook_cb_t) sink_state_changed_cb, u);
-
} else {
pa_sink_new_data data;
- pa_bool_t b;
+ bool b;
pa_sink_new_data_init(&data);
data.driver = __FILE__;
data.module = u->module;
pa_sink_new_data_set_sample_spec(&data, &u->sample_spec);
- pa_proplist_sets(data.proplist, "bluetooth.protocol", u->profile == PROFILE_A2DP ? "a2dp" : "sco");
+ pa_proplist_sets(data.proplist, "bluetooth.protocol", pa_bt_profile_to_string(u->profile));
if (u->profile == PROFILE_HSP)
pa_proplist_sets(data.proplist, PA_PROP_DEVICE_INTENDED_ROLES, "phone");
data.card = u->card;
pa_sink_new_data_done(&data);
return -1;
}
+ connect_ports(u, &data, PA_DIRECTION_OUTPUT);
+
+ if (!u->transport_acquired)
+ switch (u->profile) {
+ case PROFILE_A2DP:
+ case PROFILE_HSP:
+ pa_assert_not_reached(); /* Profile switch should have failed */
+ break;
+ case PROFILE_HFGW:
+ data.suspend_cause = PA_SUSPEND_USER;
+ break;
+ case PROFILE_A2DP_SOURCE:
+ case PROFILE_OFF:
+ pa_assert_not_reached();
+ }
u->sink = pa_sink_new(u->core, &data, PA_SINK_HARDWARE|PA_SINK_LATENCY);
pa_sink_new_data_done(&data);
u->sink->userdata = u;
u->sink->parent.process_msg = sink_process_msg;
-
- pa_sink_set_max_request(u->sink, u->block_size);
- pa_sink_set_fixed_latency(u->sink,
- (u->profile == PROFILE_A2DP ? FIXED_LATENCY_PLAYBACK_A2DP : FIXED_LATENCY_PLAYBACK_HSP) +
- pa_bytes_to_usec(u->block_size, &u->sample_spec));
+ u->sink->set_port = sink_set_port_cb;
}
if (u->profile == PROFILE_HSP) {
static int add_source(struct userdata *u) {
char *k;
+ pa_assert(u->transport);
+
if (USE_SCO_OVER_PCM(u)) {
u->source = u->hsp.sco_source;
- pa_proplist_sets(u->source->proplist, "bluetooth.protocol", "hsp");
-
- if (!u->hsp.source_state_changed_slot)
- u->hsp.source_state_changed_slot = pa_hook_connect(&u->core->hooks[PA_CORE_HOOK_SOURCE_STATE_CHANGED], PA_HOOK_NORMAL, (pa_hook_cb_t) source_state_changed_cb, u);
-
+ pa_proplist_sets(u->source->proplist, "bluetooth.protocol", pa_bt_profile_to_string(u->profile));
} else {
pa_source_new_data data;
- pa_bool_t b;
+ bool b;
pa_source_new_data_init(&data);
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_SOURCE ? "a2dp_source" : "hsp");
- if ((u->profile == PROFILE_HSP) || (u->profile == PROFILE_HFGW))
+ pa_proplist_sets(data.proplist, "bluetooth.protocol", pa_bt_profile_to_string(u->profile));
+ if (u->profile == PROFILE_HSP)
pa_proplist_sets(data.proplist, PA_PROP_DEVICE_INTENDED_ROLES, "phone");
data.card = u->card;
return -1;
}
+ connect_ports(u, &data, PA_DIRECTION_INPUT);
+
+ if (!u->transport_acquired)
+ switch (u->profile) {
+ case PROFILE_HSP:
+ pa_assert_not_reached(); /* Profile switch should have failed */
+ break;
+ case PROFILE_A2DP_SOURCE:
+ case PROFILE_HFGW:
+ data.suspend_cause = PA_SUSPEND_USER;
+ break;
+ case PROFILE_A2DP:
+ case PROFILE_OFF:
+ pa_assert_not_reached();
+ }
+
u->source = pa_source_new(u->core, &data, PA_SOURCE_HARDWARE|PA_SOURCE_LATENCY);
pa_source_new_data_done(&data);
u->source->userdata = u;
u->source->parent.process_msg = source_process_msg;
-
- pa_source_set_fixed_latency(u->source,
- (u->profile == PROFILE_A2DP_SOURCE ? FIXED_LATENCY_RECORD_A2DP : FIXED_LATENCY_RECORD_HSP) +
- pa_bytes_to_usec(u->block_size, &u->sample_spec));
+ u->source->set_port = source_set_port_cb;
}
if ((u->profile == PROFILE_HSP) || (u->profile == PROFILE_HFGW)) {
- if (u->transport) {
- const pa_bluetooth_transport *t;
- t = pa_bluetooth_discovery_get_transport(u->discovery, u->transport);
- pa_assert(t);
- pa_proplist_sets(u->source->proplist, "bluetooth.nrec", t->nrec ? "1" : "0");
- } else
- pa_proplist_sets(u->source->proplist, "bluetooth.nrec", (u->hsp.pcm_capabilities.flags & BT_PCM_FLAG_NREC) ? "1" : "0");
+ pa_bluetooth_transport *t = u->transport;
+ pa_proplist_sets(u->source->proplist, "bluetooth.nrec", t->nrec ? "1" : "0");
}
if (u->profile == PROFILE_HSP) {
return 0;
}
-/* Run from main thread */
-static void shutdown_bt(struct userdata *u) {
- pa_assert(u);
-
- if (u->stream_fd >= 0) {
- pa_close(u->stream_fd);
- u->stream_fd = -1;
-
- u->stream_write_type = 0;
- }
-
- if (u->service_fd >= 0) {
- pa_close(u->service_fd);
- u->service_fd = -1;
- u->service_write_type = 0;
- u->service_read_type = 0;
- }
-
- if (u->write_memchunk.memblock) {
- pa_memblock_unref(u->write_memchunk.memblock);
- pa_memchunk_reset(&u->write_memchunk);
- }
-}
-
-static int bt_transport_config_a2dp(struct userdata *u) {
+static void bt_transport_config_a2dp(struct userdata *u) {
const pa_bluetooth_transport *t;
struct a2dp_info *a2dp = &u->a2dp;
a2dp_sbc_t *config;
- t = pa_bluetooth_discovery_get_transport(u->discovery, u->transport);
+ t = u->transport;
pa_assert(t);
config = (a2dp_sbc_t *) t->config;
sbc_reinit(&a2dp->sbc, 0);
else
sbc_init(&a2dp->sbc, 0);
- a2dp->sbc_initialized = TRUE;
+ a2dp->sbc_initialized = true;
switch (config->frequency) {
- case BT_SBC_SAMPLING_FREQ_16000:
+ case SBC_SAMPLING_FREQ_16000:
a2dp->sbc.frequency = SBC_FREQ_16000;
u->sample_spec.rate = 16000U;
break;
- case BT_SBC_SAMPLING_FREQ_32000:
+ case SBC_SAMPLING_FREQ_32000:
a2dp->sbc.frequency = SBC_FREQ_32000;
u->sample_spec.rate = 32000U;
break;
- case BT_SBC_SAMPLING_FREQ_44100:
+ case SBC_SAMPLING_FREQ_44100:
a2dp->sbc.frequency = SBC_FREQ_44100;
u->sample_spec.rate = 44100U;
break;
- case BT_SBC_SAMPLING_FREQ_48000:
+ case SBC_SAMPLING_FREQ_48000:
a2dp->sbc.frequency = SBC_FREQ_48000;
u->sample_spec.rate = 48000U;
break;
}
switch (config->channel_mode) {
- case BT_A2DP_CHANNEL_MODE_MONO:
+ case SBC_CHANNEL_MODE_MONO:
a2dp->sbc.mode = SBC_MODE_MONO;
u->sample_spec.channels = 1;
break;
- case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL:
+ case SBC_CHANNEL_MODE_DUAL_CHANNEL:
a2dp->sbc.mode = SBC_MODE_DUAL_CHANNEL;
u->sample_spec.channels = 2;
break;
- case BT_A2DP_CHANNEL_MODE_STEREO:
+ case SBC_CHANNEL_MODE_STEREO:
a2dp->sbc.mode = SBC_MODE_STEREO;
u->sample_spec.channels = 2;
break;
- case BT_A2DP_CHANNEL_MODE_JOINT_STEREO:
+ case SBC_CHANNEL_MODE_JOINT_STEREO:
a2dp->sbc.mode = SBC_MODE_JOINT_STEREO;
u->sample_spec.channels = 2;
break;
}
switch (config->allocation_method) {
- case BT_A2DP_ALLOCATION_SNR:
+ case SBC_ALLOCATION_SNR:
a2dp->sbc.allocation = SBC_AM_SNR;
break;
- case BT_A2DP_ALLOCATION_LOUDNESS:
+ case SBC_ALLOCATION_LOUDNESS:
a2dp->sbc.allocation = SBC_AM_LOUDNESS;
break;
default:
}
switch (config->subbands) {
- case BT_A2DP_SUBBANDS_4:
+ case SBC_SUBBANDS_4:
a2dp->sbc.subbands = SBC_SB_4;
break;
- case BT_A2DP_SUBBANDS_8:
+ case SBC_SUBBANDS_8:
a2dp->sbc.subbands = SBC_SB_8;
break;
default:
}
switch (config->block_length) {
- case BT_A2DP_BLOCK_LENGTH_4:
+ case SBC_BLOCK_LENGTH_4:
a2dp->sbc.blocks = SBC_BLK_4;
break;
- case BT_A2DP_BLOCK_LENGTH_8:
+ case SBC_BLOCK_LENGTH_8:
a2dp->sbc.blocks = SBC_BLK_8;
break;
- case BT_A2DP_BLOCK_LENGTH_12:
+ case SBC_BLOCK_LENGTH_12:
a2dp->sbc.blocks = SBC_BLK_12;
break;
- case BT_A2DP_BLOCK_LENGTH_16:
+ case SBC_BLOCK_LENGTH_16:
a2dp->sbc.blocks = SBC_BLK_16;
break;
default:
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) {
+static void bt_transport_config(struct userdata *u) {
if (u->profile == PROFILE_HSP || u->profile == PROFILE_HFGW) {
- u->block_size = u->link_mtu;
u->sample_spec.format = PA_SAMPLE_S16LE;
u->sample_spec.channels = 1;
u->sample_spec.rate = 8000;
- return 0;
- }
-
- return bt_transport_config_a2dp(u);
-}
-
-/* Run from main thread */
-static int bt_transport_open(struct userdata *u) {
- if (bt_transport_acquire(u, FALSE) < 0)
- return -1;
-
- return bt_transport_config(u);
+ } else
+ bt_transport_config_a2dp(u);
}
/* Run from main thread */
-static int init_bt(struct userdata *u) {
+static pa_hook_result_t transport_state_changed_cb(pa_bluetooth_discovery *y, pa_bluetooth_transport *t, struct userdata *u) {
+ pa_assert(t);
pa_assert(u);
- shutdown_bt(u);
-
- u->stream_write_type = 0;
- u->service_write_type = 0;
- u->service_read_type = 0;
-
- if ((u->service_fd = bt_audio_service_open()) < 0) {
- pa_log_warn("Bluetooth audio service not available");
- return -1;
- }
+ if (t == u->transport && t->state == PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED)
+ pa_assert_se(pa_card_set_profile(u->card, "off", false) >= 0);
- pa_log_debug("Connected to the bluetooth audio service");
+ if (t->device == u->device)
+ handle_transport_state_change(u, t);
- return 0;
+ return PA_HOOK_OK;
}
/* Run from main thread */
-static int setup_bt(struct userdata *u) {
- const pa_bluetooth_device *d;
- const pa_bluetooth_transport *t;
+static int setup_transport(struct userdata *u) {
+ 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;
- }
+ pa_assert(!u->transport);
+ pa_assert(u->profile != PROFILE_OFF);
/* 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;
-
- pa_log_debug("Got device capabilities");
-
- if (set_conf(u) < 0)
+ t = u->device->transports[u->profile];
+ if (!t || t->state == PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED) {
+ pa_log_warn("Profile has no transport");
return -1;
+ }
- pa_log_debug("Connection to the device configured");
+ u->transport = t;
- if (USE_SCO_OVER_PCM(u)) {
- pa_log_debug("Configured to use SCO over PCM");
- return 0;
- }
+ if (u->profile == PROFILE_A2DP_SOURCE || u->profile == PROFILE_HFGW)
+ bt_transport_acquire(u, true); /* In case of error, the sink/sources will be created suspended */
+ else if (bt_transport_acquire(u, false) < 0)
+ return -1; /* We need to fail here until the interactions with module-suspend-on-idle and alike get improved */
- pa_log_debug("Got the stream socket");
+ bt_transport_config(u);
return 0;
}
pa_assert(u);
pa_assert(u->profile != PROFILE_OFF);
- if (setup_bt(u) < 0)
+ if (setup_transport(u) < 0)
return -1;
+ pa_assert(u->transport);
+
if (u->profile == PROFILE_A2DP ||
u->profile == PROFILE_HSP ||
u->profile == PROFILE_HFGW)
pa_assert(u);
+ if (u->sink && !USE_SCO_OVER_PCM(u))
+ pa_sink_unlink(u->sink);
+
+ if (u->source && !USE_SCO_OVER_PCM(u))
+ pa_source_unlink(u->source);
+
if (u->thread) {
pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
pa_thread_free(u->thread);
u->rtpoll_item = NULL;
}
- if (u->hsp.sink_state_changed_slot) {
- pa_hook_slot_free(u->hsp.sink_state_changed_slot);
- u->hsp.sink_state_changed_slot = NULL;
+ if (u->rtpoll) {
+ pa_thread_mq_done(&u->thread_mq);
+
+ pa_rtpoll_free(u->rtpoll);
+ u->rtpoll = NULL;
}
- if (u->hsp.source_state_changed_slot) {
- pa_hook_slot_free(u->hsp.source_state_changed_slot);
- u->hsp.source_state_changed_slot = NULL;
+ if (u->transport) {
+ bt_transport_release(u);
+ u->transport = NULL;
}
if (u->sink) {
u->source = NULL;
}
- if (u->rtpoll) {
- pa_thread_mq_done(&u->thread_mq);
-
- pa_rtpoll_free(u->rtpoll);
- u->rtpoll = NULL;
- }
-
if (u->read_smoother) {
pa_smoother_free(u->read_smoother);
u->read_smoother = NULL;
pa_thread_mq_init(&u->thread_mq, u->core->mainloop, u->rtpoll);
if (USE_SCO_OVER_PCM(u)) {
- if (sco_over_pcm_state_update(u, FALSE) < 0) {
+ if (sco_over_pcm_state_update(u, false) < 0) {
char *k;
if (u->sink) {
if (!(u->thread = pa_thread_new("bluetooth", thread_func, u))) {
pa_log_error("Failed to create IO thread");
- stop_thread(u);
return -1;
}
static int card_set_profile(pa_card *c, pa_card_profile *new_profile) {
struct userdata *u;
enum profile *d;
- pa_queue *inputs = NULL, *outputs = NULL;
- const pa_bluetooth_device *device;
pa_assert(c);
pa_assert(new_profile);
d = PA_CARD_PROFILE_DATA(new_profile);
- if (!(device = pa_bluetooth_discovery_get_by_path(u->discovery, u->path))) {
- pa_log_error("Failed to get device object.");
- return -PA_ERR_IO;
- }
-
- /* The state signal is sent by bluez, so it is racy to check
- strictly for CONNECTED, we should also accept STREAMING state
- as being good enough. However, if the profile is used
- concurrently (which is unlikely), ipc will fail later on, and
- module will be unloaded. */
- if (device->headset_state < PA_BT_AUDIO_STATE_CONNECTED && *d == PROFILE_HSP) {
- pa_log_warn("HSP is not connected, refused to switch profile");
- return -PA_ERR_IO;
- }
- else if (device->audio_sink_state < PA_BT_AUDIO_STATE_CONNECTED && *d == PROFILE_A2DP) {
- 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);
-
- if (!USE_SCO_OVER_PCM(u))
- pa_sink_unlink(u->sink);
- }
-
- if (u->source) {
- outputs = pa_source_move_all_start(u->source, NULL);
+ if (*d != PROFILE_OFF) {
+ const pa_bluetooth_device *device = u->device;
- if (!USE_SCO_OVER_PCM(u))
- pa_source_unlink(u->source);
+ if (!device->transports[*d] || device->transports[*d]->state == PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED) {
+ pa_log_warn("Profile not connected, refused to switch profile to %s", new_profile->name);
+ return -PA_ERR_IO;
+ }
}
stop_thread(u);
- if (u->profile != PROFILE_OFF && u->transport) {
- bt_transport_release(u);
- pa_xfree(u->transport);
- u->transport = NULL;
- }
-
- shutdown_bt(u);
-
if (USE_SCO_OVER_PCM(u))
restore_sco_volume_callbacks(u);
if (USE_SCO_OVER_PCM(u))
save_sco_volume_callbacks(u);
- init_bt(u);
-
if (u->profile != PROFILE_OFF)
- init_profile(u);
+ if (init_profile(u) < 0)
+ goto off;
if (u->sink || u->source)
- start_thread(u);
+ if (start_thread(u) < 0)
+ goto off;
- if (inputs) {
- if (u->sink)
- pa_sink_move_all_finish(u->sink, inputs, FALSE);
- else
- pa_sink_move_all_fail(inputs);
- }
+ return 0;
- if (outputs) {
- if (u->source)
- pa_source_move_all_finish(u->source, outputs, FALSE);
- else
- pa_source_move_all_fail(outputs);
- }
+off:
+ stop_thread(u);
- return 0;
+ pa_assert_se(pa_card_set_profile(u->card, "off", false) >= 0);
+
+ return -PA_ERR_IO;
}
/* Run from main thread */
-static int add_card(struct userdata *u, const pa_bluetooth_device *device) {
- pa_card_new_data data;
- pa_bool_t b;
- pa_card_profile *p;
- enum profile *d;
- const char *ff;
- char *n;
- const char *default_profile;
+static void create_card_ports(struct userdata *u, pa_hashmap *ports) {
+ pa_device_port *port;
+ pa_device_port_new_data port_data;
+
+ const char *name_prefix = NULL;
+ const char *input_description = NULL;
+ const char *output_description = NULL;
pa_assert(u);
- pa_assert(device);
+ pa_assert(ports);
+ pa_assert(u->device);
- pa_card_new_data_init(&data);
- data.driver = __FILE__;
- data.module = u->module;
+ switch (pa_bluetooth_get_form_factor(u->device->class)) {
+ case PA_BT_FORM_FACTOR_UNKNOWN:
+ break;
- n = pa_bluetooth_cleanup_name(device->name);
- pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, n);
- pa_xfree(n);
- pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, device->address);
- pa_proplist_sets(data.proplist, PA_PROP_DEVICE_API, "bluez");
- pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "sound");
- pa_proplist_sets(data.proplist, PA_PROP_DEVICE_BUS, "bluetooth");
- if ((ff = pa_bluetooth_get_form_factor(device->class)))
- pa_proplist_sets(data.proplist, PA_PROP_DEVICE_FORM_FACTOR, ff);
- pa_proplist_sets(data.proplist, "bluez.path", device->path);
- pa_proplist_setf(data.proplist, "bluez.class", "0x%06x", (unsigned) device->class);
- pa_proplist_sets(data.proplist, "bluez.name", device->name);
- data.name = get_name("card", u->modargs, device->address, &b);
- data.namereg_fail = b;
+ case PA_BT_FORM_FACTOR_HEADSET:
+ name_prefix = "headset";
+ input_description = output_description = _("Headset");
+ break;
- if (pa_modargs_get_proplist(u->modargs, "card_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
- pa_log("Invalid properties");
- pa_card_new_data_done(&data);
- return -1;
+ case PA_BT_FORM_FACTOR_HANDSFREE:
+ name_prefix = "handsfree";
+ input_description = output_description = _("Handsfree");
+ break;
+
+ case PA_BT_FORM_FACTOR_MICROPHONE:
+ name_prefix = "microphone";
+ input_description = _("Microphone");
+ break;
+
+ case PA_BT_FORM_FACTOR_SPEAKER:
+ name_prefix = "speaker";
+ output_description = _("Speaker");
+ break;
+
+ case PA_BT_FORM_FACTOR_HEADPHONE:
+ name_prefix = "headphone";
+ output_description = _("Headphone");
+ break;
+
+ case PA_BT_FORM_FACTOR_PORTABLE:
+ name_prefix = "portable";
+ input_description = output_description = _("Portable");
+ break;
+
+ case PA_BT_FORM_FACTOR_CAR:
+ name_prefix = "car";
+ input_description = output_description = _("Car");
+ break;
+
+ case PA_BT_FORM_FACTOR_HIFI:
+ name_prefix = "hifi";
+ input_description = output_description = _("HiFi");
+ break;
+
+ case PA_BT_FORM_FACTOR_PHONE:
+ name_prefix = "phone";
+ input_description = output_description = _("Phone");
+ break;
}
- data.profiles = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
+ if (!name_prefix)
+ name_prefix = "unknown";
+
+ if (!output_description)
+ output_description = _("Bluetooth Output");
+
+ if (!input_description)
+ input_description = _("Bluetooth Input");
+
+ u->output_port_name = pa_sprintf_malloc("%s-output", name_prefix);
+ u->input_port_name = pa_sprintf_malloc("%s-input", name_prefix);
+
+ pa_device_port_new_data_init(&port_data);
+ pa_device_port_new_data_set_name(&port_data, u->output_port_name);
+ pa_device_port_new_data_set_description(&port_data, output_description);
+ pa_device_port_new_data_set_direction(&port_data, PA_DIRECTION_OUTPUT);
+ pa_device_port_new_data_set_available(&port_data, get_port_availability(u, PA_DIRECTION_OUTPUT));
+ pa_assert_se(port = pa_device_port_new(u->core, &port_data, 0));
+ pa_assert_se(pa_hashmap_put(ports, port->name, port) >= 0);
+ pa_device_port_new_data_done(&port_data);
+
+ pa_device_port_new_data_init(&port_data);
+ pa_device_port_new_data_set_name(&port_data, u->input_port_name);
+ pa_device_port_new_data_set_description(&port_data, output_description);
+ pa_device_port_new_data_set_direction(&port_data, PA_DIRECTION_INPUT);
+ pa_device_port_new_data_set_available(&port_data, get_port_availability(u, PA_DIRECTION_INPUT));
+ pa_assert_se(port = pa_device_port_new(u->core, &port_data, 0));
+ pa_assert_se(pa_hashmap_put(ports, port->name, port) >= 0);
+ pa_device_port_new_data_done(&port_data);
+}
+
+/* Run from main thread */
+static pa_card_profile *create_card_profile(struct userdata *u, const char *uuid, pa_hashmap *ports) {
+ pa_device_port *input_port, *output_port;
+ pa_card_profile *p = NULL;
+ enum profile *d;
+
+ pa_assert(u->input_port_name);
+ pa_assert(u->output_port_name);
+ pa_assert_se(input_port = pa_hashmap_get(ports, u->input_port_name));
+ pa_assert_se(output_port = pa_hashmap_get(ports, u->output_port_name));
- /* we base hsp/a2dp availability on UUIDs.
- Ideally, it would be based on "Connected" state, but
- we can't afford to wait for this information when
- we are loaded with profile="hsp", for instance */
- if (pa_bluetooth_uuid_has(device->uuids, A2DP_SINK_UUID)) {
+ if (pa_streq(uuid, A2DP_SINK_UUID)) {
p = pa_card_profile_new("a2dp", _("High Fidelity Playback (A2DP)"), sizeof(enum profile));
p->priority = 10;
p->n_sinks = 1;
p->n_sources = 0;
p->max_sink_channels = 2;
p->max_source_channels = 0;
+ pa_hashmap_put(output_port->profiles, p->name, p);
d = PA_CARD_PROFILE_DATA(p);
*d = PROFILE_A2DP;
-
- pa_hashmap_put(data.profiles, p->name, p);
- }
-
- if (pa_bluetooth_uuid_has(device->uuids, A2DP_SOURCE_UUID)) {
+ } else if (pa_streq(uuid, 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;
+ pa_hashmap_put(input_port->profiles, p->name, p);
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)) {
+ } else if (pa_streq(uuid, HSP_HS_UUID) || pa_streq(uuid, HFP_HS_UUID)) {
p = pa_card_profile_new("hsp", _("Telephony Duplex (HSP/HFP)"), sizeof(enum profile));
p->priority = 20;
p->n_sinks = 1;
p->n_sources = 1;
p->max_sink_channels = 1;
p->max_source_channels = 1;
+ pa_hashmap_put(input_port->profiles, p->name, p);
+ pa_hashmap_put(output_port->profiles, p->name, p);
d = PA_CARD_PROFILE_DATA(p);
*d = PROFILE_HSP;
-
- pa_hashmap_put(data.profiles, p->name, p);
- }
-
- if (pa_bluetooth_uuid_has(device->uuids, HFP_AG_UUID)) {
+ } else if (pa_streq(uuid, 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;
+ pa_hashmap_put(input_port->profiles, p->name, p);
+ pa_hashmap_put(output_port->profiles, p->name, p);
d = PA_CARD_PROFILE_DATA(p);
*d = PROFILE_HFGW;
+ }
+
+ if (p) {
+ pa_bluetooth_transport *t;
+
+ if ((t = u->device->transports[*d]))
+ p->available = transport_state_to_availability(t->state);
+ }
+
+ return p;
+}
+
+/* Run from main thread */
+static int add_card(struct userdata *u) {
+ pa_card_new_data data;
+ bool b;
+ pa_card_profile *p;
+ enum profile *d;
+ pa_bt_form_factor_t ff;
+ char *n;
+ const char *default_profile;
+ const pa_bluetooth_device *device = u->device;
+ const pa_bluetooth_uuid *uuid;
+
+ pa_assert(u);
+ pa_assert(device);
+
+ pa_card_new_data_init(&data);
+ data.driver = __FILE__;
+ data.module = u->module;
+
+ n = pa_bluetooth_cleanup_name(device->alias);
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, n);
+ pa_xfree(n);
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, device->address);
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_API, "bluez");
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "sound");
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_BUS, "bluetooth");
+
+ if ((ff = pa_bluetooth_get_form_factor(device->class)) != PA_BT_FORM_FACTOR_UNKNOWN)
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_FORM_FACTOR, pa_bt_form_factor_to_string(ff));
+
+ pa_proplist_sets(data.proplist, "bluez.path", device->path);
+ pa_proplist_setf(data.proplist, "bluez.class", "0x%06x", (unsigned) device->class);
+ pa_proplist_sets(data.proplist, "bluez.name", device->name);
+ pa_proplist_sets(data.proplist, "bluez.alias", device->alias);
+ data.name = get_name("card", u->modargs, device->address, &b);
+ data.namereg_fail = b;
+
+ if (pa_modargs_get_proplist(u->modargs, "card_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
+ pa_log("Invalid properties");
+ pa_card_new_data_done(&data);
+ return -1;
+ }
+
+ create_card_ports(u, data.ports);
+
+ PA_LLIST_FOREACH(uuid, device->uuids) {
+ p = create_card_profile(u, uuid->uuid, data.ports);
+
+ if (!p)
+ continue;
+
+ if (pa_hashmap_get(data.profiles, p->name)) {
+ pa_card_profile_free(p);
+ continue;
+ }
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));
+ p->available = PA_AVAILABLE_YES;
d = PA_CARD_PROFILE_DATA(p);
*d = PROFILE_OFF;
pa_hashmap_put(data.profiles, p->name, p);
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->hfgw_state < PA_BT_AUDIO_STATE_CONNECTED && *d == PROFILE_HFGW)) {
+ if (*d != PROFILE_OFF && (!device->transports[*d] ||
+ device->transports[*d]->state == PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED)) {
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;
+ u->card->save_profile = false;
}
d = PA_CARD_PROFILE_DATA(u->card->active_profile);
}
/* Run from main thread */
-static const pa_bluetooth_device* find_device(struct userdata *u, const char *address, const char *path) {
- const pa_bluetooth_device *d = NULL;
+static pa_bluetooth_device* find_device(struct userdata *u, const char *address, const char *path) {
+ pa_bluetooth_device *d = NULL;
pa_assert(u);
}
/* Run from main thread */
-static int setup_dbus(struct userdata *u) {
- DBusError err;
+static pa_hook_result_t uuid_added_cb(pa_bluetooth_discovery *y, const struct pa_bluetooth_hook_uuid_data *data,
+ struct userdata *u) {
+ pa_card_profile *p;
+
+ pa_assert(data);
+ pa_assert(data->device);
+ pa_assert(data->uuid);
+ pa_assert(u);
- dbus_error_init(&err);
+ if (data->device != u->device)
+ return PA_HOOK_OK;
- u->connection = pa_dbus_bus_get(u->core, DBUS_BUS_SYSTEM, &err);
+ p = create_card_profile(u, data->uuid, u->card->ports);
- if (dbus_error_is_set(&err) || !u->connection) {
- pa_log("Failed to get D-Bus connection: %s", err.message);
- dbus_error_free(&err);
- return -1;
+ if (!p)
+ return PA_HOOK_OK;
+
+ if (pa_hashmap_get(u->card->profiles, p->name)) {
+ pa_card_profile_free(p);
+ return PA_HOOK_OK;
}
- return 0;
+ pa_card_add_profile(u->card, p);
+
+ return PA_HOOK_OK;
+}
+
+/* Run from main thread */
+static pa_hook_result_t discovery_hook_cb(pa_bluetooth_discovery *y, const pa_bluetooth_device *d, struct userdata *u) {
+ pa_assert(u);
+ pa_assert(d);
+
+ if (d != u->device)
+ return PA_HOOK_OK;
+
+ if (d->dead)
+ pa_log_debug("Device %s removed: unloading module", d->path);
+ else if (!pa_bluetooth_device_any_audio_connected(d))
+ pa_log_debug("Unloading module, because device %s doesn't have any audio profiles connected anymore.", d->path);
+ else
+ return PA_HOOK_OK;
+
+ pa_module_unload(u->core, u->module, true);
+
+ return PA_HOOK_OK;
}
int pa__init(pa_module* m) {
uint32_t channels;
struct userdata *u;
const char *address, *path;
- DBusError err;
- char *mike, *speaker;
- const pa_bluetooth_device *device;
+ pa_bluetooth_device *device;
pa_assert(m);
- dbus_error_init(&err);
-
if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
pa_log_error("Failed to parse module arguments");
goto fail;
m->userdata = u = pa_xnew0(struct userdata, 1);
u->module = m;
u->core = m->core;
- u->service_fd = -1;
u->stream_fd = -1;
u->sample_spec = m->core->default_sample_spec;
u->modargs = ma;
goto fail;
}
- u->auto_connect = TRUE;
+ 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;
address = pa_modargs_get_value(ma, "address", NULL);
path = pa_modargs_get_value(ma, "path", NULL);
- if (setup_dbus(u) < 0)
- goto fail;
-
if (!(u->discovery = pa_bluetooth_discovery_get(m->core)))
goto fail;
if (!(device = find_device(u, address, path)))
goto fail;
- /* Add the card structure. This will also initialize the default profile */
- if (add_card(u, device) < 0)
- goto fail;
+ u->device = device;
- if (!(u->msg = pa_msgobject_new(bluetooth_msg)))
- goto fail;
+ u->discovery_slot =
+ pa_hook_connect(pa_bluetooth_discovery_hook(u->discovery, PA_BLUETOOTH_HOOK_DEVICE_CONNECTION_CHANGED),
+ PA_HOOK_NORMAL, (pa_hook_cb_t) discovery_hook_cb, u);
- u->msg->parent.process_msg = device_process_msg;
- u->msg->card = u->card;
+ u->uuid_added_slot =
+ pa_hook_connect(pa_bluetooth_discovery_hook(u->discovery, PA_BLUETOOTH_HOOK_DEVICE_UUID_ADDED),
+ PA_HOOK_NORMAL, (pa_hook_cb_t) uuid_added_cb, u);
- 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;
+ u->sink_state_changed_slot =
+ pa_hook_connect(&u->core->hooks[PA_CORE_HOOK_SINK_STATE_CHANGED],
+ PA_HOOK_NORMAL, (pa_hook_cb_t) sink_state_changed_cb, u);
- 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);
+ u->source_state_changed_slot =
+ pa_hook_connect(&u->core->hooks[PA_CORE_HOOK_SOURCE_STATE_CHANGED],
+ PA_HOOK_NORMAL, (pa_hook_cb_t) source_state_changed_cb, u);
- if (pa_dbus_add_matches(
- pa_dbus_connection_get(u->connection), &err,
- speaker,
- mike,
- "type='signal',sender='org.bluez',interface='org.bluez.MediaTransport',member='PropertyChanged'",
- "type='signal',sender='org.bluez',interface='org.bluez.HandsfreeGateway',member='PropertyChanged'",
- NULL) < 0) {
+ u->transport_state_changed_slot =
+ pa_hook_connect(pa_bluetooth_discovery_hook(u->discovery, PA_BLUETOOTH_HOOK_TRANSPORT_STATE_CHANGED),
+ PA_HOOK_NORMAL, (pa_hook_cb_t) transport_state_changed_cb, u);
- pa_xfree(speaker);
- pa_xfree(mike);
+ u->transport_nrec_changed_slot =
+ pa_hook_connect(pa_bluetooth_discovery_hook(u->discovery, PA_BLUETOOTH_HOOK_TRANSPORT_NREC_CHANGED),
+ PA_HOOK_NORMAL, (pa_hook_cb_t) transport_nrec_changed_cb, u);
- pa_log("Failed to add D-Bus matches: %s", err.message);
+ u->transport_microphone_changed_slot =
+ pa_hook_connect(pa_bluetooth_discovery_hook(u->discovery, PA_BLUETOOTH_HOOK_TRANSPORT_MICROPHONE_GAIN_CHANGED),
+ PA_HOOK_NORMAL, (pa_hook_cb_t) transport_microphone_gain_changed_cb, u);
+
+ u->transport_speaker_changed_slot =
+ pa_hook_connect(pa_bluetooth_discovery_hook(u->discovery, PA_BLUETOOTH_HOOK_TRANSPORT_SPEAKER_GAIN_CHANGED),
+ PA_HOOK_NORMAL, (pa_hook_cb_t) transport_speaker_gain_changed_cb, u);
+
+ /* Add the card structure. This will also initialize the default profile */
+ if (add_card(u) < 0)
goto fail;
- }
- pa_xfree(speaker);
- pa_xfree(mike);
+ if (!(u->msg = pa_msgobject_new(bluetooth_msg)))
+ goto fail;
- /* Connect to the BT service */
- init_bt(u);
+ u->msg->parent.process_msg = device_process_msg;
+ u->msg->card = u->card;
if (u->profile != PROFILE_OFF)
if (init_profile(u) < 0)
- goto fail;
+ goto off;
if (u->sink || u->source)
if (start_thread(u) < 0)
- goto fail;
+ goto off;
+
+ return 0;
+
+off:
+ stop_thread(u);
+
+ pa_assert_se(pa_card_set_profile(u->card, "off", false) >= 0);
return 0;
pa__done(m);
- dbus_error_free(&err);
-
return -1;
}
if (!(u = m->userdata))
return;
- if (u->sink && !USE_SCO_OVER_PCM(u))
- pa_sink_unlink(u->sink);
+ stop_thread(u);
- if (u->source && !USE_SCO_OVER_PCM(u))
- pa_source_unlink(u->source);
+ if (u->discovery_slot)
+ pa_hook_slot_free(u->discovery_slot);
- stop_thread(u);
+ if (u->uuid_added_slot)
+ pa_hook_slot_free(u->uuid_added_slot);
- if (USE_SCO_OVER_PCM(u))
- restore_sco_volume_callbacks(u);
+ if (u->sink_state_changed_slot)
+ pa_hook_slot_free(u->sink_state_changed_slot);
- if (u->connection) {
+ if (u->source_state_changed_slot)
+ pa_hook_slot_free(u->source_state_changed_slot);
- if (u->path) {
- char *speaker, *mike;
- 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);
+ if (u->transport_state_changed_slot)
+ pa_hook_slot_free(u->transport_state_changed_slot);
- pa_dbus_remove_matches(pa_dbus_connection_get(u->connection), speaker, mike,
- "type='signal',sender='org.bluez',interface='org.bluez.MediaTransport',member='PropertyChanged'",
- "type='signal',sender='org.bluez',interface='org.bluez.HandsfreeGateway',member='PropertyChanged'",
- NULL);
+ if (u->transport_nrec_changed_slot)
+ pa_hook_slot_free(u->transport_nrec_changed_slot);
- pa_xfree(speaker);
- pa_xfree(mike);
- }
+ if (u->transport_microphone_changed_slot)
+ pa_hook_slot_free(u->transport_microphone_changed_slot);
- if (u->filter_added)
- dbus_connection_remove_filter(pa_dbus_connection_get(u->connection), filter_cb, u);
+ if (u->transport_speaker_changed_slot)
+ pa_hook_slot_free(u->transport_speaker_changed_slot);
- pa_dbus_connection_unref(u->connection);
- }
+ if (USE_SCO_OVER_PCM(u))
+ restore_sco_volume_callbacks(u);
if (u->msg)
pa_xfree(u->msg);
if (u->card)
pa_card_free(u->card);
- if (u->read_smoother)
- pa_smoother_free(u->read_smoother);
-
- shutdown_bt(u);
-
if (u->a2dp.buffer)
pa_xfree(u->a2dp.buffer);
if (u->modargs)
pa_modargs_free(u->modargs);
+ pa_xfree(u->output_port_name);
+ pa_xfree(u->input_port_name);
+
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);