blob: 9252e1c6dda4db51e61473b021312f77ba74bbcc [file] [log] [blame]
Christopher N. Hesse297a6362017-01-28 12:40:45 +01001/*
2 * Copyright (C) 2013 The Android Open Source Project
Christopher N. Hesse2f6f8582017-01-28 12:46:15 +01003 * Copyright (C) 2017 Christopher N. Hesse <raymanfx@gmail.com>
Christopher N. Hesse297a6362017-01-28 12:40:45 +01004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Christopher N. Hesse0612a4e2017-01-28 14:05:39 +010018#ifndef SAMSUNG_AUDIO_HW_H
19#define SAMSUNG_AUDIO_HW_H
Christopher N. Hesse297a6362017-01-28 12:40:45 +010020
21#include <cutils/list.h>
22#include <hardware/audio.h>
23
24#include <tinyalsa/asoundlib.h>
25#include <tinycompress/tinycompress.h>
26/* TODO: remove resampler if possible when AudioFlinger supports downsampling from 48 to 8 */
27#include <audio_utils/resampler.h>
28#include <audio_route/audio_route.h>
29
30/* Retry for delay in FW loading*/
31#define RETRY_NUMBER 10
32#define RETRY_US 500000
33
34#ifdef __LP64__
35#define OFFLOAD_FX_LIBRARY_PATH "/system/lib64/soundfx/libnvvisualizer.so"
36#else
37#define OFFLOAD_FX_LIBRARY_PATH "/system/lib/soundfx/libnvvisualizer.so"
38#endif
39
Christopher N. Hesse297a6362017-01-28 12:40:45 +010040#ifdef PREPROCESSING_ENABLED
41#include <audio_utils/echo_reference.h>
42#define MAX_PREPROCESSORS 3
43struct effect_info_s {
44 effect_handle_t effect_itfe;
45 size_t num_channel_configs;
46 channel_config_t *channel_configs;
47};
48#endif
49
50#ifdef __LP64__
51#define SOUND_TRIGGER_HAL_LIBRARY_PATH "/system/lib64/hw/sound_trigger.primary.flounder.so"
52#else
53#define SOUND_TRIGGER_HAL_LIBRARY_PATH "/system/lib/hw/sound_trigger.primary.flounder.so"
54#endif
55
56#define TTY_MODE_OFF 1
57#define TTY_MODE_FULL 2
58#define TTY_MODE_VCO 4
59#define TTY_MODE_HCO 8
60
61#define DUALMIC_CONFIG_NONE 0
62#define DUALMIC_CONFIG_1 1
63
64/* Sound devices specific to the platform
65 * The DEVICE_OUT_* and DEVICE_IN_* should be mapped to these sound
66 * devices to enable corresponding mixer paths
67 */
68enum {
69 SND_DEVICE_NONE = 0,
70
71 /* Playback devices */
72 SND_DEVICE_MIN,
73 SND_DEVICE_OUT_BEGIN = SND_DEVICE_MIN,
74 SND_DEVICE_OUT_HANDSET = SND_DEVICE_OUT_BEGIN,
75 SND_DEVICE_OUT_SPEAKER,
76 SND_DEVICE_OUT_HEADPHONES,
77 SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES,
78 SND_DEVICE_OUT_VOICE_HANDSET,
79 SND_DEVICE_OUT_VOICE_SPEAKER,
80 SND_DEVICE_OUT_VOICE_HEADPHONES,
81 SND_DEVICE_OUT_HDMI,
82 SND_DEVICE_OUT_SPEAKER_AND_HDMI,
83 SND_DEVICE_OUT_BT_SCO,
84 SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES,
85 SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES,
86 SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET,
87 SND_DEVICE_OUT_END,
88
89 /*
90 * Note: IN_BEGIN should be same as OUT_END because total number of devices
91 * SND_DEVICES_MAX should not exceed MAX_RX + MAX_TX devices.
92 */
93 /* Capture devices */
94 SND_DEVICE_IN_BEGIN = SND_DEVICE_OUT_END,
95 SND_DEVICE_IN_HANDSET_MIC = SND_DEVICE_IN_BEGIN,
96 SND_DEVICE_IN_SPEAKER_MIC,
97 SND_DEVICE_IN_HEADSET_MIC,
98 SND_DEVICE_IN_HANDSET_MIC_AEC,
99 SND_DEVICE_IN_SPEAKER_MIC_AEC,
100 SND_DEVICE_IN_HEADSET_MIC_AEC,
101 SND_DEVICE_IN_VOICE_SPEAKER_MIC,
102 SND_DEVICE_IN_VOICE_HEADSET_MIC,
103 SND_DEVICE_IN_HDMI_MIC,
104 SND_DEVICE_IN_BT_SCO_MIC,
105 SND_DEVICE_IN_CAMCORDER_MIC,
106 SND_DEVICE_IN_VOICE_DMIC_1,
107 SND_DEVICE_IN_VOICE_SPEAKER_DMIC_1,
108 SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC,
109 SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC,
110 SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC,
111 SND_DEVICE_IN_VOICE_REC_HEADSET_MIC,
112 SND_DEVICE_IN_VOICE_REC_MIC,
113 SND_DEVICE_IN_VOICE_REC_DMIC_1,
114 SND_DEVICE_IN_VOICE_REC_DMIC_NS_1,
115 SND_DEVICE_IN_LOOPBACK_AEC,
116 SND_DEVICE_IN_END,
117
118 SND_DEVICE_MAX = SND_DEVICE_IN_END,
119
120};
121
122
123#define MIXER_CARD 0
124#define SOUND_CARD 0
125
126/*
127 * tinyAlsa library interprets period size as number of frames
128 * one frame = channel_count * sizeof (pcm sample)
129 * so if format = 16-bit PCM and channels = Stereo, frame size = 2 ch * 2 = 4 bytes
130 * DEEP_BUFFER_OUTPUT_PERIOD_SIZE = 1024 means 1024 * 4 = 4096 bytes
131 * We should take care of returning proper size when AudioFlinger queries for
132 * the buffer size of an input/output stream
133 */
134#define PLAYBACK_PERIOD_SIZE 256
135#define PLAYBACK_PERIOD_COUNT 2
136#define PLAYBACK_DEFAULT_CHANNEL_COUNT 2
137#define PLAYBACK_DEFAULT_SAMPLING_RATE 48000
138#define PLAYBACK_START_THRESHOLD(size, count) (((size) * (count)) - 1)
139#define PLAYBACK_STOP_THRESHOLD(size, count) ((size) * ((count) + 2))
140#define PLAYBACK_AVAILABLE_MIN 1
141
142
143#define SCO_PERIOD_SIZE 168
144#define SCO_PERIOD_COUNT 2
145#define SCO_DEFAULT_CHANNEL_COUNT 2
146#define SCO_DEFAULT_SAMPLING_RATE 8000
147#define SCO_START_THRESHOLD 335
148#define SCO_STOP_THRESHOLD 336
149#define SCO_AVAILABLE_MIN 1
150
151#define PLAYBACK_HDMI_MULTI_PERIOD_SIZE 1024
152#define PLAYBACK_HDMI_MULTI_PERIOD_COUNT 4
153#define PLAYBACK_HDMI_MULTI_DEFAULT_CHANNEL_COUNT 6
154#define PLAYBACK_HDMI_MULTI_PERIOD_BYTES \
155 (PLAYBACK_HDMI_MULTI_PERIOD_SIZE * PLAYBACK_HDMI_MULTI_DEFAULT_CHANNEL_COUNT * 2)
156#define PLAYBACK_HDMI_MULTI_START_THRESHOLD 4095
157#define PLAYBACK_HDMI_MULTI_STOP_THRESHOLD 4096
158#define PLAYBACK_HDMI_MULTI_AVAILABLE_MIN 1
159
160#define PLAYBACK_HDMI_DEFAULT_CHANNEL_COUNT 2
161
162#define CAPTURE_PERIOD_SIZE 1024
163#define CAPTURE_PERIOD_SIZE_LOW_LATENCY 256
164#define CAPTURE_PERIOD_COUNT 2
165#define CAPTURE_PERIOD_COUNT_LOW_LATENCY 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 0
171#define COMPRESS_DEVICE 5
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
184typedef 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 */
190typedef 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
217enum {
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
224enum {
225 OFFLOAD_STATE_IDLE,
226 OFFLOAD_STATE_PLAYING,
227 OFFLOAD_STATE_PAUSED,
228 OFFLOAD_STATE_PAUSED_FLUSHED,
229};
230
231typedef enum {
232 PCM_PLAYBACK = 0x1,
233 PCM_CAPTURE = 0x2,
234 VOICE_CALL = 0x4,
235 PCM_HOTWORD_STREAMING = 0x8,
236 PCM_CAPTURE_LOW_LATENCY = 0x10,
237} usecase_type_t;
238
239struct offload_cmd {
240 struct listnode node;
241 int cmd;
242 int data[];
243};
244
245struct pcm_device_profile {
246 struct pcm_config config;
247 int card;
248 int id;
249 usecase_type_t type;
250 audio_devices_t devices;
251};
252
253struct pcm_device {
254 struct listnode stream_list_node;
255 struct pcm_device_profile* pcm_profile;
256 struct pcm* pcm;
257 int status;
258 /* TODO: remove resampler if possible when AudioFlinger supports downsampling from 48 to 8 */
259 struct resampler_itfe* resampler;
260 int16_t* res_buffer;
261 size_t res_byte_count;
262 int sound_trigger_handle;
263};
264
265struct stream_out {
266 struct audio_stream_out stream;
267 pthread_mutex_t lock; /* see note below on mutex acquisition order */
268 pthread_mutex_t pre_lock; /* acquire before lock to avoid DOS by playback thread */
269 pthread_cond_t cond;
270 struct pcm_config config;
271 struct listnode pcm_dev_list;
272 struct compr_config compr_config;
273 struct compress* compr;
274 int standby;
275 unsigned int sample_rate;
276 audio_channel_mask_t channel_mask;
277 audio_format_t format;
278 audio_devices_t devices;
279 audio_output_flags_t flags;
280 audio_usecase_t usecase;
281 /* Array of supported channel mask configurations. +1 so that the last entry is always 0 */
282 audio_channel_mask_t supported_channel_masks[MAX_SUPPORTED_CHANNEL_MASKS + 1];
283 bool muted;
284 /* total frames written, not cleared when entering standby */
285 uint64_t written;
286 audio_io_handle_t handle;
287
288 int non_blocking;
289 int offload_state;
290 pthread_cond_t offload_cond;
291 pthread_t offload_thread;
292 struct listnode offload_cmd_list;
293 bool offload_thread_blocked;
294
295 stream_callback_t offload_callback;
296 void* offload_cookie;
297 struct compr_gapless_mdata gapless_mdata;
298 int send_new_metadata;
299
300 struct audio_device* dev;
301
302#ifdef PREPROCESSING_ENABLED
303 struct echo_reference_itfe *echo_reference;
304 // echo_reference_generation indicates if the echo reference used by the output stream is
305 // in sync with the one known by the audio_device. When different from the generation stored
306 // in the audio_device the output stream must release the echo reference.
307 // always modified with audio device and stream mutex locked.
308 int32_t echo_reference_generation;
309#endif
310
311 bool is_fastmixer_affinity_set;
312};
313
314struct stream_in {
315 struct audio_stream_in stream;
316 pthread_mutex_t lock; /* see note below on mutex acquisition order */
317 pthread_mutex_t pre_lock; /* acquire before lock to avoid DOS by
318 capture thread */
319 struct pcm_config config;
320 struct listnode pcm_dev_list;
321 int standby;
322 audio_source_t source;
323 audio_devices_t devices;
324 uint32_t main_channels;
325 audio_usecase_t usecase;
326 usecase_type_t usecase_type;
327 bool enable_aec;
328 audio_input_flags_t input_flags;
329
330 /* TODO: remove resampler if possible when AudioFlinger supports downsampling from 48 to 8 */
331 unsigned int requested_rate;
332 struct resampler_itfe* resampler;
333 struct resampler_buffer_provider buf_provider;
334 int read_status;
335 int16_t* read_buf;
336 size_t read_buf_size;
337 size_t read_buf_frames;
338
339 int16_t *proc_buf_in;
340 int16_t *proc_buf_out;
341 size_t proc_buf_size;
342 size_t proc_buf_frames;
343
344#ifdef PREPROCESSING_ENABLED
345 struct echo_reference_itfe *echo_reference;
346 int16_t *ref_buf;
347 size_t ref_buf_size;
348 size_t ref_buf_frames;
349
350#ifdef HW_AEC_LOOPBACK
351 bool hw_echo_reference;
352 int16_t* hw_ref_buf;
353 size_t hw_ref_buf_size;
354#endif
355
356 int num_preprocessors;
357 struct effect_info_s preprocessors[MAX_PREPROCESSORS];
358
359 bool aux_channels_changed;
360 uint32_t aux_channels;
361#endif
362
363 struct audio_device* dev;
364 bool is_fastcapture_affinity_set;
365};
366
367struct mixer_card {
368 struct listnode adev_list_node;
369 struct listnode uc_list_node[AUDIO_USECASE_MAX];
370 int card;
371 struct mixer* mixer;
372 struct audio_route* audio_route;
373};
374
375struct audio_usecase {
376 struct listnode adev_list_node;
377 audio_usecase_t id;
378 usecase_type_t type;
379 audio_devices_t devices;
380 snd_device_t out_snd_device;
381 snd_device_t in_snd_device;
382 struct audio_stream* stream;
383 struct listnode mixer_list;
384};
385
386
387struct audio_device {
388 struct audio_hw_device device;
389 pthread_mutex_t lock; /* see note below on mutex acquisition order */
390 struct listnode mixer_list;
391 audio_mode_t mode;
392 struct stream_in* active_input;
393 struct stream_out* primary_output;
394 int in_call;
395 float voice_volume;
396 bool mic_mute;
397 int tty_mode;
398 bool bluetooth_nrec;
399 bool screen_off;
400 int* snd_dev_ref_cnt;
401 struct listnode usecase_list;
402 bool speaker_lr_swap;
403 unsigned int cur_hdmi_channels;
404 int dualmic_config;
405 bool ns_in_voice_rec;
406
407 void* offload_fx_lib;
408 int (*offload_fx_start_output)(audio_io_handle_t);
409 int (*offload_fx_stop_output)(audio_io_handle_t);
410
411#ifdef PREPROCESSING_ENABLED
412 struct echo_reference_itfe* echo_reference;
413 // echo_reference_generation indicates if the echo reference used by the output stream is
414 // in sync with the one known by the audio_device.
415 // incremented atomically with a memory barrier and audio device mutex locked but WITHOUT
416 // stream mutex locked: the stream will load it atomically with a barrier and re-read it
417 // with audio device mutex if needed
418 volatile int32_t echo_reference_generation;
419#endif
420
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100421 void* sound_trigger_lib;
422 int (*sound_trigger_open_for_streaming)();
423 size_t (*sound_trigger_read_samples)(int, void*, size_t);
424 int (*sound_trigger_close_for_streaming)(int);
425
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100426 pthread_mutex_t lock_inputs; /* see note below on mutex acquisition order */
427};
428
429/*
430 * NOTE: when multiple mutexes have to be acquired, always take the
Christopher N. Hesse2f6f8582017-01-28 12:46:15 +0100431 * lock_inputs, stream_in, stream_out, then audio_device mutex.
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100432 * stream_in mutex must always be before stream_out mutex
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100433 * lock_inputs must be held in order to either close the input stream, or prevent closure.
434 */
435
Christopher N. Hesse0612a4e2017-01-28 14:05:39 +0100436#endif // SAMSUNG_AUDIO_HW_H