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 | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 276 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 277 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 278 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 279 | updateCallRouting(newDevice); |
| 280 | } |
| 281 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 282 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
| 283 | cleanUpForDevice(devDesc); |
| 284 | } |
| 285 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 286 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 287 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 288 | } // end if is input device |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 289 | |
| 290 | ALOGW("setDeviceConnectionState() invalid device: %x", device); |
| 291 | return BAD_VALUE; |
| 292 | } |
| 293 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 294 | audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 295 | const char *device_address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 296 | { |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 297 | sp<DeviceDescriptor> devDesc = |
| 298 | mHwModules.getDeviceDescriptor(device, device_address, "", |
| 299 | (strlen(device_address) != 0)/*matchAddress*/); |
| 300 | |
| 301 | if (devDesc == 0) { |
| 302 | ALOGW("getDeviceConnectionState() undeclared device, type %08x, address: %s", |
| 303 | device, device_address); |
| 304 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
| 305 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 306 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 307 | DeviceVector *deviceVector; |
| 308 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 309 | if (audio_is_output_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 310 | deviceVector = &mAvailableOutputDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 311 | } else if (audio_is_input_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 312 | deviceVector = &mAvailableInputDevices; |
| 313 | } else { |
| 314 | ALOGW("getDeviceConnectionState() invalid device type %08x", device); |
| 315 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 316 | } |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 317 | |
| 318 | return (deviceVector->getDevice(device, String8(device_address)) != 0) ? |
| 319 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 320 | } |
| 321 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame^] | 322 | uint32_t AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, uint32_t delayMs) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 323 | { |
| 324 | bool createTxPatch = false; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 325 | status_t status; |
| 326 | audio_patch_handle_t afPatchHandle; |
| 327 | DeviceVector deviceList; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame^] | 328 | uint32_t muteWaitMs = 0; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 329 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 330 | if(!hasPrimaryOutput()) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame^] | 331 | return muteWaitMs; |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 332 | } |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 333 | audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 334 | ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice); |
| 335 | |
| 336 | // release existing RX patch if any |
| 337 | if (mCallRxPatch != 0) { |
| 338 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 339 | mCallRxPatch.clear(); |
| 340 | } |
| 341 | // release TX patch if any |
| 342 | if (mCallTxPatch != 0) { |
| 343 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 344 | mCallTxPatch.clear(); |
| 345 | } |
| 346 | |
| 347 | // If the RX device is on the primary HW module, then use legacy routing method for voice calls |
| 348 | // via setOutputDevice() on primary output. |
| 349 | // Otherwise, create two audio patches for TX and RX path. |
| 350 | if (availablePrimaryOutputDevices() & rxDevice) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame^] | 351 | muteWaitMs = setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 352 | // If the TX device is also on the primary HW module, setOutputDevice() will take care |
| 353 | // of it due to legacy implementation. If not, create a patch. |
| 354 | if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN) |
| 355 | == AUDIO_DEVICE_NONE) { |
| 356 | createTxPatch = true; |
| 357 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 358 | } else { // create RX path audio patch |
| 359 | struct audio_patch patch; |
| 360 | |
| 361 | patch.num_sources = 1; |
| 362 | patch.num_sinks = 1; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 363 | deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice); |
| 364 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 365 | "updateCallRouting() selected device not in output device list"); |
| 366 | sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0); |
| 367 | deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX); |
| 368 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 369 | "updateCallRouting() no telephony RX device"); |
| 370 | sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0); |
| 371 | |
| 372 | rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]); |
| 373 | rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]); |
| 374 | |
| 375 | // request to reuse existing output stream if one is already opened to reach the RX device |
| 376 | SortedVector<audio_io_handle_t> outputs = |
| 377 | getOutputsForDevice(rxDevice, mOutputs); |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 378 | audio_io_handle_t output = selectOutput(outputs, |
| 379 | AUDIO_OUTPUT_FLAG_NONE, |
| 380 | AUDIO_FORMAT_INVALID); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 381 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 382 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 383 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 384 | "updateCallRouting() RX device output is duplicated"); |
| 385 | outputDesc->toAudioPortConfig(&patch.sources[1]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 386 | patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 387 | patch.num_sources = 2; |
| 388 | } |
| 389 | |
| 390 | afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame^] | 391 | status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 392 | ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch", |
| 393 | status); |
| 394 | if (status == NO_ERROR) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 395 | mCallRxPatch = new AudioPatch(&patch, mUidCached); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 396 | mCallRxPatch->mAfPatchHandle = afPatchHandle; |
| 397 | mCallRxPatch->mUid = mUidCached; |
| 398 | } |
| 399 | createTxPatch = true; |
| 400 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 401 | if (createTxPatch) { // create TX path audio patch |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 402 | struct audio_patch patch; |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 403 | |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 404 | patch.num_sources = 1; |
| 405 | patch.num_sinks = 1; |
| 406 | deviceList = mAvailableInputDevices.getDevicesFromType(txDevice); |
| 407 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 408 | "updateCallRouting() selected device not in input device list"); |
| 409 | sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0); |
| 410 | txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]); |
| 411 | deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX); |
| 412 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 413 | "updateCallRouting() no telephony TX device"); |
| 414 | sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0); |
| 415 | txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]); |
| 416 | |
| 417 | SortedVector<audio_io_handle_t> outputs = |
| 418 | getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs); |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 419 | audio_io_handle_t output = selectOutput(outputs, |
| 420 | AUDIO_OUTPUT_FLAG_NONE, |
| 421 | AUDIO_FORMAT_INVALID); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 422 | // request to reuse existing output stream if one is already opened to reach the TX |
| 423 | // path output device |
| 424 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 425 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 426 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 427 | "updateCallRouting() RX device output is duplicated"); |
| 428 | outputDesc->toAudioPortConfig(&patch.sources[1]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 429 | patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 430 | patch.num_sources = 2; |
| 431 | } |
| 432 | |
Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 433 | // terminate active capture if on the same HW module as the call TX source device |
| 434 | // FIXME: would be better to refine to only inputs whose profile connects to the |
| 435 | // call TX device but this information is not in the audio patch and logic here must be |
| 436 | // symmetric to the one in startInput() |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 437 | audio_io_handle_t activeInput = mInputs.getActiveInput(); |
| 438 | if (activeInput != 0) { |
| 439 | sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput); |
| 440 | if (activeDesc->getModuleHandle() == txSourceDeviceDesc->getModuleHandle()) { |
| 441 | //FIXME: consider all active sessions |
| 442 | AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions(); |
| 443 | audio_session_t activeSession = activeSessions.keyAt(0); |
| 444 | stopInput(activeInput, activeSession); |
| 445 | releaseInput(activeInput, activeSession); |
Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 449 | afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame^] | 450 | status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 451 | ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch", |
| 452 | status); |
| 453 | if (status == NO_ERROR) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 454 | mCallTxPatch = new AudioPatch(&patch, mUidCached); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 455 | mCallTxPatch->mAfPatchHandle = afPatchHandle; |
| 456 | mCallTxPatch->mUid = mUidCached; |
| 457 | } |
| 458 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame^] | 459 | |
| 460 | return muteWaitMs; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 463 | void AudioPolicyManager::setPhoneState(audio_mode_t state) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 464 | { |
| 465 | ALOGV("setPhoneState() state %d", state); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 466 | // store previous phone state for management of sonification strategy below |
| 467 | int oldState = mEngine->getPhoneState(); |
| 468 | |
| 469 | if (mEngine->setPhoneState(state) != NO_ERROR) { |
| 470 | ALOGW("setPhoneState() invalid or same state %d", state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 471 | return; |
| 472 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 473 | /// Opens: can these line be executed after the switch of volume curves??? |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 474 | // if leaving call state, handle special case of active streams |
| 475 | // pertaining to sonification strategy see handleIncallSonification() |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 476 | if (isStateInCall(oldState)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 477 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 478 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 479 | handleIncallSonification((audio_stream_type_t)stream, false, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 480 | } |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 481 | |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 482 | // force reevaluating accessibility routing when call stops |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 483 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 484 | } |
| 485 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 486 | /** |
| 487 | * Switching to or from incall state or switching between telephony and VoIP lead to force |
| 488 | * routing command. |
| 489 | */ |
| 490 | bool force = ((is_state_in_call(oldState) != is_state_in_call(state)) |
| 491 | || (is_state_in_call(state) && (state != oldState))); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 492 | |
| 493 | // check for device and output changes triggered by new phone state |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 494 | checkA2dpSuspend(); |
| 495 | checkOutputForAllStrategies(); |
| 496 | updateDevicesAndOutputs(); |
| 497 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 498 | int delayMs = 0; |
| 499 | if (isStateInCall(state)) { |
| 500 | nsecs_t sysTime = systemTime(); |
| 501 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 502 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 503 | // mute media and sonification strategies and delay device switch by the largest |
| 504 | // latency of any output where either strategy is active. |
| 505 | // 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] | 506 | if ((isStrategyActive(desc, STRATEGY_MEDIA, |
| 507 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 508 | sysTime) || |
| 509 | isStrategyActive(desc, STRATEGY_SONIFICATION, |
| 510 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 511 | sysTime)) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 512 | (delayMs < (int)desc->latency()*2)) { |
| 513 | delayMs = desc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 514 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 515 | setStrategyMute(STRATEGY_MEDIA, true, desc); |
| 516 | setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 517 | getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/)); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 518 | setStrategyMute(STRATEGY_SONIFICATION, true, desc); |
| 519 | setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 520 | getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/)); |
| 521 | } |
| 522 | } |
| 523 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 524 | if (hasPrimaryOutput()) { |
| 525 | // Note that despite the fact that getNewOutputDevice() is called on the primary output, |
| 526 | // the device returned is not necessarily reachable via this output |
| 527 | audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 528 | // force routing command to audio hardware when ending call |
| 529 | // even if no device change is needed |
| 530 | if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) { |
| 531 | rxDevice = mPrimaryOutput->device(); |
| 532 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 533 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 534 | if (state == AUDIO_MODE_IN_CALL) { |
| 535 | updateCallRouting(rxDevice, delayMs); |
| 536 | } else if (oldState == AUDIO_MODE_IN_CALL) { |
| 537 | if (mCallRxPatch != 0) { |
| 538 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 539 | mCallRxPatch.clear(); |
| 540 | } |
| 541 | if (mCallTxPatch != 0) { |
| 542 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 543 | mCallTxPatch.clear(); |
| 544 | } |
| 545 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
| 546 | } else { |
| 547 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 548 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 549 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 550 | // if entering in call state, handle special case of active streams |
| 551 | // pertaining to sonification strategy see handleIncallSonification() |
| 552 | if (isStateInCall(state)) { |
| 553 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 554 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 555 | handleIncallSonification((audio_stream_type_t)stream, true, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 556 | } |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 557 | |
| 558 | // force reevaluating accessibility routing when call starts |
| 559 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | // 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] | 563 | if (state == AUDIO_MODE_RINGTONE && |
| 564 | isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 565 | mLimitRingtoneVolume = true; |
| 566 | } else { |
| 567 | mLimitRingtoneVolume = false; |
| 568 | } |
| 569 | } |
| 570 | |
Jean-Michel Trivi | 887a9ed | 2015-03-31 18:02:24 -0700 | [diff] [blame] | 571 | audio_mode_t AudioPolicyManager::getPhoneState() { |
| 572 | return mEngine->getPhoneState(); |
| 573 | } |
| 574 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 575 | void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 576 | audio_policy_forced_cfg_t config) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 577 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 578 | ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 579 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 580 | if (mEngine->setForceUse(usage, config) != NO_ERROR) { |
| 581 | ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage); |
| 582 | return; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 583 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 584 | bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) || |
| 585 | (usage == AUDIO_POLICY_FORCE_FOR_DOCK) || |
| 586 | (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 587 | |
| 588 | // check for device and output changes triggered by new force usage |
| 589 | checkA2dpSuspend(); |
| 590 | checkOutputForAllStrategies(); |
| 591 | updateDevicesAndOutputs(); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 592 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame^] | 593 | //FIXME: workaround for truncated touch sounds |
| 594 | // to be removed when the problem is handled by system UI |
| 595 | uint32_t delayMs = 0; |
| 596 | uint32_t waitMs = 0; |
| 597 | if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) { |
| 598 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 599 | } |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 600 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 601 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/); |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame^] | 602 | waitMs = updateCallRouting(newDevice, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 603 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 604 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 605 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
| 606 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/); |
| 607 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame^] | 608 | waitMs = setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE), |
| 609 | delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 610 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 611 | if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame^] | 612 | applyStreamVolumes(outputDesc, newDevice, waitMs, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 613 | } |
| 614 | } |
| 615 | |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 616 | audio_io_handle_t activeInput = mInputs.getActiveInput(); |
| 617 | if (activeInput != 0) { |
| 618 | sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput); |
| 619 | audio_devices_t newDevice = getNewInputDevice(activeInput); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 620 | // 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] | 621 | if (activeDesc->mProfile->getSupportedDevices().types() & (newDevice & ~AUDIO_DEVICE_BIT_IN)) { |
| 622 | setInputDevice(activeInput, newDevice); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 623 | } else { |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 624 | closeInput(activeInput); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 625 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 626 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 627 | } |
| 628 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 629 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 630 | { |
| 631 | ALOGV("setSystemProperty() property %s, value %s", property, value); |
| 632 | } |
| 633 | |
| 634 | // Find a direct output profile compatible with the parameters passed, even if the input flags do |
| 635 | // not explicitly request a direct output |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 636 | sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput( |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 637 | audio_devices_t device, |
| 638 | uint32_t samplingRate, |
| 639 | audio_format_t format, |
| 640 | audio_channel_mask_t channelMask, |
| 641 | audio_output_flags_t flags) |
| 642 | { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 643 | // only retain flags that will drive the direct output profile selection |
| 644 | // if explicitly requested |
| 645 | static const uint32_t kRelevantFlags = |
| 646 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
| 647 | flags = |
| 648 | (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); |
| 649 | |
| 650 | sp<IOProfile> profile; |
| 651 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 652 | for (size_t i = 0; i < mHwModules.size(); i++) { |
| 653 | if (mHwModules[i]->mHandle == 0) { |
| 654 | continue; |
| 655 | } |
| 656 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 657 | sp<IOProfile> curProfile = mHwModules[i]->mOutputProfiles[j]; |
| 658 | if (!curProfile->isCompatibleProfile(device, String8(""), |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 659 | samplingRate, NULL /*updatedSamplingRate*/, |
| 660 | format, NULL /*updatedFormat*/, |
| 661 | channelMask, NULL /*updatedChannelMask*/, |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 662 | flags)) { |
| 663 | continue; |
| 664 | } |
| 665 | // reject profiles not corresponding to a device currently available |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 666 | if ((mAvailableOutputDevices.types() & curProfile->getSupportedDevicesType()) == 0) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 667 | continue; |
| 668 | } |
| 669 | // 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] | 670 | if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 671 | continue; |
| 672 | } |
| 673 | profile = curProfile; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 674 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 675 | break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 676 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 677 | } |
| 678 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 679 | return profile; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 680 | } |
| 681 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 682 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 683 | uint32_t samplingRate, |
| 684 | audio_format_t format, |
| 685 | audio_channel_mask_t channelMask, |
| 686 | audio_output_flags_t flags, |
| 687 | const audio_offload_info_t *offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 688 | { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 689 | routing_strategy strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 690 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 691 | ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x", |
| 692 | device, stream, samplingRate, format, channelMask, flags); |
| 693 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 694 | return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE, |
| 695 | stream, samplingRate,format, channelMask, |
| 696 | flags, offloadInfo); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 697 | } |
| 698 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 699 | status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, |
| 700 | audio_io_handle_t *output, |
| 701 | audio_session_t session, |
| 702 | audio_stream_type_t *stream, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 703 | uid_t uid, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 704 | uint32_t samplingRate, |
| 705 | audio_format_t format, |
| 706 | audio_channel_mask_t channelMask, |
| 707 | audio_output_flags_t flags, |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 708 | audio_port_handle_t selectedDeviceId, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 709 | const audio_offload_info_t *offloadInfo) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 710 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 711 | audio_attributes_t attributes; |
| 712 | if (attr != NULL) { |
| 713 | if (!isValidAttributes(attr)) { |
| 714 | ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]", |
| 715 | attr->usage, attr->content_type, attr->flags, |
| 716 | attr->tags); |
| 717 | return BAD_VALUE; |
| 718 | } |
| 719 | attributes = *attr; |
| 720 | } else { |
| 721 | if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) { |
| 722 | ALOGE("getOutputForAttr(): invalid stream type"); |
| 723 | return BAD_VALUE; |
| 724 | } |
| 725 | stream_type_to_audio_attributes(*stream, &attributes); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 726 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 727 | sp<SwAudioOutputDescriptor> desc; |
Jean-Michel Trivi | e8deced | 2016-02-11 12:50:39 -0800 | [diff] [blame] | 728 | if (mPolicyMixes.getOutputForAttr(attributes, uid, desc) == NO_ERROR) { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 729 | ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr"); |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 730 | if (!audio_has_proportional_frames(format)) { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 731 | return BAD_VALUE; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 732 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 733 | *stream = streamTypefromAttributesInt(&attributes); |
| 734 | *output = desc->mIoHandle; |
| 735 | ALOGV("getOutputForAttr() returns output %d", *output); |
| 736 | return NO_ERROR; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 737 | } |
| 738 | if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 739 | ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE"); |
| 740 | return BAD_VALUE; |
| 741 | } |
| 742 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 743 | ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x" |
| 744 | " session %d selectedDeviceId %d", |
| 745 | attributes.usage, attributes.content_type, attributes.tags, attributes.flags, |
| 746 | session, selectedDeviceId); |
| 747 | |
| 748 | *stream = streamTypefromAttributesInt(&attributes); |
| 749 | |
| 750 | // Explicit routing? |
| 751 | sp<DeviceDescriptor> deviceDesc; |
| 752 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { |
| 753 | if (mAvailableOutputDevices[i]->getId() == selectedDeviceId) { |
| 754 | deviceDesc = mAvailableOutputDevices[i]; |
| 755 | break; |
| 756 | } |
| 757 | } |
| 758 | mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 759 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 760 | routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 761 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 762 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 763 | if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) { |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 764 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
| 765 | } |
| 766 | |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 767 | 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] | 768 | device, samplingRate, format, channelMask, flags); |
| 769 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 770 | *output = getOutputForDevice(device, session, *stream, |
| 771 | samplingRate, format, channelMask, |
| 772 | flags, offloadInfo); |
| 773 | if (*output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 774 | mOutputRoutes.removeRoute(session); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 775 | return INVALID_OPERATION; |
| 776 | } |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 777 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 778 | return NO_ERROR; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | audio_io_handle_t AudioPolicyManager::getOutputForDevice( |
| 782 | audio_devices_t device, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 783 | audio_session_t session __unused, |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 784 | audio_stream_type_t stream, |
| 785 | uint32_t samplingRate, |
| 786 | audio_format_t format, |
| 787 | audio_channel_mask_t channelMask, |
| 788 | audio_output_flags_t flags, |
| 789 | const audio_offload_info_t *offloadInfo) |
| 790 | { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 791 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 792 | status_t status; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 793 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 794 | #ifdef AUDIO_POLICY_TEST |
| 795 | if (mCurOutput != 0) { |
| 796 | ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d", |
| 797 | mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput); |
| 798 | |
| 799 | if (mTestOutputs[mCurOutput] == 0) { |
| 800 | ALOGV("getOutput() opening test output"); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 801 | sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL, |
| 802 | mpClientInterface); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 803 | outputDesc->mDevice = mTestDevice; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 804 | outputDesc->mLatency = mTestLatencyMs; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 805 | outputDesc->mFlags = |
| 806 | (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 807 | outputDesc->mRefCount[stream] = 0; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 808 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 809 | config.sample_rate = mTestSamplingRate; |
| 810 | config.channel_mask = mTestChannels; |
| 811 | config.format = mTestFormat; |
Phil Burk | 77cce80 | 2014-08-04 16:18:15 -0700 | [diff] [blame] | 812 | if (offloadInfo != NULL) { |
| 813 | config.offload_info = *offloadInfo; |
| 814 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 815 | status = mpClientInterface->openOutput(0, |
| 816 | &mTestOutputs[mCurOutput], |
| 817 | &config, |
| 818 | &outputDesc->mDevice, |
| 819 | String8(""), |
| 820 | &outputDesc->mLatency, |
| 821 | outputDesc->mFlags); |
| 822 | if (status == NO_ERROR) { |
| 823 | outputDesc->mSamplingRate = config.sample_rate; |
| 824 | outputDesc->mFormat = config.format; |
| 825 | outputDesc->mChannelMask = config.channel_mask; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 826 | AudioParameter outputCmd = AudioParameter(); |
| 827 | outputCmd.addInt(String8("set_id"),mCurOutput); |
| 828 | mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString()); |
| 829 | addOutput(mTestOutputs[mCurOutput], outputDesc); |
| 830 | } |
| 831 | } |
| 832 | return mTestOutputs[mCurOutput]; |
| 833 | } |
| 834 | #endif //AUDIO_POLICY_TEST |
| 835 | |
| 836 | // open a direct output if required by specified parameters |
| 837 | //force direct flag if offload flag is set: offloading implies a direct output stream |
| 838 | // and all common behaviors are driven by checking only the direct flag |
| 839 | // this should normally be set appropriately in the policy configuration file |
| 840 | if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 841 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 842 | } |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 843 | if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 844 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT); |
| 845 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 846 | // only allow deep buffering for music stream type |
| 847 | if (stream != AUDIO_STREAM_MUSIC) { |
| 848 | flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 849 | } else if (/* stream == AUDIO_STREAM_MUSIC && */ |
| 850 | flags == AUDIO_OUTPUT_FLAG_NONE && |
| 851 | property_get_bool("audio.deep_buffer.media", false /* default_value */)) { |
| 852 | // use DEEP_BUFFER as default output for music stream type |
| 853 | flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER; |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 854 | } |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 855 | if (stream == AUDIO_STREAM_TTS) { |
| 856 | flags = AUDIO_OUTPUT_FLAG_TTS; |
| 857 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 858 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 859 | sp<IOProfile> profile; |
| 860 | |
| 861 | // 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] | 862 | // and not explicitly requested |
| 863 | if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 864 | audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX && |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 865 | audio_channel_count_from_out_mask(channelMask) <= 2) { |
| 866 | goto non_direct_output; |
| 867 | } |
| 868 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 869 | // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled. |
| 870 | // This prevents creating an offloaded track and tearing it down immediately after start |
| 871 | // when audioflinger detects there is an active non offloadable effect. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 872 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 873 | // This may prevent offloading in rare situations where effects are left active by apps |
| 874 | // in the background. |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 875 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 876 | if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 877 | !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 878 | profile = getProfileForDirectOutput(device, |
| 879 | samplingRate, |
| 880 | format, |
| 881 | channelMask, |
| 882 | (audio_output_flags_t)flags); |
| 883 | } |
| 884 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 885 | if (profile != 0) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 886 | sp<SwAudioOutputDescriptor> outputDesc = NULL; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 887 | |
| 888 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 889 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 890 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { |
| 891 | outputDesc = desc; |
| 892 | // reuse direct output if currently open and configured with same parameters |
| 893 | if ((samplingRate == outputDesc->mSamplingRate) && |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 894 | audio_formats_match(format, outputDesc->mFormat) && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 895 | (channelMask == outputDesc->mChannelMask)) { |
| 896 | outputDesc->mDirectOpenCount++; |
| 897 | ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i)); |
| 898 | return mOutputs.keyAt(i); |
| 899 | } |
| 900 | } |
| 901 | } |
| 902 | // close direct output if currently open and configured with different parameters |
| 903 | if (outputDesc != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 904 | closeOutput(outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 905 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 906 | |
| 907 | // if the selected profile is offloaded and no offload info was specified, |
| 908 | // create a default one |
| 909 | audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 910 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 911 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
| 912 | defaultOffloadInfo.sample_rate = samplingRate; |
| 913 | defaultOffloadInfo.channel_mask = channelMask; |
| 914 | defaultOffloadInfo.format = format; |
| 915 | defaultOffloadInfo.stream_type = stream; |
| 916 | defaultOffloadInfo.bit_rate = 0; |
| 917 | defaultOffloadInfo.duration_us = -1; |
| 918 | defaultOffloadInfo.has_video = true; // conservative |
| 919 | defaultOffloadInfo.is_streaming = true; // likely |
| 920 | offloadInfo = &defaultOffloadInfo; |
| 921 | } |
| 922 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 923 | outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 924 | outputDesc->mDevice = device; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 925 | outputDesc->mLatency = 0; |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 926 | outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 927 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 928 | config.sample_rate = samplingRate; |
| 929 | config.channel_mask = channelMask; |
| 930 | config.format = format; |
Phil Burk | 77cce80 | 2014-08-04 16:18:15 -0700 | [diff] [blame] | 931 | if (offloadInfo != NULL) { |
| 932 | config.offload_info = *offloadInfo; |
| 933 | } |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 934 | status = mpClientInterface->openOutput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 935 | &output, |
| 936 | &config, |
| 937 | &outputDesc->mDevice, |
| 938 | String8(""), |
| 939 | &outputDesc->mLatency, |
| 940 | outputDesc->mFlags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 941 | |
| 942 | // only accept an output with the requested parameters |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 943 | if (status != NO_ERROR || |
| 944 | (samplingRate != 0 && samplingRate != config.sample_rate) || |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 945 | (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) || |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 946 | (channelMask != 0 && channelMask != config.channel_mask)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 947 | ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d," |
| 948 | "format %d %d, channelMask %04x %04x", output, samplingRate, |
| 949 | outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask, |
| 950 | outputDesc->mChannelMask); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 951 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 952 | mpClientInterface->closeOutput(output); |
| 953 | } |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 954 | // 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] | 955 | if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) { |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 956 | goto non_direct_output; |
| 957 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 958 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 959 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 960 | outputDesc->mSamplingRate = config.sample_rate; |
| 961 | outputDesc->mChannelMask = config.channel_mask; |
| 962 | outputDesc->mFormat = config.format; |
| 963 | outputDesc->mRefCount[stream] = 0; |
| 964 | outputDesc->mStopTime[stream] = 0; |
| 965 | outputDesc->mDirectOpenCount = 1; |
| 966 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 967 | audio_io_handle_t srcOutput = getOutputForEffect(); |
| 968 | addOutput(output, outputDesc); |
| 969 | audio_io_handle_t dstOutput = getOutputForEffect(); |
| 970 | if (dstOutput == output) { |
| 971 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput); |
| 972 | } |
| 973 | mPreviousOutputs = mOutputs; |
| 974 | ALOGV("getOutput() returns new direct output %d", output); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 975 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 976 | return output; |
| 977 | } |
| 978 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 979 | non_direct_output: |
Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 980 | |
| 981 | // A request for HW A/V sync cannot fallback to a mixed output because time |
| 982 | // stamps are embedded in audio data |
| 983 | if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 984 | return AUDIO_IO_HANDLE_NONE; |
| 985 | } |
| 986 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 987 | // ignoring channel mask due to downmix capability in mixer |
| 988 | |
| 989 | // open a non direct output |
| 990 | |
| 991 | // for non direct outputs, only PCM is supported |
| 992 | if (audio_is_linear_pcm(format)) { |
| 993 | // get which output is suitable for the specified stream. The actual |
| 994 | // routing change will happen when startOutput() will be called |
| 995 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 996 | |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 997 | // at this stage we should ignore the DIRECT flag as no direct output could be found earlier |
| 998 | flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT); |
| 999 | output = selectOutput(outputs, flags, format); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1000 | } |
| 1001 | ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d," |
| 1002 | "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags); |
| 1003 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1004 | ALOGV(" getOutputForDevice() returns output %d", output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1005 | |
| 1006 | return output; |
| 1007 | } |
| 1008 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1009 | 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] | 1010 | audio_output_flags_t flags, |
| 1011 | audio_format_t format) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1012 | { |
| 1013 | // select one output among several that provide a path to a particular device or set of |
| 1014 | // devices (the list was previously build by getOutputsForDevice()). |
| 1015 | // The priority is as follows: |
| 1016 | // 1: the output with the highest number of requested policy flags |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1017 | // 2: the output with the bit depth the closest to the requested one |
| 1018 | // 3: the primary output |
| 1019 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1020 | |
| 1021 | if (outputs.size() == 0) { |
| 1022 | return 0; |
| 1023 | } |
| 1024 | if (outputs.size() == 1) { |
| 1025 | return outputs[0]; |
| 1026 | } |
| 1027 | |
| 1028 | int maxCommonFlags = 0; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1029 | audio_io_handle_t outputForFlags = 0; |
| 1030 | audio_io_handle_t outputForPrimary = 0; |
| 1031 | audio_io_handle_t outputForFormat = 0; |
| 1032 | audio_format_t bestFormat = AUDIO_FORMAT_INVALID; |
| 1033 | audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1034 | |
| 1035 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1036 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1037 | if (!outputDesc->isDuplicated()) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1038 | // if a valid format is specified, skip output if not compatible |
| 1039 | if (format != AUDIO_FORMAT_INVALID) { |
| 1040 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1041 | if (!audio_formats_match(format, outputDesc->mFormat)) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1042 | continue; |
| 1043 | } |
| 1044 | } else if (!audio_is_linear_pcm(format)) { |
| 1045 | continue; |
| 1046 | } |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1047 | if (AudioPort::isBetterFormatMatch( |
| 1048 | outputDesc->mFormat, bestFormat, format)) { |
| 1049 | outputForFormat = outputs[i]; |
| 1050 | bestFormat = outputDesc->mFormat; |
| 1051 | } |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1052 | } |
| 1053 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1054 | int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags); |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1055 | if (commonFlags >= maxCommonFlags) { |
| 1056 | if (commonFlags == maxCommonFlags) { |
| 1057 | if (AudioPort::isBetterFormatMatch( |
| 1058 | outputDesc->mFormat, bestFormatForFlags, format)) { |
| 1059 | outputForFlags = outputs[i]; |
| 1060 | bestFormatForFlags = outputDesc->mFormat; |
| 1061 | } |
| 1062 | } else { |
| 1063 | outputForFlags = outputs[i]; |
| 1064 | maxCommonFlags = commonFlags; |
| 1065 | bestFormatForFlags = outputDesc->mFormat; |
| 1066 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1067 | ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags); |
| 1068 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1069 | if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1070 | outputForPrimary = outputs[i]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1071 | } |
| 1072 | } |
| 1073 | } |
| 1074 | |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1075 | if (outputForFlags != 0) { |
| 1076 | return outputForFlags; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1077 | } |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1078 | if (outputForFormat != 0) { |
| 1079 | return outputForFormat; |
| 1080 | } |
| 1081 | if (outputForPrimary != 0) { |
| 1082 | return outputForPrimary; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | return outputs[0]; |
| 1086 | } |
| 1087 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1088 | status_t AudioPolicyManager::startOutput(audio_io_handle_t output, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1089 | audio_stream_type_t stream, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1090 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1091 | { |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1092 | ALOGV("startOutput() output %d, stream %d, session %d", |
| 1093 | output, stream, session); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1094 | ssize_t index = mOutputs.indexOfKey(output); |
| 1095 | if (index < 0) { |
| 1096 | ALOGW("startOutput() unknown output %d", output); |
| 1097 | return BAD_VALUE; |
| 1098 | } |
| 1099 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1100 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
| 1101 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1102 | // Routing? |
| 1103 | mOutputRoutes.incRouteActivity(session); |
| 1104 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1105 | audio_devices_t newDevice; |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1106 | AudioMix *policyMix = NULL; |
| 1107 | const char *address = NULL; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1108 | if (outputDesc->mPolicyMix != NULL) { |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1109 | policyMix = outputDesc->mPolicyMix; |
| 1110 | address = policyMix->mDeviceAddress.string(); |
| 1111 | if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 1112 | newDevice = policyMix->mDeviceType; |
| 1113 | } else { |
| 1114 | newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
| 1115 | } |
Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 1116 | } else if (mOutputRoutes.hasRouteChanged(session)) { |
| 1117 | newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1118 | checkStrategyRoute(getStrategy(stream), output); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1119 | } else { |
| 1120 | newDevice = AUDIO_DEVICE_NONE; |
| 1121 | } |
| 1122 | |
| 1123 | uint32_t delayMs = 0; |
| 1124 | |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1125 | status_t status = startSource(outputDesc, stream, newDevice, address, &delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1126 | |
| 1127 | if (status != NO_ERROR) { |
| 1128 | mOutputRoutes.decRouteActivity(session); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1129 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1130 | } |
| 1131 | // Automatically enable the remote submix input when output is started on a re routing mix |
| 1132 | // of type MIX_TYPE_RECORDERS |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1133 | if (audio_is_remote_submix_device(newDevice) && policyMix != NULL && |
| 1134 | policyMix->mMixType == MIX_TYPE_RECORDERS) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1135 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1136 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1137 | address, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1138 | "remote-submix"); |
| 1139 | } |
| 1140 | |
| 1141 | if (delayMs != 0) { |
| 1142 | usleep(delayMs * 1000); |
| 1143 | } |
| 1144 | |
| 1145 | return status; |
| 1146 | } |
| 1147 | |
| 1148 | status_t AudioPolicyManager::startSource(sp<AudioOutputDescriptor> outputDesc, |
| 1149 | audio_stream_type_t stream, |
| 1150 | audio_devices_t device, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1151 | const char *address, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1152 | uint32_t *delayMs) |
| 1153 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1154 | // cannot start playback of STREAM_TTS if any other output is being used |
| 1155 | uint32_t beaconMuteLatency = 0; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1156 | |
| 1157 | *delayMs = 0; |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1158 | if (stream == AUDIO_STREAM_TTS) { |
| 1159 | ALOGV("\t found BEACON stream"); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 1160 | if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1161 | return INVALID_OPERATION; |
| 1162 | } else { |
| 1163 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); |
| 1164 | } |
| 1165 | } else { |
| 1166 | // some playback other than beacon starts |
| 1167 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); |
| 1168 | } |
| 1169 | |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1170 | // check active before incrementing usage count |
| 1171 | bool force = !outputDesc->isActive(); |
| 1172 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1173 | // increment usage count for this stream on the requested output: |
| 1174 | // NOTE that the usage count is the same for duplicated output and hardware output which is |
| 1175 | // necessary for a correct control of hardware output routing by startOutput() and stopOutput() |
| 1176 | outputDesc->changeRefCount(stream, 1); |
| 1177 | |
Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 1178 | if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1179 | // starting an output being rerouted? |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1180 | if (device == AUDIO_DEVICE_NONE) { |
| 1181 | device = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1182 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1183 | routing_strategy strategy = getStrategy(stream); |
| 1184 | bool shouldWait = (strategy == STRATEGY_SONIFICATION) || |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1185 | (strategy == STRATEGY_SONIFICATION_RESPECTFUL) || |
| 1186 | (beaconMuteLatency > 0); |
| 1187 | uint32_t waitMs = beaconMuteLatency; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1188 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1189 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1190 | if (desc != outputDesc) { |
| 1191 | // force a device change if any other output is managed by the same hw |
| 1192 | // module and has a current device selection that differs from selected device. |
| 1193 | // In this case, the audio HAL must receive the new device selection so that it can |
| 1194 | // change the device currently selected by the other active output. |
| 1195 | if (outputDesc->sharesHwModuleWith(desc) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1196 | desc->device() != device) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1197 | force = true; |
| 1198 | } |
| 1199 | // 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] | 1200 | // a notification so that audio focus effect can propagate, or that a mute/unmute |
| 1201 | // event occurred for beacon |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1202 | uint32_t latency = desc->latency(); |
| 1203 | if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) { |
| 1204 | waitMs = latency; |
| 1205 | } |
| 1206 | } |
| 1207 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame^] | 1208 | uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force, 0, NULL, address); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1209 | |
| 1210 | // handle special case for sonification while in call |
| 1211 | if (isInCall()) { |
| 1212 | handleIncallSonification(stream, true, false); |
| 1213 | } |
| 1214 | |
| 1215 | // apply volume rules for current stream and device if necessary |
| 1216 | checkAndSetVolume(stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 1217 | mVolumeCurves->getVolumeIndex(stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1218 | outputDesc, |
| 1219 | device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1220 | |
| 1221 | // update the outputs if starting an output with a stream that can affect notification |
| 1222 | // routing |
| 1223 | handleNotificationRoutingForStream(stream); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1224 | |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 1225 | // force reevaluating accessibility routing when ringtone or alarm starts |
| 1226 | if (strategy == STRATEGY_SONIFICATION) { |
| 1227 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
| 1228 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame^] | 1229 | |
| 1230 | if (waitMs > muteWaitMs) { |
| 1231 | *delayMs = waitMs - muteWaitMs; |
| 1232 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1233 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame^] | 1234 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1235 | return NO_ERROR; |
| 1236 | } |
| 1237 | |
| 1238 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1239 | status_t AudioPolicyManager::stopOutput(audio_io_handle_t output, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1240 | audio_stream_type_t stream, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1241 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1242 | { |
| 1243 | ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session); |
| 1244 | ssize_t index = mOutputs.indexOfKey(output); |
| 1245 | if (index < 0) { |
| 1246 | ALOGW("stopOutput() unknown output %d", output); |
| 1247 | return BAD_VALUE; |
| 1248 | } |
| 1249 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1250 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1251 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1252 | if (outputDesc->mRefCount[stream] == 1) { |
| 1253 | // Automatically disable the remote submix input when output is stopped on a |
| 1254 | // re routing mix of type MIX_TYPE_RECORDERS |
| 1255 | if (audio_is_remote_submix_device(outputDesc->mDevice) && |
| 1256 | outputDesc->mPolicyMix != NULL && |
| 1257 | outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1258 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1259 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1260 | outputDesc->mPolicyMix->mDeviceAddress, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1261 | "remote-submix"); |
| 1262 | } |
| 1263 | } |
| 1264 | |
| 1265 | // Routing? |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1266 | bool forceDeviceUpdate = false; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1267 | if (outputDesc->mRefCount[stream] > 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1268 | int activityCount = mOutputRoutes.decRouteActivity(session); |
| 1269 | forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0)); |
| 1270 | |
| 1271 | if (forceDeviceUpdate) { |
| 1272 | checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE); |
| 1273 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1274 | } |
| 1275 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1276 | return stopSource(outputDesc, stream, forceDeviceUpdate); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1277 | } |
| 1278 | |
| 1279 | status_t AudioPolicyManager::stopSource(sp<AudioOutputDescriptor> outputDesc, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1280 | audio_stream_type_t stream, |
| 1281 | bool forceDeviceUpdate) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1282 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1283 | // always handle stream stop, check which stream type is stopping |
| 1284 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); |
| 1285 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1286 | // handle special case for sonification while in call |
| 1287 | if (isInCall()) { |
| 1288 | handleIncallSonification(stream, false, false); |
| 1289 | } |
| 1290 | |
| 1291 | if (outputDesc->mRefCount[stream] > 0) { |
| 1292 | // decrement usage count of this stream on the output |
| 1293 | outputDesc->changeRefCount(stream, -1); |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1294 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1295 | // store time at which the stream was stopped - see isStreamActive() |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1296 | if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1297 | outputDesc->mStopTime[stream] = systemTime(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1298 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1299 | // delay the device switch by twice the latency because stopOutput() is executed when |
| 1300 | // the track stop() command is received and at that time the audio track buffer can |
| 1301 | // still contain data that needs to be drained. The latency only covers the audio HAL |
| 1302 | // and kernel buffers. Also the latency does not always include additional delay in the |
| 1303 | // audio path (audio DSP, CODEC ...) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1304 | setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1305 | |
| 1306 | // force restoring the device selection on other active outputs if it differs from the |
| 1307 | // one being selected for this output |
| 1308 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1309 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1310 | if (desc != outputDesc && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1311 | desc->isActive() && |
| 1312 | outputDesc->sharesHwModuleWith(desc) && |
| 1313 | (newDevice != desc->device())) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1314 | setOutputDevice(desc, |
| 1315 | getNewOutputDevice(desc, false /*fromCache*/), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1316 | true, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1317 | outputDesc->latency()*2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1318 | } |
| 1319 | } |
| 1320 | // update the outputs if stopping one with a stream that can affect notification routing |
| 1321 | handleNotificationRoutingForStream(stream); |
| 1322 | } |
| 1323 | return NO_ERROR; |
| 1324 | } else { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1325 | ALOGW("stopOutput() refcount is already 0"); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1326 | return INVALID_OPERATION; |
| 1327 | } |
| 1328 | } |
| 1329 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1330 | void AudioPolicyManager::releaseOutput(audio_io_handle_t output, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1331 | audio_stream_type_t stream __unused, |
| 1332 | audio_session_t session __unused) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1333 | { |
| 1334 | ALOGV("releaseOutput() %d", output); |
| 1335 | ssize_t index = mOutputs.indexOfKey(output); |
| 1336 | if (index < 0) { |
| 1337 | ALOGW("releaseOutput() releasing unknown output %d", output); |
| 1338 | return; |
| 1339 | } |
| 1340 | |
| 1341 | #ifdef AUDIO_POLICY_TEST |
| 1342 | int testIndex = testOutputIndex(output); |
| 1343 | if (testIndex != 0) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1344 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1345 | if (outputDesc->isActive()) { |
| 1346 | mpClientInterface->closeOutput(output); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1347 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1348 | mTestOutputs[testIndex] = 0; |
| 1349 | } |
| 1350 | return; |
| 1351 | } |
| 1352 | #endif //AUDIO_POLICY_TEST |
| 1353 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1354 | // Routing |
| 1355 | mOutputRoutes.removeRoute(session); |
| 1356 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1357 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1358 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1359 | if (desc->mDirectOpenCount <= 0) { |
| 1360 | ALOGW("releaseOutput() invalid open count %d for output %d", |
| 1361 | desc->mDirectOpenCount, output); |
| 1362 | return; |
| 1363 | } |
| 1364 | if (--desc->mDirectOpenCount == 0) { |
| 1365 | closeOutput(output); |
| 1366 | // If effects where present on the output, audioflinger moved them to the primary |
| 1367 | // output by default: move them back to the appropriate output. |
| 1368 | audio_io_handle_t dstOutput = getOutputForEffect(); |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 1369 | if (hasPrimaryOutput() && dstOutput != mPrimaryOutput->mIoHandle) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1370 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, |
| 1371 | mPrimaryOutput->mIoHandle, dstOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1372 | } |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1373 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1374 | } |
| 1375 | } |
| 1376 | } |
| 1377 | |
| 1378 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1379 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, |
| 1380 | audio_io_handle_t *input, |
| 1381 | audio_session_t session, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1382 | uid_t uid, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1383 | uint32_t samplingRate, |
| 1384 | audio_format_t format, |
| 1385 | audio_channel_mask_t channelMask, |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1386 | audio_input_flags_t flags, |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1387 | audio_port_handle_t selectedDeviceId, |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1388 | input_type_t *inputType) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1389 | { |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1390 | ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x," |
| 1391 | "session %d, flags %#x", |
| 1392 | attr->source, samplingRate, format, channelMask, session, flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1393 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1394 | *input = AUDIO_IO_HANDLE_NONE; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1395 | *inputType = API_INPUT_INVALID; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1396 | audio_devices_t device; |
| 1397 | // handle legacy remote submix case where the address was not always specified |
| 1398 | String8 address = String8(""); |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1399 | audio_source_t inputSource = attr->source; |
| 1400 | audio_source_t halInputSource; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1401 | AudioMix *policyMix = NULL; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1402 | |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1403 | if (inputSource == AUDIO_SOURCE_DEFAULT) { |
| 1404 | inputSource = AUDIO_SOURCE_MIC; |
| 1405 | } |
| 1406 | halInputSource = inputSource; |
| 1407 | |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1408 | // Explicit routing? |
| 1409 | sp<DeviceDescriptor> deviceDesc; |
| 1410 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { |
| 1411 | if (mAvailableInputDevices[i]->getId() == selectedDeviceId) { |
| 1412 | deviceDesc = mAvailableInputDevices[i]; |
| 1413 | break; |
| 1414 | } |
| 1415 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1416 | mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1417 | |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1418 | if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX && |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1419 | strncmp(attr->tags, "addr=", strlen("addr=")) == 0) { |
Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 1420 | status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 1421 | if (ret != NO_ERROR) { |
| 1422 | return ret; |
| 1423 | } |
| 1424 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1425 | device = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 1426 | address = String8(attr->tags + strlen("addr=")); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1427 | } else { |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1428 | device = getDeviceAndMixForInputSource(inputSource, &policyMix); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1429 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1430 | ALOGW("getInputForAttr() could not find device for source %d", inputSource); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1431 | return BAD_VALUE; |
| 1432 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1433 | if (policyMix != NULL) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1434 | address = policyMix->mDeviceAddress; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1435 | if (policyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1436 | // there is an external policy, but this input is attached to a mix of recorders, |
| 1437 | // meaning it receives audio injected into the framework, so the recorder doesn't |
| 1438 | // know about it and is therefore considered "legacy" |
| 1439 | *inputType = API_INPUT_LEGACY; |
| 1440 | } else { |
| 1441 | // recording a mix of players defined by an external policy, we're rerouting for |
| 1442 | // an external policy |
| 1443 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
| 1444 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1445 | } else if (audio_is_remote_submix_device(device)) { |
| 1446 | address = String8("0"); |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1447 | *inputType = API_INPUT_MIX_CAPTURE; |
Eric Laurent | 82db269 | 2015-08-07 13:59:42 -0700 | [diff] [blame] | 1448 | } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) { |
| 1449 | *inputType = API_INPUT_TELEPHONY_RX; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1450 | } else { |
| 1451 | *inputType = API_INPUT_LEGACY; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1452 | } |
Ravi Kumar Alamanda | b367f5b | 2015-08-25 08:21:37 -0700 | [diff] [blame] | 1453 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | *input = getInputForDevice(device, address, session, uid, inputSource, |
| 1457 | samplingRate, format, channelMask, flags, |
| 1458 | policyMix); |
| 1459 | if (*input == AUDIO_IO_HANDLE_NONE) { |
| 1460 | mInputRoutes.removeRoute(session); |
| 1461 | return INVALID_OPERATION; |
| 1462 | } |
| 1463 | ALOGV("getInputForAttr() returns input type = %d", *inputType); |
| 1464 | return NO_ERROR; |
| 1465 | } |
| 1466 | |
| 1467 | |
| 1468 | audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device, |
| 1469 | String8 address, |
| 1470 | audio_session_t session, |
| 1471 | uid_t uid, |
| 1472 | audio_source_t inputSource, |
| 1473 | uint32_t samplingRate, |
| 1474 | audio_format_t format, |
| 1475 | audio_channel_mask_t channelMask, |
| 1476 | audio_input_flags_t flags, |
| 1477 | AudioMix *policyMix) |
| 1478 | { |
| 1479 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 1480 | audio_source_t halInputSource = inputSource; |
| 1481 | bool isSoundTrigger = false; |
| 1482 | |
| 1483 | if (inputSource == AUDIO_SOURCE_HOTWORD) { |
| 1484 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); |
| 1485 | if (index >= 0) { |
| 1486 | input = mSoundTriggerSessions.valueFor(session); |
| 1487 | isSoundTrigger = true; |
| 1488 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); |
| 1489 | ALOGV("SoundTrigger capture on session %d input %d", session, input); |
| 1490 | } else { |
| 1491 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1492 | } |
| 1493 | } |
| 1494 | |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1495 | // find a compatible input profile (not necessarily identical in parameters) |
| 1496 | sp<IOProfile> profile; |
| 1497 | // samplingRate and flags may be updated by getInputProfile |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1498 | uint32_t profileSamplingRate = (samplingRate == 0) ? SAMPLE_RATE_HZ_DEFAULT : samplingRate; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1499 | audio_format_t profileFormat = format; |
| 1500 | audio_channel_mask_t profileChannelMask = channelMask; |
| 1501 | audio_input_flags_t profileFlags = flags; |
| 1502 | for (;;) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1503 | profile = getInputProfile(device, address, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1504 | profileSamplingRate, profileFormat, profileChannelMask, |
| 1505 | profileFlags); |
| 1506 | if (profile != 0) { |
| 1507 | break; // success |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1508 | } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) { |
| 1509 | profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1510 | } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) { |
| 1511 | profileFlags = AUDIO_INPUT_FLAG_NONE; // retry |
| 1512 | } else { // fail |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1513 | ALOGW("getInputForDevice() could not find profile for device 0x%X," |
| 1514 | "samplingRate %u, format %#x, channelMask 0x%X, flags %#x", |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1515 | device, samplingRate, format, channelMask, flags); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1516 | return input; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1517 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1518 | } |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1519 | // Pick input sampling rate if not specified by client |
| 1520 | if (samplingRate == 0) { |
| 1521 | samplingRate = profileSamplingRate; |
| 1522 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1523 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1524 | if (profile->getModuleHandle() == 0) { |
| 1525 | ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName()); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1526 | return input; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1527 | } |
| 1528 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1529 | sp<AudioSession> audioSession = new AudioSession(session, |
| 1530 | inputSource, |
| 1531 | format, |
| 1532 | samplingRate, |
| 1533 | channelMask, |
| 1534 | flags, |
| 1535 | uid, |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 1536 | isSoundTrigger, |
| 1537 | policyMix, mpClientInterface); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1538 | |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1539 | // TODO enable input reuse |
| 1540 | #if 0 |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1541 | // reuse an open input if possible |
| 1542 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 1543 | sp<AudioInputDescriptor> desc = mInputs.valueAt(i); |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1544 | // reuse input if it shares the same profile and same sound trigger attribute |
| 1545 | if (profile == desc->mProfile && |
| 1546 | isSoundTrigger == desc->isSoundTrigger()) { |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1547 | |
| 1548 | sp<AudioSession> as = desc->getAudioSession(session); |
| 1549 | if (as != 0) { |
| 1550 | // do not allow unmatching properties on same session |
| 1551 | if (as->matches(audioSession)) { |
| 1552 | as->changeOpenCount(1); |
| 1553 | } else { |
| 1554 | ALOGW("getInputForDevice() record with different attributes" |
| 1555 | " exists for session %d", session); |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1556 | return input; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1557 | } |
| 1558 | } else { |
| 1559 | desc->addAudioSession(session, audioSession); |
| 1560 | } |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1561 | ALOGV("getInputForDevice() reusing input %d", mInputs.keyAt(i)); |
| 1562 | return mInputs.keyAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1563 | } |
| 1564 | } |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1565 | #endif |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1566 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1567 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1568 | config.sample_rate = profileSamplingRate; |
| 1569 | config.channel_mask = profileChannelMask; |
| 1570 | config.format = profileFormat; |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1571 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1572 | status_t status = mpClientInterface->openInput(profile->getModuleHandle(), |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1573 | &input, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1574 | &config, |
| 1575 | &device, |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 1576 | address, |
Eric Laurent | 1c9c2cc | 2014-08-28 19:37:25 -0700 | [diff] [blame] | 1577 | halInputSource, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1578 | profileFlags); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1579 | |
| 1580 | // only accept input with the exact requested set of parameters |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1581 | if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE || |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1582 | (profileSamplingRate != config.sample_rate) || |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1583 | !audio_formats_match(profileFormat, config.format) || |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1584 | (profileChannelMask != config.channel_mask)) { |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1585 | ALOGW("getInputForAttr() failed opening input: samplingRate %d" |
| 1586 | ", format %d, channelMask %x", |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1587 | samplingRate, format, channelMask); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1588 | if (input != AUDIO_IO_HANDLE_NONE) { |
| 1589 | mpClientInterface->closeInput(input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1590 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1591 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1592 | } |
| 1593 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1594 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile); |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1595 | inputDesc->mSamplingRate = profileSamplingRate; |
| 1596 | inputDesc->mFormat = profileFormat; |
| 1597 | inputDesc->mChannelMask = profileChannelMask; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1598 | inputDesc->mDevice = device; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1599 | inputDesc->mPolicyMix = policyMix; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1600 | inputDesc->addAudioSession(session, audioSession); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1601 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1602 | addInput(input, inputDesc); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1603 | mpClientInterface->onAudioPortListUpdate(); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1604 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1605 | return input; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1606 | } |
| 1607 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1608 | status_t AudioPolicyManager::startInput(audio_io_handle_t input, |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1609 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1610 | { |
| 1611 | ALOGV("startInput() input %d", input); |
| 1612 | ssize_t index = mInputs.indexOfKey(input); |
| 1613 | if (index < 0) { |
| 1614 | ALOGW("startInput() unknown input %d", input); |
| 1615 | return BAD_VALUE; |
| 1616 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1617 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1618 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1619 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
| 1620 | if (audioSession == 0) { |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1621 | ALOGW("startInput() unknown session %d on input %d", session, input); |
| 1622 | return BAD_VALUE; |
| 1623 | } |
| 1624 | |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1625 | // virtual input devices are compatible with other input devices |
| 1626 | if (!is_virtual_input_device(inputDesc->mDevice)) { |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1627 | |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1628 | // for a non-virtual input device, check if there is another (non-virtual) active input |
| 1629 | audio_io_handle_t activeInput = mInputs.getActiveInput(); |
| 1630 | if (activeInput != 0 && activeInput != input) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1631 | |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1632 | // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed, |
| 1633 | // otherwise the active input continues and the new input cannot be started. |
| 1634 | sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput); |
| 1635 | if ((activeDesc->inputSource() == AUDIO_SOURCE_HOTWORD) && |
| 1636 | !activeDesc->hasPreemptedSession(session)) { |
| 1637 | ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput); |
| 1638 | //FIXME: consider all active sessions |
| 1639 | AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions(); |
| 1640 | audio_session_t activeSession = activeSessions.keyAt(0); |
| 1641 | SortedVector<audio_session_t> sessions = |
| 1642 | activeDesc->getPreemptedSessions(); |
| 1643 | sessions.add(activeSession); |
| 1644 | inputDesc->setPreemptedSessions(sessions); |
| 1645 | stopInput(activeInput, activeSession); |
| 1646 | releaseInput(activeInput, activeSession); |
| 1647 | } else { |
| 1648 | ALOGE("startInput(%d) failed: other input %d already started", input, activeInput); |
| 1649 | return INVALID_OPERATION; |
| 1650 | } |
| 1651 | } |
| 1652 | |
| 1653 | // Do not allow capture if an active voice call is using a software patch and |
| 1654 | // the call TX source device is on the same HW module. |
| 1655 | // FIXME: would be better to refine to only inputs whose profile connects to the |
| 1656 | // call TX device but this information is not in the audio patch |
| 1657 | if (mCallTxPatch != 0 && |
| 1658 | inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) { |
| 1659 | return INVALID_OPERATION; |
| 1660 | } |
| 1661 | } |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 1662 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1663 | // Routing? |
| 1664 | mInputRoutes.incRouteActivity(session); |
| 1665 | |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1666 | if (!inputDesc->isActive() || mInputRoutes.hasRouteChanged(session)) { |
| 1667 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 1668 | if ((inputDesc->mPolicyMix != NULL) |
| 1669 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1670 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1671 | MIX_STATE_MIXING); |
| 1672 | } |
Jean-Michel Trivi | 2b0c1fc | 2015-04-15 17:29:28 -0700 | [diff] [blame] | 1673 | |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1674 | if (mInputs.activeInputsCount() == 0) { |
| 1675 | SoundTrigger::setCaptureState(true); |
| 1676 | } |
| 1677 | setInputDevice(input, getNewInputDevice(input), true /* force */); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1678 | |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1679 | // automatically enable the remote submix output when input is started if not |
| 1680 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 1681 | // For remote submix (a virtual device), we open only one input per capture request. |
| 1682 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 1683 | String8 address = String8(""); |
| 1684 | if (inputDesc->mPolicyMix == NULL) { |
| 1685 | address = String8("0"); |
| 1686 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1687 | address = inputDesc->mPolicyMix->mDeviceAddress; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1688 | } |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1689 | if (address != "") { |
| 1690 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 1691 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 1692 | address, "remote-submix"); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1693 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1694 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1695 | } |
| 1696 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1697 | ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1698 | |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1699 | audioSession->changeActiveCount(1); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1700 | return NO_ERROR; |
| 1701 | } |
| 1702 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1703 | status_t AudioPolicyManager::stopInput(audio_io_handle_t input, |
| 1704 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1705 | { |
| 1706 | ALOGV("stopInput() input %d", input); |
| 1707 | ssize_t index = mInputs.indexOfKey(input); |
| 1708 | if (index < 0) { |
| 1709 | ALOGW("stopInput() unknown input %d", input); |
| 1710 | return BAD_VALUE; |
| 1711 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1712 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1713 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1714 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1715 | if (index < 0) { |
| 1716 | ALOGW("stopInput() unknown session %d on input %d", session, input); |
| 1717 | return BAD_VALUE; |
| 1718 | } |
| 1719 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1720 | if (audioSession->activeCount() == 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1721 | ALOGW("stopInput() input %d already stopped", input); |
| 1722 | return INVALID_OPERATION; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1723 | } |
| 1724 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1725 | audioSession->changeActiveCount(-1); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1726 | |
| 1727 | // Routing? |
| 1728 | mInputRoutes.decRouteActivity(session); |
| 1729 | |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1730 | if (!inputDesc->isActive()) { |
| 1731 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 1732 | if ((inputDesc->mPolicyMix != NULL) |
| 1733 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1734 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1735 | MIX_STATE_IDLE); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 1736 | } |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1737 | |
| 1738 | // automatically disable the remote submix output when input is stopped if not |
| 1739 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 1740 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 1741 | String8 address = String8(""); |
| 1742 | if (inputDesc->mPolicyMix == NULL) { |
| 1743 | address = String8("0"); |
| 1744 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1745 | address = inputDesc->mPolicyMix->mDeviceAddress; |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1746 | } |
| 1747 | if (address != "") { |
| 1748 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 1749 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 1750 | address, "remote-submix"); |
| 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | resetInputDevice(input); |
| 1755 | |
| 1756 | if (mInputs.activeInputsCount() == 0) { |
| 1757 | SoundTrigger::setCaptureState(false); |
| 1758 | } |
| 1759 | inputDesc->clearPreemptedSessions(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1760 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1761 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1762 | } |
| 1763 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1764 | void AudioPolicyManager::releaseInput(audio_io_handle_t input, |
| 1765 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1766 | { |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 1767 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1768 | ALOGV("releaseInput() %d", input); |
| 1769 | ssize_t index = mInputs.indexOfKey(input); |
| 1770 | if (index < 0) { |
| 1771 | ALOGW("releaseInput() releasing unknown input %d", input); |
| 1772 | return; |
| 1773 | } |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1774 | |
| 1775 | // Routing |
| 1776 | mInputRoutes.removeRoute(session); |
| 1777 | |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1778 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
| 1779 | ALOG_ASSERT(inputDesc != 0); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1780 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1781 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1782 | if (index < 0) { |
| 1783 | ALOGW("releaseInput() unknown session %d on input %d", session, input); |
| 1784 | return; |
| 1785 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1786 | |
| 1787 | if (audioSession->openCount() == 0) { |
| 1788 | ALOGW("releaseInput() invalid open count %d on session %d", |
| 1789 | audioSession->openCount(), session); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1790 | return; |
| 1791 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1792 | |
| 1793 | if (audioSession->changeOpenCount(-1) == 0) { |
| 1794 | inputDesc->removeAudioSession(session); |
| 1795 | } |
| 1796 | |
Jean-Michel Trivi | 65bfe91 | 2015-12-04 15:56:47 -0800 | [diff] [blame] | 1797 | if (inputDesc->getOpenRefCount() > 0) { |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1798 | ALOGV("releaseInput() exit > 0"); |
| 1799 | return; |
| 1800 | } |
| 1801 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1802 | closeInput(input); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1803 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1804 | ALOGV("releaseInput() exit"); |
| 1805 | } |
| 1806 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1807 | void AudioPolicyManager::closeAllInputs() { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1808 | bool patchRemoved = false; |
| 1809 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1810 | for(size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1811 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index); |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 1812 | ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1813 | if (patch_index >= 0) { |
| 1814 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 1815 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1816 | mAudioPatches.removeItemsAt(patch_index); |
| 1817 | patchRemoved = true; |
| 1818 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1819 | mpClientInterface->closeInput(mInputs.keyAt(input_index)); |
| 1820 | } |
| 1821 | mInputs.clear(); |
Chris Thornton | 52785f3 | 2016-02-09 17:28:49 -0800 | [diff] [blame] | 1822 | SoundTrigger::setCaptureState(false); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1823 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1824 | |
| 1825 | if (patchRemoved) { |
| 1826 | mpClientInterface->onAudioPatchListUpdate(); |
| 1827 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1828 | } |
| 1829 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1830 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1831 | int indexMin, |
| 1832 | int indexMax) |
| 1833 | { |
| 1834 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 1835 | mVolumeCurves->initStreamVolume(stream, indexMin, indexMax); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1836 | |
| 1837 | // initialize other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 1838 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 1839 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1840 | continue; |
| 1841 | } |
| 1842 | mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1843 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1844 | } |
| 1845 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1846 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1847 | int index, |
| 1848 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1849 | { |
| 1850 | |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 1851 | if ((index < mVolumeCurves->getVolumeIndexMin(stream)) || |
| 1852 | (index > mVolumeCurves->getVolumeIndexMax(stream))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1853 | return BAD_VALUE; |
| 1854 | } |
| 1855 | if (!audio_is_output_device(device)) { |
| 1856 | return BAD_VALUE; |
| 1857 | } |
| 1858 | |
| 1859 | // Force max volume if stream cannot be muted |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 1860 | if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1861 | |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 1862 | ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1863 | stream, device, index); |
| 1864 | |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1865 | // update other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 1866 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 1867 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1868 | continue; |
| 1869 | } |
| 1870 | mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index); |
| 1871 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1872 | |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 1873 | // update volume on all outputs and streams matching the following: |
| 1874 | // - The requested stream (or a stream matching for volume control) is active on the output |
| 1875 | // - The device (or devices) selected by the strategy corresponding to this stream includes |
| 1876 | // the requested device |
| 1877 | // - For non default requested device, currently selected device on the output is either the |
| 1878 | // requested device or one of the devices selected by the strategy |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 1879 | // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if |
| 1880 | // no specific device volume value exists for currently selected device. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1881 | status_t status = NO_ERROR; |
| 1882 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1883 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 1884 | audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device()); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 1885 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 1886 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1887 | continue; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1888 | } |
Eric Laurent | d3926fe | 2016-04-15 18:10:07 -0700 | [diff] [blame] | 1889 | if (!(desc->isStreamActive((audio_stream_type_t)curStream) || |
| 1890 | (isInCall() && (curStream == AUDIO_STREAM_VOICE_CALL)))) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 1891 | continue; |
| 1892 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 1893 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1894 | audio_devices_t curStreamDevice = getDeviceForStrategy(curStrategy, true /*fromCache*/); |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 1895 | if ((curStreamDevice & device) == 0) { |
| 1896 | continue; |
| 1897 | } |
| 1898 | bool applyDefault = false; |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 1899 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 1900 | curStreamDevice |= device; |
| 1901 | } else if (!mVolumeCurves->hasVolumeIndexForDevice( |
| 1902 | stream, Volume::getDeviceForVolume(curStreamDevice))) { |
| 1903 | applyDefault = true; |
| 1904 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1905 | |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 1906 | if (applyDefault || ((curDevice & curStreamDevice) != 0)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame^] | 1907 | //FIXME: workaround for truncated touch sounds |
| 1908 | // delayed volume change for system stream to be removed when the problem is |
| 1909 | // handled by system UI |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1910 | status_t volStatus = |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame^] | 1911 | checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice, |
| 1912 | (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1913 | if (volStatus != NO_ERROR) { |
| 1914 | status = volStatus; |
| 1915 | } |
| 1916 | } |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1917 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1918 | } |
| 1919 | return status; |
| 1920 | } |
| 1921 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1922 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1923 | int *index, |
| 1924 | audio_devices_t device) |
| 1925 | { |
| 1926 | if (index == NULL) { |
| 1927 | return BAD_VALUE; |
| 1928 | } |
| 1929 | if (!audio_is_output_device(device)) { |
| 1930 | return BAD_VALUE; |
| 1931 | } |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 1932 | // 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] | 1933 | // the strategy the stream belongs to. |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 1934 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1935 | device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); |
| 1936 | } |
François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 1937 | device = Volume::getDeviceForVolume(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1938 | |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 1939 | *index = mVolumeCurves->getVolumeIndex(stream, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1940 | ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index); |
| 1941 | return NO_ERROR; |
| 1942 | } |
| 1943 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1944 | audio_io_handle_t AudioPolicyManager::selectOutputForEffects( |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1945 | const SortedVector<audio_io_handle_t>& outputs) |
| 1946 | { |
| 1947 | // select one output among several suitable for global effects. |
| 1948 | // The priority is as follows: |
| 1949 | // 1: An offloaded output. If the effect ends up not being offloadable, |
| 1950 | // AudioFlinger will invalidate the track and the offloaded output |
| 1951 | // will be closed causing the effect to be moved to a PCM output. |
| 1952 | // 2: A deep buffer output |
| 1953 | // 3: the first output in the list |
| 1954 | |
| 1955 | if (outputs.size() == 0) { |
| 1956 | return 0; |
| 1957 | } |
| 1958 | |
| 1959 | audio_io_handle_t outputOffloaded = 0; |
| 1960 | audio_io_handle_t outputDeepBuffer = 0; |
| 1961 | |
| 1962 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1963 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1964 | ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1965 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 1966 | outputOffloaded = outputs[i]; |
| 1967 | } |
| 1968 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { |
| 1969 | outputDeepBuffer = outputs[i]; |
| 1970 | } |
| 1971 | } |
| 1972 | |
| 1973 | ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d", |
| 1974 | outputOffloaded, outputDeepBuffer); |
| 1975 | if (outputOffloaded != 0) { |
| 1976 | return outputOffloaded; |
| 1977 | } |
| 1978 | if (outputDeepBuffer != 0) { |
| 1979 | return outputDeepBuffer; |
| 1980 | } |
| 1981 | |
| 1982 | return outputs[0]; |
| 1983 | } |
| 1984 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1985 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1986 | { |
| 1987 | // apply simple rule where global effects are attached to the same output as MUSIC streams |
| 1988 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1989 | routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1990 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 1991 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs); |
| 1992 | |
| 1993 | audio_io_handle_t output = selectOutputForEffects(dstOutputs); |
| 1994 | ALOGV("getOutputForEffect() got output %d for fx %s flags %x", |
| 1995 | output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags); |
| 1996 | |
| 1997 | return output; |
| 1998 | } |
| 1999 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2000 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2001 | audio_io_handle_t io, |
| 2002 | uint32_t strategy, |
| 2003 | int session, |
| 2004 | int id) |
| 2005 | { |
| 2006 | ssize_t index = mOutputs.indexOfKey(io); |
| 2007 | if (index < 0) { |
| 2008 | index = mInputs.indexOfKey(io); |
| 2009 | if (index < 0) { |
| 2010 | ALOGW("registerEffect() unknown io %d", io); |
| 2011 | return INVALID_OPERATION; |
| 2012 | } |
| 2013 | } |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2014 | return mEffects.registerEffect(desc, io, strategy, session, id); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2015 | } |
| 2016 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2017 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2018 | { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2019 | bool active = false; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2020 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) { |
| 2021 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2022 | continue; |
| 2023 | } |
| 2024 | active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs); |
| 2025 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2026 | return active; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2027 | } |
| 2028 | |
| 2029 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2030 | { |
| 2031 | return mOutputs.isStreamActiveRemotely(stream, inPastMs); |
| 2032 | } |
| 2033 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2034 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2035 | { |
| 2036 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2037 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2038 | if (inputDescriptor->isSourceActive(source)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2039 | return true; |
| 2040 | } |
| 2041 | } |
| 2042 | return false; |
| 2043 | } |
| 2044 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2045 | // Register a list of custom mixes with their attributes and format. |
| 2046 | // When a mix is registered, corresponding input and output profiles are |
| 2047 | // added to the remote submix hw module. The profile contains only the |
| 2048 | // parameters (sampling rate, format...) specified by the mix. |
| 2049 | // The corresponding input remote submix device is also connected. |
| 2050 | // |
| 2051 | // When a remote submix device is connected, the address is checked to select the |
| 2052 | // appropriate profile and the corresponding input or output stream is opened. |
| 2053 | // |
| 2054 | // When capture starts, getInputForAttr() will: |
| 2055 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2056 | // - 2 if none found, getDeviceForInputSource() will: |
| 2057 | // - 2.1 look for a mix matching the attributes source |
| 2058 | // - 2.2 if none found, default to device selection by policy rules |
| 2059 | // At this time, the corresponding output remote submix device is also connected |
| 2060 | // and active playback use cases can be transferred to this mix if needed when reconnecting |
| 2061 | // after AudioTracks are invalidated |
| 2062 | // |
| 2063 | // When playback starts, getOutputForAttr() will: |
| 2064 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2065 | // - 2 if none found, look for a mix matching the attributes usage |
| 2066 | // - 3 if none found, default to device and output selection by policy rules. |
| 2067 | |
| 2068 | status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes) |
| 2069 | { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2070 | ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size()); |
| 2071 | status_t res = NO_ERROR; |
| 2072 | |
| 2073 | sp<HwModule> rSubmixModule; |
| 2074 | // examine each mix's route type |
| 2075 | for (size_t i = 0; i < mixes.size(); i++) { |
| 2076 | // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination |
| 2077 | if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) { |
| 2078 | res = INVALID_OPERATION; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2079 | break; |
| 2080 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2081 | if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) { |
| 2082 | // Loop back through "remote submix" |
| 2083 | if (rSubmixModule == 0) { |
| 2084 | for (size_t j = 0; i < mHwModules.size(); j++) { |
| 2085 | if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0 |
| 2086 | && mHwModules[j]->mHandle != 0) { |
| 2087 | rSubmixModule = mHwModules[j]; |
| 2088 | break; |
| 2089 | } |
| 2090 | } |
| 2091 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2092 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2093 | ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK", i, mixes.size()); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2094 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2095 | if (rSubmixModule == 0) { |
| 2096 | ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", i); |
| 2097 | res = INVALID_OPERATION; |
| 2098 | break; |
| 2099 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2100 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2101 | String8 address = mixes[i].mDeviceAddress; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2102 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2103 | if (mPolicyMixes.registerMix(address, mixes[i], 0 /*output desc*/) != NO_ERROR) { |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2104 | ALOGE(" Error registering mix %zu for address %s", i, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2105 | res = INVALID_OPERATION; |
| 2106 | break; |
| 2107 | } |
| 2108 | audio_config_t outputConfig = mixes[i].mFormat; |
| 2109 | audio_config_t inputConfig = mixes[i].mFormat; |
| 2110 | // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in |
| 2111 | // stereo and let audio flinger do the channel conversion if needed. |
| 2112 | outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 2113 | inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
| 2114 | rSubmixModule->addOutputProfile(address, &outputConfig, |
| 2115 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address); |
| 2116 | rSubmixModule->addInputProfile(address, &inputConfig, |
| 2117 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, address); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2118 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2119 | if (mixes[i].mMixType == MIX_TYPE_PLAYERS) { |
| 2120 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2121 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2122 | address.string(), "remote-submix"); |
| 2123 | } else { |
| 2124 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2125 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2126 | address.string(), "remote-submix"); |
| 2127 | } |
| 2128 | } 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] | 2129 | String8 address = mixes[i].mDeviceAddress; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2130 | audio_devices_t device = mixes[i].mDeviceType; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2131 | ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s", |
| 2132 | i, mixes.size(), device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2133 | |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2134 | bool foundOutput = false; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2135 | for (size_t j = 0 ; j < mOutputs.size() ; j++) { |
| 2136 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j); |
| 2137 | sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle()); |
| 2138 | if ((patch != 0) && (patch->mPatch.num_sinks != 0) |
| 2139 | && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE) |
| 2140 | && (patch->mPatch.sinks[0].ext.device.type == device) |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2141 | && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(), |
| 2142 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2143 | if (mPolicyMixes.registerMix(address, mixes[i], desc) != NO_ERROR) { |
| 2144 | res = INVALID_OPERATION; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2145 | } else { |
| 2146 | foundOutput = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2147 | } |
| 2148 | break; |
| 2149 | } |
| 2150 | } |
| 2151 | |
| 2152 | if (res != NO_ERROR) { |
| 2153 | ALOGE(" Error registering mix %zu for device 0x%X addr %s", |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2154 | i, device, address.string()); |
| 2155 | res = INVALID_OPERATION; |
| 2156 | break; |
| 2157 | } else if (!foundOutput) { |
| 2158 | ALOGE(" Output not found for mix %zu for device 0x%X addr %s", |
| 2159 | i, device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2160 | res = INVALID_OPERATION; |
| 2161 | break; |
| 2162 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2163 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2164 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2165 | if (res != NO_ERROR) { |
| 2166 | unregisterPolicyMixes(mixes); |
| 2167 | } |
| 2168 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2169 | } |
| 2170 | |
| 2171 | status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes) |
| 2172 | { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2173 | ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2174 | status_t res = NO_ERROR; |
| 2175 | sp<HwModule> rSubmixModule; |
| 2176 | // examine each mix's route type |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2177 | for (size_t i = 0; i < mixes.size(); i++) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2178 | 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] | 2179 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2180 | if (rSubmixModule == 0) { |
| 2181 | for (size_t j = 0; i < mHwModules.size(); j++) { |
| 2182 | if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0 |
| 2183 | && mHwModules[j]->mHandle != 0) { |
| 2184 | rSubmixModule = mHwModules[j]; |
| 2185 | break; |
| 2186 | } |
| 2187 | } |
| 2188 | } |
| 2189 | if (rSubmixModule == 0) { |
| 2190 | res = INVALID_OPERATION; |
| 2191 | continue; |
| 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 | String8 address = mixes[i].mDeviceAddress; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2195 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2196 | if (mPolicyMixes.unregisterMix(address) != NO_ERROR) { |
| 2197 | res = INVALID_OPERATION; |
| 2198 | continue; |
| 2199 | } |
| 2200 | |
| 2201 | if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) == |
| 2202 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2203 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2204 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2205 | address.string(), "remote-submix"); |
| 2206 | } |
| 2207 | if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) == |
| 2208 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2209 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2210 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2211 | address.string(), "remote-submix"); |
| 2212 | } |
| 2213 | rSubmixModule->removeOutputProfile(address); |
| 2214 | rSubmixModule->removeInputProfile(address); |
| 2215 | |
| 2216 | } if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 2217 | if (mPolicyMixes.unregisterMix(mixes[i].mDeviceAddress) != NO_ERROR) { |
| 2218 | res = INVALID_OPERATION; |
| 2219 | continue; |
| 2220 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 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 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2224 | } |
| 2225 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2226 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2227 | status_t AudioPolicyManager::dump(int fd) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2228 | { |
| 2229 | const size_t SIZE = 256; |
| 2230 | char buffer[SIZE]; |
| 2231 | String8 result; |
| 2232 | |
| 2233 | snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this); |
| 2234 | result.append(buffer); |
| 2235 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 2236 | snprintf(buffer, SIZE, " Primary Output: %d\n", |
| 2237 | hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2238 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2239 | snprintf(buffer, SIZE, " Phone state: %d\n", mEngine->getPhoneState()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2240 | result.append(buffer); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2241 | snprintf(buffer, SIZE, " Force use for communications %d\n", |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2242 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2243 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2244 | 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] | 2245 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2246 | 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] | 2247 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2248 | 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] | 2249 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2250 | 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] | 2251 | result.append(buffer); |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 2252 | snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n", |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2253 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO)); |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 2254 | result.append(buffer); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 2255 | snprintf(buffer, SIZE, " Force use for encoded surround output %d\n", |
| 2256 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND)); |
| 2257 | result.append(buffer); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 2258 | snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available"); |
| 2259 | result.append(buffer); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 2260 | snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off"); |
| 2261 | result.append(buffer); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 2262 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2263 | write(fd, result.string(), result.size()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2264 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 2265 | mAvailableOutputDevices.dump(fd, String8("Available output")); |
| 2266 | mAvailableInputDevices.dump(fd, String8("Available input")); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2267 | mHwModules.dump(fd); |
| 2268 | mOutputs.dump(fd); |
| 2269 | mInputs.dump(fd); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2270 | mVolumeCurves->dump(fd); |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2271 | mEffects.dump(fd); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2272 | mAudioPatches.dump(fd); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2273 | |
| 2274 | return NO_ERROR; |
| 2275 | } |
| 2276 | |
| 2277 | // This function checks for the parameters which can be offloaded. |
| 2278 | // This can be enhanced depending on the capability of the DSP and policy |
| 2279 | // of the system. |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2280 | bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2281 | { |
| 2282 | ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2283 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2284 | offloadInfo.sample_rate, offloadInfo.channel_mask, |
| 2285 | offloadInfo.format, |
| 2286 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, |
| 2287 | offloadInfo.has_video); |
| 2288 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 2289 | if (mMasterMono) { |
| 2290 | return false; // no offloading if mono is set. |
| 2291 | } |
| 2292 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2293 | // Check if offload has been disabled |
| 2294 | char propValue[PROPERTY_VALUE_MAX]; |
| 2295 | if (property_get("audio.offload.disable", propValue, "0")) { |
| 2296 | if (atoi(propValue) != 0) { |
| 2297 | ALOGV("offload disabled by audio.offload.disable=%s", propValue ); |
| 2298 | return false; |
| 2299 | } |
| 2300 | } |
| 2301 | |
| 2302 | // Check if stream type is music, then only allow offload as of now. |
| 2303 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) |
| 2304 | { |
| 2305 | ALOGV("isOffloadSupported: stream_type != MUSIC, returning false"); |
| 2306 | return false; |
| 2307 | } |
| 2308 | |
| 2309 | //TODO: enable audio offloading with video when ready |
Andy Hung | 08945c4 | 2015-05-31 21:36:46 -0700 | [diff] [blame] | 2310 | const bool allowOffloadWithVideo = |
| 2311 | property_get_bool("audio.offload.video", false /* default_value */); |
| 2312 | if (offloadInfo.has_video && !allowOffloadWithVideo) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2313 | ALOGV("isOffloadSupported: has_video == true, returning false"); |
| 2314 | return false; |
| 2315 | } |
| 2316 | |
| 2317 | //If duration is less than minimum value defined in property, return false |
| 2318 | if (property_get("audio.offload.min.duration.secs", propValue, NULL)) { |
| 2319 | if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) { |
| 2320 | ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue); |
| 2321 | return false; |
| 2322 | } |
| 2323 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { |
| 2324 | ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS); |
| 2325 | return false; |
| 2326 | } |
| 2327 | |
| 2328 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 2329 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 2330 | // detects there is an active non offloadable effect. |
| 2331 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 2332 | // This may prevent offloading in rare situations where effects are left active by apps |
| 2333 | // in the background. |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2334 | if (mEffects.isNonOffloadableEffectEnabled()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2335 | return false; |
| 2336 | } |
| 2337 | |
| 2338 | // See if there is a profile to support this. |
| 2339 | // AUDIO_DEVICE_NONE |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2340 | sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2341 | offloadInfo.sample_rate, |
| 2342 | offloadInfo.format, |
| 2343 | offloadInfo.channel_mask, |
| 2344 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2345 | ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT "); |
| 2346 | return (profile != 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2347 | } |
| 2348 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2349 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, |
| 2350 | audio_port_type_t type, |
| 2351 | unsigned int *num_ports, |
| 2352 | struct audio_port *ports, |
| 2353 | unsigned int *generation) |
| 2354 | { |
| 2355 | if (num_ports == NULL || (*num_ports != 0 && ports == NULL) || |
| 2356 | generation == NULL) { |
| 2357 | return BAD_VALUE; |
| 2358 | } |
| 2359 | ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports); |
| 2360 | if (ports == NULL) { |
| 2361 | *num_ports = 0; |
| 2362 | } |
| 2363 | |
| 2364 | size_t portsWritten = 0; |
| 2365 | size_t portsMax = *num_ports; |
| 2366 | *num_ports = 0; |
| 2367 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2368 | // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB |
| 2369 | // as they are used by stub HALs by convention |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2370 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2371 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { |
| 2372 | if (mAvailableOutputDevices[i]->type() == AUDIO_DEVICE_OUT_STUB) { |
| 2373 | continue; |
| 2374 | } |
| 2375 | if (portsWritten < portsMax) { |
| 2376 | mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]); |
| 2377 | } |
| 2378 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2379 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2380 | } |
| 2381 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2382 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { |
| 2383 | if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_STUB) { |
| 2384 | continue; |
| 2385 | } |
| 2386 | if (portsWritten < portsMax) { |
| 2387 | mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]); |
| 2388 | } |
| 2389 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2390 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2391 | } |
| 2392 | } |
| 2393 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { |
| 2394 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 2395 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { |
| 2396 | mInputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2397 | } |
| 2398 | *num_ports += mInputs.size(); |
| 2399 | } |
| 2400 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2401 | size_t numOutputs = 0; |
| 2402 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2403 | if (!mOutputs[i]->isDuplicated()) { |
| 2404 | numOutputs++; |
| 2405 | if (portsWritten < portsMax) { |
| 2406 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2407 | } |
| 2408 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2409 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2410 | *num_ports += numOutputs; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2411 | } |
| 2412 | } |
| 2413 | *generation = curAudioPortGeneration(); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2414 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2415 | return NO_ERROR; |
| 2416 | } |
| 2417 | |
| 2418 | status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused) |
| 2419 | { |
| 2420 | return NO_ERROR; |
| 2421 | } |
| 2422 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2423 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, |
| 2424 | audio_patch_handle_t *handle, |
| 2425 | uid_t uid) |
| 2426 | { |
| 2427 | ALOGV("createAudioPatch()"); |
| 2428 | |
| 2429 | if (handle == NULL || patch == NULL) { |
| 2430 | return BAD_VALUE; |
| 2431 | } |
| 2432 | ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks); |
| 2433 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2434 | if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX || |
| 2435 | patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) { |
| 2436 | return BAD_VALUE; |
| 2437 | } |
| 2438 | // only one source per audio patch supported for now |
| 2439 | if (patch->num_sources > 1) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2440 | return INVALID_OPERATION; |
| 2441 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2442 | |
| 2443 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2444 | return INVALID_OPERATION; |
| 2445 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2446 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2447 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { |
| 2448 | return INVALID_OPERATION; |
| 2449 | } |
| 2450 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2451 | |
| 2452 | sp<AudioPatch> patchDesc; |
| 2453 | ssize_t index = mAudioPatches.indexOfKey(*handle); |
| 2454 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2455 | ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id, |
| 2456 | patch->sources[0].role, |
| 2457 | patch->sources[0].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2458 | #if LOG_NDEBUG == 0 |
| 2459 | for (size_t i = 0; i < patch->num_sinks; i++) { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2460 | 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] | 2461 | patch->sinks[i].role, |
| 2462 | patch->sinks[i].type); |
| 2463 | } |
| 2464 | #endif |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2465 | |
| 2466 | if (index >= 0) { |
| 2467 | patchDesc = mAudioPatches.valueAt(index); |
| 2468 | ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 2469 | mUidCached, patchDesc->mUid, uid); |
| 2470 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 2471 | return INVALID_OPERATION; |
| 2472 | } |
| 2473 | } else { |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 2474 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2475 | } |
| 2476 | |
| 2477 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2478 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2479 | if (outputDesc == NULL) { |
| 2480 | ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id); |
| 2481 | return BAD_VALUE; |
| 2482 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2483 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", |
| 2484 | outputDesc->mIoHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2485 | if (patchDesc != 0) { |
| 2486 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
| 2487 | ALOGV("createAudioPatch() source id differs for patch current id %d new id %d", |
| 2488 | patchDesc->mPatch.sources[0].id, patch->sources[0].id); |
| 2489 | return BAD_VALUE; |
| 2490 | } |
| 2491 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2492 | DeviceVector devices; |
| 2493 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2494 | // Only support mix to devices connection |
| 2495 | // TODO add support for mix to mix connection |
| 2496 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 2497 | ALOGV("createAudioPatch() source mix but sink is not a device"); |
| 2498 | return INVALID_OPERATION; |
| 2499 | } |
| 2500 | sp<DeviceDescriptor> devDesc = |
| 2501 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 2502 | if (devDesc == 0) { |
| 2503 | ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id); |
| 2504 | return BAD_VALUE; |
| 2505 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2506 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2507 | if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2508 | devDesc->mAddress, |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2509 | patch->sources[0].sample_rate, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2510 | NULL, // updatedSamplingRate |
| 2511 | patch->sources[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2512 | NULL, // updatedFormat |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2513 | patch->sources[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2514 | NULL, // updatedChannelMask |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2515 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2516 | ALOGV("createAudioPatch() profile not supported for device %08x", |
| 2517 | devDesc->type()); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2518 | return INVALID_OPERATION; |
| 2519 | } |
| 2520 | devices.add(devDesc); |
| 2521 | } |
| 2522 | if (devices.size() == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2523 | return INVALID_OPERATION; |
| 2524 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2525 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2526 | // TODO: reconfigure output format and channels here |
| 2527 | ALOGV("createAudioPatch() setting device %08x on output %d", |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2528 | devices.types(), outputDesc->mIoHandle); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2529 | setOutputDevice(outputDesc, devices.types(), true, 0, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2530 | index = mAudioPatches.indexOfKey(*handle); |
| 2531 | if (index >= 0) { |
| 2532 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 2533 | ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided"); |
| 2534 | } |
| 2535 | patchDesc = mAudioPatches.valueAt(index); |
| 2536 | patchDesc->mUid = uid; |
| 2537 | ALOGV("createAudioPatch() success"); |
| 2538 | } else { |
| 2539 | ALOGW("createAudioPatch() setOutputDevice() failed to create a patch"); |
| 2540 | return INVALID_OPERATION; |
| 2541 | } |
| 2542 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2543 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 2544 | // input device to input mix connection |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2545 | // only one sink supported when connecting an input device to a mix |
| 2546 | if (patch->num_sinks > 1) { |
| 2547 | return INVALID_OPERATION; |
| 2548 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2549 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2550 | if (inputDesc == NULL) { |
| 2551 | return BAD_VALUE; |
| 2552 | } |
| 2553 | if (patchDesc != 0) { |
| 2554 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { |
| 2555 | return BAD_VALUE; |
| 2556 | } |
| 2557 | } |
| 2558 | sp<DeviceDescriptor> devDesc = |
| 2559 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
| 2560 | if (devDesc == 0) { |
| 2561 | return BAD_VALUE; |
| 2562 | } |
| 2563 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2564 | if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2565 | devDesc->mAddress, |
| 2566 | patch->sinks[0].sample_rate, |
| 2567 | NULL, /*updatedSampleRate*/ |
| 2568 | patch->sinks[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2569 | NULL, /*updatedFormat*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2570 | patch->sinks[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2571 | NULL, /*updatedChannelMask*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2572 | // FIXME for the parameter type, |
| 2573 | // and the NONE |
| 2574 | (audio_output_flags_t) |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2575 | AUDIO_INPUT_FLAG_NONE)) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2576 | return INVALID_OPERATION; |
| 2577 | } |
| 2578 | // TODO: reconfigure output format and channels here |
| 2579 | ALOGV("createAudioPatch() setting device %08x on output %d", |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2580 | devDesc->type(), inputDesc->mIoHandle); |
| 2581 | setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2582 | index = mAudioPatches.indexOfKey(*handle); |
| 2583 | if (index >= 0) { |
| 2584 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 2585 | ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided"); |
| 2586 | } |
| 2587 | patchDesc = mAudioPatches.valueAt(index); |
| 2588 | patchDesc->mUid = uid; |
| 2589 | ALOGV("createAudioPatch() success"); |
| 2590 | } else { |
| 2591 | ALOGW("createAudioPatch() setInputDevice() failed to create a patch"); |
| 2592 | return INVALID_OPERATION; |
| 2593 | } |
| 2594 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2595 | // device to device connection |
| 2596 | if (patchDesc != 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2597 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2598 | return BAD_VALUE; |
| 2599 | } |
| 2600 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2601 | sp<DeviceDescriptor> srcDeviceDesc = |
| 2602 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 2603 | if (srcDeviceDesc == 0) { |
| 2604 | return BAD_VALUE; |
| 2605 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2606 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2607 | //update source and sink with our own data as the data passed in the patch may |
| 2608 | // be incomplete. |
| 2609 | struct audio_patch newPatch = *patch; |
| 2610 | srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2611 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2612 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2613 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 2614 | ALOGV("createAudioPatch() source device but one sink is not a device"); |
| 2615 | return INVALID_OPERATION; |
| 2616 | } |
| 2617 | |
| 2618 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 2619 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 2620 | if (sinkDeviceDesc == 0) { |
| 2621 | return BAD_VALUE; |
| 2622 | } |
| 2623 | sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]); |
| 2624 | |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2625 | // create a software bridge in PatchPanel if: |
| 2626 | // - source and sink devices are on differnt HW modules OR |
| 2627 | // - audio HAL version is < 3.0 |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 2628 | if ((srcDeviceDesc->getModuleHandle() != sinkDeviceDesc->getModuleHandle()) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 2629 | (srcDeviceDesc->mModule->getHalVersion() < AUDIO_DEVICE_API_VERSION_3_0)) { |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2630 | // support only one sink device for now to simplify output selection logic |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2631 | if (patch->num_sinks > 1) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2632 | return INVALID_OPERATION; |
| 2633 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2634 | SortedVector<audio_io_handle_t> outputs = |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2635 | getOutputsForDevice(sinkDeviceDesc->type(), mOutputs); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2636 | // if the sink device is reachable via an opened output stream, request to go via |
| 2637 | // this output stream by adding a second source to the patch description |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 2638 | audio_io_handle_t output = selectOutput(outputs, |
| 2639 | AUDIO_OUTPUT_FLAG_NONE, |
| 2640 | AUDIO_FORMAT_INVALID); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2641 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 2642 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 2643 | if (outputDesc->isDuplicated()) { |
| 2644 | return INVALID_OPERATION; |
| 2645 | } |
| 2646 | outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2647 | newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2648 | newPatch.num_sources = 2; |
| 2649 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2650 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2651 | } |
| 2652 | // TODO: check from routing capabilities in config file and other conflicting patches |
| 2653 | |
| 2654 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 2655 | if (index >= 0) { |
| 2656 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 2657 | } |
| 2658 | |
| 2659 | status_t status = mpClientInterface->createAudioPatch(&newPatch, |
| 2660 | &afPatchHandle, |
| 2661 | 0); |
| 2662 | ALOGV("createAudioPatch() patch panel returned %d patchHandle %d", |
| 2663 | status, afPatchHandle); |
| 2664 | if (status == NO_ERROR) { |
| 2665 | if (index < 0) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 2666 | patchDesc = new AudioPatch(&newPatch, uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2667 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 2668 | } else { |
| 2669 | patchDesc->mPatch = newPatch; |
| 2670 | } |
| 2671 | patchDesc->mAfPatchHandle = afPatchHandle; |
| 2672 | *handle = patchDesc->mHandle; |
| 2673 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2674 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2675 | } else { |
| 2676 | ALOGW("createAudioPatch() patch panel could not connect device patch, error %d", |
| 2677 | status); |
| 2678 | return INVALID_OPERATION; |
| 2679 | } |
| 2680 | } else { |
| 2681 | return BAD_VALUE; |
| 2682 | } |
| 2683 | } else { |
| 2684 | return BAD_VALUE; |
| 2685 | } |
| 2686 | return NO_ERROR; |
| 2687 | } |
| 2688 | |
| 2689 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, |
| 2690 | uid_t uid) |
| 2691 | { |
| 2692 | ALOGV("releaseAudioPatch() patch %d", handle); |
| 2693 | |
| 2694 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 2695 | |
| 2696 | if (index < 0) { |
| 2697 | return BAD_VALUE; |
| 2698 | } |
| 2699 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 2700 | ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 2701 | mUidCached, patchDesc->mUid, uid); |
| 2702 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 2703 | return INVALID_OPERATION; |
| 2704 | } |
| 2705 | |
| 2706 | struct audio_patch *patch = &patchDesc->mPatch; |
| 2707 | patchDesc->mUid = mUidCached; |
| 2708 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2709 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2710 | if (outputDesc == NULL) { |
| 2711 | ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id); |
| 2712 | return BAD_VALUE; |
| 2713 | } |
| 2714 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2715 | setOutputDevice(outputDesc, |
| 2716 | getNewOutputDevice(outputDesc, true /*fromCache*/), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2717 | true, |
| 2718 | 0, |
| 2719 | NULL); |
| 2720 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2721 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2722 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2723 | if (inputDesc == NULL) { |
| 2724 | ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id); |
| 2725 | return BAD_VALUE; |
| 2726 | } |
| 2727 | setInputDevice(inputDesc->mIoHandle, |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 2728 | getNewInputDevice(inputDesc->mIoHandle), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2729 | true, |
| 2730 | NULL); |
| 2731 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2732 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 2733 | ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d", |
| 2734 | status, patchDesc->mAfPatchHandle); |
| 2735 | removeAudioPatch(patchDesc->mHandle); |
| 2736 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2737 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2738 | } else { |
| 2739 | return BAD_VALUE; |
| 2740 | } |
| 2741 | } else { |
| 2742 | return BAD_VALUE; |
| 2743 | } |
| 2744 | return NO_ERROR; |
| 2745 | } |
| 2746 | |
| 2747 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, |
| 2748 | struct audio_patch *patches, |
| 2749 | unsigned int *generation) |
| 2750 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2751 | if (generation == NULL) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2752 | return BAD_VALUE; |
| 2753 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2754 | *generation = curAudioPortGeneration(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2755 | return mAudioPatches.listAudioPatches(num_patches, patches); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2756 | } |
| 2757 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2758 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2759 | { |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2760 | ALOGV("setAudioPortConfig()"); |
| 2761 | |
| 2762 | if (config == NULL) { |
| 2763 | return BAD_VALUE; |
| 2764 | } |
| 2765 | ALOGV("setAudioPortConfig() on port handle %d", config->id); |
| 2766 | // Only support gain configuration for now |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2767 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { |
| 2768 | return INVALID_OPERATION; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2769 | } |
| 2770 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2771 | sp<AudioPortConfig> audioPortConfig; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2772 | if (config->type == AUDIO_PORT_TYPE_MIX) { |
| 2773 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2774 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2775 | if (outputDesc == NULL) { |
| 2776 | return BAD_VALUE; |
| 2777 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2778 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 2779 | "setAudioPortConfig() called on duplicated output %d", |
| 2780 | outputDesc->mIoHandle); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2781 | audioPortConfig = outputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2782 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2783 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2784 | if (inputDesc == NULL) { |
| 2785 | return BAD_VALUE; |
| 2786 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2787 | audioPortConfig = inputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2788 | } else { |
| 2789 | return BAD_VALUE; |
| 2790 | } |
| 2791 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { |
| 2792 | sp<DeviceDescriptor> deviceDesc; |
| 2793 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
| 2794 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); |
| 2795 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
| 2796 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); |
| 2797 | } else { |
| 2798 | return BAD_VALUE; |
| 2799 | } |
| 2800 | if (deviceDesc == NULL) { |
| 2801 | return BAD_VALUE; |
| 2802 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2803 | audioPortConfig = deviceDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2804 | } else { |
| 2805 | return BAD_VALUE; |
| 2806 | } |
| 2807 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2808 | struct audio_port_config backupConfig; |
| 2809 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); |
| 2810 | if (status == NO_ERROR) { |
| 2811 | struct audio_port_config newConfig; |
| 2812 | audioPortConfig->toAudioPortConfig(&newConfig, config); |
| 2813 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2814 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2815 | if (status != NO_ERROR) { |
| 2816 | audioPortConfig->applyAudioPortConfig(&backupConfig); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2817 | } |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2818 | |
| 2819 | return status; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2820 | } |
| 2821 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2822 | void AudioPolicyManager::releaseResourcesForUid(uid_t uid) |
| 2823 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2824 | clearAudioSources(uid); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2825 | clearAudioPatches(uid); |
| 2826 | clearSessionRoutes(uid); |
| 2827 | } |
| 2828 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2829 | void AudioPolicyManager::clearAudioPatches(uid_t uid) |
| 2830 | { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 2831 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2832 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 2833 | if (patchDesc->mUid == uid) { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 2834 | releaseAudioPatch(mAudioPatches.keyAt(i), uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2835 | } |
| 2836 | } |
| 2837 | } |
| 2838 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2839 | void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy, |
| 2840 | audio_io_handle_t ouptutToSkip) |
| 2841 | { |
| 2842 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 2843 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 2844 | for (size_t j = 0; j < mOutputs.size(); j++) { |
| 2845 | if (mOutputs.keyAt(j) == ouptutToSkip) { |
| 2846 | continue; |
| 2847 | } |
| 2848 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j); |
| 2849 | if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) { |
| 2850 | continue; |
| 2851 | } |
| 2852 | // If the default device for this strategy is on another output mix, |
| 2853 | // invalidate all tracks in this strategy to force re connection. |
| 2854 | // Otherwise select new device on the output mix. |
| 2855 | if (outputs.indexOf(mOutputs.keyAt(j)) < 0) { |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2856 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2857 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
| 2858 | mpClientInterface->invalidateStream((audio_stream_type_t)stream); |
| 2859 | } |
| 2860 | } |
| 2861 | } else { |
| 2862 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
| 2863 | setOutputDevice(outputDesc, newDevice, false); |
| 2864 | } |
| 2865 | } |
| 2866 | } |
| 2867 | |
| 2868 | void AudioPolicyManager::clearSessionRoutes(uid_t uid) |
| 2869 | { |
| 2870 | // remove output routes associated with this uid |
| 2871 | SortedVector<routing_strategy> affectedStrategies; |
| 2872 | for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) { |
| 2873 | sp<SessionRoute> route = mOutputRoutes.valueAt(i); |
| 2874 | if (route->mUid == uid) { |
| 2875 | mOutputRoutes.removeItemsAt(i); |
| 2876 | if (route->mDeviceDescriptor != 0) { |
| 2877 | affectedStrategies.add(getStrategy(route->mStreamType)); |
| 2878 | } |
| 2879 | } |
| 2880 | } |
| 2881 | // reroute outputs if necessary |
| 2882 | for (size_t i = 0; i < affectedStrategies.size(); i++) { |
| 2883 | checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE); |
| 2884 | } |
| 2885 | |
| 2886 | // remove input routes associated with this uid |
| 2887 | SortedVector<audio_source_t> affectedSources; |
| 2888 | for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) { |
| 2889 | sp<SessionRoute> route = mInputRoutes.valueAt(i); |
| 2890 | if (route->mUid == uid) { |
| 2891 | mInputRoutes.removeItemsAt(i); |
| 2892 | if (route->mDeviceDescriptor != 0) { |
| 2893 | affectedSources.add(route->mSource); |
| 2894 | } |
| 2895 | } |
| 2896 | } |
| 2897 | // reroute inputs if necessary |
| 2898 | SortedVector<audio_io_handle_t> inputsToClose; |
| 2899 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 2900 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2901 | if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2902 | inputsToClose.add(inputDesc->mIoHandle); |
| 2903 | } |
| 2904 | } |
| 2905 | for (size_t i = 0; i < inputsToClose.size(); i++) { |
| 2906 | closeInput(inputsToClose[i]); |
| 2907 | } |
| 2908 | } |
| 2909 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2910 | void AudioPolicyManager::clearAudioSources(uid_t uid) |
| 2911 | { |
| 2912 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
| 2913 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 2914 | if (sourceDesc->mUid == uid) { |
| 2915 | stopAudioSource(mAudioSources.keyAt(i)); |
| 2916 | } |
| 2917 | } |
| 2918 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2919 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2920 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, |
| 2921 | audio_io_handle_t *ioHandle, |
| 2922 | audio_devices_t *device) |
| 2923 | { |
Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 2924 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); |
| 2925 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT); |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 2926 | *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2927 | |
François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 2928 | return mSoundTriggerSessions.acquireSession(*session, *ioHandle); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2929 | } |
| 2930 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2931 | status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source, |
| 2932 | const audio_attributes_t *attributes, |
| 2933 | audio_io_handle_t *handle, |
| 2934 | uid_t uid) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 2935 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2936 | ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle); |
| 2937 | if (source == NULL || attributes == NULL || handle == NULL) { |
| 2938 | return BAD_VALUE; |
| 2939 | } |
| 2940 | |
| 2941 | *handle = AUDIO_IO_HANDLE_NONE; |
| 2942 | |
| 2943 | if (source->role != AUDIO_PORT_ROLE_SOURCE || |
| 2944 | source->type != AUDIO_PORT_TYPE_DEVICE) { |
| 2945 | ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type); |
| 2946 | return INVALID_OPERATION; |
| 2947 | } |
| 2948 | |
| 2949 | sp<DeviceDescriptor> srcDeviceDesc = |
| 2950 | mAvailableInputDevices.getDevice(source->ext.device.type, |
| 2951 | String8(source->ext.device.address)); |
| 2952 | if (srcDeviceDesc == 0) { |
| 2953 | ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type); |
| 2954 | return BAD_VALUE; |
| 2955 | } |
| 2956 | sp<AudioSourceDescriptor> sourceDesc = |
| 2957 | new AudioSourceDescriptor(srcDeviceDesc, attributes, uid); |
| 2958 | |
| 2959 | struct audio_patch dummyPatch; |
| 2960 | sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid); |
| 2961 | sourceDesc->mPatchDesc = patchDesc; |
| 2962 | |
| 2963 | status_t status = connectAudioSource(sourceDesc); |
| 2964 | if (status == NO_ERROR) { |
| 2965 | mAudioSources.add(sourceDesc->getHandle(), sourceDesc); |
| 2966 | *handle = sourceDesc->getHandle(); |
| 2967 | } |
| 2968 | return status; |
| 2969 | } |
| 2970 | |
| 2971 | status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc) |
| 2972 | { |
| 2973 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle()); |
| 2974 | |
| 2975 | // make sure we only have one patch per source. |
| 2976 | disconnectAudioSource(sourceDesc); |
| 2977 | |
| 2978 | routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2979 | audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2980 | sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice; |
| 2981 | |
| 2982 | audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true); |
| 2983 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 2984 | mAvailableOutputDevices.getDevice(sinkDevice, String8("")); |
| 2985 | |
| 2986 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 2987 | struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch; |
| 2988 | |
| 2989 | if (srcDeviceDesc->getAudioPort()->mModule->getHandle() == |
| 2990 | sinkDeviceDesc->getAudioPort()->mModule->getHandle() && |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 2991 | srcDeviceDesc->getAudioPort()->mModule->getHalVersion() >= AUDIO_DEVICE_API_VERSION_3_0 && |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2992 | srcDeviceDesc->getAudioPort()->mGains.size() > 0) { |
| 2993 | ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__); |
| 2994 | // create patch between src device and output device |
| 2995 | // create Hwoutput and add to mHwOutputs |
| 2996 | } else { |
| 2997 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs); |
| 2998 | audio_io_handle_t output = |
| 2999 | selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID); |
| 3000 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 3001 | ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice); |
| 3002 | return INVALID_OPERATION; |
| 3003 | } |
| 3004 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 3005 | if (outputDesc->isDuplicated()) { |
| 3006 | ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice); |
| 3007 | return INVALID_OPERATION; |
| 3008 | } |
| 3009 | // create a special patch with no sink and two sources: |
| 3010 | // - the second source indicates to PatchPanel through which output mix this patch should |
| 3011 | // be connected as well as the stream type for volume control |
| 3012 | // - the sink is defined by whatever output device is currently selected for the output |
| 3013 | // though which this patch is routed. |
| 3014 | patch->num_sinks = 0; |
| 3015 | patch->num_sources = 2; |
| 3016 | srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL); |
| 3017 | outputDesc->toAudioPortConfig(&patch->sources[1], NULL); |
| 3018 | patch->sources[1].ext.mix.usecase.stream = stream; |
| 3019 | status_t status = mpClientInterface->createAudioPatch(patch, |
| 3020 | &afPatchHandle, |
| 3021 | 0); |
| 3022 | ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__, |
| 3023 | status, afPatchHandle); |
| 3024 | if (status != NO_ERROR) { |
| 3025 | ALOGW("%s patch panel could not connect device patch, error %d", |
| 3026 | __FUNCTION__, status); |
| 3027 | return INVALID_OPERATION; |
| 3028 | } |
| 3029 | uint32_t delayMs = 0; |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3030 | status = startSource(outputDesc, stream, sinkDevice, NULL, &delayMs); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3031 | |
| 3032 | if (status != NO_ERROR) { |
| 3033 | mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0); |
| 3034 | return status; |
| 3035 | } |
| 3036 | sourceDesc->mSwOutput = outputDesc; |
| 3037 | if (delayMs != 0) { |
| 3038 | usleep(delayMs * 1000); |
| 3039 | } |
| 3040 | } |
| 3041 | |
| 3042 | sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle; |
| 3043 | addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc); |
| 3044 | |
| 3045 | return NO_ERROR; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3046 | } |
| 3047 | |
Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 3048 | status_t AudioPolicyManager::stopAudioSource(audio_io_handle_t handle __unused) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3049 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3050 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle); |
| 3051 | ALOGV("%s handle %d", __FUNCTION__, handle); |
| 3052 | if (sourceDesc == 0) { |
| 3053 | ALOGW("%s unknown source for handle %d", __FUNCTION__, handle); |
| 3054 | return BAD_VALUE; |
| 3055 | } |
| 3056 | status_t status = disconnectAudioSource(sourceDesc); |
| 3057 | |
| 3058 | mAudioSources.removeItem(handle); |
| 3059 | return status; |
| 3060 | } |
| 3061 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3062 | status_t AudioPolicyManager::setMasterMono(bool mono) |
| 3063 | { |
| 3064 | if (mMasterMono == mono) { |
| 3065 | return NO_ERROR; |
| 3066 | } |
| 3067 | mMasterMono = mono; |
| 3068 | // if enabling mono we close all offloaded devices, which will invalidate the |
| 3069 | // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible |
| 3070 | // for recreating the new AudioTrack as non-offloaded PCM. |
| 3071 | // |
| 3072 | // If disabling mono, we leave all tracks as is: we don't know which clients |
| 3073 | // and tracks are able to be recreated as offloaded. The next "song" should |
| 3074 | // play back offloaded. |
| 3075 | if (mMasterMono) { |
| 3076 | Vector<audio_io_handle_t> offloaded; |
| 3077 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3078 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 3079 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
| 3080 | offloaded.push(desc->mIoHandle); |
| 3081 | } |
| 3082 | } |
| 3083 | for (size_t i = 0; i < offloaded.size(); ++i) { |
| 3084 | closeOutput(offloaded[i]); |
| 3085 | } |
| 3086 | } |
| 3087 | // update master mono for all remaining outputs |
| 3088 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3089 | updateMono(mOutputs.keyAt(i)); |
| 3090 | } |
| 3091 | return NO_ERROR; |
| 3092 | } |
| 3093 | |
| 3094 | status_t AudioPolicyManager::getMasterMono(bool *mono) |
| 3095 | { |
| 3096 | *mono = mMasterMono; |
| 3097 | return NO_ERROR; |
| 3098 | } |
| 3099 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3100 | status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc) |
| 3101 | { |
| 3102 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle()); |
| 3103 | |
| 3104 | sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle); |
| 3105 | if (patchDesc == 0) { |
| 3106 | ALOGW("%s source has no patch with handle %d", __FUNCTION__, |
| 3107 | sourceDesc->mPatchDesc->mHandle); |
| 3108 | return BAD_VALUE; |
| 3109 | } |
| 3110 | removeAudioPatch(sourceDesc->mPatchDesc->mHandle); |
| 3111 | |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 3112 | audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3113 | sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote(); |
| 3114 | if (swOutputDesc != 0) { |
| 3115 | stopSource(swOutputDesc, stream, false); |
| 3116 | mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3117 | } else { |
| 3118 | sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote(); |
| 3119 | if (hwOutputDesc != 0) { |
| 3120 | // release patch between src device and output device |
| 3121 | // close Hwoutput and remove from mHwOutputs |
| 3122 | } else { |
| 3123 | ALOGW("%s source has neither SW nor HW output", __FUNCTION__); |
| 3124 | } |
| 3125 | } |
| 3126 | return NO_ERROR; |
| 3127 | } |
| 3128 | |
| 3129 | sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput( |
| 3130 | audio_io_handle_t output, routing_strategy strategy) |
| 3131 | { |
| 3132 | sp<AudioSourceDescriptor> source; |
| 3133 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
| 3134 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 3135 | routing_strategy sourceStrategy = |
| 3136 | (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes); |
| 3137 | sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote(); |
| 3138 | if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) { |
| 3139 | source = sourceDesc; |
| 3140 | break; |
| 3141 | } |
| 3142 | } |
| 3143 | return source; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3144 | } |
| 3145 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3146 | // ---------------------------------------------------------------------------- |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3147 | // AudioPolicyManager |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3148 | // ---------------------------------------------------------------------------- |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3149 | uint32_t AudioPolicyManager::nextAudioPortGeneration() |
| 3150 | { |
| 3151 | return android_atomic_inc(&mAudioPortGeneration); |
| 3152 | } |
| 3153 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3154 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3155 | : |
| 3156 | #ifdef AUDIO_POLICY_TEST |
| 3157 | Thread(false), |
| 3158 | #endif //AUDIO_POLICY_TEST |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3159 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3160 | mA2dpSuspended(false), |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 3161 | mAudioPortGeneration(1), |
| 3162 | mBeaconMuteRefCount(0), |
| 3163 | mBeaconPlayingRefCount(0), |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3164 | mBeaconMuted(false), |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3165 | mTtsOutputAvailable(false), |
| 3166 | mMasterMono(false) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3167 | { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3168 | mUidCached = getuid(); |
| 3169 | mpClientInterface = clientInterface; |
| 3170 | |
| 3171 | // TODO: remove when legacy conf file is removed. true on devices that use DRC on the |
| 3172 | // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly. |
| 3173 | // Note: remove also speaker_drc_enabled from global configuration of XML config file. |
| 3174 | bool speakerDrcEnabled = false; |
| 3175 | |
| 3176 | #ifdef USE_XML_AUDIO_POLICY_CONF |
| 3177 | mVolumeCurves = new VolumeCurvesCollection(); |
| 3178 | AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices, |
| 3179 | mDefaultOutputDevice, speakerDrcEnabled, |
| 3180 | static_cast<VolumeCurvesCollection *>(mVolumeCurves)); |
| 3181 | PolicySerializer serializer; |
| 3182 | if (serializer.deserialize(AUDIO_POLICY_XML_CONFIG_FILE, config) != NO_ERROR) { |
| 3183 | #else |
| 3184 | mVolumeCurves = new StreamDescriptorCollection(); |
| 3185 | AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices, |
| 3186 | mDefaultOutputDevice, speakerDrcEnabled); |
| 3187 | if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, config) != NO_ERROR) && |
| 3188 | (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, config) != NO_ERROR)) { |
| 3189 | #endif |
| 3190 | ALOGE("could not load audio policy configuration file, setting defaults"); |
| 3191 | config.setDefault(); |
| 3192 | } |
| 3193 | // must be done after reading the policy (since conditionned by Speaker Drc Enabling) |
| 3194 | mVolumeCurves->initializeVolumeCurves(speakerDrcEnabled); |
| 3195 | |
| 3196 | // 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] | 3197 | audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance(); |
| 3198 | if (!engineInstance) { |
| 3199 | ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__); |
| 3200 | return; |
| 3201 | } |
| 3202 | // Retrieve the Policy Manager Interface |
| 3203 | mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>(); |
| 3204 | if (mEngine == NULL) { |
| 3205 | ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__); |
| 3206 | return; |
| 3207 | } |
| 3208 | mEngine->setObserver(this); |
| 3209 | status_t status = mEngine->initCheck(); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 3210 | (void) status; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3211 | ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status); |
| 3212 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3213 | // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3214 | // open all output streams needed to access attached devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3215 | audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types(); |
| 3216 | audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3217 | for (size_t i = 0; i < mHwModules.size(); i++) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3218 | mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3219 | if (mHwModules[i]->mHandle == 0) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3220 | ALOGW("could not open HW module %s", mHwModules[i]->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3221 | continue; |
| 3222 | } |
| 3223 | // open all output streams needed to access attached devices |
| 3224 | // except for direct output streams that are only opened when they are actually |
| 3225 | // required by an app. |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3226 | // This also validates mAvailableOutputDevices list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3227 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 3228 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3229 | const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3230 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3231 | if (!outProfile->hasSupportedDevices()) { |
| 3232 | ALOGW("Output profile contains no device on module %s", mHwModules[i]->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3233 | continue; |
| 3234 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3235 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3236 | mTtsOutputAvailable = true; |
| 3237 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3238 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3239 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3240 | continue; |
| 3241 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3242 | audio_devices_t profileType = outProfile->getSupportedDevicesType(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3243 | if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) { |
| 3244 | profileType = mDefaultOutputDevice->type(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3245 | } else { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3246 | // chose first device present in profile's SupportedDevices also part of |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3247 | // outputDeviceTypes |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3248 | profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3249 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3250 | if ((profileType & outputDeviceTypes) == 0) { |
| 3251 | continue; |
| 3252 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3253 | sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile, |
| 3254 | mpClientInterface); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3255 | const DeviceVector &supportedDevices = outProfile->getSupportedDevices(); |
| 3256 | const DeviceVector &devicesForType = supportedDevices.getDevicesFromType(profileType); |
| 3257 | String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress |
| 3258 | : String8(""); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3259 | |
| 3260 | outputDesc->mDevice = profileType; |
| 3261 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3262 | config.sample_rate = outputDesc->mSamplingRate; |
| 3263 | config.channel_mask = outputDesc->mChannelMask; |
| 3264 | config.format = outputDesc->mFormat; |
| 3265 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3266 | status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(), |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3267 | &output, |
| 3268 | &config, |
| 3269 | &outputDesc->mDevice, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3270 | address, |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3271 | &outputDesc->mLatency, |
| 3272 | outputDesc->mFlags); |
| 3273 | |
| 3274 | if (status != NO_ERROR) { |
| 3275 | ALOGW("Cannot open output stream for device %08x on hw module %s", |
| 3276 | outputDesc->mDevice, |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3277 | mHwModules[i]->getName()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3278 | } else { |
| 3279 | outputDesc->mSamplingRate = config.sample_rate; |
| 3280 | outputDesc->mChannelMask = config.channel_mask; |
| 3281 | outputDesc->mFormat = config.format; |
| 3282 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3283 | for (size_t k = 0; k < supportedDevices.size(); k++) { |
| 3284 | ssize_t index = mAvailableOutputDevices.indexOf(supportedDevices[k]); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3285 | // 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] | 3286 | if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) { |
| 3287 | mAvailableOutputDevices[index]->attach(mHwModules[i]); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3288 | } |
| 3289 | } |
| 3290 | if (mPrimaryOutput == 0 && |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3291 | outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3292 | mPrimaryOutput = outputDesc; |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3293 | } |
| 3294 | addOutput(output, outputDesc); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3295 | setOutputDevice(outputDesc, |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3296 | outputDesc->mDevice, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3297 | true, |
| 3298 | 0, |
| 3299 | NULL, |
| 3300 | address.string()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3301 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3302 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3303 | // open input streams needed to access attached devices to validate |
| 3304 | // mAvailableInputDevices list |
| 3305 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) |
| 3306 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3307 | const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3308 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3309 | if (!inProfile->hasSupportedDevices()) { |
| 3310 | ALOGW("Input profile contains no device on module %s", mHwModules[i]->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3311 | continue; |
| 3312 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3313 | // chose first device present in profile's SupportedDevices also part of |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3314 | // inputDeviceTypes |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3315 | audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes); |
| 3316 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3317 | if ((profileType & inputDeviceTypes) == 0) { |
| 3318 | continue; |
| 3319 | } |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 3320 | sp<AudioInputDescriptor> inputDesc = |
| 3321 | new AudioInputDescriptor(inProfile); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3322 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3323 | inputDesc->mDevice = profileType; |
| 3324 | |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 3325 | // find the address |
| 3326 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType); |
| 3327 | // the inputs vector must be of size 1, but we don't want to crash here |
| 3328 | String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress |
| 3329 | : String8(""); |
| 3330 | ALOGV(" for input device 0x%x using address %s", profileType, address.string()); |
| 3331 | ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!"); |
| 3332 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3333 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3334 | config.sample_rate = inputDesc->mSamplingRate; |
| 3335 | config.channel_mask = inputDesc->mChannelMask; |
| 3336 | config.format = inputDesc->mFormat; |
| 3337 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3338 | status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(), |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3339 | &input, |
| 3340 | &config, |
| 3341 | &inputDesc->mDevice, |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 3342 | address, |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3343 | AUDIO_SOURCE_MIC, |
| 3344 | AUDIO_INPUT_FLAG_NONE); |
| 3345 | |
| 3346 | if (status == NO_ERROR) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3347 | const DeviceVector &supportedDevices = inProfile->getSupportedDevices(); |
| 3348 | for (size_t k = 0; k < supportedDevices.size(); k++) { |
| 3349 | ssize_t index = mAvailableInputDevices.indexOf(supportedDevices[k]); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3350 | // 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] | 3351 | if (index >= 0) { |
| 3352 | sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index]; |
| 3353 | if (!devDesc->isAttached()) { |
| 3354 | devDesc->attach(mHwModules[i]); |
| 3355 | devDesc->importAudioPort(inProfile); |
| 3356 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3357 | } |
| 3358 | } |
| 3359 | mpClientInterface->closeInput(input); |
| 3360 | } else { |
| 3361 | ALOGW("Cannot open input stream for device %08x on hw module %s", |
| 3362 | inputDesc->mDevice, |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3363 | mHwModules[i]->getName()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3364 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3365 | } |
| 3366 | } |
| 3367 | // make sure all attached devices have been allocated a unique ID |
| 3368 | for (size_t i = 0; i < mAvailableOutputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 3369 | if (!mAvailableOutputDevices[i]->isAttached()) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3370 | ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3371 | mAvailableOutputDevices.remove(mAvailableOutputDevices[i]); |
| 3372 | continue; |
| 3373 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3374 | // The device is now validated and can be appended to the available devices of the engine |
| 3375 | mEngine->setDeviceConnectionState(mAvailableOutputDevices[i], |
| 3376 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3377 | i++; |
| 3378 | } |
| 3379 | for (size_t i = 0; i < mAvailableInputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 3380 | if (!mAvailableInputDevices[i]->isAttached()) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3381 | ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3382 | mAvailableInputDevices.remove(mAvailableInputDevices[i]); |
| 3383 | continue; |
| 3384 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3385 | // The device is now validated and can be appended to the available devices of the engine |
| 3386 | mEngine->setDeviceConnectionState(mAvailableInputDevices[i], |
| 3387 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3388 | i++; |
| 3389 | } |
| 3390 | // make sure default device is reachable |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3391 | if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3392 | ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3393 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3394 | |
| 3395 | ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output"); |
| 3396 | |
| 3397 | updateDevicesAndOutputs(); |
| 3398 | |
| 3399 | #ifdef AUDIO_POLICY_TEST |
| 3400 | if (mPrimaryOutput != 0) { |
| 3401 | AudioParameter outputCmd = AudioParameter(); |
| 3402 | outputCmd.addInt(String8("set_id"), 0); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3403 | mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3404 | |
| 3405 | mTestDevice = AUDIO_DEVICE_OUT_SPEAKER; |
| 3406 | mTestSamplingRate = 44100; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3407 | mTestFormat = AUDIO_FORMAT_PCM_16_BIT; |
| 3408 | mTestChannels = AUDIO_CHANNEL_OUT_STEREO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3409 | mTestLatencyMs = 0; |
| 3410 | mCurOutput = 0; |
| 3411 | mDirectOutput = false; |
| 3412 | for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { |
| 3413 | mTestOutputs[i] = 0; |
| 3414 | } |
| 3415 | |
| 3416 | const size_t SIZE = 256; |
| 3417 | char buffer[SIZE]; |
| 3418 | snprintf(buffer, SIZE, "AudioPolicyManagerTest"); |
| 3419 | run(buffer, ANDROID_PRIORITY_AUDIO); |
| 3420 | } |
| 3421 | #endif //AUDIO_POLICY_TEST |
| 3422 | } |
| 3423 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3424 | AudioPolicyManager::~AudioPolicyManager() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3425 | { |
| 3426 | #ifdef AUDIO_POLICY_TEST |
| 3427 | exit(); |
| 3428 | #endif //AUDIO_POLICY_TEST |
| 3429 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3430 | mpClientInterface->closeOutput(mOutputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3431 | } |
| 3432 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3433 | mpClientInterface->closeInput(mInputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3434 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3435 | mAvailableOutputDevices.clear(); |
| 3436 | mAvailableInputDevices.clear(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3437 | mOutputs.clear(); |
| 3438 | mInputs.clear(); |
| 3439 | mHwModules.clear(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3440 | } |
| 3441 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3442 | status_t AudioPolicyManager::initCheck() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3443 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 3444 | return hasPrimaryOutput() ? NO_ERROR : NO_INIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3445 | } |
| 3446 | |
| 3447 | #ifdef AUDIO_POLICY_TEST |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3448 | bool AudioPolicyManager::threadLoop() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3449 | { |
| 3450 | ALOGV("entering threadLoop()"); |
| 3451 | while (!exitPending()) |
| 3452 | { |
| 3453 | String8 command; |
| 3454 | int valueInt; |
| 3455 | String8 value; |
| 3456 | |
| 3457 | Mutex::Autolock _l(mLock); |
| 3458 | mWaitWorkCV.waitRelative(mLock, milliseconds(50)); |
| 3459 | |
| 3460 | command = mpClientInterface->getParameters(0, String8("test_cmd_policy")); |
| 3461 | AudioParameter param = AudioParameter(command); |
| 3462 | |
| 3463 | if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR && |
| 3464 | valueInt != 0) { |
| 3465 | ALOGV("Test command %s received", command.string()); |
| 3466 | String8 target; |
| 3467 | if (param.get(String8("target"), target) != NO_ERROR) { |
| 3468 | target = "Manager"; |
| 3469 | } |
| 3470 | if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) { |
| 3471 | param.remove(String8("test_cmd_policy_output")); |
| 3472 | mCurOutput = valueInt; |
| 3473 | } |
| 3474 | if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) { |
| 3475 | param.remove(String8("test_cmd_policy_direct")); |
| 3476 | if (value == "false") { |
| 3477 | mDirectOutput = false; |
| 3478 | } else if (value == "true") { |
| 3479 | mDirectOutput = true; |
| 3480 | } |
| 3481 | } |
| 3482 | if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) { |
| 3483 | param.remove(String8("test_cmd_policy_input")); |
| 3484 | mTestInput = valueInt; |
| 3485 | } |
| 3486 | |
| 3487 | if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) { |
| 3488 | param.remove(String8("test_cmd_policy_format")); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3489 | int format = AUDIO_FORMAT_INVALID; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3490 | if (value == "PCM 16 bits") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3491 | format = AUDIO_FORMAT_PCM_16_BIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3492 | } else if (value == "PCM 8 bits") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3493 | format = AUDIO_FORMAT_PCM_8_BIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3494 | } else if (value == "Compressed MP3") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3495 | format = AUDIO_FORMAT_MP3; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3496 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3497 | if (format != AUDIO_FORMAT_INVALID) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3498 | if (target == "Manager") { |
| 3499 | mTestFormat = format; |
| 3500 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 3501 | AudioParameter outputParam = AudioParameter(); |
| 3502 | outputParam.addInt(String8("format"), format); |
| 3503 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 3504 | } |
| 3505 | } |
| 3506 | } |
| 3507 | if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) { |
| 3508 | param.remove(String8("test_cmd_policy_channels")); |
| 3509 | int channels = 0; |
| 3510 | |
| 3511 | if (value == "Channels Stereo") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3512 | channels = AUDIO_CHANNEL_OUT_STEREO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3513 | } else if (value == "Channels Mono") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3514 | channels = AUDIO_CHANNEL_OUT_MONO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3515 | } |
| 3516 | if (channels != 0) { |
| 3517 | if (target == "Manager") { |
| 3518 | mTestChannels = channels; |
| 3519 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 3520 | AudioParameter outputParam = AudioParameter(); |
| 3521 | outputParam.addInt(String8("channels"), channels); |
| 3522 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 3523 | } |
| 3524 | } |
| 3525 | } |
| 3526 | if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) { |
| 3527 | param.remove(String8("test_cmd_policy_sampleRate")); |
| 3528 | if (valueInt >= 0 && valueInt <= 96000) { |
| 3529 | int samplingRate = valueInt; |
| 3530 | if (target == "Manager") { |
| 3531 | mTestSamplingRate = samplingRate; |
| 3532 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 3533 | AudioParameter outputParam = AudioParameter(); |
| 3534 | outputParam.addInt(String8("sampling_rate"), samplingRate); |
| 3535 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 3536 | } |
| 3537 | } |
| 3538 | } |
| 3539 | |
| 3540 | if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) { |
| 3541 | param.remove(String8("test_cmd_policy_reopen")); |
| 3542 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3543 | mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput);); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3544 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3545 | audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3546 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3547 | removeOutput(mPrimaryOutput->mIoHandle); |
| 3548 | sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL, |
| 3549 | mpClientInterface); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3550 | outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3551 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3552 | config.sample_rate = outputDesc->mSamplingRate; |
| 3553 | config.channel_mask = outputDesc->mChannelMask; |
| 3554 | config.format = outputDesc->mFormat; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3555 | audio_io_handle_t handle; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3556 | status_t status = mpClientInterface->openOutput(moduleHandle, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3557 | &handle, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3558 | &config, |
| 3559 | &outputDesc->mDevice, |
| 3560 | String8(""), |
| 3561 | &outputDesc->mLatency, |
| 3562 | outputDesc->mFlags); |
| 3563 | if (status != NO_ERROR) { |
| 3564 | ALOGE("Failed to reopen hardware output stream, " |
| 3565 | "samplingRate: %d, format %d, channels %d", |
| 3566 | outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3567 | } else { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3568 | outputDesc->mSamplingRate = config.sample_rate; |
| 3569 | outputDesc->mChannelMask = config.channel_mask; |
| 3570 | outputDesc->mFormat = config.format; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3571 | mPrimaryOutput = outputDesc; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3572 | AudioParameter outputCmd = AudioParameter(); |
| 3573 | outputCmd.addInt(String8("set_id"), 0); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3574 | mpClientInterface->setParameters(handle, outputCmd.toString()); |
| 3575 | addOutput(handle, outputDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3576 | } |
| 3577 | } |
| 3578 | |
| 3579 | |
| 3580 | mpClientInterface->setParameters(0, String8("test_cmd_policy=")); |
| 3581 | } |
| 3582 | } |
| 3583 | return false; |
| 3584 | } |
| 3585 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3586 | void AudioPolicyManager::exit() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3587 | { |
| 3588 | { |
| 3589 | AutoMutex _l(mLock); |
| 3590 | requestExit(); |
| 3591 | mWaitWorkCV.signal(); |
| 3592 | } |
| 3593 | requestExitAndWait(); |
| 3594 | } |
| 3595 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3596 | int AudioPolicyManager::testOutputIndex(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3597 | { |
| 3598 | for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { |
| 3599 | if (output == mTestOutputs[i]) return i; |
| 3600 | } |
| 3601 | return 0; |
| 3602 | } |
| 3603 | #endif //AUDIO_POLICY_TEST |
| 3604 | |
| 3605 | // --- |
| 3606 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3607 | void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3608 | { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 3609 | outputDesc->setIoHandle(output); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3610 | mOutputs.add(output, outputDesc); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3611 | updateMono(output); // update mono status when adding to output list |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3612 | nextAudioPortGeneration(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3613 | } |
| 3614 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3615 | void AudioPolicyManager::removeOutput(audio_io_handle_t output) |
| 3616 | { |
| 3617 | mOutputs.removeItem(output); |
| 3618 | } |
| 3619 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3620 | void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3621 | { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 3622 | inputDesc->setIoHandle(input); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3623 | mInputs.add(input, inputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3624 | nextAudioPortGeneration(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3625 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3626 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3627 | void AudioPolicyManager::findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/, |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3628 | const audio_devices_t device /*in*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3629 | const String8 address /*in*/, |
| 3630 | SortedVector<audio_io_handle_t>& outputs /*out*/) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3631 | sp<DeviceDescriptor> devDesc = |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3632 | desc->mProfile->getSupportedDeviceByAddress(device, address); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3633 | if (devDesc != 0) { |
| 3634 | ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s", |
| 3635 | desc->mIoHandle, address.string()); |
| 3636 | outputs.add(desc->mIoHandle); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3637 | } |
| 3638 | } |
| 3639 | |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3640 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3641 | audio_policy_dev_state_t state, |
| 3642 | SortedVector<audio_io_handle_t>& outputs, |
| 3643 | const String8 address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3644 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3645 | audio_devices_t device = devDesc->type(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3646 | sp<SwAudioOutputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 3647 | |
| 3648 | if (audio_device_is_digital(device)) { |
| 3649 | // erase all current sample rates, formats and channel masks |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 3650 | devDesc->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 3651 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3652 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3653 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3654 | // first list already open outputs that can be routed to this device |
| 3655 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3656 | desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3657 | if (!desc->isDuplicated() && (desc->supportedDevices() & device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3658 | if (!device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3659 | ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i)); |
| 3660 | outputs.add(mOutputs.keyAt(i)); |
| 3661 | } else { |
| 3662 | ALOGV(" checking address match due to device 0x%x", device); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3663 | findIoHandlesByAddress(desc, device, address, outputs); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3664 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3665 | } |
| 3666 | } |
| 3667 | // then look for output profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3668 | SortedVector< sp<IOProfile> > profiles; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3669 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 3670 | { |
| 3671 | if (mHwModules[i]->mHandle == 0) { |
| 3672 | continue; |
| 3673 | } |
| 3674 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 3675 | { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3676 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3677 | if (profile->supportDevice(device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3678 | if (!device_distinguishes_on_address(device) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3679 | profile->supportDeviceAddress(address)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3680 | profiles.add(profile); |
| 3681 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i); |
| 3682 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3683 | } |
| 3684 | } |
| 3685 | } |
| 3686 | |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 3687 | ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size()); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3688 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3689 | if (profiles.isEmpty() && outputs.isEmpty()) { |
| 3690 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 3691 | return BAD_VALUE; |
| 3692 | } |
| 3693 | |
| 3694 | // open outputs for matching profiles if needed. Direct outputs are also opened to |
| 3695 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 3696 | 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] | 3697 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3698 | |
| 3699 | // nothing to do if one output is already opened for this profile |
| 3700 | size_t j; |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3701 | for (j = 0; j < outputs.size(); j++) { |
| 3702 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3703 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3704 | // matching profile: save the sample rates, format and channel masks supported |
| 3705 | // by the profile in our device descriptor |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3706 | if (audio_device_is_digital(device)) { |
| 3707 | devDesc->importAudioPort(profile); |
| 3708 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3709 | break; |
| 3710 | } |
| 3711 | } |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3712 | if (j != outputs.size()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3713 | continue; |
| 3714 | } |
| 3715 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3716 | ALOGV("opening output for device %08x with params %s profile %p", |
| 3717 | device, address.string(), profile.get()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3718 | desc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3719 | desc->mDevice = device; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3720 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3721 | config.sample_rate = desc->mSamplingRate; |
| 3722 | config.channel_mask = desc->mChannelMask; |
| 3723 | config.format = desc->mFormat; |
| 3724 | config.offload_info.sample_rate = desc->mSamplingRate; |
| 3725 | config.offload_info.channel_mask = desc->mChannelMask; |
| 3726 | config.offload_info.format = desc->mFormat; |
| 3727 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3728 | status_t status = mpClientInterface->openOutput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3729 | &output, |
| 3730 | &config, |
| 3731 | &desc->mDevice, |
| 3732 | address, |
| 3733 | &desc->mLatency, |
| 3734 | desc->mFlags); |
| 3735 | if (status == NO_ERROR) { |
| 3736 | desc->mSamplingRate = config.sample_rate; |
| 3737 | desc->mChannelMask = config.channel_mask; |
| 3738 | desc->mFormat = config.format; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3739 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3740 | // Here is where the out_set_parameters() for card & device gets called |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3741 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3742 | char *param = audio_device_address_to_parameter(device, address); |
| 3743 | mpClientInterface->setParameters(output, String8(param)); |
| 3744 | free(param); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3745 | } |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 3746 | updateAudioProfiles(device, output, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3747 | if (!profile->hasValidAudioProfile()) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3748 | ALOGW("checkOutputsForDevice() missing param"); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3749 | mpClientInterface->closeOutput(output); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3750 | output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3751 | } else if (profile->hasDynamicAudioProfile()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3752 | mpClientInterface->closeOutput(output); |
Phil Burk | 702b105 | 2016-03-02 16:38:26 -0800 | [diff] [blame] | 3753 | output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3754 | profile->pickAudioProfile(config.sample_rate, config.channel_mask, config.format); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3755 | config.offload_info.sample_rate = config.sample_rate; |
| 3756 | config.offload_info.channel_mask = config.channel_mask; |
| 3757 | config.offload_info.format = config.format; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3758 | status = mpClientInterface->openOutput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3759 | &output, |
| 3760 | &config, |
| 3761 | &desc->mDevice, |
| 3762 | address, |
| 3763 | &desc->mLatency, |
| 3764 | desc->mFlags); |
| 3765 | if (status == NO_ERROR) { |
| 3766 | desc->mSamplingRate = config.sample_rate; |
| 3767 | desc->mChannelMask = config.channel_mask; |
| 3768 | desc->mFormat = config.format; |
| 3769 | } else { |
| 3770 | output = AUDIO_IO_HANDLE_NONE; |
| 3771 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3772 | } |
| 3773 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3774 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3775 | addOutput(output, desc); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3776 | if (device_distinguishes_on_address(device) && address != "0") { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3777 | sp<AudioPolicyMix> policyMix; |
| 3778 | if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3779 | ALOGE("checkOutputsForDevice() cannot find policy for address %s", |
| 3780 | address.string()); |
| 3781 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3782 | policyMix->setOutput(desc); |
Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 3783 | desc->mPolicyMix = policyMix->getMix(); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3784 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 3785 | } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
| 3786 | hasPrimaryOutput()) { |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 3787 | // no duplicated output for direct outputs and |
| 3788 | // outputs used by dynamic policy mixes |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3789 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3790 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3791 | // set initial stream volume for device |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3792 | applyStreamVolumes(desc, device, 0, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3793 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3794 | //TODO: configure audio effect output stage here |
| 3795 | |
| 3796 | // 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] | 3797 | duplicatedOutput = |
| 3798 | mpClientInterface->openDuplicateOutput(output, |
| 3799 | mPrimaryOutput->mIoHandle); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3800 | if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3801 | // add duplicated output descriptor |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3802 | sp<SwAudioOutputDescriptor> dupOutputDesc = |
| 3803 | new SwAudioOutputDescriptor(NULL, mpClientInterface); |
| 3804 | dupOutputDesc->mOutput1 = mPrimaryOutput; |
| 3805 | dupOutputDesc->mOutput2 = desc; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3806 | dupOutputDesc->mSamplingRate = desc->mSamplingRate; |
| 3807 | dupOutputDesc->mFormat = desc->mFormat; |
| 3808 | dupOutputDesc->mChannelMask = desc->mChannelMask; |
| 3809 | dupOutputDesc->mLatency = desc->mLatency; |
| 3810 | addOutput(duplicatedOutput, dupOutputDesc); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3811 | applyStreamVolumes(dupOutputDesc, device, 0, true); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3812 | } else { |
| 3813 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3814 | mPrimaryOutput->mIoHandle, output); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3815 | mpClientInterface->closeOutput(output); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3816 | removeOutput(output); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3817 | nextAudioPortGeneration(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3818 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3819 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3820 | } |
| 3821 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3822 | } else { |
| 3823 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3824 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3825 | if (output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3826 | ALOGW("checkOutputsForDevice() could not open output for device %x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3827 | profiles.removeAt(profile_index); |
| 3828 | profile_index--; |
| 3829 | } else { |
| 3830 | outputs.add(output); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3831 | // Load digital format info only for digital devices |
| 3832 | if (audio_device_is_digital(device)) { |
| 3833 | devDesc->importAudioPort(profile); |
| 3834 | } |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3835 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3836 | if (device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3837 | ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)", |
| 3838 | device, address.string()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3839 | setOutputDevice(desc, device, true/*force*/, 0/*delay*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3840 | NULL/*patch handle*/, address.string()); |
| 3841 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3842 | ALOGV("checkOutputsForDevice(): adding output %d", output); |
| 3843 | } |
| 3844 | } |
| 3845 | |
| 3846 | if (profiles.isEmpty()) { |
| 3847 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 3848 | return BAD_VALUE; |
| 3849 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3850 | } else { // Disconnect |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3851 | // check if one opened output is not needed any more after disconnecting one device |
| 3852 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3853 | desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3854 | if (!desc->isDuplicated()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3855 | // exact match on device |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3856 | if (device_distinguishes_on_address(device) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3857 | (desc->supportedDevices() == device)) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3858 | findIoHandlesByAddress(desc, device, address, outputs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3859 | } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3860 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", |
| 3861 | mOutputs.keyAt(i)); |
| 3862 | outputs.add(mOutputs.keyAt(i)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3863 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3864 | } |
| 3865 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3866 | // Clear any profiles associated with the disconnected device. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3867 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 3868 | { |
| 3869 | if (mHwModules[i]->mHandle == 0) { |
| 3870 | continue; |
| 3871 | } |
| 3872 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 3873 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3874 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3875 | if (profile->supportDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3876 | ALOGV("checkOutputsForDevice(): " |
| 3877 | "clearing direct output profile %zu on module %zu", j, i); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3878 | profile->clearAudioProfiles(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3879 | } |
| 3880 | } |
| 3881 | } |
| 3882 | } |
| 3883 | return NO_ERROR; |
| 3884 | } |
| 3885 | |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3886 | status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor> devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3887 | audio_policy_dev_state_t state, |
| 3888 | SortedVector<audio_io_handle_t>& inputs, |
| 3889 | const String8 address) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3890 | { |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3891 | audio_devices_t device = devDesc->type(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3892 | sp<AudioInputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 3893 | |
| 3894 | if (audio_device_is_digital(device)) { |
| 3895 | // erase all current sample rates, formats and channel masks |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 3896 | devDesc->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 3897 | } |
| 3898 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3899 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 3900 | // first list already open inputs that can be routed to this device |
| 3901 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 3902 | desc = mInputs.valueAt(input_index); |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3903 | if (desc->mProfile->supportDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3904 | ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index)); |
| 3905 | inputs.add(mInputs.keyAt(input_index)); |
| 3906 | } |
| 3907 | } |
| 3908 | |
| 3909 | // then look for input profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3910 | SortedVector< sp<IOProfile> > profiles; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3911 | for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++) |
| 3912 | { |
| 3913 | if (mHwModules[module_idx]->mHandle == 0) { |
| 3914 | continue; |
| 3915 | } |
| 3916 | for (size_t profile_index = 0; |
| 3917 | profile_index < mHwModules[module_idx]->mInputProfiles.size(); |
| 3918 | profile_index++) |
| 3919 | { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3920 | sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index]; |
| 3921 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3922 | if (profile->supportDevice(device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3923 | if (!device_distinguishes_on_address(device) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3924 | profile->supportDeviceAddress(address)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3925 | profiles.add(profile); |
| 3926 | ALOGV("checkInputsForDevice(): adding profile %zu from module %zu", |
| 3927 | profile_index, module_idx); |
| 3928 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3929 | } |
| 3930 | } |
| 3931 | } |
| 3932 | |
| 3933 | if (profiles.isEmpty() && inputs.isEmpty()) { |
| 3934 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 3935 | return BAD_VALUE; |
| 3936 | } |
| 3937 | |
| 3938 | // open inputs for matching profiles if needed. Direct inputs are also opened to |
| 3939 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 3940 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { |
| 3941 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3942 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3943 | // nothing to do if one input is already opened for this profile |
| 3944 | size_t input_index; |
| 3945 | for (input_index = 0; input_index < mInputs.size(); input_index++) { |
| 3946 | desc = mInputs.valueAt(input_index); |
| 3947 | if (desc->mProfile == profile) { |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3948 | if (audio_device_is_digital(device)) { |
| 3949 | devDesc->importAudioPort(profile); |
| 3950 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3951 | break; |
| 3952 | } |
| 3953 | } |
| 3954 | if (input_index != mInputs.size()) { |
| 3955 | continue; |
| 3956 | } |
| 3957 | |
| 3958 | ALOGV("opening input for device 0x%X with params %s", device, address.string()); |
| 3959 | desc = new AudioInputDescriptor(profile); |
| 3960 | desc->mDevice = device; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3961 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3962 | config.sample_rate = desc->mSamplingRate; |
| 3963 | config.channel_mask = desc->mChannelMask; |
| 3964 | config.format = desc->mFormat; |
| 3965 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3966 | status_t status = mpClientInterface->openInput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3967 | &input, |
| 3968 | &config, |
| 3969 | &desc->mDevice, |
| 3970 | address, |
| 3971 | AUDIO_SOURCE_MIC, |
| 3972 | AUDIO_INPUT_FLAG_NONE /*FIXME*/); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3973 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3974 | if (status == NO_ERROR) { |
| 3975 | desc->mSamplingRate = config.sample_rate; |
| 3976 | desc->mChannelMask = config.channel_mask; |
| 3977 | desc->mFormat = config.format; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3978 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3979 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3980 | char *param = audio_device_address_to_parameter(device, address); |
| 3981 | mpClientInterface->setParameters(input, String8(param)); |
| 3982 | free(param); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3983 | } |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 3984 | updateAudioProfiles(device, input, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3985 | if (!profile->hasValidAudioProfile()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3986 | ALOGW("checkInputsForDevice() direct input missing param"); |
| 3987 | mpClientInterface->closeInput(input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3988 | input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3989 | } |
| 3990 | |
| 3991 | if (input != 0) { |
| 3992 | addInput(input, desc); |
| 3993 | } |
| 3994 | } // endif input != 0 |
| 3995 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3996 | if (input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3997 | ALOGW("checkInputsForDevice() could not open input for device 0x%X", device); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3998 | profiles.removeAt(profile_index); |
| 3999 | profile_index--; |
| 4000 | } else { |
| 4001 | inputs.add(input); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4002 | if (audio_device_is_digital(device)) { |
| 4003 | devDesc->importAudioPort(profile); |
| 4004 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4005 | ALOGV("checkInputsForDevice(): adding input %d", input); |
| 4006 | } |
| 4007 | } // end scan profiles |
| 4008 | |
| 4009 | if (profiles.isEmpty()) { |
| 4010 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 4011 | return BAD_VALUE; |
| 4012 | } |
| 4013 | } else { |
| 4014 | // Disconnect |
| 4015 | // check if one opened input is not needed any more after disconnecting one device |
| 4016 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4017 | desc = mInputs.valueAt(input_index); |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4018 | if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4019 | ALOGV("checkInputsForDevice(): disconnecting adding input %d", |
| 4020 | mInputs.keyAt(input_index)); |
| 4021 | inputs.add(mInputs.keyAt(input_index)); |
| 4022 | } |
| 4023 | } |
| 4024 | // Clear any profiles associated with the disconnected device. |
| 4025 | for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) { |
| 4026 | if (mHwModules[module_index]->mHandle == 0) { |
| 4027 | continue; |
| 4028 | } |
| 4029 | for (size_t profile_index = 0; |
| 4030 | profile_index < mHwModules[module_index]->mInputProfiles.size(); |
| 4031 | profile_index++) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4032 | sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4033 | if (profile->supportDevice(device)) { |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 4034 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu", |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4035 | profile_index, module_index); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4036 | profile->clearAudioProfiles(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4037 | } |
| 4038 | } |
| 4039 | } |
| 4040 | } // end disconnect |
| 4041 | |
| 4042 | return NO_ERROR; |
| 4043 | } |
| 4044 | |
| 4045 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4046 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4047 | { |
| 4048 | ALOGV("closeOutput(%d)", output); |
| 4049 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4050 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4051 | if (outputDesc == NULL) { |
| 4052 | ALOGW("closeOutput() unknown output %d", output); |
| 4053 | return; |
| 4054 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4055 | mPolicyMixes.closeOutput(outputDesc); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4056 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4057 | // look for duplicated outputs connected to the output being removed. |
| 4058 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4059 | sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4060 | if (dupOutputDesc->isDuplicated() && |
| 4061 | (dupOutputDesc->mOutput1 == outputDesc || |
| 4062 | dupOutputDesc->mOutput2 == outputDesc)) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4063 | sp<AudioOutputDescriptor> outputDesc2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4064 | if (dupOutputDesc->mOutput1 == outputDesc) { |
| 4065 | outputDesc2 = dupOutputDesc->mOutput2; |
| 4066 | } else { |
| 4067 | outputDesc2 = dupOutputDesc->mOutput1; |
| 4068 | } |
| 4069 | // As all active tracks on duplicated output will be deleted, |
| 4070 | // and as they were also referenced on the other output, the reference |
| 4071 | // count for their stream type must be adjusted accordingly on |
| 4072 | // the other output. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4073 | for (int j = 0; j < AUDIO_STREAM_CNT; j++) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4074 | int refCount = dupOutputDesc->mRefCount[j]; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4075 | outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4076 | } |
| 4077 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); |
| 4078 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); |
| 4079 | |
| 4080 | mpClientInterface->closeOutput(duplicatedOutput); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4081 | removeOutput(duplicatedOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4082 | } |
| 4083 | } |
| 4084 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4085 | nextAudioPortGeneration(); |
| 4086 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4087 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4088 | if (index >= 0) { |
| 4089 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4090 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4091 | mAudioPatches.removeItemsAt(index); |
| 4092 | mpClientInterface->onAudioPatchListUpdate(); |
| 4093 | } |
| 4094 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4095 | AudioParameter param; |
| 4096 | param.add(String8("closing"), String8("true")); |
| 4097 | mpClientInterface->setParameters(output, param.toString()); |
| 4098 | |
| 4099 | mpClientInterface->closeOutput(output); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4100 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4101 | mPreviousOutputs = mOutputs; |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4102 | } |
| 4103 | |
| 4104 | void AudioPolicyManager::closeInput(audio_io_handle_t input) |
| 4105 | { |
| 4106 | ALOGV("closeInput(%d)", input); |
| 4107 | |
| 4108 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
| 4109 | if (inputDesc == NULL) { |
| 4110 | ALOGW("closeInput() unknown input %d", input); |
| 4111 | return; |
| 4112 | } |
| 4113 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4114 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4115 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4116 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->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 | |
| 4124 | mpClientInterface->closeInput(input); |
| 4125 | mInputs.removeItem(input); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4126 | } |
| 4127 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4128 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice( |
| 4129 | audio_devices_t device, |
| 4130 | SwAudioOutputCollection openOutputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4131 | { |
| 4132 | SortedVector<audio_io_handle_t> outputs; |
| 4133 | |
| 4134 | ALOGVV("getOutputsForDevice() device %04x", device); |
| 4135 | for (size_t i = 0; i < openOutputs.size(); i++) { |
| 4136 | ALOGVV("output %d isDuplicated=%d device=%04x", |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4137 | i, openOutputs.valueAt(i)->isDuplicated(), |
| 4138 | openOutputs.valueAt(i)->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4139 | if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) { |
| 4140 | ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i)); |
| 4141 | outputs.add(openOutputs.keyAt(i)); |
| 4142 | } |
| 4143 | } |
| 4144 | return outputs; |
| 4145 | } |
| 4146 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4147 | bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4148 | SortedVector<audio_io_handle_t>& outputs2) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4149 | { |
| 4150 | if (outputs1.size() != outputs2.size()) { |
| 4151 | return false; |
| 4152 | } |
| 4153 | for (size_t i = 0; i < outputs1.size(); i++) { |
| 4154 | if (outputs1[i] != outputs2[i]) { |
| 4155 | return false; |
| 4156 | } |
| 4157 | } |
| 4158 | return true; |
| 4159 | } |
| 4160 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4161 | void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4162 | { |
| 4163 | audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/); |
| 4164 | audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 4165 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs); |
| 4166 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs); |
| 4167 | |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4168 | // also take into account external policy-related changes: add all outputs which are |
| 4169 | // associated with policies in the "before" and "after" output vectors |
| 4170 | ALOGVV("checkOutputForStrategy(): policy related outputs"); |
| 4171 | for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4172 | const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4173 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4174 | srcOutputs.add(desc->mIoHandle); |
| 4175 | ALOGVV(" previous outputs: adding %d", desc->mIoHandle); |
| 4176 | } |
| 4177 | } |
| 4178 | for (size_t i = 0 ; i < mOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4179 | const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4180 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4181 | dstOutputs.add(desc->mIoHandle); |
| 4182 | ALOGVV(" new outputs: adding %d", desc->mIoHandle); |
| 4183 | } |
| 4184 | } |
| 4185 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4186 | if (!vectorsEqual(srcOutputs,dstOutputs)) { |
| 4187 | ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d", |
| 4188 | strategy, srcOutputs[0], dstOutputs[0]); |
| 4189 | // mute strategy while moving tracks from one output to another |
| 4190 | for (size_t i = 0; i < srcOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4191 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4192 | if (isStrategyActive(desc, strategy)) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4193 | setStrategyMute(strategy, true, desc); |
| 4194 | setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4195 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4196 | sp<AudioSourceDescriptor> source = |
| 4197 | getSourceForStrategyOnOutput(srcOutputs[i], strategy); |
| 4198 | if (source != 0){ |
| 4199 | connectAudioSource(source); |
| 4200 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4201 | } |
| 4202 | |
| 4203 | // Move effects associated to this strategy from previous output to new output |
| 4204 | if (strategy == STRATEGY_MEDIA) { |
| 4205 | audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs); |
| 4206 | SortedVector<audio_io_handle_t> moved; |
| 4207 | for (size_t i = 0; i < mEffects.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4208 | sp<EffectDescriptor> effectDesc = mEffects.valueAt(i); |
| 4209 | if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX && |
| 4210 | effectDesc->mIo != fxOutput) { |
| 4211 | if (moved.indexOf(effectDesc->mIo) < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4212 | ALOGV("checkOutputForStrategy() moving effect %d to output %d", |
| 4213 | mEffects.keyAt(i), fxOutput); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4214 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4215 | fxOutput); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4216 | moved.add(effectDesc->mIo); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4217 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4218 | effectDesc->mIo = fxOutput; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4219 | } |
| 4220 | } |
| 4221 | } |
| 4222 | // Move tracks associated to this strategy from previous output to new output |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4223 | for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4224 | if (getStrategy((audio_stream_type_t)i) == strategy) { |
| 4225 | mpClientInterface->invalidateStream((audio_stream_type_t)i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4226 | } |
| 4227 | } |
| 4228 | } |
| 4229 | } |
| 4230 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4231 | void AudioPolicyManager::checkOutputForAllStrategies() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4232 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4233 | 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] | 4234 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4235 | checkOutputForStrategy(STRATEGY_PHONE); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4236 | 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] | 4237 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4238 | checkOutputForStrategy(STRATEGY_SONIFICATION); |
| 4239 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4240 | checkOutputForStrategy(STRATEGY_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4241 | checkOutputForStrategy(STRATEGY_MEDIA); |
| 4242 | checkOutputForStrategy(STRATEGY_DTMF); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4243 | checkOutputForStrategy(STRATEGY_REROUTING); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4244 | } |
| 4245 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4246 | void AudioPolicyManager::checkA2dpSuspend() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4247 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4248 | audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4249 | if (a2dpOutput == 0) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4250 | mA2dpSuspended = false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4251 | return; |
| 4252 | } |
| 4253 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4254 | bool isScoConnected = |
Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 4255 | ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET & |
| 4256 | ~AUDIO_DEVICE_BIT_IN) != 0) || |
| 4257 | ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4258 | // suspend A2DP output if: |
| 4259 | // (NOT already suspended) && |
| 4260 | // ((SCO device is connected && |
| 4261 | // (forced usage for communication || for record is SCO))) || |
| 4262 | // (phone state is ringing || in call) |
| 4263 | // |
| 4264 | // restore A2DP output if: |
| 4265 | // (Already suspended) && |
| 4266 | // ((SCO device is NOT connected || |
| 4267 | // (forced usage NOT for communication && NOT for record is SCO))) && |
| 4268 | // (phone state is NOT ringing && NOT in call) |
| 4269 | // |
| 4270 | if (mA2dpSuspended) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4271 | if ((!isScoConnected || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4272 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) && |
| 4273 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) && |
| 4274 | ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) && |
| 4275 | (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4276 | |
| 4277 | mpClientInterface->restoreOutput(a2dpOutput); |
| 4278 | mA2dpSuspended = false; |
| 4279 | } |
| 4280 | } else { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4281 | if ((isScoConnected && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4282 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) || |
| 4283 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO))) || |
| 4284 | ((mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) || |
| 4285 | (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4286 | |
| 4287 | mpClientInterface->suspendOutput(a2dpOutput); |
| 4288 | mA2dpSuspended = true; |
| 4289 | } |
| 4290 | } |
| 4291 | } |
| 4292 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4293 | audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
| 4294 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4295 | { |
| 4296 | audio_devices_t device = AUDIO_DEVICE_NONE; |
| 4297 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4298 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4299 | if (index >= 0) { |
| 4300 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4301 | if (patchDesc->mUid != mUidCached) { |
| 4302 | ALOGV("getNewOutputDevice() device %08x forced by patch %d", |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4303 | outputDesc->device(), outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4304 | return outputDesc->device(); |
| 4305 | } |
| 4306 | } |
| 4307 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4308 | // 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] | 4309 | // 1: the strategy enforced audible is active and enforced on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4310 | // use device for strategy enforced audible |
| 4311 | // 2: we are in call or the strategy phone is active on the output: |
| 4312 | // use device for strategy phone |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 4313 | // 3: the strategy for enforced audible is active but not enforced on the output: |
| 4314 | // use the device for strategy enforced audible |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4315 | // 4: the strategy sonification is active on the output: |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4316 | // use device for strategy sonification |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4317 | // 5: the strategy accessibility is active on the output: |
| 4318 | // use device for strategy accessibility |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4319 | // 6: the strategy "respectful" sonification is active on the output: |
| 4320 | // use device for strategy "respectful" sonification |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4321 | // 7: the strategy media is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4322 | // use device for strategy media |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4323 | // 8: the strategy DTMF is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4324 | // use device for strategy DTMF |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4325 | // 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] | 4326 | // use device for strategy t-t-s |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4327 | if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4328 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4329 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
| 4330 | } else if (isInCall() || |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4331 | isStrategyActive(outputDesc, STRATEGY_PHONE)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4332 | device = getDeviceForStrategy(STRATEGY_PHONE, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4333 | } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) { |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 4334 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4335 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4336 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4337 | } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) { |
| 4338 | device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4339 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4340 | device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4341 | } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4342 | device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4343 | } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4344 | device = getDeviceForStrategy(STRATEGY_DTMF, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4345 | } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4346 | device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4347 | } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4348 | device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4349 | } |
| 4350 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4351 | ALOGV("getNewOutputDevice() selected device %x", device); |
| 4352 | return device; |
| 4353 | } |
| 4354 | |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 4355 | audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4356 | { |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 4357 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4358 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4359 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4360 | if (index >= 0) { |
| 4361 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4362 | if (patchDesc->mUid != mUidCached) { |
| 4363 | ALOGV("getNewInputDevice() device %08x forced by patch %d", |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4364 | inputDesc->mDevice, inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4365 | return inputDesc->mDevice; |
| 4366 | } |
| 4367 | } |
| 4368 | |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 4369 | audio_devices_t device = getDeviceAndMixForInputSource(inputDesc->inputSource()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4370 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4371 | return device; |
| 4372 | } |
| 4373 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4374 | bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1, |
| 4375 | audio_stream_type_t stream2) { |
| 4376 | return ((stream1 == stream2) || |
| 4377 | ((stream1 == AUDIO_STREAM_ACCESSIBILITY) && (stream2 == AUDIO_STREAM_MUSIC)) || |
| 4378 | ((stream1 == AUDIO_STREAM_MUSIC) && (stream2 == AUDIO_STREAM_ACCESSIBILITY))); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4379 | } |
| 4380 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4381 | uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4382 | return (uint32_t)getStrategy(stream); |
| 4383 | } |
| 4384 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4385 | audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4386 | // By checking the range of stream before calling getStrategy, we avoid |
| 4387 | // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE |
| 4388 | // 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] | 4389 | if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4390 | return AUDIO_DEVICE_NONE; |
| 4391 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4392 | audio_devices_t devices = AUDIO_DEVICE_NONE; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4393 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 4394 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4395 | continue; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4396 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4397 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4398 | audio_devices_t curDevices = |
Eric Laurent | 447a87b | 2016-07-07 17:32:10 -0700 | [diff] [blame] | 4399 | getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4400 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(curDevices, mOutputs); |
| 4401 | for (size_t i = 0; i < outputs.size(); i++) { |
| 4402 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4403 | if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4404 | curDevices |= outputDesc->device(); |
| 4405 | } |
| 4406 | } |
| 4407 | devices |= curDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4408 | } |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4409 | |
| 4410 | /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it |
| 4411 | and doesn't really need to.*/ |
| 4412 | if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) { |
| 4413 | devices |= AUDIO_DEVICE_OUT_SPEAKER; |
| 4414 | devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
| 4415 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4416 | return devices; |
| 4417 | } |
| 4418 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4419 | routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const |
| 4420 | { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4421 | 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] | 4422 | return mEngine->getStrategyForStream(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4423 | } |
| 4424 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4425 | uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) { |
| 4426 | // flags to strategy mapping |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4427 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 4428 | return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER; |
| 4429 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4430 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 4431 | return (uint32_t) STRATEGY_ENFORCED_AUDIBLE; |
| 4432 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4433 | // usage to strategy mapping |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4434 | return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage)); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4435 | } |
| 4436 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4437 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4438 | switch(stream) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4439 | case AUDIO_STREAM_MUSIC: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4440 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
| 4441 | updateDevicesAndOutputs(); |
| 4442 | break; |
| 4443 | default: |
| 4444 | break; |
| 4445 | } |
| 4446 | } |
| 4447 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4448 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 4449 | |
| 4450 | // skip beacon mute management if a dedicated TTS output is available |
| 4451 | if (mTtsOutputAvailable) { |
| 4452 | return 0; |
| 4453 | } |
| 4454 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4455 | switch(event) { |
| 4456 | case STARTING_OUTPUT: |
| 4457 | mBeaconMuteRefCount++; |
| 4458 | break; |
| 4459 | case STOPPING_OUTPUT: |
| 4460 | if (mBeaconMuteRefCount > 0) { |
| 4461 | mBeaconMuteRefCount--; |
| 4462 | } |
| 4463 | break; |
| 4464 | case STARTING_BEACON: |
| 4465 | mBeaconPlayingRefCount++; |
| 4466 | break; |
| 4467 | case STOPPING_BEACON: |
| 4468 | if (mBeaconPlayingRefCount > 0) { |
| 4469 | mBeaconPlayingRefCount--; |
| 4470 | } |
| 4471 | break; |
| 4472 | } |
| 4473 | |
| 4474 | if (mBeaconMuteRefCount > 0) { |
| 4475 | // any playback causes beacon to be muted |
| 4476 | return setBeaconMute(true); |
| 4477 | } else { |
| 4478 | // no other playback: unmute when beacon starts playing, mute when it stops |
| 4479 | return setBeaconMute(mBeaconPlayingRefCount == 0); |
| 4480 | } |
| 4481 | } |
| 4482 | |
| 4483 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { |
| 4484 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", |
| 4485 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); |
| 4486 | // keep track of muted state to avoid repeating mute/unmute operations |
| 4487 | if (mBeaconMuted != mute) { |
| 4488 | // mute/unmute AUDIO_STREAM_TTS on all outputs |
| 4489 | ALOGV("\t muting %d", mute); |
| 4490 | uint32_t maxLatency = 0; |
| 4491 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4492 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4493 | setStreamMute(AUDIO_STREAM_TTS, mute/*on*/, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4494 | desc, |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4495 | 0 /*delay*/, AUDIO_DEVICE_NONE); |
| 4496 | const uint32_t latency = desc->latency() * 2; |
| 4497 | if (latency > maxLatency) { |
| 4498 | maxLatency = latency; |
| 4499 | } |
| 4500 | } |
| 4501 | mBeaconMuted = mute; |
| 4502 | return maxLatency; |
| 4503 | } |
| 4504 | return 0; |
| 4505 | } |
| 4506 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4507 | audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4508 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4509 | { |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4510 | // Routing |
| 4511 | // see if we have an explicit route |
| 4512 | // scan the whole RouteMap, for each entry, convert the stream type to a strategy |
| 4513 | // (getStrategy(stream)). |
| 4514 | // if the strategy from the stream type in the RouteMap is the same as the argument above, |
| 4515 | // and activity count is non-zero |
| 4516 | // the device = the device from the descriptor in the RouteMap, and exit. |
| 4517 | for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) { |
| 4518 | sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4519 | routing_strategy routeStrategy = getStrategy(route->mStreamType); |
| 4520 | if ((routeStrategy == strategy) && route->isActive()) { |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4521 | return route->mDeviceDescriptor->type(); |
| 4522 | } |
| 4523 | } |
| 4524 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4525 | if (fromCache) { |
| 4526 | ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x", |
| 4527 | strategy, mDeviceForStrategy[strategy]); |
| 4528 | return mDeviceForStrategy[strategy]; |
| 4529 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4530 | return mEngine->getDeviceForStrategy(strategy); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4531 | } |
| 4532 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4533 | void AudioPolicyManager::updateDevicesAndOutputs() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4534 | { |
| 4535 | for (int i = 0; i < NUM_STRATEGIES; i++) { |
| 4536 | mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
| 4537 | } |
| 4538 | mPreviousOutputs = mOutputs; |
| 4539 | } |
| 4540 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4541 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4542 | audio_devices_t prevDevice, |
| 4543 | uint32_t delayMs) |
| 4544 | { |
| 4545 | // mute/unmute strategies using an incompatible device combination |
| 4546 | // if muting, wait for the audio in pcm buffer to be drained before proceeding |
| 4547 | // if unmuting, unmute only after the specified delay |
| 4548 | if (outputDesc->isDuplicated()) { |
| 4549 | return 0; |
| 4550 | } |
| 4551 | |
| 4552 | uint32_t muteWaitMs = 0; |
| 4553 | audio_devices_t device = outputDesc->device(); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4554 | bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4555 | |
| 4556 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
| 4557 | audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4558 | curDevice = curDevice & outputDesc->supportedDevices(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4559 | bool mute = shouldMute && (curDevice & device) && (curDevice != device); |
| 4560 | bool doMute = false; |
| 4561 | |
| 4562 | if (mute && !outputDesc->mStrategyMutedByDevice[i]) { |
| 4563 | doMute = true; |
| 4564 | outputDesc->mStrategyMutedByDevice[i] = true; |
| 4565 | } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){ |
| 4566 | doMute = true; |
| 4567 | outputDesc->mStrategyMutedByDevice[i] = false; |
| 4568 | } |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4569 | if (doMute) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4570 | for (size_t j = 0; j < mOutputs.size(); j++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4571 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4572 | // skip output if it does not share any device with current output |
| 4573 | if ((desc->supportedDevices() & outputDesc->supportedDevices()) |
| 4574 | == AUDIO_DEVICE_NONE) { |
| 4575 | continue; |
| 4576 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4577 | ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x)", |
| 4578 | mute ? "muting" : "unmuting", i, curDevice); |
| 4579 | setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4580 | if (isStrategyActive(desc, (routing_strategy)i)) { |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4581 | if (mute) { |
| 4582 | // FIXME: should not need to double latency if volume could be applied |
| 4583 | // immediately by the audioflinger mixer. We must account for the delay |
| 4584 | // between now and the next time the audioflinger thread for this output |
| 4585 | // will process a buffer (which corresponds to one buffer size, |
| 4586 | // usually 1/2 or 1/4 of the latency). |
| 4587 | if (muteWaitMs < desc->latency() * 2) { |
| 4588 | muteWaitMs = desc->latency() * 2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4589 | } |
| 4590 | } |
| 4591 | } |
| 4592 | } |
| 4593 | } |
| 4594 | } |
| 4595 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4596 | // temporary mute output if device selection changes to avoid volume bursts due to |
| 4597 | // different per device volumes |
| 4598 | if (outputDesc->isActive() && (device != prevDevice)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame^] | 4599 | uint32_t tempMuteWaitMs = outputDesc->latency() * 2; |
| 4600 | // temporary mute duration is conservatively set to 4 times the reported latency |
| 4601 | uint32_t tempMuteDurationMs = outputDesc->latency() * 4; |
| 4602 | if (muteWaitMs < tempMuteWaitMs) { |
| 4603 | muteWaitMs = tempMuteWaitMs; |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4604 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame^] | 4605 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4606 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4607 | if (isStrategyActive(outputDesc, (routing_strategy)i)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame^] | 4608 | // make sure that we do not start the temporary mute period too early in case of |
| 4609 | // delayed device change |
| 4610 | setStrategyMute((routing_strategy)i, true, outputDesc, delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4611 | setStrategyMute((routing_strategy)i, false, outputDesc, |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame^] | 4612 | delayMs + tempMuteDurationMs, device); |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4613 | } |
| 4614 | } |
| 4615 | } |
| 4616 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4617 | // wait for the PCM output buffers to empty before proceeding with the rest of the command |
| 4618 | if (muteWaitMs > delayMs) { |
| 4619 | muteWaitMs -= delayMs; |
| 4620 | usleep(muteWaitMs * 1000); |
| 4621 | return muteWaitMs; |
| 4622 | } |
| 4623 | return 0; |
| 4624 | } |
| 4625 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4626 | uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4627 | audio_devices_t device, |
| 4628 | bool force, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4629 | int delayMs, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4630 | audio_patch_handle_t *patchHandle, |
| 4631 | const char* address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4632 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4633 | ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4634 | AudioParameter param; |
| 4635 | uint32_t muteWaitMs; |
| 4636 | |
| 4637 | if (outputDesc->isDuplicated()) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4638 | muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs); |
| 4639 | muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4640 | return muteWaitMs; |
| 4641 | } |
| 4642 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current |
| 4643 | // output profile |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4644 | if ((device != AUDIO_DEVICE_NONE) && |
| 4645 | ((device & outputDesc->supportedDevices()) == 0)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4646 | return 0; |
| 4647 | } |
| 4648 | |
| 4649 | // filter devices according to output selected |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4650 | device = (audio_devices_t)(device & outputDesc->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4651 | |
| 4652 | audio_devices_t prevDevice = outputDesc->mDevice; |
| 4653 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4654 | ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4655 | |
| 4656 | if (device != AUDIO_DEVICE_NONE) { |
| 4657 | outputDesc->mDevice = device; |
| 4658 | } |
| 4659 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs); |
| 4660 | |
| 4661 | // Do not change the routing if: |
Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 4662 | // the requested device is AUDIO_DEVICE_NONE |
| 4663 | // OR the requested device is the same as current device |
| 4664 | // AND force is not specified |
| 4665 | // AND the output is connected by a valid audio patch. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4666 | // Doing this check here allows the caller to call setOutputDevice() without conditions |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4667 | if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && |
| 4668 | !force && |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4669 | outputDesc->getPatchHandle() != 0) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4670 | ALOGV("setOutputDevice() setting same device 0x%04x or null device", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4671 | return muteWaitMs; |
| 4672 | } |
| 4673 | |
| 4674 | ALOGV("setOutputDevice() changing device"); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4675 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4676 | // do the routing |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4677 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4678 | resetOutputDevice(outputDesc, delayMs, NULL); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4679 | } else { |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 4680 | DeviceVector deviceList; |
| 4681 | if ((address == NULL) || (strlen(address) == 0)) { |
| 4682 | deviceList = mAvailableOutputDevices.getDevicesFromType(device); |
| 4683 | } else { |
| 4684 | deviceList = mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address)); |
| 4685 | } |
| 4686 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4687 | if (!deviceList.isEmpty()) { |
| 4688 | struct audio_patch patch; |
| 4689 | outputDesc->toAudioPortConfig(&patch.sources[0]); |
| 4690 | patch.num_sources = 1; |
| 4691 | patch.num_sinks = 0; |
| 4692 | for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) { |
| 4693 | deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4694 | patch.num_sinks++; |
| 4695 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4696 | ssize_t index; |
| 4697 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { |
| 4698 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4699 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4700 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4701 | } |
| 4702 | sp< AudioPatch> patchDesc; |
| 4703 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 4704 | if (index >= 0) { |
| 4705 | patchDesc = mAudioPatches.valueAt(index); |
| 4706 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 4707 | } |
| 4708 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4709 | status_t status = mpClientInterface->createAudioPatch(&patch, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4710 | &afPatchHandle, |
| 4711 | delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4712 | ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d" |
| 4713 | "num_sources %d num_sinks %d", |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4714 | status, afPatchHandle, patch.num_sources, patch.num_sinks); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4715 | if (status == NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4716 | if (index < 0) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 4717 | patchDesc = new AudioPatch(&patch, mUidCached); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4718 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 4719 | } else { |
| 4720 | patchDesc->mPatch = patch; |
| 4721 | } |
| 4722 | patchDesc->mAfPatchHandle = afPatchHandle; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4723 | if (patchHandle) { |
| 4724 | *patchHandle = patchDesc->mHandle; |
| 4725 | } |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4726 | outputDesc->setPatchHandle(patchDesc->mHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4727 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4728 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4729 | } |
| 4730 | } |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 4731 | |
| 4732 | // inform all input as well |
| 4733 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 4734 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4735 | if (!is_virtual_input_device(inputDescriptor->mDevice)) { |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 4736 | AudioParameter inputCmd = AudioParameter(); |
| 4737 | ALOGV("%s: inform input %d of device:%d", __func__, |
| 4738 | inputDescriptor->mIoHandle, device); |
| 4739 | inputCmd.addInt(String8(AudioParameter::keyRouting),device); |
| 4740 | mpClientInterface->setParameters(inputDescriptor->mIoHandle, |
| 4741 | inputCmd.toString(), |
| 4742 | delayMs); |
| 4743 | } |
| 4744 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4745 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4746 | |
| 4747 | // update stream volumes according to new device |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4748 | applyStreamVolumes(outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4749 | |
| 4750 | return muteWaitMs; |
| 4751 | } |
| 4752 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4753 | status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4754 | int delayMs, |
| 4755 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4756 | { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4757 | ssize_t index; |
| 4758 | if (patchHandle) { |
| 4759 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4760 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4761 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4762 | } |
| 4763 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4764 | return INVALID_OPERATION; |
| 4765 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4766 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4767 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4768 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 4769 | outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4770 | removeAudioPatch(patchDesc->mHandle); |
| 4771 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4772 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4773 | return status; |
| 4774 | } |
| 4775 | |
| 4776 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, |
| 4777 | audio_devices_t device, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4778 | bool force, |
| 4779 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4780 | { |
| 4781 | status_t status = NO_ERROR; |
| 4782 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4783 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4784 | if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) { |
| 4785 | inputDesc->mDevice = device; |
| 4786 | |
| 4787 | DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device); |
| 4788 | if (!deviceList.isEmpty()) { |
| 4789 | struct audio_patch patch; |
| 4790 | inputDesc->toAudioPortConfig(&patch.sinks[0]); |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 4791 | // AUDIO_SOURCE_HOTWORD is for internal use only: |
| 4792 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 4793 | if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD && |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 4794 | !inputDesc->isSoundTrigger()) { |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 4795 | patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 4796 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4797 | patch.num_sinks = 1; |
| 4798 | //only one input device for now |
| 4799 | deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4800 | patch.num_sources = 1; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4801 | ssize_t index; |
| 4802 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { |
| 4803 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4804 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4805 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4806 | } |
| 4807 | sp< AudioPatch> patchDesc; |
| 4808 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 4809 | if (index >= 0) { |
| 4810 | patchDesc = mAudioPatches.valueAt(index); |
| 4811 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 4812 | } |
| 4813 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4814 | status_t status = mpClientInterface->createAudioPatch(&patch, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4815 | &afPatchHandle, |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4816 | 0); |
| 4817 | ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d", |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4818 | status, afPatchHandle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4819 | if (status == NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4820 | if (index < 0) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 4821 | patchDesc = new AudioPatch(&patch, mUidCached); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4822 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 4823 | } else { |
| 4824 | patchDesc->mPatch = patch; |
| 4825 | } |
| 4826 | patchDesc->mAfPatchHandle = afPatchHandle; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4827 | if (patchHandle) { |
| 4828 | *patchHandle = patchDesc->mHandle; |
| 4829 | } |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4830 | inputDesc->setPatchHandle(patchDesc->mHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4831 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4832 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4833 | } |
| 4834 | } |
| 4835 | } |
| 4836 | return status; |
| 4837 | } |
| 4838 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4839 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, |
| 4840 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4841 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4842 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4843 | ssize_t index; |
| 4844 | if (patchHandle) { |
| 4845 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4846 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4847 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4848 | } |
| 4849 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4850 | return INVALID_OPERATION; |
| 4851 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4852 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4853 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4854 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 4855 | inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4856 | removeAudioPatch(patchDesc->mHandle); |
| 4857 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4858 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4859 | return status; |
| 4860 | } |
| 4861 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 4862 | sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4863 | String8 address, |
| 4864 | uint32_t& samplingRate, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4865 | audio_format_t& format, |
| 4866 | audio_channel_mask_t& channelMask, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4867 | audio_input_flags_t flags) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4868 | { |
| 4869 | // Choose an input profile based on the requested capture parameters: select the first available |
| 4870 | // profile supporting all requested parameters. |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4871 | // |
| 4872 | // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return |
| 4873 | // the best matching profile, not the first one. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4874 | |
| 4875 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 4876 | { |
| 4877 | if (mHwModules[i]->mHandle == 0) { |
| 4878 | continue; |
| 4879 | } |
| 4880 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) |
| 4881 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4882 | sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j]; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4883 | // profile->log(); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4884 | if (profile->isCompatibleProfile(device, address, samplingRate, |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 4885 | &samplingRate /*updatedSamplingRate*/, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4886 | format, |
| 4887 | &format /*updatedFormat*/, |
| 4888 | channelMask, |
| 4889 | &channelMask /*updatedChannelMask*/, |
| 4890 | (audio_output_flags_t) flags)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4891 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4892 | return profile; |
| 4893 | } |
| 4894 | } |
| 4895 | } |
| 4896 | return NULL; |
| 4897 | } |
| 4898 | |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 4899 | |
| 4900 | audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4901 | AudioMix **policyMix) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4902 | { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4903 | audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
| 4904 | audio_devices_t selectedDeviceFromMix = |
| 4905 | mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4906 | |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4907 | if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) { |
| 4908 | return selectedDeviceFromMix; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4909 | } |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 4910 | return getDeviceForInputSource(inputSource); |
| 4911 | } |
| 4912 | |
| 4913 | audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource) |
| 4914 | { |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 4915 | for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) { |
| 4916 | sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4917 | if (inputSource == route->mSource && route->isActive()) { |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 4918 | return route->mDeviceDescriptor->type(); |
| 4919 | } |
| 4920 | } |
| 4921 | |
| 4922 | return mEngine->getDeviceForInputSource(inputSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4923 | } |
| 4924 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4925 | float AudioPolicyManager::computeVolume(audio_stream_type_t stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4926 | int index, |
| 4927 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4928 | { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 4929 | float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4930 | // if a headset is connected, apply the following rules to ring tones and notifications |
| 4931 | // to avoid sound level bursts in user's ears: |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 4932 | // - always attenuate notifications volume by 6dB |
| 4933 | // - attenuate ring tones volume by 6dB unless music is not playing and |
| 4934 | // speaker is part of the select devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4935 | // - if music is playing, always limit the volume to current music volume, |
| 4936 | // with a minimum threshold at -36dB so that notification is always perceived. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4937 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4938 | if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 4939 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | |
| 4940 | AUDIO_DEVICE_OUT_WIRED_HEADSET | |
| 4941 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) && |
| 4942 | ((stream_strategy == STRATEGY_SONIFICATION) |
| 4943 | || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL) |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4944 | || (stream == AUDIO_STREAM_SYSTEM) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4945 | || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4946 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) && |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4947 | mVolumeCurves->canBeMuted(stream)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4948 | // when the phone is ringing we must consider that music could have been paused just before |
| 4949 | // by the music application and behave as if music was active if the last music track was |
| 4950 | // just stopped |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4951 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4952 | mLimitRingtoneVolume) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 4953 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4954 | audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4955 | float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4956 | mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC, |
| 4957 | musicDevice), |
| 4958 | musicDevice); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4959 | float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ? |
| 4960 | musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB; |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 4961 | if (volumeDB > minVolDB) { |
| 4962 | volumeDB = minVolDB; |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4963 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4964 | } |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 4965 | if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 4966 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) { |
| 4967 | // on A2DP, also ensure notification volume is not too low compared to media when |
| 4968 | // intended to be played |
| 4969 | if ((volumeDB > -96.0f) && |
| 4970 | (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) { |
| 4971 | ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f", |
| 4972 | stream, device, |
| 4973 | volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB); |
| 4974 | volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB; |
| 4975 | } |
| 4976 | } |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 4977 | } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) || |
| 4978 | stream_strategy != STRATEGY_SONIFICATION) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 4979 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4980 | } |
| 4981 | } |
| 4982 | |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 4983 | return volumeDB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4984 | } |
| 4985 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4986 | status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4987 | int index, |
| 4988 | const sp<AudioOutputDescriptor>& outputDesc, |
| 4989 | audio_devices_t device, |
| 4990 | int delayMs, |
| 4991 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4992 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4993 | // do not change actual stream volume if the stream is muted |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4994 | if (outputDesc->mMuteCount[stream] != 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4995 | ALOGVV("checkAndSetVolume() stream %d muted count %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4996 | stream, outputDesc->mMuteCount[stream]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4997 | return NO_ERROR; |
| 4998 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4999 | audio_policy_forced_cfg_t forceUseForComm = |
| 5000 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5001 | // 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] | 5002 | if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) || |
| 5003 | (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5004 | 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] | 5005 | stream, forceUseForComm); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5006 | return INVALID_OPERATION; |
| 5007 | } |
| 5008 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5009 | if (device == AUDIO_DEVICE_NONE) { |
| 5010 | device = outputDesc->device(); |
| 5011 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5012 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5013 | float volumeDb = computeVolume(stream, index, device); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5014 | if (outputDesc->isFixedVolume(device)) { |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5015 | volumeDb = 0.0f; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5016 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5017 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5018 | outputDesc->setVolume(volumeDb, stream, device, delayMs, force); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5019 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5020 | if (stream == AUDIO_STREAM_VOICE_CALL || |
| 5021 | stream == AUDIO_STREAM_BLUETOOTH_SCO) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5022 | float voiceVolume; |
| 5023 | // 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] | 5024 | if (stream == AUDIO_STREAM_VOICE_CALL) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5025 | voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5026 | } else { |
| 5027 | voiceVolume = 1.0; |
| 5028 | } |
| 5029 | |
Eric Laurent | 18fba84 | 2016-03-31 14:41:26 -0700 | [diff] [blame] | 5030 | if (voiceVolume != mLastVoiceVolume) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5031 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); |
| 5032 | mLastVoiceVolume = voiceVolume; |
| 5033 | } |
| 5034 | } |
| 5035 | |
| 5036 | return NO_ERROR; |
| 5037 | } |
| 5038 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5039 | void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc, |
| 5040 | audio_devices_t device, |
| 5041 | int delayMs, |
| 5042 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5043 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5044 | ALOGVV("applyStreamVolumes() for device %08x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5045 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5046 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5047 | checkAndSetVolume((audio_stream_type_t)stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5048 | mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5049 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5050 | device, |
| 5051 | delayMs, |
| 5052 | force); |
| 5053 | } |
| 5054 | } |
| 5055 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5056 | void AudioPolicyManager::setStrategyMute(routing_strategy strategy, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5057 | bool on, |
| 5058 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5059 | int delayMs, |
| 5060 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5061 | { |
Eric Laurent | 72249d5 | 2015-06-08 11:12:15 -0700 | [diff] [blame] | 5062 | ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d", |
| 5063 | strategy, on, outputDesc->getId()); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5064 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5065 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5066 | setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5067 | } |
| 5068 | } |
| 5069 | } |
| 5070 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5071 | void AudioPolicyManager::setStreamMute(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5072 | bool on, |
| 5073 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5074 | int delayMs, |
| 5075 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5076 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5077 | if (device == AUDIO_DEVICE_NONE) { |
| 5078 | device = outputDesc->device(); |
| 5079 | } |
| 5080 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5081 | ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x", |
| 5082 | stream, on, outputDesc->mMuteCount[stream], device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5083 | |
| 5084 | if (on) { |
| 5085 | if (outputDesc->mMuteCount[stream] == 0) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5086 | if (mVolumeCurves->canBeMuted(stream) && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5087 | ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5088 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5089 | checkAndSetVolume(stream, 0, outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5090 | } |
| 5091 | } |
| 5092 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored |
| 5093 | outputDesc->mMuteCount[stream]++; |
| 5094 | } else { |
| 5095 | if (outputDesc->mMuteCount[stream] == 0) { |
| 5096 | ALOGV("setStreamMute() unmuting non muted stream!"); |
| 5097 | return; |
| 5098 | } |
| 5099 | if (--outputDesc->mMuteCount[stream] == 0) { |
| 5100 | checkAndSetVolume(stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5101 | mVolumeCurves->getVolumeIndex(stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5102 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5103 | device, |
| 5104 | delayMs); |
| 5105 | } |
| 5106 | } |
| 5107 | } |
| 5108 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5109 | void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5110 | bool starting, bool stateChange) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5111 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 5112 | if(!hasPrimaryOutput()) { |
| 5113 | return; |
| 5114 | } |
| 5115 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5116 | // if the stream pertains to sonification strategy and we are in call we must |
| 5117 | // mute the stream if it is low visibility. If it is high visibility, we must play a tone |
| 5118 | // in the device used for phone strategy and play the tone if the selected device does not |
| 5119 | // interfere with the device used for phone strategy |
| 5120 | // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as |
| 5121 | // many times as there are active tracks on the output |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5122 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5123 | if ((stream_strategy == STRATEGY_SONIFICATION) || |
| 5124 | ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5125 | sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5126 | ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d", |
| 5127 | stream, starting, outputDesc->mDevice, stateChange); |
| 5128 | if (outputDesc->mRefCount[stream]) { |
| 5129 | int muteCount = 1; |
| 5130 | if (stateChange) { |
| 5131 | muteCount = outputDesc->mRefCount[stream]; |
| 5132 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5133 | if (audio_is_low_visibility(stream)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5134 | ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount); |
| 5135 | for (int i = 0; i < muteCount; i++) { |
| 5136 | setStreamMute(stream, starting, mPrimaryOutput); |
| 5137 | } |
| 5138 | } else { |
| 5139 | ALOGV("handleIncallSonification() high visibility"); |
| 5140 | if (outputDesc->device() & |
| 5141 | getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) { |
| 5142 | ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount); |
| 5143 | for (int i = 0; i < muteCount; i++) { |
| 5144 | setStreamMute(stream, starting, mPrimaryOutput); |
| 5145 | } |
| 5146 | } |
| 5147 | if (starting) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5148 | mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION, |
| 5149 | AUDIO_STREAM_VOICE_CALL); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5150 | } else { |
| 5151 | mpClientInterface->stopTone(); |
| 5152 | } |
| 5153 | } |
| 5154 | } |
| 5155 | } |
| 5156 | } |
| 5157 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5158 | audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr) |
| 5159 | { |
| 5160 | // flags to stream type mapping |
| 5161 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 5162 | return AUDIO_STREAM_ENFORCED_AUDIBLE; |
| 5163 | } |
| 5164 | if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) { |
| 5165 | return AUDIO_STREAM_BLUETOOTH_SCO; |
| 5166 | } |
Jean-Michel Trivi | 79ad438 | 2015-01-29 10:49:39 -0800 | [diff] [blame] | 5167 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 5168 | return AUDIO_STREAM_TTS; |
| 5169 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5170 | |
| 5171 | // usage to stream type mapping |
| 5172 | switch (attr->usage) { |
| 5173 | case AUDIO_USAGE_MEDIA: |
| 5174 | case AUDIO_USAGE_GAME: |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5175 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 5176 | return AUDIO_STREAM_MUSIC; |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5177 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 5178 | return AUDIO_STREAM_ACCESSIBILITY; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5179 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 5180 | return AUDIO_STREAM_SYSTEM; |
| 5181 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 5182 | return AUDIO_STREAM_VOICE_CALL; |
| 5183 | |
| 5184 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 5185 | return AUDIO_STREAM_DTMF; |
| 5186 | |
| 5187 | case AUDIO_USAGE_ALARM: |
| 5188 | return AUDIO_STREAM_ALARM; |
| 5189 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 5190 | return AUDIO_STREAM_RING; |
| 5191 | |
| 5192 | case AUDIO_USAGE_NOTIFICATION: |
| 5193 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 5194 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 5195 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 5196 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 5197 | return AUDIO_STREAM_NOTIFICATION; |
| 5198 | |
| 5199 | case AUDIO_USAGE_UNKNOWN: |
| 5200 | default: |
| 5201 | return AUDIO_STREAM_MUSIC; |
| 5202 | } |
| 5203 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5204 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5205 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) |
| 5206 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5207 | // has flags that map to a strategy? |
| 5208 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { |
| 5209 | return true; |
| 5210 | } |
| 5211 | |
| 5212 | // has known usage? |
| 5213 | switch (paa->usage) { |
| 5214 | case AUDIO_USAGE_UNKNOWN: |
| 5215 | case AUDIO_USAGE_MEDIA: |
| 5216 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 5217 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 5218 | case AUDIO_USAGE_ALARM: |
| 5219 | case AUDIO_USAGE_NOTIFICATION: |
| 5220 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 5221 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 5222 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 5223 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 5224 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 5225 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 5226 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 5227 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 5228 | case AUDIO_USAGE_GAME: |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5229 | case AUDIO_USAGE_VIRTUAL_SOURCE: |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5230 | break; |
| 5231 | default: |
| 5232 | return false; |
| 5233 | } |
| 5234 | return true; |
| 5235 | } |
| 5236 | |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5237 | bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc, |
| 5238 | routing_strategy strategy, uint32_t inPastMs, |
| 5239 | nsecs_t sysTime) const |
| 5240 | { |
| 5241 | if ((sysTime == 0) && (inPastMs != 0)) { |
| 5242 | sysTime = systemTime(); |
| 5243 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5244 | 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] | 5245 | if (((getStrategy((audio_stream_type_t)i) == strategy) || |
| 5246 | (NUM_STRATEGIES == strategy)) && |
| 5247 | outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) { |
| 5248 | return true; |
| 5249 | } |
| 5250 | } |
| 5251 | return false; |
| 5252 | } |
| 5253 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5254 | audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage) |
| 5255 | { |
| 5256 | return mEngine->getForceUse(usage); |
| 5257 | } |
| 5258 | |
| 5259 | bool AudioPolicyManager::isInCall() |
| 5260 | { |
| 5261 | return isStateInCall(mEngine->getPhoneState()); |
| 5262 | } |
| 5263 | |
| 5264 | bool AudioPolicyManager::isStateInCall(int state) |
| 5265 | { |
| 5266 | return is_state_in_call(state); |
| 5267 | } |
| 5268 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5269 | void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc) |
| 5270 | { |
| 5271 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
| 5272 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 5273 | if (sourceDesc->mDevice->equals(deviceDesc)) { |
| 5274 | ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle()); |
| 5275 | stopAudioSource(sourceDesc->getHandle()); |
| 5276 | } |
| 5277 | } |
| 5278 | |
| 5279 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
| 5280 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 5281 | bool release = false; |
| 5282 | for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) { |
| 5283 | const struct audio_port_config *source = &patchDesc->mPatch.sources[j]; |
| 5284 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 5285 | source->ext.device.type == deviceDesc->type()) { |
| 5286 | release = true; |
| 5287 | } |
| 5288 | } |
| 5289 | for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) { |
| 5290 | const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j]; |
| 5291 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && |
| 5292 | sink->ext.device.type == deviceDesc->type()) { |
| 5293 | release = true; |
| 5294 | } |
| 5295 | } |
| 5296 | if (release) { |
| 5297 | ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle); |
| 5298 | releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid); |
| 5299 | } |
| 5300 | } |
| 5301 | } |
| 5302 | |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5303 | // Modify the list of surround sound formats supported. |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5304 | void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) { |
| 5305 | FormatVector &formats = *formatsPtr; |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5306 | // TODO Set this based on Config properties. |
| 5307 | const bool alwaysForceAC3 = true; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5308 | |
| 5309 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5310 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5311 | ALOGD("%s: forced use = %d", __FUNCTION__, forceUse); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5312 | |
| 5313 | // Analyze original support for various formats. |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5314 | bool supportsAC3 = false; |
| 5315 | bool supportsOtherSurround = false; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5316 | bool supportsIEC61937 = false; |
| 5317 | for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) { |
| 5318 | audio_format_t format = formats[formatIndex]; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5319 | switch (format) { |
| 5320 | case AUDIO_FORMAT_AC3: |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5321 | supportsAC3 = true; |
| 5322 | break; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5323 | case AUDIO_FORMAT_E_AC3: |
| 5324 | case AUDIO_FORMAT_DTS: |
| 5325 | case AUDIO_FORMAT_DTS_HD: |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5326 | supportsOtherSurround = true; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5327 | break; |
| 5328 | case AUDIO_FORMAT_IEC61937: |
| 5329 | supportsIEC61937 = true; |
| 5330 | break; |
| 5331 | default: |
| 5332 | break; |
| 5333 | } |
| 5334 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5335 | |
| 5336 | // Modify formats based on surround preferences. |
| 5337 | // If NEVER, remove support for surround formats. |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5338 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
| 5339 | if (supportsAC3 || supportsOtherSurround || supportsIEC61937) { |
| 5340 | // Remove surround sound related formats. |
| 5341 | for (size_t formatIndex = 0; formatIndex < formats.size(); ) { |
| 5342 | audio_format_t format = formats[formatIndex]; |
| 5343 | switch(format) { |
| 5344 | case AUDIO_FORMAT_AC3: |
| 5345 | case AUDIO_FORMAT_E_AC3: |
| 5346 | case AUDIO_FORMAT_DTS: |
| 5347 | case AUDIO_FORMAT_DTS_HD: |
| 5348 | case AUDIO_FORMAT_IEC61937: |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5349 | formats.removeAt(formatIndex); |
| 5350 | break; |
| 5351 | default: |
| 5352 | formatIndex++; // keep it |
| 5353 | break; |
| 5354 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5355 | } |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5356 | supportsAC3 = false; |
| 5357 | supportsOtherSurround = false; |
| 5358 | supportsIEC61937 = false; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5359 | } |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5360 | } else { // AUTO or ALWAYS |
| 5361 | // Most TVs support AC3 even if they do not report it in the EDID. |
| 5362 | if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)) |
| 5363 | && !supportsAC3) { |
| 5364 | formats.add(AUDIO_FORMAT_AC3); |
| 5365 | supportsAC3 = true; |
| 5366 | } |
| 5367 | |
| 5368 | // If ALWAYS, add support for raw surround formats if all are missing. |
| 5369 | // This assumes that if any of these formats are reported by the HAL |
| 5370 | // then the report is valid and should not be modified. |
| 5371 | if ((forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) |
| 5372 | && !supportsOtherSurround) { |
| 5373 | formats.add(AUDIO_FORMAT_E_AC3); |
| 5374 | formats.add(AUDIO_FORMAT_DTS); |
| 5375 | formats.add(AUDIO_FORMAT_DTS_HD); |
| 5376 | supportsOtherSurround = true; |
| 5377 | } |
| 5378 | |
| 5379 | // Add support for IEC61937 if any raw surround supported. |
| 5380 | // The HAL could do this but add it here, just in case. |
| 5381 | if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) { |
| 5382 | formats.add(AUDIO_FORMAT_IEC61937); |
| 5383 | supportsIEC61937 = true; |
| 5384 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5385 | } |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5386 | } |
| 5387 | |
| 5388 | // Modify the list of channel masks supported. |
| 5389 | void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) { |
| 5390 | ChannelsVector &channelMasks = *channelMasksPtr; |
| 5391 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5392 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
| 5393 | |
| 5394 | // If NEVER, then remove support for channelMasks > stereo. |
| 5395 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
| 5396 | for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) { |
| 5397 | audio_channel_mask_t channelMask = channelMasks[maskIndex]; |
| 5398 | if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) { |
| 5399 | ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask); |
| 5400 | channelMasks.removeAt(maskIndex); |
| 5401 | } else { |
| 5402 | maskIndex++; |
| 5403 | } |
| 5404 | } |
| 5405 | // If ALWAYS, then make sure we at least support 5.1 |
| 5406 | } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { |
| 5407 | bool supports5dot1 = false; |
| 5408 | // Are there any channel masks that can be considered "surround"? |
| 5409 | for (size_t maskIndex = 0; maskIndex < channelMasks.size(); maskIndex++) { |
| 5410 | audio_channel_mask_t channelMask = channelMasks[maskIndex]; |
| 5411 | if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) { |
| 5412 | supports5dot1 = true; |
| 5413 | break; |
| 5414 | } |
| 5415 | } |
| 5416 | // If not then add 5.1 support. |
| 5417 | if (!supports5dot1) { |
| 5418 | channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1); |
| 5419 | ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__); |
| 5420 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5421 | } |
| 5422 | } |
| 5423 | |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5424 | void AudioPolicyManager::updateAudioProfiles(audio_devices_t device, |
| 5425 | audio_io_handle_t ioHandle, |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5426 | AudioProfileVector &profiles) |
| 5427 | { |
| 5428 | String8 reply; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5429 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5430 | // Format MUST be checked first to update the list of AudioProfile |
| 5431 | if (profiles.hasDynamicFormat()) { |
| 5432 | reply = mpClientInterface->getParameters(ioHandle, |
| 5433 | String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS)); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5434 | ALOGV("%s: supported formats %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5435 | AudioParameter repliedParameters(reply); |
| 5436 | if (repliedParameters.get( |
| 5437 | String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS), reply) != NO_ERROR) { |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5438 | ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__); |
| 5439 | return; |
| 5440 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5441 | FormatVector formats = formatsFromString(reply.string()); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5442 | if (device == AUDIO_DEVICE_OUT_HDMI) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5443 | filterSurroundFormats(&formats); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5444 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5445 | profiles.setFormats(formats); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5446 | } |
| 5447 | const FormatVector &supportedFormats = profiles.getSupportedFormats(); |
| 5448 | |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5449 | for (size_t formatIndex = 0; formatIndex < supportedFormats.size(); formatIndex++) { |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5450 | audio_format_t format = supportedFormats[formatIndex]; |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5451 | ChannelsVector channelMasks; |
| 5452 | SampleRateVector samplingRates; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5453 | AudioParameter requestedParameters; |
| 5454 | requestedParameters.addInt(String8(AUDIO_PARAMETER_STREAM_FORMAT), format); |
| 5455 | |
| 5456 | if (profiles.hasDynamicRateFor(format)) { |
| 5457 | reply = mpClientInterface->getParameters(ioHandle, |
| 5458 | requestedParameters.toString() + ";" + |
| 5459 | AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES); |
| 5460 | ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5461 | AudioParameter repliedParameters(reply); |
| 5462 | if (repliedParameters.get( |
| 5463 | String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES), reply) == NO_ERROR) { |
| 5464 | samplingRates = samplingRatesFromString(reply.string()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5465 | } |
| 5466 | } |
| 5467 | if (profiles.hasDynamicChannelsFor(format)) { |
| 5468 | reply = mpClientInterface->getParameters(ioHandle, |
| 5469 | requestedParameters.toString() + ";" + |
| 5470 | AUDIO_PARAMETER_STREAM_SUP_CHANNELS); |
| 5471 | ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5472 | AudioParameter repliedParameters(reply); |
| 5473 | if (repliedParameters.get( |
| 5474 | String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS), reply) == NO_ERROR) { |
| 5475 | channelMasks = channelMasksFromString(reply.string()); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5476 | if (device == AUDIO_DEVICE_OUT_HDMI) { |
| 5477 | filterSurroundChannelMasks(&channelMasks); |
| 5478 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5479 | } |
| 5480 | } |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5481 | profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates)); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5482 | } |
| 5483 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5484 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5485 | }; // namespace android |