1 /* 2 * Copyright (C) 2018 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 <functional> 20 21 #include <android/media/audio/common/AudioHalEngineConfig.h> 22 #include <EngineConfig.h> 23 #include <EngineInterface.h> 24 #include <ProductStrategy.h> 25 #include <VolumeGroup.h> 26 #include <LastRemovableMediaDevices.h> 27 28 namespace android { 29 namespace audio_policy { 30 31 class EngineBase : public EngineInterface 32 { 33 public: 34 /// 35 /// from EngineInterface 36 /// 37 android::status_t initCheck() override; 38 39 void setObserver(AudioPolicyManagerObserver *observer) override; 40 41 status_t setPhoneState(audio_mode_t mode) override; 42 getPhoneState()43 audio_mode_t getPhoneState() const override { return mPhoneState; } 44 setForceUse(audio_policy_force_use_t usage,audio_policy_forced_cfg_t config)45 status_t setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config) override 46 { 47 mForceUse[usage] = config; 48 return NO_ERROR; 49 } 50 getForceUse(audio_policy_force_use_t usage)51 audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage) const override 52 { 53 return mForceUse[usage]; 54 } 55 android::status_t setDeviceConnectionState(const sp<DeviceDescriptor> /*devDesc*/, 56 audio_policy_dev_state_t /*state*/) override; 57 58 product_strategy_t getProductStrategyForAttributes( 59 const audio_attributes_t &attr, bool fallbackOnDefault = true) const override; 60 61 audio_stream_type_t getStreamTypeForAttributes(const audio_attributes_t &attr) const override; 62 63 audio_attributes_t getAttributesForStreamType(audio_stream_type_t stream) const override; 64 65 StreamTypeVector getStreamTypesForProductStrategy(product_strategy_t ps) const override; 66 67 AttributesVector getAllAttributesForProductStrategy(product_strategy_t ps) const override; 68 69 StrategyVector getOrderedProductStrategies() const override; 70 71 status_t listAudioProductStrategies(AudioProductStrategyVector &strategies) const override; 72 73 VolumeCurves *getVolumeCurvesForAttributes(const audio_attributes_t &attr) const override; 74 75 VolumeCurves *getVolumeCurvesForStreamType(audio_stream_type_t stream) const override; 76 getVolumeCurvesForVolumeGroup(volume_group_t group)77 IVolumeCurves *getVolumeCurvesForVolumeGroup(volume_group_t group) const override 78 { 79 return mVolumeGroups.find(group) != end(mVolumeGroups) ? 80 mVolumeGroups.at(group)->getVolumeCurves() : nullptr; 81 } 82 83 VolumeGroupVector getVolumeGroups() const override; 84 85 volume_group_t getVolumeGroupForAttributes( 86 const audio_attributes_t &attr, bool fallbackOnDefault = true) const override; 87 88 volume_group_t getVolumeGroupForStreamType( 89 audio_stream_type_t stream, bool fallbackOnDefault = true) const override; 90 91 status_t listAudioVolumeGroups(AudioVolumeGroupVector &groups) const override; 92 93 /** 94 * Get the list of currently connected removable device types ordered from most recently 95 * connected to least recently connected. 96 * @param group the device group to consider: wired, a2dp... If none, consider all groups. 97 * @param excludedDevices list of device types to ignore 98 * @return a potentially empty ordered list of connected removable devices. 99 */ 100 std::vector<audio_devices_t> getLastRemovableMediaDevices( 101 device_out_group_t group = GROUP_NONE, 102 std::vector<audio_devices_t> excludedDevices = {}) const { 103 return mLastRemovableMediaDevices.getLastRemovableMediaDevices(group, excludedDevices); 104 } 105 106 void dump(String8 *dst) const override; 107 108 status_t setDevicesRoleForStrategy(product_strategy_t strategy, device_role_t role, 109 const AudioDeviceTypeAddrVector &devices) override; 110 111 status_t removeDevicesRoleForStrategy(product_strategy_t strategy, device_role_t role, 112 const AudioDeviceTypeAddrVector &devices) override; 113 114 status_t clearDevicesRoleForStrategy(product_strategy_t strategy, device_role_t role) override; 115 116 status_t getDevicesForRoleAndStrategy(product_strategy_t strategy, device_role_t role, 117 AudioDeviceTypeAddrVector &devices) const override; 118 119 engineConfig::ParsingResult loadAudioPolicyEngineConfig( 120 const media::audio::common::AudioHalEngineConfig& aidlConfig, bool); 121 122 engineConfig::ParsingResult loadAudioPolicyEngineConfig( 123 const std::string& xmlFilePath = "", bool isConfigurable = false); 124 getProductStrategies()125 const ProductStrategyMap &getProductStrategies() const { return mProductStrategies; } 126 getProductStrategies()127 ProductStrategyMap &getProductStrategies() { return mProductStrategies; } 128 129 product_strategy_t getProductStrategyForStream(audio_stream_type_t stream) const; 130 131 product_strategy_t getProductStrategyByName(const std::string &name) const; 132 133 std::string getProductStrategyName(product_strategy_t id) const; 134 getApmObserver()135 AudioPolicyManagerObserver *getApmObserver() const { return mApmObserver; } 136 isInCall()137 inline bool isInCall() const 138 { 139 return is_state_in_call(getPhoneState()); 140 } 141 toVolumeSource(audio_stream_type_t stream)142 VolumeSource toVolumeSource(audio_stream_type_t stream) const 143 { 144 return static_cast<VolumeSource>(getVolumeGroupForStreamType(stream)); 145 } 146 147 status_t switchVolumeCurve(audio_stream_type_t streamSrc, audio_stream_type_t streamDst); 148 149 status_t restoreOriginVolumeCurve(audio_stream_type_t stream); 150 151 status_t setDevicesRoleForCapturePreset(audio_source_t audioSource, device_role_t role, 152 const AudioDeviceTypeAddrVector &devices) override; 153 154 status_t addDevicesRoleForCapturePreset(audio_source_t audioSource, device_role_t role, 155 const AudioDeviceTypeAddrVector &devices) override; 156 157 /** 158 * Remove devices role for capture preset. When `forceMatched` is true, the devices to be 159 * removed must all show as role for the capture preset. Otherwise, only devices that has shown 160 * as role for the capture preset will be remove. 161 */ 162 status_t doRemoveDevicesRoleForCapturePreset(audio_source_t audioSource, 163 device_role_t role, const AudioDeviceTypeAddrVector& devices, 164 bool forceMatched=true); 165 166 status_t removeDevicesRoleForCapturePreset(audio_source_t audioSource, 167 device_role_t role, const AudioDeviceTypeAddrVector& devices) override; 168 169 status_t clearDevicesRoleForCapturePreset(audio_source_t audioSource, 170 device_role_t role) override; 171 172 status_t getDevicesForRoleAndCapturePreset(audio_source_t audioSource, 173 device_role_t role, AudioDeviceTypeAddrVector &devices) const override; 174 175 DeviceVector getActiveMediaDevices(const DeviceVector& availableDevices) const override; 176 177 void initializeDeviceSelectionCache() override; 178 179 void updateDeviceSelectionCache() override; 180 181 protected: 182 DeviceVector getPreferredAvailableDevicesForProductStrategy( 183 const DeviceVector& availableOutputDevices, product_strategy_t strategy) const; 184 DeviceVector getDisabledDevicesForProductStrategy( 185 const DeviceVector& availableOutputDevices, product_strategy_t strategy) const; 186 187 private: 188 engineConfig::ParsingResult processParsingResult(engineConfig::ParsingResult&& rawResult); 189 190 /** 191 * Get media devices as the given role 192 * 193 * @param role the audio devices role 194 * @param availableDevices all available devices 195 * @param devices the DeviceVector to store devices as the given role 196 * @return NO_ERROR if all devices associated to the given role are present in available devices 197 * NAME_NO_FOUND if there is no strategy for media or there are no devices associate to 198 * the given role 199 * NOT_ENOUGH_DATA if not all devices as given role are present in available devices 200 */ 201 status_t getMediaDevicesForRole(device_role_t role, const DeviceVector& availableDevices, 202 DeviceVector& devices) const; 203 204 void dumpCapturePresetDevicesRoleMap(String8 *dst, int spaces) const; 205 206 AudioPolicyManagerObserver *mApmObserver = nullptr; 207 208 ProductStrategyMap mProductStrategies; 209 ProductStrategyDevicesRoleMap mProductStrategyDeviceRoleMap; 210 CapturePresetDevicesRoleMap mCapturePresetDevicesRoleMap; 211 VolumeGroupMap mVolumeGroups; 212 LastRemovableMediaDevices mLastRemovableMediaDevices; 213 audio_mode_t mPhoneState = AUDIO_MODE_NORMAL; /**< current phone state. */ 214 215 /** current forced use configuration. */ 216 audio_policy_forced_cfg_t mForceUse[AUDIO_POLICY_FORCE_USE_CNT] = {}; 217 218 protected: 219 /** 220 * Set the device information for a given strategy. 221 * 222 * @param strategy the strategy to set devices information 223 * @param devices the devices selected for the strategy 224 */ setStrategyDevices(const sp<ProductStrategy> &,const DeviceVector &)225 virtual void setStrategyDevices(const sp<ProductStrategy>& /*strategy*/, 226 const DeviceVector& /*devices*/) { 227 // In EngineBase, do nothing. It is up to the actual engine to decide if it is needed to 228 // set devices information for the given strategy. 229 } 230 231 /** 232 * Get devices that will be used for the given product strategy. 233 * 234 * @param strategy the strategy to query 235 */ 236 virtual DeviceVector getDevicesForProductStrategy(product_strategy_t strategy) const = 0; 237 238 DeviceStrategyMap mDevicesForStrategies; 239 }; 240 241 } // namespace audio_policy 242 } // namespace android 243