| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2009 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
| Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 17 | #define LOG_TAG "APM_AudioPolicyManager" | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 | 
|  | 19 |  | 
|  | 20 | //#define VERY_VERBOSE_LOGGING | 
|  | 21 | #ifdef VERY_VERBOSE_LOGGING | 
|  | 22 | #define ALOGVV ALOGV | 
|  | 23 | #else | 
|  | 24 | #define ALOGVV(a...) do { } while(0) | 
|  | 25 | #endif | 
|  | 26 |  | 
| Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 27 | #define AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH 128 | 
|  | 28 | #define AUDIO_POLICY_XML_CONFIG_FILE_NAME "audio_policy_configuration.xml" | 
| Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 29 | #define AUDIO_POLICY_A2DP_OFFLOAD_DISABLED_XML_CONFIG_FILE_NAME \ | 
|  | 30 | "audio_policy_configuration_a2dp_offload_disabled.xml" | 
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 31 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 32 | #include <inttypes.h> | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 33 | #include <math.h> | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 34 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 35 | #include <AudioPolicyManagerInterface.h> | 
|  | 36 | #include <AudioPolicyEngineInstance.h> | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 37 | #include <cutils/properties.h> | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 38 | #include <utils/Log.h> | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 39 | #include <media/AudioParameter.h> | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 40 | #include <media/AudioPolicyHelper.h> | 
| Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 41 | #include <media/PatchBuilder.h> | 
| Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 42 | #include <private/android_filesystem_config.h> | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 43 | #include <soundtrigger/SoundTrigger.h> | 
| Mikhail Naganov | cbc8f61 | 2016-10-11 18:05:13 -0700 | [diff] [blame] | 44 | #include <system/audio.h> | 
| Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 45 | #include <audio_policy_conf.h> | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 46 | #include "AudioPolicyManager.h" | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 47 | #ifndef USE_XML_AUDIO_POLICY_CONF | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 48 | #include <ConfigParsingUtils.h> | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 49 | #include <StreamDescriptor.h> | 
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 50 | #endif | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 51 | #include <Serializer.h> | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 52 | #include "TypeConverter.h" | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 53 | #include <policy.h> | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 54 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 55 | namespace android { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 56 |  | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 57 | //FIXME: workaround for truncated touch sounds | 
|  | 58 | // to be removed when the problem is handled by system UI | 
|  | 59 | #define TOUCH_SOUND_FIXED_DELAY_MS 100 | 
| Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 60 |  | 
|  | 61 | // Largest difference in dB on earpiece in call between the voice volume and another | 
|  | 62 | // media / notification / system volume. | 
|  | 63 | constexpr float IN_CALL_EARPIECE_HEADROOM_DB = 3.f; | 
|  | 64 |  | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 65 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) | 
|  | 66 | // Array of all surround formats. | 
|  | 67 | static const audio_format_t SURROUND_FORMATS[] = { | 
|  | 68 | AUDIO_FORMAT_AC3, | 
|  | 69 | AUDIO_FORMAT_E_AC3, | 
|  | 70 | AUDIO_FORMAT_DTS, | 
|  | 71 | AUDIO_FORMAT_DTS_HD, | 
|  | 72 | AUDIO_FORMAT_AAC_LC, | 
|  | 73 | AUDIO_FORMAT_DOLBY_TRUEHD, | 
|  | 74 | AUDIO_FORMAT_E_AC3_JOC, | 
|  | 75 | }; | 
|  | 76 | // Array of all AAC formats. When AAC is enabled by users, all AAC formats should be enabled. | 
|  | 77 | static const audio_format_t AAC_FORMATS[] = { | 
|  | 78 | AUDIO_FORMAT_AAC_LC, | 
|  | 79 | AUDIO_FORMAT_AAC_HE_V1, | 
|  | 80 | AUDIO_FORMAT_AAC_HE_V2, | 
|  | 81 | AUDIO_FORMAT_AAC_ELD, | 
|  | 82 | AUDIO_FORMAT_AAC_XHE, | 
|  | 83 | }; | 
|  | 84 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 85 | // ---------------------------------------------------------------------------- | 
|  | 86 | // AudioPolicyInterface implementation | 
|  | 87 | // ---------------------------------------------------------------------------- | 
|  | 88 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 89 | status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device, | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 90 | audio_policy_dev_state_t state, | 
|  | 91 | const char *device_address, | 
|  | 92 | const char *device_name) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 93 | { | 
| jiabin | a7b4379 | 2018-02-15 16:04:46 -0800 | [diff] [blame] | 94 | status_t status = setDeviceConnectionStateInt(device, state, device_address, device_name); | 
|  | 95 | nextAudioPortGeneration(); | 
|  | 96 | return status; | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 97 | } | 
|  | 98 |  | 
| François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 99 | void AudioPolicyManager::broadcastDeviceConnectionState(audio_devices_t device, | 
|  | 100 | audio_policy_dev_state_t state, | 
|  | 101 | const String8 &device_address) | 
|  | 102 | { | 
|  | 103 | AudioParameter param(device_address); | 
|  | 104 | const String8 key(state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE ? | 
| Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 105 | AudioParameter::keyStreamConnect : AudioParameter::keyStreamDisconnect); | 
| François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 106 | param.addInt(key, device); | 
|  | 107 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); | 
|  | 108 | } | 
|  | 109 |  | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 110 | status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device, | 
| Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 111 | audio_policy_dev_state_t state, | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 112 | const char *device_address, | 
|  | 113 | const char *device_name) | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 114 | { | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 115 | ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s", | 
| Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 116 | device, state, device_address, device_name); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 117 |  | 
|  | 118 | // connect/disconnect only 1 device at a time | 
|  | 119 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; | 
|  | 120 |  | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 121 | sp<DeviceDescriptor> devDesc = | 
|  | 122 | mHwModules.getDeviceDescriptor(device, device_address, device_name); | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 123 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 124 | // handle output devices | 
|  | 125 | if (audio_is_output_device(device)) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 126 | SortedVector <audio_io_handle_t> outputs; | 
|  | 127 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 128 | ssize_t index = mAvailableOutputDevices.indexOf(devDesc); | 
|  | 129 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 130 | // save a copy of the opened output descriptors before any output is opened or closed | 
|  | 131 | // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies() | 
|  | 132 | mPreviousOutputs = mOutputs; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 133 | switch (state) | 
|  | 134 | { | 
|  | 135 | // handle output device connection | 
| Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 136 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 137 | if (index >= 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 138 | ALOGW("setDeviceConnectionState() device already connected: %x", device); | 
|  | 139 | return INVALID_OPERATION; | 
|  | 140 | } | 
|  | 141 | ALOGV("setDeviceConnectionState() connecting device %x", device); | 
|  | 142 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 143 | // register new device as available | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 144 | index = mAvailableOutputDevices.add(devDesc); | 
|  | 145 | if (index >= 0) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 146 | sp<HwModule> module = mHwModules.getModuleForDevice(device); | 
| Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 147 | if (module == 0) { | 
|  | 148 | ALOGD("setDeviceConnectionState() could not find HW module for device %08x", | 
|  | 149 | device); | 
|  | 150 | mAvailableOutputDevices.remove(devDesc); | 
|  | 151 | return INVALID_OPERATION; | 
|  | 152 | } | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 153 | mAvailableOutputDevices[index]->attach(module); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 154 | } else { | 
|  | 155 | return NO_MEMORY; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 156 | } | 
|  | 157 |  | 
| François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 158 | // Before checking outputs, broadcast connect event to allow HAL to retrieve dynamic | 
|  | 159 | // parameters on newly connected devices (instead of opening the outputs...) | 
|  | 160 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); | 
|  | 161 |  | 
| Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 162 | if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) { | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 163 | mAvailableOutputDevices.remove(devDesc); | 
| François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 164 |  | 
|  | 165 | broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, | 
|  | 166 | devDesc->mAddress); | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 167 | return INVALID_OPERATION; | 
|  | 168 | } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 169 | // Propagate device availability to Engine | 
|  | 170 | mEngine->setDeviceConnectionState(devDesc, state); | 
|  | 171 |  | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 172 | // outputs should never be empty here | 
|  | 173 | ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():" | 
|  | 174 | "checkOutputsForDevice() returned no outputs but status OK"); | 
|  | 175 | ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs", | 
|  | 176 | outputs.size()); | 
| Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 177 |  | 
| Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 178 | } break; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 179 | // handle output device disconnection | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 180 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 181 | if (index < 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 182 | ALOGW("setDeviceConnectionState() device not connected: %x", device); | 
|  | 183 | return INVALID_OPERATION; | 
|  | 184 | } | 
|  | 185 |  | 
| Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 186 | ALOGV("setDeviceConnectionState() disconnecting output device %x", device); | 
|  | 187 |  | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 188 | // Send Disconnect to HALs | 
| François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 189 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); | 
| Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 190 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 191 | // remove device from available output devices | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 192 | mAvailableOutputDevices.remove(devDesc); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 193 |  | 
| Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 194 | checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress); | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 195 |  | 
|  | 196 | // Propagate device availability to Engine | 
|  | 197 | mEngine->setDeviceConnectionState(devDesc, state); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 198 | } break; | 
|  | 199 |  | 
|  | 200 | default: | 
|  | 201 | ALOGE("setDeviceConnectionState() invalid state: %x", state); | 
|  | 202 | return BAD_VALUE; | 
|  | 203 | } | 
|  | 204 |  | 
| Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 205 | checkForDeviceAndOutputChanges([&]() { | 
|  | 206 | // outputs must be closed after checkOutputForAllStrategies() is executed | 
|  | 207 | if (!outputs.isEmpty()) { | 
|  | 208 | for (audio_io_handle_t output : outputs) { | 
|  | 209 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); | 
|  | 210 | // close unused outputs after device disconnection or direct outputs that have been | 
|  | 211 | // opened by checkOutputsForDevice() to query dynamic parameters | 
|  | 212 | if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) || | 
|  | 213 | (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) && | 
|  | 214 | (desc->mDirectOpenCount == 0))) { | 
|  | 215 | closeOutput(output); | 
|  | 216 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 217 | } | 
| Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 218 | // check A2DP again after closing A2DP output to reset mA2dpSuspended if needed | 
|  | 219 | return true; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 220 | } | 
| Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 221 | return false; | 
|  | 222 | }); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 223 |  | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 224 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 225 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); | 
|  | 226 | updateCallRouting(newDevice); | 
|  | 227 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 228 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 229 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
|  | 230 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) { | 
|  | 231 | audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 232 | // do not force device change on duplicated output because if device is 0, it will | 
|  | 233 | // also force a device 0 for the two outputs it is duplicated to which may override | 
|  | 234 | // a valid device selection on those outputs. | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 235 | bool force = !desc->isDuplicated() | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 236 | && (!device_distinguishes_on_address(device) | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 237 | // always force when disconnecting (a non-duplicated device) | 
|  | 238 | || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 239 | setOutputDevice(desc, newDevice, force, 0); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 240 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 241 | } | 
|  | 242 |  | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 243 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { | 
|  | 244 | cleanUpForDevice(devDesc); | 
|  | 245 | } | 
|  | 246 |  | 
| Eric Laurent | 72aa32f | 2014-05-30 18:51:48 -0700 | [diff] [blame] | 247 | mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | b71e58b | 2014-05-29 16:08:11 -0700 | [diff] [blame] | 248 | return NO_ERROR; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 249 | }  // end if is output device | 
|  | 250 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 251 | // handle input devices | 
|  | 252 | if (audio_is_input_device(device)) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 253 | SortedVector <audio_io_handle_t> inputs; | 
|  | 254 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 255 | ssize_t index = mAvailableInputDevices.indexOf(devDesc); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 256 | switch (state) | 
|  | 257 | { | 
|  | 258 | // handle input device connection | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 259 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 260 | if (index >= 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 261 | ALOGW("setDeviceConnectionState() device already connected: %d", device); | 
|  | 262 | return INVALID_OPERATION; | 
|  | 263 | } | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 264 | sp<HwModule> module = mHwModules.getModuleForDevice(device); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 265 | if (module == NULL) { | 
|  | 266 | ALOGW("setDeviceConnectionState(): could not find HW module for device %08x", | 
|  | 267 | device); | 
|  | 268 | return INVALID_OPERATION; | 
|  | 269 | } | 
| François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 270 |  | 
|  | 271 | // Before checking intputs, broadcast connect event to allow HAL to retrieve dynamic | 
|  | 272 | // parameters on newly connected devices (instead of opening the inputs...) | 
|  | 273 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); | 
|  | 274 |  | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 275 | if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) { | 
| François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 276 | broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, | 
|  | 277 | devDesc->mAddress); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 278 | return INVALID_OPERATION; | 
|  | 279 | } | 
|  | 280 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 281 | index = mAvailableInputDevices.add(devDesc); | 
|  | 282 | if (index >= 0) { | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 283 | mAvailableInputDevices[index]->attach(module); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 284 | } else { | 
|  | 285 | return NO_MEMORY; | 
|  | 286 | } | 
| Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 287 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 288 | // Propagate device availability to Engine | 
|  | 289 | mEngine->setDeviceConnectionState(devDesc, state); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 290 | } break; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 291 |  | 
|  | 292 | // handle input device disconnection | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 293 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 294 | if (index < 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 295 | ALOGW("setDeviceConnectionState() device not connected: %d", device); | 
|  | 296 | return INVALID_OPERATION; | 
|  | 297 | } | 
| Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 298 |  | 
|  | 299 | ALOGV("setDeviceConnectionState() disconnecting input device %x", device); | 
|  | 300 |  | 
|  | 301 | // Set Disconnect to HALs | 
| François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 302 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); | 
| Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 303 |  | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 304 | checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 305 | mAvailableInputDevices.remove(devDesc); | 
| Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 306 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 307 | // Propagate device availability to Engine | 
|  | 308 | mEngine->setDeviceConnectionState(devDesc, state); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 309 | } break; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 310 |  | 
|  | 311 | default: | 
|  | 312 | ALOGE("setDeviceConnectionState() invalid state: %x", state); | 
|  | 313 | return BAD_VALUE; | 
|  | 314 | } | 
|  | 315 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 316 | closeAllInputs(); | 
| Eric Laurent | 5f5fca5 | 2016-08-04 11:48:57 -0700 | [diff] [blame] | 317 | // As the input device list can impact the output device selection, update | 
|  | 318 | // getDeviceForStrategy() cache | 
|  | 319 | updateDevicesAndOutputs(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 320 |  | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 321 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 322 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); | 
|  | 323 | updateCallRouting(newDevice); | 
|  | 324 | } | 
|  | 325 |  | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 326 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { | 
|  | 327 | cleanUpForDevice(devDesc); | 
|  | 328 | } | 
|  | 329 |  | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 330 | mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 331 | return NO_ERROR; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 332 | } // end if is input device | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 333 |  | 
|  | 334 | ALOGW("setDeviceConnectionState() invalid device: %x", device); | 
|  | 335 | return BAD_VALUE; | 
|  | 336 | } | 
|  | 337 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 338 | audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 339 | const char *device_address) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 340 | { | 
| Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 341 | sp<DeviceDescriptor> devDesc = | 
|  | 342 | mHwModules.getDeviceDescriptor(device, device_address, "", | 
|  | 343 | (strlen(device_address) != 0)/*matchAddress*/); | 
|  | 344 |  | 
|  | 345 | if (devDesc == 0) { | 
|  | 346 | ALOGW("getDeviceConnectionState() undeclared device, type %08x, address: %s", | 
|  | 347 | device, device_address); | 
|  | 348 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; | 
|  | 349 | } | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 350 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 351 | DeviceVector *deviceVector; | 
|  | 352 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 353 | if (audio_is_output_device(device)) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 354 | deviceVector = &mAvailableOutputDevices; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 355 | } else if (audio_is_input_device(device)) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 356 | deviceVector = &mAvailableInputDevices; | 
|  | 357 | } else { | 
|  | 358 | ALOGW("getDeviceConnectionState() invalid device type %08x", device); | 
|  | 359 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 360 | } | 
| Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 361 |  | 
|  | 362 | return (deviceVector->getDevice(device, String8(device_address)) != 0) ? | 
|  | 363 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; | 
| Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 364 | } | 
|  | 365 |  | 
| Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 366 | status_t AudioPolicyManager::handleDeviceConfigChange(audio_devices_t device, | 
|  | 367 | const char *device_address, | 
|  | 368 | const char *device_name) | 
|  | 369 | { | 
|  | 370 | status_t status; | 
| Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 371 | String8 reply; | 
|  | 372 | AudioParameter param; | 
|  | 373 | int isReconfigA2dpSupported = 0; | 
| Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 374 |  | 
|  | 375 | ALOGV("handleDeviceConfigChange(() device: 0x%X, address %s name %s", | 
|  | 376 | device, device_address, device_name); | 
|  | 377 |  | 
| Pavlin Radoslavov | c694ff4 | 2017-01-09 23:27:29 -0800 | [diff] [blame] | 378 | // connect/disconnect only 1 device at a time | 
|  | 379 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; | 
|  | 380 |  | 
| Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 381 | // Check if the device is currently connected | 
|  | 382 | sp<DeviceDescriptor> devDesc = | 
|  | 383 | mHwModules.getDeviceDescriptor(device, device_address, device_name); | 
|  | 384 | ssize_t index = mAvailableOutputDevices.indexOf(devDesc); | 
|  | 385 | if (index < 0) { | 
|  | 386 | // Nothing to do: device is not connected | 
|  | 387 | return NO_ERROR; | 
|  | 388 | } | 
|  | 389 |  | 
| Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 390 | // For offloaded A2DP, Hw modules may have the capability to | 
|  | 391 | // configure codecs. Check if any of the loaded hw modules | 
|  | 392 | // supports this. | 
|  | 393 | // If supported, send a set parameter to configure A2DP codecs | 
|  | 394 | // and return. No need to toggle device state. | 
|  | 395 | if (device & AUDIO_DEVICE_OUT_ALL_A2DP) { | 
|  | 396 | reply = mpClientInterface->getParameters( | 
|  | 397 | AUDIO_IO_HANDLE_NONE, | 
|  | 398 | String8(AudioParameter::keyReconfigA2dpSupported)); | 
|  | 399 | AudioParameter repliedParameters(reply); | 
|  | 400 | repliedParameters.getInt( | 
|  | 401 | String8(AudioParameter::keyReconfigA2dpSupported), isReconfigA2dpSupported); | 
|  | 402 | if (isReconfigA2dpSupported) { | 
|  | 403 | const String8 key(AudioParameter::keyReconfigA2dp); | 
|  | 404 | param.add(key, String8("true")); | 
|  | 405 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); | 
|  | 406 | return NO_ERROR; | 
|  | 407 | } | 
|  | 408 | } | 
|  | 409 |  | 
| Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 410 | // Toggle the device state: UNAVAILABLE -> AVAILABLE | 
|  | 411 | // This will force reading again the device configuration | 
|  | 412 | status = setDeviceConnectionState(device, | 
|  | 413 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, | 
|  | 414 | device_address, device_name); | 
|  | 415 | if (status != NO_ERROR) { | 
|  | 416 | ALOGW("handleDeviceConfigChange() error disabling connection state: %d", | 
|  | 417 | status); | 
|  | 418 | return status; | 
|  | 419 | } | 
|  | 420 |  | 
|  | 421 | status = setDeviceConnectionState(device, | 
|  | 422 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, | 
|  | 423 | device_address, device_name); | 
|  | 424 | if (status != NO_ERROR) { | 
|  | 425 | ALOGW("handleDeviceConfigChange() error enabling connection state: %d", | 
|  | 426 | status); | 
|  | 427 | return status; | 
|  | 428 | } | 
|  | 429 |  | 
|  | 430 | return NO_ERROR; | 
|  | 431 | } | 
|  | 432 |  | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 433 | uint32_t AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, uint32_t delayMs) | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 434 | { | 
|  | 435 | bool createTxPatch = false; | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 436 | uint32_t muteWaitMs = 0; | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 437 |  | 
| Andy Hung | a76c7de | 2016-12-13 19:14:31 -0800 | [diff] [blame] | 438 | if(!hasPrimaryOutput() || mPrimaryOutput->device() == AUDIO_DEVICE_OUT_STUB) { | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 439 | return muteWaitMs; | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 440 | } | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 441 | audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 442 | ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice); | 
|  | 443 |  | 
|  | 444 | // release existing RX patch if any | 
|  | 445 | if (mCallRxPatch != 0) { | 
|  | 446 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); | 
|  | 447 | mCallRxPatch.clear(); | 
|  | 448 | } | 
|  | 449 | // release TX patch if any | 
|  | 450 | if (mCallTxPatch != 0) { | 
|  | 451 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); | 
|  | 452 | mCallTxPatch.clear(); | 
|  | 453 | } | 
|  | 454 |  | 
|  | 455 | // If the RX device is on the primary HW module, then use legacy routing method for voice calls | 
|  | 456 | // via setOutputDevice() on primary output. | 
|  | 457 | // Otherwise, create two audio patches for TX and RX path. | 
|  | 458 | if (availablePrimaryOutputDevices() & rxDevice) { | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 459 | muteWaitMs = setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 460 | // If the TX device is also on the primary HW module, setOutputDevice() will take care | 
|  | 461 | // of it due to legacy implementation. If not, create a patch. | 
|  | 462 | if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN) | 
|  | 463 | == AUDIO_DEVICE_NONE) { | 
|  | 464 | createTxPatch = true; | 
|  | 465 | } | 
| Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 466 | } else { // create RX path audio patch | 
| Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 467 | mCallRxPatch = createTelephonyPatch(true /*isRx*/, rxDevice, delayMs); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 468 | createTxPatch = true; | 
|  | 469 | } | 
| Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 470 | if (createTxPatch) { // create TX path audio patch | 
| Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 471 | mCallTxPatch = createTelephonyPatch(false /*isRx*/, txDevice, delayMs); | 
|  | 472 | } | 
| Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 473 |  | 
| Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 474 | return muteWaitMs; | 
|  | 475 | } | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 476 |  | 
| Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 477 | sp<AudioPatch> AudioPolicyManager::createTelephonyPatch( | 
|  | 478 | bool isRx, audio_devices_t device, uint32_t delayMs) { | 
| Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 479 | PatchBuilder patchBuilder; | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 480 |  | 
| Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 481 | sp<DeviceDescriptor> txSourceDeviceDesc; | 
|  | 482 | if (isRx) { | 
| Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 483 | patchBuilder.addSink(findDevice(mAvailableOutputDevices, device)). | 
|  | 484 | addSource(findDevice(mAvailableInputDevices, AUDIO_DEVICE_IN_TELEPHONY_RX)); | 
| Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 485 | } else { | 
| Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 486 | patchBuilder.addSource(txSourceDeviceDesc = findDevice(mAvailableInputDevices, device)). | 
|  | 487 | addSink(findDevice(mAvailableOutputDevices, AUDIO_DEVICE_OUT_TELEPHONY_TX)); | 
| Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 488 | } | 
|  | 489 |  | 
|  | 490 | audio_devices_t outputDevice = isRx ? device : AUDIO_DEVICE_OUT_TELEPHONY_TX; | 
|  | 491 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(outputDevice, mOutputs); | 
|  | 492 | audio_io_handle_t output = selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID); | 
|  | 493 | // request to reuse existing output stream if one is already opened to reach the target device | 
|  | 494 | if (output != AUDIO_IO_HANDLE_NONE) { | 
|  | 495 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
|  | 496 | ALOG_ASSERT(!outputDesc->isDuplicated(), | 
|  | 497 | "%s() %#x device output %d is duplicated", __func__, outputDevice, output); | 
| Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 498 | patchBuilder.addSource(outputDesc, { .stream = AUDIO_STREAM_PATCH }); | 
| Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 499 | } | 
|  | 500 |  | 
|  | 501 | if (!isRx) { | 
| Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 502 | // terminate active capture if on the same HW module as the call TX source device | 
|  | 503 | // FIXME: would be better to refine to only inputs whose profile connects to the | 
|  | 504 | // call TX device but this information is not in the audio patch and logic here must be | 
|  | 505 | // symmetric to the one in startInput() | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 506 | for (const auto& activeDesc : mInputs.getActiveInputs()) { | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 507 | if (activeDesc->hasSameHwModuleAs(txSourceDeviceDesc)) { | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame^] | 508 | closeSessions(activeDesc, true  /*activeOnly*/); | 
| Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 509 | } | 
|  | 510 | } | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 511 | } | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 512 |  | 
| Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 513 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; | 
| Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 514 | status_t status = mpClientInterface->createAudioPatch( | 
|  | 515 | patchBuilder.patch(), &afPatchHandle, delayMs); | 
| Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 516 | ALOGW_IF(status != NO_ERROR, | 
|  | 517 | "%s() error %d creating %s audio patch", __func__, status, isRx ? "RX" : "TX"); | 
|  | 518 | sp<AudioPatch> audioPatch; | 
|  | 519 | if (status == NO_ERROR) { | 
| Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 520 | audioPatch = new AudioPatch(patchBuilder.patch(), mUidCached); | 
| Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 521 | audioPatch->mAfPatchHandle = afPatchHandle; | 
|  | 522 | audioPatch->mUid = mUidCached; | 
|  | 523 | } | 
|  | 524 | return audioPatch; | 
|  | 525 | } | 
|  | 526 |  | 
| Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 527 | sp<DeviceDescriptor> AudioPolicyManager::findDevice( | 
|  | 528 | const DeviceVector& devices, audio_devices_t device) { | 
| Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 529 | DeviceVector deviceList = devices.getDevicesFromTypeMask(device); | 
| Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 530 | ALOG_ASSERT(!deviceList.isEmpty(), | 
|  | 531 | "%s() selected device type %#x is not in devices list", __func__, device); | 
| Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 532 | return deviceList.itemAt(0); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 533 | } | 
|  | 534 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 535 | void AudioPolicyManager::setPhoneState(audio_mode_t state) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 536 | { | 
|  | 537 | ALOGV("setPhoneState() state %d", state); | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 538 | // store previous phone state for management of sonification strategy below | 
|  | 539 | int oldState = mEngine->getPhoneState(); | 
|  | 540 |  | 
|  | 541 | if (mEngine->setPhoneState(state) != NO_ERROR) { | 
|  | 542 | ALOGW("setPhoneState() invalid or same state %d", state); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 543 | return; | 
|  | 544 | } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 545 | /// Opens: can these line be executed after the switch of volume curves??? | 
| Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 546 | if (isStateInCall(oldState)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 547 | ALOGV("setPhoneState() in call state management: new state is %d", state); | 
| Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 548 | // force reevaluating accessibility routing when call stops | 
| Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 549 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 550 | } | 
|  | 551 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 552 | /** | 
|  | 553 | * Switching to or from incall state or switching between telephony and VoIP lead to force | 
|  | 554 | * routing command. | 
|  | 555 | */ | 
|  | 556 | bool force = ((is_state_in_call(oldState) != is_state_in_call(state)) | 
|  | 557 | || (is_state_in_call(state) && (state != oldState))); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 558 |  | 
|  | 559 | // check for device and output changes triggered by new phone state | 
| Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 560 | checkForDeviceAndOutputChanges(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 561 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 562 | int delayMs = 0; | 
|  | 563 | if (isStateInCall(state)) { | 
|  | 564 | nsecs_t sysTime = systemTime(); | 
|  | 565 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 566 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 567 | // mute media and sonification strategies and delay device switch by the largest | 
|  | 568 | // latency of any output where either strategy is active. | 
|  | 569 | // 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] | 570 | if ((isStrategyActive(desc, STRATEGY_MEDIA, | 
|  | 571 | SONIFICATION_HEADSET_MUSIC_DELAY, | 
|  | 572 | sysTime) || | 
|  | 573 | isStrategyActive(desc, STRATEGY_SONIFICATION, | 
|  | 574 | SONIFICATION_HEADSET_MUSIC_DELAY, | 
|  | 575 | sysTime)) && | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 576 | (delayMs < (int)desc->latency()*2)) { | 
|  | 577 | delayMs = desc->latency()*2; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 578 | } | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 579 | setStrategyMute(STRATEGY_MEDIA, true, desc); | 
|  | 580 | setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 581 | getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/)); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 582 | setStrategyMute(STRATEGY_SONIFICATION, true, desc); | 
|  | 583 | setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 584 | getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/)); | 
|  | 585 | } | 
|  | 586 | } | 
|  | 587 |  | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 588 | if (hasPrimaryOutput()) { | 
|  | 589 | // Note that despite the fact that getNewOutputDevice() is called on the primary output, | 
|  | 590 | // the device returned is not necessarily reachable via this output | 
|  | 591 | audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); | 
|  | 592 | // force routing command to audio hardware when ending call | 
|  | 593 | // even if no device change is needed | 
|  | 594 | if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) { | 
|  | 595 | rxDevice = mPrimaryOutput->device(); | 
|  | 596 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 597 |  | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 598 | if (state == AUDIO_MODE_IN_CALL) { | 
|  | 599 | updateCallRouting(rxDevice, delayMs); | 
|  | 600 | } else if (oldState == AUDIO_MODE_IN_CALL) { | 
|  | 601 | if (mCallRxPatch != 0) { | 
|  | 602 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); | 
|  | 603 | mCallRxPatch.clear(); | 
|  | 604 | } | 
|  | 605 | if (mCallTxPatch != 0) { | 
|  | 606 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); | 
|  | 607 | mCallTxPatch.clear(); | 
|  | 608 | } | 
|  | 609 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); | 
|  | 610 | } else { | 
|  | 611 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 612 | } | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 613 | } | 
| Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 614 |  | 
|  | 615 | // reevaluate routing on all outputs in case tracks have been started during the call | 
|  | 616 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 617 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
|  | 618 | audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/); | 
|  | 619 | if (state != AUDIO_MODE_IN_CALL || desc != mPrimaryOutput) { | 
| Yung Ti Su | f60c824 | 2018-05-10 18:07:26 +0800 | [diff] [blame] | 620 | setOutputDevice(desc, newDevice, (newDevice != AUDIO_DEVICE_NONE), 0 /*delayMs*/); | 
| Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 621 | } | 
|  | 622 | } | 
|  | 623 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 624 | if (isStateInCall(state)) { | 
|  | 625 | ALOGV("setPhoneState() in call state management: new state is %d", state); | 
| Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 626 | // force reevaluating accessibility routing when call starts | 
|  | 627 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 628 | } | 
|  | 629 |  | 
|  | 630 | // 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] | 631 | if (state == AUDIO_MODE_RINGTONE && | 
|  | 632 | isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 633 | mLimitRingtoneVolume = true; | 
|  | 634 | } else { | 
|  | 635 | mLimitRingtoneVolume = false; | 
|  | 636 | } | 
|  | 637 | } | 
|  | 638 |  | 
| Jean-Michel Trivi | 887a9ed | 2015-03-31 18:02:24 -0700 | [diff] [blame] | 639 | audio_mode_t AudioPolicyManager::getPhoneState() { | 
|  | 640 | return mEngine->getPhoneState(); | 
|  | 641 | } | 
|  | 642 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 643 | void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 644 | audio_policy_forced_cfg_t config) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 645 | { | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 646 | ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState()); | 
| Eric Laurent | 8dc87a6 | 2017-05-16 19:00:40 -0700 | [diff] [blame] | 647 | if (config == mEngine->getForceUse(usage)) { | 
|  | 648 | return; | 
|  | 649 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 650 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 651 | if (mEngine->setForceUse(usage, config) != NO_ERROR) { | 
|  | 652 | ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage); | 
|  | 653 | return; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 654 | } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 655 | bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) || | 
|  | 656 | (usage == AUDIO_POLICY_FORCE_FOR_DOCK) || | 
|  | 657 | (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 658 |  | 
|  | 659 | // check for device and output changes triggered by new force usage | 
| Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 660 | checkForDeviceAndOutputChanges(); | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 661 |  | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 662 | //FIXME: workaround for truncated touch sounds | 
|  | 663 | // to be removed when the problem is handled by system UI | 
|  | 664 | uint32_t delayMs = 0; | 
|  | 665 | uint32_t waitMs = 0; | 
|  | 666 | if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) { | 
|  | 667 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; | 
|  | 668 | } | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 669 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 670 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/); | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 671 | waitMs = updateCallRouting(newDevice, delayMs); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 672 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 673 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 674 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); | 
|  | 675 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/); | 
|  | 676 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) { | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 677 | waitMs = setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE), | 
|  | 678 | delayMs); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 679 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 680 | if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) { | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 681 | applyStreamVolumes(outputDesc, newDevice, waitMs, true); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 682 | } | 
|  | 683 | } | 
|  | 684 |  | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 685 | for (const auto& activeDesc : mInputs.getActiveInputs()) { | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 686 | audio_devices_t newDevice = getNewInputDevice(activeDesc); | 
| Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 687 | // Force new input selection if the new device can not be reached via current input | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 688 | if (activeDesc->mProfile->getSupportedDevices().types() & | 
|  | 689 | (newDevice & ~AUDIO_DEVICE_BIT_IN)) { | 
|  | 690 | setInputDevice(activeDesc->mIoHandle, newDevice); | 
| Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 691 | } else { | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 692 | closeInput(activeDesc->mIoHandle); | 
| Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 693 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 694 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 695 | } | 
|  | 696 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 697 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 698 | { | 
|  | 699 | ALOGV("setSystemProperty() property %s, value %s", property, value); | 
|  | 700 | } | 
|  | 701 |  | 
|  | 702 | // Find a direct output profile compatible with the parameters passed, even if the input flags do | 
|  | 703 | // not explicitly request a direct output | 
| Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 704 | sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput( | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 705 | audio_devices_t device, | 
|  | 706 | uint32_t samplingRate, | 
|  | 707 | audio_format_t format, | 
|  | 708 | audio_channel_mask_t channelMask, | 
|  | 709 | audio_output_flags_t flags) | 
|  | 710 | { | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 711 | // only retain flags that will drive the direct output profile selection | 
|  | 712 | // if explicitly requested | 
|  | 713 | static const uint32_t kRelevantFlags = | 
| Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 714 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | | 
|  | 715 | AUDIO_OUTPUT_FLAG_VOIP_RX); | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 716 | flags = | 
|  | 717 | (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); | 
|  | 718 |  | 
|  | 719 | sp<IOProfile> profile; | 
|  | 720 |  | 
| Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 721 | for (const auto& hwModule : mHwModules) { | 
| Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 722 | for (const auto& curProfile : hwModule->getOutputProfiles()) { | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 723 | if (!curProfile->isCompatibleProfile(device, String8(""), | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 724 | samplingRate, NULL /*updatedSamplingRate*/, | 
|  | 725 | format, NULL /*updatedFormat*/, | 
|  | 726 | channelMask, NULL /*updatedChannelMask*/, | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 727 | flags)) { | 
|  | 728 | continue; | 
|  | 729 | } | 
|  | 730 | // reject profiles not corresponding to a device currently available | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 731 | if ((mAvailableOutputDevices.types() & curProfile->getSupportedDevicesType()) == 0) { | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 732 | continue; | 
|  | 733 | } | 
|  | 734 | // if several profiles are compatible, give priority to one with offload capability | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 735 | if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) { | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 736 | continue; | 
|  | 737 | } | 
|  | 738 | profile = curProfile; | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 739 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 740 | break; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 741 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 742 | } | 
|  | 743 | } | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 744 | return profile; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 745 | } | 
|  | 746 |  | 
| Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 747 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 748 | { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 749 | routing_strategy strategy = getStrategy(stream); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 750 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); | 
| Andy Hung | c990152 | 2017-11-10 20:07:54 -0800 | [diff] [blame] | 751 |  | 
|  | 752 | // Note that related method getOutputForAttr() uses getOutputForDevice() not selectOutput(). | 
|  | 753 | // We use selectOutput() here since we don't have the desired AudioTrack sample rate, | 
|  | 754 | // format, flags, etc. This may result in some discrepancy for functions that utilize | 
|  | 755 | // getOutput() solely on audio_stream_type such as AudioSystem::getOutputFrameCount() | 
|  | 756 | // and AudioSystem::getOutputSamplingRate(). | 
|  | 757 |  | 
| Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 758 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); | 
|  | 759 | audio_io_handle_t output = selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 760 |  | 
| Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 761 | ALOGV("getOutput() stream %d selected device %08x, output %d", stream, device, output); | 
|  | 762 | return output; | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 763 | } | 
|  | 764 |  | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 765 | status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, | 
|  | 766 | audio_io_handle_t *output, | 
|  | 767 | audio_session_t session, | 
|  | 768 | audio_stream_type_t *stream, | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 769 | uid_t uid, | 
| Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 770 | const audio_config_t *config, | 
| Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 771 | audio_output_flags_t *flags, | 
| Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 772 | audio_port_handle_t *selectedDeviceId, | 
| Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 773 | audio_port_handle_t *portId) | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 774 | { | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 775 | audio_attributes_t attributes; | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame^] | 776 | DeviceVector outputDevices; | 
|  | 777 | routing_strategy strategy; | 
|  | 778 | audio_devices_t device; | 
|  | 779 | audio_port_handle_t requestedDeviceId = *selectedDeviceId; | 
|  | 780 |  | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 781 | if (attr != NULL) { | 
|  | 782 | if (!isValidAttributes(attr)) { | 
|  | 783 | ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]", | 
|  | 784 | attr->usage, attr->content_type, attr->flags, | 
|  | 785 | attr->tags); | 
|  | 786 | return BAD_VALUE; | 
|  | 787 | } | 
|  | 788 | attributes = *attr; | 
|  | 789 | } else { | 
|  | 790 | if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) { | 
|  | 791 | ALOGE("getOutputForAttr():  invalid stream type"); | 
|  | 792 | return BAD_VALUE; | 
|  | 793 | } | 
|  | 794 | stream_type_to_audio_attributes(*stream, &attributes); | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 795 | } | 
| Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 796 |  | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 797 | ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x" | 
|  | 798 | " session %d selectedDeviceId %d", | 
|  | 799 | attributes.usage, attributes.content_type, attributes.tags, attributes.flags, | 
| Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 800 | session, *selectedDeviceId); | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 801 |  | 
| Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 802 | // TODO: check for existing client for this port ID | 
|  | 803 | if (*portId == AUDIO_PORT_HANDLE_NONE) { | 
|  | 804 | *portId = AudioPort::getNextUniqueId(); | 
|  | 805 | } | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 806 |  | 
| Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 807 | // First check for explicit routing (eg. setPreferredDevice) | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 808 | sp<DeviceDescriptor> deviceDesc; | 
| Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 809 | if (*selectedDeviceId != AUDIO_PORT_HANDLE_NONE) { | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 810 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(*selectedDeviceId); | 
| Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 811 | } else { | 
|  | 812 | // If no explict route, is there a matching dynamic policy that applies? | 
|  | 813 | sp<SwAudioOutputDescriptor> desc; | 
|  | 814 | if (mPolicyMixes.getOutputForAttr(attributes, uid, desc) == NO_ERROR) { | 
|  | 815 | ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr"); | 
|  | 816 | if (!audio_has_proportional_frames(config->format)) { | 
|  | 817 | return BAD_VALUE; | 
|  | 818 | } | 
|  | 819 | *stream = streamTypefromAttributesInt(&attributes); | 
|  | 820 | *output = desc->mIoHandle; | 
|  | 821 | ALOGV("getOutputForAttr() returns output %d", *output); | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame^] | 822 | goto exit; | 
| Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 823 | } | 
|  | 824 |  | 
|  | 825 | // Virtual sources must always be dynamicaly or explicitly routed | 
|  | 826 | if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) { | 
|  | 827 | ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE"); | 
|  | 828 | return BAD_VALUE; | 
|  | 829 | } | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 830 | } | 
| Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 831 |  | 
|  | 832 | // Virtual sources must always be dynamicaly or explicitly routed | 
|  | 833 | if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) { | 
|  | 834 | ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE"); | 
|  | 835 | return BAD_VALUE; | 
|  | 836 | } | 
|  | 837 |  | 
|  | 838 | *stream = streamTypefromAttributesInt(&attributes); | 
|  | 839 |  | 
|  | 840 | // TODO:  Should this happen only if an explicit route is active? | 
|  | 841 | // the previous code structure meant that this would always happen which | 
|  | 842 | // would appear to result in adding a null deviceDesc when not using an | 
|  | 843 | // explicit route.  Is that the intended and necessary behavior? | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 844 | mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid); | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 845 |  | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame^] | 846 | strategy = (routing_strategy) getStrategyForAttr(&attributes); | 
|  | 847 | device = getDeviceForStrategy(strategy, false /*fromCache*/); | 
| Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 848 |  | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 849 | if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) { | 
| Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 850 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); | 
| Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 851 | } | 
|  | 852 |  | 
| Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 853 | // Set incall music only if device was explicitly set, and fallback to the device which is | 
|  | 854 | // chosen by the engine if not. | 
|  | 855 | // FIXME: provide a more generic approach which is not device specific and move this back | 
|  | 856 | // to getOutputForDevice. | 
|  | 857 | if (device == AUDIO_DEVICE_OUT_TELEPHONY_TX && | 
|  | 858 | *stream == AUDIO_STREAM_MUSIC && | 
|  | 859 | audio_is_linear_pcm(config->format) && | 
|  | 860 | isInCall()) { | 
|  | 861 | if (*selectedDeviceId != AUDIO_PORT_HANDLE_NONE) { | 
|  | 862 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INCALL_MUSIC; | 
|  | 863 | } else { | 
|  | 864 | device = mEngine->getDeviceForStrategy(strategy); | 
|  | 865 | } | 
|  | 866 | } | 
|  | 867 |  | 
| Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 868 | ALOGV("getOutputForAttr() device 0x%x, sampling rate %d, format %#x, channel mask %#x, " | 
|  | 869 | "flags %#x", | 
| Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 870 | device, config->sample_rate, config->format, config->channel_mask, *flags); | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 871 |  | 
| Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 872 | *output = getOutputForDevice(device, session, *stream, config, flags); | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 873 | if (*output == AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 874 | mOutputRoutes.removeRoute(session); | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 875 | return INVALID_OPERATION; | 
|  | 876 | } | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 877 |  | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame^] | 878 | outputDevices = mAvailableOutputDevices.getDevicesFromTypeMask(device); | 
| Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 879 | *selectedDeviceId = outputDevices.size() > 0 ? outputDevices.itemAt(0)->getId() | 
|  | 880 | : AUDIO_PORT_HANDLE_NONE; | 
|  | 881 |  | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame^] | 882 | exit: | 
|  | 883 | audio_config_base_t clientConfig = {.sample_rate = config->sample_rate, | 
|  | 884 | .format = config->format, | 
|  | 885 | .channel_mask = config->channel_mask }; | 
|  | 886 | sp<TrackClientDescriptor> clientDesc = | 
|  | 887 | new TrackClientDescriptor(*portId, uid, session, | 
|  | 888 | attributes, clientConfig, requestedDeviceId, *stream, *flags); | 
|  | 889 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(*output); | 
|  | 890 | outputDesc->clients().emplace(*portId, clientDesc); | 
|  | 891 |  | 
|  | 892 | ALOGV("  getOutputForAttr() returns output %d selectedDeviceId %d for port ID %d", | 
|  | 893 | *output, *selectedDeviceId, *portId); | 
| Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 894 |  | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 895 | return NO_ERROR; | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 896 | } | 
|  | 897 |  | 
|  | 898 | audio_io_handle_t AudioPolicyManager::getOutputForDevice( | 
|  | 899 | audio_devices_t device, | 
| Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 900 | audio_session_t session, | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 901 | audio_stream_type_t stream, | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 902 | const audio_config_t *config, | 
| Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 903 | audio_output_flags_t *flags) | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 904 | { | 
| Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 905 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 906 | status_t status; | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 907 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 908 | // open a direct output if required by specified parameters | 
|  | 909 | //force direct flag if offload flag is set: offloading implies a direct output stream | 
|  | 910 | // and all common behaviors are driven by checking only the direct flag | 
|  | 911 | // this should normally be set appropriately in the policy configuration file | 
| Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 912 | if ((*flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { | 
|  | 913 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 914 | } | 
| Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 915 | if ((*flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { | 
|  | 916 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); | 
| Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 917 | } | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 918 | // only allow deep buffering for music stream type | 
|  | 919 | if (stream != AUDIO_STREAM_MUSIC) { | 
| Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 920 | *flags = (audio_output_flags_t)(*flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); | 
| Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 921 | } else if (/* stream == AUDIO_STREAM_MUSIC && */ | 
| Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 922 | *flags == AUDIO_OUTPUT_FLAG_NONE && | 
| Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 923 | property_get_bool("audio.deep_buffer.media", false /* default_value */)) { | 
|  | 924 | // use DEEP_BUFFER as default output for music stream type | 
| Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 925 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER; | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 926 | } | 
| Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 927 | if (stream == AUDIO_STREAM_TTS) { | 
| Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 928 | *flags = AUDIO_OUTPUT_FLAG_TTS; | 
| Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 929 | } else if (stream == AUDIO_STREAM_VOICE_CALL && | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 930 | audio_is_linear_pcm(config->format)) { | 
| Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 931 | *flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX | | 
| Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 932 | AUDIO_OUTPUT_FLAG_DIRECT); | 
|  | 933 | ALOGV("Set VoIP and Direct output flags for PCM format"); | 
| Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 934 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 935 |  | 
| Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 936 |  | 
| Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 937 | sp<IOProfile> profile; | 
|  | 938 |  | 
|  | 939 | // 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] | 940 | // and not explicitly requested | 
| Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 941 | if (((*flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 942 | audio_is_linear_pcm(config->format) && config->sample_rate <= SAMPLE_RATE_HZ_MAX && | 
|  | 943 | audio_channel_count_from_out_mask(config->channel_mask) <= 2) { | 
| Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 944 | goto non_direct_output; | 
|  | 945 | } | 
|  | 946 |  | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 947 | // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled. | 
|  | 948 | // This prevents creating an offloaded track and tearing it down immediately after start | 
|  | 949 | // when audioflinger detects there is an active non offloadable effect. | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 950 | // FIXME: We should check the audio session here but we do not have it in this context. | 
|  | 951 | // This may prevent offloading in rare situations where effects are left active by apps | 
|  | 952 | // in the background. | 
| Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 953 |  | 
| Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 954 | if (((*flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 955 | !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 956 | profile = getProfileForDirectOutput(device, | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 957 | config->sample_rate, | 
|  | 958 | config->format, | 
|  | 959 | config->channel_mask, | 
| Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 960 | (audio_output_flags_t)*flags); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 961 | } | 
|  | 962 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 963 | if (profile != 0) { | 
| Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 964 | // exclusive outputs for MMAP and Offload are enforced by different session ids. | 
|  | 965 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 966 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
|  | 967 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { | 
|  | 968 | // reuse direct output if currently open by the same client | 
|  | 969 | // and configured with same parameters | 
|  | 970 | if ((config->sample_rate == desc->mSamplingRate) && | 
| Andy Hung | 5659ed5 | 2018-04-30 10:31:26 -0700 | [diff] [blame] | 971 | (config->format == desc->mFormat) && | 
| Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 972 | (config->channel_mask == desc->mChannelMask) && | 
|  | 973 | (session == desc->mDirectClientSession)) { | 
|  | 974 | desc->mDirectOpenCount++; | 
|  | 975 | ALOGI("getOutputForDevice() reusing direct output %d for session %d", | 
|  | 976 | mOutputs.keyAt(i), session); | 
|  | 977 | return mOutputs.keyAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 978 | } | 
|  | 979 | } | 
|  | 980 | } | 
| Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 981 |  | 
|  | 982 | if (!profile->canOpenNewIo()) { | 
|  | 983 | goto non_direct_output; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 984 | } | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 985 |  | 
| Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 986 | sp<SwAudioOutputDescriptor> outputDesc = | 
|  | 987 | new SwAudioOutputDescriptor(profile, mpClientInterface); | 
| Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 988 |  | 
| Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 989 | DeviceVector outputDevices = mAvailableOutputDevices.getDevicesFromTypeMask(device); | 
| Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 990 | String8 address = outputDevices.size() > 0 ? outputDevices.itemAt(0)->mAddress | 
|  | 991 | : String8(""); | 
|  | 992 |  | 
| Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 993 | status = outputDesc->open(config, device, address, stream, *flags, &output); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 994 |  | 
|  | 995 | // only accept an output with the requested parameters | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 996 | if (status != NO_ERROR || | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 997 | (config->sample_rate != 0 && config->sample_rate != outputDesc->mSamplingRate) || | 
| Andy Hung | 5659ed5 | 2018-04-30 10:31:26 -0700 | [diff] [blame] | 998 | (config->format != AUDIO_FORMAT_DEFAULT && config->format != outputDesc->mFormat) || | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 999 | (config->channel_mask != 0 && config->channel_mask != outputDesc->mChannelMask)) { | 
|  | 1000 | ALOGV("getOutputForDevice() failed opening direct output: output %d sample rate %d %d," | 
|  | 1001 | "format %d %d, channel mask %04x %04x", output, config->sample_rate, | 
|  | 1002 | outputDesc->mSamplingRate, config->format, outputDesc->mFormat, | 
|  | 1003 | config->channel_mask, outputDesc->mChannelMask); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1004 | if (output != AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1005 | outputDesc->close(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1006 | } | 
| Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 1007 | // fall back to mixer output if possible when the direct output could not be open | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1008 | if (audio_is_linear_pcm(config->format) && | 
|  | 1009 | config->sample_rate  <= SAMPLE_RATE_HZ_MAX) { | 
| Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 1010 | goto non_direct_output; | 
|  | 1011 | } | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1012 | return AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1013 | } | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1014 | outputDesc->mRefCount[stream] = 0; | 
|  | 1015 | outputDesc->mStopTime[stream] = 0; | 
|  | 1016 | outputDesc->mDirectOpenCount = 1; | 
| Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 1017 | outputDesc->mDirectClientSession = session; | 
|  | 1018 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1019 | addOutput(output, outputDesc); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1020 | mPreviousOutputs = mOutputs; | 
| Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 1021 | ALOGV("getOutputForDevice() returns new direct output %d", output); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1022 | mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1023 | return output; | 
|  | 1024 | } | 
|  | 1025 |  | 
| Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 1026 | non_direct_output: | 
| Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 1027 |  | 
|  | 1028 | // A request for HW A/V sync cannot fallback to a mixed output because time | 
|  | 1029 | // stamps are embedded in audio data | 
| Phil Burk | 2d05993 | 2018-02-15 15:55:11 -0800 | [diff] [blame] | 1030 | if ((*flags & (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_MMAP_NOIRQ)) != 0) { | 
| Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 1031 | return AUDIO_IO_HANDLE_NONE; | 
|  | 1032 | } | 
|  | 1033 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1034 | // ignoring channel mask due to downmix capability in mixer | 
|  | 1035 |  | 
|  | 1036 | // open a non direct output | 
|  | 1037 |  | 
|  | 1038 | // for non direct outputs, only PCM is supported | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1039 | if (audio_is_linear_pcm(config->format)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1040 | // get which output is suitable for the specified stream. The actual | 
|  | 1041 | // routing change will happen when startOutput() will be called | 
|  | 1042 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); | 
|  | 1043 |  | 
| Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1044 | // at this stage we should ignore the DIRECT flag as no direct output could be found earlier | 
| Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1045 | *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_DIRECT); | 
|  | 1046 | output = selectOutput(outputs, *flags, config->format); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1047 | } | 
| Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 1048 | ALOGW_IF((output == 0), "getOutputForDevice() could not find output for stream %d, " | 
| Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1049 | "sampling rate %d, format %#x, channels %#x, flags %#x", | 
| Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1050 | stream, config->sample_rate, config->format, config->channel_mask, *flags); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1051 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1052 | return output; | 
|  | 1053 | } | 
|  | 1054 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1055 | 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] | 1056 | audio_output_flags_t flags, | 
|  | 1057 | audio_format_t format) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1058 | { | 
|  | 1059 | // select one output among several that provide a path to a particular device or set of | 
|  | 1060 | // devices (the list was previously build by getOutputsForDevice()). | 
|  | 1061 | // The priority is as follows: | 
|  | 1062 | // 1: the output with the highest number of requested policy flags | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1063 | // 2: the output with the bit depth the closest to the requested one | 
|  | 1064 | // 3: the primary output | 
|  | 1065 | // 4: the first output in the list | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1066 |  | 
|  | 1067 | if (outputs.size() == 0) { | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1068 | return AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1069 | } | 
|  | 1070 | if (outputs.size() == 1) { | 
|  | 1071 | return outputs[0]; | 
|  | 1072 | } | 
|  | 1073 |  | 
|  | 1074 | int maxCommonFlags = 0; | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1075 | audio_io_handle_t outputForFlags = AUDIO_IO_HANDLE_NONE; | 
|  | 1076 | audio_io_handle_t outputForPrimary = AUDIO_IO_HANDLE_NONE; | 
|  | 1077 | audio_io_handle_t outputForFormat = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1078 | audio_format_t bestFormat = AUDIO_FORMAT_INVALID; | 
|  | 1079 | audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1080 |  | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1081 | for (audio_io_handle_t output : outputs) { | 
|  | 1082 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1083 | if (!outputDesc->isDuplicated()) { | 
| Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1084 | // if a valid format is specified, skip output if not compatible | 
|  | 1085 | if (format != AUDIO_FORMAT_INVALID) { | 
|  | 1086 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { | 
| Andy Hung | 5659ed5 | 2018-04-30 10:31:26 -0700 | [diff] [blame] | 1087 | if (format != outputDesc->mFormat) { | 
| Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1088 | continue; | 
|  | 1089 | } | 
|  | 1090 | } else if (!audio_is_linear_pcm(format)) { | 
|  | 1091 | continue; | 
|  | 1092 | } | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1093 | if (AudioPort::isBetterFormatMatch( | 
|  | 1094 | outputDesc->mFormat, bestFormat, format)) { | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1095 | outputForFormat = output; | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1096 | bestFormat = outputDesc->mFormat; | 
|  | 1097 | } | 
| Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1098 | } | 
|  | 1099 |  | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1100 | int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags); | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1101 | if (commonFlags >= maxCommonFlags) { | 
|  | 1102 | if (commonFlags == maxCommonFlags) { | 
| Andy Hung | c990152 | 2017-11-10 20:07:54 -0800 | [diff] [blame] | 1103 | if (format != AUDIO_FORMAT_INVALID | 
|  | 1104 | && AudioPort::isBetterFormatMatch( | 
|  | 1105 | outputDesc->mFormat, bestFormatForFlags, format)) { | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1106 | outputForFlags = output; | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1107 | bestFormatForFlags = outputDesc->mFormat; | 
|  | 1108 | } | 
|  | 1109 | } else { | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1110 | outputForFlags = output; | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1111 | maxCommonFlags = commonFlags; | 
|  | 1112 | bestFormatForFlags = outputDesc->mFormat; | 
|  | 1113 | } | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1114 | ALOGV("selectOutput() commonFlags for output %d, %04x", output, commonFlags); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1115 | } | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1116 | if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1117 | outputForPrimary = output; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1118 | } | 
|  | 1119 | } | 
|  | 1120 | } | 
|  | 1121 |  | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1122 | if (outputForFlags != AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1123 | return outputForFlags; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1124 | } | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1125 | if (outputForFormat != AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1126 | return outputForFormat; | 
|  | 1127 | } | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1128 | if (outputForPrimary != AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1129 | return outputForPrimary; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1130 | } | 
|  | 1131 |  | 
|  | 1132 | return outputs[0]; | 
|  | 1133 | } | 
|  | 1134 |  | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame^] | 1135 | status_t AudioPolicyManager::startOutput(audio_port_handle_t portId) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1136 | { | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame^] | 1137 | ALOGV("%s portId %d", __FUNCTION__, portId); | 
|  | 1138 |  | 
|  | 1139 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); | 
|  | 1140 | if (outputDesc == 0) { | 
|  | 1141 | ALOGW("startOutput() no output for client %d", portId); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1142 | return BAD_VALUE; | 
|  | 1143 | } | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame^] | 1144 | sp<TrackClientDescriptor> client = outputDesc->clients()[portId]; | 
|  | 1145 | audio_stream_type_t stream = client->stream(); | 
|  | 1146 | audio_session_t session = client->session(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1147 |  | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame^] | 1148 | ALOGV("startOutput() output %d, stream %d, session %d", | 
|  | 1149 | outputDesc->mIoHandle, stream, session); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1150 |  | 
| Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1151 | status_t status = outputDesc->start(); | 
|  | 1152 | if (status != NO_ERROR) { | 
|  | 1153 | return status; | 
| Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1154 | } | 
|  | 1155 |  | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1156 | // Routing? | 
|  | 1157 | mOutputRoutes.incRouteActivity(session); | 
|  | 1158 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1159 | audio_devices_t newDevice; | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1160 | AudioMix *policyMix = NULL; | 
|  | 1161 | const char *address = NULL; | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1162 | if (outputDesc->mPolicyMix != NULL) { | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1163 | policyMix = outputDesc->mPolicyMix; | 
|  | 1164 | address = policyMix->mDeviceAddress.string(); | 
|  | 1165 | if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { | 
|  | 1166 | newDevice = policyMix->mDeviceType; | 
|  | 1167 | } else { | 
|  | 1168 | newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; | 
|  | 1169 | } | 
| Eric Laurent | 2157f5b | 2018-04-25 18:33:02 -0700 | [diff] [blame] | 1170 | } else if (mOutputRoutes.getAndClearRouteChanged(session)) { | 
| Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 1171 | newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); | 
| Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 1172 | if (newDevice != outputDesc->device()) { | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame^] | 1173 | checkStrategyRoute(getStrategy(stream), outputDesc->mIoHandle); | 
| Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 1174 | } | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1175 | } else { | 
|  | 1176 | newDevice = AUDIO_DEVICE_NONE; | 
|  | 1177 | } | 
|  | 1178 |  | 
|  | 1179 | uint32_t delayMs = 0; | 
|  | 1180 |  | 
| Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1181 | status = startSource(outputDesc, stream, newDevice, address, &delayMs); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1182 |  | 
|  | 1183 | if (status != NO_ERROR) { | 
|  | 1184 | mOutputRoutes.decRouteActivity(session); | 
| Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1185 | outputDesc->stop(); | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1186 | return status; | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1187 | } | 
|  | 1188 | // Automatically enable the remote submix input when output is started on a re routing mix | 
|  | 1189 | // of type MIX_TYPE_RECORDERS | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1190 | if (audio_is_remote_submix_device(newDevice) && policyMix != NULL && | 
|  | 1191 | policyMix->mMixType == MIX_TYPE_RECORDERS) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1192 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, | 
|  | 1193 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1194 | address, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1195 | "remote-submix"); | 
|  | 1196 | } | 
|  | 1197 |  | 
|  | 1198 | if (delayMs != 0) { | 
|  | 1199 | usleep(delayMs * 1000); | 
|  | 1200 | } | 
|  | 1201 |  | 
|  | 1202 | return status; | 
|  | 1203 | } | 
|  | 1204 |  | 
| Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 1205 | status_t AudioPolicyManager::startSource(const sp<AudioOutputDescriptor>& outputDesc, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1206 | audio_stream_type_t stream, | 
|  | 1207 | audio_devices_t device, | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1208 | const char *address, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1209 | uint32_t *delayMs) | 
|  | 1210 | { | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1211 | // cannot start playback of STREAM_TTS if any other output is being used | 
|  | 1212 | uint32_t beaconMuteLatency = 0; | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1213 |  | 
|  | 1214 | *delayMs = 0; | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1215 | if (stream == AUDIO_STREAM_TTS) { | 
|  | 1216 | ALOGV("\t found BEACON stream"); | 
| Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 1217 | if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) { | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1218 | return INVALID_OPERATION; | 
|  | 1219 | } else { | 
|  | 1220 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); | 
|  | 1221 | } | 
|  | 1222 | } else { | 
|  | 1223 | // some playback other than beacon starts | 
|  | 1224 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); | 
|  | 1225 | } | 
|  | 1226 |  | 
| Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1227 | // force device change if the output is inactive and no audio patch is already present. | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1228 | // check active before incrementing usage count | 
| Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1229 | bool force = !outputDesc->isActive() && | 
|  | 1230 | (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE); | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1231 |  | 
| Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1232 | // requiresMuteCheck is false when we can bypass mute strategy. | 
|  | 1233 | // It covers a common case when there is no materially active audio | 
|  | 1234 | // and muting would result in unnecessary delay and dropped audio. | 
|  | 1235 | const uint32_t outputLatencyMs = outputDesc->latency(); | 
|  | 1236 | bool requiresMuteCheck = outputDesc->isActive(outputLatencyMs * 2);  // account for drain | 
|  | 1237 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1238 | // increment usage count for this stream on the requested output: | 
|  | 1239 | // NOTE that the usage count is the same for duplicated output and hardware output which is | 
|  | 1240 | // necessary for a correct control of hardware output routing by startOutput() and stopOutput() | 
|  | 1241 | outputDesc->changeRefCount(stream, 1); | 
|  | 1242 |  | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1243 | if (stream == AUDIO_STREAM_MUSIC) { | 
|  | 1244 | selectOutputForMusicEffects(); | 
|  | 1245 | } | 
|  | 1246 |  | 
| Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 1247 | if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1248 | // starting an output being rerouted? | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1249 | if (device == AUDIO_DEVICE_NONE) { | 
|  | 1250 | device = getNewOutputDevice(outputDesc, false /*fromCache*/); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1251 | } | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1252 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1253 | routing_strategy strategy = getStrategy(stream); | 
|  | 1254 | bool shouldWait = (strategy == STRATEGY_SONIFICATION) || | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1255 | (strategy == STRATEGY_SONIFICATION_RESPECTFUL) || | 
|  | 1256 | (beaconMuteLatency > 0); | 
|  | 1257 | uint32_t waitMs = beaconMuteLatency; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1258 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1259 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1260 | if (desc != outputDesc) { | 
| Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1261 | // An output has a shared device if | 
|  | 1262 | // - managed by the same hw module | 
|  | 1263 | // - supports the currently selected device | 
|  | 1264 | const bool sharedDevice = outputDesc->sharesHwModuleWith(desc) | 
|  | 1265 | && (desc->supportedDevices() & device) != AUDIO_DEVICE_NONE; | 
|  | 1266 |  | 
| Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1267 | // force a device change if any other output is: | 
|  | 1268 | // - managed by the same hw module | 
| Jean-Michel Trivi | 4a5b481 | 2018-02-08 17:22:32 +0000 | [diff] [blame] | 1269 | // - supports currently selected device | 
| Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1270 | // - has a current device selection that differs from selected device. | 
| Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1271 | // - has an active audio patch | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1272 | // In this case, the audio HAL must receive the new device selection so that it can | 
| Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1273 | // change the device currently selected by the other output. | 
|  | 1274 | if (sharedDevice && | 
| Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1275 | desc->device() != device && | 
| Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1276 | desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1277 | force = true; | 
|  | 1278 | } | 
|  | 1279 | // 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] | 1280 | // a notification so that audio focus effect can propagate, or that a mute/unmute | 
|  | 1281 | // event occurred for beacon | 
| Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1282 | const uint32_t latencyMs = desc->latency(); | 
|  | 1283 | const bool isActive = desc->isActive(latencyMs * 2);  // account for drain | 
|  | 1284 |  | 
|  | 1285 | if (shouldWait && isActive && (waitMs < latencyMs)) { | 
|  | 1286 | waitMs = latencyMs; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1287 | } | 
| Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1288 |  | 
|  | 1289 | // Require mute check if another output is on a shared device | 
|  | 1290 | // and currently active to have proper drain and avoid pops. | 
|  | 1291 | // Note restoring AudioTracks onto this output needs to invoke | 
|  | 1292 | // a volume ramp if there is no mute. | 
|  | 1293 | requiresMuteCheck |= sharedDevice && isActive; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1294 | } | 
|  | 1295 | } | 
| Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1296 |  | 
|  | 1297 | const uint32_t muteWaitMs = | 
|  | 1298 | setOutputDevice(outputDesc, device, force, 0, NULL, address, requiresMuteCheck); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1299 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1300 | // apply volume rules for current stream and device if necessary | 
|  | 1301 | checkAndSetVolume(stream, | 
| Shuhei Miyazaki | 6fe7033 | 2017-07-31 15:21:28 +0900 | [diff] [blame] | 1302 | mVolumeCurves->getVolumeIndex(stream, outputDesc->device()), | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1303 | outputDesc, | 
| Shuhei Miyazaki | 6fe7033 | 2017-07-31 15:21:28 +0900 | [diff] [blame] | 1304 | outputDesc->device()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1305 |  | 
|  | 1306 | // update the outputs if starting an output with a stream that can affect notification | 
|  | 1307 | // routing | 
|  | 1308 | handleNotificationRoutingForStream(stream); | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1309 |  | 
| Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 1310 | // force reevaluating accessibility routing when ringtone or alarm starts | 
|  | 1311 | if (strategy == STRATEGY_SONIFICATION) { | 
|  | 1312 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); | 
|  | 1313 | } | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1314 |  | 
|  | 1315 | if (waitMs > muteWaitMs) { | 
|  | 1316 | *delayMs = waitMs - muteWaitMs; | 
|  | 1317 | } | 
| Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1318 |  | 
|  | 1319 | // FIXME: A device change (muteWaitMs > 0) likely introduces a volume change. | 
|  | 1320 | // A volume change enacted by APM with 0 delay is not synchronous, as it goes | 
|  | 1321 | // via AudioCommandThread to AudioFlinger.  Hence it is possible that the volume | 
|  | 1322 | // change occurs after the MixerThread starts and causes a stream volume | 
|  | 1323 | // glitch. | 
|  | 1324 | // | 
|  | 1325 | // We do not introduce additional delay here. | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1326 | } | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1327 |  | 
| Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 1328 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && | 
|  | 1329 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { | 
|  | 1330 | setStrategyMute(STRATEGY_SONIFICATION, true, outputDesc); | 
|  | 1331 | } | 
|  | 1332 |  | 
| Tomoharu Kasahara | a81f098 | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 1333 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && | 
|  | 1334 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { | 
|  | 1335 | setStrategyMute(STRATEGY_SONIFICATION, true, outputDesc); | 
|  | 1336 | } | 
|  | 1337 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1338 | return NO_ERROR; | 
|  | 1339 | } | 
|  | 1340 |  | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame^] | 1341 | status_t AudioPolicyManager::stopOutput(audio_port_handle_t portId) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1342 | { | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame^] | 1343 | ALOGV("%s portId %d", __FUNCTION__, portId); | 
|  | 1344 |  | 
|  | 1345 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); | 
|  | 1346 | if (outputDesc == 0) { | 
|  | 1347 | ALOGW("stopOutput() no output for client %d", portId); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1348 | return BAD_VALUE; | 
|  | 1349 | } | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame^] | 1350 | sp<TrackClientDescriptor> client = outputDesc->clients()[portId]; | 
|  | 1351 | audio_stream_type_t stream = client->stream(); | 
|  | 1352 | audio_session_t session = client->session(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1353 |  | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame^] | 1354 | ALOGV("stopOutput() output %d, stream %d, session %d", outputDesc->mIoHandle, stream, session); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1355 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1356 | if (outputDesc->mRefCount[stream] == 1) { | 
|  | 1357 | // Automatically disable the remote submix input when output is stopped on a | 
|  | 1358 | // re routing mix of type MIX_TYPE_RECORDERS | 
|  | 1359 | if (audio_is_remote_submix_device(outputDesc->mDevice) && | 
|  | 1360 | outputDesc->mPolicyMix != NULL && | 
|  | 1361 | outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) { | 
|  | 1362 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, | 
|  | 1363 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1364 | outputDesc->mPolicyMix->mDeviceAddress, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1365 | "remote-submix"); | 
|  | 1366 | } | 
|  | 1367 | } | 
|  | 1368 |  | 
|  | 1369 | // Routing? | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1370 | bool forceDeviceUpdate = false; | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1371 | if (outputDesc->mRefCount[stream] > 0) { | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1372 | int activityCount = mOutputRoutes.decRouteActivity(session); | 
|  | 1373 | forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0)); | 
|  | 1374 |  | 
|  | 1375 | if (forceDeviceUpdate) { | 
|  | 1376 | checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE); | 
|  | 1377 | } | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1378 | } | 
|  | 1379 |  | 
| Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1380 | status_t status = stopSource(outputDesc, stream, forceDeviceUpdate); | 
|  | 1381 |  | 
| Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1382 | if (status == NO_ERROR ) { | 
|  | 1383 | outputDesc->stop(); | 
| Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1384 | } | 
|  | 1385 | return status; | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1386 | } | 
|  | 1387 |  | 
| Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 1388 | status_t AudioPolicyManager::stopSource(const sp<AudioOutputDescriptor>& outputDesc, | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1389 | audio_stream_type_t stream, | 
|  | 1390 | bool forceDeviceUpdate) | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1391 | { | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1392 | // always handle stream stop, check which stream type is stopping | 
|  | 1393 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); | 
|  | 1394 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1395 | if (outputDesc->mRefCount[stream] > 0) { | 
|  | 1396 | // decrement usage count of this stream on the output | 
|  | 1397 | outputDesc->changeRefCount(stream, -1); | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1398 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1399 | // store time at which the stream was stopped - see isStreamActive() | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1400 | if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1401 | outputDesc->mStopTime[stream] = systemTime(); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1402 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1403 | // delay the device switch by twice the latency because stopOutput() is executed when | 
|  | 1404 | // the track stop() command is received and at that time the audio track buffer can | 
|  | 1405 | // still contain data that needs to be drained. The latency only covers the audio HAL | 
|  | 1406 | // and kernel buffers. Also the latency does not always include additional delay in the | 
|  | 1407 | // audio path (audio DSP, CODEC ...) | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1408 | setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1409 |  | 
|  | 1410 | // force restoring the device selection on other active outputs if it differs from the | 
|  | 1411 | // one being selected for this output | 
| Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1412 | uint32_t delayMs = outputDesc->latency()*2; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1413 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1414 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1415 | if (desc != outputDesc && | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1416 | desc->isActive() && | 
|  | 1417 | outputDesc->sharesHwModuleWith(desc) && | 
|  | 1418 | (newDevice != desc->device())) { | 
| Haynes Mathew George | 11c499a | 2016-08-26 12:08:25 -0700 | [diff] [blame] | 1419 | audio_devices_t newDevice2 = getNewOutputDevice(desc, false /*fromCache*/); | 
|  | 1420 | bool force = desc->device() != newDevice2; | 
| Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 1421 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1422 | setOutputDevice(desc, | 
| Haynes Mathew George | 11c499a | 2016-08-26 12:08:25 -0700 | [diff] [blame] | 1423 | newDevice2, | 
|  | 1424 | force, | 
| Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1425 | delayMs); | 
|  | 1426 | // re-apply device specific volume if not done by setOutputDevice() | 
|  | 1427 | if (!force) { | 
|  | 1428 | applyStreamVolumes(desc, newDevice2, delayMs); | 
|  | 1429 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1430 | } | 
|  | 1431 | } | 
|  | 1432 | // update the outputs if stopping one with a stream that can affect notification routing | 
|  | 1433 | handleNotificationRoutingForStream(stream); | 
|  | 1434 | } | 
| Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 1435 |  | 
|  | 1436 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && | 
|  | 1437 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { | 
|  | 1438 | setStrategyMute(STRATEGY_SONIFICATION, false, outputDesc); | 
|  | 1439 | } | 
|  | 1440 |  | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1441 | if (stream == AUDIO_STREAM_MUSIC) { | 
|  | 1442 | selectOutputForMusicEffects(); | 
|  | 1443 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1444 | return NO_ERROR; | 
|  | 1445 | } else { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1446 | ALOGW("stopOutput() refcount is already 0"); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1447 | return INVALID_OPERATION; | 
|  | 1448 | } | 
|  | 1449 | } | 
|  | 1450 |  | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame^] | 1451 | void AudioPolicyManager::releaseOutput(audio_port_handle_t portId) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1452 | { | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame^] | 1453 | ALOGV("%s portId %d", __FUNCTION__, portId); | 
|  | 1454 |  | 
|  | 1455 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); | 
|  | 1456 | if (outputDesc == 0) { | 
|  | 1457 | ALOGW("releaseOutput() no output for client %d", portId); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1458 | return; | 
|  | 1459 | } | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame^] | 1460 | sp<TrackClientDescriptor> client = outputDesc->clients()[portId]; | 
|  | 1461 | audio_session_t session = client->session(); | 
|  | 1462 |  | 
|  | 1463 | ALOGV("releaseOutput() %d", outputDesc->mIoHandle); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1464 |  | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1465 | // Routing | 
|  | 1466 | mOutputRoutes.removeRoute(session); | 
|  | 1467 |  | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame^] | 1468 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { | 
|  | 1469 | if (outputDesc->mDirectOpenCount <= 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1470 | ALOGW("releaseOutput() invalid open count %d for output %d", | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame^] | 1471 | outputDesc->mDirectOpenCount, outputDesc->mIoHandle); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1472 | return; | 
|  | 1473 | } | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame^] | 1474 | if (--outputDesc->mDirectOpenCount == 0) { | 
|  | 1475 | closeOutput(outputDesc->mIoHandle); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1476 | mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1477 | } | 
|  | 1478 | } | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame^] | 1479 | outputDesc->clients().erase(portId); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1480 | } | 
|  | 1481 |  | 
|  | 1482 |  | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1483 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, | 
|  | 1484 | audio_io_handle_t *input, | 
|  | 1485 | audio_session_t session, | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1486 | uid_t uid, | 
| Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1487 | const audio_config_base_t *config, | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1488 | audio_input_flags_t flags, | 
| Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1489 | audio_port_handle_t *selectedDeviceId, | 
| Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1490 | input_type_t *inputType, | 
|  | 1491 | audio_port_handle_t *portId) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1492 | { | 
| Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1493 | ALOGV("getInputForAttr() source %d, sampling rate %d, format %#x, channel mask %#x," | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1494 | "session %d, flags %#x", | 
| Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1495 | attr->source, config->sample_rate, config->format, config->channel_mask, session, flags); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1496 |  | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1497 | status_t status = NO_ERROR; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1498 | // handle legacy remote submix case where the address was not always specified | 
|  | 1499 | String8 address = String8(""); | 
| Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1500 | audio_source_t halInputSource; | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1501 | audio_source_t inputSource = attr->source; | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1502 | AudioMix *policyMix = NULL; | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1503 | DeviceVector inputDevices; | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame^] | 1504 | sp<AudioInputDescriptor> inputDesc; | 
|  | 1505 | sp<RecordClientDescriptor> clientDesc; | 
|  | 1506 | audio_port_handle_t requestedDeviceId = *selectedDeviceId; | 
| Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1507 |  | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1508 | if (inputSource == AUDIO_SOURCE_DEFAULT) { | 
|  | 1509 | inputSource = AUDIO_SOURCE_MIC; | 
|  | 1510 | } | 
|  | 1511 |  | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1512 | // Explicit routing? | 
|  | 1513 | sp<DeviceDescriptor> deviceDesc; | 
| Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1514 | if (*selectedDeviceId != AUDIO_PORT_HANDLE_NONE) { | 
| jiabin | c0c831a | 2018-01-29 17:55:15 -0800 | [diff] [blame] | 1515 | deviceDesc = mAvailableInputDevices.getDeviceFromId(*selectedDeviceId); | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1516 | } | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1517 | mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid); | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1518 |  | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1519 | // special case for mmap capture: if an input IO handle is specified, we reuse this input if | 
|  | 1520 | // possible | 
|  | 1521 | if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) == AUDIO_INPUT_FLAG_MMAP_NOIRQ && | 
|  | 1522 | *input != AUDIO_IO_HANDLE_NONE) { | 
|  | 1523 | ssize_t index = mInputs.indexOfKey(*input); | 
|  | 1524 | if (index < 0) { | 
|  | 1525 | ALOGW("getInputForAttr() unknown MMAP input %d", *input); | 
|  | 1526 | status = BAD_VALUE; | 
|  | 1527 | goto error; | 
|  | 1528 | } | 
|  | 1529 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); | 
|  | 1530 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); | 
|  | 1531 | if (audioSession == 0) { | 
|  | 1532 | ALOGW("getInputForAttr() unknown session %d on input %d", session, *input); | 
|  | 1533 | status = BAD_VALUE; | 
|  | 1534 | goto error; | 
|  | 1535 | } | 
|  | 1536 | // For MMAP mode, the first call to getInputForAttr() is made on behalf of audioflinger. | 
|  | 1537 | // The second call is for the first active client and sets the UID. Any further call | 
| Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 1538 | // corresponds to a new client and is only permitted from the same UID. | 
|  | 1539 | // If the first UID is silenced, allow a new UID connection and replace with new UID | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1540 | if (audioSession->openCount() == 1) { | 
|  | 1541 | audioSession->setUid(uid); | 
|  | 1542 | } else if (audioSession->uid() != uid) { | 
| Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 1543 | if (!audioSession->isSilenced()) { | 
|  | 1544 | ALOGW("getInputForAttr() bad uid %d for session %d uid %d", | 
|  | 1545 | uid, session, audioSession->uid()); | 
|  | 1546 | status = INVALID_OPERATION; | 
|  | 1547 | goto error; | 
|  | 1548 | } | 
|  | 1549 | audioSession->setUid(uid); | 
|  | 1550 | audioSession->setSilenced(false); | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1551 | } | 
|  | 1552 | audioSession->changeOpenCount(1); | 
|  | 1553 | *inputType = API_INPUT_LEGACY; | 
|  | 1554 | if (*portId == AUDIO_PORT_HANDLE_NONE) { | 
|  | 1555 | *portId = AudioPort::getNextUniqueId(); | 
|  | 1556 | } | 
| Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 1557 | inputDevices = mAvailableInputDevices.getDevicesFromTypeMask(inputDesc->mDevice); | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1558 | *selectedDeviceId = inputDevices.size() > 0 ? inputDevices.itemAt(0)->getId() | 
|  | 1559 | : AUDIO_PORT_HANDLE_NONE; | 
|  | 1560 | ALOGI("%s reusing MMAP input %d for session %d", __FUNCTION__, *input, session); | 
|  | 1561 |  | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame^] | 1562 | goto exit; | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1563 | } | 
|  | 1564 |  | 
|  | 1565 | *input = AUDIO_IO_HANDLE_NONE; | 
|  | 1566 | *inputType = API_INPUT_INVALID; | 
|  | 1567 |  | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1568 | halInputSource = inputSource; | 
|  | 1569 |  | 
|  | 1570 | // TODO: check for existing client for this port ID | 
|  | 1571 | if (*portId == AUDIO_PORT_HANDLE_NONE) { | 
|  | 1572 | *portId = AudioPort::getNextUniqueId(); | 
|  | 1573 | } | 
|  | 1574 |  | 
|  | 1575 | audio_devices_t device; | 
|  | 1576 |  | 
| Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1577 | if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX && | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1578 | strncmp(attr->tags, "addr=", strlen("addr=")) == 0) { | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1579 | status = mPolicyMixes.getInputMixForAttr(*attr, &policyMix); | 
|  | 1580 | if (status != NO_ERROR) { | 
|  | 1581 | goto error; | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 1582 | } | 
|  | 1583 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1584 | device = AUDIO_DEVICE_IN_REMOTE_SUBMIX; | 
|  | 1585 | address = String8(attr->tags + strlen("addr=")); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1586 | } else { | 
| Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1587 | device = getDeviceAndMixForInputSource(inputSource, &policyMix); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1588 | if (device == AUDIO_DEVICE_NONE) { | 
| Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1589 | ALOGW("getInputForAttr() could not find device for source %d", inputSource); | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1590 | status = BAD_VALUE; | 
|  | 1591 | goto error; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1592 | } | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1593 | if (policyMix != NULL) { | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1594 | address = policyMix->mDeviceAddress; | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1595 | if (policyMix->mMixType == MIX_TYPE_RECORDERS) { | 
|  | 1596 | // there is an external policy, but this input is attached to a mix of recorders, | 
|  | 1597 | // meaning it receives audio injected into the framework, so the recorder doesn't | 
|  | 1598 | // know about it and is therefore considered "legacy" | 
|  | 1599 | *inputType = API_INPUT_LEGACY; | 
|  | 1600 | } else { | 
|  | 1601 | // recording a mix of players defined by an external policy, we're rerouting for | 
|  | 1602 | // an external policy | 
|  | 1603 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; | 
|  | 1604 | } | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1605 | } else if (audio_is_remote_submix_device(device)) { | 
|  | 1606 | address = String8("0"); | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1607 | *inputType = API_INPUT_MIX_CAPTURE; | 
| Eric Laurent | 82db269 | 2015-08-07 13:59:42 -0700 | [diff] [blame] | 1608 | } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) { | 
|  | 1609 | *inputType = API_INPUT_TELEPHONY_RX; | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1610 | } else { | 
|  | 1611 | *inputType = API_INPUT_LEGACY; | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1612 | } | 
| Ravi Kumar Alamanda | b367f5b | 2015-08-25 08:21:37 -0700 | [diff] [blame] | 1613 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1614 | } | 
|  | 1615 |  | 
|  | 1616 | *input = getInputForDevice(device, address, session, uid, inputSource, | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1617 | config, flags, | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1618 | policyMix); | 
|  | 1619 | if (*input == AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1620 | status = INVALID_OPERATION; | 
|  | 1621 | goto error; | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1622 | } | 
| Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1623 |  | 
| Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 1624 | inputDevices = mAvailableInputDevices.getDevicesFromTypeMask(device); | 
| Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1625 | *selectedDeviceId = inputDevices.size() > 0 ? inputDevices.itemAt(0)->getId() | 
|  | 1626 | : AUDIO_PORT_HANDLE_NONE; | 
|  | 1627 |  | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame^] | 1628 | exit: | 
|  | 1629 | clientDesc = new RecordClientDescriptor(*portId, uid, session, | 
|  | 1630 | *attr, *config, requestedDeviceId, inputSource, flags); | 
|  | 1631 | inputDesc = mInputs.valueFor(*input); | 
|  | 1632 | inputDesc->clients().emplace(*portId, clientDesc); | 
|  | 1633 |  | 
|  | 1634 | ALOGV("getInputForAttr() returns input %d type %d selectedDeviceId %d for port ID %d", | 
|  | 1635 | *input, *inputType, *selectedDeviceId, *portId); | 
| Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1636 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1637 | return NO_ERROR; | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1638 |  | 
|  | 1639 | error: | 
|  | 1640 | mInputRoutes.removeRoute(session); | 
|  | 1641 | return status; | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1642 | } | 
|  | 1643 |  | 
|  | 1644 |  | 
|  | 1645 | audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device, | 
|  | 1646 | String8 address, | 
|  | 1647 | audio_session_t session, | 
|  | 1648 | uid_t uid, | 
|  | 1649 | audio_source_t inputSource, | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1650 | const audio_config_base_t *config, | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1651 | audio_input_flags_t flags, | 
|  | 1652 | AudioMix *policyMix) | 
|  | 1653 | { | 
|  | 1654 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; | 
|  | 1655 | audio_source_t halInputSource = inputSource; | 
|  | 1656 | bool isSoundTrigger = false; | 
|  | 1657 |  | 
|  | 1658 | if (inputSource == AUDIO_SOURCE_HOTWORD) { | 
|  | 1659 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); | 
|  | 1660 | if (index >= 0) { | 
|  | 1661 | input = mSoundTriggerSessions.valueFor(session); | 
|  | 1662 | isSoundTrigger = true; | 
|  | 1663 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); | 
|  | 1664 | ALOGV("SoundTrigger capture on session %d input %d", session, input); | 
|  | 1665 | } else { | 
|  | 1666 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; | 
| Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1667 | } | 
| Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame] | 1668 | } else if (inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION && | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1669 | audio_is_linear_pcm(config->format)) { | 
| Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame] | 1670 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_VOIP_TX); | 
| Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1671 | } | 
|  | 1672 |  | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1673 | // find a compatible input profile (not necessarily identical in parameters) | 
|  | 1674 | sp<IOProfile> profile; | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1675 | // sampling rate and flags may be updated by getInputProfile | 
|  | 1676 | uint32_t profileSamplingRate = (config->sample_rate == 0) ? | 
|  | 1677 | SAMPLE_RATE_HZ_DEFAULT : config->sample_rate; | 
| Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 1678 | audio_format_t profileFormat; | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1679 | audio_channel_mask_t profileChannelMask = config->channel_mask; | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1680 | audio_input_flags_t profileFlags = flags; | 
|  | 1681 | for (;;) { | 
| Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 1682 | profileFormat = config->format; // reset each time through loop, in case it is updated | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1683 | profile = getInputProfile(device, address, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1684 | profileSamplingRate, profileFormat, profileChannelMask, | 
|  | 1685 | profileFlags); | 
|  | 1686 | if (profile != 0) { | 
|  | 1687 | break; // success | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1688 | } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) { | 
|  | 1689 | profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1690 | } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) { | 
|  | 1691 | profileFlags = AUDIO_INPUT_FLAG_NONE; // retry | 
|  | 1692 | } else { // fail | 
| Glenn Kasten | faa10a6 | 2017-05-25 15:52:05 -0700 | [diff] [blame] | 1693 | ALOGW("getInputForDevice() could not find profile for device 0x%X, " | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1694 | "sampling rate %u, format %#x, channel mask 0x%X, flags %#x", | 
|  | 1695 | device, config->sample_rate, config->format, config->channel_mask, flags); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1696 | return input; | 
| Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1697 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1698 | } | 
| Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1699 | // Pick input sampling rate if not specified by client | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1700 | uint32_t samplingRate = config->sample_rate; | 
| Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1701 | if (samplingRate == 0) { | 
|  | 1702 | samplingRate = profileSamplingRate; | 
|  | 1703 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1704 |  | 
| Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1705 | if (profile->getModuleHandle() == 0) { | 
|  | 1706 | ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName()); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1707 | return input; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1708 | } | 
|  | 1709 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1710 | sp<AudioSession> audioSession = new AudioSession(session, | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1711 | inputSource, | 
|  | 1712 | config->format, | 
|  | 1713 | samplingRate, | 
|  | 1714 | config->channel_mask, | 
|  | 1715 | flags, | 
|  | 1716 | uid, | 
| Eric Laurent | a18dced | 2018-07-20 15:14:54 -0700 | [diff] [blame] | 1717 | isSoundTrigger); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1718 |  | 
| Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1719 | if (!profile->canOpenNewIo()) { | 
|  | 1720 | return AUDIO_IO_HANDLE_NONE; | 
|  | 1721 | } | 
|  | 1722 |  | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1723 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile, mpClientInterface); | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1724 |  | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1725 | audio_config_t lConfig = AUDIO_CONFIG_INITIALIZER; | 
|  | 1726 | lConfig.sample_rate = profileSamplingRate; | 
|  | 1727 | lConfig.channel_mask = profileChannelMask; | 
|  | 1728 | lConfig.format = profileFormat; | 
| Eric Laurent | e301410 | 2017-05-03 11:15:43 -0700 | [diff] [blame] | 1729 |  | 
| Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 1730 | if (address == "") { | 
| Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 1731 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromTypeMask(device); | 
| Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 1732 | // the inputs vector must be of size >= 1, but we don't want to crash here | 
|  | 1733 | address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress : String8(""); | 
|  | 1734 | } | 
|  | 1735 |  | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1736 | status_t status = inputDesc->open(&lConfig, device, address, | 
|  | 1737 | halInputSource, profileFlags, &input); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1738 |  | 
|  | 1739 | // only accept input with the exact requested set of parameters | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1740 | if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE || | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1741 | (profileSamplingRate != lConfig.sample_rate) || | 
|  | 1742 | !audio_formats_match(profileFormat, lConfig.format) || | 
|  | 1743 | (profileChannelMask != lConfig.channel_mask)) { | 
|  | 1744 | ALOGW("getInputForAttr() failed opening input: sampling rate %d" | 
| Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1745 | ", format %#x, channel mask %#x", | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1746 | profileSamplingRate, profileFormat, profileChannelMask); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1747 | if (input != AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1748 | inputDesc->close(); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1749 | } | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1750 | return AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1751 | } | 
|  | 1752 |  | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1753 | inputDesc->mPolicyMix = policyMix; | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1754 | inputDesc->addAudioSession(session, audioSession); | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1755 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1756 | addInput(input, inputDesc); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1757 | mpClientInterface->onAudioPortListUpdate(); | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1758 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1759 | return input; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1760 | } | 
|  | 1761 |  | 
| Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1762 | //static | 
|  | 1763 | bool AudioPolicyManager::isConcurrentSource(audio_source_t source) | 
|  | 1764 | { | 
|  | 1765 | return (source == AUDIO_SOURCE_HOTWORD) || | 
|  | 1766 | (source == AUDIO_SOURCE_VOICE_RECOGNITION) || | 
|  | 1767 | (source == AUDIO_SOURCE_FM_TUNER); | 
|  | 1768 | } | 
|  | 1769 |  | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1770 | bool AudioPolicyManager::isConcurentCaptureAllowed(const sp<AudioInputDescriptor>& inputDesc, | 
|  | 1771 | const sp<AudioSession>& audioSession) | 
|  | 1772 | { | 
|  | 1773 | // Do not allow capture if an active voice call is using a software patch and | 
|  | 1774 | // the call TX source device is on the same HW module. | 
|  | 1775 | // FIXME: would be better to refine to only inputs whose profile connects to the | 
|  | 1776 | // call TX device but this information is not in the audio patch | 
|  | 1777 | if (mCallTxPatch != 0 && | 
|  | 1778 | inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) { | 
|  | 1779 | return false; | 
|  | 1780 | } | 
|  | 1781 |  | 
|  | 1782 | // starting concurrent capture is enabled if: | 
|  | 1783 | // 1) capturing for re-routing | 
|  | 1784 | // 2) capturing for HOTWORD source | 
|  | 1785 | // 3) capturing for FM TUNER source | 
|  | 1786 | // 3) All other active captures are either for re-routing or HOTWORD | 
|  | 1787 |  | 
|  | 1788 | if (is_virtual_input_device(inputDesc->mDevice) || | 
| Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1789 | isConcurrentSource(audioSession->inputSource())) { | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1790 | return true; | 
|  | 1791 | } | 
|  | 1792 |  | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1793 | for (const auto& activeInput : mInputs.getActiveInputs()) { | 
| Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1794 | if (!isConcurrentSource(activeInput->inputSource(true)) && | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1795 | !is_virtual_input_device(activeInput->mDevice)) { | 
|  | 1796 | return false; | 
|  | 1797 | } | 
|  | 1798 | } | 
|  | 1799 |  | 
|  | 1800 | return true; | 
|  | 1801 | } | 
|  | 1802 |  | 
| Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 1803 | // FIXME: remove when concurrent capture is ready. This is a hack to work around bug b/63083537. | 
|  | 1804 | bool AudioPolicyManager::soundTriggerSupportsConcurrentCapture() { | 
|  | 1805 | if (!mHasComputedSoundTriggerSupportsConcurrentCapture) { | 
|  | 1806 | bool soundTriggerSupportsConcurrentCapture = false; | 
|  | 1807 | unsigned int numModules = 0; | 
|  | 1808 | struct sound_trigger_module_descriptor* nModules = NULL; | 
|  | 1809 |  | 
|  | 1810 | status_t status = SoundTrigger::listModules(nModules, &numModules); | 
|  | 1811 | if (status == NO_ERROR && numModules != 0) { | 
|  | 1812 | nModules = (struct sound_trigger_module_descriptor*) calloc( | 
|  | 1813 | numModules, sizeof(struct sound_trigger_module_descriptor)); | 
|  | 1814 | if (nModules == NULL) { | 
|  | 1815 | // We failed to malloc the buffer, so just say no for now, and hope that we have more | 
|  | 1816 | // ram the next time this function is called. | 
|  | 1817 | ALOGE("Failed to allocate buffer for module descriptors"); | 
|  | 1818 | return false; | 
|  | 1819 | } | 
|  | 1820 |  | 
|  | 1821 | status = SoundTrigger::listModules(nModules, &numModules); | 
|  | 1822 | if (status == NO_ERROR) { | 
|  | 1823 | soundTriggerSupportsConcurrentCapture = true; | 
|  | 1824 | for (size_t i = 0; i < numModules; ++i) { | 
|  | 1825 | soundTriggerSupportsConcurrentCapture &= | 
|  | 1826 | nModules[i].properties.concurrent_capture; | 
|  | 1827 | } | 
|  | 1828 | } | 
|  | 1829 | free(nModules); | 
|  | 1830 | } | 
|  | 1831 | mSoundTriggerSupportsConcurrentCapture = soundTriggerSupportsConcurrentCapture; | 
|  | 1832 | mHasComputedSoundTriggerSupportsConcurrentCapture = true; | 
|  | 1833 | } | 
|  | 1834 | return mSoundTriggerSupportsConcurrentCapture; | 
|  | 1835 | } | 
|  | 1836 |  | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1837 |  | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame^] | 1838 | status_t AudioPolicyManager::startInput(audio_port_handle_t portId, | 
| Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1839 | bool silenced, | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1840 | concurrency_type__mask_t *concurrency) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1841 | { | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame^] | 1842 | *concurrency = API_INPUT_CONCURRENCY_NONE; | 
|  | 1843 |  | 
|  | 1844 | ALOGV("%s portId %d", __FUNCTION__, portId); | 
|  | 1845 |  | 
|  | 1846 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); | 
|  | 1847 | if (inputDesc == 0) { | 
|  | 1848 | ALOGW("startInput() no input for client %d", portId); | 
|  | 1849 | return BAD_VALUE; | 
|  | 1850 | } | 
|  | 1851 | sp<RecordClientDescriptor> client = inputDesc->clients()[portId]; | 
|  | 1852 | audio_session_t session = client->session(); | 
|  | 1853 | audio_io_handle_t input = inputDesc->mIoHandle; | 
| Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1854 |  | 
|  | 1855 | ALOGV("AudioPolicyManager::startInput(input:%d, session:%d, silenced:%d, concurrency:%d)", | 
|  | 1856 | input, session, silenced, *concurrency); | 
|  | 1857 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1858 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); | 
|  | 1859 | if (audioSession == 0) { | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1860 | ALOGW("startInput() unknown session %d on input %d", session, input); | 
|  | 1861 | return BAD_VALUE; | 
|  | 1862 | } | 
|  | 1863 |  | 
| Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1864 | if (!is_virtual_input_device(inputDesc->mDevice)) { | 
|  | 1865 | if (mCallTxPatch != 0 && | 
|  | 1866 | inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) { | 
|  | 1867 | ALOGW("startInput(%d) failed: call in progress", input); | 
| Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 1868 | *concurrency |= API_INPUT_CONCURRENCY_CALL; | 
| Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1869 | return INVALID_OPERATION; | 
|  | 1870 | } | 
|  | 1871 |  | 
| Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1872 | Vector<sp<AudioInputDescriptor>> activeInputs = mInputs.getActiveInputs(); | 
| Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1873 |  | 
| Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1874 | // If a UID is idle and records silence and another not silenced recording starts | 
|  | 1875 | // from another UID (idle or active) we stop the current idle UID recording in | 
|  | 1876 | // favor of the new one - "There can be only one" TM | 
|  | 1877 | if (!silenced) { | 
|  | 1878 | for (const auto& activeDesc : activeInputs) { | 
|  | 1879 | if ((audioSession->flags() & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0 && | 
|  | 1880 | activeDesc->getId() == inputDesc->getId()) { | 
|  | 1881 | continue; | 
|  | 1882 | } | 
|  | 1883 |  | 
|  | 1884 | AudioSessionCollection activeSessions = activeDesc->getAudioSessions( | 
|  | 1885 | true /*activeOnly*/); | 
|  | 1886 | sp<AudioSession> activeSession = activeSessions.valueAt(0); | 
|  | 1887 | if (activeSession->isSilenced()) { | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame^] | 1888 | closeSession(activeDesc, activeSession); | 
|  | 1889 | ALOGV("startInput() session %d stopping silenced session %d", session, activeSession->session()); | 
| Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1890 | activeInputs = mInputs.getActiveInputs(); | 
|  | 1891 | } | 
|  | 1892 | } | 
|  | 1893 | } | 
|  | 1894 |  | 
|  | 1895 | for (const auto& activeDesc : activeInputs) { | 
| Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 1896 | if ((audioSession->flags() & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0 && | 
|  | 1897 | activeDesc->getId() == inputDesc->getId()) { | 
|  | 1898 | continue; | 
|  | 1899 | } | 
|  | 1900 |  | 
| Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1901 | audio_source_t activeSource = activeDesc->inputSource(true); | 
|  | 1902 | if (audioSession->inputSource() == AUDIO_SOURCE_HOTWORD) { | 
|  | 1903 | if (activeSource == AUDIO_SOURCE_HOTWORD) { | 
|  | 1904 | if (activeDesc->hasPreemptedSession(session)) { | 
|  | 1905 | ALOGW("startInput(%d) failed for HOTWORD: " | 
|  | 1906 | "other input %d already started for HOTWORD", | 
|  | 1907 | input, activeDesc->mIoHandle); | 
| Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 1908 | *concurrency |= API_INPUT_CONCURRENCY_HOTWORD; | 
| Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1909 | return INVALID_OPERATION; | 
|  | 1910 | } | 
|  | 1911 | } else { | 
|  | 1912 | ALOGV("startInput(%d) failed for HOTWORD: other input %d already started", | 
|  | 1913 | input, activeDesc->mIoHandle); | 
| Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 1914 | *concurrency |= API_INPUT_CONCURRENCY_CAPTURE; | 
| Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1915 | return INVALID_OPERATION; | 
|  | 1916 | } | 
|  | 1917 | } else { | 
|  | 1918 | if (activeSource != AUDIO_SOURCE_HOTWORD) { | 
|  | 1919 | ALOGW("startInput(%d) failed: other input %d already started", | 
|  | 1920 | input, activeDesc->mIoHandle); | 
| Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 1921 | *concurrency |= API_INPUT_CONCURRENCY_CAPTURE; | 
| Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1922 | return INVALID_OPERATION; | 
|  | 1923 | } | 
|  | 1924 | } | 
|  | 1925 | } | 
|  | 1926 |  | 
| Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 1927 | // We only need to check if the sound trigger session supports concurrent capture if the | 
|  | 1928 | // input is also a sound trigger input. Otherwise, we should preempt any hotword stream | 
|  | 1929 | // that's running. | 
|  | 1930 | const bool allowConcurrentWithSoundTrigger = | 
|  | 1931 | inputDesc->isSoundTrigger() ? soundTriggerSupportsConcurrentCapture() : false; | 
|  | 1932 |  | 
| Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1933 | // if capture is allowed, preempt currently active HOTWORD captures | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1934 | for (const auto& activeDesc : activeInputs) { | 
| Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 1935 | if (allowConcurrentWithSoundTrigger && activeDesc->isSoundTrigger()) { | 
|  | 1936 | continue; | 
|  | 1937 | } | 
|  | 1938 |  | 
| Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1939 | audio_source_t activeSource = activeDesc->inputSource(true); | 
|  | 1940 | if (activeSource == AUDIO_SOURCE_HOTWORD) { | 
|  | 1941 | AudioSessionCollection activeSessions = | 
|  | 1942 | activeDesc->getAudioSessions(true /*activeOnly*/); | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame^] | 1943 | sp<AudioSession> activeSession = activeSessions[0]; | 
| Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1944 | SortedVector<audio_session_t> sessions = activeDesc->getPreemptedSessions(); | 
| Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 1945 | *concurrency |= API_INPUT_CONCURRENCY_PREEMPT; | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame^] | 1946 | sessions.add(activeSession->session()); | 
| Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1947 | inputDesc->setPreemptedSessions(sessions); | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame^] | 1948 | closeSession(inputDesc, activeSession); | 
| Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1949 | ALOGV("startInput(%d) for HOTWORD preempting HOTWORD input %d", | 
|  | 1950 | input, activeDesc->mIoHandle); | 
|  | 1951 | } | 
|  | 1952 | } | 
|  | 1953 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1954 |  | 
| Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1955 | // Make sure we start with the correct silence state | 
|  | 1956 | audioSession->setSilenced(silenced); | 
|  | 1957 |  | 
| Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 1958 | // increment activity count before calling getNewInputDevice() below as only active sessions | 
|  | 1959 | // are considered for device selection | 
| Eric Laurent | a18dced | 2018-07-20 15:14:54 -0700 | [diff] [blame] | 1960 | inputDesc->changeRefCount(session, 1); | 
| Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 1961 |  | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1962 | // Routing? | 
|  | 1963 | mInputRoutes.incRouteActivity(session); | 
|  | 1964 |  | 
| Eric Laurent | 2157f5b | 2018-04-25 18:33:02 -0700 | [diff] [blame] | 1965 | if (audioSession->activeCount() == 1 || mInputRoutes.getAndClearRouteChanged(session)) { | 
| Eric Laurent | 271a93e | 2016-09-29 14:47:06 -0700 | [diff] [blame] | 1966 | // indicate active capture to sound trigger service if starting capture from a mic on | 
|  | 1967 | // primary HW module | 
| Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 1968 | audio_devices_t device = getNewInputDevice(inputDesc); | 
| Eric Laurent | 271a93e | 2016-09-29 14:47:06 -0700 | [diff] [blame] | 1969 | setInputDevice(input, device, true /* force */); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1970 |  | 
| Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1971 | status_t status = inputDesc->start(); | 
|  | 1972 | if (status != NO_ERROR) { | 
|  | 1973 | mInputRoutes.decRouteActivity(session); | 
| Eric Laurent | a18dced | 2018-07-20 15:14:54 -0700 | [diff] [blame] | 1974 | inputDesc->changeRefCount(session, -1); | 
| Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1975 | return status; | 
|  | 1976 | } | 
| Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1977 |  | 
| Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1978 | if (inputDesc->getAudioSessionCount(true/*activeOnly*/) == 1) { | 
| Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 1979 | // if input maps to a dynamic policy with an activity listener, notify of state change | 
|  | 1980 | if ((inputDesc->mPolicyMix != NULL) | 
|  | 1981 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { | 
|  | 1982 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, | 
|  | 1983 | MIX_STATE_MIXING); | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1984 | } | 
| Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 1985 |  | 
| Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 1986 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); | 
|  | 1987 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && | 
| Eric Laurent | 05b345e | 2016-11-18 12:36:27 -0800 | [diff] [blame] | 1988 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) { | 
| Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 1989 | SoundTrigger::setCaptureState(true); | 
|  | 1990 | } | 
|  | 1991 |  | 
|  | 1992 | // automatically enable the remote submix output when input is started if not | 
|  | 1993 | // used by a policy mix of type MIX_TYPE_RECORDERS | 
|  | 1994 | // For remote submix (a virtual device), we open only one input per capture request. | 
|  | 1995 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { | 
|  | 1996 | String8 address = String8(""); | 
|  | 1997 | if (inputDesc->mPolicyMix == NULL) { | 
|  | 1998 | address = String8("0"); | 
|  | 1999 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { | 
|  | 2000 | address = inputDesc->mPolicyMix->mDeviceAddress; | 
|  | 2001 | } | 
|  | 2002 | if (address != "") { | 
|  | 2003 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, | 
|  | 2004 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, | 
|  | 2005 | address, "remote-submix"); | 
|  | 2006 | } | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2007 | } | 
| Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 2008 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2009 | } | 
|  | 2010 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2011 | ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2012 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2013 | return NO_ERROR; | 
|  | 2014 | } | 
|  | 2015 |  | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame^] | 2016 | status_t AudioPolicyManager::stopInput(audio_port_handle_t portId) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2017 | { | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame^] | 2018 | ALOGV("%s portId %d", __FUNCTION__, portId); | 
|  | 2019 |  | 
|  | 2020 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); | 
|  | 2021 | if (inputDesc == 0) { | 
|  | 2022 | ALOGW("stopInput() no input for client %d", portId); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2023 | return BAD_VALUE; | 
|  | 2024 | } | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame^] | 2025 | sp<RecordClientDescriptor> client = inputDesc->clients()[portId]; | 
|  | 2026 | audio_session_t session = client->session(); | 
|  | 2027 | audio_io_handle_t input = inputDesc->mIoHandle; | 
|  | 2028 |  | 
|  | 2029 | ALOGV("stopInput() input %d", input); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2030 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2031 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2032 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2033 | if (audioSession->activeCount() == 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2034 | ALOGW("stopInput() input %d already stopped", input); | 
|  | 2035 | return INVALID_OPERATION; | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2036 | } | 
|  | 2037 |  | 
| Eric Laurent | a18dced | 2018-07-20 15:14:54 -0700 | [diff] [blame] | 2038 | inputDesc->changeRefCount(session, -1); | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2039 |  | 
|  | 2040 | // Routing? | 
|  | 2041 | mInputRoutes.decRouteActivity(session); | 
|  | 2042 |  | 
| Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2043 | if (audioSession->activeCount() == 0) { | 
| Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2044 | inputDesc->stop(); | 
| Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2045 | if (inputDesc->isActive()) { | 
|  | 2046 | setInputDevice(input, getNewInputDevice(inputDesc), false /* force */); | 
|  | 2047 | } else { | 
|  | 2048 | // if input maps to a dynamic policy with an activity listener, notify of state change | 
|  | 2049 | if ((inputDesc->mPolicyMix != NULL) | 
|  | 2050 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { | 
|  | 2051 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, | 
|  | 2052 | MIX_STATE_IDLE); | 
| Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2053 | } | 
| Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2054 |  | 
|  | 2055 | // automatically disable the remote submix output when input is stopped if not | 
|  | 2056 | // used by a policy mix of type MIX_TYPE_RECORDERS | 
|  | 2057 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { | 
|  | 2058 | String8 address = String8(""); | 
|  | 2059 | if (inputDesc->mPolicyMix == NULL) { | 
|  | 2060 | address = String8("0"); | 
|  | 2061 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { | 
|  | 2062 | address = inputDesc->mPolicyMix->mDeviceAddress; | 
|  | 2063 | } | 
|  | 2064 | if (address != "") { | 
|  | 2065 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, | 
|  | 2066 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, | 
|  | 2067 | address, "remote-submix"); | 
|  | 2068 | } | 
| Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2069 | } | 
| Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2070 |  | 
| Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 2071 | audio_devices_t device = inputDesc->mDevice; | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 2072 | resetInputDevice(input); | 
| Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2073 |  | 
| Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 2074 | // indicate inactive capture to sound trigger service if stopping capture from a mic on | 
|  | 2075 | // primary HW module | 
|  | 2076 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); | 
|  | 2077 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && | 
|  | 2078 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 2079 | SoundTrigger::setCaptureState(false); | 
|  | 2080 | } | 
|  | 2081 | inputDesc->clearPreemptedSessions(); | 
| Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2082 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2083 | } | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2084 | return NO_ERROR; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2085 | } | 
|  | 2086 |  | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame^] | 2087 | void AudioPolicyManager::releaseInput(audio_port_handle_t portId) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2088 | { | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame^] | 2089 | ALOGV("%s portId %d", __FUNCTION__, portId); | 
|  | 2090 |  | 
|  | 2091 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); | 
|  | 2092 | if (inputDesc == 0) { | 
|  | 2093 | ALOGW("releaseInput() no input for client %d", portId); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2094 | return; | 
|  | 2095 | } | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame^] | 2096 | sp<RecordClientDescriptor> client = inputDesc->clients()[portId]; | 
|  | 2097 | audio_session_t session = client->session(); | 
|  | 2098 | audio_io_handle_t input = inputDesc->mIoHandle; | 
|  | 2099 |  | 
|  | 2100 | ALOGV("releaseInput() %d", input); | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2101 |  | 
|  | 2102 | // Routing | 
|  | 2103 | mInputRoutes.removeRoute(session); | 
|  | 2104 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2105 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); | 
| vivek mehta | 587b8df | 2017-01-31 14:58:44 -0800 | [diff] [blame] | 2106 | if (audioSession == 0) { | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2107 | ALOGW("releaseInput() unknown session %d on input %d", session, input); | 
|  | 2108 | return; | 
|  | 2109 | } | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2110 |  | 
|  | 2111 | if (audioSession->openCount() == 0) { | 
|  | 2112 | ALOGW("releaseInput() invalid open count %d on session %d", | 
|  | 2113 | audioSession->openCount(), session); | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2114 | return; | 
|  | 2115 | } | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2116 |  | 
|  | 2117 | if (audioSession->changeOpenCount(-1) == 0) { | 
|  | 2118 | inputDesc->removeAudioSession(session); | 
|  | 2119 | } | 
|  | 2120 |  | 
| Jean-Michel Trivi | 65bfe91 | 2015-12-04 15:56:47 -0800 | [diff] [blame] | 2121 | if (inputDesc->getOpenRefCount() > 0) { | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2122 | ALOGV("releaseInput() exit > 0"); | 
|  | 2123 | return; | 
|  | 2124 | } | 
|  | 2125 |  | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2126 | closeInput(input); | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame^] | 2127 | inputDesc->clients().erase(portId); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2128 | mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2129 | ALOGV("releaseInput() exit"); | 
|  | 2130 | } | 
|  | 2131 |  | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame^] | 2132 | void AudioPolicyManager::closeSessions(const sp<AudioInputDescriptor>& input, bool activeOnly) | 
|  | 2133 | { | 
|  | 2134 | AudioSessionCollection sessions = input->getAudioSessions(activeOnly /*activeOnly*/); | 
|  | 2135 | for (size_t i = 0; i < sessions.size(); i++) { | 
|  | 2136 | closeSession(input, sessions[i]); | 
|  | 2137 | } | 
|  | 2138 | } | 
|  | 2139 |  | 
|  | 2140 | void AudioPolicyManager::closeSession(const sp<AudioInputDescriptor>& input, | 
|  | 2141 | const sp<AudioSession>& session) | 
|  | 2142 | { | 
|  | 2143 | RecordClientVector clients = input->getClientsForSession(session->session()); | 
|  | 2144 |  | 
|  | 2145 | for (const auto& client : clients) { | 
|  | 2146 | stopInput(client->portId()); | 
|  | 2147 | releaseInput(client->portId()); | 
|  | 2148 | } | 
|  | 2149 | } | 
|  | 2150 |  | 
|  | 2151 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2152 | void AudioPolicyManager::closeAllInputs() { | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2153 | bool patchRemoved = false; | 
|  | 2154 |  | 
| Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 2155 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2156 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index); | 
| Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 2157 | ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2158 | if (patch_index >= 0) { | 
|  | 2159 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index); | 
| Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 2160 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2161 | mAudioPatches.removeItemsAt(patch_index); | 
|  | 2162 | patchRemoved = true; | 
|  | 2163 | } | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2164 | inputDesc->close(); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2165 | } | 
| jiabin | 829a00b | 2018-04-04 16:28:32 -0700 | [diff] [blame] | 2166 | mInputRoutes.clear(); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2167 | mInputs.clear(); | 
| Chris Thornton | 52785f3 | 2016-02-09 17:28:49 -0800 | [diff] [blame] | 2168 | SoundTrigger::setCaptureState(false); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2169 | nextAudioPortGeneration(); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2170 |  | 
|  | 2171 | if (patchRemoved) { | 
|  | 2172 | mpClientInterface->onAudioPatchListUpdate(); | 
|  | 2173 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2174 | } | 
|  | 2175 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2176 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2177 | int indexMin, | 
|  | 2178 | int indexMax) | 
|  | 2179 | { | 
|  | 2180 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2181 | mVolumeCurves->initStreamVolume(stream, indexMin, indexMax); | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2182 |  | 
|  | 2183 | // initialize other private stream volumes which follow this one | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2184 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { | 
|  | 2185 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2186 | continue; | 
|  | 2187 | } | 
|  | 2188 | mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax); | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2189 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2190 | } | 
|  | 2191 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2192 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2193 | int index, | 
|  | 2194 | audio_devices_t device) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2195 | { | 
|  | 2196 |  | 
| Nadav Bar | 7c605f6 | 2017-12-25 00:01:52 +0200 | [diff] [blame] | 2197 | // VOICE_CALL stream has minVolumeIndex > 0  but can be muted directly by an | 
|  | 2198 | // app that has MODIFY_PHONE_STATE permission. | 
|  | 2199 | if (((index < mVolumeCurves->getVolumeIndexMin(stream)) && | 
|  | 2200 | !(stream == AUDIO_STREAM_VOICE_CALL && index == 0)) || | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2201 | (index > mVolumeCurves->getVolumeIndexMax(stream))) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2202 | return BAD_VALUE; | 
|  | 2203 | } | 
|  | 2204 | if (!audio_is_output_device(device)) { | 
|  | 2205 | return BAD_VALUE; | 
|  | 2206 | } | 
|  | 2207 |  | 
|  | 2208 | // Force max volume if stream cannot be muted | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2209 | if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2210 |  | 
| Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2211 | ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d", | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2212 | stream, device, index); | 
|  | 2213 |  | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2214 | // update other private stream volumes which follow this one | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2215 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { | 
|  | 2216 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2217 | continue; | 
|  | 2218 | } | 
|  | 2219 | mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index); | 
|  | 2220 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2221 |  | 
| Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2222 | // update volume on all outputs and streams matching the following: | 
|  | 2223 | // - The requested stream (or a stream matching for volume control) is active on the output | 
|  | 2224 | // - The device (or devices) selected by the strategy corresponding to this stream includes | 
|  | 2225 | // the requested device | 
|  | 2226 | // - For non default requested device, currently selected device on the output is either the | 
|  | 2227 | // requested device or one of the devices selected by the strategy | 
| Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2228 | // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if | 
|  | 2229 | // no specific device volume value exists for currently selected device. | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2230 | status_t status = NO_ERROR; | 
|  | 2231 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2232 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
|  | 2233 | audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device()); | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2234 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { | 
| Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 2235 | if (!(streamsMatchForvolume(stream, (audio_stream_type_t)curStream))) { | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2236 | continue; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2237 | } | 
| Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 2238 | if (!(desc->isStreamActive((audio_stream_type_t)curStream) || isInCall())) { | 
| Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2239 | continue; | 
|  | 2240 | } | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2241 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); | 
| Jean-Michel Trivi | b7fdce6 | 2017-06-27 19:38:42 -0700 | [diff] [blame] | 2242 | audio_devices_t curStreamDevice = Volume::getDeviceForVolume(getDeviceForStrategy( | 
|  | 2243 | curStrategy, false /*fromCache*/)); | 
| Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2244 | if ((device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) && | 
|  | 2245 | ((curStreamDevice & device) == 0)) { | 
| Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2246 | continue; | 
|  | 2247 | } | 
| Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2248 | bool applyVolume; | 
| Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2249 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { | 
| Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2250 | curStreamDevice |= device; | 
| Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2251 | applyVolume = (curDevice & curStreamDevice) != 0; | 
|  | 2252 | } else { | 
|  | 2253 | applyVolume = !mVolumeCurves->hasVolumeIndexForDevice( | 
| Jean-Michel Trivi | b7fdce6 | 2017-06-27 19:38:42 -0700 | [diff] [blame] | 2254 | stream, curStreamDevice); | 
| Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2255 | } | 
| Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 2256 | // rescale index before applying to curStream as ranges may be different for | 
|  | 2257 | // stream and curStream | 
|  | 2258 | int idx = rescaleVolumeIndex(index, stream, (audio_stream_type_t)curStream); | 
| Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2259 | if (applyVolume) { | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2260 | //FIXME: workaround for truncated touch sounds | 
|  | 2261 | // delayed volume change for system stream to be removed when the problem is | 
|  | 2262 | // handled by system UI | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2263 | status_t volStatus = | 
| Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 2264 | checkAndSetVolume((audio_stream_type_t)curStream, idx, desc, curDevice, | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2265 | (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0); | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2266 | if (volStatus != NO_ERROR) { | 
|  | 2267 | status = volStatus; | 
|  | 2268 | } | 
|  | 2269 | } | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2270 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2271 | } | 
|  | 2272 | return status; | 
|  | 2273 | } | 
|  | 2274 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2275 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2276 | int *index, | 
|  | 2277 | audio_devices_t device) | 
|  | 2278 | { | 
|  | 2279 | if (index == NULL) { | 
|  | 2280 | return BAD_VALUE; | 
|  | 2281 | } | 
|  | 2282 | if (!audio_is_output_device(device)) { | 
|  | 2283 | return BAD_VALUE; | 
|  | 2284 | } | 
| Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2285 | // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device corresponding to | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2286 | // the strategy the stream belongs to. | 
| Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2287 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2288 | device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); | 
|  | 2289 | } | 
| François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 2290 | device = Volume::getDeviceForVolume(device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2291 |  | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2292 | *index =  mVolumeCurves->getVolumeIndex(stream, device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2293 | ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index); | 
|  | 2294 | return NO_ERROR; | 
|  | 2295 | } | 
|  | 2296 |  | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2297 | audio_io_handle_t AudioPolicyManager::selectOutputForMusicEffects() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2298 | { | 
|  | 2299 | // select one output among several suitable for global effects. | 
|  | 2300 | // The priority is as follows: | 
|  | 2301 | // 1: An offloaded output. If the effect ends up not being offloadable, | 
|  | 2302 | //    AudioFlinger will invalidate the track and the offloaded output | 
|  | 2303 | //    will be closed causing the effect to be moved to a PCM output. | 
|  | 2304 | // 2: A deep buffer output | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2305 | // 3: The primary output | 
|  | 2306 | // 4: the first output in the list | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2307 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2308 | routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2309 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2310 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2311 |  | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2312 | if (outputs.size() == 0) { | 
|  | 2313 | return AUDIO_IO_HANDLE_NONE; | 
|  | 2314 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2315 |  | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2316 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; | 
|  | 2317 | bool activeOnly = true; | 
|  | 2318 |  | 
|  | 2319 | while (output == AUDIO_IO_HANDLE_NONE) { | 
|  | 2320 | audio_io_handle_t outputOffloaded = AUDIO_IO_HANDLE_NONE; | 
|  | 2321 | audio_io_handle_t outputDeepBuffer = AUDIO_IO_HANDLE_NONE; | 
|  | 2322 | audio_io_handle_t outputPrimary = AUDIO_IO_HANDLE_NONE; | 
|  | 2323 |  | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2324 | for (audio_io_handle_t output : outputs) { | 
|  | 2325 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2326 | if (activeOnly && !desc->isStreamActive(AUDIO_STREAM_MUSIC)) { | 
|  | 2327 | continue; | 
|  | 2328 | } | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2329 | ALOGV("selectOutputForMusicEffects activeOnly %d output %d flags 0x%08x", | 
|  | 2330 | activeOnly, output, desc->mFlags); | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2331 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2332 | outputOffloaded = output; | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2333 | } | 
|  | 2334 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2335 | outputDeepBuffer = output; | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2336 | } | 
|  | 2337 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) != 0) { | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2338 | outputPrimary = output; | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2339 | } | 
|  | 2340 | } | 
|  | 2341 | if (outputOffloaded != AUDIO_IO_HANDLE_NONE) { | 
|  | 2342 | output = outputOffloaded; | 
|  | 2343 | } else if (outputDeepBuffer != AUDIO_IO_HANDLE_NONE) { | 
|  | 2344 | output = outputDeepBuffer; | 
|  | 2345 | } else if (outputPrimary != AUDIO_IO_HANDLE_NONE) { | 
|  | 2346 | output = outputPrimary; | 
|  | 2347 | } else { | 
|  | 2348 | output = outputs[0]; | 
|  | 2349 | } | 
|  | 2350 | activeOnly = false; | 
|  | 2351 | } | 
|  | 2352 |  | 
|  | 2353 | if (output != mMusicEffectOutput) { | 
|  | 2354 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output); | 
|  | 2355 | mMusicEffectOutput = output; | 
|  | 2356 | } | 
|  | 2357 |  | 
|  | 2358 | ALOGV("selectOutputForMusicEffects selected output %d", output); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2359 | return output; | 
|  | 2360 | } | 
|  | 2361 |  | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2362 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc __unused) | 
|  | 2363 | { | 
|  | 2364 | return selectOutputForMusicEffects(); | 
|  | 2365 | } | 
|  | 2366 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2367 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2368 | audio_io_handle_t io, | 
|  | 2369 | uint32_t strategy, | 
|  | 2370 | int session, | 
|  | 2371 | int id) | 
|  | 2372 | { | 
|  | 2373 | ssize_t index = mOutputs.indexOfKey(io); | 
|  | 2374 | if (index < 0) { | 
|  | 2375 | index = mInputs.indexOfKey(io); | 
|  | 2376 | if (index < 0) { | 
|  | 2377 | ALOGW("registerEffect() unknown io %d", io); | 
|  | 2378 | return INVALID_OPERATION; | 
|  | 2379 | } | 
|  | 2380 | } | 
| François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2381 | return mEffects.registerEffect(desc, io, strategy, session, id); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2382 | } | 
|  | 2383 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2384 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const | 
|  | 2385 | { | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2386 | bool active = false; | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2387 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) { | 
|  | 2388 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2389 | continue; | 
|  | 2390 | } | 
|  | 2391 | active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs); | 
|  | 2392 | } | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2393 | return active; | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2394 | } | 
|  | 2395 |  | 
|  | 2396 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const | 
|  | 2397 | { | 
|  | 2398 | return mOutputs.isStreamActiveRemotely(stream, inPastMs); | 
|  | 2399 | } | 
|  | 2400 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2401 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2402 | { | 
|  | 2403 | for (size_t i = 0; i < mInputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2404 | const sp<AudioInputDescriptor>  inputDescriptor = mInputs.valueAt(i); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2405 | if (inputDescriptor->isSourceActive(source)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2406 | return true; | 
|  | 2407 | } | 
|  | 2408 | } | 
|  | 2409 | return false; | 
|  | 2410 | } | 
|  | 2411 |  | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2412 | // Register a list of custom mixes with their attributes and format. | 
|  | 2413 | // When a mix is registered, corresponding input and output profiles are | 
|  | 2414 | // added to the remote submix hw module. The profile contains only the | 
|  | 2415 | // parameters (sampling rate, format...) specified by the mix. | 
|  | 2416 | // The corresponding input remote submix device is also connected. | 
|  | 2417 | // | 
|  | 2418 | // When a remote submix device is connected, the address is checked to select the | 
|  | 2419 | // appropriate profile and the corresponding input or output stream is opened. | 
|  | 2420 | // | 
|  | 2421 | // When capture starts, getInputForAttr() will: | 
|  | 2422 | //  - 1 look for a mix matching the address passed in attribtutes tags if any | 
|  | 2423 | //  - 2 if none found, getDeviceForInputSource() will: | 
|  | 2424 | //     - 2.1 look for a mix matching the attributes source | 
|  | 2425 | //     - 2.2 if none found, default to device selection by policy rules | 
|  | 2426 | // At this time, the corresponding output remote submix device is also connected | 
|  | 2427 | // and active playback use cases can be transferred to this mix if needed when reconnecting | 
|  | 2428 | // after AudioTracks are invalidated | 
|  | 2429 | // | 
|  | 2430 | // When playback starts, getOutputForAttr() will: | 
|  | 2431 | //  - 1 look for a mix matching the address passed in attribtutes tags if any | 
|  | 2432 | //  - 2 if none found, look for a mix matching the attributes usage | 
|  | 2433 | //  - 3 if none found, default to device and output selection by policy rules. | 
|  | 2434 |  | 
| Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 2435 | status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes) | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2436 | { | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2437 | ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size()); | 
|  | 2438 | status_t res = NO_ERROR; | 
|  | 2439 |  | 
|  | 2440 | sp<HwModule> rSubmixModule; | 
|  | 2441 | // examine each mix's route type | 
|  | 2442 | for (size_t i = 0; i < mixes.size(); i++) { | 
|  | 2443 | // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination | 
|  | 2444 | if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) { | 
|  | 2445 | res = INVALID_OPERATION; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2446 | break; | 
|  | 2447 | } | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2448 | if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) { | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2449 | ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK", i, mixes.size()); | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2450 | if (rSubmixModule == 0) { | 
| Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 2451 | rSubmixModule = mHwModules.getModuleFromName( | 
|  | 2452 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); | 
|  | 2453 | if (rSubmixModule == 0) { | 
|  | 2454 | ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", | 
|  | 2455 | i); | 
|  | 2456 | res = INVALID_OPERATION; | 
|  | 2457 | break; | 
|  | 2458 | } | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2459 | } | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2460 |  | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2461 | String8 address = mixes[i].mDeviceAddress; | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2462 |  | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2463 | if (mPolicyMixes.registerMix(address, mixes[i], 0 /*output desc*/) != NO_ERROR) { | 
| Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2464 | ALOGE(" Error registering mix %zu for address %s", i, address.string()); | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2465 | res = INVALID_OPERATION; | 
|  | 2466 | break; | 
|  | 2467 | } | 
|  | 2468 | audio_config_t outputConfig = mixes[i].mFormat; | 
|  | 2469 | audio_config_t inputConfig = mixes[i].mFormat; | 
|  | 2470 | // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in | 
|  | 2471 | // stereo and let audio flinger do the channel conversion if needed. | 
|  | 2472 | outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; | 
|  | 2473 | inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; | 
|  | 2474 | rSubmixModule->addOutputProfile(address, &outputConfig, | 
|  | 2475 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address); | 
|  | 2476 | rSubmixModule->addInputProfile(address, &inputConfig, | 
|  | 2477 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, address); | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2478 |  | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2479 | if (mixes[i].mMixType == MIX_TYPE_PLAYERS) { | 
|  | 2480 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, | 
|  | 2481 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, | 
|  | 2482 | address.string(), "remote-submix"); | 
|  | 2483 | } else { | 
|  | 2484 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, | 
|  | 2485 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, | 
|  | 2486 | address.string(), "remote-submix"); | 
|  | 2487 | } | 
|  | 2488 | } else if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2489 | String8 address = mixes[i].mDeviceAddress; | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2490 | audio_devices_t device = mixes[i].mDeviceType; | 
| Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2491 | ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s", | 
|  | 2492 | i, mixes.size(), device, address.string()); | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2493 |  | 
| Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2494 | bool foundOutput = false; | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2495 | for (size_t j = 0 ; j < mOutputs.size() ; j++) { | 
|  | 2496 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j); | 
|  | 2497 | sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle()); | 
|  | 2498 | if ((patch != 0) && (patch->mPatch.num_sinks != 0) | 
|  | 2499 | && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE) | 
|  | 2500 | && (patch->mPatch.sinks[0].ext.device.type == device) | 
| Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2501 | && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(), | 
|  | 2502 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2503 | if (mPolicyMixes.registerMix(address, mixes[i], desc) != NO_ERROR) { | 
|  | 2504 | res = INVALID_OPERATION; | 
| Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2505 | } else { | 
|  | 2506 | foundOutput = true; | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2507 | } | 
|  | 2508 | break; | 
|  | 2509 | } | 
|  | 2510 | } | 
|  | 2511 |  | 
|  | 2512 | if (res != NO_ERROR) { | 
|  | 2513 | ALOGE(" Error registering mix %zu for device 0x%X addr %s", | 
| Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2514 | i, device, address.string()); | 
|  | 2515 | res = INVALID_OPERATION; | 
|  | 2516 | break; | 
|  | 2517 | } else if (!foundOutput) { | 
|  | 2518 | ALOGE(" Output not found for mix %zu for device 0x%X addr %s", | 
|  | 2519 | i, device, address.string()); | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2520 | res = INVALID_OPERATION; | 
|  | 2521 | break; | 
|  | 2522 | } | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2523 | } | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2524 | } | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2525 | if (res != NO_ERROR) { | 
|  | 2526 | unregisterPolicyMixes(mixes); | 
|  | 2527 | } | 
|  | 2528 | return res; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2529 | } | 
|  | 2530 |  | 
|  | 2531 | status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes) | 
|  | 2532 | { | 
| Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2533 | ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size()); | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2534 | status_t res = NO_ERROR; | 
|  | 2535 | sp<HwModule> rSubmixModule; | 
|  | 2536 | // examine each mix's route type | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2537 | for (const auto& mix : mixes) { | 
|  | 2538 | if ((mix.mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) { | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2539 |  | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2540 | if (rSubmixModule == 0) { | 
| Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 2541 | rSubmixModule = mHwModules.getModuleFromName( | 
|  | 2542 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); | 
|  | 2543 | if (rSubmixModule == 0) { | 
|  | 2544 | res = INVALID_OPERATION; | 
|  | 2545 | continue; | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2546 | } | 
|  | 2547 | } | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2548 |  | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2549 | String8 address = mix.mDeviceAddress; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2550 |  | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2551 | if (mPolicyMixes.unregisterMix(address) != NO_ERROR) { | 
|  | 2552 | res = INVALID_OPERATION; | 
|  | 2553 | continue; | 
|  | 2554 | } | 
|  | 2555 |  | 
|  | 2556 | if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) == | 
|  | 2557 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE)  { | 
|  | 2558 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, | 
|  | 2559 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, | 
|  | 2560 | address.string(), "remote-submix"); | 
|  | 2561 | } | 
|  | 2562 | if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) == | 
|  | 2563 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE)  { | 
|  | 2564 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, | 
|  | 2565 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, | 
|  | 2566 | address.string(), "remote-submix"); | 
|  | 2567 | } | 
|  | 2568 | rSubmixModule->removeOutputProfile(address); | 
|  | 2569 | rSubmixModule->removeInputProfile(address); | 
|  | 2570 |  | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2571 | } if ((mix.mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { | 
|  | 2572 | if (mPolicyMixes.unregisterMix(mix.mDeviceAddress) != NO_ERROR) { | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2573 | res = INVALID_OPERATION; | 
|  | 2574 | continue; | 
|  | 2575 | } | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2576 | } | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2577 | } | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2578 | return res; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2579 | } | 
|  | 2580 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2581 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2582 | status_t AudioPolicyManager::dump(int fd) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2583 | { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2584 | String8 result; | 
| Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 2585 | result.appendFormat("\nAudioPolicyManager Dump: %p\n", this); | 
|  | 2586 | result.appendFormat(" Primary Output: %d\n", | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 2587 | hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE); | 
| Mikhail Naganov | 0d6a033 | 2016-04-19 17:12:38 -0700 | [diff] [blame] | 2588 | std::string stateLiteral; | 
|  | 2589 | AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral); | 
| Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 2590 | result.appendFormat(" Phone state: %s\n", stateLiteral.c_str()); | 
|  | 2591 | const char* forceUses[AUDIO_POLICY_FORCE_USE_CNT] = { | 
|  | 2592 | "communications", "media", "record", "dock", "system", | 
|  | 2593 | "HDMI system audio", "encoded surround output", "vibrate ringing" }; | 
|  | 2594 | for (audio_policy_force_use_t i = AUDIO_POLICY_FORCE_FOR_COMMUNICATION; | 
|  | 2595 | i < AUDIO_POLICY_FORCE_USE_CNT; i = (audio_policy_force_use_t)((int)i + 1)) { | 
|  | 2596 | result.appendFormat(" Force use for %s: %d\n", | 
|  | 2597 | forceUses[i], mEngine->getForceUse(i)); | 
|  | 2598 | } | 
|  | 2599 | result.appendFormat(" TTS output %savailable\n", mTtsOutputAvailable ? "" : "not "); | 
|  | 2600 | result.appendFormat(" Master mono: %s\n", mMasterMono ? "on" : "off"); | 
|  | 2601 | result.appendFormat(" Config source: %s\n", getConfig().getSource().c_str()); | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2602 | write(fd, result.string(), result.size()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2603 |  | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 2604 | mAvailableOutputDevices.dump(fd, String8("Available output")); | 
|  | 2605 | mAvailableInputDevices.dump(fd, String8("Available input")); | 
| Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 2606 | mHwModulesAll.dump(fd); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2607 | mOutputs.dump(fd); | 
|  | 2608 | mInputs.dump(fd); | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2609 | mVolumeCurves->dump(fd); | 
| François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2610 | mEffects.dump(fd); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2611 | mAudioPatches.dump(fd); | 
| Mikhail Naganov | 44344b0 | 2016-12-13 11:21:02 -0800 | [diff] [blame] | 2612 | mPolicyMixes.dump(fd); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2613 |  | 
|  | 2614 | return NO_ERROR; | 
|  | 2615 | } | 
|  | 2616 |  | 
|  | 2617 | // This function checks for the parameters which can be offloaded. | 
|  | 2618 | // This can be enhanced depending on the capability of the DSP and policy | 
|  | 2619 | // of the system. | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2620 | bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2621 | { | 
|  | 2622 | ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2623 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2624 | offloadInfo.sample_rate, offloadInfo.channel_mask, | 
|  | 2625 | offloadInfo.format, | 
|  | 2626 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, | 
|  | 2627 | offloadInfo.has_video); | 
|  | 2628 |  | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 2629 | if (mMasterMono) { | 
|  | 2630 | return false; // no offloading if mono is set. | 
|  | 2631 | } | 
|  | 2632 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2633 | // Check if offload has been disabled | 
|  | 2634 | char propValue[PROPERTY_VALUE_MAX]; | 
|  | 2635 | if (property_get("audio.offload.disable", propValue, "0")) { | 
|  | 2636 | if (atoi(propValue) != 0) { | 
|  | 2637 | ALOGV("offload disabled by audio.offload.disable=%s", propValue ); | 
|  | 2638 | return false; | 
|  | 2639 | } | 
|  | 2640 | } | 
|  | 2641 |  | 
|  | 2642 | // Check if stream type is music, then only allow offload as of now. | 
|  | 2643 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) | 
|  | 2644 | { | 
|  | 2645 | ALOGV("isOffloadSupported: stream_type != MUSIC, returning false"); | 
|  | 2646 | return false; | 
|  | 2647 | } | 
|  | 2648 |  | 
|  | 2649 | //TODO: enable audio offloading with video when ready | 
| Andy Hung | 08945c4 | 2015-05-31 21:36:46 -0700 | [diff] [blame] | 2650 | const bool allowOffloadWithVideo = | 
|  | 2651 | property_get_bool("audio.offload.video", false /* default_value */); | 
|  | 2652 | if (offloadInfo.has_video && !allowOffloadWithVideo) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2653 | ALOGV("isOffloadSupported: has_video == true, returning false"); | 
|  | 2654 | return false; | 
|  | 2655 | } | 
|  | 2656 |  | 
|  | 2657 | //If duration is less than minimum value defined in property, return false | 
|  | 2658 | if (property_get("audio.offload.min.duration.secs", propValue, NULL)) { | 
|  | 2659 | if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) { | 
|  | 2660 | ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue); | 
|  | 2661 | return false; | 
|  | 2662 | } | 
|  | 2663 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { | 
|  | 2664 | ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS); | 
|  | 2665 | return false; | 
|  | 2666 | } | 
|  | 2667 |  | 
|  | 2668 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from | 
|  | 2669 | // creating an offloaded track and tearing it down immediately after start when audioflinger | 
|  | 2670 | // detects there is an active non offloadable effect. | 
|  | 2671 | // FIXME: We should check the audio session here but we do not have it in this context. | 
|  | 2672 | // This may prevent offloading in rare situations where effects are left active by apps | 
|  | 2673 | // in the background. | 
| François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2674 | if (mEffects.isNonOffloadableEffectEnabled()) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2675 | return false; | 
|  | 2676 | } | 
|  | 2677 |  | 
|  | 2678 | // See if there is a profile to support this. | 
|  | 2679 | // AUDIO_DEVICE_NONE | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2680 | sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2681 | offloadInfo.sample_rate, | 
|  | 2682 | offloadInfo.format, | 
|  | 2683 | offloadInfo.channel_mask, | 
|  | 2684 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2685 | ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT "); | 
|  | 2686 | return (profile != 0); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2687 | } | 
|  | 2688 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2689 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, | 
|  | 2690 | audio_port_type_t type, | 
|  | 2691 | unsigned int *num_ports, | 
|  | 2692 | struct audio_port *ports, | 
|  | 2693 | unsigned int *generation) | 
|  | 2694 | { | 
|  | 2695 | if (num_ports == NULL || (*num_ports != 0 && ports == NULL) || | 
|  | 2696 | generation == NULL) { | 
|  | 2697 | return BAD_VALUE; | 
|  | 2698 | } | 
|  | 2699 | ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports); | 
|  | 2700 | if (ports == NULL) { | 
|  | 2701 | *num_ports = 0; | 
|  | 2702 | } | 
|  | 2703 |  | 
|  | 2704 | size_t portsWritten = 0; | 
|  | 2705 | size_t portsMax = *num_ports; | 
|  | 2706 | *num_ports = 0; | 
|  | 2707 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { | 
| Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2708 | // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB | 
|  | 2709 | // as they are used by stub HALs by convention | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2710 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2711 | for (const auto& dev : mAvailableOutputDevices) { | 
|  | 2712 | if (dev->type() == AUDIO_DEVICE_OUT_STUB) { | 
| Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2713 | continue; | 
|  | 2714 | } | 
|  | 2715 | if (portsWritten < portsMax) { | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2716 | dev->toAudioPort(&ports[portsWritten++]); | 
| Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2717 | } | 
|  | 2718 | (*num_ports)++; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2719 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2720 | } | 
|  | 2721 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2722 | for (const auto& dev : mAvailableInputDevices) { | 
|  | 2723 | if (dev->type() == AUDIO_DEVICE_IN_STUB) { | 
| Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2724 | continue; | 
|  | 2725 | } | 
|  | 2726 | if (portsWritten < portsMax) { | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2727 | dev->toAudioPort(&ports[portsWritten++]); | 
| Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2728 | } | 
|  | 2729 | (*num_ports)++; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2730 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2731 | } | 
|  | 2732 | } | 
|  | 2733 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { | 
|  | 2734 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { | 
|  | 2735 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { | 
|  | 2736 | mInputs[i]->toAudioPort(&ports[portsWritten++]); | 
|  | 2737 | } | 
|  | 2738 | *num_ports += mInputs.size(); | 
|  | 2739 | } | 
|  | 2740 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2741 | size_t numOutputs = 0; | 
|  | 2742 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 2743 | if (!mOutputs[i]->isDuplicated()) { | 
|  | 2744 | numOutputs++; | 
|  | 2745 | if (portsWritten < portsMax) { | 
|  | 2746 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); | 
|  | 2747 | } | 
|  | 2748 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2749 | } | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2750 | *num_ports += numOutputs; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2751 | } | 
|  | 2752 | } | 
|  | 2753 | *generation = curAudioPortGeneration(); | 
| Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2754 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2755 | return NO_ERROR; | 
|  | 2756 | } | 
|  | 2757 |  | 
| Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 2758 | status_t AudioPolicyManager::getAudioPort(struct audio_port *port) | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2759 | { | 
| Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 2760 | if (port == nullptr || port->id == AUDIO_PORT_HANDLE_NONE) { | 
|  | 2761 | return BAD_VALUE; | 
|  | 2762 | } | 
|  | 2763 | sp<DeviceDescriptor> dev = mAvailableOutputDevices.getDeviceFromId(port->id); | 
|  | 2764 | if (dev != 0) { | 
|  | 2765 | dev->toAudioPort(port); | 
|  | 2766 | return NO_ERROR; | 
|  | 2767 | } | 
|  | 2768 | dev = mAvailableInputDevices.getDeviceFromId(port->id); | 
|  | 2769 | if (dev != 0) { | 
|  | 2770 | dev->toAudioPort(port); | 
|  | 2771 | return NO_ERROR; | 
|  | 2772 | } | 
|  | 2773 | sp<SwAudioOutputDescriptor> out = mOutputs.getOutputFromId(port->id); | 
|  | 2774 | if (out != 0) { | 
|  | 2775 | out->toAudioPort(port); | 
|  | 2776 | return NO_ERROR; | 
|  | 2777 | } | 
|  | 2778 | sp<AudioInputDescriptor> in = mInputs.getInputFromId(port->id); | 
|  | 2779 | if (in != 0) { | 
|  | 2780 | in->toAudioPort(port); | 
|  | 2781 | return NO_ERROR; | 
|  | 2782 | } | 
|  | 2783 | return BAD_VALUE; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2784 | } | 
|  | 2785 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2786 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, | 
|  | 2787 | audio_patch_handle_t *handle, | 
|  | 2788 | uid_t uid) | 
|  | 2789 | { | 
|  | 2790 | ALOGV("createAudioPatch()"); | 
|  | 2791 |  | 
|  | 2792 | if (handle == NULL || patch == NULL) { | 
|  | 2793 | return BAD_VALUE; | 
|  | 2794 | } | 
|  | 2795 | ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks); | 
|  | 2796 |  | 
| Mikhail Naganov | ac9858b | 2018-06-15 13:12:37 -0700 | [diff] [blame] | 2797 | if (!audio_patch_is_valid(patch)) { | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2798 | return BAD_VALUE; | 
|  | 2799 | } | 
|  | 2800 | // only one source per audio patch supported for now | 
|  | 2801 | if (patch->num_sources > 1) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2802 | return INVALID_OPERATION; | 
|  | 2803 | } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2804 |  | 
|  | 2805 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2806 | return INVALID_OPERATION; | 
|  | 2807 | } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2808 | for (size_t i = 0; i < patch->num_sinks; i++) { | 
|  | 2809 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { | 
|  | 2810 | return INVALID_OPERATION; | 
|  | 2811 | } | 
|  | 2812 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2813 |  | 
|  | 2814 | sp<AudioPatch> patchDesc; | 
|  | 2815 | ssize_t index = mAudioPatches.indexOfKey(*handle); | 
|  | 2816 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2817 | ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id, | 
|  | 2818 | patch->sources[0].role, | 
|  | 2819 | patch->sources[0].type); | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2820 | #if LOG_NDEBUG == 0 | 
|  | 2821 | for (size_t i = 0; i < patch->num_sinks; i++) { | 
| Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2822 | ALOGV("createAudioPatch sink %zu: id %d role %d type %d", i, patch->sinks[i].id, | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2823 | patch->sinks[i].role, | 
|  | 2824 | patch->sinks[i].type); | 
|  | 2825 | } | 
|  | 2826 | #endif | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2827 |  | 
|  | 2828 | if (index >= 0) { | 
|  | 2829 | patchDesc = mAudioPatches.valueAt(index); | 
|  | 2830 | ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", | 
|  | 2831 | mUidCached, patchDesc->mUid, uid); | 
|  | 2832 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { | 
|  | 2833 | return INVALID_OPERATION; | 
|  | 2834 | } | 
|  | 2835 | } else { | 
| Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 2836 | *handle = AUDIO_PATCH_HANDLE_NONE; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2837 | } | 
|  | 2838 |  | 
|  | 2839 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2840 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2841 | if (outputDesc == NULL) { | 
|  | 2842 | ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id); | 
|  | 2843 | return BAD_VALUE; | 
|  | 2844 | } | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2845 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", | 
|  | 2846 | outputDesc->mIoHandle); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2847 | if (patchDesc != 0) { | 
|  | 2848 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { | 
|  | 2849 | ALOGV("createAudioPatch() source id differs for patch current id %d new id %d", | 
|  | 2850 | patchDesc->mPatch.sources[0].id, patch->sources[0].id); | 
|  | 2851 | return BAD_VALUE; | 
|  | 2852 | } | 
|  | 2853 | } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2854 | DeviceVector devices; | 
|  | 2855 | for (size_t i = 0; i < patch->num_sinks; i++) { | 
|  | 2856 | // Only support mix to devices connection | 
|  | 2857 | // TODO add support for mix to mix connection | 
|  | 2858 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { | 
|  | 2859 | ALOGV("createAudioPatch() source mix but sink is not a device"); | 
|  | 2860 | return INVALID_OPERATION; | 
|  | 2861 | } | 
|  | 2862 | sp<DeviceDescriptor> devDesc = | 
|  | 2863 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); | 
|  | 2864 | if (devDesc == 0) { | 
|  | 2865 | ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id); | 
|  | 2866 | return BAD_VALUE; | 
|  | 2867 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2868 |  | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2869 | if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(), | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2870 | devDesc->mAddress, | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2871 | patch->sources[0].sample_rate, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2872 | NULL,  // updatedSamplingRate | 
|  | 2873 | patch->sources[0].format, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2874 | NULL,  // updatedFormat | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2875 | patch->sources[0].channel_mask, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2876 | NULL,  // updatedChannelMask | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2877 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2878 | ALOGV("createAudioPatch() profile not supported for device %08x", | 
|  | 2879 | devDesc->type()); | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2880 | return INVALID_OPERATION; | 
|  | 2881 | } | 
|  | 2882 | devices.add(devDesc); | 
|  | 2883 | } | 
|  | 2884 | if (devices.size() == 0) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2885 | return INVALID_OPERATION; | 
|  | 2886 | } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2887 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2888 | // TODO: reconfigure output format and channels here | 
|  | 2889 | ALOGV("createAudioPatch() setting device %08x on output %d", | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2890 | devices.types(), outputDesc->mIoHandle); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2891 | setOutputDevice(outputDesc, devices.types(), true, 0, handle); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2892 | index = mAudioPatches.indexOfKey(*handle); | 
|  | 2893 | if (index >= 0) { | 
|  | 2894 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { | 
|  | 2895 | ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided"); | 
|  | 2896 | } | 
|  | 2897 | patchDesc = mAudioPatches.valueAt(index); | 
|  | 2898 | patchDesc->mUid = uid; | 
|  | 2899 | ALOGV("createAudioPatch() success"); | 
|  | 2900 | } else { | 
|  | 2901 | ALOGW("createAudioPatch() setOutputDevice() failed to create a patch"); | 
|  | 2902 | return INVALID_OPERATION; | 
|  | 2903 | } | 
|  | 2904 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { | 
|  | 2905 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { | 
|  | 2906 | // input device to input mix connection | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2907 | // only one sink supported when connecting an input device to a mix | 
|  | 2908 | if (patch->num_sinks > 1) { | 
|  | 2909 | return INVALID_OPERATION; | 
|  | 2910 | } | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2911 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2912 | if (inputDesc == NULL) { | 
|  | 2913 | return BAD_VALUE; | 
|  | 2914 | } | 
|  | 2915 | if (patchDesc != 0) { | 
|  | 2916 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { | 
|  | 2917 | return BAD_VALUE; | 
|  | 2918 | } | 
|  | 2919 | } | 
|  | 2920 | sp<DeviceDescriptor> devDesc = | 
|  | 2921 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); | 
|  | 2922 | if (devDesc == 0) { | 
|  | 2923 | return BAD_VALUE; | 
|  | 2924 | } | 
|  | 2925 |  | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2926 | if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(), | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2927 | devDesc->mAddress, | 
|  | 2928 | patch->sinks[0].sample_rate, | 
|  | 2929 | NULL, /*updatedSampleRate*/ | 
|  | 2930 | patch->sinks[0].format, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2931 | NULL, /*updatedFormat*/ | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2932 | patch->sinks[0].channel_mask, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2933 | NULL, /*updatedChannelMask*/ | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2934 | // FIXME for the parameter type, | 
|  | 2935 | // and the NONE | 
|  | 2936 | (audio_output_flags_t) | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2937 | AUDIO_INPUT_FLAG_NONE)) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2938 | return INVALID_OPERATION; | 
|  | 2939 | } | 
|  | 2940 | // TODO: reconfigure output format and channels here | 
|  | 2941 | ALOGV("createAudioPatch() setting device %08x on output %d", | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2942 | devDesc->type(), inputDesc->mIoHandle); | 
|  | 2943 | setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2944 | index = mAudioPatches.indexOfKey(*handle); | 
|  | 2945 | if (index >= 0) { | 
|  | 2946 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { | 
|  | 2947 | ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided"); | 
|  | 2948 | } | 
|  | 2949 | patchDesc = mAudioPatches.valueAt(index); | 
|  | 2950 | patchDesc->mUid = uid; | 
|  | 2951 | ALOGV("createAudioPatch() success"); | 
|  | 2952 | } else { | 
|  | 2953 | ALOGW("createAudioPatch() setInputDevice() failed to create a patch"); | 
|  | 2954 | return INVALID_OPERATION; | 
|  | 2955 | } | 
|  | 2956 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { | 
|  | 2957 | // device to device connection | 
|  | 2958 | if (patchDesc != 0) { | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2959 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2960 | return BAD_VALUE; | 
|  | 2961 | } | 
|  | 2962 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2963 | sp<DeviceDescriptor> srcDeviceDesc = | 
|  | 2964 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); | 
| Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 2965 | if (srcDeviceDesc == 0) { | 
|  | 2966 | return BAD_VALUE; | 
|  | 2967 | } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2968 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2969 | //update source and sink with our own data as the data passed in the patch may | 
|  | 2970 | // be incomplete. | 
|  | 2971 | struct audio_patch newPatch = *patch; | 
|  | 2972 | srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2973 |  | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2974 | for (size_t i = 0; i < patch->num_sinks; i++) { | 
|  | 2975 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { | 
|  | 2976 | ALOGV("createAudioPatch() source device but one sink is not a device"); | 
|  | 2977 | return INVALID_OPERATION; | 
|  | 2978 | } | 
|  | 2979 |  | 
|  | 2980 | sp<DeviceDescriptor> sinkDeviceDesc = | 
|  | 2981 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); | 
|  | 2982 | if (sinkDeviceDesc == 0) { | 
|  | 2983 | return BAD_VALUE; | 
|  | 2984 | } | 
|  | 2985 | sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]); | 
|  | 2986 |  | 
| Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2987 | // create a software bridge in PatchPanel if: | 
| Scott Randolph | f317240 | 2018-01-23 17:06:53 -0800 | [diff] [blame] | 2988 | // - source and sink devices are on different HW modules OR | 
| Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2989 | // - audio HAL version is < 3.0 | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 2990 | if (!srcDeviceDesc->hasSameHwModuleAs(sinkDeviceDesc) || | 
| Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 2991 | (srcDeviceDesc->mModule->getHalVersionMajor() < 3)) { | 
| Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2992 | // support only one sink device for now to simplify output selection logic | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2993 | if (patch->num_sinks > 1) { | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2994 | return INVALID_OPERATION; | 
|  | 2995 | } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2996 | SortedVector<audio_io_handle_t> outputs = | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2997 | getOutputsForDevice(sinkDeviceDesc->type(), mOutputs); | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2998 | // if the sink device is reachable via an opened output stream, request to go via | 
|  | 2999 | // this output stream by adding a second source to the patch description | 
| Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 3000 | audio_io_handle_t output = selectOutput(outputs, | 
|  | 3001 | AUDIO_OUTPUT_FLAG_NONE, | 
|  | 3002 | AUDIO_FORMAT_INVALID); | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3003 | if (output != AUDIO_IO_HANDLE_NONE) { | 
|  | 3004 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
|  | 3005 | if (outputDesc->isDuplicated()) { | 
|  | 3006 | return INVALID_OPERATION; | 
|  | 3007 | } | 
|  | 3008 | outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]); | 
| Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3009 | newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3010 | newPatch.num_sources = 2; | 
|  | 3011 | } | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3012 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3013 | } | 
|  | 3014 | // TODO: check from routing capabilities in config file and other conflicting patches | 
|  | 3015 |  | 
| Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 3016 | status_t status = installPatch(__func__, index, handle, &newPatch, 0, uid, &patchDesc); | 
|  | 3017 | if (status != NO_ERROR) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3018 | ALOGW("createAudioPatch() patch panel could not connect device patch, error %d", | 
|  | 3019 | status); | 
|  | 3020 | return INVALID_OPERATION; | 
|  | 3021 | } | 
|  | 3022 | } else { | 
|  | 3023 | return BAD_VALUE; | 
|  | 3024 | } | 
|  | 3025 | } else { | 
|  | 3026 | return BAD_VALUE; | 
|  | 3027 | } | 
|  | 3028 | return NO_ERROR; | 
|  | 3029 | } | 
|  | 3030 |  | 
|  | 3031 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, | 
|  | 3032 | uid_t uid) | 
|  | 3033 | { | 
|  | 3034 | ALOGV("releaseAudioPatch() patch %d", handle); | 
|  | 3035 |  | 
|  | 3036 | ssize_t index = mAudioPatches.indexOfKey(handle); | 
|  | 3037 |  | 
|  | 3038 | if (index < 0) { | 
|  | 3039 | return BAD_VALUE; | 
|  | 3040 | } | 
|  | 3041 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
|  | 3042 | ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", | 
|  | 3043 | mUidCached, patchDesc->mUid, uid); | 
|  | 3044 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { | 
|  | 3045 | return INVALID_OPERATION; | 
|  | 3046 | } | 
|  | 3047 |  | 
|  | 3048 | struct audio_patch *patch = &patchDesc->mPatch; | 
|  | 3049 | patchDesc->mUid = mUidCached; | 
|  | 3050 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3051 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3052 | if (outputDesc == NULL) { | 
|  | 3053 | ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id); | 
|  | 3054 | return BAD_VALUE; | 
|  | 3055 | } | 
|  | 3056 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3057 | setOutputDevice(outputDesc, | 
|  | 3058 | getNewOutputDevice(outputDesc, true /*fromCache*/), | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3059 | true, | 
|  | 3060 | 0, | 
|  | 3061 | NULL); | 
|  | 3062 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { | 
|  | 3063 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3064 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3065 | if (inputDesc == NULL) { | 
|  | 3066 | ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id); | 
|  | 3067 | return BAD_VALUE; | 
|  | 3068 | } | 
|  | 3069 | setInputDevice(inputDesc->mIoHandle, | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 3070 | getNewInputDevice(inputDesc), | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3071 | true, | 
|  | 3072 | NULL); | 
|  | 3073 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3074 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); | 
|  | 3075 | ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d", | 
|  | 3076 | status, patchDesc->mAfPatchHandle); | 
|  | 3077 | removeAudioPatch(patchDesc->mHandle); | 
|  | 3078 | nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 3079 | mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3080 | } else { | 
|  | 3081 | return BAD_VALUE; | 
|  | 3082 | } | 
|  | 3083 | } else { | 
|  | 3084 | return BAD_VALUE; | 
|  | 3085 | } | 
|  | 3086 | return NO_ERROR; | 
|  | 3087 | } | 
|  | 3088 |  | 
|  | 3089 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, | 
|  | 3090 | struct audio_patch *patches, | 
|  | 3091 | unsigned int *generation) | 
|  | 3092 | { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3093 | if (generation == NULL) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3094 | return BAD_VALUE; | 
|  | 3095 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3096 | *generation = curAudioPortGeneration(); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3097 | return mAudioPatches.listAudioPatches(num_patches, patches); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3098 | } | 
|  | 3099 |  | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3100 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3101 | { | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3102 | ALOGV("setAudioPortConfig()"); | 
|  | 3103 |  | 
|  | 3104 | if (config == NULL) { | 
|  | 3105 | return BAD_VALUE; | 
|  | 3106 | } | 
|  | 3107 | ALOGV("setAudioPortConfig() on port handle %d", config->id); | 
|  | 3108 | // Only support gain configuration for now | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3109 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { | 
|  | 3110 | return INVALID_OPERATION; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3111 | } | 
|  | 3112 |  | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3113 | sp<AudioPortConfig> audioPortConfig; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3114 | if (config->type == AUDIO_PORT_TYPE_MIX) { | 
|  | 3115 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3116 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id); | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3117 | if (outputDesc == NULL) { | 
|  | 3118 | return BAD_VALUE; | 
|  | 3119 | } | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 3120 | ALOG_ASSERT(!outputDesc->isDuplicated(), | 
|  | 3121 | "setAudioPortConfig() called on duplicated output %d", | 
|  | 3122 | outputDesc->mIoHandle); | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3123 | audioPortConfig = outputDesc; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3124 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3125 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id); | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3126 | if (inputDesc == NULL) { | 
|  | 3127 | return BAD_VALUE; | 
|  | 3128 | } | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3129 | audioPortConfig = inputDesc; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3130 | } else { | 
|  | 3131 | return BAD_VALUE; | 
|  | 3132 | } | 
|  | 3133 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { | 
|  | 3134 | sp<DeviceDescriptor> deviceDesc; | 
|  | 3135 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { | 
|  | 3136 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); | 
|  | 3137 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { | 
|  | 3138 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); | 
|  | 3139 | } else { | 
|  | 3140 | return BAD_VALUE; | 
|  | 3141 | } | 
|  | 3142 | if (deviceDesc == NULL) { | 
|  | 3143 | return BAD_VALUE; | 
|  | 3144 | } | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3145 | audioPortConfig = deviceDesc; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3146 | } else { | 
|  | 3147 | return BAD_VALUE; | 
|  | 3148 | } | 
|  | 3149 |  | 
| Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 3150 | struct audio_port_config backupConfig = {}; | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3151 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); | 
|  | 3152 | if (status == NO_ERROR) { | 
| Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 3153 | struct audio_port_config newConfig = {}; | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3154 | audioPortConfig->toAudioPortConfig(&newConfig, config); | 
|  | 3155 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3156 | } | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3157 | if (status != NO_ERROR) { | 
|  | 3158 | audioPortConfig->applyAudioPortConfig(&backupConfig); | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3159 | } | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3160 |  | 
|  | 3161 | return status; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3162 | } | 
|  | 3163 |  | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3164 | void AudioPolicyManager::releaseResourcesForUid(uid_t uid) | 
|  | 3165 | { | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3166 | clearAudioSources(uid); | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3167 | clearAudioPatches(uid); | 
|  | 3168 | clearSessionRoutes(uid); | 
|  | 3169 | } | 
|  | 3170 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3171 | void AudioPolicyManager::clearAudioPatches(uid_t uid) | 
|  | 3172 | { | 
| Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 3173 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--)  { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3174 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); | 
|  | 3175 | if (patchDesc->mUid == uid) { | 
| Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 3176 | releaseAudioPatch(mAudioPatches.keyAt(i), uid); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3177 | } | 
|  | 3178 | } | 
|  | 3179 | } | 
|  | 3180 |  | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3181 | void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy, | 
|  | 3182 | audio_io_handle_t ouptutToSkip) | 
|  | 3183 | { | 
|  | 3184 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); | 
|  | 3185 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); | 
|  | 3186 | for (size_t j = 0; j < mOutputs.size(); j++) { | 
|  | 3187 | if (mOutputs.keyAt(j) == ouptutToSkip) { | 
|  | 3188 | continue; | 
|  | 3189 | } | 
|  | 3190 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j); | 
|  | 3191 | if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) { | 
|  | 3192 | continue; | 
|  | 3193 | } | 
|  | 3194 | // If the default device for this strategy is on another output mix, | 
|  | 3195 | // invalidate all tracks in this strategy to force re connection. | 
|  | 3196 | // Otherwise select new device on the output mix. | 
|  | 3197 | if (outputs.indexOf(mOutputs.keyAt(j)) < 0) { | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 3198 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3199 | if (getStrategy((audio_stream_type_t)stream) == strategy) { | 
|  | 3200 | mpClientInterface->invalidateStream((audio_stream_type_t)stream); | 
|  | 3201 | } | 
|  | 3202 | } | 
|  | 3203 | } else { | 
|  | 3204 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); | 
|  | 3205 | setOutputDevice(outputDesc, newDevice, false); | 
|  | 3206 | } | 
|  | 3207 | } | 
|  | 3208 | } | 
|  | 3209 |  | 
|  | 3210 | void AudioPolicyManager::clearSessionRoutes(uid_t uid) | 
|  | 3211 | { | 
|  | 3212 | // remove output routes associated with this uid | 
|  | 3213 | SortedVector<routing_strategy> affectedStrategies; | 
|  | 3214 | for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--)  { | 
|  | 3215 | sp<SessionRoute> route = mOutputRoutes.valueAt(i); | 
|  | 3216 | if (route->mUid == uid) { | 
|  | 3217 | mOutputRoutes.removeItemsAt(i); | 
|  | 3218 | if (route->mDeviceDescriptor != 0) { | 
|  | 3219 | affectedStrategies.add(getStrategy(route->mStreamType)); | 
|  | 3220 | } | 
|  | 3221 | } | 
|  | 3222 | } | 
|  | 3223 | // reroute outputs if necessary | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3224 | for (const auto& strategy : affectedStrategies) { | 
|  | 3225 | checkStrategyRoute(strategy, AUDIO_IO_HANDLE_NONE); | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3226 | } | 
|  | 3227 |  | 
|  | 3228 | // remove input routes associated with this uid | 
|  | 3229 | SortedVector<audio_source_t> affectedSources; | 
|  | 3230 | for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--)  { | 
|  | 3231 | sp<SessionRoute> route = mInputRoutes.valueAt(i); | 
|  | 3232 | if (route->mUid == uid) { | 
|  | 3233 | mInputRoutes.removeItemsAt(i); | 
|  | 3234 | if (route->mDeviceDescriptor != 0) { | 
|  | 3235 | affectedSources.add(route->mSource); | 
|  | 3236 | } | 
|  | 3237 | } | 
|  | 3238 | } | 
|  | 3239 | // reroute inputs if necessary | 
|  | 3240 | SortedVector<audio_io_handle_t> inputsToClose; | 
|  | 3241 | for (size_t i = 0; i < mInputs.size(); i++) { | 
|  | 3242 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3243 | if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) { | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3244 | inputsToClose.add(inputDesc->mIoHandle); | 
|  | 3245 | } | 
|  | 3246 | } | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3247 | for (const auto& input : inputsToClose) { | 
|  | 3248 | closeInput(input); | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3249 | } | 
|  | 3250 | } | 
|  | 3251 |  | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3252 | void AudioPolicyManager::clearAudioSources(uid_t uid) | 
|  | 3253 | { | 
|  | 3254 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--)  { | 
|  | 3255 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); | 
|  | 3256 | if (sourceDesc->mUid == uid) { | 
|  | 3257 | stopAudioSource(mAudioSources.keyAt(i)); | 
|  | 3258 | } | 
|  | 3259 | } | 
|  | 3260 | } | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3261 |  | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3262 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, | 
|  | 3263 | audio_io_handle_t *ioHandle, | 
|  | 3264 | audio_devices_t *device) | 
|  | 3265 | { | 
| Glenn Kasten | f0c6d7d | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 3266 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); | 
|  | 3267 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT); | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 3268 | *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD); | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3269 |  | 
| François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 3270 | return mSoundTriggerSessions.acquireSession(*session, *ioHandle); | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3271 | } | 
|  | 3272 |  | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3273 | status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source, | 
|  | 3274 | const audio_attributes_t *attributes, | 
| Glenn Kasten | 559d439 | 2016-03-29 13:42:57 -0700 | [diff] [blame] | 3275 | audio_patch_handle_t *handle, | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3276 | uid_t uid) | 
| Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3277 | { | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3278 | ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle); | 
|  | 3279 | if (source == NULL || attributes == NULL || handle == NULL) { | 
|  | 3280 | return BAD_VALUE; | 
|  | 3281 | } | 
|  | 3282 |  | 
| Glenn Kasten | 559d439 | 2016-03-29 13:42:57 -0700 | [diff] [blame] | 3283 | *handle = AUDIO_PATCH_HANDLE_NONE; | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3284 |  | 
|  | 3285 | if (source->role != AUDIO_PORT_ROLE_SOURCE || | 
|  | 3286 | source->type != AUDIO_PORT_TYPE_DEVICE) { | 
|  | 3287 | ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type); | 
|  | 3288 | return INVALID_OPERATION; | 
|  | 3289 | } | 
|  | 3290 |  | 
|  | 3291 | sp<DeviceDescriptor> srcDeviceDesc = | 
|  | 3292 | mAvailableInputDevices.getDevice(source->ext.device.type, | 
|  | 3293 | String8(source->ext.device.address)); | 
|  | 3294 | if (srcDeviceDesc == 0) { | 
|  | 3295 | ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type); | 
|  | 3296 | return BAD_VALUE; | 
|  | 3297 | } | 
|  | 3298 | sp<AudioSourceDescriptor> sourceDesc = | 
|  | 3299 | new AudioSourceDescriptor(srcDeviceDesc, attributes, uid); | 
|  | 3300 |  | 
| Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 3301 | struct audio_patch dummyPatch = {}; | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3302 | sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid); | 
|  | 3303 | sourceDesc->mPatchDesc = patchDesc; | 
|  | 3304 |  | 
|  | 3305 | status_t status = connectAudioSource(sourceDesc); | 
|  | 3306 | if (status == NO_ERROR) { | 
|  | 3307 | mAudioSources.add(sourceDesc->getHandle(), sourceDesc); | 
|  | 3308 | *handle = sourceDesc->getHandle(); | 
|  | 3309 | } | 
|  | 3310 | return status; | 
|  | 3311 | } | 
|  | 3312 |  | 
|  | 3313 | status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc) | 
|  | 3314 | { | 
|  | 3315 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle()); | 
|  | 3316 |  | 
|  | 3317 | // make sure we only have one patch per source. | 
|  | 3318 | disconnectAudioSource(sourceDesc); | 
|  | 3319 |  | 
|  | 3320 | routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes); | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 3321 | audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes); | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3322 | sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice; | 
|  | 3323 |  | 
|  | 3324 | audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true); | 
|  | 3325 | sp<DeviceDescriptor> sinkDeviceDesc = | 
|  | 3326 | mAvailableOutputDevices.getDevice(sinkDevice, String8("")); | 
|  | 3327 |  | 
|  | 3328 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3329 |  | 
|  | 3330 | if (srcDeviceDesc->getAudioPort()->mModule->getHandle() == | 
|  | 3331 | sinkDeviceDesc->getAudioPort()->mModule->getHandle() && | 
| Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 3332 | srcDeviceDesc->getAudioPort()->mModule->getHalVersionMajor() >= 3 && | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3333 | srcDeviceDesc->getAudioPort()->mGains.size() > 0) { | 
|  | 3334 | ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__); | 
|  | 3335 | //   create patch between src device and output device | 
|  | 3336 | //   create Hwoutput and add to mHwOutputs | 
|  | 3337 | } else { | 
|  | 3338 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs); | 
|  | 3339 | audio_io_handle_t output = | 
|  | 3340 | selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID); | 
|  | 3341 | if (output == AUDIO_IO_HANDLE_NONE) { | 
|  | 3342 | ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice); | 
|  | 3343 | return INVALID_OPERATION; | 
|  | 3344 | } | 
|  | 3345 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
|  | 3346 | if (outputDesc->isDuplicated()) { | 
|  | 3347 | ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice); | 
|  | 3348 | return INVALID_OPERATION; | 
|  | 3349 | } | 
| Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 3350 | status_t status = outputDesc->start(); | 
|  | 3351 | if (status != NO_ERROR) { | 
|  | 3352 | return status; | 
|  | 3353 | } | 
|  | 3354 |  | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3355 | // create a special patch with no sink and two sources: | 
|  | 3356 | // - the second source indicates to PatchPanel through which output mix this patch should | 
|  | 3357 | // be connected as well as the stream type for volume control | 
|  | 3358 | // - the sink is defined by whatever output device is currently selected for the output | 
|  | 3359 | // though which this patch is routed. | 
| Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 3360 | PatchBuilder patchBuilder; | 
|  | 3361 | patchBuilder.addSource(srcDeviceDesc).addSource(outputDesc, { .stream = stream }); | 
|  | 3362 | status = mpClientInterface->createAudioPatch(patchBuilder.patch(), | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3363 | &afPatchHandle, | 
|  | 3364 | 0); | 
|  | 3365 | ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__, | 
|  | 3366 | status, afPatchHandle); | 
| Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 3367 | sourceDesc->mPatchDesc->mPatch = *patchBuilder.patch(); | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3368 | if (status != NO_ERROR) { | 
|  | 3369 | ALOGW("%s patch panel could not connect device patch, error %d", | 
|  | 3370 | __FUNCTION__, status); | 
|  | 3371 | return INVALID_OPERATION; | 
|  | 3372 | } | 
|  | 3373 | uint32_t delayMs = 0; | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3374 | status = startSource(outputDesc, stream, sinkDevice, NULL, &delayMs); | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3375 |  | 
|  | 3376 | if (status != NO_ERROR) { | 
|  | 3377 | mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0); | 
|  | 3378 | return status; | 
|  | 3379 | } | 
|  | 3380 | sourceDesc->mSwOutput = outputDesc; | 
|  | 3381 | if (delayMs != 0) { | 
|  | 3382 | usleep(delayMs * 1000); | 
|  | 3383 | } | 
|  | 3384 | } | 
|  | 3385 |  | 
|  | 3386 | sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle; | 
|  | 3387 | addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc); | 
|  | 3388 |  | 
|  | 3389 | return NO_ERROR; | 
| Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3390 | } | 
|  | 3391 |  | 
| Mikhail Naganov | 9de8bd1 | 2018-07-23 16:41:31 -0700 | [diff] [blame] | 3392 | status_t AudioPolicyManager::stopAudioSource(audio_patch_handle_t handle) | 
| Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3393 | { | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3394 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle); | 
|  | 3395 | ALOGV("%s handle %d", __FUNCTION__, handle); | 
|  | 3396 | if (sourceDesc == 0) { | 
|  | 3397 | ALOGW("%s unknown source for handle %d", __FUNCTION__, handle); | 
|  | 3398 | return BAD_VALUE; | 
|  | 3399 | } | 
|  | 3400 | status_t status = disconnectAudioSource(sourceDesc); | 
|  | 3401 |  | 
|  | 3402 | mAudioSources.removeItem(handle); | 
|  | 3403 | return status; | 
|  | 3404 | } | 
|  | 3405 |  | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3406 | status_t AudioPolicyManager::setMasterMono(bool mono) | 
|  | 3407 | { | 
|  | 3408 | if (mMasterMono == mono) { | 
|  | 3409 | return NO_ERROR; | 
|  | 3410 | } | 
|  | 3411 | mMasterMono = mono; | 
|  | 3412 | // if enabling mono we close all offloaded devices, which will invalidate the | 
|  | 3413 | // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible | 
|  | 3414 | // for recreating the new AudioTrack as non-offloaded PCM. | 
|  | 3415 | // | 
|  | 3416 | // If disabling mono, we leave all tracks as is: we don't know which clients | 
|  | 3417 | // and tracks are able to be recreated as offloaded. The next "song" should | 
|  | 3418 | // play back offloaded. | 
|  | 3419 | if (mMasterMono) { | 
|  | 3420 | Vector<audio_io_handle_t> offloaded; | 
|  | 3421 | for (size_t i = 0; i < mOutputs.size(); ++i) { | 
|  | 3422 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
|  | 3423 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { | 
|  | 3424 | offloaded.push(desc->mIoHandle); | 
|  | 3425 | } | 
|  | 3426 | } | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3427 | for (const auto& handle : offloaded) { | 
|  | 3428 | closeOutput(handle); | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3429 | } | 
|  | 3430 | } | 
|  | 3431 | // update master mono for all remaining outputs | 
|  | 3432 | for (size_t i = 0; i < mOutputs.size(); ++i) { | 
|  | 3433 | updateMono(mOutputs.keyAt(i)); | 
|  | 3434 | } | 
|  | 3435 | return NO_ERROR; | 
|  | 3436 | } | 
|  | 3437 |  | 
|  | 3438 | status_t AudioPolicyManager::getMasterMono(bool *mono) | 
|  | 3439 | { | 
|  | 3440 | *mono = mMasterMono; | 
|  | 3441 | return NO_ERROR; | 
|  | 3442 | } | 
|  | 3443 |  | 
| Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 3444 | float AudioPolicyManager::getStreamVolumeDB( | 
|  | 3445 | audio_stream_type_t stream, int index, audio_devices_t device) | 
|  | 3446 | { | 
|  | 3447 | return computeVolume(stream, index, device); | 
|  | 3448 | } | 
|  | 3449 |  | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3450 | status_t AudioPolicyManager::getSupportedFormats(audio_io_handle_t ioHandle, | 
|  | 3451 | FormatVector& formats) { | 
|  | 3452 | if (ioHandle == AUDIO_IO_HANDLE_NONE) { | 
|  | 3453 | return BAD_VALUE; | 
|  | 3454 | } | 
|  | 3455 | String8 reply; | 
|  | 3456 | reply = mpClientInterface->getParameters( | 
|  | 3457 | ioHandle, String8(AudioParameter::keyStreamSupportedFormats)); | 
|  | 3458 | ALOGV("%s: supported formats %s", __FUNCTION__, reply.string()); | 
|  | 3459 | AudioParameter repliedParameters(reply); | 
|  | 3460 | if (repliedParameters.get( | 
|  | 3461 | String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) { | 
|  | 3462 | ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__); | 
|  | 3463 | return BAD_VALUE; | 
|  | 3464 | } | 
|  | 3465 | for (auto format : formatsFromString(reply.string())) { | 
|  | 3466 | // Only AUDIO_FORMAT_AAC_LC will be used in Settings UI for all AAC formats. | 
|  | 3467 | for (size_t i = 0; i < ARRAY_SIZE(AAC_FORMATS); i++) { | 
|  | 3468 | if (format == AAC_FORMATS[i]) { | 
|  | 3469 | format = AUDIO_FORMAT_AAC_LC; | 
|  | 3470 | break; | 
|  | 3471 | } | 
|  | 3472 | } | 
|  | 3473 | bool exist = false; | 
|  | 3474 | for (size_t i = 0; i < formats.size(); i++) { | 
|  | 3475 | if (format == formats[i]) { | 
|  | 3476 | exist = true; | 
|  | 3477 | break; | 
|  | 3478 | } | 
|  | 3479 | } | 
|  | 3480 | bool isSurroundFormat = false; | 
|  | 3481 | for (size_t i = 0; i < ARRAY_SIZE(SURROUND_FORMATS); i++) { | 
|  | 3482 | if (SURROUND_FORMATS[i] == format) { | 
|  | 3483 | isSurroundFormat = true; | 
|  | 3484 | break; | 
|  | 3485 | } | 
|  | 3486 | } | 
|  | 3487 | if (!exist && isSurroundFormat) { | 
|  | 3488 | formats.add(format); | 
|  | 3489 | } | 
|  | 3490 | } | 
|  | 3491 | return NO_ERROR; | 
|  | 3492 | } | 
|  | 3493 |  | 
|  | 3494 | status_t AudioPolicyManager::getSurroundFormats(unsigned int *numSurroundFormats, | 
|  | 3495 | audio_format_t *surroundFormats, | 
|  | 3496 | bool *surroundFormatsEnabled, | 
|  | 3497 | bool reported) | 
|  | 3498 | { | 
|  | 3499 | if (numSurroundFormats == NULL || (*numSurroundFormats != 0 && | 
|  | 3500 | (surroundFormats == NULL || surroundFormatsEnabled == NULL))) { | 
|  | 3501 | return BAD_VALUE; | 
|  | 3502 | } | 
|  | 3503 | ALOGV("getSurroundFormats() numSurroundFormats %d surroundFormats %p surroundFormatsEnabled %p", | 
|  | 3504 | *numSurroundFormats, surroundFormats, surroundFormatsEnabled); | 
|  | 3505 |  | 
|  | 3506 | // Only return value if there is HDMI output. | 
|  | 3507 | if ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_HDMI) == 0) { | 
|  | 3508 | return INVALID_OPERATION; | 
|  | 3509 | } | 
|  | 3510 |  | 
|  | 3511 | size_t formatsWritten = 0; | 
|  | 3512 | size_t formatsMax = *numSurroundFormats; | 
|  | 3513 | *numSurroundFormats = 0; | 
|  | 3514 | FormatVector formats; | 
|  | 3515 | if (reported) { | 
|  | 3516 | // Only get surround formats which are reported by device. | 
|  | 3517 | // First list already open outputs that can be routed to this device | 
|  | 3518 | audio_devices_t device = AUDIO_DEVICE_OUT_HDMI; | 
|  | 3519 | SortedVector<audio_io_handle_t> outputs; | 
|  | 3520 | bool reportedFormatFound = false; | 
|  | 3521 | status_t status; | 
|  | 3522 | sp<SwAudioOutputDescriptor> desc; | 
|  | 3523 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 3524 | desc = mOutputs.valueAt(i); | 
|  | 3525 | if (!desc->isDuplicated() && (desc->supportedDevices() & device)) { | 
|  | 3526 | outputs.add(mOutputs.keyAt(i)); | 
|  | 3527 | } | 
|  | 3528 | } | 
|  | 3529 | // Open an output to query dynamic parameters. | 
| Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 3530 | DeviceVector hdmiOutputDevices = mAvailableOutputDevices.getDevicesFromTypeMask( | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3531 | AUDIO_DEVICE_OUT_HDMI); | 
|  | 3532 | for (size_t i = 0; i < hdmiOutputDevices.size(); i++) { | 
|  | 3533 | String8 address = hdmiOutputDevices[i]->mAddress; | 
|  | 3534 | for (const auto& hwModule : mHwModules) { | 
|  | 3535 | for (size_t i = 0; i < hwModule->getOutputProfiles().size(); i++) { | 
|  | 3536 | sp<IOProfile> profile = hwModule->getOutputProfiles()[i]; | 
|  | 3537 | if (profile->supportDevice(AUDIO_DEVICE_OUT_HDMI) && | 
|  | 3538 | profile->supportDeviceAddress(address)) { | 
|  | 3539 | size_t j; | 
|  | 3540 | for (j = 0; j < outputs.size(); j++) { | 
|  | 3541 | desc = mOutputs.valueFor(outputs.itemAt(j)); | 
|  | 3542 | if (!desc->isDuplicated() && desc->mProfile == profile) { | 
|  | 3543 | break; | 
|  | 3544 | } | 
|  | 3545 | } | 
|  | 3546 | if (j != outputs.size()) { | 
|  | 3547 | status = getSupportedFormats(outputs.itemAt(j), formats); | 
|  | 3548 | reportedFormatFound |= (status == NO_ERROR); | 
|  | 3549 | continue; | 
|  | 3550 | } | 
|  | 3551 |  | 
|  | 3552 | if (!profile->canOpenNewIo()) { | 
|  | 3553 | ALOGW("Max Output number %u already opened for this profile %s", | 
|  | 3554 | profile->maxOpenCount, profile->getTagName().c_str()); | 
|  | 3555 | continue; | 
|  | 3556 | } | 
|  | 3557 |  | 
|  | 3558 | ALOGV("opening output for device %08x with params %s profile %p name %s", | 
|  | 3559 | device, address.string(), profile.get(), profile->getName().string()); | 
|  | 3560 | desc = new SwAudioOutputDescriptor(profile, mpClientInterface); | 
|  | 3561 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; | 
|  | 3562 | status_t status = desc->open(nullptr, device, address, | 
|  | 3563 | AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, | 
|  | 3564 | &output); | 
|  | 3565 |  | 
|  | 3566 | if (status == NO_ERROR) { | 
|  | 3567 | status = getSupportedFormats(output, formats); | 
|  | 3568 | reportedFormatFound |= (status == NO_ERROR); | 
|  | 3569 | desc->close(); | 
|  | 3570 | output = AUDIO_IO_HANDLE_NONE; | 
|  | 3571 | } | 
|  | 3572 | } | 
|  | 3573 | } | 
|  | 3574 | } | 
|  | 3575 | } | 
|  | 3576 |  | 
|  | 3577 | if (!reportedFormatFound) { | 
|  | 3578 | return UNKNOWN_ERROR; | 
|  | 3579 | } | 
|  | 3580 | } else { | 
|  | 3581 | for (size_t i = 0; i < ARRAY_SIZE(SURROUND_FORMATS); i++) { | 
|  | 3582 | formats.add(SURROUND_FORMATS[i]); | 
|  | 3583 | } | 
|  | 3584 | } | 
|  | 3585 | for (size_t i = 0; i < formats.size(); i++) { | 
|  | 3586 | if (formatsWritten < formatsMax) { | 
|  | 3587 | surroundFormats[formatsWritten] = formats[i]; | 
|  | 3588 | bool formatEnabled = false; | 
|  | 3589 | if (formats[i] == AUDIO_FORMAT_AAC_LC) { | 
|  | 3590 | for (size_t j = 0; j < ARRAY_SIZE(AAC_FORMATS); j++) { | 
|  | 3591 | formatEnabled = | 
|  | 3592 | mSurroundFormats.find(AAC_FORMATS[i]) != mSurroundFormats.end(); | 
|  | 3593 | break; | 
|  | 3594 | } | 
|  | 3595 | } else { | 
|  | 3596 | formatEnabled = mSurroundFormats.find(formats[i]) != mSurroundFormats.end(); | 
|  | 3597 | } | 
|  | 3598 | surroundFormatsEnabled[formatsWritten++] = formatEnabled; | 
|  | 3599 | } | 
|  | 3600 | (*numSurroundFormats)++; | 
|  | 3601 | } | 
|  | 3602 | return NO_ERROR; | 
|  | 3603 | } | 
|  | 3604 |  | 
|  | 3605 | status_t AudioPolicyManager::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) | 
|  | 3606 | { | 
|  | 3607 | // Check if audio format is a surround formats. | 
|  | 3608 | bool isSurroundFormat = false; | 
|  | 3609 | for (size_t i = 0; i < ARRAY_SIZE(SURROUND_FORMATS); i++) { | 
|  | 3610 | if (audioFormat == SURROUND_FORMATS[i]) { | 
|  | 3611 | isSurroundFormat = true; | 
|  | 3612 | break; | 
|  | 3613 | } | 
|  | 3614 | } | 
|  | 3615 | if (!isSurroundFormat) { | 
|  | 3616 | return BAD_VALUE; | 
|  | 3617 | } | 
|  | 3618 |  | 
|  | 3619 | // Should only be called when MANUAL. | 
|  | 3620 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( | 
|  | 3621 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); | 
|  | 3622 | if (forceUse != AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { | 
|  | 3623 | return INVALID_OPERATION; | 
|  | 3624 | } | 
|  | 3625 |  | 
|  | 3626 | if ((mSurroundFormats.find(audioFormat) != mSurroundFormats.end() && enabled) | 
|  | 3627 | || (mSurroundFormats.find(audioFormat) == mSurroundFormats.end() && !enabled)) { | 
|  | 3628 | return NO_ERROR; | 
|  | 3629 | } | 
|  | 3630 |  | 
|  | 3631 | // The operation is valid only when there is HDMI output available. | 
|  | 3632 | if ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_HDMI) == 0) { | 
|  | 3633 | return INVALID_OPERATION; | 
|  | 3634 | } | 
|  | 3635 |  | 
|  | 3636 | if (enabled) { | 
|  | 3637 | if (audioFormat == AUDIO_FORMAT_AAC_LC) { | 
|  | 3638 | for (size_t i = 0; i < ARRAY_SIZE(AAC_FORMATS); i++) { | 
|  | 3639 | mSurroundFormats.insert(AAC_FORMATS[i]); | 
|  | 3640 | } | 
|  | 3641 | } else { | 
|  | 3642 | mSurroundFormats.insert(audioFormat); | 
|  | 3643 | } | 
|  | 3644 | } else { | 
|  | 3645 | if (audioFormat == AUDIO_FORMAT_AAC_LC) { | 
|  | 3646 | for (size_t i = 0; i < ARRAY_SIZE(AAC_FORMATS); i++) { | 
|  | 3647 | mSurroundFormats.erase(AAC_FORMATS[i]); | 
|  | 3648 | } | 
|  | 3649 | } else { | 
|  | 3650 | mSurroundFormats.erase(audioFormat); | 
|  | 3651 | } | 
|  | 3652 | } | 
|  | 3653 |  | 
|  | 3654 | sp<SwAudioOutputDescriptor> outputDesc; | 
|  | 3655 | bool profileUpdated = false; | 
| Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 3656 | DeviceVector hdmiOutputDevices = mAvailableOutputDevices.getDevicesFromTypeMask( | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3657 | AUDIO_DEVICE_OUT_HDMI); | 
|  | 3658 | for (size_t i = 0; i < hdmiOutputDevices.size(); i++) { | 
|  | 3659 | // Simulate reconnection to update enabled surround sound formats. | 
|  | 3660 | String8 address = hdmiOutputDevices[i]->mAddress; | 
|  | 3661 | String8 name = hdmiOutputDevices[i]->getName(); | 
|  | 3662 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, | 
|  | 3663 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, | 
|  | 3664 | address.c_str(), | 
|  | 3665 | name.c_str()); | 
|  | 3666 | if (status != NO_ERROR) { | 
|  | 3667 | continue; | 
|  | 3668 | } | 
|  | 3669 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, | 
|  | 3670 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, | 
|  | 3671 | address.c_str(), | 
|  | 3672 | name.c_str()); | 
|  | 3673 | profileUpdated |= (status == NO_ERROR); | 
|  | 3674 | } | 
| Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 3675 | DeviceVector hdmiInputDevices = mAvailableInputDevices.getDevicesFromTypeMask( | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3676 | AUDIO_DEVICE_IN_HDMI); | 
|  | 3677 | for (size_t i = 0; i < hdmiInputDevices.size(); i++) { | 
|  | 3678 | // Simulate reconnection to update enabled surround sound formats. | 
|  | 3679 | String8 address = hdmiInputDevices[i]->mAddress; | 
|  | 3680 | String8 name = hdmiInputDevices[i]->getName(); | 
|  | 3681 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, | 
|  | 3682 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, | 
|  | 3683 | address.c_str(), | 
|  | 3684 | name.c_str()); | 
|  | 3685 | if (status != NO_ERROR) { | 
|  | 3686 | continue; | 
|  | 3687 | } | 
|  | 3688 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, | 
|  | 3689 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, | 
|  | 3690 | address.c_str(), | 
|  | 3691 | name.c_str()); | 
|  | 3692 | profileUpdated |= (status == NO_ERROR); | 
|  | 3693 | } | 
|  | 3694 |  | 
|  | 3695 | // Undo the surround formats change due to no audio profiles updated. | 
|  | 3696 | if (!profileUpdated) { | 
|  | 3697 | if (enabled) { | 
|  | 3698 | if (audioFormat == AUDIO_FORMAT_AAC_LC) { | 
|  | 3699 | for (size_t i = 0; i < ARRAY_SIZE(AAC_FORMATS); i++) { | 
|  | 3700 | mSurroundFormats.erase(AAC_FORMATS[i]); | 
|  | 3701 | } | 
|  | 3702 | } else { | 
|  | 3703 | mSurroundFormats.erase(audioFormat); | 
|  | 3704 | } | 
|  | 3705 | } else { | 
|  | 3706 | if (audioFormat == AUDIO_FORMAT_AAC_LC) { | 
|  | 3707 | for (size_t i = 0; i < ARRAY_SIZE(AAC_FORMATS); i++) { | 
|  | 3708 | mSurroundFormats.insert(AAC_FORMATS[i]); | 
|  | 3709 | } | 
|  | 3710 | } else { | 
|  | 3711 | mSurroundFormats.insert(audioFormat); | 
|  | 3712 | } | 
|  | 3713 | } | 
|  | 3714 | } | 
|  | 3715 |  | 
|  | 3716 | return profileUpdated ? NO_ERROR : INVALID_OPERATION; | 
|  | 3717 | } | 
|  | 3718 |  | 
| Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 3719 | void AudioPolicyManager::setRecordSilenced(uid_t uid, bool silenced) | 
|  | 3720 | { | 
|  | 3721 | ALOGV("AudioPolicyManager:setRecordSilenced(uid:%d, silenced:%d)", uid, silenced); | 
|  | 3722 |  | 
|  | 3723 | Vector<sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs(); | 
|  | 3724 | for (size_t i = 0; i < activeInputs.size(); i++) { | 
|  | 3725 | sp<AudioInputDescriptor> activeDesc = activeInputs[i]; | 
|  | 3726 | AudioSessionCollection activeSessions = activeDesc->getAudioSessions(true); | 
|  | 3727 | for (size_t j = 0; j < activeSessions.size(); j++) { | 
|  | 3728 | sp<AudioSession> activeSession = activeSessions.valueAt(j); | 
|  | 3729 | if (activeSession->uid() == uid) { | 
|  | 3730 | activeSession->setSilenced(silenced); | 
|  | 3731 | } | 
|  | 3732 | } | 
|  | 3733 | } | 
|  | 3734 | } | 
|  | 3735 |  | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3736 | status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc) | 
|  | 3737 | { | 
|  | 3738 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle()); | 
|  | 3739 |  | 
|  | 3740 | sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle); | 
|  | 3741 | if (patchDesc == 0) { | 
|  | 3742 | ALOGW("%s source has no patch with handle %d", __FUNCTION__, | 
|  | 3743 | sourceDesc->mPatchDesc->mHandle); | 
|  | 3744 | return BAD_VALUE; | 
|  | 3745 | } | 
|  | 3746 | removeAudioPatch(sourceDesc->mPatchDesc->mHandle); | 
|  | 3747 |  | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 3748 | audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes); | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3749 | sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote(); | 
|  | 3750 | if (swOutputDesc != 0) { | 
| Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 3751 | status_t status = stopSource(swOutputDesc, stream, false); | 
|  | 3752 | if (status == NO_ERROR) { | 
|  | 3753 | swOutputDesc->stop(); | 
|  | 3754 | } | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3755 | mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); | 
|  | 3756 | } else { | 
|  | 3757 | sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote(); | 
|  | 3758 | if (hwOutputDesc != 0) { | 
|  | 3759 | //   release patch between src device and output device | 
|  | 3760 | //   close Hwoutput and remove from mHwOutputs | 
|  | 3761 | } else { | 
|  | 3762 | ALOGW("%s source has neither SW nor HW output", __FUNCTION__); | 
|  | 3763 | } | 
|  | 3764 | } | 
|  | 3765 | return NO_ERROR; | 
|  | 3766 | } | 
|  | 3767 |  | 
|  | 3768 | sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput( | 
|  | 3769 | audio_io_handle_t output, routing_strategy strategy) | 
|  | 3770 | { | 
|  | 3771 | sp<AudioSourceDescriptor> source; | 
|  | 3772 | for (size_t i = 0; i < mAudioSources.size(); i++)  { | 
|  | 3773 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); | 
|  | 3774 | routing_strategy sourceStrategy = | 
|  | 3775 | (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes); | 
|  | 3776 | sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote(); | 
|  | 3777 | if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) { | 
|  | 3778 | source = sourceDesc; | 
|  | 3779 | break; | 
|  | 3780 | } | 
|  | 3781 | } | 
|  | 3782 | return source; | 
| Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3783 | } | 
|  | 3784 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3785 | // ---------------------------------------------------------------------------- | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3786 | // AudioPolicyManager | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3787 | // ---------------------------------------------------------------------------- | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3788 | uint32_t AudioPolicyManager::nextAudioPortGeneration() | 
|  | 3789 | { | 
| Mikhail Naganov | 2773dd7 | 2017-12-08 10:12:11 -0800 | [diff] [blame] | 3790 | return mAudioPortGeneration++; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3791 | } | 
|  | 3792 |  | 
| Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3793 | #ifdef USE_XML_AUDIO_POLICY_CONF | 
|  | 3794 | // Treblized audio policy xml config will be located in /odm/etc or /vendor/etc. | 
|  | 3795 | static const char *kConfigLocationList[] = | 
|  | 3796 | {"/odm/etc", "/vendor/etc", "/system/etc"}; | 
|  | 3797 | static const int kConfigLocationListSize = | 
|  | 3798 | (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0])); | 
|  | 3799 |  | 
|  | 3800 | static status_t deserializeAudioPolicyXmlConfig(AudioPolicyConfig &config) { | 
|  | 3801 | char audioPolicyXmlConfigFile[AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH]; | 
| Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3802 | std::vector<const char*> fileNames; | 
| Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3803 | status_t ret; | 
|  | 3804 |  | 
| Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3805 | if (property_get_bool("ro.bluetooth.a2dp_offload.supported", false) && | 
|  | 3806 | property_get_bool("persist.bluetooth.a2dp_offload.disabled", false)) { | 
|  | 3807 | // A2DP offload supported but disabled: try to use special XML file | 
|  | 3808 | fileNames.push_back(AUDIO_POLICY_A2DP_OFFLOAD_DISABLED_XML_CONFIG_FILE_NAME); | 
|  | 3809 | } | 
|  | 3810 | fileNames.push_back(AUDIO_POLICY_XML_CONFIG_FILE_NAME); | 
|  | 3811 |  | 
|  | 3812 | for (const char* fileName : fileNames) { | 
|  | 3813 | for (int i = 0; i < kConfigLocationListSize; i++) { | 
|  | 3814 | PolicySerializer serializer; | 
|  | 3815 | snprintf(audioPolicyXmlConfigFile, sizeof(audioPolicyXmlConfigFile), | 
|  | 3816 | "%s/%s", kConfigLocationList[i], fileName); | 
|  | 3817 | ret = serializer.deserialize(audioPolicyXmlConfigFile, config); | 
|  | 3818 | if (ret == NO_ERROR) { | 
| Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 3819 | config.setSource(audioPolicyXmlConfigFile); | 
| Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3820 | return ret; | 
|  | 3821 | } | 
| Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3822 | } | 
|  | 3823 | } | 
|  | 3824 | return ret; | 
|  | 3825 | } | 
|  | 3826 | #endif | 
|  | 3827 |  | 
| Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3828 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface, | 
|  | 3829 | bool /*forTesting*/) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3830 | : | 
| Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 3831 | mUidCached(AID_AUDIOSERVER), // no need to call getuid(), there's only one of us running. | 
| Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3832 | mpClientInterface(clientInterface), | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3833 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3834 | mA2dpSuspended(false), | 
| Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3835 | #ifdef USE_XML_AUDIO_POLICY_CONF | 
|  | 3836 | mVolumeCurves(new VolumeCurvesCollection()), | 
|  | 3837 | mConfig(mHwModulesAll, mAvailableOutputDevices, mAvailableInputDevices, | 
| Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 3838 | mDefaultOutputDevice, static_cast<VolumeCurvesCollection*>(mVolumeCurves.get())), | 
| Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3839 | #else | 
|  | 3840 | mVolumeCurves(new StreamDescriptorCollection()), | 
|  | 3841 | mConfig(mHwModulesAll, mAvailableOutputDevices, mAvailableInputDevices, | 
|  | 3842 | mDefaultOutputDevice), | 
|  | 3843 | #endif | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 3844 | mAudioPortGeneration(1), | 
|  | 3845 | mBeaconMuteRefCount(0), | 
|  | 3846 | mBeaconPlayingRefCount(0), | 
| Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3847 | mBeaconMuted(false), | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3848 | mTtsOutputAvailable(false), | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3849 | mMasterMono(false), | 
| Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 3850 | mMusicEffectOutput(AUDIO_IO_HANDLE_NONE), | 
|  | 3851 | mHasComputedSoundTriggerSupportsConcurrentCapture(false) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3852 | { | 
| Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3853 | } | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3854 |  | 
| Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3855 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) | 
|  | 3856 | : AudioPolicyManager(clientInterface, false /*forTesting*/) | 
|  | 3857 | { | 
|  | 3858 | loadConfig(); | 
|  | 3859 | initialize(); | 
|  | 3860 | } | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3861 |  | 
| Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3862 | void AudioPolicyManager::loadConfig() { | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3863 | #ifdef USE_XML_AUDIO_POLICY_CONF | 
| Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3864 | if (deserializeAudioPolicyXmlConfig(getConfig()) != NO_ERROR) { | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3865 | #else | 
| Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3866 | if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, getConfig()) != NO_ERROR) | 
|  | 3867 | && (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, getConfig()) != NO_ERROR)) { | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3868 | #endif | 
|  | 3869 | ALOGE("could not load audio policy configuration file, setting defaults"); | 
| Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3870 | getConfig().setDefault(); | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3871 | } | 
| Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3872 | } | 
|  | 3873 |  | 
|  | 3874 | status_t AudioPolicyManager::initialize() { | 
|  | 3875 | mVolumeCurves->initializeVolumeCurves(getConfig().isSpeakerDrcEnabled()); | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3876 |  | 
|  | 3877 | // Once policy config has been parsed, retrieve an instance of the engine and initialize it. | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3878 | audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance(); | 
|  | 3879 | if (!engineInstance) { | 
|  | 3880 | ALOGE("%s:  Could not get an instance of policy engine", __FUNCTION__); | 
| Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3881 | return NO_INIT; | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3882 | } | 
|  | 3883 | // Retrieve the Policy Manager Interface | 
|  | 3884 | mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>(); | 
|  | 3885 | if (mEngine == NULL) { | 
|  | 3886 | ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__); | 
| Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3887 | return NO_INIT; | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3888 | } | 
|  | 3889 | mEngine->setObserver(this); | 
|  | 3890 | status_t status = mEngine->initCheck(); | 
| Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3891 | if (status != NO_ERROR) { | 
|  | 3892 | LOG_FATAL("Policy engine not initialized(err=%d)", status); | 
|  | 3893 | return status; | 
|  | 3894 | } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3895 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3896 | // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3897 | // open all output streams needed to access attached devices | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3898 | audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types(); | 
|  | 3899 | audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; | 
| Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3900 | for (const auto& hwModule : mHwModulesAll) { | 
| Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 3901 | hwModule->setHandle(mpClientInterface->loadHwModule(hwModule->getName())); | 
| Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3902 | if (hwModule->getHandle() == AUDIO_MODULE_HANDLE_NONE) { | 
|  | 3903 | ALOGW("could not open HW module %s", hwModule->getName()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3904 | continue; | 
|  | 3905 | } | 
| Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3906 | mHwModules.push_back(hwModule); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3907 | // open all output streams needed to access attached devices | 
|  | 3908 | // except for direct output streams that are only opened when they are actually | 
|  | 3909 | // required by an app. | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3910 | // This also validates mAvailableOutputDevices list | 
| Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 3911 | for (const auto& outProfile : hwModule->getOutputProfiles()) { | 
| Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 3912 | if (!outProfile->canOpenNewIo()) { | 
|  | 3913 | ALOGE("Invalid Output profile max open count %u for profile %s", | 
|  | 3914 | outProfile->maxOpenCount, outProfile->getTagName().c_str()); | 
|  | 3915 | continue; | 
|  | 3916 | } | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3917 | if (!outProfile->hasSupportedDevices()) { | 
| Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3918 | ALOGW("Output profile contains no device on module %s", hwModule->getName()); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3919 | continue; | 
|  | 3920 | } | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3921 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) { | 
| Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3922 | mTtsOutputAvailable = true; | 
|  | 3923 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3924 |  | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3925 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3926 | continue; | 
|  | 3927 | } | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3928 | audio_devices_t profileType = outProfile->getSupportedDevicesType(); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3929 | if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) { | 
|  | 3930 | profileType = mDefaultOutputDevice->type(); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3931 | } else { | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3932 | // chose first device present in profile's SupportedDevices also part of | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3933 | // outputDeviceTypes | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3934 | profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3935 | } | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3936 | if ((profileType & outputDeviceTypes) == 0) { | 
|  | 3937 | continue; | 
|  | 3938 | } | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3939 | sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile, | 
|  | 3940 | mpClientInterface); | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3941 | const DeviceVector &supportedDevices = outProfile->getSupportedDevices(); | 
| Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 3942 | const DeviceVector &devicesForType = supportedDevices.getDevicesFromTypeMask( | 
|  | 3943 | profileType); | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3944 | String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress | 
|  | 3945 | : String8(""); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3946 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 3947 | status_t status = outputDesc->open(nullptr, profileType, address, | 
|  | 3948 | AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, &output); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3949 |  | 
|  | 3950 | if (status != NO_ERROR) { | 
|  | 3951 | ALOGW("Cannot open output stream for device %08x on hw module %s", | 
|  | 3952 | outputDesc->mDevice, | 
| Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3953 | hwModule->getName()); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3954 | } else { | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3955 | for (const auto& dev : supportedDevices) { | 
|  | 3956 | ssize_t index = mAvailableOutputDevices.indexOf(dev); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3957 | // 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] | 3958 | if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) { | 
| Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3959 | mAvailableOutputDevices[index]->attach(hwModule); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3960 | } | 
|  | 3961 | } | 
|  | 3962 | if (mPrimaryOutput == 0 && | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3963 | outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3964 | mPrimaryOutput = outputDesc; | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3965 | } | 
|  | 3966 | addOutput(output, outputDesc); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3967 | setOutputDevice(outputDesc, | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 3968 | profileType, | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3969 | true, | 
|  | 3970 | 0, | 
|  | 3971 | NULL, | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 3972 | address); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3973 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3974 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3975 | // open input streams needed to access attached devices to validate | 
|  | 3976 | // mAvailableInputDevices list | 
| Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 3977 | for (const auto& inProfile : hwModule->getInputProfiles()) { | 
| Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 3978 | if (!inProfile->canOpenNewIo()) { | 
|  | 3979 | ALOGE("Invalid Input profile max open count %u for profile %s", | 
|  | 3980 | inProfile->maxOpenCount, inProfile->getTagName().c_str()); | 
|  | 3981 | continue; | 
|  | 3982 | } | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3983 | if (!inProfile->hasSupportedDevices()) { | 
| Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3984 | ALOGW("Input profile contains no device on module %s", hwModule->getName()); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3985 | continue; | 
|  | 3986 | } | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3987 | // chose first device present in profile's SupportedDevices also part of | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3988 | // inputDeviceTypes | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3989 | audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes); | 
|  | 3990 |  | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3991 | if ((profileType & inputDeviceTypes) == 0) { | 
|  | 3992 | continue; | 
|  | 3993 | } | 
| Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 3994 | sp<AudioInputDescriptor> inputDesc = | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 3995 | new AudioInputDescriptor(inProfile, mpClientInterface); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3996 |  | 
| Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 3997 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromTypeMask(profileType); | 
| Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 3998 | //   the inputs vector must be of size >= 1, but we don't want to crash here | 
|  | 3999 | String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress | 
|  | 4000 | : String8(""); | 
|  | 4001 | ALOGV("  for input device 0x%x using address %s", profileType, address.string()); | 
|  | 4002 | ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!"); | 
|  | 4003 |  | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4004 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4005 | status_t status = inputDesc->open(nullptr, | 
|  | 4006 | profileType, | 
| Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 4007 | address, | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4008 | AUDIO_SOURCE_MIC, | 
|  | 4009 | AUDIO_INPUT_FLAG_NONE, | 
|  | 4010 | &input); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4011 |  | 
|  | 4012 | if (status == NO_ERROR) { | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4013 | for (const auto& dev : inProfile->getSupportedDevices()) { | 
|  | 4014 | ssize_t index = mAvailableInputDevices.indexOf(dev); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4015 | // 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] | 4016 | if (index >= 0) { | 
|  | 4017 | sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index]; | 
|  | 4018 | if (!devDesc->isAttached()) { | 
| Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4019 | devDesc->attach(hwModule); | 
| Eric Laurent | 83efe1c | 2017-07-09 16:51:08 -0700 | [diff] [blame] | 4020 | devDesc->importAudioPort(inProfile, true); | 
| Eric Laurent | 45aabc3 | 2015-08-06 09:11:13 -0700 | [diff] [blame] | 4021 | } | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4022 | } | 
|  | 4023 | } | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4024 | inputDesc->close(); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4025 | } else { | 
|  | 4026 | ALOGW("Cannot open input stream for device %08x on hw module %s", | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4027 | profileType, | 
| Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4028 | hwModule->getName()); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4029 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4030 | } | 
|  | 4031 | } | 
|  | 4032 | // make sure all attached devices have been allocated a unique ID | 
|  | 4033 | for (size_t i = 0; i  < mAvailableOutputDevices.size();) { | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 4034 | if (!mAvailableOutputDevices[i]->isAttached()) { | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4035 | ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type()); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4036 | mAvailableOutputDevices.remove(mAvailableOutputDevices[i]); | 
|  | 4037 | continue; | 
|  | 4038 | } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4039 | // The device is now validated and can be appended to the available devices of the engine | 
|  | 4040 | mEngine->setDeviceConnectionState(mAvailableOutputDevices[i], | 
|  | 4041 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4042 | i++; | 
|  | 4043 | } | 
|  | 4044 | for (size_t i = 0; i  < mAvailableInputDevices.size();) { | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 4045 | if (!mAvailableInputDevices[i]->isAttached()) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4046 | ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type()); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4047 | mAvailableInputDevices.remove(mAvailableInputDevices[i]); | 
|  | 4048 | continue; | 
|  | 4049 | } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4050 | // The device is now validated and can be appended to the available devices of the engine | 
|  | 4051 | mEngine->setDeviceConnectionState(mAvailableInputDevices[i], | 
|  | 4052 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4053 | i++; | 
|  | 4054 | } | 
|  | 4055 | // make sure default device is reachable | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4056 | if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4057 | ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type()); | 
| Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4058 | status = NO_INIT; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4059 | } | 
| jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 4060 | // If microphones address is empty, set it according to device type | 
|  | 4061 | for (size_t i = 0; i  < mAvailableInputDevices.size(); i++) { | 
|  | 4062 | if (mAvailableInputDevices[i]->mAddress.isEmpty()) { | 
|  | 4063 | if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BUILTIN_MIC) { | 
|  | 4064 | mAvailableInputDevices[i]->mAddress = String8(AUDIO_BOTTOM_MICROPHONE_ADDRESS); | 
|  | 4065 | } else if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BACK_MIC) { | 
|  | 4066 | mAvailableInputDevices[i]->mAddress = String8(AUDIO_BACK_MICROPHONE_ADDRESS); | 
|  | 4067 | } | 
|  | 4068 | } | 
|  | 4069 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4070 |  | 
| Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4071 | if (mPrimaryOutput == 0) { | 
|  | 4072 | ALOGE("Failed to open primary output"); | 
|  | 4073 | status = NO_INIT; | 
|  | 4074 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4075 |  | 
|  | 4076 | updateDevicesAndOutputs(); | 
| Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4077 | return status; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4078 | } | 
|  | 4079 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4080 | AudioPolicyManager::~AudioPolicyManager() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4081 | { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4082 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4083 | mOutputs.valueAt(i)->close(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4084 | } | 
|  | 4085 | for (size_t i = 0; i < mInputs.size(); i++) { | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4086 | mInputs.valueAt(i)->close(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4087 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4088 | mAvailableOutputDevices.clear(); | 
|  | 4089 | mAvailableInputDevices.clear(); | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4090 | mOutputs.clear(); | 
|  | 4091 | mInputs.clear(); | 
|  | 4092 | mHwModules.clear(); | 
| Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4093 | mHwModulesAll.clear(); | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4094 | mSurroundFormats.clear(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4095 | } | 
|  | 4096 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4097 | status_t AudioPolicyManager::initCheck() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4098 | { | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 4099 | return hasPrimaryOutput() ? NO_ERROR : NO_INIT; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4100 | } | 
|  | 4101 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4102 | // --- | 
|  | 4103 |  | 
| Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 4104 | void AudioPolicyManager::addOutput(audio_io_handle_t output, | 
|  | 4105 | const sp<SwAudioOutputDescriptor>& outputDesc) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4106 | { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4107 | mOutputs.add(output, outputDesc); | 
| Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 4108 | applyStreamVolumes(outputDesc, AUDIO_DEVICE_NONE, 0 /* delayMs */, true /* force */); | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4109 | updateMono(output); // update mono status when adding to output list | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4110 | selectOutputForMusicEffects(); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4111 | nextAudioPortGeneration(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4112 | } | 
|  | 4113 |  | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4114 | void AudioPolicyManager::removeOutput(audio_io_handle_t output) | 
|  | 4115 | { | 
|  | 4116 | mOutputs.removeItem(output); | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4117 | selectOutputForMusicEffects(); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4118 | } | 
|  | 4119 |  | 
| Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 4120 | void AudioPolicyManager::addInput(audio_io_handle_t input, | 
|  | 4121 | const sp<AudioInputDescriptor>& inputDesc) | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4122 | { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4123 | mInputs.add(input, inputDesc); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4124 | nextAudioPortGeneration(); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4125 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4126 |  | 
| Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4127 | void AudioPolicyManager::findIoHandlesByAddress(const sp<SwAudioOutputDescriptor>& desc /*in*/, | 
| keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4128 | const audio_devices_t device /*in*/, | 
| Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4129 | const String8& address /*in*/, | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4130 | SortedVector<audio_io_handle_t>& outputs /*out*/) { | 
| keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4131 | sp<DeviceDescriptor> devDesc = | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4132 | desc->mProfile->getSupportedDeviceByAddress(device, address); | 
| keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4133 | if (devDesc != 0) { | 
|  | 4134 | ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s", | 
|  | 4135 | desc->mIoHandle, address.string()); | 
|  | 4136 | outputs.add(desc->mIoHandle); | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4137 | } | 
|  | 4138 | } | 
|  | 4139 |  | 
| Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4140 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& devDesc, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4141 | audio_policy_dev_state_t state, | 
|  | 4142 | SortedVector<audio_io_handle_t>& outputs, | 
| Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4143 | const String8& address) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4144 | { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4145 | audio_devices_t device = devDesc->type(); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4146 | sp<SwAudioOutputDescriptor> desc; | 
| Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4147 |  | 
|  | 4148 | if (audio_device_is_digital(device)) { | 
|  | 4149 | // erase all current sample rates, formats and channel masks | 
| Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 4150 | devDesc->clearAudioProfiles(); | 
| Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4151 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4152 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4153 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4154 | // first list already open outputs that can be routed to this device | 
|  | 4155 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 4156 | desc = mOutputs.valueAt(i); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4157 | if (!desc->isDuplicated() && (desc->supportedDevices() & device)) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4158 | if (!device_distinguishes_on_address(device)) { | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4159 | ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i)); | 
|  | 4160 | outputs.add(mOutputs.keyAt(i)); | 
|  | 4161 | } else { | 
|  | 4162 | ALOGV("  checking address match due to device 0x%x", device); | 
| keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4163 | findIoHandlesByAddress(desc, device, address, outputs); | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4164 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4165 | } | 
|  | 4166 | } | 
|  | 4167 | // then look for output profiles that can be routed to this device | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4168 | SortedVector< sp<IOProfile> > profiles; | 
| Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4169 | for (const auto& hwModule : mHwModules) { | 
| Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4170 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { | 
|  | 4171 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4172 | if (profile->supportDevice(device)) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4173 | if (!device_distinguishes_on_address(device) || | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4174 | profile->supportDeviceAddress(address)) { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4175 | profiles.add(profile); | 
| Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4176 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %s", | 
|  | 4177 | j, hwModule->getName()); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4178 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4179 | } | 
|  | 4180 | } | 
|  | 4181 | } | 
|  | 4182 |  | 
| Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 4183 | ALOGV("  found %zu profiles, %zu outputs", profiles.size(), outputs.size()); | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4184 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4185 | if (profiles.isEmpty() && outputs.isEmpty()) { | 
|  | 4186 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); | 
|  | 4187 | return BAD_VALUE; | 
|  | 4188 | } | 
|  | 4189 |  | 
|  | 4190 | // open outputs for matching profiles if needed. Direct outputs are also opened to | 
|  | 4191 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() | 
|  | 4192 | 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] | 4193 | sp<IOProfile> profile = profiles[profile_index]; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4194 |  | 
|  | 4195 | // nothing to do if one output is already opened for this profile | 
|  | 4196 | size_t j; | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4197 | for (j = 0; j < outputs.size(); j++) { | 
|  | 4198 | desc = mOutputs.valueFor(outputs.itemAt(j)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4199 | if (!desc->isDuplicated() && desc->mProfile == profile) { | 
| Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 4200 | // matching profile: save the sample rates, format and channel masks supported | 
|  | 4201 | // by the profile in our device descriptor | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4202 | if (audio_device_is_digital(device)) { | 
|  | 4203 | devDesc->importAudioPort(profile); | 
|  | 4204 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4205 | break; | 
|  | 4206 | } | 
|  | 4207 | } | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4208 | if (j != outputs.size()) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4209 | continue; | 
|  | 4210 | } | 
|  | 4211 |  | 
| Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4212 | if (!profile->canOpenNewIo()) { | 
|  | 4213 | ALOGW("Max Output number %u already opened for this profile %s", | 
|  | 4214 | profile->maxOpenCount, profile->getTagName().c_str()); | 
|  | 4215 | continue; | 
|  | 4216 | } | 
|  | 4217 |  | 
| Eric Laurent | 83efe1c | 2017-07-09 16:51:08 -0700 | [diff] [blame] | 4218 | ALOGV("opening output for device %08x with params %s profile %p name %s", | 
|  | 4219 | device, address.string(), profile.get(), profile->getName().string()); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4220 | desc = new SwAudioOutputDescriptor(profile, mpClientInterface); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4221 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4222 | status_t status = desc->open(nullptr, device, address, | 
|  | 4223 | AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, &output); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4224 |  | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4225 | if (status == NO_ERROR) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4226 | // Here is where the out_set_parameters() for card & device gets called | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4227 | if (!address.isEmpty()) { | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4228 | char *param = audio_device_address_to_parameter(device, address); | 
|  | 4229 | mpClientInterface->setParameters(output, String8(param)); | 
|  | 4230 | free(param); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4231 | } | 
| Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 4232 | updateAudioProfiles(device, output, profile->getAudioProfiles()); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4233 | if (!profile->hasValidAudioProfile()) { | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4234 | ALOGW("checkOutputsForDevice() missing param"); | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4235 | desc->close(); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4236 | output = AUDIO_IO_HANDLE_NONE; | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4237 | } else if (profile->hasDynamicAudioProfile()) { | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4238 | desc->close(); | 
| Phil Burk | 702b105 | 2016-03-02 16:38:26 -0800 | [diff] [blame] | 4239 | output = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4240 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
|  | 4241 | profile->pickAudioProfile( | 
|  | 4242 | config.sample_rate, config.channel_mask, config.format); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4243 | config.offload_info.sample_rate = config.sample_rate; | 
|  | 4244 | config.offload_info.channel_mask = config.channel_mask; | 
|  | 4245 | config.offload_info.format = config.format; | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4246 |  | 
|  | 4247 | status_t status = desc->open(&config, device, address, AUDIO_STREAM_DEFAULT, | 
|  | 4248 | AUDIO_OUTPUT_FLAG_NONE, &output); | 
|  | 4249 | if (status != NO_ERROR) { | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4250 | output = AUDIO_IO_HANDLE_NONE; | 
|  | 4251 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4252 | } | 
|  | 4253 |  | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4254 | if (output != AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4255 | addOutput(output, desc); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4256 | if (device_distinguishes_on_address(device) && address != "0") { | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4257 | sp<AudioPolicyMix> policyMix; | 
|  | 4258 | if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4259 | ALOGE("checkOutputsForDevice() cannot find policy for address %s", | 
|  | 4260 | address.string()); | 
|  | 4261 | } | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4262 | policyMix->setOutput(desc); | 
| Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 4263 | desc->mPolicyMix = policyMix->getMix(); | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4264 |  | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 4265 | } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && | 
|  | 4266 | hasPrimaryOutput()) { | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 4267 | // no duplicated output for direct outputs and | 
|  | 4268 | // outputs used by dynamic policy mixes | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4269 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4270 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4271 | //TODO: configure audio effect output stage here | 
|  | 4272 |  | 
|  | 4273 | // open a duplicating output thread for the new output and the primary output | 
| Eric Laurent | 5babc4f | 2018-02-15 12:33:44 -0800 | [diff] [blame] | 4274 | sp<SwAudioOutputDescriptor> dupOutputDesc = | 
|  | 4275 | new SwAudioOutputDescriptor(NULL, mpClientInterface); | 
|  | 4276 | status_t status = dupOutputDesc->openDuplicating(mPrimaryOutput, desc, | 
|  | 4277 | &duplicatedOutput); | 
|  | 4278 | if (status == NO_ERROR) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4279 | // add duplicated output descriptor | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4280 | addOutput(duplicatedOutput, dupOutputDesc); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4281 | } else { | 
|  | 4282 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4283 | mPrimaryOutput->mIoHandle, output); | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4284 | desc->close(); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4285 | removeOutput(output); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4286 | nextAudioPortGeneration(); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4287 | output = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4288 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4289 | } | 
|  | 4290 | } | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4291 | } else { | 
|  | 4292 | output = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4293 | } | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4294 | if (output == AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4295 | ALOGW("checkOutputsForDevice() could not open output for device %x", device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4296 | profiles.removeAt(profile_index); | 
|  | 4297 | profile_index--; | 
|  | 4298 | } else { | 
|  | 4299 | outputs.add(output); | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4300 | // Load digital format info only for digital devices | 
|  | 4301 | if (audio_device_is_digital(device)) { | 
|  | 4302 | devDesc->importAudioPort(profile); | 
|  | 4303 | } | 
| Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 4304 |  | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4305 | if (device_distinguishes_on_address(device)) { | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4306 | ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)", | 
|  | 4307 | device, address.string()); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4308 | setOutputDevice(desc, device, true/*force*/, 0/*delay*/, | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4309 | NULL/*patch handle*/, address.string()); | 
|  | 4310 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4311 | ALOGV("checkOutputsForDevice(): adding output %d", output); | 
|  | 4312 | } | 
|  | 4313 | } | 
|  | 4314 |  | 
|  | 4315 | if (profiles.isEmpty()) { | 
|  | 4316 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); | 
|  | 4317 | return BAD_VALUE; | 
|  | 4318 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4319 | } else { // Disconnect | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4320 | // check if one opened output is not needed any more after disconnecting one device | 
|  | 4321 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 4322 | desc = mOutputs.valueAt(i); | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4323 | if (!desc->isDuplicated()) { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4324 | // exact match on device | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4325 | if (device_distinguishes_on_address(device) && | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4326 | (desc->supportedDevices() == device)) { | 
| keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4327 | findIoHandlesByAddress(desc, device, address, outputs); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4328 | } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) { | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4329 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", | 
|  | 4330 | mOutputs.keyAt(i)); | 
|  | 4331 | outputs.add(mOutputs.keyAt(i)); | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4332 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4333 | } | 
|  | 4334 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4335 | // Clear any profiles associated with the disconnected device. | 
| Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4336 | for (const auto& hwModule : mHwModules) { | 
| Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4337 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { | 
|  | 4338 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4339 | if (profile->supportDevice(device)) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4340 | ALOGV("checkOutputsForDevice(): " | 
| Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4341 | "clearing direct output profile %zu on module %s", | 
|  | 4342 | j, hwModule->getName()); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4343 | profile->clearAudioProfiles(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4344 | } | 
|  | 4345 | } | 
|  | 4346 | } | 
|  | 4347 | } | 
|  | 4348 | return NO_ERROR; | 
|  | 4349 | } | 
|  | 4350 |  | 
| Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4351 | status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& devDesc, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4352 | audio_policy_dev_state_t state, | 
|  | 4353 | SortedVector<audio_io_handle_t>& inputs, | 
| Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4354 | const String8& address) | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4355 | { | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4356 | audio_devices_t device = devDesc->type(); | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4357 | sp<AudioInputDescriptor> desc; | 
| Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4358 |  | 
|  | 4359 | if (audio_device_is_digital(device)) { | 
|  | 4360 | // erase all current sample rates, formats and channel masks | 
| Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 4361 | devDesc->clearAudioProfiles(); | 
| Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4362 | } | 
|  | 4363 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4364 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { | 
|  | 4365 | // first list already open inputs that can be routed to this device | 
|  | 4366 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { | 
|  | 4367 | desc = mInputs.valueAt(input_index); | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4368 | if (desc->mProfile->supportDevice(device)) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4369 | ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index)); | 
|  | 4370 | inputs.add(mInputs.keyAt(input_index)); | 
|  | 4371 | } | 
|  | 4372 | } | 
|  | 4373 |  | 
|  | 4374 | // then look for input profiles that can be routed to this device | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4375 | SortedVector< sp<IOProfile> > profiles; | 
| Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4376 | for (const auto& hwModule : mHwModules) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4377 | for (size_t profile_index = 0; | 
| Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4378 | profile_index < hwModule->getInputProfiles().size(); | 
| Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4379 | profile_index++) { | 
| Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4380 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4381 |  | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4382 | if (profile->supportDevice(device)) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4383 | if (!device_distinguishes_on_address(device) || | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4384 | profile->supportDeviceAddress(address)) { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4385 | profiles.add(profile); | 
| Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4386 | ALOGV("checkInputsForDevice(): adding profile %zu from module %s", | 
|  | 4387 | profile_index, hwModule->getName()); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4388 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4389 | } | 
|  | 4390 | } | 
|  | 4391 | } | 
|  | 4392 |  | 
|  | 4393 | if (profiles.isEmpty() && inputs.isEmpty()) { | 
|  | 4394 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); | 
|  | 4395 | return BAD_VALUE; | 
|  | 4396 | } | 
|  | 4397 |  | 
|  | 4398 | // open inputs for matching profiles if needed. Direct inputs are also opened to | 
|  | 4399 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() | 
|  | 4400 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { | 
|  | 4401 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4402 | sp<IOProfile> profile = profiles[profile_index]; | 
| Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4403 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4404 | // nothing to do if one input is already opened for this profile | 
|  | 4405 | size_t input_index; | 
|  | 4406 | for (input_index = 0; input_index < mInputs.size(); input_index++) { | 
|  | 4407 | desc = mInputs.valueAt(input_index); | 
|  | 4408 | if (desc->mProfile == profile) { | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4409 | if (audio_device_is_digital(device)) { | 
|  | 4410 | devDesc->importAudioPort(profile); | 
|  | 4411 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4412 | break; | 
|  | 4413 | } | 
|  | 4414 | } | 
|  | 4415 | if (input_index != mInputs.size()) { | 
|  | 4416 | continue; | 
|  | 4417 | } | 
|  | 4418 |  | 
| Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4419 | if (!profile->canOpenNewIo()) { | 
|  | 4420 | ALOGW("Max Input number %u already opened for this profile %s", | 
|  | 4421 | profile->maxOpenCount, profile->getTagName().c_str()); | 
|  | 4422 | continue; | 
|  | 4423 | } | 
|  | 4424 |  | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4425 | desc = new AudioInputDescriptor(profile, mpClientInterface); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4426 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4427 | status_t status = desc->open(nullptr, | 
|  | 4428 | device, | 
|  | 4429 | address, | 
|  | 4430 | AUDIO_SOURCE_MIC, | 
|  | 4431 | AUDIO_INPUT_FLAG_NONE, | 
|  | 4432 | &input); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4433 |  | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4434 | if (status == NO_ERROR) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4435 | if (!address.isEmpty()) { | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4436 | char *param = audio_device_address_to_parameter(device, address); | 
|  | 4437 | mpClientInterface->setParameters(input, String8(param)); | 
|  | 4438 | free(param); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4439 | } | 
| Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 4440 | updateAudioProfiles(device, input, profile->getAudioProfiles()); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4441 | if (!profile->hasValidAudioProfile()) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4442 | ALOGW("checkInputsForDevice() direct input missing param"); | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4443 | desc->close(); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4444 | input = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4445 | } | 
|  | 4446 |  | 
|  | 4447 | if (input != 0) { | 
|  | 4448 | addInput(input, desc); | 
|  | 4449 | } | 
|  | 4450 | } // endif input != 0 | 
|  | 4451 |  | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4452 | if (input == AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4453 | ALOGW("checkInputsForDevice() could not open input for device 0x%X", device); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4454 | profiles.removeAt(profile_index); | 
|  | 4455 | profile_index--; | 
|  | 4456 | } else { | 
|  | 4457 | inputs.add(input); | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4458 | if (audio_device_is_digital(device)) { | 
|  | 4459 | devDesc->importAudioPort(profile); | 
|  | 4460 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4461 | ALOGV("checkInputsForDevice(): adding input %d", input); | 
|  | 4462 | } | 
|  | 4463 | } // end scan profiles | 
|  | 4464 |  | 
|  | 4465 | if (profiles.isEmpty()) { | 
|  | 4466 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); | 
|  | 4467 | return BAD_VALUE; | 
|  | 4468 | } | 
|  | 4469 | } else { | 
|  | 4470 | // Disconnect | 
|  | 4471 | // check if one opened input is not needed any more after disconnecting one device | 
|  | 4472 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { | 
|  | 4473 | desc = mInputs.valueAt(input_index); | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4474 | if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4475 | ALOGV("checkInputsForDevice(): disconnecting adding input %d", | 
|  | 4476 | mInputs.keyAt(input_index)); | 
|  | 4477 | inputs.add(mInputs.keyAt(input_index)); | 
|  | 4478 | } | 
|  | 4479 | } | 
|  | 4480 | // Clear any profiles associated with the disconnected device. | 
| Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4481 | for (const auto& hwModule : mHwModules) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4482 | for (size_t profile_index = 0; | 
| Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4483 | profile_index < hwModule->getInputProfiles().size(); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4484 | profile_index++) { | 
| Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4485 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4486 | if (profile->supportDevice(device)) { | 
| Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4487 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %s", | 
|  | 4488 | profile_index, hwModule->getName()); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4489 | profile->clearAudioProfiles(); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4490 | } | 
|  | 4491 | } | 
|  | 4492 | } | 
|  | 4493 | } // end disconnect | 
|  | 4494 |  | 
|  | 4495 | return NO_ERROR; | 
|  | 4496 | } | 
|  | 4497 |  | 
|  | 4498 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4499 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4500 | { | 
|  | 4501 | ALOGV("closeOutput(%d)", output); | 
|  | 4502 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4503 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4504 | if (outputDesc == NULL) { | 
|  | 4505 | ALOGW("closeOutput() unknown output %d", output); | 
|  | 4506 | return; | 
|  | 4507 | } | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4508 | mPolicyMixes.closeOutput(outputDesc); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4509 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4510 | // look for duplicated outputs connected to the output being removed. | 
|  | 4511 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4512 | sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4513 | if (dupOutputDesc->isDuplicated() && | 
|  | 4514 | (dupOutputDesc->mOutput1 == outputDesc || | 
|  | 4515 | dupOutputDesc->mOutput2 == outputDesc)) { | 
| Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4516 | sp<SwAudioOutputDescriptor> outputDesc2; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4517 | if (dupOutputDesc->mOutput1 == outputDesc) { | 
|  | 4518 | outputDesc2 = dupOutputDesc->mOutput2; | 
|  | 4519 | } else { | 
|  | 4520 | outputDesc2 = dupOutputDesc->mOutput1; | 
|  | 4521 | } | 
|  | 4522 | // As all active tracks on duplicated output will be deleted, | 
|  | 4523 | // and as they were also referenced on the other output, the reference | 
|  | 4524 | // count for their stream type must be adjusted accordingly on | 
|  | 4525 | // the other output. | 
| Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4526 | bool wasActive = outputDesc2->isActive(); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4527 | for (int j = 0; j < AUDIO_STREAM_CNT; j++) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4528 | int refCount = dupOutputDesc->mRefCount[j]; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4529 | outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4530 | } | 
| Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4531 | // stop() will be a no op if the output is still active but is needed in case all | 
|  | 4532 | // active streams refcounts where cleared above | 
|  | 4533 | if (wasActive) { | 
|  | 4534 | outputDesc2->stop(); | 
|  | 4535 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4536 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); | 
|  | 4537 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); | 
|  | 4538 |  | 
|  | 4539 | mpClientInterface->closeOutput(duplicatedOutput); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4540 | removeOutput(duplicatedOutput); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4541 | } | 
|  | 4542 | } | 
|  | 4543 |  | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4544 | nextAudioPortGeneration(); | 
|  | 4545 |  | 
| Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4546 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4547 | if (index >= 0) { | 
|  | 4548 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
| Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4549 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4550 | mAudioPatches.removeItemsAt(index); | 
|  | 4551 | mpClientInterface->onAudioPatchListUpdate(); | 
|  | 4552 | } | 
|  | 4553 |  | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4554 | outputDesc->close(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4555 |  | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4556 | removeOutput(output); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4557 | mPreviousOutputs = mOutputs; | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4558 | } | 
|  | 4559 |  | 
|  | 4560 | void AudioPolicyManager::closeInput(audio_io_handle_t input) | 
|  | 4561 | { | 
|  | 4562 | ALOGV("closeInput(%d)", input); | 
|  | 4563 |  | 
|  | 4564 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); | 
|  | 4565 | if (inputDesc == NULL) { | 
|  | 4566 | ALOGW("closeInput() unknown input %d", input); | 
|  | 4567 | return; | 
|  | 4568 | } | 
|  | 4569 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4570 | nextAudioPortGeneration(); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4571 |  | 
| Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4572 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4573 | if (index >= 0) { | 
|  | 4574 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
| Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4575 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4576 | mAudioPatches.removeItemsAt(index); | 
|  | 4577 | mpClientInterface->onAudioPatchListUpdate(); | 
|  | 4578 | } | 
|  | 4579 |  | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4580 | inputDesc->close(); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4581 | mInputs.removeItem(input); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4582 | } | 
|  | 4583 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4584 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice( | 
|  | 4585 | audio_devices_t device, | 
| Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4586 | const SwAudioOutputCollection& openOutputs) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4587 | { | 
|  | 4588 | SortedVector<audio_io_handle_t> outputs; | 
|  | 4589 |  | 
|  | 4590 | ALOGVV("getOutputsForDevice() device %04x", device); | 
|  | 4591 | for (size_t i = 0; i < openOutputs.size(); i++) { | 
| Eric Laurent | 37ddbb4 | 2016-08-10 16:19:14 -0700 | [diff] [blame] | 4592 | ALOGVV("output %zu isDuplicated=%d device=%04x", | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4593 | i, openOutputs.valueAt(i)->isDuplicated(), | 
|  | 4594 | openOutputs.valueAt(i)->supportedDevices()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4595 | if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) { | 
|  | 4596 | ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i)); | 
|  | 4597 | outputs.add(openOutputs.keyAt(i)); | 
|  | 4598 | } | 
|  | 4599 | } | 
|  | 4600 | return outputs; | 
|  | 4601 | } | 
|  | 4602 |  | 
| Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 4603 | void AudioPolicyManager::checkForDeviceAndOutputChanges() | 
|  | 4604 | { | 
|  | 4605 | checkForDeviceAndOutputChanges([](){ return false; }); | 
|  | 4606 | } | 
|  | 4607 |  | 
|  | 4608 | void AudioPolicyManager::checkForDeviceAndOutputChanges(std::function<bool()> onOutputsChecked) | 
|  | 4609 | { | 
|  | 4610 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP | 
|  | 4611 | // output is suspended before any tracks are moved to it | 
|  | 4612 | checkA2dpSuspend(); | 
|  | 4613 | checkOutputForAllStrategies(); | 
|  | 4614 | if (onOutputsChecked()) checkA2dpSuspend(); | 
|  | 4615 | updateDevicesAndOutputs(); | 
|  | 4616 | } | 
|  | 4617 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4618 | void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4619 | { | 
|  | 4620 | audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/); | 
|  | 4621 | audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/); | 
|  | 4622 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs); | 
|  | 4623 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs); | 
|  | 4624 |  | 
| Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4625 | // also take into account external policy-related changes: add all outputs which are | 
|  | 4626 | // associated with policies in the "before" and "after" output vectors | 
|  | 4627 | ALOGVV("checkOutputForStrategy(): policy related outputs"); | 
|  | 4628 | for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4629 | const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i); | 
| Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4630 | if (desc != 0 && desc->mPolicyMix != NULL) { | 
|  | 4631 | srcOutputs.add(desc->mIoHandle); | 
|  | 4632 | ALOGVV(" previous outputs: adding %d", desc->mIoHandle); | 
|  | 4633 | } | 
|  | 4634 | } | 
|  | 4635 | for (size_t i = 0 ; i < mOutputs.size() ; i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4636 | const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4637 | if (desc != 0 && desc->mPolicyMix != NULL) { | 
|  | 4638 | dstOutputs.add(desc->mIoHandle); | 
|  | 4639 | ALOGVV(" new outputs: adding %d", desc->mIoHandle); | 
|  | 4640 | } | 
|  | 4641 | } | 
|  | 4642 |  | 
| Mikhail Naganov | 9de8bd1 | 2018-07-23 16:41:31 -0700 | [diff] [blame] | 4643 | if (srcOutputs != dstOutputs) { | 
| Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 4644 | // get maximum latency of all source outputs to determine the minimum mute time guaranteeing | 
|  | 4645 | // audio from invalidated tracks will be rendered when unmuting | 
|  | 4646 | uint32_t maxLatency = 0; | 
|  | 4647 | for (audio_io_handle_t srcOut : srcOutputs) { | 
|  | 4648 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); | 
|  | 4649 | if (desc != 0 && maxLatency < desc->latency()) { | 
|  | 4650 | maxLatency = desc->latency(); | 
|  | 4651 | } | 
|  | 4652 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4653 | ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d", | 
|  | 4654 | strategy, srcOutputs[0], dstOutputs[0]); | 
|  | 4655 | // mute strategy while moving tracks from one output to another | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4656 | for (audio_io_handle_t srcOut : srcOutputs) { | 
| Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 4657 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); | 
|  | 4658 | if (desc != 0 && isStrategyActive(desc, strategy)) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4659 | setStrategyMute(strategy, true, desc); | 
| Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 4660 | setStrategyMute(strategy, false, desc, maxLatency * LATENCY_MUTE_FACTOR, newDevice); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4661 | } | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4662 | sp<AudioSourceDescriptor> source = | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4663 | getSourceForStrategyOnOutput(srcOut, strategy); | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4664 | if (source != 0){ | 
|  | 4665 | connectAudioSource(source); | 
|  | 4666 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4667 | } | 
|  | 4668 |  | 
|  | 4669 | // Move effects associated to this strategy from previous output to new output | 
|  | 4670 | if (strategy == STRATEGY_MEDIA) { | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4671 | selectOutputForMusicEffects(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4672 | } | 
|  | 4673 | // Move tracks associated to this strategy from previous output to new output | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4674 | for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4675 | if (getStrategy((audio_stream_type_t)i) == strategy) { | 
|  | 4676 | mpClientInterface->invalidateStream((audio_stream_type_t)i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4677 | } | 
|  | 4678 | } | 
|  | 4679 | } | 
|  | 4680 | } | 
|  | 4681 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4682 | void AudioPolicyManager::checkOutputForAllStrategies() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4683 | { | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4684 | 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] | 4685 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4686 | checkOutputForStrategy(STRATEGY_PHONE); | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4687 | 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] | 4688 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4689 | checkOutputForStrategy(STRATEGY_SONIFICATION); | 
|  | 4690 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4691 | checkOutputForStrategy(STRATEGY_ACCESSIBILITY); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4692 | checkOutputForStrategy(STRATEGY_MEDIA); | 
|  | 4693 | checkOutputForStrategy(STRATEGY_DTMF); | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4694 | checkOutputForStrategy(STRATEGY_REROUTING); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4695 | } | 
|  | 4696 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4697 | void AudioPolicyManager::checkA2dpSuspend() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4698 | { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4699 | audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput(); | 
| Aniket Kumar Lata | a8ee996 | 2018-01-31 20:24:23 -0800 | [diff] [blame] | 4700 | if (a2dpOutput == 0 || mOutputs.isA2dpOffloadedOnPrimary()) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4701 | mA2dpSuspended = false; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4702 | return; | 
|  | 4703 | } | 
|  | 4704 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4705 | bool isScoConnected = | 
| Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 4706 | ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET & | 
|  | 4707 | ~AUDIO_DEVICE_BIT_IN) != 0) || | 
|  | 4708 | ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0); | 
| Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4709 |  | 
|  | 4710 | // if suspended, restore A2DP output if: | 
|  | 4711 | //      ((SCO device is NOT connected) || | 
|  | 4712 | //       ((forced usage communication is NOT SCO) && (forced usage for record is NOT SCO) && | 
|  | 4713 | //        (phone state is NOT in call) && (phone state is NOT ringing))) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4714 | // | 
| Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4715 | // if not suspended, suspend A2DP output if: | 
|  | 4716 | //      (SCO device is connected) && | 
|  | 4717 | //       ((forced usage for communication is SCO) || (forced usage for record is SCO) || | 
|  | 4718 | //       ((phone state is in call) || (phone state is ringing))) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4719 | // | 
|  | 4720 | if (mA2dpSuspended) { | 
| Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4721 | if (!isScoConnected || | 
|  | 4722 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != | 
|  | 4723 | AUDIO_POLICY_FORCE_BT_SCO) && | 
|  | 4724 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != | 
|  | 4725 | AUDIO_POLICY_FORCE_BT_SCO) && | 
|  | 4726 | (mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) && | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4727 | (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4728 |  | 
|  | 4729 | mpClientInterface->restoreOutput(a2dpOutput); | 
|  | 4730 | mA2dpSuspended = false; | 
|  | 4731 | } | 
|  | 4732 | } else { | 
| Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4733 | if (isScoConnected && | 
|  | 4734 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == | 
|  | 4735 | AUDIO_POLICY_FORCE_BT_SCO) || | 
|  | 4736 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == | 
|  | 4737 | AUDIO_POLICY_FORCE_BT_SCO) || | 
|  | 4738 | (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) || | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4739 | (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4740 |  | 
|  | 4741 | mpClientInterface->suspendOutput(a2dpOutput); | 
|  | 4742 | mA2dpSuspended = true; | 
|  | 4743 | } | 
|  | 4744 | } | 
|  | 4745 | } | 
|  | 4746 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4747 | audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, | 
|  | 4748 | bool fromCache) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4749 | { | 
|  | 4750 | audio_devices_t device = AUDIO_DEVICE_NONE; | 
|  | 4751 |  | 
| Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4752 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4753 | if (index >= 0) { | 
|  | 4754 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
|  | 4755 | if (patchDesc->mUid != mUidCached) { | 
|  | 4756 | ALOGV("getNewOutputDevice() device %08x forced by patch %d", | 
| Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4757 | outputDesc->device(), outputDesc->getPatchHandle()); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4758 | return outputDesc->device(); | 
|  | 4759 | } | 
|  | 4760 | } | 
|  | 4761 |  | 
| Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 4762 | // Check if an explicit routing request exists for an active stream on this output and | 
|  | 4763 | // use it in priority before any other rule | 
|  | 4764 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { | 
|  | 4765 | if (outputDesc->isStreamActive((audio_stream_type_t)stream)) { | 
|  | 4766 | audio_devices_t forcedDevice = | 
|  | 4767 | mOutputRoutes.getActiveDeviceForStream( | 
|  | 4768 | (audio_stream_type_t)stream, mAvailableOutputDevices); | 
|  | 4769 |  | 
|  | 4770 | if (forcedDevice != AUDIO_DEVICE_NONE) { | 
|  | 4771 | return forcedDevice; | 
|  | 4772 | } | 
|  | 4773 | } | 
|  | 4774 | } | 
|  | 4775 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4776 | // 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] | 4777 | // 1: the strategy enforced audible is active and enforced on the output: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4778 | //      use device for strategy enforced audible | 
|  | 4779 | // 2: we are in call or the strategy phone is active on the output: | 
|  | 4780 | //      use device for strategy phone | 
| Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4781 | // 3: the strategy sonification is active on the output: | 
| Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4782 | //      use device for strategy sonification | 
| Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4783 | // 4: the strategy for enforced audible is active but not enforced on the output: | 
|  | 4784 | //      use the device for strategy enforced audible | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4785 | // 5: the strategy accessibility is active on the output: | 
|  | 4786 | //      use device for strategy accessibility | 
| Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4787 | // 6: the strategy "respectful" sonification is active on the output: | 
|  | 4788 | //      use device for strategy "respectful" sonification | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4789 | // 7: the strategy media is active on the output: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4790 | //      use device for strategy media | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4791 | // 8: the strategy DTMF is active on the output: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4792 | //      use device for strategy DTMF | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4793 | // 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] | 4794 | //      use device for strategy t-t-s | 
| Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 4795 |  | 
|  | 4796 | // FIXME: extend use of isStrategyActiveOnSameModule() to all strategies | 
|  | 4797 | // with a refined rule considering mutually exclusive devices (using same backend) | 
|  | 4798 | // as opposed to all streams on the same audio HAL module. | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4799 | if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) && | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4800 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4801 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); | 
|  | 4802 | } else if (isInCall() || | 
| Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 4803 | isStrategyActiveOnSameModule(outputDesc, STRATEGY_PHONE)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4804 | device = getDeviceForStrategy(STRATEGY_PHONE, fromCache); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4805 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4806 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache); | 
| Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4807 | } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) { | 
|  | 4808 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4809 | } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) { | 
|  | 4810 | device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4811 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4812 | device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4813 | } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4814 | device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4815 | } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4816 | device = getDeviceForStrategy(STRATEGY_DTMF, fromCache); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4817 | } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) { | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4818 | device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4819 | } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) { | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4820 | device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4821 | } | 
|  | 4822 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4823 | ALOGV("getNewOutputDevice() selected device %x", device); | 
|  | 4824 | return device; | 
|  | 4825 | } | 
|  | 4826 |  | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4827 | audio_devices_t AudioPolicyManager::getNewInputDevice(const sp<AudioInputDescriptor>& inputDesc) | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4828 | { | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4829 | audio_devices_t device = AUDIO_DEVICE_NONE; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4830 |  | 
| Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4831 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4832 | if (index >= 0) { | 
|  | 4833 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
|  | 4834 | if (patchDesc->mUid != mUidCached) { | 
|  | 4835 | ALOGV("getNewInputDevice() device %08x forced by patch %d", | 
| Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4836 | inputDesc->mDevice, inputDesc->getPatchHandle()); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4837 | return inputDesc->mDevice; | 
|  | 4838 | } | 
|  | 4839 | } | 
|  | 4840 |  | 
| Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 4841 | // If we are not in call and no client is active on this input, this methods returns | 
|  | 4842 | // AUDIO_DEVICE_NONE, causing the patch on the input stream to be released. | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4843 | audio_source_t source = inputDesc->getHighestPrioritySource(true /*activeOnly*/); | 
| Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 4844 | if (source == AUDIO_SOURCE_DEFAULT && isInCall()) { | 
|  | 4845 | source = AUDIO_SOURCE_VOICE_COMMUNICATION; | 
|  | 4846 | } | 
|  | 4847 | if (source != AUDIO_SOURCE_DEFAULT) { | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4848 | device = getDeviceAndMixForInputSource(source); | 
|  | 4849 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4850 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4851 | return device; | 
|  | 4852 | } | 
|  | 4853 |  | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4854 | bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1, | 
|  | 4855 | audio_stream_type_t stream2) { | 
| Jean-Michel Trivi | 99bb2f9 | 2016-11-23 15:52:07 -0800 | [diff] [blame] | 4856 | return (stream1 == stream2); | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4857 | } | 
|  | 4858 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4859 | uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4860 | return (uint32_t)getStrategy(stream); | 
|  | 4861 | } | 
|  | 4862 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4863 | audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4864 | // By checking the range of stream before calling getStrategy, we avoid | 
|  | 4865 | // getStrategy's behavior for invalid streams.  getStrategy would do a ALOGE | 
|  | 4866 | // 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] | 4867 | if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4868 | return AUDIO_DEVICE_NONE; | 
|  | 4869 | } | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4870 | audio_devices_t devices = AUDIO_DEVICE_NONE; | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4871 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { | 
|  | 4872 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4873 | continue; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4874 | } | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4875 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4876 | audio_devices_t curDevices = | 
| Eric Laurent | 447a87b | 2016-07-07 17:32:10 -0700 | [diff] [blame] | 4877 | getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/); | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4878 | for (audio_io_handle_t output : getOutputsForDevice(curDevices, mOutputs)) { | 
|  | 4879 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4880 | if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) { | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4881 | curDevices |= outputDesc->device(); | 
|  | 4882 | } | 
|  | 4883 | } | 
|  | 4884 | devices |= curDevices; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4885 | } | 
| Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4886 |  | 
|  | 4887 | /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it | 
|  | 4888 | and doesn't really need to.*/ | 
|  | 4889 | if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) { | 
|  | 4890 | devices |= AUDIO_DEVICE_OUT_SPEAKER; | 
|  | 4891 | devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE; | 
|  | 4892 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4893 | return devices; | 
|  | 4894 | } | 
|  | 4895 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4896 | routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const | 
|  | 4897 | { | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4898 | 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] | 4899 | return mEngine->getStrategyForStream(stream); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4900 | } | 
|  | 4901 |  | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4902 | uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) { | 
|  | 4903 | // flags to strategy mapping | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4904 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { | 
|  | 4905 | return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER; | 
|  | 4906 | } | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4907 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { | 
|  | 4908 | return (uint32_t) STRATEGY_ENFORCED_AUDIBLE; | 
|  | 4909 | } | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4910 | // usage to strategy mapping | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4911 | return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage)); | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4912 | } | 
|  | 4913 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4914 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4915 | switch(stream) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4916 | case AUDIO_STREAM_MUSIC: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4917 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); | 
|  | 4918 | updateDevicesAndOutputs(); | 
|  | 4919 | break; | 
|  | 4920 | default: | 
|  | 4921 | break; | 
|  | 4922 | } | 
|  | 4923 | } | 
|  | 4924 |  | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4925 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { | 
| Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 4926 |  | 
|  | 4927 | // skip beacon mute management if a dedicated TTS output is available | 
|  | 4928 | if (mTtsOutputAvailable) { | 
|  | 4929 | return 0; | 
|  | 4930 | } | 
|  | 4931 |  | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4932 | switch(event) { | 
|  | 4933 | case STARTING_OUTPUT: | 
|  | 4934 | mBeaconMuteRefCount++; | 
|  | 4935 | break; | 
|  | 4936 | case STOPPING_OUTPUT: | 
|  | 4937 | if (mBeaconMuteRefCount > 0) { | 
|  | 4938 | mBeaconMuteRefCount--; | 
|  | 4939 | } | 
|  | 4940 | break; | 
|  | 4941 | case STARTING_BEACON: | 
|  | 4942 | mBeaconPlayingRefCount++; | 
|  | 4943 | break; | 
|  | 4944 | case STOPPING_BEACON: | 
|  | 4945 | if (mBeaconPlayingRefCount > 0) { | 
|  | 4946 | mBeaconPlayingRefCount--; | 
|  | 4947 | } | 
|  | 4948 | break; | 
|  | 4949 | } | 
|  | 4950 |  | 
|  | 4951 | if (mBeaconMuteRefCount > 0) { | 
|  | 4952 | // any playback causes beacon to be muted | 
|  | 4953 | return setBeaconMute(true); | 
|  | 4954 | } else { | 
|  | 4955 | // no other playback: unmute when beacon starts playing, mute when it stops | 
|  | 4956 | return setBeaconMute(mBeaconPlayingRefCount == 0); | 
|  | 4957 | } | 
|  | 4958 | } | 
|  | 4959 |  | 
|  | 4960 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { | 
|  | 4961 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", | 
|  | 4962 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); | 
|  | 4963 | // keep track of muted state to avoid repeating mute/unmute operations | 
|  | 4964 | if (mBeaconMuted != mute) { | 
|  | 4965 | // mute/unmute AUDIO_STREAM_TTS on all outputs | 
|  | 4966 | ALOGV("\t muting %d", mute); | 
|  | 4967 | uint32_t maxLatency = 0; | 
|  | 4968 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4969 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4970 | setStreamMute(AUDIO_STREAM_TTS, mute/*on*/, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4971 | desc, | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4972 | 0 /*delay*/, AUDIO_DEVICE_NONE); | 
|  | 4973 | const uint32_t latency = desc->latency() * 2; | 
|  | 4974 | if (latency > maxLatency) { | 
|  | 4975 | maxLatency = latency; | 
|  | 4976 | } | 
|  | 4977 | } | 
|  | 4978 | mBeaconMuted = mute; | 
|  | 4979 | return maxLatency; | 
|  | 4980 | } | 
|  | 4981 | return 0; | 
|  | 4982 | } | 
|  | 4983 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4984 | audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4985 | bool fromCache) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4986 | { | 
| Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 4987 | // Check if an explicit routing request exists for a stream type corresponding to the | 
|  | 4988 | // specified strategy and use it in priority over default routing rules. | 
|  | 4989 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { | 
|  | 4990 | if (getStrategy((audio_stream_type_t)stream) == strategy) { | 
|  | 4991 | audio_devices_t forcedDevice = | 
|  | 4992 | mOutputRoutes.getActiveDeviceForStream( | 
|  | 4993 | (audio_stream_type_t)stream, mAvailableOutputDevices); | 
|  | 4994 | if (forcedDevice != AUDIO_DEVICE_NONE) { | 
|  | 4995 | return forcedDevice; | 
|  | 4996 | } | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4997 | } | 
|  | 4998 | } | 
|  | 4999 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5000 | if (fromCache) { | 
|  | 5001 | ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x", | 
|  | 5002 | strategy, mDeviceForStrategy[strategy]); | 
|  | 5003 | return mDeviceForStrategy[strategy]; | 
|  | 5004 | } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5005 | return mEngine->getDeviceForStrategy(strategy); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5006 | } | 
|  | 5007 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5008 | void AudioPolicyManager::updateDevicesAndOutputs() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5009 | { | 
|  | 5010 | for (int i = 0; i < NUM_STRATEGIES; i++) { | 
|  | 5011 | mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); | 
|  | 5012 | } | 
|  | 5013 | mPreviousOutputs = mOutputs; | 
|  | 5014 | } | 
|  | 5015 |  | 
| Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5016 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5017 | audio_devices_t prevDevice, | 
|  | 5018 | uint32_t delayMs) | 
|  | 5019 | { | 
|  | 5020 | // mute/unmute strategies using an incompatible device combination | 
|  | 5021 | // if muting, wait for the audio in pcm buffer to be drained before proceeding | 
|  | 5022 | // if unmuting, unmute only after the specified delay | 
|  | 5023 | if (outputDesc->isDuplicated()) { | 
|  | 5024 | return 0; | 
|  | 5025 | } | 
|  | 5026 |  | 
|  | 5027 | uint32_t muteWaitMs = 0; | 
|  | 5028 | audio_devices_t device = outputDesc->device(); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5029 | bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5030 |  | 
|  | 5031 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { | 
|  | 5032 | audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5033 | curDevice = curDevice & outputDesc->supportedDevices(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5034 | bool mute = shouldMute && (curDevice & device) && (curDevice != device); | 
|  | 5035 | bool doMute = false; | 
|  | 5036 |  | 
|  | 5037 | if (mute && !outputDesc->mStrategyMutedByDevice[i]) { | 
|  | 5038 | doMute = true; | 
|  | 5039 | outputDesc->mStrategyMutedByDevice[i] = true; | 
|  | 5040 | } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){ | 
|  | 5041 | doMute = true; | 
|  | 5042 | outputDesc->mStrategyMutedByDevice[i] = false; | 
|  | 5043 | } | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5044 | if (doMute) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5045 | for (size_t j = 0; j < mOutputs.size(); j++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5046 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5047 | // skip output if it does not share any device with current output | 
|  | 5048 | if ((desc->supportedDevices() & outputDesc->supportedDevices()) | 
|  | 5049 | == AUDIO_DEVICE_NONE) { | 
|  | 5050 | continue; | 
|  | 5051 | } | 
| Eric Laurent | 37ddbb4 | 2016-08-10 16:19:14 -0700 | [diff] [blame] | 5052 | ALOGVV("checkDeviceMuteStrategies() %s strategy %zu (curDevice %04x)", | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5053 | mute ? "muting" : "unmuting", i, curDevice); | 
|  | 5054 | setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5055 | if (isStrategyActive(desc, (routing_strategy)i)) { | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5056 | if (mute) { | 
|  | 5057 | // FIXME: should not need to double latency if volume could be applied | 
|  | 5058 | // immediately by the audioflinger mixer. We must account for the delay | 
|  | 5059 | // between now and the next time the audioflinger thread for this output | 
|  | 5060 | // will process a buffer (which corresponds to one buffer size, | 
|  | 5061 | // usually 1/2 or 1/4 of the latency). | 
|  | 5062 | if (muteWaitMs < desc->latency() * 2) { | 
|  | 5063 | muteWaitMs = desc->latency() * 2; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5064 | } | 
|  | 5065 | } | 
|  | 5066 | } | 
|  | 5067 | } | 
|  | 5068 | } | 
|  | 5069 | } | 
|  | 5070 |  | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5071 | // temporary mute output if device selection changes to avoid volume bursts due to | 
|  | 5072 | // different per device volumes | 
|  | 5073 | if (outputDesc->isActive() && (device != prevDevice)) { | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5074 | uint32_t tempMuteWaitMs = outputDesc->latency() * 2; | 
|  | 5075 | // temporary mute duration is conservatively set to 4 times the reported latency | 
|  | 5076 | uint32_t tempMuteDurationMs = outputDesc->latency() * 4; | 
|  | 5077 | if (muteWaitMs < tempMuteWaitMs) { | 
|  | 5078 | muteWaitMs = tempMuteWaitMs; | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5079 | } | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5080 |  | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5081 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5082 | if (isStrategyActive(outputDesc, (routing_strategy)i)) { | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5083 | // make sure that we do not start the temporary mute period too early in case of | 
|  | 5084 | // delayed device change | 
|  | 5085 | setStrategyMute((routing_strategy)i, true, outputDesc, delayMs); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5086 | setStrategyMute((routing_strategy)i, false, outputDesc, | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5087 | delayMs + tempMuteDurationMs, device); | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5088 | } | 
|  | 5089 | } | 
|  | 5090 | } | 
|  | 5091 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5092 | // wait for the PCM output buffers to empty before proceeding with the rest of the command | 
|  | 5093 | if (muteWaitMs > delayMs) { | 
|  | 5094 | muteWaitMs -= delayMs; | 
|  | 5095 | usleep(muteWaitMs * 1000); | 
|  | 5096 | return muteWaitMs; | 
|  | 5097 | } | 
|  | 5098 | return 0; | 
|  | 5099 | } | 
|  | 5100 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5101 | uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5102 | audio_devices_t device, | 
|  | 5103 | bool force, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5104 | int delayMs, | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5105 | audio_patch_handle_t *patchHandle, | 
| Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5106 | const char *address, | 
|  | 5107 | bool requiresMuteCheck) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5108 | { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5109 | ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5110 | AudioParameter param; | 
|  | 5111 | uint32_t muteWaitMs; | 
|  | 5112 |  | 
|  | 5113 | if (outputDesc->isDuplicated()) { | 
| Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5114 | muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs, | 
|  | 5115 | nullptr /* patchHandle */, nullptr /* address */, requiresMuteCheck); | 
|  | 5116 | muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs, | 
|  | 5117 | nullptr /* patchHandle */, nullptr /* address */, requiresMuteCheck); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5118 | return muteWaitMs; | 
|  | 5119 | } | 
|  | 5120 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current | 
|  | 5121 | // output profile | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5122 | if ((device != AUDIO_DEVICE_NONE) && | 
| Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5123 | ((device & outputDesc->supportedDevices()) == AUDIO_DEVICE_NONE)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5124 | return 0; | 
|  | 5125 | } | 
|  | 5126 |  | 
|  | 5127 | // filter devices according to output selected | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5128 | device = (audio_devices_t)(device & outputDesc->supportedDevices()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5129 |  | 
|  | 5130 | audio_devices_t prevDevice = outputDesc->mDevice; | 
|  | 5131 |  | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 5132 | ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5133 |  | 
|  | 5134 | if (device != AUDIO_DEVICE_NONE) { | 
|  | 5135 | outputDesc->mDevice = device; | 
|  | 5136 | } | 
| Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5137 |  | 
|  | 5138 | // if the outputs are not materially active, there is no need to mute. | 
|  | 5139 | if (requiresMuteCheck) { | 
|  | 5140 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs); | 
|  | 5141 | } else { | 
|  | 5142 | ALOGV("%s: suppressing checkDeviceMuteStrategies", __func__); | 
|  | 5143 | muteWaitMs = 0; | 
|  | 5144 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5145 |  | 
|  | 5146 | // Do not change the routing if: | 
| Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 5147 | //      the requested device is AUDIO_DEVICE_NONE | 
|  | 5148 | //      OR the requested device is the same as current device | 
|  | 5149 | //  AND force is not specified | 
|  | 5150 | //  AND the output is connected by a valid audio patch. | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5151 | // Doing this check here allows the caller to call setOutputDevice() without conditions | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 5152 | if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && | 
|  | 5153 | !force && | 
| Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 5154 | outputDesc->getPatchHandle() != 0) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5155 | ALOGV("setOutputDevice() setting same device 0x%04x or null device", device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5156 | return muteWaitMs; | 
|  | 5157 | } | 
|  | 5158 |  | 
|  | 5159 | ALOGV("setOutputDevice() changing device"); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5160 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5161 | // do the routing | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5162 | if (device == AUDIO_DEVICE_NONE) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5163 | resetOutputDevice(outputDesc, delayMs, NULL); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5164 | } else { | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 5165 | DeviceVector deviceList; | 
|  | 5166 | if ((address == NULL) || (strlen(address) == 0)) { | 
| Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 5167 | deviceList = mAvailableOutputDevices.getDevicesFromTypeMask(device); | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 5168 | } else { | 
| Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 5169 | sp<DeviceDescriptor> deviceDesc = mAvailableOutputDevices.getDevice( | 
|  | 5170 | device, String8(address)); | 
|  | 5171 | if (deviceDesc) deviceList.add(deviceDesc); | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 5172 | } | 
|  | 5173 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5174 | if (!deviceList.isEmpty()) { | 
| Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5175 | PatchBuilder patchBuilder; | 
|  | 5176 | patchBuilder.addSource(outputDesc); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5177 | for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) { | 
| Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5178 | patchBuilder.addSink(deviceList.itemAt(i)); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5179 | } | 
| Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5180 | installPatch(__func__, patchHandle, outputDesc.get(), patchBuilder.patch(), delayMs); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5181 | } | 
| bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 5182 |  | 
|  | 5183 | // inform all input as well | 
|  | 5184 | for (size_t i = 0; i < mInputs.size(); i++) { | 
|  | 5185 | const sp<AudioInputDescriptor>  inputDescriptor = mInputs.valueAt(i); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5186 | if (!is_virtual_input_device(inputDescriptor->mDevice)) { | 
| bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 5187 | AudioParameter inputCmd = AudioParameter(); | 
|  | 5188 | ALOGV("%s: inform input %d of device:%d", __func__, | 
|  | 5189 | inputDescriptor->mIoHandle, device); | 
|  | 5190 | inputCmd.addInt(String8(AudioParameter::keyRouting),device); | 
|  | 5191 | mpClientInterface->setParameters(inputDescriptor->mIoHandle, | 
|  | 5192 | inputCmd.toString(), | 
|  | 5193 | delayMs); | 
|  | 5194 | } | 
|  | 5195 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5196 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5197 |  | 
|  | 5198 | // update stream volumes according to new device | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5199 | applyStreamVolumes(outputDesc, device, delayMs); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5200 |  | 
|  | 5201 | return muteWaitMs; | 
|  | 5202 | } | 
|  | 5203 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5204 | status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5205 | int delayMs, | 
|  | 5206 | audio_patch_handle_t *patchHandle) | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5207 | { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5208 | ssize_t index; | 
|  | 5209 | if (patchHandle) { | 
|  | 5210 | index = mAudioPatches.indexOfKey(*patchHandle); | 
|  | 5211 | } else { | 
| Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 5212 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5213 | } | 
|  | 5214 | if (index < 0) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5215 | return INVALID_OPERATION; | 
|  | 5216 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5217 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
|  | 5218 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5219 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); | 
| Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 5220 | outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5221 | removeAudioPatch(patchDesc->mHandle); | 
|  | 5222 | nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5223 | mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5224 | return status; | 
|  | 5225 | } | 
|  | 5226 |  | 
|  | 5227 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, | 
|  | 5228 | audio_devices_t device, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5229 | bool force, | 
|  | 5230 | audio_patch_handle_t *patchHandle) | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5231 | { | 
|  | 5232 | status_t status = NO_ERROR; | 
|  | 5233 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5234 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5235 | if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) { | 
|  | 5236 | inputDesc->mDevice = device; | 
|  | 5237 |  | 
| Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 5238 | DeviceVector deviceList = mAvailableInputDevices.getDevicesFromTypeMask(device); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5239 | if (!deviceList.isEmpty()) { | 
| Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5240 | PatchBuilder patchBuilder; | 
|  | 5241 | patchBuilder.addSink(inputDesc, | 
| Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 5242 | // AUDIO_SOURCE_HOTWORD is for internal use only: | 
|  | 5243 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL | 
| Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5244 | [inputDesc](const PatchBuilder::mix_usecase_t& usecase) { | 
|  | 5245 | auto result = usecase; | 
|  | 5246 | if (result.source == AUDIO_SOURCE_HOTWORD && !inputDesc->isSoundTrigger()) { | 
|  | 5247 | result.source = AUDIO_SOURCE_VOICE_RECOGNITION; | 
|  | 5248 | } | 
|  | 5249 | return result; }). | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5250 | //only one input device for now | 
| Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5251 | addSource(deviceList.itemAt(0)); | 
|  | 5252 | status = installPatch(__func__, patchHandle, inputDesc.get(), patchBuilder.patch(), 0); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5253 | } | 
|  | 5254 | } | 
|  | 5255 | return status; | 
|  | 5256 | } | 
|  | 5257 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5258 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, | 
|  | 5259 | audio_patch_handle_t *patchHandle) | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5260 | { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5261 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5262 | ssize_t index; | 
|  | 5263 | if (patchHandle) { | 
|  | 5264 | index = mAudioPatches.indexOfKey(*patchHandle); | 
|  | 5265 | } else { | 
| Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 5266 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5267 | } | 
|  | 5268 | if (index < 0) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5269 | return INVALID_OPERATION; | 
|  | 5270 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5271 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
|  | 5272 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5273 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); | 
| Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 5274 | inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5275 | removeAudioPatch(patchDesc->mHandle); | 
|  | 5276 | nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5277 | mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5278 | return status; | 
|  | 5279 | } | 
|  | 5280 |  | 
| Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 5281 | sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device, | 
| Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5282 | const String8& address, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5283 | uint32_t& samplingRate, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5284 | audio_format_t& format, | 
|  | 5285 | audio_channel_mask_t& channelMask, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5286 | audio_input_flags_t flags) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5287 | { | 
|  | 5288 | // Choose an input profile based on the requested capture parameters: select the first available | 
|  | 5289 | // profile supporting all requested parameters. | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5290 | // | 
|  | 5291 | // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return | 
|  | 5292 | // the best matching profile, not the first one. | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5293 |  | 
| Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5294 | sp<IOProfile> firstInexact; | 
|  | 5295 | uint32_t updatedSamplingRate = 0; | 
|  | 5296 | audio_format_t updatedFormat = AUDIO_FORMAT_INVALID; | 
|  | 5297 | audio_channel_mask_t updatedChannelMask = AUDIO_CHANNEL_INVALID; | 
| Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 5298 | for (const auto& hwModule : mHwModules) { | 
| Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 5299 | for (const auto& profile : hwModule->getInputProfiles()) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5300 | // profile->log(); | 
| Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5301 | //updatedFormat = format; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5302 | if (profile->isCompatibleProfile(device, address, samplingRate, | 
| Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5303 | &samplingRate  /*updatedSamplingRate*/, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5304 | format, | 
| Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5305 | &format,       /*updatedFormat*/ | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5306 | channelMask, | 
| Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5307 | &channelMask   /*updatedChannelMask*/, | 
|  | 5308 | // FIXME ugly cast | 
|  | 5309 | (audio_output_flags_t) flags, | 
|  | 5310 | true /*exactMatchRequiredForInputFlags*/)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5311 | return profile; | 
|  | 5312 | } | 
| Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5313 | if (firstInexact == nullptr && profile->isCompatibleProfile(device, address, | 
|  | 5314 | samplingRate, | 
|  | 5315 | &updatedSamplingRate, | 
|  | 5316 | format, | 
|  | 5317 | &updatedFormat, | 
|  | 5318 | channelMask, | 
|  | 5319 | &updatedChannelMask, | 
|  | 5320 | // FIXME ugly cast | 
|  | 5321 | (audio_output_flags_t) flags, | 
|  | 5322 | false /*exactMatchRequiredForInputFlags*/)) { | 
|  | 5323 | firstInexact = profile; | 
|  | 5324 | } | 
|  | 5325 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5326 | } | 
|  | 5327 | } | 
| Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5328 | if (firstInexact != nullptr) { | 
|  | 5329 | samplingRate = updatedSamplingRate; | 
|  | 5330 | format = updatedFormat; | 
|  | 5331 | channelMask = updatedChannelMask; | 
|  | 5332 | return firstInexact; | 
|  | 5333 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5334 | return NULL; | 
|  | 5335 | } | 
|  | 5336 |  | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5337 |  | 
|  | 5338 | audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5339 | AudioMix **policyMix) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5340 | { | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 5341 | audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; | 
|  | 5342 | audio_devices_t selectedDeviceFromMix = | 
|  | 5343 | mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5344 |  | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 5345 | if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) { | 
|  | 5346 | return selectedDeviceFromMix; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5347 | } | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5348 | return getDeviceForInputSource(inputSource); | 
|  | 5349 | } | 
|  | 5350 |  | 
|  | 5351 | audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource) | 
|  | 5352 | { | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 5353 | // Routing | 
|  | 5354 | // Scan the whole RouteMap to see if we have an explicit route: | 
|  | 5355 | // if the input source in the RouteMap is the same as the argument above, | 
|  | 5356 | // and activity count is non-zero and the device in the route descriptor is available | 
|  | 5357 | // then select this device. | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 5358 | for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) { | 
|  | 5359 | sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex); | 
| Eric Laurent | 2157f5b | 2018-04-25 18:33:02 -0700 | [diff] [blame] | 5360 | if ((inputSource == route->mSource) && route->isActiveOrChanged() && | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 5361 | (mAvailableInputDevices.indexOf(route->mDeviceDescriptor) >= 0)) { | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 5362 | return route->mDeviceDescriptor->type(); | 
|  | 5363 | } | 
|  | 5364 | } | 
|  | 5365 |  | 
|  | 5366 | return mEngine->getDeviceForInputSource(inputSource); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5367 | } | 
|  | 5368 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5369 | float AudioPolicyManager::computeVolume(audio_stream_type_t stream, | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5370 | int index, | 
|  | 5371 | audio_devices_t device) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5372 | { | 
| Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5373 | float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index); | 
| Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 5374 |  | 
|  | 5375 | // handle the case of accessibility active while a ringtone is playing: if the ringtone is much | 
|  | 5376 | // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch | 
|  | 5377 | // exploration of the dialer UI. In this situation, bring the accessibility volume closer to | 
|  | 5378 | // the ringtone volume | 
|  | 5379 | if ((stream == AUDIO_STREAM_ACCESSIBILITY) | 
|  | 5380 | && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState()) | 
|  | 5381 | && isStreamActive(AUDIO_STREAM_RING, 0)) { | 
|  | 5382 | const float ringVolumeDB = computeVolume(AUDIO_STREAM_RING, index, device); | 
|  | 5383 | return ringVolumeDB - 4 > volumeDB ? ringVolumeDB - 4 : volumeDB; | 
|  | 5384 | } | 
|  | 5385 |  | 
| Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 5386 | // in-call: always cap volume by voice volume + some low headroom | 
|  | 5387 | if ((stream != AUDIO_STREAM_VOICE_CALL) && | 
| Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5388 | (isInCall() || mOutputs.isStreamActiveLocally(AUDIO_STREAM_VOICE_CALL))) { | 
| Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 5389 | switch (stream) { | 
|  | 5390 | case AUDIO_STREAM_SYSTEM: | 
|  | 5391 | case AUDIO_STREAM_RING: | 
|  | 5392 | case AUDIO_STREAM_MUSIC: | 
|  | 5393 | case AUDIO_STREAM_ALARM: | 
|  | 5394 | case AUDIO_STREAM_NOTIFICATION: | 
|  | 5395 | case AUDIO_STREAM_ENFORCED_AUDIBLE: | 
|  | 5396 | case AUDIO_STREAM_DTMF: | 
|  | 5397 | case AUDIO_STREAM_ACCESSIBILITY: { | 
| Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5398 | int voiceVolumeIndex = | 
| Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 5399 | mVolumeCurves->getVolumeIndex(AUDIO_STREAM_VOICE_CALL, device); | 
| Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5400 | const float maxVoiceVolDb = | 
| Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 5401 | computeVolume(AUDIO_STREAM_VOICE_CALL, voiceVolumeIndex, device) | 
| Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5402 | + IN_CALL_EARPIECE_HEADROOM_DB; | 
| Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 5403 | if (volumeDB > maxVoiceVolDb) { | 
|  | 5404 | ALOGV("computeVolume() stream %d at vol=%f overriden by stream %d at vol=%f", | 
|  | 5405 | stream, volumeDB, AUDIO_STREAM_VOICE_CALL, maxVoiceVolDb); | 
|  | 5406 | volumeDB = maxVoiceVolDb; | 
|  | 5407 | } | 
|  | 5408 | } break; | 
|  | 5409 | default: | 
|  | 5410 | break; | 
|  | 5411 | } | 
|  | 5412 | } | 
|  | 5413 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5414 | // if a headset is connected, apply the following rules to ring tones and notifications | 
|  | 5415 | // to avoid sound level bursts in user's ears: | 
| Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5416 | // - always attenuate notifications volume by 6dB | 
|  | 5417 | // - attenuate ring tones volume by 6dB unless music is not playing and | 
|  | 5418 | // speaker is part of the select devices | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5419 | // - if music is playing, always limit the volume to current music volume, | 
|  | 5420 | // with a minimum threshold at -36dB so that notification is always perceived. | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5421 | const routing_strategy stream_strategy = getStrategy(stream); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5422 | if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | | 
|  | 5423 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | | 
|  | 5424 | AUDIO_DEVICE_OUT_WIRED_HEADSET | | 
| Eric Laurent | 904d632 | 2017-03-17 17:20:47 -0700 | [diff] [blame] | 5425 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE | | 
| Jakub Pawlowski | 00f928c | 2018-03-22 13:20:03 -0700 | [diff] [blame] | 5426 | AUDIO_DEVICE_OUT_USB_HEADSET | | 
|  | 5427 | AUDIO_DEVICE_OUT_HEARING_AID)) && | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5428 | ((stream_strategy == STRATEGY_SONIFICATION) | 
|  | 5429 | || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL) | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5430 | || (stream == AUDIO_STREAM_SYSTEM) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5431 | || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) && | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5432 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) && | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5433 | mVolumeCurves->canBeMuted(stream)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5434 | // when the phone is ringing we must consider that music could have been paused just before | 
|  | 5435 | // by the music application and behave as if music was active if the last music track was | 
|  | 5436 | // just stopped | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5437 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5438 | mLimitRingtoneVolume) { | 
| Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5439 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5440 | audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/); | 
| Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5441 | float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC, | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5442 | mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC, | 
|  | 5443 | musicDevice), | 
|  | 5444 | musicDevice); | 
| Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5445 | float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ? | 
|  | 5446 | musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB; | 
| Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5447 | if (volumeDB > minVolDB) { | 
|  | 5448 | volumeDB = minVolDB; | 
| Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5449 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5450 | } | 
| Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5451 | if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | | 
|  | 5452 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) { | 
|  | 5453 | // on A2DP, also ensure notification volume is not too low compared to media when | 
|  | 5454 | // intended to be played | 
|  | 5455 | if ((volumeDB > -96.0f) && | 
|  | 5456 | (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) { | 
|  | 5457 | ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f", | 
|  | 5458 | stream, device, | 
|  | 5459 | volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB); | 
|  | 5460 | volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB; | 
|  | 5461 | } | 
|  | 5462 | } | 
| Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5463 | } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) || | 
|  | 5464 | stream_strategy != STRATEGY_SONIFICATION) { | 
| Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5465 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5466 | } | 
|  | 5467 | } | 
|  | 5468 |  | 
| Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5469 | return volumeDB; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5470 | } | 
|  | 5471 |  | 
| Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 5472 | int AudioPolicyManager::rescaleVolumeIndex(int srcIndex, | 
|  | 5473 | audio_stream_type_t srcStream, | 
|  | 5474 | audio_stream_type_t dstStream) | 
|  | 5475 | { | 
|  | 5476 | if (srcStream == dstStream) { | 
|  | 5477 | return srcIndex; | 
|  | 5478 | } | 
|  | 5479 | float minSrc = (float)mVolumeCurves->getVolumeIndexMin(srcStream); | 
|  | 5480 | float maxSrc = (float)mVolumeCurves->getVolumeIndexMax(srcStream); | 
|  | 5481 | float minDst = (float)mVolumeCurves->getVolumeIndexMin(dstStream); | 
|  | 5482 | float maxDst = (float)mVolumeCurves->getVolumeIndexMax(dstStream); | 
|  | 5483 |  | 
|  | 5484 | return (int)(minDst + ((srcIndex - minSrc) * (maxDst - minDst)) / (maxSrc - minSrc)); | 
|  | 5485 | } | 
|  | 5486 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5487 | status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5488 | int index, | 
|  | 5489 | const sp<AudioOutputDescriptor>& outputDesc, | 
|  | 5490 | audio_devices_t device, | 
|  | 5491 | int delayMs, | 
|  | 5492 | bool force) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5493 | { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5494 | // do not change actual stream volume if the stream is muted | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5495 | if (outputDesc->mMuteCount[stream] != 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5496 | ALOGVV("checkAndSetVolume() stream %d muted count %d", | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5497 | stream, outputDesc->mMuteCount[stream]); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5498 | return NO_ERROR; | 
|  | 5499 | } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5500 | audio_policy_forced_cfg_t forceUseForComm = | 
|  | 5501 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5502 | // 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] | 5503 | if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) || | 
|  | 5504 | (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5505 | 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] | 5506 | stream, forceUseForComm); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5507 | return INVALID_OPERATION; | 
|  | 5508 | } | 
|  | 5509 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5510 | if (device == AUDIO_DEVICE_NONE) { | 
|  | 5511 | device = outputDesc->device(); | 
|  | 5512 | } | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5513 |  | 
| Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5514 | float volumeDb = computeVolume(stream, index, device); | 
| HW Lee | da7581e | 2018-05-22 18:31:34 +0800 | [diff] [blame] | 5515 | if (outputDesc->isFixedVolume(device) || | 
|  | 5516 | // Force VoIP volume to max for bluetooth SCO | 
|  | 5517 | ((stream == AUDIO_STREAM_VOICE_CALL || stream == AUDIO_STREAM_BLUETOOTH_SCO) && | 
|  | 5518 | (device & AUDIO_DEVICE_OUT_ALL_SCO) != 0)) { | 
| Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5519 | volumeDb = 0.0f; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5520 | } | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5521 |  | 
| Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5522 | outputDesc->setVolume(volumeDb, stream, device, delayMs, force); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5523 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5524 | if (stream == AUDIO_STREAM_VOICE_CALL || | 
|  | 5525 | stream == AUDIO_STREAM_BLUETOOTH_SCO) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5526 | float voiceVolume; | 
|  | 5527 | // 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] | 5528 | if (stream == AUDIO_STREAM_VOICE_CALL) { | 
| Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 5529 | voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5530 | } else { | 
|  | 5531 | voiceVolume = 1.0; | 
|  | 5532 | } | 
|  | 5533 |  | 
| Eric Laurent | 18fba84 | 2016-03-31 14:41:26 -0700 | [diff] [blame] | 5534 | if (voiceVolume != mLastVoiceVolume) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5535 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); | 
|  | 5536 | mLastVoiceVolume = voiceVolume; | 
|  | 5537 | } | 
|  | 5538 | } | 
|  | 5539 |  | 
|  | 5540 | return NO_ERROR; | 
|  | 5541 | } | 
|  | 5542 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5543 | void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc, | 
|  | 5544 | audio_devices_t device, | 
|  | 5545 | int delayMs, | 
|  | 5546 | bool force) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5547 | { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5548 | ALOGVV("applyStreamVolumes() for device %08x", device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5549 |  | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5550 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5551 | checkAndSetVolume((audio_stream_type_t)stream, | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5552 | mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device), | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5553 | outputDesc, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5554 | device, | 
|  | 5555 | delayMs, | 
|  | 5556 | force); | 
|  | 5557 | } | 
|  | 5558 | } | 
|  | 5559 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5560 | void AudioPolicyManager::setStrategyMute(routing_strategy strategy, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5561 | bool on, | 
|  | 5562 | const sp<AudioOutputDescriptor>& outputDesc, | 
|  | 5563 | int delayMs, | 
|  | 5564 | audio_devices_t device) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5565 | { | 
| Eric Laurent | 72249d5 | 2015-06-08 11:12:15 -0700 | [diff] [blame] | 5566 | ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d", | 
|  | 5567 | strategy, on, outputDesc->getId()); | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5568 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5569 | if (getStrategy((audio_stream_type_t)stream) == strategy) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5570 | setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5571 | } | 
|  | 5572 | } | 
|  | 5573 | } | 
|  | 5574 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5575 | void AudioPolicyManager::setStreamMute(audio_stream_type_t stream, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5576 | bool on, | 
|  | 5577 | const sp<AudioOutputDescriptor>& outputDesc, | 
|  | 5578 | int delayMs, | 
|  | 5579 | audio_devices_t device) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5580 | { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5581 | if (device == AUDIO_DEVICE_NONE) { | 
|  | 5582 | device = outputDesc->device(); | 
|  | 5583 | } | 
|  | 5584 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5585 | ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x", | 
|  | 5586 | stream, on, outputDesc->mMuteCount[stream], device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5587 |  | 
|  | 5588 | if (on) { | 
|  | 5589 | if (outputDesc->mMuteCount[stream] == 0) { | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5590 | if (mVolumeCurves->canBeMuted(stream) && | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5591 | ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) || | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5592 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5593 | checkAndSetVolume(stream, 0, outputDesc, device, delayMs); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5594 | } | 
|  | 5595 | } | 
|  | 5596 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored | 
|  | 5597 | outputDesc->mMuteCount[stream]++; | 
|  | 5598 | } else { | 
|  | 5599 | if (outputDesc->mMuteCount[stream] == 0) { | 
|  | 5600 | ALOGV("setStreamMute() unmuting non muted stream!"); | 
|  | 5601 | return; | 
|  | 5602 | } | 
|  | 5603 | if (--outputDesc->mMuteCount[stream] == 0) { | 
|  | 5604 | checkAndSetVolume(stream, | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5605 | mVolumeCurves->getVolumeIndex(stream, device), | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5606 | outputDesc, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5607 | device, | 
|  | 5608 | delayMs); | 
|  | 5609 | } | 
|  | 5610 | } | 
|  | 5611 | } | 
|  | 5612 |  | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5613 | audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr) | 
|  | 5614 | { | 
|  | 5615 | // flags to stream type mapping | 
|  | 5616 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { | 
|  | 5617 | return AUDIO_STREAM_ENFORCED_AUDIBLE; | 
|  | 5618 | } | 
|  | 5619 | if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) { | 
|  | 5620 | return AUDIO_STREAM_BLUETOOTH_SCO; | 
|  | 5621 | } | 
| Jean-Michel Trivi | 79ad438 | 2015-01-29 10:49:39 -0800 | [diff] [blame] | 5622 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { | 
|  | 5623 | return AUDIO_STREAM_TTS; | 
|  | 5624 | } | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5625 |  | 
|  | 5626 | // usage to stream type mapping | 
|  | 5627 | switch (attr->usage) { | 
|  | 5628 | case AUDIO_USAGE_MEDIA: | 
|  | 5629 | case AUDIO_USAGE_GAME: | 
| Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 5630 | case AUDIO_USAGE_ASSISTANT: | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5631 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: | 
|  | 5632 | return AUDIO_STREAM_MUSIC; | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5633 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: | 
|  | 5634 | return AUDIO_STREAM_ACCESSIBILITY; | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5635 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: | 
|  | 5636 | return AUDIO_STREAM_SYSTEM; | 
|  | 5637 | case AUDIO_USAGE_VOICE_COMMUNICATION: | 
|  | 5638 | return AUDIO_STREAM_VOICE_CALL; | 
|  | 5639 |  | 
|  | 5640 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: | 
|  | 5641 | return AUDIO_STREAM_DTMF; | 
|  | 5642 |  | 
|  | 5643 | case AUDIO_USAGE_ALARM: | 
|  | 5644 | return AUDIO_STREAM_ALARM; | 
|  | 5645 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: | 
|  | 5646 | return AUDIO_STREAM_RING; | 
|  | 5647 |  | 
|  | 5648 | case AUDIO_USAGE_NOTIFICATION: | 
|  | 5649 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: | 
|  | 5650 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: | 
|  | 5651 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: | 
|  | 5652 | case AUDIO_USAGE_NOTIFICATION_EVENT: | 
|  | 5653 | return AUDIO_STREAM_NOTIFICATION; | 
|  | 5654 |  | 
|  | 5655 | case AUDIO_USAGE_UNKNOWN: | 
|  | 5656 | default: | 
|  | 5657 | return AUDIO_STREAM_MUSIC; | 
|  | 5658 | } | 
|  | 5659 | } | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5660 |  | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5661 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) | 
|  | 5662 | { | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5663 | // has flags that map to a strategy? | 
|  | 5664 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { | 
|  | 5665 | return true; | 
|  | 5666 | } | 
|  | 5667 |  | 
|  | 5668 | // has known usage? | 
|  | 5669 | switch (paa->usage) { | 
|  | 5670 | case AUDIO_USAGE_UNKNOWN: | 
|  | 5671 | case AUDIO_USAGE_MEDIA: | 
|  | 5672 | case AUDIO_USAGE_VOICE_COMMUNICATION: | 
|  | 5673 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: | 
|  | 5674 | case AUDIO_USAGE_ALARM: | 
|  | 5675 | case AUDIO_USAGE_NOTIFICATION: | 
|  | 5676 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: | 
|  | 5677 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: | 
|  | 5678 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: | 
|  | 5679 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: | 
|  | 5680 | case AUDIO_USAGE_NOTIFICATION_EVENT: | 
|  | 5681 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: | 
|  | 5682 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: | 
|  | 5683 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: | 
|  | 5684 | case AUDIO_USAGE_GAME: | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5685 | case AUDIO_USAGE_VIRTUAL_SOURCE: | 
| Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 5686 | case AUDIO_USAGE_ASSISTANT: | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5687 | break; | 
|  | 5688 | default: | 
|  | 5689 | return false; | 
|  | 5690 | } | 
|  | 5691 | return true; | 
|  | 5692 | } | 
|  | 5693 |  | 
| Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5694 | bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor>& outputDesc, | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5695 | routing_strategy strategy, uint32_t inPastMs, | 
|  | 5696 | nsecs_t sysTime) const | 
|  | 5697 | { | 
|  | 5698 | if ((sysTime == 0) && (inPastMs != 0)) { | 
|  | 5699 | sysTime = systemTime(); | 
|  | 5700 | } | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5701 | for (int i = 0; i < (int)AUDIO_STREAM_FOR_POLICY_CNT; i++) { | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5702 | if (((getStrategy((audio_stream_type_t)i) == strategy) || | 
|  | 5703 | (NUM_STRATEGIES == strategy)) && | 
|  | 5704 | outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) { | 
|  | 5705 | return true; | 
|  | 5706 | } | 
|  | 5707 | } | 
|  | 5708 | return false; | 
|  | 5709 | } | 
|  | 5710 |  | 
| Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 5711 | bool AudioPolicyManager::isStrategyActiveOnSameModule(const sp<AudioOutputDescriptor>& outputDesc, | 
|  | 5712 | routing_strategy strategy, uint32_t inPastMs, | 
|  | 5713 | nsecs_t sysTime) const | 
|  | 5714 | { | 
|  | 5715 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 5716 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
|  | 5717 | if (outputDesc->sharesHwModuleWith(desc) | 
|  | 5718 | && isStrategyActive(desc, strategy, inPastMs, sysTime)) { | 
|  | 5719 | return true; | 
|  | 5720 | } | 
|  | 5721 | } | 
|  | 5722 | return false; | 
|  | 5723 | } | 
|  | 5724 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5725 | audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage) | 
|  | 5726 | { | 
|  | 5727 | return mEngine->getForceUse(usage); | 
|  | 5728 | } | 
|  | 5729 |  | 
|  | 5730 | bool AudioPolicyManager::isInCall() | 
|  | 5731 | { | 
|  | 5732 | return isStateInCall(mEngine->getPhoneState()); | 
|  | 5733 | } | 
|  | 5734 |  | 
|  | 5735 | bool AudioPolicyManager::isStateInCall(int state) | 
|  | 5736 | { | 
|  | 5737 | return is_state_in_call(state); | 
|  | 5738 | } | 
|  | 5739 |  | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5740 | void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc) | 
|  | 5741 | { | 
|  | 5742 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--)  { | 
|  | 5743 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); | 
|  | 5744 | if (sourceDesc->mDevice->equals(deviceDesc)) { | 
|  | 5745 | ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle()); | 
|  | 5746 | stopAudioSource(sourceDesc->getHandle()); | 
|  | 5747 | } | 
|  | 5748 | } | 
|  | 5749 |  | 
|  | 5750 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--)  { | 
|  | 5751 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); | 
|  | 5752 | bool release = false; | 
|  | 5753 | for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++)  { | 
|  | 5754 | const struct audio_port_config *source = &patchDesc->mPatch.sources[j]; | 
|  | 5755 | if (source->type == AUDIO_PORT_TYPE_DEVICE && | 
|  | 5756 | source->ext.device.type == deviceDesc->type()) { | 
|  | 5757 | release = true; | 
|  | 5758 | } | 
|  | 5759 | } | 
|  | 5760 | for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++)  { | 
|  | 5761 | const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j]; | 
|  | 5762 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && | 
|  | 5763 | sink->ext.device.type == deviceDesc->type()) { | 
|  | 5764 | release = true; | 
|  | 5765 | } | 
|  | 5766 | } | 
|  | 5767 | if (release) { | 
|  | 5768 | ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle); | 
|  | 5769 | releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid); | 
|  | 5770 | } | 
|  | 5771 | } | 
|  | 5772 | } | 
|  | 5773 |  | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5774 | // Modify the list of surround sound formats supported. | 
| Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5775 | void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) { | 
|  | 5776 | FormatVector &formats = *formatsPtr; | 
| Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5777 | // TODO Set this based on Config properties. | 
|  | 5778 | const bool alwaysForceAC3 = true; | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5779 |  | 
|  | 5780 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( | 
|  | 5781 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); | 
| Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5782 | ALOGD("%s: forced use = %d", __FUNCTION__, forceUse); | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5783 |  | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5784 | // If MANUAL, keep the supported surround sound formats as current enabled ones. | 
|  | 5785 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { | 
|  | 5786 | formats.clear(); | 
|  | 5787 | for (auto it = mSurroundFormats.begin(); it != mSurroundFormats.end(); it++) { | 
|  | 5788 | formats.add(*it); | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5789 | } | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5790 | // Always enable IEC61937 when in MANUAL mode. | 
|  | 5791 | formats.add(AUDIO_FORMAT_IEC61937); | 
|  | 5792 | } else { // NEVER, AUTO or ALWAYS | 
|  | 5793 | // Analyze original support for various formats. | 
|  | 5794 | bool supportsAC3 = false; | 
|  | 5795 | bool supportsOtherSurround = false; | 
|  | 5796 | bool supportsIEC61937 = false; | 
|  | 5797 | mSurroundFormats.clear(); | 
|  | 5798 | for (ssize_t formatIndex = 0; formatIndex < (ssize_t)formats.size(); formatIndex++) { | 
|  | 5799 | audio_format_t format = formats[formatIndex]; | 
|  | 5800 | switch (format) { | 
|  | 5801 | case AUDIO_FORMAT_AC3: | 
|  | 5802 | supportsAC3 = true; | 
|  | 5803 | break; | 
|  | 5804 | case AUDIO_FORMAT_E_AC3: | 
|  | 5805 | case AUDIO_FORMAT_DTS: | 
|  | 5806 | case AUDIO_FORMAT_DTS_HD: | 
|  | 5807 | // If ALWAYS, remove all other surround formats here | 
|  | 5808 | // since we will add them later. | 
|  | 5809 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { | 
|  | 5810 | formats.removeAt(formatIndex); | 
|  | 5811 | formatIndex--; | 
|  | 5812 | } | 
|  | 5813 | supportsOtherSurround = true; | 
|  | 5814 | break; | 
|  | 5815 | case AUDIO_FORMAT_IEC61937: | 
|  | 5816 | supportsIEC61937 = true; | 
|  | 5817 | break; | 
|  | 5818 | default: | 
|  | 5819 | break; | 
|  | 5820 | } | 
|  | 5821 | } | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5822 |  | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5823 | // Modify formats based on surround preferences. | 
|  | 5824 | // If NEVER, remove support for surround formats. | 
|  | 5825 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { | 
|  | 5826 | if (supportsAC3 || supportsOtherSurround || supportsIEC61937) { | 
|  | 5827 | // Remove surround sound related formats. | 
|  | 5828 | for (size_t formatIndex = 0; formatIndex < formats.size(); ) { | 
|  | 5829 | audio_format_t format = formats[formatIndex]; | 
|  | 5830 | switch(format) { | 
|  | 5831 | case AUDIO_FORMAT_AC3: | 
|  | 5832 | case AUDIO_FORMAT_E_AC3: | 
|  | 5833 | case AUDIO_FORMAT_DTS: | 
|  | 5834 | case AUDIO_FORMAT_DTS_HD: | 
|  | 5835 | case AUDIO_FORMAT_IEC61937: | 
|  | 5836 | formats.removeAt(formatIndex); | 
|  | 5837 | break; | 
|  | 5838 | default: | 
|  | 5839 | formatIndex++; // keep it | 
|  | 5840 | break; | 
|  | 5841 | } | 
|  | 5842 | } | 
|  | 5843 | supportsAC3 = false; | 
|  | 5844 | supportsOtherSurround = false; | 
|  | 5845 | supportsIEC61937 = false; | 
|  | 5846 | } | 
|  | 5847 | } else { // AUTO or ALWAYS | 
|  | 5848 | // Most TVs support AC3 even if they do not report it in the EDID. | 
|  | 5849 | if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)) | 
|  | 5850 | && !supportsAC3) { | 
|  | 5851 | formats.add(AUDIO_FORMAT_AC3); | 
|  | 5852 | supportsAC3 = true; | 
|  | 5853 | } | 
|  | 5854 |  | 
|  | 5855 | // If ALWAYS, add support for raw surround formats if all are missing. | 
|  | 5856 | // This assumes that if any of these formats are reported by the HAL | 
|  | 5857 | // then the report is valid and should not be modified. | 
|  | 5858 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { | 
|  | 5859 | formats.add(AUDIO_FORMAT_E_AC3); | 
|  | 5860 | formats.add(AUDIO_FORMAT_DTS); | 
|  | 5861 | formats.add(AUDIO_FORMAT_DTS_HD); | 
|  | 5862 | supportsOtherSurround = true; | 
|  | 5863 | } | 
|  | 5864 |  | 
|  | 5865 | // Add support for IEC61937 if any raw surround supported. | 
|  | 5866 | // The HAL could do this but add it here, just in case. | 
|  | 5867 | if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) { | 
|  | 5868 | formats.add(AUDIO_FORMAT_IEC61937); | 
|  | 5869 | supportsIEC61937 = true; | 
|  | 5870 | } | 
|  | 5871 |  | 
|  | 5872 | // Add reported surround sound formats to enabled surround formats. | 
|  | 5873 | for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) { | 
| Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5874 | audio_format_t format = formats[formatIndex]; | 
|  | 5875 | switch(format) { | 
|  | 5876 | case AUDIO_FORMAT_AC3: | 
|  | 5877 | case AUDIO_FORMAT_E_AC3: | 
|  | 5878 | case AUDIO_FORMAT_DTS: | 
|  | 5879 | case AUDIO_FORMAT_DTS_HD: | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5880 | case AUDIO_FORMAT_AAC_LC: | 
|  | 5881 | case AUDIO_FORMAT_DOLBY_TRUEHD: | 
|  | 5882 | case AUDIO_FORMAT_E_AC3_JOC: | 
|  | 5883 | mSurroundFormats.insert(format); | 
| Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5884 | default: | 
| Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5885 | break; | 
|  | 5886 | } | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5887 | } | 
| Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5888 | } | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5889 | } | 
| Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5890 | } | 
|  | 5891 |  | 
|  | 5892 | // Modify the list of channel masks supported. | 
|  | 5893 | void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) { | 
|  | 5894 | ChannelsVector &channelMasks = *channelMasksPtr; | 
|  | 5895 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( | 
|  | 5896 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); | 
|  | 5897 |  | 
|  | 5898 | // If NEVER, then remove support for channelMasks > stereo. | 
|  | 5899 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { | 
|  | 5900 | for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) { | 
|  | 5901 | audio_channel_mask_t channelMask = channelMasks[maskIndex]; | 
|  | 5902 | if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) { | 
|  | 5903 | ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask); | 
|  | 5904 | channelMasks.removeAt(maskIndex); | 
|  | 5905 | } else { | 
|  | 5906 | maskIndex++; | 
|  | 5907 | } | 
|  | 5908 | } | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5909 | // If ALWAYS or MANUAL, then make sure we at least support 5.1 | 
|  | 5910 | } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS | 
|  | 5911 | || forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { | 
| Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5912 | bool supports5dot1 = false; | 
|  | 5913 | // Are there any channel masks that can be considered "surround"? | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5914 | for (audio_channel_mask_t channelMask : channelMasks) { | 
| Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5915 | if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) { | 
|  | 5916 | supports5dot1 = true; | 
|  | 5917 | break; | 
|  | 5918 | } | 
|  | 5919 | } | 
|  | 5920 | // If not then add 5.1 support. | 
|  | 5921 | if (!supports5dot1) { | 
|  | 5922 | channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1); | 
|  | 5923 | ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__); | 
|  | 5924 | } | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5925 | } | 
|  | 5926 | } | 
|  | 5927 |  | 
| Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5928 | void AudioPolicyManager::updateAudioProfiles(audio_devices_t device, | 
|  | 5929 | audio_io_handle_t ioHandle, | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5930 | AudioProfileVector &profiles) | 
|  | 5931 | { | 
|  | 5932 | String8 reply; | 
| Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5933 |  | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5934 | // Format MUST be checked first to update the list of AudioProfile | 
|  | 5935 | if (profiles.hasDynamicFormat()) { | 
| Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5936 | reply = mpClientInterface->getParameters( | 
|  | 5937 | ioHandle, String8(AudioParameter::keyStreamSupportedFormats)); | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5938 | ALOGV("%s: supported formats %d, %s", __FUNCTION__, ioHandle, reply.string()); | 
| Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5939 | AudioParameter repliedParameters(reply); | 
|  | 5940 | if (repliedParameters.get( | 
| Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5941 | String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) { | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5942 | ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__); | 
|  | 5943 | return; | 
|  | 5944 | } | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5945 | FormatVector formats = formatsFromString(reply.string()); | 
| Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5946 | if (device == AUDIO_DEVICE_OUT_HDMI) { | 
| Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5947 | filterSurroundFormats(&formats); | 
| Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5948 | } | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5949 | profiles.setFormats(formats); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5950 | } | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5951 |  | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5952 | for (audio_format_t format : profiles.getSupportedFormats()) { | 
| Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5953 | ChannelsVector channelMasks; | 
|  | 5954 | SampleRateVector samplingRates; | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5955 | AudioParameter requestedParameters; | 
| Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5956 | requestedParameters.addInt(String8(AudioParameter::keyFormat), format); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5957 |  | 
|  | 5958 | if (profiles.hasDynamicRateFor(format)) { | 
| Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5959 | reply = mpClientInterface->getParameters( | 
|  | 5960 | ioHandle, | 
|  | 5961 | requestedParameters.toString() + ";" + | 
|  | 5962 | AudioParameter::keyStreamSupportedSamplingRates); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5963 | ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string()); | 
| Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5964 | AudioParameter repliedParameters(reply); | 
|  | 5965 | if (repliedParameters.get( | 
| Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5966 | String8(AudioParameter::keyStreamSupportedSamplingRates), reply) == NO_ERROR) { | 
| Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5967 | samplingRates = samplingRatesFromString(reply.string()); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5968 | } | 
|  | 5969 | } | 
|  | 5970 | if (profiles.hasDynamicChannelsFor(format)) { | 
|  | 5971 | reply = mpClientInterface->getParameters(ioHandle, | 
|  | 5972 | requestedParameters.toString() + ";" + | 
| Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5973 | AudioParameter::keyStreamSupportedChannels); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5974 | ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string()); | 
| Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5975 | AudioParameter repliedParameters(reply); | 
|  | 5976 | if (repliedParameters.get( | 
| Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5977 | String8(AudioParameter::keyStreamSupportedChannels), reply) == NO_ERROR) { | 
| Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5978 | channelMasks = channelMasksFromString(reply.string()); | 
| Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5979 | if (device == AUDIO_DEVICE_OUT_HDMI) { | 
|  | 5980 | filterSurroundChannelMasks(&channelMasks); | 
|  | 5981 | } | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5982 | } | 
|  | 5983 | } | 
| Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5984 | profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates)); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5985 | } | 
|  | 5986 | } | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5987 |  | 
| Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5988 | status_t AudioPolicyManager::installPatch(const char *caller, | 
|  | 5989 | audio_patch_handle_t *patchHandle, | 
|  | 5990 | AudioIODescriptorInterface *ioDescriptor, | 
|  | 5991 | const struct audio_patch *patch, | 
|  | 5992 | int delayMs) | 
|  | 5993 | { | 
|  | 5994 | ssize_t index = mAudioPatches.indexOfKey( | 
|  | 5995 | patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE ? | 
|  | 5996 | *patchHandle : ioDescriptor->getPatchHandle()); | 
|  | 5997 | sp<AudioPatch> patchDesc; | 
|  | 5998 | status_t status = installPatch( | 
|  | 5999 | caller, index, patchHandle, patch, delayMs, mUidCached, &patchDesc); | 
|  | 6000 | if (status == NO_ERROR) { | 
|  | 6001 | ioDescriptor->setPatchHandle(patchDesc->mHandle); | 
|  | 6002 | } | 
|  | 6003 | return status; | 
|  | 6004 | } | 
|  | 6005 |  | 
|  | 6006 | status_t AudioPolicyManager::installPatch(const char *caller, | 
|  | 6007 | ssize_t index, | 
|  | 6008 | audio_patch_handle_t *patchHandle, | 
|  | 6009 | const struct audio_patch *patch, | 
|  | 6010 | int delayMs, | 
|  | 6011 | uid_t uid, | 
|  | 6012 | sp<AudioPatch> *patchDescPtr) | 
|  | 6013 | { | 
|  | 6014 | sp<AudioPatch> patchDesc; | 
|  | 6015 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; | 
|  | 6016 | if (index >= 0) { | 
|  | 6017 | patchDesc = mAudioPatches.valueAt(index); | 
|  | 6018 | afPatchHandle = patchDesc->mAfPatchHandle; | 
|  | 6019 | } | 
|  | 6020 |  | 
|  | 6021 | status_t status = mpClientInterface->createAudioPatch(patch, &afPatchHandle, delayMs); | 
|  | 6022 | ALOGV("%s() AF::createAudioPatch returned %d patchHandle %d num_sources %d num_sinks %d", | 
|  | 6023 | caller, status, afPatchHandle, patch->num_sources, patch->num_sinks); | 
|  | 6024 | if (status == NO_ERROR) { | 
|  | 6025 | if (index < 0) { | 
|  | 6026 | patchDesc = new AudioPatch(patch, uid); | 
|  | 6027 | addAudioPatch(patchDesc->mHandle, patchDesc); | 
|  | 6028 | } else { | 
|  | 6029 | patchDesc->mPatch = *patch; | 
|  | 6030 | } | 
|  | 6031 | patchDesc->mAfPatchHandle = afPatchHandle; | 
|  | 6032 | if (patchHandle) { | 
|  | 6033 | *patchHandle = patchDesc->mHandle; | 
|  | 6034 | } | 
|  | 6035 | nextAudioPortGeneration(); | 
|  | 6036 | mpClientInterface->onAudioPatchListUpdate(); | 
|  | 6037 | } | 
|  | 6038 | if (patchDescPtr) *patchDescPtr = patchDesc; | 
|  | 6039 | return status; | 
|  | 6040 | } | 
|  | 6041 |  | 
| Mikhail Naganov | 1b2a794 | 2017-12-08 10:18:09 -0800 | [diff] [blame] | 6042 | } // namespace android |