Home | History | Annotate | Download | only in libaudio
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #define LOG_TAG "AudioHAL:audio_hal_thunks"
     18 #include <utils/Log.h>
     19 
     20 #include <errno.h>
     21 #include <stdlib.h>
     22 
     23 #include <hardware/hardware.h>
     24 #include <hardware/audio.h>
     25 
     26 #include "AudioHardwareInput.h"
     27 #include "AudioHardwareOutput.h"
     28 #include "AudioStreamIn.h"
     29 #include "AudioStreamOut.h"
     30 
     31 namespace android {
     32 
     33 extern AudioHardwareInput gAudioHardwareInput;
     34 extern AudioHardwareOutput gAudioHardwareOutput;
     35 
     36 struct atv_audio_device {
     37     struct audio_hw_device device;
     38     AudioHardwareOutput* output;
     39     AudioHardwareInput* input;
     40 };
     41 
     42 struct atv_stream_out {
     43     struct audio_stream_out stream;
     44     AudioHardwareOutput* hw;
     45     AudioStreamOut* impl;
     46 };
     47 
     48 struct atv_stream_in {
     49     struct audio_stream_in stream;
     50     AudioStreamIn* impl;
     51 };
     52 
     53 /*******************************************************************************
     54  *
     55  * Audio output stream stubs.
     56  *
     57  ******************************************************************************/
     58 
     59 static int out_set_volume(struct audio_stream_out *stream,
     60                           float left,
     61                           float right)
     62 {
     63     (void) stream;
     64     (void) left;
     65     (void) right;
     66     return 0;
     67 }
     68 
     69 static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
     70 {
     71     (void) stream;
     72     (void) effect;
     73     return 0;
     74 }
     75 
     76 static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
     77 {
     78     (void) stream;
     79     (void) effect;
     80     return 0;
     81 }
     82 
     83 /*******************************************************************************
     84  *
     85  * Audio output stream implementation
     86  *
     87  ******************************************************************************/
     88 
     89 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
     90 {
     91     const struct atv_stream_out* tstream =
     92         reinterpret_cast<const struct atv_stream_out*>(stream);
     93 
     94     return tstream->impl->sampleRate();
     95 }
     96 
     97 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
     98 {
     99     const struct atv_stream_out* tstream =
    100         reinterpret_cast<const struct atv_stream_out*>(stream);
    101 
    102     if (rate != tstream->impl->sampleRate())
    103         return -EINVAL;
    104 
    105     return 0;
    106 }
    107 
    108 static size_t out_get_buffer_size(const struct audio_stream *stream)
    109 {
    110     const struct atv_stream_out* tstream =
    111         reinterpret_cast<const struct atv_stream_out*>(stream);
    112 
    113     return tstream->impl->bufferSize();
    114 }
    115 
    116 static audio_channel_mask_t out_get_channels(const struct audio_stream *stream)
    117 {
    118     const struct atv_stream_out* tstream =
    119         reinterpret_cast<const struct atv_stream_out*>(stream);
    120 
    121     return tstream->impl->chanMask();
    122 }
    123 
    124 static audio_format_t out_get_format(const struct audio_stream *stream)
    125 {
    126     const struct atv_stream_out* tstream =
    127         reinterpret_cast<const struct atv_stream_out*>(stream);
    128 
    129     return tstream->impl->format();
    130 }
    131 
    132 static int out_set_format(struct audio_stream *stream, audio_format_t format)
    133 {
    134     const struct atv_stream_out* tstream =
    135         reinterpret_cast<const struct atv_stream_out*>(stream);
    136 
    137     if (format != tstream->impl->format())
    138         return -EINVAL;
    139 
    140     return 0;
    141 }
    142 
    143 static uint32_t out_get_latency(const struct audio_stream_out *stream)
    144 {
    145     const struct atv_stream_out* tstream =
    146         reinterpret_cast<const struct atv_stream_out*>(stream);
    147 
    148     return tstream->impl->latency();
    149 }
    150 
    151 static int out_standby(struct audio_stream *stream)
    152 {
    153     struct atv_stream_out* tstream =
    154         reinterpret_cast<struct atv_stream_out*>(stream);
    155 
    156     return tstream->impl->standby();
    157 }
    158 
    159 static int out_dump(const struct audio_stream *stream, int fd)
    160 {
    161     const struct atv_stream_out* tstream =
    162         reinterpret_cast<const struct atv_stream_out*>(stream);
    163 
    164     return tstream->impl->dump(fd);
    165 }
    166 
    167 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
    168 {
    169     struct atv_stream_out* tstream =
    170         reinterpret_cast<struct atv_stream_out*>(stream);
    171 
    172     return tstream->impl->setParameters(stream, kvpairs);
    173 }
    174 
    175 static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
    176 {
    177     const struct atv_stream_out* tstream =
    178         reinterpret_cast<const struct atv_stream_out*>(stream);
    179 
    180     return tstream->impl->getParameters(keys);
    181 }
    182 
    183 static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
    184                          size_t bytes)
    185 {
    186     struct atv_stream_out* tstream =
    187         reinterpret_cast<struct atv_stream_out*>(stream);
    188 
    189     return tstream->impl->write(buffer, bytes);
    190 }
    191 
    192 static int out_get_render_position(const struct audio_stream_out *stream,
    193                                    uint32_t *dsp_frames)
    194 {
    195     const struct atv_stream_out* tstream =
    196         reinterpret_cast<const struct atv_stream_out*>(stream);
    197 
    198     return tstream->impl->getRenderPosition(dsp_frames);
    199 }
    200 
    201 static int out_get_presentation_position(const struct audio_stream_out *stream,
    202                                uint64_t *frames, struct timespec *timestamp)
    203 {
    204     const struct atv_stream_out* tstream =
    205         reinterpret_cast<const struct atv_stream_out*>(stream);
    206 
    207     return tstream->impl->getPresentationPosition(frames, timestamp);
    208 }
    209 
    210 static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
    211                                         int64_t *timestamp)
    212 {
    213     const struct atv_stream_out* tstream =
    214         reinterpret_cast<const struct atv_stream_out*>(stream);
    215 
    216     return tstream->impl->getNextWriteTimestamp(timestamp);
    217 }
    218 
    219 /*******************************************************************************
    220  *
    221  * Audio input stream implementation
    222  *
    223  ******************************************************************************/
    224 
    225 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
    226 {
    227     const struct atv_stream_in* tstream =
    228         reinterpret_cast<const struct atv_stream_in*>(stream);
    229 
    230     return tstream->impl->getSampleRate();
    231 }
    232 
    233 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
    234 {
    235     const struct atv_stream_in* tstream =
    236         reinterpret_cast<const struct atv_stream_in*>(stream);
    237 
    238     return tstream->impl->setSampleRate(rate);
    239 }
    240 
    241 static size_t in_get_buffer_size(const struct audio_stream *stream)
    242 {
    243     const struct atv_stream_in* tstream =
    244         reinterpret_cast<const struct atv_stream_in*>(stream);
    245 
    246     return tstream->impl->getBufferSize();
    247 }
    248 
    249 static uint32_t in_get_channels(const struct audio_stream *stream)
    250 {
    251     const struct atv_stream_in* tstream =
    252         reinterpret_cast<const struct atv_stream_in*>(stream);
    253 
    254     return tstream->impl->getChannelMask();
    255 }
    256 
    257 static audio_format_t in_get_format(const struct audio_stream *stream)
    258 {
    259     const struct atv_stream_in* tstream =
    260         reinterpret_cast<const struct atv_stream_in*>(stream);
    261 
    262     return tstream->impl->getFormat();
    263 }
    264 
    265 static int in_set_format(struct audio_stream *stream, audio_format_t format)
    266 {
    267     const struct atv_stream_in* tstream =
    268         reinterpret_cast<const struct atv_stream_in*>(stream);
    269 
    270     return tstream->impl->setFormat(format);
    271 }
    272 
    273 static int in_standby(struct audio_stream *stream)
    274 {
    275     const struct atv_stream_in* tstream =
    276         reinterpret_cast<const struct atv_stream_in*>(stream);
    277 
    278     return tstream->impl->standby();
    279 }
    280 
    281 static int in_dump(const struct audio_stream *stream, int fd)
    282 {
    283     const struct atv_stream_in* tstream =
    284         reinterpret_cast<const struct atv_stream_in*>(stream);
    285 
    286     return tstream->impl->dump(fd);
    287 }
    288 
    289 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
    290 {
    291     const struct atv_stream_in* tstream =
    292         reinterpret_cast<const struct atv_stream_in*>(stream);
    293 
    294     return tstream->impl->setParameters(stream, kvpairs);
    295 }
    296 
    297 static char* in_get_parameters(const struct audio_stream *stream,
    298                                const char *keys)
    299 {
    300     const struct atv_stream_in* tstream =
    301         reinterpret_cast<const struct atv_stream_in*>(stream);
    302 
    303     return tstream->impl->getParameters(keys);
    304 }
    305 
    306 static int in_set_gain(struct audio_stream_in *stream, float gain)
    307 {
    308     const struct atv_stream_in* tstream =
    309         reinterpret_cast<const struct atv_stream_in*>(stream);
    310 
    311     return tstream->impl->setGain(gain);
    312 }
    313 
    314 static ssize_t in_read(struct audio_stream_in *stream, void* buffer,
    315                        size_t bytes)
    316 {
    317     const struct atv_stream_in* tstream =
    318         reinterpret_cast<const struct atv_stream_in*>(stream);
    319 
    320     return tstream->impl->read(buffer, bytes);
    321 }
    322 
    323 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
    324 {
    325     const struct atv_stream_in* tstream =
    326         reinterpret_cast<const struct atv_stream_in*>(stream);
    327 
    328     return tstream->impl->getInputFramesLost();
    329 }
    330 
    331 static int in_add_audio_effect(const struct audio_stream *stream,
    332                                effect_handle_t effect)
    333 {
    334     const struct atv_stream_in* tstream =
    335         reinterpret_cast<const struct atv_stream_in*>(stream);
    336 
    337     return tstream->impl->addAudioEffect(effect);
    338 }
    339 
    340 static int in_remove_audio_effect(const struct audio_stream *stream,
    341                                   effect_handle_t effect)
    342 {
    343     const struct atv_stream_in* tstream =
    344         reinterpret_cast<const struct atv_stream_in*>(stream);
    345 
    346     return tstream->impl->removeAudioEffect(effect);
    347 }
    348 
    349 /*******************************************************************************
    350  *
    351  * Audio device stubs
    352  *
    353  ******************************************************************************/
    354 
    355 static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
    356 {
    357     const struct atv_audio_device* adev =
    358         reinterpret_cast<const struct atv_audio_device*>(dev);
    359 
    360     return adev->output->setParameters(kvpairs);
    361 
    362 }
    363 
    364 static char * adev_get_parameters(const struct audio_hw_device *dev,
    365                                   const char *keys)
    366 {
    367     const struct atv_audio_device* adev =
    368         reinterpret_cast<const struct atv_audio_device*>(dev);
    369 
    370     return adev->output->getParameters(keys);
    371 }
    372 
    373 static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
    374 {
    375     (void) dev;
    376     (void) volume;
    377     return 0;
    378 }
    379 
    380 static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
    381 {
    382     (void) dev;
    383     (void) mode;
    384     return 0;
    385 }
    386 
    387 static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
    388 {
    389     const struct atv_audio_device* adev =
    390         reinterpret_cast<struct atv_audio_device*>(dev);
    391 
    392     return adev->input->setMicMute(state);
    393 }
    394 
    395 static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
    396 {
    397     const struct atv_audio_device* adev =
    398         reinterpret_cast<const struct atv_audio_device*>(dev);
    399 
    400     return adev->input->getMicMute(state);
    401 }
    402 
    403 static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
    404                                          const audio_config *config)
    405 {
    406     const struct atv_audio_device* adev =
    407         reinterpret_cast<const struct atv_audio_device*>(dev);
    408 
    409     return adev->input->getInputBufferSize(config);
    410 }
    411 
    412 static int adev_open_input_stream(struct audio_hw_device *dev,
    413                                   audio_io_handle_t handle,
    414                                   audio_devices_t devices,
    415                                   struct audio_config *config,
    416                                   struct audio_stream_in **stream_in,
    417                                   audio_input_flags_t flags,
    418                                   const char *address __unused,
    419                                   audio_source_t source __unused)
    420 {
    421     (void) handle;
    422     (void) flags;
    423     int ret = 0;
    424     struct atv_audio_device* adev =
    425         reinterpret_cast<struct atv_audio_device*>(dev);
    426     struct atv_stream_in *in = NULL;
    427 
    428     *stream_in = NULL;
    429 
    430     in = reinterpret_cast<struct atv_stream_in*>(
    431             calloc(1, sizeof(struct atv_stream_in)));
    432     if (!in)  {
    433         ret = -ENOMEM;
    434         goto bailout;
    435     }
    436 
    437     in->stream.common.get_sample_rate = in_get_sample_rate;
    438     in->stream.common.set_sample_rate = in_set_sample_rate;
    439     in->stream.common.get_buffer_size = in_get_buffer_size;
    440     in->stream.common.get_channels = in_get_channels;
    441     in->stream.common.get_format = in_get_format;
    442     in->stream.common.set_format = in_set_format;
    443     in->stream.common.standby = in_standby;
    444     in->stream.common.dump = in_dump;
    445     in->stream.common.set_parameters = in_set_parameters;
    446     in->stream.common.get_parameters = in_get_parameters;
    447     in->stream.common.add_audio_effect = in_add_audio_effect;
    448     in->stream.common.remove_audio_effect = in_remove_audio_effect;
    449     in->stream.set_gain = in_set_gain;
    450     in->stream.read = in_read;
    451     in->stream.get_input_frames_lost = in_get_input_frames_lost;
    452 
    453     in->impl = adev->input->openInputStream(devices,
    454                                             &config->format,
    455                                             &config->channel_mask,
    456                                             &config->sample_rate,
    457                                             reinterpret_cast<status_t*>(&ret));
    458     if (NULL == in->impl) {
    459         ret = -ENOMEM;
    460         goto bailout;
    461     }
    462 
    463     if (0 == ret)
    464         *stream_in = &in->stream;
    465 
    466 bailout:
    467     if ((0 != ret) && (NULL != in)) {
    468         delete in->impl;
    469         free(in);
    470     }
    471 
    472     return ret;
    473 }
    474 
    475 static void adev_close_input_stream(struct audio_hw_device *dev,
    476                                     struct audio_stream_in *stream)
    477 {
    478     struct atv_audio_device* adev =
    479         reinterpret_cast<struct atv_audio_device*>(dev);
    480     struct atv_stream_in* tstream =
    481         reinterpret_cast<struct atv_stream_in*>(stream);
    482 
    483     adev->input->closeInputStream(tstream->impl);
    484     free(stream);
    485 }
    486 
    487 static int adev_open_output_stream(struct audio_hw_device *dev,
    488                                    audio_io_handle_t handle,
    489                                    audio_devices_t devices,
    490                                    audio_output_flags_t flags,
    491                                    struct audio_config *config,
    492                                    struct audio_stream_out **stream_out,
    493                                    const char *address __unused)
    494 {
    495     (void) handle;
    496     int ret = 0;
    497     struct atv_audio_device* adev =
    498         reinterpret_cast<struct atv_audio_device*>(dev);
    499     struct atv_stream_out *out = NULL;
    500 
    501     *stream_out = NULL;
    502 
    503     out = reinterpret_cast<struct atv_stream_out*>(
    504             calloc(1, sizeof(struct atv_stream_out)));
    505     if (!out)  {
    506         ret = -ENOMEM;
    507         goto bailout;
    508     }
    509 
    510     out->stream.common.get_sample_rate = out_get_sample_rate;
    511     out->stream.common.set_sample_rate = out_set_sample_rate;
    512     out->stream.common.get_buffer_size = out_get_buffer_size;
    513     out->stream.common.get_channels = out_get_channels;
    514     out->stream.common.get_format = out_get_format;
    515     out->stream.common.set_format = out_set_format;
    516     out->stream.common.standby = out_standby;
    517     out->stream.common.dump = out_dump;
    518     out->stream.common.set_parameters = out_set_parameters;
    519     out->stream.common.get_parameters = out_get_parameters;
    520     out->stream.common.add_audio_effect = out_add_audio_effect;
    521     out->stream.common.remove_audio_effect = out_remove_audio_effect;
    522     out->stream.get_latency = out_get_latency;
    523     out->stream.set_volume = out_set_volume;
    524     out->stream.write = out_write;
    525     out->stream.get_render_position = out_get_render_position;
    526     out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
    527     out->stream.get_presentation_position = out_get_presentation_position;
    528 
    529     out->impl = adev->output->openOutputStream(
    530             devices,
    531             &config->format,
    532             &config->channel_mask,
    533             &config->sample_rate,
    534             flags,
    535             reinterpret_cast<status_t*>(&ret));
    536 
    537     if (NULL == out->impl) {
    538         ret = -ENOMEM;
    539         goto bailout;
    540     }
    541 
    542     if (0 == ret) {
    543         out->hw = adev->output;
    544         *stream_out = &out->stream;
    545     }
    546 
    547 bailout:
    548     if ((0 != ret) && (NULL != out)) {
    549         delete out->impl;
    550         free(out);
    551     }
    552 
    553     return ret;
    554 }
    555 
    556 static void adev_close_output_stream(struct audio_hw_device *dev,
    557                                      struct audio_stream_out *stream)
    558 {
    559     struct atv_audio_device* adev =
    560         reinterpret_cast<struct atv_audio_device*>(dev);
    561     struct atv_stream_out* tstream =
    562         reinterpret_cast<struct atv_stream_out*>(stream);
    563 
    564     adev->output->closeOutputStream(tstream->impl);
    565     free(stream);
    566 }
    567 
    568 static int adev_init_check(const struct audio_hw_device *dev)
    569 {
    570     const struct atv_audio_device* adev =
    571         reinterpret_cast<const struct atv_audio_device*>(dev);
    572 
    573     return adev->output->initCheck();
    574 }
    575 
    576 static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
    577 {
    578     struct atv_audio_device* adev =
    579         reinterpret_cast<struct atv_audio_device*>(dev);
    580 
    581     return adev->output->setMasterVolume(volume);
    582 }
    583 
    584 static int adev_get_master_volume(struct audio_hw_device *dev,
    585                                   float *volume)
    586 {
    587     struct atv_audio_device* adev =
    588         reinterpret_cast<struct atv_audio_device*>(dev);
    589 
    590     return adev->output->getMasterVolume(volume);
    591 }
    592 
    593 static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
    594 {
    595     struct atv_audio_device* adev =
    596         reinterpret_cast<struct atv_audio_device*>(dev);
    597 
    598     return adev->output->setMasterMute(muted);
    599 }
    600 
    601 static int adev_get_master_mute(struct audio_hw_device *dev,
    602                                 bool *muted)
    603 {
    604     struct atv_audio_device* adev =
    605         reinterpret_cast<struct atv_audio_device*>(dev);
    606 
    607     return adev->output->getMasterMute(muted);
    608 }
    609 
    610 static int adev_dump(const audio_hw_device* dev, int fd)
    611 {
    612     const struct atv_audio_device* adev =
    613         reinterpret_cast<const struct atv_audio_device*>(dev);
    614 
    615     int ret = adev->output->dump(fd);
    616     if (ret == 0) {
    617         ret = adev->input->dump(fd);
    618     }
    619     return ret;
    620 }
    621 
    622 static int adev_close(hw_device_t *device)
    623 {
    624     struct atv_audio_device* adev =
    625         reinterpret_cast<struct atv_audio_device*>(device);
    626 
    627     free(device);
    628 
    629     return 0;
    630 }
    631 
    632 static int atv_audiodev_open_cpp(
    633         const hw_module_t* module,
    634         const char* name,
    635         hw_device_t** device)
    636 {
    637     struct atv_audio_device* adev = NULL;
    638     int ret = 0;
    639 
    640     if (NULL == device) {
    641         ret = -EINVAL;
    642         goto bailout;
    643     }
    644 
    645     *device = NULL;
    646 
    647     if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) {
    648         ret = -EINVAL;
    649         goto bailout;
    650     }
    651 
    652     adev = (struct atv_audio_device*)calloc(1,
    653             sizeof(struct atv_audio_device));
    654     if (NULL == adev) {
    655         ret = -ENOMEM;
    656         goto bailout;
    657     }
    658 
    659     adev->device.common.tag = HARDWARE_DEVICE_TAG;
    660     adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
    661     adev->device.common.module = (struct hw_module_t *) module;
    662     adev->device.common.close = adev_close;
    663 
    664     adev->device.init_check = adev_init_check;
    665     adev->device.set_voice_volume = adev_set_voice_volume;
    666     adev->device.set_master_volume = adev_set_master_volume;
    667     adev->device.get_master_volume = adev_get_master_volume;
    668     adev->device.set_master_mute = adev_set_master_mute;
    669     adev->device.get_master_mute = adev_get_master_mute;
    670     adev->device.set_mode = adev_set_mode;
    671     adev->device.set_mic_mute = adev_set_mic_mute;
    672     adev->device.get_mic_mute = adev_get_mic_mute;
    673     adev->device.set_parameters = adev_set_parameters;
    674     adev->device.get_parameters = adev_get_parameters;
    675     adev->device.get_input_buffer_size = adev_get_input_buffer_size;
    676     adev->device.open_output_stream = adev_open_output_stream;
    677     adev->device.close_output_stream = adev_close_output_stream;
    678     adev->device.open_input_stream = adev_open_input_stream;
    679     adev->device.close_input_stream = adev_close_input_stream;
    680     adev->device.dump = adev_dump;
    681 
    682     adev->output = &gAudioHardwareOutput;
    683     adev->input = &gAudioHardwareInput;
    684     *device = &adev->device.common;
    685 
    686 bailout:
    687     if ((0 != ret) && (NULL != adev)) {
    688         free(adev);
    689     }
    690 
    691     return 0;
    692 }
    693 
    694 }  // namespace android
    695 
    696 extern "C" int atv_audiodev_open(const hw_module_t* module,
    697                              const char* name,
    698                              hw_device_t** device) {
    699     return android::atv_audiodev_open_cpp(module, name, device);
    700 }
    701