• 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 
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_types.h"
16 
17 struct cras_iodev;
18 struct cras_iodev_info;
19 struct cras_ionode;
20 struct cras_rclient;
21 struct cras_rstream;
22 struct cras_audio_format;
23 struct stream_list;
24 
25 /* Device enabled/disabled callback. */
26 typedef void (*device_enabled_callback_t)(struct cras_iodev *dev, void *cb_data);
27 typedef void (*device_disabled_callback_t)(struct cras_iodev *dev, void *cb_data);
28 
29 /* Initialize the list of iodevs. */
30 void cras_iodev_list_init();
31 
32 /* Clean up any resources used by iodev. */
33 void cras_iodev_list_deinit();
34 
35 /* Adds an output to the output list.
36  * Args:
37  *    output - the output to add.
38  * Returns:
39  *    0 on success, negative error on failure.
40  */
41 int cras_iodev_list_add_output(struct cras_iodev *output);
42 
43 /* Adds an input to the input list.
44  * Args:
45  *    input - the input to add.
46  * Returns:
47  *    0 on success, negative error on failure.
48  */
49 int cras_iodev_list_add_input(struct cras_iodev *input);
50 
51 /* Removes an output from the output list.
52  * Args:
53  *    output - the output to remove.
54  * Returns:
55  *    0 on success, negative error on failure.
56  */
57 int cras_iodev_list_rm_output(struct cras_iodev *output);
58 
59 /* Removes an input from the input list.
60  * Args:
61  *    output - the input to remove.
62  * Returns:
63  *    0 on success, negative error on failure.
64  */
65 int cras_iodev_list_rm_input(struct cras_iodev *input);
66 
67 /* Gets a list of outputs. Callee must free the list when finished.  If list_out
68  * is NULL, this function can be used to return the number of outputs.
69  * Args:
70  *    list_out - This will be set to the malloc'd area containing the list of
71  *        devices.  Ignored if NULL.
72  * Returns:
73  *    The number of devices on the list.
74  */
75 int cras_iodev_list_get_outputs(struct cras_iodev_info **list_out);
76 
77 /* Gets a list of inputs. Callee must free the list when finished.  If list_out
78  * is NULL, this function can be used to return the number of inputs.
79  * Args:
80  *    list_out - This will be set to the malloc'd area containing the list of
81  *        devices.  Ignored if NULL.
82  * Returns:
83  *    The number of devices on the list.
84  */
85 int cras_iodev_list_get_inputs(struct cras_iodev_info **list_out);
86 
87 /* Returns the first enabled device.
88  * Args:
89  *    direction - Playback or capture.
90  * Returns:
91  *    Pointer to the first enabled device of direction.
92  */
93 struct cras_iodev *cras_iodev_list_get_first_enabled_iodev(
94 	enum CRAS_STREAM_DIRECTION direction);
95 
96 /* Returns the active node id.
97  * Args:
98  *    direction - Playback or capture.
99  * Returns:
100  *    The id of the active node.
101  */
102 cras_node_id_t cras_iodev_list_get_active_node_id(
103 	enum CRAS_STREAM_DIRECTION direction);
104 
105 /* Stores the following data to the shared memory server state region:
106  * (1) device list
107  * (2) node list
108  * (3) selected nodes
109  */
110 void cras_iodev_list_update_device_list();
111 
112 /* Stores the node list in the shared memory server state region. */
113 void cras_iodev_list_update_node_list();
114 
115 /* Gets the supported hotword models of an ionode. Caller should free
116  * the returned string after use. */
117 char *cras_iodev_list_get_hotword_models(cras_node_id_t node_id);
118 
119 /* Sets the desired hotword model to an ionode. */
120 int cras_iodev_list_set_hotword_model(cras_node_id_t id,
121 				      const char *model_name);
122 
123 /* Notify that nodes are added/removed. */
124 void cras_iodev_list_notify_nodes_changed();
125 
126 /* Notify that active node is changed for the given direction.
127  * Args:
128  *    direction - Direction of the node.
129  */
130 void cras_iodev_list_notify_active_node_changed(
131 		enum CRAS_STREAM_DIRECTION direction);
132 
133 /* Sets an attribute of an ionode on a device.
134  * Args:
135  *    id - the id of the ionode.
136  *    node_index - Index of the ionode on the device.
137  *    attr - the attribute we want to change.
138  *    value - the value we want to set.
139  */
140 int cras_iodev_list_set_node_attr(cras_node_id_t id,
141 				  enum ionode_attr attr, int value);
142 
143 /* Select a node as the preferred node.
144  * Args:
145  *    direction - Playback or capture.
146  *    node_id - the id of the ionode to be selected. As a special case, if
147  *        node_id is 0, don't select any node in this direction.
148  */
149 void cras_iodev_list_select_node(enum CRAS_STREAM_DIRECTION direction,
150 				 cras_node_id_t node_id);
151 
152 /* Checks if an iodev is enabled. */
153 int cras_iodev_list_dev_is_enabled(const struct cras_iodev *dev);
154 
155 /* Enables an iodev. If the fallback device was already enabled, this
156  * call will disable it. */
157 void cras_iodev_list_enable_dev(struct cras_iodev *dev);
158 
159 /*
160  * Disables an iodev. If this is the last device to disable, the
161  * fallback devices will be enabled accordingly.
162  * Set `foce_close` to true if the device must be closed regardless of having
163  * pinned streams attached.
164  */
165 void cras_iodev_list_disable_dev(struct cras_iodev *dev, bool force_close);
166 
167 /* Adds a node to the active devices list.
168  * Args:
169  *    direction - Playback or capture.
170  *    node_id - The id of the ionode to be added.
171  */
172 void cras_iodev_list_add_active_node(enum CRAS_STREAM_DIRECTION direction,
173 				     cras_node_id_t node_id);
174 
175 /* Removes a node from the active devices list.
176  * Args:
177  *    direction - Playback or capture.
178  *    node_id - The id of the ionode to be removed.
179  */
180 void cras_iodev_list_rm_active_node(enum CRAS_STREAM_DIRECTION direction,
181 				    cras_node_id_t node_id);
182 
183 /* Returns 1 if the node is selected, 0 otherwise. */
184 int cras_iodev_list_node_selected(struct cras_ionode *node);
185 
186 /* Notify the current volume of the given node. */
187 void cras_iodev_list_notify_node_volume(struct cras_ionode *node);
188 
189 /* Notify the current capture gain of the given node. */
190 void cras_iodev_list_notify_node_capture_gain(struct cras_ionode *node);
191 
192 /* Notify the current left right channel swapping state of the given node. */
193 void cras_iodev_list_notify_node_left_right_swapped(struct cras_ionode *node);
194 
195 /* Handles the adding and removing of test iodevs. */
196 void cras_iodev_list_add_test_dev(enum TEST_IODEV_TYPE type);
197 
198 /* Handles sending a command to a test iodev. */
199 void cras_iodev_list_test_dev_command(unsigned int iodev_idx,
200 				      enum CRAS_TEST_IODEV_CMD command,
201 				      unsigned int data_len,
202 				      const uint8_t *data);
203 
204 /* Gets the audio thread used by the devices. */
205 struct audio_thread *cras_iodev_list_get_audio_thread();
206 
207 /* Gets the list of all active audio streams attached to devices. */
208 struct stream_list *cras_iodev_list_get_stream_list();
209 
210 /* Sets the function to call when a device is enabled or disabled. */
211 int cras_iodev_list_set_device_enabled_callback(
212 		device_enabled_callback_t enabled_cb,
213 		device_disabled_callback_t disabled_cb,
214 		void *cb_data);
215 
216 /* Suspends all hotwording streams. */
217 int cras_iodev_list_suspend_hotword_streams();
218 
219 /* Resumes all hotwording streams. */
220 int cras_iodev_list_resume_hotword_stream();
221 
222 /* For unit test only. */
223 void cras_iodev_list_reset();
224 
225 #endif /* CRAS_IODEV_LIST_H_ */
226