[pulseaudio-discuss] [PATCH 48/56] bluetooth: Create sink for BlueZ 5 cards

jprvita at gmail.com jprvita at gmail.com
Thu Jul 25 22:12:43 PDT 2013


From: João Paulo Rechi Vita <jprvita at openbossa.org>

---
 src/modules/bluetooth/module-bluez5-device.c | 79 +++++++++++++++++++++++++++-
 1 file changed, 78 insertions(+), 1 deletion(-)

diff --git a/src/modules/bluetooth/module-bluez5-device.c b/src/modules/bluetooth/module-bluez5-device.c
index 3794af1..e2e86e1 100644
--- a/src/modules/bluetooth/module-bluez5-device.c
+++ b/src/modules/bluetooth/module-bluez5-device.c
@@ -72,6 +72,7 @@ struct userdata {
     bool transport_acquired;
 
     pa_card *card;
+    pa_sink *sink;
     pa_bluetooth_profile_t profile;
     char *output_port_name;
     char *input_port_name;
@@ -167,6 +168,39 @@ static const char *pa_bluetooth_form_factor_to_string(pa_bluetooth_form_factor_t
     pa_assert_not_reached();
 }
 
+/* Run from main thread */
+static const char *pa_bluetooth_profile_to_string(enum profile profile) {
+    switch(profile) {
+        case PROFILE_A2DP_SINK:
+            return "a2dp_sink";
+        case PROFILE_A2DP_SOURCE:
+            return "a2dp_source";
+        case PROFILE_OFF:
+            pa_assert_not_reached();
+    }
+
+    return NULL;
+}
+
+/* Run from main thread */
+static void connect_ports(struct userdata *u, void *new_data, pa_direction_t direction) {
+    pa_device_port *port;
+
+    if (direction == PA_DIRECTION_OUTPUT) {
+        pa_sink_new_data *sink_new_data = 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 = 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 transport_acquire(struct userdata *u, bool optional) {
     pa_assert(u->transport);
 
@@ -186,6 +220,45 @@ static int transport_acquire(struct userdata *u, bool optional) {
 }
 
 /* Run from main thread */
+static int add_sink(struct userdata *u) {
+    pa_sink_new_data data;
+
+    pa_assert(u->transport);
+
+    pa_sink_new_data_init(&data);
+    data.module = u->module;
+    data.card = u->card;
+    data.driver = __FILE__;
+    data.name = pa_sprintf_malloc("bluez_sink.%s", u->device->remote);
+    data.namereg_fail = false;
+    pa_proplist_sets(data.proplist, "bluetooth.protocol", pa_bluetooth_profile_to_string(u->profile));
+    pa_sink_new_data_set_sample_spec(&data, &u->sample_spec);
+
+    connect_ports(u, &data, PA_DIRECTION_OUTPUT);
+
+    if (!u->transport_acquired)
+        switch (u->profile) {
+            case PROFILE_A2DP_SINK:
+                /* Profile switch should have failed */
+            case PROFILE_A2DP_SOURCE:
+            case PROFILE_OFF:
+                pa_assert_not_reached();
+                break;
+        }
+
+    u->sink = pa_sink_new(u->core, &data, PA_SINK_HARDWARE|PA_SINK_LATENCY);
+    pa_sink_new_data_done(&data);
+    if (!u->sink) {
+        pa_log_error("Failed to create sink");
+        return -1;
+    }
+
+    u->sink->userdata = u;
+
+    return 0;
+}
+
+/* Run from main thread */
 static void transport_config(struct userdata *u) {
     sbc_info_t *sbc_info = &u->sbc_info;
     a2dp_sbc_t *config;
@@ -332,7 +405,11 @@ static int init_profile(struct userdata *u) {
 
     pa_assert(u->transport);
 
-    /* TODO: add sink/source */
+    if (u->profile == PROFILE_A2DP_SINK)
+        if (add_sink(u) < 0)
+            r = -1;
+
+    /* TODO: add source */
 
     return r;
 }
-- 
1.7.11.7



More information about the pulseaudio-discuss mailing list