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 ((PLAYBACK_PERIOD_SIZE * PLAYBACK_PERIOD_COUNT) - 1) 140 #define PLAYBACK_STOP_THRESHOLD (PLAYBACK_PERIOD_SIZE * PLAYBACK_PERIOD_COUNT) 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_DEFAULT_CHANNEL_COUNT 2 167 #define CAPTURE_DEFAULT_SAMPLING_RATE 48000 168 #define CAPTURE_START_THRESHOLD 1 169 170 #define COMPRESS_CARD 2 171 #define COMPRESS_DEVICE 0 172 #define COMPRESS_OFFLOAD_FRAGMENT_SIZE (32 * 1024) 173 #define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4 174 /* ToDo: Check and update a proper value in msec */ 175 #define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96 176 #define COMPRESS_PLAYBACK_VOLUME_MAX 0x10000 //NV suggested value 177 178 #define DEEP_BUFFER_OUTPUT_SAMPLING_RATE 48000 179 #define DEEP_BUFFER_OUTPUT_PERIOD_SIZE 480 180 #define DEEP_BUFFER_OUTPUT_PERIOD_COUNT 8 181 182 #define MAX_SUPPORTED_CHANNEL_MASKS 2 183 184 typedef int snd_device_t; 185 186 /* These are the supported use cases by the hardware. 187 * Each usecase is mapped to a specific PCM device. 188 * Refer to pcm_device_table[]. 189 */ 190 typedef enum { 191 USECASE_INVALID = -1, 192 /* Playback usecases */ 193 USECASE_AUDIO_PLAYBACK = 0, 194 USECASE_AUDIO_PLAYBACK_MULTI_CH, 195 USECASE_AUDIO_PLAYBACK_OFFLOAD, 196 USECASE_AUDIO_PLAYBACK_DEEP_BUFFER, 197 198 /* Capture usecases */ 199 USECASE_AUDIO_CAPTURE, 200 USECASE_AUDIO_CAPTURE_HOTWORD, 201 202 USECASE_VOICE_CALL, 203 AUDIO_USECASE_MAX 204 } audio_usecase_t; 205 206 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) 207 208 /* 209 * tinyAlsa library interprets period size as number of frames 210 * one frame = channel_count * sizeof (pcm sample) 211 * so if format = 16-bit PCM and channels = Stereo, frame size = 2 ch * 2 = 4 bytes 212 * DEEP_BUFFER_OUTPUT_PERIOD_SIZE = 1024 means 1024 * 4 = 4096 bytes 213 * We should take care of returning proper size when AudioFlinger queries for 214 * the buffer size of an input/output stream 215 */ 216 217 enum { 218 OFFLOAD_CMD_EXIT, /* exit compress offload thread loop*/ 219 OFFLOAD_CMD_DRAIN, /* send a full drain request to DSP */ 220 OFFLOAD_CMD_PARTIAL_DRAIN, /* send a partial drain request to DSP */ 221 OFFLOAD_CMD_WAIT_FOR_BUFFER, /* wait for buffer released by DSP */ 222 }; 223 224 enum { 225 OFFLOAD_STATE_IDLE, 226 OFFLOAD_STATE_PLAYING, 227 OFFLOAD_STATE_PAUSED, 228 OFFLOAD_STATE_PAUSED_FLUSHED, 229 }; 230 231 typedef enum { 232 PCM_PLAYBACK = 0x1, 233 PCM_CAPTURE = 0x2, 234 VOICE_CALL = 0x4, 235 PCM_HOTWORD_STREAMING = 0x8 236 } usecase_type_t; 237 238 struct offload_cmd { 239 struct listnode node; 240 int cmd; 241 int data[]; 242 }; 243 244 struct pcm_device_profile { 245 struct pcm_config config; 246 int card; 247 int id; 248 usecase_type_t type; 249 audio_devices_t devices; 250 }; 251 252 struct pcm_device { 253 struct listnode stream_list_node; 254 struct pcm_device_profile* pcm_profile; 255 struct pcm* pcm; 256 int status; 257 /* TODO: remove resampler if possible when AudioFlinger supports downsampling from 48 to 8 */ 258 struct resampler_itfe* resampler; 259 int16_t* res_buffer; 260 size_t res_byte_count; 261 int sound_trigger_handle; 262 }; 263 264 struct stream_out { 265 struct audio_stream_out stream; 266 pthread_mutex_t lock; /* see note below on mutex acquisition order */ 267 pthread_cond_t cond; 268 struct pcm_config config; 269 struct listnode pcm_dev_list; 270 struct compr_config compr_config; 271 struct compress* compr; 272 int standby; 273 unsigned int sample_rate; 274 audio_channel_mask_t channel_mask; 275 audio_format_t format; 276 audio_devices_t devices; 277 audio_output_flags_t flags; 278 audio_usecase_t usecase; 279 /* Array of supported channel mask configurations. +1 so that the last entry is always 0 */ 280 audio_channel_mask_t supported_channel_masks[MAX_SUPPORTED_CHANNEL_MASKS + 1]; 281 bool muted; 282 /* total frames written, not cleared when entering standby */ 283 uint64_t written; 284 audio_io_handle_t handle; 285 286 int non_blocking; 287 int offload_state; 288 pthread_cond_t offload_cond; 289 pthread_t offload_thread; 290 struct listnode offload_cmd_list; 291 bool offload_thread_blocked; 292 293 stream_callback_t offload_callback; 294 void* offload_cookie; 295 struct compr_gapless_mdata gapless_mdata; 296 int send_new_metadata; 297 298 struct audio_device* dev; 299 300 #ifdef PREPROCESSING_ENABLED 301 struct echo_reference_itfe *echo_reference; 302 // echo_reference_generation indicates if the echo reference used by the output stream is 303 // in sync with the one known by the audio_device. When different from the generation stored 304 // in the audio_device the output stream must release the echo reference. 305 // always modified with audio device and stream mutex locked. 306 int32_t echo_reference_generation; 307 #endif 308 }; 309 310 struct stream_in { 311 struct audio_stream_in stream; 312 pthread_mutex_t lock; /* see note below on mutex acquisition order */ 313 struct pcm_config config; 314 struct listnode pcm_dev_list; 315 int standby; 316 audio_source_t source; 317 audio_devices_t devices; 318 uint32_t main_channels; 319 audio_usecase_t usecase; 320 bool enable_aec; 321 audio_input_flags_t input_flags; 322 323 /* TODO: remove resampler if possible when AudioFlinger supports downsampling from 48 to 8 */ 324 unsigned int requested_rate; 325 struct resampler_itfe* resampler; 326 struct resampler_buffer_provider buf_provider; 327 int read_status; 328 int16_t* read_buf; 329 size_t read_buf_size; 330 size_t read_buf_frames; 331 332 int16_t *proc_buf_in; 333 int16_t *proc_buf_out; 334 size_t proc_buf_size; 335 size_t proc_buf_frames; 336 337 #ifdef PREPROCESSING_ENABLED 338 struct echo_reference_itfe *echo_reference; 339 int16_t *ref_buf; 340 size_t ref_buf_size; 341 size_t ref_buf_frames; 342 343 #ifdef HW_AEC_LOOPBACK 344 bool hw_echo_reference; 345 int16_t* hw_ref_buf; 346 size_t hw_ref_buf_size; 347 #endif 348 349 int num_preprocessors; 350 struct effect_info_s preprocessors[MAX_PREPROCESSORS]; 351 352 bool aux_channels_changed; 353 uint32_t aux_channels; 354 #endif 355 356 struct audio_device* dev; 357 }; 358 359 struct mixer_card { 360 struct listnode adev_list_node; 361 struct listnode uc_list_node[AUDIO_USECASE_MAX]; 362 int card; 363 struct mixer* mixer; 364 struct audio_route* audio_route; 365 }; 366 367 struct audio_usecase { 368 struct listnode adev_list_node; 369 audio_usecase_t id; 370 usecase_type_t type; 371 audio_devices_t devices; 372 snd_device_t out_snd_device; 373 snd_device_t in_snd_device; 374 struct audio_stream* stream; 375 struct listnode mixer_list; 376 }; 377 378 379 struct audio_device { 380 struct audio_hw_device device; 381 pthread_mutex_t lock; /* see note below on mutex acquisition order */ 382 struct listnode mixer_list; 383 audio_mode_t mode; 384 struct stream_in* active_input; 385 struct stream_out* primary_output; 386 int in_call; 387 float voice_volume; 388 bool mic_mute; 389 int tty_mode; 390 bool bluetooth_nrec; 391 bool screen_off; 392 int* snd_dev_ref_cnt; 393 struct listnode usecase_list; 394 bool speaker_lr_swap; 395 unsigned int cur_hdmi_channels; 396 int dualmic_config; 397 bool ns_in_voice_rec; 398 399 void* offload_fx_lib; 400 int (*offload_fx_start_output)(audio_io_handle_t); 401 int (*offload_fx_stop_output)(audio_io_handle_t); 402 403 #ifdef PREPROCESSING_ENABLED 404 struct echo_reference_itfe* echo_reference; 405 // echo_reference_generation indicates if the echo reference used by the output stream is 406 // in sync with the one known by the audio_device. 407 // incremented atomically with a memory barrier and audio device mutex locked but WITHOUT 408 // stream mutex locked: the stream will load it atomically with a barrier and re-read it 409 // with audio device mutex if needed 410 volatile int32_t echo_reference_generation; 411 #endif 412 413 void* htc_acoustic_lib; 414 int (*htc_acoustic_init_rt5506)(); 415 int (*htc_acoustic_set_rt5506_amp)(int, int); 416 int (*htc_acoustic_set_amp_mode)(int, int, int, int, bool); 417 int (*htc_acoustic_spk_reverse)(bool); 418 419 void* sound_trigger_lib; 420 int (*sound_trigger_open_for_streaming)(); 421 size_t (*sound_trigger_read_samples)(int, void*, size_t); 422 int (*sound_trigger_close_for_streaming)(int); 423 424 int tfa9895_init; 425 int tfa9895_mode_change; 426 pthread_mutex_t tfa9895_lock; 427 428 int dummybuf_thread_timeout; 429 int dummybuf_thread_cancel; 430 int dummybuf_thread_active; 431 audio_devices_t dummybuf_thread_devices; 432 pthread_mutex_t dummybuf_thread_lock; 433 pthread_t dummybuf_thread; 434 435 pthread_mutex_t lock_inputs; /* see note below on mutex acquisition order */ 436 }; 437 438 /* 439 * NOTE: when multiple mutexes have to be acquired, always take the 440 * lock_inputs, stream_in, stream_out, audio_device, then tfa9895 mutex. 441 * stream_in mutex must always be before stream_out mutex 442 * if both have to be taken (see get_echo_reference(), put_echo_reference()...) 443 * dummybuf_thread mutex is not related to the other mutexes with respect to order. 444 * lock_inputs must be held in order to either close the input stream, or prevent closure. 445 */ 446 447 #endif // NVIDIA_AUDIO_HW_H 448