1 /* Copyright 2018 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 */
5
6 #ifndef CRAS_APM_LIST_H_
7 #define CRAS_APM_LIST_H_
8
9 #include "cras_types.h"
10
11 struct cras_audio_area;
12 struct cras_audio_format;
13 struct cras_apm;
14 struct cras_apm_list;
15 struct float_buffer;
16
17 #ifdef HAVE_WEBRTC_APM
18
19 /* Initialize the apm list for analyzing output data. */
20 int cras_apm_list_init(const char *device_config_dir);
21
22 /* Reloads the aec config. Used for debug and tuning. */
23 void cras_apm_list_reload_aec_config();
24
25 /* Deinitialize apm list to free all allocated resources. */
26 int cras_apm_list_deinit();
27
28 /*
29 * Creates an list to hold all APM instances created when a stream
30 * attaches to iodev(s). This should be called in main thread.
31 *
32 * Below diagram explains the life cycle of an APM instance, how are
33 * related APIs used, and in which thread should each API be called.
34 *
35 * Main thread Audio thread
36 * maintaining apm_list maintaining active_apms
37 * ----------- ------------
38 * cras_apm_list_create
39 * cras_apm_list_add_apm -> cras_apm_list_start_apm
40 *
41 * cras_apm_list_get_active_apm
42 * cras_apm_list_process
43 * cras_apm_list_get_processed
44 * cras_apm_list_put_processed
45 *
46 * cras_apm_list_remove_apm <- cras_apm_list_stop_apm
47 * cras_apm_list_destroy
48 *
49 * Args:
50 * stream_ptr - Pointer to the stream.
51 * effects - Bit map specifying the enabled effects on this stream.
52 */
53 struct cras_apm_list *cras_apm_list_create(void *stream_ptr, uint64_t effects);
54
55 /*
56 * Creates a cras_apm associated to given dev_ptr and adds it to the list.
57 * If there already exists an APM instance linked to dev_ptr, we assume
58 * the open format is unchanged so just return it. This should be called
59 * in main thread.
60 * Args:
61 * list - The list holding APM instances.
62 * dev_ptr - Pointer to the iodev to add new APM for.
63 * fmt - Format of the audio data used for this cras_apm.
64 * is_aec_use_case - If the dev_ptr is for typical AEC use case.
65 */
66 struct cras_apm *cras_apm_list_add_apm(struct cras_apm_list *list,
67 void *dev_ptr,
68 const struct cras_audio_format *fmt,
69 bool is_aec_use_case);
70
71 /*
72 * Gets the active APM instance that is associated to given stream and dev pair.
73 * This should be called in audio thread.
74 * Args:
75 * stream_ptr - Pointer to the stream.
76 * dev_ptr - The iodev as key to look up associated APM.
77 */
78 struct cras_apm *cras_apm_list_get_active_apm(void *stream_ptr, void *dev_ptr);
79
80 /*
81 * Starts the APM instance in the list that is associated with dev_ptr by
82 * adding it to the active APM list in audio thread.
83 */
84 void cras_apm_list_start_apm(struct cras_apm_list *list, void *dev_ptr);
85
86 /*
87 * Stops the APM instance in the list that is associated with dev_ptr by
88 * removing it from the active APM list in audio thread.
89 */
90 void cras_apm_list_stop_apm(struct cras_apm_list *list, void *dev_ptr);
91
92 /*
93 * Gets the effects bit map of the APM list.
94 * Args:
95 * list - The list holding APM instances.
96 */
97 uint64_t cras_apm_list_get_effects(struct cras_apm_list *list);
98
99 /* Removes a cras_apm from list and destroys it. */
100 int cras_apm_list_destroy(struct cras_apm_list *list);
101
102 /*
103 * Removes an APM from the list, expected to be used when an iodev is no
104 * longer open for the client stream holding the APM list. This should
105 * be called in main thread.
106 * Args:
107 * list - The list holding APM instances.
108 * dev_ptr - Device pointer used to look up which apm to remove.
109 */
110 void cras_apm_list_remove_apm(struct cras_apm_list *list, void *dev_ptr);
111
112 /* Passes audio data from hardware for cras_apm to process.
113 * Args:
114 * apm - The cras_apm instance.
115 * input - Float buffer from device for apm to process.
116 * offset - Offset in |input| to note the data position to start
117 * reading.
118 */
119 int cras_apm_list_process(struct cras_apm *apm, struct float_buffer *input,
120 unsigned int offset);
121
122 /* Gets the APM processed data in the form of audio area.
123 * Args:
124 * apm - The cras_apm instance that owns the audio area pointer and
125 * processed data.
126 * Returns:
127 * The audio area used to read processed data. No need to free
128 * by caller.
129 */
130 struct cras_audio_area *cras_apm_list_get_processed(struct cras_apm *apm);
131
132 /* Tells |apm| that |frames| of processed data has been used, so |apm|
133 * can allocate space to read more from input device.
134 * Args:
135 * apm - The cras_apm instance owns the processed data.
136 * frames - The number in frames of processed data to mark as used.
137 */
138 void cras_apm_list_put_processed(struct cras_apm *apm, unsigned int frames);
139
140 /* Gets the format of the actual data processed by webrtc-apm library.
141 * Args:
142 * apm - The cras_apm instance holding audio data and format info.
143 */
144 struct cras_audio_format *cras_apm_list_get_format(struct cras_apm *apm);
145
146 /*
147 * Gets if this apm instance is using tuned settings.
148 */
149 bool cras_apm_list_get_use_tuned_settings(struct cras_apm *apm);
150
151 /* Sets debug recording to start or stop.
152 * Args:
153 * list - List contains the apm instance to start/stop debug recording.
154 * dev_ptr - Use as key to look up specific apm to do aec dump.
155 * start - True to set debug recording start, otherwise stop.
156 * fd - File descriptor to aec dump destination.
157 */
158 void cras_apm_list_set_aec_dump(struct cras_apm_list *list, void *dev_ptr,
159 int start, int fd);
160
161 #else
162
163 /*
164 * If webrtc audio processing library is not available then define all
165 * cras_apm_list functions as empty. As long as cras_apm_list_add returns
166 * NULL, non of the other functions should be called.
167 */
cras_apm_list_init(const char * device_config_dir)168 static inline int cras_apm_list_init(const char *device_config_dir)
169 {
170 return 0;
171 }
cras_apm_list_reload_aec_config()172 static inline void cras_apm_list_reload_aec_config()
173 {
174 }
cras_apm_list_create(void * stream_ptr,unsigned int effects)175 static inline struct cras_apm_list *cras_apm_list_create(void *stream_ptr,
176 unsigned int effects)
177 {
178 return NULL;
179 }
180 static inline struct cras_apm *
cras_apm_list_add_apm(struct cras_apm_list * list,void * dev_ptr,const struct cras_audio_format * fmt,bool is_aec_use_case)181 cras_apm_list_add_apm(struct cras_apm_list *list, void *dev_ptr,
182 const struct cras_audio_format *fmt, bool is_aec_use_case)
183 {
184 return NULL;
185 }
cras_apm_list_get_active_apm(void * stream_ptr,void * dev_ptr)186 static inline struct cras_apm *cras_apm_list_get_active_apm(void *stream_ptr,
187 void *dev_ptr)
188 {
189 return NULL;
190 }
cras_apm_list_get_effects(struct cras_apm_list * list)191 static inline uint64_t cras_apm_list_get_effects(struct cras_apm_list *list)
192 {
193 return 0;
194 }
cras_apm_list_destroy(struct cras_apm_list * list)195 static inline int cras_apm_list_destroy(struct cras_apm_list *list)
196 {
197 return 0;
198 }
cras_apm_list_remove_apm(struct cras_apm_list * list,void * dev_ptr)199 static inline void cras_apm_list_remove_apm(struct cras_apm_list *list,
200 void *dev_ptr)
201 {
202 }
203
cras_apm_list_process(struct cras_apm * apm,struct float_buffer * input,unsigned int offset)204 static inline int cras_apm_list_process(struct cras_apm *apm,
205 struct float_buffer *input,
206 unsigned int offset)
207 {
208 return 0;
209 }
210
211 static inline struct cras_audio_area *
cras_apm_list_get_processed(struct cras_apm * apm)212 cras_apm_list_get_processed(struct cras_apm *apm)
213 {
214 return NULL;
215 }
216
cras_apm_list_put_processed(struct cras_apm * apm,unsigned int frames)217 static inline void cras_apm_list_put_processed(struct cras_apm *apm,
218 unsigned int frames)
219 {
220 }
cras_apm_list_start_apm(struct cras_apm_list * list,void * dev_ptr)221 static inline void cras_apm_list_start_apm(struct cras_apm_list *list,
222 void *dev_ptr)
223 {
224 }
cras_apm_list_stop_apm(struct cras_apm_list * list,void * dev_ptr)225 static inline void cras_apm_list_stop_apm(struct cras_apm_list *list,
226 void *dev_ptr)
227 {
228 }
229
230 static inline struct cras_audio_format *
cras_apm_list_get_format(struct cras_apm * apm)231 cras_apm_list_get_format(struct cras_apm *apm)
232 {
233 return NULL;
234 }
235
cras_apm_list_get_use_tuned_settings(struct cras_apm * apm)236 static inline bool cras_apm_list_get_use_tuned_settings(struct cras_apm *apm)
237 {
238 return 0;
239 }
240
cras_apm_list_set_aec_dump(struct cras_apm_list * list,void * dev_ptr,int start,int fd)241 static inline void cras_apm_list_set_aec_dump(struct cras_apm_list *list,
242 void *dev_ptr, int start, int fd)
243 {
244 }
245
246 #endif /* HAVE_WEBRTC_APM */
247
248 #endif /* CRAS_APM_LIST_H_ */
249