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