Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 17 | #define LOG_TAG "APM_AudioPolicyManager" |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
| 19 | |
| 20 | //#define VERY_VERBOSE_LOGGING |
| 21 | #ifdef VERY_VERBOSE_LOGGING |
| 22 | #define ALOGVV ALOGV |
| 23 | #else |
| 24 | #define ALOGVV(a...) do { } while(0) |
| 25 | #endif |
| 26 | |
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 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 52 | //FIXME: workaround for truncated touch sounds |
| 53 | // to be removed when the problem is handled by system UI |
| 54 | #define TOUCH_SOUND_FIXED_DELAY_MS 100 |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 55 | // ---------------------------------------------------------------------------- |
| 56 | // AudioPolicyInterface implementation |
| 57 | // ---------------------------------------------------------------------------- |
| 58 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 59 | status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 60 | audio_policy_dev_state_t state, |
| 61 | const char *device_address, |
| 62 | const char *device_name) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 63 | { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 64 | return setDeviceConnectionStateInt(device, state, device_address, device_name); |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device, |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 68 | audio_policy_dev_state_t state, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 69 | const char *device_address, |
| 70 | const char *device_name) |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 71 | { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 72 | ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s", |
| 73 | - device, state, device_address, device_name); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 74 | |
| 75 | // connect/disconnect only 1 device at a time |
| 76 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; |
| 77 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 78 | sp<DeviceDescriptor> devDesc = |
| 79 | mHwModules.getDeviceDescriptor(device, device_address, device_name); |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 80 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 81 | // handle output devices |
| 82 | if (audio_is_output_device(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 83 | SortedVector <audio_io_handle_t> outputs; |
| 84 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 85 | ssize_t index = mAvailableOutputDevices.indexOf(devDesc); |
| 86 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 87 | // save a copy of the opened output descriptors before any output is opened or closed |
| 88 | // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies() |
| 89 | mPreviousOutputs = mOutputs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 90 | switch (state) |
| 91 | { |
| 92 | // handle output device connection |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 93 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 94 | if (index >= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 95 | ALOGW("setDeviceConnectionState() device already connected: %x", device); |
| 96 | return INVALID_OPERATION; |
| 97 | } |
| 98 | ALOGV("setDeviceConnectionState() connecting device %x", device); |
| 99 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 100 | // register new device as available |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 101 | index = mAvailableOutputDevices.add(devDesc); |
| 102 | if (index >= 0) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 103 | sp<HwModule> module = mHwModules.getModuleForDevice(device); |
Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 104 | if (module == 0) { |
| 105 | ALOGD("setDeviceConnectionState() could not find HW module for device %08x", |
| 106 | device); |
| 107 | mAvailableOutputDevices.remove(devDesc); |
| 108 | return INVALID_OPERATION; |
| 109 | } |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 110 | mAvailableOutputDevices[index]->attach(module); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 111 | } else { |
| 112 | return NO_MEMORY; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 115 | if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 116 | mAvailableOutputDevices.remove(devDesc); |
| 117 | return INVALID_OPERATION; |
| 118 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 119 | // Propagate device availability to Engine |
| 120 | mEngine->setDeviceConnectionState(devDesc, state); |
| 121 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 122 | // outputs should never be empty here |
| 123 | ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():" |
| 124 | "checkOutputsForDevice() returned no outputs but status OK"); |
| 125 | ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs", |
| 126 | outputs.size()); |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 127 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 128 | // Send connect to HALs |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 129 | AudioParameter param = AudioParameter(devDesc->mAddress); |
| 130 | param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device); |
| 131 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 132 | |
| 133 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 134 | // handle output device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 135 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 136 | if (index < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 137 | ALOGW("setDeviceConnectionState() device not connected: %x", device); |
| 138 | return INVALID_OPERATION; |
| 139 | } |
| 140 | |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 141 | ALOGV("setDeviceConnectionState() disconnecting output device %x", device); |
| 142 | |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 143 | // Send Disconnect to HALs |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 144 | AudioParameter param = AudioParameter(devDesc->mAddress); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 145 | param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device); |
| 146 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 147 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 148 | // remove device from available output devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 149 | mAvailableOutputDevices.remove(devDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 150 | |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 151 | checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 152 | |
| 153 | // Propagate device availability to Engine |
| 154 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 155 | } break; |
| 156 | |
| 157 | default: |
| 158 | ALOGE("setDeviceConnectionState() invalid state: %x", state); |
| 159 | return BAD_VALUE; |
| 160 | } |
| 161 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 162 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP |
| 163 | // output is suspended before any tracks are moved to it |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 164 | checkA2dpSuspend(); |
| 165 | checkOutputForAllStrategies(); |
| 166 | // outputs must be closed after checkOutputForAllStrategies() is executed |
| 167 | if (!outputs.isEmpty()) { |
| 168 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 169 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 170 | // close unused outputs after device disconnection or direct outputs that have been |
| 171 | // opened by checkOutputsForDevice() to query dynamic parameters |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 172 | if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 173 | (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) && |
| 174 | (desc->mDirectOpenCount == 0))) { |
| 175 | closeOutput(outputs[i]); |
| 176 | } |
| 177 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 178 | // check again after closing A2DP output to reset mA2dpSuspended if needed |
| 179 | checkA2dpSuspend(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | updateDevicesAndOutputs(); |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 183 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 184 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 185 | updateCallRouting(newDevice); |
| 186 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 187 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 188 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 189 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) { |
| 190 | audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 191 | // do not force device change on duplicated output because if device is 0, it will |
| 192 | // also force a device 0 for the two outputs it is duplicated to which may override |
| 193 | // a valid device selection on those outputs. |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 194 | bool force = !desc->isDuplicated() |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 195 | && (!device_distinguishes_on_address(device) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 196 | // always force when disconnecting (a non-duplicated device) |
| 197 | || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 198 | setOutputDevice(desc, newDevice, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 199 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 202 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
| 203 | cleanUpForDevice(devDesc); |
| 204 | } |
| 205 | |
Eric Laurent | 72aa32f | 2014-05-30 18:51:48 -0700 | [diff] [blame] | 206 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | b71e58b | 2014-05-29 16:08:11 -0700 | [diff] [blame] | 207 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 208 | } // end if is output device |
| 209 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 210 | // handle input devices |
| 211 | if (audio_is_input_device(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 212 | SortedVector <audio_io_handle_t> inputs; |
| 213 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 214 | ssize_t index = mAvailableInputDevices.indexOf(devDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 215 | switch (state) |
| 216 | { |
| 217 | // handle input device connection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 218 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 219 | if (index >= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 220 | ALOGW("setDeviceConnectionState() device already connected: %d", device); |
| 221 | return INVALID_OPERATION; |
| 222 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 223 | sp<HwModule> module = mHwModules.getModuleForDevice(device); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 224 | if (module == NULL) { |
| 225 | ALOGW("setDeviceConnectionState(): could not find HW module for device %08x", |
| 226 | device); |
| 227 | return INVALID_OPERATION; |
| 228 | } |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 229 | if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 230 | return INVALID_OPERATION; |
| 231 | } |
| 232 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 233 | index = mAvailableInputDevices.add(devDesc); |
| 234 | if (index >= 0) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 235 | mAvailableInputDevices[index]->attach(module); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 236 | } else { |
| 237 | return NO_MEMORY; |
| 238 | } |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 239 | |
| 240 | // Set connect to HALs |
| 241 | AudioParameter param = AudioParameter(devDesc->mAddress); |
| 242 | param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device); |
| 243 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 244 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 245 | // Propagate device availability to Engine |
| 246 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 247 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 248 | |
| 249 | // handle input device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 250 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 251 | if (index < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 252 | ALOGW("setDeviceConnectionState() device not connected: %d", device); |
| 253 | return INVALID_OPERATION; |
| 254 | } |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 255 | |
| 256 | ALOGV("setDeviceConnectionState() disconnecting input device %x", device); |
| 257 | |
| 258 | // Set Disconnect to HALs |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 259 | AudioParameter param = AudioParameter(devDesc->mAddress); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 260 | param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device); |
| 261 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 262 | |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 263 | checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 264 | mAvailableInputDevices.remove(devDesc); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 265 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 266 | // Propagate device availability to Engine |
| 267 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 268 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 269 | |
| 270 | default: |
| 271 | ALOGE("setDeviceConnectionState() invalid state: %x", state); |
| 272 | return BAD_VALUE; |
| 273 | } |
| 274 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 275 | closeAllInputs(); |
Eric Laurent | 5f5fca5 | 2016-08-04 11:48:57 -0700 | [diff] [blame] | 276 | // As the input device list can impact the output device selection, update |
| 277 | // getDeviceForStrategy() cache |
| 278 | updateDevicesAndOutputs(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 279 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 280 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 281 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 282 | updateCallRouting(newDevice); |
| 283 | } |
| 284 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 285 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
| 286 | cleanUpForDevice(devDesc); |
| 287 | } |
| 288 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 289 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 290 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 291 | } // end if is input device |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 292 | |
| 293 | ALOGW("setDeviceConnectionState() invalid device: %x", device); |
| 294 | return BAD_VALUE; |
| 295 | } |
| 296 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 297 | audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 298 | const char *device_address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 299 | { |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 300 | sp<DeviceDescriptor> devDesc = |
| 301 | mHwModules.getDeviceDescriptor(device, device_address, "", |
| 302 | (strlen(device_address) != 0)/*matchAddress*/); |
| 303 | |
| 304 | if (devDesc == 0) { |
| 305 | ALOGW("getDeviceConnectionState() undeclared device, type %08x, address: %s", |
| 306 | device, device_address); |
| 307 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
| 308 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 309 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 310 | DeviceVector *deviceVector; |
| 311 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 312 | if (audio_is_output_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 313 | deviceVector = &mAvailableOutputDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 314 | } else if (audio_is_input_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 315 | deviceVector = &mAvailableInputDevices; |
| 316 | } else { |
| 317 | ALOGW("getDeviceConnectionState() invalid device type %08x", device); |
| 318 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 319 | } |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 320 | |
| 321 | return (deviceVector->getDevice(device, String8(device_address)) != 0) ? |
| 322 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 323 | } |
| 324 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 325 | uint32_t AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, uint32_t delayMs) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 326 | { |
| 327 | bool createTxPatch = false; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 328 | status_t status; |
| 329 | audio_patch_handle_t afPatchHandle; |
| 330 | DeviceVector deviceList; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 331 | uint32_t muteWaitMs = 0; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 332 | |
Andy Hung | a76c7de | 2016-12-13 19:14:31 -0800 | [diff] [blame^] | 333 | if(!hasPrimaryOutput() || mPrimaryOutput->device() == AUDIO_DEVICE_OUT_STUB) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 334 | return muteWaitMs; |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 335 | } |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 336 | audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 337 | ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice); |
| 338 | |
| 339 | // release existing RX patch if any |
| 340 | if (mCallRxPatch != 0) { |
| 341 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 342 | mCallRxPatch.clear(); |
| 343 | } |
| 344 | // release TX patch if any |
| 345 | if (mCallTxPatch != 0) { |
| 346 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 347 | mCallTxPatch.clear(); |
| 348 | } |
| 349 | |
| 350 | // If the RX device is on the primary HW module, then use legacy routing method for voice calls |
| 351 | // via setOutputDevice() on primary output. |
| 352 | // Otherwise, create two audio patches for TX and RX path. |
| 353 | if (availablePrimaryOutputDevices() & rxDevice) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 354 | muteWaitMs = setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 355 | // If the TX device is also on the primary HW module, setOutputDevice() will take care |
| 356 | // of it due to legacy implementation. If not, create a patch. |
| 357 | if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN) |
| 358 | == AUDIO_DEVICE_NONE) { |
| 359 | createTxPatch = true; |
| 360 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 361 | } else { // create RX path audio patch |
| 362 | struct audio_patch patch; |
| 363 | |
| 364 | patch.num_sources = 1; |
| 365 | patch.num_sinks = 1; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 366 | deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice); |
| 367 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 368 | "updateCallRouting() selected device not in output device list"); |
| 369 | sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0); |
| 370 | deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX); |
| 371 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 372 | "updateCallRouting() no telephony RX device"); |
| 373 | sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0); |
| 374 | |
| 375 | rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]); |
| 376 | rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]); |
| 377 | |
| 378 | // request to reuse existing output stream if one is already opened to reach the RX device |
| 379 | SortedVector<audio_io_handle_t> outputs = |
| 380 | getOutputsForDevice(rxDevice, mOutputs); |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 381 | audio_io_handle_t output = selectOutput(outputs, |
| 382 | AUDIO_OUTPUT_FLAG_NONE, |
| 383 | AUDIO_FORMAT_INVALID); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 384 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 385 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 386 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 387 | "updateCallRouting() RX device output is duplicated"); |
| 388 | outputDesc->toAudioPortConfig(&patch.sources[1]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 389 | patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 390 | patch.num_sources = 2; |
| 391 | } |
| 392 | |
| 393 | afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 394 | status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 395 | ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch", |
| 396 | status); |
| 397 | if (status == NO_ERROR) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 398 | mCallRxPatch = new AudioPatch(&patch, mUidCached); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 399 | mCallRxPatch->mAfPatchHandle = afPatchHandle; |
| 400 | mCallRxPatch->mUid = mUidCached; |
| 401 | } |
| 402 | createTxPatch = true; |
| 403 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 404 | if (createTxPatch) { // create TX path audio patch |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 405 | struct audio_patch patch; |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 406 | |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 407 | patch.num_sources = 1; |
| 408 | patch.num_sinks = 1; |
| 409 | deviceList = mAvailableInputDevices.getDevicesFromType(txDevice); |
| 410 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 411 | "updateCallRouting() selected device not in input device list"); |
| 412 | sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0); |
| 413 | txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]); |
| 414 | deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX); |
| 415 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 416 | "updateCallRouting() no telephony TX device"); |
| 417 | sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0); |
| 418 | txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]); |
| 419 | |
| 420 | SortedVector<audio_io_handle_t> outputs = |
| 421 | getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs); |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 422 | audio_io_handle_t output = selectOutput(outputs, |
| 423 | AUDIO_OUTPUT_FLAG_NONE, |
| 424 | AUDIO_FORMAT_INVALID); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 425 | // request to reuse existing output stream if one is already opened to reach the TX |
| 426 | // path output device |
| 427 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 428 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 429 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 430 | "updateCallRouting() RX device output is duplicated"); |
| 431 | outputDesc->toAudioPortConfig(&patch.sources[1]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 432 | patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 433 | patch.num_sources = 2; |
| 434 | } |
| 435 | |
Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 436 | // terminate active capture if on the same HW module as the call TX source device |
| 437 | // FIXME: would be better to refine to only inputs whose profile connects to the |
| 438 | // call TX device but this information is not in the audio patch and logic here must be |
| 439 | // symmetric to the one in startInput() |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 440 | audio_io_handle_t activeInput = mInputs.getActiveInput(); |
| 441 | if (activeInput != 0) { |
| 442 | sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput); |
| 443 | if (activeDesc->getModuleHandle() == txSourceDeviceDesc->getModuleHandle()) { |
| 444 | //FIXME: consider all active sessions |
| 445 | AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions(); |
| 446 | audio_session_t activeSession = activeSessions.keyAt(0); |
| 447 | stopInput(activeInput, activeSession); |
| 448 | releaseInput(activeInput, activeSession); |
Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 452 | afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 453 | status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 454 | ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch", |
| 455 | status); |
| 456 | if (status == NO_ERROR) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 457 | mCallTxPatch = new AudioPatch(&patch, mUidCached); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 458 | mCallTxPatch->mAfPatchHandle = afPatchHandle; |
| 459 | mCallTxPatch->mUid = mUidCached; |
| 460 | } |
| 461 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 462 | |
| 463 | return muteWaitMs; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 466 | void AudioPolicyManager::setPhoneState(audio_mode_t state) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 467 | { |
| 468 | ALOGV("setPhoneState() state %d", state); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 469 | // store previous phone state for management of sonification strategy below |
| 470 | int oldState = mEngine->getPhoneState(); |
| 471 | |
| 472 | if (mEngine->setPhoneState(state) != NO_ERROR) { |
| 473 | ALOGW("setPhoneState() invalid or same state %d", state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 474 | return; |
| 475 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 476 | /// Opens: can these line be executed after the switch of volume curves??? |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 477 | // if leaving call state, handle special case of active streams |
| 478 | // pertaining to sonification strategy see handleIncallSonification() |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 479 | if (isStateInCall(oldState)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 480 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 481 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 482 | handleIncallSonification((audio_stream_type_t)stream, false, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 483 | } |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 484 | |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 485 | // force reevaluating accessibility routing when call stops |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 486 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 487 | } |
| 488 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 489 | /** |
| 490 | * Switching to or from incall state or switching between telephony and VoIP lead to force |
| 491 | * routing command. |
| 492 | */ |
| 493 | bool force = ((is_state_in_call(oldState) != is_state_in_call(state)) |
| 494 | || (is_state_in_call(state) && (state != oldState))); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 495 | |
| 496 | // check for device and output changes triggered by new phone state |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 497 | checkA2dpSuspend(); |
| 498 | checkOutputForAllStrategies(); |
| 499 | updateDevicesAndOutputs(); |
| 500 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 501 | int delayMs = 0; |
| 502 | if (isStateInCall(state)) { |
| 503 | nsecs_t sysTime = systemTime(); |
| 504 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 505 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 506 | // mute media and sonification strategies and delay device switch by the largest |
| 507 | // latency of any output where either strategy is active. |
| 508 | // 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] | 509 | if ((isStrategyActive(desc, STRATEGY_MEDIA, |
| 510 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 511 | sysTime) || |
| 512 | isStrategyActive(desc, STRATEGY_SONIFICATION, |
| 513 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 514 | sysTime)) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 515 | (delayMs < (int)desc->latency()*2)) { |
| 516 | delayMs = desc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 517 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 518 | setStrategyMute(STRATEGY_MEDIA, true, desc); |
| 519 | setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 520 | getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/)); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 521 | setStrategyMute(STRATEGY_SONIFICATION, true, desc); |
| 522 | setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 523 | getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/)); |
| 524 | } |
| 525 | } |
| 526 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 527 | if (hasPrimaryOutput()) { |
| 528 | // Note that despite the fact that getNewOutputDevice() is called on the primary output, |
| 529 | // the device returned is not necessarily reachable via this output |
| 530 | audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 531 | // force routing command to audio hardware when ending call |
| 532 | // even if no device change is needed |
| 533 | if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) { |
| 534 | rxDevice = mPrimaryOutput->device(); |
| 535 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 536 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 537 | if (state == AUDIO_MODE_IN_CALL) { |
| 538 | updateCallRouting(rxDevice, delayMs); |
| 539 | } else if (oldState == AUDIO_MODE_IN_CALL) { |
| 540 | if (mCallRxPatch != 0) { |
| 541 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 542 | mCallRxPatch.clear(); |
| 543 | } |
| 544 | if (mCallTxPatch != 0) { |
| 545 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 546 | mCallTxPatch.clear(); |
| 547 | } |
| 548 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
| 549 | } else { |
| 550 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 551 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 552 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 553 | // if entering in call state, handle special case of active streams |
| 554 | // pertaining to sonification strategy see handleIncallSonification() |
| 555 | if (isStateInCall(state)) { |
| 556 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 557 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 558 | handleIncallSonification((audio_stream_type_t)stream, true, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 559 | } |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 560 | |
| 561 | // force reevaluating accessibility routing when call starts |
| 562 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | // 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] | 566 | if (state == AUDIO_MODE_RINGTONE && |
| 567 | isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 568 | mLimitRingtoneVolume = true; |
| 569 | } else { |
| 570 | mLimitRingtoneVolume = false; |
| 571 | } |
| 572 | } |
| 573 | |
Jean-Michel Trivi | 887a9ed | 2015-03-31 18:02:24 -0700 | [diff] [blame] | 574 | audio_mode_t AudioPolicyManager::getPhoneState() { |
| 575 | return mEngine->getPhoneState(); |
| 576 | } |
| 577 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 578 | void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 579 | audio_policy_forced_cfg_t config) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 580 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 581 | ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 582 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 583 | if (mEngine->setForceUse(usage, config) != NO_ERROR) { |
| 584 | ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage); |
| 585 | return; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 586 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 587 | bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) || |
| 588 | (usage == AUDIO_POLICY_FORCE_FOR_DOCK) || |
| 589 | (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 590 | |
| 591 | // check for device and output changes triggered by new force usage |
| 592 | checkA2dpSuspend(); |
| 593 | checkOutputForAllStrategies(); |
| 594 | updateDevicesAndOutputs(); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 595 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 596 | //FIXME: workaround for truncated touch sounds |
| 597 | // to be removed when the problem is handled by system UI |
| 598 | uint32_t delayMs = 0; |
| 599 | uint32_t waitMs = 0; |
| 600 | if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) { |
| 601 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 602 | } |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 603 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 604 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/); |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 605 | waitMs = updateCallRouting(newDevice, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 606 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 607 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 608 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
| 609 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/); |
| 610 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 611 | waitMs = setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE), |
| 612 | delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 613 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 614 | if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 615 | applyStreamVolumes(outputDesc, newDevice, waitMs, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 616 | } |
| 617 | } |
| 618 | |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 619 | audio_io_handle_t activeInput = mInputs.getActiveInput(); |
| 620 | if (activeInput != 0) { |
| 621 | sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput); |
| 622 | audio_devices_t newDevice = getNewInputDevice(activeInput); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 623 | // Force new input selection if the new device can not be reached via current input |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 624 | if (activeDesc->mProfile->getSupportedDevices().types() & (newDevice & ~AUDIO_DEVICE_BIT_IN)) { |
| 625 | setInputDevice(activeInput, newDevice); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 626 | } else { |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 627 | closeInput(activeInput); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 628 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 629 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 630 | } |
| 631 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 632 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 633 | { |
| 634 | ALOGV("setSystemProperty() property %s, value %s", property, value); |
| 635 | } |
| 636 | |
| 637 | // Find a direct output profile compatible with the parameters passed, even if the input flags do |
| 638 | // not explicitly request a direct output |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 639 | sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput( |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 640 | audio_devices_t device, |
| 641 | uint32_t samplingRate, |
| 642 | audio_format_t format, |
| 643 | audio_channel_mask_t channelMask, |
| 644 | audio_output_flags_t flags) |
| 645 | { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 646 | // only retain flags that will drive the direct output profile selection |
| 647 | // if explicitly requested |
| 648 | static const uint32_t kRelevantFlags = |
| 649 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
| 650 | flags = |
| 651 | (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); |
| 652 | |
| 653 | sp<IOProfile> profile; |
| 654 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 655 | for (size_t i = 0; i < mHwModules.size(); i++) { |
| 656 | if (mHwModules[i]->mHandle == 0) { |
| 657 | continue; |
| 658 | } |
| 659 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 660 | sp<IOProfile> curProfile = mHwModules[i]->mOutputProfiles[j]; |
| 661 | if (!curProfile->isCompatibleProfile(device, String8(""), |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 662 | samplingRate, NULL /*updatedSamplingRate*/, |
| 663 | format, NULL /*updatedFormat*/, |
| 664 | channelMask, NULL /*updatedChannelMask*/, |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 665 | flags)) { |
| 666 | continue; |
| 667 | } |
| 668 | // reject profiles not corresponding to a device currently available |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 669 | if ((mAvailableOutputDevices.types() & curProfile->getSupportedDevicesType()) == 0) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 670 | continue; |
| 671 | } |
| 672 | // 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] | 673 | if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 674 | continue; |
| 675 | } |
| 676 | profile = curProfile; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 677 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 678 | break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 679 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 680 | } |
| 681 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 682 | return profile; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 683 | } |
| 684 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 685 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 686 | uint32_t samplingRate, |
| 687 | audio_format_t format, |
| 688 | audio_channel_mask_t channelMask, |
| 689 | audio_output_flags_t flags, |
| 690 | const audio_offload_info_t *offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 691 | { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 692 | routing_strategy strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 693 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 694 | ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x", |
| 695 | device, stream, samplingRate, format, channelMask, flags); |
| 696 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 697 | return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE, |
| 698 | stream, samplingRate,format, channelMask, |
| 699 | flags, offloadInfo); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 700 | } |
| 701 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 702 | status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, |
| 703 | audio_io_handle_t *output, |
| 704 | audio_session_t session, |
| 705 | audio_stream_type_t *stream, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 706 | uid_t uid, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 707 | uint32_t samplingRate, |
| 708 | audio_format_t format, |
| 709 | audio_channel_mask_t channelMask, |
| 710 | audio_output_flags_t flags, |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 711 | audio_port_handle_t selectedDeviceId, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 712 | const audio_offload_info_t *offloadInfo) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 713 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 714 | audio_attributes_t attributes; |
| 715 | if (attr != NULL) { |
| 716 | if (!isValidAttributes(attr)) { |
| 717 | ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]", |
| 718 | attr->usage, attr->content_type, attr->flags, |
| 719 | attr->tags); |
| 720 | return BAD_VALUE; |
| 721 | } |
| 722 | attributes = *attr; |
| 723 | } else { |
| 724 | if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) { |
| 725 | ALOGE("getOutputForAttr(): invalid stream type"); |
| 726 | return BAD_VALUE; |
| 727 | } |
| 728 | stream_type_to_audio_attributes(*stream, &attributes); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 729 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 730 | sp<SwAudioOutputDescriptor> desc; |
Jean-Michel Trivi | e8deced | 2016-02-11 12:50:39 -0800 | [diff] [blame] | 731 | if (mPolicyMixes.getOutputForAttr(attributes, uid, desc) == NO_ERROR) { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 732 | ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr"); |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 733 | if (!audio_has_proportional_frames(format)) { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 734 | return BAD_VALUE; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 735 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 736 | *stream = streamTypefromAttributesInt(&attributes); |
| 737 | *output = desc->mIoHandle; |
| 738 | ALOGV("getOutputForAttr() returns output %d", *output); |
| 739 | return NO_ERROR; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 740 | } |
| 741 | if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 742 | ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE"); |
| 743 | return BAD_VALUE; |
| 744 | } |
| 745 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 746 | ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x" |
| 747 | " session %d selectedDeviceId %d", |
| 748 | attributes.usage, attributes.content_type, attributes.tags, attributes.flags, |
| 749 | session, selectedDeviceId); |
| 750 | |
| 751 | *stream = streamTypefromAttributesInt(&attributes); |
| 752 | |
| 753 | // Explicit routing? |
| 754 | sp<DeviceDescriptor> deviceDesc; |
| 755 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { |
| 756 | if (mAvailableOutputDevices[i]->getId() == selectedDeviceId) { |
| 757 | deviceDesc = mAvailableOutputDevices[i]; |
| 758 | break; |
| 759 | } |
| 760 | } |
| 761 | mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 762 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 763 | routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 764 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 765 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 766 | if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) { |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 767 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
| 768 | } |
| 769 | |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 770 | 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] | 771 | device, samplingRate, format, channelMask, flags); |
| 772 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 773 | *output = getOutputForDevice(device, session, *stream, |
| 774 | samplingRate, format, channelMask, |
| 775 | flags, offloadInfo); |
| 776 | if (*output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 777 | mOutputRoutes.removeRoute(session); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 778 | return INVALID_OPERATION; |
| 779 | } |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 780 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 781 | return NO_ERROR; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | audio_io_handle_t AudioPolicyManager::getOutputForDevice( |
| 785 | audio_devices_t device, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 786 | audio_session_t session __unused, |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 787 | audio_stream_type_t stream, |
| 788 | uint32_t samplingRate, |
| 789 | audio_format_t format, |
| 790 | audio_channel_mask_t channelMask, |
| 791 | audio_output_flags_t flags, |
| 792 | const audio_offload_info_t *offloadInfo) |
| 793 | { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 794 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 795 | status_t status; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 796 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 797 | #ifdef AUDIO_POLICY_TEST |
| 798 | if (mCurOutput != 0) { |
| 799 | ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d", |
| 800 | mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput); |
| 801 | |
| 802 | if (mTestOutputs[mCurOutput] == 0) { |
| 803 | ALOGV("getOutput() opening test output"); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 804 | sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL, |
| 805 | mpClientInterface); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 806 | outputDesc->mDevice = mTestDevice; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 807 | outputDesc->mLatency = mTestLatencyMs; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 808 | outputDesc->mFlags = |
| 809 | (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 810 | outputDesc->mRefCount[stream] = 0; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 811 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 812 | config.sample_rate = mTestSamplingRate; |
| 813 | config.channel_mask = mTestChannels; |
| 814 | config.format = mTestFormat; |
Phil Burk | 77cce80 | 2014-08-04 16:18:15 -0700 | [diff] [blame] | 815 | if (offloadInfo != NULL) { |
| 816 | config.offload_info = *offloadInfo; |
| 817 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 818 | status = mpClientInterface->openOutput(0, |
| 819 | &mTestOutputs[mCurOutput], |
| 820 | &config, |
| 821 | &outputDesc->mDevice, |
| 822 | String8(""), |
| 823 | &outputDesc->mLatency, |
| 824 | outputDesc->mFlags); |
| 825 | if (status == NO_ERROR) { |
| 826 | outputDesc->mSamplingRate = config.sample_rate; |
| 827 | outputDesc->mFormat = config.format; |
| 828 | outputDesc->mChannelMask = config.channel_mask; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 829 | AudioParameter outputCmd = AudioParameter(); |
| 830 | outputCmd.addInt(String8("set_id"),mCurOutput); |
| 831 | mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString()); |
| 832 | addOutput(mTestOutputs[mCurOutput], outputDesc); |
| 833 | } |
| 834 | } |
| 835 | return mTestOutputs[mCurOutput]; |
| 836 | } |
| 837 | #endif //AUDIO_POLICY_TEST |
| 838 | |
| 839 | // open a direct output if required by specified parameters |
| 840 | //force direct flag if offload flag is set: offloading implies a direct output stream |
| 841 | // and all common behaviors are driven by checking only the direct flag |
| 842 | // this should normally be set appropriately in the policy configuration file |
| 843 | if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 844 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 845 | } |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 846 | if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 847 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT); |
| 848 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 849 | // only allow deep buffering for music stream type |
| 850 | if (stream != AUDIO_STREAM_MUSIC) { |
| 851 | flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 852 | } else if (/* stream == AUDIO_STREAM_MUSIC && */ |
| 853 | flags == AUDIO_OUTPUT_FLAG_NONE && |
| 854 | property_get_bool("audio.deep_buffer.media", false /* default_value */)) { |
| 855 | // use DEEP_BUFFER as default output for music stream type |
| 856 | flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER; |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 857 | } |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 858 | if (stream == AUDIO_STREAM_TTS) { |
| 859 | flags = AUDIO_OUTPUT_FLAG_TTS; |
| 860 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 861 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 862 | sp<IOProfile> profile; |
| 863 | |
| 864 | // 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] | 865 | // and not explicitly requested |
| 866 | if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 867 | audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX && |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 868 | audio_channel_count_from_out_mask(channelMask) <= 2) { |
| 869 | goto non_direct_output; |
| 870 | } |
| 871 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 872 | // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled. |
| 873 | // This prevents creating an offloaded track and tearing it down immediately after start |
| 874 | // when audioflinger detects there is an active non offloadable effect. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 875 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 876 | // This may prevent offloading in rare situations where effects are left active by apps |
| 877 | // in the background. |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 878 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 879 | if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 880 | !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 881 | profile = getProfileForDirectOutput(device, |
| 882 | samplingRate, |
| 883 | format, |
| 884 | channelMask, |
| 885 | (audio_output_flags_t)flags); |
| 886 | } |
| 887 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 888 | if (profile != 0) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 889 | sp<SwAudioOutputDescriptor> outputDesc = NULL; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 890 | |
| 891 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 892 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 893 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { |
| 894 | outputDesc = desc; |
| 895 | // reuse direct output if currently open and configured with same parameters |
| 896 | if ((samplingRate == outputDesc->mSamplingRate) && |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 897 | audio_formats_match(format, outputDesc->mFormat) && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 898 | (channelMask == outputDesc->mChannelMask)) { |
| 899 | outputDesc->mDirectOpenCount++; |
| 900 | ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i)); |
| 901 | return mOutputs.keyAt(i); |
| 902 | } |
| 903 | } |
| 904 | } |
| 905 | // close direct output if currently open and configured with different parameters |
| 906 | if (outputDesc != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 907 | closeOutput(outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 908 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 909 | |
| 910 | // if the selected profile is offloaded and no offload info was specified, |
| 911 | // create a default one |
| 912 | audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 913 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 914 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
| 915 | defaultOffloadInfo.sample_rate = samplingRate; |
| 916 | defaultOffloadInfo.channel_mask = channelMask; |
| 917 | defaultOffloadInfo.format = format; |
| 918 | defaultOffloadInfo.stream_type = stream; |
| 919 | defaultOffloadInfo.bit_rate = 0; |
| 920 | defaultOffloadInfo.duration_us = -1; |
| 921 | defaultOffloadInfo.has_video = true; // conservative |
| 922 | defaultOffloadInfo.is_streaming = true; // likely |
| 923 | offloadInfo = &defaultOffloadInfo; |
| 924 | } |
| 925 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 926 | outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 927 | outputDesc->mDevice = device; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 928 | outputDesc->mLatency = 0; |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 929 | outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 930 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 931 | config.sample_rate = samplingRate; |
| 932 | config.channel_mask = channelMask; |
| 933 | config.format = format; |
Phil Burk | 77cce80 | 2014-08-04 16:18:15 -0700 | [diff] [blame] | 934 | if (offloadInfo != NULL) { |
| 935 | config.offload_info = *offloadInfo; |
| 936 | } |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 937 | status = mpClientInterface->openOutput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 938 | &output, |
| 939 | &config, |
| 940 | &outputDesc->mDevice, |
| 941 | String8(""), |
| 942 | &outputDesc->mLatency, |
| 943 | outputDesc->mFlags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 944 | |
| 945 | // only accept an output with the requested parameters |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 946 | if (status != NO_ERROR || |
| 947 | (samplingRate != 0 && samplingRate != config.sample_rate) || |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 948 | (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) || |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 949 | (channelMask != 0 && channelMask != config.channel_mask)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 950 | ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d," |
| 951 | "format %d %d, channelMask %04x %04x", output, samplingRate, |
| 952 | outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask, |
| 953 | outputDesc->mChannelMask); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 954 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 955 | mpClientInterface->closeOutput(output); |
| 956 | } |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 957 | // 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] | 958 | if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) { |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 959 | goto non_direct_output; |
| 960 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 961 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 962 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 963 | outputDesc->mSamplingRate = config.sample_rate; |
| 964 | outputDesc->mChannelMask = config.channel_mask; |
| 965 | outputDesc->mFormat = config.format; |
| 966 | outputDesc->mRefCount[stream] = 0; |
| 967 | outputDesc->mStopTime[stream] = 0; |
| 968 | outputDesc->mDirectOpenCount = 1; |
| 969 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 970 | audio_io_handle_t srcOutput = getOutputForEffect(); |
| 971 | addOutput(output, outputDesc); |
| 972 | audio_io_handle_t dstOutput = getOutputForEffect(); |
| 973 | if (dstOutput == output) { |
| 974 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput); |
| 975 | } |
| 976 | mPreviousOutputs = mOutputs; |
| 977 | ALOGV("getOutput() returns new direct output %d", output); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 978 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 979 | return output; |
| 980 | } |
| 981 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 982 | non_direct_output: |
Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 983 | |
| 984 | // A request for HW A/V sync cannot fallback to a mixed output because time |
| 985 | // stamps are embedded in audio data |
| 986 | if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 987 | return AUDIO_IO_HANDLE_NONE; |
| 988 | } |
| 989 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 990 | // ignoring channel mask due to downmix capability in mixer |
| 991 | |
| 992 | // open a non direct output |
| 993 | |
| 994 | // for non direct outputs, only PCM is supported |
| 995 | if (audio_is_linear_pcm(format)) { |
| 996 | // get which output is suitable for the specified stream. The actual |
| 997 | // routing change will happen when startOutput() will be called |
| 998 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 999 | |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1000 | // at this stage we should ignore the DIRECT flag as no direct output could be found earlier |
| 1001 | flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT); |
| 1002 | output = selectOutput(outputs, flags, format); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1003 | } |
| 1004 | ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d," |
| 1005 | "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags); |
| 1006 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1007 | ALOGV(" getOutputForDevice() returns output %d", output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1008 | |
| 1009 | return output; |
| 1010 | } |
| 1011 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1012 | 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] | 1013 | audio_output_flags_t flags, |
| 1014 | audio_format_t format) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1015 | { |
| 1016 | // select one output among several that provide a path to a particular device or set of |
| 1017 | // devices (the list was previously build by getOutputsForDevice()). |
| 1018 | // The priority is as follows: |
| 1019 | // 1: the output with the highest number of requested policy flags |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1020 | // 2: the output with the bit depth the closest to the requested one |
| 1021 | // 3: the primary output |
| 1022 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1023 | |
| 1024 | if (outputs.size() == 0) { |
| 1025 | return 0; |
| 1026 | } |
| 1027 | if (outputs.size() == 1) { |
| 1028 | return outputs[0]; |
| 1029 | } |
| 1030 | |
| 1031 | int maxCommonFlags = 0; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1032 | audio_io_handle_t outputForFlags = 0; |
| 1033 | audio_io_handle_t outputForPrimary = 0; |
| 1034 | audio_io_handle_t outputForFormat = 0; |
| 1035 | audio_format_t bestFormat = AUDIO_FORMAT_INVALID; |
| 1036 | audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1037 | |
| 1038 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1039 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1040 | if (!outputDesc->isDuplicated()) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1041 | // if a valid format is specified, skip output if not compatible |
| 1042 | if (format != AUDIO_FORMAT_INVALID) { |
| 1043 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1044 | if (!audio_formats_match(format, outputDesc->mFormat)) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1045 | continue; |
| 1046 | } |
| 1047 | } else if (!audio_is_linear_pcm(format)) { |
| 1048 | continue; |
| 1049 | } |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1050 | if (AudioPort::isBetterFormatMatch( |
| 1051 | outputDesc->mFormat, bestFormat, format)) { |
| 1052 | outputForFormat = outputs[i]; |
| 1053 | bestFormat = outputDesc->mFormat; |
| 1054 | } |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1055 | } |
| 1056 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1057 | int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags); |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1058 | if (commonFlags >= maxCommonFlags) { |
| 1059 | if (commonFlags == maxCommonFlags) { |
| 1060 | if (AudioPort::isBetterFormatMatch( |
| 1061 | outputDesc->mFormat, bestFormatForFlags, format)) { |
| 1062 | outputForFlags = outputs[i]; |
| 1063 | bestFormatForFlags = outputDesc->mFormat; |
| 1064 | } |
| 1065 | } else { |
| 1066 | outputForFlags = outputs[i]; |
| 1067 | maxCommonFlags = commonFlags; |
| 1068 | bestFormatForFlags = outputDesc->mFormat; |
| 1069 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1070 | ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags); |
| 1071 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1072 | if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1073 | outputForPrimary = outputs[i]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1074 | } |
| 1075 | } |
| 1076 | } |
| 1077 | |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1078 | if (outputForFlags != 0) { |
| 1079 | return outputForFlags; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1080 | } |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1081 | if (outputForFormat != 0) { |
| 1082 | return outputForFormat; |
| 1083 | } |
| 1084 | if (outputForPrimary != 0) { |
| 1085 | return outputForPrimary; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | return outputs[0]; |
| 1089 | } |
| 1090 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1091 | status_t AudioPolicyManager::startOutput(audio_io_handle_t output, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1092 | audio_stream_type_t stream, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1093 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1094 | { |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1095 | ALOGV("startOutput() output %d, stream %d, session %d", |
| 1096 | output, stream, session); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1097 | ssize_t index = mOutputs.indexOfKey(output); |
| 1098 | if (index < 0) { |
| 1099 | ALOGW("startOutput() unknown output %d", output); |
| 1100 | return BAD_VALUE; |
| 1101 | } |
| 1102 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1103 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
| 1104 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1105 | // Routing? |
| 1106 | mOutputRoutes.incRouteActivity(session); |
| 1107 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1108 | audio_devices_t newDevice; |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1109 | AudioMix *policyMix = NULL; |
| 1110 | const char *address = NULL; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1111 | if (outputDesc->mPolicyMix != NULL) { |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1112 | policyMix = outputDesc->mPolicyMix; |
| 1113 | address = policyMix->mDeviceAddress.string(); |
| 1114 | if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 1115 | newDevice = policyMix->mDeviceType; |
| 1116 | } else { |
| 1117 | newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
| 1118 | } |
Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 1119 | } else if (mOutputRoutes.hasRouteChanged(session)) { |
| 1120 | newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1121 | checkStrategyRoute(getStrategy(stream), output); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1122 | } else { |
| 1123 | newDevice = AUDIO_DEVICE_NONE; |
| 1124 | } |
| 1125 | |
| 1126 | uint32_t delayMs = 0; |
| 1127 | |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1128 | status_t status = startSource(outputDesc, stream, newDevice, address, &delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1129 | |
| 1130 | if (status != NO_ERROR) { |
| 1131 | mOutputRoutes.decRouteActivity(session); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1132 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1133 | } |
| 1134 | // Automatically enable the remote submix input when output is started on a re routing mix |
| 1135 | // of type MIX_TYPE_RECORDERS |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1136 | if (audio_is_remote_submix_device(newDevice) && policyMix != NULL && |
| 1137 | policyMix->mMixType == MIX_TYPE_RECORDERS) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1138 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1139 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1140 | address, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1141 | "remote-submix"); |
| 1142 | } |
| 1143 | |
| 1144 | if (delayMs != 0) { |
| 1145 | usleep(delayMs * 1000); |
| 1146 | } |
| 1147 | |
| 1148 | return status; |
| 1149 | } |
| 1150 | |
| 1151 | status_t AudioPolicyManager::startSource(sp<AudioOutputDescriptor> outputDesc, |
| 1152 | audio_stream_type_t stream, |
| 1153 | audio_devices_t device, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1154 | const char *address, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1155 | uint32_t *delayMs) |
| 1156 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1157 | // cannot start playback of STREAM_TTS if any other output is being used |
| 1158 | uint32_t beaconMuteLatency = 0; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1159 | |
| 1160 | *delayMs = 0; |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1161 | if (stream == AUDIO_STREAM_TTS) { |
| 1162 | ALOGV("\t found BEACON stream"); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 1163 | if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1164 | return INVALID_OPERATION; |
| 1165 | } else { |
| 1166 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); |
| 1167 | } |
| 1168 | } else { |
| 1169 | // some playback other than beacon starts |
| 1170 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); |
| 1171 | } |
| 1172 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1173 | // force device change if the output is inactive and no audio patch is already present. |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1174 | // check active before incrementing usage count |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1175 | bool force = !outputDesc->isActive() && |
| 1176 | (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1177 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1178 | // increment usage count for this stream on the requested output: |
| 1179 | // NOTE that the usage count is the same for duplicated output and hardware output which is |
| 1180 | // necessary for a correct control of hardware output routing by startOutput() and stopOutput() |
| 1181 | outputDesc->changeRefCount(stream, 1); |
| 1182 | |
Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 1183 | if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1184 | // starting an output being rerouted? |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1185 | if (device == AUDIO_DEVICE_NONE) { |
| 1186 | device = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1187 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1188 | routing_strategy strategy = getStrategy(stream); |
| 1189 | bool shouldWait = (strategy == STRATEGY_SONIFICATION) || |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1190 | (strategy == STRATEGY_SONIFICATION_RESPECTFUL) || |
| 1191 | (beaconMuteLatency > 0); |
| 1192 | uint32_t waitMs = beaconMuteLatency; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1193 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1194 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1195 | if (desc != outputDesc) { |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1196 | // force a device change if any other output is: |
| 1197 | // - managed by the same hw module |
| 1198 | // - has a current device selection that differs from selected device. |
| 1199 | // - supports currently selected device |
| 1200 | // - has an active audio patch |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1201 | // In this case, the audio HAL must receive the new device selection so that it can |
| 1202 | // change the device currently selected by the other active output. |
| 1203 | if (outputDesc->sharesHwModuleWith(desc) && |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1204 | desc->device() != device && |
| 1205 | desc->supportedDevices() & device && |
| 1206 | desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1207 | force = true; |
| 1208 | } |
| 1209 | // 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] | 1210 | // a notification so that audio focus effect can propagate, or that a mute/unmute |
| 1211 | // event occurred for beacon |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1212 | uint32_t latency = desc->latency(); |
| 1213 | if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) { |
| 1214 | waitMs = latency; |
| 1215 | } |
| 1216 | } |
| 1217 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1218 | uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force, 0, NULL, address); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1219 | |
| 1220 | // handle special case for sonification while in call |
| 1221 | if (isInCall()) { |
| 1222 | handleIncallSonification(stream, true, false); |
| 1223 | } |
| 1224 | |
| 1225 | // apply volume rules for current stream and device if necessary |
| 1226 | checkAndSetVolume(stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 1227 | mVolumeCurves->getVolumeIndex(stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1228 | outputDesc, |
| 1229 | device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1230 | |
| 1231 | // update the outputs if starting an output with a stream that can affect notification |
| 1232 | // routing |
| 1233 | handleNotificationRoutingForStream(stream); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1234 | |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 1235 | // force reevaluating accessibility routing when ringtone or alarm starts |
| 1236 | if (strategy == STRATEGY_SONIFICATION) { |
| 1237 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
| 1238 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1239 | |
| 1240 | if (waitMs > muteWaitMs) { |
| 1241 | *delayMs = waitMs - muteWaitMs; |
| 1242 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1243 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1244 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1245 | return NO_ERROR; |
| 1246 | } |
| 1247 | |
| 1248 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1249 | status_t AudioPolicyManager::stopOutput(audio_io_handle_t output, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1250 | audio_stream_type_t stream, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1251 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1252 | { |
| 1253 | ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session); |
| 1254 | ssize_t index = mOutputs.indexOfKey(output); |
| 1255 | if (index < 0) { |
| 1256 | ALOGW("stopOutput() unknown output %d", output); |
| 1257 | return BAD_VALUE; |
| 1258 | } |
| 1259 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1260 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1261 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1262 | if (outputDesc->mRefCount[stream] == 1) { |
| 1263 | // Automatically disable the remote submix input when output is stopped on a |
| 1264 | // re routing mix of type MIX_TYPE_RECORDERS |
| 1265 | if (audio_is_remote_submix_device(outputDesc->mDevice) && |
| 1266 | outputDesc->mPolicyMix != NULL && |
| 1267 | outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1268 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1269 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1270 | outputDesc->mPolicyMix->mDeviceAddress, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1271 | "remote-submix"); |
| 1272 | } |
| 1273 | } |
| 1274 | |
| 1275 | // Routing? |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1276 | bool forceDeviceUpdate = false; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1277 | if (outputDesc->mRefCount[stream] > 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1278 | int activityCount = mOutputRoutes.decRouteActivity(session); |
| 1279 | forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0)); |
| 1280 | |
| 1281 | if (forceDeviceUpdate) { |
| 1282 | checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE); |
| 1283 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1284 | } |
| 1285 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1286 | return stopSource(outputDesc, stream, forceDeviceUpdate); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | status_t AudioPolicyManager::stopSource(sp<AudioOutputDescriptor> outputDesc, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1290 | audio_stream_type_t stream, |
| 1291 | bool forceDeviceUpdate) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1292 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1293 | // always handle stream stop, check which stream type is stopping |
| 1294 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); |
| 1295 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1296 | // handle special case for sonification while in call |
| 1297 | if (isInCall()) { |
| 1298 | handleIncallSonification(stream, false, false); |
| 1299 | } |
| 1300 | |
| 1301 | if (outputDesc->mRefCount[stream] > 0) { |
| 1302 | // decrement usage count of this stream on the output |
| 1303 | outputDesc->changeRefCount(stream, -1); |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1304 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1305 | // store time at which the stream was stopped - see isStreamActive() |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1306 | if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1307 | outputDesc->mStopTime[stream] = systemTime(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1308 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1309 | // delay the device switch by twice the latency because stopOutput() is executed when |
| 1310 | // the track stop() command is received and at that time the audio track buffer can |
| 1311 | // still contain data that needs to be drained. The latency only covers the audio HAL |
| 1312 | // and kernel buffers. Also the latency does not always include additional delay in the |
| 1313 | // audio path (audio DSP, CODEC ...) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1314 | setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1315 | |
| 1316 | // force restoring the device selection on other active outputs if it differs from the |
| 1317 | // one being selected for this output |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1318 | uint32_t delayMs = outputDesc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1319 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1320 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1321 | if (desc != outputDesc && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1322 | desc->isActive() && |
| 1323 | outputDesc->sharesHwModuleWith(desc) && |
| 1324 | (newDevice != desc->device())) { |
Haynes Mathew George | 11c499a | 2016-08-26 12:08:25 -0700 | [diff] [blame] | 1325 | audio_devices_t newDevice2 = getNewOutputDevice(desc, false /*fromCache*/); |
| 1326 | bool force = desc->device() != newDevice2; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1327 | setOutputDevice(desc, |
Haynes Mathew George | 11c499a | 2016-08-26 12:08:25 -0700 | [diff] [blame] | 1328 | newDevice2, |
| 1329 | force, |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1330 | delayMs); |
| 1331 | // re-apply device specific volume if not done by setOutputDevice() |
| 1332 | if (!force) { |
| 1333 | applyStreamVolumes(desc, newDevice2, delayMs); |
| 1334 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1335 | } |
| 1336 | } |
| 1337 | // update the outputs if stopping one with a stream that can affect notification routing |
| 1338 | handleNotificationRoutingForStream(stream); |
| 1339 | } |
| 1340 | return NO_ERROR; |
| 1341 | } else { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1342 | ALOGW("stopOutput() refcount is already 0"); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1343 | return INVALID_OPERATION; |
| 1344 | } |
| 1345 | } |
| 1346 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1347 | void AudioPolicyManager::releaseOutput(audio_io_handle_t output, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1348 | audio_stream_type_t stream __unused, |
| 1349 | audio_session_t session __unused) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1350 | { |
| 1351 | ALOGV("releaseOutput() %d", output); |
| 1352 | ssize_t index = mOutputs.indexOfKey(output); |
| 1353 | if (index < 0) { |
| 1354 | ALOGW("releaseOutput() releasing unknown output %d", output); |
| 1355 | return; |
| 1356 | } |
| 1357 | |
| 1358 | #ifdef AUDIO_POLICY_TEST |
| 1359 | int testIndex = testOutputIndex(output); |
| 1360 | if (testIndex != 0) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1361 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1362 | if (outputDesc->isActive()) { |
| 1363 | mpClientInterface->closeOutput(output); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1364 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1365 | mTestOutputs[testIndex] = 0; |
| 1366 | } |
| 1367 | return; |
| 1368 | } |
| 1369 | #endif //AUDIO_POLICY_TEST |
| 1370 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1371 | // Routing |
| 1372 | mOutputRoutes.removeRoute(session); |
| 1373 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1374 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1375 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1376 | if (desc->mDirectOpenCount <= 0) { |
| 1377 | ALOGW("releaseOutput() invalid open count %d for output %d", |
| 1378 | desc->mDirectOpenCount, output); |
| 1379 | return; |
| 1380 | } |
| 1381 | if (--desc->mDirectOpenCount == 0) { |
| 1382 | closeOutput(output); |
| 1383 | // If effects where present on the output, audioflinger moved them to the primary |
| 1384 | // output by default: move them back to the appropriate output. |
| 1385 | audio_io_handle_t dstOutput = getOutputForEffect(); |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 1386 | if (hasPrimaryOutput() && dstOutput != mPrimaryOutput->mIoHandle) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1387 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, |
| 1388 | mPrimaryOutput->mIoHandle, dstOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1389 | } |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1390 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1391 | } |
| 1392 | } |
| 1393 | } |
| 1394 | |
| 1395 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1396 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, |
| 1397 | audio_io_handle_t *input, |
| 1398 | audio_session_t session, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1399 | uid_t uid, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1400 | uint32_t samplingRate, |
| 1401 | audio_format_t format, |
| 1402 | audio_channel_mask_t channelMask, |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1403 | audio_input_flags_t flags, |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1404 | audio_port_handle_t selectedDeviceId, |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1405 | input_type_t *inputType) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1406 | { |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1407 | ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x," |
| 1408 | "session %d, flags %#x", |
| 1409 | attr->source, samplingRate, format, channelMask, session, flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1410 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1411 | *input = AUDIO_IO_HANDLE_NONE; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1412 | *inputType = API_INPUT_INVALID; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1413 | audio_devices_t device; |
| 1414 | // handle legacy remote submix case where the address was not always specified |
| 1415 | String8 address = String8(""); |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1416 | audio_source_t inputSource = attr->source; |
| 1417 | audio_source_t halInputSource; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1418 | AudioMix *policyMix = NULL; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1419 | |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1420 | if (inputSource == AUDIO_SOURCE_DEFAULT) { |
| 1421 | inputSource = AUDIO_SOURCE_MIC; |
| 1422 | } |
| 1423 | halInputSource = inputSource; |
| 1424 | |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1425 | // Explicit routing? |
| 1426 | sp<DeviceDescriptor> deviceDesc; |
| 1427 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { |
| 1428 | if (mAvailableInputDevices[i]->getId() == selectedDeviceId) { |
| 1429 | deviceDesc = mAvailableInputDevices[i]; |
| 1430 | break; |
| 1431 | } |
| 1432 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1433 | mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1434 | |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1435 | if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX && |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1436 | strncmp(attr->tags, "addr=", strlen("addr=")) == 0) { |
Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 1437 | status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 1438 | if (ret != NO_ERROR) { |
| 1439 | return ret; |
| 1440 | } |
| 1441 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1442 | device = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 1443 | address = String8(attr->tags + strlen("addr=")); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1444 | } else { |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1445 | device = getDeviceAndMixForInputSource(inputSource, &policyMix); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1446 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1447 | ALOGW("getInputForAttr() could not find device for source %d", inputSource); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1448 | return BAD_VALUE; |
| 1449 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1450 | if (policyMix != NULL) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1451 | address = policyMix->mDeviceAddress; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1452 | if (policyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1453 | // there is an external policy, but this input is attached to a mix of recorders, |
| 1454 | // meaning it receives audio injected into the framework, so the recorder doesn't |
| 1455 | // know about it and is therefore considered "legacy" |
| 1456 | *inputType = API_INPUT_LEGACY; |
| 1457 | } else { |
| 1458 | // recording a mix of players defined by an external policy, we're rerouting for |
| 1459 | // an external policy |
| 1460 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
| 1461 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1462 | } else if (audio_is_remote_submix_device(device)) { |
| 1463 | address = String8("0"); |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1464 | *inputType = API_INPUT_MIX_CAPTURE; |
Eric Laurent | 82db269 | 2015-08-07 13:59:42 -0700 | [diff] [blame] | 1465 | } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) { |
| 1466 | *inputType = API_INPUT_TELEPHONY_RX; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1467 | } else { |
| 1468 | *inputType = API_INPUT_LEGACY; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1469 | } |
Ravi Kumar Alamanda | b367f5b | 2015-08-25 08:21:37 -0700 | [diff] [blame] | 1470 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1471 | } |
| 1472 | |
| 1473 | *input = getInputForDevice(device, address, session, uid, inputSource, |
| 1474 | samplingRate, format, channelMask, flags, |
| 1475 | policyMix); |
| 1476 | if (*input == AUDIO_IO_HANDLE_NONE) { |
| 1477 | mInputRoutes.removeRoute(session); |
| 1478 | return INVALID_OPERATION; |
| 1479 | } |
| 1480 | ALOGV("getInputForAttr() returns input type = %d", *inputType); |
| 1481 | return NO_ERROR; |
| 1482 | } |
| 1483 | |
| 1484 | |
| 1485 | audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device, |
| 1486 | String8 address, |
| 1487 | audio_session_t session, |
| 1488 | uid_t uid, |
| 1489 | audio_source_t inputSource, |
| 1490 | uint32_t samplingRate, |
| 1491 | audio_format_t format, |
| 1492 | audio_channel_mask_t channelMask, |
| 1493 | audio_input_flags_t flags, |
| 1494 | AudioMix *policyMix) |
| 1495 | { |
| 1496 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 1497 | audio_source_t halInputSource = inputSource; |
| 1498 | bool isSoundTrigger = false; |
| 1499 | |
| 1500 | if (inputSource == AUDIO_SOURCE_HOTWORD) { |
| 1501 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); |
| 1502 | if (index >= 0) { |
| 1503 | input = mSoundTriggerSessions.valueFor(session); |
| 1504 | isSoundTrigger = true; |
| 1505 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); |
| 1506 | ALOGV("SoundTrigger capture on session %d input %d", session, input); |
| 1507 | } else { |
| 1508 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1509 | } |
| 1510 | } |
| 1511 | |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1512 | // find a compatible input profile (not necessarily identical in parameters) |
| 1513 | sp<IOProfile> profile; |
| 1514 | // samplingRate and flags may be updated by getInputProfile |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1515 | uint32_t profileSamplingRate = (samplingRate == 0) ? SAMPLE_RATE_HZ_DEFAULT : samplingRate; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1516 | audio_format_t profileFormat = format; |
| 1517 | audio_channel_mask_t profileChannelMask = channelMask; |
| 1518 | audio_input_flags_t profileFlags = flags; |
| 1519 | for (;;) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1520 | profile = getInputProfile(device, address, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1521 | profileSamplingRate, profileFormat, profileChannelMask, |
| 1522 | profileFlags); |
| 1523 | if (profile != 0) { |
| 1524 | break; // success |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1525 | } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) { |
| 1526 | profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1527 | } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) { |
| 1528 | profileFlags = AUDIO_INPUT_FLAG_NONE; // retry |
| 1529 | } else { // fail |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1530 | ALOGW("getInputForDevice() could not find profile for device 0x%X," |
| 1531 | "samplingRate %u, format %#x, channelMask 0x%X, flags %#x", |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1532 | device, samplingRate, format, channelMask, flags); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1533 | return input; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1534 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1535 | } |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1536 | // Pick input sampling rate if not specified by client |
| 1537 | if (samplingRate == 0) { |
| 1538 | samplingRate = profileSamplingRate; |
| 1539 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1540 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1541 | if (profile->getModuleHandle() == 0) { |
| 1542 | ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName()); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1543 | return input; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1544 | } |
| 1545 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1546 | sp<AudioSession> audioSession = new AudioSession(session, |
| 1547 | inputSource, |
| 1548 | format, |
| 1549 | samplingRate, |
| 1550 | channelMask, |
| 1551 | flags, |
| 1552 | uid, |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 1553 | isSoundTrigger, |
| 1554 | policyMix, mpClientInterface); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1555 | |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1556 | // TODO enable input reuse |
| 1557 | #if 0 |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1558 | // reuse an open input if possible |
| 1559 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 1560 | sp<AudioInputDescriptor> desc = mInputs.valueAt(i); |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1561 | // reuse input if it shares the same profile and same sound trigger attribute |
| 1562 | if (profile == desc->mProfile && |
| 1563 | isSoundTrigger == desc->isSoundTrigger()) { |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1564 | |
| 1565 | sp<AudioSession> as = desc->getAudioSession(session); |
| 1566 | if (as != 0) { |
| 1567 | // do not allow unmatching properties on same session |
| 1568 | if (as->matches(audioSession)) { |
| 1569 | as->changeOpenCount(1); |
| 1570 | } else { |
| 1571 | ALOGW("getInputForDevice() record with different attributes" |
| 1572 | " exists for session %d", session); |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1573 | return input; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1574 | } |
| 1575 | } else { |
| 1576 | desc->addAudioSession(session, audioSession); |
| 1577 | } |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1578 | ALOGV("getInputForDevice() reusing input %d", mInputs.keyAt(i)); |
| 1579 | return mInputs.keyAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1580 | } |
| 1581 | } |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1582 | #endif |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1583 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1584 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1585 | config.sample_rate = profileSamplingRate; |
| 1586 | config.channel_mask = profileChannelMask; |
| 1587 | config.format = profileFormat; |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1588 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1589 | status_t status = mpClientInterface->openInput(profile->getModuleHandle(), |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1590 | &input, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1591 | &config, |
| 1592 | &device, |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 1593 | address, |
Eric Laurent | 1c9c2cc | 2014-08-28 19:37:25 -0700 | [diff] [blame] | 1594 | halInputSource, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1595 | profileFlags); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1596 | |
| 1597 | // only accept input with the exact requested set of parameters |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1598 | if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE || |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1599 | (profileSamplingRate != config.sample_rate) || |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1600 | !audio_formats_match(profileFormat, config.format) || |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1601 | (profileChannelMask != config.channel_mask)) { |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1602 | ALOGW("getInputForAttr() failed opening input: samplingRate %d" |
| 1603 | ", format %d, channelMask %x", |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1604 | samplingRate, format, channelMask); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1605 | if (input != AUDIO_IO_HANDLE_NONE) { |
| 1606 | mpClientInterface->closeInput(input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1607 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1608 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1609 | } |
| 1610 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1611 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile); |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1612 | inputDesc->mSamplingRate = profileSamplingRate; |
| 1613 | inputDesc->mFormat = profileFormat; |
| 1614 | inputDesc->mChannelMask = profileChannelMask; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1615 | inputDesc->mDevice = device; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1616 | inputDesc->mPolicyMix = policyMix; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1617 | inputDesc->addAudioSession(session, audioSession); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1618 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1619 | addInput(input, inputDesc); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1620 | mpClientInterface->onAudioPortListUpdate(); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1621 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1622 | return input; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1623 | } |
| 1624 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1625 | status_t AudioPolicyManager::startInput(audio_io_handle_t input, |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1626 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1627 | { |
| 1628 | ALOGV("startInput() input %d", input); |
| 1629 | ssize_t index = mInputs.indexOfKey(input); |
| 1630 | if (index < 0) { |
| 1631 | ALOGW("startInput() unknown input %d", input); |
| 1632 | return BAD_VALUE; |
| 1633 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1634 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1635 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1636 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
| 1637 | if (audioSession == 0) { |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1638 | ALOGW("startInput() unknown session %d on input %d", session, input); |
| 1639 | return BAD_VALUE; |
| 1640 | } |
| 1641 | |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1642 | // virtual input devices are compatible with other input devices |
| 1643 | if (!is_virtual_input_device(inputDesc->mDevice)) { |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1644 | |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1645 | // for a non-virtual input device, check if there is another (non-virtual) active input |
| 1646 | audio_io_handle_t activeInput = mInputs.getActiveInput(); |
| 1647 | if (activeInput != 0 && activeInput != input) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1648 | |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1649 | // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed, |
| 1650 | // otherwise the active input continues and the new input cannot be started. |
| 1651 | sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput); |
| 1652 | if ((activeDesc->inputSource() == AUDIO_SOURCE_HOTWORD) && |
| 1653 | !activeDesc->hasPreemptedSession(session)) { |
| 1654 | ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput); |
| 1655 | //FIXME: consider all active sessions |
| 1656 | AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions(); |
| 1657 | audio_session_t activeSession = activeSessions.keyAt(0); |
| 1658 | SortedVector<audio_session_t> sessions = |
| 1659 | activeDesc->getPreemptedSessions(); |
| 1660 | sessions.add(activeSession); |
| 1661 | inputDesc->setPreemptedSessions(sessions); |
| 1662 | stopInput(activeInput, activeSession); |
| 1663 | releaseInput(activeInput, activeSession); |
| 1664 | } else { |
| 1665 | ALOGE("startInput(%d) failed: other input %d already started", input, activeInput); |
| 1666 | return INVALID_OPERATION; |
| 1667 | } |
| 1668 | } |
| 1669 | |
| 1670 | // Do not allow capture if an active voice call is using a software patch and |
| 1671 | // the call TX source device is on the same HW module. |
| 1672 | // FIXME: would be better to refine to only inputs whose profile connects to the |
| 1673 | // call TX device but this information is not in the audio patch |
| 1674 | if (mCallTxPatch != 0 && |
| 1675 | inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) { |
| 1676 | return INVALID_OPERATION; |
| 1677 | } |
| 1678 | } |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 1679 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1680 | // Routing? |
| 1681 | mInputRoutes.incRouteActivity(session); |
| 1682 | |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1683 | if (!inputDesc->isActive() || mInputRoutes.hasRouteChanged(session)) { |
| 1684 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 1685 | if ((inputDesc->mPolicyMix != NULL) |
| 1686 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1687 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1688 | MIX_STATE_MIXING); |
| 1689 | } |
Jean-Michel Trivi | 2b0c1fc | 2015-04-15 17:29:28 -0700 | [diff] [blame] | 1690 | |
Eric Laurent | 271a93e | 2016-09-29 14:47:06 -0700 | [diff] [blame] | 1691 | // indicate active capture to sound trigger service if starting capture from a mic on |
| 1692 | // primary HW module |
| 1693 | audio_devices_t device = getNewInputDevice(input); |
| 1694 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); |
| 1695 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
| 1696 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1697 | SoundTrigger::setCaptureState(true); |
| 1698 | } |
Eric Laurent | 271a93e | 2016-09-29 14:47:06 -0700 | [diff] [blame] | 1699 | setInputDevice(input, device, true /* force */); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1700 | |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1701 | // automatically enable the remote submix output when input is started if not |
| 1702 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 1703 | // For remote submix (a virtual device), we open only one input per capture request. |
| 1704 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 1705 | String8 address = String8(""); |
| 1706 | if (inputDesc->mPolicyMix == NULL) { |
| 1707 | address = String8("0"); |
| 1708 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1709 | address = inputDesc->mPolicyMix->mDeviceAddress; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1710 | } |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1711 | if (address != "") { |
| 1712 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 1713 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 1714 | address, "remote-submix"); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1715 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1716 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1717 | } |
| 1718 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1719 | ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1720 | |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1721 | audioSession->changeActiveCount(1); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1722 | return NO_ERROR; |
| 1723 | } |
| 1724 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1725 | status_t AudioPolicyManager::stopInput(audio_io_handle_t input, |
| 1726 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1727 | { |
| 1728 | ALOGV("stopInput() input %d", input); |
| 1729 | ssize_t index = mInputs.indexOfKey(input); |
| 1730 | if (index < 0) { |
| 1731 | ALOGW("stopInput() unknown input %d", input); |
| 1732 | return BAD_VALUE; |
| 1733 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1734 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1735 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1736 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1737 | if (index < 0) { |
| 1738 | ALOGW("stopInput() unknown session %d on input %d", session, input); |
| 1739 | return BAD_VALUE; |
| 1740 | } |
| 1741 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1742 | if (audioSession->activeCount() == 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1743 | ALOGW("stopInput() input %d already stopped", input); |
| 1744 | return INVALID_OPERATION; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1745 | } |
| 1746 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1747 | audioSession->changeActiveCount(-1); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1748 | |
| 1749 | // Routing? |
| 1750 | mInputRoutes.decRouteActivity(session); |
| 1751 | |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1752 | if (!inputDesc->isActive()) { |
| 1753 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 1754 | if ((inputDesc->mPolicyMix != NULL) |
| 1755 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1756 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1757 | MIX_STATE_IDLE); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 1758 | } |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1759 | |
| 1760 | // automatically disable the remote submix output when input is stopped if not |
| 1761 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 1762 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 1763 | String8 address = String8(""); |
| 1764 | if (inputDesc->mPolicyMix == NULL) { |
| 1765 | address = String8("0"); |
| 1766 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1767 | address = inputDesc->mPolicyMix->mDeviceAddress; |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1768 | } |
| 1769 | if (address != "") { |
| 1770 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 1771 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 1772 | address, "remote-submix"); |
| 1773 | } |
| 1774 | } |
| 1775 | |
Eric Laurent | 271a93e | 2016-09-29 14:47:06 -0700 | [diff] [blame] | 1776 | audio_devices_t device = inputDesc->mDevice; |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1777 | resetInputDevice(input); |
| 1778 | |
Eric Laurent | 271a93e | 2016-09-29 14:47:06 -0700 | [diff] [blame] | 1779 | // indicate inactive capture to sound trigger service if stopping capture from a mic on |
| 1780 | // primary HW module |
| 1781 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); |
| 1782 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
| 1783 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1784 | SoundTrigger::setCaptureState(false); |
| 1785 | } |
| 1786 | inputDesc->clearPreemptedSessions(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1787 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1788 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1789 | } |
| 1790 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1791 | void AudioPolicyManager::releaseInput(audio_io_handle_t input, |
| 1792 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1793 | { |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 1794 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1795 | ALOGV("releaseInput() %d", input); |
| 1796 | ssize_t index = mInputs.indexOfKey(input); |
| 1797 | if (index < 0) { |
| 1798 | ALOGW("releaseInput() releasing unknown input %d", input); |
| 1799 | return; |
| 1800 | } |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1801 | |
| 1802 | // Routing |
| 1803 | mInputRoutes.removeRoute(session); |
| 1804 | |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1805 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
| 1806 | ALOG_ASSERT(inputDesc != 0); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1807 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1808 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1809 | if (index < 0) { |
| 1810 | ALOGW("releaseInput() unknown session %d on input %d", session, input); |
| 1811 | return; |
| 1812 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1813 | |
| 1814 | if (audioSession->openCount() == 0) { |
| 1815 | ALOGW("releaseInput() invalid open count %d on session %d", |
| 1816 | audioSession->openCount(), session); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1817 | return; |
| 1818 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1819 | |
| 1820 | if (audioSession->changeOpenCount(-1) == 0) { |
| 1821 | inputDesc->removeAudioSession(session); |
| 1822 | } |
| 1823 | |
Jean-Michel Trivi | 65bfe91 | 2015-12-04 15:56:47 -0800 | [diff] [blame] | 1824 | if (inputDesc->getOpenRefCount() > 0) { |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1825 | ALOGV("releaseInput() exit > 0"); |
| 1826 | return; |
| 1827 | } |
| 1828 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1829 | closeInput(input); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1830 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1831 | ALOGV("releaseInput() exit"); |
| 1832 | } |
| 1833 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1834 | void AudioPolicyManager::closeAllInputs() { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1835 | bool patchRemoved = false; |
| 1836 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1837 | for(size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1838 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index); |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 1839 | ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1840 | if (patch_index >= 0) { |
| 1841 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 1842 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1843 | mAudioPatches.removeItemsAt(patch_index); |
| 1844 | patchRemoved = true; |
| 1845 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1846 | mpClientInterface->closeInput(mInputs.keyAt(input_index)); |
| 1847 | } |
| 1848 | mInputs.clear(); |
Chris Thornton | 52785f3 | 2016-02-09 17:28:49 -0800 | [diff] [blame] | 1849 | SoundTrigger::setCaptureState(false); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1850 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1851 | |
| 1852 | if (patchRemoved) { |
| 1853 | mpClientInterface->onAudioPatchListUpdate(); |
| 1854 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1855 | } |
| 1856 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1857 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1858 | int indexMin, |
| 1859 | int indexMax) |
| 1860 | { |
| 1861 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 1862 | mVolumeCurves->initStreamVolume(stream, indexMin, indexMax); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1863 | |
| 1864 | // initialize other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 1865 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 1866 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1867 | continue; |
| 1868 | } |
| 1869 | mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1870 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1871 | } |
| 1872 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1873 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1874 | int index, |
| 1875 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1876 | { |
| 1877 | |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 1878 | if ((index < mVolumeCurves->getVolumeIndexMin(stream)) || |
| 1879 | (index > mVolumeCurves->getVolumeIndexMax(stream))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1880 | return BAD_VALUE; |
| 1881 | } |
| 1882 | if (!audio_is_output_device(device)) { |
| 1883 | return BAD_VALUE; |
| 1884 | } |
| 1885 | |
| 1886 | // Force max volume if stream cannot be muted |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 1887 | if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1888 | |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 1889 | ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1890 | stream, device, index); |
| 1891 | |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1892 | // update other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 1893 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 1894 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1895 | continue; |
| 1896 | } |
| 1897 | mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index); |
| 1898 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1899 | |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 1900 | // update volume on all outputs and streams matching the following: |
| 1901 | // - The requested stream (or a stream matching for volume control) is active on the output |
| 1902 | // - The device (or devices) selected by the strategy corresponding to this stream includes |
| 1903 | // the requested device |
| 1904 | // - For non default requested device, currently selected device on the output is either the |
| 1905 | // requested device or one of the devices selected by the strategy |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 1906 | // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if |
| 1907 | // no specific device volume value exists for currently selected device. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1908 | status_t status = NO_ERROR; |
| 1909 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1910 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 1911 | audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device()); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 1912 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 1913 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1914 | continue; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1915 | } |
Eric Laurent | d3926fe | 2016-04-15 18:10:07 -0700 | [diff] [blame] | 1916 | if (!(desc->isStreamActive((audio_stream_type_t)curStream) || |
| 1917 | (isInCall() && (curStream == AUDIO_STREAM_VOICE_CALL)))) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 1918 | continue; |
| 1919 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 1920 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 1921 | audio_devices_t curStreamDevice = getDeviceForStrategy(curStrategy, false /*fromCache*/); |
| 1922 | if ((device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) && |
| 1923 | ((curStreamDevice & device) == 0)) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 1924 | continue; |
| 1925 | } |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 1926 | bool applyVolume; |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 1927 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 1928 | curStreamDevice |= device; |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 1929 | applyVolume = (curDevice & curStreamDevice) != 0; |
| 1930 | } else { |
| 1931 | applyVolume = !mVolumeCurves->hasVolumeIndexForDevice( |
| 1932 | stream, Volume::getDeviceForVolume(curStreamDevice)); |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 1933 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1934 | |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 1935 | if (applyVolume) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1936 | //FIXME: workaround for truncated touch sounds |
| 1937 | // delayed volume change for system stream to be removed when the problem is |
| 1938 | // handled by system UI |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1939 | status_t volStatus = |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1940 | checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice, |
| 1941 | (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1942 | if (volStatus != NO_ERROR) { |
| 1943 | status = volStatus; |
| 1944 | } |
| 1945 | } |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1946 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1947 | } |
| 1948 | return status; |
| 1949 | } |
| 1950 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1951 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1952 | int *index, |
| 1953 | audio_devices_t device) |
| 1954 | { |
| 1955 | if (index == NULL) { |
| 1956 | return BAD_VALUE; |
| 1957 | } |
| 1958 | if (!audio_is_output_device(device)) { |
| 1959 | return BAD_VALUE; |
| 1960 | } |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 1961 | // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device corresponding to |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1962 | // the strategy the stream belongs to. |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 1963 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1964 | device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); |
| 1965 | } |
François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 1966 | device = Volume::getDeviceForVolume(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1967 | |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 1968 | *index = mVolumeCurves->getVolumeIndex(stream, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1969 | ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index); |
| 1970 | return NO_ERROR; |
| 1971 | } |
| 1972 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1973 | audio_io_handle_t AudioPolicyManager::selectOutputForEffects( |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1974 | const SortedVector<audio_io_handle_t>& outputs) |
| 1975 | { |
| 1976 | // select one output among several suitable for global effects. |
| 1977 | // The priority is as follows: |
| 1978 | // 1: An offloaded output. If the effect ends up not being offloadable, |
| 1979 | // AudioFlinger will invalidate the track and the offloaded output |
| 1980 | // will be closed causing the effect to be moved to a PCM output. |
| 1981 | // 2: A deep buffer output |
| 1982 | // 3: the first output in the list |
| 1983 | |
| 1984 | if (outputs.size() == 0) { |
| 1985 | return 0; |
| 1986 | } |
| 1987 | |
| 1988 | audio_io_handle_t outputOffloaded = 0; |
| 1989 | audio_io_handle_t outputDeepBuffer = 0; |
| 1990 | |
| 1991 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1992 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1993 | ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1994 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 1995 | outputOffloaded = outputs[i]; |
| 1996 | } |
| 1997 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { |
| 1998 | outputDeepBuffer = outputs[i]; |
| 1999 | } |
| 2000 | } |
| 2001 | |
| 2002 | ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d", |
| 2003 | outputOffloaded, outputDeepBuffer); |
| 2004 | if (outputOffloaded != 0) { |
| 2005 | return outputOffloaded; |
| 2006 | } |
| 2007 | if (outputDeepBuffer != 0) { |
| 2008 | return outputDeepBuffer; |
| 2009 | } |
| 2010 | |
| 2011 | return outputs[0]; |
| 2012 | } |
| 2013 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2014 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2015 | { |
| 2016 | // apply simple rule where global effects are attached to the same output as MUSIC streams |
| 2017 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2018 | routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2019 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 2020 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs); |
| 2021 | |
| 2022 | audio_io_handle_t output = selectOutputForEffects(dstOutputs); |
| 2023 | ALOGV("getOutputForEffect() got output %d for fx %s flags %x", |
| 2024 | output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags); |
| 2025 | |
| 2026 | return output; |
| 2027 | } |
| 2028 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2029 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2030 | audio_io_handle_t io, |
| 2031 | uint32_t strategy, |
| 2032 | int session, |
| 2033 | int id) |
| 2034 | { |
| 2035 | ssize_t index = mOutputs.indexOfKey(io); |
| 2036 | if (index < 0) { |
| 2037 | index = mInputs.indexOfKey(io); |
| 2038 | if (index < 0) { |
| 2039 | ALOGW("registerEffect() unknown io %d", io); |
| 2040 | return INVALID_OPERATION; |
| 2041 | } |
| 2042 | } |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2043 | return mEffects.registerEffect(desc, io, strategy, session, id); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2044 | } |
| 2045 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2046 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2047 | { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2048 | bool active = false; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2049 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) { |
| 2050 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2051 | continue; |
| 2052 | } |
| 2053 | active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs); |
| 2054 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2055 | return active; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2056 | } |
| 2057 | |
| 2058 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2059 | { |
| 2060 | return mOutputs.isStreamActiveRemotely(stream, inPastMs); |
| 2061 | } |
| 2062 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2063 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2064 | { |
| 2065 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2066 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2067 | if (inputDescriptor->isSourceActive(source)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2068 | return true; |
| 2069 | } |
| 2070 | } |
| 2071 | return false; |
| 2072 | } |
| 2073 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2074 | // Register a list of custom mixes with their attributes and format. |
| 2075 | // When a mix is registered, corresponding input and output profiles are |
| 2076 | // added to the remote submix hw module. The profile contains only the |
| 2077 | // parameters (sampling rate, format...) specified by the mix. |
| 2078 | // The corresponding input remote submix device is also connected. |
| 2079 | // |
| 2080 | // When a remote submix device is connected, the address is checked to select the |
| 2081 | // appropriate profile and the corresponding input or output stream is opened. |
| 2082 | // |
| 2083 | // When capture starts, getInputForAttr() will: |
| 2084 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2085 | // - 2 if none found, getDeviceForInputSource() will: |
| 2086 | // - 2.1 look for a mix matching the attributes source |
| 2087 | // - 2.2 if none found, default to device selection by policy rules |
| 2088 | // At this time, the corresponding output remote submix device is also connected |
| 2089 | // and active playback use cases can be transferred to this mix if needed when reconnecting |
| 2090 | // after AudioTracks are invalidated |
| 2091 | // |
| 2092 | // When playback starts, getOutputForAttr() will: |
| 2093 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2094 | // - 2 if none found, look for a mix matching the attributes usage |
| 2095 | // - 3 if none found, default to device and output selection by policy rules. |
| 2096 | |
| 2097 | status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes) |
| 2098 | { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2099 | ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size()); |
| 2100 | status_t res = NO_ERROR; |
| 2101 | |
| 2102 | sp<HwModule> rSubmixModule; |
| 2103 | // examine each mix's route type |
| 2104 | for (size_t i = 0; i < mixes.size(); i++) { |
| 2105 | // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination |
| 2106 | if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) { |
| 2107 | res = INVALID_OPERATION; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2108 | break; |
| 2109 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2110 | if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) { |
| 2111 | // Loop back through "remote submix" |
| 2112 | if (rSubmixModule == 0) { |
| 2113 | for (size_t j = 0; i < mHwModules.size(); j++) { |
| 2114 | if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0 |
| 2115 | && mHwModules[j]->mHandle != 0) { |
| 2116 | rSubmixModule = mHwModules[j]; |
| 2117 | break; |
| 2118 | } |
| 2119 | } |
| 2120 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2121 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2122 | ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK", i, mixes.size()); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2123 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2124 | if (rSubmixModule == 0) { |
| 2125 | ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", i); |
| 2126 | res = INVALID_OPERATION; |
| 2127 | break; |
| 2128 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2129 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2130 | String8 address = mixes[i].mDeviceAddress; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2131 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2132 | if (mPolicyMixes.registerMix(address, mixes[i], 0 /*output desc*/) != NO_ERROR) { |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2133 | ALOGE(" Error registering mix %zu for address %s", i, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2134 | res = INVALID_OPERATION; |
| 2135 | break; |
| 2136 | } |
| 2137 | audio_config_t outputConfig = mixes[i].mFormat; |
| 2138 | audio_config_t inputConfig = mixes[i].mFormat; |
| 2139 | // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in |
| 2140 | // stereo and let audio flinger do the channel conversion if needed. |
| 2141 | outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 2142 | inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
| 2143 | rSubmixModule->addOutputProfile(address, &outputConfig, |
| 2144 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address); |
| 2145 | rSubmixModule->addInputProfile(address, &inputConfig, |
| 2146 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, address); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2147 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2148 | if (mixes[i].mMixType == MIX_TYPE_PLAYERS) { |
| 2149 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2150 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2151 | address.string(), "remote-submix"); |
| 2152 | } else { |
| 2153 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2154 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2155 | address.string(), "remote-submix"); |
| 2156 | } |
| 2157 | } else if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2158 | String8 address = mixes[i].mDeviceAddress; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2159 | audio_devices_t device = mixes[i].mDeviceType; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2160 | ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s", |
| 2161 | i, mixes.size(), device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2162 | |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2163 | bool foundOutput = false; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2164 | for (size_t j = 0 ; j < mOutputs.size() ; j++) { |
| 2165 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j); |
| 2166 | sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle()); |
| 2167 | if ((patch != 0) && (patch->mPatch.num_sinks != 0) |
| 2168 | && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE) |
| 2169 | && (patch->mPatch.sinks[0].ext.device.type == device) |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2170 | && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(), |
| 2171 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2172 | if (mPolicyMixes.registerMix(address, mixes[i], desc) != NO_ERROR) { |
| 2173 | res = INVALID_OPERATION; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2174 | } else { |
| 2175 | foundOutput = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2176 | } |
| 2177 | break; |
| 2178 | } |
| 2179 | } |
| 2180 | |
| 2181 | if (res != NO_ERROR) { |
| 2182 | ALOGE(" Error registering mix %zu for device 0x%X addr %s", |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2183 | i, device, address.string()); |
| 2184 | res = INVALID_OPERATION; |
| 2185 | break; |
| 2186 | } else if (!foundOutput) { |
| 2187 | ALOGE(" Output not found for mix %zu for device 0x%X addr %s", |
| 2188 | i, device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2189 | res = INVALID_OPERATION; |
| 2190 | break; |
| 2191 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2192 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2193 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2194 | if (res != NO_ERROR) { |
| 2195 | unregisterPolicyMixes(mixes); |
| 2196 | } |
| 2197 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2198 | } |
| 2199 | |
| 2200 | status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes) |
| 2201 | { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2202 | ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2203 | status_t res = NO_ERROR; |
| 2204 | sp<HwModule> rSubmixModule; |
| 2205 | // examine each mix's route type |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2206 | for (size_t i = 0; i < mixes.size(); i++) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2207 | 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] | 2208 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2209 | if (rSubmixModule == 0) { |
| 2210 | for (size_t j = 0; i < mHwModules.size(); j++) { |
| 2211 | if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0 |
| 2212 | && mHwModules[j]->mHandle != 0) { |
| 2213 | rSubmixModule = mHwModules[j]; |
| 2214 | break; |
| 2215 | } |
| 2216 | } |
| 2217 | } |
| 2218 | if (rSubmixModule == 0) { |
| 2219 | res = INVALID_OPERATION; |
| 2220 | continue; |
| 2221 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2222 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2223 | String8 address = mixes[i].mDeviceAddress; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2224 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2225 | if (mPolicyMixes.unregisterMix(address) != NO_ERROR) { |
| 2226 | res = INVALID_OPERATION; |
| 2227 | continue; |
| 2228 | } |
| 2229 | |
| 2230 | if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) == |
| 2231 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2232 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2233 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2234 | address.string(), "remote-submix"); |
| 2235 | } |
| 2236 | if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) == |
| 2237 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2238 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2239 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2240 | address.string(), "remote-submix"); |
| 2241 | } |
| 2242 | rSubmixModule->removeOutputProfile(address); |
| 2243 | rSubmixModule->removeInputProfile(address); |
| 2244 | |
| 2245 | } if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 2246 | if (mPolicyMixes.unregisterMix(mixes[i].mDeviceAddress) != NO_ERROR) { |
| 2247 | res = INVALID_OPERATION; |
| 2248 | continue; |
| 2249 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2250 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2251 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2252 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2253 | } |
| 2254 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2255 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2256 | status_t AudioPolicyManager::dump(int fd) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2257 | { |
| 2258 | const size_t SIZE = 256; |
| 2259 | char buffer[SIZE]; |
| 2260 | String8 result; |
| 2261 | |
| 2262 | snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this); |
| 2263 | result.append(buffer); |
| 2264 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 2265 | snprintf(buffer, SIZE, " Primary Output: %d\n", |
| 2266 | hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2267 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2268 | snprintf(buffer, SIZE, " Phone state: %d\n", mEngine->getPhoneState()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2269 | result.append(buffer); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2270 | snprintf(buffer, SIZE, " Force use for communications %d\n", |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2271 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2272 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2273 | 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] | 2274 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2275 | 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] | 2276 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2277 | 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] | 2278 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2279 | 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] | 2280 | result.append(buffer); |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 2281 | snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n", |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2282 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO)); |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 2283 | result.append(buffer); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 2284 | snprintf(buffer, SIZE, " Force use for encoded surround output %d\n", |
| 2285 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND)); |
| 2286 | result.append(buffer); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 2287 | snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available"); |
| 2288 | result.append(buffer); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 2289 | snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off"); |
| 2290 | result.append(buffer); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 2291 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2292 | write(fd, result.string(), result.size()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2293 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 2294 | mAvailableOutputDevices.dump(fd, String8("Available output")); |
| 2295 | mAvailableInputDevices.dump(fd, String8("Available input")); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2296 | mHwModules.dump(fd); |
| 2297 | mOutputs.dump(fd); |
| 2298 | mInputs.dump(fd); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2299 | mVolumeCurves->dump(fd); |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2300 | mEffects.dump(fd); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2301 | mAudioPatches.dump(fd); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2302 | |
| 2303 | return NO_ERROR; |
| 2304 | } |
| 2305 | |
| 2306 | // This function checks for the parameters which can be offloaded. |
| 2307 | // This can be enhanced depending on the capability of the DSP and policy |
| 2308 | // of the system. |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2309 | bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2310 | { |
| 2311 | ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2312 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2313 | offloadInfo.sample_rate, offloadInfo.channel_mask, |
| 2314 | offloadInfo.format, |
| 2315 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, |
| 2316 | offloadInfo.has_video); |
| 2317 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 2318 | if (mMasterMono) { |
| 2319 | return false; // no offloading if mono is set. |
| 2320 | } |
| 2321 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2322 | // Check if offload has been disabled |
| 2323 | char propValue[PROPERTY_VALUE_MAX]; |
| 2324 | if (property_get("audio.offload.disable", propValue, "0")) { |
| 2325 | if (atoi(propValue) != 0) { |
| 2326 | ALOGV("offload disabled by audio.offload.disable=%s", propValue ); |
| 2327 | return false; |
| 2328 | } |
| 2329 | } |
| 2330 | |
| 2331 | // Check if stream type is music, then only allow offload as of now. |
| 2332 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) |
| 2333 | { |
| 2334 | ALOGV("isOffloadSupported: stream_type != MUSIC, returning false"); |
| 2335 | return false; |
| 2336 | } |
| 2337 | |
| 2338 | //TODO: enable audio offloading with video when ready |
Andy Hung | 08945c4 | 2015-05-31 21:36:46 -0700 | [diff] [blame] | 2339 | const bool allowOffloadWithVideo = |
| 2340 | property_get_bool("audio.offload.video", false /* default_value */); |
| 2341 | if (offloadInfo.has_video && !allowOffloadWithVideo) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2342 | ALOGV("isOffloadSupported: has_video == true, returning false"); |
| 2343 | return false; |
| 2344 | } |
| 2345 | |
| 2346 | //If duration is less than minimum value defined in property, return false |
| 2347 | if (property_get("audio.offload.min.duration.secs", propValue, NULL)) { |
| 2348 | if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) { |
| 2349 | ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue); |
| 2350 | return false; |
| 2351 | } |
| 2352 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { |
| 2353 | ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS); |
| 2354 | return false; |
| 2355 | } |
| 2356 | |
| 2357 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 2358 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 2359 | // detects there is an active non offloadable effect. |
| 2360 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 2361 | // This may prevent offloading in rare situations where effects are left active by apps |
| 2362 | // in the background. |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2363 | if (mEffects.isNonOffloadableEffectEnabled()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2364 | return false; |
| 2365 | } |
| 2366 | |
| 2367 | // See if there is a profile to support this. |
| 2368 | // AUDIO_DEVICE_NONE |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2369 | sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2370 | offloadInfo.sample_rate, |
| 2371 | offloadInfo.format, |
| 2372 | offloadInfo.channel_mask, |
| 2373 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2374 | ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT "); |
| 2375 | return (profile != 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2376 | } |
| 2377 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2378 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, |
| 2379 | audio_port_type_t type, |
| 2380 | unsigned int *num_ports, |
| 2381 | struct audio_port *ports, |
| 2382 | unsigned int *generation) |
| 2383 | { |
| 2384 | if (num_ports == NULL || (*num_ports != 0 && ports == NULL) || |
| 2385 | generation == NULL) { |
| 2386 | return BAD_VALUE; |
| 2387 | } |
| 2388 | ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports); |
| 2389 | if (ports == NULL) { |
| 2390 | *num_ports = 0; |
| 2391 | } |
| 2392 | |
| 2393 | size_t portsWritten = 0; |
| 2394 | size_t portsMax = *num_ports; |
| 2395 | *num_ports = 0; |
| 2396 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2397 | // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB |
| 2398 | // as they are used by stub HALs by convention |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2399 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2400 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { |
| 2401 | if (mAvailableOutputDevices[i]->type() == AUDIO_DEVICE_OUT_STUB) { |
| 2402 | continue; |
| 2403 | } |
| 2404 | if (portsWritten < portsMax) { |
| 2405 | mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]); |
| 2406 | } |
| 2407 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2408 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2409 | } |
| 2410 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2411 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { |
| 2412 | if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_STUB) { |
| 2413 | continue; |
| 2414 | } |
| 2415 | if (portsWritten < portsMax) { |
| 2416 | mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]); |
| 2417 | } |
| 2418 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2419 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2420 | } |
| 2421 | } |
| 2422 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { |
| 2423 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 2424 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { |
| 2425 | mInputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2426 | } |
| 2427 | *num_ports += mInputs.size(); |
| 2428 | } |
| 2429 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2430 | size_t numOutputs = 0; |
| 2431 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2432 | if (!mOutputs[i]->isDuplicated()) { |
| 2433 | numOutputs++; |
| 2434 | if (portsWritten < portsMax) { |
| 2435 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2436 | } |
| 2437 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2438 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2439 | *num_ports += numOutputs; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2440 | } |
| 2441 | } |
| 2442 | *generation = curAudioPortGeneration(); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2443 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2444 | return NO_ERROR; |
| 2445 | } |
| 2446 | |
| 2447 | status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused) |
| 2448 | { |
| 2449 | return NO_ERROR; |
| 2450 | } |
| 2451 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2452 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, |
| 2453 | audio_patch_handle_t *handle, |
| 2454 | uid_t uid) |
| 2455 | { |
| 2456 | ALOGV("createAudioPatch()"); |
| 2457 | |
| 2458 | if (handle == NULL || patch == NULL) { |
| 2459 | return BAD_VALUE; |
| 2460 | } |
| 2461 | ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks); |
| 2462 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2463 | if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX || |
| 2464 | patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) { |
| 2465 | return BAD_VALUE; |
| 2466 | } |
| 2467 | // only one source per audio patch supported for now |
| 2468 | if (patch->num_sources > 1) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2469 | return INVALID_OPERATION; |
| 2470 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2471 | |
| 2472 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2473 | return INVALID_OPERATION; |
| 2474 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2475 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2476 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { |
| 2477 | return INVALID_OPERATION; |
| 2478 | } |
| 2479 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2480 | |
| 2481 | sp<AudioPatch> patchDesc; |
| 2482 | ssize_t index = mAudioPatches.indexOfKey(*handle); |
| 2483 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2484 | ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id, |
| 2485 | patch->sources[0].role, |
| 2486 | patch->sources[0].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2487 | #if LOG_NDEBUG == 0 |
| 2488 | for (size_t i = 0; i < patch->num_sinks; i++) { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2489 | 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] | 2490 | patch->sinks[i].role, |
| 2491 | patch->sinks[i].type); |
| 2492 | } |
| 2493 | #endif |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2494 | |
| 2495 | if (index >= 0) { |
| 2496 | patchDesc = mAudioPatches.valueAt(index); |
| 2497 | ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 2498 | mUidCached, patchDesc->mUid, uid); |
| 2499 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 2500 | return INVALID_OPERATION; |
| 2501 | } |
| 2502 | } else { |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 2503 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2504 | } |
| 2505 | |
| 2506 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2507 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2508 | if (outputDesc == NULL) { |
| 2509 | ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id); |
| 2510 | return BAD_VALUE; |
| 2511 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2512 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", |
| 2513 | outputDesc->mIoHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2514 | if (patchDesc != 0) { |
| 2515 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
| 2516 | ALOGV("createAudioPatch() source id differs for patch current id %d new id %d", |
| 2517 | patchDesc->mPatch.sources[0].id, patch->sources[0].id); |
| 2518 | return BAD_VALUE; |
| 2519 | } |
| 2520 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2521 | DeviceVector devices; |
| 2522 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2523 | // Only support mix to devices connection |
| 2524 | // TODO add support for mix to mix connection |
| 2525 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 2526 | ALOGV("createAudioPatch() source mix but sink is not a device"); |
| 2527 | return INVALID_OPERATION; |
| 2528 | } |
| 2529 | sp<DeviceDescriptor> devDesc = |
| 2530 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 2531 | if (devDesc == 0) { |
| 2532 | ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id); |
| 2533 | return BAD_VALUE; |
| 2534 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2535 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2536 | if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2537 | devDesc->mAddress, |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2538 | patch->sources[0].sample_rate, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2539 | NULL, // updatedSamplingRate |
| 2540 | patch->sources[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2541 | NULL, // updatedFormat |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2542 | patch->sources[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2543 | NULL, // updatedChannelMask |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2544 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2545 | ALOGV("createAudioPatch() profile not supported for device %08x", |
| 2546 | devDesc->type()); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2547 | return INVALID_OPERATION; |
| 2548 | } |
| 2549 | devices.add(devDesc); |
| 2550 | } |
| 2551 | if (devices.size() == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2552 | return INVALID_OPERATION; |
| 2553 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2554 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2555 | // TODO: reconfigure output format and channels here |
| 2556 | ALOGV("createAudioPatch() setting device %08x on output %d", |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2557 | devices.types(), outputDesc->mIoHandle); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2558 | setOutputDevice(outputDesc, devices.types(), true, 0, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2559 | index = mAudioPatches.indexOfKey(*handle); |
| 2560 | if (index >= 0) { |
| 2561 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 2562 | ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided"); |
| 2563 | } |
| 2564 | patchDesc = mAudioPatches.valueAt(index); |
| 2565 | patchDesc->mUid = uid; |
| 2566 | ALOGV("createAudioPatch() success"); |
| 2567 | } else { |
| 2568 | ALOGW("createAudioPatch() setOutputDevice() failed to create a patch"); |
| 2569 | return INVALID_OPERATION; |
| 2570 | } |
| 2571 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2572 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 2573 | // input device to input mix connection |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2574 | // only one sink supported when connecting an input device to a mix |
| 2575 | if (patch->num_sinks > 1) { |
| 2576 | return INVALID_OPERATION; |
| 2577 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2578 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2579 | if (inputDesc == NULL) { |
| 2580 | return BAD_VALUE; |
| 2581 | } |
| 2582 | if (patchDesc != 0) { |
| 2583 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { |
| 2584 | return BAD_VALUE; |
| 2585 | } |
| 2586 | } |
| 2587 | sp<DeviceDescriptor> devDesc = |
| 2588 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
| 2589 | if (devDesc == 0) { |
| 2590 | return BAD_VALUE; |
| 2591 | } |
| 2592 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2593 | if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2594 | devDesc->mAddress, |
| 2595 | patch->sinks[0].sample_rate, |
| 2596 | NULL, /*updatedSampleRate*/ |
| 2597 | patch->sinks[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2598 | NULL, /*updatedFormat*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2599 | patch->sinks[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2600 | NULL, /*updatedChannelMask*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2601 | // FIXME for the parameter type, |
| 2602 | // and the NONE |
| 2603 | (audio_output_flags_t) |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2604 | AUDIO_INPUT_FLAG_NONE)) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2605 | return INVALID_OPERATION; |
| 2606 | } |
| 2607 | // TODO: reconfigure output format and channels here |
| 2608 | ALOGV("createAudioPatch() setting device %08x on output %d", |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2609 | devDesc->type(), inputDesc->mIoHandle); |
| 2610 | setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2611 | index = mAudioPatches.indexOfKey(*handle); |
| 2612 | if (index >= 0) { |
| 2613 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 2614 | ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided"); |
| 2615 | } |
| 2616 | patchDesc = mAudioPatches.valueAt(index); |
| 2617 | patchDesc->mUid = uid; |
| 2618 | ALOGV("createAudioPatch() success"); |
| 2619 | } else { |
| 2620 | ALOGW("createAudioPatch() setInputDevice() failed to create a patch"); |
| 2621 | return INVALID_OPERATION; |
| 2622 | } |
| 2623 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2624 | // device to device connection |
| 2625 | if (patchDesc != 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2626 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2627 | return BAD_VALUE; |
| 2628 | } |
| 2629 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2630 | sp<DeviceDescriptor> srcDeviceDesc = |
| 2631 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 2632 | if (srcDeviceDesc == 0) { |
| 2633 | return BAD_VALUE; |
| 2634 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2635 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2636 | //update source and sink with our own data as the data passed in the patch may |
| 2637 | // be incomplete. |
| 2638 | struct audio_patch newPatch = *patch; |
| 2639 | srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2640 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2641 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2642 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 2643 | ALOGV("createAudioPatch() source device but one sink is not a device"); |
| 2644 | return INVALID_OPERATION; |
| 2645 | } |
| 2646 | |
| 2647 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 2648 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 2649 | if (sinkDeviceDesc == 0) { |
| 2650 | return BAD_VALUE; |
| 2651 | } |
| 2652 | sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]); |
| 2653 | |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2654 | // create a software bridge in PatchPanel if: |
| 2655 | // - source and sink devices are on differnt HW modules OR |
| 2656 | // - audio HAL version is < 3.0 |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 2657 | if ((srcDeviceDesc->getModuleHandle() != sinkDeviceDesc->getModuleHandle()) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 2658 | (srcDeviceDesc->mModule->getHalVersion() < AUDIO_DEVICE_API_VERSION_3_0)) { |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2659 | // support only one sink device for now to simplify output selection logic |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2660 | if (patch->num_sinks > 1) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2661 | return INVALID_OPERATION; |
| 2662 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2663 | SortedVector<audio_io_handle_t> outputs = |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2664 | getOutputsForDevice(sinkDeviceDesc->type(), mOutputs); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2665 | // if the sink device is reachable via an opened output stream, request to go via |
| 2666 | // this output stream by adding a second source to the patch description |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 2667 | audio_io_handle_t output = selectOutput(outputs, |
| 2668 | AUDIO_OUTPUT_FLAG_NONE, |
| 2669 | AUDIO_FORMAT_INVALID); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2670 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 2671 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 2672 | if (outputDesc->isDuplicated()) { |
| 2673 | return INVALID_OPERATION; |
| 2674 | } |
| 2675 | outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2676 | newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2677 | newPatch.num_sources = 2; |
| 2678 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2679 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2680 | } |
| 2681 | // TODO: check from routing capabilities in config file and other conflicting patches |
| 2682 | |
| 2683 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 2684 | if (index >= 0) { |
| 2685 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 2686 | } |
| 2687 | |
| 2688 | status_t status = mpClientInterface->createAudioPatch(&newPatch, |
| 2689 | &afPatchHandle, |
| 2690 | 0); |
| 2691 | ALOGV("createAudioPatch() patch panel returned %d patchHandle %d", |
| 2692 | status, afPatchHandle); |
| 2693 | if (status == NO_ERROR) { |
| 2694 | if (index < 0) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 2695 | patchDesc = new AudioPatch(&newPatch, uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2696 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 2697 | } else { |
| 2698 | patchDesc->mPatch = newPatch; |
| 2699 | } |
| 2700 | patchDesc->mAfPatchHandle = afPatchHandle; |
| 2701 | *handle = patchDesc->mHandle; |
| 2702 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2703 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2704 | } else { |
| 2705 | ALOGW("createAudioPatch() patch panel could not connect device patch, error %d", |
| 2706 | status); |
| 2707 | return INVALID_OPERATION; |
| 2708 | } |
| 2709 | } else { |
| 2710 | return BAD_VALUE; |
| 2711 | } |
| 2712 | } else { |
| 2713 | return BAD_VALUE; |
| 2714 | } |
| 2715 | return NO_ERROR; |
| 2716 | } |
| 2717 | |
| 2718 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, |
| 2719 | uid_t uid) |
| 2720 | { |
| 2721 | ALOGV("releaseAudioPatch() patch %d", handle); |
| 2722 | |
| 2723 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 2724 | |
| 2725 | if (index < 0) { |
| 2726 | return BAD_VALUE; |
| 2727 | } |
| 2728 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 2729 | ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 2730 | mUidCached, patchDesc->mUid, uid); |
| 2731 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 2732 | return INVALID_OPERATION; |
| 2733 | } |
| 2734 | |
| 2735 | struct audio_patch *patch = &patchDesc->mPatch; |
| 2736 | patchDesc->mUid = mUidCached; |
| 2737 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2738 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2739 | if (outputDesc == NULL) { |
| 2740 | ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id); |
| 2741 | return BAD_VALUE; |
| 2742 | } |
| 2743 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2744 | setOutputDevice(outputDesc, |
| 2745 | getNewOutputDevice(outputDesc, true /*fromCache*/), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2746 | true, |
| 2747 | 0, |
| 2748 | NULL); |
| 2749 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2750 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2751 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2752 | if (inputDesc == NULL) { |
| 2753 | ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id); |
| 2754 | return BAD_VALUE; |
| 2755 | } |
| 2756 | setInputDevice(inputDesc->mIoHandle, |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 2757 | getNewInputDevice(inputDesc->mIoHandle), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2758 | true, |
| 2759 | NULL); |
| 2760 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2761 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 2762 | ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d", |
| 2763 | status, patchDesc->mAfPatchHandle); |
| 2764 | removeAudioPatch(patchDesc->mHandle); |
| 2765 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2766 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2767 | } else { |
| 2768 | return BAD_VALUE; |
| 2769 | } |
| 2770 | } else { |
| 2771 | return BAD_VALUE; |
| 2772 | } |
| 2773 | return NO_ERROR; |
| 2774 | } |
| 2775 | |
| 2776 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, |
| 2777 | struct audio_patch *patches, |
| 2778 | unsigned int *generation) |
| 2779 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2780 | if (generation == NULL) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2781 | return BAD_VALUE; |
| 2782 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2783 | *generation = curAudioPortGeneration(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2784 | return mAudioPatches.listAudioPatches(num_patches, patches); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2785 | } |
| 2786 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2787 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2788 | { |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2789 | ALOGV("setAudioPortConfig()"); |
| 2790 | |
| 2791 | if (config == NULL) { |
| 2792 | return BAD_VALUE; |
| 2793 | } |
| 2794 | ALOGV("setAudioPortConfig() on port handle %d", config->id); |
| 2795 | // Only support gain configuration for now |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2796 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { |
| 2797 | return INVALID_OPERATION; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2798 | } |
| 2799 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2800 | sp<AudioPortConfig> audioPortConfig; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2801 | if (config->type == AUDIO_PORT_TYPE_MIX) { |
| 2802 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2803 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2804 | if (outputDesc == NULL) { |
| 2805 | return BAD_VALUE; |
| 2806 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2807 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 2808 | "setAudioPortConfig() called on duplicated output %d", |
| 2809 | outputDesc->mIoHandle); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2810 | audioPortConfig = outputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2811 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2812 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2813 | if (inputDesc == NULL) { |
| 2814 | return BAD_VALUE; |
| 2815 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2816 | audioPortConfig = inputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2817 | } else { |
| 2818 | return BAD_VALUE; |
| 2819 | } |
| 2820 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { |
| 2821 | sp<DeviceDescriptor> deviceDesc; |
| 2822 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
| 2823 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); |
| 2824 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
| 2825 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); |
| 2826 | } else { |
| 2827 | return BAD_VALUE; |
| 2828 | } |
| 2829 | if (deviceDesc == NULL) { |
| 2830 | return BAD_VALUE; |
| 2831 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2832 | audioPortConfig = deviceDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2833 | } else { |
| 2834 | return BAD_VALUE; |
| 2835 | } |
| 2836 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2837 | struct audio_port_config backupConfig; |
| 2838 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); |
| 2839 | if (status == NO_ERROR) { |
| 2840 | struct audio_port_config newConfig; |
| 2841 | audioPortConfig->toAudioPortConfig(&newConfig, config); |
| 2842 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2843 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2844 | if (status != NO_ERROR) { |
| 2845 | audioPortConfig->applyAudioPortConfig(&backupConfig); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2846 | } |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2847 | |
| 2848 | return status; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2849 | } |
| 2850 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2851 | void AudioPolicyManager::releaseResourcesForUid(uid_t uid) |
| 2852 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2853 | clearAudioSources(uid); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2854 | clearAudioPatches(uid); |
| 2855 | clearSessionRoutes(uid); |
| 2856 | } |
| 2857 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2858 | void AudioPolicyManager::clearAudioPatches(uid_t uid) |
| 2859 | { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 2860 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2861 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 2862 | if (patchDesc->mUid == uid) { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 2863 | releaseAudioPatch(mAudioPatches.keyAt(i), uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2864 | } |
| 2865 | } |
| 2866 | } |
| 2867 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2868 | void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy, |
| 2869 | audio_io_handle_t ouptutToSkip) |
| 2870 | { |
| 2871 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 2872 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 2873 | for (size_t j = 0; j < mOutputs.size(); j++) { |
| 2874 | if (mOutputs.keyAt(j) == ouptutToSkip) { |
| 2875 | continue; |
| 2876 | } |
| 2877 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j); |
| 2878 | if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) { |
| 2879 | continue; |
| 2880 | } |
| 2881 | // If the default device for this strategy is on another output mix, |
| 2882 | // invalidate all tracks in this strategy to force re connection. |
| 2883 | // Otherwise select new device on the output mix. |
| 2884 | if (outputs.indexOf(mOutputs.keyAt(j)) < 0) { |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2885 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2886 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
| 2887 | mpClientInterface->invalidateStream((audio_stream_type_t)stream); |
| 2888 | } |
| 2889 | } |
| 2890 | } else { |
| 2891 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
| 2892 | setOutputDevice(outputDesc, newDevice, false); |
| 2893 | } |
| 2894 | } |
| 2895 | } |
| 2896 | |
| 2897 | void AudioPolicyManager::clearSessionRoutes(uid_t uid) |
| 2898 | { |
| 2899 | // remove output routes associated with this uid |
| 2900 | SortedVector<routing_strategy> affectedStrategies; |
| 2901 | for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) { |
| 2902 | sp<SessionRoute> route = mOutputRoutes.valueAt(i); |
| 2903 | if (route->mUid == uid) { |
| 2904 | mOutputRoutes.removeItemsAt(i); |
| 2905 | if (route->mDeviceDescriptor != 0) { |
| 2906 | affectedStrategies.add(getStrategy(route->mStreamType)); |
| 2907 | } |
| 2908 | } |
| 2909 | } |
| 2910 | // reroute outputs if necessary |
| 2911 | for (size_t i = 0; i < affectedStrategies.size(); i++) { |
| 2912 | checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE); |
| 2913 | } |
| 2914 | |
| 2915 | // remove input routes associated with this uid |
| 2916 | SortedVector<audio_source_t> affectedSources; |
| 2917 | for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) { |
| 2918 | sp<SessionRoute> route = mInputRoutes.valueAt(i); |
| 2919 | if (route->mUid == uid) { |
| 2920 | mInputRoutes.removeItemsAt(i); |
| 2921 | if (route->mDeviceDescriptor != 0) { |
| 2922 | affectedSources.add(route->mSource); |
| 2923 | } |
| 2924 | } |
| 2925 | } |
| 2926 | // reroute inputs if necessary |
| 2927 | SortedVector<audio_io_handle_t> inputsToClose; |
| 2928 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 2929 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2930 | if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2931 | inputsToClose.add(inputDesc->mIoHandle); |
| 2932 | } |
| 2933 | } |
| 2934 | for (size_t i = 0; i < inputsToClose.size(); i++) { |
| 2935 | closeInput(inputsToClose[i]); |
| 2936 | } |
| 2937 | } |
| 2938 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2939 | void AudioPolicyManager::clearAudioSources(uid_t uid) |
| 2940 | { |
| 2941 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
| 2942 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 2943 | if (sourceDesc->mUid == uid) { |
| 2944 | stopAudioSource(mAudioSources.keyAt(i)); |
| 2945 | } |
| 2946 | } |
| 2947 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2948 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2949 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, |
| 2950 | audio_io_handle_t *ioHandle, |
| 2951 | audio_devices_t *device) |
| 2952 | { |
Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 2953 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); |
| 2954 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT); |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 2955 | *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2956 | |
François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 2957 | return mSoundTriggerSessions.acquireSession(*session, *ioHandle); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2958 | } |
| 2959 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2960 | status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source, |
| 2961 | const audio_attributes_t *attributes, |
| 2962 | audio_io_handle_t *handle, |
| 2963 | uid_t uid) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 2964 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2965 | ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle); |
| 2966 | if (source == NULL || attributes == NULL || handle == NULL) { |
| 2967 | return BAD_VALUE; |
| 2968 | } |
| 2969 | |
| 2970 | *handle = AUDIO_IO_HANDLE_NONE; |
| 2971 | |
| 2972 | if (source->role != AUDIO_PORT_ROLE_SOURCE || |
| 2973 | source->type != AUDIO_PORT_TYPE_DEVICE) { |
| 2974 | ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type); |
| 2975 | return INVALID_OPERATION; |
| 2976 | } |
| 2977 | |
| 2978 | sp<DeviceDescriptor> srcDeviceDesc = |
| 2979 | mAvailableInputDevices.getDevice(source->ext.device.type, |
| 2980 | String8(source->ext.device.address)); |
| 2981 | if (srcDeviceDesc == 0) { |
| 2982 | ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type); |
| 2983 | return BAD_VALUE; |
| 2984 | } |
| 2985 | sp<AudioSourceDescriptor> sourceDesc = |
| 2986 | new AudioSourceDescriptor(srcDeviceDesc, attributes, uid); |
| 2987 | |
| 2988 | struct audio_patch dummyPatch; |
| 2989 | sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid); |
| 2990 | sourceDesc->mPatchDesc = patchDesc; |
| 2991 | |
| 2992 | status_t status = connectAudioSource(sourceDesc); |
| 2993 | if (status == NO_ERROR) { |
| 2994 | mAudioSources.add(sourceDesc->getHandle(), sourceDesc); |
| 2995 | *handle = sourceDesc->getHandle(); |
| 2996 | } |
| 2997 | return status; |
| 2998 | } |
| 2999 | |
| 3000 | status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc) |
| 3001 | { |
| 3002 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle()); |
| 3003 | |
| 3004 | // make sure we only have one patch per source. |
| 3005 | disconnectAudioSource(sourceDesc); |
| 3006 | |
| 3007 | routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 3008 | audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3009 | sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice; |
| 3010 | |
| 3011 | audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true); |
| 3012 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 3013 | mAvailableOutputDevices.getDevice(sinkDevice, String8("")); |
| 3014 | |
| 3015 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 3016 | struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch; |
| 3017 | |
| 3018 | if (srcDeviceDesc->getAudioPort()->mModule->getHandle() == |
| 3019 | sinkDeviceDesc->getAudioPort()->mModule->getHandle() && |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3020 | srcDeviceDesc->getAudioPort()->mModule->getHalVersion() >= AUDIO_DEVICE_API_VERSION_3_0 && |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3021 | srcDeviceDesc->getAudioPort()->mGains.size() > 0) { |
| 3022 | ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__); |
| 3023 | // create patch between src device and output device |
| 3024 | // create Hwoutput and add to mHwOutputs |
| 3025 | } else { |
| 3026 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs); |
| 3027 | audio_io_handle_t output = |
| 3028 | selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID); |
| 3029 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 3030 | ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice); |
| 3031 | return INVALID_OPERATION; |
| 3032 | } |
| 3033 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 3034 | if (outputDesc->isDuplicated()) { |
| 3035 | ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice); |
| 3036 | return INVALID_OPERATION; |
| 3037 | } |
| 3038 | // create a special patch with no sink and two sources: |
| 3039 | // - the second source indicates to PatchPanel through which output mix this patch should |
| 3040 | // be connected as well as the stream type for volume control |
| 3041 | // - the sink is defined by whatever output device is currently selected for the output |
| 3042 | // though which this patch is routed. |
| 3043 | patch->num_sinks = 0; |
| 3044 | patch->num_sources = 2; |
| 3045 | srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL); |
| 3046 | outputDesc->toAudioPortConfig(&patch->sources[1], NULL); |
| 3047 | patch->sources[1].ext.mix.usecase.stream = stream; |
| 3048 | status_t status = mpClientInterface->createAudioPatch(patch, |
| 3049 | &afPatchHandle, |
| 3050 | 0); |
| 3051 | ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__, |
| 3052 | status, afPatchHandle); |
| 3053 | if (status != NO_ERROR) { |
| 3054 | ALOGW("%s patch panel could not connect device patch, error %d", |
| 3055 | __FUNCTION__, status); |
| 3056 | return INVALID_OPERATION; |
| 3057 | } |
| 3058 | uint32_t delayMs = 0; |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3059 | status = startSource(outputDesc, stream, sinkDevice, NULL, &delayMs); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3060 | |
| 3061 | if (status != NO_ERROR) { |
| 3062 | mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0); |
| 3063 | return status; |
| 3064 | } |
| 3065 | sourceDesc->mSwOutput = outputDesc; |
| 3066 | if (delayMs != 0) { |
| 3067 | usleep(delayMs * 1000); |
| 3068 | } |
| 3069 | } |
| 3070 | |
| 3071 | sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle; |
| 3072 | addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc); |
| 3073 | |
| 3074 | return NO_ERROR; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3075 | } |
| 3076 | |
Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 3077 | status_t AudioPolicyManager::stopAudioSource(audio_io_handle_t handle __unused) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3078 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3079 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle); |
| 3080 | ALOGV("%s handle %d", __FUNCTION__, handle); |
| 3081 | if (sourceDesc == 0) { |
| 3082 | ALOGW("%s unknown source for handle %d", __FUNCTION__, handle); |
| 3083 | return BAD_VALUE; |
| 3084 | } |
| 3085 | status_t status = disconnectAudioSource(sourceDesc); |
| 3086 | |
| 3087 | mAudioSources.removeItem(handle); |
| 3088 | return status; |
| 3089 | } |
| 3090 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3091 | status_t AudioPolicyManager::setMasterMono(bool mono) |
| 3092 | { |
| 3093 | if (mMasterMono == mono) { |
| 3094 | return NO_ERROR; |
| 3095 | } |
| 3096 | mMasterMono = mono; |
| 3097 | // if enabling mono we close all offloaded devices, which will invalidate the |
| 3098 | // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible |
| 3099 | // for recreating the new AudioTrack as non-offloaded PCM. |
| 3100 | // |
| 3101 | // If disabling mono, we leave all tracks as is: we don't know which clients |
| 3102 | // and tracks are able to be recreated as offloaded. The next "song" should |
| 3103 | // play back offloaded. |
| 3104 | if (mMasterMono) { |
| 3105 | Vector<audio_io_handle_t> offloaded; |
| 3106 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3107 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 3108 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
| 3109 | offloaded.push(desc->mIoHandle); |
| 3110 | } |
| 3111 | } |
| 3112 | for (size_t i = 0; i < offloaded.size(); ++i) { |
| 3113 | closeOutput(offloaded[i]); |
| 3114 | } |
| 3115 | } |
| 3116 | // update master mono for all remaining outputs |
| 3117 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3118 | updateMono(mOutputs.keyAt(i)); |
| 3119 | } |
| 3120 | return NO_ERROR; |
| 3121 | } |
| 3122 | |
| 3123 | status_t AudioPolicyManager::getMasterMono(bool *mono) |
| 3124 | { |
| 3125 | *mono = mMasterMono; |
| 3126 | return NO_ERROR; |
| 3127 | } |
| 3128 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3129 | status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc) |
| 3130 | { |
| 3131 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle()); |
| 3132 | |
| 3133 | sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle); |
| 3134 | if (patchDesc == 0) { |
| 3135 | ALOGW("%s source has no patch with handle %d", __FUNCTION__, |
| 3136 | sourceDesc->mPatchDesc->mHandle); |
| 3137 | return BAD_VALUE; |
| 3138 | } |
| 3139 | removeAudioPatch(sourceDesc->mPatchDesc->mHandle); |
| 3140 | |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 3141 | audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3142 | sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote(); |
| 3143 | if (swOutputDesc != 0) { |
| 3144 | stopSource(swOutputDesc, stream, false); |
| 3145 | mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3146 | } else { |
| 3147 | sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote(); |
| 3148 | if (hwOutputDesc != 0) { |
| 3149 | // release patch between src device and output device |
| 3150 | // close Hwoutput and remove from mHwOutputs |
| 3151 | } else { |
| 3152 | ALOGW("%s source has neither SW nor HW output", __FUNCTION__); |
| 3153 | } |
| 3154 | } |
| 3155 | return NO_ERROR; |
| 3156 | } |
| 3157 | |
| 3158 | sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput( |
| 3159 | audio_io_handle_t output, routing_strategy strategy) |
| 3160 | { |
| 3161 | sp<AudioSourceDescriptor> source; |
| 3162 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
| 3163 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 3164 | routing_strategy sourceStrategy = |
| 3165 | (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes); |
| 3166 | sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote(); |
| 3167 | if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) { |
| 3168 | source = sourceDesc; |
| 3169 | break; |
| 3170 | } |
| 3171 | } |
| 3172 | return source; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3173 | } |
| 3174 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3175 | // ---------------------------------------------------------------------------- |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3176 | // AudioPolicyManager |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3177 | // ---------------------------------------------------------------------------- |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3178 | uint32_t AudioPolicyManager::nextAudioPortGeneration() |
| 3179 | { |
| 3180 | return android_atomic_inc(&mAudioPortGeneration); |
| 3181 | } |
| 3182 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3183 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3184 | : |
| 3185 | #ifdef AUDIO_POLICY_TEST |
| 3186 | Thread(false), |
| 3187 | #endif //AUDIO_POLICY_TEST |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3188 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3189 | mA2dpSuspended(false), |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 3190 | mAudioPortGeneration(1), |
| 3191 | mBeaconMuteRefCount(0), |
| 3192 | mBeaconPlayingRefCount(0), |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3193 | mBeaconMuted(false), |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3194 | mTtsOutputAvailable(false), |
| 3195 | mMasterMono(false) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3196 | { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3197 | mUidCached = getuid(); |
| 3198 | mpClientInterface = clientInterface; |
| 3199 | |
| 3200 | // TODO: remove when legacy conf file is removed. true on devices that use DRC on the |
| 3201 | // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly. |
| 3202 | // Note: remove also speaker_drc_enabled from global configuration of XML config file. |
| 3203 | bool speakerDrcEnabled = false; |
| 3204 | |
| 3205 | #ifdef USE_XML_AUDIO_POLICY_CONF |
| 3206 | mVolumeCurves = new VolumeCurvesCollection(); |
| 3207 | AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices, |
| 3208 | mDefaultOutputDevice, speakerDrcEnabled, |
| 3209 | static_cast<VolumeCurvesCollection *>(mVolumeCurves)); |
| 3210 | PolicySerializer serializer; |
| 3211 | if (serializer.deserialize(AUDIO_POLICY_XML_CONFIG_FILE, config) != NO_ERROR) { |
| 3212 | #else |
| 3213 | mVolumeCurves = new StreamDescriptorCollection(); |
| 3214 | AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices, |
| 3215 | mDefaultOutputDevice, speakerDrcEnabled); |
| 3216 | if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, config) != NO_ERROR) && |
| 3217 | (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, config) != NO_ERROR)) { |
| 3218 | #endif |
| 3219 | ALOGE("could not load audio policy configuration file, setting defaults"); |
| 3220 | config.setDefault(); |
| 3221 | } |
| 3222 | // must be done after reading the policy (since conditionned by Speaker Drc Enabling) |
| 3223 | mVolumeCurves->initializeVolumeCurves(speakerDrcEnabled); |
| 3224 | |
| 3225 | // 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] | 3226 | audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance(); |
| 3227 | if (!engineInstance) { |
| 3228 | ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__); |
| 3229 | return; |
| 3230 | } |
| 3231 | // Retrieve the Policy Manager Interface |
| 3232 | mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>(); |
| 3233 | if (mEngine == NULL) { |
| 3234 | ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__); |
| 3235 | return; |
| 3236 | } |
| 3237 | mEngine->setObserver(this); |
| 3238 | status_t status = mEngine->initCheck(); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 3239 | (void) status; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3240 | ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status); |
| 3241 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3242 | // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3243 | // open all output streams needed to access attached devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3244 | audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types(); |
| 3245 | audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3246 | for (size_t i = 0; i < mHwModules.size(); i++) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3247 | mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3248 | if (mHwModules[i]->mHandle == 0) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3249 | ALOGW("could not open HW module %s", mHwModules[i]->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3250 | continue; |
| 3251 | } |
| 3252 | // open all output streams needed to access attached devices |
| 3253 | // except for direct output streams that are only opened when they are actually |
| 3254 | // required by an app. |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3255 | // This also validates mAvailableOutputDevices list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3256 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 3257 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3258 | const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3259 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3260 | if (!outProfile->hasSupportedDevices()) { |
| 3261 | ALOGW("Output profile contains no device on module %s", mHwModules[i]->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3262 | continue; |
| 3263 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3264 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3265 | mTtsOutputAvailable = true; |
| 3266 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3267 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3268 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3269 | continue; |
| 3270 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3271 | audio_devices_t profileType = outProfile->getSupportedDevicesType(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3272 | if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) { |
| 3273 | profileType = mDefaultOutputDevice->type(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3274 | } else { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3275 | // chose first device present in profile's SupportedDevices also part of |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3276 | // outputDeviceTypes |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3277 | profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3278 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3279 | if ((profileType & outputDeviceTypes) == 0) { |
| 3280 | continue; |
| 3281 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3282 | sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile, |
| 3283 | mpClientInterface); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3284 | const DeviceVector &supportedDevices = outProfile->getSupportedDevices(); |
| 3285 | const DeviceVector &devicesForType = supportedDevices.getDevicesFromType(profileType); |
| 3286 | String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress |
| 3287 | : String8(""); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3288 | |
| 3289 | outputDesc->mDevice = profileType; |
| 3290 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3291 | config.sample_rate = outputDesc->mSamplingRate; |
| 3292 | config.channel_mask = outputDesc->mChannelMask; |
| 3293 | config.format = outputDesc->mFormat; |
| 3294 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3295 | status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(), |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3296 | &output, |
| 3297 | &config, |
| 3298 | &outputDesc->mDevice, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3299 | address, |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3300 | &outputDesc->mLatency, |
| 3301 | outputDesc->mFlags); |
| 3302 | |
| 3303 | if (status != NO_ERROR) { |
| 3304 | ALOGW("Cannot open output stream for device %08x on hw module %s", |
| 3305 | outputDesc->mDevice, |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3306 | mHwModules[i]->getName()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3307 | } else { |
| 3308 | outputDesc->mSamplingRate = config.sample_rate; |
| 3309 | outputDesc->mChannelMask = config.channel_mask; |
| 3310 | outputDesc->mFormat = config.format; |
| 3311 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3312 | for (size_t k = 0; k < supportedDevices.size(); k++) { |
| 3313 | ssize_t index = mAvailableOutputDevices.indexOf(supportedDevices[k]); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3314 | // 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] | 3315 | if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) { |
| 3316 | mAvailableOutputDevices[index]->attach(mHwModules[i]); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3317 | } |
| 3318 | } |
| 3319 | if (mPrimaryOutput == 0 && |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3320 | outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3321 | mPrimaryOutput = outputDesc; |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3322 | } |
| 3323 | addOutput(output, outputDesc); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3324 | setOutputDevice(outputDesc, |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3325 | outputDesc->mDevice, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3326 | true, |
| 3327 | 0, |
| 3328 | NULL, |
| 3329 | address.string()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3330 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3331 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3332 | // open input streams needed to access attached devices to validate |
| 3333 | // mAvailableInputDevices list |
| 3334 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) |
| 3335 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3336 | const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3337 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3338 | if (!inProfile->hasSupportedDevices()) { |
| 3339 | ALOGW("Input profile contains no device on module %s", mHwModules[i]->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3340 | continue; |
| 3341 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3342 | // chose first device present in profile's SupportedDevices also part of |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3343 | // inputDeviceTypes |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3344 | audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes); |
| 3345 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3346 | if ((profileType & inputDeviceTypes) == 0) { |
| 3347 | continue; |
| 3348 | } |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 3349 | sp<AudioInputDescriptor> inputDesc = |
| 3350 | new AudioInputDescriptor(inProfile); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3351 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3352 | inputDesc->mDevice = profileType; |
| 3353 | |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 3354 | // find the address |
| 3355 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType); |
| 3356 | // the inputs vector must be of size 1, but we don't want to crash here |
| 3357 | String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress |
| 3358 | : String8(""); |
| 3359 | ALOGV(" for input device 0x%x using address %s", profileType, address.string()); |
| 3360 | ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!"); |
| 3361 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3362 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3363 | config.sample_rate = inputDesc->mSamplingRate; |
| 3364 | config.channel_mask = inputDesc->mChannelMask; |
| 3365 | config.format = inputDesc->mFormat; |
| 3366 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3367 | status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(), |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3368 | &input, |
| 3369 | &config, |
| 3370 | &inputDesc->mDevice, |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 3371 | address, |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3372 | AUDIO_SOURCE_MIC, |
| 3373 | AUDIO_INPUT_FLAG_NONE); |
| 3374 | |
| 3375 | if (status == NO_ERROR) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3376 | const DeviceVector &supportedDevices = inProfile->getSupportedDevices(); |
| 3377 | for (size_t k = 0; k < supportedDevices.size(); k++) { |
| 3378 | ssize_t index = mAvailableInputDevices.indexOf(supportedDevices[k]); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3379 | // 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] | 3380 | if (index >= 0) { |
| 3381 | sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index]; |
| 3382 | if (!devDesc->isAttached()) { |
| 3383 | devDesc->attach(mHwModules[i]); |
| 3384 | devDesc->importAudioPort(inProfile); |
| 3385 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3386 | } |
| 3387 | } |
| 3388 | mpClientInterface->closeInput(input); |
| 3389 | } else { |
| 3390 | ALOGW("Cannot open input stream for device %08x on hw module %s", |
| 3391 | inputDesc->mDevice, |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3392 | mHwModules[i]->getName()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3393 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3394 | } |
| 3395 | } |
| 3396 | // make sure all attached devices have been allocated a unique ID |
| 3397 | for (size_t i = 0; i < mAvailableOutputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 3398 | if (!mAvailableOutputDevices[i]->isAttached()) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3399 | ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3400 | mAvailableOutputDevices.remove(mAvailableOutputDevices[i]); |
| 3401 | continue; |
| 3402 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3403 | // The device is now validated and can be appended to the available devices of the engine |
| 3404 | mEngine->setDeviceConnectionState(mAvailableOutputDevices[i], |
| 3405 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3406 | i++; |
| 3407 | } |
| 3408 | for (size_t i = 0; i < mAvailableInputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 3409 | if (!mAvailableInputDevices[i]->isAttached()) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3410 | ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3411 | mAvailableInputDevices.remove(mAvailableInputDevices[i]); |
| 3412 | continue; |
| 3413 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3414 | // The device is now validated and can be appended to the available devices of the engine |
| 3415 | mEngine->setDeviceConnectionState(mAvailableInputDevices[i], |
| 3416 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3417 | i++; |
| 3418 | } |
| 3419 | // make sure default device is reachable |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3420 | if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3421 | ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3422 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3423 | |
| 3424 | ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output"); |
| 3425 | |
| 3426 | updateDevicesAndOutputs(); |
| 3427 | |
| 3428 | #ifdef AUDIO_POLICY_TEST |
| 3429 | if (mPrimaryOutput != 0) { |
| 3430 | AudioParameter outputCmd = AudioParameter(); |
| 3431 | outputCmd.addInt(String8("set_id"), 0); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3432 | mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3433 | |
| 3434 | mTestDevice = AUDIO_DEVICE_OUT_SPEAKER; |
| 3435 | mTestSamplingRate = 44100; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3436 | mTestFormat = AUDIO_FORMAT_PCM_16_BIT; |
| 3437 | mTestChannels = AUDIO_CHANNEL_OUT_STEREO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3438 | mTestLatencyMs = 0; |
| 3439 | mCurOutput = 0; |
| 3440 | mDirectOutput = false; |
| 3441 | for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { |
| 3442 | mTestOutputs[i] = 0; |
| 3443 | } |
| 3444 | |
| 3445 | const size_t SIZE = 256; |
| 3446 | char buffer[SIZE]; |
| 3447 | snprintf(buffer, SIZE, "AudioPolicyManagerTest"); |
| 3448 | run(buffer, ANDROID_PRIORITY_AUDIO); |
| 3449 | } |
| 3450 | #endif //AUDIO_POLICY_TEST |
| 3451 | } |
| 3452 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3453 | AudioPolicyManager::~AudioPolicyManager() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3454 | { |
| 3455 | #ifdef AUDIO_POLICY_TEST |
| 3456 | exit(); |
| 3457 | #endif //AUDIO_POLICY_TEST |
| 3458 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3459 | mpClientInterface->closeOutput(mOutputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3460 | } |
| 3461 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3462 | mpClientInterface->closeInput(mInputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3463 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3464 | mAvailableOutputDevices.clear(); |
| 3465 | mAvailableInputDevices.clear(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3466 | mOutputs.clear(); |
| 3467 | mInputs.clear(); |
| 3468 | mHwModules.clear(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3469 | } |
| 3470 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3471 | status_t AudioPolicyManager::initCheck() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3472 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 3473 | return hasPrimaryOutput() ? NO_ERROR : NO_INIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3474 | } |
| 3475 | |
| 3476 | #ifdef AUDIO_POLICY_TEST |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3477 | bool AudioPolicyManager::threadLoop() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3478 | { |
| 3479 | ALOGV("entering threadLoop()"); |
| 3480 | while (!exitPending()) |
| 3481 | { |
| 3482 | String8 command; |
| 3483 | int valueInt; |
| 3484 | String8 value; |
| 3485 | |
| 3486 | Mutex::Autolock _l(mLock); |
| 3487 | mWaitWorkCV.waitRelative(mLock, milliseconds(50)); |
| 3488 | |
| 3489 | command = mpClientInterface->getParameters(0, String8("test_cmd_policy")); |
| 3490 | AudioParameter param = AudioParameter(command); |
| 3491 | |
| 3492 | if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR && |
| 3493 | valueInt != 0) { |
| 3494 | ALOGV("Test command %s received", command.string()); |
| 3495 | String8 target; |
| 3496 | if (param.get(String8("target"), target) != NO_ERROR) { |
| 3497 | target = "Manager"; |
| 3498 | } |
| 3499 | if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) { |
| 3500 | param.remove(String8("test_cmd_policy_output")); |
| 3501 | mCurOutput = valueInt; |
| 3502 | } |
| 3503 | if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) { |
| 3504 | param.remove(String8("test_cmd_policy_direct")); |
| 3505 | if (value == "false") { |
| 3506 | mDirectOutput = false; |
| 3507 | } else if (value == "true") { |
| 3508 | mDirectOutput = true; |
| 3509 | } |
| 3510 | } |
| 3511 | if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) { |
| 3512 | param.remove(String8("test_cmd_policy_input")); |
| 3513 | mTestInput = valueInt; |
| 3514 | } |
| 3515 | |
| 3516 | if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) { |
| 3517 | param.remove(String8("test_cmd_policy_format")); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3518 | int format = AUDIO_FORMAT_INVALID; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3519 | if (value == "PCM 16 bits") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3520 | format = AUDIO_FORMAT_PCM_16_BIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3521 | } else if (value == "PCM 8 bits") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3522 | format = AUDIO_FORMAT_PCM_8_BIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3523 | } else if (value == "Compressed MP3") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3524 | format = AUDIO_FORMAT_MP3; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3525 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3526 | if (format != AUDIO_FORMAT_INVALID) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3527 | if (target == "Manager") { |
| 3528 | mTestFormat = format; |
| 3529 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 3530 | AudioParameter outputParam = AudioParameter(); |
| 3531 | outputParam.addInt(String8("format"), format); |
| 3532 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 3533 | } |
| 3534 | } |
| 3535 | } |
| 3536 | if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) { |
| 3537 | param.remove(String8("test_cmd_policy_channels")); |
| 3538 | int channels = 0; |
| 3539 | |
| 3540 | if (value == "Channels Stereo") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3541 | channels = AUDIO_CHANNEL_OUT_STEREO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3542 | } else if (value == "Channels Mono") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3543 | channels = AUDIO_CHANNEL_OUT_MONO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3544 | } |
| 3545 | if (channels != 0) { |
| 3546 | if (target == "Manager") { |
| 3547 | mTestChannels = channels; |
| 3548 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 3549 | AudioParameter outputParam = AudioParameter(); |
| 3550 | outputParam.addInt(String8("channels"), channels); |
| 3551 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 3552 | } |
| 3553 | } |
| 3554 | } |
| 3555 | if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) { |
| 3556 | param.remove(String8("test_cmd_policy_sampleRate")); |
| 3557 | if (valueInt >= 0 && valueInt <= 96000) { |
| 3558 | int samplingRate = valueInt; |
| 3559 | if (target == "Manager") { |
| 3560 | mTestSamplingRate = samplingRate; |
| 3561 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 3562 | AudioParameter outputParam = AudioParameter(); |
| 3563 | outputParam.addInt(String8("sampling_rate"), samplingRate); |
| 3564 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 3565 | } |
| 3566 | } |
| 3567 | } |
| 3568 | |
| 3569 | if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) { |
| 3570 | param.remove(String8("test_cmd_policy_reopen")); |
| 3571 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3572 | mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput);); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3573 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3574 | audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3575 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3576 | removeOutput(mPrimaryOutput->mIoHandle); |
| 3577 | sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL, |
| 3578 | mpClientInterface); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3579 | outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3580 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3581 | config.sample_rate = outputDesc->mSamplingRate; |
| 3582 | config.channel_mask = outputDesc->mChannelMask; |
| 3583 | config.format = outputDesc->mFormat; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3584 | audio_io_handle_t handle; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3585 | status_t status = mpClientInterface->openOutput(moduleHandle, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3586 | &handle, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3587 | &config, |
| 3588 | &outputDesc->mDevice, |
| 3589 | String8(""), |
| 3590 | &outputDesc->mLatency, |
| 3591 | outputDesc->mFlags); |
| 3592 | if (status != NO_ERROR) { |
| 3593 | ALOGE("Failed to reopen hardware output stream, " |
| 3594 | "samplingRate: %d, format %d, channels %d", |
| 3595 | outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3596 | } else { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3597 | outputDesc->mSamplingRate = config.sample_rate; |
| 3598 | outputDesc->mChannelMask = config.channel_mask; |
| 3599 | outputDesc->mFormat = config.format; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3600 | mPrimaryOutput = outputDesc; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3601 | AudioParameter outputCmd = AudioParameter(); |
| 3602 | outputCmd.addInt(String8("set_id"), 0); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3603 | mpClientInterface->setParameters(handle, outputCmd.toString()); |
| 3604 | addOutput(handle, outputDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3605 | } |
| 3606 | } |
| 3607 | |
| 3608 | |
| 3609 | mpClientInterface->setParameters(0, String8("test_cmd_policy=")); |
| 3610 | } |
| 3611 | } |
| 3612 | return false; |
| 3613 | } |
| 3614 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3615 | void AudioPolicyManager::exit() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3616 | { |
| 3617 | { |
| 3618 | AutoMutex _l(mLock); |
| 3619 | requestExit(); |
| 3620 | mWaitWorkCV.signal(); |
| 3621 | } |
| 3622 | requestExitAndWait(); |
| 3623 | } |
| 3624 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3625 | int AudioPolicyManager::testOutputIndex(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3626 | { |
| 3627 | for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { |
| 3628 | if (output == mTestOutputs[i]) return i; |
| 3629 | } |
| 3630 | return 0; |
| 3631 | } |
| 3632 | #endif //AUDIO_POLICY_TEST |
| 3633 | |
| 3634 | // --- |
| 3635 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3636 | void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3637 | { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 3638 | outputDesc->setIoHandle(output); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3639 | mOutputs.add(output, outputDesc); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3640 | updateMono(output); // update mono status when adding to output list |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3641 | nextAudioPortGeneration(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3642 | } |
| 3643 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3644 | void AudioPolicyManager::removeOutput(audio_io_handle_t output) |
| 3645 | { |
| 3646 | mOutputs.removeItem(output); |
| 3647 | } |
| 3648 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3649 | void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3650 | { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 3651 | inputDesc->setIoHandle(input); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3652 | mInputs.add(input, inputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3653 | nextAudioPortGeneration(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3654 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3655 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3656 | void AudioPolicyManager::findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/, |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3657 | const audio_devices_t device /*in*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3658 | const String8 address /*in*/, |
| 3659 | SortedVector<audio_io_handle_t>& outputs /*out*/) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3660 | sp<DeviceDescriptor> devDesc = |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3661 | desc->mProfile->getSupportedDeviceByAddress(device, address); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3662 | if (devDesc != 0) { |
| 3663 | ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s", |
| 3664 | desc->mIoHandle, address.string()); |
| 3665 | outputs.add(desc->mIoHandle); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3666 | } |
| 3667 | } |
| 3668 | |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3669 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3670 | audio_policy_dev_state_t state, |
| 3671 | SortedVector<audio_io_handle_t>& outputs, |
| 3672 | const String8 address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3673 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3674 | audio_devices_t device = devDesc->type(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3675 | sp<SwAudioOutputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 3676 | |
| 3677 | if (audio_device_is_digital(device)) { |
| 3678 | // erase all current sample rates, formats and channel masks |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 3679 | devDesc->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 3680 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3681 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3682 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3683 | // first list already open outputs that can be routed to this device |
| 3684 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3685 | desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3686 | if (!desc->isDuplicated() && (desc->supportedDevices() & device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3687 | if (!device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3688 | ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i)); |
| 3689 | outputs.add(mOutputs.keyAt(i)); |
| 3690 | } else { |
| 3691 | ALOGV(" checking address match due to device 0x%x", device); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3692 | findIoHandlesByAddress(desc, device, address, outputs); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3693 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3694 | } |
| 3695 | } |
| 3696 | // then look for output profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3697 | SortedVector< sp<IOProfile> > profiles; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3698 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 3699 | { |
| 3700 | if (mHwModules[i]->mHandle == 0) { |
| 3701 | continue; |
| 3702 | } |
| 3703 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 3704 | { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3705 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3706 | if (profile->supportDevice(device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3707 | if (!device_distinguishes_on_address(device) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3708 | profile->supportDeviceAddress(address)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3709 | profiles.add(profile); |
| 3710 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i); |
| 3711 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3712 | } |
| 3713 | } |
| 3714 | } |
| 3715 | |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 3716 | ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size()); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3717 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3718 | if (profiles.isEmpty() && outputs.isEmpty()) { |
| 3719 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 3720 | return BAD_VALUE; |
| 3721 | } |
| 3722 | |
| 3723 | // open outputs for matching profiles if needed. Direct outputs are also opened to |
| 3724 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 3725 | 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] | 3726 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3727 | |
| 3728 | // nothing to do if one output is already opened for this profile |
| 3729 | size_t j; |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3730 | for (j = 0; j < outputs.size(); j++) { |
| 3731 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3732 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3733 | // matching profile: save the sample rates, format and channel masks supported |
| 3734 | // by the profile in our device descriptor |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3735 | if (audio_device_is_digital(device)) { |
| 3736 | devDesc->importAudioPort(profile); |
| 3737 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3738 | break; |
| 3739 | } |
| 3740 | } |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3741 | if (j != outputs.size()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3742 | continue; |
| 3743 | } |
| 3744 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3745 | ALOGV("opening output for device %08x with params %s profile %p", |
| 3746 | device, address.string(), profile.get()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3747 | desc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3748 | desc->mDevice = device; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3749 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3750 | config.sample_rate = desc->mSamplingRate; |
| 3751 | config.channel_mask = desc->mChannelMask; |
| 3752 | config.format = desc->mFormat; |
| 3753 | config.offload_info.sample_rate = desc->mSamplingRate; |
| 3754 | config.offload_info.channel_mask = desc->mChannelMask; |
| 3755 | config.offload_info.format = desc->mFormat; |
| 3756 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3757 | status_t status = mpClientInterface->openOutput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3758 | &output, |
| 3759 | &config, |
| 3760 | &desc->mDevice, |
| 3761 | address, |
| 3762 | &desc->mLatency, |
| 3763 | desc->mFlags); |
| 3764 | if (status == NO_ERROR) { |
| 3765 | desc->mSamplingRate = config.sample_rate; |
| 3766 | desc->mChannelMask = config.channel_mask; |
| 3767 | desc->mFormat = config.format; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3768 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3769 | // Here is where the out_set_parameters() for card & device gets called |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3770 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3771 | char *param = audio_device_address_to_parameter(device, address); |
| 3772 | mpClientInterface->setParameters(output, String8(param)); |
| 3773 | free(param); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3774 | } |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 3775 | updateAudioProfiles(device, output, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3776 | if (!profile->hasValidAudioProfile()) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3777 | ALOGW("checkOutputsForDevice() missing param"); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3778 | mpClientInterface->closeOutput(output); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3779 | output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3780 | } else if (profile->hasDynamicAudioProfile()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3781 | mpClientInterface->closeOutput(output); |
Phil Burk | 702b105 | 2016-03-02 16:38:26 -0800 | [diff] [blame] | 3782 | output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3783 | profile->pickAudioProfile(config.sample_rate, config.channel_mask, config.format); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3784 | config.offload_info.sample_rate = config.sample_rate; |
| 3785 | config.offload_info.channel_mask = config.channel_mask; |
| 3786 | config.offload_info.format = config.format; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3787 | status = mpClientInterface->openOutput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3788 | &output, |
| 3789 | &config, |
| 3790 | &desc->mDevice, |
| 3791 | address, |
| 3792 | &desc->mLatency, |
| 3793 | desc->mFlags); |
| 3794 | if (status == NO_ERROR) { |
| 3795 | desc->mSamplingRate = config.sample_rate; |
| 3796 | desc->mChannelMask = config.channel_mask; |
| 3797 | desc->mFormat = config.format; |
| 3798 | } else { |
| 3799 | output = AUDIO_IO_HANDLE_NONE; |
| 3800 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3801 | } |
| 3802 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3803 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3804 | addOutput(output, desc); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3805 | if (device_distinguishes_on_address(device) && address != "0") { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3806 | sp<AudioPolicyMix> policyMix; |
| 3807 | if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3808 | ALOGE("checkOutputsForDevice() cannot find policy for address %s", |
| 3809 | address.string()); |
| 3810 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3811 | policyMix->setOutput(desc); |
Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 3812 | desc->mPolicyMix = policyMix->getMix(); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3813 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 3814 | } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
| 3815 | hasPrimaryOutput()) { |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 3816 | // no duplicated output for direct outputs and |
| 3817 | // outputs used by dynamic policy mixes |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3818 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3819 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3820 | // set initial stream volume for device |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3821 | applyStreamVolumes(desc, device, 0, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3822 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3823 | //TODO: configure audio effect output stage here |
| 3824 | |
| 3825 | // 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] | 3826 | duplicatedOutput = |
| 3827 | mpClientInterface->openDuplicateOutput(output, |
| 3828 | mPrimaryOutput->mIoHandle); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3829 | if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3830 | // add duplicated output descriptor |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3831 | sp<SwAudioOutputDescriptor> dupOutputDesc = |
| 3832 | new SwAudioOutputDescriptor(NULL, mpClientInterface); |
| 3833 | dupOutputDesc->mOutput1 = mPrimaryOutput; |
| 3834 | dupOutputDesc->mOutput2 = desc; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3835 | dupOutputDesc->mSamplingRate = desc->mSamplingRate; |
| 3836 | dupOutputDesc->mFormat = desc->mFormat; |
| 3837 | dupOutputDesc->mChannelMask = desc->mChannelMask; |
| 3838 | dupOutputDesc->mLatency = desc->mLatency; |
| 3839 | addOutput(duplicatedOutput, dupOutputDesc); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3840 | applyStreamVolumes(dupOutputDesc, device, 0, true); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3841 | } else { |
| 3842 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3843 | mPrimaryOutput->mIoHandle, output); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3844 | mpClientInterface->closeOutput(output); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3845 | removeOutput(output); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3846 | nextAudioPortGeneration(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3847 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3848 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3849 | } |
| 3850 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3851 | } else { |
| 3852 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3853 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3854 | if (output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3855 | ALOGW("checkOutputsForDevice() could not open output for device %x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3856 | profiles.removeAt(profile_index); |
| 3857 | profile_index--; |
| 3858 | } else { |
| 3859 | outputs.add(output); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3860 | // Load digital format info only for digital devices |
| 3861 | if (audio_device_is_digital(device)) { |
| 3862 | devDesc->importAudioPort(profile); |
| 3863 | } |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3864 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3865 | if (device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3866 | ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)", |
| 3867 | device, address.string()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3868 | setOutputDevice(desc, device, true/*force*/, 0/*delay*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3869 | NULL/*patch handle*/, address.string()); |
| 3870 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3871 | ALOGV("checkOutputsForDevice(): adding output %d", output); |
| 3872 | } |
| 3873 | } |
| 3874 | |
| 3875 | if (profiles.isEmpty()) { |
| 3876 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 3877 | return BAD_VALUE; |
| 3878 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3879 | } else { // Disconnect |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3880 | // check if one opened output is not needed any more after disconnecting one device |
| 3881 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3882 | desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3883 | if (!desc->isDuplicated()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3884 | // exact match on device |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3885 | if (device_distinguishes_on_address(device) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3886 | (desc->supportedDevices() == device)) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3887 | findIoHandlesByAddress(desc, device, address, outputs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3888 | } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3889 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", |
| 3890 | mOutputs.keyAt(i)); |
| 3891 | outputs.add(mOutputs.keyAt(i)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3892 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3893 | } |
| 3894 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3895 | // Clear any profiles associated with the disconnected device. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3896 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 3897 | { |
| 3898 | if (mHwModules[i]->mHandle == 0) { |
| 3899 | continue; |
| 3900 | } |
| 3901 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 3902 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3903 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3904 | if (profile->supportDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3905 | ALOGV("checkOutputsForDevice(): " |
| 3906 | "clearing direct output profile %zu on module %zu", j, i); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3907 | profile->clearAudioProfiles(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3908 | } |
| 3909 | } |
| 3910 | } |
| 3911 | } |
| 3912 | return NO_ERROR; |
| 3913 | } |
| 3914 | |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3915 | status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor> devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3916 | audio_policy_dev_state_t state, |
| 3917 | SortedVector<audio_io_handle_t>& inputs, |
| 3918 | const String8 address) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3919 | { |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3920 | audio_devices_t device = devDesc->type(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3921 | sp<AudioInputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 3922 | |
| 3923 | if (audio_device_is_digital(device)) { |
| 3924 | // erase all current sample rates, formats and channel masks |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 3925 | devDesc->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 3926 | } |
| 3927 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3928 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 3929 | // first list already open inputs that can be routed to this device |
| 3930 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 3931 | desc = mInputs.valueAt(input_index); |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3932 | if (desc->mProfile->supportDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3933 | ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index)); |
| 3934 | inputs.add(mInputs.keyAt(input_index)); |
| 3935 | } |
| 3936 | } |
| 3937 | |
| 3938 | // then look for input profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3939 | SortedVector< sp<IOProfile> > profiles; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3940 | for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++) |
| 3941 | { |
| 3942 | if (mHwModules[module_idx]->mHandle == 0) { |
| 3943 | continue; |
| 3944 | } |
| 3945 | for (size_t profile_index = 0; |
| 3946 | profile_index < mHwModules[module_idx]->mInputProfiles.size(); |
| 3947 | profile_index++) |
| 3948 | { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3949 | sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index]; |
| 3950 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3951 | if (profile->supportDevice(device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3952 | if (!device_distinguishes_on_address(device) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3953 | profile->supportDeviceAddress(address)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3954 | profiles.add(profile); |
| 3955 | ALOGV("checkInputsForDevice(): adding profile %zu from module %zu", |
| 3956 | profile_index, module_idx); |
| 3957 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3958 | } |
| 3959 | } |
| 3960 | } |
| 3961 | |
| 3962 | if (profiles.isEmpty() && inputs.isEmpty()) { |
| 3963 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 3964 | return BAD_VALUE; |
| 3965 | } |
| 3966 | |
| 3967 | // open inputs for matching profiles if needed. Direct inputs are also opened to |
| 3968 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 3969 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { |
| 3970 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3971 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3972 | // nothing to do if one input is already opened for this profile |
| 3973 | size_t input_index; |
| 3974 | for (input_index = 0; input_index < mInputs.size(); input_index++) { |
| 3975 | desc = mInputs.valueAt(input_index); |
| 3976 | if (desc->mProfile == profile) { |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3977 | if (audio_device_is_digital(device)) { |
| 3978 | devDesc->importAudioPort(profile); |
| 3979 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3980 | break; |
| 3981 | } |
| 3982 | } |
| 3983 | if (input_index != mInputs.size()) { |
| 3984 | continue; |
| 3985 | } |
| 3986 | |
| 3987 | ALOGV("opening input for device 0x%X with params %s", device, address.string()); |
| 3988 | desc = new AudioInputDescriptor(profile); |
| 3989 | desc->mDevice = device; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3990 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3991 | config.sample_rate = desc->mSamplingRate; |
| 3992 | config.channel_mask = desc->mChannelMask; |
| 3993 | config.format = desc->mFormat; |
| 3994 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3995 | status_t status = mpClientInterface->openInput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3996 | &input, |
| 3997 | &config, |
| 3998 | &desc->mDevice, |
| 3999 | address, |
| 4000 | AUDIO_SOURCE_MIC, |
| 4001 | AUDIO_INPUT_FLAG_NONE /*FIXME*/); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4002 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4003 | if (status == NO_ERROR) { |
| 4004 | desc->mSamplingRate = config.sample_rate; |
| 4005 | desc->mChannelMask = config.channel_mask; |
| 4006 | desc->mFormat = config.format; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4007 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4008 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4009 | char *param = audio_device_address_to_parameter(device, address); |
| 4010 | mpClientInterface->setParameters(input, String8(param)); |
| 4011 | free(param); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4012 | } |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 4013 | updateAudioProfiles(device, input, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4014 | if (!profile->hasValidAudioProfile()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4015 | ALOGW("checkInputsForDevice() direct input missing param"); |
| 4016 | mpClientInterface->closeInput(input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4017 | input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4018 | } |
| 4019 | |
| 4020 | if (input != 0) { |
| 4021 | addInput(input, desc); |
| 4022 | } |
| 4023 | } // endif input != 0 |
| 4024 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4025 | if (input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4026 | ALOGW("checkInputsForDevice() could not open input for device 0x%X", device); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4027 | profiles.removeAt(profile_index); |
| 4028 | profile_index--; |
| 4029 | } else { |
| 4030 | inputs.add(input); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4031 | if (audio_device_is_digital(device)) { |
| 4032 | devDesc->importAudioPort(profile); |
| 4033 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4034 | ALOGV("checkInputsForDevice(): adding input %d", input); |
| 4035 | } |
| 4036 | } // end scan profiles |
| 4037 | |
| 4038 | if (profiles.isEmpty()) { |
| 4039 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 4040 | return BAD_VALUE; |
| 4041 | } |
| 4042 | } else { |
| 4043 | // Disconnect |
| 4044 | // check if one opened input is not needed any more after disconnecting one device |
| 4045 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4046 | desc = mInputs.valueAt(input_index); |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4047 | if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4048 | ALOGV("checkInputsForDevice(): disconnecting adding input %d", |
| 4049 | mInputs.keyAt(input_index)); |
| 4050 | inputs.add(mInputs.keyAt(input_index)); |
| 4051 | } |
| 4052 | } |
| 4053 | // Clear any profiles associated with the disconnected device. |
| 4054 | for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) { |
| 4055 | if (mHwModules[module_index]->mHandle == 0) { |
| 4056 | continue; |
| 4057 | } |
| 4058 | for (size_t profile_index = 0; |
| 4059 | profile_index < mHwModules[module_index]->mInputProfiles.size(); |
| 4060 | profile_index++) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4061 | sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4062 | if (profile->supportDevice(device)) { |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 4063 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu", |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4064 | profile_index, module_index); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4065 | profile->clearAudioProfiles(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4066 | } |
| 4067 | } |
| 4068 | } |
| 4069 | } // end disconnect |
| 4070 | |
| 4071 | return NO_ERROR; |
| 4072 | } |
| 4073 | |
| 4074 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4075 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4076 | { |
| 4077 | ALOGV("closeOutput(%d)", output); |
| 4078 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4079 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4080 | if (outputDesc == NULL) { |
| 4081 | ALOGW("closeOutput() unknown output %d", output); |
| 4082 | return; |
| 4083 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4084 | mPolicyMixes.closeOutput(outputDesc); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4085 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4086 | // look for duplicated outputs connected to the output being removed. |
| 4087 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4088 | sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4089 | if (dupOutputDesc->isDuplicated() && |
| 4090 | (dupOutputDesc->mOutput1 == outputDesc || |
| 4091 | dupOutputDesc->mOutput2 == outputDesc)) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4092 | sp<AudioOutputDescriptor> outputDesc2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4093 | if (dupOutputDesc->mOutput1 == outputDesc) { |
| 4094 | outputDesc2 = dupOutputDesc->mOutput2; |
| 4095 | } else { |
| 4096 | outputDesc2 = dupOutputDesc->mOutput1; |
| 4097 | } |
| 4098 | // As all active tracks on duplicated output will be deleted, |
| 4099 | // and as they were also referenced on the other output, the reference |
| 4100 | // count for their stream type must be adjusted accordingly on |
| 4101 | // the other output. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4102 | for (int j = 0; j < AUDIO_STREAM_CNT; j++) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4103 | int refCount = dupOutputDesc->mRefCount[j]; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4104 | outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4105 | } |
| 4106 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); |
| 4107 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); |
| 4108 | |
| 4109 | mpClientInterface->closeOutput(duplicatedOutput); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4110 | removeOutput(duplicatedOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4111 | } |
| 4112 | } |
| 4113 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4114 | nextAudioPortGeneration(); |
| 4115 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4116 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4117 | if (index >= 0) { |
| 4118 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4119 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4120 | mAudioPatches.removeItemsAt(index); |
| 4121 | mpClientInterface->onAudioPatchListUpdate(); |
| 4122 | } |
| 4123 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4124 | AudioParameter param; |
| 4125 | param.add(String8("closing"), String8("true")); |
| 4126 | mpClientInterface->setParameters(output, param.toString()); |
| 4127 | |
| 4128 | mpClientInterface->closeOutput(output); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4129 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4130 | mPreviousOutputs = mOutputs; |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4131 | } |
| 4132 | |
| 4133 | void AudioPolicyManager::closeInput(audio_io_handle_t input) |
| 4134 | { |
| 4135 | ALOGV("closeInput(%d)", input); |
| 4136 | |
| 4137 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
| 4138 | if (inputDesc == NULL) { |
| 4139 | ALOGW("closeInput() unknown input %d", input); |
| 4140 | return; |
| 4141 | } |
| 4142 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4143 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4144 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4145 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4146 | if (index >= 0) { |
| 4147 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4148 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4149 | mAudioPatches.removeItemsAt(index); |
| 4150 | mpClientInterface->onAudioPatchListUpdate(); |
| 4151 | } |
| 4152 | |
| 4153 | mpClientInterface->closeInput(input); |
| 4154 | mInputs.removeItem(input); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4155 | } |
| 4156 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4157 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice( |
| 4158 | audio_devices_t device, |
| 4159 | SwAudioOutputCollection openOutputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4160 | { |
| 4161 | SortedVector<audio_io_handle_t> outputs; |
| 4162 | |
| 4163 | ALOGVV("getOutputsForDevice() device %04x", device); |
| 4164 | for (size_t i = 0; i < openOutputs.size(); i++) { |
| 4165 | ALOGVV("output %d isDuplicated=%d device=%04x", |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4166 | i, openOutputs.valueAt(i)->isDuplicated(), |
| 4167 | openOutputs.valueAt(i)->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4168 | if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) { |
| 4169 | ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i)); |
| 4170 | outputs.add(openOutputs.keyAt(i)); |
| 4171 | } |
| 4172 | } |
| 4173 | return outputs; |
| 4174 | } |
| 4175 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4176 | bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4177 | SortedVector<audio_io_handle_t>& outputs2) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4178 | { |
| 4179 | if (outputs1.size() != outputs2.size()) { |
| 4180 | return false; |
| 4181 | } |
| 4182 | for (size_t i = 0; i < outputs1.size(); i++) { |
| 4183 | if (outputs1[i] != outputs2[i]) { |
| 4184 | return false; |
| 4185 | } |
| 4186 | } |
| 4187 | return true; |
| 4188 | } |
| 4189 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4190 | void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4191 | { |
| 4192 | audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/); |
| 4193 | audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 4194 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs); |
| 4195 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs); |
| 4196 | |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4197 | // also take into account external policy-related changes: add all outputs which are |
| 4198 | // associated with policies in the "before" and "after" output vectors |
| 4199 | ALOGVV("checkOutputForStrategy(): policy related outputs"); |
| 4200 | for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4201 | const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4202 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4203 | srcOutputs.add(desc->mIoHandle); |
| 4204 | ALOGVV(" previous outputs: adding %d", desc->mIoHandle); |
| 4205 | } |
| 4206 | } |
| 4207 | for (size_t i = 0 ; i < mOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4208 | const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4209 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4210 | dstOutputs.add(desc->mIoHandle); |
| 4211 | ALOGVV(" new outputs: adding %d", desc->mIoHandle); |
| 4212 | } |
| 4213 | } |
| 4214 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4215 | if (!vectorsEqual(srcOutputs,dstOutputs)) { |
| 4216 | ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d", |
| 4217 | strategy, srcOutputs[0], dstOutputs[0]); |
| 4218 | // mute strategy while moving tracks from one output to another |
| 4219 | for (size_t i = 0; i < srcOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4220 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4221 | if (isStrategyActive(desc, strategy)) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4222 | setStrategyMute(strategy, true, desc); |
| 4223 | setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4224 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4225 | sp<AudioSourceDescriptor> source = |
| 4226 | getSourceForStrategyOnOutput(srcOutputs[i], strategy); |
| 4227 | if (source != 0){ |
| 4228 | connectAudioSource(source); |
| 4229 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4230 | } |
| 4231 | |
| 4232 | // Move effects associated to this strategy from previous output to new output |
| 4233 | if (strategy == STRATEGY_MEDIA) { |
| 4234 | audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs); |
| 4235 | SortedVector<audio_io_handle_t> moved; |
| 4236 | for (size_t i = 0; i < mEffects.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4237 | sp<EffectDescriptor> effectDesc = mEffects.valueAt(i); |
| 4238 | if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX && |
| 4239 | effectDesc->mIo != fxOutput) { |
| 4240 | if (moved.indexOf(effectDesc->mIo) < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4241 | ALOGV("checkOutputForStrategy() moving effect %d to output %d", |
| 4242 | mEffects.keyAt(i), fxOutput); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4243 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4244 | fxOutput); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4245 | moved.add(effectDesc->mIo); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4246 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4247 | effectDesc->mIo = fxOutput; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4248 | } |
| 4249 | } |
| 4250 | } |
| 4251 | // Move tracks associated to this strategy from previous output to new output |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4252 | for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4253 | if (getStrategy((audio_stream_type_t)i) == strategy) { |
| 4254 | mpClientInterface->invalidateStream((audio_stream_type_t)i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4255 | } |
| 4256 | } |
| 4257 | } |
| 4258 | } |
| 4259 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4260 | void AudioPolicyManager::checkOutputForAllStrategies() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4261 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4262 | 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] | 4263 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4264 | checkOutputForStrategy(STRATEGY_PHONE); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4265 | 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] | 4266 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4267 | checkOutputForStrategy(STRATEGY_SONIFICATION); |
| 4268 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4269 | checkOutputForStrategy(STRATEGY_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4270 | checkOutputForStrategy(STRATEGY_MEDIA); |
| 4271 | checkOutputForStrategy(STRATEGY_DTMF); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4272 | checkOutputForStrategy(STRATEGY_REROUTING); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4273 | } |
| 4274 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4275 | void AudioPolicyManager::checkA2dpSuspend() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4276 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4277 | audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4278 | if (a2dpOutput == 0) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4279 | mA2dpSuspended = false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4280 | return; |
| 4281 | } |
| 4282 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4283 | bool isScoConnected = |
Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 4284 | ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET & |
| 4285 | ~AUDIO_DEVICE_BIT_IN) != 0) || |
| 4286 | ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4287 | // suspend A2DP output if: |
| 4288 | // (NOT already suspended) && |
| 4289 | // ((SCO device is connected && |
| 4290 | // (forced usage for communication || for record is SCO))) || |
| 4291 | // (phone state is ringing || in call) |
| 4292 | // |
| 4293 | // restore A2DP output if: |
| 4294 | // (Already suspended) && |
| 4295 | // ((SCO device is NOT connected || |
| 4296 | // (forced usage NOT for communication && NOT for record is SCO))) && |
| 4297 | // (phone state is NOT ringing && NOT in call) |
| 4298 | // |
| 4299 | if (mA2dpSuspended) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4300 | if ((!isScoConnected || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4301 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) && |
| 4302 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) && |
| 4303 | ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) && |
| 4304 | (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4305 | |
| 4306 | mpClientInterface->restoreOutput(a2dpOutput); |
| 4307 | mA2dpSuspended = false; |
| 4308 | } |
| 4309 | } else { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4310 | if ((isScoConnected && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4311 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) || |
| 4312 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO))) || |
| 4313 | ((mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) || |
| 4314 | (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4315 | |
| 4316 | mpClientInterface->suspendOutput(a2dpOutput); |
| 4317 | mA2dpSuspended = true; |
| 4318 | } |
| 4319 | } |
| 4320 | } |
| 4321 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4322 | audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
| 4323 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4324 | { |
| 4325 | audio_devices_t device = AUDIO_DEVICE_NONE; |
| 4326 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4327 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4328 | if (index >= 0) { |
| 4329 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4330 | if (patchDesc->mUid != mUidCached) { |
| 4331 | ALOGV("getNewOutputDevice() device %08x forced by patch %d", |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4332 | outputDesc->device(), outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4333 | return outputDesc->device(); |
| 4334 | } |
| 4335 | } |
| 4336 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4337 | // 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] | 4338 | // 1: the strategy enforced audible is active and enforced on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4339 | // use device for strategy enforced audible |
| 4340 | // 2: we are in call or the strategy phone is active on the output: |
| 4341 | // use device for strategy phone |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 4342 | // 3: the strategy for enforced audible is active but not enforced on the output: |
| 4343 | // use the device for strategy enforced audible |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4344 | // 4: the strategy sonification is active on the output: |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4345 | // use device for strategy sonification |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4346 | // 5: the strategy accessibility is active on the output: |
| 4347 | // use device for strategy accessibility |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4348 | // 6: the strategy "respectful" sonification is active on the output: |
| 4349 | // use device for strategy "respectful" sonification |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4350 | // 7: the strategy media is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4351 | // use device for strategy media |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4352 | // 8: the strategy DTMF is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4353 | // use device for strategy DTMF |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4354 | // 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] | 4355 | // use device for strategy t-t-s |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4356 | if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4357 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4358 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
| 4359 | } else if (isInCall() || |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4360 | isStrategyActive(outputDesc, STRATEGY_PHONE)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4361 | device = getDeviceForStrategy(STRATEGY_PHONE, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4362 | } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) { |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 4363 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4364 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4365 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4366 | } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) { |
| 4367 | device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4368 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4369 | device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4370 | } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4371 | device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4372 | } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4373 | device = getDeviceForStrategy(STRATEGY_DTMF, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4374 | } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4375 | device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4376 | } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4377 | device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4378 | } |
| 4379 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4380 | ALOGV("getNewOutputDevice() selected device %x", device); |
| 4381 | return device; |
| 4382 | } |
| 4383 | |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 4384 | audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4385 | { |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 4386 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4387 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4388 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4389 | if (index >= 0) { |
| 4390 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4391 | if (patchDesc->mUid != mUidCached) { |
| 4392 | ALOGV("getNewInputDevice() device %08x forced by patch %d", |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4393 | inputDesc->mDevice, inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4394 | return inputDesc->mDevice; |
| 4395 | } |
| 4396 | } |
| 4397 | |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 4398 | audio_devices_t device = getDeviceAndMixForInputSource(inputDesc->inputSource()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4399 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4400 | return device; |
| 4401 | } |
| 4402 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4403 | bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1, |
| 4404 | audio_stream_type_t stream2) { |
| 4405 | return ((stream1 == stream2) || |
| 4406 | ((stream1 == AUDIO_STREAM_ACCESSIBILITY) && (stream2 == AUDIO_STREAM_MUSIC)) || |
| 4407 | ((stream1 == AUDIO_STREAM_MUSIC) && (stream2 == AUDIO_STREAM_ACCESSIBILITY))); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4408 | } |
| 4409 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4410 | uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4411 | return (uint32_t)getStrategy(stream); |
| 4412 | } |
| 4413 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4414 | audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4415 | // By checking the range of stream before calling getStrategy, we avoid |
| 4416 | // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE |
| 4417 | // 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] | 4418 | if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4419 | return AUDIO_DEVICE_NONE; |
| 4420 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4421 | audio_devices_t devices = AUDIO_DEVICE_NONE; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4422 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 4423 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4424 | continue; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4425 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4426 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4427 | audio_devices_t curDevices = |
Eric Laurent | 447a87b | 2016-07-07 17:32:10 -0700 | [diff] [blame] | 4428 | getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4429 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(curDevices, mOutputs); |
| 4430 | for (size_t i = 0; i < outputs.size(); i++) { |
| 4431 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4432 | if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4433 | curDevices |= outputDesc->device(); |
| 4434 | } |
| 4435 | } |
| 4436 | devices |= curDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4437 | } |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4438 | |
| 4439 | /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it |
| 4440 | and doesn't really need to.*/ |
| 4441 | if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) { |
| 4442 | devices |= AUDIO_DEVICE_OUT_SPEAKER; |
| 4443 | devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
| 4444 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4445 | return devices; |
| 4446 | } |
| 4447 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4448 | routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const |
| 4449 | { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4450 | 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] | 4451 | return mEngine->getStrategyForStream(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4452 | } |
| 4453 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4454 | uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) { |
| 4455 | // flags to strategy mapping |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4456 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 4457 | return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER; |
| 4458 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4459 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 4460 | return (uint32_t) STRATEGY_ENFORCED_AUDIBLE; |
| 4461 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4462 | // usage to strategy mapping |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4463 | return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage)); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4464 | } |
| 4465 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4466 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4467 | switch(stream) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4468 | case AUDIO_STREAM_MUSIC: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4469 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
| 4470 | updateDevicesAndOutputs(); |
| 4471 | break; |
| 4472 | default: |
| 4473 | break; |
| 4474 | } |
| 4475 | } |
| 4476 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4477 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 4478 | |
| 4479 | // skip beacon mute management if a dedicated TTS output is available |
| 4480 | if (mTtsOutputAvailable) { |
| 4481 | return 0; |
| 4482 | } |
| 4483 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4484 | switch(event) { |
| 4485 | case STARTING_OUTPUT: |
| 4486 | mBeaconMuteRefCount++; |
| 4487 | break; |
| 4488 | case STOPPING_OUTPUT: |
| 4489 | if (mBeaconMuteRefCount > 0) { |
| 4490 | mBeaconMuteRefCount--; |
| 4491 | } |
| 4492 | break; |
| 4493 | case STARTING_BEACON: |
| 4494 | mBeaconPlayingRefCount++; |
| 4495 | break; |
| 4496 | case STOPPING_BEACON: |
| 4497 | if (mBeaconPlayingRefCount > 0) { |
| 4498 | mBeaconPlayingRefCount--; |
| 4499 | } |
| 4500 | break; |
| 4501 | } |
| 4502 | |
| 4503 | if (mBeaconMuteRefCount > 0) { |
| 4504 | // any playback causes beacon to be muted |
| 4505 | return setBeaconMute(true); |
| 4506 | } else { |
| 4507 | // no other playback: unmute when beacon starts playing, mute when it stops |
| 4508 | return setBeaconMute(mBeaconPlayingRefCount == 0); |
| 4509 | } |
| 4510 | } |
| 4511 | |
| 4512 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { |
| 4513 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", |
| 4514 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); |
| 4515 | // keep track of muted state to avoid repeating mute/unmute operations |
| 4516 | if (mBeaconMuted != mute) { |
| 4517 | // mute/unmute AUDIO_STREAM_TTS on all outputs |
| 4518 | ALOGV("\t muting %d", mute); |
| 4519 | uint32_t maxLatency = 0; |
| 4520 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4521 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4522 | setStreamMute(AUDIO_STREAM_TTS, mute/*on*/, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4523 | desc, |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4524 | 0 /*delay*/, AUDIO_DEVICE_NONE); |
| 4525 | const uint32_t latency = desc->latency() * 2; |
| 4526 | if (latency > maxLatency) { |
| 4527 | maxLatency = latency; |
| 4528 | } |
| 4529 | } |
| 4530 | mBeaconMuted = mute; |
| 4531 | return maxLatency; |
| 4532 | } |
| 4533 | return 0; |
| 4534 | } |
| 4535 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4536 | audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4537 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4538 | { |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4539 | // Routing |
| 4540 | // see if we have an explicit route |
| 4541 | // scan the whole RouteMap, for each entry, convert the stream type to a strategy |
| 4542 | // (getStrategy(stream)). |
| 4543 | // if the strategy from the stream type in the RouteMap is the same as the argument above, |
| 4544 | // and activity count is non-zero |
| 4545 | // the device = the device from the descriptor in the RouteMap, and exit. |
| 4546 | for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) { |
| 4547 | sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4548 | routing_strategy routeStrategy = getStrategy(route->mStreamType); |
| 4549 | if ((routeStrategy == strategy) && route->isActive()) { |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4550 | return route->mDeviceDescriptor->type(); |
| 4551 | } |
| 4552 | } |
| 4553 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4554 | if (fromCache) { |
| 4555 | ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x", |
| 4556 | strategy, mDeviceForStrategy[strategy]); |
| 4557 | return mDeviceForStrategy[strategy]; |
| 4558 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4559 | return mEngine->getDeviceForStrategy(strategy); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4560 | } |
| 4561 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4562 | void AudioPolicyManager::updateDevicesAndOutputs() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4563 | { |
| 4564 | for (int i = 0; i < NUM_STRATEGIES; i++) { |
| 4565 | mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
| 4566 | } |
| 4567 | mPreviousOutputs = mOutputs; |
| 4568 | } |
| 4569 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4570 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4571 | audio_devices_t prevDevice, |
| 4572 | uint32_t delayMs) |
| 4573 | { |
| 4574 | // mute/unmute strategies using an incompatible device combination |
| 4575 | // if muting, wait for the audio in pcm buffer to be drained before proceeding |
| 4576 | // if unmuting, unmute only after the specified delay |
| 4577 | if (outputDesc->isDuplicated()) { |
| 4578 | return 0; |
| 4579 | } |
| 4580 | |
| 4581 | uint32_t muteWaitMs = 0; |
| 4582 | audio_devices_t device = outputDesc->device(); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4583 | bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4584 | |
| 4585 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
| 4586 | audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4587 | curDevice = curDevice & outputDesc->supportedDevices(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4588 | bool mute = shouldMute && (curDevice & device) && (curDevice != device); |
| 4589 | bool doMute = false; |
| 4590 | |
| 4591 | if (mute && !outputDesc->mStrategyMutedByDevice[i]) { |
| 4592 | doMute = true; |
| 4593 | outputDesc->mStrategyMutedByDevice[i] = true; |
| 4594 | } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){ |
| 4595 | doMute = true; |
| 4596 | outputDesc->mStrategyMutedByDevice[i] = false; |
| 4597 | } |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4598 | if (doMute) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4599 | for (size_t j = 0; j < mOutputs.size(); j++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4600 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4601 | // skip output if it does not share any device with current output |
| 4602 | if ((desc->supportedDevices() & outputDesc->supportedDevices()) |
| 4603 | == AUDIO_DEVICE_NONE) { |
| 4604 | continue; |
| 4605 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4606 | ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x)", |
| 4607 | mute ? "muting" : "unmuting", i, curDevice); |
| 4608 | setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4609 | if (isStrategyActive(desc, (routing_strategy)i)) { |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4610 | if (mute) { |
| 4611 | // FIXME: should not need to double latency if volume could be applied |
| 4612 | // immediately by the audioflinger mixer. We must account for the delay |
| 4613 | // between now and the next time the audioflinger thread for this output |
| 4614 | // will process a buffer (which corresponds to one buffer size, |
| 4615 | // usually 1/2 or 1/4 of the latency). |
| 4616 | if (muteWaitMs < desc->latency() * 2) { |
| 4617 | muteWaitMs = desc->latency() * 2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4618 | } |
| 4619 | } |
| 4620 | } |
| 4621 | } |
| 4622 | } |
| 4623 | } |
| 4624 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4625 | // temporary mute output if device selection changes to avoid volume bursts due to |
| 4626 | // different per device volumes |
| 4627 | if (outputDesc->isActive() && (device != prevDevice)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 4628 | uint32_t tempMuteWaitMs = outputDesc->latency() * 2; |
| 4629 | // temporary mute duration is conservatively set to 4 times the reported latency |
| 4630 | uint32_t tempMuteDurationMs = outputDesc->latency() * 4; |
| 4631 | if (muteWaitMs < tempMuteWaitMs) { |
| 4632 | muteWaitMs = tempMuteWaitMs; |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4633 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 4634 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4635 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4636 | if (isStrategyActive(outputDesc, (routing_strategy)i)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 4637 | // make sure that we do not start the temporary mute period too early in case of |
| 4638 | // delayed device change |
| 4639 | setStrategyMute((routing_strategy)i, true, outputDesc, delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4640 | setStrategyMute((routing_strategy)i, false, outputDesc, |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 4641 | delayMs + tempMuteDurationMs, device); |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4642 | } |
| 4643 | } |
| 4644 | } |
| 4645 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4646 | // wait for the PCM output buffers to empty before proceeding with the rest of the command |
| 4647 | if (muteWaitMs > delayMs) { |
| 4648 | muteWaitMs -= delayMs; |
| 4649 | usleep(muteWaitMs * 1000); |
| 4650 | return muteWaitMs; |
| 4651 | } |
| 4652 | return 0; |
| 4653 | } |
| 4654 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4655 | uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4656 | audio_devices_t device, |
| 4657 | bool force, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4658 | int delayMs, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4659 | audio_patch_handle_t *patchHandle, |
| 4660 | const char* address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4661 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4662 | ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4663 | AudioParameter param; |
| 4664 | uint32_t muteWaitMs; |
| 4665 | |
| 4666 | if (outputDesc->isDuplicated()) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4667 | muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs); |
| 4668 | muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4669 | return muteWaitMs; |
| 4670 | } |
| 4671 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current |
| 4672 | // output profile |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4673 | if ((device != AUDIO_DEVICE_NONE) && |
| 4674 | ((device & outputDesc->supportedDevices()) == 0)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4675 | return 0; |
| 4676 | } |
| 4677 | |
| 4678 | // filter devices according to output selected |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4679 | device = (audio_devices_t)(device & outputDesc->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4680 | |
| 4681 | audio_devices_t prevDevice = outputDesc->mDevice; |
| 4682 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4683 | ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4684 | |
| 4685 | if (device != AUDIO_DEVICE_NONE) { |
| 4686 | outputDesc->mDevice = device; |
| 4687 | } |
| 4688 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs); |
| 4689 | |
| 4690 | // Do not change the routing if: |
Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 4691 | // the requested device is AUDIO_DEVICE_NONE |
| 4692 | // OR the requested device is the same as current device |
| 4693 | // AND force is not specified |
| 4694 | // AND the output is connected by a valid audio patch. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4695 | // Doing this check here allows the caller to call setOutputDevice() without conditions |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4696 | if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && |
| 4697 | !force && |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4698 | outputDesc->getPatchHandle() != 0) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4699 | ALOGV("setOutputDevice() setting same device 0x%04x or null device", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4700 | return muteWaitMs; |
| 4701 | } |
| 4702 | |
| 4703 | ALOGV("setOutputDevice() changing device"); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4704 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4705 | // do the routing |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4706 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4707 | resetOutputDevice(outputDesc, delayMs, NULL); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4708 | } else { |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 4709 | DeviceVector deviceList; |
| 4710 | if ((address == NULL) || (strlen(address) == 0)) { |
| 4711 | deviceList = mAvailableOutputDevices.getDevicesFromType(device); |
| 4712 | } else { |
| 4713 | deviceList = mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address)); |
| 4714 | } |
| 4715 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4716 | if (!deviceList.isEmpty()) { |
| 4717 | struct audio_patch patch; |
| 4718 | outputDesc->toAudioPortConfig(&patch.sources[0]); |
| 4719 | patch.num_sources = 1; |
| 4720 | patch.num_sinks = 0; |
| 4721 | for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) { |
| 4722 | deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4723 | patch.num_sinks++; |
| 4724 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4725 | ssize_t index; |
| 4726 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { |
| 4727 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4728 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4729 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4730 | } |
| 4731 | sp< AudioPatch> patchDesc; |
| 4732 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 4733 | if (index >= 0) { |
| 4734 | patchDesc = mAudioPatches.valueAt(index); |
| 4735 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 4736 | } |
| 4737 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4738 | status_t status = mpClientInterface->createAudioPatch(&patch, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4739 | &afPatchHandle, |
| 4740 | delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4741 | ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d" |
| 4742 | "num_sources %d num_sinks %d", |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4743 | status, afPatchHandle, patch.num_sources, patch.num_sinks); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4744 | if (status == NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4745 | if (index < 0) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 4746 | patchDesc = new AudioPatch(&patch, mUidCached); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4747 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 4748 | } else { |
| 4749 | patchDesc->mPatch = patch; |
| 4750 | } |
| 4751 | patchDesc->mAfPatchHandle = afPatchHandle; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4752 | if (patchHandle) { |
| 4753 | *patchHandle = patchDesc->mHandle; |
| 4754 | } |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4755 | outputDesc->setPatchHandle(patchDesc->mHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4756 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4757 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4758 | } |
| 4759 | } |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 4760 | |
| 4761 | // inform all input as well |
| 4762 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 4763 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4764 | if (!is_virtual_input_device(inputDescriptor->mDevice)) { |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 4765 | AudioParameter inputCmd = AudioParameter(); |
| 4766 | ALOGV("%s: inform input %d of device:%d", __func__, |
| 4767 | inputDescriptor->mIoHandle, device); |
| 4768 | inputCmd.addInt(String8(AudioParameter::keyRouting),device); |
| 4769 | mpClientInterface->setParameters(inputDescriptor->mIoHandle, |
| 4770 | inputCmd.toString(), |
| 4771 | delayMs); |
| 4772 | } |
| 4773 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4774 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4775 | |
| 4776 | // update stream volumes according to new device |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4777 | applyStreamVolumes(outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4778 | |
| 4779 | return muteWaitMs; |
| 4780 | } |
| 4781 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4782 | status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4783 | int delayMs, |
| 4784 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4785 | { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4786 | ssize_t index; |
| 4787 | if (patchHandle) { |
| 4788 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4789 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4790 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4791 | } |
| 4792 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4793 | return INVALID_OPERATION; |
| 4794 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4795 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4796 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4797 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 4798 | outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4799 | removeAudioPatch(patchDesc->mHandle); |
| 4800 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4801 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4802 | return status; |
| 4803 | } |
| 4804 | |
| 4805 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, |
| 4806 | audio_devices_t device, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4807 | bool force, |
| 4808 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4809 | { |
| 4810 | status_t status = NO_ERROR; |
| 4811 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4812 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4813 | if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) { |
| 4814 | inputDesc->mDevice = device; |
| 4815 | |
| 4816 | DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device); |
| 4817 | if (!deviceList.isEmpty()) { |
| 4818 | struct audio_patch patch; |
| 4819 | inputDesc->toAudioPortConfig(&patch.sinks[0]); |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 4820 | // AUDIO_SOURCE_HOTWORD is for internal use only: |
| 4821 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 4822 | if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD && |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 4823 | !inputDesc->isSoundTrigger()) { |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 4824 | patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 4825 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4826 | patch.num_sinks = 1; |
| 4827 | //only one input device for now |
| 4828 | deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4829 | patch.num_sources = 1; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4830 | ssize_t index; |
| 4831 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { |
| 4832 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4833 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4834 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4835 | } |
| 4836 | sp< AudioPatch> patchDesc; |
| 4837 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 4838 | if (index >= 0) { |
| 4839 | patchDesc = mAudioPatches.valueAt(index); |
| 4840 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 4841 | } |
| 4842 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4843 | status_t status = mpClientInterface->createAudioPatch(&patch, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4844 | &afPatchHandle, |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4845 | 0); |
| 4846 | ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d", |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4847 | status, afPatchHandle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4848 | if (status == NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4849 | if (index < 0) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 4850 | patchDesc = new AudioPatch(&patch, mUidCached); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4851 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 4852 | } else { |
| 4853 | patchDesc->mPatch = patch; |
| 4854 | } |
| 4855 | patchDesc->mAfPatchHandle = afPatchHandle; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4856 | if (patchHandle) { |
| 4857 | *patchHandle = patchDesc->mHandle; |
| 4858 | } |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4859 | inputDesc->setPatchHandle(patchDesc->mHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4860 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4861 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4862 | } |
| 4863 | } |
| 4864 | } |
| 4865 | return status; |
| 4866 | } |
| 4867 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4868 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, |
| 4869 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4870 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4871 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4872 | ssize_t index; |
| 4873 | if (patchHandle) { |
| 4874 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4875 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4876 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4877 | } |
| 4878 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4879 | return INVALID_OPERATION; |
| 4880 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4881 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4882 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4883 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 4884 | inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4885 | removeAudioPatch(patchDesc->mHandle); |
| 4886 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4887 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4888 | return status; |
| 4889 | } |
| 4890 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 4891 | sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4892 | String8 address, |
| 4893 | uint32_t& samplingRate, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4894 | audio_format_t& format, |
| 4895 | audio_channel_mask_t& channelMask, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4896 | audio_input_flags_t flags) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4897 | { |
| 4898 | // Choose an input profile based on the requested capture parameters: select the first available |
| 4899 | // profile supporting all requested parameters. |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4900 | // |
| 4901 | // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return |
| 4902 | // the best matching profile, not the first one. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4903 | |
| 4904 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 4905 | { |
| 4906 | if (mHwModules[i]->mHandle == 0) { |
| 4907 | continue; |
| 4908 | } |
| 4909 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) |
| 4910 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4911 | sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j]; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4912 | // profile->log(); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4913 | if (profile->isCompatibleProfile(device, address, samplingRate, |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 4914 | &samplingRate /*updatedSamplingRate*/, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4915 | format, |
| 4916 | &format /*updatedFormat*/, |
| 4917 | channelMask, |
| 4918 | &channelMask /*updatedChannelMask*/, |
| 4919 | (audio_output_flags_t) flags)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4920 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4921 | return profile; |
| 4922 | } |
| 4923 | } |
| 4924 | } |
| 4925 | return NULL; |
| 4926 | } |
| 4927 | |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 4928 | |
| 4929 | audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4930 | AudioMix **policyMix) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4931 | { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4932 | audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
| 4933 | audio_devices_t selectedDeviceFromMix = |
| 4934 | mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4935 | |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4936 | if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) { |
| 4937 | return selectedDeviceFromMix; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4938 | } |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 4939 | return getDeviceForInputSource(inputSource); |
| 4940 | } |
| 4941 | |
| 4942 | audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource) |
| 4943 | { |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 4944 | for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) { |
| 4945 | sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4946 | if (inputSource == route->mSource && route->isActive()) { |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 4947 | return route->mDeviceDescriptor->type(); |
| 4948 | } |
| 4949 | } |
| 4950 | |
| 4951 | return mEngine->getDeviceForInputSource(inputSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4952 | } |
| 4953 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4954 | float AudioPolicyManager::computeVolume(audio_stream_type_t stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4955 | int index, |
| 4956 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4957 | { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 4958 | float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index); |
Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 4959 | |
| 4960 | // handle the case of accessibility active while a ringtone is playing: if the ringtone is much |
| 4961 | // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch |
| 4962 | // exploration of the dialer UI. In this situation, bring the accessibility volume closer to |
| 4963 | // the ringtone volume |
| 4964 | if ((stream == AUDIO_STREAM_ACCESSIBILITY) |
| 4965 | && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState()) |
| 4966 | && isStreamActive(AUDIO_STREAM_RING, 0)) { |
| 4967 | const float ringVolumeDB = computeVolume(AUDIO_STREAM_RING, index, device); |
| 4968 | return ringVolumeDB - 4 > volumeDB ? ringVolumeDB - 4 : volumeDB; |
| 4969 | } |
| 4970 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4971 | // if a headset is connected, apply the following rules to ring tones and notifications |
| 4972 | // to avoid sound level bursts in user's ears: |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 4973 | // - always attenuate notifications volume by 6dB |
| 4974 | // - attenuate ring tones volume by 6dB unless music is not playing and |
| 4975 | // speaker is part of the select devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4976 | // - if music is playing, always limit the volume to current music volume, |
| 4977 | // with a minimum threshold at -36dB so that notification is always perceived. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4978 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4979 | if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 4980 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | |
| 4981 | AUDIO_DEVICE_OUT_WIRED_HEADSET | |
| 4982 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) && |
| 4983 | ((stream_strategy == STRATEGY_SONIFICATION) |
| 4984 | || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL) |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4985 | || (stream == AUDIO_STREAM_SYSTEM) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4986 | || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4987 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) && |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4988 | mVolumeCurves->canBeMuted(stream)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4989 | // when the phone is ringing we must consider that music could have been paused just before |
| 4990 | // by the music application and behave as if music was active if the last music track was |
| 4991 | // just stopped |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4992 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4993 | mLimitRingtoneVolume) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 4994 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4995 | audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4996 | float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4997 | mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC, |
| 4998 | musicDevice), |
| 4999 | musicDevice); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5000 | float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ? |
| 5001 | musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB; |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5002 | if (volumeDB > minVolDB) { |
| 5003 | volumeDB = minVolDB; |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5004 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5005 | } |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5006 | if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 5007 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) { |
| 5008 | // on A2DP, also ensure notification volume is not too low compared to media when |
| 5009 | // intended to be played |
| 5010 | if ((volumeDB > -96.0f) && |
| 5011 | (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) { |
| 5012 | ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f", |
| 5013 | stream, device, |
| 5014 | volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB); |
| 5015 | volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB; |
| 5016 | } |
| 5017 | } |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5018 | } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) || |
| 5019 | stream_strategy != STRATEGY_SONIFICATION) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5020 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5021 | } |
| 5022 | } |
| 5023 | |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5024 | return volumeDB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5025 | } |
| 5026 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5027 | status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5028 | int index, |
| 5029 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5030 | audio_devices_t device, |
| 5031 | int delayMs, |
| 5032 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5033 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5034 | // do not change actual stream volume if the stream is muted |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5035 | if (outputDesc->mMuteCount[stream] != 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5036 | ALOGVV("checkAndSetVolume() stream %d muted count %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5037 | stream, outputDesc->mMuteCount[stream]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5038 | return NO_ERROR; |
| 5039 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5040 | audio_policy_forced_cfg_t forceUseForComm = |
| 5041 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5042 | // 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] | 5043 | if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) || |
| 5044 | (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5045 | 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] | 5046 | stream, forceUseForComm); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5047 | return INVALID_OPERATION; |
| 5048 | } |
| 5049 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5050 | if (device == AUDIO_DEVICE_NONE) { |
| 5051 | device = outputDesc->device(); |
| 5052 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5053 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5054 | float volumeDb = computeVolume(stream, index, device); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5055 | if (outputDesc->isFixedVolume(device)) { |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5056 | volumeDb = 0.0f; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5057 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5058 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5059 | outputDesc->setVolume(volumeDb, stream, device, delayMs, force); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5060 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5061 | if (stream == AUDIO_STREAM_VOICE_CALL || |
| 5062 | stream == AUDIO_STREAM_BLUETOOTH_SCO) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5063 | float voiceVolume; |
| 5064 | // 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] | 5065 | if (stream == AUDIO_STREAM_VOICE_CALL) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5066 | voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5067 | } else { |
| 5068 | voiceVolume = 1.0; |
| 5069 | } |
| 5070 | |
Eric Laurent | 18fba84 | 2016-03-31 14:41:26 -0700 | [diff] [blame] | 5071 | if (voiceVolume != mLastVoiceVolume) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5072 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); |
| 5073 | mLastVoiceVolume = voiceVolume; |
| 5074 | } |
| 5075 | } |
| 5076 | |
| 5077 | return NO_ERROR; |
| 5078 | } |
| 5079 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5080 | void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc, |
| 5081 | audio_devices_t device, |
| 5082 | int delayMs, |
| 5083 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5084 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5085 | ALOGVV("applyStreamVolumes() for device %08x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5086 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5087 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5088 | checkAndSetVolume((audio_stream_type_t)stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5089 | mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5090 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5091 | device, |
| 5092 | delayMs, |
| 5093 | force); |
| 5094 | } |
| 5095 | } |
| 5096 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5097 | void AudioPolicyManager::setStrategyMute(routing_strategy strategy, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5098 | bool on, |
| 5099 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5100 | int delayMs, |
| 5101 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5102 | { |
Eric Laurent | 72249d5 | 2015-06-08 11:12:15 -0700 | [diff] [blame] | 5103 | ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d", |
| 5104 | strategy, on, outputDesc->getId()); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5105 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5106 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5107 | setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5108 | } |
| 5109 | } |
| 5110 | } |
| 5111 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5112 | void AudioPolicyManager::setStreamMute(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5113 | bool on, |
| 5114 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5115 | int delayMs, |
| 5116 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5117 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5118 | if (device == AUDIO_DEVICE_NONE) { |
| 5119 | device = outputDesc->device(); |
| 5120 | } |
| 5121 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5122 | ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x", |
| 5123 | stream, on, outputDesc->mMuteCount[stream], device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5124 | |
| 5125 | if (on) { |
| 5126 | if (outputDesc->mMuteCount[stream] == 0) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5127 | if (mVolumeCurves->canBeMuted(stream) && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5128 | ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5129 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5130 | checkAndSetVolume(stream, 0, outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5131 | } |
| 5132 | } |
| 5133 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored |
| 5134 | outputDesc->mMuteCount[stream]++; |
| 5135 | } else { |
| 5136 | if (outputDesc->mMuteCount[stream] == 0) { |
| 5137 | ALOGV("setStreamMute() unmuting non muted stream!"); |
| 5138 | return; |
| 5139 | } |
| 5140 | if (--outputDesc->mMuteCount[stream] == 0) { |
| 5141 | checkAndSetVolume(stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5142 | mVolumeCurves->getVolumeIndex(stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5143 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5144 | device, |
| 5145 | delayMs); |
| 5146 | } |
| 5147 | } |
| 5148 | } |
| 5149 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5150 | void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5151 | bool starting, bool stateChange) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5152 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 5153 | if(!hasPrimaryOutput()) { |
| 5154 | return; |
| 5155 | } |
| 5156 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5157 | // if the stream pertains to sonification strategy and we are in call we must |
| 5158 | // mute the stream if it is low visibility. If it is high visibility, we must play a tone |
| 5159 | // in the device used for phone strategy and play the tone if the selected device does not |
| 5160 | // interfere with the device used for phone strategy |
| 5161 | // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as |
| 5162 | // many times as there are active tracks on the output |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5163 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5164 | if ((stream_strategy == STRATEGY_SONIFICATION) || |
| 5165 | ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5166 | sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5167 | ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d", |
| 5168 | stream, starting, outputDesc->mDevice, stateChange); |
| 5169 | if (outputDesc->mRefCount[stream]) { |
| 5170 | int muteCount = 1; |
| 5171 | if (stateChange) { |
| 5172 | muteCount = outputDesc->mRefCount[stream]; |
| 5173 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5174 | if (audio_is_low_visibility(stream)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5175 | ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount); |
| 5176 | for (int i = 0; i < muteCount; i++) { |
| 5177 | setStreamMute(stream, starting, mPrimaryOutput); |
| 5178 | } |
| 5179 | } else { |
| 5180 | ALOGV("handleIncallSonification() high visibility"); |
| 5181 | if (outputDesc->device() & |
| 5182 | getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) { |
| 5183 | ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount); |
| 5184 | for (int i = 0; i < muteCount; i++) { |
| 5185 | setStreamMute(stream, starting, mPrimaryOutput); |
| 5186 | } |
| 5187 | } |
| 5188 | if (starting) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5189 | mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION, |
| 5190 | AUDIO_STREAM_VOICE_CALL); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5191 | } else { |
| 5192 | mpClientInterface->stopTone(); |
| 5193 | } |
| 5194 | } |
| 5195 | } |
| 5196 | } |
| 5197 | } |
| 5198 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5199 | audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr) |
| 5200 | { |
| 5201 | // flags to stream type mapping |
| 5202 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 5203 | return AUDIO_STREAM_ENFORCED_AUDIBLE; |
| 5204 | } |
| 5205 | if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) { |
| 5206 | return AUDIO_STREAM_BLUETOOTH_SCO; |
| 5207 | } |
Jean-Michel Trivi | 79ad438 | 2015-01-29 10:49:39 -0800 | [diff] [blame] | 5208 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 5209 | return AUDIO_STREAM_TTS; |
| 5210 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5211 | |
| 5212 | // usage to stream type mapping |
| 5213 | switch (attr->usage) { |
| 5214 | case AUDIO_USAGE_MEDIA: |
| 5215 | case AUDIO_USAGE_GAME: |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5216 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 5217 | return AUDIO_STREAM_MUSIC; |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5218 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 5219 | return AUDIO_STREAM_ACCESSIBILITY; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5220 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 5221 | return AUDIO_STREAM_SYSTEM; |
| 5222 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 5223 | return AUDIO_STREAM_VOICE_CALL; |
| 5224 | |
| 5225 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 5226 | return AUDIO_STREAM_DTMF; |
| 5227 | |
| 5228 | case AUDIO_USAGE_ALARM: |
| 5229 | return AUDIO_STREAM_ALARM; |
| 5230 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 5231 | return AUDIO_STREAM_RING; |
| 5232 | |
| 5233 | case AUDIO_USAGE_NOTIFICATION: |
| 5234 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 5235 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 5236 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 5237 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 5238 | return AUDIO_STREAM_NOTIFICATION; |
| 5239 | |
| 5240 | case AUDIO_USAGE_UNKNOWN: |
| 5241 | default: |
| 5242 | return AUDIO_STREAM_MUSIC; |
| 5243 | } |
| 5244 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5245 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5246 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) |
| 5247 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5248 | // has flags that map to a strategy? |
| 5249 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { |
| 5250 | return true; |
| 5251 | } |
| 5252 | |
| 5253 | // has known usage? |
| 5254 | switch (paa->usage) { |
| 5255 | case AUDIO_USAGE_UNKNOWN: |
| 5256 | case AUDIO_USAGE_MEDIA: |
| 5257 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 5258 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 5259 | case AUDIO_USAGE_ALARM: |
| 5260 | case AUDIO_USAGE_NOTIFICATION: |
| 5261 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 5262 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 5263 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 5264 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 5265 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 5266 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 5267 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 5268 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 5269 | case AUDIO_USAGE_GAME: |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5270 | case AUDIO_USAGE_VIRTUAL_SOURCE: |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5271 | break; |
| 5272 | default: |
| 5273 | return false; |
| 5274 | } |
| 5275 | return true; |
| 5276 | } |
| 5277 | |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5278 | bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc, |
| 5279 | routing_strategy strategy, uint32_t inPastMs, |
| 5280 | nsecs_t sysTime) const |
| 5281 | { |
| 5282 | if ((sysTime == 0) && (inPastMs != 0)) { |
| 5283 | sysTime = systemTime(); |
| 5284 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5285 | 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] | 5286 | if (((getStrategy((audio_stream_type_t)i) == strategy) || |
| 5287 | (NUM_STRATEGIES == strategy)) && |
| 5288 | outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) { |
| 5289 | return true; |
| 5290 | } |
| 5291 | } |
| 5292 | return false; |
| 5293 | } |
| 5294 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5295 | audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage) |
| 5296 | { |
| 5297 | return mEngine->getForceUse(usage); |
| 5298 | } |
| 5299 | |
| 5300 | bool AudioPolicyManager::isInCall() |
| 5301 | { |
| 5302 | return isStateInCall(mEngine->getPhoneState()); |
| 5303 | } |
| 5304 | |
| 5305 | bool AudioPolicyManager::isStateInCall(int state) |
| 5306 | { |
| 5307 | return is_state_in_call(state); |
| 5308 | } |
| 5309 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5310 | void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc) |
| 5311 | { |
| 5312 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
| 5313 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 5314 | if (sourceDesc->mDevice->equals(deviceDesc)) { |
| 5315 | ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle()); |
| 5316 | stopAudioSource(sourceDesc->getHandle()); |
| 5317 | } |
| 5318 | } |
| 5319 | |
| 5320 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
| 5321 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 5322 | bool release = false; |
| 5323 | for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) { |
| 5324 | const struct audio_port_config *source = &patchDesc->mPatch.sources[j]; |
| 5325 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 5326 | source->ext.device.type == deviceDesc->type()) { |
| 5327 | release = true; |
| 5328 | } |
| 5329 | } |
| 5330 | for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) { |
| 5331 | const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j]; |
| 5332 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && |
| 5333 | sink->ext.device.type == deviceDesc->type()) { |
| 5334 | release = true; |
| 5335 | } |
| 5336 | } |
| 5337 | if (release) { |
| 5338 | ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle); |
| 5339 | releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid); |
| 5340 | } |
| 5341 | } |
| 5342 | } |
| 5343 | |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5344 | // Modify the list of surround sound formats supported. |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5345 | void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) { |
| 5346 | FormatVector &formats = *formatsPtr; |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5347 | // TODO Set this based on Config properties. |
| 5348 | const bool alwaysForceAC3 = true; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5349 | |
| 5350 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5351 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5352 | ALOGD("%s: forced use = %d", __FUNCTION__, forceUse); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5353 | |
| 5354 | // Analyze original support for various formats. |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5355 | bool supportsAC3 = false; |
| 5356 | bool supportsOtherSurround = false; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5357 | bool supportsIEC61937 = false; |
| 5358 | for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) { |
| 5359 | audio_format_t format = formats[formatIndex]; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5360 | switch (format) { |
| 5361 | case AUDIO_FORMAT_AC3: |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5362 | supportsAC3 = true; |
| 5363 | break; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5364 | case AUDIO_FORMAT_E_AC3: |
| 5365 | case AUDIO_FORMAT_DTS: |
| 5366 | case AUDIO_FORMAT_DTS_HD: |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5367 | supportsOtherSurround = true; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5368 | break; |
| 5369 | case AUDIO_FORMAT_IEC61937: |
| 5370 | supportsIEC61937 = true; |
| 5371 | break; |
| 5372 | default: |
| 5373 | break; |
| 5374 | } |
| 5375 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5376 | |
| 5377 | // Modify formats based on surround preferences. |
| 5378 | // If NEVER, remove support for surround formats. |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5379 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
| 5380 | if (supportsAC3 || supportsOtherSurround || supportsIEC61937) { |
| 5381 | // Remove surround sound related formats. |
| 5382 | for (size_t formatIndex = 0; formatIndex < formats.size(); ) { |
| 5383 | audio_format_t format = formats[formatIndex]; |
| 5384 | switch(format) { |
| 5385 | case AUDIO_FORMAT_AC3: |
| 5386 | case AUDIO_FORMAT_E_AC3: |
| 5387 | case AUDIO_FORMAT_DTS: |
| 5388 | case AUDIO_FORMAT_DTS_HD: |
| 5389 | case AUDIO_FORMAT_IEC61937: |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5390 | formats.removeAt(formatIndex); |
| 5391 | break; |
| 5392 | default: |
| 5393 | formatIndex++; // keep it |
| 5394 | break; |
| 5395 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5396 | } |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5397 | supportsAC3 = false; |
| 5398 | supportsOtherSurround = false; |
| 5399 | supportsIEC61937 = false; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5400 | } |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5401 | } else { // AUTO or ALWAYS |
| 5402 | // Most TVs support AC3 even if they do not report it in the EDID. |
| 5403 | if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)) |
| 5404 | && !supportsAC3) { |
| 5405 | formats.add(AUDIO_FORMAT_AC3); |
| 5406 | supportsAC3 = true; |
| 5407 | } |
| 5408 | |
| 5409 | // If ALWAYS, add support for raw surround formats if all are missing. |
| 5410 | // This assumes that if any of these formats are reported by the HAL |
| 5411 | // then the report is valid and should not be modified. |
| 5412 | if ((forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) |
| 5413 | && !supportsOtherSurround) { |
| 5414 | formats.add(AUDIO_FORMAT_E_AC3); |
| 5415 | formats.add(AUDIO_FORMAT_DTS); |
| 5416 | formats.add(AUDIO_FORMAT_DTS_HD); |
| 5417 | supportsOtherSurround = true; |
| 5418 | } |
| 5419 | |
| 5420 | // Add support for IEC61937 if any raw surround supported. |
| 5421 | // The HAL could do this but add it here, just in case. |
| 5422 | if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) { |
| 5423 | formats.add(AUDIO_FORMAT_IEC61937); |
| 5424 | supportsIEC61937 = true; |
| 5425 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5426 | } |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5427 | } |
| 5428 | |
| 5429 | // Modify the list of channel masks supported. |
| 5430 | void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) { |
| 5431 | ChannelsVector &channelMasks = *channelMasksPtr; |
| 5432 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5433 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
| 5434 | |
| 5435 | // If NEVER, then remove support for channelMasks > stereo. |
| 5436 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
| 5437 | for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) { |
| 5438 | audio_channel_mask_t channelMask = channelMasks[maskIndex]; |
| 5439 | if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) { |
| 5440 | ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask); |
| 5441 | channelMasks.removeAt(maskIndex); |
| 5442 | } else { |
| 5443 | maskIndex++; |
| 5444 | } |
| 5445 | } |
| 5446 | // If ALWAYS, then make sure we at least support 5.1 |
| 5447 | } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { |
| 5448 | bool supports5dot1 = false; |
| 5449 | // Are there any channel masks that can be considered "surround"? |
| 5450 | for (size_t maskIndex = 0; maskIndex < channelMasks.size(); maskIndex++) { |
| 5451 | audio_channel_mask_t channelMask = channelMasks[maskIndex]; |
| 5452 | if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) { |
| 5453 | supports5dot1 = true; |
| 5454 | break; |
| 5455 | } |
| 5456 | } |
| 5457 | // If not then add 5.1 support. |
| 5458 | if (!supports5dot1) { |
| 5459 | channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1); |
| 5460 | ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__); |
| 5461 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5462 | } |
| 5463 | } |
| 5464 | |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5465 | void AudioPolicyManager::updateAudioProfiles(audio_devices_t device, |
| 5466 | audio_io_handle_t ioHandle, |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5467 | AudioProfileVector &profiles) |
| 5468 | { |
| 5469 | String8 reply; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5470 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5471 | // Format MUST be checked first to update the list of AudioProfile |
| 5472 | if (profiles.hasDynamicFormat()) { |
| 5473 | reply = mpClientInterface->getParameters(ioHandle, |
| 5474 | String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS)); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5475 | ALOGV("%s: supported formats %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5476 | AudioParameter repliedParameters(reply); |
| 5477 | if (repliedParameters.get( |
| 5478 | String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS), reply) != NO_ERROR) { |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5479 | ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__); |
| 5480 | return; |
| 5481 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5482 | FormatVector formats = formatsFromString(reply.string()); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5483 | if (device == AUDIO_DEVICE_OUT_HDMI) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5484 | filterSurroundFormats(&formats); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5485 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5486 | profiles.setFormats(formats); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5487 | } |
| 5488 | const FormatVector &supportedFormats = profiles.getSupportedFormats(); |
| 5489 | |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5490 | for (size_t formatIndex = 0; formatIndex < supportedFormats.size(); formatIndex++) { |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5491 | audio_format_t format = supportedFormats[formatIndex]; |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5492 | ChannelsVector channelMasks; |
| 5493 | SampleRateVector samplingRates; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5494 | AudioParameter requestedParameters; |
| 5495 | requestedParameters.addInt(String8(AUDIO_PARAMETER_STREAM_FORMAT), format); |
| 5496 | |
| 5497 | if (profiles.hasDynamicRateFor(format)) { |
| 5498 | reply = mpClientInterface->getParameters(ioHandle, |
| 5499 | requestedParameters.toString() + ";" + |
| 5500 | AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES); |
| 5501 | ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5502 | AudioParameter repliedParameters(reply); |
| 5503 | if (repliedParameters.get( |
| 5504 | String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES), reply) == NO_ERROR) { |
| 5505 | samplingRates = samplingRatesFromString(reply.string()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5506 | } |
| 5507 | } |
| 5508 | if (profiles.hasDynamicChannelsFor(format)) { |
| 5509 | reply = mpClientInterface->getParameters(ioHandle, |
| 5510 | requestedParameters.toString() + ";" + |
| 5511 | AUDIO_PARAMETER_STREAM_SUP_CHANNELS); |
| 5512 | ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5513 | AudioParameter repliedParameters(reply); |
| 5514 | if (repliedParameters.get( |
| 5515 | String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS), reply) == NO_ERROR) { |
| 5516 | channelMasks = channelMasksFromString(reply.string()); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5517 | if (device == AUDIO_DEVICE_OUT_HDMI) { |
| 5518 | filterSurroundChannelMasks(&channelMasks); |
| 5519 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5520 | } |
| 5521 | } |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5522 | profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates)); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5523 | } |
| 5524 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5525 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5526 | }; // namespace android |