Skip to contentSkip to conversation

Add support for HandsfreeGateway to Media

PATCH Superseded Frédéric Dalleau 2 participants 13 messages Aug 12, 2011 – Aug 14, 2011 Bluetooth drivers

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

cover Add support for HandsfreeGateway to Media

This patch will enable HandsfreeGateway interface to work with PulseAudio. This is similar to what is done for Headset interface.

Some details about patch 2 :
In an error situation, endpoint_reply calls request->cb which frees endpoint->request and sets it to NULL. But endpoint_reply uses a local variable with a copy of the pointer and tries to free the request again.

Also there is some minor fixes in the pulse audio part still required. I'll send the patch on the PA list.

Frédéric Dalleau (7):
Add missing call to headset_remove_state_cb Fix double free in error case in endpoint_reply Add state change callback to HandsfreeGateway Use state change callback from media interface Update g_strcmp0 to strcasecmp
Add state change callback to audio interface Introduce new state GATEWAY_STATE_PLAY_IN_PROGRESS

 audio/device.c  |   54 +++++++++++++++++++++++++++++++++++++++++++++
 audio/gateway.c |   66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 audio/gateway.h |    8 ++++++
 audio/media.c   |   61 +++++++++++++++++++++++++++++++++++++++++++++++++-
 4 files changed, 187 insertions(+), 2 deletions(-)
#1 lore raw

PATCH 1/7 Add missing call to headset_remove_state_cb

---
 audio/device.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
audio/device.c +3 -0
index 18e873e..f61ef0f 100644
@@ -702,6 +702,9 @@ void audio_device_unregister(struct audio_device *device)
 {
 	unix_device_removed(device);
 
+	if (headset_callback_id != 0)
+		headset_remove_state_cb(headset_callback_id);
+
 	if (device->hs_preauth_id) {
 		g_source_remove(device->hs_preauth_id);
 		device->hs_preauth_id = 0;
Signature · 1 line
1.7.1
#3 lore raw

PATCH 2/7 Fix double free in error case in endpoint_reply

In reply to Frédéric Dalleau
---
 audio/media.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
audio/media.c +2 -1
index 42d8637..2076d04 100644
@@ -264,7 +264,8 @@ done:
 	if (request->cb)
 		request->cb(endpoint, ret, size, request->user_data);
 
-	endpoint_request_free(request);
+	if (endpoint->request)
+		endpoint_request_free(endpoint->request);
 	endpoint->request = NULL;
 }
 
Signature · 1 line
1.7.1
#4 lore raw

PATCH 3/7 Add state change callback to HandsfreeGateway

In reply to Frédéric Dalleau
---
 audio/gateway.c |   61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 audio/gateway.h |    7 ++++++
 2 files changed, 68 insertions(+), 0 deletions(-)
audio/gateway.c +61 -0
index e9485d0..805b8cb 100644
@@ -72,6 +72,14 @@ struct gateway {
 	int version;
 };
 
+struct gateway_state_callback {
+	gateway_state_cb cb;
+	void *user_data;
+	unsigned int id;
+};
+
+static GSList *gateway_callbacks = NULL;
+
 int gateway_close(struct audio_device *device);
 
 static const char *state2str(gateway_state_t state)
@@ -104,16 +112,37 @@ static void change_state(struct audio_device *dev, gateway_state_t new_state)
 {
 	struct gateway *gw = dev->gateway;
 	const char *val;
+	GSList *l;
+	gateway_state_t old_state;
 
 	if (gw->state == new_state)
 		return;
 
 	val = state2str(new_state);
+	old_state = gw->state;
 	gw->state = new_state;
 
 	emit_property_changed(dev->conn, dev->path,
 			AUDIO_GATEWAY_INTERFACE, "State",
 			DBUS_TYPE_STRING, &val);
+
+	for (l = gateway_callbacks; l != NULL; l = l->next) {
+		struct gateway_state_callback *cb = l->data;
+		cb->cb(dev, old_state, new_state, cb->user_data);
+	}
+}
+
+void gateway_set_state(struct audio_device *dev, gateway_state_t new_state)
+{
+	switch (new_state) {
+	case GATEWAY_STATE_DISCONNECTED:
+		gateway_close(dev);
+		break;
+	case GATEWAY_STATE_CONNECTING:
+	case GATEWAY_STATE_CONNECTED:
+	case GATEWAY_STATE_PLAYING:
+		break;
+	}
 }
 
 static void agent_disconnect(struct audio_device *dev, struct hf_agent *agent)
@@ -810,3 +839,35 @@ void gateway_suspend_stream(struct audio_device *dev)
 	gw->sco_start_cb_data = NULL;
 	change_state(dev, GATEWAY_STATE_CONNECTED);
 }
+
+unsigned int gateway_add_state_cb(gateway_state_cb cb, void *user_data)
+{
+	struct gateway_state_callback *state_cb;
+	static unsigned int id = 0;
+
+	state_cb = g_new(struct gateway_state_callback, 1);
+	state_cb->cb = cb;
+	state_cb->user_data = user_data;
+	state_cb->id = ++id;
+
+	gateway_callbacks = g_slist_append(gateway_callbacks, state_cb);
+
+	return state_cb->id;
+}
+
+gboolean gateway_remove_state_cb(unsigned int id)
+{
+	GSList *l;
+
+	for (l = gateway_callbacks; l != NULL; l = l->next) {
+		struct gateway_state_callback *cb = l->data;
+		if (cb && cb->id == id) {
+			gateway_callbacks = g_slist_remove(gateway_callbacks,
+									cb);
+			g_free(cb);
+			return TRUE;
+		}
+	}
+
+	return FALSE;
+}
audio/gateway.h +7 -0
index a45ef82..32b5d6d 100644
@@ -33,9 +33,14 @@ typedef enum {
 	GATEWAY_STATE_PLAYING,
 } gateway_state_t;
 
+typedef void (*gateway_state_cb) (struct audio_device *dev,
+					gateway_state_t old_state,
+					gateway_state_t new_state,
+					void *user_data);
 typedef void (*gateway_stream_cb_t) (struct audio_device *dev, GError *err,
 		void *user_data);
 
+void gateway_set_state(struct audio_device *dev, gateway_state_t new_state);
 void gateway_unregister(struct audio_device *dev);
 struct gateway *gateway_init(struct audio_device *device);
 gboolean gateway_is_connected(struct audio_device *dev);
@@ -49,3 +54,5 @@ int gateway_config_stream(struct audio_device *dev, gateway_stream_cb_t cb,
 gboolean gateway_cancel_stream(struct audio_device *dev, unsigned int id);
 int gateway_get_sco_fd(struct audio_device *dev);
 void gateway_suspend_stream(struct audio_device *dev);
+unsigned int gateway_add_state_cb(gateway_state_cb cb, void *user_data);
+gboolean gateway_remove_state_cb(unsigned int id);
Signature · 1 line
1.7.1
#6 lore raw

PATCH 4/7 Use state change callback from media interface

In reply to Frédéric Dalleau
---
 audio/media.c |   56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 56 insertions(+), 0 deletions(-)
audio/media.c +56 -0
index 2076d04..ae2b471 100644
@@ -43,6 +43,7 @@
 #include "transport.h"
 #include "a2dp.h"
 #include "headset.h"
+#include "gateway.h"
 #include "manager.h"
 
 #ifndef DBUS_TYPE_UNIX_FD
@@ -78,6 +79,7 @@ struct media_endpoint {
 	uint8_t			*capabilities;	/* Endpoint property capabilities */
 	size_t			size;		/* Endpoint capabilities size */
 	guint			hs_watch;
+	guint			ag_watch;
 	guint			watch;
 	struct endpoint_request *request;
 	struct media_transport	*transport;
@@ -118,6 +120,9 @@ static void media_endpoint_destroy(struct media_endpoint *endpoint)
 	if (endpoint->hs_watch)
 		headset_remove_state_cb(endpoint->hs_watch);
 
+	if (endpoint->ag_watch)
+		gateway_remove_state_cb(endpoint->ag_watch);
+
 	if (endpoint->request)
 		media_endpoint_cancel(endpoint);
 
@@ -553,6 +558,46 @@ static void a2dp_destroy_endpoint(void *user_data)
 	release_endpoint(endpoint);
 }
 
+static void gateway_setconf_cb(struct media_endpoint *endpoint, void *ret,
+						int size, void *user_data)
+{
+	struct audio_device *dev = user_data;
+
+	if (ret != NULL)
+		return;
+
+	gateway_set_state(dev, GATEWAY_STATE_DISCONNECTED);
+}
+
+static void gateway_state_changed(struct audio_device *dev,
+					gateway_state_t old_state,
+					gateway_state_t new_state,
+					void *user_data)
+{
+	struct media_endpoint *endpoint = user_data;
+
+	DBG("");
+
+	switch (new_state) {
+	case GATEWAY_STATE_DISCONNECTED:
+		if (endpoint->transport &&
+			media_transport_get_dev(endpoint->transport) == dev) {
+
+			DBG("Clear endpoint %p", endpoint);
+			clear_configuration(endpoint);
+		}
+		break;
+	case GATEWAY_STATE_CONNECTING:
+		set_configuration(endpoint, dev, NULL, 0,
+					gateway_setconf_cb, dev, NULL);
+		break;
+	case GATEWAY_STATE_CONNECTED:
+		break;
+	case GATEWAY_STATE_PLAYING:
+		break;
+	}
+}
+
 static struct media_endpoint *media_endpoint_create(struct media_adapter *adapter,
 						const char *sender,
 						const char *path,
@@ -604,6 +649,17 @@ static struct media_endpoint *media_endpoint_create(struct media_adapter *adapte
 		if (dev)
 			set_configuration(endpoint, dev, NULL, 0,
 						headset_setconf_cb, dev, NULL);
+	} else if (strcasecmp(uuid, HFP_HS_UUID) == 0 ||
+					strcasecmp(uuid, HSP_HS_UUID) == 0) {
+		struct audio_device *dev;
+
+		endpoint->ag_watch = gateway_add_state_cb(gateway_state_changed,
+								endpoint);
+		dev = manager_find_device(NULL, &adapter->src, BDADDR_ANY,
+						AUDIO_GATEWAY_INTERFACE, TRUE);
+		if (dev)
+			set_configuration(endpoint, dev, NULL, 0,
+						gateway_setconf_cb, dev, NULL);
 	} else {
 		if (err)
 			*err = -EINVAL;
Signature · 1 line
1.7.1
#7 lore raw

PATCH 5/7 Update g_strcmp0 to strcasecmp

In reply to Frédéric Dalleau
---
 audio/media.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
audio/media.c +1 -1
index ae2b471..5ab3eab 100644
@@ -639,7 +639,7 @@ static struct media_endpoint *media_endpoint_create(struct media_adapter *adapte
 		if (endpoint->sep == NULL)
 			goto failed;
 	} else if (strcasecmp(uuid, HFP_AG_UUID) == 0 ||
-					g_strcmp0(uuid, HSP_AG_UUID) == 0) {
+					strcasecmp(uuid, HSP_AG_UUID) == 0) {
 		struct audio_device *dev;
 
 		endpoint->hs_watch = headset_add_state_cb(headset_state_changed,
Signature · 1 line
1.7.1
#9 lore raw

PATCH 6/7 Add state change callback to audio interface

In reply to Frédéric Dalleau
---
 audio/device.c |   50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 50 insertions(+), 0 deletions(-)
audio/device.c +50 -0
index f61ef0f..abf8da4 100644
@@ -78,6 +78,7 @@ struct dev_priv {
 	audio_state_t state;
 
 	headset_state_t hs_state;
+	gateway_state_t ag_state;
 	sink_state_t sink_state;
 	avctp_state_t avctp_state;
 	GSList *auths;
@@ -99,6 +100,7 @@ static unsigned int sink_callback_id = 0;
 static unsigned int avctp_callback_id = 0;
 static unsigned int avdtp_callback_id = 0;
 static unsigned int headset_callback_id = 0;
+static unsigned int gateway_callback_id = 0;
 
 static void device_free(struct audio_device *dev)
 {
@@ -510,6 +512,48 @@ static void device_headset_cb(struct audio_device *dev,
 	}
 }
 
+static void device_gateway_cb(struct audio_device *dev,
+				gateway_state_t old_state,
+				gateway_state_t new_state,
+				void *user_data)
+{
+	struct dev_priv *priv = dev->priv;
+
+	if (!dev->gateway)
+		return;
+
+	priv->ag_state = new_state;
+
+	switch (new_state) {
+	case GATEWAY_STATE_DISCONNECTED:
+		if (priv->sink_state != SINK_STATE_DISCONNECTED && dev->sink &&
+				(priv->dc_req || priv->disconnecting)) {
+			sink_shutdown(dev->sink);
+			break;
+		}
+		if (priv->sink_state == SINK_STATE_DISCONNECTED)
+			device_set_state(dev, AUDIO_STATE_DISCONNECTED);
+		else if (old_state == GATEWAY_STATE_CONNECTING &&
+				(priv->sink_state == SINK_STATE_CONNECTED ||
+				priv->sink_state == SINK_STATE_PLAYING))
+			device_set_state(dev, AUDIO_STATE_CONNECTED);
+		break;
+	case GATEWAY_STATE_CONNECTING:
+		if (priv->sink_state == SINK_STATE_DISCONNECTED)
+			device_set_state(dev, AUDIO_STATE_CONNECTING);
+		break;
+	case GATEWAY_STATE_CONNECTED:
+		if (old_state == GATEWAY_STATE_CONNECTED)
+			break;
+		if (priv->sink_state == SINK_STATE_DISCONNECTED ||
+				priv->sink_state == SINK_STATE_CONNECTING)
+			device_set_state(dev, AUDIO_STATE_CONNECTED);
+		break;
+	case GATEWAY_STATE_PLAYING:
+		break;
+	}
+}
+
 static DBusMessage *dev_connect(DBusConnection *conn, DBusMessage *msg,
 								void *data)
 {
@@ -666,6 +710,9 @@ struct audio_device *audio_device_register(DBusConnection *conn,
 	if (headset_callback_id == 0)
 		headset_callback_id = headset_add_state_cb(device_headset_cb,
 									NULL);
+	if (gateway_callback_id == 0)
+		gateway_callback_id = gateway_add_state_cb(device_gateway_cb,
+									NULL);
 
 	return dev;
 }
@@ -705,6 +752,9 @@ void audio_device_unregister(struct audio_device *device)
 	if (headset_callback_id != 0)
 		headset_remove_state_cb(headset_callback_id);
 
+	if (gateway_callback_id != 0)
+		gateway_remove_state_cb(gateway_callback_id);
+
 	if (device->hs_preauth_id) {
 		g_source_remove(device->hs_preauth_id);
 		device->hs_preauth_id = 0;
Signature · 1 line
1.7.1
#11 lore raw

PATCH 7/7 Introduce new state GATEWAY_STATE_PLAY_IN_PROGRESS

In reply to Frédéric Dalleau
---
 audio/device.c  |    1 +
 audio/gateway.c |    5 +++++
 audio/gateway.h |    1 +
 audio/media.c   |    2 +-
 4 files changed, 8 insertions(+), 1 deletions(-)
audio/device.c +1 -0
index abf8da4..667bab0 100644
@@ -549,6 +549,7 @@ static void device_gateway_cb(struct audio_device *dev,
 				priv->sink_state == SINK_STATE_CONNECTING)
 			device_set_state(dev, AUDIO_STATE_CONNECTED);
 		break;
+	case GATEWAY_STATE_PLAY_IN_PROGRESS:
 	case GATEWAY_STATE_PLAYING:
 		break;
 	}
audio/gateway.c +5 -0
index 805b8cb..60f84e0 100644
@@ -90,6 +90,7 @@ static const char *state2str(gateway_state_t state)
 	case GATEWAY_STATE_CONNECTING:
 		return "connecting";
 	case GATEWAY_STATE_CONNECTED:
+	case GATEWAY_STATE_PLAY_IN_PROGRESS:
 		return "connected";
 	case GATEWAY_STATE_PLAYING:
 		return "playing";
@@ -140,6 +141,7 @@ void gateway_set_state(struct audio_device *dev, gateway_state_t new_state)
 		break;
 	case GATEWAY_STATE_CONNECTING:
 	case GATEWAY_STATE_CONNECTED:
+	case GATEWAY_STATE_PLAY_IN_PROGRESS:
 	case GATEWAY_STATE_PLAYING:
 		break;
 	}
@@ -216,6 +218,8 @@ static void sco_connect_cb(GIOChannel *chan, GError *err, gpointer user_data)
 
 	g_io_add_watch(gw->sco, G_IO_ERR | G_IO_HUP | G_IO_NVAL,
 				(GIOFunc) sco_io_cb, dev);
+
+	change_state(dev, GATEWAY_STATE_PLAYING);
 }
 
 static gboolean rfcomm_disconnect_cb(GIOChannel *chan, GIOCondition cond,
@@ -786,6 +790,7 @@ gboolean gateway_request_stream(struct audio_device *dev,
 			g_error_free(err);
 			return FALSE;
 		}
+		change_state(dev, GATEWAY_STATE_PLAY_IN_PROGRESS);
 	} else if (cb)
 		cb(dev, err, user_data);
 
audio/gateway.h +1 -0
index 32b5d6d..80d3dfc 100644
@@ -30,6 +30,7 @@ typedef enum {
 	GATEWAY_STATE_DISCONNECTED,
 	GATEWAY_STATE_CONNECTING,
 	GATEWAY_STATE_CONNECTED,
+	GATEWAY_STATE_PLAY_IN_PROGRESS,
 	GATEWAY_STATE_PLAYING,
 } gateway_state_t;
 
audio/media.c +1 -1
index 5ab3eab..db16b71 100644
@@ -592,7 +592,7 @@ static void gateway_state_changed(struct audio_device *dev,
 					gateway_setconf_cb, dev, NULL);
 		break;
 	case GATEWAY_STATE_CONNECTED:
-		break;
+	case GATEWAY_STATE_PLAY_IN_PROGRESS:
 	case GATEWAY_STATE_PLAYING:
 		break;
 	}
Signature · 1 line
1.7.1
#2 lore raw

Re: [PATCH 1/7] Add missing call to headset_remove_state_cb

In reply to Frédéric Dalleau

Hi Frédéric,

2011/8/12 Frédéric Dalleau <[email protected]>:

Quoted 19 lines

---
 audio/device.c |    3 +++

 1 files changed, 3 insertions(+), 0 deletions(-)
audio/device.c +3 -1
index 18e873e..f61ef0f 100644
@@ -702,6 +702,9 @@ void audio_device_unregister(struct audio_device *device)
  {
        unix_device_removed(device);
 
+       if (headset_callback_id != 0)
+               headset_remove_state_cb(headset_callback_id);
+
        if (device->hs_preauth_id) {
                g_source_remove(device->hs_preauth_id);
                device->hs_preauth_id = 0;
--
Quoted 2 lines

1.7.1

I don't think the callback is per-device, notice that it doesn't take a device pointer, so if you got a problem with that it is basically because the callback needs to check what device it is about (see media.c as a reference)

Signature · 1 line
Luiz Augusto von Dentz
#5 lore raw

Re: [PATCH 3/7] Add state change callback to HandsfreeGateway

In reply to Frédéric Dalleau

Hi Frédéric,

2011/8/12 Frédéric Dalleau <[email protected]>:

Quoted 124 lines

---
 audio/gateway.c |   61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ audio/gateway.h |    7 ++++++

 2 files changed, 68 insertions(+), 0 deletions(-)
audio/gateway.c +61 -0
index e9485d0..805b8cb 100644
@@ -72,6 +72,14 @@ struct gateway {
        int version;
  };
 
+struct gateway_state_callback {
+       gateway_state_cb cb;
+       void *user_data;
+       unsigned int id;
+};
+
+static GSList *gateway_callbacks = NULL;
+
  int gateway_close(struct audio_device *device);
 
  static const char *state2str(gateway_state_t state)
@@ -104,16 +112,37 @@ static void change_state(struct audio_device *dev, gateway_state_t new_state)
  {
        struct gateway *gw = dev->gateway;
        const char *val;
+       GSList *l;
+       gateway_state_t old_state;
 
        if (gw->state == new_state)
                return;
 
        val = state2str(new_state);
+       old_state = gw->state;
        gw->state = new_state;
 
        emit_property_changed(dev->conn, dev->path,
                        AUDIO_GATEWAY_INTERFACE, "State",
                        DBUS_TYPE_STRING, &val);
+
+       for (l = gateway_callbacks; l != NULL; l = l->next) {
+               struct gateway_state_callback *cb = l->data;
+               cb->cb(dev, old_state, new_state, cb->user_data);
+       }
+}
+
+void gateway_set_state(struct audio_device *dev, gateway_state_t new_state)
+{
+       switch (new_state) {
+       case GATEWAY_STATE_DISCONNECTED:
+               gateway_close(dev);
+               break;
+       case GATEWAY_STATE_CONNECTING:
+       case GATEWAY_STATE_CONNECTED:
+       case GATEWAY_STATE_PLAYING:
+               break;
+       }
  }
 
  static void agent_disconnect(struct audio_device *dev, struct hf_agent *agent)
@@ -810,3 +839,35 @@ void gateway_suspend_stream(struct audio_device *dev)
        gw->sco_start_cb_data = NULL;
        change_state(dev, GATEWAY_STATE_CONNECTED);
  }
+
+unsigned int gateway_add_state_cb(gateway_state_cb cb, void *user_data)
+{
+       struct gateway_state_callback *state_cb;
+       static unsigned int id = 0;
+
+       state_cb = g_new(struct gateway_state_callback, 1);
+       state_cb->cb = cb;
+       state_cb->user_data = user_data;
+       state_cb->id = ++id;
+
+       gateway_callbacks = g_slist_append(gateway_callbacks, state_cb);
+
+       return state_cb->id;
+}
+
+gboolean gateway_remove_state_cb(unsigned int id)
+{
+       GSList *l;
+
+       for (l = gateway_callbacks; l != NULL; l = l->next) {
+               struct gateway_state_callback *cb = l->data;
+               if (cb && cb->id == id) {
+                       gateway_callbacks = g_slist_remove(gateway_callbacks,
+                                                                       cb);
+                       g_free(cb);
+                       return TRUE;
+               }
+       }
+
+       return FALSE;
+}
audio/gateway.h +7 -1
index a45ef82..32b5d6d 100644
@@ -33,9 +33,14 @@ typedef enum {
        GATEWAY_STATE_PLAYING,
  } gateway_state_t;
 
+typedef void (*gateway_state_cb) (struct audio_device *dev,
+                                       gateway_state_t old_state,
+                                       gateway_state_t new_state,
+                                       void *user_data);
  typedef void (*gateway_stream_cb_t) (struct audio_device *dev, GError *err,
                void *user_data);
 
+void gateway_set_state(struct audio_device *dev, gateway_state_t new_state);
  void gateway_unregister(struct audio_device *dev);
  struct gateway *gateway_init(struct audio_device *device);
  gboolean gateway_is_connected(struct audio_device *dev);
@@ -49,3 +54,5 @@ int gateway_config_stream(struct audio_device *dev, gateway_stream_cb_t cb,
  gboolean gateway_cancel_stream(struct audio_device *dev, unsigned int id);
  int gateway_get_sco_fd(struct audio_device *dev);
  void gateway_suspend_stream(struct audio_device *dev);
+unsigned int gateway_add_state_cb(gateway_state_cb cb, void *user_data);
+gboolean gateway_remove_state_cb(unsigned int id);
--
Quoted 2 lines

1.7.1

Looks good, so the callbacks are not per-device like we have done for headset.

Signature · 1 line
Luiz Augusto von Dentz
#8 lore raw

Re: [PATCH 5/7] Update g_strcmp0 to strcasecmp

In reply to Frédéric Dalleau

Hi Frédéric,

2011/8/12 Frédéric Dalleau <[email protected]>:

Quoted 18 lines

---
 audio/media.c |    2 +-

 1 files changed, 1 insertions(+), 1 deletions(-)
audio/media.c +1 -2
index ae2b471..5ab3eab 100644
@@ -639,7 +639,7 @@ static struct media_endpoint *media_endpoint_create(struct media_adapter *adapte
                if (endpoint->sep == NULL)
                        goto failed;
        } else if (strcasecmp(uuid, HFP_AG_UUID) == 0 ||
-                                       g_strcmp0(uuid, HSP_AG_UUID) == 0) {
+                                       strcasecmp(uuid, HSP_AG_UUID) == 0) {
                struct audio_device *dev;
 
                endpoint->hs_watch = headset_add_state_cb(headset_state_changed,
--
Quoted 2 lines

1.7.1

Nice catch, ack.

Signature · 1 line
Luiz Augusto von Dentz
#10 lore raw

Re: [PATCH 6/7] Add state change callback to audio interface

In reply to Frédéric Dalleau

Hi Frédéric,

2011/8/12 Frédéric Dalleau <[email protected]>:

Quoted 60 lines

---
 audio/device.c |   50 ++++++++++++++++++++++++++++++++++++++++++++++++++

 1 files changed, 50 insertions(+), 0 deletions(-)
audio/device.c +34 -0
index f61ef0f..abf8da4 100644
@@ -78,6 +78,7 @@ struct dev_priv {
        audio_state_t state;
 
        headset_state_t hs_state;
+       gateway_state_t ag_state;
        sink_state_t sink_state;
        avctp_state_t avctp_state;
        GSList *auths;
@@ -99,6 +100,7 @@ static unsigned int sink_callback_id = 0;
  static unsigned int avctp_callback_id = 0;
  static unsigned int avdtp_callback_id = 0;
  static unsigned int headset_callback_id = 0;
+static unsigned int gateway_callback_id = 0;
 
  static void device_free(struct audio_device *dev)
  {
@@ -510,6 +512,48 @@ static void device_headset_cb(struct audio_device *dev,
        }
  }
 
+static void device_gateway_cb(struct audio_device *dev,
+                               gateway_state_t old_state,
+                               gateway_state_t new_state,
+                               void *user_data)
+{
+       struct dev_priv *priv = dev->priv;
+
+       if (!dev->gateway)
+               return;
+
+       priv->ag_state = new_state;
+
+       switch (new_state) {
+       case GATEWAY_STATE_DISCONNECTED:
+               if (priv->sink_state != SINK_STATE_DISCONNECTED && dev->sink &&
+                               (priv->dc_req || priv->disconnecting)) {
+                       sink_shutdown(dev->sink);
+                       break;
+               }
+               if (priv->sink_state == SINK_STATE_DISCONNECTED)
+                       device_set_state(dev, AUDIO_STATE_DISCONNECTED);
+               else if (old_state == GATEWAY_STATE_CONNECTING &&
+                               (priv->sink_state == SINK_STATE_CONNECTED ||
+                               priv->sink_state == SINK_STATE_PLAYING))
+                       device_set_state(dev, AUDIO_STATE_CONNECTED);
+               break;
+       case GATEWAY_STATE_CONNECTING:
+               if (priv->sink_state == SINK_STATE_DISCONNECTED)
+                       device_set_state(dev, AUDIO_STATE_CONNECTING);
+               break;
+       case GATEWAY_STATE_CONNECTED:
+               if (old_state == GATEWAY_STATE_CONNECTED)

audio/device.c static void device_headset_cb(struct audio_device *dev, · line 556

Quoted 10 lines
audio/device.c +10 -0
+                       break;
+               if (priv->sink_state == SINK_STATE_DISCONNECTED ||
+                               priv->sink_state == SINK_STATE_CONNECTING)
+                       device_set_state(dev, AUDIO_STATE_CONNECTED);
+               break;
+       case GATEWAY_STATE_PLAYING:
+               break;
+       }
+}
+

Hmm, this is a bit trickier, we can be acting as A2DP sink (audio.conf:Enable=Source) in this case, isn't it? In that case you should be checking sounce states not sinks.

Quoted 24 lines
audio/device.c +6 -1
  static DBusMessage *dev_connect(DBusConnection *conn, DBusMessage *msg,
                                                                void *data)
  {
@@ -666,6 +710,9 @@ struct audio_device *audio_device_register(DBusConnection *conn,
        if (headset_callback_id == 0)
                headset_callback_id = headset_add_state_cb(device_headset_cb,
                                                                        NULL);
+       if (gateway_callback_id == 0)
+               gateway_callback_id = gateway_add_state_cb(device_gateway_cb,
+                                                                       NULL);
 
        return dev;
  }
@@ -705,6 +752,9 @@ void audio_device_unregister(struct audio_device *device)
        if (headset_callback_id != 0)
                headset_remove_state_cb(headset_callback_id);
 
+       if (gateway_callback_id != 0)
+               gateway_remove_state_cb(gateway_callback_id);
+
        if (device->hs_preauth_id) {
                g_source_remove(device->hs_preauth_id);
                device->hs_preauth_id = 0;
--
Quoted 1 line

1.7.1

Did you check the white paper about audio profiles? There you can find what is recommended to do when supporting multiple audio profiles, we follow that for HFP-HS+A2DP-Source, but in this case other roles are involved. Btw, you can find the white paper in bluetooth.org.

Signature · 1 line
Luiz Augusto von Dentz
#12 lore raw

Re: [PATCH 7/7] Introduce new state GATEWAY_STATE_PLAY_IN_PROGRESS

In reply to Frédéric Dalleau

Hi Frédéric,

2011/8/12 Frédéric Dalleau <[email protected]>:

Quoted 75 lines

---
 audio/device.c  |    1 +
 audio/gateway.c |    5 +++++
 audio/gateway.h |    1 +
 audio/media.c   |    2 +-

 4 files changed, 8 insertions(+), 1 deletions(-)
audio/device.c +1 -0
index abf8da4..667bab0 100644
@@ -549,6 +549,7 @@ static void device_gateway_cb(struct audio_device *dev,
                                priv->sink_state == SINK_STATE_CONNECTING)
                        device_set_state(dev, AUDIO_STATE_CONNECTED);
                break;
+       case GATEWAY_STATE_PLAY_IN_PROGRESS:

       case GATEWAY_STATE_PLAYING:
               break;
       }

audio/gateway.c +0 -0
index 805b8cb..60f84e0 100644
@@ -90,6 +90,7 @@ static const char *state2str(gateway_state_t state)

       case GATEWAY_STATE_CONNECTING:
               return "connecting";
       case GATEWAY_STATE_CONNECTED:
+       case GATEWAY_STATE_PLAY_IN_PROGRESS:
               return "connected";
       case GATEWAY_STATE_PLAYING:
               return "playing";

audio/gateway.c +0 -0
@@ -140,6 +141,7 @@ void gateway_set_state(struct audio_device *dev, gateway_state_t new_state)
                break;

       case GATEWAY_STATE_CONNECTING:
       case GATEWAY_STATE_CONNECTED:
+       case GATEWAY_STATE_PLAY_IN_PROGRESS:
       case GATEWAY_STATE_PLAYING:
               break;
       }

audio/gateway.c +3 -0
@@ -216,6 +218,8 @@ static void sco_connect_cb(GIOChannel *chan, GError *err, gpointer user_data)
 
        g_io_add_watch(gw->sco, G_IO_ERR | G_IO_HUP | G_IO_NVAL,
                                (GIOFunc) sco_io_cb, dev);
+
+       change_state(dev, GATEWAY_STATE_PLAYING);
  }
 
  static gboolean rfcomm_disconnect_cb(GIOChannel *chan, GIOCondition cond,
@@ -786,6 +790,7 @@ gboolean gateway_request_stream(struct audio_device *dev,
                        g_error_free(err);
                        return FALSE;
                }
+               change_state(dev, GATEWAY_STATE_PLAY_IN_PROGRESS);
        } else if (cb)
                cb(dev, err, user_data);
 
audio/gateway.h +1 -0
index 32b5d6d..80d3dfc 100644
@@ -30,6 +30,7 @@ typedef enum {
        GATEWAY_STATE_DISCONNECTED,
        GATEWAY_STATE_CONNECTING,
        GATEWAY_STATE_CONNECTED,
+       GATEWAY_STATE_PLAY_IN_PROGRESS,
        GATEWAY_STATE_PLAYING,
  } gateway_state_t;
 
audio/media.c +0 -0
index 5ab3eab..db16b71 100644
@@ -592,7 +592,7 @@ static void gateway_state_changed(struct audio_device *dev,
                                        gateway_setconf_cb, dev, NULL);
                break;
Quoted 8 lines

       case GATEWAY_STATE_CONNECTED:
-               break;
+       case GATEWAY_STATE_PLAY_IN_PROGRESS:
       case GATEWAY_STATE_PLAYING:
               break;
       }
--
1.7.1

Looks good, but I would add a description why you are adding this, actually now I realized you didn't have much in the commit messages for the other patches, so while you are at it please improve them.

Signature · 1 line
Luiz Augusto von Dentz