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 an iodev.
31 * Args:
32 * stream_ptr - Pointer to the stream.
33 * effects - Bit map specifying the enabled effects on this stream.
34 */
35 struct cras_apm_list *cras_apm_list_create(void *stream_ptr, uint64_t effects);
36
37 /*
38 * Creates a cras_apm associated to given dev_ptr and adds it to the list.
39 * If there already exists an APM instance linked to dev_ptr, we assume
40 * the open format is unchanged so just return it.
41 * Args:
42 * list - The list holding APM instances.
43 * dev_ptr - Pointer to the iodev to add new APM for.
44 * fmt - Format of the audio data used for this cras_apm.
45 */
46 struct cras_apm *cras_apm_list_add(struct cras_apm_list *list, void *dev_ptr,
47 const struct cras_audio_format *fmt);
48
49 /*
50 * Gets the cras_apm instance in the list that associates with given dev.
51 * Args:
52 * list - The list holding APM instances.
53 * dev_ptr - The iodev as key to look up associated APM.
54 */
55 struct cras_apm *cras_apm_list_get(struct cras_apm_list *list, void *dev_ptr);
56
57 /*
58 * Gets the effects bit map of the APM list.
59 * Args:
60 * list - The list holding APM instances.
61 */
62 uint64_t cras_apm_list_get_effects(struct cras_apm_list *list);
63
64 /* Removes a cras_apm from list and destroys it. */
65 int cras_apm_list_destroy(struct cras_apm_list *list);
66
67 /*
68 * Removes an APM from the list, expected to be used when an iodev is no
69 * longer open for the client stream holding the APM list.
70 * Args:
71 * list - The list holding APM instances.
72 * dev_ptr - Device pointer used to look up which apm to remove.
73 */
74 void cras_apm_list_remove(struct cras_apm_list *list, void *dev_ptr);
75
76 /* Passes audio data from hardware for cras_apm to process.
77 * Args:
78 * apm - The cras_apm instance.
79 * input - Float buffer from device for apm to process.
80 * offset - Offset in |input| to note the data position to start
81 * reading.
82 */
83 int cras_apm_list_process(struct cras_apm *apm, struct float_buffer *input,
84 unsigned int offset);
85
86 /* Gets the APM processed data in the form of audio area.
87 * Args:
88 * apm - The cras_apm instance that owns the audio area pointer and
89 * processed data.
90 * Returns:
91 * The audio area used to read processed data. No need to free
92 * by caller.
93 */
94 struct cras_audio_area *cras_apm_list_get_processed(struct cras_apm *apm);
95
96 /* Tells |apm| that |frames| of processed data has been used, so |apm|
97 * can allocate space to read more from input device.
98 * Args:
99 * apm - The cras_apm instance owns the processed data.
100 * frames - The number in frames of processed data to mark as used.
101 */
102 void cras_apm_list_put_processed(struct cras_apm *apm, unsigned int frames);
103
104 /* Gets the format of the actual data processed by webrtc-apm library.
105 * Args:
106 * apm - The cras_apm instance holding audio data and format info.
107 */
108 struct cras_audio_format *cras_apm_list_get_format(struct cras_apm *apm);
109
110 /* Sets debug recording to start or stop.
111 * Args:
112 * list - List contains the apm instance to start/stop debug recording.
113 * dev_ptr - Use as key to look up specific apm to do aec dump.
114 * start - True to set debug recording start, otherwise stop.
115 * fd - File descriptor to aec dump destination.
116 */
117 void cras_apm_list_set_aec_dump(struct cras_apm_list *list, void *dev_ptr,
118 int start, int fd);
119
120 #else
121
122 /*
123 * If webrtc audio processing library is not available then define all
124 * cras_apm_list functions as dummy. As long as cras_apm_list_add returns
125 * NULL, non of the other functions should be called.
126 */
cras_apm_list_init(const char * device_config_dir)127 static inline int cras_apm_list_init(const char *device_config_dir)
128 {
129 return 0;
130 }
cras_apm_list_reload_aec_config()131 static inline void cras_apm_list_reload_aec_config()
132 {
133 }
cras_apm_list_create(void * stream_ptr,unsigned int effects)134 static inline struct cras_apm_list *cras_apm_list_create(void *stream_ptr,
135 unsigned int effects)
136 {
137 return NULL;
138 }
139 static inline struct cras_apm *
cras_apm_list_add(struct cras_apm_list * list,void * dev_ptr,const struct cras_audio_format * fmt)140 cras_apm_list_add(struct cras_apm_list *list, void *dev_ptr,
141 const struct cras_audio_format *fmt)
142 {
143 return NULL;
144 }
cras_apm_list_get(struct cras_apm_list * list,void * dev_ptr)145 static inline struct cras_apm *cras_apm_list_get(struct cras_apm_list *list,
146 void *dev_ptr)
147 {
148 return NULL;
149 }
cras_apm_list_get_effects(struct cras_apm_list * list)150 static inline uint64_t cras_apm_list_get_effects(struct cras_apm_list *list)
151 {
152 return 0;
153 }
cras_apm_list_destroy(struct cras_apm_list * list)154 static inline int cras_apm_list_destroy(struct cras_apm_list *list)
155 {
156 return 0;
157 }
cras_apm_list_remove(struct cras_apm_list * list,void * dev_ptr)158 static inline void cras_apm_list_remove(struct cras_apm_list *list,
159 void *dev_ptr)
160 {
161 }
162
cras_apm_list_process(struct cras_apm * apm,struct float_buffer * input,unsigned int offset)163 static inline int cras_apm_list_process(struct cras_apm *apm,
164 struct float_buffer *input,
165 unsigned int offset)
166 {
167 return 0;
168 }
169
170 static inline struct cras_audio_area *
cras_apm_list_get_processed(struct cras_apm * apm)171 cras_apm_list_get_processed(struct cras_apm *apm)
172 {
173 return NULL;
174 }
175
cras_apm_list_put_processed(struct cras_apm * apm,unsigned int frames)176 static inline void cras_apm_list_put_processed(struct cras_apm *apm,
177 unsigned int frames)
178 {
179 }
180
181 static inline struct cras_audio_format *
cras_apm_list_get_format(struct cras_apm * apm)182 cras_apm_list_get_format(struct cras_apm *apm)
183 {
184 return NULL;
185 }
186
cras_apm_list_set_aec_dump(struct cras_apm_list * list,void * dev_ptr,int start,int fd)187 static inline void cras_apm_list_set_aec_dump(struct cras_apm_list *list,
188 void *dev_ptr, int start, int fd)
189 {
190 }
191
192 #endif /* HAVE_WEBRTC_APM */
193
194 #endif /* CRAS_APM_LIST_H_ */
195