blob: ea8fc41fbb052af86949e39c7526b3eb7f8e641b [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 */
Jan Sebechlebsky1a80c062022-08-09 15:21:18 +0200176 virtual sp<DeviceDescriptor> getInputDeviceForAttributes(
177 const audio_attributes_t &attr,
178 uid_t uid = 0,
179 audio_session_t session = AUDIO_SESSION_NONE,
180 sp<AudioPolicyMix> *mix = nullptr) const = 0;
François Gaffiedc7553f2018-11-02 10:39:57 +0100181
182 /**
183 * Get the legacy stream type for a given audio attributes.
184 *
185 * @param[in] audio attributes to get the associated audio_stream_type_t.
186 *
187 * @return audio_stream_type_t associated to the attributes.
188 */
189 virtual audio_stream_type_t getStreamTypeForAttributes(
190 const audio_attributes_t &attr) const = 0;
191
192 /**
193 * @brief getAttributesForStream get the audio attributes from legacy stream type
François Gaffie251c7f02018-11-07 10:41:08 +0100194 * Attributes returned might only be used to check upon routing decision, not volume decisions.
François Gaffiedc7553f2018-11-02 10:39:57 +0100195 * @param stream to consider
196 * @return audio attributes matching the legacy stream type
197 */
198 virtual audio_attributes_t getAttributesForStreamType(audio_stream_type_t stream) const = 0;
199
200 /**
201 * @brief getStreamTypesForProductStrategy retrieves the list of legacy stream type following
202 * the given product strategy
203 * @param ps product strategy to consider
204 * @return associated legacy Stream Types vector of the given product strategy
205 */
206 virtual StreamTypeVector getStreamTypesForProductStrategy(product_strategy_t ps) const = 0;
207
208 /**
209 * @brief getAllAttributesForProductStrategy retrieves all the attributes following the given
210 * product strategy. Any attributes that "matches" with this one will follow the product
211 * strategy.
212 * "matching" means the usage shall match if reference attributes has a defined usage, AND
213 * content type shall match if reference attributes has a defined content type AND
214 * flags shall match if reference attributes has defined flags AND
215 * tags shall match if reference attributes has defined tags.
216 * @param ps product strategy to consider
217 * @return vector of product strategy ids, empty if unknown strategy.
218 */
219 virtual AttributesVector getAllAttributesForProductStrategy(product_strategy_t ps) const = 0;
220
221 /**
222 * @brief getOrderedAudioProductStrategies
223 * @return priority ordered product strategies to help the AudioPolicyManager evaluating the
224 * device selection per output according to the prioritized strategies.
225 */
226 virtual StrategyVector getOrderedProductStrategies() const = 0;
227
228 /**
229 * @brief updateDeviceSelectionCache. Device selection for AudioAttribute / Streams is cached
230 * in the engine in order to speed up process when the audio system is stable.
231 * When a device is connected, the android mode is changed, engine is notified and can update
232 * the cache.
233 * When starting / stopping an output with a stream that can affect notification, the engine
234 * needs to update the cache upon this function call.
235 */
236 virtual void updateDeviceSelectionCache() = 0;
237
François Gaffied0ba9ed2018-11-05 11:50:42 +0100238 /**
239 * @brief listAudioProductStrategies. Introspection API to retrieve a collection of
240 * AudioProductStrategyVector that allows to build AudioAttributes according to a
241 * product_strategy which is just an index. It has also a human readable name to help the
242 * Car/Oem/AudioManager identiying the use case.
243 * @param strategies collection.
244 * @return OK if the list has been retrieved, error code otherwise
245 */
246 virtual status_t listAudioProductStrategies(AudioProductStrategyVector &strategies) const = 0;
247
Eric Laurentf5aa58d2019-02-22 18:20:11 -0800248 /**
249 * @brief getVolumeCurvesForAttributes retrieves the Volume Curves interface for the
250 * requested Audio Attributes.
251 * @param attr to be considered
252 * @return IVolumeCurves interface pointer if found, nullptr otherwise
253 */
François Gaffie251c7f02018-11-07 10:41:08 +0100254 virtual IVolumeCurves *getVolumeCurvesForAttributes(const audio_attributes_t &attr) const = 0;
Eric Laurentf5aa58d2019-02-22 18:20:11 -0800255
256 /**
257 * @brief getVolumeCurvesForStreamType retrieves the Volume Curves interface for the stream
258 * @param stream to be considered
259 * @return IVolumeCurves interface pointer if found, nullptr otherwise
260 */
François Gaffie251c7f02018-11-07 10:41:08 +0100261 virtual IVolumeCurves *getVolumeCurvesForStreamType(audio_stream_type_t stream) const = 0;
262
263 /**
264 * @brief getVolumeCurvesForVolumeGroup retrieves the Volume Curves interface for volume group
265 * @param group to be considered
266 * @return IVolumeCurves interface pointer if found, nullptr otherwise
267 */
268 virtual IVolumeCurves *getVolumeCurvesForVolumeGroup(volume_group_t group) const = 0;
269
270 /**
271 * @brief getVolumeGroups retrieves the collection of volume groups.
272 * @return vector of volume groups
273 */
274 virtual VolumeGroupVector getVolumeGroups() const = 0;
275
276 /**
277 * @brief getVolumeGroupForAttributes gets the appropriate volume group to be used for a given
278 * Audio Attributes.
279 * @param attr to be considered
Francois Gaffie11b65922020-09-24 16:59:08 +0200280 * @param fallbackOnDefault if true, will return the fallback volume group if the attributes
281 * are not associated to any volume group.
François Gaffie251c7f02018-11-07 10:41:08 +0100282 * @return volume group associated to the given audio attributes, default group if none
283 * applicable, VOLUME_GROUP_NONE if no default group defined.
284 */
Francois Gaffie11b65922020-09-24 16:59:08 +0200285 virtual volume_group_t getVolumeGroupForAttributes(
286 const audio_attributes_t &attr, bool fallbackOnDefault = true) const = 0;
François Gaffie251c7f02018-11-07 10:41:08 +0100287
288 /**
289 * @brief getVolumeGroupForStreamType gets the appropriate volume group to be used for a given
290 * legacy stream type
291 * @param stream type to be considered
Francois Gaffie11b65922020-09-24 16:59:08 +0200292 * @param fallbackOnDefault if true, will return the fallback volume group if the stream type
293 * is not associated to any volume group.
François Gaffie251c7f02018-11-07 10:41:08 +0100294 * @return volume group associated to the given stream type, default group if none applicable,
295 * VOLUME_GROUP_NONE if no default group defined.
296 */
Francois Gaffie11b65922020-09-24 16:59:08 +0200297 virtual volume_group_t getVolumeGroupForStreamType(
298 audio_stream_type_t stream, bool fallbackOnDefault = true) const = 0;
François Gaffie251c7f02018-11-07 10:41:08 +0100299
François Gaffie4b2018b2018-11-07 11:18:59 +0100300 /**
301 * @brief listAudioVolumeGroups introspection API to get the Audio Volume Groups, aka
302 * former stream aliases in Audio Service, defining volume curves attached to one or more
303 * Audio Attributes.
304 * @param groups
305 * @return NO_ERROR if the volume groups were retrieved successfully, error code otherwise
306 */
307 virtual status_t listAudioVolumeGroups(AudioVolumeGroupVector &groups) const = 0;
308
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700309 /**
jiabin0a488932020-08-07 17:32:40 -0700310 * @brief setDevicesRoleForStrategy sets devices role for a strategy when available. To remove
311 * devices role, removeDevicesRoleForStrategy must be called. When devices role is set
312 * successfully, previously set devices for the same role and strategy will be removed.
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700313 * @param strategy the audio strategy whose routing will be affected
jiabin0a488932020-08-07 17:32:40 -0700314 * @param role the role of the devices for the strategy. All device roles are defined at
315 * system/media/audio/include/system/audio_policy.h. DEVICE_ROLE_NONE is invalid
316 * for setting.
317 * @param devices the audio devices to be set
318 * @return BAD_VALUE if the strategy or role is invalid,
319 * or NO_ERROR if the role of the devices for strategy was set
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700320 */
jiabin0a488932020-08-07 17:32:40 -0700321 virtual status_t setDevicesRoleForStrategy(product_strategy_t strategy, device_role_t role,
322 const AudioDeviceTypeAddrVector &devices) = 0;
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700323
324 /**
jiabin0a488932020-08-07 17:32:40 -0700325 * @brief removeDevicesRoleForStrategy removes the role of device(s) previously set
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700326 * for the given strategy
327 * @param strategy the audio strategy whose routing will be affected
jiabin0a488932020-08-07 17:32:40 -0700328 * @param role the role of the devices for strategy
Paul Wang5d7cdb52022-11-22 09:45:06 +0000329 * @param devices the audio devices to be removed
jiabin0a488932020-08-07 17:32:40 -0700330 * @return BAD_VALUE if the strategy or role is invalid,
331 * or NO_ERROR if the devices for this role was removed
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700332 */
Paul Wang5d7cdb52022-11-22 09:45:06 +0000333 virtual status_t removeDevicesRoleForStrategy(product_strategy_t strategy, device_role_t role,
334 const AudioDeviceTypeAddrVector &devices) = 0;
335
336 /**
337 * @brief clearDevicesRoleForStrategy removes the role of all devices previously set
338 * for the given strategy
339 * @param strategy the audio strategy whose routing will be affected
340 * @param role the role of the devices for strategy
341 * @return BAD_VALUE if the strategy or role is invalid,
342 * or NO_ERROR if the devices for this role was removed
343 */
344 virtual status_t clearDevicesRoleForStrategy(product_strategy_t strategy,
jiabin0a488932020-08-07 17:32:40 -0700345 device_role_t role) = 0;
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700346
347 /**
jiabin0a488932020-08-07 17:32:40 -0700348 * @brief getDevicesForRoleAndStrategy queries which devices have the specified role for the
349 * specified strategy
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700350 * @param strategy the strategy to query
jiabin0a488932020-08-07 17:32:40 -0700351 * @param role the role of the devices to query
352 * @param devices returns list of devices with matching role for the specified strategy.
353 * DEVICE_ROLE_NONE is invalid as input.
354 * @return BAD_VALUE if the strategy or role is invalid,
355 * or NAME_NOT_FOUND if no device for the role and strategy was set
356 * or NO_ERROR if the devices parameter contains a list of devices
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700357 */
jiabin0a488932020-08-07 17:32:40 -0700358 virtual status_t getDevicesForRoleAndStrategy(product_strategy_t strategy, device_role_t role,
359 AudioDeviceTypeAddrVector &devices) const = 0;
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700360
Jiabin Huang3b98d322020-09-03 17:54:16 +0000361 /**
362 * @brief setDevicesRoleForCapturePreset sets devices role for a capture preset when available.
363 * To remove devices role, removeDevicesRoleForCapturePreset must be called. Calling
364 * clearDevicesRoleForCapturePreset will remove all devices as role. When devices role is set
365 * successfully, previously set devices for the same role and capture preset will be removed.
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 set
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 set
373 */
374 virtual status_t setDevicesRoleForCapturePreset(audio_source_t audioSource, device_role_t role,
375 const AudioDeviceTypeAddrVector &devices) = 0;
376
377 /**
378 * @brief addDevicesRoleForCapturePreset adds devices role for a capture preset when available.
379 * To remove devices role, removeDevicesRoleForCapturePreset must be called. Calling
380 * clearDevicesRoleForCapturePreset will remove all devices as role.
381 * @param audioSource the audio capture preset whose routing will be affected
382 * @param role the role of the devices for the capture preset. All device roles are defined at
383 * system/media/audio/include/system/audio_policy.h. DEVICE_ROLE_NONE is invalid
384 * for setting.
385 * @param devices the audio devices to be added
386 * @return BAD_VALUE if the capture preset or role is invalid,
387 * or NO_ERROR if the role of the devices for capture preset was added
388 */
389 virtual status_t addDevicesRoleForCapturePreset(audio_source_t audioSource, device_role_t role,
390 const AudioDeviceTypeAddrVector &devices) = 0;
391
392 /**
393 * @brief removeDevicesRoleForCapturePreset removes the role of device(s) previously set
394 * for the given capture preset
395 * @param audioSource the audio capture preset whose routing will be affected
396 * @param role the role of the devices for the capture preset
397 * @param devices the devices to be removed
398 * @return BAD_VALUE if 1) the capture preset is invalid, 2) role is invalid or 3) the list of
399 * devices to be removed are not all present as role for a capture preset
400 * or NO_ERROR if the devices for this role was removed
401 */
402 virtual status_t removeDevicesRoleForCapturePreset(audio_source_t audioSource,
403 device_role_t role, const AudioDeviceTypeAddrVector& devices) = 0;
404
405 /**
406 * @brief clearDevicesRoleForCapturePreset removes the role of all device(s) previously set
407 * for the given capture preset
408 * @param audioSource the audio capture preset whose routing will be affected
409 * @param role the role of the devices for the capture preset
410 * @return BAD_VALUE if the capture preset or role is invalid,
411 * or NO_ERROR if the devices for this role was removed
412 */
413 virtual status_t clearDevicesRoleForCapturePreset(audio_source_t audioSource,
414 device_role_t role);
415
416 /**
417 * @brief getDevicesForRoleAndCapturePreset queries which devices have the specified role for
418 * the specified capture preset
419 * @param audioSource the capture preset to query
420 * @param role the role of the devices to query
421 * @param devices returns list of devices with matching role for the specified capture preset.
422 * DEVICE_ROLE_NONE is invalid as input.
423 * @return BAD_VALUE if the capture preset or role is invalid,
424 * or NAME_NOT_FOUND if no device for the role and capture preset was set
425 * or NO_ERROR if the devices parameter contains a list of devices
426 */
427 virtual status_t getDevicesForRoleAndCapturePreset(audio_source_t audioSource,
428 device_role_t role, AudioDeviceTypeAddrVector &devices) const = 0;
429
jiabin415af492020-09-17 14:31:20 -0700430 /**
431 * @brief getActiveMediaDevices returns which devices will most likely to be used for media
432 * @param availableDevices all available devices
433 * @return collection of active devices
434 */
435 virtual DeviceVector getActiveMediaDevices(const DeviceVector& availableDevices) const = 0;
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700436
jiabin29230182023-04-04 21:02:36 +0000437 /**
438 * @brief initializeDeviceSelectionCache. Device selection for AudioAttribute / Streams is
439 * cached in the engine in order to speed up process when the audio system is stable. When the
440 * audio system is initializing, not all audio devices information will be available. In that
441 * case, calling this function can allow the engine to initialize the device selection cache
442 * with default values.
443 * This must only be called when audio policy manager is initializing.
444 */
445 virtual void initializeDeviceSelectionCache() = 0;
446
François Gaffiedc7553f2018-11-02 10:39:57 +0100447 virtual void dump(String8 *dst) const = 0;
448
François Gaffie2110e042015-03-24 08:41:51 +0100449protected:
Mikhail Naganov47835552019-05-14 10:32:51 -0700450 virtual ~EngineInterface() {}
François Gaffie2110e042015-03-24 08:41:51 +0100451};
452
Mikhail Naganov47835552019-05-14 10:32:51 -0700453__attribute__((visibility("default")))
454extern "C" EngineInterface* createEngineInstance();
455
456__attribute__((visibility("default")))
457extern "C" void destroyEngineInstance(EngineInterface *engine);
458
Mikhail Naganov1b2a7942017-12-08 10:18:09 -0800459} // namespace android