OSDN Git Service

android/hal-audio: Use hal-utils helpers for unaligned access
[android-x86/external-bluetooth-bluez.git] / android / hal-audio.c
index 0a9fb36..e70351e 100644 (file)
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <unistd.h>
+#include <arpa/inet.h>
+#include <fcntl.h>
 
 #include <hardware/audio.h>
 #include <hardware/hardware.h>
 
 #include "audio-msg.h"
+#include "ipc-common.h"
 #include "hal-log.h"
 #include "hal-msg.h"
-#include "../profiles/audio/a2dp-codecs.h"
+#include "hal-audio.h"
+#include "hal-utils.h"
+
+#define FIXED_A2DP_PLAYBACK_LATENCY_MS 25
+
+#define FIXED_BUFFER_SIZE (20 * 512)
+
+#define MAX_DELAY      100000 /* 100ms */
 
 static const uint8_t a2dp_src_uuid[] = {
                0x00, 0x00, 0x11, 0x0a, 0x00, 0x00, 0x10, 0x00,
@@ -39,51 +49,59 @@ static const uint8_t a2dp_src_uuid[] = {
 
 static int listen_sk = -1;
 static int audio_sk = -1;
-static bool close_thread = false;
 
 static pthread_t ipc_th = 0;
-static pthread_mutex_t close_mutex = PTHREAD_MUTEX_INITIALIZER;
 static pthread_mutex_t sk_mutex = PTHREAD_MUTEX_INITIALIZER;
 
-struct audio_input_config {
-       uint32_t rate;
-       uint32_t channels;
-       audio_format_t format;
-};
-
-struct sbc_data {
-       a2dp_sbc_t sbc;
-};
+static void timespec_add(struct timespec *base, uint64_t time_us,
+                                                       struct timespec *res)
+{
+       res->tv_sec = base->tv_sec + time_us / 1000000;
+       res->tv_nsec = base->tv_nsec + (time_us % 1000000) * 1000;
 
-static int sbc_get_presets(struct audio_preset *preset, size_t *len);
-static int sbc_init(struct audio_preset *preset, void **codec_data);
-static int sbc_cleanup(void *codec_data);
-static int sbc_get_config(void *codec_data,
-                                       struct audio_input_config *config);
+       if (res->tv_nsec >= 1000000000) {
+               res->tv_sec++;
+               res->tv_nsec -= 1000000000;
+       }
+}
 
-struct audio_codec {
-       uint8_t type;
+static void timespec_diff(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;
 
-       int (*get_presets) (struct audio_preset *preset, size_t *len);
+       if (res->tv_nsec < 0) {
+               res->tv_sec--;
+               res->tv_nsec += 1000000000; /* 1sec */
+       }
+}
 
-       int (*init) (struct audio_preset *preset, void **codec_data);
-       int (*cleanup) (void *codec_data);
-       int (*get_config) (void *codec_data,
-                                       struct audio_input_config *config);
-       ssize_t (*write_data) (void *codec_data, const void *buffer,
-                               size_t bytes);
-};
+static uint64_t timespec_diff_us(struct timespec *a, struct timespec *b)
+{
+       struct timespec res;
 
-static const struct audio_codec audio_codecs[] = {
-       {
-               .type = A2DP_CODEC_SBC,
+       timespec_diff(a, b, &res);
 
-               .get_presets = sbc_get_presets,
+       return res.tv_sec * 1000000ll + res.tv_nsec / 1000ll;
+}
 
-               .init = sbc_init,
-               .cleanup = sbc_cleanup,
-               .get_config = sbc_get_config,
-       }
+#if defined(ANDROID)
+/*
+ * Bionic does not have clock_nanosleep() prototype in time.h even though
+ * it provides its implementation.
+ */
+extern int clock_nanosleep(clockid_t clock_id, int flags,
+                                       const struct timespec *request,
+                                       struct timespec *remain);
+#endif
+
+static struct {
+       const audio_codec_get_t get_codec;
+       bool loaded;
+} audio_codecs[] = {
+               { .get_codec = codec_aptx, .loaded = false },
+               { .get_codec = codec_sbc, .loaded = false },
 };
 
 #define NUM_CODECS (sizeof(audio_codecs) / sizeof(audio_codecs[0]))
@@ -95,6 +113,15 @@ struct audio_endpoint {
        const struct audio_codec *codec;
        void *codec_data;
        int fd;
+
+       struct media_packet *mp;
+       size_t mp_data_len;
+
+       uint16_t seq;
+       uint32_t samples;
+       struct timespec start;
+
+       bool resync;
 };
 
 static struct audio_endpoint audio_endpoints[MAX_AUDIO_ENDPOINTS];
@@ -112,6 +139,8 @@ struct a2dp_stream_out {
        struct audio_endpoint *ep;
        enum a2dp_state_t audio_state;
        struct audio_input_config cfg;
+
+       uint8_t *downmix_buf;
 };
 
 struct a2dp_audio_dev {
@@ -119,148 +148,19 @@ struct a2dp_audio_dev {
        struct a2dp_stream_out *out;
 };
 
-static const a2dp_sbc_t sbc_presets[] = {
-       {
-               .frequency = SBC_SAMPLING_FREQ_44100 | SBC_SAMPLING_FREQ_48000,
-               .channel_mode = SBC_CHANNEL_MODE_MONO |
-                               SBC_CHANNEL_MODE_DUAL_CHANNEL |
-                               SBC_CHANNEL_MODE_STEREO |
-                               SBC_CHANNEL_MODE_JOINT_STEREO,
-               .subbands = SBC_SUBBANDS_4 | SBC_SUBBANDS_8,
-               .allocation_method = SBC_ALLOCATION_SNR |
-                                       SBC_ALLOCATION_LOUDNESS,
-               .block_length = SBC_BLOCK_LENGTH_4 | SBC_BLOCK_LENGTH_8 |
-                               SBC_BLOCK_LENGTH_12 | SBC_BLOCK_LENGTH_16,
-               .min_bitpool = MIN_BITPOOL,
-               .max_bitpool = MAX_BITPOOL
-       },
-       {
-               .frequency = SBC_SAMPLING_FREQ_44100,
-               .channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO,
-               .subbands = SBC_SUBBANDS_8,
-               .allocation_method = SBC_ALLOCATION_LOUDNESS,
-               .block_length = SBC_BLOCK_LENGTH_16,
-               .min_bitpool = MIN_BITPOOL,
-               .max_bitpool = MAX_BITPOOL
-       },
-       {
-               .frequency = SBC_SAMPLING_FREQ_48000,
-               .channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO,
-               .subbands = SBC_SUBBANDS_8,
-               .allocation_method = SBC_ALLOCATION_LOUDNESS,
-               .block_length = SBC_BLOCK_LENGTH_16,
-               .min_bitpool = MIN_BITPOOL,
-               .max_bitpool = MAX_BITPOOL
-       },
-};
-
-static int sbc_get_presets(struct audio_preset *preset, size_t *len)
-{
-       int i;
-       int count;
-       size_t new_len = 0;
-       uint8_t *ptr = (uint8_t *) preset;
-       size_t preset_size = sizeof(*preset) + sizeof(a2dp_sbc_t);
-
-       DBG("");
-
-       count = sizeof(sbc_presets) / sizeof(sbc_presets[0]);
-
-       for (i = 0; i < count; i++) {
-               preset = (struct audio_preset *) ptr;
-
-               if (new_len + preset_size > *len)
-                       break;
-
-               preset->len = sizeof(a2dp_sbc_t);
-               memcpy(preset->data, &sbc_presets[i], preset->len);
-
-               new_len += preset_size;
-               ptr += preset_size;
-       }
-
-       *len = new_len;
-
-       return i;
-}
-
-static int sbc_init(struct audio_preset *preset, void **codec_data)
-{
-       struct sbc_data *sbc_data;
-
-       DBG("");
-
-       if (preset->len != sizeof(a2dp_sbc_t)) {
-               DBG("preset size mismatch");
-               return AUDIO_STATUS_FAILED;
-       }
-
-       sbc_data = calloc(sizeof(struct sbc_data), 1);
-
-       memcpy(&sbc_data->sbc, preset->data, preset->len);
-
-       *codec_data = sbc_data;
-
-       return AUDIO_STATUS_SUCCESS;
-}
-
-static int sbc_cleanup(void *codec_data)
-{
-       DBG("");
-
-       free(codec_data);
-
-       return AUDIO_STATUS_SUCCESS;
-}
-
-static int sbc_get_config(void *codec_data,
-                                       struct audio_input_config *config)
-{
-       struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
-
-       switch (sbc_data->sbc.frequency) {
-       case SBC_SAMPLING_FREQ_16000:
-               config->rate = 16000;
-               break;
-       case SBC_SAMPLING_FREQ_32000:
-               config->rate = 32000;
-               break;
-       case SBC_SAMPLING_FREQ_44100:
-               config->rate = 44100;
-               break;
-       case SBC_SAMPLING_FREQ_48000:
-               config->rate = 48000;
-               break;
-       default:
-               return AUDIO_STATUS_FAILED;
-       }
-       config->channels = sbc_data->sbc.channel_mode == SBC_CHANNEL_MODE_MONO ?
-                               AUDIO_CHANNEL_OUT_MONO :
-                               AUDIO_CHANNEL_OUT_STEREO;
-       config->format = AUDIO_FORMAT_PCM_16_BIT;
-
-       return AUDIO_STATUS_SUCCESS;
-}
-
-static void audio_ipc_cleanup(void)
-{
-       if (audio_sk >= 0) {
-               shutdown(audio_sk, SHUT_RDWR);
-               audio_sk = -1;
-       }
-}
-
 static int audio_ipc_cmd(uint8_t service_id, uint8_t opcode, uint16_t len,
                        void *param, size_t *rsp_len, void *rsp, int *fd)
 {
        ssize_t ret;
        struct msghdr msg;
        struct iovec iv[2];
-       struct hal_hdr cmd;
+       struct ipc_hdr cmd;
        char cmsgbuf[CMSG_SPACE(sizeof(int))];
-       struct hal_status s;
+       struct ipc_status s;
        size_t s_len = sizeof(s);
 
+       pthread_mutex_lock(&sk_mutex);
+
        if (audio_sk < 0) {
                error("audio: Invalid cmd socket passed to audio_ipc_cmd");
                goto failed;
@@ -288,12 +188,9 @@ static int audio_ipc_cmd(uint8_t service_id, uint8_t opcode, uint16_t len,
        msg.msg_iov = iv;
        msg.msg_iovlen = 2;
 
-       pthread_mutex_lock(&sk_mutex);
-
        ret = sendmsg(audio_sk, &msg, 0);
        if (ret < 0) {
                error("audio: Sending command failed:%s", strerror(errno));
-               pthread_mutex_unlock(&sk_mutex);
                goto failed;
        }
 
@@ -325,12 +222,9 @@ static int audio_ipc_cmd(uint8_t service_id, uint8_t opcode, uint16_t len,
        if (ret < 0) {
                error("audio: Receiving command response failed:%s",
                                                        strerror(errno));
-               pthread_mutex_unlock(&sk_mutex);
                goto failed;
        }
 
-       pthread_mutex_unlock(&sk_mutex);
-
        if (ret < (ssize_t) sizeof(cmd)) {
                error("audio: Too small response received(%zd bytes)", ret);
                goto failed;
@@ -354,7 +248,7 @@ static int audio_ipc_cmd(uint8_t service_id, uint8_t opcode, uint16_t len,
        }
 
        if (cmd.opcode == AUDIO_OP_STATUS) {
-               struct hal_status *s = rsp;
+               struct ipc_status *s = rsp;
 
                if (sizeof(*s) != cmd.len) {
                        error("audio: Invalid status length");
@@ -366,9 +260,13 @@ static int audio_ipc_cmd(uint8_t service_id, uint8_t opcode, uint16_t len,
                        goto failed;
                }
 
+               pthread_mutex_unlock(&sk_mutex);
+
                return s->code;
        }
 
+       pthread_mutex_unlock(&sk_mutex);
+
        /* Receive auxiliary data in msg */
        if (fd) {
                struct cmsghdr *cmsg;
@@ -383,17 +281,20 @@ static int audio_ipc_cmd(uint8_t service_id, uint8_t opcode, uint16_t len,
                                break;
                        }
                }
+
+               if (*fd < 0)
+                       goto failed;
        }
 
-       if (rsp_len)
-               *rsp_len = cmd.len;
+       *rsp_len = cmd.len;
 
        return AUDIO_STATUS_SUCCESS;
 
 failed:
        /* Some serious issue happen on IPC - recover */
        shutdown(audio_sk, SHUT_RDWR);
-       audio_sk = -1;
+       pthread_mutex_unlock(&sk_mutex);
+
        return AUDIO_STATUS_FAILED;
 }
 
@@ -439,8 +340,8 @@ static int ipc_close_cmd(uint8_t endpoint_id)
        return result;
 }
 
-static int ipc_open_stream_cmd(uint8_t endpoint_id,
-                                       struct audio_preset **caps)
+static int ipc_open_stream_cmd(uint8_t *endpoint_id, uint16_t *mtu, int *fd,
+                                               struct audio_preset **caps)
 {
        char buf[BLUEZ_AUDIO_MTU];
        struct audio_cmd_open_stream cmd;
@@ -454,14 +355,15 @@ static int ipc_open_stream_cmd(uint8_t endpoint_id,
        if (!caps)
                return AUDIO_STATUS_FAILED;
 
-       cmd.id = endpoint_id;
+       cmd.id = *endpoint_id;
 
        result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_OPEN_STREAM,
-                               sizeof(cmd), &cmd, &rsp_len, rsp, NULL);
-
+                               sizeof(cmd), &cmd, &rsp_len, rsp, fd);
        if (result == AUDIO_STATUS_SUCCESS) {
                size_t buf_len = sizeof(struct audio_preset) +
                                        rsp->preset[0].len;
+               *endpoint_id = rsp->id;
+               *mtu = rsp->mtu;
                *caps = malloc(buf_len);
                memcpy(*caps, &rsp->preset, buf_len);
        } else {
@@ -481,7 +383,7 @@ static int ipc_close_stream_cmd(uint8_t endpoint_id)
        cmd.id = endpoint_id;
 
        result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_CLOSE_STREAM,
-                               sizeof(cmd), &cmd, NULL, NULL, NULL);
+                                       sizeof(cmd), &cmd, NULL, NULL, NULL);
 
        return result;
 }
@@ -496,7 +398,7 @@ static int ipc_resume_stream_cmd(uint8_t endpoint_id)
        cmd.id = endpoint_id;
 
        result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_RESUME_STREAM,
-                               sizeof(cmd), &cmd, NULL, NULL, NULL);
+                                       sizeof(cmd), &cmd, NULL, NULL, NULL);
 
        return result;
 }
@@ -511,30 +413,58 @@ static int ipc_suspend_stream_cmd(uint8_t endpoint_id)
        cmd.id = endpoint_id;
 
        result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_SUSPEND_STREAM,
-                               sizeof(cmd), &cmd, NULL, NULL, NULL);
+                                       sizeof(cmd), &cmd, NULL, NULL, NULL);
 
        return result;
 }
 
+struct register_state {
+       struct audio_endpoint *ep;
+       bool error;
+};
+
+static void register_endpoint(const struct audio_codec *codec,
+                                               struct register_state *state)
+{
+       struct audio_endpoint *ep = state->ep;
+
+       /* don't even try to register more endpoints if one failed */
+       if (state->error)
+               return;
+
+       ep->id = ipc_open_cmd(codec);
+
+       if (!ep->id) {
+               state->error = true;
+               error("Failed to register endpoint");
+               return;
+       }
+
+       ep->codec = codec;
+       ep->codec_data = NULL;
+       ep->fd = -1;
+
+       state->ep++;
+}
+
 static int register_endpoints(void)
 {
-       struct audio_endpoint *ep = &audio_endpoints[0];
-       size_t i;
+       struct register_state state;
+       unsigned int i;
 
-       for (i = 0; i < NUM_CODECS; i++, ep++) {
-               const struct audio_codec *codec = &audio_codecs[i];
+       state.ep = &audio_endpoints[0];
+       state.error = false;
 
-               ep->id = ipc_open_cmd(codec);
+       for (i = 0; i < NUM_CODECS; i++) {
+               const struct audio_codec *codec = audio_codecs[i].get_codec();
 
-               if (!ep->id)
-                       return AUDIO_STATUS_FAILED;
+               if (!audio_codecs[i].loaded)
+                       continue;
 
-               ep->codec = codec;
-               ep->codec_data = NULL;
-               ep->fd = -1;
+               register_endpoint(codec, &state);
        }
 
-       return AUDIO_STATUS_SUCCESS;
+       return state.error ? AUDIO_STATUS_FAILED : AUDIO_STATUS_SUCCESS;
 }
 
 static void unregister_endpoints(void)
@@ -551,27 +481,349 @@ static void unregister_endpoints(void)
        }
 }
 
+static bool open_endpoint(struct audio_endpoint **epp,
+                                               struct audio_input_config *cfg)
+{
+       struct audio_preset *preset;
+       struct audio_endpoint *ep = *epp;
+       const struct audio_codec *codec;
+       uint16_t mtu;
+       uint16_t payload_len;
+       int fd;
+       size_t i;
+       uint8_t ep_id = 0;
+
+       if (ep)
+               ep_id = ep->id;
+
+       if (ipc_open_stream_cmd(&ep_id, &mtu, &fd, &preset) !=
+                                                       AUDIO_STATUS_SUCCESS)
+               return false;
+
+       DBG("ep_id=%d mtu=%u", ep_id, mtu);
+
+       for (i = 0; i < MAX_AUDIO_ENDPOINTS; i++)
+               if (audio_endpoints[i].id == ep_id) {
+                       ep = &audio_endpoints[i];
+                       break;
+               }
+
+       if (!ep) {
+               error("Cound not find opened endpoint");
+               goto failed;
+       }
+
+       *epp = ep;
+
+       payload_len = mtu;
+       if (ep->codec->use_rtp)
+               payload_len -= sizeof(struct rtp_header);
+
+       ep->fd = fd;
+
+       codec = ep->codec;
+       codec->init(preset, payload_len, &ep->codec_data);
+       codec->get_config(ep->codec_data, cfg);
+
+       ep->mp = calloc(mtu, 1);
+       if (!ep->mp)
+               goto failed;
+
+       if (ep->codec->use_rtp) {
+               struct media_packet_rtp *mp_rtp =
+                                       (struct media_packet_rtp *) ep->mp;
+               mp_rtp->hdr.v = 2;
+               mp_rtp->hdr.pt = 0x60;
+               mp_rtp->hdr.ssrc = htonl(1);
+       }
+
+       ep->mp_data_len = payload_len;
+
+       free(preset);
+
+       return true;
+
+failed:
+       close(fd);
+       free(preset);
+
+       return false;
+}
+
+static void close_endpoint(struct audio_endpoint *ep)
+{
+       ipc_close_stream_cmd(ep->id);
+       if (ep->fd >= 0) {
+               close(ep->fd);
+               ep->fd = -1;
+       }
+
+       free(ep->mp);
+
+       ep->codec->cleanup(ep->codec_data);
+       ep->codec_data = NULL;
+}
+
+static bool resume_endpoint(struct audio_endpoint *ep)
+{
+       if (ipc_resume_stream_cmd(ep->id) != AUDIO_STATUS_SUCCESS)
+               return false;
+
+       ep->samples = 0;
+       ep->resync = false;
+
+       ep->codec->update_qos(ep->codec_data, QOS_POLICY_DEFAULT);
+
+       return true;
+}
+
+static void downmix_to_mono(struct a2dp_stream_out *out, const uint8_t *buffer,
+                                                               size_t bytes)
+{
+       const int16_t *input = (const void *) buffer;
+       int16_t *output = (void *) out->downmix_buf;
+       size_t i, frames;
+
+       /* PCM 16bit stereo */
+       frames = bytes / (2 * sizeof(int16_t));
+
+       for (i = 0; i < frames; i++) {
+               int16_t l = get_le16(&input[i * 2]);
+               int16_t r = get_le16(&input[i * 2 + 1]);
+
+               put_le16((l + r) / 2, &output[i]);
+       }
+}
+
+static bool wait_for_endpoint(struct audio_endpoint *ep, bool *writable)
+{
+       int ret;
+
+       while (true) {
+               struct pollfd pollfd;
+
+               pollfd.fd = ep->fd;
+               pollfd.events = POLLOUT;
+               pollfd.revents = 0;
+
+               ret = poll(&pollfd, 1, 500);
+
+               if (ret >= 0) {
+                       *writable = !!(pollfd.revents & POLLOUT);
+                       break;
+               }
+
+               if (errno != EINTR) {
+                       ret = errno;
+                       error("poll failed (%d)", ret);
+                       return false;
+               }
+       }
+
+       return true;
+}
+
+static bool write_to_endpoint(struct audio_endpoint *ep, size_t bytes)
+{
+       struct media_packet *mp = (struct media_packet *) ep->mp;
+       int ret;
+
+       while (true) {
+               ret = write(ep->fd, mp, bytes);
+
+               if (ret >= 0)
+                       break;
+
+               /*
+                * this should not happen so let's issue warning, but do not
+                * fail, we can try to write next packet
+                */
+               if (errno == EAGAIN) {
+                       ret = errno;
+                       warn("write failed (%d)", ret);
+                       break;
+               }
+
+               if (errno != EINTR) {
+                       ret = errno;
+                       error("write failed (%d)", ret);
+                       return false;
+               }
+       }
+
+       return true;
+}
+
+static bool write_data(struct a2dp_stream_out *out, const void *buffer,
+                                                               size_t bytes)
+{
+       struct audio_endpoint *ep = out->ep;
+       struct media_packet *mp = (struct media_packet *) ep->mp;
+       struct media_packet_rtp *mp_rtp = (struct media_packet_rtp *) ep->mp;
+       size_t free_space = ep->mp_data_len;
+       size_t consumed = 0;
+
+       while (consumed < bytes) {
+               size_t written = 0;
+               ssize_t read;
+               uint32_t samples;
+               int ret;
+               struct timespec current;
+               uint64_t audio_sent, audio_passed;
+               bool do_write = false;
+
+               /*
+                * prepare media packet in advance so we don't waste time after
+                * wakeup
+                */
+               if (ep->codec->use_rtp) {
+                       mp_rtp->hdr.sequence_number = htons(ep->seq++);
+                       mp_rtp->hdr.timestamp = htonl(ep->samples);
+               }
+               read = ep->codec->encode_mediapacket(ep->codec_data,
+                                               buffer + consumed,
+                                               bytes - consumed, mp,
+                                               free_space, &written);
+
+               /*
+                * not much we can do here, let's just ignore remaining
+                * data and continue
+                */
+               if (read <= 0)
+                       return true;
+
+               /* calculate where are we and where we should be */
+               clock_gettime(CLOCK_MONOTONIC, &current);
+               if (!ep->samples)
+                       memcpy(&ep->start, &current, sizeof(ep->start));
+               audio_sent = ep->samples * 1000000ll / out->cfg.rate;
+               audio_passed = timespec_diff_us(&current, &ep->start);
+
+               /*
+                * if we're ahead of stream then wait for next write point,
+                * if we're lagging more than 100ms then stop writing and just
+                * skip data until we're back in sync
+                */
+               if (audio_sent > audio_passed) {
+                       struct timespec anchor;
+
+                       ep->resync = false;
+
+                       timespec_add(&ep->start, audio_sent, &anchor);
+
+                       while (true) {
+                               ret = clock_nanosleep(CLOCK_MONOTONIC,
+                                                       TIMER_ABSTIME, &anchor,
+                                                       NULL);
+
+                               if (!ret)
+                                       break;
+
+                               if (ret != EINTR) {
+                                       error("clock_nanosleep failed (%d)",
+                                                                       ret);
+                                       return false;
+                               }
+                       }
+               } else if (!ep->resync) {
+                       uint64_t diff = audio_passed - audio_sent;
+
+                       if (diff > MAX_DELAY) {
+                               warn("lag is %jums, resyncing", diff / 1000);
+
+                               ep->codec->update_qos(ep->codec_data,
+                                                       QOS_POLICY_DECREASE);
+                               ep->resync = true;
+                       }
+               }
+
+               /* we send data only in case codec encoded some data, i.e. some
+                * codecs do internal buffering and output data only if full
+                * frame can be encoded
+                * in resync mode we'll just drop mediapackets
+                */
+               if (written > 0 && !ep->resync) {
+                       /* wait some time for socket to be ready for write,
+                        * but we'll just skip writing data if timeout occurs
+                        */
+                       if (!wait_for_endpoint(ep, &do_write))
+                               return false;
+
+                       if (do_write) {
+                               if (ep->codec->use_rtp)
+                                       written += sizeof(struct rtp_header);
+
+                               if (!write_to_endpoint(ep, written))
+                                       return false;
+                       }
+               }
+
+               /*
+                * AudioFlinger provides 16bit PCM, so sample size is 2 bytes
+                * multiplied by number of channels. Number of channels is
+                * simply number of bits set in channels mask.
+                */
+               samples = read / (2 * popcount(out->cfg.channels));
+               ep->samples += samples;
+               consumed += read;
+       }
+
+       return true;
+}
+
 static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
                                                                size_t bytes)
 {
        struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
+       const void *in_buf = buffer;
+       size_t in_len = bytes;
+
+       /* just return in case we're closing */
+       if (out->audio_state == AUDIO_A2DP_STATE_NONE)
+               return -1;
 
        /* We can auto-start only from standby */
        if (out->audio_state == AUDIO_A2DP_STATE_STANDBY) {
                DBG("stream in standby, auto-start");
 
-               if (ipc_resume_stream_cmd(out->ep->id) != AUDIO_STATUS_SUCCESS)
+               if (!resume_endpoint(out->ep))
                        return -1;
 
                out->audio_state = AUDIO_A2DP_STATE_STARTED;
        }
 
        if (out->audio_state != AUDIO_A2DP_STATE_STARTED) {
-               DBG("stream not started");
+               error("audio: stream not started");
                return -1;
        }
 
-       /* TODO: encode data using codec */
+       if (out->ep->fd < 0) {
+               error("audio: no transport socket");
+               return -1;
+       }
+
+       /*
+        * currently Android audioflinger is not able to provide mono stream on
+        * A2DP output so down mixing needs to be done in hal-audio plugin.
+        *
+        * for reference see
+        * AudioFlinger::PlaybackThread::readOutputParameters()
+        * frameworks/av/services/audioflinger/Threads.cpp:1631
+        */
+       if (out->cfg.channels == AUDIO_CHANNEL_OUT_MONO) {
+               if (!out->downmix_buf) {
+                       error("audio: downmix buffer not initialized");
+                       return -1;
+               }
+
+               downmix_to_mono(out, buffer, bytes);
+
+               in_buf = out->downmix_buf;
+               in_len = bytes / 2;
+       }
+
+       if (!write_data(out, in_buf, in_len))
+               return -1;
 
        return bytes;
 }
@@ -592,7 +844,7 @@ static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
        DBG("");
 
        if (rate != out->cfg.rate) {
-               DBG("cannot set sample rate to %d", rate);
+               warn("audio: cannot set sample rate to %d", rate);
                return -1;
        }
 
@@ -602,16 +854,27 @@ static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
 static size_t out_get_buffer_size(const struct audio_stream *stream)
 {
        DBG("");
-       return -ENOSYS;
+
+       /*
+        * We should return proper buffer size calculated by codec (so each
+        * input buffer is encoded into single media packed) but this does not
+        * work well with AudioFlinger and causes problems. For this reason we
+        * use magic value here and out_write code takes care of splitting
+        * input buffer into multiple media packets.
+        */
+       return FIXED_BUFFER_SIZE;
 }
 
 static uint32_t out_get_channels(const struct audio_stream *stream)
 {
-       struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
-
        DBG("");
 
-       return out->cfg.channels;
+       /*
+        * AudioFlinger can only provide stereo stream, so we return it here and
+        * later we'll downmix this to mono in case codec requires it
+        */
+
+       return AUDIO_CHANNEL_OUT_STEREO;
 }
 
 static audio_format_t out_get_format(const struct audio_stream *stream)
@@ -662,6 +925,9 @@ static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
        DBG("%s", kvpairs);
 
        str = strdup(kvpairs);
+       if (!str)
+               return -ENOMEM;
+
        kvpair = strtok_r(str, ";", &saveptr);
 
        for (; kvpair && *kvpair; kvpair = strtok_r(NULL, ";", &saveptr)) {
@@ -708,8 +974,15 @@ static char *out_get_parameters(const struct audio_stream *stream,
 
 static uint32_t out_get_latency(const struct audio_stream_out *stream)
 {
+       struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
+       struct audio_endpoint *ep = out->ep;
+       size_t pkt_duration;
+
        DBG("");
-       return -ENOSYS;
+
+       pkt_duration = ep->codec->get_mediapacket_duration(ep->codec_data);
+
+       return FIXED_A2DP_PLAYBACK_LATENCY_MS + pkt_duration / 1000;
 }
 
 static int out_set_volume(struct audio_stream_out *stream, float left,
@@ -845,8 +1118,6 @@ static int audio_open_output_stream(struct audio_hw_device *dev,
 {
        struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
        struct a2dp_stream_out *out;
-       struct audio_preset *preset;
-       const struct audio_codec *codec;
 
        out = calloc(1, sizeof(struct a2dp_stream_out));
        if (!out)
@@ -871,24 +1142,20 @@ static int audio_open_output_stream(struct audio_hw_device *dev,
        out->stream.write = out_write;
        out->stream.get_render_position = out_get_render_position;
 
-       /* TODO: for now we always use endpoint 0 */
-       out->ep = &audio_endpoints[0];
+       /* We want to autoselect opened endpoint */
+       out->ep = NULL;
 
-       if (ipc_open_stream_cmd(out->ep->id, &preset) != AUDIO_STATUS_SUCCESS)
+       if (!open_endpoint(&out->ep, &out->cfg))
                goto fail;
 
-       if (!preset)
-               goto fail;
-
-       codec = out->ep->codec;
-
-       codec->init(preset, &out->ep->codec_data);
-       codec->get_config(out->ep->codec_data, &out->cfg);
-
        DBG("rate=%d channels=%d format=%d", out->cfg.rate,
-                       out->cfg.channels, out->cfg.format);
+                                       out->cfg.channels, out->cfg.format);
 
-       free(preset);
+       if (out->cfg.channels == AUDIO_CHANNEL_OUT_MONO) {
+               out->downmix_buf = malloc(FIXED_BUFFER_SIZE / 2);
+               if (!out->downmix_buf)
+                       goto fail;
+       }
 
        *stream_out = &out->stream;
        a2dp_dev->out = out;
@@ -898,6 +1165,7 @@ static int audio_open_output_stream(struct audio_hw_device *dev,
        return 0;
 
 fail:
+       error("audio: cannot open output stream");
        free(out);
        *stream_out = NULL;
        return -EIO;
@@ -907,14 +1175,13 @@ static void audio_close_output_stream(struct audio_hw_device *dev,
                                        struct audio_stream_out *stream)
 {
        struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
-       struct audio_endpoint *ep = a2dp_dev->out->ep;
+       struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
 
        DBG("");
 
-       ipc_close_stream_cmd(ep->id);
+       close_endpoint(a2dp_dev->out->ep);
 
-       ep->codec->cleanup(ep->codec_data);
-       ep->codec_data = NULL;
+       free(out->downmix_buf);
 
        free(stream);
        a2dp_dev->out = NULL;
@@ -923,8 +1190,16 @@ static void audio_close_output_stream(struct audio_hw_device *dev,
 static int audio_set_parameters(struct audio_hw_device *dev,
                                                        const char *kvpairs)
 {
+       struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
+       struct a2dp_stream_out *out = a2dp_dev->out;
+
        DBG("");
-       return -ENOSYS;
+
+       if (!out)
+               return 0;
+
+       return out->stream.common.set_parameters((struct audio_stream *) out,
+                                                               kvpairs);
 }
 
 static char *audio_get_parameters(const struct audio_hw_device *dev,
@@ -1028,13 +1303,26 @@ static int audio_dump(const audio_hw_device_t *device, int fd)
 static int audio_close(hw_device_t *device)
 {
        struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *)device;
+       unsigned int i;
 
        DBG("");
 
-       pthread_mutex_lock(&close_mutex);
-       audio_ipc_cleanup();
-       close_thread = true;
-       pthread_mutex_unlock(&close_mutex);
+       unregister_endpoints();
+
+       for (i = 0; i < NUM_CODECS; i++) {
+               const struct audio_codec *codec = audio_codecs[i].get_codec();
+
+               if (!audio_codecs[i].loaded)
+                       continue;
+
+               if (codec->unload)
+                       codec->unload();
+
+               audio_codecs[i].loaded = false;
+       }
+
+       shutdown(listen_sk, SHUT_RDWR);
+       shutdown(audio_sk, SHUT_RDWR);
 
        pthread_join(ipc_th, NULL);
 
@@ -1049,19 +1337,31 @@ static void *ipc_handler(void *data)
 {
        bool done = false;
        struct pollfd pfd;
+       int sk;
 
        DBG("");
 
        while (!done) {
                DBG("Waiting for connection ...");
-               audio_sk = accept(listen_sk, NULL, NULL);
-               if (audio_sk < 0) {
+
+               sk = accept(listen_sk, NULL, NULL);
+               if (sk < 0) {
                        int err = errno;
-                       error("audio: Failed to accept socket: %d (%s)", err,
-                                                               strerror(err));
-                       continue;
+
+                       if (err == EINTR)
+                               continue;
+
+                       if (err != ECONNABORTED && err != EINVAL)
+                               error("audio: Failed to accept socket: %d (%s)",
+                                                       err, strerror(err));
+
+                       break;
                }
 
+               pthread_mutex_lock(&sk_mutex);
+               audio_sk = sk;
+               pthread_mutex_unlock(&sk_mutex);
+
                DBG("Audio IPC: Connected");
 
                if (register_endpoints() != AUDIO_STATUS_SUCCESS) {
@@ -1069,7 +1369,12 @@ static void *ipc_handler(void *data)
 
                        unregister_endpoints();
 
+                       pthread_mutex_lock(&sk_mutex);
                        shutdown(audio_sk, SHUT_RDWR);
+                       close(audio_sk);
+                       audio_sk = -1;
+                       pthread_mutex_unlock(&sk_mutex);
+
                        continue;
                }
 
@@ -1080,19 +1385,16 @@ 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");
-                       audio_sk = -1;
-               }
+               info("Audio HAL: Socket closed");
 
-               /*Check if audio_dev is closed */
-               pthread_mutex_lock(&close_mutex);
-               done = close_thread;
-               close_thread = false;
-               pthread_mutex_unlock(&close_mutex);
+               pthread_mutex_lock(&sk_mutex);
+               close(audio_sk);
+               audio_sk = -1;
+               pthread_mutex_unlock(&sk_mutex);
        }
 
-       unregister_endpoints();
+       /* audio_sk is closed at this point, just cleanup endpoints states */
+       memset(audio_endpoints, 0, sizeof(audio_endpoints));
 
        info("Closing Audio IPC thread");
        return NULL;
@@ -1108,9 +1410,9 @@ static int audio_ipc_init(void)
 
        sk = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
        if (sk < 0) {
-               err = errno;
-               error("audio: Failed to create socket: %d (%s)", err,
-                                                               strerror(err));
+               err = -errno;
+               error("audio: Failed to create socket: %d (%s)", -err,
+                                                               strerror(-err));
                return err;
        }
 
@@ -1121,16 +1423,16 @@ static int audio_ipc_init(void)
                                        sizeof(BLUEZ_AUDIO_SK_PATH));
 
        if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
-               err = errno;
-               error("audio: Failed to bind socket: %d (%s)", err,
-                                                               strerror(err));
+               err = -errno;
+               error("audio: Failed to bind socket: %d (%s)", -err,
+                                                               strerror(-err));
                goto failed;
        }
 
        if (listen(sk, 1) < 0) {
-               err = errno;
-               error("audio: Failed to listen on the socket: %d (%s)", err,
-                                                               strerror(err));
+               err = -errno;
+               error("audio: Failed to listen on the socket: %d (%s)", -err,
+                                                               strerror(-err));
                goto failed;
        }
 
@@ -1141,7 +1443,7 @@ static int audio_ipc_init(void)
                err = -err;
                ipc_th = 0;
                error("audio: Failed to start Audio IPC thread: %d (%s)",
-                                                       err, strerror(err));
+                                                       -err, strerror(-err));
                goto failed;
        }
 
@@ -1156,6 +1458,7 @@ static int audio_open(const hw_module_t *module, const char *name,
                                                        hw_device_t **device)
 {
        struct a2dp_audio_dev *a2dp_dev;
+       size_t i;
        int err;
 
        DBG("");
@@ -1167,8 +1470,8 @@ static int audio_open(const hw_module_t *module, const char *name,
        }
 
        err = audio_ipc_init();
-       if (err)
-               return -err;
+       if (err < 0)
+               return err;
 
        a2dp_dev = calloc(1, sizeof(struct a2dp_audio_dev));
        if (!a2dp_dev)
@@ -1194,9 +1497,20 @@ static int audio_open(const hw_module_t *module, const char *name,
        a2dp_dev->dev.close_input_stream = audio_close_input_stream;
        a2dp_dev->dev.dump = audio_dump;
 
-       /* Note that &a2dp_dev->dev.common is the same pointer as a2dp_dev.
+       for (i = 0; i < NUM_CODECS; i++) {
+               const struct audio_codec *codec = audio_codecs[i].get_codec();
+
+               if (codec->load && !codec->load())
+                       continue;
+
+               audio_codecs[i].loaded = true;
+       }
+
+       /*
+        * Note that &a2dp_dev->dev.common is the same pointer as a2dp_dev.
         * This results from the structure of following structs:a2dp_audio_dev,
-        * audio_hw_device. We will rely on this later in the code.*/
+        * audio_hw_device. We will rely on this later in the code.
+        */
        *device = &a2dp_dev->dev.common;
 
        return 0;
@@ -1208,12 +1522,12 @@ static struct hw_module_methods_t hal_module_methods = {
 
 struct audio_module HAL_MODULE_INFO_SYM = {
        .common = {
-       .tag = HARDWARE_MODULE_TAG,
-       .version_major = 1,
-       .version_minor = 0,
-       .id = AUDIO_HARDWARE_MODULE_ID,
-       .name = "A2DP Bluez HW HAL",
-       .author = "Intel Corporation",
-       .methods = &hal_module_methods,
+               .tag = HARDWARE_MODULE_TAG,
+               .version_major = 1,
+               .version_minor = 0,
+               .id = AUDIO_HARDWARE_MODULE_ID,
+               .name = "A2DP Bluez HW HAL",
+               .author = "Intel Corporation",
+               .methods = &hal_module_methods,
        },
 };