Home | History | Annotate | Download | only in audio_a2dp_hw
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 2009-2012 Broadcom Corporation
      4  *
      5  *  Licensed under the Apache License, Version 2.0 (the "License");
      6  *  you may not use this file except in compliance with the License.
      7  *  You may obtain a copy of the License at:
      8  *
      9  *  http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  *
     17  ******************************************************************************/
     18 
     19 /*****************************************************************************
     20  *
     21  *  Filename:      audio_a2dp_hw.c
     22  *
     23  *  Description:   Implements hal for bluedroid a2dp audio device
     24  *
     25  *****************************************************************************/
     26 
     27 #include <errno.h>
     28 #include <inttypes.h>
     29 #include <pthread.h>
     30 #include <stdint.h>
     31 #include <sys/time.h>
     32 #include <sys/socket.h>
     33 #include <sys/un.h>
     34 #include <sys/poll.h>
     35 #include <sys/errno.h>
     36 #include <sys/stat.h>
     37 #include <unistd.h>
     38 #include <fcntl.h>
     39 #include <cutils/str_parms.h>
     40 #include <cutils/sockets.h>
     41 
     42 #include <system/audio.h>
     43 #include <hardware/audio.h>
     44 
     45 #include <hardware/hardware.h>
     46 #include "audio_a2dp_hw.h"
     47 #include "bt_utils.h"
     48 
     49 #define LOG_TAG "bt_a2dp_hw"
     50 #include "osi/include/log.h"
     51 
     52 /*****************************************************************************
     53 **  Constants & Macros
     54 ******************************************************************************/
     55 
     56 #define CTRL_CHAN_RETRY_COUNT 3
     57 #define USEC_PER_SEC 1000000L
     58 
     59 #define CASE_RETURN_STR(const) case const: return #const;
     60 
     61 #define FNLOG()             LOG_VERBOSE("%s", __FUNCTION__);
     62 #define DEBUG(fmt, ...)     LOG_VERBOSE("%s: " fmt,__FUNCTION__, ## __VA_ARGS__)
     63 #define INFO(fmt, ...)      LOG_INFO("%s: " fmt,__FUNCTION__, ## __VA_ARGS__)
     64 #define ERROR(fmt, ...)     LOG_ERROR("%s: " fmt,__FUNCTION__, ## __VA_ARGS__)
     65 
     66 #define ASSERTC(cond, msg, val) if (!(cond)) {ERROR("### ASSERT : %s line %d %s (%d) ###", __FILE__, __LINE__, msg, val);}
     67 
     68 /*****************************************************************************
     69 **  Local type definitions
     70 ******************************************************************************/
     71 
     72 typedef enum {
     73     AUDIO_A2DP_STATE_STARTING,
     74     AUDIO_A2DP_STATE_STARTED,
     75     AUDIO_A2DP_STATE_STOPPING,
     76     AUDIO_A2DP_STATE_STOPPED,
     77     AUDIO_A2DP_STATE_SUSPENDED, /* need explicit set param call to resume (suspend=false) */
     78     AUDIO_A2DP_STATE_STANDBY    /* allows write to autoresume */
     79 } a2dp_state_t;
     80 
     81 struct a2dp_stream_in;
     82 struct a2dp_stream_out;
     83 
     84 struct a2dp_audio_device {
     85     struct audio_hw_device device;
     86     struct a2dp_stream_in  *input;
     87     struct a2dp_stream_out *output;
     88 };
     89 
     90 struct a2dp_config {
     91     uint32_t                rate;
     92     uint32_t                channel_flags;
     93     int                     format;
     94 };
     95 
     96 /* move ctrl_fd outside output stream and keep open until HAL unloaded ? */
     97 
     98 struct a2dp_stream_common {
     99     pthread_mutex_t         lock;
    100     int                     ctrl_fd;
    101     int                     audio_fd;
    102     size_t                  buffer_sz;
    103     struct a2dp_config      cfg;
    104     a2dp_state_t            state;
    105 };
    106 
    107 struct a2dp_stream_out {
    108     struct audio_stream_out stream;
    109     struct a2dp_stream_common common;
    110 };
    111 
    112 struct a2dp_stream_in {
    113     struct audio_stream_in  stream;
    114     struct a2dp_stream_common common;
    115 };
    116 
    117 /*****************************************************************************
    118 **  Static variables
    119 ******************************************************************************/
    120 
    121 /*****************************************************************************
    122 **  Static functions
    123 ******************************************************************************/
    124 
    125 static size_t out_get_buffer_size(const struct audio_stream *stream);
    126 
    127 /*****************************************************************************
    128 **  Externs
    129 ******************************************************************************/
    130 
    131 /*****************************************************************************
    132 **  Functions
    133 ******************************************************************************/
    134 
    135 /*****************************************************************************
    136 **   Miscellaneous helper functions
    137 ******************************************************************************/
    138 
    139 static const char* dump_a2dp_ctrl_event(char event)
    140 {
    141     switch(event)
    142     {
    143         CASE_RETURN_STR(A2DP_CTRL_CMD_NONE)
    144         CASE_RETURN_STR(A2DP_CTRL_CMD_CHECK_READY)
    145         CASE_RETURN_STR(A2DP_CTRL_CMD_START)
    146         CASE_RETURN_STR(A2DP_CTRL_CMD_STOP)
    147         CASE_RETURN_STR(A2DP_CTRL_CMD_SUSPEND)
    148         default:
    149             return "UNKNOWN MSG ID";
    150     }
    151 }
    152 
    153 /* logs timestamp with microsec precision
    154    pprev is optional in case a dedicated diff is required */
    155 static void ts_log(char *tag, int val, struct timespec *pprev_opt)
    156 {
    157     struct timespec now;
    158     static struct timespec prev = {0,0};
    159     unsigned long long now_us;
    160     unsigned long long diff_us;
    161     UNUSED(tag);
    162     UNUSED(val);
    163 
    164     clock_gettime(CLOCK_MONOTONIC, &now);
    165 
    166     now_us = now.tv_sec*USEC_PER_SEC + now.tv_nsec/1000;
    167 
    168     if (pprev_opt)
    169     {
    170         diff_us = (now.tv_sec - prev.tv_sec) * USEC_PER_SEC + (now.tv_nsec - prev.tv_nsec)/1000;
    171         *pprev_opt = now;
    172         DEBUG("[%s] ts %08lld, *diff %08lld, val %d", tag, now_us, diff_us, val);
    173     }
    174     else
    175     {
    176         diff_us = (now.tv_sec - prev.tv_sec) * USEC_PER_SEC + (now.tv_nsec - prev.tv_nsec)/1000;
    177         prev = now;
    178         DEBUG("[%s] ts %08lld, diff %08lld, val %d", tag, now_us, diff_us, val);
    179     }
    180 }
    181 
    182 static int calc_audiotime(struct a2dp_config cfg, int bytes)
    183 {
    184     int chan_count = popcount(cfg.channel_flags);
    185 
    186     ASSERTC(cfg.format == AUDIO_FORMAT_PCM_16_BIT,
    187             "unsupported sample sz", cfg.format);
    188 
    189     return bytes*(1000000/(chan_count*2))/cfg.rate;
    190 }
    191 
    192 /*****************************************************************************
    193 **
    194 **   bluedroid stack adaptation
    195 **
    196 *****************************************************************************/
    197 
    198 static int skt_connect(char *path, size_t buffer_sz)
    199 {
    200     int ret;
    201     int skt_fd;
    202     struct sockaddr_un remote;
    203     int len;
    204 
    205     INFO("connect to %s (sz %zu)", path, buffer_sz);
    206 
    207     skt_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
    208 
    209     if(socket_local_client_connect(skt_fd, path,
    210             ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM) < 0)
    211     {
    212         ERROR("failed to connect (%s)", strerror(errno));
    213         close(skt_fd);
    214         return -1;
    215     }
    216 
    217     len = buffer_sz;
    218     ret = setsockopt(skt_fd, SOL_SOCKET, SO_SNDBUF, (char*)&len, (int)sizeof(len));
    219 
    220     /* only issue warning if failed */
    221     if (ret < 0)
    222         ERROR("setsockopt failed (%s)", strerror(errno));
    223 
    224     ret = setsockopt(skt_fd, SOL_SOCKET, SO_RCVBUF, (char*)&len, (int)sizeof(len));
    225 
    226     /* only issue warning if failed */
    227     if (ret < 0)
    228         ERROR("setsockopt failed (%s)", strerror(errno));
    229 
    230     INFO("connected to stack fd = %d", skt_fd);
    231 
    232     return skt_fd;
    233 }
    234 
    235 static int skt_read(int fd, void *p, size_t len)
    236 {
    237     int read;
    238     struct pollfd pfd;
    239     struct timespec ts;
    240 
    241     FNLOG();
    242 
    243     ts_log("skt_read recv", len, NULL);
    244 
    245     if ((read = recv(fd, p, len, MSG_NOSIGNAL)) == -1)
    246     {
    247         ERROR("write failed with errno=%d\n", errno);
    248         return -1;
    249     }
    250 
    251     return read;
    252 }
    253 
    254 static int skt_write(int fd, const void *p, size_t len)
    255 {
    256     int sent;
    257     struct pollfd pfd;
    258 
    259     FNLOG();
    260 
    261     pfd.fd = fd;
    262     pfd.events = POLLOUT;
    263 
    264     /* poll for 500 ms */
    265 
    266     /* send time out */
    267     if (poll(&pfd, 1, 500) == 0)
    268         return 0;
    269 
    270     ts_log("skt_write", len, NULL);
    271 
    272     if ((sent = send(fd, p, len, MSG_NOSIGNAL)) == -1)
    273     {
    274         ERROR("write failed with errno=%d\n", errno);
    275         return -1;
    276     }
    277 
    278     return sent;
    279 }
    280 
    281 static int skt_disconnect(int fd)
    282 {
    283     INFO("fd %d", fd);
    284 
    285     if (fd != AUDIO_SKT_DISCONNECTED)
    286     {
    287         shutdown(fd, SHUT_RDWR);
    288         close(fd);
    289     }
    290     return 0;
    291 }
    292 
    293 
    294 
    295 /*****************************************************************************
    296 **
    297 **  AUDIO CONTROL PATH
    298 **
    299 *****************************************************************************/
    300 
    301 static int a2dp_ctrl_receive(struct a2dp_stream_common *common, void* buffer, int length)
    302 {
    303     int ret = recv(common->ctrl_fd, buffer, length, MSG_NOSIGNAL);
    304     if (ret < 0)
    305     {
    306         ERROR("ack failed (%s)", strerror(errno));
    307         if (errno == EINTR)
    308         {
    309             /* retry again */
    310             ret = recv(common->ctrl_fd, buffer, length, MSG_NOSIGNAL);
    311             if (ret < 0)
    312             {
    313                ERROR("ack failed (%s)", strerror(errno));
    314                skt_disconnect(common->ctrl_fd);
    315                common->ctrl_fd = AUDIO_SKT_DISCONNECTED;
    316                return -1;
    317             }
    318         }
    319         else
    320         {
    321                skt_disconnect(common->ctrl_fd);
    322                common->ctrl_fd = AUDIO_SKT_DISCONNECTED;
    323                return -1;
    324 
    325         }
    326     }
    327     return ret;
    328 }
    329 
    330 static int a2dp_command(struct a2dp_stream_common *common, char cmd)
    331 {
    332     char ack;
    333 
    334     DEBUG("A2DP COMMAND %s", dump_a2dp_ctrl_event(cmd));
    335 
    336     /* send command */
    337     if (send(common->ctrl_fd, &cmd, 1, MSG_NOSIGNAL) == -1)
    338     {
    339         ERROR("cmd failed (%s)", strerror(errno));
    340         skt_disconnect(common->ctrl_fd);
    341         common->ctrl_fd = AUDIO_SKT_DISCONNECTED;
    342         return -1;
    343     }
    344 
    345     /* wait for ack byte */
    346     if (a2dp_ctrl_receive(common, &ack, 1) < 0)
    347         return -1;
    348 
    349     DEBUG("A2DP COMMAND %s DONE STATUS %d", dump_a2dp_ctrl_event(cmd), ack);
    350 
    351     if (ack == A2DP_CTRL_ACK_INCALL_FAILURE)
    352         return ack;
    353     if (ack != A2DP_CTRL_ACK_SUCCESS)
    354         return -1;
    355 
    356     return 0;
    357 }
    358 
    359 static int check_a2dp_ready(struct a2dp_stream_common *common)
    360 {
    361     if (a2dp_command(common, A2DP_CTRL_CMD_CHECK_READY) < 0)
    362     {
    363         ERROR("check a2dp ready failed");
    364         return -1;
    365     }
    366     return 0;
    367 }
    368 
    369 static int a2dp_read_audio_config(struct a2dp_stream_common *common)
    370 {
    371     char cmd = A2DP_CTRL_GET_AUDIO_CONFIG;
    372     uint32_t sample_rate;
    373     uint8_t channel_count;
    374 
    375     if (a2dp_command(common, A2DP_CTRL_GET_AUDIO_CONFIG) < 0)
    376     {
    377         ERROR("check a2dp ready failed");
    378         return -1;
    379     }
    380 
    381     if (a2dp_ctrl_receive(common, &sample_rate, 4) < 0)
    382         return -1;
    383     if (a2dp_ctrl_receive(common, &channel_count, 1) < 0)
    384         return -1;
    385 
    386     common->cfg.channel_flags = (channel_count == 1 ? AUDIO_CHANNEL_IN_MONO : AUDIO_CHANNEL_IN_STEREO);
    387     common->cfg.format = AUDIO_STREAM_DEFAULT_FORMAT;
    388     common->cfg.rate = sample_rate;
    389 
    390     INFO("got config %d %d", common->cfg.format, common->cfg.rate);
    391 
    392     return 0;
    393 }
    394 
    395 static void a2dp_open_ctrl_path(struct a2dp_stream_common *common)
    396 {
    397     int i;
    398 
    399     /* retry logic to catch any timing variations on control channel */
    400     for (i = 0; i < CTRL_CHAN_RETRY_COUNT; i++)
    401     {
    402         /* connect control channel if not already connected */
    403         if ((common->ctrl_fd = skt_connect(A2DP_CTRL_PATH, common->buffer_sz)) > 0)
    404         {
    405             /* success, now check if stack is ready */
    406             if (check_a2dp_ready(common) == 0)
    407                 break;
    408 
    409             ERROR("error : a2dp not ready, wait 250 ms and retry");
    410             usleep(250000);
    411             skt_disconnect(common->ctrl_fd);
    412             common->ctrl_fd = AUDIO_SKT_DISCONNECTED;
    413         }
    414 
    415         /* ctrl channel not ready, wait a bit */
    416         usleep(250000);
    417     }
    418 }
    419 
    420 /*****************************************************************************
    421 **
    422 ** AUDIO DATA PATH
    423 **
    424 *****************************************************************************/
    425 
    426 static void a2dp_stream_common_init(struct a2dp_stream_common *common)
    427 {
    428     pthread_mutexattr_t lock_attr;
    429 
    430     FNLOG();
    431 
    432     pthread_mutexattr_init(&lock_attr);
    433     pthread_mutexattr_settype(&lock_attr, PTHREAD_MUTEX_RECURSIVE);
    434     pthread_mutex_init(&common->lock, &lock_attr);
    435 
    436     common->ctrl_fd = AUDIO_SKT_DISCONNECTED;
    437     common->audio_fd = AUDIO_SKT_DISCONNECTED;
    438     common->state = AUDIO_A2DP_STATE_STOPPED;
    439 
    440     /* manages max capacity of socket pipe */
    441     common->buffer_sz = AUDIO_STREAM_OUTPUT_BUFFER_SZ;
    442 }
    443 
    444 static int start_audio_datapath(struct a2dp_stream_common *common)
    445 {
    446     INFO("state %d", common->state);
    447 
    448     if (common->ctrl_fd == AUDIO_SKT_DISCONNECTED) {
    449         INFO("%s AUDIO_SKT_DISCONNECTED", __func__);
    450         return -1;
    451     }
    452 
    453     int oldstate = common->state;
    454     common->state = AUDIO_A2DP_STATE_STARTING;
    455 
    456     int a2dp_status = a2dp_command(common, A2DP_CTRL_CMD_START);
    457     if (a2dp_status < 0)
    458     {
    459         ERROR("%s Audiopath start failed (status %d)", __func__, a2dp_status);
    460 
    461         common->state = oldstate;
    462         return -1;
    463     }
    464     else if (a2dp_status == A2DP_CTRL_ACK_INCALL_FAILURE)
    465     {
    466         ERROR("%s Audiopath start failed - in call, move to suspended", __func__);
    467         common->state = oldstate;
    468         return -1;
    469     }
    470 
    471     /* connect socket if not yet connected */
    472     if (common->audio_fd == AUDIO_SKT_DISCONNECTED)
    473     {
    474         common->audio_fd = skt_connect(A2DP_DATA_PATH, common->buffer_sz);
    475         if (common->audio_fd < 0)
    476         {
    477             common->state = oldstate;
    478             return -1;
    479         }
    480 
    481         common->state = AUDIO_A2DP_STATE_STARTED;
    482     }
    483 
    484     return 0;
    485 }
    486 
    487 static int stop_audio_datapath(struct a2dp_stream_common *common)
    488 {
    489     int oldstate = common->state;
    490 
    491     INFO("state %d", common->state);
    492 
    493     if (common->ctrl_fd == AUDIO_SKT_DISCONNECTED)
    494          return -1;
    495 
    496     /* prevent any stray output writes from autostarting the stream
    497        while stopping audiopath */
    498     common->state = AUDIO_A2DP_STATE_STOPPING;
    499 
    500     if (a2dp_command(common, A2DP_CTRL_CMD_STOP) < 0)
    501     {
    502         ERROR("audiopath stop failed");
    503         common->state = oldstate;
    504         return -1;
    505     }
    506 
    507     common->state = AUDIO_A2DP_STATE_STOPPED;
    508 
    509     /* disconnect audio path */
    510     skt_disconnect(common->audio_fd);
    511     common->audio_fd = AUDIO_SKT_DISCONNECTED;
    512 
    513     return 0;
    514 }
    515 
    516 static int suspend_audio_datapath(struct a2dp_stream_common *common, bool standby)
    517 {
    518     INFO("state %d", common->state);
    519 
    520     if (common->ctrl_fd == AUDIO_SKT_DISCONNECTED)
    521          return -1;
    522 
    523     if (common->state == AUDIO_A2DP_STATE_STOPPING)
    524         return -1;
    525 
    526     if (a2dp_command(common, A2DP_CTRL_CMD_SUSPEND) < 0)
    527         return -1;
    528 
    529     if (standby)
    530         common->state = AUDIO_A2DP_STATE_STANDBY;
    531     else
    532         common->state = AUDIO_A2DP_STATE_SUSPENDED;
    533 
    534     /* disconnect audio path */
    535     skt_disconnect(common->audio_fd);
    536 
    537     common->audio_fd = AUDIO_SKT_DISCONNECTED;
    538 
    539     return 0;
    540 }
    541 
    542 
    543 /*****************************************************************************
    544 **
    545 **  audio output callbacks
    546 **
    547 *****************************************************************************/
    548 
    549 static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
    550                          size_t bytes)
    551 {
    552     struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
    553     int sent;
    554 
    555     DEBUG("write %zu bytes (fd %d)", bytes, out->common.audio_fd);
    556 
    557     pthread_mutex_lock(&out->common.lock);
    558 
    559     if (out->common.state == AUDIO_A2DP_STATE_SUSPENDED)
    560     {
    561         DEBUG("stream suspended");
    562         pthread_mutex_unlock(&out->common.lock);
    563         return -1;
    564     }
    565 
    566     /* only allow autostarting if we are in stopped or standby */
    567     if ((out->common.state == AUDIO_A2DP_STATE_STOPPED) ||
    568         (out->common.state == AUDIO_A2DP_STATE_STANDBY))
    569     {
    570         if (start_audio_datapath(&out->common) < 0)
    571         {
    572             /* emulate time this write represents to avoid very fast write
    573                failures during transition periods or remote suspend */
    574 
    575             int us_delay = calc_audiotime(out->common.cfg, bytes);
    576 
    577             DEBUG("emulate a2dp write delay (%d us)", us_delay);
    578 
    579             usleep(us_delay);
    580             pthread_mutex_unlock(&out->common.lock);
    581             return -1;
    582         }
    583     }
    584     else if (out->common.state != AUDIO_A2DP_STATE_STARTED)
    585     {
    586         ERROR("stream not in stopped or standby");
    587         pthread_mutex_unlock(&out->common.lock);
    588         return -1;
    589     }
    590 
    591     pthread_mutex_unlock(&out->common.lock);
    592     sent = skt_write(out->common.audio_fd, buffer,  bytes);
    593 
    594     if (sent == -1)
    595     {
    596         skt_disconnect(out->common.audio_fd);
    597         out->common.audio_fd = AUDIO_SKT_DISCONNECTED;
    598         if (out->common.state != AUDIO_A2DP_STATE_SUSPENDED)
    599             out->common.state = AUDIO_A2DP_STATE_STOPPED;
    600         else
    601             ERROR("write failed : stream suspended, avoid resetting state");
    602     }
    603 
    604     DEBUG("wrote %d bytes out of %zu bytes", sent, bytes);
    605     return sent;
    606 }
    607 
    608 
    609 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
    610 {
    611     struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
    612 
    613     DEBUG("rate %" PRIu32,out->common.cfg.rate);
    614 
    615     return out->common.cfg.rate;
    616 }
    617 
    618 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
    619 {
    620     struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
    621 
    622     DEBUG("out_set_sample_rate : %" PRIu32, rate);
    623 
    624     if (rate != AUDIO_STREAM_DEFAULT_RATE)
    625     {
    626         ERROR("only rate %d supported", AUDIO_STREAM_DEFAULT_RATE);
    627         return -1;
    628     }
    629 
    630     out->common.cfg.rate = rate;
    631 
    632     return 0;
    633 }
    634 
    635 static size_t out_get_buffer_size(const struct audio_stream *stream)
    636 {
    637     struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
    638 
    639     DEBUG("buffer_size : %zu", out->common.buffer_sz);
    640 
    641     return out->common.buffer_sz;
    642 }
    643 
    644 static uint32_t out_get_channels(const struct audio_stream *stream)
    645 {
    646     struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
    647 
    648     DEBUG("channels 0x%" PRIx32, out->common.cfg.channel_flags);
    649 
    650     return out->common.cfg.channel_flags;
    651 }
    652 
    653 static audio_format_t out_get_format(const struct audio_stream *stream)
    654 {
    655     struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
    656     DEBUG("format 0x%x", out->common.cfg.format);
    657     return out->common.cfg.format;
    658 }
    659 
    660 static int out_set_format(struct audio_stream *stream, audio_format_t format)
    661 {
    662     UNUSED(format);
    663     struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
    664     DEBUG("setting format not yet supported (0x%x)", format);
    665     return -ENOSYS;
    666 }
    667 
    668 static int out_standby(struct audio_stream *stream)
    669 {
    670     struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
    671     int retVal = 0;
    672 
    673     FNLOG();
    674 
    675     pthread_mutex_lock(&out->common.lock);
    676     // Do nothing in SUSPENDED state.
    677     if (out->common.state != AUDIO_A2DP_STATE_SUSPENDED)
    678         retVal = suspend_audio_datapath(&out->common, true);
    679     pthread_mutex_unlock (&out->common.lock);
    680 
    681     return retVal;
    682 }
    683 
    684 static int out_dump(const struct audio_stream *stream, int fd)
    685 {
    686     UNUSED(fd);
    687     struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
    688     FNLOG();
    689     return 0;
    690 }
    691 
    692 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
    693 {
    694     struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
    695     struct str_parms *parms;
    696     char keyval[16];
    697     int retval;
    698     int status = 0;
    699 
    700     INFO("state %d", out->common.state);
    701 
    702     parms = str_parms_create_str(kvpairs);
    703 
    704     /* dump params */
    705     str_parms_dump(parms);
    706 
    707     retval = str_parms_get_str(parms, "closing", keyval, sizeof(keyval));
    708 
    709     if (retval >= 0)
    710     {
    711         if (strcmp(keyval, "true") == 0)
    712         {
    713             DEBUG("stream closing, disallow any writes");
    714             pthread_mutex_lock(&out->common.lock);
    715             out->common.state = AUDIO_A2DP_STATE_STOPPING;
    716             pthread_mutex_unlock(&out->common.lock);
    717         }
    718     }
    719 
    720     retval = str_parms_get_str(parms, "A2dpSuspended", keyval, sizeof(keyval));
    721 
    722     if (retval >= 0)
    723     {
    724         pthread_mutex_lock(&out->common.lock);
    725         if (strcmp(keyval, "true") == 0)
    726         {
    727             if (out->common.state == AUDIO_A2DP_STATE_STARTED)
    728                 status = suspend_audio_datapath(&out->common, false);
    729         }
    730         else
    731         {
    732             /* Do not start the streaming automatically. If the phone was streaming
    733              * prior to being suspended, the next out_write shall trigger the
    734              * AVDTP start procedure */
    735             if (out->common.state == AUDIO_A2DP_STATE_SUSPENDED)
    736                 out->common.state = AUDIO_A2DP_STATE_STANDBY;
    737             /* Irrespective of the state, return 0 */
    738         }
    739         pthread_mutex_unlock(&out->common.lock);
    740     }
    741 
    742     str_parms_destroy(parms);
    743 
    744     return status;
    745 }
    746 
    747 static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
    748 {
    749     UNUSED(keys);
    750     struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
    751 
    752     FNLOG();
    753 
    754     /* add populating param here */
    755 
    756     return strdup("");
    757 }
    758 
    759 static uint32_t out_get_latency(const struct audio_stream_out *stream)
    760 {
    761     int latency_us;
    762 
    763     struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
    764 
    765     FNLOG();
    766 
    767     latency_us = ((out->common.buffer_sz * 1000 ) /
    768                     audio_stream_out_frame_size(&out->stream) /
    769                     out->common.cfg.rate) * 1000;
    770 
    771 
    772     return (latency_us / 1000) + 200;
    773 }
    774 
    775 static int out_set_volume(struct audio_stream_out *stream, float left,
    776                           float right)
    777 {
    778     UNUSED(stream);
    779     UNUSED(left);
    780     UNUSED(right);
    781 
    782     FNLOG();
    783 
    784     /* volume controlled in audioflinger mixer (digital) */
    785 
    786     return -ENOSYS;
    787 }
    788 
    789 
    790 
    791 static int out_get_render_position(const struct audio_stream_out *stream,
    792                                    uint32_t *dsp_frames)
    793 {
    794     UNUSED(stream);
    795     UNUSED(dsp_frames);
    796 
    797     FNLOG();
    798     return -EINVAL;
    799 }
    800 
    801 static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
    802 {
    803     UNUSED(stream);
    804     UNUSED(effect);
    805 
    806     FNLOG();
    807     return 0;
    808 }
    809 
    810 static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
    811 {
    812     UNUSED(stream);
    813     UNUSED(effect);
    814 
    815     FNLOG();
    816     return 0;
    817 }
    818 
    819 /*
    820  * AUDIO INPUT STREAM
    821  */
    822 
    823 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
    824 {
    825     struct a2dp_stream_in *in = (struct a2dp_stream_in *)stream;
    826 
    827     FNLOG();
    828     return in->common.cfg.rate;
    829 }
    830 
    831 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
    832 {
    833     struct a2dp_stream_in *in = (struct a2dp_stream_in *)stream;
    834 
    835     FNLOG();
    836 
    837     if (in->common.cfg.rate > 0 && in->common.cfg.rate == rate)
    838         return 0;
    839     else
    840         return -1;
    841 }
    842 
    843 static size_t in_get_buffer_size(const struct audio_stream *stream)
    844 {
    845     UNUSED(stream);
    846 
    847     FNLOG();
    848     return 320;
    849 }
    850 
    851 static uint32_t in_get_channels(const struct audio_stream *stream)
    852 {
    853     struct a2dp_stream_in *in = (struct a2dp_stream_in *)stream;
    854 
    855     FNLOG();
    856     return in->common.cfg.channel_flags;
    857 }
    858 
    859 static audio_format_t in_get_format(const struct audio_stream *stream)
    860 {
    861     UNUSED(stream);
    862 
    863     FNLOG();
    864     return AUDIO_FORMAT_PCM_16_BIT;
    865 }
    866 
    867 static int in_set_format(struct audio_stream *stream, audio_format_t format)
    868 {
    869     UNUSED(stream);
    870     UNUSED(format);
    871 
    872     FNLOG();
    873     if (format == AUDIO_FORMAT_PCM_16_BIT)
    874         return 0;
    875     else
    876         return -1;
    877 }
    878 
    879 static int in_standby(struct audio_stream *stream)
    880 {
    881     UNUSED(stream);
    882 
    883     FNLOG();
    884     return 0;
    885 }
    886 
    887 static int in_dump(const struct audio_stream *stream, int fd)
    888 {
    889     UNUSED(stream);
    890     UNUSED(fd);
    891 
    892     FNLOG();
    893     return 0;
    894 }
    895 
    896 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
    897 {
    898     UNUSED(stream);
    899     UNUSED(kvpairs);
    900 
    901     FNLOG();
    902     return 0;
    903 }
    904 
    905 static char * in_get_parameters(const struct audio_stream *stream,
    906                                 const char *keys)
    907 {
    908     UNUSED(stream);
    909     UNUSED(keys);
    910 
    911     FNLOG();
    912     return strdup("");
    913 }
    914 
    915 static int in_set_gain(struct audio_stream_in *stream, float gain)
    916 {
    917     UNUSED(stream);
    918     UNUSED(gain);
    919 
    920     FNLOG();
    921     return 0;
    922 }
    923 
    924 static ssize_t in_read(struct audio_stream_in *stream, void* buffer,
    925                        size_t bytes)
    926 {
    927     struct a2dp_stream_in *in = (struct a2dp_stream_in *)stream;
    928     int read;
    929 
    930     DEBUG("read %zu bytes, state: %d", bytes, in->common.state);
    931 
    932     if (in->common.state == AUDIO_A2DP_STATE_SUSPENDED)
    933     {
    934         DEBUG("stream suspended");
    935         return -1;
    936     }
    937 
    938     /* only allow autostarting if we are in stopped or standby */
    939     if ((in->common.state == AUDIO_A2DP_STATE_STOPPED) ||
    940         (in->common.state == AUDIO_A2DP_STATE_STANDBY))
    941     {
    942         pthread_mutex_lock(&in->common.lock);
    943 
    944         if (start_audio_datapath(&in->common) < 0)
    945         {
    946             /* emulate time this write represents to avoid very fast write
    947                failures during transition periods or remote suspend */
    948 
    949             int us_delay = calc_audiotime(in->common.cfg, bytes);
    950 
    951             DEBUG("emulate a2dp read delay (%d us)", us_delay);
    952 
    953             usleep(us_delay);
    954             pthread_mutex_unlock(&in->common.lock);
    955             return -1;
    956         }
    957 
    958         pthread_mutex_unlock(&in->common.lock);
    959     }
    960     else if (in->common.state != AUDIO_A2DP_STATE_STARTED)
    961     {
    962         ERROR("stream not in stopped or standby");
    963         return -1;
    964     }
    965 
    966     read = skt_read(in->common.audio_fd, buffer, bytes);
    967 
    968     if (read == -1)
    969     {
    970         skt_disconnect(in->common.audio_fd);
    971         in->common.audio_fd = AUDIO_SKT_DISCONNECTED;
    972         in->common.state = AUDIO_A2DP_STATE_STOPPED;
    973     } else if (read == 0) {
    974         DEBUG("read time out - return zeros");
    975         memset(buffer, 0, bytes);
    976         read = bytes;
    977     }
    978 
    979     DEBUG("read %d bytes out of %zu bytes", read, bytes);
    980     return read;
    981 }
    982 
    983 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
    984 {
    985     UNUSED(stream);
    986 
    987     FNLOG();
    988     return 0;
    989 }
    990 
    991 static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
    992 {
    993     UNUSED(stream);
    994     UNUSED(effect);
    995 
    996     FNLOG();
    997     return 0;
    998 }
    999 
   1000 static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
   1001 {
   1002     UNUSED(stream);
   1003     UNUSED(effect);
   1004 
   1005     FNLOG();
   1006 
   1007     return 0;
   1008 }
   1009 
   1010 static int adev_open_output_stream(struct audio_hw_device *dev,
   1011                                    audio_io_handle_t handle,
   1012                                    audio_devices_t devices,
   1013                                    audio_output_flags_t flags,
   1014                                    struct audio_config *config,
   1015                                    struct audio_stream_out **stream_out,
   1016                                    const char *address)
   1017 
   1018 {
   1019     struct a2dp_audio_device *a2dp_dev = (struct a2dp_audio_device *)dev;
   1020     struct a2dp_stream_out *out;
   1021     int ret = 0;
   1022     int i;
   1023     UNUSED(address);
   1024     UNUSED(handle);
   1025     UNUSED(devices);
   1026     UNUSED(flags);
   1027 
   1028     INFO("opening output");
   1029 
   1030     out = (struct a2dp_stream_out *)calloc(1, sizeof(struct a2dp_stream_out));
   1031 
   1032     if (!out)
   1033         return -ENOMEM;
   1034 
   1035     out->stream.common.get_sample_rate = out_get_sample_rate;
   1036     out->stream.common.set_sample_rate = out_set_sample_rate;
   1037     out->stream.common.get_buffer_size = out_get_buffer_size;
   1038     out->stream.common.get_channels = out_get_channels;
   1039     out->stream.common.get_format = out_get_format;
   1040     out->stream.common.set_format = out_set_format;
   1041     out->stream.common.standby = out_standby;
   1042     out->stream.common.dump = out_dump;
   1043     out->stream.common.set_parameters = out_set_parameters;
   1044     out->stream.common.get_parameters = out_get_parameters;
   1045     out->stream.common.add_audio_effect = out_add_audio_effect;
   1046     out->stream.common.remove_audio_effect = out_remove_audio_effect;
   1047     out->stream.get_latency = out_get_latency;
   1048     out->stream.set_volume = out_set_volume;
   1049     out->stream.write = out_write;
   1050     out->stream.get_render_position = out_get_render_position;
   1051 
   1052     /* initialize a2dp specifics */
   1053     a2dp_stream_common_init(&out->common);
   1054 
   1055     out->common.cfg.channel_flags = AUDIO_STREAM_DEFAULT_CHANNEL_FLAG;
   1056     out->common.cfg.format = AUDIO_STREAM_DEFAULT_FORMAT;
   1057     out->common.cfg.rate = AUDIO_STREAM_DEFAULT_RATE;
   1058 
   1059    /* set output config values */
   1060    if (config)
   1061    {
   1062       config->format = out_get_format((const struct audio_stream *)&out->stream);
   1063       config->sample_rate = out_get_sample_rate((const struct audio_stream *)&out->stream);
   1064       config->channel_mask = out_get_channels((const struct audio_stream *)&out->stream);
   1065    }
   1066     *stream_out = &out->stream;
   1067     a2dp_dev->output = out;
   1068 
   1069     a2dp_open_ctrl_path(&out->common);
   1070     if (out->common.ctrl_fd == AUDIO_SKT_DISCONNECTED)
   1071     {
   1072         ERROR("ctrl socket failed to connect (%s)", strerror(errno));
   1073         ret = -1;
   1074         goto err_open;
   1075     }
   1076 
   1077     DEBUG("success");
   1078     /* Delay to ensure Headset is in proper state when START is initiated
   1079        from DUT immediately after the connection due to ongoing music playback. */
   1080     usleep(250000);
   1081     return 0;
   1082 
   1083 err_open:
   1084     free(out);
   1085     *stream_out = NULL;
   1086     a2dp_dev->output = NULL;
   1087     ERROR("failed");
   1088     return ret;
   1089 }
   1090 
   1091 static void adev_close_output_stream(struct audio_hw_device *dev,
   1092                                      struct audio_stream_out *stream)
   1093 {
   1094     struct a2dp_audio_device *a2dp_dev = (struct a2dp_audio_device *)dev;
   1095     struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
   1096 
   1097     INFO("closing output (state %d)", out->common.state);
   1098 
   1099     pthread_mutex_lock(&out->common.lock);
   1100     if ((out->common.state == AUDIO_A2DP_STATE_STARTED) || (out->common.state == AUDIO_A2DP_STATE_STOPPING))
   1101         stop_audio_datapath(&out->common);
   1102 
   1103     skt_disconnect(out->common.ctrl_fd);
   1104     free(stream);
   1105     a2dp_dev->output = NULL;
   1106     pthread_mutex_unlock(&out->common.lock);
   1107 
   1108     DEBUG("done");
   1109 }
   1110 
   1111 static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
   1112 {
   1113     struct a2dp_audio_device *a2dp_dev = (struct a2dp_audio_device *)dev;
   1114     struct a2dp_stream_out *out = a2dp_dev->output;
   1115     int retval = 0;
   1116 
   1117     if (out == NULL)
   1118         return retval;
   1119 
   1120     INFO("state %d", out->common.state);
   1121 
   1122     retval = out->stream.common.set_parameters((struct audio_stream *)out, kvpairs);
   1123 
   1124     return retval;
   1125 }
   1126 
   1127 static char * adev_get_parameters(const struct audio_hw_device *dev,
   1128                                   const char *keys)
   1129 {
   1130     struct str_parms *parms;
   1131     UNUSED(dev);
   1132 
   1133     FNLOG();
   1134 
   1135     parms = str_parms_create_str(keys);
   1136 
   1137     str_parms_dump(parms);
   1138 
   1139     str_parms_destroy(parms);
   1140 
   1141     return strdup("");
   1142 }
   1143 
   1144 static int adev_init_check(const struct audio_hw_device *dev)
   1145 {
   1146     struct a2dp_audio_device *a2dp_dev = (struct a2dp_audio_device*)dev;
   1147 
   1148     FNLOG();
   1149 
   1150     return 0;
   1151 }
   1152 
   1153 static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
   1154 {
   1155     UNUSED(dev);
   1156     UNUSED(volume);
   1157 
   1158     FNLOG();
   1159 
   1160     return -ENOSYS;
   1161 }
   1162 
   1163 static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
   1164 {
   1165     UNUSED(dev);
   1166     UNUSED(volume);
   1167 
   1168     FNLOG();
   1169 
   1170     return -ENOSYS;
   1171 }
   1172 
   1173 static int adev_set_mode(struct audio_hw_device *dev, int mode)
   1174 {
   1175     UNUSED(dev);
   1176     UNUSED(mode);
   1177 
   1178     FNLOG();
   1179 
   1180     return 0;
   1181 }
   1182 
   1183 static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
   1184 {
   1185     UNUSED(dev);
   1186     UNUSED(state);
   1187 
   1188     FNLOG();
   1189 
   1190     return -ENOSYS;
   1191 }
   1192 
   1193 static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
   1194 {
   1195     UNUSED(dev);
   1196     UNUSED(state);
   1197 
   1198     FNLOG();
   1199 
   1200     return -ENOSYS;
   1201 }
   1202 
   1203 static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
   1204                                          const struct audio_config *config)
   1205 {
   1206     UNUSED(dev);
   1207     UNUSED(config);
   1208 
   1209     FNLOG();
   1210 
   1211     return 320;
   1212 }
   1213 
   1214 static int adev_open_input_stream(struct audio_hw_device *dev,
   1215                                   audio_io_handle_t handle,
   1216                                   audio_devices_t devices,
   1217                                   struct audio_config *config,
   1218                                   struct audio_stream_in **stream_in,
   1219                                   audio_input_flags_t flags,
   1220                                   const char *address,
   1221                                   audio_source_t source)
   1222 {
   1223     struct a2dp_audio_device *a2dp_dev = (struct a2dp_audio_device *)dev;
   1224     struct a2dp_stream_in *in;
   1225     int ret;
   1226     UNUSED(address);
   1227     UNUSED(config);
   1228     UNUSED(devices);
   1229     UNUSED(flags);
   1230     UNUSED(handle);
   1231     UNUSED(source);
   1232 
   1233     FNLOG();
   1234 
   1235     in = (struct a2dp_stream_in *)calloc(1, sizeof(struct a2dp_stream_in));
   1236 
   1237     if (!in)
   1238         return -ENOMEM;
   1239 
   1240     in->stream.common.get_sample_rate = in_get_sample_rate;
   1241     in->stream.common.set_sample_rate = in_set_sample_rate;
   1242     in->stream.common.get_buffer_size = in_get_buffer_size;
   1243     in->stream.common.get_channels = in_get_channels;
   1244     in->stream.common.get_format = in_get_format;
   1245     in->stream.common.set_format = in_set_format;
   1246     in->stream.common.standby = in_standby;
   1247     in->stream.common.dump = in_dump;
   1248     in->stream.common.set_parameters = in_set_parameters;
   1249     in->stream.common.get_parameters = in_get_parameters;
   1250     in->stream.common.add_audio_effect = in_add_audio_effect;
   1251     in->stream.common.remove_audio_effect = in_remove_audio_effect;
   1252     in->stream.set_gain = in_set_gain;
   1253     in->stream.read = in_read;
   1254     in->stream.get_input_frames_lost = in_get_input_frames_lost;
   1255 
   1256     /* initialize a2dp specifics */
   1257     a2dp_stream_common_init(&in->common);
   1258 
   1259     *stream_in = &in->stream;
   1260     a2dp_dev->input = in;
   1261 
   1262     a2dp_open_ctrl_path(&in->common);
   1263     if (in->common.ctrl_fd == AUDIO_SKT_DISCONNECTED)
   1264     {
   1265         ERROR("ctrl socket failed to connect (%s)", strerror(errno));
   1266         ret = -1;
   1267         goto err_open;
   1268     }
   1269 
   1270     if (a2dp_read_audio_config(&in->common) < 0) {
   1271         ERROR("a2dp_read_audio_config failed (%s)", strerror(errno));
   1272         ret = -1;
   1273         goto err_open;
   1274     }
   1275 
   1276     DEBUG("success");
   1277     return 0;
   1278 
   1279 err_open:
   1280     free(in);
   1281     *stream_in = NULL;
   1282     a2dp_dev->input = NULL;
   1283     ERROR("failed");
   1284     return ret;
   1285 }
   1286 
   1287 static void adev_close_input_stream(struct audio_hw_device *dev,
   1288                                    struct audio_stream_in *stream)
   1289 {
   1290     struct a2dp_audio_device *a2dp_dev = (struct a2dp_audio_device *)dev;
   1291     struct a2dp_stream_in* in = (struct a2dp_stream_in *)stream;
   1292     a2dp_state_t state = in->common.state;
   1293 
   1294     INFO("closing input (state %d)", state);
   1295 
   1296     if ((state == AUDIO_A2DP_STATE_STARTED) || (state == AUDIO_A2DP_STATE_STOPPING))
   1297         stop_audio_datapath(&in->common);
   1298 
   1299     skt_disconnect(in->common.ctrl_fd);
   1300     free(stream);
   1301     a2dp_dev->input = NULL;
   1302 
   1303     DEBUG("done");
   1304 }
   1305 
   1306 static int adev_dump(const audio_hw_device_t *device, int fd)
   1307 {
   1308     UNUSED(device);
   1309     UNUSED(fd);
   1310 
   1311     FNLOG();
   1312 
   1313     return 0;
   1314 }
   1315 
   1316 static int adev_close(hw_device_t *device)
   1317 {
   1318     FNLOG();
   1319 
   1320     free(device);
   1321     return 0;
   1322 }
   1323 
   1324 static int adev_open(const hw_module_t* module, const char* name,
   1325                      hw_device_t** device)
   1326 {
   1327     struct a2dp_audio_device *adev;
   1328     int ret;
   1329 
   1330     INFO(" adev_open in A2dp_hw module");
   1331     FNLOG();
   1332 
   1333     if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
   1334     {
   1335         ERROR("interface %s not matching [%s]", name, AUDIO_HARDWARE_INTERFACE);
   1336         return -EINVAL;
   1337     }
   1338 
   1339     adev = calloc(1, sizeof(struct a2dp_audio_device));
   1340 
   1341     if (!adev)
   1342         return -ENOMEM;
   1343 
   1344     adev->device.common.tag = HARDWARE_DEVICE_TAG;
   1345     adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
   1346     adev->device.common.module = (struct hw_module_t *) module;
   1347     adev->device.common.close = adev_close;
   1348 
   1349     adev->device.init_check = adev_init_check;
   1350     adev->device.set_voice_volume = adev_set_voice_volume;
   1351     adev->device.set_master_volume = adev_set_master_volume;
   1352     adev->device.set_mode = adev_set_mode;
   1353     adev->device.set_mic_mute = adev_set_mic_mute;
   1354     adev->device.get_mic_mute = adev_get_mic_mute;
   1355     adev->device.set_parameters = adev_set_parameters;
   1356     adev->device.get_parameters = adev_get_parameters;
   1357     adev->device.get_input_buffer_size = adev_get_input_buffer_size;
   1358     adev->device.open_output_stream = adev_open_output_stream;
   1359     adev->device.close_output_stream = adev_close_output_stream;
   1360     adev->device.open_input_stream = adev_open_input_stream;
   1361     adev->device.close_input_stream = adev_close_input_stream;
   1362     adev->device.dump = adev_dump;
   1363 
   1364     adev->output = NULL;
   1365 
   1366 
   1367     *device = &adev->device.common;
   1368 
   1369     return 0;
   1370 }
   1371 
   1372 static struct hw_module_methods_t hal_module_methods = {
   1373     .open = adev_open,
   1374 };
   1375 
   1376 struct audio_module HAL_MODULE_INFO_SYM = {
   1377     .common = {
   1378         .tag = HARDWARE_MODULE_TAG,
   1379         .version_major = 1,
   1380         .version_minor = 0,
   1381         .id = AUDIO_HARDWARE_MODULE_ID,
   1382         .name = "A2DP Audio HW HAL",
   1383         .author = "The Android Open Source Project",
   1384         .methods = &hal_module_methods,
   1385     },
   1386 };
   1387