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