François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 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 | #define LOG_TAG "APM::AudioPolicyEngine" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
| 20 | //#define VERY_VERBOSE_LOGGING |
| 21 | #ifdef VERY_VERBOSE_LOGGING |
| 22 | #define ALOGVV ALOGV |
| 23 | #else |
| 24 | #define ALOGVV(a...) do { } while(0) |
| 25 | #endif |
| 26 | |
| 27 | #include "Engine.h" |
| 28 | #include "Strategy.h" |
| 29 | #include "Stream.h" |
| 30 | #include "InputSource.h" |
| 31 | #include "Usage.h" |
| 32 | #include <policy.h> |
| 33 | #include <ParameterManagerWrapper.h> |
| 34 | |
| 35 | using std::string; |
| 36 | using std::map; |
| 37 | |
François Gaffie | f19cf79 | 2018-05-30 17:22:17 +0200 | [diff] [blame] | 38 | namespace android { |
| 39 | namespace audio_policy { |
| 40 | |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 41 | template <> |
| 42 | StrategyCollection &Engine::getCollection<routing_strategy>() |
| 43 | { |
| 44 | return mStrategyCollection; |
| 45 | } |
| 46 | template <> |
| 47 | StreamCollection &Engine::getCollection<audio_stream_type_t>() |
| 48 | { |
| 49 | return mStreamCollection; |
| 50 | } |
| 51 | template <> |
| 52 | UsageCollection &Engine::getCollection<audio_usage_t>() |
| 53 | { |
| 54 | return mUsageCollection; |
| 55 | } |
| 56 | template <> |
| 57 | InputSourceCollection &Engine::getCollection<audio_source_t>() |
| 58 | { |
| 59 | return mInputSourceCollection; |
| 60 | } |
| 61 | |
| 62 | template <> |
| 63 | const StrategyCollection &Engine::getCollection<routing_strategy>() const |
| 64 | { |
| 65 | return mStrategyCollection; |
| 66 | } |
| 67 | template <> |
| 68 | const StreamCollection &Engine::getCollection<audio_stream_type_t>() const |
| 69 | { |
| 70 | return mStreamCollection; |
| 71 | } |
| 72 | template <> |
| 73 | const UsageCollection &Engine::getCollection<audio_usage_t>() const |
| 74 | { |
| 75 | return mUsageCollection; |
| 76 | } |
| 77 | template <> |
| 78 | const InputSourceCollection &Engine::getCollection<audio_source_t>() const |
| 79 | { |
| 80 | return mInputSourceCollection; |
| 81 | } |
| 82 | |
| 83 | Engine::Engine() |
| 84 | : mManagerInterface(this), |
| 85 | mPluginInterface(this), |
| 86 | mPolicyParameterMgr(new ParameterManagerWrapper()), |
| 87 | mApmObserver(NULL) |
| 88 | { |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | Engine::~Engine() |
| 92 | { |
| 93 | mStrategyCollection.clear(); |
| 94 | mStreamCollection.clear(); |
| 95 | mInputSourceCollection.clear(); |
| 96 | mUsageCollection.clear(); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | void Engine::setObserver(AudioPolicyManagerObserver *observer) |
| 101 | { |
| 102 | ALOG_ASSERT(observer != NULL, "Invalid Audio Policy Manager observer"); |
| 103 | mApmObserver = observer; |
| 104 | } |
| 105 | |
| 106 | status_t Engine::initCheck() |
| 107 | { |
François Gaffie | 814ce80 | 2016-01-18 11:23:47 +0100 | [diff] [blame] | 108 | if (mPolicyParameterMgr == NULL || mPolicyParameterMgr->start() != NO_ERROR) { |
François Gaffie | 0f17ab7 | 2015-05-13 18:13:00 +0200 | [diff] [blame] | 109 | ALOGE("%s: could not start Policy PFW", __FUNCTION__); |
François Gaffie | 0f17ab7 | 2015-05-13 18:13:00 +0200 | [diff] [blame] | 110 | return NO_INIT; |
| 111 | } |
| 112 | return (mApmObserver != NULL)? NO_ERROR : NO_INIT; |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 113 | } |
| 114 | |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 115 | template <typename Key> |
| 116 | Element<Key> *Engine::getFromCollection(const Key &key) const |
| 117 | { |
| 118 | const Collection<Key> collection = getCollection<Key>(); |
| 119 | return collection.get(key); |
| 120 | } |
| 121 | |
| 122 | template <typename Key> |
| 123 | status_t Engine::add(const std::string &name, const Key &key) |
| 124 | { |
| 125 | Collection<Key> &collection = getCollection<Key>(); |
| 126 | return collection.add(name, key); |
| 127 | } |
| 128 | |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 129 | template <typename Property, typename Key> |
| 130 | Property Engine::getPropertyForKey(Key key) const |
| 131 | { |
| 132 | Element<Key> *element = getFromCollection<Key>(key); |
| 133 | if (element == NULL) { |
| 134 | ALOGE("%s: Element not found within collection", __FUNCTION__); |
| 135 | return static_cast<Property>(0); |
| 136 | } |
| 137 | return element->template get<Property>(); |
| 138 | } |
| 139 | |
François Gaffie | 0f17ab7 | 2015-05-13 18:13:00 +0200 | [diff] [blame] | 140 | routing_strategy Engine::ManagerInterfaceImpl::getStrategyForUsage(audio_usage_t usage) |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 141 | { |
François Gaffie | 0f17ab7 | 2015-05-13 18:13:00 +0200 | [diff] [blame] | 142 | return mPolicyEngine->getPropertyForKey<routing_strategy, audio_usage_t>(usage); |
| 143 | } |
| 144 | |
| 145 | audio_devices_t Engine::ManagerInterfaceImpl::getDeviceForStrategy(routing_strategy strategy) const |
| 146 | { |
| 147 | const SwAudioOutputCollection &outputs = mPolicyEngine->mApmObserver->getOutputs(); |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 148 | |
| 149 | /** This is the only case handled programmatically because the PFW is unable to know the |
| 150 | * activity of streams. |
| 151 | * |
| 152 | * -While media is playing on a remote device, use the the sonification behavior. |
| 153 | * Note that we test this usecase before testing if media is playing because |
| 154 | * the isStreamActive() method only informs about the activity of a stream, not |
| 155 | * if it's for local playback. Note also that we use the same delay between both tests |
| 156 | * |
| 157 | * -When media is not playing anymore, fall back on the sonification behavior |
| 158 | */ |
| 159 | if (strategy == STRATEGY_SONIFICATION_RESPECTFUL && |
| 160 | !is_state_in_call(getPhoneState()) && |
| 161 | !outputs.isStreamActiveRemotely(AUDIO_STREAM_MUSIC, |
| 162 | SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY) && |
| 163 | outputs.isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) { |
François Gaffie | 0f17ab7 | 2015-05-13 18:13:00 +0200 | [diff] [blame] | 164 | return mPolicyEngine->getPropertyForKey<audio_devices_t, routing_strategy>(STRATEGY_MEDIA); |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 165 | } |
François Gaffie | 4ac9d84 | 2016-04-12 16:56:35 +0200 | [diff] [blame] | 166 | if (strategy == STRATEGY_ACCESSIBILITY && |
| 167 | (outputs.isStreamActive(AUDIO_STREAM_RING) || outputs.isStreamActive(AUDIO_STREAM_ALARM))) { |
| 168 | // do not route accessibility prompts to a digital output currently configured with a |
| 169 | // compressed format as they would likely not be mixed and dropped. |
| 170 | // Device For Sonification conf file has HDMI, SPDIF and HDMI ARC unreacheable. |
| 171 | return mPolicyEngine->getPropertyForKey<audio_devices_t, routing_strategy>( |
| 172 | STRATEGY_SONIFICATION); |
| 173 | } |
François Gaffie | 0f17ab7 | 2015-05-13 18:13:00 +0200 | [diff] [blame] | 174 | return mPolicyEngine->getPropertyForKey<audio_devices_t, routing_strategy>(strategy); |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 175 | } |
| 176 | |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 177 | bool Engine::PluginInterfaceImpl::setVolumeProfileForStream(const audio_stream_type_t &stream, |
| 178 | const audio_stream_type_t &profile) |
| 179 | { |
| 180 | if (mPolicyEngine->setPropertyForKey<audio_stream_type_t, audio_stream_type_t>(stream, |
| 181 | profile)) { |
| 182 | mPolicyEngine->mApmObserver->getVolumeCurves().switchVolumeCurve(profile, stream); |
| 183 | return true; |
| 184 | } |
| 185 | return false; |
| 186 | } |
| 187 | |
| 188 | |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 189 | template <typename Property, typename Key> |
| 190 | bool Engine::setPropertyForKey(const Property &property, const Key &key) |
| 191 | { |
| 192 | Element<Key> *element = getFromCollection<Key>(key); |
| 193 | if (element == NULL) { |
| 194 | ALOGE("%s: Element not found within collection", __FUNCTION__); |
| 195 | return BAD_VALUE; |
| 196 | } |
| 197 | return element->template set<Property>(property) == NO_ERROR; |
| 198 | } |
| 199 | |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 200 | status_t Engine::setPhoneState(audio_mode_t mode) |
| 201 | { |
| 202 | return mPolicyParameterMgr->setPhoneState(mode); |
| 203 | } |
| 204 | |
| 205 | audio_mode_t Engine::getPhoneState() const |
| 206 | { |
| 207 | return mPolicyParameterMgr->getPhoneState(); |
| 208 | } |
| 209 | |
| 210 | status_t Engine::setForceUse(audio_policy_force_use_t usage, |
| 211 | audio_policy_forced_cfg_t config) |
| 212 | { |
| 213 | return mPolicyParameterMgr->setForceUse(usage, config); |
| 214 | } |
| 215 | |
| 216 | audio_policy_forced_cfg_t Engine::getForceUse(audio_policy_force_use_t usage) const |
| 217 | { |
| 218 | return mPolicyParameterMgr->getForceUse(usage); |
| 219 | } |
| 220 | |
François Gaffie | a3e696d | 2015-12-18 09:38:43 +0100 | [diff] [blame] | 221 | status_t Engine::setDeviceConnectionState(const sp<DeviceDescriptor> devDesc, |
François Gaffie | 3305c11 | 2018-02-22 10:56:49 +0100 | [diff] [blame^] | 222 | audio_policy_dev_state_t state) |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 223 | { |
François Gaffie | 3305c11 | 2018-02-22 10:56:49 +0100 | [diff] [blame^] | 224 | mPolicyParameterMgr->setDeviceConnectionState(devDesc, state); |
| 225 | |
François Gaffie | a3e696d | 2015-12-18 09:38:43 +0100 | [diff] [blame] | 226 | if (audio_is_output_device(devDesc->type())) { |
| 227 | return mPolicyParameterMgr->setAvailableOutputDevices( |
| 228 | mApmObserver->getAvailableOutputDevices().types()); |
| 229 | } else if (audio_is_input_device(devDesc->type())) { |
| 230 | return mPolicyParameterMgr->setAvailableInputDevices( |
| 231 | mApmObserver->getAvailableInputDevices().types()); |
| 232 | } |
| 233 | return BAD_TYPE; |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | template <> |
| 237 | AudioPolicyManagerInterface *Engine::queryInterface() |
| 238 | { |
| 239 | return &mManagerInterface; |
| 240 | } |
| 241 | |
| 242 | template <> |
| 243 | AudioPolicyPluginInterface *Engine::queryInterface() |
| 244 | { |
| 245 | return &mPluginInterface; |
| 246 | } |
| 247 | |
| 248 | } // namespace audio_policy |
| 249 | } // namespace android |
| 250 | |
| 251 | |