• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <stddef.h>
17 
18 #include "cras_types.h"
19 
20 #define CRAS_MAX_SYSTEM_VOLUME 100
21 #define DEFAULT_CAPTURE_GAIN 2000 /* 20dB of gain. */
22 /* Default to 1--dB of range for palyback and capture. */
23 #define DEFAULT_MIN_VOLUME_DBFS -10000
24 #define DEFAULT_MAX_VOLUME_DBFS 0
25 #define DEFAULT_MIN_CAPTURE_GAIN -5000
26 #define DEFAULT_MAX_CAPTURE_GAIN 5000
27 
28 struct cras_tm;
29 
30 /* Initialize system settings.
31  *
32  * Args:
33  *    device_config_dir - Directory for device configs where volume curves live.
34  *    shm_name - Name of the shared memory region used to store the state.
35  *    rw_shm_fd - FD of the shm region.
36  *    ro_shm_fd - FD of the shm region opened RO for sharing with clients.
37  *    exp_state - Shared memory region for storing state.
38  *    exp_state_size - Size of |exp_state|.
39  */
40 void cras_system_state_init(const char *device_config_dir,
41                             const char *shm_name,
42                             int rw_shm_fd,
43                             int ro_shm_fd,
44                             struct cras_server_state *exp_state,
45                             size_t exp_state_size);
46 void cras_system_state_deinit();
47 
48 /* Sets the suffix string to control which UCM config fo load. */
49 void cras_system_state_set_internal_ucm_suffix(const char *internal_ucm_suffix);
50 
51 /* Sets the system volume.  Will be applied by the active device. */
52 void cras_system_set_volume(size_t volume);
53 /* Gets the current system volume. */
54 size_t cras_system_get_volume();
55 
56 /* Sets the system capture volume.  Will be applied by the active device. */
57 void cras_system_set_capture_gain(long gain);
58 /* Gets the current system capture volume. */
59 long cras_system_get_capture_gain();
60 
61 /* Sets if the system is muted by the user. */
62 void cras_system_set_user_mute(int muted);
63 /* Sets if the system is muted for . */
64 void cras_system_set_mute(int muted);
65 /* Sets if the system muting is locked or not. */
66 void cras_system_set_mute_locked(int locked);
67 /* Gets the current mute state of the system. */
68 int cras_system_get_mute();
69 /* Gets the current user mute state. */
70 int cras_system_get_user_mute();
71 /* Gets the current system mute state. */
72 int cras_system_get_system_mute();
73 /* Gets if the system muting is locked or not. */
74 int cras_system_get_mute_locked();
75 
76 /* Gets the suspend state of audio. */
77 int cras_system_get_suspended();
78 
79 /* Sets the suspend state of audio.
80  * Args:
81  *    suspend - True for suspend, false for resume.
82  */
83 void cras_system_set_suspended(int suspend);
84 
85 /* Sets if the system capture path is muted or not. */
86 void cras_system_set_capture_mute(int muted);
87 /* Sets if the system capture path muting is locked or not. */
88 void cras_system_set_capture_mute_locked(int locked);
89 /* Gets the current mute state of the system capture path. */
90 int cras_system_get_capture_mute();
91 /* Gets if the system capture path muting is locked or not. */
92 int cras_system_get_capture_mute_locked();
93 
94 /* Sets the value in dB of the MAX and MIN volume settings.  This will allow
95  * clients to query what range of control is available.  Both arguments are
96  * specified as dB * 100.
97  * Args:
98  *     min - dB value when volume = 1 (0 mutes).
99  *     max - dB value when volume = CRAS_MAX_SYSTEM_VOLUME
100  */
101 void cras_system_set_volume_limits(long min, long max);
102 /* Returns the dB value when volume = 1, in dB * 100. */
103 long cras_system_get_min_volume();
104 /* Returns the dB value when volume = CRAS_MAX_SYSTEM_VOLUME, in dB * 100. */
105 long cras_system_get_max_volume();
106 
107 /* Sets the limits in dB * 100 of the MAX and MIN capture gain.  This will allow
108  * clients to query what range of control is available.  Both arguments are
109  * specified as dB * 100.
110  * Args:
111  *     min - minimum allowed capture gain.
112  *     max - maximum allowed capture gaax.
113  */
114 void cras_system_set_capture_gain_limits(long min, long max);
115 /* Returns the max value allowed for capture gain in dB * 100. */
116 long cras_system_get_min_capture_gain();
117 /* Returns the min value allowed for capture gain in dB * 100. */
118 long cras_system_get_max_capture_gain();
119 
120 /* Returns the default value of output buffer size in frames. */
121 int cras_system_get_default_output_buffer_size();
122 
123 /* Returns if system aec is supported. */
124 int cras_system_get_aec_supported();
125 
126 /* Adds a card at the given index to the system.  When a new card is found
127  * (through a udev event notification) this will add the card to the system,
128  * causing its devices to become available for playback/capture.
129  * Args:
130  *    alsa_card_info - Info about the alsa card (Index, type, etc.).
131  * Returns:
132  *    0 on success, negative error on failure (Can't create or card already
133  *    exists).
134  */
135 int cras_system_add_alsa_card(struct cras_alsa_card_info *alsa_card_info);
136 
137 /* Removes a card.  When a device is removed this will do the cleanup.  Device
138  * at index must have been added using cras_system_add_alsa_card().
139  * Args:
140  *    alsa_card_index - Index ALSA uses to refer to the card.  The X in "hw:X".
141  * Returns:
142  *    0 on success, negative error on failure (Can't destroy or card doesn't
143  *    exist).
144  */
145 int cras_system_remove_alsa_card(size_t alsa_card_index);
146 
147 /* Checks if an alsa card has been added to the system.
148  * Args:
149  *    alsa_card_index - Index ALSA uses to refer to the card.  The X in "hw:X".
150  * Returns:
151  *    1 if the card has already been added, 0 if not.
152  */
153 int cras_system_alsa_card_exists(unsigned alsa_card_index);
154 
155 /* Poll interface provides a way of getting a callback when 'select'
156  * returns for a given file descriptor.
157  */
158 
159 /* Register the function to use to inform the select loop about new
160  * file descriptors and callbacks.
161  * Args:
162  *    add - The function to call when a new fd is added.
163  *    rm - The function to call when a new fd is removed.
164  *    select_data - Additional value passed back to add and remove.
165  * Returns:
166  *    0 on success, or -EBUSY if there is already a registered handler.
167  */
168 int cras_system_set_select_handler(int (*add)(int fd,
169 					      void (*callback)(void *data),
170 					      void *callback_data,
171 					      void *select_data),
172 				  void (*rm)(int fd, void *select_data),
173 				  void *select_data);
174 
175 /* Adds the fd and callback pair.  When select indicates that fd is readable,
176  * the callback will be called.
177  * Args:
178  *    fd - The file descriptor to pass to select(2).
179  *    callback - The callback to call when fd is ready.
180  *    callback_data - Value passed back to the callback.
181  * Returns:
182  *    0 on success or a negative error code on failure.
183  */
184 int cras_system_add_select_fd(int fd,
185 			      void (*callback)(void *data),
186 			      void *callback_data);
187 
188 /* Removes the fd from the list of fds that are passed to select.
189  * Args:
190  *    fd - The file descriptor to remove from the list.
191  */
192 void cras_system_rm_select_fd(int fd);
193 
194 /*
195  * Register the function to use to add a task for main thread to execute.
196  * Args:
197  *    add_task - The function to call when new task is added.
198  *    task_data - Additional data to pass back to add_task.
199  * Returns:
200  *    0 on success, or -EEXIST if there's already a registered handler.
201  */
202 int cras_system_set_add_task_handler(int (*add_task)(void (*cb)(void *data),
203 						     void *callback_data,
204 						     void *task_data),
205 				     void *task_data);
206 
207 /*
208  * Adds a task callback and data pair, to be executed in the next main thread
209  * loop without additional wait time.
210  * Args:
211  *    callback - The function to execute.
212  *    callback_data - The data to be passed to callback when executed.
213  * Returns:
214  *    0 on success, or -EINVAL if there's no handler for adding task.
215  */
216 int cras_system_add_task(void (*callback)(void *data), void *callback_data);
217 
218 /* Signals that an audio input or output stream has been added to the system.
219  * This allows the count of active streams can be used to notice when the audio
220  * subsystem is idle.
221  * Args:
222  *   direction - Directions of audio streams.
223  */
224 void cras_system_state_stream_added(enum CRAS_STREAM_DIRECTION direction);
225 
226 /* Signals that an audio input or output stream has been removed from the
227  * system.  This allows the count of active streams can be used to notice when
228  * the audio subsystem is idle.
229  * Args:
230  *   direction - Directions of audio stream.
231  */
232 void cras_system_state_stream_removed(enum CRAS_STREAM_DIRECTION direction);
233 
234 /* Returns the number of active playback and capture streams. */
235 unsigned cras_system_state_get_active_streams();
236 
237 /* Returns the number of active streams with given direction.
238  * Args:
239  *   direction - Directions of audio stream.
240  */
241 unsigned cras_system_state_get_active_streams_by_direction(
242 	enum CRAS_STREAM_DIRECTION direction);
243 
244 /* Fills ts with the time the last stream was removed from the system, the time
245  * the stream count went to zero.
246  */
247 void cras_system_state_get_last_stream_active_time(struct cras_timespec *ts);
248 
249 /* Returns output devices information.
250  * Args:
251  *    devs - returns the array of output devices information.
252  * Returns:
253  *    number of output devices.
254  */
255 int cras_system_state_get_output_devs(const struct cras_iodev_info **devs);
256 
257 /* Returns input devices information.
258  * Args:
259  *    devs - returns the array of input devices information.
260  * Returns:
261  *    number of input devices.
262  */
263 int cras_system_state_get_input_devs(const struct cras_iodev_info **devs);
264 
265 /* Returns output nodes information.
266  * Args:
267  *    nodes - returns the array of output nodes information.
268  * Returns:
269  *    number of output nodes.
270  */
271 int cras_system_state_get_output_nodes(const struct cras_ionode_info **nodes);
272 
273 /* Returns input nodes information.
274  * Args:
275  *    nodes - returns the array of input nodes information.
276  * Returns:
277  *    number of input nodes.
278  */
279 int cras_system_state_get_input_nodes(const struct cras_ionode_info **nodes);
280 
281 /*
282  * Sets the non-empty audio status.
283  */
284 void cras_system_state_set_non_empty_status(int non_empty);
285 
286 /*
287  * Returns the non-empty audio status.
288  */
289 int cras_system_state_get_non_empty_status();
290 
291 /* Returns a pointer to the current system state that is shared with clients.
292  * This also 'locks' the structure by incrementing the update count to an odd
293  * value.
294  */
295 struct cras_server_state *cras_system_state_update_begin();
296 
297 /* Unlocks the system state structure that was updated after calling
298  * cras_system_state_update_begin by again incrementing the update count.
299  */
300 void cras_system_state_update_complete();
301 
302 /* Gets a pointer to the system state without locking it.  Only used for debug
303  * log.  Don't add calls to this function. */
304 struct cras_server_state *cras_system_state_get_no_lock();
305 
306 /* Returns the shm fd for the server_state structure. */
307 key_t cras_sys_state_shm_fd();
308 
309 /* Returns the timer manager. */
310 struct cras_tm *cras_system_state_get_tm();
311 
312 /*
313  * Add snapshot to snapshot buffer in system state
314  */
315 void cras_system_state_add_snapshot(struct cras_audio_thread_snapshot *);
316 
317 /*
318  * Dump snapshots from system state to shared memory with client
319  */
320 void cras_system_state_dump_snapshots();
321 
322 #endif /* CRAS_SYSTEM_STATE_H_ */
323