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