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)) { |
| 508 | AudioSessionCollection activeSessions = |
| 509 | activeDesc->getAudioSessions(true /*activeOnly*/); |
| 510 | for (size_t j = 0; j < activeSessions.size(); j++) { |
| 511 | audio_session_t activeSession = activeSessions.keyAt(j); |
| 512 | stopInput(activeDesc->mIoHandle, activeSession); |
| 513 | releaseInput(activeDesc->mIoHandle, activeSession); |
| 514 | } |
Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 515 | } |
| 516 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 517 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 518 | |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 519 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 520 | status_t status = mpClientInterface->createAudioPatch( |
| 521 | patchBuilder.patch(), &afPatchHandle, delayMs); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 522 | ALOGW_IF(status != NO_ERROR, |
| 523 | "%s() error %d creating %s audio patch", __func__, status, isRx ? "RX" : "TX"); |
| 524 | sp<AudioPatch> audioPatch; |
| 525 | if (status == NO_ERROR) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 526 | audioPatch = new AudioPatch(patchBuilder.patch(), mUidCached); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 527 | audioPatch->mAfPatchHandle = afPatchHandle; |
| 528 | audioPatch->mUid = mUidCached; |
| 529 | } |
| 530 | return audioPatch; |
| 531 | } |
| 532 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 533 | sp<DeviceDescriptor> AudioPolicyManager::findDevice( |
| 534 | const DeviceVector& devices, audio_devices_t device) { |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 535 | DeviceVector deviceList = devices.getDevicesFromTypeMask(device); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 536 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 537 | "%s() selected device type %#x is not in devices list", __func__, device); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 538 | return deviceList.itemAt(0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 539 | } |
| 540 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 541 | void AudioPolicyManager::setPhoneState(audio_mode_t state) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 542 | { |
| 543 | ALOGV("setPhoneState() state %d", state); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 544 | // store previous phone state for management of sonification strategy below |
| 545 | int oldState = mEngine->getPhoneState(); |
| 546 | |
| 547 | if (mEngine->setPhoneState(state) != NO_ERROR) { |
| 548 | ALOGW("setPhoneState() invalid or same state %d", state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 549 | return; |
| 550 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 551 | /// Opens: can these line be executed after the switch of volume curves??? |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 552 | if (isStateInCall(oldState)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 553 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 554 | // force reevaluating accessibility routing when call stops |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 555 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 556 | } |
| 557 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 558 | /** |
| 559 | * Switching to or from incall state or switching between telephony and VoIP lead to force |
| 560 | * routing command. |
| 561 | */ |
| 562 | bool force = ((is_state_in_call(oldState) != is_state_in_call(state)) |
| 563 | || (is_state_in_call(state) && (state != oldState))); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 564 | |
| 565 | // check for device and output changes triggered by new phone state |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 566 | checkForDeviceAndOutputChanges(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 567 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 568 | int delayMs = 0; |
| 569 | if (isStateInCall(state)) { |
| 570 | nsecs_t sysTime = systemTime(); |
| 571 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 572 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 573 | // mute media and sonification strategies and delay device switch by the largest |
| 574 | // latency of any output where either strategy is active. |
| 575 | // 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] | 576 | if ((isStrategyActive(desc, STRATEGY_MEDIA, |
| 577 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 578 | sysTime) || |
| 579 | isStrategyActive(desc, STRATEGY_SONIFICATION, |
| 580 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 581 | sysTime)) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 582 | (delayMs < (int)desc->latency()*2)) { |
| 583 | delayMs = desc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 584 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 585 | setStrategyMute(STRATEGY_MEDIA, true, desc); |
| 586 | setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 587 | getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/)); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 588 | setStrategyMute(STRATEGY_SONIFICATION, true, desc); |
| 589 | setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 590 | getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/)); |
| 591 | } |
| 592 | } |
| 593 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 594 | if (hasPrimaryOutput()) { |
| 595 | // Note that despite the fact that getNewOutputDevice() is called on the primary output, |
| 596 | // the device returned is not necessarily reachable via this output |
| 597 | audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 598 | // force routing command to audio hardware when ending call |
| 599 | // even if no device change is needed |
| 600 | if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) { |
| 601 | rxDevice = mPrimaryOutput->device(); |
| 602 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 603 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 604 | if (state == AUDIO_MODE_IN_CALL) { |
| 605 | updateCallRouting(rxDevice, delayMs); |
| 606 | } else if (oldState == AUDIO_MODE_IN_CALL) { |
| 607 | if (mCallRxPatch != 0) { |
| 608 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 609 | mCallRxPatch.clear(); |
| 610 | } |
| 611 | if (mCallTxPatch != 0) { |
| 612 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 613 | mCallTxPatch.clear(); |
| 614 | } |
| 615 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
| 616 | } else { |
| 617 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 618 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 619 | } |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 620 | |
| 621 | // reevaluate routing on all outputs in case tracks have been started during the call |
| 622 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 623 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 624 | audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/); |
| 625 | if (state != AUDIO_MODE_IN_CALL || desc != mPrimaryOutput) { |
Yung Ti Su | f60c824 | 2018-05-10 18:07:26 +0800 | [diff] [blame] | 626 | setOutputDevice(desc, newDevice, (newDevice != AUDIO_DEVICE_NONE), 0 /*delayMs*/); |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 627 | } |
| 628 | } |
| 629 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 630 | if (isStateInCall(state)) { |
| 631 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 632 | // force reevaluating accessibility routing when call starts |
| 633 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | // 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] | 637 | if (state == AUDIO_MODE_RINGTONE && |
| 638 | isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 639 | mLimitRingtoneVolume = true; |
| 640 | } else { |
| 641 | mLimitRingtoneVolume = false; |
| 642 | } |
| 643 | } |
| 644 | |
Jean-Michel Trivi | 887a9ed | 2015-03-31 18:02:24 -0700 | [diff] [blame] | 645 | audio_mode_t AudioPolicyManager::getPhoneState() { |
| 646 | return mEngine->getPhoneState(); |
| 647 | } |
| 648 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 649 | void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 650 | audio_policy_forced_cfg_t config) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 651 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 652 | ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState()); |
Eric Laurent | 8dc87a6 | 2017-05-16 19:00:40 -0700 | [diff] [blame] | 653 | if (config == mEngine->getForceUse(usage)) { |
| 654 | return; |
| 655 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 656 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 657 | if (mEngine->setForceUse(usage, config) != NO_ERROR) { |
| 658 | ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage); |
| 659 | return; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 660 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 661 | bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) || |
| 662 | (usage == AUDIO_POLICY_FORCE_FOR_DOCK) || |
| 663 | (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 664 | |
| 665 | // check for device and output changes triggered by new force usage |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 666 | checkForDeviceAndOutputChanges(); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 667 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 668 | //FIXME: workaround for truncated touch sounds |
| 669 | // to be removed when the problem is handled by system UI |
| 670 | uint32_t delayMs = 0; |
| 671 | uint32_t waitMs = 0; |
| 672 | if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) { |
| 673 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 674 | } |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 675 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 676 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/); |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 677 | waitMs = updateCallRouting(newDevice, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 678 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 679 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 680 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
| 681 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/); |
| 682 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 683 | waitMs = setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE), |
| 684 | delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 685 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 686 | if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 687 | applyStreamVolumes(outputDesc, newDevice, waitMs, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 691 | for (const auto& activeDesc : mInputs.getActiveInputs()) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 692 | audio_devices_t newDevice = getNewInputDevice(activeDesc); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 693 | // 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] | 694 | if (activeDesc->mProfile->getSupportedDevices().types() & |
| 695 | (newDevice & ~AUDIO_DEVICE_BIT_IN)) { |
| 696 | setInputDevice(activeDesc->mIoHandle, newDevice); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 697 | } else { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 698 | closeInput(activeDesc->mIoHandle); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 699 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 700 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 701 | } |
| 702 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 703 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 704 | { |
| 705 | ALOGV("setSystemProperty() property %s, value %s", property, value); |
| 706 | } |
| 707 | |
| 708 | // Find a direct output profile compatible with the parameters passed, even if the input flags do |
| 709 | // not explicitly request a direct output |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 710 | sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput( |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 711 | audio_devices_t device, |
| 712 | uint32_t samplingRate, |
| 713 | audio_format_t format, |
| 714 | audio_channel_mask_t channelMask, |
| 715 | audio_output_flags_t flags) |
| 716 | { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 717 | // only retain flags that will drive the direct output profile selection |
| 718 | // if explicitly requested |
| 719 | static const uint32_t kRelevantFlags = |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 720 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | |
| 721 | AUDIO_OUTPUT_FLAG_VOIP_RX); |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 722 | flags = |
| 723 | (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); |
| 724 | |
| 725 | sp<IOProfile> profile; |
| 726 | |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 727 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 728 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 729 | if (!curProfile->isCompatibleProfile(device, String8(""), |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 730 | samplingRate, NULL /*updatedSamplingRate*/, |
| 731 | format, NULL /*updatedFormat*/, |
| 732 | channelMask, NULL /*updatedChannelMask*/, |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 733 | flags)) { |
| 734 | continue; |
| 735 | } |
| 736 | // reject profiles not corresponding to a device currently available |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 737 | if ((mAvailableOutputDevices.types() & curProfile->getSupportedDevicesType()) == 0) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 738 | continue; |
| 739 | } |
| 740 | // 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] | 741 | if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 742 | continue; |
| 743 | } |
| 744 | profile = curProfile; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 745 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 746 | break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 747 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 748 | } |
| 749 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 750 | return profile; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 751 | } |
| 752 | |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 753 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 754 | { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 755 | routing_strategy strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 756 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Andy Hung | c990152 | 2017-11-10 20:07:54 -0800 | [diff] [blame] | 757 | |
| 758 | // Note that related method getOutputForAttr() uses getOutputForDevice() not selectOutput(). |
| 759 | // We use selectOutput() here since we don't have the desired AudioTrack sample rate, |
| 760 | // format, flags, etc. This may result in some discrepancy for functions that utilize |
| 761 | // getOutput() solely on audio_stream_type such as AudioSystem::getOutputFrameCount() |
| 762 | // and AudioSystem::getOutputSamplingRate(). |
| 763 | |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 764 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 765 | 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] | 766 | |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 767 | ALOGV("getOutput() stream %d selected device %08x, output %d", stream, device, output); |
| 768 | return output; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 769 | } |
| 770 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 771 | status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, |
| 772 | audio_io_handle_t *output, |
| 773 | audio_session_t session, |
| 774 | audio_stream_type_t *stream, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 775 | uid_t uid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 776 | const audio_config_t *config, |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 777 | audio_output_flags_t *flags, |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 778 | audio_port_handle_t *selectedDeviceId, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 779 | audio_port_handle_t *portId) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 780 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 781 | audio_attributes_t attributes; |
| 782 | if (attr != NULL) { |
| 783 | if (!isValidAttributes(attr)) { |
| 784 | ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]", |
| 785 | attr->usage, attr->content_type, attr->flags, |
| 786 | attr->tags); |
| 787 | return BAD_VALUE; |
| 788 | } |
| 789 | attributes = *attr; |
| 790 | } else { |
| 791 | if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) { |
| 792 | ALOGE("getOutputForAttr(): invalid stream type"); |
| 793 | return BAD_VALUE; |
| 794 | } |
| 795 | stream_type_to_audio_attributes(*stream, &attributes); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 796 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 797 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 798 | ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x" |
| 799 | " session %d selectedDeviceId %d", |
| 800 | attributes.usage, attributes.content_type, attributes.tags, attributes.flags, |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 801 | session, *selectedDeviceId); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 802 | |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 803 | // TODO: check for existing client for this port ID |
| 804 | if (*portId == AUDIO_PORT_HANDLE_NONE) { |
| 805 | *portId = AudioPort::getNextUniqueId(); |
| 806 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 807 | |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 808 | // First check for explicit routing (eg. setPreferredDevice) |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 809 | sp<DeviceDescriptor> deviceDesc; |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 810 | if (*selectedDeviceId != AUDIO_PORT_HANDLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 811 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(*selectedDeviceId); |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 812 | } else { |
| 813 | // If no explict route, is there a matching dynamic policy that applies? |
| 814 | sp<SwAudioOutputDescriptor> desc; |
| 815 | if (mPolicyMixes.getOutputForAttr(attributes, uid, desc) == NO_ERROR) { |
| 816 | ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr"); |
| 817 | if (!audio_has_proportional_frames(config->format)) { |
| 818 | return BAD_VALUE; |
| 819 | } |
| 820 | *stream = streamTypefromAttributesInt(&attributes); |
| 821 | *output = desc->mIoHandle; |
| 822 | ALOGV("getOutputForAttr() returns output %d", *output); |
| 823 | return NO_ERROR; |
| 824 | } |
| 825 | |
| 826 | // Virtual sources must always be dynamicaly or explicitly routed |
| 827 | if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 828 | ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE"); |
| 829 | return BAD_VALUE; |
| 830 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 831 | } |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 832 | |
| 833 | // Virtual sources must always be dynamicaly or explicitly routed |
| 834 | if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 835 | ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE"); |
| 836 | return BAD_VALUE; |
| 837 | } |
| 838 | |
| 839 | *stream = streamTypefromAttributesInt(&attributes); |
| 840 | |
| 841 | // TODO: Should this happen only if an explicit route is active? |
| 842 | // the previous code structure meant that this would always happen which |
| 843 | // would appear to result in adding a null deviceDesc when not using an |
| 844 | // explicit route. Is that the intended and necessary behavior? |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 845 | mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 846 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 847 | routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 848 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 849 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 850 | if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 851 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 852 | } |
| 853 | |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 854 | ALOGV("getOutputForAttr() device 0x%x, sampling rate %d, format %#x, channel mask %#x, " |
| 855 | "flags %#x", |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 856 | device, config->sample_rate, config->format, config->channel_mask, *flags); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 857 | |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 858 | *output = getOutputForDevice(device, session, *stream, config, flags); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 859 | if (*output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 860 | mOutputRoutes.removeRoute(session); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 861 | return INVALID_OPERATION; |
| 862 | } |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 863 | |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 864 | DeviceVector outputDevices = mAvailableOutputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 865 | *selectedDeviceId = outputDevices.size() > 0 ? outputDevices.itemAt(0)->getId() |
| 866 | : AUDIO_PORT_HANDLE_NONE; |
| 867 | |
| 868 | ALOGV(" getOutputForAttr() returns output %d selectedDeviceId %d", *output, *selectedDeviceId); |
| 869 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 870 | return NO_ERROR; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | audio_io_handle_t AudioPolicyManager::getOutputForDevice( |
| 874 | audio_devices_t device, |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 875 | audio_session_t session, |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 876 | audio_stream_type_t stream, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 877 | const audio_config_t *config, |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 878 | audio_output_flags_t *flags) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 879 | { |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 880 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 881 | status_t status; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 882 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 883 | // open a direct output if required by specified parameters |
| 884 | //force direct flag if offload flag is set: offloading implies a direct output stream |
| 885 | // and all common behaviors are driven by checking only the direct flag |
| 886 | // this should normally be set appropriately in the policy configuration file |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 887 | if ((*flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 888 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 889 | } |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 890 | if ((*flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 891 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 892 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 893 | // only allow deep buffering for music stream type |
| 894 | if (stream != AUDIO_STREAM_MUSIC) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 895 | *flags = (audio_output_flags_t)(*flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 896 | } else if (/* stream == AUDIO_STREAM_MUSIC && */ |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 897 | *flags == AUDIO_OUTPUT_FLAG_NONE && |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 898 | property_get_bool("audio.deep_buffer.media", false /* default_value */)) { |
| 899 | // use DEEP_BUFFER as default output for music stream type |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 900 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER; |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 901 | } |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 902 | if (stream == AUDIO_STREAM_TTS) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 903 | *flags = AUDIO_OUTPUT_FLAG_TTS; |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 904 | } else if (stream == AUDIO_STREAM_VOICE_CALL && |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 905 | audio_is_linear_pcm(config->format)) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 906 | *flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX | |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 907 | AUDIO_OUTPUT_FLAG_DIRECT); |
| 908 | ALOGV("Set VoIP and Direct output flags for PCM format"); |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 909 | } else if (device == AUDIO_DEVICE_OUT_TELEPHONY_TX && |
| 910 | stream == AUDIO_STREAM_MUSIC && |
| 911 | audio_is_linear_pcm(config->format) && |
| 912 | isInCall()) { |
| 913 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INCALL_MUSIC; |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 914 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 915 | |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 916 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 917 | sp<IOProfile> profile; |
| 918 | |
| 919 | // 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] | 920 | // and not explicitly requested |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 921 | if (((*flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 922 | audio_is_linear_pcm(config->format) && config->sample_rate <= SAMPLE_RATE_HZ_MAX && |
| 923 | audio_channel_count_from_out_mask(config->channel_mask) <= 2) { |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 924 | goto non_direct_output; |
| 925 | } |
| 926 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 927 | // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled. |
| 928 | // This prevents creating an offloaded track and tearing it down immediately after start |
| 929 | // when audioflinger detects there is an active non offloadable effect. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 930 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 931 | // This may prevent offloading in rare situations where effects are left active by apps |
| 932 | // in the background. |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 933 | |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 934 | if (((*flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 935 | !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 936 | profile = getProfileForDirectOutput(device, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 937 | config->sample_rate, |
| 938 | config->format, |
| 939 | config->channel_mask, |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 940 | (audio_output_flags_t)*flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 941 | } |
| 942 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 943 | if (profile != 0) { |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 944 | // exclusive outputs for MMAP and Offload are enforced by different session ids. |
| 945 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 946 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 947 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { |
| 948 | // reuse direct output if currently open by the same client |
| 949 | // and configured with same parameters |
| 950 | if ((config->sample_rate == desc->mSamplingRate) && |
Andy Hung | 5659ed5 | 2018-04-30 10:31:26 -0700 | [diff] [blame] | 951 | (config->format == desc->mFormat) && |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 952 | (config->channel_mask == desc->mChannelMask) && |
| 953 | (session == desc->mDirectClientSession)) { |
| 954 | desc->mDirectOpenCount++; |
| 955 | ALOGI("getOutputForDevice() reusing direct output %d for session %d", |
| 956 | mOutputs.keyAt(i), session); |
| 957 | return mOutputs.keyAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 958 | } |
| 959 | } |
| 960 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 961 | |
| 962 | if (!profile->canOpenNewIo()) { |
| 963 | goto non_direct_output; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 964 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 965 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 966 | sp<SwAudioOutputDescriptor> outputDesc = |
| 967 | new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 968 | |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 969 | DeviceVector outputDevices = mAvailableOutputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 970 | String8 address = outputDevices.size() > 0 ? outputDevices.itemAt(0)->mAddress |
| 971 | : String8(""); |
| 972 | |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 973 | status = outputDesc->open(config, device, address, stream, *flags, &output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 974 | |
| 975 | // only accept an output with the requested parameters |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 976 | if (status != NO_ERROR || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 977 | (config->sample_rate != 0 && config->sample_rate != outputDesc->mSamplingRate) || |
Andy Hung | 5659ed5 | 2018-04-30 10:31:26 -0700 | [diff] [blame] | 978 | (config->format != AUDIO_FORMAT_DEFAULT && config->format != outputDesc->mFormat) || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 979 | (config->channel_mask != 0 && config->channel_mask != outputDesc->mChannelMask)) { |
| 980 | ALOGV("getOutputForDevice() failed opening direct output: output %d sample rate %d %d," |
| 981 | "format %d %d, channel mask %04x %04x", output, config->sample_rate, |
| 982 | outputDesc->mSamplingRate, config->format, outputDesc->mFormat, |
| 983 | config->channel_mask, outputDesc->mChannelMask); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 984 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 985 | outputDesc->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 986 | } |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 987 | // 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] | 988 | if (audio_is_linear_pcm(config->format) && |
| 989 | config->sample_rate <= SAMPLE_RATE_HZ_MAX) { |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 990 | goto non_direct_output; |
| 991 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 992 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 993 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 994 | outputDesc->mRefCount[stream] = 0; |
| 995 | outputDesc->mStopTime[stream] = 0; |
| 996 | outputDesc->mDirectOpenCount = 1; |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 997 | outputDesc->mDirectClientSession = session; |
| 998 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 999 | addOutput(output, outputDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1000 | mPreviousOutputs = mOutputs; |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 1001 | ALOGV("getOutputForDevice() returns new direct output %d", output); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1002 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1003 | return output; |
| 1004 | } |
| 1005 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 1006 | non_direct_output: |
Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 1007 | |
| 1008 | // A request for HW A/V sync cannot fallback to a mixed output because time |
| 1009 | // stamps are embedded in audio data |
Phil Burk | 2d05993 | 2018-02-15 15:55:11 -0800 | [diff] [blame] | 1010 | 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] | 1011 | return AUDIO_IO_HANDLE_NONE; |
| 1012 | } |
| 1013 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1014 | // ignoring channel mask due to downmix capability in mixer |
| 1015 | |
| 1016 | // open a non direct output |
| 1017 | |
| 1018 | // for non direct outputs, only PCM is supported |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1019 | if (audio_is_linear_pcm(config->format)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1020 | // get which output is suitable for the specified stream. The actual |
| 1021 | // routing change will happen when startOutput() will be called |
| 1022 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 1023 | |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1024 | // 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] | 1025 | *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_DIRECT); |
| 1026 | output = selectOutput(outputs, *flags, config->format); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1027 | } |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 1028 | ALOGW_IF((output == 0), "getOutputForDevice() could not find output for stream %d, " |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1029 | "sampling rate %d, format %#x, channels %#x, flags %#x", |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1030 | stream, config->sample_rate, config->format, config->channel_mask, *flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1031 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1032 | return output; |
| 1033 | } |
| 1034 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1035 | 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] | 1036 | audio_output_flags_t flags, |
| 1037 | audio_format_t format) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1038 | { |
| 1039 | // select one output among several that provide a path to a particular device or set of |
| 1040 | // devices (the list was previously build by getOutputsForDevice()). |
| 1041 | // The priority is as follows: |
| 1042 | // 1: the output with the highest number of requested policy flags |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1043 | // 2: the output with the bit depth the closest to the requested one |
| 1044 | // 3: the primary output |
| 1045 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1046 | |
| 1047 | if (outputs.size() == 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1048 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1049 | } |
| 1050 | if (outputs.size() == 1) { |
| 1051 | return outputs[0]; |
| 1052 | } |
| 1053 | |
| 1054 | int maxCommonFlags = 0; |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1055 | audio_io_handle_t outputForFlags = AUDIO_IO_HANDLE_NONE; |
| 1056 | audio_io_handle_t outputForPrimary = AUDIO_IO_HANDLE_NONE; |
| 1057 | audio_io_handle_t outputForFormat = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1058 | audio_format_t bestFormat = AUDIO_FORMAT_INVALID; |
| 1059 | audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1060 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1061 | for (audio_io_handle_t output : outputs) { |
| 1062 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1063 | if (!outputDesc->isDuplicated()) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1064 | // if a valid format is specified, skip output if not compatible |
| 1065 | if (format != AUDIO_FORMAT_INVALID) { |
| 1066 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
Andy Hung | 5659ed5 | 2018-04-30 10:31:26 -0700 | [diff] [blame] | 1067 | if (format != outputDesc->mFormat) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1068 | continue; |
| 1069 | } |
| 1070 | } else if (!audio_is_linear_pcm(format)) { |
| 1071 | continue; |
| 1072 | } |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1073 | if (AudioPort::isBetterFormatMatch( |
| 1074 | outputDesc->mFormat, bestFormat, format)) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1075 | outputForFormat = output; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1076 | bestFormat = outputDesc->mFormat; |
| 1077 | } |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1078 | } |
| 1079 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1080 | int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags); |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1081 | if (commonFlags >= maxCommonFlags) { |
| 1082 | if (commonFlags == maxCommonFlags) { |
Andy Hung | c990152 | 2017-11-10 20:07:54 -0800 | [diff] [blame] | 1083 | if (format != AUDIO_FORMAT_INVALID |
| 1084 | && AudioPort::isBetterFormatMatch( |
| 1085 | outputDesc->mFormat, bestFormatForFlags, format)) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1086 | outputForFlags = output; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1087 | bestFormatForFlags = outputDesc->mFormat; |
| 1088 | } |
| 1089 | } else { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1090 | outputForFlags = output; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1091 | maxCommonFlags = commonFlags; |
| 1092 | bestFormatForFlags = outputDesc->mFormat; |
| 1093 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1094 | ALOGV("selectOutput() commonFlags for output %d, %04x", output, commonFlags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1095 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1096 | if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1097 | outputForPrimary = output; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1098 | } |
| 1099 | } |
| 1100 | } |
| 1101 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1102 | if (outputForFlags != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1103 | return outputForFlags; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1104 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1105 | if (outputForFormat != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1106 | return outputForFormat; |
| 1107 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1108 | if (outputForPrimary != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1109 | return outputForPrimary; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | return outputs[0]; |
| 1113 | } |
| 1114 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1115 | status_t AudioPolicyManager::startOutput(audio_io_handle_t output, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1116 | audio_stream_type_t stream, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1117 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1118 | { |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1119 | ALOGV("startOutput() output %d, stream %d, session %d", |
| 1120 | output, stream, session); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1121 | ssize_t index = mOutputs.indexOfKey(output); |
| 1122 | if (index < 0) { |
| 1123 | ALOGW("startOutput() unknown output %d", output); |
| 1124 | return BAD_VALUE; |
| 1125 | } |
| 1126 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1127 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
| 1128 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1129 | status_t status = outputDesc->start(); |
| 1130 | if (status != NO_ERROR) { |
| 1131 | return status; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1132 | } |
| 1133 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1134 | // Routing? |
| 1135 | mOutputRoutes.incRouteActivity(session); |
| 1136 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1137 | audio_devices_t newDevice; |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1138 | AudioMix *policyMix = NULL; |
| 1139 | const char *address = NULL; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1140 | if (outputDesc->mPolicyMix != NULL) { |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1141 | policyMix = outputDesc->mPolicyMix; |
| 1142 | address = policyMix->mDeviceAddress.string(); |
| 1143 | if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 1144 | newDevice = policyMix->mDeviceType; |
| 1145 | } else { |
| 1146 | newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
| 1147 | } |
Eric Laurent | 2157f5b | 2018-04-25 18:33:02 -0700 | [diff] [blame] | 1148 | } else if (mOutputRoutes.getAndClearRouteChanged(session)) { |
Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 1149 | newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 1150 | if (newDevice != outputDesc->device()) { |
| 1151 | checkStrategyRoute(getStrategy(stream), output); |
| 1152 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1153 | } else { |
| 1154 | newDevice = AUDIO_DEVICE_NONE; |
| 1155 | } |
| 1156 | |
| 1157 | uint32_t delayMs = 0; |
| 1158 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1159 | status = startSource(outputDesc, stream, newDevice, address, &delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1160 | |
| 1161 | if (status != NO_ERROR) { |
| 1162 | mOutputRoutes.decRouteActivity(session); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1163 | outputDesc->stop(); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1164 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1165 | } |
| 1166 | // Automatically enable the remote submix input when output is started on a re routing mix |
| 1167 | // of type MIX_TYPE_RECORDERS |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1168 | if (audio_is_remote_submix_device(newDevice) && policyMix != NULL && |
| 1169 | policyMix->mMixType == MIX_TYPE_RECORDERS) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1170 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1171 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1172 | address, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1173 | "remote-submix"); |
| 1174 | } |
| 1175 | |
| 1176 | if (delayMs != 0) { |
| 1177 | usleep(delayMs * 1000); |
| 1178 | } |
| 1179 | |
| 1180 | return status; |
| 1181 | } |
| 1182 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 1183 | status_t AudioPolicyManager::startSource(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1184 | audio_stream_type_t stream, |
| 1185 | audio_devices_t device, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1186 | const char *address, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1187 | uint32_t *delayMs) |
| 1188 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1189 | // cannot start playback of STREAM_TTS if any other output is being used |
| 1190 | uint32_t beaconMuteLatency = 0; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1191 | |
| 1192 | *delayMs = 0; |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1193 | if (stream == AUDIO_STREAM_TTS) { |
| 1194 | ALOGV("\t found BEACON stream"); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 1195 | if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1196 | return INVALID_OPERATION; |
| 1197 | } else { |
| 1198 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); |
| 1199 | } |
| 1200 | } else { |
| 1201 | // some playback other than beacon starts |
| 1202 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); |
| 1203 | } |
| 1204 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1205 | // 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] | 1206 | // check active before incrementing usage count |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1207 | bool force = !outputDesc->isActive() && |
| 1208 | (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1209 | |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1210 | // requiresMuteCheck is false when we can bypass mute strategy. |
| 1211 | // It covers a common case when there is no materially active audio |
| 1212 | // and muting would result in unnecessary delay and dropped audio. |
| 1213 | const uint32_t outputLatencyMs = outputDesc->latency(); |
| 1214 | bool requiresMuteCheck = outputDesc->isActive(outputLatencyMs * 2); // account for drain |
| 1215 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1216 | // increment usage count for this stream on the requested output: |
| 1217 | // NOTE that the usage count is the same for duplicated output and hardware output which is |
| 1218 | // necessary for a correct control of hardware output routing by startOutput() and stopOutput() |
| 1219 | outputDesc->changeRefCount(stream, 1); |
| 1220 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1221 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1222 | selectOutputForMusicEffects(); |
| 1223 | } |
| 1224 | |
Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 1225 | if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1226 | // starting an output being rerouted? |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1227 | if (device == AUDIO_DEVICE_NONE) { |
| 1228 | device = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1229 | } |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1230 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1231 | routing_strategy strategy = getStrategy(stream); |
| 1232 | bool shouldWait = (strategy == STRATEGY_SONIFICATION) || |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1233 | (strategy == STRATEGY_SONIFICATION_RESPECTFUL) || |
| 1234 | (beaconMuteLatency > 0); |
| 1235 | uint32_t waitMs = beaconMuteLatency; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1236 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1237 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1238 | if (desc != outputDesc) { |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1239 | // An output has a shared device if |
| 1240 | // - managed by the same hw module |
| 1241 | // - supports the currently selected device |
| 1242 | const bool sharedDevice = outputDesc->sharesHwModuleWith(desc) |
| 1243 | && (desc->supportedDevices() & device) != AUDIO_DEVICE_NONE; |
| 1244 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1245 | // force a device change if any other output is: |
| 1246 | // - managed by the same hw module |
Jean-Michel Trivi | 4a5b481 | 2018-02-08 17:22:32 +0000 | [diff] [blame] | 1247 | // - supports currently selected device |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1248 | // - has a current device selection that differs from selected device. |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1249 | // - has an active audio patch |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1250 | // 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] | 1251 | // change the device currently selected by the other output. |
| 1252 | if (sharedDevice && |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1253 | desc->device() != device && |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1254 | desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1255 | force = true; |
| 1256 | } |
| 1257 | // 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] | 1258 | // a notification so that audio focus effect can propagate, or that a mute/unmute |
| 1259 | // event occurred for beacon |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1260 | const uint32_t latencyMs = desc->latency(); |
| 1261 | const bool isActive = desc->isActive(latencyMs * 2); // account for drain |
| 1262 | |
| 1263 | if (shouldWait && isActive && (waitMs < latencyMs)) { |
| 1264 | waitMs = latencyMs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1265 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1266 | |
| 1267 | // Require mute check if another output is on a shared device |
| 1268 | // and currently active to have proper drain and avoid pops. |
| 1269 | // Note restoring AudioTracks onto this output needs to invoke |
| 1270 | // a volume ramp if there is no mute. |
| 1271 | requiresMuteCheck |= sharedDevice && isActive; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1272 | } |
| 1273 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1274 | |
| 1275 | const uint32_t muteWaitMs = |
| 1276 | setOutputDevice(outputDesc, device, force, 0, NULL, address, requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1277 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1278 | // apply volume rules for current stream and device if necessary |
| 1279 | checkAndSetVolume(stream, |
Shuhei Miyazaki | 6fe7033 | 2017-07-31 15:21:28 +0900 | [diff] [blame] | 1280 | mVolumeCurves->getVolumeIndex(stream, outputDesc->device()), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1281 | outputDesc, |
Shuhei Miyazaki | 6fe7033 | 2017-07-31 15:21:28 +0900 | [diff] [blame] | 1282 | outputDesc->device()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1283 | |
| 1284 | // update the outputs if starting an output with a stream that can affect notification |
| 1285 | // routing |
| 1286 | handleNotificationRoutingForStream(stream); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1287 | |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 1288 | // force reevaluating accessibility routing when ringtone or alarm starts |
| 1289 | if (strategy == STRATEGY_SONIFICATION) { |
| 1290 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
| 1291 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1292 | |
| 1293 | if (waitMs > muteWaitMs) { |
| 1294 | *delayMs = waitMs - muteWaitMs; |
| 1295 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1296 | |
| 1297 | // FIXME: A device change (muteWaitMs > 0) likely introduces a volume change. |
| 1298 | // A volume change enacted by APM with 0 delay is not synchronous, as it goes |
| 1299 | // via AudioCommandThread to AudioFlinger. Hence it is possible that the volume |
| 1300 | // change occurs after the MixerThread starts and causes a stream volume |
| 1301 | // glitch. |
| 1302 | // |
| 1303 | // We do not introduce additional delay here. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1304 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1305 | |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 1306 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
| 1307 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
| 1308 | setStrategyMute(STRATEGY_SONIFICATION, true, outputDesc); |
| 1309 | } |
| 1310 | |
Tomoharu Kasahara | a81f098 | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 1311 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
| 1312 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
| 1313 | setStrategyMute(STRATEGY_SONIFICATION, true, outputDesc); |
| 1314 | } |
| 1315 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1316 | return NO_ERROR; |
| 1317 | } |
| 1318 | |
| 1319 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1320 | status_t AudioPolicyManager::stopOutput(audio_io_handle_t output, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1321 | audio_stream_type_t stream, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1322 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1323 | { |
| 1324 | ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session); |
| 1325 | ssize_t index = mOutputs.indexOfKey(output); |
| 1326 | if (index < 0) { |
| 1327 | ALOGW("stopOutput() unknown output %d", output); |
| 1328 | return BAD_VALUE; |
| 1329 | } |
| 1330 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1331 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1332 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1333 | if (outputDesc->mRefCount[stream] == 1) { |
| 1334 | // Automatically disable the remote submix input when output is stopped on a |
| 1335 | // re routing mix of type MIX_TYPE_RECORDERS |
| 1336 | if (audio_is_remote_submix_device(outputDesc->mDevice) && |
| 1337 | outputDesc->mPolicyMix != NULL && |
| 1338 | outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1339 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1340 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1341 | outputDesc->mPolicyMix->mDeviceAddress, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1342 | "remote-submix"); |
| 1343 | } |
| 1344 | } |
| 1345 | |
| 1346 | // Routing? |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1347 | bool forceDeviceUpdate = false; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1348 | if (outputDesc->mRefCount[stream] > 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1349 | int activityCount = mOutputRoutes.decRouteActivity(session); |
| 1350 | forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0)); |
| 1351 | |
| 1352 | if (forceDeviceUpdate) { |
| 1353 | checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE); |
| 1354 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1355 | } |
| 1356 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1357 | status_t status = stopSource(outputDesc, stream, forceDeviceUpdate); |
| 1358 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1359 | if (status == NO_ERROR ) { |
| 1360 | outputDesc->stop(); |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1361 | } |
| 1362 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1363 | } |
| 1364 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 1365 | status_t AudioPolicyManager::stopSource(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1366 | audio_stream_type_t stream, |
| 1367 | bool forceDeviceUpdate) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1368 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1369 | // always handle stream stop, check which stream type is stopping |
| 1370 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); |
| 1371 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1372 | if (outputDesc->mRefCount[stream] > 0) { |
| 1373 | // decrement usage count of this stream on the output |
| 1374 | outputDesc->changeRefCount(stream, -1); |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1375 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1376 | // store time at which the stream was stopped - see isStreamActive() |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1377 | if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1378 | outputDesc->mStopTime[stream] = systemTime(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1379 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1380 | // delay the device switch by twice the latency because stopOutput() is executed when |
| 1381 | // the track stop() command is received and at that time the audio track buffer can |
| 1382 | // still contain data that needs to be drained. The latency only covers the audio HAL |
| 1383 | // and kernel buffers. Also the latency does not always include additional delay in the |
| 1384 | // audio path (audio DSP, CODEC ...) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1385 | setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1386 | |
| 1387 | // force restoring the device selection on other active outputs if it differs from the |
| 1388 | // one being selected for this output |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1389 | uint32_t delayMs = outputDesc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1390 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1391 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1392 | if (desc != outputDesc && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1393 | desc->isActive() && |
| 1394 | outputDesc->sharesHwModuleWith(desc) && |
| 1395 | (newDevice != desc->device())) { |
Haynes Mathew George | 11c499a | 2016-08-26 12:08:25 -0700 | [diff] [blame] | 1396 | audio_devices_t newDevice2 = getNewOutputDevice(desc, false /*fromCache*/); |
| 1397 | bool force = desc->device() != newDevice2; |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 1398 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1399 | setOutputDevice(desc, |
Haynes Mathew George | 11c499a | 2016-08-26 12:08:25 -0700 | [diff] [blame] | 1400 | newDevice2, |
| 1401 | force, |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1402 | delayMs); |
| 1403 | // re-apply device specific volume if not done by setOutputDevice() |
| 1404 | if (!force) { |
| 1405 | applyStreamVolumes(desc, newDevice2, delayMs); |
| 1406 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1407 | } |
| 1408 | } |
| 1409 | // update the outputs if stopping one with a stream that can affect notification routing |
| 1410 | handleNotificationRoutingForStream(stream); |
| 1411 | } |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 1412 | |
| 1413 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
| 1414 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
| 1415 | setStrategyMute(STRATEGY_SONIFICATION, false, outputDesc); |
| 1416 | } |
| 1417 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1418 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1419 | selectOutputForMusicEffects(); |
| 1420 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1421 | return NO_ERROR; |
| 1422 | } else { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1423 | ALOGW("stopOutput() refcount is already 0"); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1424 | return INVALID_OPERATION; |
| 1425 | } |
| 1426 | } |
| 1427 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1428 | void AudioPolicyManager::releaseOutput(audio_io_handle_t output, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1429 | audio_stream_type_t stream __unused, |
| 1430 | audio_session_t session __unused) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1431 | { |
| 1432 | ALOGV("releaseOutput() %d", output); |
| 1433 | ssize_t index = mOutputs.indexOfKey(output); |
| 1434 | if (index < 0) { |
| 1435 | ALOGW("releaseOutput() releasing unknown output %d", output); |
| 1436 | return; |
| 1437 | } |
| 1438 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1439 | // Routing |
| 1440 | mOutputRoutes.removeRoute(session); |
| 1441 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1442 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1443 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1444 | if (desc->mDirectOpenCount <= 0) { |
| 1445 | ALOGW("releaseOutput() invalid open count %d for output %d", |
| 1446 | desc->mDirectOpenCount, output); |
| 1447 | return; |
| 1448 | } |
| 1449 | if (--desc->mDirectOpenCount == 0) { |
| 1450 | closeOutput(output); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1451 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1452 | } |
| 1453 | } |
| 1454 | } |
| 1455 | |
| 1456 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1457 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, |
| 1458 | audio_io_handle_t *input, |
| 1459 | audio_session_t session, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1460 | uid_t uid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1461 | const audio_config_base_t *config, |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1462 | audio_input_flags_t flags, |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1463 | audio_port_handle_t *selectedDeviceId, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1464 | input_type_t *inputType, |
| 1465 | audio_port_handle_t *portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1466 | { |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1467 | ALOGV("getInputForAttr() source %d, sampling rate %d, format %#x, channel mask %#x," |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1468 | "session %d, flags %#x", |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1469 | attr->source, config->sample_rate, config->format, config->channel_mask, session, flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1470 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1471 | status_t status = NO_ERROR; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1472 | // handle legacy remote submix case where the address was not always specified |
| 1473 | String8 address = String8(""); |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1474 | audio_source_t halInputSource; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1475 | audio_source_t inputSource = attr->source; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1476 | AudioMix *policyMix = NULL; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1477 | DeviceVector inputDevices; |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1478 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1479 | if (inputSource == AUDIO_SOURCE_DEFAULT) { |
| 1480 | inputSource = AUDIO_SOURCE_MIC; |
| 1481 | } |
| 1482 | |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1483 | // Explicit routing? |
| 1484 | sp<DeviceDescriptor> deviceDesc; |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1485 | if (*selectedDeviceId != AUDIO_PORT_HANDLE_NONE) { |
jiabin | c0c831a | 2018-01-29 17:55:15 -0800 | [diff] [blame] | 1486 | deviceDesc = mAvailableInputDevices.getDeviceFromId(*selectedDeviceId); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1487 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1488 | mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1489 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1490 | // special case for mmap capture: if an input IO handle is specified, we reuse this input if |
| 1491 | // possible |
| 1492 | if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) == AUDIO_INPUT_FLAG_MMAP_NOIRQ && |
| 1493 | *input != AUDIO_IO_HANDLE_NONE) { |
| 1494 | ssize_t index = mInputs.indexOfKey(*input); |
| 1495 | if (index < 0) { |
| 1496 | ALOGW("getInputForAttr() unknown MMAP input %d", *input); |
| 1497 | status = BAD_VALUE; |
| 1498 | goto error; |
| 1499 | } |
| 1500 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
| 1501 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
| 1502 | if (audioSession == 0) { |
| 1503 | ALOGW("getInputForAttr() unknown session %d on input %d", session, *input); |
| 1504 | status = BAD_VALUE; |
| 1505 | goto error; |
| 1506 | } |
| 1507 | // For MMAP mode, the first call to getInputForAttr() is made on behalf of audioflinger. |
| 1508 | // 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] | 1509 | // corresponds to a new client and is only permitted from the same UID. |
| 1510 | // 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] | 1511 | if (audioSession->openCount() == 1) { |
| 1512 | audioSession->setUid(uid); |
| 1513 | } else if (audioSession->uid() != uid) { |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 1514 | if (!audioSession->isSilenced()) { |
| 1515 | ALOGW("getInputForAttr() bad uid %d for session %d uid %d", |
| 1516 | uid, session, audioSession->uid()); |
| 1517 | status = INVALID_OPERATION; |
| 1518 | goto error; |
| 1519 | } |
| 1520 | audioSession->setUid(uid); |
| 1521 | audioSession->setSilenced(false); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1522 | } |
| 1523 | audioSession->changeOpenCount(1); |
| 1524 | *inputType = API_INPUT_LEGACY; |
| 1525 | if (*portId == AUDIO_PORT_HANDLE_NONE) { |
| 1526 | *portId = AudioPort::getNextUniqueId(); |
| 1527 | } |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 1528 | inputDevices = mAvailableInputDevices.getDevicesFromTypeMask(inputDesc->mDevice); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1529 | *selectedDeviceId = inputDevices.size() > 0 ? inputDevices.itemAt(0)->getId() |
| 1530 | : AUDIO_PORT_HANDLE_NONE; |
| 1531 | ALOGI("%s reusing MMAP input %d for session %d", __FUNCTION__, *input, session); |
| 1532 | |
| 1533 | return NO_ERROR; |
| 1534 | } |
| 1535 | |
| 1536 | *input = AUDIO_IO_HANDLE_NONE; |
| 1537 | *inputType = API_INPUT_INVALID; |
| 1538 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1539 | halInputSource = inputSource; |
| 1540 | |
| 1541 | // TODO: check for existing client for this port ID |
| 1542 | if (*portId == AUDIO_PORT_HANDLE_NONE) { |
| 1543 | *portId = AudioPort::getNextUniqueId(); |
| 1544 | } |
| 1545 | |
| 1546 | audio_devices_t device; |
| 1547 | |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1548 | if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX && |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1549 | strncmp(attr->tags, "addr=", strlen("addr=")) == 0) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1550 | status = mPolicyMixes.getInputMixForAttr(*attr, &policyMix); |
| 1551 | if (status != NO_ERROR) { |
| 1552 | goto error; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 1553 | } |
| 1554 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1555 | device = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 1556 | address = String8(attr->tags + strlen("addr=")); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1557 | } else { |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1558 | device = getDeviceAndMixForInputSource(inputSource, &policyMix); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1559 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1560 | ALOGW("getInputForAttr() could not find device for source %d", inputSource); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1561 | status = BAD_VALUE; |
| 1562 | goto error; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1563 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1564 | if (policyMix != NULL) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1565 | address = policyMix->mDeviceAddress; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1566 | if (policyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1567 | // there is an external policy, but this input is attached to a mix of recorders, |
| 1568 | // meaning it receives audio injected into the framework, so the recorder doesn't |
| 1569 | // know about it and is therefore considered "legacy" |
| 1570 | *inputType = API_INPUT_LEGACY; |
| 1571 | } else { |
| 1572 | // recording a mix of players defined by an external policy, we're rerouting for |
| 1573 | // an external policy |
| 1574 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
| 1575 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1576 | } else if (audio_is_remote_submix_device(device)) { |
| 1577 | address = String8("0"); |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1578 | *inputType = API_INPUT_MIX_CAPTURE; |
Eric Laurent | 82db269 | 2015-08-07 13:59:42 -0700 | [diff] [blame] | 1579 | } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) { |
| 1580 | *inputType = API_INPUT_TELEPHONY_RX; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1581 | } else { |
| 1582 | *inputType = API_INPUT_LEGACY; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1583 | } |
Ravi Kumar Alamanda | b367f5b | 2015-08-25 08:21:37 -0700 | [diff] [blame] | 1584 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1585 | } |
| 1586 | |
| 1587 | *input = getInputForDevice(device, address, session, uid, inputSource, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1588 | config, flags, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1589 | policyMix); |
| 1590 | if (*input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1591 | status = INVALID_OPERATION; |
| 1592 | goto error; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1593 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1594 | |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 1595 | inputDevices = mAvailableInputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1596 | *selectedDeviceId = inputDevices.size() > 0 ? inputDevices.itemAt(0)->getId() |
| 1597 | : AUDIO_PORT_HANDLE_NONE; |
| 1598 | |
| 1599 | ALOGV("getInputForAttr() returns input %d type %d selectedDeviceId %d", |
| 1600 | *input, *inputType, *selectedDeviceId); |
| 1601 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1602 | return NO_ERROR; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1603 | |
| 1604 | error: |
| 1605 | mInputRoutes.removeRoute(session); |
| 1606 | return status; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1607 | } |
| 1608 | |
| 1609 | |
| 1610 | audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device, |
| 1611 | String8 address, |
| 1612 | audio_session_t session, |
| 1613 | uid_t uid, |
| 1614 | audio_source_t inputSource, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1615 | const audio_config_base_t *config, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1616 | audio_input_flags_t flags, |
| 1617 | AudioMix *policyMix) |
| 1618 | { |
| 1619 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 1620 | audio_source_t halInputSource = inputSource; |
| 1621 | bool isSoundTrigger = false; |
| 1622 | |
| 1623 | if (inputSource == AUDIO_SOURCE_HOTWORD) { |
| 1624 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); |
| 1625 | if (index >= 0) { |
| 1626 | input = mSoundTriggerSessions.valueFor(session); |
| 1627 | isSoundTrigger = true; |
| 1628 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); |
| 1629 | ALOGV("SoundTrigger capture on session %d input %d", session, input); |
| 1630 | } else { |
| 1631 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1632 | } |
Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame] | 1633 | } else if (inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION && |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1634 | audio_is_linear_pcm(config->format)) { |
Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame] | 1635 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_VOIP_TX); |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1636 | } |
| 1637 | |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1638 | // find a compatible input profile (not necessarily identical in parameters) |
| 1639 | sp<IOProfile> profile; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1640 | // sampling rate and flags may be updated by getInputProfile |
| 1641 | uint32_t profileSamplingRate = (config->sample_rate == 0) ? |
| 1642 | SAMPLE_RATE_HZ_DEFAULT : config->sample_rate; |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 1643 | audio_format_t profileFormat; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1644 | audio_channel_mask_t profileChannelMask = config->channel_mask; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1645 | audio_input_flags_t profileFlags = flags; |
| 1646 | for (;;) { |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 1647 | 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] | 1648 | profile = getInputProfile(device, address, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1649 | profileSamplingRate, profileFormat, profileChannelMask, |
| 1650 | profileFlags); |
| 1651 | if (profile != 0) { |
| 1652 | break; // success |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1653 | } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) { |
| 1654 | profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1655 | } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) { |
| 1656 | profileFlags = AUDIO_INPUT_FLAG_NONE; // retry |
| 1657 | } else { // fail |
Glenn Kasten | faa10a6 | 2017-05-25 15:52:05 -0700 | [diff] [blame] | 1658 | ALOGW("getInputForDevice() could not find profile for device 0x%X, " |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1659 | "sampling rate %u, format %#x, channel mask 0x%X, flags %#x", |
| 1660 | device, config->sample_rate, config->format, config->channel_mask, flags); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1661 | return input; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1662 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1663 | } |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1664 | // Pick input sampling rate if not specified by client |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1665 | uint32_t samplingRate = config->sample_rate; |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1666 | if (samplingRate == 0) { |
| 1667 | samplingRate = profileSamplingRate; |
| 1668 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1669 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1670 | if (profile->getModuleHandle() == 0) { |
| 1671 | ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName()); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1672 | return input; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1673 | } |
| 1674 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1675 | sp<AudioSession> audioSession = new AudioSession(session, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1676 | inputSource, |
| 1677 | config->format, |
| 1678 | samplingRate, |
| 1679 | config->channel_mask, |
| 1680 | flags, |
| 1681 | uid, |
Eric Laurent | a18dced | 2018-07-20 15:14:54 -0700 | [diff] [blame^] | 1682 | isSoundTrigger); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1683 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1684 | // FIXME: disable concurrent capture until UI is ready |
| 1685 | #if 0 |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1686 | // reuse an open input if possible |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1687 | sp<AudioInputDescriptor> reusedInputDesc; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1688 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 1689 | sp<AudioInputDescriptor> desc = mInputs.valueAt(i); |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1690 | // reuse input if: |
| 1691 | // - it shares the same profile |
| 1692 | // AND |
| 1693 | // - it is not a reroute submix input |
| 1694 | // AND |
| 1695 | // - it is: not used for sound trigger |
| 1696 | // OR |
| 1697 | // used for sound trigger and all clients use the same session ID |
| 1698 | // |
| 1699 | if ((profile == desc->mProfile) && |
| 1700 | (isSoundTrigger == desc->isSoundTrigger()) && |
| 1701 | !is_virtual_input_device(device)) { |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1702 | |
| 1703 | sp<AudioSession> as = desc->getAudioSession(session); |
| 1704 | if (as != 0) { |
| 1705 | // do not allow unmatching properties on same session |
| 1706 | if (as->matches(audioSession)) { |
| 1707 | as->changeOpenCount(1); |
| 1708 | } else { |
| 1709 | ALOGW("getInputForDevice() record with different attributes" |
| 1710 | " exists for session %d", session); |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1711 | continue; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1712 | } |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1713 | } else if (isSoundTrigger) { |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1714 | continue; |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1715 | } |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1716 | |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1717 | // Reuse the already opened input stream on this profile if: |
| 1718 | // - the new capture source is background OR |
| 1719 | // - the path requested configurations match OR |
| 1720 | // - the new source priority is less than the highest source priority on this input |
| 1721 | // If the input stream cannot be reused, close it before opening a new stream |
| 1722 | // on the same profile for the new client so that the requested path configuration |
| 1723 | // can be selected. |
| 1724 | if (!isConcurrentSource(inputSource) && |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1725 | ((desc->mSamplingRate != samplingRate || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1726 | desc->mChannelMask != config->channel_mask || |
| 1727 | !audio_formats_match(desc->mFormat, config->format)) && |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1728 | (source_priority(desc->getHighestPrioritySource(false /*activeOnly*/)) < |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1729 | source_priority(inputSource)))) { |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1730 | reusedInputDesc = desc; |
| 1731 | continue; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1732 | } else { |
| 1733 | desc->addAudioSession(session, audioSession); |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1734 | ALOGV("%s: reusing input %d", __FUNCTION__, mInputs.keyAt(i)); |
| 1735 | return mInputs.keyAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1736 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1737 | } |
| 1738 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1739 | |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1740 | if (reusedInputDesc != 0) { |
| 1741 | AudioSessionCollection sessions = reusedInputDesc->getAudioSessions(false /*activeOnly*/); |
| 1742 | for (size_t j = 0; j < sessions.size(); j++) { |
| 1743 | audio_session_t currentSession = sessions.keyAt(j); |
| 1744 | stopInput(reusedInputDesc->mIoHandle, currentSession); |
| 1745 | releaseInput(reusedInputDesc->mIoHandle, currentSession); |
| 1746 | } |
| 1747 | } |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1748 | #endif |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1749 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1750 | if (!profile->canOpenNewIo()) { |
| 1751 | return AUDIO_IO_HANDLE_NONE; |
| 1752 | } |
| 1753 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1754 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1755 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1756 | audio_config_t lConfig = AUDIO_CONFIG_INITIALIZER; |
| 1757 | lConfig.sample_rate = profileSamplingRate; |
| 1758 | lConfig.channel_mask = profileChannelMask; |
| 1759 | lConfig.format = profileFormat; |
Eric Laurent | e301410 | 2017-05-03 11:15:43 -0700 | [diff] [blame] | 1760 | |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 1761 | if (address == "") { |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 1762 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 1763 | // the inputs vector must be of size >= 1, but we don't want to crash here |
| 1764 | address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress : String8(""); |
| 1765 | } |
| 1766 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1767 | status_t status = inputDesc->open(&lConfig, device, address, |
| 1768 | halInputSource, profileFlags, &input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1769 | |
| 1770 | // only accept input with the exact requested set of parameters |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1771 | if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1772 | (profileSamplingRate != lConfig.sample_rate) || |
| 1773 | !audio_formats_match(profileFormat, lConfig.format) || |
| 1774 | (profileChannelMask != lConfig.channel_mask)) { |
| 1775 | ALOGW("getInputForAttr() failed opening input: sampling rate %d" |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1776 | ", format %#x, channel mask %#x", |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1777 | profileSamplingRate, profileFormat, profileChannelMask); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1778 | if (input != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1779 | inputDesc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1780 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1781 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1782 | } |
| 1783 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1784 | inputDesc->mPolicyMix = policyMix; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1785 | inputDesc->addAudioSession(session, audioSession); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1786 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1787 | addInput(input, inputDesc); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1788 | mpClientInterface->onAudioPortListUpdate(); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1789 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1790 | return input; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1791 | } |
| 1792 | |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1793 | //static |
| 1794 | bool AudioPolicyManager::isConcurrentSource(audio_source_t source) |
| 1795 | { |
| 1796 | return (source == AUDIO_SOURCE_HOTWORD) || |
| 1797 | (source == AUDIO_SOURCE_VOICE_RECOGNITION) || |
| 1798 | (source == AUDIO_SOURCE_FM_TUNER); |
| 1799 | } |
| 1800 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1801 | bool AudioPolicyManager::isConcurentCaptureAllowed(const sp<AudioInputDescriptor>& inputDesc, |
| 1802 | const sp<AudioSession>& audioSession) |
| 1803 | { |
| 1804 | // Do not allow capture if an active voice call is using a software patch and |
| 1805 | // the call TX source device is on the same HW module. |
| 1806 | // FIXME: would be better to refine to only inputs whose profile connects to the |
| 1807 | // call TX device but this information is not in the audio patch |
| 1808 | if (mCallTxPatch != 0 && |
| 1809 | inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) { |
| 1810 | return false; |
| 1811 | } |
| 1812 | |
| 1813 | // starting concurrent capture is enabled if: |
| 1814 | // 1) capturing for re-routing |
| 1815 | // 2) capturing for HOTWORD source |
| 1816 | // 3) capturing for FM TUNER source |
| 1817 | // 3) All other active captures are either for re-routing or HOTWORD |
| 1818 | |
| 1819 | if (is_virtual_input_device(inputDesc->mDevice) || |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1820 | isConcurrentSource(audioSession->inputSource())) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1821 | return true; |
| 1822 | } |
| 1823 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1824 | for (const auto& activeInput : mInputs.getActiveInputs()) { |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1825 | if (!isConcurrentSource(activeInput->inputSource(true)) && |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1826 | !is_virtual_input_device(activeInput->mDevice)) { |
| 1827 | return false; |
| 1828 | } |
| 1829 | } |
| 1830 | |
| 1831 | return true; |
| 1832 | } |
| 1833 | |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 1834 | // FIXME: remove when concurrent capture is ready. This is a hack to work around bug b/63083537. |
| 1835 | bool AudioPolicyManager::soundTriggerSupportsConcurrentCapture() { |
| 1836 | if (!mHasComputedSoundTriggerSupportsConcurrentCapture) { |
| 1837 | bool soundTriggerSupportsConcurrentCapture = false; |
| 1838 | unsigned int numModules = 0; |
| 1839 | struct sound_trigger_module_descriptor* nModules = NULL; |
| 1840 | |
| 1841 | status_t status = SoundTrigger::listModules(nModules, &numModules); |
| 1842 | if (status == NO_ERROR && numModules != 0) { |
| 1843 | nModules = (struct sound_trigger_module_descriptor*) calloc( |
| 1844 | numModules, sizeof(struct sound_trigger_module_descriptor)); |
| 1845 | if (nModules == NULL) { |
| 1846 | // We failed to malloc the buffer, so just say no for now, and hope that we have more |
| 1847 | // ram the next time this function is called. |
| 1848 | ALOGE("Failed to allocate buffer for module descriptors"); |
| 1849 | return false; |
| 1850 | } |
| 1851 | |
| 1852 | status = SoundTrigger::listModules(nModules, &numModules); |
| 1853 | if (status == NO_ERROR) { |
| 1854 | soundTriggerSupportsConcurrentCapture = true; |
| 1855 | for (size_t i = 0; i < numModules; ++i) { |
| 1856 | soundTriggerSupportsConcurrentCapture &= |
| 1857 | nModules[i].properties.concurrent_capture; |
| 1858 | } |
| 1859 | } |
| 1860 | free(nModules); |
| 1861 | } |
| 1862 | mSoundTriggerSupportsConcurrentCapture = soundTriggerSupportsConcurrentCapture; |
| 1863 | mHasComputedSoundTriggerSupportsConcurrentCapture = true; |
| 1864 | } |
| 1865 | return mSoundTriggerSupportsConcurrentCapture; |
| 1866 | } |
| 1867 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1868 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1869 | status_t AudioPolicyManager::startInput(audio_io_handle_t input, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1870 | audio_session_t session, |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1871 | bool silenced, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1872 | concurrency_type__mask_t *concurrency) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1873 | { |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1874 | |
| 1875 | ALOGV("AudioPolicyManager::startInput(input:%d, session:%d, silenced:%d, concurrency:%d)", |
| 1876 | input, session, silenced, *concurrency); |
| 1877 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1878 | *concurrency = API_INPUT_CONCURRENCY_NONE; |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1879 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1880 | ssize_t index = mInputs.indexOfKey(input); |
| 1881 | if (index < 0) { |
| 1882 | ALOGW("startInput() unknown input %d", input); |
| 1883 | return BAD_VALUE; |
| 1884 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1885 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1886 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1887 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
| 1888 | if (audioSession == 0) { |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1889 | ALOGW("startInput() unknown session %d on input %d", session, input); |
| 1890 | return BAD_VALUE; |
| 1891 | } |
| 1892 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1893 | // FIXME: disable concurrent capture until UI is ready |
| 1894 | #if 0 |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1895 | if (!isConcurentCaptureAllowed(inputDesc, audioSession)) { |
| 1896 | ALOGW("startInput(%d) failed: other input already started", input); |
| 1897 | return INVALID_OPERATION; |
| 1898 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1899 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1900 | if (isInCall()) { |
| 1901 | *concurrency |= API_INPUT_CONCURRENCY_CALL; |
| 1902 | } |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 1903 | if (mInputs.activeInputsCountOnDevices() != 0) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1904 | *concurrency |= API_INPUT_CONCURRENCY_CAPTURE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1905 | } |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1906 | #else |
| 1907 | if (!is_virtual_input_device(inputDesc->mDevice)) { |
| 1908 | if (mCallTxPatch != 0 && |
| 1909 | inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) { |
| 1910 | ALOGW("startInput(%d) failed: call in progress", input); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 1911 | *concurrency |= API_INPUT_CONCURRENCY_CALL; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1912 | return INVALID_OPERATION; |
| 1913 | } |
| 1914 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1915 | Vector<sp<AudioInputDescriptor>> activeInputs = mInputs.getActiveInputs(); |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1916 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1917 | // If a UID is idle and records silence and another not silenced recording starts |
| 1918 | // from another UID (idle or active) we stop the current idle UID recording in |
| 1919 | // favor of the new one - "There can be only one" TM |
| 1920 | if (!silenced) { |
| 1921 | for (const auto& activeDesc : activeInputs) { |
| 1922 | if ((audioSession->flags() & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0 && |
| 1923 | activeDesc->getId() == inputDesc->getId()) { |
| 1924 | continue; |
| 1925 | } |
| 1926 | |
| 1927 | AudioSessionCollection activeSessions = activeDesc->getAudioSessions( |
| 1928 | true /*activeOnly*/); |
| 1929 | sp<AudioSession> activeSession = activeSessions.valueAt(0); |
| 1930 | if (activeSession->isSilenced()) { |
| 1931 | audio_io_handle_t activeInput = activeDesc->mIoHandle; |
| 1932 | audio_session_t activeSessionId = activeSession->session(); |
| 1933 | stopInput(activeInput, activeSessionId); |
| 1934 | releaseInput(activeInput, activeSessionId); |
| 1935 | ALOGV("startInput(%d) stopping silenced input %d", input, activeInput); |
| 1936 | activeInputs = mInputs.getActiveInputs(); |
| 1937 | } |
| 1938 | } |
| 1939 | } |
| 1940 | |
| 1941 | for (const auto& activeDesc : activeInputs) { |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 1942 | if ((audioSession->flags() & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0 && |
| 1943 | activeDesc->getId() == inputDesc->getId()) { |
| 1944 | continue; |
| 1945 | } |
| 1946 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1947 | audio_source_t activeSource = activeDesc->inputSource(true); |
| 1948 | if (audioSession->inputSource() == AUDIO_SOURCE_HOTWORD) { |
| 1949 | if (activeSource == AUDIO_SOURCE_HOTWORD) { |
| 1950 | if (activeDesc->hasPreemptedSession(session)) { |
| 1951 | ALOGW("startInput(%d) failed for HOTWORD: " |
| 1952 | "other input %d already started for HOTWORD", |
| 1953 | input, activeDesc->mIoHandle); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 1954 | *concurrency |= API_INPUT_CONCURRENCY_HOTWORD; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1955 | return INVALID_OPERATION; |
| 1956 | } |
| 1957 | } else { |
| 1958 | ALOGV("startInput(%d) failed for HOTWORD: other input %d already started", |
| 1959 | input, activeDesc->mIoHandle); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 1960 | *concurrency |= API_INPUT_CONCURRENCY_CAPTURE; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1961 | return INVALID_OPERATION; |
| 1962 | } |
| 1963 | } else { |
| 1964 | if (activeSource != AUDIO_SOURCE_HOTWORD) { |
| 1965 | ALOGW("startInput(%d) failed: other input %d already started", |
| 1966 | input, activeDesc->mIoHandle); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 1967 | *concurrency |= API_INPUT_CONCURRENCY_CAPTURE; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1968 | return INVALID_OPERATION; |
| 1969 | } |
| 1970 | } |
| 1971 | } |
| 1972 | |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 1973 | // We only need to check if the sound trigger session supports concurrent capture if the |
| 1974 | // input is also a sound trigger input. Otherwise, we should preempt any hotword stream |
| 1975 | // that's running. |
| 1976 | const bool allowConcurrentWithSoundTrigger = |
| 1977 | inputDesc->isSoundTrigger() ? soundTriggerSupportsConcurrentCapture() : false; |
| 1978 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1979 | // if capture is allowed, preempt currently active HOTWORD captures |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1980 | for (const auto& activeDesc : activeInputs) { |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 1981 | if (allowConcurrentWithSoundTrigger && activeDesc->isSoundTrigger()) { |
| 1982 | continue; |
| 1983 | } |
| 1984 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1985 | audio_source_t activeSource = activeDesc->inputSource(true); |
| 1986 | if (activeSource == AUDIO_SOURCE_HOTWORD) { |
| 1987 | AudioSessionCollection activeSessions = |
| 1988 | activeDesc->getAudioSessions(true /*activeOnly*/); |
| 1989 | audio_session_t activeSession = activeSessions.keyAt(0); |
| 1990 | audio_io_handle_t activeHandle = activeDesc->mIoHandle; |
| 1991 | SortedVector<audio_session_t> sessions = activeDesc->getPreemptedSessions(); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 1992 | *concurrency |= API_INPUT_CONCURRENCY_PREEMPT; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1993 | sessions.add(activeSession); |
| 1994 | inputDesc->setPreemptedSessions(sessions); |
| 1995 | stopInput(activeHandle, activeSession); |
| 1996 | releaseInput(activeHandle, activeSession); |
| 1997 | ALOGV("startInput(%d) for HOTWORD preempting HOTWORD input %d", |
| 1998 | input, activeDesc->mIoHandle); |
| 1999 | } |
| 2000 | } |
| 2001 | } |
| 2002 | #endif |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2003 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 2004 | // Make sure we start with the correct silence state |
| 2005 | audioSession->setSilenced(silenced); |
| 2006 | |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 2007 | // increment activity count before calling getNewInputDevice() below as only active sessions |
| 2008 | // are considered for device selection |
Eric Laurent | a18dced | 2018-07-20 15:14:54 -0700 | [diff] [blame^] | 2009 | inputDesc->changeRefCount(session, 1); |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 2010 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2011 | // Routing? |
| 2012 | mInputRoutes.incRouteActivity(session); |
| 2013 | |
Eric Laurent | 2157f5b | 2018-04-25 18:33:02 -0700 | [diff] [blame] | 2014 | if (audioSession->activeCount() == 1 || mInputRoutes.getAndClearRouteChanged(session)) { |
Eric Laurent | 271a93e | 2016-09-29 14:47:06 -0700 | [diff] [blame] | 2015 | // indicate active capture to sound trigger service if starting capture from a mic on |
| 2016 | // primary HW module |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 2017 | audio_devices_t device = getNewInputDevice(inputDesc); |
Eric Laurent | 271a93e | 2016-09-29 14:47:06 -0700 | [diff] [blame] | 2018 | setInputDevice(input, device, true /* force */); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2019 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2020 | status_t status = inputDesc->start(); |
| 2021 | if (status != NO_ERROR) { |
| 2022 | mInputRoutes.decRouteActivity(session); |
Eric Laurent | a18dced | 2018-07-20 15:14:54 -0700 | [diff] [blame^] | 2023 | inputDesc->changeRefCount(session, -1); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2024 | return status; |
| 2025 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2026 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2027 | if (inputDesc->getAudioSessionCount(true/*activeOnly*/) == 1) { |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2028 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 2029 | if ((inputDesc->mPolicyMix != NULL) |
| 2030 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 2031 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
| 2032 | MIX_STATE_MIXING); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2033 | } |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2034 | |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 2035 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); |
| 2036 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
Eric Laurent | 05b345e | 2016-11-18 12:36:27 -0800 | [diff] [blame] | 2037 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) { |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2038 | SoundTrigger::setCaptureState(true); |
| 2039 | } |
| 2040 | |
| 2041 | // automatically enable the remote submix output when input is started if not |
| 2042 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 2043 | // For remote submix (a virtual device), we open only one input per capture request. |
| 2044 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 2045 | String8 address = String8(""); |
| 2046 | if (inputDesc->mPolicyMix == NULL) { |
| 2047 | address = String8("0"); |
| 2048 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 2049 | address = inputDesc->mPolicyMix->mDeviceAddress; |
| 2050 | } |
| 2051 | if (address != "") { |
| 2052 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2053 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2054 | address, "remote-submix"); |
| 2055 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2056 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 2057 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2058 | } |
| 2059 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2060 | ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2061 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2062 | return NO_ERROR; |
| 2063 | } |
| 2064 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2065 | status_t AudioPolicyManager::stopInput(audio_io_handle_t input, |
| 2066 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2067 | { |
| 2068 | ALOGV("stopInput() input %d", input); |
| 2069 | ssize_t index = mInputs.indexOfKey(input); |
| 2070 | if (index < 0) { |
| 2071 | ALOGW("stopInput() unknown input %d", input); |
| 2072 | return BAD_VALUE; |
| 2073 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2074 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2075 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2076 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2077 | if (index < 0) { |
| 2078 | ALOGW("stopInput() unknown session %d on input %d", session, input); |
| 2079 | return BAD_VALUE; |
| 2080 | } |
| 2081 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2082 | if (audioSession->activeCount() == 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2083 | ALOGW("stopInput() input %d already stopped", input); |
| 2084 | return INVALID_OPERATION; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2085 | } |
| 2086 | |
Eric Laurent | a18dced | 2018-07-20 15:14:54 -0700 | [diff] [blame^] | 2087 | inputDesc->changeRefCount(session, -1); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2088 | |
| 2089 | // Routing? |
| 2090 | mInputRoutes.decRouteActivity(session); |
| 2091 | |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2092 | if (audioSession->activeCount() == 0) { |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2093 | inputDesc->stop(); |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2094 | if (inputDesc->isActive()) { |
| 2095 | setInputDevice(input, getNewInputDevice(inputDesc), false /* force */); |
| 2096 | } else { |
| 2097 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 2098 | if ((inputDesc->mPolicyMix != NULL) |
| 2099 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 2100 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
| 2101 | MIX_STATE_IDLE); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2102 | } |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2103 | |
| 2104 | // automatically disable the remote submix output when input is stopped if not |
| 2105 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 2106 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 2107 | String8 address = String8(""); |
| 2108 | if (inputDesc->mPolicyMix == NULL) { |
| 2109 | address = String8("0"); |
| 2110 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 2111 | address = inputDesc->mPolicyMix->mDeviceAddress; |
| 2112 | } |
| 2113 | if (address != "") { |
| 2114 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2115 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2116 | address, "remote-submix"); |
| 2117 | } |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2118 | } |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2119 | |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 2120 | audio_devices_t device = inputDesc->mDevice; |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 2121 | resetInputDevice(input); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2122 | |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 2123 | // indicate inactive capture to sound trigger service if stopping capture from a mic on |
| 2124 | // primary HW module |
| 2125 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); |
| 2126 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
| 2127 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 2128 | SoundTrigger::setCaptureState(false); |
| 2129 | } |
| 2130 | inputDesc->clearPreemptedSessions(); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2131 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2132 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2133 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2134 | } |
| 2135 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2136 | void AudioPolicyManager::releaseInput(audio_io_handle_t input, |
| 2137 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2138 | { |
| 2139 | ALOGV("releaseInput() %d", input); |
| 2140 | ssize_t index = mInputs.indexOfKey(input); |
| 2141 | if (index < 0) { |
| 2142 | ALOGW("releaseInput() releasing unknown input %d", input); |
| 2143 | return; |
| 2144 | } |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2145 | |
| 2146 | // Routing |
| 2147 | mInputRoutes.removeRoute(session); |
| 2148 | |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2149 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
| 2150 | ALOG_ASSERT(inputDesc != 0); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2151 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2152 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
vivek mehta | 587b8df | 2017-01-31 14:58:44 -0800 | [diff] [blame] | 2153 | if (audioSession == 0) { |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2154 | ALOGW("releaseInput() unknown session %d on input %d", session, input); |
| 2155 | return; |
| 2156 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2157 | |
| 2158 | if (audioSession->openCount() == 0) { |
| 2159 | ALOGW("releaseInput() invalid open count %d on session %d", |
| 2160 | audioSession->openCount(), session); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2161 | return; |
| 2162 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2163 | |
| 2164 | if (audioSession->changeOpenCount(-1) == 0) { |
| 2165 | inputDesc->removeAudioSession(session); |
| 2166 | } |
| 2167 | |
Jean-Michel Trivi | 65bfe91 | 2015-12-04 15:56:47 -0800 | [diff] [blame] | 2168 | if (inputDesc->getOpenRefCount() > 0) { |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2169 | ALOGV("releaseInput() exit > 0"); |
| 2170 | return; |
| 2171 | } |
| 2172 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2173 | closeInput(input); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2174 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2175 | ALOGV("releaseInput() exit"); |
| 2176 | } |
| 2177 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2178 | void AudioPolicyManager::closeAllInputs() { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2179 | bool patchRemoved = false; |
| 2180 | |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 2181 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2182 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index); |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 2183 | ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2184 | if (patch_index >= 0) { |
| 2185 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 2186 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2187 | mAudioPatches.removeItemsAt(patch_index); |
| 2188 | patchRemoved = true; |
| 2189 | } |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2190 | inputDesc->close(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2191 | } |
jiabin | 829a00b | 2018-04-04 16:28:32 -0700 | [diff] [blame] | 2192 | mInputRoutes.clear(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2193 | mInputs.clear(); |
Chris Thornton | 52785f3 | 2016-02-09 17:28:49 -0800 | [diff] [blame] | 2194 | SoundTrigger::setCaptureState(false); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2195 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2196 | |
| 2197 | if (patchRemoved) { |
| 2198 | mpClientInterface->onAudioPatchListUpdate(); |
| 2199 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2200 | } |
| 2201 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2202 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2203 | int indexMin, |
| 2204 | int indexMax) |
| 2205 | { |
| 2206 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2207 | mVolumeCurves->initStreamVolume(stream, indexMin, indexMax); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2208 | |
| 2209 | // initialize other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2210 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2211 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2212 | continue; |
| 2213 | } |
| 2214 | mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2215 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2216 | } |
| 2217 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2218 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2219 | int index, |
| 2220 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2221 | { |
| 2222 | |
Nadav Bar | 7c605f6 | 2017-12-25 00:01:52 +0200 | [diff] [blame] | 2223 | // VOICE_CALL stream has minVolumeIndex > 0 but can be muted directly by an |
| 2224 | // app that has MODIFY_PHONE_STATE permission. |
| 2225 | if (((index < mVolumeCurves->getVolumeIndexMin(stream)) && |
| 2226 | !(stream == AUDIO_STREAM_VOICE_CALL && index == 0)) || |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2227 | (index > mVolumeCurves->getVolumeIndexMax(stream))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2228 | return BAD_VALUE; |
| 2229 | } |
| 2230 | if (!audio_is_output_device(device)) { |
| 2231 | return BAD_VALUE; |
| 2232 | } |
| 2233 | |
| 2234 | // Force max volume if stream cannot be muted |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2235 | if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2236 | |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2237 | ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2238 | stream, device, index); |
| 2239 | |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2240 | // update other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2241 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2242 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2243 | continue; |
| 2244 | } |
| 2245 | mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index); |
| 2246 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2247 | |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2248 | // update volume on all outputs and streams matching the following: |
| 2249 | // - The requested stream (or a stream matching for volume control) is active on the output |
| 2250 | // - The device (or devices) selected by the strategy corresponding to this stream includes |
| 2251 | // the requested device |
| 2252 | // - For non default requested device, currently selected device on the output is either the |
| 2253 | // requested device or one of the devices selected by the strategy |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2254 | // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if |
| 2255 | // no specific device volume value exists for currently selected device. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2256 | status_t status = NO_ERROR; |
| 2257 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2258 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 2259 | audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device()); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2260 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 2261 | if (!(streamsMatchForvolume(stream, (audio_stream_type_t)curStream))) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2262 | continue; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2263 | } |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 2264 | if (!(desc->isStreamActive((audio_stream_type_t)curStream) || isInCall())) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2265 | continue; |
| 2266 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2267 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Jean-Michel Trivi | b7fdce6 | 2017-06-27 19:38:42 -0700 | [diff] [blame] | 2268 | audio_devices_t curStreamDevice = Volume::getDeviceForVolume(getDeviceForStrategy( |
| 2269 | curStrategy, false /*fromCache*/)); |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2270 | if ((device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) && |
| 2271 | ((curStreamDevice & device) == 0)) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2272 | continue; |
| 2273 | } |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2274 | bool applyVolume; |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2275 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2276 | curStreamDevice |= device; |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2277 | applyVolume = (curDevice & curStreamDevice) != 0; |
| 2278 | } else { |
| 2279 | applyVolume = !mVolumeCurves->hasVolumeIndexForDevice( |
Jean-Michel Trivi | b7fdce6 | 2017-06-27 19:38:42 -0700 | [diff] [blame] | 2280 | stream, curStreamDevice); |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2281 | } |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 2282 | // rescale index before applying to curStream as ranges may be different for |
| 2283 | // stream and curStream |
| 2284 | int idx = rescaleVolumeIndex(index, stream, (audio_stream_type_t)curStream); |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2285 | if (applyVolume) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2286 | //FIXME: workaround for truncated touch sounds |
| 2287 | // delayed volume change for system stream to be removed when the problem is |
| 2288 | // handled by system UI |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2289 | status_t volStatus = |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 2290 | checkAndSetVolume((audio_stream_type_t)curStream, idx, desc, curDevice, |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2291 | (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2292 | if (volStatus != NO_ERROR) { |
| 2293 | status = volStatus; |
| 2294 | } |
| 2295 | } |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2296 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2297 | } |
| 2298 | return status; |
| 2299 | } |
| 2300 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2301 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2302 | int *index, |
| 2303 | audio_devices_t device) |
| 2304 | { |
| 2305 | if (index == NULL) { |
| 2306 | return BAD_VALUE; |
| 2307 | } |
| 2308 | if (!audio_is_output_device(device)) { |
| 2309 | return BAD_VALUE; |
| 2310 | } |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2311 | // 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] | 2312 | // the strategy the stream belongs to. |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2313 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2314 | device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); |
| 2315 | } |
François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 2316 | device = Volume::getDeviceForVolume(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2317 | |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2318 | *index = mVolumeCurves->getVolumeIndex(stream, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2319 | ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index); |
| 2320 | return NO_ERROR; |
| 2321 | } |
| 2322 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2323 | audio_io_handle_t AudioPolicyManager::selectOutputForMusicEffects() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2324 | { |
| 2325 | // select one output among several suitable for global effects. |
| 2326 | // The priority is as follows: |
| 2327 | // 1: An offloaded output. If the effect ends up not being offloadable, |
| 2328 | // AudioFlinger will invalidate the track and the offloaded output |
| 2329 | // will be closed causing the effect to be moved to a PCM output. |
| 2330 | // 2: A deep buffer output |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2331 | // 3: The primary output |
| 2332 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2333 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2334 | routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2335 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2336 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2337 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2338 | if (outputs.size() == 0) { |
| 2339 | return AUDIO_IO_HANDLE_NONE; |
| 2340 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2341 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2342 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 2343 | bool activeOnly = true; |
| 2344 | |
| 2345 | while (output == AUDIO_IO_HANDLE_NONE) { |
| 2346 | audio_io_handle_t outputOffloaded = AUDIO_IO_HANDLE_NONE; |
| 2347 | audio_io_handle_t outputDeepBuffer = AUDIO_IO_HANDLE_NONE; |
| 2348 | audio_io_handle_t outputPrimary = AUDIO_IO_HANDLE_NONE; |
| 2349 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2350 | for (audio_io_handle_t output : outputs) { |
| 2351 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2352 | if (activeOnly && !desc->isStreamActive(AUDIO_STREAM_MUSIC)) { |
| 2353 | continue; |
| 2354 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2355 | ALOGV("selectOutputForMusicEffects activeOnly %d output %d flags 0x%08x", |
| 2356 | activeOnly, output, desc->mFlags); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2357 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2358 | outputOffloaded = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2359 | } |
| 2360 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2361 | outputDeepBuffer = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2362 | } |
| 2363 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2364 | outputPrimary = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2365 | } |
| 2366 | } |
| 2367 | if (outputOffloaded != AUDIO_IO_HANDLE_NONE) { |
| 2368 | output = outputOffloaded; |
| 2369 | } else if (outputDeepBuffer != AUDIO_IO_HANDLE_NONE) { |
| 2370 | output = outputDeepBuffer; |
| 2371 | } else if (outputPrimary != AUDIO_IO_HANDLE_NONE) { |
| 2372 | output = outputPrimary; |
| 2373 | } else { |
| 2374 | output = outputs[0]; |
| 2375 | } |
| 2376 | activeOnly = false; |
| 2377 | } |
| 2378 | |
| 2379 | if (output != mMusicEffectOutput) { |
| 2380 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output); |
| 2381 | mMusicEffectOutput = output; |
| 2382 | } |
| 2383 | |
| 2384 | ALOGV("selectOutputForMusicEffects selected output %d", output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2385 | return output; |
| 2386 | } |
| 2387 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2388 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc __unused) |
| 2389 | { |
| 2390 | return selectOutputForMusicEffects(); |
| 2391 | } |
| 2392 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2393 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2394 | audio_io_handle_t io, |
| 2395 | uint32_t strategy, |
| 2396 | int session, |
| 2397 | int id) |
| 2398 | { |
| 2399 | ssize_t index = mOutputs.indexOfKey(io); |
| 2400 | if (index < 0) { |
| 2401 | index = mInputs.indexOfKey(io); |
| 2402 | if (index < 0) { |
| 2403 | ALOGW("registerEffect() unknown io %d", io); |
| 2404 | return INVALID_OPERATION; |
| 2405 | } |
| 2406 | } |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2407 | return mEffects.registerEffect(desc, io, strategy, session, id); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2408 | } |
| 2409 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2410 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2411 | { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2412 | bool active = false; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2413 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) { |
| 2414 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2415 | continue; |
| 2416 | } |
| 2417 | active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs); |
| 2418 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2419 | return active; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2420 | } |
| 2421 | |
| 2422 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2423 | { |
| 2424 | return mOutputs.isStreamActiveRemotely(stream, inPastMs); |
| 2425 | } |
| 2426 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2427 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2428 | { |
| 2429 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2430 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2431 | if (inputDescriptor->isSourceActive(source)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2432 | return true; |
| 2433 | } |
| 2434 | } |
| 2435 | return false; |
| 2436 | } |
| 2437 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2438 | // Register a list of custom mixes with their attributes and format. |
| 2439 | // When a mix is registered, corresponding input and output profiles are |
| 2440 | // added to the remote submix hw module. The profile contains only the |
| 2441 | // parameters (sampling rate, format...) specified by the mix. |
| 2442 | // The corresponding input remote submix device is also connected. |
| 2443 | // |
| 2444 | // When a remote submix device is connected, the address is checked to select the |
| 2445 | // appropriate profile and the corresponding input or output stream is opened. |
| 2446 | // |
| 2447 | // When capture starts, getInputForAttr() will: |
| 2448 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2449 | // - 2 if none found, getDeviceForInputSource() will: |
| 2450 | // - 2.1 look for a mix matching the attributes source |
| 2451 | // - 2.2 if none found, default to device selection by policy rules |
| 2452 | // At this time, the corresponding output remote submix device is also connected |
| 2453 | // and active playback use cases can be transferred to this mix if needed when reconnecting |
| 2454 | // after AudioTracks are invalidated |
| 2455 | // |
| 2456 | // When playback starts, getOutputForAttr() will: |
| 2457 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2458 | // - 2 if none found, look for a mix matching the attributes usage |
| 2459 | // - 3 if none found, default to device and output selection by policy rules. |
| 2460 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 2461 | status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes) |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2462 | { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2463 | ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size()); |
| 2464 | status_t res = NO_ERROR; |
| 2465 | |
| 2466 | sp<HwModule> rSubmixModule; |
| 2467 | // examine each mix's route type |
| 2468 | for (size_t i = 0; i < mixes.size(); i++) { |
| 2469 | // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination |
| 2470 | if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) { |
| 2471 | res = INVALID_OPERATION; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2472 | break; |
| 2473 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2474 | 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] | 2475 | 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] | 2476 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 2477 | rSubmixModule = mHwModules.getModuleFromName( |
| 2478 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 2479 | if (rSubmixModule == 0) { |
| 2480 | ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", |
| 2481 | i); |
| 2482 | res = INVALID_OPERATION; |
| 2483 | break; |
| 2484 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2485 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2486 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2487 | String8 address = mixes[i].mDeviceAddress; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2488 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2489 | if (mPolicyMixes.registerMix(address, mixes[i], 0 /*output desc*/) != NO_ERROR) { |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2490 | ALOGE(" Error registering mix %zu for address %s", i, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2491 | res = INVALID_OPERATION; |
| 2492 | break; |
| 2493 | } |
| 2494 | audio_config_t outputConfig = mixes[i].mFormat; |
| 2495 | audio_config_t inputConfig = mixes[i].mFormat; |
| 2496 | // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in |
| 2497 | // stereo and let audio flinger do the channel conversion if needed. |
| 2498 | outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 2499 | inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
| 2500 | rSubmixModule->addOutputProfile(address, &outputConfig, |
| 2501 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address); |
| 2502 | rSubmixModule->addInputProfile(address, &inputConfig, |
| 2503 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, address); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2504 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2505 | if (mixes[i].mMixType == MIX_TYPE_PLAYERS) { |
| 2506 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2507 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2508 | address.string(), "remote-submix"); |
| 2509 | } else { |
| 2510 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2511 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2512 | address.string(), "remote-submix"); |
| 2513 | } |
| 2514 | } 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] | 2515 | String8 address = mixes[i].mDeviceAddress; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2516 | audio_devices_t device = mixes[i].mDeviceType; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2517 | ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s", |
| 2518 | i, mixes.size(), device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2519 | |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2520 | bool foundOutput = false; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2521 | for (size_t j = 0 ; j < mOutputs.size() ; j++) { |
| 2522 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j); |
| 2523 | sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle()); |
| 2524 | if ((patch != 0) && (patch->mPatch.num_sinks != 0) |
| 2525 | && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE) |
| 2526 | && (patch->mPatch.sinks[0].ext.device.type == device) |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2527 | && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(), |
| 2528 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2529 | if (mPolicyMixes.registerMix(address, mixes[i], desc) != NO_ERROR) { |
| 2530 | res = INVALID_OPERATION; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2531 | } else { |
| 2532 | foundOutput = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2533 | } |
| 2534 | break; |
| 2535 | } |
| 2536 | } |
| 2537 | |
| 2538 | if (res != NO_ERROR) { |
| 2539 | ALOGE(" Error registering mix %zu for device 0x%X addr %s", |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2540 | i, device, address.string()); |
| 2541 | res = INVALID_OPERATION; |
| 2542 | break; |
| 2543 | } else if (!foundOutput) { |
| 2544 | ALOGE(" Output not found for mix %zu for device 0x%X addr %s", |
| 2545 | i, device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2546 | res = INVALID_OPERATION; |
| 2547 | break; |
| 2548 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2549 | } |
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 (res != NO_ERROR) { |
| 2552 | unregisterPolicyMixes(mixes); |
| 2553 | } |
| 2554 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2555 | } |
| 2556 | |
| 2557 | status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes) |
| 2558 | { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2559 | ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2560 | status_t res = NO_ERROR; |
| 2561 | sp<HwModule> rSubmixModule; |
| 2562 | // examine each mix's route type |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2563 | for (const auto& mix : mixes) { |
| 2564 | 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] | 2565 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2566 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 2567 | rSubmixModule = mHwModules.getModuleFromName( |
| 2568 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 2569 | if (rSubmixModule == 0) { |
| 2570 | res = INVALID_OPERATION; |
| 2571 | continue; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2572 | } |
| 2573 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2574 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2575 | String8 address = mix.mDeviceAddress; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2576 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2577 | if (mPolicyMixes.unregisterMix(address) != NO_ERROR) { |
| 2578 | res = INVALID_OPERATION; |
| 2579 | continue; |
| 2580 | } |
| 2581 | |
| 2582 | if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) == |
| 2583 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2584 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2585 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2586 | address.string(), "remote-submix"); |
| 2587 | } |
| 2588 | if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) == |
| 2589 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2590 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2591 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2592 | address.string(), "remote-submix"); |
| 2593 | } |
| 2594 | rSubmixModule->removeOutputProfile(address); |
| 2595 | rSubmixModule->removeInputProfile(address); |
| 2596 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2597 | } if ((mix.mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 2598 | if (mPolicyMixes.unregisterMix(mix.mDeviceAddress) != NO_ERROR) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2599 | res = INVALID_OPERATION; |
| 2600 | continue; |
| 2601 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2602 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2603 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2604 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2605 | } |
| 2606 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2607 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2608 | status_t AudioPolicyManager::dump(int fd) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2609 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2610 | String8 result; |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 2611 | result.appendFormat("\nAudioPolicyManager Dump: %p\n", this); |
| 2612 | result.appendFormat(" Primary Output: %d\n", |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 2613 | hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE); |
Mikhail Naganov | 0d6a033 | 2016-04-19 17:12:38 -0700 | [diff] [blame] | 2614 | std::string stateLiteral; |
| 2615 | AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral); |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 2616 | result.appendFormat(" Phone state: %s\n", stateLiteral.c_str()); |
| 2617 | const char* forceUses[AUDIO_POLICY_FORCE_USE_CNT] = { |
| 2618 | "communications", "media", "record", "dock", "system", |
| 2619 | "HDMI system audio", "encoded surround output", "vibrate ringing" }; |
| 2620 | for (audio_policy_force_use_t i = AUDIO_POLICY_FORCE_FOR_COMMUNICATION; |
| 2621 | i < AUDIO_POLICY_FORCE_USE_CNT; i = (audio_policy_force_use_t)((int)i + 1)) { |
| 2622 | result.appendFormat(" Force use for %s: %d\n", |
| 2623 | forceUses[i], mEngine->getForceUse(i)); |
| 2624 | } |
| 2625 | result.appendFormat(" TTS output %savailable\n", mTtsOutputAvailable ? "" : "not "); |
| 2626 | result.appendFormat(" Master mono: %s\n", mMasterMono ? "on" : "off"); |
| 2627 | result.appendFormat(" Config source: %s\n", getConfig().getSource().c_str()); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2628 | write(fd, result.string(), result.size()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2629 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 2630 | mAvailableOutputDevices.dump(fd, String8("Available output")); |
| 2631 | mAvailableInputDevices.dump(fd, String8("Available input")); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 2632 | mHwModulesAll.dump(fd); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2633 | mOutputs.dump(fd); |
| 2634 | mInputs.dump(fd); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2635 | mVolumeCurves->dump(fd); |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2636 | mEffects.dump(fd); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2637 | mAudioPatches.dump(fd); |
Mikhail Naganov | 44344b0 | 2016-12-13 11:21:02 -0800 | [diff] [blame] | 2638 | mPolicyMixes.dump(fd); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2639 | |
| 2640 | return NO_ERROR; |
| 2641 | } |
| 2642 | |
| 2643 | // This function checks for the parameters which can be offloaded. |
| 2644 | // This can be enhanced depending on the capability of the DSP and policy |
| 2645 | // of the system. |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2646 | bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2647 | { |
| 2648 | ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2649 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2650 | offloadInfo.sample_rate, offloadInfo.channel_mask, |
| 2651 | offloadInfo.format, |
| 2652 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, |
| 2653 | offloadInfo.has_video); |
| 2654 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 2655 | if (mMasterMono) { |
| 2656 | return false; // no offloading if mono is set. |
| 2657 | } |
| 2658 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2659 | // Check if offload has been disabled |
| 2660 | char propValue[PROPERTY_VALUE_MAX]; |
| 2661 | if (property_get("audio.offload.disable", propValue, "0")) { |
| 2662 | if (atoi(propValue) != 0) { |
| 2663 | ALOGV("offload disabled by audio.offload.disable=%s", propValue ); |
| 2664 | return false; |
| 2665 | } |
| 2666 | } |
| 2667 | |
| 2668 | // Check if stream type is music, then only allow offload as of now. |
| 2669 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) |
| 2670 | { |
| 2671 | ALOGV("isOffloadSupported: stream_type != MUSIC, returning false"); |
| 2672 | return false; |
| 2673 | } |
| 2674 | |
| 2675 | //TODO: enable audio offloading with video when ready |
Andy Hung | 08945c4 | 2015-05-31 21:36:46 -0700 | [diff] [blame] | 2676 | const bool allowOffloadWithVideo = |
| 2677 | property_get_bool("audio.offload.video", false /* default_value */); |
| 2678 | if (offloadInfo.has_video && !allowOffloadWithVideo) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2679 | ALOGV("isOffloadSupported: has_video == true, returning false"); |
| 2680 | return false; |
| 2681 | } |
| 2682 | |
| 2683 | //If duration is less than minimum value defined in property, return false |
| 2684 | if (property_get("audio.offload.min.duration.secs", propValue, NULL)) { |
| 2685 | if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) { |
| 2686 | ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue); |
| 2687 | return false; |
| 2688 | } |
| 2689 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { |
| 2690 | ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS); |
| 2691 | return false; |
| 2692 | } |
| 2693 | |
| 2694 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 2695 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 2696 | // detects there is an active non offloadable effect. |
| 2697 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 2698 | // This may prevent offloading in rare situations where effects are left active by apps |
| 2699 | // in the background. |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2700 | if (mEffects.isNonOffloadableEffectEnabled()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2701 | return false; |
| 2702 | } |
| 2703 | |
| 2704 | // See if there is a profile to support this. |
| 2705 | // AUDIO_DEVICE_NONE |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2706 | sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2707 | offloadInfo.sample_rate, |
| 2708 | offloadInfo.format, |
| 2709 | offloadInfo.channel_mask, |
| 2710 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2711 | ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT "); |
| 2712 | return (profile != 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2713 | } |
| 2714 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2715 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, |
| 2716 | audio_port_type_t type, |
| 2717 | unsigned int *num_ports, |
| 2718 | struct audio_port *ports, |
| 2719 | unsigned int *generation) |
| 2720 | { |
| 2721 | if (num_ports == NULL || (*num_ports != 0 && ports == NULL) || |
| 2722 | generation == NULL) { |
| 2723 | return BAD_VALUE; |
| 2724 | } |
| 2725 | ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports); |
| 2726 | if (ports == NULL) { |
| 2727 | *num_ports = 0; |
| 2728 | } |
| 2729 | |
| 2730 | size_t portsWritten = 0; |
| 2731 | size_t portsMax = *num_ports; |
| 2732 | *num_ports = 0; |
| 2733 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2734 | // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB |
| 2735 | // as they are used by stub HALs by convention |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2736 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2737 | for (const auto& dev : mAvailableOutputDevices) { |
| 2738 | if (dev->type() == AUDIO_DEVICE_OUT_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2739 | continue; |
| 2740 | } |
| 2741 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2742 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2743 | } |
| 2744 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2745 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2746 | } |
| 2747 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2748 | for (const auto& dev : mAvailableInputDevices) { |
| 2749 | if (dev->type() == AUDIO_DEVICE_IN_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2750 | continue; |
| 2751 | } |
| 2752 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2753 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2754 | } |
| 2755 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2756 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2757 | } |
| 2758 | } |
| 2759 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { |
| 2760 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 2761 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { |
| 2762 | mInputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2763 | } |
| 2764 | *num_ports += mInputs.size(); |
| 2765 | } |
| 2766 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2767 | size_t numOutputs = 0; |
| 2768 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2769 | if (!mOutputs[i]->isDuplicated()) { |
| 2770 | numOutputs++; |
| 2771 | if (portsWritten < portsMax) { |
| 2772 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2773 | } |
| 2774 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2775 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2776 | *num_ports += numOutputs; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2777 | } |
| 2778 | } |
| 2779 | *generation = curAudioPortGeneration(); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2780 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2781 | return NO_ERROR; |
| 2782 | } |
| 2783 | |
Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 2784 | status_t AudioPolicyManager::getAudioPort(struct audio_port *port) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2785 | { |
Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 2786 | if (port == nullptr || port->id == AUDIO_PORT_HANDLE_NONE) { |
| 2787 | return BAD_VALUE; |
| 2788 | } |
| 2789 | sp<DeviceDescriptor> dev = mAvailableOutputDevices.getDeviceFromId(port->id); |
| 2790 | if (dev != 0) { |
| 2791 | dev->toAudioPort(port); |
| 2792 | return NO_ERROR; |
| 2793 | } |
| 2794 | dev = mAvailableInputDevices.getDeviceFromId(port->id); |
| 2795 | if (dev != 0) { |
| 2796 | dev->toAudioPort(port); |
| 2797 | return NO_ERROR; |
| 2798 | } |
| 2799 | sp<SwAudioOutputDescriptor> out = mOutputs.getOutputFromId(port->id); |
| 2800 | if (out != 0) { |
| 2801 | out->toAudioPort(port); |
| 2802 | return NO_ERROR; |
| 2803 | } |
| 2804 | sp<AudioInputDescriptor> in = mInputs.getInputFromId(port->id); |
| 2805 | if (in != 0) { |
| 2806 | in->toAudioPort(port); |
| 2807 | return NO_ERROR; |
| 2808 | } |
| 2809 | return BAD_VALUE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2810 | } |
| 2811 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2812 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, |
| 2813 | audio_patch_handle_t *handle, |
| 2814 | uid_t uid) |
| 2815 | { |
| 2816 | ALOGV("createAudioPatch()"); |
| 2817 | |
| 2818 | if (handle == NULL || patch == NULL) { |
| 2819 | return BAD_VALUE; |
| 2820 | } |
| 2821 | ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks); |
| 2822 | |
Mikhail Naganov | ac9858b | 2018-06-15 13:12:37 -0700 | [diff] [blame] | 2823 | if (!audio_patch_is_valid(patch)) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2824 | return BAD_VALUE; |
| 2825 | } |
| 2826 | // only one source per audio patch supported for now |
| 2827 | if (patch->num_sources > 1) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2828 | return INVALID_OPERATION; |
| 2829 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2830 | |
| 2831 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2832 | return INVALID_OPERATION; |
| 2833 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2834 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2835 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { |
| 2836 | return INVALID_OPERATION; |
| 2837 | } |
| 2838 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2839 | |
| 2840 | sp<AudioPatch> patchDesc; |
| 2841 | ssize_t index = mAudioPatches.indexOfKey(*handle); |
| 2842 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2843 | ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id, |
| 2844 | patch->sources[0].role, |
| 2845 | patch->sources[0].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2846 | #if LOG_NDEBUG == 0 |
| 2847 | for (size_t i = 0; i < patch->num_sinks; i++) { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2848 | 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] | 2849 | patch->sinks[i].role, |
| 2850 | patch->sinks[i].type); |
| 2851 | } |
| 2852 | #endif |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2853 | |
| 2854 | if (index >= 0) { |
| 2855 | patchDesc = mAudioPatches.valueAt(index); |
| 2856 | ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 2857 | mUidCached, patchDesc->mUid, uid); |
| 2858 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 2859 | return INVALID_OPERATION; |
| 2860 | } |
| 2861 | } else { |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 2862 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2863 | } |
| 2864 | |
| 2865 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2866 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2867 | if (outputDesc == NULL) { |
| 2868 | ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id); |
| 2869 | return BAD_VALUE; |
| 2870 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2871 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", |
| 2872 | outputDesc->mIoHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2873 | if (patchDesc != 0) { |
| 2874 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
| 2875 | ALOGV("createAudioPatch() source id differs for patch current id %d new id %d", |
| 2876 | patchDesc->mPatch.sources[0].id, patch->sources[0].id); |
| 2877 | return BAD_VALUE; |
| 2878 | } |
| 2879 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2880 | DeviceVector devices; |
| 2881 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2882 | // Only support mix to devices connection |
| 2883 | // TODO add support for mix to mix connection |
| 2884 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 2885 | ALOGV("createAudioPatch() source mix but sink is not a device"); |
| 2886 | return INVALID_OPERATION; |
| 2887 | } |
| 2888 | sp<DeviceDescriptor> devDesc = |
| 2889 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 2890 | if (devDesc == 0) { |
| 2891 | ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id); |
| 2892 | return BAD_VALUE; |
| 2893 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2894 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2895 | if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2896 | devDesc->mAddress, |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2897 | patch->sources[0].sample_rate, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2898 | NULL, // updatedSamplingRate |
| 2899 | patch->sources[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2900 | NULL, // updatedFormat |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2901 | patch->sources[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2902 | NULL, // updatedChannelMask |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2903 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2904 | ALOGV("createAudioPatch() profile not supported for device %08x", |
| 2905 | devDesc->type()); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2906 | return INVALID_OPERATION; |
| 2907 | } |
| 2908 | devices.add(devDesc); |
| 2909 | } |
| 2910 | if (devices.size() == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2911 | return INVALID_OPERATION; |
| 2912 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2913 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2914 | // TODO: reconfigure output format and channels here |
| 2915 | ALOGV("createAudioPatch() setting device %08x on output %d", |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2916 | devices.types(), outputDesc->mIoHandle); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2917 | setOutputDevice(outputDesc, devices.types(), true, 0, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2918 | index = mAudioPatches.indexOfKey(*handle); |
| 2919 | if (index >= 0) { |
| 2920 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 2921 | ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided"); |
| 2922 | } |
| 2923 | patchDesc = mAudioPatches.valueAt(index); |
| 2924 | patchDesc->mUid = uid; |
| 2925 | ALOGV("createAudioPatch() success"); |
| 2926 | } else { |
| 2927 | ALOGW("createAudioPatch() setOutputDevice() failed to create a patch"); |
| 2928 | return INVALID_OPERATION; |
| 2929 | } |
| 2930 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2931 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 2932 | // input device to input mix connection |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2933 | // only one sink supported when connecting an input device to a mix |
| 2934 | if (patch->num_sinks > 1) { |
| 2935 | return INVALID_OPERATION; |
| 2936 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2937 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2938 | if (inputDesc == NULL) { |
| 2939 | return BAD_VALUE; |
| 2940 | } |
| 2941 | if (patchDesc != 0) { |
| 2942 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { |
| 2943 | return BAD_VALUE; |
| 2944 | } |
| 2945 | } |
| 2946 | sp<DeviceDescriptor> devDesc = |
| 2947 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
| 2948 | if (devDesc == 0) { |
| 2949 | return BAD_VALUE; |
| 2950 | } |
| 2951 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2952 | if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2953 | devDesc->mAddress, |
| 2954 | patch->sinks[0].sample_rate, |
| 2955 | NULL, /*updatedSampleRate*/ |
| 2956 | patch->sinks[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2957 | NULL, /*updatedFormat*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2958 | patch->sinks[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2959 | NULL, /*updatedChannelMask*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2960 | // FIXME for the parameter type, |
| 2961 | // and the NONE |
| 2962 | (audio_output_flags_t) |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2963 | AUDIO_INPUT_FLAG_NONE)) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2964 | return INVALID_OPERATION; |
| 2965 | } |
| 2966 | // TODO: reconfigure output format and channels here |
| 2967 | ALOGV("createAudioPatch() setting device %08x on output %d", |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2968 | devDesc->type(), inputDesc->mIoHandle); |
| 2969 | setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2970 | index = mAudioPatches.indexOfKey(*handle); |
| 2971 | if (index >= 0) { |
| 2972 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 2973 | ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided"); |
| 2974 | } |
| 2975 | patchDesc = mAudioPatches.valueAt(index); |
| 2976 | patchDesc->mUid = uid; |
| 2977 | ALOGV("createAudioPatch() success"); |
| 2978 | } else { |
| 2979 | ALOGW("createAudioPatch() setInputDevice() failed to create a patch"); |
| 2980 | return INVALID_OPERATION; |
| 2981 | } |
| 2982 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2983 | // device to device connection |
| 2984 | if (patchDesc != 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2985 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2986 | return BAD_VALUE; |
| 2987 | } |
| 2988 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2989 | sp<DeviceDescriptor> srcDeviceDesc = |
| 2990 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 2991 | if (srcDeviceDesc == 0) { |
| 2992 | return BAD_VALUE; |
| 2993 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2994 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2995 | //update source and sink with our own data as the data passed in the patch may |
| 2996 | // be incomplete. |
| 2997 | struct audio_patch newPatch = *patch; |
| 2998 | srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2999 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3000 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 3001 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 3002 | ALOGV("createAudioPatch() source device but one sink is not a device"); |
| 3003 | return INVALID_OPERATION; |
| 3004 | } |
| 3005 | |
| 3006 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 3007 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 3008 | if (sinkDeviceDesc == 0) { |
| 3009 | return BAD_VALUE; |
| 3010 | } |
| 3011 | sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]); |
| 3012 | |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3013 | // create a software bridge in PatchPanel if: |
Scott Randolph | f317240 | 2018-01-23 17:06:53 -0800 | [diff] [blame] | 3014 | // - source and sink devices are on different HW modules OR |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3015 | // - audio HAL version is < 3.0 |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 3016 | if (!srcDeviceDesc->hasSameHwModuleAs(sinkDeviceDesc) || |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 3017 | (srcDeviceDesc->mModule->getHalVersionMajor() < 3)) { |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3018 | // support only one sink device for now to simplify output selection logic |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3019 | if (patch->num_sinks > 1) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3020 | return INVALID_OPERATION; |
| 3021 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3022 | SortedVector<audio_io_handle_t> outputs = |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3023 | getOutputsForDevice(sinkDeviceDesc->type(), mOutputs); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3024 | // if the sink device is reachable via an opened output stream, request to go via |
| 3025 | // this output stream by adding a second source to the patch description |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 3026 | audio_io_handle_t output = selectOutput(outputs, |
| 3027 | AUDIO_OUTPUT_FLAG_NONE, |
| 3028 | AUDIO_FORMAT_INVALID); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3029 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 3030 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 3031 | if (outputDesc->isDuplicated()) { |
| 3032 | return INVALID_OPERATION; |
| 3033 | } |
| 3034 | outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3035 | newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3036 | newPatch.num_sources = 2; |
| 3037 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3038 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3039 | } |
| 3040 | // TODO: check from routing capabilities in config file and other conflicting patches |
| 3041 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 3042 | status_t status = installPatch(__func__, index, handle, &newPatch, 0, uid, &patchDesc); |
| 3043 | if (status != NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3044 | ALOGW("createAudioPatch() patch panel could not connect device patch, error %d", |
| 3045 | status); |
| 3046 | return INVALID_OPERATION; |
| 3047 | } |
| 3048 | } else { |
| 3049 | return BAD_VALUE; |
| 3050 | } |
| 3051 | } else { |
| 3052 | return BAD_VALUE; |
| 3053 | } |
| 3054 | return NO_ERROR; |
| 3055 | } |
| 3056 | |
| 3057 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, |
| 3058 | uid_t uid) |
| 3059 | { |
| 3060 | ALOGV("releaseAudioPatch() patch %d", handle); |
| 3061 | |
| 3062 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 3063 | |
| 3064 | if (index < 0) { |
| 3065 | return BAD_VALUE; |
| 3066 | } |
| 3067 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 3068 | ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 3069 | mUidCached, patchDesc->mUid, uid); |
| 3070 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 3071 | return INVALID_OPERATION; |
| 3072 | } |
| 3073 | |
| 3074 | struct audio_patch *patch = &patchDesc->mPatch; |
| 3075 | patchDesc->mUid = mUidCached; |
| 3076 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3077 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3078 | if (outputDesc == NULL) { |
| 3079 | ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id); |
| 3080 | return BAD_VALUE; |
| 3081 | } |
| 3082 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3083 | setOutputDevice(outputDesc, |
| 3084 | getNewOutputDevice(outputDesc, true /*fromCache*/), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3085 | true, |
| 3086 | 0, |
| 3087 | NULL); |
| 3088 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 3089 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3090 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3091 | if (inputDesc == NULL) { |
| 3092 | ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id); |
| 3093 | return BAD_VALUE; |
| 3094 | } |
| 3095 | setInputDevice(inputDesc->mIoHandle, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 3096 | getNewInputDevice(inputDesc), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3097 | true, |
| 3098 | NULL); |
| 3099 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3100 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3101 | ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d", |
| 3102 | status, patchDesc->mAfPatchHandle); |
| 3103 | removeAudioPatch(patchDesc->mHandle); |
| 3104 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 3105 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3106 | } else { |
| 3107 | return BAD_VALUE; |
| 3108 | } |
| 3109 | } else { |
| 3110 | return BAD_VALUE; |
| 3111 | } |
| 3112 | return NO_ERROR; |
| 3113 | } |
| 3114 | |
| 3115 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, |
| 3116 | struct audio_patch *patches, |
| 3117 | unsigned int *generation) |
| 3118 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3119 | if (generation == NULL) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3120 | return BAD_VALUE; |
| 3121 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3122 | *generation = curAudioPortGeneration(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3123 | return mAudioPatches.listAudioPatches(num_patches, patches); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3124 | } |
| 3125 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3126 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3127 | { |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3128 | ALOGV("setAudioPortConfig()"); |
| 3129 | |
| 3130 | if (config == NULL) { |
| 3131 | return BAD_VALUE; |
| 3132 | } |
| 3133 | ALOGV("setAudioPortConfig() on port handle %d", config->id); |
| 3134 | // Only support gain configuration for now |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3135 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { |
| 3136 | return INVALID_OPERATION; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3137 | } |
| 3138 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3139 | sp<AudioPortConfig> audioPortConfig; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3140 | if (config->type == AUDIO_PORT_TYPE_MIX) { |
| 3141 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3142 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3143 | if (outputDesc == NULL) { |
| 3144 | return BAD_VALUE; |
| 3145 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 3146 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 3147 | "setAudioPortConfig() called on duplicated output %d", |
| 3148 | outputDesc->mIoHandle); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3149 | audioPortConfig = outputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3150 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3151 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3152 | if (inputDesc == NULL) { |
| 3153 | return BAD_VALUE; |
| 3154 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3155 | audioPortConfig = inputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3156 | } else { |
| 3157 | return BAD_VALUE; |
| 3158 | } |
| 3159 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { |
| 3160 | sp<DeviceDescriptor> deviceDesc; |
| 3161 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
| 3162 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); |
| 3163 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
| 3164 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); |
| 3165 | } else { |
| 3166 | return BAD_VALUE; |
| 3167 | } |
| 3168 | if (deviceDesc == NULL) { |
| 3169 | return BAD_VALUE; |
| 3170 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3171 | audioPortConfig = deviceDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3172 | } else { |
| 3173 | return BAD_VALUE; |
| 3174 | } |
| 3175 | |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 3176 | struct audio_port_config backupConfig = {}; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3177 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); |
| 3178 | if (status == NO_ERROR) { |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 3179 | struct audio_port_config newConfig = {}; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3180 | audioPortConfig->toAudioPortConfig(&newConfig, config); |
| 3181 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3182 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3183 | if (status != NO_ERROR) { |
| 3184 | audioPortConfig->applyAudioPortConfig(&backupConfig); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3185 | } |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3186 | |
| 3187 | return status; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3188 | } |
| 3189 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3190 | void AudioPolicyManager::releaseResourcesForUid(uid_t uid) |
| 3191 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3192 | clearAudioSources(uid); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3193 | clearAudioPatches(uid); |
| 3194 | clearSessionRoutes(uid); |
| 3195 | } |
| 3196 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3197 | void AudioPolicyManager::clearAudioPatches(uid_t uid) |
| 3198 | { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 3199 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3200 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 3201 | if (patchDesc->mUid == uid) { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 3202 | releaseAudioPatch(mAudioPatches.keyAt(i), uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3203 | } |
| 3204 | } |
| 3205 | } |
| 3206 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3207 | void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy, |
| 3208 | audio_io_handle_t ouptutToSkip) |
| 3209 | { |
| 3210 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 3211 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 3212 | for (size_t j = 0; j < mOutputs.size(); j++) { |
| 3213 | if (mOutputs.keyAt(j) == ouptutToSkip) { |
| 3214 | continue; |
| 3215 | } |
| 3216 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j); |
| 3217 | if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) { |
| 3218 | continue; |
| 3219 | } |
| 3220 | // If the default device for this strategy is on another output mix, |
| 3221 | // invalidate all tracks in this strategy to force re connection. |
| 3222 | // Otherwise select new device on the output mix. |
| 3223 | if (outputs.indexOf(mOutputs.keyAt(j)) < 0) { |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 3224 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3225 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
| 3226 | mpClientInterface->invalidateStream((audio_stream_type_t)stream); |
| 3227 | } |
| 3228 | } |
| 3229 | } else { |
| 3230 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
| 3231 | setOutputDevice(outputDesc, newDevice, false); |
| 3232 | } |
| 3233 | } |
| 3234 | } |
| 3235 | |
| 3236 | void AudioPolicyManager::clearSessionRoutes(uid_t uid) |
| 3237 | { |
| 3238 | // remove output routes associated with this uid |
| 3239 | SortedVector<routing_strategy> affectedStrategies; |
| 3240 | for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) { |
| 3241 | sp<SessionRoute> route = mOutputRoutes.valueAt(i); |
| 3242 | if (route->mUid == uid) { |
| 3243 | mOutputRoutes.removeItemsAt(i); |
| 3244 | if (route->mDeviceDescriptor != 0) { |
| 3245 | affectedStrategies.add(getStrategy(route->mStreamType)); |
| 3246 | } |
| 3247 | } |
| 3248 | } |
| 3249 | // reroute outputs if necessary |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3250 | for (const auto& strategy : affectedStrategies) { |
| 3251 | checkStrategyRoute(strategy, AUDIO_IO_HANDLE_NONE); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3252 | } |
| 3253 | |
| 3254 | // remove input routes associated with this uid |
| 3255 | SortedVector<audio_source_t> affectedSources; |
| 3256 | for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) { |
| 3257 | sp<SessionRoute> route = mInputRoutes.valueAt(i); |
| 3258 | if (route->mUid == uid) { |
| 3259 | mInputRoutes.removeItemsAt(i); |
| 3260 | if (route->mDeviceDescriptor != 0) { |
| 3261 | affectedSources.add(route->mSource); |
| 3262 | } |
| 3263 | } |
| 3264 | } |
| 3265 | // reroute inputs if necessary |
| 3266 | SortedVector<audio_io_handle_t> inputsToClose; |
| 3267 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3268 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3269 | if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3270 | inputsToClose.add(inputDesc->mIoHandle); |
| 3271 | } |
| 3272 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3273 | for (const auto& input : inputsToClose) { |
| 3274 | closeInput(input); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3275 | } |
| 3276 | } |
| 3277 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3278 | void AudioPolicyManager::clearAudioSources(uid_t uid) |
| 3279 | { |
| 3280 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
| 3281 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 3282 | if (sourceDesc->mUid == uid) { |
| 3283 | stopAudioSource(mAudioSources.keyAt(i)); |
| 3284 | } |
| 3285 | } |
| 3286 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3287 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3288 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, |
| 3289 | audio_io_handle_t *ioHandle, |
| 3290 | audio_devices_t *device) |
| 3291 | { |
Glenn Kasten | f0c6d7d | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 3292 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); |
| 3293 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT); |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 3294 | *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3295 | |
François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 3296 | return mSoundTriggerSessions.acquireSession(*session, *ioHandle); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3297 | } |
| 3298 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3299 | status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source, |
| 3300 | const audio_attributes_t *attributes, |
Glenn Kasten | 559d439 | 2016-03-29 13:42:57 -0700 | [diff] [blame] | 3301 | audio_patch_handle_t *handle, |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3302 | uid_t uid) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3303 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3304 | ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle); |
| 3305 | if (source == NULL || attributes == NULL || handle == NULL) { |
| 3306 | return BAD_VALUE; |
| 3307 | } |
| 3308 | |
Glenn Kasten | 559d439 | 2016-03-29 13:42:57 -0700 | [diff] [blame] | 3309 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3310 | |
| 3311 | if (source->role != AUDIO_PORT_ROLE_SOURCE || |
| 3312 | source->type != AUDIO_PORT_TYPE_DEVICE) { |
| 3313 | ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type); |
| 3314 | return INVALID_OPERATION; |
| 3315 | } |
| 3316 | |
| 3317 | sp<DeviceDescriptor> srcDeviceDesc = |
| 3318 | mAvailableInputDevices.getDevice(source->ext.device.type, |
| 3319 | String8(source->ext.device.address)); |
| 3320 | if (srcDeviceDesc == 0) { |
| 3321 | ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type); |
| 3322 | return BAD_VALUE; |
| 3323 | } |
| 3324 | sp<AudioSourceDescriptor> sourceDesc = |
| 3325 | new AudioSourceDescriptor(srcDeviceDesc, attributes, uid); |
| 3326 | |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 3327 | struct audio_patch dummyPatch = {}; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3328 | sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid); |
| 3329 | sourceDesc->mPatchDesc = patchDesc; |
| 3330 | |
| 3331 | status_t status = connectAudioSource(sourceDesc); |
| 3332 | if (status == NO_ERROR) { |
| 3333 | mAudioSources.add(sourceDesc->getHandle(), sourceDesc); |
| 3334 | *handle = sourceDesc->getHandle(); |
| 3335 | } |
| 3336 | return status; |
| 3337 | } |
| 3338 | |
| 3339 | status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc) |
| 3340 | { |
| 3341 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle()); |
| 3342 | |
| 3343 | // make sure we only have one patch per source. |
| 3344 | disconnectAudioSource(sourceDesc); |
| 3345 | |
| 3346 | routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 3347 | audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3348 | sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice; |
| 3349 | |
| 3350 | audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true); |
| 3351 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 3352 | mAvailableOutputDevices.getDevice(sinkDevice, String8("")); |
| 3353 | |
| 3354 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3355 | |
| 3356 | if (srcDeviceDesc->getAudioPort()->mModule->getHandle() == |
| 3357 | sinkDeviceDesc->getAudioPort()->mModule->getHandle() && |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 3358 | srcDeviceDesc->getAudioPort()->mModule->getHalVersionMajor() >= 3 && |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3359 | srcDeviceDesc->getAudioPort()->mGains.size() > 0) { |
| 3360 | ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__); |
| 3361 | // create patch between src device and output device |
| 3362 | // create Hwoutput and add to mHwOutputs |
| 3363 | } else { |
| 3364 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs); |
| 3365 | audio_io_handle_t output = |
| 3366 | selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID); |
| 3367 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 3368 | ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice); |
| 3369 | return INVALID_OPERATION; |
| 3370 | } |
| 3371 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 3372 | if (outputDesc->isDuplicated()) { |
| 3373 | ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice); |
| 3374 | return INVALID_OPERATION; |
| 3375 | } |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 3376 | status_t status = outputDesc->start(); |
| 3377 | if (status != NO_ERROR) { |
| 3378 | return status; |
| 3379 | } |
| 3380 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3381 | // create a special patch with no sink and two sources: |
| 3382 | // - the second source indicates to PatchPanel through which output mix this patch should |
| 3383 | // be connected as well as the stream type for volume control |
| 3384 | // - the sink is defined by whatever output device is currently selected for the output |
| 3385 | // though which this patch is routed. |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 3386 | PatchBuilder patchBuilder; |
| 3387 | patchBuilder.addSource(srcDeviceDesc).addSource(outputDesc, { .stream = stream }); |
| 3388 | status = mpClientInterface->createAudioPatch(patchBuilder.patch(), |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3389 | &afPatchHandle, |
| 3390 | 0); |
| 3391 | ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__, |
| 3392 | status, afPatchHandle); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 3393 | sourceDesc->mPatchDesc->mPatch = *patchBuilder.patch(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3394 | if (status != NO_ERROR) { |
| 3395 | ALOGW("%s patch panel could not connect device patch, error %d", |
| 3396 | __FUNCTION__, status); |
| 3397 | return INVALID_OPERATION; |
| 3398 | } |
| 3399 | uint32_t delayMs = 0; |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3400 | status = startSource(outputDesc, stream, sinkDevice, NULL, &delayMs); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3401 | |
| 3402 | if (status != NO_ERROR) { |
| 3403 | mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0); |
| 3404 | return status; |
| 3405 | } |
| 3406 | sourceDesc->mSwOutput = outputDesc; |
| 3407 | if (delayMs != 0) { |
| 3408 | usleep(delayMs * 1000); |
| 3409 | } |
| 3410 | } |
| 3411 | |
| 3412 | sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle; |
| 3413 | addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc); |
| 3414 | |
| 3415 | return NO_ERROR; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3416 | } |
| 3417 | |
Glenn Kasten | 559d439 | 2016-03-29 13:42:57 -0700 | [diff] [blame] | 3418 | status_t AudioPolicyManager::stopAudioSource(audio_patch_handle_t handle __unused) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3419 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3420 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle); |
| 3421 | ALOGV("%s handle %d", __FUNCTION__, handle); |
| 3422 | if (sourceDesc == 0) { |
| 3423 | ALOGW("%s unknown source for handle %d", __FUNCTION__, handle); |
| 3424 | return BAD_VALUE; |
| 3425 | } |
| 3426 | status_t status = disconnectAudioSource(sourceDesc); |
| 3427 | |
| 3428 | mAudioSources.removeItem(handle); |
| 3429 | return status; |
| 3430 | } |
| 3431 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3432 | status_t AudioPolicyManager::setMasterMono(bool mono) |
| 3433 | { |
| 3434 | if (mMasterMono == mono) { |
| 3435 | return NO_ERROR; |
| 3436 | } |
| 3437 | mMasterMono = mono; |
| 3438 | // if enabling mono we close all offloaded devices, which will invalidate the |
| 3439 | // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible |
| 3440 | // for recreating the new AudioTrack as non-offloaded PCM. |
| 3441 | // |
| 3442 | // If disabling mono, we leave all tracks as is: we don't know which clients |
| 3443 | // and tracks are able to be recreated as offloaded. The next "song" should |
| 3444 | // play back offloaded. |
| 3445 | if (mMasterMono) { |
| 3446 | Vector<audio_io_handle_t> offloaded; |
| 3447 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3448 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 3449 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
| 3450 | offloaded.push(desc->mIoHandle); |
| 3451 | } |
| 3452 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3453 | for (const auto& handle : offloaded) { |
| 3454 | closeOutput(handle); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3455 | } |
| 3456 | } |
| 3457 | // update master mono for all remaining outputs |
| 3458 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3459 | updateMono(mOutputs.keyAt(i)); |
| 3460 | } |
| 3461 | return NO_ERROR; |
| 3462 | } |
| 3463 | |
| 3464 | status_t AudioPolicyManager::getMasterMono(bool *mono) |
| 3465 | { |
| 3466 | *mono = mMasterMono; |
| 3467 | return NO_ERROR; |
| 3468 | } |
| 3469 | |
Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 3470 | float AudioPolicyManager::getStreamVolumeDB( |
| 3471 | audio_stream_type_t stream, int index, audio_devices_t device) |
| 3472 | { |
| 3473 | return computeVolume(stream, index, device); |
| 3474 | } |
| 3475 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3476 | status_t AudioPolicyManager::getSupportedFormats(audio_io_handle_t ioHandle, |
| 3477 | FormatVector& formats) { |
| 3478 | if (ioHandle == AUDIO_IO_HANDLE_NONE) { |
| 3479 | return BAD_VALUE; |
| 3480 | } |
| 3481 | String8 reply; |
| 3482 | reply = mpClientInterface->getParameters( |
| 3483 | ioHandle, String8(AudioParameter::keyStreamSupportedFormats)); |
| 3484 | ALOGV("%s: supported formats %s", __FUNCTION__, reply.string()); |
| 3485 | AudioParameter repliedParameters(reply); |
| 3486 | if (repliedParameters.get( |
| 3487 | String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) { |
| 3488 | ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__); |
| 3489 | return BAD_VALUE; |
| 3490 | } |
| 3491 | for (auto format : formatsFromString(reply.string())) { |
| 3492 | // Only AUDIO_FORMAT_AAC_LC will be used in Settings UI for all AAC formats. |
| 3493 | for (size_t i = 0; i < ARRAY_SIZE(AAC_FORMATS); i++) { |
| 3494 | if (format == AAC_FORMATS[i]) { |
| 3495 | format = AUDIO_FORMAT_AAC_LC; |
| 3496 | break; |
| 3497 | } |
| 3498 | } |
| 3499 | bool exist = false; |
| 3500 | for (size_t i = 0; i < formats.size(); i++) { |
| 3501 | if (format == formats[i]) { |
| 3502 | exist = true; |
| 3503 | break; |
| 3504 | } |
| 3505 | } |
| 3506 | bool isSurroundFormat = false; |
| 3507 | for (size_t i = 0; i < ARRAY_SIZE(SURROUND_FORMATS); i++) { |
| 3508 | if (SURROUND_FORMATS[i] == format) { |
| 3509 | isSurroundFormat = true; |
| 3510 | break; |
| 3511 | } |
| 3512 | } |
| 3513 | if (!exist && isSurroundFormat) { |
| 3514 | formats.add(format); |
| 3515 | } |
| 3516 | } |
| 3517 | return NO_ERROR; |
| 3518 | } |
| 3519 | |
| 3520 | status_t AudioPolicyManager::getSurroundFormats(unsigned int *numSurroundFormats, |
| 3521 | audio_format_t *surroundFormats, |
| 3522 | bool *surroundFormatsEnabled, |
| 3523 | bool reported) |
| 3524 | { |
| 3525 | if (numSurroundFormats == NULL || (*numSurroundFormats != 0 && |
| 3526 | (surroundFormats == NULL || surroundFormatsEnabled == NULL))) { |
| 3527 | return BAD_VALUE; |
| 3528 | } |
| 3529 | ALOGV("getSurroundFormats() numSurroundFormats %d surroundFormats %p surroundFormatsEnabled %p", |
| 3530 | *numSurroundFormats, surroundFormats, surroundFormatsEnabled); |
| 3531 | |
| 3532 | // Only return value if there is HDMI output. |
| 3533 | if ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_HDMI) == 0) { |
| 3534 | return INVALID_OPERATION; |
| 3535 | } |
| 3536 | |
| 3537 | size_t formatsWritten = 0; |
| 3538 | size_t formatsMax = *numSurroundFormats; |
| 3539 | *numSurroundFormats = 0; |
| 3540 | FormatVector formats; |
| 3541 | if (reported) { |
| 3542 | // Only get surround formats which are reported by device. |
| 3543 | // First list already open outputs that can be routed to this device |
| 3544 | audio_devices_t device = AUDIO_DEVICE_OUT_HDMI; |
| 3545 | SortedVector<audio_io_handle_t> outputs; |
| 3546 | bool reportedFormatFound = false; |
| 3547 | status_t status; |
| 3548 | sp<SwAudioOutputDescriptor> desc; |
| 3549 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3550 | desc = mOutputs.valueAt(i); |
| 3551 | if (!desc->isDuplicated() && (desc->supportedDevices() & device)) { |
| 3552 | outputs.add(mOutputs.keyAt(i)); |
| 3553 | } |
| 3554 | } |
| 3555 | // Open an output to query dynamic parameters. |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 3556 | DeviceVector hdmiOutputDevices = mAvailableOutputDevices.getDevicesFromTypeMask( |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3557 | AUDIO_DEVICE_OUT_HDMI); |
| 3558 | for (size_t i = 0; i < hdmiOutputDevices.size(); i++) { |
| 3559 | String8 address = hdmiOutputDevices[i]->mAddress; |
| 3560 | for (const auto& hwModule : mHwModules) { |
| 3561 | for (size_t i = 0; i < hwModule->getOutputProfiles().size(); i++) { |
| 3562 | sp<IOProfile> profile = hwModule->getOutputProfiles()[i]; |
| 3563 | if (profile->supportDevice(AUDIO_DEVICE_OUT_HDMI) && |
| 3564 | profile->supportDeviceAddress(address)) { |
| 3565 | size_t j; |
| 3566 | for (j = 0; j < outputs.size(); j++) { |
| 3567 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
| 3568 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
| 3569 | break; |
| 3570 | } |
| 3571 | } |
| 3572 | if (j != outputs.size()) { |
| 3573 | status = getSupportedFormats(outputs.itemAt(j), formats); |
| 3574 | reportedFormatFound |= (status == NO_ERROR); |
| 3575 | continue; |
| 3576 | } |
| 3577 | |
| 3578 | if (!profile->canOpenNewIo()) { |
| 3579 | ALOGW("Max Output number %u already opened for this profile %s", |
| 3580 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 3581 | continue; |
| 3582 | } |
| 3583 | |
| 3584 | ALOGV("opening output for device %08x with params %s profile %p name %s", |
| 3585 | device, address.string(), profile.get(), profile->getName().string()); |
| 3586 | desc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
| 3587 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 3588 | status_t status = desc->open(nullptr, device, address, |
| 3589 | AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, |
| 3590 | &output); |
| 3591 | |
| 3592 | if (status == NO_ERROR) { |
| 3593 | status = getSupportedFormats(output, formats); |
| 3594 | reportedFormatFound |= (status == NO_ERROR); |
| 3595 | desc->close(); |
| 3596 | output = AUDIO_IO_HANDLE_NONE; |
| 3597 | } |
| 3598 | } |
| 3599 | } |
| 3600 | } |
| 3601 | } |
| 3602 | |
| 3603 | if (!reportedFormatFound) { |
| 3604 | return UNKNOWN_ERROR; |
| 3605 | } |
| 3606 | } else { |
| 3607 | for (size_t i = 0; i < ARRAY_SIZE(SURROUND_FORMATS); i++) { |
| 3608 | formats.add(SURROUND_FORMATS[i]); |
| 3609 | } |
| 3610 | } |
| 3611 | for (size_t i = 0; i < formats.size(); i++) { |
| 3612 | if (formatsWritten < formatsMax) { |
| 3613 | surroundFormats[formatsWritten] = formats[i]; |
| 3614 | bool formatEnabled = false; |
| 3615 | if (formats[i] == AUDIO_FORMAT_AAC_LC) { |
| 3616 | for (size_t j = 0; j < ARRAY_SIZE(AAC_FORMATS); j++) { |
| 3617 | formatEnabled = |
| 3618 | mSurroundFormats.find(AAC_FORMATS[i]) != mSurroundFormats.end(); |
| 3619 | break; |
| 3620 | } |
| 3621 | } else { |
| 3622 | formatEnabled = mSurroundFormats.find(formats[i]) != mSurroundFormats.end(); |
| 3623 | } |
| 3624 | surroundFormatsEnabled[formatsWritten++] = formatEnabled; |
| 3625 | } |
| 3626 | (*numSurroundFormats)++; |
| 3627 | } |
| 3628 | return NO_ERROR; |
| 3629 | } |
| 3630 | |
| 3631 | status_t AudioPolicyManager::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) |
| 3632 | { |
| 3633 | // Check if audio format is a surround formats. |
| 3634 | bool isSurroundFormat = false; |
| 3635 | for (size_t i = 0; i < ARRAY_SIZE(SURROUND_FORMATS); i++) { |
| 3636 | if (audioFormat == SURROUND_FORMATS[i]) { |
| 3637 | isSurroundFormat = true; |
| 3638 | break; |
| 3639 | } |
| 3640 | } |
| 3641 | if (!isSurroundFormat) { |
| 3642 | return BAD_VALUE; |
| 3643 | } |
| 3644 | |
| 3645 | // Should only be called when MANUAL. |
| 3646 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 3647 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
| 3648 | if (forceUse != AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
| 3649 | return INVALID_OPERATION; |
| 3650 | } |
| 3651 | |
| 3652 | if ((mSurroundFormats.find(audioFormat) != mSurroundFormats.end() && enabled) |
| 3653 | || (mSurroundFormats.find(audioFormat) == mSurroundFormats.end() && !enabled)) { |
| 3654 | return NO_ERROR; |
| 3655 | } |
| 3656 | |
| 3657 | // The operation is valid only when there is HDMI output available. |
| 3658 | if ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_HDMI) == 0) { |
| 3659 | return INVALID_OPERATION; |
| 3660 | } |
| 3661 | |
| 3662 | if (enabled) { |
| 3663 | if (audioFormat == AUDIO_FORMAT_AAC_LC) { |
| 3664 | for (size_t i = 0; i < ARRAY_SIZE(AAC_FORMATS); i++) { |
| 3665 | mSurroundFormats.insert(AAC_FORMATS[i]); |
| 3666 | } |
| 3667 | } else { |
| 3668 | mSurroundFormats.insert(audioFormat); |
| 3669 | } |
| 3670 | } else { |
| 3671 | if (audioFormat == AUDIO_FORMAT_AAC_LC) { |
| 3672 | for (size_t i = 0; i < ARRAY_SIZE(AAC_FORMATS); i++) { |
| 3673 | mSurroundFormats.erase(AAC_FORMATS[i]); |
| 3674 | } |
| 3675 | } else { |
| 3676 | mSurroundFormats.erase(audioFormat); |
| 3677 | } |
| 3678 | } |
| 3679 | |
| 3680 | sp<SwAudioOutputDescriptor> outputDesc; |
| 3681 | bool profileUpdated = false; |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 3682 | DeviceVector hdmiOutputDevices = mAvailableOutputDevices.getDevicesFromTypeMask( |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3683 | AUDIO_DEVICE_OUT_HDMI); |
| 3684 | for (size_t i = 0; i < hdmiOutputDevices.size(); i++) { |
| 3685 | // Simulate reconnection to update enabled surround sound formats. |
| 3686 | String8 address = hdmiOutputDevices[i]->mAddress; |
| 3687 | String8 name = hdmiOutputDevices[i]->getName(); |
| 3688 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 3689 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 3690 | address.c_str(), |
| 3691 | name.c_str()); |
| 3692 | if (status != NO_ERROR) { |
| 3693 | continue; |
| 3694 | } |
| 3695 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 3696 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 3697 | address.c_str(), |
| 3698 | name.c_str()); |
| 3699 | profileUpdated |= (status == NO_ERROR); |
| 3700 | } |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 3701 | DeviceVector hdmiInputDevices = mAvailableInputDevices.getDevicesFromTypeMask( |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3702 | AUDIO_DEVICE_IN_HDMI); |
| 3703 | for (size_t i = 0; i < hdmiInputDevices.size(); i++) { |
| 3704 | // Simulate reconnection to update enabled surround sound formats. |
| 3705 | String8 address = hdmiInputDevices[i]->mAddress; |
| 3706 | String8 name = hdmiInputDevices[i]->getName(); |
| 3707 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 3708 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 3709 | address.c_str(), |
| 3710 | name.c_str()); |
| 3711 | if (status != NO_ERROR) { |
| 3712 | continue; |
| 3713 | } |
| 3714 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 3715 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 3716 | address.c_str(), |
| 3717 | name.c_str()); |
| 3718 | profileUpdated |= (status == NO_ERROR); |
| 3719 | } |
| 3720 | |
| 3721 | // Undo the surround formats change due to no audio profiles updated. |
| 3722 | if (!profileUpdated) { |
| 3723 | if (enabled) { |
| 3724 | if (audioFormat == AUDIO_FORMAT_AAC_LC) { |
| 3725 | for (size_t i = 0; i < ARRAY_SIZE(AAC_FORMATS); i++) { |
| 3726 | mSurroundFormats.erase(AAC_FORMATS[i]); |
| 3727 | } |
| 3728 | } else { |
| 3729 | mSurroundFormats.erase(audioFormat); |
| 3730 | } |
| 3731 | } else { |
| 3732 | if (audioFormat == AUDIO_FORMAT_AAC_LC) { |
| 3733 | for (size_t i = 0; i < ARRAY_SIZE(AAC_FORMATS); i++) { |
| 3734 | mSurroundFormats.insert(AAC_FORMATS[i]); |
| 3735 | } |
| 3736 | } else { |
| 3737 | mSurroundFormats.insert(audioFormat); |
| 3738 | } |
| 3739 | } |
| 3740 | } |
| 3741 | |
| 3742 | return profileUpdated ? NO_ERROR : INVALID_OPERATION; |
| 3743 | } |
| 3744 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 3745 | void AudioPolicyManager::setRecordSilenced(uid_t uid, bool silenced) |
| 3746 | { |
| 3747 | ALOGV("AudioPolicyManager:setRecordSilenced(uid:%d, silenced:%d)", uid, silenced); |
| 3748 | |
| 3749 | Vector<sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs(); |
| 3750 | for (size_t i = 0; i < activeInputs.size(); i++) { |
| 3751 | sp<AudioInputDescriptor> activeDesc = activeInputs[i]; |
| 3752 | AudioSessionCollection activeSessions = activeDesc->getAudioSessions(true); |
| 3753 | for (size_t j = 0; j < activeSessions.size(); j++) { |
| 3754 | sp<AudioSession> activeSession = activeSessions.valueAt(j); |
| 3755 | if (activeSession->uid() == uid) { |
| 3756 | activeSession->setSilenced(silenced); |
| 3757 | } |
| 3758 | } |
| 3759 | } |
| 3760 | } |
| 3761 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3762 | status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc) |
| 3763 | { |
| 3764 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle()); |
| 3765 | |
| 3766 | sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle); |
| 3767 | if (patchDesc == 0) { |
| 3768 | ALOGW("%s source has no patch with handle %d", __FUNCTION__, |
| 3769 | sourceDesc->mPatchDesc->mHandle); |
| 3770 | return BAD_VALUE; |
| 3771 | } |
| 3772 | removeAudioPatch(sourceDesc->mPatchDesc->mHandle); |
| 3773 | |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 3774 | audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3775 | sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote(); |
| 3776 | if (swOutputDesc != 0) { |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 3777 | status_t status = stopSource(swOutputDesc, stream, false); |
| 3778 | if (status == NO_ERROR) { |
| 3779 | swOutputDesc->stop(); |
| 3780 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3781 | mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3782 | } else { |
| 3783 | sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote(); |
| 3784 | if (hwOutputDesc != 0) { |
| 3785 | // release patch between src device and output device |
| 3786 | // close Hwoutput and remove from mHwOutputs |
| 3787 | } else { |
| 3788 | ALOGW("%s source has neither SW nor HW output", __FUNCTION__); |
| 3789 | } |
| 3790 | } |
| 3791 | return NO_ERROR; |
| 3792 | } |
| 3793 | |
| 3794 | sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput( |
| 3795 | audio_io_handle_t output, routing_strategy strategy) |
| 3796 | { |
| 3797 | sp<AudioSourceDescriptor> source; |
| 3798 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
| 3799 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 3800 | routing_strategy sourceStrategy = |
| 3801 | (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes); |
| 3802 | sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote(); |
| 3803 | if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) { |
| 3804 | source = sourceDesc; |
| 3805 | break; |
| 3806 | } |
| 3807 | } |
| 3808 | return source; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3809 | } |
| 3810 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3811 | // ---------------------------------------------------------------------------- |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3812 | // AudioPolicyManager |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3813 | // ---------------------------------------------------------------------------- |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3814 | uint32_t AudioPolicyManager::nextAudioPortGeneration() |
| 3815 | { |
Mikhail Naganov | 2773dd7 | 2017-12-08 10:12:11 -0800 | [diff] [blame] | 3816 | return mAudioPortGeneration++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3817 | } |
| 3818 | |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3819 | #ifdef USE_XML_AUDIO_POLICY_CONF |
| 3820 | // Treblized audio policy xml config will be located in /odm/etc or /vendor/etc. |
| 3821 | static const char *kConfigLocationList[] = |
| 3822 | {"/odm/etc", "/vendor/etc", "/system/etc"}; |
| 3823 | static const int kConfigLocationListSize = |
| 3824 | (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0])); |
| 3825 | |
| 3826 | static status_t deserializeAudioPolicyXmlConfig(AudioPolicyConfig &config) { |
| 3827 | char audioPolicyXmlConfigFile[AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH]; |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3828 | std::vector<const char*> fileNames; |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3829 | status_t ret; |
| 3830 | |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3831 | if (property_get_bool("ro.bluetooth.a2dp_offload.supported", false) && |
| 3832 | property_get_bool("persist.bluetooth.a2dp_offload.disabled", false)) { |
| 3833 | // A2DP offload supported but disabled: try to use special XML file |
| 3834 | fileNames.push_back(AUDIO_POLICY_A2DP_OFFLOAD_DISABLED_XML_CONFIG_FILE_NAME); |
| 3835 | } |
| 3836 | fileNames.push_back(AUDIO_POLICY_XML_CONFIG_FILE_NAME); |
| 3837 | |
| 3838 | for (const char* fileName : fileNames) { |
| 3839 | for (int i = 0; i < kConfigLocationListSize; i++) { |
| 3840 | PolicySerializer serializer; |
| 3841 | snprintf(audioPolicyXmlConfigFile, sizeof(audioPolicyXmlConfigFile), |
| 3842 | "%s/%s", kConfigLocationList[i], fileName); |
| 3843 | ret = serializer.deserialize(audioPolicyXmlConfigFile, config); |
| 3844 | if (ret == NO_ERROR) { |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 3845 | config.setSource(audioPolicyXmlConfigFile); |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3846 | return ret; |
| 3847 | } |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3848 | } |
| 3849 | } |
| 3850 | return ret; |
| 3851 | } |
| 3852 | #endif |
| 3853 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3854 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface, |
| 3855 | bool /*forTesting*/) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3856 | : |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 3857 | 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] | 3858 | mpClientInterface(clientInterface), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3859 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3860 | mA2dpSuspended(false), |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3861 | #ifdef USE_XML_AUDIO_POLICY_CONF |
| 3862 | mVolumeCurves(new VolumeCurvesCollection()), |
| 3863 | mConfig(mHwModulesAll, mAvailableOutputDevices, mAvailableInputDevices, |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 3864 | mDefaultOutputDevice, static_cast<VolumeCurvesCollection*>(mVolumeCurves.get())), |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3865 | #else |
| 3866 | mVolumeCurves(new StreamDescriptorCollection()), |
| 3867 | mConfig(mHwModulesAll, mAvailableOutputDevices, mAvailableInputDevices, |
| 3868 | mDefaultOutputDevice), |
| 3869 | #endif |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 3870 | mAudioPortGeneration(1), |
| 3871 | mBeaconMuteRefCount(0), |
| 3872 | mBeaconPlayingRefCount(0), |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3873 | mBeaconMuted(false), |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3874 | mTtsOutputAvailable(false), |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3875 | mMasterMono(false), |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 3876 | mMusicEffectOutput(AUDIO_IO_HANDLE_NONE), |
| 3877 | mHasComputedSoundTriggerSupportsConcurrentCapture(false) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3878 | { |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3879 | } |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3880 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3881 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) |
| 3882 | : AudioPolicyManager(clientInterface, false /*forTesting*/) |
| 3883 | { |
| 3884 | loadConfig(); |
| 3885 | initialize(); |
| 3886 | } |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3887 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3888 | void AudioPolicyManager::loadConfig() { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3889 | #ifdef USE_XML_AUDIO_POLICY_CONF |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3890 | if (deserializeAudioPolicyXmlConfig(getConfig()) != NO_ERROR) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3891 | #else |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3892 | if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, getConfig()) != NO_ERROR) |
| 3893 | && (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, getConfig()) != NO_ERROR)) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3894 | #endif |
| 3895 | ALOGE("could not load audio policy configuration file, setting defaults"); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3896 | getConfig().setDefault(); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3897 | } |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3898 | } |
| 3899 | |
| 3900 | status_t AudioPolicyManager::initialize() { |
| 3901 | mVolumeCurves->initializeVolumeCurves(getConfig().isSpeakerDrcEnabled()); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3902 | |
| 3903 | // 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] | 3904 | audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance(); |
| 3905 | if (!engineInstance) { |
| 3906 | ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3907 | return NO_INIT; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3908 | } |
| 3909 | // Retrieve the Policy Manager Interface |
| 3910 | mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>(); |
| 3911 | if (mEngine == NULL) { |
| 3912 | ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3913 | return NO_INIT; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3914 | } |
| 3915 | mEngine->setObserver(this); |
| 3916 | status_t status = mEngine->initCheck(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3917 | if (status != NO_ERROR) { |
| 3918 | LOG_FATAL("Policy engine not initialized(err=%d)", status); |
| 3919 | return status; |
| 3920 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3921 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3922 | // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3923 | // open all output streams needed to access attached devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3924 | audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types(); |
| 3925 | audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3926 | for (const auto& hwModule : mHwModulesAll) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 3927 | hwModule->setHandle(mpClientInterface->loadHwModule(hwModule->getName())); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3928 | if (hwModule->getHandle() == AUDIO_MODULE_HANDLE_NONE) { |
| 3929 | ALOGW("could not open HW module %s", hwModule->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3930 | continue; |
| 3931 | } |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3932 | mHwModules.push_back(hwModule); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3933 | // open all output streams needed to access attached devices |
| 3934 | // except for direct output streams that are only opened when they are actually |
| 3935 | // required by an app. |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3936 | // This also validates mAvailableOutputDevices list |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 3937 | for (const auto& outProfile : hwModule->getOutputProfiles()) { |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 3938 | if (!outProfile->canOpenNewIo()) { |
| 3939 | ALOGE("Invalid Output profile max open count %u for profile %s", |
| 3940 | outProfile->maxOpenCount, outProfile->getTagName().c_str()); |
| 3941 | continue; |
| 3942 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3943 | if (!outProfile->hasSupportedDevices()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3944 | ALOGW("Output profile contains no device on module %s", hwModule->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3945 | continue; |
| 3946 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3947 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3948 | mTtsOutputAvailable = true; |
| 3949 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3950 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3951 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3952 | continue; |
| 3953 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3954 | audio_devices_t profileType = outProfile->getSupportedDevicesType(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3955 | if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) { |
| 3956 | profileType = mDefaultOutputDevice->type(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3957 | } else { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3958 | // chose first device present in profile's SupportedDevices also part of |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3959 | // outputDeviceTypes |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3960 | profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3961 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3962 | if ((profileType & outputDeviceTypes) == 0) { |
| 3963 | continue; |
| 3964 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3965 | sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile, |
| 3966 | mpClientInterface); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3967 | const DeviceVector &supportedDevices = outProfile->getSupportedDevices(); |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 3968 | const DeviceVector &devicesForType = supportedDevices.getDevicesFromTypeMask( |
| 3969 | profileType); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3970 | String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress |
| 3971 | : String8(""); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3972 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 3973 | status_t status = outputDesc->open(nullptr, profileType, address, |
| 3974 | AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, &output); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3975 | |
| 3976 | if (status != NO_ERROR) { |
| 3977 | ALOGW("Cannot open output stream for device %08x on hw module %s", |
| 3978 | outputDesc->mDevice, |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3979 | hwModule->getName()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3980 | } else { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3981 | for (const auto& dev : supportedDevices) { |
| 3982 | ssize_t index = mAvailableOutputDevices.indexOf(dev); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3983 | // 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] | 3984 | if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3985 | mAvailableOutputDevices[index]->attach(hwModule); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3986 | } |
| 3987 | } |
| 3988 | if (mPrimaryOutput == 0 && |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3989 | outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3990 | mPrimaryOutput = outputDesc; |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3991 | } |
| 3992 | addOutput(output, outputDesc); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3993 | setOutputDevice(outputDesc, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 3994 | profileType, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3995 | true, |
| 3996 | 0, |
| 3997 | NULL, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 3998 | address); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3999 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4000 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4001 | // open input streams needed to access attached devices to validate |
| 4002 | // mAvailableInputDevices list |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4003 | for (const auto& inProfile : hwModule->getInputProfiles()) { |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4004 | if (!inProfile->canOpenNewIo()) { |
| 4005 | ALOGE("Invalid Input profile max open count %u for profile %s", |
| 4006 | inProfile->maxOpenCount, inProfile->getTagName().c_str()); |
| 4007 | continue; |
| 4008 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4009 | if (!inProfile->hasSupportedDevices()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4010 | ALOGW("Input profile contains no device on module %s", hwModule->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4011 | continue; |
| 4012 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4013 | // chose first device present in profile's SupportedDevices also part of |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4014 | // inputDeviceTypes |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4015 | audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes); |
| 4016 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4017 | if ((profileType & inputDeviceTypes) == 0) { |
| 4018 | continue; |
| 4019 | } |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 4020 | sp<AudioInputDescriptor> inputDesc = |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4021 | new AudioInputDescriptor(inProfile, mpClientInterface); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4022 | |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 4023 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromTypeMask(profileType); |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 4024 | // the inputs vector must be of size >= 1, but we don't want to crash here |
| 4025 | String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress |
| 4026 | : String8(""); |
| 4027 | ALOGV(" for input device 0x%x using address %s", profileType, address.string()); |
| 4028 | ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!"); |
| 4029 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4030 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4031 | status_t status = inputDesc->open(nullptr, |
| 4032 | profileType, |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 4033 | address, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4034 | AUDIO_SOURCE_MIC, |
| 4035 | AUDIO_INPUT_FLAG_NONE, |
| 4036 | &input); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4037 | |
| 4038 | if (status == NO_ERROR) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4039 | for (const auto& dev : inProfile->getSupportedDevices()) { |
| 4040 | ssize_t index = mAvailableInputDevices.indexOf(dev); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4041 | // 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] | 4042 | if (index >= 0) { |
| 4043 | sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index]; |
| 4044 | if (!devDesc->isAttached()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4045 | devDesc->attach(hwModule); |
Eric Laurent | 83efe1c | 2017-07-09 16:51:08 -0700 | [diff] [blame] | 4046 | devDesc->importAudioPort(inProfile, true); |
Eric Laurent | 45aabc3 | 2015-08-06 09:11:13 -0700 | [diff] [blame] | 4047 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4048 | } |
| 4049 | } |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4050 | inputDesc->close(); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4051 | } else { |
| 4052 | ALOGW("Cannot open input stream for device %08x on hw module %s", |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4053 | profileType, |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4054 | hwModule->getName()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4055 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4056 | } |
| 4057 | } |
| 4058 | // make sure all attached devices have been allocated a unique ID |
| 4059 | for (size_t i = 0; i < mAvailableOutputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 4060 | if (!mAvailableOutputDevices[i]->isAttached()) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4061 | ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4062 | mAvailableOutputDevices.remove(mAvailableOutputDevices[i]); |
| 4063 | continue; |
| 4064 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4065 | // The device is now validated and can be appended to the available devices of the engine |
| 4066 | mEngine->setDeviceConnectionState(mAvailableOutputDevices[i], |
| 4067 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4068 | i++; |
| 4069 | } |
| 4070 | for (size_t i = 0; i < mAvailableInputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 4071 | if (!mAvailableInputDevices[i]->isAttached()) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4072 | ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4073 | mAvailableInputDevices.remove(mAvailableInputDevices[i]); |
| 4074 | continue; |
| 4075 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4076 | // The device is now validated and can be appended to the available devices of the engine |
| 4077 | mEngine->setDeviceConnectionState(mAvailableInputDevices[i], |
| 4078 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4079 | i++; |
| 4080 | } |
| 4081 | // make sure default device is reachable |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4082 | if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4083 | ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type()); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4084 | status = NO_INIT; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4085 | } |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 4086 | // If microphones address is empty, set it according to device type |
| 4087 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { |
| 4088 | if (mAvailableInputDevices[i]->mAddress.isEmpty()) { |
| 4089 | if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BUILTIN_MIC) { |
| 4090 | mAvailableInputDevices[i]->mAddress = String8(AUDIO_BOTTOM_MICROPHONE_ADDRESS); |
| 4091 | } else if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BACK_MIC) { |
| 4092 | mAvailableInputDevices[i]->mAddress = String8(AUDIO_BACK_MICROPHONE_ADDRESS); |
| 4093 | } |
| 4094 | } |
| 4095 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4096 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4097 | if (mPrimaryOutput == 0) { |
| 4098 | ALOGE("Failed to open primary output"); |
| 4099 | status = NO_INIT; |
| 4100 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4101 | |
| 4102 | updateDevicesAndOutputs(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4103 | return status; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4104 | } |
| 4105 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4106 | AudioPolicyManager::~AudioPolicyManager() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4107 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4108 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4109 | mOutputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4110 | } |
| 4111 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4112 | mInputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4113 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4114 | mAvailableOutputDevices.clear(); |
| 4115 | mAvailableInputDevices.clear(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4116 | mOutputs.clear(); |
| 4117 | mInputs.clear(); |
| 4118 | mHwModules.clear(); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4119 | mHwModulesAll.clear(); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4120 | mSurroundFormats.clear(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4121 | } |
| 4122 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4123 | status_t AudioPolicyManager::initCheck() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4124 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 4125 | return hasPrimaryOutput() ? NO_ERROR : NO_INIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4126 | } |
| 4127 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4128 | // --- |
| 4129 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 4130 | void AudioPolicyManager::addOutput(audio_io_handle_t output, |
| 4131 | const sp<SwAudioOutputDescriptor>& outputDesc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4132 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4133 | mOutputs.add(output, outputDesc); |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 4134 | applyStreamVolumes(outputDesc, AUDIO_DEVICE_NONE, 0 /* delayMs */, true /* force */); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4135 | updateMono(output); // update mono status when adding to output list |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4136 | selectOutputForMusicEffects(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4137 | nextAudioPortGeneration(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4138 | } |
| 4139 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4140 | void AudioPolicyManager::removeOutput(audio_io_handle_t output) |
| 4141 | { |
| 4142 | mOutputs.removeItem(output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4143 | selectOutputForMusicEffects(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4144 | } |
| 4145 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 4146 | void AudioPolicyManager::addInput(audio_io_handle_t input, |
| 4147 | const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4148 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4149 | mInputs.add(input, inputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4150 | nextAudioPortGeneration(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4151 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4152 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4153 | void AudioPolicyManager::findIoHandlesByAddress(const sp<SwAudioOutputDescriptor>& desc /*in*/, |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4154 | const audio_devices_t device /*in*/, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4155 | const String8& address /*in*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4156 | SortedVector<audio_io_handle_t>& outputs /*out*/) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4157 | sp<DeviceDescriptor> devDesc = |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4158 | desc->mProfile->getSupportedDeviceByAddress(device, address); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4159 | if (devDesc != 0) { |
| 4160 | ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s", |
| 4161 | desc->mIoHandle, address.string()); |
| 4162 | outputs.add(desc->mIoHandle); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4163 | } |
| 4164 | } |
| 4165 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4166 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4167 | audio_policy_dev_state_t state, |
| 4168 | SortedVector<audio_io_handle_t>& outputs, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4169 | const String8& address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4170 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4171 | audio_devices_t device = devDesc->type(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4172 | sp<SwAudioOutputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4173 | |
| 4174 | if (audio_device_is_digital(device)) { |
| 4175 | // erase all current sample rates, formats and channel masks |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 4176 | devDesc->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4177 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4178 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4179 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4180 | // first list already open outputs that can be routed to this device |
| 4181 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4182 | desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4183 | if (!desc->isDuplicated() && (desc->supportedDevices() & device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4184 | if (!device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4185 | ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i)); |
| 4186 | outputs.add(mOutputs.keyAt(i)); |
| 4187 | } else { |
| 4188 | ALOGV(" checking address match due to device 0x%x", device); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4189 | findIoHandlesByAddress(desc, device, address, outputs); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4190 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4191 | } |
| 4192 | } |
| 4193 | // then look for output profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4194 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4195 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4196 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 4197 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4198 | if (profile->supportDevice(device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4199 | if (!device_distinguishes_on_address(device) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4200 | profile->supportDeviceAddress(address)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4201 | profiles.add(profile); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4202 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %s", |
| 4203 | j, hwModule->getName()); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4204 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4205 | } |
| 4206 | } |
| 4207 | } |
| 4208 | |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 4209 | ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size()); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4210 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4211 | if (profiles.isEmpty() && outputs.isEmpty()) { |
| 4212 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 4213 | return BAD_VALUE; |
| 4214 | } |
| 4215 | |
| 4216 | // open outputs for matching profiles if needed. Direct outputs are also opened to |
| 4217 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 4218 | 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] | 4219 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4220 | |
| 4221 | // nothing to do if one output is already opened for this profile |
| 4222 | size_t j; |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4223 | for (j = 0; j < outputs.size(); j++) { |
| 4224 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4225 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 4226 | // matching profile: save the sample rates, format and channel masks supported |
| 4227 | // by the profile in our device descriptor |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4228 | if (audio_device_is_digital(device)) { |
| 4229 | devDesc->importAudioPort(profile); |
| 4230 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4231 | break; |
| 4232 | } |
| 4233 | } |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4234 | if (j != outputs.size()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4235 | continue; |
| 4236 | } |
| 4237 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4238 | if (!profile->canOpenNewIo()) { |
| 4239 | ALOGW("Max Output number %u already opened for this profile %s", |
| 4240 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 4241 | continue; |
| 4242 | } |
| 4243 | |
Eric Laurent | 83efe1c | 2017-07-09 16:51:08 -0700 | [diff] [blame] | 4244 | ALOGV("opening output for device %08x with params %s profile %p name %s", |
| 4245 | device, address.string(), profile.get(), profile->getName().string()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4246 | desc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4247 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4248 | status_t status = desc->open(nullptr, device, address, |
| 4249 | AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, &output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4250 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4251 | if (status == NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4252 | // Here is where the out_set_parameters() for card & device gets called |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4253 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4254 | char *param = audio_device_address_to_parameter(device, address); |
| 4255 | mpClientInterface->setParameters(output, String8(param)); |
| 4256 | free(param); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4257 | } |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 4258 | updateAudioProfiles(device, output, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4259 | if (!profile->hasValidAudioProfile()) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4260 | ALOGW("checkOutputsForDevice() missing param"); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4261 | desc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4262 | output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4263 | } else if (profile->hasDynamicAudioProfile()) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4264 | desc->close(); |
Phil Burk | 702b105 | 2016-03-02 16:38:26 -0800 | [diff] [blame] | 4265 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4266 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 4267 | profile->pickAudioProfile( |
| 4268 | config.sample_rate, config.channel_mask, config.format); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4269 | config.offload_info.sample_rate = config.sample_rate; |
| 4270 | config.offload_info.channel_mask = config.channel_mask; |
| 4271 | config.offload_info.format = config.format; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4272 | |
| 4273 | status_t status = desc->open(&config, device, address, AUDIO_STREAM_DEFAULT, |
| 4274 | AUDIO_OUTPUT_FLAG_NONE, &output); |
| 4275 | if (status != NO_ERROR) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4276 | output = AUDIO_IO_HANDLE_NONE; |
| 4277 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4278 | } |
| 4279 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4280 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4281 | addOutput(output, desc); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4282 | if (device_distinguishes_on_address(device) && address != "0") { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4283 | sp<AudioPolicyMix> policyMix; |
| 4284 | if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4285 | ALOGE("checkOutputsForDevice() cannot find policy for address %s", |
| 4286 | address.string()); |
| 4287 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4288 | policyMix->setOutput(desc); |
Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 4289 | desc->mPolicyMix = policyMix->getMix(); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4290 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 4291 | } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
| 4292 | hasPrimaryOutput()) { |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 4293 | // no duplicated output for direct outputs and |
| 4294 | // outputs used by dynamic policy mixes |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4295 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4296 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4297 | //TODO: configure audio effect output stage here |
| 4298 | |
| 4299 | // 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] | 4300 | sp<SwAudioOutputDescriptor> dupOutputDesc = |
| 4301 | new SwAudioOutputDescriptor(NULL, mpClientInterface); |
| 4302 | status_t status = dupOutputDesc->openDuplicating(mPrimaryOutput, desc, |
| 4303 | &duplicatedOutput); |
| 4304 | if (status == NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4305 | // add duplicated output descriptor |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4306 | addOutput(duplicatedOutput, dupOutputDesc); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4307 | } else { |
| 4308 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4309 | mPrimaryOutput->mIoHandle, output); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4310 | desc->close(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4311 | removeOutput(output); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4312 | nextAudioPortGeneration(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4313 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4314 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4315 | } |
| 4316 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4317 | } else { |
| 4318 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4319 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4320 | if (output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4321 | ALOGW("checkOutputsForDevice() could not open output for device %x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4322 | profiles.removeAt(profile_index); |
| 4323 | profile_index--; |
| 4324 | } else { |
| 4325 | outputs.add(output); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4326 | // Load digital format info only for digital devices |
| 4327 | if (audio_device_is_digital(device)) { |
| 4328 | devDesc->importAudioPort(profile); |
| 4329 | } |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 4330 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4331 | if (device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4332 | ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)", |
| 4333 | device, address.string()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4334 | setOutputDevice(desc, device, true/*force*/, 0/*delay*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4335 | NULL/*patch handle*/, address.string()); |
| 4336 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4337 | ALOGV("checkOutputsForDevice(): adding output %d", output); |
| 4338 | } |
| 4339 | } |
| 4340 | |
| 4341 | if (profiles.isEmpty()) { |
| 4342 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 4343 | return BAD_VALUE; |
| 4344 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4345 | } else { // Disconnect |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4346 | // check if one opened output is not needed any more after disconnecting one device |
| 4347 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4348 | desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4349 | if (!desc->isDuplicated()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4350 | // exact match on device |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4351 | if (device_distinguishes_on_address(device) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4352 | (desc->supportedDevices() == device)) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4353 | findIoHandlesByAddress(desc, device, address, outputs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4354 | } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4355 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", |
| 4356 | mOutputs.keyAt(i)); |
| 4357 | outputs.add(mOutputs.keyAt(i)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4358 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4359 | } |
| 4360 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4361 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4362 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4363 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 4364 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4365 | if (profile->supportDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4366 | ALOGV("checkOutputsForDevice(): " |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4367 | "clearing direct output profile %zu on module %s", |
| 4368 | j, hwModule->getName()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4369 | profile->clearAudioProfiles(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4370 | } |
| 4371 | } |
| 4372 | } |
| 4373 | } |
| 4374 | return NO_ERROR; |
| 4375 | } |
| 4376 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4377 | status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4378 | audio_policy_dev_state_t state, |
| 4379 | SortedVector<audio_io_handle_t>& inputs, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4380 | const String8& address) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4381 | { |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4382 | audio_devices_t device = devDesc->type(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4383 | sp<AudioInputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4384 | |
| 4385 | if (audio_device_is_digital(device)) { |
| 4386 | // erase all current sample rates, formats and channel masks |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 4387 | devDesc->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4388 | } |
| 4389 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4390 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 4391 | // first list already open inputs that can be routed to this device |
| 4392 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4393 | desc = mInputs.valueAt(input_index); |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4394 | if (desc->mProfile->supportDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4395 | ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index)); |
| 4396 | inputs.add(mInputs.keyAt(input_index)); |
| 4397 | } |
| 4398 | } |
| 4399 | |
| 4400 | // then look for input profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4401 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4402 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4403 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4404 | profile_index < hwModule->getInputProfiles().size(); |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4405 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4406 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4407 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4408 | if (profile->supportDevice(device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4409 | if (!device_distinguishes_on_address(device) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4410 | profile->supportDeviceAddress(address)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4411 | profiles.add(profile); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4412 | ALOGV("checkInputsForDevice(): adding profile %zu from module %s", |
| 4413 | profile_index, hwModule->getName()); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4414 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4415 | } |
| 4416 | } |
| 4417 | } |
| 4418 | |
| 4419 | if (profiles.isEmpty() && inputs.isEmpty()) { |
| 4420 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 4421 | return BAD_VALUE; |
| 4422 | } |
| 4423 | |
| 4424 | // open inputs for matching profiles if needed. Direct inputs are also opened to |
| 4425 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 4426 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { |
| 4427 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4428 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4429 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4430 | // nothing to do if one input is already opened for this profile |
| 4431 | size_t input_index; |
| 4432 | for (input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4433 | desc = mInputs.valueAt(input_index); |
| 4434 | if (desc->mProfile == profile) { |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4435 | if (audio_device_is_digital(device)) { |
| 4436 | devDesc->importAudioPort(profile); |
| 4437 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4438 | break; |
| 4439 | } |
| 4440 | } |
| 4441 | if (input_index != mInputs.size()) { |
| 4442 | continue; |
| 4443 | } |
| 4444 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4445 | if (!profile->canOpenNewIo()) { |
| 4446 | ALOGW("Max Input number %u already opened for this profile %s", |
| 4447 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 4448 | continue; |
| 4449 | } |
| 4450 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4451 | desc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4452 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4453 | status_t status = desc->open(nullptr, |
| 4454 | device, |
| 4455 | address, |
| 4456 | AUDIO_SOURCE_MIC, |
| 4457 | AUDIO_INPUT_FLAG_NONE, |
| 4458 | &input); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4459 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4460 | if (status == NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4461 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4462 | char *param = audio_device_address_to_parameter(device, address); |
| 4463 | mpClientInterface->setParameters(input, String8(param)); |
| 4464 | free(param); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4465 | } |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 4466 | updateAudioProfiles(device, input, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4467 | if (!profile->hasValidAudioProfile()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4468 | ALOGW("checkInputsForDevice() direct input missing param"); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4469 | desc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4470 | input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4471 | } |
| 4472 | |
| 4473 | if (input != 0) { |
| 4474 | addInput(input, desc); |
| 4475 | } |
| 4476 | } // endif input != 0 |
| 4477 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4478 | if (input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4479 | ALOGW("checkInputsForDevice() could not open input for device 0x%X", device); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4480 | profiles.removeAt(profile_index); |
| 4481 | profile_index--; |
| 4482 | } else { |
| 4483 | inputs.add(input); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4484 | if (audio_device_is_digital(device)) { |
| 4485 | devDesc->importAudioPort(profile); |
| 4486 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4487 | ALOGV("checkInputsForDevice(): adding input %d", input); |
| 4488 | } |
| 4489 | } // end scan profiles |
| 4490 | |
| 4491 | if (profiles.isEmpty()) { |
| 4492 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 4493 | return BAD_VALUE; |
| 4494 | } |
| 4495 | } else { |
| 4496 | // Disconnect |
| 4497 | // check if one opened input is not needed any more after disconnecting one device |
| 4498 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4499 | desc = mInputs.valueAt(input_index); |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4500 | if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4501 | ALOGV("checkInputsForDevice(): disconnecting adding input %d", |
| 4502 | mInputs.keyAt(input_index)); |
| 4503 | inputs.add(mInputs.keyAt(input_index)); |
| 4504 | } |
| 4505 | } |
| 4506 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4507 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4508 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4509 | profile_index < hwModule->getInputProfiles().size(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4510 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4511 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4512 | if (profile->supportDevice(device)) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4513 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %s", |
| 4514 | profile_index, hwModule->getName()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4515 | profile->clearAudioProfiles(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4516 | } |
| 4517 | } |
| 4518 | } |
| 4519 | } // end disconnect |
| 4520 | |
| 4521 | return NO_ERROR; |
| 4522 | } |
| 4523 | |
| 4524 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4525 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4526 | { |
| 4527 | ALOGV("closeOutput(%d)", output); |
| 4528 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4529 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4530 | if (outputDesc == NULL) { |
| 4531 | ALOGW("closeOutput() unknown output %d", output); |
| 4532 | return; |
| 4533 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4534 | mPolicyMixes.closeOutput(outputDesc); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4535 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4536 | // look for duplicated outputs connected to the output being removed. |
| 4537 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4538 | sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4539 | if (dupOutputDesc->isDuplicated() && |
| 4540 | (dupOutputDesc->mOutput1 == outputDesc || |
| 4541 | dupOutputDesc->mOutput2 == outputDesc)) { |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4542 | sp<SwAudioOutputDescriptor> outputDesc2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4543 | if (dupOutputDesc->mOutput1 == outputDesc) { |
| 4544 | outputDesc2 = dupOutputDesc->mOutput2; |
| 4545 | } else { |
| 4546 | outputDesc2 = dupOutputDesc->mOutput1; |
| 4547 | } |
| 4548 | // As all active tracks on duplicated output will be deleted, |
| 4549 | // and as they were also referenced on the other output, the reference |
| 4550 | // count for their stream type must be adjusted accordingly on |
| 4551 | // the other output. |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4552 | bool wasActive = outputDesc2->isActive(); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4553 | for (int j = 0; j < AUDIO_STREAM_CNT; j++) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4554 | int refCount = dupOutputDesc->mRefCount[j]; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4555 | outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4556 | } |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4557 | // stop() will be a no op if the output is still active but is needed in case all |
| 4558 | // active streams refcounts where cleared above |
| 4559 | if (wasActive) { |
| 4560 | outputDesc2->stop(); |
| 4561 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4562 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); |
| 4563 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); |
| 4564 | |
| 4565 | mpClientInterface->closeOutput(duplicatedOutput); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4566 | removeOutput(duplicatedOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4567 | } |
| 4568 | } |
| 4569 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4570 | nextAudioPortGeneration(); |
| 4571 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4572 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->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 | outputDesc->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4581 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4582 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4583 | mPreviousOutputs = mOutputs; |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4584 | } |
| 4585 | |
| 4586 | void AudioPolicyManager::closeInput(audio_io_handle_t input) |
| 4587 | { |
| 4588 | ALOGV("closeInput(%d)", input); |
| 4589 | |
| 4590 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
| 4591 | if (inputDesc == NULL) { |
| 4592 | ALOGW("closeInput() unknown input %d", input); |
| 4593 | return; |
| 4594 | } |
| 4595 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4596 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4597 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4598 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4599 | if (index >= 0) { |
| 4600 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4601 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4602 | mAudioPatches.removeItemsAt(index); |
| 4603 | mpClientInterface->onAudioPatchListUpdate(); |
| 4604 | } |
| 4605 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4606 | inputDesc->close(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4607 | mInputs.removeItem(input); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4608 | } |
| 4609 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4610 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice( |
| 4611 | audio_devices_t device, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4612 | const SwAudioOutputCollection& openOutputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4613 | { |
| 4614 | SortedVector<audio_io_handle_t> outputs; |
| 4615 | |
| 4616 | ALOGVV("getOutputsForDevice() device %04x", device); |
| 4617 | for (size_t i = 0; i < openOutputs.size(); i++) { |
Eric Laurent | 37ddbb4 | 2016-08-10 16:19:14 -0700 | [diff] [blame] | 4618 | ALOGVV("output %zu isDuplicated=%d device=%04x", |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4619 | i, openOutputs.valueAt(i)->isDuplicated(), |
| 4620 | openOutputs.valueAt(i)->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4621 | if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) { |
| 4622 | ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i)); |
| 4623 | outputs.add(openOutputs.keyAt(i)); |
| 4624 | } |
| 4625 | } |
| 4626 | return outputs; |
| 4627 | } |
| 4628 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4629 | bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4630 | SortedVector<audio_io_handle_t>& outputs2) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4631 | { |
| 4632 | if (outputs1.size() != outputs2.size()) { |
| 4633 | return false; |
| 4634 | } |
| 4635 | for (size_t i = 0; i < outputs1.size(); i++) { |
| 4636 | if (outputs1[i] != outputs2[i]) { |
| 4637 | return false; |
| 4638 | } |
| 4639 | } |
| 4640 | return true; |
| 4641 | } |
| 4642 | |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 4643 | void AudioPolicyManager::checkForDeviceAndOutputChanges() |
| 4644 | { |
| 4645 | checkForDeviceAndOutputChanges([](){ return false; }); |
| 4646 | } |
| 4647 | |
| 4648 | void AudioPolicyManager::checkForDeviceAndOutputChanges(std::function<bool()> onOutputsChecked) |
| 4649 | { |
| 4650 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP |
| 4651 | // output is suspended before any tracks are moved to it |
| 4652 | checkA2dpSuspend(); |
| 4653 | checkOutputForAllStrategies(); |
| 4654 | if (onOutputsChecked()) checkA2dpSuspend(); |
| 4655 | updateDevicesAndOutputs(); |
| 4656 | } |
| 4657 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4658 | void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4659 | { |
| 4660 | audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/); |
| 4661 | audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 4662 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs); |
| 4663 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs); |
| 4664 | |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4665 | // also take into account external policy-related changes: add all outputs which are |
| 4666 | // associated with policies in the "before" and "after" output vectors |
| 4667 | ALOGVV("checkOutputForStrategy(): policy related outputs"); |
| 4668 | for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4669 | const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4670 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4671 | srcOutputs.add(desc->mIoHandle); |
| 4672 | ALOGVV(" previous outputs: adding %d", desc->mIoHandle); |
| 4673 | } |
| 4674 | } |
| 4675 | for (size_t i = 0 ; i < mOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4676 | const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4677 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4678 | dstOutputs.add(desc->mIoHandle); |
| 4679 | ALOGVV(" new outputs: adding %d", desc->mIoHandle); |
| 4680 | } |
| 4681 | } |
| 4682 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4683 | if (!vectorsEqual(srcOutputs,dstOutputs)) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 4684 | // get maximum latency of all source outputs to determine the minimum mute time guaranteeing |
| 4685 | // audio from invalidated tracks will be rendered when unmuting |
| 4686 | uint32_t maxLatency = 0; |
| 4687 | for (audio_io_handle_t srcOut : srcOutputs) { |
| 4688 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); |
| 4689 | if (desc != 0 && maxLatency < desc->latency()) { |
| 4690 | maxLatency = desc->latency(); |
| 4691 | } |
| 4692 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4693 | ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d", |
| 4694 | strategy, srcOutputs[0], dstOutputs[0]); |
| 4695 | // mute strategy while moving tracks from one output to another |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4696 | for (audio_io_handle_t srcOut : srcOutputs) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 4697 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); |
| 4698 | if (desc != 0 && isStrategyActive(desc, strategy)) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4699 | setStrategyMute(strategy, true, desc); |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 4700 | setStrategyMute(strategy, false, desc, maxLatency * LATENCY_MUTE_FACTOR, newDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4701 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4702 | sp<AudioSourceDescriptor> source = |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4703 | getSourceForStrategyOnOutput(srcOut, strategy); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4704 | if (source != 0){ |
| 4705 | connectAudioSource(source); |
| 4706 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4707 | } |
| 4708 | |
| 4709 | // Move effects associated to this strategy from previous output to new output |
| 4710 | if (strategy == STRATEGY_MEDIA) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4711 | selectOutputForMusicEffects(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4712 | } |
| 4713 | // Move tracks associated to this strategy from previous output to new output |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4714 | for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4715 | if (getStrategy((audio_stream_type_t)i) == strategy) { |
| 4716 | mpClientInterface->invalidateStream((audio_stream_type_t)i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4717 | } |
| 4718 | } |
| 4719 | } |
| 4720 | } |
| 4721 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4722 | void AudioPolicyManager::checkOutputForAllStrategies() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4723 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4724 | 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] | 4725 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4726 | checkOutputForStrategy(STRATEGY_PHONE); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4727 | 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] | 4728 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4729 | checkOutputForStrategy(STRATEGY_SONIFICATION); |
| 4730 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4731 | checkOutputForStrategy(STRATEGY_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4732 | checkOutputForStrategy(STRATEGY_MEDIA); |
| 4733 | checkOutputForStrategy(STRATEGY_DTMF); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4734 | checkOutputForStrategy(STRATEGY_REROUTING); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4735 | } |
| 4736 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4737 | void AudioPolicyManager::checkA2dpSuspend() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4738 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4739 | audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput(); |
Aniket Kumar Lata | a8ee996 | 2018-01-31 20:24:23 -0800 | [diff] [blame] | 4740 | if (a2dpOutput == 0 || mOutputs.isA2dpOffloadedOnPrimary()) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4741 | mA2dpSuspended = false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4742 | return; |
| 4743 | } |
| 4744 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4745 | bool isScoConnected = |
Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 4746 | ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET & |
| 4747 | ~AUDIO_DEVICE_BIT_IN) != 0) || |
| 4748 | ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0); |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4749 | |
| 4750 | // if suspended, restore A2DP output if: |
| 4751 | // ((SCO device is NOT connected) || |
| 4752 | // ((forced usage communication is NOT SCO) && (forced usage for record is NOT SCO) && |
| 4753 | // (phone state is NOT in call) && (phone state is NOT ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4754 | // |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4755 | // if not suspended, suspend A2DP output if: |
| 4756 | // (SCO device is connected) && |
| 4757 | // ((forced usage for communication is SCO) || (forced usage for record is SCO) || |
| 4758 | // ((phone state is in call) || (phone state is ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4759 | // |
| 4760 | if (mA2dpSuspended) { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4761 | if (!isScoConnected || |
| 4762 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != |
| 4763 | AUDIO_POLICY_FORCE_BT_SCO) && |
| 4764 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != |
| 4765 | AUDIO_POLICY_FORCE_BT_SCO) && |
| 4766 | (mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4767 | (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4768 | |
| 4769 | mpClientInterface->restoreOutput(a2dpOutput); |
| 4770 | mA2dpSuspended = false; |
| 4771 | } |
| 4772 | } else { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4773 | if (isScoConnected && |
| 4774 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == |
| 4775 | AUDIO_POLICY_FORCE_BT_SCO) || |
| 4776 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == |
| 4777 | AUDIO_POLICY_FORCE_BT_SCO) || |
| 4778 | (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4779 | (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4780 | |
| 4781 | mpClientInterface->suspendOutput(a2dpOutput); |
| 4782 | mA2dpSuspended = true; |
| 4783 | } |
| 4784 | } |
| 4785 | } |
| 4786 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4787 | audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
| 4788 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4789 | { |
| 4790 | audio_devices_t device = AUDIO_DEVICE_NONE; |
| 4791 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4792 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4793 | if (index >= 0) { |
| 4794 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4795 | if (patchDesc->mUid != mUidCached) { |
| 4796 | ALOGV("getNewOutputDevice() device %08x forced by patch %d", |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4797 | outputDesc->device(), outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4798 | return outputDesc->device(); |
| 4799 | } |
| 4800 | } |
| 4801 | |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 4802 | // Check if an explicit routing request exists for an active stream on this output and |
| 4803 | // use it in priority before any other rule |
| 4804 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
| 4805 | if (outputDesc->isStreamActive((audio_stream_type_t)stream)) { |
| 4806 | audio_devices_t forcedDevice = |
| 4807 | mOutputRoutes.getActiveDeviceForStream( |
| 4808 | (audio_stream_type_t)stream, mAvailableOutputDevices); |
| 4809 | |
| 4810 | if (forcedDevice != AUDIO_DEVICE_NONE) { |
| 4811 | return forcedDevice; |
| 4812 | } |
| 4813 | } |
| 4814 | } |
| 4815 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4816 | // 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] | 4817 | // 1: the strategy enforced audible is active and enforced on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4818 | // use device for strategy enforced audible |
| 4819 | // 2: we are in call or the strategy phone is active on the output: |
| 4820 | // use device for strategy phone |
Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4821 | // 3: the strategy sonification is active on the output: |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4822 | // use device for strategy sonification |
Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4823 | // 4: the strategy for enforced audible is active but not enforced on the output: |
| 4824 | // use the device for strategy enforced audible |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4825 | // 5: the strategy accessibility is active on the output: |
| 4826 | // use device for strategy accessibility |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4827 | // 6: the strategy "respectful" sonification is active on the output: |
| 4828 | // use device for strategy "respectful" sonification |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4829 | // 7: the strategy media is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4830 | // use device for strategy media |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4831 | // 8: the strategy DTMF is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4832 | // use device for strategy DTMF |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4833 | // 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] | 4834 | // use device for strategy t-t-s |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 4835 | |
| 4836 | // FIXME: extend use of isStrategyActiveOnSameModule() to all strategies |
| 4837 | // with a refined rule considering mutually exclusive devices (using same backend) |
| 4838 | // as opposed to all streams on the same audio HAL module. |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4839 | if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4840 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4841 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
| 4842 | } else if (isInCall() || |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 4843 | isStrategyActiveOnSameModule(outputDesc, STRATEGY_PHONE)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4844 | device = getDeviceForStrategy(STRATEGY_PHONE, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4845 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4846 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache); |
Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4847 | } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) { |
| 4848 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4849 | } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) { |
| 4850 | device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4851 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4852 | device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4853 | } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4854 | device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4855 | } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4856 | device = getDeviceForStrategy(STRATEGY_DTMF, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4857 | } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4858 | device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4859 | } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4860 | device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4861 | } |
| 4862 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4863 | ALOGV("getNewOutputDevice() selected device %x", device); |
| 4864 | return device; |
| 4865 | } |
| 4866 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4867 | audio_devices_t AudioPolicyManager::getNewInputDevice(const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4868 | { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4869 | audio_devices_t device = AUDIO_DEVICE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4870 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4871 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4872 | if (index >= 0) { |
| 4873 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4874 | if (patchDesc->mUid != mUidCached) { |
| 4875 | ALOGV("getNewInputDevice() device %08x forced by patch %d", |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4876 | inputDesc->mDevice, inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4877 | return inputDesc->mDevice; |
| 4878 | } |
| 4879 | } |
| 4880 | |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 4881 | // If we are not in call and no client is active on this input, this methods returns |
| 4882 | // 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] | 4883 | audio_source_t source = inputDesc->getHighestPrioritySource(true /*activeOnly*/); |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 4884 | if (source == AUDIO_SOURCE_DEFAULT && isInCall()) { |
| 4885 | source = AUDIO_SOURCE_VOICE_COMMUNICATION; |
| 4886 | } |
| 4887 | if (source != AUDIO_SOURCE_DEFAULT) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4888 | device = getDeviceAndMixForInputSource(source); |
| 4889 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4890 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4891 | return device; |
| 4892 | } |
| 4893 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4894 | bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1, |
| 4895 | audio_stream_type_t stream2) { |
Jean-Michel Trivi | 99bb2f9 | 2016-11-23 15:52:07 -0800 | [diff] [blame] | 4896 | return (stream1 == stream2); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4897 | } |
| 4898 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4899 | uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4900 | return (uint32_t)getStrategy(stream); |
| 4901 | } |
| 4902 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4903 | audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4904 | // By checking the range of stream before calling getStrategy, we avoid |
| 4905 | // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE |
| 4906 | // 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] | 4907 | if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4908 | return AUDIO_DEVICE_NONE; |
| 4909 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4910 | audio_devices_t devices = AUDIO_DEVICE_NONE; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4911 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 4912 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4913 | continue; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4914 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4915 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4916 | audio_devices_t curDevices = |
Eric Laurent | 447a87b | 2016-07-07 17:32:10 -0700 | [diff] [blame] | 4917 | getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/); |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4918 | for (audio_io_handle_t output : getOutputsForDevice(curDevices, mOutputs)) { |
| 4919 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4920 | if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4921 | curDevices |= outputDesc->device(); |
| 4922 | } |
| 4923 | } |
| 4924 | devices |= curDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4925 | } |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4926 | |
| 4927 | /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it |
| 4928 | and doesn't really need to.*/ |
| 4929 | if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) { |
| 4930 | devices |= AUDIO_DEVICE_OUT_SPEAKER; |
| 4931 | devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
| 4932 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4933 | return devices; |
| 4934 | } |
| 4935 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4936 | routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const |
| 4937 | { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4938 | 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] | 4939 | return mEngine->getStrategyForStream(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4940 | } |
| 4941 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4942 | uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) { |
| 4943 | // flags to strategy mapping |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4944 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 4945 | return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER; |
| 4946 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4947 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 4948 | return (uint32_t) STRATEGY_ENFORCED_AUDIBLE; |
| 4949 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4950 | // usage to strategy mapping |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4951 | return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage)); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4952 | } |
| 4953 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4954 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4955 | switch(stream) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4956 | case AUDIO_STREAM_MUSIC: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4957 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
| 4958 | updateDevicesAndOutputs(); |
| 4959 | break; |
| 4960 | default: |
| 4961 | break; |
| 4962 | } |
| 4963 | } |
| 4964 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4965 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 4966 | |
| 4967 | // skip beacon mute management if a dedicated TTS output is available |
| 4968 | if (mTtsOutputAvailable) { |
| 4969 | return 0; |
| 4970 | } |
| 4971 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4972 | switch(event) { |
| 4973 | case STARTING_OUTPUT: |
| 4974 | mBeaconMuteRefCount++; |
| 4975 | break; |
| 4976 | case STOPPING_OUTPUT: |
| 4977 | if (mBeaconMuteRefCount > 0) { |
| 4978 | mBeaconMuteRefCount--; |
| 4979 | } |
| 4980 | break; |
| 4981 | case STARTING_BEACON: |
| 4982 | mBeaconPlayingRefCount++; |
| 4983 | break; |
| 4984 | case STOPPING_BEACON: |
| 4985 | if (mBeaconPlayingRefCount > 0) { |
| 4986 | mBeaconPlayingRefCount--; |
| 4987 | } |
| 4988 | break; |
| 4989 | } |
| 4990 | |
| 4991 | if (mBeaconMuteRefCount > 0) { |
| 4992 | // any playback causes beacon to be muted |
| 4993 | return setBeaconMute(true); |
| 4994 | } else { |
| 4995 | // no other playback: unmute when beacon starts playing, mute when it stops |
| 4996 | return setBeaconMute(mBeaconPlayingRefCount == 0); |
| 4997 | } |
| 4998 | } |
| 4999 | |
| 5000 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { |
| 5001 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", |
| 5002 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); |
| 5003 | // keep track of muted state to avoid repeating mute/unmute operations |
| 5004 | if (mBeaconMuted != mute) { |
| 5005 | // mute/unmute AUDIO_STREAM_TTS on all outputs |
| 5006 | ALOGV("\t muting %d", mute); |
| 5007 | uint32_t maxLatency = 0; |
| 5008 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5009 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5010 | setStreamMute(AUDIO_STREAM_TTS, mute/*on*/, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5011 | desc, |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5012 | 0 /*delay*/, AUDIO_DEVICE_NONE); |
| 5013 | const uint32_t latency = desc->latency() * 2; |
| 5014 | if (latency > maxLatency) { |
| 5015 | maxLatency = latency; |
| 5016 | } |
| 5017 | } |
| 5018 | mBeaconMuted = mute; |
| 5019 | return maxLatency; |
| 5020 | } |
| 5021 | return 0; |
| 5022 | } |
| 5023 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5024 | audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5025 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5026 | { |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 5027 | // Check if an explicit routing request exists for a stream type corresponding to the |
| 5028 | // specified strategy and use it in priority over default routing rules. |
| 5029 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
| 5030 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
| 5031 | audio_devices_t forcedDevice = |
| 5032 | mOutputRoutes.getActiveDeviceForStream( |
| 5033 | (audio_stream_type_t)stream, mAvailableOutputDevices); |
| 5034 | if (forcedDevice != AUDIO_DEVICE_NONE) { |
| 5035 | return forcedDevice; |
| 5036 | } |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 5037 | } |
| 5038 | } |
| 5039 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5040 | if (fromCache) { |
| 5041 | ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x", |
| 5042 | strategy, mDeviceForStrategy[strategy]); |
| 5043 | return mDeviceForStrategy[strategy]; |
| 5044 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5045 | return mEngine->getDeviceForStrategy(strategy); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5046 | } |
| 5047 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5048 | void AudioPolicyManager::updateDevicesAndOutputs() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5049 | { |
| 5050 | for (int i = 0; i < NUM_STRATEGIES; i++) { |
| 5051 | mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
| 5052 | } |
| 5053 | mPreviousOutputs = mOutputs; |
| 5054 | } |
| 5055 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5056 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5057 | audio_devices_t prevDevice, |
| 5058 | uint32_t delayMs) |
| 5059 | { |
| 5060 | // mute/unmute strategies using an incompatible device combination |
| 5061 | // if muting, wait for the audio in pcm buffer to be drained before proceeding |
| 5062 | // if unmuting, unmute only after the specified delay |
| 5063 | if (outputDesc->isDuplicated()) { |
| 5064 | return 0; |
| 5065 | } |
| 5066 | |
| 5067 | uint32_t muteWaitMs = 0; |
| 5068 | audio_devices_t device = outputDesc->device(); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5069 | bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5070 | |
| 5071 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
| 5072 | audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5073 | curDevice = curDevice & outputDesc->supportedDevices(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5074 | bool mute = shouldMute && (curDevice & device) && (curDevice != device); |
| 5075 | bool doMute = false; |
| 5076 | |
| 5077 | if (mute && !outputDesc->mStrategyMutedByDevice[i]) { |
| 5078 | doMute = true; |
| 5079 | outputDesc->mStrategyMutedByDevice[i] = true; |
| 5080 | } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){ |
| 5081 | doMute = true; |
| 5082 | outputDesc->mStrategyMutedByDevice[i] = false; |
| 5083 | } |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5084 | if (doMute) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5085 | for (size_t j = 0; j < mOutputs.size(); j++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5086 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5087 | // skip output if it does not share any device with current output |
| 5088 | if ((desc->supportedDevices() & outputDesc->supportedDevices()) |
| 5089 | == AUDIO_DEVICE_NONE) { |
| 5090 | continue; |
| 5091 | } |
Eric Laurent | 37ddbb4 | 2016-08-10 16:19:14 -0700 | [diff] [blame] | 5092 | ALOGVV("checkDeviceMuteStrategies() %s strategy %zu (curDevice %04x)", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5093 | mute ? "muting" : "unmuting", i, curDevice); |
| 5094 | setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5095 | if (isStrategyActive(desc, (routing_strategy)i)) { |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5096 | if (mute) { |
| 5097 | // FIXME: should not need to double latency if volume could be applied |
| 5098 | // immediately by the audioflinger mixer. We must account for the delay |
| 5099 | // between now and the next time the audioflinger thread for this output |
| 5100 | // will process a buffer (which corresponds to one buffer size, |
| 5101 | // usually 1/2 or 1/4 of the latency). |
| 5102 | if (muteWaitMs < desc->latency() * 2) { |
| 5103 | muteWaitMs = desc->latency() * 2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5104 | } |
| 5105 | } |
| 5106 | } |
| 5107 | } |
| 5108 | } |
| 5109 | } |
| 5110 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5111 | // temporary mute output if device selection changes to avoid volume bursts due to |
| 5112 | // different per device volumes |
| 5113 | if (outputDesc->isActive() && (device != prevDevice)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5114 | uint32_t tempMuteWaitMs = outputDesc->latency() * 2; |
| 5115 | // temporary mute duration is conservatively set to 4 times the reported latency |
| 5116 | uint32_t tempMuteDurationMs = outputDesc->latency() * 4; |
| 5117 | if (muteWaitMs < tempMuteWaitMs) { |
| 5118 | muteWaitMs = tempMuteWaitMs; |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5119 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5120 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5121 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5122 | if (isStrategyActive(outputDesc, (routing_strategy)i)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5123 | // make sure that we do not start the temporary mute period too early in case of |
| 5124 | // delayed device change |
| 5125 | setStrategyMute((routing_strategy)i, true, outputDesc, delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5126 | setStrategyMute((routing_strategy)i, false, outputDesc, |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5127 | delayMs + tempMuteDurationMs, device); |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5128 | } |
| 5129 | } |
| 5130 | } |
| 5131 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5132 | // wait for the PCM output buffers to empty before proceeding with the rest of the command |
| 5133 | if (muteWaitMs > delayMs) { |
| 5134 | muteWaitMs -= delayMs; |
| 5135 | usleep(muteWaitMs * 1000); |
| 5136 | return muteWaitMs; |
| 5137 | } |
| 5138 | return 0; |
| 5139 | } |
| 5140 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5141 | uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5142 | audio_devices_t device, |
| 5143 | bool force, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5144 | int delayMs, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5145 | audio_patch_handle_t *patchHandle, |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5146 | const char *address, |
| 5147 | bool requiresMuteCheck) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5148 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5149 | ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5150 | AudioParameter param; |
| 5151 | uint32_t muteWaitMs; |
| 5152 | |
| 5153 | if (outputDesc->isDuplicated()) { |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5154 | muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs, |
| 5155 | nullptr /* patchHandle */, nullptr /* address */, requiresMuteCheck); |
| 5156 | muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs, |
| 5157 | nullptr /* patchHandle */, nullptr /* address */, requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5158 | return muteWaitMs; |
| 5159 | } |
| 5160 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current |
| 5161 | // output profile |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5162 | if ((device != AUDIO_DEVICE_NONE) && |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5163 | ((device & outputDesc->supportedDevices()) == AUDIO_DEVICE_NONE)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5164 | return 0; |
| 5165 | } |
| 5166 | |
| 5167 | // filter devices according to output selected |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5168 | device = (audio_devices_t)(device & outputDesc->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5169 | |
| 5170 | audio_devices_t prevDevice = outputDesc->mDevice; |
| 5171 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 5172 | ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5173 | |
| 5174 | if (device != AUDIO_DEVICE_NONE) { |
| 5175 | outputDesc->mDevice = device; |
| 5176 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5177 | |
| 5178 | // if the outputs are not materially active, there is no need to mute. |
| 5179 | if (requiresMuteCheck) { |
| 5180 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs); |
| 5181 | } else { |
| 5182 | ALOGV("%s: suppressing checkDeviceMuteStrategies", __func__); |
| 5183 | muteWaitMs = 0; |
| 5184 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5185 | |
| 5186 | // Do not change the routing if: |
Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 5187 | // the requested device is AUDIO_DEVICE_NONE |
| 5188 | // OR the requested device is the same as current device |
| 5189 | // AND force is not specified |
| 5190 | // AND the output is connected by a valid audio patch. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5191 | // Doing this check here allows the caller to call setOutputDevice() without conditions |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 5192 | if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && |
| 5193 | !force && |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 5194 | outputDesc->getPatchHandle() != 0) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5195 | ALOGV("setOutputDevice() setting same device 0x%04x or null device", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5196 | return muteWaitMs; |
| 5197 | } |
| 5198 | |
| 5199 | ALOGV("setOutputDevice() changing device"); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5200 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5201 | // do the routing |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5202 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5203 | resetOutputDevice(outputDesc, delayMs, NULL); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5204 | } else { |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 5205 | DeviceVector deviceList; |
| 5206 | if ((address == NULL) || (strlen(address) == 0)) { |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 5207 | deviceList = mAvailableOutputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 5208 | } else { |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 5209 | sp<DeviceDescriptor> deviceDesc = mAvailableOutputDevices.getDevice( |
| 5210 | device, String8(address)); |
| 5211 | if (deviceDesc) deviceList.add(deviceDesc); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 5212 | } |
| 5213 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5214 | if (!deviceList.isEmpty()) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5215 | PatchBuilder patchBuilder; |
| 5216 | patchBuilder.addSource(outputDesc); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5217 | 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] | 5218 | patchBuilder.addSink(deviceList.itemAt(i)); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5219 | } |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5220 | installPatch(__func__, patchHandle, outputDesc.get(), patchBuilder.patch(), delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5221 | } |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 5222 | |
| 5223 | // inform all input as well |
| 5224 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 5225 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5226 | if (!is_virtual_input_device(inputDescriptor->mDevice)) { |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 5227 | AudioParameter inputCmd = AudioParameter(); |
| 5228 | ALOGV("%s: inform input %d of device:%d", __func__, |
| 5229 | inputDescriptor->mIoHandle, device); |
| 5230 | inputCmd.addInt(String8(AudioParameter::keyRouting),device); |
| 5231 | mpClientInterface->setParameters(inputDescriptor->mIoHandle, |
| 5232 | inputCmd.toString(), |
| 5233 | delayMs); |
| 5234 | } |
| 5235 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5236 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5237 | |
| 5238 | // update stream volumes according to new device |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5239 | applyStreamVolumes(outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5240 | |
| 5241 | return muteWaitMs; |
| 5242 | } |
| 5243 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5244 | status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5245 | int delayMs, |
| 5246 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5247 | { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5248 | ssize_t index; |
| 5249 | if (patchHandle) { |
| 5250 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 5251 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 5252 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5253 | } |
| 5254 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5255 | return INVALID_OPERATION; |
| 5256 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5257 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 5258 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5259 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 5260 | outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5261 | removeAudioPatch(patchDesc->mHandle); |
| 5262 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5263 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5264 | return status; |
| 5265 | } |
| 5266 | |
| 5267 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, |
| 5268 | audio_devices_t device, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5269 | bool force, |
| 5270 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5271 | { |
| 5272 | status_t status = NO_ERROR; |
| 5273 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5274 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5275 | if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) { |
| 5276 | inputDesc->mDevice = device; |
| 5277 | |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 5278 | DeviceVector deviceList = mAvailableInputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5279 | if (!deviceList.isEmpty()) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5280 | PatchBuilder patchBuilder; |
| 5281 | patchBuilder.addSink(inputDesc, |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 5282 | // AUDIO_SOURCE_HOTWORD is for internal use only: |
| 5283 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5284 | [inputDesc](const PatchBuilder::mix_usecase_t& usecase) { |
| 5285 | auto result = usecase; |
| 5286 | if (result.source == AUDIO_SOURCE_HOTWORD && !inputDesc->isSoundTrigger()) { |
| 5287 | result.source = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 5288 | } |
| 5289 | return result; }). |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5290 | //only one input device for now |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5291 | addSource(deviceList.itemAt(0)); |
| 5292 | status = installPatch(__func__, patchHandle, inputDesc.get(), patchBuilder.patch(), 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5293 | } |
| 5294 | } |
| 5295 | return status; |
| 5296 | } |
| 5297 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5298 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, |
| 5299 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5300 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5301 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5302 | ssize_t index; |
| 5303 | if (patchHandle) { |
| 5304 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 5305 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 5306 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5307 | } |
| 5308 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5309 | return INVALID_OPERATION; |
| 5310 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5311 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 5312 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5313 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 5314 | inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5315 | removeAudioPatch(patchDesc->mHandle); |
| 5316 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5317 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5318 | return status; |
| 5319 | } |
| 5320 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 5321 | sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5322 | const String8& address, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5323 | uint32_t& samplingRate, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5324 | audio_format_t& format, |
| 5325 | audio_channel_mask_t& channelMask, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5326 | audio_input_flags_t flags) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5327 | { |
| 5328 | // Choose an input profile based on the requested capture parameters: select the first available |
| 5329 | // profile supporting all requested parameters. |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5330 | // |
| 5331 | // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return |
| 5332 | // the best matching profile, not the first one. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5333 | |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5334 | sp<IOProfile> firstInexact; |
| 5335 | uint32_t updatedSamplingRate = 0; |
| 5336 | audio_format_t updatedFormat = AUDIO_FORMAT_INVALID; |
| 5337 | audio_channel_mask_t updatedChannelMask = AUDIO_CHANNEL_INVALID; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 5338 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 5339 | for (const auto& profile : hwModule->getInputProfiles()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5340 | // profile->log(); |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5341 | //updatedFormat = format; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5342 | if (profile->isCompatibleProfile(device, address, samplingRate, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5343 | &samplingRate /*updatedSamplingRate*/, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5344 | format, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5345 | &format, /*updatedFormat*/ |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5346 | channelMask, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5347 | &channelMask /*updatedChannelMask*/, |
| 5348 | // FIXME ugly cast |
| 5349 | (audio_output_flags_t) flags, |
| 5350 | true /*exactMatchRequiredForInputFlags*/)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5351 | return profile; |
| 5352 | } |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5353 | if (firstInexact == nullptr && profile->isCompatibleProfile(device, address, |
| 5354 | samplingRate, |
| 5355 | &updatedSamplingRate, |
| 5356 | format, |
| 5357 | &updatedFormat, |
| 5358 | channelMask, |
| 5359 | &updatedChannelMask, |
| 5360 | // FIXME ugly cast |
| 5361 | (audio_output_flags_t) flags, |
| 5362 | false /*exactMatchRequiredForInputFlags*/)) { |
| 5363 | firstInexact = profile; |
| 5364 | } |
| 5365 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5366 | } |
| 5367 | } |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5368 | if (firstInexact != nullptr) { |
| 5369 | samplingRate = updatedSamplingRate; |
| 5370 | format = updatedFormat; |
| 5371 | channelMask = updatedChannelMask; |
| 5372 | return firstInexact; |
| 5373 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5374 | return NULL; |
| 5375 | } |
| 5376 | |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5377 | |
| 5378 | audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5379 | AudioMix **policyMix) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5380 | { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 5381 | audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
| 5382 | audio_devices_t selectedDeviceFromMix = |
| 5383 | mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5384 | |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 5385 | if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) { |
| 5386 | return selectedDeviceFromMix; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5387 | } |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5388 | return getDeviceForInputSource(inputSource); |
| 5389 | } |
| 5390 | |
| 5391 | audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource) |
| 5392 | { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 5393 | // Routing |
| 5394 | // Scan the whole RouteMap to see if we have an explicit route: |
| 5395 | // if the input source in the RouteMap is the same as the argument above, |
| 5396 | // and activity count is non-zero and the device in the route descriptor is available |
| 5397 | // then select this device. |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 5398 | for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) { |
| 5399 | sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex); |
Eric Laurent | 2157f5b | 2018-04-25 18:33:02 -0700 | [diff] [blame] | 5400 | if ((inputSource == route->mSource) && route->isActiveOrChanged() && |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 5401 | (mAvailableInputDevices.indexOf(route->mDeviceDescriptor) >= 0)) { |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 5402 | return route->mDeviceDescriptor->type(); |
| 5403 | } |
| 5404 | } |
| 5405 | |
| 5406 | return mEngine->getDeviceForInputSource(inputSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5407 | } |
| 5408 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5409 | float AudioPolicyManager::computeVolume(audio_stream_type_t stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5410 | int index, |
| 5411 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5412 | { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5413 | float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index); |
Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 5414 | |
| 5415 | // handle the case of accessibility active while a ringtone is playing: if the ringtone is much |
| 5416 | // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch |
| 5417 | // exploration of the dialer UI. In this situation, bring the accessibility volume closer to |
| 5418 | // the ringtone volume |
| 5419 | if ((stream == AUDIO_STREAM_ACCESSIBILITY) |
| 5420 | && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState()) |
| 5421 | && isStreamActive(AUDIO_STREAM_RING, 0)) { |
| 5422 | const float ringVolumeDB = computeVolume(AUDIO_STREAM_RING, index, device); |
| 5423 | return ringVolumeDB - 4 > volumeDB ? ringVolumeDB - 4 : volumeDB; |
| 5424 | } |
| 5425 | |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 5426 | // in-call: always cap volume by voice volume + some low headroom |
| 5427 | if ((stream != AUDIO_STREAM_VOICE_CALL) && |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5428 | (isInCall() || mOutputs.isStreamActiveLocally(AUDIO_STREAM_VOICE_CALL))) { |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 5429 | switch (stream) { |
| 5430 | case AUDIO_STREAM_SYSTEM: |
| 5431 | case AUDIO_STREAM_RING: |
| 5432 | case AUDIO_STREAM_MUSIC: |
| 5433 | case AUDIO_STREAM_ALARM: |
| 5434 | case AUDIO_STREAM_NOTIFICATION: |
| 5435 | case AUDIO_STREAM_ENFORCED_AUDIBLE: |
| 5436 | case AUDIO_STREAM_DTMF: |
| 5437 | case AUDIO_STREAM_ACCESSIBILITY: { |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5438 | int voiceVolumeIndex = |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 5439 | mVolumeCurves->getVolumeIndex(AUDIO_STREAM_VOICE_CALL, device); |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5440 | const float maxVoiceVolDb = |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 5441 | computeVolume(AUDIO_STREAM_VOICE_CALL, voiceVolumeIndex, device) |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5442 | + IN_CALL_EARPIECE_HEADROOM_DB; |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 5443 | if (volumeDB > maxVoiceVolDb) { |
| 5444 | ALOGV("computeVolume() stream %d at vol=%f overriden by stream %d at vol=%f", |
| 5445 | stream, volumeDB, AUDIO_STREAM_VOICE_CALL, maxVoiceVolDb); |
| 5446 | volumeDB = maxVoiceVolDb; |
| 5447 | } |
| 5448 | } break; |
| 5449 | default: |
| 5450 | break; |
| 5451 | } |
| 5452 | } |
| 5453 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5454 | // if a headset is connected, apply the following rules to ring tones and notifications |
| 5455 | // to avoid sound level bursts in user's ears: |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5456 | // - always attenuate notifications volume by 6dB |
| 5457 | // - attenuate ring tones volume by 6dB unless music is not playing and |
| 5458 | // speaker is part of the select devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5459 | // - if music is playing, always limit the volume to current music volume, |
| 5460 | // with a minimum threshold at -36dB so that notification is always perceived. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5461 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5462 | if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 5463 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | |
| 5464 | AUDIO_DEVICE_OUT_WIRED_HEADSET | |
Eric Laurent | 904d632 | 2017-03-17 17:20:47 -0700 | [diff] [blame] | 5465 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE | |
Jakub Pawlowski | 00f928c | 2018-03-22 13:20:03 -0700 | [diff] [blame] | 5466 | AUDIO_DEVICE_OUT_USB_HEADSET | |
| 5467 | AUDIO_DEVICE_OUT_HEARING_AID)) && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5468 | ((stream_strategy == STRATEGY_SONIFICATION) |
| 5469 | || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL) |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5470 | || (stream == AUDIO_STREAM_SYSTEM) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5471 | || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5472 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) && |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5473 | mVolumeCurves->canBeMuted(stream)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5474 | // when the phone is ringing we must consider that music could have been paused just before |
| 5475 | // by the music application and behave as if music was active if the last music track was |
| 5476 | // just stopped |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5477 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5478 | mLimitRingtoneVolume) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5479 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5480 | audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5481 | float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5482 | mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC, |
| 5483 | musicDevice), |
| 5484 | musicDevice); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5485 | float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ? |
| 5486 | musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB; |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5487 | if (volumeDB > minVolDB) { |
| 5488 | volumeDB = minVolDB; |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5489 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5490 | } |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5491 | if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 5492 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) { |
| 5493 | // on A2DP, also ensure notification volume is not too low compared to media when |
| 5494 | // intended to be played |
| 5495 | if ((volumeDB > -96.0f) && |
| 5496 | (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) { |
| 5497 | ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f", |
| 5498 | stream, device, |
| 5499 | volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB); |
| 5500 | volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB; |
| 5501 | } |
| 5502 | } |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5503 | } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) || |
| 5504 | stream_strategy != STRATEGY_SONIFICATION) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5505 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5506 | } |
| 5507 | } |
| 5508 | |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5509 | return volumeDB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5510 | } |
| 5511 | |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 5512 | int AudioPolicyManager::rescaleVolumeIndex(int srcIndex, |
| 5513 | audio_stream_type_t srcStream, |
| 5514 | audio_stream_type_t dstStream) |
| 5515 | { |
| 5516 | if (srcStream == dstStream) { |
| 5517 | return srcIndex; |
| 5518 | } |
| 5519 | float minSrc = (float)mVolumeCurves->getVolumeIndexMin(srcStream); |
| 5520 | float maxSrc = (float)mVolumeCurves->getVolumeIndexMax(srcStream); |
| 5521 | float minDst = (float)mVolumeCurves->getVolumeIndexMin(dstStream); |
| 5522 | float maxDst = (float)mVolumeCurves->getVolumeIndexMax(dstStream); |
| 5523 | |
| 5524 | return (int)(minDst + ((srcIndex - minSrc) * (maxDst - minDst)) / (maxSrc - minSrc)); |
| 5525 | } |
| 5526 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5527 | status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5528 | int index, |
| 5529 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5530 | audio_devices_t device, |
| 5531 | int delayMs, |
| 5532 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5533 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5534 | // do not change actual stream volume if the stream is muted |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5535 | if (outputDesc->mMuteCount[stream] != 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5536 | ALOGVV("checkAndSetVolume() stream %d muted count %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5537 | stream, outputDesc->mMuteCount[stream]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5538 | return NO_ERROR; |
| 5539 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5540 | audio_policy_forced_cfg_t forceUseForComm = |
| 5541 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5542 | // 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] | 5543 | if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) || |
| 5544 | (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5545 | 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] | 5546 | stream, forceUseForComm); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5547 | return INVALID_OPERATION; |
| 5548 | } |
| 5549 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5550 | if (device == AUDIO_DEVICE_NONE) { |
| 5551 | device = outputDesc->device(); |
| 5552 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5553 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5554 | float volumeDb = computeVolume(stream, index, device); |
HW Lee | da7581e | 2018-05-22 18:31:34 +0800 | [diff] [blame] | 5555 | if (outputDesc->isFixedVolume(device) || |
| 5556 | // Force VoIP volume to max for bluetooth SCO |
| 5557 | ((stream == AUDIO_STREAM_VOICE_CALL || stream == AUDIO_STREAM_BLUETOOTH_SCO) && |
| 5558 | (device & AUDIO_DEVICE_OUT_ALL_SCO) != 0)) { |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5559 | volumeDb = 0.0f; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5560 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5561 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5562 | outputDesc->setVolume(volumeDb, stream, device, delayMs, force); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5563 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5564 | if (stream == AUDIO_STREAM_VOICE_CALL || |
| 5565 | stream == AUDIO_STREAM_BLUETOOTH_SCO) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5566 | float voiceVolume; |
| 5567 | // 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] | 5568 | if (stream == AUDIO_STREAM_VOICE_CALL) { |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 5569 | voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5570 | } else { |
| 5571 | voiceVolume = 1.0; |
| 5572 | } |
| 5573 | |
Eric Laurent | 18fba84 | 2016-03-31 14:41:26 -0700 | [diff] [blame] | 5574 | if (voiceVolume != mLastVoiceVolume) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5575 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); |
| 5576 | mLastVoiceVolume = voiceVolume; |
| 5577 | } |
| 5578 | } |
| 5579 | |
| 5580 | return NO_ERROR; |
| 5581 | } |
| 5582 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5583 | void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc, |
| 5584 | audio_devices_t device, |
| 5585 | int delayMs, |
| 5586 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5587 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5588 | ALOGVV("applyStreamVolumes() for device %08x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5589 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5590 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5591 | checkAndSetVolume((audio_stream_type_t)stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5592 | mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5593 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5594 | device, |
| 5595 | delayMs, |
| 5596 | force); |
| 5597 | } |
| 5598 | } |
| 5599 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5600 | void AudioPolicyManager::setStrategyMute(routing_strategy strategy, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5601 | bool on, |
| 5602 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5603 | int delayMs, |
| 5604 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5605 | { |
Eric Laurent | 72249d5 | 2015-06-08 11:12:15 -0700 | [diff] [blame] | 5606 | ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d", |
| 5607 | strategy, on, outputDesc->getId()); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5608 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5609 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5610 | setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5611 | } |
| 5612 | } |
| 5613 | } |
| 5614 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5615 | void AudioPolicyManager::setStreamMute(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5616 | bool on, |
| 5617 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5618 | int delayMs, |
| 5619 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5620 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5621 | if (device == AUDIO_DEVICE_NONE) { |
| 5622 | device = outputDesc->device(); |
| 5623 | } |
| 5624 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5625 | ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x", |
| 5626 | stream, on, outputDesc->mMuteCount[stream], device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5627 | |
| 5628 | if (on) { |
| 5629 | if (outputDesc->mMuteCount[stream] == 0) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5630 | if (mVolumeCurves->canBeMuted(stream) && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5631 | ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5632 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5633 | checkAndSetVolume(stream, 0, outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5634 | } |
| 5635 | } |
| 5636 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored |
| 5637 | outputDesc->mMuteCount[stream]++; |
| 5638 | } else { |
| 5639 | if (outputDesc->mMuteCount[stream] == 0) { |
| 5640 | ALOGV("setStreamMute() unmuting non muted stream!"); |
| 5641 | return; |
| 5642 | } |
| 5643 | if (--outputDesc->mMuteCount[stream] == 0) { |
| 5644 | checkAndSetVolume(stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5645 | mVolumeCurves->getVolumeIndex(stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5646 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5647 | device, |
| 5648 | delayMs); |
| 5649 | } |
| 5650 | } |
| 5651 | } |
| 5652 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5653 | audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr) |
| 5654 | { |
| 5655 | // flags to stream type mapping |
| 5656 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 5657 | return AUDIO_STREAM_ENFORCED_AUDIBLE; |
| 5658 | } |
| 5659 | if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) { |
| 5660 | return AUDIO_STREAM_BLUETOOTH_SCO; |
| 5661 | } |
Jean-Michel Trivi | 79ad438 | 2015-01-29 10:49:39 -0800 | [diff] [blame] | 5662 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 5663 | return AUDIO_STREAM_TTS; |
| 5664 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5665 | |
| 5666 | // usage to stream type mapping |
| 5667 | switch (attr->usage) { |
| 5668 | case AUDIO_USAGE_MEDIA: |
| 5669 | case AUDIO_USAGE_GAME: |
Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 5670 | case AUDIO_USAGE_ASSISTANT: |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5671 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 5672 | return AUDIO_STREAM_MUSIC; |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5673 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 5674 | return AUDIO_STREAM_ACCESSIBILITY; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5675 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 5676 | return AUDIO_STREAM_SYSTEM; |
| 5677 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 5678 | return AUDIO_STREAM_VOICE_CALL; |
| 5679 | |
| 5680 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 5681 | return AUDIO_STREAM_DTMF; |
| 5682 | |
| 5683 | case AUDIO_USAGE_ALARM: |
| 5684 | return AUDIO_STREAM_ALARM; |
| 5685 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 5686 | return AUDIO_STREAM_RING; |
| 5687 | |
| 5688 | case AUDIO_USAGE_NOTIFICATION: |
| 5689 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 5690 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 5691 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 5692 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 5693 | return AUDIO_STREAM_NOTIFICATION; |
| 5694 | |
| 5695 | case AUDIO_USAGE_UNKNOWN: |
| 5696 | default: |
| 5697 | return AUDIO_STREAM_MUSIC; |
| 5698 | } |
| 5699 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5700 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5701 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) |
| 5702 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5703 | // has flags that map to a strategy? |
| 5704 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { |
| 5705 | return true; |
| 5706 | } |
| 5707 | |
| 5708 | // has known usage? |
| 5709 | switch (paa->usage) { |
| 5710 | case AUDIO_USAGE_UNKNOWN: |
| 5711 | case AUDIO_USAGE_MEDIA: |
| 5712 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 5713 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 5714 | case AUDIO_USAGE_ALARM: |
| 5715 | case AUDIO_USAGE_NOTIFICATION: |
| 5716 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 5717 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 5718 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 5719 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 5720 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 5721 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 5722 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 5723 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 5724 | case AUDIO_USAGE_GAME: |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5725 | case AUDIO_USAGE_VIRTUAL_SOURCE: |
Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 5726 | case AUDIO_USAGE_ASSISTANT: |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5727 | break; |
| 5728 | default: |
| 5729 | return false; |
| 5730 | } |
| 5731 | return true; |
| 5732 | } |
| 5733 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5734 | bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor>& outputDesc, |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5735 | routing_strategy strategy, uint32_t inPastMs, |
| 5736 | nsecs_t sysTime) const |
| 5737 | { |
| 5738 | if ((sysTime == 0) && (inPastMs != 0)) { |
| 5739 | sysTime = systemTime(); |
| 5740 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5741 | 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] | 5742 | if (((getStrategy((audio_stream_type_t)i) == strategy) || |
| 5743 | (NUM_STRATEGIES == strategy)) && |
| 5744 | outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) { |
| 5745 | return true; |
| 5746 | } |
| 5747 | } |
| 5748 | return false; |
| 5749 | } |
| 5750 | |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 5751 | bool AudioPolicyManager::isStrategyActiveOnSameModule(const sp<AudioOutputDescriptor>& outputDesc, |
| 5752 | routing_strategy strategy, uint32_t inPastMs, |
| 5753 | nsecs_t sysTime) const |
| 5754 | { |
| 5755 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5756 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 5757 | if (outputDesc->sharesHwModuleWith(desc) |
| 5758 | && isStrategyActive(desc, strategy, inPastMs, sysTime)) { |
| 5759 | return true; |
| 5760 | } |
| 5761 | } |
| 5762 | return false; |
| 5763 | } |
| 5764 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5765 | audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage) |
| 5766 | { |
| 5767 | return mEngine->getForceUse(usage); |
| 5768 | } |
| 5769 | |
| 5770 | bool AudioPolicyManager::isInCall() |
| 5771 | { |
| 5772 | return isStateInCall(mEngine->getPhoneState()); |
| 5773 | } |
| 5774 | |
| 5775 | bool AudioPolicyManager::isStateInCall(int state) |
| 5776 | { |
| 5777 | return is_state_in_call(state); |
| 5778 | } |
| 5779 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5780 | void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc) |
| 5781 | { |
| 5782 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
| 5783 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 5784 | if (sourceDesc->mDevice->equals(deviceDesc)) { |
| 5785 | ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle()); |
| 5786 | stopAudioSource(sourceDesc->getHandle()); |
| 5787 | } |
| 5788 | } |
| 5789 | |
| 5790 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
| 5791 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 5792 | bool release = false; |
| 5793 | for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) { |
| 5794 | const struct audio_port_config *source = &patchDesc->mPatch.sources[j]; |
| 5795 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 5796 | source->ext.device.type == deviceDesc->type()) { |
| 5797 | release = true; |
| 5798 | } |
| 5799 | } |
| 5800 | for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) { |
| 5801 | const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j]; |
| 5802 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && |
| 5803 | sink->ext.device.type == deviceDesc->type()) { |
| 5804 | release = true; |
| 5805 | } |
| 5806 | } |
| 5807 | if (release) { |
| 5808 | ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle); |
| 5809 | releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid); |
| 5810 | } |
| 5811 | } |
| 5812 | } |
| 5813 | |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5814 | // Modify the list of surround sound formats supported. |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5815 | void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) { |
| 5816 | FormatVector &formats = *formatsPtr; |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5817 | // TODO Set this based on Config properties. |
| 5818 | const bool alwaysForceAC3 = true; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5819 | |
| 5820 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5821 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5822 | ALOGD("%s: forced use = %d", __FUNCTION__, forceUse); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5823 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5824 | // If MANUAL, keep the supported surround sound formats as current enabled ones. |
| 5825 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
| 5826 | formats.clear(); |
| 5827 | for (auto it = mSurroundFormats.begin(); it != mSurroundFormats.end(); it++) { |
| 5828 | formats.add(*it); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5829 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5830 | // Always enable IEC61937 when in MANUAL mode. |
| 5831 | formats.add(AUDIO_FORMAT_IEC61937); |
| 5832 | } else { // NEVER, AUTO or ALWAYS |
| 5833 | // Analyze original support for various formats. |
| 5834 | bool supportsAC3 = false; |
| 5835 | bool supportsOtherSurround = false; |
| 5836 | bool supportsIEC61937 = false; |
| 5837 | mSurroundFormats.clear(); |
| 5838 | for (ssize_t formatIndex = 0; formatIndex < (ssize_t)formats.size(); formatIndex++) { |
| 5839 | audio_format_t format = formats[formatIndex]; |
| 5840 | switch (format) { |
| 5841 | case AUDIO_FORMAT_AC3: |
| 5842 | supportsAC3 = true; |
| 5843 | break; |
| 5844 | case AUDIO_FORMAT_E_AC3: |
| 5845 | case AUDIO_FORMAT_DTS: |
| 5846 | case AUDIO_FORMAT_DTS_HD: |
| 5847 | // If ALWAYS, remove all other surround formats here |
| 5848 | // since we will add them later. |
| 5849 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { |
| 5850 | formats.removeAt(formatIndex); |
| 5851 | formatIndex--; |
| 5852 | } |
| 5853 | supportsOtherSurround = true; |
| 5854 | break; |
| 5855 | case AUDIO_FORMAT_IEC61937: |
| 5856 | supportsIEC61937 = true; |
| 5857 | break; |
| 5858 | default: |
| 5859 | break; |
| 5860 | } |
| 5861 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5862 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5863 | // Modify formats based on surround preferences. |
| 5864 | // If NEVER, remove support for surround formats. |
| 5865 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
| 5866 | if (supportsAC3 || supportsOtherSurround || supportsIEC61937) { |
| 5867 | // Remove surround sound related formats. |
| 5868 | for (size_t formatIndex = 0; formatIndex < formats.size(); ) { |
| 5869 | audio_format_t format = formats[formatIndex]; |
| 5870 | switch(format) { |
| 5871 | case AUDIO_FORMAT_AC3: |
| 5872 | case AUDIO_FORMAT_E_AC3: |
| 5873 | case AUDIO_FORMAT_DTS: |
| 5874 | case AUDIO_FORMAT_DTS_HD: |
| 5875 | case AUDIO_FORMAT_IEC61937: |
| 5876 | formats.removeAt(formatIndex); |
| 5877 | break; |
| 5878 | default: |
| 5879 | formatIndex++; // keep it |
| 5880 | break; |
| 5881 | } |
| 5882 | } |
| 5883 | supportsAC3 = false; |
| 5884 | supportsOtherSurround = false; |
| 5885 | supportsIEC61937 = false; |
| 5886 | } |
| 5887 | } else { // AUTO or ALWAYS |
| 5888 | // Most TVs support AC3 even if they do not report it in the EDID. |
| 5889 | if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)) |
| 5890 | && !supportsAC3) { |
| 5891 | formats.add(AUDIO_FORMAT_AC3); |
| 5892 | supportsAC3 = true; |
| 5893 | } |
| 5894 | |
| 5895 | // If ALWAYS, add support for raw surround formats if all are missing. |
| 5896 | // This assumes that if any of these formats are reported by the HAL |
| 5897 | // then the report is valid and should not be modified. |
| 5898 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { |
| 5899 | formats.add(AUDIO_FORMAT_E_AC3); |
| 5900 | formats.add(AUDIO_FORMAT_DTS); |
| 5901 | formats.add(AUDIO_FORMAT_DTS_HD); |
| 5902 | supportsOtherSurround = true; |
| 5903 | } |
| 5904 | |
| 5905 | // Add support for IEC61937 if any raw surround supported. |
| 5906 | // The HAL could do this but add it here, just in case. |
| 5907 | if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) { |
| 5908 | formats.add(AUDIO_FORMAT_IEC61937); |
| 5909 | supportsIEC61937 = true; |
| 5910 | } |
| 5911 | |
| 5912 | // Add reported surround sound formats to enabled surround formats. |
| 5913 | for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) { |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5914 | audio_format_t format = formats[formatIndex]; |
| 5915 | switch(format) { |
| 5916 | case AUDIO_FORMAT_AC3: |
| 5917 | case AUDIO_FORMAT_E_AC3: |
| 5918 | case AUDIO_FORMAT_DTS: |
| 5919 | case AUDIO_FORMAT_DTS_HD: |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5920 | case AUDIO_FORMAT_AAC_LC: |
| 5921 | case AUDIO_FORMAT_DOLBY_TRUEHD: |
| 5922 | case AUDIO_FORMAT_E_AC3_JOC: |
| 5923 | mSurroundFormats.insert(format); |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5924 | default: |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5925 | break; |
| 5926 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5927 | } |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5928 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5929 | } |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5930 | } |
| 5931 | |
| 5932 | // Modify the list of channel masks supported. |
| 5933 | void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) { |
| 5934 | ChannelsVector &channelMasks = *channelMasksPtr; |
| 5935 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5936 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
| 5937 | |
| 5938 | // If NEVER, then remove support for channelMasks > stereo. |
| 5939 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
| 5940 | for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) { |
| 5941 | audio_channel_mask_t channelMask = channelMasks[maskIndex]; |
| 5942 | if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) { |
| 5943 | ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask); |
| 5944 | channelMasks.removeAt(maskIndex); |
| 5945 | } else { |
| 5946 | maskIndex++; |
| 5947 | } |
| 5948 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5949 | // If ALWAYS or MANUAL, then make sure we at least support 5.1 |
| 5950 | } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS |
| 5951 | || forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5952 | bool supports5dot1 = false; |
| 5953 | // Are there any channel masks that can be considered "surround"? |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5954 | for (audio_channel_mask_t channelMask : channelMasks) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5955 | if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) { |
| 5956 | supports5dot1 = true; |
| 5957 | break; |
| 5958 | } |
| 5959 | } |
| 5960 | // If not then add 5.1 support. |
| 5961 | if (!supports5dot1) { |
| 5962 | channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1); |
| 5963 | ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__); |
| 5964 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5965 | } |
| 5966 | } |
| 5967 | |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5968 | void AudioPolicyManager::updateAudioProfiles(audio_devices_t device, |
| 5969 | audio_io_handle_t ioHandle, |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5970 | AudioProfileVector &profiles) |
| 5971 | { |
| 5972 | String8 reply; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5973 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5974 | // Format MUST be checked first to update the list of AudioProfile |
| 5975 | if (profiles.hasDynamicFormat()) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5976 | reply = mpClientInterface->getParameters( |
| 5977 | ioHandle, String8(AudioParameter::keyStreamSupportedFormats)); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5978 | ALOGV("%s: supported formats %d, %s", __FUNCTION__, ioHandle, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5979 | AudioParameter repliedParameters(reply); |
| 5980 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5981 | String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) { |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5982 | ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__); |
| 5983 | return; |
| 5984 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5985 | FormatVector formats = formatsFromString(reply.string()); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5986 | if (device == AUDIO_DEVICE_OUT_HDMI) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5987 | filterSurroundFormats(&formats); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5988 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5989 | profiles.setFormats(formats); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5990 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5991 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5992 | for (audio_format_t format : profiles.getSupportedFormats()) { |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5993 | ChannelsVector channelMasks; |
| 5994 | SampleRateVector samplingRates; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5995 | AudioParameter requestedParameters; |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5996 | requestedParameters.addInt(String8(AudioParameter::keyFormat), format); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5997 | |
| 5998 | if (profiles.hasDynamicRateFor(format)) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5999 | reply = mpClientInterface->getParameters( |
| 6000 | ioHandle, |
| 6001 | requestedParameters.toString() + ";" + |
| 6002 | AudioParameter::keyStreamSupportedSamplingRates); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6003 | ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 6004 | AudioParameter repliedParameters(reply); |
| 6005 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6006 | String8(AudioParameter::keyStreamSupportedSamplingRates), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 6007 | samplingRates = samplingRatesFromString(reply.string()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6008 | } |
| 6009 | } |
| 6010 | if (profiles.hasDynamicChannelsFor(format)) { |
| 6011 | reply = mpClientInterface->getParameters(ioHandle, |
| 6012 | requestedParameters.toString() + ";" + |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6013 | AudioParameter::keyStreamSupportedChannels); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6014 | ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 6015 | AudioParameter repliedParameters(reply); |
| 6016 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6017 | String8(AudioParameter::keyStreamSupportedChannels), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 6018 | channelMasks = channelMasksFromString(reply.string()); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6019 | if (device == AUDIO_DEVICE_OUT_HDMI) { |
| 6020 | filterSurroundChannelMasks(&channelMasks); |
| 6021 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6022 | } |
| 6023 | } |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 6024 | profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates)); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6025 | } |
| 6026 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6027 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 6028 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 6029 | audio_patch_handle_t *patchHandle, |
| 6030 | AudioIODescriptorInterface *ioDescriptor, |
| 6031 | const struct audio_patch *patch, |
| 6032 | int delayMs) |
| 6033 | { |
| 6034 | ssize_t index = mAudioPatches.indexOfKey( |
| 6035 | patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE ? |
| 6036 | *patchHandle : ioDescriptor->getPatchHandle()); |
| 6037 | sp<AudioPatch> patchDesc; |
| 6038 | status_t status = installPatch( |
| 6039 | caller, index, patchHandle, patch, delayMs, mUidCached, &patchDesc); |
| 6040 | if (status == NO_ERROR) { |
| 6041 | ioDescriptor->setPatchHandle(patchDesc->mHandle); |
| 6042 | } |
| 6043 | return status; |
| 6044 | } |
| 6045 | |
| 6046 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 6047 | ssize_t index, |
| 6048 | audio_patch_handle_t *patchHandle, |
| 6049 | const struct audio_patch *patch, |
| 6050 | int delayMs, |
| 6051 | uid_t uid, |
| 6052 | sp<AudioPatch> *patchDescPtr) |
| 6053 | { |
| 6054 | sp<AudioPatch> patchDesc; |
| 6055 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 6056 | if (index >= 0) { |
| 6057 | patchDesc = mAudioPatches.valueAt(index); |
| 6058 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 6059 | } |
| 6060 | |
| 6061 | status_t status = mpClientInterface->createAudioPatch(patch, &afPatchHandle, delayMs); |
| 6062 | ALOGV("%s() AF::createAudioPatch returned %d patchHandle %d num_sources %d num_sinks %d", |
| 6063 | caller, status, afPatchHandle, patch->num_sources, patch->num_sinks); |
| 6064 | if (status == NO_ERROR) { |
| 6065 | if (index < 0) { |
| 6066 | patchDesc = new AudioPatch(patch, uid); |
| 6067 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 6068 | } else { |
| 6069 | patchDesc->mPatch = *patch; |
| 6070 | } |
| 6071 | patchDesc->mAfPatchHandle = afPatchHandle; |
| 6072 | if (patchHandle) { |
| 6073 | *patchHandle = patchDesc->mHandle; |
| 6074 | } |
| 6075 | nextAudioPortGeneration(); |
| 6076 | mpClientInterface->onAudioPatchListUpdate(); |
| 6077 | } |
| 6078 | if (patchDescPtr) *patchDescPtr = patchDesc; |
| 6079 | return status; |
| 6080 | } |
| 6081 | |
Mikhail Naganov | 1b2a794 | 2017-12-08 10:18:09 -0800 | [diff] [blame] | 6082 | } // namespace android |