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 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 205 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP |
| 206 | // output is suspended before any tracks are moved to it |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 207 | checkA2dpSuspend(); |
| 208 | checkOutputForAllStrategies(); |
| 209 | // outputs must be closed after checkOutputForAllStrategies() is executed |
| 210 | if (!outputs.isEmpty()) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 211 | for (audio_io_handle_t output : outputs) { |
| 212 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 213 | // close unused outputs after device disconnection or direct outputs that have been |
| 214 | // opened by checkOutputsForDevice() to query dynamic parameters |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 215 | if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 216 | (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) && |
| 217 | (desc->mDirectOpenCount == 0))) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 218 | closeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 219 | } |
| 220 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 221 | // check again after closing A2DP output to reset mA2dpSuspended if needed |
| 222 | checkA2dpSuspend(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | updateDevicesAndOutputs(); |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 226 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 227 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 228 | updateCallRouting(newDevice); |
| 229 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 230 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 231 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 232 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) { |
| 233 | audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 234 | // do not force device change on duplicated output because if device is 0, it will |
| 235 | // also force a device 0 for the two outputs it is duplicated to which may override |
| 236 | // a valid device selection on those outputs. |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 237 | bool force = !desc->isDuplicated() |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 238 | && (!device_distinguishes_on_address(device) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 239 | // always force when disconnecting (a non-duplicated device) |
| 240 | || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 241 | setOutputDevice(desc, newDevice, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 242 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 245 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
| 246 | cleanUpForDevice(devDesc); |
| 247 | } |
| 248 | |
Eric Laurent | 72aa32f | 2014-05-30 18:51:48 -0700 | [diff] [blame] | 249 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | b71e58b | 2014-05-29 16:08:11 -0700 | [diff] [blame] | 250 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 251 | } // end if is output device |
| 252 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 253 | // handle input devices |
| 254 | if (audio_is_input_device(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 255 | SortedVector <audio_io_handle_t> inputs; |
| 256 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 257 | ssize_t index = mAvailableInputDevices.indexOf(devDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 258 | switch (state) |
| 259 | { |
| 260 | // handle input device connection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 261 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 262 | if (index >= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 263 | ALOGW("setDeviceConnectionState() device already connected: %d", device); |
| 264 | return INVALID_OPERATION; |
| 265 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 266 | sp<HwModule> module = mHwModules.getModuleForDevice(device); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 267 | if (module == NULL) { |
| 268 | ALOGW("setDeviceConnectionState(): could not find HW module for device %08x", |
| 269 | device); |
| 270 | return INVALID_OPERATION; |
| 271 | } |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 272 | |
| 273 | // Before checking intputs, broadcast connect event to allow HAL to retrieve dynamic |
| 274 | // parameters on newly connected devices (instead of opening the inputs...) |
| 275 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); |
| 276 | |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 277 | if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) { |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 278 | broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 279 | devDesc->mAddress); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 280 | return INVALID_OPERATION; |
| 281 | } |
| 282 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 283 | index = mAvailableInputDevices.add(devDesc); |
| 284 | if (index >= 0) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 285 | mAvailableInputDevices[index]->attach(module); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 286 | } else { |
| 287 | return NO_MEMORY; |
| 288 | } |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 289 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 290 | // Propagate device availability to Engine |
| 291 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 292 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 293 | |
| 294 | // handle input device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 295 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 296 | if (index < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 297 | ALOGW("setDeviceConnectionState() device not connected: %d", device); |
| 298 | return INVALID_OPERATION; |
| 299 | } |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 300 | |
| 301 | ALOGV("setDeviceConnectionState() disconnecting input device %x", device); |
| 302 | |
| 303 | // Set Disconnect to HALs |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 304 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 305 | |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 306 | checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 307 | mAvailableInputDevices.remove(devDesc); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 308 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 309 | // Propagate device availability to Engine |
| 310 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 311 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 312 | |
| 313 | default: |
| 314 | ALOGE("setDeviceConnectionState() invalid state: %x", state); |
| 315 | return BAD_VALUE; |
| 316 | } |
| 317 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 318 | closeAllInputs(); |
Eric Laurent | 5f5fca5 | 2016-08-04 11:48:57 -0700 | [diff] [blame] | 319 | // As the input device list can impact the output device selection, update |
| 320 | // getDeviceForStrategy() cache |
| 321 | updateDevicesAndOutputs(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 322 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 323 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 324 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 325 | updateCallRouting(newDevice); |
| 326 | } |
| 327 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 328 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
| 329 | cleanUpForDevice(devDesc); |
| 330 | } |
| 331 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 332 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 333 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 334 | } // end if is input device |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 335 | |
| 336 | ALOGW("setDeviceConnectionState() invalid device: %x", device); |
| 337 | return BAD_VALUE; |
| 338 | } |
| 339 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 340 | audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 341 | const char *device_address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 342 | { |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 343 | sp<DeviceDescriptor> devDesc = |
| 344 | mHwModules.getDeviceDescriptor(device, device_address, "", |
| 345 | (strlen(device_address) != 0)/*matchAddress*/); |
| 346 | |
| 347 | if (devDesc == 0) { |
| 348 | ALOGW("getDeviceConnectionState() undeclared device, type %08x, address: %s", |
| 349 | device, device_address); |
| 350 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
| 351 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 352 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 353 | DeviceVector *deviceVector; |
| 354 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 355 | if (audio_is_output_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 356 | deviceVector = &mAvailableOutputDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 357 | } else if (audio_is_input_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 358 | deviceVector = &mAvailableInputDevices; |
| 359 | } else { |
| 360 | ALOGW("getDeviceConnectionState() invalid device type %08x", device); |
| 361 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 362 | } |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 363 | |
| 364 | return (deviceVector->getDevice(device, String8(device_address)) != 0) ? |
| 365 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 366 | } |
| 367 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 368 | status_t AudioPolicyManager::handleDeviceConfigChange(audio_devices_t device, |
| 369 | const char *device_address, |
| 370 | const char *device_name) |
| 371 | { |
| 372 | status_t status; |
| 373 | |
| 374 | ALOGV("handleDeviceConfigChange(() device: 0x%X, address %s name %s", |
| 375 | device, device_address, device_name); |
| 376 | |
Pavlin Radoslavov | c694ff4 | 2017-01-09 23:27:29 -0800 | [diff] [blame] | 377 | // connect/disconnect only 1 device at a time |
| 378 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; |
| 379 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 380 | // Check if the device is currently connected |
| 381 | sp<DeviceDescriptor> devDesc = |
| 382 | mHwModules.getDeviceDescriptor(device, device_address, device_name); |
| 383 | ssize_t index = mAvailableOutputDevices.indexOf(devDesc); |
| 384 | if (index < 0) { |
| 385 | // Nothing to do: device is not connected |
| 386 | return NO_ERROR; |
| 387 | } |
| 388 | |
| 389 | // Toggle the device state: UNAVAILABLE -> AVAILABLE |
| 390 | // This will force reading again the device configuration |
| 391 | status = setDeviceConnectionState(device, |
| 392 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 393 | device_address, device_name); |
| 394 | if (status != NO_ERROR) { |
| 395 | ALOGW("handleDeviceConfigChange() error disabling connection state: %d", |
| 396 | status); |
| 397 | return status; |
| 398 | } |
| 399 | |
| 400 | status = setDeviceConnectionState(device, |
| 401 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 402 | device_address, device_name); |
| 403 | if (status != NO_ERROR) { |
| 404 | ALOGW("handleDeviceConfigChange() error enabling connection state: %d", |
| 405 | status); |
| 406 | return status; |
| 407 | } |
| 408 | |
| 409 | return NO_ERROR; |
| 410 | } |
| 411 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 412 | uint32_t AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, uint32_t delayMs) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 413 | { |
| 414 | bool createTxPatch = false; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 415 | uint32_t muteWaitMs = 0; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 416 | |
Andy Hung | a76c7de | 2016-12-13 19:14:31 -0800 | [diff] [blame] | 417 | if(!hasPrimaryOutput() || mPrimaryOutput->device() == AUDIO_DEVICE_OUT_STUB) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 418 | return muteWaitMs; |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 419 | } |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 420 | audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 421 | ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice); |
| 422 | |
| 423 | // release existing RX patch if any |
| 424 | if (mCallRxPatch != 0) { |
| 425 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 426 | mCallRxPatch.clear(); |
| 427 | } |
| 428 | // release TX patch if any |
| 429 | if (mCallTxPatch != 0) { |
| 430 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 431 | mCallTxPatch.clear(); |
| 432 | } |
| 433 | |
| 434 | // If the RX device is on the primary HW module, then use legacy routing method for voice calls |
| 435 | // via setOutputDevice() on primary output. |
| 436 | // Otherwise, create two audio patches for TX and RX path. |
| 437 | if (availablePrimaryOutputDevices() & rxDevice) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 438 | muteWaitMs = setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 439 | // If the TX device is also on the primary HW module, setOutputDevice() will take care |
| 440 | // of it due to legacy implementation. If not, create a patch. |
| 441 | if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN) |
| 442 | == AUDIO_DEVICE_NONE) { |
| 443 | createTxPatch = true; |
| 444 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 445 | } else { // create RX path audio patch |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 446 | mCallRxPatch = createTelephonyPatch(true /*isRx*/, rxDevice, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 447 | createTxPatch = true; |
| 448 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 449 | if (createTxPatch) { // create TX path audio patch |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 450 | mCallTxPatch = createTelephonyPatch(false /*isRx*/, txDevice, delayMs); |
| 451 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 452 | |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 453 | return muteWaitMs; |
| 454 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 455 | |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 456 | sp<AudioPatch> AudioPolicyManager::createTelephonyPatch( |
| 457 | bool isRx, audio_devices_t device, uint32_t delayMs) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 458 | PatchBuilder patchBuilder; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 459 | |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 460 | sp<DeviceDescriptor> txSourceDeviceDesc; |
| 461 | if (isRx) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 462 | patchBuilder.addSink(findDevice(mAvailableOutputDevices, device)). |
| 463 | addSource(findDevice(mAvailableInputDevices, AUDIO_DEVICE_IN_TELEPHONY_RX)); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 464 | } else { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 465 | patchBuilder.addSource(txSourceDeviceDesc = findDevice(mAvailableInputDevices, device)). |
| 466 | addSink(findDevice(mAvailableOutputDevices, AUDIO_DEVICE_OUT_TELEPHONY_TX)); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | audio_devices_t outputDevice = isRx ? device : AUDIO_DEVICE_OUT_TELEPHONY_TX; |
| 470 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(outputDevice, mOutputs); |
| 471 | audio_io_handle_t output = selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID); |
| 472 | // request to reuse existing output stream if one is already opened to reach the target device |
| 473 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 474 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 475 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 476 | "%s() %#x device output %d is duplicated", __func__, outputDevice, output); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 477 | patchBuilder.addSource(outputDesc, { .stream = AUDIO_STREAM_PATCH }); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | if (!isRx) { |
Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 481 | // terminate active capture if on the same HW module as the call TX source device |
| 482 | // FIXME: would be better to refine to only inputs whose profile connects to the |
| 483 | // call TX device but this information is not in the audio patch and logic here must be |
| 484 | // symmetric to the one in startInput() |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 485 | for (const auto& activeDesc : mInputs.getActiveInputs()) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 486 | if (activeDesc->hasSameHwModuleAs(txSourceDeviceDesc)) { |
| 487 | AudioSessionCollection activeSessions = |
| 488 | activeDesc->getAudioSessions(true /*activeOnly*/); |
| 489 | for (size_t j = 0; j < activeSessions.size(); j++) { |
| 490 | audio_session_t activeSession = activeSessions.keyAt(j); |
| 491 | stopInput(activeDesc->mIoHandle, activeSession); |
| 492 | releaseInput(activeDesc->mIoHandle, activeSession); |
| 493 | } |
Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 494 | } |
| 495 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 496 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 497 | |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 498 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 499 | status_t status = mpClientInterface->createAudioPatch( |
| 500 | patchBuilder.patch(), &afPatchHandle, delayMs); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 501 | ALOGW_IF(status != NO_ERROR, |
| 502 | "%s() error %d creating %s audio patch", __func__, status, isRx ? "RX" : "TX"); |
| 503 | sp<AudioPatch> audioPatch; |
| 504 | if (status == NO_ERROR) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 505 | audioPatch = new AudioPatch(patchBuilder.patch(), mUidCached); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 506 | audioPatch->mAfPatchHandle = afPatchHandle; |
| 507 | audioPatch->mUid = mUidCached; |
| 508 | } |
| 509 | return audioPatch; |
| 510 | } |
| 511 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 512 | sp<DeviceDescriptor> AudioPolicyManager::findDevice( |
| 513 | const DeviceVector& devices, audio_devices_t device) { |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 514 | DeviceVector deviceList = devices.getDevicesFromType(device); |
| 515 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 516 | "%s() selected device type %#x is not in devices list", __func__, device); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 517 | return deviceList.itemAt(0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 518 | } |
| 519 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 520 | void AudioPolicyManager::setPhoneState(audio_mode_t state) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 521 | { |
| 522 | ALOGV("setPhoneState() state %d", state); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 523 | // store previous phone state for management of sonification strategy below |
| 524 | int oldState = mEngine->getPhoneState(); |
| 525 | |
| 526 | if (mEngine->setPhoneState(state) != NO_ERROR) { |
| 527 | ALOGW("setPhoneState() invalid or same state %d", state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 528 | return; |
| 529 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 530 | /// Opens: can these line be executed after the switch of volume curves??? |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 531 | // if leaving call state, handle special case of active streams |
| 532 | // pertaining to sonification strategy see handleIncallSonification() |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 533 | if (isStateInCall(oldState)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 534 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 535 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 536 | handleIncallSonification((audio_stream_type_t)stream, false, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 537 | } |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 538 | |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 539 | // force reevaluating accessibility routing when call stops |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 540 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 541 | } |
| 542 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 543 | /** |
| 544 | * Switching to or from incall state or switching between telephony and VoIP lead to force |
| 545 | * routing command. |
| 546 | */ |
| 547 | bool force = ((is_state_in_call(oldState) != is_state_in_call(state)) |
| 548 | || (is_state_in_call(state) && (state != oldState))); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 549 | |
| 550 | // check for device and output changes triggered by new phone state |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 551 | checkA2dpSuspend(); |
| 552 | checkOutputForAllStrategies(); |
| 553 | updateDevicesAndOutputs(); |
| 554 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 555 | int delayMs = 0; |
| 556 | if (isStateInCall(state)) { |
| 557 | nsecs_t sysTime = systemTime(); |
| 558 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 559 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 560 | // mute media and sonification strategies and delay device switch by the largest |
| 561 | // latency of any output where either strategy is active. |
| 562 | // 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] | 563 | if ((isStrategyActive(desc, STRATEGY_MEDIA, |
| 564 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 565 | sysTime) || |
| 566 | isStrategyActive(desc, STRATEGY_SONIFICATION, |
| 567 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 568 | sysTime)) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 569 | (delayMs < (int)desc->latency()*2)) { |
| 570 | delayMs = desc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 571 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 572 | setStrategyMute(STRATEGY_MEDIA, true, desc); |
| 573 | setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 574 | getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/)); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 575 | setStrategyMute(STRATEGY_SONIFICATION, true, desc); |
| 576 | setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 577 | getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/)); |
| 578 | } |
| 579 | } |
| 580 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 581 | if (hasPrimaryOutput()) { |
| 582 | // Note that despite the fact that getNewOutputDevice() is called on the primary output, |
| 583 | // the device returned is not necessarily reachable via this output |
| 584 | audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 585 | // force routing command to audio hardware when ending call |
| 586 | // even if no device change is needed |
| 587 | if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) { |
| 588 | rxDevice = mPrimaryOutput->device(); |
| 589 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 590 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 591 | if (state == AUDIO_MODE_IN_CALL) { |
| 592 | updateCallRouting(rxDevice, delayMs); |
| 593 | } else if (oldState == AUDIO_MODE_IN_CALL) { |
| 594 | if (mCallRxPatch != 0) { |
| 595 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 596 | mCallRxPatch.clear(); |
| 597 | } |
| 598 | if (mCallTxPatch != 0) { |
| 599 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 600 | mCallTxPatch.clear(); |
| 601 | } |
| 602 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
| 603 | } else { |
| 604 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 605 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 606 | } |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 607 | |
| 608 | // reevaluate routing on all outputs in case tracks have been started during the call |
| 609 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 610 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 611 | audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/); |
| 612 | if (state != AUDIO_MODE_IN_CALL || desc != mPrimaryOutput) { |
Yung Ti Su | f60c824 | 2018-05-10 18:07:26 +0800 | [diff] [blame] | 613 | setOutputDevice(desc, newDevice, (newDevice != AUDIO_DEVICE_NONE), 0 /*delayMs*/); |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 614 | } |
| 615 | } |
| 616 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 617 | // if entering in call state, handle special case of active streams |
| 618 | // pertaining to sonification strategy see handleIncallSonification() |
| 619 | if (isStateInCall(state)) { |
| 620 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 621 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 622 | handleIncallSonification((audio_stream_type_t)stream, true, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 623 | } |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 624 | |
| 625 | // force reevaluating accessibility routing when call starts |
| 626 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | // 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] | 630 | if (state == AUDIO_MODE_RINGTONE && |
| 631 | isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 632 | mLimitRingtoneVolume = true; |
| 633 | } else { |
| 634 | mLimitRingtoneVolume = false; |
| 635 | } |
| 636 | } |
| 637 | |
Jean-Michel Trivi | 887a9ed | 2015-03-31 18:02:24 -0700 | [diff] [blame] | 638 | audio_mode_t AudioPolicyManager::getPhoneState() { |
| 639 | return mEngine->getPhoneState(); |
| 640 | } |
| 641 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 642 | void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 643 | audio_policy_forced_cfg_t config) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 644 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 645 | ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState()); |
Eric Laurent | 8dc87a6 | 2017-05-16 19:00:40 -0700 | [diff] [blame] | 646 | if (config == mEngine->getForceUse(usage)) { |
| 647 | return; |
| 648 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 649 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 650 | if (mEngine->setForceUse(usage, config) != NO_ERROR) { |
| 651 | ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage); |
| 652 | return; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 653 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 654 | bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) || |
| 655 | (usage == AUDIO_POLICY_FORCE_FOR_DOCK) || |
| 656 | (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 657 | |
| 658 | // check for device and output changes triggered by new force usage |
| 659 | checkA2dpSuspend(); |
| 660 | checkOutputForAllStrategies(); |
| 661 | updateDevicesAndOutputs(); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 662 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 663 | //FIXME: workaround for truncated touch sounds |
| 664 | // to be removed when the problem is handled by system UI |
| 665 | uint32_t delayMs = 0; |
| 666 | uint32_t waitMs = 0; |
| 667 | if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) { |
| 668 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 669 | } |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 670 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 671 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/); |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 672 | waitMs = updateCallRouting(newDevice, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 673 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 674 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 675 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
| 676 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/); |
| 677 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 678 | waitMs = setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE), |
| 679 | delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 680 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 681 | if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 682 | applyStreamVolumes(outputDesc, newDevice, waitMs, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 683 | } |
| 684 | } |
| 685 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 686 | for (const auto& activeDesc : mInputs.getActiveInputs()) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 687 | audio_devices_t newDevice = getNewInputDevice(activeDesc); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 688 | // 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] | 689 | if (activeDesc->mProfile->getSupportedDevices().types() & |
| 690 | (newDevice & ~AUDIO_DEVICE_BIT_IN)) { |
| 691 | setInputDevice(activeDesc->mIoHandle, newDevice); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 692 | } else { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 693 | closeInput(activeDesc->mIoHandle); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 694 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 695 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 696 | } |
| 697 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 698 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 699 | { |
| 700 | ALOGV("setSystemProperty() property %s, value %s", property, value); |
| 701 | } |
| 702 | |
| 703 | // Find a direct output profile compatible with the parameters passed, even if the input flags do |
| 704 | // not explicitly request a direct output |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 705 | sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput( |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 706 | audio_devices_t device, |
| 707 | uint32_t samplingRate, |
| 708 | audio_format_t format, |
| 709 | audio_channel_mask_t channelMask, |
| 710 | audio_output_flags_t flags) |
| 711 | { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 712 | // only retain flags that will drive the direct output profile selection |
| 713 | // if explicitly requested |
| 714 | static const uint32_t kRelevantFlags = |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 715 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | |
| 716 | AUDIO_OUTPUT_FLAG_VOIP_RX); |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 717 | flags = |
| 718 | (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); |
| 719 | |
| 720 | sp<IOProfile> profile; |
| 721 | |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 722 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 723 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 724 | if (!curProfile->isCompatibleProfile(device, String8(""), |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 725 | samplingRate, NULL /*updatedSamplingRate*/, |
| 726 | format, NULL /*updatedFormat*/, |
| 727 | channelMask, NULL /*updatedChannelMask*/, |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 728 | flags)) { |
| 729 | continue; |
| 730 | } |
| 731 | // reject profiles not corresponding to a device currently available |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 732 | if ((mAvailableOutputDevices.types() & curProfile->getSupportedDevicesType()) == 0) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 733 | continue; |
| 734 | } |
| 735 | // 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] | 736 | if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 737 | continue; |
| 738 | } |
| 739 | profile = curProfile; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 740 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 741 | break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 742 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 743 | } |
| 744 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 745 | return profile; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 746 | } |
| 747 | |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 748 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 749 | { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 750 | routing_strategy strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 751 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Andy Hung | c990152 | 2017-11-10 20:07:54 -0800 | [diff] [blame] | 752 | |
| 753 | // Note that related method getOutputForAttr() uses getOutputForDevice() not selectOutput(). |
| 754 | // We use selectOutput() here since we don't have the desired AudioTrack sample rate, |
| 755 | // format, flags, etc. This may result in some discrepancy for functions that utilize |
| 756 | // getOutput() solely on audio_stream_type such as AudioSystem::getOutputFrameCount() |
| 757 | // and AudioSystem::getOutputSamplingRate(). |
| 758 | |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 759 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 760 | 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] | 761 | |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 762 | ALOGV("getOutput() stream %d selected device %08x, output %d", stream, device, output); |
| 763 | return output; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 764 | } |
| 765 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 766 | status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, |
| 767 | audio_io_handle_t *output, |
| 768 | audio_session_t session, |
| 769 | audio_stream_type_t *stream, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 770 | uid_t uid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 771 | const audio_config_t *config, |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 772 | audio_output_flags_t *flags, |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 773 | audio_port_handle_t *selectedDeviceId, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 774 | audio_port_handle_t *portId) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 775 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 776 | audio_attributes_t attributes; |
| 777 | if (attr != NULL) { |
| 778 | if (!isValidAttributes(attr)) { |
| 779 | ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]", |
| 780 | attr->usage, attr->content_type, attr->flags, |
| 781 | attr->tags); |
| 782 | return BAD_VALUE; |
| 783 | } |
| 784 | attributes = *attr; |
| 785 | } else { |
| 786 | if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) { |
| 787 | ALOGE("getOutputForAttr(): invalid stream type"); |
| 788 | return BAD_VALUE; |
| 789 | } |
| 790 | stream_type_to_audio_attributes(*stream, &attributes); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 791 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 792 | |
| 793 | // TODO: check for existing client for this port ID |
| 794 | if (*portId == AUDIO_PORT_HANDLE_NONE) { |
| 795 | *portId = AudioPort::getNextUniqueId(); |
| 796 | } |
| 797 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 798 | sp<SwAudioOutputDescriptor> desc; |
Jean-Michel Trivi | e8deced | 2016-02-11 12:50:39 -0800 | [diff] [blame] | 799 | if (mPolicyMixes.getOutputForAttr(attributes, uid, desc) == NO_ERROR) { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 800 | ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr"); |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 801 | if (!audio_has_proportional_frames(config->format)) { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 802 | return BAD_VALUE; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 803 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 804 | *stream = streamTypefromAttributesInt(&attributes); |
| 805 | *output = desc->mIoHandle; |
| 806 | ALOGV("getOutputForAttr() returns output %d", *output); |
| 807 | return NO_ERROR; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 808 | } |
| 809 | if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 810 | ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE"); |
| 811 | return BAD_VALUE; |
| 812 | } |
| 813 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 814 | ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x" |
| 815 | " session %d selectedDeviceId %d", |
| 816 | attributes.usage, attributes.content_type, attributes.tags, attributes.flags, |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 817 | session, *selectedDeviceId); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 818 | |
| 819 | *stream = streamTypefromAttributesInt(&attributes); |
| 820 | |
| 821 | // Explicit routing? |
| 822 | sp<DeviceDescriptor> deviceDesc; |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 823 | if (*selectedDeviceId != AUDIO_PORT_HANDLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 824 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(*selectedDeviceId); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 825 | } |
| 826 | mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 827 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 828 | routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 829 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 830 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 831 | if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 832 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 833 | } |
| 834 | |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 835 | ALOGV("getOutputForAttr() device 0x%x, sampling rate %d, format %#x, channel mask %#x, " |
| 836 | "flags %#x", |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 837 | device, config->sample_rate, config->format, config->channel_mask, *flags); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 838 | |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 839 | *output = getOutputForDevice(device, session, *stream, config, flags); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 840 | if (*output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 841 | mOutputRoutes.removeRoute(session); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 842 | return INVALID_OPERATION; |
| 843 | } |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 844 | |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 845 | DeviceVector outputDevices = mAvailableOutputDevices.getDevicesFromType(device); |
| 846 | *selectedDeviceId = outputDevices.size() > 0 ? outputDevices.itemAt(0)->getId() |
| 847 | : AUDIO_PORT_HANDLE_NONE; |
| 848 | |
| 849 | ALOGV(" getOutputForAttr() returns output %d selectedDeviceId %d", *output, *selectedDeviceId); |
| 850 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 851 | return NO_ERROR; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | audio_io_handle_t AudioPolicyManager::getOutputForDevice( |
| 855 | audio_devices_t device, |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 856 | audio_session_t session, |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 857 | audio_stream_type_t stream, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 858 | const audio_config_t *config, |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 859 | audio_output_flags_t *flags) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 860 | { |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 861 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 862 | status_t status; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 863 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 864 | // open a direct output if required by specified parameters |
| 865 | //force direct flag if offload flag is set: offloading implies a direct output stream |
| 866 | // and all common behaviors are driven by checking only the direct flag |
| 867 | // this should normally be set appropriately in the policy configuration file |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 868 | if ((*flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 869 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 870 | } |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 871 | if ((*flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 872 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 873 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 874 | // only allow deep buffering for music stream type |
| 875 | if (stream != AUDIO_STREAM_MUSIC) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 876 | *flags = (audio_output_flags_t)(*flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 877 | } else if (/* stream == AUDIO_STREAM_MUSIC && */ |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 878 | *flags == AUDIO_OUTPUT_FLAG_NONE && |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 879 | property_get_bool("audio.deep_buffer.media", false /* default_value */)) { |
| 880 | // use DEEP_BUFFER as default output for music stream type |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 881 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER; |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 882 | } |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 883 | if (stream == AUDIO_STREAM_TTS) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 884 | *flags = AUDIO_OUTPUT_FLAG_TTS; |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 885 | } else if (stream == AUDIO_STREAM_VOICE_CALL && |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 886 | audio_is_linear_pcm(config->format)) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 887 | *flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX | |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 888 | AUDIO_OUTPUT_FLAG_DIRECT); |
| 889 | ALOGV("Set VoIP and Direct output flags for PCM format"); |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 890 | } else if (device == AUDIO_DEVICE_OUT_TELEPHONY_TX && |
| 891 | stream == AUDIO_STREAM_MUSIC && |
| 892 | audio_is_linear_pcm(config->format) && |
| 893 | isInCall()) { |
| 894 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INCALL_MUSIC; |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 895 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 896 | |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 897 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 898 | sp<IOProfile> profile; |
| 899 | |
| 900 | // 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] | 901 | // and not explicitly requested |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 902 | if (((*flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 903 | audio_is_linear_pcm(config->format) && config->sample_rate <= SAMPLE_RATE_HZ_MAX && |
| 904 | audio_channel_count_from_out_mask(config->channel_mask) <= 2) { |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 905 | goto non_direct_output; |
| 906 | } |
| 907 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 908 | // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled. |
| 909 | // This prevents creating an offloaded track and tearing it down immediately after start |
| 910 | // when audioflinger detects there is an active non offloadable effect. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 911 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 912 | // This may prevent offloading in rare situations where effects are left active by apps |
| 913 | // in the background. |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 914 | |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 915 | if (((*flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 916 | !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 917 | profile = getProfileForDirectOutput(device, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 918 | config->sample_rate, |
| 919 | config->format, |
| 920 | config->channel_mask, |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 921 | (audio_output_flags_t)*flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 922 | } |
| 923 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 924 | if (profile != 0) { |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 925 | // exclusive outputs for MMAP and Offload are enforced by different session ids. |
| 926 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 927 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 928 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { |
| 929 | // reuse direct output if currently open by the same client |
| 930 | // and configured with same parameters |
| 931 | if ((config->sample_rate == desc->mSamplingRate) && |
Andy Hung | 5659ed5 | 2018-04-30 10:31:26 -0700 | [diff] [blame] | 932 | (config->format == desc->mFormat) && |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 933 | (config->channel_mask == desc->mChannelMask) && |
| 934 | (session == desc->mDirectClientSession)) { |
| 935 | desc->mDirectOpenCount++; |
| 936 | ALOGI("getOutputForDevice() reusing direct output %d for session %d", |
| 937 | mOutputs.keyAt(i), session); |
| 938 | return mOutputs.keyAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 939 | } |
| 940 | } |
| 941 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 942 | |
| 943 | if (!profile->canOpenNewIo()) { |
| 944 | goto non_direct_output; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 945 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 946 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 947 | sp<SwAudioOutputDescriptor> outputDesc = |
| 948 | new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 949 | |
| 950 | DeviceVector outputDevices = mAvailableOutputDevices.getDevicesFromType(device); |
| 951 | String8 address = outputDevices.size() > 0 ? outputDevices.itemAt(0)->mAddress |
| 952 | : String8(""); |
| 953 | |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 954 | status = outputDesc->open(config, device, address, stream, *flags, &output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 955 | |
| 956 | // only accept an output with the requested parameters |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 957 | if (status != NO_ERROR || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 958 | (config->sample_rate != 0 && config->sample_rate != outputDesc->mSamplingRate) || |
Andy Hung | 5659ed5 | 2018-04-30 10:31:26 -0700 | [diff] [blame] | 959 | (config->format != AUDIO_FORMAT_DEFAULT && config->format != outputDesc->mFormat) || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 960 | (config->channel_mask != 0 && config->channel_mask != outputDesc->mChannelMask)) { |
| 961 | ALOGV("getOutputForDevice() failed opening direct output: output %d sample rate %d %d," |
| 962 | "format %d %d, channel mask %04x %04x", output, config->sample_rate, |
| 963 | outputDesc->mSamplingRate, config->format, outputDesc->mFormat, |
| 964 | config->channel_mask, outputDesc->mChannelMask); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 965 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 966 | outputDesc->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 967 | } |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 968 | // 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] | 969 | if (audio_is_linear_pcm(config->format) && |
| 970 | config->sample_rate <= SAMPLE_RATE_HZ_MAX) { |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 971 | goto non_direct_output; |
| 972 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 973 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 974 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 975 | outputDesc->mRefCount[stream] = 0; |
| 976 | outputDesc->mStopTime[stream] = 0; |
| 977 | outputDesc->mDirectOpenCount = 1; |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 978 | outputDesc->mDirectClientSession = session; |
| 979 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 980 | addOutput(output, outputDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 981 | mPreviousOutputs = mOutputs; |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 982 | ALOGV("getOutputForDevice() returns new direct output %d", output); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 983 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 984 | return output; |
| 985 | } |
| 986 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 987 | non_direct_output: |
Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 988 | |
| 989 | // A request for HW A/V sync cannot fallback to a mixed output because time |
| 990 | // stamps are embedded in audio data |
Phil Burk | 2d05993 | 2018-02-15 15:55:11 -0800 | [diff] [blame] | 991 | 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] | 992 | return AUDIO_IO_HANDLE_NONE; |
| 993 | } |
| 994 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 995 | // ignoring channel mask due to downmix capability in mixer |
| 996 | |
| 997 | // open a non direct output |
| 998 | |
| 999 | // for non direct outputs, only PCM is supported |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1000 | if (audio_is_linear_pcm(config->format)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1001 | // get which output is suitable for the specified stream. The actual |
| 1002 | // routing change will happen when startOutput() will be called |
| 1003 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 1004 | |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1005 | // 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] | 1006 | *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_DIRECT); |
| 1007 | output = selectOutput(outputs, *flags, config->format); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1008 | } |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 1009 | ALOGW_IF((output == 0), "getOutputForDevice() could not find output for stream %d, " |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1010 | "sampling rate %d, format %#x, channels %#x, flags %#x", |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1011 | stream, config->sample_rate, config->format, config->channel_mask, *flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1012 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1013 | return output; |
| 1014 | } |
| 1015 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1016 | 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] | 1017 | audio_output_flags_t flags, |
| 1018 | audio_format_t format) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1019 | { |
| 1020 | // select one output among several that provide a path to a particular device or set of |
| 1021 | // devices (the list was previously build by getOutputsForDevice()). |
| 1022 | // The priority is as follows: |
| 1023 | // 1: the output with the highest number of requested policy flags |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1024 | // 2: the output with the bit depth the closest to the requested one |
| 1025 | // 3: the primary output |
| 1026 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1027 | |
| 1028 | if (outputs.size() == 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1029 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1030 | } |
| 1031 | if (outputs.size() == 1) { |
| 1032 | return outputs[0]; |
| 1033 | } |
| 1034 | |
| 1035 | int maxCommonFlags = 0; |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1036 | audio_io_handle_t outputForFlags = AUDIO_IO_HANDLE_NONE; |
| 1037 | audio_io_handle_t outputForPrimary = AUDIO_IO_HANDLE_NONE; |
| 1038 | audio_io_handle_t outputForFormat = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1039 | audio_format_t bestFormat = AUDIO_FORMAT_INVALID; |
| 1040 | audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1041 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1042 | for (audio_io_handle_t output : outputs) { |
| 1043 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1044 | if (!outputDesc->isDuplicated()) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1045 | // if a valid format is specified, skip output if not compatible |
| 1046 | if (format != AUDIO_FORMAT_INVALID) { |
| 1047 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
Andy Hung | 5659ed5 | 2018-04-30 10:31:26 -0700 | [diff] [blame] | 1048 | if (format != outputDesc->mFormat) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1049 | continue; |
| 1050 | } |
| 1051 | } else if (!audio_is_linear_pcm(format)) { |
| 1052 | continue; |
| 1053 | } |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1054 | if (AudioPort::isBetterFormatMatch( |
| 1055 | outputDesc->mFormat, bestFormat, format)) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1056 | outputForFormat = output; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1057 | bestFormat = outputDesc->mFormat; |
| 1058 | } |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1061 | int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags); |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1062 | if (commonFlags >= maxCommonFlags) { |
| 1063 | if (commonFlags == maxCommonFlags) { |
Andy Hung | c990152 | 2017-11-10 20:07:54 -0800 | [diff] [blame] | 1064 | if (format != AUDIO_FORMAT_INVALID |
| 1065 | && AudioPort::isBetterFormatMatch( |
| 1066 | outputDesc->mFormat, bestFormatForFlags, format)) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1067 | outputForFlags = output; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1068 | bestFormatForFlags = outputDesc->mFormat; |
| 1069 | } |
| 1070 | } else { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1071 | outputForFlags = output; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1072 | maxCommonFlags = commonFlags; |
| 1073 | bestFormatForFlags = outputDesc->mFormat; |
| 1074 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1075 | ALOGV("selectOutput() commonFlags for output %d, %04x", output, commonFlags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1076 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1077 | if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1078 | outputForPrimary = output; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1079 | } |
| 1080 | } |
| 1081 | } |
| 1082 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1083 | if (outputForFlags != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1084 | return outputForFlags; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1085 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1086 | if (outputForFormat != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1087 | return outputForFormat; |
| 1088 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1089 | if (outputForPrimary != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1090 | return outputForPrimary; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | return outputs[0]; |
| 1094 | } |
| 1095 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1096 | status_t AudioPolicyManager::startOutput(audio_io_handle_t output, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1097 | audio_stream_type_t stream, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1098 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1099 | { |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1100 | ALOGV("startOutput() output %d, stream %d, session %d", |
| 1101 | output, stream, session); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1102 | ssize_t index = mOutputs.indexOfKey(output); |
| 1103 | if (index < 0) { |
| 1104 | ALOGW("startOutput() unknown output %d", output); |
| 1105 | return BAD_VALUE; |
| 1106 | } |
| 1107 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1108 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
| 1109 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1110 | status_t status = outputDesc->start(); |
| 1111 | if (status != NO_ERROR) { |
| 1112 | return status; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1113 | } |
| 1114 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1115 | // Routing? |
| 1116 | mOutputRoutes.incRouteActivity(session); |
| 1117 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1118 | audio_devices_t newDevice; |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1119 | AudioMix *policyMix = NULL; |
| 1120 | const char *address = NULL; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1121 | if (outputDesc->mPolicyMix != NULL) { |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1122 | policyMix = outputDesc->mPolicyMix; |
| 1123 | address = policyMix->mDeviceAddress.string(); |
| 1124 | if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 1125 | newDevice = policyMix->mDeviceType; |
| 1126 | } else { |
| 1127 | newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
| 1128 | } |
Eric Laurent | 2157f5b | 2018-04-25 18:33:02 -0700 | [diff] [blame] | 1129 | } else if (mOutputRoutes.getAndClearRouteChanged(session)) { |
Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 1130 | newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1131 | checkStrategyRoute(getStrategy(stream), output); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1132 | } else { |
| 1133 | newDevice = AUDIO_DEVICE_NONE; |
| 1134 | } |
| 1135 | |
| 1136 | uint32_t delayMs = 0; |
| 1137 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1138 | status = startSource(outputDesc, stream, newDevice, address, &delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1139 | |
| 1140 | if (status != NO_ERROR) { |
| 1141 | mOutputRoutes.decRouteActivity(session); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1142 | outputDesc->stop(); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1143 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1144 | } |
| 1145 | // Automatically enable the remote submix input when output is started on a re routing mix |
| 1146 | // of type MIX_TYPE_RECORDERS |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1147 | if (audio_is_remote_submix_device(newDevice) && policyMix != NULL && |
| 1148 | policyMix->mMixType == MIX_TYPE_RECORDERS) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1149 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1150 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1151 | address, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1152 | "remote-submix"); |
| 1153 | } |
| 1154 | |
| 1155 | if (delayMs != 0) { |
| 1156 | usleep(delayMs * 1000); |
| 1157 | } |
| 1158 | |
| 1159 | return status; |
| 1160 | } |
| 1161 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 1162 | status_t AudioPolicyManager::startSource(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1163 | audio_stream_type_t stream, |
| 1164 | audio_devices_t device, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1165 | const char *address, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1166 | uint32_t *delayMs) |
| 1167 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1168 | // cannot start playback of STREAM_TTS if any other output is being used |
| 1169 | uint32_t beaconMuteLatency = 0; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1170 | |
| 1171 | *delayMs = 0; |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1172 | if (stream == AUDIO_STREAM_TTS) { |
| 1173 | ALOGV("\t found BEACON stream"); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 1174 | if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1175 | return INVALID_OPERATION; |
| 1176 | } else { |
| 1177 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); |
| 1178 | } |
| 1179 | } else { |
| 1180 | // some playback other than beacon starts |
| 1181 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); |
| 1182 | } |
| 1183 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1184 | // 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] | 1185 | // check active before incrementing usage count |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1186 | bool force = !outputDesc->isActive() && |
| 1187 | (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1188 | |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1189 | // requiresMuteCheck is false when we can bypass mute strategy. |
| 1190 | // It covers a common case when there is no materially active audio |
| 1191 | // and muting would result in unnecessary delay and dropped audio. |
| 1192 | const uint32_t outputLatencyMs = outputDesc->latency(); |
| 1193 | bool requiresMuteCheck = outputDesc->isActive(outputLatencyMs * 2); // account for drain |
| 1194 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1195 | // increment usage count for this stream on the requested output: |
| 1196 | // NOTE that the usage count is the same for duplicated output and hardware output which is |
| 1197 | // necessary for a correct control of hardware output routing by startOutput() and stopOutput() |
| 1198 | outputDesc->changeRefCount(stream, 1); |
| 1199 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1200 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1201 | selectOutputForMusicEffects(); |
| 1202 | } |
| 1203 | |
Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 1204 | if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1205 | // starting an output being rerouted? |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1206 | if (device == AUDIO_DEVICE_NONE) { |
| 1207 | device = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1208 | } |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1209 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1210 | routing_strategy strategy = getStrategy(stream); |
| 1211 | bool shouldWait = (strategy == STRATEGY_SONIFICATION) || |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1212 | (strategy == STRATEGY_SONIFICATION_RESPECTFUL) || |
| 1213 | (beaconMuteLatency > 0); |
| 1214 | uint32_t waitMs = beaconMuteLatency; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1215 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1216 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1217 | if (desc != outputDesc) { |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1218 | // An output has a shared device if |
| 1219 | // - managed by the same hw module |
| 1220 | // - supports the currently selected device |
| 1221 | const bool sharedDevice = outputDesc->sharesHwModuleWith(desc) |
| 1222 | && (desc->supportedDevices() & device) != AUDIO_DEVICE_NONE; |
| 1223 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1224 | // force a device change if any other output is: |
| 1225 | // - managed by the same hw module |
Jean-Michel Trivi | 4a5b481 | 2018-02-08 17:22:32 +0000 | [diff] [blame] | 1226 | // - supports currently selected device |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1227 | // - has a current device selection that differs from selected device. |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1228 | // - has an active audio patch |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1229 | // 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] | 1230 | // change the device currently selected by the other output. |
| 1231 | if (sharedDevice && |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1232 | desc->device() != device && |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1233 | desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1234 | force = true; |
| 1235 | } |
| 1236 | // 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] | 1237 | // a notification so that audio focus effect can propagate, or that a mute/unmute |
| 1238 | // event occurred for beacon |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1239 | const uint32_t latencyMs = desc->latency(); |
| 1240 | const bool isActive = desc->isActive(latencyMs * 2); // account for drain |
| 1241 | |
| 1242 | if (shouldWait && isActive && (waitMs < latencyMs)) { |
| 1243 | waitMs = latencyMs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1244 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1245 | |
| 1246 | // Require mute check if another output is on a shared device |
| 1247 | // and currently active to have proper drain and avoid pops. |
| 1248 | // Note restoring AudioTracks onto this output needs to invoke |
| 1249 | // a volume ramp if there is no mute. |
| 1250 | requiresMuteCheck |= sharedDevice && isActive; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1251 | } |
| 1252 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1253 | |
| 1254 | const uint32_t muteWaitMs = |
| 1255 | setOutputDevice(outputDesc, device, force, 0, NULL, address, requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1256 | |
| 1257 | // handle special case for sonification while in call |
| 1258 | if (isInCall()) { |
| 1259 | handleIncallSonification(stream, true, false); |
| 1260 | } |
| 1261 | |
| 1262 | // apply volume rules for current stream and device if necessary |
| 1263 | checkAndSetVolume(stream, |
Shuhei Miyazaki | 6fe7033 | 2017-07-31 15:21:28 +0900 | [diff] [blame] | 1264 | mVolumeCurves->getVolumeIndex(stream, outputDesc->device()), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1265 | outputDesc, |
Shuhei Miyazaki | 6fe7033 | 2017-07-31 15:21:28 +0900 | [diff] [blame] | 1266 | outputDesc->device()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1267 | |
| 1268 | // update the outputs if starting an output with a stream that can affect notification |
| 1269 | // routing |
| 1270 | handleNotificationRoutingForStream(stream); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1271 | |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 1272 | // force reevaluating accessibility routing when ringtone or alarm starts |
| 1273 | if (strategy == STRATEGY_SONIFICATION) { |
| 1274 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
| 1275 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1276 | |
| 1277 | if (waitMs > muteWaitMs) { |
| 1278 | *delayMs = waitMs - muteWaitMs; |
| 1279 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1280 | |
| 1281 | // FIXME: A device change (muteWaitMs > 0) likely introduces a volume change. |
| 1282 | // A volume change enacted by APM with 0 delay is not synchronous, as it goes |
| 1283 | // via AudioCommandThread to AudioFlinger. Hence it is possible that the volume |
| 1284 | // change occurs after the MixerThread starts and causes a stream volume |
| 1285 | // glitch. |
| 1286 | // |
| 1287 | // We do not introduce additional delay here. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1288 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1289 | |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 1290 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
| 1291 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
| 1292 | setStrategyMute(STRATEGY_SONIFICATION, true, outputDesc); |
| 1293 | } |
| 1294 | |
Tomoharu Kasahara | a81f098 | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 1295 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
| 1296 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
| 1297 | setStrategyMute(STRATEGY_SONIFICATION, true, outputDesc); |
| 1298 | } |
| 1299 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1300 | return NO_ERROR; |
| 1301 | } |
| 1302 | |
| 1303 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1304 | status_t AudioPolicyManager::stopOutput(audio_io_handle_t output, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1305 | audio_stream_type_t stream, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1306 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1307 | { |
| 1308 | ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session); |
| 1309 | ssize_t index = mOutputs.indexOfKey(output); |
| 1310 | if (index < 0) { |
| 1311 | ALOGW("stopOutput() unknown output %d", output); |
| 1312 | return BAD_VALUE; |
| 1313 | } |
| 1314 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1315 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1316 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1317 | if (outputDesc->mRefCount[stream] == 1) { |
| 1318 | // Automatically disable the remote submix input when output is stopped on a |
| 1319 | // re routing mix of type MIX_TYPE_RECORDERS |
| 1320 | if (audio_is_remote_submix_device(outputDesc->mDevice) && |
| 1321 | outputDesc->mPolicyMix != NULL && |
| 1322 | outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1323 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1324 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1325 | outputDesc->mPolicyMix->mDeviceAddress, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1326 | "remote-submix"); |
| 1327 | } |
| 1328 | } |
| 1329 | |
| 1330 | // Routing? |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1331 | bool forceDeviceUpdate = false; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1332 | if (outputDesc->mRefCount[stream] > 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1333 | int activityCount = mOutputRoutes.decRouteActivity(session); |
| 1334 | forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0)); |
| 1335 | |
| 1336 | if (forceDeviceUpdate) { |
| 1337 | checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE); |
| 1338 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1339 | } |
| 1340 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1341 | status_t status = stopSource(outputDesc, stream, forceDeviceUpdate); |
| 1342 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1343 | if (status == NO_ERROR ) { |
| 1344 | outputDesc->stop(); |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1345 | } |
| 1346 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1347 | } |
| 1348 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 1349 | status_t AudioPolicyManager::stopSource(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1350 | audio_stream_type_t stream, |
| 1351 | bool forceDeviceUpdate) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1352 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1353 | // always handle stream stop, check which stream type is stopping |
| 1354 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); |
| 1355 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1356 | // handle special case for sonification while in call |
| 1357 | if (isInCall()) { |
| 1358 | handleIncallSonification(stream, false, false); |
| 1359 | } |
| 1360 | |
| 1361 | if (outputDesc->mRefCount[stream] > 0) { |
| 1362 | // decrement usage count of this stream on the output |
| 1363 | outputDesc->changeRefCount(stream, -1); |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1364 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1365 | // store time at which the stream was stopped - see isStreamActive() |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1366 | if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1367 | outputDesc->mStopTime[stream] = systemTime(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1368 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1369 | // delay the device switch by twice the latency because stopOutput() is executed when |
| 1370 | // the track stop() command is received and at that time the audio track buffer can |
| 1371 | // still contain data that needs to be drained. The latency only covers the audio HAL |
| 1372 | // and kernel buffers. Also the latency does not always include additional delay in the |
| 1373 | // audio path (audio DSP, CODEC ...) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1374 | setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1375 | |
| 1376 | // force restoring the device selection on other active outputs if it differs from the |
| 1377 | // one being selected for this output |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1378 | uint32_t delayMs = outputDesc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1379 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1380 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1381 | if (desc != outputDesc && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1382 | desc->isActive() && |
| 1383 | outputDesc->sharesHwModuleWith(desc) && |
| 1384 | (newDevice != desc->device())) { |
Haynes Mathew George | 11c499a | 2016-08-26 12:08:25 -0700 | [diff] [blame] | 1385 | audio_devices_t newDevice2 = getNewOutputDevice(desc, false /*fromCache*/); |
| 1386 | bool force = desc->device() != newDevice2; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1387 | setOutputDevice(desc, |
Haynes Mathew George | 11c499a | 2016-08-26 12:08:25 -0700 | [diff] [blame] | 1388 | newDevice2, |
| 1389 | force, |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1390 | delayMs); |
| 1391 | // re-apply device specific volume if not done by setOutputDevice() |
| 1392 | if (!force) { |
| 1393 | applyStreamVolumes(desc, newDevice2, delayMs); |
| 1394 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1395 | } |
| 1396 | } |
| 1397 | // update the outputs if stopping one with a stream that can affect notification routing |
| 1398 | handleNotificationRoutingForStream(stream); |
| 1399 | } |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 1400 | |
| 1401 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
| 1402 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
| 1403 | setStrategyMute(STRATEGY_SONIFICATION, false, outputDesc); |
| 1404 | } |
| 1405 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1406 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1407 | selectOutputForMusicEffects(); |
| 1408 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1409 | return NO_ERROR; |
| 1410 | } else { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1411 | ALOGW("stopOutput() refcount is already 0"); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1412 | return INVALID_OPERATION; |
| 1413 | } |
| 1414 | } |
| 1415 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1416 | void AudioPolicyManager::releaseOutput(audio_io_handle_t output, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1417 | audio_stream_type_t stream __unused, |
| 1418 | audio_session_t session __unused) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1419 | { |
| 1420 | ALOGV("releaseOutput() %d", output); |
| 1421 | ssize_t index = mOutputs.indexOfKey(output); |
| 1422 | if (index < 0) { |
| 1423 | ALOGW("releaseOutput() releasing unknown output %d", output); |
| 1424 | return; |
| 1425 | } |
| 1426 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1427 | // Routing |
| 1428 | mOutputRoutes.removeRoute(session); |
| 1429 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1430 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1431 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1432 | if (desc->mDirectOpenCount <= 0) { |
| 1433 | ALOGW("releaseOutput() invalid open count %d for output %d", |
| 1434 | desc->mDirectOpenCount, output); |
| 1435 | return; |
| 1436 | } |
| 1437 | if (--desc->mDirectOpenCount == 0) { |
| 1438 | closeOutput(output); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1439 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1440 | } |
| 1441 | } |
| 1442 | } |
| 1443 | |
| 1444 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1445 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, |
| 1446 | audio_io_handle_t *input, |
| 1447 | audio_session_t session, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1448 | uid_t uid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1449 | const audio_config_base_t *config, |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1450 | audio_input_flags_t flags, |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1451 | audio_port_handle_t *selectedDeviceId, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1452 | input_type_t *inputType, |
| 1453 | audio_port_handle_t *portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1454 | { |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1455 | ALOGV("getInputForAttr() source %d, sampling rate %d, format %#x, channel mask %#x," |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1456 | "session %d, flags %#x", |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1457 | attr->source, config->sample_rate, config->format, config->channel_mask, session, flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1458 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1459 | status_t status = NO_ERROR; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1460 | // handle legacy remote submix case where the address was not always specified |
| 1461 | String8 address = String8(""); |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1462 | audio_source_t halInputSource; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1463 | audio_source_t inputSource = attr->source; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1464 | AudioMix *policyMix = NULL; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1465 | DeviceVector inputDevices; |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1466 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1467 | if (inputSource == AUDIO_SOURCE_DEFAULT) { |
| 1468 | inputSource = AUDIO_SOURCE_MIC; |
| 1469 | } |
| 1470 | |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1471 | // Explicit routing? |
| 1472 | sp<DeviceDescriptor> deviceDesc; |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1473 | if (*selectedDeviceId != AUDIO_PORT_HANDLE_NONE) { |
jiabin | c0c831a | 2018-01-29 17:55:15 -0800 | [diff] [blame] | 1474 | deviceDesc = mAvailableInputDevices.getDeviceFromId(*selectedDeviceId); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1475 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1476 | mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1477 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1478 | // special case for mmap capture: if an input IO handle is specified, we reuse this input if |
| 1479 | // possible |
| 1480 | if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) == AUDIO_INPUT_FLAG_MMAP_NOIRQ && |
| 1481 | *input != AUDIO_IO_HANDLE_NONE) { |
| 1482 | ssize_t index = mInputs.indexOfKey(*input); |
| 1483 | if (index < 0) { |
| 1484 | ALOGW("getInputForAttr() unknown MMAP input %d", *input); |
| 1485 | status = BAD_VALUE; |
| 1486 | goto error; |
| 1487 | } |
| 1488 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
| 1489 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
| 1490 | if (audioSession == 0) { |
| 1491 | ALOGW("getInputForAttr() unknown session %d on input %d", session, *input); |
| 1492 | status = BAD_VALUE; |
| 1493 | goto error; |
| 1494 | } |
| 1495 | // For MMAP mode, the first call to getInputForAttr() is made on behalf of audioflinger. |
| 1496 | // 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] | 1497 | // corresponds to a new client and is only permitted from the same UID. |
| 1498 | // 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] | 1499 | if (audioSession->openCount() == 1) { |
| 1500 | audioSession->setUid(uid); |
| 1501 | } else if (audioSession->uid() != uid) { |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 1502 | if (!audioSession->isSilenced()) { |
| 1503 | ALOGW("getInputForAttr() bad uid %d for session %d uid %d", |
| 1504 | uid, session, audioSession->uid()); |
| 1505 | status = INVALID_OPERATION; |
| 1506 | goto error; |
| 1507 | } |
| 1508 | audioSession->setUid(uid); |
| 1509 | audioSession->setSilenced(false); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1510 | } |
| 1511 | audioSession->changeOpenCount(1); |
| 1512 | *inputType = API_INPUT_LEGACY; |
| 1513 | if (*portId == AUDIO_PORT_HANDLE_NONE) { |
| 1514 | *portId = AudioPort::getNextUniqueId(); |
| 1515 | } |
| 1516 | inputDevices = mAvailableInputDevices.getDevicesFromType(inputDesc->mDevice); |
| 1517 | *selectedDeviceId = inputDevices.size() > 0 ? inputDevices.itemAt(0)->getId() |
| 1518 | : AUDIO_PORT_HANDLE_NONE; |
| 1519 | ALOGI("%s reusing MMAP input %d for session %d", __FUNCTION__, *input, session); |
| 1520 | |
| 1521 | return NO_ERROR; |
| 1522 | } |
| 1523 | |
| 1524 | *input = AUDIO_IO_HANDLE_NONE; |
| 1525 | *inputType = API_INPUT_INVALID; |
| 1526 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1527 | halInputSource = inputSource; |
| 1528 | |
| 1529 | // TODO: check for existing client for this port ID |
| 1530 | if (*portId == AUDIO_PORT_HANDLE_NONE) { |
| 1531 | *portId = AudioPort::getNextUniqueId(); |
| 1532 | } |
| 1533 | |
| 1534 | audio_devices_t device; |
| 1535 | |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1536 | if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX && |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1537 | strncmp(attr->tags, "addr=", strlen("addr=")) == 0) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1538 | status = mPolicyMixes.getInputMixForAttr(*attr, &policyMix); |
| 1539 | if (status != NO_ERROR) { |
| 1540 | goto error; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 1541 | } |
| 1542 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1543 | device = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 1544 | address = String8(attr->tags + strlen("addr=")); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1545 | } else { |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1546 | device = getDeviceAndMixForInputSource(inputSource, &policyMix); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1547 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1548 | ALOGW("getInputForAttr() could not find device for source %d", inputSource); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1549 | status = BAD_VALUE; |
| 1550 | goto error; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1551 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1552 | if (policyMix != NULL) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1553 | address = policyMix->mDeviceAddress; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1554 | if (policyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1555 | // there is an external policy, but this input is attached to a mix of recorders, |
| 1556 | // meaning it receives audio injected into the framework, so the recorder doesn't |
| 1557 | // know about it and is therefore considered "legacy" |
| 1558 | *inputType = API_INPUT_LEGACY; |
| 1559 | } else { |
| 1560 | // recording a mix of players defined by an external policy, we're rerouting for |
| 1561 | // an external policy |
| 1562 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
| 1563 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1564 | } else if (audio_is_remote_submix_device(device)) { |
| 1565 | address = String8("0"); |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1566 | *inputType = API_INPUT_MIX_CAPTURE; |
Eric Laurent | 82db269 | 2015-08-07 13:59:42 -0700 | [diff] [blame] | 1567 | } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) { |
| 1568 | *inputType = API_INPUT_TELEPHONY_RX; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1569 | } else { |
| 1570 | *inputType = API_INPUT_LEGACY; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1571 | } |
Ravi Kumar Alamanda | b367f5b | 2015-08-25 08:21:37 -0700 | [diff] [blame] | 1572 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1573 | } |
| 1574 | |
| 1575 | *input = getInputForDevice(device, address, session, uid, inputSource, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1576 | config, flags, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1577 | policyMix); |
| 1578 | if (*input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1579 | status = INVALID_OPERATION; |
| 1580 | goto error; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1581 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1582 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1583 | inputDevices = mAvailableInputDevices.getDevicesFromType(device); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1584 | *selectedDeviceId = inputDevices.size() > 0 ? inputDevices.itemAt(0)->getId() |
| 1585 | : AUDIO_PORT_HANDLE_NONE; |
| 1586 | |
| 1587 | ALOGV("getInputForAttr() returns input %d type %d selectedDeviceId %d", |
| 1588 | *input, *inputType, *selectedDeviceId); |
| 1589 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1590 | return NO_ERROR; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1591 | |
| 1592 | error: |
| 1593 | mInputRoutes.removeRoute(session); |
| 1594 | return status; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1595 | } |
| 1596 | |
| 1597 | |
| 1598 | audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device, |
| 1599 | String8 address, |
| 1600 | audio_session_t session, |
| 1601 | uid_t uid, |
| 1602 | audio_source_t inputSource, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1603 | const audio_config_base_t *config, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1604 | audio_input_flags_t flags, |
| 1605 | AudioMix *policyMix) |
| 1606 | { |
| 1607 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 1608 | audio_source_t halInputSource = inputSource; |
| 1609 | bool isSoundTrigger = false; |
| 1610 | |
| 1611 | if (inputSource == AUDIO_SOURCE_HOTWORD) { |
| 1612 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); |
| 1613 | if (index >= 0) { |
| 1614 | input = mSoundTriggerSessions.valueFor(session); |
| 1615 | isSoundTrigger = true; |
| 1616 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); |
| 1617 | ALOGV("SoundTrigger capture on session %d input %d", session, input); |
| 1618 | } else { |
| 1619 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1620 | } |
Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame] | 1621 | } else if (inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION && |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1622 | audio_is_linear_pcm(config->format)) { |
Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame] | 1623 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_VOIP_TX); |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1624 | } |
| 1625 | |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1626 | // find a compatible input profile (not necessarily identical in parameters) |
| 1627 | sp<IOProfile> profile; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1628 | // sampling rate and flags may be updated by getInputProfile |
| 1629 | uint32_t profileSamplingRate = (config->sample_rate == 0) ? |
| 1630 | SAMPLE_RATE_HZ_DEFAULT : config->sample_rate; |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 1631 | audio_format_t profileFormat; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1632 | audio_channel_mask_t profileChannelMask = config->channel_mask; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1633 | audio_input_flags_t profileFlags = flags; |
| 1634 | for (;;) { |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 1635 | 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] | 1636 | profile = getInputProfile(device, address, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1637 | profileSamplingRate, profileFormat, profileChannelMask, |
| 1638 | profileFlags); |
| 1639 | if (profile != 0) { |
| 1640 | break; // success |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1641 | } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) { |
| 1642 | profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1643 | } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) { |
| 1644 | profileFlags = AUDIO_INPUT_FLAG_NONE; // retry |
| 1645 | } else { // fail |
Glenn Kasten | faa10a6 | 2017-05-25 15:52:05 -0700 | [diff] [blame] | 1646 | ALOGW("getInputForDevice() could not find profile for device 0x%X, " |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1647 | "sampling rate %u, format %#x, channel mask 0x%X, flags %#x", |
| 1648 | device, config->sample_rate, config->format, config->channel_mask, flags); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1649 | return input; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1650 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1651 | } |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1652 | // Pick input sampling rate if not specified by client |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1653 | uint32_t samplingRate = config->sample_rate; |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1654 | if (samplingRate == 0) { |
| 1655 | samplingRate = profileSamplingRate; |
| 1656 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1657 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1658 | if (profile->getModuleHandle() == 0) { |
| 1659 | ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName()); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1660 | return input; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1661 | } |
| 1662 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1663 | sp<AudioSession> audioSession = new AudioSession(session, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1664 | inputSource, |
| 1665 | config->format, |
| 1666 | samplingRate, |
| 1667 | config->channel_mask, |
| 1668 | flags, |
| 1669 | uid, |
| 1670 | isSoundTrigger, |
| 1671 | policyMix, mpClientInterface); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1672 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1673 | // FIXME: disable concurrent capture until UI is ready |
| 1674 | #if 0 |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1675 | // reuse an open input if possible |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1676 | sp<AudioInputDescriptor> reusedInputDesc; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1677 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 1678 | sp<AudioInputDescriptor> desc = mInputs.valueAt(i); |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1679 | // reuse input if: |
| 1680 | // - it shares the same profile |
| 1681 | // AND |
| 1682 | // - it is not a reroute submix input |
| 1683 | // AND |
| 1684 | // - it is: not used for sound trigger |
| 1685 | // OR |
| 1686 | // used for sound trigger and all clients use the same session ID |
| 1687 | // |
| 1688 | if ((profile == desc->mProfile) && |
| 1689 | (isSoundTrigger == desc->isSoundTrigger()) && |
| 1690 | !is_virtual_input_device(device)) { |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1691 | |
| 1692 | sp<AudioSession> as = desc->getAudioSession(session); |
| 1693 | if (as != 0) { |
| 1694 | // do not allow unmatching properties on same session |
| 1695 | if (as->matches(audioSession)) { |
| 1696 | as->changeOpenCount(1); |
| 1697 | } else { |
| 1698 | ALOGW("getInputForDevice() record with different attributes" |
| 1699 | " exists for session %d", session); |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1700 | continue; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1701 | } |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1702 | } else if (isSoundTrigger) { |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1703 | continue; |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1704 | } |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1705 | |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1706 | // Reuse the already opened input stream on this profile if: |
| 1707 | // - the new capture source is background OR |
| 1708 | // - the path requested configurations match OR |
| 1709 | // - the new source priority is less than the highest source priority on this input |
| 1710 | // If the input stream cannot be reused, close it before opening a new stream |
| 1711 | // on the same profile for the new client so that the requested path configuration |
| 1712 | // can be selected. |
| 1713 | if (!isConcurrentSource(inputSource) && |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1714 | ((desc->mSamplingRate != samplingRate || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1715 | desc->mChannelMask != config->channel_mask || |
| 1716 | !audio_formats_match(desc->mFormat, config->format)) && |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1717 | (source_priority(desc->getHighestPrioritySource(false /*activeOnly*/)) < |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1718 | source_priority(inputSource)))) { |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1719 | reusedInputDesc = desc; |
| 1720 | continue; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1721 | } else { |
| 1722 | desc->addAudioSession(session, audioSession); |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1723 | ALOGV("%s: reusing input %d", __FUNCTION__, mInputs.keyAt(i)); |
| 1724 | return mInputs.keyAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1725 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1726 | } |
| 1727 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1728 | |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1729 | if (reusedInputDesc != 0) { |
| 1730 | AudioSessionCollection sessions = reusedInputDesc->getAudioSessions(false /*activeOnly*/); |
| 1731 | for (size_t j = 0; j < sessions.size(); j++) { |
| 1732 | audio_session_t currentSession = sessions.keyAt(j); |
| 1733 | stopInput(reusedInputDesc->mIoHandle, currentSession); |
| 1734 | releaseInput(reusedInputDesc->mIoHandle, currentSession); |
| 1735 | } |
| 1736 | } |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1737 | #endif |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1738 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1739 | if (!profile->canOpenNewIo()) { |
| 1740 | return AUDIO_IO_HANDLE_NONE; |
| 1741 | } |
| 1742 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1743 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1744 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1745 | audio_config_t lConfig = AUDIO_CONFIG_INITIALIZER; |
| 1746 | lConfig.sample_rate = profileSamplingRate; |
| 1747 | lConfig.channel_mask = profileChannelMask; |
| 1748 | lConfig.format = profileFormat; |
Eric Laurent | e301410 | 2017-05-03 11:15:43 -0700 | [diff] [blame] | 1749 | |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 1750 | if (address == "") { |
| 1751 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(device); |
| 1752 | // the inputs vector must be of size >= 1, but we don't want to crash here |
| 1753 | address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress : String8(""); |
| 1754 | } |
| 1755 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1756 | status_t status = inputDesc->open(&lConfig, device, address, |
| 1757 | halInputSource, profileFlags, &input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1758 | |
| 1759 | // only accept input with the exact requested set of parameters |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1760 | if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1761 | (profileSamplingRate != lConfig.sample_rate) || |
| 1762 | !audio_formats_match(profileFormat, lConfig.format) || |
| 1763 | (profileChannelMask != lConfig.channel_mask)) { |
| 1764 | ALOGW("getInputForAttr() failed opening input: sampling rate %d" |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1765 | ", format %#x, channel mask %#x", |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1766 | profileSamplingRate, profileFormat, profileChannelMask); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1767 | if (input != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1768 | inputDesc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1769 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1770 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1771 | } |
| 1772 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1773 | inputDesc->mPolicyMix = policyMix; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1774 | inputDesc->addAudioSession(session, audioSession); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1775 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1776 | addInput(input, inputDesc); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1777 | mpClientInterface->onAudioPortListUpdate(); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1778 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1779 | return input; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1780 | } |
| 1781 | |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1782 | //static |
| 1783 | bool AudioPolicyManager::isConcurrentSource(audio_source_t source) |
| 1784 | { |
| 1785 | return (source == AUDIO_SOURCE_HOTWORD) || |
| 1786 | (source == AUDIO_SOURCE_VOICE_RECOGNITION) || |
| 1787 | (source == AUDIO_SOURCE_FM_TUNER); |
| 1788 | } |
| 1789 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1790 | bool AudioPolicyManager::isConcurentCaptureAllowed(const sp<AudioInputDescriptor>& inputDesc, |
| 1791 | const sp<AudioSession>& audioSession) |
| 1792 | { |
| 1793 | // Do not allow capture if an active voice call is using a software patch and |
| 1794 | // the call TX source device is on the same HW module. |
| 1795 | // FIXME: would be better to refine to only inputs whose profile connects to the |
| 1796 | // call TX device but this information is not in the audio patch |
| 1797 | if (mCallTxPatch != 0 && |
| 1798 | inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) { |
| 1799 | return false; |
| 1800 | } |
| 1801 | |
| 1802 | // starting concurrent capture is enabled if: |
| 1803 | // 1) capturing for re-routing |
| 1804 | // 2) capturing for HOTWORD source |
| 1805 | // 3) capturing for FM TUNER source |
| 1806 | // 3) All other active captures are either for re-routing or HOTWORD |
| 1807 | |
| 1808 | if (is_virtual_input_device(inputDesc->mDevice) || |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1809 | isConcurrentSource(audioSession->inputSource())) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1810 | return true; |
| 1811 | } |
| 1812 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1813 | for (const auto& activeInput : mInputs.getActiveInputs()) { |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1814 | if (!isConcurrentSource(activeInput->inputSource(true)) && |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1815 | !is_virtual_input_device(activeInput->mDevice)) { |
| 1816 | return false; |
| 1817 | } |
| 1818 | } |
| 1819 | |
| 1820 | return true; |
| 1821 | } |
| 1822 | |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 1823 | // FIXME: remove when concurrent capture is ready. This is a hack to work around bug b/63083537. |
| 1824 | bool AudioPolicyManager::soundTriggerSupportsConcurrentCapture() { |
| 1825 | if (!mHasComputedSoundTriggerSupportsConcurrentCapture) { |
| 1826 | bool soundTriggerSupportsConcurrentCapture = false; |
| 1827 | unsigned int numModules = 0; |
| 1828 | struct sound_trigger_module_descriptor* nModules = NULL; |
| 1829 | |
| 1830 | status_t status = SoundTrigger::listModules(nModules, &numModules); |
| 1831 | if (status == NO_ERROR && numModules != 0) { |
| 1832 | nModules = (struct sound_trigger_module_descriptor*) calloc( |
| 1833 | numModules, sizeof(struct sound_trigger_module_descriptor)); |
| 1834 | if (nModules == NULL) { |
| 1835 | // We failed to malloc the buffer, so just say no for now, and hope that we have more |
| 1836 | // ram the next time this function is called. |
| 1837 | ALOGE("Failed to allocate buffer for module descriptors"); |
| 1838 | return false; |
| 1839 | } |
| 1840 | |
| 1841 | status = SoundTrigger::listModules(nModules, &numModules); |
| 1842 | if (status == NO_ERROR) { |
| 1843 | soundTriggerSupportsConcurrentCapture = true; |
| 1844 | for (size_t i = 0; i < numModules; ++i) { |
| 1845 | soundTriggerSupportsConcurrentCapture &= |
| 1846 | nModules[i].properties.concurrent_capture; |
| 1847 | } |
| 1848 | } |
| 1849 | free(nModules); |
| 1850 | } |
| 1851 | mSoundTriggerSupportsConcurrentCapture = soundTriggerSupportsConcurrentCapture; |
| 1852 | mHasComputedSoundTriggerSupportsConcurrentCapture = true; |
| 1853 | } |
| 1854 | return mSoundTriggerSupportsConcurrentCapture; |
| 1855 | } |
| 1856 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1857 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1858 | status_t AudioPolicyManager::startInput(audio_io_handle_t input, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1859 | audio_session_t session, |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1860 | bool silenced, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1861 | concurrency_type__mask_t *concurrency) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1862 | { |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1863 | |
| 1864 | ALOGV("AudioPolicyManager::startInput(input:%d, session:%d, silenced:%d, concurrency:%d)", |
| 1865 | input, session, silenced, *concurrency); |
| 1866 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1867 | *concurrency = API_INPUT_CONCURRENCY_NONE; |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1868 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1869 | ssize_t index = mInputs.indexOfKey(input); |
| 1870 | if (index < 0) { |
| 1871 | ALOGW("startInput() unknown input %d", input); |
| 1872 | return BAD_VALUE; |
| 1873 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1874 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1875 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1876 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
| 1877 | if (audioSession == 0) { |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1878 | ALOGW("startInput() unknown session %d on input %d", session, input); |
| 1879 | return BAD_VALUE; |
| 1880 | } |
| 1881 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1882 | // FIXME: disable concurrent capture until UI is ready |
| 1883 | #if 0 |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1884 | if (!isConcurentCaptureAllowed(inputDesc, audioSession)) { |
| 1885 | ALOGW("startInput(%d) failed: other input already started", input); |
| 1886 | return INVALID_OPERATION; |
| 1887 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1888 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1889 | if (isInCall()) { |
| 1890 | *concurrency |= API_INPUT_CONCURRENCY_CALL; |
| 1891 | } |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 1892 | if (mInputs.activeInputsCountOnDevices() != 0) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1893 | *concurrency |= API_INPUT_CONCURRENCY_CAPTURE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1894 | } |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1895 | #else |
| 1896 | if (!is_virtual_input_device(inputDesc->mDevice)) { |
| 1897 | if (mCallTxPatch != 0 && |
| 1898 | inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) { |
| 1899 | ALOGW("startInput(%d) failed: call in progress", input); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 1900 | *concurrency |= API_INPUT_CONCURRENCY_CALL; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1901 | return INVALID_OPERATION; |
| 1902 | } |
| 1903 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1904 | Vector<sp<AudioInputDescriptor>> activeInputs = mInputs.getActiveInputs(); |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1905 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1906 | // If a UID is idle and records silence and another not silenced recording starts |
| 1907 | // from another UID (idle or active) we stop the current idle UID recording in |
| 1908 | // favor of the new one - "There can be only one" TM |
| 1909 | if (!silenced) { |
| 1910 | for (const auto& activeDesc : activeInputs) { |
| 1911 | if ((audioSession->flags() & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0 && |
| 1912 | activeDesc->getId() == inputDesc->getId()) { |
| 1913 | continue; |
| 1914 | } |
| 1915 | |
| 1916 | AudioSessionCollection activeSessions = activeDesc->getAudioSessions( |
| 1917 | true /*activeOnly*/); |
| 1918 | sp<AudioSession> activeSession = activeSessions.valueAt(0); |
| 1919 | if (activeSession->isSilenced()) { |
| 1920 | audio_io_handle_t activeInput = activeDesc->mIoHandle; |
| 1921 | audio_session_t activeSessionId = activeSession->session(); |
| 1922 | stopInput(activeInput, activeSessionId); |
| 1923 | releaseInput(activeInput, activeSessionId); |
| 1924 | ALOGV("startInput(%d) stopping silenced input %d", input, activeInput); |
| 1925 | activeInputs = mInputs.getActiveInputs(); |
| 1926 | } |
| 1927 | } |
| 1928 | } |
| 1929 | |
| 1930 | for (const auto& activeDesc : activeInputs) { |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 1931 | if ((audioSession->flags() & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0 && |
| 1932 | activeDesc->getId() == inputDesc->getId()) { |
| 1933 | continue; |
| 1934 | } |
| 1935 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1936 | audio_source_t activeSource = activeDesc->inputSource(true); |
| 1937 | if (audioSession->inputSource() == AUDIO_SOURCE_HOTWORD) { |
| 1938 | if (activeSource == AUDIO_SOURCE_HOTWORD) { |
| 1939 | if (activeDesc->hasPreemptedSession(session)) { |
| 1940 | ALOGW("startInput(%d) failed for HOTWORD: " |
| 1941 | "other input %d already started for HOTWORD", |
| 1942 | input, activeDesc->mIoHandle); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 1943 | *concurrency |= API_INPUT_CONCURRENCY_HOTWORD; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1944 | return INVALID_OPERATION; |
| 1945 | } |
| 1946 | } else { |
| 1947 | ALOGV("startInput(%d) failed for HOTWORD: other input %d already started", |
| 1948 | input, activeDesc->mIoHandle); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 1949 | *concurrency |= API_INPUT_CONCURRENCY_CAPTURE; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1950 | return INVALID_OPERATION; |
| 1951 | } |
| 1952 | } else { |
| 1953 | if (activeSource != AUDIO_SOURCE_HOTWORD) { |
| 1954 | ALOGW("startInput(%d) failed: other input %d already started", |
| 1955 | input, activeDesc->mIoHandle); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 1956 | *concurrency |= API_INPUT_CONCURRENCY_CAPTURE; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1957 | return INVALID_OPERATION; |
| 1958 | } |
| 1959 | } |
| 1960 | } |
| 1961 | |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 1962 | // We only need to check if the sound trigger session supports concurrent capture if the |
| 1963 | // input is also a sound trigger input. Otherwise, we should preempt any hotword stream |
| 1964 | // that's running. |
| 1965 | const bool allowConcurrentWithSoundTrigger = |
| 1966 | inputDesc->isSoundTrigger() ? soundTriggerSupportsConcurrentCapture() : false; |
| 1967 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1968 | // if capture is allowed, preempt currently active HOTWORD captures |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1969 | for (const auto& activeDesc : activeInputs) { |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 1970 | if (allowConcurrentWithSoundTrigger && activeDesc->isSoundTrigger()) { |
| 1971 | continue; |
| 1972 | } |
| 1973 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1974 | audio_source_t activeSource = activeDesc->inputSource(true); |
| 1975 | if (activeSource == AUDIO_SOURCE_HOTWORD) { |
| 1976 | AudioSessionCollection activeSessions = |
| 1977 | activeDesc->getAudioSessions(true /*activeOnly*/); |
| 1978 | audio_session_t activeSession = activeSessions.keyAt(0); |
| 1979 | audio_io_handle_t activeHandle = activeDesc->mIoHandle; |
| 1980 | SortedVector<audio_session_t> sessions = activeDesc->getPreemptedSessions(); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 1981 | *concurrency |= API_INPUT_CONCURRENCY_PREEMPT; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1982 | sessions.add(activeSession); |
| 1983 | inputDesc->setPreemptedSessions(sessions); |
| 1984 | stopInput(activeHandle, activeSession); |
| 1985 | releaseInput(activeHandle, activeSession); |
| 1986 | ALOGV("startInput(%d) for HOTWORD preempting HOTWORD input %d", |
| 1987 | input, activeDesc->mIoHandle); |
| 1988 | } |
| 1989 | } |
| 1990 | } |
| 1991 | #endif |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1992 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1993 | // Make sure we start with the correct silence state |
| 1994 | audioSession->setSilenced(silenced); |
| 1995 | |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 1996 | // increment activity count before calling getNewInputDevice() below as only active sessions |
| 1997 | // are considered for device selection |
| 1998 | audioSession->changeActiveCount(1); |
| 1999 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2000 | // Routing? |
| 2001 | mInputRoutes.incRouteActivity(session); |
| 2002 | |
Eric Laurent | 2157f5b | 2018-04-25 18:33:02 -0700 | [diff] [blame] | 2003 | if (audioSession->activeCount() == 1 || mInputRoutes.getAndClearRouteChanged(session)) { |
Eric Laurent | 271a93e | 2016-09-29 14:47:06 -0700 | [diff] [blame] | 2004 | // indicate active capture to sound trigger service if starting capture from a mic on |
| 2005 | // primary HW module |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 2006 | audio_devices_t device = getNewInputDevice(inputDesc); |
Eric Laurent | 271a93e | 2016-09-29 14:47:06 -0700 | [diff] [blame] | 2007 | setInputDevice(input, device, true /* force */); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2008 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2009 | status_t status = inputDesc->start(); |
| 2010 | if (status != NO_ERROR) { |
| 2011 | mInputRoutes.decRouteActivity(session); |
| 2012 | audioSession->changeActiveCount(-1); |
| 2013 | return status; |
| 2014 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2015 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2016 | if (inputDesc->getAudioSessionCount(true/*activeOnly*/) == 1) { |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2017 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 2018 | if ((inputDesc->mPolicyMix != NULL) |
| 2019 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 2020 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
| 2021 | MIX_STATE_MIXING); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2022 | } |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2023 | |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 2024 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); |
| 2025 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
Eric Laurent | 05b345e | 2016-11-18 12:36:27 -0800 | [diff] [blame] | 2026 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) { |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2027 | SoundTrigger::setCaptureState(true); |
| 2028 | } |
| 2029 | |
| 2030 | // automatically enable the remote submix output when input is started if not |
| 2031 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 2032 | // For remote submix (a virtual device), we open only one input per capture request. |
| 2033 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 2034 | String8 address = String8(""); |
| 2035 | if (inputDesc->mPolicyMix == NULL) { |
| 2036 | address = String8("0"); |
| 2037 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 2038 | address = inputDesc->mPolicyMix->mDeviceAddress; |
| 2039 | } |
| 2040 | if (address != "") { |
| 2041 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2042 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2043 | address, "remote-submix"); |
| 2044 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2045 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 2046 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2047 | } |
| 2048 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2049 | ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2050 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2051 | return NO_ERROR; |
| 2052 | } |
| 2053 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2054 | status_t AudioPolicyManager::stopInput(audio_io_handle_t input, |
| 2055 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2056 | { |
| 2057 | ALOGV("stopInput() input %d", input); |
| 2058 | ssize_t index = mInputs.indexOfKey(input); |
| 2059 | if (index < 0) { |
| 2060 | ALOGW("stopInput() unknown input %d", input); |
| 2061 | return BAD_VALUE; |
| 2062 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2063 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2064 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2065 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2066 | if (index < 0) { |
| 2067 | ALOGW("stopInput() unknown session %d on input %d", session, input); |
| 2068 | return BAD_VALUE; |
| 2069 | } |
| 2070 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2071 | if (audioSession->activeCount() == 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2072 | ALOGW("stopInput() input %d already stopped", input); |
| 2073 | return INVALID_OPERATION; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2074 | } |
| 2075 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2076 | audioSession->changeActiveCount(-1); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2077 | |
| 2078 | // Routing? |
| 2079 | mInputRoutes.decRouteActivity(session); |
| 2080 | |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2081 | if (audioSession->activeCount() == 0) { |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2082 | inputDesc->stop(); |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2083 | if (inputDesc->isActive()) { |
| 2084 | setInputDevice(input, getNewInputDevice(inputDesc), false /* force */); |
| 2085 | } else { |
| 2086 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 2087 | if ((inputDesc->mPolicyMix != NULL) |
| 2088 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 2089 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
| 2090 | MIX_STATE_IDLE); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2091 | } |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2092 | |
| 2093 | // automatically disable the remote submix output when input is stopped if not |
| 2094 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 2095 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 2096 | String8 address = String8(""); |
| 2097 | if (inputDesc->mPolicyMix == NULL) { |
| 2098 | address = String8("0"); |
| 2099 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 2100 | address = inputDesc->mPolicyMix->mDeviceAddress; |
| 2101 | } |
| 2102 | if (address != "") { |
| 2103 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2104 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2105 | address, "remote-submix"); |
| 2106 | } |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2107 | } |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2108 | |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 2109 | audio_devices_t device = inputDesc->mDevice; |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 2110 | resetInputDevice(input); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2111 | |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 2112 | // indicate inactive capture to sound trigger service if stopping capture from a mic on |
| 2113 | // primary HW module |
| 2114 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); |
| 2115 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
| 2116 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 2117 | SoundTrigger::setCaptureState(false); |
| 2118 | } |
| 2119 | inputDesc->clearPreemptedSessions(); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2120 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2121 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2122 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2123 | } |
| 2124 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2125 | void AudioPolicyManager::releaseInput(audio_io_handle_t input, |
| 2126 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2127 | { |
| 2128 | ALOGV("releaseInput() %d", input); |
| 2129 | ssize_t index = mInputs.indexOfKey(input); |
| 2130 | if (index < 0) { |
| 2131 | ALOGW("releaseInput() releasing unknown input %d", input); |
| 2132 | return; |
| 2133 | } |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2134 | |
| 2135 | // Routing |
| 2136 | mInputRoutes.removeRoute(session); |
| 2137 | |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2138 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
| 2139 | ALOG_ASSERT(inputDesc != 0); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2140 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2141 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
vivek mehta | 587b8df | 2017-01-31 14:58:44 -0800 | [diff] [blame] | 2142 | if (audioSession == 0) { |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2143 | ALOGW("releaseInput() unknown session %d on input %d", session, input); |
| 2144 | return; |
| 2145 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2146 | |
| 2147 | if (audioSession->openCount() == 0) { |
| 2148 | ALOGW("releaseInput() invalid open count %d on session %d", |
| 2149 | audioSession->openCount(), session); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2150 | return; |
| 2151 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2152 | |
| 2153 | if (audioSession->changeOpenCount(-1) == 0) { |
| 2154 | inputDesc->removeAudioSession(session); |
| 2155 | } |
| 2156 | |
Jean-Michel Trivi | 65bfe91 | 2015-12-04 15:56:47 -0800 | [diff] [blame] | 2157 | if (inputDesc->getOpenRefCount() > 0) { |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2158 | ALOGV("releaseInput() exit > 0"); |
| 2159 | return; |
| 2160 | } |
| 2161 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2162 | closeInput(input); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2163 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2164 | ALOGV("releaseInput() exit"); |
| 2165 | } |
| 2166 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2167 | void AudioPolicyManager::closeAllInputs() { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2168 | bool patchRemoved = false; |
| 2169 | |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 2170 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2171 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index); |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 2172 | ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2173 | if (patch_index >= 0) { |
| 2174 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 2175 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2176 | mAudioPatches.removeItemsAt(patch_index); |
| 2177 | patchRemoved = true; |
| 2178 | } |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2179 | inputDesc->close(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2180 | } |
jiabin | 829a00b | 2018-04-04 16:28:32 -0700 | [diff] [blame] | 2181 | mInputRoutes.clear(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2182 | mInputs.clear(); |
Chris Thornton | 52785f3 | 2016-02-09 17:28:49 -0800 | [diff] [blame] | 2183 | SoundTrigger::setCaptureState(false); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2184 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2185 | |
| 2186 | if (patchRemoved) { |
| 2187 | mpClientInterface->onAudioPatchListUpdate(); |
| 2188 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2189 | } |
| 2190 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2191 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2192 | int indexMin, |
| 2193 | int indexMax) |
| 2194 | { |
| 2195 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2196 | mVolumeCurves->initStreamVolume(stream, indexMin, indexMax); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2197 | |
| 2198 | // initialize other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2199 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2200 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2201 | continue; |
| 2202 | } |
| 2203 | mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2204 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2205 | } |
| 2206 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2207 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2208 | int index, |
| 2209 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2210 | { |
| 2211 | |
Nadav Bar | 7c605f6 | 2017-12-25 00:01:52 +0200 | [diff] [blame] | 2212 | // VOICE_CALL stream has minVolumeIndex > 0 but can be muted directly by an |
| 2213 | // app that has MODIFY_PHONE_STATE permission. |
| 2214 | if (((index < mVolumeCurves->getVolumeIndexMin(stream)) && |
| 2215 | !(stream == AUDIO_STREAM_VOICE_CALL && index == 0)) || |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2216 | (index > mVolumeCurves->getVolumeIndexMax(stream))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2217 | return BAD_VALUE; |
| 2218 | } |
| 2219 | if (!audio_is_output_device(device)) { |
| 2220 | return BAD_VALUE; |
| 2221 | } |
| 2222 | |
| 2223 | // Force max volume if stream cannot be muted |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2224 | if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2225 | |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2226 | ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2227 | stream, device, index); |
| 2228 | |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2229 | // update other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2230 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2231 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2232 | continue; |
| 2233 | } |
| 2234 | mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index); |
| 2235 | } |
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 | // update volume on all outputs and streams matching the following: |
| 2238 | // - The requested stream (or a stream matching for volume control) is active on the output |
| 2239 | // - The device (or devices) selected by the strategy corresponding to this stream includes |
| 2240 | // the requested device |
| 2241 | // - For non default requested device, currently selected device on the output is either the |
| 2242 | // requested device or one of the devices selected by the strategy |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2243 | // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if |
| 2244 | // no specific device volume value exists for currently selected device. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2245 | status_t status = NO_ERROR; |
| 2246 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2247 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 2248 | audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device()); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2249 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2250 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2251 | continue; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2252 | } |
Eric Laurent | d3926fe | 2016-04-15 18:10:07 -0700 | [diff] [blame] | 2253 | if (!(desc->isStreamActive((audio_stream_type_t)curStream) || |
| 2254 | (isInCall() && (curStream == AUDIO_STREAM_VOICE_CALL)))) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2255 | continue; |
| 2256 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2257 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Jean-Michel Trivi | b7fdce6 | 2017-06-27 19:38:42 -0700 | [diff] [blame] | 2258 | audio_devices_t curStreamDevice = Volume::getDeviceForVolume(getDeviceForStrategy( |
| 2259 | curStrategy, false /*fromCache*/)); |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2260 | if ((device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) && |
| 2261 | ((curStreamDevice & device) == 0)) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2262 | continue; |
| 2263 | } |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2264 | bool applyVolume; |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2265 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2266 | curStreamDevice |= device; |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2267 | applyVolume = (curDevice & curStreamDevice) != 0; |
| 2268 | } else { |
| 2269 | applyVolume = !mVolumeCurves->hasVolumeIndexForDevice( |
Jean-Michel Trivi | b7fdce6 | 2017-06-27 19:38:42 -0700 | [diff] [blame] | 2270 | stream, curStreamDevice); |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2271 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2272 | |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2273 | if (applyVolume) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2274 | //FIXME: workaround for truncated touch sounds |
| 2275 | // delayed volume change for system stream to be removed when the problem is |
| 2276 | // handled by system UI |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2277 | status_t volStatus = |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2278 | checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice, |
| 2279 | (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2280 | if (volStatus != NO_ERROR) { |
| 2281 | status = volStatus; |
| 2282 | } |
| 2283 | } |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2284 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2285 | } |
| 2286 | return status; |
| 2287 | } |
| 2288 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2289 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2290 | int *index, |
| 2291 | audio_devices_t device) |
| 2292 | { |
| 2293 | if (index == NULL) { |
| 2294 | return BAD_VALUE; |
| 2295 | } |
| 2296 | if (!audio_is_output_device(device)) { |
| 2297 | return BAD_VALUE; |
| 2298 | } |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2299 | // 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] | 2300 | // the strategy the stream belongs to. |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2301 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2302 | device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); |
| 2303 | } |
François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 2304 | device = Volume::getDeviceForVolume(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2305 | |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2306 | *index = mVolumeCurves->getVolumeIndex(stream, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2307 | ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index); |
| 2308 | return NO_ERROR; |
| 2309 | } |
| 2310 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2311 | audio_io_handle_t AudioPolicyManager::selectOutputForMusicEffects() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2312 | { |
| 2313 | // select one output among several suitable for global effects. |
| 2314 | // The priority is as follows: |
| 2315 | // 1: An offloaded output. If the effect ends up not being offloadable, |
| 2316 | // AudioFlinger will invalidate the track and the offloaded output |
| 2317 | // will be closed causing the effect to be moved to a PCM output. |
| 2318 | // 2: A deep buffer output |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2319 | // 3: The primary output |
| 2320 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2321 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2322 | routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2323 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2324 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2325 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2326 | if (outputs.size() == 0) { |
| 2327 | return AUDIO_IO_HANDLE_NONE; |
| 2328 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2329 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2330 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 2331 | bool activeOnly = true; |
| 2332 | |
| 2333 | while (output == AUDIO_IO_HANDLE_NONE) { |
| 2334 | audio_io_handle_t outputOffloaded = AUDIO_IO_HANDLE_NONE; |
| 2335 | audio_io_handle_t outputDeepBuffer = AUDIO_IO_HANDLE_NONE; |
| 2336 | audio_io_handle_t outputPrimary = AUDIO_IO_HANDLE_NONE; |
| 2337 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2338 | for (audio_io_handle_t output : outputs) { |
| 2339 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2340 | if (activeOnly && !desc->isStreamActive(AUDIO_STREAM_MUSIC)) { |
| 2341 | continue; |
| 2342 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2343 | ALOGV("selectOutputForMusicEffects activeOnly %d output %d flags 0x%08x", |
| 2344 | activeOnly, output, desc->mFlags); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2345 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2346 | outputOffloaded = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2347 | } |
| 2348 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2349 | outputDeepBuffer = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2350 | } |
| 2351 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2352 | outputPrimary = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2353 | } |
| 2354 | } |
| 2355 | if (outputOffloaded != AUDIO_IO_HANDLE_NONE) { |
| 2356 | output = outputOffloaded; |
| 2357 | } else if (outputDeepBuffer != AUDIO_IO_HANDLE_NONE) { |
| 2358 | output = outputDeepBuffer; |
| 2359 | } else if (outputPrimary != AUDIO_IO_HANDLE_NONE) { |
| 2360 | output = outputPrimary; |
| 2361 | } else { |
| 2362 | output = outputs[0]; |
| 2363 | } |
| 2364 | activeOnly = false; |
| 2365 | } |
| 2366 | |
| 2367 | if (output != mMusicEffectOutput) { |
| 2368 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output); |
| 2369 | mMusicEffectOutput = output; |
| 2370 | } |
| 2371 | |
| 2372 | ALOGV("selectOutputForMusicEffects selected output %d", output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2373 | return output; |
| 2374 | } |
| 2375 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2376 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc __unused) |
| 2377 | { |
| 2378 | return selectOutputForMusicEffects(); |
| 2379 | } |
| 2380 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2381 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2382 | audio_io_handle_t io, |
| 2383 | uint32_t strategy, |
| 2384 | int session, |
| 2385 | int id) |
| 2386 | { |
| 2387 | ssize_t index = mOutputs.indexOfKey(io); |
| 2388 | if (index < 0) { |
| 2389 | index = mInputs.indexOfKey(io); |
| 2390 | if (index < 0) { |
| 2391 | ALOGW("registerEffect() unknown io %d", io); |
| 2392 | return INVALID_OPERATION; |
| 2393 | } |
| 2394 | } |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2395 | return mEffects.registerEffect(desc, io, strategy, session, id); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2396 | } |
| 2397 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2398 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2399 | { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2400 | bool active = false; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2401 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) { |
| 2402 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2403 | continue; |
| 2404 | } |
| 2405 | active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs); |
| 2406 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2407 | return active; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2408 | } |
| 2409 | |
| 2410 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2411 | { |
| 2412 | return mOutputs.isStreamActiveRemotely(stream, inPastMs); |
| 2413 | } |
| 2414 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2415 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2416 | { |
| 2417 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2418 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2419 | if (inputDescriptor->isSourceActive(source)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2420 | return true; |
| 2421 | } |
| 2422 | } |
| 2423 | return false; |
| 2424 | } |
| 2425 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2426 | // Register a list of custom mixes with their attributes and format. |
| 2427 | // When a mix is registered, corresponding input and output profiles are |
| 2428 | // added to the remote submix hw module. The profile contains only the |
| 2429 | // parameters (sampling rate, format...) specified by the mix. |
| 2430 | // The corresponding input remote submix device is also connected. |
| 2431 | // |
| 2432 | // When a remote submix device is connected, the address is checked to select the |
| 2433 | // appropriate profile and the corresponding input or output stream is opened. |
| 2434 | // |
| 2435 | // When capture starts, getInputForAttr() will: |
| 2436 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2437 | // - 2 if none found, getDeviceForInputSource() will: |
| 2438 | // - 2.1 look for a mix matching the attributes source |
| 2439 | // - 2.2 if none found, default to device selection by policy rules |
| 2440 | // At this time, the corresponding output remote submix device is also connected |
| 2441 | // and active playback use cases can be transferred to this mix if needed when reconnecting |
| 2442 | // after AudioTracks are invalidated |
| 2443 | // |
| 2444 | // When playback starts, getOutputForAttr() will: |
| 2445 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2446 | // - 2 if none found, look for a mix matching the attributes usage |
| 2447 | // - 3 if none found, default to device and output selection by policy rules. |
| 2448 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 2449 | status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes) |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2450 | { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2451 | ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size()); |
| 2452 | status_t res = NO_ERROR; |
| 2453 | |
| 2454 | sp<HwModule> rSubmixModule; |
| 2455 | // examine each mix's route type |
| 2456 | for (size_t i = 0; i < mixes.size(); i++) { |
| 2457 | // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination |
| 2458 | if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) { |
| 2459 | res = INVALID_OPERATION; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2460 | break; |
| 2461 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2462 | 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] | 2463 | 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] | 2464 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 2465 | rSubmixModule = mHwModules.getModuleFromName( |
| 2466 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 2467 | if (rSubmixModule == 0) { |
| 2468 | ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", |
| 2469 | i); |
| 2470 | res = INVALID_OPERATION; |
| 2471 | break; |
| 2472 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2473 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2474 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2475 | String8 address = mixes[i].mDeviceAddress; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2476 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2477 | if (mPolicyMixes.registerMix(address, mixes[i], 0 /*output desc*/) != NO_ERROR) { |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2478 | ALOGE(" Error registering mix %zu for address %s", i, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2479 | res = INVALID_OPERATION; |
| 2480 | break; |
| 2481 | } |
| 2482 | audio_config_t outputConfig = mixes[i].mFormat; |
| 2483 | audio_config_t inputConfig = mixes[i].mFormat; |
| 2484 | // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in |
| 2485 | // stereo and let audio flinger do the channel conversion if needed. |
| 2486 | outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 2487 | inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
| 2488 | rSubmixModule->addOutputProfile(address, &outputConfig, |
| 2489 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address); |
| 2490 | rSubmixModule->addInputProfile(address, &inputConfig, |
| 2491 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, address); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2492 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2493 | if (mixes[i].mMixType == MIX_TYPE_PLAYERS) { |
| 2494 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2495 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2496 | address.string(), "remote-submix"); |
| 2497 | } else { |
| 2498 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2499 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2500 | address.string(), "remote-submix"); |
| 2501 | } |
| 2502 | } 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] | 2503 | String8 address = mixes[i].mDeviceAddress; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2504 | audio_devices_t device = mixes[i].mDeviceType; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2505 | ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s", |
| 2506 | i, mixes.size(), device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2507 | |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2508 | bool foundOutput = false; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2509 | for (size_t j = 0 ; j < mOutputs.size() ; j++) { |
| 2510 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j); |
| 2511 | sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle()); |
| 2512 | if ((patch != 0) && (patch->mPatch.num_sinks != 0) |
| 2513 | && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE) |
| 2514 | && (patch->mPatch.sinks[0].ext.device.type == device) |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2515 | && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(), |
| 2516 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2517 | if (mPolicyMixes.registerMix(address, mixes[i], desc) != NO_ERROR) { |
| 2518 | res = INVALID_OPERATION; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2519 | } else { |
| 2520 | foundOutput = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2521 | } |
| 2522 | break; |
| 2523 | } |
| 2524 | } |
| 2525 | |
| 2526 | if (res != NO_ERROR) { |
| 2527 | ALOGE(" Error registering mix %zu for device 0x%X addr %s", |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2528 | i, device, address.string()); |
| 2529 | res = INVALID_OPERATION; |
| 2530 | break; |
| 2531 | } else if (!foundOutput) { |
| 2532 | ALOGE(" Output not found for mix %zu for device 0x%X addr %s", |
| 2533 | i, device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2534 | res = INVALID_OPERATION; |
| 2535 | break; |
| 2536 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2537 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2538 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2539 | if (res != NO_ERROR) { |
| 2540 | unregisterPolicyMixes(mixes); |
| 2541 | } |
| 2542 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2543 | } |
| 2544 | |
| 2545 | status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes) |
| 2546 | { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2547 | ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2548 | status_t res = NO_ERROR; |
| 2549 | sp<HwModule> rSubmixModule; |
| 2550 | // examine each mix's route type |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2551 | for (const auto& mix : mixes) { |
| 2552 | 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] | 2553 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2554 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 2555 | rSubmixModule = mHwModules.getModuleFromName( |
| 2556 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 2557 | if (rSubmixModule == 0) { |
| 2558 | res = INVALID_OPERATION; |
| 2559 | continue; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2560 | } |
| 2561 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2562 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2563 | String8 address = mix.mDeviceAddress; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2564 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2565 | if (mPolicyMixes.unregisterMix(address) != NO_ERROR) { |
| 2566 | res = INVALID_OPERATION; |
| 2567 | continue; |
| 2568 | } |
| 2569 | |
| 2570 | if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) == |
| 2571 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2572 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2573 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2574 | address.string(), "remote-submix"); |
| 2575 | } |
| 2576 | if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) == |
| 2577 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2578 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2579 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2580 | address.string(), "remote-submix"); |
| 2581 | } |
| 2582 | rSubmixModule->removeOutputProfile(address); |
| 2583 | rSubmixModule->removeInputProfile(address); |
| 2584 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2585 | } if ((mix.mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 2586 | if (mPolicyMixes.unregisterMix(mix.mDeviceAddress) != NO_ERROR) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2587 | res = INVALID_OPERATION; |
| 2588 | continue; |
| 2589 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2590 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2591 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2592 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2593 | } |
| 2594 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2595 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2596 | status_t AudioPolicyManager::dump(int fd) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2597 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2598 | String8 result; |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 2599 | result.appendFormat("\nAudioPolicyManager Dump: %p\n", this); |
| 2600 | result.appendFormat(" Primary Output: %d\n", |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 2601 | hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE); |
Mikhail Naganov | 0d6a033 | 2016-04-19 17:12:38 -0700 | [diff] [blame] | 2602 | std::string stateLiteral; |
| 2603 | AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral); |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 2604 | result.appendFormat(" Phone state: %s\n", stateLiteral.c_str()); |
| 2605 | const char* forceUses[AUDIO_POLICY_FORCE_USE_CNT] = { |
| 2606 | "communications", "media", "record", "dock", "system", |
| 2607 | "HDMI system audio", "encoded surround output", "vibrate ringing" }; |
| 2608 | for (audio_policy_force_use_t i = AUDIO_POLICY_FORCE_FOR_COMMUNICATION; |
| 2609 | i < AUDIO_POLICY_FORCE_USE_CNT; i = (audio_policy_force_use_t)((int)i + 1)) { |
| 2610 | result.appendFormat(" Force use for %s: %d\n", |
| 2611 | forceUses[i], mEngine->getForceUse(i)); |
| 2612 | } |
| 2613 | result.appendFormat(" TTS output %savailable\n", mTtsOutputAvailable ? "" : "not "); |
| 2614 | result.appendFormat(" Master mono: %s\n", mMasterMono ? "on" : "off"); |
| 2615 | result.appendFormat(" Config source: %s\n", getConfig().getSource().c_str()); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2616 | write(fd, result.string(), result.size()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2617 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 2618 | mAvailableOutputDevices.dump(fd, String8("Available output")); |
| 2619 | mAvailableInputDevices.dump(fd, String8("Available input")); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 2620 | mHwModulesAll.dump(fd); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2621 | mOutputs.dump(fd); |
| 2622 | mInputs.dump(fd); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2623 | mVolumeCurves->dump(fd); |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2624 | mEffects.dump(fd); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2625 | mAudioPatches.dump(fd); |
Mikhail Naganov | 44344b0 | 2016-12-13 11:21:02 -0800 | [diff] [blame] | 2626 | mPolicyMixes.dump(fd); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2627 | |
| 2628 | return NO_ERROR; |
| 2629 | } |
| 2630 | |
| 2631 | // This function checks for the parameters which can be offloaded. |
| 2632 | // This can be enhanced depending on the capability of the DSP and policy |
| 2633 | // of the system. |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2634 | bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2635 | { |
| 2636 | ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2637 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2638 | offloadInfo.sample_rate, offloadInfo.channel_mask, |
| 2639 | offloadInfo.format, |
| 2640 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, |
| 2641 | offloadInfo.has_video); |
| 2642 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 2643 | if (mMasterMono) { |
| 2644 | return false; // no offloading if mono is set. |
| 2645 | } |
| 2646 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2647 | // Check if offload has been disabled |
| 2648 | char propValue[PROPERTY_VALUE_MAX]; |
| 2649 | if (property_get("audio.offload.disable", propValue, "0")) { |
| 2650 | if (atoi(propValue) != 0) { |
| 2651 | ALOGV("offload disabled by audio.offload.disable=%s", propValue ); |
| 2652 | return false; |
| 2653 | } |
| 2654 | } |
| 2655 | |
| 2656 | // Check if stream type is music, then only allow offload as of now. |
| 2657 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) |
| 2658 | { |
| 2659 | ALOGV("isOffloadSupported: stream_type != MUSIC, returning false"); |
| 2660 | return false; |
| 2661 | } |
| 2662 | |
| 2663 | //TODO: enable audio offloading with video when ready |
Andy Hung | 08945c4 | 2015-05-31 21:36:46 -0700 | [diff] [blame] | 2664 | const bool allowOffloadWithVideo = |
| 2665 | property_get_bool("audio.offload.video", false /* default_value */); |
| 2666 | if (offloadInfo.has_video && !allowOffloadWithVideo) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2667 | ALOGV("isOffloadSupported: has_video == true, returning false"); |
| 2668 | return false; |
| 2669 | } |
| 2670 | |
| 2671 | //If duration is less than minimum value defined in property, return false |
| 2672 | if (property_get("audio.offload.min.duration.secs", propValue, NULL)) { |
| 2673 | if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) { |
| 2674 | ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue); |
| 2675 | return false; |
| 2676 | } |
| 2677 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { |
| 2678 | ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS); |
| 2679 | return false; |
| 2680 | } |
| 2681 | |
| 2682 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 2683 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 2684 | // detects there is an active non offloadable effect. |
| 2685 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 2686 | // This may prevent offloading in rare situations where effects are left active by apps |
| 2687 | // in the background. |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2688 | if (mEffects.isNonOffloadableEffectEnabled()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2689 | return false; |
| 2690 | } |
| 2691 | |
| 2692 | // See if there is a profile to support this. |
| 2693 | // AUDIO_DEVICE_NONE |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2694 | sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2695 | offloadInfo.sample_rate, |
| 2696 | offloadInfo.format, |
| 2697 | offloadInfo.channel_mask, |
| 2698 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2699 | ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT "); |
| 2700 | return (profile != 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2701 | } |
| 2702 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2703 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, |
| 2704 | audio_port_type_t type, |
| 2705 | unsigned int *num_ports, |
| 2706 | struct audio_port *ports, |
| 2707 | unsigned int *generation) |
| 2708 | { |
| 2709 | if (num_ports == NULL || (*num_ports != 0 && ports == NULL) || |
| 2710 | generation == NULL) { |
| 2711 | return BAD_VALUE; |
| 2712 | } |
| 2713 | ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports); |
| 2714 | if (ports == NULL) { |
| 2715 | *num_ports = 0; |
| 2716 | } |
| 2717 | |
| 2718 | size_t portsWritten = 0; |
| 2719 | size_t portsMax = *num_ports; |
| 2720 | *num_ports = 0; |
| 2721 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2722 | // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB |
| 2723 | // as they are used by stub HALs by convention |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2724 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2725 | for (const auto& dev : mAvailableOutputDevices) { |
| 2726 | if (dev->type() == AUDIO_DEVICE_OUT_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2727 | continue; |
| 2728 | } |
| 2729 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2730 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2731 | } |
| 2732 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2733 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2734 | } |
| 2735 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2736 | for (const auto& dev : mAvailableInputDevices) { |
| 2737 | if (dev->type() == AUDIO_DEVICE_IN_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2738 | continue; |
| 2739 | } |
| 2740 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2741 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2742 | } |
| 2743 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2744 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2745 | } |
| 2746 | } |
| 2747 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { |
| 2748 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 2749 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { |
| 2750 | mInputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2751 | } |
| 2752 | *num_ports += mInputs.size(); |
| 2753 | } |
| 2754 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2755 | size_t numOutputs = 0; |
| 2756 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2757 | if (!mOutputs[i]->isDuplicated()) { |
| 2758 | numOutputs++; |
| 2759 | if (portsWritten < portsMax) { |
| 2760 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2761 | } |
| 2762 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2763 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2764 | *num_ports += numOutputs; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2765 | } |
| 2766 | } |
| 2767 | *generation = curAudioPortGeneration(); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2768 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2769 | return NO_ERROR; |
| 2770 | } |
| 2771 | |
| 2772 | status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused) |
| 2773 | { |
| 2774 | return NO_ERROR; |
| 2775 | } |
| 2776 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2777 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, |
| 2778 | audio_patch_handle_t *handle, |
| 2779 | uid_t uid) |
| 2780 | { |
| 2781 | ALOGV("createAudioPatch()"); |
| 2782 | |
| 2783 | if (handle == NULL || patch == NULL) { |
| 2784 | return BAD_VALUE; |
| 2785 | } |
| 2786 | ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks); |
| 2787 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2788 | if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX || |
| 2789 | patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) { |
| 2790 | return BAD_VALUE; |
| 2791 | } |
| 2792 | // only one source per audio patch supported for now |
| 2793 | if (patch->num_sources > 1) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2794 | return INVALID_OPERATION; |
| 2795 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2796 | |
| 2797 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2798 | return INVALID_OPERATION; |
| 2799 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2800 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2801 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { |
| 2802 | return INVALID_OPERATION; |
| 2803 | } |
| 2804 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2805 | |
| 2806 | sp<AudioPatch> patchDesc; |
| 2807 | ssize_t index = mAudioPatches.indexOfKey(*handle); |
| 2808 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2809 | ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id, |
| 2810 | patch->sources[0].role, |
| 2811 | patch->sources[0].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2812 | #if LOG_NDEBUG == 0 |
| 2813 | for (size_t i = 0; i < patch->num_sinks; i++) { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2814 | 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] | 2815 | patch->sinks[i].role, |
| 2816 | patch->sinks[i].type); |
| 2817 | } |
| 2818 | #endif |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2819 | |
| 2820 | if (index >= 0) { |
| 2821 | patchDesc = mAudioPatches.valueAt(index); |
| 2822 | ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 2823 | mUidCached, patchDesc->mUid, uid); |
| 2824 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 2825 | return INVALID_OPERATION; |
| 2826 | } |
| 2827 | } else { |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 2828 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2829 | } |
| 2830 | |
| 2831 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2832 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2833 | if (outputDesc == NULL) { |
| 2834 | ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id); |
| 2835 | return BAD_VALUE; |
| 2836 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2837 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", |
| 2838 | outputDesc->mIoHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2839 | if (patchDesc != 0) { |
| 2840 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
| 2841 | ALOGV("createAudioPatch() source id differs for patch current id %d new id %d", |
| 2842 | patchDesc->mPatch.sources[0].id, patch->sources[0].id); |
| 2843 | return BAD_VALUE; |
| 2844 | } |
| 2845 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2846 | DeviceVector devices; |
| 2847 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2848 | // Only support mix to devices connection |
| 2849 | // TODO add support for mix to mix connection |
| 2850 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 2851 | ALOGV("createAudioPatch() source mix but sink is not a device"); |
| 2852 | return INVALID_OPERATION; |
| 2853 | } |
| 2854 | sp<DeviceDescriptor> devDesc = |
| 2855 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 2856 | if (devDesc == 0) { |
| 2857 | ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id); |
| 2858 | return BAD_VALUE; |
| 2859 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2860 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2861 | if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2862 | devDesc->mAddress, |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2863 | patch->sources[0].sample_rate, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2864 | NULL, // updatedSamplingRate |
| 2865 | patch->sources[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2866 | NULL, // updatedFormat |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2867 | patch->sources[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2868 | NULL, // updatedChannelMask |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2869 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2870 | ALOGV("createAudioPatch() profile not supported for device %08x", |
| 2871 | devDesc->type()); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2872 | return INVALID_OPERATION; |
| 2873 | } |
| 2874 | devices.add(devDesc); |
| 2875 | } |
| 2876 | if (devices.size() == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2877 | return INVALID_OPERATION; |
| 2878 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2879 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2880 | // TODO: reconfigure output format and channels here |
| 2881 | ALOGV("createAudioPatch() setting device %08x on output %d", |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2882 | devices.types(), outputDesc->mIoHandle); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2883 | setOutputDevice(outputDesc, devices.types(), true, 0, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2884 | index = mAudioPatches.indexOfKey(*handle); |
| 2885 | if (index >= 0) { |
| 2886 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 2887 | ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided"); |
| 2888 | } |
| 2889 | patchDesc = mAudioPatches.valueAt(index); |
| 2890 | patchDesc->mUid = uid; |
| 2891 | ALOGV("createAudioPatch() success"); |
| 2892 | } else { |
| 2893 | ALOGW("createAudioPatch() setOutputDevice() failed to create a patch"); |
| 2894 | return INVALID_OPERATION; |
| 2895 | } |
| 2896 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2897 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 2898 | // input device to input mix connection |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2899 | // only one sink supported when connecting an input device to a mix |
| 2900 | if (patch->num_sinks > 1) { |
| 2901 | return INVALID_OPERATION; |
| 2902 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2903 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2904 | if (inputDesc == NULL) { |
| 2905 | return BAD_VALUE; |
| 2906 | } |
| 2907 | if (patchDesc != 0) { |
| 2908 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { |
| 2909 | return BAD_VALUE; |
| 2910 | } |
| 2911 | } |
| 2912 | sp<DeviceDescriptor> devDesc = |
| 2913 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
| 2914 | if (devDesc == 0) { |
| 2915 | return BAD_VALUE; |
| 2916 | } |
| 2917 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2918 | if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2919 | devDesc->mAddress, |
| 2920 | patch->sinks[0].sample_rate, |
| 2921 | NULL, /*updatedSampleRate*/ |
| 2922 | patch->sinks[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2923 | NULL, /*updatedFormat*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2924 | patch->sinks[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2925 | NULL, /*updatedChannelMask*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2926 | // FIXME for the parameter type, |
| 2927 | // and the NONE |
| 2928 | (audio_output_flags_t) |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2929 | AUDIO_INPUT_FLAG_NONE)) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2930 | return INVALID_OPERATION; |
| 2931 | } |
| 2932 | // TODO: reconfigure output format and channels here |
| 2933 | ALOGV("createAudioPatch() setting device %08x on output %d", |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2934 | devDesc->type(), inputDesc->mIoHandle); |
| 2935 | setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2936 | index = mAudioPatches.indexOfKey(*handle); |
| 2937 | if (index >= 0) { |
| 2938 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 2939 | ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided"); |
| 2940 | } |
| 2941 | patchDesc = mAudioPatches.valueAt(index); |
| 2942 | patchDesc->mUid = uid; |
| 2943 | ALOGV("createAudioPatch() success"); |
| 2944 | } else { |
| 2945 | ALOGW("createAudioPatch() setInputDevice() failed to create a patch"); |
| 2946 | return INVALID_OPERATION; |
| 2947 | } |
| 2948 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2949 | // device to device connection |
| 2950 | if (patchDesc != 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2951 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2952 | return BAD_VALUE; |
| 2953 | } |
| 2954 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2955 | sp<DeviceDescriptor> srcDeviceDesc = |
| 2956 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 2957 | if (srcDeviceDesc == 0) { |
| 2958 | return BAD_VALUE; |
| 2959 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2960 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2961 | //update source and sink with our own data as the data passed in the patch may |
| 2962 | // be incomplete. |
| 2963 | struct audio_patch newPatch = *patch; |
| 2964 | srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2965 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2966 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2967 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 2968 | ALOGV("createAudioPatch() source device but one sink is not a device"); |
| 2969 | return INVALID_OPERATION; |
| 2970 | } |
| 2971 | |
| 2972 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 2973 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 2974 | if (sinkDeviceDesc == 0) { |
| 2975 | return BAD_VALUE; |
| 2976 | } |
| 2977 | sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]); |
| 2978 | |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2979 | // create a software bridge in PatchPanel if: |
Scott Randolph | f317240 | 2018-01-23 17:06:53 -0800 | [diff] [blame] | 2980 | // - source and sink devices are on different HW modules OR |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2981 | // - audio HAL version is < 3.0 |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 2982 | if (!srcDeviceDesc->hasSameHwModuleAs(sinkDeviceDesc) || |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 2983 | (srcDeviceDesc->mModule->getHalVersionMajor() < 3)) { |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2984 | // support only one sink device for now to simplify output selection logic |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2985 | if (patch->num_sinks > 1) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2986 | return INVALID_OPERATION; |
| 2987 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2988 | SortedVector<audio_io_handle_t> outputs = |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2989 | getOutputsForDevice(sinkDeviceDesc->type(), mOutputs); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2990 | // if the sink device is reachable via an opened output stream, request to go via |
| 2991 | // this output stream by adding a second source to the patch description |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 2992 | audio_io_handle_t output = selectOutput(outputs, |
| 2993 | AUDIO_OUTPUT_FLAG_NONE, |
| 2994 | AUDIO_FORMAT_INVALID); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2995 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 2996 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 2997 | if (outputDesc->isDuplicated()) { |
| 2998 | return INVALID_OPERATION; |
| 2999 | } |
| 3000 | outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3001 | newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3002 | newPatch.num_sources = 2; |
| 3003 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3004 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3005 | } |
| 3006 | // TODO: check from routing capabilities in config file and other conflicting patches |
| 3007 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 3008 | status_t status = installPatch(__func__, index, handle, &newPatch, 0, uid, &patchDesc); |
| 3009 | if (status != NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3010 | ALOGW("createAudioPatch() patch panel could not connect device patch, error %d", |
| 3011 | status); |
| 3012 | return INVALID_OPERATION; |
| 3013 | } |
| 3014 | } else { |
| 3015 | return BAD_VALUE; |
| 3016 | } |
| 3017 | } else { |
| 3018 | return BAD_VALUE; |
| 3019 | } |
| 3020 | return NO_ERROR; |
| 3021 | } |
| 3022 | |
| 3023 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, |
| 3024 | uid_t uid) |
| 3025 | { |
| 3026 | ALOGV("releaseAudioPatch() patch %d", handle); |
| 3027 | |
| 3028 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 3029 | |
| 3030 | if (index < 0) { |
| 3031 | return BAD_VALUE; |
| 3032 | } |
| 3033 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 3034 | ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 3035 | mUidCached, patchDesc->mUid, uid); |
| 3036 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 3037 | return INVALID_OPERATION; |
| 3038 | } |
| 3039 | |
| 3040 | struct audio_patch *patch = &patchDesc->mPatch; |
| 3041 | patchDesc->mUid = mUidCached; |
| 3042 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3043 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3044 | if (outputDesc == NULL) { |
| 3045 | ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id); |
| 3046 | return BAD_VALUE; |
| 3047 | } |
| 3048 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3049 | setOutputDevice(outputDesc, |
| 3050 | getNewOutputDevice(outputDesc, true /*fromCache*/), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3051 | true, |
| 3052 | 0, |
| 3053 | NULL); |
| 3054 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 3055 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3056 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3057 | if (inputDesc == NULL) { |
| 3058 | ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id); |
| 3059 | return BAD_VALUE; |
| 3060 | } |
| 3061 | setInputDevice(inputDesc->mIoHandle, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 3062 | getNewInputDevice(inputDesc), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3063 | true, |
| 3064 | NULL); |
| 3065 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3066 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3067 | ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d", |
| 3068 | status, patchDesc->mAfPatchHandle); |
| 3069 | removeAudioPatch(patchDesc->mHandle); |
| 3070 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 3071 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3072 | } else { |
| 3073 | return BAD_VALUE; |
| 3074 | } |
| 3075 | } else { |
| 3076 | return BAD_VALUE; |
| 3077 | } |
| 3078 | return NO_ERROR; |
| 3079 | } |
| 3080 | |
| 3081 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, |
| 3082 | struct audio_patch *patches, |
| 3083 | unsigned int *generation) |
| 3084 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3085 | if (generation == NULL) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3086 | return BAD_VALUE; |
| 3087 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3088 | *generation = curAudioPortGeneration(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3089 | return mAudioPatches.listAudioPatches(num_patches, patches); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3090 | } |
| 3091 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3092 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3093 | { |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3094 | ALOGV("setAudioPortConfig()"); |
| 3095 | |
| 3096 | if (config == NULL) { |
| 3097 | return BAD_VALUE; |
| 3098 | } |
| 3099 | ALOGV("setAudioPortConfig() on port handle %d", config->id); |
| 3100 | // Only support gain configuration for now |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3101 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { |
| 3102 | return INVALID_OPERATION; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3103 | } |
| 3104 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3105 | sp<AudioPortConfig> audioPortConfig; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3106 | if (config->type == AUDIO_PORT_TYPE_MIX) { |
| 3107 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3108 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3109 | if (outputDesc == NULL) { |
| 3110 | return BAD_VALUE; |
| 3111 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 3112 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 3113 | "setAudioPortConfig() called on duplicated output %d", |
| 3114 | outputDesc->mIoHandle); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3115 | audioPortConfig = outputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3116 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3117 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3118 | if (inputDesc == NULL) { |
| 3119 | return BAD_VALUE; |
| 3120 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3121 | audioPortConfig = inputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3122 | } else { |
| 3123 | return BAD_VALUE; |
| 3124 | } |
| 3125 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { |
| 3126 | sp<DeviceDescriptor> deviceDesc; |
| 3127 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
| 3128 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); |
| 3129 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
| 3130 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); |
| 3131 | } else { |
| 3132 | return BAD_VALUE; |
| 3133 | } |
| 3134 | if (deviceDesc == NULL) { |
| 3135 | return BAD_VALUE; |
| 3136 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3137 | audioPortConfig = deviceDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3138 | } else { |
| 3139 | return BAD_VALUE; |
| 3140 | } |
| 3141 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3142 | struct audio_port_config backupConfig; |
| 3143 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); |
| 3144 | if (status == NO_ERROR) { |
| 3145 | struct audio_port_config newConfig; |
| 3146 | audioPortConfig->toAudioPortConfig(&newConfig, config); |
| 3147 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3148 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3149 | if (status != NO_ERROR) { |
| 3150 | audioPortConfig->applyAudioPortConfig(&backupConfig); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3151 | } |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3152 | |
| 3153 | return status; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3154 | } |
| 3155 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3156 | void AudioPolicyManager::releaseResourcesForUid(uid_t uid) |
| 3157 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3158 | clearAudioSources(uid); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3159 | clearAudioPatches(uid); |
| 3160 | clearSessionRoutes(uid); |
| 3161 | } |
| 3162 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3163 | void AudioPolicyManager::clearAudioPatches(uid_t uid) |
| 3164 | { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 3165 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3166 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 3167 | if (patchDesc->mUid == uid) { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 3168 | releaseAudioPatch(mAudioPatches.keyAt(i), uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3169 | } |
| 3170 | } |
| 3171 | } |
| 3172 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3173 | void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy, |
| 3174 | audio_io_handle_t ouptutToSkip) |
| 3175 | { |
| 3176 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 3177 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 3178 | for (size_t j = 0; j < mOutputs.size(); j++) { |
| 3179 | if (mOutputs.keyAt(j) == ouptutToSkip) { |
| 3180 | continue; |
| 3181 | } |
| 3182 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j); |
| 3183 | if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) { |
| 3184 | continue; |
| 3185 | } |
| 3186 | // If the default device for this strategy is on another output mix, |
| 3187 | // invalidate all tracks in this strategy to force re connection. |
| 3188 | // Otherwise select new device on the output mix. |
| 3189 | if (outputs.indexOf(mOutputs.keyAt(j)) < 0) { |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 3190 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3191 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
| 3192 | mpClientInterface->invalidateStream((audio_stream_type_t)stream); |
| 3193 | } |
| 3194 | } |
| 3195 | } else { |
| 3196 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
| 3197 | setOutputDevice(outputDesc, newDevice, false); |
| 3198 | } |
| 3199 | } |
| 3200 | } |
| 3201 | |
| 3202 | void AudioPolicyManager::clearSessionRoutes(uid_t uid) |
| 3203 | { |
| 3204 | // remove output routes associated with this uid |
| 3205 | SortedVector<routing_strategy> affectedStrategies; |
| 3206 | for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) { |
| 3207 | sp<SessionRoute> route = mOutputRoutes.valueAt(i); |
| 3208 | if (route->mUid == uid) { |
| 3209 | mOutputRoutes.removeItemsAt(i); |
| 3210 | if (route->mDeviceDescriptor != 0) { |
| 3211 | affectedStrategies.add(getStrategy(route->mStreamType)); |
| 3212 | } |
| 3213 | } |
| 3214 | } |
| 3215 | // reroute outputs if necessary |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3216 | for (const auto& strategy : affectedStrategies) { |
| 3217 | checkStrategyRoute(strategy, AUDIO_IO_HANDLE_NONE); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3218 | } |
| 3219 | |
| 3220 | // remove input routes associated with this uid |
| 3221 | SortedVector<audio_source_t> affectedSources; |
| 3222 | for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) { |
| 3223 | sp<SessionRoute> route = mInputRoutes.valueAt(i); |
| 3224 | if (route->mUid == uid) { |
| 3225 | mInputRoutes.removeItemsAt(i); |
| 3226 | if (route->mDeviceDescriptor != 0) { |
| 3227 | affectedSources.add(route->mSource); |
| 3228 | } |
| 3229 | } |
| 3230 | } |
| 3231 | // reroute inputs if necessary |
| 3232 | SortedVector<audio_io_handle_t> inputsToClose; |
| 3233 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3234 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3235 | if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3236 | inputsToClose.add(inputDesc->mIoHandle); |
| 3237 | } |
| 3238 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3239 | for (const auto& input : inputsToClose) { |
| 3240 | closeInput(input); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3241 | } |
| 3242 | } |
| 3243 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3244 | void AudioPolicyManager::clearAudioSources(uid_t uid) |
| 3245 | { |
| 3246 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
| 3247 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 3248 | if (sourceDesc->mUid == uid) { |
| 3249 | stopAudioSource(mAudioSources.keyAt(i)); |
| 3250 | } |
| 3251 | } |
| 3252 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3253 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3254 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, |
| 3255 | audio_io_handle_t *ioHandle, |
| 3256 | audio_devices_t *device) |
| 3257 | { |
Glenn Kasten | f0c6d7d | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 3258 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); |
| 3259 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT); |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 3260 | *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3261 | |
François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 3262 | return mSoundTriggerSessions.acquireSession(*session, *ioHandle); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3263 | } |
| 3264 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3265 | status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source, |
| 3266 | const audio_attributes_t *attributes, |
Glenn Kasten | 559d439 | 2016-03-29 13:42:57 -0700 | [diff] [blame] | 3267 | audio_patch_handle_t *handle, |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3268 | uid_t uid) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3269 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3270 | ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle); |
| 3271 | if (source == NULL || attributes == NULL || handle == NULL) { |
| 3272 | return BAD_VALUE; |
| 3273 | } |
| 3274 | |
Glenn Kasten | 559d439 | 2016-03-29 13:42:57 -0700 | [diff] [blame] | 3275 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3276 | |
| 3277 | if (source->role != AUDIO_PORT_ROLE_SOURCE || |
| 3278 | source->type != AUDIO_PORT_TYPE_DEVICE) { |
| 3279 | ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type); |
| 3280 | return INVALID_OPERATION; |
| 3281 | } |
| 3282 | |
| 3283 | sp<DeviceDescriptor> srcDeviceDesc = |
| 3284 | mAvailableInputDevices.getDevice(source->ext.device.type, |
| 3285 | String8(source->ext.device.address)); |
| 3286 | if (srcDeviceDesc == 0) { |
| 3287 | ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type); |
| 3288 | return BAD_VALUE; |
| 3289 | } |
| 3290 | sp<AudioSourceDescriptor> sourceDesc = |
| 3291 | new AudioSourceDescriptor(srcDeviceDesc, attributes, uid); |
| 3292 | |
| 3293 | struct audio_patch dummyPatch; |
| 3294 | sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid); |
| 3295 | sourceDesc->mPatchDesc = patchDesc; |
| 3296 | |
| 3297 | status_t status = connectAudioSource(sourceDesc); |
| 3298 | if (status == NO_ERROR) { |
| 3299 | mAudioSources.add(sourceDesc->getHandle(), sourceDesc); |
| 3300 | *handle = sourceDesc->getHandle(); |
| 3301 | } |
| 3302 | return status; |
| 3303 | } |
| 3304 | |
| 3305 | status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc) |
| 3306 | { |
| 3307 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle()); |
| 3308 | |
| 3309 | // make sure we only have one patch per source. |
| 3310 | disconnectAudioSource(sourceDesc); |
| 3311 | |
| 3312 | routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 3313 | audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3314 | sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice; |
| 3315 | |
| 3316 | audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true); |
| 3317 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 3318 | mAvailableOutputDevices.getDevice(sinkDevice, String8("")); |
| 3319 | |
| 3320 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3321 | |
| 3322 | if (srcDeviceDesc->getAudioPort()->mModule->getHandle() == |
| 3323 | sinkDeviceDesc->getAudioPort()->mModule->getHandle() && |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 3324 | srcDeviceDesc->getAudioPort()->mModule->getHalVersionMajor() >= 3 && |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3325 | srcDeviceDesc->getAudioPort()->mGains.size() > 0) { |
| 3326 | ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__); |
| 3327 | // create patch between src device and output device |
| 3328 | // create Hwoutput and add to mHwOutputs |
| 3329 | } else { |
| 3330 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs); |
| 3331 | audio_io_handle_t output = |
| 3332 | selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID); |
| 3333 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 3334 | ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice); |
| 3335 | return INVALID_OPERATION; |
| 3336 | } |
| 3337 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 3338 | if (outputDesc->isDuplicated()) { |
| 3339 | ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice); |
| 3340 | return INVALID_OPERATION; |
| 3341 | } |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 3342 | status_t status = outputDesc->start(); |
| 3343 | if (status != NO_ERROR) { |
| 3344 | return status; |
| 3345 | } |
| 3346 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3347 | // create a special patch with no sink and two sources: |
| 3348 | // - the second source indicates to PatchPanel through which output mix this patch should |
| 3349 | // be connected as well as the stream type for volume control |
| 3350 | // - the sink is defined by whatever output device is currently selected for the output |
| 3351 | // though which this patch is routed. |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 3352 | PatchBuilder patchBuilder; |
| 3353 | patchBuilder.addSource(srcDeviceDesc).addSource(outputDesc, { .stream = stream }); |
| 3354 | status = mpClientInterface->createAudioPatch(patchBuilder.patch(), |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3355 | &afPatchHandle, |
| 3356 | 0); |
| 3357 | ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__, |
| 3358 | status, afPatchHandle); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 3359 | sourceDesc->mPatchDesc->mPatch = *patchBuilder.patch(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3360 | if (status != NO_ERROR) { |
| 3361 | ALOGW("%s patch panel could not connect device patch, error %d", |
| 3362 | __FUNCTION__, status); |
| 3363 | return INVALID_OPERATION; |
| 3364 | } |
| 3365 | uint32_t delayMs = 0; |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3366 | status = startSource(outputDesc, stream, sinkDevice, NULL, &delayMs); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3367 | |
| 3368 | if (status != NO_ERROR) { |
| 3369 | mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0); |
| 3370 | return status; |
| 3371 | } |
| 3372 | sourceDesc->mSwOutput = outputDesc; |
| 3373 | if (delayMs != 0) { |
| 3374 | usleep(delayMs * 1000); |
| 3375 | } |
| 3376 | } |
| 3377 | |
| 3378 | sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle; |
| 3379 | addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc); |
| 3380 | |
| 3381 | return NO_ERROR; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3382 | } |
| 3383 | |
Glenn Kasten | 559d439 | 2016-03-29 13:42:57 -0700 | [diff] [blame] | 3384 | status_t AudioPolicyManager::stopAudioSource(audio_patch_handle_t handle __unused) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3385 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3386 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle); |
| 3387 | ALOGV("%s handle %d", __FUNCTION__, handle); |
| 3388 | if (sourceDesc == 0) { |
| 3389 | ALOGW("%s unknown source for handle %d", __FUNCTION__, handle); |
| 3390 | return BAD_VALUE; |
| 3391 | } |
| 3392 | status_t status = disconnectAudioSource(sourceDesc); |
| 3393 | |
| 3394 | mAudioSources.removeItem(handle); |
| 3395 | return status; |
| 3396 | } |
| 3397 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3398 | status_t AudioPolicyManager::setMasterMono(bool mono) |
| 3399 | { |
| 3400 | if (mMasterMono == mono) { |
| 3401 | return NO_ERROR; |
| 3402 | } |
| 3403 | mMasterMono = mono; |
| 3404 | // if enabling mono we close all offloaded devices, which will invalidate the |
| 3405 | // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible |
| 3406 | // for recreating the new AudioTrack as non-offloaded PCM. |
| 3407 | // |
| 3408 | // If disabling mono, we leave all tracks as is: we don't know which clients |
| 3409 | // and tracks are able to be recreated as offloaded. The next "song" should |
| 3410 | // play back offloaded. |
| 3411 | if (mMasterMono) { |
| 3412 | Vector<audio_io_handle_t> offloaded; |
| 3413 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3414 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 3415 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
| 3416 | offloaded.push(desc->mIoHandle); |
| 3417 | } |
| 3418 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3419 | for (const auto& handle : offloaded) { |
| 3420 | closeOutput(handle); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3421 | } |
| 3422 | } |
| 3423 | // update master mono for all remaining outputs |
| 3424 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3425 | updateMono(mOutputs.keyAt(i)); |
| 3426 | } |
| 3427 | return NO_ERROR; |
| 3428 | } |
| 3429 | |
| 3430 | status_t AudioPolicyManager::getMasterMono(bool *mono) |
| 3431 | { |
| 3432 | *mono = mMasterMono; |
| 3433 | return NO_ERROR; |
| 3434 | } |
| 3435 | |
Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 3436 | float AudioPolicyManager::getStreamVolumeDB( |
| 3437 | audio_stream_type_t stream, int index, audio_devices_t device) |
| 3438 | { |
| 3439 | return computeVolume(stream, index, device); |
| 3440 | } |
| 3441 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3442 | status_t AudioPolicyManager::getSupportedFormats(audio_io_handle_t ioHandle, |
| 3443 | FormatVector& formats) { |
| 3444 | if (ioHandle == AUDIO_IO_HANDLE_NONE) { |
| 3445 | return BAD_VALUE; |
| 3446 | } |
| 3447 | String8 reply; |
| 3448 | reply = mpClientInterface->getParameters( |
| 3449 | ioHandle, String8(AudioParameter::keyStreamSupportedFormats)); |
| 3450 | ALOGV("%s: supported formats %s", __FUNCTION__, reply.string()); |
| 3451 | AudioParameter repliedParameters(reply); |
| 3452 | if (repliedParameters.get( |
| 3453 | String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) { |
| 3454 | ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__); |
| 3455 | return BAD_VALUE; |
| 3456 | } |
| 3457 | for (auto format : formatsFromString(reply.string())) { |
| 3458 | // Only AUDIO_FORMAT_AAC_LC will be used in Settings UI for all AAC formats. |
| 3459 | for (size_t i = 0; i < ARRAY_SIZE(AAC_FORMATS); i++) { |
| 3460 | if (format == AAC_FORMATS[i]) { |
| 3461 | format = AUDIO_FORMAT_AAC_LC; |
| 3462 | break; |
| 3463 | } |
| 3464 | } |
| 3465 | bool exist = false; |
| 3466 | for (size_t i = 0; i < formats.size(); i++) { |
| 3467 | if (format == formats[i]) { |
| 3468 | exist = true; |
| 3469 | break; |
| 3470 | } |
| 3471 | } |
| 3472 | bool isSurroundFormat = false; |
| 3473 | for (size_t i = 0; i < ARRAY_SIZE(SURROUND_FORMATS); i++) { |
| 3474 | if (SURROUND_FORMATS[i] == format) { |
| 3475 | isSurroundFormat = true; |
| 3476 | break; |
| 3477 | } |
| 3478 | } |
| 3479 | if (!exist && isSurroundFormat) { |
| 3480 | formats.add(format); |
| 3481 | } |
| 3482 | } |
| 3483 | return NO_ERROR; |
| 3484 | } |
| 3485 | |
| 3486 | status_t AudioPolicyManager::getSurroundFormats(unsigned int *numSurroundFormats, |
| 3487 | audio_format_t *surroundFormats, |
| 3488 | bool *surroundFormatsEnabled, |
| 3489 | bool reported) |
| 3490 | { |
| 3491 | if (numSurroundFormats == NULL || (*numSurroundFormats != 0 && |
| 3492 | (surroundFormats == NULL || surroundFormatsEnabled == NULL))) { |
| 3493 | return BAD_VALUE; |
| 3494 | } |
| 3495 | ALOGV("getSurroundFormats() numSurroundFormats %d surroundFormats %p surroundFormatsEnabled %p", |
| 3496 | *numSurroundFormats, surroundFormats, surroundFormatsEnabled); |
| 3497 | |
| 3498 | // Only return value if there is HDMI output. |
| 3499 | if ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_HDMI) == 0) { |
| 3500 | return INVALID_OPERATION; |
| 3501 | } |
| 3502 | |
| 3503 | size_t formatsWritten = 0; |
| 3504 | size_t formatsMax = *numSurroundFormats; |
| 3505 | *numSurroundFormats = 0; |
| 3506 | FormatVector formats; |
| 3507 | if (reported) { |
| 3508 | // Only get surround formats which are reported by device. |
| 3509 | // First list already open outputs that can be routed to this device |
| 3510 | audio_devices_t device = AUDIO_DEVICE_OUT_HDMI; |
| 3511 | SortedVector<audio_io_handle_t> outputs; |
| 3512 | bool reportedFormatFound = false; |
| 3513 | status_t status; |
| 3514 | sp<SwAudioOutputDescriptor> desc; |
| 3515 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3516 | desc = mOutputs.valueAt(i); |
| 3517 | if (!desc->isDuplicated() && (desc->supportedDevices() & device)) { |
| 3518 | outputs.add(mOutputs.keyAt(i)); |
| 3519 | } |
| 3520 | } |
| 3521 | // Open an output to query dynamic parameters. |
| 3522 | DeviceVector hdmiOutputDevices = mAvailableOutputDevices.getDevicesFromType( |
| 3523 | AUDIO_DEVICE_OUT_HDMI); |
| 3524 | for (size_t i = 0; i < hdmiOutputDevices.size(); i++) { |
| 3525 | String8 address = hdmiOutputDevices[i]->mAddress; |
| 3526 | for (const auto& hwModule : mHwModules) { |
| 3527 | for (size_t i = 0; i < hwModule->getOutputProfiles().size(); i++) { |
| 3528 | sp<IOProfile> profile = hwModule->getOutputProfiles()[i]; |
| 3529 | if (profile->supportDevice(AUDIO_DEVICE_OUT_HDMI) && |
| 3530 | profile->supportDeviceAddress(address)) { |
| 3531 | size_t j; |
| 3532 | for (j = 0; j < outputs.size(); j++) { |
| 3533 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
| 3534 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
| 3535 | break; |
| 3536 | } |
| 3537 | } |
| 3538 | if (j != outputs.size()) { |
| 3539 | status = getSupportedFormats(outputs.itemAt(j), formats); |
| 3540 | reportedFormatFound |= (status == NO_ERROR); |
| 3541 | continue; |
| 3542 | } |
| 3543 | |
| 3544 | if (!profile->canOpenNewIo()) { |
| 3545 | ALOGW("Max Output number %u already opened for this profile %s", |
| 3546 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 3547 | continue; |
| 3548 | } |
| 3549 | |
| 3550 | ALOGV("opening output for device %08x with params %s profile %p name %s", |
| 3551 | device, address.string(), profile.get(), profile->getName().string()); |
| 3552 | desc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
| 3553 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 3554 | status_t status = desc->open(nullptr, device, address, |
| 3555 | AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, |
| 3556 | &output); |
| 3557 | |
| 3558 | if (status == NO_ERROR) { |
| 3559 | status = getSupportedFormats(output, formats); |
| 3560 | reportedFormatFound |= (status == NO_ERROR); |
| 3561 | desc->close(); |
| 3562 | output = AUDIO_IO_HANDLE_NONE; |
| 3563 | } |
| 3564 | } |
| 3565 | } |
| 3566 | } |
| 3567 | } |
| 3568 | |
| 3569 | if (!reportedFormatFound) { |
| 3570 | return UNKNOWN_ERROR; |
| 3571 | } |
| 3572 | } else { |
| 3573 | for (size_t i = 0; i < ARRAY_SIZE(SURROUND_FORMATS); i++) { |
| 3574 | formats.add(SURROUND_FORMATS[i]); |
| 3575 | } |
| 3576 | } |
| 3577 | for (size_t i = 0; i < formats.size(); i++) { |
| 3578 | if (formatsWritten < formatsMax) { |
| 3579 | surroundFormats[formatsWritten] = formats[i]; |
| 3580 | bool formatEnabled = false; |
| 3581 | if (formats[i] == AUDIO_FORMAT_AAC_LC) { |
| 3582 | for (size_t j = 0; j < ARRAY_SIZE(AAC_FORMATS); j++) { |
| 3583 | formatEnabled = |
| 3584 | mSurroundFormats.find(AAC_FORMATS[i]) != mSurroundFormats.end(); |
| 3585 | break; |
| 3586 | } |
| 3587 | } else { |
| 3588 | formatEnabled = mSurroundFormats.find(formats[i]) != mSurroundFormats.end(); |
| 3589 | } |
| 3590 | surroundFormatsEnabled[formatsWritten++] = formatEnabled; |
| 3591 | } |
| 3592 | (*numSurroundFormats)++; |
| 3593 | } |
| 3594 | return NO_ERROR; |
| 3595 | } |
| 3596 | |
| 3597 | status_t AudioPolicyManager::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) |
| 3598 | { |
| 3599 | // Check if audio format is a surround formats. |
| 3600 | bool isSurroundFormat = false; |
| 3601 | for (size_t i = 0; i < ARRAY_SIZE(SURROUND_FORMATS); i++) { |
| 3602 | if (audioFormat == SURROUND_FORMATS[i]) { |
| 3603 | isSurroundFormat = true; |
| 3604 | break; |
| 3605 | } |
| 3606 | } |
| 3607 | if (!isSurroundFormat) { |
| 3608 | return BAD_VALUE; |
| 3609 | } |
| 3610 | |
| 3611 | // Should only be called when MANUAL. |
| 3612 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 3613 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
| 3614 | if (forceUse != AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
| 3615 | return INVALID_OPERATION; |
| 3616 | } |
| 3617 | |
| 3618 | if ((mSurroundFormats.find(audioFormat) != mSurroundFormats.end() && enabled) |
| 3619 | || (mSurroundFormats.find(audioFormat) == mSurroundFormats.end() && !enabled)) { |
| 3620 | return NO_ERROR; |
| 3621 | } |
| 3622 | |
| 3623 | // The operation is valid only when there is HDMI output available. |
| 3624 | if ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_HDMI) == 0) { |
| 3625 | return INVALID_OPERATION; |
| 3626 | } |
| 3627 | |
| 3628 | if (enabled) { |
| 3629 | if (audioFormat == AUDIO_FORMAT_AAC_LC) { |
| 3630 | for (size_t i = 0; i < ARRAY_SIZE(AAC_FORMATS); i++) { |
| 3631 | mSurroundFormats.insert(AAC_FORMATS[i]); |
| 3632 | } |
| 3633 | } else { |
| 3634 | mSurroundFormats.insert(audioFormat); |
| 3635 | } |
| 3636 | } else { |
| 3637 | if (audioFormat == AUDIO_FORMAT_AAC_LC) { |
| 3638 | for (size_t i = 0; i < ARRAY_SIZE(AAC_FORMATS); i++) { |
| 3639 | mSurroundFormats.erase(AAC_FORMATS[i]); |
| 3640 | } |
| 3641 | } else { |
| 3642 | mSurroundFormats.erase(audioFormat); |
| 3643 | } |
| 3644 | } |
| 3645 | |
| 3646 | sp<SwAudioOutputDescriptor> outputDesc; |
| 3647 | bool profileUpdated = false; |
| 3648 | DeviceVector hdmiOutputDevices = mAvailableOutputDevices.getDevicesFromType( |
| 3649 | AUDIO_DEVICE_OUT_HDMI); |
| 3650 | for (size_t i = 0; i < hdmiOutputDevices.size(); i++) { |
| 3651 | // Simulate reconnection to update enabled surround sound formats. |
| 3652 | String8 address = hdmiOutputDevices[i]->mAddress; |
| 3653 | String8 name = hdmiOutputDevices[i]->getName(); |
| 3654 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 3655 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 3656 | address.c_str(), |
| 3657 | name.c_str()); |
| 3658 | if (status != NO_ERROR) { |
| 3659 | continue; |
| 3660 | } |
| 3661 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 3662 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 3663 | address.c_str(), |
| 3664 | name.c_str()); |
| 3665 | profileUpdated |= (status == NO_ERROR); |
| 3666 | } |
| 3667 | DeviceVector hdmiInputDevices = mAvailableInputDevices.getDevicesFromType( |
| 3668 | AUDIO_DEVICE_IN_HDMI); |
| 3669 | for (size_t i = 0; i < hdmiInputDevices.size(); i++) { |
| 3670 | // Simulate reconnection to update enabled surround sound formats. |
| 3671 | String8 address = hdmiInputDevices[i]->mAddress; |
| 3672 | String8 name = hdmiInputDevices[i]->getName(); |
| 3673 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 3674 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 3675 | address.c_str(), |
| 3676 | name.c_str()); |
| 3677 | if (status != NO_ERROR) { |
| 3678 | continue; |
| 3679 | } |
| 3680 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 3681 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 3682 | address.c_str(), |
| 3683 | name.c_str()); |
| 3684 | profileUpdated |= (status == NO_ERROR); |
| 3685 | } |
| 3686 | |
| 3687 | // Undo the surround formats change due to no audio profiles updated. |
| 3688 | if (!profileUpdated) { |
| 3689 | if (enabled) { |
| 3690 | if (audioFormat == AUDIO_FORMAT_AAC_LC) { |
| 3691 | for (size_t i = 0; i < ARRAY_SIZE(AAC_FORMATS); i++) { |
| 3692 | mSurroundFormats.erase(AAC_FORMATS[i]); |
| 3693 | } |
| 3694 | } else { |
| 3695 | mSurroundFormats.erase(audioFormat); |
| 3696 | } |
| 3697 | } else { |
| 3698 | if (audioFormat == AUDIO_FORMAT_AAC_LC) { |
| 3699 | for (size_t i = 0; i < ARRAY_SIZE(AAC_FORMATS); i++) { |
| 3700 | mSurroundFormats.insert(AAC_FORMATS[i]); |
| 3701 | } |
| 3702 | } else { |
| 3703 | mSurroundFormats.insert(audioFormat); |
| 3704 | } |
| 3705 | } |
| 3706 | } |
| 3707 | |
| 3708 | return profileUpdated ? NO_ERROR : INVALID_OPERATION; |
| 3709 | } |
| 3710 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 3711 | void AudioPolicyManager::setRecordSilenced(uid_t uid, bool silenced) |
| 3712 | { |
| 3713 | ALOGV("AudioPolicyManager:setRecordSilenced(uid:%d, silenced:%d)", uid, silenced); |
| 3714 | |
| 3715 | Vector<sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs(); |
| 3716 | for (size_t i = 0; i < activeInputs.size(); i++) { |
| 3717 | sp<AudioInputDescriptor> activeDesc = activeInputs[i]; |
| 3718 | AudioSessionCollection activeSessions = activeDesc->getAudioSessions(true); |
| 3719 | for (size_t j = 0; j < activeSessions.size(); j++) { |
| 3720 | sp<AudioSession> activeSession = activeSessions.valueAt(j); |
| 3721 | if (activeSession->uid() == uid) { |
| 3722 | activeSession->setSilenced(silenced); |
| 3723 | } |
| 3724 | } |
| 3725 | } |
| 3726 | } |
| 3727 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3728 | status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc) |
| 3729 | { |
| 3730 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle()); |
| 3731 | |
| 3732 | sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle); |
| 3733 | if (patchDesc == 0) { |
| 3734 | ALOGW("%s source has no patch with handle %d", __FUNCTION__, |
| 3735 | sourceDesc->mPatchDesc->mHandle); |
| 3736 | return BAD_VALUE; |
| 3737 | } |
| 3738 | removeAudioPatch(sourceDesc->mPatchDesc->mHandle); |
| 3739 | |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 3740 | audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3741 | sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote(); |
| 3742 | if (swOutputDesc != 0) { |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 3743 | status_t status = stopSource(swOutputDesc, stream, false); |
| 3744 | if (status == NO_ERROR) { |
| 3745 | swOutputDesc->stop(); |
| 3746 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3747 | mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3748 | } else { |
| 3749 | sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote(); |
| 3750 | if (hwOutputDesc != 0) { |
| 3751 | // release patch between src device and output device |
| 3752 | // close Hwoutput and remove from mHwOutputs |
| 3753 | } else { |
| 3754 | ALOGW("%s source has neither SW nor HW output", __FUNCTION__); |
| 3755 | } |
| 3756 | } |
| 3757 | return NO_ERROR; |
| 3758 | } |
| 3759 | |
| 3760 | sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput( |
| 3761 | audio_io_handle_t output, routing_strategy strategy) |
| 3762 | { |
| 3763 | sp<AudioSourceDescriptor> source; |
| 3764 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
| 3765 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 3766 | routing_strategy sourceStrategy = |
| 3767 | (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes); |
| 3768 | sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote(); |
| 3769 | if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) { |
| 3770 | source = sourceDesc; |
| 3771 | break; |
| 3772 | } |
| 3773 | } |
| 3774 | return source; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3775 | } |
| 3776 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3777 | // ---------------------------------------------------------------------------- |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3778 | // AudioPolicyManager |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3779 | // ---------------------------------------------------------------------------- |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3780 | uint32_t AudioPolicyManager::nextAudioPortGeneration() |
| 3781 | { |
Mikhail Naganov | 2773dd7 | 2017-12-08 10:12:11 -0800 | [diff] [blame] | 3782 | return mAudioPortGeneration++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3783 | } |
| 3784 | |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3785 | #ifdef USE_XML_AUDIO_POLICY_CONF |
| 3786 | // Treblized audio policy xml config will be located in /odm/etc or /vendor/etc. |
| 3787 | static const char *kConfigLocationList[] = |
| 3788 | {"/odm/etc", "/vendor/etc", "/system/etc"}; |
| 3789 | static const int kConfigLocationListSize = |
| 3790 | (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0])); |
| 3791 | |
| 3792 | static status_t deserializeAudioPolicyXmlConfig(AudioPolicyConfig &config) { |
| 3793 | char audioPolicyXmlConfigFile[AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH]; |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3794 | std::vector<const char*> fileNames; |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3795 | status_t ret; |
| 3796 | |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3797 | if (property_get_bool("ro.bluetooth.a2dp_offload.supported", false) && |
| 3798 | property_get_bool("persist.bluetooth.a2dp_offload.disabled", false)) { |
| 3799 | // A2DP offload supported but disabled: try to use special XML file |
| 3800 | fileNames.push_back(AUDIO_POLICY_A2DP_OFFLOAD_DISABLED_XML_CONFIG_FILE_NAME); |
| 3801 | } |
| 3802 | fileNames.push_back(AUDIO_POLICY_XML_CONFIG_FILE_NAME); |
| 3803 | |
| 3804 | for (const char* fileName : fileNames) { |
| 3805 | for (int i = 0; i < kConfigLocationListSize; i++) { |
| 3806 | PolicySerializer serializer; |
| 3807 | snprintf(audioPolicyXmlConfigFile, sizeof(audioPolicyXmlConfigFile), |
| 3808 | "%s/%s", kConfigLocationList[i], fileName); |
| 3809 | ret = serializer.deserialize(audioPolicyXmlConfigFile, config); |
| 3810 | if (ret == NO_ERROR) { |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 3811 | config.setSource(audioPolicyXmlConfigFile); |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3812 | return ret; |
| 3813 | } |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3814 | } |
| 3815 | } |
| 3816 | return ret; |
| 3817 | } |
| 3818 | #endif |
| 3819 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3820 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface, |
| 3821 | bool /*forTesting*/) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3822 | : |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame^] | 3823 | 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] | 3824 | mpClientInterface(clientInterface), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3825 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3826 | mA2dpSuspended(false), |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3827 | #ifdef USE_XML_AUDIO_POLICY_CONF |
| 3828 | mVolumeCurves(new VolumeCurvesCollection()), |
| 3829 | mConfig(mHwModulesAll, mAvailableOutputDevices, mAvailableInputDevices, |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 3830 | mDefaultOutputDevice, static_cast<VolumeCurvesCollection*>(mVolumeCurves.get())), |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3831 | #else |
| 3832 | mVolumeCurves(new StreamDescriptorCollection()), |
| 3833 | mConfig(mHwModulesAll, mAvailableOutputDevices, mAvailableInputDevices, |
| 3834 | mDefaultOutputDevice), |
| 3835 | #endif |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 3836 | mAudioPortGeneration(1), |
| 3837 | mBeaconMuteRefCount(0), |
| 3838 | mBeaconPlayingRefCount(0), |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3839 | mBeaconMuted(false), |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3840 | mTtsOutputAvailable(false), |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3841 | mMasterMono(false), |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 3842 | mMusicEffectOutput(AUDIO_IO_HANDLE_NONE), |
| 3843 | mHasComputedSoundTriggerSupportsConcurrentCapture(false) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3844 | { |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3845 | } |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3846 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3847 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) |
| 3848 | : AudioPolicyManager(clientInterface, false /*forTesting*/) |
| 3849 | { |
| 3850 | loadConfig(); |
| 3851 | initialize(); |
| 3852 | } |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3853 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3854 | void AudioPolicyManager::loadConfig() { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3855 | #ifdef USE_XML_AUDIO_POLICY_CONF |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3856 | if (deserializeAudioPolicyXmlConfig(getConfig()) != NO_ERROR) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3857 | #else |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3858 | if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, getConfig()) != NO_ERROR) |
| 3859 | && (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, getConfig()) != NO_ERROR)) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3860 | #endif |
| 3861 | ALOGE("could not load audio policy configuration file, setting defaults"); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3862 | getConfig().setDefault(); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3863 | } |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3864 | } |
| 3865 | |
| 3866 | status_t AudioPolicyManager::initialize() { |
| 3867 | mVolumeCurves->initializeVolumeCurves(getConfig().isSpeakerDrcEnabled()); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3868 | |
| 3869 | // 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] | 3870 | audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance(); |
| 3871 | if (!engineInstance) { |
| 3872 | ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3873 | return NO_INIT; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3874 | } |
| 3875 | // Retrieve the Policy Manager Interface |
| 3876 | mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>(); |
| 3877 | if (mEngine == NULL) { |
| 3878 | ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3879 | return NO_INIT; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3880 | } |
| 3881 | mEngine->setObserver(this); |
| 3882 | status_t status = mEngine->initCheck(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3883 | if (status != NO_ERROR) { |
| 3884 | LOG_FATAL("Policy engine not initialized(err=%d)", status); |
| 3885 | return status; |
| 3886 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3887 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3888 | // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3889 | // open all output streams needed to access attached devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3890 | audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types(); |
| 3891 | audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3892 | for (const auto& hwModule : mHwModulesAll) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 3893 | hwModule->setHandle(mpClientInterface->loadHwModule(hwModule->getName())); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3894 | if (hwModule->getHandle() == AUDIO_MODULE_HANDLE_NONE) { |
| 3895 | ALOGW("could not open HW module %s", hwModule->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3896 | continue; |
| 3897 | } |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3898 | mHwModules.push_back(hwModule); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3899 | // open all output streams needed to access attached devices |
| 3900 | // except for direct output streams that are only opened when they are actually |
| 3901 | // required by an app. |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3902 | // This also validates mAvailableOutputDevices list |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 3903 | for (const auto& outProfile : hwModule->getOutputProfiles()) { |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 3904 | if (!outProfile->canOpenNewIo()) { |
| 3905 | ALOGE("Invalid Output profile max open count %u for profile %s", |
| 3906 | outProfile->maxOpenCount, outProfile->getTagName().c_str()); |
| 3907 | continue; |
| 3908 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3909 | if (!outProfile->hasSupportedDevices()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3910 | ALOGW("Output profile contains no device on module %s", hwModule->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3911 | continue; |
| 3912 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3913 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3914 | mTtsOutputAvailable = true; |
| 3915 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3916 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3917 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3918 | continue; |
| 3919 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3920 | audio_devices_t profileType = outProfile->getSupportedDevicesType(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3921 | if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) { |
| 3922 | profileType = mDefaultOutputDevice->type(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3923 | } else { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3924 | // chose first device present in profile's SupportedDevices also part of |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3925 | // outputDeviceTypes |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3926 | profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3927 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3928 | if ((profileType & outputDeviceTypes) == 0) { |
| 3929 | continue; |
| 3930 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3931 | sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile, |
| 3932 | mpClientInterface); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3933 | const DeviceVector &supportedDevices = outProfile->getSupportedDevices(); |
| 3934 | const DeviceVector &devicesForType = supportedDevices.getDevicesFromType(profileType); |
| 3935 | String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress |
| 3936 | : String8(""); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3937 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 3938 | status_t status = outputDesc->open(nullptr, profileType, address, |
| 3939 | AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, &output); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3940 | |
| 3941 | if (status != NO_ERROR) { |
| 3942 | ALOGW("Cannot open output stream for device %08x on hw module %s", |
| 3943 | outputDesc->mDevice, |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3944 | hwModule->getName()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3945 | } else { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3946 | for (const auto& dev : supportedDevices) { |
| 3947 | ssize_t index = mAvailableOutputDevices.indexOf(dev); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3948 | // 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] | 3949 | if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3950 | mAvailableOutputDevices[index]->attach(hwModule); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3951 | } |
| 3952 | } |
| 3953 | if (mPrimaryOutput == 0 && |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3954 | outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3955 | mPrimaryOutput = outputDesc; |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3956 | } |
| 3957 | addOutput(output, outputDesc); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3958 | setOutputDevice(outputDesc, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 3959 | profileType, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3960 | true, |
| 3961 | 0, |
| 3962 | NULL, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 3963 | address); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3964 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3965 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3966 | // open input streams needed to access attached devices to validate |
| 3967 | // mAvailableInputDevices list |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 3968 | for (const auto& inProfile : hwModule->getInputProfiles()) { |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 3969 | if (!inProfile->canOpenNewIo()) { |
| 3970 | ALOGE("Invalid Input profile max open count %u for profile %s", |
| 3971 | inProfile->maxOpenCount, inProfile->getTagName().c_str()); |
| 3972 | continue; |
| 3973 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3974 | if (!inProfile->hasSupportedDevices()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3975 | ALOGW("Input profile contains no device on module %s", hwModule->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3976 | continue; |
| 3977 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3978 | // chose first device present in profile's SupportedDevices also part of |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3979 | // inputDeviceTypes |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3980 | audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes); |
| 3981 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3982 | if ((profileType & inputDeviceTypes) == 0) { |
| 3983 | continue; |
| 3984 | } |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 3985 | sp<AudioInputDescriptor> inputDesc = |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 3986 | new AudioInputDescriptor(inProfile, mpClientInterface); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3987 | |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 3988 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType); |
| 3989 | // the inputs vector must be of size >= 1, but we don't want to crash here |
| 3990 | String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress |
| 3991 | : String8(""); |
| 3992 | ALOGV(" for input device 0x%x using address %s", profileType, address.string()); |
| 3993 | ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!"); |
| 3994 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3995 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 3996 | status_t status = inputDesc->open(nullptr, |
| 3997 | profileType, |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 3998 | address, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 3999 | AUDIO_SOURCE_MIC, |
| 4000 | AUDIO_INPUT_FLAG_NONE, |
| 4001 | &input); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4002 | |
| 4003 | if (status == NO_ERROR) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4004 | for (const auto& dev : inProfile->getSupportedDevices()) { |
| 4005 | ssize_t index = mAvailableInputDevices.indexOf(dev); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4006 | // 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] | 4007 | if (index >= 0) { |
| 4008 | sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index]; |
| 4009 | if (!devDesc->isAttached()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4010 | devDesc->attach(hwModule); |
Eric Laurent | 83efe1c | 2017-07-09 16:51:08 -0700 | [diff] [blame] | 4011 | devDesc->importAudioPort(inProfile, true); |
Eric Laurent | 45aabc3 | 2015-08-06 09:11:13 -0700 | [diff] [blame] | 4012 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4013 | } |
| 4014 | } |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4015 | inputDesc->close(); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4016 | } else { |
| 4017 | ALOGW("Cannot open input stream for device %08x on hw module %s", |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4018 | profileType, |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4019 | hwModule->getName()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4020 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4021 | } |
| 4022 | } |
| 4023 | // make sure all attached devices have been allocated a unique ID |
| 4024 | for (size_t i = 0; i < mAvailableOutputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 4025 | if (!mAvailableOutputDevices[i]->isAttached()) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4026 | ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4027 | mAvailableOutputDevices.remove(mAvailableOutputDevices[i]); |
| 4028 | continue; |
| 4029 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4030 | // The device is now validated and can be appended to the available devices of the engine |
| 4031 | mEngine->setDeviceConnectionState(mAvailableOutputDevices[i], |
| 4032 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4033 | i++; |
| 4034 | } |
| 4035 | for (size_t i = 0; i < mAvailableInputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 4036 | if (!mAvailableInputDevices[i]->isAttached()) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4037 | ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4038 | mAvailableInputDevices.remove(mAvailableInputDevices[i]); |
| 4039 | continue; |
| 4040 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4041 | // The device is now validated and can be appended to the available devices of the engine |
| 4042 | mEngine->setDeviceConnectionState(mAvailableInputDevices[i], |
| 4043 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4044 | i++; |
| 4045 | } |
| 4046 | // make sure default device is reachable |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4047 | if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4048 | ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type()); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4049 | status = NO_INIT; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4050 | } |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 4051 | // If microphones address is empty, set it according to device type |
| 4052 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { |
| 4053 | if (mAvailableInputDevices[i]->mAddress.isEmpty()) { |
| 4054 | if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BUILTIN_MIC) { |
| 4055 | mAvailableInputDevices[i]->mAddress = String8(AUDIO_BOTTOM_MICROPHONE_ADDRESS); |
| 4056 | } else if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BACK_MIC) { |
| 4057 | mAvailableInputDevices[i]->mAddress = String8(AUDIO_BACK_MICROPHONE_ADDRESS); |
| 4058 | } |
| 4059 | } |
| 4060 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4061 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4062 | if (mPrimaryOutput == 0) { |
| 4063 | ALOGE("Failed to open primary output"); |
| 4064 | status = NO_INIT; |
| 4065 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4066 | |
| 4067 | updateDevicesAndOutputs(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4068 | return status; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4069 | } |
| 4070 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4071 | AudioPolicyManager::~AudioPolicyManager() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4072 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4073 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4074 | mOutputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4075 | } |
| 4076 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4077 | mInputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4078 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4079 | mAvailableOutputDevices.clear(); |
| 4080 | mAvailableInputDevices.clear(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4081 | mOutputs.clear(); |
| 4082 | mInputs.clear(); |
| 4083 | mHwModules.clear(); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4084 | mHwModulesAll.clear(); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4085 | mSurroundFormats.clear(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4086 | } |
| 4087 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4088 | status_t AudioPolicyManager::initCheck() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4089 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 4090 | return hasPrimaryOutput() ? NO_ERROR : NO_INIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4091 | } |
| 4092 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4093 | // --- |
| 4094 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 4095 | void AudioPolicyManager::addOutput(audio_io_handle_t output, |
| 4096 | const sp<SwAudioOutputDescriptor>& outputDesc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4097 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4098 | mOutputs.add(output, outputDesc); |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 4099 | applyStreamVolumes(outputDesc, AUDIO_DEVICE_NONE, 0 /* delayMs */, true /* force */); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4100 | updateMono(output); // update mono status when adding to output list |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4101 | selectOutputForMusicEffects(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4102 | nextAudioPortGeneration(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4103 | } |
| 4104 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4105 | void AudioPolicyManager::removeOutput(audio_io_handle_t output) |
| 4106 | { |
| 4107 | mOutputs.removeItem(output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4108 | selectOutputForMusicEffects(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4109 | } |
| 4110 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 4111 | void AudioPolicyManager::addInput(audio_io_handle_t input, |
| 4112 | const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4113 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4114 | mInputs.add(input, inputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4115 | nextAudioPortGeneration(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4116 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4117 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4118 | void AudioPolicyManager::findIoHandlesByAddress(const sp<SwAudioOutputDescriptor>& desc /*in*/, |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4119 | const audio_devices_t device /*in*/, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4120 | const String8& address /*in*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4121 | SortedVector<audio_io_handle_t>& outputs /*out*/) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4122 | sp<DeviceDescriptor> devDesc = |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4123 | desc->mProfile->getSupportedDeviceByAddress(device, address); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4124 | if (devDesc != 0) { |
| 4125 | ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s", |
| 4126 | desc->mIoHandle, address.string()); |
| 4127 | outputs.add(desc->mIoHandle); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4128 | } |
| 4129 | } |
| 4130 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4131 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4132 | audio_policy_dev_state_t state, |
| 4133 | SortedVector<audio_io_handle_t>& outputs, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4134 | const String8& address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4135 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4136 | audio_devices_t device = devDesc->type(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4137 | sp<SwAudioOutputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4138 | |
| 4139 | if (audio_device_is_digital(device)) { |
| 4140 | // erase all current sample rates, formats and channel masks |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 4141 | devDesc->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4142 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4143 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4144 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4145 | // first list already open outputs that can be routed to this device |
| 4146 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4147 | desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4148 | if (!desc->isDuplicated() && (desc->supportedDevices() & device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4149 | if (!device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4150 | ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i)); |
| 4151 | outputs.add(mOutputs.keyAt(i)); |
| 4152 | } else { |
| 4153 | ALOGV(" checking address match due to device 0x%x", device); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4154 | findIoHandlesByAddress(desc, device, address, outputs); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4155 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4156 | } |
| 4157 | } |
| 4158 | // then look for output profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4159 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4160 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4161 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 4162 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4163 | if (profile->supportDevice(device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4164 | if (!device_distinguishes_on_address(device) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4165 | profile->supportDeviceAddress(address)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4166 | profiles.add(profile); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4167 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %s", |
| 4168 | j, hwModule->getName()); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4169 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4170 | } |
| 4171 | } |
| 4172 | } |
| 4173 | |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 4174 | ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size()); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4175 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4176 | if (profiles.isEmpty() && outputs.isEmpty()) { |
| 4177 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 4178 | return BAD_VALUE; |
| 4179 | } |
| 4180 | |
| 4181 | // open outputs for matching profiles if needed. Direct outputs are also opened to |
| 4182 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 4183 | 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] | 4184 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4185 | |
| 4186 | // nothing to do if one output is already opened for this profile |
| 4187 | size_t j; |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4188 | for (j = 0; j < outputs.size(); j++) { |
| 4189 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4190 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 4191 | // matching profile: save the sample rates, format and channel masks supported |
| 4192 | // by the profile in our device descriptor |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4193 | if (audio_device_is_digital(device)) { |
| 4194 | devDesc->importAudioPort(profile); |
| 4195 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4196 | break; |
| 4197 | } |
| 4198 | } |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4199 | if (j != outputs.size()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4200 | continue; |
| 4201 | } |
| 4202 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4203 | if (!profile->canOpenNewIo()) { |
| 4204 | ALOGW("Max Output number %u already opened for this profile %s", |
| 4205 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 4206 | continue; |
| 4207 | } |
| 4208 | |
Eric Laurent | 83efe1c | 2017-07-09 16:51:08 -0700 | [diff] [blame] | 4209 | ALOGV("opening output for device %08x with params %s profile %p name %s", |
| 4210 | device, address.string(), profile.get(), profile->getName().string()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4211 | desc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4212 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4213 | status_t status = desc->open(nullptr, device, address, |
| 4214 | AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, &output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4215 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4216 | if (status == NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4217 | // Here is where the out_set_parameters() for card & device gets called |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4218 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4219 | char *param = audio_device_address_to_parameter(device, address); |
| 4220 | mpClientInterface->setParameters(output, String8(param)); |
| 4221 | free(param); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4222 | } |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 4223 | updateAudioProfiles(device, output, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4224 | if (!profile->hasValidAudioProfile()) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4225 | ALOGW("checkOutputsForDevice() missing param"); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4226 | desc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4227 | output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4228 | } else if (profile->hasDynamicAudioProfile()) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4229 | desc->close(); |
Phil Burk | 702b105 | 2016-03-02 16:38:26 -0800 | [diff] [blame] | 4230 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4231 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 4232 | profile->pickAudioProfile( |
| 4233 | config.sample_rate, config.channel_mask, config.format); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4234 | config.offload_info.sample_rate = config.sample_rate; |
| 4235 | config.offload_info.channel_mask = config.channel_mask; |
| 4236 | config.offload_info.format = config.format; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4237 | |
| 4238 | status_t status = desc->open(&config, device, address, AUDIO_STREAM_DEFAULT, |
| 4239 | AUDIO_OUTPUT_FLAG_NONE, &output); |
| 4240 | if (status != NO_ERROR) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4241 | output = AUDIO_IO_HANDLE_NONE; |
| 4242 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4243 | } |
| 4244 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4245 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4246 | addOutput(output, desc); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4247 | if (device_distinguishes_on_address(device) && address != "0") { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4248 | sp<AudioPolicyMix> policyMix; |
| 4249 | if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4250 | ALOGE("checkOutputsForDevice() cannot find policy for address %s", |
| 4251 | address.string()); |
| 4252 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4253 | policyMix->setOutput(desc); |
Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 4254 | desc->mPolicyMix = policyMix->getMix(); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4255 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 4256 | } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
| 4257 | hasPrimaryOutput()) { |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 4258 | // no duplicated output for direct outputs and |
| 4259 | // outputs used by dynamic policy mixes |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4260 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4261 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4262 | //TODO: configure audio effect output stage here |
| 4263 | |
| 4264 | // 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] | 4265 | sp<SwAudioOutputDescriptor> dupOutputDesc = |
| 4266 | new SwAudioOutputDescriptor(NULL, mpClientInterface); |
| 4267 | status_t status = dupOutputDesc->openDuplicating(mPrimaryOutput, desc, |
| 4268 | &duplicatedOutput); |
| 4269 | if (status == NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4270 | // add duplicated output descriptor |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4271 | addOutput(duplicatedOutput, dupOutputDesc); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4272 | } else { |
| 4273 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4274 | mPrimaryOutput->mIoHandle, output); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4275 | desc->close(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4276 | removeOutput(output); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4277 | nextAudioPortGeneration(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4278 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4279 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4280 | } |
| 4281 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4282 | } else { |
| 4283 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4284 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4285 | if (output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4286 | ALOGW("checkOutputsForDevice() could not open output for device %x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4287 | profiles.removeAt(profile_index); |
| 4288 | profile_index--; |
| 4289 | } else { |
| 4290 | outputs.add(output); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4291 | // Load digital format info only for digital devices |
| 4292 | if (audio_device_is_digital(device)) { |
| 4293 | devDesc->importAudioPort(profile); |
| 4294 | } |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 4295 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4296 | if (device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4297 | ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)", |
| 4298 | device, address.string()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4299 | setOutputDevice(desc, device, true/*force*/, 0/*delay*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4300 | NULL/*patch handle*/, address.string()); |
| 4301 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4302 | ALOGV("checkOutputsForDevice(): adding output %d", output); |
| 4303 | } |
| 4304 | } |
| 4305 | |
| 4306 | if (profiles.isEmpty()) { |
| 4307 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 4308 | return BAD_VALUE; |
| 4309 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4310 | } else { // Disconnect |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4311 | // check if one opened output is not needed any more after disconnecting one device |
| 4312 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4313 | desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4314 | if (!desc->isDuplicated()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4315 | // exact match on device |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4316 | if (device_distinguishes_on_address(device) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4317 | (desc->supportedDevices() == device)) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4318 | findIoHandlesByAddress(desc, device, address, outputs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4319 | } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4320 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", |
| 4321 | mOutputs.keyAt(i)); |
| 4322 | outputs.add(mOutputs.keyAt(i)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4323 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4324 | } |
| 4325 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4326 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4327 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4328 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 4329 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4330 | if (profile->supportDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4331 | ALOGV("checkOutputsForDevice(): " |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4332 | "clearing direct output profile %zu on module %s", |
| 4333 | j, hwModule->getName()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4334 | profile->clearAudioProfiles(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4335 | } |
| 4336 | } |
| 4337 | } |
| 4338 | } |
| 4339 | return NO_ERROR; |
| 4340 | } |
| 4341 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4342 | status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4343 | audio_policy_dev_state_t state, |
| 4344 | SortedVector<audio_io_handle_t>& inputs, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4345 | const String8& address) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4346 | { |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4347 | audio_devices_t device = devDesc->type(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4348 | sp<AudioInputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4349 | |
| 4350 | if (audio_device_is_digital(device)) { |
| 4351 | // erase all current sample rates, formats and channel masks |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 4352 | devDesc->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4353 | } |
| 4354 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4355 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 4356 | // first list already open inputs that can be routed to this device |
| 4357 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4358 | desc = mInputs.valueAt(input_index); |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4359 | if (desc->mProfile->supportDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4360 | ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index)); |
| 4361 | inputs.add(mInputs.keyAt(input_index)); |
| 4362 | } |
| 4363 | } |
| 4364 | |
| 4365 | // then look for input profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4366 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4367 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4368 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4369 | profile_index < hwModule->getInputProfiles().size(); |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4370 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4371 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4372 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4373 | if (profile->supportDevice(device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4374 | if (!device_distinguishes_on_address(device) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4375 | profile->supportDeviceAddress(address)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4376 | profiles.add(profile); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4377 | ALOGV("checkInputsForDevice(): adding profile %zu from module %s", |
| 4378 | profile_index, hwModule->getName()); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4379 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4380 | } |
| 4381 | } |
| 4382 | } |
| 4383 | |
| 4384 | if (profiles.isEmpty() && inputs.isEmpty()) { |
| 4385 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 4386 | return BAD_VALUE; |
| 4387 | } |
| 4388 | |
| 4389 | // open inputs for matching profiles if needed. Direct inputs are also opened to |
| 4390 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 4391 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { |
| 4392 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4393 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4394 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4395 | // nothing to do if one input is already opened for this profile |
| 4396 | size_t input_index; |
| 4397 | for (input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4398 | desc = mInputs.valueAt(input_index); |
| 4399 | if (desc->mProfile == profile) { |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4400 | if (audio_device_is_digital(device)) { |
| 4401 | devDesc->importAudioPort(profile); |
| 4402 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4403 | break; |
| 4404 | } |
| 4405 | } |
| 4406 | if (input_index != mInputs.size()) { |
| 4407 | continue; |
| 4408 | } |
| 4409 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4410 | if (!profile->canOpenNewIo()) { |
| 4411 | ALOGW("Max Input number %u already opened for this profile %s", |
| 4412 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 4413 | continue; |
| 4414 | } |
| 4415 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4416 | desc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4417 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4418 | status_t status = desc->open(nullptr, |
| 4419 | device, |
| 4420 | address, |
| 4421 | AUDIO_SOURCE_MIC, |
| 4422 | AUDIO_INPUT_FLAG_NONE, |
| 4423 | &input); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4424 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4425 | if (status == NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4426 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4427 | char *param = audio_device_address_to_parameter(device, address); |
| 4428 | mpClientInterface->setParameters(input, String8(param)); |
| 4429 | free(param); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4430 | } |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 4431 | updateAudioProfiles(device, input, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4432 | if (!profile->hasValidAudioProfile()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4433 | ALOGW("checkInputsForDevice() direct input missing param"); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4434 | desc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4435 | input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4436 | } |
| 4437 | |
| 4438 | if (input != 0) { |
| 4439 | addInput(input, desc); |
| 4440 | } |
| 4441 | } // endif input != 0 |
| 4442 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4443 | if (input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4444 | ALOGW("checkInputsForDevice() could not open input for device 0x%X", device); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4445 | profiles.removeAt(profile_index); |
| 4446 | profile_index--; |
| 4447 | } else { |
| 4448 | inputs.add(input); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4449 | if (audio_device_is_digital(device)) { |
| 4450 | devDesc->importAudioPort(profile); |
| 4451 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4452 | ALOGV("checkInputsForDevice(): adding input %d", input); |
| 4453 | } |
| 4454 | } // end scan profiles |
| 4455 | |
| 4456 | if (profiles.isEmpty()) { |
| 4457 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 4458 | return BAD_VALUE; |
| 4459 | } |
| 4460 | } else { |
| 4461 | // Disconnect |
| 4462 | // check if one opened input is not needed any more after disconnecting one device |
| 4463 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4464 | desc = mInputs.valueAt(input_index); |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4465 | if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4466 | ALOGV("checkInputsForDevice(): disconnecting adding input %d", |
| 4467 | mInputs.keyAt(input_index)); |
| 4468 | inputs.add(mInputs.keyAt(input_index)); |
| 4469 | } |
| 4470 | } |
| 4471 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4472 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4473 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4474 | profile_index < hwModule->getInputProfiles().size(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4475 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4476 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4477 | if (profile->supportDevice(device)) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4478 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %s", |
| 4479 | profile_index, hwModule->getName()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4480 | profile->clearAudioProfiles(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4481 | } |
| 4482 | } |
| 4483 | } |
| 4484 | } // end disconnect |
| 4485 | |
| 4486 | return NO_ERROR; |
| 4487 | } |
| 4488 | |
| 4489 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4490 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4491 | { |
| 4492 | ALOGV("closeOutput(%d)", output); |
| 4493 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4494 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4495 | if (outputDesc == NULL) { |
| 4496 | ALOGW("closeOutput() unknown output %d", output); |
| 4497 | return; |
| 4498 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4499 | mPolicyMixes.closeOutput(outputDesc); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4500 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4501 | // look for duplicated outputs connected to the output being removed. |
| 4502 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4503 | sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4504 | if (dupOutputDesc->isDuplicated() && |
| 4505 | (dupOutputDesc->mOutput1 == outputDesc || |
| 4506 | dupOutputDesc->mOutput2 == outputDesc)) { |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4507 | sp<SwAudioOutputDescriptor> outputDesc2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4508 | if (dupOutputDesc->mOutput1 == outputDesc) { |
| 4509 | outputDesc2 = dupOutputDesc->mOutput2; |
| 4510 | } else { |
| 4511 | outputDesc2 = dupOutputDesc->mOutput1; |
| 4512 | } |
| 4513 | // As all active tracks on duplicated output will be deleted, |
| 4514 | // and as they were also referenced on the other output, the reference |
| 4515 | // count for their stream type must be adjusted accordingly on |
| 4516 | // the other output. |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4517 | bool wasActive = outputDesc2->isActive(); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4518 | for (int j = 0; j < AUDIO_STREAM_CNT; j++) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4519 | int refCount = dupOutputDesc->mRefCount[j]; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4520 | outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4521 | } |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4522 | // stop() will be a no op if the output is still active but is needed in case all |
| 4523 | // active streams refcounts where cleared above |
| 4524 | if (wasActive) { |
| 4525 | outputDesc2->stop(); |
| 4526 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4527 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); |
| 4528 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); |
| 4529 | |
| 4530 | mpClientInterface->closeOutput(duplicatedOutput); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4531 | removeOutput(duplicatedOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4532 | } |
| 4533 | } |
| 4534 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4535 | nextAudioPortGeneration(); |
| 4536 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4537 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4538 | if (index >= 0) { |
| 4539 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4540 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4541 | mAudioPatches.removeItemsAt(index); |
| 4542 | mpClientInterface->onAudioPatchListUpdate(); |
| 4543 | } |
| 4544 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4545 | outputDesc->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4546 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4547 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4548 | mPreviousOutputs = mOutputs; |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4549 | } |
| 4550 | |
| 4551 | void AudioPolicyManager::closeInput(audio_io_handle_t input) |
| 4552 | { |
| 4553 | ALOGV("closeInput(%d)", input); |
| 4554 | |
| 4555 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
| 4556 | if (inputDesc == NULL) { |
| 4557 | ALOGW("closeInput() unknown input %d", input); |
| 4558 | return; |
| 4559 | } |
| 4560 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4561 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4562 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4563 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4564 | if (index >= 0) { |
| 4565 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4566 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4567 | mAudioPatches.removeItemsAt(index); |
| 4568 | mpClientInterface->onAudioPatchListUpdate(); |
| 4569 | } |
| 4570 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4571 | inputDesc->close(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4572 | mInputs.removeItem(input); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4573 | } |
| 4574 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4575 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice( |
| 4576 | audio_devices_t device, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4577 | const SwAudioOutputCollection& openOutputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4578 | { |
| 4579 | SortedVector<audio_io_handle_t> outputs; |
| 4580 | |
| 4581 | ALOGVV("getOutputsForDevice() device %04x", device); |
| 4582 | for (size_t i = 0; i < openOutputs.size(); i++) { |
Eric Laurent | 37ddbb4 | 2016-08-10 16:19:14 -0700 | [diff] [blame] | 4583 | ALOGVV("output %zu isDuplicated=%d device=%04x", |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4584 | i, openOutputs.valueAt(i)->isDuplicated(), |
| 4585 | openOutputs.valueAt(i)->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4586 | if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) { |
| 4587 | ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i)); |
| 4588 | outputs.add(openOutputs.keyAt(i)); |
| 4589 | } |
| 4590 | } |
| 4591 | return outputs; |
| 4592 | } |
| 4593 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4594 | bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4595 | SortedVector<audio_io_handle_t>& outputs2) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4596 | { |
| 4597 | if (outputs1.size() != outputs2.size()) { |
| 4598 | return false; |
| 4599 | } |
| 4600 | for (size_t i = 0; i < outputs1.size(); i++) { |
| 4601 | if (outputs1[i] != outputs2[i]) { |
| 4602 | return false; |
| 4603 | } |
| 4604 | } |
| 4605 | return true; |
| 4606 | } |
| 4607 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4608 | void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4609 | { |
| 4610 | audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/); |
| 4611 | audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 4612 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs); |
| 4613 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs); |
| 4614 | |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4615 | // also take into account external policy-related changes: add all outputs which are |
| 4616 | // associated with policies in the "before" and "after" output vectors |
| 4617 | ALOGVV("checkOutputForStrategy(): policy related outputs"); |
| 4618 | for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4619 | const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4620 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4621 | srcOutputs.add(desc->mIoHandle); |
| 4622 | ALOGVV(" previous outputs: adding %d", desc->mIoHandle); |
| 4623 | } |
| 4624 | } |
| 4625 | for (size_t i = 0 ; i < mOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4626 | const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4627 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4628 | dstOutputs.add(desc->mIoHandle); |
| 4629 | ALOGVV(" new outputs: adding %d", desc->mIoHandle); |
| 4630 | } |
| 4631 | } |
| 4632 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4633 | if (!vectorsEqual(srcOutputs,dstOutputs)) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 4634 | // get maximum latency of all source outputs to determine the minimum mute time guaranteeing |
| 4635 | // audio from invalidated tracks will be rendered when unmuting |
| 4636 | uint32_t maxLatency = 0; |
| 4637 | for (audio_io_handle_t srcOut : srcOutputs) { |
| 4638 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); |
| 4639 | if (desc != 0 && maxLatency < desc->latency()) { |
| 4640 | maxLatency = desc->latency(); |
| 4641 | } |
| 4642 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4643 | ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d", |
| 4644 | strategy, srcOutputs[0], dstOutputs[0]); |
| 4645 | // mute strategy while moving tracks from one output to another |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4646 | for (audio_io_handle_t srcOut : srcOutputs) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 4647 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); |
| 4648 | if (desc != 0 && isStrategyActive(desc, strategy)) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4649 | setStrategyMute(strategy, true, desc); |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 4650 | setStrategyMute(strategy, false, desc, maxLatency * LATENCY_MUTE_FACTOR, newDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4651 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4652 | sp<AudioSourceDescriptor> source = |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4653 | getSourceForStrategyOnOutput(srcOut, strategy); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4654 | if (source != 0){ |
| 4655 | connectAudioSource(source); |
| 4656 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4657 | } |
| 4658 | |
| 4659 | // Move effects associated to this strategy from previous output to new output |
| 4660 | if (strategy == STRATEGY_MEDIA) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4661 | selectOutputForMusicEffects(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4662 | } |
| 4663 | // Move tracks associated to this strategy from previous output to new output |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4664 | for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4665 | if (getStrategy((audio_stream_type_t)i) == strategy) { |
| 4666 | mpClientInterface->invalidateStream((audio_stream_type_t)i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4667 | } |
| 4668 | } |
| 4669 | } |
| 4670 | } |
| 4671 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4672 | void AudioPolicyManager::checkOutputForAllStrategies() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4673 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4674 | 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] | 4675 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4676 | checkOutputForStrategy(STRATEGY_PHONE); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4677 | 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] | 4678 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4679 | checkOutputForStrategy(STRATEGY_SONIFICATION); |
| 4680 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4681 | checkOutputForStrategy(STRATEGY_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4682 | checkOutputForStrategy(STRATEGY_MEDIA); |
| 4683 | checkOutputForStrategy(STRATEGY_DTMF); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4684 | checkOutputForStrategy(STRATEGY_REROUTING); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4685 | } |
| 4686 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4687 | void AudioPolicyManager::checkA2dpSuspend() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4688 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4689 | audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput(); |
Aniket Kumar Lata | a8ee996 | 2018-01-31 20:24:23 -0800 | [diff] [blame] | 4690 | if (a2dpOutput == 0 || mOutputs.isA2dpOffloadedOnPrimary()) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4691 | mA2dpSuspended = false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4692 | return; |
| 4693 | } |
| 4694 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4695 | bool isScoConnected = |
Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 4696 | ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET & |
| 4697 | ~AUDIO_DEVICE_BIT_IN) != 0) || |
| 4698 | ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0); |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4699 | |
| 4700 | // if suspended, restore A2DP output if: |
| 4701 | // ((SCO device is NOT connected) || |
| 4702 | // ((forced usage communication is NOT SCO) && (forced usage for record is NOT SCO) && |
| 4703 | // (phone state is NOT in call) && (phone state is NOT ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4704 | // |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4705 | // if not suspended, suspend A2DP output if: |
| 4706 | // (SCO device is connected) && |
| 4707 | // ((forced usage for communication is SCO) || (forced usage for record is SCO) || |
| 4708 | // ((phone state is in call) || (phone state is ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4709 | // |
| 4710 | if (mA2dpSuspended) { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4711 | if (!isScoConnected || |
| 4712 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != |
| 4713 | AUDIO_POLICY_FORCE_BT_SCO) && |
| 4714 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != |
| 4715 | AUDIO_POLICY_FORCE_BT_SCO) && |
| 4716 | (mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4717 | (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4718 | |
| 4719 | mpClientInterface->restoreOutput(a2dpOutput); |
| 4720 | mA2dpSuspended = false; |
| 4721 | } |
| 4722 | } else { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4723 | if (isScoConnected && |
| 4724 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == |
| 4725 | AUDIO_POLICY_FORCE_BT_SCO) || |
| 4726 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == |
| 4727 | AUDIO_POLICY_FORCE_BT_SCO) || |
| 4728 | (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4729 | (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4730 | |
| 4731 | mpClientInterface->suspendOutput(a2dpOutput); |
| 4732 | mA2dpSuspended = true; |
| 4733 | } |
| 4734 | } |
| 4735 | } |
| 4736 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4737 | audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
| 4738 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4739 | { |
| 4740 | audio_devices_t device = AUDIO_DEVICE_NONE; |
| 4741 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4742 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4743 | if (index >= 0) { |
| 4744 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4745 | if (patchDesc->mUid != mUidCached) { |
| 4746 | ALOGV("getNewOutputDevice() device %08x forced by patch %d", |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4747 | outputDesc->device(), outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4748 | return outputDesc->device(); |
| 4749 | } |
| 4750 | } |
| 4751 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4752 | // 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] | 4753 | // 1: the strategy enforced audible is active and enforced on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4754 | // use device for strategy enforced audible |
| 4755 | // 2: we are in call or the strategy phone is active on the output: |
| 4756 | // use device for strategy phone |
Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4757 | // 3: the strategy sonification is active on the output: |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4758 | // use device for strategy sonification |
Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4759 | // 4: the strategy for enforced audible is active but not enforced on the output: |
| 4760 | // use the device for strategy enforced audible |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4761 | // 5: the strategy accessibility is active on the output: |
| 4762 | // use device for strategy accessibility |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4763 | // 6: the strategy "respectful" sonification is active on the output: |
| 4764 | // use device for strategy "respectful" sonification |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4765 | // 7: the strategy media is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4766 | // use device for strategy media |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4767 | // 8: the strategy DTMF is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4768 | // use device for strategy DTMF |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4769 | // 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] | 4770 | // use device for strategy t-t-s |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4771 | if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4772 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4773 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
| 4774 | } else if (isInCall() || |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4775 | isStrategyActive(outputDesc, STRATEGY_PHONE)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4776 | device = getDeviceForStrategy(STRATEGY_PHONE, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4777 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4778 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache); |
Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4779 | } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) { |
| 4780 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4781 | } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) { |
| 4782 | device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4783 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4784 | device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4785 | } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4786 | device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4787 | } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4788 | device = getDeviceForStrategy(STRATEGY_DTMF, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4789 | } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4790 | device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4791 | } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4792 | device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4793 | } |
| 4794 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4795 | ALOGV("getNewOutputDevice() selected device %x", device); |
| 4796 | return device; |
| 4797 | } |
| 4798 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4799 | audio_devices_t AudioPolicyManager::getNewInputDevice(const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4800 | { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4801 | audio_devices_t device = AUDIO_DEVICE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4802 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4803 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4804 | if (index >= 0) { |
| 4805 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4806 | if (patchDesc->mUid != mUidCached) { |
| 4807 | ALOGV("getNewInputDevice() device %08x forced by patch %d", |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4808 | inputDesc->mDevice, inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4809 | return inputDesc->mDevice; |
| 4810 | } |
| 4811 | } |
| 4812 | |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 4813 | // If we are not in call and no client is active on this input, this methods returns |
| 4814 | // 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] | 4815 | audio_source_t source = inputDesc->getHighestPrioritySource(true /*activeOnly*/); |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 4816 | if (source == AUDIO_SOURCE_DEFAULT && isInCall()) { |
| 4817 | source = AUDIO_SOURCE_VOICE_COMMUNICATION; |
| 4818 | } |
| 4819 | if (source != AUDIO_SOURCE_DEFAULT) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4820 | device = getDeviceAndMixForInputSource(source); |
| 4821 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4822 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4823 | return device; |
| 4824 | } |
| 4825 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4826 | bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1, |
| 4827 | audio_stream_type_t stream2) { |
Jean-Michel Trivi | 99bb2f9 | 2016-11-23 15:52:07 -0800 | [diff] [blame] | 4828 | return (stream1 == stream2); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4829 | } |
| 4830 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4831 | uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4832 | return (uint32_t)getStrategy(stream); |
| 4833 | } |
| 4834 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4835 | audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4836 | // By checking the range of stream before calling getStrategy, we avoid |
| 4837 | // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE |
| 4838 | // 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] | 4839 | if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4840 | return AUDIO_DEVICE_NONE; |
| 4841 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4842 | audio_devices_t devices = AUDIO_DEVICE_NONE; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4843 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 4844 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4845 | continue; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4846 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4847 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4848 | audio_devices_t curDevices = |
Eric Laurent | 447a87b | 2016-07-07 17:32:10 -0700 | [diff] [blame] | 4849 | getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/); |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4850 | for (audio_io_handle_t output : getOutputsForDevice(curDevices, mOutputs)) { |
| 4851 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4852 | if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4853 | curDevices |= outputDesc->device(); |
| 4854 | } |
| 4855 | } |
| 4856 | devices |= curDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4857 | } |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4858 | |
| 4859 | /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it |
| 4860 | and doesn't really need to.*/ |
| 4861 | if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) { |
| 4862 | devices |= AUDIO_DEVICE_OUT_SPEAKER; |
| 4863 | devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
| 4864 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4865 | return devices; |
| 4866 | } |
| 4867 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4868 | routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const |
| 4869 | { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4870 | 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] | 4871 | return mEngine->getStrategyForStream(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4872 | } |
| 4873 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4874 | uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) { |
| 4875 | // flags to strategy mapping |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4876 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 4877 | return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER; |
| 4878 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4879 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 4880 | return (uint32_t) STRATEGY_ENFORCED_AUDIBLE; |
| 4881 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4882 | // usage to strategy mapping |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4883 | return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage)); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4884 | } |
| 4885 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4886 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4887 | switch(stream) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4888 | case AUDIO_STREAM_MUSIC: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4889 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
| 4890 | updateDevicesAndOutputs(); |
| 4891 | break; |
| 4892 | default: |
| 4893 | break; |
| 4894 | } |
| 4895 | } |
| 4896 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4897 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 4898 | |
| 4899 | // skip beacon mute management if a dedicated TTS output is available |
| 4900 | if (mTtsOutputAvailable) { |
| 4901 | return 0; |
| 4902 | } |
| 4903 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4904 | switch(event) { |
| 4905 | case STARTING_OUTPUT: |
| 4906 | mBeaconMuteRefCount++; |
| 4907 | break; |
| 4908 | case STOPPING_OUTPUT: |
| 4909 | if (mBeaconMuteRefCount > 0) { |
| 4910 | mBeaconMuteRefCount--; |
| 4911 | } |
| 4912 | break; |
| 4913 | case STARTING_BEACON: |
| 4914 | mBeaconPlayingRefCount++; |
| 4915 | break; |
| 4916 | case STOPPING_BEACON: |
| 4917 | if (mBeaconPlayingRefCount > 0) { |
| 4918 | mBeaconPlayingRefCount--; |
| 4919 | } |
| 4920 | break; |
| 4921 | } |
| 4922 | |
| 4923 | if (mBeaconMuteRefCount > 0) { |
| 4924 | // any playback causes beacon to be muted |
| 4925 | return setBeaconMute(true); |
| 4926 | } else { |
| 4927 | // no other playback: unmute when beacon starts playing, mute when it stops |
| 4928 | return setBeaconMute(mBeaconPlayingRefCount == 0); |
| 4929 | } |
| 4930 | } |
| 4931 | |
| 4932 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { |
| 4933 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", |
| 4934 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); |
| 4935 | // keep track of muted state to avoid repeating mute/unmute operations |
| 4936 | if (mBeaconMuted != mute) { |
| 4937 | // mute/unmute AUDIO_STREAM_TTS on all outputs |
| 4938 | ALOGV("\t muting %d", mute); |
| 4939 | uint32_t maxLatency = 0; |
| 4940 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4941 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4942 | setStreamMute(AUDIO_STREAM_TTS, mute/*on*/, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4943 | desc, |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4944 | 0 /*delay*/, AUDIO_DEVICE_NONE); |
| 4945 | const uint32_t latency = desc->latency() * 2; |
| 4946 | if (latency > maxLatency) { |
| 4947 | maxLatency = latency; |
| 4948 | } |
| 4949 | } |
| 4950 | mBeaconMuted = mute; |
| 4951 | return maxLatency; |
| 4952 | } |
| 4953 | return 0; |
| 4954 | } |
| 4955 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4956 | audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4957 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4958 | { |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4959 | // Routing |
| 4960 | // see if we have an explicit route |
| 4961 | // scan the whole RouteMap, for each entry, convert the stream type to a strategy |
| 4962 | // (getStrategy(stream)). |
| 4963 | // if the strategy from the stream type in the RouteMap is the same as the argument above, |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 4964 | // and activity count is non-zero and the device in the route descriptor is available |
| 4965 | // then select this device. |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4966 | for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) { |
| 4967 | sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4968 | routing_strategy routeStrategy = getStrategy(route->mStreamType); |
Eric Laurent | 2157f5b | 2018-04-25 18:33:02 -0700 | [diff] [blame] | 4969 | if ((routeStrategy == strategy) && route->isActiveOrChanged() && |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 4970 | (mAvailableOutputDevices.indexOf(route->mDeviceDescriptor) >= 0)) { |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4971 | return route->mDeviceDescriptor->type(); |
| 4972 | } |
| 4973 | } |
| 4974 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4975 | if (fromCache) { |
| 4976 | ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x", |
| 4977 | strategy, mDeviceForStrategy[strategy]); |
| 4978 | return mDeviceForStrategy[strategy]; |
| 4979 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4980 | return mEngine->getDeviceForStrategy(strategy); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4981 | } |
| 4982 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4983 | void AudioPolicyManager::updateDevicesAndOutputs() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4984 | { |
| 4985 | for (int i = 0; i < NUM_STRATEGIES; i++) { |
| 4986 | mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
| 4987 | } |
| 4988 | mPreviousOutputs = mOutputs; |
| 4989 | } |
| 4990 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4991 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4992 | audio_devices_t prevDevice, |
| 4993 | uint32_t delayMs) |
| 4994 | { |
| 4995 | // mute/unmute strategies using an incompatible device combination |
| 4996 | // if muting, wait for the audio in pcm buffer to be drained before proceeding |
| 4997 | // if unmuting, unmute only after the specified delay |
| 4998 | if (outputDesc->isDuplicated()) { |
| 4999 | return 0; |
| 5000 | } |
| 5001 | |
| 5002 | uint32_t muteWaitMs = 0; |
| 5003 | audio_devices_t device = outputDesc->device(); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5004 | bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5005 | |
| 5006 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
| 5007 | audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5008 | curDevice = curDevice & outputDesc->supportedDevices(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5009 | bool mute = shouldMute && (curDevice & device) && (curDevice != device); |
| 5010 | bool doMute = false; |
| 5011 | |
| 5012 | if (mute && !outputDesc->mStrategyMutedByDevice[i]) { |
| 5013 | doMute = true; |
| 5014 | outputDesc->mStrategyMutedByDevice[i] = true; |
| 5015 | } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){ |
| 5016 | doMute = true; |
| 5017 | outputDesc->mStrategyMutedByDevice[i] = false; |
| 5018 | } |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5019 | if (doMute) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5020 | for (size_t j = 0; j < mOutputs.size(); j++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5021 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5022 | // skip output if it does not share any device with current output |
| 5023 | if ((desc->supportedDevices() & outputDesc->supportedDevices()) |
| 5024 | == AUDIO_DEVICE_NONE) { |
| 5025 | continue; |
| 5026 | } |
Eric Laurent | 37ddbb4 | 2016-08-10 16:19:14 -0700 | [diff] [blame] | 5027 | ALOGVV("checkDeviceMuteStrategies() %s strategy %zu (curDevice %04x)", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5028 | mute ? "muting" : "unmuting", i, curDevice); |
| 5029 | setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5030 | if (isStrategyActive(desc, (routing_strategy)i)) { |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5031 | if (mute) { |
| 5032 | // FIXME: should not need to double latency if volume could be applied |
| 5033 | // immediately by the audioflinger mixer. We must account for the delay |
| 5034 | // between now and the next time the audioflinger thread for this output |
| 5035 | // will process a buffer (which corresponds to one buffer size, |
| 5036 | // usually 1/2 or 1/4 of the latency). |
| 5037 | if (muteWaitMs < desc->latency() * 2) { |
| 5038 | muteWaitMs = desc->latency() * 2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5039 | } |
| 5040 | } |
| 5041 | } |
| 5042 | } |
| 5043 | } |
| 5044 | } |
| 5045 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5046 | // temporary mute output if device selection changes to avoid volume bursts due to |
| 5047 | // different per device volumes |
| 5048 | if (outputDesc->isActive() && (device != prevDevice)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5049 | uint32_t tempMuteWaitMs = outputDesc->latency() * 2; |
| 5050 | // temporary mute duration is conservatively set to 4 times the reported latency |
| 5051 | uint32_t tempMuteDurationMs = outputDesc->latency() * 4; |
| 5052 | if (muteWaitMs < tempMuteWaitMs) { |
| 5053 | muteWaitMs = tempMuteWaitMs; |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5054 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5055 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5056 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5057 | if (isStrategyActive(outputDesc, (routing_strategy)i)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5058 | // make sure that we do not start the temporary mute period too early in case of |
| 5059 | // delayed device change |
| 5060 | setStrategyMute((routing_strategy)i, true, outputDesc, delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5061 | setStrategyMute((routing_strategy)i, false, outputDesc, |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5062 | delayMs + tempMuteDurationMs, device); |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5063 | } |
| 5064 | } |
| 5065 | } |
| 5066 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5067 | // wait for the PCM output buffers to empty before proceeding with the rest of the command |
| 5068 | if (muteWaitMs > delayMs) { |
| 5069 | muteWaitMs -= delayMs; |
| 5070 | usleep(muteWaitMs * 1000); |
| 5071 | return muteWaitMs; |
| 5072 | } |
| 5073 | return 0; |
| 5074 | } |
| 5075 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5076 | uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5077 | audio_devices_t device, |
| 5078 | bool force, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5079 | int delayMs, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5080 | audio_patch_handle_t *patchHandle, |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5081 | const char *address, |
| 5082 | bool requiresMuteCheck) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5083 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5084 | ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5085 | AudioParameter param; |
| 5086 | uint32_t muteWaitMs; |
| 5087 | |
| 5088 | if (outputDesc->isDuplicated()) { |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5089 | muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs, |
| 5090 | nullptr /* patchHandle */, nullptr /* address */, requiresMuteCheck); |
| 5091 | muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs, |
| 5092 | nullptr /* patchHandle */, nullptr /* address */, requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5093 | return muteWaitMs; |
| 5094 | } |
| 5095 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current |
| 5096 | // output profile |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5097 | if ((device != AUDIO_DEVICE_NONE) && |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5098 | ((device & outputDesc->supportedDevices()) == AUDIO_DEVICE_NONE)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5099 | return 0; |
| 5100 | } |
| 5101 | |
| 5102 | // filter devices according to output selected |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5103 | device = (audio_devices_t)(device & outputDesc->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5104 | |
| 5105 | audio_devices_t prevDevice = outputDesc->mDevice; |
| 5106 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 5107 | ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5108 | |
| 5109 | if (device != AUDIO_DEVICE_NONE) { |
| 5110 | outputDesc->mDevice = device; |
| 5111 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5112 | |
| 5113 | // if the outputs are not materially active, there is no need to mute. |
| 5114 | if (requiresMuteCheck) { |
| 5115 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs); |
| 5116 | } else { |
| 5117 | ALOGV("%s: suppressing checkDeviceMuteStrategies", __func__); |
| 5118 | muteWaitMs = 0; |
| 5119 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5120 | |
| 5121 | // Do not change the routing if: |
Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 5122 | // the requested device is AUDIO_DEVICE_NONE |
| 5123 | // OR the requested device is the same as current device |
| 5124 | // AND force is not specified |
| 5125 | // AND the output is connected by a valid audio patch. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5126 | // Doing this check here allows the caller to call setOutputDevice() without conditions |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 5127 | if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && |
| 5128 | !force && |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 5129 | outputDesc->getPatchHandle() != 0) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5130 | ALOGV("setOutputDevice() setting same device 0x%04x or null device", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5131 | return muteWaitMs; |
| 5132 | } |
| 5133 | |
| 5134 | ALOGV("setOutputDevice() changing device"); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5135 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5136 | // do the routing |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5137 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5138 | resetOutputDevice(outputDesc, delayMs, NULL); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5139 | } else { |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 5140 | DeviceVector deviceList; |
| 5141 | if ((address == NULL) || (strlen(address) == 0)) { |
| 5142 | deviceList = mAvailableOutputDevices.getDevicesFromType(device); |
| 5143 | } else { |
| 5144 | deviceList = mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address)); |
| 5145 | } |
| 5146 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5147 | if (!deviceList.isEmpty()) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5148 | PatchBuilder patchBuilder; |
| 5149 | patchBuilder.addSource(outputDesc); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5150 | 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] | 5151 | patchBuilder.addSink(deviceList.itemAt(i)); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5152 | } |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5153 | installPatch(__func__, patchHandle, outputDesc.get(), patchBuilder.patch(), delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5154 | } |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 5155 | |
| 5156 | // inform all input as well |
| 5157 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 5158 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5159 | if (!is_virtual_input_device(inputDescriptor->mDevice)) { |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 5160 | AudioParameter inputCmd = AudioParameter(); |
| 5161 | ALOGV("%s: inform input %d of device:%d", __func__, |
| 5162 | inputDescriptor->mIoHandle, device); |
| 5163 | inputCmd.addInt(String8(AudioParameter::keyRouting),device); |
| 5164 | mpClientInterface->setParameters(inputDescriptor->mIoHandle, |
| 5165 | inputCmd.toString(), |
| 5166 | delayMs); |
| 5167 | } |
| 5168 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5169 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5170 | |
| 5171 | // update stream volumes according to new device |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5172 | applyStreamVolumes(outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5173 | |
| 5174 | return muteWaitMs; |
| 5175 | } |
| 5176 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5177 | status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5178 | int delayMs, |
| 5179 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5180 | { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5181 | ssize_t index; |
| 5182 | if (patchHandle) { |
| 5183 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 5184 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 5185 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5186 | } |
| 5187 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5188 | return INVALID_OPERATION; |
| 5189 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5190 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 5191 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5192 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 5193 | outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5194 | removeAudioPatch(patchDesc->mHandle); |
| 5195 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5196 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5197 | return status; |
| 5198 | } |
| 5199 | |
| 5200 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, |
| 5201 | audio_devices_t device, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5202 | bool force, |
| 5203 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5204 | { |
| 5205 | status_t status = NO_ERROR; |
| 5206 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5207 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5208 | if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) { |
| 5209 | inputDesc->mDevice = device; |
| 5210 | |
| 5211 | DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device); |
| 5212 | if (!deviceList.isEmpty()) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5213 | PatchBuilder patchBuilder; |
| 5214 | patchBuilder.addSink(inputDesc, |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 5215 | // AUDIO_SOURCE_HOTWORD is for internal use only: |
| 5216 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5217 | [inputDesc](const PatchBuilder::mix_usecase_t& usecase) { |
| 5218 | auto result = usecase; |
| 5219 | if (result.source == AUDIO_SOURCE_HOTWORD && !inputDesc->isSoundTrigger()) { |
| 5220 | result.source = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 5221 | } |
| 5222 | return result; }). |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5223 | //only one input device for now |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5224 | addSource(deviceList.itemAt(0)); |
| 5225 | status = installPatch(__func__, patchHandle, inputDesc.get(), patchBuilder.patch(), 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5226 | } |
| 5227 | } |
| 5228 | return status; |
| 5229 | } |
| 5230 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5231 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, |
| 5232 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5233 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5234 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5235 | ssize_t index; |
| 5236 | if (patchHandle) { |
| 5237 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 5238 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 5239 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5240 | } |
| 5241 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5242 | return INVALID_OPERATION; |
| 5243 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5244 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 5245 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5246 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 5247 | inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5248 | removeAudioPatch(patchDesc->mHandle); |
| 5249 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5250 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5251 | return status; |
| 5252 | } |
| 5253 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 5254 | sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5255 | const String8& address, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5256 | uint32_t& samplingRate, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5257 | audio_format_t& format, |
| 5258 | audio_channel_mask_t& channelMask, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5259 | audio_input_flags_t flags) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5260 | { |
| 5261 | // Choose an input profile based on the requested capture parameters: select the first available |
| 5262 | // profile supporting all requested parameters. |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5263 | // |
| 5264 | // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return |
| 5265 | // the best matching profile, not the first one. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5266 | |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5267 | sp<IOProfile> firstInexact; |
| 5268 | uint32_t updatedSamplingRate = 0; |
| 5269 | audio_format_t updatedFormat = AUDIO_FORMAT_INVALID; |
| 5270 | audio_channel_mask_t updatedChannelMask = AUDIO_CHANNEL_INVALID; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 5271 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 5272 | for (const auto& profile : hwModule->getInputProfiles()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5273 | // profile->log(); |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5274 | //updatedFormat = format; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5275 | if (profile->isCompatibleProfile(device, address, samplingRate, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5276 | &samplingRate /*updatedSamplingRate*/, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5277 | format, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5278 | &format, /*updatedFormat*/ |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5279 | channelMask, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5280 | &channelMask /*updatedChannelMask*/, |
| 5281 | // FIXME ugly cast |
| 5282 | (audio_output_flags_t) flags, |
| 5283 | true /*exactMatchRequiredForInputFlags*/)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5284 | return profile; |
| 5285 | } |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5286 | if (firstInexact == nullptr && profile->isCompatibleProfile(device, address, |
| 5287 | samplingRate, |
| 5288 | &updatedSamplingRate, |
| 5289 | format, |
| 5290 | &updatedFormat, |
| 5291 | channelMask, |
| 5292 | &updatedChannelMask, |
| 5293 | // FIXME ugly cast |
| 5294 | (audio_output_flags_t) flags, |
| 5295 | false /*exactMatchRequiredForInputFlags*/)) { |
| 5296 | firstInexact = profile; |
| 5297 | } |
| 5298 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5299 | } |
| 5300 | } |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5301 | if (firstInexact != nullptr) { |
| 5302 | samplingRate = updatedSamplingRate; |
| 5303 | format = updatedFormat; |
| 5304 | channelMask = updatedChannelMask; |
| 5305 | return firstInexact; |
| 5306 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5307 | return NULL; |
| 5308 | } |
| 5309 | |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5310 | |
| 5311 | audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5312 | AudioMix **policyMix) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5313 | { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 5314 | audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
| 5315 | audio_devices_t selectedDeviceFromMix = |
| 5316 | mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5317 | |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 5318 | if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) { |
| 5319 | return selectedDeviceFromMix; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5320 | } |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5321 | return getDeviceForInputSource(inputSource); |
| 5322 | } |
| 5323 | |
| 5324 | audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource) |
| 5325 | { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 5326 | // Routing |
| 5327 | // Scan the whole RouteMap to see if we have an explicit route: |
| 5328 | // if the input source in the RouteMap is the same as the argument above, |
| 5329 | // and activity count is non-zero and the device in the route descriptor is available |
| 5330 | // then select this device. |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 5331 | for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) { |
| 5332 | sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex); |
Eric Laurent | 2157f5b | 2018-04-25 18:33:02 -0700 | [diff] [blame] | 5333 | if ((inputSource == route->mSource) && route->isActiveOrChanged() && |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 5334 | (mAvailableInputDevices.indexOf(route->mDeviceDescriptor) >= 0)) { |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 5335 | return route->mDeviceDescriptor->type(); |
| 5336 | } |
| 5337 | } |
| 5338 | |
| 5339 | return mEngine->getDeviceForInputSource(inputSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5340 | } |
| 5341 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5342 | float AudioPolicyManager::computeVolume(audio_stream_type_t stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5343 | int index, |
| 5344 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5345 | { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5346 | float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index); |
Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 5347 | |
| 5348 | // handle the case of accessibility active while a ringtone is playing: if the ringtone is much |
| 5349 | // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch |
| 5350 | // exploration of the dialer UI. In this situation, bring the accessibility volume closer to |
| 5351 | // the ringtone volume |
| 5352 | if ((stream == AUDIO_STREAM_ACCESSIBILITY) |
| 5353 | && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState()) |
| 5354 | && isStreamActive(AUDIO_STREAM_RING, 0)) { |
| 5355 | const float ringVolumeDB = computeVolume(AUDIO_STREAM_RING, index, device); |
| 5356 | return ringVolumeDB - 4 > volumeDB ? ringVolumeDB - 4 : volumeDB; |
| 5357 | } |
| 5358 | |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 5359 | // in-call: always cap earpiece volume by voice volume + some low headroom |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5360 | if ((stream != AUDIO_STREAM_VOICE_CALL) && (device & AUDIO_DEVICE_OUT_EARPIECE) && |
| 5361 | (isInCall() || mOutputs.isStreamActiveLocally(AUDIO_STREAM_VOICE_CALL))) { |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 5362 | switch (stream) { |
| 5363 | case AUDIO_STREAM_SYSTEM: |
| 5364 | case AUDIO_STREAM_RING: |
| 5365 | case AUDIO_STREAM_MUSIC: |
| 5366 | case AUDIO_STREAM_ALARM: |
| 5367 | case AUDIO_STREAM_NOTIFICATION: |
| 5368 | case AUDIO_STREAM_ENFORCED_AUDIBLE: |
| 5369 | case AUDIO_STREAM_DTMF: |
| 5370 | case AUDIO_STREAM_ACCESSIBILITY: { |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5371 | int voiceVolumeIndex = |
| 5372 | mVolumeCurves->getVolumeIndex(AUDIO_STREAM_VOICE_CALL, AUDIO_DEVICE_OUT_EARPIECE); |
| 5373 | const float maxVoiceVolDb = |
| 5374 | computeVolume(AUDIO_STREAM_VOICE_CALL, voiceVolumeIndex, AUDIO_DEVICE_OUT_EARPIECE) |
| 5375 | + IN_CALL_EARPIECE_HEADROOM_DB; |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 5376 | if (volumeDB > maxVoiceVolDb) { |
| 5377 | ALOGV("computeVolume() stream %d at vol=%f overriden by stream %d at vol=%f", |
| 5378 | stream, volumeDB, AUDIO_STREAM_VOICE_CALL, maxVoiceVolDb); |
| 5379 | volumeDB = maxVoiceVolDb; |
| 5380 | } |
| 5381 | } break; |
| 5382 | default: |
| 5383 | break; |
| 5384 | } |
| 5385 | } |
| 5386 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5387 | // if a headset is connected, apply the following rules to ring tones and notifications |
| 5388 | // to avoid sound level bursts in user's ears: |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5389 | // - always attenuate notifications volume by 6dB |
| 5390 | // - attenuate ring tones volume by 6dB unless music is not playing and |
| 5391 | // speaker is part of the select devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5392 | // - if music is playing, always limit the volume to current music volume, |
| 5393 | // with a minimum threshold at -36dB so that notification is always perceived. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5394 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5395 | if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 5396 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | |
| 5397 | AUDIO_DEVICE_OUT_WIRED_HEADSET | |
Eric Laurent | 904d632 | 2017-03-17 17:20:47 -0700 | [diff] [blame] | 5398 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE | |
Jakub Pawlowski | 00f928c | 2018-03-22 13:20:03 -0700 | [diff] [blame] | 5399 | AUDIO_DEVICE_OUT_USB_HEADSET | |
| 5400 | AUDIO_DEVICE_OUT_HEARING_AID)) && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5401 | ((stream_strategy == STRATEGY_SONIFICATION) |
| 5402 | || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL) |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5403 | || (stream == AUDIO_STREAM_SYSTEM) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5404 | || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5405 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) && |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5406 | mVolumeCurves->canBeMuted(stream)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5407 | // when the phone is ringing we must consider that music could have been paused just before |
| 5408 | // by the music application and behave as if music was active if the last music track was |
| 5409 | // just stopped |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5410 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5411 | mLimitRingtoneVolume) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5412 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5413 | audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5414 | float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5415 | mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC, |
| 5416 | musicDevice), |
| 5417 | musicDevice); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5418 | float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ? |
| 5419 | musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB; |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5420 | if (volumeDB > minVolDB) { |
| 5421 | volumeDB = minVolDB; |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5422 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5423 | } |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5424 | if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 5425 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) { |
| 5426 | // on A2DP, also ensure notification volume is not too low compared to media when |
| 5427 | // intended to be played |
| 5428 | if ((volumeDB > -96.0f) && |
| 5429 | (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) { |
| 5430 | ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f", |
| 5431 | stream, device, |
| 5432 | volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB); |
| 5433 | volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB; |
| 5434 | } |
| 5435 | } |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5436 | } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) || |
| 5437 | stream_strategy != STRATEGY_SONIFICATION) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5438 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5439 | } |
| 5440 | } |
| 5441 | |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5442 | return volumeDB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5443 | } |
| 5444 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5445 | status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5446 | int index, |
| 5447 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5448 | audio_devices_t device, |
| 5449 | int delayMs, |
| 5450 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5451 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5452 | // do not change actual stream volume if the stream is muted |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5453 | if (outputDesc->mMuteCount[stream] != 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5454 | ALOGVV("checkAndSetVolume() stream %d muted count %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5455 | stream, outputDesc->mMuteCount[stream]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5456 | return NO_ERROR; |
| 5457 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5458 | audio_policy_forced_cfg_t forceUseForComm = |
| 5459 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5460 | // 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] | 5461 | if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) || |
| 5462 | (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5463 | 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] | 5464 | stream, forceUseForComm); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5465 | return INVALID_OPERATION; |
| 5466 | } |
| 5467 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5468 | if (device == AUDIO_DEVICE_NONE) { |
| 5469 | device = outputDesc->device(); |
| 5470 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5471 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5472 | float volumeDb = computeVolume(stream, index, device); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5473 | if (outputDesc->isFixedVolume(device)) { |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5474 | volumeDb = 0.0f; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5475 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5476 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5477 | outputDesc->setVolume(volumeDb, stream, device, delayMs, force); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5478 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5479 | if (stream == AUDIO_STREAM_VOICE_CALL || |
| 5480 | stream == AUDIO_STREAM_BLUETOOTH_SCO) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5481 | float voiceVolume; |
| 5482 | // 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] | 5483 | if (stream == AUDIO_STREAM_VOICE_CALL) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5484 | voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5485 | } else { |
| 5486 | voiceVolume = 1.0; |
| 5487 | } |
| 5488 | |
Eric Laurent | 18fba84 | 2016-03-31 14:41:26 -0700 | [diff] [blame] | 5489 | if (voiceVolume != mLastVoiceVolume) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5490 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); |
| 5491 | mLastVoiceVolume = voiceVolume; |
| 5492 | } |
| 5493 | } |
| 5494 | |
| 5495 | return NO_ERROR; |
| 5496 | } |
| 5497 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5498 | void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc, |
| 5499 | audio_devices_t device, |
| 5500 | int delayMs, |
| 5501 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5502 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5503 | ALOGVV("applyStreamVolumes() for device %08x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5504 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5505 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5506 | checkAndSetVolume((audio_stream_type_t)stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5507 | mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5508 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5509 | device, |
| 5510 | delayMs, |
| 5511 | force); |
| 5512 | } |
| 5513 | } |
| 5514 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5515 | void AudioPolicyManager::setStrategyMute(routing_strategy strategy, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5516 | bool on, |
| 5517 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5518 | int delayMs, |
| 5519 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5520 | { |
Eric Laurent | 72249d5 | 2015-06-08 11:12:15 -0700 | [diff] [blame] | 5521 | ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d", |
| 5522 | strategy, on, outputDesc->getId()); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5523 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5524 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5525 | setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5526 | } |
| 5527 | } |
| 5528 | } |
| 5529 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5530 | void AudioPolicyManager::setStreamMute(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5531 | bool on, |
| 5532 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5533 | int delayMs, |
| 5534 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5535 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5536 | if (device == AUDIO_DEVICE_NONE) { |
| 5537 | device = outputDesc->device(); |
| 5538 | } |
| 5539 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5540 | ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x", |
| 5541 | stream, on, outputDesc->mMuteCount[stream], device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5542 | |
| 5543 | if (on) { |
| 5544 | if (outputDesc->mMuteCount[stream] == 0) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5545 | if (mVolumeCurves->canBeMuted(stream) && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5546 | ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5547 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5548 | checkAndSetVolume(stream, 0, outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5549 | } |
| 5550 | } |
| 5551 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored |
| 5552 | outputDesc->mMuteCount[stream]++; |
| 5553 | } else { |
| 5554 | if (outputDesc->mMuteCount[stream] == 0) { |
| 5555 | ALOGV("setStreamMute() unmuting non muted stream!"); |
| 5556 | return; |
| 5557 | } |
| 5558 | if (--outputDesc->mMuteCount[stream] == 0) { |
| 5559 | checkAndSetVolume(stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5560 | mVolumeCurves->getVolumeIndex(stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5561 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5562 | device, |
| 5563 | delayMs); |
| 5564 | } |
| 5565 | } |
| 5566 | } |
| 5567 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5568 | void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5569 | bool starting, bool stateChange) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5570 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 5571 | if(!hasPrimaryOutput()) { |
| 5572 | return; |
| 5573 | } |
| 5574 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5575 | // if the stream pertains to sonification strategy and we are in call we must |
| 5576 | // mute the stream if it is low visibility. If it is high visibility, we must play a tone |
| 5577 | // in the device used for phone strategy and play the tone if the selected device does not |
| 5578 | // interfere with the device used for phone strategy |
| 5579 | // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as |
| 5580 | // many times as there are active tracks on the output |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5581 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5582 | if ((stream_strategy == STRATEGY_SONIFICATION) || |
| 5583 | ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5584 | sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5585 | ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d", |
| 5586 | stream, starting, outputDesc->mDevice, stateChange); |
| 5587 | if (outputDesc->mRefCount[stream]) { |
| 5588 | int muteCount = 1; |
| 5589 | if (stateChange) { |
| 5590 | muteCount = outputDesc->mRefCount[stream]; |
| 5591 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5592 | if (audio_is_low_visibility(stream)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5593 | ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount); |
| 5594 | for (int i = 0; i < muteCount; i++) { |
| 5595 | setStreamMute(stream, starting, mPrimaryOutput); |
| 5596 | } |
| 5597 | } else { |
| 5598 | ALOGV("handleIncallSonification() high visibility"); |
| 5599 | if (outputDesc->device() & |
| 5600 | getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) { |
| 5601 | ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount); |
| 5602 | for (int i = 0; i < muteCount; i++) { |
| 5603 | setStreamMute(stream, starting, mPrimaryOutput); |
| 5604 | } |
| 5605 | } |
| 5606 | if (starting) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5607 | mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION, |
| 5608 | AUDIO_STREAM_VOICE_CALL); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5609 | } else { |
| 5610 | mpClientInterface->stopTone(); |
| 5611 | } |
| 5612 | } |
| 5613 | } |
| 5614 | } |
| 5615 | } |
| 5616 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5617 | audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr) |
| 5618 | { |
| 5619 | // flags to stream type mapping |
| 5620 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 5621 | return AUDIO_STREAM_ENFORCED_AUDIBLE; |
| 5622 | } |
| 5623 | if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) { |
| 5624 | return AUDIO_STREAM_BLUETOOTH_SCO; |
| 5625 | } |
Jean-Michel Trivi | 79ad438 | 2015-01-29 10:49:39 -0800 | [diff] [blame] | 5626 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 5627 | return AUDIO_STREAM_TTS; |
| 5628 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5629 | |
| 5630 | // usage to stream type mapping |
| 5631 | switch (attr->usage) { |
| 5632 | case AUDIO_USAGE_MEDIA: |
| 5633 | case AUDIO_USAGE_GAME: |
Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 5634 | case AUDIO_USAGE_ASSISTANT: |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5635 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 5636 | return AUDIO_STREAM_MUSIC; |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5637 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 5638 | return AUDIO_STREAM_ACCESSIBILITY; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5639 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 5640 | return AUDIO_STREAM_SYSTEM; |
| 5641 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 5642 | return AUDIO_STREAM_VOICE_CALL; |
| 5643 | |
| 5644 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 5645 | return AUDIO_STREAM_DTMF; |
| 5646 | |
| 5647 | case AUDIO_USAGE_ALARM: |
| 5648 | return AUDIO_STREAM_ALARM; |
| 5649 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 5650 | return AUDIO_STREAM_RING; |
| 5651 | |
| 5652 | case AUDIO_USAGE_NOTIFICATION: |
| 5653 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 5654 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 5655 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 5656 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 5657 | return AUDIO_STREAM_NOTIFICATION; |
| 5658 | |
| 5659 | case AUDIO_USAGE_UNKNOWN: |
| 5660 | default: |
| 5661 | return AUDIO_STREAM_MUSIC; |
| 5662 | } |
| 5663 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5664 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5665 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) |
| 5666 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5667 | // has flags that map to a strategy? |
| 5668 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { |
| 5669 | return true; |
| 5670 | } |
| 5671 | |
| 5672 | // has known usage? |
| 5673 | switch (paa->usage) { |
| 5674 | case AUDIO_USAGE_UNKNOWN: |
| 5675 | case AUDIO_USAGE_MEDIA: |
| 5676 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 5677 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 5678 | case AUDIO_USAGE_ALARM: |
| 5679 | case AUDIO_USAGE_NOTIFICATION: |
| 5680 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 5681 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 5682 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 5683 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 5684 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 5685 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 5686 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 5687 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 5688 | case AUDIO_USAGE_GAME: |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5689 | case AUDIO_USAGE_VIRTUAL_SOURCE: |
Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 5690 | case AUDIO_USAGE_ASSISTANT: |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5691 | break; |
| 5692 | default: |
| 5693 | return false; |
| 5694 | } |
| 5695 | return true; |
| 5696 | } |
| 5697 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5698 | bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor>& outputDesc, |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5699 | routing_strategy strategy, uint32_t inPastMs, |
| 5700 | nsecs_t sysTime) const |
| 5701 | { |
| 5702 | if ((sysTime == 0) && (inPastMs != 0)) { |
| 5703 | sysTime = systemTime(); |
| 5704 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5705 | 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] | 5706 | if (((getStrategy((audio_stream_type_t)i) == strategy) || |
| 5707 | (NUM_STRATEGIES == strategy)) && |
| 5708 | outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) { |
| 5709 | return true; |
| 5710 | } |
| 5711 | } |
| 5712 | return false; |
| 5713 | } |
| 5714 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5715 | audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage) |
| 5716 | { |
| 5717 | return mEngine->getForceUse(usage); |
| 5718 | } |
| 5719 | |
| 5720 | bool AudioPolicyManager::isInCall() |
| 5721 | { |
| 5722 | return isStateInCall(mEngine->getPhoneState()); |
| 5723 | } |
| 5724 | |
| 5725 | bool AudioPolicyManager::isStateInCall(int state) |
| 5726 | { |
| 5727 | return is_state_in_call(state); |
| 5728 | } |
| 5729 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5730 | void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc) |
| 5731 | { |
| 5732 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
| 5733 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 5734 | if (sourceDesc->mDevice->equals(deviceDesc)) { |
| 5735 | ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle()); |
| 5736 | stopAudioSource(sourceDesc->getHandle()); |
| 5737 | } |
| 5738 | } |
| 5739 | |
| 5740 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
| 5741 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 5742 | bool release = false; |
| 5743 | for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) { |
| 5744 | const struct audio_port_config *source = &patchDesc->mPatch.sources[j]; |
| 5745 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 5746 | source->ext.device.type == deviceDesc->type()) { |
| 5747 | release = true; |
| 5748 | } |
| 5749 | } |
| 5750 | for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) { |
| 5751 | const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j]; |
| 5752 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && |
| 5753 | sink->ext.device.type == deviceDesc->type()) { |
| 5754 | release = true; |
| 5755 | } |
| 5756 | } |
| 5757 | if (release) { |
| 5758 | ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle); |
| 5759 | releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid); |
| 5760 | } |
| 5761 | } |
| 5762 | } |
| 5763 | |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5764 | // Modify the list of surround sound formats supported. |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5765 | void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) { |
| 5766 | FormatVector &formats = *formatsPtr; |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5767 | // TODO Set this based on Config properties. |
| 5768 | const bool alwaysForceAC3 = true; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5769 | |
| 5770 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5771 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5772 | ALOGD("%s: forced use = %d", __FUNCTION__, forceUse); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5773 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5774 | // If MANUAL, keep the supported surround sound formats as current enabled ones. |
| 5775 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
| 5776 | formats.clear(); |
| 5777 | for (auto it = mSurroundFormats.begin(); it != mSurroundFormats.end(); it++) { |
| 5778 | formats.add(*it); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5779 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5780 | // Always enable IEC61937 when in MANUAL mode. |
| 5781 | formats.add(AUDIO_FORMAT_IEC61937); |
| 5782 | } else { // NEVER, AUTO or ALWAYS |
| 5783 | // Analyze original support for various formats. |
| 5784 | bool supportsAC3 = false; |
| 5785 | bool supportsOtherSurround = false; |
| 5786 | bool supportsIEC61937 = false; |
| 5787 | mSurroundFormats.clear(); |
| 5788 | for (ssize_t formatIndex = 0; formatIndex < (ssize_t)formats.size(); formatIndex++) { |
| 5789 | audio_format_t format = formats[formatIndex]; |
| 5790 | switch (format) { |
| 5791 | case AUDIO_FORMAT_AC3: |
| 5792 | supportsAC3 = true; |
| 5793 | break; |
| 5794 | case AUDIO_FORMAT_E_AC3: |
| 5795 | case AUDIO_FORMAT_DTS: |
| 5796 | case AUDIO_FORMAT_DTS_HD: |
| 5797 | // If ALWAYS, remove all other surround formats here |
| 5798 | // since we will add them later. |
| 5799 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { |
| 5800 | formats.removeAt(formatIndex); |
| 5801 | formatIndex--; |
| 5802 | } |
| 5803 | supportsOtherSurround = true; |
| 5804 | break; |
| 5805 | case AUDIO_FORMAT_IEC61937: |
| 5806 | supportsIEC61937 = true; |
| 5807 | break; |
| 5808 | default: |
| 5809 | break; |
| 5810 | } |
| 5811 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5812 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5813 | // Modify formats based on surround preferences. |
| 5814 | // If NEVER, remove support for surround formats. |
| 5815 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
| 5816 | if (supportsAC3 || supportsOtherSurround || supportsIEC61937) { |
| 5817 | // Remove surround sound related formats. |
| 5818 | for (size_t formatIndex = 0; formatIndex < formats.size(); ) { |
| 5819 | audio_format_t format = formats[formatIndex]; |
| 5820 | switch(format) { |
| 5821 | case AUDIO_FORMAT_AC3: |
| 5822 | case AUDIO_FORMAT_E_AC3: |
| 5823 | case AUDIO_FORMAT_DTS: |
| 5824 | case AUDIO_FORMAT_DTS_HD: |
| 5825 | case AUDIO_FORMAT_IEC61937: |
| 5826 | formats.removeAt(formatIndex); |
| 5827 | break; |
| 5828 | default: |
| 5829 | formatIndex++; // keep it |
| 5830 | break; |
| 5831 | } |
| 5832 | } |
| 5833 | supportsAC3 = false; |
| 5834 | supportsOtherSurround = false; |
| 5835 | supportsIEC61937 = false; |
| 5836 | } |
| 5837 | } else { // AUTO or ALWAYS |
| 5838 | // Most TVs support AC3 even if they do not report it in the EDID. |
| 5839 | if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)) |
| 5840 | && !supportsAC3) { |
| 5841 | formats.add(AUDIO_FORMAT_AC3); |
| 5842 | supportsAC3 = true; |
| 5843 | } |
| 5844 | |
| 5845 | // If ALWAYS, add support for raw surround formats if all are missing. |
| 5846 | // This assumes that if any of these formats are reported by the HAL |
| 5847 | // then the report is valid and should not be modified. |
| 5848 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { |
| 5849 | formats.add(AUDIO_FORMAT_E_AC3); |
| 5850 | formats.add(AUDIO_FORMAT_DTS); |
| 5851 | formats.add(AUDIO_FORMAT_DTS_HD); |
| 5852 | supportsOtherSurround = true; |
| 5853 | } |
| 5854 | |
| 5855 | // Add support for IEC61937 if any raw surround supported. |
| 5856 | // The HAL could do this but add it here, just in case. |
| 5857 | if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) { |
| 5858 | formats.add(AUDIO_FORMAT_IEC61937); |
| 5859 | supportsIEC61937 = true; |
| 5860 | } |
| 5861 | |
| 5862 | // Add reported surround sound formats to enabled surround formats. |
| 5863 | for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) { |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5864 | audio_format_t format = formats[formatIndex]; |
| 5865 | switch(format) { |
| 5866 | case AUDIO_FORMAT_AC3: |
| 5867 | case AUDIO_FORMAT_E_AC3: |
| 5868 | case AUDIO_FORMAT_DTS: |
| 5869 | case AUDIO_FORMAT_DTS_HD: |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5870 | case AUDIO_FORMAT_AAC_LC: |
| 5871 | case AUDIO_FORMAT_DOLBY_TRUEHD: |
| 5872 | case AUDIO_FORMAT_E_AC3_JOC: |
| 5873 | mSurroundFormats.insert(format); |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5874 | default: |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5875 | break; |
| 5876 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5877 | } |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5878 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5879 | } |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5880 | } |
| 5881 | |
| 5882 | // Modify the list of channel masks supported. |
| 5883 | void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) { |
| 5884 | ChannelsVector &channelMasks = *channelMasksPtr; |
| 5885 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5886 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
| 5887 | |
| 5888 | // If NEVER, then remove support for channelMasks > stereo. |
| 5889 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
| 5890 | for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) { |
| 5891 | audio_channel_mask_t channelMask = channelMasks[maskIndex]; |
| 5892 | if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) { |
| 5893 | ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask); |
| 5894 | channelMasks.removeAt(maskIndex); |
| 5895 | } else { |
| 5896 | maskIndex++; |
| 5897 | } |
| 5898 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5899 | // If ALWAYS or MANUAL, then make sure we at least support 5.1 |
| 5900 | } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS |
| 5901 | || forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5902 | bool supports5dot1 = false; |
| 5903 | // Are there any channel masks that can be considered "surround"? |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5904 | for (audio_channel_mask_t channelMask : channelMasks) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5905 | if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) { |
| 5906 | supports5dot1 = true; |
| 5907 | break; |
| 5908 | } |
| 5909 | } |
| 5910 | // If not then add 5.1 support. |
| 5911 | if (!supports5dot1) { |
| 5912 | channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1); |
| 5913 | ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__); |
| 5914 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5915 | } |
| 5916 | } |
| 5917 | |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5918 | void AudioPolicyManager::updateAudioProfiles(audio_devices_t device, |
| 5919 | audio_io_handle_t ioHandle, |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5920 | AudioProfileVector &profiles) |
| 5921 | { |
| 5922 | String8 reply; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5923 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5924 | // Format MUST be checked first to update the list of AudioProfile |
| 5925 | if (profiles.hasDynamicFormat()) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5926 | reply = mpClientInterface->getParameters( |
| 5927 | ioHandle, String8(AudioParameter::keyStreamSupportedFormats)); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5928 | ALOGV("%s: supported formats %d, %s", __FUNCTION__, ioHandle, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5929 | AudioParameter repliedParameters(reply); |
| 5930 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5931 | String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) { |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5932 | ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__); |
| 5933 | return; |
| 5934 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5935 | FormatVector formats = formatsFromString(reply.string()); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5936 | if (device == AUDIO_DEVICE_OUT_HDMI) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5937 | filterSurroundFormats(&formats); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5938 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5939 | profiles.setFormats(formats); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5940 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5941 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5942 | for (audio_format_t format : profiles.getSupportedFormats()) { |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5943 | ChannelsVector channelMasks; |
| 5944 | SampleRateVector samplingRates; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5945 | AudioParameter requestedParameters; |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5946 | requestedParameters.addInt(String8(AudioParameter::keyFormat), format); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5947 | |
| 5948 | if (profiles.hasDynamicRateFor(format)) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5949 | reply = mpClientInterface->getParameters( |
| 5950 | ioHandle, |
| 5951 | requestedParameters.toString() + ";" + |
| 5952 | AudioParameter::keyStreamSupportedSamplingRates); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5953 | ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5954 | AudioParameter repliedParameters(reply); |
| 5955 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5956 | String8(AudioParameter::keyStreamSupportedSamplingRates), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5957 | samplingRates = samplingRatesFromString(reply.string()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5958 | } |
| 5959 | } |
| 5960 | if (profiles.hasDynamicChannelsFor(format)) { |
| 5961 | reply = mpClientInterface->getParameters(ioHandle, |
| 5962 | requestedParameters.toString() + ";" + |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5963 | AudioParameter::keyStreamSupportedChannels); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5964 | ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5965 | AudioParameter repliedParameters(reply); |
| 5966 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5967 | String8(AudioParameter::keyStreamSupportedChannels), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5968 | channelMasks = channelMasksFromString(reply.string()); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5969 | if (device == AUDIO_DEVICE_OUT_HDMI) { |
| 5970 | filterSurroundChannelMasks(&channelMasks); |
| 5971 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5972 | } |
| 5973 | } |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5974 | profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates)); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5975 | } |
| 5976 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5977 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5978 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 5979 | audio_patch_handle_t *patchHandle, |
| 5980 | AudioIODescriptorInterface *ioDescriptor, |
| 5981 | const struct audio_patch *patch, |
| 5982 | int delayMs) |
| 5983 | { |
| 5984 | ssize_t index = mAudioPatches.indexOfKey( |
| 5985 | patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE ? |
| 5986 | *patchHandle : ioDescriptor->getPatchHandle()); |
| 5987 | sp<AudioPatch> patchDesc; |
| 5988 | status_t status = installPatch( |
| 5989 | caller, index, patchHandle, patch, delayMs, mUidCached, &patchDesc); |
| 5990 | if (status == NO_ERROR) { |
| 5991 | ioDescriptor->setPatchHandle(patchDesc->mHandle); |
| 5992 | } |
| 5993 | return status; |
| 5994 | } |
| 5995 | |
| 5996 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 5997 | ssize_t index, |
| 5998 | audio_patch_handle_t *patchHandle, |
| 5999 | const struct audio_patch *patch, |
| 6000 | int delayMs, |
| 6001 | uid_t uid, |
| 6002 | sp<AudioPatch> *patchDescPtr) |
| 6003 | { |
| 6004 | sp<AudioPatch> patchDesc; |
| 6005 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 6006 | if (index >= 0) { |
| 6007 | patchDesc = mAudioPatches.valueAt(index); |
| 6008 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 6009 | } |
| 6010 | |
| 6011 | status_t status = mpClientInterface->createAudioPatch(patch, &afPatchHandle, delayMs); |
| 6012 | ALOGV("%s() AF::createAudioPatch returned %d patchHandle %d num_sources %d num_sinks %d", |
| 6013 | caller, status, afPatchHandle, patch->num_sources, patch->num_sinks); |
| 6014 | if (status == NO_ERROR) { |
| 6015 | if (index < 0) { |
| 6016 | patchDesc = new AudioPatch(patch, uid); |
| 6017 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 6018 | } else { |
| 6019 | patchDesc->mPatch = *patch; |
| 6020 | } |
| 6021 | patchDesc->mAfPatchHandle = afPatchHandle; |
| 6022 | if (patchHandle) { |
| 6023 | *patchHandle = patchDesc->mHandle; |
| 6024 | } |
| 6025 | nextAudioPortGeneration(); |
| 6026 | mpClientInterface->onAudioPatchListUpdate(); |
| 6027 | } |
| 6028 | if (patchDescPtr) *patchDescPtr = patchDesc; |
| 6029 | return status; |
| 6030 | } |
| 6031 | |
Mikhail Naganov | 1b2a794 | 2017-12-08 10:18:09 -0800 | [diff] [blame] | 6032 | } // namespace android |