1 /* 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 <string> 20 #include <utility> 21 22 #include <AudioPolicyManagerObserver.h> 23 #include <android/media/audio/common/AudioHalEngineConfig.h> 24 #include <media/AudioProductStrategy.h> 25 #include <media/AudioVolumeGroup.h> 26 #include <IVolumeCurves.h> 27 #include <policy.h> 28 #include <Volume.h> 29 #include <HwModule.h> 30 #include <DeviceDescriptor.h> 31 #include <system/audio.h> 32 #include <system/audio_policy.h> 33 #include <utils/Errors.h> 34 #include <utils/Vector.h> 35 36 namespace android { 37 38 using DeviceStrategyMap = std::map<product_strategy_t, DeviceVector>; 39 using StrategyVector = std::vector<product_strategy_t>; 40 using VolumeGroupVector = std::vector<volume_group_t>; 41 using CapturePresetDevicesRoleMap = 42 std::map<std::pair<audio_source_t, device_role_t>, AudioDeviceTypeAddrVector>; 43 44 /** 45 * This interface is dedicated to the policy manager that a Policy Engine shall implement. 46 */ 47 class EngineInterface 48 { 49 public: 50 /** 51 * Loads the engine configuration from AIDL configuration data. 52 * If loading failed, tries to fall back to some default configuration. If fallback 53 * is impossible, returns an error. 54 */ 55 virtual status_t loadFromHalConfigWithFallback( 56 const media::audio::common::AudioHalEngineConfig& config) = 0; 57 58 /** 59 * Loads the engine configuration from the specified or the default config file. 60 * If loading failed, tries to fall back to some default configuration. If fallback 61 * is impossible, returns an error. 62 */ 63 virtual status_t loadFromXmlConfigWithFallback(const std::string& xmlFilePath = "") = 0; 64 65 /** 66 * Checks if the engine was correctly initialized. 67 * 68 * @return NO_ERROR if initialization has been done correctly, error code otherwise.. 69 */ 70 virtual status_t initCheck() = 0; 71 72 /** 73 * Sets the Manager observer that allows the engine to retrieve information on collection 74 * of devices, streams, HwModules, ... 75 * 76 * @param[in] observer handle on the manager. 77 */ 78 virtual void setObserver(AudioPolicyManagerObserver *observer) = 0; 79 80 /** 81 * Set the Telephony Mode. 82 * 83 * @param[in] mode: Android Phone state (normal, ringtone, csv, in communication) 84 * 85 * @return NO_ERROR if Telephony Mode set correctly, error code otherwise. 86 */ 87 virtual status_t setPhoneState(audio_mode_t mode) = 0; 88 89 /** 90 * Get the telephony Mode 91 * 92 * @return the current telephony mode 93 */ 94 virtual audio_mode_t getPhoneState() const = 0; 95 96 /** 97 * Set Force Use config for a given usage. 98 * 99 * @param[in] usage for which a configuration shall be forced. 100 * @param[in] config wished to be forced for the given usage. 101 * 102 * @return NO_ERROR if the Force Use config was set correctly, error code otherwise (e.g. config 103 * not allowed a given usage...) 104 */ 105 virtual status_t setForceUse(audio_policy_force_use_t usage, 106 audio_policy_forced_cfg_t config) = 0; 107 108 /** 109 * Get Force Use config for a given usage. 110 * 111 * @param[in] usage for which a configuration shall be forced. 112 * 113 * @return config wished to be forced for the given usage. 114 */ 115 virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage) const = 0; 116 117 /** 118 * Set the connection state of device(s). 119 * 120 * @param[in] devDesc for which the state has changed. 121 * @param[in] state of availability of this(these) device(s). 122 * 123 * @return NO_ERROR if devices criterion updated correctly, error code otherwise. 124 */ 125 virtual status_t setDeviceConnectionState(const android::sp<android::DeviceDescriptor> devDesc, 126 audio_policy_dev_state_t state) = 0; 127 128 /** 129 * Get the strategy selected for a given audio attributes. 130 * 131 * @param[in] audio attributes to get the selected @product_strategy_t followed by. 132 * @param fallbackOnDefault if true, will return the fallback strategy if the attributes 133 * are not explicitly assigned to a given strategy. 134 * @return @product_strategy_t to be followed. 135 */ 136 virtual product_strategy_t getProductStrategyForAttributes( 137 const audio_attributes_t &attr, bool fallbackOnDefault = true) const = 0; 138 139 /** 140 * @brief getOutputDevicesForAttributes retrieves the devices to be used for given 141 * audio attributes. 142 * @param attributes of the output requesting Device(s) selection 143 * @param preferedDevice valid reference if a prefered device is requested, nullptr otherwise. 144 * @param fromCache if true, the device is returned from internal cache, 145 * otherwise it is determined by current state (device connected,phone state, 146 * force use, a2dp output...) 147 * @return vector of selected device descriptors. 148 * Appropriate device for streams handled by the specified audio attributes according 149 * to current phone state, forced states, connected devices... 150 * if fromCache is true, the device is returned from internal cache, 151 * otherwise it is determined by current state (device connected,phone state, force use, 152 * a2dp output...) 153 * This allows to: 154 * 1 speed up process when the state is stable (when starting or stopping an output) 155 * 2 access to either current device selection (fromCache == true) or 156 * "future" device selection (fromCache == false) when called from a context 157 * where conditions are changing (setDeviceConnectionState(), setPhoneState()...) AND 158 * before manager updates its outputs. 159 */ 160 virtual DeviceVector getOutputDevicesForAttributes( 161 const audio_attributes_t &attributes, 162 const sp<DeviceDescriptor> &preferedDevice = nullptr, 163 bool fromCache = false) const = 0; 164 165 /** 166 * @brief getOutputDevicesForStream Legacy function retrieving devices from a stream type. 167 * @param stream type of the output requesting Device(s) selection 168 * @param fromCache if true, the device is returned from internal cache, 169 * otherwise it is determined by current state (device connected,phone state, 170 * force use, a2dp output...) 171 * @return appropriate device for streams handled by the specified audio attributes according 172 * to current phone state, forced states, connected devices... 173 * if fromCache is true, the device is returned from internal cache, 174 * otherwise it is determined by current state (device connected,phone state, force use, 175 * a2dp output...) 176 * This allows to: 177 * 1 speed up process when the state is stable (when starting or stopping an output) 178 * 2 access to either current device selection (fromCache == true) or 179 * "future" device selection (fromCache == false) when called from a context 180 * where conditions are changing (setDeviceConnectionState(), setPhoneState()...) AND 181 * before manager updates its outputs. 182 */ 183 virtual DeviceVector getOutputDevicesForStream(audio_stream_type_t stream, 184 bool fromCache = false) const = 0; 185 186 /** 187 * Get the input device selected for given audio attributes. 188 * 189 * @param[in] attr audio attributes to consider 190 * @param[out] mix to be used if a mix has been installed for the given audio attributes. 191 * @return selected input device for the audio attributes, may be null if error. 192 */ 193 virtual sp<DeviceDescriptor> getInputDeviceForAttributes( 194 const audio_attributes_t &attr, 195 uid_t uid = 0, 196 audio_session_t session = AUDIO_SESSION_NONE, 197 sp<AudioPolicyMix> *mix = nullptr) const = 0; 198 199 /** 200 * Get the legacy stream type for a given audio attributes. 201 * 202 * @param[in] audio attributes to get the associated audio_stream_type_t. 203 * 204 * @return audio_stream_type_t associated to the attributes. 205 */ 206 virtual audio_stream_type_t getStreamTypeForAttributes( 207 const audio_attributes_t &attr) const = 0; 208 209 /** 210 * @brief getAttributesForStream get the audio attributes from legacy stream type 211 * Attributes returned might only be used to check upon routing decision, not volume decisions. 212 * @param stream to consider 213 * @return audio attributes matching the legacy stream type 214 */ 215 virtual audio_attributes_t getAttributesForStreamType(audio_stream_type_t stream) const = 0; 216 217 /** 218 * @brief getStreamTypesForProductStrategy retrieves the list of legacy stream type following 219 * the given product strategy 220 * @param ps product strategy to consider 221 * @return associated legacy Stream Types vector of the given product strategy 222 */ 223 virtual StreamTypeVector getStreamTypesForProductStrategy(product_strategy_t ps) const = 0; 224 225 /** 226 * @brief getAllAttributesForProductStrategy retrieves all the attributes following the given 227 * product strategy. Any attributes that "matches" with this one will follow the product 228 * strategy. 229 * "matching" means the usage shall match if reference attributes has a defined usage, AND 230 * content type shall match if reference attributes has a defined content type AND 231 * flags shall match if reference attributes has defined flags AND 232 * tags shall match if reference attributes has defined tags. 233 * @param ps product strategy to consider 234 * @return vector of product strategy ids, empty if unknown strategy. 235 */ 236 virtual AttributesVector getAllAttributesForProductStrategy(product_strategy_t ps) const = 0; 237 238 /** 239 * @brief getOrderedAudioProductStrategies 240 * @return priority ordered product strategies to help the AudioPolicyManager evaluating the 241 * device selection per output according to the prioritized strategies. 242 */ 243 virtual StrategyVector getOrderedProductStrategies() const = 0; 244 245 /** 246 * @brief updateDeviceSelectionCache. Device selection for AudioAttribute / Streams is cached 247 * in the engine in order to speed up process when the audio system is stable. 248 * When a device is connected, the android mode is changed, engine is notified and can update 249 * the cache. 250 * When starting / stopping an output with a stream that can affect notification, the engine 251 * needs to update the cache upon this function call. 252 */ 253 virtual void updateDeviceSelectionCache() = 0; 254 255 /** 256 * @brief listAudioProductStrategies. Introspection API to retrieve a collection of 257 * AudioProductStrategyVector that allows to build AudioAttributes according to a 258 * product_strategy which is just an index. It has also a human readable name to help the 259 * Car/Oem/AudioManager identiying the use case. 260 * @param strategies collection. 261 * @return OK if the list has been retrieved, error code otherwise 262 */ 263 virtual status_t listAudioProductStrategies(AudioProductStrategyVector &strategies) const = 0; 264 265 /** 266 * @brief getVolumeCurvesForAttributes retrieves the Volume Curves interface for the 267 * requested Audio Attributes. 268 * @param attr to be considered 269 * @return IVolumeCurves interface pointer if found, nullptr otherwise 270 */ 271 virtual IVolumeCurves *getVolumeCurvesForAttributes(const audio_attributes_t &attr) const = 0; 272 273 /** 274 * @brief getVolumeCurvesForStreamType retrieves the Volume Curves interface for the stream 275 * @param stream to be considered 276 * @return IVolumeCurves interface pointer if found, nullptr otherwise 277 */ 278 virtual IVolumeCurves *getVolumeCurvesForStreamType(audio_stream_type_t stream) const = 0; 279 280 /** 281 * @brief getVolumeCurvesForVolumeGroup retrieves the Volume Curves interface for volume group 282 * @param group to be considered 283 * @return IVolumeCurves interface pointer if found, nullptr otherwise 284 */ 285 virtual IVolumeCurves *getVolumeCurvesForVolumeGroup(volume_group_t group) const = 0; 286 287 /** 288 * @brief getVolumeGroups retrieves the collection of volume groups. 289 * @return vector of volume groups 290 */ 291 virtual VolumeGroupVector getVolumeGroups() const = 0; 292 293 /** 294 * @brief getVolumeGroupForAttributes gets the appropriate volume group to be used for a given 295 * Audio Attributes. 296 * @param attr to be considered 297 * @param fallbackOnDefault if true, will return the fallback volume group if the attributes 298 * are not associated to any volume group. 299 * @return volume group associated to the given audio attributes, default group if none 300 * applicable, VOLUME_GROUP_NONE if no default group defined. 301 */ 302 virtual volume_group_t getVolumeGroupForAttributes( 303 const audio_attributes_t &attr, bool fallbackOnDefault = true) const = 0; 304 305 /** 306 * @brief getVolumeGroupForStreamType gets the appropriate volume group to be used for a given 307 * legacy stream type 308 * @param stream type to be considered 309 * @param fallbackOnDefault if true, will return the fallback volume group if the stream type 310 * is not associated to any volume group. 311 * @return volume group associated to the given stream type, default group if none applicable, 312 * VOLUME_GROUP_NONE if no default group defined. 313 */ 314 virtual volume_group_t getVolumeGroupForStreamType( 315 audio_stream_type_t stream, bool fallbackOnDefault = true) const = 0; 316 317 /** 318 * @brief listAudioVolumeGroups introspection API to get the Audio Volume Groups, aka 319 * former stream aliases in Audio Service, defining volume curves attached to one or more 320 * Audio Attributes. 321 * @param groups 322 * @return NO_ERROR if the volume groups were retrieved successfully, error code otherwise 323 */ 324 virtual status_t listAudioVolumeGroups(AudioVolumeGroupVector &groups) const = 0; 325 326 /** 327 * @brief setDevicesRoleForStrategy sets devices role for a strategy when available. To remove 328 * devices role, removeDevicesRoleForStrategy must be called. When devices role is set 329 * successfully, previously set devices for the same role and strategy will be removed. 330 * @param strategy the audio strategy whose routing will be affected 331 * @param role the role of the devices for the strategy. All device roles are defined at 332 * system/media/audio/include/system/audio_policy.h. DEVICE_ROLE_NONE is invalid 333 * for setting. 334 * @param devices the audio devices to be set 335 * @return BAD_VALUE if the strategy or role is invalid, 336 * or NO_ERROR if the role of the devices for strategy was set 337 */ 338 virtual status_t setDevicesRoleForStrategy(product_strategy_t strategy, device_role_t role, 339 const AudioDeviceTypeAddrVector &devices) = 0; 340 341 /** 342 * @brief removeDevicesRoleForStrategy removes the role of device(s) previously set 343 * for the given strategy 344 * @param strategy the audio strategy whose routing will be affected 345 * @param role the role of the devices for strategy 346 * @param devices the audio devices to be removed 347 * @return BAD_VALUE if the strategy or role is invalid, 348 * or NO_ERROR if the devices for this role was removed 349 */ 350 virtual status_t removeDevicesRoleForStrategy(product_strategy_t strategy, device_role_t role, 351 const AudioDeviceTypeAddrVector &devices) = 0; 352 353 /** 354 * @brief clearDevicesRoleForStrategy removes the role of all devices previously set 355 * for the given strategy 356 * @param strategy the audio strategy whose routing will be affected 357 * @param role the role of the devices for strategy 358 * @return BAD_VALUE if the strategy or role is invalid, 359 * or NO_ERROR if the devices for this role was removed 360 */ 361 virtual status_t clearDevicesRoleForStrategy(product_strategy_t strategy, 362 device_role_t role) = 0; 363 364 /** 365 * @brief getDevicesForRoleAndStrategy queries which devices have the specified role for the 366 * specified strategy 367 * @param strategy the strategy to query 368 * @param role the role of the devices to query 369 * @param devices returns list of devices with matching role for the specified strategy. 370 * DEVICE_ROLE_NONE is invalid as input. 371 * @return BAD_VALUE if the strategy or role is invalid, 372 * or NAME_NOT_FOUND if no device for the role and strategy was set 373 * or NO_ERROR if the devices parameter contains a list of devices 374 */ 375 virtual status_t getDevicesForRoleAndStrategy(product_strategy_t strategy, device_role_t role, 376 AudioDeviceTypeAddrVector &devices) const = 0; 377 378 /** 379 * @brief setDevicesRoleForCapturePreset sets devices role for a capture preset when available. 380 * To remove devices role, removeDevicesRoleForCapturePreset must be called. Calling 381 * clearDevicesRoleForCapturePreset will remove all devices as role. When devices role is set 382 * successfully, previously set devices for the same role and capture preset will be removed. 383 * @param audioSource the audio capture preset whose routing will be affected 384 * @param role the role of the devices for the capture preset. All device roles are defined at 385 * system/media/audio/include/system/audio_policy.h. DEVICE_ROLE_NONE is invalid 386 * for setting. 387 * @param devices the audio devices to be set 388 * @return BAD_VALUE if the capture preset or role is invalid, 389 * or NO_ERROR if the role of the devices for capture preset was set 390 */ 391 virtual status_t setDevicesRoleForCapturePreset(audio_source_t audioSource, device_role_t role, 392 const AudioDeviceTypeAddrVector &devices) = 0; 393 394 /** 395 * @brief addDevicesRoleForCapturePreset adds devices role for a capture preset when available. 396 * To remove devices role, removeDevicesRoleForCapturePreset must be called. Calling 397 * clearDevicesRoleForCapturePreset will remove all devices as role. 398 * @param audioSource the audio capture preset whose routing will be affected 399 * @param role the role of the devices for the capture preset. All device roles are defined at 400 * system/media/audio/include/system/audio_policy.h. DEVICE_ROLE_NONE is invalid 401 * for setting. 402 * @param devices the audio devices to be added 403 * @return BAD_VALUE if the capture preset or role is invalid, 404 * or NO_ERROR if the role of the devices for capture preset was added 405 */ 406 virtual status_t addDevicesRoleForCapturePreset(audio_source_t audioSource, device_role_t role, 407 const AudioDeviceTypeAddrVector &devices) = 0; 408 409 /** 410 * @brief removeDevicesRoleForCapturePreset removes the role of device(s) previously set 411 * for the given capture preset 412 * @param audioSource the audio capture preset whose routing will be affected 413 * @param role the role of the devices for the capture preset 414 * @param devices the devices to be removed 415 * @return BAD_VALUE if 1) the capture preset is invalid, 2) role is invalid or 3) the list of 416 * devices to be removed are not all present as role for a capture preset 417 * or NO_ERROR if the devices for this role was removed 418 */ 419 virtual status_t removeDevicesRoleForCapturePreset(audio_source_t audioSource, 420 device_role_t role, const AudioDeviceTypeAddrVector& devices) = 0; 421 422 /** 423 * @brief clearDevicesRoleForCapturePreset removes the role of all device(s) previously set 424 * for the given capture preset 425 * @param audioSource the audio capture preset whose routing will be affected 426 * @param role the role of the devices for the capture preset 427 * @return BAD_VALUE if the capture preset or role is invalid, 428 * or NO_ERROR if the devices for this role was removed 429 */ 430 virtual status_t clearDevicesRoleForCapturePreset(audio_source_t audioSource, 431 device_role_t role); 432 433 /** 434 * @brief getDevicesForRoleAndCapturePreset queries which devices have the specified role for 435 * the specified capture preset 436 * @param audioSource the capture preset to query 437 * @param role the role of the devices to query 438 * @param devices returns list of devices with matching role for the specified capture preset. 439 * DEVICE_ROLE_NONE is invalid as input. 440 * @return BAD_VALUE if the capture preset or role is invalid, 441 * or NAME_NOT_FOUND if no device for the role and capture preset was set 442 * or NO_ERROR if the devices parameter contains a list of devices 443 */ 444 virtual status_t getDevicesForRoleAndCapturePreset(audio_source_t audioSource, 445 device_role_t role, AudioDeviceTypeAddrVector &devices) const = 0; 446 447 /** 448 * @brief getActiveMediaDevices returns which devices will most likely to be used for media 449 * @param availableDevices all available devices 450 * @return collection of active devices 451 */ 452 virtual DeviceVector getActiveMediaDevices(const DeviceVector& availableDevices) const = 0; 453 454 /** 455 * @brief initializeDeviceSelectionCache. Device selection for AudioAttribute / Streams is 456 * cached in the engine in order to speed up process when the audio system is stable. When the 457 * audio system is initializing, not all audio devices information will be available. In that 458 * case, calling this function can allow the engine to initialize the device selection cache 459 * with default values. 460 * This must only be called when audio policy manager is initializing. 461 */ 462 virtual void initializeDeviceSelectionCache() = 0; 463 464 virtual void dump(String8 *dst) const = 0; 465 466 protected: ~EngineInterface()467 virtual ~EngineInterface() {} 468 }; 469 470 __attribute__((visibility("default"))) 471 extern "C" EngineInterface* createEngineInstance(); 472 473 __attribute__((visibility("default"))) 474 extern "C" void destroyEngineInstance(EngineInterface *engine); 475 476 } // namespace android 477