blob: d05acded642efe78dc750026111f56866501e8e8 [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
Christopher N. Hesse297a6362017-01-28 12:40:45 +010056/* Sound devices specific to the platform
57 * The DEVICE_OUT_* and DEVICE_IN_* should be mapped to these sound
58 * devices to enable corresponding mixer paths
59 */
60enum {
61 SND_DEVICE_NONE = 0,
62
63 /* Playback devices */
64 SND_DEVICE_MIN,
65 SND_DEVICE_OUT_BEGIN = SND_DEVICE_MIN,
Christopher N. Hesse530cf0d2017-01-31 21:59:54 +010066 SND_DEVICE_OUT_EARPIECE = SND_DEVICE_OUT_BEGIN,
Christopher N. Hesse297a6362017-01-28 12:40:45 +010067 SND_DEVICE_OUT_SPEAKER,
68 SND_DEVICE_OUT_HEADPHONES,
69 SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES,
Christopher N. Hesse530cf0d2017-01-31 21:59:54 +010070 SND_DEVICE_OUT_VOICE_EARPIECE,
Andreas Schneider59486fa2017-02-06 09:16:39 +010071 SND_DEVICE_OUT_VOICE_EARPIECE_WB,
Christopher N. Hesse297a6362017-01-28 12:40:45 +010072 SND_DEVICE_OUT_VOICE_SPEAKER,
Andreas Schneider59486fa2017-02-06 09:16:39 +010073 SND_DEVICE_OUT_VOICE_SPEAKER_WB,
Christopher N. Hesse297a6362017-01-28 12:40:45 +010074 SND_DEVICE_OUT_VOICE_HEADPHONES,
Andreas Schneider59486fa2017-02-06 09:16:39 +010075 SND_DEVICE_OUT_VOICE_HEADPHONES_WB,
Christopher N. Hesse56caa262017-03-20 19:40:53 +010076 SND_DEVICE_OUT_VOICE_BT_SCO,
77 SND_DEVICE_OUT_VOICE_BT_SCO_WB,
Christopher N. Hesse297a6362017-01-28 12:40:45 +010078 SND_DEVICE_OUT_HDMI,
79 SND_DEVICE_OUT_SPEAKER_AND_HDMI,
80 SND_DEVICE_OUT_BT_SCO,
Christopher N. Hesse297a6362017-01-28 12:40:45 +010081 SND_DEVICE_OUT_END,
82
83 /*
84 * Note: IN_BEGIN should be same as OUT_END because total number of devices
85 * SND_DEVICES_MAX should not exceed MAX_RX + MAX_TX devices.
86 */
87 /* Capture devices */
88 SND_DEVICE_IN_BEGIN = SND_DEVICE_OUT_END,
Christopher N. Hesse530cf0d2017-01-31 21:59:54 +010089 SND_DEVICE_IN_EARPIECE_MIC = SND_DEVICE_IN_BEGIN,
Christopher N. Hesse297a6362017-01-28 12:40:45 +010090 SND_DEVICE_IN_SPEAKER_MIC,
91 SND_DEVICE_IN_HEADSET_MIC,
Christopher N. Hesse530cf0d2017-01-31 21:59:54 +010092 SND_DEVICE_IN_EARPIECE_MIC_AEC,
Christopher N. Hesse297a6362017-01-28 12:40:45 +010093 SND_DEVICE_IN_SPEAKER_MIC_AEC,
94 SND_DEVICE_IN_HEADSET_MIC_AEC,
Andreas Schneider82f32482017-02-06 09:00:48 +010095 SND_DEVICE_IN_VOICE_MIC,
96 SND_DEVICE_IN_VOICE_EARPIECE_MIC,
Andreas Schneider59486fa2017-02-06 09:16:39 +010097 SND_DEVICE_IN_VOICE_EARPIECE_MIC_WB,
Christopher N. Hesse297a6362017-01-28 12:40:45 +010098 SND_DEVICE_IN_VOICE_SPEAKER_MIC,
Andreas Schneider59486fa2017-02-06 09:16:39 +010099 SND_DEVICE_IN_VOICE_SPEAKER_MIC_WB,
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100100 SND_DEVICE_IN_VOICE_HEADSET_MIC,
Andreas Schneider59486fa2017-02-06 09:16:39 +0100101 SND_DEVICE_IN_VOICE_HEADSET_MIC_WB,
Christopher N. Hesse56caa262017-03-20 19:40:53 +0100102 SND_DEVICE_IN_VOICE_BT_SCO_MIC,
103 SND_DEVICE_IN_VOICE_BT_SCO_MIC_WB,
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100104 SND_DEVICE_IN_HDMI_MIC,
105 SND_DEVICE_IN_BT_SCO_MIC,
106 SND_DEVICE_IN_CAMCORDER_MIC,
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100107 SND_DEVICE_IN_VOICE_REC_HEADSET_MIC,
108 SND_DEVICE_IN_VOICE_REC_MIC,
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100109 SND_DEVICE_IN_LOOPBACK_AEC,
110 SND_DEVICE_IN_END,
111
112 SND_DEVICE_MAX = SND_DEVICE_IN_END,
113
114};
115
116
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100117/*
118 * tinyAlsa library interprets period size as number of frames
119 * one frame = channel_count * sizeof (pcm sample)
120 * so if format = 16-bit PCM and channels = Stereo, frame size = 2 ch * 2 = 4 bytes
121 * DEEP_BUFFER_OUTPUT_PERIOD_SIZE = 1024 means 1024 * 4 = 4096 bytes
122 * We should take care of returning proper size when AudioFlinger queries for
123 * the buffer size of an input/output stream
124 */
125#define PLAYBACK_PERIOD_SIZE 256
126#define PLAYBACK_PERIOD_COUNT 2
127#define PLAYBACK_DEFAULT_CHANNEL_COUNT 2
128#define PLAYBACK_DEFAULT_SAMPLING_RATE 48000
129#define PLAYBACK_START_THRESHOLD(size, count) (((size) * (count)) - 1)
130#define PLAYBACK_STOP_THRESHOLD(size, count) ((size) * ((count) + 2))
131#define PLAYBACK_AVAILABLE_MIN 1
132
133
134#define SCO_PERIOD_SIZE 168
135#define SCO_PERIOD_COUNT 2
136#define SCO_DEFAULT_CHANNEL_COUNT 2
137#define SCO_DEFAULT_SAMPLING_RATE 8000
Fevax51bd12c2017-03-15 10:56:39 -0300138#define SCO_WB_SAMPLING_RATE 16000
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100139#define SCO_START_THRESHOLD 335
140#define SCO_STOP_THRESHOLD 336
141#define SCO_AVAILABLE_MIN 1
142
143#define PLAYBACK_HDMI_MULTI_PERIOD_SIZE 1024
144#define PLAYBACK_HDMI_MULTI_PERIOD_COUNT 4
145#define PLAYBACK_HDMI_MULTI_DEFAULT_CHANNEL_COUNT 6
146#define PLAYBACK_HDMI_MULTI_PERIOD_BYTES \
147 (PLAYBACK_HDMI_MULTI_PERIOD_SIZE * PLAYBACK_HDMI_MULTI_DEFAULT_CHANNEL_COUNT * 2)
148#define PLAYBACK_HDMI_MULTI_START_THRESHOLD 4095
149#define PLAYBACK_HDMI_MULTI_STOP_THRESHOLD 4096
150#define PLAYBACK_HDMI_MULTI_AVAILABLE_MIN 1
151
152#define PLAYBACK_HDMI_DEFAULT_CHANNEL_COUNT 2
153
154#define CAPTURE_PERIOD_SIZE 1024
155#define CAPTURE_PERIOD_SIZE_LOW_LATENCY 256
156#define CAPTURE_PERIOD_COUNT 2
157#define CAPTURE_PERIOD_COUNT_LOW_LATENCY 2
158#define CAPTURE_DEFAULT_CHANNEL_COUNT 2
159#define CAPTURE_DEFAULT_SAMPLING_RATE 48000
160#define CAPTURE_START_THRESHOLD 1
161
162#define COMPRESS_CARD 0
163#define COMPRESS_DEVICE 5
164#define COMPRESS_OFFLOAD_FRAGMENT_SIZE (32 * 1024)
165#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4
166/* ToDo: Check and update a proper value in msec */
167#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96
168#define COMPRESS_PLAYBACK_VOLUME_MAX 0x10000 //NV suggested value
169
170#define DEEP_BUFFER_OUTPUT_SAMPLING_RATE 48000
171#define DEEP_BUFFER_OUTPUT_PERIOD_SIZE 480
172#define DEEP_BUFFER_OUTPUT_PERIOD_COUNT 8
173
174#define MAX_SUPPORTED_CHANNEL_MASKS 2
175
176typedef int snd_device_t;
177
178/* These are the supported use cases by the hardware.
179 * Each usecase is mapped to a specific PCM device.
180 * Refer to pcm_device_table[].
181 */
182typedef enum {
183 USECASE_INVALID = -1,
184 /* Playback usecases */
185 USECASE_AUDIO_PLAYBACK = 0,
186 USECASE_AUDIO_PLAYBACK_MULTI_CH,
187 USECASE_AUDIO_PLAYBACK_OFFLOAD,
188 USECASE_AUDIO_PLAYBACK_DEEP_BUFFER,
189
190 /* Capture usecases */
191 USECASE_AUDIO_CAPTURE,
192 USECASE_AUDIO_CAPTURE_HOTWORD,
193
194 USECASE_VOICE_CALL,
195 AUDIO_USECASE_MAX
196} audio_usecase_t;
197
198#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
199
200/*
201 * tinyAlsa library interprets period size as number of frames
202 * one frame = channel_count * sizeof (pcm sample)
203 * so if format = 16-bit PCM and channels = Stereo, frame size = 2 ch * 2 = 4 bytes
204 * DEEP_BUFFER_OUTPUT_PERIOD_SIZE = 1024 means 1024 * 4 = 4096 bytes
205 * We should take care of returning proper size when AudioFlinger queries for
206 * the buffer size of an input/output stream
207 */
208
209enum {
210 OFFLOAD_CMD_EXIT, /* exit compress offload thread loop*/
211 OFFLOAD_CMD_DRAIN, /* send a full drain request to DSP */
212 OFFLOAD_CMD_PARTIAL_DRAIN, /* send a partial drain request to DSP */
213 OFFLOAD_CMD_WAIT_FOR_BUFFER, /* wait for buffer released by DSP */
214};
215
216enum {
217 OFFLOAD_STATE_IDLE,
218 OFFLOAD_STATE_PLAYING,
219 OFFLOAD_STATE_PAUSED,
220 OFFLOAD_STATE_PAUSED_FLUSHED,
221};
222
223typedef enum {
224 PCM_PLAYBACK = 0x1,
225 PCM_CAPTURE = 0x2,
226 VOICE_CALL = 0x4,
227 PCM_HOTWORD_STREAMING = 0x8,
228 PCM_CAPTURE_LOW_LATENCY = 0x10,
229} usecase_type_t;
230
231struct offload_cmd {
232 struct listnode node;
233 int cmd;
234 int data[];
235};
236
237struct pcm_device_profile {
238 struct pcm_config config;
239 int card;
240 int id;
241 usecase_type_t type;
242 audio_devices_t devices;
243};
244
245struct pcm_device {
246 struct listnode stream_list_node;
247 struct pcm_device_profile* pcm_profile;
248 struct pcm* pcm;
249 int status;
250 /* TODO: remove resampler if possible when AudioFlinger supports downsampling from 48 to 8 */
251 struct resampler_itfe* resampler;
252 int16_t* res_buffer;
253 size_t res_byte_count;
254 int sound_trigger_handle;
255};
256
257struct stream_out {
258 struct audio_stream_out stream;
259 pthread_mutex_t lock; /* see note below on mutex acquisition order */
260 pthread_mutex_t pre_lock; /* acquire before lock to avoid DOS by playback thread */
261 pthread_cond_t cond;
262 struct pcm_config config;
263 struct listnode pcm_dev_list;
264 struct compr_config compr_config;
265 struct compress* compr;
266 int standby;
267 unsigned int sample_rate;
268 audio_channel_mask_t channel_mask;
269 audio_format_t format;
270 audio_devices_t devices;
271 audio_output_flags_t flags;
272 audio_usecase_t usecase;
273 /* Array of supported channel mask configurations. +1 so that the last entry is always 0 */
274 audio_channel_mask_t supported_channel_masks[MAX_SUPPORTED_CHANNEL_MASKS + 1];
275 bool muted;
276 /* total frames written, not cleared when entering standby */
277 uint64_t written;
278 audio_io_handle_t handle;
279
280 int non_blocking;
281 int offload_state;
282 pthread_cond_t offload_cond;
283 pthread_t offload_thread;
284 struct listnode offload_cmd_list;
285 bool offload_thread_blocked;
286
287 stream_callback_t offload_callback;
288 void* offload_cookie;
289 struct compr_gapless_mdata gapless_mdata;
290 int send_new_metadata;
291
292 struct audio_device* dev;
293
294#ifdef PREPROCESSING_ENABLED
295 struct echo_reference_itfe *echo_reference;
296 // echo_reference_generation indicates if the echo reference used by the output stream is
297 // in sync with the one known by the audio_device. When different from the generation stored
298 // in the audio_device the output stream must release the echo reference.
299 // always modified with audio device and stream mutex locked.
300 int32_t echo_reference_generation;
301#endif
302
303 bool is_fastmixer_affinity_set;
Christopher N. Hessee6b3a3e2017-01-08 00:03:23 +0100304
305 int64_t last_write_time_us;
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100306};
307
308struct stream_in {
309 struct audio_stream_in stream;
310 pthread_mutex_t lock; /* see note below on mutex acquisition order */
311 pthread_mutex_t pre_lock; /* acquire before lock to avoid DOS by
312 capture thread */
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 usecase_type_t usecase_type;
321 bool enable_aec;
322 audio_input_flags_t input_flags;
323
324 /* TODO: remove resampler if possible when AudioFlinger supports downsampling from 48 to 8 */
325 unsigned int requested_rate;
326 struct resampler_itfe* resampler;
327 struct resampler_buffer_provider buf_provider;
328 int read_status;
329 int16_t* read_buf;
330 size_t read_buf_size;
331 size_t read_buf_frames;
332
333 int16_t *proc_buf_in;
334 int16_t *proc_buf_out;
335 size_t proc_buf_size;
336 size_t proc_buf_frames;
337
338#ifdef PREPROCESSING_ENABLED
339 struct echo_reference_itfe *echo_reference;
340 int16_t *ref_buf;
341 size_t ref_buf_size;
342 size_t ref_buf_frames;
343
344#ifdef HW_AEC_LOOPBACK
345 bool hw_echo_reference;
346 int16_t* hw_ref_buf;
347 size_t hw_ref_buf_size;
348#endif
349
350 int num_preprocessors;
351 struct effect_info_s preprocessors[MAX_PREPROCESSORS];
352
353 bool aux_channels_changed;
354 uint32_t aux_channels;
355#endif
356
357 struct audio_device* dev;
358 bool is_fastcapture_affinity_set;
Christopher N. Hessee6b3a3e2017-01-08 00:03:23 +0100359
360 int64_t last_read_time_us;
Christopher N. Hessece6d5af2017-01-12 11:40:30 +0100361 int64_t frames_read; /* total frames read, not cleared when
362 entering standby */
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100363};
364
365struct mixer_card {
366 struct listnode adev_list_node;
367 struct listnode uc_list_node[AUDIO_USECASE_MAX];
368 int card;
369 struct mixer* mixer;
370 struct audio_route* audio_route;
Andreas Schneider759368f2017-02-02 16:11:14 +0100371 struct timespec dsp_poweroff_time;
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100372};
373
374struct audio_usecase {
375 struct listnode adev_list_node;
376 audio_usecase_t id;
377 usecase_type_t type;
378 audio_devices_t devices;
379 snd_device_t out_snd_device;
380 snd_device_t in_snd_device;
381 struct audio_stream* stream;
382 struct listnode mixer_list;
383};
384
Andreas Schneider74ef3a12017-02-02 18:29:12 +0100385struct voice_data {
386 bool in_call;
387 float volume;
388 bool bluetooth_nrec;
Andreas Schneider05bc1882017-02-09 14:03:11 +0100389 bool bluetooth_wb;
Christopher N. Hesse41c9f3d2017-02-02 20:48:56 +0100390 void *session;
Andreas Schneider74ef3a12017-02-02 18:29:12 +0100391};
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100392
393struct audio_device {
394 struct audio_hw_device device;
395 pthread_mutex_t lock; /* see note below on mutex acquisition order */
396 struct listnode mixer_list;
397 audio_mode_t mode;
398 struct stream_in* active_input;
399 struct stream_out* primary_output;
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100400 bool mic_mute;
Andreas Schneiderecd17ce2017-02-09 10:45:21 +0100401 bool screen_off;
Andreas Schneider74ef3a12017-02-02 18:29:12 +0100402
403 struct voice_data voice;
404
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100405 int* snd_dev_ref_cnt;
406 struct listnode usecase_list;
407 bool speaker_lr_swap;
408 unsigned int cur_hdmi_channels;
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100409 bool ns_in_voice_rec;
410
411 void* offload_fx_lib;
412 int (*offload_fx_start_output)(audio_io_handle_t);
413 int (*offload_fx_stop_output)(audio_io_handle_t);
414
415#ifdef PREPROCESSING_ENABLED
416 struct echo_reference_itfe* echo_reference;
417 // echo_reference_generation indicates if the echo reference used by the output stream is
418 // in sync with the one known by the audio_device.
419 // incremented atomically with a memory barrier and audio device mutex locked but WITHOUT
420 // stream mutex locked: the stream will load it atomically with a barrier and re-read it
421 // with audio device mutex if needed
422 volatile int32_t echo_reference_generation;
423#endif
424
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100425 void* sound_trigger_lib;
426 int (*sound_trigger_open_for_streaming)();
427 size_t (*sound_trigger_read_samples)(int, void*, size_t);
428 int (*sound_trigger_close_for_streaming)(int);
429
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100430 pthread_mutex_t lock_inputs; /* see note below on mutex acquisition order */
431};
432
433/*
434 * NOTE: when multiple mutexes have to be acquired, always take the
Christopher N. Hesse2f6f8582017-01-28 12:46:15 +0100435 * lock_inputs, stream_in, stream_out, then audio_device mutex.
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100436 * stream_in mutex must always be before stream_out mutex
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100437 * lock_inputs must be held in order to either close the input stream, or prevent closure.
438 */
439
Christopher N. Hesse0612a4e2017-01-28 14:05:39 +0100440#endif // SAMSUNG_AUDIO_HW_H