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); 121 122 engineConfig::ParsingResult loadAudioPolicyEngineConfig(const std::string& xmlFilePath = ""); 123 getProductStrategies()124 const ProductStrategyMap &getProductStrategies() const { return mProductStrategies; } 125 getProductStrategies()126 ProductStrategyMap &getProductStrategies() { return mProductStrategies; } 127 128 product_strategy_t getProductStrategyForStream(audio_stream_type_t stream) const; 129 130 product_strategy_t getProductStrategyByName(const std::string &name) const; 131 getApmObserver()132 AudioPolicyManagerObserver *getApmObserver() const { return mApmObserver; } 133 isInCall()134 inline bool isInCall() const 135 { 136 return is_state_in_call(getPhoneState()); 137 } 138 toVolumeSource(audio_stream_type_t stream)139 VolumeSource toVolumeSource(audio_stream_type_t stream) const 140 { 141 return static_cast<VolumeSource>(getVolumeGroupForStreamType(stream)); 142 } 143 144 status_t switchVolumeCurve(audio_stream_type_t streamSrc, audio_stream_type_t streamDst); 145 146 status_t restoreOriginVolumeCurve(audio_stream_type_t stream); 147 148 status_t setDevicesRoleForCapturePreset(audio_source_t audioSource, device_role_t role, 149 const AudioDeviceTypeAddrVector &devices) override; 150 151 status_t addDevicesRoleForCapturePreset(audio_source_t audioSource, device_role_t role, 152 const AudioDeviceTypeAddrVector &devices) override; 153 154 /** 155 * Remove devices role for capture preset. When `forceMatched` is true, the devices to be 156 * removed must all show as role for the capture preset. Otherwise, only devices that has shown 157 * as role for the capture preset will be remove. 158 */ 159 status_t doRemoveDevicesRoleForCapturePreset(audio_source_t audioSource, 160 device_role_t role, const AudioDeviceTypeAddrVector& devices, 161 bool forceMatched=true); 162 163 status_t removeDevicesRoleForCapturePreset(audio_source_t audioSource, 164 device_role_t role, const AudioDeviceTypeAddrVector& devices) override; 165 166 status_t clearDevicesRoleForCapturePreset(audio_source_t audioSource, 167 device_role_t role) override; 168 169 status_t getDevicesForRoleAndCapturePreset(audio_source_t audioSource, 170 device_role_t role, AudioDeviceTypeAddrVector &devices) const override; 171 172 DeviceVector getActiveMediaDevices(const DeviceVector& availableDevices) const override; 173 174 void initializeDeviceSelectionCache() override; 175 176 void updateDeviceSelectionCache() override; 177 178 protected: 179 DeviceVector getPreferredAvailableDevicesForProductStrategy( 180 const DeviceVector& availableOutputDevices, product_strategy_t strategy) const; 181 DeviceVector getDisabledDevicesForProductStrategy( 182 const DeviceVector& availableOutputDevices, product_strategy_t strategy) const; 183 184 private: 185 engineConfig::ParsingResult processParsingResult(engineConfig::ParsingResult&& rawResult); 186 187 /** 188 * Get media devices as the given role 189 * 190 * @param role the audio devices role 191 * @param availableDevices all available devices 192 * @param devices the DeviceVector to store devices as the given role 193 * @return NO_ERROR if all devices associated to the given role are present in available devices 194 * NAME_NO_FOUND if there is no strategy for media or there are no devices associate to 195 * the given role 196 * NOT_ENOUGH_DATA if not all devices as given role are present in available devices 197 */ 198 status_t getMediaDevicesForRole(device_role_t role, const DeviceVector& availableDevices, 199 DeviceVector& devices) const; 200 201 void dumpCapturePresetDevicesRoleMap(String8 *dst, int spaces) const; 202 203 AudioPolicyManagerObserver *mApmObserver = nullptr; 204 205 ProductStrategyMap mProductStrategies; 206 ProductStrategyDevicesRoleMap mProductStrategyDeviceRoleMap; 207 CapturePresetDevicesRoleMap mCapturePresetDevicesRoleMap; 208 VolumeGroupMap mVolumeGroups; 209 LastRemovableMediaDevices mLastRemovableMediaDevices; 210 audio_mode_t mPhoneState = AUDIO_MODE_NORMAL; /**< current phone state. */ 211 212 /** current forced use configuration. */ 213 audio_policy_forced_cfg_t mForceUse[AUDIO_POLICY_FORCE_USE_CNT] = {}; 214 215 protected: 216 /** 217 * Set the device information for a given strategy. 218 * 219 * @param strategy the strategy to set devices information 220 * @param devices the devices selected for the strategy 221 */ setStrategyDevices(const sp<ProductStrategy> &,const DeviceVector &)222 virtual void setStrategyDevices(const sp<ProductStrategy>& /*strategy*/, 223 const DeviceVector& /*devices*/) { 224 // In EngineBase, do nothing. It is up to the actual engine to decide if it is needed to 225 // set devices information for the given strategy. 226 } 227 228 /** 229 * Get devices that will be used for the given product strategy. 230 * 231 * @param strategy the strategy to query 232 */ 233 virtual DeviceVector getDevicesForProductStrategy(product_strategy_t strategy) const = 0; 234 235 DeviceStrategyMap mDevicesForStrategies; 236 }; 237 238 } // namespace audio_policy 239 } // namespace android 240