]> code.delx.au - pulseaudio/blobdiff - src/modules/bluetooth/module-bluetooth-device.c
devices: Set certain sink/source flags automatically.
[pulseaudio] / src / modules / bluetooth / module-bluetooth-device.c
index fadfa5403d4071c569ea5ea06c309e50976e4c72..9f8133638e9c5ac3b42e3db464370b82e3909117 100644 (file)
@@ -1,7 +1,7 @@
 /***
   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 <string.h>
 #include <errno.h>
-#include <poll.h>
-#include <sys/ioctl.h>
 #include <linux/sockios.h>
 #include <arpa/inet.h>
 
-#include <pulse/xmalloc.h>
-#include <pulse/timeval.h>
-#include <pulse/sample.h>
 #include <pulse/i18n.h>
+#include <pulse/rtclock.h>
+#include <pulse/sample.h>
+#include <pulse/timeval.h>
+#include <pulse/xmalloc.h>
 
 #include <pulsecore/module.h>
 #include <pulsecore/modargs.h>
+#include <pulsecore/core-rtclock.h>
 #include <pulsecore/core-util.h>
 #include <pulsecore/core-error.h>
+#include <pulsecore/shared.h>
 #include <pulsecore/socket-util.h>
 #include <pulsecore/thread.h>
 #include <pulsecore/thread-mq.h>
+#include <pulsecore/poll.h>
 #include <pulsecore/rtpoll.h>
 #include <pulsecore/time-smoother.h>
-#include <pulsecore/rtclock.h>
 #include <pulsecore/namereg.h>
 #include <pulsecore/dbus-shared.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 MAX_BITPOOL 64
-#define MIN_BITPOOL 2U
+#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");
@@ -64,37 +67,38 @@ PA_MODULE_LOAD_ONCE(FALSE);
 PA_MODULE_USAGE(
         "name=<name for the card/sink/source, to be prefixed> "
         "card_name=<name for the card> "
+        "card_properties=<properties for the card> "
         "sink_name=<name for the sink> "
+        "sink_properties=<properties for the sink> "
         "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>");
-
-/*
-#ifdef NOKIA
+        "path=<device object path> "
+        "auto_connect=<automatically connect?> "
         "sco_sink=<SCO over PCM sink name> "
-        "sco_source=<SCO over PCM source name>"
-#endif
-*/
+        "sco_source=<SCO over PCM source name>");
 
 /* TODO: not close fd when entering suspend mode in a2dp */
 
 static const char* const valid_modargs[] = {
     "name",
     "card_name",
+    "card_properties",
     "sink_name",
+    "sink_properties",
     "source_name",
+    "source_properties",
     "address",
     "profile",
     "rate",
     "channels",
     "path",
-#ifdef NOKIA
+    "auto_connect",
     "sco_sink",
     "sco_source",
-#endif
     NULL
 };
 
@@ -108,31 +112,31 @@ struct a2dp_info {
     size_t buffer_size;                  /* Size of the buffer */
 
     uint16_t seq_num;                    /* Cumulative packet sequence */
+    uint8_t min_bitpool;
+    uint8_t max_bitpool;
 };
 
 struct hsp_info {
     pcm_capabilities_t pcm_capabilities;
-#ifdef NOKIA
     pa_sink *sco_sink;
+    void (*sco_sink_set_volume)(pa_sink *s);
     pa_source *sco_source;
-#endif
+    void (*sco_source_set_volume)(pa_source *s);
     pa_hook_slot *sink_state_changed_slot;
     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;
 
@@ -166,13 +170,20 @@ struct userdata {
 
     pa_modargs *modargs;
 
-    int stream_write_type, stream_read_type;
+    int stream_write_type;
     int service_write_type, service_read_type;
+
+    pa_bool_t filter_added;
 };
 
-#ifdef NOKIA
+#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)
+
+#define MAX_PLAYBACK_CATCH_UP_USEC (100*PA_USEC_PER_MSEC)
+
 #define USE_SCO_OVER_PCM(u) (u->profile == PROFILE_HSP && (u->hsp.sco_sink && u->hsp.sco_source))
-#endif
 
 static int init_bt(struct userdata *u);
 static int init_profile(struct userdata *u);
@@ -181,10 +192,14 @@ static int service_send(struct userdata *u, const bt_audio_msg_header_t *msg) {
     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)));
@@ -206,9 +221,7 @@ static int service_recv(struct userdata *u, bt_audio_msg_header_t *msg, size_t r
     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...");
 
@@ -221,6 +234,11 @@ static int service_recv(struct userdata *u, bt_audio_msg_header_t *msg, size_t r
         return -1;
     }
 
+    if (msg->length > room) {
+        pa_log_error("Not enough room.");
+        return -1;
+    }
+
     /* Secondly, read the payload */
     if (msg->length > sizeof(*msg)) {
 
@@ -275,6 +293,7 @@ static ssize_t service_expect(struct userdata*u, bt_audio_msg_header_t *rsp, siz
     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;
@@ -293,13 +312,13 @@ static int parse_caps(struct userdata *u, uint8_t seid, const struct bt_get_capa
 
     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;
@@ -326,6 +345,26 @@ static int parse_caps(struct userdata *u, uint8_t seid, const struct bt_get_capa
 
         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;
 
@@ -335,6 +374,7 @@ static int parse_caps(struct userdata *u, uint8_t seid, const struct bt_get_capa
     return 0;
 }
 
+/* Run from main thread */
 static int get_caps(struct userdata *u, uint8_t seid) {
     union {
         struct bt_get_capabilities_req getcaps_req;
@@ -353,13 +393,13 @@ static int get_caps(struct userdata *u, uint8_t seid) {
     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;
@@ -374,6 +414,7 @@ static int get_caps(struct userdata *u, uint8_t seid) {
     return get_caps(u, ret);
 }
 
+/* Run from main thread */
 static uint8_t a2dp_default_bitpool(uint8_t freq, uint8_t mode) {
 
     switch (freq) {
@@ -419,6 +460,7 @@ static uint8_t a2dp_default_bitpool(uint8_t freq, uint8_t mode) {
     }
 }
 
+/* Run from main thread */
 static int setup_a2dp(struct userdata *u) {
     sbc_capabilities_t *cap;
     int i;
@@ -434,7 +476,7 @@ static int setup_a2dp(struct userdata *u) {
     };
 
     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;
 
@@ -462,7 +504,7 @@ static int setup_a2dp(struct userdata *u) {
         }
     }
 
-    pa_assert(i < PA_ELEMENTSOF(freq_table));
+    pa_assert((unsigned) i < PA_ELEMENTSOF(freq_table));
 
     if (cap->capability.configured)
         return 0;
@@ -526,7 +568,8 @@ static int setup_a2dp(struct userdata *u) {
     return 0;
 }
 
-static void setup_sbc(struct a2dp_info *a2dp) {
+/* Run from main thread */
+static void setup_sbc(struct a2dp_info *a2dp, enum profile p) {
     sbc_capabilities_t *active_capabilities;
 
     pa_assert(a2dp);
@@ -612,11 +655,16 @@ static void setup_sbc(struct a2dp_info *a2dp) {
             pa_assert_not_reached();
     }
 
-    a2dp->sbc.bitpool = active_capabilities->max_bitpool;
+    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;
@@ -633,8 +681,8 @@ static int set_conf(struct userdata *u) {
     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;
@@ -642,13 +690,13 @@ static int set_conf(struct userdata *u) {
     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;
@@ -660,7 +708,7 @@ static int set_conf(struct userdata *u) {
     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;
@@ -678,8 +726,8 @@ static int set_conf(struct userdata *u) {
     u->link_mtu = msg.setconf_rsp.link_mtu;
 
     /* setup SBC encoder now we agree on parameters */
-    if (u->profile == PROFILE_A2DP) {
-        setup_sbc(&u->a2dp);
+    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))
@@ -694,7 +742,78 @@ static int set_conf(struct userdata *u) {
     return 0;
 }
 
+/* from IO thread */
+static void a2dp_set_bitpool(struct userdata *u, uint8_t bitpool)
+{
+    struct a2dp_info *a2dp;
+
+    pa_assert(u);
+
+    a2dp = &u->a2dp;
+
+    if (a2dp->sbc.bitpool == bitpool)
+        return;
+
+    if (bitpool > a2dp->max_bitpool)
+        bitpool = a2dp->max_bitpool;
+    else if (bitpool < a2dp->min_bitpool)
+        bitpool = a2dp->min_bitpool;
+
+    a2dp->sbc.bitpool = bitpool;
+
+    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->block_size =
+            (u->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->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));
+}
+
 /* 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!");
+
+    if (u->profile == PROFILE_A2DP)
+        a2dp_set_bitpool(u, u->a2dp.max_bitpool);
+
+    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;
@@ -704,7 +823,6 @@ static int start_stream_fd(struct userdata *u) {
         bt_audio_error_t error;
         uint8_t buf[BT_SUGGESTED_BUFFER_SIZE];
     } msg;
-    struct pollfd *pollfd;
 
     pa_assert(u);
     pa_assert(u->rtpoll);
@@ -730,18 +848,7 @@ static int start_stream_fd(struct userdata *u) {
         return -1;
     }
 
-    pa_make_fd_nonblock(u->stream_fd);
-    pa_make_socket_low_delay(u->stream_fd);
-
-    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 = 0;
-    u->write_index = 0;
-
-    return 0;
+    return setup_stream(u);
 }
 
 /* from IO thread */
@@ -757,27 +864,104 @@ static int stop_stream_fd(struct userdata *u) {
 
     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;
+    }
+
+    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);
 
-    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);
+        u->read_smoother = NULL;
+    }
 
     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;
+    }
+
+    /* 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;
+
+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_bool_t failed = FALSE;
@@ -785,7 +969,6 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse
 
     pa_assert(u->sink == PA_SINK(o));
 
-    pa_log_debug("got message: %d", code);
     switch (code) {
 
         case PA_SINK_MESSAGE_SET_STATE:
@@ -796,11 +979,15 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse
                     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;
 
@@ -810,11 +997,13 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse
                         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;
-
-                    u->started_at = pa_rtclock_usec();
+                    }
                     break;
 
                 case PA_SINK_UNLINKED:
@@ -825,7 +1014,24 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse
             break;
 
         case PA_SINK_MESSAGE_GET_LATENCY: {
-            *((pa_usec_t*) data) = 0;
+
+            if (u->read_smoother) {
+                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);
+
+                *((pa_usec_t*) data) = wi > ri ? wi - ri : 0;
+            } else {
+                pa_usec_t ri, wi;
+
+                ri = pa_rtclock_now() - u->started_at;
+                wi = pa_bytes_to_usec(u->write_index, &u->sample_spec);
+
+                *((pa_usec_t*) data) = wi > ri ? wi - ri : 0;
+            }
+
+            *((pa_usec_t*) data) += u->sink->thread_info.fixed_latency;
             return 0;
         }
     }
@@ -835,6 +1041,7 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse
     return (r < 0 || !failed) ? r : -1;
 }
 
+/* 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;
@@ -842,7 +1049,6 @@ static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t off
 
     pa_assert(u->source == PA_SOURCE(o));
 
-    pa_log_debug("got message: %d", code);
     switch (code) {
 
         case PA_SOURCE_MESSAGE_SET_STATE:
@@ -853,10 +1059,15 @@ static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t off
                     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);
+                    }
 
-                    pa_smoother_pause(u->read_smoother, pa_rtclock_usec());
+                    if (u->read_smoother)
+                        pa_smoother_pause(u->read_smoother, pa_rtclock_now());
                     break;
 
                 case PA_SOURCE_IDLE:
@@ -865,11 +1076,15 @@ static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t off
                         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;
-
-                    pa_smoother_resume(u->read_smoother, pa_rtclock_usec());
+                        } 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_UNLINKED:
@@ -880,7 +1095,16 @@ static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t off
             break;
 
         case PA_SOURCE_MESSAGE_GET_LATENCY: {
-            *((pa_usec_t*) data) = 0;
+            pa_usec_t wi, ri;
+
+            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;
+
             return 0;
         }
 
@@ -891,11 +1115,12 @@ static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t off
     return (r < 0 || !failed) ? r : -1;
 }
 
+/* Run from IO thread */
 static int hsp_process_render(struct userdata *u) {
     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 */
@@ -947,19 +1172,22 @@ static int hsp_process_render(struct userdata *u) {
         pa_memblock_unref(u->write_memchunk.memblock);
         pa_memchunk_reset(&u->write_memchunk);
 
+        ret = 1;
         break;
     }
 
     return ret;
 }
 
+/* Run from IO thread */
 static int hsp_process_push(struct userdata *u) {
     int ret = 0;
     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);
 
     memchunk.memblock = pa_memblock_new(u->core->mempool, u->block_size);
     memchunk.index = memchunk.length = 0;
@@ -967,9 +1195,26 @@ static int hsp_process_push(struct userdata *u) {
     for (;;) {
         ssize_t l;
         void *p;
+        struct msghdr m;
+        struct cmsghdr *cm;
+        uint8_t aux[1024];
+        struct iovec iov;
+        pa_bool_t found_tstamp = FALSE;
+        pa_usec_t tstamp;
+
+        memset(&m, 0, sizeof(m));
+        memset(&aux, 0, sizeof(aux));
+        memset(&iov, 0, sizeof(iov));
+
+        m.msg_iov = &iov;
+        m.msg_iovlen = 1;
+        m.msg_control = aux;
+        m.msg_controllen = sizeof(aux);
 
         p = pa_memblock_acquire(memchunk.memblock);
-        l = pa_read(u->stream_fd, p, pa_memblock_get_length(memchunk.memblock), &u->stream_read_type);
+        iov.iov_base = p;
+        iov.iov_len = pa_memblock_get_length(memchunk.memblock);
+        l = recvmsg(u->stream_fd, &m, 0);
         pa_memblock_release(memchunk.memblock);
 
         if (l <= 0) {
@@ -992,7 +1237,26 @@ static int hsp_process_push(struct userdata *u) {
         memchunk.length = (size_t) l;
         u->read_index += (uint64_t) l;
 
+        for (cm = CMSG_FIRSTHDR(&m); cm; cm = CMSG_NXTHDR(&m, cm))
+            if (cm->cmsg_level == SOL_SOCKET && cm->cmsg_type == SO_TIMESTAMP) {
+                struct timeval *tv = (struct timeval*) CMSG_DATA(cm);
+                pa_rtclock_from_wallclock(tv);
+                tstamp = pa_timeval_load(tv);
+                found_tstamp = TRUE;
+                break;
+            }
+
+        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);
+
         pa_source_post(u->source, &memchunk);
+
+        ret = 1;
         break;
     }
 
@@ -1001,6 +1265,7 @@ static int hsp_process_push(struct userdata *u) {
     return ret;
 }
 
+/* Run from IO thread */
 static void a2dp_prepare_buffer(struct userdata *u) {
     pa_assert(u);
 
@@ -1012,6 +1277,7 @@ static void a2dp_prepare_buffer(struct userdata *u) {
     u->a2dp.buffer = pa_xmalloc(u->a2dp.buffer_size);
 }
 
+/* Run from IO thread */
 static int a2dp_process_render(struct userdata *u) {
     struct a2dp_info *a2dp;
     struct rtp_header *header;
@@ -1050,7 +1316,7 @@ static int a2dp_process_render(struct userdata *u) {
     to_write = a2dp->buffer_size - sizeof(*header) - sizeof(*payload);
 
     while (PA_LIKELY(to_encode > 0 && to_write > 0)) {
-        size_t written;
+        ssize_t written;
         ssize_t encoded;
 
         encoded = sbc_encode(&a2dp->sbc,
@@ -1095,7 +1361,7 @@ static int a2dp_process_render(struct userdata *u) {
     header->v = 2;
     header->pt = 1;
     header->sequence_number = htons(a2dp->seq_num++);
-    header->timestamp = htonl(u->write_index / pa_frame_size(&u->sink->sample_spec));
+    header->timestamp = htonl(u->write_index / pa_frame_size(&u->sample_spec));
     header->ssrc = htonl(1);
     payload->frame_count = frame_count;
 
@@ -1119,7 +1385,7 @@ static int a2dp_process_render(struct userdata *u) {
                 break;
 
             pa_log_error("Failed to write data to socket: %s", pa_cstrerror(errno));
-            ret  = -1;
+            ret = -1;
             break;
         }
 
@@ -1137,15 +1403,156 @@ static int a2dp_process_render(struct userdata *u) {
         pa_memblock_unref(u->write_memchunk.memblock);
         pa_memchunk_reset(&u->write_memchunk);
 
+        ret = 1;
+
+        break;
+    }
+
+    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)) {
+            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); */
+
+            /* Reset frame length, it can be changed due to bitpool change */
+            a2dp->frame_length = sbc_get_frame_length(&a2dp->sbc);
+
+            pa_assert_fp((size_t) decoded <= to_decode);
+            pa_assert_fp((size_t) decoded == a2dp->frame_length);
+
+            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++;
+        }
+
+        memchunk.length -= to_write;
+
+        pa_memblock_release(memchunk.memblock);
+
+        pa_source_post(u->source, &memchunk);
+
+        ret = 1;
         break;
     }
 
+    pa_memblock_unref(memchunk.memblock);
+
     return ret;
 }
 
+static void a2dp_reduce_bitpool(struct userdata *u)
+{
+    struct a2dp_info *a2dp;
+    uint8_t bitpool;
+
+    pa_assert(u);
+
+    a2dp = &u->a2dp;
+
+    /* Check if bitpool is already at its limit */
+    if (a2dp->sbc.bitpool <= BITPOOL_DEC_LIMIT)
+        return;
+
+    bitpool = a2dp->sbc.bitpool - BITPOOL_DEC_STEP;
+
+    if (bitpool < BITPOOL_DEC_LIMIT)
+        bitpool = BITPOOL_DEC_LIMIT;
+
+    a2dp_set_bitpool(u, bitpool);
+}
+
 static void thread_func(void *userdata) {
     struct userdata *u = userdata;
-    pa_bool_t do_write = FALSE, writable = FALSE;
+    unsigned do_write = 0;
+    pa_bool_t writable = FALSE;
 
     pa_assert(u);
 
@@ -1154,13 +1561,13 @@ static void thread_func(void *userdata) {
     if (u->core->realtime_scheduling)
         pa_make_realtime(u->core->realtime_priority);
 
-    if (start_stream_fd(u) < 0)
-        goto fail;
-
     pa_thread_mq_install(&u->thread_mq);
-    pa_rtpoll_install(u->rtpoll);
 
-    pa_smoother_set_time_offset(u->read_smoother, pa_rtclock_usec());
+    if (u->transport) {
+        if (bt_transport_acquire(u, TRUE) < 0)
+            goto fail;
+    } else if (start_stream_fd(u) < 0)
+        goto fail;
 
     for (;;) {
         struct pollfd *pollfd;
@@ -1171,13 +1578,25 @@ static void thread_func(void *userdata) {
 
         if (u->source && PA_SOURCE_IS_LINKED(u->source->thread_info.state)) {
 
+            /* We should send two blocks to the device before we expect
+             * a response. */
+
+            if (u->write_index == 0 && u->read_index <= 0)
+                do_write = 2;
+
             if (pollfd && (pollfd->revents & POLLIN)) {
+                int n_read;
+
+                if (u->profile == PROFILE_HSP || u->profile == PROFILE_HFGW)
+                    n_read = hsp_process_push(u);
+                else
+                    n_read = a2dp_process_push(u);
 
-                if (hsp_process_push(u) < 0)
+                if (n_read < 0)
                     goto fail;
 
                 /* We just read something, so we are supposed to write something, too */
-                do_write = TRUE;
+                do_write += n_read;
             }
         }
 
@@ -1190,47 +1609,82 @@ static void thread_func(void *userdata) {
                 if (pollfd->revents & POLLOUT)
                     writable = TRUE;
 
-                if ((!u->source || !PA_SOURCE_IS_LINKED(u->source->thread_info.state)) && !do_write && writable) {
+                if ((!u->source || !PA_SOURCE_IS_LINKED(u->source->thread_info.state)) && do_write <= 0 && writable) {
                     pa_usec_t time_passed;
-                    uint64_t should_have_written;
+                    pa_usec_t audio_sent;
 
                     /* Hmm, there is no input stream we could synchronize
                      * to. So let's do things by time */
 
-                    time_passed = pa_rtclock_usec() - u->started_at;
-                    should_have_written = pa_usec_to_bytes(time_passed, &u->sink->sample_spec);
+                    time_passed = pa_rtclock_now() - u->started_at;
+                    audio_sent = pa_bytes_to_usec(u->write_index, &u->sample_spec);
 
-                    do_write = u->write_index <= should_have_written ;
-/*                 pa_log_debug("Time has come: %s", pa_yes_no(do_write)); */
+                    if (audio_sent <= time_passed) {
+                        pa_usec_t audio_to_send = time_passed - audio_sent;
+
+                        /* Never try to catch up for more than 100ms */
+                        if (u->write_index > 0 && audio_to_send > MAX_PLAYBACK_CATCH_UP_USEC) {
+                            pa_usec_t skip_usec;
+                            uint64_t skip_bytes;
+
+                            skip_usec = audio_to_send - MAX_PLAYBACK_CATCH_UP_USEC;
+                            skip_bytes = pa_usec_to_bytes(skip_usec, &u->sample_spec);
+
+                            if (skip_bytes > 0) {
+                                pa_memchunk tmp;
+
+                                pa_log_warn("Skipping %llu us (= %llu bytes) in audio stream",
+                                            (unsigned long long) skip_usec,
+                                            (unsigned long long) skip_bytes);
+
+                                pa_sink_render_full(u->sink, skip_bytes, &tmp);
+                                pa_memblock_unref(tmp.memblock);
+                                u->write_index += skip_bytes;
+
+                                if (u->profile == PROFILE_A2DP)
+                                    a2dp_reduce_bitpool(u);
+                            }
+                        }
+
+                        do_write = 1;
+                    }
                 }
 
-                if (writable && do_write) {
-                    if (u->write_index == 0)
-                        u->started_at = pa_rtclock_usec();
+                if (writable && do_write > 0) {
+                    int n_written;
+
+                    if (u->write_index <= 0)
+                        u->started_at = pa_rtclock_now();
 
                     if (u->profile == PROFILE_A2DP) {
-                        if (a2dp_process_render(u) < 0)
+                        if ((n_written = a2dp_process_render(u)) < 0)
                             goto fail;
                     } else {
-                        if (hsp_process_render(u) < 0)
+                        if ((n_written = hsp_process_render(u)) < 0)
                             goto fail;
                     }
 
-                    do_write = FALSE;
+                    if (n_written == 0)
+                        pa_log("Broken kernel: we got EAGAIN on write() after POLLOUT!");
+
+                    do_write -= n_written;
                     writable = FALSE;
                 }
 
-                if ((!u->source || !PA_SOURCE_IS_LINKED(u->source->thread_info.state)) && !do_write) {
-                    pa_usec_t time_passed, next_write_at, sleep_for;
-
-                    /* Hmm, there is no input stream we could synchronize
-                     * to. So let's estimate when we need to wake up the latest */
-
-                    time_passed = pa_rtclock_usec() - u->started_at;
-                    next_write_at = pa_bytes_to_usec(u->write_index, &u->sink->sample_spec);
-                    sleep_for = time_passed < next_write_at ? next_write_at - time_passed : 0;
-
-/*                 pa_log("Sleeping for %lu; time passed %lu, next write at %lu", (unsigned long) sleep_for, (unsigned long) time_passed, (unsigned long)next_write_at); */
+                if ((!u->source || !PA_SOURCE_IS_LINKED(u->source->thread_info.state)) && do_write <= 0) {
+                    pa_usec_t sleep_for;
+                    pa_usec_t time_passed, next_write_at;
+
+                    if (writable) {
+                        /* Hmm, there is no input stream we could synchronize
+                         * to. So let's estimate when we need to wake up the latest */
+                        time_passed = pa_rtclock_now() - u->started_at;
+                        next_write_at = pa_bytes_to_usec(u->write_index, &u->sample_spec);
+                        sleep_for = time_passed < next_write_at ? next_write_at - time_passed : 0;
+                        /* pa_log("Sleeping for %lu; time passed %lu, next write at %lu", (unsigned long) sleep_for, (unsigned long) time_passed, (unsigned long)next_write_at); */
+                    } else
+                        /* drop stream every 500 ms */
+                        sleep_for = PA_USEC_PER_MSEC * 500;
 
                     pa_rtpoll_set_timer_relative(u->rtpoll, sleep_for);
                     disable_timer = FALSE;
@@ -1255,7 +1709,11 @@ static void thread_func(void *userdata) {
         pollfd = u->rtpoll_item ? pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL) : NULL;
 
         if (pollfd && (pollfd->revents & ~(POLLOUT|POLLIN))) {
-            pa_log_error("FD error.");
+            pa_log_info("FD error: %s%s%s%s",
+                        pollfd->revents & POLLERR ? "POLLERR " :"",
+                        pollfd->revents & POLLHUP ? "POLLHUP " :"",
+                        pollfd->revents & POLLPRI ? "POLLPRI " :"",
+                        pollfd->revents & POLLNVAL ? "POLLNVAL " :"");
             goto fail;
         }
     }
@@ -1270,6 +1728,7 @@ 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;
@@ -1285,8 +1744,8 @@ static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *m, void *us
                  dbus_message_get_path(m),
                  dbus_message_get_member(m));
 
-   if (!dbus_message_has_path(m, u->path))
-       goto fail;
+    if (!dbus_message_has_path(m, u->path) && !dbus_message_has_path(m, u->transport))
+        goto fail;
 
     if (dbus_message_is_signal(m, "org.bluez.Headset", "SpeakerGainChanged") ||
         dbus_message_is_signal(m, "org.bluez.Headset", "MicrophoneGainChanged")) {
@@ -1294,23 +1753,55 @@ static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *m, void *us
         dbus_uint16_t gain;
         pa_cvolume v;
 
-        if (!dbus_message_get_args(m, &err, DBUS_TYPE_UINT16, &gain, DBUS_TYPE_INVALID) || gain > 15) {
+        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;
         }
 
         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);
 
-                pa_cvolume_set(&v, u->sink->sample_spec.channels, (pa_volume_t) (gain * PA_VOLUME_NORM / 15));
+                /* increment volume by one to correct rounding errors */
+                if (volume < PA_VOLUME_NORM)
+                    volume++;
+
+                pa_cvolume_set(&v, u->sample_spec.channels, volume);
                 pa_sink_volume_changed(u->sink, &v);
 
             } 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);
+
+                /* increment volume by one to correct rounding errors */
+                if (volume < PA_VOLUME_NORM)
+                    volume++;
 
-                pa_cvolume_set(&v, u->sink->sample_spec.channels, (pa_volume_t) (gain * PA_VOLUME_NORM / 15));
+                pa_cvolume_set(&v, u->sample_spec.channels, volume);
                 pa_source_volume_changed(u->source, &v);
             }
         }
+    } 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);
+
+        if (!dbus_message_iter_init(m, &arg_i)) {
+            pa_log("Failed to parse PropertyChanged: %s", err.message);
+            goto fail;
+        }
+
+        nrec = t->nrec;
+
+        if (pa_bluetooth_transport_parse_property(t, &arg_i) < 0)
+            goto fail;
+
+        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");
+        }
     }
 
 fail:
@@ -1319,22 +1810,37 @@ fail:
     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 }
 
+/* Run from main thread */
 static void sink_set_volume_cb(pa_sink *s) {
-    struct userdata *u = s->userdata;
     DBusMessage *m;
     dbus_uint16_t gain;
+    pa_volume_t volume;
+    struct userdata *u;
+    char *k;
+
+    pa_assert(s);
+    pa_assert(s->core);
+
+    k = pa_sprintf_malloc("bluetooth-device@%p", (void*) s);
+    u = pa_shared_get(s->core, k);
+    pa_xfree(k);
 
     pa_assert(u);
+    pa_assert(u->sink == s);
+    pa_assert(u->profile == PROFILE_HSP);
 
-    if (u->profile != PROFILE_HSP)
-        return;
+    gain = (pa_cvolume_max(&s->real_volume) * HSP_MAX_GAIN) / PA_VOLUME_NORM;
+
+    if (gain > HSP_MAX_GAIN)
+        gain = HSP_MAX_GAIN;
 
-    gain = (pa_cvolume_max(&s->virtual_volume) * 15) / PA_VOLUME_NORM;
+    volume = (pa_volume_t) (gain * PA_VOLUME_NORM / HSP_MAX_GAIN);
 
-    if (gain > 15)
-        gain = 15;
+    /* increment volume by one to correct rounding errors */
+    if (volume < PA_VOLUME_NORM)
+        volume++;
 
-    pa_cvolume_set(&s->virtual_volume, u->sink->sample_spec.channels, (pa_volume_t) (gain * PA_VOLUME_NORM / 15));
+    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));
@@ -1342,22 +1848,37 @@ static void sink_set_volume_cb(pa_sink *s) {
     dbus_message_unref(m);
 }
 
+/* Run from main thread */
 static void source_set_volume_cb(pa_source *s) {
-    struct userdata *u = s->userdata;
     DBusMessage *m;
     dbus_uint16_t gain;
+    pa_volume_t volume;
+    struct userdata *u;
+    char *k;
+
+    pa_assert(s);
+    pa_assert(s->core);
+
+    k = pa_sprintf_malloc("bluetooth-device@%p", (void*) s);
+    u = pa_shared_get(s->core, k);
+    pa_xfree(k);
 
     pa_assert(u);
+    pa_assert(u->source == s);
+    pa_assert(u->profile == PROFILE_HSP);
 
-    if (u->profile != PROFILE_HSP)
-        return;
+    gain = (pa_cvolume_max(&s->real_volume) * HSP_MAX_GAIN) / PA_VOLUME_NORM;
 
-    gain = (pa_cvolume_max(&s->virtual_volume) * 15) / PA_VOLUME_NORM;
+    if (gain > HSP_MAX_GAIN)
+        gain = HSP_MAX_GAIN;
 
-    if (gain > 15)
-        gain = 15;
+    volume = (pa_volume_t) (gain * PA_VOLUME_NORM / HSP_MAX_GAIN);
 
-    pa_cvolume_set(&s->virtual_volume, u->source->sample_spec.channels, (pa_volume_t) (gain * PA_VOLUME_NORM / 15));
+    /* increment volume by one to correct rounding errors */
+    if (volume < PA_VOLUME_NORM)
+        volume++;
+
+    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));
@@ -1365,6 +1886,7 @@ static void source_set_volume_cb(pa_source *s) {
     dbus_message_unref(m);
 }
 
+/* Run from main thread */
 static char *get_name(const char *type, pa_modargs *ma, const char *device_id, pa_bool_t *namereg_fail) {
     char *t;
     const char *n;
@@ -1393,34 +1915,48 @@ static char *get_name(const char *type, pa_modargs *ma, const char *device_id, p
     return pa_sprintf_malloc("bluez_%s.%s", type, n);
 }
 
-#ifdef NOKIA
-
-static void sco_over_pcm_state_update(struct userdata *u) {
+static int sco_over_pcm_state_update(struct userdata *u, pa_bool_t 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)
-            return;
+        if (u->service_fd >= 0 && u->stream_fd >= 0)
+            return 0;
+
+        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");
+            return -1;
+        }
 
-        start_stream_fd(u);
-    } else {
+        if (u->transport)
+            return bt_transport_acquire(u, TRUE);
 
-        if (u->service_fd < 0)
-            return;
+        return start_stream_fd(u);
+    }
 
-        stop_stream_fd(u);
+    if (changed) {
+        if (u->service_fd < 0 && u->stream_fd < 0)
+            return 0;
 
         pa_log_debug("Closing SCO over PCM");
-        pa_close(u->service_fd);
-        u->service_fd = -1;
+
+        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;
+        }
     }
+
+    return 0;
 }
 
 static pa_hook_result_t sink_state_changed_cb(pa_core *c, pa_sink *s, struct userdata *u) {
@@ -1431,7 +1967,7 @@ static pa_hook_result_t sink_state_changed_cb(pa_core *c, pa_sink *s, struct use
     if (s != u->hsp.sco_sink)
         return PA_HOOK_OK;
 
-    sco_over_pcm_state_update(u);
+    sco_over_pcm_state_update(u, TRUE);
 
     return PA_HOOK_OK;
 }
@@ -1444,16 +1980,15 @@ static pa_hook_result_t source_state_changed_cb(pa_core *c, pa_source *s, struct
     if (s != u->hsp.sco_source)
         return PA_HOOK_OK;
 
-    sco_over_pcm_state_update(u);
+    sco_over_pcm_state_update(u, TRUE);
 
     return PA_HOOK_OK;
 }
 
-#endif
-
+/* Run from main thread */
 static int add_sink(struct userdata *u) {
+    char *k;
 
-#ifdef NOKIA
     if (USE_SCO_OVER_PCM(u)) {
         pa_proplist *p;
 
@@ -1466,10 +2001,7 @@ static int add_sink(struct userdata *u) {
         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
-#endif
-
-    {
+    } else {
         pa_sink_new_data data;
         pa_bool_t b;
 
@@ -1478,11 +2010,19 @@ static int add_sink(struct userdata *u) {
         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");
+        if (u->profile == PROFILE_HSP)
+            pa_proplist_sets(data.proplist, PA_PROP_DEVICE_INTENDED_ROLES, "phone");
         data.card = u->card;
         data.name = get_name("sink", u->modargs, u->address, &b);
         data.namereg_fail = b;
 
-        u->sink = pa_sink_new(u->core, &data, PA_SINK_HARDWARE|PA_SINK_LATENCY | (u->profile == PROFILE_HSP ? PA_SINK_HW_VOLUME_CTRL : 0));
+        if (pa_modargs_get_proplist(u->modargs, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
+            pa_log("Invalid properties");
+            pa_sink_new_data_done(&data);
+            return -1;
+        }
+
+        u->sink = pa_sink_new(u->core, &data, PA_SINK_HARDWARE|PA_SINK_LATENCY);
         pa_sink_new_data_done(&data);
 
         if (!u->sink) {
@@ -1492,30 +2032,37 @@ static int add_sink(struct userdata *u) {
 
         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));
     }
 
     if (u->profile == PROFILE_HSP) {
-        u->sink->set_volume = sink_set_volume_cb;
+        pa_sink_set_set_volume_callback(u->sink, sink_set_volume_cb);
         u->sink->n_volume_steps = 16;
+
+        k = pa_sprintf_malloc("bluetooth-device@%p", (void*) u->sink);
+        pa_shared_set(u->core, k, u);
+        pa_xfree(k);
     }
 
     return 0;
 }
 
+/* Run from main thread */
 static int add_source(struct userdata *u) {
+    char *k;
 
-#ifdef NOKIA
     if (USE_SCO_OVER_PCM(u)) {
         u->source = u->hsp.sco_source;
-        pa_proplist_sets(u->source->proplist, "bluetooth.protocol", "sco");
+        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);
 
-    } else
-#endif
-
-    {
+    } else {
         pa_source_new_data data;
         pa_bool_t b;
 
@@ -1523,12 +2070,21 @@ static int add_source(struct userdata *u) {
         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" : "sco");
+        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);
         data.namereg_fail = b;
 
-        u->source = pa_source_new(u->core, &data, PA_SOURCE_HARDWARE|PA_SOURCE_LATENCY | (u->profile == PROFILE_HSP ? PA_SOURCE_HW_VOLUME_CTRL : 0));
+        if (pa_modargs_get_proplist(u->modargs, "source_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
+            pa_log("Invalid properties");
+            pa_source_new_data_done(&data);
+            return -1;
+        }
+
+        u->source = pa_source_new(u->core, &data, PA_SOURCE_HARDWARE|PA_SOURCE_LATENCY);
         pa_source_new_data_done(&data);
 
         if (!u->source) {
@@ -1538,17 +2094,35 @@ static int add_source(struct userdata *u) {
 
         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));
+    }
+
+    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");
     }
 
     if (u->profile == PROFILE_HSP) {
-        pa_proplist_sets(u->source->proplist, "bluetooth.nrec", (u->hsp.pcm_capabilities.flags & BT_PCM_FLAG_NREC) ? "1" : "0");
-        u->source->set_volume = source_set_volume_cb;
+        pa_source_set_set_volume_callback(u->source, source_set_volume_cb);
         u->source->n_volume_steps = 16;
+
+        k = pa_sprintf_malloc("bluetooth-device@%p", (void*) u->source);
+        pa_shared_set(u->core, k, u);
+        pa_xfree(k);
     }
 
     return 0;
 }
 
+/* Run from main thread */
 static void shutdown_bt(struct userdata *u) {
     pa_assert(u);
 
@@ -1557,12 +2131,13 @@ static void shutdown_bt(struct userdata *u) {
         u->stream_fd = -1;
 
         u->stream_write_type = 0;
-        u->stream_read_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) {
@@ -1571,16 +2146,156 @@ static void shutdown_bt(struct userdata *u) {
     }
 }
 
+static int 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);
+    pa_assert(t);
+
+    config = (a2dp_sbc_t *) t->config;
+
+    u->sample_spec.format = PA_SAMPLE_S16LE;
+
+    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;
+            u->sample_spec.rate = 16000U;
+            break;
+        case BT_SBC_SAMPLING_FREQ_32000:
+            a2dp->sbc.frequency = SBC_FREQ_32000;
+            u->sample_spec.rate = 32000U;
+            break;
+        case BT_SBC_SAMPLING_FREQ_44100:
+            a2dp->sbc.frequency = SBC_FREQ_44100;
+            u->sample_spec.rate = 44100U;
+            break;
+        case BT_SBC_SAMPLING_FREQ_48000:
+            a2dp->sbc.frequency = SBC_FREQ_48000;
+            u->sample_spec.rate = 48000U;
+            break;
+        default:
+            pa_assert_not_reached();
+    }
+
+    switch (config->channel_mode) {
+        case BT_A2DP_CHANNEL_MODE_MONO:
+            a2dp->sbc.mode = SBC_MODE_MONO;
+            u->sample_spec.channels = 1;
+            break;
+        case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL:
+            a2dp->sbc.mode = SBC_MODE_DUAL_CHANNEL;
+            u->sample_spec.channels = 2;
+            break;
+        case BT_A2DP_CHANNEL_MODE_STEREO:
+            a2dp->sbc.mode = SBC_MODE_STEREO;
+            u->sample_spec.channels = 2;
+            break;
+        case BT_A2DP_CHANNEL_MODE_JOINT_STEREO:
+            a2dp->sbc.mode = SBC_MODE_JOINT_STEREO;
+            u->sample_spec.channels = 2;
+            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->min_bitpool = config->min_bitpool;
+    a2dp->max_bitpool = config->max_bitpool;
+
+    /* Set minimum bitpool for source to get the maximum possible block_size */
+    a2dp->sbc.bitpool = u->profile == PROFILE_A2DP ? a2dp->max_bitpool : a2dp->min_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;
+        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);
+}
+
+/* Run from main thread */
 static int init_bt(struct userdata *u) {
     pa_assert(u);
 
     shutdown_bt(u);
 
-    u->stream_write_type = u->stream_read_type = 0;
-    u->service_write_type = u->service_write_type = 0;
+    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_error("Couldn't connect to bluetooth audio service");
+        pa_log_warn("Bluetooth audio service not available");
         return -1;
     }
 
@@ -1589,9 +2304,32 @@ static int init_bt(struct userdata *u) {
     return 0;
 }
 
+/* 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;
 
@@ -1602,18 +2340,17 @@ static int setup_bt(struct userdata *u) {
 
     pa_log_debug("Connection to the device configured");
 
-#ifdef NOKIA
     if (USE_SCO_OVER_PCM(u)) {
         pa_log_debug("Configured to use SCO over PCM");
         return 0;
     }
-#endif
 
     pa_log_debug("Got the stream socket");
 
     return 0;
 }
 
+/* Run from main thread */
 static int init_profile(struct userdata *u) {
     int r = 0;
     pa_assert(u);
@@ -1623,18 +2360,24 @@ static int init_profile(struct userdata *u) {
         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;
 
     return r;
 }
 
+/* Run from main thread */
 static void stop_thread(struct userdata *u) {
+    char *k;
+
     pa_assert(u);
 
     if (u->thread) {
@@ -1659,11 +2402,23 @@ static void stop_thread(struct userdata *u) {
     }
 
     if (u->sink) {
+        if (u->profile == PROFILE_HSP) {
+            k = pa_sprintf_malloc("bluetooth-device@%p", (void*) u->sink);
+            pa_shared_remove(u->core, k);
+            pa_xfree(k);
+        }
+
         pa_sink_unref(u->sink);
         u->sink = NULL;
     }
 
     if (u->source) {
+        if (u->profile == PROFILE_HSP) {
+            k = pa_sprintf_malloc("bluetooth-device@%p", (void*) u->source);
+            pa_shared_remove(u->core, k);
+            pa_xfree(k);
+        }
+
         pa_source_unref(u->source);
         u->source = NULL;
     }
@@ -1674,8 +2429,14 @@ static void stop_thread(struct userdata *u) {
         pa_rtpoll_free(u->rtpoll);
         u->rtpoll = NULL;
     }
+
+    if (u->read_smoother) {
+        pa_smoother_free(u->read_smoother);
+        u->read_smoother = NULL;
+    }
 }
 
+/* Run from main thread */
 static int start_thread(struct userdata *u) {
     pa_assert(u);
     pa_assert(!u->thread);
@@ -1685,19 +2446,32 @@ static int start_thread(struct userdata *u) {
     u->rtpoll = pa_rtpoll_new();
     pa_thread_mq_init(&u->thread_mq, u->core->mainloop, u->rtpoll);
 
-#ifdef NOKIA
     if (USE_SCO_OVER_PCM(u)) {
-        if (start_stream_fd(u) < 0)
+        if (sco_over_pcm_state_update(u, FALSE) < 0) {
+            char *k;
+
+            if (u->sink) {
+                k = pa_sprintf_malloc("bluetooth-device@%p", (void*) u->sink);
+                pa_shared_remove(u->core, k);
+                pa_xfree(k);
+                u->sink = NULL;
+            }
+            if (u->source) {
+                k = pa_sprintf_malloc("bluetooth-device@%p", (void*) u->source);
+                pa_shared_remove(u->core, k);
+                pa_xfree(k);
+                u->source = NULL;
+            }
             return -1;
+        }
 
         pa_sink_ref(u->sink);
         pa_source_ref(u->source);
         /* FIXME: monitor stream_fd error */
         return 0;
     }
-#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;
@@ -1724,6 +2498,23 @@ static int start_thread(struct userdata *u) {
     return 0;
 }
 
+static void save_sco_volume_callbacks(struct userdata *u) {
+    pa_assert(u);
+    pa_assert(USE_SCO_OVER_PCM(u));
+
+    u->hsp.sco_sink_set_volume = u->hsp.sco_sink->set_volume;
+    u->hsp.sco_source_set_volume = u->hsp.sco_source->set_volume;
+}
+
+static void restore_sco_volume_callbacks(struct userdata *u) {
+    pa_assert(u);
+    pa_assert(USE_SCO_OVER_PCM(u));
+
+    pa_sink_set_set_volume_callback(u->hsp.sco_sink, u->hsp.sco_sink_set_volume);
+    pa_source_set_set_volume_callback(u->hsp.sco_source, u->hsp.sco_source_set_volume);
+}
+
+/* Run from main thread */
 static int card_set_profile(pa_card *c, pa_card_profile *new_profile) {
     struct userdata *u;
     enum profile *d;
@@ -1738,40 +2529,53 @@ static int card_set_profile(pa_card *c, pa_card_profile *new_profile) {
 
     if (!(device = pa_bluetooth_discovery_get_by_path(u->discovery, u->path))) {
         pa_log_error("Failed to get device object.");
-        return -1;
+        return -PA_ERR_IO;
     }
 
-    if (device->headset_state != PA_BT_AUDIO_STATE_CONNECTED && *d == PROFILE_HSP) {
+    /* 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 -1;
+        return -PA_ERR_IO;
     }
-    else if (device->audio_sink_state != PA_BT_AUDIO_STATE_CONNECTED && *d == PROFILE_A2DP) {
+    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 -1;
+        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);
-#ifdef NOKIA
+        inputs = pa_sink_move_all_start(u->sink, NULL);
+
         if (!USE_SCO_OVER_PCM(u))
-#endif
             pa_sink_unlink(u->sink);
     }
 
     if (u->source) {
-        outputs = pa_source_move_all_start(u->source);
-#ifdef NOKIA
+        outputs = pa_source_move_all_start(u->source, NULL);
+
         if (!USE_SCO_OVER_PCM(u))
-#endif
             pa_source_unlink(u->source);
     }
 
     stop_thread(u);
     shutdown_bt(u);
 
+    if (USE_SCO_OVER_PCM(u))
+        restore_sco_volume_callbacks(u);
+
     u->profile = *d;
     u->sample_spec = u->requested_sample_spec;
 
+    if (USE_SCO_OVER_PCM(u))
+        save_sco_volume_callbacks(u);
+
     init_bt(u);
 
     if (u->profile != PROFILE_OFF)
@@ -1797,13 +2601,18 @@ static int card_set_profile(pa_card *c, pa_card_profile *new_profile) {
     return 0;
 }
 
-static int add_card(struct userdata *u, const char *default_profile, const pa_bluetooth_device *device) {
+/* 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;
+
+    pa_assert(u);
+    pa_assert(device);
 
     pa_card_new_data_init(&data);
     data.driver = __FILE__;
@@ -1824,6 +2633,12 @@ static int add_card(struct userdata *u, const char *default_profile, const pa_bl
     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;
+    }
+
     data.profiles = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
 
     /* we base hsp/a2dp availability on UUIDs.
@@ -1844,8 +2659,22 @@ static int add_card(struct userdata *u, const char *default_profile, const pa_bl
         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)) {
+        pa_bluetooth_uuid_has(device->uuids, HFP_HS_UUID)) {
         p = pa_card_profile_new("hsp", _("Telephony Duplex (HSP/HFP)"), sizeof(enum profile));
         p->priority = 20;
         p->n_sinks = 1;
@@ -1859,6 +2688,20 @@ static int add_card(struct userdata *u, const char *default_profile, const pa_bl
         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));
@@ -1866,7 +2709,7 @@ static int add_card(struct userdata *u, const char *default_profile, const pa_bl
     *d = PROFILE_OFF;
     pa_hashmap_put(data.profiles, p->name, p);
 
-    if (default_profile) {
+    if ((default_profile = pa_modargs_get_value(u->modargs, "profile", NULL))) {
         if (pa_hashmap_get(data.profiles, default_profile))
             pa_card_new_data_set_profile(&data, default_profile);
         else
@@ -1884,12 +2727,26 @@ static int add_card(struct userdata *u, const char *default_profile, const pa_bl
     u->card->userdata = u;
     u->card->set_profile = card_set_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->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;
+    }
+
     d = PA_CARD_PROFILE_DATA(u->card->active_profile);
     u->profile = *d;
 
+    if (USE_SCO_OVER_PCM(u))
+        save_sco_volume_callbacks(u);
+
     return 0;
 }
 
+/* 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;
 
@@ -1907,7 +2764,7 @@ static const pa_bluetooth_device* find_device(struct userdata *u, const char *ad
         }
 
         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;
         }
 
@@ -1926,6 +2783,7 @@ static const pa_bluetooth_device* find_device(struct userdata *u, const char *ad
     return d;
 }
 
+/* Run from main thread */
 static int setup_dbus(struct userdata *u) {
     DBusError err;
 
@@ -1948,7 +2806,7 @@ int pa__init(pa_module* m) {
     struct userdata *u;
     const char *address, *path;
     DBusError err;
-    char *mike, *speaker;
+    char *mike, *speaker, *transport;
     const pa_bluetooth_device *device;
 
     pa_assert(m);
@@ -1965,11 +2823,9 @@ int pa__init(pa_module* m) {
     u->core = m->core;
     u->service_fd = -1;
     u->stream_fd = -1;
-    u->read_smoother = pa_smoother_new(PA_USEC_PER_SEC, PA_USEC_PER_SEC*2, TRUE, 10);
     u->sample_spec = m->core->default_sample_spec;
     u->modargs = ma;
 
-#ifdef NOKIA
     if (pa_modargs_get_value(ma, "sco_sink", NULL) &&
         !(u->hsp.sco_sink = pa_namereg_get(m->core, pa_modargs_get_value(ma, "sco_sink", NULL), PA_NAMEREG_SINK))) {
         pa_log("SCO sink not found");
@@ -1981,7 +2837,6 @@ int pa__init(pa_module* m) {
         pa_log("SCO source not found");
         goto fail;
     }
-#endif
 
     if (pa_modargs_get_value_u32(ma, "rate", &u->sample_spec.rate) < 0 ||
         u->sample_spec.rate <= 0 || u->sample_spec.rate > PA_RATE_MAX) {
@@ -1989,6 +2844,12 @@ int pa__init(pa_module* m) {
         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) {
@@ -2011,29 +2872,29 @@ int pa__init(pa_module* m) {
         goto fail;
 
     /* Add the card structure. This will also initialize the default profile */
-    if (add_card(u, pa_modargs_get_value(ma, "profile", NULL), device) < 0)
-        goto fail;
-
-    /* Connect to the BT service and query capabilities */
-    if (init_bt(u) < 0)
+    if (add_card(u, device) < 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);
+    transport = pa_sprintf_malloc("type='signal',sender='org.bluez',interface='org.bluez.MediaTransport',member='PropertyChanged'");
 
     if (pa_dbus_add_matches(
                 pa_dbus_connection_get(u->connection), &err,
                 speaker,
                 mike,
+                transport,
                 NULL) < 0) {
 
         pa_xfree(speaker);
         pa_xfree(mike);
+        pa_xfree(transport);
 
         pa_log("Failed to add D-Bus matches: %s", err.message);
         goto fail;
@@ -2041,6 +2902,10 @@ int pa__init(pa_module* m) {
 
     pa_xfree(speaker);
     pa_xfree(mike);
+    pa_xfree(transport);
+
+    /* Connect to the BT service */
+    init_bt(u);
 
     if (u->profile != PROFILE_OFF)
         if (init_profile(u) < 0)
@@ -2074,27 +2939,23 @@ int pa__get_n_used(pa_module *m) {
 
 void pa__done(pa_module *m) {
     struct userdata *u;
+
     pa_assert(m);
 
     if (!(u = m->userdata))
         return;
 
-    if (u->sink
-#ifdef NOKIA
-        && !USE_SCO_OVER_PCM(u)
-#endif
-    )
+    if (u->sink && !USE_SCO_OVER_PCM(u))
         pa_sink_unlink(u->sink);
 
-    if (u->source
-#ifdef NOKIA
-        && !USE_SCO_OVER_PCM(u)
-#endif
-    )
+    if (u->source && !USE_SCO_OVER_PCM(u))
         pa_source_unlink(u->source);
 
     stop_thread(u);
 
+    if (USE_SCO_OVER_PCM(u))
+        restore_sco_volume_callbacks(u);
+
     if (u->connection) {
 
         if (u->path) {
@@ -2102,16 +2963,15 @@ void pa__done(pa_module *m) {
             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_dbus_remove_matches(pa_dbus_connection_get(u->connection),
-                                   speaker,
-                                   mike,
-                                   NULL);
+            pa_dbus_remove_matches(pa_dbus_connection_get(u->connection), speaker, mike, NULL);
 
             pa_xfree(speaker);
             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);
     }
 
@@ -2134,6 +2994,11 @@ void pa__done(pa_module *m) {
     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);