1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef DRAGON_AUDIO_HW_H
18 #define DRAGON_AUDIO_HW_H
19 
20 #include <cutils/list.h>
21 #include <hardware/audio.h>
22 
23 #include <tinyalsa/asoundlib.h>
24 /* TODO: remove resampler if possible when AudioFlinger supports downsampling from 48 to 8 */
25 #include <audio_utils/resampler.h>
26 #include <audio_route/audio_route.h>
27 
28 #define PREPROCESSING_ENABLED
29 
30 #define SOUND_TRIGGER_HAL_LIBRARY_PATH "/system/lib/hw/sound_trigger.primary.dragon.so"
31 
32 /* Retry for delay in FW loading*/
33 #define RETRY_NUMBER 10
34 #define RETRY_US 500000
35 
36 #ifdef PREPROCESSING_ENABLED
37 #define MAX_PREPROCESSORS 3
38 struct effect_info_s {
39     effect_handle_t effect_itfe;
40     size_t num_channel_configs;
41     channel_config_t *channel_configs;
42 };
43 #endif
44 
45 #define TTY_MODE_OFF    1
46 #define TTY_MODE_FULL   2
47 #define TTY_MODE_VCO    4
48 #define TTY_MODE_HCO    8
49 
50 #define DUALMIC_CONFIG_NONE 0
51 #define DUALMIC_CONFIG_1 1
52 
53 /* Sound devices specific to the platform
54  * The DEVICE_OUT_* and DEVICE_IN_* should be mapped to these sound
55  * devices to enable corresponding mixer paths
56  */
57 enum {
58     SND_DEVICE_NONE = 0,
59 
60     /* Playback devices */
61     SND_DEVICE_MIN,
62     SND_DEVICE_OUT_BEGIN = SND_DEVICE_MIN,
63     SND_DEVICE_OUT_HANDSET = SND_DEVICE_OUT_BEGIN,
64     SND_DEVICE_OUT_SPEAKER,
65     SND_DEVICE_OUT_HEADPHONES,
66     SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES,
67     SND_DEVICE_OUT_VOICE_HANDSET,
68     SND_DEVICE_OUT_VOICE_SPEAKER,
69     SND_DEVICE_OUT_VOICE_HEADPHONES,
70     SND_DEVICE_OUT_HDMI,
71     SND_DEVICE_OUT_SPEAKER_AND_HDMI,
72     SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES,
73     SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES,
74     SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET,
75     SND_DEVICE_OUT_END,
76 
77     /*
78      * Note: IN_BEGIN should be same as OUT_END because total number of devices
79      * SND_DEVICES_MAX should not exceed MAX_RX + MAX_TX devices.
80      */
81     /* Capture devices */
82     SND_DEVICE_IN_BEGIN = SND_DEVICE_OUT_END,
83     SND_DEVICE_IN_HANDSET_MIC  = SND_DEVICE_IN_BEGIN,
84     SND_DEVICE_IN_SPEAKER_MIC,
85     SND_DEVICE_IN_HEADSET_MIC,
86     SND_DEVICE_IN_HANDSET_MIC_AEC,
87     SND_DEVICE_IN_SPEAKER_MIC_AEC,
88     SND_DEVICE_IN_HEADSET_MIC_AEC,
89     SND_DEVICE_IN_VOICE_SPEAKER_MIC,
90     SND_DEVICE_IN_VOICE_HEADSET_MIC,
91     SND_DEVICE_IN_HDMI_MIC,
92     SND_DEVICE_IN_CAMCORDER_MIC,
93     SND_DEVICE_IN_VOICE_DMIC_1,
94     SND_DEVICE_IN_VOICE_SPEAKER_DMIC_1,
95     SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC,
96     SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC,
97     SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC,
98     SND_DEVICE_IN_VOICE_REC_HEADSET_MIC,
99     SND_DEVICE_IN_VOICE_REC_MIC,
100     SND_DEVICE_IN_VOICE_REC_DMIC_1,
101     SND_DEVICE_IN_VOICE_REC_DMIC_NS_1,
102     SND_DEVICE_IN_LOOPBACK_AEC,
103     SND_DEVICE_IN_END,
104 
105     SND_DEVICE_MAX = SND_DEVICE_IN_END,
106 
107 };
108 
109 
110 #define MIXER_CARD 0
111 #define SOUND_CARD 0
112 
113 /*
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 512
122 #define PLAYBACK_PERIOD_COUNT 2
123 #define PLAYBACK_DEFAULT_CHANNEL_COUNT 4
124 #define PLAYBACK_DEFAULT_SAMPLING_RATE 48000
125 #define PLAYBACK_START_THRESHOLD ((PLAYBACK_PERIOD_SIZE * PLAYBACK_PERIOD_COUNT) - 1)
126 #define PLAYBACK_STOP_THRESHOLD (PLAYBACK_PERIOD_SIZE * PLAYBACK_PERIOD_COUNT)
127 #define PLAYBACK_AVAILABLE_MIN 1
128 
129 #define PLAYBACK_HDMI_MULTI_PERIOD_SIZE  1024
130 #define PLAYBACK_HDMI_MULTI_PERIOD_COUNT 4
131 #define PLAYBACK_HDMI_MULTI_DEFAULT_CHANNEL_COUNT 6
132 #define PLAYBACK_HDMI_MULTI_PERIOD_BYTES \
133     (PLAYBACK_HDMI_MULTI_PERIOD_SIZE * PLAYBACK_HDMI_MULTI_DEFAULT_CHANNEL_COUNT * 2)
134 #define PLAYBACK_HDMI_MULTI_START_THRESHOLD 4095
135 #define PLAYBACK_HDMI_MULTI_STOP_THRESHOLD 4096
136 #define PLAYBACK_HDMI_MULTI_AVAILABLE_MIN 1
137 
138 #define PLAYBACK_HDMI_DEFAULT_CHANNEL_COUNT   2
139 
140 #define CAPTURE_PERIOD_SIZE 1024
141 #define CAPTURE_PERIOD_SIZE_LOW_LATENCY 512
142 #define CAPTURE_PERIOD_COUNT 2
143 #define CAPTURE_DEFAULT_CHANNEL_COUNT 4
144 #define CAPTURE_DEFAULT_SAMPLING_RATE 48000
145 #define CAPTURE_START_THRESHOLD 1
146 
147 #define DEEP_BUFFER_OUTPUT_SAMPLING_RATE 48000
148 #define DEEP_BUFFER_OUTPUT_PERIOD_SIZE 1440
149 #define DEEP_BUFFER_OUTPUT_PERIOD_COUNT 8
150 
151 #define MAX_SUPPORTED_CHANNEL_MASKS 2
152 
153 struct cras_dsp_context;
154 
155 typedef int snd_device_t;
156 
157 /* These are the supported use cases by the hardware.
158  * Each usecase is mapped to a specific PCM device.
159  * Refer to pcm_device_table[].
160  */
161 typedef enum {
162     USECASE_INVALID = -1,
163     /* Playback usecases */
164     USECASE_AUDIO_PLAYBACK = 0,
165     USECASE_AUDIO_PLAYBACK_MULTI_CH,
166     USECASE_AUDIO_PLAYBACK_DEEP_BUFFER,
167 
168     /* Capture usecases */
169     USECASE_AUDIO_CAPTURE,
170     USECASE_AUDIO_CAPTURE_HOTWORD,
171 
172     USECASE_VOICE_CALL,
173     AUDIO_USECASE_MAX
174 } audio_usecase_t;
175 
176 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
177 
178 /*
179  * tinyAlsa library interprets period size as number of frames
180  * one frame = channel_count * sizeof (pcm sample)
181  * so if format = 16-bit PCM and channels = Stereo, frame size = 2 ch * 2 = 4 bytes
182  * DEEP_BUFFER_OUTPUT_PERIOD_SIZE = 1024 means 1024 * 4 = 4096 bytes
183  * We should take care of returning proper size when AudioFlinger queries for
184  * the buffer size of an input/output stream
185  */
186 
187 typedef enum {
188     PCM_PLAYBACK = 0x1,
189     PCM_CAPTURE = 0x2,
190     VOICE_CALL = 0x4,
191     PCM_HOTWORD_STREAMING = 0x8
192 } usecase_type_t;
193 
194 struct pcm_device_profile {
195     struct pcm_config config;
196     int               card;
197     int               device;
198     int               id;
199     usecase_type_t    type;
200     audio_devices_t   devices;
201     const char*       dsp_name;
202 };
203 
204 struct pcm_device {
205     struct listnode            stream_list_node;
206     struct pcm_device_profile* pcm_profile;
207     struct pcm*                pcm;
208     int                        status;
209     /* TODO: remove resampler if possible when AudioFlinger supports downsampling from 48 to 8 */
210     struct resampler_itfe*     resampler;
211     int16_t*                   res_buffer;
212     size_t                     res_byte_count;
213     struct cras_dsp_context*   dsp_context;
214     int                        sound_trigger_handle;
215 };
216 
217 struct stream_out {
218     struct audio_stream_out     stream;
219     pthread_mutex_t             lock; /* see note below on mutex acquisition order */
220     pthread_mutex_t             pre_lock; /* acquire before lock to avoid DOS by playback thread */
221     pthread_cond_t              cond;
222     struct pcm_config           config;
223     struct listnode             pcm_dev_list;
224     int                         standby;
225     unsigned int                sample_rate;
226     audio_channel_mask_t        channel_mask;
227     audio_format_t              format;
228     audio_devices_t             devices;
229     audio_output_flags_t        flags;
230     audio_usecase_t             usecase;
231     /* Array of supported channel mask configurations. +1 so that the last entry is always 0 */
232     audio_channel_mask_t        supported_channel_masks[MAX_SUPPORTED_CHANNEL_MASKS + 1];
233     bool                        muted;
234     /* total frames written, not cleared when entering standby */
235     uint64_t                    written;
236     audio_io_handle_t           handle;
237 
238     int                         non_blocking;
239 
240     int                         send_new_metadata;
241 
242     struct audio_device*        dev;
243     void *proc_buf_out;
244     size_t proc_buf_size;
245 };
246 
247 struct stream_in {
248     struct audio_stream_in              stream;
249     pthread_mutex_t                     lock; /* see note below on mutex acquisition order */
250     pthread_mutex_t                     pre_lock; /* acquire before lock to avoid DOS by
251                                                      capture thread */
252     struct pcm_config                   config;
253     struct listnode                     pcm_dev_list;
254     int                                 standby;
255     audio_source_t                      source;
256     audio_devices_t                     devices;
257     uint32_t                            main_channels;
258     audio_usecase_t                     usecase;
259     usecase_type_t                      usecase_type;
260     bool                                enable_aec;
261     audio_input_flags_t                 input_flags;
262 
263     /* TODO: remove resampler if possible when AudioFlinger supports downsampling from 48 to 8 */
264     unsigned int                        requested_rate;
265     struct resampler_itfe*              resampler;
266     struct resampler_buffer_provider    buf_provider;
267     int                                 read_status;
268     int16_t*                            read_buf;
269     size_t                              read_buf_size;
270     size_t                              read_buf_frames;
271 
272     void *proc_buf_out;
273     size_t proc_buf_size;
274 
275 #ifdef PREPROCESSING_ENABLED
276     void *proc_buf_in;
277     size_t proc_buf_frames;
278 
279     int num_preprocessors;
280     struct effect_info_s preprocessors[MAX_PREPROCESSORS];
281 
282     bool aux_channels_changed;
283     uint32_t aux_channels;
284 #endif
285 
286     struct audio_device*                dev;
287 };
288 
289 struct mixer_card {
290     struct listnode     adev_list_node;
291     struct listnode     uc_list_node[AUDIO_USECASE_MAX];
292     int                 card;
293     struct mixer*       mixer;
294     struct audio_route* audio_route;
295 };
296 
297 struct audio_usecase {
298     struct listnode         adev_list_node;
299     audio_usecase_t         id;
300     usecase_type_t          type;
301     audio_devices_t         devices;
302     snd_device_t            out_snd_device;
303     snd_device_t            in_snd_device;
304     struct audio_stream*    stream;
305     struct listnode         mixer_list;
306 };
307 
308 
309 struct audio_device {
310     struct audio_hw_device  device;
311     pthread_mutex_t         lock; /* see note below on mutex acquisition order */
312     struct listnode         mixer_list;
313     audio_mode_t            mode;
314     struct stream_in*       active_input;
315     struct stream_out*      primary_output;
316     int                     in_call;
317     float                   voice_volume;
318     bool                    mic_mute;
319     int                     tty_mode;
320     bool                    bluetooth_nrec;
321     bool                    screen_off;
322     int*                    snd_dev_ref_cnt;
323     struct listnode         usecase_list;
324     bool                    speaker_lr_swap;
325     unsigned int            cur_hdmi_channels;
326     int                     dualmic_config;
327     bool                    ns_in_voice_rec;
328 
329     void*                   sound_trigger_lib;
330     int                     (*sound_trigger_open_for_streaming)();
331     size_t                  (*sound_trigger_read_samples)(int, void*, size_t);
332     int                     (*sound_trigger_close_for_streaming)(int);
333 
334     int                     dummybuf_thread_timeout;
335     int                     dummybuf_thread_cancel;
336     int                     dummybuf_thread_active;
337     audio_devices_t         dummybuf_thread_devices;
338     pthread_mutex_t         dummybuf_thread_lock;
339     pthread_t               dummybuf_thread;
340 
341     pthread_mutex_t         lock_inputs; /* see note below on mutex acquisition order */
342 };
343 
344 /*
345  * NOTE: when multiple mutexes have to be acquired, always take the
346  * lock_inputs, stream_in, stream_out, and then audio_device.
347  * stream_in mutex must always be before stream_out mutex
348  * if both have to be taken (see get_echo_reference(), put_echo_reference()...)
349  * dummybuf_thread mutex is not related to the other mutexes with respect to order.
350  * lock_inputs must be held in order to either close the input stream, or prevent closure.
351  */
352 
353 #endif // NVIDIA_AUDIO_HW_H
354