Home | History | Annotate | Download | only in audio
      1 /*
      2  *  Copyright (C) 2008-2011 The Android Open Source Project
      3  *
      4  *  This library is free software; you can redistribute it and/or
      5  *  modify it under the terms of the GNU Lesser General Public
      6  *  License as published by the Free Software Foundation; either
      7  *  version 2.1 of the License, or (at your option) any later version.
      8  *
      9  *  This library is distributed in the hope that it will be useful,
     10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12  *  Lesser General Public License for more details.
     13  *
     14  *  You should have received a copy of the GNU Lesser General Public
     15  *  License along with this library; if not, write to the Free Software
     16  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     17  *
     18  */
     19 
     20 #define LOG_TAG "a2dp_audio_hw"
     21 //#define LOG_NDEBUG 0
     22 
     23 #include <errno.h>
     24 #include <pthread.h>
     25 #include <stdint.h>
     26 #include <sys/time.h>
     27 
     28 #include <cutils/log.h>
     29 #include <cutils/str_parms.h>
     30 
     31 #include <hardware/hardware.h>
     32 #include <system/audio.h>
     33 #include <hardware/audio.h>
     34 
     35 #include <hardware_legacy/power.h>
     36 
     37 #include "liba2dp.h"
     38 
     39 /* for backward compatibility with older audio framework */
     40 #ifndef AUDIO_PARAMETER_A2DP_SINK_ADDRESS
     41     #define AUDIO_PARAMETER_A2DP_SINK_ADDRESS "a2dp_sink_address"
     42 #endif
     43 
     44 #define A2DP_WAKE_LOCK_NAME            "A2dpOutputStream"
     45 #define MAX_WRITE_RETRIES              5
     46 
     47 #define A2DP_SUSPENDED_PARM            "A2dpSuspended"
     48 #define BLUETOOOTH_ENABLED_PARM        "bluetooth_enabled"
     49 
     50 
     51 /* number of periods in pcm buffer (one period corresponds to buffer size reported to audio flinger
     52  * by out_get_buffer_size() */
     53 #define BUF_NUM_PERIODS 6
     54 /* maximum time allowed by out_standby_stream_locked() for 2dp_write() to complete */
     55 #define BUF_WRITE_COMPLETION_TIMEOUT_MS 5000
     56 /* maximum time allowed by out_write() for frames to be available in in write thread buffer */
     57 #define BUF_WRITE_AVAILABILITY_TIMEOUT_MS 5000
     58 /* maximum number of attempts to wait for a write completion in out_standby_stream_locked() */
     59 #define MAX_WRITE_COMPLETION_ATTEMPTS 5
     60 
     61 /* NOTE: there are 2 mutexes used by the a2dp output stream.
     62  *  - lock: protects all calls to a2dp lib functions (a2dp_stop(), a2dp_cleanup()...).
     63  *    One exception is a2dp_write() which is also protected by the flag write_busy. This is because
     64  *    out_write() cannot block waiting for a2dp_write() to complete because this function
     65  *    can sleep to throttle the A2DP bit rate.
     66  *    This flag is always set/reset and tested with "lock" mutex held.
     67  *  - buf_lock: protects access to pcm buffer read and write indexes.
     68  *
     69  *  The locking order is always as follows:
     70  *      buf_lock -> lock
     71  *
     72  * If you need to hold the adev_a2dp->lock AND the astream_out->lock or astream_out->buf_lock,
     73  * you MUST take adev_a2dp lock first!!
     74  */
     75 
     76 struct astream_out;
     77 struct adev_a2dp {
     78     struct audio_hw_device  device;
     79 
     80     audio_mode_t            mode;
     81     bool                    bt_enabled;
     82     bool                    suspended;
     83 
     84     pthread_mutex_t         lock;
     85 
     86     struct astream_out      *output;
     87 };
     88 
     89 struct astream_out {
     90     struct audio_stream_out stream;
     91 
     92     uint32_t                sample_rate;
     93     size_t                  buffer_size;
     94     uint32_t                channels;
     95     audio_format_t          format;
     96 
     97     int                     fd;
     98     bool                    standby;
     99     int                     start_count;
    100     int                     retry_count;
    101     void*                   data;
    102 
    103     pthread_mutex_t         lock;   /* see NOTE on mutex locking order above */
    104 
    105     audio_devices_t         device;
    106     uint64_t                 last_write_time;
    107     uint32_t                buffer_duration_us;
    108 
    109     bool                    bt_enabled;
    110     bool                    suspended;
    111     char                    a2dp_addr[20];
    112 
    113     uint32_t *buf;              /* pcm buffer between audioflinger thread and write thread*/
    114     size_t buf_size;            /* size of pcm buffer in frames */
    115     size_t buf_rd_idx;          /* read index in pcm buffer, in frames*/
    116     size_t buf_wr_idx;          /* write index in pcm buffer, in frames */
    117     size_t buf_frames_ready;    /* number of frames ready for writing to a2dp sink */
    118     pthread_mutex_t buf_lock;   /* mutex protecting read and write indexes */
    119                                 /* see NOTE on mutex locking order above */
    120     pthread_cond_t buf_cond;    /* condition signaling data write/read to/from pcm buffer */
    121     pthread_t buf_thread;       /* thread reading data from buffer and writing to a2dp sink*/
    122     bool buf_thread_exit;       /* flag requesting write thread exit */
    123     bool write_busy;            /* indicates that a write to a2dp sink is in progress and that
    124                                    standby must wait for this flag to be cleared by write thread */
    125     pthread_cond_t write_cond;  /* condition associated with write_busy flag */
    126 };
    127 
    128 static uint64_t system_time(void)
    129 {
    130     struct timespec t;
    131 
    132     t.tv_sec = t.tv_nsec = 0;
    133     clock_gettime(CLOCK_MONOTONIC, &t);
    134 
    135     return t.tv_sec*1000000000LL + t.tv_nsec;
    136 }
    137 
    138 /** audio_stream_out implementation **/
    139 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
    140 {
    141     const struct astream_out *out = (const struct astream_out *)stream;
    142     return out->sample_rate;
    143 }
    144 
    145 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
    146 {
    147     struct astream_out *out = (struct astream_out *)stream;
    148 
    149     ALOGE("(%s:%d) %s: Implement me!", __FILE__, __LINE__, __func__);
    150     return 0;
    151 }
    152 
    153 static size_t out_get_buffer_size(const struct audio_stream *stream)
    154 {
    155     const struct astream_out *out = (const struct astream_out *)stream;
    156     return out->buffer_size;
    157 }
    158 
    159 static uint32_t out_get_channels(const struct audio_stream *stream)
    160 {
    161     const struct astream_out *out = (const struct astream_out *)stream;
    162     return out->channels;
    163 }
    164 
    165 static audio_format_t out_get_format(const struct audio_stream *stream)
    166 {
    167     const struct astream_out *out = (const struct astream_out *)stream;
    168     return out->format;
    169 }
    170 
    171 static audio_format_t out_set_format(struct audio_stream *stream, audio_format_t format)
    172 {
    173     struct astream_out *out = (struct astream_out *)stream;
    174     ALOGE("(%s:%d) %s: Implement me!", __FILE__, __LINE__, __func__);
    175     return 0;
    176 }
    177 
    178 static int out_dump(const struct audio_stream *stream, int fd)
    179 {
    180     return 0;
    181 }
    182 
    183 static uint32_t out_get_latency(const struct audio_stream_out *stream)
    184 {
    185     const struct astream_out *out = (const struct astream_out *)stream;
    186 
    187     return ((out->buffer_duration_us * BUF_NUM_PERIODS) / 1000) + 200;
    188 }
    189 
    190 static int out_set_volume(struct audio_stream_out *stream, float left,
    191                           float right)
    192 {
    193     return -ENOSYS;
    194 }
    195 
    196 static int out_get_render_position(const struct audio_stream_out *stream,
    197                                    uint32_t *dsp_frames)
    198 {
    199     return -ENOSYS;
    200 }
    201 
    202 static int _out_init_locked(struct astream_out *out, const char *addr)
    203 {
    204     int ret;
    205 
    206     if (out->data)
    207         return 0;
    208 
    209     /* XXX: shouldn't this use the sample_rate/channel_count from 'out'? */
    210     ret = a2dp_init(44100, 2, &out->data);
    211     if (ret < 0) {
    212         ALOGE("a2dp_init failed err: %d\n", ret);
    213         out->data = NULL;
    214         return ret;
    215     }
    216 
    217     /* XXX: is this even necessary? */
    218     if (addr)
    219         strlcpy(out->a2dp_addr, addr, sizeof(out->a2dp_addr));
    220     a2dp_set_sink(out->data, out->a2dp_addr);
    221 
    222     return 0;
    223 }
    224 
    225 static bool _out_validate_parms(struct astream_out *out, audio_format_t format,
    226                                 uint32_t chans, uint32_t rate)
    227 {
    228     if ((format && (format != out->format)) ||
    229         (chans && (chans != out->channels)) ||
    230         (rate && (rate != out->sample_rate)))
    231         return false;
    232     return true;
    233 }
    234 
    235 static int out_standby_stream_locked(struct astream_out *out)
    236 {
    237     int ret = 0;
    238     int attempts = MAX_WRITE_COMPLETION_ATTEMPTS;
    239 
    240     if (out->standby || !out->data)
    241         return 0;
    242 
    243     out->standby = true;
    244     /* wait for write completion if needed */
    245     while (out->write_busy && attempts--) {
    246         ret = pthread_cond_timeout_np(&out->write_cond,
    247                                 &out->lock,
    248                                 BUF_WRITE_COMPLETION_TIMEOUT_MS);
    249         ALOGE_IF(ret != 0, "out_standby_stream_locked() wait cond error %d", ret);
    250     }
    251     ALOGE_IF(attempts == 0, "out_standby_stream_locked() a2dp_write() would not stop!!!");
    252 
    253     ALOGV_IF(!out->bt_enabled, "Standby skip stop: enabled %d", out->bt_enabled);
    254     if (out->bt_enabled) {
    255         ret = a2dp_stop(out->data);
    256     }
    257     release_wake_lock(A2DP_WAKE_LOCK_NAME);
    258 
    259     return ret;
    260 }
    261 
    262 static int out_close_stream_locked(struct astream_out *out)
    263 {
    264     out_standby_stream_locked(out);
    265 
    266     if (out->data) {
    267         ALOGV("%s: calling a2dp_cleanup()", __func__);
    268         a2dp_cleanup(out->data);
    269         out->data = NULL;
    270     }
    271 
    272     return 0;
    273 }
    274 
    275 static int out_standby(struct audio_stream *stream)
    276 {
    277     struct astream_out *out = (struct astream_out *)stream;
    278 
    279     pthread_mutex_lock(&out->lock);
    280     out_standby_stream_locked(out);
    281     pthread_mutex_unlock(&out->lock);
    282 
    283     return 0;
    284 }
    285 
    286 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
    287 {
    288     struct astream_out *out = (struct astream_out *)stream;
    289     struct str_parms *parms;
    290     char *str;
    291     char value[32];
    292     int ret;
    293 
    294     parms = str_parms_create_str(kvpairs);
    295 
    296     pthread_mutex_lock(&out->lock);
    297 
    298     ret = str_parms_get_str(parms, AUDIO_PARAMETER_A2DP_SINK_ADDRESS, value, sizeof(value));
    299     if (ret >= 0) {
    300         /* strlen(00:00:00:00:00:00) == 17 */
    301         if (strlen(value) == 17) {
    302             strlcpy(out->a2dp_addr, value, sizeof(out->a2dp_addr));
    303             if (out->data)
    304                 a2dp_set_sink(out->data, out->a2dp_addr);
    305         } else
    306             ret = -EINVAL;
    307     }
    308 
    309     pthread_mutex_unlock(&out->lock);
    310     str_parms_destroy(parms);
    311     return ret;
    312 }
    313 
    314 static audio_devices_t out_get_device(const struct audio_stream *stream)
    315 {
    316     const struct astream_out *out = (const struct astream_out *)stream;
    317     return out->device;
    318 }
    319 
    320 
    321 static int out_set_device(struct audio_stream *stream, audio_devices_t device)
    322 {
    323     struct astream_out *out = (struct astream_out *)stream;
    324 
    325     if (!audio_is_a2dp_device(device))
    326         return -EINVAL;
    327 
    328     /* XXX: if out->device ever starts getting used for anything, need to
    329      * grab the out->lock */
    330     out->device = device;
    331     return 0;
    332 }
    333 
    334 static char * out_get_parameters(const struct audio_stream *stream,
    335                                  const char *keys)
    336 {
    337     struct astream_out *out = (struct astream_out *)stream;
    338     struct str_parms *parms;
    339     struct str_parms *out_parms;
    340     char *str;
    341     char value[20];
    342     int ret;
    343 
    344     parms = str_parms_create_str(keys);
    345     out_parms = str_parms_create();
    346 
    347     pthread_mutex_lock(&out->lock);
    348 
    349     ret = str_parms_get_str(parms, AUDIO_PARAMETER_A2DP_SINK_ADDRESS, value, sizeof(value));
    350     if (ret >= 0)
    351         str_parms_add_str(out_parms, AUDIO_PARAMETER_A2DP_SINK_ADDRESS, out->a2dp_addr);
    352 
    353     pthread_mutex_unlock(&out->lock);
    354 
    355     str = str_parms_to_str(out_parms);
    356     str_parms_destroy(out_parms);
    357     str_parms_destroy(parms);
    358 
    359     return str;
    360 }
    361 
    362 size_t _out_frames_available_locked(struct astream_out *out)
    363 {
    364 
    365     size_t frames = out->buf_size - out->buf_frames_ready;
    366     if (frames > out->buf_size - out->buf_wr_idx) {
    367         frames = out->buf_size - out->buf_wr_idx;
    368     }
    369     return frames;
    370 }
    371 
    372 size_t _out_frames_ready_locked(struct astream_out *out)
    373 {
    374     size_t frames = out->buf_frames_ready;
    375 
    376     if (frames > out->buf_size - out->buf_rd_idx) {
    377         frames = out->buf_size - out->buf_rd_idx;
    378     }
    379     return frames;
    380 }
    381 
    382 void _out_inc_wr_idx_locked(struct astream_out *out, size_t frames)
    383 {
    384     out->buf_wr_idx += frames;
    385     out->buf_frames_ready += frames;
    386     if (out->buf_wr_idx == out->buf_size) {
    387         out->buf_wr_idx = 0;
    388     }
    389     pthread_cond_signal(&out->buf_cond);
    390 }
    391 
    392 void _out_inc_rd_idx_locked(struct astream_out *out, size_t frames)
    393 {
    394     out->buf_rd_idx += frames;
    395     out->buf_frames_ready -= frames;
    396     if (out->buf_rd_idx == out->buf_size) {
    397         out->buf_rd_idx = 0;
    398     }
    399     pthread_cond_signal(&out->buf_cond);
    400 }
    401 
    402 static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
    403                          size_t bytes)
    404 {
    405     struct astream_out *out = (struct astream_out *)stream;
    406     int ret;
    407     size_t frames_total = bytes / sizeof(uint32_t); // always stereo 16 bit
    408     uint32_t *buf = (uint32_t *)buffer;
    409     size_t frames_written = 0;
    410 
    411     pthread_mutex_lock(&out->buf_lock);
    412     pthread_mutex_lock(&out->lock);
    413     if (!out->bt_enabled || out->suspended) {
    414         ALOGV("a2dp %s: bluetooth disabled bt_en %d, suspended %d",
    415              out->bt_enabled, out->suspended);
    416         ret = -1;
    417         goto err_bt_disabled;
    418     }
    419 
    420     if (out->standby) {
    421         acquire_wake_lock(PARTIAL_WAKE_LOCK, A2DP_WAKE_LOCK_NAME);
    422         out->standby = false;
    423         out->last_write_time = system_time();
    424         out->buf_rd_idx = 0;
    425         out->buf_wr_idx = 0;
    426         out->buf_frames_ready = 0;
    427     }
    428 
    429     ret = _out_init_locked(out, NULL);
    430     if (ret < 0) {
    431         goto err_init;
    432     }
    433 
    434     pthread_mutex_unlock(&out->lock);
    435 
    436     while (frames_written < frames_total) {
    437         size_t frames = _out_frames_available_locked(out);
    438         if (frames == 0) {
    439             int ret = pthread_cond_timeout_np(&out->buf_cond,
    440                                               &out->buf_lock,
    441                                               BUF_WRITE_AVAILABILITY_TIMEOUT_MS);
    442             if (ret != 0) {
    443                 pthread_mutex_lock(&out->lock);
    444                 goto err_write;
    445             }
    446             frames  = _out_frames_available_locked(out);
    447         }
    448         if (frames > frames_total - frames_written) {
    449             frames = frames_total - frames_written;
    450         }
    451         memcpy(out->buf + out->buf_wr_idx, buf + frames_written, frames * sizeof(uint32_t));
    452         frames_written += frames;
    453         _out_inc_wr_idx_locked(out, frames);
    454         pthread_mutex_lock(&out->lock);
    455         if (out->standby) {
    456             goto err_write;
    457         }
    458         pthread_mutex_unlock(&out->lock);
    459     }
    460     pthread_mutex_unlock(&out->buf_lock);
    461 
    462     return bytes;
    463 
    464 /* out->lock must be locked and out->buf_lock unlocked when jumping here */
    465 err_write:
    466 err_init:
    467 err_bt_disabled:
    468     pthread_mutex_unlock(&out->buf_lock);
    469     ALOGV("!!!! write error");
    470     out_standby_stream_locked(out);
    471     pthread_mutex_unlock(&out->lock);
    472 
    473     /* XXX: simulate audio output timing in case of error?!?! */
    474     usleep(out->buffer_duration_us);
    475     return ret;
    476 }
    477 
    478 
    479 static void *_out_buf_thread_func(void *context)
    480 {
    481     struct astream_out *out = (struct astream_out *)context;
    482 
    483     pthread_mutex_lock(&out->buf_lock);
    484 
    485     while(!out->buf_thread_exit) {
    486         size_t frames;
    487 
    488         frames = _out_frames_ready_locked(out);
    489         while (frames && !out->buf_thread_exit) {
    490             int retries = MAX_WRITE_RETRIES;
    491             uint64_t now;
    492             uint32_t elapsed_us;
    493 
    494             while (frames > 0 && !out->buf_thread_exit) {
    495                 int ret;
    496                 uint32_t buffer_duration_us;
    497                 /* PCM format is always 16bit stereo */
    498                 size_t bytes = frames * sizeof(uint32_t);
    499                 if (bytes > out->buffer_size) {
    500                     bytes = out->buffer_size;
    501                 }
    502 
    503                 pthread_mutex_lock(&out->lock);
    504                 if (out->standby) {
    505                     /* abort and clear all pending frames if standby requested */
    506                     pthread_mutex_unlock(&out->lock);
    507                     frames = _out_frames_ready_locked(out);
    508                     _out_inc_rd_idx_locked(out, frames);
    509                     goto wait;
    510                 }
    511                 /* indicate to out_standby_stream_locked() that a2dp_write() is active */
    512                 out->write_busy = true;
    513                 pthread_mutex_unlock(&out->lock);
    514                 pthread_mutex_unlock(&out->buf_lock);
    515 
    516                 ret = a2dp_write(out->data, out->buf + out->buf_rd_idx, bytes);
    517 
    518                 /* clear write_busy condition */
    519                 pthread_mutex_lock(&out->buf_lock);
    520                 pthread_mutex_lock(&out->lock);
    521                 out->write_busy = false;
    522                 pthread_cond_signal(&out->write_cond);
    523                 pthread_mutex_unlock(&out->lock);
    524 
    525                 if (ret < 0) {
    526                     ALOGE("%s: a2dp_write failed (%d)\n", __func__, ret);
    527                     /* skip pending frames in case of write error */
    528                     _out_inc_rd_idx_locked(out, frames);
    529                     break;
    530                 } else if (ret == 0) {
    531                     if (retries-- == 0) {
    532                         /* skip pending frames in case of multiple time out */
    533                         _out_inc_rd_idx_locked(out, frames);
    534                         break;
    535                     }
    536                     continue;
    537                 }
    538                 ret /= sizeof(uint32_t);
    539                 _out_inc_rd_idx_locked(out, ret);
    540                 frames -= ret;
    541 
    542                 /* XXX: PLEASE FIX ME!!!! */
    543 
    544                 /* if A2DP sink runs abnormally fast, sleep a little so that
    545                  * audioflinger mixer thread does no spin and starve other threads. */
    546                 /* NOTE: It is likely that the A2DP headset is being disconnected */
    547                 now = system_time();
    548                 elapsed_us = (now - out->last_write_time) / 1000UL;
    549                 buffer_duration_us = ((ret * 1000) / out->sample_rate) * 1000;
    550 
    551                 if (elapsed_us < (buffer_duration_us / 4)) {
    552                     ALOGV("A2DP sink runs too fast");
    553                     usleep(buffer_duration_us - elapsed_us);
    554                 }
    555                 out->last_write_time = now;
    556 
    557             }
    558             frames = _out_frames_ready_locked(out);
    559         }
    560 wait:
    561         if (!out->buf_thread_exit) {
    562             pthread_cond_wait(&out->buf_cond, &out->buf_lock);
    563         }
    564     }
    565     pthread_mutex_unlock(&out->buf_lock);
    566     return NULL;
    567 }
    568 
    569 
    570 
    571 static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
    572 {
    573     return 0;
    574 }
    575 
    576 static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
    577 {
    578     return 0;
    579 }
    580 
    581 static int _out_bt_enable(struct astream_out *out, bool enable)
    582 {
    583     int ret = 0;
    584 
    585     pthread_mutex_lock(&out->lock);
    586     out->bt_enabled = enable;
    587     if (!enable)
    588         ret = out_close_stream_locked(out);
    589     pthread_mutex_unlock(&out->lock);
    590 
    591     return ret;
    592 }
    593 
    594 static int _out_a2dp_suspend(struct astream_out *out, bool suspend)
    595 {
    596     pthread_mutex_lock(&out->lock);
    597     out->suspended = suspend;
    598     out_standby_stream_locked(out);
    599     pthread_mutex_unlock(&out->lock);
    600 
    601     return 0;
    602 }
    603 
    604 #ifdef AUDIO_DEVICE_API_VERSION_1_0
    605 static int adev_open_output_stream(struct audio_hw_device *dev,
    606                                    audio_io_handle_t handle,
    607                                    audio_devices_t devices,
    608                                    audio_output_flags_t flags,
    609                                    struct audio_config *config,
    610                                    struct audio_stream_out **stream_out)
    611 #else
    612 static int adev_open_output_stream(struct audio_hw_device *dev,
    613                                    uint32_t devices, audio_format_t *format,
    614                                    uint32_t *channels, uint32_t *sample_rate,
    615                                    struct audio_stream_out **stream_out)
    616 #endif
    617 {
    618     struct adev_a2dp *adev = (struct adev_a2dp *)dev;
    619     struct astream_out *out;
    620     int ret;
    621 
    622     pthread_mutex_lock(&adev->lock);
    623 
    624     /* one output stream at a time */
    625     if (adev->output) {
    626         ALOGV("output exists");
    627         ret = -EBUSY;
    628         goto err_output_exists;
    629     }
    630 
    631     out = calloc(1, sizeof(struct astream_out));
    632     if (!out) {
    633         ret = -ENOMEM;
    634         goto err_alloc;
    635     }
    636 
    637     pthread_mutex_init(&out->lock, NULL);
    638 
    639     out->stream.common.get_sample_rate = out_get_sample_rate;
    640     out->stream.common.set_sample_rate = out_set_sample_rate;
    641     out->stream.common.get_buffer_size = out_get_buffer_size;
    642     out->stream.common.get_channels = out_get_channels;
    643     out->stream.common.get_format = out_get_format;
    644     out->stream.common.set_format = out_set_format;
    645     out->stream.common.standby = out_standby;
    646     out->stream.common.dump = out_dump;
    647     out->stream.common.set_parameters = out_set_parameters;
    648     out->stream.common.get_parameters = out_get_parameters;
    649     out->stream.common.set_device = out_set_device;
    650     out->stream.common.get_device = out_get_device;
    651     out->stream.common.add_audio_effect = out_add_audio_effect;
    652     out->stream.common.remove_audio_effect = out_remove_audio_effect;
    653     out->stream.get_latency = out_get_latency;
    654     out->stream.set_volume = out_set_volume;
    655     out->stream.write = out_write;
    656     out->stream.get_render_position = out_get_render_position;
    657 
    658     out->sample_rate = 44100;
    659     out->buffer_size = 512 * 20;
    660     out->channels = AUDIO_CHANNEL_OUT_STEREO;
    661     out->format = AUDIO_FORMAT_PCM_16_BIT;
    662 
    663     out->fd = -1;
    664     out->device = devices;
    665     out->bt_enabled = adev->bt_enabled;
    666     out->suspended = adev->suspended;
    667 
    668     /* for now, buffer_duration_us is precalculated and never changed.
    669      * if the sample rate or the format ever changes on the fly, we'd have
    670      * to recalculate this */
    671     out->buffer_duration_us = ((out->buffer_size * 1000 ) /
    672                                audio_stream_frame_size(&out->stream.common) /
    673                                out->sample_rate) * 1000;
    674 #ifdef AUDIO_DEVICE_API_VERSION_1_0
    675     if (!_out_validate_parms(out, config->format,
    676                              config->channel_mask,
    677                              config->sample_rate))
    678 #else
    679     if (!_out_validate_parms(out, format ? *format : 0,
    680                              channels ? *channels : 0,
    681                              sample_rate ? *sample_rate : 0))
    682 #endif
    683     {
    684         ALOGV("invalid parameters");
    685         ret = -EINVAL;
    686         goto err_validate_parms;
    687     }
    688 
    689     int err = pthread_create(&out->buf_thread, (const pthread_attr_t *) NULL, _out_buf_thread_func, out);
    690     if (err != 0) {
    691         goto err_validate_parms;
    692     }
    693 
    694     /* PCM format is always 16bit, stereo */
    695     out->buf_size = (out->buffer_size * BUF_NUM_PERIODS) / sizeof(int32_t);
    696     out->buf = (uint32_t *)malloc(out->buf_size * sizeof(int32_t));
    697     if (!out->buf) {
    698         goto err_validate_parms;
    699     }
    700 
    701     /* XXX: check return code? */
    702     if (adev->bt_enabled)
    703         _out_init_locked(out, "00:00:00:00:00:00");
    704 
    705     adev->output = out;
    706 
    707 #ifdef AUDIO_DEVICE_API_VERSION_1_0
    708     config->format = out->format;
    709     config->channel_mask = out->channels;
    710     config->sample_rate = out->sample_rate;
    711 #else
    712     if (format)
    713         *format = out->format;
    714     if (channels)
    715         *channels = out->channels;
    716     if (sample_rate)
    717         *sample_rate = out->sample_rate;
    718 #endif
    719     pthread_mutex_unlock(&adev->lock);
    720 
    721     *stream_out = &out->stream;
    722 
    723     return 0;
    724 
    725 err_validate_parms:
    726     free(out);
    727 err_alloc:
    728 err_output_exists:
    729     pthread_mutex_unlock(&adev->lock);
    730     *stream_out = NULL;
    731     return ret;
    732 }
    733 
    734 /* needs the adev->lock held */
    735 static void adev_close_output_stream_locked(struct adev_a2dp *dev,
    736                                             struct astream_out *stream)
    737 {
    738     struct adev_a2dp *adev = (struct adev_a2dp *)dev;
    739     struct astream_out *out = (struct astream_out *)stream;
    740 
    741     /* invalid stream? */
    742     if (!adev->output || adev->output != out) {
    743         ALOGE("%s: unknown stream %p (ours is %p)", __func__, out, adev->output);
    744         return;
    745     }
    746 
    747     pthread_mutex_lock(&out->lock);
    748     /* out_write() must not be executed from now on */
    749     out->bt_enabled = false;
    750     out_close_stream_locked(out);
    751     pthread_mutex_unlock(&out->lock);
    752     if (out->buf_thread) {
    753         pthread_mutex_lock(&out->buf_lock);
    754         out->buf_thread_exit = true;
    755         pthread_cond_broadcast(&out->buf_cond);
    756         pthread_mutex_unlock(&out->buf_lock);
    757         pthread_join(out->buf_thread, (void **) NULL);
    758         pthread_cond_destroy(&out->buf_cond);
    759         pthread_mutex_destroy(&out->buf_lock);
    760     }
    761     if (out->buf) {
    762         free(out->buf);
    763     }
    764 
    765     adev->output = NULL;
    766     free(out);
    767 }
    768 
    769 static void adev_close_output_stream(struct audio_hw_device *dev,
    770                                      struct audio_stream_out *stream)
    771 {
    772     struct adev_a2dp *adev = (struct adev_a2dp *)dev;
    773     struct astream_out *out = (struct astream_out *)stream;
    774 
    775     pthread_mutex_lock(&adev->lock);
    776     adev_close_output_stream_locked(adev, out);
    777     pthread_mutex_unlock(&adev->lock);
    778 }
    779 
    780 static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
    781 {
    782     struct adev_a2dp *adev = (struct adev_a2dp *)dev;
    783     struct str_parms *parms;
    784     char *str;
    785     char value[8];
    786     int ret;
    787 
    788     parms = str_parms_create_str(kvpairs);
    789 
    790     pthread_mutex_lock(&adev->lock);
    791 
    792     ret = str_parms_get_str(parms, BLUETOOOTH_ENABLED_PARM, value,
    793                             sizeof(value));
    794     if (ret >= 0) {
    795         adev->bt_enabled = !strcmp(value, "true");
    796         if (adev->output)
    797             _out_bt_enable(adev->output, adev->bt_enabled);
    798     }
    799 
    800     ret = str_parms_get_str(parms, A2DP_SUSPENDED_PARM, value, sizeof(value));
    801     if (ret >= 0) {
    802         adev->suspended = !strcmp(value, "true");
    803         if (adev->output)
    804             _out_a2dp_suspend(adev->output, adev->suspended);
    805     }
    806 
    807     pthread_mutex_unlock(&adev->lock);
    808 
    809     str_parms_destroy(parms);
    810 
    811     return ret;
    812 }
    813 
    814 static char * adev_get_parameters(const struct audio_hw_device *dev,
    815                                   const char *keys)
    816 {
    817     struct adev_a2dp *adev = (struct adev_a2dp *)dev;
    818     struct str_parms *parms;
    819     struct str_parms *out_parms;
    820     char *str;
    821     char value[8];
    822     int ret;
    823 
    824     parms = str_parms_create_str(keys);
    825     out_parms = str_parms_create();
    826 
    827     pthread_mutex_lock(&adev->lock);
    828 
    829     ret = str_parms_get_str(parms, BLUETOOOTH_ENABLED_PARM, value,
    830                             sizeof(value));
    831     if (ret >= 0)
    832         str_parms_add_str(out_parms, BLUETOOOTH_ENABLED_PARM,
    833                           adev->bt_enabled ? "true" : "false");
    834 
    835     ret = str_parms_get_str(parms, A2DP_SUSPENDED_PARM, value, sizeof(value));
    836     if (ret >= 0)
    837         str_parms_add_str(out_parms, A2DP_SUSPENDED_PARM,
    838                           adev->suspended ? "true" : "false");
    839 
    840     pthread_mutex_unlock(&adev->lock);
    841 
    842     str = str_parms_to_str(out_parms);
    843     str_parms_destroy(out_parms);
    844     str_parms_destroy(parms);
    845 
    846     return str;
    847 }
    848 
    849 static int adev_init_check(const struct audio_hw_device *dev)
    850 {
    851     return 0;
    852 }
    853 
    854 static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
    855 {
    856     return -ENOSYS;
    857 }
    858 
    859 static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
    860 {
    861     return -ENOSYS;
    862 }
    863 
    864 static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
    865 {
    866     /* TODO: do we care for the mode? */
    867     return 0;
    868 }
    869 
    870 static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
    871 {
    872     return -ENOSYS;
    873 }
    874 
    875 static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
    876 {
    877     return -ENOSYS;
    878 }
    879 
    880 #ifdef AUDIO_DEVICE_API_VERSION_1_0
    881 static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
    882                                          const struct audio_config *config)
    883 #else
    884 static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
    885                                          uint32_t sample_rate, audio_format_t format,
    886                                          int channel_count)
    887 #endif
    888 {
    889     /* no input */
    890     return 0;
    891 }
    892 
    893 #ifdef AUDIO_DEVICE_API_VERSION_1_0
    894 static int adev_open_input_stream(struct audio_hw_device *dev,
    895                                   audio_io_handle_t handle,
    896                                   audio_devices_t devices,
    897                                   struct audio_config *config,
    898                                   struct audio_stream_in **stream_in)
    899 #else
    900 static int adev_open_input_stream(struct audio_hw_device *dev, uint32_t devices,
    901                                   audio_format_t *format, uint32_t *channels,
    902                                   uint32_t *sample_rate,
    903                                   audio_in_acoustics_t acoustics,
    904                                   struct audio_stream_in **stream_in)
    905 #endif
    906 {
    907     return -ENOSYS;
    908 }
    909 
    910 static void adev_close_input_stream(struct audio_hw_device *dev,
    911                                    struct audio_stream_in *in)
    912 {
    913     return;
    914 }
    915 
    916 static int adev_dump(const audio_hw_device_t *device, int fd)
    917 {
    918     return 0;
    919 }
    920 
    921 static int adev_close(hw_device_t *device)
    922 {
    923     struct adev_a2dp *adev = (struct adev_a2dp *)device;
    924 
    925     pthread_mutex_lock(&adev->lock);
    926     if (adev->output)
    927         adev_close_output_stream_locked(adev, adev->output);
    928     pthread_mutex_unlock(&adev->lock);
    929     free(adev);
    930 
    931     return 0;
    932 }
    933 
    934 static uint32_t adev_get_supported_devices(const struct audio_hw_device *dev)
    935 {
    936     return AUDIO_DEVICE_OUT_ALL_A2DP;
    937 }
    938 
    939 static int adev_open(const hw_module_t* module, const char* name,
    940                      hw_device_t** device)
    941 {
    942     struct adev_a2dp *adev;
    943     int ret;
    944 
    945     if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
    946         return -EINVAL;
    947 
    948     adev = calloc(1, sizeof(struct adev_a2dp));
    949     if (!adev)
    950         return -ENOMEM;
    951 
    952     adev->bt_enabled = true;
    953     adev->suspended = false;
    954     pthread_mutex_init(&adev->lock, NULL);
    955     adev->output = NULL;
    956 
    957     adev->device.common.tag = HARDWARE_DEVICE_TAG;
    958 #ifdef AUDIO_DEVICE_API_VERSION_1_0
    959     adev->device.common.version = AUDIO_DEVICE_API_VERSION_1_0;
    960 #else
    961     adev->device.common.version = 0;
    962 #endif
    963     adev->device.common.module = (struct hw_module_t *) module;
    964     adev->device.common.close = adev_close;
    965 
    966     adev->device.get_supported_devices = adev_get_supported_devices;
    967     adev->device.init_check = adev_init_check;
    968     adev->device.set_voice_volume = adev_set_voice_volume;
    969     adev->device.set_master_volume = adev_set_master_volume;
    970     adev->device.set_mode = adev_set_mode;
    971     adev->device.set_mic_mute = adev_set_mic_mute;
    972     adev->device.get_mic_mute = adev_get_mic_mute;
    973     adev->device.set_parameters = adev_set_parameters;
    974     adev->device.get_parameters = adev_get_parameters;
    975     adev->device.get_input_buffer_size = adev_get_input_buffer_size;
    976     adev->device.open_output_stream = adev_open_output_stream;
    977     adev->device.close_output_stream = adev_close_output_stream;
    978     adev->device.open_input_stream = adev_open_input_stream;
    979     adev->device.close_input_stream = adev_close_input_stream;
    980     adev->device.dump = adev_dump;
    981 
    982     *device = &adev->device.common;
    983 
    984     return 0;
    985 
    986 err_str_parms_create:
    987     free(adev);
    988     return ret;
    989 }
    990 
    991 static struct hw_module_methods_t hal_module_methods = {
    992     .open = adev_open,
    993 };
    994 
    995 struct audio_module HAL_MODULE_INFO_SYM = {
    996     .common = {
    997         .tag = HARDWARE_MODULE_TAG,
    998 #ifdef AUDIO_MODULE_API_VERSION_0_1
    999         .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
   1000         .hal_api_version = HARDWARE_HAL_API_VERSION,
   1001 #else
   1002         .version_major = 1,
   1003         .version_minor = 0,
   1004 #endif
   1005         .id = AUDIO_HARDWARE_MODULE_ID,
   1006         .name = "A2DP Audio HW HAL",
   1007         .author = "The Android Open Source Project",
   1008         .methods = &hal_module_methods,
   1009     },
   1010 };
   1011 
   1012 
   1013