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 6 /* 7 * IO list manages the list of inputs and outputs available. 8 */ 9 #ifndef CRAS_IODEV_LIST_H_ 10 #define CRAS_IODEV_LIST_H_ 11 12 #include <stdbool.h> 13 #include <stdint.h> 14 15 #include "cras_iodev.h" 16 #include "cras_types.h" 17 18 struct cras_rclient; 19 struct stream_list; 20 21 /* Device enabled/disabled callback. */ 22 typedef void (*device_enabled_callback_t)(struct cras_iodev *dev, 23 void *cb_data); 24 typedef void (*device_disabled_callback_t)(struct cras_iodev *dev, 25 void *cb_data); 26 27 /* Initialize the list of iodevs. */ 28 void cras_iodev_list_init(); 29 30 /* Clean up any resources used by iodev. */ 31 void cras_iodev_list_deinit(); 32 33 /* Adds an output to the output list. 34 * Args: 35 * output - the output to add. 36 * Returns: 37 * 0 on success, negative error on failure. 38 */ 39 int cras_iodev_list_add_output(struct cras_iodev *output); 40 41 /* Adds an input to the input list. 42 * Args: 43 * input - the input to add. 44 * Returns: 45 * 0 on success, negative error on failure. 46 */ 47 int cras_iodev_list_add_input(struct cras_iodev *input); 48 49 /* Removes an output from the output list. 50 * Args: 51 * output - the output to remove. 52 * Returns: 53 * 0 on success, negative error on failure. 54 */ 55 int cras_iodev_list_rm_output(struct cras_iodev *output); 56 57 /* Removes an input from the input list. 58 * Args: 59 * output - the input to remove. 60 * Returns: 61 * 0 on success, negative error on failure. 62 */ 63 int cras_iodev_list_rm_input(struct cras_iodev *input); 64 65 /* Gets a list of outputs. Callee must free the list when finished. If list_out 66 * is NULL, this function can be used to return the number of outputs. 67 * Args: 68 * list_out - This will be set to the malloc'd area containing the list of 69 * devices. Ignored if NULL. 70 * Returns: 71 * The number of devices on the list. 72 */ 73 int cras_iodev_list_get_outputs(struct cras_iodev_info **list_out); 74 75 /* Gets a list of inputs. Callee must free the list when finished. If list_out 76 * is NULL, this function can be used to return the number of inputs. 77 * Args: 78 * list_out - This will be set to the malloc'd area containing the list of 79 * devices. Ignored if NULL. 80 * Returns: 81 * The number of devices on the list. 82 */ 83 int cras_iodev_list_get_inputs(struct cras_iodev_info **list_out); 84 85 /* Returns the first enabled device. 86 * Args: 87 * direction - Playback or capture. 88 * Returns: 89 * Pointer to the first enabled device of direction. 90 */ 91 struct cras_iodev * 92 cras_iodev_list_get_first_enabled_iodev(enum CRAS_STREAM_DIRECTION direction); 93 94 /* Returns SCO PCM device. 95 * Args: 96 * direction - Playback or capture. 97 * Returns: 98 * Pointer to the SCO PCM device of direction. 99 */ 100 struct cras_iodev * 101 cras_iodev_list_get_sco_pcm_iodev(enum CRAS_STREAM_DIRECTION direction); 102 103 /* Returns the active node id. 104 * Args: 105 * direction - Playback or capture. 106 * Returns: 107 * The id of the active node. 108 */ 109 cras_node_id_t 110 cras_iodev_list_get_active_node_id(enum CRAS_STREAM_DIRECTION direction); 111 112 /* Stores the following data to the shared memory server state region: 113 * (1) device list 114 * (2) node list 115 * (3) selected nodes 116 */ 117 void cras_iodev_list_update_device_list(); 118 119 /* Stores the node list in the shared memory server state region. */ 120 void cras_iodev_list_update_node_list(); 121 122 /* Gets the supported hotword models of an ionode. Caller should free 123 * the returned string after use. */ 124 char *cras_iodev_list_get_hotword_models(cras_node_id_t node_id); 125 126 /* Sets the desired hotword model to an ionode. */ 127 int cras_iodev_list_set_hotword_model(cras_node_id_t id, 128 const char *model_name); 129 130 /* Notify that nodes are added/removed. */ 131 void cras_iodev_list_notify_nodes_changed(); 132 133 /* Notify that active node is changed for the given direction. 134 * Args: 135 * direction - Direction of the node. 136 */ 137 void cras_iodev_list_notify_active_node_changed( 138 enum CRAS_STREAM_DIRECTION direction); 139 140 /* Sets an attribute of an ionode on a device. 141 * Args: 142 * id - the id of the ionode. 143 * node_index - Index of the ionode on the device. 144 * attr - the attribute we want to change. 145 * value - the value we want to set. 146 */ 147 int cras_iodev_list_set_node_attr(cras_node_id_t id, enum ionode_attr attr, 148 int value); 149 150 /* Select a node as the preferred node. 151 * Args: 152 * direction - Playback or capture. 153 * node_id - the id of the ionode to be selected. As a special case, if 154 * node_id is 0, don't select any node in this direction. 155 */ 156 void cras_iodev_list_select_node(enum CRAS_STREAM_DIRECTION direction, 157 cras_node_id_t node_id); 158 159 /* 160 * Checks if an iodev is enabled. By enabled we mean all default streams will 161 * be routed to this iodev. 162 */ 163 int cras_iodev_list_dev_is_enabled(const struct cras_iodev *dev); 164 165 /* Enables an iodev. If the fallback device was already enabled, this 166 * call will disable it. */ 167 void cras_iodev_list_enable_dev(struct cras_iodev *dev); 168 169 /* 170 * Suspends the connection of all types of stream attached to given iodev. 171 * This call doesn't disable the given iodev. 172 */ 173 void cras_iodev_list_suspend_dev(unsigned int dev_idx); 174 175 /* 176 * Resumes the connection of all types of stream attached to given iodev. 177 * This call doesn't enable the given dev. 178 */ 179 void cras_iodev_list_resume_dev(unsigned int dev_idx); 180 181 /* 182 * Sets mute state to device of given index. 183 * Args: 184 * dev_idx - Index of the device to set mute state. 185 */ 186 void cras_iodev_list_set_dev_mute(unsigned int dev_idx); 187 188 /* 189 * Disables an iodev. If this is the last device to disable, the 190 * fallback devices will be enabled accordingly. 191 * Set `foce_close` to true if the device must be closed regardless of having 192 * pinned streams attached. 193 */ 194 void cras_iodev_list_disable_dev(struct cras_iodev *dev, bool force_close); 195 196 /* Adds a node to the active devices list. 197 * Args: 198 * direction - Playback or capture. 199 * node_id - The id of the ionode to be added. 200 */ 201 void cras_iodev_list_add_active_node(enum CRAS_STREAM_DIRECTION direction, 202 cras_node_id_t node_id); 203 204 /* Removes a node from the active devices list. 205 * Args: 206 * direction - Playback or capture. 207 * node_id - The id of the ionode to be removed. 208 */ 209 void cras_iodev_list_rm_active_node(enum CRAS_STREAM_DIRECTION direction, 210 cras_node_id_t node_id); 211 212 /* Returns 1 if the node is selected, 0 otherwise. */ 213 int cras_iodev_list_node_selected(struct cras_ionode *node); 214 215 /* Notify the current volume of the given node. */ 216 void cras_iodev_list_notify_node_volume(struct cras_ionode *node); 217 218 /* Notify the current capture gain of the given node. */ 219 void cras_iodev_list_notify_node_capture_gain(struct cras_ionode *node); 220 221 /* Notify the current left right channel swapping state of the given node. */ 222 void cras_iodev_list_notify_node_left_right_swapped(struct cras_ionode *node); 223 224 /* Handles the adding and removing of test iodevs. */ 225 void cras_iodev_list_add_test_dev(enum TEST_IODEV_TYPE type); 226 227 /* Handles sending a command to a test iodev. */ 228 void cras_iodev_list_test_dev_command(unsigned int iodev_idx, 229 enum CRAS_TEST_IODEV_CMD command, 230 unsigned int data_len, 231 const uint8_t *data); 232 233 /* Gets the audio thread used by the devices. */ 234 struct audio_thread *cras_iodev_list_get_audio_thread(); 235 236 /* Gets the list of all active audio streams attached to devices. */ 237 struct stream_list *cras_iodev_list_get_stream_list(); 238 239 /* Sets the function to call when a device is enabled or disabled. */ 240 int cras_iodev_list_set_device_enabled_callback( 241 device_enabled_callback_t enabled_cb, 242 device_disabled_callback_t disabled_cb, void *cb_data); 243 244 /* Registers loopback to an output device. 245 * Args: 246 * loopback_type - Pre or post software DSP. 247 * output_dev_idx - Index of the target output device. 248 * hook_data - Callback function to process loopback data. 249 * hook_start - Callback for starting or stopping loopback. 250 * loopback_dev_idx - Index of the loopback device that 251 * listens for output data. 252 */ 253 void cras_iodev_list_register_loopback(enum CRAS_LOOPBACK_TYPE loopback_type, 254 unsigned int output_dev_idx, 255 loopback_hook_data_t hook_data, 256 loopback_hook_control_t hook_start, 257 unsigned int loopback_dev_idx); 258 259 /* Unregisters loopback from an output device by matching 260 * loopback type and loopback device index. 261 * Args: 262 * loopback_type - Pre or post software DSP. 263 * output_dev_idx - Index of the target output device. 264 * loopback_dev_idx - Index of the loopback device that 265 * listens for output data. 266 */ 267 void cras_iodev_list_unregister_loopback(enum CRAS_LOOPBACK_TYPE loopback_type, 268 unsigned int output_dev_idx, 269 unsigned int loopback_dev_idx); 270 271 /* Suspends all hotwording streams. */ 272 int cras_iodev_list_suspend_hotword_streams(); 273 274 /* Resumes all hotwording streams. */ 275 int cras_iodev_list_resume_hotword_stream(); 276 277 /* For unit test only. */ 278 void cras_iodev_list_reset(); 279 280 #endif /* CRAS_IODEV_LIST_H_ */ 281