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 | |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 17 | #define LOG_TAG "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 | |
François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 27 | #define AUDIO_POLICY_XML_CONFIG_FILE "/system/etc/audio_policy_configuration.xml" |
| 28 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 29 | #include <inttypes.h> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 30 | #include <math.h> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 31 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 32 | #include <AudioPolicyManagerInterface.h> |
| 33 | #include <AudioPolicyEngineInstance.h> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 34 | #include <cutils/properties.h> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 35 | #include <utils/Log.h> |
| 36 | #include <hardware/audio.h> |
| 37 | #include <hardware/audio_effect.h> |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 38 | #include <media/AudioParameter.h> |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 39 | #include <media/AudioPolicyHelper.h> |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 40 | #include <soundtrigger/SoundTrigger.h> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 41 | #include "AudioPolicyManager.h" |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 42 | #ifndef USE_XML_AUDIO_POLICY_CONF |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 43 | #include <ConfigParsingUtils.h> |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 44 | #include <StreamDescriptor.h> |
François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 45 | #endif |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 46 | #include <Serializer.h> |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 47 | #include "TypeConverter.h" |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 48 | #include <policy.h> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 49 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 50 | namespace android { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 51 | |
| 52 | // ---------------------------------------------------------------------------- |
| 53 | // AudioPolicyInterface implementation |
| 54 | // ---------------------------------------------------------------------------- |
| 55 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 56 | status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 57 | audio_policy_dev_state_t state, |
| 58 | const char *device_address, |
| 59 | const char *device_name) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 60 | { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 61 | return setDeviceConnectionStateInt(device, state, device_address, device_name); |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device, |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 65 | audio_policy_dev_state_t state, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 66 | const char *device_address, |
| 67 | const char *device_name) |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 68 | { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 69 | ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s", |
| 70 | - device, state, device_address, device_name); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 71 | |
| 72 | // connect/disconnect only 1 device at a time |
| 73 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; |
| 74 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 75 | sp<DeviceDescriptor> devDesc = |
| 76 | mHwModules.getDeviceDescriptor(device, device_address, device_name); |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 77 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 78 | // handle output devices |
| 79 | if (audio_is_output_device(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 80 | SortedVector <audio_io_handle_t> outputs; |
| 81 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 82 | ssize_t index = mAvailableOutputDevices.indexOf(devDesc); |
| 83 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 84 | // save a copy of the opened output descriptors before any output is opened or closed |
| 85 | // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies() |
| 86 | mPreviousOutputs = mOutputs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 87 | switch (state) |
| 88 | { |
| 89 | // handle output device connection |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 90 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 91 | if (index >= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 92 | ALOGW("setDeviceConnectionState() device already connected: %x", device); |
| 93 | return INVALID_OPERATION; |
| 94 | } |
| 95 | ALOGV("setDeviceConnectionState() connecting device %x", device); |
| 96 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 97 | // register new device as available |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 98 | index = mAvailableOutputDevices.add(devDesc); |
| 99 | if (index >= 0) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 100 | sp<HwModule> module = mHwModules.getModuleForDevice(device); |
Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 101 | if (module == 0) { |
| 102 | ALOGD("setDeviceConnectionState() could not find HW module for device %08x", |
| 103 | device); |
| 104 | mAvailableOutputDevices.remove(devDesc); |
| 105 | return INVALID_OPERATION; |
| 106 | } |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 107 | mAvailableOutputDevices[index]->attach(module); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 108 | } else { |
| 109 | return NO_MEMORY; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 112 | if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 113 | mAvailableOutputDevices.remove(devDesc); |
| 114 | return INVALID_OPERATION; |
| 115 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 116 | // Propagate device availability to Engine |
| 117 | mEngine->setDeviceConnectionState(devDesc, state); |
| 118 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 119 | // outputs should never be empty here |
| 120 | ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():" |
| 121 | "checkOutputsForDevice() returned no outputs but status OK"); |
| 122 | ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs", |
| 123 | outputs.size()); |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 124 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 125 | // Send connect to HALs |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 126 | AudioParameter param = AudioParameter(devDesc->mAddress); |
| 127 | param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device); |
| 128 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 129 | |
| 130 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 131 | // handle output device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 132 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 133 | if (index < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 134 | ALOGW("setDeviceConnectionState() device not connected: %x", device); |
| 135 | return INVALID_OPERATION; |
| 136 | } |
| 137 | |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 138 | ALOGV("setDeviceConnectionState() disconnecting output device %x", device); |
| 139 | |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 140 | // Send Disconnect to HALs |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 141 | AudioParameter param = AudioParameter(devDesc->mAddress); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 142 | param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device); |
| 143 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 144 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 145 | // remove device from available output devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 146 | mAvailableOutputDevices.remove(devDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 147 | |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 148 | checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 149 | |
| 150 | // Propagate device availability to Engine |
| 151 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 152 | } break; |
| 153 | |
| 154 | default: |
| 155 | ALOGE("setDeviceConnectionState() invalid state: %x", state); |
| 156 | return BAD_VALUE; |
| 157 | } |
| 158 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 159 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP |
| 160 | // output is suspended before any tracks are moved to it |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 161 | checkA2dpSuspend(); |
| 162 | checkOutputForAllStrategies(); |
| 163 | // outputs must be closed after checkOutputForAllStrategies() is executed |
| 164 | if (!outputs.isEmpty()) { |
| 165 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 166 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 167 | // close unused outputs after device disconnection or direct outputs that have been |
| 168 | // opened by checkOutputsForDevice() to query dynamic parameters |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 169 | if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 170 | (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) && |
| 171 | (desc->mDirectOpenCount == 0))) { |
| 172 | closeOutput(outputs[i]); |
| 173 | } |
| 174 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 175 | // check again after closing A2DP output to reset mA2dpSuspended if needed |
| 176 | checkA2dpSuspend(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | updateDevicesAndOutputs(); |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 180 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 181 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 182 | updateCallRouting(newDevice); |
| 183 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 184 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 185 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 186 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) { |
| 187 | audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 188 | // do not force device change on duplicated output because if device is 0, it will |
| 189 | // also force a device 0 for the two outputs it is duplicated to which may override |
| 190 | // a valid device selection on those outputs. |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 191 | bool force = !desc->isDuplicated() |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 192 | && (!device_distinguishes_on_address(device) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 193 | // always force when disconnecting (a non-duplicated device) |
| 194 | || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 195 | setOutputDevice(desc, newDevice, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 196 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 199 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
| 200 | cleanUpForDevice(devDesc); |
| 201 | } |
| 202 | |
Eric Laurent | 72aa32f | 2014-05-30 18:51:48 -0700 | [diff] [blame] | 203 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | b71e58b | 2014-05-29 16:08:11 -0700 | [diff] [blame] | 204 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 205 | } // end if is output device |
| 206 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 207 | // handle input devices |
| 208 | if (audio_is_input_device(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 209 | SortedVector <audio_io_handle_t> inputs; |
| 210 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 211 | ssize_t index = mAvailableInputDevices.indexOf(devDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 212 | switch (state) |
| 213 | { |
| 214 | // handle input device connection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 215 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 216 | if (index >= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 217 | ALOGW("setDeviceConnectionState() device already connected: %d", device); |
| 218 | return INVALID_OPERATION; |
| 219 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 220 | sp<HwModule> module = mHwModules.getModuleForDevice(device); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 221 | if (module == NULL) { |
| 222 | ALOGW("setDeviceConnectionState(): could not find HW module for device %08x", |
| 223 | device); |
| 224 | return INVALID_OPERATION; |
| 225 | } |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 226 | if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 227 | return INVALID_OPERATION; |
| 228 | } |
| 229 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 230 | index = mAvailableInputDevices.add(devDesc); |
| 231 | if (index >= 0) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 232 | mAvailableInputDevices[index]->attach(module); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 233 | } else { |
| 234 | return NO_MEMORY; |
| 235 | } |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 236 | |
| 237 | // Set connect to HALs |
| 238 | AudioParameter param = AudioParameter(devDesc->mAddress); |
| 239 | param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device); |
| 240 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 241 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 242 | // Propagate device availability to Engine |
| 243 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 244 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 245 | |
| 246 | // handle input device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 247 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 248 | if (index < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 249 | ALOGW("setDeviceConnectionState() device not connected: %d", device); |
| 250 | return INVALID_OPERATION; |
| 251 | } |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 252 | |
| 253 | ALOGV("setDeviceConnectionState() disconnecting input device %x", device); |
| 254 | |
| 255 | // Set Disconnect to HALs |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 256 | AudioParameter param = AudioParameter(devDesc->mAddress); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 257 | param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device); |
| 258 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 259 | |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 260 | checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 261 | mAvailableInputDevices.remove(devDesc); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 262 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 263 | // Propagate device availability to Engine |
| 264 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 265 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 266 | |
| 267 | default: |
| 268 | ALOGE("setDeviceConnectionState() invalid state: %x", state); |
| 269 | return BAD_VALUE; |
| 270 | } |
| 271 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 272 | closeAllInputs(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 273 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 274 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 275 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 276 | updateCallRouting(newDevice); |
| 277 | } |
| 278 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 279 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
| 280 | cleanUpForDevice(devDesc); |
| 281 | } |
| 282 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 283 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 284 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 285 | } // end if is input device |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 286 | |
| 287 | ALOGW("setDeviceConnectionState() invalid device: %x", device); |
| 288 | return BAD_VALUE; |
| 289 | } |
| 290 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 291 | audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 292 | const char *device_address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 293 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 294 | sp<DeviceDescriptor> devDesc = mHwModules.getDeviceDescriptor(device, device_address, ""); |
| 295 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 296 | DeviceVector *deviceVector; |
| 297 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 298 | if (audio_is_output_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 299 | deviceVector = &mAvailableOutputDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 300 | } else if (audio_is_input_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 301 | deviceVector = &mAvailableInputDevices; |
| 302 | } else { |
| 303 | ALOGW("getDeviceConnectionState() invalid device type %08x", device); |
| 304 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 305 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 306 | return deviceVector->getDeviceConnectionState(devDesc); |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 307 | } |
| 308 | |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 309 | void AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, int delayMs) |
| 310 | { |
| 311 | bool createTxPatch = false; |
| 312 | struct audio_patch patch; |
| 313 | patch.num_sources = 1; |
| 314 | patch.num_sinks = 1; |
| 315 | status_t status; |
| 316 | audio_patch_handle_t afPatchHandle; |
| 317 | DeviceVector deviceList; |
| 318 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 319 | if(!hasPrimaryOutput()) { |
| 320 | return; |
| 321 | } |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 322 | audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 323 | ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice); |
| 324 | |
| 325 | // release existing RX patch if any |
| 326 | if (mCallRxPatch != 0) { |
| 327 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 328 | mCallRxPatch.clear(); |
| 329 | } |
| 330 | // release TX patch if any |
| 331 | if (mCallTxPatch != 0) { |
| 332 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 333 | mCallTxPatch.clear(); |
| 334 | } |
| 335 | |
| 336 | // If the RX device is on the primary HW module, then use legacy routing method for voice calls |
| 337 | // via setOutputDevice() on primary output. |
| 338 | // Otherwise, create two audio patches for TX and RX path. |
| 339 | if (availablePrimaryOutputDevices() & rxDevice) { |
| 340 | setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs); |
| 341 | // If the TX device is also on the primary HW module, setOutputDevice() will take care |
| 342 | // of it due to legacy implementation. If not, create a patch. |
| 343 | if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN) |
| 344 | == AUDIO_DEVICE_NONE) { |
| 345 | createTxPatch = true; |
| 346 | } |
| 347 | } else { |
| 348 | // create RX path audio patch |
| 349 | deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice); |
| 350 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 351 | "updateCallRouting() selected device not in output device list"); |
| 352 | sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0); |
| 353 | deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX); |
| 354 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 355 | "updateCallRouting() no telephony RX device"); |
| 356 | sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0); |
| 357 | |
| 358 | rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]); |
| 359 | rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]); |
| 360 | |
| 361 | // request to reuse existing output stream if one is already opened to reach the RX device |
| 362 | SortedVector<audio_io_handle_t> outputs = |
| 363 | getOutputsForDevice(rxDevice, mOutputs); |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 364 | audio_io_handle_t output = selectOutput(outputs, |
| 365 | AUDIO_OUTPUT_FLAG_NONE, |
| 366 | AUDIO_FORMAT_INVALID); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 367 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 368 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 369 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 370 | "updateCallRouting() RX device output is duplicated"); |
| 371 | outputDesc->toAudioPortConfig(&patch.sources[1]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 372 | patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 373 | patch.num_sources = 2; |
| 374 | } |
| 375 | |
| 376 | afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 377 | status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0); |
| 378 | ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch", |
| 379 | status); |
| 380 | if (status == NO_ERROR) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 381 | mCallRxPatch = new AudioPatch(&patch, mUidCached); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 382 | mCallRxPatch->mAfPatchHandle = afPatchHandle; |
| 383 | mCallRxPatch->mUid = mUidCached; |
| 384 | } |
| 385 | createTxPatch = true; |
| 386 | } |
| 387 | if (createTxPatch) { |
| 388 | |
| 389 | struct audio_patch patch; |
| 390 | patch.num_sources = 1; |
| 391 | patch.num_sinks = 1; |
| 392 | deviceList = mAvailableInputDevices.getDevicesFromType(txDevice); |
| 393 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 394 | "updateCallRouting() selected device not in input device list"); |
| 395 | sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0); |
| 396 | txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]); |
| 397 | deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX); |
| 398 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 399 | "updateCallRouting() no telephony TX device"); |
| 400 | sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0); |
| 401 | txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]); |
| 402 | |
| 403 | SortedVector<audio_io_handle_t> outputs = |
| 404 | getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs); |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 405 | audio_io_handle_t output = selectOutput(outputs, |
| 406 | AUDIO_OUTPUT_FLAG_NONE, |
| 407 | AUDIO_FORMAT_INVALID); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 408 | // request to reuse existing output stream if one is already opened to reach the TX |
| 409 | // path output device |
| 410 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 411 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 412 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 413 | "updateCallRouting() RX device output is duplicated"); |
| 414 | outputDesc->toAudioPortConfig(&patch.sources[1]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 415 | patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 416 | patch.num_sources = 2; |
| 417 | } |
| 418 | |
Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 419 | // terminate active capture if on the same HW module as the call TX source device |
| 420 | // FIXME: would be better to refine to only inputs whose profile connects to the |
| 421 | // call TX device but this information is not in the audio patch and logic here must be |
| 422 | // symmetric to the one in startInput() |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 423 | Vector<sp <AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs(); |
| 424 | for (size_t i = 0; i < activeInputs.size(); i++) { |
| 425 | sp<AudioInputDescriptor> activeDesc = activeInputs[i]; |
| 426 | if (activeDesc->hasSameHwModuleAs(txSourceDeviceDesc)) { |
| 427 | AudioSessionCollection activeSessions = |
| 428 | activeDesc->getAudioSessions(true /*activeOnly*/); |
| 429 | for (size_t j = 0; j < activeSessions.size(); j++) { |
| 430 | audio_session_t activeSession = activeSessions.keyAt(j); |
| 431 | stopInput(activeDesc->mIoHandle, activeSession); |
| 432 | releaseInput(activeDesc->mIoHandle, activeSession); |
| 433 | } |
Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 437 | afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 438 | status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0); |
| 439 | ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch", |
| 440 | status); |
| 441 | if (status == NO_ERROR) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 442 | mCallTxPatch = new AudioPatch(&patch, mUidCached); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 443 | mCallTxPatch->mAfPatchHandle = afPatchHandle; |
| 444 | mCallTxPatch->mUid = mUidCached; |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 449 | void AudioPolicyManager::setPhoneState(audio_mode_t state) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 450 | { |
| 451 | ALOGV("setPhoneState() state %d", state); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 452 | // store previous phone state for management of sonification strategy below |
| 453 | int oldState = mEngine->getPhoneState(); |
| 454 | |
| 455 | if (mEngine->setPhoneState(state) != NO_ERROR) { |
| 456 | ALOGW("setPhoneState() invalid or same state %d", state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 457 | return; |
| 458 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 459 | /// Opens: can these line be executed after the switch of volume curves??? |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 460 | // if leaving call state, handle special case of active streams |
| 461 | // pertaining to sonification strategy see handleIncallSonification() |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 462 | if (isStateInCall(oldState)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 463 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 464 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 465 | handleIncallSonification((audio_stream_type_t)stream, false, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 466 | } |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 467 | |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 468 | // force reevaluating accessibility routing when call stops |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 469 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 470 | } |
| 471 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 472 | /** |
| 473 | * Switching to or from incall state or switching between telephony and VoIP lead to force |
| 474 | * routing command. |
| 475 | */ |
| 476 | bool force = ((is_state_in_call(oldState) != is_state_in_call(state)) |
| 477 | || (is_state_in_call(state) && (state != oldState))); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 478 | |
| 479 | // check for device and output changes triggered by new phone state |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 480 | checkA2dpSuspend(); |
| 481 | checkOutputForAllStrategies(); |
| 482 | updateDevicesAndOutputs(); |
| 483 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 484 | int delayMs = 0; |
| 485 | if (isStateInCall(state)) { |
| 486 | nsecs_t sysTime = systemTime(); |
| 487 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 488 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 489 | // mute media and sonification strategies and delay device switch by the largest |
| 490 | // latency of any output where either strategy is active. |
| 491 | // 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] | 492 | if ((isStrategyActive(desc, STRATEGY_MEDIA, |
| 493 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 494 | sysTime) || |
| 495 | isStrategyActive(desc, STRATEGY_SONIFICATION, |
| 496 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 497 | sysTime)) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 498 | (delayMs < (int)desc->latency()*2)) { |
| 499 | delayMs = desc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 500 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 501 | setStrategyMute(STRATEGY_MEDIA, true, desc); |
| 502 | setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 503 | getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/)); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 504 | setStrategyMute(STRATEGY_SONIFICATION, true, desc); |
| 505 | setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 506 | getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/)); |
| 507 | } |
| 508 | } |
| 509 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 510 | if (hasPrimaryOutput()) { |
| 511 | // Note that despite the fact that getNewOutputDevice() is called on the primary output, |
| 512 | // the device returned is not necessarily reachable via this output |
| 513 | audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 514 | // force routing command to audio hardware when ending call |
| 515 | // even if no device change is needed |
| 516 | if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) { |
| 517 | rxDevice = mPrimaryOutput->device(); |
| 518 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 519 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 520 | if (state == AUDIO_MODE_IN_CALL) { |
| 521 | updateCallRouting(rxDevice, delayMs); |
| 522 | } else if (oldState == AUDIO_MODE_IN_CALL) { |
| 523 | if (mCallRxPatch != 0) { |
| 524 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 525 | mCallRxPatch.clear(); |
| 526 | } |
| 527 | if (mCallTxPatch != 0) { |
| 528 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 529 | mCallTxPatch.clear(); |
| 530 | } |
| 531 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
| 532 | } else { |
| 533 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 534 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 535 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 536 | // if entering in call state, handle special case of active streams |
| 537 | // pertaining to sonification strategy see handleIncallSonification() |
| 538 | if (isStateInCall(state)) { |
| 539 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 540 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 541 | handleIncallSonification((audio_stream_type_t)stream, true, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 542 | } |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 543 | |
| 544 | // force reevaluating accessibility routing when call starts |
| 545 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | // 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] | 549 | if (state == AUDIO_MODE_RINGTONE && |
| 550 | isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 551 | mLimitRingtoneVolume = true; |
| 552 | } else { |
| 553 | mLimitRingtoneVolume = false; |
| 554 | } |
| 555 | } |
| 556 | |
Jean-Michel Trivi | 887a9ed | 2015-03-31 18:02:24 -0700 | [diff] [blame] | 557 | audio_mode_t AudioPolicyManager::getPhoneState() { |
| 558 | return mEngine->getPhoneState(); |
| 559 | } |
| 560 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 561 | void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 562 | audio_policy_forced_cfg_t config) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 563 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 564 | ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 565 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 566 | if (mEngine->setForceUse(usage, config) != NO_ERROR) { |
| 567 | ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage); |
| 568 | return; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 569 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 570 | bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) || |
| 571 | (usage == AUDIO_POLICY_FORCE_FOR_DOCK) || |
| 572 | (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 573 | |
| 574 | // check for device and output changes triggered by new force usage |
| 575 | checkA2dpSuspend(); |
| 576 | checkOutputForAllStrategies(); |
| 577 | updateDevicesAndOutputs(); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 578 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 579 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 580 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/); |
| 581 | updateCallRouting(newDevice); |
| 582 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 583 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 584 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
| 585 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/); |
| 586 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) { |
| 587 | setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE)); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 588 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 589 | if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 590 | applyStreamVolumes(outputDesc, newDevice, 0, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 591 | } |
| 592 | } |
| 593 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 594 | Vector<sp <AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs(); |
| 595 | for (size_t i = 0; i < activeInputs.size(); i++) { |
| 596 | sp<AudioInputDescriptor> activeDesc = activeInputs[i]; |
| 597 | audio_devices_t newDevice = getNewInputDevice(activeDesc); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 598 | // Force new input selection if the new device can not be reached via current input |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 599 | if (activeDesc->mProfile->getSupportedDevices().types() & |
| 600 | (newDevice & ~AUDIO_DEVICE_BIT_IN)) { |
| 601 | setInputDevice(activeDesc->mIoHandle, newDevice); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 602 | } else { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 603 | closeInput(activeDesc->mIoHandle); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 604 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 605 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 606 | } |
| 607 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 608 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 609 | { |
| 610 | ALOGV("setSystemProperty() property %s, value %s", property, value); |
| 611 | } |
| 612 | |
| 613 | // Find a direct output profile compatible with the parameters passed, even if the input flags do |
| 614 | // not explicitly request a direct output |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 615 | sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput( |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 616 | audio_devices_t device, |
| 617 | uint32_t samplingRate, |
| 618 | audio_format_t format, |
| 619 | audio_channel_mask_t channelMask, |
| 620 | audio_output_flags_t flags) |
| 621 | { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 622 | // only retain flags that will drive the direct output profile selection |
| 623 | // if explicitly requested |
| 624 | static const uint32_t kRelevantFlags = |
| 625 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
| 626 | flags = |
| 627 | (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); |
| 628 | |
| 629 | sp<IOProfile> profile; |
| 630 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 631 | for (size_t i = 0; i < mHwModules.size(); i++) { |
| 632 | if (mHwModules[i]->mHandle == 0) { |
| 633 | continue; |
| 634 | } |
| 635 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 636 | sp<IOProfile> curProfile = mHwModules[i]->mOutputProfiles[j]; |
| 637 | if (!curProfile->isCompatibleProfile(device, String8(""), |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 638 | samplingRate, NULL /*updatedSamplingRate*/, |
| 639 | format, NULL /*updatedFormat*/, |
| 640 | channelMask, NULL /*updatedChannelMask*/, |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 641 | flags)) { |
| 642 | continue; |
| 643 | } |
| 644 | // reject profiles not corresponding to a device currently available |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 645 | if ((mAvailableOutputDevices.types() & curProfile->getSupportedDevicesType()) == 0) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 646 | continue; |
| 647 | } |
| 648 | // if several profiles are compatible, give priority to one with offload capability |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 649 | if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 650 | continue; |
| 651 | } |
| 652 | profile = curProfile; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 653 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 654 | break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 655 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 656 | } |
| 657 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 658 | return profile; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 659 | } |
| 660 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 661 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 662 | uint32_t samplingRate, |
| 663 | audio_format_t format, |
| 664 | audio_channel_mask_t channelMask, |
| 665 | audio_output_flags_t flags, |
| 666 | const audio_offload_info_t *offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 667 | { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 668 | routing_strategy strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 669 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 670 | ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x", |
| 671 | device, stream, samplingRate, format, channelMask, flags); |
| 672 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 673 | return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE, |
| 674 | stream, samplingRate,format, channelMask, |
| 675 | flags, offloadInfo); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 676 | } |
| 677 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 678 | status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, |
| 679 | audio_io_handle_t *output, |
| 680 | audio_session_t session, |
| 681 | audio_stream_type_t *stream, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 682 | uid_t uid, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 683 | uint32_t samplingRate, |
| 684 | audio_format_t format, |
| 685 | audio_channel_mask_t channelMask, |
| 686 | audio_output_flags_t flags, |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 687 | audio_port_handle_t selectedDeviceId, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 688 | const audio_offload_info_t *offloadInfo) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 689 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 690 | audio_attributes_t attributes; |
| 691 | if (attr != NULL) { |
| 692 | if (!isValidAttributes(attr)) { |
| 693 | ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]", |
| 694 | attr->usage, attr->content_type, attr->flags, |
| 695 | attr->tags); |
| 696 | return BAD_VALUE; |
| 697 | } |
| 698 | attributes = *attr; |
| 699 | } else { |
| 700 | if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) { |
| 701 | ALOGE("getOutputForAttr(): invalid stream type"); |
| 702 | return BAD_VALUE; |
| 703 | } |
| 704 | stream_type_to_audio_attributes(*stream, &attributes); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 705 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 706 | sp<SwAudioOutputDescriptor> desc; |
Jean-Michel Trivi | e8deced | 2016-02-11 12:50:39 -0800 | [diff] [blame] | 707 | if (mPolicyMixes.getOutputForAttr(attributes, uid, desc) == NO_ERROR) { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 708 | ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr"); |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 709 | if (!audio_has_proportional_frames(format)) { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 710 | return BAD_VALUE; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 711 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 712 | *stream = streamTypefromAttributesInt(&attributes); |
| 713 | *output = desc->mIoHandle; |
| 714 | ALOGV("getOutputForAttr() returns output %d", *output); |
| 715 | return NO_ERROR; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 716 | } |
| 717 | if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 718 | ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE"); |
| 719 | return BAD_VALUE; |
| 720 | } |
| 721 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 722 | ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x" |
| 723 | " session %d selectedDeviceId %d", |
| 724 | attributes.usage, attributes.content_type, attributes.tags, attributes.flags, |
| 725 | session, selectedDeviceId); |
| 726 | |
| 727 | *stream = streamTypefromAttributesInt(&attributes); |
| 728 | |
| 729 | // Explicit routing? |
| 730 | sp<DeviceDescriptor> deviceDesc; |
| 731 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { |
| 732 | if (mAvailableOutputDevices[i]->getId() == selectedDeviceId) { |
| 733 | deviceDesc = mAvailableOutputDevices[i]; |
| 734 | break; |
| 735 | } |
| 736 | } |
| 737 | mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 738 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 739 | routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 740 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 741 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 742 | if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) { |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 743 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
| 744 | } |
| 745 | |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 746 | 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] | 747 | device, samplingRate, format, channelMask, flags); |
| 748 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 749 | *output = getOutputForDevice(device, session, *stream, |
| 750 | samplingRate, format, channelMask, |
| 751 | flags, offloadInfo); |
| 752 | if (*output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 753 | mOutputRoutes.removeRoute(session); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 754 | return INVALID_OPERATION; |
| 755 | } |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 756 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 757 | return NO_ERROR; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | audio_io_handle_t AudioPolicyManager::getOutputForDevice( |
| 761 | audio_devices_t device, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 762 | audio_session_t session __unused, |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 763 | audio_stream_type_t stream, |
| 764 | uint32_t samplingRate, |
| 765 | audio_format_t format, |
| 766 | audio_channel_mask_t channelMask, |
| 767 | audio_output_flags_t flags, |
| 768 | const audio_offload_info_t *offloadInfo) |
| 769 | { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 770 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 771 | uint32_t latency = 0; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 772 | status_t status; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 773 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 774 | #ifdef AUDIO_POLICY_TEST |
| 775 | if (mCurOutput != 0) { |
| 776 | ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d", |
| 777 | mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput); |
| 778 | |
| 779 | if (mTestOutputs[mCurOutput] == 0) { |
| 780 | ALOGV("getOutput() opening test output"); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 781 | sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL, |
| 782 | mpClientInterface); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 783 | outputDesc->mDevice = mTestDevice; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 784 | outputDesc->mLatency = mTestLatencyMs; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 785 | outputDesc->mFlags = |
| 786 | (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 787 | outputDesc->mRefCount[stream] = 0; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 788 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 789 | config.sample_rate = mTestSamplingRate; |
| 790 | config.channel_mask = mTestChannels; |
| 791 | config.format = mTestFormat; |
Phil Burk | 77cce80 | 2014-08-04 16:18:15 -0700 | [diff] [blame] | 792 | if (offloadInfo != NULL) { |
| 793 | config.offload_info = *offloadInfo; |
| 794 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 795 | status = mpClientInterface->openOutput(0, |
| 796 | &mTestOutputs[mCurOutput], |
| 797 | &config, |
| 798 | &outputDesc->mDevice, |
| 799 | String8(""), |
| 800 | &outputDesc->mLatency, |
| 801 | outputDesc->mFlags); |
| 802 | if (status == NO_ERROR) { |
| 803 | outputDesc->mSamplingRate = config.sample_rate; |
| 804 | outputDesc->mFormat = config.format; |
| 805 | outputDesc->mChannelMask = config.channel_mask; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 806 | AudioParameter outputCmd = AudioParameter(); |
| 807 | outputCmd.addInt(String8("set_id"),mCurOutput); |
| 808 | mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString()); |
| 809 | addOutput(mTestOutputs[mCurOutput], outputDesc); |
| 810 | } |
| 811 | } |
| 812 | return mTestOutputs[mCurOutput]; |
| 813 | } |
| 814 | #endif //AUDIO_POLICY_TEST |
| 815 | |
| 816 | // open a direct output if required by specified parameters |
| 817 | //force direct flag if offload flag is set: offloading implies a direct output stream |
| 818 | // and all common behaviors are driven by checking only the direct flag |
| 819 | // this should normally be set appropriately in the policy configuration file |
| 820 | if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 821 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 822 | } |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 823 | if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 824 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT); |
| 825 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 826 | // only allow deep buffering for music stream type |
| 827 | if (stream != AUDIO_STREAM_MUSIC) { |
| 828 | flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 829 | } else if (/* stream == AUDIO_STREAM_MUSIC && */ |
| 830 | flags == AUDIO_OUTPUT_FLAG_NONE && |
| 831 | property_get_bool("audio.deep_buffer.media", false /* default_value */)) { |
| 832 | // use DEEP_BUFFER as default output for music stream type |
| 833 | flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER; |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 834 | } |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 835 | if (stream == AUDIO_STREAM_TTS) { |
| 836 | flags = AUDIO_OUTPUT_FLAG_TTS; |
| 837 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 838 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 839 | sp<IOProfile> profile; |
| 840 | |
| 841 | // 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] | 842 | // and not explicitly requested |
| 843 | if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 844 | audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX && |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 845 | audio_channel_count_from_out_mask(channelMask) <= 2) { |
| 846 | goto non_direct_output; |
| 847 | } |
| 848 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 849 | // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled. |
| 850 | // This prevents creating an offloaded track and tearing it down immediately after start |
| 851 | // when audioflinger detects there is an active non offloadable effect. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 852 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 853 | // This may prevent offloading in rare situations where effects are left active by apps |
| 854 | // in the background. |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 855 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 856 | if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 857 | !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 858 | profile = getProfileForDirectOutput(device, |
| 859 | samplingRate, |
| 860 | format, |
| 861 | channelMask, |
| 862 | (audio_output_flags_t)flags); |
| 863 | } |
| 864 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 865 | if (profile != 0) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 866 | sp<SwAudioOutputDescriptor> outputDesc = NULL; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 867 | |
| 868 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 869 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 870 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { |
| 871 | outputDesc = desc; |
| 872 | // reuse direct output if currently open and configured with same parameters |
| 873 | if ((samplingRate == outputDesc->mSamplingRate) && |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 874 | audio_formats_match(format, outputDesc->mFormat) && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 875 | (channelMask == outputDesc->mChannelMask)) { |
| 876 | outputDesc->mDirectOpenCount++; |
| 877 | ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i)); |
| 878 | return mOutputs.keyAt(i); |
| 879 | } |
| 880 | } |
| 881 | } |
| 882 | // close direct output if currently open and configured with different parameters |
| 883 | if (outputDesc != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 884 | closeOutput(outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 885 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 886 | |
| 887 | // if the selected profile is offloaded and no offload info was specified, |
| 888 | // create a default one |
| 889 | audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 890 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 891 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
| 892 | defaultOffloadInfo.sample_rate = samplingRate; |
| 893 | defaultOffloadInfo.channel_mask = channelMask; |
| 894 | defaultOffloadInfo.format = format; |
| 895 | defaultOffloadInfo.stream_type = stream; |
| 896 | defaultOffloadInfo.bit_rate = 0; |
| 897 | defaultOffloadInfo.duration_us = -1; |
| 898 | defaultOffloadInfo.has_video = true; // conservative |
| 899 | defaultOffloadInfo.is_streaming = true; // likely |
| 900 | offloadInfo = &defaultOffloadInfo; |
| 901 | } |
| 902 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 903 | outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 904 | outputDesc->mDevice = device; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 905 | outputDesc->mLatency = 0; |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 906 | outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 907 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 908 | config.sample_rate = samplingRate; |
| 909 | config.channel_mask = channelMask; |
| 910 | config.format = format; |
Phil Burk | 77cce80 | 2014-08-04 16:18:15 -0700 | [diff] [blame] | 911 | if (offloadInfo != NULL) { |
| 912 | config.offload_info = *offloadInfo; |
| 913 | } |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 914 | status = mpClientInterface->openOutput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 915 | &output, |
| 916 | &config, |
| 917 | &outputDesc->mDevice, |
| 918 | String8(""), |
| 919 | &outputDesc->mLatency, |
| 920 | outputDesc->mFlags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 921 | |
| 922 | // only accept an output with the requested parameters |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 923 | if (status != NO_ERROR || |
| 924 | (samplingRate != 0 && samplingRate != config.sample_rate) || |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 925 | (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) || |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 926 | (channelMask != 0 && channelMask != config.channel_mask)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 927 | ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d," |
| 928 | "format %d %d, channelMask %04x %04x", output, samplingRate, |
| 929 | outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask, |
| 930 | outputDesc->mChannelMask); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 931 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 932 | mpClientInterface->closeOutput(output); |
| 933 | } |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 934 | // fall back to mixer output if possible when the direct output could not be open |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 935 | if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) { |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 936 | goto non_direct_output; |
| 937 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 938 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 939 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 940 | outputDesc->mSamplingRate = config.sample_rate; |
| 941 | outputDesc->mChannelMask = config.channel_mask; |
| 942 | outputDesc->mFormat = config.format; |
| 943 | outputDesc->mRefCount[stream] = 0; |
| 944 | outputDesc->mStopTime[stream] = 0; |
| 945 | outputDesc->mDirectOpenCount = 1; |
| 946 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 947 | audio_io_handle_t srcOutput = getOutputForEffect(); |
| 948 | addOutput(output, outputDesc); |
| 949 | audio_io_handle_t dstOutput = getOutputForEffect(); |
| 950 | if (dstOutput == output) { |
| 951 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput); |
| 952 | } |
| 953 | mPreviousOutputs = mOutputs; |
| 954 | ALOGV("getOutput() returns new direct output %d", output); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 955 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 956 | return output; |
| 957 | } |
| 958 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 959 | non_direct_output: |
Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 960 | |
| 961 | // A request for HW A/V sync cannot fallback to a mixed output because time |
| 962 | // stamps are embedded in audio data |
| 963 | if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 964 | return AUDIO_IO_HANDLE_NONE; |
| 965 | } |
| 966 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 967 | // ignoring channel mask due to downmix capability in mixer |
| 968 | |
| 969 | // open a non direct output |
| 970 | |
| 971 | // for non direct outputs, only PCM is supported |
| 972 | if (audio_is_linear_pcm(format)) { |
| 973 | // get which output is suitable for the specified stream. The actual |
| 974 | // routing change will happen when startOutput() will be called |
| 975 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 976 | |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 977 | // at this stage we should ignore the DIRECT flag as no direct output could be found earlier |
| 978 | flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT); |
| 979 | output = selectOutput(outputs, flags, format); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 980 | } |
| 981 | ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d," |
| 982 | "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags); |
| 983 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 984 | ALOGV(" getOutputForDevice() returns output %d", output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 985 | |
| 986 | return output; |
| 987 | } |
| 988 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 989 | 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] | 990 | audio_output_flags_t flags, |
| 991 | audio_format_t format) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 992 | { |
| 993 | // select one output among several that provide a path to a particular device or set of |
| 994 | // devices (the list was previously build by getOutputsForDevice()). |
| 995 | // The priority is as follows: |
| 996 | // 1: the output with the highest number of requested policy flags |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 997 | // 2: the output with the bit depth the closest to the requested one |
| 998 | // 3: the primary output |
| 999 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1000 | |
| 1001 | if (outputs.size() == 0) { |
| 1002 | return 0; |
| 1003 | } |
| 1004 | if (outputs.size() == 1) { |
| 1005 | return outputs[0]; |
| 1006 | } |
| 1007 | |
| 1008 | int maxCommonFlags = 0; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1009 | audio_io_handle_t outputForFlags = 0; |
| 1010 | audio_io_handle_t outputForPrimary = 0; |
| 1011 | audio_io_handle_t outputForFormat = 0; |
| 1012 | audio_format_t bestFormat = AUDIO_FORMAT_INVALID; |
| 1013 | audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1014 | |
| 1015 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1016 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1017 | if (!outputDesc->isDuplicated()) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1018 | // if a valid format is specified, skip output if not compatible |
| 1019 | if (format != AUDIO_FORMAT_INVALID) { |
| 1020 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1021 | if (!audio_formats_match(format, outputDesc->mFormat)) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1022 | continue; |
| 1023 | } |
| 1024 | } else if (!audio_is_linear_pcm(format)) { |
| 1025 | continue; |
| 1026 | } |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1027 | if (AudioPort::isBetterFormatMatch( |
| 1028 | outputDesc->mFormat, bestFormat, format)) { |
| 1029 | outputForFormat = outputs[i]; |
| 1030 | bestFormat = outputDesc->mFormat; |
| 1031 | } |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1032 | } |
| 1033 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1034 | int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags); |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1035 | if (commonFlags >= maxCommonFlags) { |
| 1036 | if (commonFlags == maxCommonFlags) { |
| 1037 | if (AudioPort::isBetterFormatMatch( |
| 1038 | outputDesc->mFormat, bestFormatForFlags, format)) { |
| 1039 | outputForFlags = outputs[i]; |
| 1040 | bestFormatForFlags = outputDesc->mFormat; |
| 1041 | } |
| 1042 | } else { |
| 1043 | outputForFlags = outputs[i]; |
| 1044 | maxCommonFlags = commonFlags; |
| 1045 | bestFormatForFlags = outputDesc->mFormat; |
| 1046 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1047 | ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags); |
| 1048 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1049 | if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1050 | outputForPrimary = outputs[i]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1051 | } |
| 1052 | } |
| 1053 | } |
| 1054 | |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1055 | if (outputForFlags != 0) { |
| 1056 | return outputForFlags; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1057 | } |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1058 | if (outputForFormat != 0) { |
| 1059 | return outputForFormat; |
| 1060 | } |
| 1061 | if (outputForPrimary != 0) { |
| 1062 | return outputForPrimary; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | return outputs[0]; |
| 1066 | } |
| 1067 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1068 | status_t AudioPolicyManager::startOutput(audio_io_handle_t output, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1069 | audio_stream_type_t stream, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1070 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1071 | { |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1072 | ALOGV("startOutput() output %d, stream %d, session %d", |
| 1073 | output, stream, session); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1074 | ssize_t index = mOutputs.indexOfKey(output); |
| 1075 | if (index < 0) { |
| 1076 | ALOGW("startOutput() unknown output %d", output); |
| 1077 | return BAD_VALUE; |
| 1078 | } |
| 1079 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1080 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
| 1081 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1082 | // Routing? |
| 1083 | mOutputRoutes.incRouteActivity(session); |
| 1084 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1085 | audio_devices_t newDevice; |
| 1086 | if (outputDesc->mPolicyMix != NULL) { |
| 1087 | newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 1088 | } else if (mOutputRoutes.hasRouteChanged(session)) { |
| 1089 | newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1090 | checkStrategyRoute(getStrategy(stream), output); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1091 | } else { |
| 1092 | newDevice = AUDIO_DEVICE_NONE; |
| 1093 | } |
| 1094 | |
| 1095 | uint32_t delayMs = 0; |
| 1096 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1097 | status_t status = startSource(outputDesc, stream, newDevice, &delayMs); |
| 1098 | |
| 1099 | if (status != NO_ERROR) { |
| 1100 | mOutputRoutes.decRouteActivity(session); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1101 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1102 | } |
| 1103 | // Automatically enable the remote submix input when output is started on a re routing mix |
| 1104 | // of type MIX_TYPE_RECORDERS |
| 1105 | if (audio_is_remote_submix_device(newDevice) && outputDesc->mPolicyMix != NULL && |
| 1106 | outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1107 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1108 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1109 | outputDesc->mPolicyMix->mDeviceAddress, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1110 | "remote-submix"); |
| 1111 | } |
| 1112 | |
| 1113 | if (delayMs != 0) { |
| 1114 | usleep(delayMs * 1000); |
| 1115 | } |
| 1116 | |
| 1117 | return status; |
| 1118 | } |
| 1119 | |
| 1120 | status_t AudioPolicyManager::startSource(sp<AudioOutputDescriptor> outputDesc, |
| 1121 | audio_stream_type_t stream, |
| 1122 | audio_devices_t device, |
| 1123 | uint32_t *delayMs) |
| 1124 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1125 | // cannot start playback of STREAM_TTS if any other output is being used |
| 1126 | uint32_t beaconMuteLatency = 0; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1127 | |
| 1128 | *delayMs = 0; |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1129 | if (stream == AUDIO_STREAM_TTS) { |
| 1130 | ALOGV("\t found BEACON stream"); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 1131 | if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1132 | return INVALID_OPERATION; |
| 1133 | } else { |
| 1134 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); |
| 1135 | } |
| 1136 | } else { |
| 1137 | // some playback other than beacon starts |
| 1138 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); |
| 1139 | } |
| 1140 | |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1141 | // check active before incrementing usage count |
| 1142 | bool force = !outputDesc->isActive(); |
| 1143 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1144 | // increment usage count for this stream on the requested output: |
| 1145 | // NOTE that the usage count is the same for duplicated output and hardware output which is |
| 1146 | // necessary for a correct control of hardware output routing by startOutput() and stopOutput() |
| 1147 | outputDesc->changeRefCount(stream, 1); |
| 1148 | |
Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 1149 | if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1150 | // starting an output being rerouted? |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1151 | if (device == AUDIO_DEVICE_NONE) { |
| 1152 | device = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1153 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1154 | routing_strategy strategy = getStrategy(stream); |
| 1155 | bool shouldWait = (strategy == STRATEGY_SONIFICATION) || |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1156 | (strategy == STRATEGY_SONIFICATION_RESPECTFUL) || |
| 1157 | (beaconMuteLatency > 0); |
| 1158 | uint32_t waitMs = beaconMuteLatency; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1159 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1160 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1161 | if (desc != outputDesc) { |
| 1162 | // force a device change if any other output is managed by the same hw |
| 1163 | // module and has a current device selection that differs from selected device. |
| 1164 | // In this case, the audio HAL must receive the new device selection so that it can |
| 1165 | // change the device currently selected by the other active output. |
| 1166 | if (outputDesc->sharesHwModuleWith(desc) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1167 | desc->device() != device) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1168 | force = true; |
| 1169 | } |
| 1170 | // 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] | 1171 | // a notification so that audio focus effect can propagate, or that a mute/unmute |
| 1172 | // event occurred for beacon |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1173 | uint32_t latency = desc->latency(); |
| 1174 | if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) { |
| 1175 | waitMs = latency; |
| 1176 | } |
| 1177 | } |
| 1178 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1179 | uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1180 | |
| 1181 | // handle special case for sonification while in call |
| 1182 | if (isInCall()) { |
| 1183 | handleIncallSonification(stream, true, false); |
| 1184 | } |
| 1185 | |
| 1186 | // apply volume rules for current stream and device if necessary |
| 1187 | checkAndSetVolume(stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 1188 | mVolumeCurves->getVolumeIndex(stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1189 | outputDesc, |
| 1190 | device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1191 | |
| 1192 | // update the outputs if starting an output with a stream that can affect notification |
| 1193 | // routing |
| 1194 | handleNotificationRoutingForStream(stream); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1195 | |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 1196 | // force reevaluating accessibility routing when ringtone or alarm starts |
| 1197 | if (strategy == STRATEGY_SONIFICATION) { |
| 1198 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
| 1199 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1200 | } |
| 1201 | return NO_ERROR; |
| 1202 | } |
| 1203 | |
| 1204 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1205 | status_t AudioPolicyManager::stopOutput(audio_io_handle_t output, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1206 | audio_stream_type_t stream, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1207 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1208 | { |
| 1209 | ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session); |
| 1210 | ssize_t index = mOutputs.indexOfKey(output); |
| 1211 | if (index < 0) { |
| 1212 | ALOGW("stopOutput() unknown output %d", output); |
| 1213 | return BAD_VALUE; |
| 1214 | } |
| 1215 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1216 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1217 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1218 | if (outputDesc->mRefCount[stream] == 1) { |
| 1219 | // Automatically disable the remote submix input when output is stopped on a |
| 1220 | // re routing mix of type MIX_TYPE_RECORDERS |
| 1221 | if (audio_is_remote_submix_device(outputDesc->mDevice) && |
| 1222 | outputDesc->mPolicyMix != NULL && |
| 1223 | outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1224 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1225 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1226 | outputDesc->mPolicyMix->mDeviceAddress, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1227 | "remote-submix"); |
| 1228 | } |
| 1229 | } |
| 1230 | |
| 1231 | // Routing? |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1232 | bool forceDeviceUpdate = false; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1233 | if (outputDesc->mRefCount[stream] > 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1234 | int activityCount = mOutputRoutes.decRouteActivity(session); |
| 1235 | forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0)); |
| 1236 | |
| 1237 | if (forceDeviceUpdate) { |
| 1238 | checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE); |
| 1239 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1240 | } |
| 1241 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1242 | return stopSource(outputDesc, stream, forceDeviceUpdate); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | status_t AudioPolicyManager::stopSource(sp<AudioOutputDescriptor> outputDesc, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1246 | audio_stream_type_t stream, |
| 1247 | bool forceDeviceUpdate) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1248 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1249 | // always handle stream stop, check which stream type is stopping |
| 1250 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); |
| 1251 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1252 | // handle special case for sonification while in call |
| 1253 | if (isInCall()) { |
| 1254 | handleIncallSonification(stream, false, false); |
| 1255 | } |
| 1256 | |
| 1257 | if (outputDesc->mRefCount[stream] > 0) { |
| 1258 | // decrement usage count of this stream on the output |
| 1259 | outputDesc->changeRefCount(stream, -1); |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1260 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1261 | // store time at which the stream was stopped - see isStreamActive() |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1262 | if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1263 | outputDesc->mStopTime[stream] = systemTime(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1264 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1265 | // delay the device switch by twice the latency because stopOutput() is executed when |
| 1266 | // the track stop() command is received and at that time the audio track buffer can |
| 1267 | // still contain data that needs to be drained. The latency only covers the audio HAL |
| 1268 | // and kernel buffers. Also the latency does not always include additional delay in the |
| 1269 | // audio path (audio DSP, CODEC ...) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1270 | setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1271 | |
| 1272 | // force restoring the device selection on other active outputs if it differs from the |
| 1273 | // one being selected for this output |
| 1274 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 1275 | audio_io_handle_t curOutput = mOutputs.keyAt(i); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1276 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1277 | if (desc != outputDesc && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1278 | desc->isActive() && |
| 1279 | outputDesc->sharesHwModuleWith(desc) && |
| 1280 | (newDevice != desc->device())) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1281 | setOutputDevice(desc, |
| 1282 | getNewOutputDevice(desc, false /*fromCache*/), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1283 | true, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1284 | outputDesc->latency()*2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1285 | } |
| 1286 | } |
| 1287 | // update the outputs if stopping one with a stream that can affect notification routing |
| 1288 | handleNotificationRoutingForStream(stream); |
| 1289 | } |
| 1290 | return NO_ERROR; |
| 1291 | } else { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1292 | ALOGW("stopOutput() refcount is already 0"); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1293 | return INVALID_OPERATION; |
| 1294 | } |
| 1295 | } |
| 1296 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1297 | void AudioPolicyManager::releaseOutput(audio_io_handle_t output, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1298 | audio_stream_type_t stream __unused, |
| 1299 | audio_session_t session __unused) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1300 | { |
| 1301 | ALOGV("releaseOutput() %d", output); |
| 1302 | ssize_t index = mOutputs.indexOfKey(output); |
| 1303 | if (index < 0) { |
| 1304 | ALOGW("releaseOutput() releasing unknown output %d", output); |
| 1305 | return; |
| 1306 | } |
| 1307 | |
| 1308 | #ifdef AUDIO_POLICY_TEST |
| 1309 | int testIndex = testOutputIndex(output); |
| 1310 | if (testIndex != 0) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1311 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1312 | if (outputDesc->isActive()) { |
| 1313 | mpClientInterface->closeOutput(output); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1314 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1315 | mTestOutputs[testIndex] = 0; |
| 1316 | } |
| 1317 | return; |
| 1318 | } |
| 1319 | #endif //AUDIO_POLICY_TEST |
| 1320 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1321 | // Routing |
| 1322 | mOutputRoutes.removeRoute(session); |
| 1323 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1324 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1325 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1326 | if (desc->mDirectOpenCount <= 0) { |
| 1327 | ALOGW("releaseOutput() invalid open count %d for output %d", |
| 1328 | desc->mDirectOpenCount, output); |
| 1329 | return; |
| 1330 | } |
| 1331 | if (--desc->mDirectOpenCount == 0) { |
| 1332 | closeOutput(output); |
| 1333 | // If effects where present on the output, audioflinger moved them to the primary |
| 1334 | // output by default: move them back to the appropriate output. |
| 1335 | audio_io_handle_t dstOutput = getOutputForEffect(); |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 1336 | if (hasPrimaryOutput() && dstOutput != mPrimaryOutput->mIoHandle) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1337 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, |
| 1338 | mPrimaryOutput->mIoHandle, dstOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1339 | } |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1340 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1341 | } |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1346 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, |
| 1347 | audio_io_handle_t *input, |
| 1348 | audio_session_t session, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1349 | uid_t uid, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1350 | uint32_t samplingRate, |
| 1351 | audio_format_t format, |
| 1352 | audio_channel_mask_t channelMask, |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1353 | audio_input_flags_t flags, |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1354 | audio_port_handle_t selectedDeviceId, |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1355 | input_type_t *inputType) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1356 | { |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1357 | ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x," |
| 1358 | "session %d, flags %#x", |
| 1359 | attr->source, samplingRate, format, channelMask, session, flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1360 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1361 | *input = AUDIO_IO_HANDLE_NONE; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1362 | *inputType = API_INPUT_INVALID; |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1363 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1364 | audio_devices_t device; |
| 1365 | // handle legacy remote submix case where the address was not always specified |
| 1366 | String8 address = String8(""); |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1367 | audio_source_t inputSource = attr->source; |
| 1368 | audio_source_t halInputSource; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1369 | AudioMix *policyMix = NULL; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1370 | |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1371 | if (inputSource == AUDIO_SOURCE_DEFAULT) { |
| 1372 | inputSource = AUDIO_SOURCE_MIC; |
| 1373 | } |
| 1374 | halInputSource = inputSource; |
| 1375 | |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1376 | // Explicit routing? |
| 1377 | sp<DeviceDescriptor> deviceDesc; |
| 1378 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { |
| 1379 | if (mAvailableInputDevices[i]->getId() == selectedDeviceId) { |
| 1380 | deviceDesc = mAvailableInputDevices[i]; |
| 1381 | break; |
| 1382 | } |
| 1383 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1384 | mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1385 | |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1386 | if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX && |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1387 | strncmp(attr->tags, "addr=", strlen("addr=")) == 0) { |
Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 1388 | status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 1389 | if (ret != NO_ERROR) { |
| 1390 | return ret; |
| 1391 | } |
| 1392 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1393 | device = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 1394 | address = String8(attr->tags + strlen("addr=")); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1395 | } else { |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1396 | device = getDeviceAndMixForInputSource(inputSource, &policyMix); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1397 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1398 | ALOGW("getInputForAttr() could not find device for source %d", inputSource); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1399 | return BAD_VALUE; |
| 1400 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1401 | if (policyMix != NULL) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1402 | address = policyMix->mDeviceAddress; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1403 | if (policyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1404 | // there is an external policy, but this input is attached to a mix of recorders, |
| 1405 | // meaning it receives audio injected into the framework, so the recorder doesn't |
| 1406 | // know about it and is therefore considered "legacy" |
| 1407 | *inputType = API_INPUT_LEGACY; |
| 1408 | } else { |
| 1409 | // recording a mix of players defined by an external policy, we're rerouting for |
| 1410 | // an external policy |
| 1411 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
| 1412 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1413 | } else if (audio_is_remote_submix_device(device)) { |
| 1414 | address = String8("0"); |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1415 | *inputType = API_INPUT_MIX_CAPTURE; |
Eric Laurent | 82db269 | 2015-08-07 13:59:42 -0700 | [diff] [blame] | 1416 | } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) { |
| 1417 | *inputType = API_INPUT_TELEPHONY_RX; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1418 | } else { |
| 1419 | *inputType = API_INPUT_LEGACY; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1420 | } |
Ravi Kumar Alamanda | b367f5b | 2015-08-25 08:21:37 -0700 | [diff] [blame] | 1421 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1422 | } |
| 1423 | |
| 1424 | *input = getInputForDevice(device, address, session, uid, inputSource, |
| 1425 | samplingRate, format, channelMask, flags, |
| 1426 | policyMix); |
| 1427 | if (*input == AUDIO_IO_HANDLE_NONE) { |
| 1428 | mInputRoutes.removeRoute(session); |
| 1429 | return INVALID_OPERATION; |
| 1430 | } |
| 1431 | ALOGV("getInputForAttr() returns input type = %d", *inputType); |
| 1432 | return NO_ERROR; |
| 1433 | } |
| 1434 | |
| 1435 | |
| 1436 | audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device, |
| 1437 | String8 address, |
| 1438 | audio_session_t session, |
| 1439 | uid_t uid, |
| 1440 | audio_source_t inputSource, |
| 1441 | uint32_t samplingRate, |
| 1442 | audio_format_t format, |
| 1443 | audio_channel_mask_t channelMask, |
| 1444 | audio_input_flags_t flags, |
| 1445 | AudioMix *policyMix) |
| 1446 | { |
| 1447 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 1448 | audio_source_t halInputSource = inputSource; |
| 1449 | bool isSoundTrigger = false; |
| 1450 | |
| 1451 | if (inputSource == AUDIO_SOURCE_HOTWORD) { |
| 1452 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); |
| 1453 | if (index >= 0) { |
| 1454 | input = mSoundTriggerSessions.valueFor(session); |
| 1455 | isSoundTrigger = true; |
| 1456 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); |
| 1457 | ALOGV("SoundTrigger capture on session %d input %d", session, input); |
| 1458 | } else { |
| 1459 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1460 | } |
| 1461 | } |
| 1462 | |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1463 | // find a compatible input profile (not necessarily identical in parameters) |
| 1464 | sp<IOProfile> profile; |
| 1465 | // samplingRate and flags may be updated by getInputProfile |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1466 | uint32_t profileSamplingRate = (samplingRate == 0) ? SAMPLE_RATE_HZ_DEFAULT : samplingRate; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1467 | audio_format_t profileFormat = format; |
| 1468 | audio_channel_mask_t profileChannelMask = channelMask; |
| 1469 | audio_input_flags_t profileFlags = flags; |
| 1470 | for (;;) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1471 | profile = getInputProfile(device, address, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1472 | profileSamplingRate, profileFormat, profileChannelMask, |
| 1473 | profileFlags); |
| 1474 | if (profile != 0) { |
| 1475 | break; // success |
| 1476 | } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) { |
| 1477 | profileFlags = AUDIO_INPUT_FLAG_NONE; // retry |
| 1478 | } else { // fail |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1479 | ALOGW("getInputForDevice() could not find profile for device 0x%X," |
| 1480 | "samplingRate %u, format %#x, channelMask 0x%X, flags %#x", |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1481 | device, samplingRate, format, channelMask, flags); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1482 | return input; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1483 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1484 | } |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1485 | // Pick input sampling rate if not specified by client |
| 1486 | if (samplingRate == 0) { |
| 1487 | samplingRate = profileSamplingRate; |
| 1488 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1489 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1490 | if (profile->getModuleHandle() == 0) { |
| 1491 | ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName()); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1492 | return input; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1493 | } |
| 1494 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1495 | sp<AudioSession> audioSession = new AudioSession(session, |
| 1496 | inputSource, |
| 1497 | format, |
| 1498 | samplingRate, |
| 1499 | channelMask, |
| 1500 | flags, |
| 1501 | uid, |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 1502 | isSoundTrigger, |
| 1503 | policyMix, mpClientInterface); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1504 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1505 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1506 | // reuse an open input if possible |
| 1507 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 1508 | sp<AudioInputDescriptor> desc = mInputs.valueAt(i); |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1509 | // reuse input if: |
| 1510 | // - it shares the same profile |
| 1511 | // AND |
| 1512 | // - it is not a reroute submix input |
| 1513 | // AND |
| 1514 | // - it is: not used for sound trigger |
| 1515 | // OR |
| 1516 | // used for sound trigger and all clients use the same session ID |
| 1517 | // |
| 1518 | if ((profile == desc->mProfile) && |
| 1519 | (isSoundTrigger == desc->isSoundTrigger()) && |
| 1520 | !is_virtual_input_device(device)) { |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1521 | |
| 1522 | sp<AudioSession> as = desc->getAudioSession(session); |
| 1523 | if (as != 0) { |
| 1524 | // do not allow unmatching properties on same session |
| 1525 | if (as->matches(audioSession)) { |
| 1526 | as->changeOpenCount(1); |
| 1527 | } else { |
| 1528 | ALOGW("getInputForDevice() record with different attributes" |
| 1529 | " exists for session %d", session); |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1530 | break; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1531 | } |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1532 | } else if (isSoundTrigger) { |
| 1533 | break; |
| 1534 | } |
| 1535 | // force close input if current source is now the highest priority request on this input |
| 1536 | // and current input properties are not exactly as requested. |
| 1537 | if ((desc->mSamplingRate != samplingRate || |
| 1538 | desc->mChannelMask != channelMask || |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1539 | !audio_formats_match(desc->mFormat, format)) && |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1540 | (source_priority(desc->getHighestPrioritySource(false /*activeOnly*/)) < |
| 1541 | source_priority(inputSource))) { |
| 1542 | ALOGV("%s: ", __FUNCTION__); |
| 1543 | AudioSessionCollection sessions = desc->getAudioSessions(false /*activeOnly*/); |
| 1544 | for (size_t j = 0; j < sessions.size(); j++) { |
| 1545 | audio_session_t currentSession = sessions.keyAt(j); |
| 1546 | stopInput(desc->mIoHandle, currentSession); |
| 1547 | releaseInput(desc->mIoHandle, currentSession); |
| 1548 | } |
| 1549 | break; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1550 | } else { |
| 1551 | desc->addAudioSession(session, audioSession); |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1552 | ALOGV("%s: reusing input %d", __FUNCTION__, mInputs.keyAt(i)); |
| 1553 | return mInputs.keyAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1554 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1555 | } |
| 1556 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1557 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1558 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1559 | config.sample_rate = profileSamplingRate; |
| 1560 | config.channel_mask = profileChannelMask; |
| 1561 | config.format = profileFormat; |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1562 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1563 | status_t status = mpClientInterface->openInput(profile->getModuleHandle(), |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1564 | &input, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1565 | &config, |
| 1566 | &device, |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 1567 | address, |
Eric Laurent | 1c9c2cc | 2014-08-28 19:37:25 -0700 | [diff] [blame] | 1568 | halInputSource, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1569 | profileFlags); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1570 | |
| 1571 | // only accept input with the exact requested set of parameters |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1572 | if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE || |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1573 | (profileSamplingRate != config.sample_rate) || |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1574 | !audio_formats_match(profileFormat, config.format) || |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1575 | (profileChannelMask != config.channel_mask)) { |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1576 | ALOGW("getInputForAttr() failed opening input: samplingRate %d" |
| 1577 | ", format %d, channelMask %x", |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1578 | samplingRate, format, channelMask); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1579 | if (input != AUDIO_IO_HANDLE_NONE) { |
| 1580 | mpClientInterface->closeInput(input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1581 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1582 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1583 | } |
| 1584 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1585 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile); |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1586 | inputDesc->mSamplingRate = profileSamplingRate; |
| 1587 | inputDesc->mFormat = profileFormat; |
| 1588 | inputDesc->mChannelMask = profileChannelMask; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1589 | inputDesc->mDevice = device; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1590 | inputDesc->mPolicyMix = policyMix; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1591 | inputDesc->addAudioSession(session, audioSession); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1592 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1593 | addInput(input, inputDesc); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1594 | mpClientInterface->onAudioPortListUpdate(); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1595 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1596 | return input; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1597 | } |
| 1598 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1599 | bool AudioPolicyManager::isConcurentCaptureAllowed(const sp<AudioInputDescriptor>& inputDesc, |
| 1600 | const sp<AudioSession>& audioSession) |
| 1601 | { |
| 1602 | // Do not allow capture if an active voice call is using a software patch and |
| 1603 | // the call TX source device is on the same HW module. |
| 1604 | // FIXME: would be better to refine to only inputs whose profile connects to the |
| 1605 | // call TX device but this information is not in the audio patch |
| 1606 | if (mCallTxPatch != 0 && |
| 1607 | inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) { |
| 1608 | return false; |
| 1609 | } |
| 1610 | |
| 1611 | // starting concurrent capture is enabled if: |
| 1612 | // 1) capturing for re-routing |
| 1613 | // 2) capturing for HOTWORD source |
| 1614 | // 3) capturing for FM TUNER source |
| 1615 | // 3) All other active captures are either for re-routing or HOTWORD |
| 1616 | |
| 1617 | if (is_virtual_input_device(inputDesc->mDevice) || |
| 1618 | audioSession->inputSource() == AUDIO_SOURCE_HOTWORD || |
| 1619 | audioSession->inputSource() == AUDIO_SOURCE_FM_TUNER) { |
| 1620 | return true; |
| 1621 | } |
| 1622 | |
| 1623 | Vector< sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs(); |
| 1624 | for (size_t i = 0; i < activeInputs.size(); i++) { |
| 1625 | sp<AudioInputDescriptor> activeInput = activeInputs[i]; |
| 1626 | if ((activeInput->inputSource() != AUDIO_SOURCE_HOTWORD) && |
| 1627 | (activeInput->inputSource() != AUDIO_SOURCE_FM_TUNER) && |
| 1628 | !is_virtual_input_device(activeInput->mDevice)) { |
| 1629 | return false; |
| 1630 | } |
| 1631 | } |
| 1632 | |
| 1633 | return true; |
| 1634 | } |
| 1635 | |
| 1636 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1637 | status_t AudioPolicyManager::startInput(audio_io_handle_t input, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1638 | audio_session_t session, |
| 1639 | concurrency_type__mask_t *concurrency) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1640 | { |
| 1641 | ALOGV("startInput() input %d", input); |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1642 | *concurrency = API_INPUT_CONCURRENCY_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1643 | ssize_t index = mInputs.indexOfKey(input); |
| 1644 | if (index < 0) { |
| 1645 | ALOGW("startInput() unknown input %d", input); |
| 1646 | return BAD_VALUE; |
| 1647 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1648 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1649 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1650 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
| 1651 | if (audioSession == 0) { |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1652 | ALOGW("startInput() unknown session %d on input %d", session, input); |
| 1653 | return BAD_VALUE; |
| 1654 | } |
| 1655 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1656 | if (!isConcurentCaptureAllowed(inputDesc, audioSession)) { |
| 1657 | ALOGW("startInput(%d) failed: other input already started", input); |
| 1658 | return INVALID_OPERATION; |
| 1659 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1660 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1661 | if (isInCall()) { |
| 1662 | *concurrency |= API_INPUT_CONCURRENCY_CALL; |
| 1663 | } |
| 1664 | if (mInputs.activeInputsCount() != 0) { |
| 1665 | *concurrency |= API_INPUT_CONCURRENCY_CAPTURE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1666 | } |
| 1667 | |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 1668 | // increment activity count before calling getNewInputDevice() below as only active sessions |
| 1669 | // are considered for device selection |
| 1670 | audioSession->changeActiveCount(1); |
| 1671 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1672 | // Routing? |
| 1673 | mInputRoutes.incRouteActivity(session); |
| 1674 | |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame^] | 1675 | if (audioSession->activeCount() == 1 || mInputRoutes.hasRouteChanged(session)) { |
Jean-Michel Trivi | 2b0c1fc | 2015-04-15 17:29:28 -0700 | [diff] [blame] | 1676 | |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame^] | 1677 | setInputDevice(input, getNewInputDevice(inputDesc), true /* force */); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1678 | |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame^] | 1679 | if (inputDesc->getAudioSessionCount(true/*activeOnly*/) == 1) { |
| 1680 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 1681 | if ((inputDesc->mPolicyMix != NULL) |
| 1682 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 1683 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
| 1684 | MIX_STATE_MIXING); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1685 | } |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame^] | 1686 | |
| 1687 | if (mInputs.activeInputsCount() == 0) { |
| 1688 | SoundTrigger::setCaptureState(true); |
| 1689 | } |
| 1690 | |
| 1691 | // automatically enable the remote submix output when input is started if not |
| 1692 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 1693 | // For remote submix (a virtual device), we open only one input per capture request. |
| 1694 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 1695 | String8 address = String8(""); |
| 1696 | if (inputDesc->mPolicyMix == NULL) { |
| 1697 | address = String8("0"); |
| 1698 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 1699 | address = inputDesc->mPolicyMix->mDeviceAddress; |
| 1700 | } |
| 1701 | if (address != "") { |
| 1702 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 1703 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 1704 | address, "remote-submix"); |
| 1705 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1706 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1707 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1708 | } |
| 1709 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1710 | ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1711 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1712 | return NO_ERROR; |
| 1713 | } |
| 1714 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1715 | status_t AudioPolicyManager::stopInput(audio_io_handle_t input, |
| 1716 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1717 | { |
| 1718 | ALOGV("stopInput() input %d", input); |
| 1719 | ssize_t index = mInputs.indexOfKey(input); |
| 1720 | if (index < 0) { |
| 1721 | ALOGW("stopInput() unknown input %d", input); |
| 1722 | return BAD_VALUE; |
| 1723 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1724 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1725 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1726 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1727 | if (index < 0) { |
| 1728 | ALOGW("stopInput() unknown session %d on input %d", session, input); |
| 1729 | return BAD_VALUE; |
| 1730 | } |
| 1731 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1732 | if (audioSession->activeCount() == 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1733 | ALOGW("stopInput() input %d already stopped", input); |
| 1734 | return INVALID_OPERATION; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1735 | } |
| 1736 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1737 | audioSession->changeActiveCount(-1); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1738 | |
| 1739 | // Routing? |
| 1740 | mInputRoutes.decRouteActivity(session); |
| 1741 | |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame^] | 1742 | if (audioSession->activeCount() == 0) { |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 1743 | |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame^] | 1744 | if (inputDesc->isActive()) { |
| 1745 | setInputDevice(input, getNewInputDevice(inputDesc), false /* force */); |
| 1746 | } else { |
| 1747 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 1748 | if ((inputDesc->mPolicyMix != NULL) |
| 1749 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 1750 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
| 1751 | MIX_STATE_IDLE); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 1752 | } |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame^] | 1753 | |
| 1754 | // automatically disable the remote submix output when input is stopped if not |
| 1755 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 1756 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 1757 | String8 address = String8(""); |
| 1758 | if (inputDesc->mPolicyMix == NULL) { |
| 1759 | address = String8("0"); |
| 1760 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 1761 | address = inputDesc->mPolicyMix->mDeviceAddress; |
| 1762 | } |
| 1763 | if (address != "") { |
| 1764 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 1765 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 1766 | address, "remote-submix"); |
| 1767 | } |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 1768 | } |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 1769 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1770 | resetInputDevice(input); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 1771 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1772 | if (mInputs.activeInputsCount() == 0) { |
| 1773 | SoundTrigger::setCaptureState(false); |
| 1774 | } |
| 1775 | inputDesc->clearPreemptedSessions(); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 1776 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1777 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1778 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1779 | } |
| 1780 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1781 | void AudioPolicyManager::releaseInput(audio_io_handle_t input, |
| 1782 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1783 | { |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 1784 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1785 | ALOGV("releaseInput() %d", input); |
| 1786 | ssize_t index = mInputs.indexOfKey(input); |
| 1787 | if (index < 0) { |
| 1788 | ALOGW("releaseInput() releasing unknown input %d", input); |
| 1789 | return; |
| 1790 | } |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1791 | |
| 1792 | // Routing |
| 1793 | mInputRoutes.removeRoute(session); |
| 1794 | |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1795 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
| 1796 | ALOG_ASSERT(inputDesc != 0); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1797 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1798 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1799 | if (index < 0) { |
| 1800 | ALOGW("releaseInput() unknown session %d on input %d", session, input); |
| 1801 | return; |
| 1802 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1803 | |
| 1804 | if (audioSession->openCount() == 0) { |
| 1805 | ALOGW("releaseInput() invalid open count %d on session %d", |
| 1806 | audioSession->openCount(), session); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1807 | return; |
| 1808 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1809 | |
| 1810 | if (audioSession->changeOpenCount(-1) == 0) { |
| 1811 | inputDesc->removeAudioSession(session); |
| 1812 | } |
| 1813 | |
Jean-Michel Trivi | 65bfe91 | 2015-12-04 15:56:47 -0800 | [diff] [blame] | 1814 | if (inputDesc->getOpenRefCount() > 0) { |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1815 | ALOGV("releaseInput() exit > 0"); |
| 1816 | return; |
| 1817 | } |
| 1818 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1819 | closeInput(input); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1820 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1821 | ALOGV("releaseInput() exit"); |
| 1822 | } |
| 1823 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1824 | void AudioPolicyManager::closeAllInputs() { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1825 | bool patchRemoved = false; |
| 1826 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1827 | for(size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1828 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index); |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 1829 | ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1830 | if (patch_index >= 0) { |
| 1831 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index); |
| 1832 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 1833 | mAudioPatches.removeItemsAt(patch_index); |
| 1834 | patchRemoved = true; |
| 1835 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1836 | mpClientInterface->closeInput(mInputs.keyAt(input_index)); |
| 1837 | } |
| 1838 | mInputs.clear(); |
Chris Thornton | 52785f3 | 2016-02-09 17:28:49 -0800 | [diff] [blame] | 1839 | SoundTrigger::setCaptureState(false); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1840 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1841 | |
| 1842 | if (patchRemoved) { |
| 1843 | mpClientInterface->onAudioPatchListUpdate(); |
| 1844 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1845 | } |
| 1846 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1847 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1848 | int indexMin, |
| 1849 | int indexMax) |
| 1850 | { |
| 1851 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 1852 | mVolumeCurves->initStreamVolume(stream, indexMin, indexMax); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1853 | |
| 1854 | // initialize other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 1855 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 1856 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1857 | continue; |
| 1858 | } |
| 1859 | mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1860 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1861 | } |
| 1862 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1863 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1864 | int index, |
| 1865 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1866 | { |
| 1867 | |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 1868 | if ((index < mVolumeCurves->getVolumeIndexMin(stream)) || |
| 1869 | (index > mVolumeCurves->getVolumeIndexMax(stream))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1870 | return BAD_VALUE; |
| 1871 | } |
| 1872 | if (!audio_is_output_device(device)) { |
| 1873 | return BAD_VALUE; |
| 1874 | } |
| 1875 | |
| 1876 | // Force max volume if stream cannot be muted |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 1877 | if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1878 | |
| 1879 | ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d", |
| 1880 | stream, device, index); |
| 1881 | |
| 1882 | // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and |
| 1883 | // clear all device specific values |
| 1884 | if (device == AUDIO_DEVICE_OUT_DEFAULT) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 1885 | mVolumeCurves->clearCurrentVolumeIndex(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1886 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1887 | |
| 1888 | // update other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 1889 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 1890 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1891 | continue; |
| 1892 | } |
| 1893 | mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index); |
| 1894 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1895 | |
Eric Laurent | 31551f8 | 2014-10-10 18:21:56 -0700 | [diff] [blame] | 1896 | // update volume on all outputs whose current device is also selected by the same |
| 1897 | // strategy as the device specified by the caller |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1898 | status_t status = NO_ERROR; |
| 1899 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1900 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 1901 | audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device()); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 1902 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 1903 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1904 | continue; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1905 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 1906 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1907 | audio_devices_t curStreamDevice = getDeviceForStrategy(curStrategy, true /*fromCache*/); |
| 1908 | // it is possible that the requested device is not selected by the strategy |
| 1909 | // (e.g an explicit audio patch is active causing getDevicesForStream() |
| 1910 | // to return this device. We must make sure that the device passed is part of the |
| 1911 | // devices considered when applying volume below. |
| 1912 | curStreamDevice |= device; |
| 1913 | |
| 1914 | if (((device == AUDIO_DEVICE_OUT_DEFAULT) || |
| 1915 | ((curDevice & curStreamDevice) != 0))) { |
| 1916 | status_t volStatus = |
| 1917 | checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice); |
| 1918 | if (volStatus != NO_ERROR) { |
| 1919 | status = volStatus; |
| 1920 | } |
| 1921 | } |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1922 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1923 | } |
| 1924 | return status; |
| 1925 | } |
| 1926 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1927 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1928 | int *index, |
| 1929 | audio_devices_t device) |
| 1930 | { |
| 1931 | if (index == NULL) { |
| 1932 | return BAD_VALUE; |
| 1933 | } |
| 1934 | if (!audio_is_output_device(device)) { |
| 1935 | return BAD_VALUE; |
| 1936 | } |
| 1937 | // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to |
| 1938 | // the strategy the stream belongs to. |
| 1939 | if (device == AUDIO_DEVICE_OUT_DEFAULT) { |
| 1940 | device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); |
| 1941 | } |
François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 1942 | device = Volume::getDeviceForVolume(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1943 | |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 1944 | *index = mVolumeCurves->getVolumeIndex(stream, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1945 | ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index); |
| 1946 | return NO_ERROR; |
| 1947 | } |
| 1948 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1949 | audio_io_handle_t AudioPolicyManager::selectOutputForEffects( |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1950 | const SortedVector<audio_io_handle_t>& outputs) |
| 1951 | { |
| 1952 | // select one output among several suitable for global effects. |
| 1953 | // The priority is as follows: |
| 1954 | // 1: An offloaded output. If the effect ends up not being offloadable, |
| 1955 | // AudioFlinger will invalidate the track and the offloaded output |
| 1956 | // will be closed causing the effect to be moved to a PCM output. |
| 1957 | // 2: A deep buffer output |
| 1958 | // 3: the first output in the list |
| 1959 | |
| 1960 | if (outputs.size() == 0) { |
| 1961 | return 0; |
| 1962 | } |
| 1963 | |
| 1964 | audio_io_handle_t outputOffloaded = 0; |
| 1965 | audio_io_handle_t outputDeepBuffer = 0; |
| 1966 | |
| 1967 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1968 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1969 | ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1970 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 1971 | outputOffloaded = outputs[i]; |
| 1972 | } |
| 1973 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { |
| 1974 | outputDeepBuffer = outputs[i]; |
| 1975 | } |
| 1976 | } |
| 1977 | |
| 1978 | ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d", |
| 1979 | outputOffloaded, outputDeepBuffer); |
| 1980 | if (outputOffloaded != 0) { |
| 1981 | return outputOffloaded; |
| 1982 | } |
| 1983 | if (outputDeepBuffer != 0) { |
| 1984 | return outputDeepBuffer; |
| 1985 | } |
| 1986 | |
| 1987 | return outputs[0]; |
| 1988 | } |
| 1989 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1990 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1991 | { |
| 1992 | // apply simple rule where global effects are attached to the same output as MUSIC streams |
| 1993 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1994 | routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1995 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 1996 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs); |
| 1997 | |
| 1998 | audio_io_handle_t output = selectOutputForEffects(dstOutputs); |
| 1999 | ALOGV("getOutputForEffect() got output %d for fx %s flags %x", |
| 2000 | output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags); |
| 2001 | |
| 2002 | return output; |
| 2003 | } |
| 2004 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2005 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2006 | audio_io_handle_t io, |
| 2007 | uint32_t strategy, |
| 2008 | int session, |
| 2009 | int id) |
| 2010 | { |
| 2011 | ssize_t index = mOutputs.indexOfKey(io); |
| 2012 | if (index < 0) { |
| 2013 | index = mInputs.indexOfKey(io); |
| 2014 | if (index < 0) { |
| 2015 | ALOGW("registerEffect() unknown io %d", io); |
| 2016 | return INVALID_OPERATION; |
| 2017 | } |
| 2018 | } |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2019 | return mEffects.registerEffect(desc, io, strategy, session, id); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2020 | } |
| 2021 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2022 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2023 | { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2024 | bool active = false; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2025 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) { |
| 2026 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2027 | continue; |
| 2028 | } |
| 2029 | active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs); |
| 2030 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2031 | return active; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2032 | } |
| 2033 | |
| 2034 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2035 | { |
| 2036 | return mOutputs.isStreamActiveRemotely(stream, inPastMs); |
| 2037 | } |
| 2038 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2039 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2040 | { |
| 2041 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2042 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2043 | if (inputDescriptor->isSourceActive(source)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2044 | return true; |
| 2045 | } |
| 2046 | } |
| 2047 | return false; |
| 2048 | } |
| 2049 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2050 | // Register a list of custom mixes with their attributes and format. |
| 2051 | // When a mix is registered, corresponding input and output profiles are |
| 2052 | // added to the remote submix hw module. The profile contains only the |
| 2053 | // parameters (sampling rate, format...) specified by the mix. |
| 2054 | // The corresponding input remote submix device is also connected. |
| 2055 | // |
| 2056 | // When a remote submix device is connected, the address is checked to select the |
| 2057 | // appropriate profile and the corresponding input or output stream is opened. |
| 2058 | // |
| 2059 | // When capture starts, getInputForAttr() will: |
| 2060 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2061 | // - 2 if none found, getDeviceForInputSource() will: |
| 2062 | // - 2.1 look for a mix matching the attributes source |
| 2063 | // - 2.2 if none found, default to device selection by policy rules |
| 2064 | // At this time, the corresponding output remote submix device is also connected |
| 2065 | // and active playback use cases can be transferred to this mix if needed when reconnecting |
| 2066 | // after AudioTracks are invalidated |
| 2067 | // |
| 2068 | // When playback starts, getOutputForAttr() will: |
| 2069 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2070 | // - 2 if none found, look for a mix matching the attributes usage |
| 2071 | // - 3 if none found, default to device and output selection by policy rules. |
| 2072 | |
| 2073 | status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes) |
| 2074 | { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2075 | ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size()); |
| 2076 | status_t res = NO_ERROR; |
| 2077 | |
| 2078 | sp<HwModule> rSubmixModule; |
| 2079 | // examine each mix's route type |
| 2080 | for (size_t i = 0; i < mixes.size(); i++) { |
| 2081 | // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination |
| 2082 | if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) { |
| 2083 | res = INVALID_OPERATION; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2084 | break; |
| 2085 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2086 | if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) { |
| 2087 | // Loop back through "remote submix" |
| 2088 | if (rSubmixModule == 0) { |
| 2089 | for (size_t j = 0; i < mHwModules.size(); j++) { |
| 2090 | if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0 |
| 2091 | && mHwModules[j]->mHandle != 0) { |
| 2092 | rSubmixModule = mHwModules[j]; |
| 2093 | break; |
| 2094 | } |
| 2095 | } |
| 2096 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2097 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2098 | ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK", i, mixes.size()); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2099 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2100 | if (rSubmixModule == 0) { |
| 2101 | ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", i); |
| 2102 | res = INVALID_OPERATION; |
| 2103 | break; |
| 2104 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2105 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2106 | String8 address = mixes[i].mDeviceAddress; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2107 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2108 | if (mPolicyMixes.registerMix(address, mixes[i], 0 /*output desc*/) != NO_ERROR) { |
| 2109 | ALOGE(" Error regisering mix %zu for address %s", i, address.string()); |
| 2110 | res = INVALID_OPERATION; |
| 2111 | break; |
| 2112 | } |
| 2113 | audio_config_t outputConfig = mixes[i].mFormat; |
| 2114 | audio_config_t inputConfig = mixes[i].mFormat; |
| 2115 | // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in |
| 2116 | // stereo and let audio flinger do the channel conversion if needed. |
| 2117 | outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 2118 | inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
| 2119 | rSubmixModule->addOutputProfile(address, &outputConfig, |
| 2120 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address); |
| 2121 | rSubmixModule->addInputProfile(address, &inputConfig, |
| 2122 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, address); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2123 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2124 | if (mixes[i].mMixType == MIX_TYPE_PLAYERS) { |
| 2125 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2126 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2127 | address.string(), "remote-submix"); |
| 2128 | } else { |
| 2129 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2130 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2131 | address.string(), "remote-submix"); |
| 2132 | } |
| 2133 | } else if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 2134 | ALOGV("registerPolicyMixes() mix %zu of %zu is RENDER", i, mixes.size()); |
| 2135 | String8 address = mixes[i].mDeviceAddress; |
| 2136 | |
| 2137 | audio_devices_t device = mixes[i].mDeviceType; |
| 2138 | |
| 2139 | for (size_t j = 0 ; j < mOutputs.size() ; j++) { |
| 2140 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j); |
| 2141 | sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle()); |
| 2142 | if ((patch != 0) && (patch->mPatch.num_sinks != 0) |
| 2143 | && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE) |
| 2144 | && (patch->mPatch.sinks[0].ext.device.type == device) |
| 2145 | && (patch->mPatch.sinks[0].ext.device.address == address)) { |
| 2146 | |
| 2147 | if (mPolicyMixes.registerMix(address, mixes[i], desc) != NO_ERROR) { |
| 2148 | res = INVALID_OPERATION; |
| 2149 | } |
| 2150 | break; |
| 2151 | } |
| 2152 | } |
| 2153 | |
| 2154 | if (res != NO_ERROR) { |
| 2155 | ALOGE(" Error registering mix %zu for device 0x%X addr %s", |
| 2156 | i,device, address.string()); |
| 2157 | res = INVALID_OPERATION; |
| 2158 | break; |
| 2159 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2160 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2161 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2162 | if (res != NO_ERROR) { |
| 2163 | unregisterPolicyMixes(mixes); |
| 2164 | } |
| 2165 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2166 | } |
| 2167 | |
| 2168 | status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes) |
| 2169 | { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2170 | ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2171 | status_t res = NO_ERROR; |
| 2172 | sp<HwModule> rSubmixModule; |
| 2173 | // examine each mix's route type |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2174 | for (size_t i = 0; i < mixes.size(); i++) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2175 | if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2176 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2177 | if (rSubmixModule == 0) { |
| 2178 | for (size_t j = 0; i < mHwModules.size(); j++) { |
| 2179 | if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0 |
| 2180 | && mHwModules[j]->mHandle != 0) { |
| 2181 | rSubmixModule = mHwModules[j]; |
| 2182 | break; |
| 2183 | } |
| 2184 | } |
| 2185 | } |
| 2186 | if (rSubmixModule == 0) { |
| 2187 | res = INVALID_OPERATION; |
| 2188 | continue; |
| 2189 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2190 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2191 | String8 address = mixes[i].mDeviceAddress; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2192 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2193 | if (mPolicyMixes.unregisterMix(address) != NO_ERROR) { |
| 2194 | res = INVALID_OPERATION; |
| 2195 | continue; |
| 2196 | } |
| 2197 | |
| 2198 | if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) == |
| 2199 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2200 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2201 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2202 | address.string(), "remote-submix"); |
| 2203 | } |
| 2204 | if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) == |
| 2205 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2206 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2207 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2208 | address.string(), "remote-submix"); |
| 2209 | } |
| 2210 | rSubmixModule->removeOutputProfile(address); |
| 2211 | rSubmixModule->removeInputProfile(address); |
| 2212 | |
| 2213 | } if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 2214 | if (mPolicyMixes.unregisterMix(mixes[i].mDeviceAddress) != NO_ERROR) { |
| 2215 | res = INVALID_OPERATION; |
| 2216 | continue; |
| 2217 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2218 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2219 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2220 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2221 | } |
| 2222 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2223 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2224 | status_t AudioPolicyManager::dump(int fd) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2225 | { |
| 2226 | const size_t SIZE = 256; |
| 2227 | char buffer[SIZE]; |
| 2228 | String8 result; |
| 2229 | |
| 2230 | snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this); |
| 2231 | result.append(buffer); |
| 2232 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 2233 | snprintf(buffer, SIZE, " Primary Output: %d\n", |
| 2234 | hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2235 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2236 | snprintf(buffer, SIZE, " Phone state: %d\n", mEngine->getPhoneState()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2237 | result.append(buffer); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2238 | snprintf(buffer, SIZE, " Force use for communications %d\n", |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2239 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2240 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2241 | 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] | 2242 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2243 | 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] | 2244 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2245 | 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] | 2246 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2247 | 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] | 2248 | result.append(buffer); |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 2249 | snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n", |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2250 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO)); |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 2251 | result.append(buffer); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 2252 | snprintf(buffer, SIZE, " Force use for encoded surround output %d\n", |
| 2253 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND)); |
| 2254 | result.append(buffer); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 2255 | snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available"); |
| 2256 | result.append(buffer); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 2257 | snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off"); |
| 2258 | result.append(buffer); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 2259 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2260 | write(fd, result.string(), result.size()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2261 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 2262 | mAvailableOutputDevices.dump(fd, String8("Available output")); |
| 2263 | mAvailableInputDevices.dump(fd, String8("Available input")); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2264 | mHwModules.dump(fd); |
| 2265 | mOutputs.dump(fd); |
| 2266 | mInputs.dump(fd); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2267 | mVolumeCurves->dump(fd); |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2268 | mEffects.dump(fd); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2269 | mAudioPatches.dump(fd); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2270 | |
| 2271 | return NO_ERROR; |
| 2272 | } |
| 2273 | |
| 2274 | // This function checks for the parameters which can be offloaded. |
| 2275 | // This can be enhanced depending on the capability of the DSP and policy |
| 2276 | // of the system. |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2277 | bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2278 | { |
| 2279 | ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2280 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2281 | offloadInfo.sample_rate, offloadInfo.channel_mask, |
| 2282 | offloadInfo.format, |
| 2283 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, |
| 2284 | offloadInfo.has_video); |
| 2285 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 2286 | if (mMasterMono) { |
| 2287 | return false; // no offloading if mono is set. |
| 2288 | } |
| 2289 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2290 | // Check if offload has been disabled |
| 2291 | char propValue[PROPERTY_VALUE_MAX]; |
| 2292 | if (property_get("audio.offload.disable", propValue, "0")) { |
| 2293 | if (atoi(propValue) != 0) { |
| 2294 | ALOGV("offload disabled by audio.offload.disable=%s", propValue ); |
| 2295 | return false; |
| 2296 | } |
| 2297 | } |
| 2298 | |
| 2299 | // Check if stream type is music, then only allow offload as of now. |
| 2300 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) |
| 2301 | { |
| 2302 | ALOGV("isOffloadSupported: stream_type != MUSIC, returning false"); |
| 2303 | return false; |
| 2304 | } |
| 2305 | |
| 2306 | //TODO: enable audio offloading with video when ready |
Andy Hung | 08945c4 | 2015-05-31 21:36:46 -0700 | [diff] [blame] | 2307 | const bool allowOffloadWithVideo = |
| 2308 | property_get_bool("audio.offload.video", false /* default_value */); |
| 2309 | if (offloadInfo.has_video && !allowOffloadWithVideo) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2310 | ALOGV("isOffloadSupported: has_video == true, returning false"); |
| 2311 | return false; |
| 2312 | } |
| 2313 | |
| 2314 | //If duration is less than minimum value defined in property, return false |
| 2315 | if (property_get("audio.offload.min.duration.secs", propValue, NULL)) { |
| 2316 | if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) { |
| 2317 | ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue); |
| 2318 | return false; |
| 2319 | } |
| 2320 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { |
| 2321 | ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS); |
| 2322 | return false; |
| 2323 | } |
| 2324 | |
| 2325 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 2326 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 2327 | // detects there is an active non offloadable effect. |
| 2328 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 2329 | // This may prevent offloading in rare situations where effects are left active by apps |
| 2330 | // in the background. |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2331 | if (mEffects.isNonOffloadableEffectEnabled()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2332 | return false; |
| 2333 | } |
| 2334 | |
| 2335 | // See if there is a profile to support this. |
| 2336 | // AUDIO_DEVICE_NONE |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2337 | sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2338 | offloadInfo.sample_rate, |
| 2339 | offloadInfo.format, |
| 2340 | offloadInfo.channel_mask, |
| 2341 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2342 | ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT "); |
| 2343 | return (profile != 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2344 | } |
| 2345 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2346 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, |
| 2347 | audio_port_type_t type, |
| 2348 | unsigned int *num_ports, |
| 2349 | struct audio_port *ports, |
| 2350 | unsigned int *generation) |
| 2351 | { |
| 2352 | if (num_ports == NULL || (*num_ports != 0 && ports == NULL) || |
| 2353 | generation == NULL) { |
| 2354 | return BAD_VALUE; |
| 2355 | } |
| 2356 | ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports); |
| 2357 | if (ports == NULL) { |
| 2358 | *num_ports = 0; |
| 2359 | } |
| 2360 | |
| 2361 | size_t portsWritten = 0; |
| 2362 | size_t portsMax = *num_ports; |
| 2363 | *num_ports = 0; |
| 2364 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { |
| 2365 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 2366 | for (size_t i = 0; |
| 2367 | i < mAvailableOutputDevices.size() && portsWritten < portsMax; i++) { |
| 2368 | mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]); |
| 2369 | } |
| 2370 | *num_ports += mAvailableOutputDevices.size(); |
| 2371 | } |
| 2372 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
| 2373 | for (size_t i = 0; |
| 2374 | i < mAvailableInputDevices.size() && portsWritten < portsMax; i++) { |
| 2375 | mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]); |
| 2376 | } |
| 2377 | *num_ports += mAvailableInputDevices.size(); |
| 2378 | } |
| 2379 | } |
| 2380 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { |
| 2381 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 2382 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { |
| 2383 | mInputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2384 | } |
| 2385 | *num_ports += mInputs.size(); |
| 2386 | } |
| 2387 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2388 | size_t numOutputs = 0; |
| 2389 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2390 | if (!mOutputs[i]->isDuplicated()) { |
| 2391 | numOutputs++; |
| 2392 | if (portsWritten < portsMax) { |
| 2393 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2394 | } |
| 2395 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2396 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2397 | *num_ports += numOutputs; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2398 | } |
| 2399 | } |
| 2400 | *generation = curAudioPortGeneration(); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2401 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2402 | return NO_ERROR; |
| 2403 | } |
| 2404 | |
| 2405 | status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused) |
| 2406 | { |
| 2407 | return NO_ERROR; |
| 2408 | } |
| 2409 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2410 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, |
| 2411 | audio_patch_handle_t *handle, |
| 2412 | uid_t uid) |
| 2413 | { |
| 2414 | ALOGV("createAudioPatch()"); |
| 2415 | |
| 2416 | if (handle == NULL || patch == NULL) { |
| 2417 | return BAD_VALUE; |
| 2418 | } |
| 2419 | ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks); |
| 2420 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2421 | if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX || |
| 2422 | patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) { |
| 2423 | return BAD_VALUE; |
| 2424 | } |
| 2425 | // only one source per audio patch supported for now |
| 2426 | if (patch->num_sources > 1) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2427 | return INVALID_OPERATION; |
| 2428 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2429 | |
| 2430 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2431 | return INVALID_OPERATION; |
| 2432 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2433 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2434 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { |
| 2435 | return INVALID_OPERATION; |
| 2436 | } |
| 2437 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2438 | |
| 2439 | sp<AudioPatch> patchDesc; |
| 2440 | ssize_t index = mAudioPatches.indexOfKey(*handle); |
| 2441 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2442 | ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id, |
| 2443 | patch->sources[0].role, |
| 2444 | patch->sources[0].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2445 | #if LOG_NDEBUG == 0 |
| 2446 | for (size_t i = 0; i < patch->num_sinks; i++) { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2447 | ALOGV("createAudioPatch sink %zu: id %d role %d type %d", i, patch->sinks[i].id, |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2448 | patch->sinks[i].role, |
| 2449 | patch->sinks[i].type); |
| 2450 | } |
| 2451 | #endif |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2452 | |
| 2453 | if (index >= 0) { |
| 2454 | patchDesc = mAudioPatches.valueAt(index); |
| 2455 | ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 2456 | mUidCached, patchDesc->mUid, uid); |
| 2457 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 2458 | return INVALID_OPERATION; |
| 2459 | } |
| 2460 | } else { |
| 2461 | *handle = 0; |
| 2462 | } |
| 2463 | |
| 2464 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2465 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2466 | if (outputDesc == NULL) { |
| 2467 | ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id); |
| 2468 | return BAD_VALUE; |
| 2469 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2470 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", |
| 2471 | outputDesc->mIoHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2472 | if (patchDesc != 0) { |
| 2473 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
| 2474 | ALOGV("createAudioPatch() source id differs for patch current id %d new id %d", |
| 2475 | patchDesc->mPatch.sources[0].id, patch->sources[0].id); |
| 2476 | return BAD_VALUE; |
| 2477 | } |
| 2478 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2479 | DeviceVector devices; |
| 2480 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2481 | // Only support mix to devices connection |
| 2482 | // TODO add support for mix to mix connection |
| 2483 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 2484 | ALOGV("createAudioPatch() source mix but sink is not a device"); |
| 2485 | return INVALID_OPERATION; |
| 2486 | } |
| 2487 | sp<DeviceDescriptor> devDesc = |
| 2488 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 2489 | if (devDesc == 0) { |
| 2490 | ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id); |
| 2491 | return BAD_VALUE; |
| 2492 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2493 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2494 | if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2495 | devDesc->mAddress, |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2496 | patch->sources[0].sample_rate, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2497 | NULL, // updatedSamplingRate |
| 2498 | patch->sources[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2499 | NULL, // updatedFormat |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2500 | patch->sources[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2501 | NULL, // updatedChannelMask |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2502 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2503 | ALOGV("createAudioPatch() profile not supported for device %08x", |
| 2504 | devDesc->type()); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2505 | return INVALID_OPERATION; |
| 2506 | } |
| 2507 | devices.add(devDesc); |
| 2508 | } |
| 2509 | if (devices.size() == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2510 | return INVALID_OPERATION; |
| 2511 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2512 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2513 | // TODO: reconfigure output format and channels here |
| 2514 | ALOGV("createAudioPatch() setting device %08x on output %d", |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2515 | devices.types(), outputDesc->mIoHandle); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2516 | setOutputDevice(outputDesc, devices.types(), true, 0, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2517 | index = mAudioPatches.indexOfKey(*handle); |
| 2518 | if (index >= 0) { |
| 2519 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 2520 | ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided"); |
| 2521 | } |
| 2522 | patchDesc = mAudioPatches.valueAt(index); |
| 2523 | patchDesc->mUid = uid; |
| 2524 | ALOGV("createAudioPatch() success"); |
| 2525 | } else { |
| 2526 | ALOGW("createAudioPatch() setOutputDevice() failed to create a patch"); |
| 2527 | return INVALID_OPERATION; |
| 2528 | } |
| 2529 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2530 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 2531 | // input device to input mix connection |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2532 | // only one sink supported when connecting an input device to a mix |
| 2533 | if (patch->num_sinks > 1) { |
| 2534 | return INVALID_OPERATION; |
| 2535 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2536 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2537 | if (inputDesc == NULL) { |
| 2538 | return BAD_VALUE; |
| 2539 | } |
| 2540 | if (patchDesc != 0) { |
| 2541 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { |
| 2542 | return BAD_VALUE; |
| 2543 | } |
| 2544 | } |
| 2545 | sp<DeviceDescriptor> devDesc = |
| 2546 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
| 2547 | if (devDesc == 0) { |
| 2548 | return BAD_VALUE; |
| 2549 | } |
| 2550 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2551 | if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2552 | devDesc->mAddress, |
| 2553 | patch->sinks[0].sample_rate, |
| 2554 | NULL, /*updatedSampleRate*/ |
| 2555 | patch->sinks[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2556 | NULL, /*updatedFormat*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2557 | patch->sinks[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2558 | NULL, /*updatedChannelMask*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2559 | // FIXME for the parameter type, |
| 2560 | // and the NONE |
| 2561 | (audio_output_flags_t) |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2562 | AUDIO_INPUT_FLAG_NONE)) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2563 | return INVALID_OPERATION; |
| 2564 | } |
| 2565 | // TODO: reconfigure output format and channels here |
| 2566 | ALOGV("createAudioPatch() setting device %08x on output %d", |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2567 | devDesc->type(), inputDesc->mIoHandle); |
| 2568 | setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2569 | index = mAudioPatches.indexOfKey(*handle); |
| 2570 | if (index >= 0) { |
| 2571 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 2572 | ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided"); |
| 2573 | } |
| 2574 | patchDesc = mAudioPatches.valueAt(index); |
| 2575 | patchDesc->mUid = uid; |
| 2576 | ALOGV("createAudioPatch() success"); |
| 2577 | } else { |
| 2578 | ALOGW("createAudioPatch() setInputDevice() failed to create a patch"); |
| 2579 | return INVALID_OPERATION; |
| 2580 | } |
| 2581 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2582 | // device to device connection |
| 2583 | if (patchDesc != 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2584 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2585 | return BAD_VALUE; |
| 2586 | } |
| 2587 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2588 | sp<DeviceDescriptor> srcDeviceDesc = |
| 2589 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 2590 | if (srcDeviceDesc == 0) { |
| 2591 | return BAD_VALUE; |
| 2592 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2593 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2594 | //update source and sink with our own data as the data passed in the patch may |
| 2595 | // be incomplete. |
| 2596 | struct audio_patch newPatch = *patch; |
| 2597 | srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2598 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2599 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2600 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 2601 | ALOGV("createAudioPatch() source device but one sink is not a device"); |
| 2602 | return INVALID_OPERATION; |
| 2603 | } |
| 2604 | |
| 2605 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 2606 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 2607 | if (sinkDeviceDesc == 0) { |
| 2608 | return BAD_VALUE; |
| 2609 | } |
| 2610 | sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]); |
| 2611 | |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2612 | // create a software bridge in PatchPanel if: |
| 2613 | // - source and sink devices are on differnt HW modules OR |
| 2614 | // - audio HAL version is < 3.0 |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 2615 | if (!srcDeviceDesc->hasSameHwModuleAs(sinkDeviceDesc) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 2616 | (srcDeviceDesc->mModule->getHalVersion() < AUDIO_DEVICE_API_VERSION_3_0)) { |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2617 | // support only one sink device for now to simplify output selection logic |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2618 | if (patch->num_sinks > 1) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2619 | return INVALID_OPERATION; |
| 2620 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2621 | SortedVector<audio_io_handle_t> outputs = |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2622 | getOutputsForDevice(sinkDeviceDesc->type(), mOutputs); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2623 | // if the sink device is reachable via an opened output stream, request to go via |
| 2624 | // this output stream by adding a second source to the patch description |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 2625 | audio_io_handle_t output = selectOutput(outputs, |
| 2626 | AUDIO_OUTPUT_FLAG_NONE, |
| 2627 | AUDIO_FORMAT_INVALID); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2628 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 2629 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 2630 | if (outputDesc->isDuplicated()) { |
| 2631 | return INVALID_OPERATION; |
| 2632 | } |
| 2633 | outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2634 | newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2635 | newPatch.num_sources = 2; |
| 2636 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2637 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2638 | } |
| 2639 | // TODO: check from routing capabilities in config file and other conflicting patches |
| 2640 | |
| 2641 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 2642 | if (index >= 0) { |
| 2643 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 2644 | } |
| 2645 | |
| 2646 | status_t status = mpClientInterface->createAudioPatch(&newPatch, |
| 2647 | &afPatchHandle, |
| 2648 | 0); |
| 2649 | ALOGV("createAudioPatch() patch panel returned %d patchHandle %d", |
| 2650 | status, afPatchHandle); |
| 2651 | if (status == NO_ERROR) { |
| 2652 | if (index < 0) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 2653 | patchDesc = new AudioPatch(&newPatch, uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2654 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 2655 | } else { |
| 2656 | patchDesc->mPatch = newPatch; |
| 2657 | } |
| 2658 | patchDesc->mAfPatchHandle = afPatchHandle; |
| 2659 | *handle = patchDesc->mHandle; |
| 2660 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2661 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2662 | } else { |
| 2663 | ALOGW("createAudioPatch() patch panel could not connect device patch, error %d", |
| 2664 | status); |
| 2665 | return INVALID_OPERATION; |
| 2666 | } |
| 2667 | } else { |
| 2668 | return BAD_VALUE; |
| 2669 | } |
| 2670 | } else { |
| 2671 | return BAD_VALUE; |
| 2672 | } |
| 2673 | return NO_ERROR; |
| 2674 | } |
| 2675 | |
| 2676 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, |
| 2677 | uid_t uid) |
| 2678 | { |
| 2679 | ALOGV("releaseAudioPatch() patch %d", handle); |
| 2680 | |
| 2681 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 2682 | |
| 2683 | if (index < 0) { |
| 2684 | return BAD_VALUE; |
| 2685 | } |
| 2686 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 2687 | ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 2688 | mUidCached, patchDesc->mUid, uid); |
| 2689 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 2690 | return INVALID_OPERATION; |
| 2691 | } |
| 2692 | |
| 2693 | struct audio_patch *patch = &patchDesc->mPatch; |
| 2694 | patchDesc->mUid = mUidCached; |
| 2695 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2696 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2697 | if (outputDesc == NULL) { |
| 2698 | ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id); |
| 2699 | return BAD_VALUE; |
| 2700 | } |
| 2701 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2702 | setOutputDevice(outputDesc, |
| 2703 | getNewOutputDevice(outputDesc, true /*fromCache*/), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2704 | true, |
| 2705 | 0, |
| 2706 | NULL); |
| 2707 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2708 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2709 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2710 | if (inputDesc == NULL) { |
| 2711 | ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id); |
| 2712 | return BAD_VALUE; |
| 2713 | } |
| 2714 | setInputDevice(inputDesc->mIoHandle, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 2715 | getNewInputDevice(inputDesc), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2716 | true, |
| 2717 | NULL); |
| 2718 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2719 | audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle; |
| 2720 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 2721 | ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d", |
| 2722 | status, patchDesc->mAfPatchHandle); |
| 2723 | removeAudioPatch(patchDesc->mHandle); |
| 2724 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2725 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2726 | } else { |
| 2727 | return BAD_VALUE; |
| 2728 | } |
| 2729 | } else { |
| 2730 | return BAD_VALUE; |
| 2731 | } |
| 2732 | return NO_ERROR; |
| 2733 | } |
| 2734 | |
| 2735 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, |
| 2736 | struct audio_patch *patches, |
| 2737 | unsigned int *generation) |
| 2738 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2739 | if (generation == NULL) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2740 | return BAD_VALUE; |
| 2741 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2742 | *generation = curAudioPortGeneration(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2743 | return mAudioPatches.listAudioPatches(num_patches, patches); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2744 | } |
| 2745 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2746 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2747 | { |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2748 | ALOGV("setAudioPortConfig()"); |
| 2749 | |
| 2750 | if (config == NULL) { |
| 2751 | return BAD_VALUE; |
| 2752 | } |
| 2753 | ALOGV("setAudioPortConfig() on port handle %d", config->id); |
| 2754 | // Only support gain configuration for now |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2755 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { |
| 2756 | return INVALID_OPERATION; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2757 | } |
| 2758 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2759 | sp<AudioPortConfig> audioPortConfig; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2760 | if (config->type == AUDIO_PORT_TYPE_MIX) { |
| 2761 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2762 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2763 | if (outputDesc == NULL) { |
| 2764 | return BAD_VALUE; |
| 2765 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2766 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 2767 | "setAudioPortConfig() called on duplicated output %d", |
| 2768 | outputDesc->mIoHandle); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2769 | audioPortConfig = outputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2770 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2771 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2772 | if (inputDesc == NULL) { |
| 2773 | return BAD_VALUE; |
| 2774 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2775 | audioPortConfig = inputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2776 | } else { |
| 2777 | return BAD_VALUE; |
| 2778 | } |
| 2779 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { |
| 2780 | sp<DeviceDescriptor> deviceDesc; |
| 2781 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
| 2782 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); |
| 2783 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
| 2784 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); |
| 2785 | } else { |
| 2786 | return BAD_VALUE; |
| 2787 | } |
| 2788 | if (deviceDesc == NULL) { |
| 2789 | return BAD_VALUE; |
| 2790 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2791 | audioPortConfig = deviceDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2792 | } else { |
| 2793 | return BAD_VALUE; |
| 2794 | } |
| 2795 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2796 | struct audio_port_config backupConfig; |
| 2797 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); |
| 2798 | if (status == NO_ERROR) { |
| 2799 | struct audio_port_config newConfig; |
| 2800 | audioPortConfig->toAudioPortConfig(&newConfig, config); |
| 2801 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2802 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2803 | if (status != NO_ERROR) { |
| 2804 | audioPortConfig->applyAudioPortConfig(&backupConfig); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2805 | } |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2806 | |
| 2807 | return status; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2808 | } |
| 2809 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2810 | void AudioPolicyManager::releaseResourcesForUid(uid_t uid) |
| 2811 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2812 | clearAudioSources(uid); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2813 | clearAudioPatches(uid); |
| 2814 | clearSessionRoutes(uid); |
| 2815 | } |
| 2816 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2817 | void AudioPolicyManager::clearAudioPatches(uid_t uid) |
| 2818 | { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 2819 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2820 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 2821 | if (patchDesc->mUid == uid) { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 2822 | releaseAudioPatch(mAudioPatches.keyAt(i), uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2823 | } |
| 2824 | } |
| 2825 | } |
| 2826 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2827 | void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy, |
| 2828 | audio_io_handle_t ouptutToSkip) |
| 2829 | { |
| 2830 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 2831 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 2832 | for (size_t j = 0; j < mOutputs.size(); j++) { |
| 2833 | if (mOutputs.keyAt(j) == ouptutToSkip) { |
| 2834 | continue; |
| 2835 | } |
| 2836 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j); |
| 2837 | if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) { |
| 2838 | continue; |
| 2839 | } |
| 2840 | // If the default device for this strategy is on another output mix, |
| 2841 | // invalidate all tracks in this strategy to force re connection. |
| 2842 | // Otherwise select new device on the output mix. |
| 2843 | if (outputs.indexOf(mOutputs.keyAt(j)) < 0) { |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2844 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2845 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
| 2846 | mpClientInterface->invalidateStream((audio_stream_type_t)stream); |
| 2847 | } |
| 2848 | } |
| 2849 | } else { |
| 2850 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
| 2851 | setOutputDevice(outputDesc, newDevice, false); |
| 2852 | } |
| 2853 | } |
| 2854 | } |
| 2855 | |
| 2856 | void AudioPolicyManager::clearSessionRoutes(uid_t uid) |
| 2857 | { |
| 2858 | // remove output routes associated with this uid |
| 2859 | SortedVector<routing_strategy> affectedStrategies; |
| 2860 | for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) { |
| 2861 | sp<SessionRoute> route = mOutputRoutes.valueAt(i); |
| 2862 | if (route->mUid == uid) { |
| 2863 | mOutputRoutes.removeItemsAt(i); |
| 2864 | if (route->mDeviceDescriptor != 0) { |
| 2865 | affectedStrategies.add(getStrategy(route->mStreamType)); |
| 2866 | } |
| 2867 | } |
| 2868 | } |
| 2869 | // reroute outputs if necessary |
| 2870 | for (size_t i = 0; i < affectedStrategies.size(); i++) { |
| 2871 | checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE); |
| 2872 | } |
| 2873 | |
| 2874 | // remove input routes associated with this uid |
| 2875 | SortedVector<audio_source_t> affectedSources; |
| 2876 | for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) { |
| 2877 | sp<SessionRoute> route = mInputRoutes.valueAt(i); |
| 2878 | if (route->mUid == uid) { |
| 2879 | mInputRoutes.removeItemsAt(i); |
| 2880 | if (route->mDeviceDescriptor != 0) { |
| 2881 | affectedSources.add(route->mSource); |
| 2882 | } |
| 2883 | } |
| 2884 | } |
| 2885 | // reroute inputs if necessary |
| 2886 | SortedVector<audio_io_handle_t> inputsToClose; |
| 2887 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 2888 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2889 | if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2890 | inputsToClose.add(inputDesc->mIoHandle); |
| 2891 | } |
| 2892 | } |
| 2893 | for (size_t i = 0; i < inputsToClose.size(); i++) { |
| 2894 | closeInput(inputsToClose[i]); |
| 2895 | } |
| 2896 | } |
| 2897 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2898 | void AudioPolicyManager::clearAudioSources(uid_t uid) |
| 2899 | { |
| 2900 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
| 2901 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 2902 | if (sourceDesc->mUid == uid) { |
| 2903 | stopAudioSource(mAudioSources.keyAt(i)); |
| 2904 | } |
| 2905 | } |
| 2906 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2907 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2908 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, |
| 2909 | audio_io_handle_t *ioHandle, |
| 2910 | audio_devices_t *device) |
| 2911 | { |
Glenn Kasten | f0c6d7d | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 2912 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); |
| 2913 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT); |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 2914 | *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2915 | |
François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 2916 | return mSoundTriggerSessions.acquireSession(*session, *ioHandle); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2917 | } |
| 2918 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2919 | status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source, |
| 2920 | const audio_attributes_t *attributes, |
| 2921 | audio_io_handle_t *handle, |
| 2922 | uid_t uid) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 2923 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2924 | ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle); |
| 2925 | if (source == NULL || attributes == NULL || handle == NULL) { |
| 2926 | return BAD_VALUE; |
| 2927 | } |
| 2928 | |
| 2929 | *handle = AUDIO_IO_HANDLE_NONE; |
| 2930 | |
| 2931 | if (source->role != AUDIO_PORT_ROLE_SOURCE || |
| 2932 | source->type != AUDIO_PORT_TYPE_DEVICE) { |
| 2933 | ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type); |
| 2934 | return INVALID_OPERATION; |
| 2935 | } |
| 2936 | |
| 2937 | sp<DeviceDescriptor> srcDeviceDesc = |
| 2938 | mAvailableInputDevices.getDevice(source->ext.device.type, |
| 2939 | String8(source->ext.device.address)); |
| 2940 | if (srcDeviceDesc == 0) { |
| 2941 | ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type); |
| 2942 | return BAD_VALUE; |
| 2943 | } |
| 2944 | sp<AudioSourceDescriptor> sourceDesc = |
| 2945 | new AudioSourceDescriptor(srcDeviceDesc, attributes, uid); |
| 2946 | |
| 2947 | struct audio_patch dummyPatch; |
| 2948 | sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid); |
| 2949 | sourceDesc->mPatchDesc = patchDesc; |
| 2950 | |
| 2951 | status_t status = connectAudioSource(sourceDesc); |
| 2952 | if (status == NO_ERROR) { |
| 2953 | mAudioSources.add(sourceDesc->getHandle(), sourceDesc); |
| 2954 | *handle = sourceDesc->getHandle(); |
| 2955 | } |
| 2956 | return status; |
| 2957 | } |
| 2958 | |
| 2959 | status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc) |
| 2960 | { |
| 2961 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle()); |
| 2962 | |
| 2963 | // make sure we only have one patch per source. |
| 2964 | disconnectAudioSource(sourceDesc); |
| 2965 | |
| 2966 | routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2967 | audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2968 | sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice; |
| 2969 | |
| 2970 | audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true); |
| 2971 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 2972 | mAvailableOutputDevices.getDevice(sinkDevice, String8("")); |
| 2973 | |
| 2974 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 2975 | struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch; |
| 2976 | |
| 2977 | if (srcDeviceDesc->getAudioPort()->mModule->getHandle() == |
| 2978 | sinkDeviceDesc->getAudioPort()->mModule->getHandle() && |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 2979 | srcDeviceDesc->getAudioPort()->mModule->getHalVersion() >= AUDIO_DEVICE_API_VERSION_3_0 && |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2980 | srcDeviceDesc->getAudioPort()->mGains.size() > 0) { |
| 2981 | ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__); |
| 2982 | // create patch between src device and output device |
| 2983 | // create Hwoutput and add to mHwOutputs |
| 2984 | } else { |
| 2985 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs); |
| 2986 | audio_io_handle_t output = |
| 2987 | selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID); |
| 2988 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 2989 | ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice); |
| 2990 | return INVALID_OPERATION; |
| 2991 | } |
| 2992 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 2993 | if (outputDesc->isDuplicated()) { |
| 2994 | ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice); |
| 2995 | return INVALID_OPERATION; |
| 2996 | } |
| 2997 | // create a special patch with no sink and two sources: |
| 2998 | // - the second source indicates to PatchPanel through which output mix this patch should |
| 2999 | // be connected as well as the stream type for volume control |
| 3000 | // - the sink is defined by whatever output device is currently selected for the output |
| 3001 | // though which this patch is routed. |
| 3002 | patch->num_sinks = 0; |
| 3003 | patch->num_sources = 2; |
| 3004 | srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL); |
| 3005 | outputDesc->toAudioPortConfig(&patch->sources[1], NULL); |
| 3006 | patch->sources[1].ext.mix.usecase.stream = stream; |
| 3007 | status_t status = mpClientInterface->createAudioPatch(patch, |
| 3008 | &afPatchHandle, |
| 3009 | 0); |
| 3010 | ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__, |
| 3011 | status, afPatchHandle); |
| 3012 | if (status != NO_ERROR) { |
| 3013 | ALOGW("%s patch panel could not connect device patch, error %d", |
| 3014 | __FUNCTION__, status); |
| 3015 | return INVALID_OPERATION; |
| 3016 | } |
| 3017 | uint32_t delayMs = 0; |
| 3018 | status = startSource(outputDesc, stream, sinkDevice, &delayMs); |
| 3019 | |
| 3020 | if (status != NO_ERROR) { |
| 3021 | mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0); |
| 3022 | return status; |
| 3023 | } |
| 3024 | sourceDesc->mSwOutput = outputDesc; |
| 3025 | if (delayMs != 0) { |
| 3026 | usleep(delayMs * 1000); |
| 3027 | } |
| 3028 | } |
| 3029 | |
| 3030 | sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle; |
| 3031 | addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc); |
| 3032 | |
| 3033 | return NO_ERROR; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3034 | } |
| 3035 | |
Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 3036 | status_t AudioPolicyManager::stopAudioSource(audio_io_handle_t handle __unused) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3037 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3038 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle); |
| 3039 | ALOGV("%s handle %d", __FUNCTION__, handle); |
| 3040 | if (sourceDesc == 0) { |
| 3041 | ALOGW("%s unknown source for handle %d", __FUNCTION__, handle); |
| 3042 | return BAD_VALUE; |
| 3043 | } |
| 3044 | status_t status = disconnectAudioSource(sourceDesc); |
| 3045 | |
| 3046 | mAudioSources.removeItem(handle); |
| 3047 | return status; |
| 3048 | } |
| 3049 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3050 | status_t AudioPolicyManager::setMasterMono(bool mono) |
| 3051 | { |
| 3052 | if (mMasterMono == mono) { |
| 3053 | return NO_ERROR; |
| 3054 | } |
| 3055 | mMasterMono = mono; |
| 3056 | // if enabling mono we close all offloaded devices, which will invalidate the |
| 3057 | // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible |
| 3058 | // for recreating the new AudioTrack as non-offloaded PCM. |
| 3059 | // |
| 3060 | // If disabling mono, we leave all tracks as is: we don't know which clients |
| 3061 | // and tracks are able to be recreated as offloaded. The next "song" should |
| 3062 | // play back offloaded. |
| 3063 | if (mMasterMono) { |
| 3064 | Vector<audio_io_handle_t> offloaded; |
| 3065 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3066 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 3067 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
| 3068 | offloaded.push(desc->mIoHandle); |
| 3069 | } |
| 3070 | } |
| 3071 | for (size_t i = 0; i < offloaded.size(); ++i) { |
| 3072 | closeOutput(offloaded[i]); |
| 3073 | } |
| 3074 | } |
| 3075 | // update master mono for all remaining outputs |
| 3076 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3077 | updateMono(mOutputs.keyAt(i)); |
| 3078 | } |
| 3079 | return NO_ERROR; |
| 3080 | } |
| 3081 | |
| 3082 | status_t AudioPolicyManager::getMasterMono(bool *mono) |
| 3083 | { |
| 3084 | *mono = mMasterMono; |
| 3085 | return NO_ERROR; |
| 3086 | } |
| 3087 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3088 | status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc) |
| 3089 | { |
| 3090 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle()); |
| 3091 | |
| 3092 | sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle); |
| 3093 | if (patchDesc == 0) { |
| 3094 | ALOGW("%s source has no patch with handle %d", __FUNCTION__, |
| 3095 | sourceDesc->mPatchDesc->mHandle); |
| 3096 | return BAD_VALUE; |
| 3097 | } |
| 3098 | removeAudioPatch(sourceDesc->mPatchDesc->mHandle); |
| 3099 | |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 3100 | audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3101 | sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote(); |
| 3102 | if (swOutputDesc != 0) { |
| 3103 | stopSource(swOutputDesc, stream, false); |
| 3104 | mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3105 | } else { |
| 3106 | sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote(); |
| 3107 | if (hwOutputDesc != 0) { |
| 3108 | // release patch between src device and output device |
| 3109 | // close Hwoutput and remove from mHwOutputs |
| 3110 | } else { |
| 3111 | ALOGW("%s source has neither SW nor HW output", __FUNCTION__); |
| 3112 | } |
| 3113 | } |
| 3114 | return NO_ERROR; |
| 3115 | } |
| 3116 | |
| 3117 | sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput( |
| 3118 | audio_io_handle_t output, routing_strategy strategy) |
| 3119 | { |
| 3120 | sp<AudioSourceDescriptor> source; |
| 3121 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
| 3122 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 3123 | routing_strategy sourceStrategy = |
| 3124 | (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes); |
| 3125 | sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote(); |
| 3126 | if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) { |
| 3127 | source = sourceDesc; |
| 3128 | break; |
| 3129 | } |
| 3130 | } |
| 3131 | return source; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3132 | } |
| 3133 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3134 | // ---------------------------------------------------------------------------- |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3135 | // AudioPolicyManager |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3136 | // ---------------------------------------------------------------------------- |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3137 | uint32_t AudioPolicyManager::nextAudioPortGeneration() |
| 3138 | { |
| 3139 | return android_atomic_inc(&mAudioPortGeneration); |
| 3140 | } |
| 3141 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3142 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3143 | : |
| 3144 | #ifdef AUDIO_POLICY_TEST |
| 3145 | Thread(false), |
| 3146 | #endif //AUDIO_POLICY_TEST |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3147 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3148 | mA2dpSuspended(false), |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 3149 | mAudioPortGeneration(1), |
| 3150 | mBeaconMuteRefCount(0), |
| 3151 | mBeaconPlayingRefCount(0), |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3152 | mBeaconMuted(false), |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3153 | mTtsOutputAvailable(false), |
| 3154 | mMasterMono(false) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3155 | { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3156 | mUidCached = getuid(); |
| 3157 | mpClientInterface = clientInterface; |
| 3158 | |
| 3159 | // TODO: remove when legacy conf file is removed. true on devices that use DRC on the |
| 3160 | // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly. |
| 3161 | // Note: remove also speaker_drc_enabled from global configuration of XML config file. |
| 3162 | bool speakerDrcEnabled = false; |
| 3163 | |
| 3164 | #ifdef USE_XML_AUDIO_POLICY_CONF |
| 3165 | mVolumeCurves = new VolumeCurvesCollection(); |
| 3166 | AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices, |
| 3167 | mDefaultOutputDevice, speakerDrcEnabled, |
| 3168 | static_cast<VolumeCurvesCollection *>(mVolumeCurves)); |
| 3169 | PolicySerializer serializer; |
| 3170 | if (serializer.deserialize(AUDIO_POLICY_XML_CONFIG_FILE, config) != NO_ERROR) { |
| 3171 | #else |
| 3172 | mVolumeCurves = new StreamDescriptorCollection(); |
| 3173 | AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices, |
| 3174 | mDefaultOutputDevice, speakerDrcEnabled); |
| 3175 | if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, config) != NO_ERROR) && |
| 3176 | (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, config) != NO_ERROR)) { |
| 3177 | #endif |
| 3178 | ALOGE("could not load audio policy configuration file, setting defaults"); |
| 3179 | config.setDefault(); |
| 3180 | } |
| 3181 | // must be done after reading the policy (since conditionned by Speaker Drc Enabling) |
| 3182 | mVolumeCurves->initializeVolumeCurves(speakerDrcEnabled); |
| 3183 | |
| 3184 | // Once policy config has been parsed, retrieve an instance of the engine and initialize it. |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3185 | audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance(); |
| 3186 | if (!engineInstance) { |
| 3187 | ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__); |
| 3188 | return; |
| 3189 | } |
| 3190 | // Retrieve the Policy Manager Interface |
| 3191 | mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>(); |
| 3192 | if (mEngine == NULL) { |
| 3193 | ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__); |
| 3194 | return; |
| 3195 | } |
| 3196 | mEngine->setObserver(this); |
| 3197 | status_t status = mEngine->initCheck(); |
| 3198 | ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status); |
| 3199 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3200 | // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3201 | // open all output streams needed to access attached devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3202 | audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types(); |
| 3203 | audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3204 | for (size_t i = 0; i < mHwModules.size(); i++) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3205 | mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3206 | if (mHwModules[i]->mHandle == 0) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3207 | ALOGW("could not open HW module %s", mHwModules[i]->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3208 | continue; |
| 3209 | } |
| 3210 | // open all output streams needed to access attached devices |
| 3211 | // except for direct output streams that are only opened when they are actually |
| 3212 | // required by an app. |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3213 | // This also validates mAvailableOutputDevices list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3214 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 3215 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3216 | const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3217 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3218 | if (!outProfile->hasSupportedDevices()) { |
| 3219 | ALOGW("Output profile contains no device on module %s", mHwModules[i]->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3220 | continue; |
| 3221 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3222 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3223 | mTtsOutputAvailable = true; |
| 3224 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3225 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3226 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3227 | continue; |
| 3228 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3229 | audio_devices_t profileType = outProfile->getSupportedDevicesType(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3230 | if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) { |
| 3231 | profileType = mDefaultOutputDevice->type(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3232 | } else { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3233 | // chose first device present in profile's SupportedDevices also part of |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3234 | // outputDeviceTypes |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3235 | profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3236 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3237 | if ((profileType & outputDeviceTypes) == 0) { |
| 3238 | continue; |
| 3239 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3240 | sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile, |
| 3241 | mpClientInterface); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3242 | |
| 3243 | outputDesc->mDevice = profileType; |
| 3244 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3245 | config.sample_rate = outputDesc->mSamplingRate; |
| 3246 | config.channel_mask = outputDesc->mChannelMask; |
| 3247 | config.format = outputDesc->mFormat; |
| 3248 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3249 | status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(), |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3250 | &output, |
| 3251 | &config, |
| 3252 | &outputDesc->mDevice, |
| 3253 | String8(""), |
| 3254 | &outputDesc->mLatency, |
| 3255 | outputDesc->mFlags); |
| 3256 | |
| 3257 | if (status != NO_ERROR) { |
| 3258 | ALOGW("Cannot open output stream for device %08x on hw module %s", |
| 3259 | outputDesc->mDevice, |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3260 | mHwModules[i]->getName()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3261 | } else { |
| 3262 | outputDesc->mSamplingRate = config.sample_rate; |
| 3263 | outputDesc->mChannelMask = config.channel_mask; |
| 3264 | outputDesc->mFormat = config.format; |
| 3265 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3266 | const DeviceVector &supportedDevices = outProfile->getSupportedDevices(); |
| 3267 | for (size_t k = 0; k < supportedDevices.size(); k++) { |
| 3268 | ssize_t index = mAvailableOutputDevices.indexOf(supportedDevices[k]); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3269 | // 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] | 3270 | if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) { |
| 3271 | mAvailableOutputDevices[index]->attach(mHwModules[i]); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3272 | } |
| 3273 | } |
| 3274 | if (mPrimaryOutput == 0 && |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3275 | outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3276 | mPrimaryOutput = outputDesc; |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3277 | } |
| 3278 | addOutput(output, outputDesc); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3279 | setOutputDevice(outputDesc, |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3280 | outputDesc->mDevice, |
| 3281 | true); |
| 3282 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3283 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3284 | // open input streams needed to access attached devices to validate |
| 3285 | // mAvailableInputDevices list |
| 3286 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) |
| 3287 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3288 | const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3289 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3290 | if (!inProfile->hasSupportedDevices()) { |
| 3291 | ALOGW("Input profile contains no device on module %s", mHwModules[i]->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3292 | continue; |
| 3293 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3294 | // chose first device present in profile's SupportedDevices also part of |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3295 | // inputDeviceTypes |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3296 | audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes); |
| 3297 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3298 | if ((profileType & inputDeviceTypes) == 0) { |
| 3299 | continue; |
| 3300 | } |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 3301 | sp<AudioInputDescriptor> inputDesc = |
| 3302 | new AudioInputDescriptor(inProfile); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3303 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3304 | inputDesc->mDevice = profileType; |
| 3305 | |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 3306 | // find the address |
| 3307 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType); |
| 3308 | // the inputs vector must be of size 1, but we don't want to crash here |
| 3309 | String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress |
| 3310 | : String8(""); |
| 3311 | ALOGV(" for input device 0x%x using address %s", profileType, address.string()); |
| 3312 | ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!"); |
| 3313 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3314 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3315 | config.sample_rate = inputDesc->mSamplingRate; |
| 3316 | config.channel_mask = inputDesc->mChannelMask; |
| 3317 | config.format = inputDesc->mFormat; |
| 3318 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3319 | status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(), |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3320 | &input, |
| 3321 | &config, |
| 3322 | &inputDesc->mDevice, |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 3323 | address, |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3324 | AUDIO_SOURCE_MIC, |
| 3325 | AUDIO_INPUT_FLAG_NONE); |
| 3326 | |
| 3327 | if (status == NO_ERROR) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3328 | const DeviceVector &supportedDevices = inProfile->getSupportedDevices(); |
| 3329 | for (size_t k = 0; k < supportedDevices.size(); k++) { |
| 3330 | ssize_t index = mAvailableInputDevices.indexOf(supportedDevices[k]); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3331 | // give a valid ID to an attached device once confirmed it is reachable |
Eric Laurent | 45aabc3 | 2015-08-06 09:11:13 -0700 | [diff] [blame] | 3332 | if (index >= 0) { |
| 3333 | sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index]; |
| 3334 | if (!devDesc->isAttached()) { |
| 3335 | devDesc->attach(mHwModules[i]); |
| 3336 | devDesc->importAudioPort(inProfile); |
| 3337 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3338 | } |
| 3339 | } |
| 3340 | mpClientInterface->closeInput(input); |
| 3341 | } else { |
| 3342 | ALOGW("Cannot open input stream for device %08x on hw module %s", |
| 3343 | inputDesc->mDevice, |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3344 | mHwModules[i]->getName()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3345 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3346 | } |
| 3347 | } |
| 3348 | // make sure all attached devices have been allocated a unique ID |
| 3349 | for (size_t i = 0; i < mAvailableOutputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 3350 | if (!mAvailableOutputDevices[i]->isAttached()) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3351 | ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3352 | mAvailableOutputDevices.remove(mAvailableOutputDevices[i]); |
| 3353 | continue; |
| 3354 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3355 | // The device is now validated and can be appended to the available devices of the engine |
| 3356 | mEngine->setDeviceConnectionState(mAvailableOutputDevices[i], |
| 3357 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3358 | i++; |
| 3359 | } |
| 3360 | for (size_t i = 0; i < mAvailableInputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 3361 | if (!mAvailableInputDevices[i]->isAttached()) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3362 | ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3363 | mAvailableInputDevices.remove(mAvailableInputDevices[i]); |
| 3364 | continue; |
| 3365 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3366 | // The device is now validated and can be appended to the available devices of the engine |
| 3367 | mEngine->setDeviceConnectionState(mAvailableInputDevices[i], |
| 3368 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3369 | i++; |
| 3370 | } |
| 3371 | // make sure default device is reachable |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3372 | if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3373 | ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3374 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3375 | |
| 3376 | ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output"); |
| 3377 | |
| 3378 | updateDevicesAndOutputs(); |
| 3379 | |
| 3380 | #ifdef AUDIO_POLICY_TEST |
| 3381 | if (mPrimaryOutput != 0) { |
| 3382 | AudioParameter outputCmd = AudioParameter(); |
| 3383 | outputCmd.addInt(String8("set_id"), 0); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3384 | mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3385 | |
| 3386 | mTestDevice = AUDIO_DEVICE_OUT_SPEAKER; |
| 3387 | mTestSamplingRate = 44100; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3388 | mTestFormat = AUDIO_FORMAT_PCM_16_BIT; |
| 3389 | mTestChannels = AUDIO_CHANNEL_OUT_STEREO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3390 | mTestLatencyMs = 0; |
| 3391 | mCurOutput = 0; |
| 3392 | mDirectOutput = false; |
| 3393 | for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { |
| 3394 | mTestOutputs[i] = 0; |
| 3395 | } |
| 3396 | |
| 3397 | const size_t SIZE = 256; |
| 3398 | char buffer[SIZE]; |
| 3399 | snprintf(buffer, SIZE, "AudioPolicyManagerTest"); |
| 3400 | run(buffer, ANDROID_PRIORITY_AUDIO); |
| 3401 | } |
| 3402 | #endif //AUDIO_POLICY_TEST |
| 3403 | } |
| 3404 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3405 | AudioPolicyManager::~AudioPolicyManager() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3406 | { |
| 3407 | #ifdef AUDIO_POLICY_TEST |
| 3408 | exit(); |
| 3409 | #endif //AUDIO_POLICY_TEST |
| 3410 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3411 | mpClientInterface->closeOutput(mOutputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3412 | } |
| 3413 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3414 | mpClientInterface->closeInput(mInputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3415 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3416 | mAvailableOutputDevices.clear(); |
| 3417 | mAvailableInputDevices.clear(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3418 | mOutputs.clear(); |
| 3419 | mInputs.clear(); |
| 3420 | mHwModules.clear(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3421 | } |
| 3422 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3423 | status_t AudioPolicyManager::initCheck() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3424 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 3425 | return hasPrimaryOutput() ? NO_ERROR : NO_INIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3426 | } |
| 3427 | |
| 3428 | #ifdef AUDIO_POLICY_TEST |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3429 | bool AudioPolicyManager::threadLoop() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3430 | { |
| 3431 | ALOGV("entering threadLoop()"); |
| 3432 | while (!exitPending()) |
| 3433 | { |
| 3434 | String8 command; |
| 3435 | int valueInt; |
| 3436 | String8 value; |
| 3437 | |
| 3438 | Mutex::Autolock _l(mLock); |
| 3439 | mWaitWorkCV.waitRelative(mLock, milliseconds(50)); |
| 3440 | |
| 3441 | command = mpClientInterface->getParameters(0, String8("test_cmd_policy")); |
| 3442 | AudioParameter param = AudioParameter(command); |
| 3443 | |
| 3444 | if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR && |
| 3445 | valueInt != 0) { |
| 3446 | ALOGV("Test command %s received", command.string()); |
| 3447 | String8 target; |
| 3448 | if (param.get(String8("target"), target) != NO_ERROR) { |
| 3449 | target = "Manager"; |
| 3450 | } |
| 3451 | if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) { |
| 3452 | param.remove(String8("test_cmd_policy_output")); |
| 3453 | mCurOutput = valueInt; |
| 3454 | } |
| 3455 | if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) { |
| 3456 | param.remove(String8("test_cmd_policy_direct")); |
| 3457 | if (value == "false") { |
| 3458 | mDirectOutput = false; |
| 3459 | } else if (value == "true") { |
| 3460 | mDirectOutput = true; |
| 3461 | } |
| 3462 | } |
| 3463 | if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) { |
| 3464 | param.remove(String8("test_cmd_policy_input")); |
| 3465 | mTestInput = valueInt; |
| 3466 | } |
| 3467 | |
| 3468 | if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) { |
| 3469 | param.remove(String8("test_cmd_policy_format")); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3470 | int format = AUDIO_FORMAT_INVALID; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3471 | if (value == "PCM 16 bits") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3472 | format = AUDIO_FORMAT_PCM_16_BIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3473 | } else if (value == "PCM 8 bits") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3474 | format = AUDIO_FORMAT_PCM_8_BIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3475 | } else if (value == "Compressed MP3") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3476 | format = AUDIO_FORMAT_MP3; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3477 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3478 | if (format != AUDIO_FORMAT_INVALID) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3479 | if (target == "Manager") { |
| 3480 | mTestFormat = format; |
| 3481 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 3482 | AudioParameter outputParam = AudioParameter(); |
| 3483 | outputParam.addInt(String8("format"), format); |
| 3484 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 3485 | } |
| 3486 | } |
| 3487 | } |
| 3488 | if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) { |
| 3489 | param.remove(String8("test_cmd_policy_channels")); |
| 3490 | int channels = 0; |
| 3491 | |
| 3492 | if (value == "Channels Stereo") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3493 | channels = AUDIO_CHANNEL_OUT_STEREO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3494 | } else if (value == "Channels Mono") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3495 | channels = AUDIO_CHANNEL_OUT_MONO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3496 | } |
| 3497 | if (channels != 0) { |
| 3498 | if (target == "Manager") { |
| 3499 | mTestChannels = channels; |
| 3500 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 3501 | AudioParameter outputParam = AudioParameter(); |
| 3502 | outputParam.addInt(String8("channels"), channels); |
| 3503 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 3504 | } |
| 3505 | } |
| 3506 | } |
| 3507 | if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) { |
| 3508 | param.remove(String8("test_cmd_policy_sampleRate")); |
| 3509 | if (valueInt >= 0 && valueInt <= 96000) { |
| 3510 | int samplingRate = valueInt; |
| 3511 | if (target == "Manager") { |
| 3512 | mTestSamplingRate = samplingRate; |
| 3513 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 3514 | AudioParameter outputParam = AudioParameter(); |
| 3515 | outputParam.addInt(String8("sampling_rate"), samplingRate); |
| 3516 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 3517 | } |
| 3518 | } |
| 3519 | } |
| 3520 | |
| 3521 | if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) { |
| 3522 | param.remove(String8("test_cmd_policy_reopen")); |
| 3523 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3524 | mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput);); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3525 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3526 | audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3527 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3528 | removeOutput(mPrimaryOutput->mIoHandle); |
| 3529 | sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL, |
| 3530 | mpClientInterface); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3531 | outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3532 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3533 | config.sample_rate = outputDesc->mSamplingRate; |
| 3534 | config.channel_mask = outputDesc->mChannelMask; |
| 3535 | config.format = outputDesc->mFormat; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3536 | audio_io_handle_t handle; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3537 | status_t status = mpClientInterface->openOutput(moduleHandle, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3538 | &handle, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3539 | &config, |
| 3540 | &outputDesc->mDevice, |
| 3541 | String8(""), |
| 3542 | &outputDesc->mLatency, |
| 3543 | outputDesc->mFlags); |
| 3544 | if (status != NO_ERROR) { |
| 3545 | ALOGE("Failed to reopen hardware output stream, " |
| 3546 | "samplingRate: %d, format %d, channels %d", |
| 3547 | outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3548 | } else { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3549 | outputDesc->mSamplingRate = config.sample_rate; |
| 3550 | outputDesc->mChannelMask = config.channel_mask; |
| 3551 | outputDesc->mFormat = config.format; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3552 | mPrimaryOutput = outputDesc; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3553 | AudioParameter outputCmd = AudioParameter(); |
| 3554 | outputCmd.addInt(String8("set_id"), 0); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3555 | mpClientInterface->setParameters(handle, outputCmd.toString()); |
| 3556 | addOutput(handle, outputDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3557 | } |
| 3558 | } |
| 3559 | |
| 3560 | |
| 3561 | mpClientInterface->setParameters(0, String8("test_cmd_policy=")); |
| 3562 | } |
| 3563 | } |
| 3564 | return false; |
| 3565 | } |
| 3566 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3567 | void AudioPolicyManager::exit() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3568 | { |
| 3569 | { |
| 3570 | AutoMutex _l(mLock); |
| 3571 | requestExit(); |
| 3572 | mWaitWorkCV.signal(); |
| 3573 | } |
| 3574 | requestExitAndWait(); |
| 3575 | } |
| 3576 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3577 | int AudioPolicyManager::testOutputIndex(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3578 | { |
| 3579 | for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { |
| 3580 | if (output == mTestOutputs[i]) return i; |
| 3581 | } |
| 3582 | return 0; |
| 3583 | } |
| 3584 | #endif //AUDIO_POLICY_TEST |
| 3585 | |
| 3586 | // --- |
| 3587 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3588 | void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3589 | { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 3590 | outputDesc->setIoHandle(output); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3591 | mOutputs.add(output, outputDesc); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3592 | updateMono(output); // update mono status when adding to output list |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3593 | nextAudioPortGeneration(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3594 | } |
| 3595 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3596 | void AudioPolicyManager::removeOutput(audio_io_handle_t output) |
| 3597 | { |
| 3598 | mOutputs.removeItem(output); |
| 3599 | } |
| 3600 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3601 | void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3602 | { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 3603 | inputDesc->setIoHandle(input); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3604 | mInputs.add(input, inputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3605 | nextAudioPortGeneration(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3606 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3607 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3608 | void AudioPolicyManager::findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/, |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3609 | const audio_devices_t device /*in*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3610 | const String8 address /*in*/, |
| 3611 | SortedVector<audio_io_handle_t>& outputs /*out*/) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3612 | sp<DeviceDescriptor> devDesc = |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3613 | desc->mProfile->getSupportedDeviceByAddress(device, address); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3614 | if (devDesc != 0) { |
| 3615 | ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s", |
| 3616 | desc->mIoHandle, address.string()); |
| 3617 | outputs.add(desc->mIoHandle); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3618 | } |
| 3619 | } |
| 3620 | |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3621 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3622 | audio_policy_dev_state_t state, |
| 3623 | SortedVector<audio_io_handle_t>& outputs, |
| 3624 | const String8 address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3625 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3626 | audio_devices_t device = devDesc->type(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3627 | sp<SwAudioOutputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 3628 | |
| 3629 | if (audio_device_is_digital(device)) { |
| 3630 | // erase all current sample rates, formats and channel masks |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 3631 | devDesc->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 3632 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3633 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3634 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3635 | // first list already open outputs that can be routed to this device |
| 3636 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3637 | desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3638 | if (!desc->isDuplicated() && (desc->supportedDevices() & device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3639 | if (!device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3640 | ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i)); |
| 3641 | outputs.add(mOutputs.keyAt(i)); |
| 3642 | } else { |
| 3643 | ALOGV(" checking address match due to device 0x%x", device); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3644 | findIoHandlesByAddress(desc, device, address, outputs); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3645 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3646 | } |
| 3647 | } |
| 3648 | // then look for output profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3649 | SortedVector< sp<IOProfile> > profiles; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3650 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 3651 | { |
| 3652 | if (mHwModules[i]->mHandle == 0) { |
| 3653 | continue; |
| 3654 | } |
| 3655 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 3656 | { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3657 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3658 | if (profile->supportDevice(device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3659 | if (!device_distinguishes_on_address(device) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3660 | profile->supportDeviceAddress(address)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3661 | profiles.add(profile); |
| 3662 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i); |
| 3663 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3664 | } |
| 3665 | } |
| 3666 | } |
| 3667 | |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 3668 | ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size()); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3669 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3670 | if (profiles.isEmpty() && outputs.isEmpty()) { |
| 3671 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 3672 | return BAD_VALUE; |
| 3673 | } |
| 3674 | |
| 3675 | // open outputs for matching profiles if needed. Direct outputs are also opened to |
| 3676 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 3677 | 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] | 3678 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3679 | |
| 3680 | // nothing to do if one output is already opened for this profile |
| 3681 | size_t j; |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3682 | for (j = 0; j < outputs.size(); j++) { |
| 3683 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3684 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3685 | // matching profile: save the sample rates, format and channel masks supported |
| 3686 | // by the profile in our device descriptor |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3687 | if (audio_device_is_digital(device)) { |
| 3688 | devDesc->importAudioPort(profile); |
| 3689 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3690 | break; |
| 3691 | } |
| 3692 | } |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3693 | if (j != outputs.size()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3694 | continue; |
| 3695 | } |
| 3696 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3697 | ALOGV("opening output for device %08x with params %s profile %p", |
| 3698 | device, address.string(), profile.get()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3699 | desc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3700 | desc->mDevice = device; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3701 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3702 | config.sample_rate = desc->mSamplingRate; |
| 3703 | config.channel_mask = desc->mChannelMask; |
| 3704 | config.format = desc->mFormat; |
| 3705 | config.offload_info.sample_rate = desc->mSamplingRate; |
| 3706 | config.offload_info.channel_mask = desc->mChannelMask; |
| 3707 | config.offload_info.format = desc->mFormat; |
| 3708 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3709 | status_t status = mpClientInterface->openOutput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3710 | &output, |
| 3711 | &config, |
| 3712 | &desc->mDevice, |
| 3713 | address, |
| 3714 | &desc->mLatency, |
| 3715 | desc->mFlags); |
| 3716 | if (status == NO_ERROR) { |
| 3717 | desc->mSamplingRate = config.sample_rate; |
| 3718 | desc->mChannelMask = config.channel_mask; |
| 3719 | desc->mFormat = config.format; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3720 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3721 | // Here is where the out_set_parameters() for card & device gets called |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3722 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3723 | char *param = audio_device_address_to_parameter(device, address); |
| 3724 | mpClientInterface->setParameters(output, String8(param)); |
| 3725 | free(param); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3726 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3727 | updateAudioProfiles(output, profile->getAudioProfiles()); |
| 3728 | if (!profile->hasValidAudioProfile()) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3729 | ALOGW("checkOutputsForDevice() missing param"); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3730 | mpClientInterface->closeOutput(output); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3731 | output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3732 | } else if (profile->hasDynamicAudioProfile()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3733 | mpClientInterface->closeOutput(output); |
Phil Burk | 702b105 | 2016-03-02 16:38:26 -0800 | [diff] [blame] | 3734 | output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3735 | profile->pickAudioProfile(config.sample_rate, config.channel_mask, config.format); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3736 | config.offload_info.sample_rate = config.sample_rate; |
| 3737 | config.offload_info.channel_mask = config.channel_mask; |
| 3738 | config.offload_info.format = config.format; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3739 | status = mpClientInterface->openOutput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3740 | &output, |
| 3741 | &config, |
| 3742 | &desc->mDevice, |
| 3743 | address, |
| 3744 | &desc->mLatency, |
| 3745 | desc->mFlags); |
| 3746 | if (status == NO_ERROR) { |
| 3747 | desc->mSamplingRate = config.sample_rate; |
| 3748 | desc->mChannelMask = config.channel_mask; |
| 3749 | desc->mFormat = config.format; |
| 3750 | } else { |
| 3751 | output = AUDIO_IO_HANDLE_NONE; |
| 3752 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3753 | } |
| 3754 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3755 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3756 | addOutput(output, desc); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3757 | if (device_distinguishes_on_address(device) && address != "0") { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3758 | sp<AudioPolicyMix> policyMix; |
| 3759 | if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3760 | ALOGE("checkOutputsForDevice() cannot find policy for address %s", |
| 3761 | address.string()); |
| 3762 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3763 | policyMix->setOutput(desc); |
Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 3764 | desc->mPolicyMix = policyMix->getMix(); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3765 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 3766 | } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
| 3767 | hasPrimaryOutput()) { |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 3768 | // no duplicated output for direct outputs and |
| 3769 | // outputs used by dynamic policy mixes |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3770 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3771 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3772 | // set initial stream volume for device |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3773 | applyStreamVolumes(desc, device, 0, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3774 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3775 | //TODO: configure audio effect output stage here |
| 3776 | |
| 3777 | // 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] | 3778 | duplicatedOutput = |
| 3779 | mpClientInterface->openDuplicateOutput(output, |
| 3780 | mPrimaryOutput->mIoHandle); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3781 | if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3782 | // add duplicated output descriptor |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3783 | sp<SwAudioOutputDescriptor> dupOutputDesc = |
| 3784 | new SwAudioOutputDescriptor(NULL, mpClientInterface); |
| 3785 | dupOutputDesc->mOutput1 = mPrimaryOutput; |
| 3786 | dupOutputDesc->mOutput2 = desc; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3787 | dupOutputDesc->mSamplingRate = desc->mSamplingRate; |
| 3788 | dupOutputDesc->mFormat = desc->mFormat; |
| 3789 | dupOutputDesc->mChannelMask = desc->mChannelMask; |
| 3790 | dupOutputDesc->mLatency = desc->mLatency; |
| 3791 | addOutput(duplicatedOutput, dupOutputDesc); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3792 | applyStreamVolumes(dupOutputDesc, device, 0, true); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3793 | } else { |
| 3794 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3795 | mPrimaryOutput->mIoHandle, output); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3796 | mpClientInterface->closeOutput(output); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3797 | removeOutput(output); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3798 | nextAudioPortGeneration(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3799 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3800 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3801 | } |
| 3802 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3803 | } else { |
| 3804 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3805 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3806 | if (output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3807 | ALOGW("checkOutputsForDevice() could not open output for device %x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3808 | profiles.removeAt(profile_index); |
| 3809 | profile_index--; |
| 3810 | } else { |
| 3811 | outputs.add(output); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3812 | // Load digital format info only for digital devices |
| 3813 | if (audio_device_is_digital(device)) { |
| 3814 | devDesc->importAudioPort(profile); |
| 3815 | } |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3816 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3817 | if (device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3818 | ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)", |
| 3819 | device, address.string()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3820 | setOutputDevice(desc, device, true/*force*/, 0/*delay*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3821 | NULL/*patch handle*/, address.string()); |
| 3822 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3823 | ALOGV("checkOutputsForDevice(): adding output %d", output); |
| 3824 | } |
| 3825 | } |
| 3826 | |
| 3827 | if (profiles.isEmpty()) { |
| 3828 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 3829 | return BAD_VALUE; |
| 3830 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3831 | } else { // Disconnect |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3832 | // check if one opened output is not needed any more after disconnecting one device |
| 3833 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3834 | desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3835 | if (!desc->isDuplicated()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3836 | // exact match on device |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3837 | if (device_distinguishes_on_address(device) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3838 | (desc->supportedDevices() == device)) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3839 | findIoHandlesByAddress(desc, device, address, outputs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3840 | } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3841 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", |
| 3842 | mOutputs.keyAt(i)); |
| 3843 | outputs.add(mOutputs.keyAt(i)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3844 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3845 | } |
| 3846 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3847 | // Clear any profiles associated with the disconnected device. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3848 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 3849 | { |
| 3850 | if (mHwModules[i]->mHandle == 0) { |
| 3851 | continue; |
| 3852 | } |
| 3853 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 3854 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3855 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3856 | if (profile->supportDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3857 | ALOGV("checkOutputsForDevice(): " |
| 3858 | "clearing direct output profile %zu on module %zu", j, i); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3859 | profile->clearAudioProfiles(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3860 | } |
| 3861 | } |
| 3862 | } |
| 3863 | } |
| 3864 | return NO_ERROR; |
| 3865 | } |
| 3866 | |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3867 | status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor> devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3868 | audio_policy_dev_state_t state, |
| 3869 | SortedVector<audio_io_handle_t>& inputs, |
| 3870 | const String8 address) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3871 | { |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3872 | audio_devices_t device = devDesc->type(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3873 | sp<AudioInputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 3874 | |
| 3875 | if (audio_device_is_digital(device)) { |
| 3876 | // erase all current sample rates, formats and channel masks |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 3877 | devDesc->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 3878 | } |
| 3879 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3880 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 3881 | // first list already open inputs that can be routed to this device |
| 3882 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 3883 | desc = mInputs.valueAt(input_index); |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3884 | if (desc->mProfile->supportDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3885 | ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index)); |
| 3886 | inputs.add(mInputs.keyAt(input_index)); |
| 3887 | } |
| 3888 | } |
| 3889 | |
| 3890 | // then look for input profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3891 | SortedVector< sp<IOProfile> > profiles; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3892 | for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++) |
| 3893 | { |
| 3894 | if (mHwModules[module_idx]->mHandle == 0) { |
| 3895 | continue; |
| 3896 | } |
| 3897 | for (size_t profile_index = 0; |
| 3898 | profile_index < mHwModules[module_idx]->mInputProfiles.size(); |
| 3899 | profile_index++) |
| 3900 | { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3901 | sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index]; |
| 3902 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3903 | if (profile->supportDevice(device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3904 | if (!device_distinguishes_on_address(device) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3905 | profile->supportDeviceAddress(address)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3906 | profiles.add(profile); |
| 3907 | ALOGV("checkInputsForDevice(): adding profile %zu from module %zu", |
| 3908 | profile_index, module_idx); |
| 3909 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3910 | } |
| 3911 | } |
| 3912 | } |
| 3913 | |
| 3914 | if (profiles.isEmpty() && inputs.isEmpty()) { |
| 3915 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 3916 | return BAD_VALUE; |
| 3917 | } |
| 3918 | |
| 3919 | // open inputs for matching profiles if needed. Direct inputs are also opened to |
| 3920 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 3921 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { |
| 3922 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3923 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3924 | // nothing to do if one input is already opened for this profile |
| 3925 | size_t input_index; |
| 3926 | for (input_index = 0; input_index < mInputs.size(); input_index++) { |
| 3927 | desc = mInputs.valueAt(input_index); |
| 3928 | if (desc->mProfile == profile) { |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3929 | if (audio_device_is_digital(device)) { |
| 3930 | devDesc->importAudioPort(profile); |
| 3931 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3932 | break; |
| 3933 | } |
| 3934 | } |
| 3935 | if (input_index != mInputs.size()) { |
| 3936 | continue; |
| 3937 | } |
| 3938 | |
| 3939 | ALOGV("opening input for device 0x%X with params %s", device, address.string()); |
| 3940 | desc = new AudioInputDescriptor(profile); |
| 3941 | desc->mDevice = device; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3942 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3943 | config.sample_rate = desc->mSamplingRate; |
| 3944 | config.channel_mask = desc->mChannelMask; |
| 3945 | config.format = desc->mFormat; |
| 3946 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3947 | status_t status = mpClientInterface->openInput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3948 | &input, |
| 3949 | &config, |
| 3950 | &desc->mDevice, |
| 3951 | address, |
| 3952 | AUDIO_SOURCE_MIC, |
| 3953 | AUDIO_INPUT_FLAG_NONE /*FIXME*/); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3954 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3955 | if (status == NO_ERROR) { |
| 3956 | desc->mSamplingRate = config.sample_rate; |
| 3957 | desc->mChannelMask = config.channel_mask; |
| 3958 | desc->mFormat = config.format; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3959 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3960 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3961 | char *param = audio_device_address_to_parameter(device, address); |
| 3962 | mpClientInterface->setParameters(input, String8(param)); |
| 3963 | free(param); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3964 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3965 | updateAudioProfiles(input, profile->getAudioProfiles()); |
| 3966 | if (!profile->hasValidAudioProfile()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3967 | ALOGW("checkInputsForDevice() direct input missing param"); |
| 3968 | mpClientInterface->closeInput(input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3969 | input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3970 | } |
| 3971 | |
| 3972 | if (input != 0) { |
| 3973 | addInput(input, desc); |
| 3974 | } |
| 3975 | } // endif input != 0 |
| 3976 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3977 | if (input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3978 | ALOGW("checkInputsForDevice() could not open input for device 0x%X", device); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3979 | profiles.removeAt(profile_index); |
| 3980 | profile_index--; |
| 3981 | } else { |
| 3982 | inputs.add(input); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3983 | if (audio_device_is_digital(device)) { |
| 3984 | devDesc->importAudioPort(profile); |
| 3985 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3986 | ALOGV("checkInputsForDevice(): adding input %d", input); |
| 3987 | } |
| 3988 | } // end scan profiles |
| 3989 | |
| 3990 | if (profiles.isEmpty()) { |
| 3991 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 3992 | return BAD_VALUE; |
| 3993 | } |
| 3994 | } else { |
| 3995 | // Disconnect |
| 3996 | // check if one opened input is not needed any more after disconnecting one device |
| 3997 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 3998 | desc = mInputs.valueAt(input_index); |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3999 | if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4000 | ALOGV("checkInputsForDevice(): disconnecting adding input %d", |
| 4001 | mInputs.keyAt(input_index)); |
| 4002 | inputs.add(mInputs.keyAt(input_index)); |
| 4003 | } |
| 4004 | } |
| 4005 | // Clear any profiles associated with the disconnected device. |
| 4006 | for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) { |
| 4007 | if (mHwModules[module_index]->mHandle == 0) { |
| 4008 | continue; |
| 4009 | } |
| 4010 | for (size_t profile_index = 0; |
| 4011 | profile_index < mHwModules[module_index]->mInputProfiles.size(); |
| 4012 | profile_index++) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4013 | sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4014 | if (profile->supportDevice(device)) { |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 4015 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu", |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4016 | profile_index, module_index); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4017 | profile->clearAudioProfiles(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4018 | } |
| 4019 | } |
| 4020 | } |
| 4021 | } // end disconnect |
| 4022 | |
| 4023 | return NO_ERROR; |
| 4024 | } |
| 4025 | |
| 4026 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4027 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4028 | { |
| 4029 | ALOGV("closeOutput(%d)", output); |
| 4030 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4031 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4032 | if (outputDesc == NULL) { |
| 4033 | ALOGW("closeOutput() unknown output %d", output); |
| 4034 | return; |
| 4035 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4036 | mPolicyMixes.closeOutput(outputDesc); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4037 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4038 | // look for duplicated outputs connected to the output being removed. |
| 4039 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4040 | sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4041 | if (dupOutputDesc->isDuplicated() && |
| 4042 | (dupOutputDesc->mOutput1 == outputDesc || |
| 4043 | dupOutputDesc->mOutput2 == outputDesc)) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4044 | sp<AudioOutputDescriptor> outputDesc2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4045 | if (dupOutputDesc->mOutput1 == outputDesc) { |
| 4046 | outputDesc2 = dupOutputDesc->mOutput2; |
| 4047 | } else { |
| 4048 | outputDesc2 = dupOutputDesc->mOutput1; |
| 4049 | } |
| 4050 | // As all active tracks on duplicated output will be deleted, |
| 4051 | // and as they were also referenced on the other output, the reference |
| 4052 | // count for their stream type must be adjusted accordingly on |
| 4053 | // the other output. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4054 | for (int j = 0; j < AUDIO_STREAM_CNT; j++) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4055 | int refCount = dupOutputDesc->mRefCount[j]; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4056 | outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4057 | } |
| 4058 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); |
| 4059 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); |
| 4060 | |
| 4061 | mpClientInterface->closeOutput(duplicatedOutput); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4062 | removeOutput(duplicatedOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4063 | } |
| 4064 | } |
| 4065 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4066 | nextAudioPortGeneration(); |
| 4067 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4068 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4069 | if (index >= 0) { |
| 4070 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4071 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 4072 | mAudioPatches.removeItemsAt(index); |
| 4073 | mpClientInterface->onAudioPatchListUpdate(); |
| 4074 | } |
| 4075 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4076 | AudioParameter param; |
| 4077 | param.add(String8("closing"), String8("true")); |
| 4078 | mpClientInterface->setParameters(output, param.toString()); |
| 4079 | |
| 4080 | mpClientInterface->closeOutput(output); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4081 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4082 | mPreviousOutputs = mOutputs; |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4083 | } |
| 4084 | |
| 4085 | void AudioPolicyManager::closeInput(audio_io_handle_t input) |
| 4086 | { |
| 4087 | ALOGV("closeInput(%d)", input); |
| 4088 | |
| 4089 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
| 4090 | if (inputDesc == NULL) { |
| 4091 | ALOGW("closeInput() unknown input %d", input); |
| 4092 | return; |
| 4093 | } |
| 4094 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4095 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4096 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4097 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4098 | if (index >= 0) { |
| 4099 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4100 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 4101 | mAudioPatches.removeItemsAt(index); |
| 4102 | mpClientInterface->onAudioPatchListUpdate(); |
| 4103 | } |
| 4104 | |
| 4105 | mpClientInterface->closeInput(input); |
| 4106 | mInputs.removeItem(input); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4107 | } |
| 4108 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4109 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice( |
| 4110 | audio_devices_t device, |
| 4111 | SwAudioOutputCollection openOutputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4112 | { |
| 4113 | SortedVector<audio_io_handle_t> outputs; |
| 4114 | |
| 4115 | ALOGVV("getOutputsForDevice() device %04x", device); |
| 4116 | for (size_t i = 0; i < openOutputs.size(); i++) { |
| 4117 | ALOGVV("output %d isDuplicated=%d device=%04x", |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4118 | i, openOutputs.valueAt(i)->isDuplicated(), |
| 4119 | openOutputs.valueAt(i)->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4120 | if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) { |
| 4121 | ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i)); |
| 4122 | outputs.add(openOutputs.keyAt(i)); |
| 4123 | } |
| 4124 | } |
| 4125 | return outputs; |
| 4126 | } |
| 4127 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4128 | bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4129 | SortedVector<audio_io_handle_t>& outputs2) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4130 | { |
| 4131 | if (outputs1.size() != outputs2.size()) { |
| 4132 | return false; |
| 4133 | } |
| 4134 | for (size_t i = 0; i < outputs1.size(); i++) { |
| 4135 | if (outputs1[i] != outputs2[i]) { |
| 4136 | return false; |
| 4137 | } |
| 4138 | } |
| 4139 | return true; |
| 4140 | } |
| 4141 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4142 | void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4143 | { |
| 4144 | audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/); |
| 4145 | audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 4146 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs); |
| 4147 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs); |
| 4148 | |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4149 | // also take into account external policy-related changes: add all outputs which are |
| 4150 | // associated with policies in the "before" and "after" output vectors |
| 4151 | ALOGVV("checkOutputForStrategy(): policy related outputs"); |
| 4152 | for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4153 | const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4154 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4155 | srcOutputs.add(desc->mIoHandle); |
| 4156 | ALOGVV(" previous outputs: adding %d", desc->mIoHandle); |
| 4157 | } |
| 4158 | } |
| 4159 | for (size_t i = 0 ; i < mOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4160 | const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4161 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4162 | dstOutputs.add(desc->mIoHandle); |
| 4163 | ALOGVV(" new outputs: adding %d", desc->mIoHandle); |
| 4164 | } |
| 4165 | } |
| 4166 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4167 | if (!vectorsEqual(srcOutputs,dstOutputs)) { |
| 4168 | ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d", |
| 4169 | strategy, srcOutputs[0], dstOutputs[0]); |
| 4170 | // mute strategy while moving tracks from one output to another |
| 4171 | for (size_t i = 0; i < srcOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4172 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4173 | if (isStrategyActive(desc, strategy)) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4174 | setStrategyMute(strategy, true, desc); |
| 4175 | setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4176 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4177 | sp<AudioSourceDescriptor> source = |
| 4178 | getSourceForStrategyOnOutput(srcOutputs[i], strategy); |
| 4179 | if (source != 0){ |
| 4180 | connectAudioSource(source); |
| 4181 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4182 | } |
| 4183 | |
| 4184 | // Move effects associated to this strategy from previous output to new output |
| 4185 | if (strategy == STRATEGY_MEDIA) { |
| 4186 | audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs); |
| 4187 | SortedVector<audio_io_handle_t> moved; |
| 4188 | for (size_t i = 0; i < mEffects.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4189 | sp<EffectDescriptor> effectDesc = mEffects.valueAt(i); |
| 4190 | if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX && |
| 4191 | effectDesc->mIo != fxOutput) { |
| 4192 | if (moved.indexOf(effectDesc->mIo) < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4193 | ALOGV("checkOutputForStrategy() moving effect %d to output %d", |
| 4194 | mEffects.keyAt(i), fxOutput); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4195 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4196 | fxOutput); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4197 | moved.add(effectDesc->mIo); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4198 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4199 | effectDesc->mIo = fxOutput; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4200 | } |
| 4201 | } |
| 4202 | } |
| 4203 | // Move tracks associated to this strategy from previous output to new output |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4204 | for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4205 | if (getStrategy((audio_stream_type_t)i) == strategy) { |
| 4206 | mpClientInterface->invalidateStream((audio_stream_type_t)i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4207 | } |
| 4208 | } |
| 4209 | } |
| 4210 | } |
| 4211 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4212 | void AudioPolicyManager::checkOutputForAllStrategies() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4213 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4214 | 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] | 4215 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4216 | checkOutputForStrategy(STRATEGY_PHONE); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4217 | 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] | 4218 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4219 | checkOutputForStrategy(STRATEGY_SONIFICATION); |
| 4220 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4221 | checkOutputForStrategy(STRATEGY_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4222 | checkOutputForStrategy(STRATEGY_MEDIA); |
| 4223 | checkOutputForStrategy(STRATEGY_DTMF); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4224 | checkOutputForStrategy(STRATEGY_REROUTING); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4225 | } |
| 4226 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4227 | void AudioPolicyManager::checkA2dpSuspend() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4228 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4229 | audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4230 | if (a2dpOutput == 0) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4231 | mA2dpSuspended = false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4232 | return; |
| 4233 | } |
| 4234 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4235 | bool isScoConnected = |
Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 4236 | ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET & |
| 4237 | ~AUDIO_DEVICE_BIT_IN) != 0) || |
| 4238 | ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4239 | // suspend A2DP output if: |
| 4240 | // (NOT already suspended) && |
| 4241 | // ((SCO device is connected && |
| 4242 | // (forced usage for communication || for record is SCO))) || |
| 4243 | // (phone state is ringing || in call) |
| 4244 | // |
| 4245 | // restore A2DP output if: |
| 4246 | // (Already suspended) && |
| 4247 | // ((SCO device is NOT connected || |
| 4248 | // (forced usage NOT for communication && NOT for record is SCO))) && |
| 4249 | // (phone state is NOT ringing && NOT in call) |
| 4250 | // |
| 4251 | if (mA2dpSuspended) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4252 | if ((!isScoConnected || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4253 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) && |
| 4254 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) && |
| 4255 | ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) && |
| 4256 | (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4257 | |
| 4258 | mpClientInterface->restoreOutput(a2dpOutput); |
| 4259 | mA2dpSuspended = false; |
| 4260 | } |
| 4261 | } else { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4262 | if ((isScoConnected && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4263 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) || |
| 4264 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO))) || |
| 4265 | ((mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) || |
| 4266 | (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4267 | |
| 4268 | mpClientInterface->suspendOutput(a2dpOutput); |
| 4269 | mA2dpSuspended = true; |
| 4270 | } |
| 4271 | } |
| 4272 | } |
| 4273 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4274 | audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
| 4275 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4276 | { |
| 4277 | audio_devices_t device = AUDIO_DEVICE_NONE; |
| 4278 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4279 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4280 | if (index >= 0) { |
| 4281 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4282 | if (patchDesc->mUid != mUidCached) { |
| 4283 | ALOGV("getNewOutputDevice() device %08x forced by patch %d", |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4284 | outputDesc->device(), outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4285 | return outputDesc->device(); |
| 4286 | } |
| 4287 | } |
| 4288 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4289 | // 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] | 4290 | // 1: the strategy enforced audible is active and enforced on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4291 | // use device for strategy enforced audible |
| 4292 | // 2: we are in call or the strategy phone is active on the output: |
| 4293 | // use device for strategy phone |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 4294 | // 3: the strategy for enforced audible is active but not enforced on the output: |
| 4295 | // use the device for strategy enforced audible |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4296 | // 4: the strategy sonification is active on the output: |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4297 | // use device for strategy sonification |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4298 | // 5: the strategy accessibility is active on the output: |
| 4299 | // use device for strategy accessibility |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4300 | // 6: the strategy "respectful" sonification is active on the output: |
| 4301 | // use device for strategy "respectful" sonification |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4302 | // 7: the strategy media is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4303 | // use device for strategy media |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4304 | // 8: the strategy DTMF is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4305 | // use device for strategy DTMF |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4306 | // 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] | 4307 | // use device for strategy t-t-s |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4308 | if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4309 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4310 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
| 4311 | } else if (isInCall() || |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4312 | isStrategyActive(outputDesc, STRATEGY_PHONE)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4313 | device = getDeviceForStrategy(STRATEGY_PHONE, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4314 | } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) { |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 4315 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4316 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4317 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4318 | } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) { |
| 4319 | device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4320 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4321 | device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4322 | } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4323 | device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4324 | } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4325 | device = getDeviceForStrategy(STRATEGY_DTMF, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4326 | } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4327 | device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4328 | } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4329 | device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4330 | } |
| 4331 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4332 | ALOGV("getNewOutputDevice() selected device %x", device); |
| 4333 | return device; |
| 4334 | } |
| 4335 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4336 | audio_devices_t AudioPolicyManager::getNewInputDevice(const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4337 | { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4338 | audio_devices_t device = AUDIO_DEVICE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4339 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4340 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4341 | if (index >= 0) { |
| 4342 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4343 | if (patchDesc->mUid != mUidCached) { |
| 4344 | ALOGV("getNewInputDevice() device %08x forced by patch %d", |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4345 | inputDesc->mDevice, inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4346 | return inputDesc->mDevice; |
| 4347 | } |
| 4348 | } |
| 4349 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4350 | audio_source_t source = inputDesc->getHighestPrioritySource(true /*activeOnly*/); |
| 4351 | if (isInCall()) { |
| 4352 | device = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); |
| 4353 | } else if (source != AUDIO_SOURCE_DEFAULT) { |
| 4354 | device = getDeviceAndMixForInputSource(source); |
| 4355 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4356 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4357 | return device; |
| 4358 | } |
| 4359 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4360 | bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1, |
| 4361 | audio_stream_type_t stream2) { |
| 4362 | return ((stream1 == stream2) || |
| 4363 | ((stream1 == AUDIO_STREAM_ACCESSIBILITY) && (stream2 == AUDIO_STREAM_MUSIC)) || |
| 4364 | ((stream1 == AUDIO_STREAM_MUSIC) && (stream2 == AUDIO_STREAM_ACCESSIBILITY))); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4365 | } |
| 4366 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4367 | uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4368 | return (uint32_t)getStrategy(stream); |
| 4369 | } |
| 4370 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4371 | audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4372 | // By checking the range of stream before calling getStrategy, we avoid |
| 4373 | // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE |
| 4374 | // 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] | 4375 | if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4376 | return AUDIO_DEVICE_NONE; |
| 4377 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4378 | audio_devices_t devices = AUDIO_DEVICE_NONE; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4379 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 4380 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4381 | continue; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4382 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4383 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4384 | audio_devices_t curDevices = |
| 4385 | getDeviceForStrategy((routing_strategy)curStrategy, true /*fromCache*/); |
| 4386 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(curDevices, mOutputs); |
| 4387 | for (size_t i = 0; i < outputs.size(); i++) { |
| 4388 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4389 | if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4390 | curDevices |= outputDesc->device(); |
| 4391 | } |
| 4392 | } |
| 4393 | devices |= curDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4394 | } |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4395 | |
| 4396 | /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it |
| 4397 | and doesn't really need to.*/ |
| 4398 | if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) { |
| 4399 | devices |= AUDIO_DEVICE_OUT_SPEAKER; |
| 4400 | devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
| 4401 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4402 | return devices; |
| 4403 | } |
| 4404 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4405 | routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const |
| 4406 | { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4407 | 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] | 4408 | return mEngine->getStrategyForStream(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4409 | } |
| 4410 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4411 | uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) { |
| 4412 | // flags to strategy mapping |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4413 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 4414 | return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER; |
| 4415 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4416 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 4417 | return (uint32_t) STRATEGY_ENFORCED_AUDIBLE; |
| 4418 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4419 | // usage to strategy mapping |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4420 | return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage)); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4421 | } |
| 4422 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4423 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4424 | switch(stream) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4425 | case AUDIO_STREAM_MUSIC: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4426 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
| 4427 | updateDevicesAndOutputs(); |
| 4428 | break; |
| 4429 | default: |
| 4430 | break; |
| 4431 | } |
| 4432 | } |
| 4433 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4434 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 4435 | |
| 4436 | // skip beacon mute management if a dedicated TTS output is available |
| 4437 | if (mTtsOutputAvailable) { |
| 4438 | return 0; |
| 4439 | } |
| 4440 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4441 | switch(event) { |
| 4442 | case STARTING_OUTPUT: |
| 4443 | mBeaconMuteRefCount++; |
| 4444 | break; |
| 4445 | case STOPPING_OUTPUT: |
| 4446 | if (mBeaconMuteRefCount > 0) { |
| 4447 | mBeaconMuteRefCount--; |
| 4448 | } |
| 4449 | break; |
| 4450 | case STARTING_BEACON: |
| 4451 | mBeaconPlayingRefCount++; |
| 4452 | break; |
| 4453 | case STOPPING_BEACON: |
| 4454 | if (mBeaconPlayingRefCount > 0) { |
| 4455 | mBeaconPlayingRefCount--; |
| 4456 | } |
| 4457 | break; |
| 4458 | } |
| 4459 | |
| 4460 | if (mBeaconMuteRefCount > 0) { |
| 4461 | // any playback causes beacon to be muted |
| 4462 | return setBeaconMute(true); |
| 4463 | } else { |
| 4464 | // no other playback: unmute when beacon starts playing, mute when it stops |
| 4465 | return setBeaconMute(mBeaconPlayingRefCount == 0); |
| 4466 | } |
| 4467 | } |
| 4468 | |
| 4469 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { |
| 4470 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", |
| 4471 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); |
| 4472 | // keep track of muted state to avoid repeating mute/unmute operations |
| 4473 | if (mBeaconMuted != mute) { |
| 4474 | // mute/unmute AUDIO_STREAM_TTS on all outputs |
| 4475 | ALOGV("\t muting %d", mute); |
| 4476 | uint32_t maxLatency = 0; |
| 4477 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4478 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4479 | setStreamMute(AUDIO_STREAM_TTS, mute/*on*/, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4480 | desc, |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4481 | 0 /*delay*/, AUDIO_DEVICE_NONE); |
| 4482 | const uint32_t latency = desc->latency() * 2; |
| 4483 | if (latency > maxLatency) { |
| 4484 | maxLatency = latency; |
| 4485 | } |
| 4486 | } |
| 4487 | mBeaconMuted = mute; |
| 4488 | return maxLatency; |
| 4489 | } |
| 4490 | return 0; |
| 4491 | } |
| 4492 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4493 | audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4494 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4495 | { |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4496 | // Routing |
| 4497 | // see if we have an explicit route |
| 4498 | // scan the whole RouteMap, for each entry, convert the stream type to a strategy |
| 4499 | // (getStrategy(stream)). |
| 4500 | // if the strategy from the stream type in the RouteMap is the same as the argument above, |
| 4501 | // and activity count is non-zero |
| 4502 | // the device = the device from the descriptor in the RouteMap, and exit. |
| 4503 | for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) { |
| 4504 | sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4505 | routing_strategy routeStrategy = getStrategy(route->mStreamType); |
| 4506 | if ((routeStrategy == strategy) && route->isActive()) { |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4507 | return route->mDeviceDescriptor->type(); |
| 4508 | } |
| 4509 | } |
| 4510 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4511 | if (fromCache) { |
| 4512 | ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x", |
| 4513 | strategy, mDeviceForStrategy[strategy]); |
| 4514 | return mDeviceForStrategy[strategy]; |
| 4515 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4516 | return mEngine->getDeviceForStrategy(strategy); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4517 | } |
| 4518 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4519 | void AudioPolicyManager::updateDevicesAndOutputs() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4520 | { |
| 4521 | for (int i = 0; i < NUM_STRATEGIES; i++) { |
| 4522 | mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
| 4523 | } |
| 4524 | mPreviousOutputs = mOutputs; |
| 4525 | } |
| 4526 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4527 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4528 | audio_devices_t prevDevice, |
| 4529 | uint32_t delayMs) |
| 4530 | { |
| 4531 | // mute/unmute strategies using an incompatible device combination |
| 4532 | // if muting, wait for the audio in pcm buffer to be drained before proceeding |
| 4533 | // if unmuting, unmute only after the specified delay |
| 4534 | if (outputDesc->isDuplicated()) { |
| 4535 | return 0; |
| 4536 | } |
| 4537 | |
| 4538 | uint32_t muteWaitMs = 0; |
| 4539 | audio_devices_t device = outputDesc->device(); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4540 | bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4541 | |
| 4542 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
| 4543 | audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4544 | curDevice = curDevice & outputDesc->supportedDevices(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4545 | bool mute = shouldMute && (curDevice & device) && (curDevice != device); |
| 4546 | bool doMute = false; |
| 4547 | |
| 4548 | if (mute && !outputDesc->mStrategyMutedByDevice[i]) { |
| 4549 | doMute = true; |
| 4550 | outputDesc->mStrategyMutedByDevice[i] = true; |
| 4551 | } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){ |
| 4552 | doMute = true; |
| 4553 | outputDesc->mStrategyMutedByDevice[i] = false; |
| 4554 | } |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4555 | if (doMute) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4556 | for (size_t j = 0; j < mOutputs.size(); j++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4557 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4558 | // skip output if it does not share any device with current output |
| 4559 | if ((desc->supportedDevices() & outputDesc->supportedDevices()) |
| 4560 | == AUDIO_DEVICE_NONE) { |
| 4561 | continue; |
| 4562 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4563 | ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x)", |
| 4564 | mute ? "muting" : "unmuting", i, curDevice); |
| 4565 | setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4566 | if (isStrategyActive(desc, (routing_strategy)i)) { |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4567 | if (mute) { |
| 4568 | // FIXME: should not need to double latency if volume could be applied |
| 4569 | // immediately by the audioflinger mixer. We must account for the delay |
| 4570 | // between now and the next time the audioflinger thread for this output |
| 4571 | // will process a buffer (which corresponds to one buffer size, |
| 4572 | // usually 1/2 or 1/4 of the latency). |
| 4573 | if (muteWaitMs < desc->latency() * 2) { |
| 4574 | muteWaitMs = desc->latency() * 2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4575 | } |
| 4576 | } |
| 4577 | } |
| 4578 | } |
| 4579 | } |
| 4580 | } |
| 4581 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4582 | // temporary mute output if device selection changes to avoid volume bursts due to |
| 4583 | // different per device volumes |
| 4584 | if (outputDesc->isActive() && (device != prevDevice)) { |
| 4585 | if (muteWaitMs < outputDesc->latency() * 2) { |
| 4586 | muteWaitMs = outputDesc->latency() * 2; |
| 4587 | } |
| 4588 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4589 | if (isStrategyActive(outputDesc, (routing_strategy)i)) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4590 | setStrategyMute((routing_strategy)i, true, outputDesc); |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4591 | // do tempMute unmute after twice the mute wait time |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4592 | setStrategyMute((routing_strategy)i, false, outputDesc, |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4593 | muteWaitMs *2, device); |
| 4594 | } |
| 4595 | } |
| 4596 | } |
| 4597 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4598 | // wait for the PCM output buffers to empty before proceeding with the rest of the command |
| 4599 | if (muteWaitMs > delayMs) { |
| 4600 | muteWaitMs -= delayMs; |
| 4601 | usleep(muteWaitMs * 1000); |
| 4602 | return muteWaitMs; |
| 4603 | } |
| 4604 | return 0; |
| 4605 | } |
| 4606 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4607 | uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4608 | audio_devices_t device, |
| 4609 | bool force, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4610 | int delayMs, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4611 | audio_patch_handle_t *patchHandle, |
| 4612 | const char* address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4613 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4614 | ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4615 | AudioParameter param; |
| 4616 | uint32_t muteWaitMs; |
| 4617 | |
| 4618 | if (outputDesc->isDuplicated()) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4619 | muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs); |
| 4620 | muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4621 | return muteWaitMs; |
| 4622 | } |
| 4623 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current |
| 4624 | // output profile |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4625 | if ((device != AUDIO_DEVICE_NONE) && |
| 4626 | ((device & outputDesc->supportedDevices()) == 0)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4627 | return 0; |
| 4628 | } |
| 4629 | |
| 4630 | // filter devices according to output selected |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4631 | device = (audio_devices_t)(device & outputDesc->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4632 | |
| 4633 | audio_devices_t prevDevice = outputDesc->mDevice; |
| 4634 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4635 | ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4636 | |
| 4637 | if (device != AUDIO_DEVICE_NONE) { |
| 4638 | outputDesc->mDevice = device; |
| 4639 | } |
| 4640 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs); |
| 4641 | |
| 4642 | // Do not change the routing if: |
Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 4643 | // the requested device is AUDIO_DEVICE_NONE |
| 4644 | // OR the requested device is the same as current device |
| 4645 | // AND force is not specified |
| 4646 | // AND the output is connected by a valid audio patch. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4647 | // Doing this check here allows the caller to call setOutputDevice() without conditions |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4648 | if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && |
| 4649 | !force && |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4650 | outputDesc->getPatchHandle() != 0) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4651 | ALOGV("setOutputDevice() setting same device 0x%04x or null device", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4652 | return muteWaitMs; |
| 4653 | } |
| 4654 | |
| 4655 | ALOGV("setOutputDevice() changing device"); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4656 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4657 | // do the routing |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4658 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4659 | resetOutputDevice(outputDesc, delayMs, NULL); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4660 | } else { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4661 | DeviceVector deviceList = (address == NULL) ? |
| 4662 | mAvailableOutputDevices.getDevicesFromType(device) |
| 4663 | : mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address)); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4664 | if (!deviceList.isEmpty()) { |
| 4665 | struct audio_patch patch; |
| 4666 | outputDesc->toAudioPortConfig(&patch.sources[0]); |
| 4667 | patch.num_sources = 1; |
| 4668 | patch.num_sinks = 0; |
| 4669 | for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) { |
| 4670 | deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4671 | patch.num_sinks++; |
| 4672 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4673 | ssize_t index; |
| 4674 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { |
| 4675 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4676 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4677 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4678 | } |
| 4679 | sp< AudioPatch> patchDesc; |
| 4680 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 4681 | if (index >= 0) { |
| 4682 | patchDesc = mAudioPatches.valueAt(index); |
| 4683 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 4684 | } |
| 4685 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4686 | status_t status = mpClientInterface->createAudioPatch(&patch, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4687 | &afPatchHandle, |
| 4688 | delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4689 | ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d" |
| 4690 | "num_sources %d num_sinks %d", |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4691 | status, afPatchHandle, patch.num_sources, patch.num_sinks); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4692 | if (status == NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4693 | if (index < 0) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 4694 | patchDesc = new AudioPatch(&patch, mUidCached); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4695 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 4696 | } else { |
| 4697 | patchDesc->mPatch = patch; |
| 4698 | } |
| 4699 | patchDesc->mAfPatchHandle = afPatchHandle; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4700 | if (patchHandle) { |
| 4701 | *patchHandle = patchDesc->mHandle; |
| 4702 | } |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4703 | outputDesc->setPatchHandle(patchDesc->mHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4704 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4705 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4706 | } |
| 4707 | } |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 4708 | |
| 4709 | // inform all input as well |
| 4710 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 4711 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4712 | if (!is_virtual_input_device(inputDescriptor->mDevice)) { |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 4713 | AudioParameter inputCmd = AudioParameter(); |
| 4714 | ALOGV("%s: inform input %d of device:%d", __func__, |
| 4715 | inputDescriptor->mIoHandle, device); |
| 4716 | inputCmd.addInt(String8(AudioParameter::keyRouting),device); |
| 4717 | mpClientInterface->setParameters(inputDescriptor->mIoHandle, |
| 4718 | inputCmd.toString(), |
| 4719 | delayMs); |
| 4720 | } |
| 4721 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4722 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4723 | |
| 4724 | // update stream volumes according to new device |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4725 | applyStreamVolumes(outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4726 | |
| 4727 | return muteWaitMs; |
| 4728 | } |
| 4729 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4730 | status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4731 | int delayMs, |
| 4732 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4733 | { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4734 | ssize_t index; |
| 4735 | if (patchHandle) { |
| 4736 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4737 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4738 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4739 | } |
| 4740 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4741 | return INVALID_OPERATION; |
| 4742 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4743 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4744 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4745 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4746 | outputDesc->setPatchHandle(0); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4747 | removeAudioPatch(patchDesc->mHandle); |
| 4748 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4749 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4750 | return status; |
| 4751 | } |
| 4752 | |
| 4753 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, |
| 4754 | audio_devices_t device, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4755 | bool force, |
| 4756 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4757 | { |
| 4758 | status_t status = NO_ERROR; |
| 4759 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4760 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4761 | if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) { |
| 4762 | inputDesc->mDevice = device; |
| 4763 | |
| 4764 | DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device); |
| 4765 | if (!deviceList.isEmpty()) { |
| 4766 | struct audio_patch patch; |
| 4767 | inputDesc->toAudioPortConfig(&patch.sinks[0]); |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 4768 | // AUDIO_SOURCE_HOTWORD is for internal use only: |
| 4769 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 4770 | if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD && |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 4771 | !inputDesc->isSoundTrigger()) { |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 4772 | patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 4773 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4774 | patch.num_sinks = 1; |
| 4775 | //only one input device for now |
| 4776 | deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4777 | patch.num_sources = 1; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4778 | ssize_t index; |
| 4779 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { |
| 4780 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4781 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4782 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4783 | } |
| 4784 | sp< AudioPatch> patchDesc; |
| 4785 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 4786 | if (index >= 0) { |
| 4787 | patchDesc = mAudioPatches.valueAt(index); |
| 4788 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 4789 | } |
| 4790 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4791 | status_t status = mpClientInterface->createAudioPatch(&patch, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4792 | &afPatchHandle, |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4793 | 0); |
| 4794 | ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d", |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4795 | status, afPatchHandle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4796 | if (status == NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4797 | if (index < 0) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 4798 | patchDesc = new AudioPatch(&patch, mUidCached); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4799 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 4800 | } else { |
| 4801 | patchDesc->mPatch = patch; |
| 4802 | } |
| 4803 | patchDesc->mAfPatchHandle = afPatchHandle; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4804 | if (patchHandle) { |
| 4805 | *patchHandle = patchDesc->mHandle; |
| 4806 | } |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4807 | inputDesc->setPatchHandle(patchDesc->mHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4808 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4809 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4810 | } |
| 4811 | } |
| 4812 | } |
| 4813 | return status; |
| 4814 | } |
| 4815 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4816 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, |
| 4817 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4818 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4819 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4820 | ssize_t index; |
| 4821 | if (patchHandle) { |
| 4822 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4823 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4824 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4825 | } |
| 4826 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4827 | return INVALID_OPERATION; |
| 4828 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4829 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4830 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4831 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4832 | inputDesc->setPatchHandle(0); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4833 | removeAudioPatch(patchDesc->mHandle); |
| 4834 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4835 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4836 | return status; |
| 4837 | } |
| 4838 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 4839 | sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4840 | String8 address, |
| 4841 | uint32_t& samplingRate, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4842 | audio_format_t& format, |
| 4843 | audio_channel_mask_t& channelMask, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4844 | audio_input_flags_t flags) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4845 | { |
| 4846 | // Choose an input profile based on the requested capture parameters: select the first available |
| 4847 | // profile supporting all requested parameters. |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4848 | // |
| 4849 | // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return |
| 4850 | // the best matching profile, not the first one. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4851 | |
| 4852 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 4853 | { |
| 4854 | if (mHwModules[i]->mHandle == 0) { |
| 4855 | continue; |
| 4856 | } |
| 4857 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) |
| 4858 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4859 | sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j]; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4860 | // profile->log(); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4861 | if (profile->isCompatibleProfile(device, address, samplingRate, |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 4862 | &samplingRate /*updatedSamplingRate*/, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4863 | format, |
| 4864 | &format /*updatedFormat*/, |
| 4865 | channelMask, |
| 4866 | &channelMask /*updatedChannelMask*/, |
| 4867 | (audio_output_flags_t) flags)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4868 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4869 | return profile; |
| 4870 | } |
| 4871 | } |
| 4872 | } |
| 4873 | return NULL; |
| 4874 | } |
| 4875 | |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 4876 | |
| 4877 | audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4878 | AudioMix **policyMix) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4879 | { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4880 | audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
| 4881 | audio_devices_t selectedDeviceFromMix = |
| 4882 | mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4883 | |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4884 | if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) { |
| 4885 | return selectedDeviceFromMix; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4886 | } |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 4887 | return getDeviceForInputSource(inputSource); |
| 4888 | } |
| 4889 | |
| 4890 | audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource) |
| 4891 | { |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 4892 | for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) { |
| 4893 | sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4894 | if (inputSource == route->mSource && route->isActive()) { |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 4895 | return route->mDeviceDescriptor->type(); |
| 4896 | } |
| 4897 | } |
| 4898 | |
| 4899 | return mEngine->getDeviceForInputSource(inputSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4900 | } |
| 4901 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4902 | float AudioPolicyManager::computeVolume(audio_stream_type_t stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4903 | int index, |
| 4904 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4905 | { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4906 | float volumeDb = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4907 | // if a headset is connected, apply the following rules to ring tones and notifications |
| 4908 | // to avoid sound level bursts in user's ears: |
| 4909 | // - always attenuate ring tones and notifications volume by 6dB |
| 4910 | // - if music is playing, always limit the volume to current music volume, |
| 4911 | // with a minimum threshold at -36dB so that notification is always perceived. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4912 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4913 | if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 4914 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | |
| 4915 | AUDIO_DEVICE_OUT_WIRED_HEADSET | |
| 4916 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) && |
| 4917 | ((stream_strategy == STRATEGY_SONIFICATION) |
| 4918 | || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL) |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4919 | || (stream == AUDIO_STREAM_SYSTEM) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4920 | || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4921 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) && |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4922 | mVolumeCurves->canBeMuted(stream)) { |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4923 | volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4924 | // when the phone is ringing we must consider that music could have been paused just before |
| 4925 | // by the music application and behave as if music was active if the last music track was |
| 4926 | // just stopped |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4927 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4928 | mLimitRingtoneVolume) { |
| 4929 | audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4930 | float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4931 | mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC, |
| 4932 | musicDevice), |
| 4933 | musicDevice); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4934 | float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ? |
| 4935 | musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB; |
| 4936 | if (volumeDb > minVolDB) { |
| 4937 | volumeDb = minVolDB; |
| 4938 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4939 | } |
| 4940 | } |
| 4941 | } |
| 4942 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4943 | return volumeDb; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4944 | } |
| 4945 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4946 | status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4947 | int index, |
| 4948 | const sp<AudioOutputDescriptor>& outputDesc, |
| 4949 | audio_devices_t device, |
| 4950 | int delayMs, |
| 4951 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4952 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4953 | // do not change actual stream volume if the stream is muted |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4954 | if (outputDesc->mMuteCount[stream] != 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4955 | ALOGVV("checkAndSetVolume() stream %d muted count %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4956 | stream, outputDesc->mMuteCount[stream]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4957 | return NO_ERROR; |
| 4958 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4959 | audio_policy_forced_cfg_t forceUseForComm = |
| 4960 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4961 | // 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] | 4962 | if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) || |
| 4963 | (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4964 | 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] | 4965 | stream, forceUseForComm); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4966 | return INVALID_OPERATION; |
| 4967 | } |
| 4968 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4969 | if (device == AUDIO_DEVICE_NONE) { |
| 4970 | device = outputDesc->device(); |
| 4971 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4972 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4973 | float volumeDb = computeVolume(stream, index, device); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4974 | if (outputDesc->isFixedVolume(device)) { |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4975 | volumeDb = 0.0f; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4976 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4977 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4978 | outputDesc->setVolume(volumeDb, stream, device, delayMs, force); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4979 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4980 | if (stream == AUDIO_STREAM_VOICE_CALL || |
| 4981 | stream == AUDIO_STREAM_BLUETOOTH_SCO) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4982 | float voiceVolume; |
| 4983 | // 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] | 4984 | if (stream == AUDIO_STREAM_VOICE_CALL) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4985 | voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4986 | } else { |
| 4987 | voiceVolume = 1.0; |
| 4988 | } |
| 4989 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4990 | if (voiceVolume != mLastVoiceVolume && outputDesc == mPrimaryOutput) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4991 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); |
| 4992 | mLastVoiceVolume = voiceVolume; |
| 4993 | } |
| 4994 | } |
| 4995 | |
| 4996 | return NO_ERROR; |
| 4997 | } |
| 4998 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4999 | void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc, |
| 5000 | audio_devices_t device, |
| 5001 | int delayMs, |
| 5002 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5003 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5004 | ALOGVV("applyStreamVolumes() for device %08x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5005 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5006 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5007 | checkAndSetVolume((audio_stream_type_t)stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5008 | mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5009 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5010 | device, |
| 5011 | delayMs, |
| 5012 | force); |
| 5013 | } |
| 5014 | } |
| 5015 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5016 | void AudioPolicyManager::setStrategyMute(routing_strategy strategy, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5017 | bool on, |
| 5018 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5019 | int delayMs, |
| 5020 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5021 | { |
Eric Laurent | 72249d5 | 2015-06-08 11:12:15 -0700 | [diff] [blame] | 5022 | ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d", |
| 5023 | strategy, on, outputDesc->getId()); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5024 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5025 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5026 | setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5027 | } |
| 5028 | } |
| 5029 | } |
| 5030 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5031 | void AudioPolicyManager::setStreamMute(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5032 | bool on, |
| 5033 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5034 | int delayMs, |
| 5035 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5036 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5037 | if (device == AUDIO_DEVICE_NONE) { |
| 5038 | device = outputDesc->device(); |
| 5039 | } |
| 5040 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5041 | ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x", |
| 5042 | stream, on, outputDesc->mMuteCount[stream], device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5043 | |
| 5044 | if (on) { |
| 5045 | if (outputDesc->mMuteCount[stream] == 0) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5046 | if (mVolumeCurves->canBeMuted(stream) && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5047 | ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5048 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5049 | checkAndSetVolume(stream, 0, outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5050 | } |
| 5051 | } |
| 5052 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored |
| 5053 | outputDesc->mMuteCount[stream]++; |
| 5054 | } else { |
| 5055 | if (outputDesc->mMuteCount[stream] == 0) { |
| 5056 | ALOGV("setStreamMute() unmuting non muted stream!"); |
| 5057 | return; |
| 5058 | } |
| 5059 | if (--outputDesc->mMuteCount[stream] == 0) { |
| 5060 | checkAndSetVolume(stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5061 | mVolumeCurves->getVolumeIndex(stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5062 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5063 | device, |
| 5064 | delayMs); |
| 5065 | } |
| 5066 | } |
| 5067 | } |
| 5068 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5069 | void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5070 | bool starting, bool stateChange) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5071 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 5072 | if(!hasPrimaryOutput()) { |
| 5073 | return; |
| 5074 | } |
| 5075 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5076 | // if the stream pertains to sonification strategy and we are in call we must |
| 5077 | // mute the stream if it is low visibility. If it is high visibility, we must play a tone |
| 5078 | // in the device used for phone strategy and play the tone if the selected device does not |
| 5079 | // interfere with the device used for phone strategy |
| 5080 | // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as |
| 5081 | // many times as there are active tracks on the output |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5082 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5083 | if ((stream_strategy == STRATEGY_SONIFICATION) || |
| 5084 | ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5085 | sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5086 | ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d", |
| 5087 | stream, starting, outputDesc->mDevice, stateChange); |
| 5088 | if (outputDesc->mRefCount[stream]) { |
| 5089 | int muteCount = 1; |
| 5090 | if (stateChange) { |
| 5091 | muteCount = outputDesc->mRefCount[stream]; |
| 5092 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5093 | if (audio_is_low_visibility(stream)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5094 | ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount); |
| 5095 | for (int i = 0; i < muteCount; i++) { |
| 5096 | setStreamMute(stream, starting, mPrimaryOutput); |
| 5097 | } |
| 5098 | } else { |
| 5099 | ALOGV("handleIncallSonification() high visibility"); |
| 5100 | if (outputDesc->device() & |
| 5101 | getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) { |
| 5102 | ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount); |
| 5103 | for (int i = 0; i < muteCount; i++) { |
| 5104 | setStreamMute(stream, starting, mPrimaryOutput); |
| 5105 | } |
| 5106 | } |
| 5107 | if (starting) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5108 | mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION, |
| 5109 | AUDIO_STREAM_VOICE_CALL); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5110 | } else { |
| 5111 | mpClientInterface->stopTone(); |
| 5112 | } |
| 5113 | } |
| 5114 | } |
| 5115 | } |
| 5116 | } |
| 5117 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5118 | audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr) |
| 5119 | { |
| 5120 | // flags to stream type mapping |
| 5121 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 5122 | return AUDIO_STREAM_ENFORCED_AUDIBLE; |
| 5123 | } |
| 5124 | if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) { |
| 5125 | return AUDIO_STREAM_BLUETOOTH_SCO; |
| 5126 | } |
Jean-Michel Trivi | 79ad438 | 2015-01-29 10:49:39 -0800 | [diff] [blame] | 5127 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 5128 | return AUDIO_STREAM_TTS; |
| 5129 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5130 | |
| 5131 | // usage to stream type mapping |
| 5132 | switch (attr->usage) { |
| 5133 | case AUDIO_USAGE_MEDIA: |
| 5134 | case AUDIO_USAGE_GAME: |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5135 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 5136 | return AUDIO_STREAM_MUSIC; |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5137 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 5138 | return AUDIO_STREAM_ACCESSIBILITY; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5139 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 5140 | return AUDIO_STREAM_SYSTEM; |
| 5141 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 5142 | return AUDIO_STREAM_VOICE_CALL; |
| 5143 | |
| 5144 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 5145 | return AUDIO_STREAM_DTMF; |
| 5146 | |
| 5147 | case AUDIO_USAGE_ALARM: |
| 5148 | return AUDIO_STREAM_ALARM; |
| 5149 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 5150 | return AUDIO_STREAM_RING; |
| 5151 | |
| 5152 | case AUDIO_USAGE_NOTIFICATION: |
| 5153 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 5154 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 5155 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 5156 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 5157 | return AUDIO_STREAM_NOTIFICATION; |
| 5158 | |
| 5159 | case AUDIO_USAGE_UNKNOWN: |
| 5160 | default: |
| 5161 | return AUDIO_STREAM_MUSIC; |
| 5162 | } |
| 5163 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5164 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5165 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) |
| 5166 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5167 | // has flags that map to a strategy? |
| 5168 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { |
| 5169 | return true; |
| 5170 | } |
| 5171 | |
| 5172 | // has known usage? |
| 5173 | switch (paa->usage) { |
| 5174 | case AUDIO_USAGE_UNKNOWN: |
| 5175 | case AUDIO_USAGE_MEDIA: |
| 5176 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 5177 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 5178 | case AUDIO_USAGE_ALARM: |
| 5179 | case AUDIO_USAGE_NOTIFICATION: |
| 5180 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 5181 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 5182 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 5183 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 5184 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 5185 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 5186 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 5187 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 5188 | case AUDIO_USAGE_GAME: |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5189 | case AUDIO_USAGE_VIRTUAL_SOURCE: |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5190 | break; |
| 5191 | default: |
| 5192 | return false; |
| 5193 | } |
| 5194 | return true; |
| 5195 | } |
| 5196 | |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5197 | bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc, |
| 5198 | routing_strategy strategy, uint32_t inPastMs, |
| 5199 | nsecs_t sysTime) const |
| 5200 | { |
| 5201 | if ((sysTime == 0) && (inPastMs != 0)) { |
| 5202 | sysTime = systemTime(); |
| 5203 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5204 | for (int i = 0; i < (int)AUDIO_STREAM_FOR_POLICY_CNT; i++) { |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5205 | if (((getStrategy((audio_stream_type_t)i) == strategy) || |
| 5206 | (NUM_STRATEGIES == strategy)) && |
| 5207 | outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) { |
| 5208 | return true; |
| 5209 | } |
| 5210 | } |
| 5211 | return false; |
| 5212 | } |
| 5213 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5214 | audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage) |
| 5215 | { |
| 5216 | return mEngine->getForceUse(usage); |
| 5217 | } |
| 5218 | |
| 5219 | bool AudioPolicyManager::isInCall() |
| 5220 | { |
| 5221 | return isStateInCall(mEngine->getPhoneState()); |
| 5222 | } |
| 5223 | |
| 5224 | bool AudioPolicyManager::isStateInCall(int state) |
| 5225 | { |
| 5226 | return is_state_in_call(state); |
| 5227 | } |
| 5228 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5229 | void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc) |
| 5230 | { |
| 5231 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
| 5232 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 5233 | if (sourceDesc->mDevice->equals(deviceDesc)) { |
| 5234 | ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle()); |
| 5235 | stopAudioSource(sourceDesc->getHandle()); |
| 5236 | } |
| 5237 | } |
| 5238 | |
| 5239 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
| 5240 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 5241 | bool release = false; |
| 5242 | for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) { |
| 5243 | const struct audio_port_config *source = &patchDesc->mPatch.sources[j]; |
| 5244 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 5245 | source->ext.device.type == deviceDesc->type()) { |
| 5246 | release = true; |
| 5247 | } |
| 5248 | } |
| 5249 | for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) { |
| 5250 | const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j]; |
| 5251 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && |
| 5252 | sink->ext.device.type == deviceDesc->type()) { |
| 5253 | release = true; |
| 5254 | } |
| 5255 | } |
| 5256 | if (release) { |
| 5257 | ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle); |
| 5258 | releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid); |
| 5259 | } |
| 5260 | } |
| 5261 | } |
| 5262 | |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5263 | // Modify the list of surround sound formats supported. |
| 5264 | void AudioPolicyManager::filterSurroundFormats(FormatVector &formats) { |
| 5265 | |
| 5266 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5267 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
| 5268 | ALOGI("%s: forced use = %d", __FUNCTION__, forceUse); |
| 5269 | |
| 5270 | // Analyze original support for various formats. |
| 5271 | bool supportsRawSurround = false; |
| 5272 | bool supportsIEC61937 = false; |
| 5273 | for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) { |
| 5274 | audio_format_t format = formats[formatIndex]; |
| 5275 | ALOGI("%s: original formats: #%x", __FUNCTION__, format); |
| 5276 | switch (format) { |
| 5277 | case AUDIO_FORMAT_AC3: |
| 5278 | case AUDIO_FORMAT_E_AC3: |
| 5279 | case AUDIO_FORMAT_DTS: |
| 5280 | case AUDIO_FORMAT_DTS_HD: |
| 5281 | supportsRawSurround = true; |
| 5282 | break; |
| 5283 | case AUDIO_FORMAT_IEC61937: |
| 5284 | supportsIEC61937 = true; |
| 5285 | break; |
| 5286 | default: |
| 5287 | break; |
| 5288 | } |
| 5289 | } |
| 5290 | ALOGI("%s: supportsRawSurround = %d, supportsIEC61937 = %d", |
| 5291 | __FUNCTION__, supportsRawSurround, supportsIEC61937); |
| 5292 | |
| 5293 | // Modify formats based on surround preferences. |
| 5294 | // If NEVER, remove support for surround formats. |
| 5295 | if ((forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) |
| 5296 | && (supportsRawSurround || supportsIEC61937)) { |
| 5297 | // Remove surround sound related formats. |
| 5298 | for (size_t formatIndex = 0; formatIndex < formats.size(); ) { |
| 5299 | audio_format_t format = formats[formatIndex]; |
| 5300 | switch(format) { |
| 5301 | case AUDIO_FORMAT_AC3: |
| 5302 | case AUDIO_FORMAT_E_AC3: |
| 5303 | case AUDIO_FORMAT_DTS: |
| 5304 | case AUDIO_FORMAT_DTS_HD: |
| 5305 | case AUDIO_FORMAT_IEC61937: |
| 5306 | ALOGI("%s: remove #%x", __FUNCTION__, format); |
| 5307 | formats.removeAt(formatIndex); |
| 5308 | break; |
| 5309 | default: |
| 5310 | formatIndex++; // keep it |
| 5311 | break; |
| 5312 | } |
| 5313 | } |
| 5314 | supportsRawSurround = false; |
| 5315 | supportsIEC61937 = false; |
| 5316 | } |
| 5317 | // If ALWAYS, add support for raw surround formats if all are missing. |
| 5318 | // This assumes that if any of these formats are reported by the HAL |
| 5319 | // then the report is valid and should not be modified. |
| 5320 | if ((forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) |
| 5321 | && !supportsRawSurround) { |
| 5322 | formats.add(AUDIO_FORMAT_AC3); |
| 5323 | formats.add(AUDIO_FORMAT_E_AC3); |
| 5324 | formats.add(AUDIO_FORMAT_DTS); |
| 5325 | formats.add(AUDIO_FORMAT_DTS_HD); |
| 5326 | supportsRawSurround = true; |
| 5327 | } |
| 5328 | // Add support for IEC61937 if raw surround supported. |
| 5329 | // The HAL could do this but add it here, just in case. |
| 5330 | if (supportsRawSurround && !supportsIEC61937) { |
| 5331 | formats.add(AUDIO_FORMAT_IEC61937); |
| 5332 | // supportsIEC61937 = true; |
| 5333 | } |
| 5334 | // Just for debugging. |
| 5335 | for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) { |
| 5336 | audio_format_t format = formats[formatIndex]; |
| 5337 | ALOGI("%s: final formats: #%x", __FUNCTION__, format); |
| 5338 | } |
| 5339 | } |
| 5340 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5341 | void AudioPolicyManager::updateAudioProfiles(audio_io_handle_t ioHandle, |
| 5342 | AudioProfileVector &profiles) |
| 5343 | { |
| 5344 | String8 reply; |
| 5345 | char *value; |
| 5346 | // Format MUST be checked first to update the list of AudioProfile |
| 5347 | if (profiles.hasDynamicFormat()) { |
| 5348 | reply = mpClientInterface->getParameters(ioHandle, |
| 5349 | String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS)); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5350 | ALOGI("%s: supported formats %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5351 | AudioParameter repliedParameters(reply); |
| 5352 | if (repliedParameters.get( |
| 5353 | String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS), reply) != NO_ERROR) { |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5354 | ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__); |
| 5355 | return; |
| 5356 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5357 | FormatVector formats = formatsFromString(reply.string()); |
| 5358 | filterSurroundFormats(formats); |
| 5359 | profiles.setFormats(formats); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5360 | } |
| 5361 | const FormatVector &supportedFormats = profiles.getSupportedFormats(); |
| 5362 | |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5363 | for (size_t formatIndex = 0; formatIndex < supportedFormats.size(); formatIndex++) { |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5364 | audio_format_t format = supportedFormats[formatIndex]; |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5365 | ChannelsVector channelMasks; |
| 5366 | SampleRateVector samplingRates; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5367 | AudioParameter requestedParameters; |
| 5368 | requestedParameters.addInt(String8(AUDIO_PARAMETER_STREAM_FORMAT), format); |
| 5369 | |
| 5370 | if (profiles.hasDynamicRateFor(format)) { |
| 5371 | reply = mpClientInterface->getParameters(ioHandle, |
| 5372 | requestedParameters.toString() + ";" + |
| 5373 | AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES); |
| 5374 | ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5375 | AudioParameter repliedParameters(reply); |
| 5376 | if (repliedParameters.get( |
| 5377 | String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES), reply) == NO_ERROR) { |
| 5378 | samplingRates = samplingRatesFromString(reply.string()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5379 | } |
| 5380 | } |
| 5381 | if (profiles.hasDynamicChannelsFor(format)) { |
| 5382 | reply = mpClientInterface->getParameters(ioHandle, |
| 5383 | requestedParameters.toString() + ";" + |
| 5384 | AUDIO_PARAMETER_STREAM_SUP_CHANNELS); |
| 5385 | ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5386 | AudioParameter repliedParameters(reply); |
| 5387 | if (repliedParameters.get( |
| 5388 | String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS), reply) == NO_ERROR) { |
| 5389 | channelMasks = channelMasksFromString(reply.string()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5390 | } |
| 5391 | } |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5392 | profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates)); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5393 | } |
| 5394 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5395 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5396 | }; // namespace android |