Home | History | Annotate | Download | only in hal
      1 /*
      2  * Copyright (C) 2013 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 #ifndef NVIDIA_AUDIO_HW_H
     18 #define NVIDIA_AUDIO_HW_H
     19 
     20 #include <cutils/list.h>
     21 #include <hardware/audio.h>
     22 
     23 #include <tinyalsa/asoundlib.h>
     24 #include <tinycompress/tinycompress.h>
     25 /* TODO: remove resampler if possible when AudioFlinger supports downsampling from 48 to 8 */
     26 #include <audio_utils/resampler.h>
     27 #include <audio_route/audio_route.h>
     28 
     29 /* Retry for delay in FW loading*/
     30 #define RETRY_NUMBER 10
     31 #define RETRY_US 500000
     32 
     33 #ifdef __LP64__
     34 #define OFFLOAD_FX_LIBRARY_PATH "/system/lib64/soundfx/libnvvisualizer.so"
     35 #else
     36 #define OFFLOAD_FX_LIBRARY_PATH "/system/lib/soundfx/libnvvisualizer.so"
     37 #endif
     38 
     39 #define HTC_ACOUSTIC_LIBRARY_PATH "/vendor/lib/libhtcacoustic.so"
     40 
     41 #ifdef PREPROCESSING_ENABLED
     42 #include <audio_utils/echo_reference.h>
     43 #define MAX_PREPROCESSORS 3
     44 struct effect_info_s {
     45     effect_handle_t effect_itfe;
     46     size_t num_channel_configs;
     47     channel_config_t *channel_configs;
     48 };
     49 #endif
     50 
     51 #ifdef __LP64__
     52 #define SOUND_TRIGGER_HAL_LIBRARY_PATH "/system/lib64/hw/sound_trigger.primary.flounder.so"
     53 #else
     54 #define SOUND_TRIGGER_HAL_LIBRARY_PATH "/system/lib/hw/sound_trigger.primary.flounder.so"
     55 #endif
     56 
     57 #define TTY_MODE_OFF    1
     58 #define TTY_MODE_FULL   2
     59 #define TTY_MODE_VCO    4
     60 #define TTY_MODE_HCO    8
     61 
     62 #define DUALMIC_CONFIG_NONE 0
     63 #define DUALMIC_CONFIG_1 1
     64 
     65 /* Sound devices specific to the platform
     66  * The DEVICE_OUT_* and DEVICE_IN_* should be mapped to these sound
     67  * devices to enable corresponding mixer paths
     68  */
     69 enum {
     70     SND_DEVICE_NONE = 0,
     71 
     72     /* Playback devices */
     73     SND_DEVICE_MIN,
     74     SND_DEVICE_OUT_BEGIN = SND_DEVICE_MIN,
     75     SND_DEVICE_OUT_HANDSET = SND_DEVICE_OUT_BEGIN,
     76     SND_DEVICE_OUT_SPEAKER,
     77     SND_DEVICE_OUT_HEADPHONES,
     78     SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES,
     79     SND_DEVICE_OUT_VOICE_HANDSET,
     80     SND_DEVICE_OUT_VOICE_SPEAKER,
     81     SND_DEVICE_OUT_VOICE_HEADPHONES,
     82     SND_DEVICE_OUT_HDMI,
     83     SND_DEVICE_OUT_SPEAKER_AND_HDMI,
     84     SND_DEVICE_OUT_BT_SCO,
     85     SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES,
     86     SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES,
     87     SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET,
     88     SND_DEVICE_OUT_END,
     89 
     90     /*
     91      * Note: IN_BEGIN should be same as OUT_END because total number of devices
     92      * SND_DEVICES_MAX should not exceed MAX_RX + MAX_TX devices.
     93      */
     94     /* Capture devices */
     95     SND_DEVICE_IN_BEGIN = SND_DEVICE_OUT_END,
     96     SND_DEVICE_IN_HANDSET_MIC  = SND_DEVICE_IN_BEGIN,
     97     SND_DEVICE_IN_SPEAKER_MIC,
     98     SND_DEVICE_IN_HEADSET_MIC,
     99     SND_DEVICE_IN_HANDSET_MIC_AEC,
    100     SND_DEVICE_IN_SPEAKER_MIC_AEC,
    101     SND_DEVICE_IN_HEADSET_MIC_AEC,
    102     SND_DEVICE_IN_VOICE_SPEAKER_MIC,
    103     SND_DEVICE_IN_VOICE_HEADSET_MIC,
    104     SND_DEVICE_IN_HDMI_MIC,
    105     SND_DEVICE_IN_BT_SCO_MIC,
    106     SND_DEVICE_IN_CAMCORDER_MIC,
    107     SND_DEVICE_IN_VOICE_DMIC_1,
    108     SND_DEVICE_IN_VOICE_SPEAKER_DMIC_1,
    109     SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC,
    110     SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC,
    111     SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC,
    112     SND_DEVICE_IN_VOICE_REC_HEADSET_MIC,
    113     SND_DEVICE_IN_VOICE_REC_MIC,
    114     SND_DEVICE_IN_VOICE_REC_DMIC_1,
    115     SND_DEVICE_IN_VOICE_REC_DMIC_NS_1,
    116     SND_DEVICE_IN_LOOPBACK_AEC,
    117     SND_DEVICE_IN_END,
    118 
    119     SND_DEVICE_MAX = SND_DEVICE_IN_END,
    120 
    121 };
    122 
    123 
    124 #define MIXER_CARD 0
    125 #define SOUND_CARD 0
    126 
    127 /*
    128  * tinyAlsa library interprets period size as number of frames
    129  * one frame = channel_count * sizeof (pcm sample)
    130  * so if format = 16-bit PCM and channels = Stereo, frame size = 2 ch * 2 = 4 bytes
    131  * DEEP_BUFFER_OUTPUT_PERIOD_SIZE = 1024 means 1024 * 4 = 4096 bytes
    132  * We should take care of returning proper size when AudioFlinger queries for
    133  * the buffer size of an input/output stream
    134  */
    135 #define PLAYBACK_PERIOD_SIZE 256
    136 #define PLAYBACK_PERIOD_COUNT 2
    137 #define PLAYBACK_DEFAULT_CHANNEL_COUNT 2
    138 #define PLAYBACK_DEFAULT_SAMPLING_RATE 48000
    139 #define PLAYBACK_START_THRESHOLD(size, count) (((size) * (count)) - 1)
    140 #define PLAYBACK_STOP_THRESHOLD(size, count) ((size) * ((count) + 2))
    141 #define PLAYBACK_AVAILABLE_MIN 1
    142 
    143 
    144 #define SCO_PERIOD_SIZE 168
    145 #define SCO_PERIOD_COUNT 2
    146 #define SCO_DEFAULT_CHANNEL_COUNT 2
    147 #define SCO_DEFAULT_SAMPLING_RATE 8000
    148 #define SCO_START_THRESHOLD 335
    149 #define SCO_STOP_THRESHOLD 336
    150 #define SCO_AVAILABLE_MIN 1
    151 
    152 #define PLAYBACK_HDMI_MULTI_PERIOD_SIZE  1024
    153 #define PLAYBACK_HDMI_MULTI_PERIOD_COUNT 4
    154 #define PLAYBACK_HDMI_MULTI_DEFAULT_CHANNEL_COUNT 6
    155 #define PLAYBACK_HDMI_MULTI_PERIOD_BYTES \
    156     (PLAYBACK_HDMI_MULTI_PERIOD_SIZE * PLAYBACK_HDMI_MULTI_DEFAULT_CHANNEL_COUNT * 2)
    157 #define PLAYBACK_HDMI_MULTI_START_THRESHOLD 4095
    158 #define PLAYBACK_HDMI_MULTI_STOP_THRESHOLD 4096
    159 #define PLAYBACK_HDMI_MULTI_AVAILABLE_MIN 1
    160 
    161 #define PLAYBACK_HDMI_DEFAULT_CHANNEL_COUNT   2
    162 
    163 #define CAPTURE_PERIOD_SIZE 1024
    164 #define CAPTURE_PERIOD_SIZE_LOW_LATENCY 256
    165 #define CAPTURE_PERIOD_COUNT 2
    166 #define CAPTURE_PERIOD_COUNT_LOW_LATENCY 2
    167 #define CAPTURE_DEFAULT_CHANNEL_COUNT 2
    168 #define CAPTURE_DEFAULT_SAMPLING_RATE 48000
    169 #define CAPTURE_START_THRESHOLD 1
    170 
    171 #define COMPRESS_CARD       0
    172 #define COMPRESS_DEVICE     5
    173 #define COMPRESS_OFFLOAD_FRAGMENT_SIZE (32 * 1024)
    174 #define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4
    175 /* ToDo: Check and update a proper value in msec */
    176 #define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96
    177 #define COMPRESS_PLAYBACK_VOLUME_MAX 0x10000 //NV suggested value
    178 
    179 #define DEEP_BUFFER_OUTPUT_SAMPLING_RATE 48000
    180 #define DEEP_BUFFER_OUTPUT_PERIOD_SIZE 480
    181 #define DEEP_BUFFER_OUTPUT_PERIOD_COUNT 8
    182 
    183 #define MAX_SUPPORTED_CHANNEL_MASKS 2
    184 
    185 typedef int snd_device_t;
    186 
    187 /* These are the supported use cases by the hardware.
    188  * Each usecase is mapped to a specific PCM device.
    189  * Refer to pcm_device_table[].
    190  */
    191 typedef enum {
    192     USECASE_INVALID = -1,
    193     /* Playback usecases */
    194     USECASE_AUDIO_PLAYBACK = 0,
    195     USECASE_AUDIO_PLAYBACK_MULTI_CH,
    196     USECASE_AUDIO_PLAYBACK_OFFLOAD,
    197     USECASE_AUDIO_PLAYBACK_DEEP_BUFFER,
    198 
    199     /* Capture usecases */
    200     USECASE_AUDIO_CAPTURE,
    201     USECASE_AUDIO_CAPTURE_HOTWORD,
    202 
    203     USECASE_VOICE_CALL,
    204     AUDIO_USECASE_MAX
    205 } audio_usecase_t;
    206 
    207 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
    208 
    209 /*
    210  * tinyAlsa library interprets period size as number of frames
    211  * one frame = channel_count * sizeof (pcm sample)
    212  * so if format = 16-bit PCM and channels = Stereo, frame size = 2 ch * 2 = 4 bytes
    213  * DEEP_BUFFER_OUTPUT_PERIOD_SIZE = 1024 means 1024 * 4 = 4096 bytes
    214  * We should take care of returning proper size when AudioFlinger queries for
    215  * the buffer size of an input/output stream
    216  */
    217 
    218 enum {
    219     OFFLOAD_CMD_EXIT,               /* exit compress offload thread loop*/
    220     OFFLOAD_CMD_DRAIN,              /* send a full drain request to DSP */
    221     OFFLOAD_CMD_PARTIAL_DRAIN,      /* send a partial drain request to DSP */
    222     OFFLOAD_CMD_WAIT_FOR_BUFFER,    /* wait for buffer released by DSP */
    223 };
    224 
    225 enum {
    226     OFFLOAD_STATE_IDLE,
    227     OFFLOAD_STATE_PLAYING,
    228     OFFLOAD_STATE_PAUSED,
    229     OFFLOAD_STATE_PAUSED_FLUSHED,
    230 };
    231 
    232 typedef enum {
    233     PCM_PLAYBACK = 0x1,
    234     PCM_CAPTURE = 0x2,
    235     VOICE_CALL = 0x4,
    236     PCM_HOTWORD_STREAMING = 0x8,
    237     PCM_CAPTURE_LOW_LATENCY = 0x10,
    238 } usecase_type_t;
    239 
    240 struct offload_cmd {
    241     struct listnode node;
    242     int             cmd;
    243     int             data[];
    244 };
    245 
    246 struct pcm_device_profile {
    247     struct pcm_config config;
    248     int               card;
    249     int               id;
    250     usecase_type_t    type;
    251     audio_devices_t   devices;
    252 };
    253 
    254 struct pcm_device {
    255     struct listnode            stream_list_node;
    256     struct pcm_device_profile* pcm_profile;
    257     struct pcm*                pcm;
    258     int                        status;
    259     /* TODO: remove resampler if possible when AudioFlinger supports downsampling from 48 to 8 */
    260     struct resampler_itfe*     resampler;
    261     int16_t*                   res_buffer;
    262     size_t                     res_byte_count;
    263     int                        sound_trigger_handle;
    264 };
    265 
    266 struct stream_out {
    267     struct audio_stream_out     stream;
    268     pthread_mutex_t             lock; /* see note below on mutex acquisition order */
    269     pthread_mutex_t             pre_lock; /* acquire before lock to avoid DOS by playback thread */
    270     pthread_cond_t              cond;
    271     struct pcm_config           config;
    272     struct listnode             pcm_dev_list;
    273     struct compr_config         compr_config;
    274     struct compress*            compr;
    275     int                         standby;
    276     unsigned int                sample_rate;
    277     audio_channel_mask_t        channel_mask;
    278     audio_format_t              format;
    279     audio_devices_t             devices;
    280     audio_output_flags_t        flags;
    281     audio_usecase_t             usecase;
    282     /* Array of supported channel mask configurations. +1 so that the last entry is always 0 */
    283     audio_channel_mask_t        supported_channel_masks[MAX_SUPPORTED_CHANNEL_MASKS + 1];
    284     bool                        muted;
    285     /* total frames written, not cleared when entering standby */
    286     uint64_t                    written;
    287     audio_io_handle_t           handle;
    288 
    289     int                         non_blocking;
    290     int                         offload_state;
    291     pthread_cond_t              offload_cond;
    292     pthread_t                   offload_thread;
    293     struct listnode             offload_cmd_list;
    294     bool                        offload_thread_blocked;
    295 
    296     stream_callback_t           offload_callback;
    297     void*                       offload_cookie;
    298     struct compr_gapless_mdata  gapless_mdata;
    299     int                         send_new_metadata;
    300 
    301     struct audio_device*        dev;
    302 
    303 #ifdef PREPROCESSING_ENABLED
    304     struct echo_reference_itfe *echo_reference;
    305     // echo_reference_generation indicates if the echo reference used by the output stream is
    306     // in sync with the one known by the audio_device. When different from the generation stored
    307     // in the audio_device the output stream must release the echo reference.
    308     // always modified with audio device and stream mutex locked.
    309     int32_t echo_reference_generation;
    310 #endif
    311 
    312     bool                         is_fastmixer_affinity_set;
    313 };
    314 
    315 struct stream_in {
    316     struct audio_stream_in              stream;
    317     pthread_mutex_t                     lock; /* see note below on mutex acquisition order */
    318     pthread_mutex_t                     pre_lock; /* acquire before lock to avoid DOS by
    319                                                      capture thread */
    320     struct pcm_config                   config;
    321     struct listnode                     pcm_dev_list;
    322     int                                 standby;
    323     audio_source_t                      source;
    324     audio_devices_t                     devices;
    325     uint32_t                            main_channels;
    326     audio_usecase_t                     usecase;
    327     usecase_type_t                      usecase_type;
    328     bool                                enable_aec;
    329     audio_input_flags_t                 input_flags;
    330 
    331     /* TODO: remove resampler if possible when AudioFlinger supports downsampling from 48 to 8 */
    332     unsigned int                        requested_rate;
    333     struct resampler_itfe*              resampler;
    334     struct resampler_buffer_provider    buf_provider;
    335     int                                 read_status;
    336     int16_t*                            read_buf;
    337     size_t                              read_buf_size;
    338     size_t                              read_buf_frames;
    339 
    340     int16_t *proc_buf_in;
    341     int16_t *proc_buf_out;
    342     size_t proc_buf_size;
    343     size_t proc_buf_frames;
    344 
    345 #ifdef PREPROCESSING_ENABLED
    346     struct echo_reference_itfe *echo_reference;
    347     int16_t *ref_buf;
    348     size_t ref_buf_size;
    349     size_t ref_buf_frames;
    350 
    351 #ifdef HW_AEC_LOOPBACK
    352     bool hw_echo_reference;
    353     int16_t* hw_ref_buf;
    354     size_t hw_ref_buf_size;
    355 #endif
    356 
    357     int num_preprocessors;
    358     struct effect_info_s preprocessors[MAX_PREPROCESSORS];
    359 
    360     bool aux_channels_changed;
    361     uint32_t aux_channels;
    362 #endif
    363 
    364     struct audio_device*                dev;
    365     bool                                is_fastcapture_affinity_set;
    366 };
    367 
    368 struct mixer_card {
    369     struct listnode     adev_list_node;
    370     struct listnode     uc_list_node[AUDIO_USECASE_MAX];
    371     int                 card;
    372     struct mixer*       mixer;
    373     struct audio_route* audio_route;
    374 };
    375 
    376 struct audio_usecase {
    377     struct listnode         adev_list_node;
    378     audio_usecase_t         id;
    379     usecase_type_t          type;
    380     audio_devices_t         devices;
    381     snd_device_t            out_snd_device;
    382     snd_device_t            in_snd_device;
    383     struct audio_stream*    stream;
    384     struct listnode         mixer_list;
    385 };
    386 
    387 
    388 struct audio_device {
    389     struct audio_hw_device  device;
    390     pthread_mutex_t         lock; /* see note below on mutex acquisition order */
    391     struct listnode         mixer_list;
    392     audio_mode_t            mode;
    393     struct stream_in*       active_input;
    394     struct stream_out*      primary_output;
    395     int                     in_call;
    396     float                   voice_volume;
    397     bool                    mic_mute;
    398     int                     tty_mode;
    399     bool                    bluetooth_nrec;
    400     bool                    screen_off;
    401     int*                    snd_dev_ref_cnt;
    402     struct listnode         usecase_list;
    403     bool                    speaker_lr_swap;
    404     unsigned int            cur_hdmi_channels;
    405     int                     dualmic_config;
    406     bool                    ns_in_voice_rec;
    407 
    408     void*                   offload_fx_lib;
    409     int                     (*offload_fx_start_output)(audio_io_handle_t);
    410     int                     (*offload_fx_stop_output)(audio_io_handle_t);
    411 
    412 #ifdef PREPROCESSING_ENABLED
    413     struct echo_reference_itfe* echo_reference;
    414     // echo_reference_generation indicates if the echo reference used by the output stream is
    415     // in sync with the one known by the audio_device.
    416     // incremented atomically with a memory barrier and audio device mutex locked but WITHOUT
    417     // stream mutex locked: the stream will load it atomically with a barrier and re-read it
    418     // with audio device mutex if needed
    419     volatile int32_t        echo_reference_generation;
    420 #endif
    421 
    422     void*                   htc_acoustic_lib;
    423     int                     (*htc_acoustic_init_rt5506)();
    424     int                     (*htc_acoustic_set_rt5506_amp)(int, int);
    425     int                     (*htc_acoustic_set_amp_mode)(int, int, int, int, bool);
    426     int                     (*htc_acoustic_spk_reverse)(bool);
    427 
    428     void*                   sound_trigger_lib;
    429     int                     (*sound_trigger_open_for_streaming)();
    430     size_t                  (*sound_trigger_read_samples)(int, void*, size_t);
    431     int                     (*sound_trigger_close_for_streaming)(int);
    432 
    433     int                     tfa9895_init;
    434     int                     tfa9895_mode_change;
    435     pthread_mutex_t         tfa9895_lock;
    436 
    437     int                     dummybuf_thread_timeout;
    438     int                     dummybuf_thread_cancel;
    439     int                     dummybuf_thread_active;
    440     audio_devices_t         dummybuf_thread_devices;
    441     pthread_mutex_t         dummybuf_thread_lock;
    442     pthread_t               dummybuf_thread;
    443 
    444     pthread_mutex_t         lock_inputs; /* see note below on mutex acquisition order */
    445 };
    446 
    447 /*
    448  * NOTE: when multiple mutexes have to be acquired, always take the
    449  * lock_inputs, stream_in, stream_out, audio_device, then tfa9895 mutex.
    450  * stream_in mutex must always be before stream_out mutex
    451  * if both have to be taken (see get_echo_reference(), put_echo_reference()...)
    452  * dummybuf_thread mutex is not related to the other mutexes with respect to order.
    453  * lock_inputs must be held in order to either close the input stream, or prevent closure.
    454  */
    455 
    456 #endif // NVIDIA_AUDIO_HW_H
    457