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 <EngineConfig.h> 20 #include <EngineInterface.h> 21 #include <ProductStrategy.h> 22 #include <VolumeGroup.h> 23 #include <LastRemovableMediaDevices.h> 24 25 namespace android { 26 namespace audio_policy { 27 28 class EngineBase : public EngineInterface 29 { 30 public: 31 /// 32 /// from EngineInterface 33 /// 34 android::status_t initCheck() override; 35 36 void setObserver(AudioPolicyManagerObserver *observer) override; 37 38 status_t setPhoneState(audio_mode_t mode) override; 39 getPhoneState()40 audio_mode_t getPhoneState() const override { return mPhoneState; } 41 setForceUse(audio_policy_force_use_t usage,audio_policy_forced_cfg_t config)42 status_t setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config) override 43 { 44 mForceUse[usage] = config; 45 return NO_ERROR; 46 } 47 getForceUse(audio_policy_force_use_t usage)48 audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage) const override 49 { 50 return mForceUse[usage]; 51 } 52 android::status_t setDeviceConnectionState(const sp<DeviceDescriptor> /*devDesc*/, 53 audio_policy_dev_state_t /*state*/) override; 54 55 product_strategy_t getProductStrategyForAttributes( 56 const audio_attributes_t &attr, bool fallbackOnDefault = true) const override; 57 58 audio_stream_type_t getStreamTypeForAttributes(const audio_attributes_t &attr) const override; 59 60 audio_attributes_t getAttributesForStreamType(audio_stream_type_t stream) const override; 61 62 StreamTypeVector getStreamTypesForProductStrategy(product_strategy_t ps) const override; 63 64 AttributesVector getAllAttributesForProductStrategy(product_strategy_t ps) const override; 65 66 StrategyVector getOrderedProductStrategies() const override; 67 68 status_t listAudioProductStrategies(AudioProductStrategyVector &strategies) const override; 69 70 VolumeCurves *getVolumeCurvesForAttributes(const audio_attributes_t &attr) const override; 71 72 VolumeCurves *getVolumeCurvesForStreamType(audio_stream_type_t stream) const override; 73 getVolumeCurvesForVolumeGroup(volume_group_t group)74 IVolumeCurves *getVolumeCurvesForVolumeGroup(volume_group_t group) const override 75 { 76 return mVolumeGroups.find(group) != end(mVolumeGroups) ? 77 mVolumeGroups.at(group)->getVolumeCurves() : nullptr; 78 } 79 80 VolumeGroupVector getVolumeGroups() const override; 81 82 volume_group_t getVolumeGroupForAttributes( 83 const audio_attributes_t &attr, bool fallbackOnDefault = true) const override; 84 85 volume_group_t getVolumeGroupForStreamType( 86 audio_stream_type_t stream, bool fallbackOnDefault = true) const override; 87 88 status_t listAudioVolumeGroups(AudioVolumeGroupVector &groups) const override; 89 90 std::vector<audio_devices_t> getLastRemovableMediaDevices( 91 device_out_group_t group = GROUP_NONE) const 92 { 93 return mLastRemovableMediaDevices.getLastRemovableMediaDevices(group); 94 } 95 96 void dump(String8 *dst) const override; 97 98 status_t setDevicesRoleForStrategy(product_strategy_t strategy, device_role_t role, 99 const AudioDeviceTypeAddrVector &devices) override; 100 101 status_t removeDevicesRoleForStrategy(product_strategy_t strategy, device_role_t role) override; 102 103 status_t getDevicesForRoleAndStrategy(product_strategy_t strategy, device_role_t role, 104 AudioDeviceTypeAddrVector &devices) const override; 105 106 engineConfig::ParsingResult loadAudioPolicyEngineConfig(); 107 getProductStrategies()108 const ProductStrategyMap &getProductStrategies() const { return mProductStrategies; } 109 getProductStrategies()110 ProductStrategyMap &getProductStrategies() { return mProductStrategies; } 111 112 product_strategy_t getProductStrategyForStream(audio_stream_type_t stream) const; 113 114 product_strategy_t getProductStrategyByName(const std::string &name) const; 115 getApmObserver()116 AudioPolicyManagerObserver *getApmObserver() const { return mApmObserver; } 117 isInCall()118 inline bool isInCall() const 119 { 120 return is_state_in_call(getPhoneState()); 121 } 122 toVolumeSource(audio_stream_type_t stream)123 VolumeSource toVolumeSource(audio_stream_type_t stream) const 124 { 125 return static_cast<VolumeSource>(getVolumeGroupForStreamType(stream)); 126 } 127 128 status_t switchVolumeCurve(audio_stream_type_t streamSrc, audio_stream_type_t streamDst); 129 130 status_t restoreOriginVolumeCurve(audio_stream_type_t stream); 131 132 status_t setDevicesRoleForCapturePreset(audio_source_t audioSource, device_role_t role, 133 const AudioDeviceTypeAddrVector &devices) override; 134 135 status_t addDevicesRoleForCapturePreset(audio_source_t audioSource, device_role_t role, 136 const AudioDeviceTypeAddrVector &devices) override; 137 138 /** 139 * Remove devices role for capture preset. When `forceMatched` is true, the devices to be 140 * removed must all show as role for the capture preset. Otherwise, only devices that has shown 141 * as role for the capture preset will be remove. 142 */ 143 status_t doRemoveDevicesRoleForCapturePreset(audio_source_t audioSource, 144 device_role_t role, const AudioDeviceTypeAddrVector& devices, 145 bool forceMatched=true); 146 147 status_t removeDevicesRoleForCapturePreset(audio_source_t audioSource, 148 device_role_t role, const AudioDeviceTypeAddrVector& devices) override; 149 150 status_t clearDevicesRoleForCapturePreset(audio_source_t audioSource, 151 device_role_t role) override; 152 153 status_t getDevicesForRoleAndCapturePreset(audio_source_t audioSource, 154 device_role_t role, AudioDeviceTypeAddrVector &devices) const override; 155 156 DeviceVector getActiveMediaDevices(const DeviceVector& availableDevices) const override; 157 158 private: 159 /** 160 * Get media devices as the given role 161 * 162 * @param role the audio devices role 163 * @param availableDevices all available devices 164 * @param devices the DeviceVector to store devices as the given role 165 * @return NO_ERROR if all devices associated to the given role are present in available devices 166 * NAME_NO_FOUND if there is no strategy for media or there are no devices associate to 167 * the given role 168 * NOT_ENOUGH_DATA if not all devices as given role are present in available devices 169 */ 170 status_t getMediaDevicesForRole(device_role_t role, const DeviceVector& availableDevices, 171 DeviceVector& devices) const; 172 173 void dumpCapturePresetDevicesRoleMap(String8 *dst, int spaces) const; 174 175 AudioPolicyManagerObserver *mApmObserver = nullptr; 176 177 ProductStrategyMap mProductStrategies; 178 ProductStrategyDevicesRoleMap mProductStrategyDeviceRoleMap; 179 CapturePresetDevicesRoleMap mCapturePresetDevicesRoleMap; 180 VolumeGroupMap mVolumeGroups; 181 LastRemovableMediaDevices mLastRemovableMediaDevices; 182 audio_mode_t mPhoneState = AUDIO_MODE_NORMAL; /**< current phone state. */ 183 184 /** current forced use configuration. */ 185 audio_policy_forced_cfg_t mForceUse[AUDIO_POLICY_FORCE_USE_CNT] = {}; 186 }; 187 188 } // namespace audio_policy 189 } // namespace android 190