| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2009 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
| Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 17 | #define LOG_TAG "APM_AudioPolicyManager" | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 | 
|  | 19 |  | 
|  | 20 | //#define VERY_VERBOSE_LOGGING | 
|  | 21 | #ifdef VERY_VERBOSE_LOGGING | 
|  | 22 | #define ALOGVV ALOGV | 
|  | 23 | #else | 
|  | 24 | #define ALOGVV(a...) do { } while(0) | 
|  | 25 | #endif | 
|  | 26 |  | 
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 27 | #define AUDIO_POLICY_XML_CONFIG_FILE "/system/etc/audio_policy_configuration.xml" | 
|  | 28 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 29 | #include <inttypes.h> | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 30 | #include <math.h> | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 31 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 32 | #include <AudioPolicyManagerInterface.h> | 
|  | 33 | #include <AudioPolicyEngineInstance.h> | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 34 | #include <cutils/properties.h> | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 35 | #include <utils/Log.h> | 
|  | 36 | #include <hardware/audio.h> | 
|  | 37 | #include <hardware/audio_effect.h> | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 38 | #include <media/AudioParameter.h> | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 39 | #include <media/AudioPolicyHelper.h> | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 40 | #include <soundtrigger/SoundTrigger.h> | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 41 | #include "AudioPolicyManager.h" | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 42 | #ifndef USE_XML_AUDIO_POLICY_CONF | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 43 | #include <ConfigParsingUtils.h> | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 44 | #include <StreamDescriptor.h> | 
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 45 | #endif | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 46 | #include <Serializer.h> | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 47 | #include "TypeConverter.h" | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 48 | #include <policy.h> | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 49 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 50 | namespace android { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 51 |  | 
|  | 52 | // ---------------------------------------------------------------------------- | 
|  | 53 | // AudioPolicyInterface implementation | 
|  | 54 | // ---------------------------------------------------------------------------- | 
|  | 55 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 56 | status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device, | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 57 | audio_policy_dev_state_t state, | 
|  | 58 | const char *device_address, | 
|  | 59 | const char *device_name) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 60 | { | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 61 | return setDeviceConnectionStateInt(device, state, device_address, device_name); | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 62 | } | 
|  | 63 |  | 
|  | 64 | status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device, | 
| Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 65 | audio_policy_dev_state_t state, | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 66 | const char *device_address, | 
|  | 67 | const char *device_name) | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 68 | { | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 69 | ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s", | 
|  | 70 | -            device, state, device_address, device_name); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 71 |  | 
|  | 72 | // connect/disconnect only 1 device at a time | 
|  | 73 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; | 
|  | 74 |  | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 75 | sp<DeviceDescriptor> devDesc = | 
|  | 76 | mHwModules.getDeviceDescriptor(device, device_address, device_name); | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 77 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 78 | // handle output devices | 
|  | 79 | if (audio_is_output_device(device)) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 80 | SortedVector <audio_io_handle_t> outputs; | 
|  | 81 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 82 | ssize_t index = mAvailableOutputDevices.indexOf(devDesc); | 
|  | 83 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 84 | // save a copy of the opened output descriptors before any output is opened or closed | 
|  | 85 | // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies() | 
|  | 86 | mPreviousOutputs = mOutputs; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 87 | switch (state) | 
|  | 88 | { | 
|  | 89 | // handle output device connection | 
| Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 90 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 91 | if (index >= 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 92 | ALOGW("setDeviceConnectionState() device already connected: %x", device); | 
|  | 93 | return INVALID_OPERATION; | 
|  | 94 | } | 
|  | 95 | ALOGV("setDeviceConnectionState() connecting device %x", device); | 
|  | 96 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 97 | // register new device as available | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 98 | index = mAvailableOutputDevices.add(devDesc); | 
|  | 99 | if (index >= 0) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 100 | sp<HwModule> module = mHwModules.getModuleForDevice(device); | 
| Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 101 | if (module == 0) { | 
|  | 102 | ALOGD("setDeviceConnectionState() could not find HW module for device %08x", | 
|  | 103 | device); | 
|  | 104 | mAvailableOutputDevices.remove(devDesc); | 
|  | 105 | return INVALID_OPERATION; | 
|  | 106 | } | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 107 | mAvailableOutputDevices[index]->attach(module); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 108 | } else { | 
|  | 109 | return NO_MEMORY; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 110 | } | 
|  | 111 |  | 
| Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 112 | if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) { | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 113 | mAvailableOutputDevices.remove(devDesc); | 
|  | 114 | return INVALID_OPERATION; | 
|  | 115 | } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 116 | // Propagate device availability to Engine | 
|  | 117 | mEngine->setDeviceConnectionState(devDesc, state); | 
|  | 118 |  | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 119 | // outputs should never be empty here | 
|  | 120 | ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():" | 
|  | 121 | "checkOutputsForDevice() returned no outputs but status OK"); | 
|  | 122 | ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs", | 
|  | 123 | outputs.size()); | 
| Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 124 |  | 
| Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 125 | // Send connect to HALs | 
| Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 126 | AudioParameter param = AudioParameter(devDesc->mAddress); | 
|  | 127 | param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device); | 
|  | 128 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); | 
|  | 129 |  | 
|  | 130 | } break; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 131 | // handle output device disconnection | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 132 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 133 | if (index < 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 134 | ALOGW("setDeviceConnectionState() device not connected: %x", device); | 
|  | 135 | return INVALID_OPERATION; | 
|  | 136 | } | 
|  | 137 |  | 
| Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 138 | ALOGV("setDeviceConnectionState() disconnecting output device %x", device); | 
|  | 139 |  | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 140 | // Send Disconnect to HALs | 
| Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 141 | AudioParameter param = AudioParameter(devDesc->mAddress); | 
| Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 142 | param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device); | 
|  | 143 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); | 
|  | 144 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 145 | // remove device from available output devices | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 146 | mAvailableOutputDevices.remove(devDesc); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 147 |  | 
| Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 148 | checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress); | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 149 |  | 
|  | 150 | // Propagate device availability to Engine | 
|  | 151 | mEngine->setDeviceConnectionState(devDesc, state); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 152 | } break; | 
|  | 153 |  | 
|  | 154 | default: | 
|  | 155 | ALOGE("setDeviceConnectionState() invalid state: %x", state); | 
|  | 156 | return BAD_VALUE; | 
|  | 157 | } | 
|  | 158 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 159 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP | 
|  | 160 | // output is suspended before any tracks are moved to it | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 161 | checkA2dpSuspend(); | 
|  | 162 | checkOutputForAllStrategies(); | 
|  | 163 | // outputs must be closed after checkOutputForAllStrategies() is executed | 
|  | 164 | if (!outputs.isEmpty()) { | 
|  | 165 | for (size_t i = 0; i < outputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 166 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 167 | // close unused outputs after device disconnection or direct outputs that have been | 
|  | 168 | // opened by checkOutputsForDevice() to query dynamic parameters | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 169 | if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) || | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 170 | (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) && | 
|  | 171 | (desc->mDirectOpenCount == 0))) { | 
|  | 172 | closeOutput(outputs[i]); | 
|  | 173 | } | 
|  | 174 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 175 | // check again after closing A2DP output to reset mA2dpSuspended if needed | 
|  | 176 | checkA2dpSuspend(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 177 | } | 
|  | 178 |  | 
|  | 179 | updateDevicesAndOutputs(); | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 180 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 181 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); | 
|  | 182 | updateCallRouting(newDevice); | 
|  | 183 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 184 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 185 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
|  | 186 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) { | 
|  | 187 | audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 188 | // do not force device change on duplicated output because if device is 0, it will | 
|  | 189 | // also force a device 0 for the two outputs it is duplicated to which may override | 
|  | 190 | // a valid device selection on those outputs. | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 191 | bool force = !desc->isDuplicated() | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 192 | && (!device_distinguishes_on_address(device) | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 193 | // always force when disconnecting (a non-duplicated device) | 
|  | 194 | || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 195 | setOutputDevice(desc, newDevice, force, 0); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 196 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 197 | } | 
|  | 198 |  | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 199 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { | 
|  | 200 | cleanUpForDevice(devDesc); | 
|  | 201 | } | 
|  | 202 |  | 
| Eric Laurent | 72aa32f | 2014-05-30 18:51:48 -0700 | [diff] [blame] | 203 | mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | b71e58b | 2014-05-29 16:08:11 -0700 | [diff] [blame] | 204 | return NO_ERROR; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 205 | }  // end if is output device | 
|  | 206 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 207 | // handle input devices | 
|  | 208 | if (audio_is_input_device(device)) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 209 | SortedVector <audio_io_handle_t> inputs; | 
|  | 210 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 211 | ssize_t index = mAvailableInputDevices.indexOf(devDesc); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 212 | switch (state) | 
|  | 213 | { | 
|  | 214 | // handle input device connection | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 215 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 216 | if (index >= 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 217 | ALOGW("setDeviceConnectionState() device already connected: %d", device); | 
|  | 218 | return INVALID_OPERATION; | 
|  | 219 | } | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 220 | sp<HwModule> module = mHwModules.getModuleForDevice(device); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 221 | if (module == NULL) { | 
|  | 222 | ALOGW("setDeviceConnectionState(): could not find HW module for device %08x", | 
|  | 223 | device); | 
|  | 224 | return INVALID_OPERATION; | 
|  | 225 | } | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 226 | if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 227 | return INVALID_OPERATION; | 
|  | 228 | } | 
|  | 229 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 230 | index = mAvailableInputDevices.add(devDesc); | 
|  | 231 | if (index >= 0) { | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 232 | mAvailableInputDevices[index]->attach(module); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 233 | } else { | 
|  | 234 | return NO_MEMORY; | 
|  | 235 | } | 
| Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 236 |  | 
|  | 237 | // Set connect to HALs | 
|  | 238 | AudioParameter param = AudioParameter(devDesc->mAddress); | 
|  | 239 | param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device); | 
|  | 240 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); | 
|  | 241 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 242 | // Propagate device availability to Engine | 
|  | 243 | mEngine->setDeviceConnectionState(devDesc, state); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 244 | } break; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 245 |  | 
|  | 246 | // handle input device disconnection | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 247 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 248 | if (index < 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 249 | ALOGW("setDeviceConnectionState() device not connected: %d", device); | 
|  | 250 | return INVALID_OPERATION; | 
|  | 251 | } | 
| Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 252 |  | 
|  | 253 | ALOGV("setDeviceConnectionState() disconnecting input device %x", device); | 
|  | 254 |  | 
|  | 255 | // Set Disconnect to HALs | 
| Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 256 | AudioParameter param = AudioParameter(devDesc->mAddress); | 
| Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 257 | param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device); | 
|  | 258 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); | 
|  | 259 |  | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 260 | checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 261 | mAvailableInputDevices.remove(devDesc); | 
| Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 262 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 263 | // Propagate device availability to Engine | 
|  | 264 | mEngine->setDeviceConnectionState(devDesc, state); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 265 | } break; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 266 |  | 
|  | 267 | default: | 
|  | 268 | ALOGE("setDeviceConnectionState() invalid state: %x", state); | 
|  | 269 | return BAD_VALUE; | 
|  | 270 | } | 
|  | 271 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 272 | closeAllInputs(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 273 |  | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 274 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 275 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); | 
|  | 276 | updateCallRouting(newDevice); | 
|  | 277 | } | 
|  | 278 |  | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 279 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { | 
|  | 280 | cleanUpForDevice(devDesc); | 
|  | 281 | } | 
|  | 282 |  | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 283 | mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 284 | return NO_ERROR; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 285 | } // end if is input device | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 286 |  | 
|  | 287 | ALOGW("setDeviceConnectionState() invalid device: %x", device); | 
|  | 288 | return BAD_VALUE; | 
|  | 289 | } | 
|  | 290 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 291 | audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 292 | const char *device_address) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 293 | { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 294 | sp<DeviceDescriptor> devDesc = mHwModules.getDeviceDescriptor(device, device_address, ""); | 
|  | 295 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 296 | DeviceVector *deviceVector; | 
|  | 297 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 298 | if (audio_is_output_device(device)) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 299 | deviceVector = &mAvailableOutputDevices; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 300 | } else if (audio_is_input_device(device)) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 301 | deviceVector = &mAvailableInputDevices; | 
|  | 302 | } else { | 
|  | 303 | ALOGW("getDeviceConnectionState() invalid device type %08x", device); | 
|  | 304 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 305 | } | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 306 | return deviceVector->getDeviceConnectionState(devDesc); | 
| Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 307 | } | 
|  | 308 |  | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 309 | void AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, int delayMs) | 
|  | 310 | { | 
|  | 311 | bool createTxPatch = false; | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 312 | status_t status; | 
|  | 313 | audio_patch_handle_t afPatchHandle; | 
|  | 314 | DeviceVector deviceList; | 
|  | 315 |  | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 316 | if(!hasPrimaryOutput()) { | 
|  | 317 | return; | 
|  | 318 | } | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 319 | audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 320 | ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice); | 
|  | 321 |  | 
|  | 322 | // release existing RX patch if any | 
|  | 323 | if (mCallRxPatch != 0) { | 
|  | 324 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); | 
|  | 325 | mCallRxPatch.clear(); | 
|  | 326 | } | 
|  | 327 | // release TX patch if any | 
|  | 328 | if (mCallTxPatch != 0) { | 
|  | 329 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); | 
|  | 330 | mCallTxPatch.clear(); | 
|  | 331 | } | 
|  | 332 |  | 
|  | 333 | // If the RX device is on the primary HW module, then use legacy routing method for voice calls | 
|  | 334 | // via setOutputDevice() on primary output. | 
|  | 335 | // Otherwise, create two audio patches for TX and RX path. | 
|  | 336 | if (availablePrimaryOutputDevices() & rxDevice) { | 
|  | 337 | setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs); | 
|  | 338 | // If the TX device is also on the primary HW module, setOutputDevice() will take care | 
|  | 339 | // of it due to legacy implementation. If not, create a patch. | 
|  | 340 | if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN) | 
|  | 341 | == AUDIO_DEVICE_NONE) { | 
|  | 342 | createTxPatch = true; | 
|  | 343 | } | 
| Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 344 | } else { // create RX path audio patch | 
|  | 345 | struct audio_patch patch; | 
|  | 346 |  | 
|  | 347 | patch.num_sources = 1; | 
|  | 348 | patch.num_sinks = 1; | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 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 | } | 
| Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 387 | if (createTxPatch) { // create TX path audio patch | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 388 | struct audio_patch patch; | 
| Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 389 |  | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 390 | patch.num_sources = 1; | 
|  | 391 | patch.num_sinks = 1; | 
|  | 392 | deviceList = mAvailableInputDevices.getDevicesFromType(txDevice); | 
|  | 393 | ALOG_ASSERT(!deviceList.isEmpty(), | 
|  | 394 | "updateCallRouting() selected device not in input device list"); | 
|  | 395 | sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0); | 
|  | 396 | txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]); | 
|  | 397 | deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX); | 
|  | 398 | ALOG_ASSERT(!deviceList.isEmpty(), | 
|  | 399 | "updateCallRouting() no telephony TX device"); | 
|  | 400 | sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0); | 
|  | 401 | txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]); | 
|  | 402 |  | 
|  | 403 | SortedVector<audio_io_handle_t> outputs = | 
|  | 404 | getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs); | 
| Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 405 | audio_io_handle_t output = selectOutput(outputs, | 
|  | 406 | AUDIO_OUTPUT_FLAG_NONE, | 
|  | 407 | AUDIO_FORMAT_INVALID); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 408 | // request to reuse existing output stream if one is already opened to reach the TX | 
|  | 409 | // path output device | 
|  | 410 | if (output != AUDIO_IO_HANDLE_NONE) { | 
|  | 411 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
|  | 412 | ALOG_ASSERT(!outputDesc->isDuplicated(), | 
|  | 413 | "updateCallRouting() RX device output is duplicated"); | 
|  | 414 | outputDesc->toAudioPortConfig(&patch.sources[1]); | 
| Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 415 | patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 416 | patch.num_sources = 2; | 
|  | 417 | } | 
|  | 418 |  | 
| Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 419 | // terminate active capture if on the same HW module as the call TX source device | 
|  | 420 | // FIXME: would be better to refine to only inputs whose profile connects to the | 
|  | 421 | // call TX device but this information is not in the audio patch and logic here must be | 
|  | 422 | // symmetric to the one in startInput() | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 423 | audio_io_handle_t activeInput = mInputs.getActiveInput(); | 
|  | 424 | if (activeInput != 0) { | 
|  | 425 | sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput); | 
|  | 426 | if (activeDesc->getModuleHandle() == txSourceDeviceDesc->getModuleHandle()) { | 
|  | 427 | //FIXME: consider all active sessions | 
|  | 428 | AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions(); | 
|  | 429 | audio_session_t activeSession = activeSessions.keyAt(0); | 
|  | 430 | stopInput(activeInput, activeSession); | 
|  | 431 | releaseInput(activeInput, activeSession); | 
| Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 432 | } | 
|  | 433 | } | 
|  | 434 |  | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 435 | afPatchHandle = AUDIO_PATCH_HANDLE_NONE; | 
|  | 436 | status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0); | 
|  | 437 | ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch", | 
|  | 438 | status); | 
|  | 439 | if (status == NO_ERROR) { | 
| François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 440 | mCallTxPatch = new AudioPatch(&patch, mUidCached); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 441 | mCallTxPatch->mAfPatchHandle = afPatchHandle; | 
|  | 442 | mCallTxPatch->mUid = mUidCached; | 
|  | 443 | } | 
|  | 444 | } | 
|  | 445 | } | 
|  | 446 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 447 | void AudioPolicyManager::setPhoneState(audio_mode_t state) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 448 | { | 
|  | 449 | ALOGV("setPhoneState() state %d", state); | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 450 | // store previous phone state for management of sonification strategy below | 
|  | 451 | int oldState = mEngine->getPhoneState(); | 
|  | 452 |  | 
|  | 453 | if (mEngine->setPhoneState(state) != NO_ERROR) { | 
|  | 454 | ALOGW("setPhoneState() invalid or same state %d", state); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 455 | return; | 
|  | 456 | } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 457 | /// Opens: can these line be executed after the switch of volume curves??? | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 458 | // if leaving call state, handle special case of active streams | 
|  | 459 | // pertaining to sonification strategy see handleIncallSonification() | 
| Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 460 | if (isStateInCall(oldState)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 461 | ALOGV("setPhoneState() in call state management: new state is %d", state); | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 462 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 463 | handleIncallSonification((audio_stream_type_t)stream, false, true); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 464 | } | 
| Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 465 |  | 
| Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 466 | // force reevaluating accessibility routing when call stops | 
| Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 467 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 468 | } | 
|  | 469 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 470 | /** | 
|  | 471 | * Switching to or from incall state or switching between telephony and VoIP lead to force | 
|  | 472 | * routing command. | 
|  | 473 | */ | 
|  | 474 | bool force = ((is_state_in_call(oldState) != is_state_in_call(state)) | 
|  | 475 | || (is_state_in_call(state) && (state != oldState))); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 476 |  | 
|  | 477 | // check for device and output changes triggered by new phone state | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 478 | checkA2dpSuspend(); | 
|  | 479 | checkOutputForAllStrategies(); | 
|  | 480 | updateDevicesAndOutputs(); | 
|  | 481 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 482 | int delayMs = 0; | 
|  | 483 | if (isStateInCall(state)) { | 
|  | 484 | nsecs_t sysTime = systemTime(); | 
|  | 485 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 486 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 487 | // mute media and sonification strategies and delay device switch by the largest | 
|  | 488 | // latency of any output where either strategy is active. | 
|  | 489 | // This avoid sending the ring tone or music tail into the earpiece or headset. | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 490 | if ((isStrategyActive(desc, STRATEGY_MEDIA, | 
|  | 491 | SONIFICATION_HEADSET_MUSIC_DELAY, | 
|  | 492 | sysTime) || | 
|  | 493 | isStrategyActive(desc, STRATEGY_SONIFICATION, | 
|  | 494 | SONIFICATION_HEADSET_MUSIC_DELAY, | 
|  | 495 | sysTime)) && | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 496 | (delayMs < (int)desc->latency()*2)) { | 
|  | 497 | delayMs = desc->latency()*2; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 498 | } | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 499 | setStrategyMute(STRATEGY_MEDIA, true, desc); | 
|  | 500 | setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 501 | getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/)); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 502 | setStrategyMute(STRATEGY_SONIFICATION, true, desc); | 
|  | 503 | setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 504 | getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/)); | 
|  | 505 | } | 
|  | 506 | } | 
|  | 507 |  | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 508 | if (hasPrimaryOutput()) { | 
|  | 509 | // Note that despite the fact that getNewOutputDevice() is called on the primary output, | 
|  | 510 | // the device returned is not necessarily reachable via this output | 
|  | 511 | audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); | 
|  | 512 | // force routing command to audio hardware when ending call | 
|  | 513 | // even if no device change is needed | 
|  | 514 | if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) { | 
|  | 515 | rxDevice = mPrimaryOutput->device(); | 
|  | 516 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 517 |  | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 518 | if (state == AUDIO_MODE_IN_CALL) { | 
|  | 519 | updateCallRouting(rxDevice, delayMs); | 
|  | 520 | } else if (oldState == AUDIO_MODE_IN_CALL) { | 
|  | 521 | if (mCallRxPatch != 0) { | 
|  | 522 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); | 
|  | 523 | mCallRxPatch.clear(); | 
|  | 524 | } | 
|  | 525 | if (mCallTxPatch != 0) { | 
|  | 526 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); | 
|  | 527 | mCallTxPatch.clear(); | 
|  | 528 | } | 
|  | 529 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); | 
|  | 530 | } else { | 
|  | 531 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 532 | } | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 533 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 534 | // if entering in call state, handle special case of active streams | 
|  | 535 | // pertaining to sonification strategy see handleIncallSonification() | 
|  | 536 | if (isStateInCall(state)) { | 
|  | 537 | ALOGV("setPhoneState() in call state management: new state is %d", state); | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 538 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 539 | handleIncallSonification((audio_stream_type_t)stream, true, true); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 540 | } | 
| Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 541 |  | 
|  | 542 | // force reevaluating accessibility routing when call starts | 
|  | 543 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 544 | } | 
|  | 545 |  | 
|  | 546 | // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 547 | if (state == AUDIO_MODE_RINGTONE && | 
|  | 548 | isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 549 | mLimitRingtoneVolume = true; | 
|  | 550 | } else { | 
|  | 551 | mLimitRingtoneVolume = false; | 
|  | 552 | } | 
|  | 553 | } | 
|  | 554 |  | 
| Jean-Michel Trivi | 887a9ed | 2015-03-31 18:02:24 -0700 | [diff] [blame] | 555 | audio_mode_t AudioPolicyManager::getPhoneState() { | 
|  | 556 | return mEngine->getPhoneState(); | 
|  | 557 | } | 
|  | 558 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 559 | void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 560 | audio_policy_forced_cfg_t config) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 561 | { | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 562 | ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 563 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 564 | if (mEngine->setForceUse(usage, config) != NO_ERROR) { | 
|  | 565 | ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage); | 
|  | 566 | return; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 567 | } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 568 | bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) || | 
|  | 569 | (usage == AUDIO_POLICY_FORCE_FOR_DOCK) || | 
|  | 570 | (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 571 |  | 
|  | 572 | // check for device and output changes triggered by new force usage | 
|  | 573 | checkA2dpSuspend(); | 
|  | 574 | checkOutputForAllStrategies(); | 
|  | 575 | updateDevicesAndOutputs(); | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 576 |  | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 577 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 578 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/); | 
|  | 579 | updateCallRouting(newDevice); | 
|  | 580 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 581 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 582 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); | 
|  | 583 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/); | 
|  | 584 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) { | 
|  | 585 | setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE)); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 586 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 587 | if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 588 | applyStreamVolumes(outputDesc, newDevice, 0, true); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 589 | } | 
|  | 590 | } | 
|  | 591 |  | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 592 | audio_io_handle_t activeInput = mInputs.getActiveInput(); | 
|  | 593 | if (activeInput != 0) { | 
|  | 594 | sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput); | 
|  | 595 | audio_devices_t newDevice = getNewInputDevice(activeInput); | 
| Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 596 | // Force new input selection if the new device can not be reached via current input | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 597 | if (activeDesc->mProfile->getSupportedDevices().types() & (newDevice & ~AUDIO_DEVICE_BIT_IN)) { | 
|  | 598 | setInputDevice(activeInput, newDevice); | 
| Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 599 | } else { | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 600 | closeInput(activeInput); | 
| Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 601 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 602 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 603 | } | 
|  | 604 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 605 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 606 | { | 
|  | 607 | ALOGV("setSystemProperty() property %s, value %s", property, value); | 
|  | 608 | } | 
|  | 609 |  | 
|  | 610 | // Find a direct output profile compatible with the parameters passed, even if the input flags do | 
|  | 611 | // not explicitly request a direct output | 
| Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 612 | sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput( | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 613 | audio_devices_t device, | 
|  | 614 | uint32_t samplingRate, | 
|  | 615 | audio_format_t format, | 
|  | 616 | audio_channel_mask_t channelMask, | 
|  | 617 | audio_output_flags_t flags) | 
|  | 618 | { | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 619 | // only retain flags that will drive the direct output profile selection | 
|  | 620 | // if explicitly requested | 
|  | 621 | static const uint32_t kRelevantFlags = | 
|  | 622 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); | 
|  | 623 | flags = | 
|  | 624 | (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); | 
|  | 625 |  | 
|  | 626 | sp<IOProfile> profile; | 
|  | 627 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 628 | for (size_t i = 0; i < mHwModules.size(); i++) { | 
|  | 629 | if (mHwModules[i]->mHandle == 0) { | 
|  | 630 | continue; | 
|  | 631 | } | 
|  | 632 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) { | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 633 | sp<IOProfile> curProfile = mHwModules[i]->mOutputProfiles[j]; | 
|  | 634 | if (!curProfile->isCompatibleProfile(device, String8(""), | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 635 | samplingRate, NULL /*updatedSamplingRate*/, | 
|  | 636 | format, NULL /*updatedFormat*/, | 
|  | 637 | channelMask, NULL /*updatedChannelMask*/, | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 638 | flags)) { | 
|  | 639 | continue; | 
|  | 640 | } | 
|  | 641 | // reject profiles not corresponding to a device currently available | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 642 | if ((mAvailableOutputDevices.types() & curProfile->getSupportedDevicesType()) == 0) { | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 643 | continue; | 
|  | 644 | } | 
|  | 645 | // if several profiles are compatible, give priority to one with offload capability | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 646 | if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) { | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 647 | continue; | 
|  | 648 | } | 
|  | 649 | profile = curProfile; | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 650 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 651 | break; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 652 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 653 | } | 
|  | 654 | } | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 655 | return profile; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 656 | } | 
|  | 657 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 658 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 659 | uint32_t samplingRate, | 
|  | 660 | audio_format_t format, | 
|  | 661 | audio_channel_mask_t channelMask, | 
|  | 662 | audio_output_flags_t flags, | 
|  | 663 | const audio_offload_info_t *offloadInfo) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 664 | { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 665 | routing_strategy strategy = getStrategy(stream); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 666 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); | 
|  | 667 | ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x", | 
|  | 668 | device, stream, samplingRate, format, channelMask, flags); | 
|  | 669 |  | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 670 | return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE, | 
|  | 671 | stream, samplingRate,format, channelMask, | 
|  | 672 | flags, offloadInfo); | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 673 | } | 
|  | 674 |  | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 675 | status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, | 
|  | 676 | audio_io_handle_t *output, | 
|  | 677 | audio_session_t session, | 
|  | 678 | audio_stream_type_t *stream, | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 679 | uid_t uid, | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 680 | uint32_t samplingRate, | 
|  | 681 | audio_format_t format, | 
|  | 682 | audio_channel_mask_t channelMask, | 
|  | 683 | audio_output_flags_t flags, | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 684 | audio_port_handle_t selectedDeviceId, | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 685 | const audio_offload_info_t *offloadInfo) | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 686 | { | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 687 | audio_attributes_t attributes; | 
|  | 688 | if (attr != NULL) { | 
|  | 689 | if (!isValidAttributes(attr)) { | 
|  | 690 | ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]", | 
|  | 691 | attr->usage, attr->content_type, attr->flags, | 
|  | 692 | attr->tags); | 
|  | 693 | return BAD_VALUE; | 
|  | 694 | } | 
|  | 695 | attributes = *attr; | 
|  | 696 | } else { | 
|  | 697 | if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) { | 
|  | 698 | ALOGE("getOutputForAttr():  invalid stream type"); | 
|  | 699 | return BAD_VALUE; | 
|  | 700 | } | 
|  | 701 | stream_type_to_audio_attributes(*stream, &attributes); | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 702 | } | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 703 | sp<SwAudioOutputDescriptor> desc; | 
| Jean-Michel Trivi | e8deced | 2016-02-11 12:50:39 -0800 | [diff] [blame] | 704 | if (mPolicyMixes.getOutputForAttr(attributes, uid, desc) == NO_ERROR) { | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 705 | ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr"); | 
| Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 706 | if (!audio_has_proportional_frames(format)) { | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 707 | return BAD_VALUE; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 708 | } | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 709 | *stream = streamTypefromAttributesInt(&attributes); | 
|  | 710 | *output = desc->mIoHandle; | 
|  | 711 | ALOGV("getOutputForAttr() returns output %d", *output); | 
|  | 712 | return NO_ERROR; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 713 | } | 
|  | 714 | if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) { | 
|  | 715 | ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE"); | 
|  | 716 | return BAD_VALUE; | 
|  | 717 | } | 
|  | 718 |  | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 719 | ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x" | 
|  | 720 | " session %d selectedDeviceId %d", | 
|  | 721 | attributes.usage, attributes.content_type, attributes.tags, attributes.flags, | 
|  | 722 | session, selectedDeviceId); | 
|  | 723 |  | 
|  | 724 | *stream = streamTypefromAttributesInt(&attributes); | 
|  | 725 |  | 
|  | 726 | // Explicit routing? | 
|  | 727 | sp<DeviceDescriptor> deviceDesc; | 
|  | 728 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { | 
|  | 729 | if (mAvailableOutputDevices[i]->getId() == selectedDeviceId) { | 
|  | 730 | deviceDesc = mAvailableOutputDevices[i]; | 
|  | 731 | break; | 
|  | 732 | } | 
|  | 733 | } | 
|  | 734 | mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid); | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 735 |  | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 736 | routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes); | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 737 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); | 
| Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 738 |  | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 739 | if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) { | 
| Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 740 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); | 
|  | 741 | } | 
|  | 742 |  | 
| Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 743 | ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x", | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 744 | device, samplingRate, format, channelMask, flags); | 
|  | 745 |  | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 746 | *output = getOutputForDevice(device, session, *stream, | 
|  | 747 | samplingRate, format, channelMask, | 
|  | 748 | flags, offloadInfo); | 
|  | 749 | if (*output == AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 750 | mOutputRoutes.removeRoute(session); | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 751 | return INVALID_OPERATION; | 
|  | 752 | } | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 753 |  | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 754 | return NO_ERROR; | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 755 | } | 
|  | 756 |  | 
|  | 757 | audio_io_handle_t AudioPolicyManager::getOutputForDevice( | 
|  | 758 | audio_devices_t device, | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 759 | audio_session_t session __unused, | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 760 | audio_stream_type_t stream, | 
|  | 761 | uint32_t samplingRate, | 
|  | 762 | audio_format_t format, | 
|  | 763 | audio_channel_mask_t channelMask, | 
|  | 764 | audio_output_flags_t flags, | 
|  | 765 | const audio_offload_info_t *offloadInfo) | 
|  | 766 | { | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 767 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 768 | uint32_t latency = 0; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 769 | status_t status; | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 770 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 771 | #ifdef AUDIO_POLICY_TEST | 
|  | 772 | if (mCurOutput != 0) { | 
|  | 773 | ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d", | 
|  | 774 | mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput); | 
|  | 775 |  | 
|  | 776 | if (mTestOutputs[mCurOutput] == 0) { | 
|  | 777 | ALOGV("getOutput() opening test output"); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 778 | sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL, | 
|  | 779 | mpClientInterface); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 780 | outputDesc->mDevice = mTestDevice; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 781 | outputDesc->mLatency = mTestLatencyMs; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 782 | outputDesc->mFlags = | 
|  | 783 | (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 784 | outputDesc->mRefCount[stream] = 0; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 785 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
|  | 786 | config.sample_rate = mTestSamplingRate; | 
|  | 787 | config.channel_mask = mTestChannels; | 
|  | 788 | config.format = mTestFormat; | 
| Phil Burk | 77cce80 | 2014-08-04 16:18:15 -0700 | [diff] [blame] | 789 | if (offloadInfo != NULL) { | 
|  | 790 | config.offload_info = *offloadInfo; | 
|  | 791 | } | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 792 | status = mpClientInterface->openOutput(0, | 
|  | 793 | &mTestOutputs[mCurOutput], | 
|  | 794 | &config, | 
|  | 795 | &outputDesc->mDevice, | 
|  | 796 | String8(""), | 
|  | 797 | &outputDesc->mLatency, | 
|  | 798 | outputDesc->mFlags); | 
|  | 799 | if (status == NO_ERROR) { | 
|  | 800 | outputDesc->mSamplingRate = config.sample_rate; | 
|  | 801 | outputDesc->mFormat = config.format; | 
|  | 802 | outputDesc->mChannelMask = config.channel_mask; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 803 | AudioParameter outputCmd = AudioParameter(); | 
|  | 804 | outputCmd.addInt(String8("set_id"),mCurOutput); | 
|  | 805 | mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString()); | 
|  | 806 | addOutput(mTestOutputs[mCurOutput], outputDesc); | 
|  | 807 | } | 
|  | 808 | } | 
|  | 809 | return mTestOutputs[mCurOutput]; | 
|  | 810 | } | 
|  | 811 | #endif //AUDIO_POLICY_TEST | 
|  | 812 |  | 
|  | 813 | // open a direct output if required by specified parameters | 
|  | 814 | //force direct flag if offload flag is set: offloading implies a direct output stream | 
|  | 815 | // and all common behaviors are driven by checking only the direct flag | 
|  | 816 | // this should normally be set appropriately in the policy configuration file | 
|  | 817 | if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 818 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 819 | } | 
| Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 820 | if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { | 
|  | 821 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT); | 
|  | 822 | } | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 823 | // only allow deep buffering for music stream type | 
|  | 824 | if (stream != AUDIO_STREAM_MUSIC) { | 
|  | 825 | flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); | 
| Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 826 | } else if (/* stream == AUDIO_STREAM_MUSIC && */ | 
|  | 827 | flags == AUDIO_OUTPUT_FLAG_NONE && | 
|  | 828 | property_get_bool("audio.deep_buffer.media", false /* default_value */)) { | 
|  | 829 | // use DEEP_BUFFER as default output for music stream type | 
|  | 830 | flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER; | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 831 | } | 
| Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 832 | if (stream == AUDIO_STREAM_TTS) { | 
|  | 833 | flags = AUDIO_OUTPUT_FLAG_TTS; | 
|  | 834 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 835 |  | 
| Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 836 | sp<IOProfile> profile; | 
|  | 837 |  | 
|  | 838 | // skip direct output selection if the request can obviously be attached to a mixed output | 
| Eric Laurent | c260784 | 2014-09-29 09:43:03 -0700 | [diff] [blame] | 839 | // and not explicitly requested | 
|  | 840 | if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && | 
| Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 841 | audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX && | 
| Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 842 | audio_channel_count_from_out_mask(channelMask) <= 2) { | 
|  | 843 | goto non_direct_output; | 
|  | 844 | } | 
|  | 845 |  | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 846 | // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled. | 
|  | 847 | // This prevents creating an offloaded track and tearing it down immediately after start | 
|  | 848 | // when audioflinger detects there is an active non offloadable effect. | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 849 | // FIXME: We should check the audio session here but we do not have it in this context. | 
|  | 850 | // This may prevent offloading in rare situations where effects are left active by apps | 
|  | 851 | // in the background. | 
| Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 852 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 853 | if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 854 | !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 855 | profile = getProfileForDirectOutput(device, | 
|  | 856 | samplingRate, | 
|  | 857 | format, | 
|  | 858 | channelMask, | 
|  | 859 | (audio_output_flags_t)flags); | 
|  | 860 | } | 
|  | 861 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 862 | if (profile != 0) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 863 | sp<SwAudioOutputDescriptor> outputDesc = NULL; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 864 |  | 
|  | 865 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 866 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 867 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { | 
|  | 868 | outputDesc = desc; | 
|  | 869 | // reuse direct output if currently open and configured with same parameters | 
|  | 870 | if ((samplingRate == outputDesc->mSamplingRate) && | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 871 | audio_formats_match(format, outputDesc->mFormat) && | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 872 | (channelMask == outputDesc->mChannelMask)) { | 
|  | 873 | outputDesc->mDirectOpenCount++; | 
|  | 874 | ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i)); | 
|  | 875 | return mOutputs.keyAt(i); | 
|  | 876 | } | 
|  | 877 | } | 
|  | 878 | } | 
|  | 879 | // close direct output if currently open and configured with different parameters | 
|  | 880 | if (outputDesc != NULL) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 881 | closeOutput(outputDesc->mIoHandle); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 882 | } | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 883 |  | 
|  | 884 | // if the selected profile is offloaded and no offload info was specified, | 
|  | 885 | // create a default one | 
|  | 886 | audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER; | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 887 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) { | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 888 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); | 
|  | 889 | defaultOffloadInfo.sample_rate = samplingRate; | 
|  | 890 | defaultOffloadInfo.channel_mask = channelMask; | 
|  | 891 | defaultOffloadInfo.format = format; | 
|  | 892 | defaultOffloadInfo.stream_type = stream; | 
|  | 893 | defaultOffloadInfo.bit_rate = 0; | 
|  | 894 | defaultOffloadInfo.duration_us = -1; | 
|  | 895 | defaultOffloadInfo.has_video = true; // conservative | 
|  | 896 | defaultOffloadInfo.is_streaming = true; // likely | 
|  | 897 | offloadInfo = &defaultOffloadInfo; | 
|  | 898 | } | 
|  | 899 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 900 | outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 901 | outputDesc->mDevice = device; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 902 | outputDesc->mLatency = 0; | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 903 | outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 904 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
|  | 905 | config.sample_rate = samplingRate; | 
|  | 906 | config.channel_mask = channelMask; | 
|  | 907 | config.format = format; | 
| Phil Burk | 77cce80 | 2014-08-04 16:18:15 -0700 | [diff] [blame] | 908 | if (offloadInfo != NULL) { | 
|  | 909 | config.offload_info = *offloadInfo; | 
|  | 910 | } | 
| Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 911 | status = mpClientInterface->openOutput(profile->getModuleHandle(), | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 912 | &output, | 
|  | 913 | &config, | 
|  | 914 | &outputDesc->mDevice, | 
|  | 915 | String8(""), | 
|  | 916 | &outputDesc->mLatency, | 
|  | 917 | outputDesc->mFlags); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 918 |  | 
|  | 919 | // only accept an output with the requested parameters | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 920 | if (status != NO_ERROR || | 
|  | 921 | (samplingRate != 0 && samplingRate != config.sample_rate) || | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 922 | (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) || | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 923 | (channelMask != 0 && channelMask != config.channel_mask)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 924 | ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d," | 
|  | 925 | "format %d %d, channelMask %04x %04x", output, samplingRate, | 
|  | 926 | outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask, | 
|  | 927 | outputDesc->mChannelMask); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 928 | if (output != AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 929 | mpClientInterface->closeOutput(output); | 
|  | 930 | } | 
| Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 931 | // fall back to mixer output if possible when the direct output could not be open | 
| Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 932 | if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) { | 
| Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 933 | goto non_direct_output; | 
|  | 934 | } | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 935 | return AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 936 | } | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 937 | outputDesc->mSamplingRate = config.sample_rate; | 
|  | 938 | outputDesc->mChannelMask = config.channel_mask; | 
|  | 939 | outputDesc->mFormat = config.format; | 
|  | 940 | outputDesc->mRefCount[stream] = 0; | 
|  | 941 | outputDesc->mStopTime[stream] = 0; | 
|  | 942 | outputDesc->mDirectOpenCount = 1; | 
|  | 943 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 944 | audio_io_handle_t srcOutput = getOutputForEffect(); | 
|  | 945 | addOutput(output, outputDesc); | 
|  | 946 | audio_io_handle_t dstOutput = getOutputForEffect(); | 
|  | 947 | if (dstOutput == output) { | 
|  | 948 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput); | 
|  | 949 | } | 
|  | 950 | mPreviousOutputs = mOutputs; | 
|  | 951 | ALOGV("getOutput() returns new direct output %d", output); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 952 | mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 953 | return output; | 
|  | 954 | } | 
|  | 955 |  | 
| Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 956 | non_direct_output: | 
| Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 957 |  | 
|  | 958 | // A request for HW A/V sync cannot fallback to a mixed output because time | 
|  | 959 | // stamps are embedded in audio data | 
|  | 960 | if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { | 
|  | 961 | return AUDIO_IO_HANDLE_NONE; | 
|  | 962 | } | 
|  | 963 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 964 | // ignoring channel mask due to downmix capability in mixer | 
|  | 965 |  | 
|  | 966 | // open a non direct output | 
|  | 967 |  | 
|  | 968 | // for non direct outputs, only PCM is supported | 
|  | 969 | if (audio_is_linear_pcm(format)) { | 
|  | 970 | // get which output is suitable for the specified stream. The actual | 
|  | 971 | // routing change will happen when startOutput() will be called | 
|  | 972 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); | 
|  | 973 |  | 
| Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 974 | // at this stage we should ignore the DIRECT flag as no direct output could be found earlier | 
|  | 975 | flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT); | 
|  | 976 | output = selectOutput(outputs, flags, format); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 977 | } | 
|  | 978 | ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d," | 
|  | 979 | "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags); | 
|  | 980 |  | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 981 | ALOGV("  getOutputForDevice() returns output %d", output); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 982 |  | 
|  | 983 | return output; | 
|  | 984 | } | 
|  | 985 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 986 | audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs, | 
| Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 987 | audio_output_flags_t flags, | 
|  | 988 | audio_format_t format) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 989 | { | 
|  | 990 | // select one output among several that provide a path to a particular device or set of | 
|  | 991 | // devices (the list was previously build by getOutputsForDevice()). | 
|  | 992 | // The priority is as follows: | 
|  | 993 | // 1: the output with the highest number of requested policy flags | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 994 | // 2: the output with the bit depth the closest to the requested one | 
|  | 995 | // 3: the primary output | 
|  | 996 | // 4: the first output in the list | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 997 |  | 
|  | 998 | if (outputs.size() == 0) { | 
|  | 999 | return 0; | 
|  | 1000 | } | 
|  | 1001 | if (outputs.size() == 1) { | 
|  | 1002 | return outputs[0]; | 
|  | 1003 | } | 
|  | 1004 |  | 
|  | 1005 | int maxCommonFlags = 0; | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1006 | audio_io_handle_t outputForFlags = 0; | 
|  | 1007 | audio_io_handle_t outputForPrimary = 0; | 
|  | 1008 | audio_io_handle_t outputForFormat = 0; | 
|  | 1009 | audio_format_t bestFormat = AUDIO_FORMAT_INVALID; | 
|  | 1010 | audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1011 |  | 
|  | 1012 | for (size_t i = 0; i < outputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1013 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1014 | if (!outputDesc->isDuplicated()) { | 
| Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1015 | // if a valid format is specified, skip output if not compatible | 
|  | 1016 | if (format != AUDIO_FORMAT_INVALID) { | 
|  | 1017 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1018 | if (!audio_formats_match(format, outputDesc->mFormat)) { | 
| Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1019 | continue; | 
|  | 1020 | } | 
|  | 1021 | } else if (!audio_is_linear_pcm(format)) { | 
|  | 1022 | continue; | 
|  | 1023 | } | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1024 | if (AudioPort::isBetterFormatMatch( | 
|  | 1025 | outputDesc->mFormat, bestFormat, format)) { | 
|  | 1026 | outputForFormat = outputs[i]; | 
|  | 1027 | bestFormat = outputDesc->mFormat; | 
|  | 1028 | } | 
| Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1029 | } | 
|  | 1030 |  | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1031 | int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags); | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1032 | if (commonFlags >= maxCommonFlags) { | 
|  | 1033 | if (commonFlags == maxCommonFlags) { | 
|  | 1034 | if (AudioPort::isBetterFormatMatch( | 
|  | 1035 | outputDesc->mFormat, bestFormatForFlags, format)) { | 
|  | 1036 | outputForFlags = outputs[i]; | 
|  | 1037 | bestFormatForFlags = outputDesc->mFormat; | 
|  | 1038 | } | 
|  | 1039 | } else { | 
|  | 1040 | outputForFlags = outputs[i]; | 
|  | 1041 | maxCommonFlags = commonFlags; | 
|  | 1042 | bestFormatForFlags = outputDesc->mFormat; | 
|  | 1043 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1044 | ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags); | 
|  | 1045 | } | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1046 | if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1047 | outputForPrimary = outputs[i]; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1048 | } | 
|  | 1049 | } | 
|  | 1050 | } | 
|  | 1051 |  | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1052 | if (outputForFlags != 0) { | 
|  | 1053 | return outputForFlags; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1054 | } | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1055 | if (outputForFormat != 0) { | 
|  | 1056 | return outputForFormat; | 
|  | 1057 | } | 
|  | 1058 | if (outputForPrimary != 0) { | 
|  | 1059 | return outputForPrimary; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1060 | } | 
|  | 1061 |  | 
|  | 1062 | return outputs[0]; | 
|  | 1063 | } | 
|  | 1064 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1065 | status_t AudioPolicyManager::startOutput(audio_io_handle_t output, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1066 | audio_stream_type_t stream, | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1067 | audio_session_t session) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1068 | { | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1069 | ALOGV("startOutput() output %d, stream %d, session %d", | 
|  | 1070 | output, stream, session); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1071 | ssize_t index = mOutputs.indexOfKey(output); | 
|  | 1072 | if (index < 0) { | 
|  | 1073 | ALOGW("startOutput() unknown output %d", output); | 
|  | 1074 | return BAD_VALUE; | 
|  | 1075 | } | 
|  | 1076 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1077 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); | 
|  | 1078 |  | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1079 | // Routing? | 
|  | 1080 | mOutputRoutes.incRouteActivity(session); | 
|  | 1081 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1082 | audio_devices_t newDevice; | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1083 | AudioMix *policyMix = NULL; | 
|  | 1084 | const char *address = NULL; | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1085 | if (outputDesc->mPolicyMix != NULL) { | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1086 | policyMix = outputDesc->mPolicyMix; | 
|  | 1087 | address = policyMix->mDeviceAddress.string(); | 
|  | 1088 | if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { | 
|  | 1089 | newDevice = policyMix->mDeviceType; | 
|  | 1090 | } else { | 
|  | 1091 | newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; | 
|  | 1092 | } | 
| Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 1093 | } else if (mOutputRoutes.hasRouteChanged(session)) { | 
|  | 1094 | newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1095 | checkStrategyRoute(getStrategy(stream), output); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1096 | } else { | 
|  | 1097 | newDevice = AUDIO_DEVICE_NONE; | 
|  | 1098 | } | 
|  | 1099 |  | 
|  | 1100 | uint32_t delayMs = 0; | 
|  | 1101 |  | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1102 | status_t status = startSource(outputDesc, stream, newDevice, address, &delayMs); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1103 |  | 
|  | 1104 | if (status != NO_ERROR) { | 
|  | 1105 | mOutputRoutes.decRouteActivity(session); | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1106 | return status; | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1107 | } | 
|  | 1108 | // Automatically enable the remote submix input when output is started on a re routing mix | 
|  | 1109 | // of type MIX_TYPE_RECORDERS | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1110 | if (audio_is_remote_submix_device(newDevice) && policyMix != NULL && | 
|  | 1111 | policyMix->mMixType == MIX_TYPE_RECORDERS) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1112 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, | 
|  | 1113 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1114 | address, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1115 | "remote-submix"); | 
|  | 1116 | } | 
|  | 1117 |  | 
|  | 1118 | if (delayMs != 0) { | 
|  | 1119 | usleep(delayMs * 1000); | 
|  | 1120 | } | 
|  | 1121 |  | 
|  | 1122 | return status; | 
|  | 1123 | } | 
|  | 1124 |  | 
|  | 1125 | status_t AudioPolicyManager::startSource(sp<AudioOutputDescriptor> outputDesc, | 
|  | 1126 | audio_stream_type_t stream, | 
|  | 1127 | audio_devices_t device, | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1128 | const char *address, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1129 | uint32_t *delayMs) | 
|  | 1130 | { | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1131 | // cannot start playback of STREAM_TTS if any other output is being used | 
|  | 1132 | uint32_t beaconMuteLatency = 0; | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1133 |  | 
|  | 1134 | *delayMs = 0; | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1135 | if (stream == AUDIO_STREAM_TTS) { | 
|  | 1136 | ALOGV("\t found BEACON stream"); | 
| Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 1137 | if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) { | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1138 | return INVALID_OPERATION; | 
|  | 1139 | } else { | 
|  | 1140 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); | 
|  | 1141 | } | 
|  | 1142 | } else { | 
|  | 1143 | // some playback other than beacon starts | 
|  | 1144 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); | 
|  | 1145 | } | 
|  | 1146 |  | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1147 | // check active before incrementing usage count | 
|  | 1148 | bool force = !outputDesc->isActive(); | 
|  | 1149 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1150 | // increment usage count for this stream on the requested output: | 
|  | 1151 | // NOTE that the usage count is the same for duplicated output and hardware output which is | 
|  | 1152 | // necessary for a correct control of hardware output routing by startOutput() and stopOutput() | 
|  | 1153 | outputDesc->changeRefCount(stream, 1); | 
|  | 1154 |  | 
| Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 1155 | if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1156 | // starting an output being rerouted? | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1157 | if (device == AUDIO_DEVICE_NONE) { | 
|  | 1158 | device = getNewOutputDevice(outputDesc, false /*fromCache*/); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1159 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1160 | routing_strategy strategy = getStrategy(stream); | 
|  | 1161 | bool shouldWait = (strategy == STRATEGY_SONIFICATION) || | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1162 | (strategy == STRATEGY_SONIFICATION_RESPECTFUL) || | 
|  | 1163 | (beaconMuteLatency > 0); | 
|  | 1164 | uint32_t waitMs = beaconMuteLatency; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1165 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1166 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1167 | if (desc != outputDesc) { | 
|  | 1168 | // force a device change if any other output is managed by the same hw | 
|  | 1169 | // module and has a current device selection that differs from selected device. | 
|  | 1170 | // In this case, the audio HAL must receive the new device selection so that it can | 
|  | 1171 | // change the device currently selected by the other active output. | 
|  | 1172 | if (outputDesc->sharesHwModuleWith(desc) && | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1173 | desc->device() != device) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1174 | force = true; | 
|  | 1175 | } | 
|  | 1176 | // 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] | 1177 | // a notification so that audio focus effect can propagate, or that a mute/unmute | 
|  | 1178 | // event occurred for beacon | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1179 | uint32_t latency = desc->latency(); | 
|  | 1180 | if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) { | 
|  | 1181 | waitMs = latency; | 
|  | 1182 | } | 
|  | 1183 | } | 
|  | 1184 | } | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1185 | uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force, 0, NULL, address); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1186 |  | 
|  | 1187 | // handle special case for sonification while in call | 
|  | 1188 | if (isInCall()) { | 
|  | 1189 | handleIncallSonification(stream, true, false); | 
|  | 1190 | } | 
|  | 1191 |  | 
|  | 1192 | // apply volume rules for current stream and device if necessary | 
|  | 1193 | checkAndSetVolume(stream, | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 1194 | mVolumeCurves->getVolumeIndex(stream, device), | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1195 | outputDesc, | 
|  | 1196 | device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1197 |  | 
|  | 1198 | // update the outputs if starting an output with a stream that can affect notification | 
|  | 1199 | // routing | 
|  | 1200 | handleNotificationRoutingForStream(stream); | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1201 |  | 
| Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 1202 | // force reevaluating accessibility routing when ringtone or alarm starts | 
|  | 1203 | if (strategy == STRATEGY_SONIFICATION) { | 
|  | 1204 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); | 
|  | 1205 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1206 | } | 
|  | 1207 | return NO_ERROR; | 
|  | 1208 | } | 
|  | 1209 |  | 
|  | 1210 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1211 | status_t AudioPolicyManager::stopOutput(audio_io_handle_t output, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1212 | audio_stream_type_t stream, | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1213 | audio_session_t session) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1214 | { | 
|  | 1215 | ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session); | 
|  | 1216 | ssize_t index = mOutputs.indexOfKey(output); | 
|  | 1217 | if (index < 0) { | 
|  | 1218 | ALOGW("stopOutput() unknown output %d", output); | 
|  | 1219 | return BAD_VALUE; | 
|  | 1220 | } | 
|  | 1221 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1222 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1223 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1224 | if (outputDesc->mRefCount[stream] == 1) { | 
|  | 1225 | // Automatically disable the remote submix input when output is stopped on a | 
|  | 1226 | // re routing mix of type MIX_TYPE_RECORDERS | 
|  | 1227 | if (audio_is_remote_submix_device(outputDesc->mDevice) && | 
|  | 1228 | outputDesc->mPolicyMix != NULL && | 
|  | 1229 | outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) { | 
|  | 1230 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, | 
|  | 1231 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1232 | outputDesc->mPolicyMix->mDeviceAddress, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1233 | "remote-submix"); | 
|  | 1234 | } | 
|  | 1235 | } | 
|  | 1236 |  | 
|  | 1237 | // Routing? | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1238 | bool forceDeviceUpdate = false; | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1239 | if (outputDesc->mRefCount[stream] > 0) { | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1240 | int activityCount = mOutputRoutes.decRouteActivity(session); | 
|  | 1241 | forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0)); | 
|  | 1242 |  | 
|  | 1243 | if (forceDeviceUpdate) { | 
|  | 1244 | checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE); | 
|  | 1245 | } | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1246 | } | 
|  | 1247 |  | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1248 | return stopSource(outputDesc, stream, forceDeviceUpdate); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1249 | } | 
|  | 1250 |  | 
|  | 1251 | status_t AudioPolicyManager::stopSource(sp<AudioOutputDescriptor> outputDesc, | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1252 | audio_stream_type_t stream, | 
|  | 1253 | bool forceDeviceUpdate) | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1254 | { | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1255 | // always handle stream stop, check which stream type is stopping | 
|  | 1256 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); | 
|  | 1257 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1258 | // handle special case for sonification while in call | 
|  | 1259 | if (isInCall()) { | 
|  | 1260 | handleIncallSonification(stream, false, false); | 
|  | 1261 | } | 
|  | 1262 |  | 
|  | 1263 | if (outputDesc->mRefCount[stream] > 0) { | 
|  | 1264 | // decrement usage count of this stream on the output | 
|  | 1265 | outputDesc->changeRefCount(stream, -1); | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1266 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1267 | // store time at which the stream was stopped - see isStreamActive() | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1268 | if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1269 | outputDesc->mStopTime[stream] = systemTime(); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1270 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1271 | // delay the device switch by twice the latency because stopOutput() is executed when | 
|  | 1272 | // the track stop() command is received and at that time the audio track buffer can | 
|  | 1273 | // still contain data that needs to be drained. The latency only covers the audio HAL | 
|  | 1274 | // and kernel buffers. Also the latency does not always include additional delay in the | 
|  | 1275 | // audio path (audio DSP, CODEC ...) | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1276 | setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1277 |  | 
|  | 1278 | // force restoring the device selection on other active outputs if it differs from the | 
|  | 1279 | // one being selected for this output | 
|  | 1280 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 1281 | audio_io_handle_t curOutput = mOutputs.keyAt(i); | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1282 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1283 | if (desc != outputDesc && | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1284 | desc->isActive() && | 
|  | 1285 | outputDesc->sharesHwModuleWith(desc) && | 
|  | 1286 | (newDevice != desc->device())) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1287 | setOutputDevice(desc, | 
|  | 1288 | getNewOutputDevice(desc, false /*fromCache*/), | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1289 | true, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1290 | outputDesc->latency()*2); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1291 | } | 
|  | 1292 | } | 
|  | 1293 | // update the outputs if stopping one with a stream that can affect notification routing | 
|  | 1294 | handleNotificationRoutingForStream(stream); | 
|  | 1295 | } | 
|  | 1296 | return NO_ERROR; | 
|  | 1297 | } else { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1298 | ALOGW("stopOutput() refcount is already 0"); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1299 | return INVALID_OPERATION; | 
|  | 1300 | } | 
|  | 1301 | } | 
|  | 1302 |  | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1303 | void AudioPolicyManager::releaseOutput(audio_io_handle_t output, | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1304 | audio_stream_type_t stream __unused, | 
|  | 1305 | audio_session_t session __unused) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1306 | { | 
|  | 1307 | ALOGV("releaseOutput() %d", output); | 
|  | 1308 | ssize_t index = mOutputs.indexOfKey(output); | 
|  | 1309 | if (index < 0) { | 
|  | 1310 | ALOGW("releaseOutput() releasing unknown output %d", output); | 
|  | 1311 | return; | 
|  | 1312 | } | 
|  | 1313 |  | 
|  | 1314 | #ifdef AUDIO_POLICY_TEST | 
|  | 1315 | int testIndex = testOutputIndex(output); | 
|  | 1316 | if (testIndex != 0) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1317 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1318 | if (outputDesc->isActive()) { | 
|  | 1319 | mpClientInterface->closeOutput(output); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1320 | removeOutput(output); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1321 | mTestOutputs[testIndex] = 0; | 
|  | 1322 | } | 
|  | 1323 | return; | 
|  | 1324 | } | 
|  | 1325 | #endif //AUDIO_POLICY_TEST | 
|  | 1326 |  | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1327 | // Routing | 
|  | 1328 | mOutputRoutes.removeRoute(session); | 
|  | 1329 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1330 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1331 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1332 | if (desc->mDirectOpenCount <= 0) { | 
|  | 1333 | ALOGW("releaseOutput() invalid open count %d for output %d", | 
|  | 1334 | desc->mDirectOpenCount, output); | 
|  | 1335 | return; | 
|  | 1336 | } | 
|  | 1337 | if (--desc->mDirectOpenCount == 0) { | 
|  | 1338 | closeOutput(output); | 
|  | 1339 | // If effects where present on the output, audioflinger moved them to the primary | 
|  | 1340 | // output by default: move them back to the appropriate output. | 
|  | 1341 | audio_io_handle_t dstOutput = getOutputForEffect(); | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 1342 | if (hasPrimaryOutput() && dstOutput != mPrimaryOutput->mIoHandle) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1343 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, | 
|  | 1344 | mPrimaryOutput->mIoHandle, dstOutput); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1345 | } | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1346 | mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1347 | } | 
|  | 1348 | } | 
|  | 1349 | } | 
|  | 1350 |  | 
|  | 1351 |  | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1352 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, | 
|  | 1353 | audio_io_handle_t *input, | 
|  | 1354 | audio_session_t session, | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1355 | uid_t uid, | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1356 | uint32_t samplingRate, | 
|  | 1357 | audio_format_t format, | 
|  | 1358 | audio_channel_mask_t channelMask, | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1359 | audio_input_flags_t flags, | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1360 | audio_port_handle_t selectedDeviceId, | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1361 | input_type_t *inputType) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1362 | { | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1363 | ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x," | 
|  | 1364 | "session %d, flags %#x", | 
|  | 1365 | attr->source, samplingRate, format, channelMask, session, flags); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1366 |  | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1367 | *input = AUDIO_IO_HANDLE_NONE; | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1368 | *inputType = API_INPUT_INVALID; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1369 | audio_devices_t device; | 
|  | 1370 | // handle legacy remote submix case where the address was not always specified | 
|  | 1371 | String8 address = String8(""); | 
| Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1372 | audio_source_t inputSource = attr->source; | 
|  | 1373 | audio_source_t halInputSource; | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1374 | AudioMix *policyMix = NULL; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1375 |  | 
| Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1376 | if (inputSource == AUDIO_SOURCE_DEFAULT) { | 
|  | 1377 | inputSource = AUDIO_SOURCE_MIC; | 
|  | 1378 | } | 
|  | 1379 | halInputSource = inputSource; | 
|  | 1380 |  | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1381 | // Explicit routing? | 
|  | 1382 | sp<DeviceDescriptor> deviceDesc; | 
|  | 1383 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { | 
|  | 1384 | if (mAvailableInputDevices[i]->getId() == selectedDeviceId) { | 
|  | 1385 | deviceDesc = mAvailableInputDevices[i]; | 
|  | 1386 | break; | 
|  | 1387 | } | 
|  | 1388 | } | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1389 | mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid); | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1390 |  | 
| Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1391 | if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX && | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1392 | strncmp(attr->tags, "addr=", strlen("addr=")) == 0) { | 
| Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 1393 | status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix); | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 1394 | if (ret != NO_ERROR) { | 
|  | 1395 | return ret; | 
|  | 1396 | } | 
|  | 1397 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1398 | device = AUDIO_DEVICE_IN_REMOTE_SUBMIX; | 
|  | 1399 | address = String8(attr->tags + strlen("addr=")); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1400 | } else { | 
| Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1401 | device = getDeviceAndMixForInputSource(inputSource, &policyMix); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1402 | if (device == AUDIO_DEVICE_NONE) { | 
| Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1403 | ALOGW("getInputForAttr() could not find device for source %d", inputSource); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1404 | return BAD_VALUE; | 
|  | 1405 | } | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1406 | if (policyMix != NULL) { | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1407 | address = policyMix->mDeviceAddress; | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1408 | if (policyMix->mMixType == MIX_TYPE_RECORDERS) { | 
|  | 1409 | // there is an external policy, but this input is attached to a mix of recorders, | 
|  | 1410 | // meaning it receives audio injected into the framework, so the recorder doesn't | 
|  | 1411 | // know about it and is therefore considered "legacy" | 
|  | 1412 | *inputType = API_INPUT_LEGACY; | 
|  | 1413 | } else { | 
|  | 1414 | // recording a mix of players defined by an external policy, we're rerouting for | 
|  | 1415 | // an external policy | 
|  | 1416 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; | 
|  | 1417 | } | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1418 | } else if (audio_is_remote_submix_device(device)) { | 
|  | 1419 | address = String8("0"); | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1420 | *inputType = API_INPUT_MIX_CAPTURE; | 
| Eric Laurent | 82db269 | 2015-08-07 13:59:42 -0700 | [diff] [blame] | 1421 | } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) { | 
|  | 1422 | *inputType = API_INPUT_TELEPHONY_RX; | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1423 | } else { | 
|  | 1424 | *inputType = API_INPUT_LEGACY; | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1425 | } | 
| Ravi Kumar Alamanda | b367f5b | 2015-08-25 08:21:37 -0700 | [diff] [blame] | 1426 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1427 | } | 
|  | 1428 |  | 
|  | 1429 | *input = getInputForDevice(device, address, session, uid, inputSource, | 
|  | 1430 | samplingRate, format, channelMask, flags, | 
|  | 1431 | policyMix); | 
|  | 1432 | if (*input == AUDIO_IO_HANDLE_NONE) { | 
|  | 1433 | mInputRoutes.removeRoute(session); | 
|  | 1434 | return INVALID_OPERATION; | 
|  | 1435 | } | 
|  | 1436 | ALOGV("getInputForAttr() returns input type = %d", *inputType); | 
|  | 1437 | return NO_ERROR; | 
|  | 1438 | } | 
|  | 1439 |  | 
|  | 1440 |  | 
|  | 1441 | audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device, | 
|  | 1442 | String8 address, | 
|  | 1443 | audio_session_t session, | 
|  | 1444 | uid_t uid, | 
|  | 1445 | audio_source_t inputSource, | 
|  | 1446 | uint32_t samplingRate, | 
|  | 1447 | audio_format_t format, | 
|  | 1448 | audio_channel_mask_t channelMask, | 
|  | 1449 | audio_input_flags_t flags, | 
|  | 1450 | AudioMix *policyMix) | 
|  | 1451 | { | 
|  | 1452 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; | 
|  | 1453 | audio_source_t halInputSource = inputSource; | 
|  | 1454 | bool isSoundTrigger = false; | 
|  | 1455 |  | 
|  | 1456 | if (inputSource == AUDIO_SOURCE_HOTWORD) { | 
|  | 1457 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); | 
|  | 1458 | if (index >= 0) { | 
|  | 1459 | input = mSoundTriggerSessions.valueFor(session); | 
|  | 1460 | isSoundTrigger = true; | 
|  | 1461 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); | 
|  | 1462 | ALOGV("SoundTrigger capture on session %d input %d", session, input); | 
|  | 1463 | } else { | 
|  | 1464 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; | 
| Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1465 | } | 
|  | 1466 | } | 
|  | 1467 |  | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1468 | // find a compatible input profile (not necessarily identical in parameters) | 
|  | 1469 | sp<IOProfile> profile; | 
|  | 1470 | // samplingRate and flags may be updated by getInputProfile | 
| Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1471 | uint32_t profileSamplingRate = (samplingRate == 0) ? SAMPLE_RATE_HZ_DEFAULT : samplingRate; | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1472 | audio_format_t profileFormat = format; | 
|  | 1473 | audio_channel_mask_t profileChannelMask = channelMask; | 
|  | 1474 | audio_input_flags_t profileFlags = flags; | 
|  | 1475 | for (;;) { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1476 | profile = getInputProfile(device, address, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1477 | profileSamplingRate, profileFormat, profileChannelMask, | 
|  | 1478 | profileFlags); | 
|  | 1479 | if (profile != 0) { | 
|  | 1480 | break; // success | 
|  | 1481 | } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) { | 
|  | 1482 | profileFlags = AUDIO_INPUT_FLAG_NONE; // retry | 
|  | 1483 | } else { // fail | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1484 | ALOGW("getInputForDevice() could not find profile for device 0x%X," | 
|  | 1485 | "samplingRate %u, format %#x, channelMask 0x%X, flags %#x", | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1486 | device, samplingRate, format, channelMask, flags); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1487 | return input; | 
| Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1488 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1489 | } | 
| Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1490 | // Pick input sampling rate if not specified by client | 
|  | 1491 | if (samplingRate == 0) { | 
|  | 1492 | samplingRate = profileSamplingRate; | 
|  | 1493 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1494 |  | 
| Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1495 | if (profile->getModuleHandle() == 0) { | 
|  | 1496 | ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName()); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1497 | return input; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1498 | } | 
|  | 1499 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1500 | sp<AudioSession> audioSession = new AudioSession(session, | 
|  | 1501 | inputSource, | 
|  | 1502 | format, | 
|  | 1503 | samplingRate, | 
|  | 1504 | channelMask, | 
|  | 1505 | flags, | 
|  | 1506 | uid, | 
| Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 1507 | isSoundTrigger, | 
|  | 1508 | policyMix, mpClientInterface); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1509 |  | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1510 | // TODO enable input reuse | 
|  | 1511 | #if 0 | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1512 | // reuse an open input if possible | 
|  | 1513 | for (size_t i = 0; i < mInputs.size(); i++) { | 
|  | 1514 | sp<AudioInputDescriptor> desc = mInputs.valueAt(i); | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1515 | // reuse input if it shares the same profile and same sound trigger attribute | 
|  | 1516 | if (profile == desc->mProfile && | 
|  | 1517 | isSoundTrigger == desc->isSoundTrigger()) { | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1518 |  | 
|  | 1519 | sp<AudioSession> as = desc->getAudioSession(session); | 
|  | 1520 | if (as != 0) { | 
|  | 1521 | // do not allow unmatching properties on same session | 
|  | 1522 | if (as->matches(audioSession)) { | 
|  | 1523 | as->changeOpenCount(1); | 
|  | 1524 | } else { | 
|  | 1525 | ALOGW("getInputForDevice() record with different attributes" | 
|  | 1526 | " exists for session %d", session); | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1527 | return input; | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1528 | } | 
|  | 1529 | } else { | 
|  | 1530 | desc->addAudioSession(session, audioSession); | 
|  | 1531 | } | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1532 | ALOGV("getInputForDevice() reusing input %d", mInputs.keyAt(i)); | 
|  | 1533 | return mInputs.keyAt(i); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1534 | } | 
|  | 1535 | } | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1536 | #endif | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1537 |  | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1538 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1539 | config.sample_rate = profileSamplingRate; | 
|  | 1540 | config.channel_mask = profileChannelMask; | 
|  | 1541 | config.format = profileFormat; | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1542 |  | 
| Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1543 | status_t status = mpClientInterface->openInput(profile->getModuleHandle(), | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1544 | &input, | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1545 | &config, | 
|  | 1546 | &device, | 
| Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 1547 | address, | 
| Eric Laurent | 1c9c2cc | 2014-08-28 19:37:25 -0700 | [diff] [blame] | 1548 | halInputSource, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1549 | profileFlags); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1550 |  | 
|  | 1551 | // only accept input with the exact requested set of parameters | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1552 | if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE || | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1553 | (profileSamplingRate != config.sample_rate) || | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1554 | !audio_formats_match(profileFormat, config.format) || | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1555 | (profileChannelMask != config.channel_mask)) { | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1556 | ALOGW("getInputForAttr() failed opening input: samplingRate %d" | 
|  | 1557 | ", format %d, channelMask %x", | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1558 | samplingRate, format, channelMask); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1559 | if (input != AUDIO_IO_HANDLE_NONE) { | 
|  | 1560 | mpClientInterface->closeInput(input); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1561 | } | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1562 | return AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1563 | } | 
|  | 1564 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1565 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile); | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1566 | inputDesc->mSamplingRate = profileSamplingRate; | 
|  | 1567 | inputDesc->mFormat = profileFormat; | 
|  | 1568 | inputDesc->mChannelMask = profileChannelMask; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1569 | inputDesc->mDevice = device; | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1570 | inputDesc->mPolicyMix = policyMix; | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1571 | inputDesc->addAudioSession(session, audioSession); | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1572 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1573 | addInput(input, inputDesc); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1574 | mpClientInterface->onAudioPortListUpdate(); | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1575 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1576 | return input; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1577 | } | 
|  | 1578 |  | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1579 | status_t AudioPolicyManager::startInput(audio_io_handle_t input, | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1580 | audio_session_t session) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1581 | { | 
|  | 1582 | ALOGV("startInput() input %d", input); | 
|  | 1583 | ssize_t index = mInputs.indexOfKey(input); | 
|  | 1584 | if (index < 0) { | 
|  | 1585 | ALOGW("startInput() unknown input %d", input); | 
|  | 1586 | return BAD_VALUE; | 
|  | 1587 | } | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1588 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1589 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1590 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); | 
|  | 1591 | if (audioSession == 0) { | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1592 | ALOGW("startInput() unknown session %d on input %d", session, input); | 
|  | 1593 | return BAD_VALUE; | 
|  | 1594 | } | 
|  | 1595 |  | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1596 | // virtual input devices are compatible with other input devices | 
|  | 1597 | if (!is_virtual_input_device(inputDesc->mDevice)) { | 
| Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1598 |  | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1599 | // for a non-virtual input device, check if there is another (non-virtual) active input | 
|  | 1600 | audio_io_handle_t activeInput = mInputs.getActiveInput(); | 
|  | 1601 | if (activeInput != 0 && activeInput != input) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1602 |  | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1603 | // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed, | 
|  | 1604 | // otherwise the active input continues and the new input cannot be started. | 
|  | 1605 | sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput); | 
|  | 1606 | if ((activeDesc->inputSource() == AUDIO_SOURCE_HOTWORD) && | 
|  | 1607 | !activeDesc->hasPreemptedSession(session)) { | 
|  | 1608 | ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput); | 
|  | 1609 | //FIXME: consider all active sessions | 
|  | 1610 | AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions(); | 
|  | 1611 | audio_session_t activeSession = activeSessions.keyAt(0); | 
|  | 1612 | SortedVector<audio_session_t> sessions = | 
|  | 1613 | activeDesc->getPreemptedSessions(); | 
|  | 1614 | sessions.add(activeSession); | 
|  | 1615 | inputDesc->setPreemptedSessions(sessions); | 
|  | 1616 | stopInput(activeInput, activeSession); | 
|  | 1617 | releaseInput(activeInput, activeSession); | 
|  | 1618 | } else { | 
|  | 1619 | ALOGE("startInput(%d) failed: other input %d already started", input, activeInput); | 
|  | 1620 | return INVALID_OPERATION; | 
|  | 1621 | } | 
|  | 1622 | } | 
|  | 1623 |  | 
|  | 1624 | // Do not allow capture if an active voice call is using a software patch and | 
|  | 1625 | // the call TX source device is on the same HW module. | 
|  | 1626 | // FIXME: would be better to refine to only inputs whose profile connects to the | 
|  | 1627 | // call TX device but this information is not in the audio patch | 
|  | 1628 | if (mCallTxPatch != 0 && | 
|  | 1629 | inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) { | 
|  | 1630 | return INVALID_OPERATION; | 
|  | 1631 | } | 
|  | 1632 | } | 
| Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 1633 |  | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1634 | // Routing? | 
|  | 1635 | mInputRoutes.incRouteActivity(session); | 
|  | 1636 |  | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1637 | if (!inputDesc->isActive() || mInputRoutes.hasRouteChanged(session)) { | 
|  | 1638 | // if input maps to a dynamic policy with an activity listener, notify of state change | 
|  | 1639 | if ((inputDesc->mPolicyMix != NULL) | 
|  | 1640 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1641 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1642 | MIX_STATE_MIXING); | 
|  | 1643 | } | 
| Jean-Michel Trivi | 2b0c1fc | 2015-04-15 17:29:28 -0700 | [diff] [blame] | 1644 |  | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1645 | if (mInputs.activeInputsCount() == 0) { | 
|  | 1646 | SoundTrigger::setCaptureState(true); | 
|  | 1647 | } | 
|  | 1648 | setInputDevice(input, getNewInputDevice(input), true /* force */); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1649 |  | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1650 | // automatically enable the remote submix output when input is started if not | 
|  | 1651 | // used by a policy mix of type MIX_TYPE_RECORDERS | 
|  | 1652 | // For remote submix (a virtual device), we open only one input per capture request. | 
|  | 1653 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { | 
|  | 1654 | String8 address = String8(""); | 
|  | 1655 | if (inputDesc->mPolicyMix == NULL) { | 
|  | 1656 | address = String8("0"); | 
|  | 1657 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1658 | address = inputDesc->mPolicyMix->mDeviceAddress; | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1659 | } | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1660 | if (address != "") { | 
|  | 1661 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, | 
|  | 1662 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, | 
|  | 1663 | address, "remote-submix"); | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1664 | } | 
| Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1665 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1666 | } | 
|  | 1667 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1668 | ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1669 |  | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1670 | audioSession->changeActiveCount(1); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1671 | return NO_ERROR; | 
|  | 1672 | } | 
|  | 1673 |  | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1674 | status_t AudioPolicyManager::stopInput(audio_io_handle_t input, | 
|  | 1675 | audio_session_t session) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1676 | { | 
|  | 1677 | ALOGV("stopInput() input %d", input); | 
|  | 1678 | ssize_t index = mInputs.indexOfKey(input); | 
|  | 1679 | if (index < 0) { | 
|  | 1680 | ALOGW("stopInput() unknown input %d", input); | 
|  | 1681 | return BAD_VALUE; | 
|  | 1682 | } | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1683 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1684 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1685 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1686 | if (index < 0) { | 
|  | 1687 | ALOGW("stopInput() unknown session %d on input %d", session, input); | 
|  | 1688 | return BAD_VALUE; | 
|  | 1689 | } | 
|  | 1690 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1691 | if (audioSession->activeCount() == 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1692 | ALOGW("stopInput() input %d already stopped", input); | 
|  | 1693 | return INVALID_OPERATION; | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1694 | } | 
|  | 1695 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1696 | audioSession->changeActiveCount(-1); | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1697 |  | 
|  | 1698 | // Routing? | 
|  | 1699 | mInputRoutes.decRouteActivity(session); | 
|  | 1700 |  | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1701 | if (!inputDesc->isActive()) { | 
|  | 1702 | // if input maps to a dynamic policy with an activity listener, notify of state change | 
|  | 1703 | if ((inputDesc->mPolicyMix != NULL) | 
|  | 1704 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1705 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1706 | MIX_STATE_IDLE); | 
| Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 1707 | } | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1708 |  | 
|  | 1709 | // automatically disable the remote submix output when input is stopped if not | 
|  | 1710 | // used by a policy mix of type MIX_TYPE_RECORDERS | 
|  | 1711 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { | 
|  | 1712 | String8 address = String8(""); | 
|  | 1713 | if (inputDesc->mPolicyMix == NULL) { | 
|  | 1714 | address = String8("0"); | 
|  | 1715 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1716 | address = inputDesc->mPolicyMix->mDeviceAddress; | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1717 | } | 
|  | 1718 | if (address != "") { | 
|  | 1719 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, | 
|  | 1720 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, | 
|  | 1721 | address, "remote-submix"); | 
|  | 1722 | } | 
|  | 1723 | } | 
|  | 1724 |  | 
|  | 1725 | resetInputDevice(input); | 
|  | 1726 |  | 
|  | 1727 | if (mInputs.activeInputsCount() == 0) { | 
|  | 1728 | SoundTrigger::setCaptureState(false); | 
|  | 1729 | } | 
|  | 1730 | inputDesc->clearPreemptedSessions(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1731 | } | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1732 | return NO_ERROR; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1733 | } | 
|  | 1734 |  | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1735 | void AudioPolicyManager::releaseInput(audio_io_handle_t input, | 
|  | 1736 | audio_session_t session) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1737 | { | 
| Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 1738 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1739 | ALOGV("releaseInput() %d", input); | 
|  | 1740 | ssize_t index = mInputs.indexOfKey(input); | 
|  | 1741 | if (index < 0) { | 
|  | 1742 | ALOGW("releaseInput() releasing unknown input %d", input); | 
|  | 1743 | return; | 
|  | 1744 | } | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1745 |  | 
|  | 1746 | // Routing | 
|  | 1747 | mInputRoutes.removeRoute(session); | 
|  | 1748 |  | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1749 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); | 
|  | 1750 | ALOG_ASSERT(inputDesc != 0); | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1751 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1752 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1753 | if (index < 0) { | 
|  | 1754 | ALOGW("releaseInput() unknown session %d on input %d", session, input); | 
|  | 1755 | return; | 
|  | 1756 | } | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1757 |  | 
|  | 1758 | if (audioSession->openCount() == 0) { | 
|  | 1759 | ALOGW("releaseInput() invalid open count %d on session %d", | 
|  | 1760 | audioSession->openCount(), session); | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1761 | return; | 
|  | 1762 | } | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1763 |  | 
|  | 1764 | if (audioSession->changeOpenCount(-1) == 0) { | 
|  | 1765 | inputDesc->removeAudioSession(session); | 
|  | 1766 | } | 
|  | 1767 |  | 
| Jean-Michel Trivi | 65bfe91 | 2015-12-04 15:56:47 -0800 | [diff] [blame] | 1768 | if (inputDesc->getOpenRefCount() > 0) { | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1769 | ALOGV("releaseInput() exit > 0"); | 
|  | 1770 | return; | 
|  | 1771 | } | 
|  | 1772 |  | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1773 | closeInput(input); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1774 | mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1775 | ALOGV("releaseInput() exit"); | 
|  | 1776 | } | 
|  | 1777 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1778 | void AudioPolicyManager::closeAllInputs() { | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1779 | bool patchRemoved = false; | 
|  | 1780 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1781 | for(size_t input_index = 0; input_index < mInputs.size(); input_index++) { | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1782 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index); | 
| Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 1783 | ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1784 | if (patch_index >= 0) { | 
|  | 1785 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index); | 
|  | 1786 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); | 
|  | 1787 | mAudioPatches.removeItemsAt(patch_index); | 
|  | 1788 | patchRemoved = true; | 
|  | 1789 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1790 | mpClientInterface->closeInput(mInputs.keyAt(input_index)); | 
|  | 1791 | } | 
|  | 1792 | mInputs.clear(); | 
| Chris Thornton | 52785f3 | 2016-02-09 17:28:49 -0800 | [diff] [blame] | 1793 | SoundTrigger::setCaptureState(false); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1794 | nextAudioPortGeneration(); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1795 |  | 
|  | 1796 | if (patchRemoved) { | 
|  | 1797 | mpClientInterface->onAudioPatchListUpdate(); | 
|  | 1798 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1799 | } | 
|  | 1800 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1801 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1802 | int indexMin, | 
|  | 1803 | int indexMax) | 
|  | 1804 | { | 
|  | 1805 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 1806 | mVolumeCurves->initStreamVolume(stream, indexMin, indexMax); | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1807 |  | 
|  | 1808 | // initialize other private stream volumes which follow this one | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 1809 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { | 
|  | 1810 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1811 | continue; | 
|  | 1812 | } | 
|  | 1813 | mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax); | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1814 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1815 | } | 
|  | 1816 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1817 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1818 | int index, | 
|  | 1819 | audio_devices_t device) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1820 | { | 
|  | 1821 |  | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 1822 | if ((index < mVolumeCurves->getVolumeIndexMin(stream)) || | 
|  | 1823 | (index > mVolumeCurves->getVolumeIndexMax(stream))) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1824 | return BAD_VALUE; | 
|  | 1825 | } | 
|  | 1826 | if (!audio_is_output_device(device)) { | 
|  | 1827 | return BAD_VALUE; | 
|  | 1828 | } | 
|  | 1829 |  | 
|  | 1830 | // Force max volume if stream cannot be muted | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 1831 | if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1832 |  | 
| Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 1833 | ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d", | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1834 | stream, device, index); | 
|  | 1835 |  | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1836 | // update other private stream volumes which follow this one | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 1837 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { | 
|  | 1838 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 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 | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 1844 | // update volume on all outputs and streams matching the following: | 
|  | 1845 | // - The requested stream (or a stream matching for volume control) is active on the output | 
|  | 1846 | // - The device (or devices) selected by the strategy corresponding to this stream includes | 
|  | 1847 | // the requested device | 
|  | 1848 | // - For non default requested device, currently selected device on the output is either the | 
|  | 1849 | // requested device or one of the devices selected by the strategy | 
| Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 1850 | // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if | 
|  | 1851 | // no specific device volume value exists for currently selected device. | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1852 | status_t status = NO_ERROR; | 
|  | 1853 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1854 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
|  | 1855 | audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device()); | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 1856 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { | 
|  | 1857 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1858 | continue; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1859 | } | 
| Eric Laurent | d3926fe | 2016-04-15 18:10:07 -0700 | [diff] [blame] | 1860 | if (!(desc->isStreamActive((audio_stream_type_t)curStream) || | 
|  | 1861 | (isInCall() && (curStream == AUDIO_STREAM_VOICE_CALL)))) { | 
| Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 1862 | continue; | 
|  | 1863 | } | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 1864 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1865 | audio_devices_t curStreamDevice = getDeviceForStrategy(curStrategy, true /*fromCache*/); | 
| Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 1866 | if ((curStreamDevice & device) == 0) { | 
|  | 1867 | continue; | 
|  | 1868 | } | 
|  | 1869 | bool applyDefault = false; | 
| Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 1870 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { | 
| Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 1871 | curStreamDevice |= device; | 
|  | 1872 | } else if (!mVolumeCurves->hasVolumeIndexForDevice( | 
|  | 1873 | stream, Volume::getDeviceForVolume(curStreamDevice))) { | 
|  | 1874 | applyDefault = true; | 
|  | 1875 | } | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1876 |  | 
| Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 1877 | if (applyDefault || ((curDevice & curStreamDevice) != 0)) { | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1878 | status_t volStatus = | 
|  | 1879 | checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice); | 
|  | 1880 | if (volStatus != NO_ERROR) { | 
|  | 1881 | status = volStatus; | 
|  | 1882 | } | 
|  | 1883 | } | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1884 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1885 | } | 
|  | 1886 | return status; | 
|  | 1887 | } | 
|  | 1888 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1889 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1890 | int *index, | 
|  | 1891 | audio_devices_t device) | 
|  | 1892 | { | 
|  | 1893 | if (index == NULL) { | 
|  | 1894 | return BAD_VALUE; | 
|  | 1895 | } | 
|  | 1896 | if (!audio_is_output_device(device)) { | 
|  | 1897 | return BAD_VALUE; | 
|  | 1898 | } | 
| Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 1899 | // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device corresponding to | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1900 | // the strategy the stream belongs to. | 
| Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 1901 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1902 | device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); | 
|  | 1903 | } | 
| François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 1904 | device = Volume::getDeviceForVolume(device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1905 |  | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 1906 | *index =  mVolumeCurves->getVolumeIndex(stream, device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1907 | ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index); | 
|  | 1908 | return NO_ERROR; | 
|  | 1909 | } | 
|  | 1910 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1911 | audio_io_handle_t AudioPolicyManager::selectOutputForEffects( | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1912 | const SortedVector<audio_io_handle_t>& outputs) | 
|  | 1913 | { | 
|  | 1914 | // select one output among several suitable for global effects. | 
|  | 1915 | // The priority is as follows: | 
|  | 1916 | // 1: An offloaded output. If the effect ends up not being offloadable, | 
|  | 1917 | //    AudioFlinger will invalidate the track and the offloaded output | 
|  | 1918 | //    will be closed causing the effect to be moved to a PCM output. | 
|  | 1919 | // 2: A deep buffer output | 
|  | 1920 | // 3: the first output in the list | 
|  | 1921 |  | 
|  | 1922 | if (outputs.size() == 0) { | 
|  | 1923 | return 0; | 
|  | 1924 | } | 
|  | 1925 |  | 
|  | 1926 | audio_io_handle_t outputOffloaded = 0; | 
|  | 1927 | audio_io_handle_t outputDeepBuffer = 0; | 
|  | 1928 |  | 
|  | 1929 | for (size_t i = 0; i < outputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1930 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1931 | ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1932 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { | 
|  | 1933 | outputOffloaded = outputs[i]; | 
|  | 1934 | } | 
|  | 1935 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { | 
|  | 1936 | outputDeepBuffer = outputs[i]; | 
|  | 1937 | } | 
|  | 1938 | } | 
|  | 1939 |  | 
|  | 1940 | ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d", | 
|  | 1941 | outputOffloaded, outputDeepBuffer); | 
|  | 1942 | if (outputOffloaded != 0) { | 
|  | 1943 | return outputOffloaded; | 
|  | 1944 | } | 
|  | 1945 | if (outputDeepBuffer != 0) { | 
|  | 1946 | return outputDeepBuffer; | 
|  | 1947 | } | 
|  | 1948 |  | 
|  | 1949 | return outputs[0]; | 
|  | 1950 | } | 
|  | 1951 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1952 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1953 | { | 
|  | 1954 | // apply simple rule where global effects are attached to the same output as MUSIC streams | 
|  | 1955 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1956 | routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1957 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); | 
|  | 1958 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs); | 
|  | 1959 |  | 
|  | 1960 | audio_io_handle_t output = selectOutputForEffects(dstOutputs); | 
|  | 1961 | ALOGV("getOutputForEffect() got output %d for fx %s flags %x", | 
|  | 1962 | output, (desc == NULL) ? "unspecified" : desc->name,  (desc == NULL) ? 0 : desc->flags); | 
|  | 1963 |  | 
|  | 1964 | return output; | 
|  | 1965 | } | 
|  | 1966 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1967 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1968 | audio_io_handle_t io, | 
|  | 1969 | uint32_t strategy, | 
|  | 1970 | int session, | 
|  | 1971 | int id) | 
|  | 1972 | { | 
|  | 1973 | ssize_t index = mOutputs.indexOfKey(io); | 
|  | 1974 | if (index < 0) { | 
|  | 1975 | index = mInputs.indexOfKey(io); | 
|  | 1976 | if (index < 0) { | 
|  | 1977 | ALOGW("registerEffect() unknown io %d", io); | 
|  | 1978 | return INVALID_OPERATION; | 
|  | 1979 | } | 
|  | 1980 | } | 
| François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 1981 | return mEffects.registerEffect(desc, io, strategy, session, id); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1982 | } | 
|  | 1983 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1984 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const | 
|  | 1985 | { | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1986 | bool active = false; | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 1987 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) { | 
|  | 1988 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1989 | continue; | 
|  | 1990 | } | 
|  | 1991 | active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs); | 
|  | 1992 | } | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1993 | return active; | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1994 | } | 
|  | 1995 |  | 
|  | 1996 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const | 
|  | 1997 | { | 
|  | 1998 | return mOutputs.isStreamActiveRemotely(stream, inPastMs); | 
|  | 1999 | } | 
|  | 2000 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2001 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2002 | { | 
|  | 2003 | for (size_t i = 0; i < mInputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2004 | const sp<AudioInputDescriptor>  inputDescriptor = mInputs.valueAt(i); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2005 | if (inputDescriptor->isSourceActive(source)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2006 | return true; | 
|  | 2007 | } | 
|  | 2008 | } | 
|  | 2009 | return false; | 
|  | 2010 | } | 
|  | 2011 |  | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2012 | // Register a list of custom mixes with their attributes and format. | 
|  | 2013 | // When a mix is registered, corresponding input and output profiles are | 
|  | 2014 | // added to the remote submix hw module. The profile contains only the | 
|  | 2015 | // parameters (sampling rate, format...) specified by the mix. | 
|  | 2016 | // The corresponding input remote submix device is also connected. | 
|  | 2017 | // | 
|  | 2018 | // When a remote submix device is connected, the address is checked to select the | 
|  | 2019 | // appropriate profile and the corresponding input or output stream is opened. | 
|  | 2020 | // | 
|  | 2021 | // When capture starts, getInputForAttr() will: | 
|  | 2022 | //  - 1 look for a mix matching the address passed in attribtutes tags if any | 
|  | 2023 | //  - 2 if none found, getDeviceForInputSource() will: | 
|  | 2024 | //     - 2.1 look for a mix matching the attributes source | 
|  | 2025 | //     - 2.2 if none found, default to device selection by policy rules | 
|  | 2026 | // At this time, the corresponding output remote submix device is also connected | 
|  | 2027 | // and active playback use cases can be transferred to this mix if needed when reconnecting | 
|  | 2028 | // after AudioTracks are invalidated | 
|  | 2029 | // | 
|  | 2030 | // When playback starts, getOutputForAttr() will: | 
|  | 2031 | //  - 1 look for a mix matching the address passed in attribtutes tags if any | 
|  | 2032 | //  - 2 if none found, look for a mix matching the attributes usage | 
|  | 2033 | //  - 3 if none found, default to device and output selection by policy rules. | 
|  | 2034 |  | 
|  | 2035 | status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes) | 
|  | 2036 | { | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2037 | ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size()); | 
|  | 2038 | status_t res = NO_ERROR; | 
|  | 2039 |  | 
|  | 2040 | sp<HwModule> rSubmixModule; | 
|  | 2041 | // examine each mix's route type | 
|  | 2042 | for (size_t i = 0; i < mixes.size(); i++) { | 
|  | 2043 | // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination | 
|  | 2044 | if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) { | 
|  | 2045 | res = INVALID_OPERATION; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2046 | break; | 
|  | 2047 | } | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2048 | if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) { | 
|  | 2049 | // Loop back through "remote submix" | 
|  | 2050 | if (rSubmixModule == 0) { | 
|  | 2051 | for (size_t j = 0; i < mHwModules.size(); j++) { | 
|  | 2052 | if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0 | 
|  | 2053 | && mHwModules[j]->mHandle != 0) { | 
|  | 2054 | rSubmixModule = mHwModules[j]; | 
|  | 2055 | break; | 
|  | 2056 | } | 
|  | 2057 | } | 
|  | 2058 | } | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2059 |  | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2060 | ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK", i, mixes.size()); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2061 |  | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2062 | if (rSubmixModule == 0) { | 
|  | 2063 | ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", i); | 
|  | 2064 | res = INVALID_OPERATION; | 
|  | 2065 | break; | 
|  | 2066 | } | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2067 |  | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2068 | String8 address = mixes[i].mDeviceAddress; | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2069 |  | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2070 | if (mPolicyMixes.registerMix(address, mixes[i], 0 /*output desc*/) != NO_ERROR) { | 
| Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2071 | ALOGE(" Error registering mix %zu for address %s", i, address.string()); | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2072 | res = INVALID_OPERATION; | 
|  | 2073 | break; | 
|  | 2074 | } | 
|  | 2075 | audio_config_t outputConfig = mixes[i].mFormat; | 
|  | 2076 | audio_config_t inputConfig = mixes[i].mFormat; | 
|  | 2077 | // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in | 
|  | 2078 | // stereo and let audio flinger do the channel conversion if needed. | 
|  | 2079 | outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; | 
|  | 2080 | inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; | 
|  | 2081 | rSubmixModule->addOutputProfile(address, &outputConfig, | 
|  | 2082 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address); | 
|  | 2083 | rSubmixModule->addInputProfile(address, &inputConfig, | 
|  | 2084 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, address); | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2085 |  | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2086 | if (mixes[i].mMixType == MIX_TYPE_PLAYERS) { | 
|  | 2087 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, | 
|  | 2088 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, | 
|  | 2089 | address.string(), "remote-submix"); | 
|  | 2090 | } else { | 
|  | 2091 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, | 
|  | 2092 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, | 
|  | 2093 | address.string(), "remote-submix"); | 
|  | 2094 | } | 
|  | 2095 | } else if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2096 | String8 address = mixes[i].mDeviceAddress; | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2097 | audio_devices_t device = mixes[i].mDeviceType; | 
| Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2098 | ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s", | 
|  | 2099 | i, mixes.size(), device, address.string()); | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2100 |  | 
| Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2101 | bool foundOutput = false; | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2102 | for (size_t j = 0 ; j < mOutputs.size() ; j++) { | 
|  | 2103 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j); | 
|  | 2104 | sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle()); | 
|  | 2105 | if ((patch != 0) && (patch->mPatch.num_sinks != 0) | 
|  | 2106 | && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE) | 
|  | 2107 | && (patch->mPatch.sinks[0].ext.device.type == device) | 
| Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2108 | && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(), | 
|  | 2109 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2110 | if (mPolicyMixes.registerMix(address, mixes[i], desc) != NO_ERROR) { | 
|  | 2111 | res = INVALID_OPERATION; | 
| Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2112 | } else { | 
|  | 2113 | foundOutput = true; | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2114 | } | 
|  | 2115 | break; | 
|  | 2116 | } | 
|  | 2117 | } | 
|  | 2118 |  | 
|  | 2119 | if (res != NO_ERROR) { | 
|  | 2120 | ALOGE(" Error registering mix %zu for device 0x%X addr %s", | 
| Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2121 | i, device, address.string()); | 
|  | 2122 | res = INVALID_OPERATION; | 
|  | 2123 | break; | 
|  | 2124 | } else if (!foundOutput) { | 
|  | 2125 | ALOGE(" Output not found for mix %zu for device 0x%X addr %s", | 
|  | 2126 | i, device, address.string()); | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2127 | res = INVALID_OPERATION; | 
|  | 2128 | break; | 
|  | 2129 | } | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2130 | } | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2131 | } | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2132 | if (res != NO_ERROR) { | 
|  | 2133 | unregisterPolicyMixes(mixes); | 
|  | 2134 | } | 
|  | 2135 | return res; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2136 | } | 
|  | 2137 |  | 
|  | 2138 | status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes) | 
|  | 2139 | { | 
| Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2140 | ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size()); | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2141 | status_t res = NO_ERROR; | 
|  | 2142 | sp<HwModule> rSubmixModule; | 
|  | 2143 | // examine each mix's route type | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2144 | for (size_t i = 0; i < mixes.size(); i++) { | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2145 | if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) { | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2146 |  | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2147 | if (rSubmixModule == 0) { | 
|  | 2148 | for (size_t j = 0; i < mHwModules.size(); j++) { | 
|  | 2149 | if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0 | 
|  | 2150 | && mHwModules[j]->mHandle != 0) { | 
|  | 2151 | rSubmixModule = mHwModules[j]; | 
|  | 2152 | break; | 
|  | 2153 | } | 
|  | 2154 | } | 
|  | 2155 | } | 
|  | 2156 | if (rSubmixModule == 0) { | 
|  | 2157 | res = INVALID_OPERATION; | 
|  | 2158 | continue; | 
|  | 2159 | } | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2160 |  | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2161 | String8 address = mixes[i].mDeviceAddress; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2162 |  | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2163 | if (mPolicyMixes.unregisterMix(address) != NO_ERROR) { | 
|  | 2164 | res = INVALID_OPERATION; | 
|  | 2165 | continue; | 
|  | 2166 | } | 
|  | 2167 |  | 
|  | 2168 | if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) == | 
|  | 2169 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE)  { | 
|  | 2170 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, | 
|  | 2171 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, | 
|  | 2172 | address.string(), "remote-submix"); | 
|  | 2173 | } | 
|  | 2174 | if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) == | 
|  | 2175 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE)  { | 
|  | 2176 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, | 
|  | 2177 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, | 
|  | 2178 | address.string(), "remote-submix"); | 
|  | 2179 | } | 
|  | 2180 | rSubmixModule->removeOutputProfile(address); | 
|  | 2181 | rSubmixModule->removeInputProfile(address); | 
|  | 2182 |  | 
|  | 2183 | } if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { | 
|  | 2184 | if (mPolicyMixes.unregisterMix(mixes[i].mDeviceAddress) != NO_ERROR) { | 
|  | 2185 | res = INVALID_OPERATION; | 
|  | 2186 | continue; | 
|  | 2187 | } | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2188 | } | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2189 | } | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2190 | return res; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2191 | } | 
|  | 2192 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2193 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2194 | status_t AudioPolicyManager::dump(int fd) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2195 | { | 
|  | 2196 | const size_t SIZE = 256; | 
|  | 2197 | char buffer[SIZE]; | 
|  | 2198 | String8 result; | 
|  | 2199 |  | 
|  | 2200 | snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this); | 
|  | 2201 | result.append(buffer); | 
|  | 2202 |  | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 2203 | snprintf(buffer, SIZE, " Primary Output: %d\n", | 
|  | 2204 | hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2205 | result.append(buffer); | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2206 | snprintf(buffer, SIZE, " Phone state: %d\n", mEngine->getPhoneState()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2207 | result.append(buffer); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2208 | snprintf(buffer, SIZE, " Force use for communications %d\n", | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2209 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2210 | result.append(buffer); | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2211 | 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] | 2212 | result.append(buffer); | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2213 | 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] | 2214 | result.append(buffer); | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2215 | 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] | 2216 | result.append(buffer); | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2217 | 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] | 2218 | result.append(buffer); | 
| Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 2219 | snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n", | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2220 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO)); | 
| Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 2221 | result.append(buffer); | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 2222 | snprintf(buffer, SIZE, " Force use for encoded surround output %d\n", | 
|  | 2223 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND)); | 
|  | 2224 | result.append(buffer); | 
| Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 2225 | snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available"); | 
|  | 2226 | result.append(buffer); | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 2227 | snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off"); | 
|  | 2228 | result.append(buffer); | 
| Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 2229 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2230 | write(fd, result.string(), result.size()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2231 |  | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 2232 | mAvailableOutputDevices.dump(fd, String8("Available output")); | 
|  | 2233 | mAvailableInputDevices.dump(fd, String8("Available input")); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2234 | mHwModules.dump(fd); | 
|  | 2235 | mOutputs.dump(fd); | 
|  | 2236 | mInputs.dump(fd); | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2237 | mVolumeCurves->dump(fd); | 
| François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2238 | mEffects.dump(fd); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2239 | mAudioPatches.dump(fd); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2240 |  | 
|  | 2241 | return NO_ERROR; | 
|  | 2242 | } | 
|  | 2243 |  | 
|  | 2244 | // This function checks for the parameters which can be offloaded. | 
|  | 2245 | // This can be enhanced depending on the capability of the DSP and policy | 
|  | 2246 | // of the system. | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2247 | bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2248 | { | 
|  | 2249 | ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2250 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2251 | offloadInfo.sample_rate, offloadInfo.channel_mask, | 
|  | 2252 | offloadInfo.format, | 
|  | 2253 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, | 
|  | 2254 | offloadInfo.has_video); | 
|  | 2255 |  | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 2256 | if (mMasterMono) { | 
|  | 2257 | return false; // no offloading if mono is set. | 
|  | 2258 | } | 
|  | 2259 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2260 | // Check if offload has been disabled | 
|  | 2261 | char propValue[PROPERTY_VALUE_MAX]; | 
|  | 2262 | if (property_get("audio.offload.disable", propValue, "0")) { | 
|  | 2263 | if (atoi(propValue) != 0) { | 
|  | 2264 | ALOGV("offload disabled by audio.offload.disable=%s", propValue ); | 
|  | 2265 | return false; | 
|  | 2266 | } | 
|  | 2267 | } | 
|  | 2268 |  | 
|  | 2269 | // Check if stream type is music, then only allow offload as of now. | 
|  | 2270 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) | 
|  | 2271 | { | 
|  | 2272 | ALOGV("isOffloadSupported: stream_type != MUSIC, returning false"); | 
|  | 2273 | return false; | 
|  | 2274 | } | 
|  | 2275 |  | 
|  | 2276 | //TODO: enable audio offloading with video when ready | 
| Andy Hung | 08945c4 | 2015-05-31 21:36:46 -0700 | [diff] [blame] | 2277 | const bool allowOffloadWithVideo = | 
|  | 2278 | property_get_bool("audio.offload.video", false /* default_value */); | 
|  | 2279 | if (offloadInfo.has_video && !allowOffloadWithVideo) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2280 | ALOGV("isOffloadSupported: has_video == true, returning false"); | 
|  | 2281 | return false; | 
|  | 2282 | } | 
|  | 2283 |  | 
|  | 2284 | //If duration is less than minimum value defined in property, return false | 
|  | 2285 | if (property_get("audio.offload.min.duration.secs", propValue, NULL)) { | 
|  | 2286 | if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) { | 
|  | 2287 | ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue); | 
|  | 2288 | return false; | 
|  | 2289 | } | 
|  | 2290 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { | 
|  | 2291 | ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS); | 
|  | 2292 | return false; | 
|  | 2293 | } | 
|  | 2294 |  | 
|  | 2295 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from | 
|  | 2296 | // creating an offloaded track and tearing it down immediately after start when audioflinger | 
|  | 2297 | // detects there is an active non offloadable effect. | 
|  | 2298 | // FIXME: We should check the audio session here but we do not have it in this context. | 
|  | 2299 | // This may prevent offloading in rare situations where effects are left active by apps | 
|  | 2300 | // in the background. | 
| François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2301 | if (mEffects.isNonOffloadableEffectEnabled()) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2302 | return false; | 
|  | 2303 | } | 
|  | 2304 |  | 
|  | 2305 | // See if there is a profile to support this. | 
|  | 2306 | // AUDIO_DEVICE_NONE | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2307 | sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2308 | offloadInfo.sample_rate, | 
|  | 2309 | offloadInfo.format, | 
|  | 2310 | offloadInfo.channel_mask, | 
|  | 2311 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2312 | ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT "); | 
|  | 2313 | return (profile != 0); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2314 | } | 
|  | 2315 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2316 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, | 
|  | 2317 | audio_port_type_t type, | 
|  | 2318 | unsigned int *num_ports, | 
|  | 2319 | struct audio_port *ports, | 
|  | 2320 | unsigned int *generation) | 
|  | 2321 | { | 
|  | 2322 | if (num_ports == NULL || (*num_ports != 0 && ports == NULL) || | 
|  | 2323 | generation == NULL) { | 
|  | 2324 | return BAD_VALUE; | 
|  | 2325 | } | 
|  | 2326 | ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports); | 
|  | 2327 | if (ports == NULL) { | 
|  | 2328 | *num_ports = 0; | 
|  | 2329 | } | 
|  | 2330 |  | 
|  | 2331 | size_t portsWritten = 0; | 
|  | 2332 | size_t portsMax = *num_ports; | 
|  | 2333 | *num_ports = 0; | 
|  | 2334 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { | 
| Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2335 | // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB | 
|  | 2336 | // as they are used by stub HALs by convention | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2337 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { | 
| Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2338 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { | 
|  | 2339 | if (mAvailableOutputDevices[i]->type() == AUDIO_DEVICE_OUT_STUB) { | 
|  | 2340 | continue; | 
|  | 2341 | } | 
|  | 2342 | if (portsWritten < portsMax) { | 
|  | 2343 | mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]); | 
|  | 2344 | } | 
|  | 2345 | (*num_ports)++; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2346 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2347 | } | 
|  | 2348 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { | 
| Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2349 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { | 
|  | 2350 | if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_STUB) { | 
|  | 2351 | continue; | 
|  | 2352 | } | 
|  | 2353 | if (portsWritten < portsMax) { | 
|  | 2354 | mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]); | 
|  | 2355 | } | 
|  | 2356 | (*num_ports)++; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2357 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2358 | } | 
|  | 2359 | } | 
|  | 2360 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { | 
|  | 2361 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { | 
|  | 2362 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { | 
|  | 2363 | mInputs[i]->toAudioPort(&ports[portsWritten++]); | 
|  | 2364 | } | 
|  | 2365 | *num_ports += mInputs.size(); | 
|  | 2366 | } | 
|  | 2367 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2368 | size_t numOutputs = 0; | 
|  | 2369 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 2370 | if (!mOutputs[i]->isDuplicated()) { | 
|  | 2371 | numOutputs++; | 
|  | 2372 | if (portsWritten < portsMax) { | 
|  | 2373 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); | 
|  | 2374 | } | 
|  | 2375 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2376 | } | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2377 | *num_ports += numOutputs; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2378 | } | 
|  | 2379 | } | 
|  | 2380 | *generation = curAudioPortGeneration(); | 
| Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2381 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2382 | return NO_ERROR; | 
|  | 2383 | } | 
|  | 2384 |  | 
|  | 2385 | status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused) | 
|  | 2386 | { | 
|  | 2387 | return NO_ERROR; | 
|  | 2388 | } | 
|  | 2389 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2390 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, | 
|  | 2391 | audio_patch_handle_t *handle, | 
|  | 2392 | uid_t uid) | 
|  | 2393 | { | 
|  | 2394 | ALOGV("createAudioPatch()"); | 
|  | 2395 |  | 
|  | 2396 | if (handle == NULL || patch == NULL) { | 
|  | 2397 | return BAD_VALUE; | 
|  | 2398 | } | 
|  | 2399 | ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks); | 
|  | 2400 |  | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2401 | if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX || | 
|  | 2402 | patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) { | 
|  | 2403 | return BAD_VALUE; | 
|  | 2404 | } | 
|  | 2405 | // only one source per audio patch supported for now | 
|  | 2406 | if (patch->num_sources > 1) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2407 | return INVALID_OPERATION; | 
|  | 2408 | } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2409 |  | 
|  | 2410 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2411 | return INVALID_OPERATION; | 
|  | 2412 | } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2413 | for (size_t i = 0; i < patch->num_sinks; i++) { | 
|  | 2414 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { | 
|  | 2415 | return INVALID_OPERATION; | 
|  | 2416 | } | 
|  | 2417 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2418 |  | 
|  | 2419 | sp<AudioPatch> patchDesc; | 
|  | 2420 | ssize_t index = mAudioPatches.indexOfKey(*handle); | 
|  | 2421 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2422 | ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id, | 
|  | 2423 | patch->sources[0].role, | 
|  | 2424 | patch->sources[0].type); | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2425 | #if LOG_NDEBUG == 0 | 
|  | 2426 | for (size_t i = 0; i < patch->num_sinks; i++) { | 
| Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2427 | 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] | 2428 | patch->sinks[i].role, | 
|  | 2429 | patch->sinks[i].type); | 
|  | 2430 | } | 
|  | 2431 | #endif | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2432 |  | 
|  | 2433 | if (index >= 0) { | 
|  | 2434 | patchDesc = mAudioPatches.valueAt(index); | 
|  | 2435 | ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", | 
|  | 2436 | mUidCached, patchDesc->mUid, uid); | 
|  | 2437 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { | 
|  | 2438 | return INVALID_OPERATION; | 
|  | 2439 | } | 
|  | 2440 | } else { | 
| Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 2441 | *handle = AUDIO_PATCH_HANDLE_NONE; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2442 | } | 
|  | 2443 |  | 
|  | 2444 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2445 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2446 | if (outputDesc == NULL) { | 
|  | 2447 | ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id); | 
|  | 2448 | return BAD_VALUE; | 
|  | 2449 | } | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2450 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", | 
|  | 2451 | outputDesc->mIoHandle); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2452 | if (patchDesc != 0) { | 
|  | 2453 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { | 
|  | 2454 | ALOGV("createAudioPatch() source id differs for patch current id %d new id %d", | 
|  | 2455 | patchDesc->mPatch.sources[0].id, patch->sources[0].id); | 
|  | 2456 | return BAD_VALUE; | 
|  | 2457 | } | 
|  | 2458 | } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2459 | DeviceVector devices; | 
|  | 2460 | for (size_t i = 0; i < patch->num_sinks; i++) { | 
|  | 2461 | // Only support mix to devices connection | 
|  | 2462 | // TODO add support for mix to mix connection | 
|  | 2463 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { | 
|  | 2464 | ALOGV("createAudioPatch() source mix but sink is not a device"); | 
|  | 2465 | return INVALID_OPERATION; | 
|  | 2466 | } | 
|  | 2467 | sp<DeviceDescriptor> devDesc = | 
|  | 2468 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); | 
|  | 2469 | if (devDesc == 0) { | 
|  | 2470 | ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id); | 
|  | 2471 | return BAD_VALUE; | 
|  | 2472 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2473 |  | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2474 | if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(), | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2475 | devDesc->mAddress, | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2476 | patch->sources[0].sample_rate, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2477 | NULL,  // updatedSamplingRate | 
|  | 2478 | patch->sources[0].format, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2479 | NULL,  // updatedFormat | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2480 | patch->sources[0].channel_mask, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2481 | NULL,  // updatedChannelMask | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2482 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2483 | ALOGV("createAudioPatch() profile not supported for device %08x", | 
|  | 2484 | devDesc->type()); | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2485 | return INVALID_OPERATION; | 
|  | 2486 | } | 
|  | 2487 | devices.add(devDesc); | 
|  | 2488 | } | 
|  | 2489 | if (devices.size() == 0) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2490 | return INVALID_OPERATION; | 
|  | 2491 | } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2492 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2493 | // TODO: reconfigure output format and channels here | 
|  | 2494 | ALOGV("createAudioPatch() setting device %08x on output %d", | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2495 | devices.types(), outputDesc->mIoHandle); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2496 | setOutputDevice(outputDesc, devices.types(), true, 0, handle); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2497 | index = mAudioPatches.indexOfKey(*handle); | 
|  | 2498 | if (index >= 0) { | 
|  | 2499 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { | 
|  | 2500 | ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided"); | 
|  | 2501 | } | 
|  | 2502 | patchDesc = mAudioPatches.valueAt(index); | 
|  | 2503 | patchDesc->mUid = uid; | 
|  | 2504 | ALOGV("createAudioPatch() success"); | 
|  | 2505 | } else { | 
|  | 2506 | ALOGW("createAudioPatch() setOutputDevice() failed to create a patch"); | 
|  | 2507 | return INVALID_OPERATION; | 
|  | 2508 | } | 
|  | 2509 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { | 
|  | 2510 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { | 
|  | 2511 | // input device to input mix connection | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2512 | // only one sink supported when connecting an input device to a mix | 
|  | 2513 | if (patch->num_sinks > 1) { | 
|  | 2514 | return INVALID_OPERATION; | 
|  | 2515 | } | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2516 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2517 | if (inputDesc == NULL) { | 
|  | 2518 | return BAD_VALUE; | 
|  | 2519 | } | 
|  | 2520 | if (patchDesc != 0) { | 
|  | 2521 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { | 
|  | 2522 | return BAD_VALUE; | 
|  | 2523 | } | 
|  | 2524 | } | 
|  | 2525 | sp<DeviceDescriptor> devDesc = | 
|  | 2526 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); | 
|  | 2527 | if (devDesc == 0) { | 
|  | 2528 | return BAD_VALUE; | 
|  | 2529 | } | 
|  | 2530 |  | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2531 | if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(), | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2532 | devDesc->mAddress, | 
|  | 2533 | patch->sinks[0].sample_rate, | 
|  | 2534 | NULL, /*updatedSampleRate*/ | 
|  | 2535 | patch->sinks[0].format, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2536 | NULL, /*updatedFormat*/ | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2537 | patch->sinks[0].channel_mask, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2538 | NULL, /*updatedChannelMask*/ | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2539 | // FIXME for the parameter type, | 
|  | 2540 | // and the NONE | 
|  | 2541 | (audio_output_flags_t) | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2542 | AUDIO_INPUT_FLAG_NONE)) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2543 | return INVALID_OPERATION; | 
|  | 2544 | } | 
|  | 2545 | // TODO: reconfigure output format and channels here | 
|  | 2546 | ALOGV("createAudioPatch() setting device %08x on output %d", | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2547 | devDesc->type(), inputDesc->mIoHandle); | 
|  | 2548 | setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2549 | index = mAudioPatches.indexOfKey(*handle); | 
|  | 2550 | if (index >= 0) { | 
|  | 2551 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { | 
|  | 2552 | ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided"); | 
|  | 2553 | } | 
|  | 2554 | patchDesc = mAudioPatches.valueAt(index); | 
|  | 2555 | patchDesc->mUid = uid; | 
|  | 2556 | ALOGV("createAudioPatch() success"); | 
|  | 2557 | } else { | 
|  | 2558 | ALOGW("createAudioPatch() setInputDevice() failed to create a patch"); | 
|  | 2559 | return INVALID_OPERATION; | 
|  | 2560 | } | 
|  | 2561 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { | 
|  | 2562 | // device to device connection | 
|  | 2563 | if (patchDesc != 0) { | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2564 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2565 | return BAD_VALUE; | 
|  | 2566 | } | 
|  | 2567 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2568 | sp<DeviceDescriptor> srcDeviceDesc = | 
|  | 2569 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); | 
| Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 2570 | if (srcDeviceDesc == 0) { | 
|  | 2571 | return BAD_VALUE; | 
|  | 2572 | } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2573 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2574 | //update source and sink with our own data as the data passed in the patch may | 
|  | 2575 | // be incomplete. | 
|  | 2576 | struct audio_patch newPatch = *patch; | 
|  | 2577 | srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2578 |  | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2579 | for (size_t i = 0; i < patch->num_sinks; i++) { | 
|  | 2580 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { | 
|  | 2581 | ALOGV("createAudioPatch() source device but one sink is not a device"); | 
|  | 2582 | return INVALID_OPERATION; | 
|  | 2583 | } | 
|  | 2584 |  | 
|  | 2585 | sp<DeviceDescriptor> sinkDeviceDesc = | 
|  | 2586 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); | 
|  | 2587 | if (sinkDeviceDesc == 0) { | 
|  | 2588 | return BAD_VALUE; | 
|  | 2589 | } | 
|  | 2590 | sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]); | 
|  | 2591 |  | 
| Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2592 | // create a software bridge in PatchPanel if: | 
|  | 2593 | // - source and sink devices are on differnt HW modules OR | 
|  | 2594 | // - audio HAL version is < 3.0 | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 2595 | if ((srcDeviceDesc->getModuleHandle() != sinkDeviceDesc->getModuleHandle()) || | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 2596 | (srcDeviceDesc->mModule->getHalVersion() < AUDIO_DEVICE_API_VERSION_3_0)) { | 
| Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2597 | // support only one sink device for now to simplify output selection logic | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2598 | if (patch->num_sinks > 1) { | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2599 | return INVALID_OPERATION; | 
|  | 2600 | } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2601 | SortedVector<audio_io_handle_t> outputs = | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2602 | getOutputsForDevice(sinkDeviceDesc->type(), mOutputs); | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2603 | // if the sink device is reachable via an opened output stream, request to go via | 
|  | 2604 | // this output stream by adding a second source to the patch description | 
| Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 2605 | audio_io_handle_t output = selectOutput(outputs, | 
|  | 2606 | AUDIO_OUTPUT_FLAG_NONE, | 
|  | 2607 | AUDIO_FORMAT_INVALID); | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2608 | if (output != AUDIO_IO_HANDLE_NONE) { | 
|  | 2609 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
|  | 2610 | if (outputDesc->isDuplicated()) { | 
|  | 2611 | return INVALID_OPERATION; | 
|  | 2612 | } | 
|  | 2613 | outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]); | 
| Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2614 | newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2615 | newPatch.num_sources = 2; | 
|  | 2616 | } | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2617 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2618 | } | 
|  | 2619 | // TODO: check from routing capabilities in config file and other conflicting patches | 
|  | 2620 |  | 
|  | 2621 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; | 
|  | 2622 | if (index >= 0) { | 
|  | 2623 | afPatchHandle = patchDesc->mAfPatchHandle; | 
|  | 2624 | } | 
|  | 2625 |  | 
|  | 2626 | status_t status = mpClientInterface->createAudioPatch(&newPatch, | 
|  | 2627 | &afPatchHandle, | 
|  | 2628 | 0); | 
|  | 2629 | ALOGV("createAudioPatch() patch panel returned %d patchHandle %d", | 
|  | 2630 | status, afPatchHandle); | 
|  | 2631 | if (status == NO_ERROR) { | 
|  | 2632 | if (index < 0) { | 
| François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 2633 | patchDesc = new AudioPatch(&newPatch, uid); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2634 | addAudioPatch(patchDesc->mHandle, patchDesc); | 
|  | 2635 | } else { | 
|  | 2636 | patchDesc->mPatch = newPatch; | 
|  | 2637 | } | 
|  | 2638 | patchDesc->mAfPatchHandle = afPatchHandle; | 
|  | 2639 | *handle = patchDesc->mHandle; | 
|  | 2640 | nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2641 | mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2642 | } else { | 
|  | 2643 | ALOGW("createAudioPatch() patch panel could not connect device patch, error %d", | 
|  | 2644 | status); | 
|  | 2645 | return INVALID_OPERATION; | 
|  | 2646 | } | 
|  | 2647 | } else { | 
|  | 2648 | return BAD_VALUE; | 
|  | 2649 | } | 
|  | 2650 | } else { | 
|  | 2651 | return BAD_VALUE; | 
|  | 2652 | } | 
|  | 2653 | return NO_ERROR; | 
|  | 2654 | } | 
|  | 2655 |  | 
|  | 2656 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, | 
|  | 2657 | uid_t uid) | 
|  | 2658 | { | 
|  | 2659 | ALOGV("releaseAudioPatch() patch %d", handle); | 
|  | 2660 |  | 
|  | 2661 | ssize_t index = mAudioPatches.indexOfKey(handle); | 
|  | 2662 |  | 
|  | 2663 | if (index < 0) { | 
|  | 2664 | return BAD_VALUE; | 
|  | 2665 | } | 
|  | 2666 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
|  | 2667 | ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", | 
|  | 2668 | mUidCached, patchDesc->mUid, uid); | 
|  | 2669 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { | 
|  | 2670 | return INVALID_OPERATION; | 
|  | 2671 | } | 
|  | 2672 |  | 
|  | 2673 | struct audio_patch *patch = &patchDesc->mPatch; | 
|  | 2674 | patchDesc->mUid = mUidCached; | 
|  | 2675 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2676 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2677 | if (outputDesc == NULL) { | 
|  | 2678 | ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id); | 
|  | 2679 | return BAD_VALUE; | 
|  | 2680 | } | 
|  | 2681 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2682 | setOutputDevice(outputDesc, | 
|  | 2683 | getNewOutputDevice(outputDesc, true /*fromCache*/), | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2684 | true, | 
|  | 2685 | 0, | 
|  | 2686 | NULL); | 
|  | 2687 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { | 
|  | 2688 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2689 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2690 | if (inputDesc == NULL) { | 
|  | 2691 | ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id); | 
|  | 2692 | return BAD_VALUE; | 
|  | 2693 | } | 
|  | 2694 | setInputDevice(inputDesc->mIoHandle, | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 2695 | getNewInputDevice(inputDesc->mIoHandle), | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2696 | true, | 
|  | 2697 | NULL); | 
|  | 2698 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { | 
|  | 2699 | audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle; | 
|  | 2700 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); | 
|  | 2701 | ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d", | 
|  | 2702 | status, patchDesc->mAfPatchHandle); | 
|  | 2703 | removeAudioPatch(patchDesc->mHandle); | 
|  | 2704 | nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2705 | mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2706 | } else { | 
|  | 2707 | return BAD_VALUE; | 
|  | 2708 | } | 
|  | 2709 | } else { | 
|  | 2710 | return BAD_VALUE; | 
|  | 2711 | } | 
|  | 2712 | return NO_ERROR; | 
|  | 2713 | } | 
|  | 2714 |  | 
|  | 2715 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, | 
|  | 2716 | struct audio_patch *patches, | 
|  | 2717 | unsigned int *generation) | 
|  | 2718 | { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2719 | if (generation == NULL) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2720 | return BAD_VALUE; | 
|  | 2721 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2722 | *generation = curAudioPortGeneration(); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2723 | return mAudioPatches.listAudioPatches(num_patches, patches); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2724 | } | 
|  | 2725 |  | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2726 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2727 | { | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2728 | ALOGV("setAudioPortConfig()"); | 
|  | 2729 |  | 
|  | 2730 | if (config == NULL) { | 
|  | 2731 | return BAD_VALUE; | 
|  | 2732 | } | 
|  | 2733 | ALOGV("setAudioPortConfig() on port handle %d", config->id); | 
|  | 2734 | // Only support gain configuration for now | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2735 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { | 
|  | 2736 | return INVALID_OPERATION; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2737 | } | 
|  | 2738 |  | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2739 | sp<AudioPortConfig> audioPortConfig; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2740 | if (config->type == AUDIO_PORT_TYPE_MIX) { | 
|  | 2741 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2742 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id); | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2743 | if (outputDesc == NULL) { | 
|  | 2744 | return BAD_VALUE; | 
|  | 2745 | } | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2746 | ALOG_ASSERT(!outputDesc->isDuplicated(), | 
|  | 2747 | "setAudioPortConfig() called on duplicated output %d", | 
|  | 2748 | outputDesc->mIoHandle); | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2749 | audioPortConfig = outputDesc; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2750 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2751 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id); | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2752 | if (inputDesc == NULL) { | 
|  | 2753 | return BAD_VALUE; | 
|  | 2754 | } | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2755 | audioPortConfig = inputDesc; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2756 | } else { | 
|  | 2757 | return BAD_VALUE; | 
|  | 2758 | } | 
|  | 2759 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { | 
|  | 2760 | sp<DeviceDescriptor> deviceDesc; | 
|  | 2761 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { | 
|  | 2762 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); | 
|  | 2763 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { | 
|  | 2764 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); | 
|  | 2765 | } else { | 
|  | 2766 | return BAD_VALUE; | 
|  | 2767 | } | 
|  | 2768 | if (deviceDesc == NULL) { | 
|  | 2769 | return BAD_VALUE; | 
|  | 2770 | } | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2771 | audioPortConfig = deviceDesc; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2772 | } else { | 
|  | 2773 | return BAD_VALUE; | 
|  | 2774 | } | 
|  | 2775 |  | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2776 | struct audio_port_config backupConfig; | 
|  | 2777 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); | 
|  | 2778 | if (status == NO_ERROR) { | 
|  | 2779 | struct audio_port_config newConfig; | 
|  | 2780 | audioPortConfig->toAudioPortConfig(&newConfig, config); | 
|  | 2781 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2782 | } | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2783 | if (status != NO_ERROR) { | 
|  | 2784 | audioPortConfig->applyAudioPortConfig(&backupConfig); | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2785 | } | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2786 |  | 
|  | 2787 | return status; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2788 | } | 
|  | 2789 |  | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2790 | void AudioPolicyManager::releaseResourcesForUid(uid_t uid) | 
|  | 2791 | { | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2792 | clearAudioSources(uid); | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2793 | clearAudioPatches(uid); | 
|  | 2794 | clearSessionRoutes(uid); | 
|  | 2795 | } | 
|  | 2796 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2797 | void AudioPolicyManager::clearAudioPatches(uid_t uid) | 
|  | 2798 | { | 
| Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 2799 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--)  { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2800 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); | 
|  | 2801 | if (patchDesc->mUid == uid) { | 
| Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 2802 | releaseAudioPatch(mAudioPatches.keyAt(i), uid); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2803 | } | 
|  | 2804 | } | 
|  | 2805 | } | 
|  | 2806 |  | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2807 | void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy, | 
|  | 2808 | audio_io_handle_t ouptutToSkip) | 
|  | 2809 | { | 
|  | 2810 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); | 
|  | 2811 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); | 
|  | 2812 | for (size_t j = 0; j < mOutputs.size(); j++) { | 
|  | 2813 | if (mOutputs.keyAt(j) == ouptutToSkip) { | 
|  | 2814 | continue; | 
|  | 2815 | } | 
|  | 2816 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j); | 
|  | 2817 | if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) { | 
|  | 2818 | continue; | 
|  | 2819 | } | 
|  | 2820 | // If the default device for this strategy is on another output mix, | 
|  | 2821 | // invalidate all tracks in this strategy to force re connection. | 
|  | 2822 | // Otherwise select new device on the output mix. | 
|  | 2823 | if (outputs.indexOf(mOutputs.keyAt(j)) < 0) { | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2824 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2825 | if (getStrategy((audio_stream_type_t)stream) == strategy) { | 
|  | 2826 | mpClientInterface->invalidateStream((audio_stream_type_t)stream); | 
|  | 2827 | } | 
|  | 2828 | } | 
|  | 2829 | } else { | 
|  | 2830 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); | 
|  | 2831 | setOutputDevice(outputDesc, newDevice, false); | 
|  | 2832 | } | 
|  | 2833 | } | 
|  | 2834 | } | 
|  | 2835 |  | 
|  | 2836 | void AudioPolicyManager::clearSessionRoutes(uid_t uid) | 
|  | 2837 | { | 
|  | 2838 | // remove output routes associated with this uid | 
|  | 2839 | SortedVector<routing_strategy> affectedStrategies; | 
|  | 2840 | for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--)  { | 
|  | 2841 | sp<SessionRoute> route = mOutputRoutes.valueAt(i); | 
|  | 2842 | if (route->mUid == uid) { | 
|  | 2843 | mOutputRoutes.removeItemsAt(i); | 
|  | 2844 | if (route->mDeviceDescriptor != 0) { | 
|  | 2845 | affectedStrategies.add(getStrategy(route->mStreamType)); | 
|  | 2846 | } | 
|  | 2847 | } | 
|  | 2848 | } | 
|  | 2849 | // reroute outputs if necessary | 
|  | 2850 | for (size_t i = 0; i < affectedStrategies.size(); i++) { | 
|  | 2851 | checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE); | 
|  | 2852 | } | 
|  | 2853 |  | 
|  | 2854 | // remove input routes associated with this uid | 
|  | 2855 | SortedVector<audio_source_t> affectedSources; | 
|  | 2856 | for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--)  { | 
|  | 2857 | sp<SessionRoute> route = mInputRoutes.valueAt(i); | 
|  | 2858 | if (route->mUid == uid) { | 
|  | 2859 | mInputRoutes.removeItemsAt(i); | 
|  | 2860 | if (route->mDeviceDescriptor != 0) { | 
|  | 2861 | affectedSources.add(route->mSource); | 
|  | 2862 | } | 
|  | 2863 | } | 
|  | 2864 | } | 
|  | 2865 | // reroute inputs if necessary | 
|  | 2866 | SortedVector<audio_io_handle_t> inputsToClose; | 
|  | 2867 | for (size_t i = 0; i < mInputs.size(); i++) { | 
|  | 2868 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2869 | if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) { | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2870 | inputsToClose.add(inputDesc->mIoHandle); | 
|  | 2871 | } | 
|  | 2872 | } | 
|  | 2873 | for (size_t i = 0; i < inputsToClose.size(); i++) { | 
|  | 2874 | closeInput(inputsToClose[i]); | 
|  | 2875 | } | 
|  | 2876 | } | 
|  | 2877 |  | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2878 | void AudioPolicyManager::clearAudioSources(uid_t uid) | 
|  | 2879 | { | 
|  | 2880 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--)  { | 
|  | 2881 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); | 
|  | 2882 | if (sourceDesc->mUid == uid) { | 
|  | 2883 | stopAudioSource(mAudioSources.keyAt(i)); | 
|  | 2884 | } | 
|  | 2885 | } | 
|  | 2886 | } | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2887 |  | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2888 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, | 
|  | 2889 | audio_io_handle_t *ioHandle, | 
|  | 2890 | audio_devices_t *device) | 
|  | 2891 | { | 
| Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 2892 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); | 
|  | 2893 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT); | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 2894 | *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD); | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2895 |  | 
| François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 2896 | return mSoundTriggerSessions.acquireSession(*session, *ioHandle); | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2897 | } | 
|  | 2898 |  | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2899 | status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source, | 
|  | 2900 | const audio_attributes_t *attributes, | 
|  | 2901 | audio_io_handle_t *handle, | 
|  | 2902 | uid_t uid) | 
| Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 2903 | { | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2904 | ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle); | 
|  | 2905 | if (source == NULL || attributes == NULL || handle == NULL) { | 
|  | 2906 | return BAD_VALUE; | 
|  | 2907 | } | 
|  | 2908 |  | 
|  | 2909 | *handle = AUDIO_IO_HANDLE_NONE; | 
|  | 2910 |  | 
|  | 2911 | if (source->role != AUDIO_PORT_ROLE_SOURCE || | 
|  | 2912 | source->type != AUDIO_PORT_TYPE_DEVICE) { | 
|  | 2913 | ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type); | 
|  | 2914 | return INVALID_OPERATION; | 
|  | 2915 | } | 
|  | 2916 |  | 
|  | 2917 | sp<DeviceDescriptor> srcDeviceDesc = | 
|  | 2918 | mAvailableInputDevices.getDevice(source->ext.device.type, | 
|  | 2919 | String8(source->ext.device.address)); | 
|  | 2920 | if (srcDeviceDesc == 0) { | 
|  | 2921 | ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type); | 
|  | 2922 | return BAD_VALUE; | 
|  | 2923 | } | 
|  | 2924 | sp<AudioSourceDescriptor> sourceDesc = | 
|  | 2925 | new AudioSourceDescriptor(srcDeviceDesc, attributes, uid); | 
|  | 2926 |  | 
|  | 2927 | struct audio_patch dummyPatch; | 
|  | 2928 | sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid); | 
|  | 2929 | sourceDesc->mPatchDesc = patchDesc; | 
|  | 2930 |  | 
|  | 2931 | status_t status = connectAudioSource(sourceDesc); | 
|  | 2932 | if (status == NO_ERROR) { | 
|  | 2933 | mAudioSources.add(sourceDesc->getHandle(), sourceDesc); | 
|  | 2934 | *handle = sourceDesc->getHandle(); | 
|  | 2935 | } | 
|  | 2936 | return status; | 
|  | 2937 | } | 
|  | 2938 |  | 
|  | 2939 | status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc) | 
|  | 2940 | { | 
|  | 2941 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle()); | 
|  | 2942 |  | 
|  | 2943 | // make sure we only have one patch per source. | 
|  | 2944 | disconnectAudioSource(sourceDesc); | 
|  | 2945 |  | 
|  | 2946 | routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes); | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2947 | audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes); | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2948 | sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice; | 
|  | 2949 |  | 
|  | 2950 | audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true); | 
|  | 2951 | sp<DeviceDescriptor> sinkDeviceDesc = | 
|  | 2952 | mAvailableOutputDevices.getDevice(sinkDevice, String8("")); | 
|  | 2953 |  | 
|  | 2954 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; | 
|  | 2955 | struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch; | 
|  | 2956 |  | 
|  | 2957 | if (srcDeviceDesc->getAudioPort()->mModule->getHandle() == | 
|  | 2958 | sinkDeviceDesc->getAudioPort()->mModule->getHandle() && | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 2959 | srcDeviceDesc->getAudioPort()->mModule->getHalVersion() >= AUDIO_DEVICE_API_VERSION_3_0 && | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2960 | srcDeviceDesc->getAudioPort()->mGains.size() > 0) { | 
|  | 2961 | ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__); | 
|  | 2962 | //   create patch between src device and output device | 
|  | 2963 | //   create Hwoutput and add to mHwOutputs | 
|  | 2964 | } else { | 
|  | 2965 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs); | 
|  | 2966 | audio_io_handle_t output = | 
|  | 2967 | selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID); | 
|  | 2968 | if (output == AUDIO_IO_HANDLE_NONE) { | 
|  | 2969 | ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice); | 
|  | 2970 | return INVALID_OPERATION; | 
|  | 2971 | } | 
|  | 2972 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
|  | 2973 | if (outputDesc->isDuplicated()) { | 
|  | 2974 | ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice); | 
|  | 2975 | return INVALID_OPERATION; | 
|  | 2976 | } | 
|  | 2977 | // create a special patch with no sink and two sources: | 
|  | 2978 | // - the second source indicates to PatchPanel through which output mix this patch should | 
|  | 2979 | // be connected as well as the stream type for volume control | 
|  | 2980 | // - the sink is defined by whatever output device is currently selected for the output | 
|  | 2981 | // though which this patch is routed. | 
|  | 2982 | patch->num_sinks = 0; | 
|  | 2983 | patch->num_sources = 2; | 
|  | 2984 | srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL); | 
|  | 2985 | outputDesc->toAudioPortConfig(&patch->sources[1], NULL); | 
|  | 2986 | patch->sources[1].ext.mix.usecase.stream = stream; | 
|  | 2987 | status_t status = mpClientInterface->createAudioPatch(patch, | 
|  | 2988 | &afPatchHandle, | 
|  | 2989 | 0); | 
|  | 2990 | ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__, | 
|  | 2991 | status, afPatchHandle); | 
|  | 2992 | if (status != NO_ERROR) { | 
|  | 2993 | ALOGW("%s patch panel could not connect device patch, error %d", | 
|  | 2994 | __FUNCTION__, status); | 
|  | 2995 | return INVALID_OPERATION; | 
|  | 2996 | } | 
|  | 2997 | uint32_t delayMs = 0; | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 2998 | status = startSource(outputDesc, stream, sinkDevice, NULL, &delayMs); | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2999 |  | 
|  | 3000 | if (status != NO_ERROR) { | 
|  | 3001 | mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0); | 
|  | 3002 | return status; | 
|  | 3003 | } | 
|  | 3004 | sourceDesc->mSwOutput = outputDesc; | 
|  | 3005 | if (delayMs != 0) { | 
|  | 3006 | usleep(delayMs * 1000); | 
|  | 3007 | } | 
|  | 3008 | } | 
|  | 3009 |  | 
|  | 3010 | sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle; | 
|  | 3011 | addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc); | 
|  | 3012 |  | 
|  | 3013 | return NO_ERROR; | 
| Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3014 | } | 
|  | 3015 |  | 
| Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 3016 | status_t AudioPolicyManager::stopAudioSource(audio_io_handle_t handle __unused) | 
| Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3017 | { | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3018 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle); | 
|  | 3019 | ALOGV("%s handle %d", __FUNCTION__, handle); | 
|  | 3020 | if (sourceDesc == 0) { | 
|  | 3021 | ALOGW("%s unknown source for handle %d", __FUNCTION__, handle); | 
|  | 3022 | return BAD_VALUE; | 
|  | 3023 | } | 
|  | 3024 | status_t status = disconnectAudioSource(sourceDesc); | 
|  | 3025 |  | 
|  | 3026 | mAudioSources.removeItem(handle); | 
|  | 3027 | return status; | 
|  | 3028 | } | 
|  | 3029 |  | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3030 | status_t AudioPolicyManager::setMasterMono(bool mono) | 
|  | 3031 | { | 
|  | 3032 | if (mMasterMono == mono) { | 
|  | 3033 | return NO_ERROR; | 
|  | 3034 | } | 
|  | 3035 | mMasterMono = mono; | 
|  | 3036 | // if enabling mono we close all offloaded devices, which will invalidate the | 
|  | 3037 | // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible | 
|  | 3038 | // for recreating the new AudioTrack as non-offloaded PCM. | 
|  | 3039 | // | 
|  | 3040 | // If disabling mono, we leave all tracks as is: we don't know which clients | 
|  | 3041 | // and tracks are able to be recreated as offloaded. The next "song" should | 
|  | 3042 | // play back offloaded. | 
|  | 3043 | if (mMasterMono) { | 
|  | 3044 | Vector<audio_io_handle_t> offloaded; | 
|  | 3045 | for (size_t i = 0; i < mOutputs.size(); ++i) { | 
|  | 3046 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
|  | 3047 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { | 
|  | 3048 | offloaded.push(desc->mIoHandle); | 
|  | 3049 | } | 
|  | 3050 | } | 
|  | 3051 | for (size_t i = 0; i < offloaded.size(); ++i) { | 
|  | 3052 | closeOutput(offloaded[i]); | 
|  | 3053 | } | 
|  | 3054 | } | 
|  | 3055 | // update master mono for all remaining outputs | 
|  | 3056 | for (size_t i = 0; i < mOutputs.size(); ++i) { | 
|  | 3057 | updateMono(mOutputs.keyAt(i)); | 
|  | 3058 | } | 
|  | 3059 | return NO_ERROR; | 
|  | 3060 | } | 
|  | 3061 |  | 
|  | 3062 | status_t AudioPolicyManager::getMasterMono(bool *mono) | 
|  | 3063 | { | 
|  | 3064 | *mono = mMasterMono; | 
|  | 3065 | return NO_ERROR; | 
|  | 3066 | } | 
|  | 3067 |  | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3068 | status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc) | 
|  | 3069 | { | 
|  | 3070 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle()); | 
|  | 3071 |  | 
|  | 3072 | sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle); | 
|  | 3073 | if (patchDesc == 0) { | 
|  | 3074 | ALOGW("%s source has no patch with handle %d", __FUNCTION__, | 
|  | 3075 | sourceDesc->mPatchDesc->mHandle); | 
|  | 3076 | return BAD_VALUE; | 
|  | 3077 | } | 
|  | 3078 | removeAudioPatch(sourceDesc->mPatchDesc->mHandle); | 
|  | 3079 |  | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 3080 | audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes); | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3081 | sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote(); | 
|  | 3082 | if (swOutputDesc != 0) { | 
|  | 3083 | stopSource(swOutputDesc, stream, false); | 
|  | 3084 | mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); | 
|  | 3085 | } else { | 
|  | 3086 | sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote(); | 
|  | 3087 | if (hwOutputDesc != 0) { | 
|  | 3088 | //   release patch between src device and output device | 
|  | 3089 | //   close Hwoutput and remove from mHwOutputs | 
|  | 3090 | } else { | 
|  | 3091 | ALOGW("%s source has neither SW nor HW output", __FUNCTION__); | 
|  | 3092 | } | 
|  | 3093 | } | 
|  | 3094 | return NO_ERROR; | 
|  | 3095 | } | 
|  | 3096 |  | 
|  | 3097 | sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput( | 
|  | 3098 | audio_io_handle_t output, routing_strategy strategy) | 
|  | 3099 | { | 
|  | 3100 | sp<AudioSourceDescriptor> source; | 
|  | 3101 | for (size_t i = 0; i < mAudioSources.size(); i++)  { | 
|  | 3102 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); | 
|  | 3103 | routing_strategy sourceStrategy = | 
|  | 3104 | (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes); | 
|  | 3105 | sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote(); | 
|  | 3106 | if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) { | 
|  | 3107 | source = sourceDesc; | 
|  | 3108 | break; | 
|  | 3109 | } | 
|  | 3110 | } | 
|  | 3111 | return source; | 
| Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3112 | } | 
|  | 3113 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3114 | // ---------------------------------------------------------------------------- | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3115 | // AudioPolicyManager | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3116 | // ---------------------------------------------------------------------------- | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3117 | uint32_t AudioPolicyManager::nextAudioPortGeneration() | 
|  | 3118 | { | 
|  | 3119 | return android_atomic_inc(&mAudioPortGeneration); | 
|  | 3120 | } | 
|  | 3121 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3122 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3123 | : | 
|  | 3124 | #ifdef AUDIO_POLICY_TEST | 
|  | 3125 | Thread(false), | 
|  | 3126 | #endif //AUDIO_POLICY_TEST | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3127 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3128 | mA2dpSuspended(false), | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 3129 | mAudioPortGeneration(1), | 
|  | 3130 | mBeaconMuteRefCount(0), | 
|  | 3131 | mBeaconPlayingRefCount(0), | 
| Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3132 | mBeaconMuted(false), | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3133 | mTtsOutputAvailable(false), | 
|  | 3134 | mMasterMono(false) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3135 | { | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3136 | mUidCached = getuid(); | 
|  | 3137 | mpClientInterface = clientInterface; | 
|  | 3138 |  | 
|  | 3139 | // TODO: remove when legacy conf file is removed. true on devices that use DRC on the | 
|  | 3140 | // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly. | 
|  | 3141 | // Note: remove also speaker_drc_enabled from global configuration of XML config file. | 
|  | 3142 | bool speakerDrcEnabled = false; | 
|  | 3143 |  | 
|  | 3144 | #ifdef USE_XML_AUDIO_POLICY_CONF | 
|  | 3145 | mVolumeCurves = new VolumeCurvesCollection(); | 
|  | 3146 | AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices, | 
|  | 3147 | mDefaultOutputDevice, speakerDrcEnabled, | 
|  | 3148 | static_cast<VolumeCurvesCollection *>(mVolumeCurves)); | 
|  | 3149 | PolicySerializer serializer; | 
|  | 3150 | if (serializer.deserialize(AUDIO_POLICY_XML_CONFIG_FILE, config) != NO_ERROR) { | 
|  | 3151 | #else | 
|  | 3152 | mVolumeCurves = new StreamDescriptorCollection(); | 
|  | 3153 | AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices, | 
|  | 3154 | mDefaultOutputDevice, speakerDrcEnabled); | 
|  | 3155 | if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, config) != NO_ERROR) && | 
|  | 3156 | (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, config) != NO_ERROR)) { | 
|  | 3157 | #endif | 
|  | 3158 | ALOGE("could not load audio policy configuration file, setting defaults"); | 
|  | 3159 | config.setDefault(); | 
|  | 3160 | } | 
|  | 3161 | // must be done after reading the policy (since conditionned by Speaker Drc Enabling) | 
|  | 3162 | mVolumeCurves->initializeVolumeCurves(speakerDrcEnabled); | 
|  | 3163 |  | 
|  | 3164 | // 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] | 3165 | audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance(); | 
|  | 3166 | if (!engineInstance) { | 
|  | 3167 | ALOGE("%s:  Could not get an instance of policy engine", __FUNCTION__); | 
|  | 3168 | return; | 
|  | 3169 | } | 
|  | 3170 | // Retrieve the Policy Manager Interface | 
|  | 3171 | mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>(); | 
|  | 3172 | if (mEngine == NULL) { | 
|  | 3173 | ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__); | 
|  | 3174 | return; | 
|  | 3175 | } | 
|  | 3176 | mEngine->setObserver(this); | 
|  | 3177 | status_t status = mEngine->initCheck(); | 
|  | 3178 | ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status); | 
|  | 3179 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3180 | // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3181 | // open all output streams needed to access attached devices | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3182 | audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types(); | 
|  | 3183 | audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3184 | for (size_t i = 0; i < mHwModules.size(); i++) { | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3185 | mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->getName()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3186 | if (mHwModules[i]->mHandle == 0) { | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3187 | ALOGW("could not open HW module %s", mHwModules[i]->getName()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3188 | continue; | 
|  | 3189 | } | 
|  | 3190 | // open all output streams needed to access attached devices | 
|  | 3191 | // except for direct output streams that are only opened when they are actually | 
|  | 3192 | // required by an app. | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3193 | // This also validates mAvailableOutputDevices list | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3194 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) | 
|  | 3195 | { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3196 | const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j]; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3197 |  | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3198 | if (!outProfile->hasSupportedDevices()) { | 
|  | 3199 | ALOGW("Output profile contains no device on module %s", mHwModules[i]->getName()); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3200 | continue; | 
|  | 3201 | } | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3202 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) { | 
| Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3203 | mTtsOutputAvailable = true; | 
|  | 3204 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3205 |  | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3206 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3207 | continue; | 
|  | 3208 | } | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3209 | audio_devices_t profileType = outProfile->getSupportedDevicesType(); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3210 | if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) { | 
|  | 3211 | profileType = mDefaultOutputDevice->type(); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3212 | } else { | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3213 | // chose first device present in profile's SupportedDevices also part of | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3214 | // outputDeviceTypes | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3215 | profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3216 | } | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3217 | if ((profileType & outputDeviceTypes) == 0) { | 
|  | 3218 | continue; | 
|  | 3219 | } | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3220 | sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile, | 
|  | 3221 | mpClientInterface); | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3222 | const DeviceVector &supportedDevices = outProfile->getSupportedDevices(); | 
|  | 3223 | const DeviceVector &devicesForType = supportedDevices.getDevicesFromType(profileType); | 
|  | 3224 | String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress | 
|  | 3225 | : String8(""); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3226 |  | 
|  | 3227 | outputDesc->mDevice = profileType; | 
|  | 3228 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
|  | 3229 | config.sample_rate = outputDesc->mSamplingRate; | 
|  | 3230 | config.channel_mask = outputDesc->mChannelMask; | 
|  | 3231 | config.format = outputDesc->mFormat; | 
|  | 3232 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3233 | status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(), | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3234 | &output, | 
|  | 3235 | &config, | 
|  | 3236 | &outputDesc->mDevice, | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3237 | address, | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3238 | &outputDesc->mLatency, | 
|  | 3239 | outputDesc->mFlags); | 
|  | 3240 |  | 
|  | 3241 | if (status != NO_ERROR) { | 
|  | 3242 | ALOGW("Cannot open output stream for device %08x on hw module %s", | 
|  | 3243 | outputDesc->mDevice, | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3244 | mHwModules[i]->getName()); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3245 | } else { | 
|  | 3246 | outputDesc->mSamplingRate = config.sample_rate; | 
|  | 3247 | outputDesc->mChannelMask = config.channel_mask; | 
|  | 3248 | outputDesc->mFormat = config.format; | 
|  | 3249 |  | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3250 | for (size_t k = 0; k  < supportedDevices.size(); k++) { | 
|  | 3251 | ssize_t index = mAvailableOutputDevices.indexOf(supportedDevices[k]); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3252 | // 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] | 3253 | if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) { | 
|  | 3254 | mAvailableOutputDevices[index]->attach(mHwModules[i]); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3255 | } | 
|  | 3256 | } | 
|  | 3257 | if (mPrimaryOutput == 0 && | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3258 | outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3259 | mPrimaryOutput = outputDesc; | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3260 | } | 
|  | 3261 | addOutput(output, outputDesc); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3262 | setOutputDevice(outputDesc, | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3263 | outputDesc->mDevice, | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3264 | true, | 
|  | 3265 | 0, | 
|  | 3266 | NULL, | 
|  | 3267 | address.string()); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3268 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3269 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3270 | // open input streams needed to access attached devices to validate | 
|  | 3271 | // mAvailableInputDevices list | 
|  | 3272 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) | 
|  | 3273 | { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3274 | const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j]; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3275 |  | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3276 | if (!inProfile->hasSupportedDevices()) { | 
|  | 3277 | ALOGW("Input profile contains no device on module %s", mHwModules[i]->getName()); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3278 | continue; | 
|  | 3279 | } | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3280 | // chose first device present in profile's SupportedDevices also part of | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3281 | // inputDeviceTypes | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3282 | audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes); | 
|  | 3283 |  | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3284 | if ((profileType & inputDeviceTypes) == 0) { | 
|  | 3285 | continue; | 
|  | 3286 | } | 
| Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 3287 | sp<AudioInputDescriptor> inputDesc = | 
|  | 3288 | new AudioInputDescriptor(inProfile); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3289 |  | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3290 | inputDesc->mDevice = profileType; | 
|  | 3291 |  | 
| Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 3292 | // find the address | 
|  | 3293 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType); | 
|  | 3294 | //   the inputs vector must be of size 1, but we don't want to crash here | 
|  | 3295 | String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress | 
|  | 3296 | : String8(""); | 
|  | 3297 | ALOGV("  for input device 0x%x using address %s", profileType, address.string()); | 
|  | 3298 | ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!"); | 
|  | 3299 |  | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3300 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
|  | 3301 | config.sample_rate = inputDesc->mSamplingRate; | 
|  | 3302 | config.channel_mask = inputDesc->mChannelMask; | 
|  | 3303 | config.format = inputDesc->mFormat; | 
|  | 3304 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3305 | status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(), | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3306 | &input, | 
|  | 3307 | &config, | 
|  | 3308 | &inputDesc->mDevice, | 
| Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 3309 | address, | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3310 | AUDIO_SOURCE_MIC, | 
|  | 3311 | AUDIO_INPUT_FLAG_NONE); | 
|  | 3312 |  | 
|  | 3313 | if (status == NO_ERROR) { | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3314 | const DeviceVector &supportedDevices = inProfile->getSupportedDevices(); | 
|  | 3315 | for (size_t k = 0; k  < supportedDevices.size(); k++) { | 
|  | 3316 | ssize_t index =  mAvailableInputDevices.indexOf(supportedDevices[k]); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3317 | // 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] | 3318 | if (index >= 0) { | 
|  | 3319 | sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index]; | 
|  | 3320 | if (!devDesc->isAttached()) { | 
|  | 3321 | devDesc->attach(mHwModules[i]); | 
|  | 3322 | devDesc->importAudioPort(inProfile); | 
|  | 3323 | } | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3324 | } | 
|  | 3325 | } | 
|  | 3326 | mpClientInterface->closeInput(input); | 
|  | 3327 | } else { | 
|  | 3328 | ALOGW("Cannot open input stream for device %08x on hw module %s", | 
|  | 3329 | inputDesc->mDevice, | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3330 | mHwModules[i]->getName()); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3331 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3332 | } | 
|  | 3333 | } | 
|  | 3334 | // make sure all attached devices have been allocated a unique ID | 
|  | 3335 | for (size_t i = 0; i  < mAvailableOutputDevices.size();) { | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 3336 | if (!mAvailableOutputDevices[i]->isAttached()) { | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3337 | ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type()); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3338 | mAvailableOutputDevices.remove(mAvailableOutputDevices[i]); | 
|  | 3339 | continue; | 
|  | 3340 | } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3341 | // The device is now validated and can be appended to the available devices of the engine | 
|  | 3342 | mEngine->setDeviceConnectionState(mAvailableOutputDevices[i], | 
|  | 3343 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3344 | i++; | 
|  | 3345 | } | 
|  | 3346 | for (size_t i = 0; i  < mAvailableInputDevices.size();) { | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 3347 | if (!mAvailableInputDevices[i]->isAttached()) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3348 | ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type()); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3349 | mAvailableInputDevices.remove(mAvailableInputDevices[i]); | 
|  | 3350 | continue; | 
|  | 3351 | } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3352 | // The device is now validated and can be appended to the available devices of the engine | 
|  | 3353 | mEngine->setDeviceConnectionState(mAvailableInputDevices[i], | 
|  | 3354 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3355 | i++; | 
|  | 3356 | } | 
|  | 3357 | // make sure default device is reachable | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3358 | if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3359 | ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type()); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3360 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3361 |  | 
|  | 3362 | ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output"); | 
|  | 3363 |  | 
|  | 3364 | updateDevicesAndOutputs(); | 
|  | 3365 |  | 
|  | 3366 | #ifdef AUDIO_POLICY_TEST | 
|  | 3367 | if (mPrimaryOutput != 0) { | 
|  | 3368 | AudioParameter outputCmd = AudioParameter(); | 
|  | 3369 | outputCmd.addInt(String8("set_id"), 0); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3370 | mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3371 |  | 
|  | 3372 | mTestDevice = AUDIO_DEVICE_OUT_SPEAKER; | 
|  | 3373 | mTestSamplingRate = 44100; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3374 | mTestFormat = AUDIO_FORMAT_PCM_16_BIT; | 
|  | 3375 | mTestChannels =  AUDIO_CHANNEL_OUT_STEREO; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3376 | mTestLatencyMs = 0; | 
|  | 3377 | mCurOutput = 0; | 
|  | 3378 | mDirectOutput = false; | 
|  | 3379 | for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { | 
|  | 3380 | mTestOutputs[i] = 0; | 
|  | 3381 | } | 
|  | 3382 |  | 
|  | 3383 | const size_t SIZE = 256; | 
|  | 3384 | char buffer[SIZE]; | 
|  | 3385 | snprintf(buffer, SIZE, "AudioPolicyManagerTest"); | 
|  | 3386 | run(buffer, ANDROID_PRIORITY_AUDIO); | 
|  | 3387 | } | 
|  | 3388 | #endif //AUDIO_POLICY_TEST | 
|  | 3389 | } | 
|  | 3390 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3391 | AudioPolicyManager::~AudioPolicyManager() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3392 | { | 
|  | 3393 | #ifdef AUDIO_POLICY_TEST | 
|  | 3394 | exit(); | 
|  | 3395 | #endif //AUDIO_POLICY_TEST | 
|  | 3396 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 3397 | mpClientInterface->closeOutput(mOutputs.keyAt(i)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3398 | } | 
|  | 3399 | for (size_t i = 0; i < mInputs.size(); i++) { | 
|  | 3400 | mpClientInterface->closeInput(mInputs.keyAt(i)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3401 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3402 | mAvailableOutputDevices.clear(); | 
|  | 3403 | mAvailableInputDevices.clear(); | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3404 | mOutputs.clear(); | 
|  | 3405 | mInputs.clear(); | 
|  | 3406 | mHwModules.clear(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3407 | } | 
|  | 3408 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3409 | status_t AudioPolicyManager::initCheck() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3410 | { | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 3411 | return hasPrimaryOutput() ? NO_ERROR : NO_INIT; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3412 | } | 
|  | 3413 |  | 
|  | 3414 | #ifdef AUDIO_POLICY_TEST | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3415 | bool AudioPolicyManager::threadLoop() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3416 | { | 
|  | 3417 | ALOGV("entering threadLoop()"); | 
|  | 3418 | while (!exitPending()) | 
|  | 3419 | { | 
|  | 3420 | String8 command; | 
|  | 3421 | int valueInt; | 
|  | 3422 | String8 value; | 
|  | 3423 |  | 
|  | 3424 | Mutex::Autolock _l(mLock); | 
|  | 3425 | mWaitWorkCV.waitRelative(mLock, milliseconds(50)); | 
|  | 3426 |  | 
|  | 3427 | command = mpClientInterface->getParameters(0, String8("test_cmd_policy")); | 
|  | 3428 | AudioParameter param = AudioParameter(command); | 
|  | 3429 |  | 
|  | 3430 | if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR && | 
|  | 3431 | valueInt != 0) { | 
|  | 3432 | ALOGV("Test command %s received", command.string()); | 
|  | 3433 | String8 target; | 
|  | 3434 | if (param.get(String8("target"), target) != NO_ERROR) { | 
|  | 3435 | target = "Manager"; | 
|  | 3436 | } | 
|  | 3437 | if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) { | 
|  | 3438 | param.remove(String8("test_cmd_policy_output")); | 
|  | 3439 | mCurOutput = valueInt; | 
|  | 3440 | } | 
|  | 3441 | if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) { | 
|  | 3442 | param.remove(String8("test_cmd_policy_direct")); | 
|  | 3443 | if (value == "false") { | 
|  | 3444 | mDirectOutput = false; | 
|  | 3445 | } else if (value == "true") { | 
|  | 3446 | mDirectOutput = true; | 
|  | 3447 | } | 
|  | 3448 | } | 
|  | 3449 | if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) { | 
|  | 3450 | param.remove(String8("test_cmd_policy_input")); | 
|  | 3451 | mTestInput = valueInt; | 
|  | 3452 | } | 
|  | 3453 |  | 
|  | 3454 | if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) { | 
|  | 3455 | param.remove(String8("test_cmd_policy_format")); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3456 | int format = AUDIO_FORMAT_INVALID; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3457 | if (value == "PCM 16 bits") { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3458 | format = AUDIO_FORMAT_PCM_16_BIT; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3459 | } else if (value == "PCM 8 bits") { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3460 | format = AUDIO_FORMAT_PCM_8_BIT; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3461 | } else if (value == "Compressed MP3") { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3462 | format = AUDIO_FORMAT_MP3; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3463 | } | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3464 | if (format != AUDIO_FORMAT_INVALID) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3465 | if (target == "Manager") { | 
|  | 3466 | mTestFormat = format; | 
|  | 3467 | } else if (mTestOutputs[mCurOutput] != 0) { | 
|  | 3468 | AudioParameter outputParam = AudioParameter(); | 
|  | 3469 | outputParam.addInt(String8("format"), format); | 
|  | 3470 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); | 
|  | 3471 | } | 
|  | 3472 | } | 
|  | 3473 | } | 
|  | 3474 | if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) { | 
|  | 3475 | param.remove(String8("test_cmd_policy_channels")); | 
|  | 3476 | int channels = 0; | 
|  | 3477 |  | 
|  | 3478 | if (value == "Channels Stereo") { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3479 | channels =  AUDIO_CHANNEL_OUT_STEREO; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3480 | } else if (value == "Channels Mono") { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3481 | channels =  AUDIO_CHANNEL_OUT_MONO; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3482 | } | 
|  | 3483 | if (channels != 0) { | 
|  | 3484 | if (target == "Manager") { | 
|  | 3485 | mTestChannels = channels; | 
|  | 3486 | } else if (mTestOutputs[mCurOutput] != 0) { | 
|  | 3487 | AudioParameter outputParam = AudioParameter(); | 
|  | 3488 | outputParam.addInt(String8("channels"), channels); | 
|  | 3489 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); | 
|  | 3490 | } | 
|  | 3491 | } | 
|  | 3492 | } | 
|  | 3493 | if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) { | 
|  | 3494 | param.remove(String8("test_cmd_policy_sampleRate")); | 
|  | 3495 | if (valueInt >= 0 && valueInt <= 96000) { | 
|  | 3496 | int samplingRate = valueInt; | 
|  | 3497 | if (target == "Manager") { | 
|  | 3498 | mTestSamplingRate = samplingRate; | 
|  | 3499 | } else if (mTestOutputs[mCurOutput] != 0) { | 
|  | 3500 | AudioParameter outputParam = AudioParameter(); | 
|  | 3501 | outputParam.addInt(String8("sampling_rate"), samplingRate); | 
|  | 3502 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); | 
|  | 3503 | } | 
|  | 3504 | } | 
|  | 3505 | } | 
|  | 3506 |  | 
|  | 3507 | if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) { | 
|  | 3508 | param.remove(String8("test_cmd_policy_reopen")); | 
|  | 3509 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3510 | mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput);); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3511 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3512 | audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3513 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3514 | removeOutput(mPrimaryOutput->mIoHandle); | 
|  | 3515 | sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL, | 
|  | 3516 | mpClientInterface); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3517 | outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3518 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
|  | 3519 | config.sample_rate = outputDesc->mSamplingRate; | 
|  | 3520 | config.channel_mask = outputDesc->mChannelMask; | 
|  | 3521 | config.format = outputDesc->mFormat; | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3522 | audio_io_handle_t handle; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3523 | status_t status = mpClientInterface->openOutput(moduleHandle, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3524 | &handle, | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3525 | &config, | 
|  | 3526 | &outputDesc->mDevice, | 
|  | 3527 | String8(""), | 
|  | 3528 | &outputDesc->mLatency, | 
|  | 3529 | outputDesc->mFlags); | 
|  | 3530 | if (status != NO_ERROR) { | 
|  | 3531 | ALOGE("Failed to reopen hardware output stream, " | 
|  | 3532 | "samplingRate: %d, format %d, channels %d", | 
|  | 3533 | outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3534 | } else { | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3535 | outputDesc->mSamplingRate = config.sample_rate; | 
|  | 3536 | outputDesc->mChannelMask = config.channel_mask; | 
|  | 3537 | outputDesc->mFormat = config.format; | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3538 | mPrimaryOutput = outputDesc; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3539 | AudioParameter outputCmd = AudioParameter(); | 
|  | 3540 | outputCmd.addInt(String8("set_id"), 0); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3541 | mpClientInterface->setParameters(handle, outputCmd.toString()); | 
|  | 3542 | addOutput(handle, outputDesc); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3543 | } | 
|  | 3544 | } | 
|  | 3545 |  | 
|  | 3546 |  | 
|  | 3547 | mpClientInterface->setParameters(0, String8("test_cmd_policy=")); | 
|  | 3548 | } | 
|  | 3549 | } | 
|  | 3550 | return false; | 
|  | 3551 | } | 
|  | 3552 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3553 | void AudioPolicyManager::exit() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3554 | { | 
|  | 3555 | { | 
|  | 3556 | AutoMutex _l(mLock); | 
|  | 3557 | requestExit(); | 
|  | 3558 | mWaitWorkCV.signal(); | 
|  | 3559 | } | 
|  | 3560 | requestExitAndWait(); | 
|  | 3561 | } | 
|  | 3562 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3563 | int AudioPolicyManager::testOutputIndex(audio_io_handle_t output) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3564 | { | 
|  | 3565 | for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { | 
|  | 3566 | if (output == mTestOutputs[i]) return i; | 
|  | 3567 | } | 
|  | 3568 | return 0; | 
|  | 3569 | } | 
|  | 3570 | #endif //AUDIO_POLICY_TEST | 
|  | 3571 |  | 
|  | 3572 | // --- | 
|  | 3573 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3574 | void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3575 | { | 
| François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 3576 | outputDesc->setIoHandle(output); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3577 | mOutputs.add(output, outputDesc); | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3578 | updateMono(output); // update mono status when adding to output list | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3579 | nextAudioPortGeneration(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3580 | } | 
|  | 3581 |  | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3582 | void AudioPolicyManager::removeOutput(audio_io_handle_t output) | 
|  | 3583 | { | 
|  | 3584 | mOutputs.removeItem(output); | 
|  | 3585 | } | 
|  | 3586 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3587 | void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc) | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3588 | { | 
| François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 3589 | inputDesc->setIoHandle(input); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3590 | mInputs.add(input, inputDesc); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3591 | nextAudioPortGeneration(); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3592 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3593 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3594 | void AudioPolicyManager::findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/, | 
| keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3595 | const audio_devices_t device /*in*/, | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3596 | const String8 address /*in*/, | 
|  | 3597 | SortedVector<audio_io_handle_t>& outputs /*out*/) { | 
| keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3598 | sp<DeviceDescriptor> devDesc = | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3599 | desc->mProfile->getSupportedDeviceByAddress(device, address); | 
| keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3600 | if (devDesc != 0) { | 
|  | 3601 | ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s", | 
|  | 3602 | desc->mIoHandle, address.string()); | 
|  | 3603 | outputs.add(desc->mIoHandle); | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3604 | } | 
|  | 3605 | } | 
|  | 3606 |  | 
| Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3607 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3608 | audio_policy_dev_state_t state, | 
|  | 3609 | SortedVector<audio_io_handle_t>& outputs, | 
|  | 3610 | const String8 address) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3611 | { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3612 | audio_devices_t device = devDesc->type(); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3613 | sp<SwAudioOutputDescriptor> desc; | 
| Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 3614 |  | 
|  | 3615 | if (audio_device_is_digital(device)) { | 
|  | 3616 | // erase all current sample rates, formats and channel masks | 
| Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 3617 | devDesc->clearAudioProfiles(); | 
| Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 3618 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3619 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3620 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3621 | // first list already open outputs that can be routed to this device | 
|  | 3622 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 3623 | desc = mOutputs.valueAt(i); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3624 | if (!desc->isDuplicated() && (desc->supportedDevices() & device)) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3625 | if (!device_distinguishes_on_address(device)) { | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3626 | ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i)); | 
|  | 3627 | outputs.add(mOutputs.keyAt(i)); | 
|  | 3628 | } else { | 
|  | 3629 | ALOGV("  checking address match due to device 0x%x", device); | 
| keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3630 | findIoHandlesByAddress(desc, device, address, outputs); | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3631 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3632 | } | 
|  | 3633 | } | 
|  | 3634 | // then look for output profiles that can be routed to this device | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3635 | SortedVector< sp<IOProfile> > profiles; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3636 | for (size_t i = 0; i < mHwModules.size(); i++) | 
|  | 3637 | { | 
|  | 3638 | if (mHwModules[i]->mHandle == 0) { | 
|  | 3639 | continue; | 
|  | 3640 | } | 
|  | 3641 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) | 
|  | 3642 | { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3643 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3644 | if (profile->supportDevice(device)) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3645 | if (!device_distinguishes_on_address(device) || | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3646 | profile->supportDeviceAddress(address)) { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3647 | profiles.add(profile); | 
|  | 3648 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i); | 
|  | 3649 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3650 | } | 
|  | 3651 | } | 
|  | 3652 | } | 
|  | 3653 |  | 
| Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 3654 | ALOGV("  found %zu profiles, %zu outputs", profiles.size(), outputs.size()); | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3655 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3656 | if (profiles.isEmpty() && outputs.isEmpty()) { | 
|  | 3657 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); | 
|  | 3658 | return BAD_VALUE; | 
|  | 3659 | } | 
|  | 3660 |  | 
|  | 3661 | // open outputs for matching profiles if needed. Direct outputs are also opened to | 
|  | 3662 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() | 
|  | 3663 | 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] | 3664 | sp<IOProfile> profile = profiles[profile_index]; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3665 |  | 
|  | 3666 | // nothing to do if one output is already opened for this profile | 
|  | 3667 | size_t j; | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3668 | for (j = 0; j < outputs.size(); j++) { | 
|  | 3669 | desc = mOutputs.valueFor(outputs.itemAt(j)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3670 | if (!desc->isDuplicated() && desc->mProfile == profile) { | 
| Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3671 | // matching profile: save the sample rates, format and channel masks supported | 
|  | 3672 | // by the profile in our device descriptor | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3673 | if (audio_device_is_digital(device)) { | 
|  | 3674 | devDesc->importAudioPort(profile); | 
|  | 3675 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3676 | break; | 
|  | 3677 | } | 
|  | 3678 | } | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3679 | if (j != outputs.size()) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3680 | continue; | 
|  | 3681 | } | 
|  | 3682 |  | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3683 | ALOGV("opening output for device %08x with params %s profile %p", | 
|  | 3684 | device, address.string(), profile.get()); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3685 | desc = new SwAudioOutputDescriptor(profile, mpClientInterface); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3686 | desc->mDevice = device; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3687 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
|  | 3688 | config.sample_rate = desc->mSamplingRate; | 
|  | 3689 | config.channel_mask = desc->mChannelMask; | 
|  | 3690 | config.format = desc->mFormat; | 
|  | 3691 | config.offload_info.sample_rate = desc->mSamplingRate; | 
|  | 3692 | config.offload_info.channel_mask = desc->mChannelMask; | 
|  | 3693 | config.offload_info.format = desc->mFormat; | 
|  | 3694 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3695 | status_t status = mpClientInterface->openOutput(profile->getModuleHandle(), | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3696 | &output, | 
|  | 3697 | &config, | 
|  | 3698 | &desc->mDevice, | 
|  | 3699 | address, | 
|  | 3700 | &desc->mLatency, | 
|  | 3701 | desc->mFlags); | 
|  | 3702 | if (status == NO_ERROR) { | 
|  | 3703 | desc->mSamplingRate = config.sample_rate; | 
|  | 3704 | desc->mChannelMask = config.channel_mask; | 
|  | 3705 | desc->mFormat = config.format; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3706 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3707 | // Here is where the out_set_parameters() for card & device gets called | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3708 | if (!address.isEmpty()) { | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3709 | char *param = audio_device_address_to_parameter(device, address); | 
|  | 3710 | mpClientInterface->setParameters(output, String8(param)); | 
|  | 3711 | free(param); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3712 | } | 
| Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 3713 | updateAudioProfiles(device, output, profile->getAudioProfiles()); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3714 | if (!profile->hasValidAudioProfile()) { | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3715 | ALOGW("checkOutputsForDevice() missing param"); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3716 | mpClientInterface->closeOutput(output); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3717 | output = AUDIO_IO_HANDLE_NONE; | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3718 | } else if (profile->hasDynamicAudioProfile()) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3719 | mpClientInterface->closeOutput(output); | 
| Phil Burk | 702b105 | 2016-03-02 16:38:26 -0800 | [diff] [blame] | 3720 | output = AUDIO_IO_HANDLE_NONE; | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3721 | profile->pickAudioProfile(config.sample_rate, config.channel_mask, config.format); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3722 | config.offload_info.sample_rate = config.sample_rate; | 
|  | 3723 | config.offload_info.channel_mask = config.channel_mask; | 
|  | 3724 | config.offload_info.format = config.format; | 
| Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3725 | status = mpClientInterface->openOutput(profile->getModuleHandle(), | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3726 | &output, | 
|  | 3727 | &config, | 
|  | 3728 | &desc->mDevice, | 
|  | 3729 | address, | 
|  | 3730 | &desc->mLatency, | 
|  | 3731 | desc->mFlags); | 
|  | 3732 | if (status == NO_ERROR) { | 
|  | 3733 | desc->mSamplingRate = config.sample_rate; | 
|  | 3734 | desc->mChannelMask = config.channel_mask; | 
|  | 3735 | desc->mFormat = config.format; | 
|  | 3736 | } else { | 
|  | 3737 | output = AUDIO_IO_HANDLE_NONE; | 
|  | 3738 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3739 | } | 
|  | 3740 |  | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3741 | if (output != AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3742 | addOutput(output, desc); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3743 | if (device_distinguishes_on_address(device) && address != "0") { | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3744 | sp<AudioPolicyMix> policyMix; | 
|  | 3745 | if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3746 | ALOGE("checkOutputsForDevice() cannot find policy for address %s", | 
|  | 3747 | address.string()); | 
|  | 3748 | } | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3749 | policyMix->setOutput(desc); | 
| Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 3750 | desc->mPolicyMix = policyMix->getMix(); | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3751 |  | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 3752 | } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && | 
|  | 3753 | hasPrimaryOutput()) { | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 3754 | // no duplicated output for direct outputs and | 
|  | 3755 | // outputs used by dynamic policy mixes | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3756 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3757 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3758 | // set initial stream volume for device | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3759 | applyStreamVolumes(desc, device, 0, true); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3760 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3761 | //TODO: configure audio effect output stage here | 
|  | 3762 |  | 
|  | 3763 | // 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] | 3764 | duplicatedOutput = | 
|  | 3765 | mpClientInterface->openDuplicateOutput(output, | 
|  | 3766 | mPrimaryOutput->mIoHandle); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3767 | if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3768 | // add duplicated output descriptor | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3769 | sp<SwAudioOutputDescriptor> dupOutputDesc = | 
|  | 3770 | new SwAudioOutputDescriptor(NULL, mpClientInterface); | 
|  | 3771 | dupOutputDesc->mOutput1 = mPrimaryOutput; | 
|  | 3772 | dupOutputDesc->mOutput2 = desc; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3773 | dupOutputDesc->mSamplingRate = desc->mSamplingRate; | 
|  | 3774 | dupOutputDesc->mFormat = desc->mFormat; | 
|  | 3775 | dupOutputDesc->mChannelMask = desc->mChannelMask; | 
|  | 3776 | dupOutputDesc->mLatency = desc->mLatency; | 
|  | 3777 | addOutput(duplicatedOutput, dupOutputDesc); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3778 | applyStreamVolumes(dupOutputDesc, device, 0, true); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3779 | } else { | 
|  | 3780 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3781 | mPrimaryOutput->mIoHandle, output); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3782 | mpClientInterface->closeOutput(output); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3783 | removeOutput(output); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3784 | nextAudioPortGeneration(); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3785 | output = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3786 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3787 | } | 
|  | 3788 | } | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3789 | } else { | 
|  | 3790 | output = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3791 | } | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3792 | if (output == AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3793 | ALOGW("checkOutputsForDevice() could not open output for device %x", device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3794 | profiles.removeAt(profile_index); | 
|  | 3795 | profile_index--; | 
|  | 3796 | } else { | 
|  | 3797 | outputs.add(output); | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3798 | // Load digital format info only for digital devices | 
|  | 3799 | if (audio_device_is_digital(device)) { | 
|  | 3800 | devDesc->importAudioPort(profile); | 
|  | 3801 | } | 
| Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3802 |  | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3803 | if (device_distinguishes_on_address(device)) { | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3804 | ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)", | 
|  | 3805 | device, address.string()); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3806 | setOutputDevice(desc, device, true/*force*/, 0/*delay*/, | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3807 | NULL/*patch handle*/, address.string()); | 
|  | 3808 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3809 | ALOGV("checkOutputsForDevice(): adding output %d", output); | 
|  | 3810 | } | 
|  | 3811 | } | 
|  | 3812 |  | 
|  | 3813 | if (profiles.isEmpty()) { | 
|  | 3814 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); | 
|  | 3815 | return BAD_VALUE; | 
|  | 3816 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3817 | } else { // Disconnect | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3818 | // check if one opened output is not needed any more after disconnecting one device | 
|  | 3819 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 3820 | desc = mOutputs.valueAt(i); | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3821 | if (!desc->isDuplicated()) { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3822 | // exact match on device | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3823 | if (device_distinguishes_on_address(device) && | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3824 | (desc->supportedDevices() == device)) { | 
| keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3825 | findIoHandlesByAddress(desc, device, address, outputs); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3826 | } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) { | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3827 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", | 
|  | 3828 | mOutputs.keyAt(i)); | 
|  | 3829 | outputs.add(mOutputs.keyAt(i)); | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3830 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3831 | } | 
|  | 3832 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3833 | // Clear any profiles associated with the disconnected device. | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3834 | for (size_t i = 0; i < mHwModules.size(); i++) | 
|  | 3835 | { | 
|  | 3836 | if (mHwModules[i]->mHandle == 0) { | 
|  | 3837 | continue; | 
|  | 3838 | } | 
|  | 3839 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) | 
|  | 3840 | { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3841 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3842 | if (profile->supportDevice(device)) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3843 | ALOGV("checkOutputsForDevice(): " | 
|  | 3844 | "clearing direct output profile %zu on module %zu", j, i); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3845 | profile->clearAudioProfiles(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3846 | } | 
|  | 3847 | } | 
|  | 3848 | } | 
|  | 3849 | } | 
|  | 3850 | return NO_ERROR; | 
|  | 3851 | } | 
|  | 3852 |  | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3853 | status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor> devDesc, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3854 | audio_policy_dev_state_t state, | 
|  | 3855 | SortedVector<audio_io_handle_t>& inputs, | 
|  | 3856 | const String8 address) | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3857 | { | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3858 | audio_devices_t device = devDesc->type(); | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3859 | sp<AudioInputDescriptor> desc; | 
| Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 3860 |  | 
|  | 3861 | if (audio_device_is_digital(device)) { | 
|  | 3862 | // erase all current sample rates, formats and channel masks | 
| Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 3863 | devDesc->clearAudioProfiles(); | 
| Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 3864 | } | 
|  | 3865 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3866 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { | 
|  | 3867 | // first list already open inputs that can be routed to this device | 
|  | 3868 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { | 
|  | 3869 | desc = mInputs.valueAt(input_index); | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3870 | if (desc->mProfile->supportDevice(device)) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3871 | ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index)); | 
|  | 3872 | inputs.add(mInputs.keyAt(input_index)); | 
|  | 3873 | } | 
|  | 3874 | } | 
|  | 3875 |  | 
|  | 3876 | // then look for input profiles that can be routed to this device | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3877 | SortedVector< sp<IOProfile> > profiles; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3878 | for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++) | 
|  | 3879 | { | 
|  | 3880 | if (mHwModules[module_idx]->mHandle == 0) { | 
|  | 3881 | continue; | 
|  | 3882 | } | 
|  | 3883 | for (size_t profile_index = 0; | 
|  | 3884 | profile_index < mHwModules[module_idx]->mInputProfiles.size(); | 
|  | 3885 | profile_index++) | 
|  | 3886 | { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3887 | sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index]; | 
|  | 3888 |  | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3889 | if (profile->supportDevice(device)) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3890 | if (!device_distinguishes_on_address(device) || | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3891 | profile->supportDeviceAddress(address)) { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3892 | profiles.add(profile); | 
|  | 3893 | ALOGV("checkInputsForDevice(): adding profile %zu from module %zu", | 
|  | 3894 | profile_index, module_idx); | 
|  | 3895 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3896 | } | 
|  | 3897 | } | 
|  | 3898 | } | 
|  | 3899 |  | 
|  | 3900 | if (profiles.isEmpty() && inputs.isEmpty()) { | 
|  | 3901 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); | 
|  | 3902 | return BAD_VALUE; | 
|  | 3903 | } | 
|  | 3904 |  | 
|  | 3905 | // open inputs for matching profiles if needed. Direct inputs are also opened to | 
|  | 3906 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() | 
|  | 3907 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { | 
|  | 3908 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3909 | sp<IOProfile> profile = profiles[profile_index]; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3910 | // nothing to do if one input is already opened for this profile | 
|  | 3911 | size_t input_index; | 
|  | 3912 | for (input_index = 0; input_index < mInputs.size(); input_index++) { | 
|  | 3913 | desc = mInputs.valueAt(input_index); | 
|  | 3914 | if (desc->mProfile == profile) { | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3915 | if (audio_device_is_digital(device)) { | 
|  | 3916 | devDesc->importAudioPort(profile); | 
|  | 3917 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3918 | break; | 
|  | 3919 | } | 
|  | 3920 | } | 
|  | 3921 | if (input_index != mInputs.size()) { | 
|  | 3922 | continue; | 
|  | 3923 | } | 
|  | 3924 |  | 
|  | 3925 | ALOGV("opening input for device 0x%X with params %s", device, address.string()); | 
|  | 3926 | desc = new AudioInputDescriptor(profile); | 
|  | 3927 | desc->mDevice = device; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3928 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
|  | 3929 | config.sample_rate = desc->mSamplingRate; | 
|  | 3930 | config.channel_mask = desc->mChannelMask; | 
|  | 3931 | config.format = desc->mFormat; | 
|  | 3932 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3933 | status_t status = mpClientInterface->openInput(profile->getModuleHandle(), | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3934 | &input, | 
|  | 3935 | &config, | 
|  | 3936 | &desc->mDevice, | 
|  | 3937 | address, | 
|  | 3938 | AUDIO_SOURCE_MIC, | 
|  | 3939 | AUDIO_INPUT_FLAG_NONE /*FIXME*/); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3940 |  | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3941 | if (status == NO_ERROR) { | 
|  | 3942 | desc->mSamplingRate = config.sample_rate; | 
|  | 3943 | desc->mChannelMask = config.channel_mask; | 
|  | 3944 | desc->mFormat = config.format; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3945 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3946 | if (!address.isEmpty()) { | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3947 | char *param = audio_device_address_to_parameter(device, address); | 
|  | 3948 | mpClientInterface->setParameters(input, String8(param)); | 
|  | 3949 | free(param); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3950 | } | 
| Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 3951 | updateAudioProfiles(device, input, profile->getAudioProfiles()); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3952 | if (!profile->hasValidAudioProfile()) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3953 | ALOGW("checkInputsForDevice() direct input missing param"); | 
|  | 3954 | mpClientInterface->closeInput(input); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3955 | input = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3956 | } | 
|  | 3957 |  | 
|  | 3958 | if (input != 0) { | 
|  | 3959 | addInput(input, desc); | 
|  | 3960 | } | 
|  | 3961 | } // endif input != 0 | 
|  | 3962 |  | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3963 | if (input == AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3964 | ALOGW("checkInputsForDevice() could not open input for device 0x%X", device); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3965 | profiles.removeAt(profile_index); | 
|  | 3966 | profile_index--; | 
|  | 3967 | } else { | 
|  | 3968 | inputs.add(input); | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3969 | if (audio_device_is_digital(device)) { | 
|  | 3970 | devDesc->importAudioPort(profile); | 
|  | 3971 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3972 | ALOGV("checkInputsForDevice(): adding input %d", input); | 
|  | 3973 | } | 
|  | 3974 | } // end scan profiles | 
|  | 3975 |  | 
|  | 3976 | if (profiles.isEmpty()) { | 
|  | 3977 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); | 
|  | 3978 | return BAD_VALUE; | 
|  | 3979 | } | 
|  | 3980 | } else { | 
|  | 3981 | // Disconnect | 
|  | 3982 | // check if one opened input is not needed any more after disconnecting one device | 
|  | 3983 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { | 
|  | 3984 | desc = mInputs.valueAt(input_index); | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3985 | if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3986 | ALOGV("checkInputsForDevice(): disconnecting adding input %d", | 
|  | 3987 | mInputs.keyAt(input_index)); | 
|  | 3988 | inputs.add(mInputs.keyAt(input_index)); | 
|  | 3989 | } | 
|  | 3990 | } | 
|  | 3991 | // Clear any profiles associated with the disconnected device. | 
|  | 3992 | for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) { | 
|  | 3993 | if (mHwModules[module_index]->mHandle == 0) { | 
|  | 3994 | continue; | 
|  | 3995 | } | 
|  | 3996 | for (size_t profile_index = 0; | 
|  | 3997 | profile_index < mHwModules[module_index]->mInputProfiles.size(); | 
|  | 3998 | profile_index++) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3999 | sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index]; | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4000 | if (profile->supportDevice(device)) { | 
| Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 4001 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu", | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4002 | profile_index, module_index); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4003 | profile->clearAudioProfiles(); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4004 | } | 
|  | 4005 | } | 
|  | 4006 | } | 
|  | 4007 | } // end disconnect | 
|  | 4008 |  | 
|  | 4009 | return NO_ERROR; | 
|  | 4010 | } | 
|  | 4011 |  | 
|  | 4012 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4013 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4014 | { | 
|  | 4015 | ALOGV("closeOutput(%d)", output); | 
|  | 4016 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4017 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4018 | if (outputDesc == NULL) { | 
|  | 4019 | ALOGW("closeOutput() unknown output %d", output); | 
|  | 4020 | return; | 
|  | 4021 | } | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4022 | mPolicyMixes.closeOutput(outputDesc); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4023 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4024 | // look for duplicated outputs connected to the output being removed. | 
|  | 4025 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4026 | sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4027 | if (dupOutputDesc->isDuplicated() && | 
|  | 4028 | (dupOutputDesc->mOutput1 == outputDesc || | 
|  | 4029 | dupOutputDesc->mOutput2 == outputDesc)) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4030 | sp<AudioOutputDescriptor> outputDesc2; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4031 | if (dupOutputDesc->mOutput1 == outputDesc) { | 
|  | 4032 | outputDesc2 = dupOutputDesc->mOutput2; | 
|  | 4033 | } else { | 
|  | 4034 | outputDesc2 = dupOutputDesc->mOutput1; | 
|  | 4035 | } | 
|  | 4036 | // As all active tracks on duplicated output will be deleted, | 
|  | 4037 | // and as they were also referenced on the other output, the reference | 
|  | 4038 | // count for their stream type must be adjusted accordingly on | 
|  | 4039 | // the other output. | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4040 | for (int j = 0; j < AUDIO_STREAM_CNT; j++) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4041 | int refCount = dupOutputDesc->mRefCount[j]; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4042 | outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4043 | } | 
|  | 4044 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); | 
|  | 4045 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); | 
|  | 4046 |  | 
|  | 4047 | mpClientInterface->closeOutput(duplicatedOutput); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4048 | removeOutput(duplicatedOutput); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4049 | } | 
|  | 4050 | } | 
|  | 4051 |  | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4052 | nextAudioPortGeneration(); | 
|  | 4053 |  | 
| Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4054 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4055 | if (index >= 0) { | 
|  | 4056 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
|  | 4057 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); | 
|  | 4058 | mAudioPatches.removeItemsAt(index); | 
|  | 4059 | mpClientInterface->onAudioPatchListUpdate(); | 
|  | 4060 | } | 
|  | 4061 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4062 | AudioParameter param; | 
|  | 4063 | param.add(String8("closing"), String8("true")); | 
|  | 4064 | mpClientInterface->setParameters(output, param.toString()); | 
|  | 4065 |  | 
|  | 4066 | mpClientInterface->closeOutput(output); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4067 | removeOutput(output); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4068 | mPreviousOutputs = mOutputs; | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4069 | } | 
|  | 4070 |  | 
|  | 4071 | void AudioPolicyManager::closeInput(audio_io_handle_t input) | 
|  | 4072 | { | 
|  | 4073 | ALOGV("closeInput(%d)", input); | 
|  | 4074 |  | 
|  | 4075 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); | 
|  | 4076 | if (inputDesc == NULL) { | 
|  | 4077 | ALOGW("closeInput() unknown input %d", input); | 
|  | 4078 | return; | 
|  | 4079 | } | 
|  | 4080 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4081 | nextAudioPortGeneration(); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4082 |  | 
| Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4083 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4084 | if (index >= 0) { | 
|  | 4085 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
|  | 4086 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); | 
|  | 4087 | mAudioPatches.removeItemsAt(index); | 
|  | 4088 | mpClientInterface->onAudioPatchListUpdate(); | 
|  | 4089 | } | 
|  | 4090 |  | 
|  | 4091 | mpClientInterface->closeInput(input); | 
|  | 4092 | mInputs.removeItem(input); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4093 | } | 
|  | 4094 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4095 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice( | 
|  | 4096 | audio_devices_t device, | 
|  | 4097 | SwAudioOutputCollection openOutputs) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4098 | { | 
|  | 4099 | SortedVector<audio_io_handle_t> outputs; | 
|  | 4100 |  | 
|  | 4101 | ALOGVV("getOutputsForDevice() device %04x", device); | 
|  | 4102 | for (size_t i = 0; i < openOutputs.size(); i++) { | 
|  | 4103 | ALOGVV("output %d isDuplicated=%d device=%04x", | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4104 | i, openOutputs.valueAt(i)->isDuplicated(), | 
|  | 4105 | openOutputs.valueAt(i)->supportedDevices()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4106 | if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) { | 
|  | 4107 | ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i)); | 
|  | 4108 | outputs.add(openOutputs.keyAt(i)); | 
|  | 4109 | } | 
|  | 4110 | } | 
|  | 4111 | return outputs; | 
|  | 4112 | } | 
|  | 4113 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4114 | bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4115 | SortedVector<audio_io_handle_t>& outputs2) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4116 | { | 
|  | 4117 | if (outputs1.size() != outputs2.size()) { | 
|  | 4118 | return false; | 
|  | 4119 | } | 
|  | 4120 | for (size_t i = 0; i < outputs1.size(); i++) { | 
|  | 4121 | if (outputs1[i] != outputs2[i]) { | 
|  | 4122 | return false; | 
|  | 4123 | } | 
|  | 4124 | } | 
|  | 4125 | return true; | 
|  | 4126 | } | 
|  | 4127 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4128 | void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4129 | { | 
|  | 4130 | audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/); | 
|  | 4131 | audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/); | 
|  | 4132 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs); | 
|  | 4133 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs); | 
|  | 4134 |  | 
| Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4135 | // also take into account external policy-related changes: add all outputs which are | 
|  | 4136 | // associated with policies in the "before" and "after" output vectors | 
|  | 4137 | ALOGVV("checkOutputForStrategy(): policy related outputs"); | 
|  | 4138 | for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4139 | const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i); | 
| Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4140 | if (desc != 0 && desc->mPolicyMix != NULL) { | 
|  | 4141 | srcOutputs.add(desc->mIoHandle); | 
|  | 4142 | ALOGVV(" previous outputs: adding %d", desc->mIoHandle); | 
|  | 4143 | } | 
|  | 4144 | } | 
|  | 4145 | for (size_t i = 0 ; i < mOutputs.size() ; i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4146 | const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4147 | if (desc != 0 && desc->mPolicyMix != NULL) { | 
|  | 4148 | dstOutputs.add(desc->mIoHandle); | 
|  | 4149 | ALOGVV(" new outputs: adding %d", desc->mIoHandle); | 
|  | 4150 | } | 
|  | 4151 | } | 
|  | 4152 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4153 | if (!vectorsEqual(srcOutputs,dstOutputs)) { | 
|  | 4154 | ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d", | 
|  | 4155 | strategy, srcOutputs[0], dstOutputs[0]); | 
|  | 4156 | // mute strategy while moving tracks from one output to another | 
|  | 4157 | for (size_t i = 0; i < srcOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4158 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4159 | if (isStrategyActive(desc, strategy)) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4160 | setStrategyMute(strategy, true, desc); | 
|  | 4161 | setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4162 | } | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4163 | sp<AudioSourceDescriptor> source = | 
|  | 4164 | getSourceForStrategyOnOutput(srcOutputs[i], strategy); | 
|  | 4165 | if (source != 0){ | 
|  | 4166 | connectAudioSource(source); | 
|  | 4167 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4168 | } | 
|  | 4169 |  | 
|  | 4170 | // Move effects associated to this strategy from previous output to new output | 
|  | 4171 | if (strategy == STRATEGY_MEDIA) { | 
|  | 4172 | audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs); | 
|  | 4173 | SortedVector<audio_io_handle_t> moved; | 
|  | 4174 | for (size_t i = 0; i < mEffects.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4175 | sp<EffectDescriptor> effectDesc = mEffects.valueAt(i); | 
|  | 4176 | if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX && | 
|  | 4177 | effectDesc->mIo != fxOutput) { | 
|  | 4178 | if (moved.indexOf(effectDesc->mIo) < 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4179 | ALOGV("checkOutputForStrategy() moving effect %d to output %d", | 
|  | 4180 | mEffects.keyAt(i), fxOutput); | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4181 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4182 | fxOutput); | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4183 | moved.add(effectDesc->mIo); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4184 | } | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4185 | effectDesc->mIo = fxOutput; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4186 | } | 
|  | 4187 | } | 
|  | 4188 | } | 
|  | 4189 | // Move tracks associated to this strategy from previous output to new output | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4190 | for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4191 | if (getStrategy((audio_stream_type_t)i) == strategy) { | 
|  | 4192 | mpClientInterface->invalidateStream((audio_stream_type_t)i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4193 | } | 
|  | 4194 | } | 
|  | 4195 | } | 
|  | 4196 | } | 
|  | 4197 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4198 | void AudioPolicyManager::checkOutputForAllStrategies() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4199 | { | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4200 | 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] | 4201 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4202 | checkOutputForStrategy(STRATEGY_PHONE); | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4203 | 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] | 4204 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4205 | checkOutputForStrategy(STRATEGY_SONIFICATION); | 
|  | 4206 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4207 | checkOutputForStrategy(STRATEGY_ACCESSIBILITY); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4208 | checkOutputForStrategy(STRATEGY_MEDIA); | 
|  | 4209 | checkOutputForStrategy(STRATEGY_DTMF); | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4210 | checkOutputForStrategy(STRATEGY_REROUTING); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4211 | } | 
|  | 4212 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4213 | void AudioPolicyManager::checkA2dpSuspend() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4214 | { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4215 | audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4216 | if (a2dpOutput == 0) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4217 | mA2dpSuspended = false; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4218 | return; | 
|  | 4219 | } | 
|  | 4220 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4221 | bool isScoConnected = | 
| Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 4222 | ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET & | 
|  | 4223 | ~AUDIO_DEVICE_BIT_IN) != 0) || | 
|  | 4224 | ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4225 | // suspend A2DP output if: | 
|  | 4226 | //      (NOT already suspended) && | 
|  | 4227 | //      ((SCO device is connected && | 
|  | 4228 | //       (forced usage for communication || for record is SCO))) || | 
|  | 4229 | //      (phone state is ringing || in call) | 
|  | 4230 | // | 
|  | 4231 | // restore A2DP output if: | 
|  | 4232 | //      (Already suspended) && | 
|  | 4233 | //      ((SCO device is NOT connected || | 
|  | 4234 | //       (forced usage NOT for communication && NOT for record is SCO))) && | 
|  | 4235 | //      (phone state is NOT ringing && NOT in call) | 
|  | 4236 | // | 
|  | 4237 | if (mA2dpSuspended) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4238 | if ((!isScoConnected || | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4239 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) && | 
|  | 4240 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) && | 
|  | 4241 | ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) && | 
|  | 4242 | (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4243 |  | 
|  | 4244 | mpClientInterface->restoreOutput(a2dpOutput); | 
|  | 4245 | mA2dpSuspended = false; | 
|  | 4246 | } | 
|  | 4247 | } else { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4248 | if ((isScoConnected && | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4249 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) || | 
|  | 4250 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO))) || | 
|  | 4251 | ((mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) || | 
|  | 4252 | (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4253 |  | 
|  | 4254 | mpClientInterface->suspendOutput(a2dpOutput); | 
|  | 4255 | mA2dpSuspended = true; | 
|  | 4256 | } | 
|  | 4257 | } | 
|  | 4258 | } | 
|  | 4259 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4260 | audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, | 
|  | 4261 | bool fromCache) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4262 | { | 
|  | 4263 | audio_devices_t device = AUDIO_DEVICE_NONE; | 
|  | 4264 |  | 
| Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4265 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4266 | if (index >= 0) { | 
|  | 4267 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
|  | 4268 | if (patchDesc->mUid != mUidCached) { | 
|  | 4269 | ALOGV("getNewOutputDevice() device %08x forced by patch %d", | 
| Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4270 | outputDesc->device(), outputDesc->getPatchHandle()); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4271 | return outputDesc->device(); | 
|  | 4272 | } | 
|  | 4273 | } | 
|  | 4274 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4275 | // 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] | 4276 | // 1: the strategy enforced audible is active and enforced on the output: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4277 | //      use device for strategy enforced audible | 
|  | 4278 | // 2: we are in call or the strategy phone is active on the output: | 
|  | 4279 | //      use device for strategy phone | 
| Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 4280 | // 3: the strategy for enforced audible is active but not enforced on the output: | 
|  | 4281 | //      use the device for strategy enforced audible | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4282 | // 4: the strategy sonification is active on the output: | 
| Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4283 | //      use device for strategy sonification | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4284 | // 5: the strategy accessibility is active on the output: | 
|  | 4285 | //      use device for strategy accessibility | 
| Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4286 | // 6: the strategy "respectful" sonification is active on the output: | 
|  | 4287 | //      use device for strategy "respectful" sonification | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4288 | // 7: the strategy media is active on the output: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4289 | //      use device for strategy media | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4290 | // 8: the strategy DTMF is active on the output: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4291 | //      use device for strategy DTMF | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4292 | // 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] | 4293 | //      use device for strategy t-t-s | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4294 | if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) && | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4295 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4296 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); | 
|  | 4297 | } else if (isInCall() || | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4298 | isStrategyActive(outputDesc, STRATEGY_PHONE)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4299 | device = getDeviceForStrategy(STRATEGY_PHONE, fromCache); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4300 | } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) { | 
| Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 4301 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4302 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4303 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache); | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4304 | } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) { | 
|  | 4305 | device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4306 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4307 | device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4308 | } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4309 | device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4310 | } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4311 | device = getDeviceForStrategy(STRATEGY_DTMF, fromCache); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4312 | } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) { | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4313 | device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4314 | } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) { | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4315 | device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4316 | } | 
|  | 4317 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4318 | ALOGV("getNewOutputDevice() selected device %x", device); | 
|  | 4319 | return device; | 
|  | 4320 | } | 
|  | 4321 |  | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 4322 | audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input) | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4323 | { | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 4324 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4325 |  | 
| Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4326 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4327 | if (index >= 0) { | 
|  | 4328 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
|  | 4329 | if (patchDesc->mUid != mUidCached) { | 
|  | 4330 | ALOGV("getNewInputDevice() device %08x forced by patch %d", | 
| Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4331 | inputDesc->mDevice, inputDesc->getPatchHandle()); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4332 | return inputDesc->mDevice; | 
|  | 4333 | } | 
|  | 4334 | } | 
|  | 4335 |  | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 4336 | audio_devices_t device = getDeviceAndMixForInputSource(inputDesc->inputSource()); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4337 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4338 | return device; | 
|  | 4339 | } | 
|  | 4340 |  | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4341 | bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1, | 
|  | 4342 | audio_stream_type_t stream2) { | 
|  | 4343 | return ((stream1 == stream2) || | 
|  | 4344 | ((stream1 == AUDIO_STREAM_ACCESSIBILITY) && (stream2 == AUDIO_STREAM_MUSIC)) || | 
|  | 4345 | ((stream1 == AUDIO_STREAM_MUSIC) && (stream2 == AUDIO_STREAM_ACCESSIBILITY))); | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4346 | } | 
|  | 4347 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4348 | uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4349 | return (uint32_t)getStrategy(stream); | 
|  | 4350 | } | 
|  | 4351 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4352 | audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4353 | // By checking the range of stream before calling getStrategy, we avoid | 
|  | 4354 | // getStrategy's behavior for invalid streams.  getStrategy would do a ALOGE | 
|  | 4355 | // 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] | 4356 | if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4357 | return AUDIO_DEVICE_NONE; | 
|  | 4358 | } | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4359 | audio_devices_t devices = AUDIO_DEVICE_NONE; | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4360 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { | 
|  | 4361 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4362 | continue; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4363 | } | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4364 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4365 | audio_devices_t curDevices = | 
|  | 4366 | getDeviceForStrategy((routing_strategy)curStrategy, true /*fromCache*/); | 
|  | 4367 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(curDevices, mOutputs); | 
|  | 4368 | for (size_t i = 0; i < outputs.size(); i++) { | 
|  | 4369 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4370 | if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) { | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4371 | curDevices |= outputDesc->device(); | 
|  | 4372 | } | 
|  | 4373 | } | 
|  | 4374 | devices |= curDevices; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4375 | } | 
| Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4376 |  | 
|  | 4377 | /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it | 
|  | 4378 | and doesn't really need to.*/ | 
|  | 4379 | if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) { | 
|  | 4380 | devices |= AUDIO_DEVICE_OUT_SPEAKER; | 
|  | 4381 | devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE; | 
|  | 4382 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4383 | return devices; | 
|  | 4384 | } | 
|  | 4385 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4386 | routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const | 
|  | 4387 | { | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4388 | 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] | 4389 | return mEngine->getStrategyForStream(stream); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4390 | } | 
|  | 4391 |  | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4392 | uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) { | 
|  | 4393 | // flags to strategy mapping | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4394 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { | 
|  | 4395 | return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER; | 
|  | 4396 | } | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4397 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { | 
|  | 4398 | return (uint32_t) STRATEGY_ENFORCED_AUDIBLE; | 
|  | 4399 | } | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4400 | // usage to strategy mapping | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4401 | return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage)); | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4402 | } | 
|  | 4403 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4404 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4405 | switch(stream) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4406 | case AUDIO_STREAM_MUSIC: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4407 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); | 
|  | 4408 | updateDevicesAndOutputs(); | 
|  | 4409 | break; | 
|  | 4410 | default: | 
|  | 4411 | break; | 
|  | 4412 | } | 
|  | 4413 | } | 
|  | 4414 |  | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4415 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { | 
| Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 4416 |  | 
|  | 4417 | // skip beacon mute management if a dedicated TTS output is available | 
|  | 4418 | if (mTtsOutputAvailable) { | 
|  | 4419 | return 0; | 
|  | 4420 | } | 
|  | 4421 |  | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4422 | switch(event) { | 
|  | 4423 | case STARTING_OUTPUT: | 
|  | 4424 | mBeaconMuteRefCount++; | 
|  | 4425 | break; | 
|  | 4426 | case STOPPING_OUTPUT: | 
|  | 4427 | if (mBeaconMuteRefCount > 0) { | 
|  | 4428 | mBeaconMuteRefCount--; | 
|  | 4429 | } | 
|  | 4430 | break; | 
|  | 4431 | case STARTING_BEACON: | 
|  | 4432 | mBeaconPlayingRefCount++; | 
|  | 4433 | break; | 
|  | 4434 | case STOPPING_BEACON: | 
|  | 4435 | if (mBeaconPlayingRefCount > 0) { | 
|  | 4436 | mBeaconPlayingRefCount--; | 
|  | 4437 | } | 
|  | 4438 | break; | 
|  | 4439 | } | 
|  | 4440 |  | 
|  | 4441 | if (mBeaconMuteRefCount > 0) { | 
|  | 4442 | // any playback causes beacon to be muted | 
|  | 4443 | return setBeaconMute(true); | 
|  | 4444 | } else { | 
|  | 4445 | // no other playback: unmute when beacon starts playing, mute when it stops | 
|  | 4446 | return setBeaconMute(mBeaconPlayingRefCount == 0); | 
|  | 4447 | } | 
|  | 4448 | } | 
|  | 4449 |  | 
|  | 4450 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { | 
|  | 4451 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", | 
|  | 4452 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); | 
|  | 4453 | // keep track of muted state to avoid repeating mute/unmute operations | 
|  | 4454 | if (mBeaconMuted != mute) { | 
|  | 4455 | // mute/unmute AUDIO_STREAM_TTS on all outputs | 
|  | 4456 | ALOGV("\t muting %d", mute); | 
|  | 4457 | uint32_t maxLatency = 0; | 
|  | 4458 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4459 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4460 | setStreamMute(AUDIO_STREAM_TTS, mute/*on*/, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4461 | desc, | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4462 | 0 /*delay*/, AUDIO_DEVICE_NONE); | 
|  | 4463 | const uint32_t latency = desc->latency() * 2; | 
|  | 4464 | if (latency > maxLatency) { | 
|  | 4465 | maxLatency = latency; | 
|  | 4466 | } | 
|  | 4467 | } | 
|  | 4468 | mBeaconMuted = mute; | 
|  | 4469 | return maxLatency; | 
|  | 4470 | } | 
|  | 4471 | return 0; | 
|  | 4472 | } | 
|  | 4473 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4474 | audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4475 | bool fromCache) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4476 | { | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4477 | // Routing | 
|  | 4478 | // see if we have an explicit route | 
|  | 4479 | // scan the whole RouteMap, for each entry, convert the stream type to a strategy | 
|  | 4480 | // (getStrategy(stream)). | 
|  | 4481 | // if the strategy from the stream type in the RouteMap is the same as the argument above, | 
|  | 4482 | // and activity count is non-zero | 
|  | 4483 | // the device = the device from the descriptor in the RouteMap, and exit. | 
|  | 4484 | for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) { | 
|  | 4485 | sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex); | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4486 | routing_strategy routeStrategy = getStrategy(route->mStreamType); | 
|  | 4487 | if ((routeStrategy == strategy) && route->isActive()) { | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4488 | return route->mDeviceDescriptor->type(); | 
|  | 4489 | } | 
|  | 4490 | } | 
|  | 4491 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4492 | if (fromCache) { | 
|  | 4493 | ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x", | 
|  | 4494 | strategy, mDeviceForStrategy[strategy]); | 
|  | 4495 | return mDeviceForStrategy[strategy]; | 
|  | 4496 | } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4497 | return mEngine->getDeviceForStrategy(strategy); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4498 | } | 
|  | 4499 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4500 | void AudioPolicyManager::updateDevicesAndOutputs() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4501 | { | 
|  | 4502 | for (int i = 0; i < NUM_STRATEGIES; i++) { | 
|  | 4503 | mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); | 
|  | 4504 | } | 
|  | 4505 | mPreviousOutputs = mOutputs; | 
|  | 4506 | } | 
|  | 4507 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4508 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4509 | audio_devices_t prevDevice, | 
|  | 4510 | uint32_t delayMs) | 
|  | 4511 | { | 
|  | 4512 | // mute/unmute strategies using an incompatible device combination | 
|  | 4513 | // if muting, wait for the audio in pcm buffer to be drained before proceeding | 
|  | 4514 | // if unmuting, unmute only after the specified delay | 
|  | 4515 | if (outputDesc->isDuplicated()) { | 
|  | 4516 | return 0; | 
|  | 4517 | } | 
|  | 4518 |  | 
|  | 4519 | uint32_t muteWaitMs = 0; | 
|  | 4520 | audio_devices_t device = outputDesc->device(); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4521 | bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4522 |  | 
|  | 4523 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { | 
|  | 4524 | audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4525 | curDevice = curDevice & outputDesc->supportedDevices(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4526 | bool mute = shouldMute && (curDevice & device) && (curDevice != device); | 
|  | 4527 | bool doMute = false; | 
|  | 4528 |  | 
|  | 4529 | if (mute && !outputDesc->mStrategyMutedByDevice[i]) { | 
|  | 4530 | doMute = true; | 
|  | 4531 | outputDesc->mStrategyMutedByDevice[i] = true; | 
|  | 4532 | } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){ | 
|  | 4533 | doMute = true; | 
|  | 4534 | outputDesc->mStrategyMutedByDevice[i] = false; | 
|  | 4535 | } | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4536 | if (doMute) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4537 | for (size_t j = 0; j < mOutputs.size(); j++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4538 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4539 | // skip output if it does not share any device with current output | 
|  | 4540 | if ((desc->supportedDevices() & outputDesc->supportedDevices()) | 
|  | 4541 | == AUDIO_DEVICE_NONE) { | 
|  | 4542 | continue; | 
|  | 4543 | } | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4544 | ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x)", | 
|  | 4545 | mute ? "muting" : "unmuting", i, curDevice); | 
|  | 4546 | setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4547 | if (isStrategyActive(desc, (routing_strategy)i)) { | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4548 | if (mute) { | 
|  | 4549 | // FIXME: should not need to double latency if volume could be applied | 
|  | 4550 | // immediately by the audioflinger mixer. We must account for the delay | 
|  | 4551 | // between now and the next time the audioflinger thread for this output | 
|  | 4552 | // will process a buffer (which corresponds to one buffer size, | 
|  | 4553 | // usually 1/2 or 1/4 of the latency). | 
|  | 4554 | if (muteWaitMs < desc->latency() * 2) { | 
|  | 4555 | muteWaitMs = desc->latency() * 2; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4556 | } | 
|  | 4557 | } | 
|  | 4558 | } | 
|  | 4559 | } | 
|  | 4560 | } | 
|  | 4561 | } | 
|  | 4562 |  | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4563 | // temporary mute output if device selection changes to avoid volume bursts due to | 
|  | 4564 | // different per device volumes | 
|  | 4565 | if (outputDesc->isActive() && (device != prevDevice)) { | 
|  | 4566 | if (muteWaitMs < outputDesc->latency() * 2) { | 
|  | 4567 | muteWaitMs = outputDesc->latency() * 2; | 
|  | 4568 | } | 
|  | 4569 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4570 | if (isStrategyActive(outputDesc, (routing_strategy)i)) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4571 | setStrategyMute((routing_strategy)i, true, outputDesc); | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4572 | // do tempMute unmute after twice the mute wait time | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4573 | setStrategyMute((routing_strategy)i, false, outputDesc, | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4574 | muteWaitMs *2, device); | 
|  | 4575 | } | 
|  | 4576 | } | 
|  | 4577 | } | 
|  | 4578 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4579 | // wait for the PCM output buffers to empty before proceeding with the rest of the command | 
|  | 4580 | if (muteWaitMs > delayMs) { | 
|  | 4581 | muteWaitMs -= delayMs; | 
|  | 4582 | usleep(muteWaitMs * 1000); | 
|  | 4583 | return muteWaitMs; | 
|  | 4584 | } | 
|  | 4585 | return 0; | 
|  | 4586 | } | 
|  | 4587 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4588 | uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4589 | audio_devices_t device, | 
|  | 4590 | bool force, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4591 | int delayMs, | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4592 | audio_patch_handle_t *patchHandle, | 
|  | 4593 | const char* address) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4594 | { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4595 | ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4596 | AudioParameter param; | 
|  | 4597 | uint32_t muteWaitMs; | 
|  | 4598 |  | 
|  | 4599 | if (outputDesc->isDuplicated()) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4600 | muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs); | 
|  | 4601 | muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4602 | return muteWaitMs; | 
|  | 4603 | } | 
|  | 4604 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current | 
|  | 4605 | // output profile | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4606 | if ((device != AUDIO_DEVICE_NONE) && | 
|  | 4607 | ((device & outputDesc->supportedDevices()) == 0)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4608 | return 0; | 
|  | 4609 | } | 
|  | 4610 |  | 
|  | 4611 | // filter devices according to output selected | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4612 | device = (audio_devices_t)(device & outputDesc->supportedDevices()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4613 |  | 
|  | 4614 | audio_devices_t prevDevice = outputDesc->mDevice; | 
|  | 4615 |  | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4616 | ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4617 |  | 
|  | 4618 | if (device != AUDIO_DEVICE_NONE) { | 
|  | 4619 | outputDesc->mDevice = device; | 
|  | 4620 | } | 
|  | 4621 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs); | 
|  | 4622 |  | 
|  | 4623 | // Do not change the routing if: | 
| Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 4624 | //      the requested device is AUDIO_DEVICE_NONE | 
|  | 4625 | //      OR the requested device is the same as current device | 
|  | 4626 | //  AND force is not specified | 
|  | 4627 | //  AND the output is connected by a valid audio patch. | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4628 | // Doing this check here allows the caller to call setOutputDevice() without conditions | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4629 | if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && | 
|  | 4630 | !force && | 
| Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4631 | outputDesc->getPatchHandle() != 0) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4632 | ALOGV("setOutputDevice() setting same device 0x%04x or null device", device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4633 | return muteWaitMs; | 
|  | 4634 | } | 
|  | 4635 |  | 
|  | 4636 | ALOGV("setOutputDevice() changing device"); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4637 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4638 | // do the routing | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4639 | if (device == AUDIO_DEVICE_NONE) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4640 | resetOutputDevice(outputDesc, delayMs, NULL); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4641 | } else { | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 4642 | DeviceVector deviceList; | 
|  | 4643 | if ((address == NULL) || (strlen(address) == 0)) { | 
|  | 4644 | deviceList = mAvailableOutputDevices.getDevicesFromType(device); | 
|  | 4645 | } else { | 
|  | 4646 | deviceList = mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address)); | 
|  | 4647 | } | 
|  | 4648 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4649 | if (!deviceList.isEmpty()) { | 
|  | 4650 | struct audio_patch patch; | 
|  | 4651 | outputDesc->toAudioPortConfig(&patch.sources[0]); | 
|  | 4652 | patch.num_sources = 1; | 
|  | 4653 | patch.num_sinks = 0; | 
|  | 4654 | for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) { | 
|  | 4655 | deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4656 | patch.num_sinks++; | 
|  | 4657 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4658 | ssize_t index; | 
|  | 4659 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { | 
|  | 4660 | index = mAudioPatches.indexOfKey(*patchHandle); | 
|  | 4661 | } else { | 
| Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4662 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4663 | } | 
|  | 4664 | sp< AudioPatch> patchDesc; | 
|  | 4665 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; | 
|  | 4666 | if (index >= 0) { | 
|  | 4667 | patchDesc = mAudioPatches.valueAt(index); | 
|  | 4668 | afPatchHandle = patchDesc->mAfPatchHandle; | 
|  | 4669 | } | 
|  | 4670 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4671 | status_t status = mpClientInterface->createAudioPatch(&patch, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4672 | &afPatchHandle, | 
|  | 4673 | delayMs); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4674 | ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d" | 
|  | 4675 | "num_sources %d num_sinks %d", | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4676 | status, afPatchHandle, patch.num_sources, patch.num_sinks); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4677 | if (status == NO_ERROR) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4678 | if (index < 0) { | 
| François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 4679 | patchDesc = new AudioPatch(&patch, mUidCached); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4680 | addAudioPatch(patchDesc->mHandle, patchDesc); | 
|  | 4681 | } else { | 
|  | 4682 | patchDesc->mPatch = patch; | 
|  | 4683 | } | 
|  | 4684 | patchDesc->mAfPatchHandle = afPatchHandle; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4685 | if (patchHandle) { | 
|  | 4686 | *patchHandle = patchDesc->mHandle; | 
|  | 4687 | } | 
| Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4688 | outputDesc->setPatchHandle(patchDesc->mHandle); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4689 | nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4690 | mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4691 | } | 
|  | 4692 | } | 
| bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 4693 |  | 
|  | 4694 | // inform all input as well | 
|  | 4695 | for (size_t i = 0; i < mInputs.size(); i++) { | 
|  | 4696 | const sp<AudioInputDescriptor>  inputDescriptor = mInputs.valueAt(i); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4697 | if (!is_virtual_input_device(inputDescriptor->mDevice)) { | 
| bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 4698 | AudioParameter inputCmd = AudioParameter(); | 
|  | 4699 | ALOGV("%s: inform input %d of device:%d", __func__, | 
|  | 4700 | inputDescriptor->mIoHandle, device); | 
|  | 4701 | inputCmd.addInt(String8(AudioParameter::keyRouting),device); | 
|  | 4702 | mpClientInterface->setParameters(inputDescriptor->mIoHandle, | 
|  | 4703 | inputCmd.toString(), | 
|  | 4704 | delayMs); | 
|  | 4705 | } | 
|  | 4706 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4707 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4708 |  | 
|  | 4709 | // update stream volumes according to new device | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4710 | applyStreamVolumes(outputDesc, device, delayMs); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4711 |  | 
|  | 4712 | return muteWaitMs; | 
|  | 4713 | } | 
|  | 4714 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4715 | status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4716 | int delayMs, | 
|  | 4717 | audio_patch_handle_t *patchHandle) | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4718 | { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4719 | ssize_t index; | 
|  | 4720 | if (patchHandle) { | 
|  | 4721 | index = mAudioPatches.indexOfKey(*patchHandle); | 
|  | 4722 | } else { | 
| Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4723 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4724 | } | 
|  | 4725 | if (index < 0) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4726 | return INVALID_OPERATION; | 
|  | 4727 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4728 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
|  | 4729 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4730 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); | 
| Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 4731 | outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4732 | removeAudioPatch(patchDesc->mHandle); | 
|  | 4733 | nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4734 | mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4735 | return status; | 
|  | 4736 | } | 
|  | 4737 |  | 
|  | 4738 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, | 
|  | 4739 | audio_devices_t device, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4740 | bool force, | 
|  | 4741 | audio_patch_handle_t *patchHandle) | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4742 | { | 
|  | 4743 | status_t status = NO_ERROR; | 
|  | 4744 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4745 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4746 | if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) { | 
|  | 4747 | inputDesc->mDevice = device; | 
|  | 4748 |  | 
|  | 4749 | DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device); | 
|  | 4750 | if (!deviceList.isEmpty()) { | 
|  | 4751 | struct audio_patch patch; | 
|  | 4752 | inputDesc->toAudioPortConfig(&patch.sinks[0]); | 
| Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 4753 | // AUDIO_SOURCE_HOTWORD is for internal use only: | 
|  | 4754 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 4755 | if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD && | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 4756 | !inputDesc->isSoundTrigger()) { | 
| Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 4757 | patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION; | 
|  | 4758 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4759 | patch.num_sinks = 1; | 
|  | 4760 | //only one input device for now | 
|  | 4761 | deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4762 | patch.num_sources = 1; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4763 | ssize_t index; | 
|  | 4764 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { | 
|  | 4765 | index = mAudioPatches.indexOfKey(*patchHandle); | 
|  | 4766 | } else { | 
| Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4767 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4768 | } | 
|  | 4769 | sp< AudioPatch> patchDesc; | 
|  | 4770 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; | 
|  | 4771 | if (index >= 0) { | 
|  | 4772 | patchDesc = mAudioPatches.valueAt(index); | 
|  | 4773 | afPatchHandle = patchDesc->mAfPatchHandle; | 
|  | 4774 | } | 
|  | 4775 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4776 | status_t status = mpClientInterface->createAudioPatch(&patch, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4777 | &afPatchHandle, | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4778 | 0); | 
|  | 4779 | ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d", | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4780 | status, afPatchHandle); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4781 | if (status == NO_ERROR) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4782 | if (index < 0) { | 
| François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 4783 | patchDesc = new AudioPatch(&patch, mUidCached); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4784 | addAudioPatch(patchDesc->mHandle, patchDesc); | 
|  | 4785 | } else { | 
|  | 4786 | patchDesc->mPatch = patch; | 
|  | 4787 | } | 
|  | 4788 | patchDesc->mAfPatchHandle = afPatchHandle; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4789 | if (patchHandle) { | 
|  | 4790 | *patchHandle = patchDesc->mHandle; | 
|  | 4791 | } | 
| Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4792 | inputDesc->setPatchHandle(patchDesc->mHandle); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4793 | nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4794 | mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4795 | } | 
|  | 4796 | } | 
|  | 4797 | } | 
|  | 4798 | return status; | 
|  | 4799 | } | 
|  | 4800 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4801 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, | 
|  | 4802 | audio_patch_handle_t *patchHandle) | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4803 | { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4804 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4805 | ssize_t index; | 
|  | 4806 | if (patchHandle) { | 
|  | 4807 | index = mAudioPatches.indexOfKey(*patchHandle); | 
|  | 4808 | } else { | 
| Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4809 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4810 | } | 
|  | 4811 | if (index < 0) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4812 | return INVALID_OPERATION; | 
|  | 4813 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4814 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
|  | 4815 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4816 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); | 
| Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 4817 | inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4818 | removeAudioPatch(patchDesc->mHandle); | 
|  | 4819 | nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4820 | mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4821 | return status; | 
|  | 4822 | } | 
|  | 4823 |  | 
| Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 4824 | sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4825 | String8 address, | 
|  | 4826 | uint32_t& samplingRate, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4827 | audio_format_t& format, | 
|  | 4828 | audio_channel_mask_t& channelMask, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4829 | audio_input_flags_t flags) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4830 | { | 
|  | 4831 | // Choose an input profile based on the requested capture parameters: select the first available | 
|  | 4832 | // profile supporting all requested parameters. | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4833 | // | 
|  | 4834 | // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return | 
|  | 4835 | // the best matching profile, not the first one. | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4836 |  | 
|  | 4837 | for (size_t i = 0; i < mHwModules.size(); i++) | 
|  | 4838 | { | 
|  | 4839 | if (mHwModules[i]->mHandle == 0) { | 
|  | 4840 | continue; | 
|  | 4841 | } | 
|  | 4842 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) | 
|  | 4843 | { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4844 | sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j]; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4845 | // profile->log(); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4846 | if (profile->isCompatibleProfile(device, address, samplingRate, | 
| Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 4847 | &samplingRate /*updatedSamplingRate*/, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4848 | format, | 
|  | 4849 | &format /*updatedFormat*/, | 
|  | 4850 | channelMask, | 
|  | 4851 | &channelMask /*updatedChannelMask*/, | 
|  | 4852 | (audio_output_flags_t) flags)) { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4853 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4854 | return profile; | 
|  | 4855 | } | 
|  | 4856 | } | 
|  | 4857 | } | 
|  | 4858 | return NULL; | 
|  | 4859 | } | 
|  | 4860 |  | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 4861 |  | 
|  | 4862 | audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4863 | AudioMix **policyMix) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4864 | { | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4865 | audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; | 
|  | 4866 | audio_devices_t selectedDeviceFromMix = | 
|  | 4867 | mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4868 |  | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4869 | if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) { | 
|  | 4870 | return selectedDeviceFromMix; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4871 | } | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 4872 | return getDeviceForInputSource(inputSource); | 
|  | 4873 | } | 
|  | 4874 |  | 
|  | 4875 | audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource) | 
|  | 4876 | { | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 4877 | for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) { | 
|  | 4878 | sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex); | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4879 | if (inputSource == route->mSource && route->isActive()) { | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 4880 | return route->mDeviceDescriptor->type(); | 
|  | 4881 | } | 
|  | 4882 | } | 
|  | 4883 |  | 
|  | 4884 | return mEngine->getDeviceForInputSource(inputSource); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4885 | } | 
|  | 4886 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4887 | float AudioPolicyManager::computeVolume(audio_stream_type_t stream, | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4888 | int index, | 
|  | 4889 | audio_devices_t device) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4890 | { | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4891 | float volumeDb = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4892 | // if a headset is connected, apply the following rules to ring tones and notifications | 
|  | 4893 | // to avoid sound level bursts in user's ears: | 
| Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 4894 | // - always attenuate notifications volume by 6dB | 
|  | 4895 | // - attenuate ring tones volume by 6dB unless music is not playing and | 
|  | 4896 | // speaker is part of the select devices | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4897 | // - if music is playing, always limit the volume to current music volume, | 
|  | 4898 | // with a minimum threshold at -36dB so that notification is always perceived. | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4899 | const routing_strategy stream_strategy = getStrategy(stream); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4900 | if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | | 
|  | 4901 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | | 
|  | 4902 | AUDIO_DEVICE_OUT_WIRED_HEADSET | | 
|  | 4903 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) && | 
|  | 4904 | ((stream_strategy == STRATEGY_SONIFICATION) | 
|  | 4905 | || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL) | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4906 | || (stream == AUDIO_STREAM_SYSTEM) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4907 | || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) && | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4908 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) && | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4909 | mVolumeCurves->canBeMuted(stream)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4910 | // when the phone is ringing we must consider that music could have been paused just before | 
|  | 4911 | // by the music application and behave as if music was active if the last music track was | 
|  | 4912 | // just stopped | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4913 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4914 | mLimitRingtoneVolume) { | 
| Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 4915 | volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4916 | audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/); | 
| Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4917 | float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC, | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4918 | mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC, | 
|  | 4919 | musicDevice), | 
|  | 4920 | musicDevice); | 
| Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4921 | float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ? | 
|  | 4922 | musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB; | 
|  | 4923 | if (volumeDb > minVolDB) { | 
|  | 4924 | volumeDb = minVolDB; | 
|  | 4925 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4926 | } | 
| Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 4927 | } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) || | 
|  | 4928 | stream_strategy != STRATEGY_SONIFICATION) { | 
|  | 4929 | volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4930 | } | 
|  | 4931 | } | 
|  | 4932 |  | 
| Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4933 | return volumeDb; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4934 | } | 
|  | 4935 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4936 | status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4937 | int index, | 
|  | 4938 | const sp<AudioOutputDescriptor>& outputDesc, | 
|  | 4939 | audio_devices_t device, | 
|  | 4940 | int delayMs, | 
|  | 4941 | bool force) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4942 | { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4943 | // do not change actual stream volume if the stream is muted | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4944 | if (outputDesc->mMuteCount[stream] != 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4945 | ALOGVV("checkAndSetVolume() stream %d muted count %d", | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4946 | stream, outputDesc->mMuteCount[stream]); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4947 | return NO_ERROR; | 
|  | 4948 | } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4949 | audio_policy_forced_cfg_t forceUseForComm = | 
|  | 4950 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4951 | // 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] | 4952 | if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) || | 
|  | 4953 | (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4954 | 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] | 4955 | stream, forceUseForComm); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4956 | return INVALID_OPERATION; | 
|  | 4957 | } | 
|  | 4958 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4959 | if (device == AUDIO_DEVICE_NONE) { | 
|  | 4960 | device = outputDesc->device(); | 
|  | 4961 | } | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4962 |  | 
| Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4963 | float volumeDb = computeVolume(stream, index, device); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4964 | if (outputDesc->isFixedVolume(device)) { | 
| Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4965 | volumeDb = 0.0f; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4966 | } | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4967 |  | 
| Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4968 | outputDesc->setVolume(volumeDb, stream, device, delayMs, force); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4969 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4970 | if (stream == AUDIO_STREAM_VOICE_CALL || | 
|  | 4971 | stream == AUDIO_STREAM_BLUETOOTH_SCO) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4972 | float voiceVolume; | 
|  | 4973 | // 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] | 4974 | if (stream == AUDIO_STREAM_VOICE_CALL) { | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4975 | voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4976 | } else { | 
|  | 4977 | voiceVolume = 1.0; | 
|  | 4978 | } | 
|  | 4979 |  | 
| Eric Laurent | 18fba84 | 2016-03-31 14:41:26 -0700 | [diff] [blame] | 4980 | if (voiceVolume != mLastVoiceVolume) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4981 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); | 
|  | 4982 | mLastVoiceVolume = voiceVolume; | 
|  | 4983 | } | 
|  | 4984 | } | 
|  | 4985 |  | 
|  | 4986 | return NO_ERROR; | 
|  | 4987 | } | 
|  | 4988 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4989 | void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc, | 
|  | 4990 | audio_devices_t device, | 
|  | 4991 | int delayMs, | 
|  | 4992 | bool force) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4993 | { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4994 | ALOGVV("applyStreamVolumes() for device %08x", device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4995 |  | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4996 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4997 | checkAndSetVolume((audio_stream_type_t)stream, | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4998 | mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device), | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4999 | outputDesc, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5000 | device, | 
|  | 5001 | delayMs, | 
|  | 5002 | force); | 
|  | 5003 | } | 
|  | 5004 | } | 
|  | 5005 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5006 | void AudioPolicyManager::setStrategyMute(routing_strategy strategy, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5007 | bool on, | 
|  | 5008 | const sp<AudioOutputDescriptor>& outputDesc, | 
|  | 5009 | int delayMs, | 
|  | 5010 | audio_devices_t device) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5011 | { | 
| Eric Laurent | 72249d5 | 2015-06-08 11:12:15 -0700 | [diff] [blame] | 5012 | ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d", | 
|  | 5013 | strategy, on, outputDesc->getId()); | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5014 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5015 | if (getStrategy((audio_stream_type_t)stream) == strategy) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5016 | setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5017 | } | 
|  | 5018 | } | 
|  | 5019 | } | 
|  | 5020 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5021 | void AudioPolicyManager::setStreamMute(audio_stream_type_t stream, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5022 | bool on, | 
|  | 5023 | const sp<AudioOutputDescriptor>& outputDesc, | 
|  | 5024 | int delayMs, | 
|  | 5025 | audio_devices_t device) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5026 | { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5027 | if (device == AUDIO_DEVICE_NONE) { | 
|  | 5028 | device = outputDesc->device(); | 
|  | 5029 | } | 
|  | 5030 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5031 | ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x", | 
|  | 5032 | stream, on, outputDesc->mMuteCount[stream], device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5033 |  | 
|  | 5034 | if (on) { | 
|  | 5035 | if (outputDesc->mMuteCount[stream] == 0) { | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5036 | if (mVolumeCurves->canBeMuted(stream) && | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5037 | ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) || | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5038 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5039 | checkAndSetVolume(stream, 0, outputDesc, device, delayMs); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5040 | } | 
|  | 5041 | } | 
|  | 5042 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored | 
|  | 5043 | outputDesc->mMuteCount[stream]++; | 
|  | 5044 | } else { | 
|  | 5045 | if (outputDesc->mMuteCount[stream] == 0) { | 
|  | 5046 | ALOGV("setStreamMute() unmuting non muted stream!"); | 
|  | 5047 | return; | 
|  | 5048 | } | 
|  | 5049 | if (--outputDesc->mMuteCount[stream] == 0) { | 
|  | 5050 | checkAndSetVolume(stream, | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5051 | mVolumeCurves->getVolumeIndex(stream, device), | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5052 | outputDesc, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5053 | device, | 
|  | 5054 | delayMs); | 
|  | 5055 | } | 
|  | 5056 | } | 
|  | 5057 | } | 
|  | 5058 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5059 | void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5060 | bool starting, bool stateChange) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5061 | { | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 5062 | if(!hasPrimaryOutput()) { | 
|  | 5063 | return; | 
|  | 5064 | } | 
|  | 5065 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5066 | // if the stream pertains to sonification strategy and we are in call we must | 
|  | 5067 | // mute the stream if it is low visibility. If it is high visibility, we must play a tone | 
|  | 5068 | // in the device used for phone strategy and play the tone if the selected device does not | 
|  | 5069 | // interfere with the device used for phone strategy | 
|  | 5070 | // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as | 
|  | 5071 | // many times as there are active tracks on the output | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5072 | const routing_strategy stream_strategy = getStrategy(stream); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5073 | if ((stream_strategy == STRATEGY_SONIFICATION) || | 
|  | 5074 | ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5075 | sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5076 | ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d", | 
|  | 5077 | stream, starting, outputDesc->mDevice, stateChange); | 
|  | 5078 | if (outputDesc->mRefCount[stream]) { | 
|  | 5079 | int muteCount = 1; | 
|  | 5080 | if (stateChange) { | 
|  | 5081 | muteCount = outputDesc->mRefCount[stream]; | 
|  | 5082 | } | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5083 | if (audio_is_low_visibility(stream)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5084 | ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount); | 
|  | 5085 | for (int i = 0; i < muteCount; i++) { | 
|  | 5086 | setStreamMute(stream, starting, mPrimaryOutput); | 
|  | 5087 | } | 
|  | 5088 | } else { | 
|  | 5089 | ALOGV("handleIncallSonification() high visibility"); | 
|  | 5090 | if (outputDesc->device() & | 
|  | 5091 | getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) { | 
|  | 5092 | ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount); | 
|  | 5093 | for (int i = 0; i < muteCount; i++) { | 
|  | 5094 | setStreamMute(stream, starting, mPrimaryOutput); | 
|  | 5095 | } | 
|  | 5096 | } | 
|  | 5097 | if (starting) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5098 | mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION, | 
|  | 5099 | AUDIO_STREAM_VOICE_CALL); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5100 | } else { | 
|  | 5101 | mpClientInterface->stopTone(); | 
|  | 5102 | } | 
|  | 5103 | } | 
|  | 5104 | } | 
|  | 5105 | } | 
|  | 5106 | } | 
|  | 5107 |  | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5108 | audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr) | 
|  | 5109 | { | 
|  | 5110 | // flags to stream type mapping | 
|  | 5111 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { | 
|  | 5112 | return AUDIO_STREAM_ENFORCED_AUDIBLE; | 
|  | 5113 | } | 
|  | 5114 | if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) { | 
|  | 5115 | return AUDIO_STREAM_BLUETOOTH_SCO; | 
|  | 5116 | } | 
| Jean-Michel Trivi | 79ad438 | 2015-01-29 10:49:39 -0800 | [diff] [blame] | 5117 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { | 
|  | 5118 | return AUDIO_STREAM_TTS; | 
|  | 5119 | } | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5120 |  | 
|  | 5121 | // usage to stream type mapping | 
|  | 5122 | switch (attr->usage) { | 
|  | 5123 | case AUDIO_USAGE_MEDIA: | 
|  | 5124 | case AUDIO_USAGE_GAME: | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5125 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: | 
|  | 5126 | return AUDIO_STREAM_MUSIC; | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5127 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: | 
|  | 5128 | return AUDIO_STREAM_ACCESSIBILITY; | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5129 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: | 
|  | 5130 | return AUDIO_STREAM_SYSTEM; | 
|  | 5131 | case AUDIO_USAGE_VOICE_COMMUNICATION: | 
|  | 5132 | return AUDIO_STREAM_VOICE_CALL; | 
|  | 5133 |  | 
|  | 5134 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: | 
|  | 5135 | return AUDIO_STREAM_DTMF; | 
|  | 5136 |  | 
|  | 5137 | case AUDIO_USAGE_ALARM: | 
|  | 5138 | return AUDIO_STREAM_ALARM; | 
|  | 5139 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: | 
|  | 5140 | return AUDIO_STREAM_RING; | 
|  | 5141 |  | 
|  | 5142 | case AUDIO_USAGE_NOTIFICATION: | 
|  | 5143 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: | 
|  | 5144 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: | 
|  | 5145 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: | 
|  | 5146 | case AUDIO_USAGE_NOTIFICATION_EVENT: | 
|  | 5147 | return AUDIO_STREAM_NOTIFICATION; | 
|  | 5148 |  | 
|  | 5149 | case AUDIO_USAGE_UNKNOWN: | 
|  | 5150 | default: | 
|  | 5151 | return AUDIO_STREAM_MUSIC; | 
|  | 5152 | } | 
|  | 5153 | } | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5154 |  | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5155 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) | 
|  | 5156 | { | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5157 | // has flags that map to a strategy? | 
|  | 5158 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { | 
|  | 5159 | return true; | 
|  | 5160 | } | 
|  | 5161 |  | 
|  | 5162 | // has known usage? | 
|  | 5163 | switch (paa->usage) { | 
|  | 5164 | case AUDIO_USAGE_UNKNOWN: | 
|  | 5165 | case AUDIO_USAGE_MEDIA: | 
|  | 5166 | case AUDIO_USAGE_VOICE_COMMUNICATION: | 
|  | 5167 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: | 
|  | 5168 | case AUDIO_USAGE_ALARM: | 
|  | 5169 | case AUDIO_USAGE_NOTIFICATION: | 
|  | 5170 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: | 
|  | 5171 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: | 
|  | 5172 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: | 
|  | 5173 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: | 
|  | 5174 | case AUDIO_USAGE_NOTIFICATION_EVENT: | 
|  | 5175 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: | 
|  | 5176 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: | 
|  | 5177 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: | 
|  | 5178 | case AUDIO_USAGE_GAME: | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5179 | case AUDIO_USAGE_VIRTUAL_SOURCE: | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5180 | break; | 
|  | 5181 | default: | 
|  | 5182 | return false; | 
|  | 5183 | } | 
|  | 5184 | return true; | 
|  | 5185 | } | 
|  | 5186 |  | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5187 | bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc, | 
|  | 5188 | routing_strategy strategy, uint32_t inPastMs, | 
|  | 5189 | nsecs_t sysTime) const | 
|  | 5190 | { | 
|  | 5191 | if ((sysTime == 0) && (inPastMs != 0)) { | 
|  | 5192 | sysTime = systemTime(); | 
|  | 5193 | } | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5194 | for (int i = 0; i < (int)AUDIO_STREAM_FOR_POLICY_CNT; i++) { | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5195 | if (((getStrategy((audio_stream_type_t)i) == strategy) || | 
|  | 5196 | (NUM_STRATEGIES == strategy)) && | 
|  | 5197 | outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) { | 
|  | 5198 | return true; | 
|  | 5199 | } | 
|  | 5200 | } | 
|  | 5201 | return false; | 
|  | 5202 | } | 
|  | 5203 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5204 | audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage) | 
|  | 5205 | { | 
|  | 5206 | return mEngine->getForceUse(usage); | 
|  | 5207 | } | 
|  | 5208 |  | 
|  | 5209 | bool AudioPolicyManager::isInCall() | 
|  | 5210 | { | 
|  | 5211 | return isStateInCall(mEngine->getPhoneState()); | 
|  | 5212 | } | 
|  | 5213 |  | 
|  | 5214 | bool AudioPolicyManager::isStateInCall(int state) | 
|  | 5215 | { | 
|  | 5216 | return is_state_in_call(state); | 
|  | 5217 | } | 
|  | 5218 |  | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5219 | void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc) | 
|  | 5220 | { | 
|  | 5221 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--)  { | 
|  | 5222 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); | 
|  | 5223 | if (sourceDesc->mDevice->equals(deviceDesc)) { | 
|  | 5224 | ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle()); | 
|  | 5225 | stopAudioSource(sourceDesc->getHandle()); | 
|  | 5226 | } | 
|  | 5227 | } | 
|  | 5228 |  | 
|  | 5229 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--)  { | 
|  | 5230 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); | 
|  | 5231 | bool release = false; | 
|  | 5232 | for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++)  { | 
|  | 5233 | const struct audio_port_config *source = &patchDesc->mPatch.sources[j]; | 
|  | 5234 | if (source->type == AUDIO_PORT_TYPE_DEVICE && | 
|  | 5235 | source->ext.device.type == deviceDesc->type()) { | 
|  | 5236 | release = true; | 
|  | 5237 | } | 
|  | 5238 | } | 
|  | 5239 | for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++)  { | 
|  | 5240 | const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j]; | 
|  | 5241 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && | 
|  | 5242 | sink->ext.device.type == deviceDesc->type()) { | 
|  | 5243 | release = true; | 
|  | 5244 | } | 
|  | 5245 | } | 
|  | 5246 | if (release) { | 
|  | 5247 | ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle); | 
|  | 5248 | releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid); | 
|  | 5249 | } | 
|  | 5250 | } | 
|  | 5251 | } | 
|  | 5252 |  | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5253 | // Modify the list of surround sound formats supported. | 
| Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5254 | void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) { | 
|  | 5255 | FormatVector &formats = *formatsPtr; | 
| Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5256 | // TODO Set this based on Config properties. | 
|  | 5257 | const bool alwaysForceAC3 = true; | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5258 |  | 
|  | 5259 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( | 
|  | 5260 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); | 
| Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5261 | ALOGD("%s: forced use = %d", __FUNCTION__, forceUse); | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5262 |  | 
|  | 5263 | // Analyze original support for various formats. | 
| Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5264 | bool supportsAC3 = false; | 
|  | 5265 | bool supportsOtherSurround = false; | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5266 | bool supportsIEC61937 = false; | 
|  | 5267 | for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) { | 
|  | 5268 | audio_format_t format = formats[formatIndex]; | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5269 | switch (format) { | 
|  | 5270 | case AUDIO_FORMAT_AC3: | 
| Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5271 | supportsAC3 = true; | 
|  | 5272 | break; | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5273 | case AUDIO_FORMAT_E_AC3: | 
|  | 5274 | case AUDIO_FORMAT_DTS: | 
|  | 5275 | case AUDIO_FORMAT_DTS_HD: | 
| Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5276 | supportsOtherSurround = true; | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5277 | break; | 
|  | 5278 | case AUDIO_FORMAT_IEC61937: | 
|  | 5279 | supportsIEC61937 = true; | 
|  | 5280 | break; | 
|  | 5281 | default: | 
|  | 5282 | break; | 
|  | 5283 | } | 
|  | 5284 | } | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5285 |  | 
|  | 5286 | // Modify formats based on surround preferences. | 
|  | 5287 | // If NEVER, remove support for surround formats. | 
| Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5288 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { | 
|  | 5289 | if (supportsAC3 || supportsOtherSurround || supportsIEC61937) { | 
|  | 5290 | // Remove surround sound related formats. | 
|  | 5291 | for (size_t formatIndex = 0; formatIndex < formats.size(); ) { | 
|  | 5292 | audio_format_t format = formats[formatIndex]; | 
|  | 5293 | switch(format) { | 
|  | 5294 | case AUDIO_FORMAT_AC3: | 
|  | 5295 | case AUDIO_FORMAT_E_AC3: | 
|  | 5296 | case AUDIO_FORMAT_DTS: | 
|  | 5297 | case AUDIO_FORMAT_DTS_HD: | 
|  | 5298 | case AUDIO_FORMAT_IEC61937: | 
| Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5299 | formats.removeAt(formatIndex); | 
|  | 5300 | break; | 
|  | 5301 | default: | 
|  | 5302 | formatIndex++; // keep it | 
|  | 5303 | break; | 
|  | 5304 | } | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5305 | } | 
| Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5306 | supportsAC3 = false; | 
|  | 5307 | supportsOtherSurround = false; | 
|  | 5308 | supportsIEC61937 = false; | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5309 | } | 
| Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5310 | } else { // AUTO or ALWAYS | 
|  | 5311 | // Most TVs support AC3 even if they do not report it in the EDID. | 
|  | 5312 | if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)) | 
|  | 5313 | && !supportsAC3) { | 
|  | 5314 | formats.add(AUDIO_FORMAT_AC3); | 
|  | 5315 | supportsAC3 = true; | 
|  | 5316 | } | 
|  | 5317 |  | 
|  | 5318 | // If ALWAYS, add support for raw surround formats if all are missing. | 
|  | 5319 | // This assumes that if any of these formats are reported by the HAL | 
|  | 5320 | // then the report is valid and should not be modified. | 
|  | 5321 | if ((forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) | 
|  | 5322 | && !supportsOtherSurround) { | 
|  | 5323 | formats.add(AUDIO_FORMAT_E_AC3); | 
|  | 5324 | formats.add(AUDIO_FORMAT_DTS); | 
|  | 5325 | formats.add(AUDIO_FORMAT_DTS_HD); | 
|  | 5326 | supportsOtherSurround = true; | 
|  | 5327 | } | 
|  | 5328 |  | 
|  | 5329 | // Add support for IEC61937 if any raw surround supported. | 
|  | 5330 | // The HAL could do this but add it here, just in case. | 
|  | 5331 | if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) { | 
|  | 5332 | formats.add(AUDIO_FORMAT_IEC61937); | 
|  | 5333 | supportsIEC61937 = true; | 
|  | 5334 | } | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5335 | } | 
| Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5336 | } | 
|  | 5337 |  | 
|  | 5338 | // Modify the list of channel masks supported. | 
|  | 5339 | void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) { | 
|  | 5340 | ChannelsVector &channelMasks = *channelMasksPtr; | 
|  | 5341 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( | 
|  | 5342 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); | 
|  | 5343 |  | 
|  | 5344 | // If NEVER, then remove support for channelMasks > stereo. | 
|  | 5345 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { | 
|  | 5346 | for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) { | 
|  | 5347 | audio_channel_mask_t channelMask = channelMasks[maskIndex]; | 
|  | 5348 | if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) { | 
|  | 5349 | ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask); | 
|  | 5350 | channelMasks.removeAt(maskIndex); | 
|  | 5351 | } else { | 
|  | 5352 | maskIndex++; | 
|  | 5353 | } | 
|  | 5354 | } | 
|  | 5355 | // If ALWAYS, then make sure we at least support 5.1 | 
|  | 5356 | } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { | 
|  | 5357 | bool supports5dot1 = false; | 
|  | 5358 | // Are there any channel masks that can be considered "surround"? | 
|  | 5359 | for (size_t maskIndex = 0; maskIndex < channelMasks.size(); maskIndex++) { | 
|  | 5360 | audio_channel_mask_t channelMask = channelMasks[maskIndex]; | 
|  | 5361 | if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) { | 
|  | 5362 | supports5dot1 = true; | 
|  | 5363 | break; | 
|  | 5364 | } | 
|  | 5365 | } | 
|  | 5366 | // If not then add 5.1 support. | 
|  | 5367 | if (!supports5dot1) { | 
|  | 5368 | channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1); | 
|  | 5369 | ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__); | 
|  | 5370 | } | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5371 | } | 
|  | 5372 | } | 
|  | 5373 |  | 
| Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5374 | void AudioPolicyManager::updateAudioProfiles(audio_devices_t device, | 
|  | 5375 | audio_io_handle_t ioHandle, | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5376 | AudioProfileVector &profiles) | 
|  | 5377 | { | 
|  | 5378 | String8 reply; | 
|  | 5379 | char *value; | 
| Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5380 |  | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5381 | // Format MUST be checked first to update the list of AudioProfile | 
|  | 5382 | if (profiles.hasDynamicFormat()) { | 
|  | 5383 | reply = mpClientInterface->getParameters(ioHandle, | 
|  | 5384 | String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS)); | 
| Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5385 | ALOGV("%s: supported formats %s", __FUNCTION__, reply.string()); | 
| Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5386 | AudioParameter repliedParameters(reply); | 
|  | 5387 | if (repliedParameters.get( | 
|  | 5388 | String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS), reply) != NO_ERROR) { | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5389 | ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__); | 
|  | 5390 | return; | 
|  | 5391 | } | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5392 | FormatVector formats = formatsFromString(reply.string()); | 
| Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5393 | if (device == AUDIO_DEVICE_OUT_HDMI) { | 
| Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5394 | filterSurroundFormats(&formats); | 
| Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5395 | } | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5396 | profiles.setFormats(formats); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5397 | } | 
|  | 5398 | const FormatVector &supportedFormats = profiles.getSupportedFormats(); | 
|  | 5399 |  | 
| Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5400 | for (size_t formatIndex = 0; formatIndex < supportedFormats.size(); formatIndex++) { | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5401 | audio_format_t format = supportedFormats[formatIndex]; | 
| Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5402 | ChannelsVector channelMasks; | 
|  | 5403 | SampleRateVector samplingRates; | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5404 | AudioParameter requestedParameters; | 
|  | 5405 | requestedParameters.addInt(String8(AUDIO_PARAMETER_STREAM_FORMAT), format); | 
|  | 5406 |  | 
|  | 5407 | if (profiles.hasDynamicRateFor(format)) { | 
|  | 5408 | reply = mpClientInterface->getParameters(ioHandle, | 
|  | 5409 | requestedParameters.toString() + ";" + | 
|  | 5410 | AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES); | 
|  | 5411 | ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string()); | 
| Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5412 | AudioParameter repliedParameters(reply); | 
|  | 5413 | if (repliedParameters.get( | 
|  | 5414 | String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES), reply) == NO_ERROR) { | 
|  | 5415 | samplingRates = samplingRatesFromString(reply.string()); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5416 | } | 
|  | 5417 | } | 
|  | 5418 | if (profiles.hasDynamicChannelsFor(format)) { | 
|  | 5419 | reply = mpClientInterface->getParameters(ioHandle, | 
|  | 5420 | requestedParameters.toString() + ";" + | 
|  | 5421 | AUDIO_PARAMETER_STREAM_SUP_CHANNELS); | 
|  | 5422 | ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string()); | 
| Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5423 | AudioParameter repliedParameters(reply); | 
|  | 5424 | if (repliedParameters.get( | 
|  | 5425 | String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS), reply) == NO_ERROR) { | 
|  | 5426 | channelMasks = channelMasksFromString(reply.string()); | 
| Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5427 | if (device == AUDIO_DEVICE_OUT_HDMI) { | 
|  | 5428 | filterSurroundChannelMasks(&channelMasks); | 
|  | 5429 | } | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5430 | } | 
|  | 5431 | } | 
| Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5432 | profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates)); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5433 | } | 
|  | 5434 | } | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5435 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5436 | }; // namespace android |