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