1 /* Copyright (c) 2012 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  * Handles various system-level settings.
6  *
7  * Volume:  The system volume is represented as a value from 0 to 100.  This
8  * number will be interpreted by the output device and applied to the hardware.
9  * The value will be mapped to dB by the active device as it will know its curve
10  * the best.
11  */
12 
13 #ifndef CRAS_SYSTEM_STATE_H_
14 #define CRAS_SYSTEM_STATE_H_
15 
16 #include <stdbool.h>
17 #include <stddef.h>
18 
19 #include "cras_types.h"
20 
21 #define CRAS_MAX_SYSTEM_VOLUME 100
22 #define DEFAULT_CAPTURE_GAIN 2000 /* 20dB of gain. */
23 /* Default to -6 dBFS as 90% of CrOS boards use microphone with -26dBFS
24  * sensitivity under 94dB SPL @ 1kHz and we generally added 20dB gain to it.
25  * This is a temporary value that should be refined when the standard process
26  * measuring intrinsic sensitivity is built. */
27 #define DEFAULT_CAPTURE_VOLUME_DBFS -600
28 /* Default to 1--dB of range for playback and capture. */
29 #define DEFAULT_MIN_VOLUME_DBFS -10000
30 #define DEFAULT_MAX_VOLUME_DBFS 0
31 #define DEFAULT_MIN_CAPTURE_GAIN -5000
32 #define DEFAULT_MAX_CAPTURE_GAIN 5000
33 
34 struct cras_tm;
35 
36 /* Initialize system settings.
37  *
38  * Args:
39  *    device_config_dir - Directory for device configs where volume curves live.
40  *    shm_name - Name of the shared memory region used to store the state.
41  *    rw_shm_fd - FD of the shm region.
42  *    ro_shm_fd - FD of the shm region opened RO for sharing with clients.
43  *    exp_state - Shared memory region for storing state.
44  *    exp_state_size - Size of |exp_state|.
45  */
46 void cras_system_state_init(const char *device_config_dir, const char *shm_name,
47 			    int rw_shm_fd, int ro_shm_fd,
48 			    struct cras_server_state *exp_state,
49 			    size_t exp_state_size);
50 void cras_system_state_deinit();
51 
52 /* Sets the suffix string to control which UCM config fo load. */
53 void cras_system_state_set_internal_ucm_suffix(const char *internal_ucm_suffix);
54 
55 /* Sets the system volume.  Will be applied by the active device. */
56 void cras_system_set_volume(size_t volume);
57 /* Gets the current system volume. */
58 size_t cras_system_get_volume();
59 
60 /* Gets the current system capture volume. As we remove the support of setting
61  * system capture gain, it should always be DEFAULT_CAPTURE_GAIN now. */
62 long cras_system_get_capture_gain();
63 
64 /* Sets if the system is muted by the user. */
65 void cras_system_set_user_mute(int muted);
66 /* Sets if the system is muted for . */
67 void cras_system_set_mute(int muted);
68 /* Sets if the system muting is locked or not. */
69 void cras_system_set_mute_locked(int locked);
70 /* Gets the current mute state of the system. */
71 int cras_system_get_mute();
72 /* Gets the current user mute state. */
73 int cras_system_get_user_mute();
74 /* Gets the current system mute state. */
75 int cras_system_get_system_mute();
76 /* Gets if the system muting is locked or not. */
77 int cras_system_get_mute_locked();
78 
79 /* Gets the suspend state of audio. */
80 int cras_system_get_suspended();
81 
82 /* Sets the suspend state of audio.
83  * Args:
84  *    suspend - True for suspend, false for resume.
85  */
86 void cras_system_set_suspended(int suspend);
87 
88 /* Sets if the system capture path is muted or not. */
89 void cras_system_set_capture_mute(int muted);
90 /* Sets if the system capture path muting is locked or not. */
91 void cras_system_set_capture_mute_locked(int locked);
92 /* Gets the current mute state of the system capture path. */
93 int cras_system_get_capture_mute();
94 /* Gets if the system capture path muting is locked or not. */
95 int cras_system_get_capture_mute_locked();
96 
97 /* Sets the value in dB of the MAX and MIN volume settings.  This will allow
98  * clients to query what range of control is available.  Both arguments are
99  * specified as dB * 100.
100  * Args:
101  *     min - dB value when volume = 1 (0 mutes).
102  *     max - dB value when volume = CRAS_MAX_SYSTEM_VOLUME
103  */
104 void cras_system_set_volume_limits(long min, long max);
105 /* Returns the dB value when volume = 1, in dB * 100. */
106 long cras_system_get_min_volume();
107 /* Returns the dB value when volume = CRAS_MAX_SYSTEM_VOLUME, in dB * 100. */
108 long cras_system_get_max_volume();
109 
110 /* Returns the default value of output buffer size in frames. */
111 int cras_system_get_default_output_buffer_size();
112 
113 /* Returns if system aec is supported. */
114 int cras_system_get_aec_supported();
115 
116 /* Returns the system aec group id is available. */
117 int cras_system_get_aec_group_id();
118 
119 /* Sets the flag to enable or disable bluetooth wideband speech feature. */
120 void cras_system_set_bt_wbs_enabled(bool enabled);
121 
122 /* Gets the elable flag of bluetooth wideband speech feature. */
123 bool cras_system_get_bt_wbs_enabled();
124 
125 /*
126  * Returns if Bluetooth WBS mic should be deprioritized for selecting
127  * as default audio input option.
128  */
129 bool cras_system_get_deprioritize_bt_wbs_mic();
130 
131 /* Sets the flag to enable or disable Bluetooth fixed A2DP packet size. */
132 void cras_system_set_bt_fix_a2dp_packet_size_enabled(bool enabled);
133 
134 /* Gets the flag of Bluetooth fixed A2DP packet size. */
135 bool cras_system_get_bt_fix_a2dp_packet_size_enabled();
136 
137 /* Sets the flag to enable or disable Noise Cancellation. */
138 void cras_system_set_noise_cancellation_enabled(bool enabled);
139 
140 /* Gets the flag of Noise Cancellation. */
141 bool cras_system_get_noise_cancellation_enabled();
142 
143 /* Checks if the card ignores the ucm suffix. */
144 bool cras_system_check_ignore_ucm_suffix(const char *card_name);
145 
146 /* Returns true if hotword detection is paused at system suspend. */
147 bool cras_system_get_hotword_pause_at_suspend();
148 
149 /* Sets whether to pause hotword detection at system suspend. */
150 void cras_system_set_hotword_pause_at_suspend(bool pause);
151 
152 /* Adds a card at the given index to the system.  When a new card is found
153  * (through a udev event notification) this will add the card to the system,
154  * causing its devices to become available for playback/capture.
155  * Args:
156  *    alsa_card_info - Info about the alsa card (Index, type, etc.).
157  * Returns:
158  *    0 on success, negative error on failure (Can't create or card already
159  *    exists).
160  */
161 int cras_system_add_alsa_card(struct cras_alsa_card_info *alsa_card_info);
162 
163 /* Removes a card.  When a device is removed this will do the cleanup.  Device
164  * at index must have been added using cras_system_add_alsa_card().
165  * Args:
166  *    alsa_card_index - Index ALSA uses to refer to the card.  The X in "hw:X".
167  * Returns:
168  *    0 on success, negative error on failure (Can't destroy or card doesn't
169  *    exist).
170  */
171 int cras_system_remove_alsa_card(size_t alsa_card_index);
172 
173 /* Checks if an alsa card has been added to the system.
174  * Args:
175  *    alsa_card_index - Index ALSA uses to refer to the card.  The X in "hw:X".
176  * Returns:
177  *    1 if the card has already been added, 0 if not.
178  */
179 int cras_system_alsa_card_exists(unsigned alsa_card_index);
180 
181 /* Poll interface provides a way of getting a callback when 'select'
182  * returns for a given file descriptor.
183  */
184 
185 /* Register the function to use to inform the select loop about new
186  * file descriptors and callbacks.
187  * Args:
188  *    add - The function to call when a new fd is added.
189  *    rm - The function to call when a new fd is removed.
190  *    select_data - Additional value passed back to add and remove.
191  * Returns:
192  *    0 on success, or -EBUSY if there is already a registered handler.
193  */
194 int cras_system_set_select_handler(
195 	int (*add)(int fd, void (*callback)(void *data, int revents),
196 		   void *callback_data, int events, void *select_data),
197 	void (*rm)(int fd, void *select_data), void *select_data);
198 
199 /* Adds the fd and callback pair.  When select indicates that fd is readable,
200  * the callback will be called.
201  * Args:
202  *    fd - The file descriptor to pass to select(2).
203  *    callback - The callback to call when fd is ready.
204  *    callback_data - Value passed back to the callback.
205  *    events - The events to poll for.
206  * Returns:
207  *    0 on success or a negative error code on failure.
208  */
209 int cras_system_add_select_fd(int fd, void (*callback)(void *data, int revents),
210 			      void *callback_data, int events);
211 
212 /* Removes the fd from the list of fds that are passed to select.
213  * Args:
214  *    fd - The file descriptor to remove from the list.
215  */
216 void cras_system_rm_select_fd(int fd);
217 
218 /*
219  * Register the function to use to add a task for main thread to execute.
220  * Args:
221  *    add_task - The function to call when new task is added.
222  *    task_data - Additional data to pass back to add_task.
223  * Returns:
224  *    0 on success, or -EEXIST if there's already a registered handler.
225  */
226 int cras_system_set_add_task_handler(int (*add_task)(void (*cb)(void *data),
227 						     void *callback_data,
228 						     void *task_data),
229 				     void *task_data);
230 
231 /*
232  * Adds a task callback and data pair, to be executed in the next main thread
233  * loop without additional wait time.
234  * Args:
235  *    callback - The function to execute.
236  *    callback_data - The data to be passed to callback when executed.
237  * Returns:
238  *    0 on success, or -EINVAL if there's no handler for adding task.
239  */
240 int cras_system_add_task(void (*callback)(void *data), void *callback_data);
241 
242 /* Signals that an audio input or output stream has been added to the system.
243  * This allows the count of active streams can be used to notice when the audio
244  * subsystem is idle.
245  * Args:
246  *   direction - Directions of audio streams.
247  *   client_type - CRAS_CLIENT_TYPE of the audio stream.
248  */
249 void cras_system_state_stream_added(enum CRAS_STREAM_DIRECTION direction,
250 				    enum CRAS_CLIENT_TYPE client_type);
251 
252 /* Signals that an audio input or output stream has been removed from the
253  * system.  This allows the count of active streams can be used to notice when
254  * the audio subsystem is idle.
255  * Args:
256  *   direction - Directions of audio stream.
257  *   client_type - CRAS_CLIENT_TYPE of the audio stream.
258  */
259 void cras_system_state_stream_removed(enum CRAS_STREAM_DIRECTION direction,
260 				      enum CRAS_CLIENT_TYPE client_type);
261 
262 /* Returns the number of active playback and capture streams. */
263 unsigned cras_system_state_get_active_streams();
264 
265 /* Returns the number of active streams with given direction.
266  * Args:
267  *   direction - Directions of audio stream.
268  */
269 unsigned cras_system_state_get_active_streams_by_direction(
270 	enum CRAS_STREAM_DIRECTION direction);
271 
272 /* Returns the number of input streams with permission per CRAS_CLIENT_TYPE.
273  *
274  * Returns:
275  *   num_input_streams - An array with length = CRAS_NUM_CLIENT_TYPE and each
276  *                        element is the number of the current input
277  *                        streams with permission in each client type.
278  */
279 void cras_system_state_get_input_streams_with_permission(
280 	uint32_t num_input_streams[CRAS_NUM_CLIENT_TYPE]);
281 
282 /* Fills ts with the time the last stream was removed from the system, the time
283  * the stream count went to zero.
284  */
285 void cras_system_state_get_last_stream_active_time(struct cras_timespec *ts);
286 
287 /* Returns output devices information.
288  * Args:
289  *    devs - returns the array of output devices information.
290  * Returns:
291  *    number of output devices.
292  */
293 int cras_system_state_get_output_devs(const struct cras_iodev_info **devs);
294 
295 /* Returns input devices information.
296  * Args:
297  *    devs - returns the array of input devices information.
298  * Returns:
299  *    number of input devices.
300  */
301 int cras_system_state_get_input_devs(const struct cras_iodev_info **devs);
302 
303 /* Returns output nodes information.
304  * Args:
305  *    nodes - returns the array of output nodes information.
306  * Returns:
307  *    number of output nodes.
308  */
309 int cras_system_state_get_output_nodes(const struct cras_ionode_info **nodes);
310 
311 /* Returns input nodes information.
312  * Args:
313  *    nodes - returns the array of input nodes information.
314  * Returns:
315  *    number of input nodes.
316  */
317 int cras_system_state_get_input_nodes(const struct cras_ionode_info **nodes);
318 
319 /*
320  * Sets the non-empty audio status.
321  */
322 void cras_system_state_set_non_empty_status(int non_empty);
323 
324 /*
325  * Returns the non-empty audio status.
326  */
327 int cras_system_state_get_non_empty_status();
328 
329 /* Returns a pointer to the current system state that is shared with clients.
330  * This also 'locks' the structure by incrementing the update count to an odd
331  * value.
332  */
333 struct cras_server_state *cras_system_state_update_begin();
334 
335 /* Unlocks the system state structure that was updated after calling
336  * cras_system_state_update_begin by again incrementing the update count.
337  */
338 void cras_system_state_update_complete();
339 
340 /* Gets a pointer to the system state without locking it.  Only used for debug
341  * log.  Don't add calls to this function. */
342 struct cras_server_state *cras_system_state_get_no_lock();
343 
344 /* Returns the shm fd for the server_state structure. */
345 key_t cras_sys_state_shm_fd();
346 
347 /* Returns the timer manager. */
348 struct cras_tm *cras_system_state_get_tm();
349 
350 /*
351  * Add snapshot to snapshot buffer in system state
352  */
353 void cras_system_state_add_snapshot(struct cras_audio_thread_snapshot *);
354 
355 /*
356  * Dump snapshots from system state to shared memory with client
357  */
358 void cras_system_state_dump_snapshots();
359 
360 /*
361  * Returns true if in the main thread.
362  */
363 int cras_system_state_in_main_thread();
364 
365 #endif /* CRAS_SYSTEM_STATE_H_ */
366