blob: f0a01d39e7caeb2782de13de2a346ec7c8a4c29a [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
19#include <AudioPolicyManagerObserver.h>
François Gaffied0ba9ed2018-11-05 11:50:42 +010020#include <media/AudioProductStrategy.h>
François Gaffie4b2018b2018-11-07 11:18:59 +010021#include <media/AudioVolumeGroup.h>
Eric Laurentf5aa58d2019-02-22 18:20:11 -080022#include <IVolumeCurves.h>
François Gaffiedc7553f2018-11-02 10:39:57 +010023#include <policy.h>
François Gaffie2110e042015-03-24 08:41:51 +010024#include <Volume.h>
25#include <HwModule.h>
26#include <DeviceDescriptor.h>
27#include <system/audio.h>
28#include <system/audio_policy.h>
29#include <utils/Errors.h>
30#include <utils/Vector.h>
31
32namespace android {
33
François Gaffiedc7553f2018-11-02 10:39:57 +010034using DeviceStrategyMap = std::map<product_strategy_t, DeviceVector>;
35using StrategyVector = std::vector<product_strategy_t>;
François Gaffie251c7f02018-11-07 10:41:08 +010036using VolumeGroupVector = std::vector<volume_group_t>;
Jiabin Huang3b98d322020-09-03 17:54:16 +000037using CapturePresetDevicesRoleMap =
38 std::map<audio_source_t, std::map<device_role_t, AudioDeviceTypeAddrVector>>;
François Gaffiedc7553f2018-11-02 10:39:57 +010039
François Gaffie2110e042015-03-24 08:41:51 +010040/**
41 * This interface is dedicated to the policy manager that a Policy Engine shall implement.
42 */
Mikhail Naganov47835552019-05-14 10:32:51 -070043class EngineInterface
François Gaffie2110e042015-03-24 08:41:51 +010044{
45public:
46 /**
47 * Checks if the engine was correctly initialized.
48 *
49 * @return NO_ERROR if initialization has been done correctly, error code otherwise..
50 */
51 virtual status_t initCheck() = 0;
52
53 /**
54 * Sets the Manager observer that allows the engine to retrieve information on collection
55 * of devices, streams, HwModules, ...
56 *
57 * @param[in] observer handle on the manager.
58 */
59 virtual void setObserver(AudioPolicyManagerObserver *observer) = 0;
60
61 /**
François Gaffie2110e042015-03-24 08:41:51 +010062 * Set the Telephony Mode.
63 *
64 * @param[in] mode: Android Phone state (normal, ringtone, csv, in communication)
65 *
66 * @return NO_ERROR if Telephony Mode set correctly, error code otherwise.
67 */
68 virtual status_t setPhoneState(audio_mode_t mode) = 0;
69
70 /**
71 * Get the telephony Mode
72 *
73 * @return the current telephony mode
74 */
75 virtual audio_mode_t getPhoneState() const = 0;
76
77 /**
78 * Set Force Use config for a given usage.
79 *
80 * @param[in] usage for which a configuration shall be forced.
81 * @param[in] config wished to be forced for the given usage.
82 *
François Gaffie20f06f92015-03-24 09:01:14 +010083 * @return NO_ERROR if the Force Use config was set correctly, error code otherwise (e.g. config
84 * not allowed a given usage...)
François Gaffie2110e042015-03-24 08:41:51 +010085 */
86 virtual status_t setForceUse(audio_policy_force_use_t usage,
87 audio_policy_forced_cfg_t config) = 0;
88
89 /**
90 * Get Force Use config for a given usage.
91 *
92 * @param[in] usage for which a configuration shall be forced.
93 *
94 * @return config wished to be forced for the given usage.
95 */
96 virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage) const = 0;
97
98 /**
99 * Set the connection state of device(s).
100 *
101 * @param[in] devDesc for which the state has changed.
102 * @param[in] state of availability of this(these) device(s).
103 *
104 * @return NO_ERROR if devices criterion updated correctly, error code otherwise.
105 */
106 virtual status_t setDeviceConnectionState(const android::sp<android::DeviceDescriptor> devDesc,
107 audio_policy_dev_state_t state) = 0;
108
François Gaffiedc7553f2018-11-02 10:39:57 +0100109 /**
110 * Get the strategy selected for a given audio attributes.
111 *
112 * @param[in] audio attributes to get the selected @product_strategy_t followed by.
Francois Gaffie11b65922020-09-24 16:59:08 +0200113 * @param fallbackOnDefault if true, will return the fallback strategy if the attributes
114 * are not explicitly assigned to a given strategy.
François Gaffiedc7553f2018-11-02 10:39:57 +0100115 * @return @product_strategy_t to be followed.
116 */
117 virtual product_strategy_t getProductStrategyForAttributes(
Francois Gaffie11b65922020-09-24 16:59:08 +0200118 const audio_attributes_t &attr, bool fallbackOnDefault = true) const = 0;
François Gaffiedc7553f2018-11-02 10:39:57 +0100119
120 /**
121 * @brief getOutputDevicesForAttributes retrieves the devices to be used for given
122 * audio attributes.
123 * @param attributes of the output requesting Device(s) selection
124 * @param preferedDevice valid reference if a prefered device is requested, nullptr otherwise.
125 * @param fromCache if true, the device is returned from internal cache,
126 * otherwise it is determined by current state (device connected,phone state,
127 * force use, a2dp output...)
128 * @return vector of selected device descriptors.
129 * Appropriate device for streams handled by the specified audio attributes according
130 * to current phone state, forced states, connected devices...
131 * if fromCache is true, the device is returned from internal cache,
132 * otherwise it is determined by current state (device connected,phone state, force use,
133 * a2dp output...)
134 * This allows to:
135 * 1 speed up process when the state is stable (when starting or stopping an output)
136 * 2 access to either current device selection (fromCache == true) or
137 * "future" device selection (fromCache == false) when called from a context
138 * where conditions are changing (setDeviceConnectionState(), setPhoneState()...) AND
139 * before manager updates its outputs.
140 */
141 virtual DeviceVector getOutputDevicesForAttributes(
142 const audio_attributes_t &attributes,
143 const sp<DeviceDescriptor> &preferedDevice = nullptr,
144 bool fromCache = false) const = 0;
145
146 /**
147 * @brief getOutputDevicesForStream Legacy function retrieving devices from a stream type.
148 * @param stream type of the output requesting Device(s) selection
149 * @param fromCache if true, the device is returned from internal cache,
150 * otherwise it is determined by current state (device connected,phone state,
151 * force use, a2dp output...)
152 * @return appropriate device for streams handled by the specified audio attributes according
153 * to current phone state, forced states, connected devices...
154 * if fromCache is true, the device is returned from internal cache,
155 * otherwise it is determined by current state (device connected,phone state, force use,
156 * a2dp output...)
157 * This allows to:
158 * 1 speed up process when the state is stable (when starting or stopping an output)
159 * 2 access to either current device selection (fromCache == true) or
160 * "future" device selection (fromCache == false) when called from a context
161 * where conditions are changing (setDeviceConnectionState(), setPhoneState()...) AND
162 * before manager updates its outputs.
163 */
164 virtual DeviceVector getOutputDevicesForStream(audio_stream_type_t stream,
165 bool fromCache = false) const = 0;
166
167 /**
168 * Get the input device selected for given audio attributes.
169 *
170 * @param[in] attr audio attributes to consider
171 * @param[out] mix to be used if a mix has been installed for the given audio attributes.
172 * @return selected input device for the audio attributes, may be null if error.
173 */
174 virtual sp<DeviceDescriptor> getInputDeviceForAttributes(
Mikhail Naganovbfac5832019-03-05 16:55:28 -0800175 const audio_attributes_t &attr, sp<AudioPolicyMix> *mix = nullptr) const = 0;
François Gaffiedc7553f2018-11-02 10:39:57 +0100176
177 /**
178 * Get the legacy stream type for a given audio attributes.
179 *
180 * @param[in] audio attributes to get the associated audio_stream_type_t.
181 *
182 * @return audio_stream_type_t associated to the attributes.
183 */
184 virtual audio_stream_type_t getStreamTypeForAttributes(
185 const audio_attributes_t &attr) const = 0;
186
187 /**
188 * @brief getAttributesForStream get the audio attributes from legacy stream type
François Gaffie251c7f02018-11-07 10:41:08 +0100189 * Attributes returned might only be used to check upon routing decision, not volume decisions.
François Gaffiedc7553f2018-11-02 10:39:57 +0100190 * @param stream to consider
191 * @return audio attributes matching the legacy stream type
192 */
193 virtual audio_attributes_t getAttributesForStreamType(audio_stream_type_t stream) const = 0;
194
195 /**
196 * @brief getStreamTypesForProductStrategy retrieves the list of legacy stream type following
197 * the given product strategy
198 * @param ps product strategy to consider
199 * @return associated legacy Stream Types vector of the given product strategy
200 */
201 virtual StreamTypeVector getStreamTypesForProductStrategy(product_strategy_t ps) const = 0;
202
203 /**
204 * @brief getAllAttributesForProductStrategy retrieves all the attributes following the given
205 * product strategy. Any attributes that "matches" with this one will follow the product
206 * strategy.
207 * "matching" means the usage shall match if reference attributes has a defined usage, AND
208 * content type shall match if reference attributes has a defined content type AND
209 * flags shall match if reference attributes has defined flags AND
210 * tags shall match if reference attributes has defined tags.
211 * @param ps product strategy to consider
212 * @return vector of product strategy ids, empty if unknown strategy.
213 */
214 virtual AttributesVector getAllAttributesForProductStrategy(product_strategy_t ps) const = 0;
215
216 /**
217 * @brief getOrderedAudioProductStrategies
218 * @return priority ordered product strategies to help the AudioPolicyManager evaluating the
219 * device selection per output according to the prioritized strategies.
220 */
221 virtual StrategyVector getOrderedProductStrategies() const = 0;
222
223 /**
224 * @brief updateDeviceSelectionCache. Device selection for AudioAttribute / Streams is cached
225 * in the engine in order to speed up process when the audio system is stable.
226 * When a device is connected, the android mode is changed, engine is notified and can update
227 * the cache.
228 * When starting / stopping an output with a stream that can affect notification, the engine
229 * needs to update the cache upon this function call.
230 */
231 virtual void updateDeviceSelectionCache() = 0;
232
François Gaffied0ba9ed2018-11-05 11:50:42 +0100233 /**
234 * @brief listAudioProductStrategies. Introspection API to retrieve a collection of
235 * AudioProductStrategyVector that allows to build AudioAttributes according to a
236 * product_strategy which is just an index. It has also a human readable name to help the
237 * Car/Oem/AudioManager identiying the use case.
238 * @param strategies collection.
239 * @return OK if the list has been retrieved, error code otherwise
240 */
241 virtual status_t listAudioProductStrategies(AudioProductStrategyVector &strategies) const = 0;
242
Eric Laurentf5aa58d2019-02-22 18:20:11 -0800243 /**
244 * @brief getVolumeCurvesForAttributes retrieves the Volume Curves interface for the
245 * requested Audio Attributes.
246 * @param attr to be considered
247 * @return IVolumeCurves interface pointer if found, nullptr otherwise
248 */
François Gaffie251c7f02018-11-07 10:41:08 +0100249 virtual IVolumeCurves *getVolumeCurvesForAttributes(const audio_attributes_t &attr) const = 0;
Eric Laurentf5aa58d2019-02-22 18:20:11 -0800250
251 /**
252 * @brief getVolumeCurvesForStreamType retrieves the Volume Curves interface for the stream
253 * @param stream to be considered
254 * @return IVolumeCurves interface pointer if found, nullptr otherwise
255 */
François Gaffie251c7f02018-11-07 10:41:08 +0100256 virtual IVolumeCurves *getVolumeCurvesForStreamType(audio_stream_type_t stream) const = 0;
257
258 /**
259 * @brief getVolumeCurvesForVolumeGroup retrieves the Volume Curves interface for volume group
260 * @param group to be considered
261 * @return IVolumeCurves interface pointer if found, nullptr otherwise
262 */
263 virtual IVolumeCurves *getVolumeCurvesForVolumeGroup(volume_group_t group) const = 0;
264
265 /**
266 * @brief getVolumeGroups retrieves the collection of volume groups.
267 * @return vector of volume groups
268 */
269 virtual VolumeGroupVector getVolumeGroups() const = 0;
270
271 /**
272 * @brief getVolumeGroupForAttributes gets the appropriate volume group to be used for a given
273 * Audio Attributes.
274 * @param attr to be considered
Francois Gaffie11b65922020-09-24 16:59:08 +0200275 * @param fallbackOnDefault if true, will return the fallback volume group if the attributes
276 * are not associated to any volume group.
François Gaffie251c7f02018-11-07 10:41:08 +0100277 * @return volume group associated to the given audio attributes, default group if none
278 * applicable, VOLUME_GROUP_NONE if no default group defined.
279 */
Francois Gaffie11b65922020-09-24 16:59:08 +0200280 virtual volume_group_t getVolumeGroupForAttributes(
281 const audio_attributes_t &attr, bool fallbackOnDefault = true) const = 0;
François Gaffie251c7f02018-11-07 10:41:08 +0100282
283 /**
284 * @brief getVolumeGroupForStreamType gets the appropriate volume group to be used for a given
285 * legacy stream type
286 * @param stream type to be considered
Francois Gaffie11b65922020-09-24 16:59:08 +0200287 * @param fallbackOnDefault if true, will return the fallback volume group if the stream type
288 * is not associated to any volume group.
François Gaffie251c7f02018-11-07 10:41:08 +0100289 * @return volume group associated to the given stream type, default group if none applicable,
290 * VOLUME_GROUP_NONE if no default group defined.
291 */
Francois Gaffie11b65922020-09-24 16:59:08 +0200292 virtual volume_group_t getVolumeGroupForStreamType(
293 audio_stream_type_t stream, bool fallbackOnDefault = true) const = 0;
François Gaffie251c7f02018-11-07 10:41:08 +0100294
François Gaffie4b2018b2018-11-07 11:18:59 +0100295 /**
296 * @brief listAudioVolumeGroups introspection API to get the Audio Volume Groups, aka
297 * former stream aliases in Audio Service, defining volume curves attached to one or more
298 * Audio Attributes.
299 * @param groups
300 * @return NO_ERROR if the volume groups were retrieved successfully, error code otherwise
301 */
302 virtual status_t listAudioVolumeGroups(AudioVolumeGroupVector &groups) const = 0;
303
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700304 /**
jiabin0a488932020-08-07 17:32:40 -0700305 * @brief setDevicesRoleForStrategy sets devices role for a strategy when available. To remove
306 * devices role, removeDevicesRoleForStrategy must be called. When devices role is set
307 * successfully, previously set devices for the same role and strategy will be removed.
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700308 * @param strategy the audio strategy whose routing will be affected
jiabin0a488932020-08-07 17:32:40 -0700309 * @param role the role of the devices for the strategy. All device roles are defined at
310 * system/media/audio/include/system/audio_policy.h. DEVICE_ROLE_NONE is invalid
311 * for setting.
312 * @param devices the audio devices to be set
313 * @return BAD_VALUE if the strategy or role is invalid,
314 * or NO_ERROR if the role of the devices for strategy was set
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700315 */
jiabin0a488932020-08-07 17:32:40 -0700316 virtual status_t setDevicesRoleForStrategy(product_strategy_t strategy, device_role_t role,
317 const AudioDeviceTypeAddrVector &devices) = 0;
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700318
319 /**
jiabin0a488932020-08-07 17:32:40 -0700320 * @brief removeDevicesRoleForStrategy removes the role of device(s) previously set
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700321 * for the given strategy
322 * @param strategy the audio strategy whose routing will be affected
jiabin0a488932020-08-07 17:32:40 -0700323 * @param role the role of the devices for strategy
324 * @return BAD_VALUE if the strategy or role is invalid,
325 * or NO_ERROR if the devices for this role was removed
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700326 */
jiabin0a488932020-08-07 17:32:40 -0700327 virtual status_t removeDevicesRoleForStrategy(product_strategy_t strategy,
328 device_role_t role) = 0;
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700329
330 /**
jiabin0a488932020-08-07 17:32:40 -0700331 * @brief getDevicesForRoleAndStrategy queries which devices have the specified role for the
332 * specified strategy
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700333 * @param strategy the strategy to query
jiabin0a488932020-08-07 17:32:40 -0700334 * @param role the role of the devices to query
335 * @param devices returns list of devices with matching role for the specified strategy.
336 * DEVICE_ROLE_NONE is invalid as input.
337 * @return BAD_VALUE if the strategy or role is invalid,
338 * or NAME_NOT_FOUND if no device for the role and strategy was set
339 * or NO_ERROR if the devices parameter contains a list of devices
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700340 */
jiabin0a488932020-08-07 17:32:40 -0700341 virtual status_t getDevicesForRoleAndStrategy(product_strategy_t strategy, device_role_t role,
342 AudioDeviceTypeAddrVector &devices) const = 0;
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700343
Jiabin Huang3b98d322020-09-03 17:54:16 +0000344 /**
345 * @brief setDevicesRoleForCapturePreset sets devices role for a capture preset when available.
346 * To remove devices role, removeDevicesRoleForCapturePreset must be called. Calling
347 * clearDevicesRoleForCapturePreset will remove all devices as role. When devices role is set
348 * successfully, previously set devices for the same role and capture preset will be removed.
349 * @param audioSource the audio capture preset whose routing will be affected
350 * @param role the role of the devices for the capture preset. All device roles are defined at
351 * system/media/audio/include/system/audio_policy.h. DEVICE_ROLE_NONE is invalid
352 * for setting.
353 * @param devices the audio devices to be set
354 * @return BAD_VALUE if the capture preset or role is invalid,
355 * or NO_ERROR if the role of the devices for capture preset was set
356 */
357 virtual status_t setDevicesRoleForCapturePreset(audio_source_t audioSource, device_role_t role,
358 const AudioDeviceTypeAddrVector &devices) = 0;
359
360 /**
361 * @brief addDevicesRoleForCapturePreset adds devices role for a capture preset when available.
362 * To remove devices role, removeDevicesRoleForCapturePreset must be called. Calling
363 * clearDevicesRoleForCapturePreset will remove all devices as role.
364 * @param audioSource the audio capture preset whose routing will be affected
365 * @param role the role of the devices for the capture preset. All device roles are defined at
366 * system/media/audio/include/system/audio_policy.h. DEVICE_ROLE_NONE is invalid
367 * for setting.
368 * @param devices the audio devices to be added
369 * @return BAD_VALUE if the capture preset or role is invalid,
370 * or NO_ERROR if the role of the devices for capture preset was added
371 */
372 virtual status_t addDevicesRoleForCapturePreset(audio_source_t audioSource, device_role_t role,
373 const AudioDeviceTypeAddrVector &devices) = 0;
374
375 /**
376 * @brief removeDevicesRoleForCapturePreset removes the role of device(s) previously set
377 * for the given capture preset
378 * @param audioSource the audio capture preset whose routing will be affected
379 * @param role the role of the devices for the capture preset
380 * @param devices the devices to be removed
381 * @return BAD_VALUE if 1) the capture preset is invalid, 2) role is invalid or 3) the list of
382 * devices to be removed are not all present as role for a capture preset
383 * or NO_ERROR if the devices for this role was removed
384 */
385 virtual status_t removeDevicesRoleForCapturePreset(audio_source_t audioSource,
386 device_role_t role, const AudioDeviceTypeAddrVector& devices) = 0;
387
388 /**
389 * @brief clearDevicesRoleForCapturePreset removes the role of all device(s) previously set
390 * for the given capture preset
391 * @param audioSource the audio capture preset whose routing will be affected
392 * @param role the role of the devices for the capture preset
393 * @return BAD_VALUE if the capture preset or role is invalid,
394 * or NO_ERROR if the devices for this role was removed
395 */
396 virtual status_t clearDevicesRoleForCapturePreset(audio_source_t audioSource,
397 device_role_t role);
398
399 /**
400 * @brief getDevicesForRoleAndCapturePreset queries which devices have the specified role for
401 * the specified capture preset
402 * @param audioSource the capture preset to query
403 * @param role the role of the devices to query
404 * @param devices returns list of devices with matching role for the specified capture preset.
405 * DEVICE_ROLE_NONE is invalid as input.
406 * @return BAD_VALUE if the capture preset or role is invalid,
407 * or NAME_NOT_FOUND if no device for the role and capture preset was set
408 * or NO_ERROR if the devices parameter contains a list of devices
409 */
410 virtual status_t getDevicesForRoleAndCapturePreset(audio_source_t audioSource,
411 device_role_t role, AudioDeviceTypeAddrVector &devices) const = 0;
412
jiabin415af492020-09-17 14:31:20 -0700413 /**
414 * @brief getActiveMediaDevices returns which devices will most likely to be used for media
415 * @param availableDevices all available devices
416 * @return collection of active devices
417 */
418 virtual DeviceVector getActiveMediaDevices(const DeviceVector& availableDevices) const = 0;
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700419
François Gaffiedc7553f2018-11-02 10:39:57 +0100420 virtual void dump(String8 *dst) const = 0;
421
François Gaffie2110e042015-03-24 08:41:51 +0100422protected:
Mikhail Naganov47835552019-05-14 10:32:51 -0700423 virtual ~EngineInterface() {}
François Gaffie2110e042015-03-24 08:41:51 +0100424};
425
Mikhail Naganov47835552019-05-14 10:32:51 -0700426__attribute__((visibility("default")))
427extern "C" EngineInterface* createEngineInstance();
428
429__attribute__((visibility("default")))
430extern "C" void destroyEngineInstance(EngineInterface *engine);
431
Mikhail Naganov1b2a7942017-12-08 10:18:09 -0800432} // namespace android