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