blob: a9b536b79255dd980d506da7a3daf0f7c769f3d3 [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.
113 *
114 * @return @product_strategy_t to be followed.
115 */
116 virtual product_strategy_t getProductStrategyForAttributes(
117 const audio_attributes_t &attr) const = 0;
118
119 /**
120 * @brief getOutputDevicesForAttributes retrieves the devices to be used for given
121 * audio attributes.
122 * @param attributes of the output requesting Device(s) selection
123 * @param preferedDevice valid reference if a prefered device is requested, nullptr otherwise.
124 * @param fromCache if true, the device is returned from internal cache,
125 * otherwise it is determined by current state (device connected,phone state,
126 * force use, a2dp output...)
127 * @return vector of selected device descriptors.
128 * Appropriate device for streams handled by the specified audio attributes according
129 * to current phone state, forced states, connected devices...
130 * if fromCache is true, the device is returned from internal cache,
131 * otherwise it is determined by current state (device connected,phone state, force use,
132 * a2dp output...)
133 * This allows to:
134 * 1 speed up process when the state is stable (when starting or stopping an output)
135 * 2 access to either current device selection (fromCache == true) or
136 * "future" device selection (fromCache == false) when called from a context
137 * where conditions are changing (setDeviceConnectionState(), setPhoneState()...) AND
138 * before manager updates its outputs.
139 */
140 virtual DeviceVector getOutputDevicesForAttributes(
141 const audio_attributes_t &attributes,
142 const sp<DeviceDescriptor> &preferedDevice = nullptr,
143 bool fromCache = false) const = 0;
144
145 /**
146 * @brief getOutputDevicesForStream Legacy function retrieving devices from a stream type.
147 * @param stream type of the output requesting Device(s) selection
148 * @param fromCache if true, the device is returned from internal cache,
149 * otherwise it is determined by current state (device connected,phone state,
150 * force use, a2dp output...)
151 * @return appropriate device for streams handled by the specified audio attributes according
152 * to current phone state, forced states, connected devices...
153 * if fromCache is true, the device is returned from internal cache,
154 * otherwise it is determined by current state (device connected,phone state, force use,
155 * a2dp output...)
156 * This allows to:
157 * 1 speed up process when the state is stable (when starting or stopping an output)
158 * 2 access to either current device selection (fromCache == true) or
159 * "future" device selection (fromCache == false) when called from a context
160 * where conditions are changing (setDeviceConnectionState(), setPhoneState()...) AND
161 * before manager updates its outputs.
162 */
163 virtual DeviceVector getOutputDevicesForStream(audio_stream_type_t stream,
164 bool fromCache = false) const = 0;
165
166 /**
167 * Get the input device selected for given audio attributes.
168 *
169 * @param[in] attr audio attributes to consider
170 * @param[out] mix to be used if a mix has been installed for the given audio attributes.
171 * @return selected input device for the audio attributes, may be null if error.
172 */
173 virtual sp<DeviceDescriptor> getInputDeviceForAttributes(
Mikhail Naganovbfac5832019-03-05 16:55:28 -0800174 const audio_attributes_t &attr, sp<AudioPolicyMix> *mix = nullptr) const = 0;
François Gaffiedc7553f2018-11-02 10:39:57 +0100175
176 /**
177 * Get the legacy stream type for a given audio attributes.
178 *
179 * @param[in] audio attributes to get the associated audio_stream_type_t.
180 *
181 * @return audio_stream_type_t associated to the attributes.
182 */
183 virtual audio_stream_type_t getStreamTypeForAttributes(
184 const audio_attributes_t &attr) const = 0;
185
186 /**
187 * @brief getAttributesForStream get the audio attributes from legacy stream type
François Gaffie251c7f02018-11-07 10:41:08 +0100188 * Attributes returned might only be used to check upon routing decision, not volume decisions.
François Gaffiedc7553f2018-11-02 10:39:57 +0100189 * @param stream to consider
190 * @return audio attributes matching the legacy stream type
191 */
192 virtual audio_attributes_t getAttributesForStreamType(audio_stream_type_t stream) const = 0;
193
194 /**
195 * @brief getStreamTypesForProductStrategy retrieves the list of legacy stream type following
196 * the given product strategy
197 * @param ps product strategy to consider
198 * @return associated legacy Stream Types vector of the given product strategy
199 */
200 virtual StreamTypeVector getStreamTypesForProductStrategy(product_strategy_t ps) const = 0;
201
202 /**
203 * @brief getAllAttributesForProductStrategy retrieves all the attributes following the given
204 * product strategy. Any attributes that "matches" with this one will follow the product
205 * strategy.
206 * "matching" means the usage shall match if reference attributes has a defined usage, AND
207 * content type shall match if reference attributes has a defined content type AND
208 * flags shall match if reference attributes has defined flags AND
209 * tags shall match if reference attributes has defined tags.
210 * @param ps product strategy to consider
211 * @return vector of product strategy ids, empty if unknown strategy.
212 */
213 virtual AttributesVector getAllAttributesForProductStrategy(product_strategy_t ps) const = 0;
214
215 /**
216 * @brief getOrderedAudioProductStrategies
217 * @return priority ordered product strategies to help the AudioPolicyManager evaluating the
218 * device selection per output according to the prioritized strategies.
219 */
220 virtual StrategyVector getOrderedProductStrategies() const = 0;
221
222 /**
223 * @brief updateDeviceSelectionCache. Device selection for AudioAttribute / Streams is cached
224 * in the engine in order to speed up process when the audio system is stable.
225 * When a device is connected, the android mode is changed, engine is notified and can update
226 * the cache.
227 * When starting / stopping an output with a stream that can affect notification, the engine
228 * needs to update the cache upon this function call.
229 */
230 virtual void updateDeviceSelectionCache() = 0;
231
François Gaffied0ba9ed2018-11-05 11:50:42 +0100232 /**
233 * @brief listAudioProductStrategies. Introspection API to retrieve a collection of
234 * AudioProductStrategyVector that allows to build AudioAttributes according to a
235 * product_strategy which is just an index. It has also a human readable name to help the
236 * Car/Oem/AudioManager identiying the use case.
237 * @param strategies collection.
238 * @return OK if the list has been retrieved, error code otherwise
239 */
240 virtual status_t listAudioProductStrategies(AudioProductStrategyVector &strategies) const = 0;
241
Eric Laurentf5aa58d2019-02-22 18:20:11 -0800242 /**
243 * @brief getVolumeCurvesForAttributes retrieves the Volume Curves interface for the
244 * requested Audio Attributes.
245 * @param attr to be considered
246 * @return IVolumeCurves interface pointer if found, nullptr otherwise
247 */
François Gaffie251c7f02018-11-07 10:41:08 +0100248 virtual IVolumeCurves *getVolumeCurvesForAttributes(const audio_attributes_t &attr) const = 0;
Eric Laurentf5aa58d2019-02-22 18:20:11 -0800249
250 /**
251 * @brief getVolumeCurvesForStreamType retrieves the Volume Curves interface for the stream
252 * @param stream to be considered
253 * @return IVolumeCurves interface pointer if found, nullptr otherwise
254 */
François Gaffie251c7f02018-11-07 10:41:08 +0100255 virtual IVolumeCurves *getVolumeCurvesForStreamType(audio_stream_type_t stream) const = 0;
256
257 /**
258 * @brief getVolumeCurvesForVolumeGroup retrieves the Volume Curves interface for volume group
259 * @param group to be considered
260 * @return IVolumeCurves interface pointer if found, nullptr otherwise
261 */
262 virtual IVolumeCurves *getVolumeCurvesForVolumeGroup(volume_group_t group) const = 0;
263
264 /**
265 * @brief getVolumeGroups retrieves the collection of volume groups.
266 * @return vector of volume groups
267 */
268 virtual VolumeGroupVector getVolumeGroups() const = 0;
269
270 /**
271 * @brief getVolumeGroupForAttributes gets the appropriate volume group to be used for a given
272 * Audio Attributes.
273 * @param attr to be considered
274 * @return volume group associated to the given audio attributes, default group if none
275 * applicable, VOLUME_GROUP_NONE if no default group defined.
276 */
277 virtual volume_group_t getVolumeGroupForAttributes(const audio_attributes_t &attr) const = 0;
278
279 /**
280 * @brief getVolumeGroupForStreamType gets the appropriate volume group to be used for a given
281 * legacy stream type
282 * @param stream type to be considered
283 * @return volume group associated to the given stream type, default group if none applicable,
284 * VOLUME_GROUP_NONE if no default group defined.
285 */
286 virtual volume_group_t getVolumeGroupForStreamType(audio_stream_type_t stream) const = 0;
287
François Gaffie4b2018b2018-11-07 11:18:59 +0100288 /**
289 * @brief listAudioVolumeGroups introspection API to get the Audio Volume Groups, aka
290 * former stream aliases in Audio Service, defining volume curves attached to one or more
291 * Audio Attributes.
292 * @param groups
293 * @return NO_ERROR if the volume groups were retrieved successfully, error code otherwise
294 */
295 virtual status_t listAudioVolumeGroups(AudioVolumeGroupVector &groups) const = 0;
296
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700297 /**
jiabin0a488932020-08-07 17:32:40 -0700298 * @brief setDevicesRoleForStrategy sets devices role for a strategy when available. To remove
299 * devices role, removeDevicesRoleForStrategy must be called. When devices role is set
300 * successfully, previously set devices for the same role and strategy will be removed.
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700301 * @param strategy the audio strategy whose routing will be affected
jiabin0a488932020-08-07 17:32:40 -0700302 * @param role the role of the devices for the strategy. All device roles are defined at
303 * system/media/audio/include/system/audio_policy.h. DEVICE_ROLE_NONE is invalid
304 * for setting.
305 * @param devices the audio devices to be set
306 * @return BAD_VALUE if the strategy or role is invalid,
307 * or NO_ERROR if the role of the devices for strategy was set
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700308 */
jiabin0a488932020-08-07 17:32:40 -0700309 virtual status_t setDevicesRoleForStrategy(product_strategy_t strategy, device_role_t role,
310 const AudioDeviceTypeAddrVector &devices) = 0;
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700311
312 /**
jiabin0a488932020-08-07 17:32:40 -0700313 * @brief removeDevicesRoleForStrategy removes the role of device(s) previously set
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700314 * for the given strategy
315 * @param strategy the audio strategy whose routing will be affected
jiabin0a488932020-08-07 17:32:40 -0700316 * @param role the role of the devices for strategy
317 * @return BAD_VALUE if the strategy or role is invalid,
318 * or NO_ERROR if the devices for this role was removed
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700319 */
jiabin0a488932020-08-07 17:32:40 -0700320 virtual status_t removeDevicesRoleForStrategy(product_strategy_t strategy,
321 device_role_t role) = 0;
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700322
323 /**
jiabin0a488932020-08-07 17:32:40 -0700324 * @brief getDevicesForRoleAndStrategy queries which devices have the specified role for the
325 * specified strategy
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700326 * @param strategy the strategy to query
jiabin0a488932020-08-07 17:32:40 -0700327 * @param role the role of the devices to query
328 * @param devices returns list of devices with matching role for the specified strategy.
329 * DEVICE_ROLE_NONE is invalid as input.
330 * @return BAD_VALUE if the strategy or role is invalid,
331 * or NAME_NOT_FOUND if no device for the role and strategy was set
332 * or NO_ERROR if the devices parameter contains a list of devices
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700333 */
jiabin0a488932020-08-07 17:32:40 -0700334 virtual status_t getDevicesForRoleAndStrategy(product_strategy_t strategy, device_role_t role,
335 AudioDeviceTypeAddrVector &devices) const = 0;
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700336
Jiabin Huang3b98d322020-09-03 17:54:16 +0000337 /**
338 * @brief setDevicesRoleForCapturePreset sets devices role for a capture preset when available.
339 * To remove devices role, removeDevicesRoleForCapturePreset must be called. Calling
340 * clearDevicesRoleForCapturePreset will remove all devices as role. When devices role is set
341 * successfully, previously set devices for the same role and capture preset will be removed.
342 * @param audioSource the audio capture preset whose routing will be affected
343 * @param role the role of the devices for the capture preset. All device roles are defined at
344 * system/media/audio/include/system/audio_policy.h. DEVICE_ROLE_NONE is invalid
345 * for setting.
346 * @param devices the audio devices to be set
347 * @return BAD_VALUE if the capture preset or role is invalid,
348 * or NO_ERROR if the role of the devices for capture preset was set
349 */
350 virtual status_t setDevicesRoleForCapturePreset(audio_source_t audioSource, device_role_t role,
351 const AudioDeviceTypeAddrVector &devices) = 0;
352
353 /**
354 * @brief addDevicesRoleForCapturePreset adds devices role for a capture preset when available.
355 * To remove devices role, removeDevicesRoleForCapturePreset must be called. Calling
356 * clearDevicesRoleForCapturePreset will remove all devices as role.
357 * @param audioSource the audio capture preset whose routing will be affected
358 * @param role the role of the devices for the capture preset. All device roles are defined at
359 * system/media/audio/include/system/audio_policy.h. DEVICE_ROLE_NONE is invalid
360 * for setting.
361 * @param devices the audio devices to be added
362 * @return BAD_VALUE if the capture preset or role is invalid,
363 * or NO_ERROR if the role of the devices for capture preset was added
364 */
365 virtual status_t addDevicesRoleForCapturePreset(audio_source_t audioSource, device_role_t role,
366 const AudioDeviceTypeAddrVector &devices) = 0;
367
368 /**
369 * @brief removeDevicesRoleForCapturePreset removes the role of device(s) previously set
370 * for the given capture preset
371 * @param audioSource the audio capture preset whose routing will be affected
372 * @param role the role of the devices for the capture preset
373 * @param devices the devices to be removed
374 * @return BAD_VALUE if 1) the capture preset is invalid, 2) role is invalid or 3) the list of
375 * devices to be removed are not all present as role for a capture preset
376 * or NO_ERROR if the devices for this role was removed
377 */
378 virtual status_t removeDevicesRoleForCapturePreset(audio_source_t audioSource,
379 device_role_t role, const AudioDeviceTypeAddrVector& devices) = 0;
380
381 /**
382 * @brief clearDevicesRoleForCapturePreset removes the role of all device(s) previously set
383 * for the given capture preset
384 * @param audioSource the audio capture preset whose routing will be affected
385 * @param role the role of the devices for the capture preset
386 * @return BAD_VALUE if the capture preset or role is invalid,
387 * or NO_ERROR if the devices for this role was removed
388 */
389 virtual status_t clearDevicesRoleForCapturePreset(audio_source_t audioSource,
390 device_role_t role);
391
392 /**
393 * @brief getDevicesForRoleAndCapturePreset queries which devices have the specified role for
394 * the specified capture preset
395 * @param audioSource the capture preset to query
396 * @param role the role of the devices to query
397 * @param devices returns list of devices with matching role for the specified capture preset.
398 * DEVICE_ROLE_NONE is invalid as input.
399 * @return BAD_VALUE if the capture preset or role is invalid,
400 * or NAME_NOT_FOUND if no device for the role and capture preset was set
401 * or NO_ERROR if the devices parameter contains a list of devices
402 */
403 virtual status_t getDevicesForRoleAndCapturePreset(audio_source_t audioSource,
404 device_role_t role, AudioDeviceTypeAddrVector &devices) const = 0;
405
jiabin415af492020-09-17 14:31:20 -0700406 /**
407 * @brief getActiveMediaDevices returns which devices will most likely to be used for media
408 * @param availableDevices all available devices
409 * @return collection of active devices
410 */
411 virtual DeviceVector getActiveMediaDevices(const DeviceVector& availableDevices) const = 0;
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700412
François Gaffiedc7553f2018-11-02 10:39:57 +0100413 virtual void dump(String8 *dst) const = 0;
414
François Gaffie2110e042015-03-24 08:41:51 +0100415protected:
Mikhail Naganov47835552019-05-14 10:32:51 -0700416 virtual ~EngineInterface() {}
François Gaffie2110e042015-03-24 08:41:51 +0100417};
418
Mikhail Naganov47835552019-05-14 10:32:51 -0700419__attribute__((visibility("default")))
420extern "C" EngineInterface* createEngineInstance();
421
422__attribute__((visibility("default")))
423extern "C" void destroyEngineInstance(EngineInterface *engine);
424
Mikhail Naganov1b2a7942017-12-08 10:18:09 -0800425} // namespace android