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