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