blob: 0be414923dbc9bf9d9eb0d7f617380799f228d65 [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>
stenkinevgeniy44335362018-05-07 18:00:13 +00004 * Copyright (C) 2018 The LineageOS Project
Christopher N. Hesse297a6362017-01-28 12:40:45 +01005 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
Christopher N. Hesse0612a4e2017-01-28 14:05:39 +010019#ifndef SAMSUNG_AUDIO_HW_H
20#define SAMSUNG_AUDIO_HW_H
Christopher N. Hesse297a6362017-01-28 12:40:45 +010021
22#include <cutils/list.h>
23#include <hardware/audio.h>
Christopher N. Hesse6a2a3072017-08-04 21:17:55 +020024#include <hardware/audio_amplifier.h>
Christopher N. Hesse297a6362017-01-28 12:40:45 +010025
26#include <tinyalsa/asoundlib.h>
27#include <tinycompress/tinycompress.h>
28/* TODO: remove resampler if possible when AudioFlinger supports downsampling from 48 to 8 */
29#include <audio_utils/resampler.h>
30#include <audio_route/audio_route.h>
31
32/* Retry for delay in FW loading*/
33#define RETRY_NUMBER 10
34#define RETRY_US 500000
35
36#ifdef __LP64__
37#define OFFLOAD_FX_LIBRARY_PATH "/system/lib64/soundfx/libnvvisualizer.so"
38#else
39#define OFFLOAD_FX_LIBRARY_PATH "/system/lib/soundfx/libnvvisualizer.so"
40#endif
41
Christopher N. Hesse297a6362017-01-28 12:40:45 +010042#ifdef PREPROCESSING_ENABLED
43#include <audio_utils/echo_reference.h>
44#define MAX_PREPROCESSORS 3
45struct effect_info_s {
46 effect_handle_t effect_itfe;
47 size_t num_channel_configs;
48 channel_config_t *channel_configs;
49};
50#endif
51
Christopher N. Hesse297a6362017-01-28 12:40:45 +010052/* Sound devices specific to the platform
53 * The DEVICE_OUT_* and DEVICE_IN_* should be mapped to these sound
54 * devices to enable corresponding mixer paths
55 */
56enum {
57 SND_DEVICE_NONE = 0,
58
59 /* Playback devices */
60 SND_DEVICE_MIN,
61 SND_DEVICE_OUT_BEGIN = SND_DEVICE_MIN,
Christopher N. Hesse530cf0d2017-01-31 21:59:54 +010062 SND_DEVICE_OUT_EARPIECE = SND_DEVICE_OUT_BEGIN,
Christopher N. Hesse297a6362017-01-28 12:40:45 +010063 SND_DEVICE_OUT_SPEAKER,
64 SND_DEVICE_OUT_HEADPHONES,
65 SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES,
Christopher N. Hesse530cf0d2017-01-31 21:59:54 +010066 SND_DEVICE_OUT_VOICE_EARPIECE,
Andreas Schneider59486fa2017-02-06 09:16:39 +010067 SND_DEVICE_OUT_VOICE_EARPIECE_WB,
Christopher N. Hesse297a6362017-01-28 12:40:45 +010068 SND_DEVICE_OUT_VOICE_SPEAKER,
Andreas Schneider59486fa2017-02-06 09:16:39 +010069 SND_DEVICE_OUT_VOICE_SPEAKER_WB,
Christopher N. Hesse297a6362017-01-28 12:40:45 +010070 SND_DEVICE_OUT_VOICE_HEADPHONES,
Andreas Schneider59486fa2017-02-06 09:16:39 +010071 SND_DEVICE_OUT_VOICE_HEADPHONES_WB,
Christopher N. Hesse56caa262017-03-20 19:40:53 +010072 SND_DEVICE_OUT_VOICE_BT_SCO,
73 SND_DEVICE_OUT_VOICE_BT_SCO_WB,
Christopher N. Hesse297a6362017-01-28 12:40:45 +010074 SND_DEVICE_OUT_HDMI,
75 SND_DEVICE_OUT_SPEAKER_AND_HDMI,
76 SND_DEVICE_OUT_BT_SCO,
Christopher N. Hesse297a6362017-01-28 12:40:45 +010077 SND_DEVICE_OUT_END,
78
79 /*
80 * Note: IN_BEGIN should be same as OUT_END because total number of devices
81 * SND_DEVICES_MAX should not exceed MAX_RX + MAX_TX devices.
82 */
83 /* Capture devices */
84 SND_DEVICE_IN_BEGIN = SND_DEVICE_OUT_END,
Christopher N. Hesse530cf0d2017-01-31 21:59:54 +010085 SND_DEVICE_IN_EARPIECE_MIC = SND_DEVICE_IN_BEGIN,
Christopher N. Hesse297a6362017-01-28 12:40:45 +010086 SND_DEVICE_IN_SPEAKER_MIC,
87 SND_DEVICE_IN_HEADSET_MIC,
Christopher N. Hesse530cf0d2017-01-31 21:59:54 +010088 SND_DEVICE_IN_EARPIECE_MIC_AEC,
Christopher N. Hesse297a6362017-01-28 12:40:45 +010089 SND_DEVICE_IN_SPEAKER_MIC_AEC,
90 SND_DEVICE_IN_HEADSET_MIC_AEC,
Andreas Schneider82f32482017-02-06 09:00:48 +010091 SND_DEVICE_IN_VOICE_MIC,
92 SND_DEVICE_IN_VOICE_EARPIECE_MIC,
Andreas Schneider59486fa2017-02-06 09:16:39 +010093 SND_DEVICE_IN_VOICE_EARPIECE_MIC_WB,
Christopher N. Hesse297a6362017-01-28 12:40:45 +010094 SND_DEVICE_IN_VOICE_SPEAKER_MIC,
Andreas Schneider59486fa2017-02-06 09:16:39 +010095 SND_DEVICE_IN_VOICE_SPEAKER_MIC_WB,
Christopher N. Hesse297a6362017-01-28 12:40:45 +010096 SND_DEVICE_IN_VOICE_HEADSET_MIC,
Andreas Schneider59486fa2017-02-06 09:16:39 +010097 SND_DEVICE_IN_VOICE_HEADSET_MIC_WB,
Christopher N. Hesse56caa262017-03-20 19:40:53 +010098 SND_DEVICE_IN_VOICE_BT_SCO_MIC,
99 SND_DEVICE_IN_VOICE_BT_SCO_MIC_WB,
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100100 SND_DEVICE_IN_HDMI_MIC,
101 SND_DEVICE_IN_BT_SCO_MIC,
102 SND_DEVICE_IN_CAMCORDER_MIC,
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100103 SND_DEVICE_IN_VOICE_REC_HEADSET_MIC,
104 SND_DEVICE_IN_VOICE_REC_MIC,
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100105 SND_DEVICE_IN_LOOPBACK_AEC,
106 SND_DEVICE_IN_END,
107
108 SND_DEVICE_MAX = SND_DEVICE_IN_END,
109
110};
111
112
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100113/*
114 * tinyAlsa library interprets period size as number of frames
115 * one frame = channel_count * sizeof (pcm sample)
116 * so if format = 16-bit PCM and channels = Stereo, frame size = 2 ch * 2 = 4 bytes
117 * DEEP_BUFFER_OUTPUT_PERIOD_SIZE = 1024 means 1024 * 4 = 4096 bytes
118 * We should take care of returning proper size when AudioFlinger queries for
119 * the buffer size of an input/output stream
120 */
121#define PLAYBACK_PERIOD_SIZE 256
122#define PLAYBACK_PERIOD_COUNT 2
123#define PLAYBACK_DEFAULT_CHANNEL_COUNT 2
124#define PLAYBACK_DEFAULT_SAMPLING_RATE 48000
125#define PLAYBACK_START_THRESHOLD(size, count) (((size) * (count)) - 1)
126#define PLAYBACK_STOP_THRESHOLD(size, count) ((size) * ((count) + 2))
127#define PLAYBACK_AVAILABLE_MIN 1
128
129
130#define SCO_PERIOD_SIZE 168
131#define SCO_PERIOD_COUNT 2
132#define SCO_DEFAULT_CHANNEL_COUNT 2
133#define SCO_DEFAULT_SAMPLING_RATE 8000
Fevax51bd12c2017-03-15 10:56:39 -0300134#define SCO_WB_SAMPLING_RATE 16000
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100135#define SCO_START_THRESHOLD 335
136#define SCO_STOP_THRESHOLD 336
137#define SCO_AVAILABLE_MIN 1
138
139#define PLAYBACK_HDMI_MULTI_PERIOD_SIZE 1024
140#define PLAYBACK_HDMI_MULTI_PERIOD_COUNT 4
141#define PLAYBACK_HDMI_MULTI_DEFAULT_CHANNEL_COUNT 6
142#define PLAYBACK_HDMI_MULTI_PERIOD_BYTES \
143 (PLAYBACK_HDMI_MULTI_PERIOD_SIZE * PLAYBACK_HDMI_MULTI_DEFAULT_CHANNEL_COUNT * 2)
144#define PLAYBACK_HDMI_MULTI_START_THRESHOLD 4095
145#define PLAYBACK_HDMI_MULTI_STOP_THRESHOLD 4096
146#define PLAYBACK_HDMI_MULTI_AVAILABLE_MIN 1
147
148#define PLAYBACK_HDMI_DEFAULT_CHANNEL_COUNT 2
149
150#define CAPTURE_PERIOD_SIZE 1024
151#define CAPTURE_PERIOD_SIZE_LOW_LATENCY 256
152#define CAPTURE_PERIOD_COUNT 2
153#define CAPTURE_PERIOD_COUNT_LOW_LATENCY 2
154#define CAPTURE_DEFAULT_CHANNEL_COUNT 2
155#define CAPTURE_DEFAULT_SAMPLING_RATE 48000
156#define CAPTURE_START_THRESHOLD 1
157
158#define COMPRESS_CARD 0
159#define COMPRESS_DEVICE 5
160#define COMPRESS_OFFLOAD_FRAGMENT_SIZE (32 * 1024)
161#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4
162/* ToDo: Check and update a proper value in msec */
163#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96
164#define COMPRESS_PLAYBACK_VOLUME_MAX 0x10000 //NV suggested value
165
166#define DEEP_BUFFER_OUTPUT_SAMPLING_RATE 48000
167#define DEEP_BUFFER_OUTPUT_PERIOD_SIZE 480
168#define DEEP_BUFFER_OUTPUT_PERIOD_COUNT 8
169
170#define MAX_SUPPORTED_CHANNEL_MASKS 2
171
172typedef int snd_device_t;
173
174/* These are the supported use cases by the hardware.
175 * Each usecase is mapped to a specific PCM device.
176 * Refer to pcm_device_table[].
177 */
178typedef enum {
179 USECASE_INVALID = -1,
180 /* Playback usecases */
181 USECASE_AUDIO_PLAYBACK = 0,
182 USECASE_AUDIO_PLAYBACK_MULTI_CH,
183 USECASE_AUDIO_PLAYBACK_OFFLOAD,
184 USECASE_AUDIO_PLAYBACK_DEEP_BUFFER,
185
186 /* Capture usecases */
187 USECASE_AUDIO_CAPTURE,
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100188
189 USECASE_VOICE_CALL,
190 AUDIO_USECASE_MAX
191} audio_usecase_t;
192
193#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
194
195/*
196 * tinyAlsa library interprets period size as number of frames
197 * one frame = channel_count * sizeof (pcm sample)
198 * so if format = 16-bit PCM and channels = Stereo, frame size = 2 ch * 2 = 4 bytes
199 * DEEP_BUFFER_OUTPUT_PERIOD_SIZE = 1024 means 1024 * 4 = 4096 bytes
200 * We should take care of returning proper size when AudioFlinger queries for
201 * the buffer size of an input/output stream
202 */
203
204enum {
205 OFFLOAD_CMD_EXIT, /* exit compress offload thread loop*/
206 OFFLOAD_CMD_DRAIN, /* send a full drain request to DSP */
207 OFFLOAD_CMD_PARTIAL_DRAIN, /* send a partial drain request to DSP */
208 OFFLOAD_CMD_WAIT_FOR_BUFFER, /* wait for buffer released by DSP */
209};
210
211enum {
212 OFFLOAD_STATE_IDLE,
213 OFFLOAD_STATE_PLAYING,
214 OFFLOAD_STATE_PAUSED,
215 OFFLOAD_STATE_PAUSED_FLUSHED,
216};
217
218typedef enum {
219 PCM_PLAYBACK = 0x1,
220 PCM_CAPTURE = 0x2,
221 VOICE_CALL = 0x4,
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100222 PCM_CAPTURE_LOW_LATENCY = 0x10,
223} usecase_type_t;
224
225struct offload_cmd {
226 struct listnode node;
227 int cmd;
228 int data[];
229};
230
231struct pcm_device_profile {
232 struct pcm_config config;
233 int card;
234 int id;
235 usecase_type_t type;
236 audio_devices_t devices;
237};
238
239struct pcm_device {
240 struct listnode stream_list_node;
241 struct pcm_device_profile* pcm_profile;
242 struct pcm* pcm;
243 int status;
244 /* TODO: remove resampler if possible when AudioFlinger supports downsampling from 48 to 8 */
245 struct resampler_itfe* resampler;
246 int16_t* res_buffer;
247 size_t res_byte_count;
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100248};
249
250struct stream_out {
251 struct audio_stream_out stream;
252 pthread_mutex_t lock; /* see note below on mutex acquisition order */
253 pthread_mutex_t pre_lock; /* acquire before lock to avoid DOS by playback thread */
254 pthread_cond_t cond;
255 struct pcm_config config;
256 struct listnode pcm_dev_list;
257 struct compr_config compr_config;
258 struct compress* compr;
259 int standby;
260 unsigned int sample_rate;
261 audio_channel_mask_t channel_mask;
262 audio_format_t format;
263 audio_devices_t devices;
264 audio_output_flags_t flags;
265 audio_usecase_t usecase;
266 /* Array of supported channel mask configurations. +1 so that the last entry is always 0 */
267 audio_channel_mask_t supported_channel_masks[MAX_SUPPORTED_CHANNEL_MASKS + 1];
268 bool muted;
269 /* total frames written, not cleared when entering standby */
270 uint64_t written;
271 audio_io_handle_t handle;
272
273 int non_blocking;
274 int offload_state;
275 pthread_cond_t offload_cond;
276 pthread_t offload_thread;
277 struct listnode offload_cmd_list;
278 bool offload_thread_blocked;
279
280 stream_callback_t offload_callback;
281 void* offload_cookie;
282 struct compr_gapless_mdata gapless_mdata;
283 int send_new_metadata;
284
285 struct audio_device* dev;
286
287#ifdef PREPROCESSING_ENABLED
288 struct echo_reference_itfe *echo_reference;
289 // echo_reference_generation indicates if the echo reference used by the output stream is
290 // in sync with the one known by the audio_device. When different from the generation stored
291 // in the audio_device the output stream must release the echo reference.
292 // always modified with audio device and stream mutex locked.
293 int32_t echo_reference_generation;
294#endif
295
296 bool is_fastmixer_affinity_set;
Christopher N. Hessee6b3a3e2017-01-08 00:03:23 +0100297
298 int64_t last_write_time_us;
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100299};
300
301struct stream_in {
302 struct audio_stream_in stream;
303 pthread_mutex_t lock; /* see note below on mutex acquisition order */
304 pthread_mutex_t pre_lock; /* acquire before lock to avoid DOS by
305 capture thread */
306 struct pcm_config config;
307 struct listnode pcm_dev_list;
308 int standby;
309 audio_source_t source;
310 audio_devices_t devices;
311 uint32_t main_channels;
312 audio_usecase_t usecase;
313 usecase_type_t usecase_type;
314 bool enable_aec;
315 audio_input_flags_t input_flags;
316
317 /* TODO: remove resampler if possible when AudioFlinger supports downsampling from 48 to 8 */
318 unsigned int requested_rate;
319 struct resampler_itfe* resampler;
320 struct resampler_buffer_provider buf_provider;
321 int read_status;
322 int16_t* read_buf;
323 size_t read_buf_size;
324 size_t read_buf_frames;
325
326 int16_t *proc_buf_in;
327 int16_t *proc_buf_out;
328 size_t proc_buf_size;
329 size_t proc_buf_frames;
330
331#ifdef PREPROCESSING_ENABLED
332 struct echo_reference_itfe *echo_reference;
333 int16_t *ref_buf;
334 size_t ref_buf_size;
335 size_t ref_buf_frames;
336
337#ifdef HW_AEC_LOOPBACK
338 bool hw_echo_reference;
339 int16_t* hw_ref_buf;
340 size_t hw_ref_buf_size;
341#endif
342
343 int num_preprocessors;
344 struct effect_info_s preprocessors[MAX_PREPROCESSORS];
345
346 bool aux_channels_changed;
347 uint32_t aux_channels;
348#endif
349
350 struct audio_device* dev;
351 bool is_fastcapture_affinity_set;
Christopher N. Hessee6b3a3e2017-01-08 00:03:23 +0100352
353 int64_t last_read_time_us;
Christopher N. Hessece6d5af2017-01-12 11:40:30 +0100354 int64_t frames_read; /* total frames read, not cleared when
355 entering standby */
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100356};
357
358struct mixer_card {
359 struct listnode adev_list_node;
360 struct listnode uc_list_node[AUDIO_USECASE_MAX];
361 int card;
362 struct mixer* mixer;
363 struct audio_route* audio_route;
Andreas Schneider759368f2017-02-02 16:11:14 +0100364 struct timespec dsp_poweroff_time;
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100365};
366
367struct audio_usecase {
368 struct listnode adev_list_node;
369 audio_usecase_t id;
370 usecase_type_t type;
371 audio_devices_t devices;
372 snd_device_t out_snd_device;
373 snd_device_t in_snd_device;
374 struct audio_stream* stream;
375 struct listnode mixer_list;
376};
377
Andreas Schneider74ef3a12017-02-02 18:29:12 +0100378struct voice_data {
379 bool in_call;
380 float volume;
381 bool bluetooth_nrec;
Andreas Schneider05bc1882017-02-09 14:03:11 +0100382 bool bluetooth_wb;
Christopher N. Hesse41c9f3d2017-02-02 20:48:56 +0100383 void *session;
Andreas Schneider74ef3a12017-02-02 18:29:12 +0100384};
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100385
386struct audio_device {
387 struct audio_hw_device device;
388 pthread_mutex_t lock; /* see note below on mutex acquisition order */
389 struct listnode mixer_list;
390 audio_mode_t mode;
391 struct stream_in* active_input;
392 struct stream_out* primary_output;
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100393 bool mic_mute;
Andreas Schneiderecd17ce2017-02-09 10:45:21 +0100394 bool screen_off;
Andreas Schneider74ef3a12017-02-02 18:29:12 +0100395
396 struct voice_data voice;
397
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100398 int* snd_dev_ref_cnt;
399 struct listnode usecase_list;
400 bool speaker_lr_swap;
401 unsigned int cur_hdmi_channels;
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100402 bool ns_in_voice_rec;
403
404 void* offload_fx_lib;
405 int (*offload_fx_start_output)(audio_io_handle_t);
406 int (*offload_fx_stop_output)(audio_io_handle_t);
407
408#ifdef PREPROCESSING_ENABLED
409 struct echo_reference_itfe* echo_reference;
410 // echo_reference_generation indicates if the echo reference used by the output stream is
411 // in sync with the one known by the audio_device.
412 // incremented atomically with a memory barrier and audio device mutex locked but WITHOUT
413 // stream mutex locked: the stream will load it atomically with a barrier and re-read it
414 // with audio device mutex if needed
415 volatile int32_t echo_reference_generation;
416#endif
417
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100418 pthread_mutex_t lock_inputs; /* see note below on mutex acquisition order */
Christopher N. Hesse6a2a3072017-08-04 21:17:55 +0200419 amplifier_device_t *amp;
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100420};
421
422/*
423 * NOTE: when multiple mutexes have to be acquired, always take the
Christopher N. Hesse2f6f8582017-01-28 12:46:15 +0100424 * lock_inputs, stream_in, stream_out, then audio_device mutex.
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100425 * stream_in mutex must always be before stream_out mutex
Christopher N. Hesse297a6362017-01-28 12:40:45 +0100426 * lock_inputs must be held in order to either close the input stream, or prevent closure.
427 */
428
Christopher N. Hesse0612a4e2017-01-28 14:05:39 +0100429#endif // SAMSUNG_AUDIO_HW_H