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