Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 3 | * Copyright (c) 2009-2013, The Linux Foundation. All rights reserved. |
| 4 | * Not a Contribution, Apache license notifications and license are retained |
| 5 | * for attribution purposes only. |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | * you may not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | */ |
| 19 | |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 20 | #define LOG_TAG "AudioPolicyManager8660" |
| 21 | //#define LOG_NDDEBUG 0 |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 22 | |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 23 | //#define VERY_VERBOSE_LOGGING |
| 24 | #ifdef VERY_VERBOSE_LOGGING |
| 25 | #define ALOGVV ALOGV |
| 26 | #else |
| 27 | #define ALOGVV(a...) do { } while(0) |
| 28 | #endif |
| 29 | |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 30 | // A device mask for all audio input devices that are considered "virtual" when evaluating |
| 31 | // active inputs in getActiveInput() |
| 32 | #define APM_AUDIO_IN_DEVICE_VIRTUAL_ALL AUDIO_DEVICE_IN_REMOTE_SUBMIX |
| 33 | |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 34 | #include <utils/Log.h> |
| 35 | #include "AudioPolicyManager.h" |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 36 | #include <hardware/audio_effect.h> |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 37 | #include <media/mediarecorder.h> |
| 38 | #include <hardware/audio.h> |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 39 | #include <math.h> |
| 40 | #include <hardware_legacy/audio_policy_conf.h> |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 41 | |
| 42 | namespace android_audio_legacy { |
| 43 | |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 44 | |
| 45 | |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 46 | // ---------------------------------------------------------------------------- |
| 47 | // AudioPolicyManager for msm8660 platform |
| 48 | // ---------------------------------------------------------------------------- |
| 49 | audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy, bool fromCache) |
| 50 | { |
| 51 | uint32_t device = 0; |
| 52 | |
| 53 | if (fromCache) { |
| 54 | ALOGV("getDeviceForStrategy() from cache strategy %d, device %x", |
| 55 | strategy, mDeviceForStrategy[strategy]); |
| 56 | return mDeviceForStrategy[strategy]; |
| 57 | } |
| 58 | |
| 59 | switch (strategy) { |
| 60 | |
| 61 | case STRATEGY_SONIFICATION_RESPECTFUL: |
| 62 | if (isInCall()) { |
| 63 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/); |
| 64 | } else if (isStreamActive(AudioSystem::MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) { |
| 65 | // while media is playing (or has recently played), use the same device |
| 66 | device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/); |
| 67 | } else { |
| 68 | // when media is not playing anymore, fall back on the sonification behavior |
| 69 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/); |
| 70 | } |
| 71 | |
| 72 | break; |
| 73 | |
| 74 | case STRATEGY_DTMF: |
| 75 | if (!isInCall()) { |
| 76 | // when off call, DTMF strategy follows the same rules as MEDIA strategy |
| 77 | device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/); |
| 78 | break; |
| 79 | } |
| 80 | // when in call, DTMF and PHONE strategies follow the same rules |
| 81 | // FALL THROUGH |
| 82 | |
| 83 | case STRATEGY_PHONE: |
| 84 | // for phone strategy, we first consider the forced use and then the available devices by order |
| 85 | // of priority |
| 86 | switch (mForceUse[AudioSystem::FOR_COMMUNICATION]) { |
| 87 | case AudioSystem::FORCE_BT_SCO: |
| 88 | if (!isInCall() || strategy != STRATEGY_DTMF) { |
| 89 | device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_CARKIT; |
| 90 | if (device) break; |
| 91 | } |
| 92 | device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_HEADSET; |
| 93 | if (device) break; |
| 94 | device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_SCO; |
| 95 | if (device) break; |
| 96 | // if SCO device is requested but no SCO device is available, fall back to default case |
| 97 | // FALL THROUGH |
| 98 | |
| 99 | default: // FORCE_NONE |
| 100 | // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP |
| 101 | if (mHasA2dp && !isInCall() && |
| 102 | (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) && |
| 103 | (getA2dpOutput() != 0) && !mA2dpSuspended) { |
| 104 | device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP; |
| 105 | if (device) break; |
| 106 | device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES; |
| 107 | if (device) break; |
| 108 | } |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 109 | |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 110 | device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADPHONE; |
| 111 | if (device) break; |
| 112 | device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADSET; |
| 113 | if (device) break; |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 114 | device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY; |
| 115 | if (device) break; |
| 116 | device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE; |
| 117 | if (device) break; |
| 118 | device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET; |
| 119 | if (device) break; |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 120 | device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_AUX_DIGITAL; |
| 121 | if (device) break; |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 122 | device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET; |
| 123 | if (device) break; |
| 124 | device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_EARPIECE; |
| 125 | if (device) break; |
| 126 | device = mDefaultOutputDevice; |
| 127 | if (device == 0) { |
| 128 | ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE"); |
| 129 | } |
| 130 | break; |
| 131 | |
| 132 | case AudioSystem::FORCE_SPEAKER: |
| 133 | // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to |
| 134 | // A2DP speaker when forcing to speaker output |
| 135 | if (mHasA2dp && !isInCall() && |
| 136 | (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) && |
| 137 | (getA2dpOutput() != 0) && !mA2dpSuspended) { |
| 138 | device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER; |
| 139 | if (device) break; |
| 140 | } |
| 141 | device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY; |
| 142 | if (device) break; |
| 143 | device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE; |
| 144 | if (device) break; |
| 145 | device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET; |
| 146 | if (device) break; |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 147 | device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_AUX_DIGITAL; |
| 148 | if (device) break; |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 149 | device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET; |
| 150 | if (device) break; |
| 151 | device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_SPEAKER; |
| 152 | if (device) break; |
| 153 | device = mDefaultOutputDevice; |
| 154 | if (device == 0) { |
| 155 | ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER"); |
| 156 | } |
| 157 | break; |
| 158 | } |
| 159 | #ifdef QCOM_FM_ENABLED |
| 160 | if (mAvailableOutputDevices & AudioSystem::DEVICE_OUT_FM) { |
| 161 | device |= AudioSystem::DEVICE_OUT_FM; |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 162 | if (mForceUse[AudioSystem::FOR_MEDIA] == AudioSystem::FORCE_SPEAKER) { |
| 163 | device &= ~(AudioSystem::DEVICE_OUT_WIRED_HEADSET); |
| 164 | device |= AudioSystem::DEVICE_OUT_SPEAKER; |
| 165 | } |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 166 | } |
| 167 | #endif |
| 168 | break; |
| 169 | |
| 170 | case STRATEGY_SONIFICATION: |
| 171 | |
| 172 | // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by |
| 173 | // handleIncallSonification(). |
| 174 | if (isInCall()) { |
| 175 | device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/); |
| 176 | break; |
| 177 | } |
| 178 | // FALL THROUGH |
| 179 | |
| 180 | case STRATEGY_ENFORCED_AUDIBLE: |
| 181 | // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION |
| 182 | // except: |
| 183 | // - when in call where it doesn't default to STRATEGY_PHONE behavior |
| 184 | // - in countries where not enforced in which case it follows STRATEGY_MEDIA |
| 185 | |
| 186 | if (strategy == STRATEGY_SONIFICATION || |
| 187 | !mStreams[AUDIO_STREAM_ENFORCED_AUDIBLE].mCanBeMuted) { |
| 188 | device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_SPEAKER; |
| 189 | if (device == 0) { |
| 190 | ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION"); |
| 191 | } |
| 192 | } |
| 193 | // The second device used for sonification is the same as the device used by media strategy |
| 194 | // FALL THROUGH |
| 195 | |
| 196 | case STRATEGY_MEDIA: { |
| 197 | //To route FM stream to speaker when headset is connected, a new switch case is added. |
| 198 | //case AudioSystem::FORCE_SPEAKER for STRATEGY_MEDIA will come only when we need to route |
| 199 | //FM stream to speaker. |
| 200 | switch (mForceUse[AudioSystem::FOR_MEDIA]) { |
| 201 | default:{ |
| 202 | uint32_t device2 = 0; |
| 203 | if ((mHasA2dp && (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) && |
| 204 | (getA2dpOutput() != 0) && !mA2dpSuspended )) { |
| 205 | device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP; |
| 206 | if (device2 == 0) { |
| 207 | device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES; |
| 208 | } |
| 209 | if (device2 == 0) { |
| 210 | device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER; |
| 211 | } |
| 212 | } |
| 213 | if (device2 == 0) { |
| 214 | device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADPHONE; |
| 215 | } |
| 216 | if (device2 == 0) { |
| 217 | device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADSET; |
| 218 | } |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 219 | #ifdef QCOM_FM_ENABLED |
| 220 | if (device2 == 0) { |
| 221 | device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_FM_TX; |
| 222 | } |
| 223 | #endif |
| 224 | if (device2 == 0) { |
| 225 | device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY; |
| 226 | } |
| 227 | if (device2 == 0) { |
| 228 | device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE; |
| 229 | } |
| 230 | if (device2 == 0) { |
| 231 | device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET; |
| 232 | } |
| 233 | if (device2 == 0) { |
| 234 | device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_AUX_DIGITAL; |
| 235 | } |
| 236 | if (device2 == 0) { |
| 237 | device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET; |
| 238 | } |
| 239 | if (device2 == 0) { |
| 240 | device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_SPEAKER; |
| 241 | } |
| 242 | |
| 243 | // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or |
| 244 | // STRATEGY_ENFORCED_AUDIBLE, 0 otherwise |
| 245 | device |= device2; |
| 246 | if (device) break; |
| 247 | device = mDefaultOutputDevice; |
| 248 | } |
| 249 | break; |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 250 | |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 251 | case AudioSystem::FORCE_SPEAKER: |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 252 | device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_SPEAKER; |
| 253 | break; |
| 254 | } |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 255 | #ifdef QCOM_FM_ENABLED |
| 256 | if (mAvailableOutputDevices & AudioSystem::DEVICE_OUT_FM) { |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 257 | device |= AudioSystem::DEVICE_OUT_FM; |
| 258 | } |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 259 | #endif |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 260 | // Do not play media stream if in call and the requested device would change the hardware |
| 261 | // output routing |
| 262 | if (mPhoneState == AudioSystem::MODE_IN_CALL && |
| 263 | !AudioSystem::isA2dpDevice((AudioSystem::audio_devices)device) && |
| 264 | device != getDeviceForStrategy(STRATEGY_PHONE)) { |
| 265 | device = getDeviceForStrategy(STRATEGY_PHONE); |
| 266 | ALOGV("getDeviceForStrategy() incompatible media and phone devices"); |
| 267 | } |
| 268 | } break; |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 269 | |
| 270 | default: |
| 271 | ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy); |
| 272 | break; |
| 273 | } |
| 274 | |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 275 | ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device); |
| 276 | |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 277 | return (audio_devices_t)device; |
| 278 | } |
| 279 | |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 280 | status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device, |
| 281 | AudioSystem::device_connection_state state, |
| 282 | const char *device_address) |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 283 | { |
| 284 | SortedVector <audio_io_handle_t> outputs; |
| 285 | |
| 286 | ALOGV("setDeviceConnectionState() device: %x, state %d, address %s", device, state, device_address); |
| 287 | |
| 288 | // connect/disconnect only 1 device at a time |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 289 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 290 | |
| 291 | if (strlen(device_address) >= MAX_DEVICE_ADDRESS_LEN) { |
| 292 | ALOGE("setDeviceConnectionState() invalid address: %s", device_address); |
| 293 | return BAD_VALUE; |
| 294 | } |
| 295 | |
| 296 | // handle output devices |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 297 | if (audio_is_output_device(device)) { |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 298 | |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 299 | if (!mHasA2dp && audio_is_a2dp_device(device)) { |
| 300 | ALOGE("setDeviceConnectionState() invalid A2DP device: %x", device); |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 301 | return BAD_VALUE; |
| 302 | } |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 303 | if (!mHasUsb && audio_is_usb_device(device)) { |
| 304 | ALOGE("setDeviceConnectionState() invalid USB audio device: %x", device); |
| 305 | return BAD_VALUE; |
| 306 | } |
| 307 | if (!mHasRemoteSubmix && audio_is_remote_submix_device((audio_devices_t)device)) { |
| 308 | ALOGE("setDeviceConnectionState() invalid remote submix audio device: %x", device); |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 309 | return BAD_VALUE; |
| 310 | } |
| 311 | |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 312 | // save a copy of the opened output descriptors before any output is opened or closed |
| 313 | // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies() |
| 314 | mPreviousOutputs = mOutputs; |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 315 | switch (state) |
| 316 | { |
| 317 | // handle output device connection |
| 318 | case AudioSystem::DEVICE_STATE_AVAILABLE: |
| 319 | |
| 320 | if (mAvailableOutputDevices & device) { |
| 321 | ALOGW("setDeviceConnectionState() device already connected: %x", device); |
| 322 | return INVALID_OPERATION; |
| 323 | } |
| 324 | ALOGV("setDeviceConnectionState() connecting device %x", device); |
| 325 | |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 326 | if (checkOutputsForDevice(device, state, outputs) != NO_ERROR) { |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 327 | return INVALID_OPERATION; |
| 328 | } |
| 329 | ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %d outputs", |
| 330 | outputs.size()); |
| 331 | // register new device as available |
| 332 | mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices | device); |
| 333 | ALOGV("setDeviceConnectionState() connecting device %x", mAvailableOutputDevices); |
| 334 | |
| 335 | if (!outputs.isEmpty()) { |
| 336 | String8 paramStr; |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 337 | if (mHasA2dp && audio_is_a2dp_device(device)) { |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 338 | // handle A2DP device connection |
| 339 | AudioParameter param; |
| 340 | param.add(String8(AUDIO_PARAMETER_A2DP_SINK_ADDRESS), String8(device_address)); |
| 341 | paramStr = param.toString(); |
| 342 | mA2dpDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN); |
| 343 | mA2dpSuspended = false; |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 344 | } else if (audio_is_bluetooth_sco_device(device)) { |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 345 | // handle SCO device connection |
| 346 | mScoDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN); |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 347 | } else if (mHasUsb && audio_is_usb_device(device)) { |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 348 | // handle USB device connection |
| 349 | mUsbCardAndDevice = String8(device_address, MAX_DEVICE_ADDRESS_LEN); |
| 350 | paramStr = mUsbCardAndDevice; |
| 351 | } |
| 352 | if (!paramStr.isEmpty()) { |
| 353 | for (size_t i = 0; i < outputs.size(); i++) { |
| 354 | mpClientInterface->setParameters(outputs[i], paramStr); |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | break; |
| 359 | // handle output device disconnection |
| 360 | case AudioSystem::DEVICE_STATE_UNAVAILABLE: { |
| 361 | if (!(mAvailableOutputDevices & device)) { |
| 362 | ALOGW("setDeviceConnectionState() device not connected: %x", device); |
| 363 | return INVALID_OPERATION; |
| 364 | } |
| 365 | |
| 366 | ALOGV("setDeviceConnectionState() disconnecting device %x", device); |
| 367 | // remove device from available output devices |
| 368 | mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices & ~device); |
| 369 | |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 370 | checkOutputsForDevice(device, state, outputs); |
| 371 | if (mHasA2dp && audio_is_a2dp_device(device)) { |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 372 | // handle A2DP device disconnection |
| 373 | mA2dpDeviceAddress = ""; |
| 374 | mA2dpSuspended = false; |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 375 | } else if (audio_is_bluetooth_sco_device(device)) { |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 376 | // handle SCO device disconnection |
| 377 | mScoDeviceAddress = ""; |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 378 | } else if (mHasUsb && audio_is_usb_device(device)) { |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 379 | // handle USB device disconnection |
| 380 | mUsbCardAndDevice = ""; |
| 381 | } |
| 382 | } break; |
| 383 | |
| 384 | default: |
| 385 | ALOGE("setDeviceConnectionState() invalid state: %x", state); |
| 386 | return BAD_VALUE; |
| 387 | } |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 388 | #ifdef QCOM_FM_ENABLED |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 389 | |
| 390 | audio_devices_t NewDevice = AudioPolicyManagerBase::getNewDevice(mPrimaryOutput, false /*fromCache*/); |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 391 | if (device == AudioSystem::DEVICE_OUT_FM) { |
| 392 | if (state == AudioSystem::DEVICE_STATE_AVAILABLE) { |
| 393 | mOutputs.valueFor(mPrimaryOutput)->changeRefCount(AudioSystem::FM, 1); |
| 394 | } |
| 395 | else { |
| 396 | mOutputs.valueFor(mPrimaryOutput)->changeRefCount(AudioSystem::FM, -1); |
| 397 | } |
| 398 | } |
| 399 | #endif |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 400 | checkA2dpSuspend(); |
| 401 | AudioPolicyManagerBase::checkOutputForAllStrategies(); |
| 402 | // outputs must be closed after checkOutputForAllStrategies() is executed |
| 403 | if (!outputs.isEmpty()) { |
| 404 | for (size_t i = 0; i < outputs.size(); i++) { |
| 405 | // close unused outputs after device disconnection or direct outputs that have been |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 406 | if (state == AudioSystem::DEVICE_STATE_UNAVAILABLE){ |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 407 | closeOutput(outputs[i]); |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 412 | updateDevicesAndOutputs(); |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 413 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 414 | setOutputDevice(mOutputs.keyAt(i), |
| 415 | getNewDevice(mOutputs.keyAt(i), true /*fromCache*/), |
| 416 | true, |
| 417 | 0); |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 418 | } |
| 419 | |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 420 | if (device == AUDIO_DEVICE_OUT_WIRED_HEADSET) { |
| 421 | device = AUDIO_DEVICE_IN_WIRED_HEADSET; |
| 422 | } else if (device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO || |
| 423 | device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET || |
| 424 | device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT) { |
| 425 | device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET; |
| 426 | } else { |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 427 | return NO_ERROR; |
| 428 | } |
| 429 | } |
| 430 | // handle input devices |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 431 | if (audio_is_input_device(device)) { |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 432 | |
| 433 | switch (state) |
| 434 | { |
| 435 | // handle input device connection |
| 436 | case AudioSystem::DEVICE_STATE_AVAILABLE: { |
| 437 | if (mAvailableInputDevices & device) { |
| 438 | ALOGW("setDeviceConnectionState() device already connected: %d", device); |
| 439 | return INVALID_OPERATION; |
| 440 | } |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 441 | mAvailableInputDevices = mAvailableInputDevices | (device & ~AUDIO_DEVICE_BIT_IN); |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 442 | } |
| 443 | break; |
| 444 | |
| 445 | // handle input device disconnection |
| 446 | case AudioSystem::DEVICE_STATE_UNAVAILABLE: { |
| 447 | if (!(mAvailableInputDevices & device)) { |
| 448 | ALOGW("setDeviceConnectionState() device not connected: %d", device); |
| 449 | return INVALID_OPERATION; |
| 450 | } |
| 451 | mAvailableInputDevices = (audio_devices_t) (mAvailableInputDevices & ~device); |
| 452 | } break; |
| 453 | |
| 454 | default: |
| 455 | ALOGE("setDeviceConnectionState() invalid state: %x", state); |
| 456 | return BAD_VALUE; |
| 457 | } |
| 458 | |
| 459 | audio_io_handle_t activeInput = getActiveInput(); |
| 460 | if (activeInput != 0) { |
| 461 | AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput); |
| 462 | audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource); |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 463 | if ((newDevice != AUDIO_DEVICE_NONE) && (newDevice != inputDesc->mDevice)) { |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 464 | ALOGV("setDeviceConnectionState() changing device from %x to %x for input %d", |
| 465 | inputDesc->mDevice, newDevice, activeInput); |
| 466 | inputDesc->mDevice = newDevice; |
| 467 | AudioParameter param = AudioParameter(); |
| 468 | param.addInt(String8(AudioParameter::keyRouting), (int)newDevice); |
| 469 | mpClientInterface->setParameters(activeInput, param.toString()); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | return NO_ERROR; |
| 474 | } |
| 475 | |
| 476 | ALOGW("setDeviceConnectionState() invalid device: %x", device); |
| 477 | return BAD_VALUE; |
| 478 | } |
| 479 | |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 480 | AudioSystem::device_connection_state AudioPolicyManager::getDeviceConnectionState(audio_devices_t device, |
| 481 | const char *device_address) |
| 482 | { |
| 483 | AudioSystem::device_connection_state state = AudioSystem::DEVICE_STATE_UNAVAILABLE; |
| 484 | String8 address = String8(device_address); |
| 485 | if (audio_is_output_device(device)) { |
| 486 | if (device & mAvailableOutputDevices) { |
| 487 | if (audio_is_a2dp_device(device) && |
| 488 | (!mHasA2dp || (address != "" && mA2dpDeviceAddress != address))) { |
| 489 | return state; |
| 490 | } |
| 491 | if (audio_is_bluetooth_sco_device(device) && |
| 492 | address != "" && mScoDeviceAddress != address) { |
| 493 | return state; |
| 494 | } |
| 495 | if (audio_is_usb_device(device) && |
| 496 | (!mHasUsb || (address != "" && mUsbCardAndDevice != address))) { |
| 497 | ALOGE("getDeviceConnectionState() invalid device: %x", device); |
| 498 | return state; |
| 499 | } |
| 500 | if (audio_is_remote_submix_device((audio_devices_t)device) && !mHasRemoteSubmix) { |
| 501 | return state; |
| 502 | } |
| 503 | state = AudioSystem::DEVICE_STATE_AVAILABLE; |
| 504 | } |
| 505 | } else if (audio_is_input_device(device)) { |
| 506 | if (device & mAvailableInputDevices) { |
| 507 | state = AudioSystem::DEVICE_STATE_AVAILABLE; |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | return state; |
| 512 | } |
| 513 | |
| 514 | void AudioPolicyManager::setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config) |
| 515 | { |
| 516 | ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState); |
| 517 | |
| 518 | bool forceVolumeReeval = false; |
| 519 | switch(usage) { |
| 520 | case AudioSystem::FOR_COMMUNICATION: |
| 521 | if (config != AudioSystem::FORCE_SPEAKER && config != AudioSystem::FORCE_BT_SCO && |
| 522 | config != AudioSystem::FORCE_NONE) { |
| 523 | ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config); |
| 524 | return; |
| 525 | } |
| 526 | forceVolumeReeval = true; |
| 527 | mForceUse[usage] = config; |
| 528 | break; |
| 529 | case AudioSystem::FOR_MEDIA: |
| 530 | if (config != AudioSystem::FORCE_HEADPHONES && config != AudioSystem::FORCE_BT_A2DP && |
| 531 | config != AudioSystem::FORCE_WIRED_ACCESSORY && |
| 532 | config != AudioSystem::FORCE_ANALOG_DOCK && |
| 533 | config != AudioSystem::FORCE_DIGITAL_DOCK && config != AudioSystem::FORCE_NONE && |
| 534 | config != AudioSystem::FORCE_NO_BT_A2DP) { |
| 535 | ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config); |
| 536 | return; |
| 537 | } |
| 538 | mForceUse[usage] = config; |
| 539 | break; |
| 540 | case AudioSystem::FOR_RECORD: |
| 541 | if (config != AudioSystem::FORCE_BT_SCO && config != AudioSystem::FORCE_WIRED_ACCESSORY && |
| 542 | config != AudioSystem::FORCE_NONE) { |
| 543 | ALOGW("setForceUse() invalid config %d for FOR_RECORD", config); |
| 544 | return; |
| 545 | } |
| 546 | mForceUse[usage] = config; |
| 547 | break; |
| 548 | case AudioSystem::FOR_DOCK: |
| 549 | if (config != AudioSystem::FORCE_NONE && config != AudioSystem::FORCE_BT_CAR_DOCK && |
| 550 | config != AudioSystem::FORCE_BT_DESK_DOCK && |
| 551 | config != AudioSystem::FORCE_WIRED_ACCESSORY && |
| 552 | config != AudioSystem::FORCE_ANALOG_DOCK && |
| 553 | config != AudioSystem::FORCE_DIGITAL_DOCK) { |
| 554 | ALOGW("setForceUse() invalid config %d for FOR_DOCK", config); |
| 555 | } |
| 556 | forceVolumeReeval = true; |
| 557 | mForceUse[usage] = config; |
| 558 | break; |
| 559 | case AudioSystem::FOR_SYSTEM: |
| 560 | if (config != AudioSystem::FORCE_NONE && |
| 561 | config != AudioSystem::FORCE_SYSTEM_ENFORCED) { |
| 562 | ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config); |
| 563 | } |
| 564 | forceVolumeReeval = true; |
| 565 | mForceUse[usage] = config; |
| 566 | break; |
| 567 | default: |
| 568 | ALOGW("setForceUse() invalid usage %d", usage); |
| 569 | break; |
| 570 | } |
| 571 | |
| 572 | // check for device and output changes triggered by new force usage |
| 573 | checkA2dpSuspend(); |
| 574 | checkOutputForAllStrategies(); |
| 575 | updateDevicesAndOutputs(); |
| 576 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 577 | audio_io_handle_t output = mOutputs.keyAt(i); |
| 578 | audio_devices_t newDevice = getNewDevice(output, true /*fromCache*/); |
| 579 | setOutputDevice(output, newDevice, (newDevice != AUDIO_DEVICE_NONE)); |
| 580 | if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) { |
| 581 | applyStreamVolumes(output, newDevice, 0, true); |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | audio_io_handle_t activeInput = getActiveInput(); |
| 586 | if (activeInput != 0) { |
| 587 | AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput); |
| 588 | audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource); |
| 589 | if ((newDevice != AUDIO_DEVICE_NONE) && (newDevice != inputDesc->mDevice)) { |
| 590 | ALOGV("setForceUse() changing device from %x to %x for input %d", |
| 591 | inputDesc->mDevice, newDevice, activeInput); |
| 592 | inputDesc->mDevice = newDevice; |
| 593 | AudioParameter param = AudioParameter(); |
| 594 | param.addInt(String8(AudioParameter::keyRouting), (int)newDevice); |
| 595 | mpClientInterface->setParameters(activeInput, param.toString()); |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | } |
| 600 | |
| 601 | |
| 602 | AudioPolicyManagerBase::IOProfile *AudioPolicyManager::getProfileForDirectOutput( |
| 603 | audio_devices_t device, |
| 604 | uint32_t samplingRate, |
| 605 | uint32_t format, |
| 606 | uint32_t channelMask, |
| 607 | audio_output_flags_t flags) |
| 608 | { |
| 609 | if( !((flags & AUDIO_OUTPUT_FLAG_LPA) || |
| 610 | (flags & AUDIO_OUTPUT_FLAG_TUNNEL)|| |
| 611 | (flags & AUDIO_OUTPUT_FLAG_VOIP_RX)) ) |
| 612 | flags = AUDIO_OUTPUT_FLAG_DIRECT; |
| 613 | |
| 614 | for (size_t i = 0; i < mHwModules.size(); i++) { |
| 615 | if (mHwModules[i]->mHandle == 0) { |
| 616 | continue; |
| 617 | } |
| 618 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) { |
| 619 | AudioPolicyManagerBase::IOProfile *profile = mHwModules[i]->mOutputProfiles[j]; |
| 620 | if (isCompatibleProfile(profile, device, samplingRate, format, |
| 621 | channelMask, |
| 622 | flags)) { |
| 623 | if (mAvailableOutputDevices & profile->mSupportedDevices) { |
| 624 | return mHwModules[i]->mOutputProfiles[j]; |
| 625 | } |
| 626 | } |
| 627 | } |
| 628 | } |
| 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | |
| 633 | bool AudioPolicyManager::isCompatibleProfile(AudioPolicyManagerBase::IOProfile *profile, |
| 634 | audio_devices_t device, |
| 635 | uint32_t samplingRate, |
| 636 | uint32_t format, |
| 637 | uint32_t channelMask, |
| 638 | audio_output_flags_t flags) |
| 639 | { |
| 640 | if ((profile->mSupportedDevices & device) != device) { |
| 641 | return false; |
| 642 | } |
| 643 | if (profile->mFlags != flags) { |
| 644 | return false; |
| 645 | } |
| 646 | if (samplingRate != 0) { |
| 647 | size_t i; |
| 648 | for (i = 0; i < profile->mSamplingRates.size(); i++) |
| 649 | { |
| 650 | if (profile->mSamplingRates[i] == samplingRate) { |
| 651 | break; |
| 652 | } |
| 653 | } |
| 654 | if (i == profile->mSamplingRates.size()) { |
| 655 | return false; |
| 656 | } |
| 657 | } |
| 658 | if (format != 0) { |
| 659 | size_t i; |
| 660 | for (i = 0; i < profile->mFormats.size(); i++) |
| 661 | { |
| 662 | if (profile->mFormats[i] == format) { |
| 663 | break; |
| 664 | } |
| 665 | } |
| 666 | if (i == profile->mFormats.size()) { |
| 667 | return false; |
| 668 | } |
| 669 | } |
| 670 | if (channelMask != 0) { |
| 671 | size_t i; |
| 672 | for (i = 0; i < profile->mChannelMasks.size(); i++) |
| 673 | { |
| 674 | if (profile->mChannelMasks[i] == channelMask) { |
| 675 | break; |
| 676 | } |
| 677 | } |
| 678 | if (i == profile->mChannelMasks.size()) { |
| 679 | return false; |
| 680 | } |
| 681 | } |
| 682 | ALOGD(" profile found: device %x, flags %x, samplingrate %d,\ |
| 683 | format %x, channelMask %d", |
| 684 | device, flags, samplingRate, format, channelMask); |
| 685 | return true; |
| 686 | } |
| 687 | |
| 688 | status_t AudioPolicyManager::checkOutputsForDevice(audio_devices_t device, |
| 689 | AudioSystem::device_connection_state state, |
| 690 | SortedVector<audio_io_handle_t>& outputs) |
| 691 | { |
| 692 | AudioOutputDescriptor *desc; |
| 693 | |
| 694 | if (state == AudioSystem::DEVICE_STATE_AVAILABLE) { |
| 695 | // first list already open outputs that can be routed to this device |
| 696 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 697 | desc = mOutputs.valueAt(i); |
| 698 | if (!desc->isDuplicated() && (desc->mProfile->mSupportedDevices & device)) { |
| 699 | ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i)); |
| 700 | outputs.add(mOutputs.keyAt(i)); |
| 701 | } |
| 702 | } |
| 703 | // then look for output profiles that can be routed to this device |
| 704 | SortedVector<IOProfile *> profiles; |
| 705 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 706 | { |
| 707 | if (mHwModules[i]->mHandle == 0) { |
| 708 | continue; |
| 709 | } |
| 710 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 711 | { |
| 712 | if (mHwModules[i]->mOutputProfiles[j]->mSupportedDevices & device) { |
| 713 | ALOGV("checkOutputsForDevice(): adding profile %d from module %d", j, i); |
| 714 | profiles.add(mHwModules[i]->mOutputProfiles[j]); |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | if (profiles.isEmpty() && outputs.isEmpty()) { |
| 720 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 721 | return BAD_VALUE; |
| 722 | } |
| 723 | |
| 724 | // open outputs for matching profiles if needed. Direct outputs are also opened to |
| 725 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 726 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { |
| 727 | IOProfile *profile = profiles[profile_index]; |
| 728 | |
| 729 | // nothing to do if one output is already opened for this profile |
| 730 | size_t j; |
| 731 | for (j = 0; j < mOutputs.size(); j++) { |
| 732 | desc = mOutputs.valueAt(j); |
| 733 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
| 734 | break; |
| 735 | } |
| 736 | } |
| 737 | if (j != mOutputs.size()) { |
| 738 | continue; |
| 739 | } |
| 740 | |
| 741 | ALOGV("opening output for device %08x", device); |
| 742 | desc = new AudioOutputDescriptor(profile); |
| 743 | desc->mDevice = device; |
| 744 | audio_io_handle_t output = 0; |
| 745 | if (!(desc->mFlags & AUDIO_OUTPUT_FLAG_LPA || desc->mFlags & AUDIO_OUTPUT_FLAG_TUNNEL || |
| 746 | desc->mFlags & AUDIO_OUTPUT_FLAG_VOIP_RX)) { |
| 747 | output = mpClientInterface->openOutput(profile->mModule->mHandle, |
| 748 | &desc->mDevice, |
| 749 | &desc->mSamplingRate, |
| 750 | &desc->mFormat, |
| 751 | &desc->mChannelMask, |
| 752 | &desc->mLatency, |
| 753 | desc->mFlags); |
| 754 | } |
| 755 | if (output != 0) { |
| 756 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 757 | String8 reply; |
| 758 | char *value; |
| 759 | if (profile->mSamplingRates[0] == 0) { |
| 760 | reply = mpClientInterface->getParameters(output, |
| 761 | String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)); |
| 762 | ALOGV("checkOutputsForDevice() direct output sup sampling rates %s", |
| 763 | reply.string()); |
| 764 | value = strpbrk((char *)reply.string(), "="); |
| 765 | if (value != NULL) { |
| 766 | loadSamplingRates(value, profile); |
| 767 | } |
| 768 | } |
| 769 | if (profile->mFormats[0] == 0) { |
| 770 | reply = mpClientInterface->getParameters(output, |
| 771 | String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS)); |
| 772 | ALOGV("checkOutputsForDevice() direct output sup formats %s", |
| 773 | reply.string()); |
| 774 | value = strpbrk((char *)reply.string(), "="); |
| 775 | if (value != NULL) { |
| 776 | loadFormats(value, profile); |
| 777 | } |
| 778 | } |
| 779 | if (profile->mChannelMasks[0] == 0) { |
| 780 | reply = mpClientInterface->getParameters(output, |
| 781 | String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS)); |
| 782 | ALOGV("checkOutputsForDevice() direct output sup channel masks %s", |
| 783 | reply.string()); |
| 784 | value = strpbrk((char *)reply.string(), "="); |
| 785 | if (value != NULL) { |
| 786 | loadOutChannels(value + 1, profile); |
| 787 | } |
| 788 | } |
| 789 | if (((profile->mSamplingRates[0] == 0) && |
| 790 | (profile->mSamplingRates.size() < 2)) || |
| 791 | ((profile->mFormats[0] == 0) && |
| 792 | (profile->mFormats.size() < 2)) || |
| 793 | ((profile->mFormats[0] == 0) && |
| 794 | (profile->mChannelMasks.size() < 2))) { |
| 795 | ALOGW("checkOutputsForDevice() direct output missing param"); |
| 796 | output = 0; |
| 797 | } else { |
| 798 | addOutput(output, desc); |
| 799 | } |
| 800 | } else { |
| 801 | audio_io_handle_t duplicatedOutput = 0; |
| 802 | // add output descriptor |
| 803 | addOutput(output, desc); |
| 804 | // set initial stream volume for device |
| 805 | applyStreamVolumes(output, device, 0, true); |
| 806 | |
| 807 | //TODO: configure audio effect output stage here |
| 808 | |
| 809 | // open a duplicating output thread for the new output and the primary output |
| 810 | duplicatedOutput = mpClientInterface->openDuplicateOutput(output, |
| 811 | mPrimaryOutput); |
| 812 | if (duplicatedOutput != 0) { |
| 813 | // add duplicated output descriptor |
| 814 | AudioOutputDescriptor *dupOutputDesc = new AudioOutputDescriptor(NULL); |
| 815 | dupOutputDesc->mOutput1 = mOutputs.valueFor(mPrimaryOutput); |
| 816 | dupOutputDesc->mOutput2 = mOutputs.valueFor(output); |
| 817 | dupOutputDesc->mSamplingRate = desc->mSamplingRate; |
| 818 | dupOutputDesc->mFormat = desc->mFormat; |
| 819 | dupOutputDesc->mChannelMask = desc->mChannelMask; |
| 820 | dupOutputDesc->mLatency = desc->mLatency; |
| 821 | addOutput(duplicatedOutput, dupOutputDesc); |
| 822 | applyStreamVolumes(duplicatedOutput, device, 0, true); |
| 823 | } else { |
| 824 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", |
| 825 | mPrimaryOutput, output); |
| 826 | mpClientInterface->closeOutput(output); |
| 827 | mOutputs.removeItem(output); |
| 828 | output = 0; |
| 829 | } |
| 830 | } |
| 831 | } |
| 832 | if (output == 0) { |
| 833 | ALOGW("checkOutputsForDevice() could not open output for device %x", device); |
| 834 | delete desc; |
| 835 | profiles.removeAt(profile_index); |
| 836 | profile_index--; |
| 837 | } else { |
| 838 | outputs.add(output); |
| 839 | ALOGV("checkOutputsForDevice(): adding output %d", output); |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | if (profiles.isEmpty()) { |
| 844 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 845 | return BAD_VALUE; |
| 846 | } |
| 847 | } else { |
| 848 | // check if one opened output is not needed any more after disconnecting one device |
| 849 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 850 | desc = mOutputs.valueAt(i); |
| 851 | if (!desc->isDuplicated() && |
| 852 | !(desc->mProfile->mSupportedDevices & mAvailableOutputDevices)) { |
| 853 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", mOutputs.keyAt(i)); |
| 854 | outputs.add(mOutputs.keyAt(i)); |
| 855 | } |
| 856 | } |
| 857 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 858 | { |
| 859 | if (mHwModules[i]->mHandle == 0) { |
| 860 | continue; |
| 861 | } |
| 862 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 863 | { |
| 864 | IOProfile *profile = mHwModules[i]->mOutputProfiles[j]; |
| 865 | if ((profile->mSupportedDevices & device) && |
| 866 | (profile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) { |
| 867 | ALOGV("checkOutputsForDevice(): clearing direct output profile %d on module %d", |
| 868 | j, i); |
| 869 | if (profile->mSamplingRates[0] == 0) { |
| 870 | profile->mSamplingRates.clear(); |
| 871 | profile->mSamplingRates.add(0); |
| 872 | } |
| 873 | if (profile->mFormats[0] == 0) { |
| 874 | profile->mFormats.clear(); |
| 875 | profile->mFormats.add((audio_format_t)0); |
| 876 | } |
| 877 | if (profile->mChannelMasks[0] == 0) { |
| 878 | profile->mChannelMasks.clear(); |
| 879 | profile->mChannelMasks.add((audio_channel_mask_t)0); |
| 880 | } |
| 881 | } |
| 882 | } |
| 883 | } |
| 884 | } |
| 885 | return NO_ERROR; |
| 886 | } |
| 887 | status_t AudioPolicyManager::startOutput(audio_io_handle_t output, |
| 888 | AudioSystem::stream_type stream, |
| 889 | int session) |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 890 | { |
| 891 | ALOGV("startOutput() output %d, stream %d, session %d", output, stream, session); |
| 892 | ssize_t index = mOutputs.indexOfKey(output); |
| 893 | if (index < 0) { |
| 894 | ALOGW("startOutput() unknow output %d", output); |
| 895 | return BAD_VALUE; |
| 896 | } |
| 897 | |
| 898 | AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index); |
| 899 | |
| 900 | // increment usage count for this stream on the requested output: |
| 901 | // NOTE that the usage count is the same for duplicated output and hardware output which is |
| 902 | // necessary for a correct control of hardware output routing by startOutput() and stopOutput() |
| 903 | outputDesc->changeRefCount(stream, 1); |
| 904 | |
| 905 | if (outputDesc->mRefCount[stream] == 1) { |
| 906 | audio_devices_t prevDevice = outputDesc->device(); |
| 907 | audio_devices_t newDevice = AudioPolicyManagerBase::getNewDevice(output, false /*fromCache*/); |
| 908 | routing_strategy strategy = AudioPolicyManagerBase::getStrategy(stream); |
| 909 | bool shouldWait = (strategy == STRATEGY_SONIFICATION) || |
| 910 | (strategy == STRATEGY_SONIFICATION_RESPECTFUL); |
| 911 | uint32_t waitMs = 0; |
| 912 | bool force = false; |
| 913 | uint32_t muteWaitMs; |
| 914 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 915 | AudioOutputDescriptor *desc = mOutputs.valueAt(i); |
| 916 | if (desc != outputDesc) { |
| 917 | // force a device change if any other output is managed by the same hw |
| 918 | // module and has a current device selection that differs from selected device. |
| 919 | // In this case, the audio HAL must receive the new device selection so that it can |
| 920 | // change the device currently selected by the other active output. |
| 921 | if (outputDesc->sharesHwModuleWith(desc) && |
| 922 | desc->device() != newDevice) { |
| 923 | force = true; |
| 924 | } |
| 925 | // wait for audio on other active outputs to be presented when starting |
| 926 | // a notification so that audio focus effect can propagate. |
| 927 | if (shouldWait && (desc->refCount() != 0) && (waitMs < desc->latency())) { |
| 928 | waitMs = desc->latency(); |
| 929 | } |
| 930 | } |
| 931 | } |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 932 | #ifdef QCOM_FM_ENABLED |
| 933 | if(stream == AudioSystem::FM && output == getA2dpOutput()) { |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 934 | setOutputDevice(output, AudioPolicyManagerBase::getNewDevice((output), true)); |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 935 | } else |
| 936 | #endif |
| 937 | { |
| 938 | setOutputDevice(output, AudioPolicyManagerBase::getNewDevice((output), true)); |
| 939 | } |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 940 | |
| 941 | // handle special case for sonification while in call |
| 942 | if (isInCall()) { |
| 943 | AudioPolicyManagerBase::handleIncallSonification(stream, true, false); |
| 944 | } |
| 945 | |
| 946 | // apply volume rules for current stream and device if necessary |
| 947 | checkAndSetVolume(stream, |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 948 | mStreams[stream].getVolumeIndex(newDevice), |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 949 | output, |
| 950 | newDevice); |
| 951 | |
| 952 | // update the outputs if starting an output with a stream that can affect notification |
| 953 | // routing |
| 954 | handleNotificationRoutingForStream(stream); |
| 955 | if (waitMs > muteWaitMs) { |
| 956 | usleep((waitMs - muteWaitMs) * 2 * 1000); |
| 957 | } |
| 958 | } |
| 959 | return NO_ERROR; |
| 960 | } |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 961 | status_t AudioPolicyManager::stopOutput(audio_io_handle_t output, AudioSystem::stream_type stream, int session) |
| 962 | { |
| 963 | ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session); |
| 964 | ssize_t index = mOutputs.indexOfKey(output); |
| 965 | if (index < 0) { |
| 966 | ALOGW("stopOutput() unknow output %d", output); |
| 967 | return BAD_VALUE; |
| 968 | } |
| 969 | |
| 970 | AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index); |
| 971 | |
| 972 | // handle special case for sonification while in call |
| 973 | if (isInCall()) { |
| 974 | handleIncallSonification(stream, false, false); |
| 975 | } |
| 976 | |
| 977 | if (outputDesc->mRefCount[stream] > 0) { |
| 978 | // decrement usage count of this stream on the output |
| 979 | outputDesc->changeRefCount(stream, -1); |
| 980 | // store time at which the stream was stopped - see isStreamActive() |
| 981 | if (outputDesc->mRefCount[stream] == 0) { |
| 982 | if (stream == AudioSystem::MUSIC) { |
| 983 | outputDesc->mStopTime[stream] = systemTime(); |
| 984 | } |
| 985 | audio_devices_t newDevice = getNewDevice(output, false /*fromCache*/); |
| 986 | // delay the device switch by twice the latency because stopOutput() is executed when |
| 987 | // the track stop() command is received and at that time the audio track buffer can |
| 988 | // still contain data that needs to be drained. The latency only covers the audio HAL |
| 989 | // and kernel buffers. Also the latency does not always include additional delay in the |
| 990 | // audio path (audio DSP, CODEC ...) |
| 991 | setOutputDevice(output, newDevice, false, outputDesc->mLatency*2); |
| 992 | |
| 993 | // force restoring the device selection on other active outputs if it differs from the |
| 994 | // one being selected for this output |
| 995 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 996 | audio_io_handle_t curOutput = mOutputs.keyAt(i); |
| 997 | AudioOutputDescriptor *desc = mOutputs.valueAt(i); |
| 998 | if (curOutput != output && |
| 999 | desc->refCount() != 0 && |
| 1000 | outputDesc->sharesHwModuleWith(desc) && |
| 1001 | newDevice != desc->device()) { |
| 1002 | setOutputDevice(curOutput, |
| 1003 | getNewDevice(curOutput, false /*fromCache*/), |
| 1004 | true, |
| 1005 | outputDesc->mLatency*2); |
| 1006 | } |
| 1007 | } |
| 1008 | // update the outputs if stopping one with a stream that can affect notification routing |
| 1009 | handleNotificationRoutingForStream(stream); |
| 1010 | } |
| 1011 | return NO_ERROR; |
| 1012 | } else { |
| 1013 | ALOGW("stopOutput() refcount is already 0 for output %d", output); |
| 1014 | return INVALID_OPERATION; |
| 1015 | } |
| 1016 | } |
| 1017 | |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 1018 | // ---------------------------------------------------------------------------- |
| 1019 | // AudioPolicyManager |
| 1020 | // ---------------------------------------------------------------------------- |
| 1021 | |
| 1022 | // --- class factory |
| 1023 | |
| 1024 | |
| 1025 | extern "C" AudioPolicyInterface* createAudioPolicyManager(AudioPolicyClientInterface *clientInterface) |
| 1026 | { |
| 1027 | return new AudioPolicyManager(clientInterface); |
| 1028 | } |
| 1029 | |
| 1030 | extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface) |
| 1031 | { |
| 1032 | delete interface; |
| 1033 | } |
| 1034 | |
| 1035 | // --- |
| 1036 | |
| 1037 | uint32_t AudioPolicyManager::setOutputDevice(audio_io_handle_t output, audio_devices_t device, bool force, int delayMs) |
| 1038 | { |
| 1039 | ALOGV("setOutputDevice() output %d device %04x delayMs %d", output, device, delayMs); |
| 1040 | AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output); |
| 1041 | AudioParameter param; |
| 1042 | uint32_t muteWaitMs = 0; |
| 1043 | |
| 1044 | if (outputDesc->isDuplicated()) { |
| 1045 | muteWaitMs = setOutputDevice(outputDesc->mOutput1->mId, device, force, delayMs); |
| 1046 | muteWaitMs += setOutputDevice(outputDesc->mOutput2->mId, device, force, delayMs); |
| 1047 | return muteWaitMs; |
| 1048 | } |
| 1049 | // filter devices according to output selected |
| 1050 | device = (audio_devices_t)(device & outputDesc->mProfile->mSupportedDevices); |
| 1051 | |
| 1052 | audio_devices_t prevDevice = outputDesc->mDevice; |
| 1053 | |
| 1054 | ALOGV("setOutputDevice() prevDevice %04x", prevDevice); |
| 1055 | |
| 1056 | if (device != 0) { |
| 1057 | outputDesc->mDevice = device; |
| 1058 | } |
| 1059 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs); |
| 1060 | |
| 1061 | // Do not change the routing if: |
| 1062 | // - the requested device is 0 |
| 1063 | // - the requested device is the same as current device and force is not specified. |
| 1064 | // Doing this check here allows the caller to call setOutputDevice() without conditions |
| 1065 | if ((device == 0 || device == prevDevice) && !force) { |
| 1066 | ALOGV("setOutputDevice() setting same device %04x or null device for output %d", device, output); |
| 1067 | return muteWaitMs; |
| 1068 | } |
| 1069 | |
| 1070 | ALOGV("setOutputDevice() changing device"); |
| 1071 | // do the routing |
| 1072 | param.addInt(String8(AudioParameter::keyRouting), (int)device); |
| 1073 | mpClientInterface->setParameters(output, param.toString(), delayMs); |
| 1074 | |
| 1075 | // update stream volumes according to new device |
| 1076 | applyStreamVolumes(output, device, delayMs); |
| 1077 | |
| 1078 | return muteWaitMs; |
| 1079 | } |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 1080 | status_t AudioPolicyManager::checkAndSetVolume(int stream, int index, audio_io_handle_t output, audio_devices_t device, int delayMs, bool force) |
| 1081 | { |
| 1082 | // do not change actual stream volume if the stream is muted |
| 1083 | if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) { |
| 1084 | ALOGVV("checkAndSetVolume() stream %d muted count %d", |
| 1085 | stream, mOutputs.valueFor(output)->mMuteCount[stream]); |
| 1086 | return NO_ERROR; |
| 1087 | } |
| 1088 | |
| 1089 | // do not change in call volume if bluetooth is connected and vice versa |
| 1090 | if ((stream == AudioSystem::VOICE_CALL && mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) || |
| 1091 | (stream == AudioSystem::BLUETOOTH_SCO && mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO)) { |
| 1092 | ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm", |
| 1093 | stream, mForceUse[AudioSystem::FOR_COMMUNICATION]); |
| 1094 | return INVALID_OPERATION; |
| 1095 | } |
| 1096 | |
| 1097 | float volume = computeVolume(stream, index, output, device); |
| 1098 | // We actually change the volume if: |
| 1099 | // - the float value returned by computeVolume() changed |
| 1100 | // - the force flag is set |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 1101 | if (volume != mOutputs.valueFor(output)->mCurVolume[stream] || |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 1102 | #ifdef QCOM_FM_ENABLED |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 1103 | (stream == AudioSystem::FM) || |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 1104 | #endif |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 1105 | force) { |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 1106 | mOutputs.valueFor(output)->mCurVolume[stream] = volume; |
| 1107 | ALOGVV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs); |
| 1108 | if (stream == AudioSystem::VOICE_CALL || |
| 1109 | stream == AudioSystem::DTMF || |
| 1110 | stream == AudioSystem::BLUETOOTH_SCO) { |
| 1111 | float voiceVolume = -1.0; |
| 1112 | // offset value to reflect actual hardware volume that never reaches 0 |
| 1113 | // 1% corresponds roughly to first step in VOICE_CALL stream volume setting (see AudioService.java) |
| 1114 | volume = 0.01 + 0.99 * volume; |
| 1115 | if (stream == AudioSystem::VOICE_CALL) { |
| 1116 | voiceVolume = (float)index/(float)mStreams[stream].mIndexMax; |
| 1117 | } else if (stream == AudioSystem::BLUETOOTH_SCO) { |
| 1118 | voiceVolume = 1.0; |
| 1119 | } |
| 1120 | if (voiceVolume >= 0 && output == mPrimaryOutput) { |
| 1121 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); |
| 1122 | } |
| 1123 | #ifdef QCOM_FM_ENABLED |
| 1124 | } else if (stream == AudioSystem::FM) { |
| 1125 | float fmVolume = -1.0; |
| 1126 | fmVolume = computeVolume(stream, index, output, device); |
| 1127 | if (fmVolume >= 0) { |
| 1128 | if(output == mPrimaryOutput) |
| 1129 | mpClientInterface->setFmVolume(fmVolume, delayMs); |
| 1130 | else if(mHasA2dp && output == getA2dpOutput()) |
| 1131 | mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream, volume, output, delayMs); |
| 1132 | } |
| 1133 | return NO_ERROR; |
| 1134 | #endif |
| 1135 | } |
| 1136 | mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream, volume, output, delayMs); |
| 1137 | } |
| 1138 | return NO_ERROR; |
| 1139 | } |
| 1140 | |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 1141 | audio_io_handle_t AudioPolicyManager::getInput(int inputSource, |
| 1142 | uint32_t samplingRate, |
| 1143 | uint32_t format, |
| 1144 | uint32_t channelMask, |
| 1145 | AudioSystem::audio_in_acoustics acoustics) |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 1146 | { |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 1147 | audio_io_handle_t input = 0; |
| 1148 | audio_devices_t device = getDeviceForInputSource(inputSource); |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 1149 | |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 1150 | ALOGV("getInput() inputSource %d, samplingRate %d, format %d, channelMask %x, acoustics %x", |
| 1151 | inputSource, samplingRate, format, channelMask, acoustics); |
| 1152 | |
| 1153 | if (device == AUDIO_DEVICE_NONE) { |
| 1154 | ALOGW("getInput() could not find device for inputSource %d", inputSource); |
| 1155 | return 0; |
| 1156 | } |
| 1157 | |
| 1158 | // adapt channel selection to input source |
| 1159 | switch(inputSource) { |
| 1160 | case AUDIO_SOURCE_VOICE_UPLINK: |
| 1161 | channelMask = AudioSystem::CHANNEL_IN_VOICE_UPLINK; |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 1162 | break; |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 1163 | case AUDIO_SOURCE_VOICE_DOWNLINK: |
| 1164 | channelMask = AudioSystem::CHANNEL_IN_VOICE_DNLINK; |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 1165 | break; |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 1166 | case AUDIO_SOURCE_VOICE_CALL: |
| 1167 | channelMask = (AudioSystem::CHANNEL_IN_VOICE_UPLINK | AudioSystem::CHANNEL_IN_VOICE_DNLINK); |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 1168 | break; |
| 1169 | default: |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 1170 | break; |
| 1171 | } |
| 1172 | |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 1173 | IOProfile *profile = getInputProfile(device, |
| 1174 | samplingRate, |
| 1175 | format, |
| 1176 | channelMask); |
| 1177 | if (profile == NULL) { |
| 1178 | ALOGW("getInput() could not find profile for device %04x, samplingRate %d, format %d," |
| 1179 | "channelMask %04x", |
| 1180 | device, samplingRate, format, channelMask); |
| 1181 | return 0; |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 1182 | } |
| 1183 | |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 1184 | if (profile->mModule->mHandle == 0) { |
| 1185 | ALOGE("getInput(): HW module %s not opened", profile->mModule->mName); |
| 1186 | return 0; |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 1187 | } |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 1188 | |
| 1189 | AudioInputDescriptor *inputDesc = new AudioInputDescriptor(profile); |
| 1190 | |
| 1191 | inputDesc->mInputSource = inputSource; |
| 1192 | inputDesc->mDevice = device; |
| 1193 | inputDesc->mSamplingRate = samplingRate; |
| 1194 | inputDesc->mFormat = (audio_format_t)format; |
| 1195 | inputDesc->mChannelMask = (audio_channel_mask_t)channelMask; |
| 1196 | inputDesc->mRefCount = 0; |
| 1197 | input = mpClientInterface->openInput(profile->mModule->mHandle, |
| 1198 | &inputDesc->mDevice, |
| 1199 | &inputDesc->mSamplingRate, |
| 1200 | &inputDesc->mFormat, |
| 1201 | &inputDesc->mChannelMask); |
| 1202 | |
| 1203 | // only accept input with the exact requested set of parameters |
| 1204 | if (input == 0 || |
| 1205 | (samplingRate != inputDesc->mSamplingRate) || |
| 1206 | (format != inputDesc->mFormat) || |
| 1207 | (channelMask != inputDesc->mChannelMask)) { |
| 1208 | ALOGV("getInput() failed opening input: samplingRate %d, format %d, channelMask %d", |
| 1209 | samplingRate, format, channelMask); |
| 1210 | if (input != 0) { |
| 1211 | mpClientInterface->closeInput(input); |
| 1212 | } |
| 1213 | delete inputDesc; |
| 1214 | return 0; |
| 1215 | } |
| 1216 | mInputs.add(input, inputDesc); |
| 1217 | return input; |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 1218 | } |
| 1219 | |
| 1220 | audio_devices_t AudioPolicyManager::getDeviceForInputSource(int inputSource) |
| 1221 | { |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 1222 | uint32_t device = AUDIO_DEVICE_NONE; |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 1223 | |
| 1224 | switch(inputSource) { |
| 1225 | case AUDIO_SOURCE_DEFAULT: |
| 1226 | case AUDIO_SOURCE_MIC: |
| 1227 | case AUDIO_SOURCE_VOICE_RECOGNITION: |
| 1228 | if (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO && |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 1229 | mAvailableInputDevices & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) { |
| 1230 | device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET; |
| 1231 | } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_WIRED_HEADSET) { |
| 1232 | device = AUDIO_DEVICE_IN_WIRED_HEADSET; |
| 1233 | } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) { |
| 1234 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 1235 | } |
| 1236 | break; |
| 1237 | case AUDIO_SOURCE_VOICE_COMMUNICATION: |
| 1238 | device = AudioSystem::DEVICE_IN_COMMUNICATION; |
| 1239 | break; |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 1240 | |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 1241 | case AUDIO_SOURCE_CAMCORDER: |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 1242 | if (mAvailableInputDevices & AUDIO_DEVICE_IN_BACK_MIC) { |
| 1243 | device = AUDIO_DEVICE_IN_BACK_MIC; |
| 1244 | } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) { |
| 1245 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 1246 | } |
| 1247 | break; |
| 1248 | case AUDIO_SOURCE_VOICE_UPLINK: |
| 1249 | case AUDIO_SOURCE_VOICE_DOWNLINK: |
| 1250 | case AUDIO_SOURCE_VOICE_CALL: |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 1251 | if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) { |
| 1252 | device = AUDIO_DEVICE_IN_VOICE_CALL; |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 1253 | } |
| 1254 | break; |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 1255 | case AUDIO_SOURCE_REMOTE_SUBMIX: |
| 1256 | if (mAvailableInputDevices & AUDIO_DEVICE_IN_REMOTE_SUBMIX) { |
| 1257 | device = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 1258 | } |
| 1259 | break; |
| 1260 | |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 1261 | #ifdef QCOM_FM_ENABLED |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 1262 | case AUDIO_SOURCE_FM_RX: |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 1263 | device = AudioSystem::DEVICE_IN_FM_RX; |
| 1264 | break; |
| 1265 | case AUDIO_SOURCE_FM_RX_A2DP: |
| 1266 | device = AudioSystem::DEVICE_IN_FM_RX_A2DP; |
| 1267 | break; |
| 1268 | #endif |
| 1269 | default: |
| 1270 | ALOGW("getDeviceForInputSource() invalid input source %d", inputSource); |
| 1271 | break; |
| 1272 | } |
| 1273 | ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device); |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 1274 | return device; |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 1275 | } |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 1276 | /* |
| 1277 | Overwriting this function from base class to allow 2 acitve AudioRecord clients in case of FM. |
| 1278 | One for FM A2DP playbck and other for FM recording. |
| 1279 | */ |
| 1280 | status_t AudioPolicyManager::startInput(audio_io_handle_t input) |
| 1281 | { |
| 1282 | ALOGV("startInput() input %d", input); |
| 1283 | ssize_t index = mInputs.indexOfKey(input); |
| 1284 | if (index < 0) { |
| 1285 | ALOGW("startInput() unknow input %d", input); |
| 1286 | return BAD_VALUE; |
| 1287 | } |
| 1288 | AudioInputDescriptor *inputDesc = mInputs.valueAt(index); |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 1289 | |
| 1290 | #ifdef AUDIO_POLICY_TEST |
| 1291 | if (mTestInput == 0) |
| 1292 | #endif //AUDIO_POLICY_TEST |
| 1293 | { |
| 1294 | // refuse 2 active AudioRecord clients at the same time |
| 1295 | if (getActiveInput() != 0) { |
| 1296 | ALOGW("startInput() input %d failed: other input already started", input); |
| 1297 | return INVALID_OPERATION; |
| 1298 | } |
| 1299 | } |
| 1300 | |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 1301 | AudioParameter param = AudioParameter(); |
| 1302 | param.addInt(String8(AudioParameter::keyRouting), (int)inputDesc->mDevice); |
| 1303 | // use Voice Recognition mode or not for this input based on input source |
| 1304 | int vr_enabled = inputDesc->mInputSource == AUDIO_SOURCE_VOICE_RECOGNITION ? 1 : 0; |
| 1305 | param.addInt(String8("vr_mode"), vr_enabled); |
| 1306 | ALOGV("AudioPolicyManager::startInput(%d), setting vr_mode to %d", inputDesc->mInputSource, vr_enabled); |
| 1307 | mpClientInterface->setParameters(input, param.toString()); |
| 1308 | inputDesc->mRefCount = 1; |
| 1309 | return NO_ERROR; |
| 1310 | } |
| 1311 | |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 1312 | void AudioPolicyManager::handleNotificationRoutingForStream(AudioSystem::stream_type stream) { |
| 1313 | switch(stream) { |
| 1314 | case AudioSystem::MUSIC: |
| 1315 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
| 1316 | updateDevicesAndOutputs(); |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 1317 | break; |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 1318 | default: |
| 1319 | break; |
| 1320 | } |
Steve Kondik | d149f40 | 2013-01-26 02:10:07 -0800 | [diff] [blame] | 1321 | } |
| 1322 | |
| 1323 | |
Steve Kondik | bbd2939 | 2013-01-25 23:31:23 -0800 | [diff] [blame] | 1324 | }; // namespace android_audio_legacy |