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