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