Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 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" |
Eric Laurent | f145108 | 2010-01-28 13:42:59 -0800 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 19 | #include <utils/Log.h> |
| 20 | #include <hardware_legacy/AudioPolicyManagerBase.h> |
| 21 | #include <media/mediarecorder.h> |
| 22 | |
| 23 | namespace android { |
| 24 | |
| 25 | |
| 26 | // ---------------------------------------------------------------------------- |
| 27 | // AudioPolicyInterface implementation |
| 28 | // ---------------------------------------------------------------------------- |
| 29 | |
| 30 | |
| 31 | status_t AudioPolicyManagerBase::setDeviceConnectionState(AudioSystem::audio_devices device, |
| 32 | AudioSystem::device_connection_state state, |
| 33 | const char *device_address) |
| 34 | { |
| 35 | |
| 36 | LOGV("setDeviceConnectionState() device: %x, state %d, address %s", device, state, device_address); |
| 37 | |
| 38 | // connect/disconnect only 1 device at a time |
| 39 | if (AudioSystem::popCount(device) != 1) return BAD_VALUE; |
| 40 | |
| 41 | if (strlen(device_address) >= MAX_DEVICE_ADDRESS_LEN) { |
| 42 | LOGE("setDeviceConnectionState() invalid address: %s", device_address); |
| 43 | return BAD_VALUE; |
| 44 | } |
| 45 | |
| 46 | // handle output devices |
| 47 | if (AudioSystem::isOutputDevice(device)) { |
| 48 | |
| 49 | #ifndef WITH_A2DP |
| 50 | if (AudioSystem::isA2dpDevice(device)) { |
| 51 | LOGE("setDeviceConnectionState() invalid device: %x", device); |
| 52 | return BAD_VALUE; |
| 53 | } |
| 54 | #endif |
| 55 | |
| 56 | switch (state) |
| 57 | { |
| 58 | // handle output device connection |
| 59 | case AudioSystem::DEVICE_STATE_AVAILABLE: |
| 60 | if (mAvailableOutputDevices & device) { |
| 61 | LOGW("setDeviceConnectionState() device already connected: %x", device); |
| 62 | return INVALID_OPERATION; |
| 63 | } |
| 64 | LOGV("setDeviceConnectionState() connecting device %x", device); |
| 65 | |
| 66 | // register new device as available |
| 67 | mAvailableOutputDevices |= device; |
| 68 | |
| 69 | #ifdef WITH_A2DP |
| 70 | // handle A2DP device connection |
| 71 | if (AudioSystem::isA2dpDevice(device)) { |
| 72 | status_t status = handleA2dpConnection(device, device_address); |
| 73 | if (status != NO_ERROR) { |
| 74 | mAvailableOutputDevices &= ~device; |
| 75 | return status; |
| 76 | } |
| 77 | } else |
| 78 | #endif |
| 79 | { |
| 80 | if (AudioSystem::isBluetoothScoDevice(device)) { |
| 81 | LOGV("setDeviceConnectionState() BT SCO device, address %s", device_address); |
| 82 | // keep track of SCO device address |
| 83 | mScoDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN); |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | break; |
| 87 | // handle output device disconnection |
| 88 | case AudioSystem::DEVICE_STATE_UNAVAILABLE: { |
| 89 | if (!(mAvailableOutputDevices & device)) { |
| 90 | LOGW("setDeviceConnectionState() device not connected: %x", device); |
| 91 | return INVALID_OPERATION; |
| 92 | } |
| 93 | |
| 94 | |
| 95 | LOGV("setDeviceConnectionState() disconnecting device %x", device); |
| 96 | // remove device from available output devices |
| 97 | mAvailableOutputDevices &= ~device; |
| 98 | |
| 99 | #ifdef WITH_A2DP |
| 100 | // handle A2DP device disconnection |
| 101 | if (AudioSystem::isA2dpDevice(device)) { |
| 102 | status_t status = handleA2dpDisconnection(device, device_address); |
| 103 | if (status != NO_ERROR) { |
| 104 | mAvailableOutputDevices |= device; |
| 105 | return status; |
| 106 | } |
| 107 | } else |
| 108 | #endif |
| 109 | { |
| 110 | if (AudioSystem::isBluetoothScoDevice(device)) { |
| 111 | mScoDeviceAddress = ""; |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | } break; |
| 115 | |
| 116 | default: |
| 117 | LOGE("setDeviceConnectionState() invalid state: %x", state); |
| 118 | return BAD_VALUE; |
| 119 | } |
| 120 | |
| 121 | // request routing change if necessary |
| 122 | uint32_t newDevice = getNewDevice(mHardwareOutput, false); |
| 123 | #ifdef WITH_A2DP |
Eric Laurent | b8453f4 | 2010-08-27 17:10:36 -0700 | [diff] [blame] | 124 | checkOutputForAllStrategies(); |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 125 | // A2DP outputs must be closed after checkOutputForAllStrategies() is executed |
| 126 | if (state == AudioSystem::DEVICE_STATE_UNAVAILABLE && AudioSystem::isA2dpDevice(device)) { |
| 127 | closeA2dpOutputs(); |
| 128 | } |
Eric Laurent | b87b53d | 2010-11-02 12:02:20 -0700 | [diff] [blame] | 129 | checkA2dpSuspend(); |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 130 | #endif |
| 131 | updateDeviceForStrategy(); |
| 132 | setOutputDevice(mHardwareOutput, newDevice); |
| 133 | |
| 134 | if (device == AudioSystem::DEVICE_OUT_WIRED_HEADSET) { |
| 135 | device = AudioSystem::DEVICE_IN_WIRED_HEADSET; |
| 136 | } else if (device == AudioSystem::DEVICE_OUT_BLUETOOTH_SCO || |
| 137 | device == AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_HEADSET || |
| 138 | device == AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_CARKIT) { |
| 139 | device = AudioSystem::DEVICE_IN_BLUETOOTH_SCO_HEADSET; |
| 140 | } else { |
| 141 | return NO_ERROR; |
| 142 | } |
| 143 | } |
| 144 | // handle input devices |
| 145 | if (AudioSystem::isInputDevice(device)) { |
| 146 | |
| 147 | switch (state) |
| 148 | { |
| 149 | // handle input device connection |
| 150 | case AudioSystem::DEVICE_STATE_AVAILABLE: { |
| 151 | if (mAvailableInputDevices & device) { |
| 152 | LOGW("setDeviceConnectionState() device already connected: %d", device); |
| 153 | return INVALID_OPERATION; |
| 154 | } |
| 155 | mAvailableInputDevices |= device; |
| 156 | } |
| 157 | break; |
| 158 | |
| 159 | // handle input device disconnection |
| 160 | case AudioSystem::DEVICE_STATE_UNAVAILABLE: { |
| 161 | if (!(mAvailableInputDevices & device)) { |
| 162 | LOGW("setDeviceConnectionState() device not connected: %d", device); |
| 163 | return INVALID_OPERATION; |
| 164 | } |
| 165 | mAvailableInputDevices &= ~device; |
| 166 | } break; |
| 167 | |
| 168 | default: |
| 169 | LOGE("setDeviceConnectionState() invalid state: %x", state); |
| 170 | return BAD_VALUE; |
| 171 | } |
| 172 | |
| 173 | audio_io_handle_t activeInput = getActiveInput(); |
| 174 | if (activeInput != 0) { |
| 175 | AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput); |
| 176 | uint32_t newDevice = getDeviceForInputSource(inputDesc->mInputSource); |
| 177 | if (newDevice != inputDesc->mDevice) { |
| 178 | LOGV("setDeviceConnectionState() changing device from %x to %x for input %d", |
| 179 | inputDesc->mDevice, newDevice, activeInput); |
| 180 | inputDesc->mDevice = newDevice; |
| 181 | AudioParameter param = AudioParameter(); |
| 182 | param.addInt(String8(AudioParameter::keyRouting), (int)newDevice); |
| 183 | mpClientInterface->setParameters(activeInput, param.toString()); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | return NO_ERROR; |
| 188 | } |
| 189 | |
| 190 | LOGW("setDeviceConnectionState() invalid device: %x", device); |
| 191 | return BAD_VALUE; |
| 192 | } |
| 193 | |
| 194 | AudioSystem::device_connection_state AudioPolicyManagerBase::getDeviceConnectionState(AudioSystem::audio_devices device, |
| 195 | const char *device_address) |
| 196 | { |
| 197 | AudioSystem::device_connection_state state = AudioSystem::DEVICE_STATE_UNAVAILABLE; |
| 198 | String8 address = String8(device_address); |
| 199 | if (AudioSystem::isOutputDevice(device)) { |
| 200 | if (device & mAvailableOutputDevices) { |
| 201 | #ifdef WITH_A2DP |
| 202 | if (AudioSystem::isA2dpDevice(device) && |
| 203 | address != "" && mA2dpDeviceAddress != address) { |
| 204 | return state; |
| 205 | } |
| 206 | #endif |
| 207 | if (AudioSystem::isBluetoothScoDevice(device) && |
| 208 | address != "" && mScoDeviceAddress != address) { |
| 209 | return state; |
| 210 | } |
| 211 | state = AudioSystem::DEVICE_STATE_AVAILABLE; |
| 212 | } |
| 213 | } else if (AudioSystem::isInputDevice(device)) { |
| 214 | if (device & mAvailableInputDevices) { |
| 215 | state = AudioSystem::DEVICE_STATE_AVAILABLE; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | return state; |
| 220 | } |
| 221 | |
| 222 | void AudioPolicyManagerBase::setPhoneState(int state) |
| 223 | { |
| 224 | LOGV("setPhoneState() state %d", state); |
| 225 | uint32_t newDevice = 0; |
| 226 | if (state < 0 || state >= AudioSystem::NUM_MODES) { |
| 227 | LOGW("setPhoneState() invalid state %d", state); |
| 228 | return; |
| 229 | } |
| 230 | |
| 231 | if (state == mPhoneState ) { |
| 232 | LOGW("setPhoneState() setting same state %d", state); |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | // if leaving call state, handle special case of active streams |
| 237 | // pertaining to sonification strategy see handleIncallSonification() |
Jean-Michel Trivi | 8f677d6 | 2010-11-15 12:11:32 -0800 | [diff] [blame] | 238 | if (isInCall()) { |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 239 | LOGV("setPhoneState() in call state management: new state is %d", state); |
| 240 | for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) { |
| 241 | handleIncallSonification(stream, false, true); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | // store previous phone state for management of sonification strategy below |
| 246 | int oldState = mPhoneState; |
| 247 | mPhoneState = state; |
| 248 | bool force = false; |
| 249 | |
| 250 | // are we entering or starting a call |
Jean-Michel Trivi | 8f677d6 | 2010-11-15 12:11:32 -0800 | [diff] [blame] | 251 | if (!isStateInCall(oldState) && isStateInCall(state)) { |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 252 | LOGV(" Entering call in setPhoneState()"); |
| 253 | // force routing command to audio hardware when starting a call |
| 254 | // even if no device change is needed |
| 255 | force = true; |
Jean-Michel Trivi | 8f677d6 | 2010-11-15 12:11:32 -0800 | [diff] [blame] | 256 | } else if (isStateInCall(oldState) && !isStateInCall(state)) { |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 257 | LOGV(" Exiting call in setPhoneState()"); |
| 258 | // force routing command to audio hardware when exiting a call |
| 259 | // even if no device change is needed |
| 260 | force = true; |
Jean-Michel Trivi | 8f677d6 | 2010-11-15 12:11:32 -0800 | [diff] [blame] | 261 | } else if (isStateInCall(state) && (state != oldState)) { |
| 262 | LOGV(" Switching between telephony and VoIP in setPhoneState()"); |
| 263 | // force routing command to audio hardware when switching between telephony and VoIP |
| 264 | // even if no device change is needed |
| 265 | force = true; |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | // check for device and output changes triggered by new phone state |
| 269 | newDevice = getNewDevice(mHardwareOutput, false); |
| 270 | #ifdef WITH_A2DP |
Eric Laurent | b8453f4 | 2010-08-27 17:10:36 -0700 | [diff] [blame] | 271 | checkOutputForAllStrategies(); |
Eric Laurent | b87b53d | 2010-11-02 12:02:20 -0700 | [diff] [blame] | 272 | checkA2dpSuspend(); |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 273 | #endif |
| 274 | updateDeviceForStrategy(); |
| 275 | |
| 276 | AudioOutputDescriptor *hwOutputDesc = mOutputs.valueFor(mHardwareOutput); |
| 277 | |
| 278 | // force routing command to audio hardware when ending call |
| 279 | // even if no device change is needed |
Jean-Michel Trivi | 8f677d6 | 2010-11-15 12:11:32 -0800 | [diff] [blame] | 280 | if (isStateInCall(oldState) && newDevice == 0) { |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 281 | newDevice = hwOutputDesc->device(); |
| 282 | } |
Eric Laurent | 22e1ca3 | 2010-02-23 09:48:31 -0800 | [diff] [blame] | 283 | |
| 284 | // when changing from ring tone to in call mode, mute the ringing tone |
| 285 | // immediately and delay the route change to avoid sending the ring tone |
| 286 | // tail into the earpiece or headset. |
| 287 | int delayMs = 0; |
Jean-Michel Trivi | 8f677d6 | 2010-11-15 12:11:32 -0800 | [diff] [blame] | 288 | if (isStateInCall(state) && oldState == AudioSystem::MODE_RINGTONE) { |
Eric Laurent | 22e1ca3 | 2010-02-23 09:48:31 -0800 | [diff] [blame] | 289 | // delay the device change command by twice the output latency to have some margin |
| 290 | // and be sure that audio buffers not yet affected by the mute are out when |
| 291 | // we actually apply the route change |
| 292 | delayMs = hwOutputDesc->mLatency*2; |
| 293 | setStreamMute(AudioSystem::RING, true, mHardwareOutput); |
| 294 | } |
| 295 | |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 296 | // change routing is necessary |
Eric Laurent | 22e1ca3 | 2010-02-23 09:48:31 -0800 | [diff] [blame] | 297 | setOutputDevice(mHardwareOutput, newDevice, force, delayMs); |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 298 | |
| 299 | // if entering in call state, handle special case of active streams |
| 300 | // pertaining to sonification strategy see handleIncallSonification() |
Jean-Michel Trivi | 8f677d6 | 2010-11-15 12:11:32 -0800 | [diff] [blame] | 301 | if (isStateInCall(state)) { |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 302 | LOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 22e1ca3 | 2010-02-23 09:48:31 -0800 | [diff] [blame] | 303 | // unmute the ringing tone after a sufficient delay if it was muted before |
| 304 | // setting output device above |
| 305 | if (oldState == AudioSystem::MODE_RINGTONE) { |
| 306 | setStreamMute(AudioSystem::RING, false, mHardwareOutput, MUTE_TIME_MS); |
| 307 | } |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 308 | for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) { |
| 309 | handleIncallSonification(stream, true, true); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE |
| 314 | if (state == AudioSystem::MODE_RINGTONE && |
| 315 | (hwOutputDesc->mRefCount[AudioSystem::MUSIC] || |
| 316 | (systemTime() - mMusicStopTime) < seconds(SONIFICATION_HEADSET_MUSIC_DELAY))) { |
| 317 | mLimitRingtoneVolume = true; |
| 318 | } else { |
| 319 | mLimitRingtoneVolume = false; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | void AudioPolicyManagerBase::setRingerMode(uint32_t mode, uint32_t mask) |
| 324 | { |
| 325 | LOGV("setRingerMode() mode %x, mask %x", mode, mask); |
| 326 | |
| 327 | mRingerMode = mode; |
| 328 | } |
| 329 | |
| 330 | void AudioPolicyManagerBase::setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config) |
| 331 | { |
| 332 | LOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState); |
| 333 | |
Jean-Michel Trivi | 758559e | 2010-03-09 09:26:08 -0800 | [diff] [blame] | 334 | bool forceVolumeReeval = false; |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 335 | switch(usage) { |
| 336 | case AudioSystem::FOR_COMMUNICATION: |
| 337 | if (config != AudioSystem::FORCE_SPEAKER && config != AudioSystem::FORCE_BT_SCO && |
| 338 | config != AudioSystem::FORCE_NONE) { |
| 339 | LOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config); |
| 340 | return; |
| 341 | } |
| 342 | mForceUse[usage] = config; |
| 343 | break; |
| 344 | case AudioSystem::FOR_MEDIA: |
| 345 | if (config != AudioSystem::FORCE_HEADPHONES && config != AudioSystem::FORCE_BT_A2DP && |
Praveen Bharathi | 21e941b | 2010-10-06 15:23:14 -0500 | [diff] [blame] | 346 | config != AudioSystem::FORCE_WIRED_ACCESSORY && |
| 347 | config != AudioSystem::FORCE_ANALOG_DOCK && |
| 348 | config != AudioSystem::FORCE_DIGITAL_DOCK && config != AudioSystem::FORCE_NONE) { |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 349 | LOGW("setForceUse() invalid config %d for FOR_MEDIA", config); |
| 350 | return; |
| 351 | } |
| 352 | mForceUse[usage] = config; |
| 353 | break; |
| 354 | case AudioSystem::FOR_RECORD: |
| 355 | if (config != AudioSystem::FORCE_BT_SCO && config != AudioSystem::FORCE_WIRED_ACCESSORY && |
| 356 | config != AudioSystem::FORCE_NONE) { |
| 357 | LOGW("setForceUse() invalid config %d for FOR_RECORD", config); |
| 358 | return; |
| 359 | } |
| 360 | mForceUse[usage] = config; |
| 361 | break; |
| 362 | case AudioSystem::FOR_DOCK: |
| 363 | if (config != AudioSystem::FORCE_NONE && config != AudioSystem::FORCE_BT_CAR_DOCK && |
Praveen Bharathi | 21e941b | 2010-10-06 15:23:14 -0500 | [diff] [blame] | 364 | config != AudioSystem::FORCE_BT_DESK_DOCK && |
| 365 | config != AudioSystem::FORCE_WIRED_ACCESSORY && |
| 366 | config != AudioSystem::FORCE_ANALOG_DOCK && |
| 367 | config != AudioSystem::FORCE_DIGITAL_DOCK) { |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 368 | LOGW("setForceUse() invalid config %d for FOR_DOCK", config); |
| 369 | } |
Jean-Michel Trivi | 758559e | 2010-03-09 09:26:08 -0800 | [diff] [blame] | 370 | forceVolumeReeval = true; |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 371 | mForceUse[usage] = config; |
| 372 | break; |
| 373 | default: |
| 374 | LOGW("setForceUse() invalid usage %d", usage); |
| 375 | break; |
| 376 | } |
| 377 | |
| 378 | // check for device and output changes triggered by new phone state |
| 379 | uint32_t newDevice = getNewDevice(mHardwareOutput, false); |
| 380 | #ifdef WITH_A2DP |
Eric Laurent | b8453f4 | 2010-08-27 17:10:36 -0700 | [diff] [blame] | 381 | checkOutputForAllStrategies(); |
Eric Laurent | b87b53d | 2010-11-02 12:02:20 -0700 | [diff] [blame] | 382 | checkA2dpSuspend(); |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 383 | #endif |
| 384 | updateDeviceForStrategy(); |
| 385 | setOutputDevice(mHardwareOutput, newDevice); |
Jean-Michel Trivi | 758559e | 2010-03-09 09:26:08 -0800 | [diff] [blame] | 386 | if (forceVolumeReeval) { |
| 387 | applyStreamVolumes(mHardwareOutput, newDevice); |
| 388 | } |
Eric Laurent | b8453f4 | 2010-08-27 17:10:36 -0700 | [diff] [blame] | 389 | |
| 390 | audio_io_handle_t activeInput = getActiveInput(); |
| 391 | if (activeInput != 0) { |
| 392 | AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput); |
| 393 | newDevice = getDeviceForInputSource(inputDesc->mInputSource); |
| 394 | if (newDevice != inputDesc->mDevice) { |
| 395 | LOGV("setForceUse() changing device from %x to %x for input %d", |
| 396 | inputDesc->mDevice, newDevice, activeInput); |
| 397 | inputDesc->mDevice = newDevice; |
| 398 | AudioParameter param = AudioParameter(); |
| 399 | param.addInt(String8(AudioParameter::keyRouting), (int)newDevice); |
| 400 | mpClientInterface->setParameters(activeInput, param.toString()); |
| 401 | } |
| 402 | } |
| 403 | |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | AudioSystem::forced_config AudioPolicyManagerBase::getForceUse(AudioSystem::force_use usage) |
| 407 | { |
| 408 | return mForceUse[usage]; |
| 409 | } |
| 410 | |
| 411 | void AudioPolicyManagerBase::setSystemProperty(const char* property, const char* value) |
| 412 | { |
| 413 | LOGV("setSystemProperty() property %s, value %s", property, value); |
| 414 | if (strcmp(property, "ro.camera.sound.forced") == 0) { |
| 415 | if (atoi(value)) { |
| 416 | LOGV("ENFORCED_AUDIBLE cannot be muted"); |
| 417 | mStreams[AudioSystem::ENFORCED_AUDIBLE].mCanBeMuted = false; |
| 418 | } else { |
| 419 | LOGV("ENFORCED_AUDIBLE can be muted"); |
| 420 | mStreams[AudioSystem::ENFORCED_AUDIBLE].mCanBeMuted = true; |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | audio_io_handle_t AudioPolicyManagerBase::getOutput(AudioSystem::stream_type stream, |
| 426 | uint32_t samplingRate, |
| 427 | uint32_t format, |
| 428 | uint32_t channels, |
| 429 | AudioSystem::output_flags flags) |
| 430 | { |
| 431 | audio_io_handle_t output = 0; |
| 432 | uint32_t latency = 0; |
| 433 | routing_strategy strategy = getStrategy((AudioSystem::stream_type)stream); |
| 434 | uint32_t device = getDeviceForStrategy(strategy); |
| 435 | LOGV("getOutput() stream %d, samplingRate %d, format %d, channels %x, flags %x", stream, samplingRate, format, channels, flags); |
| 436 | |
| 437 | #ifdef AUDIO_POLICY_TEST |
| 438 | if (mCurOutput != 0) { |
| 439 | LOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channels %x, mDirectOutput %d", |
| 440 | mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput); |
| 441 | |
| 442 | if (mTestOutputs[mCurOutput] == 0) { |
| 443 | LOGV("getOutput() opening test output"); |
| 444 | AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(); |
| 445 | outputDesc->mDevice = mTestDevice; |
| 446 | outputDesc->mSamplingRate = mTestSamplingRate; |
| 447 | outputDesc->mFormat = mTestFormat; |
| 448 | outputDesc->mChannels = mTestChannels; |
| 449 | outputDesc->mLatency = mTestLatencyMs; |
| 450 | outputDesc->mFlags = (AudioSystem::output_flags)(mDirectOutput ? AudioSystem::OUTPUT_FLAG_DIRECT : 0); |
| 451 | outputDesc->mRefCount[stream] = 0; |
| 452 | mTestOutputs[mCurOutput] = mpClientInterface->openOutput(&outputDesc->mDevice, |
| 453 | &outputDesc->mSamplingRate, |
| 454 | &outputDesc->mFormat, |
| 455 | &outputDesc->mChannels, |
| 456 | &outputDesc->mLatency, |
| 457 | outputDesc->mFlags); |
| 458 | if (mTestOutputs[mCurOutput]) { |
| 459 | AudioParameter outputCmd = AudioParameter(); |
| 460 | outputCmd.addInt(String8("set_id"),mCurOutput); |
| 461 | mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString()); |
| 462 | addOutput(mTestOutputs[mCurOutput], outputDesc); |
| 463 | } |
| 464 | } |
| 465 | return mTestOutputs[mCurOutput]; |
| 466 | } |
| 467 | #endif //AUDIO_POLICY_TEST |
| 468 | |
Eric Laurent | ef9500f | 2010-03-11 14:47:00 -0800 | [diff] [blame] | 469 | // open a direct output if required by specified parameters |
| 470 | if (needsDirectOuput(stream, samplingRate, format, channels, flags, device)) { |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 471 | |
| 472 | LOGV("getOutput() opening direct output device %x", device); |
| 473 | AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(); |
| 474 | outputDesc->mDevice = device; |
| 475 | outputDesc->mSamplingRate = samplingRate; |
| 476 | outputDesc->mFormat = format; |
| 477 | outputDesc->mChannels = channels; |
| 478 | outputDesc->mLatency = 0; |
| 479 | outputDesc->mFlags = (AudioSystem::output_flags)(flags | AudioSystem::OUTPUT_FLAG_DIRECT); |
Eric Laurent | ef9500f | 2010-03-11 14:47:00 -0800 | [diff] [blame] | 480 | outputDesc->mRefCount[stream] = 0; |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 481 | output = mpClientInterface->openOutput(&outputDesc->mDevice, |
| 482 | &outputDesc->mSamplingRate, |
| 483 | &outputDesc->mFormat, |
| 484 | &outputDesc->mChannels, |
| 485 | &outputDesc->mLatency, |
| 486 | outputDesc->mFlags); |
| 487 | |
| 488 | // only accept an output with the requeted parameters |
| 489 | if (output == 0 || |
| 490 | (samplingRate != 0 && samplingRate != outputDesc->mSamplingRate) || |
| 491 | (format != 0 && format != outputDesc->mFormat) || |
| 492 | (channels != 0 && channels != outputDesc->mChannels)) { |
| 493 | LOGV("getOutput() failed opening direct output: samplingRate %d, format %d, channels %d", |
| 494 | samplingRate, format, channels); |
| 495 | if (output != 0) { |
| 496 | mpClientInterface->closeOutput(output); |
| 497 | } |
| 498 | delete outputDesc; |
| 499 | return 0; |
| 500 | } |
| 501 | addOutput(output, outputDesc); |
| 502 | return output; |
| 503 | } |
| 504 | |
| 505 | if (channels != 0 && channels != AudioSystem::CHANNEL_OUT_MONO && |
| 506 | channels != AudioSystem::CHANNEL_OUT_STEREO) { |
| 507 | return 0; |
| 508 | } |
| 509 | // open a non direct output |
| 510 | |
| 511 | // get which output is suitable for the specified stream. The actual routing change will happen |
| 512 | // when startOutput() will be called |
| 513 | uint32_t a2dpDevice = device & AudioSystem::DEVICE_OUT_ALL_A2DP; |
| 514 | if (AudioSystem::popCount((AudioSystem::audio_devices)device) == 2) { |
| 515 | #ifdef WITH_A2DP |
| 516 | if (a2dpUsedForSonification() && a2dpDevice != 0) { |
| 517 | // if playing on 2 devices among which one is A2DP, use duplicated output |
| 518 | LOGV("getOutput() using duplicated output"); |
| 519 | LOGW_IF((mA2dpOutput == 0), "getOutput() A2DP device in multiple %x selected but A2DP output not opened", device); |
| 520 | output = mDuplicatedOutput; |
| 521 | } else |
| 522 | #endif |
| 523 | { |
| 524 | // if playing on 2 devices among which none is A2DP, use hardware output |
| 525 | output = mHardwareOutput; |
| 526 | } |
| 527 | LOGV("getOutput() using output %d for 2 devices %x", output, device); |
| 528 | } else { |
| 529 | #ifdef WITH_A2DP |
| 530 | if (a2dpDevice != 0) { |
| 531 | // if playing on A2DP device, use a2dp output |
| 532 | LOGW_IF((mA2dpOutput == 0), "getOutput() A2DP device %x selected but A2DP output not opened", device); |
| 533 | output = mA2dpOutput; |
| 534 | } else |
| 535 | #endif |
| 536 | { |
| 537 | // if playing on not A2DP device, use hardware output |
| 538 | output = mHardwareOutput; |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | |
| 543 | LOGW_IF((output ==0), "getOutput() could not find output for stream %d, samplingRate %d, format %d, channels %x, flags %x", |
| 544 | stream, samplingRate, format, channels, flags); |
| 545 | |
| 546 | return output; |
| 547 | } |
| 548 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 549 | status_t AudioPolicyManagerBase::startOutput(audio_io_handle_t output, |
| 550 | AudioSystem::stream_type stream, |
| 551 | int session) |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 552 | { |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 553 | LOGV("startOutput() output %d, stream %d, session %d", output, stream, session); |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 554 | ssize_t index = mOutputs.indexOfKey(output); |
| 555 | if (index < 0) { |
| 556 | LOGW("startOutput() unknow output %d", output); |
| 557 | return BAD_VALUE; |
| 558 | } |
| 559 | |
| 560 | AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index); |
| 561 | routing_strategy strategy = getStrategy((AudioSystem::stream_type)stream); |
| 562 | |
| 563 | #ifdef WITH_A2DP |
| 564 | if (mA2dpOutput != 0 && !a2dpUsedForSonification() && strategy == STRATEGY_SONIFICATION) { |
| 565 | setStrategyMute(STRATEGY_MEDIA, true, mA2dpOutput); |
| 566 | } |
| 567 | #endif |
| 568 | |
| 569 | // incremenent usage count for this stream on the requested output: |
| 570 | // NOTE that the usage count is the same for duplicated output and hardware output which is |
| 571 | // necassary for a correct control of hardware output routing by startOutput() and stopOutput() |
| 572 | outputDesc->changeRefCount(stream, 1); |
| 573 | |
| 574 | setOutputDevice(output, getNewDevice(output)); |
| 575 | |
| 576 | // handle special case for sonification while in call |
Jean-Michel Trivi | 8f677d6 | 2010-11-15 12:11:32 -0800 | [diff] [blame] | 577 | if (isInCall()) { |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 578 | handleIncallSonification(stream, true, false); |
| 579 | } |
| 580 | |
| 581 | // apply volume rules for current stream and device if necessary |
| 582 | checkAndSetVolume(stream, mStreams[stream].mIndexCur, output, outputDesc->device()); |
| 583 | |
| 584 | return NO_ERROR; |
| 585 | } |
| 586 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 587 | status_t AudioPolicyManagerBase::stopOutput(audio_io_handle_t output, |
| 588 | AudioSystem::stream_type stream, |
| 589 | int session) |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 590 | { |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 591 | LOGV("stopOutput() output %d, stream %d, session %d", output, stream, session); |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 592 | ssize_t index = mOutputs.indexOfKey(output); |
| 593 | if (index < 0) { |
| 594 | LOGW("stopOutput() unknow output %d", output); |
| 595 | return BAD_VALUE; |
| 596 | } |
| 597 | |
| 598 | AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index); |
| 599 | routing_strategy strategy = getStrategy((AudioSystem::stream_type)stream); |
| 600 | |
| 601 | // handle special case for sonification while in call |
Jean-Michel Trivi | 8f677d6 | 2010-11-15 12:11:32 -0800 | [diff] [blame] | 602 | if (isInCall()) { |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 603 | handleIncallSonification(stream, false, false); |
| 604 | } |
| 605 | |
| 606 | if (outputDesc->mRefCount[stream] > 0) { |
| 607 | // decrement usage count of this stream on the output |
| 608 | outputDesc->changeRefCount(stream, -1); |
| 609 | // store time at which the last music track was stopped - see computeVolume() |
| 610 | if (stream == AudioSystem::MUSIC) { |
| 611 | mMusicStopTime = systemTime(); |
| 612 | } |
| 613 | |
| 614 | setOutputDevice(output, getNewDevice(output)); |
| 615 | |
| 616 | #ifdef WITH_A2DP |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 617 | if (mA2dpOutput != 0 && !a2dpUsedForSonification() && |
| 618 | strategy == STRATEGY_SONIFICATION) { |
| 619 | setStrategyMute(STRATEGY_MEDIA, |
| 620 | false, |
| 621 | mA2dpOutput, |
| 622 | mOutputs.valueFor(mHardwareOutput)->mLatency*2); |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 623 | } |
| 624 | #endif |
Eric Laurent | ef9500f | 2010-03-11 14:47:00 -0800 | [diff] [blame] | 625 | if (output != mHardwareOutput) { |
| 626 | setOutputDevice(mHardwareOutput, getNewDevice(mHardwareOutput), true); |
| 627 | } |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 628 | return NO_ERROR; |
| 629 | } else { |
| 630 | LOGW("stopOutput() refcount is already 0 for output %d", output); |
| 631 | return INVALID_OPERATION; |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | void AudioPolicyManagerBase::releaseOutput(audio_io_handle_t output) |
| 636 | { |
| 637 | LOGV("releaseOutput() %d", output); |
| 638 | ssize_t index = mOutputs.indexOfKey(output); |
| 639 | if (index < 0) { |
| 640 | LOGW("releaseOutput() releasing unknown output %d", output); |
| 641 | return; |
| 642 | } |
| 643 | |
| 644 | #ifdef AUDIO_POLICY_TEST |
| 645 | int testIndex = testOutputIndex(output); |
| 646 | if (testIndex != 0) { |
| 647 | AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index); |
| 648 | if (outputDesc->refCount() == 0) { |
| 649 | mpClientInterface->closeOutput(output); |
| 650 | delete mOutputs.valueAt(index); |
| 651 | mOutputs.removeItem(output); |
| 652 | mTestOutputs[testIndex] = 0; |
| 653 | } |
| 654 | return; |
| 655 | } |
| 656 | #endif //AUDIO_POLICY_TEST |
| 657 | |
| 658 | if (mOutputs.valueAt(index)->mFlags & AudioSystem::OUTPUT_FLAG_DIRECT) { |
| 659 | mpClientInterface->closeOutput(output); |
| 660 | delete mOutputs.valueAt(index); |
| 661 | mOutputs.removeItem(output); |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | audio_io_handle_t AudioPolicyManagerBase::getInput(int inputSource, |
| 666 | uint32_t samplingRate, |
| 667 | uint32_t format, |
| 668 | uint32_t channels, |
| 669 | AudioSystem::audio_in_acoustics acoustics) |
| 670 | { |
| 671 | audio_io_handle_t input = 0; |
| 672 | uint32_t device = getDeviceForInputSource(inputSource); |
| 673 | |
| 674 | LOGV("getInput() inputSource %d, samplingRate %d, format %d, channels %x, acoustics %x", inputSource, samplingRate, format, channels, acoustics); |
| 675 | |
| 676 | if (device == 0) { |
| 677 | return 0; |
| 678 | } |
| 679 | |
| 680 | // adapt channel selection to input source |
| 681 | switch(inputSource) { |
| 682 | case AUDIO_SOURCE_VOICE_UPLINK: |
| 683 | channels = AudioSystem::CHANNEL_IN_VOICE_UPLINK; |
| 684 | break; |
| 685 | case AUDIO_SOURCE_VOICE_DOWNLINK: |
| 686 | channels = AudioSystem::CHANNEL_IN_VOICE_DNLINK; |
| 687 | break; |
| 688 | case AUDIO_SOURCE_VOICE_CALL: |
| 689 | channels = (AudioSystem::CHANNEL_IN_VOICE_UPLINK | AudioSystem::CHANNEL_IN_VOICE_DNLINK); |
| 690 | break; |
| 691 | default: |
| 692 | break; |
| 693 | } |
| 694 | |
| 695 | AudioInputDescriptor *inputDesc = new AudioInputDescriptor(); |
| 696 | |
| 697 | inputDesc->mInputSource = inputSource; |
| 698 | inputDesc->mDevice = device; |
| 699 | inputDesc->mSamplingRate = samplingRate; |
| 700 | inputDesc->mFormat = format; |
| 701 | inputDesc->mChannels = channels; |
| 702 | inputDesc->mAcoustics = acoustics; |
| 703 | inputDesc->mRefCount = 0; |
| 704 | input = mpClientInterface->openInput(&inputDesc->mDevice, |
| 705 | &inputDesc->mSamplingRate, |
| 706 | &inputDesc->mFormat, |
| 707 | &inputDesc->mChannels, |
| 708 | inputDesc->mAcoustics); |
| 709 | |
| 710 | // only accept input with the exact requested set of parameters |
| 711 | if (input == 0 || |
| 712 | (samplingRate != inputDesc->mSamplingRate) || |
| 713 | (format != inputDesc->mFormat) || |
| 714 | (channels != inputDesc->mChannels)) { |
| 715 | LOGV("getInput() failed opening input: samplingRate %d, format %d, channels %d", |
| 716 | samplingRate, format, channels); |
| 717 | if (input != 0) { |
| 718 | mpClientInterface->closeInput(input); |
| 719 | } |
| 720 | delete inputDesc; |
| 721 | return 0; |
| 722 | } |
| 723 | mInputs.add(input, inputDesc); |
| 724 | return input; |
| 725 | } |
| 726 | |
| 727 | status_t AudioPolicyManagerBase::startInput(audio_io_handle_t input) |
| 728 | { |
| 729 | LOGV("startInput() input %d", input); |
| 730 | ssize_t index = mInputs.indexOfKey(input); |
| 731 | if (index < 0) { |
| 732 | LOGW("startInput() unknow input %d", input); |
| 733 | return BAD_VALUE; |
| 734 | } |
| 735 | AudioInputDescriptor *inputDesc = mInputs.valueAt(index); |
| 736 | |
| 737 | #ifdef AUDIO_POLICY_TEST |
| 738 | if (mTestInput == 0) |
| 739 | #endif //AUDIO_POLICY_TEST |
| 740 | { |
| 741 | // refuse 2 active AudioRecord clients at the same time |
| 742 | if (getActiveInput() != 0) { |
| 743 | LOGW("startInput() input %d failed: other input already started", input); |
| 744 | return INVALID_OPERATION; |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | AudioParameter param = AudioParameter(); |
| 749 | param.addInt(String8(AudioParameter::keyRouting), (int)inputDesc->mDevice); |
| 750 | |
Jean-Michel Trivi | 1a22bdb | 2010-11-09 14:06:52 -0800 | [diff] [blame] | 751 | param.addInt(String8(AudioParameter::keyInputSource), (int)inputDesc->mInputSource); |
| 752 | LOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource); |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 753 | |
| 754 | mpClientInterface->setParameters(input, param.toString()); |
| 755 | |
| 756 | inputDesc->mRefCount = 1; |
| 757 | return NO_ERROR; |
| 758 | } |
| 759 | |
| 760 | status_t AudioPolicyManagerBase::stopInput(audio_io_handle_t input) |
| 761 | { |
| 762 | LOGV("stopInput() input %d", input); |
| 763 | ssize_t index = mInputs.indexOfKey(input); |
| 764 | if (index < 0) { |
| 765 | LOGW("stopInput() unknow input %d", input); |
| 766 | return BAD_VALUE; |
| 767 | } |
| 768 | AudioInputDescriptor *inputDesc = mInputs.valueAt(index); |
| 769 | |
| 770 | if (inputDesc->mRefCount == 0) { |
| 771 | LOGW("stopInput() input %d already stopped", input); |
| 772 | return INVALID_OPERATION; |
| 773 | } else { |
| 774 | AudioParameter param = AudioParameter(); |
| 775 | param.addInt(String8(AudioParameter::keyRouting), 0); |
| 776 | mpClientInterface->setParameters(input, param.toString()); |
| 777 | inputDesc->mRefCount = 0; |
| 778 | return NO_ERROR; |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | void AudioPolicyManagerBase::releaseInput(audio_io_handle_t input) |
| 783 | { |
| 784 | LOGV("releaseInput() %d", input); |
| 785 | ssize_t index = mInputs.indexOfKey(input); |
| 786 | if (index < 0) { |
| 787 | LOGW("releaseInput() releasing unknown input %d", input); |
| 788 | return; |
| 789 | } |
| 790 | mpClientInterface->closeInput(input); |
| 791 | delete mInputs.valueAt(index); |
| 792 | mInputs.removeItem(input); |
| 793 | LOGV("releaseInput() exit"); |
| 794 | } |
| 795 | |
| 796 | void AudioPolicyManagerBase::initStreamVolume(AudioSystem::stream_type stream, |
| 797 | int indexMin, |
| 798 | int indexMax) |
| 799 | { |
| 800 | LOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); |
| 801 | if (indexMin < 0 || indexMin >= indexMax) { |
| 802 | LOGW("initStreamVolume() invalid index limits for stream %d, min %d, max %d", stream , indexMin, indexMax); |
| 803 | return; |
| 804 | } |
| 805 | mStreams[stream].mIndexMin = indexMin; |
| 806 | mStreams[stream].mIndexMax = indexMax; |
| 807 | } |
| 808 | |
| 809 | status_t AudioPolicyManagerBase::setStreamVolumeIndex(AudioSystem::stream_type stream, int index) |
| 810 | { |
| 811 | |
| 812 | if ((index < mStreams[stream].mIndexMin) || (index > mStreams[stream].mIndexMax)) { |
| 813 | return BAD_VALUE; |
| 814 | } |
| 815 | |
| 816 | // Force max volume if stream cannot be muted |
| 817 | if (!mStreams[stream].mCanBeMuted) index = mStreams[stream].mIndexMax; |
| 818 | |
| 819 | LOGV("setStreamVolumeIndex() stream %d, index %d", stream, index); |
| 820 | mStreams[stream].mIndexCur = index; |
| 821 | |
| 822 | // compute and apply stream volume on all outputs according to connected device |
| 823 | status_t status = NO_ERROR; |
| 824 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 825 | status_t volStatus = checkAndSetVolume(stream, index, mOutputs.keyAt(i), mOutputs.valueAt(i)->device()); |
| 826 | if (volStatus != NO_ERROR) { |
| 827 | status = volStatus; |
| 828 | } |
| 829 | } |
| 830 | return status; |
| 831 | } |
| 832 | |
| 833 | status_t AudioPolicyManagerBase::getStreamVolumeIndex(AudioSystem::stream_type stream, int *index) |
| 834 | { |
| 835 | if (index == 0) { |
| 836 | return BAD_VALUE; |
| 837 | } |
| 838 | LOGV("getStreamVolumeIndex() stream %d", stream); |
| 839 | *index = mStreams[stream].mIndexCur; |
| 840 | return NO_ERROR; |
| 841 | } |
| 842 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 843 | audio_io_handle_t AudioPolicyManagerBase::getOutputForEffect(effect_descriptor_t *desc) |
| 844 | { |
| 845 | LOGV("getOutputForEffect()"); |
| 846 | // apply simple rule where global effects are attached to the same output as MUSIC streams |
| 847 | return getOutput(AudioSystem::MUSIC); |
| 848 | } |
| 849 | |
| 850 | status_t AudioPolicyManagerBase::registerEffect(effect_descriptor_t *desc, |
| 851 | audio_io_handle_t output, |
| 852 | uint32_t strategy, |
| 853 | int session, |
| 854 | int id) |
| 855 | { |
| 856 | ssize_t index = mOutputs.indexOfKey(output); |
| 857 | if (index < 0) { |
| 858 | LOGW("registerEffect() unknown output %d", output); |
| 859 | return INVALID_OPERATION; |
| 860 | } |
| 861 | |
| 862 | if (mTotalEffectsCpuLoad + desc->cpuLoad > getMaxEffectsCpuLoad()) { |
| 863 | LOGW("registerEffect() CPU Load limit exceeded for Fx %s, CPU %f MIPS", |
| 864 | desc->name, (float)desc->cpuLoad/10); |
| 865 | return INVALID_OPERATION; |
| 866 | } |
| 867 | if (mTotalEffectsMemory + desc->memoryUsage > getMaxEffectsMemory()) { |
| 868 | LOGW("registerEffect() memory limit exceeded for Fx %s, Memory %d KB", |
| 869 | desc->name, desc->memoryUsage); |
| 870 | return INVALID_OPERATION; |
| 871 | } |
| 872 | mTotalEffectsCpuLoad += desc->cpuLoad; |
| 873 | mTotalEffectsMemory += desc->memoryUsage; |
| 874 | LOGV("registerEffect() effect %s, output %d, strategy %d session %d id %d", |
| 875 | desc->name, output, strategy, session, id); |
| 876 | |
| 877 | LOGV("registerEffect() CPU %d, memory %d", desc->cpuLoad, desc->memoryUsage); |
| 878 | LOGV(" total CPU %d, total memory %d", mTotalEffectsCpuLoad, mTotalEffectsMemory); |
| 879 | |
| 880 | EffectDescriptor *pDesc = new EffectDescriptor(); |
| 881 | memcpy (&pDesc->mDesc, desc, sizeof(effect_descriptor_t)); |
| 882 | pDesc->mOutput = output; |
| 883 | pDesc->mStrategy = (routing_strategy)strategy; |
| 884 | pDesc->mSession = session; |
| 885 | mEffects.add(id, pDesc); |
| 886 | |
| 887 | return NO_ERROR; |
| 888 | } |
| 889 | |
| 890 | status_t AudioPolicyManagerBase::unregisterEffect(int id) |
| 891 | { |
| 892 | ssize_t index = mEffects.indexOfKey(id); |
| 893 | if (index < 0) { |
| 894 | LOGW("unregisterEffect() unknown effect ID %d", id); |
| 895 | return INVALID_OPERATION; |
| 896 | } |
| 897 | |
| 898 | EffectDescriptor *pDesc = mEffects.valueAt(index); |
| 899 | |
| 900 | if (mTotalEffectsCpuLoad < pDesc->mDesc.cpuLoad) { |
| 901 | LOGW("unregisterEffect() CPU load %d too high for total %d", |
| 902 | pDesc->mDesc.cpuLoad, mTotalEffectsCpuLoad); |
| 903 | pDesc->mDesc.cpuLoad = mTotalEffectsCpuLoad; |
| 904 | } |
| 905 | mTotalEffectsCpuLoad -= pDesc->mDesc.cpuLoad; |
| 906 | if (mTotalEffectsMemory < pDesc->mDesc.memoryUsage) { |
| 907 | LOGW("unregisterEffect() memory %d too big for total %d", |
| 908 | pDesc->mDesc.memoryUsage, mTotalEffectsMemory); |
| 909 | pDesc->mDesc.memoryUsage = mTotalEffectsMemory; |
| 910 | } |
| 911 | mTotalEffectsMemory -= pDesc->mDesc.memoryUsage; |
| 912 | LOGV("unregisterEffect() effect %s, ID %d, CPU %d, memory %d", |
| 913 | pDesc->mDesc.name, id, pDesc->mDesc.cpuLoad, pDesc->mDesc.memoryUsage); |
| 914 | LOGV(" total CPU %d, total memory %d", mTotalEffectsCpuLoad, mTotalEffectsMemory); |
| 915 | |
| 916 | mEffects.removeItem(id); |
| 917 | delete pDesc; |
| 918 | |
| 919 | return NO_ERROR; |
| 920 | } |
| 921 | |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 922 | status_t AudioPolicyManagerBase::dump(int fd) |
| 923 | { |
| 924 | const size_t SIZE = 256; |
| 925 | char buffer[SIZE]; |
| 926 | String8 result; |
| 927 | |
| 928 | snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this); |
| 929 | result.append(buffer); |
| 930 | snprintf(buffer, SIZE, " Hardware Output: %d\n", mHardwareOutput); |
| 931 | result.append(buffer); |
| 932 | #ifdef WITH_A2DP |
| 933 | snprintf(buffer, SIZE, " A2DP Output: %d\n", mA2dpOutput); |
| 934 | result.append(buffer); |
| 935 | snprintf(buffer, SIZE, " Duplicated Output: %d\n", mDuplicatedOutput); |
| 936 | result.append(buffer); |
| 937 | snprintf(buffer, SIZE, " A2DP device address: %s\n", mA2dpDeviceAddress.string()); |
| 938 | result.append(buffer); |
| 939 | #endif |
| 940 | snprintf(buffer, SIZE, " SCO device address: %s\n", mScoDeviceAddress.string()); |
| 941 | result.append(buffer); |
| 942 | snprintf(buffer, SIZE, " Output devices: %08x\n", mAvailableOutputDevices); |
| 943 | result.append(buffer); |
| 944 | snprintf(buffer, SIZE, " Input devices: %08x\n", mAvailableInputDevices); |
| 945 | result.append(buffer); |
| 946 | snprintf(buffer, SIZE, " Phone state: %d\n", mPhoneState); |
| 947 | result.append(buffer); |
| 948 | snprintf(buffer, SIZE, " Ringer mode: %d\n", mRingerMode); |
| 949 | result.append(buffer); |
| 950 | snprintf(buffer, SIZE, " Force use for communications %d\n", mForceUse[AudioSystem::FOR_COMMUNICATION]); |
| 951 | result.append(buffer); |
| 952 | snprintf(buffer, SIZE, " Force use for media %d\n", mForceUse[AudioSystem::FOR_MEDIA]); |
| 953 | result.append(buffer); |
| 954 | snprintf(buffer, SIZE, " Force use for record %d\n", mForceUse[AudioSystem::FOR_RECORD]); |
| 955 | result.append(buffer); |
| 956 | snprintf(buffer, SIZE, " Force use for dock %d\n", mForceUse[AudioSystem::FOR_DOCK]); |
| 957 | result.append(buffer); |
| 958 | write(fd, result.string(), result.size()); |
| 959 | |
| 960 | snprintf(buffer, SIZE, "\nOutputs dump:\n"); |
| 961 | write(fd, buffer, strlen(buffer)); |
| 962 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 963 | snprintf(buffer, SIZE, "- Output %d dump:\n", mOutputs.keyAt(i)); |
| 964 | write(fd, buffer, strlen(buffer)); |
| 965 | mOutputs.valueAt(i)->dump(fd); |
| 966 | } |
| 967 | |
| 968 | snprintf(buffer, SIZE, "\nInputs dump:\n"); |
| 969 | write(fd, buffer, strlen(buffer)); |
| 970 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 971 | snprintf(buffer, SIZE, "- Input %d dump:\n", mInputs.keyAt(i)); |
| 972 | write(fd, buffer, strlen(buffer)); |
| 973 | mInputs.valueAt(i)->dump(fd); |
| 974 | } |
| 975 | |
| 976 | snprintf(buffer, SIZE, "\nStreams dump:\n"); |
| 977 | write(fd, buffer, strlen(buffer)); |
| 978 | snprintf(buffer, SIZE, " Stream Index Min Index Max Index Cur Can be muted\n"); |
| 979 | write(fd, buffer, strlen(buffer)); |
| 980 | for (size_t i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) { |
| 981 | snprintf(buffer, SIZE, " %02d", i); |
| 982 | mStreams[i].dump(buffer + 3, SIZE); |
| 983 | write(fd, buffer, strlen(buffer)); |
| 984 | } |
| 985 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 986 | snprintf(buffer, SIZE, "\nTotal Effects CPU: %f MIPS, Total Effects memory: %d KB\n", |
| 987 | (float)mTotalEffectsCpuLoad/10, mTotalEffectsMemory); |
| 988 | write(fd, buffer, strlen(buffer)); |
| 989 | |
| 990 | snprintf(buffer, SIZE, "Registered effects:\n"); |
| 991 | write(fd, buffer, strlen(buffer)); |
| 992 | for (size_t i = 0; i < mEffects.size(); i++) { |
| 993 | snprintf(buffer, SIZE, "- Effect %d dump:\n", mEffects.keyAt(i)); |
| 994 | write(fd, buffer, strlen(buffer)); |
| 995 | mEffects.valueAt(i)->dump(fd); |
| 996 | } |
| 997 | |
| 998 | |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 999 | return NO_ERROR; |
| 1000 | } |
| 1001 | |
| 1002 | // ---------------------------------------------------------------------------- |
| 1003 | // AudioPolicyManagerBase |
| 1004 | // ---------------------------------------------------------------------------- |
| 1005 | |
| 1006 | AudioPolicyManagerBase::AudioPolicyManagerBase(AudioPolicyClientInterface *clientInterface) |
| 1007 | : |
| 1008 | #ifdef AUDIO_POLICY_TEST |
| 1009 | Thread(false), |
| 1010 | #endif //AUDIO_POLICY_TEST |
Eric Laurent | fad7787 | 2010-12-01 12:11:10 -0800 | [diff] [blame] | 1011 | mPhoneState(AudioSystem::MODE_NORMAL), mRingerMode(0), |
| 1012 | mMusicStopTime(0), mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), |
| 1013 | mTotalEffectsCpuLoad(0), mTotalEffectsMemory(0), |
Eric Laurent | b87b53d | 2010-11-02 12:02:20 -0700 | [diff] [blame] | 1014 | mA2dpSuspended(false) |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1015 | { |
| 1016 | mpClientInterface = clientInterface; |
| 1017 | |
| 1018 | for (int i = 0; i < AudioSystem::NUM_FORCE_USE; i++) { |
| 1019 | mForceUse[i] = AudioSystem::FORCE_NONE; |
| 1020 | } |
| 1021 | |
| 1022 | // devices available by default are speaker, ear piece and microphone |
| 1023 | mAvailableOutputDevices = AudioSystem::DEVICE_OUT_EARPIECE | |
| 1024 | AudioSystem::DEVICE_OUT_SPEAKER; |
| 1025 | mAvailableInputDevices = AudioSystem::DEVICE_IN_BUILTIN_MIC; |
| 1026 | |
| 1027 | #ifdef WITH_A2DP |
| 1028 | mA2dpOutput = 0; |
| 1029 | mDuplicatedOutput = 0; |
| 1030 | mA2dpDeviceAddress = String8(""); |
| 1031 | #endif |
| 1032 | mScoDeviceAddress = String8(""); |
| 1033 | |
| 1034 | // open hardware output |
| 1035 | AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(); |
| 1036 | outputDesc->mDevice = (uint32_t)AudioSystem::DEVICE_OUT_SPEAKER; |
| 1037 | mHardwareOutput = mpClientInterface->openOutput(&outputDesc->mDevice, |
| 1038 | &outputDesc->mSamplingRate, |
| 1039 | &outputDesc->mFormat, |
| 1040 | &outputDesc->mChannels, |
| 1041 | &outputDesc->mLatency, |
| 1042 | outputDesc->mFlags); |
| 1043 | |
| 1044 | if (mHardwareOutput == 0) { |
| 1045 | LOGE("Failed to initialize hardware output stream, samplingRate: %d, format %d, channels %d", |
| 1046 | outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannels); |
| 1047 | } else { |
| 1048 | addOutput(mHardwareOutput, outputDesc); |
| 1049 | setOutputDevice(mHardwareOutput, (uint32_t)AudioSystem::DEVICE_OUT_SPEAKER, true); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1050 | //TODO: configure audio effect output stage here |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | updateDeviceForStrategy(); |
| 1054 | #ifdef AUDIO_POLICY_TEST |
Eric Laurent | 0163594 | 2011-01-18 18:39:02 -0800 | [diff] [blame^] | 1055 | if (mHardwareOutput != 0) { |
| 1056 | AudioParameter outputCmd = AudioParameter(); |
| 1057 | outputCmd.addInt(String8("set_id"), 0); |
| 1058 | mpClientInterface->setParameters(mHardwareOutput, outputCmd.toString()); |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1059 | |
Eric Laurent | 0163594 | 2011-01-18 18:39:02 -0800 | [diff] [blame^] | 1060 | mTestDevice = AudioSystem::DEVICE_OUT_SPEAKER; |
| 1061 | mTestSamplingRate = 44100; |
| 1062 | mTestFormat = AudioSystem::PCM_16_BIT; |
| 1063 | mTestChannels = AudioSystem::CHANNEL_OUT_STEREO; |
| 1064 | mTestLatencyMs = 0; |
| 1065 | mCurOutput = 0; |
| 1066 | mDirectOutput = false; |
| 1067 | for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { |
| 1068 | mTestOutputs[i] = 0; |
| 1069 | } |
| 1070 | |
| 1071 | const size_t SIZE = 256; |
| 1072 | char buffer[SIZE]; |
| 1073 | snprintf(buffer, SIZE, "AudioPolicyManagerTest"); |
| 1074 | run(buffer, ANDROID_PRIORITY_AUDIO); |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1075 | } |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1076 | #endif //AUDIO_POLICY_TEST |
| 1077 | } |
| 1078 | |
| 1079 | AudioPolicyManagerBase::~AudioPolicyManagerBase() |
| 1080 | { |
| 1081 | #ifdef AUDIO_POLICY_TEST |
| 1082 | exit(); |
| 1083 | #endif //AUDIO_POLICY_TEST |
| 1084 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 1085 | mpClientInterface->closeOutput(mOutputs.keyAt(i)); |
| 1086 | delete mOutputs.valueAt(i); |
| 1087 | } |
| 1088 | mOutputs.clear(); |
| 1089 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 1090 | mpClientInterface->closeInput(mInputs.keyAt(i)); |
| 1091 | delete mInputs.valueAt(i); |
| 1092 | } |
| 1093 | mInputs.clear(); |
| 1094 | } |
| 1095 | |
Eric Laurent | 0163594 | 2011-01-18 18:39:02 -0800 | [diff] [blame^] | 1096 | status_t AudioPolicyManagerBase::initCheck() |
| 1097 | { |
| 1098 | return (mHardwareOutput == 0) ? NO_INIT : NO_ERROR; |
| 1099 | } |
| 1100 | |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1101 | #ifdef AUDIO_POLICY_TEST |
| 1102 | bool AudioPolicyManagerBase::threadLoop() |
| 1103 | { |
| 1104 | LOGV("entering threadLoop()"); |
| 1105 | while (!exitPending()) |
| 1106 | { |
| 1107 | String8 command; |
| 1108 | int valueInt; |
| 1109 | String8 value; |
| 1110 | |
| 1111 | Mutex::Autolock _l(mLock); |
| 1112 | mWaitWorkCV.waitRelative(mLock, milliseconds(50)); |
| 1113 | |
| 1114 | command = mpClientInterface->getParameters(0, String8("test_cmd_policy")); |
| 1115 | AudioParameter param = AudioParameter(command); |
| 1116 | |
| 1117 | if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR && |
| 1118 | valueInt != 0) { |
| 1119 | LOGV("Test command %s received", command.string()); |
| 1120 | String8 target; |
| 1121 | if (param.get(String8("target"), target) != NO_ERROR) { |
| 1122 | target = "Manager"; |
| 1123 | } |
| 1124 | if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) { |
| 1125 | param.remove(String8("test_cmd_policy_output")); |
| 1126 | mCurOutput = valueInt; |
| 1127 | } |
| 1128 | if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) { |
| 1129 | param.remove(String8("test_cmd_policy_direct")); |
| 1130 | if (value == "false") { |
| 1131 | mDirectOutput = false; |
| 1132 | } else if (value == "true") { |
| 1133 | mDirectOutput = true; |
| 1134 | } |
| 1135 | } |
| 1136 | if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) { |
| 1137 | param.remove(String8("test_cmd_policy_input")); |
| 1138 | mTestInput = valueInt; |
| 1139 | } |
| 1140 | |
| 1141 | if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) { |
| 1142 | param.remove(String8("test_cmd_policy_format")); |
| 1143 | int format = AudioSystem::INVALID_FORMAT; |
| 1144 | if (value == "PCM 16 bits") { |
| 1145 | format = AudioSystem::PCM_16_BIT; |
| 1146 | } else if (value == "PCM 8 bits") { |
| 1147 | format = AudioSystem::PCM_8_BIT; |
| 1148 | } else if (value == "Compressed MP3") { |
| 1149 | format = AudioSystem::MP3; |
| 1150 | } |
| 1151 | if (format != AudioSystem::INVALID_FORMAT) { |
| 1152 | if (target == "Manager") { |
| 1153 | mTestFormat = format; |
| 1154 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 1155 | AudioParameter outputParam = AudioParameter(); |
| 1156 | outputParam.addInt(String8("format"), format); |
| 1157 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 1158 | } |
| 1159 | } |
| 1160 | } |
| 1161 | if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) { |
| 1162 | param.remove(String8("test_cmd_policy_channels")); |
| 1163 | int channels = 0; |
| 1164 | |
| 1165 | if (value == "Channels Stereo") { |
| 1166 | channels = AudioSystem::CHANNEL_OUT_STEREO; |
| 1167 | } else if (value == "Channels Mono") { |
| 1168 | channels = AudioSystem::CHANNEL_OUT_MONO; |
| 1169 | } |
| 1170 | if (channels != 0) { |
| 1171 | if (target == "Manager") { |
| 1172 | mTestChannels = channels; |
| 1173 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 1174 | AudioParameter outputParam = AudioParameter(); |
| 1175 | outputParam.addInt(String8("channels"), channels); |
| 1176 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 1177 | } |
| 1178 | } |
| 1179 | } |
| 1180 | if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) { |
| 1181 | param.remove(String8("test_cmd_policy_sampleRate")); |
| 1182 | if (valueInt >= 0 && valueInt <= 96000) { |
| 1183 | int samplingRate = valueInt; |
| 1184 | if (target == "Manager") { |
| 1185 | mTestSamplingRate = samplingRate; |
| 1186 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 1187 | AudioParameter outputParam = AudioParameter(); |
| 1188 | outputParam.addInt(String8("sampling_rate"), samplingRate); |
| 1189 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 1190 | } |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) { |
| 1195 | param.remove(String8("test_cmd_policy_reopen")); |
| 1196 | |
| 1197 | mpClientInterface->closeOutput(mHardwareOutput); |
| 1198 | delete mOutputs.valueFor(mHardwareOutput); |
| 1199 | mOutputs.removeItem(mHardwareOutput); |
| 1200 | |
| 1201 | AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(); |
| 1202 | outputDesc->mDevice = (uint32_t)AudioSystem::DEVICE_OUT_SPEAKER; |
| 1203 | mHardwareOutput = mpClientInterface->openOutput(&outputDesc->mDevice, |
| 1204 | &outputDesc->mSamplingRate, |
| 1205 | &outputDesc->mFormat, |
| 1206 | &outputDesc->mChannels, |
| 1207 | &outputDesc->mLatency, |
| 1208 | outputDesc->mFlags); |
| 1209 | if (mHardwareOutput == 0) { |
| 1210 | LOGE("Failed to reopen hardware output stream, samplingRate: %d, format %d, channels %d", |
| 1211 | outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannels); |
| 1212 | } else { |
| 1213 | AudioParameter outputCmd = AudioParameter(); |
| 1214 | outputCmd.addInt(String8("set_id"), 0); |
| 1215 | mpClientInterface->setParameters(mHardwareOutput, outputCmd.toString()); |
| 1216 | addOutput(mHardwareOutput, outputDesc); |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | |
| 1221 | mpClientInterface->setParameters(0, String8("test_cmd_policy=")); |
| 1222 | } |
| 1223 | } |
| 1224 | return false; |
| 1225 | } |
| 1226 | |
| 1227 | void AudioPolicyManagerBase::exit() |
| 1228 | { |
| 1229 | { |
| 1230 | AutoMutex _l(mLock); |
| 1231 | requestExit(); |
| 1232 | mWaitWorkCV.signal(); |
| 1233 | } |
| 1234 | requestExitAndWait(); |
| 1235 | } |
| 1236 | |
| 1237 | int AudioPolicyManagerBase::testOutputIndex(audio_io_handle_t output) |
| 1238 | { |
| 1239 | for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { |
| 1240 | if (output == mTestOutputs[i]) return i; |
| 1241 | } |
| 1242 | return 0; |
| 1243 | } |
| 1244 | #endif //AUDIO_POLICY_TEST |
| 1245 | |
| 1246 | // --- |
| 1247 | |
| 1248 | void AudioPolicyManagerBase::addOutput(audio_io_handle_t id, AudioOutputDescriptor *outputDesc) |
| 1249 | { |
| 1250 | outputDesc->mId = id; |
| 1251 | mOutputs.add(id, outputDesc); |
| 1252 | } |
| 1253 | |
| 1254 | |
| 1255 | #ifdef WITH_A2DP |
| 1256 | status_t AudioPolicyManagerBase::handleA2dpConnection(AudioSystem::audio_devices device, |
| 1257 | const char *device_address) |
| 1258 | { |
| 1259 | // when an A2DP device is connected, open an A2DP and a duplicated output |
| 1260 | LOGV("opening A2DP output for device %s", device_address); |
| 1261 | AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(); |
| 1262 | outputDesc->mDevice = device; |
| 1263 | mA2dpOutput = mpClientInterface->openOutput(&outputDesc->mDevice, |
| 1264 | &outputDesc->mSamplingRate, |
| 1265 | &outputDesc->mFormat, |
| 1266 | &outputDesc->mChannels, |
| 1267 | &outputDesc->mLatency, |
| 1268 | outputDesc->mFlags); |
| 1269 | if (mA2dpOutput) { |
| 1270 | // add A2DP output descriptor |
| 1271 | addOutput(mA2dpOutput, outputDesc); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1272 | |
| 1273 | //TODO: configure audio effect output stage here |
| 1274 | |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1275 | // set initial stream volume for A2DP device |
| 1276 | applyStreamVolumes(mA2dpOutput, device); |
| 1277 | if (a2dpUsedForSonification()) { |
| 1278 | mDuplicatedOutput = mpClientInterface->openDuplicateOutput(mA2dpOutput, mHardwareOutput); |
| 1279 | } |
| 1280 | if (mDuplicatedOutput != 0 || |
| 1281 | !a2dpUsedForSonification()) { |
| 1282 | // If both A2DP and duplicated outputs are open, send device address to A2DP hardware |
| 1283 | // interface |
| 1284 | AudioParameter param; |
| 1285 | param.add(String8("a2dp_sink_address"), String8(device_address)); |
| 1286 | mpClientInterface->setParameters(mA2dpOutput, param.toString()); |
| 1287 | mA2dpDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN); |
| 1288 | |
| 1289 | if (a2dpUsedForSonification()) { |
| 1290 | // add duplicated output descriptor |
| 1291 | AudioOutputDescriptor *dupOutputDesc = new AudioOutputDescriptor(); |
| 1292 | dupOutputDesc->mOutput1 = mOutputs.valueFor(mHardwareOutput); |
| 1293 | dupOutputDesc->mOutput2 = mOutputs.valueFor(mA2dpOutput); |
| 1294 | dupOutputDesc->mSamplingRate = outputDesc->mSamplingRate; |
| 1295 | dupOutputDesc->mFormat = outputDesc->mFormat; |
| 1296 | dupOutputDesc->mChannels = outputDesc->mChannels; |
| 1297 | dupOutputDesc->mLatency = outputDesc->mLatency; |
| 1298 | addOutput(mDuplicatedOutput, dupOutputDesc); |
| 1299 | applyStreamVolumes(mDuplicatedOutput, device); |
| 1300 | } |
| 1301 | } else { |
| 1302 | LOGW("getOutput() could not open duplicated output for %d and %d", |
| 1303 | mHardwareOutput, mA2dpOutput); |
| 1304 | mpClientInterface->closeOutput(mA2dpOutput); |
| 1305 | mOutputs.removeItem(mA2dpOutput); |
| 1306 | mA2dpOutput = 0; |
| 1307 | delete outputDesc; |
| 1308 | return NO_INIT; |
| 1309 | } |
| 1310 | } else { |
| 1311 | LOGW("setDeviceConnectionState() could not open A2DP output for device %x", device); |
| 1312 | delete outputDesc; |
| 1313 | return NO_INIT; |
| 1314 | } |
| 1315 | AudioOutputDescriptor *hwOutputDesc = mOutputs.valueFor(mHardwareOutput); |
| 1316 | |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1317 | if (!a2dpUsedForSonification()) { |
| 1318 | // mute music on A2DP output if a notification or ringtone is playing |
| 1319 | uint32_t refCount = hwOutputDesc->strategyRefCount(STRATEGY_SONIFICATION); |
| 1320 | for (uint32_t i = 0; i < refCount; i++) { |
| 1321 | setStrategyMute(STRATEGY_MEDIA, true, mA2dpOutput); |
| 1322 | } |
| 1323 | } |
Eric Laurent | b87b53d | 2010-11-02 12:02:20 -0700 | [diff] [blame] | 1324 | |
| 1325 | mA2dpSuspended = false; |
| 1326 | |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1327 | return NO_ERROR; |
| 1328 | } |
| 1329 | |
| 1330 | status_t AudioPolicyManagerBase::handleA2dpDisconnection(AudioSystem::audio_devices device, |
| 1331 | const char *device_address) |
| 1332 | { |
| 1333 | if (mA2dpOutput == 0) { |
| 1334 | LOGW("setDeviceConnectionState() disconnecting A2DP and no A2DP output!"); |
| 1335 | return INVALID_OPERATION; |
| 1336 | } |
| 1337 | |
| 1338 | if (mA2dpDeviceAddress != device_address) { |
| 1339 | LOGW("setDeviceConnectionState() disconnecting unknow A2DP sink address %s", device_address); |
| 1340 | return INVALID_OPERATION; |
| 1341 | } |
| 1342 | |
Eric Laurent | 22e1ca3 | 2010-02-23 09:48:31 -0800 | [diff] [blame] | 1343 | // mute media strategy to avoid outputting sound on hardware output while music stream |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1344 | // is switched from A2DP output and before music is paused by music application |
| 1345 | setStrategyMute(STRATEGY_MEDIA, true, mHardwareOutput); |
Eric Laurent | 22e1ca3 | 2010-02-23 09:48:31 -0800 | [diff] [blame] | 1346 | setStrategyMute(STRATEGY_MEDIA, false, mHardwareOutput, MUTE_TIME_MS); |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1347 | |
| 1348 | if (!a2dpUsedForSonification()) { |
| 1349 | // unmute music on A2DP output if a notification or ringtone is playing |
| 1350 | uint32_t refCount = mOutputs.valueFor(mHardwareOutput)->strategyRefCount(STRATEGY_SONIFICATION); |
| 1351 | for (uint32_t i = 0; i < refCount; i++) { |
| 1352 | setStrategyMute(STRATEGY_MEDIA, false, mA2dpOutput); |
| 1353 | } |
| 1354 | } |
| 1355 | mA2dpDeviceAddress = ""; |
Eric Laurent | b87b53d | 2010-11-02 12:02:20 -0700 | [diff] [blame] | 1356 | mA2dpSuspended = false; |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1357 | return NO_ERROR; |
| 1358 | } |
| 1359 | |
| 1360 | void AudioPolicyManagerBase::closeA2dpOutputs() |
| 1361 | { |
Praveen Bharathi | 21e941b | 2010-10-06 15:23:14 -0500 | [diff] [blame] | 1362 | |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1363 | LOGV("setDeviceConnectionState() closing A2DP and duplicated output!"); |
| 1364 | |
| 1365 | if (mDuplicatedOutput != 0) { |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1366 | AudioOutputDescriptor *dupOutputDesc = mOutputs.valueFor(mDuplicatedOutput); |
| 1367 | AudioOutputDescriptor *hwOutputDesc = mOutputs.valueFor(mHardwareOutput); |
| 1368 | // As all active tracks on duplicated output will be deleted, |
| 1369 | // and as they were also referenced on hardware output, the reference |
| 1370 | // count for their stream type must be adjusted accordingly on |
| 1371 | // hardware output. |
| 1372 | for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) { |
| 1373 | int refCount = dupOutputDesc->mRefCount[i]; |
| 1374 | hwOutputDesc->changeRefCount((AudioSystem::stream_type)i,-refCount); |
| 1375 | } |
| 1376 | |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1377 | mpClientInterface->closeOutput(mDuplicatedOutput); |
| 1378 | delete mOutputs.valueFor(mDuplicatedOutput); |
| 1379 | mOutputs.removeItem(mDuplicatedOutput); |
| 1380 | mDuplicatedOutput = 0; |
| 1381 | } |
| 1382 | if (mA2dpOutput != 0) { |
| 1383 | AudioParameter param; |
| 1384 | param.add(String8("closing"), String8("true")); |
| 1385 | mpClientInterface->setParameters(mA2dpOutput, param.toString()); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1386 | |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1387 | mpClientInterface->closeOutput(mA2dpOutput); |
| 1388 | delete mOutputs.valueFor(mA2dpOutput); |
| 1389 | mOutputs.removeItem(mA2dpOutput); |
| 1390 | mA2dpOutput = 0; |
| 1391 | } |
| 1392 | } |
| 1393 | |
Eric Laurent | b8453f4 | 2010-08-27 17:10:36 -0700 | [diff] [blame] | 1394 | void AudioPolicyManagerBase::checkOutputForStrategy(routing_strategy strategy) |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1395 | { |
| 1396 | uint32_t prevDevice = getDeviceForStrategy(strategy); |
| 1397 | uint32_t curDevice = getDeviceForStrategy(strategy, false); |
| 1398 | bool a2dpWasUsed = AudioSystem::isA2dpDevice((AudioSystem::audio_devices)(prevDevice & ~AudioSystem::DEVICE_OUT_SPEAKER)); |
| 1399 | bool a2dpIsUsed = AudioSystem::isA2dpDevice((AudioSystem::audio_devices)(curDevice & ~AudioSystem::DEVICE_OUT_SPEAKER)); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1400 | audio_io_handle_t srcOutput = 0; |
| 1401 | audio_io_handle_t dstOutput = 0; |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1402 | |
| 1403 | if (a2dpWasUsed && !a2dpIsUsed) { |
| 1404 | bool dupUsed = a2dpUsedForSonification() && a2dpWasUsed && (AudioSystem::popCount(prevDevice) == 2); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1405 | dstOutput = mHardwareOutput; |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1406 | if (dupUsed) { |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1407 | LOGV("checkOutputForStrategy() moving strategy %d from duplicated", strategy); |
| 1408 | srcOutput = mDuplicatedOutput; |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1409 | } else { |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1410 | LOGV("checkOutputForStrategy() moving strategy %d from a2dp", strategy); |
| 1411 | srcOutput = mA2dpOutput; |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1412 | } |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1413 | } |
| 1414 | if (a2dpIsUsed && !a2dpWasUsed) { |
| 1415 | bool dupUsed = a2dpUsedForSonification() && a2dpIsUsed && (AudioSystem::popCount(curDevice) == 2); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1416 | srcOutput = mHardwareOutput; |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1417 | if (dupUsed) { |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1418 | LOGV("checkOutputForStrategy() moving strategy %d to duplicated", strategy); |
| 1419 | dstOutput = mDuplicatedOutput; |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1420 | } else { |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1421 | LOGV("checkOutputForStrategy() moving strategy %d to a2dp", strategy); |
| 1422 | dstOutput = mA2dpOutput; |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1423 | } |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1424 | } |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1425 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1426 | if (srcOutput != 0 && dstOutput != 0) { |
| 1427 | // Move effects associated to this strategy from previous output to new output |
| 1428 | for (size_t i = 0; i < mEffects.size(); i++) { |
| 1429 | EffectDescriptor *desc = mEffects.valueAt(i); |
| 1430 | if (desc->mSession != AudioSystem::SESSION_OUTPUT_STAGE && |
| 1431 | desc->mStrategy == strategy && |
| 1432 | desc->mOutput == srcOutput) { |
| 1433 | LOGV("checkOutputForStrategy() moving effect %d to output %d", mEffects.keyAt(i), dstOutput); |
| 1434 | mpClientInterface->moveEffects(desc->mSession, srcOutput, dstOutput); |
| 1435 | desc->mOutput = dstOutput; |
| 1436 | } |
| 1437 | } |
| 1438 | // Move tracks associated to this strategy from previous output to new output |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1439 | for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) { |
| 1440 | if (getStrategy((AudioSystem::stream_type)i) == strategy) { |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1441 | mpClientInterface->setStreamOutput((AudioSystem::stream_type)i, dstOutput); |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1442 | } |
| 1443 | } |
| 1444 | } |
| 1445 | } |
| 1446 | |
Eric Laurent | b8453f4 | 2010-08-27 17:10:36 -0700 | [diff] [blame] | 1447 | void AudioPolicyManagerBase::checkOutputForAllStrategies() |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1448 | { |
Eric Laurent | b8453f4 | 2010-08-27 17:10:36 -0700 | [diff] [blame] | 1449 | checkOutputForStrategy(STRATEGY_PHONE); |
| 1450 | checkOutputForStrategy(STRATEGY_SONIFICATION); |
| 1451 | checkOutputForStrategy(STRATEGY_MEDIA); |
| 1452 | checkOutputForStrategy(STRATEGY_DTMF); |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1453 | } |
| 1454 | |
Eric Laurent | b87b53d | 2010-11-02 12:02:20 -0700 | [diff] [blame] | 1455 | void AudioPolicyManagerBase::checkA2dpSuspend() |
| 1456 | { |
| 1457 | // suspend A2DP output if: |
| 1458 | // (NOT already suspended) && |
| 1459 | // ((SCO device is connected && |
| 1460 | // (forced usage for communication || for record is SCO))) || |
| 1461 | // (phone state is ringing || in call) |
| 1462 | // |
| 1463 | // restore A2DP output if: |
| 1464 | // (Already suspended) && |
| 1465 | // ((SCO device is NOT connected || |
| 1466 | // (forced usage NOT for communication && NOT for record is SCO))) && |
| 1467 | // (phone state is NOT ringing && NOT in call) |
| 1468 | // |
| 1469 | if (mA2dpOutput == 0) { |
| 1470 | return; |
| 1471 | } |
| 1472 | |
| 1473 | if (mA2dpSuspended) { |
| 1474 | if (((mScoDeviceAddress == "") || |
| 1475 | ((mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO) && |
| 1476 | (mForceUse[AudioSystem::FOR_RECORD] != AudioSystem::FORCE_BT_SCO))) && |
| 1477 | ((mPhoneState != AudioSystem::MODE_IN_CALL) && |
| 1478 | (mPhoneState != AudioSystem::MODE_RINGTONE))) { |
| 1479 | |
| 1480 | mpClientInterface->restoreOutput(mA2dpOutput); |
| 1481 | mA2dpSuspended = false; |
| 1482 | } |
| 1483 | } else { |
| 1484 | if (((mScoDeviceAddress != "") && |
| 1485 | ((mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) || |
| 1486 | (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO))) || |
| 1487 | ((mPhoneState == AudioSystem::MODE_IN_CALL) || |
| 1488 | (mPhoneState == AudioSystem::MODE_RINGTONE))) { |
| 1489 | |
| 1490 | mpClientInterface->suspendOutput(mA2dpOutput); |
| 1491 | mA2dpSuspended = true; |
| 1492 | } |
| 1493 | } |
| 1494 | } |
| 1495 | |
| 1496 | |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1497 | #endif |
| 1498 | |
| 1499 | uint32_t AudioPolicyManagerBase::getNewDevice(audio_io_handle_t output, bool fromCache) |
| 1500 | { |
| 1501 | uint32_t device = 0; |
| 1502 | |
| 1503 | AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output); |
| 1504 | // check the following by order of priority to request a routing change if necessary: |
| 1505 | // 1: we are in call or the strategy phone is active on the hardware output: |
| 1506 | // use device for strategy phone |
| 1507 | // 2: the strategy sonification is active on the hardware output: |
| 1508 | // use device for strategy sonification |
| 1509 | // 3: the strategy media is active on the hardware output: |
| 1510 | // use device for strategy media |
| 1511 | // 4: the strategy DTMF is active on the hardware output: |
| 1512 | // use device for strategy DTMF |
Jean-Michel Trivi | 8f677d6 | 2010-11-15 12:11:32 -0800 | [diff] [blame] | 1513 | if (isInCall() || |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1514 | outputDesc->isUsedByStrategy(STRATEGY_PHONE)) { |
| 1515 | device = getDeviceForStrategy(STRATEGY_PHONE, fromCache); |
| 1516 | } else if (outputDesc->isUsedByStrategy(STRATEGY_SONIFICATION)) { |
| 1517 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache); |
| 1518 | } else if (outputDesc->isUsedByStrategy(STRATEGY_MEDIA)) { |
| 1519 | device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache); |
| 1520 | } else if (outputDesc->isUsedByStrategy(STRATEGY_DTMF)) { |
| 1521 | device = getDeviceForStrategy(STRATEGY_DTMF, fromCache); |
| 1522 | } |
| 1523 | |
| 1524 | LOGV("getNewDevice() selected device %x", device); |
| 1525 | return device; |
| 1526 | } |
| 1527 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1528 | uint32_t AudioPolicyManagerBase::getStrategyForStream(AudioSystem::stream_type stream) { |
| 1529 | return (uint32_t)getStrategy(stream); |
| 1530 | } |
| 1531 | |
| 1532 | AudioPolicyManagerBase::routing_strategy AudioPolicyManagerBase::getStrategy( |
| 1533 | AudioSystem::stream_type stream) { |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1534 | // stream to strategy mapping |
| 1535 | switch (stream) { |
| 1536 | case AudioSystem::VOICE_CALL: |
| 1537 | case AudioSystem::BLUETOOTH_SCO: |
| 1538 | return STRATEGY_PHONE; |
| 1539 | case AudioSystem::RING: |
| 1540 | case AudioSystem::NOTIFICATION: |
| 1541 | case AudioSystem::ALARM: |
| 1542 | case AudioSystem::ENFORCED_AUDIBLE: |
| 1543 | return STRATEGY_SONIFICATION; |
| 1544 | case AudioSystem::DTMF: |
| 1545 | return STRATEGY_DTMF; |
| 1546 | default: |
| 1547 | LOGE("unknown stream type"); |
| 1548 | case AudioSystem::SYSTEM: |
| 1549 | // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs |
| 1550 | // while key clicks are played produces a poor result |
| 1551 | case AudioSystem::TTS: |
| 1552 | case AudioSystem::MUSIC: |
| 1553 | return STRATEGY_MEDIA; |
| 1554 | } |
| 1555 | } |
| 1556 | |
| 1557 | uint32_t AudioPolicyManagerBase::getDeviceForStrategy(routing_strategy strategy, bool fromCache) |
| 1558 | { |
| 1559 | uint32_t device = 0; |
| 1560 | |
| 1561 | if (fromCache) { |
| 1562 | LOGV("getDeviceForStrategy() from cache strategy %d, device %x", strategy, mDeviceForStrategy[strategy]); |
| 1563 | return mDeviceForStrategy[strategy]; |
| 1564 | } |
| 1565 | |
| 1566 | switch (strategy) { |
| 1567 | case STRATEGY_DTMF: |
Jean-Michel Trivi | 8f677d6 | 2010-11-15 12:11:32 -0800 | [diff] [blame] | 1568 | if (!isInCall()) { |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1569 | // when off call, DTMF strategy follows the same rules as MEDIA strategy |
| 1570 | device = getDeviceForStrategy(STRATEGY_MEDIA, false); |
| 1571 | break; |
| 1572 | } |
| 1573 | // when in call, DTMF and PHONE strategies follow the same rules |
| 1574 | // FALL THROUGH |
| 1575 | |
| 1576 | case STRATEGY_PHONE: |
| 1577 | // for phone strategy, we first consider the forced use and then the available devices by order |
| 1578 | // of priority |
| 1579 | switch (mForceUse[AudioSystem::FOR_COMMUNICATION]) { |
| 1580 | case AudioSystem::FORCE_BT_SCO: |
Jean-Michel Trivi | 8f677d6 | 2010-11-15 12:11:32 -0800 | [diff] [blame] | 1581 | if (!isInCall() || strategy != STRATEGY_DTMF) { |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1582 | device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_CARKIT; |
| 1583 | if (device) break; |
| 1584 | } |
| 1585 | device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_HEADSET; |
| 1586 | if (device) break; |
| 1587 | device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_SCO; |
| 1588 | if (device) break; |
| 1589 | // if SCO device is requested but no SCO device is available, fall back to default case |
| 1590 | // FALL THROUGH |
| 1591 | |
| 1592 | default: // FORCE_NONE |
| 1593 | device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADPHONE; |
| 1594 | if (device) break; |
| 1595 | device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADSET; |
| 1596 | if (device) break; |
Eric Laurent | 58bf1d9 | 2011-01-09 15:33:01 -0800 | [diff] [blame] | 1597 | device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_AUX_DIGITAL; |
| 1598 | if (device) break; |
| 1599 | device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET; |
| 1600 | if (device) break; |
Praveen Bharathi | 21e941b | 2010-10-06 15:23:14 -0500 | [diff] [blame] | 1601 | device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET; |
| 1602 | if (device) break; |
Jean-Michel Trivi | e4b8c42 | 2010-03-13 12:33:15 -0800 | [diff] [blame] | 1603 | #ifdef WITH_A2DP |
| 1604 | // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP |
Jean-Michel Trivi | 8f677d6 | 2010-11-15 12:11:32 -0800 | [diff] [blame] | 1605 | if (!isInCall()) { |
Jean-Michel Trivi | e4b8c42 | 2010-03-13 12:33:15 -0800 | [diff] [blame] | 1606 | device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP; |
| 1607 | if (device) break; |
| 1608 | device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES; |
| 1609 | if (device) break; |
| 1610 | } |
| 1611 | #endif |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1612 | device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_EARPIECE; |
| 1613 | if (device == 0) { |
| 1614 | LOGE("getDeviceForStrategy() earpiece device not found"); |
| 1615 | } |
| 1616 | break; |
| 1617 | |
| 1618 | case AudioSystem::FORCE_SPEAKER: |
Eric Laurent | 58bf1d9 | 2011-01-09 15:33:01 -0800 | [diff] [blame] | 1619 | device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_AUX_DIGITAL; |
| 1620 | if (device) break; |
| 1621 | device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET; |
| 1622 | if (device) break; |
Eric Laurent | aef0cbb | 2010-12-16 09:44:42 -0800 | [diff] [blame] | 1623 | device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET; |
| 1624 | if (device) break; |
Jean-Michel Trivi | e4b8c42 | 2010-03-13 12:33:15 -0800 | [diff] [blame] | 1625 | #ifdef WITH_A2DP |
| 1626 | // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to |
| 1627 | // A2DP speaker when forcing to speaker output |
Jean-Michel Trivi | 8f677d6 | 2010-11-15 12:11:32 -0800 | [diff] [blame] | 1628 | if (!isInCall()) { |
Jean-Michel Trivi | e4b8c42 | 2010-03-13 12:33:15 -0800 | [diff] [blame] | 1629 | device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER; |
| 1630 | if (device) break; |
| 1631 | } |
| 1632 | #endif |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1633 | device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_SPEAKER; |
| 1634 | if (device == 0) { |
| 1635 | LOGE("getDeviceForStrategy() speaker device not found"); |
| 1636 | } |
| 1637 | break; |
| 1638 | } |
| 1639 | break; |
| 1640 | |
| 1641 | case STRATEGY_SONIFICATION: |
| 1642 | |
| 1643 | // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by |
| 1644 | // handleIncallSonification(). |
Jean-Michel Trivi | 8f677d6 | 2010-11-15 12:11:32 -0800 | [diff] [blame] | 1645 | if (isInCall()) { |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1646 | device = getDeviceForStrategy(STRATEGY_PHONE, false); |
| 1647 | break; |
| 1648 | } |
| 1649 | device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_SPEAKER; |
| 1650 | if (device == 0) { |
| 1651 | LOGE("getDeviceForStrategy() speaker device not found"); |
| 1652 | } |
| 1653 | // The second device used for sonification is the same as the device used by media strategy |
| 1654 | // FALL THROUGH |
| 1655 | |
| 1656 | case STRATEGY_MEDIA: { |
Eric Laurent | 2c61bee | 2010-12-14 16:31:33 -0800 | [diff] [blame] | 1657 | uint32_t device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADPHONE; |
Eric Laurent | fe2e075 | 2010-03-06 15:46:31 -0800 | [diff] [blame] | 1658 | if (device2 == 0) { |
| 1659 | device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADSET; |
| 1660 | } |
Praveen Bharathi | 21e941b | 2010-10-06 15:23:14 -0500 | [diff] [blame] | 1661 | if (device2 == 0) { |
Eric Laurent | 2c61bee | 2010-12-14 16:31:33 -0800 | [diff] [blame] | 1662 | device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_AUX_DIGITAL; |
Praveen Bharathi | 21e941b | 2010-10-06 15:23:14 -0500 | [diff] [blame] | 1663 | } |
| 1664 | if (device2 == 0) { |
| 1665 | device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET; |
| 1666 | } |
Eric Laurent | 2c61bee | 2010-12-14 16:31:33 -0800 | [diff] [blame] | 1667 | if (device2 == 0) { |
| 1668 | device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET; |
| 1669 | } |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1670 | #ifdef WITH_A2DP |
| 1671 | if (mA2dpOutput != 0) { |
| 1672 | if (strategy == STRATEGY_SONIFICATION && !a2dpUsedForSonification()) { |
| 1673 | break; |
| 1674 | } |
| 1675 | if (device2 == 0) { |
| 1676 | device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP; |
| 1677 | } |
| 1678 | if (device2 == 0) { |
| 1679 | device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES; |
| 1680 | } |
| 1681 | if (device2 == 0) { |
| 1682 | device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER; |
| 1683 | } |
| 1684 | } |
| 1685 | #endif |
| 1686 | if (device2 == 0) { |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1687 | device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_SPEAKER; |
| 1688 | } |
| 1689 | |
| 1690 | // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION, 0 otherwise |
| 1691 | device |= device2; |
| 1692 | if (device == 0) { |
| 1693 | LOGE("getDeviceForStrategy() speaker device not found"); |
| 1694 | } |
| 1695 | } break; |
| 1696 | |
| 1697 | default: |
| 1698 | LOGW("getDeviceForStrategy() unknown strategy: %d", strategy); |
| 1699 | break; |
| 1700 | } |
| 1701 | |
| 1702 | LOGV("getDeviceForStrategy() strategy %d, device %x", strategy, device); |
| 1703 | return device; |
| 1704 | } |
| 1705 | |
| 1706 | void AudioPolicyManagerBase::updateDeviceForStrategy() |
| 1707 | { |
| 1708 | for (int i = 0; i < NUM_STRATEGIES; i++) { |
| 1709 | mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false); |
| 1710 | } |
| 1711 | } |
| 1712 | |
| 1713 | void AudioPolicyManagerBase::setOutputDevice(audio_io_handle_t output, uint32_t device, bool force, int delayMs) |
| 1714 | { |
| 1715 | LOGV("setOutputDevice() output %d device %x delayMs %d", output, device, delayMs); |
| 1716 | AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output); |
| 1717 | |
| 1718 | |
| 1719 | if (outputDesc->isDuplicated()) { |
| 1720 | setOutputDevice(outputDesc->mOutput1->mId, device, force, delayMs); |
| 1721 | setOutputDevice(outputDesc->mOutput2->mId, device, force, delayMs); |
| 1722 | return; |
| 1723 | } |
| 1724 | #ifdef WITH_A2DP |
| 1725 | // filter devices according to output selected |
Eric Laurent | ef9500f | 2010-03-11 14:47:00 -0800 | [diff] [blame] | 1726 | if (output == mA2dpOutput) { |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1727 | device &= AudioSystem::DEVICE_OUT_ALL_A2DP; |
Eric Laurent | ef9500f | 2010-03-11 14:47:00 -0800 | [diff] [blame] | 1728 | } else { |
| 1729 | device &= ~AudioSystem::DEVICE_OUT_ALL_A2DP; |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1730 | } |
| 1731 | #endif |
| 1732 | |
| 1733 | uint32_t prevDevice = (uint32_t)outputDesc->device(); |
| 1734 | // Do not change the routing if: |
| 1735 | // - the requestede device is 0 |
| 1736 | // - the requested device is the same as current device and force is not specified. |
| 1737 | // Doing this check here allows the caller to call setOutputDevice() without conditions |
Eric Laurent | ef9500f | 2010-03-11 14:47:00 -0800 | [diff] [blame] | 1738 | if ((device == 0 || device == prevDevice) && !force) { |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1739 | LOGV("setOutputDevice() setting same device %x or null device for output %d", device, output); |
| 1740 | return; |
| 1741 | } |
| 1742 | |
| 1743 | outputDesc->mDevice = device; |
| 1744 | // mute media streams if both speaker and headset are selected |
| 1745 | if (output == mHardwareOutput && AudioSystem::popCount(device) == 2) { |
| 1746 | setStrategyMute(STRATEGY_MEDIA, true, output); |
| 1747 | // wait for the PCM output buffers to empty before proceeding with the rest of the command |
| 1748 | usleep(outputDesc->mLatency*2*1000); |
| 1749 | } |
Eric Laurent | b87b53d | 2010-11-02 12:02:20 -0700 | [diff] [blame] | 1750 | |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1751 | // do the routing |
| 1752 | AudioParameter param = AudioParameter(); |
| 1753 | param.addInt(String8(AudioParameter::keyRouting), (int)device); |
| 1754 | mpClientInterface->setParameters(mHardwareOutput, param.toString(), delayMs); |
| 1755 | // update stream volumes according to new device |
| 1756 | applyStreamVolumes(output, device, delayMs); |
| 1757 | |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1758 | // if changing from a combined headset + speaker route, unmute media streams |
| 1759 | if (output == mHardwareOutput && AudioSystem::popCount(prevDevice) == 2) { |
| 1760 | setStrategyMute(STRATEGY_MEDIA, false, output, delayMs); |
| 1761 | } |
| 1762 | } |
| 1763 | |
| 1764 | uint32_t AudioPolicyManagerBase::getDeviceForInputSource(int inputSource) |
| 1765 | { |
| 1766 | uint32_t device; |
| 1767 | |
| 1768 | switch(inputSource) { |
| 1769 | case AUDIO_SOURCE_DEFAULT: |
| 1770 | case AUDIO_SOURCE_MIC: |
| 1771 | case AUDIO_SOURCE_VOICE_RECOGNITION: |
Jean-Michel Trivi | 820b9e0 | 2010-11-08 18:38:14 -0800 | [diff] [blame] | 1772 | case AUDIO_SOURCE_VOICE_COMMUNICATION: |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1773 | if (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO && |
| 1774 | mAvailableInputDevices & AudioSystem::DEVICE_IN_BLUETOOTH_SCO_HEADSET) { |
| 1775 | device = AudioSystem::DEVICE_IN_BLUETOOTH_SCO_HEADSET; |
| 1776 | } else if (mAvailableInputDevices & AudioSystem::DEVICE_IN_WIRED_HEADSET) { |
| 1777 | device = AudioSystem::DEVICE_IN_WIRED_HEADSET; |
| 1778 | } else { |
| 1779 | device = AudioSystem::DEVICE_IN_BUILTIN_MIC; |
| 1780 | } |
| 1781 | break; |
| 1782 | case AUDIO_SOURCE_CAMCORDER: |
| 1783 | if (hasBackMicrophone()) { |
| 1784 | device = AudioSystem::DEVICE_IN_BACK_MIC; |
| 1785 | } else { |
| 1786 | device = AudioSystem::DEVICE_IN_BUILTIN_MIC; |
| 1787 | } |
| 1788 | break; |
| 1789 | case AUDIO_SOURCE_VOICE_UPLINK: |
| 1790 | case AUDIO_SOURCE_VOICE_DOWNLINK: |
| 1791 | case AUDIO_SOURCE_VOICE_CALL: |
| 1792 | device = AudioSystem::DEVICE_IN_VOICE_CALL; |
| 1793 | break; |
| 1794 | default: |
| 1795 | LOGW("getInput() invalid input source %d", inputSource); |
| 1796 | device = 0; |
| 1797 | break; |
| 1798 | } |
| 1799 | LOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device); |
| 1800 | return device; |
| 1801 | } |
| 1802 | |
| 1803 | audio_io_handle_t AudioPolicyManagerBase::getActiveInput() |
| 1804 | { |
| 1805 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 1806 | if (mInputs.valueAt(i)->mRefCount > 0) { |
| 1807 | return mInputs.keyAt(i); |
| 1808 | } |
| 1809 | } |
| 1810 | return 0; |
| 1811 | } |
| 1812 | |
| 1813 | float AudioPolicyManagerBase::computeVolume(int stream, int index, audio_io_handle_t output, uint32_t device) |
| 1814 | { |
| 1815 | float volume = 1.0; |
| 1816 | AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output); |
| 1817 | StreamDescriptor &streamDesc = mStreams[stream]; |
| 1818 | |
| 1819 | if (device == 0) { |
| 1820 | device = outputDesc->device(); |
| 1821 | } |
| 1822 | |
| 1823 | int volInt = (100 * (index - streamDesc.mIndexMin)) / (streamDesc.mIndexMax - streamDesc.mIndexMin); |
| 1824 | volume = AudioSystem::linearToLog(volInt); |
| 1825 | |
Eric Laurent | ef9500f | 2010-03-11 14:47:00 -0800 | [diff] [blame] | 1826 | // if a headset is connected, apply the following rules to ring tones and notifications |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1827 | // to avoid sound level bursts in user's ears: |
| 1828 | // - always attenuate ring tones and notifications volume by 6dB |
| 1829 | // - if music is playing, always limit the volume to current music volume, |
| 1830 | // with a minimum threshold at -36dB so that notification is always perceived. |
| 1831 | if ((device & |
| 1832 | (AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP | |
| 1833 | AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | |
| 1834 | AudioSystem::DEVICE_OUT_WIRED_HEADSET | |
Praveen Bharathi | 21e941b | 2010-10-06 15:23:14 -0500 | [diff] [blame] | 1835 | AudioSystem::DEVICE_OUT_WIRED_HEADPHONE | |
| 1836 | AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET | |
| 1837 | AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET)) && |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1838 | (getStrategy((AudioSystem::stream_type)stream) == STRATEGY_SONIFICATION) && |
| 1839 | streamDesc.mCanBeMuted) { |
| 1840 | volume *= SONIFICATION_HEADSET_VOLUME_FACTOR; |
| 1841 | // when the phone is ringing we must consider that music could have been paused just before |
| 1842 | // by the music application and behave as if music was active if the last music track was |
| 1843 | // just stopped |
| 1844 | if (outputDesc->mRefCount[AudioSystem::MUSIC] || mLimitRingtoneVolume) { |
| 1845 | float musicVol = computeVolume(AudioSystem::MUSIC, mStreams[AudioSystem::MUSIC].mIndexCur, output, device); |
| 1846 | float minVol = (musicVol > SONIFICATION_HEADSET_VOLUME_MIN) ? musicVol : SONIFICATION_HEADSET_VOLUME_MIN; |
| 1847 | if (volume > minVol) { |
| 1848 | volume = minVol; |
| 1849 | LOGV("computeVolume limiting volume to %f musicVol %f", minVol, musicVol); |
| 1850 | } |
| 1851 | } |
| 1852 | } |
| 1853 | |
| 1854 | return volume; |
| 1855 | } |
| 1856 | |
| 1857 | status_t AudioPolicyManagerBase::checkAndSetVolume(int stream, int index, audio_io_handle_t output, uint32_t device, int delayMs, bool force) |
| 1858 | { |
| 1859 | |
| 1860 | // do not change actual stream volume if the stream is muted |
| 1861 | if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) { |
| 1862 | LOGV("checkAndSetVolume() stream %d muted count %d", stream, mOutputs.valueFor(output)->mMuteCount[stream]); |
| 1863 | return NO_ERROR; |
| 1864 | } |
| 1865 | |
| 1866 | // do not change in call volume if bluetooth is connected and vice versa |
| 1867 | if ((stream == AudioSystem::VOICE_CALL && mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) || |
| 1868 | (stream == AudioSystem::BLUETOOTH_SCO && mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO)) { |
| 1869 | LOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm", |
| 1870 | stream, mForceUse[AudioSystem::FOR_COMMUNICATION]); |
| 1871 | return INVALID_OPERATION; |
| 1872 | } |
| 1873 | |
| 1874 | float volume = computeVolume(stream, index, output, device); |
Eric Laurent | cc9c424 | 2010-05-25 09:31:59 -0700 | [diff] [blame] | 1875 | // We actually change the volume if: |
| 1876 | // - the float value returned by computeVolume() changed |
| 1877 | // - the force flag is set |
| 1878 | if (volume != mOutputs.valueFor(output)->mCurVolume[stream] || |
| 1879 | force) { |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1880 | mOutputs.valueFor(output)->mCurVolume[stream] = volume; |
| 1881 | LOGV("setStreamVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs); |
| 1882 | if (stream == AudioSystem::VOICE_CALL || |
| 1883 | stream == AudioSystem::DTMF || |
| 1884 | stream == AudioSystem::BLUETOOTH_SCO) { |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1885 | // offset value to reflect actual hardware volume that never reaches 0 |
| 1886 | // 1% corresponds roughly to first step in VOICE_CALL stream volume setting (see AudioService.java) |
| 1887 | volume = 0.01 + 0.99 * volume; |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1888 | } |
| 1889 | mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream, volume, output, delayMs); |
| 1890 | } |
| 1891 | |
Eric Laurent | cc9c424 | 2010-05-25 09:31:59 -0700 | [diff] [blame] | 1892 | if (stream == AudioSystem::VOICE_CALL || |
| 1893 | stream == AudioSystem::BLUETOOTH_SCO) { |
| 1894 | float voiceVolume; |
| 1895 | // Force voice volume to max for bluetooth SCO as volume is managed by the headset |
| 1896 | if (stream == AudioSystem::VOICE_CALL) { |
| 1897 | voiceVolume = (float)index/(float)mStreams[stream].mIndexMax; |
| 1898 | } else { |
| 1899 | voiceVolume = 1.0; |
| 1900 | } |
| 1901 | if (voiceVolume != mLastVoiceVolume && output == mHardwareOutput) { |
| 1902 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); |
| 1903 | mLastVoiceVolume = voiceVolume; |
| 1904 | } |
| 1905 | } |
| 1906 | |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 1907 | return NO_ERROR; |
| 1908 | } |
| 1909 | |
| 1910 | void AudioPolicyManagerBase::applyStreamVolumes(audio_io_handle_t output, uint32_t device, int delayMs) |
| 1911 | { |
| 1912 | LOGV("applyStreamVolumes() for output %d and device %x", output, device); |
| 1913 | |
| 1914 | for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) { |
| 1915 | checkAndSetVolume(stream, mStreams[stream].mIndexCur, output, device, delayMs); |
| 1916 | } |
| 1917 | } |
| 1918 | |
| 1919 | void AudioPolicyManagerBase::setStrategyMute(routing_strategy strategy, bool on, audio_io_handle_t output, int delayMs) |
| 1920 | { |
| 1921 | LOGV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output); |
| 1922 | for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) { |
| 1923 | if (getStrategy((AudioSystem::stream_type)stream) == strategy) { |
| 1924 | setStreamMute(stream, on, output, delayMs); |
| 1925 | } |
| 1926 | } |
| 1927 | } |
| 1928 | |
| 1929 | void AudioPolicyManagerBase::setStreamMute(int stream, bool on, audio_io_handle_t output, int delayMs) |
| 1930 | { |
| 1931 | StreamDescriptor &streamDesc = mStreams[stream]; |
| 1932 | AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output); |
| 1933 | |
| 1934 | LOGV("setStreamMute() stream %d, mute %d, output %d, mMuteCount %d", stream, on, output, outputDesc->mMuteCount[stream]); |
| 1935 | |
| 1936 | if (on) { |
| 1937 | if (outputDesc->mMuteCount[stream] == 0) { |
| 1938 | if (streamDesc.mCanBeMuted) { |
| 1939 | checkAndSetVolume(stream, 0, output, outputDesc->device(), delayMs); |
| 1940 | } |
| 1941 | } |
| 1942 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored |
| 1943 | outputDesc->mMuteCount[stream]++; |
| 1944 | } else { |
| 1945 | if (outputDesc->mMuteCount[stream] == 0) { |
| 1946 | LOGW("setStreamMute() unmuting non muted stream!"); |
| 1947 | return; |
| 1948 | } |
| 1949 | if (--outputDesc->mMuteCount[stream] == 0) { |
| 1950 | checkAndSetVolume(stream, streamDesc.mIndexCur, output, outputDesc->device(), delayMs); |
| 1951 | } |
| 1952 | } |
| 1953 | } |
| 1954 | |
| 1955 | void AudioPolicyManagerBase::handleIncallSonification(int stream, bool starting, bool stateChange) |
| 1956 | { |
| 1957 | // if the stream pertains to sonification strategy and we are in call we must |
| 1958 | // mute the stream if it is low visibility. If it is high visibility, we must play a tone |
| 1959 | // in the device used for phone strategy and play the tone if the selected device does not |
| 1960 | // interfere with the device used for phone strategy |
| 1961 | // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as |
| 1962 | // many times as there are active tracks on the output |
| 1963 | |
| 1964 | if (getStrategy((AudioSystem::stream_type)stream) == STRATEGY_SONIFICATION) { |
| 1965 | AudioOutputDescriptor *outputDesc = mOutputs.valueFor(mHardwareOutput); |
| 1966 | LOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d", |
| 1967 | stream, starting, outputDesc->mDevice, stateChange); |
| 1968 | if (outputDesc->mRefCount[stream]) { |
| 1969 | int muteCount = 1; |
| 1970 | if (stateChange) { |
| 1971 | muteCount = outputDesc->mRefCount[stream]; |
| 1972 | } |
| 1973 | if (AudioSystem::isLowVisibility((AudioSystem::stream_type)stream)) { |
| 1974 | LOGV("handleIncallSonification() low visibility, muteCount %d", muteCount); |
| 1975 | for (int i = 0; i < muteCount; i++) { |
| 1976 | setStreamMute(stream, starting, mHardwareOutput); |
| 1977 | } |
| 1978 | } else { |
| 1979 | LOGV("handleIncallSonification() high visibility"); |
| 1980 | if (outputDesc->device() & getDeviceForStrategy(STRATEGY_PHONE)) { |
| 1981 | LOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount); |
| 1982 | for (int i = 0; i < muteCount; i++) { |
| 1983 | setStreamMute(stream, starting, mHardwareOutput); |
| 1984 | } |
| 1985 | } |
| 1986 | if (starting) { |
| 1987 | mpClientInterface->startTone(ToneGenerator::TONE_SUP_CALL_WAITING, AudioSystem::VOICE_CALL); |
| 1988 | } else { |
| 1989 | mpClientInterface->stopTone(); |
| 1990 | } |
| 1991 | } |
| 1992 | } |
| 1993 | } |
| 1994 | } |
| 1995 | |
Jean-Michel Trivi | 8f677d6 | 2010-11-15 12:11:32 -0800 | [diff] [blame] | 1996 | bool AudioPolicyManagerBase::isInCall() |
| 1997 | { |
| 1998 | return isStateInCall(mPhoneState); |
| 1999 | } |
| 2000 | |
| 2001 | bool AudioPolicyManagerBase::isStateInCall(int state) { |
| 2002 | return ((state == AudioSystem::MODE_IN_CALL) || |
| 2003 | (state == AudioSystem::MODE_IN_COMMUNICATION)); |
| 2004 | } |
| 2005 | |
Eric Laurent | ef9500f | 2010-03-11 14:47:00 -0800 | [diff] [blame] | 2006 | bool AudioPolicyManagerBase::needsDirectOuput(AudioSystem::stream_type stream, |
| 2007 | uint32_t samplingRate, |
| 2008 | uint32_t format, |
| 2009 | uint32_t channels, |
| 2010 | AudioSystem::output_flags flags, |
| 2011 | uint32_t device) |
| 2012 | { |
| 2013 | return ((flags & AudioSystem::OUTPUT_FLAG_DIRECT) || |
| 2014 | (format !=0 && !AudioSystem::isLinearPCM(format))); |
| 2015 | } |
| 2016 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2017 | uint32_t AudioPolicyManagerBase::getMaxEffectsCpuLoad() |
| 2018 | { |
| 2019 | return MAX_EFFECTS_CPU_LOAD; |
| 2020 | } |
| 2021 | |
| 2022 | uint32_t AudioPolicyManagerBase::getMaxEffectsMemory() |
| 2023 | { |
| 2024 | return MAX_EFFECTS_MEMORY; |
| 2025 | } |
| 2026 | |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 2027 | // --- AudioOutputDescriptor class implementation |
| 2028 | |
| 2029 | AudioPolicyManagerBase::AudioOutputDescriptor::AudioOutputDescriptor() |
| 2030 | : mId(0), mSamplingRate(0), mFormat(0), mChannels(0), mLatency(0), |
| 2031 | mFlags((AudioSystem::output_flags)0), mDevice(0), mOutput1(0), mOutput2(0) |
| 2032 | { |
| 2033 | // clear usage count for all stream types |
| 2034 | for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) { |
| 2035 | mRefCount[i] = 0; |
| 2036 | mCurVolume[i] = -1.0; |
| 2037 | mMuteCount[i] = 0; |
| 2038 | } |
| 2039 | } |
| 2040 | |
| 2041 | uint32_t AudioPolicyManagerBase::AudioOutputDescriptor::device() |
| 2042 | { |
| 2043 | uint32_t device = 0; |
| 2044 | if (isDuplicated()) { |
| 2045 | device = mOutput1->mDevice | mOutput2->mDevice; |
| 2046 | } else { |
| 2047 | device = mDevice; |
| 2048 | } |
| 2049 | return device; |
| 2050 | } |
| 2051 | |
| 2052 | void AudioPolicyManagerBase::AudioOutputDescriptor::changeRefCount(AudioSystem::stream_type stream, int delta) |
| 2053 | { |
| 2054 | // forward usage count change to attached outputs |
| 2055 | if (isDuplicated()) { |
| 2056 | mOutput1->changeRefCount(stream, delta); |
| 2057 | mOutput2->changeRefCount(stream, delta); |
| 2058 | } |
| 2059 | if ((delta + (int)mRefCount[stream]) < 0) { |
| 2060 | LOGW("changeRefCount() invalid delta %d for stream %d, refCount %d", delta, stream, mRefCount[stream]); |
| 2061 | mRefCount[stream] = 0; |
| 2062 | return; |
| 2063 | } |
| 2064 | mRefCount[stream] += delta; |
| 2065 | LOGV("changeRefCount() stream %d, count %d", stream, mRefCount[stream]); |
| 2066 | } |
| 2067 | |
| 2068 | uint32_t AudioPolicyManagerBase::AudioOutputDescriptor::refCount() |
| 2069 | { |
| 2070 | uint32_t refcount = 0; |
| 2071 | for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) { |
| 2072 | refcount += mRefCount[i]; |
| 2073 | } |
| 2074 | return refcount; |
| 2075 | } |
| 2076 | |
| 2077 | uint32_t AudioPolicyManagerBase::AudioOutputDescriptor::strategyRefCount(routing_strategy strategy) |
| 2078 | { |
| 2079 | uint32_t refCount = 0; |
| 2080 | for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) { |
| 2081 | if (getStrategy((AudioSystem::stream_type)i) == strategy) { |
| 2082 | refCount += mRefCount[i]; |
| 2083 | } |
| 2084 | } |
| 2085 | return refCount; |
| 2086 | } |
| 2087 | |
| 2088 | |
| 2089 | status_t AudioPolicyManagerBase::AudioOutputDescriptor::dump(int fd) |
| 2090 | { |
| 2091 | const size_t SIZE = 256; |
| 2092 | char buffer[SIZE]; |
| 2093 | String8 result; |
| 2094 | |
| 2095 | snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate); |
| 2096 | result.append(buffer); |
| 2097 | snprintf(buffer, SIZE, " Format: %d\n", mFormat); |
| 2098 | result.append(buffer); |
| 2099 | snprintf(buffer, SIZE, " Channels: %08x\n", mChannels); |
| 2100 | result.append(buffer); |
| 2101 | snprintf(buffer, SIZE, " Latency: %d\n", mLatency); |
| 2102 | result.append(buffer); |
| 2103 | snprintf(buffer, SIZE, " Flags %08x\n", mFlags); |
| 2104 | result.append(buffer); |
| 2105 | snprintf(buffer, SIZE, " Devices %08x\n", device()); |
| 2106 | result.append(buffer); |
| 2107 | snprintf(buffer, SIZE, " Stream volume refCount muteCount\n"); |
| 2108 | result.append(buffer); |
| 2109 | for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) { |
| 2110 | snprintf(buffer, SIZE, " %02d %.03f %02d %02d\n", i, mCurVolume[i], mRefCount[i], mMuteCount[i]); |
| 2111 | result.append(buffer); |
| 2112 | } |
| 2113 | write(fd, result.string(), result.size()); |
| 2114 | |
| 2115 | return NO_ERROR; |
| 2116 | } |
| 2117 | |
| 2118 | // --- AudioInputDescriptor class implementation |
| 2119 | |
| 2120 | AudioPolicyManagerBase::AudioInputDescriptor::AudioInputDescriptor() |
| 2121 | : mSamplingRate(0), mFormat(0), mChannels(0), |
| 2122 | mAcoustics((AudioSystem::audio_in_acoustics)0), mDevice(0), mRefCount(0) |
| 2123 | { |
| 2124 | } |
| 2125 | |
| 2126 | status_t AudioPolicyManagerBase::AudioInputDescriptor::dump(int fd) |
| 2127 | { |
| 2128 | const size_t SIZE = 256; |
| 2129 | char buffer[SIZE]; |
| 2130 | String8 result; |
| 2131 | |
| 2132 | snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate); |
| 2133 | result.append(buffer); |
| 2134 | snprintf(buffer, SIZE, " Format: %d\n", mFormat); |
| 2135 | result.append(buffer); |
| 2136 | snprintf(buffer, SIZE, " Channels: %08x\n", mChannels); |
| 2137 | result.append(buffer); |
| 2138 | snprintf(buffer, SIZE, " Acoustics %08x\n", mAcoustics); |
| 2139 | result.append(buffer); |
| 2140 | snprintf(buffer, SIZE, " Devices %08x\n", mDevice); |
| 2141 | result.append(buffer); |
| 2142 | snprintf(buffer, SIZE, " Ref Count %d\n", mRefCount); |
| 2143 | result.append(buffer); |
| 2144 | write(fd, result.string(), result.size()); |
| 2145 | |
| 2146 | return NO_ERROR; |
| 2147 | } |
| 2148 | |
| 2149 | // --- StreamDescriptor class implementation |
| 2150 | |
| 2151 | void AudioPolicyManagerBase::StreamDescriptor::dump(char* buffer, size_t size) |
| 2152 | { |
| 2153 | snprintf(buffer, size, " %02d %02d %02d %d\n", |
| 2154 | mIndexMin, |
| 2155 | mIndexMax, |
| 2156 | mIndexCur, |
| 2157 | mCanBeMuted); |
| 2158 | } |
| 2159 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2160 | // --- EffectDescriptor class implementation |
| 2161 | |
| 2162 | status_t AudioPolicyManagerBase::EffectDescriptor::dump(int fd) |
| 2163 | { |
| 2164 | const size_t SIZE = 256; |
| 2165 | char buffer[SIZE]; |
| 2166 | String8 result; |
| 2167 | |
| 2168 | snprintf(buffer, SIZE, " Output: %d\n", mOutput); |
| 2169 | result.append(buffer); |
| 2170 | snprintf(buffer, SIZE, " Strategy: %d\n", mStrategy); |
| 2171 | result.append(buffer); |
| 2172 | snprintf(buffer, SIZE, " Session: %d\n", mSession); |
| 2173 | result.append(buffer); |
| 2174 | snprintf(buffer, SIZE, " Name: %s\n", mDesc.name); |
| 2175 | result.append(buffer); |
| 2176 | write(fd, result.string(), result.size()); |
| 2177 | |
| 2178 | return NO_ERROR; |
| 2179 | } |
| 2180 | |
| 2181 | |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 2182 | |
| 2183 | }; // namespace android |