blob: ab50ef74e5784ee5644777af7b4c8c5f3c5187a6 [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,
Christopher N. Hesse297a6362017-01-28 12:40:45 +010071 SND_DEVICE_OUT_VOICE_SPEAKER,
72 SND_DEVICE_OUT_VOICE_HEADPHONES,
73 SND_DEVICE_OUT_HDMI,
74 SND_DEVICE_OUT_SPEAKER_AND_HDMI,
75 SND_DEVICE_OUT_BT_SCO,
Christopher N. Hesse297a6362017-01-28 12:40:45 +010076 SND_DEVICE_OUT_END,
77
78 /*
79 * Note: IN_BEGIN should be same as OUT_END because total number of devices
80 * SND_DEVICES_MAX should not exceed MAX_RX + MAX_TX devices.
81 */
82 /* Capture devices */
83 SND_DEVICE_IN_BEGIN = SND_DEVICE_OUT_END,
Christopher N. Hesse530cf0d2017-01-31 21:59:54 +010084 SND_DEVICE_IN_EARPIECE_MIC = SND_DEVICE_IN_BEGIN,
Christopher N. Hesse297a6362017-01-28 12:40:45 +010085 SND_DEVICE_IN_SPEAKER_MIC,
86 SND_DEVICE_IN_HEADSET_MIC,
Christopher N. Hesse530cf0d2017-01-31 21:59:54 +010087 SND_DEVICE_IN_EARPIECE_MIC_AEC,
Christopher N. Hesse297a6362017-01-28 12:40:45 +010088 SND_DEVICE_IN_SPEAKER_MIC_AEC,
89 SND_DEVICE_IN_HEADSET_MIC_AEC,
90 SND_DEVICE_IN_VOICE_SPEAKER_MIC,
91 SND_DEVICE_IN_VOICE_HEADSET_MIC,
92 SND_DEVICE_IN_HDMI_MIC,
93 SND_DEVICE_IN_BT_SCO_MIC,
94 SND_DEVICE_IN_CAMCORDER_MIC,
Christopher N. Hesse297a6362017-01-28 12:40:45 +010095 SND_DEVICE_IN_VOICE_REC_HEADSET_MIC,
96 SND_DEVICE_IN_VOICE_REC_MIC,
Christopher N. Hesse297a6362017-01-28 12:40:45 +010097 SND_DEVICE_IN_LOOPBACK_AEC,
98 SND_DEVICE_IN_END,
99
100 SND_DEVICE_MAX = SND_DEVICE_IN_END,
101
102};
103
104
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100105/*
106 * tinyAlsa library interprets period size as number of frames
107 * one frame = channel_count * sizeof (pcm sample)
108 * so if format = 16-bit PCM and channels = Stereo, frame size = 2 ch * 2 = 4 bytes
109 * DEEP_BUFFER_OUTPUT_PERIOD_SIZE = 1024 means 1024 * 4 = 4096 bytes
110 * We should take care of returning proper size when AudioFlinger queries for
111 * the buffer size of an input/output stream
112 */
113#define PLAYBACK_PERIOD_SIZE 256
114#define PLAYBACK_PERIOD_COUNT 2
115#define PLAYBACK_DEFAULT_CHANNEL_COUNT 2
116#define PLAYBACK_DEFAULT_SAMPLING_RATE 48000
117#define PLAYBACK_START_THRESHOLD(size, count) (((size) * (count)) - 1)
118#define PLAYBACK_STOP_THRESHOLD(size, count) ((size) * ((count) + 2))
119#define PLAYBACK_AVAILABLE_MIN 1
120
121
122#define SCO_PERIOD_SIZE 168
123#define SCO_PERIOD_COUNT 2
124#define SCO_DEFAULT_CHANNEL_COUNT 2
125#define SCO_DEFAULT_SAMPLING_RATE 8000
126#define SCO_START_THRESHOLD 335
127#define SCO_STOP_THRESHOLD 336
128#define SCO_AVAILABLE_MIN 1
129
130#define PLAYBACK_HDMI_MULTI_PERIOD_SIZE 1024
131#define PLAYBACK_HDMI_MULTI_PERIOD_COUNT 4
132#define PLAYBACK_HDMI_MULTI_DEFAULT_CHANNEL_COUNT 6
133#define PLAYBACK_HDMI_MULTI_PERIOD_BYTES \
134 (PLAYBACK_HDMI_MULTI_PERIOD_SIZE * PLAYBACK_HDMI_MULTI_DEFAULT_CHANNEL_COUNT * 2)
135#define PLAYBACK_HDMI_MULTI_START_THRESHOLD 4095
136#define PLAYBACK_HDMI_MULTI_STOP_THRESHOLD 4096
137#define PLAYBACK_HDMI_MULTI_AVAILABLE_MIN 1
138
139#define PLAYBACK_HDMI_DEFAULT_CHANNEL_COUNT 2
140
141#define CAPTURE_PERIOD_SIZE 1024
142#define CAPTURE_PERIOD_SIZE_LOW_LATENCY 256
143#define CAPTURE_PERIOD_COUNT 2
144#define CAPTURE_PERIOD_COUNT_LOW_LATENCY 2
145#define CAPTURE_DEFAULT_CHANNEL_COUNT 2
146#define CAPTURE_DEFAULT_SAMPLING_RATE 48000
147#define CAPTURE_START_THRESHOLD 1
148
149#define COMPRESS_CARD 0
150#define COMPRESS_DEVICE 5
151#define COMPRESS_OFFLOAD_FRAGMENT_SIZE (32 * 1024)
152#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4
153/* ToDo: Check and update a proper value in msec */
154#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96
155#define COMPRESS_PLAYBACK_VOLUME_MAX 0x10000 //NV suggested value
156
157#define DEEP_BUFFER_OUTPUT_SAMPLING_RATE 48000
158#define DEEP_BUFFER_OUTPUT_PERIOD_SIZE 480
159#define DEEP_BUFFER_OUTPUT_PERIOD_COUNT 8
160
161#define MAX_SUPPORTED_CHANNEL_MASKS 2
162
163typedef int snd_device_t;
164
165/* These are the supported use cases by the hardware.
166 * Each usecase is mapped to a specific PCM device.
167 * Refer to pcm_device_table[].
168 */
169typedef enum {
170 USECASE_INVALID = -1,
171 /* Playback usecases */
172 USECASE_AUDIO_PLAYBACK = 0,
173 USECASE_AUDIO_PLAYBACK_MULTI_CH,
174 USECASE_AUDIO_PLAYBACK_OFFLOAD,
175 USECASE_AUDIO_PLAYBACK_DEEP_BUFFER,
176
177 /* Capture usecases */
178 USECASE_AUDIO_CAPTURE,
179 USECASE_AUDIO_CAPTURE_HOTWORD,
180
181 USECASE_VOICE_CALL,
182 AUDIO_USECASE_MAX
183} audio_usecase_t;
184
185#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
186
187/*
188 * tinyAlsa library interprets period size as number of frames
189 * one frame = channel_count * sizeof (pcm sample)
190 * so if format = 16-bit PCM and channels = Stereo, frame size = 2 ch * 2 = 4 bytes
191 * DEEP_BUFFER_OUTPUT_PERIOD_SIZE = 1024 means 1024 * 4 = 4096 bytes
192 * We should take care of returning proper size when AudioFlinger queries for
193 * the buffer size of an input/output stream
194 */
195
196enum {
197 OFFLOAD_CMD_EXIT, /* exit compress offload thread loop*/
198 OFFLOAD_CMD_DRAIN, /* send a full drain request to DSP */
199 OFFLOAD_CMD_PARTIAL_DRAIN, /* send a partial drain request to DSP */
200 OFFLOAD_CMD_WAIT_FOR_BUFFER, /* wait for buffer released by DSP */
201};
202
203enum {
204 OFFLOAD_STATE_IDLE,
205 OFFLOAD_STATE_PLAYING,
206 OFFLOAD_STATE_PAUSED,
207 OFFLOAD_STATE_PAUSED_FLUSHED,
208};
209
210typedef enum {
211 PCM_PLAYBACK = 0x1,
212 PCM_CAPTURE = 0x2,
213 VOICE_CALL = 0x4,
214 PCM_HOTWORD_STREAMING = 0x8,
215 PCM_CAPTURE_LOW_LATENCY = 0x10,
216} usecase_type_t;
217
218struct offload_cmd {
219 struct listnode node;
220 int cmd;
221 int data[];
222};
223
224struct pcm_device_profile {
225 struct pcm_config config;
226 int card;
227 int id;
228 usecase_type_t type;
229 audio_devices_t devices;
230};
231
232struct pcm_device {
233 struct listnode stream_list_node;
234 struct pcm_device_profile* pcm_profile;
235 struct pcm* pcm;
236 int status;
237 /* TODO: remove resampler if possible when AudioFlinger supports downsampling from 48 to 8 */
238 struct resampler_itfe* resampler;
239 int16_t* res_buffer;
240 size_t res_byte_count;
241 int sound_trigger_handle;
242};
243
244struct stream_out {
245 struct audio_stream_out stream;
246 pthread_mutex_t lock; /* see note below on mutex acquisition order */
247 pthread_mutex_t pre_lock; /* acquire before lock to avoid DOS by playback thread */
248 pthread_cond_t cond;
249 struct pcm_config config;
250 struct listnode pcm_dev_list;
251 struct compr_config compr_config;
252 struct compress* compr;
253 int standby;
254 unsigned int sample_rate;
255 audio_channel_mask_t channel_mask;
256 audio_format_t format;
257 audio_devices_t devices;
258 audio_output_flags_t flags;
259 audio_usecase_t usecase;
260 /* Array of supported channel mask configurations. +1 so that the last entry is always 0 */
261 audio_channel_mask_t supported_channel_masks[MAX_SUPPORTED_CHANNEL_MASKS + 1];
262 bool muted;
263 /* total frames written, not cleared when entering standby */
264 uint64_t written;
265 audio_io_handle_t handle;
266
267 int non_blocking;
268 int offload_state;
269 pthread_cond_t offload_cond;
270 pthread_t offload_thread;
271 struct listnode offload_cmd_list;
272 bool offload_thread_blocked;
273
274 stream_callback_t offload_callback;
275 void* offload_cookie;
276 struct compr_gapless_mdata gapless_mdata;
277 int send_new_metadata;
278
279 struct audio_device* dev;
280
281#ifdef PREPROCESSING_ENABLED
282 struct echo_reference_itfe *echo_reference;
283 // echo_reference_generation indicates if the echo reference used by the output stream is
284 // in sync with the one known by the audio_device. When different from the generation stored
285 // in the audio_device the output stream must release the echo reference.
286 // always modified with audio device and stream mutex locked.
287 int32_t echo_reference_generation;
288#endif
289
290 bool is_fastmixer_affinity_set;
Christopher N. Hessee6b3a3e2017-01-08 00:03:23 +0100291
292 int64_t last_write_time_us;
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100293};
294
295struct stream_in {
296 struct audio_stream_in stream;
297 pthread_mutex_t lock; /* see note below on mutex acquisition order */
298 pthread_mutex_t pre_lock; /* acquire before lock to avoid DOS by
299 capture thread */
300 struct pcm_config config;
301 struct listnode pcm_dev_list;
302 int standby;
303 audio_source_t source;
304 audio_devices_t devices;
305 uint32_t main_channels;
306 audio_usecase_t usecase;
307 usecase_type_t usecase_type;
308 bool enable_aec;
309 audio_input_flags_t input_flags;
310
311 /* TODO: remove resampler if possible when AudioFlinger supports downsampling from 48 to 8 */
312 unsigned int requested_rate;
313 struct resampler_itfe* resampler;
314 struct resampler_buffer_provider buf_provider;
315 int read_status;
316 int16_t* read_buf;
317 size_t read_buf_size;
318 size_t read_buf_frames;
319
320 int16_t *proc_buf_in;
321 int16_t *proc_buf_out;
322 size_t proc_buf_size;
323 size_t proc_buf_frames;
324
325#ifdef PREPROCESSING_ENABLED
326 struct echo_reference_itfe *echo_reference;
327 int16_t *ref_buf;
328 size_t ref_buf_size;
329 size_t ref_buf_frames;
330
331#ifdef HW_AEC_LOOPBACK
332 bool hw_echo_reference;
333 int16_t* hw_ref_buf;
334 size_t hw_ref_buf_size;
335#endif
336
337 int num_preprocessors;
338 struct effect_info_s preprocessors[MAX_PREPROCESSORS];
339
340 bool aux_channels_changed;
341 uint32_t aux_channels;
342#endif
343
344 struct audio_device* dev;
345 bool is_fastcapture_affinity_set;
Christopher N. Hessee6b3a3e2017-01-08 00:03:23 +0100346
347 int64_t last_read_time_us;
Christopher N. Hessece6d5af2017-01-12 11:40:30 +0100348 int64_t frames_read; /* total frames read, not cleared when
349 entering standby */
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100350};
351
352struct mixer_card {
353 struct listnode adev_list_node;
354 struct listnode uc_list_node[AUDIO_USECASE_MAX];
355 int card;
356 struct mixer* mixer;
357 struct audio_route* audio_route;
Andreas Schneider759368f2017-02-02 16:11:14 +0100358 struct timespec dsp_poweroff_time;
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100359};
360
361struct audio_usecase {
362 struct listnode adev_list_node;
363 audio_usecase_t id;
364 usecase_type_t type;
365 audio_devices_t devices;
366 snd_device_t out_snd_device;
367 snd_device_t in_snd_device;
368 struct audio_stream* stream;
369 struct listnode mixer_list;
370};
371
Andreas Schneider74ef3a12017-02-02 18:29:12 +0100372struct voice_data {
373 bool in_call;
374 float volume;
375 bool bluetooth_nrec;
376};
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100377
378struct audio_device {
379 struct audio_hw_device device;
380 pthread_mutex_t lock; /* see note below on mutex acquisition order */
381 struct listnode mixer_list;
382 audio_mode_t mode;
383 struct stream_in* active_input;
384 struct stream_out* primary_output;
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100385 bool mic_mute;
Andreas Schneider74ef3a12017-02-02 18:29:12 +0100386
387 struct voice_data voice;
388
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100389 int* snd_dev_ref_cnt;
390 struct listnode usecase_list;
391 bool speaker_lr_swap;
392 unsigned int cur_hdmi_channels;
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100393 bool ns_in_voice_rec;
394
395 void* offload_fx_lib;
396 int (*offload_fx_start_output)(audio_io_handle_t);
397 int (*offload_fx_stop_output)(audio_io_handle_t);
398
399#ifdef PREPROCESSING_ENABLED
400 struct echo_reference_itfe* echo_reference;
401 // echo_reference_generation indicates if the echo reference used by the output stream is
402 // in sync with the one known by the audio_device.
403 // incremented atomically with a memory barrier and audio device mutex locked but WITHOUT
404 // stream mutex locked: the stream will load it atomically with a barrier and re-read it
405 // with audio device mutex if needed
406 volatile int32_t echo_reference_generation;
407#endif
408
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100409 void* sound_trigger_lib;
410 int (*sound_trigger_open_for_streaming)();
411 size_t (*sound_trigger_read_samples)(int, void*, size_t);
412 int (*sound_trigger_close_for_streaming)(int);
413
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100414 pthread_mutex_t lock_inputs; /* see note below on mutex acquisition order */
415};
416
417/*
418 * NOTE: when multiple mutexes have to be acquired, always take the
Christopher N. Hesse2f6f8582017-01-28 12:46:15 +0100419 * lock_inputs, stream_in, stream_out, then audio_device mutex.
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100420 * stream_in mutex must always be before stream_out mutex
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100421 * lock_inputs must be held in order to either close the input stream, or prevent closure.
422 */
423
Christopher N. Hesse0612a4e2017-01-28 14:05:39 +0100424#endif // SAMSUNG_AUDIO_HW_H