1 // Copyright 2016 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 
16 // Class to manage audio devices in Brillo.
17 
18 #ifndef BRILLO_AUDIO_AUDIOSERVICE_BRILLO_AUDIO_MANAGER_H_
19 #define BRILLO_AUDIO_AUDIOSERVICE_BRILLO_AUDIO_MANAGER_H_
20 
21 #include <sys/cdefs.h>
22 
23 #include "brillo_audio_device_info.h"
24 
25 __BEGIN_DECLS
26 
27 struct BAudioManager;
28 
29 typedef struct BAudioManager BAudioManager;
30 
31 // Get a pointer to a BAudioManager. This object will refer to the same
32 // underlying client object no matter how many times it is called.
33 //
34 // Returns a pointer to a BAudioManager. Returns NULL on failure.
35 BAudioManager* BAudioManager_new();
36 
37 // Flag to get input devices.
38 static const int GET_DEVICES_INPUTS = 1;
39 // Flag to get output devices.
40 static const int GET_DEVICES_OUTPUTS = 2;
41 
42 // Returns the list of input/output devices connected to the system.
43 //
44 // Arg:
45 //   brillo_audio_manager: A pointer to a BAudioManager.
46 //   flag: Either GET_DEVICES_INPUTS or GET_DEVICES_OUTPUTS.
47 //   device_array: An array of BAudioDeviceInfo pointers. The caller has to
48 //                 allocate this array.
49 //   size: The size of device_array.
50 //   num_devices: A pointer to an unsigned int which will represent the number
51 //                of audio devices connected to the device.
52 //
53 // Returns 0 on success and errno on failure.
54 int BAudioManager_getDevices(
55     const BAudioManager* brillo_audio_manager, int flag,
56     BAudioDeviceInfo* device_array[], unsigned int size,
57     unsigned int* num_devices);
58 
59 // Select the input device to be used for recording.
60 //
61 // Arg:
62 //   brillo_audio_manager: A pointer to a BAudioManager.
63 //   device: Device to set as the input device. Note that the device has to be
64 //           an input device.
65 //
66 // Returns 0 on success and errno on failure.
67 int BAudioManager_setInputDevice(const BAudioManager* brillo_audio_manager,
68                                  const BAudioDeviceInfo* device);
69 
70 // Usage types.
71 enum BAudioUsage {
72   kUsageAlarm,
73   kUsageMedia,
74   kUsageNotifications,
75   kUsageSystem,
76   kUsageInvalid
77 };
78 
79 // Select the output device to be used for playback.
80 //
81 // Arg:
82 //   brillo_audio_manager: A pointer to a BAudioManager.
83 //   device: Device to set as the output device. Note that the device has to
84 //           be an output device.
85 //   usage: A BAudioUsage type representing a usage to route to |device|.
86 //
87 // Returns 0 on success and errno on failure.
88 int BAudioManager_setOutputDevice(
89     const BAudioManager* brillo_audio_manager, const BAudioDeviceInfo* device,
90     BAudioUsage usage);
91 
92 // Get the number of steps for a given stream type.
93 //
94 // Args:
95 //   brillo_audio_manager: A pointer to a BAudioManager object.
96 //   usage: A BAudioUsage representing the audio stream.
97 //   max_steps: A pointer to an int representing the number of steps for a given
98 //              usage.
99 //
100 // Returns 0 on success and errno on failure.
101 int BAudioManager_getMaxVolumeSteps(const BAudioManager* brillo_audio_manager,
102                                     BAudioUsage usage,
103                                     int* max_steps);
104 
105 // Set the number of steps for a given stream type.
106 //
107 // Args:
108 //   brillo_audio_manager: A pointer to a BAudioManager object.
109 //   usage: A BAudioUsage representing the audio stream.
110 //   max_steps: An int representing the number of steps to use for a given
111 //              usage.
112 //
113 // Returns 0 on success and errno on failure.
114 int BAudioManager_setMaxVolumeSteps(const BAudioManager* brillo_audio_manager,
115                                     BAudioUsage usage,
116                                     int max_steps);
117 
118 // Set the volume for a given stream type.
119 //
120 // Args:
121 //   brillo_audio_manager: A pointer to a BAudioManager object.
122 //   usage: A BAudioUsage representing the audio stream.
123 //   device: A pointer to a BAudioDeviceInfo object.
124 //   value: An int representing the index to set the volume to. The index must
125 //           be less than max_steps if BAudioManager_setMaxVolumeSteps was
126 //           called or 100 otherwise.
127 //
128 // Returns 0 on success and errno on failure.
129 int BAudioManager_setVolumeIndex(const BAudioManager* brillo_audio_manager,
130                                  BAudioUsage usage,
131                                  const BAudioDeviceInfo* device,
132                                  int index);
133 
134 // Get the volume for a given stream type.
135 //
136 // Args:
137 //   brillo_audio_manager: A pointer to a BAudioManager object.
138 //   usage: A BAudioUsage representing the audio stream.
139 //   device: A pointer to a BAudioDeviceInfo object.
140 //   value: A pointer to int. This will be set to an int representing the volume
141 //          index for |usage|.
142 //
143 // Returns 0 on success and errno on failure.
144 int BAudioManager_getVolumeIndex(const BAudioManager* brillo_audio_manager,
145                                  BAudioUsage usage,
146                                  const BAudioDeviceInfo* device,
147                                  int* index);
148 
149 // Get the default stream for volume buttons. If
150 // BAudioManager_setVolumeControlUsage has not been called, this will return
151 // kInvalidUsage.
152 //
153 // Args:
154 //  brillo_audio_manager: A pointer to a BAudioManager object.
155 //  usage: A pointer to a BAudioUsage representing the audio stream.
156 //
157 // Returns 0 on success and errno on failure.
158 int BAudioManager_getVolumeControlUsage(
159     const BAudioManager* brillo_audio_manager, BAudioUsage* usage);
160 
161 // Set the default stream to use for volume buttons. By default, streams will be
162 // ordered by priority:
163 //   1. kUsageAlarm
164 //   2. kUsageNotifications
165 //   3. kUsageSystem
166 //   4. kUsageMedia
167 //
168 // Calling BAudioMananager_setVolumeControlUsage with kInvalidUsage will reset
169 // the volume control stream to its default priorities and undo the effects of
170 // previous calls to BAudioManager_setVolumeControlUsage.
171 //
172 // Args:
173 //  brillo_audio_manager: A pointer to a BAudioManager object.
174 //  usage: A BAudioUsage representing the audio stream.
175 //
176 // Returns 0 on success and errno on failure.
177 int BAudioManager_setVolumeControlUsage(
178     const BAudioManager* brillo_audio_manager, BAudioUsage usage);
179 
180 // Increment the volume of active streams or stream selected using
181 // BAudioManager_setVolumeControlUsage.
182 //
183 // Args:
184 //   brillo_audio_manager: A pointer to a BAudioManager object.
185 //
186 // Returns 0 on success and errno on failure.
187 int BAudioManager_incrementVolume(const BAudioManager* brillo_audio_manager);
188 
189 // Decrement the volume of active streams or stream selected using
190 // BAudioManager_setVolumeControlUsage.
191 //
192 // Args:
193 //   brillo_audio_manager: A pointer to a BAudioManager object.
194 //
195 // Returns 0 on success and errno on failure.
196 int BAudioManager_decrementVolume(const BAudioManager* brillo_audio_manager);
197 
198 // Object used for callbacks.
199 struct BAudioCallback {
200   // Function to be called when an audio device is added. If multiple audio
201   // devices are added, then this function will be called multiple times. The
202   // user is not responsible for freeing added_device.
203   void (*OnAudioDeviceAdded)(const BAudioDeviceInfo* added_device,
204                              void* user_data);
205 
206   // Function to be called when an audio device is removed. If multiple audio
207   // devices are removed, then this function will be called multiple times. The
208   // user is not responsible for freeing removed_device.
209   void (*OnAudioDeviceRemoved)(const BAudioDeviceInfo* removed_device,
210                                void* user_data);
211 
212   // Function to be called when the volume button is pressed.
213   void (*OnVolumeChanged)(BAudioUsage usage,
214                           int old_volume_index,
215                           int new_volume_index,
216                           void* user_data);
217 };
218 
219 typedef struct BAudioCallback BAudioCallback;
220 
221 // Registers a callback object that lets clients know when audio devices have
222 // been added/removed from the system.
223 //
224 // Arg:
225 //   brillo_audio_manager: A pointer to a BAudioManager.
226 //   callback: An object of type BAudioCallback. The BAudioManager
227 //             maintains ownership of this object.
228 //   user_data : A pointer to user data. This is not used by BAudioManager and
229 //               is passed as an arg to callbacks.
230 //   callback_id: A pointer to an int. The int represents a token that can be
231 //                used to de-register this callback. Contains 0 on failure.
232 //
233 // Returns 0 on success and errno on failure.
234 int BAudioManager_registerAudioCallback(
235     const BAudioManager* brillo_audio_manager, const BAudioCallback* callback,
236     void* user_data, int* callback_id);
237 
238 // Unregisters a callback object.
239 //
240 // Arg:
241 //   brillo_audio_manager: A pointer to a BAudioManager.
242 //   callback_id: A token correspoding to the callback object.
243 //
244 // Returns 0 on success and errno on failure.
245 int BAudioManager_unregisterAudioCallback(
246     const BAudioManager* brillo_audio_manager, int callback_id);
247 
248 // Free a Brillo audio manager object.
249 //
250 // Arg:
251 //   brillo_audio_manager: A pointer to a BAudioManager to be freed.
252 //
253 // Returns 0 on success and errno on failure.
254 int BAudioManager_delete(BAudioManager* brillo_audio_manager);
255 
256 __END_DECLS
257 
258 #endif  // BRILLO_AUDIO_AUDIOSERVICE_BRILLO_AUDIO_MANAGER_H_
259