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