François Gaffie | 2110e04 | 2015-03-24 08:41:51 +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 "Gains.h" |
| 29 | #include <AudioPolicyManagerObserver.h> |
| 30 | #include <AudioPort.h> |
| 31 | #include <IOProfile.h> |
| 32 | #include <policy.h> |
| 33 | #include <utils/String8.h> |
| 34 | #include <utils/Log.h> |
| 35 | |
| 36 | namespace android |
| 37 | { |
| 38 | namespace audio_policy |
| 39 | { |
| 40 | |
| 41 | Engine::Engine() |
| 42 | : mManagerInterface(this), |
| 43 | mPhoneState(AUDIO_MODE_NORMAL), |
| 44 | mApmObserver(NULL) |
| 45 | { |
| 46 | for (int i = 0; i < AUDIO_POLICY_FORCE_USE_CNT; i++) { |
| 47 | mForceUse[i] = AUDIO_POLICY_FORCE_NONE; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | Engine::~Engine() |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | void Engine::setObserver(AudioPolicyManagerObserver *observer) |
| 56 | { |
| 57 | ALOG_ASSERT(observer != NULL, "Invalid Audio Policy Manager observer"); |
| 58 | mApmObserver = observer; |
| 59 | } |
| 60 | |
| 61 | status_t Engine::initCheck() |
| 62 | { |
| 63 | return (mApmObserver != NULL) ? NO_ERROR : NO_INIT; |
| 64 | } |
| 65 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 66 | float Engine::volIndexToDb(Volume::device_category category, audio_stream_type_t streamType, |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 67 | int indexInUi) |
| 68 | { |
| 69 | const StreamDescriptor &streamDesc = mApmObserver->getStreamDescriptors().valueAt(streamType); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 70 | return Gains::volIndexToDb(category, streamDesc, indexInUi); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 71 | } |
| 72 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 73 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 74 | status_t Engine::initStreamVolume(audio_stream_type_t stream, int indexMin, int indexMax) |
| 75 | { |
| 76 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); |
| 77 | if (indexMin < 0 || indexMin >= indexMax) { |
| 78 | ALOGW("initStreamVolume() invalid index limits for stream %d, min %d, max %d", |
| 79 | stream , indexMin, indexMax); |
| 80 | return BAD_VALUE; |
| 81 | } |
| 82 | mApmObserver->getStreamDescriptors().setVolumeIndexMin(stream, indexMin); |
| 83 | mApmObserver->getStreamDescriptors().setVolumeIndexMax(stream, indexMax); |
| 84 | return NO_ERROR; |
| 85 | } |
| 86 | |
| 87 | void Engine::initializeVolumeCurves(bool isSpeakerDrcEnabled) |
| 88 | { |
| 89 | StreamDescriptorCollection &streams = mApmObserver->getStreamDescriptors(); |
| 90 | |
| 91 | for (int i = 0; i < AUDIO_STREAM_CNT; i++) { |
| 92 | for (int j = 0; j < Volume::DEVICE_CATEGORY_CNT; j++) { |
| 93 | streams.setVolumeCurvePoint(static_cast<audio_stream_type_t>(i), |
| 94 | static_cast<Volume::device_category>(j), |
| 95 | Gains::sVolumeProfiles[i][j]); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // Check availability of DRC on speaker path: if available, override some of the speaker curves |
| 100 | if (isSpeakerDrcEnabled) { |
| 101 | streams.setVolumeCurvePoint(AUDIO_STREAM_SYSTEM, Volume::DEVICE_CATEGORY_SPEAKER, |
| 102 | Gains::sDefaultSystemVolumeCurveDrc); |
| 103 | streams.setVolumeCurvePoint(AUDIO_STREAM_RING, Volume::DEVICE_CATEGORY_SPEAKER, |
| 104 | Gains::sSpeakerSonificationVolumeCurveDrc); |
| 105 | streams.setVolumeCurvePoint(AUDIO_STREAM_ALARM, Volume::DEVICE_CATEGORY_SPEAKER, |
| 106 | Gains::sSpeakerSonificationVolumeCurveDrc); |
| 107 | streams.setVolumeCurvePoint(AUDIO_STREAM_NOTIFICATION, Volume::DEVICE_CATEGORY_SPEAKER, |
| 108 | Gains::sSpeakerSonificationVolumeCurveDrc); |
| 109 | streams.setVolumeCurvePoint(AUDIO_STREAM_MUSIC, Volume::DEVICE_CATEGORY_SPEAKER, |
| 110 | Gains::sSpeakerMediaVolumeCurveDrc); |
| 111 | streams.setVolumeCurvePoint(AUDIO_STREAM_ACCESSIBILITY, Volume::DEVICE_CATEGORY_SPEAKER, |
| 112 | Gains::sSpeakerMediaVolumeCurveDrc); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | |
| 117 | status_t Engine::setPhoneState(audio_mode_t state) |
| 118 | { |
| 119 | ALOGV("setPhoneState() state %d", state); |
| 120 | |
| 121 | if (state < 0 || state >= AUDIO_MODE_CNT) { |
| 122 | ALOGW("setPhoneState() invalid state %d", state); |
| 123 | return BAD_VALUE; |
| 124 | } |
| 125 | |
| 126 | if (state == mPhoneState ) { |
| 127 | ALOGW("setPhoneState() setting same state %d", state); |
| 128 | return BAD_VALUE; |
| 129 | } |
| 130 | |
| 131 | // store previous phone state for management of sonification strategy below |
| 132 | int oldState = mPhoneState; |
| 133 | mPhoneState = state; |
| 134 | StreamDescriptorCollection &streams = mApmObserver->getStreamDescriptors(); |
| 135 | // are we entering or starting a call |
| 136 | if (!is_state_in_call(oldState) && is_state_in_call(state)) { |
| 137 | ALOGV(" Entering call in setPhoneState()"); |
| 138 | for (int j = 0; j < Volume::DEVICE_CATEGORY_CNT; j++) { |
| 139 | streams.setVolumeCurvePoint(AUDIO_STREAM_DTMF, static_cast<Volume::device_category>(j), |
| 140 | Gains::sVolumeProfiles[AUDIO_STREAM_VOICE_CALL][j]); |
| 141 | } |
| 142 | } else if (is_state_in_call(oldState) && !is_state_in_call(state)) { |
| 143 | ALOGV(" Exiting call in setPhoneState()"); |
| 144 | for (int j = 0; j < Volume::DEVICE_CATEGORY_CNT; j++) { |
| 145 | streams.setVolumeCurvePoint(AUDIO_STREAM_DTMF, static_cast<Volume::device_category>(j), |
| 146 | Gains::sVolumeProfiles[AUDIO_STREAM_DTMF][j]); |
| 147 | } |
| 148 | } |
| 149 | return NO_ERROR; |
| 150 | } |
| 151 | |
| 152 | status_t Engine::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config) |
| 153 | { |
| 154 | switch(usage) { |
| 155 | case AUDIO_POLICY_FORCE_FOR_COMMUNICATION: |
| 156 | if (config != AUDIO_POLICY_FORCE_SPEAKER && config != AUDIO_POLICY_FORCE_BT_SCO && |
| 157 | config != AUDIO_POLICY_FORCE_NONE) { |
| 158 | ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config); |
| 159 | return BAD_VALUE; |
| 160 | } |
| 161 | mForceUse[usage] = config; |
| 162 | break; |
| 163 | case AUDIO_POLICY_FORCE_FOR_MEDIA: |
| 164 | if (config != AUDIO_POLICY_FORCE_HEADPHONES && config != AUDIO_POLICY_FORCE_BT_A2DP && |
| 165 | config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY && |
| 166 | config != AUDIO_POLICY_FORCE_ANALOG_DOCK && |
| 167 | config != AUDIO_POLICY_FORCE_DIGITAL_DOCK && config != AUDIO_POLICY_FORCE_NONE && |
| 168 | config != AUDIO_POLICY_FORCE_NO_BT_A2DP && config != AUDIO_POLICY_FORCE_SPEAKER ) { |
| 169 | ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config); |
| 170 | return BAD_VALUE; |
| 171 | } |
| 172 | mForceUse[usage] = config; |
| 173 | break; |
| 174 | case AUDIO_POLICY_FORCE_FOR_RECORD: |
| 175 | if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY && |
| 176 | config != AUDIO_POLICY_FORCE_NONE) { |
| 177 | ALOGW("setForceUse() invalid config %d for FOR_RECORD", config); |
| 178 | return BAD_VALUE; |
| 179 | } |
| 180 | mForceUse[usage] = config; |
| 181 | break; |
| 182 | case AUDIO_POLICY_FORCE_FOR_DOCK: |
| 183 | if (config != AUDIO_POLICY_FORCE_NONE && config != AUDIO_POLICY_FORCE_BT_CAR_DOCK && |
| 184 | config != AUDIO_POLICY_FORCE_BT_DESK_DOCK && |
| 185 | config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY && |
| 186 | config != AUDIO_POLICY_FORCE_ANALOG_DOCK && |
| 187 | config != AUDIO_POLICY_FORCE_DIGITAL_DOCK) { |
| 188 | ALOGW("setForceUse() invalid config %d for FOR_DOCK", config); |
| 189 | } |
| 190 | mForceUse[usage] = config; |
| 191 | break; |
| 192 | case AUDIO_POLICY_FORCE_FOR_SYSTEM: |
| 193 | if (config != AUDIO_POLICY_FORCE_NONE && |
| 194 | config != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
| 195 | ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config); |
| 196 | } |
| 197 | mForceUse[usage] = config; |
| 198 | break; |
| 199 | case AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO: |
| 200 | if (config != AUDIO_POLICY_FORCE_NONE && |
| 201 | config != AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED) { |
| 202 | ALOGW("setForceUse() invalid config %d forHDMI_SYSTEM_AUDIO", config); |
| 203 | } |
| 204 | mForceUse[usage] = config; |
| 205 | break; |
| 206 | default: |
| 207 | ALOGW("setForceUse() invalid usage %d", usage); |
| 208 | break; |
| 209 | } |
| 210 | return NO_ERROR; |
| 211 | } |
| 212 | |
| 213 | routing_strategy Engine::getStrategyForStream(audio_stream_type_t stream) |
| 214 | { |
| 215 | // stream to strategy mapping |
| 216 | switch (stream) { |
| 217 | case AUDIO_STREAM_VOICE_CALL: |
| 218 | case AUDIO_STREAM_BLUETOOTH_SCO: |
| 219 | return STRATEGY_PHONE; |
| 220 | case AUDIO_STREAM_RING: |
| 221 | case AUDIO_STREAM_ALARM: |
| 222 | return STRATEGY_SONIFICATION; |
| 223 | case AUDIO_STREAM_NOTIFICATION: |
| 224 | return STRATEGY_SONIFICATION_RESPECTFUL; |
| 225 | case AUDIO_STREAM_DTMF: |
| 226 | return STRATEGY_DTMF; |
| 227 | default: |
| 228 | ALOGE("unknown stream type %d", stream); |
| 229 | case AUDIO_STREAM_SYSTEM: |
| 230 | // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs |
| 231 | // while key clicks are played produces a poor result |
| 232 | case AUDIO_STREAM_MUSIC: |
| 233 | return STRATEGY_MEDIA; |
| 234 | case AUDIO_STREAM_ENFORCED_AUDIBLE: |
| 235 | return STRATEGY_ENFORCED_AUDIBLE; |
| 236 | case AUDIO_STREAM_TTS: |
| 237 | return STRATEGY_TRANSMITTED_THROUGH_SPEAKER; |
| 238 | case AUDIO_STREAM_ACCESSIBILITY: |
| 239 | return STRATEGY_ACCESSIBILITY; |
| 240 | case AUDIO_STREAM_REROUTING: |
| 241 | return STRATEGY_REROUTING; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | routing_strategy Engine::getStrategyForUsage(audio_usage_t usage) |
| 246 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 247 | const SwAudioOutputCollection &outputs = mApmObserver->getOutputs(); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 248 | |
| 249 | // usage to strategy mapping |
| 250 | switch (usage) { |
| 251 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 252 | if (outputs.isStreamActive(AUDIO_STREAM_RING) || |
| 253 | outputs.isStreamActive(AUDIO_STREAM_ALARM)) { |
| 254 | return STRATEGY_SONIFICATION; |
| 255 | } |
| 256 | if (isInCall()) { |
| 257 | return STRATEGY_PHONE; |
| 258 | } |
| 259 | return STRATEGY_ACCESSIBILITY; |
| 260 | |
| 261 | case AUDIO_USAGE_MEDIA: |
| 262 | case AUDIO_USAGE_GAME: |
| 263 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 264 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 265 | return STRATEGY_MEDIA; |
| 266 | |
| 267 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 268 | return STRATEGY_PHONE; |
| 269 | |
| 270 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 271 | return STRATEGY_DTMF; |
| 272 | |
| 273 | case AUDIO_USAGE_ALARM: |
| 274 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 275 | return STRATEGY_SONIFICATION; |
| 276 | |
| 277 | case AUDIO_USAGE_NOTIFICATION: |
| 278 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 279 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 280 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 281 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 282 | return STRATEGY_SONIFICATION_RESPECTFUL; |
| 283 | |
| 284 | case AUDIO_USAGE_UNKNOWN: |
| 285 | default: |
| 286 | return STRATEGY_MEDIA; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | audio_devices_t Engine::getDeviceForStrategy(routing_strategy strategy) const |
| 291 | { |
| 292 | const DeviceVector &availableOutputDevices = mApmObserver->getAvailableOutputDevices(); |
| 293 | const DeviceVector &availableInputDevices = mApmObserver->getAvailableInputDevices(); |
| 294 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 295 | const SwAudioOutputCollection &outputs = mApmObserver->getOutputs(); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 296 | |
| 297 | uint32_t device = AUDIO_DEVICE_NONE; |
| 298 | uint32_t availableOutputDevicesType = availableOutputDevices.types(); |
| 299 | |
| 300 | switch (strategy) { |
| 301 | |
| 302 | case STRATEGY_TRANSMITTED_THROUGH_SPEAKER: |
| 303 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER; |
| 304 | if (!device) { |
| 305 | ALOGE("getDeviceForStrategy() no device found for "\ |
| 306 | "STRATEGY_TRANSMITTED_THROUGH_SPEAKER"); |
| 307 | } |
| 308 | break; |
| 309 | |
| 310 | case STRATEGY_SONIFICATION_RESPECTFUL: |
| 311 | if (isInCall()) { |
| 312 | device = getDeviceForStrategy(STRATEGY_SONIFICATION); |
| 313 | } else if (outputs.isStreamActiveRemotely(AUDIO_STREAM_MUSIC, |
| 314 | SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) { |
| 315 | // while media is playing on a remote device, use the the sonification behavior. |
| 316 | // Note that we test this usecase before testing if media is playing because |
| 317 | // the isStreamActive() method only informs about the activity of a stream, not |
| 318 | // if it's for local playback. Note also that we use the same delay between both tests |
| 319 | device = getDeviceForStrategy(STRATEGY_SONIFICATION); |
| 320 | //user "safe" speaker if available instead of normal speaker to avoid triggering |
| 321 | //other acoustic safety mechanisms for notification |
| 322 | if (device == AUDIO_DEVICE_OUT_SPEAKER && (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER_SAFE)) |
| 323 | device = AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
| 324 | } else if (outputs.isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) { |
| 325 | // while media is playing (or has recently played), use the same device |
| 326 | device = getDeviceForStrategy(STRATEGY_MEDIA); |
| 327 | } else { |
| 328 | // when media is not playing anymore, fall back on the sonification behavior |
| 329 | device = getDeviceForStrategy(STRATEGY_SONIFICATION); |
| 330 | //user "safe" speaker if available instead of normal speaker to avoid triggering |
| 331 | //other acoustic safety mechanisms for notification |
| 332 | if (device == AUDIO_DEVICE_OUT_SPEAKER && (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER_SAFE)) |
| 333 | device = AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
| 334 | } |
| 335 | break; |
| 336 | |
| 337 | case STRATEGY_DTMF: |
| 338 | if (!isInCall()) { |
| 339 | // when off call, DTMF strategy follows the same rules as MEDIA strategy |
| 340 | device = getDeviceForStrategy(STRATEGY_MEDIA); |
| 341 | break; |
| 342 | } |
| 343 | // when in call, DTMF and PHONE strategies follow the same rules |
| 344 | // FALL THROUGH |
| 345 | |
| 346 | case STRATEGY_PHONE: |
| 347 | // Force use of only devices on primary output if: |
| 348 | // - in call AND |
| 349 | // - cannot route from voice call RX OR |
| 350 | // - audio HAL version is < 3.0 and TX device is on the primary HW module |
| 351 | if (getPhoneState() == AUDIO_MODE_IN_CALL) { |
| 352 | audio_devices_t txDevice = getDeviceForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); |
| 353 | sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput(); |
| 354 | audio_devices_t availPrimaryInputDevices = |
| 355 | availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle()); |
| 356 | audio_devices_t availPrimaryOutputDevices = |
| 357 | primaryOutput->supportedDevices() & availableOutputDevices.types(); |
| 358 | |
| 359 | if (((availableInputDevices.types() & |
| 360 | AUDIO_DEVICE_IN_TELEPHONY_RX & ~AUDIO_DEVICE_BIT_IN) == 0) || |
| 361 | (((txDevice & availPrimaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 362 | (primaryOutput->getAudioPort()->getModuleVersion() < |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 363 | AUDIO_DEVICE_API_VERSION_3_0))) { |
| 364 | availableOutputDevicesType = availPrimaryOutputDevices; |
| 365 | } |
| 366 | } |
| 367 | // for phone strategy, we first consider the forced use and then the available devices by order |
| 368 | // of priority |
| 369 | switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) { |
| 370 | case AUDIO_POLICY_FORCE_BT_SCO: |
| 371 | if (!isInCall() || strategy != STRATEGY_DTMF) { |
| 372 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT; |
| 373 | if (device) break; |
| 374 | } |
| 375 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET; |
| 376 | if (device) break; |
| 377 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO; |
| 378 | if (device) break; |
| 379 | // if SCO device is requested but no SCO device is available, fall back to default case |
| 380 | // FALL THROUGH |
| 381 | |
| 382 | default: // FORCE_NONE |
| 383 | // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP |
| 384 | if (!isInCall() && |
| 385 | (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) && |
| 386 | (outputs.getA2dpOutput() != 0)) { |
| 387 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP; |
| 388 | if (device) break; |
| 389 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES; |
| 390 | if (device) break; |
| 391 | } |
| 392 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADPHONE; |
| 393 | if (device) break; |
| 394 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADSET; |
| 395 | if (device) break; |
| 396 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_DEVICE; |
| 397 | if (device) break; |
| 398 | if (!isInCall()) { |
| 399 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_ACCESSORY; |
| 400 | if (device) break; |
| 401 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET; |
| 402 | if (device) break; |
| 403 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_DIGITAL; |
| 404 | if (device) break; |
| 405 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET; |
| 406 | if (device) break; |
| 407 | } |
| 408 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_EARPIECE; |
| 409 | if (device) break; |
| 410 | device = mApmObserver->getDefaultOutputDevice()->type(); |
| 411 | if (device == AUDIO_DEVICE_NONE) { |
| 412 | ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE"); |
| 413 | } |
| 414 | break; |
| 415 | |
| 416 | case AUDIO_POLICY_FORCE_SPEAKER: |
| 417 | // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to |
| 418 | // A2DP speaker when forcing to speaker output |
| 419 | if (!isInCall() && |
| 420 | (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) && |
| 421 | (outputs.getA2dpOutput() != 0)) { |
| 422 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER; |
| 423 | if (device) break; |
| 424 | } |
| 425 | if (!isInCall()) { |
| 426 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_ACCESSORY; |
| 427 | if (device) break; |
| 428 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_DEVICE; |
| 429 | if (device) break; |
| 430 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET; |
| 431 | if (device) break; |
| 432 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_DIGITAL; |
| 433 | if (device) break; |
| 434 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET; |
| 435 | if (device) break; |
| 436 | } |
| 437 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_LINE; |
| 438 | if (device) break; |
| 439 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER; |
| 440 | if (device) break; |
| 441 | device = mApmObserver->getDefaultOutputDevice()->type(); |
| 442 | if (device == AUDIO_DEVICE_NONE) { |
| 443 | ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER"); |
| 444 | } |
| 445 | break; |
| 446 | } |
| 447 | break; |
| 448 | |
| 449 | case STRATEGY_SONIFICATION: |
| 450 | |
| 451 | // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by |
| 452 | // handleIncallSonification(). |
| 453 | if (isInCall()) { |
| 454 | device = getDeviceForStrategy(STRATEGY_PHONE); |
| 455 | break; |
| 456 | } |
| 457 | // FALL THROUGH |
| 458 | |
| 459 | case STRATEGY_ENFORCED_AUDIBLE: |
| 460 | // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION |
| 461 | // except: |
| 462 | // - when in call where it doesn't default to STRATEGY_PHONE behavior |
| 463 | // - in countries where not enforced in which case it follows STRATEGY_MEDIA |
| 464 | |
| 465 | if ((strategy == STRATEGY_SONIFICATION) || |
| 466 | (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) { |
| 467 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER; |
| 468 | if (device == AUDIO_DEVICE_NONE) { |
| 469 | ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION"); |
| 470 | } |
| 471 | } |
| 472 | // The second device used for sonification is the same as the device used by media strategy |
| 473 | // FALL THROUGH |
| 474 | |
| 475 | // FIXME: STRATEGY_ACCESSIBILITY and STRATEGY_REROUTING follow STRATEGY_MEDIA for now |
| 476 | case STRATEGY_ACCESSIBILITY: |
| 477 | if (strategy == STRATEGY_ACCESSIBILITY) { |
| 478 | // do not route accessibility prompts to a digital output currently configured with a |
| 479 | // compressed format as they would likely not be mixed and dropped. |
| 480 | for (size_t i = 0; i < outputs.size(); i++) { |
| 481 | sp<AudioOutputDescriptor> desc = outputs.valueAt(i); |
| 482 | audio_devices_t devices = desc->device() & |
| 483 | (AUDIO_DEVICE_OUT_HDMI | AUDIO_DEVICE_OUT_SPDIF | AUDIO_DEVICE_OUT_HDMI_ARC); |
| 484 | if (desc->isActive() && !audio_is_linear_pcm(desc->mFormat) && |
| 485 | devices != AUDIO_DEVICE_NONE) { |
| 486 | availableOutputDevicesType = availableOutputDevices.types() & ~devices; |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | // FALL THROUGH |
| 491 | |
| 492 | case STRATEGY_REROUTING: |
| 493 | case STRATEGY_MEDIA: { |
| 494 | uint32_t device2 = AUDIO_DEVICE_NONE; |
| 495 | if (strategy != STRATEGY_SONIFICATION) { |
| 496 | // no sonification on remote submix (e.g. WFD) |
| 497 | if (availableOutputDevices.getDevice(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, String8("0")) != 0) { |
| 498 | device2 = availableOutputDevices.types() & AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
| 499 | } |
| 500 | } |
Eric Laurent | a20d4fa | 2015-06-04 18:39:28 -0700 | [diff] [blame^] | 501 | if (isInCall() && (strategy == STRATEGY_MEDIA)) { |
| 502 | device = getDeviceForStrategy(STRATEGY_PHONE); |
| 503 | break; |
| 504 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 505 | if ((device2 == AUDIO_DEVICE_NONE) && |
| 506 | (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) && |
| 507 | (outputs.getA2dpOutput() != 0)) { |
| 508 | device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP; |
| 509 | if (device2 == AUDIO_DEVICE_NONE) { |
| 510 | device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES; |
| 511 | } |
| 512 | if (device2 == AUDIO_DEVICE_NONE) { |
| 513 | device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER; |
| 514 | } |
| 515 | } |
| 516 | if ((device2 == AUDIO_DEVICE_NONE) && |
| 517 | (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] == AUDIO_POLICY_FORCE_SPEAKER)) { |
| 518 | device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER; |
| 519 | } |
| 520 | if (device2 == AUDIO_DEVICE_NONE) { |
| 521 | device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADPHONE; |
| 522 | } |
Jean-Michel Trivi | 5c233f8 | 2015-04-03 09:21:24 -0700 | [diff] [blame] | 523 | if (device2 == AUDIO_DEVICE_NONE) { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 524 | device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_LINE; |
| 525 | } |
| 526 | if (device2 == AUDIO_DEVICE_NONE) { |
| 527 | device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADSET; |
| 528 | } |
| 529 | if (device2 == AUDIO_DEVICE_NONE) { |
| 530 | device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_ACCESSORY; |
| 531 | } |
| 532 | if (device2 == AUDIO_DEVICE_NONE) { |
| 533 | device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_DEVICE; |
| 534 | } |
| 535 | if (device2 == AUDIO_DEVICE_NONE) { |
| 536 | device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET; |
| 537 | } |
| 538 | if ((device2 == AUDIO_DEVICE_NONE) && (strategy != STRATEGY_SONIFICATION)) { |
| 539 | // no sonification on aux digital (e.g. HDMI) |
| 540 | device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_DIGITAL; |
| 541 | } |
| 542 | if ((device2 == AUDIO_DEVICE_NONE) && |
| 543 | (mForceUse[AUDIO_POLICY_FORCE_FOR_DOCK] == AUDIO_POLICY_FORCE_ANALOG_DOCK)) { |
| 544 | device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET; |
| 545 | } |
| 546 | if (device2 == AUDIO_DEVICE_NONE) { |
| 547 | device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER; |
| 548 | } |
| 549 | int device3 = AUDIO_DEVICE_NONE; |
| 550 | if (strategy == STRATEGY_MEDIA) { |
| 551 | // ARC, SPDIF and AUX_LINE can co-exist with others. |
| 552 | device3 = availableOutputDevicesType & AUDIO_DEVICE_OUT_HDMI_ARC; |
| 553 | device3 |= (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPDIF); |
| 554 | device3 |= (availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_LINE); |
| 555 | } |
| 556 | |
| 557 | device2 |= device3; |
| 558 | // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or |
| 559 | // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise |
| 560 | device |= device2; |
| 561 | |
| 562 | // If hdmi system audio mode is on, remove speaker out of output list. |
| 563 | if ((strategy == STRATEGY_MEDIA) && |
| 564 | (mForceUse[AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO] == |
| 565 | AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) { |
| 566 | device &= ~AUDIO_DEVICE_OUT_SPEAKER; |
| 567 | } |
| 568 | |
| 569 | if (device) break; |
| 570 | device = mApmObserver->getDefaultOutputDevice()->type(); |
| 571 | if (device == AUDIO_DEVICE_NONE) { |
| 572 | ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA"); |
| 573 | } |
| 574 | } break; |
| 575 | |
| 576 | default: |
| 577 | ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy); |
| 578 | break; |
| 579 | } |
| 580 | |
| 581 | ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device); |
| 582 | return device; |
| 583 | } |
| 584 | |
| 585 | |
| 586 | audio_devices_t Engine::getDeviceForInputSource(audio_source_t inputSource) const |
| 587 | { |
| 588 | const DeviceVector &availableOutputDevices = mApmObserver->getAvailableOutputDevices(); |
| 589 | const DeviceVector &availableInputDevices = mApmObserver->getAvailableInputDevices(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 590 | const SwAudioOutputCollection &outputs = mApmObserver->getOutputs(); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 591 | audio_devices_t availableDeviceTypes = availableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
| 592 | |
| 593 | uint32_t device = AUDIO_DEVICE_NONE; |
| 594 | |
| 595 | switch (inputSource) { |
| 596 | case AUDIO_SOURCE_VOICE_UPLINK: |
| 597 | if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) { |
| 598 | device = AUDIO_DEVICE_IN_VOICE_CALL; |
| 599 | break; |
| 600 | } |
| 601 | break; |
| 602 | |
| 603 | case AUDIO_SOURCE_DEFAULT: |
| 604 | case AUDIO_SOURCE_MIC: |
| 605 | if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_A2DP) { |
| 606 | device = AUDIO_DEVICE_IN_BLUETOOTH_A2DP; |
| 607 | } else if ((mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO) && |
| 608 | (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET)) { |
| 609 | device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET; |
| 610 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) { |
| 611 | device = AUDIO_DEVICE_IN_WIRED_HEADSET; |
| 612 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) { |
| 613 | device = AUDIO_DEVICE_IN_USB_DEVICE; |
| 614 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { |
| 615 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
| 616 | } |
| 617 | break; |
| 618 | |
| 619 | case AUDIO_SOURCE_VOICE_COMMUNICATION: |
| 620 | // Allow only use of devices on primary input if in call and HAL does not support routing |
| 621 | // to voice call path. |
| 622 | if ((getPhoneState() == AUDIO_MODE_IN_CALL) && |
| 623 | (availableOutputDevices.types() & AUDIO_DEVICE_OUT_TELEPHONY_TX) == 0) { |
| 624 | sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput(); |
| 625 | availableDeviceTypes = |
| 626 | availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle()) |
| 627 | & ~AUDIO_DEVICE_BIT_IN; |
| 628 | } |
| 629 | |
| 630 | switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) { |
| 631 | case AUDIO_POLICY_FORCE_BT_SCO: |
| 632 | // if SCO device is requested but no SCO device is available, fall back to default case |
| 633 | if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) { |
| 634 | device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET; |
| 635 | break; |
| 636 | } |
| 637 | // FALL THROUGH |
| 638 | |
| 639 | default: // FORCE_NONE |
| 640 | if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) { |
| 641 | device = AUDIO_DEVICE_IN_WIRED_HEADSET; |
| 642 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) { |
| 643 | device = AUDIO_DEVICE_IN_USB_DEVICE; |
| 644 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { |
| 645 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
| 646 | } |
| 647 | break; |
| 648 | |
| 649 | case AUDIO_POLICY_FORCE_SPEAKER: |
| 650 | if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) { |
| 651 | device = AUDIO_DEVICE_IN_BACK_MIC; |
| 652 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { |
| 653 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
| 654 | } |
| 655 | break; |
| 656 | } |
| 657 | break; |
| 658 | |
| 659 | case AUDIO_SOURCE_VOICE_RECOGNITION: |
| 660 | case AUDIO_SOURCE_HOTWORD: |
| 661 | if (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO && |
| 662 | availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) { |
| 663 | device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET; |
| 664 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) { |
| 665 | device = AUDIO_DEVICE_IN_WIRED_HEADSET; |
| 666 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) { |
| 667 | device = AUDIO_DEVICE_IN_USB_DEVICE; |
| 668 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { |
| 669 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
| 670 | } |
| 671 | break; |
| 672 | case AUDIO_SOURCE_CAMCORDER: |
| 673 | if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) { |
| 674 | device = AUDIO_DEVICE_IN_BACK_MIC; |
| 675 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { |
| 676 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
| 677 | } |
| 678 | break; |
| 679 | case AUDIO_SOURCE_VOICE_DOWNLINK: |
| 680 | case AUDIO_SOURCE_VOICE_CALL: |
| 681 | if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) { |
| 682 | device = AUDIO_DEVICE_IN_VOICE_CALL; |
| 683 | } |
| 684 | break; |
| 685 | case AUDIO_SOURCE_REMOTE_SUBMIX: |
| 686 | if (availableDeviceTypes & AUDIO_DEVICE_IN_REMOTE_SUBMIX) { |
| 687 | device = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 688 | } |
| 689 | break; |
| 690 | case AUDIO_SOURCE_FM_TUNER: |
| 691 | if (availableDeviceTypes & AUDIO_DEVICE_IN_FM_TUNER) { |
| 692 | device = AUDIO_DEVICE_IN_FM_TUNER; |
| 693 | } |
| 694 | break; |
| 695 | default: |
| 696 | ALOGW("getDeviceForInputSource() invalid input source %d", inputSource); |
| 697 | break; |
| 698 | } |
| 699 | ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device); |
| 700 | return device; |
| 701 | } |
| 702 | |
| 703 | template <> |
| 704 | AudioPolicyManagerInterface *Engine::queryInterface() |
| 705 | { |
| 706 | return &mManagerInterface; |
| 707 | } |
| 708 | |
| 709 | } // namespace audio_policy |
| 710 | } // namespace android |
| 711 | |
| 712 | |