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 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 17 | #define LOG_TAG "APM::AudioPolicyManager" |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 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 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 27 | #include <inttypes.h> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 28 | #include <math.h> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 29 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 30 | #include <cutils/properties.h> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 31 | #include <utils/Log.h> |
| 32 | #include <hardware/audio.h> |
| 33 | #include <hardware/audio_effect.h> |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 34 | #include <media/AudioParameter.h> |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 35 | #include <media/AudioPolicyHelper.h> |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 36 | #include <soundtrigger/SoundTrigger.h> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 37 | #include "AudioPolicyManager.h" |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 38 | #include "audio_policy_conf.h" |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 39 | #include <ConfigParsingUtils.h> |
| 40 | #include <policy.h> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 41 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 42 | namespace android { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 43 | |
| 44 | // ---------------------------------------------------------------------------- |
| 45 | // AudioPolicyInterface implementation |
| 46 | // ---------------------------------------------------------------------------- |
| 47 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 48 | status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 49 | audio_policy_dev_state_t state, |
| 50 | const char *device_address, |
| 51 | const char *device_name) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 52 | { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 53 | return setDeviceConnectionStateInt(device, state, device_address, device_name); |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device, |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 57 | audio_policy_dev_state_t state, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 58 | const char *device_address, |
| 59 | const char *device_name) |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 60 | { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 61 | ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s", |
| 62 | - device, state, device_address, device_name); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 63 | |
| 64 | // connect/disconnect only 1 device at a time |
| 65 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; |
| 66 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 67 | sp<DeviceDescriptor> devDesc = |
| 68 | mHwModules.getDeviceDescriptor(device, device_address, device_name); |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 69 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 70 | // handle output devices |
| 71 | if (audio_is_output_device(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 72 | SortedVector <audio_io_handle_t> outputs; |
| 73 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 74 | ssize_t index = mAvailableOutputDevices.indexOf(devDesc); |
| 75 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 76 | // save a copy of the opened output descriptors before any output is opened or closed |
| 77 | // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies() |
| 78 | mPreviousOutputs = mOutputs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 79 | switch (state) |
| 80 | { |
| 81 | // handle output device connection |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 82 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 83 | if (index >= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 84 | ALOGW("setDeviceConnectionState() device already connected: %x", device); |
| 85 | return INVALID_OPERATION; |
| 86 | } |
| 87 | ALOGV("setDeviceConnectionState() connecting device %x", device); |
| 88 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 89 | // register new device as available |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 90 | index = mAvailableOutputDevices.add(devDesc); |
| 91 | if (index >= 0) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 92 | sp<HwModule> module = mHwModules.getModuleForDevice(device); |
Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 93 | if (module == 0) { |
| 94 | ALOGD("setDeviceConnectionState() could not find HW module for device %08x", |
| 95 | device); |
| 96 | mAvailableOutputDevices.remove(devDesc); |
| 97 | return INVALID_OPERATION; |
| 98 | } |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 99 | mAvailableOutputDevices[index]->attach(module); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 100 | } else { |
| 101 | return NO_MEMORY; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 104 | if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 105 | mAvailableOutputDevices.remove(devDesc); |
| 106 | return INVALID_OPERATION; |
| 107 | } |
| 108 | // outputs should never be empty here |
| 109 | ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():" |
| 110 | "checkOutputsForDevice() returned no outputs but status OK"); |
| 111 | ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs", |
| 112 | outputs.size()); |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 113 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 114 | // Send connect to HALs |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 115 | AudioParameter param = AudioParameter(devDesc->mAddress); |
| 116 | param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device); |
| 117 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 118 | |
| 119 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 120 | // handle output device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 121 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 122 | if (index < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 123 | ALOGW("setDeviceConnectionState() device not connected: %x", device); |
| 124 | return INVALID_OPERATION; |
| 125 | } |
| 126 | |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 127 | ALOGV("setDeviceConnectionState() disconnecting output device %x", device); |
| 128 | |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 129 | // Send Disconnect to HALs |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 130 | AudioParameter param = AudioParameter(devDesc->mAddress); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 131 | param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device); |
| 132 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 133 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 134 | // remove device from available output devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 135 | mAvailableOutputDevices.remove(devDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 136 | |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 137 | checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 138 | } break; |
| 139 | |
| 140 | default: |
| 141 | ALOGE("setDeviceConnectionState() invalid state: %x", state); |
| 142 | return BAD_VALUE; |
| 143 | } |
| 144 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 145 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP |
| 146 | // output is suspended before any tracks are moved to it |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 147 | checkA2dpSuspend(); |
| 148 | checkOutputForAllStrategies(); |
| 149 | // outputs must be closed after checkOutputForAllStrategies() is executed |
| 150 | if (!outputs.isEmpty()) { |
| 151 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 152 | sp<AudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 153 | // close unused outputs after device disconnection or direct outputs that have been |
| 154 | // opened by checkOutputsForDevice() to query dynamic parameters |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 155 | if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 156 | (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) && |
| 157 | (desc->mDirectOpenCount == 0))) { |
| 158 | closeOutput(outputs[i]); |
| 159 | } |
| 160 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 161 | // check again after closing A2DP output to reset mA2dpSuspended if needed |
| 162 | checkA2dpSuspend(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | updateDevicesAndOutputs(); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 166 | if (mPhoneState == AUDIO_MODE_IN_CALL) { |
| 167 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 168 | updateCallRouting(newDevice); |
| 169 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 170 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 171 | audio_io_handle_t output = mOutputs.keyAt(i); |
| 172 | if ((mPhoneState != AUDIO_MODE_IN_CALL) || (output != mPrimaryOutput)) { |
| 173 | audio_devices_t newDevice = getNewOutputDevice(mOutputs.keyAt(i), |
| 174 | true /*fromCache*/); |
| 175 | // do not force device change on duplicated output because if device is 0, it will |
| 176 | // also force a device 0 for the two outputs it is duplicated to which may override |
| 177 | // a valid device selection on those outputs. |
| 178 | bool force = !mOutputs.valueAt(i)->isDuplicated() |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 179 | && (!device_distinguishes_on_address(device) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 180 | // always force when disconnecting (a non-duplicated device) |
| 181 | || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); |
| 182 | setOutputDevice(output, newDevice, force, 0); |
| 183 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Eric Laurent | 72aa32f | 2014-05-30 18:51:48 -0700 | [diff] [blame] | 186 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | b71e58b | 2014-05-29 16:08:11 -0700 | [diff] [blame] | 187 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 188 | } // end if is output device |
| 189 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 190 | // handle input devices |
| 191 | if (audio_is_input_device(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 192 | SortedVector <audio_io_handle_t> inputs; |
| 193 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 194 | ssize_t index = mAvailableInputDevices.indexOf(devDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 195 | switch (state) |
| 196 | { |
| 197 | // handle input device connection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 198 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 199 | if (index >= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 200 | ALOGW("setDeviceConnectionState() device already connected: %d", device); |
| 201 | return INVALID_OPERATION; |
| 202 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 203 | sp<HwModule> module = mHwModules.getModuleForDevice(device); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 204 | if (module == NULL) { |
| 205 | ALOGW("setDeviceConnectionState(): could not find HW module for device %08x", |
| 206 | device); |
| 207 | return INVALID_OPERATION; |
| 208 | } |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 209 | if (checkInputsForDevice(device, state, inputs, devDesc->mAddress) != NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 210 | return INVALID_OPERATION; |
| 211 | } |
| 212 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 213 | index = mAvailableInputDevices.add(devDesc); |
| 214 | if (index >= 0) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 215 | mAvailableInputDevices[index]->attach(module); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 216 | } else { |
| 217 | return NO_MEMORY; |
| 218 | } |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 219 | |
| 220 | // Set connect to HALs |
| 221 | AudioParameter param = AudioParameter(devDesc->mAddress); |
| 222 | param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device); |
| 223 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 224 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 225 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 226 | |
| 227 | // handle input device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 228 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 229 | if (index < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 230 | ALOGW("setDeviceConnectionState() device not connected: %d", device); |
| 231 | return INVALID_OPERATION; |
| 232 | } |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 233 | |
| 234 | ALOGV("setDeviceConnectionState() disconnecting input device %x", device); |
| 235 | |
| 236 | // Set Disconnect to HALs |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 237 | AudioParameter param = AudioParameter(devDesc->mAddress); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 238 | param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device); |
| 239 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 240 | |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 241 | checkInputsForDevice(device, state, inputs, devDesc->mAddress); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 242 | mAvailableInputDevices.remove(devDesc); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 243 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 244 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 245 | |
| 246 | default: |
| 247 | ALOGE("setDeviceConnectionState() invalid state: %x", state); |
| 248 | return BAD_VALUE; |
| 249 | } |
| 250 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 251 | closeAllInputs(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 252 | |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 253 | if (mPhoneState == AUDIO_MODE_IN_CALL) { |
| 254 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 255 | updateCallRouting(newDevice); |
| 256 | } |
| 257 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 258 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 259 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 260 | } // end if is input device |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 261 | |
| 262 | ALOGW("setDeviceConnectionState() invalid device: %x", device); |
| 263 | return BAD_VALUE; |
| 264 | } |
| 265 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 266 | audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 267 | const char *device_address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 268 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 269 | sp<DeviceDescriptor> devDesc = mHwModules.getDeviceDescriptor(device, device_address, ""); |
| 270 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 271 | DeviceVector *deviceVector; |
| 272 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 273 | if (audio_is_output_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 274 | deviceVector = &mAvailableOutputDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 275 | } else if (audio_is_input_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 276 | deviceVector = &mAvailableInputDevices; |
| 277 | } else { |
| 278 | ALOGW("getDeviceConnectionState() invalid device type %08x", device); |
| 279 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 280 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 281 | return deviceVector->getDeviceConnectionState(devDesc); |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 282 | } |
| 283 | |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 284 | void AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, int delayMs) |
| 285 | { |
| 286 | bool createTxPatch = false; |
| 287 | struct audio_patch patch; |
| 288 | patch.num_sources = 1; |
| 289 | patch.num_sinks = 1; |
| 290 | status_t status; |
| 291 | audio_patch_handle_t afPatchHandle; |
| 292 | DeviceVector deviceList; |
| 293 | |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 294 | audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 295 | ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice); |
| 296 | |
| 297 | // release existing RX patch if any |
| 298 | if (mCallRxPatch != 0) { |
| 299 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 300 | mCallRxPatch.clear(); |
| 301 | } |
| 302 | // release TX patch if any |
| 303 | if (mCallTxPatch != 0) { |
| 304 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 305 | mCallTxPatch.clear(); |
| 306 | } |
| 307 | |
| 308 | // If the RX device is on the primary HW module, then use legacy routing method for voice calls |
| 309 | // via setOutputDevice() on primary output. |
| 310 | // Otherwise, create two audio patches for TX and RX path. |
| 311 | if (availablePrimaryOutputDevices() & rxDevice) { |
| 312 | setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs); |
| 313 | // If the TX device is also on the primary HW module, setOutputDevice() will take care |
| 314 | // of it due to legacy implementation. If not, create a patch. |
| 315 | if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN) |
| 316 | == AUDIO_DEVICE_NONE) { |
| 317 | createTxPatch = true; |
| 318 | } |
| 319 | } else { |
| 320 | // create RX path audio patch |
| 321 | deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice); |
| 322 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 323 | "updateCallRouting() selected device not in output device list"); |
| 324 | sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0); |
| 325 | deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX); |
| 326 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 327 | "updateCallRouting() no telephony RX device"); |
| 328 | sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0); |
| 329 | |
| 330 | rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]); |
| 331 | rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]); |
| 332 | |
| 333 | // request to reuse existing output stream if one is already opened to reach the RX device |
| 334 | SortedVector<audio_io_handle_t> outputs = |
| 335 | getOutputsForDevice(rxDevice, mOutputs); |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 336 | audio_io_handle_t output = selectOutput(outputs, |
| 337 | AUDIO_OUTPUT_FLAG_NONE, |
| 338 | AUDIO_FORMAT_INVALID); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 339 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 340 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 341 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 342 | "updateCallRouting() RX device output is duplicated"); |
| 343 | outputDesc->toAudioPortConfig(&patch.sources[1]); |
| 344 | patch.num_sources = 2; |
| 345 | } |
| 346 | |
| 347 | afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 348 | status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0); |
| 349 | ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch", |
| 350 | status); |
| 351 | if (status == NO_ERROR) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 352 | mCallRxPatch = new AudioPatch(&patch, mUidCached); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 353 | mCallRxPatch->mAfPatchHandle = afPatchHandle; |
| 354 | mCallRxPatch->mUid = mUidCached; |
| 355 | } |
| 356 | createTxPatch = true; |
| 357 | } |
| 358 | if (createTxPatch) { |
| 359 | |
| 360 | struct audio_patch patch; |
| 361 | patch.num_sources = 1; |
| 362 | patch.num_sinks = 1; |
| 363 | deviceList = mAvailableInputDevices.getDevicesFromType(txDevice); |
| 364 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 365 | "updateCallRouting() selected device not in input device list"); |
| 366 | sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0); |
| 367 | txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]); |
| 368 | deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX); |
| 369 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 370 | "updateCallRouting() no telephony TX device"); |
| 371 | sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0); |
| 372 | txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]); |
| 373 | |
| 374 | SortedVector<audio_io_handle_t> outputs = |
| 375 | getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs); |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 376 | audio_io_handle_t output = selectOutput(outputs, |
| 377 | AUDIO_OUTPUT_FLAG_NONE, |
| 378 | AUDIO_FORMAT_INVALID); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 379 | // request to reuse existing output stream if one is already opened to reach the TX |
| 380 | // path output device |
| 381 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 382 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 383 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 384 | "updateCallRouting() RX device output is duplicated"); |
| 385 | outputDesc->toAudioPortConfig(&patch.sources[1]); |
| 386 | patch.num_sources = 2; |
| 387 | } |
| 388 | |
| 389 | afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 390 | status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0); |
| 391 | ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch", |
| 392 | status); |
| 393 | if (status == NO_ERROR) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 394 | mCallTxPatch = new AudioPatch(&patch, mUidCached); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 395 | mCallTxPatch->mAfPatchHandle = afPatchHandle; |
| 396 | mCallTxPatch->mUid = mUidCached; |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 401 | void AudioPolicyManager::setPhoneState(audio_mode_t state) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 402 | { |
| 403 | ALOGV("setPhoneState() state %d", state); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 404 | if (state < 0 || state >= AUDIO_MODE_CNT) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 405 | ALOGW("setPhoneState() invalid state %d", state); |
| 406 | return; |
| 407 | } |
| 408 | |
| 409 | if (state == mPhoneState ) { |
| 410 | ALOGW("setPhoneState() setting same state %d", state); |
| 411 | return; |
| 412 | } |
| 413 | |
| 414 | // if leaving call state, handle special case of active streams |
| 415 | // pertaining to sonification strategy see handleIncallSonification() |
| 416 | if (isInCall()) { |
| 417 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 418 | for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 419 | if (stream == AUDIO_STREAM_PATCH) { |
| 420 | continue; |
| 421 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 422 | handleIncallSonification((audio_stream_type_t)stream, false, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 423 | } |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 424 | |
| 425 | // force reevaluating accessibility routing when call starts |
| 426 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | // store previous phone state for management of sonification strategy below |
| 430 | int oldState = mPhoneState; |
| 431 | mPhoneState = state; |
| 432 | bool force = false; |
| 433 | |
| 434 | // are we entering or starting a call |
| 435 | if (!isStateInCall(oldState) && isStateInCall(state)) { |
| 436 | ALOGV(" Entering call in setPhoneState()"); |
| 437 | // force routing command to audio hardware when starting a call |
| 438 | // even if no device change is needed |
| 439 | force = true; |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 440 | for (int j = 0; j < ApmGains::DEVICE_CATEGORY_CNT; j++) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 441 | mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] = |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 442 | ApmGains::sVolumeProfiles[AUDIO_STREAM_VOICE_CALL][j]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 443 | } |
| 444 | } else if (isStateInCall(oldState) && !isStateInCall(state)) { |
| 445 | ALOGV(" Exiting call in setPhoneState()"); |
| 446 | // force routing command to audio hardware when exiting a call |
| 447 | // even if no device change is needed |
| 448 | force = true; |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 449 | for (int j = 0; j < ApmGains::DEVICE_CATEGORY_CNT; j++) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 450 | mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] = |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 451 | ApmGains::sVolumeProfiles[AUDIO_STREAM_DTMF][j]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 452 | } |
| 453 | } else if (isStateInCall(state) && (state != oldState)) { |
| 454 | ALOGV(" Switching between telephony and VoIP in setPhoneState()"); |
| 455 | // force routing command to audio hardware when switching between telephony and VoIP |
| 456 | // even if no device change is needed |
| 457 | force = true; |
| 458 | } |
| 459 | |
| 460 | // check for device and output changes triggered by new phone state |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 461 | checkA2dpSuspend(); |
| 462 | checkOutputForAllStrategies(); |
| 463 | updateDevicesAndOutputs(); |
| 464 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 465 | sp<AudioOutputDescriptor> hwOutputDesc = mOutputs.valueFor(mPrimaryOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 466 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 467 | int delayMs = 0; |
| 468 | if (isStateInCall(state)) { |
| 469 | nsecs_t sysTime = systemTime(); |
| 470 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 471 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 472 | // mute media and sonification strategies and delay device switch by the largest |
| 473 | // latency of any output where either strategy is active. |
| 474 | // This avoid sending the ring tone or music tail into the earpiece or headset. |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 475 | if ((isStrategyActive(desc, STRATEGY_MEDIA, |
| 476 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 477 | sysTime) || |
| 478 | isStrategyActive(desc, STRATEGY_SONIFICATION, |
| 479 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 480 | sysTime)) && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 481 | (delayMs < (int)desc->mLatency*2)) { |
| 482 | delayMs = desc->mLatency*2; |
| 483 | } |
| 484 | setStrategyMute(STRATEGY_MEDIA, true, mOutputs.keyAt(i)); |
| 485 | setStrategyMute(STRATEGY_MEDIA, false, mOutputs.keyAt(i), MUTE_TIME_MS, |
| 486 | getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/)); |
| 487 | setStrategyMute(STRATEGY_SONIFICATION, true, mOutputs.keyAt(i)); |
| 488 | setStrategyMute(STRATEGY_SONIFICATION, false, mOutputs.keyAt(i), MUTE_TIME_MS, |
| 489 | getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/)); |
| 490 | } |
| 491 | } |
| 492 | |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 493 | // Note that despite the fact that getNewOutputDevice() is called on the primary output, |
| 494 | // the device returned is not necessarily reachable via this output |
| 495 | audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 496 | // force routing command to audio hardware when ending call |
| 497 | // even if no device change is needed |
| 498 | if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) { |
| 499 | rxDevice = hwOutputDesc->device(); |
| 500 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 501 | |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 502 | if (state == AUDIO_MODE_IN_CALL) { |
| 503 | updateCallRouting(rxDevice, delayMs); |
| 504 | } else if (oldState == AUDIO_MODE_IN_CALL) { |
| 505 | if (mCallRxPatch != 0) { |
| 506 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 507 | mCallRxPatch.clear(); |
| 508 | } |
| 509 | if (mCallTxPatch != 0) { |
| 510 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 511 | mCallTxPatch.clear(); |
| 512 | } |
| 513 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
| 514 | } else { |
| 515 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
| 516 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 517 | // if entering in call state, handle special case of active streams |
| 518 | // pertaining to sonification strategy see handleIncallSonification() |
| 519 | if (isStateInCall(state)) { |
| 520 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 521 | for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 522 | if (stream == AUDIO_STREAM_PATCH) { |
| 523 | continue; |
| 524 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 525 | handleIncallSonification((audio_stream_type_t)stream, true, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
| 529 | // 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] | 530 | if (state == AUDIO_MODE_RINGTONE && |
| 531 | isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 532 | mLimitRingtoneVolume = true; |
| 533 | } else { |
| 534 | mLimitRingtoneVolume = false; |
| 535 | } |
| 536 | } |
| 537 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 538 | void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 539 | audio_policy_forced_cfg_t config) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 540 | { |
| 541 | ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState); |
| 542 | |
| 543 | bool forceVolumeReeval = false; |
| 544 | switch(usage) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 545 | case AUDIO_POLICY_FORCE_FOR_COMMUNICATION: |
| 546 | if (config != AUDIO_POLICY_FORCE_SPEAKER && config != AUDIO_POLICY_FORCE_BT_SCO && |
| 547 | config != AUDIO_POLICY_FORCE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 548 | ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config); |
| 549 | return; |
| 550 | } |
| 551 | forceVolumeReeval = true; |
| 552 | mForceUse[usage] = config; |
| 553 | break; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 554 | case AUDIO_POLICY_FORCE_FOR_MEDIA: |
| 555 | if (config != AUDIO_POLICY_FORCE_HEADPHONES && config != AUDIO_POLICY_FORCE_BT_A2DP && |
| 556 | config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY && |
| 557 | config != AUDIO_POLICY_FORCE_ANALOG_DOCK && |
| 558 | config != AUDIO_POLICY_FORCE_DIGITAL_DOCK && config != AUDIO_POLICY_FORCE_NONE && |
Hochi Huang | 327cb70 | 2014-09-21 09:47:31 +0800 | [diff] [blame] | 559 | config != AUDIO_POLICY_FORCE_NO_BT_A2DP && config != AUDIO_POLICY_FORCE_SPEAKER ) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 560 | ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config); |
| 561 | return; |
| 562 | } |
| 563 | mForceUse[usage] = config; |
| 564 | break; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 565 | case AUDIO_POLICY_FORCE_FOR_RECORD: |
| 566 | if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY && |
| 567 | config != AUDIO_POLICY_FORCE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 568 | ALOGW("setForceUse() invalid config %d for FOR_RECORD", config); |
| 569 | return; |
| 570 | } |
| 571 | mForceUse[usage] = config; |
| 572 | break; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 573 | case AUDIO_POLICY_FORCE_FOR_DOCK: |
| 574 | if (config != AUDIO_POLICY_FORCE_NONE && config != AUDIO_POLICY_FORCE_BT_CAR_DOCK && |
| 575 | config != AUDIO_POLICY_FORCE_BT_DESK_DOCK && |
| 576 | config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY && |
| 577 | config != AUDIO_POLICY_FORCE_ANALOG_DOCK && |
| 578 | config != AUDIO_POLICY_FORCE_DIGITAL_DOCK) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 579 | ALOGW("setForceUse() invalid config %d for FOR_DOCK", config); |
| 580 | } |
| 581 | forceVolumeReeval = true; |
| 582 | mForceUse[usage] = config; |
| 583 | break; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 584 | case AUDIO_POLICY_FORCE_FOR_SYSTEM: |
| 585 | if (config != AUDIO_POLICY_FORCE_NONE && |
| 586 | config != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 587 | ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config); |
| 588 | } |
| 589 | forceVolumeReeval = true; |
| 590 | mForceUse[usage] = config; |
| 591 | break; |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 592 | case AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO: |
| 593 | if (config != AUDIO_POLICY_FORCE_NONE && |
| 594 | config != AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED) { |
| 595 | ALOGW("setForceUse() invalid config %d forHDMI_SYSTEM_AUDIO", config); |
| 596 | } |
| 597 | mForceUse[usage] = config; |
| 598 | break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 599 | default: |
| 600 | ALOGW("setForceUse() invalid usage %d", usage); |
| 601 | break; |
| 602 | } |
| 603 | |
| 604 | // check for device and output changes triggered by new force usage |
| 605 | checkA2dpSuspend(); |
| 606 | checkOutputForAllStrategies(); |
| 607 | updateDevicesAndOutputs(); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 608 | if (mPhoneState == AUDIO_MODE_IN_CALL) { |
| 609 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/); |
| 610 | updateCallRouting(newDevice); |
| 611 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 612 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 613 | audio_io_handle_t output = mOutputs.keyAt(i); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 614 | audio_devices_t newDevice = getNewOutputDevice(output, true /*fromCache*/); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 615 | if ((mPhoneState != AUDIO_MODE_IN_CALL) || (output != mPrimaryOutput)) { |
| 616 | setOutputDevice(output, newDevice, (newDevice != AUDIO_DEVICE_NONE)); |
| 617 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 618 | if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) { |
| 619 | applyStreamVolumes(output, newDevice, 0, true); |
| 620 | } |
| 621 | } |
| 622 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 623 | audio_io_handle_t activeInput = mInputs.getActiveInput(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 624 | if (activeInput != 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 625 | setInputDevice(activeInput, getNewInputDevice(activeInput)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | } |
| 629 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 630 | audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 631 | { |
| 632 | return mForceUse[usage]; |
| 633 | } |
| 634 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 635 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 636 | { |
| 637 | ALOGV("setSystemProperty() property %s, value %s", property, value); |
| 638 | } |
| 639 | |
| 640 | // Find a direct output profile compatible with the parameters passed, even if the input flags do |
| 641 | // not explicitly request a direct output |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 642 | sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput( |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 643 | audio_devices_t device, |
| 644 | uint32_t samplingRate, |
| 645 | audio_format_t format, |
| 646 | audio_channel_mask_t channelMask, |
| 647 | audio_output_flags_t flags) |
| 648 | { |
| 649 | for (size_t i = 0; i < mHwModules.size(); i++) { |
| 650 | if (mHwModules[i]->mHandle == 0) { |
| 651 | continue; |
| 652 | } |
| 653 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 654 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 655 | bool found = profile->isCompatibleProfile(device, String8(""), samplingRate, |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 656 | NULL /*updatedSamplingRate*/, format, channelMask, |
| 657 | flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD ? |
| 658 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD : AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 659 | if (found && (mAvailableOutputDevices.types() & profile->mSupportedDevices.types())) { |
| 660 | return profile; |
| 661 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | return 0; |
| 665 | } |
| 666 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 667 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 668 | uint32_t samplingRate, |
| 669 | audio_format_t format, |
| 670 | audio_channel_mask_t channelMask, |
| 671 | audio_output_flags_t flags, |
| 672 | const audio_offload_info_t *offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 673 | { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 674 | routing_strategy strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 675 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 676 | ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x", |
| 677 | device, stream, samplingRate, format, channelMask, flags); |
| 678 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 679 | return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE, |
| 680 | stream, samplingRate,format, channelMask, |
| 681 | flags, offloadInfo); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 682 | } |
| 683 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 684 | status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, |
| 685 | audio_io_handle_t *output, |
| 686 | audio_session_t session, |
| 687 | audio_stream_type_t *stream, |
| 688 | uint32_t samplingRate, |
| 689 | audio_format_t format, |
| 690 | audio_channel_mask_t channelMask, |
| 691 | audio_output_flags_t flags, |
| 692 | const audio_offload_info_t *offloadInfo) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 693 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 694 | audio_attributes_t attributes; |
| 695 | if (attr != NULL) { |
| 696 | if (!isValidAttributes(attr)) { |
| 697 | ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]", |
| 698 | attr->usage, attr->content_type, attr->flags, |
| 699 | attr->tags); |
| 700 | return BAD_VALUE; |
| 701 | } |
| 702 | attributes = *attr; |
| 703 | } else { |
| 704 | if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) { |
| 705 | ALOGE("getOutputForAttr(): invalid stream type"); |
| 706 | return BAD_VALUE; |
| 707 | } |
| 708 | stream_type_to_audio_attributes(*stream, &attributes); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 709 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame^] | 710 | sp<AudioOutputDescriptor> desc; |
| 711 | if (mPolicyMixes.getOutputForAttr(attributes, desc) == NO_ERROR) { |
| 712 | ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr"); |
| 713 | if (!audio_is_linear_pcm(format)) { |
| 714 | return BAD_VALUE; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 715 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame^] | 716 | *stream = streamTypefromAttributesInt(&attributes); |
| 717 | *output = desc->mIoHandle; |
| 718 | ALOGV("getOutputForAttr() returns output %d", *output); |
| 719 | return NO_ERROR; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 720 | } |
| 721 | if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 722 | ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE"); |
| 723 | return BAD_VALUE; |
| 724 | } |
| 725 | |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 726 | ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x", |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 727 | attributes.usage, attributes.content_type, attributes.tags, attributes.flags); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 728 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 729 | routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 730 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 731 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 732 | if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) { |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 733 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
| 734 | } |
| 735 | |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 736 | ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x", |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 737 | device, samplingRate, format, channelMask, flags); |
| 738 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 739 | *stream = streamTypefromAttributesInt(&attributes); |
| 740 | *output = getOutputForDevice(device, session, *stream, |
| 741 | samplingRate, format, channelMask, |
| 742 | flags, offloadInfo); |
| 743 | if (*output == AUDIO_IO_HANDLE_NONE) { |
| 744 | return INVALID_OPERATION; |
| 745 | } |
| 746 | return NO_ERROR; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | audio_io_handle_t AudioPolicyManager::getOutputForDevice( |
| 750 | audio_devices_t device, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 751 | audio_session_t session __unused, |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 752 | audio_stream_type_t stream, |
| 753 | uint32_t samplingRate, |
| 754 | audio_format_t format, |
| 755 | audio_channel_mask_t channelMask, |
| 756 | audio_output_flags_t flags, |
| 757 | const audio_offload_info_t *offloadInfo) |
| 758 | { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 759 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 760 | uint32_t latency = 0; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 761 | status_t status; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 762 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 763 | #ifdef AUDIO_POLICY_TEST |
| 764 | if (mCurOutput != 0) { |
| 765 | ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d", |
| 766 | mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput); |
| 767 | |
| 768 | if (mTestOutputs[mCurOutput] == 0) { |
| 769 | ALOGV("getOutput() opening test output"); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 770 | sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 771 | outputDesc->mDevice = mTestDevice; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 772 | outputDesc->mLatency = mTestLatencyMs; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 773 | outputDesc->mFlags = |
| 774 | (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 775 | outputDesc->mRefCount[stream] = 0; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 776 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 777 | config.sample_rate = mTestSamplingRate; |
| 778 | config.channel_mask = mTestChannels; |
| 779 | config.format = mTestFormat; |
Phil Burk | 77cce80 | 2014-08-04 16:18:15 -0700 | [diff] [blame] | 780 | if (offloadInfo != NULL) { |
| 781 | config.offload_info = *offloadInfo; |
| 782 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 783 | status = mpClientInterface->openOutput(0, |
| 784 | &mTestOutputs[mCurOutput], |
| 785 | &config, |
| 786 | &outputDesc->mDevice, |
| 787 | String8(""), |
| 788 | &outputDesc->mLatency, |
| 789 | outputDesc->mFlags); |
| 790 | if (status == NO_ERROR) { |
| 791 | outputDesc->mSamplingRate = config.sample_rate; |
| 792 | outputDesc->mFormat = config.format; |
| 793 | outputDesc->mChannelMask = config.channel_mask; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 794 | AudioParameter outputCmd = AudioParameter(); |
| 795 | outputCmd.addInt(String8("set_id"),mCurOutput); |
| 796 | mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString()); |
| 797 | addOutput(mTestOutputs[mCurOutput], outputDesc); |
| 798 | } |
| 799 | } |
| 800 | return mTestOutputs[mCurOutput]; |
| 801 | } |
| 802 | #endif //AUDIO_POLICY_TEST |
| 803 | |
| 804 | // open a direct output if required by specified parameters |
| 805 | //force direct flag if offload flag is set: offloading implies a direct output stream |
| 806 | // and all common behaviors are driven by checking only the direct flag |
| 807 | // this should normally be set appropriately in the policy configuration file |
| 808 | if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 809 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 810 | } |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 811 | if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 812 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT); |
| 813 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 814 | // only allow deep buffering for music stream type |
| 815 | if (stream != AUDIO_STREAM_MUSIC) { |
| 816 | flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
| 817 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 818 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 819 | sp<IOProfile> profile; |
| 820 | |
| 821 | // skip direct output selection if the request can obviously be attached to a mixed output |
Eric Laurent | c260784 | 2014-09-29 09:43:03 -0700 | [diff] [blame] | 822 | // and not explicitly requested |
| 823 | if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
| 824 | audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE && |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 825 | audio_channel_count_from_out_mask(channelMask) <= 2) { |
| 826 | goto non_direct_output; |
| 827 | } |
| 828 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 829 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 830 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 831 | // detects there is an active non offloadable effect. |
| 832 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 833 | // This may prevent offloading in rare situations where effects are left active by apps |
| 834 | // in the background. |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 835 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 836 | if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || |
| 837 | !isNonOffloadableEffectEnabled()) { |
| 838 | profile = getProfileForDirectOutput(device, |
| 839 | samplingRate, |
| 840 | format, |
| 841 | channelMask, |
| 842 | (audio_output_flags_t)flags); |
| 843 | } |
| 844 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 845 | if (profile != 0) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 846 | sp<AudioOutputDescriptor> outputDesc = NULL; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 847 | |
| 848 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 849 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 850 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { |
| 851 | outputDesc = desc; |
| 852 | // reuse direct output if currently open and configured with same parameters |
| 853 | if ((samplingRate == outputDesc->mSamplingRate) && |
| 854 | (format == outputDesc->mFormat) && |
| 855 | (channelMask == outputDesc->mChannelMask)) { |
| 856 | outputDesc->mDirectOpenCount++; |
| 857 | ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i)); |
| 858 | return mOutputs.keyAt(i); |
| 859 | } |
| 860 | } |
| 861 | } |
| 862 | // close direct output if currently open and configured with different parameters |
| 863 | if (outputDesc != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 864 | closeOutput(outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 865 | } |
| 866 | outputDesc = new AudioOutputDescriptor(profile); |
| 867 | outputDesc->mDevice = device; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 868 | outputDesc->mLatency = 0; |
| 869 | outputDesc->mFlags =(audio_output_flags_t) (outputDesc->mFlags | flags); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 870 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 871 | config.sample_rate = samplingRate; |
| 872 | config.channel_mask = channelMask; |
| 873 | config.format = format; |
Phil Burk | 77cce80 | 2014-08-04 16:18:15 -0700 | [diff] [blame] | 874 | if (offloadInfo != NULL) { |
| 875 | config.offload_info = *offloadInfo; |
| 876 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 877 | status = mpClientInterface->openOutput(profile->mModule->mHandle, |
| 878 | &output, |
| 879 | &config, |
| 880 | &outputDesc->mDevice, |
| 881 | String8(""), |
| 882 | &outputDesc->mLatency, |
| 883 | outputDesc->mFlags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 884 | |
| 885 | // only accept an output with the requested parameters |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 886 | if (status != NO_ERROR || |
| 887 | (samplingRate != 0 && samplingRate != config.sample_rate) || |
| 888 | (format != AUDIO_FORMAT_DEFAULT && format != config.format) || |
| 889 | (channelMask != 0 && channelMask != config.channel_mask)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 890 | ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d," |
| 891 | "format %d %d, channelMask %04x %04x", output, samplingRate, |
| 892 | outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask, |
| 893 | outputDesc->mChannelMask); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 894 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 895 | mpClientInterface->closeOutput(output); |
| 896 | } |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 897 | // fall back to mixer output if possible when the direct output could not be open |
| 898 | if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) { |
| 899 | goto non_direct_output; |
| 900 | } |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 901 | // fall back to mixer output if possible when the direct output could not be open |
| 902 | if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) { |
| 903 | goto non_direct_output; |
| 904 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 905 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 906 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 907 | outputDesc->mSamplingRate = config.sample_rate; |
| 908 | outputDesc->mChannelMask = config.channel_mask; |
| 909 | outputDesc->mFormat = config.format; |
| 910 | outputDesc->mRefCount[stream] = 0; |
| 911 | outputDesc->mStopTime[stream] = 0; |
| 912 | outputDesc->mDirectOpenCount = 1; |
| 913 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 914 | audio_io_handle_t srcOutput = getOutputForEffect(); |
| 915 | addOutput(output, outputDesc); |
| 916 | audio_io_handle_t dstOutput = getOutputForEffect(); |
| 917 | if (dstOutput == output) { |
| 918 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput); |
| 919 | } |
| 920 | mPreviousOutputs = mOutputs; |
| 921 | ALOGV("getOutput() returns new direct output %d", output); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 922 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 923 | return output; |
| 924 | } |
| 925 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 926 | non_direct_output: |
| 927 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 928 | // ignoring channel mask due to downmix capability in mixer |
| 929 | |
| 930 | // open a non direct output |
| 931 | |
| 932 | // for non direct outputs, only PCM is supported |
| 933 | if (audio_is_linear_pcm(format)) { |
| 934 | // get which output is suitable for the specified stream. The actual |
| 935 | // routing change will happen when startOutput() will be called |
| 936 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 937 | |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 938 | // at this stage we should ignore the DIRECT flag as no direct output could be found earlier |
| 939 | flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT); |
| 940 | output = selectOutput(outputs, flags, format); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 941 | } |
| 942 | ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d," |
| 943 | "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags); |
| 944 | |
| 945 | ALOGV("getOutput() returns output %d", output); |
| 946 | |
| 947 | return output; |
| 948 | } |
| 949 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 950 | audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs, |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 951 | audio_output_flags_t flags, |
| 952 | audio_format_t format) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 953 | { |
| 954 | // select one output among several that provide a path to a particular device or set of |
| 955 | // devices (the list was previously build by getOutputsForDevice()). |
| 956 | // The priority is as follows: |
| 957 | // 1: the output with the highest number of requested policy flags |
| 958 | // 2: the primary output |
| 959 | // 3: the first output in the list |
| 960 | |
| 961 | if (outputs.size() == 0) { |
| 962 | return 0; |
| 963 | } |
| 964 | if (outputs.size() == 1) { |
| 965 | return outputs[0]; |
| 966 | } |
| 967 | |
| 968 | int maxCommonFlags = 0; |
| 969 | audio_io_handle_t outputFlags = 0; |
| 970 | audio_io_handle_t outputPrimary = 0; |
| 971 | |
| 972 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 973 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 974 | if (!outputDesc->isDuplicated()) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 975 | // if a valid format is specified, skip output if not compatible |
| 976 | if (format != AUDIO_FORMAT_INVALID) { |
| 977 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 978 | if (format != outputDesc->mFormat) { |
| 979 | continue; |
| 980 | } |
| 981 | } else if (!audio_is_linear_pcm(format)) { |
| 982 | continue; |
| 983 | } |
| 984 | } |
| 985 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 986 | int commonFlags = popcount(outputDesc->mProfile->mFlags & flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 987 | if (commonFlags > maxCommonFlags) { |
| 988 | outputFlags = outputs[i]; |
| 989 | maxCommonFlags = commonFlags; |
| 990 | ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags); |
| 991 | } |
| 992 | if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) { |
| 993 | outputPrimary = outputs[i]; |
| 994 | } |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | if (outputFlags != 0) { |
| 999 | return outputFlags; |
| 1000 | } |
| 1001 | if (outputPrimary != 0) { |
| 1002 | return outputPrimary; |
| 1003 | } |
| 1004 | |
| 1005 | return outputs[0]; |
| 1006 | } |
| 1007 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1008 | status_t AudioPolicyManager::startOutput(audio_io_handle_t output, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1009 | audio_stream_type_t stream, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1010 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1011 | { |
| 1012 | ALOGV("startOutput() output %d, stream %d, session %d", output, stream, session); |
| 1013 | ssize_t index = mOutputs.indexOfKey(output); |
| 1014 | if (index < 0) { |
| 1015 | ALOGW("startOutput() unknown output %d", output); |
| 1016 | return BAD_VALUE; |
| 1017 | } |
| 1018 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1019 | // cannot start playback of STREAM_TTS if any other output is being used |
| 1020 | uint32_t beaconMuteLatency = 0; |
| 1021 | if (stream == AUDIO_STREAM_TTS) { |
| 1022 | ALOGV("\t found BEACON stream"); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1023 | if (mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1024 | return INVALID_OPERATION; |
| 1025 | } else { |
| 1026 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); |
| 1027 | } |
| 1028 | } else { |
| 1029 | // some playback other than beacon starts |
| 1030 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); |
| 1031 | } |
| 1032 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1033 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1034 | |
| 1035 | // increment usage count for this stream on the requested output: |
| 1036 | // NOTE that the usage count is the same for duplicated output and hardware output which is |
| 1037 | // necessary for a correct control of hardware output routing by startOutput() and stopOutput() |
| 1038 | outputDesc->changeRefCount(stream, 1); |
| 1039 | |
| 1040 | if (outputDesc->mRefCount[stream] == 1) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1041 | // starting an output being rerouted? |
| 1042 | audio_devices_t newDevice; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1043 | if (outputDesc->mPolicyMix != NULL) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1044 | newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
| 1045 | } else { |
| 1046 | newDevice = getNewOutputDevice(output, false /*fromCache*/); |
| 1047 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1048 | routing_strategy strategy = getStrategy(stream); |
| 1049 | bool shouldWait = (strategy == STRATEGY_SONIFICATION) || |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1050 | (strategy == STRATEGY_SONIFICATION_RESPECTFUL) || |
| 1051 | (beaconMuteLatency > 0); |
| 1052 | uint32_t waitMs = beaconMuteLatency; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1053 | bool force = false; |
| 1054 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1055 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1056 | if (desc != outputDesc) { |
| 1057 | // force a device change if any other output is managed by the same hw |
| 1058 | // module and has a current device selection that differs from selected device. |
| 1059 | // In this case, the audio HAL must receive the new device selection so that it can |
| 1060 | // change the device currently selected by the other active output. |
| 1061 | if (outputDesc->sharesHwModuleWith(desc) && |
| 1062 | desc->device() != newDevice) { |
| 1063 | force = true; |
| 1064 | } |
| 1065 | // wait for audio on other active outputs to be presented when starting |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1066 | // a notification so that audio focus effect can propagate, or that a mute/unmute |
| 1067 | // event occurred for beacon |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1068 | uint32_t latency = desc->latency(); |
| 1069 | if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) { |
| 1070 | waitMs = latency; |
| 1071 | } |
| 1072 | } |
| 1073 | } |
| 1074 | uint32_t muteWaitMs = setOutputDevice(output, newDevice, force); |
| 1075 | |
| 1076 | // handle special case for sonification while in call |
| 1077 | if (isInCall()) { |
| 1078 | handleIncallSonification(stream, true, false); |
| 1079 | } |
| 1080 | |
| 1081 | // apply volume rules for current stream and device if necessary |
| 1082 | checkAndSetVolume(stream, |
| 1083 | mStreams[stream].getVolumeIndex(newDevice), |
| 1084 | output, |
| 1085 | newDevice); |
| 1086 | |
| 1087 | // update the outputs if starting an output with a stream that can affect notification |
| 1088 | // routing |
| 1089 | handleNotificationRoutingForStream(stream); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1090 | |
| 1091 | // Automatically enable the remote submix input when output is started on a re routing mix |
| 1092 | // of type MIX_TYPE_RECORDERS |
| 1093 | if (audio_is_remote_submix_device(newDevice) && outputDesc->mPolicyMix != NULL && |
| 1094 | outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) { |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 1095 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1096 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 1097 | outputDesc->mPolicyMix->mRegistrationId, |
| 1098 | "remote-submix"); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1099 | } |
| 1100 | |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 1101 | // force reevaluating accessibility routing when ringtone or alarm starts |
| 1102 | if (strategy == STRATEGY_SONIFICATION) { |
| 1103 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
| 1104 | } |
| 1105 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1106 | if (waitMs > muteWaitMs) { |
| 1107 | usleep((waitMs - muteWaitMs) * 2 * 1000); |
| 1108 | } |
| 1109 | } |
| 1110 | return NO_ERROR; |
| 1111 | } |
| 1112 | |
| 1113 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1114 | status_t AudioPolicyManager::stopOutput(audio_io_handle_t output, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1115 | audio_stream_type_t stream, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1116 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1117 | { |
| 1118 | ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session); |
| 1119 | ssize_t index = mOutputs.indexOfKey(output); |
| 1120 | if (index < 0) { |
| 1121 | ALOGW("stopOutput() unknown output %d", output); |
| 1122 | return BAD_VALUE; |
| 1123 | } |
| 1124 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1125 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1126 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1127 | // always handle stream stop, check which stream type is stopping |
| 1128 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); |
| 1129 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1130 | // handle special case for sonification while in call |
| 1131 | if (isInCall()) { |
| 1132 | handleIncallSonification(stream, false, false); |
| 1133 | } |
| 1134 | |
| 1135 | if (outputDesc->mRefCount[stream] > 0) { |
| 1136 | // decrement usage count of this stream on the output |
| 1137 | outputDesc->changeRefCount(stream, -1); |
| 1138 | // store time at which the stream was stopped - see isStreamActive() |
| 1139 | if (outputDesc->mRefCount[stream] == 0) { |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1140 | // Automatically disable the remote submix input when output is stopped on a |
| 1141 | // re routing mix of type MIX_TYPE_RECORDERS |
| 1142 | if (audio_is_remote_submix_device(outputDesc->mDevice) && |
| 1143 | outputDesc->mPolicyMix != NULL && |
| 1144 | outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) { |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 1145 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1146 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 1147 | outputDesc->mPolicyMix->mRegistrationId, |
| 1148 | "remote-submix"); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1149 | } |
| 1150 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1151 | outputDesc->mStopTime[stream] = systemTime(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1152 | audio_devices_t newDevice = getNewOutputDevice(output, false /*fromCache*/); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1153 | // delay the device switch by twice the latency because stopOutput() is executed when |
| 1154 | // the track stop() command is received and at that time the audio track buffer can |
| 1155 | // still contain data that needs to be drained. The latency only covers the audio HAL |
| 1156 | // and kernel buffers. Also the latency does not always include additional delay in the |
| 1157 | // audio path (audio DSP, CODEC ...) |
| 1158 | setOutputDevice(output, newDevice, false, outputDesc->mLatency*2); |
| 1159 | |
| 1160 | // force restoring the device selection on other active outputs if it differs from the |
| 1161 | // one being selected for this output |
| 1162 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 1163 | audio_io_handle_t curOutput = mOutputs.keyAt(i); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1164 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1165 | if (curOutput != output && |
| 1166 | desc->isActive() && |
| 1167 | outputDesc->sharesHwModuleWith(desc) && |
| 1168 | (newDevice != desc->device())) { |
| 1169 | setOutputDevice(curOutput, |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1170 | getNewOutputDevice(curOutput, false /*fromCache*/), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1171 | true, |
| 1172 | outputDesc->mLatency*2); |
| 1173 | } |
| 1174 | } |
| 1175 | // update the outputs if stopping one with a stream that can affect notification routing |
| 1176 | handleNotificationRoutingForStream(stream); |
| 1177 | } |
| 1178 | return NO_ERROR; |
| 1179 | } else { |
| 1180 | ALOGW("stopOutput() refcount is already 0 for output %d", output); |
| 1181 | return INVALID_OPERATION; |
| 1182 | } |
| 1183 | } |
| 1184 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1185 | void AudioPolicyManager::releaseOutput(audio_io_handle_t output, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1186 | audio_stream_type_t stream __unused, |
| 1187 | audio_session_t session __unused) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1188 | { |
| 1189 | ALOGV("releaseOutput() %d", output); |
| 1190 | ssize_t index = mOutputs.indexOfKey(output); |
| 1191 | if (index < 0) { |
| 1192 | ALOGW("releaseOutput() releasing unknown output %d", output); |
| 1193 | return; |
| 1194 | } |
| 1195 | |
| 1196 | #ifdef AUDIO_POLICY_TEST |
| 1197 | int testIndex = testOutputIndex(output); |
| 1198 | if (testIndex != 0) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1199 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1200 | if (outputDesc->isActive()) { |
| 1201 | mpClientInterface->closeOutput(output); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1202 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1203 | mTestOutputs[testIndex] = 0; |
| 1204 | } |
| 1205 | return; |
| 1206 | } |
| 1207 | #endif //AUDIO_POLICY_TEST |
| 1208 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1209 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(index); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1210 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1211 | if (desc->mDirectOpenCount <= 0) { |
| 1212 | ALOGW("releaseOutput() invalid open count %d for output %d", |
| 1213 | desc->mDirectOpenCount, output); |
| 1214 | return; |
| 1215 | } |
| 1216 | if (--desc->mDirectOpenCount == 0) { |
| 1217 | closeOutput(output); |
| 1218 | // If effects where present on the output, audioflinger moved them to the primary |
| 1219 | // output by default: move them back to the appropriate output. |
| 1220 | audio_io_handle_t dstOutput = getOutputForEffect(); |
| 1221 | if (dstOutput != mPrimaryOutput) { |
| 1222 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mPrimaryOutput, dstOutput); |
| 1223 | } |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1224 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1225 | } |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1230 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, |
| 1231 | audio_io_handle_t *input, |
| 1232 | audio_session_t session, |
| 1233 | uint32_t samplingRate, |
| 1234 | audio_format_t format, |
| 1235 | audio_channel_mask_t channelMask, |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1236 | audio_input_flags_t flags, |
| 1237 | input_type_t *inputType) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1238 | { |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1239 | ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x," |
| 1240 | "session %d, flags %#x", |
| 1241 | attr->source, samplingRate, format, channelMask, session, flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1242 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1243 | *input = AUDIO_IO_HANDLE_NONE; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1244 | *inputType = API_INPUT_INVALID; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1245 | audio_devices_t device; |
| 1246 | // handle legacy remote submix case where the address was not always specified |
| 1247 | String8 address = String8(""); |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1248 | bool isSoundTrigger = false; |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1249 | audio_source_t inputSource = attr->source; |
| 1250 | audio_source_t halInputSource; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1251 | AudioMix *policyMix = NULL; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1252 | |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1253 | if (inputSource == AUDIO_SOURCE_DEFAULT) { |
| 1254 | inputSource = AUDIO_SOURCE_MIC; |
| 1255 | } |
| 1256 | halInputSource = inputSource; |
| 1257 | |
| 1258 | if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX && |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1259 | strncmp(attr->tags, "addr=", strlen("addr=")) == 0) { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame^] | 1260 | status_t ret = mPolicyMixes.getInputMixForAttr(*attr, policyMix); |
| 1261 | if (ret != NO_ERROR) { |
| 1262 | return ret; |
| 1263 | } |
| 1264 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1265 | device = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 1266 | address = String8(attr->tags + strlen("addr=")); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1267 | } else { |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1268 | device = getDeviceAndMixForInputSource(inputSource, &policyMix); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1269 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1270 | ALOGW("getInputForAttr() could not find device for source %d", inputSource); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1271 | return BAD_VALUE; |
| 1272 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1273 | if (policyMix != NULL) { |
| 1274 | address = policyMix->mRegistrationId; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1275 | if (policyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1276 | // there is an external policy, but this input is attached to a mix of recorders, |
| 1277 | // meaning it receives audio injected into the framework, so the recorder doesn't |
| 1278 | // know about it and is therefore considered "legacy" |
| 1279 | *inputType = API_INPUT_LEGACY; |
| 1280 | } else { |
| 1281 | // recording a mix of players defined by an external policy, we're rerouting for |
| 1282 | // an external policy |
| 1283 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
| 1284 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1285 | } else if (audio_is_remote_submix_device(device)) { |
| 1286 | address = String8("0"); |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1287 | *inputType = API_INPUT_MIX_CAPTURE; |
| 1288 | } else { |
| 1289 | *inputType = API_INPUT_LEGACY; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1290 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1291 | // adapt channel selection to input source |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1292 | switch (inputSource) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1293 | case AUDIO_SOURCE_VOICE_UPLINK: |
| 1294 | channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK; |
| 1295 | break; |
| 1296 | case AUDIO_SOURCE_VOICE_DOWNLINK: |
| 1297 | channelMask = AUDIO_CHANNEL_IN_VOICE_DNLINK; |
| 1298 | break; |
| 1299 | case AUDIO_SOURCE_VOICE_CALL: |
| 1300 | channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK | AUDIO_CHANNEL_IN_VOICE_DNLINK; |
| 1301 | break; |
| 1302 | default: |
| 1303 | break; |
| 1304 | } |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1305 | if (inputSource == AUDIO_SOURCE_HOTWORD) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1306 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); |
| 1307 | if (index >= 0) { |
| 1308 | *input = mSoundTriggerSessions.valueFor(session); |
| 1309 | isSoundTrigger = true; |
| 1310 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); |
| 1311 | ALOGV("SoundTrigger capture on session %d input %d", session, *input); |
| 1312 | } else { |
| 1313 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 1314 | } |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1315 | } |
| 1316 | } |
| 1317 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1318 | sp<IOProfile> profile = getInputProfile(device, address, |
| 1319 | samplingRate, format, channelMask, |
| 1320 | flags); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1321 | if (profile == 0) { |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1322 | //retry without flags |
| 1323 | audio_input_flags_t log_flags = flags; |
| 1324 | flags = AUDIO_INPUT_FLAG_NONE; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1325 | profile = getInputProfile(device, address, |
| 1326 | samplingRate, format, channelMask, |
| 1327 | flags); |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1328 | if (profile == 0) { |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1329 | ALOGW("getInputForAttr() could not find profile for device 0x%X, samplingRate %u," |
| 1330 | "format %#x, channelMask 0x%X, flags %#x", |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1331 | device, samplingRate, format, channelMask, log_flags); |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1332 | return BAD_VALUE; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1333 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | if (profile->mModule->mHandle == 0) { |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1337 | ALOGE("getInputForAttr(): HW module %s not opened", profile->mModule->mName); |
| 1338 | return NO_INIT; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 1342 | config.sample_rate = samplingRate; |
| 1343 | config.channel_mask = channelMask; |
| 1344 | config.format = format; |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1345 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1346 | status_t status = mpClientInterface->openInput(profile->mModule->mHandle, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1347 | input, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1348 | &config, |
| 1349 | &device, |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 1350 | address, |
Eric Laurent | 1c9c2cc | 2014-08-28 19:37:25 -0700 | [diff] [blame] | 1351 | halInputSource, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1352 | flags); |
| 1353 | |
| 1354 | // only accept input with the exact requested set of parameters |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1355 | if (status != NO_ERROR || *input == AUDIO_IO_HANDLE_NONE || |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1356 | (samplingRate != config.sample_rate) || |
| 1357 | (format != config.format) || |
| 1358 | (channelMask != config.channel_mask)) { |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1359 | ALOGW("getInputForAttr() failed opening input: samplingRate %d, format %d, channelMask %x", |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1360 | samplingRate, format, channelMask); |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1361 | if (*input != AUDIO_IO_HANDLE_NONE) { |
| 1362 | mpClientInterface->closeInput(*input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1363 | } |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1364 | return BAD_VALUE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1365 | } |
| 1366 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1367 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile); |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1368 | inputDesc->mInputSource = inputSource; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1369 | inputDesc->mRefCount = 0; |
| 1370 | inputDesc->mOpenRefCount = 1; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1371 | inputDesc->mSamplingRate = samplingRate; |
| 1372 | inputDesc->mFormat = format; |
| 1373 | inputDesc->mChannelMask = channelMask; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1374 | inputDesc->mDevice = device; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1375 | inputDesc->mSessions.add(session); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1376 | inputDesc->mIsSoundTrigger = isSoundTrigger; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1377 | inputDesc->mPolicyMix = policyMix; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1378 | |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1379 | ALOGV("getInputForAttr() returns input type = %d", inputType); |
| 1380 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1381 | addInput(*input, inputDesc); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1382 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1383 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1384 | } |
| 1385 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1386 | status_t AudioPolicyManager::startInput(audio_io_handle_t input, |
| 1387 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1388 | { |
| 1389 | ALOGV("startInput() input %d", input); |
| 1390 | ssize_t index = mInputs.indexOfKey(input); |
| 1391 | if (index < 0) { |
| 1392 | ALOGW("startInput() unknown input %d", input); |
| 1393 | return BAD_VALUE; |
| 1394 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1395 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1396 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1397 | index = inputDesc->mSessions.indexOf(session); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1398 | if (index < 0) { |
| 1399 | ALOGW("startInput() unknown session %d on input %d", session, input); |
| 1400 | return BAD_VALUE; |
| 1401 | } |
| 1402 | |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1403 | // virtual input devices are compatible with other input devices |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1404 | if (!is_virtual_input_device(inputDesc->mDevice)) { |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1405 | |
| 1406 | // for a non-virtual input device, check if there is another (non-virtual) active input |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1407 | audio_io_handle_t activeInput = mInputs.getActiveInput(); |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1408 | if (activeInput != 0 && activeInput != input) { |
| 1409 | |
| 1410 | // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed, |
| 1411 | // otherwise the active input continues and the new input cannot be started. |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1412 | sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1413 | if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) { |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1414 | ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1415 | stopInput(activeInput, activeDesc->mSessions.itemAt(0)); |
| 1416 | releaseInput(activeInput, activeDesc->mSessions.itemAt(0)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1417 | } else { |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1418 | ALOGE("startInput(%d) failed: other input %d already started", input, activeInput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1419 | return INVALID_OPERATION; |
| 1420 | } |
| 1421 | } |
| 1422 | } |
| 1423 | |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1424 | if (inputDesc->mRefCount == 0) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1425 | if (mInputs.activeInputsCount() == 0) { |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1426 | SoundTrigger::setCaptureState(true); |
| 1427 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1428 | setInputDevice(input, getNewInputDevice(input), true /* force */); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1429 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1430 | // automatically enable the remote submix output when input is started if not |
| 1431 | // used by a policy mix of type MIX_TYPE_RECORDERS |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1432 | // For remote submix (a virtual device), we open only one input per capture request. |
| 1433 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1434 | String8 address = String8(""); |
| 1435 | if (inputDesc->mPolicyMix == NULL) { |
| 1436 | address = String8("0"); |
| 1437 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 1438 | address = inputDesc->mPolicyMix->mRegistrationId; |
| 1439 | } |
| 1440 | if (address != "") { |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 1441 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1442 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 1443 | address, "remote-submix"); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1444 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1445 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1446 | } |
| 1447 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1448 | ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource); |
| 1449 | |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1450 | inputDesc->mRefCount++; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1451 | return NO_ERROR; |
| 1452 | } |
| 1453 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1454 | status_t AudioPolicyManager::stopInput(audio_io_handle_t input, |
| 1455 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1456 | { |
| 1457 | ALOGV("stopInput() input %d", input); |
| 1458 | ssize_t index = mInputs.indexOfKey(input); |
| 1459 | if (index < 0) { |
| 1460 | ALOGW("stopInput() unknown input %d", input); |
| 1461 | return BAD_VALUE; |
| 1462 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1463 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1464 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1465 | index = inputDesc->mSessions.indexOf(session); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1466 | if (index < 0) { |
| 1467 | ALOGW("stopInput() unknown session %d on input %d", session, input); |
| 1468 | return BAD_VALUE; |
| 1469 | } |
| 1470 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1471 | if (inputDesc->mRefCount == 0) { |
| 1472 | ALOGW("stopInput() input %d already stopped", input); |
| 1473 | return INVALID_OPERATION; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | inputDesc->mRefCount--; |
| 1477 | if (inputDesc->mRefCount == 0) { |
| 1478 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1479 | // automatically disable the remote submix output when input is stopped if not |
| 1480 | // used by a policy mix of type MIX_TYPE_RECORDERS |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1481 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1482 | String8 address = String8(""); |
| 1483 | if (inputDesc->mPolicyMix == NULL) { |
| 1484 | address = String8("0"); |
| 1485 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 1486 | address = inputDesc->mPolicyMix->mRegistrationId; |
| 1487 | } |
| 1488 | if (address != "") { |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 1489 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1490 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 1491 | address, "remote-submix"); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1492 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1493 | } |
| 1494 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1495 | resetInputDevice(input); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1496 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1497 | if (mInputs.activeInputsCount() == 0) { |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1498 | SoundTrigger::setCaptureState(false); |
| 1499 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1500 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1501 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1502 | } |
| 1503 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1504 | void AudioPolicyManager::releaseInput(audio_io_handle_t input, |
| 1505 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1506 | { |
| 1507 | ALOGV("releaseInput() %d", input); |
| 1508 | ssize_t index = mInputs.indexOfKey(input); |
| 1509 | if (index < 0) { |
| 1510 | ALOGW("releaseInput() releasing unknown input %d", input); |
| 1511 | return; |
| 1512 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1513 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
| 1514 | ALOG_ASSERT(inputDesc != 0); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1515 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1516 | index = inputDesc->mSessions.indexOf(session); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1517 | if (index < 0) { |
| 1518 | ALOGW("releaseInput() unknown session %d on input %d", session, input); |
| 1519 | return; |
| 1520 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1521 | inputDesc->mSessions.remove(session); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1522 | if (inputDesc->mOpenRefCount == 0) { |
| 1523 | ALOGW("releaseInput() invalid open ref count %d", inputDesc->mOpenRefCount); |
| 1524 | return; |
| 1525 | } |
| 1526 | inputDesc->mOpenRefCount--; |
| 1527 | if (inputDesc->mOpenRefCount > 0) { |
| 1528 | ALOGV("releaseInput() exit > 0"); |
| 1529 | return; |
| 1530 | } |
| 1531 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1532 | closeInput(input); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1533 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1534 | ALOGV("releaseInput() exit"); |
| 1535 | } |
| 1536 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1537 | void AudioPolicyManager::closeAllInputs() { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1538 | bool patchRemoved = false; |
| 1539 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1540 | for(size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1541 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index); |
| 1542 | ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle); |
| 1543 | if (patch_index >= 0) { |
| 1544 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index); |
| 1545 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 1546 | mAudioPatches.removeItemsAt(patch_index); |
| 1547 | patchRemoved = true; |
| 1548 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1549 | mpClientInterface->closeInput(mInputs.keyAt(input_index)); |
| 1550 | } |
| 1551 | mInputs.clear(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1552 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1553 | |
| 1554 | if (patchRemoved) { |
| 1555 | mpClientInterface->onAudioPatchListUpdate(); |
| 1556 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1557 | } |
| 1558 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1559 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1560 | int indexMin, |
| 1561 | int indexMax) |
| 1562 | { |
| 1563 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); |
| 1564 | if (indexMin < 0 || indexMin >= indexMax) { |
| 1565 | ALOGW("initStreamVolume() invalid index limits for stream %d, min %d, max %d", stream , indexMin, indexMax); |
| 1566 | return; |
| 1567 | } |
| 1568 | mStreams[stream].mIndexMin = indexMin; |
| 1569 | mStreams[stream].mIndexMax = indexMax; |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1570 | //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now |
| 1571 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1572 | mStreams[AUDIO_STREAM_ACCESSIBILITY].mIndexMin = indexMin; |
| 1573 | mStreams[AUDIO_STREAM_ACCESSIBILITY].mIndexMax = indexMax; |
| 1574 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1575 | } |
| 1576 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1577 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1578 | int index, |
| 1579 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1580 | { |
| 1581 | |
| 1582 | if ((index < mStreams[stream].mIndexMin) || (index > mStreams[stream].mIndexMax)) { |
| 1583 | return BAD_VALUE; |
| 1584 | } |
| 1585 | if (!audio_is_output_device(device)) { |
| 1586 | return BAD_VALUE; |
| 1587 | } |
| 1588 | |
| 1589 | // Force max volume if stream cannot be muted |
| 1590 | if (!mStreams[stream].mCanBeMuted) index = mStreams[stream].mIndexMax; |
| 1591 | |
| 1592 | ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d", |
| 1593 | stream, device, index); |
| 1594 | |
| 1595 | // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and |
| 1596 | // clear all device specific values |
| 1597 | if (device == AUDIO_DEVICE_OUT_DEFAULT) { |
| 1598 | mStreams[stream].mIndexCur.clear(); |
| 1599 | } |
| 1600 | mStreams[stream].mIndexCur.add(device, index); |
| 1601 | |
Eric Laurent | 31551f8 | 2014-10-10 18:21:56 -0700 | [diff] [blame] | 1602 | // update volume on all outputs whose current device is also selected by the same |
| 1603 | // strategy as the device specified by the caller |
| 1604 | audio_devices_t strategyDevice = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1605 | |
| 1606 | |
| 1607 | //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now |
| 1608 | audio_devices_t accessibilityDevice = AUDIO_DEVICE_NONE; |
| 1609 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1610 | mStreams[AUDIO_STREAM_ACCESSIBILITY].mIndexCur.add(device, index); |
| 1611 | accessibilityDevice = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, true /*fromCache*/); |
| 1612 | } |
| 1613 | if ((device != AUDIO_DEVICE_OUT_DEFAULT) && |
| 1614 | (device & (strategyDevice | accessibilityDevice)) == 0) { |
Eric Laurent | 31551f8 | 2014-10-10 18:21:56 -0700 | [diff] [blame] | 1615 | return NO_ERROR; |
| 1616 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1617 | status_t status = NO_ERROR; |
| 1618 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 1619 | audio_devices_t curDevice = |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 1620 | ApmGains::getDeviceForVolume(mOutputs.valueAt(i)->device()); |
Eric Laurent | 31551f8 | 2014-10-10 18:21:56 -0700 | [diff] [blame] | 1621 | if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & strategyDevice) != 0)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1622 | status_t volStatus = checkAndSetVolume(stream, index, mOutputs.keyAt(i), curDevice); |
| 1623 | if (volStatus != NO_ERROR) { |
| 1624 | status = volStatus; |
| 1625 | } |
| 1626 | } |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1627 | if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & accessibilityDevice) != 0)) { |
| 1628 | status_t volStatus = checkAndSetVolume(AUDIO_STREAM_ACCESSIBILITY, |
| 1629 | index, mOutputs.keyAt(i), curDevice); |
| 1630 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1631 | } |
| 1632 | return status; |
| 1633 | } |
| 1634 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1635 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1636 | int *index, |
| 1637 | audio_devices_t device) |
| 1638 | { |
| 1639 | if (index == NULL) { |
| 1640 | return BAD_VALUE; |
| 1641 | } |
| 1642 | if (!audio_is_output_device(device)) { |
| 1643 | return BAD_VALUE; |
| 1644 | } |
| 1645 | // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to |
| 1646 | // the strategy the stream belongs to. |
| 1647 | if (device == AUDIO_DEVICE_OUT_DEFAULT) { |
| 1648 | device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); |
| 1649 | } |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 1650 | device = ApmGains::getDeviceForVolume(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1651 | |
| 1652 | *index = mStreams[stream].getVolumeIndex(device); |
| 1653 | ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index); |
| 1654 | return NO_ERROR; |
| 1655 | } |
| 1656 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1657 | audio_io_handle_t AudioPolicyManager::selectOutputForEffects( |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1658 | const SortedVector<audio_io_handle_t>& outputs) |
| 1659 | { |
| 1660 | // select one output among several suitable for global effects. |
| 1661 | // The priority is as follows: |
| 1662 | // 1: An offloaded output. If the effect ends up not being offloadable, |
| 1663 | // AudioFlinger will invalidate the track and the offloaded output |
| 1664 | // will be closed causing the effect to be moved to a PCM output. |
| 1665 | // 2: A deep buffer output |
| 1666 | // 3: the first output in the list |
| 1667 | |
| 1668 | if (outputs.size() == 0) { |
| 1669 | return 0; |
| 1670 | } |
| 1671 | |
| 1672 | audio_io_handle_t outputOffloaded = 0; |
| 1673 | audio_io_handle_t outputDeepBuffer = 0; |
| 1674 | |
| 1675 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1676 | sp<AudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1677 | ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1678 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 1679 | outputOffloaded = outputs[i]; |
| 1680 | } |
| 1681 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { |
| 1682 | outputDeepBuffer = outputs[i]; |
| 1683 | } |
| 1684 | } |
| 1685 | |
| 1686 | ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d", |
| 1687 | outputOffloaded, outputDeepBuffer); |
| 1688 | if (outputOffloaded != 0) { |
| 1689 | return outputOffloaded; |
| 1690 | } |
| 1691 | if (outputDeepBuffer != 0) { |
| 1692 | return outputDeepBuffer; |
| 1693 | } |
| 1694 | |
| 1695 | return outputs[0]; |
| 1696 | } |
| 1697 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1698 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1699 | { |
| 1700 | // apply simple rule where global effects are attached to the same output as MUSIC streams |
| 1701 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1702 | routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1703 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 1704 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs); |
| 1705 | |
| 1706 | audio_io_handle_t output = selectOutputForEffects(dstOutputs); |
| 1707 | ALOGV("getOutputForEffect() got output %d for fx %s flags %x", |
| 1708 | output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags); |
| 1709 | |
| 1710 | return output; |
| 1711 | } |
| 1712 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1713 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1714 | audio_io_handle_t io, |
| 1715 | uint32_t strategy, |
| 1716 | int session, |
| 1717 | int id) |
| 1718 | { |
| 1719 | ssize_t index = mOutputs.indexOfKey(io); |
| 1720 | if (index < 0) { |
| 1721 | index = mInputs.indexOfKey(io); |
| 1722 | if (index < 0) { |
| 1723 | ALOGW("registerEffect() unknown io %d", io); |
| 1724 | return INVALID_OPERATION; |
| 1725 | } |
| 1726 | } |
| 1727 | |
| 1728 | if (mTotalEffectsMemory + desc->memoryUsage > getMaxEffectsMemory()) { |
| 1729 | ALOGW("registerEffect() memory limit exceeded for Fx %s, Memory %d KB", |
| 1730 | desc->name, desc->memoryUsage); |
| 1731 | return INVALID_OPERATION; |
| 1732 | } |
| 1733 | mTotalEffectsMemory += desc->memoryUsage; |
| 1734 | ALOGV("registerEffect() effect %s, io %d, strategy %d session %d id %d", |
| 1735 | desc->name, io, strategy, session, id); |
| 1736 | ALOGV("registerEffect() memory %d, total memory %d", desc->memoryUsage, mTotalEffectsMemory); |
| 1737 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1738 | sp<EffectDescriptor> effectDesc = new EffectDescriptor(); |
| 1739 | memcpy (&effectDesc->mDesc, desc, sizeof(effect_descriptor_t)); |
| 1740 | effectDesc->mIo = io; |
| 1741 | effectDesc->mStrategy = (routing_strategy)strategy; |
| 1742 | effectDesc->mSession = session; |
| 1743 | effectDesc->mEnabled = false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1744 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1745 | mEffects.add(id, effectDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1746 | |
| 1747 | return NO_ERROR; |
| 1748 | } |
| 1749 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1750 | status_t AudioPolicyManager::unregisterEffect(int id) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1751 | { |
| 1752 | ssize_t index = mEffects.indexOfKey(id); |
| 1753 | if (index < 0) { |
| 1754 | ALOGW("unregisterEffect() unknown effect ID %d", id); |
| 1755 | return INVALID_OPERATION; |
| 1756 | } |
| 1757 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1758 | sp<EffectDescriptor> effectDesc = mEffects.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1759 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1760 | setEffectEnabled(effectDesc, false); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1761 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1762 | if (mTotalEffectsMemory < effectDesc->mDesc.memoryUsage) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1763 | ALOGW("unregisterEffect() memory %d too big for total %d", |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1764 | effectDesc->mDesc.memoryUsage, mTotalEffectsMemory); |
| 1765 | effectDesc->mDesc.memoryUsage = mTotalEffectsMemory; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1766 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1767 | mTotalEffectsMemory -= effectDesc->mDesc.memoryUsage; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1768 | ALOGV("unregisterEffect() effect %s, ID %d, memory %d total memory %d", |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1769 | effectDesc->mDesc.name, id, effectDesc->mDesc.memoryUsage, mTotalEffectsMemory); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1770 | |
| 1771 | mEffects.removeItem(id); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1772 | |
| 1773 | return NO_ERROR; |
| 1774 | } |
| 1775 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1776 | status_t AudioPolicyManager::setEffectEnabled(int id, bool enabled) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1777 | { |
| 1778 | ssize_t index = mEffects.indexOfKey(id); |
| 1779 | if (index < 0) { |
| 1780 | ALOGW("unregisterEffect() unknown effect ID %d", id); |
| 1781 | return INVALID_OPERATION; |
| 1782 | } |
| 1783 | |
| 1784 | return setEffectEnabled(mEffects.valueAt(index), enabled); |
| 1785 | } |
| 1786 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1787 | status_t AudioPolicyManager::setEffectEnabled(const sp<EffectDescriptor>& effectDesc, bool enabled) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1788 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1789 | if (enabled == effectDesc->mEnabled) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1790 | ALOGV("setEffectEnabled(%s) effect already %s", |
| 1791 | enabled?"true":"false", enabled?"enabled":"disabled"); |
| 1792 | return INVALID_OPERATION; |
| 1793 | } |
| 1794 | |
| 1795 | if (enabled) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1796 | if (mTotalEffectsCpuLoad + effectDesc->mDesc.cpuLoad > getMaxEffectsCpuLoad()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1797 | ALOGW("setEffectEnabled(true) CPU Load limit exceeded for Fx %s, CPU %f MIPS", |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1798 | effectDesc->mDesc.name, (float)effectDesc->mDesc.cpuLoad/10); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1799 | return INVALID_OPERATION; |
| 1800 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1801 | mTotalEffectsCpuLoad += effectDesc->mDesc.cpuLoad; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1802 | ALOGV("setEffectEnabled(true) total CPU %d", mTotalEffectsCpuLoad); |
| 1803 | } else { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1804 | if (mTotalEffectsCpuLoad < effectDesc->mDesc.cpuLoad) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1805 | ALOGW("setEffectEnabled(false) CPU load %d too high for total %d", |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1806 | effectDesc->mDesc.cpuLoad, mTotalEffectsCpuLoad); |
| 1807 | effectDesc->mDesc.cpuLoad = mTotalEffectsCpuLoad; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1808 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1809 | mTotalEffectsCpuLoad -= effectDesc->mDesc.cpuLoad; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1810 | ALOGV("setEffectEnabled(false) total CPU %d", mTotalEffectsCpuLoad); |
| 1811 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1812 | effectDesc->mEnabled = enabled; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1813 | return NO_ERROR; |
| 1814 | } |
| 1815 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1816 | bool AudioPolicyManager::isNonOffloadableEffectEnabled() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1817 | { |
| 1818 | for (size_t i = 0; i < mEffects.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1819 | sp<EffectDescriptor> effectDesc = mEffects.valueAt(i); |
| 1820 | if (effectDesc->mEnabled && (effectDesc->mStrategy == STRATEGY_MEDIA) && |
| 1821 | ((effectDesc->mDesc.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) == 0)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1822 | ALOGV("isNonOffloadableEffectEnabled() non offloadable effect %s enabled on session %d", |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1823 | effectDesc->mDesc.name, effectDesc->mSession); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1824 | return true; |
| 1825 | } |
| 1826 | } |
| 1827 | return false; |
| 1828 | } |
| 1829 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1830 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1831 | { |
| 1832 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1833 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
Eric Laurent | a34c9ce | 2014-12-19 11:16:32 -0800 | [diff] [blame] | 1834 | if (inputDescriptor->mRefCount == 0) { |
| 1835 | continue; |
| 1836 | } |
| 1837 | if (inputDescriptor->mInputSource == (int)source) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1838 | return true; |
| 1839 | } |
Eric Laurent | a34c9ce | 2014-12-19 11:16:32 -0800 | [diff] [blame] | 1840 | // AUDIO_SOURCE_HOTWORD is equivalent to AUDIO_SOURCE_VOICE_RECOGNITION only if it |
| 1841 | // corresponds to an active capture triggered by a hardware hotword recognition |
| 1842 | if ((source == AUDIO_SOURCE_VOICE_RECOGNITION) && |
| 1843 | (inputDescriptor->mInputSource == AUDIO_SOURCE_HOTWORD)) { |
| 1844 | // FIXME: we should not assume that the first session is the active one and keep |
| 1845 | // activity count per session. Same in startInput(). |
| 1846 | ssize_t index = mSoundTriggerSessions.indexOfKey(inputDescriptor->mSessions.itemAt(0)); |
| 1847 | if (index >= 0) { |
| 1848 | return true; |
| 1849 | } |
| 1850 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1851 | } |
| 1852 | return false; |
| 1853 | } |
| 1854 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1855 | // Register a list of custom mixes with their attributes and format. |
| 1856 | // When a mix is registered, corresponding input and output profiles are |
| 1857 | // added to the remote submix hw module. The profile contains only the |
| 1858 | // parameters (sampling rate, format...) specified by the mix. |
| 1859 | // The corresponding input remote submix device is also connected. |
| 1860 | // |
| 1861 | // When a remote submix device is connected, the address is checked to select the |
| 1862 | // appropriate profile and the corresponding input or output stream is opened. |
| 1863 | // |
| 1864 | // When capture starts, getInputForAttr() will: |
| 1865 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 1866 | // - 2 if none found, getDeviceForInputSource() will: |
| 1867 | // - 2.1 look for a mix matching the attributes source |
| 1868 | // - 2.2 if none found, default to device selection by policy rules |
| 1869 | // At this time, the corresponding output remote submix device is also connected |
| 1870 | // and active playback use cases can be transferred to this mix if needed when reconnecting |
| 1871 | // after AudioTracks are invalidated |
| 1872 | // |
| 1873 | // When playback starts, getOutputForAttr() will: |
| 1874 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 1875 | // - 2 if none found, look for a mix matching the attributes usage |
| 1876 | // - 3 if none found, default to device and output selection by policy rules. |
| 1877 | |
| 1878 | status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes) |
| 1879 | { |
| 1880 | sp<HwModule> module; |
| 1881 | for (size_t i = 0; i < mHwModules.size(); i++) { |
| 1882 | if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 && |
| 1883 | mHwModules[i]->mHandle != 0) { |
| 1884 | module = mHwModules[i]; |
| 1885 | break; |
| 1886 | } |
| 1887 | } |
| 1888 | |
| 1889 | if (module == 0) { |
| 1890 | return INVALID_OPERATION; |
| 1891 | } |
| 1892 | |
| 1893 | ALOGV("registerPolicyMixes() num mixes %d", mixes.size()); |
| 1894 | |
| 1895 | for (size_t i = 0; i < mixes.size(); i++) { |
| 1896 | String8 address = mixes[i].mRegistrationId; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame^] | 1897 | |
| 1898 | if (mPolicyMixes.registerMix(address, mixes[i]) != NO_ERROR) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1899 | continue; |
| 1900 | } |
| 1901 | audio_config_t outputConfig = mixes[i].mFormat; |
| 1902 | audio_config_t inputConfig = mixes[i].mFormat; |
| 1903 | // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in |
| 1904 | // stereo and let audio flinger do the channel conversion if needed. |
| 1905 | outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 1906 | inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
| 1907 | module->addOutputProfile(address, &outputConfig, |
| 1908 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address); |
| 1909 | module->addInputProfile(address, &inputConfig, |
| 1910 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, address); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame^] | 1911 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1912 | if (mixes[i].mMixType == MIX_TYPE_PLAYERS) { |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 1913 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1914 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 1915 | address.string(), "remote-submix"); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1916 | } else { |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 1917 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1918 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 1919 | address.string(), "remote-submix"); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1920 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1921 | } |
| 1922 | return NO_ERROR; |
| 1923 | } |
| 1924 | |
| 1925 | status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes) |
| 1926 | { |
| 1927 | sp<HwModule> module; |
| 1928 | for (size_t i = 0; i < mHwModules.size(); i++) { |
| 1929 | if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 && |
| 1930 | mHwModules[i]->mHandle != 0) { |
| 1931 | module = mHwModules[i]; |
| 1932 | break; |
| 1933 | } |
| 1934 | } |
| 1935 | |
| 1936 | if (module == 0) { |
| 1937 | return INVALID_OPERATION; |
| 1938 | } |
| 1939 | |
| 1940 | ALOGV("unregisterPolicyMixes() num mixes %d", mixes.size()); |
| 1941 | |
| 1942 | for (size_t i = 0; i < mixes.size(); i++) { |
| 1943 | String8 address = mixes[i].mRegistrationId; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame^] | 1944 | |
| 1945 | if (mPolicyMixes.unregisterMix(address) != NO_ERROR) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1946 | continue; |
| 1947 | } |
| 1948 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1949 | if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) == |
| 1950 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) |
| 1951 | { |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 1952 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1953 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 1954 | address.string(), "remote-submix"); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1955 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1956 | |
| 1957 | if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) == |
| 1958 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) |
| 1959 | { |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 1960 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1961 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 1962 | address.string(), "remote-submix"); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1963 | } |
| 1964 | module->removeOutputProfile(address); |
| 1965 | module->removeInputProfile(address); |
| 1966 | } |
| 1967 | return NO_ERROR; |
| 1968 | } |
| 1969 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1970 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1971 | status_t AudioPolicyManager::dump(int fd) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1972 | { |
| 1973 | const size_t SIZE = 256; |
| 1974 | char buffer[SIZE]; |
| 1975 | String8 result; |
| 1976 | |
| 1977 | snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this); |
| 1978 | result.append(buffer); |
| 1979 | |
| 1980 | snprintf(buffer, SIZE, " Primary Output: %d\n", mPrimaryOutput); |
| 1981 | result.append(buffer); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1982 | snprintf(buffer, SIZE, " Phone state: %d\n", mPhoneState); |
| 1983 | result.append(buffer); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1984 | snprintf(buffer, SIZE, " Force use for communications %d\n", |
| 1985 | mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1986 | result.append(buffer); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1987 | 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] | 1988 | result.append(buffer); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1989 | 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] | 1990 | result.append(buffer); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1991 | 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] | 1992 | result.append(buffer); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1993 | 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] | 1994 | result.append(buffer); |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 1995 | snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n", |
| 1996 | mForceUse[AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO]); |
| 1997 | result.append(buffer); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1998 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1999 | mAvailableOutputDevices.dump(fd, String8("output")); |
| 2000 | mAvailableInputDevices.dump(fd, String8("input")); |
| 2001 | mHwModules.dump(fd); |
| 2002 | mOutputs.dump(fd); |
| 2003 | mInputs.dump(fd); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2004 | |
| 2005 | snprintf(buffer, SIZE, "\nStreams dump:\n"); |
| 2006 | write(fd, buffer, strlen(buffer)); |
| 2007 | snprintf(buffer, SIZE, |
| 2008 | " Stream Can be muted Index Min Index Max Index Cur [device : index]...\n"); |
| 2009 | write(fd, buffer, strlen(buffer)); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2010 | for (size_t i = 0; i < AUDIO_STREAM_CNT; i++) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2011 | snprintf(buffer, SIZE, " %02zu ", i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2012 | write(fd, buffer, strlen(buffer)); |
| 2013 | mStreams[i].dump(fd); |
| 2014 | } |
| 2015 | |
| 2016 | snprintf(buffer, SIZE, "\nTotal Effects CPU: %f MIPS, Total Effects memory: %d KB\n", |
| 2017 | (float)mTotalEffectsCpuLoad/10, mTotalEffectsMemory); |
| 2018 | write(fd, buffer, strlen(buffer)); |
| 2019 | |
| 2020 | snprintf(buffer, SIZE, "Registered effects:\n"); |
| 2021 | write(fd, buffer, strlen(buffer)); |
| 2022 | for (size_t i = 0; i < mEffects.size(); i++) { |
| 2023 | snprintf(buffer, SIZE, "- Effect %d dump:\n", mEffects.keyAt(i)); |
| 2024 | write(fd, buffer, strlen(buffer)); |
| 2025 | mEffects.valueAt(i)->dump(fd); |
| 2026 | } |
| 2027 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2028 | mAudioPatches.dump(fd); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2029 | |
| 2030 | return NO_ERROR; |
| 2031 | } |
| 2032 | |
| 2033 | // This function checks for the parameters which can be offloaded. |
| 2034 | // This can be enhanced depending on the capability of the DSP and policy |
| 2035 | // of the system. |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2036 | bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2037 | { |
| 2038 | ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2039 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2040 | offloadInfo.sample_rate, offloadInfo.channel_mask, |
| 2041 | offloadInfo.format, |
| 2042 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, |
| 2043 | offloadInfo.has_video); |
| 2044 | |
| 2045 | // Check if offload has been disabled |
| 2046 | char propValue[PROPERTY_VALUE_MAX]; |
| 2047 | if (property_get("audio.offload.disable", propValue, "0")) { |
| 2048 | if (atoi(propValue) != 0) { |
| 2049 | ALOGV("offload disabled by audio.offload.disable=%s", propValue ); |
| 2050 | return false; |
| 2051 | } |
| 2052 | } |
| 2053 | |
| 2054 | // Check if stream type is music, then only allow offload as of now. |
| 2055 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) |
| 2056 | { |
| 2057 | ALOGV("isOffloadSupported: stream_type != MUSIC, returning false"); |
| 2058 | return false; |
| 2059 | } |
| 2060 | |
| 2061 | //TODO: enable audio offloading with video when ready |
| 2062 | if (offloadInfo.has_video) |
| 2063 | { |
| 2064 | ALOGV("isOffloadSupported: has_video == true, returning false"); |
| 2065 | return false; |
| 2066 | } |
| 2067 | |
| 2068 | //If duration is less than minimum value defined in property, return false |
| 2069 | if (property_get("audio.offload.min.duration.secs", propValue, NULL)) { |
| 2070 | if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) { |
| 2071 | ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue); |
| 2072 | return false; |
| 2073 | } |
| 2074 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { |
| 2075 | ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS); |
| 2076 | return false; |
| 2077 | } |
| 2078 | |
| 2079 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 2080 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 2081 | // detects there is an active non offloadable effect. |
| 2082 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 2083 | // This may prevent offloading in rare situations where effects are left active by apps |
| 2084 | // in the background. |
| 2085 | if (isNonOffloadableEffectEnabled()) { |
| 2086 | return false; |
| 2087 | } |
| 2088 | |
| 2089 | // See if there is a profile to support this. |
| 2090 | // AUDIO_DEVICE_NONE |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2091 | sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2092 | offloadInfo.sample_rate, |
| 2093 | offloadInfo.format, |
| 2094 | offloadInfo.channel_mask, |
| 2095 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2096 | ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT "); |
| 2097 | return (profile != 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2098 | } |
| 2099 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2100 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, |
| 2101 | audio_port_type_t type, |
| 2102 | unsigned int *num_ports, |
| 2103 | struct audio_port *ports, |
| 2104 | unsigned int *generation) |
| 2105 | { |
| 2106 | if (num_ports == NULL || (*num_ports != 0 && ports == NULL) || |
| 2107 | generation == NULL) { |
| 2108 | return BAD_VALUE; |
| 2109 | } |
| 2110 | ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports); |
| 2111 | if (ports == NULL) { |
| 2112 | *num_ports = 0; |
| 2113 | } |
| 2114 | |
| 2115 | size_t portsWritten = 0; |
| 2116 | size_t portsMax = *num_ports; |
| 2117 | *num_ports = 0; |
| 2118 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { |
| 2119 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 2120 | for (size_t i = 0; |
| 2121 | i < mAvailableOutputDevices.size() && portsWritten < portsMax; i++) { |
| 2122 | mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]); |
| 2123 | } |
| 2124 | *num_ports += mAvailableOutputDevices.size(); |
| 2125 | } |
| 2126 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
| 2127 | for (size_t i = 0; |
| 2128 | i < mAvailableInputDevices.size() && portsWritten < portsMax; i++) { |
| 2129 | mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]); |
| 2130 | } |
| 2131 | *num_ports += mAvailableInputDevices.size(); |
| 2132 | } |
| 2133 | } |
| 2134 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { |
| 2135 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 2136 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { |
| 2137 | mInputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2138 | } |
| 2139 | *num_ports += mInputs.size(); |
| 2140 | } |
| 2141 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2142 | size_t numOutputs = 0; |
| 2143 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2144 | if (!mOutputs[i]->isDuplicated()) { |
| 2145 | numOutputs++; |
| 2146 | if (portsWritten < portsMax) { |
| 2147 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2148 | } |
| 2149 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2150 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2151 | *num_ports += numOutputs; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2152 | } |
| 2153 | } |
| 2154 | *generation = curAudioPortGeneration(); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2155 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2156 | return NO_ERROR; |
| 2157 | } |
| 2158 | |
| 2159 | status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused) |
| 2160 | { |
| 2161 | return NO_ERROR; |
| 2162 | } |
| 2163 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2164 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, |
| 2165 | audio_patch_handle_t *handle, |
| 2166 | uid_t uid) |
| 2167 | { |
| 2168 | ALOGV("createAudioPatch()"); |
| 2169 | |
| 2170 | if (handle == NULL || patch == NULL) { |
| 2171 | return BAD_VALUE; |
| 2172 | } |
| 2173 | ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks); |
| 2174 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2175 | if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX || |
| 2176 | patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) { |
| 2177 | return BAD_VALUE; |
| 2178 | } |
| 2179 | // only one source per audio patch supported for now |
| 2180 | if (patch->num_sources > 1) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2181 | return INVALID_OPERATION; |
| 2182 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2183 | |
| 2184 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2185 | return INVALID_OPERATION; |
| 2186 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2187 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2188 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { |
| 2189 | return INVALID_OPERATION; |
| 2190 | } |
| 2191 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2192 | |
| 2193 | sp<AudioPatch> patchDesc; |
| 2194 | ssize_t index = mAudioPatches.indexOfKey(*handle); |
| 2195 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2196 | ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id, |
| 2197 | patch->sources[0].role, |
| 2198 | patch->sources[0].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2199 | #if LOG_NDEBUG == 0 |
| 2200 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2201 | ALOGV("createAudioPatch sink %d: id %d role %d type %d", i, patch->sinks[i].id, |
| 2202 | patch->sinks[i].role, |
| 2203 | patch->sinks[i].type); |
| 2204 | } |
| 2205 | #endif |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2206 | |
| 2207 | if (index >= 0) { |
| 2208 | patchDesc = mAudioPatches.valueAt(index); |
| 2209 | ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 2210 | mUidCached, patchDesc->mUid, uid); |
| 2211 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 2212 | return INVALID_OPERATION; |
| 2213 | } |
| 2214 | } else { |
| 2215 | *handle = 0; |
| 2216 | } |
| 2217 | |
| 2218 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2219 | sp<AudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2220 | if (outputDesc == NULL) { |
| 2221 | ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id); |
| 2222 | return BAD_VALUE; |
| 2223 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2224 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", |
| 2225 | outputDesc->mIoHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2226 | if (patchDesc != 0) { |
| 2227 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
| 2228 | ALOGV("createAudioPatch() source id differs for patch current id %d new id %d", |
| 2229 | patchDesc->mPatch.sources[0].id, patch->sources[0].id); |
| 2230 | return BAD_VALUE; |
| 2231 | } |
| 2232 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2233 | DeviceVector devices; |
| 2234 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2235 | // Only support mix to devices connection |
| 2236 | // TODO add support for mix to mix connection |
| 2237 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 2238 | ALOGV("createAudioPatch() source mix but sink is not a device"); |
| 2239 | return INVALID_OPERATION; |
| 2240 | } |
| 2241 | sp<DeviceDescriptor> devDesc = |
| 2242 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 2243 | if (devDesc == 0) { |
| 2244 | ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id); |
| 2245 | return BAD_VALUE; |
| 2246 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2247 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2248 | if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2249 | devDesc->mAddress, |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2250 | patch->sources[0].sample_rate, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2251 | NULL, // updatedSamplingRate |
| 2252 | patch->sources[0].format, |
| 2253 | patch->sources[0].channel_mask, |
| 2254 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { |
| 2255 | ALOGV("createAudioPatch() profile not supported for device %08x", devDesc->type()); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2256 | return INVALID_OPERATION; |
| 2257 | } |
| 2258 | devices.add(devDesc); |
| 2259 | } |
| 2260 | if (devices.size() == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2261 | return INVALID_OPERATION; |
| 2262 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2263 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2264 | // TODO: reconfigure output format and channels here |
| 2265 | ALOGV("createAudioPatch() setting device %08x on output %d", |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2266 | devices.types(), outputDesc->mIoHandle); |
| 2267 | setOutputDevice(outputDesc->mIoHandle, devices.types(), true, 0, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2268 | index = mAudioPatches.indexOfKey(*handle); |
| 2269 | if (index >= 0) { |
| 2270 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 2271 | ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided"); |
| 2272 | } |
| 2273 | patchDesc = mAudioPatches.valueAt(index); |
| 2274 | patchDesc->mUid = uid; |
| 2275 | ALOGV("createAudioPatch() success"); |
| 2276 | } else { |
| 2277 | ALOGW("createAudioPatch() setOutputDevice() failed to create a patch"); |
| 2278 | return INVALID_OPERATION; |
| 2279 | } |
| 2280 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2281 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 2282 | // input device to input mix connection |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2283 | // only one sink supported when connecting an input device to a mix |
| 2284 | if (patch->num_sinks > 1) { |
| 2285 | return INVALID_OPERATION; |
| 2286 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2287 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2288 | if (inputDesc == NULL) { |
| 2289 | return BAD_VALUE; |
| 2290 | } |
| 2291 | if (patchDesc != 0) { |
| 2292 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { |
| 2293 | return BAD_VALUE; |
| 2294 | } |
| 2295 | } |
| 2296 | sp<DeviceDescriptor> devDesc = |
| 2297 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
| 2298 | if (devDesc == 0) { |
| 2299 | return BAD_VALUE; |
| 2300 | } |
| 2301 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2302 | if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2303 | devDesc->mAddress, |
| 2304 | patch->sinks[0].sample_rate, |
| 2305 | NULL, /*updatedSampleRate*/ |
| 2306 | patch->sinks[0].format, |
| 2307 | patch->sinks[0].channel_mask, |
| 2308 | // FIXME for the parameter type, |
| 2309 | // and the NONE |
| 2310 | (audio_output_flags_t) |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2311 | AUDIO_INPUT_FLAG_NONE)) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2312 | return INVALID_OPERATION; |
| 2313 | } |
| 2314 | // TODO: reconfigure output format and channels here |
| 2315 | ALOGV("createAudioPatch() setting device %08x on output %d", |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2316 | devDesc->type(), inputDesc->mIoHandle); |
| 2317 | setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2318 | index = mAudioPatches.indexOfKey(*handle); |
| 2319 | if (index >= 0) { |
| 2320 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 2321 | ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided"); |
| 2322 | } |
| 2323 | patchDesc = mAudioPatches.valueAt(index); |
| 2324 | patchDesc->mUid = uid; |
| 2325 | ALOGV("createAudioPatch() success"); |
| 2326 | } else { |
| 2327 | ALOGW("createAudioPatch() setInputDevice() failed to create a patch"); |
| 2328 | return INVALID_OPERATION; |
| 2329 | } |
| 2330 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2331 | // device to device connection |
| 2332 | if (patchDesc != 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2333 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2334 | return BAD_VALUE; |
| 2335 | } |
| 2336 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2337 | sp<DeviceDescriptor> srcDeviceDesc = |
| 2338 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 2339 | if (srcDeviceDesc == 0) { |
| 2340 | return BAD_VALUE; |
| 2341 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2342 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2343 | //update source and sink with our own data as the data passed in the patch may |
| 2344 | // be incomplete. |
| 2345 | struct audio_patch newPatch = *patch; |
| 2346 | srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2347 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2348 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2349 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 2350 | ALOGV("createAudioPatch() source device but one sink is not a device"); |
| 2351 | return INVALID_OPERATION; |
| 2352 | } |
| 2353 | |
| 2354 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 2355 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 2356 | if (sinkDeviceDesc == 0) { |
| 2357 | return BAD_VALUE; |
| 2358 | } |
| 2359 | sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]); |
| 2360 | |
| 2361 | if (srcDeviceDesc->mModule != sinkDeviceDesc->mModule) { |
| 2362 | // only one sink supported when connected devices across HW modules |
| 2363 | if (patch->num_sinks > 1) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2364 | return INVALID_OPERATION; |
| 2365 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2366 | SortedVector<audio_io_handle_t> outputs = |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2367 | getOutputsForDevice(sinkDeviceDesc->type(), mOutputs); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2368 | // if the sink device is reachable via an opened output stream, request to go via |
| 2369 | // this output stream by adding a second source to the patch description |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 2370 | audio_io_handle_t output = selectOutput(outputs, |
| 2371 | AUDIO_OUTPUT_FLAG_NONE, |
| 2372 | AUDIO_FORMAT_INVALID); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2373 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 2374 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 2375 | if (outputDesc->isDuplicated()) { |
| 2376 | return INVALID_OPERATION; |
| 2377 | } |
| 2378 | outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]); |
| 2379 | newPatch.num_sources = 2; |
| 2380 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2381 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2382 | } |
| 2383 | // TODO: check from routing capabilities in config file and other conflicting patches |
| 2384 | |
| 2385 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 2386 | if (index >= 0) { |
| 2387 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 2388 | } |
| 2389 | |
| 2390 | status_t status = mpClientInterface->createAudioPatch(&newPatch, |
| 2391 | &afPatchHandle, |
| 2392 | 0); |
| 2393 | ALOGV("createAudioPatch() patch panel returned %d patchHandle %d", |
| 2394 | status, afPatchHandle); |
| 2395 | if (status == NO_ERROR) { |
| 2396 | if (index < 0) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 2397 | patchDesc = new AudioPatch(&newPatch, uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2398 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 2399 | } else { |
| 2400 | patchDesc->mPatch = newPatch; |
| 2401 | } |
| 2402 | patchDesc->mAfPatchHandle = afPatchHandle; |
| 2403 | *handle = patchDesc->mHandle; |
| 2404 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2405 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2406 | } else { |
| 2407 | ALOGW("createAudioPatch() patch panel could not connect device patch, error %d", |
| 2408 | status); |
| 2409 | return INVALID_OPERATION; |
| 2410 | } |
| 2411 | } else { |
| 2412 | return BAD_VALUE; |
| 2413 | } |
| 2414 | } else { |
| 2415 | return BAD_VALUE; |
| 2416 | } |
| 2417 | return NO_ERROR; |
| 2418 | } |
| 2419 | |
| 2420 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, |
| 2421 | uid_t uid) |
| 2422 | { |
| 2423 | ALOGV("releaseAudioPatch() patch %d", handle); |
| 2424 | |
| 2425 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 2426 | |
| 2427 | if (index < 0) { |
| 2428 | return BAD_VALUE; |
| 2429 | } |
| 2430 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 2431 | ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 2432 | mUidCached, patchDesc->mUid, uid); |
| 2433 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 2434 | return INVALID_OPERATION; |
| 2435 | } |
| 2436 | |
| 2437 | struct audio_patch *patch = &patchDesc->mPatch; |
| 2438 | patchDesc->mUid = mUidCached; |
| 2439 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2440 | sp<AudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2441 | if (outputDesc == NULL) { |
| 2442 | ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id); |
| 2443 | return BAD_VALUE; |
| 2444 | } |
| 2445 | |
| 2446 | setOutputDevice(outputDesc->mIoHandle, |
| 2447 | getNewOutputDevice(outputDesc->mIoHandle, true /*fromCache*/), |
| 2448 | true, |
| 2449 | 0, |
| 2450 | NULL); |
| 2451 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2452 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2453 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2454 | if (inputDesc == NULL) { |
| 2455 | ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id); |
| 2456 | return BAD_VALUE; |
| 2457 | } |
| 2458 | setInputDevice(inputDesc->mIoHandle, |
| 2459 | getNewInputDevice(inputDesc->mIoHandle), |
| 2460 | true, |
| 2461 | NULL); |
| 2462 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2463 | audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle; |
| 2464 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 2465 | ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d", |
| 2466 | status, patchDesc->mAfPatchHandle); |
| 2467 | removeAudioPatch(patchDesc->mHandle); |
| 2468 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2469 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2470 | } else { |
| 2471 | return BAD_VALUE; |
| 2472 | } |
| 2473 | } else { |
| 2474 | return BAD_VALUE; |
| 2475 | } |
| 2476 | return NO_ERROR; |
| 2477 | } |
| 2478 | |
| 2479 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, |
| 2480 | struct audio_patch *patches, |
| 2481 | unsigned int *generation) |
| 2482 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2483 | if (generation == NULL) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2484 | return BAD_VALUE; |
| 2485 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2486 | *generation = curAudioPortGeneration(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2487 | return mAudioPatches.listAudioPatches(num_patches, patches); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2488 | } |
| 2489 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2490 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2491 | { |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2492 | ALOGV("setAudioPortConfig()"); |
| 2493 | |
| 2494 | if (config == NULL) { |
| 2495 | return BAD_VALUE; |
| 2496 | } |
| 2497 | ALOGV("setAudioPortConfig() on port handle %d", config->id); |
| 2498 | // Only support gain configuration for now |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2499 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { |
| 2500 | return INVALID_OPERATION; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2501 | } |
| 2502 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2503 | sp<AudioPortConfig> audioPortConfig; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2504 | if (config->type == AUDIO_PORT_TYPE_MIX) { |
| 2505 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2506 | sp<AudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2507 | if (outputDesc == NULL) { |
| 2508 | return BAD_VALUE; |
| 2509 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2510 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 2511 | "setAudioPortConfig() called on duplicated output %d", |
| 2512 | outputDesc->mIoHandle); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2513 | audioPortConfig = outputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2514 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2515 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2516 | if (inputDesc == NULL) { |
| 2517 | return BAD_VALUE; |
| 2518 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2519 | audioPortConfig = inputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2520 | } else { |
| 2521 | return BAD_VALUE; |
| 2522 | } |
| 2523 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { |
| 2524 | sp<DeviceDescriptor> deviceDesc; |
| 2525 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
| 2526 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); |
| 2527 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
| 2528 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); |
| 2529 | } else { |
| 2530 | return BAD_VALUE; |
| 2531 | } |
| 2532 | if (deviceDesc == NULL) { |
| 2533 | return BAD_VALUE; |
| 2534 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2535 | audioPortConfig = deviceDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2536 | } else { |
| 2537 | return BAD_VALUE; |
| 2538 | } |
| 2539 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2540 | struct audio_port_config backupConfig; |
| 2541 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); |
| 2542 | if (status == NO_ERROR) { |
| 2543 | struct audio_port_config newConfig; |
| 2544 | audioPortConfig->toAudioPortConfig(&newConfig, config); |
| 2545 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2546 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2547 | if (status != NO_ERROR) { |
| 2548 | audioPortConfig->applyAudioPortConfig(&backupConfig); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2549 | } |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2550 | |
| 2551 | return status; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2552 | } |
| 2553 | |
| 2554 | void AudioPolicyManager::clearAudioPatches(uid_t uid) |
| 2555 | { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 2556 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2557 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 2558 | if (patchDesc->mUid == uid) { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 2559 | releaseAudioPatch(mAudioPatches.keyAt(i), uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2560 | } |
| 2561 | } |
| 2562 | } |
| 2563 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2564 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, |
| 2565 | audio_io_handle_t *ioHandle, |
| 2566 | audio_devices_t *device) |
| 2567 | { |
| 2568 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(); |
| 2569 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(); |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 2570 | *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2571 | |
| 2572 | mSoundTriggerSessions.add(*session, *ioHandle); |
| 2573 | |
| 2574 | return NO_ERROR; |
| 2575 | } |
| 2576 | |
| 2577 | status_t AudioPolicyManager::releaseSoundTriggerSession(audio_session_t session) |
| 2578 | { |
| 2579 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); |
| 2580 | if (index < 0) { |
| 2581 | ALOGW("acquireSoundTriggerSession() session %d not registered", session); |
| 2582 | return BAD_VALUE; |
| 2583 | } |
| 2584 | |
| 2585 | mSoundTriggerSessions.removeItem(session); |
| 2586 | return NO_ERROR; |
| 2587 | } |
| 2588 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2589 | // ---------------------------------------------------------------------------- |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2590 | // AudioPolicyManager |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2591 | // ---------------------------------------------------------------------------- |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2592 | uint32_t AudioPolicyManager::nextAudioPortGeneration() |
| 2593 | { |
| 2594 | return android_atomic_inc(&mAudioPortGeneration); |
| 2595 | } |
| 2596 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2597 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2598 | : |
| 2599 | #ifdef AUDIO_POLICY_TEST |
| 2600 | Thread(false), |
| 2601 | #endif //AUDIO_POLICY_TEST |
| 2602 | mPrimaryOutput((audio_io_handle_t)0), |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2603 | mPhoneState(AUDIO_MODE_NORMAL), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2604 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), |
| 2605 | mTotalEffectsCpuLoad(0), mTotalEffectsMemory(0), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2606 | mA2dpSuspended(false), |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 2607 | mSpeakerDrcEnabled(false), |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2608 | mAudioPortGeneration(1), |
| 2609 | mBeaconMuteRefCount(0), |
| 2610 | mBeaconPlayingRefCount(0), |
| 2611 | mBeaconMuted(false) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2612 | { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2613 | mUidCached = getuid(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2614 | mpClientInterface = clientInterface; |
| 2615 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2616 | for (int i = 0; i < AUDIO_POLICY_FORCE_USE_CNT; i++) { |
| 2617 | mForceUse[i] = AUDIO_POLICY_FORCE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2618 | } |
| 2619 | |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 2620 | mDefaultOutputDevice = new DeviceDescriptor(String8("Speaker"), AUDIO_DEVICE_OUT_SPEAKER); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2621 | if (ConfigParsingUtils::loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, |
| 2622 | mHwModules, mAvailableInputDevices, mAvailableOutputDevices, |
| 2623 | mDefaultOutputDevice, mSpeakerDrcEnabled) != NO_ERROR) { |
| 2624 | if (ConfigParsingUtils::loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE, |
| 2625 | mHwModules, mAvailableInputDevices, mAvailableOutputDevices, |
| 2626 | mDefaultOutputDevice, mSpeakerDrcEnabled) != NO_ERROR) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2627 | ALOGE("could not load audio policy configuration file, setting defaults"); |
| 2628 | defaultAudioPolicyConfig(); |
| 2629 | } |
| 2630 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2631 | // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2632 | |
| 2633 | // must be done after reading the policy |
| 2634 | initializeVolumeCurves(); |
| 2635 | |
| 2636 | // open all output streams needed to access attached devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2637 | audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types(); |
| 2638 | audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2639 | for (size_t i = 0; i < mHwModules.size(); i++) { |
| 2640 | mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName); |
| 2641 | if (mHwModules[i]->mHandle == 0) { |
| 2642 | ALOGW("could not open HW module %s", mHwModules[i]->mName); |
| 2643 | continue; |
| 2644 | } |
| 2645 | // open all output streams needed to access attached devices |
| 2646 | // except for direct output streams that are only opened when they are actually |
| 2647 | // required by an app. |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2648 | // This also validates mAvailableOutputDevices list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2649 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 2650 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2651 | const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2652 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2653 | if (outProfile->mSupportedDevices.isEmpty()) { |
| 2654 | ALOGW("Output profile contains no device on module %s", mHwModules[i]->mName); |
| 2655 | continue; |
| 2656 | } |
| 2657 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2658 | if ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { |
| 2659 | continue; |
| 2660 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2661 | audio_devices_t profileType = outProfile->mSupportedDevices.types(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2662 | if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) { |
| 2663 | profileType = mDefaultOutputDevice->type(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2664 | } else { |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2665 | // chose first device present in mSupportedDevices also part of |
| 2666 | // outputDeviceTypes |
| 2667 | for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2668 | profileType = outProfile->mSupportedDevices[k]->type(); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2669 | if ((profileType & outputDeviceTypes) != 0) { |
| 2670 | break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2671 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2672 | } |
| 2673 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2674 | if ((profileType & outputDeviceTypes) == 0) { |
| 2675 | continue; |
| 2676 | } |
| 2677 | sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(outProfile); |
| 2678 | |
| 2679 | outputDesc->mDevice = profileType; |
| 2680 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 2681 | config.sample_rate = outputDesc->mSamplingRate; |
| 2682 | config.channel_mask = outputDesc->mChannelMask; |
| 2683 | config.format = outputDesc->mFormat; |
| 2684 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 2685 | status_t status = mpClientInterface->openOutput(outProfile->mModule->mHandle, |
| 2686 | &output, |
| 2687 | &config, |
| 2688 | &outputDesc->mDevice, |
| 2689 | String8(""), |
| 2690 | &outputDesc->mLatency, |
| 2691 | outputDesc->mFlags); |
| 2692 | |
| 2693 | if (status != NO_ERROR) { |
| 2694 | ALOGW("Cannot open output stream for device %08x on hw module %s", |
| 2695 | outputDesc->mDevice, |
| 2696 | mHwModules[i]->mName); |
| 2697 | } else { |
| 2698 | outputDesc->mSamplingRate = config.sample_rate; |
| 2699 | outputDesc->mChannelMask = config.channel_mask; |
| 2700 | outputDesc->mFormat = config.format; |
| 2701 | |
| 2702 | for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2703 | audio_devices_t type = outProfile->mSupportedDevices[k]->type(); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2704 | ssize_t index = |
| 2705 | mAvailableOutputDevices.indexOf(outProfile->mSupportedDevices[k]); |
| 2706 | // give a valid ID to an attached device once confirmed it is reachable |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 2707 | if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) { |
| 2708 | mAvailableOutputDevices[index]->attach(mHwModules[i]); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2709 | } |
| 2710 | } |
| 2711 | if (mPrimaryOutput == 0 && |
| 2712 | outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) { |
| 2713 | mPrimaryOutput = output; |
| 2714 | } |
| 2715 | addOutput(output, outputDesc); |
| 2716 | setOutputDevice(output, |
| 2717 | outputDesc->mDevice, |
| 2718 | true); |
| 2719 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2720 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2721 | // open input streams needed to access attached devices to validate |
| 2722 | // mAvailableInputDevices list |
| 2723 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) |
| 2724 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2725 | const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2726 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2727 | if (inProfile->mSupportedDevices.isEmpty()) { |
| 2728 | ALOGW("Input profile contains no device on module %s", mHwModules[i]->mName); |
| 2729 | continue; |
| 2730 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2731 | // chose first device present in mSupportedDevices also part of |
| 2732 | // inputDeviceTypes |
| 2733 | audio_devices_t profileType = AUDIO_DEVICE_NONE; |
| 2734 | for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2735 | profileType = inProfile->mSupportedDevices[k]->type(); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2736 | if (profileType & inputDeviceTypes) { |
| 2737 | break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2738 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2739 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2740 | if ((profileType & inputDeviceTypes) == 0) { |
| 2741 | continue; |
| 2742 | } |
| 2743 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(inProfile); |
| 2744 | |
| 2745 | inputDesc->mInputSource = AUDIO_SOURCE_MIC; |
| 2746 | inputDesc->mDevice = profileType; |
| 2747 | |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 2748 | // find the address |
| 2749 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType); |
| 2750 | // the inputs vector must be of size 1, but we don't want to crash here |
| 2751 | String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress |
| 2752 | : String8(""); |
| 2753 | ALOGV(" for input device 0x%x using address %s", profileType, address.string()); |
| 2754 | ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!"); |
| 2755 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2756 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 2757 | config.sample_rate = inputDesc->mSamplingRate; |
| 2758 | config.channel_mask = inputDesc->mChannelMask; |
| 2759 | config.format = inputDesc->mFormat; |
| 2760 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 2761 | status_t status = mpClientInterface->openInput(inProfile->mModule->mHandle, |
| 2762 | &input, |
| 2763 | &config, |
| 2764 | &inputDesc->mDevice, |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 2765 | address, |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2766 | AUDIO_SOURCE_MIC, |
| 2767 | AUDIO_INPUT_FLAG_NONE); |
| 2768 | |
| 2769 | if (status == NO_ERROR) { |
| 2770 | for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2771 | audio_devices_t type = inProfile->mSupportedDevices[k]->type(); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2772 | ssize_t index = |
| 2773 | mAvailableInputDevices.indexOf(inProfile->mSupportedDevices[k]); |
| 2774 | // give a valid ID to an attached device once confirmed it is reachable |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 2775 | if (index >= 0 && !mAvailableInputDevices[index]->isAttached()) { |
| 2776 | mAvailableInputDevices[index]->attach(mHwModules[i]); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2777 | } |
| 2778 | } |
| 2779 | mpClientInterface->closeInput(input); |
| 2780 | } else { |
| 2781 | ALOGW("Cannot open input stream for device %08x on hw module %s", |
| 2782 | inputDesc->mDevice, |
| 2783 | mHwModules[i]->mName); |
| 2784 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2785 | } |
| 2786 | } |
| 2787 | // make sure all attached devices have been allocated a unique ID |
| 2788 | for (size_t i = 0; i < mAvailableOutputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 2789 | if (!mAvailableOutputDevices[i]->isAttached()) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2790 | ALOGW("Input device %08x unreachable", mAvailableOutputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2791 | mAvailableOutputDevices.remove(mAvailableOutputDevices[i]); |
| 2792 | continue; |
| 2793 | } |
| 2794 | i++; |
| 2795 | } |
| 2796 | for (size_t i = 0; i < mAvailableInputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 2797 | if (!mAvailableInputDevices[i]->isAttached()) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2798 | ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2799 | mAvailableInputDevices.remove(mAvailableInputDevices[i]); |
| 2800 | continue; |
| 2801 | } |
| 2802 | i++; |
| 2803 | } |
| 2804 | // make sure default device is reachable |
| 2805 | if (mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2806 | ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2807 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2808 | |
| 2809 | ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output"); |
| 2810 | |
| 2811 | updateDevicesAndOutputs(); |
| 2812 | |
| 2813 | #ifdef AUDIO_POLICY_TEST |
| 2814 | if (mPrimaryOutput != 0) { |
| 2815 | AudioParameter outputCmd = AudioParameter(); |
| 2816 | outputCmd.addInt(String8("set_id"), 0); |
| 2817 | mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString()); |
| 2818 | |
| 2819 | mTestDevice = AUDIO_DEVICE_OUT_SPEAKER; |
| 2820 | mTestSamplingRate = 44100; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2821 | mTestFormat = AUDIO_FORMAT_PCM_16_BIT; |
| 2822 | mTestChannels = AUDIO_CHANNEL_OUT_STEREO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2823 | mTestLatencyMs = 0; |
| 2824 | mCurOutput = 0; |
| 2825 | mDirectOutput = false; |
| 2826 | for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { |
| 2827 | mTestOutputs[i] = 0; |
| 2828 | } |
| 2829 | |
| 2830 | const size_t SIZE = 256; |
| 2831 | char buffer[SIZE]; |
| 2832 | snprintf(buffer, SIZE, "AudioPolicyManagerTest"); |
| 2833 | run(buffer, ANDROID_PRIORITY_AUDIO); |
| 2834 | } |
| 2835 | #endif //AUDIO_POLICY_TEST |
| 2836 | } |
| 2837 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2838 | AudioPolicyManager::~AudioPolicyManager() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2839 | { |
| 2840 | #ifdef AUDIO_POLICY_TEST |
| 2841 | exit(); |
| 2842 | #endif //AUDIO_POLICY_TEST |
| 2843 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2844 | mpClientInterface->closeOutput(mOutputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2845 | } |
| 2846 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 2847 | mpClientInterface->closeInput(mInputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2848 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2849 | mAvailableOutputDevices.clear(); |
| 2850 | mAvailableInputDevices.clear(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2851 | mOutputs.clear(); |
| 2852 | mInputs.clear(); |
| 2853 | mHwModules.clear(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2854 | } |
| 2855 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2856 | status_t AudioPolicyManager::initCheck() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2857 | { |
| 2858 | return (mPrimaryOutput == 0) ? NO_INIT : NO_ERROR; |
| 2859 | } |
| 2860 | |
| 2861 | #ifdef AUDIO_POLICY_TEST |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2862 | bool AudioPolicyManager::threadLoop() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2863 | { |
| 2864 | ALOGV("entering threadLoop()"); |
| 2865 | while (!exitPending()) |
| 2866 | { |
| 2867 | String8 command; |
| 2868 | int valueInt; |
| 2869 | String8 value; |
| 2870 | |
| 2871 | Mutex::Autolock _l(mLock); |
| 2872 | mWaitWorkCV.waitRelative(mLock, milliseconds(50)); |
| 2873 | |
| 2874 | command = mpClientInterface->getParameters(0, String8("test_cmd_policy")); |
| 2875 | AudioParameter param = AudioParameter(command); |
| 2876 | |
| 2877 | if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR && |
| 2878 | valueInt != 0) { |
| 2879 | ALOGV("Test command %s received", command.string()); |
| 2880 | String8 target; |
| 2881 | if (param.get(String8("target"), target) != NO_ERROR) { |
| 2882 | target = "Manager"; |
| 2883 | } |
| 2884 | if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) { |
| 2885 | param.remove(String8("test_cmd_policy_output")); |
| 2886 | mCurOutput = valueInt; |
| 2887 | } |
| 2888 | if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) { |
| 2889 | param.remove(String8("test_cmd_policy_direct")); |
| 2890 | if (value == "false") { |
| 2891 | mDirectOutput = false; |
| 2892 | } else if (value == "true") { |
| 2893 | mDirectOutput = true; |
| 2894 | } |
| 2895 | } |
| 2896 | if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) { |
| 2897 | param.remove(String8("test_cmd_policy_input")); |
| 2898 | mTestInput = valueInt; |
| 2899 | } |
| 2900 | |
| 2901 | if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) { |
| 2902 | param.remove(String8("test_cmd_policy_format")); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2903 | int format = AUDIO_FORMAT_INVALID; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2904 | if (value == "PCM 16 bits") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2905 | format = AUDIO_FORMAT_PCM_16_BIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2906 | } else if (value == "PCM 8 bits") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2907 | format = AUDIO_FORMAT_PCM_8_BIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2908 | } else if (value == "Compressed MP3") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2909 | format = AUDIO_FORMAT_MP3; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2910 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2911 | if (format != AUDIO_FORMAT_INVALID) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2912 | if (target == "Manager") { |
| 2913 | mTestFormat = format; |
| 2914 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 2915 | AudioParameter outputParam = AudioParameter(); |
| 2916 | outputParam.addInt(String8("format"), format); |
| 2917 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 2918 | } |
| 2919 | } |
| 2920 | } |
| 2921 | if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) { |
| 2922 | param.remove(String8("test_cmd_policy_channels")); |
| 2923 | int channels = 0; |
| 2924 | |
| 2925 | if (value == "Channels Stereo") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2926 | channels = AUDIO_CHANNEL_OUT_STEREO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2927 | } else if (value == "Channels Mono") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2928 | channels = AUDIO_CHANNEL_OUT_MONO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2929 | } |
| 2930 | if (channels != 0) { |
| 2931 | if (target == "Manager") { |
| 2932 | mTestChannels = channels; |
| 2933 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 2934 | AudioParameter outputParam = AudioParameter(); |
| 2935 | outputParam.addInt(String8("channels"), channels); |
| 2936 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 2937 | } |
| 2938 | } |
| 2939 | } |
| 2940 | if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) { |
| 2941 | param.remove(String8("test_cmd_policy_sampleRate")); |
| 2942 | if (valueInt >= 0 && valueInt <= 96000) { |
| 2943 | int samplingRate = valueInt; |
| 2944 | if (target == "Manager") { |
| 2945 | mTestSamplingRate = samplingRate; |
| 2946 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 2947 | AudioParameter outputParam = AudioParameter(); |
| 2948 | outputParam.addInt(String8("sampling_rate"), samplingRate); |
| 2949 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 2950 | } |
| 2951 | } |
| 2952 | } |
| 2953 | |
| 2954 | if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) { |
| 2955 | param.remove(String8("test_cmd_policy_reopen")); |
| 2956 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2957 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(mPrimaryOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2958 | mpClientInterface->closeOutput(mPrimaryOutput); |
| 2959 | |
| 2960 | audio_module_handle_t moduleHandle = outputDesc->mModule->mHandle; |
| 2961 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2962 | removeOutput(mPrimaryOutput); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2963 | sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2964 | outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2965 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 2966 | config.sample_rate = outputDesc->mSamplingRate; |
| 2967 | config.channel_mask = outputDesc->mChannelMask; |
| 2968 | config.format = outputDesc->mFormat; |
| 2969 | status_t status = mpClientInterface->openOutput(moduleHandle, |
| 2970 | &mPrimaryOutput, |
| 2971 | &config, |
| 2972 | &outputDesc->mDevice, |
| 2973 | String8(""), |
| 2974 | &outputDesc->mLatency, |
| 2975 | outputDesc->mFlags); |
| 2976 | if (status != NO_ERROR) { |
| 2977 | ALOGE("Failed to reopen hardware output stream, " |
| 2978 | "samplingRate: %d, format %d, channels %d", |
| 2979 | outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2980 | } else { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2981 | outputDesc->mSamplingRate = config.sample_rate; |
| 2982 | outputDesc->mChannelMask = config.channel_mask; |
| 2983 | outputDesc->mFormat = config.format; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2984 | AudioParameter outputCmd = AudioParameter(); |
| 2985 | outputCmd.addInt(String8("set_id"), 0); |
| 2986 | mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString()); |
| 2987 | addOutput(mPrimaryOutput, outputDesc); |
| 2988 | } |
| 2989 | } |
| 2990 | |
| 2991 | |
| 2992 | mpClientInterface->setParameters(0, String8("test_cmd_policy=")); |
| 2993 | } |
| 2994 | } |
| 2995 | return false; |
| 2996 | } |
| 2997 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2998 | void AudioPolicyManager::exit() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2999 | { |
| 3000 | { |
| 3001 | AutoMutex _l(mLock); |
| 3002 | requestExit(); |
| 3003 | mWaitWorkCV.signal(); |
| 3004 | } |
| 3005 | requestExitAndWait(); |
| 3006 | } |
| 3007 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3008 | int AudioPolicyManager::testOutputIndex(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3009 | { |
| 3010 | for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { |
| 3011 | if (output == mTestOutputs[i]) return i; |
| 3012 | } |
| 3013 | return 0; |
| 3014 | } |
| 3015 | #endif //AUDIO_POLICY_TEST |
| 3016 | |
| 3017 | // --- |
| 3018 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3019 | void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<AudioOutputDescriptor> outputDesc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3020 | { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 3021 | outputDesc->setIoHandle(output); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3022 | mOutputs.add(output, outputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3023 | nextAudioPortGeneration(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3024 | } |
| 3025 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3026 | void AudioPolicyManager::removeOutput(audio_io_handle_t output) |
| 3027 | { |
| 3028 | mOutputs.removeItem(output); |
| 3029 | } |
| 3030 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3031 | void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3032 | { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 3033 | inputDesc->setIoHandle(input); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3034 | mInputs.add(input, inputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3035 | nextAudioPortGeneration(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3036 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3037 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3038 | void AudioPolicyManager::findIoHandlesByAddress(sp<AudioOutputDescriptor> desc /*in*/, |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3039 | const audio_devices_t device /*in*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3040 | const String8 address /*in*/, |
| 3041 | SortedVector<audio_io_handle_t>& outputs /*out*/) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3042 | sp<DeviceDescriptor> devDesc = |
| 3043 | desc->mProfile->mSupportedDevices.getDevice(device, address); |
| 3044 | if (devDesc != 0) { |
| 3045 | ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s", |
| 3046 | desc->mIoHandle, address.string()); |
| 3047 | outputs.add(desc->mIoHandle); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3048 | } |
| 3049 | } |
| 3050 | |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3051 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3052 | audio_policy_dev_state_t state, |
| 3053 | SortedVector<audio_io_handle_t>& outputs, |
| 3054 | const String8 address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3055 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3056 | audio_devices_t device = devDesc->type(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3057 | sp<AudioOutputDescriptor> desc; |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3058 | // erase all current sample rates, formats and channel masks |
| 3059 | devDesc->clearCapabilities(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3060 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3061 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3062 | // first list already open outputs that can be routed to this device |
| 3063 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3064 | desc = mOutputs.valueAt(i); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3065 | if (!desc->isDuplicated() && (desc->mProfile->mSupportedDevices.types() & device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3066 | if (!device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3067 | ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i)); |
| 3068 | outputs.add(mOutputs.keyAt(i)); |
| 3069 | } else { |
| 3070 | ALOGV(" checking address match due to device 0x%x", device); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3071 | findIoHandlesByAddress(desc, device, address, outputs); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3072 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3073 | } |
| 3074 | } |
| 3075 | // then look for output profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3076 | SortedVector< sp<IOProfile> > profiles; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3077 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 3078 | { |
| 3079 | if (mHwModules[i]->mHandle == 0) { |
| 3080 | continue; |
| 3081 | } |
| 3082 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 3083 | { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3084 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; |
| 3085 | if (profile->mSupportedDevices.types() & device) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3086 | if (!device_distinguishes_on_address(device) || |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3087 | address == profile->mSupportedDevices[0]->mAddress) { |
| 3088 | profiles.add(profile); |
| 3089 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i); |
| 3090 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3091 | } |
| 3092 | } |
| 3093 | } |
| 3094 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3095 | ALOGV(" found %d profiles, %d outputs", profiles.size(), outputs.size()); |
| 3096 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3097 | if (profiles.isEmpty() && outputs.isEmpty()) { |
| 3098 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 3099 | return BAD_VALUE; |
| 3100 | } |
| 3101 | |
| 3102 | // open outputs for matching profiles if needed. Direct outputs are also opened to |
| 3103 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 3104 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3105 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3106 | |
| 3107 | // nothing to do if one output is already opened for this profile |
| 3108 | size_t j; |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3109 | for (j = 0; j < outputs.size(); j++) { |
| 3110 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3111 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3112 | // matching profile: save the sample rates, format and channel masks supported |
| 3113 | // by the profile in our device descriptor |
| 3114 | devDesc->importAudioPort(profile); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3115 | break; |
| 3116 | } |
| 3117 | } |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3118 | if (j != outputs.size()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3119 | continue; |
| 3120 | } |
| 3121 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3122 | ALOGV("opening output for device %08x with params %s profile %p", |
| 3123 | device, address.string(), profile.get()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3124 | desc = new AudioOutputDescriptor(profile); |
| 3125 | desc->mDevice = device; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3126 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3127 | config.sample_rate = desc->mSamplingRate; |
| 3128 | config.channel_mask = desc->mChannelMask; |
| 3129 | config.format = desc->mFormat; |
| 3130 | config.offload_info.sample_rate = desc->mSamplingRate; |
| 3131 | config.offload_info.channel_mask = desc->mChannelMask; |
| 3132 | config.offload_info.format = desc->mFormat; |
| 3133 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 3134 | status_t status = mpClientInterface->openOutput(profile->mModule->mHandle, |
| 3135 | &output, |
| 3136 | &config, |
| 3137 | &desc->mDevice, |
| 3138 | address, |
| 3139 | &desc->mLatency, |
| 3140 | desc->mFlags); |
| 3141 | if (status == NO_ERROR) { |
| 3142 | desc->mSamplingRate = config.sample_rate; |
| 3143 | desc->mChannelMask = config.channel_mask; |
| 3144 | desc->mFormat = config.format; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3145 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3146 | // Here is where the out_set_parameters() for card & device gets called |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3147 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3148 | char *param = audio_device_address_to_parameter(device, address); |
| 3149 | mpClientInterface->setParameters(output, String8(param)); |
| 3150 | free(param); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3151 | } |
| 3152 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3153 | // Here is where we step through and resolve any "dynamic" fields |
| 3154 | String8 reply; |
| 3155 | char *value; |
| 3156 | if (profile->mSamplingRates[0] == 0) { |
| 3157 | reply = mpClientInterface->getParameters(output, |
| 3158 | String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3159 | ALOGV("checkOutputsForDevice() supported sampling rates %s", |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3160 | reply.string()); |
| 3161 | value = strpbrk((char *)reply.string(), "="); |
| 3162 | if (value != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3163 | profile->loadSamplingRates(value + 1); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3164 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3165 | } |
| 3166 | if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) { |
| 3167 | reply = mpClientInterface->getParameters(output, |
| 3168 | String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3169 | ALOGV("checkOutputsForDevice() supported formats %s", |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3170 | reply.string()); |
| 3171 | value = strpbrk((char *)reply.string(), "="); |
| 3172 | if (value != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3173 | profile->loadFormats(value + 1); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3174 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3175 | } |
| 3176 | if (profile->mChannelMasks[0] == 0) { |
| 3177 | reply = mpClientInterface->getParameters(output, |
| 3178 | String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3179 | ALOGV("checkOutputsForDevice() supported channel masks %s", |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3180 | reply.string()); |
| 3181 | value = strpbrk((char *)reply.string(), "="); |
| 3182 | if (value != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3183 | profile->loadOutChannels(value + 1); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3184 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3185 | } |
| 3186 | if (((profile->mSamplingRates[0] == 0) && |
| 3187 | (profile->mSamplingRates.size() < 2)) || |
| 3188 | ((profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) && |
| 3189 | (profile->mFormats.size() < 2)) || |
| 3190 | ((profile->mChannelMasks[0] == 0) && |
| 3191 | (profile->mChannelMasks.size() < 2))) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3192 | ALOGW("checkOutputsForDevice() missing param"); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3193 | mpClientInterface->closeOutput(output); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3194 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 3195 | } else if (profile->mSamplingRates[0] == 0 || profile->mFormats[0] == 0 || |
| 3196 | profile->mChannelMasks[0] == 0) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3197 | mpClientInterface->closeOutput(output); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3198 | config.sample_rate = profile->pickSamplingRate(); |
| 3199 | config.channel_mask = profile->pickChannelMask(); |
| 3200 | config.format = profile->pickFormat(); |
| 3201 | config.offload_info.sample_rate = config.sample_rate; |
| 3202 | config.offload_info.channel_mask = config.channel_mask; |
| 3203 | config.offload_info.format = config.format; |
| 3204 | status = mpClientInterface->openOutput(profile->mModule->mHandle, |
| 3205 | &output, |
| 3206 | &config, |
| 3207 | &desc->mDevice, |
| 3208 | address, |
| 3209 | &desc->mLatency, |
| 3210 | desc->mFlags); |
| 3211 | if (status == NO_ERROR) { |
| 3212 | desc->mSamplingRate = config.sample_rate; |
| 3213 | desc->mChannelMask = config.channel_mask; |
| 3214 | desc->mFormat = config.format; |
| 3215 | } else { |
| 3216 | output = AUDIO_IO_HANDLE_NONE; |
| 3217 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3218 | } |
| 3219 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3220 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3221 | addOutput(output, desc); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3222 | if (device_distinguishes_on_address(device) && address != "0") { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame^] | 3223 | sp<AudioPolicyMix> policyMix; |
| 3224 | if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3225 | ALOGE("checkOutputsForDevice() cannot find policy for address %s", |
| 3226 | address.string()); |
| 3227 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame^] | 3228 | policyMix->setOutput(desc); |
| 3229 | desc->mPolicyMix = &(policyMix->getMix()); |
| 3230 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 3231 | } else if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) { |
| 3232 | // no duplicated output for direct outputs and |
| 3233 | // outputs used by dynamic policy mixes |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3234 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3235 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3236 | // set initial stream volume for device |
| 3237 | applyStreamVolumes(output, device, 0, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3238 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3239 | //TODO: configure audio effect output stage here |
| 3240 | |
| 3241 | // open a duplicating output thread for the new output and the primary output |
| 3242 | duplicatedOutput = mpClientInterface->openDuplicateOutput(output, |
| 3243 | mPrimaryOutput); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3244 | if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3245 | // add duplicated output descriptor |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3246 | sp<AudioOutputDescriptor> dupOutputDesc = |
| 3247 | new AudioOutputDescriptor(NULL); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3248 | dupOutputDesc->mOutput1 = mOutputs.valueFor(mPrimaryOutput); |
| 3249 | dupOutputDesc->mOutput2 = mOutputs.valueFor(output); |
| 3250 | dupOutputDesc->mSamplingRate = desc->mSamplingRate; |
| 3251 | dupOutputDesc->mFormat = desc->mFormat; |
| 3252 | dupOutputDesc->mChannelMask = desc->mChannelMask; |
| 3253 | dupOutputDesc->mLatency = desc->mLatency; |
| 3254 | addOutput(duplicatedOutput, dupOutputDesc); |
| 3255 | applyStreamVolumes(duplicatedOutput, device, 0, true); |
| 3256 | } else { |
| 3257 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", |
| 3258 | mPrimaryOutput, output); |
| 3259 | mpClientInterface->closeOutput(output); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3260 | removeOutput(output); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3261 | nextAudioPortGeneration(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3262 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3263 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3264 | } |
| 3265 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3266 | } else { |
| 3267 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3268 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3269 | if (output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3270 | ALOGW("checkOutputsForDevice() could not open output for device %x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3271 | profiles.removeAt(profile_index); |
| 3272 | profile_index--; |
| 3273 | } else { |
| 3274 | outputs.add(output); |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3275 | devDesc->importAudioPort(profile); |
| 3276 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3277 | if (device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3278 | ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)", |
| 3279 | device, address.string()); |
| 3280 | setOutputDevice(output, device, true/*force*/, 0/*delay*/, |
| 3281 | NULL/*patch handle*/, address.string()); |
| 3282 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3283 | ALOGV("checkOutputsForDevice(): adding output %d", output); |
| 3284 | } |
| 3285 | } |
| 3286 | |
| 3287 | if (profiles.isEmpty()) { |
| 3288 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 3289 | return BAD_VALUE; |
| 3290 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3291 | } else { // Disconnect |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3292 | // check if one opened output is not needed any more after disconnecting one device |
| 3293 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3294 | desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3295 | if (!desc->isDuplicated()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3296 | // exact match on device |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3297 | if (device_distinguishes_on_address(device) && |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3298 | (desc->mProfile->mSupportedDevices.types() == device)) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3299 | findIoHandlesByAddress(desc, device, address, outputs); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3300 | } else if (!(desc->mProfile->mSupportedDevices.types() |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3301 | & mAvailableOutputDevices.types())) { |
| 3302 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", |
| 3303 | mOutputs.keyAt(i)); |
| 3304 | outputs.add(mOutputs.keyAt(i)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3305 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3306 | } |
| 3307 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3308 | // Clear any profiles associated with the disconnected device. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3309 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 3310 | { |
| 3311 | if (mHwModules[i]->mHandle == 0) { |
| 3312 | continue; |
| 3313 | } |
| 3314 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 3315 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3316 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3317 | if (profile->mSupportedDevices.types() & device) { |
| 3318 | ALOGV("checkOutputsForDevice(): " |
| 3319 | "clearing direct output profile %zu on module %zu", j, i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3320 | if (profile->mSamplingRates[0] == 0) { |
| 3321 | profile->mSamplingRates.clear(); |
| 3322 | profile->mSamplingRates.add(0); |
| 3323 | } |
| 3324 | if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) { |
| 3325 | profile->mFormats.clear(); |
| 3326 | profile->mFormats.add(AUDIO_FORMAT_DEFAULT); |
| 3327 | } |
| 3328 | if (profile->mChannelMasks[0] == 0) { |
| 3329 | profile->mChannelMasks.clear(); |
| 3330 | profile->mChannelMasks.add(0); |
| 3331 | } |
| 3332 | } |
| 3333 | } |
| 3334 | } |
| 3335 | } |
| 3336 | return NO_ERROR; |
| 3337 | } |
| 3338 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3339 | status_t AudioPolicyManager::checkInputsForDevice(audio_devices_t device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3340 | audio_policy_dev_state_t state, |
| 3341 | SortedVector<audio_io_handle_t>& inputs, |
| 3342 | const String8 address) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3343 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3344 | sp<AudioInputDescriptor> desc; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3345 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 3346 | // first list already open inputs that can be routed to this device |
| 3347 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 3348 | desc = mInputs.valueAt(input_index); |
| 3349 | if (desc->mProfile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) { |
| 3350 | ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index)); |
| 3351 | inputs.add(mInputs.keyAt(input_index)); |
| 3352 | } |
| 3353 | } |
| 3354 | |
| 3355 | // then look for input profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3356 | SortedVector< sp<IOProfile> > profiles; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3357 | for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++) |
| 3358 | { |
| 3359 | if (mHwModules[module_idx]->mHandle == 0) { |
| 3360 | continue; |
| 3361 | } |
| 3362 | for (size_t profile_index = 0; |
| 3363 | profile_index < mHwModules[module_idx]->mInputProfiles.size(); |
| 3364 | profile_index++) |
| 3365 | { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3366 | sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index]; |
| 3367 | |
| 3368 | if (profile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3369 | if (!device_distinguishes_on_address(device) || |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3370 | address == profile->mSupportedDevices[0]->mAddress) { |
| 3371 | profiles.add(profile); |
| 3372 | ALOGV("checkInputsForDevice(): adding profile %zu from module %zu", |
| 3373 | profile_index, module_idx); |
| 3374 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3375 | } |
| 3376 | } |
| 3377 | } |
| 3378 | |
| 3379 | if (profiles.isEmpty() && inputs.isEmpty()) { |
| 3380 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 3381 | return BAD_VALUE; |
| 3382 | } |
| 3383 | |
| 3384 | // open inputs for matching profiles if needed. Direct inputs are also opened to |
| 3385 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 3386 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { |
| 3387 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3388 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3389 | // nothing to do if one input is already opened for this profile |
| 3390 | size_t input_index; |
| 3391 | for (input_index = 0; input_index < mInputs.size(); input_index++) { |
| 3392 | desc = mInputs.valueAt(input_index); |
| 3393 | if (desc->mProfile == profile) { |
| 3394 | break; |
| 3395 | } |
| 3396 | } |
| 3397 | if (input_index != mInputs.size()) { |
| 3398 | continue; |
| 3399 | } |
| 3400 | |
| 3401 | ALOGV("opening input for device 0x%X with params %s", device, address.string()); |
| 3402 | desc = new AudioInputDescriptor(profile); |
| 3403 | desc->mDevice = device; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3404 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3405 | config.sample_rate = desc->mSamplingRate; |
| 3406 | config.channel_mask = desc->mChannelMask; |
| 3407 | config.format = desc->mFormat; |
| 3408 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 3409 | status_t status = mpClientInterface->openInput(profile->mModule->mHandle, |
| 3410 | &input, |
| 3411 | &config, |
| 3412 | &desc->mDevice, |
| 3413 | address, |
| 3414 | AUDIO_SOURCE_MIC, |
| 3415 | AUDIO_INPUT_FLAG_NONE /*FIXME*/); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3416 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3417 | if (status == NO_ERROR) { |
| 3418 | desc->mSamplingRate = config.sample_rate; |
| 3419 | desc->mChannelMask = config.channel_mask; |
| 3420 | desc->mFormat = config.format; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3421 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3422 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3423 | char *param = audio_device_address_to_parameter(device, address); |
| 3424 | mpClientInterface->setParameters(input, String8(param)); |
| 3425 | free(param); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3426 | } |
| 3427 | |
| 3428 | // Here is where we step through and resolve any "dynamic" fields |
| 3429 | String8 reply; |
| 3430 | char *value; |
| 3431 | if (profile->mSamplingRates[0] == 0) { |
| 3432 | reply = mpClientInterface->getParameters(input, |
| 3433 | String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)); |
| 3434 | ALOGV("checkInputsForDevice() direct input sup sampling rates %s", |
| 3435 | reply.string()); |
| 3436 | value = strpbrk((char *)reply.string(), "="); |
| 3437 | if (value != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3438 | profile->loadSamplingRates(value + 1); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3439 | } |
| 3440 | } |
| 3441 | if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) { |
| 3442 | reply = mpClientInterface->getParameters(input, |
| 3443 | String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS)); |
| 3444 | ALOGV("checkInputsForDevice() direct input sup formats %s", reply.string()); |
| 3445 | value = strpbrk((char *)reply.string(), "="); |
| 3446 | if (value != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3447 | profile->loadFormats(value + 1); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3448 | } |
| 3449 | } |
| 3450 | if (profile->mChannelMasks[0] == 0) { |
| 3451 | reply = mpClientInterface->getParameters(input, |
| 3452 | String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS)); |
| 3453 | ALOGV("checkInputsForDevice() direct input sup channel masks %s", |
| 3454 | reply.string()); |
| 3455 | value = strpbrk((char *)reply.string(), "="); |
| 3456 | if (value != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3457 | profile->loadInChannels(value + 1); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3458 | } |
| 3459 | } |
| 3460 | if (((profile->mSamplingRates[0] == 0) && (profile->mSamplingRates.size() < 2)) || |
| 3461 | ((profile->mFormats[0] == 0) && (profile->mFormats.size() < 2)) || |
| 3462 | ((profile->mChannelMasks[0] == 0) && (profile->mChannelMasks.size() < 2))) { |
| 3463 | ALOGW("checkInputsForDevice() direct input missing param"); |
| 3464 | mpClientInterface->closeInput(input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3465 | input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3466 | } |
| 3467 | |
| 3468 | if (input != 0) { |
| 3469 | addInput(input, desc); |
| 3470 | } |
| 3471 | } // endif input != 0 |
| 3472 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3473 | if (input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3474 | ALOGW("checkInputsForDevice() could not open input for device 0x%X", device); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3475 | profiles.removeAt(profile_index); |
| 3476 | profile_index--; |
| 3477 | } else { |
| 3478 | inputs.add(input); |
| 3479 | ALOGV("checkInputsForDevice(): adding input %d", input); |
| 3480 | } |
| 3481 | } // end scan profiles |
| 3482 | |
| 3483 | if (profiles.isEmpty()) { |
| 3484 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 3485 | return BAD_VALUE; |
| 3486 | } |
| 3487 | } else { |
| 3488 | // Disconnect |
| 3489 | // check if one opened input is not needed any more after disconnecting one device |
| 3490 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 3491 | desc = mInputs.valueAt(input_index); |
Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 3492 | if (!(desc->mProfile->mSupportedDevices.types() & mAvailableInputDevices.types() & |
| 3493 | ~AUDIO_DEVICE_BIT_IN)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3494 | ALOGV("checkInputsForDevice(): disconnecting adding input %d", |
| 3495 | mInputs.keyAt(input_index)); |
| 3496 | inputs.add(mInputs.keyAt(input_index)); |
| 3497 | } |
| 3498 | } |
| 3499 | // Clear any profiles associated with the disconnected device. |
| 3500 | for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) { |
| 3501 | if (mHwModules[module_index]->mHandle == 0) { |
| 3502 | continue; |
| 3503 | } |
| 3504 | for (size_t profile_index = 0; |
| 3505 | profile_index < mHwModules[module_index]->mInputProfiles.size(); |
| 3506 | profile_index++) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3507 | sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index]; |
Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 3508 | if (profile->mSupportedDevices.types() & device & ~AUDIO_DEVICE_BIT_IN) { |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 3509 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu", |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3510 | profile_index, module_index); |
| 3511 | if (profile->mSamplingRates[0] == 0) { |
| 3512 | profile->mSamplingRates.clear(); |
| 3513 | profile->mSamplingRates.add(0); |
| 3514 | } |
| 3515 | if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) { |
| 3516 | profile->mFormats.clear(); |
| 3517 | profile->mFormats.add(AUDIO_FORMAT_DEFAULT); |
| 3518 | } |
| 3519 | if (profile->mChannelMasks[0] == 0) { |
| 3520 | profile->mChannelMasks.clear(); |
| 3521 | profile->mChannelMasks.add(0); |
| 3522 | } |
| 3523 | } |
| 3524 | } |
| 3525 | } |
| 3526 | } // end disconnect |
| 3527 | |
| 3528 | return NO_ERROR; |
| 3529 | } |
| 3530 | |
| 3531 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3532 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3533 | { |
| 3534 | ALOGV("closeOutput(%d)", output); |
| 3535 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3536 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3537 | if (outputDesc == NULL) { |
| 3538 | ALOGW("closeOutput() unknown output %d", output); |
| 3539 | return; |
| 3540 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame^] | 3541 | mPolicyMixes.closeOutput(outputDesc); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3542 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3543 | // look for duplicated outputs connected to the output being removed. |
| 3544 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3545 | sp<AudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3546 | if (dupOutputDesc->isDuplicated() && |
| 3547 | (dupOutputDesc->mOutput1 == outputDesc || |
| 3548 | dupOutputDesc->mOutput2 == outputDesc)) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3549 | sp<AudioOutputDescriptor> outputDesc2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3550 | if (dupOutputDesc->mOutput1 == outputDesc) { |
| 3551 | outputDesc2 = dupOutputDesc->mOutput2; |
| 3552 | } else { |
| 3553 | outputDesc2 = dupOutputDesc->mOutput1; |
| 3554 | } |
| 3555 | // As all active tracks on duplicated output will be deleted, |
| 3556 | // and as they were also referenced on the other output, the reference |
| 3557 | // count for their stream type must be adjusted accordingly on |
| 3558 | // the other output. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3559 | for (int j = 0; j < AUDIO_STREAM_CNT; j++) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3560 | int refCount = dupOutputDesc->mRefCount[j]; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3561 | outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3562 | } |
| 3563 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); |
| 3564 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); |
| 3565 | |
| 3566 | mpClientInterface->closeOutput(duplicatedOutput); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3567 | removeOutput(duplicatedOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3568 | } |
| 3569 | } |
| 3570 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 3571 | nextAudioPortGeneration(); |
| 3572 | |
| 3573 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle); |
| 3574 | if (index >= 0) { |
| 3575 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 3576 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3577 | mAudioPatches.removeItemsAt(index); |
| 3578 | mpClientInterface->onAudioPatchListUpdate(); |
| 3579 | } |
| 3580 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3581 | AudioParameter param; |
| 3582 | param.add(String8("closing"), String8("true")); |
| 3583 | mpClientInterface->setParameters(output, param.toString()); |
| 3584 | |
| 3585 | mpClientInterface->closeOutput(output); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3586 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3587 | mPreviousOutputs = mOutputs; |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 3588 | } |
| 3589 | |
| 3590 | void AudioPolicyManager::closeInput(audio_io_handle_t input) |
| 3591 | { |
| 3592 | ALOGV("closeInput(%d)", input); |
| 3593 | |
| 3594 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
| 3595 | if (inputDesc == NULL) { |
| 3596 | ALOGW("closeInput() unknown input %d", input); |
| 3597 | return; |
| 3598 | } |
| 3599 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3600 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 3601 | |
| 3602 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle); |
| 3603 | if (index >= 0) { |
| 3604 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 3605 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3606 | mAudioPatches.removeItemsAt(index); |
| 3607 | mpClientInterface->onAudioPatchListUpdate(); |
| 3608 | } |
| 3609 | |
| 3610 | mpClientInterface->closeInput(input); |
| 3611 | mInputs.removeItem(input); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3612 | } |
| 3613 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3614 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(audio_devices_t device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3615 | AudioOutputCollection openOutputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3616 | { |
| 3617 | SortedVector<audio_io_handle_t> outputs; |
| 3618 | |
| 3619 | ALOGVV("getOutputsForDevice() device %04x", device); |
| 3620 | for (size_t i = 0; i < openOutputs.size(); i++) { |
| 3621 | ALOGVV("output %d isDuplicated=%d device=%04x", |
| 3622 | i, openOutputs.valueAt(i)->isDuplicated(), openOutputs.valueAt(i)->supportedDevices()); |
| 3623 | if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) { |
| 3624 | ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i)); |
| 3625 | outputs.add(openOutputs.keyAt(i)); |
| 3626 | } |
| 3627 | } |
| 3628 | return outputs; |
| 3629 | } |
| 3630 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3631 | bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3632 | SortedVector<audio_io_handle_t>& outputs2) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3633 | { |
| 3634 | if (outputs1.size() != outputs2.size()) { |
| 3635 | return false; |
| 3636 | } |
| 3637 | for (size_t i = 0; i < outputs1.size(); i++) { |
| 3638 | if (outputs1[i] != outputs2[i]) { |
| 3639 | return false; |
| 3640 | } |
| 3641 | } |
| 3642 | return true; |
| 3643 | } |
| 3644 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3645 | void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3646 | { |
| 3647 | audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/); |
| 3648 | audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 3649 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs); |
| 3650 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs); |
| 3651 | |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 3652 | // also take into account external policy-related changes: add all outputs which are |
| 3653 | // associated with policies in the "before" and "after" output vectors |
| 3654 | ALOGVV("checkOutputForStrategy(): policy related outputs"); |
| 3655 | for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) { |
| 3656 | const sp<AudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i); |
| 3657 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 3658 | srcOutputs.add(desc->mIoHandle); |
| 3659 | ALOGVV(" previous outputs: adding %d", desc->mIoHandle); |
| 3660 | } |
| 3661 | } |
| 3662 | for (size_t i = 0 ; i < mOutputs.size() ; i++) { |
| 3663 | const sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 3664 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 3665 | dstOutputs.add(desc->mIoHandle); |
| 3666 | ALOGVV(" new outputs: adding %d", desc->mIoHandle); |
| 3667 | } |
| 3668 | } |
| 3669 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3670 | if (!vectorsEqual(srcOutputs,dstOutputs)) { |
| 3671 | ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d", |
| 3672 | strategy, srcOutputs[0], dstOutputs[0]); |
| 3673 | // mute strategy while moving tracks from one output to another |
| 3674 | for (size_t i = 0; i < srcOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3675 | sp<AudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 3676 | if (isStrategyActive(desc, strategy)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3677 | setStrategyMute(strategy, true, srcOutputs[i]); |
| 3678 | setStrategyMute(strategy, false, srcOutputs[i], MUTE_TIME_MS, newDevice); |
| 3679 | } |
| 3680 | } |
| 3681 | |
| 3682 | // Move effects associated to this strategy from previous output to new output |
| 3683 | if (strategy == STRATEGY_MEDIA) { |
| 3684 | audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs); |
| 3685 | SortedVector<audio_io_handle_t> moved; |
| 3686 | for (size_t i = 0; i < mEffects.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3687 | sp<EffectDescriptor> effectDesc = mEffects.valueAt(i); |
| 3688 | if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX && |
| 3689 | effectDesc->mIo != fxOutput) { |
| 3690 | if (moved.indexOf(effectDesc->mIo) < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3691 | ALOGV("checkOutputForStrategy() moving effect %d to output %d", |
| 3692 | mEffects.keyAt(i), fxOutput); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3693 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3694 | fxOutput); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3695 | moved.add(effectDesc->mIo); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3696 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3697 | effectDesc->mIo = fxOutput; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3698 | } |
| 3699 | } |
| 3700 | } |
| 3701 | // Move tracks associated to this strategy from previous output to new output |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3702 | for (int i = 0; i < AUDIO_STREAM_CNT; i++) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 3703 | if (i == AUDIO_STREAM_PATCH) { |
| 3704 | continue; |
| 3705 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3706 | if (getStrategy((audio_stream_type_t)i) == strategy) { |
| 3707 | mpClientInterface->invalidateStream((audio_stream_type_t)i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3708 | } |
| 3709 | } |
| 3710 | } |
| 3711 | } |
| 3712 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3713 | void AudioPolicyManager::checkOutputForAllStrategies() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3714 | { |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 3715 | if (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) |
| 3716 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3717 | checkOutputForStrategy(STRATEGY_PHONE); |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 3718 | if (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) |
| 3719 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3720 | checkOutputForStrategy(STRATEGY_SONIFICATION); |
| 3721 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 3722 | checkOutputForStrategy(STRATEGY_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3723 | checkOutputForStrategy(STRATEGY_MEDIA); |
| 3724 | checkOutputForStrategy(STRATEGY_DTMF); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 3725 | checkOutputForStrategy(STRATEGY_REROUTING); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3726 | } |
| 3727 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3728 | void AudioPolicyManager::checkA2dpSuspend() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3729 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3730 | audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3731 | if (a2dpOutput == 0) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3732 | mA2dpSuspended = false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3733 | return; |
| 3734 | } |
| 3735 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3736 | bool isScoConnected = |
Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 3737 | ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET & |
| 3738 | ~AUDIO_DEVICE_BIT_IN) != 0) || |
| 3739 | ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3740 | // suspend A2DP output if: |
| 3741 | // (NOT already suspended) && |
| 3742 | // ((SCO device is connected && |
| 3743 | // (forced usage for communication || for record is SCO))) || |
| 3744 | // (phone state is ringing || in call) |
| 3745 | // |
| 3746 | // restore A2DP output if: |
| 3747 | // (Already suspended) && |
| 3748 | // ((SCO device is NOT connected || |
| 3749 | // (forced usage NOT for communication && NOT for record is SCO))) && |
| 3750 | // (phone state is NOT ringing && NOT in call) |
| 3751 | // |
| 3752 | if (mA2dpSuspended) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3753 | if ((!isScoConnected || |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3754 | ((mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] != AUDIO_POLICY_FORCE_BT_SCO) && |
| 3755 | (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] != AUDIO_POLICY_FORCE_BT_SCO))) && |
| 3756 | ((mPhoneState != AUDIO_MODE_IN_CALL) && |
| 3757 | (mPhoneState != AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3758 | |
| 3759 | mpClientInterface->restoreOutput(a2dpOutput); |
| 3760 | mA2dpSuspended = false; |
| 3761 | } |
| 3762 | } else { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3763 | if ((isScoConnected && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3764 | ((mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] == AUDIO_POLICY_FORCE_BT_SCO) || |
| 3765 | (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO))) || |
| 3766 | ((mPhoneState == AUDIO_MODE_IN_CALL) || |
| 3767 | (mPhoneState == AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3768 | |
| 3769 | mpClientInterface->suspendOutput(a2dpOutput); |
| 3770 | mA2dpSuspended = true; |
| 3771 | } |
| 3772 | } |
| 3773 | } |
| 3774 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3775 | audio_devices_t AudioPolicyManager::getNewOutputDevice(audio_io_handle_t output, bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3776 | { |
| 3777 | audio_devices_t device = AUDIO_DEVICE_NONE; |
| 3778 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3779 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3780 | |
| 3781 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle); |
| 3782 | if (index >= 0) { |
| 3783 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 3784 | if (patchDesc->mUid != mUidCached) { |
| 3785 | ALOGV("getNewOutputDevice() device %08x forced by patch %d", |
| 3786 | outputDesc->device(), outputDesc->mPatchHandle); |
| 3787 | return outputDesc->device(); |
| 3788 | } |
| 3789 | } |
| 3790 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3791 | // check the following by order of priority to request a routing change if necessary: |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 3792 | // 1: the strategy enforced audible is active and enforced on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3793 | // use device for strategy enforced audible |
| 3794 | // 2: we are in call or the strategy phone is active on the output: |
| 3795 | // use device for strategy phone |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 3796 | // 3: the strategy for enforced audible is active but not enforced on the output: |
| 3797 | // use the device for strategy enforced audible |
| 3798 | // 4: the strategy sonification is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3799 | // use device for strategy sonification |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 3800 | // 5: the strategy "respectful" sonification is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3801 | // use device for strategy "respectful" sonification |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 3802 | // 6: the strategy accessibility is active on the output: |
| 3803 | // use device for strategy accessibility |
| 3804 | // 7: the strategy media is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3805 | // use device for strategy media |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 3806 | // 8: the strategy DTMF is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3807 | // use device for strategy DTMF |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 3808 | // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output: |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 3809 | // use device for strategy t-t-s |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 3810 | if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) && |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 3811 | mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3812 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
| 3813 | } else if (isInCall() || |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 3814 | isStrategyActive(outputDesc, STRATEGY_PHONE)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3815 | device = getDeviceForStrategy(STRATEGY_PHONE, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 3816 | } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) { |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 3817 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 3818 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3819 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 3820 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3821 | device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 3822 | } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 3823 | device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 3824 | } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3825 | device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 3826 | } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3827 | device = getDeviceForStrategy(STRATEGY_DTMF, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 3828 | } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 3829 | device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 3830 | } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 3831 | device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3832 | } |
| 3833 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3834 | ALOGV("getNewOutputDevice() selected device %x", device); |
| 3835 | return device; |
| 3836 | } |
| 3837 | |
| 3838 | audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input) |
| 3839 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3840 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3841 | |
| 3842 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle); |
| 3843 | if (index >= 0) { |
| 3844 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 3845 | if (patchDesc->mUid != mUidCached) { |
| 3846 | ALOGV("getNewInputDevice() device %08x forced by patch %d", |
| 3847 | inputDesc->mDevice, inputDesc->mPatchHandle); |
| 3848 | return inputDesc->mDevice; |
| 3849 | } |
| 3850 | } |
| 3851 | |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 3852 | audio_devices_t device = getDeviceAndMixForInputSource(inputDesc->mInputSource); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3853 | |
| 3854 | ALOGV("getNewInputDevice() selected device %x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3855 | return device; |
| 3856 | } |
| 3857 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3858 | uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3859 | return (uint32_t)getStrategy(stream); |
| 3860 | } |
| 3861 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3862 | audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3863 | // By checking the range of stream before calling getStrategy, we avoid |
| 3864 | // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE |
| 3865 | // and then return STRATEGY_MEDIA, but we want to return the empty set. |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 3866 | if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3867 | return AUDIO_DEVICE_NONE; |
| 3868 | } |
| 3869 | audio_devices_t devices; |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 3870 | routing_strategy strategy = getStrategy(stream); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3871 | devices = getDeviceForStrategy(strategy, true /*fromCache*/); |
| 3872 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(devices, mOutputs); |
| 3873 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3874 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 3875 | if (isStrategyActive(outputDesc, strategy)) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3876 | devices = outputDesc->device(); |
| 3877 | break; |
| 3878 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3879 | } |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 3880 | |
| 3881 | /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it |
| 3882 | and doesn't really need to.*/ |
| 3883 | if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) { |
| 3884 | devices |= AUDIO_DEVICE_OUT_SPEAKER; |
| 3885 | devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
| 3886 | } |
| 3887 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3888 | return devices; |
| 3889 | } |
| 3890 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 3891 | routing_strategy AudioPolicyManager::getStrategy( |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3892 | audio_stream_type_t stream) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 3893 | |
| 3894 | ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH"); |
| 3895 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3896 | // stream to strategy mapping |
| 3897 | switch (stream) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3898 | case AUDIO_STREAM_VOICE_CALL: |
| 3899 | case AUDIO_STREAM_BLUETOOTH_SCO: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3900 | return STRATEGY_PHONE; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3901 | case AUDIO_STREAM_RING: |
| 3902 | case AUDIO_STREAM_ALARM: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3903 | return STRATEGY_SONIFICATION; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3904 | case AUDIO_STREAM_NOTIFICATION: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3905 | return STRATEGY_SONIFICATION_RESPECTFUL; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3906 | case AUDIO_STREAM_DTMF: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3907 | return STRATEGY_DTMF; |
| 3908 | default: |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 3909 | ALOGE("unknown stream type %d", stream); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3910 | case AUDIO_STREAM_SYSTEM: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3911 | // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs |
| 3912 | // while key clicks are played produces a poor result |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3913 | case AUDIO_STREAM_MUSIC: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3914 | return STRATEGY_MEDIA; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3915 | case AUDIO_STREAM_ENFORCED_AUDIBLE: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3916 | return STRATEGY_ENFORCED_AUDIBLE; |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 3917 | case AUDIO_STREAM_TTS: |
| 3918 | return STRATEGY_TRANSMITTED_THROUGH_SPEAKER; |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 3919 | case AUDIO_STREAM_ACCESSIBILITY: |
| 3920 | return STRATEGY_ACCESSIBILITY; |
| 3921 | case AUDIO_STREAM_REROUTING: |
| 3922 | return STRATEGY_REROUTING; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3923 | } |
| 3924 | } |
| 3925 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 3926 | uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) { |
| 3927 | // flags to strategy mapping |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 3928 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 3929 | return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER; |
| 3930 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 3931 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 3932 | return (uint32_t) STRATEGY_ENFORCED_AUDIBLE; |
| 3933 | } |
| 3934 | |
| 3935 | // usage to strategy mapping |
| 3936 | switch (attr->usage) { |
Eric Laurent | 29e6cec | 2014-11-13 18:17:55 -0800 | [diff] [blame] | 3937 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 3938 | if (isStreamActive(AUDIO_STREAM_RING) || isStreamActive(AUDIO_STREAM_ALARM)) { |
| 3939 | return (uint32_t) STRATEGY_SONIFICATION; |
| 3940 | } |
| 3941 | if (isInCall()) { |
| 3942 | return (uint32_t) STRATEGY_PHONE; |
| 3943 | } |
Eric Laurent | 0f78eab | 2014-11-25 11:01:34 -0800 | [diff] [blame] | 3944 | return (uint32_t) STRATEGY_ACCESSIBILITY; |
Eric Laurent | 29e6cec | 2014-11-13 18:17:55 -0800 | [diff] [blame] | 3945 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 3946 | case AUDIO_USAGE_MEDIA: |
| 3947 | case AUDIO_USAGE_GAME: |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 3948 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 3949 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 3950 | return (uint32_t) STRATEGY_MEDIA; |
| 3951 | |
| 3952 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 3953 | return (uint32_t) STRATEGY_PHONE; |
| 3954 | |
| 3955 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 3956 | return (uint32_t) STRATEGY_DTMF; |
| 3957 | |
| 3958 | case AUDIO_USAGE_ALARM: |
| 3959 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 3960 | return (uint32_t) STRATEGY_SONIFICATION; |
| 3961 | |
| 3962 | case AUDIO_USAGE_NOTIFICATION: |
| 3963 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 3964 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 3965 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 3966 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 3967 | return (uint32_t) STRATEGY_SONIFICATION_RESPECTFUL; |
| 3968 | |
| 3969 | case AUDIO_USAGE_UNKNOWN: |
| 3970 | default: |
| 3971 | return (uint32_t) STRATEGY_MEDIA; |
| 3972 | } |
| 3973 | } |
| 3974 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3975 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3976 | switch(stream) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3977 | case AUDIO_STREAM_MUSIC: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3978 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
| 3979 | updateDevicesAndOutputs(); |
| 3980 | break; |
| 3981 | default: |
| 3982 | break; |
| 3983 | } |
| 3984 | } |
| 3985 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 3986 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { |
| 3987 | switch(event) { |
| 3988 | case STARTING_OUTPUT: |
| 3989 | mBeaconMuteRefCount++; |
| 3990 | break; |
| 3991 | case STOPPING_OUTPUT: |
| 3992 | if (mBeaconMuteRefCount > 0) { |
| 3993 | mBeaconMuteRefCount--; |
| 3994 | } |
| 3995 | break; |
| 3996 | case STARTING_BEACON: |
| 3997 | mBeaconPlayingRefCount++; |
| 3998 | break; |
| 3999 | case STOPPING_BEACON: |
| 4000 | if (mBeaconPlayingRefCount > 0) { |
| 4001 | mBeaconPlayingRefCount--; |
| 4002 | } |
| 4003 | break; |
| 4004 | } |
| 4005 | |
| 4006 | if (mBeaconMuteRefCount > 0) { |
| 4007 | // any playback causes beacon to be muted |
| 4008 | return setBeaconMute(true); |
| 4009 | } else { |
| 4010 | // no other playback: unmute when beacon starts playing, mute when it stops |
| 4011 | return setBeaconMute(mBeaconPlayingRefCount == 0); |
| 4012 | } |
| 4013 | } |
| 4014 | |
| 4015 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { |
| 4016 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", |
| 4017 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); |
| 4018 | // keep track of muted state to avoid repeating mute/unmute operations |
| 4019 | if (mBeaconMuted != mute) { |
| 4020 | // mute/unmute AUDIO_STREAM_TTS on all outputs |
| 4021 | ALOGV("\t muting %d", mute); |
| 4022 | uint32_t maxLatency = 0; |
| 4023 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4024 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 4025 | setStreamMute(AUDIO_STREAM_TTS, mute/*on*/, |
| 4026 | desc->mIoHandle, |
| 4027 | 0 /*delay*/, AUDIO_DEVICE_NONE); |
| 4028 | const uint32_t latency = desc->latency() * 2; |
| 4029 | if (latency > maxLatency) { |
| 4030 | maxLatency = latency; |
| 4031 | } |
| 4032 | } |
| 4033 | mBeaconMuted = mute; |
| 4034 | return maxLatency; |
| 4035 | } |
| 4036 | return 0; |
| 4037 | } |
| 4038 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4039 | audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4040 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4041 | { |
| 4042 | uint32_t device = AUDIO_DEVICE_NONE; |
| 4043 | |
| 4044 | if (fromCache) { |
| 4045 | ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x", |
| 4046 | strategy, mDeviceForStrategy[strategy]); |
| 4047 | return mDeviceForStrategy[strategy]; |
| 4048 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4049 | audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4050 | switch (strategy) { |
| 4051 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4052 | case STRATEGY_TRANSMITTED_THROUGH_SPEAKER: |
| 4053 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER; |
| 4054 | if (!device) { |
| 4055 | ALOGE("getDeviceForStrategy() no device found for "\ |
| 4056 | "STRATEGY_TRANSMITTED_THROUGH_SPEAKER"); |
| 4057 | } |
| 4058 | break; |
| 4059 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4060 | case STRATEGY_SONIFICATION_RESPECTFUL: |
| 4061 | if (isInCall()) { |
| 4062 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4063 | } else if (isStreamActiveRemotely(AUDIO_STREAM_MUSIC, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4064 | SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) { |
| 4065 | // while media is playing on a remote device, use the the sonification behavior. |
| 4066 | // Note that we test this usecase before testing if media is playing because |
| 4067 | // the isStreamActive() method only informs about the activity of a stream, not |
| 4068 | // if it's for local playback. Note also that we use the same delay between both tests |
| 4069 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/); |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4070 | //user "safe" speaker if available instead of normal speaker to avoid triggering |
| 4071 | //other acoustic safety mechanisms for notification |
| 4072 | if (device == AUDIO_DEVICE_OUT_SPEAKER && (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER_SAFE)) |
| 4073 | device = AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4074 | } else if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4075 | // while media is playing (or has recently played), use the same device |
| 4076 | device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/); |
| 4077 | } else { |
| 4078 | // when media is not playing anymore, fall back on the sonification behavior |
| 4079 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/); |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4080 | //user "safe" speaker if available instead of normal speaker to avoid triggering |
| 4081 | //other acoustic safety mechanisms for notification |
| 4082 | if (device == AUDIO_DEVICE_OUT_SPEAKER && (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER_SAFE)) |
| 4083 | device = AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4084 | } |
| 4085 | |
| 4086 | break; |
| 4087 | |
| 4088 | case STRATEGY_DTMF: |
| 4089 | if (!isInCall()) { |
| 4090 | // when off call, DTMF strategy follows the same rules as MEDIA strategy |
| 4091 | device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/); |
| 4092 | break; |
| 4093 | } |
| 4094 | // when in call, DTMF and PHONE strategies follow the same rules |
| 4095 | // FALL THROUGH |
| 4096 | |
| 4097 | case STRATEGY_PHONE: |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 4098 | // Force use of only devices on primary output if: |
| 4099 | // - in call AND |
| 4100 | // - cannot route from voice call RX OR |
| 4101 | // - audio HAL version is < 3.0 and TX device is on the primary HW module |
| 4102 | if (mPhoneState == AUDIO_MODE_IN_CALL) { |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 4103 | audio_devices_t txDevice = |
| 4104 | getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 4105 | sp<AudioOutputDescriptor> hwOutputDesc = mOutputs.valueFor(mPrimaryOutput); |
| 4106 | if (((mAvailableInputDevices.types() & |
| 4107 | AUDIO_DEVICE_IN_TELEPHONY_RX & ~AUDIO_DEVICE_BIT_IN) == 0) || |
| 4108 | (((txDevice & availablePrimaryInputDevices() & ~AUDIO_DEVICE_BIT_IN) != 0) && |
Marco Nelissen | 961ec21 | 2014-08-25 15:58:39 -0700 | [diff] [blame] | 4109 | (hwOutputDesc->getAudioPort()->mModule->mHalVersion < |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 4110 | AUDIO_DEVICE_API_VERSION_3_0))) { |
| 4111 | availableOutputDeviceTypes = availablePrimaryOutputDevices(); |
| 4112 | } |
| 4113 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4114 | // for phone strategy, we first consider the forced use and then the available devices by order |
| 4115 | // of priority |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4116 | switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) { |
| 4117 | case AUDIO_POLICY_FORCE_BT_SCO: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4118 | if (!isInCall() || strategy != STRATEGY_DTMF) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4119 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4120 | if (device) break; |
| 4121 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4122 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4123 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4124 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4125 | if (device) break; |
| 4126 | // if SCO device is requested but no SCO device is available, fall back to default case |
| 4127 | // FALL THROUGH |
| 4128 | |
| 4129 | default: // FORCE_NONE |
| 4130 | // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4131 | if (!isInCall() && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4132 | (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) && |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4133 | (mOutputs.getA2dpOutput() != 0)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4134 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4135 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4136 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4137 | if (device) break; |
| 4138 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4139 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADPHONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4140 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4141 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADSET; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4142 | if (device) break; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 4143 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE; |
| 4144 | if (device) break; |
Eric Laurent | acc930d | 2015-03-04 10:08:29 -0800 | [diff] [blame] | 4145 | if (!isInCall()) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4146 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4147 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4148 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4149 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4150 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4151 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4152 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4153 | if (device) break; |
| 4154 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4155 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_EARPIECE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4156 | if (device) break; |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4157 | device = mDefaultOutputDevice->type(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4158 | if (device == AUDIO_DEVICE_NONE) { |
| 4159 | ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE"); |
| 4160 | } |
| 4161 | break; |
| 4162 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4163 | case AUDIO_POLICY_FORCE_SPEAKER: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4164 | // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to |
| 4165 | // A2DP speaker when forcing to speaker output |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4166 | if (!isInCall() && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4167 | (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) && |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4168 | (mOutputs.getA2dpOutput() != 0)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4169 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4170 | if (device) break; |
| 4171 | } |
Eric Laurent | cd71a69 | 2014-12-16 12:01:12 -0800 | [diff] [blame] | 4172 | if (!isInCall()) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4173 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4174 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4175 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4176 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4177 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4178 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4179 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4180 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4181 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4182 | if (device) break; |
| 4183 | } |
Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 4184 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_LINE; |
| 4185 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4186 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4187 | if (device) break; |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4188 | device = mDefaultOutputDevice->type(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4189 | if (device == AUDIO_DEVICE_NONE) { |
| 4190 | ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER"); |
| 4191 | } |
| 4192 | break; |
| 4193 | } |
| 4194 | break; |
| 4195 | |
| 4196 | case STRATEGY_SONIFICATION: |
| 4197 | |
| 4198 | // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by |
| 4199 | // handleIncallSonification(). |
| 4200 | if (isInCall()) { |
| 4201 | device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/); |
| 4202 | break; |
| 4203 | } |
| 4204 | // FALL THROUGH |
| 4205 | |
| 4206 | case STRATEGY_ENFORCED_AUDIBLE: |
| 4207 | // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION |
| 4208 | // except: |
| 4209 | // - when in call where it doesn't default to STRATEGY_PHONE behavior |
| 4210 | // - in countries where not enforced in which case it follows STRATEGY_MEDIA |
| 4211 | |
| 4212 | if ((strategy == STRATEGY_SONIFICATION) || |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4213 | (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4214 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4215 | if (device == AUDIO_DEVICE_NONE) { |
| 4216 | ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION"); |
| 4217 | } |
| 4218 | } |
| 4219 | // The second device used for sonification is the same as the device used by media strategy |
| 4220 | // FALL THROUGH |
| 4221 | |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4222 | // FIXME: STRATEGY_ACCESSIBILITY and STRATEGY_REROUTING follow STRATEGY_MEDIA for now |
| 4223 | case STRATEGY_ACCESSIBILITY: |
Eric Laurent | 066ceec | 2014-11-25 12:35:01 -0800 | [diff] [blame] | 4224 | if (strategy == STRATEGY_ACCESSIBILITY) { |
| 4225 | // do not route accessibility prompts to a digital output currently configured with a |
| 4226 | // compressed format as they would likely not be mixed and dropped. |
| 4227 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4228 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 4229 | audio_devices_t devices = desc->device() & |
| 4230 | (AUDIO_DEVICE_OUT_HDMI | AUDIO_DEVICE_OUT_SPDIF | AUDIO_DEVICE_OUT_HDMI_ARC); |
| 4231 | if (desc->isActive() && !audio_is_linear_pcm(desc->mFormat) && |
| 4232 | devices != AUDIO_DEVICE_NONE) { |
| 4233 | availableOutputDeviceTypes = availableOutputDeviceTypes & ~devices; |
| 4234 | } |
| 4235 | } |
| 4236 | } |
| 4237 | // FALL THROUGH |
| 4238 | |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4239 | case STRATEGY_REROUTING: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4240 | case STRATEGY_MEDIA: { |
| 4241 | uint32_t device2 = AUDIO_DEVICE_NONE; |
| 4242 | if (strategy != STRATEGY_SONIFICATION) { |
| 4243 | // no sonification on remote submix (e.g. WFD) |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4244 | if (mAvailableOutputDevices.getDevice(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, String8("0")) != 0) { |
| 4245 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
| 4246 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4247 | } |
| 4248 | if ((device2 == AUDIO_DEVICE_NONE) && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4249 | (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) && |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4250 | (mOutputs.getA2dpOutput() != 0)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4251 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4252 | if (device2 == AUDIO_DEVICE_NONE) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4253 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4254 | } |
| 4255 | if (device2 == AUDIO_DEVICE_NONE) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4256 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4257 | } |
| 4258 | } |
Hochi Huang | 327cb70 | 2014-09-21 09:47:31 +0800 | [diff] [blame] | 4259 | if ((device2 == AUDIO_DEVICE_NONE) && |
| 4260 | (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] == AUDIO_POLICY_FORCE_SPEAKER)) { |
| 4261 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER; |
| 4262 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4263 | if (device2 == AUDIO_DEVICE_NONE) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4264 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADPHONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4265 | } |
Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 4266 | if ((device2 == AUDIO_DEVICE_NONE)) { |
| 4267 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_LINE; |
| 4268 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4269 | if (device2 == AUDIO_DEVICE_NONE) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4270 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADSET; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4271 | } |
| 4272 | if (device2 == AUDIO_DEVICE_NONE) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4273 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4274 | } |
| 4275 | if (device2 == AUDIO_DEVICE_NONE) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4276 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4277 | } |
| 4278 | if (device2 == AUDIO_DEVICE_NONE) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4279 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4280 | } |
| 4281 | if ((device2 == AUDIO_DEVICE_NONE) && (strategy != STRATEGY_SONIFICATION)) { |
| 4282 | // no sonification on aux digital (e.g. HDMI) |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4283 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4284 | } |
| 4285 | if ((device2 == AUDIO_DEVICE_NONE) && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4286 | (mForceUse[AUDIO_POLICY_FORCE_FOR_DOCK] == AUDIO_POLICY_FORCE_ANALOG_DOCK)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4287 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4288 | } |
| 4289 | if (device2 == AUDIO_DEVICE_NONE) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4290 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4291 | } |
Jungshik Jang | 839e4f3 | 2014-06-26 17:23:40 +0900 | [diff] [blame] | 4292 | int device3 = AUDIO_DEVICE_NONE; |
| 4293 | if (strategy == STRATEGY_MEDIA) { |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 4294 | // ARC, SPDIF and AUX_LINE can co-exist with others. |
Jungshik Jang | 0c94309 | 2014-07-08 22:11:24 +0900 | [diff] [blame] | 4295 | device3 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_HDMI_ARC; |
| 4296 | device3 |= (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPDIF); |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 4297 | device3 |= (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_LINE); |
Jungshik Jang | 839e4f3 | 2014-06-26 17:23:40 +0900 | [diff] [blame] | 4298 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4299 | |
Jungshik Jang | 839e4f3 | 2014-06-26 17:23:40 +0900 | [diff] [blame] | 4300 | device2 |= device3; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4301 | // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or |
| 4302 | // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise |
| 4303 | device |= device2; |
Jungshik Jang | 839e4f3 | 2014-06-26 17:23:40 +0900 | [diff] [blame] | 4304 | |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 4305 | // If hdmi system audio mode is on, remove speaker out of output list. |
| 4306 | if ((strategy == STRATEGY_MEDIA) && |
| 4307 | (mForceUse[AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO] == |
| 4308 | AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) { |
| 4309 | device &= ~AUDIO_DEVICE_OUT_SPEAKER; |
| 4310 | } |
| 4311 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4312 | if (device) break; |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4313 | device = mDefaultOutputDevice->type(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4314 | if (device == AUDIO_DEVICE_NONE) { |
| 4315 | ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA"); |
| 4316 | } |
| 4317 | } break; |
| 4318 | |
| 4319 | default: |
| 4320 | ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy); |
| 4321 | break; |
| 4322 | } |
| 4323 | |
| 4324 | ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device); |
| 4325 | return device; |
| 4326 | } |
| 4327 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4328 | void AudioPolicyManager::updateDevicesAndOutputs() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4329 | { |
| 4330 | for (int i = 0; i < NUM_STRATEGIES; i++) { |
| 4331 | mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
| 4332 | } |
| 4333 | mPreviousOutputs = mOutputs; |
| 4334 | } |
| 4335 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4336 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4337 | audio_devices_t prevDevice, |
| 4338 | uint32_t delayMs) |
| 4339 | { |
| 4340 | // mute/unmute strategies using an incompatible device combination |
| 4341 | // if muting, wait for the audio in pcm buffer to be drained before proceeding |
| 4342 | // if unmuting, unmute only after the specified delay |
| 4343 | if (outputDesc->isDuplicated()) { |
| 4344 | return 0; |
| 4345 | } |
| 4346 | |
| 4347 | uint32_t muteWaitMs = 0; |
| 4348 | audio_devices_t device = outputDesc->device(); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4349 | bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4350 | |
| 4351 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
| 4352 | audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
Eric Laurent | 31551f8 | 2014-10-10 18:21:56 -0700 | [diff] [blame] | 4353 | curDevice = curDevice & outputDesc->mProfile->mSupportedDevices.types(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4354 | bool mute = shouldMute && (curDevice & device) && (curDevice != device); |
| 4355 | bool doMute = false; |
| 4356 | |
| 4357 | if (mute && !outputDesc->mStrategyMutedByDevice[i]) { |
| 4358 | doMute = true; |
| 4359 | outputDesc->mStrategyMutedByDevice[i] = true; |
| 4360 | } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){ |
| 4361 | doMute = true; |
| 4362 | outputDesc->mStrategyMutedByDevice[i] = false; |
| 4363 | } |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4364 | if (doMute) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4365 | for (size_t j = 0; j < mOutputs.size(); j++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4366 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4367 | // skip output if it does not share any device with current output |
| 4368 | if ((desc->supportedDevices() & outputDesc->supportedDevices()) |
| 4369 | == AUDIO_DEVICE_NONE) { |
| 4370 | continue; |
| 4371 | } |
| 4372 | audio_io_handle_t curOutput = mOutputs.keyAt(j); |
| 4373 | ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x) on output %d", |
| 4374 | mute ? "muting" : "unmuting", i, curDevice, curOutput); |
| 4375 | setStrategyMute((routing_strategy)i, mute, curOutput, mute ? 0 : delayMs); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4376 | if (isStrategyActive(desc, (routing_strategy)i)) { |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4377 | if (mute) { |
| 4378 | // FIXME: should not need to double latency if volume could be applied |
| 4379 | // immediately by the audioflinger mixer. We must account for the delay |
| 4380 | // between now and the next time the audioflinger thread for this output |
| 4381 | // will process a buffer (which corresponds to one buffer size, |
| 4382 | // usually 1/2 or 1/4 of the latency). |
| 4383 | if (muteWaitMs < desc->latency() * 2) { |
| 4384 | muteWaitMs = desc->latency() * 2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4385 | } |
| 4386 | } |
| 4387 | } |
| 4388 | } |
| 4389 | } |
| 4390 | } |
| 4391 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4392 | // temporary mute output if device selection changes to avoid volume bursts due to |
| 4393 | // different per device volumes |
| 4394 | if (outputDesc->isActive() && (device != prevDevice)) { |
| 4395 | if (muteWaitMs < outputDesc->latency() * 2) { |
| 4396 | muteWaitMs = outputDesc->latency() * 2; |
| 4397 | } |
| 4398 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4399 | if (isStrategyActive(outputDesc, (routing_strategy)i)) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4400 | setStrategyMute((routing_strategy)i, true, outputDesc->mIoHandle); |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4401 | // do tempMute unmute after twice the mute wait time |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4402 | setStrategyMute((routing_strategy)i, false, outputDesc->mIoHandle, |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4403 | muteWaitMs *2, device); |
| 4404 | } |
| 4405 | } |
| 4406 | } |
| 4407 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4408 | // wait for the PCM output buffers to empty before proceeding with the rest of the command |
| 4409 | if (muteWaitMs > delayMs) { |
| 4410 | muteWaitMs -= delayMs; |
| 4411 | usleep(muteWaitMs * 1000); |
| 4412 | return muteWaitMs; |
| 4413 | } |
| 4414 | return 0; |
| 4415 | } |
| 4416 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4417 | uint32_t AudioPolicyManager::setOutputDevice(audio_io_handle_t output, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4418 | audio_devices_t device, |
| 4419 | bool force, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4420 | int delayMs, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4421 | audio_patch_handle_t *patchHandle, |
| 4422 | const char* address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4423 | { |
| 4424 | ALOGV("setOutputDevice() output %d device %04x delayMs %d", output, device, delayMs); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4425 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4426 | AudioParameter param; |
| 4427 | uint32_t muteWaitMs; |
| 4428 | |
| 4429 | if (outputDesc->isDuplicated()) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4430 | muteWaitMs = setOutputDevice(outputDesc->mOutput1->mIoHandle, device, force, delayMs); |
| 4431 | muteWaitMs += setOutputDevice(outputDesc->mOutput2->mIoHandle, device, force, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4432 | return muteWaitMs; |
| 4433 | } |
| 4434 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current |
| 4435 | // output profile |
| 4436 | if ((device != AUDIO_DEVICE_NONE) && |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4437 | ((device & outputDesc->mProfile->mSupportedDevices.types()) == 0)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4438 | return 0; |
| 4439 | } |
| 4440 | |
| 4441 | // filter devices according to output selected |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4442 | device = (audio_devices_t)(device & outputDesc->mProfile->mSupportedDevices.types()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4443 | |
| 4444 | audio_devices_t prevDevice = outputDesc->mDevice; |
| 4445 | |
| 4446 | ALOGV("setOutputDevice() prevDevice %04x", prevDevice); |
| 4447 | |
| 4448 | if (device != AUDIO_DEVICE_NONE) { |
| 4449 | outputDesc->mDevice = device; |
| 4450 | } |
| 4451 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs); |
| 4452 | |
| 4453 | // Do not change the routing if: |
Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 4454 | // the requested device is AUDIO_DEVICE_NONE |
| 4455 | // OR the requested device is the same as current device |
| 4456 | // AND force is not specified |
| 4457 | // AND the output is connected by a valid audio patch. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4458 | // Doing this check here allows the caller to call setOutputDevice() without conditions |
Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 4459 | if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && !force && |
| 4460 | outputDesc->mPatchHandle != 0) { |
| 4461 | ALOGV("setOutputDevice() setting same device %04x or null device for output %d", |
| 4462 | device, output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4463 | return muteWaitMs; |
| 4464 | } |
| 4465 | |
| 4466 | ALOGV("setOutputDevice() changing device"); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4467 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4468 | // do the routing |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4469 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4470 | resetOutputDevice(output, delayMs, NULL); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4471 | } else { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4472 | DeviceVector deviceList = (address == NULL) ? |
| 4473 | mAvailableOutputDevices.getDevicesFromType(device) |
| 4474 | : mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address)); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4475 | if (!deviceList.isEmpty()) { |
| 4476 | struct audio_patch patch; |
| 4477 | outputDesc->toAudioPortConfig(&patch.sources[0]); |
| 4478 | patch.num_sources = 1; |
| 4479 | patch.num_sinks = 0; |
| 4480 | for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) { |
| 4481 | deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4482 | patch.num_sinks++; |
| 4483 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4484 | ssize_t index; |
| 4485 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { |
| 4486 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4487 | } else { |
| 4488 | index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle); |
| 4489 | } |
| 4490 | sp< AudioPatch> patchDesc; |
| 4491 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 4492 | if (index >= 0) { |
| 4493 | patchDesc = mAudioPatches.valueAt(index); |
| 4494 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 4495 | } |
| 4496 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4497 | status_t status = mpClientInterface->createAudioPatch(&patch, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4498 | &afPatchHandle, |
| 4499 | delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4500 | ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d" |
| 4501 | "num_sources %d num_sinks %d", |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4502 | status, afPatchHandle, patch.num_sources, patch.num_sinks); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4503 | if (status == NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4504 | if (index < 0) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 4505 | patchDesc = new AudioPatch(&patch, mUidCached); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4506 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 4507 | } else { |
| 4508 | patchDesc->mPatch = patch; |
| 4509 | } |
| 4510 | patchDesc->mAfPatchHandle = afPatchHandle; |
| 4511 | patchDesc->mUid = mUidCached; |
| 4512 | if (patchHandle) { |
| 4513 | *patchHandle = patchDesc->mHandle; |
| 4514 | } |
| 4515 | outputDesc->mPatchHandle = patchDesc->mHandle; |
| 4516 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4517 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4518 | } |
| 4519 | } |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 4520 | |
| 4521 | // inform all input as well |
| 4522 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 4523 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4524 | if (!is_virtual_input_device(inputDescriptor->mDevice)) { |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 4525 | AudioParameter inputCmd = AudioParameter(); |
| 4526 | ALOGV("%s: inform input %d of device:%d", __func__, |
| 4527 | inputDescriptor->mIoHandle, device); |
| 4528 | inputCmd.addInt(String8(AudioParameter::keyRouting),device); |
| 4529 | mpClientInterface->setParameters(inputDescriptor->mIoHandle, |
| 4530 | inputCmd.toString(), |
| 4531 | delayMs); |
| 4532 | } |
| 4533 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4534 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4535 | |
| 4536 | // update stream volumes according to new device |
| 4537 | applyStreamVolumes(output, device, delayMs); |
| 4538 | |
| 4539 | return muteWaitMs; |
| 4540 | } |
| 4541 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4542 | status_t AudioPolicyManager::resetOutputDevice(audio_io_handle_t output, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4543 | int delayMs, |
| 4544 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4545 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4546 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4547 | ssize_t index; |
| 4548 | if (patchHandle) { |
| 4549 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4550 | } else { |
| 4551 | index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle); |
| 4552 | } |
| 4553 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4554 | return INVALID_OPERATION; |
| 4555 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4556 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4557 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4558 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); |
| 4559 | outputDesc->mPatchHandle = 0; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4560 | removeAudioPatch(patchDesc->mHandle); |
| 4561 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4562 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4563 | return status; |
| 4564 | } |
| 4565 | |
| 4566 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, |
| 4567 | audio_devices_t device, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4568 | bool force, |
| 4569 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4570 | { |
| 4571 | status_t status = NO_ERROR; |
| 4572 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4573 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4574 | if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) { |
| 4575 | inputDesc->mDevice = device; |
| 4576 | |
| 4577 | DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device); |
| 4578 | if (!deviceList.isEmpty()) { |
| 4579 | struct audio_patch patch; |
| 4580 | inputDesc->toAudioPortConfig(&patch.sinks[0]); |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 4581 | // AUDIO_SOURCE_HOTWORD is for internal use only: |
| 4582 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 4583 | if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD && |
| 4584 | !inputDesc->mIsSoundTrigger) { |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 4585 | patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 4586 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4587 | patch.num_sinks = 1; |
| 4588 | //only one input device for now |
| 4589 | deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4590 | patch.num_sources = 1; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4591 | ssize_t index; |
| 4592 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { |
| 4593 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4594 | } else { |
| 4595 | index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle); |
| 4596 | } |
| 4597 | sp< AudioPatch> patchDesc; |
| 4598 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 4599 | if (index >= 0) { |
| 4600 | patchDesc = mAudioPatches.valueAt(index); |
| 4601 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 4602 | } |
| 4603 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4604 | status_t status = mpClientInterface->createAudioPatch(&patch, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4605 | &afPatchHandle, |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4606 | 0); |
| 4607 | ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d", |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4608 | status, afPatchHandle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4609 | if (status == NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4610 | if (index < 0) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 4611 | patchDesc = new AudioPatch(&patch, mUidCached); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4612 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 4613 | } else { |
| 4614 | patchDesc->mPatch = patch; |
| 4615 | } |
| 4616 | patchDesc->mAfPatchHandle = afPatchHandle; |
| 4617 | patchDesc->mUid = mUidCached; |
| 4618 | if (patchHandle) { |
| 4619 | *patchHandle = patchDesc->mHandle; |
| 4620 | } |
| 4621 | inputDesc->mPatchHandle = patchDesc->mHandle; |
| 4622 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4623 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4624 | } |
| 4625 | } |
| 4626 | } |
| 4627 | return status; |
| 4628 | } |
| 4629 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4630 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, |
| 4631 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4632 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4633 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4634 | ssize_t index; |
| 4635 | if (patchHandle) { |
| 4636 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4637 | } else { |
| 4638 | index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle); |
| 4639 | } |
| 4640 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4641 | return INVALID_OPERATION; |
| 4642 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4643 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4644 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4645 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); |
| 4646 | inputDesc->mPatchHandle = 0; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4647 | removeAudioPatch(patchDesc->mHandle); |
| 4648 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4649 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4650 | return status; |
| 4651 | } |
| 4652 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 4653 | sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4654 | String8 address, |
| 4655 | uint32_t& samplingRate, |
| 4656 | audio_format_t format, |
| 4657 | audio_channel_mask_t channelMask, |
| 4658 | audio_input_flags_t flags) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4659 | { |
| 4660 | // Choose an input profile based on the requested capture parameters: select the first available |
| 4661 | // profile supporting all requested parameters. |
| 4662 | |
| 4663 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 4664 | { |
| 4665 | if (mHwModules[i]->mHandle == 0) { |
| 4666 | continue; |
| 4667 | } |
| 4668 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) |
| 4669 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4670 | sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j]; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4671 | // profile->log(); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4672 | if (profile->isCompatibleProfile(device, address, samplingRate, |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 4673 | &samplingRate /*updatedSamplingRate*/, |
| 4674 | format, channelMask, (audio_output_flags_t) flags)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4675 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4676 | return profile; |
| 4677 | } |
| 4678 | } |
| 4679 | } |
| 4680 | return NULL; |
| 4681 | } |
| 4682 | |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 4683 | |
| 4684 | audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4685 | AudioMix **policyMix) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4686 | { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame^] | 4687 | audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
| 4688 | audio_devices_t selectedDeviceFromMix = |
| 4689 | mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4690 | |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame^] | 4691 | if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) { |
| 4692 | return selectedDeviceFromMix; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4693 | } |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 4694 | return getDeviceForInputSource(inputSource); |
| 4695 | } |
| 4696 | |
| 4697 | audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource) |
| 4698 | { |
| 4699 | uint32_t device = AUDIO_DEVICE_NONE; |
| 4700 | audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & |
| 4701 | ~AUDIO_DEVICE_BIT_IN; |
| 4702 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4703 | switch (inputSource) { |
| 4704 | case AUDIO_SOURCE_VOICE_UPLINK: |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4705 | if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4706 | device = AUDIO_DEVICE_IN_VOICE_CALL; |
| 4707 | break; |
| 4708 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 4709 | break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4710 | |
| 4711 | case AUDIO_SOURCE_DEFAULT: |
| 4712 | case AUDIO_SOURCE_MIC: |
Mike Lockwood | 41b0e24 | 2014-05-13 15:23:35 -0700 | [diff] [blame] | 4713 | if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_A2DP) { |
| 4714 | device = AUDIO_DEVICE_IN_BLUETOOTH_A2DP; |
Eric Laurent | dc136ff | 2014-12-16 12:24:18 -0800 | [diff] [blame] | 4715 | } else if ((mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO) && |
| 4716 | (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET)) { |
| 4717 | device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 4718 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) { |
| 4719 | device = AUDIO_DEVICE_IN_WIRED_HEADSET; |
| 4720 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) { |
| 4721 | device = AUDIO_DEVICE_IN_USB_DEVICE; |
| 4722 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { |
| 4723 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
Mike Lockwood | 41b0e24 | 2014-05-13 15:23:35 -0700 | [diff] [blame] | 4724 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 4725 | break; |
| 4726 | |
| 4727 | case AUDIO_SOURCE_VOICE_COMMUNICATION: |
| 4728 | // Allow only use of devices on primary input if in call and HAL does not support routing |
| 4729 | // to voice call path. |
| 4730 | if ((mPhoneState == AUDIO_MODE_IN_CALL) && |
| 4731 | (mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_TELEPHONY_TX) == 0) { |
| 4732 | availableDeviceTypes = availablePrimaryInputDevices() & ~AUDIO_DEVICE_BIT_IN; |
| 4733 | } |
| 4734 | |
| 4735 | switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) { |
| 4736 | case AUDIO_POLICY_FORCE_BT_SCO: |
| 4737 | // if SCO device is requested but no SCO device is available, fall back to default case |
| 4738 | if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) { |
| 4739 | device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET; |
| 4740 | break; |
| 4741 | } |
| 4742 | // FALL THROUGH |
| 4743 | |
| 4744 | default: // FORCE_NONE |
| 4745 | if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) { |
| 4746 | device = AUDIO_DEVICE_IN_WIRED_HEADSET; |
| 4747 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) { |
| 4748 | device = AUDIO_DEVICE_IN_USB_DEVICE; |
| 4749 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { |
| 4750 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
| 4751 | } |
| 4752 | break; |
| 4753 | |
| 4754 | case AUDIO_POLICY_FORCE_SPEAKER: |
| 4755 | if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) { |
| 4756 | device = AUDIO_DEVICE_IN_BACK_MIC; |
| 4757 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { |
| 4758 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
| 4759 | } |
| 4760 | break; |
| 4761 | } |
| 4762 | break; |
Mike Lockwood | 41b0e24 | 2014-05-13 15:23:35 -0700 | [diff] [blame] | 4763 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4764 | case AUDIO_SOURCE_VOICE_RECOGNITION: |
| 4765 | case AUDIO_SOURCE_HOTWORD: |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4766 | if (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO && |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4767 | availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4768 | device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4769 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4770 | device = AUDIO_DEVICE_IN_WIRED_HEADSET; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4771 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) { |
| 4772 | device = AUDIO_DEVICE_IN_USB_DEVICE; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4773 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4774 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
| 4775 | } |
| 4776 | break; |
| 4777 | case AUDIO_SOURCE_CAMCORDER: |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4778 | if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4779 | device = AUDIO_DEVICE_IN_BACK_MIC; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4780 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4781 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
| 4782 | } |
| 4783 | break; |
| 4784 | case AUDIO_SOURCE_VOICE_DOWNLINK: |
| 4785 | case AUDIO_SOURCE_VOICE_CALL: |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4786 | if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4787 | device = AUDIO_DEVICE_IN_VOICE_CALL; |
| 4788 | } |
| 4789 | break; |
| 4790 | case AUDIO_SOURCE_REMOTE_SUBMIX: |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4791 | if (availableDeviceTypes & AUDIO_DEVICE_IN_REMOTE_SUBMIX) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4792 | device = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 4793 | } |
| 4794 | break; |
Hochi Huang | 327cb70 | 2014-09-21 09:47:31 +0800 | [diff] [blame] | 4795 | case AUDIO_SOURCE_FM_TUNER: |
| 4796 | if (availableDeviceTypes & AUDIO_DEVICE_IN_FM_TUNER) { |
| 4797 | device = AUDIO_DEVICE_IN_FM_TUNER; |
| 4798 | } |
| 4799 | break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4800 | default: |
| 4801 | ALOGW("getDeviceForInputSource() invalid input source %d", inputSource); |
| 4802 | break; |
| 4803 | } |
| 4804 | ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device); |
| 4805 | return device; |
| 4806 | } |
| 4807 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4808 | void AudioPolicyManager::initializeVolumeCurves() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4809 | { |
| 4810 | for (int i = 0; i < AUDIO_STREAM_CNT; i++) { |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 4811 | for (int j = 0; j < ApmGains::DEVICE_CATEGORY_CNT; j++) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4812 | mStreams[i].mVolumeCurve[j] = |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 4813 | ApmGains::sVolumeProfiles[i][j]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4814 | } |
| 4815 | } |
| 4816 | |
| 4817 | // Check availability of DRC on speaker path: if available, override some of the speaker curves |
| 4818 | if (mSpeakerDrcEnabled) { |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 4819 | mStreams[AUDIO_STREAM_SYSTEM].mVolumeCurve[ApmGains::DEVICE_CATEGORY_SPEAKER] = |
| 4820 | ApmGains::sDefaultSystemVolumeCurveDrc; |
| 4821 | mStreams[AUDIO_STREAM_RING].mVolumeCurve[ApmGains::DEVICE_CATEGORY_SPEAKER] = |
| 4822 | ApmGains::sSpeakerSonificationVolumeCurveDrc; |
| 4823 | mStreams[AUDIO_STREAM_ALARM].mVolumeCurve[ApmGains::DEVICE_CATEGORY_SPEAKER] = |
| 4824 | ApmGains::sSpeakerSonificationVolumeCurveDrc; |
| 4825 | mStreams[AUDIO_STREAM_NOTIFICATION].mVolumeCurve[ApmGains::DEVICE_CATEGORY_SPEAKER] = |
| 4826 | ApmGains::sSpeakerSonificationVolumeCurveDrc; |
| 4827 | mStreams[AUDIO_STREAM_MUSIC].mVolumeCurve[ApmGains::DEVICE_CATEGORY_SPEAKER] = |
| 4828 | ApmGains::sSpeakerMediaVolumeCurveDrc; |
| 4829 | mStreams[AUDIO_STREAM_ACCESSIBILITY].mVolumeCurve[ApmGains::DEVICE_CATEGORY_SPEAKER] = |
| 4830 | ApmGains::sSpeakerMediaVolumeCurveDrc; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4831 | } |
| 4832 | } |
| 4833 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4834 | float AudioPolicyManager::computeVolume(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4835 | int index, |
| 4836 | audio_io_handle_t output, |
| 4837 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4838 | { |
| 4839 | float volume = 1.0; |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4840 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4841 | StreamDescriptor &streamDesc = mStreams[stream]; |
| 4842 | |
| 4843 | if (device == AUDIO_DEVICE_NONE) { |
| 4844 | device = outputDesc->device(); |
| 4845 | } |
| 4846 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 4847 | volume = ApmGains::volIndexToAmpl(device, streamDesc, index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4848 | |
| 4849 | // if a headset is connected, apply the following rules to ring tones and notifications |
| 4850 | // to avoid sound level bursts in user's ears: |
| 4851 | // - always attenuate ring tones and notifications volume by 6dB |
| 4852 | // - if music is playing, always limit the volume to current music volume, |
| 4853 | // with a minimum threshold at -36dB so that notification is always perceived. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4854 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4855 | if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 4856 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | |
| 4857 | AUDIO_DEVICE_OUT_WIRED_HEADSET | |
| 4858 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) && |
| 4859 | ((stream_strategy == STRATEGY_SONIFICATION) |
| 4860 | || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL) |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4861 | || (stream == AUDIO_STREAM_SYSTEM) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4862 | || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4863 | (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_NONE))) && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4864 | streamDesc.mCanBeMuted) { |
| 4865 | volume *= SONIFICATION_HEADSET_VOLUME_FACTOR; |
| 4866 | // when the phone is ringing we must consider that music could have been paused just before |
| 4867 | // by the music application and behave as if music was active if the last music track was |
| 4868 | // just stopped |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4869 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4870 | mLimitRingtoneVolume) { |
| 4871 | audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4872 | float musicVol = computeVolume(AUDIO_STREAM_MUSIC, |
| 4873 | mStreams[AUDIO_STREAM_MUSIC].getVolumeIndex(musicDevice), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4874 | output, |
| 4875 | musicDevice); |
| 4876 | float minVol = (musicVol > SONIFICATION_HEADSET_VOLUME_MIN) ? |
| 4877 | musicVol : SONIFICATION_HEADSET_VOLUME_MIN; |
| 4878 | if (volume > minVol) { |
| 4879 | volume = minVol; |
| 4880 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVol, musicVol); |
| 4881 | } |
| 4882 | } |
| 4883 | } |
| 4884 | |
| 4885 | return volume; |
| 4886 | } |
| 4887 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4888 | status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4889 | int index, |
| 4890 | audio_io_handle_t output, |
| 4891 | audio_devices_t device, |
| 4892 | int delayMs, |
| 4893 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4894 | { |
| 4895 | |
| 4896 | // do not change actual stream volume if the stream is muted |
| 4897 | if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) { |
| 4898 | ALOGVV("checkAndSetVolume() stream %d muted count %d", |
| 4899 | stream, mOutputs.valueFor(output)->mMuteCount[stream]); |
| 4900 | return NO_ERROR; |
| 4901 | } |
| 4902 | |
| 4903 | // 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] | 4904 | if ((stream == AUDIO_STREAM_VOICE_CALL && |
| 4905 | mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] == AUDIO_POLICY_FORCE_BT_SCO) || |
| 4906 | (stream == AUDIO_STREAM_BLUETOOTH_SCO && |
| 4907 | mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] != AUDIO_POLICY_FORCE_BT_SCO)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4908 | 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] | 4909 | stream, mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4910 | return INVALID_OPERATION; |
| 4911 | } |
| 4912 | |
| 4913 | float volume = computeVolume(stream, index, output, device); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4914 | // unit gain if rerouting to external policy |
| 4915 | if (device == AUDIO_DEVICE_OUT_REMOTE_SUBMIX) { |
| 4916 | ssize_t index = mOutputs.indexOfKey(output); |
| 4917 | if (index >= 0) { |
| 4918 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 4919 | if (outputDesc->mPolicyMix != NULL) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4920 | ALOGV("max gain when rerouting for output=%d", output); |
| 4921 | volume = 1.0f; |
| 4922 | } |
| 4923 | } |
| 4924 | |
| 4925 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4926 | // We actually change the volume if: |
| 4927 | // - the float value returned by computeVolume() changed |
| 4928 | // - the force flag is set |
| 4929 | if (volume != mOutputs.valueFor(output)->mCurVolume[stream] || |
| 4930 | force) { |
| 4931 | mOutputs.valueFor(output)->mCurVolume[stream] = volume; |
| 4932 | ALOGVV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs); |
| 4933 | // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is |
| 4934 | // enabled |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4935 | if (stream == AUDIO_STREAM_BLUETOOTH_SCO) { |
| 4936 | mpClientInterface->setStreamVolume(AUDIO_STREAM_VOICE_CALL, volume, output, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4937 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4938 | mpClientInterface->setStreamVolume(stream, volume, output, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4939 | } |
| 4940 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4941 | if (stream == AUDIO_STREAM_VOICE_CALL || |
| 4942 | stream == AUDIO_STREAM_BLUETOOTH_SCO) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4943 | float voiceVolume; |
| 4944 | // 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] | 4945 | if (stream == AUDIO_STREAM_VOICE_CALL) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4946 | voiceVolume = (float)index/(float)mStreams[stream].mIndexMax; |
| 4947 | } else { |
| 4948 | voiceVolume = 1.0; |
| 4949 | } |
| 4950 | |
| 4951 | if (voiceVolume != mLastVoiceVolume && output == mPrimaryOutput) { |
| 4952 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); |
| 4953 | mLastVoiceVolume = voiceVolume; |
| 4954 | } |
| 4955 | } |
| 4956 | |
| 4957 | return NO_ERROR; |
| 4958 | } |
| 4959 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4960 | void AudioPolicyManager::applyStreamVolumes(audio_io_handle_t output, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4961 | audio_devices_t device, |
| 4962 | int delayMs, |
| 4963 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4964 | { |
| 4965 | ALOGVV("applyStreamVolumes() for output %d and device %x", output, device); |
| 4966 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4967 | for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4968 | if (stream == AUDIO_STREAM_PATCH) { |
| 4969 | continue; |
| 4970 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4971 | checkAndSetVolume((audio_stream_type_t)stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4972 | mStreams[stream].getVolumeIndex(device), |
| 4973 | output, |
| 4974 | device, |
| 4975 | delayMs, |
| 4976 | force); |
| 4977 | } |
| 4978 | } |
| 4979 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4980 | void AudioPolicyManager::setStrategyMute(routing_strategy strategy, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4981 | bool on, |
| 4982 | audio_io_handle_t output, |
| 4983 | int delayMs, |
| 4984 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4985 | { |
| 4986 | ALOGVV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4987 | for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4988 | if (stream == AUDIO_STREAM_PATCH) { |
| 4989 | continue; |
| 4990 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4991 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
| 4992 | setStreamMute((audio_stream_type_t)stream, on, output, delayMs, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4993 | } |
| 4994 | } |
| 4995 | } |
| 4996 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4997 | void AudioPolicyManager::setStreamMute(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4998 | bool on, |
| 4999 | audio_io_handle_t output, |
| 5000 | int delayMs, |
| 5001 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5002 | { |
| 5003 | StreamDescriptor &streamDesc = mStreams[stream]; |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5004 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5005 | if (device == AUDIO_DEVICE_NONE) { |
| 5006 | device = outputDesc->device(); |
| 5007 | } |
| 5008 | |
| 5009 | ALOGVV("setStreamMute() stream %d, mute %d, output %d, mMuteCount %d device %04x", |
| 5010 | stream, on, output, outputDesc->mMuteCount[stream], device); |
| 5011 | |
| 5012 | if (on) { |
| 5013 | if (outputDesc->mMuteCount[stream] == 0) { |
| 5014 | if (streamDesc.mCanBeMuted && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5015 | ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) || |
| 5016 | (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_NONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5017 | checkAndSetVolume(stream, 0, output, device, delayMs); |
| 5018 | } |
| 5019 | } |
| 5020 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored |
| 5021 | outputDesc->mMuteCount[stream]++; |
| 5022 | } else { |
| 5023 | if (outputDesc->mMuteCount[stream] == 0) { |
| 5024 | ALOGV("setStreamMute() unmuting non muted stream!"); |
| 5025 | return; |
| 5026 | } |
| 5027 | if (--outputDesc->mMuteCount[stream] == 0) { |
| 5028 | checkAndSetVolume(stream, |
| 5029 | streamDesc.getVolumeIndex(device), |
| 5030 | output, |
| 5031 | device, |
| 5032 | delayMs); |
| 5033 | } |
| 5034 | } |
| 5035 | } |
| 5036 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5037 | void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5038 | bool starting, bool stateChange) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5039 | { |
| 5040 | // if the stream pertains to sonification strategy and we are in call we must |
| 5041 | // mute the stream if it is low visibility. If it is high visibility, we must play a tone |
| 5042 | // in the device used for phone strategy and play the tone if the selected device does not |
| 5043 | // interfere with the device used for phone strategy |
| 5044 | // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as |
| 5045 | // many times as there are active tracks on the output |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5046 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5047 | if ((stream_strategy == STRATEGY_SONIFICATION) || |
| 5048 | ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5049 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(mPrimaryOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5050 | ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d", |
| 5051 | stream, starting, outputDesc->mDevice, stateChange); |
| 5052 | if (outputDesc->mRefCount[stream]) { |
| 5053 | int muteCount = 1; |
| 5054 | if (stateChange) { |
| 5055 | muteCount = outputDesc->mRefCount[stream]; |
| 5056 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5057 | if (audio_is_low_visibility(stream)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5058 | ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount); |
| 5059 | for (int i = 0; i < muteCount; i++) { |
| 5060 | setStreamMute(stream, starting, mPrimaryOutput); |
| 5061 | } |
| 5062 | } else { |
| 5063 | ALOGV("handleIncallSonification() high visibility"); |
| 5064 | if (outputDesc->device() & |
| 5065 | getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) { |
| 5066 | ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount); |
| 5067 | for (int i = 0; i < muteCount; i++) { |
| 5068 | setStreamMute(stream, starting, mPrimaryOutput); |
| 5069 | } |
| 5070 | } |
| 5071 | if (starting) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5072 | mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION, |
| 5073 | AUDIO_STREAM_VOICE_CALL); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5074 | } else { |
| 5075 | mpClientInterface->stopTone(); |
| 5076 | } |
| 5077 | } |
| 5078 | } |
| 5079 | } |
| 5080 | } |
| 5081 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5082 | bool AudioPolicyManager::isInCall() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5083 | { |
| 5084 | return isStateInCall(mPhoneState); |
| 5085 | } |
| 5086 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5087 | bool AudioPolicyManager::isStateInCall(int state) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5088 | return ((state == AUDIO_MODE_IN_CALL) || |
| 5089 | (state == AUDIO_MODE_IN_COMMUNICATION)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5090 | } |
| 5091 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5092 | uint32_t AudioPolicyManager::getMaxEffectsCpuLoad() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5093 | { |
| 5094 | return MAX_EFFECTS_CPU_LOAD; |
| 5095 | } |
| 5096 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5097 | uint32_t AudioPolicyManager::getMaxEffectsMemory() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5098 | { |
| 5099 | return MAX_EFFECTS_MEMORY; |
| 5100 | } |
| 5101 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5102 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5103 | // --- EffectDescriptor class implementation |
| 5104 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5105 | status_t AudioPolicyManager::EffectDescriptor::dump(int fd) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5106 | { |
| 5107 | const size_t SIZE = 256; |
| 5108 | char buffer[SIZE]; |
| 5109 | String8 result; |
| 5110 | |
| 5111 | snprintf(buffer, SIZE, " I/O: %d\n", mIo); |
| 5112 | result.append(buffer); |
| 5113 | snprintf(buffer, SIZE, " Strategy: %d\n", mStrategy); |
| 5114 | result.append(buffer); |
| 5115 | snprintf(buffer, SIZE, " Session: %d\n", mSession); |
| 5116 | result.append(buffer); |
| 5117 | snprintf(buffer, SIZE, " Name: %s\n", mDesc.name); |
| 5118 | result.append(buffer); |
| 5119 | snprintf(buffer, SIZE, " %s\n", mEnabled ? "Enabled" : "Disabled"); |
| 5120 | result.append(buffer); |
| 5121 | write(fd, result.string(), result.size()); |
| 5122 | |
| 5123 | return NO_ERROR; |
| 5124 | } |
| 5125 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5126 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5127 | void AudioPolicyManager::defaultAudioPolicyConfig(void) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5128 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5129 | sp<HwModule> module; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5130 | sp<IOProfile> profile; |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 5131 | sp<DeviceDescriptor> defaultInputDevice = |
| 5132 | new DeviceDescriptor(String8("builtin-mic"), AUDIO_DEVICE_IN_BUILTIN_MIC); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5133 | mAvailableOutputDevices.add(mDefaultOutputDevice); |
| 5134 | mAvailableInputDevices.add(defaultInputDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5135 | |
| 5136 | module = new HwModule("primary"); |
| 5137 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5138 | profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SOURCE, module); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5139 | profile->mSamplingRates.add(44100); |
| 5140 | profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT); |
| 5141 | profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5142 | profile->mSupportedDevices.add(mDefaultOutputDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5143 | profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY; |
| 5144 | module->mOutputProfiles.add(profile); |
| 5145 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5146 | profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SINK, module); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5147 | profile->mSamplingRates.add(8000); |
| 5148 | profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT); |
| 5149 | profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5150 | profile->mSupportedDevices.add(defaultInputDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5151 | module->mInputProfiles.add(profile); |
| 5152 | |
| 5153 | mHwModules.add(module); |
| 5154 | } |
| 5155 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5156 | audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr) |
| 5157 | { |
| 5158 | // flags to stream type mapping |
| 5159 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 5160 | return AUDIO_STREAM_ENFORCED_AUDIBLE; |
| 5161 | } |
| 5162 | if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) { |
| 5163 | return AUDIO_STREAM_BLUETOOTH_SCO; |
| 5164 | } |
Jean-Michel Trivi | 79ad438 | 2015-01-29 10:49:39 -0800 | [diff] [blame] | 5165 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 5166 | return AUDIO_STREAM_TTS; |
| 5167 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5168 | |
| 5169 | // usage to stream type mapping |
| 5170 | switch (attr->usage) { |
| 5171 | case AUDIO_USAGE_MEDIA: |
| 5172 | case AUDIO_USAGE_GAME: |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5173 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 5174 | return AUDIO_STREAM_MUSIC; |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5175 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5176 | if (isStreamActive(AUDIO_STREAM_ALARM)) { |
| 5177 | return AUDIO_STREAM_ALARM; |
| 5178 | } |
| 5179 | if (isStreamActive(AUDIO_STREAM_RING)) { |
| 5180 | return AUDIO_STREAM_RING; |
| 5181 | } |
| 5182 | if (isInCall()) { |
| 5183 | return AUDIO_STREAM_VOICE_CALL; |
| 5184 | } |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5185 | return AUDIO_STREAM_ACCESSIBILITY; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5186 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 5187 | return AUDIO_STREAM_SYSTEM; |
| 5188 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 5189 | return AUDIO_STREAM_VOICE_CALL; |
| 5190 | |
| 5191 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 5192 | return AUDIO_STREAM_DTMF; |
| 5193 | |
| 5194 | case AUDIO_USAGE_ALARM: |
| 5195 | return AUDIO_STREAM_ALARM; |
| 5196 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 5197 | return AUDIO_STREAM_RING; |
| 5198 | |
| 5199 | case AUDIO_USAGE_NOTIFICATION: |
| 5200 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 5201 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 5202 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 5203 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 5204 | return AUDIO_STREAM_NOTIFICATION; |
| 5205 | |
| 5206 | case AUDIO_USAGE_UNKNOWN: |
| 5207 | default: |
| 5208 | return AUDIO_STREAM_MUSIC; |
| 5209 | } |
| 5210 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5211 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5212 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) |
| 5213 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5214 | // has flags that map to a strategy? |
| 5215 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { |
| 5216 | return true; |
| 5217 | } |
| 5218 | |
| 5219 | // has known usage? |
| 5220 | switch (paa->usage) { |
| 5221 | case AUDIO_USAGE_UNKNOWN: |
| 5222 | case AUDIO_USAGE_MEDIA: |
| 5223 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 5224 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 5225 | case AUDIO_USAGE_ALARM: |
| 5226 | case AUDIO_USAGE_NOTIFICATION: |
| 5227 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 5228 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 5229 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 5230 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 5231 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 5232 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 5233 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 5234 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 5235 | case AUDIO_USAGE_GAME: |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5236 | case AUDIO_USAGE_VIRTUAL_SOURCE: |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5237 | break; |
| 5238 | default: |
| 5239 | return false; |
| 5240 | } |
| 5241 | return true; |
| 5242 | } |
| 5243 | |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5244 | |
| 5245 | bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc, |
| 5246 | routing_strategy strategy, uint32_t inPastMs, |
| 5247 | nsecs_t sysTime) const |
| 5248 | { |
| 5249 | if ((sysTime == 0) && (inPastMs != 0)) { |
| 5250 | sysTime = systemTime(); |
| 5251 | } |
| 5252 | for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) { |
| 5253 | if (i == AUDIO_STREAM_PATCH) { |
| 5254 | continue; |
| 5255 | } |
| 5256 | if (((getStrategy((audio_stream_type_t)i) == strategy) || |
| 5257 | (NUM_STRATEGIES == strategy)) && |
| 5258 | outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) { |
| 5259 | return true; |
| 5260 | } |
| 5261 | } |
| 5262 | return false; |
| 5263 | } |
| 5264 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5265 | }; // namespace android |