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 | |
| 52 | // ---------------------------------------------------------------------------- |
| 53 | // AudioPolicyInterface implementation |
| 54 | // ---------------------------------------------------------------------------- |
| 55 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 56 | status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 57 | audio_policy_dev_state_t state, |
| 58 | const char *device_address, |
| 59 | const char *device_name) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 60 | { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 61 | return setDeviceConnectionStateInt(device, state, device_address, device_name); |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device, |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 65 | audio_policy_dev_state_t state, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 66 | const char *device_address, |
| 67 | const char *device_name) |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 68 | { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 69 | ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s", |
| 70 | - device, state, device_address, device_name); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 71 | |
| 72 | // connect/disconnect only 1 device at a time |
| 73 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; |
| 74 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 75 | sp<DeviceDescriptor> devDesc = |
| 76 | mHwModules.getDeviceDescriptor(device, device_address, device_name); |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 77 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 78 | // handle output devices |
| 79 | if (audio_is_output_device(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 80 | SortedVector <audio_io_handle_t> outputs; |
| 81 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 82 | ssize_t index = mAvailableOutputDevices.indexOf(devDesc); |
| 83 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 84 | // save a copy of the opened output descriptors before any output is opened or closed |
| 85 | // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies() |
| 86 | mPreviousOutputs = mOutputs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 87 | switch (state) |
| 88 | { |
| 89 | // handle output device connection |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 90 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 91 | if (index >= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 92 | ALOGW("setDeviceConnectionState() device already connected: %x", device); |
| 93 | return INVALID_OPERATION; |
| 94 | } |
| 95 | ALOGV("setDeviceConnectionState() connecting device %x", device); |
| 96 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 97 | // register new device as available |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 98 | index = mAvailableOutputDevices.add(devDesc); |
| 99 | if (index >= 0) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 100 | sp<HwModule> module = mHwModules.getModuleForDevice(device); |
Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 101 | if (module == 0) { |
| 102 | ALOGD("setDeviceConnectionState() could not find HW module for device %08x", |
| 103 | device); |
| 104 | mAvailableOutputDevices.remove(devDesc); |
| 105 | return INVALID_OPERATION; |
| 106 | } |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 107 | mAvailableOutputDevices[index]->attach(module); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 108 | } else { |
| 109 | return NO_MEMORY; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 112 | if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 113 | mAvailableOutputDevices.remove(devDesc); |
| 114 | return INVALID_OPERATION; |
| 115 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 116 | // Propagate device availability to Engine |
| 117 | mEngine->setDeviceConnectionState(devDesc, state); |
| 118 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 119 | // outputs should never be empty here |
| 120 | ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():" |
| 121 | "checkOutputsForDevice() returned no outputs but status OK"); |
| 122 | ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs", |
| 123 | outputs.size()); |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 124 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 125 | // Send connect to HALs |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 126 | AudioParameter param = AudioParameter(devDesc->mAddress); |
| 127 | param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device); |
| 128 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 129 | |
| 130 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 131 | // handle output device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 132 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 133 | if (index < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 134 | ALOGW("setDeviceConnectionState() device not connected: %x", device); |
| 135 | return INVALID_OPERATION; |
| 136 | } |
| 137 | |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 138 | ALOGV("setDeviceConnectionState() disconnecting output device %x", device); |
| 139 | |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 140 | // Send Disconnect to HALs |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 141 | AudioParameter param = AudioParameter(devDesc->mAddress); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 142 | param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device); |
| 143 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 144 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 145 | // remove device from available output devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 146 | mAvailableOutputDevices.remove(devDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 147 | |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 148 | checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 149 | |
| 150 | // Propagate device availability to Engine |
| 151 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 152 | } break; |
| 153 | |
| 154 | default: |
| 155 | ALOGE("setDeviceConnectionState() invalid state: %x", state); |
| 156 | return BAD_VALUE; |
| 157 | } |
| 158 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 159 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP |
| 160 | // output is suspended before any tracks are moved to it |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 161 | checkA2dpSuspend(); |
| 162 | checkOutputForAllStrategies(); |
| 163 | // outputs must be closed after checkOutputForAllStrategies() is executed |
| 164 | if (!outputs.isEmpty()) { |
| 165 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 166 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 167 | // close unused outputs after device disconnection or direct outputs that have been |
| 168 | // opened by checkOutputsForDevice() to query dynamic parameters |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 169 | if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 170 | (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) && |
| 171 | (desc->mDirectOpenCount == 0))) { |
| 172 | closeOutput(outputs[i]); |
| 173 | } |
| 174 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 175 | // check again after closing A2DP output to reset mA2dpSuspended if needed |
| 176 | checkA2dpSuspend(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | updateDevicesAndOutputs(); |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 180 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 181 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 182 | updateCallRouting(newDevice); |
| 183 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 184 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 185 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 186 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) { |
| 187 | audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 188 | // do not force device change on duplicated output because if device is 0, it will |
| 189 | // also force a device 0 for the two outputs it is duplicated to which may override |
| 190 | // a valid device selection on those outputs. |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 191 | bool force = !desc->isDuplicated() |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 192 | && (!device_distinguishes_on_address(device) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 193 | // always force when disconnecting (a non-duplicated device) |
| 194 | || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 195 | setOutputDevice(desc, newDevice, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 196 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 199 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
| 200 | cleanUpForDevice(devDesc); |
| 201 | } |
| 202 | |
Eric Laurent | 72aa32f | 2014-05-30 18:51:48 -0700 | [diff] [blame] | 203 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | b71e58b | 2014-05-29 16:08:11 -0700 | [diff] [blame] | 204 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 205 | } // end if is output device |
| 206 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 207 | // handle input devices |
| 208 | if (audio_is_input_device(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 209 | SortedVector <audio_io_handle_t> inputs; |
| 210 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 211 | ssize_t index = mAvailableInputDevices.indexOf(devDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 212 | switch (state) |
| 213 | { |
| 214 | // handle input device connection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 215 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 216 | if (index >= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 217 | ALOGW("setDeviceConnectionState() device already connected: %d", device); |
| 218 | return INVALID_OPERATION; |
| 219 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 220 | sp<HwModule> module = mHwModules.getModuleForDevice(device); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 221 | if (module == NULL) { |
| 222 | ALOGW("setDeviceConnectionState(): could not find HW module for device %08x", |
| 223 | device); |
| 224 | return INVALID_OPERATION; |
| 225 | } |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 226 | if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 227 | return INVALID_OPERATION; |
| 228 | } |
| 229 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 230 | index = mAvailableInputDevices.add(devDesc); |
| 231 | if (index >= 0) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 232 | mAvailableInputDevices[index]->attach(module); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 233 | } else { |
| 234 | return NO_MEMORY; |
| 235 | } |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 236 | |
| 237 | // Set connect to HALs |
| 238 | AudioParameter param = AudioParameter(devDesc->mAddress); |
| 239 | param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device); |
| 240 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 241 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 242 | // Propagate device availability to Engine |
| 243 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 244 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 245 | |
| 246 | // handle input device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 247 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 248 | if (index < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 249 | ALOGW("setDeviceConnectionState() device not connected: %d", device); |
| 250 | return INVALID_OPERATION; |
| 251 | } |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 252 | |
| 253 | ALOGV("setDeviceConnectionState() disconnecting input device %x", device); |
| 254 | |
| 255 | // Set Disconnect to HALs |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 256 | AudioParameter param = AudioParameter(devDesc->mAddress); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 257 | param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device); |
| 258 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 259 | |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 260 | checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 261 | mAvailableInputDevices.remove(devDesc); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 262 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 263 | // Propagate device availability to Engine |
| 264 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 265 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 266 | |
| 267 | default: |
| 268 | ALOGE("setDeviceConnectionState() invalid state: %x", state); |
| 269 | return BAD_VALUE; |
| 270 | } |
| 271 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 272 | closeAllInputs(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 273 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 274 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 275 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 276 | updateCallRouting(newDevice); |
| 277 | } |
| 278 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 279 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
| 280 | cleanUpForDevice(devDesc); |
| 281 | } |
| 282 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 283 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 284 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 285 | } // end if is input device |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 286 | |
| 287 | ALOGW("setDeviceConnectionState() invalid device: %x", device); |
| 288 | return BAD_VALUE; |
| 289 | } |
| 290 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 291 | audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 292 | const char *device_address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 293 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 294 | sp<DeviceDescriptor> devDesc = mHwModules.getDeviceDescriptor(device, device_address, ""); |
| 295 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 296 | DeviceVector *deviceVector; |
| 297 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 298 | if (audio_is_output_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 299 | deviceVector = &mAvailableOutputDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 300 | } else if (audio_is_input_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 301 | deviceVector = &mAvailableInputDevices; |
| 302 | } else { |
| 303 | ALOGW("getDeviceConnectionState() invalid device type %08x", device); |
| 304 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 305 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 306 | return deviceVector->getDeviceConnectionState(devDesc); |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 307 | } |
| 308 | |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 309 | void AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, int delayMs) |
| 310 | { |
| 311 | bool createTxPatch = false; |
| 312 | struct audio_patch patch; |
| 313 | patch.num_sources = 1; |
| 314 | patch.num_sinks = 1; |
| 315 | status_t status; |
| 316 | audio_patch_handle_t afPatchHandle; |
| 317 | DeviceVector deviceList; |
| 318 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 319 | if(!hasPrimaryOutput()) { |
| 320 | return; |
| 321 | } |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 322 | audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 323 | ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice); |
| 324 | |
| 325 | // release existing RX patch if any |
| 326 | if (mCallRxPatch != 0) { |
| 327 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 328 | mCallRxPatch.clear(); |
| 329 | } |
| 330 | // release TX patch if any |
| 331 | if (mCallTxPatch != 0) { |
| 332 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 333 | mCallTxPatch.clear(); |
| 334 | } |
| 335 | |
| 336 | // If the RX device is on the primary HW module, then use legacy routing method for voice calls |
| 337 | // via setOutputDevice() on primary output. |
| 338 | // Otherwise, create two audio patches for TX and RX path. |
| 339 | if (availablePrimaryOutputDevices() & rxDevice) { |
| 340 | setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs); |
| 341 | // If the TX device is also on the primary HW module, setOutputDevice() will take care |
| 342 | // of it due to legacy implementation. If not, create a patch. |
| 343 | if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN) |
| 344 | == AUDIO_DEVICE_NONE) { |
| 345 | createTxPatch = true; |
| 346 | } |
| 347 | } else { |
| 348 | // create RX path audio patch |
| 349 | deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice); |
| 350 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 351 | "updateCallRouting() selected device not in output device list"); |
| 352 | sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0); |
| 353 | deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX); |
| 354 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 355 | "updateCallRouting() no telephony RX device"); |
| 356 | sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0); |
| 357 | |
| 358 | rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]); |
| 359 | rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]); |
| 360 | |
| 361 | // request to reuse existing output stream if one is already opened to reach the RX device |
| 362 | SortedVector<audio_io_handle_t> outputs = |
| 363 | getOutputsForDevice(rxDevice, mOutputs); |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 364 | audio_io_handle_t output = selectOutput(outputs, |
| 365 | AUDIO_OUTPUT_FLAG_NONE, |
| 366 | AUDIO_FORMAT_INVALID); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 367 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 368 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 369 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 370 | "updateCallRouting() RX device output is duplicated"); |
| 371 | outputDesc->toAudioPortConfig(&patch.sources[1]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 372 | patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 373 | patch.num_sources = 2; |
| 374 | } |
| 375 | |
| 376 | afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 377 | status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0); |
| 378 | ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch", |
| 379 | status); |
| 380 | if (status == NO_ERROR) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 381 | mCallRxPatch = new AudioPatch(&patch, mUidCached); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 382 | mCallRxPatch->mAfPatchHandle = afPatchHandle; |
| 383 | mCallRxPatch->mUid = mUidCached; |
| 384 | } |
| 385 | createTxPatch = true; |
| 386 | } |
| 387 | if (createTxPatch) { |
| 388 | |
| 389 | struct audio_patch patch; |
| 390 | patch.num_sources = 1; |
| 391 | patch.num_sinks = 1; |
| 392 | deviceList = mAvailableInputDevices.getDevicesFromType(txDevice); |
| 393 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 394 | "updateCallRouting() selected device not in input device list"); |
| 395 | sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0); |
| 396 | txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]); |
| 397 | deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX); |
| 398 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 399 | "updateCallRouting() no telephony TX device"); |
| 400 | sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0); |
| 401 | txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]); |
| 402 | |
| 403 | SortedVector<audio_io_handle_t> outputs = |
| 404 | getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs); |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 405 | audio_io_handle_t output = selectOutput(outputs, |
| 406 | AUDIO_OUTPUT_FLAG_NONE, |
| 407 | AUDIO_FORMAT_INVALID); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 408 | // request to reuse existing output stream if one is already opened to reach the TX |
| 409 | // path output device |
| 410 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 411 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 412 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 413 | "updateCallRouting() RX device output is duplicated"); |
| 414 | outputDesc->toAudioPortConfig(&patch.sources[1]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 415 | patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 416 | patch.num_sources = 2; |
| 417 | } |
| 418 | |
Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 419 | // terminate active capture if on the same HW module as the call TX source device |
| 420 | // FIXME: would be better to refine to only inputs whose profile connects to the |
| 421 | // call TX device but this information is not in the audio patch and logic here must be |
| 422 | // symmetric to the one in startInput() |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 423 | audio_io_handle_t activeInput = mInputs.getActiveInput(); |
| 424 | if (activeInput != 0) { |
| 425 | sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput); |
| 426 | if (activeDesc->getModuleHandle() == txSourceDeviceDesc->getModuleHandle()) { |
| 427 | //FIXME: consider all active sessions |
| 428 | AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions(); |
| 429 | audio_session_t activeSession = activeSessions.keyAt(0); |
| 430 | stopInput(activeInput, activeSession); |
| 431 | releaseInput(activeInput, activeSession); |
Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 435 | afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 436 | status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0); |
| 437 | ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch", |
| 438 | status); |
| 439 | if (status == NO_ERROR) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 440 | mCallTxPatch = new AudioPatch(&patch, mUidCached); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 441 | mCallTxPatch->mAfPatchHandle = afPatchHandle; |
| 442 | mCallTxPatch->mUid = mUidCached; |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 447 | void AudioPolicyManager::setPhoneState(audio_mode_t state) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 448 | { |
| 449 | ALOGV("setPhoneState() state %d", state); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 450 | // store previous phone state for management of sonification strategy below |
| 451 | int oldState = mEngine->getPhoneState(); |
| 452 | |
| 453 | if (mEngine->setPhoneState(state) != NO_ERROR) { |
| 454 | ALOGW("setPhoneState() invalid or same state %d", state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 455 | return; |
| 456 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 457 | /// Opens: can these line be executed after the switch of volume curves??? |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 458 | // if leaving call state, handle special case of active streams |
| 459 | // pertaining to sonification strategy see handleIncallSonification() |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 460 | if (isStateInCall(oldState)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 461 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 462 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 463 | handleIncallSonification((audio_stream_type_t)stream, false, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 464 | } |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 465 | |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 466 | // force reevaluating accessibility routing when call stops |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 467 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 468 | } |
| 469 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 470 | /** |
| 471 | * Switching to or from incall state or switching between telephony and VoIP lead to force |
| 472 | * routing command. |
| 473 | */ |
| 474 | bool force = ((is_state_in_call(oldState) != is_state_in_call(state)) |
| 475 | || (is_state_in_call(state) && (state != oldState))); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 476 | |
| 477 | // check for device and output changes triggered by new phone state |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 478 | checkA2dpSuspend(); |
| 479 | checkOutputForAllStrategies(); |
| 480 | updateDevicesAndOutputs(); |
| 481 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 482 | int delayMs = 0; |
| 483 | if (isStateInCall(state)) { |
| 484 | nsecs_t sysTime = systemTime(); |
| 485 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 486 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 487 | // mute media and sonification strategies and delay device switch by the largest |
| 488 | // latency of any output where either strategy is active. |
| 489 | // 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] | 490 | if ((isStrategyActive(desc, STRATEGY_MEDIA, |
| 491 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 492 | sysTime) || |
| 493 | isStrategyActive(desc, STRATEGY_SONIFICATION, |
| 494 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 495 | sysTime)) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 496 | (delayMs < (int)desc->latency()*2)) { |
| 497 | delayMs = desc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 498 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 499 | setStrategyMute(STRATEGY_MEDIA, true, desc); |
| 500 | setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 501 | getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/)); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 502 | setStrategyMute(STRATEGY_SONIFICATION, true, desc); |
| 503 | setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 504 | getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/)); |
| 505 | } |
| 506 | } |
| 507 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 508 | if (hasPrimaryOutput()) { |
| 509 | // Note that despite the fact that getNewOutputDevice() is called on the primary output, |
| 510 | // the device returned is not necessarily reachable via this output |
| 511 | audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 512 | // force routing command to audio hardware when ending call |
| 513 | // even if no device change is needed |
| 514 | if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) { |
| 515 | rxDevice = mPrimaryOutput->device(); |
| 516 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 517 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 518 | if (state == AUDIO_MODE_IN_CALL) { |
| 519 | updateCallRouting(rxDevice, delayMs); |
| 520 | } else if (oldState == AUDIO_MODE_IN_CALL) { |
| 521 | if (mCallRxPatch != 0) { |
| 522 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 523 | mCallRxPatch.clear(); |
| 524 | } |
| 525 | if (mCallTxPatch != 0) { |
| 526 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 527 | mCallTxPatch.clear(); |
| 528 | } |
| 529 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
| 530 | } else { |
| 531 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 532 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 533 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 534 | // if entering in call state, handle special case of active streams |
| 535 | // pertaining to sonification strategy see handleIncallSonification() |
| 536 | if (isStateInCall(state)) { |
| 537 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 538 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 539 | handleIncallSonification((audio_stream_type_t)stream, true, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 540 | } |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 541 | |
| 542 | // force reevaluating accessibility routing when call starts |
| 543 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | // 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] | 547 | if (state == AUDIO_MODE_RINGTONE && |
| 548 | isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 549 | mLimitRingtoneVolume = true; |
| 550 | } else { |
| 551 | mLimitRingtoneVolume = false; |
| 552 | } |
| 553 | } |
| 554 | |
Jean-Michel Trivi | 887a9ed | 2015-03-31 18:02:24 -0700 | [diff] [blame] | 555 | audio_mode_t AudioPolicyManager::getPhoneState() { |
| 556 | return mEngine->getPhoneState(); |
| 557 | } |
| 558 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 559 | void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 560 | audio_policy_forced_cfg_t config) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 561 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 562 | ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 563 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 564 | if (mEngine->setForceUse(usage, config) != NO_ERROR) { |
| 565 | ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage); |
| 566 | return; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 567 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 568 | bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) || |
| 569 | (usage == AUDIO_POLICY_FORCE_FOR_DOCK) || |
| 570 | (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 571 | |
| 572 | // check for device and output changes triggered by new force usage |
| 573 | checkA2dpSuspend(); |
| 574 | checkOutputForAllStrategies(); |
| 575 | updateDevicesAndOutputs(); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 576 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 577 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 578 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/); |
| 579 | updateCallRouting(newDevice); |
| 580 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 581 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 582 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
| 583 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/); |
| 584 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) { |
| 585 | setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE)); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 586 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 587 | if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 588 | applyStreamVolumes(outputDesc, newDevice, 0, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 592 | audio_io_handle_t activeInput = mInputs.getActiveInput(); |
| 593 | if (activeInput != 0) { |
| 594 | sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput); |
| 595 | audio_devices_t newDevice = getNewInputDevice(activeInput); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 596 | // 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] | 597 | if (activeDesc->mProfile->getSupportedDevices().types() & (newDevice & ~AUDIO_DEVICE_BIT_IN)) { |
| 598 | setInputDevice(activeInput, newDevice); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 599 | } else { |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 600 | closeInput(activeInput); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 601 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 602 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 603 | } |
| 604 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 605 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 606 | { |
| 607 | ALOGV("setSystemProperty() property %s, value %s", property, value); |
| 608 | } |
| 609 | |
| 610 | // Find a direct output profile compatible with the parameters passed, even if the input flags do |
| 611 | // not explicitly request a direct output |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 612 | sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput( |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 613 | audio_devices_t device, |
| 614 | uint32_t samplingRate, |
| 615 | audio_format_t format, |
| 616 | audio_channel_mask_t channelMask, |
| 617 | audio_output_flags_t flags) |
| 618 | { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 619 | // only retain flags that will drive the direct output profile selection |
| 620 | // if explicitly requested |
| 621 | static const uint32_t kRelevantFlags = |
| 622 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
| 623 | flags = |
| 624 | (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); |
| 625 | |
| 626 | sp<IOProfile> profile; |
| 627 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 628 | for (size_t i = 0; i < mHwModules.size(); i++) { |
| 629 | if (mHwModules[i]->mHandle == 0) { |
| 630 | continue; |
| 631 | } |
| 632 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 633 | sp<IOProfile> curProfile = mHwModules[i]->mOutputProfiles[j]; |
| 634 | if (!curProfile->isCompatibleProfile(device, String8(""), |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 635 | samplingRate, NULL /*updatedSamplingRate*/, |
| 636 | format, NULL /*updatedFormat*/, |
| 637 | channelMask, NULL /*updatedChannelMask*/, |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 638 | flags)) { |
| 639 | continue; |
| 640 | } |
| 641 | // reject profiles not corresponding to a device currently available |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 642 | if ((mAvailableOutputDevices.types() & curProfile->getSupportedDevicesType()) == 0) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 643 | continue; |
| 644 | } |
| 645 | // 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] | 646 | if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 647 | continue; |
| 648 | } |
| 649 | profile = curProfile; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 650 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 651 | break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 652 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 653 | } |
| 654 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 655 | return profile; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 656 | } |
| 657 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 658 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 659 | uint32_t samplingRate, |
| 660 | audio_format_t format, |
| 661 | audio_channel_mask_t channelMask, |
| 662 | audio_output_flags_t flags, |
| 663 | const audio_offload_info_t *offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 664 | { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 665 | routing_strategy strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 666 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 667 | ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x", |
| 668 | device, stream, samplingRate, format, channelMask, flags); |
| 669 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 670 | return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE, |
| 671 | stream, samplingRate,format, channelMask, |
| 672 | flags, offloadInfo); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 673 | } |
| 674 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 675 | status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, |
| 676 | audio_io_handle_t *output, |
| 677 | audio_session_t session, |
| 678 | audio_stream_type_t *stream, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 679 | uid_t uid, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 680 | uint32_t samplingRate, |
| 681 | audio_format_t format, |
| 682 | audio_channel_mask_t channelMask, |
| 683 | audio_output_flags_t flags, |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 684 | audio_port_handle_t selectedDeviceId, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 685 | const audio_offload_info_t *offloadInfo) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 686 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 687 | audio_attributes_t attributes; |
| 688 | if (attr != NULL) { |
| 689 | if (!isValidAttributes(attr)) { |
| 690 | ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]", |
| 691 | attr->usage, attr->content_type, attr->flags, |
| 692 | attr->tags); |
| 693 | return BAD_VALUE; |
| 694 | } |
| 695 | attributes = *attr; |
| 696 | } else { |
| 697 | if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) { |
| 698 | ALOGE("getOutputForAttr(): invalid stream type"); |
| 699 | return BAD_VALUE; |
| 700 | } |
| 701 | stream_type_to_audio_attributes(*stream, &attributes); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 702 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 703 | sp<SwAudioOutputDescriptor> desc; |
Jean-Michel Trivi | e8deced | 2016-02-11 12:50:39 -0800 | [diff] [blame] | 704 | if (mPolicyMixes.getOutputForAttr(attributes, uid, desc) == NO_ERROR) { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 705 | ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr"); |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 706 | if (!audio_has_proportional_frames(format)) { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 707 | return BAD_VALUE; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 708 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 709 | *stream = streamTypefromAttributesInt(&attributes); |
| 710 | *output = desc->mIoHandle; |
| 711 | ALOGV("getOutputForAttr() returns output %d", *output); |
| 712 | return NO_ERROR; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 713 | } |
| 714 | if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 715 | ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE"); |
| 716 | return BAD_VALUE; |
| 717 | } |
| 718 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 719 | ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x" |
| 720 | " session %d selectedDeviceId %d", |
| 721 | attributes.usage, attributes.content_type, attributes.tags, attributes.flags, |
| 722 | session, selectedDeviceId); |
| 723 | |
| 724 | *stream = streamTypefromAttributesInt(&attributes); |
| 725 | |
| 726 | // Explicit routing? |
| 727 | sp<DeviceDescriptor> deviceDesc; |
| 728 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { |
| 729 | if (mAvailableOutputDevices[i]->getId() == selectedDeviceId) { |
| 730 | deviceDesc = mAvailableOutputDevices[i]; |
| 731 | break; |
| 732 | } |
| 733 | } |
| 734 | mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 735 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 736 | routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 737 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 738 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 739 | if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) { |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 740 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
| 741 | } |
| 742 | |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 743 | 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] | 744 | device, samplingRate, format, channelMask, flags); |
| 745 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 746 | *output = getOutputForDevice(device, session, *stream, |
| 747 | samplingRate, format, channelMask, |
| 748 | flags, offloadInfo); |
| 749 | if (*output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 750 | mOutputRoutes.removeRoute(session); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 751 | return INVALID_OPERATION; |
| 752 | } |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 753 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 754 | return NO_ERROR; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | audio_io_handle_t AudioPolicyManager::getOutputForDevice( |
| 758 | audio_devices_t device, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 759 | audio_session_t session __unused, |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 760 | audio_stream_type_t stream, |
| 761 | uint32_t samplingRate, |
| 762 | audio_format_t format, |
| 763 | audio_channel_mask_t channelMask, |
| 764 | audio_output_flags_t flags, |
| 765 | const audio_offload_info_t *offloadInfo) |
| 766 | { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 767 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 768 | uint32_t latency = 0; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 769 | status_t status; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 770 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 771 | #ifdef AUDIO_POLICY_TEST |
| 772 | if (mCurOutput != 0) { |
| 773 | ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d", |
| 774 | mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput); |
| 775 | |
| 776 | if (mTestOutputs[mCurOutput] == 0) { |
| 777 | ALOGV("getOutput() opening test output"); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 778 | sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL, |
| 779 | mpClientInterface); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 780 | outputDesc->mDevice = mTestDevice; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 781 | outputDesc->mLatency = mTestLatencyMs; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 782 | outputDesc->mFlags = |
| 783 | (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 784 | outputDesc->mRefCount[stream] = 0; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 785 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 786 | config.sample_rate = mTestSamplingRate; |
| 787 | config.channel_mask = mTestChannels; |
| 788 | config.format = mTestFormat; |
Phil Burk | 77cce80 | 2014-08-04 16:18:15 -0700 | [diff] [blame] | 789 | if (offloadInfo != NULL) { |
| 790 | config.offload_info = *offloadInfo; |
| 791 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 792 | status = mpClientInterface->openOutput(0, |
| 793 | &mTestOutputs[mCurOutput], |
| 794 | &config, |
| 795 | &outputDesc->mDevice, |
| 796 | String8(""), |
| 797 | &outputDesc->mLatency, |
| 798 | outputDesc->mFlags); |
| 799 | if (status == NO_ERROR) { |
| 800 | outputDesc->mSamplingRate = config.sample_rate; |
| 801 | outputDesc->mFormat = config.format; |
| 802 | outputDesc->mChannelMask = config.channel_mask; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 803 | AudioParameter outputCmd = AudioParameter(); |
| 804 | outputCmd.addInt(String8("set_id"),mCurOutput); |
| 805 | mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString()); |
| 806 | addOutput(mTestOutputs[mCurOutput], outputDesc); |
| 807 | } |
| 808 | } |
| 809 | return mTestOutputs[mCurOutput]; |
| 810 | } |
| 811 | #endif //AUDIO_POLICY_TEST |
| 812 | |
| 813 | // open a direct output if required by specified parameters |
| 814 | //force direct flag if offload flag is set: offloading implies a direct output stream |
| 815 | // and all common behaviors are driven by checking only the direct flag |
| 816 | // this should normally be set appropriately in the policy configuration file |
| 817 | if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 818 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 819 | } |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 820 | if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 821 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT); |
| 822 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 823 | // only allow deep buffering for music stream type |
| 824 | if (stream != AUDIO_STREAM_MUSIC) { |
| 825 | flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 826 | } else if (/* stream == AUDIO_STREAM_MUSIC && */ |
| 827 | flags == AUDIO_OUTPUT_FLAG_NONE && |
| 828 | property_get_bool("audio.deep_buffer.media", false /* default_value */)) { |
| 829 | // use DEEP_BUFFER as default output for music stream type |
| 830 | flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER; |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 831 | } |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 832 | if (stream == AUDIO_STREAM_TTS) { |
| 833 | flags = AUDIO_OUTPUT_FLAG_TTS; |
| 834 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 835 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 836 | sp<IOProfile> profile; |
| 837 | |
| 838 | // 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] | 839 | // and not explicitly requested |
| 840 | if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 841 | audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX && |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 842 | audio_channel_count_from_out_mask(channelMask) <= 2) { |
| 843 | goto non_direct_output; |
| 844 | } |
| 845 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 846 | // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled. |
| 847 | // This prevents creating an offloaded track and tearing it down immediately after start |
| 848 | // when audioflinger detects there is an active non offloadable effect. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 849 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 850 | // This may prevent offloading in rare situations where effects are left active by apps |
| 851 | // in the background. |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 852 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 853 | if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 854 | !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 855 | profile = getProfileForDirectOutput(device, |
| 856 | samplingRate, |
| 857 | format, |
| 858 | channelMask, |
| 859 | (audio_output_flags_t)flags); |
| 860 | } |
| 861 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 862 | if (profile != 0) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 863 | sp<SwAudioOutputDescriptor> outputDesc = NULL; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 864 | |
| 865 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 866 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 867 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { |
| 868 | outputDesc = desc; |
| 869 | // reuse direct output if currently open and configured with same parameters |
| 870 | if ((samplingRate == outputDesc->mSamplingRate) && |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 871 | audio_formats_match(format, outputDesc->mFormat) && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 872 | (channelMask == outputDesc->mChannelMask)) { |
| 873 | outputDesc->mDirectOpenCount++; |
| 874 | ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i)); |
| 875 | return mOutputs.keyAt(i); |
| 876 | } |
| 877 | } |
| 878 | } |
| 879 | // close direct output if currently open and configured with different parameters |
| 880 | if (outputDesc != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 881 | closeOutput(outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 882 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 883 | |
| 884 | // if the selected profile is offloaded and no offload info was specified, |
| 885 | // create a default one |
| 886 | audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 887 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 888 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
| 889 | defaultOffloadInfo.sample_rate = samplingRate; |
| 890 | defaultOffloadInfo.channel_mask = channelMask; |
| 891 | defaultOffloadInfo.format = format; |
| 892 | defaultOffloadInfo.stream_type = stream; |
| 893 | defaultOffloadInfo.bit_rate = 0; |
| 894 | defaultOffloadInfo.duration_us = -1; |
| 895 | defaultOffloadInfo.has_video = true; // conservative |
| 896 | defaultOffloadInfo.is_streaming = true; // likely |
| 897 | offloadInfo = &defaultOffloadInfo; |
| 898 | } |
| 899 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 900 | outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 901 | outputDesc->mDevice = device; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 902 | outputDesc->mLatency = 0; |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 903 | outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 904 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 905 | config.sample_rate = samplingRate; |
| 906 | config.channel_mask = channelMask; |
| 907 | config.format = format; |
Phil Burk | 77cce80 | 2014-08-04 16:18:15 -0700 | [diff] [blame] | 908 | if (offloadInfo != NULL) { |
| 909 | config.offload_info = *offloadInfo; |
| 910 | } |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 911 | status = mpClientInterface->openOutput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 912 | &output, |
| 913 | &config, |
| 914 | &outputDesc->mDevice, |
| 915 | String8(""), |
| 916 | &outputDesc->mLatency, |
| 917 | outputDesc->mFlags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 918 | |
| 919 | // only accept an output with the requested parameters |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 920 | if (status != NO_ERROR || |
| 921 | (samplingRate != 0 && samplingRate != config.sample_rate) || |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 922 | (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) || |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 923 | (channelMask != 0 && channelMask != config.channel_mask)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 924 | ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d," |
| 925 | "format %d %d, channelMask %04x %04x", output, samplingRate, |
| 926 | outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask, |
| 927 | outputDesc->mChannelMask); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 928 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 929 | mpClientInterface->closeOutput(output); |
| 930 | } |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 931 | // 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] | 932 | if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) { |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 933 | goto non_direct_output; |
| 934 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 935 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 936 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 937 | outputDesc->mSamplingRate = config.sample_rate; |
| 938 | outputDesc->mChannelMask = config.channel_mask; |
| 939 | outputDesc->mFormat = config.format; |
| 940 | outputDesc->mRefCount[stream] = 0; |
| 941 | outputDesc->mStopTime[stream] = 0; |
| 942 | outputDesc->mDirectOpenCount = 1; |
| 943 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 944 | audio_io_handle_t srcOutput = getOutputForEffect(); |
| 945 | addOutput(output, outputDesc); |
| 946 | audio_io_handle_t dstOutput = getOutputForEffect(); |
| 947 | if (dstOutput == output) { |
| 948 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput); |
| 949 | } |
| 950 | mPreviousOutputs = mOutputs; |
| 951 | ALOGV("getOutput() returns new direct output %d", output); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 952 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 953 | return output; |
| 954 | } |
| 955 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 956 | non_direct_output: |
Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 957 | |
| 958 | // A request for HW A/V sync cannot fallback to a mixed output because time |
| 959 | // stamps are embedded in audio data |
| 960 | if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 961 | return AUDIO_IO_HANDLE_NONE; |
| 962 | } |
| 963 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 964 | // ignoring channel mask due to downmix capability in mixer |
| 965 | |
| 966 | // open a non direct output |
| 967 | |
| 968 | // for non direct outputs, only PCM is supported |
| 969 | if (audio_is_linear_pcm(format)) { |
| 970 | // get which output is suitable for the specified stream. The actual |
| 971 | // routing change will happen when startOutput() will be called |
| 972 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 973 | |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 974 | // at this stage we should ignore the DIRECT flag as no direct output could be found earlier |
| 975 | flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT); |
| 976 | output = selectOutput(outputs, flags, format); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 977 | } |
| 978 | ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d," |
| 979 | "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags); |
| 980 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 981 | ALOGV(" getOutputForDevice() returns output %d", output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 982 | |
| 983 | return output; |
| 984 | } |
| 985 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 986 | 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] | 987 | audio_output_flags_t flags, |
| 988 | audio_format_t format) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 989 | { |
| 990 | // select one output among several that provide a path to a particular device or set of |
| 991 | // devices (the list was previously build by getOutputsForDevice()). |
| 992 | // The priority is as follows: |
| 993 | // 1: the output with the highest number of requested policy flags |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 994 | // 2: the output with the bit depth the closest to the requested one |
| 995 | // 3: the primary output |
| 996 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 997 | |
| 998 | if (outputs.size() == 0) { |
| 999 | return 0; |
| 1000 | } |
| 1001 | if (outputs.size() == 1) { |
| 1002 | return outputs[0]; |
| 1003 | } |
| 1004 | |
| 1005 | int maxCommonFlags = 0; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1006 | audio_io_handle_t outputForFlags = 0; |
| 1007 | audio_io_handle_t outputForPrimary = 0; |
| 1008 | audio_io_handle_t outputForFormat = 0; |
| 1009 | audio_format_t bestFormat = AUDIO_FORMAT_INVALID; |
| 1010 | audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1011 | |
| 1012 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1013 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1014 | if (!outputDesc->isDuplicated()) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1015 | // if a valid format is specified, skip output if not compatible |
| 1016 | if (format != AUDIO_FORMAT_INVALID) { |
| 1017 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1018 | if (!audio_formats_match(format, outputDesc->mFormat)) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1019 | continue; |
| 1020 | } |
| 1021 | } else if (!audio_is_linear_pcm(format)) { |
| 1022 | continue; |
| 1023 | } |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1024 | if (AudioPort::isBetterFormatMatch( |
| 1025 | outputDesc->mFormat, bestFormat, format)) { |
| 1026 | outputForFormat = outputs[i]; |
| 1027 | bestFormat = outputDesc->mFormat; |
| 1028 | } |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1029 | } |
| 1030 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1031 | int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags); |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1032 | if (commonFlags >= maxCommonFlags) { |
| 1033 | if (commonFlags == maxCommonFlags) { |
| 1034 | if (AudioPort::isBetterFormatMatch( |
| 1035 | outputDesc->mFormat, bestFormatForFlags, format)) { |
| 1036 | outputForFlags = outputs[i]; |
| 1037 | bestFormatForFlags = outputDesc->mFormat; |
| 1038 | } |
| 1039 | } else { |
| 1040 | outputForFlags = outputs[i]; |
| 1041 | maxCommonFlags = commonFlags; |
| 1042 | bestFormatForFlags = outputDesc->mFormat; |
| 1043 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1044 | ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags); |
| 1045 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1046 | if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1047 | outputForPrimary = outputs[i]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1048 | } |
| 1049 | } |
| 1050 | } |
| 1051 | |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1052 | if (outputForFlags != 0) { |
| 1053 | return outputForFlags; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1054 | } |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1055 | if (outputForFormat != 0) { |
| 1056 | return outputForFormat; |
| 1057 | } |
| 1058 | if (outputForPrimary != 0) { |
| 1059 | return outputForPrimary; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | return outputs[0]; |
| 1063 | } |
| 1064 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1065 | status_t AudioPolicyManager::startOutput(audio_io_handle_t output, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1066 | audio_stream_type_t stream, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1067 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1068 | { |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1069 | ALOGV("startOutput() output %d, stream %d, session %d", |
| 1070 | output, stream, session); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1071 | ssize_t index = mOutputs.indexOfKey(output); |
| 1072 | if (index < 0) { |
| 1073 | ALOGW("startOutput() unknown output %d", output); |
| 1074 | return BAD_VALUE; |
| 1075 | } |
| 1076 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1077 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
| 1078 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1079 | // Routing? |
| 1080 | mOutputRoutes.incRouteActivity(session); |
| 1081 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1082 | audio_devices_t newDevice; |
| 1083 | if (outputDesc->mPolicyMix != NULL) { |
| 1084 | newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 1085 | } else if (mOutputRoutes.hasRouteChanged(session)) { |
| 1086 | newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1087 | checkStrategyRoute(getStrategy(stream), output); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1088 | } else { |
| 1089 | newDevice = AUDIO_DEVICE_NONE; |
| 1090 | } |
| 1091 | |
| 1092 | uint32_t delayMs = 0; |
| 1093 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1094 | status_t status = startSource(outputDesc, stream, newDevice, &delayMs); |
| 1095 | |
| 1096 | if (status != NO_ERROR) { |
| 1097 | mOutputRoutes.decRouteActivity(session); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1098 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1099 | } |
| 1100 | // Automatically enable the remote submix input when output is started on a re routing mix |
| 1101 | // of type MIX_TYPE_RECORDERS |
| 1102 | if (audio_is_remote_submix_device(newDevice) && outputDesc->mPolicyMix != NULL && |
| 1103 | outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1104 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1105 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1106 | outputDesc->mPolicyMix->mDeviceAddress, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1107 | "remote-submix"); |
| 1108 | } |
| 1109 | |
| 1110 | if (delayMs != 0) { |
| 1111 | usleep(delayMs * 1000); |
| 1112 | } |
| 1113 | |
| 1114 | return status; |
| 1115 | } |
| 1116 | |
| 1117 | status_t AudioPolicyManager::startSource(sp<AudioOutputDescriptor> outputDesc, |
| 1118 | audio_stream_type_t stream, |
| 1119 | audio_devices_t device, |
| 1120 | uint32_t *delayMs) |
| 1121 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1122 | // cannot start playback of STREAM_TTS if any other output is being used |
| 1123 | uint32_t beaconMuteLatency = 0; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1124 | |
| 1125 | *delayMs = 0; |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1126 | if (stream == AUDIO_STREAM_TTS) { |
| 1127 | ALOGV("\t found BEACON stream"); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 1128 | if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1129 | return INVALID_OPERATION; |
| 1130 | } else { |
| 1131 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); |
| 1132 | } |
| 1133 | } else { |
| 1134 | // some playback other than beacon starts |
| 1135 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); |
| 1136 | } |
| 1137 | |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1138 | // check active before incrementing usage count |
| 1139 | bool force = !outputDesc->isActive(); |
| 1140 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1141 | // increment usage count for this stream on the requested output: |
| 1142 | // NOTE that the usage count is the same for duplicated output and hardware output which is |
| 1143 | // necessary for a correct control of hardware output routing by startOutput() and stopOutput() |
| 1144 | outputDesc->changeRefCount(stream, 1); |
| 1145 | |
Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 1146 | if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1147 | // starting an output being rerouted? |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1148 | if (device == AUDIO_DEVICE_NONE) { |
| 1149 | device = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1150 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1151 | routing_strategy strategy = getStrategy(stream); |
| 1152 | bool shouldWait = (strategy == STRATEGY_SONIFICATION) || |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1153 | (strategy == STRATEGY_SONIFICATION_RESPECTFUL) || |
| 1154 | (beaconMuteLatency > 0); |
| 1155 | uint32_t waitMs = beaconMuteLatency; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1156 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1157 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1158 | if (desc != outputDesc) { |
| 1159 | // force a device change if any other output is managed by the same hw |
| 1160 | // module and has a current device selection that differs from selected device. |
| 1161 | // In this case, the audio HAL must receive the new device selection so that it can |
| 1162 | // change the device currently selected by the other active output. |
| 1163 | if (outputDesc->sharesHwModuleWith(desc) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1164 | desc->device() != device) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1165 | force = true; |
| 1166 | } |
| 1167 | // 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] | 1168 | // a notification so that audio focus effect can propagate, or that a mute/unmute |
| 1169 | // event occurred for beacon |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1170 | uint32_t latency = desc->latency(); |
| 1171 | if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) { |
| 1172 | waitMs = latency; |
| 1173 | } |
| 1174 | } |
| 1175 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1176 | uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1177 | |
| 1178 | // handle special case for sonification while in call |
| 1179 | if (isInCall()) { |
| 1180 | handleIncallSonification(stream, true, false); |
| 1181 | } |
| 1182 | |
| 1183 | // apply volume rules for current stream and device if necessary |
| 1184 | checkAndSetVolume(stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 1185 | mVolumeCurves->getVolumeIndex(stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1186 | outputDesc, |
| 1187 | device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1188 | |
| 1189 | // update the outputs if starting an output with a stream that can affect notification |
| 1190 | // routing |
| 1191 | handleNotificationRoutingForStream(stream); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1192 | |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 1193 | // force reevaluating accessibility routing when ringtone or alarm starts |
| 1194 | if (strategy == STRATEGY_SONIFICATION) { |
| 1195 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
| 1196 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1197 | } |
| 1198 | return NO_ERROR; |
| 1199 | } |
| 1200 | |
| 1201 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1202 | status_t AudioPolicyManager::stopOutput(audio_io_handle_t output, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1203 | audio_stream_type_t stream, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1204 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1205 | { |
| 1206 | ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session); |
| 1207 | ssize_t index = mOutputs.indexOfKey(output); |
| 1208 | if (index < 0) { |
| 1209 | ALOGW("stopOutput() unknown output %d", output); |
| 1210 | return BAD_VALUE; |
| 1211 | } |
| 1212 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1213 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1214 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1215 | if (outputDesc->mRefCount[stream] == 1) { |
| 1216 | // Automatically disable the remote submix input when output is stopped on a |
| 1217 | // re routing mix of type MIX_TYPE_RECORDERS |
| 1218 | if (audio_is_remote_submix_device(outputDesc->mDevice) && |
| 1219 | outputDesc->mPolicyMix != NULL && |
| 1220 | outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1221 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1222 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1223 | outputDesc->mPolicyMix->mDeviceAddress, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1224 | "remote-submix"); |
| 1225 | } |
| 1226 | } |
| 1227 | |
| 1228 | // Routing? |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1229 | bool forceDeviceUpdate = false; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1230 | if (outputDesc->mRefCount[stream] > 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1231 | int activityCount = mOutputRoutes.decRouteActivity(session); |
| 1232 | forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0)); |
| 1233 | |
| 1234 | if (forceDeviceUpdate) { |
| 1235 | checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE); |
| 1236 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1237 | } |
| 1238 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1239 | return stopSource(outputDesc, stream, forceDeviceUpdate); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | status_t AudioPolicyManager::stopSource(sp<AudioOutputDescriptor> outputDesc, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1243 | audio_stream_type_t stream, |
| 1244 | bool forceDeviceUpdate) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1245 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1246 | // always handle stream stop, check which stream type is stopping |
| 1247 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); |
| 1248 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1249 | // handle special case for sonification while in call |
| 1250 | if (isInCall()) { |
| 1251 | handleIncallSonification(stream, false, false); |
| 1252 | } |
| 1253 | |
| 1254 | if (outputDesc->mRefCount[stream] > 0) { |
| 1255 | // decrement usage count of this stream on the output |
| 1256 | outputDesc->changeRefCount(stream, -1); |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1257 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1258 | // store time at which the stream was stopped - see isStreamActive() |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1259 | if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1260 | outputDesc->mStopTime[stream] = systemTime(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1261 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1262 | // delay the device switch by twice the latency because stopOutput() is executed when |
| 1263 | // the track stop() command is received and at that time the audio track buffer can |
| 1264 | // still contain data that needs to be drained. The latency only covers the audio HAL |
| 1265 | // and kernel buffers. Also the latency does not always include additional delay in the |
| 1266 | // audio path (audio DSP, CODEC ...) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1267 | setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1268 | |
| 1269 | // force restoring the device selection on other active outputs if it differs from the |
| 1270 | // one being selected for this output |
| 1271 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 1272 | audio_io_handle_t curOutput = mOutputs.keyAt(i); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1273 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1274 | if (desc != outputDesc && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1275 | desc->isActive() && |
| 1276 | outputDesc->sharesHwModuleWith(desc) && |
| 1277 | (newDevice != desc->device())) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1278 | setOutputDevice(desc, |
| 1279 | getNewOutputDevice(desc, false /*fromCache*/), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1280 | true, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1281 | outputDesc->latency()*2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1282 | } |
| 1283 | } |
| 1284 | // update the outputs if stopping one with a stream that can affect notification routing |
| 1285 | handleNotificationRoutingForStream(stream); |
| 1286 | } |
| 1287 | return NO_ERROR; |
| 1288 | } else { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1289 | ALOGW("stopOutput() refcount is already 0"); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1290 | return INVALID_OPERATION; |
| 1291 | } |
| 1292 | } |
| 1293 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1294 | void AudioPolicyManager::releaseOutput(audio_io_handle_t output, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1295 | audio_stream_type_t stream __unused, |
| 1296 | audio_session_t session __unused) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1297 | { |
| 1298 | ALOGV("releaseOutput() %d", output); |
| 1299 | ssize_t index = mOutputs.indexOfKey(output); |
| 1300 | if (index < 0) { |
| 1301 | ALOGW("releaseOutput() releasing unknown output %d", output); |
| 1302 | return; |
| 1303 | } |
| 1304 | |
| 1305 | #ifdef AUDIO_POLICY_TEST |
| 1306 | int testIndex = testOutputIndex(output); |
| 1307 | if (testIndex != 0) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1308 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1309 | if (outputDesc->isActive()) { |
| 1310 | mpClientInterface->closeOutput(output); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1311 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1312 | mTestOutputs[testIndex] = 0; |
| 1313 | } |
| 1314 | return; |
| 1315 | } |
| 1316 | #endif //AUDIO_POLICY_TEST |
| 1317 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1318 | // Routing |
| 1319 | mOutputRoutes.removeRoute(session); |
| 1320 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1321 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1322 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1323 | if (desc->mDirectOpenCount <= 0) { |
| 1324 | ALOGW("releaseOutput() invalid open count %d for output %d", |
| 1325 | desc->mDirectOpenCount, output); |
| 1326 | return; |
| 1327 | } |
| 1328 | if (--desc->mDirectOpenCount == 0) { |
| 1329 | closeOutput(output); |
| 1330 | // If effects where present on the output, audioflinger moved them to the primary |
| 1331 | // output by default: move them back to the appropriate output. |
| 1332 | audio_io_handle_t dstOutput = getOutputForEffect(); |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 1333 | if (hasPrimaryOutput() && dstOutput != mPrimaryOutput->mIoHandle) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1334 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, |
| 1335 | mPrimaryOutput->mIoHandle, dstOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1336 | } |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1337 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1338 | } |
| 1339 | } |
| 1340 | } |
| 1341 | |
| 1342 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1343 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, |
| 1344 | audio_io_handle_t *input, |
| 1345 | audio_session_t session, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1346 | uid_t uid, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1347 | uint32_t samplingRate, |
| 1348 | audio_format_t format, |
| 1349 | audio_channel_mask_t channelMask, |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1350 | audio_input_flags_t flags, |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1351 | audio_port_handle_t selectedDeviceId, |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1352 | input_type_t *inputType) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1353 | { |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1354 | ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x," |
| 1355 | "session %d, flags %#x", |
| 1356 | attr->source, samplingRate, format, channelMask, session, flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1357 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1358 | *input = AUDIO_IO_HANDLE_NONE; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1359 | *inputType = API_INPUT_INVALID; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1360 | audio_devices_t device; |
| 1361 | // handle legacy remote submix case where the address was not always specified |
| 1362 | String8 address = String8(""); |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1363 | audio_source_t inputSource = attr->source; |
| 1364 | audio_source_t halInputSource; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1365 | AudioMix *policyMix = NULL; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1366 | |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1367 | if (inputSource == AUDIO_SOURCE_DEFAULT) { |
| 1368 | inputSource = AUDIO_SOURCE_MIC; |
| 1369 | } |
| 1370 | halInputSource = inputSource; |
| 1371 | |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1372 | // Explicit routing? |
| 1373 | sp<DeviceDescriptor> deviceDesc; |
| 1374 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { |
| 1375 | if (mAvailableInputDevices[i]->getId() == selectedDeviceId) { |
| 1376 | deviceDesc = mAvailableInputDevices[i]; |
| 1377 | break; |
| 1378 | } |
| 1379 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1380 | mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1381 | |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1382 | if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX && |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1383 | strncmp(attr->tags, "addr=", strlen("addr=")) == 0) { |
Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 1384 | status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 1385 | if (ret != NO_ERROR) { |
| 1386 | return ret; |
| 1387 | } |
| 1388 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1389 | device = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 1390 | address = String8(attr->tags + strlen("addr=")); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1391 | } else { |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1392 | device = getDeviceAndMixForInputSource(inputSource, &policyMix); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1393 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1394 | ALOGW("getInputForAttr() could not find device for source %d", inputSource); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1395 | return BAD_VALUE; |
| 1396 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1397 | if (policyMix != NULL) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1398 | address = policyMix->mDeviceAddress; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1399 | if (policyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1400 | // there is an external policy, but this input is attached to a mix of recorders, |
| 1401 | // meaning it receives audio injected into the framework, so the recorder doesn't |
| 1402 | // know about it and is therefore considered "legacy" |
| 1403 | *inputType = API_INPUT_LEGACY; |
| 1404 | } else { |
| 1405 | // recording a mix of players defined by an external policy, we're rerouting for |
| 1406 | // an external policy |
| 1407 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
| 1408 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1409 | } else if (audio_is_remote_submix_device(device)) { |
| 1410 | address = String8("0"); |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1411 | *inputType = API_INPUT_MIX_CAPTURE; |
Eric Laurent | 82db269 | 2015-08-07 13:59:42 -0700 | [diff] [blame] | 1412 | } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) { |
| 1413 | *inputType = API_INPUT_TELEPHONY_RX; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1414 | } else { |
| 1415 | *inputType = API_INPUT_LEGACY; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1416 | } |
Ravi Kumar Alamanda | b367f5b | 2015-08-25 08:21:37 -0700 | [diff] [blame] | 1417 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1418 | } |
| 1419 | |
| 1420 | *input = getInputForDevice(device, address, session, uid, inputSource, |
| 1421 | samplingRate, format, channelMask, flags, |
| 1422 | policyMix); |
| 1423 | if (*input == AUDIO_IO_HANDLE_NONE) { |
| 1424 | mInputRoutes.removeRoute(session); |
| 1425 | return INVALID_OPERATION; |
| 1426 | } |
| 1427 | ALOGV("getInputForAttr() returns input type = %d", *inputType); |
| 1428 | return NO_ERROR; |
| 1429 | } |
| 1430 | |
| 1431 | |
| 1432 | audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device, |
| 1433 | String8 address, |
| 1434 | audio_session_t session, |
| 1435 | uid_t uid, |
| 1436 | audio_source_t inputSource, |
| 1437 | uint32_t samplingRate, |
| 1438 | audio_format_t format, |
| 1439 | audio_channel_mask_t channelMask, |
| 1440 | audio_input_flags_t flags, |
| 1441 | AudioMix *policyMix) |
| 1442 | { |
| 1443 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 1444 | audio_source_t halInputSource = inputSource; |
| 1445 | bool isSoundTrigger = false; |
| 1446 | |
| 1447 | if (inputSource == AUDIO_SOURCE_HOTWORD) { |
| 1448 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); |
| 1449 | if (index >= 0) { |
| 1450 | input = mSoundTriggerSessions.valueFor(session); |
| 1451 | isSoundTrigger = true; |
| 1452 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); |
| 1453 | ALOGV("SoundTrigger capture on session %d input %d", session, input); |
| 1454 | } else { |
| 1455 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1456 | } |
| 1457 | } |
| 1458 | |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1459 | // find a compatible input profile (not necessarily identical in parameters) |
| 1460 | sp<IOProfile> profile; |
| 1461 | // samplingRate and flags may be updated by getInputProfile |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1462 | uint32_t profileSamplingRate = (samplingRate == 0) ? SAMPLE_RATE_HZ_DEFAULT : samplingRate; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1463 | audio_format_t profileFormat = format; |
| 1464 | audio_channel_mask_t profileChannelMask = channelMask; |
| 1465 | audio_input_flags_t profileFlags = flags; |
| 1466 | for (;;) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1467 | profile = getInputProfile(device, address, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1468 | profileSamplingRate, profileFormat, profileChannelMask, |
| 1469 | profileFlags); |
| 1470 | if (profile != 0) { |
| 1471 | break; // success |
| 1472 | } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) { |
| 1473 | profileFlags = AUDIO_INPUT_FLAG_NONE; // retry |
| 1474 | } else { // fail |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1475 | ALOGW("getInputForDevice() could not find profile for device 0x%X," |
| 1476 | "samplingRate %u, format %#x, channelMask 0x%X, flags %#x", |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1477 | device, samplingRate, format, channelMask, flags); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1478 | return input; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1479 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1480 | } |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1481 | // Pick input sampling rate if not specified by client |
| 1482 | if (samplingRate == 0) { |
| 1483 | samplingRate = profileSamplingRate; |
| 1484 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1485 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1486 | if (profile->getModuleHandle() == 0) { |
| 1487 | ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName()); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1488 | return input; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1489 | } |
| 1490 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1491 | sp<AudioSession> audioSession = new AudioSession(session, |
| 1492 | inputSource, |
| 1493 | format, |
| 1494 | samplingRate, |
| 1495 | channelMask, |
| 1496 | flags, |
| 1497 | uid, |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 1498 | isSoundTrigger, |
| 1499 | policyMix, mpClientInterface); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1500 | |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1501 | // TODO enable input reuse |
| 1502 | #if 0 |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1503 | // reuse an open input if possible |
| 1504 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 1505 | sp<AudioInputDescriptor> desc = mInputs.valueAt(i); |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1506 | // reuse input if it shares the same profile and same sound trigger attribute |
| 1507 | if (profile == desc->mProfile && |
| 1508 | isSoundTrigger == desc->isSoundTrigger()) { |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1509 | |
| 1510 | sp<AudioSession> as = desc->getAudioSession(session); |
| 1511 | if (as != 0) { |
| 1512 | // do not allow unmatching properties on same session |
| 1513 | if (as->matches(audioSession)) { |
| 1514 | as->changeOpenCount(1); |
| 1515 | } else { |
| 1516 | ALOGW("getInputForDevice() record with different attributes" |
| 1517 | " exists for session %d", session); |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1518 | return input; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1519 | } |
| 1520 | } else { |
| 1521 | desc->addAudioSession(session, audioSession); |
| 1522 | } |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1523 | ALOGV("getInputForDevice() reusing input %d", mInputs.keyAt(i)); |
| 1524 | return mInputs.keyAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1525 | } |
| 1526 | } |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1527 | #endif |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1528 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1529 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1530 | config.sample_rate = profileSamplingRate; |
| 1531 | config.channel_mask = profileChannelMask; |
| 1532 | config.format = profileFormat; |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1533 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1534 | status_t status = mpClientInterface->openInput(profile->getModuleHandle(), |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1535 | &input, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1536 | &config, |
| 1537 | &device, |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 1538 | address, |
Eric Laurent | 1c9c2cc | 2014-08-28 19:37:25 -0700 | [diff] [blame] | 1539 | halInputSource, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1540 | profileFlags); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1541 | |
| 1542 | // only accept input with the exact requested set of parameters |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1543 | if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE || |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1544 | (profileSamplingRate != config.sample_rate) || |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1545 | !audio_formats_match(profileFormat, config.format) || |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1546 | (profileChannelMask != config.channel_mask)) { |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1547 | ALOGW("getInputForAttr() failed opening input: samplingRate %d" |
| 1548 | ", format %d, channelMask %x", |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1549 | samplingRate, format, channelMask); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1550 | if (input != AUDIO_IO_HANDLE_NONE) { |
| 1551 | mpClientInterface->closeInput(input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1552 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1553 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1554 | } |
| 1555 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1556 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile); |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1557 | inputDesc->mSamplingRate = profileSamplingRate; |
| 1558 | inputDesc->mFormat = profileFormat; |
| 1559 | inputDesc->mChannelMask = profileChannelMask; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1560 | inputDesc->mDevice = device; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1561 | inputDesc->mPolicyMix = policyMix; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1562 | inputDesc->addAudioSession(session, audioSession); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1563 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1564 | addInput(input, inputDesc); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1565 | mpClientInterface->onAudioPortListUpdate(); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1566 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1567 | return input; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1568 | } |
| 1569 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1570 | status_t AudioPolicyManager::startInput(audio_io_handle_t input, |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1571 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1572 | { |
| 1573 | ALOGV("startInput() input %d", input); |
| 1574 | ssize_t index = mInputs.indexOfKey(input); |
| 1575 | if (index < 0) { |
| 1576 | ALOGW("startInput() unknown input %d", input); |
| 1577 | return BAD_VALUE; |
| 1578 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1579 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1580 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1581 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
| 1582 | if (audioSession == 0) { |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1583 | ALOGW("startInput() unknown session %d on input %d", session, input); |
| 1584 | return BAD_VALUE; |
| 1585 | } |
| 1586 | |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1587 | // virtual input devices are compatible with other input devices |
| 1588 | if (!is_virtual_input_device(inputDesc->mDevice)) { |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1589 | |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1590 | // for a non-virtual input device, check if there is another (non-virtual) active input |
| 1591 | audio_io_handle_t activeInput = mInputs.getActiveInput(); |
| 1592 | if (activeInput != 0 && activeInput != input) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1593 | |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1594 | // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed, |
| 1595 | // otherwise the active input continues and the new input cannot be started. |
| 1596 | sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput); |
| 1597 | if ((activeDesc->inputSource() == AUDIO_SOURCE_HOTWORD) && |
| 1598 | !activeDesc->hasPreemptedSession(session)) { |
| 1599 | ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput); |
| 1600 | //FIXME: consider all active sessions |
| 1601 | AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions(); |
| 1602 | audio_session_t activeSession = activeSessions.keyAt(0); |
| 1603 | SortedVector<audio_session_t> sessions = |
| 1604 | activeDesc->getPreemptedSessions(); |
| 1605 | sessions.add(activeSession); |
| 1606 | inputDesc->setPreemptedSessions(sessions); |
| 1607 | stopInput(activeInput, activeSession); |
| 1608 | releaseInput(activeInput, activeSession); |
| 1609 | } else { |
| 1610 | ALOGE("startInput(%d) failed: other input %d already started", input, activeInput); |
| 1611 | return INVALID_OPERATION; |
| 1612 | } |
| 1613 | } |
| 1614 | |
| 1615 | // Do not allow capture if an active voice call is using a software patch and |
| 1616 | // the call TX source device is on the same HW module. |
| 1617 | // FIXME: would be better to refine to only inputs whose profile connects to the |
| 1618 | // call TX device but this information is not in the audio patch |
| 1619 | if (mCallTxPatch != 0 && |
| 1620 | inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) { |
| 1621 | return INVALID_OPERATION; |
| 1622 | } |
| 1623 | } |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 1624 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1625 | // Routing? |
| 1626 | mInputRoutes.incRouteActivity(session); |
| 1627 | |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1628 | if (!inputDesc->isActive() || mInputRoutes.hasRouteChanged(session)) { |
| 1629 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 1630 | if ((inputDesc->mPolicyMix != NULL) |
| 1631 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1632 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1633 | MIX_STATE_MIXING); |
| 1634 | } |
Jean-Michel Trivi | 2b0c1fc | 2015-04-15 17:29:28 -0700 | [diff] [blame] | 1635 | |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1636 | if (mInputs.activeInputsCount() == 0) { |
| 1637 | SoundTrigger::setCaptureState(true); |
| 1638 | } |
| 1639 | setInputDevice(input, getNewInputDevice(input), true /* force */); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1640 | |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1641 | // automatically enable the remote submix output when input is started if not |
| 1642 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 1643 | // For remote submix (a virtual device), we open only one input per capture request. |
| 1644 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 1645 | String8 address = String8(""); |
| 1646 | if (inputDesc->mPolicyMix == NULL) { |
| 1647 | address = String8("0"); |
| 1648 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1649 | address = inputDesc->mPolicyMix->mDeviceAddress; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1650 | } |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1651 | if (address != "") { |
| 1652 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 1653 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 1654 | address, "remote-submix"); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1655 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1656 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1657 | } |
| 1658 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1659 | ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1660 | |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1661 | audioSession->changeActiveCount(1); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1662 | return NO_ERROR; |
| 1663 | } |
| 1664 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1665 | status_t AudioPolicyManager::stopInput(audio_io_handle_t input, |
| 1666 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1667 | { |
| 1668 | ALOGV("stopInput() input %d", input); |
| 1669 | ssize_t index = mInputs.indexOfKey(input); |
| 1670 | if (index < 0) { |
| 1671 | ALOGW("stopInput() unknown input %d", input); |
| 1672 | return BAD_VALUE; |
| 1673 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1674 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1675 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1676 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1677 | if (index < 0) { |
| 1678 | ALOGW("stopInput() unknown session %d on input %d", session, input); |
| 1679 | return BAD_VALUE; |
| 1680 | } |
| 1681 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1682 | if (audioSession->activeCount() == 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1683 | ALOGW("stopInput() input %d already stopped", input); |
| 1684 | return INVALID_OPERATION; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1685 | } |
| 1686 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1687 | audioSession->changeActiveCount(-1); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1688 | |
| 1689 | // Routing? |
| 1690 | mInputRoutes.decRouteActivity(session); |
| 1691 | |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1692 | if (!inputDesc->isActive()) { |
| 1693 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 1694 | if ((inputDesc->mPolicyMix != NULL) |
| 1695 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1696 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1697 | MIX_STATE_IDLE); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 1698 | } |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1699 | |
| 1700 | // automatically disable the remote submix output when input is stopped if not |
| 1701 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 1702 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 1703 | String8 address = String8(""); |
| 1704 | if (inputDesc->mPolicyMix == NULL) { |
| 1705 | address = String8("0"); |
| 1706 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1707 | address = inputDesc->mPolicyMix->mDeviceAddress; |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1708 | } |
| 1709 | if (address != "") { |
| 1710 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 1711 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 1712 | address, "remote-submix"); |
| 1713 | } |
| 1714 | } |
| 1715 | |
| 1716 | resetInputDevice(input); |
| 1717 | |
| 1718 | if (mInputs.activeInputsCount() == 0) { |
| 1719 | SoundTrigger::setCaptureState(false); |
| 1720 | } |
| 1721 | inputDesc->clearPreemptedSessions(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1722 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1723 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1724 | } |
| 1725 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1726 | void AudioPolicyManager::releaseInput(audio_io_handle_t input, |
| 1727 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1728 | { |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 1729 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1730 | ALOGV("releaseInput() %d", input); |
| 1731 | ssize_t index = mInputs.indexOfKey(input); |
| 1732 | if (index < 0) { |
| 1733 | ALOGW("releaseInput() releasing unknown input %d", input); |
| 1734 | return; |
| 1735 | } |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1736 | |
| 1737 | // Routing |
| 1738 | mInputRoutes.removeRoute(session); |
| 1739 | |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1740 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
| 1741 | ALOG_ASSERT(inputDesc != 0); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1742 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1743 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1744 | if (index < 0) { |
| 1745 | ALOGW("releaseInput() unknown session %d on input %d", session, input); |
| 1746 | return; |
| 1747 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1748 | |
| 1749 | if (audioSession->openCount() == 0) { |
| 1750 | ALOGW("releaseInput() invalid open count %d on session %d", |
| 1751 | audioSession->openCount(), session); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1752 | return; |
| 1753 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1754 | |
| 1755 | if (audioSession->changeOpenCount(-1) == 0) { |
| 1756 | inputDesc->removeAudioSession(session); |
| 1757 | } |
| 1758 | |
Jean-Michel Trivi | 65bfe91 | 2015-12-04 15:56:47 -0800 | [diff] [blame] | 1759 | if (inputDesc->getOpenRefCount() > 0) { |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1760 | ALOGV("releaseInput() exit > 0"); |
| 1761 | return; |
| 1762 | } |
| 1763 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1764 | closeInput(input); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1765 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1766 | ALOGV("releaseInput() exit"); |
| 1767 | } |
| 1768 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1769 | void AudioPolicyManager::closeAllInputs() { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1770 | bool patchRemoved = false; |
| 1771 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1772 | for(size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1773 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index); |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 1774 | ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1775 | if (patch_index >= 0) { |
| 1776 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index); |
| 1777 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 1778 | mAudioPatches.removeItemsAt(patch_index); |
| 1779 | patchRemoved = true; |
| 1780 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1781 | mpClientInterface->closeInput(mInputs.keyAt(input_index)); |
| 1782 | } |
| 1783 | mInputs.clear(); |
Chris Thornton | 52785f3 | 2016-02-09 17:28:49 -0800 | [diff] [blame] | 1784 | SoundTrigger::setCaptureState(false); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1785 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1786 | |
| 1787 | if (patchRemoved) { |
| 1788 | mpClientInterface->onAudioPatchListUpdate(); |
| 1789 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1790 | } |
| 1791 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1792 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1793 | int indexMin, |
| 1794 | int indexMax) |
| 1795 | { |
| 1796 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 1797 | mVolumeCurves->initStreamVolume(stream, indexMin, indexMax); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1798 | |
| 1799 | // initialize other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 1800 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 1801 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1802 | continue; |
| 1803 | } |
| 1804 | mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1805 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1806 | } |
| 1807 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1808 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1809 | int index, |
| 1810 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1811 | { |
| 1812 | |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 1813 | if ((index < mVolumeCurves->getVolumeIndexMin(stream)) || |
| 1814 | (index > mVolumeCurves->getVolumeIndexMax(stream))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1815 | return BAD_VALUE; |
| 1816 | } |
| 1817 | if (!audio_is_output_device(device)) { |
| 1818 | return BAD_VALUE; |
| 1819 | } |
| 1820 | |
| 1821 | // Force max volume if stream cannot be muted |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 1822 | if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1823 | |
| 1824 | ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d", |
| 1825 | stream, device, index); |
| 1826 | |
| 1827 | // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and |
| 1828 | // clear all device specific values |
| 1829 | if (device == AUDIO_DEVICE_OUT_DEFAULT) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 1830 | mVolumeCurves->clearCurrentVolumeIndex(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1831 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1832 | |
| 1833 | // update other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 1834 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 1835 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1836 | continue; |
| 1837 | } |
| 1838 | mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index); |
| 1839 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1840 | |
Eric Laurent | 31551f8 | 2014-10-10 18:21:56 -0700 | [diff] [blame] | 1841 | // update volume on all outputs whose current device is also selected by the same |
| 1842 | // strategy as the device specified by the caller |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1843 | status_t status = NO_ERROR; |
| 1844 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1845 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 1846 | audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device()); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 1847 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 1848 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1849 | continue; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1850 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 1851 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1852 | audio_devices_t curStreamDevice = getDeviceForStrategy(curStrategy, true /*fromCache*/); |
| 1853 | // it is possible that the requested device is not selected by the strategy |
| 1854 | // (e.g an explicit audio patch is active causing getDevicesForStream() |
| 1855 | // to return this device. We must make sure that the device passed is part of the |
| 1856 | // devices considered when applying volume below. |
| 1857 | curStreamDevice |= device; |
| 1858 | |
| 1859 | if (((device == AUDIO_DEVICE_OUT_DEFAULT) || |
| 1860 | ((curDevice & curStreamDevice) != 0))) { |
| 1861 | status_t volStatus = |
| 1862 | checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice); |
| 1863 | if (volStatus != NO_ERROR) { |
| 1864 | status = volStatus; |
| 1865 | } |
| 1866 | } |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1867 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1868 | } |
| 1869 | return status; |
| 1870 | } |
| 1871 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1872 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1873 | int *index, |
| 1874 | audio_devices_t device) |
| 1875 | { |
| 1876 | if (index == NULL) { |
| 1877 | return BAD_VALUE; |
| 1878 | } |
| 1879 | if (!audio_is_output_device(device)) { |
| 1880 | return BAD_VALUE; |
| 1881 | } |
| 1882 | // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to |
| 1883 | // the strategy the stream belongs to. |
| 1884 | if (device == AUDIO_DEVICE_OUT_DEFAULT) { |
| 1885 | device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); |
| 1886 | } |
François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 1887 | device = Volume::getDeviceForVolume(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1888 | |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 1889 | *index = mVolumeCurves->getVolumeIndex(stream, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1890 | ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index); |
| 1891 | return NO_ERROR; |
| 1892 | } |
| 1893 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1894 | audio_io_handle_t AudioPolicyManager::selectOutputForEffects( |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1895 | const SortedVector<audio_io_handle_t>& outputs) |
| 1896 | { |
| 1897 | // select one output among several suitable for global effects. |
| 1898 | // The priority is as follows: |
| 1899 | // 1: An offloaded output. If the effect ends up not being offloadable, |
| 1900 | // AudioFlinger will invalidate the track and the offloaded output |
| 1901 | // will be closed causing the effect to be moved to a PCM output. |
| 1902 | // 2: A deep buffer output |
| 1903 | // 3: the first output in the list |
| 1904 | |
| 1905 | if (outputs.size() == 0) { |
| 1906 | return 0; |
| 1907 | } |
| 1908 | |
| 1909 | audio_io_handle_t outputOffloaded = 0; |
| 1910 | audio_io_handle_t outputDeepBuffer = 0; |
| 1911 | |
| 1912 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1913 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1914 | ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1915 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 1916 | outputOffloaded = outputs[i]; |
| 1917 | } |
| 1918 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { |
| 1919 | outputDeepBuffer = outputs[i]; |
| 1920 | } |
| 1921 | } |
| 1922 | |
| 1923 | ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d", |
| 1924 | outputOffloaded, outputDeepBuffer); |
| 1925 | if (outputOffloaded != 0) { |
| 1926 | return outputOffloaded; |
| 1927 | } |
| 1928 | if (outputDeepBuffer != 0) { |
| 1929 | return outputDeepBuffer; |
| 1930 | } |
| 1931 | |
| 1932 | return outputs[0]; |
| 1933 | } |
| 1934 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1935 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1936 | { |
| 1937 | // apply simple rule where global effects are attached to the same output as MUSIC streams |
| 1938 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1939 | routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1940 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 1941 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs); |
| 1942 | |
| 1943 | audio_io_handle_t output = selectOutputForEffects(dstOutputs); |
| 1944 | ALOGV("getOutputForEffect() got output %d for fx %s flags %x", |
| 1945 | output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags); |
| 1946 | |
| 1947 | return output; |
| 1948 | } |
| 1949 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1950 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1951 | audio_io_handle_t io, |
| 1952 | uint32_t strategy, |
| 1953 | int session, |
| 1954 | int id) |
| 1955 | { |
| 1956 | ssize_t index = mOutputs.indexOfKey(io); |
| 1957 | if (index < 0) { |
| 1958 | index = mInputs.indexOfKey(io); |
| 1959 | if (index < 0) { |
| 1960 | ALOGW("registerEffect() unknown io %d", io); |
| 1961 | return INVALID_OPERATION; |
| 1962 | } |
| 1963 | } |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 1964 | return mEffects.registerEffect(desc, io, strategy, session, id); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1965 | } |
| 1966 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1967 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const |
| 1968 | { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1969 | bool active = false; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 1970 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) { |
| 1971 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1972 | continue; |
| 1973 | } |
| 1974 | active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs); |
| 1975 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1976 | return active; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1977 | } |
| 1978 | |
| 1979 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const |
| 1980 | { |
| 1981 | return mOutputs.isStreamActiveRemotely(stream, inPastMs); |
| 1982 | } |
| 1983 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1984 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1985 | { |
| 1986 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1987 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1988 | if (inputDescriptor->isSourceActive(source)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1989 | return true; |
| 1990 | } |
| 1991 | } |
| 1992 | return false; |
| 1993 | } |
| 1994 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1995 | // Register a list of custom mixes with their attributes and format. |
| 1996 | // When a mix is registered, corresponding input and output profiles are |
| 1997 | // added to the remote submix hw module. The profile contains only the |
| 1998 | // parameters (sampling rate, format...) specified by the mix. |
| 1999 | // The corresponding input remote submix device is also connected. |
| 2000 | // |
| 2001 | // When a remote submix device is connected, the address is checked to select the |
| 2002 | // appropriate profile and the corresponding input or output stream is opened. |
| 2003 | // |
| 2004 | // When capture starts, getInputForAttr() will: |
| 2005 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2006 | // - 2 if none found, getDeviceForInputSource() will: |
| 2007 | // - 2.1 look for a mix matching the attributes source |
| 2008 | // - 2.2 if none found, default to device selection by policy rules |
| 2009 | // At this time, the corresponding output remote submix device is also connected |
| 2010 | // and active playback use cases can be transferred to this mix if needed when reconnecting |
| 2011 | // after AudioTracks are invalidated |
| 2012 | // |
| 2013 | // When playback starts, getOutputForAttr() will: |
| 2014 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2015 | // - 2 if none found, look for a mix matching the attributes usage |
| 2016 | // - 3 if none found, default to device and output selection by policy rules. |
| 2017 | |
| 2018 | status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes) |
| 2019 | { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2020 | ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size()); |
| 2021 | status_t res = NO_ERROR; |
| 2022 | |
| 2023 | sp<HwModule> rSubmixModule; |
| 2024 | // examine each mix's route type |
| 2025 | for (size_t i = 0; i < mixes.size(); i++) { |
| 2026 | // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination |
| 2027 | if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) { |
| 2028 | res = INVALID_OPERATION; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2029 | break; |
| 2030 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2031 | if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) { |
| 2032 | // Loop back through "remote submix" |
| 2033 | if (rSubmixModule == 0) { |
| 2034 | for (size_t j = 0; i < mHwModules.size(); j++) { |
| 2035 | if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0 |
| 2036 | && mHwModules[j]->mHandle != 0) { |
| 2037 | rSubmixModule = mHwModules[j]; |
| 2038 | break; |
| 2039 | } |
| 2040 | } |
| 2041 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2042 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2043 | ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK", i, mixes.size()); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2044 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2045 | if (rSubmixModule == 0) { |
| 2046 | ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", i); |
| 2047 | res = INVALID_OPERATION; |
| 2048 | break; |
| 2049 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2050 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2051 | String8 address = mixes[i].mDeviceAddress; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2052 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2053 | if (mPolicyMixes.registerMix(address, mixes[i], 0 /*output desc*/) != NO_ERROR) { |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2054 | ALOGE(" Error registering mix %zu for address %s", i, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2055 | res = INVALID_OPERATION; |
| 2056 | break; |
| 2057 | } |
| 2058 | audio_config_t outputConfig = mixes[i].mFormat; |
| 2059 | audio_config_t inputConfig = mixes[i].mFormat; |
| 2060 | // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in |
| 2061 | // stereo and let audio flinger do the channel conversion if needed. |
| 2062 | outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 2063 | inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
| 2064 | rSubmixModule->addOutputProfile(address, &outputConfig, |
| 2065 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address); |
| 2066 | rSubmixModule->addInputProfile(address, &inputConfig, |
| 2067 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, address); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2068 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2069 | if (mixes[i].mMixType == MIX_TYPE_PLAYERS) { |
| 2070 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2071 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2072 | address.string(), "remote-submix"); |
| 2073 | } else { |
| 2074 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2075 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2076 | address.string(), "remote-submix"); |
| 2077 | } |
| 2078 | } 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] | 2079 | String8 address = mixes[i].mDeviceAddress; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2080 | audio_devices_t device = mixes[i].mDeviceType; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2081 | ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s", |
| 2082 | i, mixes.size(), device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2083 | |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2084 | bool foundOutput = false; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2085 | for (size_t j = 0 ; j < mOutputs.size() ; j++) { |
| 2086 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j); |
| 2087 | sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle()); |
| 2088 | if ((patch != 0) && (patch->mPatch.num_sinks != 0) |
| 2089 | && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE) |
| 2090 | && (patch->mPatch.sinks[0].ext.device.type == device) |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2091 | && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(), |
| 2092 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2093 | |
| 2094 | if (mPolicyMixes.registerMix(address, mixes[i], desc) != NO_ERROR) { |
| 2095 | res = INVALID_OPERATION; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2096 | } else { |
| 2097 | foundOutput = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2098 | } |
| 2099 | break; |
| 2100 | } |
| 2101 | } |
| 2102 | |
| 2103 | if (res != NO_ERROR) { |
| 2104 | ALOGE(" Error registering mix %zu for device 0x%X addr %s", |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2105 | i, device, address.string()); |
| 2106 | res = INVALID_OPERATION; |
| 2107 | break; |
| 2108 | } else if (!foundOutput) { |
| 2109 | ALOGE(" Output not found for mix %zu for device 0x%X addr %s", |
| 2110 | i, device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2111 | res = INVALID_OPERATION; |
| 2112 | break; |
| 2113 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2114 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2115 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2116 | if (res != NO_ERROR) { |
| 2117 | unregisterPolicyMixes(mixes); |
| 2118 | } |
| 2119 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2120 | } |
| 2121 | |
| 2122 | status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes) |
| 2123 | { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2124 | ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2125 | status_t res = NO_ERROR; |
| 2126 | sp<HwModule> rSubmixModule; |
| 2127 | // examine each mix's route type |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2128 | for (size_t i = 0; i < mixes.size(); i++) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2129 | 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] | 2130 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2131 | if (rSubmixModule == 0) { |
| 2132 | for (size_t j = 0; i < mHwModules.size(); j++) { |
| 2133 | if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0 |
| 2134 | && mHwModules[j]->mHandle != 0) { |
| 2135 | rSubmixModule = mHwModules[j]; |
| 2136 | break; |
| 2137 | } |
| 2138 | } |
| 2139 | } |
| 2140 | if (rSubmixModule == 0) { |
| 2141 | res = INVALID_OPERATION; |
| 2142 | continue; |
| 2143 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2144 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2145 | String8 address = mixes[i].mDeviceAddress; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2146 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2147 | if (mPolicyMixes.unregisterMix(address) != NO_ERROR) { |
| 2148 | res = INVALID_OPERATION; |
| 2149 | continue; |
| 2150 | } |
| 2151 | |
| 2152 | if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) == |
| 2153 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2154 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2155 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2156 | address.string(), "remote-submix"); |
| 2157 | } |
| 2158 | if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) == |
| 2159 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2160 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2161 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2162 | address.string(), "remote-submix"); |
| 2163 | } |
| 2164 | rSubmixModule->removeOutputProfile(address); |
| 2165 | rSubmixModule->removeInputProfile(address); |
| 2166 | |
| 2167 | } if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 2168 | if (mPolicyMixes.unregisterMix(mixes[i].mDeviceAddress) != NO_ERROR) { |
| 2169 | res = INVALID_OPERATION; |
| 2170 | continue; |
| 2171 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2172 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2173 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2174 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2175 | } |
| 2176 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2177 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2178 | status_t AudioPolicyManager::dump(int fd) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2179 | { |
| 2180 | const size_t SIZE = 256; |
| 2181 | char buffer[SIZE]; |
| 2182 | String8 result; |
| 2183 | |
| 2184 | snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this); |
| 2185 | result.append(buffer); |
| 2186 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 2187 | snprintf(buffer, SIZE, " Primary Output: %d\n", |
| 2188 | hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2189 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2190 | snprintf(buffer, SIZE, " Phone state: %d\n", mEngine->getPhoneState()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2191 | result.append(buffer); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2192 | snprintf(buffer, SIZE, " Force use for communications %d\n", |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2193 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2194 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2195 | 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] | 2196 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2197 | 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] | 2198 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2199 | 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] | 2200 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2201 | 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] | 2202 | result.append(buffer); |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 2203 | snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n", |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2204 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO)); |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 2205 | result.append(buffer); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 2206 | snprintf(buffer, SIZE, " Force use for encoded surround output %d\n", |
| 2207 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND)); |
| 2208 | result.append(buffer); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 2209 | snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available"); |
| 2210 | result.append(buffer); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 2211 | snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off"); |
| 2212 | result.append(buffer); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 2213 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2214 | write(fd, result.string(), result.size()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2215 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 2216 | mAvailableOutputDevices.dump(fd, String8("Available output")); |
| 2217 | mAvailableInputDevices.dump(fd, String8("Available input")); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2218 | mHwModules.dump(fd); |
| 2219 | mOutputs.dump(fd); |
| 2220 | mInputs.dump(fd); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2221 | mVolumeCurves->dump(fd); |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2222 | mEffects.dump(fd); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2223 | mAudioPatches.dump(fd); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2224 | |
| 2225 | return NO_ERROR; |
| 2226 | } |
| 2227 | |
| 2228 | // This function checks for the parameters which can be offloaded. |
| 2229 | // This can be enhanced depending on the capability of the DSP and policy |
| 2230 | // of the system. |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2231 | bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2232 | { |
| 2233 | ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2234 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2235 | offloadInfo.sample_rate, offloadInfo.channel_mask, |
| 2236 | offloadInfo.format, |
| 2237 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, |
| 2238 | offloadInfo.has_video); |
| 2239 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 2240 | if (mMasterMono) { |
| 2241 | return false; // no offloading if mono is set. |
| 2242 | } |
| 2243 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2244 | // Check if offload has been disabled |
| 2245 | char propValue[PROPERTY_VALUE_MAX]; |
| 2246 | if (property_get("audio.offload.disable", propValue, "0")) { |
| 2247 | if (atoi(propValue) != 0) { |
| 2248 | ALOGV("offload disabled by audio.offload.disable=%s", propValue ); |
| 2249 | return false; |
| 2250 | } |
| 2251 | } |
| 2252 | |
| 2253 | // Check if stream type is music, then only allow offload as of now. |
| 2254 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) |
| 2255 | { |
| 2256 | ALOGV("isOffloadSupported: stream_type != MUSIC, returning false"); |
| 2257 | return false; |
| 2258 | } |
| 2259 | |
| 2260 | //TODO: enable audio offloading with video when ready |
Andy Hung | 08945c4 | 2015-05-31 21:36:46 -0700 | [diff] [blame] | 2261 | const bool allowOffloadWithVideo = |
| 2262 | property_get_bool("audio.offload.video", false /* default_value */); |
| 2263 | if (offloadInfo.has_video && !allowOffloadWithVideo) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2264 | ALOGV("isOffloadSupported: has_video == true, returning false"); |
| 2265 | return false; |
| 2266 | } |
| 2267 | |
| 2268 | //If duration is less than minimum value defined in property, return false |
| 2269 | if (property_get("audio.offload.min.duration.secs", propValue, NULL)) { |
| 2270 | if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) { |
| 2271 | ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue); |
| 2272 | return false; |
| 2273 | } |
| 2274 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { |
| 2275 | ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS); |
| 2276 | return false; |
| 2277 | } |
| 2278 | |
| 2279 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 2280 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 2281 | // detects there is an active non offloadable effect. |
| 2282 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 2283 | // This may prevent offloading in rare situations where effects are left active by apps |
| 2284 | // in the background. |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2285 | if (mEffects.isNonOffloadableEffectEnabled()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2286 | return false; |
| 2287 | } |
| 2288 | |
| 2289 | // See if there is a profile to support this. |
| 2290 | // AUDIO_DEVICE_NONE |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2291 | sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2292 | offloadInfo.sample_rate, |
| 2293 | offloadInfo.format, |
| 2294 | offloadInfo.channel_mask, |
| 2295 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2296 | ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT "); |
| 2297 | return (profile != 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2298 | } |
| 2299 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2300 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, |
| 2301 | audio_port_type_t type, |
| 2302 | unsigned int *num_ports, |
| 2303 | struct audio_port *ports, |
| 2304 | unsigned int *generation) |
| 2305 | { |
| 2306 | if (num_ports == NULL || (*num_ports != 0 && ports == NULL) || |
| 2307 | generation == NULL) { |
| 2308 | return BAD_VALUE; |
| 2309 | } |
| 2310 | ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports); |
| 2311 | if (ports == NULL) { |
| 2312 | *num_ports = 0; |
| 2313 | } |
| 2314 | |
| 2315 | size_t portsWritten = 0; |
| 2316 | size_t portsMax = *num_ports; |
| 2317 | *num_ports = 0; |
| 2318 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { |
| 2319 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 2320 | for (size_t i = 0; |
| 2321 | i < mAvailableOutputDevices.size() && portsWritten < portsMax; i++) { |
| 2322 | mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]); |
| 2323 | } |
| 2324 | *num_ports += mAvailableOutputDevices.size(); |
| 2325 | } |
| 2326 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
| 2327 | for (size_t i = 0; |
| 2328 | i < mAvailableInputDevices.size() && portsWritten < portsMax; i++) { |
| 2329 | mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]); |
| 2330 | } |
| 2331 | *num_ports += mAvailableInputDevices.size(); |
| 2332 | } |
| 2333 | } |
| 2334 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { |
| 2335 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 2336 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { |
| 2337 | mInputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2338 | } |
| 2339 | *num_ports += mInputs.size(); |
| 2340 | } |
| 2341 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2342 | size_t numOutputs = 0; |
| 2343 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2344 | if (!mOutputs[i]->isDuplicated()) { |
| 2345 | numOutputs++; |
| 2346 | if (portsWritten < portsMax) { |
| 2347 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2348 | } |
| 2349 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2350 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2351 | *num_ports += numOutputs; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2352 | } |
| 2353 | } |
| 2354 | *generation = curAudioPortGeneration(); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2355 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2356 | return NO_ERROR; |
| 2357 | } |
| 2358 | |
| 2359 | status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused) |
| 2360 | { |
| 2361 | return NO_ERROR; |
| 2362 | } |
| 2363 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2364 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, |
| 2365 | audio_patch_handle_t *handle, |
| 2366 | uid_t uid) |
| 2367 | { |
| 2368 | ALOGV("createAudioPatch()"); |
| 2369 | |
| 2370 | if (handle == NULL || patch == NULL) { |
| 2371 | return BAD_VALUE; |
| 2372 | } |
| 2373 | ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks); |
| 2374 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2375 | if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX || |
| 2376 | patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) { |
| 2377 | return BAD_VALUE; |
| 2378 | } |
| 2379 | // only one source per audio patch supported for now |
| 2380 | if (patch->num_sources > 1) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2381 | return INVALID_OPERATION; |
| 2382 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2383 | |
| 2384 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2385 | return INVALID_OPERATION; |
| 2386 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2387 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2388 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { |
| 2389 | return INVALID_OPERATION; |
| 2390 | } |
| 2391 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2392 | |
| 2393 | sp<AudioPatch> patchDesc; |
| 2394 | ssize_t index = mAudioPatches.indexOfKey(*handle); |
| 2395 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2396 | ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id, |
| 2397 | patch->sources[0].role, |
| 2398 | patch->sources[0].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2399 | #if LOG_NDEBUG == 0 |
| 2400 | for (size_t i = 0; i < patch->num_sinks; i++) { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2401 | 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] | 2402 | patch->sinks[i].role, |
| 2403 | patch->sinks[i].type); |
| 2404 | } |
| 2405 | #endif |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2406 | |
| 2407 | if (index >= 0) { |
| 2408 | patchDesc = mAudioPatches.valueAt(index); |
| 2409 | ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 2410 | mUidCached, patchDesc->mUid, uid); |
| 2411 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 2412 | return INVALID_OPERATION; |
| 2413 | } |
| 2414 | } else { |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 2415 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2416 | } |
| 2417 | |
| 2418 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2419 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2420 | if (outputDesc == NULL) { |
| 2421 | ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id); |
| 2422 | return BAD_VALUE; |
| 2423 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2424 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", |
| 2425 | outputDesc->mIoHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2426 | if (patchDesc != 0) { |
| 2427 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
| 2428 | ALOGV("createAudioPatch() source id differs for patch current id %d new id %d", |
| 2429 | patchDesc->mPatch.sources[0].id, patch->sources[0].id); |
| 2430 | return BAD_VALUE; |
| 2431 | } |
| 2432 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2433 | DeviceVector devices; |
| 2434 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2435 | // Only support mix to devices connection |
| 2436 | // TODO add support for mix to mix connection |
| 2437 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 2438 | ALOGV("createAudioPatch() source mix but sink is not a device"); |
| 2439 | return INVALID_OPERATION; |
| 2440 | } |
| 2441 | sp<DeviceDescriptor> devDesc = |
| 2442 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 2443 | if (devDesc == 0) { |
| 2444 | ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id); |
| 2445 | return BAD_VALUE; |
| 2446 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2447 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2448 | if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2449 | devDesc->mAddress, |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2450 | patch->sources[0].sample_rate, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2451 | NULL, // updatedSamplingRate |
| 2452 | patch->sources[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2453 | NULL, // updatedFormat |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2454 | patch->sources[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2455 | NULL, // updatedChannelMask |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2456 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2457 | ALOGV("createAudioPatch() profile not supported for device %08x", |
| 2458 | devDesc->type()); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2459 | return INVALID_OPERATION; |
| 2460 | } |
| 2461 | devices.add(devDesc); |
| 2462 | } |
| 2463 | if (devices.size() == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2464 | return INVALID_OPERATION; |
| 2465 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2466 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2467 | // TODO: reconfigure output format and channels here |
| 2468 | ALOGV("createAudioPatch() setting device %08x on output %d", |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2469 | devices.types(), outputDesc->mIoHandle); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2470 | setOutputDevice(outputDesc, devices.types(), true, 0, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2471 | index = mAudioPatches.indexOfKey(*handle); |
| 2472 | if (index >= 0) { |
| 2473 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 2474 | ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided"); |
| 2475 | } |
| 2476 | patchDesc = mAudioPatches.valueAt(index); |
| 2477 | patchDesc->mUid = uid; |
| 2478 | ALOGV("createAudioPatch() success"); |
| 2479 | } else { |
| 2480 | ALOGW("createAudioPatch() setOutputDevice() failed to create a patch"); |
| 2481 | return INVALID_OPERATION; |
| 2482 | } |
| 2483 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2484 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 2485 | // input device to input mix connection |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2486 | // only one sink supported when connecting an input device to a mix |
| 2487 | if (patch->num_sinks > 1) { |
| 2488 | return INVALID_OPERATION; |
| 2489 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2490 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2491 | if (inputDesc == NULL) { |
| 2492 | return BAD_VALUE; |
| 2493 | } |
| 2494 | if (patchDesc != 0) { |
| 2495 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { |
| 2496 | return BAD_VALUE; |
| 2497 | } |
| 2498 | } |
| 2499 | sp<DeviceDescriptor> devDesc = |
| 2500 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
| 2501 | if (devDesc == 0) { |
| 2502 | return BAD_VALUE; |
| 2503 | } |
| 2504 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2505 | if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2506 | devDesc->mAddress, |
| 2507 | patch->sinks[0].sample_rate, |
| 2508 | NULL, /*updatedSampleRate*/ |
| 2509 | patch->sinks[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2510 | NULL, /*updatedFormat*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2511 | patch->sinks[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2512 | NULL, /*updatedChannelMask*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2513 | // FIXME for the parameter type, |
| 2514 | // and the NONE |
| 2515 | (audio_output_flags_t) |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2516 | AUDIO_INPUT_FLAG_NONE)) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2517 | return INVALID_OPERATION; |
| 2518 | } |
| 2519 | // TODO: reconfigure output format and channels here |
| 2520 | ALOGV("createAudioPatch() setting device %08x on output %d", |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2521 | devDesc->type(), inputDesc->mIoHandle); |
| 2522 | setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2523 | index = mAudioPatches.indexOfKey(*handle); |
| 2524 | if (index >= 0) { |
| 2525 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 2526 | ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided"); |
| 2527 | } |
| 2528 | patchDesc = mAudioPatches.valueAt(index); |
| 2529 | patchDesc->mUid = uid; |
| 2530 | ALOGV("createAudioPatch() success"); |
| 2531 | } else { |
| 2532 | ALOGW("createAudioPatch() setInputDevice() failed to create a patch"); |
| 2533 | return INVALID_OPERATION; |
| 2534 | } |
| 2535 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2536 | // device to device connection |
| 2537 | if (patchDesc != 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2538 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2539 | return BAD_VALUE; |
| 2540 | } |
| 2541 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2542 | sp<DeviceDescriptor> srcDeviceDesc = |
| 2543 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 2544 | if (srcDeviceDesc == 0) { |
| 2545 | return BAD_VALUE; |
| 2546 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2547 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2548 | //update source and sink with our own data as the data passed in the patch may |
| 2549 | // be incomplete. |
| 2550 | struct audio_patch newPatch = *patch; |
| 2551 | srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2552 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2553 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2554 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 2555 | ALOGV("createAudioPatch() source device but one sink is not a device"); |
| 2556 | return INVALID_OPERATION; |
| 2557 | } |
| 2558 | |
| 2559 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 2560 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 2561 | if (sinkDeviceDesc == 0) { |
| 2562 | return BAD_VALUE; |
| 2563 | } |
| 2564 | sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]); |
| 2565 | |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2566 | // create a software bridge in PatchPanel if: |
| 2567 | // - source and sink devices are on differnt HW modules OR |
| 2568 | // - audio HAL version is < 3.0 |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 2569 | if ((srcDeviceDesc->getModuleHandle() != sinkDeviceDesc->getModuleHandle()) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 2570 | (srcDeviceDesc->mModule->getHalVersion() < AUDIO_DEVICE_API_VERSION_3_0)) { |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2571 | // support only one sink device for now to simplify output selection logic |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2572 | if (patch->num_sinks > 1) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2573 | return INVALID_OPERATION; |
| 2574 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2575 | SortedVector<audio_io_handle_t> outputs = |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2576 | getOutputsForDevice(sinkDeviceDesc->type(), mOutputs); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2577 | // if the sink device is reachable via an opened output stream, request to go via |
| 2578 | // this output stream by adding a second source to the patch description |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 2579 | audio_io_handle_t output = selectOutput(outputs, |
| 2580 | AUDIO_OUTPUT_FLAG_NONE, |
| 2581 | AUDIO_FORMAT_INVALID); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2582 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 2583 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 2584 | if (outputDesc->isDuplicated()) { |
| 2585 | return INVALID_OPERATION; |
| 2586 | } |
| 2587 | outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2588 | newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2589 | newPatch.num_sources = 2; |
| 2590 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2591 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2592 | } |
| 2593 | // TODO: check from routing capabilities in config file and other conflicting patches |
| 2594 | |
| 2595 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 2596 | if (index >= 0) { |
| 2597 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 2598 | } |
| 2599 | |
| 2600 | status_t status = mpClientInterface->createAudioPatch(&newPatch, |
| 2601 | &afPatchHandle, |
| 2602 | 0); |
| 2603 | ALOGV("createAudioPatch() patch panel returned %d patchHandle %d", |
| 2604 | status, afPatchHandle); |
| 2605 | if (status == NO_ERROR) { |
| 2606 | if (index < 0) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 2607 | patchDesc = new AudioPatch(&newPatch, uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2608 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 2609 | } else { |
| 2610 | patchDesc->mPatch = newPatch; |
| 2611 | } |
| 2612 | patchDesc->mAfPatchHandle = afPatchHandle; |
| 2613 | *handle = patchDesc->mHandle; |
| 2614 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2615 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2616 | } else { |
| 2617 | ALOGW("createAudioPatch() patch panel could not connect device patch, error %d", |
| 2618 | status); |
| 2619 | return INVALID_OPERATION; |
| 2620 | } |
| 2621 | } else { |
| 2622 | return BAD_VALUE; |
| 2623 | } |
| 2624 | } else { |
| 2625 | return BAD_VALUE; |
| 2626 | } |
| 2627 | return NO_ERROR; |
| 2628 | } |
| 2629 | |
| 2630 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, |
| 2631 | uid_t uid) |
| 2632 | { |
| 2633 | ALOGV("releaseAudioPatch() patch %d", handle); |
| 2634 | |
| 2635 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 2636 | |
| 2637 | if (index < 0) { |
| 2638 | return BAD_VALUE; |
| 2639 | } |
| 2640 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 2641 | ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 2642 | mUidCached, patchDesc->mUid, uid); |
| 2643 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 2644 | return INVALID_OPERATION; |
| 2645 | } |
| 2646 | |
| 2647 | struct audio_patch *patch = &patchDesc->mPatch; |
| 2648 | patchDesc->mUid = mUidCached; |
| 2649 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2650 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2651 | if (outputDesc == NULL) { |
| 2652 | ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id); |
| 2653 | return BAD_VALUE; |
| 2654 | } |
| 2655 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2656 | setOutputDevice(outputDesc, |
| 2657 | getNewOutputDevice(outputDesc, true /*fromCache*/), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2658 | true, |
| 2659 | 0, |
| 2660 | NULL); |
| 2661 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2662 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2663 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2664 | if (inputDesc == NULL) { |
| 2665 | ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id); |
| 2666 | return BAD_VALUE; |
| 2667 | } |
| 2668 | setInputDevice(inputDesc->mIoHandle, |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 2669 | getNewInputDevice(inputDesc->mIoHandle), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2670 | true, |
| 2671 | NULL); |
| 2672 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2673 | audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle; |
| 2674 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 2675 | ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d", |
| 2676 | status, patchDesc->mAfPatchHandle); |
| 2677 | removeAudioPatch(patchDesc->mHandle); |
| 2678 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2679 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2680 | } else { |
| 2681 | return BAD_VALUE; |
| 2682 | } |
| 2683 | } else { |
| 2684 | return BAD_VALUE; |
| 2685 | } |
| 2686 | return NO_ERROR; |
| 2687 | } |
| 2688 | |
| 2689 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, |
| 2690 | struct audio_patch *patches, |
| 2691 | unsigned int *generation) |
| 2692 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2693 | if (generation == NULL) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2694 | return BAD_VALUE; |
| 2695 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2696 | *generation = curAudioPortGeneration(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2697 | return mAudioPatches.listAudioPatches(num_patches, patches); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2698 | } |
| 2699 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2700 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2701 | { |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2702 | ALOGV("setAudioPortConfig()"); |
| 2703 | |
| 2704 | if (config == NULL) { |
| 2705 | return BAD_VALUE; |
| 2706 | } |
| 2707 | ALOGV("setAudioPortConfig() on port handle %d", config->id); |
| 2708 | // Only support gain configuration for now |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2709 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { |
| 2710 | return INVALID_OPERATION; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2711 | } |
| 2712 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2713 | sp<AudioPortConfig> audioPortConfig; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2714 | if (config->type == AUDIO_PORT_TYPE_MIX) { |
| 2715 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2716 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2717 | if (outputDesc == NULL) { |
| 2718 | return BAD_VALUE; |
| 2719 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2720 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 2721 | "setAudioPortConfig() called on duplicated output %d", |
| 2722 | outputDesc->mIoHandle); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2723 | audioPortConfig = outputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2724 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2725 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2726 | if (inputDesc == NULL) { |
| 2727 | return BAD_VALUE; |
| 2728 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2729 | audioPortConfig = inputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2730 | } else { |
| 2731 | return BAD_VALUE; |
| 2732 | } |
| 2733 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { |
| 2734 | sp<DeviceDescriptor> deviceDesc; |
| 2735 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
| 2736 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); |
| 2737 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
| 2738 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); |
| 2739 | } else { |
| 2740 | return BAD_VALUE; |
| 2741 | } |
| 2742 | if (deviceDesc == NULL) { |
| 2743 | return BAD_VALUE; |
| 2744 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2745 | audioPortConfig = deviceDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2746 | } else { |
| 2747 | return BAD_VALUE; |
| 2748 | } |
| 2749 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2750 | struct audio_port_config backupConfig; |
| 2751 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); |
| 2752 | if (status == NO_ERROR) { |
| 2753 | struct audio_port_config newConfig; |
| 2754 | audioPortConfig->toAudioPortConfig(&newConfig, config); |
| 2755 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2756 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2757 | if (status != NO_ERROR) { |
| 2758 | audioPortConfig->applyAudioPortConfig(&backupConfig); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2759 | } |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2760 | |
| 2761 | return status; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2762 | } |
| 2763 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2764 | void AudioPolicyManager::releaseResourcesForUid(uid_t uid) |
| 2765 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2766 | clearAudioSources(uid); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2767 | clearAudioPatches(uid); |
| 2768 | clearSessionRoutes(uid); |
| 2769 | } |
| 2770 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2771 | void AudioPolicyManager::clearAudioPatches(uid_t uid) |
| 2772 | { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 2773 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2774 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 2775 | if (patchDesc->mUid == uid) { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 2776 | releaseAudioPatch(mAudioPatches.keyAt(i), uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2777 | } |
| 2778 | } |
| 2779 | } |
| 2780 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2781 | void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy, |
| 2782 | audio_io_handle_t ouptutToSkip) |
| 2783 | { |
| 2784 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 2785 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 2786 | for (size_t j = 0; j < mOutputs.size(); j++) { |
| 2787 | if (mOutputs.keyAt(j) == ouptutToSkip) { |
| 2788 | continue; |
| 2789 | } |
| 2790 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j); |
| 2791 | if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) { |
| 2792 | continue; |
| 2793 | } |
| 2794 | // If the default device for this strategy is on another output mix, |
| 2795 | // invalidate all tracks in this strategy to force re connection. |
| 2796 | // Otherwise select new device on the output mix. |
| 2797 | if (outputs.indexOf(mOutputs.keyAt(j)) < 0) { |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2798 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2799 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
| 2800 | mpClientInterface->invalidateStream((audio_stream_type_t)stream); |
| 2801 | } |
| 2802 | } |
| 2803 | } else { |
| 2804 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
| 2805 | setOutputDevice(outputDesc, newDevice, false); |
| 2806 | } |
| 2807 | } |
| 2808 | } |
| 2809 | |
| 2810 | void AudioPolicyManager::clearSessionRoutes(uid_t uid) |
| 2811 | { |
| 2812 | // remove output routes associated with this uid |
| 2813 | SortedVector<routing_strategy> affectedStrategies; |
| 2814 | for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) { |
| 2815 | sp<SessionRoute> route = mOutputRoutes.valueAt(i); |
| 2816 | if (route->mUid == uid) { |
| 2817 | mOutputRoutes.removeItemsAt(i); |
| 2818 | if (route->mDeviceDescriptor != 0) { |
| 2819 | affectedStrategies.add(getStrategy(route->mStreamType)); |
| 2820 | } |
| 2821 | } |
| 2822 | } |
| 2823 | // reroute outputs if necessary |
| 2824 | for (size_t i = 0; i < affectedStrategies.size(); i++) { |
| 2825 | checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE); |
| 2826 | } |
| 2827 | |
| 2828 | // remove input routes associated with this uid |
| 2829 | SortedVector<audio_source_t> affectedSources; |
| 2830 | for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) { |
| 2831 | sp<SessionRoute> route = mInputRoutes.valueAt(i); |
| 2832 | if (route->mUid == uid) { |
| 2833 | mInputRoutes.removeItemsAt(i); |
| 2834 | if (route->mDeviceDescriptor != 0) { |
| 2835 | affectedSources.add(route->mSource); |
| 2836 | } |
| 2837 | } |
| 2838 | } |
| 2839 | // reroute inputs if necessary |
| 2840 | SortedVector<audio_io_handle_t> inputsToClose; |
| 2841 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 2842 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2843 | if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2844 | inputsToClose.add(inputDesc->mIoHandle); |
| 2845 | } |
| 2846 | } |
| 2847 | for (size_t i = 0; i < inputsToClose.size(); i++) { |
| 2848 | closeInput(inputsToClose[i]); |
| 2849 | } |
| 2850 | } |
| 2851 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2852 | void AudioPolicyManager::clearAudioSources(uid_t uid) |
| 2853 | { |
| 2854 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
| 2855 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 2856 | if (sourceDesc->mUid == uid) { |
| 2857 | stopAudioSource(mAudioSources.keyAt(i)); |
| 2858 | } |
| 2859 | } |
| 2860 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2861 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2862 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, |
| 2863 | audio_io_handle_t *ioHandle, |
| 2864 | audio_devices_t *device) |
| 2865 | { |
Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 2866 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); |
| 2867 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT); |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 2868 | *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2869 | |
François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 2870 | return mSoundTriggerSessions.acquireSession(*session, *ioHandle); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2871 | } |
| 2872 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2873 | status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source, |
| 2874 | const audio_attributes_t *attributes, |
| 2875 | audio_io_handle_t *handle, |
| 2876 | uid_t uid) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 2877 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2878 | ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle); |
| 2879 | if (source == NULL || attributes == NULL || handle == NULL) { |
| 2880 | return BAD_VALUE; |
| 2881 | } |
| 2882 | |
| 2883 | *handle = AUDIO_IO_HANDLE_NONE; |
| 2884 | |
| 2885 | if (source->role != AUDIO_PORT_ROLE_SOURCE || |
| 2886 | source->type != AUDIO_PORT_TYPE_DEVICE) { |
| 2887 | ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type); |
| 2888 | return INVALID_OPERATION; |
| 2889 | } |
| 2890 | |
| 2891 | sp<DeviceDescriptor> srcDeviceDesc = |
| 2892 | mAvailableInputDevices.getDevice(source->ext.device.type, |
| 2893 | String8(source->ext.device.address)); |
| 2894 | if (srcDeviceDesc == 0) { |
| 2895 | ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type); |
| 2896 | return BAD_VALUE; |
| 2897 | } |
| 2898 | sp<AudioSourceDescriptor> sourceDesc = |
| 2899 | new AudioSourceDescriptor(srcDeviceDesc, attributes, uid); |
| 2900 | |
| 2901 | struct audio_patch dummyPatch; |
| 2902 | sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid); |
| 2903 | sourceDesc->mPatchDesc = patchDesc; |
| 2904 | |
| 2905 | status_t status = connectAudioSource(sourceDesc); |
| 2906 | if (status == NO_ERROR) { |
| 2907 | mAudioSources.add(sourceDesc->getHandle(), sourceDesc); |
| 2908 | *handle = sourceDesc->getHandle(); |
| 2909 | } |
| 2910 | return status; |
| 2911 | } |
| 2912 | |
| 2913 | status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc) |
| 2914 | { |
| 2915 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle()); |
| 2916 | |
| 2917 | // make sure we only have one patch per source. |
| 2918 | disconnectAudioSource(sourceDesc); |
| 2919 | |
| 2920 | routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2921 | audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2922 | sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice; |
| 2923 | |
| 2924 | audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true); |
| 2925 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 2926 | mAvailableOutputDevices.getDevice(sinkDevice, String8("")); |
| 2927 | |
| 2928 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 2929 | struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch; |
| 2930 | |
| 2931 | if (srcDeviceDesc->getAudioPort()->mModule->getHandle() == |
| 2932 | sinkDeviceDesc->getAudioPort()->mModule->getHandle() && |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 2933 | srcDeviceDesc->getAudioPort()->mModule->getHalVersion() >= AUDIO_DEVICE_API_VERSION_3_0 && |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2934 | srcDeviceDesc->getAudioPort()->mGains.size() > 0) { |
| 2935 | ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__); |
| 2936 | // create patch between src device and output device |
| 2937 | // create Hwoutput and add to mHwOutputs |
| 2938 | } else { |
| 2939 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs); |
| 2940 | audio_io_handle_t output = |
| 2941 | selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID); |
| 2942 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 2943 | ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice); |
| 2944 | return INVALID_OPERATION; |
| 2945 | } |
| 2946 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 2947 | if (outputDesc->isDuplicated()) { |
| 2948 | ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice); |
| 2949 | return INVALID_OPERATION; |
| 2950 | } |
| 2951 | // create a special patch with no sink and two sources: |
| 2952 | // - the second source indicates to PatchPanel through which output mix this patch should |
| 2953 | // be connected as well as the stream type for volume control |
| 2954 | // - the sink is defined by whatever output device is currently selected for the output |
| 2955 | // though which this patch is routed. |
| 2956 | patch->num_sinks = 0; |
| 2957 | patch->num_sources = 2; |
| 2958 | srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL); |
| 2959 | outputDesc->toAudioPortConfig(&patch->sources[1], NULL); |
| 2960 | patch->sources[1].ext.mix.usecase.stream = stream; |
| 2961 | status_t status = mpClientInterface->createAudioPatch(patch, |
| 2962 | &afPatchHandle, |
| 2963 | 0); |
| 2964 | ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__, |
| 2965 | status, afPatchHandle); |
| 2966 | if (status != NO_ERROR) { |
| 2967 | ALOGW("%s patch panel could not connect device patch, error %d", |
| 2968 | __FUNCTION__, status); |
| 2969 | return INVALID_OPERATION; |
| 2970 | } |
| 2971 | uint32_t delayMs = 0; |
| 2972 | status = startSource(outputDesc, stream, sinkDevice, &delayMs); |
| 2973 | |
| 2974 | if (status != NO_ERROR) { |
| 2975 | mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0); |
| 2976 | return status; |
| 2977 | } |
| 2978 | sourceDesc->mSwOutput = outputDesc; |
| 2979 | if (delayMs != 0) { |
| 2980 | usleep(delayMs * 1000); |
| 2981 | } |
| 2982 | } |
| 2983 | |
| 2984 | sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle; |
| 2985 | addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc); |
| 2986 | |
| 2987 | return NO_ERROR; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 2988 | } |
| 2989 | |
Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 2990 | status_t AudioPolicyManager::stopAudioSource(audio_io_handle_t handle __unused) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 2991 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2992 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle); |
| 2993 | ALOGV("%s handle %d", __FUNCTION__, handle); |
| 2994 | if (sourceDesc == 0) { |
| 2995 | ALOGW("%s unknown source for handle %d", __FUNCTION__, handle); |
| 2996 | return BAD_VALUE; |
| 2997 | } |
| 2998 | status_t status = disconnectAudioSource(sourceDesc); |
| 2999 | |
| 3000 | mAudioSources.removeItem(handle); |
| 3001 | return status; |
| 3002 | } |
| 3003 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3004 | status_t AudioPolicyManager::setMasterMono(bool mono) |
| 3005 | { |
| 3006 | if (mMasterMono == mono) { |
| 3007 | return NO_ERROR; |
| 3008 | } |
| 3009 | mMasterMono = mono; |
| 3010 | // if enabling mono we close all offloaded devices, which will invalidate the |
| 3011 | // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible |
| 3012 | // for recreating the new AudioTrack as non-offloaded PCM. |
| 3013 | // |
| 3014 | // If disabling mono, we leave all tracks as is: we don't know which clients |
| 3015 | // and tracks are able to be recreated as offloaded. The next "song" should |
| 3016 | // play back offloaded. |
| 3017 | if (mMasterMono) { |
| 3018 | Vector<audio_io_handle_t> offloaded; |
| 3019 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3020 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 3021 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
| 3022 | offloaded.push(desc->mIoHandle); |
| 3023 | } |
| 3024 | } |
| 3025 | for (size_t i = 0; i < offloaded.size(); ++i) { |
| 3026 | closeOutput(offloaded[i]); |
| 3027 | } |
| 3028 | } |
| 3029 | // update master mono for all remaining outputs |
| 3030 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3031 | updateMono(mOutputs.keyAt(i)); |
| 3032 | } |
| 3033 | return NO_ERROR; |
| 3034 | } |
| 3035 | |
| 3036 | status_t AudioPolicyManager::getMasterMono(bool *mono) |
| 3037 | { |
| 3038 | *mono = mMasterMono; |
| 3039 | return NO_ERROR; |
| 3040 | } |
| 3041 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3042 | status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc) |
| 3043 | { |
| 3044 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle()); |
| 3045 | |
| 3046 | sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle); |
| 3047 | if (patchDesc == 0) { |
| 3048 | ALOGW("%s source has no patch with handle %d", __FUNCTION__, |
| 3049 | sourceDesc->mPatchDesc->mHandle); |
| 3050 | return BAD_VALUE; |
| 3051 | } |
| 3052 | removeAudioPatch(sourceDesc->mPatchDesc->mHandle); |
| 3053 | |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 3054 | audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3055 | sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote(); |
| 3056 | if (swOutputDesc != 0) { |
| 3057 | stopSource(swOutputDesc, stream, false); |
| 3058 | mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3059 | } else { |
| 3060 | sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote(); |
| 3061 | if (hwOutputDesc != 0) { |
| 3062 | // release patch between src device and output device |
| 3063 | // close Hwoutput and remove from mHwOutputs |
| 3064 | } else { |
| 3065 | ALOGW("%s source has neither SW nor HW output", __FUNCTION__); |
| 3066 | } |
| 3067 | } |
| 3068 | return NO_ERROR; |
| 3069 | } |
| 3070 | |
| 3071 | sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput( |
| 3072 | audio_io_handle_t output, routing_strategy strategy) |
| 3073 | { |
| 3074 | sp<AudioSourceDescriptor> source; |
| 3075 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
| 3076 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 3077 | routing_strategy sourceStrategy = |
| 3078 | (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes); |
| 3079 | sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote(); |
| 3080 | if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) { |
| 3081 | source = sourceDesc; |
| 3082 | break; |
| 3083 | } |
| 3084 | } |
| 3085 | return source; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3086 | } |
| 3087 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3088 | // ---------------------------------------------------------------------------- |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3089 | // AudioPolicyManager |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3090 | // ---------------------------------------------------------------------------- |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3091 | uint32_t AudioPolicyManager::nextAudioPortGeneration() |
| 3092 | { |
| 3093 | return android_atomic_inc(&mAudioPortGeneration); |
| 3094 | } |
| 3095 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3096 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3097 | : |
| 3098 | #ifdef AUDIO_POLICY_TEST |
| 3099 | Thread(false), |
| 3100 | #endif //AUDIO_POLICY_TEST |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3101 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3102 | mA2dpSuspended(false), |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 3103 | mAudioPortGeneration(1), |
| 3104 | mBeaconMuteRefCount(0), |
| 3105 | mBeaconPlayingRefCount(0), |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3106 | mBeaconMuted(false), |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3107 | mTtsOutputAvailable(false), |
| 3108 | mMasterMono(false) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3109 | { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3110 | mUidCached = getuid(); |
| 3111 | mpClientInterface = clientInterface; |
| 3112 | |
| 3113 | // TODO: remove when legacy conf file is removed. true on devices that use DRC on the |
| 3114 | // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly. |
| 3115 | // Note: remove also speaker_drc_enabled from global configuration of XML config file. |
| 3116 | bool speakerDrcEnabled = false; |
| 3117 | |
| 3118 | #ifdef USE_XML_AUDIO_POLICY_CONF |
| 3119 | mVolumeCurves = new VolumeCurvesCollection(); |
| 3120 | AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices, |
| 3121 | mDefaultOutputDevice, speakerDrcEnabled, |
| 3122 | static_cast<VolumeCurvesCollection *>(mVolumeCurves)); |
| 3123 | PolicySerializer serializer; |
| 3124 | if (serializer.deserialize(AUDIO_POLICY_XML_CONFIG_FILE, config) != NO_ERROR) { |
| 3125 | #else |
| 3126 | mVolumeCurves = new StreamDescriptorCollection(); |
| 3127 | AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices, |
| 3128 | mDefaultOutputDevice, speakerDrcEnabled); |
| 3129 | if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, config) != NO_ERROR) && |
| 3130 | (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, config) != NO_ERROR)) { |
| 3131 | #endif |
| 3132 | ALOGE("could not load audio policy configuration file, setting defaults"); |
| 3133 | config.setDefault(); |
| 3134 | } |
| 3135 | // must be done after reading the policy (since conditionned by Speaker Drc Enabling) |
| 3136 | mVolumeCurves->initializeVolumeCurves(speakerDrcEnabled); |
| 3137 | |
| 3138 | // 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] | 3139 | audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance(); |
| 3140 | if (!engineInstance) { |
| 3141 | ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__); |
| 3142 | return; |
| 3143 | } |
| 3144 | // Retrieve the Policy Manager Interface |
| 3145 | mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>(); |
| 3146 | if (mEngine == NULL) { |
| 3147 | ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__); |
| 3148 | return; |
| 3149 | } |
| 3150 | mEngine->setObserver(this); |
| 3151 | status_t status = mEngine->initCheck(); |
| 3152 | ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status); |
| 3153 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3154 | // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3155 | // open all output streams needed to access attached devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3156 | audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types(); |
| 3157 | audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3158 | for (size_t i = 0; i < mHwModules.size(); i++) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3159 | mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3160 | if (mHwModules[i]->mHandle == 0) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3161 | ALOGW("could not open HW module %s", mHwModules[i]->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3162 | continue; |
| 3163 | } |
| 3164 | // open all output streams needed to access attached devices |
| 3165 | // except for direct output streams that are only opened when they are actually |
| 3166 | // required by an app. |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3167 | // This also validates mAvailableOutputDevices list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3168 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 3169 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3170 | const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3171 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3172 | if (!outProfile->hasSupportedDevices()) { |
| 3173 | ALOGW("Output profile contains no device on module %s", mHwModules[i]->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3174 | continue; |
| 3175 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3176 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3177 | mTtsOutputAvailable = true; |
| 3178 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3179 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3180 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3181 | continue; |
| 3182 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3183 | audio_devices_t profileType = outProfile->getSupportedDevicesType(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3184 | if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) { |
| 3185 | profileType = mDefaultOutputDevice->type(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3186 | } else { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3187 | // chose first device present in profile's SupportedDevices also part of |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3188 | // outputDeviceTypes |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3189 | profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3190 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3191 | if ((profileType & outputDeviceTypes) == 0) { |
| 3192 | continue; |
| 3193 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3194 | sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile, |
| 3195 | mpClientInterface); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3196 | |
| 3197 | outputDesc->mDevice = profileType; |
| 3198 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3199 | config.sample_rate = outputDesc->mSamplingRate; |
| 3200 | config.channel_mask = outputDesc->mChannelMask; |
| 3201 | config.format = outputDesc->mFormat; |
| 3202 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3203 | status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(), |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3204 | &output, |
| 3205 | &config, |
| 3206 | &outputDesc->mDevice, |
| 3207 | String8(""), |
| 3208 | &outputDesc->mLatency, |
| 3209 | outputDesc->mFlags); |
| 3210 | |
| 3211 | if (status != NO_ERROR) { |
| 3212 | ALOGW("Cannot open output stream for device %08x on hw module %s", |
| 3213 | outputDesc->mDevice, |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3214 | mHwModules[i]->getName()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3215 | } else { |
| 3216 | outputDesc->mSamplingRate = config.sample_rate; |
| 3217 | outputDesc->mChannelMask = config.channel_mask; |
| 3218 | outputDesc->mFormat = config.format; |
| 3219 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3220 | const DeviceVector &supportedDevices = outProfile->getSupportedDevices(); |
| 3221 | for (size_t k = 0; k < supportedDevices.size(); k++) { |
| 3222 | ssize_t index = mAvailableOutputDevices.indexOf(supportedDevices[k]); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3223 | // 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] | 3224 | if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) { |
| 3225 | mAvailableOutputDevices[index]->attach(mHwModules[i]); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3226 | } |
| 3227 | } |
| 3228 | if (mPrimaryOutput == 0 && |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3229 | outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3230 | mPrimaryOutput = outputDesc; |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3231 | } |
| 3232 | addOutput(output, outputDesc); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3233 | setOutputDevice(outputDesc, |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3234 | outputDesc->mDevice, |
| 3235 | true); |
| 3236 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3237 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3238 | // open input streams needed to access attached devices to validate |
| 3239 | // mAvailableInputDevices list |
| 3240 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) |
| 3241 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3242 | const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3243 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3244 | if (!inProfile->hasSupportedDevices()) { |
| 3245 | ALOGW("Input profile contains no device on module %s", mHwModules[i]->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3246 | continue; |
| 3247 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3248 | // chose first device present in profile's SupportedDevices also part of |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3249 | // inputDeviceTypes |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3250 | audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes); |
| 3251 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3252 | if ((profileType & inputDeviceTypes) == 0) { |
| 3253 | continue; |
| 3254 | } |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 3255 | sp<AudioInputDescriptor> inputDesc = |
| 3256 | new AudioInputDescriptor(inProfile); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3257 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3258 | inputDesc->mDevice = profileType; |
| 3259 | |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 3260 | // find the address |
| 3261 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType); |
| 3262 | // the inputs vector must be of size 1, but we don't want to crash here |
| 3263 | String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress |
| 3264 | : String8(""); |
| 3265 | ALOGV(" for input device 0x%x using address %s", profileType, address.string()); |
| 3266 | ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!"); |
| 3267 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3268 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3269 | config.sample_rate = inputDesc->mSamplingRate; |
| 3270 | config.channel_mask = inputDesc->mChannelMask; |
| 3271 | config.format = inputDesc->mFormat; |
| 3272 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3273 | status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(), |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3274 | &input, |
| 3275 | &config, |
| 3276 | &inputDesc->mDevice, |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 3277 | address, |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3278 | AUDIO_SOURCE_MIC, |
| 3279 | AUDIO_INPUT_FLAG_NONE); |
| 3280 | |
| 3281 | if (status == NO_ERROR) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3282 | const DeviceVector &supportedDevices = inProfile->getSupportedDevices(); |
| 3283 | for (size_t k = 0; k < supportedDevices.size(); k++) { |
| 3284 | ssize_t index = mAvailableInputDevices.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 |
Eric Laurent | 45aabc3 | 2015-08-06 09:11:13 -0700 | [diff] [blame] | 3286 | if (index >= 0) { |
| 3287 | sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index]; |
| 3288 | if (!devDesc->isAttached()) { |
| 3289 | devDesc->attach(mHwModules[i]); |
| 3290 | devDesc->importAudioPort(inProfile); |
| 3291 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3292 | } |
| 3293 | } |
| 3294 | mpClientInterface->closeInput(input); |
| 3295 | } else { |
| 3296 | ALOGW("Cannot open input stream for device %08x on hw module %s", |
| 3297 | inputDesc->mDevice, |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3298 | mHwModules[i]->getName()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3299 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3300 | } |
| 3301 | } |
| 3302 | // make sure all attached devices have been allocated a unique ID |
| 3303 | for (size_t i = 0; i < mAvailableOutputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 3304 | if (!mAvailableOutputDevices[i]->isAttached()) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3305 | ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3306 | mAvailableOutputDevices.remove(mAvailableOutputDevices[i]); |
| 3307 | continue; |
| 3308 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3309 | // The device is now validated and can be appended to the available devices of the engine |
| 3310 | mEngine->setDeviceConnectionState(mAvailableOutputDevices[i], |
| 3311 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3312 | i++; |
| 3313 | } |
| 3314 | for (size_t i = 0; i < mAvailableInputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 3315 | if (!mAvailableInputDevices[i]->isAttached()) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3316 | ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3317 | mAvailableInputDevices.remove(mAvailableInputDevices[i]); |
| 3318 | continue; |
| 3319 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3320 | // The device is now validated and can be appended to the available devices of the engine |
| 3321 | mEngine->setDeviceConnectionState(mAvailableInputDevices[i], |
| 3322 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3323 | i++; |
| 3324 | } |
| 3325 | // make sure default device is reachable |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3326 | if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3327 | ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3328 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3329 | |
| 3330 | ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output"); |
| 3331 | |
| 3332 | updateDevicesAndOutputs(); |
| 3333 | |
| 3334 | #ifdef AUDIO_POLICY_TEST |
| 3335 | if (mPrimaryOutput != 0) { |
| 3336 | AudioParameter outputCmd = AudioParameter(); |
| 3337 | outputCmd.addInt(String8("set_id"), 0); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3338 | mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3339 | |
| 3340 | mTestDevice = AUDIO_DEVICE_OUT_SPEAKER; |
| 3341 | mTestSamplingRate = 44100; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3342 | mTestFormat = AUDIO_FORMAT_PCM_16_BIT; |
| 3343 | mTestChannels = AUDIO_CHANNEL_OUT_STEREO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3344 | mTestLatencyMs = 0; |
| 3345 | mCurOutput = 0; |
| 3346 | mDirectOutput = false; |
| 3347 | for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { |
| 3348 | mTestOutputs[i] = 0; |
| 3349 | } |
| 3350 | |
| 3351 | const size_t SIZE = 256; |
| 3352 | char buffer[SIZE]; |
| 3353 | snprintf(buffer, SIZE, "AudioPolicyManagerTest"); |
| 3354 | run(buffer, ANDROID_PRIORITY_AUDIO); |
| 3355 | } |
| 3356 | #endif //AUDIO_POLICY_TEST |
| 3357 | } |
| 3358 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3359 | AudioPolicyManager::~AudioPolicyManager() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3360 | { |
| 3361 | #ifdef AUDIO_POLICY_TEST |
| 3362 | exit(); |
| 3363 | #endif //AUDIO_POLICY_TEST |
| 3364 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3365 | mpClientInterface->closeOutput(mOutputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3366 | } |
| 3367 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3368 | mpClientInterface->closeInput(mInputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3369 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3370 | mAvailableOutputDevices.clear(); |
| 3371 | mAvailableInputDevices.clear(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3372 | mOutputs.clear(); |
| 3373 | mInputs.clear(); |
| 3374 | mHwModules.clear(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3375 | } |
| 3376 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3377 | status_t AudioPolicyManager::initCheck() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3378 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 3379 | return hasPrimaryOutput() ? NO_ERROR : NO_INIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3380 | } |
| 3381 | |
| 3382 | #ifdef AUDIO_POLICY_TEST |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3383 | bool AudioPolicyManager::threadLoop() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3384 | { |
| 3385 | ALOGV("entering threadLoop()"); |
| 3386 | while (!exitPending()) |
| 3387 | { |
| 3388 | String8 command; |
| 3389 | int valueInt; |
| 3390 | String8 value; |
| 3391 | |
| 3392 | Mutex::Autolock _l(mLock); |
| 3393 | mWaitWorkCV.waitRelative(mLock, milliseconds(50)); |
| 3394 | |
| 3395 | command = mpClientInterface->getParameters(0, String8("test_cmd_policy")); |
| 3396 | AudioParameter param = AudioParameter(command); |
| 3397 | |
| 3398 | if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR && |
| 3399 | valueInt != 0) { |
| 3400 | ALOGV("Test command %s received", command.string()); |
| 3401 | String8 target; |
| 3402 | if (param.get(String8("target"), target) != NO_ERROR) { |
| 3403 | target = "Manager"; |
| 3404 | } |
| 3405 | if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) { |
| 3406 | param.remove(String8("test_cmd_policy_output")); |
| 3407 | mCurOutput = valueInt; |
| 3408 | } |
| 3409 | if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) { |
| 3410 | param.remove(String8("test_cmd_policy_direct")); |
| 3411 | if (value == "false") { |
| 3412 | mDirectOutput = false; |
| 3413 | } else if (value == "true") { |
| 3414 | mDirectOutput = true; |
| 3415 | } |
| 3416 | } |
| 3417 | if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) { |
| 3418 | param.remove(String8("test_cmd_policy_input")); |
| 3419 | mTestInput = valueInt; |
| 3420 | } |
| 3421 | |
| 3422 | if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) { |
| 3423 | param.remove(String8("test_cmd_policy_format")); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3424 | int format = AUDIO_FORMAT_INVALID; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3425 | if (value == "PCM 16 bits") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3426 | format = AUDIO_FORMAT_PCM_16_BIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3427 | } else if (value == "PCM 8 bits") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3428 | format = AUDIO_FORMAT_PCM_8_BIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3429 | } else if (value == "Compressed MP3") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3430 | format = AUDIO_FORMAT_MP3; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3431 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3432 | if (format != AUDIO_FORMAT_INVALID) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3433 | if (target == "Manager") { |
| 3434 | mTestFormat = format; |
| 3435 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 3436 | AudioParameter outputParam = AudioParameter(); |
| 3437 | outputParam.addInt(String8("format"), format); |
| 3438 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 3439 | } |
| 3440 | } |
| 3441 | } |
| 3442 | if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) { |
| 3443 | param.remove(String8("test_cmd_policy_channels")); |
| 3444 | int channels = 0; |
| 3445 | |
| 3446 | if (value == "Channels Stereo") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3447 | channels = AUDIO_CHANNEL_OUT_STEREO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3448 | } else if (value == "Channels Mono") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3449 | channels = AUDIO_CHANNEL_OUT_MONO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3450 | } |
| 3451 | if (channels != 0) { |
| 3452 | if (target == "Manager") { |
| 3453 | mTestChannels = channels; |
| 3454 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 3455 | AudioParameter outputParam = AudioParameter(); |
| 3456 | outputParam.addInt(String8("channels"), channels); |
| 3457 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 3458 | } |
| 3459 | } |
| 3460 | } |
| 3461 | if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) { |
| 3462 | param.remove(String8("test_cmd_policy_sampleRate")); |
| 3463 | if (valueInt >= 0 && valueInt <= 96000) { |
| 3464 | int samplingRate = valueInt; |
| 3465 | if (target == "Manager") { |
| 3466 | mTestSamplingRate = samplingRate; |
| 3467 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 3468 | AudioParameter outputParam = AudioParameter(); |
| 3469 | outputParam.addInt(String8("sampling_rate"), samplingRate); |
| 3470 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 3471 | } |
| 3472 | } |
| 3473 | } |
| 3474 | |
| 3475 | if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) { |
| 3476 | param.remove(String8("test_cmd_policy_reopen")); |
| 3477 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3478 | mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput);); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3479 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3480 | audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3481 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3482 | removeOutput(mPrimaryOutput->mIoHandle); |
| 3483 | sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL, |
| 3484 | mpClientInterface); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3485 | outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3486 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3487 | config.sample_rate = outputDesc->mSamplingRate; |
| 3488 | config.channel_mask = outputDesc->mChannelMask; |
| 3489 | config.format = outputDesc->mFormat; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3490 | audio_io_handle_t handle; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3491 | status_t status = mpClientInterface->openOutput(moduleHandle, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3492 | &handle, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3493 | &config, |
| 3494 | &outputDesc->mDevice, |
| 3495 | String8(""), |
| 3496 | &outputDesc->mLatency, |
| 3497 | outputDesc->mFlags); |
| 3498 | if (status != NO_ERROR) { |
| 3499 | ALOGE("Failed to reopen hardware output stream, " |
| 3500 | "samplingRate: %d, format %d, channels %d", |
| 3501 | outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3502 | } else { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3503 | outputDesc->mSamplingRate = config.sample_rate; |
| 3504 | outputDesc->mChannelMask = config.channel_mask; |
| 3505 | outputDesc->mFormat = config.format; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3506 | mPrimaryOutput = outputDesc; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3507 | AudioParameter outputCmd = AudioParameter(); |
| 3508 | outputCmd.addInt(String8("set_id"), 0); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3509 | mpClientInterface->setParameters(handle, outputCmd.toString()); |
| 3510 | addOutput(handle, outputDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3511 | } |
| 3512 | } |
| 3513 | |
| 3514 | |
| 3515 | mpClientInterface->setParameters(0, String8("test_cmd_policy=")); |
| 3516 | } |
| 3517 | } |
| 3518 | return false; |
| 3519 | } |
| 3520 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3521 | void AudioPolicyManager::exit() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3522 | { |
| 3523 | { |
| 3524 | AutoMutex _l(mLock); |
| 3525 | requestExit(); |
| 3526 | mWaitWorkCV.signal(); |
| 3527 | } |
| 3528 | requestExitAndWait(); |
| 3529 | } |
| 3530 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3531 | int AudioPolicyManager::testOutputIndex(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3532 | { |
| 3533 | for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { |
| 3534 | if (output == mTestOutputs[i]) return i; |
| 3535 | } |
| 3536 | return 0; |
| 3537 | } |
| 3538 | #endif //AUDIO_POLICY_TEST |
| 3539 | |
| 3540 | // --- |
| 3541 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3542 | void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3543 | { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 3544 | outputDesc->setIoHandle(output); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3545 | mOutputs.add(output, outputDesc); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3546 | updateMono(output); // update mono status when adding to output list |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3547 | nextAudioPortGeneration(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3548 | } |
| 3549 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3550 | void AudioPolicyManager::removeOutput(audio_io_handle_t output) |
| 3551 | { |
| 3552 | mOutputs.removeItem(output); |
| 3553 | } |
| 3554 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3555 | void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3556 | { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 3557 | inputDesc->setIoHandle(input); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3558 | mInputs.add(input, inputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3559 | nextAudioPortGeneration(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3560 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3561 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3562 | void AudioPolicyManager::findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/, |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3563 | const audio_devices_t device /*in*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3564 | const String8 address /*in*/, |
| 3565 | SortedVector<audio_io_handle_t>& outputs /*out*/) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3566 | sp<DeviceDescriptor> devDesc = |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3567 | desc->mProfile->getSupportedDeviceByAddress(device, address); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3568 | if (devDesc != 0) { |
| 3569 | ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s", |
| 3570 | desc->mIoHandle, address.string()); |
| 3571 | outputs.add(desc->mIoHandle); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3572 | } |
| 3573 | } |
| 3574 | |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3575 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3576 | audio_policy_dev_state_t state, |
| 3577 | SortedVector<audio_io_handle_t>& outputs, |
| 3578 | const String8 address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3579 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3580 | audio_devices_t device = devDesc->type(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3581 | sp<SwAudioOutputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 3582 | |
| 3583 | if (audio_device_is_digital(device)) { |
| 3584 | // erase all current sample rates, formats and channel masks |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 3585 | devDesc->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 3586 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3587 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3588 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3589 | // first list already open outputs that can be routed to this device |
| 3590 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3591 | desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3592 | if (!desc->isDuplicated() && (desc->supportedDevices() & device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3593 | if (!device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3594 | ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i)); |
| 3595 | outputs.add(mOutputs.keyAt(i)); |
| 3596 | } else { |
| 3597 | ALOGV(" checking address match due to device 0x%x", device); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3598 | findIoHandlesByAddress(desc, device, address, outputs); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3599 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3600 | } |
| 3601 | } |
| 3602 | // then look for output profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3603 | SortedVector< sp<IOProfile> > profiles; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3604 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 3605 | { |
| 3606 | if (mHwModules[i]->mHandle == 0) { |
| 3607 | continue; |
| 3608 | } |
| 3609 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 3610 | { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3611 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3612 | if (profile->supportDevice(device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3613 | if (!device_distinguishes_on_address(device) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3614 | profile->supportDeviceAddress(address)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3615 | profiles.add(profile); |
| 3616 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i); |
| 3617 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3618 | } |
| 3619 | } |
| 3620 | } |
| 3621 | |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 3622 | ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size()); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3623 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3624 | if (profiles.isEmpty() && outputs.isEmpty()) { |
| 3625 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 3626 | return BAD_VALUE; |
| 3627 | } |
| 3628 | |
| 3629 | // open outputs for matching profiles if needed. Direct outputs are also opened to |
| 3630 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 3631 | 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] | 3632 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3633 | |
| 3634 | // nothing to do if one output is already opened for this profile |
| 3635 | size_t j; |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3636 | for (j = 0; j < outputs.size(); j++) { |
| 3637 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3638 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3639 | // matching profile: save the sample rates, format and channel masks supported |
| 3640 | // by the profile in our device descriptor |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3641 | if (audio_device_is_digital(device)) { |
| 3642 | devDesc->importAudioPort(profile); |
| 3643 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3644 | break; |
| 3645 | } |
| 3646 | } |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3647 | if (j != outputs.size()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3648 | continue; |
| 3649 | } |
| 3650 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3651 | ALOGV("opening output for device %08x with params %s profile %p", |
| 3652 | device, address.string(), profile.get()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3653 | desc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3654 | desc->mDevice = device; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3655 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3656 | config.sample_rate = desc->mSamplingRate; |
| 3657 | config.channel_mask = desc->mChannelMask; |
| 3658 | config.format = desc->mFormat; |
| 3659 | config.offload_info.sample_rate = desc->mSamplingRate; |
| 3660 | config.offload_info.channel_mask = desc->mChannelMask; |
| 3661 | config.offload_info.format = desc->mFormat; |
| 3662 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3663 | status_t status = mpClientInterface->openOutput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3664 | &output, |
| 3665 | &config, |
| 3666 | &desc->mDevice, |
| 3667 | address, |
| 3668 | &desc->mLatency, |
| 3669 | desc->mFlags); |
| 3670 | if (status == NO_ERROR) { |
| 3671 | desc->mSamplingRate = config.sample_rate; |
| 3672 | desc->mChannelMask = config.channel_mask; |
| 3673 | desc->mFormat = config.format; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3674 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3675 | // Here is where the out_set_parameters() for card & device gets called |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3676 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3677 | char *param = audio_device_address_to_parameter(device, address); |
| 3678 | mpClientInterface->setParameters(output, String8(param)); |
| 3679 | free(param); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3680 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3681 | updateAudioProfiles(output, profile->getAudioProfiles()); |
| 3682 | if (!profile->hasValidAudioProfile()) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3683 | ALOGW("checkOutputsForDevice() missing param"); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3684 | mpClientInterface->closeOutput(output); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3685 | output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3686 | } else if (profile->hasDynamicAudioProfile()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3687 | mpClientInterface->closeOutput(output); |
Phil Burk | 702b105 | 2016-03-02 16:38:26 -0800 | [diff] [blame] | 3688 | output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3689 | profile->pickAudioProfile(config.sample_rate, config.channel_mask, config.format); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3690 | config.offload_info.sample_rate = config.sample_rate; |
| 3691 | config.offload_info.channel_mask = config.channel_mask; |
| 3692 | config.offload_info.format = config.format; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3693 | status = mpClientInterface->openOutput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3694 | &output, |
| 3695 | &config, |
| 3696 | &desc->mDevice, |
| 3697 | address, |
| 3698 | &desc->mLatency, |
| 3699 | desc->mFlags); |
| 3700 | if (status == NO_ERROR) { |
| 3701 | desc->mSamplingRate = config.sample_rate; |
| 3702 | desc->mChannelMask = config.channel_mask; |
| 3703 | desc->mFormat = config.format; |
| 3704 | } else { |
| 3705 | output = AUDIO_IO_HANDLE_NONE; |
| 3706 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3707 | } |
| 3708 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3709 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3710 | addOutput(output, desc); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3711 | if (device_distinguishes_on_address(device) && address != "0") { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3712 | sp<AudioPolicyMix> policyMix; |
| 3713 | if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3714 | ALOGE("checkOutputsForDevice() cannot find policy for address %s", |
| 3715 | address.string()); |
| 3716 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3717 | policyMix->setOutput(desc); |
Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 3718 | desc->mPolicyMix = policyMix->getMix(); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3719 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 3720 | } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
| 3721 | hasPrimaryOutput()) { |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 3722 | // no duplicated output for direct outputs and |
| 3723 | // outputs used by dynamic policy mixes |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3724 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3725 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3726 | // set initial stream volume for device |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3727 | applyStreamVolumes(desc, device, 0, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3728 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3729 | //TODO: configure audio effect output stage here |
| 3730 | |
| 3731 | // 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] | 3732 | duplicatedOutput = |
| 3733 | mpClientInterface->openDuplicateOutput(output, |
| 3734 | mPrimaryOutput->mIoHandle); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3735 | if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3736 | // add duplicated output descriptor |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3737 | sp<SwAudioOutputDescriptor> dupOutputDesc = |
| 3738 | new SwAudioOutputDescriptor(NULL, mpClientInterface); |
| 3739 | dupOutputDesc->mOutput1 = mPrimaryOutput; |
| 3740 | dupOutputDesc->mOutput2 = desc; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3741 | dupOutputDesc->mSamplingRate = desc->mSamplingRate; |
| 3742 | dupOutputDesc->mFormat = desc->mFormat; |
| 3743 | dupOutputDesc->mChannelMask = desc->mChannelMask; |
| 3744 | dupOutputDesc->mLatency = desc->mLatency; |
| 3745 | addOutput(duplicatedOutput, dupOutputDesc); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3746 | applyStreamVolumes(dupOutputDesc, device, 0, true); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3747 | } else { |
| 3748 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3749 | mPrimaryOutput->mIoHandle, output); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3750 | mpClientInterface->closeOutput(output); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3751 | removeOutput(output); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3752 | nextAudioPortGeneration(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3753 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3754 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3755 | } |
| 3756 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3757 | } else { |
| 3758 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3759 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3760 | if (output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3761 | ALOGW("checkOutputsForDevice() could not open output for device %x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3762 | profiles.removeAt(profile_index); |
| 3763 | profile_index--; |
| 3764 | } else { |
| 3765 | outputs.add(output); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3766 | // Load digital format info only for digital devices |
| 3767 | if (audio_device_is_digital(device)) { |
| 3768 | devDesc->importAudioPort(profile); |
| 3769 | } |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3770 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3771 | if (device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3772 | ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)", |
| 3773 | device, address.string()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3774 | setOutputDevice(desc, device, true/*force*/, 0/*delay*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3775 | NULL/*patch handle*/, address.string()); |
| 3776 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3777 | ALOGV("checkOutputsForDevice(): adding output %d", output); |
| 3778 | } |
| 3779 | } |
| 3780 | |
| 3781 | if (profiles.isEmpty()) { |
| 3782 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 3783 | return BAD_VALUE; |
| 3784 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3785 | } else { // Disconnect |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3786 | // check if one opened output is not needed any more after disconnecting one device |
| 3787 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3788 | desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3789 | if (!desc->isDuplicated()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3790 | // exact match on device |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3791 | if (device_distinguishes_on_address(device) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3792 | (desc->supportedDevices() == device)) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3793 | findIoHandlesByAddress(desc, device, address, outputs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3794 | } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3795 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", |
| 3796 | mOutputs.keyAt(i)); |
| 3797 | outputs.add(mOutputs.keyAt(i)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3798 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3799 | } |
| 3800 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3801 | // Clear any profiles associated with the disconnected device. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3802 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 3803 | { |
| 3804 | if (mHwModules[i]->mHandle == 0) { |
| 3805 | continue; |
| 3806 | } |
| 3807 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 3808 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3809 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3810 | if (profile->supportDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3811 | ALOGV("checkOutputsForDevice(): " |
| 3812 | "clearing direct output profile %zu on module %zu", j, i); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3813 | profile->clearAudioProfiles(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3814 | } |
| 3815 | } |
| 3816 | } |
| 3817 | } |
| 3818 | return NO_ERROR; |
| 3819 | } |
| 3820 | |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3821 | status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor> devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3822 | audio_policy_dev_state_t state, |
| 3823 | SortedVector<audio_io_handle_t>& inputs, |
| 3824 | const String8 address) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3825 | { |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3826 | audio_devices_t device = devDesc->type(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3827 | sp<AudioInputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 3828 | |
| 3829 | if (audio_device_is_digital(device)) { |
| 3830 | // erase all current sample rates, formats and channel masks |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 3831 | devDesc->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 3832 | } |
| 3833 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3834 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 3835 | // first list already open inputs that can be routed to this device |
| 3836 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 3837 | desc = mInputs.valueAt(input_index); |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3838 | if (desc->mProfile->supportDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3839 | ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index)); |
| 3840 | inputs.add(mInputs.keyAt(input_index)); |
| 3841 | } |
| 3842 | } |
| 3843 | |
| 3844 | // then look for input profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3845 | SortedVector< sp<IOProfile> > profiles; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3846 | for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++) |
| 3847 | { |
| 3848 | if (mHwModules[module_idx]->mHandle == 0) { |
| 3849 | continue; |
| 3850 | } |
| 3851 | for (size_t profile_index = 0; |
| 3852 | profile_index < mHwModules[module_idx]->mInputProfiles.size(); |
| 3853 | profile_index++) |
| 3854 | { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3855 | sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index]; |
| 3856 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3857 | if (profile->supportDevice(device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3858 | if (!device_distinguishes_on_address(device) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3859 | profile->supportDeviceAddress(address)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3860 | profiles.add(profile); |
| 3861 | ALOGV("checkInputsForDevice(): adding profile %zu from module %zu", |
| 3862 | profile_index, module_idx); |
| 3863 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3864 | } |
| 3865 | } |
| 3866 | } |
| 3867 | |
| 3868 | if (profiles.isEmpty() && inputs.isEmpty()) { |
| 3869 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 3870 | return BAD_VALUE; |
| 3871 | } |
| 3872 | |
| 3873 | // open inputs for matching profiles if needed. Direct inputs are also opened to |
| 3874 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 3875 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { |
| 3876 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3877 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3878 | // nothing to do if one input is already opened for this profile |
| 3879 | size_t input_index; |
| 3880 | for (input_index = 0; input_index < mInputs.size(); input_index++) { |
| 3881 | desc = mInputs.valueAt(input_index); |
| 3882 | if (desc->mProfile == profile) { |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3883 | if (audio_device_is_digital(device)) { |
| 3884 | devDesc->importAudioPort(profile); |
| 3885 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3886 | break; |
| 3887 | } |
| 3888 | } |
| 3889 | if (input_index != mInputs.size()) { |
| 3890 | continue; |
| 3891 | } |
| 3892 | |
| 3893 | ALOGV("opening input for device 0x%X with params %s", device, address.string()); |
| 3894 | desc = new AudioInputDescriptor(profile); |
| 3895 | desc->mDevice = device; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3896 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3897 | config.sample_rate = desc->mSamplingRate; |
| 3898 | config.channel_mask = desc->mChannelMask; |
| 3899 | config.format = desc->mFormat; |
| 3900 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3901 | status_t status = mpClientInterface->openInput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3902 | &input, |
| 3903 | &config, |
| 3904 | &desc->mDevice, |
| 3905 | address, |
| 3906 | AUDIO_SOURCE_MIC, |
| 3907 | AUDIO_INPUT_FLAG_NONE /*FIXME*/); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3908 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3909 | if (status == NO_ERROR) { |
| 3910 | desc->mSamplingRate = config.sample_rate; |
| 3911 | desc->mChannelMask = config.channel_mask; |
| 3912 | desc->mFormat = config.format; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3913 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3914 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3915 | char *param = audio_device_address_to_parameter(device, address); |
| 3916 | mpClientInterface->setParameters(input, String8(param)); |
| 3917 | free(param); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3918 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3919 | updateAudioProfiles(input, profile->getAudioProfiles()); |
| 3920 | if (!profile->hasValidAudioProfile()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3921 | ALOGW("checkInputsForDevice() direct input missing param"); |
| 3922 | mpClientInterface->closeInput(input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3923 | input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3924 | } |
| 3925 | |
| 3926 | if (input != 0) { |
| 3927 | addInput(input, desc); |
| 3928 | } |
| 3929 | } // endif input != 0 |
| 3930 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3931 | if (input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3932 | ALOGW("checkInputsForDevice() could not open input for device 0x%X", device); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3933 | profiles.removeAt(profile_index); |
| 3934 | profile_index--; |
| 3935 | } else { |
| 3936 | inputs.add(input); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3937 | if (audio_device_is_digital(device)) { |
| 3938 | devDesc->importAudioPort(profile); |
| 3939 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3940 | ALOGV("checkInputsForDevice(): adding input %d", input); |
| 3941 | } |
| 3942 | } // end scan profiles |
| 3943 | |
| 3944 | if (profiles.isEmpty()) { |
| 3945 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 3946 | return BAD_VALUE; |
| 3947 | } |
| 3948 | } else { |
| 3949 | // Disconnect |
| 3950 | // check if one opened input is not needed any more after disconnecting one device |
| 3951 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 3952 | desc = mInputs.valueAt(input_index); |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3953 | if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3954 | ALOGV("checkInputsForDevice(): disconnecting adding input %d", |
| 3955 | mInputs.keyAt(input_index)); |
| 3956 | inputs.add(mInputs.keyAt(input_index)); |
| 3957 | } |
| 3958 | } |
| 3959 | // Clear any profiles associated with the disconnected device. |
| 3960 | for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) { |
| 3961 | if (mHwModules[module_index]->mHandle == 0) { |
| 3962 | continue; |
| 3963 | } |
| 3964 | for (size_t profile_index = 0; |
| 3965 | profile_index < mHwModules[module_index]->mInputProfiles.size(); |
| 3966 | profile_index++) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3967 | sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3968 | if (profile->supportDevice(device)) { |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 3969 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu", |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3970 | profile_index, module_index); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3971 | profile->clearAudioProfiles(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3972 | } |
| 3973 | } |
| 3974 | } |
| 3975 | } // end disconnect |
| 3976 | |
| 3977 | return NO_ERROR; |
| 3978 | } |
| 3979 | |
| 3980 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3981 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3982 | { |
| 3983 | ALOGV("closeOutput(%d)", output); |
| 3984 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3985 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3986 | if (outputDesc == NULL) { |
| 3987 | ALOGW("closeOutput() unknown output %d", output); |
| 3988 | return; |
| 3989 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3990 | mPolicyMixes.closeOutput(outputDesc); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3991 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3992 | // look for duplicated outputs connected to the output being removed. |
| 3993 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3994 | sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3995 | if (dupOutputDesc->isDuplicated() && |
| 3996 | (dupOutputDesc->mOutput1 == outputDesc || |
| 3997 | dupOutputDesc->mOutput2 == outputDesc)) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3998 | sp<AudioOutputDescriptor> outputDesc2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3999 | if (dupOutputDesc->mOutput1 == outputDesc) { |
| 4000 | outputDesc2 = dupOutputDesc->mOutput2; |
| 4001 | } else { |
| 4002 | outputDesc2 = dupOutputDesc->mOutput1; |
| 4003 | } |
| 4004 | // As all active tracks on duplicated output will be deleted, |
| 4005 | // and as they were also referenced on the other output, the reference |
| 4006 | // count for their stream type must be adjusted accordingly on |
| 4007 | // the other output. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4008 | for (int j = 0; j < AUDIO_STREAM_CNT; j++) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4009 | int refCount = dupOutputDesc->mRefCount[j]; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4010 | outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4011 | } |
| 4012 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); |
| 4013 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); |
| 4014 | |
| 4015 | mpClientInterface->closeOutput(duplicatedOutput); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4016 | removeOutput(duplicatedOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4017 | } |
| 4018 | } |
| 4019 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4020 | nextAudioPortGeneration(); |
| 4021 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4022 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4023 | if (index >= 0) { |
| 4024 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4025 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 4026 | mAudioPatches.removeItemsAt(index); |
| 4027 | mpClientInterface->onAudioPatchListUpdate(); |
| 4028 | } |
| 4029 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4030 | AudioParameter param; |
| 4031 | param.add(String8("closing"), String8("true")); |
| 4032 | mpClientInterface->setParameters(output, param.toString()); |
| 4033 | |
| 4034 | mpClientInterface->closeOutput(output); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4035 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4036 | mPreviousOutputs = mOutputs; |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4037 | } |
| 4038 | |
| 4039 | void AudioPolicyManager::closeInput(audio_io_handle_t input) |
| 4040 | { |
| 4041 | ALOGV("closeInput(%d)", input); |
| 4042 | |
| 4043 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
| 4044 | if (inputDesc == NULL) { |
| 4045 | ALOGW("closeInput() unknown input %d", input); |
| 4046 | return; |
| 4047 | } |
| 4048 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4049 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4050 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4051 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4052 | if (index >= 0) { |
| 4053 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4054 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 4055 | mAudioPatches.removeItemsAt(index); |
| 4056 | mpClientInterface->onAudioPatchListUpdate(); |
| 4057 | } |
| 4058 | |
| 4059 | mpClientInterface->closeInput(input); |
| 4060 | mInputs.removeItem(input); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4061 | } |
| 4062 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4063 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice( |
| 4064 | audio_devices_t device, |
| 4065 | SwAudioOutputCollection openOutputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4066 | { |
| 4067 | SortedVector<audio_io_handle_t> outputs; |
| 4068 | |
| 4069 | ALOGVV("getOutputsForDevice() device %04x", device); |
| 4070 | for (size_t i = 0; i < openOutputs.size(); i++) { |
| 4071 | ALOGVV("output %d isDuplicated=%d device=%04x", |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4072 | i, openOutputs.valueAt(i)->isDuplicated(), |
| 4073 | openOutputs.valueAt(i)->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4074 | if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) { |
| 4075 | ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i)); |
| 4076 | outputs.add(openOutputs.keyAt(i)); |
| 4077 | } |
| 4078 | } |
| 4079 | return outputs; |
| 4080 | } |
| 4081 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4082 | bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4083 | SortedVector<audio_io_handle_t>& outputs2) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4084 | { |
| 4085 | if (outputs1.size() != outputs2.size()) { |
| 4086 | return false; |
| 4087 | } |
| 4088 | for (size_t i = 0; i < outputs1.size(); i++) { |
| 4089 | if (outputs1[i] != outputs2[i]) { |
| 4090 | return false; |
| 4091 | } |
| 4092 | } |
| 4093 | return true; |
| 4094 | } |
| 4095 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4096 | void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4097 | { |
| 4098 | audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/); |
| 4099 | audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 4100 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs); |
| 4101 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs); |
| 4102 | |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4103 | // also take into account external policy-related changes: add all outputs which are |
| 4104 | // associated with policies in the "before" and "after" output vectors |
| 4105 | ALOGVV("checkOutputForStrategy(): policy related outputs"); |
| 4106 | for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4107 | const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4108 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4109 | srcOutputs.add(desc->mIoHandle); |
| 4110 | ALOGVV(" previous outputs: adding %d", desc->mIoHandle); |
| 4111 | } |
| 4112 | } |
| 4113 | for (size_t i = 0 ; i < mOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4114 | const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4115 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4116 | dstOutputs.add(desc->mIoHandle); |
| 4117 | ALOGVV(" new outputs: adding %d", desc->mIoHandle); |
| 4118 | } |
| 4119 | } |
| 4120 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4121 | if (!vectorsEqual(srcOutputs,dstOutputs)) { |
| 4122 | ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d", |
| 4123 | strategy, srcOutputs[0], dstOutputs[0]); |
| 4124 | // mute strategy while moving tracks from one output to another |
| 4125 | for (size_t i = 0; i < srcOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4126 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4127 | if (isStrategyActive(desc, strategy)) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4128 | setStrategyMute(strategy, true, desc); |
| 4129 | setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4130 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4131 | sp<AudioSourceDescriptor> source = |
| 4132 | getSourceForStrategyOnOutput(srcOutputs[i], strategy); |
| 4133 | if (source != 0){ |
| 4134 | connectAudioSource(source); |
| 4135 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4136 | } |
| 4137 | |
| 4138 | // Move effects associated to this strategy from previous output to new output |
| 4139 | if (strategy == STRATEGY_MEDIA) { |
| 4140 | audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs); |
| 4141 | SortedVector<audio_io_handle_t> moved; |
| 4142 | for (size_t i = 0; i < mEffects.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4143 | sp<EffectDescriptor> effectDesc = mEffects.valueAt(i); |
| 4144 | if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX && |
| 4145 | effectDesc->mIo != fxOutput) { |
| 4146 | if (moved.indexOf(effectDesc->mIo) < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4147 | ALOGV("checkOutputForStrategy() moving effect %d to output %d", |
| 4148 | mEffects.keyAt(i), fxOutput); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4149 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4150 | fxOutput); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4151 | moved.add(effectDesc->mIo); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4152 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4153 | effectDesc->mIo = fxOutput; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4154 | } |
| 4155 | } |
| 4156 | } |
| 4157 | // Move tracks associated to this strategy from previous output to new output |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4158 | for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4159 | if (getStrategy((audio_stream_type_t)i) == strategy) { |
| 4160 | mpClientInterface->invalidateStream((audio_stream_type_t)i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4161 | } |
| 4162 | } |
| 4163 | } |
| 4164 | } |
| 4165 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4166 | void AudioPolicyManager::checkOutputForAllStrategies() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4167 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4168 | 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] | 4169 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4170 | checkOutputForStrategy(STRATEGY_PHONE); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4171 | 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] | 4172 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4173 | checkOutputForStrategy(STRATEGY_SONIFICATION); |
| 4174 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4175 | checkOutputForStrategy(STRATEGY_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4176 | checkOutputForStrategy(STRATEGY_MEDIA); |
| 4177 | checkOutputForStrategy(STRATEGY_DTMF); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4178 | checkOutputForStrategy(STRATEGY_REROUTING); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4179 | } |
| 4180 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4181 | void AudioPolicyManager::checkA2dpSuspend() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4182 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4183 | audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4184 | if (a2dpOutput == 0) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4185 | mA2dpSuspended = false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4186 | return; |
| 4187 | } |
| 4188 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4189 | bool isScoConnected = |
Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 4190 | ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET & |
| 4191 | ~AUDIO_DEVICE_BIT_IN) != 0) || |
| 4192 | ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4193 | // suspend A2DP output if: |
| 4194 | // (NOT already suspended) && |
| 4195 | // ((SCO device is connected && |
| 4196 | // (forced usage for communication || for record is SCO))) || |
| 4197 | // (phone state is ringing || in call) |
| 4198 | // |
| 4199 | // restore A2DP output if: |
| 4200 | // (Already suspended) && |
| 4201 | // ((SCO device is NOT connected || |
| 4202 | // (forced usage NOT for communication && NOT for record is SCO))) && |
| 4203 | // (phone state is NOT ringing && NOT in call) |
| 4204 | // |
| 4205 | if (mA2dpSuspended) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4206 | if ((!isScoConnected || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4207 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) && |
| 4208 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) && |
| 4209 | ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) && |
| 4210 | (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4211 | |
| 4212 | mpClientInterface->restoreOutput(a2dpOutput); |
| 4213 | mA2dpSuspended = false; |
| 4214 | } |
| 4215 | } else { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4216 | if ((isScoConnected && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4217 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) || |
| 4218 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO))) || |
| 4219 | ((mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) || |
| 4220 | (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4221 | |
| 4222 | mpClientInterface->suspendOutput(a2dpOutput); |
| 4223 | mA2dpSuspended = true; |
| 4224 | } |
| 4225 | } |
| 4226 | } |
| 4227 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4228 | audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
| 4229 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4230 | { |
| 4231 | audio_devices_t device = AUDIO_DEVICE_NONE; |
| 4232 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4233 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4234 | if (index >= 0) { |
| 4235 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4236 | if (patchDesc->mUid != mUidCached) { |
| 4237 | ALOGV("getNewOutputDevice() device %08x forced by patch %d", |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4238 | outputDesc->device(), outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4239 | return outputDesc->device(); |
| 4240 | } |
| 4241 | } |
| 4242 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4243 | // 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] | 4244 | // 1: the strategy enforced audible is active and enforced on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4245 | // use device for strategy enforced audible |
| 4246 | // 2: we are in call or the strategy phone is active on the output: |
| 4247 | // use device for strategy phone |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 4248 | // 3: the strategy for enforced audible is active but not enforced on the output: |
| 4249 | // use the device for strategy enforced audible |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4250 | // 4: the strategy sonification is active on the output: |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4251 | // use device for strategy sonification |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4252 | // 5: the strategy accessibility is active on the output: |
| 4253 | // use device for strategy accessibility |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4254 | // 6: the strategy "respectful" sonification is active on the output: |
| 4255 | // use device for strategy "respectful" sonification |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4256 | // 7: the strategy media is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4257 | // use device for strategy media |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4258 | // 8: the strategy DTMF is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4259 | // use device for strategy DTMF |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4260 | // 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] | 4261 | // use device for strategy t-t-s |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4262 | if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4263 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4264 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
| 4265 | } else if (isInCall() || |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4266 | isStrategyActive(outputDesc, STRATEGY_PHONE)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4267 | device = getDeviceForStrategy(STRATEGY_PHONE, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4268 | } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) { |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 4269 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4270 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4271 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4272 | } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) { |
| 4273 | device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4274 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4275 | device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4276 | } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4277 | device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4278 | } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4279 | device = getDeviceForStrategy(STRATEGY_DTMF, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4280 | } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4281 | device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4282 | } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4283 | device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4284 | } |
| 4285 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4286 | ALOGV("getNewOutputDevice() selected device %x", device); |
| 4287 | return device; |
| 4288 | } |
| 4289 | |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 4290 | audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4291 | { |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 4292 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4293 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4294 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4295 | if (index >= 0) { |
| 4296 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4297 | if (patchDesc->mUid != mUidCached) { |
| 4298 | ALOGV("getNewInputDevice() device %08x forced by patch %d", |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4299 | inputDesc->mDevice, inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4300 | return inputDesc->mDevice; |
| 4301 | } |
| 4302 | } |
| 4303 | |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 4304 | audio_devices_t device = getDeviceAndMixForInputSource(inputDesc->inputSource()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4305 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4306 | return device; |
| 4307 | } |
| 4308 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4309 | bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1, |
| 4310 | audio_stream_type_t stream2) { |
| 4311 | return ((stream1 == stream2) || |
| 4312 | ((stream1 == AUDIO_STREAM_ACCESSIBILITY) && (stream2 == AUDIO_STREAM_MUSIC)) || |
| 4313 | ((stream1 == AUDIO_STREAM_MUSIC) && (stream2 == AUDIO_STREAM_ACCESSIBILITY))); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4314 | } |
| 4315 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4316 | uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4317 | return (uint32_t)getStrategy(stream); |
| 4318 | } |
| 4319 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4320 | audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4321 | // By checking the range of stream before calling getStrategy, we avoid |
| 4322 | // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE |
| 4323 | // 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] | 4324 | if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4325 | return AUDIO_DEVICE_NONE; |
| 4326 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4327 | audio_devices_t devices = AUDIO_DEVICE_NONE; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4328 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 4329 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4330 | continue; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4331 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4332 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4333 | audio_devices_t curDevices = |
| 4334 | getDeviceForStrategy((routing_strategy)curStrategy, true /*fromCache*/); |
| 4335 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(curDevices, mOutputs); |
| 4336 | for (size_t i = 0; i < outputs.size(); i++) { |
| 4337 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4338 | if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4339 | curDevices |= outputDesc->device(); |
| 4340 | } |
| 4341 | } |
| 4342 | devices |= curDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4343 | } |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4344 | |
| 4345 | /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it |
| 4346 | and doesn't really need to.*/ |
| 4347 | if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) { |
| 4348 | devices |= AUDIO_DEVICE_OUT_SPEAKER; |
| 4349 | devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
| 4350 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4351 | return devices; |
| 4352 | } |
| 4353 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4354 | routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const |
| 4355 | { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4356 | 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] | 4357 | return mEngine->getStrategyForStream(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4358 | } |
| 4359 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4360 | uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) { |
| 4361 | // flags to strategy mapping |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4362 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 4363 | return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER; |
| 4364 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4365 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 4366 | return (uint32_t) STRATEGY_ENFORCED_AUDIBLE; |
| 4367 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4368 | // usage to strategy mapping |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4369 | return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage)); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4370 | } |
| 4371 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4372 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4373 | switch(stream) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4374 | case AUDIO_STREAM_MUSIC: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4375 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
| 4376 | updateDevicesAndOutputs(); |
| 4377 | break; |
| 4378 | default: |
| 4379 | break; |
| 4380 | } |
| 4381 | } |
| 4382 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4383 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 4384 | |
| 4385 | // skip beacon mute management if a dedicated TTS output is available |
| 4386 | if (mTtsOutputAvailable) { |
| 4387 | return 0; |
| 4388 | } |
| 4389 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4390 | switch(event) { |
| 4391 | case STARTING_OUTPUT: |
| 4392 | mBeaconMuteRefCount++; |
| 4393 | break; |
| 4394 | case STOPPING_OUTPUT: |
| 4395 | if (mBeaconMuteRefCount > 0) { |
| 4396 | mBeaconMuteRefCount--; |
| 4397 | } |
| 4398 | break; |
| 4399 | case STARTING_BEACON: |
| 4400 | mBeaconPlayingRefCount++; |
| 4401 | break; |
| 4402 | case STOPPING_BEACON: |
| 4403 | if (mBeaconPlayingRefCount > 0) { |
| 4404 | mBeaconPlayingRefCount--; |
| 4405 | } |
| 4406 | break; |
| 4407 | } |
| 4408 | |
| 4409 | if (mBeaconMuteRefCount > 0) { |
| 4410 | // any playback causes beacon to be muted |
| 4411 | return setBeaconMute(true); |
| 4412 | } else { |
| 4413 | // no other playback: unmute when beacon starts playing, mute when it stops |
| 4414 | return setBeaconMute(mBeaconPlayingRefCount == 0); |
| 4415 | } |
| 4416 | } |
| 4417 | |
| 4418 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { |
| 4419 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", |
| 4420 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); |
| 4421 | // keep track of muted state to avoid repeating mute/unmute operations |
| 4422 | if (mBeaconMuted != mute) { |
| 4423 | // mute/unmute AUDIO_STREAM_TTS on all outputs |
| 4424 | ALOGV("\t muting %d", mute); |
| 4425 | uint32_t maxLatency = 0; |
| 4426 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4427 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4428 | setStreamMute(AUDIO_STREAM_TTS, mute/*on*/, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4429 | desc, |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4430 | 0 /*delay*/, AUDIO_DEVICE_NONE); |
| 4431 | const uint32_t latency = desc->latency() * 2; |
| 4432 | if (latency > maxLatency) { |
| 4433 | maxLatency = latency; |
| 4434 | } |
| 4435 | } |
| 4436 | mBeaconMuted = mute; |
| 4437 | return maxLatency; |
| 4438 | } |
| 4439 | return 0; |
| 4440 | } |
| 4441 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4442 | audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4443 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4444 | { |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4445 | // Routing |
| 4446 | // see if we have an explicit route |
| 4447 | // scan the whole RouteMap, for each entry, convert the stream type to a strategy |
| 4448 | // (getStrategy(stream)). |
| 4449 | // if the strategy from the stream type in the RouteMap is the same as the argument above, |
| 4450 | // and activity count is non-zero |
| 4451 | // the device = the device from the descriptor in the RouteMap, and exit. |
| 4452 | for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) { |
| 4453 | sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4454 | routing_strategy routeStrategy = getStrategy(route->mStreamType); |
| 4455 | if ((routeStrategy == strategy) && route->isActive()) { |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4456 | return route->mDeviceDescriptor->type(); |
| 4457 | } |
| 4458 | } |
| 4459 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4460 | if (fromCache) { |
| 4461 | ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x", |
| 4462 | strategy, mDeviceForStrategy[strategy]); |
| 4463 | return mDeviceForStrategy[strategy]; |
| 4464 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4465 | return mEngine->getDeviceForStrategy(strategy); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4466 | } |
| 4467 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4468 | void AudioPolicyManager::updateDevicesAndOutputs() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4469 | { |
| 4470 | for (int i = 0; i < NUM_STRATEGIES; i++) { |
| 4471 | mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
| 4472 | } |
| 4473 | mPreviousOutputs = mOutputs; |
| 4474 | } |
| 4475 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4476 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4477 | audio_devices_t prevDevice, |
| 4478 | uint32_t delayMs) |
| 4479 | { |
| 4480 | // mute/unmute strategies using an incompatible device combination |
| 4481 | // if muting, wait for the audio in pcm buffer to be drained before proceeding |
| 4482 | // if unmuting, unmute only after the specified delay |
| 4483 | if (outputDesc->isDuplicated()) { |
| 4484 | return 0; |
| 4485 | } |
| 4486 | |
| 4487 | uint32_t muteWaitMs = 0; |
| 4488 | audio_devices_t device = outputDesc->device(); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4489 | bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4490 | |
| 4491 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
| 4492 | audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4493 | curDevice = curDevice & outputDesc->supportedDevices(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4494 | bool mute = shouldMute && (curDevice & device) && (curDevice != device); |
| 4495 | bool doMute = false; |
| 4496 | |
| 4497 | if (mute && !outputDesc->mStrategyMutedByDevice[i]) { |
| 4498 | doMute = true; |
| 4499 | outputDesc->mStrategyMutedByDevice[i] = true; |
| 4500 | } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){ |
| 4501 | doMute = true; |
| 4502 | outputDesc->mStrategyMutedByDevice[i] = false; |
| 4503 | } |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4504 | if (doMute) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4505 | for (size_t j = 0; j < mOutputs.size(); j++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4506 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4507 | // skip output if it does not share any device with current output |
| 4508 | if ((desc->supportedDevices() & outputDesc->supportedDevices()) |
| 4509 | == AUDIO_DEVICE_NONE) { |
| 4510 | continue; |
| 4511 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4512 | ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x)", |
| 4513 | mute ? "muting" : "unmuting", i, curDevice); |
| 4514 | setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4515 | if (isStrategyActive(desc, (routing_strategy)i)) { |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4516 | if (mute) { |
| 4517 | // FIXME: should not need to double latency if volume could be applied |
| 4518 | // immediately by the audioflinger mixer. We must account for the delay |
| 4519 | // between now and the next time the audioflinger thread for this output |
| 4520 | // will process a buffer (which corresponds to one buffer size, |
| 4521 | // usually 1/2 or 1/4 of the latency). |
| 4522 | if (muteWaitMs < desc->latency() * 2) { |
| 4523 | muteWaitMs = desc->latency() * 2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4524 | } |
| 4525 | } |
| 4526 | } |
| 4527 | } |
| 4528 | } |
| 4529 | } |
| 4530 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4531 | // temporary mute output if device selection changes to avoid volume bursts due to |
| 4532 | // different per device volumes |
| 4533 | if (outputDesc->isActive() && (device != prevDevice)) { |
| 4534 | if (muteWaitMs < outputDesc->latency() * 2) { |
| 4535 | muteWaitMs = outputDesc->latency() * 2; |
| 4536 | } |
| 4537 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4538 | if (isStrategyActive(outputDesc, (routing_strategy)i)) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4539 | setStrategyMute((routing_strategy)i, true, outputDesc); |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4540 | // do tempMute unmute after twice the mute wait time |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4541 | setStrategyMute((routing_strategy)i, false, outputDesc, |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4542 | muteWaitMs *2, device); |
| 4543 | } |
| 4544 | } |
| 4545 | } |
| 4546 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4547 | // wait for the PCM output buffers to empty before proceeding with the rest of the command |
| 4548 | if (muteWaitMs > delayMs) { |
| 4549 | muteWaitMs -= delayMs; |
| 4550 | usleep(muteWaitMs * 1000); |
| 4551 | return muteWaitMs; |
| 4552 | } |
| 4553 | return 0; |
| 4554 | } |
| 4555 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4556 | uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4557 | audio_devices_t device, |
| 4558 | bool force, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4559 | int delayMs, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4560 | audio_patch_handle_t *patchHandle, |
| 4561 | const char* address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4562 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4563 | ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4564 | AudioParameter param; |
| 4565 | uint32_t muteWaitMs; |
| 4566 | |
| 4567 | if (outputDesc->isDuplicated()) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4568 | muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs); |
| 4569 | muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4570 | return muteWaitMs; |
| 4571 | } |
| 4572 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current |
| 4573 | // output profile |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4574 | if ((device != AUDIO_DEVICE_NONE) && |
| 4575 | ((device & outputDesc->supportedDevices()) == 0)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4576 | return 0; |
| 4577 | } |
| 4578 | |
| 4579 | // filter devices according to output selected |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4580 | device = (audio_devices_t)(device & outputDesc->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4581 | |
| 4582 | audio_devices_t prevDevice = outputDesc->mDevice; |
| 4583 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4584 | ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4585 | |
| 4586 | if (device != AUDIO_DEVICE_NONE) { |
| 4587 | outputDesc->mDevice = device; |
| 4588 | } |
| 4589 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs); |
| 4590 | |
| 4591 | // Do not change the routing if: |
Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 4592 | // the requested device is AUDIO_DEVICE_NONE |
| 4593 | // OR the requested device is the same as current device |
| 4594 | // AND force is not specified |
| 4595 | // AND the output is connected by a valid audio patch. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4596 | // Doing this check here allows the caller to call setOutputDevice() without conditions |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4597 | if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && |
| 4598 | !force && |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4599 | outputDesc->getPatchHandle() != 0) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4600 | ALOGV("setOutputDevice() setting same device 0x%04x or null device", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4601 | return muteWaitMs; |
| 4602 | } |
| 4603 | |
| 4604 | ALOGV("setOutputDevice() changing device"); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4605 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4606 | // do the routing |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4607 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4608 | resetOutputDevice(outputDesc, delayMs, NULL); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4609 | } else { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4610 | DeviceVector deviceList = (address == NULL) ? |
| 4611 | mAvailableOutputDevices.getDevicesFromType(device) |
| 4612 | : mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address)); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4613 | if (!deviceList.isEmpty()) { |
| 4614 | struct audio_patch patch; |
| 4615 | outputDesc->toAudioPortConfig(&patch.sources[0]); |
| 4616 | patch.num_sources = 1; |
| 4617 | patch.num_sinks = 0; |
| 4618 | for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) { |
| 4619 | deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4620 | patch.num_sinks++; |
| 4621 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4622 | ssize_t index; |
| 4623 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { |
| 4624 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4625 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4626 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4627 | } |
| 4628 | sp< AudioPatch> patchDesc; |
| 4629 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 4630 | if (index >= 0) { |
| 4631 | patchDesc = mAudioPatches.valueAt(index); |
| 4632 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 4633 | } |
| 4634 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4635 | status_t status = mpClientInterface->createAudioPatch(&patch, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4636 | &afPatchHandle, |
| 4637 | delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4638 | ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d" |
| 4639 | "num_sources %d num_sinks %d", |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4640 | status, afPatchHandle, patch.num_sources, patch.num_sinks); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4641 | if (status == NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4642 | if (index < 0) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 4643 | patchDesc = new AudioPatch(&patch, mUidCached); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4644 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 4645 | } else { |
| 4646 | patchDesc->mPatch = patch; |
| 4647 | } |
| 4648 | patchDesc->mAfPatchHandle = afPatchHandle; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4649 | if (patchHandle) { |
| 4650 | *patchHandle = patchDesc->mHandle; |
| 4651 | } |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4652 | outputDesc->setPatchHandle(patchDesc->mHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4653 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4654 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4655 | } |
| 4656 | } |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 4657 | |
| 4658 | // inform all input as well |
| 4659 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 4660 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4661 | if (!is_virtual_input_device(inputDescriptor->mDevice)) { |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 4662 | AudioParameter inputCmd = AudioParameter(); |
| 4663 | ALOGV("%s: inform input %d of device:%d", __func__, |
| 4664 | inputDescriptor->mIoHandle, device); |
| 4665 | inputCmd.addInt(String8(AudioParameter::keyRouting),device); |
| 4666 | mpClientInterface->setParameters(inputDescriptor->mIoHandle, |
| 4667 | inputCmd.toString(), |
| 4668 | delayMs); |
| 4669 | } |
| 4670 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4671 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4672 | |
| 4673 | // update stream volumes according to new device |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4674 | applyStreamVolumes(outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4675 | |
| 4676 | return muteWaitMs; |
| 4677 | } |
| 4678 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4679 | status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4680 | int delayMs, |
| 4681 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4682 | { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4683 | ssize_t index; |
| 4684 | if (patchHandle) { |
| 4685 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4686 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4687 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4688 | } |
| 4689 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4690 | return INVALID_OPERATION; |
| 4691 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4692 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4693 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4694 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 4695 | outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4696 | removeAudioPatch(patchDesc->mHandle); |
| 4697 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4698 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4699 | return status; |
| 4700 | } |
| 4701 | |
| 4702 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, |
| 4703 | audio_devices_t device, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4704 | bool force, |
| 4705 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4706 | { |
| 4707 | status_t status = NO_ERROR; |
| 4708 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4709 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4710 | if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) { |
| 4711 | inputDesc->mDevice = device; |
| 4712 | |
| 4713 | DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device); |
| 4714 | if (!deviceList.isEmpty()) { |
| 4715 | struct audio_patch patch; |
| 4716 | inputDesc->toAudioPortConfig(&patch.sinks[0]); |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 4717 | // AUDIO_SOURCE_HOTWORD is for internal use only: |
| 4718 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 4719 | if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD && |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 4720 | !inputDesc->isSoundTrigger()) { |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 4721 | patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 4722 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4723 | patch.num_sinks = 1; |
| 4724 | //only one input device for now |
| 4725 | deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4726 | patch.num_sources = 1; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4727 | ssize_t index; |
| 4728 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { |
| 4729 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4730 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4731 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4732 | } |
| 4733 | sp< AudioPatch> patchDesc; |
| 4734 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 4735 | if (index >= 0) { |
| 4736 | patchDesc = mAudioPatches.valueAt(index); |
| 4737 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 4738 | } |
| 4739 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4740 | status_t status = mpClientInterface->createAudioPatch(&patch, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4741 | &afPatchHandle, |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4742 | 0); |
| 4743 | ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d", |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4744 | status, afPatchHandle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4745 | if (status == NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4746 | if (index < 0) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 4747 | patchDesc = new AudioPatch(&patch, mUidCached); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4748 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 4749 | } else { |
| 4750 | patchDesc->mPatch = patch; |
| 4751 | } |
| 4752 | patchDesc->mAfPatchHandle = afPatchHandle; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4753 | if (patchHandle) { |
| 4754 | *patchHandle = patchDesc->mHandle; |
| 4755 | } |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4756 | inputDesc->setPatchHandle(patchDesc->mHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4757 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4758 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4759 | } |
| 4760 | } |
| 4761 | } |
| 4762 | return status; |
| 4763 | } |
| 4764 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4765 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, |
| 4766 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4767 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4768 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4769 | ssize_t index; |
| 4770 | if (patchHandle) { |
| 4771 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4772 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4773 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4774 | } |
| 4775 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4776 | return INVALID_OPERATION; |
| 4777 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4778 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4779 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4780 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 4781 | inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4782 | removeAudioPatch(patchDesc->mHandle); |
| 4783 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4784 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4785 | return status; |
| 4786 | } |
| 4787 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 4788 | sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4789 | String8 address, |
| 4790 | uint32_t& samplingRate, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4791 | audio_format_t& format, |
| 4792 | audio_channel_mask_t& channelMask, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4793 | audio_input_flags_t flags) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4794 | { |
| 4795 | // Choose an input profile based on the requested capture parameters: select the first available |
| 4796 | // profile supporting all requested parameters. |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4797 | // |
| 4798 | // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return |
| 4799 | // the best matching profile, not the first one. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4800 | |
| 4801 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 4802 | { |
| 4803 | if (mHwModules[i]->mHandle == 0) { |
| 4804 | continue; |
| 4805 | } |
| 4806 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) |
| 4807 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4808 | sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j]; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4809 | // profile->log(); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4810 | if (profile->isCompatibleProfile(device, address, samplingRate, |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 4811 | &samplingRate /*updatedSamplingRate*/, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4812 | format, |
| 4813 | &format /*updatedFormat*/, |
| 4814 | channelMask, |
| 4815 | &channelMask /*updatedChannelMask*/, |
| 4816 | (audio_output_flags_t) flags)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4817 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4818 | return profile; |
| 4819 | } |
| 4820 | } |
| 4821 | } |
| 4822 | return NULL; |
| 4823 | } |
| 4824 | |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 4825 | |
| 4826 | audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4827 | AudioMix **policyMix) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4828 | { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4829 | audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
| 4830 | audio_devices_t selectedDeviceFromMix = |
| 4831 | mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4832 | |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4833 | if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) { |
| 4834 | return selectedDeviceFromMix; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4835 | } |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 4836 | return getDeviceForInputSource(inputSource); |
| 4837 | } |
| 4838 | |
| 4839 | audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource) |
| 4840 | { |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 4841 | for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) { |
| 4842 | sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4843 | if (inputSource == route->mSource && route->isActive()) { |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 4844 | return route->mDeviceDescriptor->type(); |
| 4845 | } |
| 4846 | } |
| 4847 | |
| 4848 | return mEngine->getDeviceForInputSource(inputSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4849 | } |
| 4850 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4851 | float AudioPolicyManager::computeVolume(audio_stream_type_t stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4852 | int index, |
| 4853 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4854 | { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4855 | float volumeDb = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4856 | // if a headset is connected, apply the following rules to ring tones and notifications |
| 4857 | // to avoid sound level bursts in user's ears: |
| 4858 | // - always attenuate ring tones and notifications volume by 6dB |
| 4859 | // - if music is playing, always limit the volume to current music volume, |
| 4860 | // with a minimum threshold at -36dB so that notification is always perceived. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4861 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4862 | if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 4863 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | |
| 4864 | AUDIO_DEVICE_OUT_WIRED_HEADSET | |
| 4865 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) && |
| 4866 | ((stream_strategy == STRATEGY_SONIFICATION) |
| 4867 | || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL) |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4868 | || (stream == AUDIO_STREAM_SYSTEM) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4869 | || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4870 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) && |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4871 | mVolumeCurves->canBeMuted(stream)) { |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4872 | volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4873 | // when the phone is ringing we must consider that music could have been paused just before |
| 4874 | // by the music application and behave as if music was active if the last music track was |
| 4875 | // just stopped |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4876 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4877 | mLimitRingtoneVolume) { |
| 4878 | audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4879 | float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4880 | mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC, |
| 4881 | musicDevice), |
| 4882 | musicDevice); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4883 | float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ? |
| 4884 | musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB; |
| 4885 | if (volumeDb > minVolDB) { |
| 4886 | volumeDb = minVolDB; |
| 4887 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4888 | } |
| 4889 | } |
| 4890 | } |
| 4891 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4892 | return volumeDb; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4893 | } |
| 4894 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4895 | status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4896 | int index, |
| 4897 | const sp<AudioOutputDescriptor>& outputDesc, |
| 4898 | audio_devices_t device, |
| 4899 | int delayMs, |
| 4900 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4901 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4902 | // do not change actual stream volume if the stream is muted |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4903 | if (outputDesc->mMuteCount[stream] != 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4904 | ALOGVV("checkAndSetVolume() stream %d muted count %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4905 | stream, outputDesc->mMuteCount[stream]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4906 | return NO_ERROR; |
| 4907 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4908 | audio_policy_forced_cfg_t forceUseForComm = |
| 4909 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4910 | // 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] | 4911 | if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) || |
| 4912 | (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4913 | 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] | 4914 | stream, forceUseForComm); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4915 | return INVALID_OPERATION; |
| 4916 | } |
| 4917 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4918 | if (device == AUDIO_DEVICE_NONE) { |
| 4919 | device = outputDesc->device(); |
| 4920 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4921 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4922 | float volumeDb = computeVolume(stream, index, device); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4923 | if (outputDesc->isFixedVolume(device)) { |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4924 | volumeDb = 0.0f; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4925 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4926 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4927 | outputDesc->setVolume(volumeDb, stream, device, delayMs, force); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4928 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4929 | if (stream == AUDIO_STREAM_VOICE_CALL || |
| 4930 | stream == AUDIO_STREAM_BLUETOOTH_SCO) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4931 | float voiceVolume; |
| 4932 | // 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] | 4933 | if (stream == AUDIO_STREAM_VOICE_CALL) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4934 | voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4935 | } else { |
| 4936 | voiceVolume = 1.0; |
| 4937 | } |
| 4938 | |
Eric Laurent | 18fba84 | 2016-03-31 14:41:26 -0700 | [diff] [blame^] | 4939 | if (voiceVolume != mLastVoiceVolume) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4940 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); |
| 4941 | mLastVoiceVolume = voiceVolume; |
| 4942 | } |
| 4943 | } |
| 4944 | |
| 4945 | return NO_ERROR; |
| 4946 | } |
| 4947 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4948 | void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc, |
| 4949 | audio_devices_t device, |
| 4950 | int delayMs, |
| 4951 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4952 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4953 | ALOGVV("applyStreamVolumes() for device %08x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4954 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4955 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4956 | checkAndSetVolume((audio_stream_type_t)stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4957 | mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4958 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4959 | device, |
| 4960 | delayMs, |
| 4961 | force); |
| 4962 | } |
| 4963 | } |
| 4964 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4965 | void AudioPolicyManager::setStrategyMute(routing_strategy strategy, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4966 | bool on, |
| 4967 | const sp<AudioOutputDescriptor>& outputDesc, |
| 4968 | int delayMs, |
| 4969 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4970 | { |
Eric Laurent | 72249d5 | 2015-06-08 11:12:15 -0700 | [diff] [blame] | 4971 | ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d", |
| 4972 | strategy, on, outputDesc->getId()); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4973 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4974 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4975 | setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4976 | } |
| 4977 | } |
| 4978 | } |
| 4979 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4980 | void AudioPolicyManager::setStreamMute(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4981 | bool on, |
| 4982 | const sp<AudioOutputDescriptor>& outputDesc, |
| 4983 | int delayMs, |
| 4984 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4985 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4986 | if (device == AUDIO_DEVICE_NONE) { |
| 4987 | device = outputDesc->device(); |
| 4988 | } |
| 4989 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4990 | ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x", |
| 4991 | stream, on, outputDesc->mMuteCount[stream], device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4992 | |
| 4993 | if (on) { |
| 4994 | if (outputDesc->mMuteCount[stream] == 0) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4995 | if (mVolumeCurves->canBeMuted(stream) && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4996 | ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4997 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4998 | checkAndSetVolume(stream, 0, outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4999 | } |
| 5000 | } |
| 5001 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored |
| 5002 | outputDesc->mMuteCount[stream]++; |
| 5003 | } else { |
| 5004 | if (outputDesc->mMuteCount[stream] == 0) { |
| 5005 | ALOGV("setStreamMute() unmuting non muted stream!"); |
| 5006 | return; |
| 5007 | } |
| 5008 | if (--outputDesc->mMuteCount[stream] == 0) { |
| 5009 | checkAndSetVolume(stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5010 | mVolumeCurves->getVolumeIndex(stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5011 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5012 | device, |
| 5013 | delayMs); |
| 5014 | } |
| 5015 | } |
| 5016 | } |
| 5017 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5018 | void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5019 | bool starting, bool stateChange) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5020 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 5021 | if(!hasPrimaryOutput()) { |
| 5022 | return; |
| 5023 | } |
| 5024 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5025 | // if the stream pertains to sonification strategy and we are in call we must |
| 5026 | // mute the stream if it is low visibility. If it is high visibility, we must play a tone |
| 5027 | // in the device used for phone strategy and play the tone if the selected device does not |
| 5028 | // interfere with the device used for phone strategy |
| 5029 | // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as |
| 5030 | // many times as there are active tracks on the output |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5031 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5032 | if ((stream_strategy == STRATEGY_SONIFICATION) || |
| 5033 | ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5034 | sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5035 | ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d", |
| 5036 | stream, starting, outputDesc->mDevice, stateChange); |
| 5037 | if (outputDesc->mRefCount[stream]) { |
| 5038 | int muteCount = 1; |
| 5039 | if (stateChange) { |
| 5040 | muteCount = outputDesc->mRefCount[stream]; |
| 5041 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5042 | if (audio_is_low_visibility(stream)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5043 | ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount); |
| 5044 | for (int i = 0; i < muteCount; i++) { |
| 5045 | setStreamMute(stream, starting, mPrimaryOutput); |
| 5046 | } |
| 5047 | } else { |
| 5048 | ALOGV("handleIncallSonification() high visibility"); |
| 5049 | if (outputDesc->device() & |
| 5050 | getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) { |
| 5051 | ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount); |
| 5052 | for (int i = 0; i < muteCount; i++) { |
| 5053 | setStreamMute(stream, starting, mPrimaryOutput); |
| 5054 | } |
| 5055 | } |
| 5056 | if (starting) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5057 | mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION, |
| 5058 | AUDIO_STREAM_VOICE_CALL); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5059 | } else { |
| 5060 | mpClientInterface->stopTone(); |
| 5061 | } |
| 5062 | } |
| 5063 | } |
| 5064 | } |
| 5065 | } |
| 5066 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5067 | audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr) |
| 5068 | { |
| 5069 | // flags to stream type mapping |
| 5070 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 5071 | return AUDIO_STREAM_ENFORCED_AUDIBLE; |
| 5072 | } |
| 5073 | if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) { |
| 5074 | return AUDIO_STREAM_BLUETOOTH_SCO; |
| 5075 | } |
Jean-Michel Trivi | 79ad438 | 2015-01-29 10:49:39 -0800 | [diff] [blame] | 5076 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 5077 | return AUDIO_STREAM_TTS; |
| 5078 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5079 | |
| 5080 | // usage to stream type mapping |
| 5081 | switch (attr->usage) { |
| 5082 | case AUDIO_USAGE_MEDIA: |
| 5083 | case AUDIO_USAGE_GAME: |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5084 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 5085 | return AUDIO_STREAM_MUSIC; |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5086 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 5087 | return AUDIO_STREAM_ACCESSIBILITY; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5088 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 5089 | return AUDIO_STREAM_SYSTEM; |
| 5090 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 5091 | return AUDIO_STREAM_VOICE_CALL; |
| 5092 | |
| 5093 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 5094 | return AUDIO_STREAM_DTMF; |
| 5095 | |
| 5096 | case AUDIO_USAGE_ALARM: |
| 5097 | return AUDIO_STREAM_ALARM; |
| 5098 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 5099 | return AUDIO_STREAM_RING; |
| 5100 | |
| 5101 | case AUDIO_USAGE_NOTIFICATION: |
| 5102 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 5103 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 5104 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 5105 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 5106 | return AUDIO_STREAM_NOTIFICATION; |
| 5107 | |
| 5108 | case AUDIO_USAGE_UNKNOWN: |
| 5109 | default: |
| 5110 | return AUDIO_STREAM_MUSIC; |
| 5111 | } |
| 5112 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5113 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5114 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) |
| 5115 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5116 | // has flags that map to a strategy? |
| 5117 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { |
| 5118 | return true; |
| 5119 | } |
| 5120 | |
| 5121 | // has known usage? |
| 5122 | switch (paa->usage) { |
| 5123 | case AUDIO_USAGE_UNKNOWN: |
| 5124 | case AUDIO_USAGE_MEDIA: |
| 5125 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 5126 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 5127 | case AUDIO_USAGE_ALARM: |
| 5128 | case AUDIO_USAGE_NOTIFICATION: |
| 5129 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 5130 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 5131 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 5132 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 5133 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 5134 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 5135 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 5136 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 5137 | case AUDIO_USAGE_GAME: |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5138 | case AUDIO_USAGE_VIRTUAL_SOURCE: |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5139 | break; |
| 5140 | default: |
| 5141 | return false; |
| 5142 | } |
| 5143 | return true; |
| 5144 | } |
| 5145 | |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5146 | bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc, |
| 5147 | routing_strategy strategy, uint32_t inPastMs, |
| 5148 | nsecs_t sysTime) const |
| 5149 | { |
| 5150 | if ((sysTime == 0) && (inPastMs != 0)) { |
| 5151 | sysTime = systemTime(); |
| 5152 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5153 | 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] | 5154 | if (((getStrategy((audio_stream_type_t)i) == strategy) || |
| 5155 | (NUM_STRATEGIES == strategy)) && |
| 5156 | outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) { |
| 5157 | return true; |
| 5158 | } |
| 5159 | } |
| 5160 | return false; |
| 5161 | } |
| 5162 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5163 | audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage) |
| 5164 | { |
| 5165 | return mEngine->getForceUse(usage); |
| 5166 | } |
| 5167 | |
| 5168 | bool AudioPolicyManager::isInCall() |
| 5169 | { |
| 5170 | return isStateInCall(mEngine->getPhoneState()); |
| 5171 | } |
| 5172 | |
| 5173 | bool AudioPolicyManager::isStateInCall(int state) |
| 5174 | { |
| 5175 | return is_state_in_call(state); |
| 5176 | } |
| 5177 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5178 | void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc) |
| 5179 | { |
| 5180 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
| 5181 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 5182 | if (sourceDesc->mDevice->equals(deviceDesc)) { |
| 5183 | ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle()); |
| 5184 | stopAudioSource(sourceDesc->getHandle()); |
| 5185 | } |
| 5186 | } |
| 5187 | |
| 5188 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
| 5189 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 5190 | bool release = false; |
| 5191 | for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) { |
| 5192 | const struct audio_port_config *source = &patchDesc->mPatch.sources[j]; |
| 5193 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 5194 | source->ext.device.type == deviceDesc->type()) { |
| 5195 | release = true; |
| 5196 | } |
| 5197 | } |
| 5198 | for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) { |
| 5199 | const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j]; |
| 5200 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && |
| 5201 | sink->ext.device.type == deviceDesc->type()) { |
| 5202 | release = true; |
| 5203 | } |
| 5204 | } |
| 5205 | if (release) { |
| 5206 | ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle); |
| 5207 | releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid); |
| 5208 | } |
| 5209 | } |
| 5210 | } |
| 5211 | |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5212 | // Modify the list of surround sound formats supported. |
| 5213 | void AudioPolicyManager::filterSurroundFormats(FormatVector &formats) { |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5214 | // TODO Change the ALOGIs to ALOGVs in this function after the feature is verified. |
| 5215 | |
| 5216 | // TODO Set this based on Config properties. |
| 5217 | const bool alwaysForceAC3 = true; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5218 | |
| 5219 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5220 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
| 5221 | ALOGI("%s: forced use = %d", __FUNCTION__, forceUse); |
| 5222 | |
| 5223 | // Analyze original support for various formats. |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5224 | bool supportsAC3 = false; |
| 5225 | bool supportsOtherSurround = false; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5226 | bool supportsIEC61937 = false; |
| 5227 | for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) { |
| 5228 | audio_format_t format = formats[formatIndex]; |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5229 | ALOGI("%s: original formats: 0x%08x", __FUNCTION__, format); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5230 | switch (format) { |
| 5231 | case AUDIO_FORMAT_AC3: |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5232 | supportsAC3 = true; |
| 5233 | break; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5234 | case AUDIO_FORMAT_E_AC3: |
| 5235 | case AUDIO_FORMAT_DTS: |
| 5236 | case AUDIO_FORMAT_DTS_HD: |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5237 | supportsOtherSurround = true; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5238 | break; |
| 5239 | case AUDIO_FORMAT_IEC61937: |
| 5240 | supportsIEC61937 = true; |
| 5241 | break; |
| 5242 | default: |
| 5243 | break; |
| 5244 | } |
| 5245 | } |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5246 | ALOGI("%s: original, supportsAC3 = %d, supportsOtherSurround = %d, supportsIEC61937 = %d", |
| 5247 | __FUNCTION__, supportsAC3, supportsOtherSurround, supportsIEC61937); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5248 | |
| 5249 | // Modify formats based on surround preferences. |
| 5250 | // If NEVER, remove support for surround formats. |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5251 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
| 5252 | if (supportsAC3 || supportsOtherSurround || supportsIEC61937) { |
| 5253 | // Remove surround sound related formats. |
| 5254 | for (size_t formatIndex = 0; formatIndex < formats.size(); ) { |
| 5255 | audio_format_t format = formats[formatIndex]; |
| 5256 | switch(format) { |
| 5257 | case AUDIO_FORMAT_AC3: |
| 5258 | case AUDIO_FORMAT_E_AC3: |
| 5259 | case AUDIO_FORMAT_DTS: |
| 5260 | case AUDIO_FORMAT_DTS_HD: |
| 5261 | case AUDIO_FORMAT_IEC61937: |
| 5262 | ALOGI("%s: remove format 0x%08x", __FUNCTION__, format); |
| 5263 | formats.removeAt(formatIndex); |
| 5264 | break; |
| 5265 | default: |
| 5266 | formatIndex++; // keep it |
| 5267 | break; |
| 5268 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5269 | } |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5270 | supportsAC3 = false; |
| 5271 | supportsOtherSurround = false; |
| 5272 | supportsIEC61937 = false; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5273 | } |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5274 | } else { // AUTO or ALWAYS |
| 5275 | // Most TVs support AC3 even if they do not report it in the EDID. |
| 5276 | if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)) |
| 5277 | && !supportsAC3) { |
| 5278 | formats.add(AUDIO_FORMAT_AC3); |
| 5279 | supportsAC3 = true; |
| 5280 | } |
| 5281 | |
| 5282 | // If ALWAYS, add support for raw surround formats if all are missing. |
| 5283 | // This assumes that if any of these formats are reported by the HAL |
| 5284 | // then the report is valid and should not be modified. |
| 5285 | if ((forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) |
| 5286 | && !supportsOtherSurround) { |
| 5287 | formats.add(AUDIO_FORMAT_E_AC3); |
| 5288 | formats.add(AUDIO_FORMAT_DTS); |
| 5289 | formats.add(AUDIO_FORMAT_DTS_HD); |
| 5290 | supportsOtherSurround = true; |
| 5291 | } |
| 5292 | |
| 5293 | // Add support for IEC61937 if any raw surround supported. |
| 5294 | // The HAL could do this but add it here, just in case. |
| 5295 | if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) { |
| 5296 | formats.add(AUDIO_FORMAT_IEC61937); |
| 5297 | supportsIEC61937 = true; |
| 5298 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5299 | } |
| 5300 | // Just for debugging. |
| 5301 | for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) { |
| 5302 | audio_format_t format = formats[formatIndex]; |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5303 | ALOGI("%s: final formats: 0x%08x", __FUNCTION__, format); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5304 | } |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5305 | ALOGI("%s: final, supportsAC3 = %d, supportsOtherSurround = %d, supportsIEC61937 = %d", |
| 5306 | __FUNCTION__, supportsAC3, supportsOtherSurround, supportsIEC61937); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5307 | } |
| 5308 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5309 | void AudioPolicyManager::updateAudioProfiles(audio_io_handle_t ioHandle, |
| 5310 | AudioProfileVector &profiles) |
| 5311 | { |
| 5312 | String8 reply; |
| 5313 | char *value; |
| 5314 | // Format MUST be checked first to update the list of AudioProfile |
| 5315 | if (profiles.hasDynamicFormat()) { |
| 5316 | reply = mpClientInterface->getParameters(ioHandle, |
| 5317 | String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS)); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5318 | ALOGI("%s: supported formats %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5319 | AudioParameter repliedParameters(reply); |
| 5320 | if (repliedParameters.get( |
| 5321 | String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS), reply) != NO_ERROR) { |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5322 | ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__); |
| 5323 | return; |
| 5324 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5325 | FormatVector formats = formatsFromString(reply.string()); |
| 5326 | filterSurroundFormats(formats); |
| 5327 | profiles.setFormats(formats); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5328 | } |
| 5329 | const FormatVector &supportedFormats = profiles.getSupportedFormats(); |
| 5330 | |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5331 | for (size_t formatIndex = 0; formatIndex < supportedFormats.size(); formatIndex++) { |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5332 | audio_format_t format = supportedFormats[formatIndex]; |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5333 | ChannelsVector channelMasks; |
| 5334 | SampleRateVector samplingRates; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5335 | AudioParameter requestedParameters; |
| 5336 | requestedParameters.addInt(String8(AUDIO_PARAMETER_STREAM_FORMAT), format); |
| 5337 | |
| 5338 | if (profiles.hasDynamicRateFor(format)) { |
| 5339 | reply = mpClientInterface->getParameters(ioHandle, |
| 5340 | requestedParameters.toString() + ";" + |
| 5341 | AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES); |
| 5342 | ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5343 | AudioParameter repliedParameters(reply); |
| 5344 | if (repliedParameters.get( |
| 5345 | String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES), reply) == NO_ERROR) { |
| 5346 | samplingRates = samplingRatesFromString(reply.string()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5347 | } |
| 5348 | } |
| 5349 | if (profiles.hasDynamicChannelsFor(format)) { |
| 5350 | reply = mpClientInterface->getParameters(ioHandle, |
| 5351 | requestedParameters.toString() + ";" + |
| 5352 | AUDIO_PARAMETER_STREAM_SUP_CHANNELS); |
| 5353 | ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5354 | AudioParameter repliedParameters(reply); |
| 5355 | if (repliedParameters.get( |
| 5356 | String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS), reply) == NO_ERROR) { |
| 5357 | channelMasks = channelMasksFromString(reply.string()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5358 | } |
| 5359 | } |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5360 | profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates)); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5361 | } |
| 5362 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5363 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5364 | }; // namespace android |