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 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { 153 ALOGW("setForceUse() invalid config %d for ENCODED_SURROUND", config); 154 return BAD_VALUE; 155 } 156 mForceUse[usage] = config; 157 break; 158 case AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING: 159 if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_NONE) { 160 ALOGW("setForceUse() invalid config %d for FOR_VIBRATE_RINGING", config); 161 return BAD_VALUE; 162 } 163 mForceUse[usage] = config; 164 break; 165 default: 166 ALOGW("setForceUse() invalid usage %d", usage); 167 break; // TODO return BAD_VALUE? 168 } 169 return NO_ERROR; 170 } 171 172 routing_strategy Engine::getStrategyForStream(audio_stream_type_t stream) 173 { 174 // stream to strategy mapping 175 switch (stream) { 176 case AUDIO_STREAM_VOICE_CALL: 177 case AUDIO_STREAM_BLUETOOTH_SCO: 178 return STRATEGY_PHONE; 179 case AUDIO_STREAM_RING: 180 case AUDIO_STREAM_ALARM: 181 return STRATEGY_SONIFICATION; 182 case AUDIO_STREAM_NOTIFICATION: 183 return STRATEGY_SONIFICATION_RESPECTFUL; 184 case AUDIO_STREAM_DTMF: 185 return STRATEGY_DTMF; 186 default: 187 ALOGE("unknown stream type %d", stream); 188 case AUDIO_STREAM_SYSTEM: 189 // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs 190 // while key clicks are played produces a poor result 191 case AUDIO_STREAM_MUSIC: 192 return STRATEGY_MEDIA; 193 case AUDIO_STREAM_ENFORCED_AUDIBLE: 194 return STRATEGY_ENFORCED_AUDIBLE; 195 case AUDIO_STREAM_TTS: 196 return STRATEGY_TRANSMITTED_THROUGH_SPEAKER; 197 case AUDIO_STREAM_ACCESSIBILITY: 198 return STRATEGY_ACCESSIBILITY; 199 case AUDIO_STREAM_REROUTING: 200 return STRATEGY_REROUTING; 201 } 202 } 203 204 routing_strategy Engine::getStrategyForUsage(audio_usage_t usage) 205 { 206 // usage to strategy mapping 207 switch (usage) { 208 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: 209 return STRATEGY_ACCESSIBILITY; 210 211 case AUDIO_USAGE_MEDIA: 212 case AUDIO_USAGE_GAME: 213 case AUDIO_USAGE_ASSISTANT: 214 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: 215 case AUDIO_USAGE_ASSISTANCE_SONIFICATION: 216 return STRATEGY_MEDIA; 217 218 case AUDIO_USAGE_VOICE_COMMUNICATION: 219 return STRATEGY_PHONE; 220 221 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: 222 return STRATEGY_DTMF; 223 224 case AUDIO_USAGE_ALARM: 225 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: 226 return STRATEGY_SONIFICATION; 227 228 case AUDIO_USAGE_NOTIFICATION: 229 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: 230 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: 231 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: 232 case AUDIO_USAGE_NOTIFICATION_EVENT: 233 return STRATEGY_SONIFICATION_RESPECTFUL; 234 235 case AUDIO_USAGE_UNKNOWN: 236 default: 237 return STRATEGY_MEDIA; 238 } 239 } 240 241 audio_devices_t Engine::getDeviceForStrategy(routing_strategy strategy) const 242 { 243 DeviceVector availableOutputDevices = mApmObserver->getAvailableOutputDevices(); 244 DeviceVector availableInputDevices = mApmObserver->getAvailableInputDevices(); 245 246 const SwAudioOutputCollection &outputs = mApmObserver->getOutputs(); 247 248 return getDeviceForStrategyInt(strategy, availableOutputDevices, 249 availableInputDevices, outputs, (uint32_t)AUDIO_DEVICE_NONE); 250 } 251 252 253 audio_devices_t Engine::getDeviceForStrategyInt(routing_strategy strategy, 254 DeviceVector availableOutputDevices, 255 DeviceVector availableInputDevices, 256 const SwAudioOutputCollection &outputs, 257 uint32_t outputDeviceTypesToIgnore) const 258 { 259 uint32_t device = AUDIO_DEVICE_NONE; 260 uint32_t availableOutputDevicesType = 261 availableOutputDevices.types() & ~outputDeviceTypesToIgnore; 262 263 switch (strategy) { 264 265 case STRATEGY_TRANSMITTED_THROUGH_SPEAKER: 266 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER; 267 break; 268 269 case STRATEGY_SONIFICATION_RESPECTFUL: 270 if (isInCall() || outputs.isStreamActiveLocally(AUDIO_STREAM_VOICE_CALL)) { 271 device = getDeviceForStrategyInt( 272 STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs, 273 outputDeviceTypesToIgnore); 274 } else { 275 bool media_active_locally = 276 outputs.isStreamActiveLocally( 277 AUDIO_STREAM_MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY) 278 || outputs.isStreamActiveLocally( 279 AUDIO_STREAM_ACCESSIBILITY, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY); 280 // routing is same as media without the "remote" device 281 device = getDeviceForStrategyInt(STRATEGY_MEDIA, 282 availableOutputDevices, 283 availableInputDevices, outputs, 284 AUDIO_DEVICE_OUT_REMOTE_SUBMIX | outputDeviceTypesToIgnore); 285 // if no media is playing on the device, check for mandatory use of "safe" speaker 286 // when media would have played on speaker, and the safe speaker path is available 287 if (!media_active_locally 288 && (device & AUDIO_DEVICE_OUT_SPEAKER) 289 && (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER_SAFE)) { 290 device |= AUDIO_DEVICE_OUT_SPEAKER_SAFE; 291 device &= ~AUDIO_DEVICE_OUT_SPEAKER; 292 } 293 } 294 break; 295 296 case STRATEGY_DTMF: 297 if (!isInCall()) { 298 // when off call, DTMF strategy follows the same rules as MEDIA strategy 299 device = getDeviceForStrategyInt( 300 STRATEGY_MEDIA, availableOutputDevices, availableInputDevices, outputs, 301 outputDeviceTypesToIgnore); 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 318 // TODO: getPrimaryOutput return only devices from first module in 319 // audio_policy_configuration.xml, hearing aid is not there, but it's 320 // a primary device 321 // FIXME: this is not the right way of solving this problem 322 audio_devices_t availPrimaryOutputDevices = 323 (primaryOutput->supportedDevices() | AUDIO_DEVICE_OUT_HEARING_AID) & 324 availableOutputDevices.types(); 325 326 if (((availableInputDevices.types() & 327 AUDIO_DEVICE_IN_TELEPHONY_RX & ~AUDIO_DEVICE_BIT_IN) == 0) || 328 (((txDevice & availPrimaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && 329 (primaryOutput->getAudioPort()->getModuleVersionMajor() < 3))) { 330 availableOutputDevicesType = availPrimaryOutputDevices; 331 } 332 } 333 // for phone strategy, we first consider the forced use and then the available devices by 334 // order of priority 335 switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) { 336 case AUDIO_POLICY_FORCE_BT_SCO: 337 if (!isInCall() || strategy != STRATEGY_DTMF) { 338 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT; 339 if (device) break; 340 } 341 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET; 342 if (device) break; 343 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO; 344 if (device) break; 345 // if SCO device is requested but no SCO device is available, fall back to default case 346 // FALL THROUGH 347 348 default: // FORCE_NONE 349 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_HEARING_AID; 350 if (device) break; 351 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP 352 if (!isInCall() && 353 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) && 354 outputs.isA2dpSupported()) { 355 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP; 356 if (device) break; 357 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES; 358 if (device) break; 359 } 360 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADPHONE; 361 if (device) break; 362 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADSET; 363 if (device) break; 364 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_LINE; 365 if (device) break; 366 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_HEADSET; 367 if (device) break; 368 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_DEVICE; 369 if (device) break; 370 if (!isInCall()) { 371 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_ACCESSORY; 372 if (device) break; 373 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET; 374 if (device) break; 375 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_DIGITAL; 376 if (device) break; 377 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET; 378 if (device) break; 379 } 380 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_EARPIECE; 381 break; 382 383 case AUDIO_POLICY_FORCE_SPEAKER: 384 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to 385 // A2DP speaker when forcing to speaker output 386 if (!isInCall() && 387 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) && 388 outputs.isA2dpSupported()) { 389 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER; 390 if (device) break; 391 } 392 if (!isInCall()) { 393 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_ACCESSORY; 394 if (device) break; 395 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_DEVICE; 396 if (device) break; 397 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET; 398 if (device) break; 399 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_DIGITAL; 400 if (device) break; 401 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET; 402 if (device) break; 403 } 404 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER; 405 break; 406 } 407 break; 408 409 case STRATEGY_SONIFICATION: 410 411 // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by 412 // handleIncallSonification(). 413 if (isInCall() || outputs.isStreamActiveLocally(AUDIO_STREAM_VOICE_CALL)) { 414 device = getDeviceForStrategyInt( 415 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs, 416 outputDeviceTypesToIgnore); 417 break; 418 } 419 // FALL THROUGH 420 421 case STRATEGY_ENFORCED_AUDIBLE: 422 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION 423 // except: 424 // - when in call where it doesn't default to STRATEGY_PHONE behavior 425 // - in countries where not enforced in which case it follows STRATEGY_MEDIA 426 427 if ((strategy == STRATEGY_SONIFICATION) || 428 (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) { 429 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER; 430 } 431 432 // if SCO headset is connected and we are told to use it, play ringtone over 433 // speaker and BT SCO 434 if ((availableOutputDevicesType & AUDIO_DEVICE_OUT_ALL_SCO) != 0) { 435 uint32_t device2 = AUDIO_DEVICE_NONE; 436 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT; 437 if (device2 == AUDIO_DEVICE_NONE) { 438 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET; 439 } 440 if (device2 == AUDIO_DEVICE_NONE) { 441 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO; 442 } 443 // Use ONLY Bluetooth SCO output when ringing in vibration mode 444 if (!((mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) 445 && (strategy == STRATEGY_ENFORCED_AUDIBLE))) { 446 if (mForceUse[AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING] 447 == AUDIO_POLICY_FORCE_BT_SCO) { 448 if (device2 != AUDIO_DEVICE_NONE) { 449 device = device2; 450 break; 451 } 452 } 453 } 454 // Use both Bluetooth SCO and phone default output when ringing in normal mode 455 if (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] == AUDIO_POLICY_FORCE_BT_SCO) { 456 if ((strategy == STRATEGY_SONIFICATION) && 457 (device & AUDIO_DEVICE_OUT_SPEAKER) && 458 (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER_SAFE)) { 459 device |= AUDIO_DEVICE_OUT_SPEAKER_SAFE; 460 device &= ~AUDIO_DEVICE_OUT_SPEAKER; 461 } 462 if (device2 != AUDIO_DEVICE_NONE) { 463 device |= device2; 464 break; 465 } 466 } 467 } 468 // The second device used for sonification is the same as the device used by media strategy 469 // FALL THROUGH 470 471 case STRATEGY_ACCESSIBILITY: 472 if (strategy == STRATEGY_ACCESSIBILITY) { 473 // do not route accessibility prompts to a digital output currently configured with a 474 // compressed format as they would likely not be mixed and dropped. 475 for (size_t i = 0; i < outputs.size(); i++) { 476 sp<AudioOutputDescriptor> desc = outputs.valueAt(i); 477 audio_devices_t devices = desc->device() & 478 (AUDIO_DEVICE_OUT_HDMI | AUDIO_DEVICE_OUT_SPDIF | AUDIO_DEVICE_OUT_HDMI_ARC); 479 if (desc->isActive() && !audio_is_linear_pcm(desc->mFormat) && 480 devices != AUDIO_DEVICE_NONE) { 481 availableOutputDevicesType = availableOutputDevices.types() & ~devices; 482 } 483 } 484 availableOutputDevices = 485 availableOutputDevices.getDevicesFromType(availableOutputDevicesType); 486 if (outputs.isStreamActive(AUDIO_STREAM_RING) || 487 outputs.isStreamActive(AUDIO_STREAM_ALARM)) { 488 return getDeviceForStrategyInt( 489 STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs, 490 outputDeviceTypesToIgnore); 491 } 492 if (isInCall()) { 493 return getDeviceForStrategyInt( 494 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs, 495 outputDeviceTypesToIgnore); 496 } 497 } 498 // For other cases, STRATEGY_ACCESSIBILITY behaves like STRATEGY_MEDIA 499 // FALL THROUGH 500 501 // FIXME: STRATEGY_REROUTING follow STRATEGY_MEDIA for now 502 case STRATEGY_REROUTING: 503 case STRATEGY_MEDIA: { 504 uint32_t device2 = AUDIO_DEVICE_NONE; 505 if (strategy != STRATEGY_SONIFICATION) { 506 // no sonification on remote submix (e.g. WFD) 507 if (availableOutputDevices.getDevice(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, 508 String8("0")) != 0) { 509 device2 = availableOutputDevices.types() & AUDIO_DEVICE_OUT_REMOTE_SUBMIX; 510 } 511 } 512 if (isInCall() && (strategy == STRATEGY_MEDIA)) { 513 device = getDeviceForStrategyInt( 514 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs, 515 outputDeviceTypesToIgnore); 516 break; 517 } 518 if (device2 == AUDIO_DEVICE_NONE) { 519 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_HEARING_AID; 520 } 521 if ((device2 == AUDIO_DEVICE_NONE) && 522 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) && 523 outputs.isA2dpSupported()) { 524 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP; 525 if (device2 == AUDIO_DEVICE_NONE) { 526 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES; 527 } 528 if (device2 == AUDIO_DEVICE_NONE) { 529 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER; 530 } 531 } 532 if ((device2 == AUDIO_DEVICE_NONE) && 533 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] == AUDIO_POLICY_FORCE_SPEAKER)) { 534 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER; 535 } 536 if (device2 == AUDIO_DEVICE_NONE) { 537 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADPHONE; 538 } 539 if (device2 == AUDIO_DEVICE_NONE) { 540 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_LINE; 541 } 542 if (device2 == AUDIO_DEVICE_NONE) { 543 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADSET; 544 } 545 if (device2 == AUDIO_DEVICE_NONE) { 546 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_HEADSET; 547 } 548 if (device2 == AUDIO_DEVICE_NONE) { 549 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_ACCESSORY; 550 } 551 if (device2 == AUDIO_DEVICE_NONE) { 552 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_DEVICE; 553 } 554 if (device2 == AUDIO_DEVICE_NONE) { 555 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET; 556 } 557 if ((device2 == AUDIO_DEVICE_NONE) && (strategy != STRATEGY_SONIFICATION)) { 558 // no sonification on aux digital (e.g. HDMI) 559 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_DIGITAL; 560 } 561 if ((device2 == AUDIO_DEVICE_NONE) && 562 (mForceUse[AUDIO_POLICY_FORCE_FOR_DOCK] == AUDIO_POLICY_FORCE_ANALOG_DOCK)) { 563 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET; 564 } 565 if (device2 == AUDIO_DEVICE_NONE) { 566 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER; 567 } 568 int device3 = AUDIO_DEVICE_NONE; 569 if (strategy == STRATEGY_MEDIA) { 570 // ARC, SPDIF and AUX_LINE can co-exist with others. 571 device3 = availableOutputDevicesType & AUDIO_DEVICE_OUT_HDMI_ARC; 572 device3 |= (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPDIF); 573 device3 |= (availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_LINE); 574 } 575 576 device2 |= device3; 577 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or 578 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise 579 device |= device2; 580 581 // If hdmi system audio mode is on, remove speaker out of output list. 582 if ((strategy == STRATEGY_MEDIA) && 583 (mForceUse[AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO] == 584 AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) { 585 device &= ~AUDIO_DEVICE_OUT_SPEAKER; 586 } 587 588 // for STRATEGY_SONIFICATION: 589 // if SPEAKER was selected, and SPEAKER_SAFE is available, use SPEAKER_SAFE instead 590 if ((strategy == STRATEGY_SONIFICATION) && 591 (device & AUDIO_DEVICE_OUT_SPEAKER) && 592 (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER_SAFE)) { 593 device |= AUDIO_DEVICE_OUT_SPEAKER_SAFE; 594 device &= ~AUDIO_DEVICE_OUT_SPEAKER; 595 } 596 } break; 597 598 default: 599 ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy); 600 break; 601 } 602 603 if (device == AUDIO_DEVICE_NONE) { 604 ALOGV("getDeviceForStrategy() no device found for strategy %d", strategy); 605 device = mApmObserver->getDefaultOutputDevice()->type(); 606 ALOGE_IF(device == AUDIO_DEVICE_NONE, 607 "getDeviceForStrategy() no default device defined"); 608 } 609 ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device); 610 return device; 611 } 612 613 614 audio_devices_t Engine::getDeviceForInputSource(audio_source_t inputSource) const 615 { 616 const DeviceVector &availableOutputDevices = mApmObserver->getAvailableOutputDevices(); 617 const DeviceVector &availableInputDevices = mApmObserver->getAvailableInputDevices(); 618 const SwAudioOutputCollection &outputs = mApmObserver->getOutputs(); 619 audio_devices_t availableDeviceTypes = availableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; 620 621 uint32_t device = AUDIO_DEVICE_NONE; 622 623 // when a call is active, force device selection to match source VOICE_COMMUNICATION 624 // for most other input sources to avoid rerouting call TX audio 625 if (isInCall()) { 626 switch (inputSource) { 627 case AUDIO_SOURCE_DEFAULT: 628 case AUDIO_SOURCE_MIC: 629 case AUDIO_SOURCE_VOICE_RECOGNITION: 630 case AUDIO_SOURCE_UNPROCESSED: 631 case AUDIO_SOURCE_HOTWORD: 632 case AUDIO_SOURCE_CAMCORDER: 633 inputSource = AUDIO_SOURCE_VOICE_COMMUNICATION; 634 break; 635 default: 636 break; 637 } 638 } 639 640 switch (inputSource) { 641 case AUDIO_SOURCE_VOICE_UPLINK: 642 if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) { 643 device = AUDIO_DEVICE_IN_VOICE_CALL; 644 break; 645 } 646 break; 647 648 case AUDIO_SOURCE_DEFAULT: 649 case AUDIO_SOURCE_MIC: 650 if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_A2DP) { 651 device = AUDIO_DEVICE_IN_BLUETOOTH_A2DP; 652 } else if ((mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO) && 653 (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET)) { 654 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET; 655 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) { 656 device = AUDIO_DEVICE_IN_WIRED_HEADSET; 657 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_HEADSET) { 658 device = AUDIO_DEVICE_IN_USB_HEADSET; 659 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) { 660 device = AUDIO_DEVICE_IN_USB_DEVICE; 661 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { 662 device = AUDIO_DEVICE_IN_BUILTIN_MIC; 663 } 664 break; 665 666 case AUDIO_SOURCE_VOICE_COMMUNICATION: 667 // Allow only use of devices on primary input if in call and HAL does not support routing 668 // to voice call path. 669 if ((getPhoneState() == AUDIO_MODE_IN_CALL) && 670 (availableOutputDevices.types() & AUDIO_DEVICE_OUT_TELEPHONY_TX) == 0) { 671 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput(); 672 availableDeviceTypes = 673 availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle()) 674 & ~AUDIO_DEVICE_BIT_IN; 675 } 676 677 switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) { 678 case AUDIO_POLICY_FORCE_BT_SCO: 679 // if SCO device is requested but no SCO device is available, fall back to default case 680 if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) { 681 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET; 682 break; 683 } 684 // FALL THROUGH 685 686 default: // FORCE_NONE 687 if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) { 688 device = AUDIO_DEVICE_IN_WIRED_HEADSET; 689 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_HEADSET) { 690 device = AUDIO_DEVICE_IN_USB_HEADSET; 691 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) { 692 device = AUDIO_DEVICE_IN_USB_DEVICE; 693 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { 694 device = AUDIO_DEVICE_IN_BUILTIN_MIC; 695 } 696 break; 697 698 case AUDIO_POLICY_FORCE_SPEAKER: 699 if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) { 700 device = AUDIO_DEVICE_IN_BACK_MIC; 701 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { 702 device = AUDIO_DEVICE_IN_BUILTIN_MIC; 703 } 704 break; 705 } 706 break; 707 708 case AUDIO_SOURCE_VOICE_RECOGNITION: 709 case AUDIO_SOURCE_UNPROCESSED: 710 case AUDIO_SOURCE_HOTWORD: 711 if (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO && 712 availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) { 713 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET; 714 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) { 715 device = AUDIO_DEVICE_IN_WIRED_HEADSET; 716 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_HEADSET) { 717 device = AUDIO_DEVICE_IN_USB_HEADSET; 718 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) { 719 device = AUDIO_DEVICE_IN_USB_DEVICE; 720 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { 721 device = AUDIO_DEVICE_IN_BUILTIN_MIC; 722 } 723 break; 724 case AUDIO_SOURCE_CAMCORDER: 725 if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) { 726 device = AUDIO_DEVICE_IN_BACK_MIC; 727 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { 728 device = AUDIO_DEVICE_IN_BUILTIN_MIC; 729 } 730 break; 731 case AUDIO_SOURCE_VOICE_DOWNLINK: 732 case AUDIO_SOURCE_VOICE_CALL: 733 if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) { 734 device = AUDIO_DEVICE_IN_VOICE_CALL; 735 } 736 break; 737 case AUDIO_SOURCE_REMOTE_SUBMIX: 738 if (availableDeviceTypes & AUDIO_DEVICE_IN_REMOTE_SUBMIX) { 739 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX; 740 } 741 break; 742 case AUDIO_SOURCE_FM_TUNER: 743 if (availableDeviceTypes & AUDIO_DEVICE_IN_FM_TUNER) { 744 device = AUDIO_DEVICE_IN_FM_TUNER; 745 } 746 break; 747 default: 748 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource); 749 break; 750 } 751 if (device == AUDIO_DEVICE_NONE) { 752 ALOGV("getDeviceForInputSource() no device found for source %d", inputSource); 753 if (availableDeviceTypes & AUDIO_DEVICE_IN_STUB) { 754 device = AUDIO_DEVICE_IN_STUB; 755 } 756 ALOGE_IF(device == AUDIO_DEVICE_NONE, 757 "getDeviceForInputSource() no default device defined"); 758 } 759 ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device); 760 return device; 761 } 762 763 template <> 764 AudioPolicyManagerInterface *Engine::queryInterface() 765 { 766 return &mManagerInterface; 767 } 768 769 } // namespace audio_policy 770 } // namespace android 771 772 773