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