| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2009 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
| Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 17 | #define LOG_TAG "APM_AudioPolicyManager" | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 | 
|  | 19 |  | 
|  | 20 | //#define VERY_VERBOSE_LOGGING | 
|  | 21 | #ifdef VERY_VERBOSE_LOGGING | 
|  | 22 | #define ALOGVV ALOGV | 
|  | 23 | #else | 
|  | 24 | #define ALOGVV(a...) do { } while(0) | 
|  | 25 | #endif | 
|  | 26 |  | 
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 27 | #define AUDIO_POLICY_XML_CONFIG_FILE "/system/etc/audio_policy_configuration.xml" | 
|  | 28 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 29 | #include <inttypes.h> | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 30 | #include <math.h> | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 31 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 32 | #include <AudioPolicyManagerInterface.h> | 
|  | 33 | #include <AudioPolicyEngineInstance.h> | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 34 | #include <cutils/properties.h> | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 35 | #include <utils/Log.h> | 
|  | 36 | #include <hardware/audio.h> | 
|  | 37 | #include <hardware/audio_effect.h> | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 38 | #include <media/AudioParameter.h> | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 39 | #include <media/AudioPolicyHelper.h> | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 40 | #include <soundtrigger/SoundTrigger.h> | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 41 | #include "AudioPolicyManager.h" | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 42 | #ifndef USE_XML_AUDIO_POLICY_CONF | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 43 | #include <ConfigParsingUtils.h> | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 44 | #include <StreamDescriptor.h> | 
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 45 | #endif | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 46 | #include <Serializer.h> | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 47 | #include "TypeConverter.h" | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 48 | #include <policy.h> | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 49 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 50 | namespace android { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 51 |  | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 52 | //FIXME: workaround for truncated touch sounds | 
|  | 53 | // to be removed when the problem is handled by system UI | 
|  | 54 | #define TOUCH_SOUND_FIXED_DELAY_MS 100 | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 55 | // ---------------------------------------------------------------------------- | 
|  | 56 | // AudioPolicyInterface implementation | 
|  | 57 | // ---------------------------------------------------------------------------- | 
|  | 58 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 59 | status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device, | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 60 | audio_policy_dev_state_t state, | 
|  | 61 | const char *device_address, | 
|  | 62 | const char *device_name) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 63 | { | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 64 | return setDeviceConnectionStateInt(device, state, device_address, device_name); | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 65 | } | 
|  | 66 |  | 
|  | 67 | status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device, | 
| Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 68 | audio_policy_dev_state_t state, | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 69 | const char *device_address, | 
|  | 70 | const char *device_name) | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 71 | { | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 72 | ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s", | 
|  | 73 | -            device, state, device_address, device_name); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 74 |  | 
|  | 75 | // connect/disconnect only 1 device at a time | 
|  | 76 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; | 
|  | 77 |  | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 78 | sp<DeviceDescriptor> devDesc = | 
|  | 79 | mHwModules.getDeviceDescriptor(device, device_address, device_name); | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 80 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 81 | // handle output devices | 
|  | 82 | if (audio_is_output_device(device)) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 83 | SortedVector <audio_io_handle_t> outputs; | 
|  | 84 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 85 | ssize_t index = mAvailableOutputDevices.indexOf(devDesc); | 
|  | 86 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 87 | // save a copy of the opened output descriptors before any output is opened or closed | 
|  | 88 | // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies() | 
|  | 89 | mPreviousOutputs = mOutputs; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 90 | switch (state) | 
|  | 91 | { | 
|  | 92 | // handle output device connection | 
| Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 93 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 94 | if (index >= 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 95 | ALOGW("setDeviceConnectionState() device already connected: %x", device); | 
|  | 96 | return INVALID_OPERATION; | 
|  | 97 | } | 
|  | 98 | ALOGV("setDeviceConnectionState() connecting device %x", device); | 
|  | 99 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 100 | // register new device as available | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 101 | index = mAvailableOutputDevices.add(devDesc); | 
|  | 102 | if (index >= 0) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 103 | sp<HwModule> module = mHwModules.getModuleForDevice(device); | 
| Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 104 | if (module == 0) { | 
|  | 105 | ALOGD("setDeviceConnectionState() could not find HW module for device %08x", | 
|  | 106 | device); | 
|  | 107 | mAvailableOutputDevices.remove(devDesc); | 
|  | 108 | return INVALID_OPERATION; | 
|  | 109 | } | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 110 | mAvailableOutputDevices[index]->attach(module); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 111 | } else { | 
|  | 112 | return NO_MEMORY; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 113 | } | 
|  | 114 |  | 
| Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 115 | if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) { | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 116 | mAvailableOutputDevices.remove(devDesc); | 
|  | 117 | return INVALID_OPERATION; | 
|  | 118 | } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 119 | // Propagate device availability to Engine | 
|  | 120 | mEngine->setDeviceConnectionState(devDesc, state); | 
|  | 121 |  | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 122 | // outputs should never be empty here | 
|  | 123 | ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():" | 
|  | 124 | "checkOutputsForDevice() returned no outputs but status OK"); | 
|  | 125 | ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs", | 
|  | 126 | outputs.size()); | 
| Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 127 |  | 
| Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 128 | // Send connect to HALs | 
| Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 129 | AudioParameter param = AudioParameter(devDesc->mAddress); | 
|  | 130 | param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device); | 
|  | 131 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); | 
|  | 132 |  | 
|  | 133 | } break; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 134 | // handle output device disconnection | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 135 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 136 | if (index < 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 137 | ALOGW("setDeviceConnectionState() device not connected: %x", device); | 
|  | 138 | return INVALID_OPERATION; | 
|  | 139 | } | 
|  | 140 |  | 
| Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 141 | ALOGV("setDeviceConnectionState() disconnecting output device %x", device); | 
|  | 142 |  | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 143 | // Send Disconnect to HALs | 
| Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 144 | AudioParameter param = AudioParameter(devDesc->mAddress); | 
| Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 145 | param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device); | 
|  | 146 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); | 
|  | 147 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 148 | // remove device from available output devices | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 149 | mAvailableOutputDevices.remove(devDesc); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 150 |  | 
| Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 151 | checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress); | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 152 |  | 
|  | 153 | // Propagate device availability to Engine | 
|  | 154 | mEngine->setDeviceConnectionState(devDesc, state); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 155 | } break; | 
|  | 156 |  | 
|  | 157 | default: | 
|  | 158 | ALOGE("setDeviceConnectionState() invalid state: %x", state); | 
|  | 159 | return BAD_VALUE; | 
|  | 160 | } | 
|  | 161 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 162 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP | 
|  | 163 | // output is suspended before any tracks are moved to it | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 164 | checkA2dpSuspend(); | 
|  | 165 | checkOutputForAllStrategies(); | 
|  | 166 | // outputs must be closed after checkOutputForAllStrategies() is executed | 
|  | 167 | if (!outputs.isEmpty()) { | 
|  | 168 | for (size_t i = 0; i < outputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 169 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 170 | // close unused outputs after device disconnection or direct outputs that have been | 
|  | 171 | // opened by checkOutputsForDevice() to query dynamic parameters | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 172 | if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) || | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 173 | (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) && | 
|  | 174 | (desc->mDirectOpenCount == 0))) { | 
|  | 175 | closeOutput(outputs[i]); | 
|  | 176 | } | 
|  | 177 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 178 | // check again after closing A2DP output to reset mA2dpSuspended if needed | 
|  | 179 | checkA2dpSuspend(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 180 | } | 
|  | 181 |  | 
|  | 182 | updateDevicesAndOutputs(); | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 183 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 184 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); | 
|  | 185 | updateCallRouting(newDevice); | 
|  | 186 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 187 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 188 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
|  | 189 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) { | 
|  | 190 | audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 191 | // do not force device change on duplicated output because if device is 0, it will | 
|  | 192 | // also force a device 0 for the two outputs it is duplicated to which may override | 
|  | 193 | // a valid device selection on those outputs. | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 194 | bool force = !desc->isDuplicated() | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 195 | && (!device_distinguishes_on_address(device) | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 196 | // always force when disconnecting (a non-duplicated device) | 
|  | 197 | || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 198 | setOutputDevice(desc, newDevice, force, 0); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 199 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 200 | } | 
|  | 201 |  | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 202 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { | 
|  | 203 | cleanUpForDevice(devDesc); | 
|  | 204 | } | 
|  | 205 |  | 
| Eric Laurent | 72aa32f | 2014-05-30 18:51:48 -0700 | [diff] [blame] | 206 | mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | b71e58b | 2014-05-29 16:08:11 -0700 | [diff] [blame] | 207 | return NO_ERROR; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 208 | }  // end if is output device | 
|  | 209 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 210 | // handle input devices | 
|  | 211 | if (audio_is_input_device(device)) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 212 | SortedVector <audio_io_handle_t> inputs; | 
|  | 213 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 214 | ssize_t index = mAvailableInputDevices.indexOf(devDesc); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 215 | switch (state) | 
|  | 216 | { | 
|  | 217 | // handle input device connection | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 218 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 219 | if (index >= 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 220 | ALOGW("setDeviceConnectionState() device already connected: %d", device); | 
|  | 221 | return INVALID_OPERATION; | 
|  | 222 | } | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 223 | sp<HwModule> module = mHwModules.getModuleForDevice(device); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 224 | if (module == NULL) { | 
|  | 225 | ALOGW("setDeviceConnectionState(): could not find HW module for device %08x", | 
|  | 226 | device); | 
|  | 227 | return INVALID_OPERATION; | 
|  | 228 | } | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 229 | if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 230 | return INVALID_OPERATION; | 
|  | 231 | } | 
|  | 232 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 233 | index = mAvailableInputDevices.add(devDesc); | 
|  | 234 | if (index >= 0) { | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 235 | mAvailableInputDevices[index]->attach(module); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 236 | } else { | 
|  | 237 | return NO_MEMORY; | 
|  | 238 | } | 
| Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 239 |  | 
|  | 240 | // Set connect to HALs | 
|  | 241 | AudioParameter param = AudioParameter(devDesc->mAddress); | 
|  | 242 | param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device); | 
|  | 243 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); | 
|  | 244 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 245 | // Propagate device availability to Engine | 
|  | 246 | mEngine->setDeviceConnectionState(devDesc, state); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 247 | } break; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 248 |  | 
|  | 249 | // handle input device disconnection | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 250 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 251 | if (index < 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 252 | ALOGW("setDeviceConnectionState() device not connected: %d", device); | 
|  | 253 | return INVALID_OPERATION; | 
|  | 254 | } | 
| Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 255 |  | 
|  | 256 | ALOGV("setDeviceConnectionState() disconnecting input device %x", device); | 
|  | 257 |  | 
|  | 258 | // Set Disconnect to HALs | 
| Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 259 | AudioParameter param = AudioParameter(devDesc->mAddress); | 
| Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 260 | param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device); | 
|  | 261 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); | 
|  | 262 |  | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 263 | checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 264 | mAvailableInputDevices.remove(devDesc); | 
| Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 265 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 266 | // Propagate device availability to Engine | 
|  | 267 | mEngine->setDeviceConnectionState(devDesc, state); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 268 | } break; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 269 |  | 
|  | 270 | default: | 
|  | 271 | ALOGE("setDeviceConnectionState() invalid state: %x", state); | 
|  | 272 | return BAD_VALUE; | 
|  | 273 | } | 
|  | 274 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 275 | closeAllInputs(); | 
| Eric Laurent | 5f5fca5 | 2016-08-04 11:48:57 -0700 | [diff] [blame] | 276 | // As the input device list can impact the output device selection, update | 
|  | 277 | // getDeviceForStrategy() cache | 
|  | 278 | updateDevicesAndOutputs(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 279 |  | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 280 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 281 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); | 
|  | 282 | updateCallRouting(newDevice); | 
|  | 283 | } | 
|  | 284 |  | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 285 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { | 
|  | 286 | cleanUpForDevice(devDesc); | 
|  | 287 | } | 
|  | 288 |  | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 289 | mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 290 | return NO_ERROR; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 291 | } // end if is input device | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 292 |  | 
|  | 293 | ALOGW("setDeviceConnectionState() invalid device: %x", device); | 
|  | 294 | return BAD_VALUE; | 
|  | 295 | } | 
|  | 296 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 297 | audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 298 | const char *device_address) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 299 | { | 
| Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 300 | sp<DeviceDescriptor> devDesc = | 
|  | 301 | mHwModules.getDeviceDescriptor(device, device_address, "", | 
|  | 302 | (strlen(device_address) != 0)/*matchAddress*/); | 
|  | 303 |  | 
|  | 304 | if (devDesc == 0) { | 
|  | 305 | ALOGW("getDeviceConnectionState() undeclared device, type %08x, address: %s", | 
|  | 306 | device, device_address); | 
|  | 307 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; | 
|  | 308 | } | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 309 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 310 | DeviceVector *deviceVector; | 
|  | 311 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 312 | if (audio_is_output_device(device)) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 313 | deviceVector = &mAvailableOutputDevices; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 314 | } else if (audio_is_input_device(device)) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 315 | deviceVector = &mAvailableInputDevices; | 
|  | 316 | } else { | 
|  | 317 | ALOGW("getDeviceConnectionState() invalid device type %08x", device); | 
|  | 318 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 319 | } | 
| Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 320 |  | 
|  | 321 | return (deviceVector->getDevice(device, String8(device_address)) != 0) ? | 
|  | 322 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; | 
| Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 323 | } | 
|  | 324 |  | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 325 | uint32_t AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, uint32_t delayMs) | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 326 | { | 
|  | 327 | bool createTxPatch = false; | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 328 | status_t status; | 
|  | 329 | audio_patch_handle_t afPatchHandle; | 
|  | 330 | DeviceVector deviceList; | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 331 | uint32_t muteWaitMs = 0; | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 332 |  | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 333 | if(!hasPrimaryOutput()) { | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 334 | return muteWaitMs; | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 335 | } | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 336 | audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 337 | ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice); | 
|  | 338 |  | 
|  | 339 | // release existing RX patch if any | 
|  | 340 | if (mCallRxPatch != 0) { | 
|  | 341 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); | 
|  | 342 | mCallRxPatch.clear(); | 
|  | 343 | } | 
|  | 344 | // release TX patch if any | 
|  | 345 | if (mCallTxPatch != 0) { | 
|  | 346 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); | 
|  | 347 | mCallTxPatch.clear(); | 
|  | 348 | } | 
|  | 349 |  | 
|  | 350 | // If the RX device is on the primary HW module, then use legacy routing method for voice calls | 
|  | 351 | // via setOutputDevice() on primary output. | 
|  | 352 | // Otherwise, create two audio patches for TX and RX path. | 
|  | 353 | if (availablePrimaryOutputDevices() & rxDevice) { | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 354 | muteWaitMs = setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 355 | // If the TX device is also on the primary HW module, setOutputDevice() will take care | 
|  | 356 | // of it due to legacy implementation. If not, create a patch. | 
|  | 357 | if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN) | 
|  | 358 | == AUDIO_DEVICE_NONE) { | 
|  | 359 | createTxPatch = true; | 
|  | 360 | } | 
| Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 361 | } else { // create RX path audio patch | 
|  | 362 | struct audio_patch patch; | 
|  | 363 |  | 
|  | 364 | patch.num_sources = 1; | 
|  | 365 | patch.num_sinks = 1; | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 366 | deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice); | 
|  | 367 | ALOG_ASSERT(!deviceList.isEmpty(), | 
|  | 368 | "updateCallRouting() selected device not in output device list"); | 
|  | 369 | sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0); | 
|  | 370 | deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX); | 
|  | 371 | ALOG_ASSERT(!deviceList.isEmpty(), | 
|  | 372 | "updateCallRouting() no telephony RX device"); | 
|  | 373 | sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0); | 
|  | 374 |  | 
|  | 375 | rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]); | 
|  | 376 | rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]); | 
|  | 377 |  | 
|  | 378 | // request to reuse existing output stream if one is already opened to reach the RX device | 
|  | 379 | SortedVector<audio_io_handle_t> outputs = | 
|  | 380 | getOutputsForDevice(rxDevice, mOutputs); | 
| Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 381 | audio_io_handle_t output = selectOutput(outputs, | 
|  | 382 | AUDIO_OUTPUT_FLAG_NONE, | 
|  | 383 | AUDIO_FORMAT_INVALID); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 384 | if (output != AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 385 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 386 | ALOG_ASSERT(!outputDesc->isDuplicated(), | 
|  | 387 | "updateCallRouting() RX device output is duplicated"); | 
|  | 388 | outputDesc->toAudioPortConfig(&patch.sources[1]); | 
| Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 389 | patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 390 | patch.num_sources = 2; | 
|  | 391 | } | 
|  | 392 |  | 
|  | 393 | afPatchHandle = AUDIO_PATCH_HANDLE_NONE; | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 394 | status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, delayMs); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 395 | ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch", | 
|  | 396 | status); | 
|  | 397 | if (status == NO_ERROR) { | 
| François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 398 | mCallRxPatch = new AudioPatch(&patch, mUidCached); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 399 | mCallRxPatch->mAfPatchHandle = afPatchHandle; | 
|  | 400 | mCallRxPatch->mUid = mUidCached; | 
|  | 401 | } | 
|  | 402 | createTxPatch = true; | 
|  | 403 | } | 
| Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 404 | if (createTxPatch) { // create TX path audio patch | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 405 | struct audio_patch patch; | 
| Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 406 |  | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 407 | patch.num_sources = 1; | 
|  | 408 | patch.num_sinks = 1; | 
|  | 409 | deviceList = mAvailableInputDevices.getDevicesFromType(txDevice); | 
|  | 410 | ALOG_ASSERT(!deviceList.isEmpty(), | 
|  | 411 | "updateCallRouting() selected device not in input device list"); | 
|  | 412 | sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0); | 
|  | 413 | txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]); | 
|  | 414 | deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX); | 
|  | 415 | ALOG_ASSERT(!deviceList.isEmpty(), | 
|  | 416 | "updateCallRouting() no telephony TX device"); | 
|  | 417 | sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0); | 
|  | 418 | txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]); | 
|  | 419 |  | 
|  | 420 | SortedVector<audio_io_handle_t> outputs = | 
|  | 421 | getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs); | 
| Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 422 | audio_io_handle_t output = selectOutput(outputs, | 
|  | 423 | AUDIO_OUTPUT_FLAG_NONE, | 
|  | 424 | AUDIO_FORMAT_INVALID); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 425 | // request to reuse existing output stream if one is already opened to reach the TX | 
|  | 426 | // path output device | 
|  | 427 | if (output != AUDIO_IO_HANDLE_NONE) { | 
|  | 428 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
|  | 429 | ALOG_ASSERT(!outputDesc->isDuplicated(), | 
|  | 430 | "updateCallRouting() RX device output is duplicated"); | 
|  | 431 | outputDesc->toAudioPortConfig(&patch.sources[1]); | 
| Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 432 | patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 433 | patch.num_sources = 2; | 
|  | 434 | } | 
|  | 435 |  | 
| Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 436 | // terminate active capture if on the same HW module as the call TX source device | 
|  | 437 | // FIXME: would be better to refine to only inputs whose profile connects to the | 
|  | 438 | // call TX device but this information is not in the audio patch and logic here must be | 
|  | 439 | // symmetric to the one in startInput() | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 440 | audio_io_handle_t activeInput = mInputs.getActiveInput(); | 
|  | 441 | if (activeInput != 0) { | 
|  | 442 | sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput); | 
|  | 443 | if (activeDesc->getModuleHandle() == txSourceDeviceDesc->getModuleHandle()) { | 
|  | 444 | //FIXME: consider all active sessions | 
|  | 445 | AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions(); | 
|  | 446 | audio_session_t activeSession = activeSessions.keyAt(0); | 
|  | 447 | stopInput(activeInput, activeSession); | 
|  | 448 | releaseInput(activeInput, activeSession); | 
| Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 449 | } | 
|  | 450 | } | 
|  | 451 |  | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 452 | afPatchHandle = AUDIO_PATCH_HANDLE_NONE; | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 453 | status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, delayMs); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 454 | ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch", | 
|  | 455 | status); | 
|  | 456 | if (status == NO_ERROR) { | 
| François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 457 | mCallTxPatch = new AudioPatch(&patch, mUidCached); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 458 | mCallTxPatch->mAfPatchHandle = afPatchHandle; | 
|  | 459 | mCallTxPatch->mUid = mUidCached; | 
|  | 460 | } | 
|  | 461 | } | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 462 |  | 
|  | 463 | return muteWaitMs; | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 464 | } | 
|  | 465 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 466 | void AudioPolicyManager::setPhoneState(audio_mode_t state) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 467 | { | 
|  | 468 | ALOGV("setPhoneState() state %d", state); | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 469 | // store previous phone state for management of sonification strategy below | 
|  | 470 | int oldState = mEngine->getPhoneState(); | 
|  | 471 |  | 
|  | 472 | if (mEngine->setPhoneState(state) != NO_ERROR) { | 
|  | 473 | ALOGW("setPhoneState() invalid or same state %d", state); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 474 | return; | 
|  | 475 | } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 476 | /// Opens: can these line be executed after the switch of volume curves??? | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 477 | // if leaving call state, handle special case of active streams | 
|  | 478 | // pertaining to sonification strategy see handleIncallSonification() | 
| Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 479 | if (isStateInCall(oldState)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 480 | ALOGV("setPhoneState() in call state management: new state is %d", state); | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 481 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 482 | handleIncallSonification((audio_stream_type_t)stream, false, true); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 483 | } | 
| Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 484 |  | 
| Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 485 | // force reevaluating accessibility routing when call stops | 
| Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 486 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 487 | } | 
|  | 488 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 489 | /** | 
|  | 490 | * Switching to or from incall state or switching between telephony and VoIP lead to force | 
|  | 491 | * routing command. | 
|  | 492 | */ | 
|  | 493 | bool force = ((is_state_in_call(oldState) != is_state_in_call(state)) | 
|  | 494 | || (is_state_in_call(state) && (state != oldState))); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 495 |  | 
|  | 496 | // check for device and output changes triggered by new phone state | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 497 | checkA2dpSuspend(); | 
|  | 498 | checkOutputForAllStrategies(); | 
|  | 499 | updateDevicesAndOutputs(); | 
|  | 500 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 501 | int delayMs = 0; | 
|  | 502 | if (isStateInCall(state)) { | 
|  | 503 | nsecs_t sysTime = systemTime(); | 
|  | 504 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 505 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 506 | // mute media and sonification strategies and delay device switch by the largest | 
|  | 507 | // latency of any output where either strategy is active. | 
|  | 508 | // 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] | 509 | if ((isStrategyActive(desc, STRATEGY_MEDIA, | 
|  | 510 | SONIFICATION_HEADSET_MUSIC_DELAY, | 
|  | 511 | sysTime) || | 
|  | 512 | isStrategyActive(desc, STRATEGY_SONIFICATION, | 
|  | 513 | SONIFICATION_HEADSET_MUSIC_DELAY, | 
|  | 514 | sysTime)) && | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 515 | (delayMs < (int)desc->latency()*2)) { | 
|  | 516 | delayMs = desc->latency()*2; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 517 | } | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 518 | setStrategyMute(STRATEGY_MEDIA, true, desc); | 
|  | 519 | setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 520 | getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/)); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 521 | setStrategyMute(STRATEGY_SONIFICATION, true, desc); | 
|  | 522 | setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 523 | getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/)); | 
|  | 524 | } | 
|  | 525 | } | 
|  | 526 |  | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 527 | if (hasPrimaryOutput()) { | 
|  | 528 | // Note that despite the fact that getNewOutputDevice() is called on the primary output, | 
|  | 529 | // the device returned is not necessarily reachable via this output | 
|  | 530 | audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); | 
|  | 531 | // force routing command to audio hardware when ending call | 
|  | 532 | // even if no device change is needed | 
|  | 533 | if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) { | 
|  | 534 | rxDevice = mPrimaryOutput->device(); | 
|  | 535 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 536 |  | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 537 | if (state == AUDIO_MODE_IN_CALL) { | 
|  | 538 | updateCallRouting(rxDevice, delayMs); | 
|  | 539 | } else if (oldState == AUDIO_MODE_IN_CALL) { | 
|  | 540 | if (mCallRxPatch != 0) { | 
|  | 541 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); | 
|  | 542 | mCallRxPatch.clear(); | 
|  | 543 | } | 
|  | 544 | if (mCallTxPatch != 0) { | 
|  | 545 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); | 
|  | 546 | mCallTxPatch.clear(); | 
|  | 547 | } | 
|  | 548 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); | 
|  | 549 | } else { | 
|  | 550 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 551 | } | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 552 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 553 | // if entering in call state, handle special case of active streams | 
|  | 554 | // pertaining to sonification strategy see handleIncallSonification() | 
|  | 555 | if (isStateInCall(state)) { | 
|  | 556 | ALOGV("setPhoneState() in call state management: new state is %d", state); | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 557 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 558 | handleIncallSonification((audio_stream_type_t)stream, true, true); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 559 | } | 
| Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 560 |  | 
|  | 561 | // force reevaluating accessibility routing when call starts | 
|  | 562 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 563 | } | 
|  | 564 |  | 
|  | 565 | // 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] | 566 | if (state == AUDIO_MODE_RINGTONE && | 
|  | 567 | isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 568 | mLimitRingtoneVolume = true; | 
|  | 569 | } else { | 
|  | 570 | mLimitRingtoneVolume = false; | 
|  | 571 | } | 
|  | 572 | } | 
|  | 573 |  | 
| Jean-Michel Trivi | 887a9ed | 2015-03-31 18:02:24 -0700 | [diff] [blame] | 574 | audio_mode_t AudioPolicyManager::getPhoneState() { | 
|  | 575 | return mEngine->getPhoneState(); | 
|  | 576 | } | 
|  | 577 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 578 | void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 579 | audio_policy_forced_cfg_t config) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 580 | { | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 581 | ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 582 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 583 | if (mEngine->setForceUse(usage, config) != NO_ERROR) { | 
|  | 584 | ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage); | 
|  | 585 | return; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 586 | } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 587 | bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) || | 
|  | 588 | (usage == AUDIO_POLICY_FORCE_FOR_DOCK) || | 
|  | 589 | (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 590 |  | 
|  | 591 | // check for device and output changes triggered by new force usage | 
|  | 592 | checkA2dpSuspend(); | 
|  | 593 | checkOutputForAllStrategies(); | 
|  | 594 | updateDevicesAndOutputs(); | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 595 |  | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 596 | //FIXME: workaround for truncated touch sounds | 
|  | 597 | // to be removed when the problem is handled by system UI | 
|  | 598 | uint32_t delayMs = 0; | 
|  | 599 | uint32_t waitMs = 0; | 
|  | 600 | if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) { | 
|  | 601 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; | 
|  | 602 | } | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 603 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 604 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/); | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 605 | waitMs = updateCallRouting(newDevice, delayMs); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 606 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 607 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 608 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); | 
|  | 609 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/); | 
|  | 610 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) { | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 611 | waitMs = setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE), | 
|  | 612 | delayMs); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 613 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 614 | if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) { | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 615 | applyStreamVolumes(outputDesc, newDevice, waitMs, true); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 616 | } | 
|  | 617 | } | 
|  | 618 |  | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 619 | audio_io_handle_t activeInput = mInputs.getActiveInput(); | 
|  | 620 | if (activeInput != 0) { | 
|  | 621 | sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput); | 
|  | 622 | audio_devices_t newDevice = getNewInputDevice(activeInput); | 
| Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 623 | // 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] | 624 | if (activeDesc->mProfile->getSupportedDevices().types() & (newDevice & ~AUDIO_DEVICE_BIT_IN)) { | 
|  | 625 | setInputDevice(activeInput, newDevice); | 
| Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 626 | } else { | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 627 | closeInput(activeInput); | 
| Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 628 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 629 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 630 | } | 
|  | 631 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 632 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 633 | { | 
|  | 634 | ALOGV("setSystemProperty() property %s, value %s", property, value); | 
|  | 635 | } | 
|  | 636 |  | 
|  | 637 | // Find a direct output profile compatible with the parameters passed, even if the input flags do | 
|  | 638 | // not explicitly request a direct output | 
| Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 639 | sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput( | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 640 | audio_devices_t device, | 
|  | 641 | uint32_t samplingRate, | 
|  | 642 | audio_format_t format, | 
|  | 643 | audio_channel_mask_t channelMask, | 
|  | 644 | audio_output_flags_t flags) | 
|  | 645 | { | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 646 | // only retain flags that will drive the direct output profile selection | 
|  | 647 | // if explicitly requested | 
|  | 648 | static const uint32_t kRelevantFlags = | 
|  | 649 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); | 
|  | 650 | flags = | 
|  | 651 | (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); | 
|  | 652 |  | 
|  | 653 | sp<IOProfile> profile; | 
|  | 654 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 655 | for (size_t i = 0; i < mHwModules.size(); i++) { | 
|  | 656 | if (mHwModules[i]->mHandle == 0) { | 
|  | 657 | continue; | 
|  | 658 | } | 
|  | 659 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) { | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 660 | sp<IOProfile> curProfile = mHwModules[i]->mOutputProfiles[j]; | 
|  | 661 | if (!curProfile->isCompatibleProfile(device, String8(""), | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 662 | samplingRate, NULL /*updatedSamplingRate*/, | 
|  | 663 | format, NULL /*updatedFormat*/, | 
|  | 664 | channelMask, NULL /*updatedChannelMask*/, | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 665 | flags)) { | 
|  | 666 | continue; | 
|  | 667 | } | 
|  | 668 | // reject profiles not corresponding to a device currently available | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 669 | if ((mAvailableOutputDevices.types() & curProfile->getSupportedDevicesType()) == 0) { | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 670 | continue; | 
|  | 671 | } | 
|  | 672 | // 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] | 673 | if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) { | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 674 | continue; | 
|  | 675 | } | 
|  | 676 | profile = curProfile; | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 677 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 678 | break; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 679 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 680 | } | 
|  | 681 | } | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 682 | return profile; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 683 | } | 
|  | 684 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 685 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 686 | uint32_t samplingRate, | 
|  | 687 | audio_format_t format, | 
|  | 688 | audio_channel_mask_t channelMask, | 
|  | 689 | audio_output_flags_t flags, | 
|  | 690 | const audio_offload_info_t *offloadInfo) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 691 | { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 692 | routing_strategy strategy = getStrategy(stream); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 693 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); | 
|  | 694 | ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x", | 
|  | 695 | device, stream, samplingRate, format, channelMask, flags); | 
|  | 696 |  | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 697 | return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE, | 
|  | 698 | stream, samplingRate,format, channelMask, | 
|  | 699 | flags, offloadInfo); | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 700 | } | 
|  | 701 |  | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 702 | status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, | 
|  | 703 | audio_io_handle_t *output, | 
|  | 704 | audio_session_t session, | 
|  | 705 | audio_stream_type_t *stream, | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 706 | uid_t uid, | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 707 | uint32_t samplingRate, | 
|  | 708 | audio_format_t format, | 
|  | 709 | audio_channel_mask_t channelMask, | 
|  | 710 | audio_output_flags_t flags, | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 711 | audio_port_handle_t selectedDeviceId, | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 712 | const audio_offload_info_t *offloadInfo) | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 713 | { | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 714 | audio_attributes_t attributes; | 
|  | 715 | if (attr != NULL) { | 
|  | 716 | if (!isValidAttributes(attr)) { | 
|  | 717 | ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]", | 
|  | 718 | attr->usage, attr->content_type, attr->flags, | 
|  | 719 | attr->tags); | 
|  | 720 | return BAD_VALUE; | 
|  | 721 | } | 
|  | 722 | attributes = *attr; | 
|  | 723 | } else { | 
|  | 724 | if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) { | 
|  | 725 | ALOGE("getOutputForAttr():  invalid stream type"); | 
|  | 726 | return BAD_VALUE; | 
|  | 727 | } | 
|  | 728 | stream_type_to_audio_attributes(*stream, &attributes); | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 729 | } | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 730 | sp<SwAudioOutputDescriptor> desc; | 
| Jean-Michel Trivi | e8deced | 2016-02-11 12:50:39 -0800 | [diff] [blame] | 731 | if (mPolicyMixes.getOutputForAttr(attributes, uid, desc) == NO_ERROR) { | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 732 | ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr"); | 
| Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 733 | if (!audio_has_proportional_frames(format)) { | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 734 | return BAD_VALUE; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 735 | } | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 736 | *stream = streamTypefromAttributesInt(&attributes); | 
|  | 737 | *output = desc->mIoHandle; | 
|  | 738 | ALOGV("getOutputForAttr() returns output %d", *output); | 
|  | 739 | return NO_ERROR; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 740 | } | 
|  | 741 | if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) { | 
|  | 742 | ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE"); | 
|  | 743 | return BAD_VALUE; | 
|  | 744 | } | 
|  | 745 |  | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 746 | ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x" | 
|  | 747 | " session %d selectedDeviceId %d", | 
|  | 748 | attributes.usage, attributes.content_type, attributes.tags, attributes.flags, | 
|  | 749 | session, selectedDeviceId); | 
|  | 750 |  | 
|  | 751 | *stream = streamTypefromAttributesInt(&attributes); | 
|  | 752 |  | 
|  | 753 | // Explicit routing? | 
|  | 754 | sp<DeviceDescriptor> deviceDesc; | 
|  | 755 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { | 
|  | 756 | if (mAvailableOutputDevices[i]->getId() == selectedDeviceId) { | 
|  | 757 | deviceDesc = mAvailableOutputDevices[i]; | 
|  | 758 | break; | 
|  | 759 | } | 
|  | 760 | } | 
|  | 761 | mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid); | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 762 |  | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 763 | routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes); | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 764 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); | 
| Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 765 |  | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 766 | if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) { | 
| Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 767 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); | 
|  | 768 | } | 
|  | 769 |  | 
| Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 770 | 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] | 771 | device, samplingRate, format, channelMask, flags); | 
|  | 772 |  | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 773 | *output = getOutputForDevice(device, session, *stream, | 
|  | 774 | samplingRate, format, channelMask, | 
|  | 775 | flags, offloadInfo); | 
|  | 776 | if (*output == AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 777 | mOutputRoutes.removeRoute(session); | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 778 | return INVALID_OPERATION; | 
|  | 779 | } | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 780 |  | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 781 | return NO_ERROR; | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 782 | } | 
|  | 783 |  | 
|  | 784 | audio_io_handle_t AudioPolicyManager::getOutputForDevice( | 
|  | 785 | audio_devices_t device, | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 786 | audio_session_t session __unused, | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 787 | audio_stream_type_t stream, | 
|  | 788 | uint32_t samplingRate, | 
|  | 789 | audio_format_t format, | 
|  | 790 | audio_channel_mask_t channelMask, | 
|  | 791 | audio_output_flags_t flags, | 
|  | 792 | const audio_offload_info_t *offloadInfo) | 
|  | 793 | { | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 794 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 795 | status_t status; | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 796 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 797 | #ifdef AUDIO_POLICY_TEST | 
|  | 798 | if (mCurOutput != 0) { | 
|  | 799 | ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d", | 
|  | 800 | mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput); | 
|  | 801 |  | 
|  | 802 | if (mTestOutputs[mCurOutput] == 0) { | 
|  | 803 | ALOGV("getOutput() opening test output"); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 804 | sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL, | 
|  | 805 | mpClientInterface); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 806 | outputDesc->mDevice = mTestDevice; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 807 | outputDesc->mLatency = mTestLatencyMs; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 808 | outputDesc->mFlags = | 
|  | 809 | (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 810 | outputDesc->mRefCount[stream] = 0; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 811 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
|  | 812 | config.sample_rate = mTestSamplingRate; | 
|  | 813 | config.channel_mask = mTestChannels; | 
|  | 814 | config.format = mTestFormat; | 
| Phil Burk | 77cce80 | 2014-08-04 16:18:15 -0700 | [diff] [blame] | 815 | if (offloadInfo != NULL) { | 
|  | 816 | config.offload_info = *offloadInfo; | 
|  | 817 | } | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 818 | status = mpClientInterface->openOutput(0, | 
|  | 819 | &mTestOutputs[mCurOutput], | 
|  | 820 | &config, | 
|  | 821 | &outputDesc->mDevice, | 
|  | 822 | String8(""), | 
|  | 823 | &outputDesc->mLatency, | 
|  | 824 | outputDesc->mFlags); | 
|  | 825 | if (status == NO_ERROR) { | 
|  | 826 | outputDesc->mSamplingRate = config.sample_rate; | 
|  | 827 | outputDesc->mFormat = config.format; | 
|  | 828 | outputDesc->mChannelMask = config.channel_mask; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 829 | AudioParameter outputCmd = AudioParameter(); | 
|  | 830 | outputCmd.addInt(String8("set_id"),mCurOutput); | 
|  | 831 | mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString()); | 
|  | 832 | addOutput(mTestOutputs[mCurOutput], outputDesc); | 
|  | 833 | } | 
|  | 834 | } | 
|  | 835 | return mTestOutputs[mCurOutput]; | 
|  | 836 | } | 
|  | 837 | #endif //AUDIO_POLICY_TEST | 
|  | 838 |  | 
|  | 839 | // open a direct output if required by specified parameters | 
|  | 840 | //force direct flag if offload flag is set: offloading implies a direct output stream | 
|  | 841 | // and all common behaviors are driven by checking only the direct flag | 
|  | 842 | // this should normally be set appropriately in the policy configuration file | 
|  | 843 | if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 844 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 845 | } | 
| Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 846 | if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { | 
|  | 847 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT); | 
|  | 848 | } | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 849 | // only allow deep buffering for music stream type | 
|  | 850 | if (stream != AUDIO_STREAM_MUSIC) { | 
|  | 851 | flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); | 
| Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 852 | } else if (/* stream == AUDIO_STREAM_MUSIC && */ | 
|  | 853 | flags == AUDIO_OUTPUT_FLAG_NONE && | 
|  | 854 | property_get_bool("audio.deep_buffer.media", false /* default_value */)) { | 
|  | 855 | // use DEEP_BUFFER as default output for music stream type | 
|  | 856 | flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER; | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 857 | } | 
| Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 858 | if (stream == AUDIO_STREAM_TTS) { | 
|  | 859 | flags = AUDIO_OUTPUT_FLAG_TTS; | 
|  | 860 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 861 |  | 
| Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 862 | sp<IOProfile> profile; | 
|  | 863 |  | 
|  | 864 | // 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] | 865 | // and not explicitly requested | 
|  | 866 | if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && | 
| Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 867 | audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX && | 
| Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 868 | audio_channel_count_from_out_mask(channelMask) <= 2) { | 
|  | 869 | goto non_direct_output; | 
|  | 870 | } | 
|  | 871 |  | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 872 | // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled. | 
|  | 873 | // This prevents creating an offloaded track and tearing it down immediately after start | 
|  | 874 | // when audioflinger detects there is an active non offloadable effect. | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 875 | // FIXME: We should check the audio session here but we do not have it in this context. | 
|  | 876 | // This may prevent offloading in rare situations where effects are left active by apps | 
|  | 877 | // in the background. | 
| Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 878 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 879 | if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 880 | !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 881 | profile = getProfileForDirectOutput(device, | 
|  | 882 | samplingRate, | 
|  | 883 | format, | 
|  | 884 | channelMask, | 
|  | 885 | (audio_output_flags_t)flags); | 
|  | 886 | } | 
|  | 887 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 888 | if (profile != 0) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 889 | sp<SwAudioOutputDescriptor> outputDesc = NULL; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 890 |  | 
|  | 891 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 892 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 893 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { | 
|  | 894 | outputDesc = desc; | 
|  | 895 | // reuse direct output if currently open and configured with same parameters | 
|  | 896 | if ((samplingRate == outputDesc->mSamplingRate) && | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 897 | audio_formats_match(format, outputDesc->mFormat) && | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 898 | (channelMask == outputDesc->mChannelMask)) { | 
|  | 899 | outputDesc->mDirectOpenCount++; | 
|  | 900 | ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i)); | 
|  | 901 | return mOutputs.keyAt(i); | 
|  | 902 | } | 
|  | 903 | } | 
|  | 904 | } | 
|  | 905 | // close direct output if currently open and configured with different parameters | 
|  | 906 | if (outputDesc != NULL) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 907 | closeOutput(outputDesc->mIoHandle); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 908 | } | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 909 |  | 
|  | 910 | // if the selected profile is offloaded and no offload info was specified, | 
|  | 911 | // create a default one | 
|  | 912 | audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER; | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 913 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) { | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 914 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); | 
|  | 915 | defaultOffloadInfo.sample_rate = samplingRate; | 
|  | 916 | defaultOffloadInfo.channel_mask = channelMask; | 
|  | 917 | defaultOffloadInfo.format = format; | 
|  | 918 | defaultOffloadInfo.stream_type = stream; | 
|  | 919 | defaultOffloadInfo.bit_rate = 0; | 
|  | 920 | defaultOffloadInfo.duration_us = -1; | 
|  | 921 | defaultOffloadInfo.has_video = true; // conservative | 
|  | 922 | defaultOffloadInfo.is_streaming = true; // likely | 
|  | 923 | offloadInfo = &defaultOffloadInfo; | 
|  | 924 | } | 
|  | 925 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 926 | outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 927 | outputDesc->mDevice = device; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 928 | outputDesc->mLatency = 0; | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 929 | outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 930 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
|  | 931 | config.sample_rate = samplingRate; | 
|  | 932 | config.channel_mask = channelMask; | 
|  | 933 | config.format = format; | 
| Phil Burk | 77cce80 | 2014-08-04 16:18:15 -0700 | [diff] [blame] | 934 | if (offloadInfo != NULL) { | 
|  | 935 | config.offload_info = *offloadInfo; | 
|  | 936 | } | 
| Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 937 | status = mpClientInterface->openOutput(profile->getModuleHandle(), | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 938 | &output, | 
|  | 939 | &config, | 
|  | 940 | &outputDesc->mDevice, | 
|  | 941 | String8(""), | 
|  | 942 | &outputDesc->mLatency, | 
|  | 943 | outputDesc->mFlags); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 944 |  | 
|  | 945 | // only accept an output with the requested parameters | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 946 | if (status != NO_ERROR || | 
|  | 947 | (samplingRate != 0 && samplingRate != config.sample_rate) || | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 948 | (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) || | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 949 | (channelMask != 0 && channelMask != config.channel_mask)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 950 | ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d," | 
|  | 951 | "format %d %d, channelMask %04x %04x", output, samplingRate, | 
|  | 952 | outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask, | 
|  | 953 | outputDesc->mChannelMask); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 954 | if (output != AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 955 | mpClientInterface->closeOutput(output); | 
|  | 956 | } | 
| Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 957 | // 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] | 958 | if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) { | 
| Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 959 | goto non_direct_output; | 
|  | 960 | } | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 961 | return AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 962 | } | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 963 | outputDesc->mSamplingRate = config.sample_rate; | 
|  | 964 | outputDesc->mChannelMask = config.channel_mask; | 
|  | 965 | outputDesc->mFormat = config.format; | 
|  | 966 | outputDesc->mRefCount[stream] = 0; | 
|  | 967 | outputDesc->mStopTime[stream] = 0; | 
|  | 968 | outputDesc->mDirectOpenCount = 1; | 
|  | 969 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 970 | audio_io_handle_t srcOutput = getOutputForEffect(); | 
|  | 971 | addOutput(output, outputDesc); | 
|  | 972 | audio_io_handle_t dstOutput = getOutputForEffect(); | 
|  | 973 | if (dstOutput == output) { | 
|  | 974 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput); | 
|  | 975 | } | 
|  | 976 | mPreviousOutputs = mOutputs; | 
|  | 977 | ALOGV("getOutput() returns new direct output %d", output); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 978 | mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 979 | return output; | 
|  | 980 | } | 
|  | 981 |  | 
| Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 982 | non_direct_output: | 
| Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 983 |  | 
|  | 984 | // A request for HW A/V sync cannot fallback to a mixed output because time | 
|  | 985 | // stamps are embedded in audio data | 
|  | 986 | if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { | 
|  | 987 | return AUDIO_IO_HANDLE_NONE; | 
|  | 988 | } | 
|  | 989 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 990 | // ignoring channel mask due to downmix capability in mixer | 
|  | 991 |  | 
|  | 992 | // open a non direct output | 
|  | 993 |  | 
|  | 994 | // for non direct outputs, only PCM is supported | 
|  | 995 | if (audio_is_linear_pcm(format)) { | 
|  | 996 | // get which output is suitable for the specified stream. The actual | 
|  | 997 | // routing change will happen when startOutput() will be called | 
|  | 998 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); | 
|  | 999 |  | 
| Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1000 | // at this stage we should ignore the DIRECT flag as no direct output could be found earlier | 
|  | 1001 | flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT); | 
|  | 1002 | output = selectOutput(outputs, flags, format); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1003 | } | 
|  | 1004 | ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d," | 
|  | 1005 | "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags); | 
|  | 1006 |  | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1007 | ALOGV("  getOutputForDevice() returns output %d", output); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1008 |  | 
|  | 1009 | return output; | 
|  | 1010 | } | 
|  | 1011 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1012 | 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] | 1013 | audio_output_flags_t flags, | 
|  | 1014 | audio_format_t format) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1015 | { | 
|  | 1016 | // select one output among several that provide a path to a particular device or set of | 
|  | 1017 | // devices (the list was previously build by getOutputsForDevice()). | 
|  | 1018 | // The priority is as follows: | 
|  | 1019 | // 1: the output with the highest number of requested policy flags | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1020 | // 2: the output with the bit depth the closest to the requested one | 
|  | 1021 | // 3: the primary output | 
|  | 1022 | // 4: the first output in the list | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1023 |  | 
|  | 1024 | if (outputs.size() == 0) { | 
|  | 1025 | return 0; | 
|  | 1026 | } | 
|  | 1027 | if (outputs.size() == 1) { | 
|  | 1028 | return outputs[0]; | 
|  | 1029 | } | 
|  | 1030 |  | 
|  | 1031 | int maxCommonFlags = 0; | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1032 | audio_io_handle_t outputForFlags = 0; | 
|  | 1033 | audio_io_handle_t outputForPrimary = 0; | 
|  | 1034 | audio_io_handle_t outputForFormat = 0; | 
|  | 1035 | audio_format_t bestFormat = AUDIO_FORMAT_INVALID; | 
|  | 1036 | audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1037 |  | 
|  | 1038 | for (size_t i = 0; i < outputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1039 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1040 | if (!outputDesc->isDuplicated()) { | 
| Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1041 | // if a valid format is specified, skip output if not compatible | 
|  | 1042 | if (format != AUDIO_FORMAT_INVALID) { | 
|  | 1043 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1044 | if (!audio_formats_match(format, outputDesc->mFormat)) { | 
| Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1045 | continue; | 
|  | 1046 | } | 
|  | 1047 | } else if (!audio_is_linear_pcm(format)) { | 
|  | 1048 | continue; | 
|  | 1049 | } | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1050 | if (AudioPort::isBetterFormatMatch( | 
|  | 1051 | outputDesc->mFormat, bestFormat, format)) { | 
|  | 1052 | outputForFormat = outputs[i]; | 
|  | 1053 | bestFormat = outputDesc->mFormat; | 
|  | 1054 | } | 
| Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1055 | } | 
|  | 1056 |  | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1057 | int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags); | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1058 | if (commonFlags >= maxCommonFlags) { | 
|  | 1059 | if (commonFlags == maxCommonFlags) { | 
|  | 1060 | if (AudioPort::isBetterFormatMatch( | 
|  | 1061 | outputDesc->mFormat, bestFormatForFlags, format)) { | 
|  | 1062 | outputForFlags = outputs[i]; | 
|  | 1063 | bestFormatForFlags = outputDesc->mFormat; | 
|  | 1064 | } | 
|  | 1065 | } else { | 
|  | 1066 | outputForFlags = outputs[i]; | 
|  | 1067 | maxCommonFlags = commonFlags; | 
|  | 1068 | bestFormatForFlags = outputDesc->mFormat; | 
|  | 1069 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1070 | ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags); | 
|  | 1071 | } | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1072 | if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1073 | outputForPrimary = outputs[i]; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1074 | } | 
|  | 1075 | } | 
|  | 1076 | } | 
|  | 1077 |  | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1078 | if (outputForFlags != 0) { | 
|  | 1079 | return outputForFlags; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1080 | } | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1081 | if (outputForFormat != 0) { | 
|  | 1082 | return outputForFormat; | 
|  | 1083 | } | 
|  | 1084 | if (outputForPrimary != 0) { | 
|  | 1085 | return outputForPrimary; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1086 | } | 
|  | 1087 |  | 
|  | 1088 | return outputs[0]; | 
|  | 1089 | } | 
|  | 1090 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1091 | status_t AudioPolicyManager::startOutput(audio_io_handle_t output, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1092 | audio_stream_type_t stream, | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1093 | audio_session_t session) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1094 | { | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1095 | ALOGV("startOutput() output %d, stream %d, session %d", | 
|  | 1096 | output, stream, session); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1097 | ssize_t index = mOutputs.indexOfKey(output); | 
|  | 1098 | if (index < 0) { | 
|  | 1099 | ALOGW("startOutput() unknown output %d", output); | 
|  | 1100 | return BAD_VALUE; | 
|  | 1101 | } | 
|  | 1102 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1103 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); | 
|  | 1104 |  | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1105 | // Routing? | 
|  | 1106 | mOutputRoutes.incRouteActivity(session); | 
|  | 1107 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1108 | audio_devices_t newDevice; | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1109 | AudioMix *policyMix = NULL; | 
|  | 1110 | const char *address = NULL; | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1111 | if (outputDesc->mPolicyMix != NULL) { | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1112 | policyMix = outputDesc->mPolicyMix; | 
|  | 1113 | address = policyMix->mDeviceAddress.string(); | 
|  | 1114 | if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { | 
|  | 1115 | newDevice = policyMix->mDeviceType; | 
|  | 1116 | } else { | 
|  | 1117 | newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; | 
|  | 1118 | } | 
| Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 1119 | } else if (mOutputRoutes.hasRouteChanged(session)) { | 
|  | 1120 | newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1121 | checkStrategyRoute(getStrategy(stream), output); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1122 | } else { | 
|  | 1123 | newDevice = AUDIO_DEVICE_NONE; | 
|  | 1124 | } | 
|  | 1125 |  | 
|  | 1126 | uint32_t delayMs = 0; | 
|  | 1127 |  | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1128 | status_t status = startSource(outputDesc, stream, newDevice, address, &delayMs); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1129 |  | 
|  | 1130 | if (status != NO_ERROR) { | 
|  | 1131 | mOutputRoutes.decRouteActivity(session); | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1132 | return status; | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1133 | } | 
|  | 1134 | // Automatically enable the remote submix input when output is started on a re routing mix | 
|  | 1135 | // of type MIX_TYPE_RECORDERS | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1136 | if (audio_is_remote_submix_device(newDevice) && policyMix != NULL && | 
|  | 1137 | policyMix->mMixType == MIX_TYPE_RECORDERS) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1138 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, | 
|  | 1139 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1140 | address, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1141 | "remote-submix"); | 
|  | 1142 | } | 
|  | 1143 |  | 
|  | 1144 | if (delayMs != 0) { | 
|  | 1145 | usleep(delayMs * 1000); | 
|  | 1146 | } | 
|  | 1147 |  | 
|  | 1148 | return status; | 
|  | 1149 | } | 
|  | 1150 |  | 
|  | 1151 | status_t AudioPolicyManager::startSource(sp<AudioOutputDescriptor> outputDesc, | 
|  | 1152 | audio_stream_type_t stream, | 
|  | 1153 | audio_devices_t device, | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1154 | const char *address, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1155 | uint32_t *delayMs) | 
|  | 1156 | { | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1157 | // cannot start playback of STREAM_TTS if any other output is being used | 
|  | 1158 | uint32_t beaconMuteLatency = 0; | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1159 |  | 
|  | 1160 | *delayMs = 0; | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1161 | if (stream == AUDIO_STREAM_TTS) { | 
|  | 1162 | ALOGV("\t found BEACON stream"); | 
| Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 1163 | if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) { | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1164 | return INVALID_OPERATION; | 
|  | 1165 | } else { | 
|  | 1166 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); | 
|  | 1167 | } | 
|  | 1168 | } else { | 
|  | 1169 | // some playback other than beacon starts | 
|  | 1170 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); | 
|  | 1171 | } | 
|  | 1172 |  | 
| Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1173 | // force device change if the output is inactive and no audio patch is already present. | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1174 | // check active before incrementing usage count | 
| Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1175 | bool force = !outputDesc->isActive() && | 
|  | 1176 | (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE); | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1177 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1178 | // increment usage count for this stream on the requested output: | 
|  | 1179 | // NOTE that the usage count is the same for duplicated output and hardware output which is | 
|  | 1180 | // necessary for a correct control of hardware output routing by startOutput() and stopOutput() | 
|  | 1181 | outputDesc->changeRefCount(stream, 1); | 
|  | 1182 |  | 
| Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 1183 | if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1184 | // starting an output being rerouted? | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1185 | if (device == AUDIO_DEVICE_NONE) { | 
|  | 1186 | device = getNewOutputDevice(outputDesc, false /*fromCache*/); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1187 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1188 | routing_strategy strategy = getStrategy(stream); | 
|  | 1189 | bool shouldWait = (strategy == STRATEGY_SONIFICATION) || | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1190 | (strategy == STRATEGY_SONIFICATION_RESPECTFUL) || | 
|  | 1191 | (beaconMuteLatency > 0); | 
|  | 1192 | uint32_t waitMs = beaconMuteLatency; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1193 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1194 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1195 | if (desc != outputDesc) { | 
| Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1196 | // force a device change if any other output is: | 
|  | 1197 | // - managed by the same hw module | 
|  | 1198 | // - has a current device selection that differs from selected device. | 
|  | 1199 | // - supports currently selected device | 
|  | 1200 | // - has an active audio patch | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1201 | // In this case, the audio HAL must receive the new device selection so that it can | 
|  | 1202 | // change the device currently selected by the other active output. | 
|  | 1203 | if (outputDesc->sharesHwModuleWith(desc) && | 
| Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1204 | desc->device() != device && | 
|  | 1205 | desc->supportedDevices() & device && | 
|  | 1206 | desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1207 | force = true; | 
|  | 1208 | } | 
|  | 1209 | // 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] | 1210 | // a notification so that audio focus effect can propagate, or that a mute/unmute | 
|  | 1211 | // event occurred for beacon | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1212 | uint32_t latency = desc->latency(); | 
|  | 1213 | if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) { | 
|  | 1214 | waitMs = latency; | 
|  | 1215 | } | 
|  | 1216 | } | 
|  | 1217 | } | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1218 | uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force, 0, NULL, address); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1219 |  | 
|  | 1220 | // handle special case for sonification while in call | 
|  | 1221 | if (isInCall()) { | 
|  | 1222 | handleIncallSonification(stream, true, false); | 
|  | 1223 | } | 
|  | 1224 |  | 
|  | 1225 | // apply volume rules for current stream and device if necessary | 
|  | 1226 | checkAndSetVolume(stream, | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 1227 | mVolumeCurves->getVolumeIndex(stream, device), | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1228 | outputDesc, | 
|  | 1229 | device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1230 |  | 
|  | 1231 | // update the outputs if starting an output with a stream that can affect notification | 
|  | 1232 | // routing | 
|  | 1233 | handleNotificationRoutingForStream(stream); | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1234 |  | 
| Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 1235 | // force reevaluating accessibility routing when ringtone or alarm starts | 
|  | 1236 | if (strategy == STRATEGY_SONIFICATION) { | 
|  | 1237 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); | 
|  | 1238 | } | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1239 |  | 
|  | 1240 | if (waitMs > muteWaitMs) { | 
|  | 1241 | *delayMs = waitMs - muteWaitMs; | 
|  | 1242 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1243 | } | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1244 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1245 | return NO_ERROR; | 
|  | 1246 | } | 
|  | 1247 |  | 
|  | 1248 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1249 | status_t AudioPolicyManager::stopOutput(audio_io_handle_t output, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1250 | audio_stream_type_t stream, | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1251 | audio_session_t session) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1252 | { | 
|  | 1253 | ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session); | 
|  | 1254 | ssize_t index = mOutputs.indexOfKey(output); | 
|  | 1255 | if (index < 0) { | 
|  | 1256 | ALOGW("stopOutput() unknown output %d", output); | 
|  | 1257 | return BAD_VALUE; | 
|  | 1258 | } | 
|  | 1259 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1260 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1261 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1262 | if (outputDesc->mRefCount[stream] == 1) { | 
|  | 1263 | // Automatically disable the remote submix input when output is stopped on a | 
|  | 1264 | // re routing mix of type MIX_TYPE_RECORDERS | 
|  | 1265 | if (audio_is_remote_submix_device(outputDesc->mDevice) && | 
|  | 1266 | outputDesc->mPolicyMix != NULL && | 
|  | 1267 | outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) { | 
|  | 1268 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, | 
|  | 1269 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1270 | outputDesc->mPolicyMix->mDeviceAddress, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1271 | "remote-submix"); | 
|  | 1272 | } | 
|  | 1273 | } | 
|  | 1274 |  | 
|  | 1275 | // Routing? | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1276 | bool forceDeviceUpdate = false; | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1277 | if (outputDesc->mRefCount[stream] > 0) { | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1278 | int activityCount = mOutputRoutes.decRouteActivity(session); | 
|  | 1279 | forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0)); | 
|  | 1280 |  | 
|  | 1281 | if (forceDeviceUpdate) { | 
|  | 1282 | checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE); | 
|  | 1283 | } | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1284 | } | 
|  | 1285 |  | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1286 | return stopSource(outputDesc, stream, forceDeviceUpdate); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1287 | } | 
|  | 1288 |  | 
|  | 1289 | status_t AudioPolicyManager::stopSource(sp<AudioOutputDescriptor> outputDesc, | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1290 | audio_stream_type_t stream, | 
|  | 1291 | bool forceDeviceUpdate) | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1292 | { | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1293 | // always handle stream stop, check which stream type is stopping | 
|  | 1294 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); | 
|  | 1295 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1296 | // handle special case for sonification while in call | 
|  | 1297 | if (isInCall()) { | 
|  | 1298 | handleIncallSonification(stream, false, false); | 
|  | 1299 | } | 
|  | 1300 |  | 
|  | 1301 | if (outputDesc->mRefCount[stream] > 0) { | 
|  | 1302 | // decrement usage count of this stream on the output | 
|  | 1303 | outputDesc->changeRefCount(stream, -1); | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1304 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1305 | // store time at which the stream was stopped - see isStreamActive() | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1306 | if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1307 | outputDesc->mStopTime[stream] = systemTime(); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1308 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1309 | // delay the device switch by twice the latency because stopOutput() is executed when | 
|  | 1310 | // the track stop() command is received and at that time the audio track buffer can | 
|  | 1311 | // still contain data that needs to be drained. The latency only covers the audio HAL | 
|  | 1312 | // and kernel buffers. Also the latency does not always include additional delay in the | 
|  | 1313 | // audio path (audio DSP, CODEC ...) | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1314 | setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1315 |  | 
|  | 1316 | // force restoring the device selection on other active outputs if it differs from the | 
|  | 1317 | // one being selected for this output | 
|  | 1318 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1319 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1320 | if (desc != outputDesc && | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1321 | desc->isActive() && | 
|  | 1322 | outputDesc->sharesHwModuleWith(desc) && | 
|  | 1323 | (newDevice != desc->device())) { | 
| Haynes Mathew George | 11c499a | 2016-08-26 12:08:25 -0700 | [diff] [blame] | 1324 | audio_devices_t newDevice2 = getNewOutputDevice(desc, false /*fromCache*/); | 
|  | 1325 | bool force = desc->device() != newDevice2; | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1326 | setOutputDevice(desc, | 
| Haynes Mathew George | 11c499a | 2016-08-26 12:08:25 -0700 | [diff] [blame] | 1327 | newDevice2, | 
|  | 1328 | force, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1329 | outputDesc->latency()*2); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1330 | } | 
|  | 1331 | } | 
|  | 1332 | // update the outputs if stopping one with a stream that can affect notification routing | 
|  | 1333 | handleNotificationRoutingForStream(stream); | 
|  | 1334 | } | 
|  | 1335 | return NO_ERROR; | 
|  | 1336 | } else { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1337 | ALOGW("stopOutput() refcount is already 0"); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1338 | return INVALID_OPERATION; | 
|  | 1339 | } | 
|  | 1340 | } | 
|  | 1341 |  | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1342 | void AudioPolicyManager::releaseOutput(audio_io_handle_t output, | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1343 | audio_stream_type_t stream __unused, | 
|  | 1344 | audio_session_t session __unused) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1345 | { | 
|  | 1346 | ALOGV("releaseOutput() %d", output); | 
|  | 1347 | ssize_t index = mOutputs.indexOfKey(output); | 
|  | 1348 | if (index < 0) { | 
|  | 1349 | ALOGW("releaseOutput() releasing unknown output %d", output); | 
|  | 1350 | return; | 
|  | 1351 | } | 
|  | 1352 |  | 
|  | 1353 | #ifdef AUDIO_POLICY_TEST | 
|  | 1354 | int testIndex = testOutputIndex(output); | 
|  | 1355 | if (testIndex != 0) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1356 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1357 | if (outputDesc->isActive()) { | 
|  | 1358 | mpClientInterface->closeOutput(output); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1359 | removeOutput(output); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1360 | mTestOutputs[testIndex] = 0; | 
|  | 1361 | } | 
|  | 1362 | return; | 
|  | 1363 | } | 
|  | 1364 | #endif //AUDIO_POLICY_TEST | 
|  | 1365 |  | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1366 | // Routing | 
|  | 1367 | mOutputRoutes.removeRoute(session); | 
|  | 1368 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1369 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1370 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1371 | if (desc->mDirectOpenCount <= 0) { | 
|  | 1372 | ALOGW("releaseOutput() invalid open count %d for output %d", | 
|  | 1373 | desc->mDirectOpenCount, output); | 
|  | 1374 | return; | 
|  | 1375 | } | 
|  | 1376 | if (--desc->mDirectOpenCount == 0) { | 
|  | 1377 | closeOutput(output); | 
|  | 1378 | // If effects where present on the output, audioflinger moved them to the primary | 
|  | 1379 | // output by default: move them back to the appropriate output. | 
|  | 1380 | audio_io_handle_t dstOutput = getOutputForEffect(); | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 1381 | if (hasPrimaryOutput() && dstOutput != mPrimaryOutput->mIoHandle) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1382 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, | 
|  | 1383 | mPrimaryOutput->mIoHandle, dstOutput); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1384 | } | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1385 | mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1386 | } | 
|  | 1387 | } | 
|  | 1388 | } | 
|  | 1389 |  | 
|  | 1390 |  | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1391 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, | 
|  | 1392 | audio_io_handle_t *input, | 
|  | 1393 | audio_session_t session, | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1394 | uid_t uid, | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1395 | uint32_t samplingRate, | 
|  | 1396 | audio_format_t format, | 
|  | 1397 | audio_channel_mask_t channelMask, | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1398 | audio_input_flags_t flags, | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1399 | audio_port_handle_t selectedDeviceId, | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1400 | input_type_t *inputType) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1401 | { | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1402 | ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x," | 
|  | 1403 | "session %d, flags %#x", | 
|  | 1404 | attr->source, samplingRate, format, channelMask, session, flags); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1405 |  | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1406 | *input = AUDIO_IO_HANDLE_NONE; | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1407 | *inputType = API_INPUT_INVALID; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1408 | audio_devices_t device; | 
|  | 1409 | // handle legacy remote submix case where the address was not always specified | 
|  | 1410 | String8 address = String8(""); | 
| Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1411 | audio_source_t inputSource = attr->source; | 
|  | 1412 | audio_source_t halInputSource; | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1413 | AudioMix *policyMix = NULL; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1414 |  | 
| Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1415 | if (inputSource == AUDIO_SOURCE_DEFAULT) { | 
|  | 1416 | inputSource = AUDIO_SOURCE_MIC; | 
|  | 1417 | } | 
|  | 1418 | halInputSource = inputSource; | 
|  | 1419 |  | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1420 | // Explicit routing? | 
|  | 1421 | sp<DeviceDescriptor> deviceDesc; | 
|  | 1422 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { | 
|  | 1423 | if (mAvailableInputDevices[i]->getId() == selectedDeviceId) { | 
|  | 1424 | deviceDesc = mAvailableInputDevices[i]; | 
|  | 1425 | break; | 
|  | 1426 | } | 
|  | 1427 | } | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1428 | mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid); | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1429 |  | 
| Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1430 | if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX && | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1431 | strncmp(attr->tags, "addr=", strlen("addr=")) == 0) { | 
| Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 1432 | status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix); | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 1433 | if (ret != NO_ERROR) { | 
|  | 1434 | return ret; | 
|  | 1435 | } | 
|  | 1436 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1437 | device = AUDIO_DEVICE_IN_REMOTE_SUBMIX; | 
|  | 1438 | address = String8(attr->tags + strlen("addr=")); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1439 | } else { | 
| Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1440 | device = getDeviceAndMixForInputSource(inputSource, &policyMix); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1441 | if (device == AUDIO_DEVICE_NONE) { | 
| Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1442 | ALOGW("getInputForAttr() could not find device for source %d", inputSource); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1443 | return BAD_VALUE; | 
|  | 1444 | } | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1445 | if (policyMix != NULL) { | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1446 | address = policyMix->mDeviceAddress; | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1447 | if (policyMix->mMixType == MIX_TYPE_RECORDERS) { | 
|  | 1448 | // there is an external policy, but this input is attached to a mix of recorders, | 
|  | 1449 | // meaning it receives audio injected into the framework, so the recorder doesn't | 
|  | 1450 | // know about it and is therefore considered "legacy" | 
|  | 1451 | *inputType = API_INPUT_LEGACY; | 
|  | 1452 | } else { | 
|  | 1453 | // recording a mix of players defined by an external policy, we're rerouting for | 
|  | 1454 | // an external policy | 
|  | 1455 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; | 
|  | 1456 | } | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1457 | } else if (audio_is_remote_submix_device(device)) { | 
|  | 1458 | address = String8("0"); | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1459 | *inputType = API_INPUT_MIX_CAPTURE; | 
| Eric Laurent | 82db269 | 2015-08-07 13:59:42 -0700 | [diff] [blame] | 1460 | } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) { | 
|  | 1461 | *inputType = API_INPUT_TELEPHONY_RX; | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1462 | } else { | 
|  | 1463 | *inputType = API_INPUT_LEGACY; | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1464 | } | 
| Ravi Kumar Alamanda | b367f5b | 2015-08-25 08:21:37 -0700 | [diff] [blame] | 1465 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1466 | } | 
|  | 1467 |  | 
|  | 1468 | *input = getInputForDevice(device, address, session, uid, inputSource, | 
|  | 1469 | samplingRate, format, channelMask, flags, | 
|  | 1470 | policyMix); | 
|  | 1471 | if (*input == AUDIO_IO_HANDLE_NONE) { | 
|  | 1472 | mInputRoutes.removeRoute(session); | 
|  | 1473 | return INVALID_OPERATION; | 
|  | 1474 | } | 
|  | 1475 | ALOGV("getInputForAttr() returns input type = %d", *inputType); | 
|  | 1476 | return NO_ERROR; | 
|  | 1477 | } | 
|  | 1478 |  | 
|  | 1479 |  | 
|  | 1480 | audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device, | 
|  | 1481 | String8 address, | 
|  | 1482 | audio_session_t session, | 
|  | 1483 | uid_t uid, | 
|  | 1484 | audio_source_t inputSource, | 
|  | 1485 | uint32_t samplingRate, | 
|  | 1486 | audio_format_t format, | 
|  | 1487 | audio_channel_mask_t channelMask, | 
|  | 1488 | audio_input_flags_t flags, | 
|  | 1489 | AudioMix *policyMix) | 
|  | 1490 | { | 
|  | 1491 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; | 
|  | 1492 | audio_source_t halInputSource = inputSource; | 
|  | 1493 | bool isSoundTrigger = false; | 
|  | 1494 |  | 
|  | 1495 | if (inputSource == AUDIO_SOURCE_HOTWORD) { | 
|  | 1496 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); | 
|  | 1497 | if (index >= 0) { | 
|  | 1498 | input = mSoundTriggerSessions.valueFor(session); | 
|  | 1499 | isSoundTrigger = true; | 
|  | 1500 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); | 
|  | 1501 | ALOGV("SoundTrigger capture on session %d input %d", session, input); | 
|  | 1502 | } else { | 
|  | 1503 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; | 
| Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1504 | } | 
|  | 1505 | } | 
|  | 1506 |  | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1507 | // find a compatible input profile (not necessarily identical in parameters) | 
|  | 1508 | sp<IOProfile> profile; | 
|  | 1509 | // samplingRate and flags may be updated by getInputProfile | 
| Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1510 | uint32_t profileSamplingRate = (samplingRate == 0) ? SAMPLE_RATE_HZ_DEFAULT : samplingRate; | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1511 | audio_format_t profileFormat = format; | 
|  | 1512 | audio_channel_mask_t profileChannelMask = channelMask; | 
|  | 1513 | audio_input_flags_t profileFlags = flags; | 
|  | 1514 | for (;;) { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1515 | profile = getInputProfile(device, address, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1516 | profileSamplingRate, profileFormat, profileChannelMask, | 
|  | 1517 | profileFlags); | 
|  | 1518 | if (profile != 0) { | 
|  | 1519 | break; // success | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1520 | } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) { | 
|  | 1521 | profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1522 | } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) { | 
|  | 1523 | profileFlags = AUDIO_INPUT_FLAG_NONE; // retry | 
|  | 1524 | } else { // fail | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1525 | ALOGW("getInputForDevice() could not find profile for device 0x%X," | 
|  | 1526 | "samplingRate %u, format %#x, channelMask 0x%X, flags %#x", | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1527 | device, samplingRate, format, channelMask, flags); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1528 | return input; | 
| Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1529 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1530 | } | 
| Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1531 | // Pick input sampling rate if not specified by client | 
|  | 1532 | if (samplingRate == 0) { | 
|  | 1533 | samplingRate = profileSamplingRate; | 
|  | 1534 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1535 |  | 
| Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1536 | if (profile->getModuleHandle() == 0) { | 
|  | 1537 | ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName()); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1538 | return input; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1539 | } | 
|  | 1540 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1541 | sp<AudioSession> audioSession = new AudioSession(session, | 
|  | 1542 | inputSource, | 
|  | 1543 | format, | 
|  | 1544 | samplingRate, | 
|  | 1545 | channelMask, | 
|  | 1546 | flags, | 
|  | 1547 | uid, | 
| Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 1548 | isSoundTrigger, | 
|  | 1549 | policyMix, mpClientInterface); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1550 |  | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1551 | // TODO enable input reuse | 
|  | 1552 | #if 0 | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1553 | // reuse an open input if possible | 
|  | 1554 | for (size_t i = 0; i < mInputs.size(); i++) { | 
|  | 1555 | sp<AudioInputDescriptor> desc = mInputs.valueAt(i); | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1556 | // reuse input if it shares the same profile and same sound trigger attribute | 
|  | 1557 | if (profile == desc->mProfile && | 
|  | 1558 | isSoundTrigger == desc->isSoundTrigger()) { | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1559 |  | 
|  | 1560 | sp<AudioSession> as = desc->getAudioSession(session); | 
|  | 1561 | if (as != 0) { | 
|  | 1562 | // do not allow unmatching properties on same session | 
|  | 1563 | if (as->matches(audioSession)) { | 
|  | 1564 | as->changeOpenCount(1); | 
|  | 1565 | } else { | 
|  | 1566 | ALOGW("getInputForDevice() record with different attributes" | 
|  | 1567 | " exists for session %d", session); | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1568 | return input; | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1569 | } | 
|  | 1570 | } else { | 
|  | 1571 | desc->addAudioSession(session, audioSession); | 
|  | 1572 | } | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1573 | ALOGV("getInputForDevice() reusing input %d", mInputs.keyAt(i)); | 
|  | 1574 | return mInputs.keyAt(i); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1575 | } | 
|  | 1576 | } | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1577 | #endif | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1578 |  | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1579 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1580 | config.sample_rate = profileSamplingRate; | 
|  | 1581 | config.channel_mask = profileChannelMask; | 
|  | 1582 | config.format = profileFormat; | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1583 |  | 
| Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1584 | status_t status = mpClientInterface->openInput(profile->getModuleHandle(), | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1585 | &input, | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1586 | &config, | 
|  | 1587 | &device, | 
| Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 1588 | address, | 
| Eric Laurent | 1c9c2cc | 2014-08-28 19:37:25 -0700 | [diff] [blame] | 1589 | halInputSource, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1590 | profileFlags); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1591 |  | 
|  | 1592 | // only accept input with the exact requested set of parameters | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1593 | if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE || | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1594 | (profileSamplingRate != config.sample_rate) || | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1595 | !audio_formats_match(profileFormat, config.format) || | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1596 | (profileChannelMask != config.channel_mask)) { | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1597 | ALOGW("getInputForAttr() failed opening input: samplingRate %d" | 
|  | 1598 | ", format %d, channelMask %x", | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1599 | samplingRate, format, channelMask); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1600 | if (input != AUDIO_IO_HANDLE_NONE) { | 
|  | 1601 | mpClientInterface->closeInput(input); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1602 | } | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1603 | return AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1604 | } | 
|  | 1605 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1606 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile); | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1607 | inputDesc->mSamplingRate = profileSamplingRate; | 
|  | 1608 | inputDesc->mFormat = profileFormat; | 
|  | 1609 | inputDesc->mChannelMask = profileChannelMask; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1610 | inputDesc->mDevice = device; | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1611 | inputDesc->mPolicyMix = policyMix; | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1612 | inputDesc->addAudioSession(session, audioSession); | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1613 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1614 | addInput(input, inputDesc); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1615 | mpClientInterface->onAudioPortListUpdate(); | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1616 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1617 | return input; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1618 | } | 
|  | 1619 |  | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1620 | status_t AudioPolicyManager::startInput(audio_io_handle_t input, | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1621 | audio_session_t session) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1622 | { | 
|  | 1623 | ALOGV("startInput() input %d", input); | 
|  | 1624 | ssize_t index = mInputs.indexOfKey(input); | 
|  | 1625 | if (index < 0) { | 
|  | 1626 | ALOGW("startInput() unknown input %d", input); | 
|  | 1627 | return BAD_VALUE; | 
|  | 1628 | } | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1629 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1630 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1631 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); | 
|  | 1632 | if (audioSession == 0) { | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1633 | ALOGW("startInput() unknown session %d on input %d", session, input); | 
|  | 1634 | return BAD_VALUE; | 
|  | 1635 | } | 
|  | 1636 |  | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1637 | // virtual input devices are compatible with other input devices | 
|  | 1638 | if (!is_virtual_input_device(inputDesc->mDevice)) { | 
| Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1639 |  | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1640 | // for a non-virtual input device, check if there is another (non-virtual) active input | 
|  | 1641 | audio_io_handle_t activeInput = mInputs.getActiveInput(); | 
|  | 1642 | if (activeInput != 0 && activeInput != input) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1643 |  | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1644 | // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed, | 
|  | 1645 | // otherwise the active input continues and the new input cannot be started. | 
|  | 1646 | sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput); | 
|  | 1647 | if ((activeDesc->inputSource() == AUDIO_SOURCE_HOTWORD) && | 
|  | 1648 | !activeDesc->hasPreemptedSession(session)) { | 
|  | 1649 | ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput); | 
|  | 1650 | //FIXME: consider all active sessions | 
|  | 1651 | AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions(); | 
|  | 1652 | audio_session_t activeSession = activeSessions.keyAt(0); | 
|  | 1653 | SortedVector<audio_session_t> sessions = | 
|  | 1654 | activeDesc->getPreemptedSessions(); | 
|  | 1655 | sessions.add(activeSession); | 
|  | 1656 | inputDesc->setPreemptedSessions(sessions); | 
|  | 1657 | stopInput(activeInput, activeSession); | 
|  | 1658 | releaseInput(activeInput, activeSession); | 
|  | 1659 | } else { | 
|  | 1660 | ALOGE("startInput(%d) failed: other input %d already started", input, activeInput); | 
|  | 1661 | return INVALID_OPERATION; | 
|  | 1662 | } | 
|  | 1663 | } | 
|  | 1664 |  | 
|  | 1665 | // Do not allow capture if an active voice call is using a software patch and | 
|  | 1666 | // the call TX source device is on the same HW module. | 
|  | 1667 | // FIXME: would be better to refine to only inputs whose profile connects to the | 
|  | 1668 | // call TX device but this information is not in the audio patch | 
|  | 1669 | if (mCallTxPatch != 0 && | 
|  | 1670 | inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) { | 
|  | 1671 | return INVALID_OPERATION; | 
|  | 1672 | } | 
|  | 1673 | } | 
| Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 1674 |  | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1675 | // Routing? | 
|  | 1676 | mInputRoutes.incRouteActivity(session); | 
|  | 1677 |  | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1678 | if (!inputDesc->isActive() || mInputRoutes.hasRouteChanged(session)) { | 
|  | 1679 | // if input maps to a dynamic policy with an activity listener, notify of state change | 
|  | 1680 | if ((inputDesc->mPolicyMix != NULL) | 
|  | 1681 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1682 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1683 | MIX_STATE_MIXING); | 
|  | 1684 | } | 
| Jean-Michel Trivi | 2b0c1fc | 2015-04-15 17:29:28 -0700 | [diff] [blame] | 1685 |  | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1686 | if (mInputs.activeInputsCount() == 0) { | 
|  | 1687 | SoundTrigger::setCaptureState(true); | 
|  | 1688 | } | 
|  | 1689 | setInputDevice(input, getNewInputDevice(input), true /* force */); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1690 |  | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1691 | // automatically enable the remote submix output when input is started if not | 
|  | 1692 | // used by a policy mix of type MIX_TYPE_RECORDERS | 
|  | 1693 | // For remote submix (a virtual device), we open only one input per capture request. | 
|  | 1694 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { | 
|  | 1695 | String8 address = String8(""); | 
|  | 1696 | if (inputDesc->mPolicyMix == NULL) { | 
|  | 1697 | address = String8("0"); | 
|  | 1698 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1699 | address = inputDesc->mPolicyMix->mDeviceAddress; | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1700 | } | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1701 | if (address != "") { | 
|  | 1702 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, | 
|  | 1703 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, | 
|  | 1704 | address, "remote-submix"); | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1705 | } | 
| Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1706 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1707 | } | 
|  | 1708 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1709 | ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1710 |  | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1711 | audioSession->changeActiveCount(1); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1712 | return NO_ERROR; | 
|  | 1713 | } | 
|  | 1714 |  | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1715 | status_t AudioPolicyManager::stopInput(audio_io_handle_t input, | 
|  | 1716 | audio_session_t session) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1717 | { | 
|  | 1718 | ALOGV("stopInput() input %d", input); | 
|  | 1719 | ssize_t index = mInputs.indexOfKey(input); | 
|  | 1720 | if (index < 0) { | 
|  | 1721 | ALOGW("stopInput() unknown input %d", input); | 
|  | 1722 | return BAD_VALUE; | 
|  | 1723 | } | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1724 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1725 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1726 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1727 | if (index < 0) { | 
|  | 1728 | ALOGW("stopInput() unknown session %d on input %d", session, input); | 
|  | 1729 | return BAD_VALUE; | 
|  | 1730 | } | 
|  | 1731 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1732 | if (audioSession->activeCount() == 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1733 | ALOGW("stopInput() input %d already stopped", input); | 
|  | 1734 | return INVALID_OPERATION; | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1735 | } | 
|  | 1736 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1737 | audioSession->changeActiveCount(-1); | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1738 |  | 
|  | 1739 | // Routing? | 
|  | 1740 | mInputRoutes.decRouteActivity(session); | 
|  | 1741 |  | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1742 | if (!inputDesc->isActive()) { | 
|  | 1743 | // if input maps to a dynamic policy with an activity listener, notify of state change | 
|  | 1744 | if ((inputDesc->mPolicyMix != NULL) | 
|  | 1745 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1746 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1747 | MIX_STATE_IDLE); | 
| Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 1748 | } | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1749 |  | 
|  | 1750 | // automatically disable the remote submix output when input is stopped if not | 
|  | 1751 | // used by a policy mix of type MIX_TYPE_RECORDERS | 
|  | 1752 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { | 
|  | 1753 | String8 address = String8(""); | 
|  | 1754 | if (inputDesc->mPolicyMix == NULL) { | 
|  | 1755 | address = String8("0"); | 
|  | 1756 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1757 | address = inputDesc->mPolicyMix->mDeviceAddress; | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 1758 | } | 
|  | 1759 | if (address != "") { | 
|  | 1760 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, | 
|  | 1761 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, | 
|  | 1762 | address, "remote-submix"); | 
|  | 1763 | } | 
|  | 1764 | } | 
|  | 1765 |  | 
|  | 1766 | resetInputDevice(input); | 
|  | 1767 |  | 
|  | 1768 | if (mInputs.activeInputsCount() == 0) { | 
|  | 1769 | SoundTrigger::setCaptureState(false); | 
|  | 1770 | } | 
|  | 1771 | inputDesc->clearPreemptedSessions(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1772 | } | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1773 | return NO_ERROR; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1774 | } | 
|  | 1775 |  | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1776 | void AudioPolicyManager::releaseInput(audio_io_handle_t input, | 
|  | 1777 | audio_session_t session) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1778 | { | 
| Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 1779 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1780 | ALOGV("releaseInput() %d", input); | 
|  | 1781 | ssize_t index = mInputs.indexOfKey(input); | 
|  | 1782 | if (index < 0) { | 
|  | 1783 | ALOGW("releaseInput() releasing unknown input %d", input); | 
|  | 1784 | return; | 
|  | 1785 | } | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1786 |  | 
|  | 1787 | // Routing | 
|  | 1788 | mInputRoutes.removeRoute(session); | 
|  | 1789 |  | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1790 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); | 
|  | 1791 | ALOG_ASSERT(inputDesc != 0); | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1792 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1793 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1794 | if (index < 0) { | 
|  | 1795 | ALOGW("releaseInput() unknown session %d on input %d", session, input); | 
|  | 1796 | return; | 
|  | 1797 | } | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1798 |  | 
|  | 1799 | if (audioSession->openCount() == 0) { | 
|  | 1800 | ALOGW("releaseInput() invalid open count %d on session %d", | 
|  | 1801 | audioSession->openCount(), session); | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1802 | return; | 
|  | 1803 | } | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1804 |  | 
|  | 1805 | if (audioSession->changeOpenCount(-1) == 0) { | 
|  | 1806 | inputDesc->removeAudioSession(session); | 
|  | 1807 | } | 
|  | 1808 |  | 
| Jean-Michel Trivi | 65bfe91 | 2015-12-04 15:56:47 -0800 | [diff] [blame] | 1809 | if (inputDesc->getOpenRefCount() > 0) { | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1810 | ALOGV("releaseInput() exit > 0"); | 
|  | 1811 | return; | 
|  | 1812 | } | 
|  | 1813 |  | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1814 | closeInput(input); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1815 | mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1816 | ALOGV("releaseInput() exit"); | 
|  | 1817 | } | 
|  | 1818 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1819 | void AudioPolicyManager::closeAllInputs() { | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1820 | bool patchRemoved = false; | 
|  | 1821 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1822 | for(size_t input_index = 0; input_index < mInputs.size(); input_index++) { | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1823 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index); | 
| Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 1824 | ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1825 | if (patch_index >= 0) { | 
|  | 1826 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index); | 
| Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 1827 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1828 | mAudioPatches.removeItemsAt(patch_index); | 
|  | 1829 | patchRemoved = true; | 
|  | 1830 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1831 | mpClientInterface->closeInput(mInputs.keyAt(input_index)); | 
|  | 1832 | } | 
|  | 1833 | mInputs.clear(); | 
| Chris Thornton | 52785f3 | 2016-02-09 17:28:49 -0800 | [diff] [blame] | 1834 | SoundTrigger::setCaptureState(false); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1835 | nextAudioPortGeneration(); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1836 |  | 
|  | 1837 | if (patchRemoved) { | 
|  | 1838 | mpClientInterface->onAudioPatchListUpdate(); | 
|  | 1839 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1840 | } | 
|  | 1841 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1842 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1843 | int indexMin, | 
|  | 1844 | int indexMax) | 
|  | 1845 | { | 
|  | 1846 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 1847 | mVolumeCurves->initStreamVolume(stream, indexMin, indexMax); | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1848 |  | 
|  | 1849 | // initialize other private stream volumes which follow this one | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 1850 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { | 
|  | 1851 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1852 | continue; | 
|  | 1853 | } | 
|  | 1854 | mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax); | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1855 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1856 | } | 
|  | 1857 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1858 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1859 | int index, | 
|  | 1860 | audio_devices_t device) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1861 | { | 
|  | 1862 |  | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 1863 | if ((index < mVolumeCurves->getVolumeIndexMin(stream)) || | 
|  | 1864 | (index > mVolumeCurves->getVolumeIndexMax(stream))) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1865 | return BAD_VALUE; | 
|  | 1866 | } | 
|  | 1867 | if (!audio_is_output_device(device)) { | 
|  | 1868 | return BAD_VALUE; | 
|  | 1869 | } | 
|  | 1870 |  | 
|  | 1871 | // Force max volume if stream cannot be muted | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 1872 | if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1873 |  | 
| Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 1874 | ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d", | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1875 | stream, device, index); | 
|  | 1876 |  | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1877 | // update other private stream volumes which follow this one | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 1878 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { | 
|  | 1879 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1880 | continue; | 
|  | 1881 | } | 
|  | 1882 | mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index); | 
|  | 1883 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1884 |  | 
| Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 1885 | // update volume on all outputs and streams matching the following: | 
|  | 1886 | // - The requested stream (or a stream matching for volume control) is active on the output | 
|  | 1887 | // - The device (or devices) selected by the strategy corresponding to this stream includes | 
|  | 1888 | // the requested device | 
|  | 1889 | // - For non default requested device, currently selected device on the output is either the | 
|  | 1890 | // requested device or one of the devices selected by the strategy | 
| Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 1891 | // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if | 
|  | 1892 | // no specific device volume value exists for currently selected device. | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1893 | status_t status = NO_ERROR; | 
|  | 1894 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1895 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
|  | 1896 | audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device()); | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 1897 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { | 
|  | 1898 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1899 | continue; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1900 | } | 
| Eric Laurent | d3926fe | 2016-04-15 18:10:07 -0700 | [diff] [blame] | 1901 | if (!(desc->isStreamActive((audio_stream_type_t)curStream) || | 
|  | 1902 | (isInCall() && (curStream == AUDIO_STREAM_VOICE_CALL)))) { | 
| Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 1903 | continue; | 
|  | 1904 | } | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 1905 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1906 | audio_devices_t curStreamDevice = getDeviceForStrategy(curStrategy, true /*fromCache*/); | 
| Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 1907 | if ((curStreamDevice & device) == 0) { | 
|  | 1908 | continue; | 
|  | 1909 | } | 
|  | 1910 | bool applyDefault = false; | 
| Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 1911 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { | 
| Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 1912 | curStreamDevice |= device; | 
|  | 1913 | } else if (!mVolumeCurves->hasVolumeIndexForDevice( | 
|  | 1914 | stream, Volume::getDeviceForVolume(curStreamDevice))) { | 
|  | 1915 | applyDefault = true; | 
|  | 1916 | } | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1917 |  | 
| Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 1918 | if (applyDefault || ((curDevice & curStreamDevice) != 0)) { | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1919 | //FIXME: workaround for truncated touch sounds | 
|  | 1920 | // delayed volume change for system stream to be removed when the problem is | 
|  | 1921 | // handled by system UI | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1922 | status_t volStatus = | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1923 | checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice, | 
|  | 1924 | (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0); | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1925 | if (volStatus != NO_ERROR) { | 
|  | 1926 | status = volStatus; | 
|  | 1927 | } | 
|  | 1928 | } | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1929 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1930 | } | 
|  | 1931 | return status; | 
|  | 1932 | } | 
|  | 1933 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1934 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1935 | int *index, | 
|  | 1936 | audio_devices_t device) | 
|  | 1937 | { | 
|  | 1938 | if (index == NULL) { | 
|  | 1939 | return BAD_VALUE; | 
|  | 1940 | } | 
|  | 1941 | if (!audio_is_output_device(device)) { | 
|  | 1942 | return BAD_VALUE; | 
|  | 1943 | } | 
| Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 1944 | // 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] | 1945 | // the strategy the stream belongs to. | 
| Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 1946 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1947 | device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); | 
|  | 1948 | } | 
| François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 1949 | device = Volume::getDeviceForVolume(device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1950 |  | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 1951 | *index =  mVolumeCurves->getVolumeIndex(stream, device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1952 | ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index); | 
|  | 1953 | return NO_ERROR; | 
|  | 1954 | } | 
|  | 1955 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1956 | audio_io_handle_t AudioPolicyManager::selectOutputForEffects( | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1957 | const SortedVector<audio_io_handle_t>& outputs) | 
|  | 1958 | { | 
|  | 1959 | // select one output among several suitable for global effects. | 
|  | 1960 | // The priority is as follows: | 
|  | 1961 | // 1: An offloaded output. If the effect ends up not being offloadable, | 
|  | 1962 | //    AudioFlinger will invalidate the track and the offloaded output | 
|  | 1963 | //    will be closed causing the effect to be moved to a PCM output. | 
|  | 1964 | // 2: A deep buffer output | 
|  | 1965 | // 3: the first output in the list | 
|  | 1966 |  | 
|  | 1967 | if (outputs.size() == 0) { | 
|  | 1968 | return 0; | 
|  | 1969 | } | 
|  | 1970 |  | 
|  | 1971 | audio_io_handle_t outputOffloaded = 0; | 
|  | 1972 | audio_io_handle_t outputDeepBuffer = 0; | 
|  | 1973 |  | 
|  | 1974 | for (size_t i = 0; i < outputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1975 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1976 | ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1977 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { | 
|  | 1978 | outputOffloaded = outputs[i]; | 
|  | 1979 | } | 
|  | 1980 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { | 
|  | 1981 | outputDeepBuffer = outputs[i]; | 
|  | 1982 | } | 
|  | 1983 | } | 
|  | 1984 |  | 
|  | 1985 | ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d", | 
|  | 1986 | outputOffloaded, outputDeepBuffer); | 
|  | 1987 | if (outputOffloaded != 0) { | 
|  | 1988 | return outputOffloaded; | 
|  | 1989 | } | 
|  | 1990 | if (outputDeepBuffer != 0) { | 
|  | 1991 | return outputDeepBuffer; | 
|  | 1992 | } | 
|  | 1993 |  | 
|  | 1994 | return outputs[0]; | 
|  | 1995 | } | 
|  | 1996 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1997 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1998 | { | 
|  | 1999 | // apply simple rule where global effects are attached to the same output as MUSIC streams | 
|  | 2000 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2001 | routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2002 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); | 
|  | 2003 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs); | 
|  | 2004 |  | 
|  | 2005 | audio_io_handle_t output = selectOutputForEffects(dstOutputs); | 
|  | 2006 | ALOGV("getOutputForEffect() got output %d for fx %s flags %x", | 
|  | 2007 | output, (desc == NULL) ? "unspecified" : desc->name,  (desc == NULL) ? 0 : desc->flags); | 
|  | 2008 |  | 
|  | 2009 | return output; | 
|  | 2010 | } | 
|  | 2011 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2012 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2013 | audio_io_handle_t io, | 
|  | 2014 | uint32_t strategy, | 
|  | 2015 | int session, | 
|  | 2016 | int id) | 
|  | 2017 | { | 
|  | 2018 | ssize_t index = mOutputs.indexOfKey(io); | 
|  | 2019 | if (index < 0) { | 
|  | 2020 | index = mInputs.indexOfKey(io); | 
|  | 2021 | if (index < 0) { | 
|  | 2022 | ALOGW("registerEffect() unknown io %d", io); | 
|  | 2023 | return INVALID_OPERATION; | 
|  | 2024 | } | 
|  | 2025 | } | 
| François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2026 | return mEffects.registerEffect(desc, io, strategy, session, id); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2027 | } | 
|  | 2028 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2029 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const | 
|  | 2030 | { | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2031 | bool active = false; | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2032 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) { | 
|  | 2033 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2034 | continue; | 
|  | 2035 | } | 
|  | 2036 | active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs); | 
|  | 2037 | } | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2038 | return active; | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2039 | } | 
|  | 2040 |  | 
|  | 2041 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const | 
|  | 2042 | { | 
|  | 2043 | return mOutputs.isStreamActiveRemotely(stream, inPastMs); | 
|  | 2044 | } | 
|  | 2045 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2046 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2047 | { | 
|  | 2048 | for (size_t i = 0; i < mInputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2049 | const sp<AudioInputDescriptor>  inputDescriptor = mInputs.valueAt(i); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2050 | if (inputDescriptor->isSourceActive(source)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2051 | return true; | 
|  | 2052 | } | 
|  | 2053 | } | 
|  | 2054 | return false; | 
|  | 2055 | } | 
|  | 2056 |  | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2057 | // Register a list of custom mixes with their attributes and format. | 
|  | 2058 | // When a mix is registered, corresponding input and output profiles are | 
|  | 2059 | // added to the remote submix hw module. The profile contains only the | 
|  | 2060 | // parameters (sampling rate, format...) specified by the mix. | 
|  | 2061 | // The corresponding input remote submix device is also connected. | 
|  | 2062 | // | 
|  | 2063 | // When a remote submix device is connected, the address is checked to select the | 
|  | 2064 | // appropriate profile and the corresponding input or output stream is opened. | 
|  | 2065 | // | 
|  | 2066 | // When capture starts, getInputForAttr() will: | 
|  | 2067 | //  - 1 look for a mix matching the address passed in attribtutes tags if any | 
|  | 2068 | //  - 2 if none found, getDeviceForInputSource() will: | 
|  | 2069 | //     - 2.1 look for a mix matching the attributes source | 
|  | 2070 | //     - 2.2 if none found, default to device selection by policy rules | 
|  | 2071 | // At this time, the corresponding output remote submix device is also connected | 
|  | 2072 | // and active playback use cases can be transferred to this mix if needed when reconnecting | 
|  | 2073 | // after AudioTracks are invalidated | 
|  | 2074 | // | 
|  | 2075 | // When playback starts, getOutputForAttr() will: | 
|  | 2076 | //  - 1 look for a mix matching the address passed in attribtutes tags if any | 
|  | 2077 | //  - 2 if none found, look for a mix matching the attributes usage | 
|  | 2078 | //  - 3 if none found, default to device and output selection by policy rules. | 
|  | 2079 |  | 
|  | 2080 | status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes) | 
|  | 2081 | { | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2082 | ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size()); | 
|  | 2083 | status_t res = NO_ERROR; | 
|  | 2084 |  | 
|  | 2085 | sp<HwModule> rSubmixModule; | 
|  | 2086 | // examine each mix's route type | 
|  | 2087 | for (size_t i = 0; i < mixes.size(); i++) { | 
|  | 2088 | // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination | 
|  | 2089 | if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) { | 
|  | 2090 | res = INVALID_OPERATION; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2091 | break; | 
|  | 2092 | } | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2093 | if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) { | 
|  | 2094 | // Loop back through "remote submix" | 
|  | 2095 | if (rSubmixModule == 0) { | 
|  | 2096 | for (size_t j = 0; i < mHwModules.size(); j++) { | 
|  | 2097 | if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0 | 
|  | 2098 | && mHwModules[j]->mHandle != 0) { | 
|  | 2099 | rSubmixModule = mHwModules[j]; | 
|  | 2100 | break; | 
|  | 2101 | } | 
|  | 2102 | } | 
|  | 2103 | } | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2104 |  | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2105 | ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK", i, mixes.size()); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2106 |  | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2107 | if (rSubmixModule == 0) { | 
|  | 2108 | ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", i); | 
|  | 2109 | res = INVALID_OPERATION; | 
|  | 2110 | break; | 
|  | 2111 | } | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2112 |  | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2113 | String8 address = mixes[i].mDeviceAddress; | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2114 |  | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2115 | if (mPolicyMixes.registerMix(address, mixes[i], 0 /*output desc*/) != NO_ERROR) { | 
| Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2116 | ALOGE(" Error registering mix %zu for address %s", i, address.string()); | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2117 | res = INVALID_OPERATION; | 
|  | 2118 | break; | 
|  | 2119 | } | 
|  | 2120 | audio_config_t outputConfig = mixes[i].mFormat; | 
|  | 2121 | audio_config_t inputConfig = mixes[i].mFormat; | 
|  | 2122 | // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in | 
|  | 2123 | // stereo and let audio flinger do the channel conversion if needed. | 
|  | 2124 | outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; | 
|  | 2125 | inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; | 
|  | 2126 | rSubmixModule->addOutputProfile(address, &outputConfig, | 
|  | 2127 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address); | 
|  | 2128 | rSubmixModule->addInputProfile(address, &inputConfig, | 
|  | 2129 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, address); | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2130 |  | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2131 | if (mixes[i].mMixType == MIX_TYPE_PLAYERS) { | 
|  | 2132 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, | 
|  | 2133 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, | 
|  | 2134 | address.string(), "remote-submix"); | 
|  | 2135 | } else { | 
|  | 2136 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, | 
|  | 2137 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, | 
|  | 2138 | address.string(), "remote-submix"); | 
|  | 2139 | } | 
|  | 2140 | } 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] | 2141 | String8 address = mixes[i].mDeviceAddress; | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2142 | audio_devices_t device = mixes[i].mDeviceType; | 
| Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2143 | ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s", | 
|  | 2144 | i, mixes.size(), device, address.string()); | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2145 |  | 
| Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2146 | bool foundOutput = false; | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2147 | for (size_t j = 0 ; j < mOutputs.size() ; j++) { | 
|  | 2148 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j); | 
|  | 2149 | sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle()); | 
|  | 2150 | if ((patch != 0) && (patch->mPatch.num_sinks != 0) | 
|  | 2151 | && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE) | 
|  | 2152 | && (patch->mPatch.sinks[0].ext.device.type == device) | 
| Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2153 | && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(), | 
|  | 2154 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2155 | if (mPolicyMixes.registerMix(address, mixes[i], desc) != NO_ERROR) { | 
|  | 2156 | res = INVALID_OPERATION; | 
| Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2157 | } else { | 
|  | 2158 | foundOutput = true; | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2159 | } | 
|  | 2160 | break; | 
|  | 2161 | } | 
|  | 2162 | } | 
|  | 2163 |  | 
|  | 2164 | if (res != NO_ERROR) { | 
|  | 2165 | ALOGE(" Error registering mix %zu for device 0x%X addr %s", | 
| Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2166 | i, device, address.string()); | 
|  | 2167 | res = INVALID_OPERATION; | 
|  | 2168 | break; | 
|  | 2169 | } else if (!foundOutput) { | 
|  | 2170 | ALOGE(" Output not found for mix %zu for device 0x%X addr %s", | 
|  | 2171 | i, device, address.string()); | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2172 | res = INVALID_OPERATION; | 
|  | 2173 | break; | 
|  | 2174 | } | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2175 | } | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2176 | } | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2177 | if (res != NO_ERROR) { | 
|  | 2178 | unregisterPolicyMixes(mixes); | 
|  | 2179 | } | 
|  | 2180 | return res; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2181 | } | 
|  | 2182 |  | 
|  | 2183 | status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes) | 
|  | 2184 | { | 
| Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2185 | ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size()); | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2186 | status_t res = NO_ERROR; | 
|  | 2187 | sp<HwModule> rSubmixModule; | 
|  | 2188 | // examine each mix's route type | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2189 | for (size_t i = 0; i < mixes.size(); i++) { | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2190 | 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] | 2191 |  | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2192 | if (rSubmixModule == 0) { | 
|  | 2193 | for (size_t j = 0; i < mHwModules.size(); j++) { | 
|  | 2194 | if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0 | 
|  | 2195 | && mHwModules[j]->mHandle != 0) { | 
|  | 2196 | rSubmixModule = mHwModules[j]; | 
|  | 2197 | break; | 
|  | 2198 | } | 
|  | 2199 | } | 
|  | 2200 | } | 
|  | 2201 | if (rSubmixModule == 0) { | 
|  | 2202 | res = INVALID_OPERATION; | 
|  | 2203 | continue; | 
|  | 2204 | } | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2205 |  | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2206 | String8 address = mixes[i].mDeviceAddress; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2207 |  | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2208 | if (mPolicyMixes.unregisterMix(address) != NO_ERROR) { | 
|  | 2209 | res = INVALID_OPERATION; | 
|  | 2210 | continue; | 
|  | 2211 | } | 
|  | 2212 |  | 
|  | 2213 | if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) == | 
|  | 2214 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE)  { | 
|  | 2215 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, | 
|  | 2216 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, | 
|  | 2217 | address.string(), "remote-submix"); | 
|  | 2218 | } | 
|  | 2219 | if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) == | 
|  | 2220 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE)  { | 
|  | 2221 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, | 
|  | 2222 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, | 
|  | 2223 | address.string(), "remote-submix"); | 
|  | 2224 | } | 
|  | 2225 | rSubmixModule->removeOutputProfile(address); | 
|  | 2226 | rSubmixModule->removeInputProfile(address); | 
|  | 2227 |  | 
|  | 2228 | } if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { | 
|  | 2229 | if (mPolicyMixes.unregisterMix(mixes[i].mDeviceAddress) != NO_ERROR) { | 
|  | 2230 | res = INVALID_OPERATION; | 
|  | 2231 | continue; | 
|  | 2232 | } | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2233 | } | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2234 | } | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2235 | return res; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2236 | } | 
|  | 2237 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2238 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2239 | status_t AudioPolicyManager::dump(int fd) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2240 | { | 
|  | 2241 | const size_t SIZE = 256; | 
|  | 2242 | char buffer[SIZE]; | 
|  | 2243 | String8 result; | 
|  | 2244 |  | 
|  | 2245 | snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this); | 
|  | 2246 | result.append(buffer); | 
|  | 2247 |  | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 2248 | snprintf(buffer, SIZE, " Primary Output: %d\n", | 
|  | 2249 | hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2250 | result.append(buffer); | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2251 | snprintf(buffer, SIZE, " Phone state: %d\n", mEngine->getPhoneState()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2252 | result.append(buffer); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2253 | snprintf(buffer, SIZE, " Force use for communications %d\n", | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2254 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2255 | result.append(buffer); | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2256 | 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] | 2257 | result.append(buffer); | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2258 | 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] | 2259 | result.append(buffer); | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2260 | 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] | 2261 | result.append(buffer); | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2262 | 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] | 2263 | result.append(buffer); | 
| Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 2264 | snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n", | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2265 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO)); | 
| Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 2266 | result.append(buffer); | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 2267 | snprintf(buffer, SIZE, " Force use for encoded surround output %d\n", | 
|  | 2268 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND)); | 
|  | 2269 | result.append(buffer); | 
| Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 2270 | snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available"); | 
|  | 2271 | result.append(buffer); | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 2272 | snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off"); | 
|  | 2273 | result.append(buffer); | 
| Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 2274 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2275 | write(fd, result.string(), result.size()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2276 |  | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 2277 | mAvailableOutputDevices.dump(fd, String8("Available output")); | 
|  | 2278 | mAvailableInputDevices.dump(fd, String8("Available input")); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2279 | mHwModules.dump(fd); | 
|  | 2280 | mOutputs.dump(fd); | 
|  | 2281 | mInputs.dump(fd); | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2282 | mVolumeCurves->dump(fd); | 
| François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2283 | mEffects.dump(fd); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2284 | mAudioPatches.dump(fd); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2285 |  | 
|  | 2286 | return NO_ERROR; | 
|  | 2287 | } | 
|  | 2288 |  | 
|  | 2289 | // This function checks for the parameters which can be offloaded. | 
|  | 2290 | // This can be enhanced depending on the capability of the DSP and policy | 
|  | 2291 | // of the system. | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2292 | bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2293 | { | 
|  | 2294 | ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2295 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2296 | offloadInfo.sample_rate, offloadInfo.channel_mask, | 
|  | 2297 | offloadInfo.format, | 
|  | 2298 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, | 
|  | 2299 | offloadInfo.has_video); | 
|  | 2300 |  | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 2301 | if (mMasterMono) { | 
|  | 2302 | return false; // no offloading if mono is set. | 
|  | 2303 | } | 
|  | 2304 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2305 | // Check if offload has been disabled | 
|  | 2306 | char propValue[PROPERTY_VALUE_MAX]; | 
|  | 2307 | if (property_get("audio.offload.disable", propValue, "0")) { | 
|  | 2308 | if (atoi(propValue) != 0) { | 
|  | 2309 | ALOGV("offload disabled by audio.offload.disable=%s", propValue ); | 
|  | 2310 | return false; | 
|  | 2311 | } | 
|  | 2312 | } | 
|  | 2313 |  | 
|  | 2314 | // Check if stream type is music, then only allow offload as of now. | 
|  | 2315 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) | 
|  | 2316 | { | 
|  | 2317 | ALOGV("isOffloadSupported: stream_type != MUSIC, returning false"); | 
|  | 2318 | return false; | 
|  | 2319 | } | 
|  | 2320 |  | 
|  | 2321 | //TODO: enable audio offloading with video when ready | 
| Andy Hung | 08945c4 | 2015-05-31 21:36:46 -0700 | [diff] [blame] | 2322 | const bool allowOffloadWithVideo = | 
|  | 2323 | property_get_bool("audio.offload.video", false /* default_value */); | 
|  | 2324 | if (offloadInfo.has_video && !allowOffloadWithVideo) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2325 | ALOGV("isOffloadSupported: has_video == true, returning false"); | 
|  | 2326 | return false; | 
|  | 2327 | } | 
|  | 2328 |  | 
|  | 2329 | //If duration is less than minimum value defined in property, return false | 
|  | 2330 | if (property_get("audio.offload.min.duration.secs", propValue, NULL)) { | 
|  | 2331 | if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) { | 
|  | 2332 | ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue); | 
|  | 2333 | return false; | 
|  | 2334 | } | 
|  | 2335 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { | 
|  | 2336 | ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS); | 
|  | 2337 | return false; | 
|  | 2338 | } | 
|  | 2339 |  | 
|  | 2340 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from | 
|  | 2341 | // creating an offloaded track and tearing it down immediately after start when audioflinger | 
|  | 2342 | // detects there is an active non offloadable effect. | 
|  | 2343 | // FIXME: We should check the audio session here but we do not have it in this context. | 
|  | 2344 | // This may prevent offloading in rare situations where effects are left active by apps | 
|  | 2345 | // in the background. | 
| François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2346 | if (mEffects.isNonOffloadableEffectEnabled()) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2347 | return false; | 
|  | 2348 | } | 
|  | 2349 |  | 
|  | 2350 | // See if there is a profile to support this. | 
|  | 2351 | // AUDIO_DEVICE_NONE | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2352 | sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2353 | offloadInfo.sample_rate, | 
|  | 2354 | offloadInfo.format, | 
|  | 2355 | offloadInfo.channel_mask, | 
|  | 2356 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2357 | ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT "); | 
|  | 2358 | return (profile != 0); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2359 | } | 
|  | 2360 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2361 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, | 
|  | 2362 | audio_port_type_t type, | 
|  | 2363 | unsigned int *num_ports, | 
|  | 2364 | struct audio_port *ports, | 
|  | 2365 | unsigned int *generation) | 
|  | 2366 | { | 
|  | 2367 | if (num_ports == NULL || (*num_ports != 0 && ports == NULL) || | 
|  | 2368 | generation == NULL) { | 
|  | 2369 | return BAD_VALUE; | 
|  | 2370 | } | 
|  | 2371 | ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports); | 
|  | 2372 | if (ports == NULL) { | 
|  | 2373 | *num_ports = 0; | 
|  | 2374 | } | 
|  | 2375 |  | 
|  | 2376 | size_t portsWritten = 0; | 
|  | 2377 | size_t portsMax = *num_ports; | 
|  | 2378 | *num_ports = 0; | 
|  | 2379 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { | 
| Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2380 | // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB | 
|  | 2381 | // as they are used by stub HALs by convention | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2382 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { | 
| Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2383 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { | 
|  | 2384 | if (mAvailableOutputDevices[i]->type() == AUDIO_DEVICE_OUT_STUB) { | 
|  | 2385 | continue; | 
|  | 2386 | } | 
|  | 2387 | if (portsWritten < portsMax) { | 
|  | 2388 | mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]); | 
|  | 2389 | } | 
|  | 2390 | (*num_ports)++; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2391 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2392 | } | 
|  | 2393 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { | 
| Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2394 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { | 
|  | 2395 | if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_STUB) { | 
|  | 2396 | continue; | 
|  | 2397 | } | 
|  | 2398 | if (portsWritten < portsMax) { | 
|  | 2399 | mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]); | 
|  | 2400 | } | 
|  | 2401 | (*num_ports)++; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2402 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2403 | } | 
|  | 2404 | } | 
|  | 2405 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { | 
|  | 2406 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { | 
|  | 2407 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { | 
|  | 2408 | mInputs[i]->toAudioPort(&ports[portsWritten++]); | 
|  | 2409 | } | 
|  | 2410 | *num_ports += mInputs.size(); | 
|  | 2411 | } | 
|  | 2412 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2413 | size_t numOutputs = 0; | 
|  | 2414 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 2415 | if (!mOutputs[i]->isDuplicated()) { | 
|  | 2416 | numOutputs++; | 
|  | 2417 | if (portsWritten < portsMax) { | 
|  | 2418 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); | 
|  | 2419 | } | 
|  | 2420 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2421 | } | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2422 | *num_ports += numOutputs; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2423 | } | 
|  | 2424 | } | 
|  | 2425 | *generation = curAudioPortGeneration(); | 
| Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2426 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2427 | return NO_ERROR; | 
|  | 2428 | } | 
|  | 2429 |  | 
|  | 2430 | status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused) | 
|  | 2431 | { | 
|  | 2432 | return NO_ERROR; | 
|  | 2433 | } | 
|  | 2434 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2435 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, | 
|  | 2436 | audio_patch_handle_t *handle, | 
|  | 2437 | uid_t uid) | 
|  | 2438 | { | 
|  | 2439 | ALOGV("createAudioPatch()"); | 
|  | 2440 |  | 
|  | 2441 | if (handle == NULL || patch == NULL) { | 
|  | 2442 | return BAD_VALUE; | 
|  | 2443 | } | 
|  | 2444 | ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks); | 
|  | 2445 |  | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2446 | if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX || | 
|  | 2447 | patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) { | 
|  | 2448 | return BAD_VALUE; | 
|  | 2449 | } | 
|  | 2450 | // only one source per audio patch supported for now | 
|  | 2451 | if (patch->num_sources > 1) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2452 | return INVALID_OPERATION; | 
|  | 2453 | } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2454 |  | 
|  | 2455 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2456 | return INVALID_OPERATION; | 
|  | 2457 | } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2458 | for (size_t i = 0; i < patch->num_sinks; i++) { | 
|  | 2459 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { | 
|  | 2460 | return INVALID_OPERATION; | 
|  | 2461 | } | 
|  | 2462 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2463 |  | 
|  | 2464 | sp<AudioPatch> patchDesc; | 
|  | 2465 | ssize_t index = mAudioPatches.indexOfKey(*handle); | 
|  | 2466 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2467 | ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id, | 
|  | 2468 | patch->sources[0].role, | 
|  | 2469 | patch->sources[0].type); | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2470 | #if LOG_NDEBUG == 0 | 
|  | 2471 | for (size_t i = 0; i < patch->num_sinks; i++) { | 
| Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2472 | 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] | 2473 | patch->sinks[i].role, | 
|  | 2474 | patch->sinks[i].type); | 
|  | 2475 | } | 
|  | 2476 | #endif | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2477 |  | 
|  | 2478 | if (index >= 0) { | 
|  | 2479 | patchDesc = mAudioPatches.valueAt(index); | 
|  | 2480 | ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", | 
|  | 2481 | mUidCached, patchDesc->mUid, uid); | 
|  | 2482 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { | 
|  | 2483 | return INVALID_OPERATION; | 
|  | 2484 | } | 
|  | 2485 | } else { | 
| Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 2486 | *handle = AUDIO_PATCH_HANDLE_NONE; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2487 | } | 
|  | 2488 |  | 
|  | 2489 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2490 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2491 | if (outputDesc == NULL) { | 
|  | 2492 | ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id); | 
|  | 2493 | return BAD_VALUE; | 
|  | 2494 | } | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2495 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", | 
|  | 2496 | outputDesc->mIoHandle); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2497 | if (patchDesc != 0) { | 
|  | 2498 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { | 
|  | 2499 | ALOGV("createAudioPatch() source id differs for patch current id %d new id %d", | 
|  | 2500 | patchDesc->mPatch.sources[0].id, patch->sources[0].id); | 
|  | 2501 | return BAD_VALUE; | 
|  | 2502 | } | 
|  | 2503 | } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2504 | DeviceVector devices; | 
|  | 2505 | for (size_t i = 0; i < patch->num_sinks; i++) { | 
|  | 2506 | // Only support mix to devices connection | 
|  | 2507 | // TODO add support for mix to mix connection | 
|  | 2508 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { | 
|  | 2509 | ALOGV("createAudioPatch() source mix but sink is not a device"); | 
|  | 2510 | return INVALID_OPERATION; | 
|  | 2511 | } | 
|  | 2512 | sp<DeviceDescriptor> devDesc = | 
|  | 2513 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); | 
|  | 2514 | if (devDesc == 0) { | 
|  | 2515 | ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id); | 
|  | 2516 | return BAD_VALUE; | 
|  | 2517 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2518 |  | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2519 | if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(), | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2520 | devDesc->mAddress, | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2521 | patch->sources[0].sample_rate, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2522 | NULL,  // updatedSamplingRate | 
|  | 2523 | patch->sources[0].format, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2524 | NULL,  // updatedFormat | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2525 | patch->sources[0].channel_mask, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2526 | NULL,  // updatedChannelMask | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2527 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2528 | ALOGV("createAudioPatch() profile not supported for device %08x", | 
|  | 2529 | devDesc->type()); | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2530 | return INVALID_OPERATION; | 
|  | 2531 | } | 
|  | 2532 | devices.add(devDesc); | 
|  | 2533 | } | 
|  | 2534 | if (devices.size() == 0) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2535 | return INVALID_OPERATION; | 
|  | 2536 | } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2537 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2538 | // TODO: reconfigure output format and channels here | 
|  | 2539 | ALOGV("createAudioPatch() setting device %08x on output %d", | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2540 | devices.types(), outputDesc->mIoHandle); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2541 | setOutputDevice(outputDesc, devices.types(), true, 0, handle); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2542 | index = mAudioPatches.indexOfKey(*handle); | 
|  | 2543 | if (index >= 0) { | 
|  | 2544 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { | 
|  | 2545 | ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided"); | 
|  | 2546 | } | 
|  | 2547 | patchDesc = mAudioPatches.valueAt(index); | 
|  | 2548 | patchDesc->mUid = uid; | 
|  | 2549 | ALOGV("createAudioPatch() success"); | 
|  | 2550 | } else { | 
|  | 2551 | ALOGW("createAudioPatch() setOutputDevice() failed to create a patch"); | 
|  | 2552 | return INVALID_OPERATION; | 
|  | 2553 | } | 
|  | 2554 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { | 
|  | 2555 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { | 
|  | 2556 | // input device to input mix connection | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2557 | // only one sink supported when connecting an input device to a mix | 
|  | 2558 | if (patch->num_sinks > 1) { | 
|  | 2559 | return INVALID_OPERATION; | 
|  | 2560 | } | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2561 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2562 | if (inputDesc == NULL) { | 
|  | 2563 | return BAD_VALUE; | 
|  | 2564 | } | 
|  | 2565 | if (patchDesc != 0) { | 
|  | 2566 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { | 
|  | 2567 | return BAD_VALUE; | 
|  | 2568 | } | 
|  | 2569 | } | 
|  | 2570 | sp<DeviceDescriptor> devDesc = | 
|  | 2571 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); | 
|  | 2572 | if (devDesc == 0) { | 
|  | 2573 | return BAD_VALUE; | 
|  | 2574 | } | 
|  | 2575 |  | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2576 | if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(), | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2577 | devDesc->mAddress, | 
|  | 2578 | patch->sinks[0].sample_rate, | 
|  | 2579 | NULL, /*updatedSampleRate*/ | 
|  | 2580 | patch->sinks[0].format, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2581 | NULL, /*updatedFormat*/ | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2582 | patch->sinks[0].channel_mask, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2583 | NULL, /*updatedChannelMask*/ | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2584 | // FIXME for the parameter type, | 
|  | 2585 | // and the NONE | 
|  | 2586 | (audio_output_flags_t) | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2587 | AUDIO_INPUT_FLAG_NONE)) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2588 | return INVALID_OPERATION; | 
|  | 2589 | } | 
|  | 2590 | // TODO: reconfigure output format and channels here | 
|  | 2591 | ALOGV("createAudioPatch() setting device %08x on output %d", | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2592 | devDesc->type(), inputDesc->mIoHandle); | 
|  | 2593 | setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2594 | index = mAudioPatches.indexOfKey(*handle); | 
|  | 2595 | if (index >= 0) { | 
|  | 2596 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { | 
|  | 2597 | ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided"); | 
|  | 2598 | } | 
|  | 2599 | patchDesc = mAudioPatches.valueAt(index); | 
|  | 2600 | patchDesc->mUid = uid; | 
|  | 2601 | ALOGV("createAudioPatch() success"); | 
|  | 2602 | } else { | 
|  | 2603 | ALOGW("createAudioPatch() setInputDevice() failed to create a patch"); | 
|  | 2604 | return INVALID_OPERATION; | 
|  | 2605 | } | 
|  | 2606 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { | 
|  | 2607 | // device to device connection | 
|  | 2608 | if (patchDesc != 0) { | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2609 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2610 | return BAD_VALUE; | 
|  | 2611 | } | 
|  | 2612 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2613 | sp<DeviceDescriptor> srcDeviceDesc = | 
|  | 2614 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); | 
| Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 2615 | if (srcDeviceDesc == 0) { | 
|  | 2616 | return BAD_VALUE; | 
|  | 2617 | } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2618 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2619 | //update source and sink with our own data as the data passed in the patch may | 
|  | 2620 | // be incomplete. | 
|  | 2621 | struct audio_patch newPatch = *patch; | 
|  | 2622 | srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2623 |  | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2624 | for (size_t i = 0; i < patch->num_sinks; i++) { | 
|  | 2625 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { | 
|  | 2626 | ALOGV("createAudioPatch() source device but one sink is not a device"); | 
|  | 2627 | return INVALID_OPERATION; | 
|  | 2628 | } | 
|  | 2629 |  | 
|  | 2630 | sp<DeviceDescriptor> sinkDeviceDesc = | 
|  | 2631 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); | 
|  | 2632 | if (sinkDeviceDesc == 0) { | 
|  | 2633 | return BAD_VALUE; | 
|  | 2634 | } | 
|  | 2635 | sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]); | 
|  | 2636 |  | 
| Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2637 | // create a software bridge in PatchPanel if: | 
|  | 2638 | // - source and sink devices are on differnt HW modules OR | 
|  | 2639 | // - audio HAL version is < 3.0 | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 2640 | if ((srcDeviceDesc->getModuleHandle() != sinkDeviceDesc->getModuleHandle()) || | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 2641 | (srcDeviceDesc->mModule->getHalVersion() < AUDIO_DEVICE_API_VERSION_3_0)) { | 
| Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2642 | // support only one sink device for now to simplify output selection logic | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2643 | if (patch->num_sinks > 1) { | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2644 | return INVALID_OPERATION; | 
|  | 2645 | } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2646 | SortedVector<audio_io_handle_t> outputs = | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2647 | getOutputsForDevice(sinkDeviceDesc->type(), mOutputs); | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2648 | // if the sink device is reachable via an opened output stream, request to go via | 
|  | 2649 | // this output stream by adding a second source to the patch description | 
| Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 2650 | audio_io_handle_t output = selectOutput(outputs, | 
|  | 2651 | AUDIO_OUTPUT_FLAG_NONE, | 
|  | 2652 | AUDIO_FORMAT_INVALID); | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2653 | if (output != AUDIO_IO_HANDLE_NONE) { | 
|  | 2654 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
|  | 2655 | if (outputDesc->isDuplicated()) { | 
|  | 2656 | return INVALID_OPERATION; | 
|  | 2657 | } | 
|  | 2658 | outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]); | 
| Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2659 | newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2660 | newPatch.num_sources = 2; | 
|  | 2661 | } | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2662 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2663 | } | 
|  | 2664 | // TODO: check from routing capabilities in config file and other conflicting patches | 
|  | 2665 |  | 
|  | 2666 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; | 
|  | 2667 | if (index >= 0) { | 
|  | 2668 | afPatchHandle = patchDesc->mAfPatchHandle; | 
|  | 2669 | } | 
|  | 2670 |  | 
|  | 2671 | status_t status = mpClientInterface->createAudioPatch(&newPatch, | 
|  | 2672 | &afPatchHandle, | 
|  | 2673 | 0); | 
|  | 2674 | ALOGV("createAudioPatch() patch panel returned %d patchHandle %d", | 
|  | 2675 | status, afPatchHandle); | 
|  | 2676 | if (status == NO_ERROR) { | 
|  | 2677 | if (index < 0) { | 
| François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 2678 | patchDesc = new AudioPatch(&newPatch, uid); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2679 | addAudioPatch(patchDesc->mHandle, patchDesc); | 
|  | 2680 | } else { | 
|  | 2681 | patchDesc->mPatch = newPatch; | 
|  | 2682 | } | 
|  | 2683 | patchDesc->mAfPatchHandle = afPatchHandle; | 
|  | 2684 | *handle = patchDesc->mHandle; | 
|  | 2685 | nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2686 | mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2687 | } else { | 
|  | 2688 | ALOGW("createAudioPatch() patch panel could not connect device patch, error %d", | 
|  | 2689 | status); | 
|  | 2690 | return INVALID_OPERATION; | 
|  | 2691 | } | 
|  | 2692 | } else { | 
|  | 2693 | return BAD_VALUE; | 
|  | 2694 | } | 
|  | 2695 | } else { | 
|  | 2696 | return BAD_VALUE; | 
|  | 2697 | } | 
|  | 2698 | return NO_ERROR; | 
|  | 2699 | } | 
|  | 2700 |  | 
|  | 2701 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, | 
|  | 2702 | uid_t uid) | 
|  | 2703 | { | 
|  | 2704 | ALOGV("releaseAudioPatch() patch %d", handle); | 
|  | 2705 |  | 
|  | 2706 | ssize_t index = mAudioPatches.indexOfKey(handle); | 
|  | 2707 |  | 
|  | 2708 | if (index < 0) { | 
|  | 2709 | return BAD_VALUE; | 
|  | 2710 | } | 
|  | 2711 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
|  | 2712 | ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", | 
|  | 2713 | mUidCached, patchDesc->mUid, uid); | 
|  | 2714 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { | 
|  | 2715 | return INVALID_OPERATION; | 
|  | 2716 | } | 
|  | 2717 |  | 
|  | 2718 | struct audio_patch *patch = &patchDesc->mPatch; | 
|  | 2719 | patchDesc->mUid = mUidCached; | 
|  | 2720 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2721 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2722 | if (outputDesc == NULL) { | 
|  | 2723 | ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id); | 
|  | 2724 | return BAD_VALUE; | 
|  | 2725 | } | 
|  | 2726 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2727 | setOutputDevice(outputDesc, | 
|  | 2728 | getNewOutputDevice(outputDesc, true /*fromCache*/), | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2729 | true, | 
|  | 2730 | 0, | 
|  | 2731 | NULL); | 
|  | 2732 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { | 
|  | 2733 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2734 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2735 | if (inputDesc == NULL) { | 
|  | 2736 | ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id); | 
|  | 2737 | return BAD_VALUE; | 
|  | 2738 | } | 
|  | 2739 | setInputDevice(inputDesc->mIoHandle, | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 2740 | getNewInputDevice(inputDesc->mIoHandle), | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2741 | true, | 
|  | 2742 | NULL); | 
|  | 2743 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2744 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); | 
|  | 2745 | ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d", | 
|  | 2746 | status, patchDesc->mAfPatchHandle); | 
|  | 2747 | removeAudioPatch(patchDesc->mHandle); | 
|  | 2748 | nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2749 | mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2750 | } else { | 
|  | 2751 | return BAD_VALUE; | 
|  | 2752 | } | 
|  | 2753 | } else { | 
|  | 2754 | return BAD_VALUE; | 
|  | 2755 | } | 
|  | 2756 | return NO_ERROR; | 
|  | 2757 | } | 
|  | 2758 |  | 
|  | 2759 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, | 
|  | 2760 | struct audio_patch *patches, | 
|  | 2761 | unsigned int *generation) | 
|  | 2762 | { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2763 | if (generation == NULL) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2764 | return BAD_VALUE; | 
|  | 2765 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2766 | *generation = curAudioPortGeneration(); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2767 | return mAudioPatches.listAudioPatches(num_patches, patches); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2768 | } | 
|  | 2769 |  | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2770 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2771 | { | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2772 | ALOGV("setAudioPortConfig()"); | 
|  | 2773 |  | 
|  | 2774 | if (config == NULL) { | 
|  | 2775 | return BAD_VALUE; | 
|  | 2776 | } | 
|  | 2777 | ALOGV("setAudioPortConfig() on port handle %d", config->id); | 
|  | 2778 | // Only support gain configuration for now | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2779 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { | 
|  | 2780 | return INVALID_OPERATION; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2781 | } | 
|  | 2782 |  | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2783 | sp<AudioPortConfig> audioPortConfig; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2784 | if (config->type == AUDIO_PORT_TYPE_MIX) { | 
|  | 2785 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2786 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id); | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2787 | if (outputDesc == NULL) { | 
|  | 2788 | return BAD_VALUE; | 
|  | 2789 | } | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2790 | ALOG_ASSERT(!outputDesc->isDuplicated(), | 
|  | 2791 | "setAudioPortConfig() called on duplicated output %d", | 
|  | 2792 | outputDesc->mIoHandle); | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2793 | audioPortConfig = outputDesc; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2794 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2795 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id); | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2796 | if (inputDesc == NULL) { | 
|  | 2797 | return BAD_VALUE; | 
|  | 2798 | } | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2799 | audioPortConfig = inputDesc; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2800 | } else { | 
|  | 2801 | return BAD_VALUE; | 
|  | 2802 | } | 
|  | 2803 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { | 
|  | 2804 | sp<DeviceDescriptor> deviceDesc; | 
|  | 2805 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { | 
|  | 2806 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); | 
|  | 2807 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { | 
|  | 2808 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); | 
|  | 2809 | } else { | 
|  | 2810 | return BAD_VALUE; | 
|  | 2811 | } | 
|  | 2812 | if (deviceDesc == NULL) { | 
|  | 2813 | return BAD_VALUE; | 
|  | 2814 | } | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2815 | audioPortConfig = deviceDesc; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2816 | } else { | 
|  | 2817 | return BAD_VALUE; | 
|  | 2818 | } | 
|  | 2819 |  | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2820 | struct audio_port_config backupConfig; | 
|  | 2821 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); | 
|  | 2822 | if (status == NO_ERROR) { | 
|  | 2823 | struct audio_port_config newConfig; | 
|  | 2824 | audioPortConfig->toAudioPortConfig(&newConfig, config); | 
|  | 2825 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2826 | } | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2827 | if (status != NO_ERROR) { | 
|  | 2828 | audioPortConfig->applyAudioPortConfig(&backupConfig); | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2829 | } | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2830 |  | 
|  | 2831 | return status; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2832 | } | 
|  | 2833 |  | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2834 | void AudioPolicyManager::releaseResourcesForUid(uid_t uid) | 
|  | 2835 | { | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2836 | clearAudioSources(uid); | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2837 | clearAudioPatches(uid); | 
|  | 2838 | clearSessionRoutes(uid); | 
|  | 2839 | } | 
|  | 2840 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2841 | void AudioPolicyManager::clearAudioPatches(uid_t uid) | 
|  | 2842 | { | 
| Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 2843 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--)  { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2844 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); | 
|  | 2845 | if (patchDesc->mUid == uid) { | 
| Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 2846 | releaseAudioPatch(mAudioPatches.keyAt(i), uid); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2847 | } | 
|  | 2848 | } | 
|  | 2849 | } | 
|  | 2850 |  | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2851 | void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy, | 
|  | 2852 | audio_io_handle_t ouptutToSkip) | 
|  | 2853 | { | 
|  | 2854 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); | 
|  | 2855 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); | 
|  | 2856 | for (size_t j = 0; j < mOutputs.size(); j++) { | 
|  | 2857 | if (mOutputs.keyAt(j) == ouptutToSkip) { | 
|  | 2858 | continue; | 
|  | 2859 | } | 
|  | 2860 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j); | 
|  | 2861 | if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) { | 
|  | 2862 | continue; | 
|  | 2863 | } | 
|  | 2864 | // If the default device for this strategy is on another output mix, | 
|  | 2865 | // invalidate all tracks in this strategy to force re connection. | 
|  | 2866 | // Otherwise select new device on the output mix. | 
|  | 2867 | if (outputs.indexOf(mOutputs.keyAt(j)) < 0) { | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2868 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2869 | if (getStrategy((audio_stream_type_t)stream) == strategy) { | 
|  | 2870 | mpClientInterface->invalidateStream((audio_stream_type_t)stream); | 
|  | 2871 | } | 
|  | 2872 | } | 
|  | 2873 | } else { | 
|  | 2874 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); | 
|  | 2875 | setOutputDevice(outputDesc, newDevice, false); | 
|  | 2876 | } | 
|  | 2877 | } | 
|  | 2878 | } | 
|  | 2879 |  | 
|  | 2880 | void AudioPolicyManager::clearSessionRoutes(uid_t uid) | 
|  | 2881 | { | 
|  | 2882 | // remove output routes associated with this uid | 
|  | 2883 | SortedVector<routing_strategy> affectedStrategies; | 
|  | 2884 | for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--)  { | 
|  | 2885 | sp<SessionRoute> route = mOutputRoutes.valueAt(i); | 
|  | 2886 | if (route->mUid == uid) { | 
|  | 2887 | mOutputRoutes.removeItemsAt(i); | 
|  | 2888 | if (route->mDeviceDescriptor != 0) { | 
|  | 2889 | affectedStrategies.add(getStrategy(route->mStreamType)); | 
|  | 2890 | } | 
|  | 2891 | } | 
|  | 2892 | } | 
|  | 2893 | // reroute outputs if necessary | 
|  | 2894 | for (size_t i = 0; i < affectedStrategies.size(); i++) { | 
|  | 2895 | checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE); | 
|  | 2896 | } | 
|  | 2897 |  | 
|  | 2898 | // remove input routes associated with this uid | 
|  | 2899 | SortedVector<audio_source_t> affectedSources; | 
|  | 2900 | for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--)  { | 
|  | 2901 | sp<SessionRoute> route = mInputRoutes.valueAt(i); | 
|  | 2902 | if (route->mUid == uid) { | 
|  | 2903 | mInputRoutes.removeItemsAt(i); | 
|  | 2904 | if (route->mDeviceDescriptor != 0) { | 
|  | 2905 | affectedSources.add(route->mSource); | 
|  | 2906 | } | 
|  | 2907 | } | 
|  | 2908 | } | 
|  | 2909 | // reroute inputs if necessary | 
|  | 2910 | SortedVector<audio_io_handle_t> inputsToClose; | 
|  | 2911 | for (size_t i = 0; i < mInputs.size(); i++) { | 
|  | 2912 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2913 | if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) { | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2914 | inputsToClose.add(inputDesc->mIoHandle); | 
|  | 2915 | } | 
|  | 2916 | } | 
|  | 2917 | for (size_t i = 0; i < inputsToClose.size(); i++) { | 
|  | 2918 | closeInput(inputsToClose[i]); | 
|  | 2919 | } | 
|  | 2920 | } | 
|  | 2921 |  | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2922 | void AudioPolicyManager::clearAudioSources(uid_t uid) | 
|  | 2923 | { | 
|  | 2924 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--)  { | 
|  | 2925 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); | 
|  | 2926 | if (sourceDesc->mUid == uid) { | 
|  | 2927 | stopAudioSource(mAudioSources.keyAt(i)); | 
|  | 2928 | } | 
|  | 2929 | } | 
|  | 2930 | } | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2931 |  | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2932 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, | 
|  | 2933 | audio_io_handle_t *ioHandle, | 
|  | 2934 | audio_devices_t *device) | 
|  | 2935 | { | 
| Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 2936 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); | 
|  | 2937 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT); | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 2938 | *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD); | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2939 |  | 
| François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 2940 | return mSoundTriggerSessions.acquireSession(*session, *ioHandle); | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2941 | } | 
|  | 2942 |  | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2943 | status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source, | 
|  | 2944 | const audio_attributes_t *attributes, | 
|  | 2945 | audio_io_handle_t *handle, | 
|  | 2946 | uid_t uid) | 
| Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 2947 | { | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2948 | ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle); | 
|  | 2949 | if (source == NULL || attributes == NULL || handle == NULL) { | 
|  | 2950 | return BAD_VALUE; | 
|  | 2951 | } | 
|  | 2952 |  | 
|  | 2953 | *handle = AUDIO_IO_HANDLE_NONE; | 
|  | 2954 |  | 
|  | 2955 | if (source->role != AUDIO_PORT_ROLE_SOURCE || | 
|  | 2956 | source->type != AUDIO_PORT_TYPE_DEVICE) { | 
|  | 2957 | ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type); | 
|  | 2958 | return INVALID_OPERATION; | 
|  | 2959 | } | 
|  | 2960 |  | 
|  | 2961 | sp<DeviceDescriptor> srcDeviceDesc = | 
|  | 2962 | mAvailableInputDevices.getDevice(source->ext.device.type, | 
|  | 2963 | String8(source->ext.device.address)); | 
|  | 2964 | if (srcDeviceDesc == 0) { | 
|  | 2965 | ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type); | 
|  | 2966 | return BAD_VALUE; | 
|  | 2967 | } | 
|  | 2968 | sp<AudioSourceDescriptor> sourceDesc = | 
|  | 2969 | new AudioSourceDescriptor(srcDeviceDesc, attributes, uid); | 
|  | 2970 |  | 
|  | 2971 | struct audio_patch dummyPatch; | 
|  | 2972 | sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid); | 
|  | 2973 | sourceDesc->mPatchDesc = patchDesc; | 
|  | 2974 |  | 
|  | 2975 | status_t status = connectAudioSource(sourceDesc); | 
|  | 2976 | if (status == NO_ERROR) { | 
|  | 2977 | mAudioSources.add(sourceDesc->getHandle(), sourceDesc); | 
|  | 2978 | *handle = sourceDesc->getHandle(); | 
|  | 2979 | } | 
|  | 2980 | return status; | 
|  | 2981 | } | 
|  | 2982 |  | 
|  | 2983 | status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc) | 
|  | 2984 | { | 
|  | 2985 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle()); | 
|  | 2986 |  | 
|  | 2987 | // make sure we only have one patch per source. | 
|  | 2988 | disconnectAudioSource(sourceDesc); | 
|  | 2989 |  | 
|  | 2990 | routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes); | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2991 | audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes); | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2992 | sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice; | 
|  | 2993 |  | 
|  | 2994 | audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true); | 
|  | 2995 | sp<DeviceDescriptor> sinkDeviceDesc = | 
|  | 2996 | mAvailableOutputDevices.getDevice(sinkDevice, String8("")); | 
|  | 2997 |  | 
|  | 2998 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; | 
|  | 2999 | struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch; | 
|  | 3000 |  | 
|  | 3001 | if (srcDeviceDesc->getAudioPort()->mModule->getHandle() == | 
|  | 3002 | sinkDeviceDesc->getAudioPort()->mModule->getHandle() && | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3003 | srcDeviceDesc->getAudioPort()->mModule->getHalVersion() >= AUDIO_DEVICE_API_VERSION_3_0 && | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3004 | srcDeviceDesc->getAudioPort()->mGains.size() > 0) { | 
|  | 3005 | ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__); | 
|  | 3006 | //   create patch between src device and output device | 
|  | 3007 | //   create Hwoutput and add to mHwOutputs | 
|  | 3008 | } else { | 
|  | 3009 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs); | 
|  | 3010 | audio_io_handle_t output = | 
|  | 3011 | selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID); | 
|  | 3012 | if (output == AUDIO_IO_HANDLE_NONE) { | 
|  | 3013 | ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice); | 
|  | 3014 | return INVALID_OPERATION; | 
|  | 3015 | } | 
|  | 3016 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
|  | 3017 | if (outputDesc->isDuplicated()) { | 
|  | 3018 | ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice); | 
|  | 3019 | return INVALID_OPERATION; | 
|  | 3020 | } | 
|  | 3021 | // create a special patch with no sink and two sources: | 
|  | 3022 | // - the second source indicates to PatchPanel through which output mix this patch should | 
|  | 3023 | // be connected as well as the stream type for volume control | 
|  | 3024 | // - the sink is defined by whatever output device is currently selected for the output | 
|  | 3025 | // though which this patch is routed. | 
|  | 3026 | patch->num_sinks = 0; | 
|  | 3027 | patch->num_sources = 2; | 
|  | 3028 | srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL); | 
|  | 3029 | outputDesc->toAudioPortConfig(&patch->sources[1], NULL); | 
|  | 3030 | patch->sources[1].ext.mix.usecase.stream = stream; | 
|  | 3031 | status_t status = mpClientInterface->createAudioPatch(patch, | 
|  | 3032 | &afPatchHandle, | 
|  | 3033 | 0); | 
|  | 3034 | ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__, | 
|  | 3035 | status, afPatchHandle); | 
|  | 3036 | if (status != NO_ERROR) { | 
|  | 3037 | ALOGW("%s patch panel could not connect device patch, error %d", | 
|  | 3038 | __FUNCTION__, status); | 
|  | 3039 | return INVALID_OPERATION; | 
|  | 3040 | } | 
|  | 3041 | uint32_t delayMs = 0; | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3042 | status = startSource(outputDesc, stream, sinkDevice, NULL, &delayMs); | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3043 |  | 
|  | 3044 | if (status != NO_ERROR) { | 
|  | 3045 | mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0); | 
|  | 3046 | return status; | 
|  | 3047 | } | 
|  | 3048 | sourceDesc->mSwOutput = outputDesc; | 
|  | 3049 | if (delayMs != 0) { | 
|  | 3050 | usleep(delayMs * 1000); | 
|  | 3051 | } | 
|  | 3052 | } | 
|  | 3053 |  | 
|  | 3054 | sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle; | 
|  | 3055 | addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc); | 
|  | 3056 |  | 
|  | 3057 | return NO_ERROR; | 
| Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3058 | } | 
|  | 3059 |  | 
| Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 3060 | status_t AudioPolicyManager::stopAudioSource(audio_io_handle_t handle __unused) | 
| Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3061 | { | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3062 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle); | 
|  | 3063 | ALOGV("%s handle %d", __FUNCTION__, handle); | 
|  | 3064 | if (sourceDesc == 0) { | 
|  | 3065 | ALOGW("%s unknown source for handle %d", __FUNCTION__, handle); | 
|  | 3066 | return BAD_VALUE; | 
|  | 3067 | } | 
|  | 3068 | status_t status = disconnectAudioSource(sourceDesc); | 
|  | 3069 |  | 
|  | 3070 | mAudioSources.removeItem(handle); | 
|  | 3071 | return status; | 
|  | 3072 | } | 
|  | 3073 |  | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3074 | status_t AudioPolicyManager::setMasterMono(bool mono) | 
|  | 3075 | { | 
|  | 3076 | if (mMasterMono == mono) { | 
|  | 3077 | return NO_ERROR; | 
|  | 3078 | } | 
|  | 3079 | mMasterMono = mono; | 
|  | 3080 | // if enabling mono we close all offloaded devices, which will invalidate the | 
|  | 3081 | // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible | 
|  | 3082 | // for recreating the new AudioTrack as non-offloaded PCM. | 
|  | 3083 | // | 
|  | 3084 | // If disabling mono, we leave all tracks as is: we don't know which clients | 
|  | 3085 | // and tracks are able to be recreated as offloaded. The next "song" should | 
|  | 3086 | // play back offloaded. | 
|  | 3087 | if (mMasterMono) { | 
|  | 3088 | Vector<audio_io_handle_t> offloaded; | 
|  | 3089 | for (size_t i = 0; i < mOutputs.size(); ++i) { | 
|  | 3090 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
|  | 3091 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { | 
|  | 3092 | offloaded.push(desc->mIoHandle); | 
|  | 3093 | } | 
|  | 3094 | } | 
|  | 3095 | for (size_t i = 0; i < offloaded.size(); ++i) { | 
|  | 3096 | closeOutput(offloaded[i]); | 
|  | 3097 | } | 
|  | 3098 | } | 
|  | 3099 | // update master mono for all remaining outputs | 
|  | 3100 | for (size_t i = 0; i < mOutputs.size(); ++i) { | 
|  | 3101 | updateMono(mOutputs.keyAt(i)); | 
|  | 3102 | } | 
|  | 3103 | return NO_ERROR; | 
|  | 3104 | } | 
|  | 3105 |  | 
|  | 3106 | status_t AudioPolicyManager::getMasterMono(bool *mono) | 
|  | 3107 | { | 
|  | 3108 | *mono = mMasterMono; | 
|  | 3109 | return NO_ERROR; | 
|  | 3110 | } | 
|  | 3111 |  | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3112 | status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc) | 
|  | 3113 | { | 
|  | 3114 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle()); | 
|  | 3115 |  | 
|  | 3116 | sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle); | 
|  | 3117 | if (patchDesc == 0) { | 
|  | 3118 | ALOGW("%s source has no patch with handle %d", __FUNCTION__, | 
|  | 3119 | sourceDesc->mPatchDesc->mHandle); | 
|  | 3120 | return BAD_VALUE; | 
|  | 3121 | } | 
|  | 3122 | removeAudioPatch(sourceDesc->mPatchDesc->mHandle); | 
|  | 3123 |  | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 3124 | audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes); | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3125 | sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote(); | 
|  | 3126 | if (swOutputDesc != 0) { | 
|  | 3127 | stopSource(swOutputDesc, stream, false); | 
|  | 3128 | mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); | 
|  | 3129 | } else { | 
|  | 3130 | sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote(); | 
|  | 3131 | if (hwOutputDesc != 0) { | 
|  | 3132 | //   release patch between src device and output device | 
|  | 3133 | //   close Hwoutput and remove from mHwOutputs | 
|  | 3134 | } else { | 
|  | 3135 | ALOGW("%s source has neither SW nor HW output", __FUNCTION__); | 
|  | 3136 | } | 
|  | 3137 | } | 
|  | 3138 | return NO_ERROR; | 
|  | 3139 | } | 
|  | 3140 |  | 
|  | 3141 | sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput( | 
|  | 3142 | audio_io_handle_t output, routing_strategy strategy) | 
|  | 3143 | { | 
|  | 3144 | sp<AudioSourceDescriptor> source; | 
|  | 3145 | for (size_t i = 0; i < mAudioSources.size(); i++)  { | 
|  | 3146 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); | 
|  | 3147 | routing_strategy sourceStrategy = | 
|  | 3148 | (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes); | 
|  | 3149 | sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote(); | 
|  | 3150 | if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) { | 
|  | 3151 | source = sourceDesc; | 
|  | 3152 | break; | 
|  | 3153 | } | 
|  | 3154 | } | 
|  | 3155 | return source; | 
| Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3156 | } | 
|  | 3157 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3158 | // ---------------------------------------------------------------------------- | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3159 | // AudioPolicyManager | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3160 | // ---------------------------------------------------------------------------- | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3161 | uint32_t AudioPolicyManager::nextAudioPortGeneration() | 
|  | 3162 | { | 
|  | 3163 | return android_atomic_inc(&mAudioPortGeneration); | 
|  | 3164 | } | 
|  | 3165 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3166 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3167 | : | 
|  | 3168 | #ifdef AUDIO_POLICY_TEST | 
|  | 3169 | Thread(false), | 
|  | 3170 | #endif //AUDIO_POLICY_TEST | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3171 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3172 | mA2dpSuspended(false), | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 3173 | mAudioPortGeneration(1), | 
|  | 3174 | mBeaconMuteRefCount(0), | 
|  | 3175 | mBeaconPlayingRefCount(0), | 
| Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3176 | mBeaconMuted(false), | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3177 | mTtsOutputAvailable(false), | 
|  | 3178 | mMasterMono(false) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3179 | { | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3180 | mUidCached = getuid(); | 
|  | 3181 | mpClientInterface = clientInterface; | 
|  | 3182 |  | 
|  | 3183 | // TODO: remove when legacy conf file is removed. true on devices that use DRC on the | 
|  | 3184 | // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly. | 
|  | 3185 | // Note: remove also speaker_drc_enabled from global configuration of XML config file. | 
|  | 3186 | bool speakerDrcEnabled = false; | 
|  | 3187 |  | 
|  | 3188 | #ifdef USE_XML_AUDIO_POLICY_CONF | 
|  | 3189 | mVolumeCurves = new VolumeCurvesCollection(); | 
|  | 3190 | AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices, | 
|  | 3191 | mDefaultOutputDevice, speakerDrcEnabled, | 
|  | 3192 | static_cast<VolumeCurvesCollection *>(mVolumeCurves)); | 
|  | 3193 | PolicySerializer serializer; | 
|  | 3194 | if (serializer.deserialize(AUDIO_POLICY_XML_CONFIG_FILE, config) != NO_ERROR) { | 
|  | 3195 | #else | 
|  | 3196 | mVolumeCurves = new StreamDescriptorCollection(); | 
|  | 3197 | AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices, | 
|  | 3198 | mDefaultOutputDevice, speakerDrcEnabled); | 
|  | 3199 | if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, config) != NO_ERROR) && | 
|  | 3200 | (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, config) != NO_ERROR)) { | 
|  | 3201 | #endif | 
|  | 3202 | ALOGE("could not load audio policy configuration file, setting defaults"); | 
|  | 3203 | config.setDefault(); | 
|  | 3204 | } | 
|  | 3205 | // must be done after reading the policy (since conditionned by Speaker Drc Enabling) | 
|  | 3206 | mVolumeCurves->initializeVolumeCurves(speakerDrcEnabled); | 
|  | 3207 |  | 
|  | 3208 | // 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] | 3209 | audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance(); | 
|  | 3210 | if (!engineInstance) { | 
|  | 3211 | ALOGE("%s:  Could not get an instance of policy engine", __FUNCTION__); | 
|  | 3212 | return; | 
|  | 3213 | } | 
|  | 3214 | // Retrieve the Policy Manager Interface | 
|  | 3215 | mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>(); | 
|  | 3216 | if (mEngine == NULL) { | 
|  | 3217 | ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__); | 
|  | 3218 | return; | 
|  | 3219 | } | 
|  | 3220 | mEngine->setObserver(this); | 
|  | 3221 | status_t status = mEngine->initCheck(); | 
| Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 3222 | (void) status; | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3223 | ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status); | 
|  | 3224 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3225 | // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3226 | // open all output streams needed to access attached devices | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3227 | audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types(); | 
|  | 3228 | audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3229 | for (size_t i = 0; i < mHwModules.size(); i++) { | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3230 | mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->getName()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3231 | if (mHwModules[i]->mHandle == 0) { | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3232 | ALOGW("could not open HW module %s", mHwModules[i]->getName()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3233 | continue; | 
|  | 3234 | } | 
|  | 3235 | // open all output streams needed to access attached devices | 
|  | 3236 | // except for direct output streams that are only opened when they are actually | 
|  | 3237 | // required by an app. | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3238 | // This also validates mAvailableOutputDevices list | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3239 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) | 
|  | 3240 | { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3241 | const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j]; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3242 |  | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3243 | if (!outProfile->hasSupportedDevices()) { | 
|  | 3244 | ALOGW("Output profile contains no device on module %s", mHwModules[i]->getName()); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3245 | continue; | 
|  | 3246 | } | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3247 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) { | 
| Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3248 | mTtsOutputAvailable = true; | 
|  | 3249 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3250 |  | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3251 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3252 | continue; | 
|  | 3253 | } | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3254 | audio_devices_t profileType = outProfile->getSupportedDevicesType(); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3255 | if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) { | 
|  | 3256 | profileType = mDefaultOutputDevice->type(); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3257 | } else { | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3258 | // chose first device present in profile's SupportedDevices also part of | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3259 | // outputDeviceTypes | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3260 | profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3261 | } | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3262 | if ((profileType & outputDeviceTypes) == 0) { | 
|  | 3263 | continue; | 
|  | 3264 | } | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3265 | sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile, | 
|  | 3266 | mpClientInterface); | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3267 | const DeviceVector &supportedDevices = outProfile->getSupportedDevices(); | 
|  | 3268 | const DeviceVector &devicesForType = supportedDevices.getDevicesFromType(profileType); | 
|  | 3269 | String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress | 
|  | 3270 | : String8(""); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3271 |  | 
|  | 3272 | outputDesc->mDevice = profileType; | 
|  | 3273 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
|  | 3274 | config.sample_rate = outputDesc->mSamplingRate; | 
|  | 3275 | config.channel_mask = outputDesc->mChannelMask; | 
|  | 3276 | config.format = outputDesc->mFormat; | 
|  | 3277 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3278 | status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(), | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3279 | &output, | 
|  | 3280 | &config, | 
|  | 3281 | &outputDesc->mDevice, | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3282 | address, | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3283 | &outputDesc->mLatency, | 
|  | 3284 | outputDesc->mFlags); | 
|  | 3285 |  | 
|  | 3286 | if (status != NO_ERROR) { | 
|  | 3287 | ALOGW("Cannot open output stream for device %08x on hw module %s", | 
|  | 3288 | outputDesc->mDevice, | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3289 | mHwModules[i]->getName()); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3290 | } else { | 
|  | 3291 | outputDesc->mSamplingRate = config.sample_rate; | 
|  | 3292 | outputDesc->mChannelMask = config.channel_mask; | 
|  | 3293 | outputDesc->mFormat = config.format; | 
|  | 3294 |  | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3295 | for (size_t k = 0; k  < supportedDevices.size(); k++) { | 
|  | 3296 | ssize_t index = mAvailableOutputDevices.indexOf(supportedDevices[k]); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3297 | // 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] | 3298 | if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) { | 
|  | 3299 | mAvailableOutputDevices[index]->attach(mHwModules[i]); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3300 | } | 
|  | 3301 | } | 
|  | 3302 | if (mPrimaryOutput == 0 && | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3303 | outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3304 | mPrimaryOutput = outputDesc; | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3305 | } | 
|  | 3306 | addOutput(output, outputDesc); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3307 | setOutputDevice(outputDesc, | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3308 | outputDesc->mDevice, | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3309 | true, | 
|  | 3310 | 0, | 
|  | 3311 | NULL, | 
|  | 3312 | address.string()); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3313 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3314 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3315 | // open input streams needed to access attached devices to validate | 
|  | 3316 | // mAvailableInputDevices list | 
|  | 3317 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) | 
|  | 3318 | { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3319 | const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j]; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3320 |  | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3321 | if (!inProfile->hasSupportedDevices()) { | 
|  | 3322 | ALOGW("Input profile contains no device on module %s", mHwModules[i]->getName()); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3323 | continue; | 
|  | 3324 | } | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3325 | // chose first device present in profile's SupportedDevices also part of | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3326 | // inputDeviceTypes | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3327 | audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes); | 
|  | 3328 |  | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3329 | if ((profileType & inputDeviceTypes) == 0) { | 
|  | 3330 | continue; | 
|  | 3331 | } | 
| Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 3332 | sp<AudioInputDescriptor> inputDesc = | 
|  | 3333 | new AudioInputDescriptor(inProfile); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3334 |  | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3335 | inputDesc->mDevice = profileType; | 
|  | 3336 |  | 
| Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 3337 | // find the address | 
|  | 3338 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType); | 
|  | 3339 | //   the inputs vector must be of size 1, but we don't want to crash here | 
|  | 3340 | String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress | 
|  | 3341 | : String8(""); | 
|  | 3342 | ALOGV("  for input device 0x%x using address %s", profileType, address.string()); | 
|  | 3343 | ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!"); | 
|  | 3344 |  | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3345 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
|  | 3346 | config.sample_rate = inputDesc->mSamplingRate; | 
|  | 3347 | config.channel_mask = inputDesc->mChannelMask; | 
|  | 3348 | config.format = inputDesc->mFormat; | 
|  | 3349 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3350 | status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(), | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3351 | &input, | 
|  | 3352 | &config, | 
|  | 3353 | &inputDesc->mDevice, | 
| Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 3354 | address, | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3355 | AUDIO_SOURCE_MIC, | 
|  | 3356 | AUDIO_INPUT_FLAG_NONE); | 
|  | 3357 |  | 
|  | 3358 | if (status == NO_ERROR) { | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3359 | const DeviceVector &supportedDevices = inProfile->getSupportedDevices(); | 
|  | 3360 | for (size_t k = 0; k  < supportedDevices.size(); k++) { | 
|  | 3361 | ssize_t index =  mAvailableInputDevices.indexOf(supportedDevices[k]); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3362 | // 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] | 3363 | if (index >= 0) { | 
|  | 3364 | sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index]; | 
|  | 3365 | if (!devDesc->isAttached()) { | 
|  | 3366 | devDesc->attach(mHwModules[i]); | 
|  | 3367 | devDesc->importAudioPort(inProfile); | 
|  | 3368 | } | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3369 | } | 
|  | 3370 | } | 
|  | 3371 | mpClientInterface->closeInput(input); | 
|  | 3372 | } else { | 
|  | 3373 | ALOGW("Cannot open input stream for device %08x on hw module %s", | 
|  | 3374 | inputDesc->mDevice, | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3375 | mHwModules[i]->getName()); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3376 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3377 | } | 
|  | 3378 | } | 
|  | 3379 | // make sure all attached devices have been allocated a unique ID | 
|  | 3380 | for (size_t i = 0; i  < mAvailableOutputDevices.size();) { | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 3381 | if (!mAvailableOutputDevices[i]->isAttached()) { | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3382 | ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type()); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3383 | mAvailableOutputDevices.remove(mAvailableOutputDevices[i]); | 
|  | 3384 | continue; | 
|  | 3385 | } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3386 | // The device is now validated and can be appended to the available devices of the engine | 
|  | 3387 | mEngine->setDeviceConnectionState(mAvailableOutputDevices[i], | 
|  | 3388 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3389 | i++; | 
|  | 3390 | } | 
|  | 3391 | for (size_t i = 0; i  < mAvailableInputDevices.size();) { | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 3392 | if (!mAvailableInputDevices[i]->isAttached()) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3393 | ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type()); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3394 | mAvailableInputDevices.remove(mAvailableInputDevices[i]); | 
|  | 3395 | continue; | 
|  | 3396 | } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3397 | // The device is now validated and can be appended to the available devices of the engine | 
|  | 3398 | mEngine->setDeviceConnectionState(mAvailableInputDevices[i], | 
|  | 3399 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3400 | i++; | 
|  | 3401 | } | 
|  | 3402 | // make sure default device is reachable | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3403 | if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3404 | ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type()); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3405 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3406 |  | 
|  | 3407 | ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output"); | 
|  | 3408 |  | 
|  | 3409 | updateDevicesAndOutputs(); | 
|  | 3410 |  | 
|  | 3411 | #ifdef AUDIO_POLICY_TEST | 
|  | 3412 | if (mPrimaryOutput != 0) { | 
|  | 3413 | AudioParameter outputCmd = AudioParameter(); | 
|  | 3414 | outputCmd.addInt(String8("set_id"), 0); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3415 | mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3416 |  | 
|  | 3417 | mTestDevice = AUDIO_DEVICE_OUT_SPEAKER; | 
|  | 3418 | mTestSamplingRate = 44100; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3419 | mTestFormat = AUDIO_FORMAT_PCM_16_BIT; | 
|  | 3420 | mTestChannels =  AUDIO_CHANNEL_OUT_STEREO; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3421 | mTestLatencyMs = 0; | 
|  | 3422 | mCurOutput = 0; | 
|  | 3423 | mDirectOutput = false; | 
|  | 3424 | for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { | 
|  | 3425 | mTestOutputs[i] = 0; | 
|  | 3426 | } | 
|  | 3427 |  | 
|  | 3428 | const size_t SIZE = 256; | 
|  | 3429 | char buffer[SIZE]; | 
|  | 3430 | snprintf(buffer, SIZE, "AudioPolicyManagerTest"); | 
|  | 3431 | run(buffer, ANDROID_PRIORITY_AUDIO); | 
|  | 3432 | } | 
|  | 3433 | #endif //AUDIO_POLICY_TEST | 
|  | 3434 | } | 
|  | 3435 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3436 | AudioPolicyManager::~AudioPolicyManager() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3437 | { | 
|  | 3438 | #ifdef AUDIO_POLICY_TEST | 
|  | 3439 | exit(); | 
|  | 3440 | #endif //AUDIO_POLICY_TEST | 
|  | 3441 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 3442 | mpClientInterface->closeOutput(mOutputs.keyAt(i)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3443 | } | 
|  | 3444 | for (size_t i = 0; i < mInputs.size(); i++) { | 
|  | 3445 | mpClientInterface->closeInput(mInputs.keyAt(i)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3446 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3447 | mAvailableOutputDevices.clear(); | 
|  | 3448 | mAvailableInputDevices.clear(); | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3449 | mOutputs.clear(); | 
|  | 3450 | mInputs.clear(); | 
|  | 3451 | mHwModules.clear(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3452 | } | 
|  | 3453 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3454 | status_t AudioPolicyManager::initCheck() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3455 | { | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 3456 | return hasPrimaryOutput() ? NO_ERROR : NO_INIT; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3457 | } | 
|  | 3458 |  | 
|  | 3459 | #ifdef AUDIO_POLICY_TEST | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3460 | bool AudioPolicyManager::threadLoop() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3461 | { | 
|  | 3462 | ALOGV("entering threadLoop()"); | 
|  | 3463 | while (!exitPending()) | 
|  | 3464 | { | 
|  | 3465 | String8 command; | 
|  | 3466 | int valueInt; | 
|  | 3467 | String8 value; | 
|  | 3468 |  | 
|  | 3469 | Mutex::Autolock _l(mLock); | 
|  | 3470 | mWaitWorkCV.waitRelative(mLock, milliseconds(50)); | 
|  | 3471 |  | 
|  | 3472 | command = mpClientInterface->getParameters(0, String8("test_cmd_policy")); | 
|  | 3473 | AudioParameter param = AudioParameter(command); | 
|  | 3474 |  | 
|  | 3475 | if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR && | 
|  | 3476 | valueInt != 0) { | 
|  | 3477 | ALOGV("Test command %s received", command.string()); | 
|  | 3478 | String8 target; | 
|  | 3479 | if (param.get(String8("target"), target) != NO_ERROR) { | 
|  | 3480 | target = "Manager"; | 
|  | 3481 | } | 
|  | 3482 | if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) { | 
|  | 3483 | param.remove(String8("test_cmd_policy_output")); | 
|  | 3484 | mCurOutput = valueInt; | 
|  | 3485 | } | 
|  | 3486 | if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) { | 
|  | 3487 | param.remove(String8("test_cmd_policy_direct")); | 
|  | 3488 | if (value == "false") { | 
|  | 3489 | mDirectOutput = false; | 
|  | 3490 | } else if (value == "true") { | 
|  | 3491 | mDirectOutput = true; | 
|  | 3492 | } | 
|  | 3493 | } | 
|  | 3494 | if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) { | 
|  | 3495 | param.remove(String8("test_cmd_policy_input")); | 
|  | 3496 | mTestInput = valueInt; | 
|  | 3497 | } | 
|  | 3498 |  | 
|  | 3499 | if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) { | 
|  | 3500 | param.remove(String8("test_cmd_policy_format")); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3501 | int format = AUDIO_FORMAT_INVALID; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3502 | if (value == "PCM 16 bits") { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3503 | format = AUDIO_FORMAT_PCM_16_BIT; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3504 | } else if (value == "PCM 8 bits") { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3505 | format = AUDIO_FORMAT_PCM_8_BIT; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3506 | } else if (value == "Compressed MP3") { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3507 | format = AUDIO_FORMAT_MP3; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3508 | } | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3509 | if (format != AUDIO_FORMAT_INVALID) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3510 | if (target == "Manager") { | 
|  | 3511 | mTestFormat = format; | 
|  | 3512 | } else if (mTestOutputs[mCurOutput] != 0) { | 
|  | 3513 | AudioParameter outputParam = AudioParameter(); | 
|  | 3514 | outputParam.addInt(String8("format"), format); | 
|  | 3515 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); | 
|  | 3516 | } | 
|  | 3517 | } | 
|  | 3518 | } | 
|  | 3519 | if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) { | 
|  | 3520 | param.remove(String8("test_cmd_policy_channels")); | 
|  | 3521 | int channels = 0; | 
|  | 3522 |  | 
|  | 3523 | if (value == "Channels Stereo") { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3524 | channels =  AUDIO_CHANNEL_OUT_STEREO; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3525 | } else if (value == "Channels Mono") { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3526 | channels =  AUDIO_CHANNEL_OUT_MONO; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3527 | } | 
|  | 3528 | if (channels != 0) { | 
|  | 3529 | if (target == "Manager") { | 
|  | 3530 | mTestChannels = channels; | 
|  | 3531 | } else if (mTestOutputs[mCurOutput] != 0) { | 
|  | 3532 | AudioParameter outputParam = AudioParameter(); | 
|  | 3533 | outputParam.addInt(String8("channels"), channels); | 
|  | 3534 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); | 
|  | 3535 | } | 
|  | 3536 | } | 
|  | 3537 | } | 
|  | 3538 | if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) { | 
|  | 3539 | param.remove(String8("test_cmd_policy_sampleRate")); | 
|  | 3540 | if (valueInt >= 0 && valueInt <= 96000) { | 
|  | 3541 | int samplingRate = valueInt; | 
|  | 3542 | if (target == "Manager") { | 
|  | 3543 | mTestSamplingRate = samplingRate; | 
|  | 3544 | } else if (mTestOutputs[mCurOutput] != 0) { | 
|  | 3545 | AudioParameter outputParam = AudioParameter(); | 
|  | 3546 | outputParam.addInt(String8("sampling_rate"), samplingRate); | 
|  | 3547 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); | 
|  | 3548 | } | 
|  | 3549 | } | 
|  | 3550 | } | 
|  | 3551 |  | 
|  | 3552 | if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) { | 
|  | 3553 | param.remove(String8("test_cmd_policy_reopen")); | 
|  | 3554 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3555 | mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput);); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3556 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3557 | audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3558 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3559 | removeOutput(mPrimaryOutput->mIoHandle); | 
|  | 3560 | sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL, | 
|  | 3561 | mpClientInterface); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3562 | outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3563 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
|  | 3564 | config.sample_rate = outputDesc->mSamplingRate; | 
|  | 3565 | config.channel_mask = outputDesc->mChannelMask; | 
|  | 3566 | config.format = outputDesc->mFormat; | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3567 | audio_io_handle_t handle; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3568 | status_t status = mpClientInterface->openOutput(moduleHandle, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3569 | &handle, | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3570 | &config, | 
|  | 3571 | &outputDesc->mDevice, | 
|  | 3572 | String8(""), | 
|  | 3573 | &outputDesc->mLatency, | 
|  | 3574 | outputDesc->mFlags); | 
|  | 3575 | if (status != NO_ERROR) { | 
|  | 3576 | ALOGE("Failed to reopen hardware output stream, " | 
|  | 3577 | "samplingRate: %d, format %d, channels %d", | 
|  | 3578 | outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3579 | } else { | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3580 | outputDesc->mSamplingRate = config.sample_rate; | 
|  | 3581 | outputDesc->mChannelMask = config.channel_mask; | 
|  | 3582 | outputDesc->mFormat = config.format; | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3583 | mPrimaryOutput = outputDesc; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3584 | AudioParameter outputCmd = AudioParameter(); | 
|  | 3585 | outputCmd.addInt(String8("set_id"), 0); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3586 | mpClientInterface->setParameters(handle, outputCmd.toString()); | 
|  | 3587 | addOutput(handle, outputDesc); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3588 | } | 
|  | 3589 | } | 
|  | 3590 |  | 
|  | 3591 |  | 
|  | 3592 | mpClientInterface->setParameters(0, String8("test_cmd_policy=")); | 
|  | 3593 | } | 
|  | 3594 | } | 
|  | 3595 | return false; | 
|  | 3596 | } | 
|  | 3597 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3598 | void AudioPolicyManager::exit() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3599 | { | 
|  | 3600 | { | 
|  | 3601 | AutoMutex _l(mLock); | 
|  | 3602 | requestExit(); | 
|  | 3603 | mWaitWorkCV.signal(); | 
|  | 3604 | } | 
|  | 3605 | requestExitAndWait(); | 
|  | 3606 | } | 
|  | 3607 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3608 | int AudioPolicyManager::testOutputIndex(audio_io_handle_t output) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3609 | { | 
|  | 3610 | for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { | 
|  | 3611 | if (output == mTestOutputs[i]) return i; | 
|  | 3612 | } | 
|  | 3613 | return 0; | 
|  | 3614 | } | 
|  | 3615 | #endif //AUDIO_POLICY_TEST | 
|  | 3616 |  | 
|  | 3617 | // --- | 
|  | 3618 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3619 | void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3620 | { | 
| François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 3621 | outputDesc->setIoHandle(output); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3622 | mOutputs.add(output, outputDesc); | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3623 | updateMono(output); // update mono status when adding to output list | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3624 | nextAudioPortGeneration(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3625 | } | 
|  | 3626 |  | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3627 | void AudioPolicyManager::removeOutput(audio_io_handle_t output) | 
|  | 3628 | { | 
|  | 3629 | mOutputs.removeItem(output); | 
|  | 3630 | } | 
|  | 3631 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3632 | void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc) | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3633 | { | 
| François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 3634 | inputDesc->setIoHandle(input); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3635 | mInputs.add(input, inputDesc); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3636 | nextAudioPortGeneration(); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3637 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3638 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3639 | void AudioPolicyManager::findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/, | 
| keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3640 | const audio_devices_t device /*in*/, | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3641 | const String8 address /*in*/, | 
|  | 3642 | SortedVector<audio_io_handle_t>& outputs /*out*/) { | 
| keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3643 | sp<DeviceDescriptor> devDesc = | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3644 | desc->mProfile->getSupportedDeviceByAddress(device, address); | 
| keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3645 | if (devDesc != 0) { | 
|  | 3646 | ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s", | 
|  | 3647 | desc->mIoHandle, address.string()); | 
|  | 3648 | outputs.add(desc->mIoHandle); | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3649 | } | 
|  | 3650 | } | 
|  | 3651 |  | 
| Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3652 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3653 | audio_policy_dev_state_t state, | 
|  | 3654 | SortedVector<audio_io_handle_t>& outputs, | 
|  | 3655 | const String8 address) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3656 | { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3657 | audio_devices_t device = devDesc->type(); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3658 | sp<SwAudioOutputDescriptor> desc; | 
| Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 3659 |  | 
|  | 3660 | if (audio_device_is_digital(device)) { | 
|  | 3661 | // erase all current sample rates, formats and channel masks | 
| Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 3662 | devDesc->clearAudioProfiles(); | 
| Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 3663 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3664 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3665 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3666 | // first list already open outputs that can be routed to this device | 
|  | 3667 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 3668 | desc = mOutputs.valueAt(i); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3669 | if (!desc->isDuplicated() && (desc->supportedDevices() & device)) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3670 | if (!device_distinguishes_on_address(device)) { | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3671 | ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i)); | 
|  | 3672 | outputs.add(mOutputs.keyAt(i)); | 
|  | 3673 | } else { | 
|  | 3674 | ALOGV("  checking address match due to device 0x%x", device); | 
| keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3675 | findIoHandlesByAddress(desc, device, address, outputs); | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3676 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3677 | } | 
|  | 3678 | } | 
|  | 3679 | // then look for output profiles that can be routed to this device | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3680 | SortedVector< sp<IOProfile> > profiles; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3681 | for (size_t i = 0; i < mHwModules.size(); i++) | 
|  | 3682 | { | 
|  | 3683 | if (mHwModules[i]->mHandle == 0) { | 
|  | 3684 | continue; | 
|  | 3685 | } | 
|  | 3686 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) | 
|  | 3687 | { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3688 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3689 | if (profile->supportDevice(device)) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3690 | if (!device_distinguishes_on_address(device) || | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3691 | profile->supportDeviceAddress(address)) { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3692 | profiles.add(profile); | 
|  | 3693 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i); | 
|  | 3694 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3695 | } | 
|  | 3696 | } | 
|  | 3697 | } | 
|  | 3698 |  | 
| Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 3699 | ALOGV("  found %zu profiles, %zu outputs", profiles.size(), outputs.size()); | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3700 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3701 | if (profiles.isEmpty() && outputs.isEmpty()) { | 
|  | 3702 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); | 
|  | 3703 | return BAD_VALUE; | 
|  | 3704 | } | 
|  | 3705 |  | 
|  | 3706 | // open outputs for matching profiles if needed. Direct outputs are also opened to | 
|  | 3707 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() | 
|  | 3708 | 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] | 3709 | sp<IOProfile> profile = profiles[profile_index]; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3710 |  | 
|  | 3711 | // nothing to do if one output is already opened for this profile | 
|  | 3712 | size_t j; | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3713 | for (j = 0; j < outputs.size(); j++) { | 
|  | 3714 | desc = mOutputs.valueFor(outputs.itemAt(j)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3715 | if (!desc->isDuplicated() && desc->mProfile == profile) { | 
| Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3716 | // matching profile: save the sample rates, format and channel masks supported | 
|  | 3717 | // by the profile in our device descriptor | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3718 | if (audio_device_is_digital(device)) { | 
|  | 3719 | devDesc->importAudioPort(profile); | 
|  | 3720 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3721 | break; | 
|  | 3722 | } | 
|  | 3723 | } | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3724 | if (j != outputs.size()) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3725 | continue; | 
|  | 3726 | } | 
|  | 3727 |  | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3728 | ALOGV("opening output for device %08x with params %s profile %p", | 
|  | 3729 | device, address.string(), profile.get()); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3730 | desc = new SwAudioOutputDescriptor(profile, mpClientInterface); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3731 | desc->mDevice = device; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3732 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
|  | 3733 | config.sample_rate = desc->mSamplingRate; | 
|  | 3734 | config.channel_mask = desc->mChannelMask; | 
|  | 3735 | config.format = desc->mFormat; | 
|  | 3736 | config.offload_info.sample_rate = desc->mSamplingRate; | 
|  | 3737 | config.offload_info.channel_mask = desc->mChannelMask; | 
|  | 3738 | config.offload_info.format = desc->mFormat; | 
|  | 3739 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3740 | status_t status = mpClientInterface->openOutput(profile->getModuleHandle(), | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3741 | &output, | 
|  | 3742 | &config, | 
|  | 3743 | &desc->mDevice, | 
|  | 3744 | address, | 
|  | 3745 | &desc->mLatency, | 
|  | 3746 | desc->mFlags); | 
|  | 3747 | if (status == NO_ERROR) { | 
|  | 3748 | desc->mSamplingRate = config.sample_rate; | 
|  | 3749 | desc->mChannelMask = config.channel_mask; | 
|  | 3750 | desc->mFormat = config.format; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3751 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3752 | // Here is where the out_set_parameters() for card & device gets called | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3753 | if (!address.isEmpty()) { | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3754 | char *param = audio_device_address_to_parameter(device, address); | 
|  | 3755 | mpClientInterface->setParameters(output, String8(param)); | 
|  | 3756 | free(param); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3757 | } | 
| Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 3758 | updateAudioProfiles(device, output, profile->getAudioProfiles()); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3759 | if (!profile->hasValidAudioProfile()) { | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3760 | ALOGW("checkOutputsForDevice() missing param"); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3761 | mpClientInterface->closeOutput(output); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3762 | output = AUDIO_IO_HANDLE_NONE; | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3763 | } else if (profile->hasDynamicAudioProfile()) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3764 | mpClientInterface->closeOutput(output); | 
| Phil Burk | 702b105 | 2016-03-02 16:38:26 -0800 | [diff] [blame] | 3765 | output = AUDIO_IO_HANDLE_NONE; | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3766 | profile->pickAudioProfile(config.sample_rate, config.channel_mask, config.format); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3767 | config.offload_info.sample_rate = config.sample_rate; | 
|  | 3768 | config.offload_info.channel_mask = config.channel_mask; | 
|  | 3769 | config.offload_info.format = config.format; | 
| Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3770 | status = mpClientInterface->openOutput(profile->getModuleHandle(), | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3771 | &output, | 
|  | 3772 | &config, | 
|  | 3773 | &desc->mDevice, | 
|  | 3774 | address, | 
|  | 3775 | &desc->mLatency, | 
|  | 3776 | desc->mFlags); | 
|  | 3777 | if (status == NO_ERROR) { | 
|  | 3778 | desc->mSamplingRate = config.sample_rate; | 
|  | 3779 | desc->mChannelMask = config.channel_mask; | 
|  | 3780 | desc->mFormat = config.format; | 
|  | 3781 | } else { | 
|  | 3782 | output = AUDIO_IO_HANDLE_NONE; | 
|  | 3783 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3784 | } | 
|  | 3785 |  | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3786 | if (output != AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3787 | addOutput(output, desc); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3788 | if (device_distinguishes_on_address(device) && address != "0") { | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3789 | sp<AudioPolicyMix> policyMix; | 
|  | 3790 | if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3791 | ALOGE("checkOutputsForDevice() cannot find policy for address %s", | 
|  | 3792 | address.string()); | 
|  | 3793 | } | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3794 | policyMix->setOutput(desc); | 
| Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 3795 | desc->mPolicyMix = policyMix->getMix(); | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3796 |  | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 3797 | } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && | 
|  | 3798 | hasPrimaryOutput()) { | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 3799 | // no duplicated output for direct outputs and | 
|  | 3800 | // outputs used by dynamic policy mixes | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3801 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3802 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3803 | // set initial stream volume for device | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3804 | applyStreamVolumes(desc, device, 0, true); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3805 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3806 | //TODO: configure audio effect output stage here | 
|  | 3807 |  | 
|  | 3808 | // 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] | 3809 | duplicatedOutput = | 
|  | 3810 | mpClientInterface->openDuplicateOutput(output, | 
|  | 3811 | mPrimaryOutput->mIoHandle); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3812 | if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3813 | // add duplicated output descriptor | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3814 | sp<SwAudioOutputDescriptor> dupOutputDesc = | 
|  | 3815 | new SwAudioOutputDescriptor(NULL, mpClientInterface); | 
|  | 3816 | dupOutputDesc->mOutput1 = mPrimaryOutput; | 
|  | 3817 | dupOutputDesc->mOutput2 = desc; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3818 | dupOutputDesc->mSamplingRate = desc->mSamplingRate; | 
|  | 3819 | dupOutputDesc->mFormat = desc->mFormat; | 
|  | 3820 | dupOutputDesc->mChannelMask = desc->mChannelMask; | 
|  | 3821 | dupOutputDesc->mLatency = desc->mLatency; | 
|  | 3822 | addOutput(duplicatedOutput, dupOutputDesc); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3823 | applyStreamVolumes(dupOutputDesc, device, 0, true); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3824 | } else { | 
|  | 3825 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3826 | mPrimaryOutput->mIoHandle, output); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3827 | mpClientInterface->closeOutput(output); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3828 | removeOutput(output); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3829 | nextAudioPortGeneration(); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3830 | output = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3831 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3832 | } | 
|  | 3833 | } | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3834 | } else { | 
|  | 3835 | output = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3836 | } | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3837 | if (output == AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3838 | ALOGW("checkOutputsForDevice() could not open output for device %x", device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3839 | profiles.removeAt(profile_index); | 
|  | 3840 | profile_index--; | 
|  | 3841 | } else { | 
|  | 3842 | outputs.add(output); | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3843 | // Load digital format info only for digital devices | 
|  | 3844 | if (audio_device_is_digital(device)) { | 
|  | 3845 | devDesc->importAudioPort(profile); | 
|  | 3846 | } | 
| Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3847 |  | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3848 | if (device_distinguishes_on_address(device)) { | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3849 | ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)", | 
|  | 3850 | device, address.string()); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3851 | setOutputDevice(desc, device, true/*force*/, 0/*delay*/, | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3852 | NULL/*patch handle*/, address.string()); | 
|  | 3853 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3854 | ALOGV("checkOutputsForDevice(): adding output %d", output); | 
|  | 3855 | } | 
|  | 3856 | } | 
|  | 3857 |  | 
|  | 3858 | if (profiles.isEmpty()) { | 
|  | 3859 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); | 
|  | 3860 | return BAD_VALUE; | 
|  | 3861 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3862 | } else { // Disconnect | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3863 | // check if one opened output is not needed any more after disconnecting one device | 
|  | 3864 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 3865 | desc = mOutputs.valueAt(i); | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3866 | if (!desc->isDuplicated()) { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3867 | // exact match on device | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3868 | if (device_distinguishes_on_address(device) && | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3869 | (desc->supportedDevices() == device)) { | 
| keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3870 | findIoHandlesByAddress(desc, device, address, outputs); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3871 | } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) { | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3872 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", | 
|  | 3873 | mOutputs.keyAt(i)); | 
|  | 3874 | outputs.add(mOutputs.keyAt(i)); | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3875 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3876 | } | 
|  | 3877 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3878 | // Clear any profiles associated with the disconnected device. | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3879 | for (size_t i = 0; i < mHwModules.size(); i++) | 
|  | 3880 | { | 
|  | 3881 | if (mHwModules[i]->mHandle == 0) { | 
|  | 3882 | continue; | 
|  | 3883 | } | 
|  | 3884 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) | 
|  | 3885 | { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3886 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3887 | if (profile->supportDevice(device)) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3888 | ALOGV("checkOutputsForDevice(): " | 
|  | 3889 | "clearing direct output profile %zu on module %zu", j, i); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3890 | profile->clearAudioProfiles(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3891 | } | 
|  | 3892 | } | 
|  | 3893 | } | 
|  | 3894 | } | 
|  | 3895 | return NO_ERROR; | 
|  | 3896 | } | 
|  | 3897 |  | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3898 | status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor> devDesc, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3899 | audio_policy_dev_state_t state, | 
|  | 3900 | SortedVector<audio_io_handle_t>& inputs, | 
|  | 3901 | const String8 address) | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3902 | { | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3903 | audio_devices_t device = devDesc->type(); | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3904 | sp<AudioInputDescriptor> desc; | 
| Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 3905 |  | 
|  | 3906 | if (audio_device_is_digital(device)) { | 
|  | 3907 | // erase all current sample rates, formats and channel masks | 
| Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 3908 | devDesc->clearAudioProfiles(); | 
| Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 3909 | } | 
|  | 3910 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3911 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { | 
|  | 3912 | // first list already open inputs that can be routed to this device | 
|  | 3913 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { | 
|  | 3914 | desc = mInputs.valueAt(input_index); | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3915 | if (desc->mProfile->supportDevice(device)) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3916 | ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index)); | 
|  | 3917 | inputs.add(mInputs.keyAt(input_index)); | 
|  | 3918 | } | 
|  | 3919 | } | 
|  | 3920 |  | 
|  | 3921 | // then look for input profiles that can be routed to this device | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3922 | SortedVector< sp<IOProfile> > profiles; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3923 | for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++) | 
|  | 3924 | { | 
|  | 3925 | if (mHwModules[module_idx]->mHandle == 0) { | 
|  | 3926 | continue; | 
|  | 3927 | } | 
|  | 3928 | for (size_t profile_index = 0; | 
|  | 3929 | profile_index < mHwModules[module_idx]->mInputProfiles.size(); | 
|  | 3930 | profile_index++) | 
|  | 3931 | { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3932 | sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index]; | 
|  | 3933 |  | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3934 | if (profile->supportDevice(device)) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3935 | if (!device_distinguishes_on_address(device) || | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3936 | profile->supportDeviceAddress(address)) { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3937 | profiles.add(profile); | 
|  | 3938 | ALOGV("checkInputsForDevice(): adding profile %zu from module %zu", | 
|  | 3939 | profile_index, module_idx); | 
|  | 3940 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3941 | } | 
|  | 3942 | } | 
|  | 3943 | } | 
|  | 3944 |  | 
|  | 3945 | if (profiles.isEmpty() && inputs.isEmpty()) { | 
|  | 3946 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); | 
|  | 3947 | return BAD_VALUE; | 
|  | 3948 | } | 
|  | 3949 |  | 
|  | 3950 | // open inputs for matching profiles if needed. Direct inputs are also opened to | 
|  | 3951 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() | 
|  | 3952 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { | 
|  | 3953 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3954 | sp<IOProfile> profile = profiles[profile_index]; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3955 | // nothing to do if one input is already opened for this profile | 
|  | 3956 | size_t input_index; | 
|  | 3957 | for (input_index = 0; input_index < mInputs.size(); input_index++) { | 
|  | 3958 | desc = mInputs.valueAt(input_index); | 
|  | 3959 | if (desc->mProfile == profile) { | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3960 | if (audio_device_is_digital(device)) { | 
|  | 3961 | devDesc->importAudioPort(profile); | 
|  | 3962 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3963 | break; | 
|  | 3964 | } | 
|  | 3965 | } | 
|  | 3966 | if (input_index != mInputs.size()) { | 
|  | 3967 | continue; | 
|  | 3968 | } | 
|  | 3969 |  | 
|  | 3970 | ALOGV("opening input for device 0x%X with params %s", device, address.string()); | 
|  | 3971 | desc = new AudioInputDescriptor(profile); | 
|  | 3972 | desc->mDevice = device; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3973 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
|  | 3974 | config.sample_rate = desc->mSamplingRate; | 
|  | 3975 | config.channel_mask = desc->mChannelMask; | 
|  | 3976 | config.format = desc->mFormat; | 
|  | 3977 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3978 | status_t status = mpClientInterface->openInput(profile->getModuleHandle(), | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3979 | &input, | 
|  | 3980 | &config, | 
|  | 3981 | &desc->mDevice, | 
|  | 3982 | address, | 
|  | 3983 | AUDIO_SOURCE_MIC, | 
|  | 3984 | AUDIO_INPUT_FLAG_NONE /*FIXME*/); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3985 |  | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3986 | if (status == NO_ERROR) { | 
|  | 3987 | desc->mSamplingRate = config.sample_rate; | 
|  | 3988 | desc->mChannelMask = config.channel_mask; | 
|  | 3989 | desc->mFormat = config.format; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3990 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3991 | if (!address.isEmpty()) { | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3992 | char *param = audio_device_address_to_parameter(device, address); | 
|  | 3993 | mpClientInterface->setParameters(input, String8(param)); | 
|  | 3994 | free(param); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3995 | } | 
| Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 3996 | updateAudioProfiles(device, input, profile->getAudioProfiles()); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3997 | if (!profile->hasValidAudioProfile()) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3998 | ALOGW("checkInputsForDevice() direct input missing param"); | 
|  | 3999 | mpClientInterface->closeInput(input); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4000 | input = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4001 | } | 
|  | 4002 |  | 
|  | 4003 | if (input != 0) { | 
|  | 4004 | addInput(input, desc); | 
|  | 4005 | } | 
|  | 4006 | } // endif input != 0 | 
|  | 4007 |  | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4008 | if (input == AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4009 | ALOGW("checkInputsForDevice() could not open input for device 0x%X", device); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4010 | profiles.removeAt(profile_index); | 
|  | 4011 | profile_index--; | 
|  | 4012 | } else { | 
|  | 4013 | inputs.add(input); | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4014 | if (audio_device_is_digital(device)) { | 
|  | 4015 | devDesc->importAudioPort(profile); | 
|  | 4016 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4017 | ALOGV("checkInputsForDevice(): adding input %d", input); | 
|  | 4018 | } | 
|  | 4019 | } // end scan profiles | 
|  | 4020 |  | 
|  | 4021 | if (profiles.isEmpty()) { | 
|  | 4022 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); | 
|  | 4023 | return BAD_VALUE; | 
|  | 4024 | } | 
|  | 4025 | } else { | 
|  | 4026 | // Disconnect | 
|  | 4027 | // check if one opened input is not needed any more after disconnecting one device | 
|  | 4028 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { | 
|  | 4029 | desc = mInputs.valueAt(input_index); | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4030 | if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4031 | ALOGV("checkInputsForDevice(): disconnecting adding input %d", | 
|  | 4032 | mInputs.keyAt(input_index)); | 
|  | 4033 | inputs.add(mInputs.keyAt(input_index)); | 
|  | 4034 | } | 
|  | 4035 | } | 
|  | 4036 | // Clear any profiles associated with the disconnected device. | 
|  | 4037 | for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) { | 
|  | 4038 | if (mHwModules[module_index]->mHandle == 0) { | 
|  | 4039 | continue; | 
|  | 4040 | } | 
|  | 4041 | for (size_t profile_index = 0; | 
|  | 4042 | profile_index < mHwModules[module_index]->mInputProfiles.size(); | 
|  | 4043 | profile_index++) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4044 | sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index]; | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4045 | if (profile->supportDevice(device)) { | 
| Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 4046 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu", | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4047 | profile_index, module_index); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4048 | profile->clearAudioProfiles(); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4049 | } | 
|  | 4050 | } | 
|  | 4051 | } | 
|  | 4052 | } // end disconnect | 
|  | 4053 |  | 
|  | 4054 | return NO_ERROR; | 
|  | 4055 | } | 
|  | 4056 |  | 
|  | 4057 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4058 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4059 | { | 
|  | 4060 | ALOGV("closeOutput(%d)", output); | 
|  | 4061 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4062 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4063 | if (outputDesc == NULL) { | 
|  | 4064 | ALOGW("closeOutput() unknown output %d", output); | 
|  | 4065 | return; | 
|  | 4066 | } | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4067 | mPolicyMixes.closeOutput(outputDesc); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4068 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4069 | // look for duplicated outputs connected to the output being removed. | 
|  | 4070 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4071 | sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4072 | if (dupOutputDesc->isDuplicated() && | 
|  | 4073 | (dupOutputDesc->mOutput1 == outputDesc || | 
|  | 4074 | dupOutputDesc->mOutput2 == outputDesc)) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4075 | sp<AudioOutputDescriptor> outputDesc2; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4076 | if (dupOutputDesc->mOutput1 == outputDesc) { | 
|  | 4077 | outputDesc2 = dupOutputDesc->mOutput2; | 
|  | 4078 | } else { | 
|  | 4079 | outputDesc2 = dupOutputDesc->mOutput1; | 
|  | 4080 | } | 
|  | 4081 | // As all active tracks on duplicated output will be deleted, | 
|  | 4082 | // and as they were also referenced on the other output, the reference | 
|  | 4083 | // count for their stream type must be adjusted accordingly on | 
|  | 4084 | // the other output. | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4085 | for (int j = 0; j < AUDIO_STREAM_CNT; j++) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4086 | int refCount = dupOutputDesc->mRefCount[j]; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4087 | outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4088 | } | 
|  | 4089 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); | 
|  | 4090 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); | 
|  | 4091 |  | 
|  | 4092 | mpClientInterface->closeOutput(duplicatedOutput); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4093 | removeOutput(duplicatedOutput); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4094 | } | 
|  | 4095 | } | 
|  | 4096 |  | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4097 | nextAudioPortGeneration(); | 
|  | 4098 |  | 
| Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4099 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4100 | if (index >= 0) { | 
|  | 4101 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
| Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4102 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4103 | mAudioPatches.removeItemsAt(index); | 
|  | 4104 | mpClientInterface->onAudioPatchListUpdate(); | 
|  | 4105 | } | 
|  | 4106 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4107 | AudioParameter param; | 
|  | 4108 | param.add(String8("closing"), String8("true")); | 
|  | 4109 | mpClientInterface->setParameters(output, param.toString()); | 
|  | 4110 |  | 
|  | 4111 | mpClientInterface->closeOutput(output); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4112 | removeOutput(output); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4113 | mPreviousOutputs = mOutputs; | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4114 | } | 
|  | 4115 |  | 
|  | 4116 | void AudioPolicyManager::closeInput(audio_io_handle_t input) | 
|  | 4117 | { | 
|  | 4118 | ALOGV("closeInput(%d)", input); | 
|  | 4119 |  | 
|  | 4120 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); | 
|  | 4121 | if (inputDesc == NULL) { | 
|  | 4122 | ALOGW("closeInput() unknown input %d", input); | 
|  | 4123 | return; | 
|  | 4124 | } | 
|  | 4125 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4126 | nextAudioPortGeneration(); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4127 |  | 
| Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4128 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4129 | if (index >= 0) { | 
|  | 4130 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
| Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4131 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4132 | mAudioPatches.removeItemsAt(index); | 
|  | 4133 | mpClientInterface->onAudioPatchListUpdate(); | 
|  | 4134 | } | 
|  | 4135 |  | 
|  | 4136 | mpClientInterface->closeInput(input); | 
|  | 4137 | mInputs.removeItem(input); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4138 | } | 
|  | 4139 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4140 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice( | 
|  | 4141 | audio_devices_t device, | 
|  | 4142 | SwAudioOutputCollection openOutputs) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4143 | { | 
|  | 4144 | SortedVector<audio_io_handle_t> outputs; | 
|  | 4145 |  | 
|  | 4146 | ALOGVV("getOutputsForDevice() device %04x", device); | 
|  | 4147 | for (size_t i = 0; i < openOutputs.size(); i++) { | 
|  | 4148 | ALOGVV("output %d isDuplicated=%d device=%04x", | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4149 | i, openOutputs.valueAt(i)->isDuplicated(), | 
|  | 4150 | openOutputs.valueAt(i)->supportedDevices()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4151 | if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) { | 
|  | 4152 | ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i)); | 
|  | 4153 | outputs.add(openOutputs.keyAt(i)); | 
|  | 4154 | } | 
|  | 4155 | } | 
|  | 4156 | return outputs; | 
|  | 4157 | } | 
|  | 4158 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4159 | bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4160 | SortedVector<audio_io_handle_t>& outputs2) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4161 | { | 
|  | 4162 | if (outputs1.size() != outputs2.size()) { | 
|  | 4163 | return false; | 
|  | 4164 | } | 
|  | 4165 | for (size_t i = 0; i < outputs1.size(); i++) { | 
|  | 4166 | if (outputs1[i] != outputs2[i]) { | 
|  | 4167 | return false; | 
|  | 4168 | } | 
|  | 4169 | } | 
|  | 4170 | return true; | 
|  | 4171 | } | 
|  | 4172 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4173 | void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4174 | { | 
|  | 4175 | audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/); | 
|  | 4176 | audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/); | 
|  | 4177 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs); | 
|  | 4178 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs); | 
|  | 4179 |  | 
| Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4180 | // also take into account external policy-related changes: add all outputs which are | 
|  | 4181 | // associated with policies in the "before" and "after" output vectors | 
|  | 4182 | ALOGVV("checkOutputForStrategy(): policy related outputs"); | 
|  | 4183 | for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4184 | const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i); | 
| Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4185 | if (desc != 0 && desc->mPolicyMix != NULL) { | 
|  | 4186 | srcOutputs.add(desc->mIoHandle); | 
|  | 4187 | ALOGVV(" previous outputs: adding %d", desc->mIoHandle); | 
|  | 4188 | } | 
|  | 4189 | } | 
|  | 4190 | for (size_t i = 0 ; i < mOutputs.size() ; i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4191 | const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4192 | if (desc != 0 && desc->mPolicyMix != NULL) { | 
|  | 4193 | dstOutputs.add(desc->mIoHandle); | 
|  | 4194 | ALOGVV(" new outputs: adding %d", desc->mIoHandle); | 
|  | 4195 | } | 
|  | 4196 | } | 
|  | 4197 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4198 | if (!vectorsEqual(srcOutputs,dstOutputs)) { | 
|  | 4199 | ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d", | 
|  | 4200 | strategy, srcOutputs[0], dstOutputs[0]); | 
|  | 4201 | // mute strategy while moving tracks from one output to another | 
|  | 4202 | for (size_t i = 0; i < srcOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4203 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4204 | if (isStrategyActive(desc, strategy)) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4205 | setStrategyMute(strategy, true, desc); | 
|  | 4206 | setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4207 | } | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4208 | sp<AudioSourceDescriptor> source = | 
|  | 4209 | getSourceForStrategyOnOutput(srcOutputs[i], strategy); | 
|  | 4210 | if (source != 0){ | 
|  | 4211 | connectAudioSource(source); | 
|  | 4212 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4213 | } | 
|  | 4214 |  | 
|  | 4215 | // Move effects associated to this strategy from previous output to new output | 
|  | 4216 | if (strategy == STRATEGY_MEDIA) { | 
|  | 4217 | audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs); | 
|  | 4218 | SortedVector<audio_io_handle_t> moved; | 
|  | 4219 | for (size_t i = 0; i < mEffects.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4220 | sp<EffectDescriptor> effectDesc = mEffects.valueAt(i); | 
|  | 4221 | if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX && | 
|  | 4222 | effectDesc->mIo != fxOutput) { | 
|  | 4223 | if (moved.indexOf(effectDesc->mIo) < 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4224 | ALOGV("checkOutputForStrategy() moving effect %d to output %d", | 
|  | 4225 | mEffects.keyAt(i), fxOutput); | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4226 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4227 | fxOutput); | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4228 | moved.add(effectDesc->mIo); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4229 | } | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4230 | effectDesc->mIo = fxOutput; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4231 | } | 
|  | 4232 | } | 
|  | 4233 | } | 
|  | 4234 | // Move tracks associated to this strategy from previous output to new output | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4235 | for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4236 | if (getStrategy((audio_stream_type_t)i) == strategy) { | 
|  | 4237 | mpClientInterface->invalidateStream((audio_stream_type_t)i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4238 | } | 
|  | 4239 | } | 
|  | 4240 | } | 
|  | 4241 | } | 
|  | 4242 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4243 | void AudioPolicyManager::checkOutputForAllStrategies() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4244 | { | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4245 | 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] | 4246 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4247 | checkOutputForStrategy(STRATEGY_PHONE); | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4248 | 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] | 4249 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4250 | checkOutputForStrategy(STRATEGY_SONIFICATION); | 
|  | 4251 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4252 | checkOutputForStrategy(STRATEGY_ACCESSIBILITY); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4253 | checkOutputForStrategy(STRATEGY_MEDIA); | 
|  | 4254 | checkOutputForStrategy(STRATEGY_DTMF); | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4255 | checkOutputForStrategy(STRATEGY_REROUTING); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4256 | } | 
|  | 4257 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4258 | void AudioPolicyManager::checkA2dpSuspend() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4259 | { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4260 | audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4261 | if (a2dpOutput == 0) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4262 | mA2dpSuspended = false; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4263 | return; | 
|  | 4264 | } | 
|  | 4265 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4266 | bool isScoConnected = | 
| Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 4267 | ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET & | 
|  | 4268 | ~AUDIO_DEVICE_BIT_IN) != 0) || | 
|  | 4269 | ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4270 | // suspend A2DP output if: | 
|  | 4271 | //      (NOT already suspended) && | 
|  | 4272 | //      ((SCO device is connected && | 
|  | 4273 | //       (forced usage for communication || for record is SCO))) || | 
|  | 4274 | //      (phone state is ringing || in call) | 
|  | 4275 | // | 
|  | 4276 | // restore A2DP output if: | 
|  | 4277 | //      (Already suspended) && | 
|  | 4278 | //      ((SCO device is NOT connected || | 
|  | 4279 | //       (forced usage NOT for communication && NOT for record is SCO))) && | 
|  | 4280 | //      (phone state is NOT ringing && NOT in call) | 
|  | 4281 | // | 
|  | 4282 | if (mA2dpSuspended) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4283 | if ((!isScoConnected || | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4284 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) && | 
|  | 4285 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) && | 
|  | 4286 | ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) && | 
|  | 4287 | (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4288 |  | 
|  | 4289 | mpClientInterface->restoreOutput(a2dpOutput); | 
|  | 4290 | mA2dpSuspended = false; | 
|  | 4291 | } | 
|  | 4292 | } else { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4293 | if ((isScoConnected && | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4294 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) || | 
|  | 4295 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO))) || | 
|  | 4296 | ((mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) || | 
|  | 4297 | (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4298 |  | 
|  | 4299 | mpClientInterface->suspendOutput(a2dpOutput); | 
|  | 4300 | mA2dpSuspended = true; | 
|  | 4301 | } | 
|  | 4302 | } | 
|  | 4303 | } | 
|  | 4304 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4305 | audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, | 
|  | 4306 | bool fromCache) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4307 | { | 
|  | 4308 | audio_devices_t device = AUDIO_DEVICE_NONE; | 
|  | 4309 |  | 
| Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4310 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4311 | if (index >= 0) { | 
|  | 4312 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
|  | 4313 | if (patchDesc->mUid != mUidCached) { | 
|  | 4314 | ALOGV("getNewOutputDevice() device %08x forced by patch %d", | 
| Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4315 | outputDesc->device(), outputDesc->getPatchHandle()); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4316 | return outputDesc->device(); | 
|  | 4317 | } | 
|  | 4318 | } | 
|  | 4319 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4320 | // 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] | 4321 | // 1: the strategy enforced audible is active and enforced on the output: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4322 | //      use device for strategy enforced audible | 
|  | 4323 | // 2: we are in call or the strategy phone is active on the output: | 
|  | 4324 | //      use device for strategy phone | 
| Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 4325 | // 3: the strategy for enforced audible is active but not enforced on the output: | 
|  | 4326 | //      use the device for strategy enforced audible | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4327 | // 4: the strategy sonification is active on the output: | 
| Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4328 | //      use device for strategy sonification | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4329 | // 5: the strategy accessibility is active on the output: | 
|  | 4330 | //      use device for strategy accessibility | 
| Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4331 | // 6: the strategy "respectful" sonification is active on the output: | 
|  | 4332 | //      use device for strategy "respectful" sonification | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4333 | // 7: the strategy media is active on the output: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4334 | //      use device for strategy media | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4335 | // 8: the strategy DTMF is active on the output: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4336 | //      use device for strategy DTMF | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4337 | // 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] | 4338 | //      use device for strategy t-t-s | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4339 | if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) && | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4340 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4341 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); | 
|  | 4342 | } else if (isInCall() || | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4343 | isStrategyActive(outputDesc, STRATEGY_PHONE)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4344 | device = getDeviceForStrategy(STRATEGY_PHONE, fromCache); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4345 | } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) { | 
| Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 4346 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4347 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4348 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache); | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4349 | } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) { | 
|  | 4350 | device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4351 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4352 | device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4353 | } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4354 | device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4355 | } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4356 | device = getDeviceForStrategy(STRATEGY_DTMF, fromCache); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4357 | } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) { | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4358 | device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4359 | } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) { | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4360 | device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4361 | } | 
|  | 4362 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4363 | ALOGV("getNewOutputDevice() selected device %x", device); | 
|  | 4364 | return device; | 
|  | 4365 | } | 
|  | 4366 |  | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 4367 | audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input) | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4368 | { | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 4369 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4370 |  | 
| Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4371 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4372 | if (index >= 0) { | 
|  | 4373 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
|  | 4374 | if (patchDesc->mUid != mUidCached) { | 
|  | 4375 | ALOGV("getNewInputDevice() device %08x forced by patch %d", | 
| Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4376 | inputDesc->mDevice, inputDesc->getPatchHandle()); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4377 | return inputDesc->mDevice; | 
|  | 4378 | } | 
|  | 4379 | } | 
|  | 4380 |  | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 4381 | audio_devices_t device = getDeviceAndMixForInputSource(inputDesc->inputSource()); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4382 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4383 | return device; | 
|  | 4384 | } | 
|  | 4385 |  | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4386 | bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1, | 
|  | 4387 | audio_stream_type_t stream2) { | 
|  | 4388 | return ((stream1 == stream2) || | 
|  | 4389 | ((stream1 == AUDIO_STREAM_ACCESSIBILITY) && (stream2 == AUDIO_STREAM_MUSIC)) || | 
|  | 4390 | ((stream1 == AUDIO_STREAM_MUSIC) && (stream2 == AUDIO_STREAM_ACCESSIBILITY))); | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4391 | } | 
|  | 4392 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4393 | uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4394 | return (uint32_t)getStrategy(stream); | 
|  | 4395 | } | 
|  | 4396 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4397 | audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4398 | // By checking the range of stream before calling getStrategy, we avoid | 
|  | 4399 | // getStrategy's behavior for invalid streams.  getStrategy would do a ALOGE | 
|  | 4400 | // 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] | 4401 | if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4402 | return AUDIO_DEVICE_NONE; | 
|  | 4403 | } | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4404 | audio_devices_t devices = AUDIO_DEVICE_NONE; | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4405 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { | 
|  | 4406 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4407 | continue; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4408 | } | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4409 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4410 | audio_devices_t curDevices = | 
| Eric Laurent | 447a87b | 2016-07-07 17:32:10 -0700 | [diff] [blame] | 4411 | getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/); | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4412 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(curDevices, mOutputs); | 
|  | 4413 | for (size_t i = 0; i < outputs.size(); i++) { | 
|  | 4414 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4415 | if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) { | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4416 | curDevices |= outputDesc->device(); | 
|  | 4417 | } | 
|  | 4418 | } | 
|  | 4419 | devices |= curDevices; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4420 | } | 
| Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4421 |  | 
|  | 4422 | /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it | 
|  | 4423 | and doesn't really need to.*/ | 
|  | 4424 | if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) { | 
|  | 4425 | devices |= AUDIO_DEVICE_OUT_SPEAKER; | 
|  | 4426 | devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE; | 
|  | 4427 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4428 | return devices; | 
|  | 4429 | } | 
|  | 4430 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4431 | routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const | 
|  | 4432 | { | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4433 | 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] | 4434 | return mEngine->getStrategyForStream(stream); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4435 | } | 
|  | 4436 |  | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4437 | uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) { | 
|  | 4438 | // flags to strategy mapping | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4439 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { | 
|  | 4440 | return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER; | 
|  | 4441 | } | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4442 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { | 
|  | 4443 | return (uint32_t) STRATEGY_ENFORCED_AUDIBLE; | 
|  | 4444 | } | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4445 | // usage to strategy mapping | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4446 | return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage)); | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4447 | } | 
|  | 4448 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4449 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4450 | switch(stream) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4451 | case AUDIO_STREAM_MUSIC: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4452 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); | 
|  | 4453 | updateDevicesAndOutputs(); | 
|  | 4454 | break; | 
|  | 4455 | default: | 
|  | 4456 | break; | 
|  | 4457 | } | 
|  | 4458 | } | 
|  | 4459 |  | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4460 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { | 
| Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 4461 |  | 
|  | 4462 | // skip beacon mute management if a dedicated TTS output is available | 
|  | 4463 | if (mTtsOutputAvailable) { | 
|  | 4464 | return 0; | 
|  | 4465 | } | 
|  | 4466 |  | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4467 | switch(event) { | 
|  | 4468 | case STARTING_OUTPUT: | 
|  | 4469 | mBeaconMuteRefCount++; | 
|  | 4470 | break; | 
|  | 4471 | case STOPPING_OUTPUT: | 
|  | 4472 | if (mBeaconMuteRefCount > 0) { | 
|  | 4473 | mBeaconMuteRefCount--; | 
|  | 4474 | } | 
|  | 4475 | break; | 
|  | 4476 | case STARTING_BEACON: | 
|  | 4477 | mBeaconPlayingRefCount++; | 
|  | 4478 | break; | 
|  | 4479 | case STOPPING_BEACON: | 
|  | 4480 | if (mBeaconPlayingRefCount > 0) { | 
|  | 4481 | mBeaconPlayingRefCount--; | 
|  | 4482 | } | 
|  | 4483 | break; | 
|  | 4484 | } | 
|  | 4485 |  | 
|  | 4486 | if (mBeaconMuteRefCount > 0) { | 
|  | 4487 | // any playback causes beacon to be muted | 
|  | 4488 | return setBeaconMute(true); | 
|  | 4489 | } else { | 
|  | 4490 | // no other playback: unmute when beacon starts playing, mute when it stops | 
|  | 4491 | return setBeaconMute(mBeaconPlayingRefCount == 0); | 
|  | 4492 | } | 
|  | 4493 | } | 
|  | 4494 |  | 
|  | 4495 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { | 
|  | 4496 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", | 
|  | 4497 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); | 
|  | 4498 | // keep track of muted state to avoid repeating mute/unmute operations | 
|  | 4499 | if (mBeaconMuted != mute) { | 
|  | 4500 | // mute/unmute AUDIO_STREAM_TTS on all outputs | 
|  | 4501 | ALOGV("\t muting %d", mute); | 
|  | 4502 | uint32_t maxLatency = 0; | 
|  | 4503 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4504 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4505 | setStreamMute(AUDIO_STREAM_TTS, mute/*on*/, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4506 | desc, | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4507 | 0 /*delay*/, AUDIO_DEVICE_NONE); | 
|  | 4508 | const uint32_t latency = desc->latency() * 2; | 
|  | 4509 | if (latency > maxLatency) { | 
|  | 4510 | maxLatency = latency; | 
|  | 4511 | } | 
|  | 4512 | } | 
|  | 4513 | mBeaconMuted = mute; | 
|  | 4514 | return maxLatency; | 
|  | 4515 | } | 
|  | 4516 | return 0; | 
|  | 4517 | } | 
|  | 4518 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4519 | audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4520 | bool fromCache) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4521 | { | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4522 | // Routing | 
|  | 4523 | // see if we have an explicit route | 
|  | 4524 | // scan the whole RouteMap, for each entry, convert the stream type to a strategy | 
|  | 4525 | // (getStrategy(stream)). | 
|  | 4526 | // if the strategy from the stream type in the RouteMap is the same as the argument above, | 
|  | 4527 | // and activity count is non-zero | 
|  | 4528 | // the device = the device from the descriptor in the RouteMap, and exit. | 
|  | 4529 | for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) { | 
|  | 4530 | sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex); | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4531 | routing_strategy routeStrategy = getStrategy(route->mStreamType); | 
|  | 4532 | if ((routeStrategy == strategy) && route->isActive()) { | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4533 | return route->mDeviceDescriptor->type(); | 
|  | 4534 | } | 
|  | 4535 | } | 
|  | 4536 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4537 | if (fromCache) { | 
|  | 4538 | ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x", | 
|  | 4539 | strategy, mDeviceForStrategy[strategy]); | 
|  | 4540 | return mDeviceForStrategy[strategy]; | 
|  | 4541 | } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4542 | return mEngine->getDeviceForStrategy(strategy); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4543 | } | 
|  | 4544 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4545 | void AudioPolicyManager::updateDevicesAndOutputs() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4546 | { | 
|  | 4547 | for (int i = 0; i < NUM_STRATEGIES; i++) { | 
|  | 4548 | mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); | 
|  | 4549 | } | 
|  | 4550 | mPreviousOutputs = mOutputs; | 
|  | 4551 | } | 
|  | 4552 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4553 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4554 | audio_devices_t prevDevice, | 
|  | 4555 | uint32_t delayMs) | 
|  | 4556 | { | 
|  | 4557 | // mute/unmute strategies using an incompatible device combination | 
|  | 4558 | // if muting, wait for the audio in pcm buffer to be drained before proceeding | 
|  | 4559 | // if unmuting, unmute only after the specified delay | 
|  | 4560 | if (outputDesc->isDuplicated()) { | 
|  | 4561 | return 0; | 
|  | 4562 | } | 
|  | 4563 |  | 
|  | 4564 | uint32_t muteWaitMs = 0; | 
|  | 4565 | audio_devices_t device = outputDesc->device(); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4566 | bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4567 |  | 
|  | 4568 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { | 
|  | 4569 | audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4570 | curDevice = curDevice & outputDesc->supportedDevices(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4571 | bool mute = shouldMute && (curDevice & device) && (curDevice != device); | 
|  | 4572 | bool doMute = false; | 
|  | 4573 |  | 
|  | 4574 | if (mute && !outputDesc->mStrategyMutedByDevice[i]) { | 
|  | 4575 | doMute = true; | 
|  | 4576 | outputDesc->mStrategyMutedByDevice[i] = true; | 
|  | 4577 | } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){ | 
|  | 4578 | doMute = true; | 
|  | 4579 | outputDesc->mStrategyMutedByDevice[i] = false; | 
|  | 4580 | } | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4581 | if (doMute) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4582 | for (size_t j = 0; j < mOutputs.size(); j++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4583 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4584 | // skip output if it does not share any device with current output | 
|  | 4585 | if ((desc->supportedDevices() & outputDesc->supportedDevices()) | 
|  | 4586 | == AUDIO_DEVICE_NONE) { | 
|  | 4587 | continue; | 
|  | 4588 | } | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4589 | ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x)", | 
|  | 4590 | mute ? "muting" : "unmuting", i, curDevice); | 
|  | 4591 | setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4592 | if (isStrategyActive(desc, (routing_strategy)i)) { | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4593 | if (mute) { | 
|  | 4594 | // FIXME: should not need to double latency if volume could be applied | 
|  | 4595 | // immediately by the audioflinger mixer. We must account for the delay | 
|  | 4596 | // between now and the next time the audioflinger thread for this output | 
|  | 4597 | // will process a buffer (which corresponds to one buffer size, | 
|  | 4598 | // usually 1/2 or 1/4 of the latency). | 
|  | 4599 | if (muteWaitMs < desc->latency() * 2) { | 
|  | 4600 | muteWaitMs = desc->latency() * 2; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4601 | } | 
|  | 4602 | } | 
|  | 4603 | } | 
|  | 4604 | } | 
|  | 4605 | } | 
|  | 4606 | } | 
|  | 4607 |  | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4608 | // temporary mute output if device selection changes to avoid volume bursts due to | 
|  | 4609 | // different per device volumes | 
|  | 4610 | if (outputDesc->isActive() && (device != prevDevice)) { | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 4611 | uint32_t tempMuteWaitMs = outputDesc->latency() * 2; | 
|  | 4612 | // temporary mute duration is conservatively set to 4 times the reported latency | 
|  | 4613 | uint32_t tempMuteDurationMs = outputDesc->latency() * 4; | 
|  | 4614 | if (muteWaitMs < tempMuteWaitMs) { | 
|  | 4615 | muteWaitMs = tempMuteWaitMs; | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4616 | } | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 4617 |  | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4618 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4619 | if (isStrategyActive(outputDesc, (routing_strategy)i)) { | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 4620 | // make sure that we do not start the temporary mute period too early in case of | 
|  | 4621 | // delayed device change | 
|  | 4622 | setStrategyMute((routing_strategy)i, true, outputDesc, delayMs); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4623 | setStrategyMute((routing_strategy)i, false, outputDesc, | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 4624 | delayMs + tempMuteDurationMs, device); | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4625 | } | 
|  | 4626 | } | 
|  | 4627 | } | 
|  | 4628 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4629 | // wait for the PCM output buffers to empty before proceeding with the rest of the command | 
|  | 4630 | if (muteWaitMs > delayMs) { | 
|  | 4631 | muteWaitMs -= delayMs; | 
|  | 4632 | usleep(muteWaitMs * 1000); | 
|  | 4633 | return muteWaitMs; | 
|  | 4634 | } | 
|  | 4635 | return 0; | 
|  | 4636 | } | 
|  | 4637 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4638 | uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4639 | audio_devices_t device, | 
|  | 4640 | bool force, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4641 | int delayMs, | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4642 | audio_patch_handle_t *patchHandle, | 
|  | 4643 | const char* address) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4644 | { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4645 | ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4646 | AudioParameter param; | 
|  | 4647 | uint32_t muteWaitMs; | 
|  | 4648 |  | 
|  | 4649 | if (outputDesc->isDuplicated()) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4650 | muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs); | 
|  | 4651 | muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4652 | return muteWaitMs; | 
|  | 4653 | } | 
|  | 4654 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current | 
|  | 4655 | // output profile | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4656 | if ((device != AUDIO_DEVICE_NONE) && | 
|  | 4657 | ((device & outputDesc->supportedDevices()) == 0)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4658 | return 0; | 
|  | 4659 | } | 
|  | 4660 |  | 
|  | 4661 | // filter devices according to output selected | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4662 | device = (audio_devices_t)(device & outputDesc->supportedDevices()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4663 |  | 
|  | 4664 | audio_devices_t prevDevice = outputDesc->mDevice; | 
|  | 4665 |  | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4666 | ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4667 |  | 
|  | 4668 | if (device != AUDIO_DEVICE_NONE) { | 
|  | 4669 | outputDesc->mDevice = device; | 
|  | 4670 | } | 
|  | 4671 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs); | 
|  | 4672 |  | 
|  | 4673 | // Do not change the routing if: | 
| Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 4674 | //      the requested device is AUDIO_DEVICE_NONE | 
|  | 4675 | //      OR the requested device is the same as current device | 
|  | 4676 | //  AND force is not specified | 
|  | 4677 | //  AND the output is connected by a valid audio patch. | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4678 | // Doing this check here allows the caller to call setOutputDevice() without conditions | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4679 | if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && | 
|  | 4680 | !force && | 
| Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4681 | outputDesc->getPatchHandle() != 0) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4682 | ALOGV("setOutputDevice() setting same device 0x%04x or null device", device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4683 | return muteWaitMs; | 
|  | 4684 | } | 
|  | 4685 |  | 
|  | 4686 | ALOGV("setOutputDevice() changing device"); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4687 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4688 | // do the routing | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4689 | if (device == AUDIO_DEVICE_NONE) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4690 | resetOutputDevice(outputDesc, delayMs, NULL); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4691 | } else { | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 4692 | DeviceVector deviceList; | 
|  | 4693 | if ((address == NULL) || (strlen(address) == 0)) { | 
|  | 4694 | deviceList = mAvailableOutputDevices.getDevicesFromType(device); | 
|  | 4695 | } else { | 
|  | 4696 | deviceList = mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address)); | 
|  | 4697 | } | 
|  | 4698 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4699 | if (!deviceList.isEmpty()) { | 
|  | 4700 | struct audio_patch patch; | 
|  | 4701 | outputDesc->toAudioPortConfig(&patch.sources[0]); | 
|  | 4702 | patch.num_sources = 1; | 
|  | 4703 | patch.num_sinks = 0; | 
|  | 4704 | for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) { | 
|  | 4705 | deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4706 | patch.num_sinks++; | 
|  | 4707 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4708 | ssize_t index; | 
|  | 4709 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { | 
|  | 4710 | index = mAudioPatches.indexOfKey(*patchHandle); | 
|  | 4711 | } else { | 
| Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4712 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4713 | } | 
|  | 4714 | sp< AudioPatch> patchDesc; | 
|  | 4715 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; | 
|  | 4716 | if (index >= 0) { | 
|  | 4717 | patchDesc = mAudioPatches.valueAt(index); | 
|  | 4718 | afPatchHandle = patchDesc->mAfPatchHandle; | 
|  | 4719 | } | 
|  | 4720 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4721 | status_t status = mpClientInterface->createAudioPatch(&patch, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4722 | &afPatchHandle, | 
|  | 4723 | delayMs); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4724 | ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d" | 
|  | 4725 | "num_sources %d num_sinks %d", | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4726 | status, afPatchHandle, patch.num_sources, patch.num_sinks); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4727 | if (status == NO_ERROR) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4728 | if (index < 0) { | 
| François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 4729 | patchDesc = new AudioPatch(&patch, mUidCached); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4730 | addAudioPatch(patchDesc->mHandle, patchDesc); | 
|  | 4731 | } else { | 
|  | 4732 | patchDesc->mPatch = patch; | 
|  | 4733 | } | 
|  | 4734 | patchDesc->mAfPatchHandle = afPatchHandle; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4735 | if (patchHandle) { | 
|  | 4736 | *patchHandle = patchDesc->mHandle; | 
|  | 4737 | } | 
| Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4738 | outputDesc->setPatchHandle(patchDesc->mHandle); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4739 | nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4740 | mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4741 | } | 
|  | 4742 | } | 
| bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 4743 |  | 
|  | 4744 | // inform all input as well | 
|  | 4745 | for (size_t i = 0; i < mInputs.size(); i++) { | 
|  | 4746 | const sp<AudioInputDescriptor>  inputDescriptor = mInputs.valueAt(i); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4747 | if (!is_virtual_input_device(inputDescriptor->mDevice)) { | 
| bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 4748 | AudioParameter inputCmd = AudioParameter(); | 
|  | 4749 | ALOGV("%s: inform input %d of device:%d", __func__, | 
|  | 4750 | inputDescriptor->mIoHandle, device); | 
|  | 4751 | inputCmd.addInt(String8(AudioParameter::keyRouting),device); | 
|  | 4752 | mpClientInterface->setParameters(inputDescriptor->mIoHandle, | 
|  | 4753 | inputCmd.toString(), | 
|  | 4754 | delayMs); | 
|  | 4755 | } | 
|  | 4756 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4757 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4758 |  | 
|  | 4759 | // update stream volumes according to new device | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4760 | applyStreamVolumes(outputDesc, device, delayMs); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4761 |  | 
|  | 4762 | return muteWaitMs; | 
|  | 4763 | } | 
|  | 4764 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4765 | status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4766 | int delayMs, | 
|  | 4767 | audio_patch_handle_t *patchHandle) | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4768 | { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4769 | ssize_t index; | 
|  | 4770 | if (patchHandle) { | 
|  | 4771 | index = mAudioPatches.indexOfKey(*patchHandle); | 
|  | 4772 | } else { | 
| Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4773 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4774 | } | 
|  | 4775 | if (index < 0) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4776 | return INVALID_OPERATION; | 
|  | 4777 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4778 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
|  | 4779 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4780 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); | 
| Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 4781 | outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4782 | removeAudioPatch(patchDesc->mHandle); | 
|  | 4783 | nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4784 | mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4785 | return status; | 
|  | 4786 | } | 
|  | 4787 |  | 
|  | 4788 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, | 
|  | 4789 | audio_devices_t device, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4790 | bool force, | 
|  | 4791 | audio_patch_handle_t *patchHandle) | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4792 | { | 
|  | 4793 | status_t status = NO_ERROR; | 
|  | 4794 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4795 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4796 | if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) { | 
|  | 4797 | inputDesc->mDevice = device; | 
|  | 4798 |  | 
|  | 4799 | DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device); | 
|  | 4800 | if (!deviceList.isEmpty()) { | 
|  | 4801 | struct audio_patch patch; | 
|  | 4802 | inputDesc->toAudioPortConfig(&patch.sinks[0]); | 
| Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 4803 | // AUDIO_SOURCE_HOTWORD is for internal use only: | 
|  | 4804 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 4805 | if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD && | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 4806 | !inputDesc->isSoundTrigger()) { | 
| Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 4807 | patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION; | 
|  | 4808 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4809 | patch.num_sinks = 1; | 
|  | 4810 | //only one input device for now | 
|  | 4811 | deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4812 | patch.num_sources = 1; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4813 | ssize_t index; | 
|  | 4814 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { | 
|  | 4815 | index = mAudioPatches.indexOfKey(*patchHandle); | 
|  | 4816 | } else { | 
| Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4817 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4818 | } | 
|  | 4819 | sp< AudioPatch> patchDesc; | 
|  | 4820 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; | 
|  | 4821 | if (index >= 0) { | 
|  | 4822 | patchDesc = mAudioPatches.valueAt(index); | 
|  | 4823 | afPatchHandle = patchDesc->mAfPatchHandle; | 
|  | 4824 | } | 
|  | 4825 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4826 | status_t status = mpClientInterface->createAudioPatch(&patch, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4827 | &afPatchHandle, | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4828 | 0); | 
|  | 4829 | ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d", | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4830 | status, afPatchHandle); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4831 | if (status == NO_ERROR) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4832 | if (index < 0) { | 
| François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 4833 | patchDesc = new AudioPatch(&patch, mUidCached); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4834 | addAudioPatch(patchDesc->mHandle, patchDesc); | 
|  | 4835 | } else { | 
|  | 4836 | patchDesc->mPatch = patch; | 
|  | 4837 | } | 
|  | 4838 | patchDesc->mAfPatchHandle = afPatchHandle; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4839 | if (patchHandle) { | 
|  | 4840 | *patchHandle = patchDesc->mHandle; | 
|  | 4841 | } | 
| Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4842 | inputDesc->setPatchHandle(patchDesc->mHandle); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4843 | nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4844 | mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4845 | } | 
|  | 4846 | } | 
|  | 4847 | } | 
|  | 4848 | return status; | 
|  | 4849 | } | 
|  | 4850 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4851 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, | 
|  | 4852 | audio_patch_handle_t *patchHandle) | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4853 | { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4854 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4855 | ssize_t index; | 
|  | 4856 | if (patchHandle) { | 
|  | 4857 | index = mAudioPatches.indexOfKey(*patchHandle); | 
|  | 4858 | } else { | 
| Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4859 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4860 | } | 
|  | 4861 | if (index < 0) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4862 | return INVALID_OPERATION; | 
|  | 4863 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4864 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
|  | 4865 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4866 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); | 
| Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 4867 | inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4868 | removeAudioPatch(patchDesc->mHandle); | 
|  | 4869 | nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4870 | mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4871 | return status; | 
|  | 4872 | } | 
|  | 4873 |  | 
| Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 4874 | sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4875 | String8 address, | 
|  | 4876 | uint32_t& samplingRate, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4877 | audio_format_t& format, | 
|  | 4878 | audio_channel_mask_t& channelMask, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4879 | audio_input_flags_t flags) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4880 | { | 
|  | 4881 | // Choose an input profile based on the requested capture parameters: select the first available | 
|  | 4882 | // profile supporting all requested parameters. | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4883 | // | 
|  | 4884 | // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return | 
|  | 4885 | // the best matching profile, not the first one. | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4886 |  | 
|  | 4887 | for (size_t i = 0; i < mHwModules.size(); i++) | 
|  | 4888 | { | 
|  | 4889 | if (mHwModules[i]->mHandle == 0) { | 
|  | 4890 | continue; | 
|  | 4891 | } | 
|  | 4892 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) | 
|  | 4893 | { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4894 | sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j]; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4895 | // profile->log(); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4896 | if (profile->isCompatibleProfile(device, address, samplingRate, | 
| Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 4897 | &samplingRate /*updatedSamplingRate*/, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4898 | format, | 
|  | 4899 | &format /*updatedFormat*/, | 
|  | 4900 | channelMask, | 
|  | 4901 | &channelMask /*updatedChannelMask*/, | 
|  | 4902 | (audio_output_flags_t) flags)) { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4903 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4904 | return profile; | 
|  | 4905 | } | 
|  | 4906 | } | 
|  | 4907 | } | 
|  | 4908 | return NULL; | 
|  | 4909 | } | 
|  | 4910 |  | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 4911 |  | 
|  | 4912 | audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4913 | AudioMix **policyMix) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4914 | { | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4915 | audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; | 
|  | 4916 | audio_devices_t selectedDeviceFromMix = | 
|  | 4917 | mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4918 |  | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4919 | if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) { | 
|  | 4920 | return selectedDeviceFromMix; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4921 | } | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 4922 | return getDeviceForInputSource(inputSource); | 
|  | 4923 | } | 
|  | 4924 |  | 
|  | 4925 | audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource) | 
|  | 4926 | { | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 4927 | for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) { | 
|  | 4928 | sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex); | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4929 | if (inputSource == route->mSource && route->isActive()) { | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 4930 | return route->mDeviceDescriptor->type(); | 
|  | 4931 | } | 
|  | 4932 | } | 
|  | 4933 |  | 
|  | 4934 | return mEngine->getDeviceForInputSource(inputSource); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4935 | } | 
|  | 4936 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4937 | float AudioPolicyManager::computeVolume(audio_stream_type_t stream, | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4938 | int index, | 
|  | 4939 | audio_devices_t device) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4940 | { | 
| Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 4941 | float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index); | 
| Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame^] | 4942 |  | 
|  | 4943 | // handle the case of accessibility active while a ringtone is playing: if the ringtone is much | 
|  | 4944 | // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch | 
|  | 4945 | // exploration of the dialer UI. In this situation, bring the accessibility volume closer to | 
|  | 4946 | // the ringtone volume | 
|  | 4947 | if ((stream == AUDIO_STREAM_ACCESSIBILITY) | 
|  | 4948 | && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState()) | 
|  | 4949 | && isStreamActive(AUDIO_STREAM_RING, 0)) { | 
|  | 4950 | const float ringVolumeDB = computeVolume(AUDIO_STREAM_RING, index, device); | 
|  | 4951 | return ringVolumeDB - 4 > volumeDB ? ringVolumeDB - 4 : volumeDB; | 
|  | 4952 | } | 
|  | 4953 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4954 | // if a headset is connected, apply the following rules to ring tones and notifications | 
|  | 4955 | // to avoid sound level bursts in user's ears: | 
| Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 4956 | // - always attenuate notifications volume by 6dB | 
|  | 4957 | // - attenuate ring tones volume by 6dB unless music is not playing and | 
|  | 4958 | // speaker is part of the select devices | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4959 | // - if music is playing, always limit the volume to current music volume, | 
|  | 4960 | // with a minimum threshold at -36dB so that notification is always perceived. | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4961 | const routing_strategy stream_strategy = getStrategy(stream); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4962 | if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | | 
|  | 4963 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | | 
|  | 4964 | AUDIO_DEVICE_OUT_WIRED_HEADSET | | 
|  | 4965 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) && | 
|  | 4966 | ((stream_strategy == STRATEGY_SONIFICATION) | 
|  | 4967 | || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL) | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4968 | || (stream == AUDIO_STREAM_SYSTEM) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4969 | || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) && | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4970 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) && | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4971 | mVolumeCurves->canBeMuted(stream)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4972 | // when the phone is ringing we must consider that music could have been paused just before | 
|  | 4973 | // by the music application and behave as if music was active if the last music track was | 
|  | 4974 | // just stopped | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4975 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4976 | mLimitRingtoneVolume) { | 
| Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 4977 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4978 | audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/); | 
| Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4979 | float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC, | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4980 | mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC, | 
|  | 4981 | musicDevice), | 
|  | 4982 | musicDevice); | 
| Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4983 | float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ? | 
|  | 4984 | musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB; | 
| Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 4985 | if (volumeDB > minVolDB) { | 
|  | 4986 | volumeDB = minVolDB; | 
| Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4987 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4988 | } | 
| Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 4989 | if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | | 
|  | 4990 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) { | 
|  | 4991 | // on A2DP, also ensure notification volume is not too low compared to media when | 
|  | 4992 | // intended to be played | 
|  | 4993 | if ((volumeDB > -96.0f) && | 
|  | 4994 | (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) { | 
|  | 4995 | ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f", | 
|  | 4996 | stream, device, | 
|  | 4997 | volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB); | 
|  | 4998 | volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB; | 
|  | 4999 | } | 
|  | 5000 | } | 
| Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5001 | } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) || | 
|  | 5002 | stream_strategy != STRATEGY_SONIFICATION) { | 
| Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5003 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5004 | } | 
|  | 5005 | } | 
|  | 5006 |  | 
| Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5007 | return volumeDB; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5008 | } | 
|  | 5009 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5010 | status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5011 | int index, | 
|  | 5012 | const sp<AudioOutputDescriptor>& outputDesc, | 
|  | 5013 | audio_devices_t device, | 
|  | 5014 | int delayMs, | 
|  | 5015 | bool force) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5016 | { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5017 | // do not change actual stream volume if the stream is muted | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5018 | if (outputDesc->mMuteCount[stream] != 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5019 | ALOGVV("checkAndSetVolume() stream %d muted count %d", | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5020 | stream, outputDesc->mMuteCount[stream]); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5021 | return NO_ERROR; | 
|  | 5022 | } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5023 | audio_policy_forced_cfg_t forceUseForComm = | 
|  | 5024 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5025 | // 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] | 5026 | if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) || | 
|  | 5027 | (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5028 | 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] | 5029 | stream, forceUseForComm); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5030 | return INVALID_OPERATION; | 
|  | 5031 | } | 
|  | 5032 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5033 | if (device == AUDIO_DEVICE_NONE) { | 
|  | 5034 | device = outputDesc->device(); | 
|  | 5035 | } | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5036 |  | 
| Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5037 | float volumeDb = computeVolume(stream, index, device); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5038 | if (outputDesc->isFixedVolume(device)) { | 
| Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5039 | volumeDb = 0.0f; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5040 | } | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5041 |  | 
| Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5042 | outputDesc->setVolume(volumeDb, stream, device, delayMs, force); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5043 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5044 | if (stream == AUDIO_STREAM_VOICE_CALL || | 
|  | 5045 | stream == AUDIO_STREAM_BLUETOOTH_SCO) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5046 | float voiceVolume; | 
|  | 5047 | // 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] | 5048 | if (stream == AUDIO_STREAM_VOICE_CALL) { | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5049 | voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5050 | } else { | 
|  | 5051 | voiceVolume = 1.0; | 
|  | 5052 | } | 
|  | 5053 |  | 
| Eric Laurent | 18fba84 | 2016-03-31 14:41:26 -0700 | [diff] [blame] | 5054 | if (voiceVolume != mLastVoiceVolume) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5055 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); | 
|  | 5056 | mLastVoiceVolume = voiceVolume; | 
|  | 5057 | } | 
|  | 5058 | } | 
|  | 5059 |  | 
|  | 5060 | return NO_ERROR; | 
|  | 5061 | } | 
|  | 5062 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5063 | void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc, | 
|  | 5064 | audio_devices_t device, | 
|  | 5065 | int delayMs, | 
|  | 5066 | bool force) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5067 | { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5068 | ALOGVV("applyStreamVolumes() for device %08x", device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5069 |  | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5070 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5071 | checkAndSetVolume((audio_stream_type_t)stream, | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5072 | mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device), | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5073 | outputDesc, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5074 | device, | 
|  | 5075 | delayMs, | 
|  | 5076 | force); | 
|  | 5077 | } | 
|  | 5078 | } | 
|  | 5079 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5080 | void AudioPolicyManager::setStrategyMute(routing_strategy strategy, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5081 | bool on, | 
|  | 5082 | const sp<AudioOutputDescriptor>& outputDesc, | 
|  | 5083 | int delayMs, | 
|  | 5084 | audio_devices_t device) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5085 | { | 
| Eric Laurent | 72249d5 | 2015-06-08 11:12:15 -0700 | [diff] [blame] | 5086 | ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d", | 
|  | 5087 | strategy, on, outputDesc->getId()); | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5088 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5089 | if (getStrategy((audio_stream_type_t)stream) == strategy) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5090 | setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5091 | } | 
|  | 5092 | } | 
|  | 5093 | } | 
|  | 5094 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5095 | void AudioPolicyManager::setStreamMute(audio_stream_type_t stream, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5096 | bool on, | 
|  | 5097 | const sp<AudioOutputDescriptor>& outputDesc, | 
|  | 5098 | int delayMs, | 
|  | 5099 | audio_devices_t device) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5100 | { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5101 | if (device == AUDIO_DEVICE_NONE) { | 
|  | 5102 | device = outputDesc->device(); | 
|  | 5103 | } | 
|  | 5104 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5105 | ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x", | 
|  | 5106 | stream, on, outputDesc->mMuteCount[stream], device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5107 |  | 
|  | 5108 | if (on) { | 
|  | 5109 | if (outputDesc->mMuteCount[stream] == 0) { | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5110 | if (mVolumeCurves->canBeMuted(stream) && | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5111 | ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) || | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5112 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5113 | checkAndSetVolume(stream, 0, outputDesc, device, delayMs); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5114 | } | 
|  | 5115 | } | 
|  | 5116 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored | 
|  | 5117 | outputDesc->mMuteCount[stream]++; | 
|  | 5118 | } else { | 
|  | 5119 | if (outputDesc->mMuteCount[stream] == 0) { | 
|  | 5120 | ALOGV("setStreamMute() unmuting non muted stream!"); | 
|  | 5121 | return; | 
|  | 5122 | } | 
|  | 5123 | if (--outputDesc->mMuteCount[stream] == 0) { | 
|  | 5124 | checkAndSetVolume(stream, | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5125 | mVolumeCurves->getVolumeIndex(stream, device), | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5126 | outputDesc, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5127 | device, | 
|  | 5128 | delayMs); | 
|  | 5129 | } | 
|  | 5130 | } | 
|  | 5131 | } | 
|  | 5132 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5133 | void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5134 | bool starting, bool stateChange) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5135 | { | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 5136 | if(!hasPrimaryOutput()) { | 
|  | 5137 | return; | 
|  | 5138 | } | 
|  | 5139 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5140 | // if the stream pertains to sonification strategy and we are in call we must | 
|  | 5141 | // mute the stream if it is low visibility. If it is high visibility, we must play a tone | 
|  | 5142 | // in the device used for phone strategy and play the tone if the selected device does not | 
|  | 5143 | // interfere with the device used for phone strategy | 
|  | 5144 | // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as | 
|  | 5145 | // many times as there are active tracks on the output | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5146 | const routing_strategy stream_strategy = getStrategy(stream); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5147 | if ((stream_strategy == STRATEGY_SONIFICATION) || | 
|  | 5148 | ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5149 | sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5150 | ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d", | 
|  | 5151 | stream, starting, outputDesc->mDevice, stateChange); | 
|  | 5152 | if (outputDesc->mRefCount[stream]) { | 
|  | 5153 | int muteCount = 1; | 
|  | 5154 | if (stateChange) { | 
|  | 5155 | muteCount = outputDesc->mRefCount[stream]; | 
|  | 5156 | } | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5157 | if (audio_is_low_visibility(stream)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5158 | ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount); | 
|  | 5159 | for (int i = 0; i < muteCount; i++) { | 
|  | 5160 | setStreamMute(stream, starting, mPrimaryOutput); | 
|  | 5161 | } | 
|  | 5162 | } else { | 
|  | 5163 | ALOGV("handleIncallSonification() high visibility"); | 
|  | 5164 | if (outputDesc->device() & | 
|  | 5165 | getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) { | 
|  | 5166 | ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount); | 
|  | 5167 | for (int i = 0; i < muteCount; i++) { | 
|  | 5168 | setStreamMute(stream, starting, mPrimaryOutput); | 
|  | 5169 | } | 
|  | 5170 | } | 
|  | 5171 | if (starting) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5172 | mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION, | 
|  | 5173 | AUDIO_STREAM_VOICE_CALL); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5174 | } else { | 
|  | 5175 | mpClientInterface->stopTone(); | 
|  | 5176 | } | 
|  | 5177 | } | 
|  | 5178 | } | 
|  | 5179 | } | 
|  | 5180 | } | 
|  | 5181 |  | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5182 | audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr) | 
|  | 5183 | { | 
|  | 5184 | // flags to stream type mapping | 
|  | 5185 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { | 
|  | 5186 | return AUDIO_STREAM_ENFORCED_AUDIBLE; | 
|  | 5187 | } | 
|  | 5188 | if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) { | 
|  | 5189 | return AUDIO_STREAM_BLUETOOTH_SCO; | 
|  | 5190 | } | 
| Jean-Michel Trivi | 79ad438 | 2015-01-29 10:49:39 -0800 | [diff] [blame] | 5191 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { | 
|  | 5192 | return AUDIO_STREAM_TTS; | 
|  | 5193 | } | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5194 |  | 
|  | 5195 | // usage to stream type mapping | 
|  | 5196 | switch (attr->usage) { | 
|  | 5197 | case AUDIO_USAGE_MEDIA: | 
|  | 5198 | case AUDIO_USAGE_GAME: | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5199 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: | 
|  | 5200 | return AUDIO_STREAM_MUSIC; | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5201 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: | 
|  | 5202 | return AUDIO_STREAM_ACCESSIBILITY; | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5203 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: | 
|  | 5204 | return AUDIO_STREAM_SYSTEM; | 
|  | 5205 | case AUDIO_USAGE_VOICE_COMMUNICATION: | 
|  | 5206 | return AUDIO_STREAM_VOICE_CALL; | 
|  | 5207 |  | 
|  | 5208 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: | 
|  | 5209 | return AUDIO_STREAM_DTMF; | 
|  | 5210 |  | 
|  | 5211 | case AUDIO_USAGE_ALARM: | 
|  | 5212 | return AUDIO_STREAM_ALARM; | 
|  | 5213 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: | 
|  | 5214 | return AUDIO_STREAM_RING; | 
|  | 5215 |  | 
|  | 5216 | case AUDIO_USAGE_NOTIFICATION: | 
|  | 5217 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: | 
|  | 5218 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: | 
|  | 5219 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: | 
|  | 5220 | case AUDIO_USAGE_NOTIFICATION_EVENT: | 
|  | 5221 | return AUDIO_STREAM_NOTIFICATION; | 
|  | 5222 |  | 
|  | 5223 | case AUDIO_USAGE_UNKNOWN: | 
|  | 5224 | default: | 
|  | 5225 | return AUDIO_STREAM_MUSIC; | 
|  | 5226 | } | 
|  | 5227 | } | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5228 |  | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5229 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) | 
|  | 5230 | { | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5231 | // has flags that map to a strategy? | 
|  | 5232 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { | 
|  | 5233 | return true; | 
|  | 5234 | } | 
|  | 5235 |  | 
|  | 5236 | // has known usage? | 
|  | 5237 | switch (paa->usage) { | 
|  | 5238 | case AUDIO_USAGE_UNKNOWN: | 
|  | 5239 | case AUDIO_USAGE_MEDIA: | 
|  | 5240 | case AUDIO_USAGE_VOICE_COMMUNICATION: | 
|  | 5241 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: | 
|  | 5242 | case AUDIO_USAGE_ALARM: | 
|  | 5243 | case AUDIO_USAGE_NOTIFICATION: | 
|  | 5244 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: | 
|  | 5245 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: | 
|  | 5246 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: | 
|  | 5247 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: | 
|  | 5248 | case AUDIO_USAGE_NOTIFICATION_EVENT: | 
|  | 5249 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: | 
|  | 5250 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: | 
|  | 5251 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: | 
|  | 5252 | case AUDIO_USAGE_GAME: | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5253 | case AUDIO_USAGE_VIRTUAL_SOURCE: | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5254 | break; | 
|  | 5255 | default: | 
|  | 5256 | return false; | 
|  | 5257 | } | 
|  | 5258 | return true; | 
|  | 5259 | } | 
|  | 5260 |  | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5261 | bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc, | 
|  | 5262 | routing_strategy strategy, uint32_t inPastMs, | 
|  | 5263 | nsecs_t sysTime) const | 
|  | 5264 | { | 
|  | 5265 | if ((sysTime == 0) && (inPastMs != 0)) { | 
|  | 5266 | sysTime = systemTime(); | 
|  | 5267 | } | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5268 | 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] | 5269 | if (((getStrategy((audio_stream_type_t)i) == strategy) || | 
|  | 5270 | (NUM_STRATEGIES == strategy)) && | 
|  | 5271 | outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) { | 
|  | 5272 | return true; | 
|  | 5273 | } | 
|  | 5274 | } | 
|  | 5275 | return false; | 
|  | 5276 | } | 
|  | 5277 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5278 | audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage) | 
|  | 5279 | { | 
|  | 5280 | return mEngine->getForceUse(usage); | 
|  | 5281 | } | 
|  | 5282 |  | 
|  | 5283 | bool AudioPolicyManager::isInCall() | 
|  | 5284 | { | 
|  | 5285 | return isStateInCall(mEngine->getPhoneState()); | 
|  | 5286 | } | 
|  | 5287 |  | 
|  | 5288 | bool AudioPolicyManager::isStateInCall(int state) | 
|  | 5289 | { | 
|  | 5290 | return is_state_in_call(state); | 
|  | 5291 | } | 
|  | 5292 |  | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5293 | void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc) | 
|  | 5294 | { | 
|  | 5295 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--)  { | 
|  | 5296 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); | 
|  | 5297 | if (sourceDesc->mDevice->equals(deviceDesc)) { | 
|  | 5298 | ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle()); | 
|  | 5299 | stopAudioSource(sourceDesc->getHandle()); | 
|  | 5300 | } | 
|  | 5301 | } | 
|  | 5302 |  | 
|  | 5303 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--)  { | 
|  | 5304 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); | 
|  | 5305 | bool release = false; | 
|  | 5306 | for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++)  { | 
|  | 5307 | const struct audio_port_config *source = &patchDesc->mPatch.sources[j]; | 
|  | 5308 | if (source->type == AUDIO_PORT_TYPE_DEVICE && | 
|  | 5309 | source->ext.device.type == deviceDesc->type()) { | 
|  | 5310 | release = true; | 
|  | 5311 | } | 
|  | 5312 | } | 
|  | 5313 | for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++)  { | 
|  | 5314 | const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j]; | 
|  | 5315 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && | 
|  | 5316 | sink->ext.device.type == deviceDesc->type()) { | 
|  | 5317 | release = true; | 
|  | 5318 | } | 
|  | 5319 | } | 
|  | 5320 | if (release) { | 
|  | 5321 | ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle); | 
|  | 5322 | releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid); | 
|  | 5323 | } | 
|  | 5324 | } | 
|  | 5325 | } | 
|  | 5326 |  | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5327 | // Modify the list of surround sound formats supported. | 
| Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5328 | void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) { | 
|  | 5329 | FormatVector &formats = *formatsPtr; | 
| Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5330 | // TODO Set this based on Config properties. | 
|  | 5331 | const bool alwaysForceAC3 = true; | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5332 |  | 
|  | 5333 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( | 
|  | 5334 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); | 
| Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5335 | ALOGD("%s: forced use = %d", __FUNCTION__, forceUse); | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5336 |  | 
|  | 5337 | // Analyze original support for various formats. | 
| Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5338 | bool supportsAC3 = false; | 
|  | 5339 | bool supportsOtherSurround = false; | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5340 | bool supportsIEC61937 = false; | 
|  | 5341 | for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) { | 
|  | 5342 | audio_format_t format = formats[formatIndex]; | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5343 | switch (format) { | 
|  | 5344 | case AUDIO_FORMAT_AC3: | 
| Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5345 | supportsAC3 = true; | 
|  | 5346 | break; | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5347 | case AUDIO_FORMAT_E_AC3: | 
|  | 5348 | case AUDIO_FORMAT_DTS: | 
|  | 5349 | case AUDIO_FORMAT_DTS_HD: | 
| Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5350 | supportsOtherSurround = true; | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5351 | break; | 
|  | 5352 | case AUDIO_FORMAT_IEC61937: | 
|  | 5353 | supportsIEC61937 = true; | 
|  | 5354 | break; | 
|  | 5355 | default: | 
|  | 5356 | break; | 
|  | 5357 | } | 
|  | 5358 | } | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5359 |  | 
|  | 5360 | // Modify formats based on surround preferences. | 
|  | 5361 | // If NEVER, remove support for surround formats. | 
| Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5362 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { | 
|  | 5363 | if (supportsAC3 || supportsOtherSurround || supportsIEC61937) { | 
|  | 5364 | // Remove surround sound related formats. | 
|  | 5365 | for (size_t formatIndex = 0; formatIndex < formats.size(); ) { | 
|  | 5366 | audio_format_t format = formats[formatIndex]; | 
|  | 5367 | switch(format) { | 
|  | 5368 | case AUDIO_FORMAT_AC3: | 
|  | 5369 | case AUDIO_FORMAT_E_AC3: | 
|  | 5370 | case AUDIO_FORMAT_DTS: | 
|  | 5371 | case AUDIO_FORMAT_DTS_HD: | 
|  | 5372 | case AUDIO_FORMAT_IEC61937: | 
| Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5373 | formats.removeAt(formatIndex); | 
|  | 5374 | break; | 
|  | 5375 | default: | 
|  | 5376 | formatIndex++; // keep it | 
|  | 5377 | break; | 
|  | 5378 | } | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5379 | } | 
| Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5380 | supportsAC3 = false; | 
|  | 5381 | supportsOtherSurround = false; | 
|  | 5382 | supportsIEC61937 = false; | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5383 | } | 
| Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5384 | } else { // AUTO or ALWAYS | 
|  | 5385 | // Most TVs support AC3 even if they do not report it in the EDID. | 
|  | 5386 | if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)) | 
|  | 5387 | && !supportsAC3) { | 
|  | 5388 | formats.add(AUDIO_FORMAT_AC3); | 
|  | 5389 | supportsAC3 = true; | 
|  | 5390 | } | 
|  | 5391 |  | 
|  | 5392 | // If ALWAYS, add support for raw surround formats if all are missing. | 
|  | 5393 | // This assumes that if any of these formats are reported by the HAL | 
|  | 5394 | // then the report is valid and should not be modified. | 
|  | 5395 | if ((forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) | 
|  | 5396 | && !supportsOtherSurround) { | 
|  | 5397 | formats.add(AUDIO_FORMAT_E_AC3); | 
|  | 5398 | formats.add(AUDIO_FORMAT_DTS); | 
|  | 5399 | formats.add(AUDIO_FORMAT_DTS_HD); | 
|  | 5400 | supportsOtherSurround = true; | 
|  | 5401 | } | 
|  | 5402 |  | 
|  | 5403 | // Add support for IEC61937 if any raw surround supported. | 
|  | 5404 | // The HAL could do this but add it here, just in case. | 
|  | 5405 | if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) { | 
|  | 5406 | formats.add(AUDIO_FORMAT_IEC61937); | 
|  | 5407 | supportsIEC61937 = true; | 
|  | 5408 | } | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5409 | } | 
| Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5410 | } | 
|  | 5411 |  | 
|  | 5412 | // Modify the list of channel masks supported. | 
|  | 5413 | void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) { | 
|  | 5414 | ChannelsVector &channelMasks = *channelMasksPtr; | 
|  | 5415 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( | 
|  | 5416 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); | 
|  | 5417 |  | 
|  | 5418 | // If NEVER, then remove support for channelMasks > stereo. | 
|  | 5419 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { | 
|  | 5420 | for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) { | 
|  | 5421 | audio_channel_mask_t channelMask = channelMasks[maskIndex]; | 
|  | 5422 | if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) { | 
|  | 5423 | ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask); | 
|  | 5424 | channelMasks.removeAt(maskIndex); | 
|  | 5425 | } else { | 
|  | 5426 | maskIndex++; | 
|  | 5427 | } | 
|  | 5428 | } | 
|  | 5429 | // If ALWAYS, then make sure we at least support 5.1 | 
|  | 5430 | } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { | 
|  | 5431 | bool supports5dot1 = false; | 
|  | 5432 | // Are there any channel masks that can be considered "surround"? | 
|  | 5433 | for (size_t maskIndex = 0; maskIndex < channelMasks.size(); maskIndex++) { | 
|  | 5434 | audio_channel_mask_t channelMask = channelMasks[maskIndex]; | 
|  | 5435 | if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) { | 
|  | 5436 | supports5dot1 = true; | 
|  | 5437 | break; | 
|  | 5438 | } | 
|  | 5439 | } | 
|  | 5440 | // If not then add 5.1 support. | 
|  | 5441 | if (!supports5dot1) { | 
|  | 5442 | channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1); | 
|  | 5443 | ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__); | 
|  | 5444 | } | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5445 | } | 
|  | 5446 | } | 
|  | 5447 |  | 
| Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5448 | void AudioPolicyManager::updateAudioProfiles(audio_devices_t device, | 
|  | 5449 | audio_io_handle_t ioHandle, | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5450 | AudioProfileVector &profiles) | 
|  | 5451 | { | 
|  | 5452 | String8 reply; | 
| Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5453 |  | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5454 | // Format MUST be checked first to update the list of AudioProfile | 
|  | 5455 | if (profiles.hasDynamicFormat()) { | 
|  | 5456 | reply = mpClientInterface->getParameters(ioHandle, | 
|  | 5457 | String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS)); | 
| Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5458 | ALOGV("%s: supported formats %s", __FUNCTION__, reply.string()); | 
| Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5459 | AudioParameter repliedParameters(reply); | 
|  | 5460 | if (repliedParameters.get( | 
|  | 5461 | String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS), reply) != NO_ERROR) { | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5462 | ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__); | 
|  | 5463 | return; | 
|  | 5464 | } | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5465 | FormatVector formats = formatsFromString(reply.string()); | 
| Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5466 | if (device == AUDIO_DEVICE_OUT_HDMI) { | 
| Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5467 | filterSurroundFormats(&formats); | 
| Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5468 | } | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5469 | profiles.setFormats(formats); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5470 | } | 
|  | 5471 | const FormatVector &supportedFormats = profiles.getSupportedFormats(); | 
|  | 5472 |  | 
| Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5473 | for (size_t formatIndex = 0; formatIndex < supportedFormats.size(); formatIndex++) { | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5474 | audio_format_t format = supportedFormats[formatIndex]; | 
| Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5475 | ChannelsVector channelMasks; | 
|  | 5476 | SampleRateVector samplingRates; | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5477 | AudioParameter requestedParameters; | 
|  | 5478 | requestedParameters.addInt(String8(AUDIO_PARAMETER_STREAM_FORMAT), format); | 
|  | 5479 |  | 
|  | 5480 | if (profiles.hasDynamicRateFor(format)) { | 
|  | 5481 | reply = mpClientInterface->getParameters(ioHandle, | 
|  | 5482 | requestedParameters.toString() + ";" + | 
|  | 5483 | AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES); | 
|  | 5484 | ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string()); | 
| Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5485 | AudioParameter repliedParameters(reply); | 
|  | 5486 | if (repliedParameters.get( | 
|  | 5487 | String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES), reply) == NO_ERROR) { | 
|  | 5488 | samplingRates = samplingRatesFromString(reply.string()); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5489 | } | 
|  | 5490 | } | 
|  | 5491 | if (profiles.hasDynamicChannelsFor(format)) { | 
|  | 5492 | reply = mpClientInterface->getParameters(ioHandle, | 
|  | 5493 | requestedParameters.toString() + ";" + | 
|  | 5494 | AUDIO_PARAMETER_STREAM_SUP_CHANNELS); | 
|  | 5495 | ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string()); | 
| Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5496 | AudioParameter repliedParameters(reply); | 
|  | 5497 | if (repliedParameters.get( | 
|  | 5498 | String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS), reply) == NO_ERROR) { | 
|  | 5499 | channelMasks = channelMasksFromString(reply.string()); | 
| Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5500 | if (device == AUDIO_DEVICE_OUT_HDMI) { | 
|  | 5501 | filterSurroundChannelMasks(&channelMasks); | 
|  | 5502 | } | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5503 | } | 
|  | 5504 | } | 
| Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5505 | profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates)); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5506 | } | 
|  | 5507 | } | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5508 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5509 | }; // namespace android |