blob: 6a1d269242e1399377339233f627b27a9c936f26 [file] [log] [blame]
François Gaffie2110e042015-03-24 08:41:51 +01001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
jiabin2336de82021-04-01 23:52:18 +000019#include <utility>
20
François Gaffie2110e042015-03-24 08:41:51 +010021#include <AudioPolicyManagerObserver.h>
François Gaffied0ba9ed2018-11-05 11:50:42 +010022#include <media/AudioProductStrategy.h>
François Gaffie4b2018b2018-11-07 11:18:59 +010023#include <media/AudioVolumeGroup.h>
Eric Laurentf5aa58d2019-02-22 18:20:11 -080024#include <IVolumeCurves.h>
François Gaffiedc7553f2018-11-02 10:39:57 +010025#include <policy.h>
François Gaffie2110e042015-03-24 08:41:51 +010026#include <Volume.h>
27#include <HwModule.h>
28#include <DeviceDescriptor.h>
29#include <system/audio.h>
30#include <system/audio_policy.h>
31#include <utils/Errors.h>
32#include <utils/Vector.h>
33
34namespace android {
35
François Gaffiedc7553f2018-11-02 10:39:57 +010036using DeviceStrategyMap = std::map<product_strategy_t, DeviceVector>;
37using StrategyVector = std::vector<product_strategy_t>;
François Gaffie251c7f02018-11-07 10:41:08 +010038using VolumeGroupVector = std::vector<volume_group_t>;
Jiabin Huang3b98d322020-09-03 17:54:16 +000039using CapturePresetDevicesRoleMap =
jiabin2336de82021-04-01 23:52:18 +000040 std::map<std::pair<audio_source_t, device_role_t>, AudioDeviceTypeAddrVector>;
François Gaffiedc7553f2018-11-02 10:39:57 +010041
François Gaffie2110e042015-03-24 08:41:51 +010042/**
43 * This interface is dedicated to the policy manager that a Policy Engine shall implement.
44 */
Mikhail Naganov47835552019-05-14 10:32:51 -070045class EngineInterface
François Gaffie2110e042015-03-24 08:41:51 +010046{
47public:
48 /**
49 * Checks if the engine was correctly initialized.
50 *
51 * @return NO_ERROR if initialization has been done correctly, error code otherwise..
52 */
53 virtual status_t initCheck() = 0;
54
55 /**
56 * Sets the Manager observer that allows the engine to retrieve information on collection
57 * of devices, streams, HwModules, ...
58 *
59 * @param[in] observer handle on the manager.
60 */
61 virtual void setObserver(AudioPolicyManagerObserver *observer) = 0;
62
63 /**
François Gaffie2110e042015-03-24 08:41:51 +010064 * Set the Telephony Mode.
65 *
66 * @param[in] mode: Android Phone state (normal, ringtone, csv, in communication)
67 *
68 * @return NO_ERROR if Telephony Mode set correctly, error code otherwise.
69 */
70 virtual status_t setPhoneState(audio_mode_t mode) = 0;
71
72 /**
73 * Get the telephony Mode
74 *
75 * @return the current telephony mode
76 */
77 virtual audio_mode_t getPhoneState() const = 0;
78
79 /**
80 * Set Force Use config for a given usage.
81 *
82 * @param[in] usage for which a configuration shall be forced.
83 * @param[in] config wished to be forced for the given usage.
84 *
François Gaffie20f06f92015-03-24 09:01:14 +010085 * @return NO_ERROR if the Force Use config was set correctly, error code otherwise (e.g. config
86 * not allowed a given usage...)
François Gaffie2110e042015-03-24 08:41:51 +010087 */
88 virtual status_t setForceUse(audio_policy_force_use_t usage,
89 audio_policy_forced_cfg_t config) = 0;
90
91 /**
92 * Get Force Use config for a given usage.
93 *
94 * @param[in] usage for which a configuration shall be forced.
95 *
96 * @return config wished to be forced for the given usage.
97 */
98 virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage) const = 0;
99
100 /**
101 * Set the connection state of device(s).
102 *
103 * @param[in] devDesc for which the state has changed.
104 * @param[in] state of availability of this(these) device(s).
105 *
106 * @return NO_ERROR if devices criterion updated correctly, error code otherwise.
107 */
108 virtual status_t setDeviceConnectionState(const android::sp<android::DeviceDescriptor> devDesc,
109 audio_policy_dev_state_t state) = 0;
110
François Gaffiedc7553f2018-11-02 10:39:57 +0100111 /**
112 * Get the strategy selected for a given audio attributes.
113 *
114 * @param[in] audio attributes to get the selected @product_strategy_t followed by.
Francois Gaffie11b65922020-09-24 16:59:08 +0200115 * @param fallbackOnDefault if true, will return the fallback strategy if the attributes
116 * are not explicitly assigned to a given strategy.
François Gaffiedc7553f2018-11-02 10:39:57 +0100117 * @return @product_strategy_t to be followed.
118 */
119 virtual product_strategy_t getProductStrategyForAttributes(
Francois Gaffie11b65922020-09-24 16:59:08 +0200120 const audio_attributes_t &attr, bool fallbackOnDefault = true) const = 0;
François Gaffiedc7553f2018-11-02 10:39:57 +0100121
122 /**
123 * @brief getOutputDevicesForAttributes retrieves the devices to be used for given
124 * audio attributes.
125 * @param attributes of the output requesting Device(s) selection
126 * @param preferedDevice valid reference if a prefered device is requested, nullptr otherwise.
127 * @param fromCache if true, the device is returned from internal cache,
128 * otherwise it is determined by current state (device connected,phone state,
129 * force use, a2dp output...)
130 * @return vector of selected device descriptors.
131 * Appropriate device for streams handled by the specified audio attributes according
132 * to current phone state, forced states, connected devices...
133 * if fromCache is true, the device is returned from internal cache,
134 * otherwise it is determined by current state (device connected,phone state, force use,
135 * a2dp output...)
136 * This allows to:
137 * 1 speed up process when the state is stable (when starting or stopping an output)
138 * 2 access to either current device selection (fromCache == true) or
139 * "future" device selection (fromCache == false) when called from a context
140 * where conditions are changing (setDeviceConnectionState(), setPhoneState()...) AND
141 * before manager updates its outputs.
142 */
143 virtual DeviceVector getOutputDevicesForAttributes(
144 const audio_attributes_t &attributes,
145 const sp<DeviceDescriptor> &preferedDevice = nullptr,
146 bool fromCache = false) const = 0;
147
148 /**
149 * @brief getOutputDevicesForStream Legacy function retrieving devices from a stream type.
150 * @param stream type of the output requesting Device(s) selection
151 * @param fromCache if true, the device is returned from internal cache,
152 * otherwise it is determined by current state (device connected,phone state,
153 * force use, a2dp output...)
154 * @return appropriate device for streams handled by the specified audio attributes according
155 * to current phone state, forced states, connected devices...
156 * if fromCache is true, the device is returned from internal cache,
157 * otherwise it is determined by current state (device connected,phone state, force use,
158 * a2dp output...)
159 * This allows to:
160 * 1 speed up process when the state is stable (when starting or stopping an output)
161 * 2 access to either current device selection (fromCache == true) or
162 * "future" device selection (fromCache == false) when called from a context
163 * where conditions are changing (setDeviceConnectionState(), setPhoneState()...) AND
164 * before manager updates its outputs.
165 */
166 virtual DeviceVector getOutputDevicesForStream(audio_stream_type_t stream,
167 bool fromCache = false) const = 0;
168
169 /**
170 * Get the input device selected for given audio attributes.
171 *
172 * @param[in] attr audio attributes to consider
173 * @param[out] mix to be used if a mix has been installed for the given audio attributes.
174 * @return selected input device for the audio attributes, may be null if error.
175 */
176 virtual sp<DeviceDescriptor> getInputDeviceForAttributes(
Mikhail Naganovbfac5832019-03-05 16:55:28 -0800177 const audio_attributes_t &attr, sp<AudioPolicyMix> *mix = nullptr) const = 0;
François Gaffiedc7553f2018-11-02 10:39:57 +0100178
179 /**
180 * Get the legacy stream type for a given audio attributes.
181 *
182 * @param[in] audio attributes to get the associated audio_stream_type_t.
183 *
184 * @return audio_stream_type_t associated to the attributes.
185 */
186 virtual audio_stream_type_t getStreamTypeForAttributes(
187 const audio_attributes_t &attr) const = 0;
188
189 /**
190 * @brief getAttributesForStream get the audio attributes from legacy stream type
François Gaffie251c7f02018-11-07 10:41:08 +0100191 * Attributes returned might only be used to check upon routing decision, not volume decisions.
François Gaffiedc7553f2018-11-02 10:39:57 +0100192 * @param stream to consider
193 * @return audio attributes matching the legacy stream type
194 */
195 virtual audio_attributes_t getAttributesForStreamType(audio_stream_type_t stream) const = 0;
196
197 /**
198 * @brief getStreamTypesForProductStrategy retrieves the list of legacy stream type following
199 * the given product strategy
200 * @param ps product strategy to consider
201 * @return associated legacy Stream Types vector of the given product strategy
202 */
203 virtual StreamTypeVector getStreamTypesForProductStrategy(product_strategy_t ps) const = 0;
204
205 /**
206 * @brief getAllAttributesForProductStrategy retrieves all the attributes following the given
207 * product strategy. Any attributes that "matches" with this one will follow the product
208 * strategy.
209 * "matching" means the usage shall match if reference attributes has a defined usage, AND
210 * content type shall match if reference attributes has a defined content type AND
211 * flags shall match if reference attributes has defined flags AND
212 * tags shall match if reference attributes has defined tags.
213 * @param ps product strategy to consider
214 * @return vector of product strategy ids, empty if unknown strategy.
215 */
216 virtual AttributesVector getAllAttributesForProductStrategy(product_strategy_t ps) const = 0;
217
218 /**
219 * @brief getOrderedAudioProductStrategies
220 * @return priority ordered product strategies to help the AudioPolicyManager evaluating the
221 * device selection per output according to the prioritized strategies.
222 */
223 virtual StrategyVector getOrderedProductStrategies() const = 0;
224
225 /**
226 * @brief updateDeviceSelectionCache. Device selection for AudioAttribute / Streams is cached
227 * in the engine in order to speed up process when the audio system is stable.
228 * When a device is connected, the android mode is changed, engine is notified and can update
229 * the cache.
230 * When starting / stopping an output with a stream that can affect notification, the engine
231 * needs to update the cache upon this function call.
232 */
233 virtual void updateDeviceSelectionCache() = 0;
234
François Gaffied0ba9ed2018-11-05 11:50:42 +0100235 /**
236 * @brief listAudioProductStrategies. Introspection API to retrieve a collection of
237 * AudioProductStrategyVector that allows to build AudioAttributes according to a
238 * product_strategy which is just an index. It has also a human readable name to help the
239 * Car/Oem/AudioManager identiying the use case.
240 * @param strategies collection.
241 * @return OK if the list has been retrieved, error code otherwise
242 */
243 virtual status_t listAudioProductStrategies(AudioProductStrategyVector &strategies) const = 0;
244
Eric Laurentf5aa58d2019-02-22 18:20:11 -0800245 /**
246 * @brief getVolumeCurvesForAttributes retrieves the Volume Curves interface for the
247 * requested Audio Attributes.
248 * @param attr to be considered
249 * @return IVolumeCurves interface pointer if found, nullptr otherwise
250 */
François Gaffie251c7f02018-11-07 10:41:08 +0100251 virtual IVolumeCurves *getVolumeCurvesForAttributes(const audio_attributes_t &attr) const = 0;
Eric Laurentf5aa58d2019-02-22 18:20:11 -0800252
253 /**
254 * @brief getVolumeCurvesForStreamType retrieves the Volume Curves interface for the stream
255 * @param stream to be considered
256 * @return IVolumeCurves interface pointer if found, nullptr otherwise
257 */
François Gaffie251c7f02018-11-07 10:41:08 +0100258 virtual IVolumeCurves *getVolumeCurvesForStreamType(audio_stream_type_t stream) const = 0;
259
260 /**
261 * @brief getVolumeCurvesForVolumeGroup retrieves the Volume Curves interface for volume group
262 * @param group to be considered
263 * @return IVolumeCurves interface pointer if found, nullptr otherwise
264 */
265 virtual IVolumeCurves *getVolumeCurvesForVolumeGroup(volume_group_t group) const = 0;
266
267 /**
268 * @brief getVolumeGroups retrieves the collection of volume groups.
269 * @return vector of volume groups
270 */
271 virtual VolumeGroupVector getVolumeGroups() const = 0;
272
273 /**
274 * @brief getVolumeGroupForAttributes gets the appropriate volume group to be used for a given
275 * Audio Attributes.
276 * @param attr to be considered
Francois Gaffie11b65922020-09-24 16:59:08 +0200277 * @param fallbackOnDefault if true, will return the fallback volume group if the attributes
278 * are not associated to any volume group.
François Gaffie251c7f02018-11-07 10:41:08 +0100279 * @return volume group associated to the given audio attributes, default group if none
280 * applicable, VOLUME_GROUP_NONE if no default group defined.
281 */
Francois Gaffie11b65922020-09-24 16:59:08 +0200282 virtual volume_group_t getVolumeGroupForAttributes(
283 const audio_attributes_t &attr, bool fallbackOnDefault = true) const = 0;
François Gaffie251c7f02018-11-07 10:41:08 +0100284
285 /**
286 * @brief getVolumeGroupForStreamType gets the appropriate volume group to be used for a given
287 * legacy stream type
288 * @param stream type to be considered
Francois Gaffie11b65922020-09-24 16:59:08 +0200289 * @param fallbackOnDefault if true, will return the fallback volume group if the stream type
290 * is not associated to any volume group.
François Gaffie251c7f02018-11-07 10:41:08 +0100291 * @return volume group associated to the given stream type, default group if none applicable,
292 * VOLUME_GROUP_NONE if no default group defined.
293 */
Francois Gaffie11b65922020-09-24 16:59:08 +0200294 virtual volume_group_t getVolumeGroupForStreamType(
295 audio_stream_type_t stream, bool fallbackOnDefault = true) const = 0;
François Gaffie251c7f02018-11-07 10:41:08 +0100296
François Gaffie4b2018b2018-11-07 11:18:59 +0100297 /**
298 * @brief listAudioVolumeGroups introspection API to get the Audio Volume Groups, aka
299 * former stream aliases in Audio Service, defining volume curves attached to one or more
300 * Audio Attributes.
301 * @param groups
302 * @return NO_ERROR if the volume groups were retrieved successfully, error code otherwise
303 */
304 virtual status_t listAudioVolumeGroups(AudioVolumeGroupVector &groups) const = 0;
305
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700306 /**
jiabin0a488932020-08-07 17:32:40 -0700307 * @brief setDevicesRoleForStrategy sets devices role for a strategy when available. To remove
308 * devices role, removeDevicesRoleForStrategy must be called. When devices role is set
309 * successfully, previously set devices for the same role and strategy will be removed.
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700310 * @param strategy the audio strategy whose routing will be affected
jiabin0a488932020-08-07 17:32:40 -0700311 * @param role the role of the devices for the strategy. All device roles are defined at
312 * system/media/audio/include/system/audio_policy.h. DEVICE_ROLE_NONE is invalid
313 * for setting.
314 * @param devices the audio devices to be set
315 * @return BAD_VALUE if the strategy or role is invalid,
316 * or NO_ERROR if the role of the devices for strategy was set
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700317 */
jiabin0a488932020-08-07 17:32:40 -0700318 virtual status_t setDevicesRoleForStrategy(product_strategy_t strategy, device_role_t role,
319 const AudioDeviceTypeAddrVector &devices) = 0;
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700320
321 /**
jiabin0a488932020-08-07 17:32:40 -0700322 * @brief removeDevicesRoleForStrategy removes the role of device(s) previously set
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700323 * for the given strategy
324 * @param strategy the audio strategy whose routing will be affected
jiabin0a488932020-08-07 17:32:40 -0700325 * @param role the role of the devices for strategy
326 * @return BAD_VALUE if the strategy or role is invalid,
327 * or NO_ERROR if the devices for this role was removed
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700328 */
jiabin0a488932020-08-07 17:32:40 -0700329 virtual status_t removeDevicesRoleForStrategy(product_strategy_t strategy,
330 device_role_t role) = 0;
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700331
332 /**
jiabin0a488932020-08-07 17:32:40 -0700333 * @brief getDevicesForRoleAndStrategy queries which devices have the specified role for the
334 * specified strategy
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700335 * @param strategy the strategy to query
jiabin0a488932020-08-07 17:32:40 -0700336 * @param role the role of the devices to query
337 * @param devices returns list of devices with matching role for the specified strategy.
338 * DEVICE_ROLE_NONE is invalid as input.
339 * @return BAD_VALUE if the strategy or role is invalid,
340 * or NAME_NOT_FOUND if no device for the role and strategy was set
341 * or NO_ERROR if the devices parameter contains a list of devices
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700342 */
jiabin0a488932020-08-07 17:32:40 -0700343 virtual status_t getDevicesForRoleAndStrategy(product_strategy_t strategy, device_role_t role,
344 AudioDeviceTypeAddrVector &devices) const = 0;
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700345
Jiabin Huang3b98d322020-09-03 17:54:16 +0000346 /**
347 * @brief setDevicesRoleForCapturePreset sets devices role for a capture preset when available.
348 * To remove devices role, removeDevicesRoleForCapturePreset must be called. Calling
349 * clearDevicesRoleForCapturePreset will remove all devices as role. When devices role is set
350 * successfully, previously set devices for the same role and capture preset will be removed.
351 * @param audioSource the audio capture preset whose routing will be affected
352 * @param role the role of the devices for the capture preset. All device roles are defined at
353 * system/media/audio/include/system/audio_policy.h. DEVICE_ROLE_NONE is invalid
354 * for setting.
355 * @param devices the audio devices to be set
356 * @return BAD_VALUE if the capture preset or role is invalid,
357 * or NO_ERROR if the role of the devices for capture preset was set
358 */
359 virtual status_t setDevicesRoleForCapturePreset(audio_source_t audioSource, device_role_t role,
360 const AudioDeviceTypeAddrVector &devices) = 0;
361
362 /**
363 * @brief addDevicesRoleForCapturePreset adds devices role for a capture preset when available.
364 * To remove devices role, removeDevicesRoleForCapturePreset must be called. Calling
365 * clearDevicesRoleForCapturePreset will remove all devices as role.
366 * @param audioSource the audio capture preset whose routing will be affected
367 * @param role the role of the devices for the capture preset. All device roles are defined at
368 * system/media/audio/include/system/audio_policy.h. DEVICE_ROLE_NONE is invalid
369 * for setting.
370 * @param devices the audio devices to be added
371 * @return BAD_VALUE if the capture preset or role is invalid,
372 * or NO_ERROR if the role of the devices for capture preset was added
373 */
374 virtual status_t addDevicesRoleForCapturePreset(audio_source_t audioSource, device_role_t role,
375 const AudioDeviceTypeAddrVector &devices) = 0;
376
377 /**
378 * @brief removeDevicesRoleForCapturePreset removes the role of device(s) previously set
379 * for the given capture preset
380 * @param audioSource the audio capture preset whose routing will be affected
381 * @param role the role of the devices for the capture preset
382 * @param devices the devices to be removed
383 * @return BAD_VALUE if 1) the capture preset is invalid, 2) role is invalid or 3) the list of
384 * devices to be removed are not all present as role for a capture preset
385 * or NO_ERROR if the devices for this role was removed
386 */
387 virtual status_t removeDevicesRoleForCapturePreset(audio_source_t audioSource,
388 device_role_t role, const AudioDeviceTypeAddrVector& devices) = 0;
389
390 /**
391 * @brief clearDevicesRoleForCapturePreset removes the role of all device(s) previously set
392 * for the given capture preset
393 * @param audioSource the audio capture preset whose routing will be affected
394 * @param role the role of the devices for the capture preset
395 * @return BAD_VALUE if the capture preset or role is invalid,
396 * or NO_ERROR if the devices for this role was removed
397 */
398 virtual status_t clearDevicesRoleForCapturePreset(audio_source_t audioSource,
399 device_role_t role);
400
401 /**
402 * @brief getDevicesForRoleAndCapturePreset queries which devices have the specified role for
403 * the specified capture preset
404 * @param audioSource the capture preset to query
405 * @param role the role of the devices to query
406 * @param devices returns list of devices with matching role for the specified capture preset.
407 * DEVICE_ROLE_NONE is invalid as input.
408 * @return BAD_VALUE if the capture preset or role is invalid,
409 * or NAME_NOT_FOUND if no device for the role and capture preset was set
410 * or NO_ERROR if the devices parameter contains a list of devices
411 */
412 virtual status_t getDevicesForRoleAndCapturePreset(audio_source_t audioSource,
413 device_role_t role, AudioDeviceTypeAddrVector &devices) const = 0;
414
jiabin415af492020-09-17 14:31:20 -0700415 /**
416 * @brief getActiveMediaDevices returns which devices will most likely to be used for media
417 * @param availableDevices all available devices
418 * @return collection of active devices
419 */
420 virtual DeviceVector getActiveMediaDevices(const DeviceVector& availableDevices) const = 0;
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700421
François Gaffiedc7553f2018-11-02 10:39:57 +0100422 virtual void dump(String8 *dst) const = 0;
423
François Gaffie2110e042015-03-24 08:41:51 +0100424protected:
Mikhail Naganov47835552019-05-14 10:32:51 -0700425 virtual ~EngineInterface() {}
François Gaffie2110e042015-03-24 08:41:51 +0100426};
427
Mikhail Naganov47835552019-05-14 10:32:51 -0700428__attribute__((visibility("default")))
429extern "C" EngineInterface* createEngineInstance();
430
431__attribute__((visibility("default")))
432extern "C" void destroyEngineInstance(EngineInterface *engine);
433
Mikhail Naganov1b2a7942017-12-08 10:18:09 -0800434} // namespace android