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