AudioPolicyManagerBase.cpp revision b4d07b97d23cfaffe22c7859ad7c45e168a7df0e
1/* 2 * Copyright (C) 2009 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 "AudioPolicyManagerBase" 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// A device mask for all audio input devices that are considered "virtual" when evaluating 28// active inputs in getActiveInput() 29#define APM_AUDIO_IN_DEVICE_VIRTUAL_ALL AUDIO_DEVICE_IN_REMOTE_SUBMIX 30// A device mask for all audio output devices that are considered "remote" when evaluating 31// active output devices in isStreamActiveRemotely() 32#define APM_AUDIO_OUT_DEVICE_REMOTE_ALL AUDIO_DEVICE_OUT_REMOTE_SUBMIX 33 34#include <utils/Log.h> 35#include <hardware_legacy/AudioPolicyManagerBase.h> 36#include <hardware/audio_effect.h> 37#include <hardware/audio.h> 38#include <math.h> 39#include <hardware_legacy/audio_policy_conf.h> 40 41namespace android_audio_legacy { 42 43// ---------------------------------------------------------------------------- 44// AudioPolicyInterface implementation 45// ---------------------------------------------------------------------------- 46 47 48status_t AudioPolicyManagerBase::setDeviceConnectionState(audio_devices_t device, 49 AudioSystem::device_connection_state state, 50 const char *device_address) 51{ 52 SortedVector <audio_io_handle_t> outputs; 53 54 ALOGV("setDeviceConnectionState() device: %x, state %d, address %s", device, state, device_address); 55 56 // connect/disconnect only 1 device at a time 57 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; 58 59 if (strlen(device_address) >= MAX_DEVICE_ADDRESS_LEN) { 60 ALOGE("setDeviceConnectionState() invalid address: %s", device_address); 61 return BAD_VALUE; 62 } 63 64 // handle output devices 65 if (audio_is_output_device(device)) { 66 67 if (!mHasA2dp && audio_is_a2dp_device(device)) { 68 ALOGE("setDeviceConnectionState() invalid A2DP device: %x", device); 69 return BAD_VALUE; 70 } 71 if (!mHasUsb && audio_is_usb_device(device)) { 72 ALOGE("setDeviceConnectionState() invalid USB audio device: %x", device); 73 return BAD_VALUE; 74 } 75 if (!mHasRemoteSubmix && audio_is_remote_submix_device((audio_devices_t)device)) { 76 ALOGE("setDeviceConnectionState() invalid remote submix audio device: %x", device); 77 return BAD_VALUE; 78 } 79 80 // save a copy of the opened output descriptors before any output is opened or closed 81 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies() 82 mPreviousOutputs = mOutputs; 83 switch (state) 84 { 85 // handle output device connection 86 case AudioSystem::DEVICE_STATE_AVAILABLE: 87 if (mAvailableOutputDevices & device) { 88 ALOGW("setDeviceConnectionState() device already connected: %x", device); 89 return INVALID_OPERATION; 90 } 91 ALOGV("setDeviceConnectionState() connecting device %x", device); 92 93 if (checkOutputsForDevice(device, state, outputs) != NO_ERROR) { 94 return INVALID_OPERATION; 95 } 96 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %d outputs", 97 outputs.size()); 98 // register new device as available 99 mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices | device); 100 101 if (!outputs.isEmpty()) { 102 String8 paramStr; 103 if (mHasA2dp && audio_is_a2dp_device(device)) { 104 // handle A2DP device connection 105 AudioParameter param; 106 param.add(String8(AUDIO_PARAMETER_A2DP_SINK_ADDRESS), String8(device_address)); 107 paramStr = param.toString(); 108 mA2dpDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN); 109 mA2dpSuspended = false; 110 } else if (audio_is_bluetooth_sco_device(device)) { 111 // handle SCO device connection 112 mScoDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN); 113 } else if (mHasUsb && audio_is_usb_device(device)) { 114 // handle USB device connection 115 mUsbCardAndDevice = String8(device_address, MAX_DEVICE_ADDRESS_LEN); 116 paramStr = mUsbCardAndDevice; 117 } 118 // not currently handling multiple simultaneous submixes: ignoring remote submix 119 // case and address 120 if (!paramStr.isEmpty()) { 121 for (size_t i = 0; i < outputs.size(); i++) { 122 mpClientInterface->setParameters(outputs[i], paramStr); 123 } 124 } 125 } 126 break; 127 // handle output device disconnection 128 case AudioSystem::DEVICE_STATE_UNAVAILABLE: { 129 if (!(mAvailableOutputDevices & device)) { 130 ALOGW("setDeviceConnectionState() device not connected: %x", device); 131 return INVALID_OPERATION; 132 } 133 134 ALOGV("setDeviceConnectionState() disconnecting device %x", device); 135 // remove device from available output devices 136 mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices & ~device); 137 138 checkOutputsForDevice(device, state, outputs); 139 if (mHasA2dp && audio_is_a2dp_device(device)) { 140 // handle A2DP device disconnection 141 mA2dpDeviceAddress = ""; 142 mA2dpSuspended = false; 143 } else if (audio_is_bluetooth_sco_device(device)) { 144 // handle SCO device disconnection 145 mScoDeviceAddress = ""; 146 } else if (mHasUsb && audio_is_usb_device(device)) { 147 // handle USB device disconnection 148 mUsbCardAndDevice = ""; 149 } 150 // not currently handling multiple simultaneous submixes: ignoring remote submix 151 // case and address 152 } break; 153 154 default: 155 ALOGE("setDeviceConnectionState() invalid state: %x", state); 156 return BAD_VALUE; 157 } 158 159 checkA2dpSuspend(); 160 checkOutputForAllStrategies(); 161 // outputs must be closed after checkOutputForAllStrategies() is executed 162 if (!outputs.isEmpty()) { 163 for (size_t i = 0; i < outputs.size(); i++) { 164 // close unused outputs after device disconnection or direct outputs that have been 165 // opened by checkOutputsForDevice() to query dynamic parameters 166 if ((state == AudioSystem::DEVICE_STATE_UNAVAILABLE) || 167 (mOutputs.valueFor(outputs[i])->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) { 168 closeOutput(outputs[i]); 169 } 170 } 171 } 172 173 updateDevicesAndOutputs(); 174 for (size_t i = 0; i < mOutputs.size(); i++) { 175 // do not force device change on duplicated output because if device is 0, it will 176 // also force a device 0 for the two outputs it is duplicated to which may override 177 // a valid device selection on those outputs. 178 setOutputDevice(mOutputs.keyAt(i), 179 getNewDevice(mOutputs.keyAt(i), true /*fromCache*/), 180 !mOutputs.valueAt(i)->isDuplicated(), 181 0); 182 } 183 184 if (device == AUDIO_DEVICE_OUT_WIRED_HEADSET) { 185 device = AUDIO_DEVICE_IN_WIRED_HEADSET; 186 } else if (device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO || 187 device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET || 188 device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT) { 189 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET; 190 } else { 191 return NO_ERROR; 192 } 193 } 194 // handle input devices 195 if (audio_is_input_device(device)) { 196 197 switch (state) 198 { 199 // handle input device connection 200 case AudioSystem::DEVICE_STATE_AVAILABLE: { 201 if (mAvailableInputDevices & device) { 202 ALOGW("setDeviceConnectionState() device already connected: %d", device); 203 return INVALID_OPERATION; 204 } 205 mAvailableInputDevices = mAvailableInputDevices | (device & ~AUDIO_DEVICE_BIT_IN); 206 } 207 break; 208 209 // handle input device disconnection 210 case AudioSystem::DEVICE_STATE_UNAVAILABLE: { 211 if (!(mAvailableInputDevices & device)) { 212 ALOGW("setDeviceConnectionState() device not connected: %d", device); 213 return INVALID_OPERATION; 214 } 215 mAvailableInputDevices = (audio_devices_t) (mAvailableInputDevices & ~device); 216 } break; 217 218 default: 219 ALOGE("setDeviceConnectionState() invalid state: %x", state); 220 return BAD_VALUE; 221 } 222 223 audio_io_handle_t activeInput = getActiveInput(); 224 if (activeInput != 0) { 225 AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput); 226 audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource); 227 if ((newDevice != AUDIO_DEVICE_NONE) && (newDevice != inputDesc->mDevice)) { 228 ALOGV("setDeviceConnectionState() changing device from %x to %x for input %d", 229 inputDesc->mDevice, newDevice, activeInput); 230 inputDesc->mDevice = newDevice; 231 AudioParameter param = AudioParameter(); 232 param.addInt(String8(AudioParameter::keyRouting), (int)newDevice); 233 mpClientInterface->setParameters(activeInput, param.toString()); 234 } 235 } 236 237 return NO_ERROR; 238 } 239 240 ALOGW("setDeviceConnectionState() invalid device: %x", device); 241 return BAD_VALUE; 242} 243 244AudioSystem::device_connection_state AudioPolicyManagerBase::getDeviceConnectionState(audio_devices_t device, 245 const char *device_address) 246{ 247 AudioSystem::device_connection_state state = AudioSystem::DEVICE_STATE_UNAVAILABLE; 248 String8 address = String8(device_address); 249 if (audio_is_output_device(device)) { 250 if (device & mAvailableOutputDevices) { 251 if (audio_is_a2dp_device(device) && 252 (!mHasA2dp || (address != "" && mA2dpDeviceAddress != address))) { 253 return state; 254 } 255 if (audio_is_bluetooth_sco_device(device) && 256 address != "" && mScoDeviceAddress != address) { 257 return state; 258 } 259 if (audio_is_usb_device(device) && 260 (!mHasUsb || (address != "" && mUsbCardAndDevice != address))) { 261 ALOGE("getDeviceConnectionState() invalid device: %x", device); 262 return state; 263 } 264 if (audio_is_remote_submix_device((audio_devices_t)device) && !mHasRemoteSubmix) { 265 return state; 266 } 267 state = AudioSystem::DEVICE_STATE_AVAILABLE; 268 } 269 } else if (audio_is_input_device(device)) { 270 if (device & mAvailableInputDevices) { 271 state = AudioSystem::DEVICE_STATE_AVAILABLE; 272 } 273 } 274 275 return state; 276} 277 278void AudioPolicyManagerBase::setPhoneState(int state) 279{ 280 ALOGV("setPhoneState() state %d", state); 281 audio_devices_t newDevice = AUDIO_DEVICE_NONE; 282 if (state < 0 || state >= AudioSystem::NUM_MODES) { 283 ALOGW("setPhoneState() invalid state %d", state); 284 return; 285 } 286 287 if (state == mPhoneState ) { 288 ALOGW("setPhoneState() setting same state %d", state); 289 return; 290 } 291 292 // if leaving call state, handle special case of active streams 293 // pertaining to sonification strategy see handleIncallSonification() 294 if (isInCall()) { 295 ALOGV("setPhoneState() in call state management: new state is %d", state); 296 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) { 297 handleIncallSonification(stream, false, true); 298 } 299 } 300 301 // store previous phone state for management of sonification strategy below 302 int oldState = mPhoneState; 303 mPhoneState = state; 304 bool force = false; 305 306 // are we entering or starting a call 307 if (!isStateInCall(oldState) && isStateInCall(state)) { 308 ALOGV(" Entering call in setPhoneState()"); 309 // force routing command to audio hardware when starting a call 310 // even if no device change is needed 311 force = true; 312 } else if (isStateInCall(oldState) && !isStateInCall(state)) { 313 ALOGV(" Exiting call in setPhoneState()"); 314 // force routing command to audio hardware when exiting a call 315 // even if no device change is needed 316 force = true; 317 } else if (isStateInCall(state) && (state != oldState)) { 318 ALOGV(" Switching between telephony and VoIP in setPhoneState()"); 319 // force routing command to audio hardware when switching between telephony and VoIP 320 // even if no device change is needed 321 force = true; 322 } 323 324 // check for device and output changes triggered by new phone state 325 newDevice = getNewDevice(mPrimaryOutput, false /*fromCache*/); 326 checkA2dpSuspend(); 327 checkOutputForAllStrategies(); 328 updateDevicesAndOutputs(); 329 330 AudioOutputDescriptor *hwOutputDesc = mOutputs.valueFor(mPrimaryOutput); 331 332 // force routing command to audio hardware when ending call 333 // even if no device change is needed 334 if (isStateInCall(oldState) && newDevice == AUDIO_DEVICE_NONE) { 335 newDevice = hwOutputDesc->device(); 336 } 337 338 int delayMs = 0; 339 if (isStateInCall(state)) { 340 nsecs_t sysTime = systemTime(); 341 for (size_t i = 0; i < mOutputs.size(); i++) { 342 AudioOutputDescriptor *desc = mOutputs.valueAt(i); 343 // mute media and sonification strategies and delay device switch by the largest 344 // latency of any output where either strategy is active. 345 // This avoid sending the ring tone or music tail into the earpiece or headset. 346 if ((desc->isStrategyActive(STRATEGY_MEDIA, 347 SONIFICATION_HEADSET_MUSIC_DELAY, 348 sysTime) || 349 desc->isStrategyActive(STRATEGY_SONIFICATION, 350 SONIFICATION_HEADSET_MUSIC_DELAY, 351 sysTime)) && 352 (delayMs < (int)desc->mLatency*2)) { 353 delayMs = desc->mLatency*2; 354 } 355 setStrategyMute(STRATEGY_MEDIA, true, mOutputs.keyAt(i)); 356 setStrategyMute(STRATEGY_MEDIA, false, mOutputs.keyAt(i), MUTE_TIME_MS, 357 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/)); 358 setStrategyMute(STRATEGY_SONIFICATION, true, mOutputs.keyAt(i)); 359 setStrategyMute(STRATEGY_SONIFICATION, false, mOutputs.keyAt(i), MUTE_TIME_MS, 360 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/)); 361 } 362 } 363 364 // change routing is necessary 365 setOutputDevice(mPrimaryOutput, newDevice, force, delayMs); 366 367 // if entering in call state, handle special case of active streams 368 // pertaining to sonification strategy see handleIncallSonification() 369 if (isStateInCall(state)) { 370 ALOGV("setPhoneState() in call state management: new state is %d", state); 371 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) { 372 handleIncallSonification(stream, true, true); 373 } 374 } 375 376 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE 377 if (state == AudioSystem::MODE_RINGTONE && 378 isStreamActive(AudioSystem::MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) { 379 mLimitRingtoneVolume = true; 380 } else { 381 mLimitRingtoneVolume = false; 382 } 383} 384 385void AudioPolicyManagerBase::setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config) 386{ 387 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState); 388 389 bool forceVolumeReeval = false; 390 switch(usage) { 391 case AudioSystem::FOR_COMMUNICATION: 392 if (config != AudioSystem::FORCE_SPEAKER && config != AudioSystem::FORCE_BT_SCO && 393 config != AudioSystem::FORCE_NONE) { 394 ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config); 395 return; 396 } 397 forceVolumeReeval = true; 398 mForceUse[usage] = config; 399 break; 400 case AudioSystem::FOR_MEDIA: 401 if (config != AudioSystem::FORCE_HEADPHONES && config != AudioSystem::FORCE_BT_A2DP && 402 config != AudioSystem::FORCE_WIRED_ACCESSORY && 403 config != AudioSystem::FORCE_ANALOG_DOCK && 404 config != AudioSystem::FORCE_DIGITAL_DOCK && config != AudioSystem::FORCE_NONE && 405 config != AudioSystem::FORCE_NO_BT_A2DP) { 406 ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config); 407 return; 408 } 409 mForceUse[usage] = config; 410 break; 411 case AudioSystem::FOR_RECORD: 412 if (config != AudioSystem::FORCE_BT_SCO && config != AudioSystem::FORCE_WIRED_ACCESSORY && 413 config != AudioSystem::FORCE_NONE) { 414 ALOGW("setForceUse() invalid config %d for FOR_RECORD", config); 415 return; 416 } 417 mForceUse[usage] = config; 418 break; 419 case AudioSystem::FOR_DOCK: 420 if (config != AudioSystem::FORCE_NONE && config != AudioSystem::FORCE_BT_CAR_DOCK && 421 config != AudioSystem::FORCE_BT_DESK_DOCK && 422 config != AudioSystem::FORCE_WIRED_ACCESSORY && 423 config != AudioSystem::FORCE_ANALOG_DOCK && 424 config != AudioSystem::FORCE_DIGITAL_DOCK) { 425 ALOGW("setForceUse() invalid config %d for FOR_DOCK", config); 426 } 427 forceVolumeReeval = true; 428 mForceUse[usage] = config; 429 break; 430 case AudioSystem::FOR_SYSTEM: 431 if (config != AudioSystem::FORCE_NONE && 432 config != AudioSystem::FORCE_SYSTEM_ENFORCED) { 433 ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config); 434 } 435 forceVolumeReeval = true; 436 mForceUse[usage] = config; 437 break; 438 default: 439 ALOGW("setForceUse() invalid usage %d", usage); 440 break; 441 } 442 443 // check for device and output changes triggered by new force usage 444 checkA2dpSuspend(); 445 checkOutputForAllStrategies(); 446 updateDevicesAndOutputs(); 447 for (size_t i = 0; i < mOutputs.size(); i++) { 448 audio_io_handle_t output = mOutputs.keyAt(i); 449 audio_devices_t newDevice = getNewDevice(output, true /*fromCache*/); 450 setOutputDevice(output, newDevice, (newDevice != AUDIO_DEVICE_NONE)); 451 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) { 452 applyStreamVolumes(output, newDevice, 0, true); 453 } 454 } 455 456 audio_io_handle_t activeInput = getActiveInput(); 457 if (activeInput != 0) { 458 AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput); 459 audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource); 460 if ((newDevice != AUDIO_DEVICE_NONE) && (newDevice != inputDesc->mDevice)) { 461 ALOGV("setForceUse() changing device from %x to %x for input %d", 462 inputDesc->mDevice, newDevice, activeInput); 463 inputDesc->mDevice = newDevice; 464 AudioParameter param = AudioParameter(); 465 param.addInt(String8(AudioParameter::keyRouting), (int)newDevice); 466 mpClientInterface->setParameters(activeInput, param.toString()); 467 } 468 } 469 470} 471 472AudioSystem::forced_config AudioPolicyManagerBase::getForceUse(AudioSystem::force_use usage) 473{ 474 return mForceUse[usage]; 475} 476 477void AudioPolicyManagerBase::setSystemProperty(const char* property, const char* value) 478{ 479 ALOGV("setSystemProperty() property %s, value %s", property, value); 480} 481 482AudioPolicyManagerBase::IOProfile *AudioPolicyManagerBase::getProfileForDirectOutput( 483 audio_devices_t device, 484 uint32_t samplingRate, 485 uint32_t format, 486 uint32_t channelMask, 487 audio_output_flags_t flags) 488{ 489 for (size_t i = 0; i < mHwModules.size(); i++) { 490 if (mHwModules[i]->mHandle == 0) { 491 continue; 492 } 493 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) { 494 IOProfile *profile = mHwModules[i]->mOutputProfiles[j]; 495 if (profile->isCompatibleProfile(device, samplingRate, format, 496 channelMask, 497 AUDIO_OUTPUT_FLAG_DIRECT)) { 498 if (mAvailableOutputDevices & profile->mSupportedDevices) { 499 return mHwModules[i]->mOutputProfiles[j]; 500 } 501 } 502 } 503 } 504 return 0; 505} 506 507audio_io_handle_t AudioPolicyManagerBase::getOutput(AudioSystem::stream_type stream, 508 uint32_t samplingRate, 509 uint32_t format, 510 uint32_t channelMask, 511 AudioSystem::output_flags flags, 512 const audio_offload_info_t *offloadInfo) 513{ 514 audio_io_handle_t output = 0; 515 uint32_t latency = 0; 516 routing_strategy strategy = getStrategy((AudioSystem::stream_type)stream); 517 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); 518 ALOGV("getOutput() stream %d, samplingRate %d, format %d, channelMask %x, flags %x", 519 stream, samplingRate, format, channelMask, flags); 520 521#ifdef AUDIO_POLICY_TEST 522 if (mCurOutput != 0) { 523 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d", 524 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput); 525 526 if (mTestOutputs[mCurOutput] == 0) { 527 ALOGV("getOutput() opening test output"); 528 AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(NULL); 529 outputDesc->mDevice = mTestDevice; 530 outputDesc->mSamplingRate = mTestSamplingRate; 531 outputDesc->mFormat = mTestFormat; 532 outputDesc->mChannelMask = mTestChannels; 533 outputDesc->mLatency = mTestLatencyMs; 534 outputDesc->mFlags = (audio_output_flags_t)(mDirectOutput ? AudioSystem::OUTPUT_FLAG_DIRECT : 0); 535 outputDesc->mRefCount[stream] = 0; 536 mTestOutputs[mCurOutput] = mpClientInterface->openOutput(0, &outputDesc->mDevice, 537 &outputDesc->mSamplingRate, 538 &outputDesc->mFormat, 539 &outputDesc->mChannelMask, 540 &outputDesc->mLatency, 541 outputDesc->mFlags, 542 offloadInfo); 543 if (mTestOutputs[mCurOutput]) { 544 AudioParameter outputCmd = AudioParameter(); 545 outputCmd.addInt(String8("set_id"),mCurOutput); 546 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString()); 547 addOutput(mTestOutputs[mCurOutput], outputDesc); 548 } 549 } 550 return mTestOutputs[mCurOutput]; 551 } 552#endif //AUDIO_POLICY_TEST 553 554 // open a direct output if required by specified parameters 555 IOProfile *profile = getProfileForDirectOutput(device, 556 samplingRate, 557 format, 558 channelMask, 559 (audio_output_flags_t)flags); 560 if (profile != NULL) { 561 AudioOutputDescriptor *outputDesc = NULL; 562 563 for (size_t i = 0; i < mOutputs.size(); i++) { 564 AudioOutputDescriptor *desc = mOutputs.valueAt(i); 565 if (!desc->isDuplicated() && (profile == desc->mProfile)) { 566 outputDesc = desc; 567 // reuse direct output if currently open and configured with same parameters 568 if ((samplingRate == outputDesc->mSamplingRate) && 569 (format == outputDesc->mFormat) && 570 (channelMask == outputDesc->mChannelMask)) { 571 outputDesc->mDirectOpenCount++; 572 ALOGV("getOutput() reusing direct output %d", output); 573 return mOutputs.keyAt(i); 574 } 575 } 576 } 577 // close direct output if currently open and configured with different parameters 578 if (outputDesc != NULL) { 579 closeOutput(outputDesc->mId); 580 } 581 outputDesc = new AudioOutputDescriptor(profile); 582 outputDesc->mDevice = device; 583 outputDesc->mSamplingRate = samplingRate; 584 outputDesc->mFormat = (audio_format_t)format; 585 outputDesc->mChannelMask = (audio_channel_mask_t)channelMask; 586 outputDesc->mLatency = 0; 587 outputDesc->mFlags =(audio_output_flags_t) 588 (outputDesc->mFlags | flags | AUDIO_OUTPUT_FLAG_DIRECT); 589 outputDesc->mRefCount[stream] = 0; 590 outputDesc->mStopTime[stream] = 0; 591 outputDesc->mDirectOpenCount = 1; 592 output = mpClientInterface->openOutput(profile->mModule->mHandle, 593 &outputDesc->mDevice, 594 &outputDesc->mSamplingRate, 595 &outputDesc->mFormat, 596 &outputDesc->mChannelMask, 597 &outputDesc->mLatency, 598 outputDesc->mFlags); 599 600 // only accept an output with the requested parameters 601 if (output == 0 || 602 (samplingRate != 0 && samplingRate != outputDesc->mSamplingRate) || 603 (format != 0 && format != outputDesc->mFormat) || 604 (channelMask != 0 && channelMask != outputDesc->mChannelMask)) { 605 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d," 606 "format %d %d, channelMask %04x %04x", output, samplingRate, 607 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask, 608 outputDesc->mChannelMask); 609 if (output != 0) { 610 mpClientInterface->closeOutput(output); 611 } 612 delete outputDesc; 613 return 0; 614 } 615 addOutput(output, outputDesc); 616 mPreviousOutputs = mOutputs; 617 ALOGV("getOutput() returns new direct output %d", output); 618 return output; 619 } 620 621 // ignoring channel mask due to downmix capability in mixer 622 623 // open a non direct output 624 625 // get which output is suitable for the specified stream. The actual routing change will happen 626 // when startOutput() will be called 627 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); 628 629 output = selectOutput(outputs, flags); 630 631 ALOGW_IF((output ==0), "getOutput() could not find output for stream %d, samplingRate %d," 632 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags); 633 634 ALOGV("getOutput() returns output %d", output); 635 636 return output; 637} 638 639audio_io_handle_t AudioPolicyManagerBase::selectOutput(const SortedVector<audio_io_handle_t>& outputs, 640 AudioSystem::output_flags flags) 641{ 642 // select one output among several that provide a path to a particular device or set of 643 // devices (the list was previously build by getOutputsForDevice()). 644 // The priority is as follows: 645 // 1: the output with the highest number of requested policy flags 646 // 2: the primary output 647 // 3: the first output in the list 648 649 if (outputs.size() == 0) { 650 return 0; 651 } 652 if (outputs.size() == 1) { 653 return outputs[0]; 654 } 655 656 int maxCommonFlags = 0; 657 audio_io_handle_t outputFlags = 0; 658 audio_io_handle_t outputPrimary = 0; 659 660 for (size_t i = 0; i < outputs.size(); i++) { 661 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(outputs[i]); 662 if (!outputDesc->isDuplicated()) { 663 int commonFlags = (int)AudioSystem::popCount(outputDesc->mProfile->mFlags & flags); 664 if (commonFlags > maxCommonFlags) { 665 outputFlags = outputs[i]; 666 maxCommonFlags = commonFlags; 667 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags); 668 } 669 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) { 670 outputPrimary = outputs[i]; 671 } 672 } 673 } 674 675 if (outputFlags != 0) { 676 return outputFlags; 677 } 678 if (outputPrimary != 0) { 679 return outputPrimary; 680 } 681 682 return outputs[0]; 683} 684 685status_t AudioPolicyManagerBase::startOutput(audio_io_handle_t output, 686 AudioSystem::stream_type stream, 687 int session) 688{ 689 ALOGV("startOutput() output %d, stream %d, session %d", output, stream, session); 690 ssize_t index = mOutputs.indexOfKey(output); 691 if (index < 0) { 692 ALOGW("startOutput() unknow output %d", output); 693 return BAD_VALUE; 694 } 695 696 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index); 697 698 // increment usage count for this stream on the requested output: 699 // NOTE that the usage count is the same for duplicated output and hardware output which is 700 // necessary for a correct control of hardware output routing by startOutput() and stopOutput() 701 outputDesc->changeRefCount(stream, 1); 702 703 if (outputDesc->mRefCount[stream] == 1) { 704 audio_devices_t newDevice = getNewDevice(output, false /*fromCache*/); 705 routing_strategy strategy = getStrategy(stream); 706 bool shouldWait = (strategy == STRATEGY_SONIFICATION) || 707 (strategy == STRATEGY_SONIFICATION_RESPECTFUL); 708 uint32_t waitMs = 0; 709 bool force = false; 710 for (size_t i = 0; i < mOutputs.size(); i++) { 711 AudioOutputDescriptor *desc = mOutputs.valueAt(i); 712 if (desc != outputDesc) { 713 // force a device change if any other output is managed by the same hw 714 // module and has a current device selection that differs from selected device. 715 // In this case, the audio HAL must receive the new device selection so that it can 716 // change the device currently selected by the other active output. 717 if (outputDesc->sharesHwModuleWith(desc) && 718 desc->device() != newDevice) { 719 force = true; 720 } 721 // wait for audio on other active outputs to be presented when starting 722 // a notification so that audio focus effect can propagate. 723 uint32_t latency = desc->latency(); 724 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) { 725 waitMs = latency; 726 } 727 } 728 } 729 uint32_t muteWaitMs = setOutputDevice(output, newDevice, force); 730 731 // handle special case for sonification while in call 732 if (isInCall()) { 733 handleIncallSonification(stream, true, false); 734 } 735 736 // apply volume rules for current stream and device if necessary 737 checkAndSetVolume(stream, 738 mStreams[stream].getVolumeIndex(newDevice), 739 output, 740 newDevice); 741 742 // update the outputs if starting an output with a stream that can affect notification 743 // routing 744 handleNotificationRoutingForStream(stream); 745 if (waitMs > muteWaitMs) { 746 usleep((waitMs - muteWaitMs) * 2 * 1000); 747 } 748 } 749 return NO_ERROR; 750} 751 752 753status_t AudioPolicyManagerBase::stopOutput(audio_io_handle_t output, 754 AudioSystem::stream_type stream, 755 int session) 756{ 757 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session); 758 ssize_t index = mOutputs.indexOfKey(output); 759 if (index < 0) { 760 ALOGW("stopOutput() unknow output %d", output); 761 return BAD_VALUE; 762 } 763 764 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index); 765 766 // handle special case for sonification while in call 767 if (isInCall()) { 768 handleIncallSonification(stream, false, false); 769 } 770 771 if (outputDesc->mRefCount[stream] > 0) { 772 // decrement usage count of this stream on the output 773 outputDesc->changeRefCount(stream, -1); 774 // store time at which the stream was stopped - see isStreamActive() 775 if (outputDesc->mRefCount[stream] == 0) { 776 outputDesc->mStopTime[stream] = systemTime(); 777 audio_devices_t newDevice = getNewDevice(output, false /*fromCache*/); 778 // delay the device switch by twice the latency because stopOutput() is executed when 779 // the track stop() command is received and at that time the audio track buffer can 780 // still contain data that needs to be drained. The latency only covers the audio HAL 781 // and kernel buffers. Also the latency does not always include additional delay in the 782 // audio path (audio DSP, CODEC ...) 783 setOutputDevice(output, newDevice, false, outputDesc->mLatency*2); 784 785 // force restoring the device selection on other active outputs if it differs from the 786 // one being selected for this output 787 for (size_t i = 0; i < mOutputs.size(); i++) { 788 audio_io_handle_t curOutput = mOutputs.keyAt(i); 789 AudioOutputDescriptor *desc = mOutputs.valueAt(i); 790 if (curOutput != output && 791 desc->isActive() && 792 outputDesc->sharesHwModuleWith(desc) && 793 newDevice != desc->device()) { 794 setOutputDevice(curOutput, 795 getNewDevice(curOutput, false /*fromCache*/), 796 true, 797 outputDesc->mLatency*2); 798 } 799 } 800 // update the outputs if stopping one with a stream that can affect notification routing 801 handleNotificationRoutingForStream(stream); 802 } 803 return NO_ERROR; 804 } else { 805 ALOGW("stopOutput() refcount is already 0 for output %d", output); 806 return INVALID_OPERATION; 807 } 808} 809 810void AudioPolicyManagerBase::releaseOutput(audio_io_handle_t output) 811{ 812 ALOGV("releaseOutput() %d", output); 813 ssize_t index = mOutputs.indexOfKey(output); 814 if (index < 0) { 815 ALOGW("releaseOutput() releasing unknown output %d", output); 816 return; 817 } 818 819#ifdef AUDIO_POLICY_TEST 820 int testIndex = testOutputIndex(output); 821 if (testIndex != 0) { 822 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index); 823 if (outputDesc->isActive()) { 824 mpClientInterface->closeOutput(output); 825 delete mOutputs.valueAt(index); 826 mOutputs.removeItem(output); 827 mTestOutputs[testIndex] = 0; 828 } 829 return; 830 } 831#endif //AUDIO_POLICY_TEST 832 833 AudioOutputDescriptor *desc = mOutputs.valueAt(index); 834 if (desc->mFlags & AudioSystem::OUTPUT_FLAG_DIRECT) { 835 if (desc->mDirectOpenCount <= 0) { 836 ALOGW("releaseOutput() invalid open count %d for output %d", 837 desc->mDirectOpenCount, output); 838 return; 839 } 840 if (--desc->mDirectOpenCount == 0) { 841 closeOutput(output); 842 } 843 } 844 845} 846 847audio_io_handle_t AudioPolicyManagerBase::getInput(int inputSource, 848 uint32_t samplingRate, 849 uint32_t format, 850 uint32_t channelMask, 851 AudioSystem::audio_in_acoustics acoustics) 852{ 853 audio_io_handle_t input = 0; 854 audio_devices_t device = getDeviceForInputSource(inputSource); 855 856 ALOGV("getInput() inputSource %d, samplingRate %d, format %d, channelMask %x, acoustics %x", 857 inputSource, samplingRate, format, channelMask, acoustics); 858 859 if (device == AUDIO_DEVICE_NONE) { 860 ALOGW("getInput() could not find device for inputSource %d", inputSource); 861 return 0; 862 } 863 864 // adapt channel selection to input source 865 switch(inputSource) { 866 case AUDIO_SOURCE_VOICE_UPLINK: 867 channelMask = AudioSystem::CHANNEL_IN_VOICE_UPLINK; 868 break; 869 case AUDIO_SOURCE_VOICE_DOWNLINK: 870 channelMask = AudioSystem::CHANNEL_IN_VOICE_DNLINK; 871 break; 872 case AUDIO_SOURCE_VOICE_CALL: 873 channelMask = (AudioSystem::CHANNEL_IN_VOICE_UPLINK | AudioSystem::CHANNEL_IN_VOICE_DNLINK); 874 break; 875 default: 876 break; 877 } 878 879 IOProfile *profile = getInputProfile(device, 880 samplingRate, 881 format, 882 channelMask); 883 if (profile == NULL) { 884 ALOGW("getInput() could not find profile for device %04x, samplingRate %d, format %d," 885 "channelMask %04x", 886 device, samplingRate, format, channelMask); 887 return 0; 888 } 889 890 if (profile->mModule->mHandle == 0) { 891 ALOGE("getInput(): HW module %s not opened", profile->mModule->mName); 892 return 0; 893 } 894 895 AudioInputDescriptor *inputDesc = new AudioInputDescriptor(profile); 896 897 inputDesc->mInputSource = inputSource; 898 inputDesc->mDevice = device; 899 inputDesc->mSamplingRate = samplingRate; 900 inputDesc->mFormat = (audio_format_t)format; 901 inputDesc->mChannelMask = (audio_channel_mask_t)channelMask; 902 inputDesc->mRefCount = 0; 903 input = mpClientInterface->openInput(profile->mModule->mHandle, 904 &inputDesc->mDevice, 905 &inputDesc->mSamplingRate, 906 &inputDesc->mFormat, 907 &inputDesc->mChannelMask); 908 909 // only accept input with the exact requested set of parameters 910 if (input == 0 || 911 (samplingRate != inputDesc->mSamplingRate) || 912 (format != inputDesc->mFormat) || 913 (channelMask != inputDesc->mChannelMask)) { 914 ALOGV("getInput() failed opening input: samplingRate %d, format %d, channelMask %d", 915 samplingRate, format, channelMask); 916 if (input != 0) { 917 mpClientInterface->closeInput(input); 918 } 919 delete inputDesc; 920 return 0; 921 } 922 mInputs.add(input, inputDesc); 923 return input; 924} 925 926status_t AudioPolicyManagerBase::startInput(audio_io_handle_t input) 927{ 928 ALOGV("startInput() input %d", input); 929 ssize_t index = mInputs.indexOfKey(input); 930 if (index < 0) { 931 ALOGW("startInput() unknow input %d", input); 932 return BAD_VALUE; 933 } 934 AudioInputDescriptor *inputDesc = mInputs.valueAt(index); 935 936#ifdef AUDIO_POLICY_TEST 937 if (mTestInput == 0) 938#endif //AUDIO_POLICY_TEST 939 { 940 // refuse 2 active AudioRecord clients at the same time 941 if (getActiveInput() != 0) { 942 ALOGW("startInput() input %d failed: other input already started", input); 943 return INVALID_OPERATION; 944 } 945 } 946 947 audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource); 948 if ((newDevice != AUDIO_DEVICE_NONE) && (newDevice != inputDesc->mDevice)) { 949 inputDesc->mDevice = newDevice; 950 } 951 AudioParameter param = AudioParameter(); 952 param.addInt(String8(AudioParameter::keyRouting), (int)inputDesc->mDevice); 953 954 param.addInt(String8(AudioParameter::keyInputSource), (int)inputDesc->mInputSource); 955 ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource); 956 957 mpClientInterface->setParameters(input, param.toString()); 958 959 inputDesc->mRefCount = 1; 960 return NO_ERROR; 961} 962 963status_t AudioPolicyManagerBase::stopInput(audio_io_handle_t input) 964{ 965 ALOGV("stopInput() input %d", input); 966 ssize_t index = mInputs.indexOfKey(input); 967 if (index < 0) { 968 ALOGW("stopInput() unknow input %d", input); 969 return BAD_VALUE; 970 } 971 AudioInputDescriptor *inputDesc = mInputs.valueAt(index); 972 973 if (inputDesc->mRefCount == 0) { 974 ALOGW("stopInput() input %d already stopped", input); 975 return INVALID_OPERATION; 976 } else { 977 AudioParameter param = AudioParameter(); 978 param.addInt(String8(AudioParameter::keyRouting), 0); 979 mpClientInterface->setParameters(input, param.toString()); 980 inputDesc->mRefCount = 0; 981 return NO_ERROR; 982 } 983} 984 985void AudioPolicyManagerBase::releaseInput(audio_io_handle_t input) 986{ 987 ALOGV("releaseInput() %d", input); 988 ssize_t index = mInputs.indexOfKey(input); 989 if (index < 0) { 990 ALOGW("releaseInput() releasing unknown input %d", input); 991 return; 992 } 993 mpClientInterface->closeInput(input); 994 delete mInputs.valueAt(index); 995 mInputs.removeItem(input); 996 ALOGV("releaseInput() exit"); 997} 998 999void AudioPolicyManagerBase::initStreamVolume(AudioSystem::stream_type stream, 1000 int indexMin, 1001 int indexMax) 1002{ 1003 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); 1004 if (indexMin < 0 || indexMin >= indexMax) { 1005 ALOGW("initStreamVolume() invalid index limits for stream %d, min %d, max %d", stream , indexMin, indexMax); 1006 return; 1007 } 1008 mStreams[stream].mIndexMin = indexMin; 1009 mStreams[stream].mIndexMax = indexMax; 1010} 1011 1012status_t AudioPolicyManagerBase::setStreamVolumeIndex(AudioSystem::stream_type stream, 1013 int index, 1014 audio_devices_t device) 1015{ 1016 1017 if ((index < mStreams[stream].mIndexMin) || (index > mStreams[stream].mIndexMax)) { 1018 return BAD_VALUE; 1019 } 1020 if (!audio_is_output_device(device)) { 1021 return BAD_VALUE; 1022 } 1023 1024 // Force max volume if stream cannot be muted 1025 if (!mStreams[stream].mCanBeMuted) index = mStreams[stream].mIndexMax; 1026 1027 ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d", 1028 stream, device, index); 1029 1030 // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and 1031 // clear all device specific values 1032 if (device == AUDIO_DEVICE_OUT_DEFAULT) { 1033 mStreams[stream].mIndexCur.clear(); 1034 } 1035 mStreams[stream].mIndexCur.add(device, index); 1036 1037 // compute and apply stream volume on all outputs according to connected device 1038 status_t status = NO_ERROR; 1039 for (size_t i = 0; i < mOutputs.size(); i++) { 1040 audio_devices_t curDevice = 1041 getDeviceForVolume(mOutputs.valueAt(i)->device()); 1042 if (device == curDevice) { 1043 status_t volStatus = checkAndSetVolume(stream, index, mOutputs.keyAt(i), curDevice); 1044 if (volStatus != NO_ERROR) { 1045 status = volStatus; 1046 } 1047 } 1048 } 1049 return status; 1050} 1051 1052status_t AudioPolicyManagerBase::getStreamVolumeIndex(AudioSystem::stream_type stream, 1053 int *index, 1054 audio_devices_t device) 1055{ 1056 if (index == NULL) { 1057 return BAD_VALUE; 1058 } 1059 if (!audio_is_output_device(device)) { 1060 return BAD_VALUE; 1061 } 1062 // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to 1063 // the strategy the stream belongs to. 1064 if (device == AUDIO_DEVICE_OUT_DEFAULT) { 1065 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); 1066 } 1067 device = getDeviceForVolume(device); 1068 1069 *index = mStreams[stream].getVolumeIndex(device); 1070 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index); 1071 return NO_ERROR; 1072} 1073 1074audio_io_handle_t AudioPolicyManagerBase::getOutputForEffect(const effect_descriptor_t *desc) 1075{ 1076 ALOGV("getOutputForEffect()"); 1077 // apply simple rule where global effects are attached to the same output as MUSIC streams 1078 1079 routing_strategy strategy = getStrategy(AudioSystem::MUSIC); 1080 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); 1081 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs); 1082 int outIdx = 0; 1083 for (size_t i = 0; i < dstOutputs.size(); i++) { 1084 AudioOutputDescriptor *desc = mOutputs.valueFor(dstOutputs[i]); 1085 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) { 1086 outIdx = i; 1087 } 1088 } 1089 return dstOutputs[outIdx]; 1090} 1091 1092status_t AudioPolicyManagerBase::registerEffect(const effect_descriptor_t *desc, 1093 audio_io_handle_t io, 1094 uint32_t strategy, 1095 int session, 1096 int id) 1097{ 1098 ssize_t index = mOutputs.indexOfKey(io); 1099 if (index < 0) { 1100 index = mInputs.indexOfKey(io); 1101 if (index < 0) { 1102 ALOGW("registerEffect() unknown io %d", io); 1103 return INVALID_OPERATION; 1104 } 1105 } 1106 1107 if (mTotalEffectsMemory + desc->memoryUsage > getMaxEffectsMemory()) { 1108 ALOGW("registerEffect() memory limit exceeded for Fx %s, Memory %d KB", 1109 desc->name, desc->memoryUsage); 1110 return INVALID_OPERATION; 1111 } 1112 mTotalEffectsMemory += desc->memoryUsage; 1113 ALOGV("registerEffect() effect %s, io %d, strategy %d session %d id %d", 1114 desc->name, io, strategy, session, id); 1115 ALOGV("registerEffect() memory %d, total memory %d", desc->memoryUsage, mTotalEffectsMemory); 1116 1117 EffectDescriptor *pDesc = new EffectDescriptor(); 1118 memcpy (&pDesc->mDesc, desc, sizeof(effect_descriptor_t)); 1119 pDesc->mIo = io; 1120 pDesc->mStrategy = (routing_strategy)strategy; 1121 pDesc->mSession = session; 1122 pDesc->mEnabled = false; 1123 1124 mEffects.add(id, pDesc); 1125 1126 return NO_ERROR; 1127} 1128 1129status_t AudioPolicyManagerBase::unregisterEffect(int id) 1130{ 1131 ssize_t index = mEffects.indexOfKey(id); 1132 if (index < 0) { 1133 ALOGW("unregisterEffect() unknown effect ID %d", id); 1134 return INVALID_OPERATION; 1135 } 1136 1137 EffectDescriptor *pDesc = mEffects.valueAt(index); 1138 1139 setEffectEnabled(pDesc, false); 1140 1141 if (mTotalEffectsMemory < pDesc->mDesc.memoryUsage) { 1142 ALOGW("unregisterEffect() memory %d too big for total %d", 1143 pDesc->mDesc.memoryUsage, mTotalEffectsMemory); 1144 pDesc->mDesc.memoryUsage = mTotalEffectsMemory; 1145 } 1146 mTotalEffectsMemory -= pDesc->mDesc.memoryUsage; 1147 ALOGV("unregisterEffect() effect %s, ID %d, memory %d total memory %d", 1148 pDesc->mDesc.name, id, pDesc->mDesc.memoryUsage, mTotalEffectsMemory); 1149 1150 mEffects.removeItem(id); 1151 delete pDesc; 1152 1153 return NO_ERROR; 1154} 1155 1156status_t AudioPolicyManagerBase::setEffectEnabled(int id, bool enabled) 1157{ 1158 ssize_t index = mEffects.indexOfKey(id); 1159 if (index < 0) { 1160 ALOGW("unregisterEffect() unknown effect ID %d", id); 1161 return INVALID_OPERATION; 1162 } 1163 1164 return setEffectEnabled(mEffects.valueAt(index), enabled); 1165} 1166 1167status_t AudioPolicyManagerBase::setEffectEnabled(EffectDescriptor *pDesc, bool enabled) 1168{ 1169 if (enabled == pDesc->mEnabled) { 1170 ALOGV("setEffectEnabled(%s) effect already %s", 1171 enabled?"true":"false", enabled?"enabled":"disabled"); 1172 return INVALID_OPERATION; 1173 } 1174 1175 if (enabled) { 1176 if (mTotalEffectsCpuLoad + pDesc->mDesc.cpuLoad > getMaxEffectsCpuLoad()) { 1177 ALOGW("setEffectEnabled(true) CPU Load limit exceeded for Fx %s, CPU %f MIPS", 1178 pDesc->mDesc.name, (float)pDesc->mDesc.cpuLoad/10); 1179 return INVALID_OPERATION; 1180 } 1181 mTotalEffectsCpuLoad += pDesc->mDesc.cpuLoad; 1182 ALOGV("setEffectEnabled(true) total CPU %d", mTotalEffectsCpuLoad); 1183 } else { 1184 if (mTotalEffectsCpuLoad < pDesc->mDesc.cpuLoad) { 1185 ALOGW("setEffectEnabled(false) CPU load %d too high for total %d", 1186 pDesc->mDesc.cpuLoad, mTotalEffectsCpuLoad); 1187 pDesc->mDesc.cpuLoad = mTotalEffectsCpuLoad; 1188 } 1189 mTotalEffectsCpuLoad -= pDesc->mDesc.cpuLoad; 1190 ALOGV("setEffectEnabled(false) total CPU %d", mTotalEffectsCpuLoad); 1191 } 1192 pDesc->mEnabled = enabled; 1193 return NO_ERROR; 1194} 1195 1196bool AudioPolicyManagerBase::isStreamActive(int stream, uint32_t inPastMs) const 1197{ 1198 nsecs_t sysTime = systemTime(); 1199 for (size_t i = 0; i < mOutputs.size(); i++) { 1200 const AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i); 1201 if (outputDesc->isStreamActive((AudioSystem::stream_type)stream, inPastMs, sysTime)) { 1202 return true; 1203 } 1204 } 1205 return false; 1206} 1207 1208bool AudioPolicyManagerBase::isStreamActiveRemotely(int stream, uint32_t inPastMs) const 1209{ 1210 nsecs_t sysTime = systemTime(); 1211 for (size_t i = 0; i < mOutputs.size(); i++) { 1212 const AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i); 1213 if (((outputDesc->device() & APM_AUDIO_OUT_DEVICE_REMOTE_ALL) != 0) && 1214 outputDesc->isStreamActive((AudioSystem::stream_type)stream, inPastMs, sysTime)) { 1215 return true; 1216 } 1217 } 1218 return false; 1219} 1220 1221bool AudioPolicyManagerBase::isSourceActive(audio_source_t source) const 1222{ 1223 for (size_t i = 0; i < mInputs.size(); i++) { 1224 const AudioInputDescriptor * inputDescriptor = mInputs.valueAt(i); 1225 if ((inputDescriptor->mInputSource == (int) source) 1226 && (inputDescriptor->mRefCount > 0)) { 1227 return true; 1228 } 1229 } 1230 return false; 1231} 1232 1233 1234 1235status_t AudioPolicyManagerBase::dump(int fd) 1236{ 1237 const size_t SIZE = 256; 1238 char buffer[SIZE]; 1239 String8 result; 1240 1241 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this); 1242 result.append(buffer); 1243 1244 snprintf(buffer, SIZE, " Primary Output: %d\n", mPrimaryOutput); 1245 result.append(buffer); 1246 snprintf(buffer, SIZE, " A2DP device address: %s\n", mA2dpDeviceAddress.string()); 1247 result.append(buffer); 1248 snprintf(buffer, SIZE, " SCO device address: %s\n", mScoDeviceAddress.string()); 1249 result.append(buffer); 1250 snprintf(buffer, SIZE, " USB audio ALSA %s\n", mUsbCardAndDevice.string()); 1251 result.append(buffer); 1252 snprintf(buffer, SIZE, " Output devices: %08x\n", mAvailableOutputDevices); 1253 result.append(buffer); 1254 snprintf(buffer, SIZE, " Input devices: %08x\n", mAvailableInputDevices); 1255 result.append(buffer); 1256 snprintf(buffer, SIZE, " Phone state: %d\n", mPhoneState); 1257 result.append(buffer); 1258 snprintf(buffer, SIZE, " Force use for communications %d\n", mForceUse[AudioSystem::FOR_COMMUNICATION]); 1259 result.append(buffer); 1260 snprintf(buffer, SIZE, " Force use for media %d\n", mForceUse[AudioSystem::FOR_MEDIA]); 1261 result.append(buffer); 1262 snprintf(buffer, SIZE, " Force use for record %d\n", mForceUse[AudioSystem::FOR_RECORD]); 1263 result.append(buffer); 1264 snprintf(buffer, SIZE, " Force use for dock %d\n", mForceUse[AudioSystem::FOR_DOCK]); 1265 result.append(buffer); 1266 snprintf(buffer, SIZE, " Force use for system %d\n", mForceUse[AudioSystem::FOR_SYSTEM]); 1267 result.append(buffer); 1268 write(fd, result.string(), result.size()); 1269 1270 1271 snprintf(buffer, SIZE, "\nHW Modules dump:\n"); 1272 write(fd, buffer, strlen(buffer)); 1273 for (size_t i = 0; i < mHwModules.size(); i++) { 1274 snprintf(buffer, SIZE, "- HW Module %d:\n", i + 1); 1275 write(fd, buffer, strlen(buffer)); 1276 mHwModules[i]->dump(fd); 1277 } 1278 1279 snprintf(buffer, SIZE, "\nOutputs dump:\n"); 1280 write(fd, buffer, strlen(buffer)); 1281 for (size_t i = 0; i < mOutputs.size(); i++) { 1282 snprintf(buffer, SIZE, "- Output %d dump:\n", mOutputs.keyAt(i)); 1283 write(fd, buffer, strlen(buffer)); 1284 mOutputs.valueAt(i)->dump(fd); 1285 } 1286 1287 snprintf(buffer, SIZE, "\nInputs dump:\n"); 1288 write(fd, buffer, strlen(buffer)); 1289 for (size_t i = 0; i < mInputs.size(); i++) { 1290 snprintf(buffer, SIZE, "- Input %d dump:\n", mInputs.keyAt(i)); 1291 write(fd, buffer, strlen(buffer)); 1292 mInputs.valueAt(i)->dump(fd); 1293 } 1294 1295 snprintf(buffer, SIZE, "\nStreams dump:\n"); 1296 write(fd, buffer, strlen(buffer)); 1297 snprintf(buffer, SIZE, 1298 " Stream Can be muted Index Min Index Max Index Cur [device : index]...\n"); 1299 write(fd, buffer, strlen(buffer)); 1300 for (size_t i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) { 1301 snprintf(buffer, SIZE, " %02d ", i); 1302 write(fd, buffer, strlen(buffer)); 1303 mStreams[i].dump(fd); 1304 } 1305 1306 snprintf(buffer, SIZE, "\nTotal Effects CPU: %f MIPS, Total Effects memory: %d KB\n", 1307 (float)mTotalEffectsCpuLoad/10, mTotalEffectsMemory); 1308 write(fd, buffer, strlen(buffer)); 1309 1310 snprintf(buffer, SIZE, "Registered effects:\n"); 1311 write(fd, buffer, strlen(buffer)); 1312 for (size_t i = 0; i < mEffects.size(); i++) { 1313 snprintf(buffer, SIZE, "- Effect %d dump:\n", mEffects.keyAt(i)); 1314 write(fd, buffer, strlen(buffer)); 1315 mEffects.valueAt(i)->dump(fd); 1316 } 1317 1318 1319 return NO_ERROR; 1320} 1321 1322bool AudioPolicyManagerBase::isOffloadSupported(const audio_offload_info_t& offloadInfo) 1323{ 1324 // Stub implementation 1325 return false; 1326} 1327 1328// ---------------------------------------------------------------------------- 1329// AudioPolicyManagerBase 1330// ---------------------------------------------------------------------------- 1331 1332AudioPolicyManagerBase::AudioPolicyManagerBase(AudioPolicyClientInterface *clientInterface) 1333 : 1334#ifdef AUDIO_POLICY_TEST 1335 Thread(false), 1336#endif //AUDIO_POLICY_TEST 1337 mPrimaryOutput((audio_io_handle_t)0), 1338 mAvailableOutputDevices(AUDIO_DEVICE_NONE), 1339 mPhoneState(AudioSystem::MODE_NORMAL), 1340 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), 1341 mTotalEffectsCpuLoad(0), mTotalEffectsMemory(0), 1342 mA2dpSuspended(false), mHasA2dp(false), mHasUsb(false), mHasRemoteSubmix(false) 1343{ 1344 mpClientInterface = clientInterface; 1345 1346 for (int i = 0; i < AudioSystem::NUM_FORCE_USE; i++) { 1347 mForceUse[i] = AudioSystem::FORCE_NONE; 1348 } 1349 1350 initializeVolumeCurves(); 1351 1352 mA2dpDeviceAddress = String8(""); 1353 mScoDeviceAddress = String8(""); 1354 mUsbCardAndDevice = String8(""); 1355 1356 if (loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE) != NO_ERROR) { 1357 if (loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE) != NO_ERROR) { 1358 ALOGE("could not load audio policy configuration file, setting defaults"); 1359 defaultAudioPolicyConfig(); 1360 } 1361 } 1362 1363 // open all output streams needed to access attached devices 1364 for (size_t i = 0; i < mHwModules.size(); i++) { 1365 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName); 1366 if (mHwModules[i]->mHandle == 0) { 1367 ALOGW("could not open HW module %s", mHwModules[i]->mName); 1368 continue; 1369 } 1370 // open all output streams needed to access attached devices 1371 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) 1372 { 1373 const IOProfile *outProfile = mHwModules[i]->mOutputProfiles[j]; 1374 1375 if (outProfile->mSupportedDevices & mAttachedOutputDevices) { 1376 AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(outProfile); 1377 outputDesc->mDevice = (audio_devices_t)(mDefaultOutputDevice & 1378 outProfile->mSupportedDevices); 1379 audio_io_handle_t output = mpClientInterface->openOutput( 1380 outProfile->mModule->mHandle, 1381 &outputDesc->mDevice, 1382 &outputDesc->mSamplingRate, 1383 &outputDesc->mFormat, 1384 &outputDesc->mChannelMask, 1385 &outputDesc->mLatency, 1386 outputDesc->mFlags); 1387 if (output == 0) { 1388 delete outputDesc; 1389 } else { 1390 mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices | 1391 (outProfile->mSupportedDevices & mAttachedOutputDevices)); 1392 if (mPrimaryOutput == 0 && 1393 outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) { 1394 mPrimaryOutput = output; 1395 } 1396 addOutput(output, outputDesc); 1397 setOutputDevice(output, 1398 (audio_devices_t)(mDefaultOutputDevice & 1399 outProfile->mSupportedDevices), 1400 true); 1401 } 1402 } 1403 } 1404 } 1405 1406 ALOGE_IF((mAttachedOutputDevices & ~mAvailableOutputDevices), 1407 "Not output found for attached devices %08x", 1408 (mAttachedOutputDevices & ~mAvailableOutputDevices)); 1409 1410 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output"); 1411 1412 updateDevicesAndOutputs(); 1413 1414#ifdef AUDIO_POLICY_TEST 1415 if (mPrimaryOutput != 0) { 1416 AudioParameter outputCmd = AudioParameter(); 1417 outputCmd.addInt(String8("set_id"), 0); 1418 mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString()); 1419 1420 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER; 1421 mTestSamplingRate = 44100; 1422 mTestFormat = AudioSystem::PCM_16_BIT; 1423 mTestChannels = AudioSystem::CHANNEL_OUT_STEREO; 1424 mTestLatencyMs = 0; 1425 mCurOutput = 0; 1426 mDirectOutput = false; 1427 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { 1428 mTestOutputs[i] = 0; 1429 } 1430 1431 const size_t SIZE = 256; 1432 char buffer[SIZE]; 1433 snprintf(buffer, SIZE, "AudioPolicyManagerTest"); 1434 run(buffer, ANDROID_PRIORITY_AUDIO); 1435 } 1436#endif //AUDIO_POLICY_TEST 1437} 1438 1439AudioPolicyManagerBase::~AudioPolicyManagerBase() 1440{ 1441#ifdef AUDIO_POLICY_TEST 1442 exit(); 1443#endif //AUDIO_POLICY_TEST 1444 for (size_t i = 0; i < mOutputs.size(); i++) { 1445 mpClientInterface->closeOutput(mOutputs.keyAt(i)); 1446 delete mOutputs.valueAt(i); 1447 } 1448 for (size_t i = 0; i < mInputs.size(); i++) { 1449 mpClientInterface->closeInput(mInputs.keyAt(i)); 1450 delete mInputs.valueAt(i); 1451 } 1452 for (size_t i = 0; i < mHwModules.size(); i++) { 1453 delete mHwModules[i]; 1454 } 1455} 1456 1457status_t AudioPolicyManagerBase::initCheck() 1458{ 1459 return (mPrimaryOutput == 0) ? NO_INIT : NO_ERROR; 1460} 1461 1462#ifdef AUDIO_POLICY_TEST 1463bool AudioPolicyManagerBase::threadLoop() 1464{ 1465 ALOGV("entering threadLoop()"); 1466 while (!exitPending()) 1467 { 1468 String8 command; 1469 int valueInt; 1470 String8 value; 1471 1472 Mutex::Autolock _l(mLock); 1473 mWaitWorkCV.waitRelative(mLock, milliseconds(50)); 1474 1475 command = mpClientInterface->getParameters(0, String8("test_cmd_policy")); 1476 AudioParameter param = AudioParameter(command); 1477 1478 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR && 1479 valueInt != 0) { 1480 ALOGV("Test command %s received", command.string()); 1481 String8 target; 1482 if (param.get(String8("target"), target) != NO_ERROR) { 1483 target = "Manager"; 1484 } 1485 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) { 1486 param.remove(String8("test_cmd_policy_output")); 1487 mCurOutput = valueInt; 1488 } 1489 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) { 1490 param.remove(String8("test_cmd_policy_direct")); 1491 if (value == "false") { 1492 mDirectOutput = false; 1493 } else if (value == "true") { 1494 mDirectOutput = true; 1495 } 1496 } 1497 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) { 1498 param.remove(String8("test_cmd_policy_input")); 1499 mTestInput = valueInt; 1500 } 1501 1502 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) { 1503 param.remove(String8("test_cmd_policy_format")); 1504 int format = AudioSystem::INVALID_FORMAT; 1505 if (value == "PCM 16 bits") { 1506 format = AudioSystem::PCM_16_BIT; 1507 } else if (value == "PCM 8 bits") { 1508 format = AudioSystem::PCM_8_BIT; 1509 } else if (value == "Compressed MP3") { 1510 format = AudioSystem::MP3; 1511 } 1512 if (format != AudioSystem::INVALID_FORMAT) { 1513 if (target == "Manager") { 1514 mTestFormat = format; 1515 } else if (mTestOutputs[mCurOutput] != 0) { 1516 AudioParameter outputParam = AudioParameter(); 1517 outputParam.addInt(String8("format"), format); 1518 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); 1519 } 1520 } 1521 } 1522 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) { 1523 param.remove(String8("test_cmd_policy_channels")); 1524 int channels = 0; 1525 1526 if (value == "Channels Stereo") { 1527 channels = AudioSystem::CHANNEL_OUT_STEREO; 1528 } else if (value == "Channels Mono") { 1529 channels = AudioSystem::CHANNEL_OUT_MONO; 1530 } 1531 if (channels != 0) { 1532 if (target == "Manager") { 1533 mTestChannels = channels; 1534 } else if (mTestOutputs[mCurOutput] != 0) { 1535 AudioParameter outputParam = AudioParameter(); 1536 outputParam.addInt(String8("channels"), channels); 1537 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); 1538 } 1539 } 1540 } 1541 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) { 1542 param.remove(String8("test_cmd_policy_sampleRate")); 1543 if (valueInt >= 0 && valueInt <= 96000) { 1544 int samplingRate = valueInt; 1545 if (target == "Manager") { 1546 mTestSamplingRate = samplingRate; 1547 } else if (mTestOutputs[mCurOutput] != 0) { 1548 AudioParameter outputParam = AudioParameter(); 1549 outputParam.addInt(String8("sampling_rate"), samplingRate); 1550 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); 1551 } 1552 } 1553 } 1554 1555 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) { 1556 param.remove(String8("test_cmd_policy_reopen")); 1557 1558 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(mPrimaryOutput); 1559 mpClientInterface->closeOutput(mPrimaryOutput); 1560 1561 audio_module_handle_t moduleHandle = outputDesc->mModule->mHandle; 1562 1563 delete mOutputs.valueFor(mPrimaryOutput); 1564 mOutputs.removeItem(mPrimaryOutput); 1565 1566 AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(NULL); 1567 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER; 1568 mPrimaryOutput = mpClientInterface->openOutput(moduleHandle, 1569 &outputDesc->mDevice, 1570 &outputDesc->mSamplingRate, 1571 &outputDesc->mFormat, 1572 &outputDesc->mChannelMask, 1573 &outputDesc->mLatency, 1574 outputDesc->mFlags); 1575 if (mPrimaryOutput == 0) { 1576 ALOGE("Failed to reopen hardware output stream, samplingRate: %d, format %d, channels %d", 1577 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask); 1578 } else { 1579 AudioParameter outputCmd = AudioParameter(); 1580 outputCmd.addInt(String8("set_id"), 0); 1581 mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString()); 1582 addOutput(mPrimaryOutput, outputDesc); 1583 } 1584 } 1585 1586 1587 mpClientInterface->setParameters(0, String8("test_cmd_policy=")); 1588 } 1589 } 1590 return false; 1591} 1592 1593void AudioPolicyManagerBase::exit() 1594{ 1595 { 1596 AutoMutex _l(mLock); 1597 requestExit(); 1598 mWaitWorkCV.signal(); 1599 } 1600 requestExitAndWait(); 1601} 1602 1603int AudioPolicyManagerBase::testOutputIndex(audio_io_handle_t output) 1604{ 1605 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { 1606 if (output == mTestOutputs[i]) return i; 1607 } 1608 return 0; 1609} 1610#endif //AUDIO_POLICY_TEST 1611 1612// --- 1613 1614void AudioPolicyManagerBase::addOutput(audio_io_handle_t id, AudioOutputDescriptor *outputDesc) 1615{ 1616 outputDesc->mId = id; 1617 mOutputs.add(id, outputDesc); 1618} 1619 1620 1621status_t AudioPolicyManagerBase::checkOutputsForDevice(audio_devices_t device, 1622 AudioSystem::device_connection_state state, 1623 SortedVector<audio_io_handle_t>& outputs) 1624{ 1625 AudioOutputDescriptor *desc; 1626 1627 if (state == AudioSystem::DEVICE_STATE_AVAILABLE) { 1628 // first list already open outputs that can be routed to this device 1629 for (size_t i = 0; i < mOutputs.size(); i++) { 1630 desc = mOutputs.valueAt(i); 1631 if (!desc->isDuplicated() && (desc->mProfile->mSupportedDevices & device)) { 1632 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i)); 1633 outputs.add(mOutputs.keyAt(i)); 1634 } 1635 } 1636 // then look for output profiles that can be routed to this device 1637 SortedVector<IOProfile *> profiles; 1638 for (size_t i = 0; i < mHwModules.size(); i++) 1639 { 1640 if (mHwModules[i]->mHandle == 0) { 1641 continue; 1642 } 1643 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) 1644 { 1645 if (mHwModules[i]->mOutputProfiles[j]->mSupportedDevices & device) { 1646 ALOGV("checkOutputsForDevice(): adding profile %d from module %d", j, i); 1647 profiles.add(mHwModules[i]->mOutputProfiles[j]); 1648 } 1649 } 1650 } 1651 1652 if (profiles.isEmpty() && outputs.isEmpty()) { 1653 ALOGW("checkOutputsForDevice(): No output available for device %04x", device); 1654 return BAD_VALUE; 1655 } 1656 1657 // open outputs for matching profiles if needed. Direct outputs are also opened to 1658 // query for dynamic parameters and will be closed later by setDeviceConnectionState() 1659 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { 1660 IOProfile *profile = profiles[profile_index]; 1661 1662 // nothing to do if one output is already opened for this profile 1663 size_t j; 1664 for (j = 0; j < mOutputs.size(); j++) { 1665 desc = mOutputs.valueAt(j); 1666 if (!desc->isDuplicated() && desc->mProfile == profile) { 1667 break; 1668 } 1669 } 1670 if (j != mOutputs.size()) { 1671 continue; 1672 } 1673 1674 ALOGV("opening output for device %08x", device); 1675 desc = new AudioOutputDescriptor(profile); 1676 desc->mDevice = device; 1677 audio_io_handle_t output = mpClientInterface->openOutput(profile->mModule->mHandle, 1678 &desc->mDevice, 1679 &desc->mSamplingRate, 1680 &desc->mFormat, 1681 &desc->mChannelMask, 1682 &desc->mLatency, 1683 desc->mFlags); 1684 if (output != 0) { 1685 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { 1686 String8 reply; 1687 char *value; 1688 if (profile->mSamplingRates[0] == 0) { 1689 reply = mpClientInterface->getParameters(output, 1690 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)); 1691 ALOGV("checkOutputsForDevice() direct output sup sampling rates %s", 1692 reply.string()); 1693 value = strpbrk((char *)reply.string(), "="); 1694 if (value != NULL) { 1695 loadSamplingRates(value + 1, profile); 1696 } 1697 } 1698 if (profile->mFormats[0] == 0) { 1699 reply = mpClientInterface->getParameters(output, 1700 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS)); 1701 ALOGV("checkOutputsForDevice() direct output sup formats %s", 1702 reply.string()); 1703 value = strpbrk((char *)reply.string(), "="); 1704 if (value != NULL) { 1705 loadFormats(value + 1, profile); 1706 } 1707 } 1708 if (profile->mChannelMasks[0] == 0) { 1709 reply = mpClientInterface->getParameters(output, 1710 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS)); 1711 ALOGV("checkOutputsForDevice() direct output sup channel masks %s", 1712 reply.string()); 1713 value = strpbrk((char *)reply.string(), "="); 1714 if (value != NULL) { 1715 loadOutChannels(value + 1, profile); 1716 } 1717 } 1718 if (((profile->mSamplingRates[0] == 0) && 1719 (profile->mSamplingRates.size() < 2)) || 1720 ((profile->mFormats[0] == 0) && 1721 (profile->mFormats.size() < 2)) || 1722 ((profile->mFormats[0] == 0) && 1723 (profile->mChannelMasks.size() < 2))) { 1724 ALOGW("checkOutputsForDevice() direct output missing param"); 1725 mpClientInterface->closeOutput(output); 1726 output = 0; 1727 } else { 1728 addOutput(output, desc); 1729 } 1730 } else { 1731 audio_io_handle_t duplicatedOutput = 0; 1732 // add output descriptor 1733 addOutput(output, desc); 1734 // set initial stream volume for device 1735 applyStreamVolumes(output, device, 0, true); 1736 1737 //TODO: configure audio effect output stage here 1738 1739 // open a duplicating output thread for the new output and the primary output 1740 duplicatedOutput = mpClientInterface->openDuplicateOutput(output, 1741 mPrimaryOutput); 1742 if (duplicatedOutput != 0) { 1743 // add duplicated output descriptor 1744 AudioOutputDescriptor *dupOutputDesc = new AudioOutputDescriptor(NULL); 1745 dupOutputDesc->mOutput1 = mOutputs.valueFor(mPrimaryOutput); 1746 dupOutputDesc->mOutput2 = mOutputs.valueFor(output); 1747 dupOutputDesc->mSamplingRate = desc->mSamplingRate; 1748 dupOutputDesc->mFormat = desc->mFormat; 1749 dupOutputDesc->mChannelMask = desc->mChannelMask; 1750 dupOutputDesc->mLatency = desc->mLatency; 1751 addOutput(duplicatedOutput, dupOutputDesc); 1752 applyStreamVolumes(duplicatedOutput, device, 0, true); 1753 } else { 1754 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", 1755 mPrimaryOutput, output); 1756 mpClientInterface->closeOutput(output); 1757 mOutputs.removeItem(output); 1758 output = 0; 1759 } 1760 } 1761 } 1762 if (output == 0) { 1763 ALOGW("checkOutputsForDevice() could not open output for device %x", device); 1764 delete desc; 1765 profiles.removeAt(profile_index); 1766 profile_index--; 1767 } else { 1768 outputs.add(output); 1769 ALOGV("checkOutputsForDevice(): adding output %d", output); 1770 } 1771 } 1772 1773 if (profiles.isEmpty()) { 1774 ALOGW("checkOutputsForDevice(): No output available for device %04x", device); 1775 return BAD_VALUE; 1776 } 1777 } else { 1778 // check if one opened output is not needed any more after disconnecting one device 1779 for (size_t i = 0; i < mOutputs.size(); i++) { 1780 desc = mOutputs.valueAt(i); 1781 if (!desc->isDuplicated() && 1782 !(desc->mProfile->mSupportedDevices & mAvailableOutputDevices)) { 1783 ALOGV("checkOutputsForDevice(): disconnecting adding output %d", mOutputs.keyAt(i)); 1784 outputs.add(mOutputs.keyAt(i)); 1785 } 1786 } 1787 for (size_t i = 0; i < mHwModules.size(); i++) 1788 { 1789 if (mHwModules[i]->mHandle == 0) { 1790 continue; 1791 } 1792 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) 1793 { 1794 IOProfile *profile = mHwModules[i]->mOutputProfiles[j]; 1795 if ((profile->mSupportedDevices & device) && 1796 (profile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) { 1797 ALOGV("checkOutputsForDevice(): clearing direct output profile %d on module %d", 1798 j, i); 1799 if (profile->mSamplingRates[0] == 0) { 1800 profile->mSamplingRates.clear(); 1801 profile->mSamplingRates.add(0); 1802 } 1803 if (profile->mFormats[0] == 0) { 1804 profile->mFormats.clear(); 1805 profile->mFormats.add((audio_format_t)0); 1806 } 1807 if (profile->mChannelMasks[0] == 0) { 1808 profile->mChannelMasks.clear(); 1809 profile->mChannelMasks.add((audio_channel_mask_t)0); 1810 } 1811 } 1812 } 1813 } 1814 } 1815 return NO_ERROR; 1816} 1817 1818void AudioPolicyManagerBase::closeOutput(audio_io_handle_t output) 1819{ 1820 ALOGV("closeOutput(%d)", output); 1821 1822 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output); 1823 if (outputDesc == NULL) { 1824 ALOGW("closeOutput() unknown output %d", output); 1825 return; 1826 } 1827 1828 // look for duplicated outputs connected to the output being removed. 1829 for (size_t i = 0; i < mOutputs.size(); i++) { 1830 AudioOutputDescriptor *dupOutputDesc = mOutputs.valueAt(i); 1831 if (dupOutputDesc->isDuplicated() && 1832 (dupOutputDesc->mOutput1 == outputDesc || 1833 dupOutputDesc->mOutput2 == outputDesc)) { 1834 AudioOutputDescriptor *outputDesc2; 1835 if (dupOutputDesc->mOutput1 == outputDesc) { 1836 outputDesc2 = dupOutputDesc->mOutput2; 1837 } else { 1838 outputDesc2 = dupOutputDesc->mOutput1; 1839 } 1840 // As all active tracks on duplicated output will be deleted, 1841 // and as they were also referenced on the other output, the reference 1842 // count for their stream type must be adjusted accordingly on 1843 // the other output. 1844 for (int j = 0; j < (int)AudioSystem::NUM_STREAM_TYPES; j++) { 1845 int refCount = dupOutputDesc->mRefCount[j]; 1846 outputDesc2->changeRefCount((AudioSystem::stream_type)j,-refCount); 1847 } 1848 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); 1849 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); 1850 1851 mpClientInterface->closeOutput(duplicatedOutput); 1852 delete mOutputs.valueFor(duplicatedOutput); 1853 mOutputs.removeItem(duplicatedOutput); 1854 } 1855 } 1856 1857 AudioParameter param; 1858 param.add(String8("closing"), String8("true")); 1859 mpClientInterface->setParameters(output, param.toString()); 1860 1861 mpClientInterface->closeOutput(output); 1862 delete outputDesc; 1863 mOutputs.removeItem(output); 1864 mPreviousOutputs = mOutputs; 1865} 1866 1867SortedVector<audio_io_handle_t> AudioPolicyManagerBase::getOutputsForDevice(audio_devices_t device, 1868 DefaultKeyedVector<audio_io_handle_t, AudioOutputDescriptor *> openOutputs) 1869{ 1870 SortedVector<audio_io_handle_t> outputs; 1871 1872 ALOGVV("getOutputsForDevice() device %04x", device); 1873 for (size_t i = 0; i < openOutputs.size(); i++) { 1874 ALOGVV("output %d isDuplicated=%d device=%04x", 1875 i, openOutputs.valueAt(i)->isDuplicated(), openOutputs.valueAt(i)->supportedDevices()); 1876 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) { 1877 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i)); 1878 outputs.add(openOutputs.keyAt(i)); 1879 } 1880 } 1881 return outputs; 1882} 1883 1884bool AudioPolicyManagerBase::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1, 1885 SortedVector<audio_io_handle_t>& outputs2) 1886{ 1887 if (outputs1.size() != outputs2.size()) { 1888 return false; 1889 } 1890 for (size_t i = 0; i < outputs1.size(); i++) { 1891 if (outputs1[i] != outputs2[i]) { 1892 return false; 1893 } 1894 } 1895 return true; 1896} 1897 1898void AudioPolicyManagerBase::checkOutputForStrategy(routing_strategy strategy) 1899{ 1900 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/); 1901 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/); 1902 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs); 1903 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs); 1904 1905 if (!vectorsEqual(srcOutputs,dstOutputs)) { 1906 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d", 1907 strategy, srcOutputs[0], dstOutputs[0]); 1908 // mute strategy while moving tracks from one output to another 1909 for (size_t i = 0; i < srcOutputs.size(); i++) { 1910 AudioOutputDescriptor *desc = mOutputs.valueFor(srcOutputs[i]); 1911 if (desc->isStrategyActive(strategy)) { 1912 setStrategyMute(strategy, true, srcOutputs[i]); 1913 setStrategyMute(strategy, false, srcOutputs[i], MUTE_TIME_MS, newDevice); 1914 } 1915 } 1916 1917 // Move effects associated to this strategy from previous output to new output 1918 if (strategy == STRATEGY_MEDIA) { 1919 int outIdx = 0; 1920 for (size_t i = 0; i < dstOutputs.size(); i++) { 1921 AudioOutputDescriptor *desc = mOutputs.valueFor(dstOutputs[i]); 1922 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) { 1923 outIdx = i; 1924 } 1925 } 1926 SortedVector<audio_io_handle_t> moved; 1927 for (size_t i = 0; i < mEffects.size(); i++) { 1928 EffectDescriptor *desc = mEffects.valueAt(i); 1929 if (desc->mSession == AUDIO_SESSION_OUTPUT_MIX && 1930 desc->mIo != dstOutputs[outIdx]) { 1931 if (moved.indexOf(desc->mIo) < 0) { 1932 ALOGV("checkOutputForStrategy() moving effect %d to output %d", 1933 mEffects.keyAt(i), dstOutputs[outIdx]); 1934 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, desc->mIo, 1935 dstOutputs[outIdx]); 1936 moved.add(desc->mIo); 1937 } 1938 desc->mIo = dstOutputs[outIdx]; 1939 } 1940 } 1941 } 1942 // Move tracks associated to this strategy from previous output to new output 1943 for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) { 1944 if (getStrategy((AudioSystem::stream_type)i) == strategy) { 1945 //FIXME see fixme on name change 1946 mpClientInterface->setStreamOutput((AudioSystem::stream_type)i, 1947 dstOutputs[0] /* ignored */); 1948 } 1949 } 1950 } 1951} 1952 1953void AudioPolicyManagerBase::checkOutputForAllStrategies() 1954{ 1955 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); 1956 checkOutputForStrategy(STRATEGY_PHONE); 1957 checkOutputForStrategy(STRATEGY_SONIFICATION); 1958 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); 1959 checkOutputForStrategy(STRATEGY_MEDIA); 1960 checkOutputForStrategy(STRATEGY_DTMF); 1961} 1962 1963audio_io_handle_t AudioPolicyManagerBase::getA2dpOutput() 1964{ 1965 if (!mHasA2dp) { 1966 return 0; 1967 } 1968 1969 for (size_t i = 0; i < mOutputs.size(); i++) { 1970 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i); 1971 if (!outputDesc->isDuplicated() && outputDesc->device() & AUDIO_DEVICE_OUT_ALL_A2DP) { 1972 return mOutputs.keyAt(i); 1973 } 1974 } 1975 1976 return 0; 1977} 1978 1979void AudioPolicyManagerBase::checkA2dpSuspend() 1980{ 1981 if (!mHasA2dp) { 1982 return; 1983 } 1984 audio_io_handle_t a2dpOutput = getA2dpOutput(); 1985 if (a2dpOutput == 0) { 1986 return; 1987 } 1988 1989 // suspend A2DP output if: 1990 // (NOT already suspended) && 1991 // ((SCO device is connected && 1992 // (forced usage for communication || for record is SCO))) || 1993 // (phone state is ringing || in call) 1994 // 1995 // restore A2DP output if: 1996 // (Already suspended) && 1997 // ((SCO device is NOT connected || 1998 // (forced usage NOT for communication && NOT for record is SCO))) && 1999 // (phone state is NOT ringing && NOT in call) 2000 // 2001 if (mA2dpSuspended) { 2002 if (((mScoDeviceAddress == "") || 2003 ((mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO) && 2004 (mForceUse[AudioSystem::FOR_RECORD] != AudioSystem::FORCE_BT_SCO))) && 2005 ((mPhoneState != AudioSystem::MODE_IN_CALL) && 2006 (mPhoneState != AudioSystem::MODE_RINGTONE))) { 2007 2008 mpClientInterface->restoreOutput(a2dpOutput); 2009 mA2dpSuspended = false; 2010 } 2011 } else { 2012 if (((mScoDeviceAddress != "") && 2013 ((mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) || 2014 (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO))) || 2015 ((mPhoneState == AudioSystem::MODE_IN_CALL) || 2016 (mPhoneState == AudioSystem::MODE_RINGTONE))) { 2017 2018 mpClientInterface->suspendOutput(a2dpOutput); 2019 mA2dpSuspended = true; 2020 } 2021 } 2022} 2023 2024audio_devices_t AudioPolicyManagerBase::getNewDevice(audio_io_handle_t output, bool fromCache) 2025{ 2026 audio_devices_t device = AUDIO_DEVICE_NONE; 2027 2028 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output); 2029 // check the following by order of priority to request a routing change if necessary: 2030 // 1: the strategy enforced audible is active on the output: 2031 // use device for strategy enforced audible 2032 // 2: we are in call or the strategy phone is active on the output: 2033 // use device for strategy phone 2034 // 3: the strategy sonification is active on the output: 2035 // use device for strategy sonification 2036 // 4: the strategy "respectful" sonification is active on the output: 2037 // use device for strategy "respectful" sonification 2038 // 5: the strategy media is active on the output: 2039 // use device for strategy media 2040 // 6: the strategy DTMF is active on the output: 2041 // use device for strategy DTMF 2042 if (outputDesc->isStrategyActive(STRATEGY_ENFORCED_AUDIBLE)) { 2043 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); 2044 } else if (isInCall() || 2045 outputDesc->isStrategyActive(STRATEGY_PHONE)) { 2046 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache); 2047 } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION)) { 2048 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache); 2049 } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION_RESPECTFUL)) { 2050 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache); 2051 } else if (outputDesc->isStrategyActive(STRATEGY_MEDIA)) { 2052 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache); 2053 } else if (outputDesc->isStrategyActive(STRATEGY_DTMF)) { 2054 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache); 2055 } 2056 2057 ALOGV("getNewDevice() selected device %x", device); 2058 return device; 2059} 2060 2061uint32_t AudioPolicyManagerBase::getStrategyForStream(AudioSystem::stream_type stream) { 2062 return (uint32_t)getStrategy(stream); 2063} 2064 2065audio_devices_t AudioPolicyManagerBase::getDevicesForStream(AudioSystem::stream_type stream) { 2066 audio_devices_t devices; 2067 // By checking the range of stream before calling getStrategy, we avoid 2068 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE 2069 // and then return STRATEGY_MEDIA, but we want to return the empty set. 2070 if (stream < (AudioSystem::stream_type) 0 || stream >= AudioSystem::NUM_STREAM_TYPES) { 2071 devices = AUDIO_DEVICE_NONE; 2072 } else { 2073 AudioPolicyManagerBase::routing_strategy strategy = getStrategy(stream); 2074 devices = getDeviceForStrategy(strategy, true /*fromCache*/); 2075 } 2076 return devices; 2077} 2078 2079AudioPolicyManagerBase::routing_strategy AudioPolicyManagerBase::getStrategy( 2080 AudioSystem::stream_type stream) { 2081 // stream to strategy mapping 2082 switch (stream) { 2083 case AudioSystem::VOICE_CALL: 2084 case AudioSystem::BLUETOOTH_SCO: 2085 return STRATEGY_PHONE; 2086 case AudioSystem::RING: 2087 case AudioSystem::ALARM: 2088 return STRATEGY_SONIFICATION; 2089 case AudioSystem::NOTIFICATION: 2090 return STRATEGY_SONIFICATION_RESPECTFUL; 2091 case AudioSystem::DTMF: 2092 return STRATEGY_DTMF; 2093 default: 2094 ALOGE("unknown stream type"); 2095 case AudioSystem::SYSTEM: 2096 // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs 2097 // while key clicks are played produces a poor result 2098 case AudioSystem::TTS: 2099 case AudioSystem::MUSIC: 2100 return STRATEGY_MEDIA; 2101 case AudioSystem::ENFORCED_AUDIBLE: 2102 return STRATEGY_ENFORCED_AUDIBLE; 2103 } 2104} 2105 2106void AudioPolicyManagerBase::handleNotificationRoutingForStream(AudioSystem::stream_type stream) { 2107 switch(stream) { 2108 case AudioSystem::MUSIC: 2109 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); 2110 updateDevicesAndOutputs(); 2111 break; 2112 default: 2113 break; 2114 } 2115} 2116 2117audio_devices_t AudioPolicyManagerBase::getDeviceForStrategy(routing_strategy strategy, 2118 bool fromCache) 2119{ 2120 uint32_t device = AUDIO_DEVICE_NONE; 2121 2122 if (fromCache) { 2123 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x", 2124 strategy, mDeviceForStrategy[strategy]); 2125 return mDeviceForStrategy[strategy]; 2126 } 2127 2128 switch (strategy) { 2129 2130 case STRATEGY_SONIFICATION_RESPECTFUL: 2131 if (isInCall()) { 2132 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/); 2133 } else if (isStreamActiveRemotely(AudioSystem::MUSIC, 2134 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) { 2135 // while media is playing on a remote device, use the the sonification behavior. 2136 // Note that we test this usecase before testing if media is playing because 2137 // the isStreamActive() method only informs about the activity of a stream, not 2138 // if it's for local playback. Note also that we use the same delay between both tests 2139 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/); 2140 } else if (isStreamActive(AudioSystem::MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) { 2141 // while media is playing (or has recently played), use the same device 2142 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/); 2143 } else { 2144 // when media is not playing anymore, fall back on the sonification behavior 2145 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/); 2146 } 2147 2148 break; 2149 2150 case STRATEGY_DTMF: 2151 if (!isInCall()) { 2152 // when off call, DTMF strategy follows the same rules as MEDIA strategy 2153 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/); 2154 break; 2155 } 2156 // when in call, DTMF and PHONE strategies follow the same rules 2157 // FALL THROUGH 2158 2159 case STRATEGY_PHONE: 2160 // for phone strategy, we first consider the forced use and then the available devices by order 2161 // of priority 2162 switch (mForceUse[AudioSystem::FOR_COMMUNICATION]) { 2163 case AudioSystem::FORCE_BT_SCO: 2164 if (!isInCall() || strategy != STRATEGY_DTMF) { 2165 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT; 2166 if (device) break; 2167 } 2168 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET; 2169 if (device) break; 2170 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO; 2171 if (device) break; 2172 // if SCO device is requested but no SCO device is available, fall back to default case 2173 // FALL THROUGH 2174 2175 default: // FORCE_NONE 2176 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP 2177 if (mHasA2dp && !isInCall() && 2178 (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) && 2179 (getA2dpOutput() != 0) && !mA2dpSuspended) { 2180 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP; 2181 if (device) break; 2182 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES; 2183 if (device) break; 2184 } 2185 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE; 2186 if (device) break; 2187 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET; 2188 if (device) break; 2189 if (mPhoneState != AudioSystem::MODE_IN_CALL) { 2190 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY; 2191 if (device) break; 2192 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE; 2193 if (device) break; 2194 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET; 2195 if (device) break; 2196 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL; 2197 if (device) break; 2198 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET; 2199 if (device) break; 2200 } 2201 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_EARPIECE; 2202 if (device) break; 2203 device = mDefaultOutputDevice; 2204 if (device == AUDIO_DEVICE_NONE) { 2205 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE"); 2206 } 2207 break; 2208 2209 case AudioSystem::FORCE_SPEAKER: 2210 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to 2211 // A2DP speaker when forcing to speaker output 2212 if (mHasA2dp && !isInCall() && 2213 (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) && 2214 (getA2dpOutput() != 0) && !mA2dpSuspended) { 2215 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER; 2216 if (device) break; 2217 } 2218 if (mPhoneState != AudioSystem::MODE_IN_CALL) { 2219 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY; 2220 if (device) break; 2221 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE; 2222 if (device) break; 2223 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET; 2224 if (device) break; 2225 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL; 2226 if (device) break; 2227 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET; 2228 if (device) break; 2229 } 2230 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER; 2231 if (device) break; 2232 device = mDefaultOutputDevice; 2233 if (device == AUDIO_DEVICE_NONE) { 2234 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER"); 2235 } 2236 break; 2237 } 2238 break; 2239 2240 case STRATEGY_SONIFICATION: 2241 2242 // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by 2243 // handleIncallSonification(). 2244 if (isInCall()) { 2245 device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/); 2246 break; 2247 } 2248 // FALL THROUGH 2249 2250 case STRATEGY_ENFORCED_AUDIBLE: 2251 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION 2252 // except: 2253 // - when in call where it doesn't default to STRATEGY_PHONE behavior 2254 // - in countries where not enforced in which case it follows STRATEGY_MEDIA 2255 2256 if ((strategy == STRATEGY_SONIFICATION) || 2257 (mForceUse[AudioSystem::FOR_SYSTEM] == AudioSystem::FORCE_SYSTEM_ENFORCED)) { 2258 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER; 2259 if (device == AUDIO_DEVICE_NONE) { 2260 ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION"); 2261 } 2262 } 2263 // The second device used for sonification is the same as the device used by media strategy 2264 // FALL THROUGH 2265 2266 case STRATEGY_MEDIA: { 2267 uint32_t device2 = AUDIO_DEVICE_NONE; 2268 if (strategy != STRATEGY_SONIFICATION) { 2269 // no sonification on remote submix (e.g. WFD) 2270 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_REMOTE_SUBMIX; 2271 } 2272 if ((device2 == AUDIO_DEVICE_NONE) && 2273 mHasA2dp && (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) && 2274 (getA2dpOutput() != 0) && !mA2dpSuspended) { 2275 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP; 2276 if (device2 == AUDIO_DEVICE_NONE) { 2277 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES; 2278 } 2279 if (device2 == AUDIO_DEVICE_NONE) { 2280 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER; 2281 } 2282 } 2283 if (device2 == AUDIO_DEVICE_NONE) { 2284 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE; 2285 } 2286 if (device2 == AUDIO_DEVICE_NONE) { 2287 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET; 2288 } 2289 if (device2 == AUDIO_DEVICE_NONE) { 2290 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY; 2291 } 2292 if (device2 == AUDIO_DEVICE_NONE) { 2293 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE; 2294 } 2295 if (device2 == AUDIO_DEVICE_NONE) { 2296 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET; 2297 } 2298 if ((device2 == AUDIO_DEVICE_NONE) && (strategy != STRATEGY_SONIFICATION)) { 2299 // no sonification on aux digital (e.g. HDMI) 2300 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL; 2301 } 2302 if ((device2 == AUDIO_DEVICE_NONE) && 2303 (mForceUse[AudioSystem::FOR_DOCK] == AudioSystem::FORCE_ANALOG_DOCK)) { 2304 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET; 2305 } 2306 if (device2 == AUDIO_DEVICE_NONE) { 2307 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER; 2308 } 2309 2310 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or 2311 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise 2312 device |= device2; 2313 if (device) break; 2314 device = mDefaultOutputDevice; 2315 if (device == AUDIO_DEVICE_NONE) { 2316 ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA"); 2317 } 2318 } break; 2319 2320 default: 2321 ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy); 2322 break; 2323 } 2324 2325 ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device); 2326 return device; 2327} 2328 2329void AudioPolicyManagerBase::updateDevicesAndOutputs() 2330{ 2331 for (int i = 0; i < NUM_STRATEGIES; i++) { 2332 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); 2333 } 2334 mPreviousOutputs = mOutputs; 2335} 2336 2337uint32_t AudioPolicyManagerBase::checkDeviceMuteStrategies(AudioOutputDescriptor *outputDesc, 2338 audio_devices_t prevDevice, 2339 uint32_t delayMs) 2340{ 2341 // mute/unmute strategies using an incompatible device combination 2342 // if muting, wait for the audio in pcm buffer to be drained before proceeding 2343 // if unmuting, unmute only after the specified delay 2344 if (outputDesc->isDuplicated()) { 2345 return 0; 2346 } 2347 2348 uint32_t muteWaitMs = 0; 2349 audio_devices_t device = outputDesc->device(); 2350 bool shouldMute = outputDesc->isActive() && (AudioSystem::popCount(device) >= 2); 2351 // temporary mute output if device selection changes to avoid volume bursts due to 2352 // different per device volumes 2353 bool tempMute = outputDesc->isActive() && (device != prevDevice); 2354 2355 for (size_t i = 0; i < NUM_STRATEGIES; i++) { 2356 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); 2357 bool mute = shouldMute && (curDevice & device) && (curDevice != device); 2358 bool doMute = false; 2359 2360 if (mute && !outputDesc->mStrategyMutedByDevice[i]) { 2361 doMute = true; 2362 outputDesc->mStrategyMutedByDevice[i] = true; 2363 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){ 2364 doMute = true; 2365 outputDesc->mStrategyMutedByDevice[i] = false; 2366 } 2367 if (doMute || tempMute) { 2368 for (size_t j = 0; j < mOutputs.size(); j++) { 2369 AudioOutputDescriptor *desc = mOutputs.valueAt(j); 2370 // skip output if it does not share any device with current output 2371 if ((desc->supportedDevices() & outputDesc->supportedDevices()) 2372 == AUDIO_DEVICE_NONE) { 2373 continue; 2374 } 2375 audio_io_handle_t curOutput = mOutputs.keyAt(j); 2376 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x) on output %d", 2377 mute ? "muting" : "unmuting", i, curDevice, curOutput); 2378 setStrategyMute((routing_strategy)i, mute, curOutput, mute ? 0 : delayMs); 2379 if (desc->isStrategyActive((routing_strategy)i)) { 2380 // do tempMute only for current output 2381 if (tempMute && (desc == outputDesc)) { 2382 setStrategyMute((routing_strategy)i, true, curOutput); 2383 setStrategyMute((routing_strategy)i, false, curOutput, 2384 desc->latency() * 2, device); 2385 } 2386 if ((tempMute && (desc == outputDesc)) || mute) { 2387 if (muteWaitMs < desc->latency()) { 2388 muteWaitMs = desc->latency(); 2389 } 2390 } 2391 } 2392 } 2393 } 2394 } 2395 2396 // FIXME: should not need to double latency if volume could be applied immediately by the 2397 // audioflinger mixer. We must account for the delay between now and the next time 2398 // the audioflinger thread for this output will process a buffer (which corresponds to 2399 // one buffer size, usually 1/2 or 1/4 of the latency). 2400 muteWaitMs *= 2; 2401 // wait for the PCM output buffers to empty before proceeding with the rest of the command 2402 if (muteWaitMs > delayMs) { 2403 muteWaitMs -= delayMs; 2404 usleep(muteWaitMs * 1000); 2405 return muteWaitMs; 2406 } 2407 return 0; 2408} 2409 2410uint32_t AudioPolicyManagerBase::setOutputDevice(audio_io_handle_t output, 2411 audio_devices_t device, 2412 bool force, 2413 int delayMs) 2414{ 2415 ALOGV("setOutputDevice() output %d device %04x delayMs %d", output, device, delayMs); 2416 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output); 2417 AudioParameter param; 2418 uint32_t muteWaitMs; 2419 2420 if (outputDesc->isDuplicated()) { 2421 muteWaitMs = setOutputDevice(outputDesc->mOutput1->mId, device, force, delayMs); 2422 muteWaitMs += setOutputDevice(outputDesc->mOutput2->mId, device, force, delayMs); 2423 return muteWaitMs; 2424 } 2425 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current 2426 // output profile 2427 if ((device != AUDIO_DEVICE_NONE) && 2428 ((device & outputDesc->mProfile->mSupportedDevices) == 0)) { 2429 return 0; 2430 } 2431 2432 // filter devices according to output selected 2433 device = (audio_devices_t)(device & outputDesc->mProfile->mSupportedDevices); 2434 2435 audio_devices_t prevDevice = outputDesc->mDevice; 2436 2437 ALOGV("setOutputDevice() prevDevice %04x", prevDevice); 2438 2439 if (device != AUDIO_DEVICE_NONE) { 2440 outputDesc->mDevice = device; 2441 } 2442 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs); 2443 2444 // Do not change the routing if: 2445 // - the requested device is AUDIO_DEVICE_NONE 2446 // - the requested device is the same as current device and force is not specified. 2447 // Doing this check here allows the caller to call setOutputDevice() without conditions 2448 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && !force) { 2449 ALOGV("setOutputDevice() setting same device %04x or null device for output %d", device, output); 2450 return muteWaitMs; 2451 } 2452 2453 ALOGV("setOutputDevice() changing device"); 2454 // do the routing 2455 param.addInt(String8(AudioParameter::keyRouting), (int)device); 2456 mpClientInterface->setParameters(output, param.toString(), delayMs); 2457 2458 // update stream volumes according to new device 2459 applyStreamVolumes(output, device, delayMs); 2460 2461 return muteWaitMs; 2462} 2463 2464AudioPolicyManagerBase::IOProfile *AudioPolicyManagerBase::getInputProfile(audio_devices_t device, 2465 uint32_t samplingRate, 2466 uint32_t format, 2467 uint32_t channelMask) 2468{ 2469 // Choose an input profile based on the requested capture parameters: select the first available 2470 // profile supporting all requested parameters. 2471 2472 for (size_t i = 0; i < mHwModules.size(); i++) 2473 { 2474 if (mHwModules[i]->mHandle == 0) { 2475 continue; 2476 } 2477 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) 2478 { 2479 IOProfile *profile = mHwModules[i]->mInputProfiles[j]; 2480 if (profile->isCompatibleProfile(device, samplingRate, format, 2481 channelMask,(audio_output_flags_t)0)) { 2482 return profile; 2483 } 2484 } 2485 } 2486 return NULL; 2487} 2488 2489audio_devices_t AudioPolicyManagerBase::getDeviceForInputSource(int inputSource) 2490{ 2491 uint32_t device = AUDIO_DEVICE_NONE; 2492 2493 switch (inputSource) { 2494 case AUDIO_SOURCE_VOICE_UPLINK: 2495 if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) { 2496 device = AUDIO_DEVICE_IN_VOICE_CALL; 2497 break; 2498 } 2499 // FALL THROUGH 2500 2501 case AUDIO_SOURCE_DEFAULT: 2502 case AUDIO_SOURCE_MIC: 2503 case AUDIO_SOURCE_VOICE_RECOGNITION: 2504 case AUDIO_SOURCE_VOICE_COMMUNICATION: 2505 if (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO && 2506 mAvailableInputDevices & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) { 2507 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET; 2508 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_WIRED_HEADSET) { 2509 device = AUDIO_DEVICE_IN_WIRED_HEADSET; 2510 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) { 2511 device = AUDIO_DEVICE_IN_BUILTIN_MIC; 2512 } 2513 break; 2514 case AUDIO_SOURCE_CAMCORDER: 2515 if (mAvailableInputDevices & AUDIO_DEVICE_IN_BACK_MIC) { 2516 device = AUDIO_DEVICE_IN_BACK_MIC; 2517 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) { 2518 device = AUDIO_DEVICE_IN_BUILTIN_MIC; 2519 } 2520 break; 2521 case AUDIO_SOURCE_VOICE_DOWNLINK: 2522 case AUDIO_SOURCE_VOICE_CALL: 2523 if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) { 2524 device = AUDIO_DEVICE_IN_VOICE_CALL; 2525 } 2526 break; 2527 case AUDIO_SOURCE_REMOTE_SUBMIX: 2528 if (mAvailableInputDevices & AUDIO_DEVICE_IN_REMOTE_SUBMIX) { 2529 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX; 2530 } 2531 break; 2532 default: 2533 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource); 2534 break; 2535 } 2536 ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device); 2537 return device; 2538} 2539 2540bool AudioPolicyManagerBase::isVirtualInputDevice(audio_devices_t device) 2541{ 2542 if ((device & AUDIO_DEVICE_BIT_IN) != 0) { 2543 device &= ~AUDIO_DEVICE_BIT_IN; 2544 if ((popcount(device) == 1) && ((device & ~APM_AUDIO_IN_DEVICE_VIRTUAL_ALL) == 0)) 2545 return true; 2546 } 2547 return false; 2548} 2549 2550audio_io_handle_t AudioPolicyManagerBase::getActiveInput(bool ignoreVirtualInputs) 2551{ 2552 for (size_t i = 0; i < mInputs.size(); i++) { 2553 const AudioInputDescriptor * input_descriptor = mInputs.valueAt(i); 2554 if ((input_descriptor->mRefCount > 0) 2555 && (!ignoreVirtualInputs || !isVirtualInputDevice(input_descriptor->mDevice))) { 2556 return mInputs.keyAt(i); 2557 } 2558 } 2559 return 0; 2560} 2561 2562 2563audio_devices_t AudioPolicyManagerBase::getDeviceForVolume(audio_devices_t device) 2564{ 2565 if (device == AUDIO_DEVICE_NONE) { 2566 // this happens when forcing a route update and no track is active on an output. 2567 // In this case the returned category is not important. 2568 device = AUDIO_DEVICE_OUT_SPEAKER; 2569 } else if (AudioSystem::popCount(device) > 1) { 2570 // Multiple device selection is either: 2571 // - speaker + one other device: give priority to speaker in this case. 2572 // - one A2DP device + another device: happens with duplicated output. In this case 2573 // retain the device on the A2DP output as the other must not correspond to an active 2574 // selection if not the speaker. 2575 if (device & AUDIO_DEVICE_OUT_SPEAKER) { 2576 device = AUDIO_DEVICE_OUT_SPEAKER; 2577 } else { 2578 device = (audio_devices_t)(device & AUDIO_DEVICE_OUT_ALL_A2DP); 2579 } 2580 } 2581 2582 ALOGW_IF(AudioSystem::popCount(device) != 1, 2583 "getDeviceForVolume() invalid device combination: %08x", 2584 device); 2585 2586 return device; 2587} 2588 2589AudioPolicyManagerBase::device_category AudioPolicyManagerBase::getDeviceCategory(audio_devices_t device) 2590{ 2591 switch(getDeviceForVolume(device)) { 2592 case AUDIO_DEVICE_OUT_EARPIECE: 2593 return DEVICE_CATEGORY_EARPIECE; 2594 case AUDIO_DEVICE_OUT_WIRED_HEADSET: 2595 case AUDIO_DEVICE_OUT_WIRED_HEADPHONE: 2596 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO: 2597 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET: 2598 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP: 2599 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES: 2600 return DEVICE_CATEGORY_HEADSET; 2601 case AUDIO_DEVICE_OUT_SPEAKER: 2602 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT: 2603 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER: 2604 case AUDIO_DEVICE_OUT_AUX_DIGITAL: 2605 case AUDIO_DEVICE_OUT_USB_ACCESSORY: 2606 case AUDIO_DEVICE_OUT_USB_DEVICE: 2607 case AUDIO_DEVICE_OUT_REMOTE_SUBMIX: 2608 default: 2609 return DEVICE_CATEGORY_SPEAKER; 2610 } 2611} 2612 2613float AudioPolicyManagerBase::volIndexToAmpl(audio_devices_t device, const StreamDescriptor& streamDesc, 2614 int indexInUi) 2615{ 2616 device_category deviceCategory = getDeviceCategory(device); 2617 const VolumeCurvePoint *curve = streamDesc.mVolumeCurve[deviceCategory]; 2618 2619 // the volume index in the UI is relative to the min and max volume indices for this stream type 2620 int nbSteps = 1 + curve[VOLMAX].mIndex - 2621 curve[VOLMIN].mIndex; 2622 int volIdx = (nbSteps * (indexInUi - streamDesc.mIndexMin)) / 2623 (streamDesc.mIndexMax - streamDesc.mIndexMin); 2624 2625 // find what part of the curve this index volume belongs to, or if it's out of bounds 2626 int segment = 0; 2627 if (volIdx < curve[VOLMIN].mIndex) { // out of bounds 2628 return 0.0f; 2629 } else if (volIdx < curve[VOLKNEE1].mIndex) { 2630 segment = 0; 2631 } else if (volIdx < curve[VOLKNEE2].mIndex) { 2632 segment = 1; 2633 } else if (volIdx <= curve[VOLMAX].mIndex) { 2634 segment = 2; 2635 } else { // out of bounds 2636 return 1.0f; 2637 } 2638 2639 // linear interpolation in the attenuation table in dB 2640 float decibels = curve[segment].mDBAttenuation + 2641 ((float)(volIdx - curve[segment].mIndex)) * 2642 ( (curve[segment+1].mDBAttenuation - 2643 curve[segment].mDBAttenuation) / 2644 ((float)(curve[segment+1].mIndex - 2645 curve[segment].mIndex)) ); 2646 2647 float amplification = exp( decibels * 0.115129f); // exp( dB * ln(10) / 20 ) 2648 2649 ALOGVV("VOLUME vol index=[%d %d %d], dB=[%.1f %.1f %.1f] ampl=%.5f", 2650 curve[segment].mIndex, volIdx, 2651 curve[segment+1].mIndex, 2652 curve[segment].mDBAttenuation, 2653 decibels, 2654 curve[segment+1].mDBAttenuation, 2655 amplification); 2656 2657 return amplification; 2658} 2659 2660const AudioPolicyManagerBase::VolumeCurvePoint 2661 AudioPolicyManagerBase::sDefaultVolumeCurve[AudioPolicyManagerBase::VOLCNT] = { 2662 {1, -49.5f}, {33, -33.5f}, {66, -17.0f}, {100, 0.0f} 2663}; 2664 2665const AudioPolicyManagerBase::VolumeCurvePoint 2666 AudioPolicyManagerBase::sDefaultMediaVolumeCurve[AudioPolicyManagerBase::VOLCNT] = { 2667 {1, -58.0f}, {20, -40.0f}, {60, -17.0f}, {100, 0.0f} 2668}; 2669 2670const AudioPolicyManagerBase::VolumeCurvePoint 2671 AudioPolicyManagerBase::sSpeakerMediaVolumeCurve[AudioPolicyManagerBase::VOLCNT] = { 2672 {1, -56.0f}, {20, -34.0f}, {60, -11.0f}, {100, 0.0f} 2673}; 2674 2675const AudioPolicyManagerBase::VolumeCurvePoint 2676 AudioPolicyManagerBase::sSpeakerSonificationVolumeCurve[AudioPolicyManagerBase::VOLCNT] = { 2677 {1, -29.7f}, {33, -20.1f}, {66, -10.2f}, {100, 0.0f} 2678}; 2679 2680// AUDIO_STREAM_SYSTEM, AUDIO_STREAM_ENFORCED_AUDIBLE and AUDIO_STREAM_DTMF volume tracks 2681// AUDIO_STREAM_RING on phones and AUDIO_STREAM_MUSIC on tablets (See AudioService.java). 2682// The range is constrained between -24dB and -6dB over speaker and -30dB and -18dB over headset. 2683const AudioPolicyManagerBase::VolumeCurvePoint 2684 AudioPolicyManagerBase::sDefaultSystemVolumeCurve[AudioPolicyManagerBase::VOLCNT] = { 2685 {1, -24.0f}, {33, -18.0f}, {66, -12.0f}, {100, -6.0f} 2686}; 2687 2688const AudioPolicyManagerBase::VolumeCurvePoint 2689 AudioPolicyManagerBase::sHeadsetSystemVolumeCurve[AudioPolicyManagerBase::VOLCNT] = { 2690 {1, -30.0f}, {33, -26.0f}, {66, -22.0f}, {100, -18.0f} 2691}; 2692 2693const AudioPolicyManagerBase::VolumeCurvePoint 2694 AudioPolicyManagerBase::sDefaultVoiceVolumeCurve[AudioPolicyManagerBase::VOLCNT] = { 2695 {0, -42.0f}, {33, -28.0f}, {66, -14.0f}, {100, 0.0f} 2696}; 2697 2698const AudioPolicyManagerBase::VolumeCurvePoint 2699 AudioPolicyManagerBase::sSpeakerVoiceVolumeCurve[AudioPolicyManagerBase::VOLCNT] = { 2700 {0, -24.0f}, {33, -16.0f}, {66, -8.0f}, {100, 0.0f} 2701}; 2702 2703const AudioPolicyManagerBase::VolumeCurvePoint 2704 *AudioPolicyManagerBase::sVolumeProfiles[AUDIO_STREAM_CNT] 2705 [AudioPolicyManagerBase::DEVICE_CATEGORY_CNT] = { 2706 { // AUDIO_STREAM_VOICE_CALL 2707 sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_HEADSET 2708 sSpeakerVoiceVolumeCurve, // DEVICE_CATEGORY_SPEAKER 2709 sDefaultVoiceVolumeCurve // DEVICE_CATEGORY_EARPIECE 2710 }, 2711 { // AUDIO_STREAM_SYSTEM 2712 sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET 2713 sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER 2714 sDefaultSystemVolumeCurve // DEVICE_CATEGORY_EARPIECE 2715 }, 2716 { // AUDIO_STREAM_RING 2717 sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET 2718 sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER 2719 sDefaultVolumeCurve // DEVICE_CATEGORY_EARPIECE 2720 }, 2721 { // AUDIO_STREAM_MUSIC 2722 sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET 2723 sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER 2724 sDefaultMediaVolumeCurve // DEVICE_CATEGORY_EARPIECE 2725 }, 2726 { // AUDIO_STREAM_ALARM 2727 sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET 2728 sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER 2729 sDefaultVolumeCurve // DEVICE_CATEGORY_EARPIECE 2730 }, 2731 { // AUDIO_STREAM_NOTIFICATION 2732 sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET 2733 sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER 2734 sDefaultVolumeCurve // DEVICE_CATEGORY_EARPIECE 2735 }, 2736 { // AUDIO_STREAM_BLUETOOTH_SCO 2737 sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_HEADSET 2738 sSpeakerVoiceVolumeCurve, // DEVICE_CATEGORY_SPEAKER 2739 sDefaultVoiceVolumeCurve // DEVICE_CATEGORY_EARPIECE 2740 }, 2741 { // AUDIO_STREAM_ENFORCED_AUDIBLE 2742 sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET 2743 sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER 2744 sDefaultSystemVolumeCurve // DEVICE_CATEGORY_EARPIECE 2745 }, 2746 { // AUDIO_STREAM_DTMF 2747 sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET 2748 sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER 2749 sDefaultSystemVolumeCurve // DEVICE_CATEGORY_EARPIECE 2750 }, 2751 { // AUDIO_STREAM_TTS 2752 sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET 2753 sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER 2754 sDefaultMediaVolumeCurve // DEVICE_CATEGORY_EARPIECE 2755 }, 2756}; 2757 2758void AudioPolicyManagerBase::initializeVolumeCurves() 2759{ 2760 for (int i = 0; i < AUDIO_STREAM_CNT; i++) { 2761 for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) { 2762 mStreams[i].mVolumeCurve[j] = 2763 sVolumeProfiles[i][j]; 2764 } 2765 } 2766} 2767 2768float AudioPolicyManagerBase::computeVolume(int stream, 2769 int index, 2770 audio_io_handle_t output, 2771 audio_devices_t device) 2772{ 2773 float volume = 1.0; 2774 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output); 2775 StreamDescriptor &streamDesc = mStreams[stream]; 2776 2777 if (device == AUDIO_DEVICE_NONE) { 2778 device = outputDesc->device(); 2779 } 2780 2781 // if volume is not 0 (not muted), force media volume to max on digital output 2782 if (stream == AudioSystem::MUSIC && 2783 index != mStreams[stream].mIndexMin && 2784 (device == AUDIO_DEVICE_OUT_AUX_DIGITAL || 2785 device == AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET || 2786 device == AUDIO_DEVICE_OUT_USB_ACCESSORY || 2787 device == AUDIO_DEVICE_OUT_USB_DEVICE)) { 2788 return 1.0; 2789 } 2790 2791 volume = volIndexToAmpl(device, streamDesc, index); 2792 2793 // if a headset is connected, apply the following rules to ring tones and notifications 2794 // to avoid sound level bursts in user's ears: 2795 // - always attenuate ring tones and notifications volume by 6dB 2796 // - if music is playing, always limit the volume to current music volume, 2797 // with a minimum threshold at -36dB so that notification is always perceived. 2798 const routing_strategy stream_strategy = getStrategy((AudioSystem::stream_type)stream); 2799 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | 2800 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | 2801 AUDIO_DEVICE_OUT_WIRED_HEADSET | 2802 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) && 2803 ((stream_strategy == STRATEGY_SONIFICATION) 2804 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL) 2805 || (stream == AudioSystem::SYSTEM) 2806 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) && 2807 (mForceUse[AudioSystem::FOR_SYSTEM] == AudioSystem::FORCE_NONE))) && 2808 streamDesc.mCanBeMuted) { 2809 volume *= SONIFICATION_HEADSET_VOLUME_FACTOR; 2810 // when the phone is ringing we must consider that music could have been paused just before 2811 // by the music application and behave as if music was active if the last music track was 2812 // just stopped 2813 if (isStreamActive(AudioSystem::MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || 2814 mLimitRingtoneVolume) { 2815 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/); 2816 float musicVol = computeVolume(AudioSystem::MUSIC, 2817 mStreams[AudioSystem::MUSIC].getVolumeIndex(musicDevice), 2818 output, 2819 musicDevice); 2820 float minVol = (musicVol > SONIFICATION_HEADSET_VOLUME_MIN) ? 2821 musicVol : SONIFICATION_HEADSET_VOLUME_MIN; 2822 if (volume > minVol) { 2823 volume = minVol; 2824 ALOGV("computeVolume limiting volume to %f musicVol %f", minVol, musicVol); 2825 } 2826 } 2827 } 2828 2829 return volume; 2830} 2831 2832status_t AudioPolicyManagerBase::checkAndSetVolume(int stream, 2833 int index, 2834 audio_io_handle_t output, 2835 audio_devices_t device, 2836 int delayMs, 2837 bool force) 2838{ 2839 2840 // do not change actual stream volume if the stream is muted 2841 if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) { 2842 ALOGVV("checkAndSetVolume() stream %d muted count %d", 2843 stream, mOutputs.valueFor(output)->mMuteCount[stream]); 2844 return NO_ERROR; 2845 } 2846 2847 // do not change in call volume if bluetooth is connected and vice versa 2848 if ((stream == AudioSystem::VOICE_CALL && mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) || 2849 (stream == AudioSystem::BLUETOOTH_SCO && mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO)) { 2850 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm", 2851 stream, mForceUse[AudioSystem::FOR_COMMUNICATION]); 2852 return INVALID_OPERATION; 2853 } 2854 2855 float volume = computeVolume(stream, index, output, device); 2856 // We actually change the volume if: 2857 // - the float value returned by computeVolume() changed 2858 // - the force flag is set 2859 if (volume != mOutputs.valueFor(output)->mCurVolume[stream] || 2860 force) { 2861 mOutputs.valueFor(output)->mCurVolume[stream] = volume; 2862 ALOGVV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs); 2863 // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is 2864 // enabled 2865 if (stream == AudioSystem::BLUETOOTH_SCO) { 2866 mpClientInterface->setStreamVolume(AudioSystem::VOICE_CALL, volume, output, delayMs); 2867 } 2868 mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream, volume, output, delayMs); 2869 } 2870 2871 if (stream == AudioSystem::VOICE_CALL || 2872 stream == AudioSystem::BLUETOOTH_SCO) { 2873 float voiceVolume; 2874 // Force voice volume to max for bluetooth SCO as volume is managed by the headset 2875 if (stream == AudioSystem::VOICE_CALL) { 2876 voiceVolume = (float)index/(float)mStreams[stream].mIndexMax; 2877 } else { 2878 voiceVolume = 1.0; 2879 } 2880 2881 if (voiceVolume != mLastVoiceVolume && output == mPrimaryOutput) { 2882 mpClientInterface->setVoiceVolume(voiceVolume, delayMs); 2883 mLastVoiceVolume = voiceVolume; 2884 } 2885 } 2886 2887 return NO_ERROR; 2888} 2889 2890void AudioPolicyManagerBase::applyStreamVolumes(audio_io_handle_t output, 2891 audio_devices_t device, 2892 int delayMs, 2893 bool force) 2894{ 2895 ALOGVV("applyStreamVolumes() for output %d and device %x", output, device); 2896 2897 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) { 2898 checkAndSetVolume(stream, 2899 mStreams[stream].getVolumeIndex(device), 2900 output, 2901 device, 2902 delayMs, 2903 force); 2904 } 2905} 2906 2907void AudioPolicyManagerBase::setStrategyMute(routing_strategy strategy, 2908 bool on, 2909 audio_io_handle_t output, 2910 int delayMs, 2911 audio_devices_t device) 2912{ 2913 ALOGVV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output); 2914 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) { 2915 if (getStrategy((AudioSystem::stream_type)stream) == strategy) { 2916 setStreamMute(stream, on, output, delayMs, device); 2917 } 2918 } 2919} 2920 2921void AudioPolicyManagerBase::setStreamMute(int stream, 2922 bool on, 2923 audio_io_handle_t output, 2924 int delayMs, 2925 audio_devices_t device) 2926{ 2927 StreamDescriptor &streamDesc = mStreams[stream]; 2928 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output); 2929 if (device == AUDIO_DEVICE_NONE) { 2930 device = outputDesc->device(); 2931 } 2932 2933 ALOGVV("setStreamMute() stream %d, mute %d, output %d, mMuteCount %d device %04x", 2934 stream, on, output, outputDesc->mMuteCount[stream], device); 2935 2936 if (on) { 2937 if (outputDesc->mMuteCount[stream] == 0) { 2938 if (streamDesc.mCanBeMuted && 2939 ((stream != AudioSystem::ENFORCED_AUDIBLE) || 2940 (mForceUse[AudioSystem::FOR_SYSTEM] == AudioSystem::FORCE_NONE))) { 2941 checkAndSetVolume(stream, 0, output, device, delayMs); 2942 } 2943 } 2944 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored 2945 outputDesc->mMuteCount[stream]++; 2946 } else { 2947 if (outputDesc->mMuteCount[stream] == 0) { 2948 ALOGV("setStreamMute() unmuting non muted stream!"); 2949 return; 2950 } 2951 if (--outputDesc->mMuteCount[stream] == 0) { 2952 checkAndSetVolume(stream, 2953 streamDesc.getVolumeIndex(device), 2954 output, 2955 device, 2956 delayMs); 2957 } 2958 } 2959} 2960 2961void AudioPolicyManagerBase::handleIncallSonification(int stream, bool starting, bool stateChange) 2962{ 2963 // if the stream pertains to sonification strategy and we are in call we must 2964 // mute the stream if it is low visibility. If it is high visibility, we must play a tone 2965 // in the device used for phone strategy and play the tone if the selected device does not 2966 // interfere with the device used for phone strategy 2967 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as 2968 // many times as there are active tracks on the output 2969 const routing_strategy stream_strategy = getStrategy((AudioSystem::stream_type)stream); 2970 if ((stream_strategy == STRATEGY_SONIFICATION) || 2971 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) { 2972 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(mPrimaryOutput); 2973 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d", 2974 stream, starting, outputDesc->mDevice, stateChange); 2975 if (outputDesc->mRefCount[stream]) { 2976 int muteCount = 1; 2977 if (stateChange) { 2978 muteCount = outputDesc->mRefCount[stream]; 2979 } 2980 if (AudioSystem::isLowVisibility((AudioSystem::stream_type)stream)) { 2981 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount); 2982 for (int i = 0; i < muteCount; i++) { 2983 setStreamMute(stream, starting, mPrimaryOutput); 2984 } 2985 } else { 2986 ALOGV("handleIncallSonification() high visibility"); 2987 if (outputDesc->device() & 2988 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) { 2989 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount); 2990 for (int i = 0; i < muteCount; i++) { 2991 setStreamMute(stream, starting, mPrimaryOutput); 2992 } 2993 } 2994 if (starting) { 2995 mpClientInterface->startTone(ToneGenerator::TONE_SUP_CALL_WAITING, AudioSystem::VOICE_CALL); 2996 } else { 2997 mpClientInterface->stopTone(); 2998 } 2999 } 3000 } 3001 } 3002} 3003 3004bool AudioPolicyManagerBase::isInCall() 3005{ 3006 return isStateInCall(mPhoneState); 3007} 3008 3009bool AudioPolicyManagerBase::isStateInCall(int state) { 3010 return ((state == AudioSystem::MODE_IN_CALL) || 3011 (state == AudioSystem::MODE_IN_COMMUNICATION)); 3012} 3013 3014bool AudioPolicyManagerBase::needsDirectOuput(audio_stream_type_t stream, 3015 uint32_t samplingRate, 3016 audio_format_t format, 3017 audio_channel_mask_t channelMask, 3018 audio_output_flags_t flags, 3019 audio_devices_t device) 3020{ 3021 return ((flags & AudioSystem::OUTPUT_FLAG_DIRECT) || 3022 (format != 0 && !AudioSystem::isLinearPCM(format))); 3023} 3024 3025uint32_t AudioPolicyManagerBase::getMaxEffectsCpuLoad() 3026{ 3027 return MAX_EFFECTS_CPU_LOAD; 3028} 3029 3030uint32_t AudioPolicyManagerBase::getMaxEffectsMemory() 3031{ 3032 return MAX_EFFECTS_MEMORY; 3033} 3034 3035// --- AudioOutputDescriptor class implementation 3036 3037AudioPolicyManagerBase::AudioOutputDescriptor::AudioOutputDescriptor( 3038 const IOProfile *profile) 3039 : mId(0), mSamplingRate(0), mFormat((audio_format_t)0), 3040 mChannelMask((audio_channel_mask_t)0), mLatency(0), 3041 mFlags((audio_output_flags_t)0), mDevice(AUDIO_DEVICE_NONE), 3042 mOutput1(0), mOutput2(0), mProfile(profile), mDirectOpenCount(0) 3043{ 3044 // clear usage count for all stream types 3045 for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) { 3046 mRefCount[i] = 0; 3047 mCurVolume[i] = -1.0; 3048 mMuteCount[i] = 0; 3049 mStopTime[i] = 0; 3050 } 3051 for (int i = 0; i < NUM_STRATEGIES; i++) { 3052 mStrategyMutedByDevice[i] = false; 3053 } 3054 if (profile != NULL) { 3055 mSamplingRate = profile->mSamplingRates[0]; 3056 mFormat = profile->mFormats[0]; 3057 mChannelMask = profile->mChannelMasks[0]; 3058 mFlags = profile->mFlags; 3059 } 3060} 3061 3062audio_devices_t AudioPolicyManagerBase::AudioOutputDescriptor::device() const 3063{ 3064 if (isDuplicated()) { 3065 return (audio_devices_t)(mOutput1->mDevice | mOutput2->mDevice); 3066 } else { 3067 return mDevice; 3068 } 3069} 3070 3071uint32_t AudioPolicyManagerBase::AudioOutputDescriptor::latency() 3072{ 3073 if (isDuplicated()) { 3074 return (mOutput1->mLatency > mOutput2->mLatency) ? mOutput1->mLatency : mOutput2->mLatency; 3075 } else { 3076 return mLatency; 3077 } 3078} 3079 3080bool AudioPolicyManagerBase::AudioOutputDescriptor::sharesHwModuleWith( 3081 const AudioOutputDescriptor *outputDesc) 3082{ 3083 if (isDuplicated()) { 3084 return mOutput1->sharesHwModuleWith(outputDesc) || mOutput2->sharesHwModuleWith(outputDesc); 3085 } else if (outputDesc->isDuplicated()){ 3086 return sharesHwModuleWith(outputDesc->mOutput1) || sharesHwModuleWith(outputDesc->mOutput2); 3087 } else { 3088 return (mProfile->mModule == outputDesc->mProfile->mModule); 3089 } 3090} 3091 3092void AudioPolicyManagerBase::AudioOutputDescriptor::changeRefCount(AudioSystem::stream_type stream, int delta) 3093{ 3094 // forward usage count change to attached outputs 3095 if (isDuplicated()) { 3096 mOutput1->changeRefCount(stream, delta); 3097 mOutput2->changeRefCount(stream, delta); 3098 } 3099 if ((delta + (int)mRefCount[stream]) < 0) { 3100 ALOGW("changeRefCount() invalid delta %d for stream %d, refCount %d", delta, stream, mRefCount[stream]); 3101 mRefCount[stream] = 0; 3102 return; 3103 } 3104 mRefCount[stream] += delta; 3105 ALOGV("changeRefCount() stream %d, count %d", stream, mRefCount[stream]); 3106} 3107 3108audio_devices_t AudioPolicyManagerBase::AudioOutputDescriptor::supportedDevices() 3109{ 3110 if (isDuplicated()) { 3111 return (audio_devices_t)(mOutput1->supportedDevices() | mOutput2->supportedDevices()); 3112 } else { 3113 return mProfile->mSupportedDevices ; 3114 } 3115} 3116 3117bool AudioPolicyManagerBase::AudioOutputDescriptor::isActive(uint32_t inPastMs) const 3118{ 3119 return isStrategyActive(NUM_STRATEGIES, inPastMs); 3120} 3121 3122bool AudioPolicyManagerBase::AudioOutputDescriptor::isStrategyActive(routing_strategy strategy, 3123 uint32_t inPastMs, 3124 nsecs_t sysTime) const 3125{ 3126 if ((sysTime == 0) && (inPastMs != 0)) { 3127 sysTime = systemTime(); 3128 } 3129 for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) { 3130 if (((getStrategy((AudioSystem::stream_type)i) == strategy) || 3131 (NUM_STRATEGIES == strategy)) && 3132 isStreamActive((AudioSystem::stream_type)i, inPastMs, sysTime)) { 3133 return true; 3134 } 3135 } 3136 return false; 3137} 3138 3139bool AudioPolicyManagerBase::AudioOutputDescriptor::isStreamActive(AudioSystem::stream_type stream, 3140 uint32_t inPastMs, 3141 nsecs_t sysTime) const 3142{ 3143 if (mRefCount[stream] != 0) { 3144 return true; 3145 } 3146 if (inPastMs == 0) { 3147 return false; 3148 } 3149 if (sysTime == 0) { 3150 sysTime = systemTime(); 3151 } 3152 if (ns2ms(sysTime - mStopTime[stream]) < inPastMs) { 3153 return true; 3154 } 3155 return false; 3156} 3157 3158 3159status_t AudioPolicyManagerBase::AudioOutputDescriptor::dump(int fd) 3160{ 3161 const size_t SIZE = 256; 3162 char buffer[SIZE]; 3163 String8 result; 3164 3165 snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate); 3166 result.append(buffer); 3167 snprintf(buffer, SIZE, " Format: %d\n", mFormat); 3168 result.append(buffer); 3169 snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask); 3170 result.append(buffer); 3171 snprintf(buffer, SIZE, " Latency: %d\n", mLatency); 3172 result.append(buffer); 3173 snprintf(buffer, SIZE, " Flags %08x\n", mFlags); 3174 result.append(buffer); 3175 snprintf(buffer, SIZE, " Devices %08x\n", device()); 3176 result.append(buffer); 3177 snprintf(buffer, SIZE, " Stream volume refCount muteCount\n"); 3178 result.append(buffer); 3179 for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) { 3180 snprintf(buffer, SIZE, " %02d %.03f %02d %02d\n", i, mCurVolume[i], mRefCount[i], mMuteCount[i]); 3181 result.append(buffer); 3182 } 3183 write(fd, result.string(), result.size()); 3184 3185 return NO_ERROR; 3186} 3187 3188// --- AudioInputDescriptor class implementation 3189 3190AudioPolicyManagerBase::AudioInputDescriptor::AudioInputDescriptor(const IOProfile *profile) 3191 : mSamplingRate(0), mFormat((audio_format_t)0), mChannelMask((audio_channel_mask_t)0), 3192 mDevice(AUDIO_DEVICE_NONE), mRefCount(0), 3193 mInputSource(0), mProfile(profile) 3194{ 3195} 3196 3197status_t AudioPolicyManagerBase::AudioInputDescriptor::dump(int fd) 3198{ 3199 const size_t SIZE = 256; 3200 char buffer[SIZE]; 3201 String8 result; 3202 3203 snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate); 3204 result.append(buffer); 3205 snprintf(buffer, SIZE, " Format: %d\n", mFormat); 3206 result.append(buffer); 3207 snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask); 3208 result.append(buffer); 3209 snprintf(buffer, SIZE, " Devices %08x\n", mDevice); 3210 result.append(buffer); 3211 snprintf(buffer, SIZE, " Ref Count %d\n", mRefCount); 3212 result.append(buffer); 3213 write(fd, result.string(), result.size()); 3214 3215 return NO_ERROR; 3216} 3217 3218// --- StreamDescriptor class implementation 3219 3220AudioPolicyManagerBase::StreamDescriptor::StreamDescriptor() 3221 : mIndexMin(0), mIndexMax(1), mCanBeMuted(true) 3222{ 3223 mIndexCur.add(AUDIO_DEVICE_OUT_DEFAULT, 0); 3224} 3225 3226int AudioPolicyManagerBase::StreamDescriptor::getVolumeIndex(audio_devices_t device) 3227{ 3228 device = AudioPolicyManagerBase::getDeviceForVolume(device); 3229 // there is always a valid entry for AUDIO_DEVICE_OUT_DEFAULT 3230 if (mIndexCur.indexOfKey(device) < 0) { 3231 device = AUDIO_DEVICE_OUT_DEFAULT; 3232 } 3233 return mIndexCur.valueFor(device); 3234} 3235 3236void AudioPolicyManagerBase::StreamDescriptor::dump(int fd) 3237{ 3238 const size_t SIZE = 256; 3239 char buffer[SIZE]; 3240 String8 result; 3241 3242 snprintf(buffer, SIZE, "%s %02d %02d ", 3243 mCanBeMuted ? "true " : "false", mIndexMin, mIndexMax); 3244 result.append(buffer); 3245 for (size_t i = 0; i < mIndexCur.size(); i++) { 3246 snprintf(buffer, SIZE, "%04x : %02d, ", 3247 mIndexCur.keyAt(i), 3248 mIndexCur.valueAt(i)); 3249 result.append(buffer); 3250 } 3251 result.append("\n"); 3252 3253 write(fd, result.string(), result.size()); 3254} 3255 3256// --- EffectDescriptor class implementation 3257 3258status_t AudioPolicyManagerBase::EffectDescriptor::dump(int fd) 3259{ 3260 const size_t SIZE = 256; 3261 char buffer[SIZE]; 3262 String8 result; 3263 3264 snprintf(buffer, SIZE, " I/O: %d\n", mIo); 3265 result.append(buffer); 3266 snprintf(buffer, SIZE, " Strategy: %d\n", mStrategy); 3267 result.append(buffer); 3268 snprintf(buffer, SIZE, " Session: %d\n", mSession); 3269 result.append(buffer); 3270 snprintf(buffer, SIZE, " Name: %s\n", mDesc.name); 3271 result.append(buffer); 3272 snprintf(buffer, SIZE, " %s\n", mEnabled ? "Enabled" : "Disabled"); 3273 result.append(buffer); 3274 write(fd, result.string(), result.size()); 3275 3276 return NO_ERROR; 3277} 3278 3279// --- IOProfile class implementation 3280 3281AudioPolicyManagerBase::HwModule::HwModule(const char *name) 3282 : mName(strndup(name, AUDIO_HARDWARE_MODULE_ID_MAX_LEN)), mHandle(0) 3283{ 3284} 3285 3286AudioPolicyManagerBase::HwModule::~HwModule() 3287{ 3288 for (size_t i = 0; i < mOutputProfiles.size(); i++) { 3289 delete mOutputProfiles[i]; 3290 } 3291 for (size_t i = 0; i < mInputProfiles.size(); i++) { 3292 delete mInputProfiles[i]; 3293 } 3294 free((void *)mName); 3295} 3296 3297void AudioPolicyManagerBase::HwModule::dump(int fd) 3298{ 3299 const size_t SIZE = 256; 3300 char buffer[SIZE]; 3301 String8 result; 3302 3303 snprintf(buffer, SIZE, " - name: %s\n", mName); 3304 result.append(buffer); 3305 snprintf(buffer, SIZE, " - handle: %d\n", mHandle); 3306 result.append(buffer); 3307 write(fd, result.string(), result.size()); 3308 if (mOutputProfiles.size()) { 3309 write(fd, " - outputs:\n", sizeof(" - outputs:\n")); 3310 for (size_t i = 0; i < mOutputProfiles.size(); i++) { 3311 snprintf(buffer, SIZE, " output %d:\n", i); 3312 write(fd, buffer, strlen(buffer)); 3313 mOutputProfiles[i]->dump(fd); 3314 } 3315 } 3316 if (mInputProfiles.size()) { 3317 write(fd, " - inputs:\n", sizeof(" - inputs:\n")); 3318 for (size_t i = 0; i < mInputProfiles.size(); i++) { 3319 snprintf(buffer, SIZE, " input %d:\n", i); 3320 write(fd, buffer, strlen(buffer)); 3321 mInputProfiles[i]->dump(fd); 3322 } 3323 } 3324} 3325 3326AudioPolicyManagerBase::IOProfile::IOProfile(HwModule *module) 3327 : mFlags((audio_output_flags_t)0), mModule(module) 3328{ 3329} 3330 3331AudioPolicyManagerBase::IOProfile::~IOProfile() 3332{ 3333} 3334 3335// checks if the IO profile is compatible with specified parameters. By convention a value of 0 3336// means a parameter is don't care 3337bool AudioPolicyManagerBase::IOProfile::isCompatibleProfile(audio_devices_t device, 3338 uint32_t samplingRate, 3339 uint32_t format, 3340 uint32_t channelMask, 3341 audio_output_flags_t flags) const 3342{ 3343 if ((mSupportedDevices & device) != device) { 3344 return false; 3345 } 3346 if ((mFlags & flags) != flags) { 3347 return false; 3348 } 3349 if (samplingRate != 0) { 3350 size_t i; 3351 for (i = 0; i < mSamplingRates.size(); i++) 3352 { 3353 if (mSamplingRates[i] == samplingRate) { 3354 break; 3355 } 3356 } 3357 if (i == mSamplingRates.size()) { 3358 return false; 3359 } 3360 } 3361 if (format != 0) { 3362 size_t i; 3363 for (i = 0; i < mFormats.size(); i++) 3364 { 3365 if (mFormats[i] == format) { 3366 break; 3367 } 3368 } 3369 if (i == mFormats.size()) { 3370 return false; 3371 } 3372 } 3373 if (channelMask != 0) { 3374 size_t i; 3375 for (i = 0; i < mChannelMasks.size(); i++) 3376 { 3377 if (mChannelMasks[i] == channelMask) { 3378 break; 3379 } 3380 } 3381 if (i == mChannelMasks.size()) { 3382 return false; 3383 } 3384 } 3385 return true; 3386} 3387 3388void AudioPolicyManagerBase::IOProfile::dump(int fd) 3389{ 3390 const size_t SIZE = 256; 3391 char buffer[SIZE]; 3392 String8 result; 3393 3394 snprintf(buffer, SIZE, " - sampling rates: "); 3395 result.append(buffer); 3396 for (size_t i = 0; i < mSamplingRates.size(); i++) { 3397 snprintf(buffer, SIZE, "%d", mSamplingRates[i]); 3398 result.append(buffer); 3399 result.append(i == (mSamplingRates.size() - 1) ? "\n" : ", "); 3400 } 3401 3402 snprintf(buffer, SIZE, " - channel masks: "); 3403 result.append(buffer); 3404 for (size_t i = 0; i < mChannelMasks.size(); i++) { 3405 snprintf(buffer, SIZE, "%04x", mChannelMasks[i]); 3406 result.append(buffer); 3407 result.append(i == (mChannelMasks.size() - 1) ? "\n" : ", "); 3408 } 3409 3410 snprintf(buffer, SIZE, " - formats: "); 3411 result.append(buffer); 3412 for (size_t i = 0; i < mFormats.size(); i++) { 3413 snprintf(buffer, SIZE, "%d", mFormats[i]); 3414 result.append(buffer); 3415 result.append(i == (mFormats.size() - 1) ? "\n" : ", "); 3416 } 3417 3418 snprintf(buffer, SIZE, " - devices: %04x\n", mSupportedDevices); 3419 result.append(buffer); 3420 snprintf(buffer, SIZE, " - flags: %04x\n", mFlags); 3421 result.append(buffer); 3422 3423 write(fd, result.string(), result.size()); 3424} 3425 3426// --- audio_policy.conf file parsing 3427 3428struct StringToEnum { 3429 const char *name; 3430 uint32_t value; 3431}; 3432 3433#define STRING_TO_ENUM(string) { #string, string } 3434#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) 3435 3436const struct StringToEnum sDeviceNameToEnumTable[] = { 3437 STRING_TO_ENUM(AUDIO_DEVICE_OUT_EARPIECE), 3438 STRING_TO_ENUM(AUDIO_DEVICE_OUT_SPEAKER), 3439 STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADSET), 3440 STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADPHONE), 3441 STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_SCO), 3442 STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_A2DP), 3443 STRING_TO_ENUM(AUDIO_DEVICE_OUT_AUX_DIGITAL), 3444 STRING_TO_ENUM(AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET), 3445 STRING_TO_ENUM(AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET), 3446 STRING_TO_ENUM(AUDIO_DEVICE_OUT_USB_DEVICE), 3447 STRING_TO_ENUM(AUDIO_DEVICE_OUT_USB_ACCESSORY), 3448 STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_USB), 3449 STRING_TO_ENUM(AUDIO_DEVICE_OUT_REMOTE_SUBMIX), 3450 STRING_TO_ENUM(AUDIO_DEVICE_IN_BUILTIN_MIC), 3451 STRING_TO_ENUM(AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET), 3452 STRING_TO_ENUM(AUDIO_DEVICE_IN_WIRED_HEADSET), 3453 STRING_TO_ENUM(AUDIO_DEVICE_IN_AUX_DIGITAL), 3454 STRING_TO_ENUM(AUDIO_DEVICE_IN_VOICE_CALL), 3455 STRING_TO_ENUM(AUDIO_DEVICE_IN_BACK_MIC), 3456 STRING_TO_ENUM(AUDIO_DEVICE_IN_REMOTE_SUBMIX), 3457 STRING_TO_ENUM(AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET), 3458 STRING_TO_ENUM(AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET), 3459 STRING_TO_ENUM(AUDIO_DEVICE_IN_USB_ACCESSORY), 3460}; 3461 3462const struct StringToEnum sFlagNameToEnumTable[] = { 3463 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT), 3464 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY), 3465 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST), 3466 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER), 3467 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD), 3468 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING), 3469}; 3470 3471const struct StringToEnum sFormatNameToEnumTable[] = { 3472 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT), 3473 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT), 3474 STRING_TO_ENUM(AUDIO_FORMAT_MP3), 3475 STRING_TO_ENUM(AUDIO_FORMAT_AAC), 3476 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS), 3477}; 3478 3479const struct StringToEnum sOutChannelsNameToEnumTable[] = { 3480 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_MONO), 3481 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO), 3482 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1), 3483 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1), 3484}; 3485 3486const struct StringToEnum sInChannelsNameToEnumTable[] = { 3487 STRING_TO_ENUM(AUDIO_CHANNEL_IN_MONO), 3488 STRING_TO_ENUM(AUDIO_CHANNEL_IN_STEREO), 3489 STRING_TO_ENUM(AUDIO_CHANNEL_IN_FRONT_BACK), 3490}; 3491 3492 3493uint32_t AudioPolicyManagerBase::stringToEnum(const struct StringToEnum *table, 3494 size_t size, 3495 const char *name) 3496{ 3497 for (size_t i = 0; i < size; i++) { 3498 if (strcmp(table[i].name, name) == 0) { 3499 ALOGV("stringToEnum() found %s", table[i].name); 3500 return table[i].value; 3501 } 3502 } 3503 return 0; 3504} 3505 3506audio_output_flags_t AudioPolicyManagerBase::parseFlagNames(char *name) 3507{ 3508 uint32_t flag = 0; 3509 3510 // it is OK to cast name to non const here as we are not going to use it after 3511 // strtok() modifies it 3512 char *flagName = strtok(name, "|"); 3513 while (flagName != NULL) { 3514 if (strlen(flagName) != 0) { 3515 flag |= stringToEnum(sFlagNameToEnumTable, 3516 ARRAY_SIZE(sFlagNameToEnumTable), 3517 flagName); 3518 } 3519 flagName = strtok(NULL, "|"); 3520 } 3521 return (audio_output_flags_t)flag; 3522} 3523 3524audio_devices_t AudioPolicyManagerBase::parseDeviceNames(char *name) 3525{ 3526 uint32_t device = 0; 3527 3528 char *devName = strtok(name, "|"); 3529 while (devName != NULL) { 3530 if (strlen(devName) != 0) { 3531 device |= stringToEnum(sDeviceNameToEnumTable, 3532 ARRAY_SIZE(sDeviceNameToEnumTable), 3533 devName); 3534 } 3535 devName = strtok(NULL, "|"); 3536 } 3537 return device; 3538} 3539 3540void AudioPolicyManagerBase::loadSamplingRates(char *name, IOProfile *profile) 3541{ 3542 char *str = strtok(name, "|"); 3543 3544 // by convention, "0' in the first entry in mSamplingRates indicates the supported sampling 3545 // rates should be read from the output stream after it is opened for the first time 3546 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) { 3547 profile->mSamplingRates.add(0); 3548 return; 3549 } 3550 3551 while (str != NULL) { 3552 uint32_t rate = atoi(str); 3553 if (rate != 0) { 3554 ALOGV("loadSamplingRates() adding rate %d", rate); 3555 profile->mSamplingRates.add(rate); 3556 } 3557 str = strtok(NULL, "|"); 3558 } 3559 return; 3560} 3561 3562void AudioPolicyManagerBase::loadFormats(char *name, IOProfile *profile) 3563{ 3564 char *str = strtok(name, "|"); 3565 3566 // by convention, "0' in the first entry in mFormats indicates the supported formats 3567 // should be read from the output stream after it is opened for the first time 3568 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) { 3569 profile->mFormats.add((audio_format_t)0); 3570 return; 3571 } 3572 3573 while (str != NULL) { 3574 audio_format_t format = (audio_format_t)stringToEnum(sFormatNameToEnumTable, 3575 ARRAY_SIZE(sFormatNameToEnumTable), 3576 str); 3577 if (format != 0) { 3578 profile->mFormats.add(format); 3579 } 3580 str = strtok(NULL, "|"); 3581 } 3582 return; 3583} 3584 3585void AudioPolicyManagerBase::loadInChannels(char *name, IOProfile *profile) 3586{ 3587 const char *str = strtok(name, "|"); 3588 3589 ALOGV("loadInChannels() %s", name); 3590 3591 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) { 3592 profile->mChannelMasks.add((audio_channel_mask_t)0); 3593 return; 3594 } 3595 3596 while (str != NULL) { 3597 audio_channel_mask_t channelMask = 3598 (audio_channel_mask_t)stringToEnum(sInChannelsNameToEnumTable, 3599 ARRAY_SIZE(sInChannelsNameToEnumTable), 3600 str); 3601 if (channelMask != 0) { 3602 ALOGV("loadInChannels() adding channelMask %04x", channelMask); 3603 profile->mChannelMasks.add(channelMask); 3604 } 3605 str = strtok(NULL, "|"); 3606 } 3607 return; 3608} 3609 3610void AudioPolicyManagerBase::loadOutChannels(char *name, IOProfile *profile) 3611{ 3612 const char *str = strtok(name, "|"); 3613 3614 ALOGV("loadOutChannels() %s", name); 3615 3616 // by convention, "0' in the first entry in mChannelMasks indicates the supported channel 3617 // masks should be read from the output stream after it is opened for the first time 3618 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) { 3619 profile->mChannelMasks.add((audio_channel_mask_t)0); 3620 return; 3621 } 3622 3623 while (str != NULL) { 3624 audio_channel_mask_t channelMask = 3625 (audio_channel_mask_t)stringToEnum(sOutChannelsNameToEnumTable, 3626 ARRAY_SIZE(sOutChannelsNameToEnumTable), 3627 str); 3628 if (channelMask != 0) { 3629 profile->mChannelMasks.add(channelMask); 3630 } 3631 str = strtok(NULL, "|"); 3632 } 3633 return; 3634} 3635 3636status_t AudioPolicyManagerBase::loadInput(cnode *root, HwModule *module) 3637{ 3638 cnode *node = root->first_child; 3639 3640 IOProfile *profile = new IOProfile(module); 3641 3642 while (node) { 3643 if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) { 3644 loadSamplingRates((char *)node->value, profile); 3645 } else if (strcmp(node->name, FORMATS_TAG) == 0) { 3646 loadFormats((char *)node->value, profile); 3647 } else if (strcmp(node->name, CHANNELS_TAG) == 0) { 3648 loadInChannels((char *)node->value, profile); 3649 } else if (strcmp(node->name, DEVICES_TAG) == 0) { 3650 profile->mSupportedDevices = parseDeviceNames((char *)node->value); 3651 } 3652 node = node->next; 3653 } 3654 ALOGW_IF(profile->mSupportedDevices == AUDIO_DEVICE_NONE, 3655 "loadInput() invalid supported devices"); 3656 ALOGW_IF(profile->mChannelMasks.size() == 0, 3657 "loadInput() invalid supported channel masks"); 3658 ALOGW_IF(profile->mSamplingRates.size() == 0, 3659 "loadInput() invalid supported sampling rates"); 3660 ALOGW_IF(profile->mFormats.size() == 0, 3661 "loadInput() invalid supported formats"); 3662 if ((profile->mSupportedDevices != AUDIO_DEVICE_NONE) && 3663 (profile->mChannelMasks.size() != 0) && 3664 (profile->mSamplingRates.size() != 0) && 3665 (profile->mFormats.size() != 0)) { 3666 3667 ALOGV("loadInput() adding input mSupportedDevices %04x", profile->mSupportedDevices); 3668 3669 module->mInputProfiles.add(profile); 3670 return NO_ERROR; 3671 } else { 3672 delete profile; 3673 return BAD_VALUE; 3674 } 3675} 3676 3677status_t AudioPolicyManagerBase::loadOutput(cnode *root, HwModule *module) 3678{ 3679 cnode *node = root->first_child; 3680 3681 IOProfile *profile = new IOProfile(module); 3682 3683 while (node) { 3684 if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) { 3685 loadSamplingRates((char *)node->value, profile); 3686 } else if (strcmp(node->name, FORMATS_TAG) == 0) { 3687 loadFormats((char *)node->value, profile); 3688 } else if (strcmp(node->name, CHANNELS_TAG) == 0) { 3689 loadOutChannels((char *)node->value, profile); 3690 } else if (strcmp(node->name, DEVICES_TAG) == 0) { 3691 profile->mSupportedDevices = parseDeviceNames((char *)node->value); 3692 } else if (strcmp(node->name, FLAGS_TAG) == 0) { 3693 profile->mFlags = parseFlagNames((char *)node->value); 3694 } 3695 node = node->next; 3696 } 3697 ALOGW_IF(profile->mSupportedDevices == AUDIO_DEVICE_NONE, 3698 "loadOutput() invalid supported devices"); 3699 ALOGW_IF(profile->mChannelMasks.size() == 0, 3700 "loadOutput() invalid supported channel masks"); 3701 ALOGW_IF(profile->mSamplingRates.size() == 0, 3702 "loadOutput() invalid supported sampling rates"); 3703 ALOGW_IF(profile->mFormats.size() == 0, 3704 "loadOutput() invalid supported formats"); 3705 if ((profile->mSupportedDevices != AUDIO_DEVICE_NONE) && 3706 (profile->mChannelMasks.size() != 0) && 3707 (profile->mSamplingRates.size() != 0) && 3708 (profile->mFormats.size() != 0)) { 3709 3710 ALOGV("loadOutput() adding output mSupportedDevices %04x, mFlags %04x", 3711 profile->mSupportedDevices, profile->mFlags); 3712 3713 module->mOutputProfiles.add(profile); 3714 return NO_ERROR; 3715 } else { 3716 delete profile; 3717 return BAD_VALUE; 3718 } 3719} 3720 3721void AudioPolicyManagerBase::loadHwModule(cnode *root) 3722{ 3723 cnode *node = config_find(root, OUTPUTS_TAG); 3724 status_t status = NAME_NOT_FOUND; 3725 3726 HwModule *module = new HwModule(root->name); 3727 3728 if (node != NULL) { 3729 if (strcmp(root->name, AUDIO_HARDWARE_MODULE_ID_A2DP) == 0) { 3730 mHasA2dp = true; 3731 } else if (strcmp(root->name, AUDIO_HARDWARE_MODULE_ID_USB) == 0) { 3732 mHasUsb = true; 3733 } else if (strcmp(root->name, AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX) == 0) { 3734 mHasRemoteSubmix = true; 3735 } 3736 3737 node = node->first_child; 3738 while (node) { 3739 ALOGV("loadHwModule() loading output %s", node->name); 3740 status_t tmpStatus = loadOutput(node, module); 3741 if (status == NAME_NOT_FOUND || status == NO_ERROR) { 3742 status = tmpStatus; 3743 } 3744 node = node->next; 3745 } 3746 } 3747 node = config_find(root, INPUTS_TAG); 3748 if (node != NULL) { 3749 node = node->first_child; 3750 while (node) { 3751 ALOGV("loadHwModule() loading input %s", node->name); 3752 status_t tmpStatus = loadInput(node, module); 3753 if (status == NAME_NOT_FOUND || status == NO_ERROR) { 3754 status = tmpStatus; 3755 } 3756 node = node->next; 3757 } 3758 } 3759 if (status == NO_ERROR) { 3760 mHwModules.add(module); 3761 } else { 3762 delete module; 3763 } 3764} 3765 3766void AudioPolicyManagerBase::loadHwModules(cnode *root) 3767{ 3768 cnode *node = config_find(root, AUDIO_HW_MODULE_TAG); 3769 if (node == NULL) { 3770 return; 3771 } 3772 3773 node = node->first_child; 3774 while (node) { 3775 ALOGV("loadHwModules() loading module %s", node->name); 3776 loadHwModule(node); 3777 node = node->next; 3778 } 3779} 3780 3781void AudioPolicyManagerBase::loadGlobalConfig(cnode *root) 3782{ 3783 cnode *node = config_find(root, GLOBAL_CONFIG_TAG); 3784 if (node == NULL) { 3785 return; 3786 } 3787 node = node->first_child; 3788 while (node) { 3789 if (strcmp(ATTACHED_OUTPUT_DEVICES_TAG, node->name) == 0) { 3790 mAttachedOutputDevices = parseDeviceNames((char *)node->value); 3791 ALOGW_IF(mAttachedOutputDevices == AUDIO_DEVICE_NONE, 3792 "loadGlobalConfig() no attached output devices"); 3793 ALOGV("loadGlobalConfig() mAttachedOutputDevices %04x", mAttachedOutputDevices); 3794 } else if (strcmp(DEFAULT_OUTPUT_DEVICE_TAG, node->name) == 0) { 3795 mDefaultOutputDevice = (audio_devices_t)stringToEnum(sDeviceNameToEnumTable, 3796 ARRAY_SIZE(sDeviceNameToEnumTable), 3797 (char *)node->value); 3798 ALOGW_IF(mDefaultOutputDevice == AUDIO_DEVICE_NONE, 3799 "loadGlobalConfig() default device not specified"); 3800 ALOGV("loadGlobalConfig() mDefaultOutputDevice %04x", mDefaultOutputDevice); 3801 } else if (strcmp(ATTACHED_INPUT_DEVICES_TAG, node->name) == 0) { 3802 mAvailableInputDevices = parseDeviceNames((char *)node->value) & ~AUDIO_DEVICE_BIT_IN; 3803 ALOGV("loadGlobalConfig() mAvailableInputDevices %04x", mAvailableInputDevices); 3804 } 3805 node = node->next; 3806 } 3807} 3808 3809status_t AudioPolicyManagerBase::loadAudioPolicyConfig(const char *path) 3810{ 3811 cnode *root; 3812 char *data; 3813 3814 data = (char *)load_file(path, NULL); 3815 if (data == NULL) { 3816 return -ENODEV; 3817 } 3818 root = config_node("", ""); 3819 config_load(root, data); 3820 3821 loadGlobalConfig(root); 3822 loadHwModules(root); 3823 3824 config_free(root); 3825 free(root); 3826 free(data); 3827 3828 ALOGI("loadAudioPolicyConfig() loaded %s\n", path); 3829 3830 return NO_ERROR; 3831} 3832 3833void AudioPolicyManagerBase::defaultAudioPolicyConfig(void) 3834{ 3835 HwModule *module; 3836 IOProfile *profile; 3837 3838 mDefaultOutputDevice = AUDIO_DEVICE_OUT_SPEAKER; 3839 mAttachedOutputDevices = AUDIO_DEVICE_OUT_SPEAKER; 3840 mAvailableInputDevices = AUDIO_DEVICE_IN_BUILTIN_MIC & ~AUDIO_DEVICE_BIT_IN; 3841 3842 module = new HwModule("primary"); 3843 3844 profile = new IOProfile(module); 3845 profile->mSamplingRates.add(44100); 3846 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT); 3847 profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO); 3848 profile->mSupportedDevices = AUDIO_DEVICE_OUT_SPEAKER; 3849 profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY; 3850 module->mOutputProfiles.add(profile); 3851 3852 profile = new IOProfile(module); 3853 profile->mSamplingRates.add(8000); 3854 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT); 3855 profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO); 3856 profile->mSupportedDevices = AUDIO_DEVICE_IN_BUILTIN_MIC; 3857 module->mInputProfiles.add(profile); 3858 3859 mHwModules.add(module); 3860} 3861 3862}; // namespace android 3863