From: Andrei Emeltchenko <[email protected]>
The code makes SCO Audio HAL working with Intel device and USB Bluetooth.
Changes:
* v2: Remove connect SCO from Audio HAL, change function to get_fd(),
fixing other comments from review.
Andrei Emeltchenko (21):
android/hal-sco: Use nanosleep for SCO synchronization
android/hal-sco: Move mtu assignment to open_stream()
android/hal-sco: Add SCO packet cache
android/hal-sco: Make use of config parameter
android/hal-sco: Implement open input stream
android/hal-sco: Check file descriptor >= 0
android/hal-sco: Use global sco file descriptor
android/hal-sco: Make debug more readable
android/hal-sco: Fix memory leak
android/hal-sco: Implement read
android/hal-sco: Skip resampling for output stream with 8k
android/hal-sco: Skip resampling for input of 8k
android/hal-sco: Choose buffer size
android/hal-sco: Add stream synchronization
android/hal-sco: Get SCO audio fd on demand
android/hal-sco: Defer SCO connection to write()
android/hal-sco: Fix incorrect assignment
android/hal-audio: Fix leaving open socket
android/hal-sco: Fix leaving open socket
android/hal-sco: Fix error code printing
android/ipc: Rename connect_sco to get_fd
android/hal-audio.c | 12 +-
android/hal-sco.c | 602 ++++++++++++++++++++++++++++++++++++++++++------
android/handsfree.c | 10 +-
android/sco-ipc-api.txt | 6 +-
android/sco-msg.h | 4 +-
5 files changed, 544 insertions(+), 90 deletions(-)
--
1.9.1
Hi Andrei,
On Mon, Jul 21, 2014, Andrei Emeltchenko wrote:
> SCO get connected through handsfree HAL and Audio SCO HAL only need to
> get SCO socket fd.
> ---
> android/hal-sco.c | 14 +++++++-------
> android/handsfree.c | 10 +++++-----
> android/sco-ipc-api.txt | 6 +++---
> android/sco-msg.h | 4 ++--
> 4 files changed, 17 insertions(+), 17 deletions(-)
Applied. Thanks.
Johan
From: Andrei Emeltchenko <[email protected]>
SCO get connected through handsfree HAL and Audio SCO HAL only need to
get SCO socket fd.
---
android/hal-sco.c | 14 +++++++-------
android/handsfree.c | 10 +++++-----
android/sco-ipc-api.txt | 6 +++---
android/sco-msg.h | 4 ++--
4 files changed, 17 insertions(+), 17 deletions(-)
diff --git a/android/hal-sco.c b/android/hal-sco.c
index 55e58b5..c0a1fa6 100644
--- a/android/hal-sco.c
+++ b/android/hal-sco.c
@@ -277,19 +277,19 @@ failed:
return SCO_STATUS_FAILED;
}
-static int ipc_connect_sco(void)
+static int ipc_get_sco_fd(void)
{
int ret = SCO_STATUS_SUCCESS;
pthread_mutex_lock(&sco_mutex);
if (sco_fd < 0) {
- struct sco_rsp_connect rsp;
+ struct sco_rsp_get_fd rsp;
size_t rsp_len = sizeof(rsp);
- DBG("Connecting SCO");
+ DBG("Getting SCO fd");
- ret = sco_ipc_cmd(SCO_SERVICE_ID, SCO_OP_CONNECT, 0, NULL,
+ ret = sco_ipc_cmd(SCO_SERVICE_ID, SCO_OP_GET_FD, 0, NULL,
&rsp_len, &rsp, &sco_fd);
/* Sometimes mtu returned is wrong */
@@ -445,7 +445,7 @@ static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
DBG("write to fd %d bytes %zu", sco_fd, bytes);
- if (ipc_connect_sco() != SCO_STATUS_SUCCESS)
+ if (ipc_get_sco_fd() != SCO_STATUS_SUCCESS)
return -1;
if (!out->downmix_buf) {
@@ -631,7 +631,7 @@ static int sco_open_output_stream(struct audio_hw_device *dev,
return -EIO;
}
- if (ipc_connect_sco() != SCO_STATUS_SUCCESS)
+ if (ipc_get_sco_fd() != SCO_STATUS_SUCCESS)
DBG("SCO is not connected yet; get fd on write()");
out = calloc(1, sizeof(struct sco_stream_out));
@@ -1007,7 +1007,7 @@ static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
DBG("Read from fd %d bytes %zu", sco_fd, bytes);
- if (ipc_connect_sco() != SCO_STATUS_SUCCESS)
+ if (ipc_get_sco_fd() != SCO_STATUS_SUCCESS)
return -1;
if (!in->resampler && in->cfg.rate != AUDIO_STREAM_SCO_RATE) {
diff --git a/android/handsfree.c b/android/handsfree.c
index 599f16f..22d1861 100644
--- a/android/handsfree.c
+++ b/android/handsfree.c
@@ -2565,11 +2565,11 @@ static void disable_sco_server(void)
}
}
-static void bt_sco_connect(const void *buf, uint16_t len)
+static void bt_sco_get_fd(const void *buf, uint16_t len)
{
int fd;
GError *err;
- struct sco_rsp_connect rsp;
+ struct sco_rsp_get_fd rsp;
DBG("");
@@ -2588,7 +2588,7 @@ static void bt_sco_connect(const void *buf, uint16_t len)
DBG("fd %d mtu %u", fd, rsp.mtu);
- ipc_send_rsp_full(sco_ipc, SCO_SERVICE_ID, SCO_OP_CONNECT,
+ ipc_send_rsp_full(sco_ipc, SCO_SERVICE_ID, SCO_OP_GET_FD,
sizeof(rsp), &rsp, fd);
return;
@@ -2598,8 +2598,8 @@ failed:
}
static const struct ipc_handler sco_handlers[] = {
- /* SCO_OP_CONNECT */
- { bt_sco_connect, false, 0 }
+ /* SCO_OP_GET_FD */
+ { bt_sco_get_fd, false, 0 }
};
static void bt_sco_unregister(void)
diff --git a/android/sco-ipc-api.txt b/android/sco-ipc-api.txt
index 05848d2..17372fe 100644
--- a/android/sco-ipc-api.txt
+++ b/android/sco-ipc-api.txt
@@ -20,8 +20,8 @@ The SCO Audio Plugin communicate through abstract socket name
SCO HAL Daemon
----------------------------------------------------
- call connect_sco() --> create SCO socket
- return connect_sco() <-- return socket fd and mtu
+ call get_fd() --> Get SCO socket fd
+ return get_fd() <-- Return SCO socket fd and mtu
SCO Audio Service (ID 0)
========================
@@ -30,7 +30,7 @@ SCO Audio Service (ID 0)
Response parameters: Status (1 octet)
- Opcode 0x01 - Connect SCO command
+ Opcode 0x01 - Get SCO fd command
Command parameters: <none>
Response parameters: MTU (2 octets)
diff --git a/android/sco-msg.h b/android/sco-msg.h
index df0d858..74f25b8 100644
--- a/android/sco-msg.h
+++ b/android/sco-msg.h
@@ -30,7 +30,7 @@ static const char BLUEZ_SCO_SK_PATH[] = "\0bluez_sco_socket";
#define SCO_OP_STATUS IPC_OP_STATUS
-#define SCO_OP_CONNECT 0x01
-struct sco_rsp_connect {
+#define SCO_OP_GET_FD 0x01
+struct sco_rsp_get_fd {
uint16_t mtu;
} __attribute__((packed));
--
1.9.1
Hi Andrei,
On Fri, Jul 18, 2014, Andrei Emeltchenko wrote:
> SCO get connected through handsfree HAL and Audio SCO HAL only need to
> get SCO socket fd.
> ---
> android/hal-sco.c | 4 ++--
> android/handsfree.c | 10 +++++-----
> android/sco-ipc-api.txt | 6 +++---
> android/sco-msg.h | 4 ++--
> 4 files changed, 12 insertions(+), 12 deletions(-)
>
> diff --git a/android/hal-sco.c b/android/hal-sco.c
> index 0e87aad..e07bb9a 100644
> --- a/android/hal-sco.c
> +++ b/android/hal-sco.c
> @@ -285,12 +285,12 @@ static int ipc_connect_sco(void)
Shouldn't you change the name of this ipc_connect_sco function too?
> pthread_mutex_lock(&sco_mutex);
>
> if (sco_fd < 0) {
> - struct sco_rsp_connect rsp;
> + struct sco_rsp_get_fd rsp;
> size_t rsp_len = sizeof(rsp);
>
> DBG("Connecting SCO");
And this debug log?
Johan
From: Andrei Emeltchenko <[email protected]>
SCO get connected through handsfree HAL and Audio SCO HAL only need to
get SCO socket fd.
---
android/hal-sco.c | 4 ++--
android/handsfree.c | 10 +++++-----
android/sco-ipc-api.txt | 6 +++---
android/sco-msg.h | 4 ++--
4 files changed, 12 insertions(+), 12 deletions(-)
diff --git a/android/hal-sco.c b/android/hal-sco.c
index 0e87aad..e07bb9a 100644
--- a/android/hal-sco.c
+++ b/android/hal-sco.c
@@ -285,12 +285,12 @@ static int ipc_connect_sco(void)
pthread_mutex_lock(&sco_mutex);
if (sco_fd < 0) {
- struct sco_rsp_connect rsp;
+ struct sco_rsp_get_fd rsp;
size_t rsp_len = sizeof(rsp);
DBG("Connecting SCO");
- ret = sco_ipc_cmd(SCO_SERVICE_ID, SCO_OP_CONNECT, 0, NULL,
+ ret = sco_ipc_cmd(SCO_SERVICE_ID, SCO_OP_GET_FD, 0, NULL,
&rsp_len, &rsp, &sco_fd);
/* Sometimes mtu returned is wrong */
diff --git a/android/handsfree.c b/android/handsfree.c
index 599f16f..22d1861 100644
--- a/android/handsfree.c
+++ b/android/handsfree.c
@@ -2565,11 +2565,11 @@ static void disable_sco_server(void)
}
}
-static void bt_sco_connect(const void *buf, uint16_t len)
+static void bt_sco_get_fd(const void *buf, uint16_t len)
{
int fd;
GError *err;
- struct sco_rsp_connect rsp;
+ struct sco_rsp_get_fd rsp;
DBG("");
@@ -2588,7 +2588,7 @@ static void bt_sco_connect(const void *buf, uint16_t len)
DBG("fd %d mtu %u", fd, rsp.mtu);
- ipc_send_rsp_full(sco_ipc, SCO_SERVICE_ID, SCO_OP_CONNECT,
+ ipc_send_rsp_full(sco_ipc, SCO_SERVICE_ID, SCO_OP_GET_FD,
sizeof(rsp), &rsp, fd);
return;
@@ -2598,8 +2598,8 @@ failed:
}
static const struct ipc_handler sco_handlers[] = {
- /* SCO_OP_CONNECT */
- { bt_sco_connect, false, 0 }
+ /* SCO_OP_GET_FD */
+ { bt_sco_get_fd, false, 0 }
};
static void bt_sco_unregister(void)
diff --git a/android/sco-ipc-api.txt b/android/sco-ipc-api.txt
index 05848d2..17372fe 100644
--- a/android/sco-ipc-api.txt
+++ b/android/sco-ipc-api.txt
@@ -20,8 +20,8 @@ The SCO Audio Plugin communicate through abstract socket name
SCO HAL Daemon
----------------------------------------------------
- call connect_sco() --> create SCO socket
- return connect_sco() <-- return socket fd and mtu
+ call get_fd() --> Get SCO socket fd
+ return get_fd() <-- Return SCO socket fd and mtu
SCO Audio Service (ID 0)
========================
@@ -30,7 +30,7 @@ SCO Audio Service (ID 0)
Response parameters: Status (1 octet)
- Opcode 0x01 - Connect SCO command
+ Opcode 0x01 - Get SCO fd command
Command parameters: <none>
Response parameters: MTU (2 octets)
diff --git a/android/sco-msg.h b/android/sco-msg.h
index df0d858..74f25b8 100644
--- a/android/sco-msg.h
+++ b/android/sco-msg.h
@@ -30,7 +30,7 @@ static const char BLUEZ_SCO_SK_PATH[] = "\0bluez_sco_socket";
#define SCO_OP_STATUS IPC_OP_STATUS
-#define SCO_OP_CONNECT 0x01
-struct sco_rsp_connect {
+#define SCO_OP_GET_FD 0x01
+struct sco_rsp_get_fd {
uint16_t mtu;
} __attribute__((packed));
--
1.9.1
Hi Andrei,
On Friday 18 of July 2014 12:47:59 Andrei Emeltchenko wrote:
> From: Andrei Emeltchenko <[email protected]>
>
> The code makes SCO Audio HAL working with Intel device and USB Bluetooth.
>
> Changes:
> * v2: Remove connect SCO from Audio HAL, change function to get_fd(),
> fixing other comments from review.
>
> Andrei Emeltchenko (21):
> android/hal-sco: Use nanosleep for SCO synchronization
> android/hal-sco: Move mtu assignment to open_stream()
> android/hal-sco: Add SCO packet cache
> android/hal-sco: Make use of config parameter
> android/hal-sco: Implement open input stream
> android/hal-sco: Check file descriptor >= 0
> android/hal-sco: Use global sco file descriptor
> android/hal-sco: Make debug more readable
> android/hal-sco: Fix memory leak
> android/hal-sco: Implement read
> android/hal-sco: Skip resampling for output stream with 8k
> android/hal-sco: Skip resampling for input of 8k
> android/hal-sco: Choose buffer size
> android/hal-sco: Add stream synchronization
> android/hal-sco: Get SCO audio fd on demand
> android/hal-sco: Defer SCO connection to write()
> android/hal-sco: Fix incorrect assignment
> android/hal-audio: Fix leaving open socket
> android/hal-sco: Fix leaving open socket
> android/hal-sco: Fix error code printing
> android/ipc: Rename connect_sco to get_fd
>
> android/hal-audio.c | 12 +-
> android/hal-sco.c | 602 ++++++++++++++++++++++++++++++++++++++++++------
> android/handsfree.c | 10 +-
> android/sco-ipc-api.txt | 6 +-
> android/sco-msg.h | 4 +-
> 5 files changed, 544 insertions(+), 90 deletions(-)
>
I've pushed all patches except last one which needs to be rebased. Thanks.
--
Best regards,
Szymon Janc
From: Andrei Emeltchenko <[email protected]>
Android may open input/output stream independently so we use global sco
file descriptor and mutexes.
---
android/hal-sco.c | 85 ++++++++++++++++++++++++++++++++-----------------------
1 file changed, 50 insertions(+), 35 deletions(-)
diff --git a/android/hal-sco.c b/android/hal-sco.c
index bcaa820..09fcf5b 100644
--- a/android/hal-sco.c
+++ b/android/hal-sco.c
@@ -46,6 +46,10 @@
static int listen_sk = -1;
static int ipc_sk = -1;
+static int sco_fd = -1;
+static uint16_t sco_mtu = 0;
+static pthread_mutex_t sco_mutex = PTHREAD_MUTEX_INITIALIZER;
+
static pthread_t ipc_th = 0;
static pthread_mutex_t sk_mutex = PTHREAD_MUTEX_INITIALIZER;
@@ -53,7 +57,6 @@ struct sco_audio_config {
uint32_t rate;
uint32_t channels;
uint32_t frame_num;
- uint16_t mtu;
audio_format_t format;
};
@@ -61,7 +64,6 @@ struct sco_stream_out {
struct audio_stream_out stream;
struct sco_audio_config cfg;
- int fd;
uint8_t *downmix_buf;
uint8_t *cache;
@@ -79,7 +81,6 @@ struct sco_stream_in {
struct audio_stream_in stream;
struct sco_audio_config cfg;
- int fd;
};
struct sco_dev {
@@ -257,18 +258,25 @@ failed:
return SCO_STATUS_FAILED;
}
-static int ipc_connect_sco(int *fd, uint16_t *mtu)
+static int ipc_connect_sco(void)
{
struct sco_rsp_connect rsp;
size_t rsp_len = sizeof(rsp);
- int ret;
+ int ret = SCO_STATUS_SUCCESS;
DBG("");
- ret = sco_ipc_cmd(SCO_SERVICE_ID, SCO_OP_CONNECT, 0, NULL, &rsp_len,
- &rsp, fd);
+ pthread_mutex_lock(&sco_mutex);
+
+ if (sco_fd < 0) {
+ ret = sco_ipc_cmd(SCO_SERVICE_ID, SCO_OP_CONNECT, 0, NULL,
+ &rsp_len, &rsp, &sco_fd);
+
+ /* Sometimes mtu returned is wrong */
+ sco_mtu = /* rsp.mtu */ 48;
+ }
- *mtu = rsp.mtu;
+ pthread_mutex_unlock(&sco_mutex);
return ret;
}
@@ -311,28 +319,27 @@ static bool write_data(struct sco_stream_out *out, const uint8_t *buffer,
struct pollfd pfd;
size_t len, written = 0;
int ret;
- uint16_t mtu = out->cfg.mtu;
uint8_t *p;
uint64_t audio_sent_us, audio_passed_us;
- pfd.fd = out->fd;
- pfd.events = POLLOUT | POLLIN | POLLHUP | POLLNVAL;
+ pfd.fd = sco_fd;
+ pfd.events = POLLOUT | POLLHUP | POLLNVAL;
while (bytes > written) {
struct timespec now;
/* poll for sending */
if (poll(&pfd, 1, SOCKET_POLL_TIMEOUT_MS) == 0) {
- DBG("timeout fd %d", out->fd);
+ DBG("timeout fd %d", sco_fd);
return false;
}
if (pfd.revents & (POLLHUP | POLLNVAL)) {
- error("error fd %d, events 0x%x", out->fd, pfd.revents);
+ error("error fd %d, events 0x%x", sco_fd, pfd.revents);
return false;
}
- len = bytes - written > mtu ? mtu : bytes - written;
+ len = bytes - written > sco_mtu ? sco_mtu : bytes - written;
clock_gettime(CLOCK_REALTIME, &now);
/* Mark start of the stream */
@@ -357,10 +364,10 @@ static bool write_data(struct sco_stream_out *out, const uint8_t *buffer,
if (out->cache_len) {
DBG("First packet cache_len %zd", out->cache_len);
memcpy(out->cache + out->cache_len, buffer,
- mtu - out->cache_len);
+ sco_mtu - out->cache_len);
p = out->cache;
} else {
- if (bytes - written >= mtu)
+ if (bytes - written >= sco_mtu)
p = (void *) buffer + written;
else {
memcpy(out->cache, buffer + written,
@@ -373,10 +380,10 @@ static bool write_data(struct sco_stream_out *out, const uint8_t *buffer,
}
}
- ret = write(out->fd, p, len);
+ ret = write(sco_fd, p, len);
if (ret > 0) {
if (out->cache_len) {
- written = mtu - out->cache_len;
+ written = sco_mtu - out->cache_len;
out->cache_len = 0;
} else
written += ret;
@@ -396,7 +403,7 @@ static bool write_data(struct sco_stream_out *out, const uint8_t *buffer,
if (errno != EINTR) {
ret = errno;
- error("write failed (%d) fd %d bytes %zd", ret, out->fd,
+ error("write failed (%d) fd %d bytes %zd", ret, sco_fd,
bytes);
return false;
}
@@ -416,7 +423,7 @@ static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
void *send_buf = out->downmix_buf;
size_t total;
- DBG("write to fd %d bytes %zu", out->fd, bytes);
+ DBG("write to fd %d bytes %zu", sco_fd, bytes);
if (!out->downmix_buf) {
error("sco: downmix buffer not initialized");
@@ -587,19 +594,17 @@ static int sco_open_output_stream(struct audio_hw_device *dev,
{
struct sco_dev *adev = (struct sco_dev *) dev;
struct sco_stream_out *out;
- int fd = -1;
int chan_num, ret;
size_t resample_size;
- uint16_t mtu;
DBG("config %p device flags 0x%02x", config, devices);
- if (ipc_connect_sco(&fd, &mtu) != SCO_STATUS_SUCCESS) {
+ if (ipc_connect_sco() != SCO_STATUS_SUCCESS) {
error("sco: cannot get fd");
return -EIO;
}
- DBG("got sco fd %d mtu %u", fd, mtu);
+ DBG("got sco fd %d mtu %u", sco_fd, sco_mtu);
out = calloc(1, sizeof(struct sco_stream_out));
if (!out)
@@ -638,16 +643,13 @@ static int sco_open_output_stream(struct audio_hw_device *dev,
out->cfg.frame_num = OUT_STREAM_FRAMES;
- /* we get wrong mtu size for some reason */
- out->cfg.mtu = /* mtu */ 48;
-
out->downmix_buf = malloc(out_get_buffer_size(&out->stream.common));
if (!out->downmix_buf) {
free(out);
return -ENOMEM;
}
- out->cache = malloc(out->cfg.mtu);
+ out->cache = malloc(sco_mtu);
if (!out->cache) {
free(out->downmix_buf);
free(out);
@@ -693,7 +695,6 @@ static int sco_open_output_stream(struct audio_hw_device *dev,
*stream_out = &out->stream;
adev->out = out;
- out->fd = fd;
return 0;
failed:
@@ -709,18 +710,30 @@ failed:
return ret;
}
+static void close_sco_socket(void)
+{
+ DBG("");
+
+ pthread_mutex_lock(&sco_mutex);
+
+ if (sco_fd >= 0) {
+ shutdown(sco_fd, SHUT_RDWR);
+ close(sco_fd);
+ sco_fd = -1;
+ }
+
+ pthread_mutex_unlock(&sco_mutex);
+}
+
static void sco_close_output_stream(struct audio_hw_device *dev,
struct audio_stream_out *stream_out)
{
struct sco_dev *sco_dev = (struct sco_dev *) dev;
struct sco_stream_out *out = (struct sco_stream_out *) stream_out;
- DBG("dev %p stream %p fd %d", dev, out, sco_dev->out->fd);
+ DBG("dev %p stream %p fd %d", dev, out, sco_fd);
- if (out && out->fd >= 0) {
- close(out->fd);
- out->fd = -1;
- }
+ close_sco_socket();
if (out->resampler)
release_resampler(out->resampler);
@@ -973,7 +986,9 @@ static void sco_close_input_stream(struct audio_hw_device *dev,
struct sco_dev *sco_dev = (struct sco_dev *) dev;
struct sco_stream_in *in = (struct sco_stream_in *) stream_in;
- DBG("dev %p stream %p fd %d", dev, in, in->fd);
+ DBG("dev %p stream %p fd %d", dev, in, sco_fd);
+
+ close_sco_socket();
free(in);
sco_dev->in = NULL;
--
1.9.1
From: Andrei Emeltchenko <[email protected]>
Do not return error when opening output stream if SCO is not connected
yet, we will check it later with actual out_write().
---
android/hal-sco.c | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/android/hal-sco.c b/android/hal-sco.c
index 1627b83..472c7e8 100644
--- a/android/hal-sco.c
+++ b/android/hal-sco.c
@@ -632,10 +632,8 @@ static int sco_open_output_stream(struct audio_hw_device *dev,
return -EIO;
}
- if (ipc_connect_sco() != SCO_STATUS_SUCCESS) {
- error("sco: cannot get fd");
- return -EIO;
- }
+ if (ipc_connect_sco() != SCO_STATUS_SUCCESS)
+ DBG("SCO is not connected yet; get fd on write()");
out = calloc(1, sizeof(struct sco_stream_out));
if (!out)
--
1.9.1
From: Andrei Emeltchenko <[email protected]>
Add read and resampling from 8000 to 44100.
---
android/hal-sco.c | 151 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 149 insertions(+), 2 deletions(-)
diff --git a/android/hal-sco.c b/android/hal-sco.c
index a3fb710..3b8920a 100644
--- a/android/hal-sco.c
+++ b/android/hal-sco.c
@@ -40,6 +40,7 @@
#define OUT_BUFFER_SIZE 2560
#define OUT_STREAM_FRAMES 2560
+#define IN_STREAM_FRAMES 5292
#define SOCKET_POLL_TIMEOUT_MS 500
@@ -81,6 +82,10 @@ struct sco_stream_in {
struct audio_stream_in stream;
struct sco_audio_config cfg;
+
+ struct resampler_itfe *resampler;
+ int16_t *resample_buf;
+ uint32_t resample_frame_num;
};
struct sco_dev {
@@ -912,12 +917,109 @@ static int in_set_gain(struct audio_stream_in *stream, float gain)
return -ENOSYS;
}
+static bool read_data(struct sco_stream_in *in, char *buffer, size_t bytes)
+{
+ struct pollfd pfd;
+ size_t len, read_bytes = 0;
+
+ pfd.fd = sco_fd;
+ pfd.events = POLLIN | POLLHUP | POLLNVAL;
+
+ while (bytes > read_bytes) {
+ int ret;
+
+ /* poll for reading */
+ if (poll(&pfd, 1, SOCKET_POLL_TIMEOUT_MS) == 0) {
+ DBG("timeout fd %d", sco_fd);
+ return false;
+ }
+
+ if (pfd.revents & (POLLHUP | POLLNVAL)) {
+ error("error fd %d, events 0x%x", sco_fd, pfd.revents);
+ return false;
+ }
+
+ len = bytes - read_bytes > sco_mtu ? sco_mtu :
+ bytes - read_bytes;
+
+ ret = read(sco_fd, buffer + read_bytes, len);
+ if (ret > 0) {
+ read_bytes += ret;
+ DBG("read %d total %zd", ret, read_bytes);
+ continue;
+ }
+
+ if (errno == EAGAIN) {
+ ret = errno;
+ warn("read failed (%d)", ret);
+ continue;
+ }
+
+ if (errno != EINTR) {
+ ret = errno;
+ error("read failed (%d) fd %d bytes %zd", ret, sco_fd,
+ bytes);
+ return false;
+ }
+ }
+
+ DBG("read %zd bytes", read_bytes);
+
+ return true;
+}
+
static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
size_t bytes)
{
- DBG("");
+ struct sco_stream_in *in = (struct sco_stream_in *) stream;
+ size_t frame_size = audio_stream_frame_size(&stream->common);
+ size_t frame_num = bytes / frame_size;
+ size_t input_frame_num = frame_num;
+ void *read_buf = buffer;
+ size_t total, read_frames;
+ int ret;
- return -ENOSYS;
+ DBG("Read from fd %d bytes %zu", sco_fd, bytes);
+
+ if (!in->resampler && in->cfg.rate != AUDIO_STREAM_SCO_RATE) {
+ error("Cannot find resampler");
+ return -1;
+ }
+
+ if (in->resampler) {
+ input_frame_num = get_resample_frame_num(AUDIO_STREAM_SCO_RATE,
+ in->cfg.rate,
+ frame_num, 0);
+ if (input_frame_num > in->resample_frame_num) {
+ DBG("resize input frames from %zd to %d",
+ input_frame_num, in->resample_frame_num);
+ input_frame_num = in->resample_frame_num;
+ }
+
+ read_buf = in->resample_buf;
+ }
+
+ total = input_frame_num * sizeof(int16_t) * 1;
+
+ if(!read_data(in, read_buf, total))
+ return -1;
+
+ read_frames = input_frame_num;
+
+ ret = in->resampler->resample_from_input(in->resampler,
+ in->resample_buf,
+ &read_frames,
+ (int16_t *) buffer,
+ &frame_num);
+ if (ret) {
+ error("Failed to resample frames: %zd input %zd (%s)",
+ frame_num, input_frame_num, strerror(ret));
+ return -1;
+ }
+
+ DBG("resampler: remain %zd output %zd frames", read_frames, frame_num);
+
+ return bytes;
}
static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
@@ -935,6 +1037,8 @@ static int sco_open_input_stream(struct audio_hw_device *dev,
{
struct sco_dev *sco_dev = (struct sco_dev *) dev;
struct sco_stream_in *in;
+ int chan_num, ret;
+ size_t resample_size;
DBG("");
@@ -972,10 +1076,48 @@ static int sco_open_input_stream(struct audio_hw_device *dev,
in->cfg.rate = AUDIO_STREAM_DEFAULT_RATE;
}
+ in->cfg.frame_num = IN_STREAM_FRAMES;
+
+ /* Channel numbers for resampler */
+ chan_num = 1;
+
+ ret = create_resampler(AUDIO_STREAM_SCO_RATE, in->cfg.rate, chan_num,
+ RESAMPLER_QUALITY_DEFAULT, NULL,
+ &in->resampler);
+ if (ret) {
+ error("Failed to create resampler (%s)", strerror(ret));
+ goto failed;
+ }
+
+ in->resample_frame_num = get_resample_frame_num(AUDIO_STREAM_SCO_RATE,
+ in->cfg.rate,
+ in->cfg.frame_num, 0);
+
+ resample_size = sizeof(int16_t) * chan_num * in->resample_frame_num;
+
+ in->resample_buf = malloc(resample_size);
+ if (!in->resample_buf) {
+ error("failed to allocate resample buffer for %d frames",
+ in->resample_frame_num);
+ goto failed;
+ }
+
+ DBG("Resampler: input %d output %d chan %d frames %u size %zd",
+ AUDIO_STREAM_SCO_RATE, in->cfg.rate, chan_num,
+ in->resample_frame_num, resample_size);
+
*stream_in = &in->stream;
sco_dev->in = in;
return 0;
+failed:
+ if (in->resampler)
+ release_resampler(in->resampler);
+ free(in);
+ *stream_in = NULL;
+ sco_dev->in = NULL;
+
+ return ret;
}
static void sco_close_input_stream(struct audio_hw_device *dev,
@@ -988,6 +1130,11 @@ static void sco_close_input_stream(struct audio_hw_device *dev,
close_sco_socket();
+ if (in->resampler) {
+ release_resampler(in->resampler);
+ free(in->resample_buf);
+ }
+
free(in);
sco_dev->in = NULL;
}
--
1.9.1
From: Andrei Emeltchenko <[email protected]>
For 8k choose buffer size 576 which is multiple from 48 and 64.
---
android/hal-sco.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/android/hal-sco.c b/android/hal-sco.c
index 2c1aeed..05dbddb 100644
--- a/android/hal-sco.c
+++ b/android/hal-sco.c
@@ -492,6 +492,10 @@ static size_t out_get_buffer_size(const struct audio_stream *stream)
size_t size = audio_stream_frame_size(&out->stream.common) *
out->cfg.frame_num;
+ /* buffer size without resampling */
+ if (out->cfg.rate == AUDIO_STREAM_SCO_RATE)
+ size = 576 * 2;
+
DBG("buf size %zd", size);
return size;
@@ -838,6 +842,10 @@ static size_t in_get_buffer_size(const struct audio_stream *stream)
size_t size = audio_stream_frame_size(&in->stream.common) *
in->cfg.frame_num;
+ /* buffer size without resampling */
+ if (in->cfg.rate == AUDIO_STREAM_SCO_RATE)
+ size = 576;
+
DBG("buf size %zd", size);
return size;
--
1.9.1
From: Andrei Emeltchenko <[email protected]>
---
android/hal-sco.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/android/hal-sco.c b/android/hal-sco.c
index 905d6fc..bcaa820 100644
--- a/android/hal-sco.c
+++ b/android/hal-sco.c
@@ -715,11 +715,11 @@ static void sco_close_output_stream(struct audio_hw_device *dev,
struct sco_dev *sco_dev = (struct sco_dev *) dev;
struct sco_stream_out *out = (struct sco_stream_out *) stream_out;
- DBG("dev %p stream %p fd %d", dev, stream_out, sco_dev->out->fd);
+ DBG("dev %p stream %p fd %d", dev, out, sco_dev->out->fd);
- if (sco_dev->out && sco_dev->out->fd) {
- close(sco_dev->out->fd);
- sco_dev->out->fd = -1;
+ if (out && out->fd >= 0) {
+ close(out->fd);
+ out->fd = -1;
}
if (out->resampler)
--
1.9.1
From: Andrei Emeltchenko <[email protected]>
Release resampler on exit.
---
android/hal-sco.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/android/hal-sco.c b/android/hal-sco.c
index e084c31..a3fb710 100644
--- a/android/hal-sco.c
+++ b/android/hal-sco.c
@@ -731,8 +731,10 @@ static void sco_close_output_stream(struct audio_hw_device *dev,
close_sco_socket();
- if (out->resampler)
+ if (out->resampler) {
release_resampler(out->resampler);
+ free(out->resample_buf);
+ }
free(out->cache);
free(out->downmix_buf);
--
1.9.1
From: Andrei Emeltchenko <[email protected]>
When getting out of the poll loop we shall close socket always.
---
android/hal-audio.c | 12 +++++-------
1 file changed, 5 insertions(+), 7 deletions(-)
diff --git a/android/hal-audio.c b/android/hal-audio.c
index 1a3d3ae..d7a06fa 100644
--- a/android/hal-audio.c
+++ b/android/hal-audio.c
@@ -1377,14 +1377,12 @@ static void *ipc_handler(void *data)
/* Check if socket is still alive. Empty while loop.*/
while (poll(&pfd, 1, -1) < 0 && errno == EINTR);
- if (pfd.revents & (POLLHUP | POLLERR | POLLNVAL)) {
- info("Audio HAL: Socket closed");
+ info("Audio HAL: Socket closed");
- pthread_mutex_lock(&sk_mutex);
- close(audio_sk);
- audio_sk = -1;
- pthread_mutex_unlock(&sk_mutex);
- }
+ pthread_mutex_lock(&sk_mutex);
+ close(audio_sk);
+ audio_sk = -1;
+ pthread_mutex_unlock(&sk_mutex);
}
/* audio_sk is closed at this point, just cleanup endpoints states */
--
1.9.1
From: Andrei Emeltchenko <[email protected]>
---
android/hal-sco.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/android/hal-sco.c b/android/hal-sco.c
index b6ba55f..0e87aad 100644
--- a/android/hal-sco.c
+++ b/android/hal-sco.c
@@ -697,7 +697,7 @@ static int sco_open_output_stream(struct audio_hw_device *dev,
RESAMPLER_QUALITY_DEFAULT, NULL,
&out->resampler);
if (ret) {
- error("Failed to create resampler (%s)", strerror(ret));
+ error("Failed to create resampler (%s)", strerror(-ret));
goto failed;
}
@@ -1128,7 +1128,7 @@ static int sco_open_input_stream(struct audio_hw_device *dev,
RESAMPLER_QUALITY_DEFAULT, NULL,
&in->resampler);
if (ret) {
- error("Failed to create resampler (%s)", strerror(ret));
+ error("Failed to create resampler (%s)", strerror(-ret));
goto failed;
}
--
1.9.1
From: Andrei Emeltchenko <[email protected]>
---
android/hal-sco.c | 82 ++++++++++++++++++++++++++++++++++++++++---------------
1 file changed, 60 insertions(+), 22 deletions(-)
diff --git a/android/hal-sco.c b/android/hal-sco.c
index 05dbddb..79c13dc 100644
--- a/android/hal-sco.c
+++ b/android/hal-sco.c
@@ -54,6 +54,9 @@ static pthread_mutex_t sco_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_t ipc_th = 0;
static pthread_mutex_t sk_mutex = PTHREAD_MUTEX_INITIALIZER;
+static struct sco_stream_in *sco_stream_in = NULL;
+static struct sco_stream_out *sco_stream_out = NULL;
+
struct sco_audio_config {
uint32_t rate;
uint32_t channels;
@@ -78,6 +81,18 @@ struct sco_stream_out {
uint32_t resample_frame_num;
};
+static void sco_close_socket(void)
+{
+ DBG("sco fd %d", sco_fd);
+
+ if (sco_fd < 0)
+ return;
+
+ shutdown(sco_fd, SHUT_RDWR);
+ close(sco_fd);
+ sco_fd = -1;
+}
+
struct sco_stream_in {
struct audio_stream_in stream;
@@ -430,6 +445,9 @@ static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
DBG("write to fd %d bytes %zu", sco_fd, bytes);
+ if (sco_fd < 0)
+ return -1;
+
if (!out->downmix_buf) {
error("sco: downmix buffer not initialized");
return -1;
@@ -608,17 +626,22 @@ static int sco_open_output_stream(struct audio_hw_device *dev,
DBG("config %p device flags 0x%02x", config, devices);
+ if (sco_stream_out) {
+ DBG("stream_out already open");
+ return -EIO;
+ }
+
if (ipc_connect_sco() != SCO_STATUS_SUCCESS) {
error("sco: cannot get fd");
return -EIO;
}
- DBG("got sco fd %d mtu %u", sco_fd, sco_mtu);
-
out = calloc(1, sizeof(struct sco_stream_out));
if (!out)
return -ENOMEM;
+ DBG("stream %p sco fd %d mtu %u", out, sco_fd, sco_mtu);
+
out->stream.common.get_sample_rate = out_get_sample_rate;
out->stream.common.set_sample_rate = out_set_sample_rate;
out->stream.common.get_buffer_size = out_get_buffer_size;
@@ -703,6 +726,7 @@ static int sco_open_output_stream(struct audio_hw_device *dev,
skip_resampler:
*stream_out = &out->stream;
adev->out = out;
+ sco_stream_out = out;
return 0;
failed:
@@ -714,25 +738,11 @@ failed:
free(out);
stream_out = NULL;
adev->out = NULL;
+ sco_stream_out = NULL;
return ret;
}
-static void close_sco_socket(void)
-{
- DBG("");
-
- pthread_mutex_lock(&sco_mutex);
-
- if (sco_fd >= 0) {
- shutdown(sco_fd, SHUT_RDWR);
- close(sco_fd);
- sco_fd = -1;
- }
-
- pthread_mutex_unlock(&sco_mutex);
-}
-
static void sco_close_output_stream(struct audio_hw_device *dev,
struct audio_stream_out *stream_out)
{
@@ -741,8 +751,6 @@ static void sco_close_output_stream(struct audio_hw_device *dev,
DBG("dev %p stream %p fd %d", dev, out, sco_fd);
- close_sco_socket();
-
if (out->resampler) {
release_resampler(out->resampler);
free(out->resample_buf);
@@ -752,6 +760,15 @@ static void sco_close_output_stream(struct audio_hw_device *dev,
free(out->downmix_buf);
free(out);
sco_dev->out = NULL;
+
+ pthread_mutex_lock(&sco_mutex);
+
+ sco_stream_out = NULL;
+
+ if (!sco_stream_in)
+ sco_close_socket();
+
+ pthread_mutex_unlock(&sco_mutex);
}
static int sco_set_parameters(struct audio_hw_device *dev,
@@ -992,6 +1009,9 @@ static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
DBG("Read from fd %d bytes %zu", sco_fd, bytes);
+ if (sco_fd < 0)
+ return -1;
+
if (!in->resampler && in->cfg.rate != AUDIO_STREAM_SCO_RATE) {
error("Cannot find resampler");
return -1;
@@ -1053,12 +1073,20 @@ static int sco_open_input_stream(struct audio_hw_device *dev,
int chan_num, ret;
size_t resample_size;
- DBG("");
+ DBG("config %p device flags 0x%02x", config, devices);
+
+ if (sco_stream_in) {
+ DBG("stream_in already open");
+ ret = -EIO;
+ goto failed2;
+ }
in = calloc(1, sizeof(struct sco_stream_in));
if (!in)
return -ENOMEM;
+ DBG("stream %p sco fd %d mtu %u", in, sco_fd, sco_mtu);
+
in->stream.common.get_sample_rate = in_get_sample_rate;
in->stream.common.set_sample_rate = in_set_sample_rate;
in->stream.common.get_buffer_size = in_get_buffer_size;
@@ -1124,14 +1152,17 @@ static int sco_open_input_stream(struct audio_hw_device *dev,
skip_resampler:
*stream_in = &in->stream;
sco_dev->in = in;
+ sco_stream_in = in;
return 0;
failed:
if (in->resampler)
release_resampler(in->resampler);
free(in);
+failed2:
*stream_in = NULL;
sco_dev->in = NULL;
+ sco_stream_in = NULL;
return ret;
}
@@ -1144,8 +1175,6 @@ static void sco_close_input_stream(struct audio_hw_device *dev,
DBG("dev %p stream %p fd %d", dev, in, sco_fd);
- close_sco_socket();
-
if (in->resampler) {
release_resampler(in->resampler);
free(in->resample_buf);
@@ -1153,6 +1182,15 @@ static void sco_close_input_stream(struct audio_hw_device *dev,
free(in);
sco_dev->in = NULL;
+
+ pthread_mutex_lock(&sco_mutex);
+
+ sco_stream_in = NULL;
+
+ if (!sco_stream_out)
+ sco_close_socket();
+
+ pthread_mutex_unlock(&sco_mutex);
}
static int sco_dump(const audio_hw_device_t *device, int fd)
--
1.9.1
From: Andrei Emeltchenko <[email protected]>
---
android/hal-sco.c | 56 +++++++++++++++++++++++++++++++++++++++++--------------
1 file changed, 42 insertions(+), 14 deletions(-)
diff --git a/android/hal-sco.c b/android/hal-sco.c
index 5888275..701d15e 100644
--- a/android/hal-sco.c
+++ b/android/hal-sco.c
@@ -64,6 +64,8 @@ struct sco_stream_out {
int fd;
uint8_t *downmix_buf;
+ size_t samples;
+ struct timespec start;
struct resampler_itfe *resampler;
int16_t *resample_buf;
@@ -277,6 +279,21 @@ static void downmix_to_mono(struct sco_stream_out *out, const uint8_t *buffer,
}
}
+static uint64_t timespec_diff_us(struct timespec *a, struct timespec *b)
+{
+ struct timespec res;
+
+ res.tv_sec = a->tv_sec - b->tv_sec;
+ res.tv_nsec = a->tv_nsec - b->tv_nsec;
+
+ if (res.tv_nsec < 0) {
+ res.tv_sec--;
+ res.tv_nsec += 1000000000ll; /* 1sec */
+ }
+
+ return res.tv_sec * 1000000ll + res.tv_nsec / 1000ll;
+}
+
static bool write_data(struct sco_stream_out *out, const uint8_t *buffer,
size_t bytes)
{
@@ -284,13 +301,13 @@ static bool write_data(struct sco_stream_out *out, const uint8_t *buffer,
size_t len, written = 0;
int ret;
uint16_t mtu = /* out->cfg.mtu */ 48;
- uint8_t read_buf[mtu];
- bool do_write = false;
+ uint64_t audio_sent_us, audio_passed_us;
pfd.fd = out->fd;
pfd.events = POLLOUT | POLLIN | POLLHUP | POLLNVAL;
while (bytes > written) {
+ struct timespec now;
/* poll for sending */
if (poll(&pfd, 1, SOCKET_POLL_TIMEOUT_MS) == 0) {
@@ -303,27 +320,38 @@ static bool write_data(struct sco_stream_out *out, const uint8_t *buffer,
return false;
}
- /* FIXME synchronize by time instead of read() */
- if (pfd.revents & POLLIN) {
- ret = read(out->fd, read_buf, mtu);
- if (ret < 0) {
- error("Error reading fd %d (%s)", out->fd,
- strerror(errno));
- return false;
- }
- do_write = true;
+ clock_gettime(CLOCK_REALTIME, &now);
+ /* Mark start of the stream */
+ if (!out->samples)
+ memcpy(&out->start, &now, sizeof(out->start));
+
+ audio_sent_us = out->samples * 1000000ll / AUDIO_STREAM_SCO_RATE;
+ audio_passed_us = timespec_diff_us(&now, &out->start);
+ if ((int) (audio_sent_us - audio_passed_us) > 1500) {
+ struct timespec timeout = {0,
+ (audio_sent_us -
+ audio_passed_us) * 1000};
+ DBG("Sleeping for %d ms",
+ (int) (audio_sent_us - audio_passed_us));
+ nanosleep(&timeout, NULL);
+ } else if ((int)(audio_passed_us - audio_sent_us) > 50000) {
+ DBG("\n\nResync\n\n");
+ out->samples = 0;
+ memcpy(&out->start, &now, sizeof(out->start));
}
- if (!do_write)
- continue;
len = bytes - written > mtu ? mtu : bytes - written;
ret = write(out->fd, buffer + written, len);
if (ret > 0) {
written += ret;
- do_write = false;
+
+ out->samples += ret / 2;
+
+ DBG("written %d samples %zd total %zd bytes",
+ ret, out->samples, written);
continue;
}
--
1.9.1
From: Andrei Emeltchenko <[email protected]>
---
android/hal-sco.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/android/hal-sco.c b/android/hal-sco.c
index 472c7e8..7cbe558 100644
--- a/android/hal-sco.c
+++ b/android/hal-sco.c
@@ -735,7 +735,7 @@ failed:
free(out->cache);
free(out->downmix_buf);
free(out);
- stream_out = NULL;
+ *stream_out = NULL;
adev->out = NULL;
sco_stream_out = NULL;
--
1.9.1
From: Andrei Emeltchenko <[email protected]>
When SCO fd is not known try to get it from the daemon. SCO is
established via handsfree HAL independently from Audio HAL.
---
android/hal-sco.c | 13 +++++++------
1 file changed, 7 insertions(+), 6 deletions(-)
diff --git a/android/hal-sco.c b/android/hal-sco.c
index 79c13dc..1627b83 100644
--- a/android/hal-sco.c
+++ b/android/hal-sco.c
@@ -280,15 +280,16 @@ failed:
static int ipc_connect_sco(void)
{
- struct sco_rsp_connect rsp;
- size_t rsp_len = sizeof(rsp);
int ret = SCO_STATUS_SUCCESS;
- DBG("");
-
pthread_mutex_lock(&sco_mutex);
if (sco_fd < 0) {
+ struct sco_rsp_connect rsp;
+ size_t rsp_len = sizeof(rsp);
+
+ DBG("Connecting SCO");
+
ret = sco_ipc_cmd(SCO_SERVICE_ID, SCO_OP_CONNECT, 0, NULL,
&rsp_len, &rsp, &sco_fd);
@@ -445,7 +446,7 @@ static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
DBG("write to fd %d bytes %zu", sco_fd, bytes);
- if (sco_fd < 0)
+ if (ipc_connect_sco() != SCO_STATUS_SUCCESS)
return -1;
if (!out->downmix_buf) {
@@ -1009,7 +1010,7 @@ static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
DBG("Read from fd %d bytes %zu", sco_fd, bytes);
- if (sco_fd < 0)
+ if (ipc_connect_sco() != SCO_STATUS_SUCCESS)
return -1;
if (!in->resampler && in->cfg.rate != AUDIO_STREAM_SCO_RATE) {
--
1.9.1
From: Andrei Emeltchenko <[email protected]>
SCO get connected through handsfree HAL and Audio SCO HAL only need to
get SCO socket fd.
---
android/hal-sco.c | 4 ++--
android/handsfree.c | 10 +++++-----
android/sco-ipc-api.txt | 6 +++---
android/sco-msg.h | 4 ++--
4 files changed, 12 insertions(+), 12 deletions(-)
diff --git a/android/hal-sco.c b/android/hal-sco.c
index 0e87aad..e07bb9a 100644
--- a/android/hal-sco.c
+++ b/android/hal-sco.c
@@ -285,12 +285,12 @@ static int ipc_connect_sco(void)
pthread_mutex_lock(&sco_mutex);
if (sco_fd < 0) {
- struct sco_rsp_connect rsp;
+ struct sco_rsp_get_fd rsp;
size_t rsp_len = sizeof(rsp);
DBG("Connecting SCO");
- ret = sco_ipc_cmd(SCO_SERVICE_ID, SCO_OP_CONNECT, 0, NULL,
+ ret = sco_ipc_cmd(SCO_SERVICE_ID, SCO_OP_GET_FD, 0, NULL,
&rsp_len, &rsp, &sco_fd);
/* Sometimes mtu returned is wrong */
diff --git a/android/handsfree.c b/android/handsfree.c
index 4f84de2..3a4ac39 100644
--- a/android/handsfree.c
+++ b/android/handsfree.c
@@ -2569,11 +2569,11 @@ static void disable_sco_server(void)
}
}
-static void bt_sco_connect(const void *buf, uint16_t len)
+static void bt_sco_get_fd(const void *buf, uint16_t len)
{
int fd;
GError *err = NULL;
- struct sco_rsp_connect rsp;
+ struct sco_rsp_get_fd rsp;
DBG("");
@@ -2593,7 +2593,7 @@ static void bt_sco_connect(const void *buf, uint16_t len)
DBG("fd %d mtu %u", fd, rsp.mtu);
- ipc_send_rsp_full(sco_ipc, SCO_SERVICE_ID, SCO_OP_CONNECT,
+ ipc_send_rsp_full(sco_ipc, SCO_SERVICE_ID, SCO_OP_GET_FD,
sizeof(rsp), &rsp, fd);
return;
@@ -2603,8 +2603,8 @@ failed:
}
static const struct ipc_handler sco_handlers[] = {
- /* SCO_OP_CONNECT */
- { bt_sco_connect, false, 0 }
+ /* SCO_OP_GET_FD */
+ { bt_sco_get_fd, false, 0 }
};
static void bt_sco_unregister(void)
diff --git a/android/sco-ipc-api.txt b/android/sco-ipc-api.txt
index 05848d2..17372fe 100644
--- a/android/sco-ipc-api.txt
+++ b/android/sco-ipc-api.txt
@@ -20,8 +20,8 @@ The SCO Audio Plugin communicate through abstract socket name
SCO HAL Daemon
----------------------------------------------------
- call connect_sco() --> create SCO socket
- return connect_sco() <-- return socket fd and mtu
+ call get_fd() --> Get SCO socket fd
+ return get_fd() <-- Return SCO socket fd and mtu
SCO Audio Service (ID 0)
========================
@@ -30,7 +30,7 @@ SCO Audio Service (ID 0)
Response parameters: Status (1 octet)
- Opcode 0x01 - Connect SCO command
+ Opcode 0x01 - Get SCO fd command
Command parameters: <none>
Response parameters: MTU (2 octets)
diff --git a/android/sco-msg.h b/android/sco-msg.h
index df0d858..74f25b8 100644
--- a/android/sco-msg.h
+++ b/android/sco-msg.h
@@ -30,7 +30,7 @@ static const char BLUEZ_SCO_SK_PATH[] = "\0bluez_sco_socket";
#define SCO_OP_STATUS IPC_OP_STATUS
-#define SCO_OP_CONNECT 0x01
-struct sco_rsp_connect {
+#define SCO_OP_GET_FD 0x01
+struct sco_rsp_get_fd {
uint16_t mtu;
} __attribute__((packed));
--
1.9.1
From: Andrei Emeltchenko <[email protected]>
Close accepted socket always after poll loop.
---
android/hal-sco.c | 12 +++++-------
1 file changed, 5 insertions(+), 7 deletions(-)
diff --git a/android/hal-sco.c b/android/hal-sco.c
index 7cbe558..b6ba55f 100644
--- a/android/hal-sco.c
+++ b/android/hal-sco.c
@@ -1246,14 +1246,12 @@ static void *ipc_handler(void *data)
/* Check if socket is still alive. Empty while loop.*/
while (poll(&pfd, 1, -1) < 0 && errno == EINTR);
- if (pfd.revents & (POLLHUP | POLLERR | POLLNVAL)) {
- info("SCO HAL: Socket closed");
+ info("SCO HAL: Socket closed");
- pthread_mutex_lock(&sk_mutex);
- close(ipc_sk);
- ipc_sk = -1;
- pthread_mutex_unlock(&sk_mutex);
- }
+ pthread_mutex_lock(&sk_mutex);
+ close(ipc_sk);
+ ipc_sk = -1;
+ pthread_mutex_unlock(&sk_mutex);
}
info("Closing SCO IPC thread");
--
1.9.1
From: Andrei Emeltchenko <[email protected]>
---
android/hal-sco.c | 33 +++++++++++++++++++--------------
1 file changed, 19 insertions(+), 14 deletions(-)
diff --git a/android/hal-sco.c b/android/hal-sco.c
index 97cab84..2c1aeed 100644
--- a/android/hal-sco.c
+++ b/android/hal-sco.c
@@ -979,7 +979,7 @@ static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
size_t frame_num = bytes / frame_size;
size_t input_frame_num = frame_num;
void *read_buf = buffer;
- size_t total, read_frames;
+ size_t total = bytes;
int ret;
DBG("Read from fd %d bytes %zu", sco_fd, bytes);
@@ -1000,27 +1000,29 @@ static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
}
read_buf = in->resample_buf;
- }
- total = input_frame_num * sizeof(int16_t) * 1;
+ total = input_frame_num * sizeof(int16_t) * 1;
+ }
if(!read_data(in, read_buf, total))
return -1;
- read_frames = input_frame_num;
-
- ret = in->resampler->resample_from_input(in->resampler,
+ if (in->resampler) {
+ ret = in->resampler->resample_from_input(in->resampler,
in->resample_buf,
- &read_frames,
+ &input_frame_num,
(int16_t *) buffer,
&frame_num);
- if (ret) {
- error("Failed to resample frames: %zd input %zd (%s)",
- frame_num, input_frame_num, strerror(ret));
- return -1;
- }
+ if (ret) {
+ error("Failed to resample frames: %zd input %zd (%s)",
+ frame_num, input_frame_num,
+ strerror(ret));
+ return -1;
+ }
- DBG("resampler: remain %zd output %zd frames", read_frames, frame_num);
+ DBG("resampler: remain %zd output %zd frames", input_frame_num,
+ frame_num);
+ }
return bytes;
}
@@ -1081,6 +1083,9 @@ static int sco_open_input_stream(struct audio_hw_device *dev,
in->cfg.frame_num = IN_STREAM_FRAMES;
+ if (in->cfg.rate == AUDIO_STREAM_SCO_RATE)
+ goto skip_resampler;
+
/* Channel numbers for resampler */
chan_num = 1;
@@ -1108,7 +1113,7 @@ static int sco_open_input_stream(struct audio_hw_device *dev,
DBG("Resampler: input %d output %d chan %d frames %u size %zd",
AUDIO_STREAM_SCO_RATE, in->cfg.rate, chan_num,
in->resample_frame_num, resample_size);
-
+skip_resampler:
*stream_in = &in->stream;
sco_dev->in = in;
--
1.9.1
From: Andrei Emeltchenko <[email protected]>
---
android/hal-sco.c | 10 +++-------
1 file changed, 3 insertions(+), 7 deletions(-)
diff --git a/android/hal-sco.c b/android/hal-sco.c
index 09fcf5b..e084c31 100644
--- a/android/hal-sco.c
+++ b/android/hal-sco.c
@@ -656,8 +656,6 @@ static int sco_open_output_stream(struct audio_hw_device *dev,
return -ENOMEM;
}
- DBG("size %zd", out_get_buffer_size(&out->stream.common));
-
/* Channel numbers for resampler */
chan_num = 1;
@@ -669,9 +667,6 @@ static int sco_open_output_stream(struct audio_hw_device *dev,
goto failed;
}
- DBG("Created resampler: input rate [%d] output rate [%d] channels [%d]",
- out->cfg.rate, AUDIO_STREAM_SCO_RATE, chan_num);
-
out->resample_frame_num = get_resample_frame_num(AUDIO_STREAM_SCO_RATE,
out->cfg.rate,
out->cfg.frame_num, 1);
@@ -690,8 +685,9 @@ static int sco_open_output_stream(struct audio_hw_device *dev,
goto failed;
}
- DBG("resampler: frame num %u buf size %zd bytes",
- out->resample_frame_num, resample_size);
+ DBG("Resampler: input %d output %d chan %d frames %u size %zd",
+ out->cfg.rate, AUDIO_STREAM_SCO_RATE, chan_num,
+ out->resample_frame_num, resample_size);
*stream_out = &out->stream;
adev->out = out;
--
1.9.1
From: Andrei Emeltchenko <[email protected]>
---
android/hal-sco.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/android/hal-sco.c b/android/hal-sco.c
index 3b8920a..97cab84 100644
--- a/android/hal-sco.c
+++ b/android/hal-sco.c
@@ -661,6 +661,9 @@ static int sco_open_output_stream(struct audio_hw_device *dev,
return -ENOMEM;
}
+ if (out->cfg.rate == AUDIO_STREAM_SCO_RATE)
+ goto skip_resampler;
+
/* Channel numbers for resampler */
chan_num = 1;
@@ -693,7 +696,7 @@ static int sco_open_output_stream(struct audio_hw_device *dev,
DBG("Resampler: input %d output %d chan %d frames %u size %zd",
out->cfg.rate, AUDIO_STREAM_SCO_RATE, chan_num,
out->resample_frame_num, resample_size);
-
+skip_resampler:
*stream_out = &out->stream;
adev->out = out;
--
1.9.1
From: Andrei Emeltchenko <[email protected]>
Use config parameter when opening output stream.
---
android/hal-sco.c | 20 +++++++++++++++-----
1 file changed, 15 insertions(+), 5 deletions(-)
diff --git a/android/hal-sco.c b/android/hal-sco.c
index 7e1a981..e476f84 100644
--- a/android/hal-sco.c
+++ b/android/hal-sco.c
@@ -584,7 +584,7 @@ static int sco_open_output_stream(struct audio_hw_device *dev,
size_t resample_size;
uint16_t mtu;
- DBG("");
+ DBG("config %p device flags 0x%02x", config, devices);
if (ipc_connect_sco(&fd, &mtu) != SCO_STATUS_SUCCESS) {
error("sco: cannot get fd");
@@ -614,10 +614,20 @@ static int sco_open_output_stream(struct audio_hw_device *dev,
out->stream.write = out_write;
out->stream.get_render_position = out_get_render_position;
- /* Configuration for Android */
- out->cfg.format = AUDIO_STREAM_DEFAULT_FORMAT;
- out->cfg.channels = AUDIO_CHANNEL_OUT_STEREO;
- out->cfg.rate = AUDIO_STREAM_DEFAULT_RATE;
+ if (config) {
+ DBG("config: rate %u chan mask %x format %d offload %p",
+ config->sample_rate, config->channel_mask,
+ config->format, &config->offload_info);
+
+ out->cfg.format = config->format;
+ out->cfg.channels = config->channel_mask;
+ out->cfg.rate = config->sample_rate;
+ } else {
+ out->cfg.format = AUDIO_STREAM_DEFAULT_FORMAT;
+ out->cfg.channels = AUDIO_CHANNEL_OUT_STEREO;
+ out->cfg.rate = AUDIO_STREAM_DEFAULT_RATE;
+ }
+
out->cfg.frame_num = OUT_STREAM_FRAMES;
/* we get wrong mtu size for some reason */
--
1.9.1
From: Andrei Emeltchenko <[email protected]>
---
android/hal-sco.c | 175 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 173 insertions(+), 2 deletions(-)
diff --git a/android/hal-sco.c b/android/hal-sco.c
index e476f84..905d6fc 100644
--- a/android/hal-sco.c
+++ b/android/hal-sco.c
@@ -75,9 +75,17 @@ struct sco_stream_out {
uint32_t resample_frame_num;
};
+struct sco_stream_in {
+ struct audio_stream_in stream;
+
+ struct sco_audio_config cfg;
+ int fd;
+};
+
struct sco_dev {
struct audio_hw_device dev;
struct sco_stream_out *out;
+ struct sco_stream_in *in;
};
/*
@@ -789,23 +797,186 @@ static size_t sco_get_input_buffer_size(const struct audio_hw_device *dev,
return -ENOSYS;
}
+static uint32_t in_get_sample_rate(const struct audio_stream *stream)
+{
+ struct sco_stream_in *in = (struct sco_stream_in *) stream;
+
+ DBG("rate %u", in->cfg.rate);
+
+ return in->cfg.rate;
+}
+
+static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
+{
+ DBG("rate %u", rate);
+
+ return 0;
+}
+
+static size_t in_get_buffer_size(const struct audio_stream *stream)
+{
+ struct sco_stream_in *in = (struct sco_stream_in *) stream;
+ size_t size = audio_stream_frame_size(&in->stream.common) *
+ in->cfg.frame_num;
+
+ DBG("buf size %zd", size);
+
+ return size;
+}
+
+static uint32_t in_get_channels(const struct audio_stream *stream)
+{
+ struct sco_stream_in *in = (struct sco_stream_in *) stream;
+
+ DBG("channels num: %u", popcount(in->cfg.channels));
+
+ return in->cfg.channels;
+}
+
+static audio_format_t in_get_format(const struct audio_stream *stream)
+{
+ struct sco_stream_in *in = (struct sco_stream_in *) stream;
+
+ DBG("format: %u", in->cfg.format);
+
+ return in->cfg.format;
+}
+
+static int in_set_format(struct audio_stream *stream, audio_format_t format)
+{
+ DBG("");
+
+ return -ENOSYS;
+}
+
+static int in_standby(struct audio_stream *stream)
+{
+ DBG("");
+
+ return 0;
+}
+
+static int in_dump(const struct audio_stream *stream, int fd)
+{
+ DBG("");
+
+ return -ENOSYS;
+}
+
+static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
+{
+ DBG("%s", kvpairs);
+
+ return 0;
+}
+
+static char *in_get_parameters(const struct audio_stream *stream,
+ const char *keys)
+{
+ DBG("");
+
+ return strdup("");
+}
+
+static int in_add_audio_effect(const struct audio_stream *stream,
+ effect_handle_t effect)
+{
+ DBG("");
+
+ return -ENOSYS;
+}
+
+static int in_remove_audio_effect(const struct audio_stream *stream,
+ effect_handle_t effect)
+{
+ DBG("");
+
+ return -ENOSYS;
+}
+
+static int in_set_gain(struct audio_stream_in *stream, float gain)
+{
+ DBG("");
+
+ return -ENOSYS;
+}
+
+static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
+ size_t bytes)
+{
+ DBG("");
+
+ return -ENOSYS;
+}
+
+static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
+{
+ DBG("");
+
+ return -ENOSYS;
+}
+
static int sco_open_input_stream(struct audio_hw_device *dev,
audio_io_handle_t handle,
audio_devices_t devices,
struct audio_config *config,
struct audio_stream_in **stream_in)
{
+ struct sco_dev *sco_dev = (struct sco_dev *) dev;
+ struct sco_stream_in *in;
+
DBG("");
+ in = calloc(1, sizeof(struct sco_stream_in));
+ if (!in)
+ return -ENOMEM;
+
+ in->stream.common.get_sample_rate = in_get_sample_rate;
+ in->stream.common.set_sample_rate = in_set_sample_rate;
+ in->stream.common.get_buffer_size = in_get_buffer_size;
+ in->stream.common.get_channels = in_get_channels;
+ in->stream.common.get_format = in_get_format;
+ in->stream.common.set_format = in_set_format;
+ in->stream.common.standby = in_standby;
+ in->stream.common.dump = in_dump;
+ in->stream.common.set_parameters = in_set_parameters;
+ in->stream.common.get_parameters = in_get_parameters;
+ in->stream.common.add_audio_effect = in_add_audio_effect;
+ in->stream.common.remove_audio_effect = in_remove_audio_effect;
+ in->stream.set_gain = in_set_gain;
+ in->stream.read = in_read;
+ in->stream.get_input_frames_lost = in_get_input_frames_lost;
+
+ if (config) {
+ DBG("config: rate %u chan mask %x format %d offload %p",
+ config->sample_rate, config->channel_mask,
+ config->format, &config->offload_info);
+
+ in->cfg.format = config->format;
+ in->cfg.channels = config->channel_mask;
+ in->cfg.rate = config->sample_rate;
+ } else {
+ in->cfg.format = AUDIO_STREAM_DEFAULT_FORMAT;
+ in->cfg.channels = AUDIO_CHANNEL_OUT_MONO;
+ in->cfg.rate = AUDIO_STREAM_DEFAULT_RATE;
+ }
+
+ *stream_in = &in->stream;
+ sco_dev->in = in;
+
return 0;
}
static void sco_close_input_stream(struct audio_hw_device *dev,
struct audio_stream_in *stream_in)
{
- DBG("");
+ struct sco_dev *sco_dev = (struct sco_dev *) dev;
+ struct sco_stream_in *in = (struct sco_stream_in *) stream_in;
+
+ DBG("dev %p stream %p fd %d", dev, in, in->fd);
- free(stream_in);
+ free(in);
+ sco_dev->in = NULL;
}
static int sco_dump(const audio_hw_device_t *device, int fd)
--
1.9.1
From: Andrei Emeltchenko <[email protected]>
SCO cached is used when Android writes with packet sizes which cannot
fit to 48 bytes SCO frames. Remaining frames are cached and written next
time Android perform out->write().
---
android/hal-sco.c | 42 ++++++++++++++++++++++++++++++++++++++----
1 file changed, 38 insertions(+), 4 deletions(-)
diff --git a/android/hal-sco.c b/android/hal-sco.c
index ecf7a09..7e1a981 100644
--- a/android/hal-sco.c
+++ b/android/hal-sco.c
@@ -64,6 +64,9 @@ struct sco_stream_out {
int fd;
uint8_t *downmix_buf;
+ uint8_t *cache;
+ size_t cache_len;
+
size_t samples;
struct timespec start;
@@ -301,6 +304,7 @@ static bool write_data(struct sco_stream_out *out, const uint8_t *buffer,
size_t len, written = 0;
int ret;
uint16_t mtu = out->cfg.mtu;
+ uint8_t *p;
uint64_t audio_sent_us, audio_passed_us;
pfd.fd = out->fd;
@@ -320,6 +324,7 @@ static bool write_data(struct sco_stream_out *out, const uint8_t *buffer,
return false;
}
+ len = bytes - written > mtu ? mtu : bytes - written;
clock_gettime(CLOCK_REALTIME, &now);
/* Mark start of the stream */
@@ -341,12 +346,32 @@ static bool write_data(struct sco_stream_out *out, const uint8_t *buffer,
memcpy(&out->start, &now, sizeof(out->start));
}
+ if (out->cache_len) {
+ DBG("First packet cache_len %zd", out->cache_len);
+ memcpy(out->cache + out->cache_len, buffer,
+ mtu - out->cache_len);
+ p = out->cache;
+ } else {
+ if (bytes - written >= mtu)
+ p = (void *) buffer + written;
+ else {
+ memcpy(out->cache, buffer + written,
+ bytes - written);
+ out->cache_len = bytes - written;
+ DBG("Last packet, cache %zd bytes",
+ bytes - written);
+ written += bytes - written;
+ continue;
+ }
+ }
- len = bytes - written > mtu ? mtu : bytes - written;
-
- ret = write(out->fd, buffer + written, len);
+ ret = write(out->fd, p, len);
if (ret > 0) {
- written += ret;
+ if (out->cache_len) {
+ written = mtu - out->cache_len;
+ out->cache_len = 0;
+ } else
+ written += ret;
out->samples += ret / 2;
@@ -604,6 +629,13 @@ static int sco_open_output_stream(struct audio_hw_device *dev,
return -ENOMEM;
}
+ out->cache = malloc(out->cfg.mtu);
+ if (!out->cache) {
+ free(out->downmix_buf);
+ free(out);
+ return -ENOMEM;
+ }
+
DBG("size %zd", out_get_buffer_size(&out->stream.common));
/* Channel numbers for resampler */
@@ -650,6 +682,7 @@ failed:
if (out->resampler)
release_resampler(out->resampler);
+ free(out->cache);
free(out->downmix_buf);
free(out);
stream_out = NULL;
@@ -674,6 +707,7 @@ static void sco_close_output_stream(struct audio_hw_device *dev,
if (out->resampler)
release_resampler(out->resampler);
+ free(out->cache);
free(out->downmix_buf);
free(out);
sco_dev->out = NULL;
--
1.9.1
From: Andrei Emeltchenko <[email protected]>
mtu shall be assigned when opening stream to be logically correct.
---
android/hal-sco.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/android/hal-sco.c b/android/hal-sco.c
index 701d15e..ecf7a09 100644
--- a/android/hal-sco.c
+++ b/android/hal-sco.c
@@ -300,7 +300,7 @@ static bool write_data(struct sco_stream_out *out, const uint8_t *buffer,
struct pollfd pfd;
size_t len, written = 0;
int ret;
- uint16_t mtu = /* out->cfg.mtu */ 48;
+ uint16_t mtu = out->cfg.mtu;
uint64_t audio_sent_us, audio_passed_us;
pfd.fd = out->fd;
@@ -594,7 +594,9 @@ static int sco_open_output_stream(struct audio_hw_device *dev,
out->cfg.channels = AUDIO_CHANNEL_OUT_STEREO;
out->cfg.rate = AUDIO_STREAM_DEFAULT_RATE;
out->cfg.frame_num = OUT_STREAM_FRAMES;
- out->cfg.mtu = mtu;
+
+ /* we get wrong mtu size for some reason */
+ out->cfg.mtu = /* mtu */ 48;
out->downmix_buf = malloc(out_get_buffer_size(&out->stream.common));
if (!out->downmix_buf) {
--
1.9.1