Skip to contentSkip to conversation

android/AVDTP: Make signalling channel priority 6

PATCH Superseded Luiz Augusto von Dentz 2 participants 5 messages Jan 28, 2014 Bluetooth drivers

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Luiz Augusto von Dentz

author maintainer
#0 lore raw

PATCH v2 1/4 android/AVDTP: Make signalling channel priority 6

From: Luiz Augusto von Dentz <[email protected]>

This makes signalling priority 6 so it can push commands before the stream channel, without this the stream channel may be schedule first and cause the signalling commands to timeout while waiting a slot.
---
v2: Return error if writes fails since that probably means the socket has been disconnected, also makes code setting socket to blocking a bit cleaner.

 android/avdtp.c | 9 +++++++++
 1 file changed, 9 insertions(+)
android/avdtp.c +9 -0
index 4abcd75..4cfffc8 100644
@@ -2057,6 +2057,7 @@ struct avdtp *avdtp_new(int fd, size_t imtu, size_t omtu, uint16_t version)
 	struct avdtp *session;
 	GIOCondition cond = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
 	int new_fd;
+	int priority;
 
 	new_fd = dup(fd);
 	if (new_fd < 0) {
@@ -2064,6 +2065,14 @@ struct avdtp *avdtp_new(int fd, size_t imtu, size_t omtu, uint16_t version)
 		return NULL;
 	}
 
+	priority = 6;
+	if (setsockopt(new_fd, SOL_SOCKET, SO_PRIORITY,
+			(const void *) &priority, sizeof(priority)) < 0) {
+		error("setsockopt(SO_PRIORITY): %s (%d)", strerror(errno),
+									errno);
+		return NULL;
+	}
+
 	session = g_new0(struct avdtp, 1);
 	session->io = g_io_channel_unix_new(new_fd);
 	session->version = version;
Signature · 1 line
1.8.4.2
author maintainer
#1 lore raw

PATCH v2 2/4 android/AVDTP: Make stream channel priority 5

From: Luiz Augusto von Dentz <[email protected]>

This makes channel priority 5 so it has higher priority than regular traffic but less than signalling channel.

---
 android/avdtp.c | 9 +++++++++
 1 file changed, 9 insertions(+)
android/avdtp.c +9 -0
index 4cfffc8..23fe519 100644
@@ -2833,10 +2833,19 @@ gboolean avdtp_stream_set_transport(struct avdtp_stream *stream, int fd,
 						size_t imtu, size_t omtu)
 {
 	GIOChannel *io;
+	int priority;
 
 	if (stream != stream->session->pending_open)
 		return FALSE;
 
+	priority = 5;
+	if (setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &priority,
+						sizeof(priority)) < 0) {
+		error("setsockopt(SO_PRIORITY): %s (%d)", strerror(errno),
+									errno);
+		return FALSE;
+	}
+
 	io = g_io_channel_unix_new(fd);
 
 	handle_transport_connect(stream->session, io, imtu, omtu);
Signature · 1 line
1.8.4.2
author maintainer
#2 lore raw

PATCH v2 3/4 android/hal-audio: Fix not handling EINTR errors

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

If the kernel interrupts us while writting just try again.

---
 android/hal-audio.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
android/hal-audio.c +13 -6
index 8d737ad..2ca6289 100644
@@ -399,7 +399,7 @@ static void sbc_resume(void *codec_data)
 	sbc_data->frames_sent = 0;
 }
 
-static void write_media_packet(int fd, struct sbc_data *sbc_data,
+static int write_media_packet(int fd, struct sbc_data *sbc_data,
 				struct media_packet *mp, size_t data_len)
 {
 	struct timespec cur;
@@ -407,10 +407,13 @@ static void write_media_packet(int fd, struct sbc_data *sbc_data,
 	unsigned expected_frames;
 	int ret;
 
-	ret = write(fd, mp, sizeof(*mp) + data_len);
-	if (ret < 0) {
-		int err = errno;
-		error("SBC: failed to write data: %d (%s)", err, strerror(err));
+	while (true) {
+		ret = write(fd, mp, sizeof(*mp) + data_len);
+		if (ret >= 0)
+			break;
+
+		if (errno != EINTR)
+			return -errno;
 	}
 
 	sbc_data->frames_sent += mp->payload.frame_count;
@@ -432,6 +435,8 @@ static void write_media_packet(int fd, struct sbc_data *sbc_data,
 	if (sbc_data->frames_sent >= expected_frames)
 		usleep(sbc_data->frame_duration *
 				mp->payload.frame_count);
+
+	return ret;
 }
 
 static ssize_t sbc_write_data(void *codec_data, const void *buffer,
@@ -474,7 +479,9 @@ static ssize_t sbc_write_data(void *codec_data, const void *buffer,
 		 */
 		if (mp->payload.frame_count == sbc_data->frames_per_packet ||
 				bytes == consumed) {
-			write_media_packet(fd, sbc_data, mp, encoded);
+			ret = write_media_packet(fd, sbc_data, mp, encoded);
+			if (ret < 0)
+				return ret;
 
 			encoded = 0;
 			free_space = sbc_data->out_buf_size - sizeof(*mp);
Signature · 1 line
1.8.4.2
author maintainer
#3 lore raw

PATCH v2 4/4 android/hal-audio: Set stream fd to blocking

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

This makes the stream to block on io operation so it does not return EAGAIN on syscall such as write.

---
 android/hal-audio.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
android/hal-audio.c +13 -2
index 2ca6289..21c1d94 100644
@@ -25,6 +25,7 @@
 #include <sys/un.h>
 #include <unistd.h>
 #include <arpa/inet.h>
+#include <fcntl.h>
 
 #include <hardware/audio.h>
 #include <hardware/hardware.h>
@@ -1121,7 +1122,7 @@ static int audio_open_output_stream(struct audio_hw_device *dev,
 	struct audio_preset *preset;
 	const struct audio_codec *codec;
 	uint16_t mtu;
-	int fd;
+	int fd, flags;
 
 	out = calloc(1, sizeof(struct a2dp_stream_out));
 	if (!out)
@@ -1156,8 +1157,18 @@ static int audio_open_output_stream(struct audio_hw_device *dev,
 	if (!preset || fd < 0)
 		goto fail;
 
-	out->ep->fd = fd;
+	flags = fcntl(fd, F_GETFL, 0);
+	if (flags < 0) {
+		error("fcntl(F_GETFL): %s (%d)", strerror(errno), errno);
+		goto fail;
+	}
 
+	if (fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) < 0) {
+		error("fcntl(F_SETFL): %s (%d)", strerror(errno), errno);
+		goto fail;
+	}
+
+	out->ep->fd = fd;
 	codec = out->ep->codec;
 
 	codec->init(preset, mtu, &out->ep->codec_data);
Signature · 1 line
1.8.4.2
maintainer
#4 lore raw
In reply to Luiz Augusto von Dentz

Hi Luiz,

Quoted 15 lines

This makes signalling priority 6 so it can push commands before the stream channel, without this the stream channel may be schedule first and cause the signalling commands to timeout while waiting a slot.
---
v2: Return error if writes fails since that probably means the socket has been disconnected, also makes code setting socket to blocking a bit cleaner.

android/avdtp.c | 9 +++++++++

1 file changed, 9 insertions(+)
android/avdtp.c +0 -0
index 4abcd75..4cfffc8 100644

android/avdtp.c struct avdtp *avdtp_new(int fd, size_t imtu, size_t omtu, uint16_t version) · line 2060

Quoted 4 lines
android/avdtp.c +1 -0
@@ -2057,6 +2057,7 @@ struct avdtp *avdtp_new(int fd, size_t imtu, size_t omtu, uint16_t version)
 	struct avdtp *session;
 	GIOCondition cond = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
 	int new_fd;
+	int priority;

make it “int new_fd, priority;”.

android/avdtp.c struct avdtp *avdtp_new(int fd, size_t imtu, size_t omtu, uint16_t version) · line 2070

Quoted 10 lines
android/avdtp.c +3 -0
 	new_fd = dup(fd);
 	if (new_fd < 0) {
@@ -2064,6 +2065,14 @@ struct avdtp *avdtp_new(int fd, size_t imtu, size_t omtu, uint16_t version)
 		return NULL;
 	}
 
+	priority = 6;
+	if (setsockopt(new_fd, SOL_SOCKET, SO_PRIORITY,
+			(const void *) &priority, sizeof(priority)) < 0) {

What is this (const void *) cast for. That should not be needed.

android/avdtp.c struct avdtp *avdtp_new(int fd, size_t imtu, size_t omtu, uint16_t version) · line 2078

Quoted 8 lines
android/avdtp.c +5 -0
+		error("setsockopt(SO_PRIORITY): %s (%d)", strerror(errno),
+									errno);
+		return NULL;
+	}
+
 	session = g_new0(struct avdtp, 1);
 	session->io = g_io_channel_unix_new(new_fd);
 	session->version = version;

Regards

Marcel