blob: 9c156ab13799d46cbc060f8c520c06edb693b3cf [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
Mikhail Naganovabb04782023-05-02 13:56:01 -070019#include <string>
jiabin2336de82021-04-01 23:52:18 +000020#include <utility>
21
François Gaffie2110e042015-03-24 08:41:51 +010022#include <AudioPolicyManagerObserver.h>
François Gaffied0ba9ed2018-11-05 11:50:42 +010023#include <media/AudioProductStrategy.h>
François Gaffie4b2018b2018-11-07 11:18:59 +010024#include <media/AudioVolumeGroup.h>
Eric Laurentf5aa58d2019-02-22 18:20:11 -080025#include <IVolumeCurves.h>
François Gaffiedc7553f2018-11-02 10:39:57 +010026#include <policy.h>
François Gaffie2110e042015-03-24 08:41:51 +010027#include <Volume.h>
28#include <HwModule.h>
29#include <DeviceDescriptor.h>
30#include <system/audio.h>
31#include <system/audio_policy.h>
32#include <utils/Errors.h>
33#include <utils/Vector.h>
34
35namespace android {
36
François Gaffiedc7553f2018-11-02 10:39:57 +010037using DeviceStrategyMap = std::map<product_strategy_t, DeviceVector>;
38using StrategyVector = std::vector<product_strategy_t>;
François Gaffie251c7f02018-11-07 10:41:08 +010039using VolumeGroupVector = std::vector<volume_group_t>;
Jiabin Huang3b98d322020-09-03 17:54:16 +000040using CapturePresetDevicesRoleMap =
jiabin2336de82021-04-01 23:52:18 +000041 std::map<std::pair<audio_source_t, device_role_t>, AudioDeviceTypeAddrVector>;
François Gaffiedc7553f2018-11-02 10:39:57 +010042
François Gaffie2110e042015-03-24 08:41:51 +010043/**
44 * This interface is dedicated to the policy manager that a Policy Engine shall implement.
45 */
Mikhail Naganov47835552019-05-14 10:32:51 -070046class EngineInterface
François Gaffie2110e042015-03-24 08:41:51 +010047{
48public:
49 /**
Mikhail Naganovabb04782023-05-02 13:56:01 -070050 * Loads the engine configuration from the specified or the default config file.
51 * If loading failed, tries to fall back to some default configuration. If fallback
52 * is impossible, returns an error.
53 */
54 virtual status_t loadFromXmlConfigWithFallback(const std::string& xmlFilePath = "") = 0;
55
56 /**
François Gaffie2110e042015-03-24 08:41:51 +010057 * Checks if the engine was correctly initialized.
58 *
59 * @return NO_ERROR if initialization has been done correctly, error code otherwise..
60 */
61 virtual status_t initCheck() = 0;
62
63 /**
64 * Sets the Manager observer that allows the engine to retrieve information on collection
65 * of devices, streams, HwModules, ...
66 *
67 * @param[in] observer handle on the manager.
68 */
69 virtual void setObserver(AudioPolicyManagerObserver *observer) = 0;
70
71 /**
François Gaffie2110e042015-03-24 08:41:51 +010072 * Set the Telephony Mode.
73 *
74 * @param[in] mode: Android Phone state (normal, ringtone, csv, in communication)
75 *
76 * @return NO_ERROR if Telephony Mode set correctly, error code otherwise.
77 */
78 virtual status_t setPhoneState(audio_mode_t mode) = 0;
79
80 /**
81 * Get the telephony Mode
82 *
83 * @return the current telephony mode
84 */
85 virtual audio_mode_t getPhoneState() const = 0;
86
87 /**
88 * Set Force Use config for a given usage.
89 *
90 * @param[in] usage for which a configuration shall be forced.
91 * @param[in] config wished to be forced for the given usage.
92 *
François Gaffie20f06f92015-03-24 09:01:14 +010093 * @return NO_ERROR if the Force Use config was set correctly, error code otherwise (e.g. config
94 * not allowed a given usage...)
François Gaffie2110e042015-03-24 08:41:51 +010095 */
96 virtual status_t setForceUse(audio_policy_force_use_t usage,
97 audio_policy_forced_cfg_t config) = 0;
98
99 /**
100 * Get Force Use config for a given usage.
101 *
102 * @param[in] usage for which a configuration shall be forced.
103 *
104 * @return config wished to be forced for the given usage.
105 */
106 virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage) const = 0;
107
108 /**
109 * Set the connection state of device(s).
110 *
111 * @param[in] devDesc for which the state has changed.
112 * @param[in] state of availability of this(these) device(s).
113 *
114 * @return NO_ERROR if devices criterion updated correctly, error code otherwise.
115 */
116 virtual status_t setDeviceConnectionState(const android::sp<android::DeviceDescriptor> devDesc,
117 audio_policy_dev_state_t state) = 0;
118
François Gaffiedc7553f2018-11-02 10:39:57 +0100119 /**
120 * Get the strategy selected for a given audio attributes.
121 *
122 * @param[in] audio attributes to get the selected @product_strategy_t followed by.
Francois Gaffie11b65922020-09-24 16:59:08 +0200123 * @param fallbackOnDefault if true, will return the fallback strategy if the attributes
124 * are not explicitly assigned to a given strategy.
François Gaffiedc7553f2018-11-02 10:39:57 +0100125 * @return @product_strategy_t to be followed.
126 */
127 virtual product_strategy_t getProductStrategyForAttributes(
Francois Gaffie11b65922020-09-24 16:59:08 +0200128 const audio_attributes_t &attr, bool fallbackOnDefault = true) const = 0;
François Gaffiedc7553f2018-11-02 10:39:57 +0100129
130 /**
131 * @brief getOutputDevicesForAttributes retrieves the devices to be used for given
132 * audio attributes.
133 * @param attributes of the output requesting Device(s) selection
134 * @param preferedDevice valid reference if a prefered device is requested, nullptr otherwise.
135 * @param fromCache if true, the device is returned from internal cache,
136 * otherwise it is determined by current state (device connected,phone state,
137 * force use, a2dp output...)
138 * @return vector of selected device descriptors.
139 * Appropriate device for streams handled by the specified audio attributes according
140 * to current phone state, forced states, connected devices...
141 * if fromCache is true, the device is returned from internal cache,
142 * otherwise it is determined by current state (device connected,phone state, force use,
143 * a2dp output...)
144 * This allows to:
145 * 1 speed up process when the state is stable (when starting or stopping an output)
146 * 2 access to either current device selection (fromCache == true) or
147 * "future" device selection (fromCache == false) when called from a context
148 * where conditions are changing (setDeviceConnectionState(), setPhoneState()...) AND
149 * before manager updates its outputs.
150 */
151 virtual DeviceVector getOutputDevicesForAttributes(
152 const audio_attributes_t &attributes,
153 const sp<DeviceDescriptor> &preferedDevice = nullptr,
154 bool fromCache = false) const = 0;
155
156 /**
157 * @brief getOutputDevicesForStream Legacy function retrieving devices from a stream type.
158 * @param stream type of the output requesting Device(s) selection
159 * @param fromCache if true, the device is returned from internal cache,
160 * otherwise it is determined by current state (device connected,phone state,
161 * force use, a2dp output...)
162 * @return appropriate device for streams handled by the specified audio attributes according
163 * to current phone state, forced states, connected devices...
164 * if fromCache is true, the device is returned from internal cache,
165 * otherwise it is determined by current state (device connected,phone state, force use,
166 * a2dp output...)
167 * This allows to:
168 * 1 speed up process when the state is stable (when starting or stopping an output)
169 * 2 access to either current device selection (fromCache == true) or
170 * "future" device selection (fromCache == false) when called from a context
171 * where conditions are changing (setDeviceConnectionState(), setPhoneState()...) AND
172 * before manager updates its outputs.
173 */
174 virtual DeviceVector getOutputDevicesForStream(audio_stream_type_t stream,
175 bool fromCache = false) const = 0;
176
177 /**
178 * Get the input device selected for given audio attributes.
179 *
180 * @param[in] attr audio attributes to consider
181 * @param[out] mix to be used if a mix has been installed for the given audio attributes.
182 * @return selected input device for the audio attributes, may be null if error.
183 */
yuanjiahsu0735bf32021-03-18 08:12:54 +0800184 virtual sp<DeviceDescriptor> getInputDeviceForAttributes(const audio_attributes_t &attr,
185 uid_t uid = 0,
186 sp<AudioPolicyMix> *mix = nullptr)
187 const = 0;
François Gaffiedc7553f2018-11-02 10:39:57 +0100188
189 /**
190 * Get the legacy stream type for a given audio attributes.
191 *
192 * @param[in] audio attributes to get the associated audio_stream_type_t.
193 *
194 * @return audio_stream_type_t associated to the attributes.
195 */
196 virtual audio_stream_type_t getStreamTypeForAttributes(
197 const audio_attributes_t &attr) const = 0;
198
199 /**
200 * @brief getAttributesForStream get the audio attributes from legacy stream type
François Gaffie251c7f02018-11-07 10:41:08 +0100201 * Attributes returned might only be used to check upon routing decision, not volume decisions.
François Gaffiedc7553f2018-11-02 10:39:57 +0100202 * @param stream to consider
203 * @return audio attributes matching the legacy stream type
204 */
205 virtual audio_attributes_t getAttributesForStreamType(audio_stream_type_t stream) const = 0;
206
207 /**
208 * @brief getStreamTypesForProductStrategy retrieves the list of legacy stream type following
209 * the given product strategy
210 * @param ps product strategy to consider
211 * @return associated legacy Stream Types vector of the given product strategy
212 */
213 virtual StreamTypeVector getStreamTypesForProductStrategy(product_strategy_t ps) const = 0;
214
215 /**
216 * @brief getAllAttributesForProductStrategy retrieves all the attributes following the given
217 * product strategy. Any attributes that "matches" with this one will follow the product
218 * strategy.
219 * "matching" means the usage shall match if reference attributes has a defined usage, AND
220 * content type shall match if reference attributes has a defined content type AND
221 * flags shall match if reference attributes has defined flags AND
222 * tags shall match if reference attributes has defined tags.
223 * @param ps product strategy to consider
224 * @return vector of product strategy ids, empty if unknown strategy.
225 */
226 virtual AttributesVector getAllAttributesForProductStrategy(product_strategy_t ps) const = 0;
227
228 /**
229 * @brief getOrderedAudioProductStrategies
230 * @return priority ordered product strategies to help the AudioPolicyManager evaluating the
231 * device selection per output according to the prioritized strategies.
232 */
233 virtual StrategyVector getOrderedProductStrategies() const = 0;
234
235 /**
236 * @brief updateDeviceSelectionCache. Device selection for AudioAttribute / Streams is cached
237 * in the engine in order to speed up process when the audio system is stable.
238 * When a device is connected, the android mode is changed, engine is notified and can update
239 * the cache.
240 * When starting / stopping an output with a stream that can affect notification, the engine
241 * needs to update the cache upon this function call.
242 */
243 virtual void updateDeviceSelectionCache() = 0;
244
François Gaffied0ba9ed2018-11-05 11:50:42 +0100245 /**
246 * @brief listAudioProductStrategies. Introspection API to retrieve a collection of
247 * AudioProductStrategyVector that allows to build AudioAttributes according to a
248 * product_strategy which is just an index. It has also a human readable name to help the
249 * Car/Oem/AudioManager identiying the use case.
250 * @param strategies collection.
251 * @return OK if the list has been retrieved, error code otherwise
252 */
253 virtual status_t listAudioProductStrategies(AudioProductStrategyVector &strategies) const = 0;
254
Eric Laurentf5aa58d2019-02-22 18:20:11 -0800255 /**
256 * @brief getVolumeCurvesForAttributes retrieves the Volume Curves interface for the
257 * requested Audio Attributes.
258 * @param attr to be considered
259 * @return IVolumeCurves interface pointer if found, nullptr otherwise
260 */
François Gaffie251c7f02018-11-07 10:41:08 +0100261 virtual IVolumeCurves *getVolumeCurvesForAttributes(const audio_attributes_t &attr) const = 0;
Eric Laurentf5aa58d2019-02-22 18:20:11 -0800262
263 /**
264 * @brief getVolumeCurvesForStreamType retrieves the Volume Curves interface for the stream
265 * @param stream to be considered
266 * @return IVolumeCurves interface pointer if found, nullptr otherwise
267 */
François Gaffie251c7f02018-11-07 10:41:08 +0100268 virtual IVolumeCurves *getVolumeCurvesForStreamType(audio_stream_type_t stream) const = 0;
269
270 /**
271 * @brief getVolumeCurvesForVolumeGroup retrieves the Volume Curves interface for volume group
272 * @param group to be considered
273 * @return IVolumeCurves interface pointer if found, nullptr otherwise
274 */
275 virtual IVolumeCurves *getVolumeCurvesForVolumeGroup(volume_group_t group) const = 0;
276
277 /**
278 * @brief getVolumeGroups retrieves the collection of volume groups.
279 * @return vector of volume groups
280 */
281 virtual VolumeGroupVector getVolumeGroups() const = 0;
282
283 /**
284 * @brief getVolumeGroupForAttributes gets the appropriate volume group to be used for a given
285 * Audio Attributes.
286 * @param attr to be considered
Francois Gaffie11b65922020-09-24 16:59:08 +0200287 * @param fallbackOnDefault if true, will return the fallback volume group if the attributes
288 * are not associated to any volume group.
François Gaffie251c7f02018-11-07 10:41:08 +0100289 * @return volume group associated to the given audio attributes, default group if none
290 * applicable, VOLUME_GROUP_NONE if no default group defined.
291 */
Francois Gaffie11b65922020-09-24 16:59:08 +0200292 virtual volume_group_t getVolumeGroupForAttributes(
293 const audio_attributes_t &attr, bool fallbackOnDefault = true) const = 0;
François Gaffie251c7f02018-11-07 10:41:08 +0100294
295 /**
296 * @brief getVolumeGroupForStreamType gets the appropriate volume group to be used for a given
297 * legacy stream type
298 * @param stream type to be considered
Francois Gaffie11b65922020-09-24 16:59:08 +0200299 * @param fallbackOnDefault if true, will return the fallback volume group if the stream type
300 * is not associated to any volume group.
François Gaffie251c7f02018-11-07 10:41:08 +0100301 * @return volume group associated to the given stream type, default group if none applicable,
302 * VOLUME_GROUP_NONE if no default group defined.
303 */
Francois Gaffie11b65922020-09-24 16:59:08 +0200304 virtual volume_group_t getVolumeGroupForStreamType(
305 audio_stream_type_t stream, bool fallbackOnDefault = true) const = 0;
François Gaffie251c7f02018-11-07 10:41:08 +0100306
François Gaffie4b2018b2018-11-07 11:18:59 +0100307 /**
308 * @brief listAudioVolumeGroups introspection API to get the Audio Volume Groups, aka
309 * former stream aliases in Audio Service, defining volume curves attached to one or more
310 * Audio Attributes.
311 * @param groups
312 * @return NO_ERROR if the volume groups were retrieved successfully, error code otherwise
313 */
314 virtual status_t listAudioVolumeGroups(AudioVolumeGroupVector &groups) const = 0;
315
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700316 /**
jiabin0a488932020-08-07 17:32:40 -0700317 * @brief setDevicesRoleForStrategy sets devices role for a strategy when available. To remove
318 * devices role, removeDevicesRoleForStrategy must be called. When devices role is set
319 * successfully, previously set devices for the same role and strategy will be removed.
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700320 * @param strategy the audio strategy whose routing will be affected
jiabin0a488932020-08-07 17:32:40 -0700321 * @param role the role of the devices for the strategy. All device roles are defined at
322 * system/media/audio/include/system/audio_policy.h. DEVICE_ROLE_NONE is invalid
323 * for setting.
324 * @param devices the audio devices to be set
325 * @return BAD_VALUE if the strategy or role is invalid,
326 * or NO_ERROR if the role of the devices for strategy was set
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700327 */
jiabin0a488932020-08-07 17:32:40 -0700328 virtual status_t setDevicesRoleForStrategy(product_strategy_t strategy, device_role_t role,
329 const AudioDeviceTypeAddrVector &devices) = 0;
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700330
331 /**
jiabin0a488932020-08-07 17:32:40 -0700332 * @brief removeDevicesRoleForStrategy removes the role of device(s) previously set
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700333 * for the given strategy
334 * @param strategy the audio strategy whose routing will be affected
jiabin0a488932020-08-07 17:32:40 -0700335 * @param role the role of the devices for strategy
336 * @return BAD_VALUE if the strategy or role is invalid,
337 * or NO_ERROR if the devices for this role was removed
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700338 */
jiabin0a488932020-08-07 17:32:40 -0700339 virtual status_t removeDevicesRoleForStrategy(product_strategy_t strategy,
340 device_role_t role) = 0;
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700341
342 /**
jiabin0a488932020-08-07 17:32:40 -0700343 * @brief getDevicesForRoleAndStrategy queries which devices have the specified role for the
344 * specified strategy
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700345 * @param strategy the strategy to query
jiabin0a488932020-08-07 17:32:40 -0700346 * @param role the role of the devices to query
347 * @param devices returns list of devices with matching role for the specified strategy.
348 * DEVICE_ROLE_NONE is invalid as input.
349 * @return BAD_VALUE if the strategy or role is invalid,
350 * or NAME_NOT_FOUND if no device for the role and strategy was set
351 * or NO_ERROR if the devices parameter contains a list of devices
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700352 */
jiabin0a488932020-08-07 17:32:40 -0700353 virtual status_t getDevicesForRoleAndStrategy(product_strategy_t strategy, device_role_t role,
354 AudioDeviceTypeAddrVector &devices) const = 0;
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700355
Jiabin Huang3b98d322020-09-03 17:54:16 +0000356 /**
357 * @brief setDevicesRoleForCapturePreset sets devices role for a capture preset when available.
358 * To remove devices role, removeDevicesRoleForCapturePreset must be called. Calling
359 * clearDevicesRoleForCapturePreset will remove all devices as role. When devices role is set
360 * successfully, previously set devices for the same role and capture preset will be removed.
361 * @param audioSource the audio capture preset whose routing will be affected
362 * @param role the role of the devices for the capture preset. All device roles are defined at
363 * system/media/audio/include/system/audio_policy.h. DEVICE_ROLE_NONE is invalid
364 * for setting.
365 * @param devices the audio devices to be set
366 * @return BAD_VALUE if the capture preset or role is invalid,
367 * or NO_ERROR if the role of the devices for capture preset was set
368 */
369 virtual status_t setDevicesRoleForCapturePreset(audio_source_t audioSource, device_role_t role,
370 const AudioDeviceTypeAddrVector &devices) = 0;
371
372 /**
373 * @brief addDevicesRoleForCapturePreset adds devices role for a capture preset when available.
374 * To remove devices role, removeDevicesRoleForCapturePreset must be called. Calling
375 * clearDevicesRoleForCapturePreset will remove all devices as role.
376 * @param audioSource the audio capture preset whose routing will be affected
377 * @param role the role of the devices for the capture preset. All device roles are defined at
378 * system/media/audio/include/system/audio_policy.h. DEVICE_ROLE_NONE is invalid
379 * for setting.
380 * @param devices the audio devices to be added
381 * @return BAD_VALUE if the capture preset or role is invalid,
382 * or NO_ERROR if the role of the devices for capture preset was added
383 */
384 virtual status_t addDevicesRoleForCapturePreset(audio_source_t audioSource, device_role_t role,
385 const AudioDeviceTypeAddrVector &devices) = 0;
386
387 /**
388 * @brief removeDevicesRoleForCapturePreset removes the role of device(s) previously set
389 * for the given capture preset
390 * @param audioSource the audio capture preset whose routing will be affected
391 * @param role the role of the devices for the capture preset
392 * @param devices the devices to be removed
393 * @return BAD_VALUE if 1) the capture preset is invalid, 2) role is invalid or 3) the list of
394 * devices to be removed are not all present as role for a capture preset
395 * or NO_ERROR if the devices for this role was removed
396 */
397 virtual status_t removeDevicesRoleForCapturePreset(audio_source_t audioSource,
398 device_role_t role, const AudioDeviceTypeAddrVector& devices) = 0;
399
400 /**
401 * @brief clearDevicesRoleForCapturePreset removes the role of all device(s) previously set
402 * for the given capture preset
403 * @param audioSource the audio capture preset whose routing will be affected
404 * @param role the role of the devices for the capture preset
405 * @return BAD_VALUE if the capture preset or role is invalid,
406 * or NO_ERROR if the devices for this role was removed
407 */
408 virtual status_t clearDevicesRoleForCapturePreset(audio_source_t audioSource,
409 device_role_t role);
410
411 /**
412 * @brief getDevicesForRoleAndCapturePreset queries which devices have the specified role for
413 * the specified capture preset
414 * @param audioSource the capture preset to query
415 * @param role the role of the devices to query
416 * @param devices returns list of devices with matching role for the specified capture preset.
417 * DEVICE_ROLE_NONE is invalid as input.
418 * @return BAD_VALUE if the capture preset or role is invalid,
419 * or NAME_NOT_FOUND if no device for the role and capture preset was set
420 * or NO_ERROR if the devices parameter contains a list of devices
421 */
422 virtual status_t getDevicesForRoleAndCapturePreset(audio_source_t audioSource,
423 device_role_t role, AudioDeviceTypeAddrVector &devices) const = 0;
424
jiabin415af492020-09-17 14:31:20 -0700425 /**
426 * @brief getActiveMediaDevices returns which devices will most likely to be used for media
427 * @param availableDevices all available devices
428 * @return collection of active devices
429 */
430 virtual DeviceVector getActiveMediaDevices(const DeviceVector& availableDevices) const = 0;
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700431
jiabin76e829d2023-04-04 21:02:36 +0000432 /**
433 * @brief initializeDeviceSelectionCache. Device selection for AudioAttribute / Streams is
434 * cached in the engine in order to speed up process when the audio system is stable. When the
435 * audio system is initializing, not all audio devices information will be available. In that
436 * case, calling this function can allow the engine to initialize the device selection cache
437 * with default values.
438 * This must only be called when audio policy manager is initializing.
439 */
440 virtual void initializeDeviceSelectionCache() = 0;
441
François Gaffiedc7553f2018-11-02 10:39:57 +0100442 virtual void dump(String8 *dst) const = 0;
443
François Gaffie2110e042015-03-24 08:41:51 +0100444protected:
Mikhail Naganov47835552019-05-14 10:32:51 -0700445 virtual ~EngineInterface() {}
François Gaffie2110e042015-03-24 08:41:51 +0100446};
447
Mikhail Naganov47835552019-05-14 10:32:51 -0700448__attribute__((visibility("default")))
449extern "C" EngineInterface* createEngineInstance();
450
451__attribute__((visibility("default")))
452extern "C" void destroyEngineInstance(EngineInterface *engine);
453
Mikhail Naganov1b2a7942017-12-08 10:18:09 -0800454} // namespace android