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" |
Tomoharu Kasahara | 71c9091 | 2018-10-31 09:10:12 +0900 | [diff] [blame] | 18 | |
| 19 | // Need to keep the log statements even in production builds |
| 20 | // to enable VERBOSE logging dynamically. |
| 21 | // You can enable VERBOSE logging as follows: |
| 22 | // adb shell setprop log.tag.APM_AudioPolicyManager V |
| 23 | #define LOG_NDEBUG 0 |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 24 | |
| 25 | //#define VERY_VERBOSE_LOGGING |
| 26 | #ifdef VERY_VERBOSE_LOGGING |
| 27 | #define ALOGVV ALOGV |
| 28 | #else |
| 29 | #define ALOGVV(a...) do { } while(0) |
| 30 | #endif |
| 31 | |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 32 | #define AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH 128 |
| 33 | #define AUDIO_POLICY_XML_CONFIG_FILE_NAME "audio_policy_configuration.xml" |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 34 | #define AUDIO_POLICY_A2DP_OFFLOAD_DISABLED_XML_CONFIG_FILE_NAME \ |
| 35 | "audio_policy_configuration_a2dp_offload_disabled.xml" |
Cheney Ni | 6851adb | 2018-11-01 06:30:37 +0800 | [diff] [blame] | 36 | #define AUDIO_POLICY_BLUETOOTH_HAL_ENABLED_XML_CONFIG_FILE_NAME \ |
| 37 | "audio_policy_configuration_bluetooth_hal_enabled.xml" |
François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 38 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 39 | #include <inttypes.h> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 40 | #include <math.h> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 41 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 42 | #include <AudioPolicyManagerInterface.h> |
| 43 | #include <AudioPolicyEngineInstance.h> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 44 | #include <cutils/properties.h> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 45 | #include <utils/Log.h> |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 46 | #include <media/AudioParameter.h> |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 47 | #include <media/AudioPolicyHelper.h> |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 48 | #include <soundtrigger/SoundTrigger.h> |
Mikhail Naganov | cbc8f61 | 2016-10-11 18:05:13 -0700 | [diff] [blame] | 49 | #include <system/audio.h> |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 50 | #include <audio_policy_conf.h> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 51 | #include "AudioPolicyManager.h" |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 52 | #ifndef USE_XML_AUDIO_POLICY_CONF |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 53 | #include <ConfigParsingUtils.h> |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 54 | #include <StreamDescriptor.h> |
François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 55 | #endif |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 56 | #include <Serializer.h> |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 57 | #include "TypeConverter.h" |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 58 | #include <policy.h> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 59 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 60 | namespace android { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 61 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 62 | //FIXME: workaround for truncated touch sounds |
| 63 | // to be removed when the problem is handled by system UI |
| 64 | #define TOUCH_SOUND_FIXED_DELAY_MS 100 |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 65 | |
| 66 | // Largest difference in dB on earpiece in call between the voice volume and another |
| 67 | // media / notification / system volume. |
| 68 | constexpr float IN_CALL_EARPIECE_HEADROOM_DB = 3.f; |
| 69 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 70 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) |
| 71 | // Array of all surround formats. |
| 72 | static const audio_format_t SURROUND_FORMATS[] = { |
| 73 | AUDIO_FORMAT_AC3, |
| 74 | AUDIO_FORMAT_E_AC3, |
| 75 | AUDIO_FORMAT_DTS, |
| 76 | AUDIO_FORMAT_DTS_HD, |
| 77 | AUDIO_FORMAT_AAC_LC, |
| 78 | AUDIO_FORMAT_DOLBY_TRUEHD, |
| 79 | AUDIO_FORMAT_E_AC3_JOC, |
| 80 | }; |
| 81 | // Array of all AAC formats. When AAC is enabled by users, all AAC formats should be enabled. |
| 82 | static const audio_format_t AAC_FORMATS[] = { |
| 83 | AUDIO_FORMAT_AAC_LC, |
| 84 | AUDIO_FORMAT_AAC_HE_V1, |
| 85 | AUDIO_FORMAT_AAC_HE_V2, |
| 86 | AUDIO_FORMAT_AAC_ELD, |
| 87 | AUDIO_FORMAT_AAC_XHE, |
| 88 | }; |
| 89 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 90 | // ---------------------------------------------------------------------------- |
| 91 | // AudioPolicyInterface implementation |
| 92 | // ---------------------------------------------------------------------------- |
| 93 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 94 | status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 95 | audio_policy_dev_state_t state, |
| 96 | const char *device_address, |
| 97 | const char *device_name) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 98 | { |
jiabin | a7b4379 | 2018-02-15 16:04:46 -0800 | [diff] [blame] | 99 | status_t status = setDeviceConnectionStateInt(device, state, device_address, device_name); |
| 100 | nextAudioPortGeneration(); |
| 101 | return status; |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 102 | } |
| 103 | |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 104 | void AudioPolicyManager::broadcastDeviceConnectionState(audio_devices_t device, |
| 105 | audio_policy_dev_state_t state, |
| 106 | const String8 &device_address) |
| 107 | { |
| 108 | AudioParameter param(device_address); |
| 109 | const String8 key(state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE ? |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 110 | AudioParameter::keyStreamConnect : AudioParameter::keyStreamDisconnect); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 111 | param.addInt(key, device); |
| 112 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 113 | } |
| 114 | |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 115 | status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device, |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 116 | audio_policy_dev_state_t state, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 117 | const char *device_address, |
| 118 | const char *device_name) |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 119 | { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 120 | ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s", |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 121 | device, state, device_address, device_name); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 122 | |
| 123 | // connect/disconnect only 1 device at a time |
| 124 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; |
| 125 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 126 | sp<DeviceDescriptor> devDesc = |
| 127 | mHwModules.getDeviceDescriptor(device, device_address, device_name); |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 128 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 129 | // handle output devices |
| 130 | if (audio_is_output_device(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 131 | SortedVector <audio_io_handle_t> outputs; |
| 132 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 133 | ssize_t index = mAvailableOutputDevices.indexOf(devDesc); |
| 134 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 135 | // save a copy of the opened output descriptors before any output is opened or closed |
| 136 | // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies() |
| 137 | mPreviousOutputs = mOutputs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 138 | switch (state) |
| 139 | { |
| 140 | // handle output device connection |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 141 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 142 | if (index >= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 143 | ALOGW("setDeviceConnectionState() device already connected: %x", device); |
| 144 | return INVALID_OPERATION; |
| 145 | } |
| 146 | ALOGV("setDeviceConnectionState() connecting device %x", device); |
| 147 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 148 | // register new device as available |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 149 | index = mAvailableOutputDevices.add(devDesc); |
| 150 | if (index >= 0) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 151 | sp<HwModule> module = mHwModules.getModuleForDevice(device); |
Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 152 | if (module == 0) { |
| 153 | ALOGD("setDeviceConnectionState() could not find HW module for device %08x", |
| 154 | device); |
| 155 | mAvailableOutputDevices.remove(devDesc); |
| 156 | return INVALID_OPERATION; |
| 157 | } |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 158 | mAvailableOutputDevices[index]->attach(module); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 159 | } else { |
| 160 | return NO_MEMORY; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 161 | } |
| 162 | |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 163 | // Before checking outputs, broadcast connect event to allow HAL to retrieve dynamic |
| 164 | // parameters on newly connected devices (instead of opening the outputs...) |
| 165 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); |
| 166 | |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 167 | if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 168 | mAvailableOutputDevices.remove(devDesc); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 169 | |
| 170 | broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 171 | devDesc->mAddress); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 172 | return INVALID_OPERATION; |
| 173 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 174 | // Propagate device availability to Engine |
| 175 | mEngine->setDeviceConnectionState(devDesc, state); |
| 176 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 177 | // outputs should never be empty here |
| 178 | ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():" |
| 179 | "checkOutputsForDevice() returned no outputs but status OK"); |
| 180 | ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs", |
| 181 | outputs.size()); |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 182 | |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 183 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 184 | // handle output device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 185 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 186 | if (index < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 187 | ALOGW("setDeviceConnectionState() device not connected: %x", device); |
| 188 | return INVALID_OPERATION; |
| 189 | } |
| 190 | |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 191 | ALOGV("setDeviceConnectionState() disconnecting output device %x", device); |
| 192 | |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 193 | // Send Disconnect to HALs |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 194 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 195 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 196 | // remove device from available output devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 197 | mAvailableOutputDevices.remove(devDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 198 | |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 199 | checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 200 | |
| 201 | // Propagate device availability to Engine |
| 202 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 203 | } break; |
| 204 | |
| 205 | default: |
| 206 | ALOGE("setDeviceConnectionState() invalid state: %x", state); |
| 207 | return BAD_VALUE; |
| 208 | } |
| 209 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 210 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP |
| 211 | // output is suspended before any tracks are moved to it |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 212 | checkA2dpSuspend(); |
| 213 | checkOutputForAllStrategies(); |
| 214 | // outputs must be closed after checkOutputForAllStrategies() is executed |
| 215 | if (!outputs.isEmpty()) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 216 | for (audio_io_handle_t output : outputs) { |
| 217 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 218 | // close unused outputs after device disconnection or direct outputs that have been |
| 219 | // opened by checkOutputsForDevice() to query dynamic parameters |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 220 | if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 221 | (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) && |
| 222 | (desc->mDirectOpenCount == 0))) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 223 | closeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 224 | } |
| 225 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 226 | // check again after closing A2DP output to reset mA2dpSuspended if needed |
| 227 | checkA2dpSuspend(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | updateDevicesAndOutputs(); |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 231 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 232 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 233 | updateCallRouting(newDevice); |
| 234 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 235 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 236 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 237 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) { |
| 238 | audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 239 | // do not force device change on duplicated output because if device is 0, it will |
| 240 | // also force a device 0 for the two outputs it is duplicated to which may override |
| 241 | // a valid device selection on those outputs. |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 242 | bool force = !desc->isDuplicated() |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 243 | && (!device_distinguishes_on_address(device) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 244 | // always force when disconnecting (a non-duplicated device) |
| 245 | || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 246 | setOutputDevice(desc, newDevice, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 247 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 250 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
| 251 | cleanUpForDevice(devDesc); |
| 252 | } |
| 253 | |
Eric Laurent | 72aa32f | 2014-05-30 18:51:48 -0700 | [diff] [blame] | 254 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | b71e58b | 2014-05-29 16:08:11 -0700 | [diff] [blame] | 255 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 256 | } // end if is output device |
| 257 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 258 | // handle input devices |
| 259 | if (audio_is_input_device(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 260 | SortedVector <audio_io_handle_t> inputs; |
| 261 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 262 | ssize_t index = mAvailableInputDevices.indexOf(devDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 263 | switch (state) |
| 264 | { |
| 265 | // handle input device connection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 266 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 267 | if (index >= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 268 | ALOGW("setDeviceConnectionState() device already connected: %d", device); |
| 269 | return INVALID_OPERATION; |
| 270 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 271 | sp<HwModule> module = mHwModules.getModuleForDevice(device); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 272 | if (module == NULL) { |
| 273 | ALOGW("setDeviceConnectionState(): could not find HW module for device %08x", |
| 274 | device); |
| 275 | return INVALID_OPERATION; |
| 276 | } |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 277 | |
| 278 | // Before checking intputs, broadcast connect event to allow HAL to retrieve dynamic |
| 279 | // parameters on newly connected devices (instead of opening the inputs...) |
| 280 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); |
| 281 | |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 282 | if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) { |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 283 | broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 284 | devDesc->mAddress); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 285 | return INVALID_OPERATION; |
| 286 | } |
| 287 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 288 | index = mAvailableInputDevices.add(devDesc); |
| 289 | if (index >= 0) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 290 | mAvailableInputDevices[index]->attach(module); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 291 | } else { |
| 292 | return NO_MEMORY; |
| 293 | } |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 294 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 295 | // Propagate device availability to Engine |
| 296 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 297 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 298 | |
| 299 | // handle input device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 300 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 301 | if (index < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 302 | ALOGW("setDeviceConnectionState() device not connected: %d", device); |
| 303 | return INVALID_OPERATION; |
| 304 | } |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 305 | |
| 306 | ALOGV("setDeviceConnectionState() disconnecting input device %x", device); |
| 307 | |
| 308 | // Set Disconnect to HALs |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 309 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 310 | |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 311 | checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 312 | mAvailableInputDevices.remove(devDesc); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 313 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 314 | // Propagate device availability to Engine |
| 315 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 316 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 317 | |
| 318 | default: |
| 319 | ALOGE("setDeviceConnectionState() invalid state: %x", state); |
| 320 | return BAD_VALUE; |
| 321 | } |
| 322 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 323 | closeAllInputs(); |
Eric Laurent | 5f5fca5 | 2016-08-04 11:48:57 -0700 | [diff] [blame] | 324 | // As the input device list can impact the output device selection, update |
| 325 | // getDeviceForStrategy() cache |
| 326 | updateDevicesAndOutputs(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 327 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 328 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 329 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 330 | updateCallRouting(newDevice); |
| 331 | } |
| 332 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 333 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
| 334 | cleanUpForDevice(devDesc); |
| 335 | } |
| 336 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 337 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 338 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 339 | } // end if is input device |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 340 | |
| 341 | ALOGW("setDeviceConnectionState() invalid device: %x", device); |
| 342 | return BAD_VALUE; |
| 343 | } |
| 344 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 345 | audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 346 | const char *device_address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 347 | { |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 348 | sp<DeviceDescriptor> devDesc = |
| 349 | mHwModules.getDeviceDescriptor(device, device_address, "", |
| 350 | (strlen(device_address) != 0)/*matchAddress*/); |
| 351 | |
| 352 | if (devDesc == 0) { |
| 353 | ALOGW("getDeviceConnectionState() undeclared device, type %08x, address: %s", |
| 354 | device, device_address); |
| 355 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
| 356 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 357 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 358 | DeviceVector *deviceVector; |
| 359 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 360 | if (audio_is_output_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 361 | deviceVector = &mAvailableOutputDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 362 | } else if (audio_is_input_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 363 | deviceVector = &mAvailableInputDevices; |
| 364 | } else { |
| 365 | ALOGW("getDeviceConnectionState() invalid device type %08x", device); |
| 366 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 367 | } |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 368 | |
| 369 | return (deviceVector->getDevice(device, String8(device_address)) != 0) ? |
| 370 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 371 | } |
| 372 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 373 | status_t AudioPolicyManager::handleDeviceConfigChange(audio_devices_t device, |
| 374 | const char *device_address, |
| 375 | const char *device_name) |
| 376 | { |
| 377 | status_t status; |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 378 | String8 reply; |
| 379 | AudioParameter param; |
| 380 | int isReconfigA2dpSupported = 0; |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 381 | |
| 382 | ALOGV("handleDeviceConfigChange(() device: 0x%X, address %s name %s", |
| 383 | device, device_address, device_name); |
| 384 | |
Pavlin Radoslavov | c694ff4 | 2017-01-09 23:27:29 -0800 | [diff] [blame] | 385 | // connect/disconnect only 1 device at a time |
| 386 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; |
| 387 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 388 | // Check if the device is currently connected |
| 389 | sp<DeviceDescriptor> devDesc = |
| 390 | mHwModules.getDeviceDescriptor(device, device_address, device_name); |
| 391 | ssize_t index = mAvailableOutputDevices.indexOf(devDesc); |
| 392 | if (index < 0) { |
| 393 | // Nothing to do: device is not connected |
| 394 | return NO_ERROR; |
| 395 | } |
| 396 | |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 397 | // For offloaded A2DP, Hw modules may have the capability to |
| 398 | // configure codecs. Check if any of the loaded hw modules |
| 399 | // supports this. |
| 400 | // If supported, send a set parameter to configure A2DP codecs |
| 401 | // and return. No need to toggle device state. |
| 402 | if (device & AUDIO_DEVICE_OUT_ALL_A2DP) { |
| 403 | reply = mpClientInterface->getParameters( |
| 404 | AUDIO_IO_HANDLE_NONE, |
| 405 | String8(AudioParameter::keyReconfigA2dpSupported)); |
| 406 | AudioParameter repliedParameters(reply); |
| 407 | repliedParameters.getInt( |
| 408 | String8(AudioParameter::keyReconfigA2dpSupported), isReconfigA2dpSupported); |
| 409 | if (isReconfigA2dpSupported) { |
| 410 | const String8 key(AudioParameter::keyReconfigA2dp); |
| 411 | param.add(key, String8("true")); |
| 412 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 413 | return NO_ERROR; |
| 414 | } |
| 415 | } |
| 416 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 417 | // Toggle the device state: UNAVAILABLE -> AVAILABLE |
| 418 | // This will force reading again the device configuration |
| 419 | status = setDeviceConnectionState(device, |
| 420 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 421 | device_address, device_name); |
| 422 | if (status != NO_ERROR) { |
| 423 | ALOGW("handleDeviceConfigChange() error disabling connection state: %d", |
| 424 | status); |
| 425 | return status; |
| 426 | } |
| 427 | |
| 428 | status = setDeviceConnectionState(device, |
| 429 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 430 | device_address, device_name); |
| 431 | if (status != NO_ERROR) { |
| 432 | ALOGW("handleDeviceConfigChange() error enabling connection state: %d", |
| 433 | status); |
| 434 | return status; |
| 435 | } |
| 436 | |
| 437 | return NO_ERROR; |
| 438 | } |
| 439 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 440 | uint32_t AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, uint32_t delayMs) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 441 | { |
| 442 | bool createTxPatch = false; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 443 | uint32_t muteWaitMs = 0; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 444 | |
Andy Hung | a76c7de | 2016-12-13 19:14:31 -0800 | [diff] [blame] | 445 | if(!hasPrimaryOutput() || mPrimaryOutput->device() == AUDIO_DEVICE_OUT_STUB) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 446 | return muteWaitMs; |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 447 | } |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 448 | audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 449 | ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice); |
| 450 | |
| 451 | // release existing RX patch if any |
| 452 | if (mCallRxPatch != 0) { |
| 453 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 454 | mCallRxPatch.clear(); |
| 455 | } |
| 456 | // release TX patch if any |
| 457 | if (mCallTxPatch != 0) { |
| 458 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 459 | mCallTxPatch.clear(); |
| 460 | } |
| 461 | |
| 462 | // If the RX device is on the primary HW module, then use legacy routing method for voice calls |
| 463 | // via setOutputDevice() on primary output. |
| 464 | // Otherwise, create two audio patches for TX and RX path. |
| 465 | if (availablePrimaryOutputDevices() & rxDevice) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 466 | muteWaitMs = setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 467 | // If the TX device is also on the primary HW module, setOutputDevice() will take care |
| 468 | // of it due to legacy implementation. If not, create a patch. |
| 469 | if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN) |
| 470 | == AUDIO_DEVICE_NONE) { |
| 471 | createTxPatch = true; |
| 472 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 473 | } else { // create RX path audio patch |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 474 | mCallRxPatch = createTelephonyPatch(true /*isRx*/, rxDevice, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 475 | createTxPatch = true; |
| 476 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 477 | if (createTxPatch) { // create TX path audio patch |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 478 | mCallTxPatch = createTelephonyPatch(false /*isRx*/, txDevice, delayMs); |
| 479 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 480 | |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 481 | return muteWaitMs; |
| 482 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 483 | |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 484 | sp<AudioPatch> AudioPolicyManager::createTelephonyPatch( |
| 485 | bool isRx, audio_devices_t device, uint32_t delayMs) { |
| 486 | struct audio_patch patch; |
| 487 | patch.num_sources = 1; |
| 488 | patch.num_sinks = 1; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 489 | |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 490 | sp<DeviceDescriptor> txSourceDeviceDesc; |
| 491 | if (isRx) { |
| 492 | fillAudioPortConfigForDevice(mAvailableOutputDevices, device, &patch.sinks[0]); |
| 493 | fillAudioPortConfigForDevice( |
| 494 | mAvailableInputDevices, AUDIO_DEVICE_IN_TELEPHONY_RX, &patch.sources[0]); |
| 495 | } else { |
| 496 | txSourceDeviceDesc = fillAudioPortConfigForDevice( |
| 497 | mAvailableInputDevices, device, &patch.sources[0]); |
| 498 | fillAudioPortConfigForDevice( |
| 499 | mAvailableOutputDevices, AUDIO_DEVICE_OUT_TELEPHONY_TX, &patch.sinks[0]); |
| 500 | } |
| 501 | |
| 502 | audio_devices_t outputDevice = isRx ? device : AUDIO_DEVICE_OUT_TELEPHONY_TX; |
| 503 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(outputDevice, mOutputs); |
| 504 | audio_io_handle_t output = selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID); |
| 505 | // request to reuse existing output stream if one is already opened to reach the target device |
| 506 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 507 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 508 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 509 | "%s() %#x device output %d is duplicated", __func__, outputDevice, output); |
| 510 | outputDesc->toAudioPortConfig(&patch.sources[1]); |
| 511 | patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
| 512 | patch.num_sources = 2; |
| 513 | } |
| 514 | |
| 515 | if (!isRx) { |
Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 516 | // terminate active capture if on the same HW module as the call TX source device |
| 517 | // FIXME: would be better to refine to only inputs whose profile connects to the |
| 518 | // call TX device but this information is not in the audio patch and logic here must be |
| 519 | // symmetric to the one in startInput() |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 520 | for (const auto& activeDesc : mInputs.getActiveInputs()) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 521 | if (activeDesc->hasSameHwModuleAs(txSourceDeviceDesc)) { |
| 522 | AudioSessionCollection activeSessions = |
| 523 | activeDesc->getAudioSessions(true /*activeOnly*/); |
| 524 | for (size_t j = 0; j < activeSessions.size(); j++) { |
| 525 | audio_session_t activeSession = activeSessions.keyAt(j); |
| 526 | stopInput(activeDesc->mIoHandle, activeSession); |
| 527 | releaseInput(activeDesc->mIoHandle, activeSession); |
| 528 | } |
Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 529 | } |
| 530 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 531 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 532 | |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 533 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 534 | status_t status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, delayMs); |
| 535 | ALOGW_IF(status != NO_ERROR, |
| 536 | "%s() error %d creating %s audio patch", __func__, status, isRx ? "RX" : "TX"); |
| 537 | sp<AudioPatch> audioPatch; |
| 538 | if (status == NO_ERROR) { |
| 539 | audioPatch = new AudioPatch(&patch, mUidCached); |
| 540 | audioPatch->mAfPatchHandle = afPatchHandle; |
| 541 | audioPatch->mUid = mUidCached; |
| 542 | } |
| 543 | return audioPatch; |
| 544 | } |
| 545 | |
| 546 | sp<DeviceDescriptor> AudioPolicyManager::fillAudioPortConfigForDevice( |
| 547 | const DeviceVector& devices, audio_devices_t device, audio_port_config *config) { |
| 548 | DeviceVector deviceList = devices.getDevicesFromType(device); |
| 549 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 550 | "%s() selected device type %#x is not in devices list", __func__, device); |
| 551 | sp<DeviceDescriptor> deviceDesc = deviceList.itemAt(0); |
| 552 | deviceDesc->toAudioPortConfig(config); |
| 553 | return deviceDesc; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 554 | } |
| 555 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 556 | void AudioPolicyManager::setPhoneState(audio_mode_t state) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 557 | { |
| 558 | ALOGV("setPhoneState() state %d", state); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 559 | // store previous phone state for management of sonification strategy below |
| 560 | int oldState = mEngine->getPhoneState(); |
| 561 | |
| 562 | if (mEngine->setPhoneState(state) != NO_ERROR) { |
| 563 | ALOGW("setPhoneState() invalid or same state %d", state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 564 | return; |
| 565 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 566 | /// Opens: can these line be executed after the switch of volume curves??? |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 567 | // if leaving call state, handle special case of active streams |
| 568 | // pertaining to sonification strategy see handleIncallSonification() |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 569 | if (isStateInCall(oldState)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 570 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 571 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 572 | handleIncallSonification((audio_stream_type_t)stream, false, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 573 | } |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 574 | |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 575 | // force reevaluating accessibility routing when call stops |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 576 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 577 | } |
| 578 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 579 | /** |
| 580 | * Switching to or from incall state or switching between telephony and VoIP lead to force |
| 581 | * routing command. |
| 582 | */ |
| 583 | bool force = ((is_state_in_call(oldState) != is_state_in_call(state)) |
| 584 | || (is_state_in_call(state) && (state != oldState))); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 585 | |
| 586 | // check for device and output changes triggered by new phone state |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 587 | checkA2dpSuspend(); |
| 588 | checkOutputForAllStrategies(); |
| 589 | updateDevicesAndOutputs(); |
| 590 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 591 | int delayMs = 0; |
| 592 | if (isStateInCall(state)) { |
| 593 | nsecs_t sysTime = systemTime(); |
| 594 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 595 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 596 | // mute media and sonification strategies and delay device switch by the largest |
| 597 | // latency of any output where either strategy is active. |
| 598 | // 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] | 599 | if ((isStrategyActive(desc, STRATEGY_MEDIA, |
| 600 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 601 | sysTime) || |
| 602 | isStrategyActive(desc, STRATEGY_SONIFICATION, |
| 603 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 604 | sysTime)) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 605 | (delayMs < (int)desc->latency()*2)) { |
| 606 | delayMs = desc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 607 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 608 | setStrategyMute(STRATEGY_MEDIA, true, desc); |
| 609 | setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 610 | getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/)); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 611 | setStrategyMute(STRATEGY_SONIFICATION, true, desc); |
| 612 | setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 613 | getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/)); |
| 614 | } |
| 615 | } |
| 616 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 617 | if (hasPrimaryOutput()) { |
| 618 | // Note that despite the fact that getNewOutputDevice() is called on the primary output, |
| 619 | // the device returned is not necessarily reachable via this output |
| 620 | audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 621 | // force routing command to audio hardware when ending call |
| 622 | // even if no device change is needed |
| 623 | if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) { |
| 624 | rxDevice = mPrimaryOutput->device(); |
| 625 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 626 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 627 | if (state == AUDIO_MODE_IN_CALL) { |
| 628 | updateCallRouting(rxDevice, delayMs); |
| 629 | } else if (oldState == AUDIO_MODE_IN_CALL) { |
| 630 | if (mCallRxPatch != 0) { |
| 631 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 632 | mCallRxPatch.clear(); |
| 633 | } |
| 634 | if (mCallTxPatch != 0) { |
| 635 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 636 | mCallTxPatch.clear(); |
| 637 | } |
| 638 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
| 639 | } else { |
| 640 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 641 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 642 | } |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 643 | |
| 644 | // reevaluate routing on all outputs in case tracks have been started during the call |
| 645 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 646 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 647 | audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/); |
| 648 | if (state != AUDIO_MODE_IN_CALL || desc != mPrimaryOutput) { |
Yung Ti Su | f60c824 | 2018-05-10 18:07:26 +0800 | [diff] [blame] | 649 | setOutputDevice(desc, newDevice, (newDevice != AUDIO_DEVICE_NONE), 0 /*delayMs*/); |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 653 | // if entering in call state, handle special case of active streams |
| 654 | // pertaining to sonification strategy see handleIncallSonification() |
| 655 | if (isStateInCall(state)) { |
| 656 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 657 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 658 | handleIncallSonification((audio_stream_type_t)stream, true, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 659 | } |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 660 | |
| 661 | // force reevaluating accessibility routing when call starts |
| 662 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | // 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] | 666 | if (state == AUDIO_MODE_RINGTONE && |
| 667 | isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 668 | mLimitRingtoneVolume = true; |
| 669 | } else { |
| 670 | mLimitRingtoneVolume = false; |
| 671 | } |
| 672 | } |
| 673 | |
Jean-Michel Trivi | 887a9ed | 2015-03-31 18:02:24 -0700 | [diff] [blame] | 674 | audio_mode_t AudioPolicyManager::getPhoneState() { |
| 675 | return mEngine->getPhoneState(); |
| 676 | } |
| 677 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 678 | void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 679 | audio_policy_forced_cfg_t config) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 680 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 681 | ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState()); |
Eric Laurent | 8dc87a6 | 2017-05-16 19:00:40 -0700 | [diff] [blame] | 682 | if (config == mEngine->getForceUse(usage)) { |
| 683 | return; |
| 684 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 685 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 686 | if (mEngine->setForceUse(usage, config) != NO_ERROR) { |
| 687 | ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage); |
| 688 | return; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 689 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 690 | bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) || |
| 691 | (usage == AUDIO_POLICY_FORCE_FOR_DOCK) || |
| 692 | (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 693 | |
| 694 | // check for device and output changes triggered by new force usage |
| 695 | checkA2dpSuspend(); |
| 696 | checkOutputForAllStrategies(); |
| 697 | updateDevicesAndOutputs(); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 698 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 699 | //FIXME: workaround for truncated touch sounds |
| 700 | // to be removed when the problem is handled by system UI |
| 701 | uint32_t delayMs = 0; |
| 702 | uint32_t waitMs = 0; |
| 703 | if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) { |
| 704 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 705 | } |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 706 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 707 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/); |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 708 | waitMs = updateCallRouting(newDevice, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 709 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 710 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 711 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
| 712 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/); |
| 713 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 714 | waitMs = setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE), |
| 715 | delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 716 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 717 | if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 718 | applyStreamVolumes(outputDesc, newDevice, waitMs, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 722 | for (const auto& activeDesc : mInputs.getActiveInputs()) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 723 | audio_devices_t newDevice = getNewInputDevice(activeDesc); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 724 | // 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] | 725 | if (activeDesc->mProfile->getSupportedDevices().types() & |
| 726 | (newDevice & ~AUDIO_DEVICE_BIT_IN)) { |
| 727 | setInputDevice(activeDesc->mIoHandle, newDevice); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 728 | } else { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 729 | closeInput(activeDesc->mIoHandle); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 730 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 731 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 732 | } |
| 733 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 734 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 735 | { |
| 736 | ALOGV("setSystemProperty() property %s, value %s", property, value); |
| 737 | } |
| 738 | |
| 739 | // Find a direct output profile compatible with the parameters passed, even if the input flags do |
| 740 | // not explicitly request a direct output |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 741 | sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput( |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 742 | audio_devices_t device, |
| 743 | uint32_t samplingRate, |
| 744 | audio_format_t format, |
| 745 | audio_channel_mask_t channelMask, |
| 746 | audio_output_flags_t flags) |
| 747 | { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 748 | // only retain flags that will drive the direct output profile selection |
| 749 | // if explicitly requested |
| 750 | static const uint32_t kRelevantFlags = |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 751 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | |
| 752 | AUDIO_OUTPUT_FLAG_VOIP_RX); |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 753 | flags = |
| 754 | (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); |
| 755 | |
| 756 | sp<IOProfile> profile; |
| 757 | |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 758 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 759 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 760 | if (!curProfile->isCompatibleProfile(device, String8(""), |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 761 | samplingRate, NULL /*updatedSamplingRate*/, |
| 762 | format, NULL /*updatedFormat*/, |
| 763 | channelMask, NULL /*updatedChannelMask*/, |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 764 | flags)) { |
| 765 | continue; |
| 766 | } |
| 767 | // reject profiles not corresponding to a device currently available |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 768 | if ((mAvailableOutputDevices.types() & curProfile->getSupportedDevicesType()) == 0) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 769 | continue; |
| 770 | } |
| 771 | // 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] | 772 | if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 773 | continue; |
| 774 | } |
| 775 | profile = curProfile; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 776 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 777 | break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 778 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 779 | } |
| 780 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 781 | return profile; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 782 | } |
| 783 | |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 784 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 785 | { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 786 | routing_strategy strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 787 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Andy Hung | c990152 | 2017-11-10 20:07:54 -0800 | [diff] [blame] | 788 | |
| 789 | // Note that related method getOutputForAttr() uses getOutputForDevice() not selectOutput(). |
| 790 | // We use selectOutput() here since we don't have the desired AudioTrack sample rate, |
| 791 | // format, flags, etc. This may result in some discrepancy for functions that utilize |
| 792 | // getOutput() solely on audio_stream_type such as AudioSystem::getOutputFrameCount() |
| 793 | // and AudioSystem::getOutputSamplingRate(). |
| 794 | |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 795 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 796 | 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] | 797 | |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 798 | ALOGV("getOutput() stream %d selected device %08x, output %d", stream, device, output); |
| 799 | return output; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 800 | } |
| 801 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 802 | status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, |
| 803 | audio_io_handle_t *output, |
| 804 | audio_session_t session, |
| 805 | audio_stream_type_t *stream, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 806 | uid_t uid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 807 | const audio_config_t *config, |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 808 | audio_output_flags_t *flags, |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 809 | audio_port_handle_t *selectedDeviceId, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 810 | audio_port_handle_t *portId) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 811 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 812 | audio_attributes_t attributes; |
| 813 | if (attr != NULL) { |
| 814 | if (!isValidAttributes(attr)) { |
| 815 | ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]", |
| 816 | attr->usage, attr->content_type, attr->flags, |
| 817 | attr->tags); |
| 818 | return BAD_VALUE; |
| 819 | } |
| 820 | attributes = *attr; |
| 821 | } else { |
| 822 | if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) { |
| 823 | ALOGE("getOutputForAttr(): invalid stream type"); |
| 824 | return BAD_VALUE; |
| 825 | } |
| 826 | stream_type_to_audio_attributes(*stream, &attributes); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 827 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 828 | |
| 829 | // TODO: check for existing client for this port ID |
| 830 | if (*portId == AUDIO_PORT_HANDLE_NONE) { |
| 831 | *portId = AudioPort::getNextUniqueId(); |
| 832 | } |
| 833 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 834 | sp<SwAudioOutputDescriptor> desc; |
Jean-Michel Trivi | e8deced | 2016-02-11 12:50:39 -0800 | [diff] [blame] | 835 | if (mPolicyMixes.getOutputForAttr(attributes, uid, desc) == NO_ERROR) { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 836 | ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr"); |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 837 | if (!audio_has_proportional_frames(config->format)) { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 838 | return BAD_VALUE; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 839 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 840 | *stream = streamTypefromAttributesInt(&attributes); |
| 841 | *output = desc->mIoHandle; |
| 842 | ALOGV("getOutputForAttr() returns output %d", *output); |
| 843 | return NO_ERROR; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 844 | } |
| 845 | if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 846 | ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE"); |
| 847 | return BAD_VALUE; |
| 848 | } |
| 849 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 850 | ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x" |
| 851 | " session %d selectedDeviceId %d", |
| 852 | attributes.usage, attributes.content_type, attributes.tags, attributes.flags, |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 853 | session, *selectedDeviceId); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 854 | |
| 855 | *stream = streamTypefromAttributesInt(&attributes); |
| 856 | |
| 857 | // Explicit routing? |
| 858 | sp<DeviceDescriptor> deviceDesc; |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 859 | if (*selectedDeviceId != AUDIO_PORT_HANDLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 860 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(*selectedDeviceId); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 861 | } |
| 862 | mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 863 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 864 | routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 865 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 866 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 867 | if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 868 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 869 | } |
| 870 | |
Nadav Bar | 0d143ae | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 871 | // Set incall music only if device was explicitly set, and fallback to the device which is |
| 872 | // chosen by the engine if not. |
| 873 | // FIXME: provide a more generic approach which is not device specific and move this back |
| 874 | // to getOutputForDevice. |
| 875 | if (device == AUDIO_DEVICE_OUT_TELEPHONY_TX && |
| 876 | *stream == AUDIO_STREAM_MUSIC && |
| 877 | audio_is_linear_pcm(config->format) && |
| 878 | isInCall()) { |
| 879 | if (*selectedDeviceId != AUDIO_PORT_HANDLE_NONE) { |
| 880 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INCALL_MUSIC; |
| 881 | } else { |
| 882 | device = mEngine->getDeviceForStrategy(strategy); |
| 883 | } |
| 884 | } |
| 885 | |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 886 | ALOGV("getOutputForAttr() device 0x%x, sampling rate %d, format %#x, channel mask %#x, " |
| 887 | "flags %#x", |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 888 | device, config->sample_rate, config->format, config->channel_mask, *flags); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 889 | |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 890 | *output = getOutputForDevice(device, session, *stream, config, flags); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 891 | if (*output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 892 | mOutputRoutes.removeRoute(session); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 893 | return INVALID_OPERATION; |
| 894 | } |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 895 | |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 896 | DeviceVector outputDevices = mAvailableOutputDevices.getDevicesFromType(device); |
| 897 | *selectedDeviceId = outputDevices.size() > 0 ? outputDevices.itemAt(0)->getId() |
| 898 | : AUDIO_PORT_HANDLE_NONE; |
| 899 | |
| 900 | ALOGV(" getOutputForAttr() returns output %d selectedDeviceId %d", *output, *selectedDeviceId); |
| 901 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 902 | return NO_ERROR; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | audio_io_handle_t AudioPolicyManager::getOutputForDevice( |
| 906 | audio_devices_t device, |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 907 | audio_session_t session, |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 908 | audio_stream_type_t stream, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 909 | const audio_config_t *config, |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 910 | audio_output_flags_t *flags) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 911 | { |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 912 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 913 | status_t status; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 914 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 915 | // open a direct output if required by specified parameters |
| 916 | //force direct flag if offload flag is set: offloading implies a direct output stream |
| 917 | // and all common behaviors are driven by checking only the direct flag |
| 918 | // this should normally be set appropriately in the policy configuration file |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 919 | if ((*flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 920 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 921 | } |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 922 | if ((*flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 923 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 924 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 925 | // only allow deep buffering for music stream type |
| 926 | if (stream != AUDIO_STREAM_MUSIC) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 927 | *flags = (audio_output_flags_t)(*flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 928 | } else if (/* stream == AUDIO_STREAM_MUSIC && */ |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 929 | *flags == AUDIO_OUTPUT_FLAG_NONE && |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 930 | property_get_bool("audio.deep_buffer.media", false /* default_value */)) { |
| 931 | // use DEEP_BUFFER as default output for music stream type |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 932 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER; |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 933 | } |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 934 | if (stream == AUDIO_STREAM_TTS) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 935 | *flags = AUDIO_OUTPUT_FLAG_TTS; |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 936 | } else if (stream == AUDIO_STREAM_VOICE_CALL && |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 937 | audio_is_linear_pcm(config->format)) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 938 | *flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX | |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 939 | AUDIO_OUTPUT_FLAG_DIRECT); |
| 940 | ALOGV("Set VoIP and Direct output flags for PCM format"); |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 941 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 942 | |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 943 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 944 | sp<IOProfile> profile; |
| 945 | |
| 946 | // 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] | 947 | // and not explicitly requested |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 948 | if (((*flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 949 | audio_is_linear_pcm(config->format) && config->sample_rate <= SAMPLE_RATE_HZ_MAX && |
| 950 | audio_channel_count_from_out_mask(config->channel_mask) <= 2) { |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 951 | goto non_direct_output; |
| 952 | } |
| 953 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 954 | // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled. |
| 955 | // This prevents creating an offloaded track and tearing it down immediately after start |
| 956 | // when audioflinger detects there is an active non offloadable effect. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 957 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 958 | // This may prevent offloading in rare situations where effects are left active by apps |
| 959 | // in the background. |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 960 | |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 961 | if (((*flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 962 | !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 963 | profile = getProfileForDirectOutput(device, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 964 | config->sample_rate, |
| 965 | config->format, |
| 966 | config->channel_mask, |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 967 | (audio_output_flags_t)*flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 968 | } |
| 969 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 970 | if (profile != 0) { |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 971 | // exclusive outputs for MMAP and Offload are enforced by different session ids. |
| 972 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 973 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 974 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { |
| 975 | // reuse direct output if currently open by the same client |
| 976 | // and configured with same parameters |
| 977 | if ((config->sample_rate == desc->mSamplingRate) && |
Andy Hung | 5659ed5 | 2018-04-30 10:31:26 -0700 | [diff] [blame] | 978 | (config->format == desc->mFormat) && |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 979 | (config->channel_mask == desc->mChannelMask) && |
| 980 | (session == desc->mDirectClientSession)) { |
| 981 | desc->mDirectOpenCount++; |
| 982 | ALOGI("getOutputForDevice() reusing direct output %d for session %d", |
| 983 | mOutputs.keyAt(i), session); |
| 984 | return mOutputs.keyAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 985 | } |
| 986 | } |
| 987 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 988 | |
| 989 | if (!profile->canOpenNewIo()) { |
| 990 | goto non_direct_output; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 991 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 992 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 993 | sp<SwAudioOutputDescriptor> outputDesc = |
| 994 | new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 995 | |
| 996 | DeviceVector outputDevices = mAvailableOutputDevices.getDevicesFromType(device); |
| 997 | String8 address = outputDevices.size() > 0 ? outputDevices.itemAt(0)->mAddress |
| 998 | : String8(""); |
| 999 | |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1000 | status = outputDesc->open(config, device, address, stream, *flags, &output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1001 | |
| 1002 | // only accept an output with the requested parameters |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1003 | if (status != NO_ERROR || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1004 | (config->sample_rate != 0 && config->sample_rate != outputDesc->mSamplingRate) || |
Andy Hung | 5659ed5 | 2018-04-30 10:31:26 -0700 | [diff] [blame] | 1005 | (config->format != AUDIO_FORMAT_DEFAULT && config->format != outputDesc->mFormat) || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1006 | (config->channel_mask != 0 && config->channel_mask != outputDesc->mChannelMask)) { |
| 1007 | ALOGV("getOutputForDevice() failed opening direct output: output %d sample rate %d %d," |
| 1008 | "format %d %d, channel mask %04x %04x", output, config->sample_rate, |
| 1009 | outputDesc->mSamplingRate, config->format, outputDesc->mFormat, |
| 1010 | config->channel_mask, outputDesc->mChannelMask); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1011 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1012 | outputDesc->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1013 | } |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 1014 | // 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] | 1015 | if (audio_is_linear_pcm(config->format) && |
| 1016 | config->sample_rate <= SAMPLE_RATE_HZ_MAX) { |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 1017 | goto non_direct_output; |
| 1018 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1019 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1020 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1021 | outputDesc->mRefCount[stream] = 0; |
| 1022 | outputDesc->mStopTime[stream] = 0; |
| 1023 | outputDesc->mDirectOpenCount = 1; |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 1024 | outputDesc->mDirectClientSession = session; |
| 1025 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1026 | addOutput(output, outputDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1027 | mPreviousOutputs = mOutputs; |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 1028 | ALOGV("getOutputForDevice() returns new direct output %d", output); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1029 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1030 | return output; |
| 1031 | } |
| 1032 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 1033 | non_direct_output: |
Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 1034 | |
| 1035 | // A request for HW A/V sync cannot fallback to a mixed output because time |
| 1036 | // stamps are embedded in audio data |
Phil Burk | 2d05993 | 2018-02-15 15:55:11 -0800 | [diff] [blame] | 1037 | 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] | 1038 | return AUDIO_IO_HANDLE_NONE; |
| 1039 | } |
| 1040 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1041 | // ignoring channel mask due to downmix capability in mixer |
| 1042 | |
| 1043 | // open a non direct output |
| 1044 | |
| 1045 | // for non direct outputs, only PCM is supported |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1046 | if (audio_is_linear_pcm(config->format)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1047 | // get which output is suitable for the specified stream. The actual |
| 1048 | // routing change will happen when startOutput() will be called |
| 1049 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 1050 | |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1051 | // 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] | 1052 | *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_DIRECT); |
| 1053 | output = selectOutput(outputs, *flags, config->format); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1054 | } |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 1055 | ALOGW_IF((output == 0), "getOutputForDevice() could not find output for stream %d, " |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1056 | "sampling rate %d, format %#x, channels %#x, flags %#x", |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1057 | stream, config->sample_rate, config->format, config->channel_mask, *flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1058 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1059 | return output; |
| 1060 | } |
| 1061 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1062 | 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] | 1063 | audio_output_flags_t flags, |
| 1064 | audio_format_t format) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1065 | { |
| 1066 | // select one output among several that provide a path to a particular device or set of |
| 1067 | // devices (the list was previously build by getOutputsForDevice()). |
| 1068 | // The priority is as follows: |
| 1069 | // 1: the output with the highest number of requested policy flags |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1070 | // 2: the output with the bit depth the closest to the requested one |
| 1071 | // 3: the primary output |
| 1072 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1073 | |
| 1074 | if (outputs.size() == 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1075 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1076 | } |
| 1077 | if (outputs.size() == 1) { |
| 1078 | return outputs[0]; |
| 1079 | } |
| 1080 | |
| 1081 | int maxCommonFlags = 0; |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1082 | audio_io_handle_t outputForFlags = AUDIO_IO_HANDLE_NONE; |
| 1083 | audio_io_handle_t outputForPrimary = AUDIO_IO_HANDLE_NONE; |
| 1084 | audio_io_handle_t outputForFormat = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1085 | audio_format_t bestFormat = AUDIO_FORMAT_INVALID; |
| 1086 | audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1087 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1088 | for (audio_io_handle_t output : outputs) { |
| 1089 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1090 | if (!outputDesc->isDuplicated()) { |
chenhg | 1794d71 | 2018-10-09 15:20:37 -0700 | [diff] [blame] | 1091 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 1092 | continue; |
| 1093 | } |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1094 | // if a valid format is specified, skip output if not compatible |
| 1095 | if (format != AUDIO_FORMAT_INVALID) { |
chenhg | 1794d71 | 2018-10-09 15:20:37 -0700 | [diff] [blame] | 1096 | if (!audio_is_linear_pcm(format)) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1097 | continue; |
| 1098 | } |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1099 | if (AudioPort::isBetterFormatMatch( |
| 1100 | outputDesc->mFormat, bestFormat, format)) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1101 | outputForFormat = output; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1102 | bestFormat = outputDesc->mFormat; |
| 1103 | } |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1104 | } |
| 1105 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1106 | int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags); |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1107 | if (commonFlags >= maxCommonFlags) { |
| 1108 | if (commonFlags == maxCommonFlags) { |
Andy Hung | c990152 | 2017-11-10 20:07:54 -0800 | [diff] [blame] | 1109 | if (format != AUDIO_FORMAT_INVALID |
| 1110 | && AudioPort::isBetterFormatMatch( |
| 1111 | outputDesc->mFormat, bestFormatForFlags, format)) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1112 | outputForFlags = output; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1113 | bestFormatForFlags = outputDesc->mFormat; |
| 1114 | } |
| 1115 | } else { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1116 | outputForFlags = output; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1117 | maxCommonFlags = commonFlags; |
| 1118 | bestFormatForFlags = outputDesc->mFormat; |
| 1119 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1120 | ALOGV("selectOutput() commonFlags for output %d, %04x", output, commonFlags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1121 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1122 | if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1123 | outputForPrimary = output; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1124 | } |
| 1125 | } |
| 1126 | } |
| 1127 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1128 | if (outputForFlags != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1129 | return outputForFlags; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1130 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1131 | if (outputForFormat != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1132 | return outputForFormat; |
| 1133 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1134 | if (outputForPrimary != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1135 | return outputForPrimary; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | return outputs[0]; |
| 1139 | } |
| 1140 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1141 | status_t AudioPolicyManager::startOutput(audio_io_handle_t output, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1142 | audio_stream_type_t stream, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1143 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1144 | { |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1145 | ALOGV("startOutput() output %d, stream %d, session %d", |
| 1146 | output, stream, session); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1147 | ssize_t index = mOutputs.indexOfKey(output); |
| 1148 | if (index < 0) { |
| 1149 | ALOGW("startOutput() unknown output %d", output); |
| 1150 | return BAD_VALUE; |
| 1151 | } |
| 1152 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1153 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
| 1154 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1155 | status_t status = outputDesc->start(); |
| 1156 | if (status != NO_ERROR) { |
| 1157 | return status; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1158 | } |
| 1159 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1160 | // Routing? |
| 1161 | mOutputRoutes.incRouteActivity(session); |
| 1162 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1163 | audio_devices_t newDevice; |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1164 | AudioMix *policyMix = NULL; |
| 1165 | const char *address = NULL; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1166 | if (outputDesc->mPolicyMix != NULL) { |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1167 | policyMix = outputDesc->mPolicyMix; |
| 1168 | address = policyMix->mDeviceAddress.string(); |
| 1169 | if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 1170 | newDevice = policyMix->mDeviceType; |
| 1171 | } else { |
| 1172 | newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
| 1173 | } |
Eric Laurent | 2157f5b | 2018-04-25 18:33:02 -0700 | [diff] [blame] | 1174 | } else if (mOutputRoutes.getAndClearRouteChanged(session)) { |
Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 1175 | newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 1176 | if (newDevice != outputDesc->device()) { |
| 1177 | checkStrategyRoute(getStrategy(stream), output); |
| 1178 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1179 | } else { |
| 1180 | newDevice = AUDIO_DEVICE_NONE; |
| 1181 | } |
| 1182 | |
| 1183 | uint32_t delayMs = 0; |
| 1184 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1185 | status = startSource(outputDesc, stream, newDevice, address, &delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1186 | |
| 1187 | if (status != NO_ERROR) { |
| 1188 | mOutputRoutes.decRouteActivity(session); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1189 | outputDesc->stop(); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1190 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1191 | } |
| 1192 | // Automatically enable the remote submix input when output is started on a re routing mix |
| 1193 | // of type MIX_TYPE_RECORDERS |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1194 | if (audio_is_remote_submix_device(newDevice) && policyMix != NULL && |
| 1195 | policyMix->mMixType == MIX_TYPE_RECORDERS) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1196 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1197 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1198 | address, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1199 | "remote-submix"); |
| 1200 | } |
| 1201 | |
| 1202 | if (delayMs != 0) { |
| 1203 | usleep(delayMs * 1000); |
| 1204 | } |
| 1205 | |
| 1206 | return status; |
| 1207 | } |
| 1208 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 1209 | status_t AudioPolicyManager::startSource(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1210 | audio_stream_type_t stream, |
| 1211 | audio_devices_t device, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1212 | const char *address, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1213 | uint32_t *delayMs) |
| 1214 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1215 | // cannot start playback of STREAM_TTS if any other output is being used |
| 1216 | uint32_t beaconMuteLatency = 0; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1217 | |
| 1218 | *delayMs = 0; |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1219 | if (stream == AUDIO_STREAM_TTS) { |
| 1220 | ALOGV("\t found BEACON stream"); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 1221 | if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1222 | return INVALID_OPERATION; |
| 1223 | } else { |
| 1224 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); |
| 1225 | } |
| 1226 | } else { |
| 1227 | // some playback other than beacon starts |
| 1228 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); |
| 1229 | } |
| 1230 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1231 | // 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] | 1232 | // check active before incrementing usage count |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1233 | bool force = !outputDesc->isActive() && |
| 1234 | (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1235 | |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1236 | // requiresMuteCheck is false when we can bypass mute strategy. |
| 1237 | // It covers a common case when there is no materially active audio |
| 1238 | // and muting would result in unnecessary delay and dropped audio. |
| 1239 | const uint32_t outputLatencyMs = outputDesc->latency(); |
| 1240 | bool requiresMuteCheck = outputDesc->isActive(outputLatencyMs * 2); // account for drain |
| 1241 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1242 | // increment usage count for this stream on the requested output: |
| 1243 | // NOTE that the usage count is the same for duplicated output and hardware output which is |
| 1244 | // necessary for a correct control of hardware output routing by startOutput() and stopOutput() |
| 1245 | outputDesc->changeRefCount(stream, 1); |
| 1246 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1247 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1248 | selectOutputForMusicEffects(); |
| 1249 | } |
| 1250 | |
Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 1251 | if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1252 | // starting an output being rerouted? |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1253 | if (device == AUDIO_DEVICE_NONE) { |
| 1254 | device = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1255 | } |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1256 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1257 | routing_strategy strategy = getStrategy(stream); |
| 1258 | bool shouldWait = (strategy == STRATEGY_SONIFICATION) || |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1259 | (strategy == STRATEGY_SONIFICATION_RESPECTFUL) || |
| 1260 | (beaconMuteLatency > 0); |
| 1261 | uint32_t waitMs = beaconMuteLatency; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1262 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1263 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1264 | if (desc != outputDesc) { |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1265 | // An output has a shared device if |
| 1266 | // - managed by the same hw module |
| 1267 | // - supports the currently selected device |
| 1268 | const bool sharedDevice = outputDesc->sharesHwModuleWith(desc) |
| 1269 | && (desc->supportedDevices() & device) != AUDIO_DEVICE_NONE; |
| 1270 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1271 | // force a device change if any other output is: |
| 1272 | // - managed by the same hw module |
Jean-Michel Trivi | 4a5b481 | 2018-02-08 17:22:32 +0000 | [diff] [blame] | 1273 | // - supports currently selected device |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1274 | // - has a current device selection that differs from selected device. |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1275 | // - has an active audio patch |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1276 | // 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] | 1277 | // change the device currently selected by the other output. |
| 1278 | if (sharedDevice && |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1279 | desc->device() != device && |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1280 | desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1281 | force = true; |
| 1282 | } |
| 1283 | // 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] | 1284 | // a notification so that audio focus effect can propagate, or that a mute/unmute |
| 1285 | // event occurred for beacon |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1286 | const uint32_t latencyMs = desc->latency(); |
| 1287 | const bool isActive = desc->isActive(latencyMs * 2); // account for drain |
| 1288 | |
| 1289 | if (shouldWait && isActive && (waitMs < latencyMs)) { |
| 1290 | waitMs = latencyMs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1291 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1292 | |
| 1293 | // Require mute check if another output is on a shared device |
| 1294 | // and currently active to have proper drain and avoid pops. |
| 1295 | // Note restoring AudioTracks onto this output needs to invoke |
| 1296 | // a volume ramp if there is no mute. |
| 1297 | requiresMuteCheck |= sharedDevice && isActive; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1298 | } |
| 1299 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1300 | |
| 1301 | const uint32_t muteWaitMs = |
| 1302 | setOutputDevice(outputDesc, device, force, 0, NULL, address, requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1303 | |
| 1304 | // handle special case for sonification while in call |
| 1305 | if (isInCall()) { |
| 1306 | handleIncallSonification(stream, true, false); |
| 1307 | } |
| 1308 | |
| 1309 | // apply volume rules for current stream and device if necessary |
| 1310 | checkAndSetVolume(stream, |
Shuhei Miyazaki | 6fe7033 | 2017-07-31 15:21:28 +0900 | [diff] [blame] | 1311 | mVolumeCurves->getVolumeIndex(stream, outputDesc->device()), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1312 | outputDesc, |
Shuhei Miyazaki | 6fe7033 | 2017-07-31 15:21:28 +0900 | [diff] [blame] | 1313 | outputDesc->device()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1314 | |
| 1315 | // update the outputs if starting an output with a stream that can affect notification |
| 1316 | // routing |
| 1317 | handleNotificationRoutingForStream(stream); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1318 | |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 1319 | // force reevaluating accessibility routing when ringtone or alarm starts |
| 1320 | if (strategy == STRATEGY_SONIFICATION) { |
| 1321 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
| 1322 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1323 | |
| 1324 | if (waitMs > muteWaitMs) { |
| 1325 | *delayMs = waitMs - muteWaitMs; |
| 1326 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1327 | |
| 1328 | // FIXME: A device change (muteWaitMs > 0) likely introduces a volume change. |
| 1329 | // A volume change enacted by APM with 0 delay is not synchronous, as it goes |
| 1330 | // via AudioCommandThread to AudioFlinger. Hence it is possible that the volume |
| 1331 | // change occurs after the MixerThread starts and causes a stream volume |
| 1332 | // glitch. |
| 1333 | // |
| 1334 | // We do not introduce additional delay here. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1335 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1336 | |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 1337 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
| 1338 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
| 1339 | setStrategyMute(STRATEGY_SONIFICATION, true, outputDesc); |
| 1340 | } |
| 1341 | |
Tomoharu Kasahara | a81f098 | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 1342 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
| 1343 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
| 1344 | setStrategyMute(STRATEGY_SONIFICATION, true, outputDesc); |
| 1345 | } |
| 1346 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1347 | return NO_ERROR; |
| 1348 | } |
| 1349 | |
| 1350 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1351 | status_t AudioPolicyManager::stopOutput(audio_io_handle_t output, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1352 | audio_stream_type_t stream, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1353 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1354 | { |
| 1355 | ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session); |
| 1356 | ssize_t index = mOutputs.indexOfKey(output); |
| 1357 | if (index < 0) { |
| 1358 | ALOGW("stopOutput() unknown output %d", output); |
| 1359 | return BAD_VALUE; |
| 1360 | } |
| 1361 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1362 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1363 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1364 | if (outputDesc->mRefCount[stream] == 1) { |
| 1365 | // Automatically disable the remote submix input when output is stopped on a |
| 1366 | // re routing mix of type MIX_TYPE_RECORDERS |
| 1367 | if (audio_is_remote_submix_device(outputDesc->mDevice) && |
| 1368 | outputDesc->mPolicyMix != NULL && |
| 1369 | outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1370 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1371 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1372 | outputDesc->mPolicyMix->mDeviceAddress, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1373 | "remote-submix"); |
| 1374 | } |
| 1375 | } |
| 1376 | |
| 1377 | // Routing? |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1378 | bool forceDeviceUpdate = false; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1379 | if (outputDesc->mRefCount[stream] > 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1380 | int activityCount = mOutputRoutes.decRouteActivity(session); |
| 1381 | forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0)); |
| 1382 | |
| 1383 | if (forceDeviceUpdate) { |
| 1384 | checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE); |
| 1385 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1386 | } |
| 1387 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1388 | status_t status = stopSource(outputDesc, stream, forceDeviceUpdate); |
| 1389 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1390 | if (status == NO_ERROR ) { |
| 1391 | outputDesc->stop(); |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1392 | } |
| 1393 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1394 | } |
| 1395 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 1396 | status_t AudioPolicyManager::stopSource(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1397 | audio_stream_type_t stream, |
| 1398 | bool forceDeviceUpdate) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1399 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1400 | // always handle stream stop, check which stream type is stopping |
| 1401 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); |
| 1402 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1403 | // handle special case for sonification while in call |
| 1404 | if (isInCall()) { |
| 1405 | handleIncallSonification(stream, false, false); |
| 1406 | } |
| 1407 | |
| 1408 | if (outputDesc->mRefCount[stream] > 0) { |
| 1409 | // decrement usage count of this stream on the output |
| 1410 | outputDesc->changeRefCount(stream, -1); |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1411 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1412 | // store time at which the stream was stopped - see isStreamActive() |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1413 | if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1414 | outputDesc->mStopTime[stream] = systemTime(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1415 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1416 | // delay the device switch by twice the latency because stopOutput() is executed when |
| 1417 | // the track stop() command is received and at that time the audio track buffer can |
| 1418 | // still contain data that needs to be drained. The latency only covers the audio HAL |
| 1419 | // and kernel buffers. Also the latency does not always include additional delay in the |
| 1420 | // audio path (audio DSP, CODEC ...) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1421 | setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1422 | |
| 1423 | // force restoring the device selection on other active outputs if it differs from the |
| 1424 | // one being selected for this output |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1425 | uint32_t delayMs = outputDesc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1426 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1427 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1428 | if (desc != outputDesc && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1429 | desc->isActive() && |
| 1430 | outputDesc->sharesHwModuleWith(desc) && |
| 1431 | (newDevice != desc->device())) { |
Haynes Mathew George | 11c499a | 2016-08-26 12:08:25 -0700 | [diff] [blame] | 1432 | audio_devices_t newDevice2 = getNewOutputDevice(desc, false /*fromCache*/); |
| 1433 | bool force = desc->device() != newDevice2; |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 1434 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1435 | setOutputDevice(desc, |
Haynes Mathew George | 11c499a | 2016-08-26 12:08:25 -0700 | [diff] [blame] | 1436 | newDevice2, |
| 1437 | force, |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1438 | delayMs); |
| 1439 | // re-apply device specific volume if not done by setOutputDevice() |
| 1440 | if (!force) { |
| 1441 | applyStreamVolumes(desc, newDevice2, delayMs); |
| 1442 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1443 | } |
| 1444 | } |
| 1445 | // update the outputs if stopping one with a stream that can affect notification routing |
| 1446 | handleNotificationRoutingForStream(stream); |
| 1447 | } |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 1448 | |
| 1449 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
| 1450 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
| 1451 | setStrategyMute(STRATEGY_SONIFICATION, false, outputDesc); |
| 1452 | } |
| 1453 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1454 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1455 | selectOutputForMusicEffects(); |
| 1456 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1457 | return NO_ERROR; |
| 1458 | } else { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1459 | ALOGW("stopOutput() refcount is already 0"); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1460 | return INVALID_OPERATION; |
| 1461 | } |
| 1462 | } |
| 1463 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1464 | void AudioPolicyManager::releaseOutput(audio_io_handle_t output, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1465 | audio_stream_type_t stream __unused, |
| 1466 | audio_session_t session __unused) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1467 | { |
| 1468 | ALOGV("releaseOutput() %d", output); |
| 1469 | ssize_t index = mOutputs.indexOfKey(output); |
| 1470 | if (index < 0) { |
| 1471 | ALOGW("releaseOutput() releasing unknown output %d", output); |
| 1472 | return; |
| 1473 | } |
| 1474 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1475 | // Routing |
| 1476 | mOutputRoutes.removeRoute(session); |
| 1477 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1478 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1479 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1480 | if (desc->mDirectOpenCount <= 0) { |
| 1481 | ALOGW("releaseOutput() invalid open count %d for output %d", |
| 1482 | desc->mDirectOpenCount, output); |
| 1483 | return; |
| 1484 | } |
| 1485 | if (--desc->mDirectOpenCount == 0) { |
| 1486 | closeOutput(output); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1487 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1488 | } |
| 1489 | } |
| 1490 | } |
| 1491 | |
| 1492 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1493 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, |
| 1494 | audio_io_handle_t *input, |
| 1495 | audio_session_t session, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1496 | uid_t uid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1497 | const audio_config_base_t *config, |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1498 | audio_input_flags_t flags, |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1499 | audio_port_handle_t *selectedDeviceId, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1500 | input_type_t *inputType, |
| 1501 | audio_port_handle_t *portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1502 | { |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1503 | ALOGV("getInputForAttr() source %d, sampling rate %d, format %#x, channel mask %#x," |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1504 | "session %d, flags %#x", |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1505 | attr->source, config->sample_rate, config->format, config->channel_mask, session, flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1506 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1507 | status_t status = NO_ERROR; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1508 | // handle legacy remote submix case where the address was not always specified |
| 1509 | String8 address = String8(""); |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1510 | audio_source_t halInputSource; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1511 | audio_source_t inputSource = attr->source; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1512 | AudioMix *policyMix = NULL; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1513 | DeviceVector inputDevices; |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1514 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1515 | if (inputSource == AUDIO_SOURCE_DEFAULT) { |
| 1516 | inputSource = AUDIO_SOURCE_MIC; |
| 1517 | } |
| 1518 | |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1519 | // Explicit routing? |
| 1520 | sp<DeviceDescriptor> deviceDesc; |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1521 | if (*selectedDeviceId != AUDIO_PORT_HANDLE_NONE) { |
jiabin | c0c831a | 2018-01-29 17:55:15 -0800 | [diff] [blame] | 1522 | deviceDesc = mAvailableInputDevices.getDeviceFromId(*selectedDeviceId); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1523 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1524 | mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1525 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1526 | // special case for mmap capture: if an input IO handle is specified, we reuse this input if |
| 1527 | // possible |
| 1528 | if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) == AUDIO_INPUT_FLAG_MMAP_NOIRQ && |
| 1529 | *input != AUDIO_IO_HANDLE_NONE) { |
| 1530 | ssize_t index = mInputs.indexOfKey(*input); |
| 1531 | if (index < 0) { |
| 1532 | ALOGW("getInputForAttr() unknown MMAP input %d", *input); |
| 1533 | status = BAD_VALUE; |
| 1534 | goto error; |
| 1535 | } |
| 1536 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
| 1537 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
| 1538 | if (audioSession == 0) { |
| 1539 | ALOGW("getInputForAttr() unknown session %d on input %d", session, *input); |
| 1540 | status = BAD_VALUE; |
| 1541 | goto error; |
| 1542 | } |
| 1543 | // For MMAP mode, the first call to getInputForAttr() is made on behalf of audioflinger. |
| 1544 | // 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] | 1545 | // corresponds to a new client and is only permitted from the same UID. |
| 1546 | // 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] | 1547 | if (audioSession->openCount() == 1) { |
| 1548 | audioSession->setUid(uid); |
| 1549 | } else if (audioSession->uid() != uid) { |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 1550 | if (!audioSession->isSilenced()) { |
| 1551 | ALOGW("getInputForAttr() bad uid %d for session %d uid %d", |
| 1552 | uid, session, audioSession->uid()); |
| 1553 | status = INVALID_OPERATION; |
| 1554 | goto error; |
| 1555 | } |
| 1556 | audioSession->setUid(uid); |
| 1557 | audioSession->setSilenced(false); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1558 | } |
| 1559 | audioSession->changeOpenCount(1); |
| 1560 | *inputType = API_INPUT_LEGACY; |
| 1561 | if (*portId == AUDIO_PORT_HANDLE_NONE) { |
| 1562 | *portId = AudioPort::getNextUniqueId(); |
| 1563 | } |
| 1564 | inputDevices = mAvailableInputDevices.getDevicesFromType(inputDesc->mDevice); |
| 1565 | *selectedDeviceId = inputDevices.size() > 0 ? inputDevices.itemAt(0)->getId() |
| 1566 | : AUDIO_PORT_HANDLE_NONE; |
| 1567 | ALOGI("%s reusing MMAP input %d for session %d", __FUNCTION__, *input, session); |
| 1568 | |
| 1569 | return NO_ERROR; |
| 1570 | } |
| 1571 | |
| 1572 | *input = AUDIO_IO_HANDLE_NONE; |
| 1573 | *inputType = API_INPUT_INVALID; |
| 1574 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1575 | halInputSource = inputSource; |
| 1576 | |
| 1577 | // TODO: check for existing client for this port ID |
| 1578 | if (*portId == AUDIO_PORT_HANDLE_NONE) { |
| 1579 | *portId = AudioPort::getNextUniqueId(); |
| 1580 | } |
| 1581 | |
| 1582 | audio_devices_t device; |
| 1583 | |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1584 | if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX && |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1585 | strncmp(attr->tags, "addr=", strlen("addr=")) == 0) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1586 | status = mPolicyMixes.getInputMixForAttr(*attr, &policyMix); |
| 1587 | if (status != NO_ERROR) { |
| 1588 | goto error; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 1589 | } |
| 1590 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1591 | device = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 1592 | address = String8(attr->tags + strlen("addr=")); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1593 | } else { |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1594 | device = getDeviceAndMixForInputSource(inputSource, &policyMix); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1595 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1596 | ALOGW("getInputForAttr() could not find device for source %d", inputSource); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1597 | status = BAD_VALUE; |
| 1598 | goto error; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1599 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1600 | if (policyMix != NULL) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1601 | address = policyMix->mDeviceAddress; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1602 | if (policyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1603 | // there is an external policy, but this input is attached to a mix of recorders, |
| 1604 | // meaning it receives audio injected into the framework, so the recorder doesn't |
| 1605 | // know about it and is therefore considered "legacy" |
| 1606 | *inputType = API_INPUT_LEGACY; |
| 1607 | } else { |
| 1608 | // recording a mix of players defined by an external policy, we're rerouting for |
| 1609 | // an external policy |
| 1610 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
| 1611 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1612 | } else if (audio_is_remote_submix_device(device)) { |
| 1613 | address = String8("0"); |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1614 | *inputType = API_INPUT_MIX_CAPTURE; |
Eric Laurent | 82db269 | 2015-08-07 13:59:42 -0700 | [diff] [blame] | 1615 | } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) { |
| 1616 | *inputType = API_INPUT_TELEPHONY_RX; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1617 | } else { |
| 1618 | *inputType = API_INPUT_LEGACY; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1619 | } |
Ravi Kumar Alamanda | b367f5b | 2015-08-25 08:21:37 -0700 | [diff] [blame] | 1620 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | *input = getInputForDevice(device, address, session, uid, inputSource, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1624 | config, flags, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1625 | policyMix); |
| 1626 | if (*input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1627 | status = INVALID_OPERATION; |
| 1628 | goto error; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1629 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1630 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1631 | inputDevices = mAvailableInputDevices.getDevicesFromType(device); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1632 | *selectedDeviceId = inputDevices.size() > 0 ? inputDevices.itemAt(0)->getId() |
| 1633 | : AUDIO_PORT_HANDLE_NONE; |
| 1634 | |
| 1635 | ALOGV("getInputForAttr() returns input %d type %d selectedDeviceId %d", |
| 1636 | *input, *inputType, *selectedDeviceId); |
| 1637 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1638 | return NO_ERROR; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1639 | |
| 1640 | error: |
| 1641 | mInputRoutes.removeRoute(session); |
| 1642 | return status; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1643 | } |
| 1644 | |
| 1645 | |
| 1646 | audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device, |
| 1647 | String8 address, |
| 1648 | audio_session_t session, |
| 1649 | uid_t uid, |
| 1650 | audio_source_t inputSource, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1651 | const audio_config_base_t *config, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1652 | audio_input_flags_t flags, |
| 1653 | AudioMix *policyMix) |
| 1654 | { |
| 1655 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 1656 | audio_source_t halInputSource = inputSource; |
| 1657 | bool isSoundTrigger = false; |
| 1658 | |
| 1659 | if (inputSource == AUDIO_SOURCE_HOTWORD) { |
| 1660 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); |
| 1661 | if (index >= 0) { |
| 1662 | input = mSoundTriggerSessions.valueFor(session); |
| 1663 | isSoundTrigger = true; |
| 1664 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); |
| 1665 | ALOGV("SoundTrigger capture on session %d input %d", session, input); |
| 1666 | } else { |
| 1667 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1668 | } |
Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame] | 1669 | } else if (inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION && |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1670 | audio_is_linear_pcm(config->format)) { |
Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame] | 1671 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_VOIP_TX); |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1672 | } |
| 1673 | |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1674 | // find a compatible input profile (not necessarily identical in parameters) |
| 1675 | sp<IOProfile> profile; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1676 | // sampling rate and flags may be updated by getInputProfile |
| 1677 | uint32_t profileSamplingRate = (config->sample_rate == 0) ? |
| 1678 | SAMPLE_RATE_HZ_DEFAULT : config->sample_rate; |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 1679 | audio_format_t profileFormat; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1680 | audio_channel_mask_t profileChannelMask = config->channel_mask; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1681 | audio_input_flags_t profileFlags = flags; |
| 1682 | for (;;) { |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 1683 | 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] | 1684 | profile = getInputProfile(device, address, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1685 | profileSamplingRate, profileFormat, profileChannelMask, |
| 1686 | profileFlags); |
| 1687 | if (profile != 0) { |
| 1688 | break; // success |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1689 | } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) { |
| 1690 | profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1691 | } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) { |
| 1692 | profileFlags = AUDIO_INPUT_FLAG_NONE; // retry |
| 1693 | } else { // fail |
Glenn Kasten | faa10a6 | 2017-05-25 15:52:05 -0700 | [diff] [blame] | 1694 | ALOGW("getInputForDevice() could not find profile for device 0x%X, " |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1695 | "sampling rate %u, format %#x, channel mask 0x%X, flags %#x", |
| 1696 | device, config->sample_rate, config->format, config->channel_mask, flags); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1697 | return input; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1698 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1699 | } |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1700 | // Pick input sampling rate if not specified by client |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1701 | uint32_t samplingRate = config->sample_rate; |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1702 | if (samplingRate == 0) { |
| 1703 | samplingRate = profileSamplingRate; |
| 1704 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1705 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1706 | if (profile->getModuleHandle() == 0) { |
| 1707 | ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName()); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1708 | return input; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1709 | } |
| 1710 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1711 | sp<AudioSession> audioSession = new AudioSession(session, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1712 | inputSource, |
| 1713 | config->format, |
| 1714 | samplingRate, |
| 1715 | config->channel_mask, |
| 1716 | flags, |
| 1717 | uid, |
| 1718 | isSoundTrigger, |
| 1719 | policyMix, mpClientInterface); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1720 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1721 | // FIXME: disable concurrent capture until UI is ready |
| 1722 | #if 0 |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1723 | // reuse an open input if possible |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1724 | sp<AudioInputDescriptor> reusedInputDesc; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1725 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 1726 | sp<AudioInputDescriptor> desc = mInputs.valueAt(i); |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1727 | // reuse input if: |
| 1728 | // - it shares the same profile |
| 1729 | // AND |
| 1730 | // - it is not a reroute submix input |
| 1731 | // AND |
| 1732 | // - it is: not used for sound trigger |
| 1733 | // OR |
| 1734 | // used for sound trigger and all clients use the same session ID |
| 1735 | // |
| 1736 | if ((profile == desc->mProfile) && |
| 1737 | (isSoundTrigger == desc->isSoundTrigger()) && |
| 1738 | !is_virtual_input_device(device)) { |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1739 | |
| 1740 | sp<AudioSession> as = desc->getAudioSession(session); |
| 1741 | if (as != 0) { |
| 1742 | // do not allow unmatching properties on same session |
| 1743 | if (as->matches(audioSession)) { |
| 1744 | as->changeOpenCount(1); |
| 1745 | } else { |
| 1746 | ALOGW("getInputForDevice() record with different attributes" |
| 1747 | " exists for session %d", session); |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1748 | continue; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1749 | } |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1750 | } else if (isSoundTrigger) { |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1751 | continue; |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1752 | } |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1753 | |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1754 | // Reuse the already opened input stream on this profile if: |
| 1755 | // - the new capture source is background OR |
| 1756 | // - the path requested configurations match OR |
| 1757 | // - the new source priority is less than the highest source priority on this input |
| 1758 | // If the input stream cannot be reused, close it before opening a new stream |
| 1759 | // on the same profile for the new client so that the requested path configuration |
| 1760 | // can be selected. |
| 1761 | if (!isConcurrentSource(inputSource) && |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1762 | ((desc->mSamplingRate != samplingRate || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1763 | desc->mChannelMask != config->channel_mask || |
| 1764 | !audio_formats_match(desc->mFormat, config->format)) && |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1765 | (source_priority(desc->getHighestPrioritySource(false /*activeOnly*/)) < |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1766 | source_priority(inputSource)))) { |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1767 | reusedInputDesc = desc; |
| 1768 | continue; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1769 | } else { |
| 1770 | desc->addAudioSession(session, audioSession); |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1771 | ALOGV("%s: reusing input %d", __FUNCTION__, mInputs.keyAt(i)); |
| 1772 | return mInputs.keyAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1773 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1774 | } |
| 1775 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1776 | |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1777 | if (reusedInputDesc != 0) { |
| 1778 | AudioSessionCollection sessions = reusedInputDesc->getAudioSessions(false /*activeOnly*/); |
| 1779 | for (size_t j = 0; j < sessions.size(); j++) { |
| 1780 | audio_session_t currentSession = sessions.keyAt(j); |
| 1781 | stopInput(reusedInputDesc->mIoHandle, currentSession); |
| 1782 | releaseInput(reusedInputDesc->mIoHandle, currentSession); |
| 1783 | } |
| 1784 | } |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1785 | #endif |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1786 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1787 | if (!profile->canOpenNewIo()) { |
| 1788 | return AUDIO_IO_HANDLE_NONE; |
| 1789 | } |
| 1790 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1791 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1792 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1793 | audio_config_t lConfig = AUDIO_CONFIG_INITIALIZER; |
| 1794 | lConfig.sample_rate = profileSamplingRate; |
| 1795 | lConfig.channel_mask = profileChannelMask; |
| 1796 | lConfig.format = profileFormat; |
Eric Laurent | e301410 | 2017-05-03 11:15:43 -0700 | [diff] [blame] | 1797 | |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 1798 | if (address == "") { |
| 1799 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(device); |
| 1800 | // the inputs vector must be of size >= 1, but we don't want to crash here |
| 1801 | address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress : String8(""); |
| 1802 | } |
| 1803 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1804 | status_t status = inputDesc->open(&lConfig, device, address, |
| 1805 | halInputSource, profileFlags, &input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1806 | |
| 1807 | // only accept input with the exact requested set of parameters |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1808 | if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1809 | (profileSamplingRate != lConfig.sample_rate) || |
| 1810 | !audio_formats_match(profileFormat, lConfig.format) || |
| 1811 | (profileChannelMask != lConfig.channel_mask)) { |
| 1812 | ALOGW("getInputForAttr() failed opening input: sampling rate %d" |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1813 | ", format %#x, channel mask %#x", |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1814 | profileSamplingRate, profileFormat, profileChannelMask); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1815 | if (input != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1816 | inputDesc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1817 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1818 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1819 | } |
| 1820 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1821 | inputDesc->mPolicyMix = policyMix; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1822 | inputDesc->addAudioSession(session, audioSession); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1823 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1824 | addInput(input, inputDesc); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1825 | mpClientInterface->onAudioPortListUpdate(); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1826 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1827 | return input; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1828 | } |
| 1829 | |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1830 | //static |
| 1831 | bool AudioPolicyManager::isConcurrentSource(audio_source_t source) |
| 1832 | { |
| 1833 | return (source == AUDIO_SOURCE_HOTWORD) || |
| 1834 | (source == AUDIO_SOURCE_VOICE_RECOGNITION) || |
| 1835 | (source == AUDIO_SOURCE_FM_TUNER); |
| 1836 | } |
| 1837 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1838 | bool AudioPolicyManager::isConcurentCaptureAllowed(const sp<AudioInputDescriptor>& inputDesc, |
| 1839 | const sp<AudioSession>& audioSession) |
| 1840 | { |
| 1841 | // Do not allow capture if an active voice call is using a software patch and |
| 1842 | // the call TX source device is on the same HW module. |
| 1843 | // FIXME: would be better to refine to only inputs whose profile connects to the |
| 1844 | // call TX device but this information is not in the audio patch |
| 1845 | if (mCallTxPatch != 0 && |
| 1846 | inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) { |
| 1847 | return false; |
| 1848 | } |
| 1849 | |
| 1850 | // starting concurrent capture is enabled if: |
| 1851 | // 1) capturing for re-routing |
| 1852 | // 2) capturing for HOTWORD source |
| 1853 | // 3) capturing for FM TUNER source |
| 1854 | // 3) All other active captures are either for re-routing or HOTWORD |
| 1855 | |
| 1856 | if (is_virtual_input_device(inputDesc->mDevice) || |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1857 | isConcurrentSource(audioSession->inputSource())) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1858 | return true; |
| 1859 | } |
| 1860 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1861 | for (const auto& activeInput : mInputs.getActiveInputs()) { |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1862 | if (!isConcurrentSource(activeInput->inputSource(true)) && |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1863 | !is_virtual_input_device(activeInput->mDevice)) { |
| 1864 | return false; |
| 1865 | } |
| 1866 | } |
| 1867 | |
| 1868 | return true; |
| 1869 | } |
| 1870 | |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 1871 | // FIXME: remove when concurrent capture is ready. This is a hack to work around bug b/63083537. |
| 1872 | bool AudioPolicyManager::soundTriggerSupportsConcurrentCapture() { |
| 1873 | if (!mHasComputedSoundTriggerSupportsConcurrentCapture) { |
| 1874 | bool soundTriggerSupportsConcurrentCapture = false; |
| 1875 | unsigned int numModules = 0; |
| 1876 | struct sound_trigger_module_descriptor* nModules = NULL; |
| 1877 | |
| 1878 | status_t status = SoundTrigger::listModules(nModules, &numModules); |
| 1879 | if (status == NO_ERROR && numModules != 0) { |
| 1880 | nModules = (struct sound_trigger_module_descriptor*) calloc( |
| 1881 | numModules, sizeof(struct sound_trigger_module_descriptor)); |
| 1882 | if (nModules == NULL) { |
| 1883 | // We failed to malloc the buffer, so just say no for now, and hope that we have more |
| 1884 | // ram the next time this function is called. |
| 1885 | ALOGE("Failed to allocate buffer for module descriptors"); |
| 1886 | return false; |
| 1887 | } |
| 1888 | |
| 1889 | status = SoundTrigger::listModules(nModules, &numModules); |
| 1890 | if (status == NO_ERROR) { |
| 1891 | soundTriggerSupportsConcurrentCapture = true; |
| 1892 | for (size_t i = 0; i < numModules; ++i) { |
| 1893 | soundTriggerSupportsConcurrentCapture &= |
| 1894 | nModules[i].properties.concurrent_capture; |
| 1895 | } |
| 1896 | } |
| 1897 | free(nModules); |
| 1898 | } |
| 1899 | mSoundTriggerSupportsConcurrentCapture = soundTriggerSupportsConcurrentCapture; |
| 1900 | mHasComputedSoundTriggerSupportsConcurrentCapture = true; |
| 1901 | } |
| 1902 | return mSoundTriggerSupportsConcurrentCapture; |
| 1903 | } |
| 1904 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1905 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1906 | status_t AudioPolicyManager::startInput(audio_io_handle_t input, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1907 | audio_session_t session, |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1908 | bool silenced, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1909 | concurrency_type__mask_t *concurrency) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1910 | { |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1911 | |
| 1912 | ALOGV("AudioPolicyManager::startInput(input:%d, session:%d, silenced:%d, concurrency:%d)", |
| 1913 | input, session, silenced, *concurrency); |
| 1914 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1915 | *concurrency = API_INPUT_CONCURRENCY_NONE; |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1916 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1917 | ssize_t index = mInputs.indexOfKey(input); |
| 1918 | if (index < 0) { |
| 1919 | ALOGW("startInput() unknown input %d", input); |
| 1920 | return BAD_VALUE; |
| 1921 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1922 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1923 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1924 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
| 1925 | if (audioSession == 0) { |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1926 | ALOGW("startInput() unknown session %d on input %d", session, input); |
| 1927 | return BAD_VALUE; |
| 1928 | } |
| 1929 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1930 | // FIXME: disable concurrent capture until UI is ready |
| 1931 | #if 0 |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1932 | if (!isConcurentCaptureAllowed(inputDesc, audioSession)) { |
| 1933 | ALOGW("startInput(%d) failed: other input already started", input); |
| 1934 | return INVALID_OPERATION; |
| 1935 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1936 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1937 | if (isInCall()) { |
| 1938 | *concurrency |= API_INPUT_CONCURRENCY_CALL; |
| 1939 | } |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 1940 | if (mInputs.activeInputsCountOnDevices() != 0) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1941 | *concurrency |= API_INPUT_CONCURRENCY_CAPTURE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1942 | } |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1943 | #else |
| 1944 | if (!is_virtual_input_device(inputDesc->mDevice)) { |
| 1945 | if (mCallTxPatch != 0 && |
| 1946 | inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) { |
| 1947 | ALOGW("startInput(%d) failed: call in progress", input); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 1948 | *concurrency |= API_INPUT_CONCURRENCY_CALL; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1949 | return INVALID_OPERATION; |
| 1950 | } |
| 1951 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1952 | Vector<sp<AudioInputDescriptor>> activeInputs = mInputs.getActiveInputs(); |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1953 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1954 | // If a UID is idle and records silence and another not silenced recording starts |
| 1955 | // from another UID (idle or active) we stop the current idle UID recording in |
| 1956 | // favor of the new one - "There can be only one" TM |
| 1957 | if (!silenced) { |
| 1958 | for (const auto& activeDesc : activeInputs) { |
| 1959 | if ((audioSession->flags() & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0 && |
| 1960 | activeDesc->getId() == inputDesc->getId()) { |
| 1961 | continue; |
| 1962 | } |
| 1963 | |
| 1964 | AudioSessionCollection activeSessions = activeDesc->getAudioSessions( |
| 1965 | true /*activeOnly*/); |
| 1966 | sp<AudioSession> activeSession = activeSessions.valueAt(0); |
| 1967 | if (activeSession->isSilenced()) { |
| 1968 | audio_io_handle_t activeInput = activeDesc->mIoHandle; |
| 1969 | audio_session_t activeSessionId = activeSession->session(); |
| 1970 | stopInput(activeInput, activeSessionId); |
| 1971 | releaseInput(activeInput, activeSessionId); |
| 1972 | ALOGV("startInput(%d) stopping silenced input %d", input, activeInput); |
| 1973 | activeInputs = mInputs.getActiveInputs(); |
| 1974 | } |
| 1975 | } |
| 1976 | } |
| 1977 | |
| 1978 | for (const auto& activeDesc : activeInputs) { |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 1979 | if ((audioSession->flags() & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0 && |
| 1980 | activeDesc->getId() == inputDesc->getId()) { |
| 1981 | continue; |
| 1982 | } |
| 1983 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1984 | audio_source_t activeSource = activeDesc->inputSource(true); |
| 1985 | if (audioSession->inputSource() == AUDIO_SOURCE_HOTWORD) { |
| 1986 | if (activeSource == AUDIO_SOURCE_HOTWORD) { |
| 1987 | if (activeDesc->hasPreemptedSession(session)) { |
| 1988 | ALOGW("startInput(%d) failed for HOTWORD: " |
| 1989 | "other input %d already started for HOTWORD", |
| 1990 | input, activeDesc->mIoHandle); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 1991 | *concurrency |= API_INPUT_CONCURRENCY_HOTWORD; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1992 | return INVALID_OPERATION; |
| 1993 | } |
| 1994 | } else { |
| 1995 | ALOGV("startInput(%d) failed for HOTWORD: other input %d already started", |
| 1996 | input, activeDesc->mIoHandle); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 1997 | *concurrency |= API_INPUT_CONCURRENCY_CAPTURE; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1998 | return INVALID_OPERATION; |
| 1999 | } |
| 2000 | } else { |
| 2001 | if (activeSource != AUDIO_SOURCE_HOTWORD) { |
| 2002 | ALOGW("startInput(%d) failed: other input %d already started", |
| 2003 | input, activeDesc->mIoHandle); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 2004 | *concurrency |= API_INPUT_CONCURRENCY_CAPTURE; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2005 | return INVALID_OPERATION; |
| 2006 | } |
| 2007 | } |
| 2008 | } |
| 2009 | |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 2010 | // We only need to check if the sound trigger session supports concurrent capture if the |
| 2011 | // input is also a sound trigger input. Otherwise, we should preempt any hotword stream |
| 2012 | // that's running. |
| 2013 | const bool allowConcurrentWithSoundTrigger = |
| 2014 | inputDesc->isSoundTrigger() ? soundTriggerSupportsConcurrentCapture() : false; |
| 2015 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2016 | // if capture is allowed, preempt currently active HOTWORD captures |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2017 | for (const auto& activeDesc : activeInputs) { |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 2018 | if (allowConcurrentWithSoundTrigger && activeDesc->isSoundTrigger()) { |
| 2019 | continue; |
| 2020 | } |
| 2021 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2022 | audio_source_t activeSource = activeDesc->inputSource(true); |
| 2023 | if (activeSource == AUDIO_SOURCE_HOTWORD) { |
| 2024 | AudioSessionCollection activeSessions = |
| 2025 | activeDesc->getAudioSessions(true /*activeOnly*/); |
| 2026 | audio_session_t activeSession = activeSessions.keyAt(0); |
| 2027 | audio_io_handle_t activeHandle = activeDesc->mIoHandle; |
| 2028 | SortedVector<audio_session_t> sessions = activeDesc->getPreemptedSessions(); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 2029 | *concurrency |= API_INPUT_CONCURRENCY_PREEMPT; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2030 | sessions.add(activeSession); |
| 2031 | inputDesc->setPreemptedSessions(sessions); |
| 2032 | stopInput(activeHandle, activeSession); |
| 2033 | releaseInput(activeHandle, activeSession); |
| 2034 | ALOGV("startInput(%d) for HOTWORD preempting HOTWORD input %d", |
| 2035 | input, activeDesc->mIoHandle); |
| 2036 | } |
| 2037 | } |
| 2038 | } |
| 2039 | #endif |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2040 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 2041 | // Make sure we start with the correct silence state |
| 2042 | audioSession->setSilenced(silenced); |
| 2043 | |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 2044 | // increment activity count before calling getNewInputDevice() below as only active sessions |
| 2045 | // are considered for device selection |
| 2046 | audioSession->changeActiveCount(1); |
| 2047 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2048 | // Routing? |
| 2049 | mInputRoutes.incRouteActivity(session); |
| 2050 | |
Eric Laurent | 2157f5b | 2018-04-25 18:33:02 -0700 | [diff] [blame] | 2051 | if (audioSession->activeCount() == 1 || mInputRoutes.getAndClearRouteChanged(session)) { |
Eric Laurent | 271a93e | 2016-09-29 14:47:06 -0700 | [diff] [blame] | 2052 | // indicate active capture to sound trigger service if starting capture from a mic on |
| 2053 | // primary HW module |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 2054 | audio_devices_t device = getNewInputDevice(inputDesc); |
Eric Laurent | 271a93e | 2016-09-29 14:47:06 -0700 | [diff] [blame] | 2055 | setInputDevice(input, device, true /* force */); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2056 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2057 | status_t status = inputDesc->start(); |
| 2058 | if (status != NO_ERROR) { |
| 2059 | mInputRoutes.decRouteActivity(session); |
| 2060 | audioSession->changeActiveCount(-1); |
| 2061 | return status; |
| 2062 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2063 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2064 | if (inputDesc->getAudioSessionCount(true/*activeOnly*/) == 1) { |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2065 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 2066 | if ((inputDesc->mPolicyMix != NULL) |
| 2067 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 2068 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
| 2069 | MIX_STATE_MIXING); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2070 | } |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2071 | |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 2072 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); |
| 2073 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
Eric Laurent | 05b345e | 2016-11-18 12:36:27 -0800 | [diff] [blame] | 2074 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) { |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2075 | SoundTrigger::setCaptureState(true); |
| 2076 | } |
| 2077 | |
| 2078 | // automatically enable the remote submix output when input is started if not |
| 2079 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 2080 | // For remote submix (a virtual device), we open only one input per capture request. |
| 2081 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 2082 | String8 address = String8(""); |
| 2083 | if (inputDesc->mPolicyMix == NULL) { |
| 2084 | address = String8("0"); |
| 2085 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 2086 | address = inputDesc->mPolicyMix->mDeviceAddress; |
| 2087 | } |
| 2088 | if (address != "") { |
| 2089 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2090 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2091 | address, "remote-submix"); |
| 2092 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2093 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 2094 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2095 | } |
| 2096 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2097 | ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2098 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2099 | return NO_ERROR; |
| 2100 | } |
| 2101 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2102 | status_t AudioPolicyManager::stopInput(audio_io_handle_t input, |
| 2103 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2104 | { |
| 2105 | ALOGV("stopInput() input %d", input); |
| 2106 | ssize_t index = mInputs.indexOfKey(input); |
| 2107 | if (index < 0) { |
| 2108 | ALOGW("stopInput() unknown input %d", input); |
| 2109 | return BAD_VALUE; |
| 2110 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2111 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2112 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2113 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2114 | if (index < 0) { |
| 2115 | ALOGW("stopInput() unknown session %d on input %d", session, input); |
| 2116 | return BAD_VALUE; |
| 2117 | } |
| 2118 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2119 | if (audioSession->activeCount() == 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2120 | ALOGW("stopInput() input %d already stopped", input); |
| 2121 | return INVALID_OPERATION; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2122 | } |
| 2123 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2124 | audioSession->changeActiveCount(-1); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2125 | |
| 2126 | // Routing? |
| 2127 | mInputRoutes.decRouteActivity(session); |
| 2128 | |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2129 | if (audioSession->activeCount() == 0) { |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2130 | inputDesc->stop(); |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2131 | if (inputDesc->isActive()) { |
| 2132 | setInputDevice(input, getNewInputDevice(inputDesc), false /* force */); |
| 2133 | } else { |
| 2134 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 2135 | if ((inputDesc->mPolicyMix != NULL) |
| 2136 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 2137 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
| 2138 | MIX_STATE_IDLE); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2139 | } |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2140 | |
| 2141 | // automatically disable the remote submix output when input is stopped if not |
| 2142 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 2143 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 2144 | String8 address = String8(""); |
| 2145 | if (inputDesc->mPolicyMix == NULL) { |
| 2146 | address = String8("0"); |
| 2147 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 2148 | address = inputDesc->mPolicyMix->mDeviceAddress; |
| 2149 | } |
| 2150 | if (address != "") { |
| 2151 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2152 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2153 | address, "remote-submix"); |
| 2154 | } |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2155 | } |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2156 | |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 2157 | audio_devices_t device = inputDesc->mDevice; |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 2158 | resetInputDevice(input); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2159 | |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 2160 | // indicate inactive capture to sound trigger service if stopping capture from a mic on |
| 2161 | // primary HW module |
| 2162 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); |
| 2163 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
| 2164 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 2165 | SoundTrigger::setCaptureState(false); |
| 2166 | } |
| 2167 | inputDesc->clearPreemptedSessions(); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2168 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2169 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2170 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2171 | } |
| 2172 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2173 | void AudioPolicyManager::releaseInput(audio_io_handle_t input, |
| 2174 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2175 | { |
| 2176 | ALOGV("releaseInput() %d", input); |
| 2177 | ssize_t index = mInputs.indexOfKey(input); |
| 2178 | if (index < 0) { |
| 2179 | ALOGW("releaseInput() releasing unknown input %d", input); |
| 2180 | return; |
| 2181 | } |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2182 | |
| 2183 | // Routing |
| 2184 | mInputRoutes.removeRoute(session); |
| 2185 | |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2186 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
| 2187 | ALOG_ASSERT(inputDesc != 0); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2188 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2189 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
vivek mehta | 587b8df | 2017-01-31 14:58:44 -0800 | [diff] [blame] | 2190 | if (audioSession == 0) { |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2191 | ALOGW("releaseInput() unknown session %d on input %d", session, input); |
| 2192 | return; |
| 2193 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2194 | |
| 2195 | if (audioSession->openCount() == 0) { |
| 2196 | ALOGW("releaseInput() invalid open count %d on session %d", |
| 2197 | audioSession->openCount(), session); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2198 | return; |
| 2199 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2200 | |
| 2201 | if (audioSession->changeOpenCount(-1) == 0) { |
| 2202 | inputDesc->removeAudioSession(session); |
| 2203 | } |
| 2204 | |
Jean-Michel Trivi | 65bfe91 | 2015-12-04 15:56:47 -0800 | [diff] [blame] | 2205 | if (inputDesc->getOpenRefCount() > 0) { |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2206 | ALOGV("releaseInput() exit > 0"); |
| 2207 | return; |
| 2208 | } |
| 2209 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2210 | closeInput(input); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2211 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2212 | ALOGV("releaseInput() exit"); |
| 2213 | } |
| 2214 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2215 | void AudioPolicyManager::closeAllInputs() { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2216 | bool patchRemoved = false; |
| 2217 | |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 2218 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2219 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index); |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 2220 | ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2221 | if (patch_index >= 0) { |
| 2222 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 2223 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2224 | mAudioPatches.removeItemsAt(patch_index); |
| 2225 | patchRemoved = true; |
| 2226 | } |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2227 | inputDesc->close(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2228 | } |
jiabin | 829a00b | 2018-04-04 16:28:32 -0700 | [diff] [blame] | 2229 | mInputRoutes.clear(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2230 | mInputs.clear(); |
Chris Thornton | 52785f3 | 2016-02-09 17:28:49 -0800 | [diff] [blame] | 2231 | SoundTrigger::setCaptureState(false); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2232 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2233 | |
| 2234 | if (patchRemoved) { |
| 2235 | mpClientInterface->onAudioPatchListUpdate(); |
| 2236 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2237 | } |
| 2238 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2239 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2240 | int indexMin, |
| 2241 | int indexMax) |
| 2242 | { |
| 2243 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2244 | mVolumeCurves->initStreamVolume(stream, indexMin, indexMax); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2245 | |
| 2246 | // initialize other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2247 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2248 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2249 | continue; |
| 2250 | } |
| 2251 | mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2252 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2253 | } |
| 2254 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2255 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2256 | int index, |
| 2257 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2258 | { |
| 2259 | |
Tomoharu Kasahara | 63c1550 | 2019-01-23 12:42:04 +0900 | [diff] [blame] | 2260 | // VOICE_CALL and BLUETOOTH_SCO stream have minVolumeIndex > 0 but |
| 2261 | // can be muted directly by an app that has MODIFY_PHONE_STATE permission. |
Nadav Bar | 7c605f6 | 2017-12-25 00:01:52 +0200 | [diff] [blame] | 2262 | if (((index < mVolumeCurves->getVolumeIndexMin(stream)) && |
Tomoharu Kasahara | 63c1550 | 2019-01-23 12:42:04 +0900 | [diff] [blame] | 2263 | !((stream == AUDIO_STREAM_VOICE_CALL || stream == AUDIO_STREAM_BLUETOOTH_SCO) && |
| 2264 | index == 0)) || |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2265 | (index > mVolumeCurves->getVolumeIndexMax(stream))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2266 | return BAD_VALUE; |
| 2267 | } |
| 2268 | if (!audio_is_output_device(device)) { |
| 2269 | return BAD_VALUE; |
| 2270 | } |
| 2271 | |
| 2272 | // Force max volume if stream cannot be muted |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2273 | if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2274 | |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2275 | ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2276 | stream, device, index); |
| 2277 | |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2278 | // update other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2279 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2280 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2281 | continue; |
| 2282 | } |
| 2283 | mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index); |
| 2284 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2285 | |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2286 | // update volume on all outputs and streams matching the following: |
| 2287 | // - The requested stream (or a stream matching for volume control) is active on the output |
| 2288 | // - The device (or devices) selected by the strategy corresponding to this stream includes |
| 2289 | // the requested device |
| 2290 | // - For non default requested device, currently selected device on the output is either the |
| 2291 | // requested device or one of the devices selected by the strategy |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2292 | // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if |
| 2293 | // no specific device volume value exists for currently selected device. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2294 | status_t status = NO_ERROR; |
| 2295 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2296 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
nobuaki tanaka | 985d15a | 2017-07-19 13:38:51 +0900 | [diff] [blame] | 2297 | audio_devices_t curDevice = desc->device(); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2298 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2299 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2300 | continue; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2301 | } |
Eric Laurent | d3926fe | 2016-04-15 18:10:07 -0700 | [diff] [blame] | 2302 | if (!(desc->isStreamActive((audio_stream_type_t)curStream) || |
| 2303 | (isInCall() && (curStream == AUDIO_STREAM_VOICE_CALL)))) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2304 | continue; |
| 2305 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2306 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Jean-Michel Trivi | b7fdce6 | 2017-06-27 19:38:42 -0700 | [diff] [blame] | 2307 | audio_devices_t curStreamDevice = Volume::getDeviceForVolume(getDeviceForStrategy( |
| 2308 | curStrategy, false /*fromCache*/)); |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2309 | if ((device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) && |
| 2310 | ((curStreamDevice & device) == 0)) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2311 | continue; |
| 2312 | } |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2313 | bool applyVolume; |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2314 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2315 | curStreamDevice |= device; |
nobuaki tanaka | 985d15a | 2017-07-19 13:38:51 +0900 | [diff] [blame] | 2316 | applyVolume = (Volume::getDeviceForVolume(curDevice) & curStreamDevice) != 0; |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2317 | } else { |
| 2318 | applyVolume = !mVolumeCurves->hasVolumeIndexForDevice( |
Jean-Michel Trivi | b7fdce6 | 2017-06-27 19:38:42 -0700 | [diff] [blame] | 2319 | stream, curStreamDevice); |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2320 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2321 | |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2322 | if (applyVolume) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2323 | //FIXME: workaround for truncated touch sounds |
| 2324 | // delayed volume change for system stream to be removed when the problem is |
| 2325 | // handled by system UI |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2326 | status_t volStatus = |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2327 | checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice, |
| 2328 | (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2329 | if (volStatus != NO_ERROR) { |
| 2330 | status = volStatus; |
| 2331 | } |
| 2332 | } |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2333 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2334 | } |
| 2335 | return status; |
| 2336 | } |
| 2337 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2338 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2339 | int *index, |
| 2340 | audio_devices_t device) |
| 2341 | { |
| 2342 | if (index == NULL) { |
| 2343 | return BAD_VALUE; |
| 2344 | } |
| 2345 | if (!audio_is_output_device(device)) { |
| 2346 | return BAD_VALUE; |
| 2347 | } |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2348 | // 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] | 2349 | // the strategy the stream belongs to. |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2350 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2351 | device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); |
| 2352 | } |
François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 2353 | device = Volume::getDeviceForVolume(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2354 | |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2355 | *index = mVolumeCurves->getVolumeIndex(stream, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2356 | ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index); |
| 2357 | return NO_ERROR; |
| 2358 | } |
| 2359 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2360 | audio_io_handle_t AudioPolicyManager::selectOutputForMusicEffects() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2361 | { |
| 2362 | // select one output among several suitable for global effects. |
| 2363 | // The priority is as follows: |
| 2364 | // 1: An offloaded output. If the effect ends up not being offloadable, |
| 2365 | // AudioFlinger will invalidate the track and the offloaded output |
| 2366 | // will be closed causing the effect to be moved to a PCM output. |
| 2367 | // 2: A deep buffer output |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2368 | // 3: The primary output |
| 2369 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2370 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2371 | routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2372 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2373 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2374 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2375 | if (outputs.size() == 0) { |
| 2376 | return AUDIO_IO_HANDLE_NONE; |
| 2377 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2378 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2379 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 2380 | bool activeOnly = true; |
| 2381 | |
| 2382 | while (output == AUDIO_IO_HANDLE_NONE) { |
| 2383 | audio_io_handle_t outputOffloaded = AUDIO_IO_HANDLE_NONE; |
| 2384 | audio_io_handle_t outputDeepBuffer = AUDIO_IO_HANDLE_NONE; |
| 2385 | audio_io_handle_t outputPrimary = AUDIO_IO_HANDLE_NONE; |
| 2386 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2387 | for (audio_io_handle_t output : outputs) { |
| 2388 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2389 | if (activeOnly && !desc->isStreamActive(AUDIO_STREAM_MUSIC)) { |
| 2390 | continue; |
| 2391 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2392 | ALOGV("selectOutputForMusicEffects activeOnly %d output %d flags 0x%08x", |
| 2393 | activeOnly, output, desc->mFlags); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2394 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2395 | outputOffloaded = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2396 | } |
| 2397 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2398 | outputDeepBuffer = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2399 | } |
| 2400 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2401 | outputPrimary = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2402 | } |
| 2403 | } |
| 2404 | if (outputOffloaded != AUDIO_IO_HANDLE_NONE) { |
| 2405 | output = outputOffloaded; |
| 2406 | } else if (outputDeepBuffer != AUDIO_IO_HANDLE_NONE) { |
| 2407 | output = outputDeepBuffer; |
| 2408 | } else if (outputPrimary != AUDIO_IO_HANDLE_NONE) { |
| 2409 | output = outputPrimary; |
| 2410 | } else { |
| 2411 | output = outputs[0]; |
| 2412 | } |
| 2413 | activeOnly = false; |
| 2414 | } |
| 2415 | |
| 2416 | if (output != mMusicEffectOutput) { |
| 2417 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output); |
| 2418 | mMusicEffectOutput = output; |
| 2419 | } |
| 2420 | |
| 2421 | ALOGV("selectOutputForMusicEffects selected output %d", output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2422 | return output; |
| 2423 | } |
| 2424 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2425 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc __unused) |
| 2426 | { |
| 2427 | return selectOutputForMusicEffects(); |
| 2428 | } |
| 2429 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2430 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2431 | audio_io_handle_t io, |
| 2432 | uint32_t strategy, |
| 2433 | int session, |
| 2434 | int id) |
| 2435 | { |
| 2436 | ssize_t index = mOutputs.indexOfKey(io); |
| 2437 | if (index < 0) { |
| 2438 | index = mInputs.indexOfKey(io); |
| 2439 | if (index < 0) { |
| 2440 | ALOGW("registerEffect() unknown io %d", io); |
| 2441 | return INVALID_OPERATION; |
| 2442 | } |
| 2443 | } |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2444 | return mEffects.registerEffect(desc, io, strategy, session, id); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2445 | } |
| 2446 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2447 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2448 | { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2449 | bool active = false; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2450 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) { |
| 2451 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2452 | continue; |
| 2453 | } |
| 2454 | active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs); |
| 2455 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2456 | return active; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2457 | } |
| 2458 | |
| 2459 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2460 | { |
| 2461 | return mOutputs.isStreamActiveRemotely(stream, inPastMs); |
| 2462 | } |
| 2463 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2464 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2465 | { |
| 2466 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2467 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2468 | if (inputDescriptor->isSourceActive(source)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2469 | return true; |
| 2470 | } |
| 2471 | } |
| 2472 | return false; |
| 2473 | } |
| 2474 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2475 | // Register a list of custom mixes with their attributes and format. |
| 2476 | // When a mix is registered, corresponding input and output profiles are |
| 2477 | // added to the remote submix hw module. The profile contains only the |
| 2478 | // parameters (sampling rate, format...) specified by the mix. |
| 2479 | // The corresponding input remote submix device is also connected. |
| 2480 | // |
| 2481 | // When a remote submix device is connected, the address is checked to select the |
| 2482 | // appropriate profile and the corresponding input or output stream is opened. |
| 2483 | // |
| 2484 | // When capture starts, getInputForAttr() will: |
| 2485 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2486 | // - 2 if none found, getDeviceForInputSource() will: |
| 2487 | // - 2.1 look for a mix matching the attributes source |
| 2488 | // - 2.2 if none found, default to device selection by policy rules |
| 2489 | // At this time, the corresponding output remote submix device is also connected |
| 2490 | // and active playback use cases can be transferred to this mix if needed when reconnecting |
| 2491 | // after AudioTracks are invalidated |
| 2492 | // |
| 2493 | // When playback starts, getOutputForAttr() will: |
| 2494 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2495 | // - 2 if none found, look for a mix matching the attributes usage |
| 2496 | // - 3 if none found, default to device and output selection by policy rules. |
| 2497 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 2498 | status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes) |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2499 | { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2500 | ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size()); |
| 2501 | status_t res = NO_ERROR; |
| 2502 | |
| 2503 | sp<HwModule> rSubmixModule; |
| 2504 | // examine each mix's route type |
| 2505 | for (size_t i = 0; i < mixes.size(); i++) { |
| 2506 | // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination |
| 2507 | if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) { |
| 2508 | res = INVALID_OPERATION; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2509 | break; |
| 2510 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2511 | 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] | 2512 | 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] | 2513 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 2514 | rSubmixModule = mHwModules.getModuleFromName( |
| 2515 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 2516 | if (rSubmixModule == 0) { |
| 2517 | ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", |
| 2518 | i); |
| 2519 | res = INVALID_OPERATION; |
| 2520 | break; |
| 2521 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2522 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2523 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2524 | String8 address = mixes[i].mDeviceAddress; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2525 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2526 | if (mPolicyMixes.registerMix(address, mixes[i], 0 /*output desc*/) != NO_ERROR) { |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2527 | ALOGE(" Error registering mix %zu for address %s", i, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2528 | res = INVALID_OPERATION; |
| 2529 | break; |
| 2530 | } |
| 2531 | audio_config_t outputConfig = mixes[i].mFormat; |
| 2532 | audio_config_t inputConfig = mixes[i].mFormat; |
| 2533 | // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in |
| 2534 | // stereo and let audio flinger do the channel conversion if needed. |
| 2535 | outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 2536 | inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
| 2537 | rSubmixModule->addOutputProfile(address, &outputConfig, |
| 2538 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address); |
| 2539 | rSubmixModule->addInputProfile(address, &inputConfig, |
| 2540 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, address); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2541 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2542 | if (mixes[i].mMixType == MIX_TYPE_PLAYERS) { |
| 2543 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2544 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2545 | address.string(), "remote-submix"); |
| 2546 | } else { |
| 2547 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2548 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2549 | address.string(), "remote-submix"); |
| 2550 | } |
| 2551 | } 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] | 2552 | String8 address = mixes[i].mDeviceAddress; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2553 | audio_devices_t device = mixes[i].mDeviceType; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2554 | ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s", |
| 2555 | i, mixes.size(), device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2556 | |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2557 | bool foundOutput = false; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2558 | for (size_t j = 0 ; j < mOutputs.size() ; j++) { |
| 2559 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j); |
| 2560 | sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle()); |
| 2561 | if ((patch != 0) && (patch->mPatch.num_sinks != 0) |
| 2562 | && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE) |
| 2563 | && (patch->mPatch.sinks[0].ext.device.type == device) |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2564 | && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(), |
| 2565 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2566 | if (mPolicyMixes.registerMix(address, mixes[i], desc) != NO_ERROR) { |
| 2567 | res = INVALID_OPERATION; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2568 | } else { |
| 2569 | foundOutput = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2570 | } |
| 2571 | break; |
| 2572 | } |
| 2573 | } |
| 2574 | |
| 2575 | if (res != NO_ERROR) { |
| 2576 | ALOGE(" Error registering mix %zu for device 0x%X addr %s", |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2577 | i, device, address.string()); |
| 2578 | res = INVALID_OPERATION; |
| 2579 | break; |
| 2580 | } else if (!foundOutput) { |
| 2581 | ALOGE(" Output not found for mix %zu for device 0x%X addr %s", |
| 2582 | i, device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2583 | res = INVALID_OPERATION; |
| 2584 | break; |
| 2585 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2586 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2587 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2588 | if (res != NO_ERROR) { |
| 2589 | unregisterPolicyMixes(mixes); |
| 2590 | } |
| 2591 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2592 | } |
| 2593 | |
| 2594 | status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes) |
| 2595 | { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2596 | ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2597 | status_t res = NO_ERROR; |
| 2598 | sp<HwModule> rSubmixModule; |
| 2599 | // examine each mix's route type |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2600 | for (const auto& mix : mixes) { |
| 2601 | 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] | 2602 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2603 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 2604 | rSubmixModule = mHwModules.getModuleFromName( |
| 2605 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 2606 | if (rSubmixModule == 0) { |
| 2607 | res = INVALID_OPERATION; |
| 2608 | continue; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2609 | } |
| 2610 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2611 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2612 | String8 address = mix.mDeviceAddress; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2613 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2614 | if (mPolicyMixes.unregisterMix(address) != NO_ERROR) { |
| 2615 | res = INVALID_OPERATION; |
| 2616 | continue; |
| 2617 | } |
| 2618 | |
| 2619 | if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) == |
| 2620 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2621 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2622 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2623 | address.string(), "remote-submix"); |
| 2624 | } |
| 2625 | if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) == |
| 2626 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2627 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2628 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2629 | address.string(), "remote-submix"); |
| 2630 | } |
| 2631 | rSubmixModule->removeOutputProfile(address); |
| 2632 | rSubmixModule->removeInputProfile(address); |
| 2633 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2634 | } if ((mix.mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 2635 | if (mPolicyMixes.unregisterMix(mix.mDeviceAddress) != NO_ERROR) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2636 | res = INVALID_OPERATION; |
| 2637 | continue; |
| 2638 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2639 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2640 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2641 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2642 | } |
| 2643 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2644 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2645 | status_t AudioPolicyManager::dump(int fd) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2646 | { |
| 2647 | const size_t SIZE = 256; |
| 2648 | char buffer[SIZE]; |
| 2649 | String8 result; |
| 2650 | |
| 2651 | snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this); |
| 2652 | result.append(buffer); |
| 2653 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 2654 | snprintf(buffer, SIZE, " Primary Output: %d\n", |
| 2655 | hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2656 | result.append(buffer); |
Mikhail Naganov | 0d6a033 | 2016-04-19 17:12:38 -0700 | [diff] [blame] | 2657 | std::string stateLiteral; |
| 2658 | AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral); |
| 2659 | snprintf(buffer, SIZE, " Phone state: %s\n", stateLiteral.c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2660 | result.append(buffer); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2661 | snprintf(buffer, SIZE, " Force use for communications %d\n", |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2662 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2663 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2664 | snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2665 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2666 | snprintf(buffer, SIZE, " Force use for record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2667 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2668 | snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2669 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2670 | snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2671 | result.append(buffer); |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 2672 | snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n", |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2673 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO)); |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 2674 | result.append(buffer); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 2675 | snprintf(buffer, SIZE, " Force use for encoded surround output %d\n", |
| 2676 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND)); |
| 2677 | result.append(buffer); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 2678 | snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available"); |
| 2679 | result.append(buffer); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 2680 | snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off"); |
| 2681 | result.append(buffer); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 2682 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2683 | write(fd, result.string(), result.size()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2684 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 2685 | mAvailableOutputDevices.dump(fd, String8("Available output")); |
| 2686 | mAvailableInputDevices.dump(fd, String8("Available input")); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 2687 | mHwModulesAll.dump(fd); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2688 | mOutputs.dump(fd); |
| 2689 | mInputs.dump(fd); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2690 | mVolumeCurves->dump(fd); |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2691 | mEffects.dump(fd); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2692 | mAudioPatches.dump(fd); |
Mikhail Naganov | 44344b0 | 2016-12-13 11:21:02 -0800 | [diff] [blame] | 2693 | mPolicyMixes.dump(fd); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2694 | |
| 2695 | return NO_ERROR; |
| 2696 | } |
| 2697 | |
| 2698 | // This function checks for the parameters which can be offloaded. |
| 2699 | // This can be enhanced depending on the capability of the DSP and policy |
| 2700 | // of the system. |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2701 | bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2702 | { |
| 2703 | ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2704 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2705 | offloadInfo.sample_rate, offloadInfo.channel_mask, |
| 2706 | offloadInfo.format, |
| 2707 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, |
| 2708 | offloadInfo.has_video); |
| 2709 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 2710 | if (mMasterMono) { |
| 2711 | return false; // no offloading if mono is set. |
| 2712 | } |
| 2713 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2714 | // Check if offload has been disabled |
| 2715 | char propValue[PROPERTY_VALUE_MAX]; |
| 2716 | if (property_get("audio.offload.disable", propValue, "0")) { |
| 2717 | if (atoi(propValue) != 0) { |
| 2718 | ALOGV("offload disabled by audio.offload.disable=%s", propValue ); |
| 2719 | return false; |
| 2720 | } |
| 2721 | } |
| 2722 | |
| 2723 | // Check if stream type is music, then only allow offload as of now. |
| 2724 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) |
| 2725 | { |
| 2726 | ALOGV("isOffloadSupported: stream_type != MUSIC, returning false"); |
| 2727 | return false; |
| 2728 | } |
| 2729 | |
| 2730 | //TODO: enable audio offloading with video when ready |
Andy Hung | 08945c4 | 2015-05-31 21:36:46 -0700 | [diff] [blame] | 2731 | const bool allowOffloadWithVideo = |
| 2732 | property_get_bool("audio.offload.video", false /* default_value */); |
| 2733 | if (offloadInfo.has_video && !allowOffloadWithVideo) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2734 | ALOGV("isOffloadSupported: has_video == true, returning false"); |
| 2735 | return false; |
| 2736 | } |
| 2737 | |
| 2738 | //If duration is less than minimum value defined in property, return false |
| 2739 | if (property_get("audio.offload.min.duration.secs", propValue, NULL)) { |
| 2740 | if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) { |
| 2741 | ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue); |
| 2742 | return false; |
| 2743 | } |
| 2744 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { |
| 2745 | ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS); |
| 2746 | return false; |
| 2747 | } |
| 2748 | |
| 2749 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 2750 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 2751 | // detects there is an active non offloadable effect. |
| 2752 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 2753 | // This may prevent offloading in rare situations where effects are left active by apps |
| 2754 | // in the background. |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2755 | if (mEffects.isNonOffloadableEffectEnabled()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2756 | return false; |
| 2757 | } |
| 2758 | |
| 2759 | // See if there is a profile to support this. |
| 2760 | // AUDIO_DEVICE_NONE |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2761 | sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2762 | offloadInfo.sample_rate, |
| 2763 | offloadInfo.format, |
| 2764 | offloadInfo.channel_mask, |
| 2765 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2766 | ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT "); |
| 2767 | return (profile != 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2768 | } |
| 2769 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2770 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, |
| 2771 | audio_port_type_t type, |
| 2772 | unsigned int *num_ports, |
| 2773 | struct audio_port *ports, |
| 2774 | unsigned int *generation) |
| 2775 | { |
| 2776 | if (num_ports == NULL || (*num_ports != 0 && ports == NULL) || |
| 2777 | generation == NULL) { |
| 2778 | return BAD_VALUE; |
| 2779 | } |
| 2780 | ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports); |
| 2781 | if (ports == NULL) { |
| 2782 | *num_ports = 0; |
| 2783 | } |
| 2784 | |
| 2785 | size_t portsWritten = 0; |
| 2786 | size_t portsMax = *num_ports; |
| 2787 | *num_ports = 0; |
| 2788 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2789 | // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB |
| 2790 | // as they are used by stub HALs by convention |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2791 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2792 | for (const auto& dev : mAvailableOutputDevices) { |
| 2793 | if (dev->type() == AUDIO_DEVICE_OUT_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2794 | continue; |
| 2795 | } |
| 2796 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2797 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2798 | } |
| 2799 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2800 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2801 | } |
| 2802 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2803 | for (const auto& dev : mAvailableInputDevices) { |
| 2804 | if (dev->type() == AUDIO_DEVICE_IN_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2805 | continue; |
| 2806 | } |
| 2807 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2808 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2809 | } |
| 2810 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2811 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2812 | } |
| 2813 | } |
| 2814 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { |
| 2815 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 2816 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { |
| 2817 | mInputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2818 | } |
| 2819 | *num_ports += mInputs.size(); |
| 2820 | } |
| 2821 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2822 | size_t numOutputs = 0; |
| 2823 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2824 | if (!mOutputs[i]->isDuplicated()) { |
| 2825 | numOutputs++; |
| 2826 | if (portsWritten < portsMax) { |
| 2827 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2828 | } |
| 2829 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2830 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2831 | *num_ports += numOutputs; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2832 | } |
| 2833 | } |
| 2834 | *generation = curAudioPortGeneration(); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2835 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2836 | return NO_ERROR; |
| 2837 | } |
| 2838 | |
Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 2839 | status_t AudioPolicyManager::getAudioPort(struct audio_port *port) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2840 | { |
Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 2841 | if (port == nullptr || port->id == AUDIO_PORT_HANDLE_NONE) { |
| 2842 | return BAD_VALUE; |
| 2843 | } |
| 2844 | sp<DeviceDescriptor> dev = mAvailableOutputDevices.getDeviceFromId(port->id); |
| 2845 | if (dev != 0) { |
| 2846 | dev->toAudioPort(port); |
| 2847 | return NO_ERROR; |
| 2848 | } |
| 2849 | dev = mAvailableInputDevices.getDeviceFromId(port->id); |
| 2850 | if (dev != 0) { |
| 2851 | dev->toAudioPort(port); |
| 2852 | return NO_ERROR; |
| 2853 | } |
| 2854 | sp<SwAudioOutputDescriptor> out = mOutputs.getOutputFromId(port->id); |
| 2855 | if (out != 0) { |
| 2856 | out->toAudioPort(port); |
| 2857 | return NO_ERROR; |
| 2858 | } |
| 2859 | sp<AudioInputDescriptor> in = mInputs.getInputFromId(port->id); |
| 2860 | if (in != 0) { |
| 2861 | in->toAudioPort(port); |
| 2862 | return NO_ERROR; |
| 2863 | } |
| 2864 | return BAD_VALUE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2865 | } |
| 2866 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2867 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, |
| 2868 | audio_patch_handle_t *handle, |
| 2869 | uid_t uid) |
| 2870 | { |
| 2871 | ALOGV("createAudioPatch()"); |
| 2872 | |
| 2873 | if (handle == NULL || patch == NULL) { |
| 2874 | return BAD_VALUE; |
| 2875 | } |
| 2876 | ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks); |
| 2877 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2878 | if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX || |
| 2879 | patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) { |
| 2880 | return BAD_VALUE; |
| 2881 | } |
| 2882 | // only one source per audio patch supported for now |
| 2883 | if (patch->num_sources > 1) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2884 | return INVALID_OPERATION; |
| 2885 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2886 | |
| 2887 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2888 | return INVALID_OPERATION; |
| 2889 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2890 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2891 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { |
| 2892 | return INVALID_OPERATION; |
| 2893 | } |
| 2894 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2895 | |
| 2896 | sp<AudioPatch> patchDesc; |
| 2897 | ssize_t index = mAudioPatches.indexOfKey(*handle); |
| 2898 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2899 | ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id, |
| 2900 | patch->sources[0].role, |
| 2901 | patch->sources[0].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2902 | #if LOG_NDEBUG == 0 |
| 2903 | for (size_t i = 0; i < patch->num_sinks; i++) { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2904 | 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] | 2905 | patch->sinks[i].role, |
| 2906 | patch->sinks[i].type); |
| 2907 | } |
| 2908 | #endif |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2909 | |
| 2910 | if (index >= 0) { |
| 2911 | patchDesc = mAudioPatches.valueAt(index); |
| 2912 | ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 2913 | mUidCached, patchDesc->mUid, uid); |
| 2914 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 2915 | return INVALID_OPERATION; |
| 2916 | } |
| 2917 | } else { |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 2918 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2919 | } |
| 2920 | |
| 2921 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2922 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2923 | if (outputDesc == NULL) { |
| 2924 | ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id); |
| 2925 | return BAD_VALUE; |
| 2926 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2927 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", |
| 2928 | outputDesc->mIoHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2929 | if (patchDesc != 0) { |
| 2930 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
| 2931 | ALOGV("createAudioPatch() source id differs for patch current id %d new id %d", |
| 2932 | patchDesc->mPatch.sources[0].id, patch->sources[0].id); |
| 2933 | return BAD_VALUE; |
| 2934 | } |
| 2935 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2936 | DeviceVector devices; |
| 2937 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2938 | // Only support mix to devices connection |
| 2939 | // TODO add support for mix to mix connection |
| 2940 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 2941 | ALOGV("createAudioPatch() source mix but sink is not a device"); |
| 2942 | return INVALID_OPERATION; |
| 2943 | } |
| 2944 | sp<DeviceDescriptor> devDesc = |
| 2945 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 2946 | if (devDesc == 0) { |
| 2947 | ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id); |
| 2948 | return BAD_VALUE; |
| 2949 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2950 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2951 | if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2952 | devDesc->mAddress, |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2953 | patch->sources[0].sample_rate, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2954 | NULL, // updatedSamplingRate |
| 2955 | patch->sources[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2956 | NULL, // updatedFormat |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2957 | patch->sources[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2958 | NULL, // updatedChannelMask |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2959 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2960 | ALOGV("createAudioPatch() profile not supported for device %08x", |
| 2961 | devDesc->type()); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2962 | return INVALID_OPERATION; |
| 2963 | } |
| 2964 | devices.add(devDesc); |
| 2965 | } |
| 2966 | if (devices.size() == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2967 | return INVALID_OPERATION; |
| 2968 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2969 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2970 | // TODO: reconfigure output format and channels here |
| 2971 | ALOGV("createAudioPatch() setting device %08x on output %d", |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2972 | devices.types(), outputDesc->mIoHandle); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2973 | setOutputDevice(outputDesc, devices.types(), true, 0, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2974 | index = mAudioPatches.indexOfKey(*handle); |
| 2975 | if (index >= 0) { |
| 2976 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 2977 | ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided"); |
| 2978 | } |
| 2979 | patchDesc = mAudioPatches.valueAt(index); |
| 2980 | patchDesc->mUid = uid; |
| 2981 | ALOGV("createAudioPatch() success"); |
| 2982 | } else { |
| 2983 | ALOGW("createAudioPatch() setOutputDevice() failed to create a patch"); |
| 2984 | return INVALID_OPERATION; |
| 2985 | } |
| 2986 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2987 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 2988 | // input device to input mix connection |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2989 | // only one sink supported when connecting an input device to a mix |
| 2990 | if (patch->num_sinks > 1) { |
| 2991 | return INVALID_OPERATION; |
| 2992 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2993 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2994 | if (inputDesc == NULL) { |
| 2995 | return BAD_VALUE; |
| 2996 | } |
| 2997 | if (patchDesc != 0) { |
| 2998 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { |
| 2999 | return BAD_VALUE; |
| 3000 | } |
| 3001 | } |
| 3002 | sp<DeviceDescriptor> devDesc = |
| 3003 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
| 3004 | if (devDesc == 0) { |
| 3005 | return BAD_VALUE; |
| 3006 | } |
| 3007 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3008 | if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3009 | devDesc->mAddress, |
| 3010 | patch->sinks[0].sample_rate, |
| 3011 | NULL, /*updatedSampleRate*/ |
| 3012 | patch->sinks[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3013 | NULL, /*updatedFormat*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3014 | patch->sinks[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3015 | NULL, /*updatedChannelMask*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3016 | // FIXME for the parameter type, |
| 3017 | // and the NONE |
| 3018 | (audio_output_flags_t) |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 3019 | AUDIO_INPUT_FLAG_NONE)) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3020 | return INVALID_OPERATION; |
| 3021 | } |
| 3022 | // TODO: reconfigure output format and channels here |
| 3023 | ALOGV("createAudioPatch() setting device %08x on output %d", |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3024 | devDesc->type(), inputDesc->mIoHandle); |
| 3025 | setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3026 | index = mAudioPatches.indexOfKey(*handle); |
| 3027 | if (index >= 0) { |
| 3028 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 3029 | ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided"); |
| 3030 | } |
| 3031 | patchDesc = mAudioPatches.valueAt(index); |
| 3032 | patchDesc->mUid = uid; |
| 3033 | ALOGV("createAudioPatch() success"); |
| 3034 | } else { |
| 3035 | ALOGW("createAudioPatch() setInputDevice() failed to create a patch"); |
| 3036 | return INVALID_OPERATION; |
| 3037 | } |
| 3038 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 3039 | // device to device connection |
| 3040 | if (patchDesc != 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3041 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3042 | return BAD_VALUE; |
| 3043 | } |
| 3044 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3045 | sp<DeviceDescriptor> srcDeviceDesc = |
| 3046 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 3047 | if (srcDeviceDesc == 0) { |
| 3048 | return BAD_VALUE; |
| 3049 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3050 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3051 | //update source and sink with our own data as the data passed in the patch may |
| 3052 | // be incomplete. |
| 3053 | struct audio_patch newPatch = *patch; |
| 3054 | srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3055 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3056 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 3057 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 3058 | ALOGV("createAudioPatch() source device but one sink is not a device"); |
| 3059 | return INVALID_OPERATION; |
| 3060 | } |
| 3061 | |
| 3062 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 3063 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 3064 | if (sinkDeviceDesc == 0) { |
| 3065 | return BAD_VALUE; |
| 3066 | } |
| 3067 | sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]); |
| 3068 | |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3069 | // create a software bridge in PatchPanel if: |
Scott Randolph | f317240 | 2018-01-23 17:06:53 -0800 | [diff] [blame] | 3070 | // - source and sink devices are on different HW modules OR |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3071 | // - audio HAL version is < 3.0 |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 3072 | if (!srcDeviceDesc->hasSameHwModuleAs(sinkDeviceDesc) || |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 3073 | (srcDeviceDesc->mModule->getHalVersionMajor() < 3)) { |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3074 | // support only one sink device for now to simplify output selection logic |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3075 | if (patch->num_sinks > 1) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3076 | return INVALID_OPERATION; |
| 3077 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3078 | SortedVector<audio_io_handle_t> outputs = |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3079 | getOutputsForDevice(sinkDeviceDesc->type(), mOutputs); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3080 | // if the sink device is reachable via an opened output stream, request to go via |
| 3081 | // this output stream by adding a second source to the patch description |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 3082 | audio_io_handle_t output = selectOutput(outputs, |
| 3083 | AUDIO_OUTPUT_FLAG_NONE, |
| 3084 | AUDIO_FORMAT_INVALID); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3085 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 3086 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 3087 | if (outputDesc->isDuplicated()) { |
| 3088 | return INVALID_OPERATION; |
| 3089 | } |
| 3090 | outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3091 | newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3092 | newPatch.num_sources = 2; |
| 3093 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3094 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3095 | } |
| 3096 | // TODO: check from routing capabilities in config file and other conflicting patches |
| 3097 | |
| 3098 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 3099 | if (index >= 0) { |
| 3100 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 3101 | } |
| 3102 | |
| 3103 | status_t status = mpClientInterface->createAudioPatch(&newPatch, |
| 3104 | &afPatchHandle, |
| 3105 | 0); |
| 3106 | ALOGV("createAudioPatch() patch panel returned %d patchHandle %d", |
| 3107 | status, afPatchHandle); |
| 3108 | if (status == NO_ERROR) { |
| 3109 | if (index < 0) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 3110 | patchDesc = new AudioPatch(&newPatch, uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3111 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 3112 | } else { |
| 3113 | patchDesc->mPatch = newPatch; |
| 3114 | } |
| 3115 | patchDesc->mAfPatchHandle = afPatchHandle; |
| 3116 | *handle = patchDesc->mHandle; |
| 3117 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 3118 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3119 | } else { |
| 3120 | ALOGW("createAudioPatch() patch panel could not connect device patch, error %d", |
| 3121 | status); |
| 3122 | return INVALID_OPERATION; |
| 3123 | } |
| 3124 | } else { |
| 3125 | return BAD_VALUE; |
| 3126 | } |
| 3127 | } else { |
| 3128 | return BAD_VALUE; |
| 3129 | } |
| 3130 | return NO_ERROR; |
| 3131 | } |
| 3132 | |
| 3133 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, |
| 3134 | uid_t uid) |
| 3135 | { |
| 3136 | ALOGV("releaseAudioPatch() patch %d", handle); |
| 3137 | |
| 3138 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 3139 | |
| 3140 | if (index < 0) { |
| 3141 | return BAD_VALUE; |
| 3142 | } |
| 3143 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 3144 | ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 3145 | mUidCached, patchDesc->mUid, uid); |
| 3146 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 3147 | return INVALID_OPERATION; |
| 3148 | } |
| 3149 | |
| 3150 | struct audio_patch *patch = &patchDesc->mPatch; |
| 3151 | patchDesc->mUid = mUidCached; |
| 3152 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3153 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3154 | if (outputDesc == NULL) { |
| 3155 | ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id); |
| 3156 | return BAD_VALUE; |
| 3157 | } |
| 3158 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3159 | setOutputDevice(outputDesc, |
| 3160 | getNewOutputDevice(outputDesc, true /*fromCache*/), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3161 | true, |
| 3162 | 0, |
| 3163 | NULL); |
| 3164 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 3165 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3166 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3167 | if (inputDesc == NULL) { |
| 3168 | ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id); |
| 3169 | return BAD_VALUE; |
| 3170 | } |
| 3171 | setInputDevice(inputDesc->mIoHandle, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 3172 | getNewInputDevice(inputDesc), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3173 | true, |
| 3174 | NULL); |
| 3175 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3176 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3177 | ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d", |
| 3178 | status, patchDesc->mAfPatchHandle); |
| 3179 | removeAudioPatch(patchDesc->mHandle); |
| 3180 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 3181 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3182 | } else { |
| 3183 | return BAD_VALUE; |
| 3184 | } |
| 3185 | } else { |
| 3186 | return BAD_VALUE; |
| 3187 | } |
| 3188 | return NO_ERROR; |
| 3189 | } |
| 3190 | |
| 3191 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, |
| 3192 | struct audio_patch *patches, |
| 3193 | unsigned int *generation) |
| 3194 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3195 | if (generation == NULL) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3196 | return BAD_VALUE; |
| 3197 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3198 | *generation = curAudioPortGeneration(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3199 | return mAudioPatches.listAudioPatches(num_patches, patches); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3200 | } |
| 3201 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3202 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3203 | { |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3204 | ALOGV("setAudioPortConfig()"); |
| 3205 | |
| 3206 | if (config == NULL) { |
| 3207 | return BAD_VALUE; |
| 3208 | } |
| 3209 | ALOGV("setAudioPortConfig() on port handle %d", config->id); |
| 3210 | // Only support gain configuration for now |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3211 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { |
| 3212 | return INVALID_OPERATION; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3213 | } |
| 3214 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3215 | sp<AudioPortConfig> audioPortConfig; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3216 | if (config->type == AUDIO_PORT_TYPE_MIX) { |
| 3217 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3218 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3219 | if (outputDesc == NULL) { |
| 3220 | return BAD_VALUE; |
| 3221 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 3222 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 3223 | "setAudioPortConfig() called on duplicated output %d", |
| 3224 | outputDesc->mIoHandle); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3225 | audioPortConfig = outputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3226 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3227 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3228 | if (inputDesc == NULL) { |
| 3229 | return BAD_VALUE; |
| 3230 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3231 | audioPortConfig = inputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3232 | } else { |
| 3233 | return BAD_VALUE; |
| 3234 | } |
| 3235 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { |
| 3236 | sp<DeviceDescriptor> deviceDesc; |
| 3237 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
| 3238 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); |
| 3239 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
| 3240 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); |
| 3241 | } else { |
| 3242 | return BAD_VALUE; |
| 3243 | } |
| 3244 | if (deviceDesc == NULL) { |
| 3245 | return BAD_VALUE; |
| 3246 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3247 | audioPortConfig = deviceDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3248 | } else { |
| 3249 | return BAD_VALUE; |
| 3250 | } |
| 3251 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3252 | struct audio_port_config backupConfig; |
| 3253 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); |
| 3254 | if (status == NO_ERROR) { |
| 3255 | struct audio_port_config newConfig; |
| 3256 | audioPortConfig->toAudioPortConfig(&newConfig, config); |
| 3257 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3258 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3259 | if (status != NO_ERROR) { |
| 3260 | audioPortConfig->applyAudioPortConfig(&backupConfig); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3261 | } |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3262 | |
| 3263 | return status; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3264 | } |
| 3265 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3266 | void AudioPolicyManager::releaseResourcesForUid(uid_t uid) |
| 3267 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3268 | clearAudioSources(uid); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3269 | clearAudioPatches(uid); |
| 3270 | clearSessionRoutes(uid); |
| 3271 | } |
| 3272 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3273 | void AudioPolicyManager::clearAudioPatches(uid_t uid) |
| 3274 | { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 3275 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3276 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 3277 | if (patchDesc->mUid == uid) { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 3278 | releaseAudioPatch(mAudioPatches.keyAt(i), uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3279 | } |
| 3280 | } |
| 3281 | } |
| 3282 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3283 | void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy, |
| 3284 | audio_io_handle_t ouptutToSkip) |
| 3285 | { |
| 3286 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 3287 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 3288 | for (size_t j = 0; j < mOutputs.size(); j++) { |
| 3289 | if (mOutputs.keyAt(j) == ouptutToSkip) { |
| 3290 | continue; |
| 3291 | } |
| 3292 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j); |
| 3293 | if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) { |
| 3294 | continue; |
| 3295 | } |
| 3296 | // If the default device for this strategy is on another output mix, |
| 3297 | // invalidate all tracks in this strategy to force re connection. |
| 3298 | // Otherwise select new device on the output mix. |
| 3299 | if (outputs.indexOf(mOutputs.keyAt(j)) < 0) { |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 3300 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3301 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
| 3302 | mpClientInterface->invalidateStream((audio_stream_type_t)stream); |
| 3303 | } |
| 3304 | } |
| 3305 | } else { |
| 3306 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
| 3307 | setOutputDevice(outputDesc, newDevice, false); |
| 3308 | } |
| 3309 | } |
| 3310 | } |
| 3311 | |
| 3312 | void AudioPolicyManager::clearSessionRoutes(uid_t uid) |
| 3313 | { |
| 3314 | // remove output routes associated with this uid |
| 3315 | SortedVector<routing_strategy> affectedStrategies; |
| 3316 | for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) { |
| 3317 | sp<SessionRoute> route = mOutputRoutes.valueAt(i); |
| 3318 | if (route->mUid == uid) { |
| 3319 | mOutputRoutes.removeItemsAt(i); |
| 3320 | if (route->mDeviceDescriptor != 0) { |
| 3321 | affectedStrategies.add(getStrategy(route->mStreamType)); |
| 3322 | } |
| 3323 | } |
| 3324 | } |
| 3325 | // reroute outputs if necessary |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3326 | for (const auto& strategy : affectedStrategies) { |
| 3327 | checkStrategyRoute(strategy, AUDIO_IO_HANDLE_NONE); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3328 | } |
| 3329 | |
| 3330 | // remove input routes associated with this uid |
| 3331 | SortedVector<audio_source_t> affectedSources; |
| 3332 | for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) { |
| 3333 | sp<SessionRoute> route = mInputRoutes.valueAt(i); |
| 3334 | if (route->mUid == uid) { |
| 3335 | mInputRoutes.removeItemsAt(i); |
| 3336 | if (route->mDeviceDescriptor != 0) { |
| 3337 | affectedSources.add(route->mSource); |
| 3338 | } |
| 3339 | } |
| 3340 | } |
| 3341 | // reroute inputs if necessary |
| 3342 | SortedVector<audio_io_handle_t> inputsToClose; |
| 3343 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3344 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3345 | if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3346 | inputsToClose.add(inputDesc->mIoHandle); |
| 3347 | } |
| 3348 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3349 | for (const auto& input : inputsToClose) { |
| 3350 | closeInput(input); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3351 | } |
| 3352 | } |
| 3353 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3354 | void AudioPolicyManager::clearAudioSources(uid_t uid) |
| 3355 | { |
| 3356 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
| 3357 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 3358 | if (sourceDesc->mUid == uid) { |
| 3359 | stopAudioSource(mAudioSources.keyAt(i)); |
| 3360 | } |
| 3361 | } |
| 3362 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3363 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3364 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, |
| 3365 | audio_io_handle_t *ioHandle, |
| 3366 | audio_devices_t *device) |
| 3367 | { |
Glenn Kasten | f0c6d7d | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 3368 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); |
| 3369 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT); |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 3370 | *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3371 | |
François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 3372 | return mSoundTriggerSessions.acquireSession(*session, *ioHandle); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3373 | } |
| 3374 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3375 | status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source, |
| 3376 | const audio_attributes_t *attributes, |
Glenn Kasten | 559d439 | 2016-03-29 13:42:57 -0700 | [diff] [blame] | 3377 | audio_patch_handle_t *handle, |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3378 | uid_t uid) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3379 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3380 | ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle); |
| 3381 | if (source == NULL || attributes == NULL || handle == NULL) { |
| 3382 | return BAD_VALUE; |
| 3383 | } |
| 3384 | |
Glenn Kasten | 559d439 | 2016-03-29 13:42:57 -0700 | [diff] [blame] | 3385 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3386 | |
| 3387 | if (source->role != AUDIO_PORT_ROLE_SOURCE || |
| 3388 | source->type != AUDIO_PORT_TYPE_DEVICE) { |
| 3389 | ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type); |
| 3390 | return INVALID_OPERATION; |
| 3391 | } |
| 3392 | |
| 3393 | sp<DeviceDescriptor> srcDeviceDesc = |
| 3394 | mAvailableInputDevices.getDevice(source->ext.device.type, |
| 3395 | String8(source->ext.device.address)); |
| 3396 | if (srcDeviceDesc == 0) { |
| 3397 | ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type); |
| 3398 | return BAD_VALUE; |
| 3399 | } |
| 3400 | sp<AudioSourceDescriptor> sourceDesc = |
| 3401 | new AudioSourceDescriptor(srcDeviceDesc, attributes, uid); |
| 3402 | |
| 3403 | struct audio_patch dummyPatch; |
| 3404 | sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid); |
| 3405 | sourceDesc->mPatchDesc = patchDesc; |
| 3406 | |
| 3407 | status_t status = connectAudioSource(sourceDesc); |
| 3408 | if (status == NO_ERROR) { |
| 3409 | mAudioSources.add(sourceDesc->getHandle(), sourceDesc); |
| 3410 | *handle = sourceDesc->getHandle(); |
| 3411 | } |
| 3412 | return status; |
| 3413 | } |
| 3414 | |
| 3415 | status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc) |
| 3416 | { |
| 3417 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle()); |
| 3418 | |
| 3419 | // make sure we only have one patch per source. |
| 3420 | disconnectAudioSource(sourceDesc); |
| 3421 | |
| 3422 | routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 3423 | audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3424 | sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice; |
| 3425 | |
| 3426 | audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true); |
| 3427 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 3428 | mAvailableOutputDevices.getDevice(sinkDevice, String8("")); |
| 3429 | |
| 3430 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 3431 | struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch; |
| 3432 | |
| 3433 | if (srcDeviceDesc->getAudioPort()->mModule->getHandle() == |
| 3434 | sinkDeviceDesc->getAudioPort()->mModule->getHandle() && |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 3435 | srcDeviceDesc->getAudioPort()->mModule->getHalVersionMajor() >= 3 && |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3436 | srcDeviceDesc->getAudioPort()->mGains.size() > 0) { |
| 3437 | ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__); |
| 3438 | // create patch between src device and output device |
| 3439 | // create Hwoutput and add to mHwOutputs |
| 3440 | } else { |
| 3441 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs); |
| 3442 | audio_io_handle_t output = |
| 3443 | selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID); |
| 3444 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 3445 | ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice); |
| 3446 | return INVALID_OPERATION; |
| 3447 | } |
| 3448 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 3449 | if (outputDesc->isDuplicated()) { |
| 3450 | ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice); |
| 3451 | return INVALID_OPERATION; |
| 3452 | } |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 3453 | status_t status = outputDesc->start(); |
| 3454 | if (status != NO_ERROR) { |
| 3455 | return status; |
| 3456 | } |
| 3457 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3458 | // create a special patch with no sink and two sources: |
| 3459 | // - the second source indicates to PatchPanel through which output mix this patch should |
| 3460 | // be connected as well as the stream type for volume control |
| 3461 | // - the sink is defined by whatever output device is currently selected for the output |
| 3462 | // though which this patch is routed. |
| 3463 | patch->num_sinks = 0; |
| 3464 | patch->num_sources = 2; |
| 3465 | srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL); |
| 3466 | outputDesc->toAudioPortConfig(&patch->sources[1], NULL); |
| 3467 | patch->sources[1].ext.mix.usecase.stream = stream; |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 3468 | status = mpClientInterface->createAudioPatch(patch, |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3469 | &afPatchHandle, |
| 3470 | 0); |
| 3471 | ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__, |
| 3472 | status, afPatchHandle); |
| 3473 | if (status != NO_ERROR) { |
| 3474 | ALOGW("%s patch panel could not connect device patch, error %d", |
| 3475 | __FUNCTION__, status); |
| 3476 | return INVALID_OPERATION; |
| 3477 | } |
| 3478 | uint32_t delayMs = 0; |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3479 | status = startSource(outputDesc, stream, sinkDevice, NULL, &delayMs); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3480 | |
| 3481 | if (status != NO_ERROR) { |
| 3482 | mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0); |
| 3483 | return status; |
| 3484 | } |
| 3485 | sourceDesc->mSwOutput = outputDesc; |
| 3486 | if (delayMs != 0) { |
| 3487 | usleep(delayMs * 1000); |
| 3488 | } |
| 3489 | } |
| 3490 | |
| 3491 | sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle; |
| 3492 | addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc); |
| 3493 | |
| 3494 | return NO_ERROR; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3495 | } |
| 3496 | |
Glenn Kasten | 559d439 | 2016-03-29 13:42:57 -0700 | [diff] [blame] | 3497 | status_t AudioPolicyManager::stopAudioSource(audio_patch_handle_t handle __unused) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3498 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3499 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle); |
| 3500 | ALOGV("%s handle %d", __FUNCTION__, handle); |
| 3501 | if (sourceDesc == 0) { |
| 3502 | ALOGW("%s unknown source for handle %d", __FUNCTION__, handle); |
| 3503 | return BAD_VALUE; |
| 3504 | } |
| 3505 | status_t status = disconnectAudioSource(sourceDesc); |
| 3506 | |
| 3507 | mAudioSources.removeItem(handle); |
| 3508 | return status; |
| 3509 | } |
| 3510 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3511 | status_t AudioPolicyManager::setMasterMono(bool mono) |
| 3512 | { |
| 3513 | if (mMasterMono == mono) { |
| 3514 | return NO_ERROR; |
| 3515 | } |
| 3516 | mMasterMono = mono; |
| 3517 | // if enabling mono we close all offloaded devices, which will invalidate the |
| 3518 | // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible |
| 3519 | // for recreating the new AudioTrack as non-offloaded PCM. |
| 3520 | // |
| 3521 | // If disabling mono, we leave all tracks as is: we don't know which clients |
| 3522 | // and tracks are able to be recreated as offloaded. The next "song" should |
| 3523 | // play back offloaded. |
| 3524 | if (mMasterMono) { |
| 3525 | Vector<audio_io_handle_t> offloaded; |
| 3526 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3527 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 3528 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
| 3529 | offloaded.push(desc->mIoHandle); |
| 3530 | } |
| 3531 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3532 | for (const auto& handle : offloaded) { |
| 3533 | closeOutput(handle); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3534 | } |
| 3535 | } |
| 3536 | // update master mono for all remaining outputs |
| 3537 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3538 | updateMono(mOutputs.keyAt(i)); |
| 3539 | } |
| 3540 | return NO_ERROR; |
| 3541 | } |
| 3542 | |
| 3543 | status_t AudioPolicyManager::getMasterMono(bool *mono) |
| 3544 | { |
| 3545 | *mono = mMasterMono; |
| 3546 | return NO_ERROR; |
| 3547 | } |
| 3548 | |
Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 3549 | float AudioPolicyManager::getStreamVolumeDB( |
| 3550 | audio_stream_type_t stream, int index, audio_devices_t device) |
| 3551 | { |
| 3552 | return computeVolume(stream, index, device); |
| 3553 | } |
| 3554 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3555 | status_t AudioPolicyManager::getSupportedFormats(audio_io_handle_t ioHandle, |
| 3556 | FormatVector& formats) { |
| 3557 | if (ioHandle == AUDIO_IO_HANDLE_NONE) { |
| 3558 | return BAD_VALUE; |
| 3559 | } |
| 3560 | String8 reply; |
| 3561 | reply = mpClientInterface->getParameters( |
| 3562 | ioHandle, String8(AudioParameter::keyStreamSupportedFormats)); |
| 3563 | ALOGV("%s: supported formats %s", __FUNCTION__, reply.string()); |
| 3564 | AudioParameter repliedParameters(reply); |
| 3565 | if (repliedParameters.get( |
| 3566 | String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) { |
| 3567 | ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__); |
| 3568 | return BAD_VALUE; |
| 3569 | } |
| 3570 | for (auto format : formatsFromString(reply.string())) { |
| 3571 | // Only AUDIO_FORMAT_AAC_LC will be used in Settings UI for all AAC formats. |
| 3572 | for (size_t i = 0; i < ARRAY_SIZE(AAC_FORMATS); i++) { |
| 3573 | if (format == AAC_FORMATS[i]) { |
| 3574 | format = AUDIO_FORMAT_AAC_LC; |
| 3575 | break; |
| 3576 | } |
| 3577 | } |
| 3578 | bool exist = false; |
| 3579 | for (size_t i = 0; i < formats.size(); i++) { |
| 3580 | if (format == formats[i]) { |
| 3581 | exist = true; |
| 3582 | break; |
| 3583 | } |
| 3584 | } |
| 3585 | bool isSurroundFormat = false; |
| 3586 | for (size_t i = 0; i < ARRAY_SIZE(SURROUND_FORMATS); i++) { |
| 3587 | if (SURROUND_FORMATS[i] == format) { |
| 3588 | isSurroundFormat = true; |
| 3589 | break; |
| 3590 | } |
| 3591 | } |
| 3592 | if (!exist && isSurroundFormat) { |
| 3593 | formats.add(format); |
| 3594 | } |
| 3595 | } |
| 3596 | return NO_ERROR; |
| 3597 | } |
| 3598 | |
| 3599 | status_t AudioPolicyManager::getSurroundFormats(unsigned int *numSurroundFormats, |
| 3600 | audio_format_t *surroundFormats, |
| 3601 | bool *surroundFormatsEnabled, |
| 3602 | bool reported) |
| 3603 | { |
| 3604 | if (numSurroundFormats == NULL || (*numSurroundFormats != 0 && |
| 3605 | (surroundFormats == NULL || surroundFormatsEnabled == NULL))) { |
| 3606 | return BAD_VALUE; |
| 3607 | } |
| 3608 | ALOGV("getSurroundFormats() numSurroundFormats %d surroundFormats %p surroundFormatsEnabled %p", |
| 3609 | *numSurroundFormats, surroundFormats, surroundFormatsEnabled); |
| 3610 | |
| 3611 | // Only return value if there is HDMI output. |
| 3612 | if ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_HDMI) == 0) { |
| 3613 | return INVALID_OPERATION; |
| 3614 | } |
| 3615 | |
| 3616 | size_t formatsWritten = 0; |
| 3617 | size_t formatsMax = *numSurroundFormats; |
| 3618 | *numSurroundFormats = 0; |
| 3619 | FormatVector formats; |
| 3620 | if (reported) { |
| 3621 | // Only get surround formats which are reported by device. |
| 3622 | // First list already open outputs that can be routed to this device |
| 3623 | audio_devices_t device = AUDIO_DEVICE_OUT_HDMI; |
| 3624 | SortedVector<audio_io_handle_t> outputs; |
| 3625 | bool reportedFormatFound = false; |
| 3626 | status_t status; |
| 3627 | sp<SwAudioOutputDescriptor> desc; |
| 3628 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3629 | desc = mOutputs.valueAt(i); |
| 3630 | if (!desc->isDuplicated() && (desc->supportedDevices() & device)) { |
| 3631 | outputs.add(mOutputs.keyAt(i)); |
| 3632 | } |
| 3633 | } |
| 3634 | // Open an output to query dynamic parameters. |
| 3635 | DeviceVector hdmiOutputDevices = mAvailableOutputDevices.getDevicesFromType( |
| 3636 | AUDIO_DEVICE_OUT_HDMI); |
| 3637 | for (size_t i = 0; i < hdmiOutputDevices.size(); i++) { |
| 3638 | String8 address = hdmiOutputDevices[i]->mAddress; |
| 3639 | for (const auto& hwModule : mHwModules) { |
| 3640 | for (size_t i = 0; i < hwModule->getOutputProfiles().size(); i++) { |
| 3641 | sp<IOProfile> profile = hwModule->getOutputProfiles()[i]; |
| 3642 | if (profile->supportDevice(AUDIO_DEVICE_OUT_HDMI) && |
| 3643 | profile->supportDeviceAddress(address)) { |
| 3644 | size_t j; |
| 3645 | for (j = 0; j < outputs.size(); j++) { |
| 3646 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
| 3647 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
| 3648 | break; |
| 3649 | } |
| 3650 | } |
| 3651 | if (j != outputs.size()) { |
| 3652 | status = getSupportedFormats(outputs.itemAt(j), formats); |
| 3653 | reportedFormatFound |= (status == NO_ERROR); |
| 3654 | continue; |
| 3655 | } |
| 3656 | |
| 3657 | if (!profile->canOpenNewIo()) { |
| 3658 | ALOGW("Max Output number %u already opened for this profile %s", |
| 3659 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 3660 | continue; |
| 3661 | } |
| 3662 | |
| 3663 | ALOGV("opening output for device %08x with params %s profile %p name %s", |
| 3664 | device, address.string(), profile.get(), profile->getName().string()); |
| 3665 | desc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
| 3666 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 3667 | status_t status = desc->open(nullptr, device, address, |
| 3668 | AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, |
| 3669 | &output); |
| 3670 | |
| 3671 | if (status == NO_ERROR) { |
| 3672 | status = getSupportedFormats(output, formats); |
| 3673 | reportedFormatFound |= (status == NO_ERROR); |
| 3674 | desc->close(); |
| 3675 | output = AUDIO_IO_HANDLE_NONE; |
| 3676 | } |
| 3677 | } |
| 3678 | } |
| 3679 | } |
| 3680 | } |
| 3681 | |
| 3682 | if (!reportedFormatFound) { |
| 3683 | return UNKNOWN_ERROR; |
| 3684 | } |
| 3685 | } else { |
| 3686 | for (size_t i = 0; i < ARRAY_SIZE(SURROUND_FORMATS); i++) { |
| 3687 | formats.add(SURROUND_FORMATS[i]); |
| 3688 | } |
| 3689 | } |
| 3690 | for (size_t i = 0; i < formats.size(); i++) { |
| 3691 | if (formatsWritten < formatsMax) { |
| 3692 | surroundFormats[formatsWritten] = formats[i]; |
| 3693 | bool formatEnabled = false; |
| 3694 | if (formats[i] == AUDIO_FORMAT_AAC_LC) { |
| 3695 | for (size_t j = 0; j < ARRAY_SIZE(AAC_FORMATS); j++) { |
| 3696 | formatEnabled = |
| 3697 | mSurroundFormats.find(AAC_FORMATS[i]) != mSurroundFormats.end(); |
| 3698 | break; |
| 3699 | } |
| 3700 | } else { |
| 3701 | formatEnabled = mSurroundFormats.find(formats[i]) != mSurroundFormats.end(); |
| 3702 | } |
| 3703 | surroundFormatsEnabled[formatsWritten++] = formatEnabled; |
| 3704 | } |
| 3705 | (*numSurroundFormats)++; |
| 3706 | } |
| 3707 | return NO_ERROR; |
| 3708 | } |
| 3709 | |
| 3710 | status_t AudioPolicyManager::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) |
| 3711 | { |
| 3712 | // Check if audio format is a surround formats. |
| 3713 | bool isSurroundFormat = false; |
| 3714 | for (size_t i = 0; i < ARRAY_SIZE(SURROUND_FORMATS); i++) { |
| 3715 | if (audioFormat == SURROUND_FORMATS[i]) { |
| 3716 | isSurroundFormat = true; |
| 3717 | break; |
| 3718 | } |
| 3719 | } |
| 3720 | if (!isSurroundFormat) { |
| 3721 | return BAD_VALUE; |
| 3722 | } |
| 3723 | |
| 3724 | // Should only be called when MANUAL. |
| 3725 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 3726 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
| 3727 | if (forceUse != AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
| 3728 | return INVALID_OPERATION; |
| 3729 | } |
| 3730 | |
| 3731 | if ((mSurroundFormats.find(audioFormat) != mSurroundFormats.end() && enabled) |
| 3732 | || (mSurroundFormats.find(audioFormat) == mSurroundFormats.end() && !enabled)) { |
| 3733 | return NO_ERROR; |
| 3734 | } |
| 3735 | |
| 3736 | // The operation is valid only when there is HDMI output available. |
| 3737 | if ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_HDMI) == 0) { |
| 3738 | return INVALID_OPERATION; |
| 3739 | } |
| 3740 | |
| 3741 | if (enabled) { |
| 3742 | if (audioFormat == AUDIO_FORMAT_AAC_LC) { |
| 3743 | for (size_t i = 0; i < ARRAY_SIZE(AAC_FORMATS); i++) { |
| 3744 | mSurroundFormats.insert(AAC_FORMATS[i]); |
| 3745 | } |
| 3746 | } else { |
| 3747 | mSurroundFormats.insert(audioFormat); |
| 3748 | } |
| 3749 | } else { |
| 3750 | if (audioFormat == AUDIO_FORMAT_AAC_LC) { |
| 3751 | for (size_t i = 0; i < ARRAY_SIZE(AAC_FORMATS); i++) { |
| 3752 | mSurroundFormats.erase(AAC_FORMATS[i]); |
| 3753 | } |
| 3754 | } else { |
| 3755 | mSurroundFormats.erase(audioFormat); |
| 3756 | } |
| 3757 | } |
| 3758 | |
| 3759 | sp<SwAudioOutputDescriptor> outputDesc; |
| 3760 | bool profileUpdated = false; |
| 3761 | DeviceVector hdmiOutputDevices = mAvailableOutputDevices.getDevicesFromType( |
| 3762 | AUDIO_DEVICE_OUT_HDMI); |
| 3763 | for (size_t i = 0; i < hdmiOutputDevices.size(); i++) { |
| 3764 | // Simulate reconnection to update enabled surround sound formats. |
| 3765 | String8 address = hdmiOutputDevices[i]->mAddress; |
| 3766 | String8 name = hdmiOutputDevices[i]->getName(); |
| 3767 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 3768 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 3769 | address.c_str(), |
| 3770 | name.c_str()); |
| 3771 | if (status != NO_ERROR) { |
| 3772 | continue; |
| 3773 | } |
| 3774 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 3775 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 3776 | address.c_str(), |
| 3777 | name.c_str()); |
| 3778 | profileUpdated |= (status == NO_ERROR); |
| 3779 | } |
| 3780 | DeviceVector hdmiInputDevices = mAvailableInputDevices.getDevicesFromType( |
| 3781 | AUDIO_DEVICE_IN_HDMI); |
| 3782 | for (size_t i = 0; i < hdmiInputDevices.size(); i++) { |
| 3783 | // Simulate reconnection to update enabled surround sound formats. |
| 3784 | String8 address = hdmiInputDevices[i]->mAddress; |
| 3785 | String8 name = hdmiInputDevices[i]->getName(); |
| 3786 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 3787 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 3788 | address.c_str(), |
| 3789 | name.c_str()); |
| 3790 | if (status != NO_ERROR) { |
| 3791 | continue; |
| 3792 | } |
| 3793 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 3794 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 3795 | address.c_str(), |
| 3796 | name.c_str()); |
| 3797 | profileUpdated |= (status == NO_ERROR); |
| 3798 | } |
| 3799 | |
| 3800 | // Undo the surround formats change due to no audio profiles updated. |
| 3801 | if (!profileUpdated) { |
| 3802 | if (enabled) { |
| 3803 | if (audioFormat == AUDIO_FORMAT_AAC_LC) { |
| 3804 | for (size_t i = 0; i < ARRAY_SIZE(AAC_FORMATS); i++) { |
| 3805 | mSurroundFormats.erase(AAC_FORMATS[i]); |
| 3806 | } |
| 3807 | } else { |
| 3808 | mSurroundFormats.erase(audioFormat); |
| 3809 | } |
| 3810 | } else { |
| 3811 | if (audioFormat == AUDIO_FORMAT_AAC_LC) { |
| 3812 | for (size_t i = 0; i < ARRAY_SIZE(AAC_FORMATS); i++) { |
| 3813 | mSurroundFormats.insert(AAC_FORMATS[i]); |
| 3814 | } |
| 3815 | } else { |
| 3816 | mSurroundFormats.insert(audioFormat); |
| 3817 | } |
| 3818 | } |
| 3819 | } |
| 3820 | |
| 3821 | return profileUpdated ? NO_ERROR : INVALID_OPERATION; |
| 3822 | } |
| 3823 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 3824 | void AudioPolicyManager::setRecordSilenced(uid_t uid, bool silenced) |
| 3825 | { |
| 3826 | ALOGV("AudioPolicyManager:setRecordSilenced(uid:%d, silenced:%d)", uid, silenced); |
| 3827 | |
| 3828 | Vector<sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs(); |
| 3829 | for (size_t i = 0; i < activeInputs.size(); i++) { |
| 3830 | sp<AudioInputDescriptor> activeDesc = activeInputs[i]; |
| 3831 | AudioSessionCollection activeSessions = activeDesc->getAudioSessions(true); |
| 3832 | for (size_t j = 0; j < activeSessions.size(); j++) { |
| 3833 | sp<AudioSession> activeSession = activeSessions.valueAt(j); |
| 3834 | if (activeSession->uid() == uid) { |
| 3835 | activeSession->setSilenced(silenced); |
| 3836 | } |
| 3837 | } |
| 3838 | } |
| 3839 | } |
| 3840 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3841 | status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc) |
| 3842 | { |
| 3843 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle()); |
| 3844 | |
| 3845 | sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle); |
| 3846 | if (patchDesc == 0) { |
| 3847 | ALOGW("%s source has no patch with handle %d", __FUNCTION__, |
| 3848 | sourceDesc->mPatchDesc->mHandle); |
| 3849 | return BAD_VALUE; |
| 3850 | } |
| 3851 | removeAudioPatch(sourceDesc->mPatchDesc->mHandle); |
| 3852 | |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 3853 | audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3854 | sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote(); |
| 3855 | if (swOutputDesc != 0) { |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 3856 | status_t status = stopSource(swOutputDesc, stream, false); |
| 3857 | if (status == NO_ERROR) { |
| 3858 | swOutputDesc->stop(); |
| 3859 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3860 | mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3861 | } else { |
| 3862 | sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote(); |
| 3863 | if (hwOutputDesc != 0) { |
| 3864 | // release patch between src device and output device |
| 3865 | // close Hwoutput and remove from mHwOutputs |
| 3866 | } else { |
| 3867 | ALOGW("%s source has neither SW nor HW output", __FUNCTION__); |
| 3868 | } |
| 3869 | } |
| 3870 | return NO_ERROR; |
| 3871 | } |
| 3872 | |
| 3873 | sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput( |
| 3874 | audio_io_handle_t output, routing_strategy strategy) |
| 3875 | { |
| 3876 | sp<AudioSourceDescriptor> source; |
| 3877 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
| 3878 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 3879 | routing_strategy sourceStrategy = |
| 3880 | (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes); |
| 3881 | sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote(); |
| 3882 | if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) { |
| 3883 | source = sourceDesc; |
| 3884 | break; |
| 3885 | } |
| 3886 | } |
| 3887 | return source; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3888 | } |
| 3889 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3890 | // ---------------------------------------------------------------------------- |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3891 | // AudioPolicyManager |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3892 | // ---------------------------------------------------------------------------- |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3893 | uint32_t AudioPolicyManager::nextAudioPortGeneration() |
| 3894 | { |
Mikhail Naganov | 2773dd7 | 2017-12-08 10:12:11 -0800 | [diff] [blame] | 3895 | return mAudioPortGeneration++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3896 | } |
| 3897 | |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3898 | #ifdef USE_XML_AUDIO_POLICY_CONF |
| 3899 | // Treblized audio policy xml config will be located in /odm/etc or /vendor/etc. |
| 3900 | static const char *kConfigLocationList[] = |
| 3901 | {"/odm/etc", "/vendor/etc", "/system/etc"}; |
| 3902 | static const int kConfigLocationListSize = |
| 3903 | (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0])); |
| 3904 | |
| 3905 | static status_t deserializeAudioPolicyXmlConfig(AudioPolicyConfig &config) { |
| 3906 | char audioPolicyXmlConfigFile[AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH]; |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3907 | std::vector<const char*> fileNames; |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3908 | status_t ret; |
| 3909 | |
Cheney Ni | 00ce33d | 2018-11-01 06:30:37 +0800 | [diff] [blame^] | 3910 | if (property_get_bool("ro.bluetooth.a2dp_offload.supported", false)) { |
| 3911 | if (property_get_bool("persist.bluetooth.a2dp_offload.disabled", false)) { |
Cheney Ni | 6851adb | 2018-11-01 06:30:37 +0800 | [diff] [blame] | 3912 | fileNames.push_back(AUDIO_POLICY_A2DP_OFFLOAD_DISABLED_XML_CONFIG_FILE_NAME); |
Cheney Ni | 00ce33d | 2018-11-01 06:30:37 +0800 | [diff] [blame^] | 3913 | } else if (property_get_bool("persist.bluetooth.bluetooth_audio_hal.enabled", false)) { |
| 3914 | // This property persist.bluetooth.bluetooth_audio_hal.enabled is temporary only. |
| 3915 | // xml files AUDIO_POLICY_BLUETOOTH_HAL_ENABLED_XML_CONFIG_FILE_NAME, although having |
| 3916 | // the same name, must be different in offload and non offload cases in device |
| 3917 | // specific configuration file. |
| 3918 | fileNames.push_back(AUDIO_POLICY_BLUETOOTH_HAL_ENABLED_XML_CONFIG_FILE_NAME); |
Cheney Ni | 6851adb | 2018-11-01 06:30:37 +0800 | [diff] [blame] | 3919 | } |
Cheney Ni | 00ce33d | 2018-11-01 06:30:37 +0800 | [diff] [blame^] | 3920 | } else if (property_get_bool("persist.bluetooth.bluetooth_audio_hal.enabled", false)) { |
| 3921 | fileNames.push_back(AUDIO_POLICY_BLUETOOTH_HAL_ENABLED_XML_CONFIG_FILE_NAME); |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3922 | } |
| 3923 | fileNames.push_back(AUDIO_POLICY_XML_CONFIG_FILE_NAME); |
| 3924 | |
| 3925 | for (const char* fileName : fileNames) { |
| 3926 | for (int i = 0; i < kConfigLocationListSize; i++) { |
| 3927 | PolicySerializer serializer; |
| 3928 | snprintf(audioPolicyXmlConfigFile, sizeof(audioPolicyXmlConfigFile), |
| 3929 | "%s/%s", kConfigLocationList[i], fileName); |
| 3930 | ret = serializer.deserialize(audioPolicyXmlConfigFile, config); |
| 3931 | if (ret == NO_ERROR) { |
| 3932 | return ret; |
| 3933 | } |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3934 | } |
| 3935 | } |
| 3936 | return ret; |
| 3937 | } |
| 3938 | #endif |
| 3939 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3940 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface, |
| 3941 | bool /*forTesting*/) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3942 | : |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3943 | mUidCached(getuid()), |
| 3944 | mpClientInterface(clientInterface), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3945 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3946 | mA2dpSuspended(false), |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3947 | #ifdef USE_XML_AUDIO_POLICY_CONF |
| 3948 | mVolumeCurves(new VolumeCurvesCollection()), |
| 3949 | mConfig(mHwModulesAll, mAvailableOutputDevices, mAvailableInputDevices, |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 3950 | mDefaultOutputDevice, static_cast<VolumeCurvesCollection*>(mVolumeCurves.get())), |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3951 | #else |
| 3952 | mVolumeCurves(new StreamDescriptorCollection()), |
| 3953 | mConfig(mHwModulesAll, mAvailableOutputDevices, mAvailableInputDevices, |
| 3954 | mDefaultOutputDevice), |
| 3955 | #endif |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 3956 | mAudioPortGeneration(1), |
| 3957 | mBeaconMuteRefCount(0), |
| 3958 | mBeaconPlayingRefCount(0), |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3959 | mBeaconMuted(false), |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3960 | mTtsOutputAvailable(false), |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3961 | mMasterMono(false), |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 3962 | mMusicEffectOutput(AUDIO_IO_HANDLE_NONE), |
| 3963 | mHasComputedSoundTriggerSupportsConcurrentCapture(false) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3964 | { |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3965 | } |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3966 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3967 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) |
| 3968 | : AudioPolicyManager(clientInterface, false /*forTesting*/) |
| 3969 | { |
| 3970 | loadConfig(); |
| 3971 | initialize(); |
| 3972 | } |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3973 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3974 | void AudioPolicyManager::loadConfig() { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3975 | #ifdef USE_XML_AUDIO_POLICY_CONF |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3976 | if (deserializeAudioPolicyXmlConfig(getConfig()) != NO_ERROR) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3977 | #else |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3978 | if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, getConfig()) != NO_ERROR) |
| 3979 | && (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, getConfig()) != NO_ERROR)) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3980 | #endif |
| 3981 | ALOGE("could not load audio policy configuration file, setting defaults"); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3982 | getConfig().setDefault(); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3983 | } |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3984 | } |
| 3985 | |
| 3986 | status_t AudioPolicyManager::initialize() { |
| 3987 | mVolumeCurves->initializeVolumeCurves(getConfig().isSpeakerDrcEnabled()); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3988 | |
| 3989 | // 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] | 3990 | audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance(); |
| 3991 | if (!engineInstance) { |
| 3992 | ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3993 | return NO_INIT; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3994 | } |
| 3995 | // Retrieve the Policy Manager Interface |
| 3996 | mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>(); |
| 3997 | if (mEngine == NULL) { |
| 3998 | ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3999 | return NO_INIT; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4000 | } |
| 4001 | mEngine->setObserver(this); |
| 4002 | status_t status = mEngine->initCheck(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4003 | if (status != NO_ERROR) { |
| 4004 | LOG_FATAL("Policy engine not initialized(err=%d)", status); |
| 4005 | return status; |
| 4006 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4007 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4008 | // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4009 | // open all output streams needed to access attached devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4010 | audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types(); |
| 4011 | audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4012 | for (const auto& hwModule : mHwModulesAll) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4013 | hwModule->setHandle(mpClientInterface->loadHwModule(hwModule->getName())); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4014 | if (hwModule->getHandle() == AUDIO_MODULE_HANDLE_NONE) { |
| 4015 | ALOGW("could not open HW module %s", hwModule->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4016 | continue; |
| 4017 | } |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4018 | mHwModules.push_back(hwModule); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4019 | // open all output streams needed to access attached devices |
| 4020 | // except for direct output streams that are only opened when they are actually |
| 4021 | // required by an app. |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4022 | // This also validates mAvailableOutputDevices list |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4023 | for (const auto& outProfile : hwModule->getOutputProfiles()) { |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4024 | if (!outProfile->canOpenNewIo()) { |
| 4025 | ALOGE("Invalid Output profile max open count %u for profile %s", |
| 4026 | outProfile->maxOpenCount, outProfile->getTagName().c_str()); |
| 4027 | continue; |
| 4028 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4029 | if (!outProfile->hasSupportedDevices()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4030 | ALOGW("Output profile contains no device on module %s", hwModule->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4031 | continue; |
| 4032 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4033 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 4034 | mTtsOutputAvailable = true; |
| 4035 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4036 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4037 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4038 | continue; |
| 4039 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4040 | audio_devices_t profileType = outProfile->getSupportedDevicesType(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4041 | if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) { |
| 4042 | profileType = mDefaultOutputDevice->type(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 4043 | } else { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4044 | // chose first device present in profile's SupportedDevices also part of |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4045 | // outputDeviceTypes |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4046 | profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4047 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4048 | if ((profileType & outputDeviceTypes) == 0) { |
| 4049 | continue; |
| 4050 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4051 | sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile, |
| 4052 | mpClientInterface); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 4053 | const DeviceVector &supportedDevices = outProfile->getSupportedDevices(); |
| 4054 | const DeviceVector &devicesForType = supportedDevices.getDevicesFromType(profileType); |
| 4055 | String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress |
| 4056 | : String8(""); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4057 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4058 | status_t status = outputDesc->open(nullptr, profileType, address, |
| 4059 | AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, &output); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4060 | |
| 4061 | if (status != NO_ERROR) { |
| 4062 | ALOGW("Cannot open output stream for device %08x on hw module %s", |
| 4063 | outputDesc->mDevice, |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4064 | hwModule->getName()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4065 | } else { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4066 | for (const auto& dev : supportedDevices) { |
| 4067 | ssize_t index = mAvailableOutputDevices.indexOf(dev); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4068 | // 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] | 4069 | if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4070 | mAvailableOutputDevices[index]->attach(hwModule); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4071 | } |
| 4072 | } |
| 4073 | if (mPrimaryOutput == 0 && |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4074 | outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4075 | mPrimaryOutput = outputDesc; |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4076 | } |
| 4077 | addOutput(output, outputDesc); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4078 | setOutputDevice(outputDesc, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4079 | profileType, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 4080 | true, |
| 4081 | 0, |
| 4082 | NULL, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4083 | address); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4084 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4085 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4086 | // open input streams needed to access attached devices to validate |
| 4087 | // mAvailableInputDevices list |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4088 | for (const auto& inProfile : hwModule->getInputProfiles()) { |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4089 | if (!inProfile->canOpenNewIo()) { |
| 4090 | ALOGE("Invalid Input profile max open count %u for profile %s", |
| 4091 | inProfile->maxOpenCount, inProfile->getTagName().c_str()); |
| 4092 | continue; |
| 4093 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4094 | if (!inProfile->hasSupportedDevices()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4095 | ALOGW("Input profile contains no device on module %s", hwModule->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4096 | continue; |
| 4097 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4098 | // chose first device present in profile's SupportedDevices also part of |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4099 | // inputDeviceTypes |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4100 | audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes); |
| 4101 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4102 | if ((profileType & inputDeviceTypes) == 0) { |
| 4103 | continue; |
| 4104 | } |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 4105 | sp<AudioInputDescriptor> inputDesc = |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4106 | new AudioInputDescriptor(inProfile, mpClientInterface); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4107 | |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 4108 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType); |
| 4109 | // the inputs vector must be of size >= 1, but we don't want to crash here |
| 4110 | String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress |
| 4111 | : String8(""); |
| 4112 | ALOGV(" for input device 0x%x using address %s", profileType, address.string()); |
| 4113 | ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!"); |
| 4114 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4115 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4116 | status_t status = inputDesc->open(nullptr, |
| 4117 | profileType, |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 4118 | address, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4119 | AUDIO_SOURCE_MIC, |
| 4120 | AUDIO_INPUT_FLAG_NONE, |
| 4121 | &input); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4122 | |
| 4123 | if (status == NO_ERROR) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4124 | for (const auto& dev : inProfile->getSupportedDevices()) { |
| 4125 | ssize_t index = mAvailableInputDevices.indexOf(dev); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4126 | // 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] | 4127 | if (index >= 0) { |
| 4128 | sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index]; |
| 4129 | if (!devDesc->isAttached()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4130 | devDesc->attach(hwModule); |
Eric Laurent | 83efe1c | 2017-07-09 16:51:08 -0700 | [diff] [blame] | 4131 | devDesc->importAudioPort(inProfile, true); |
Eric Laurent | 45aabc3 | 2015-08-06 09:11:13 -0700 | [diff] [blame] | 4132 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4133 | } |
| 4134 | } |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4135 | inputDesc->close(); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4136 | } else { |
| 4137 | ALOGW("Cannot open input stream for device %08x on hw module %s", |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4138 | profileType, |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4139 | hwModule->getName()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4140 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4141 | } |
| 4142 | } |
| 4143 | // make sure all attached devices have been allocated a unique ID |
| 4144 | for (size_t i = 0; i < mAvailableOutputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 4145 | if (!mAvailableOutputDevices[i]->isAttached()) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4146 | ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4147 | mAvailableOutputDevices.remove(mAvailableOutputDevices[i]); |
| 4148 | continue; |
| 4149 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4150 | // The device is now validated and can be appended to the available devices of the engine |
| 4151 | mEngine->setDeviceConnectionState(mAvailableOutputDevices[i], |
| 4152 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4153 | i++; |
| 4154 | } |
| 4155 | for (size_t i = 0; i < mAvailableInputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 4156 | if (!mAvailableInputDevices[i]->isAttached()) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4157 | ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4158 | mAvailableInputDevices.remove(mAvailableInputDevices[i]); |
| 4159 | continue; |
| 4160 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4161 | // The device is now validated and can be appended to the available devices of the engine |
| 4162 | mEngine->setDeviceConnectionState(mAvailableInputDevices[i], |
| 4163 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4164 | i++; |
| 4165 | } |
| 4166 | // make sure default device is reachable |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4167 | if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4168 | ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type()); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4169 | status = NO_INIT; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4170 | } |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 4171 | // If microphones address is empty, set it according to device type |
| 4172 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { |
| 4173 | if (mAvailableInputDevices[i]->mAddress.isEmpty()) { |
| 4174 | if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BUILTIN_MIC) { |
| 4175 | mAvailableInputDevices[i]->mAddress = String8(AUDIO_BOTTOM_MICROPHONE_ADDRESS); |
| 4176 | } else if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BACK_MIC) { |
| 4177 | mAvailableInputDevices[i]->mAddress = String8(AUDIO_BACK_MICROPHONE_ADDRESS); |
| 4178 | } |
| 4179 | } |
| 4180 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4181 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4182 | if (mPrimaryOutput == 0) { |
| 4183 | ALOGE("Failed to open primary output"); |
| 4184 | status = NO_INIT; |
| 4185 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4186 | |
Tomoharu Kasahara | 71c9091 | 2018-10-31 09:10:12 +0900 | [diff] [blame] | 4187 | // Silence ALOGV statements |
| 4188 | property_set("log.tag." LOG_TAG, "D"); |
| 4189 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4190 | updateDevicesAndOutputs(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4191 | return status; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4192 | } |
| 4193 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4194 | AudioPolicyManager::~AudioPolicyManager() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4195 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4196 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4197 | mOutputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4198 | } |
| 4199 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4200 | mInputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4201 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4202 | mAvailableOutputDevices.clear(); |
| 4203 | mAvailableInputDevices.clear(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4204 | mOutputs.clear(); |
| 4205 | mInputs.clear(); |
| 4206 | mHwModules.clear(); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4207 | mHwModulesAll.clear(); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4208 | mSurroundFormats.clear(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4209 | } |
| 4210 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4211 | status_t AudioPolicyManager::initCheck() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4212 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 4213 | return hasPrimaryOutput() ? NO_ERROR : NO_INIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4214 | } |
| 4215 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4216 | // --- |
| 4217 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 4218 | void AudioPolicyManager::addOutput(audio_io_handle_t output, |
| 4219 | const sp<SwAudioOutputDescriptor>& outputDesc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4220 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4221 | mOutputs.add(output, outputDesc); |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 4222 | applyStreamVolumes(outputDesc, AUDIO_DEVICE_NONE, 0 /* delayMs */, true /* force */); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4223 | updateMono(output); // update mono status when adding to output list |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4224 | selectOutputForMusicEffects(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4225 | nextAudioPortGeneration(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4226 | } |
| 4227 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4228 | void AudioPolicyManager::removeOutput(audio_io_handle_t output) |
| 4229 | { |
| 4230 | mOutputs.removeItem(output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4231 | selectOutputForMusicEffects(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4232 | } |
| 4233 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 4234 | void AudioPolicyManager::addInput(audio_io_handle_t input, |
| 4235 | const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4236 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4237 | mInputs.add(input, inputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4238 | nextAudioPortGeneration(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4239 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4240 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4241 | void AudioPolicyManager::findIoHandlesByAddress(const sp<SwAudioOutputDescriptor>& desc /*in*/, |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4242 | const audio_devices_t device /*in*/, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4243 | const String8& address /*in*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4244 | SortedVector<audio_io_handle_t>& outputs /*out*/) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4245 | sp<DeviceDescriptor> devDesc = |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4246 | desc->mProfile->getSupportedDeviceByAddress(device, address); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4247 | if (devDesc != 0) { |
| 4248 | ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s", |
| 4249 | desc->mIoHandle, address.string()); |
| 4250 | outputs.add(desc->mIoHandle); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4251 | } |
| 4252 | } |
| 4253 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4254 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4255 | audio_policy_dev_state_t state, |
| 4256 | SortedVector<audio_io_handle_t>& outputs, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4257 | const String8& address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4258 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4259 | audio_devices_t device = devDesc->type(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4260 | sp<SwAudioOutputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4261 | |
| 4262 | if (audio_device_is_digital(device)) { |
| 4263 | // erase all current sample rates, formats and channel masks |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 4264 | devDesc->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4265 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4266 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4267 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4268 | // first list already open outputs that can be routed to this device |
| 4269 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4270 | desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4271 | if (!desc->isDuplicated() && (desc->supportedDevices() & device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4272 | if (!device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4273 | ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i)); |
| 4274 | outputs.add(mOutputs.keyAt(i)); |
| 4275 | } else { |
| 4276 | ALOGV(" checking address match due to device 0x%x", device); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4277 | findIoHandlesByAddress(desc, device, address, outputs); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4278 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4279 | } |
| 4280 | } |
| 4281 | // then look for output profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4282 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4283 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4284 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 4285 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4286 | if (profile->supportDevice(device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4287 | if (!device_distinguishes_on_address(device) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4288 | profile->supportDeviceAddress(address)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4289 | profiles.add(profile); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4290 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %s", |
| 4291 | j, hwModule->getName()); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4292 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4293 | } |
| 4294 | } |
| 4295 | } |
| 4296 | |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 4297 | ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size()); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4298 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4299 | if (profiles.isEmpty() && outputs.isEmpty()) { |
| 4300 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 4301 | return BAD_VALUE; |
| 4302 | } |
| 4303 | |
| 4304 | // open outputs for matching profiles if needed. Direct outputs are also opened to |
| 4305 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 4306 | 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] | 4307 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4308 | |
| 4309 | // nothing to do if one output is already opened for this profile |
| 4310 | size_t j; |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4311 | for (j = 0; j < outputs.size(); j++) { |
| 4312 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4313 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 4314 | // matching profile: save the sample rates, format and channel masks supported |
| 4315 | // by the profile in our device descriptor |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4316 | if (audio_device_is_digital(device)) { |
| 4317 | devDesc->importAudioPort(profile); |
| 4318 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4319 | break; |
| 4320 | } |
| 4321 | } |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4322 | if (j != outputs.size()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4323 | continue; |
| 4324 | } |
| 4325 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4326 | if (!profile->canOpenNewIo()) { |
| 4327 | ALOGW("Max Output number %u already opened for this profile %s", |
| 4328 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 4329 | continue; |
| 4330 | } |
| 4331 | |
Eric Laurent | 83efe1c | 2017-07-09 16:51:08 -0700 | [diff] [blame] | 4332 | ALOGV("opening output for device %08x with params %s profile %p name %s", |
| 4333 | device, address.string(), profile.get(), profile->getName().string()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4334 | desc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4335 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4336 | status_t status = desc->open(nullptr, device, address, |
| 4337 | AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, &output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4338 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4339 | if (status == NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4340 | // Here is where the out_set_parameters() for card & device gets called |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4341 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4342 | char *param = audio_device_address_to_parameter(device, address); |
| 4343 | mpClientInterface->setParameters(output, String8(param)); |
| 4344 | free(param); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4345 | } |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 4346 | updateAudioProfiles(device, output, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4347 | if (!profile->hasValidAudioProfile()) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4348 | ALOGW("checkOutputsForDevice() missing param"); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4349 | desc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4350 | output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4351 | } else if (profile->hasDynamicAudioProfile()) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4352 | desc->close(); |
Phil Burk | 702b105 | 2016-03-02 16:38:26 -0800 | [diff] [blame] | 4353 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4354 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 4355 | profile->pickAudioProfile( |
| 4356 | config.sample_rate, config.channel_mask, config.format); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4357 | config.offload_info.sample_rate = config.sample_rate; |
| 4358 | config.offload_info.channel_mask = config.channel_mask; |
| 4359 | config.offload_info.format = config.format; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4360 | |
| 4361 | status_t status = desc->open(&config, device, address, AUDIO_STREAM_DEFAULT, |
| 4362 | AUDIO_OUTPUT_FLAG_NONE, &output); |
| 4363 | if (status != NO_ERROR) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4364 | output = AUDIO_IO_HANDLE_NONE; |
| 4365 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4366 | } |
| 4367 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4368 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4369 | addOutput(output, desc); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4370 | if (device_distinguishes_on_address(device) && address != "0") { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4371 | sp<AudioPolicyMix> policyMix; |
| 4372 | if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4373 | ALOGE("checkOutputsForDevice() cannot find policy for address %s", |
| 4374 | address.string()); |
| 4375 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4376 | policyMix->setOutput(desc); |
Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 4377 | desc->mPolicyMix = policyMix->getMix(); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4378 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 4379 | } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
| 4380 | hasPrimaryOutput()) { |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 4381 | // no duplicated output for direct outputs and |
| 4382 | // outputs used by dynamic policy mixes |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4383 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4384 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4385 | //TODO: configure audio effect output stage here |
| 4386 | |
| 4387 | // 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] | 4388 | sp<SwAudioOutputDescriptor> dupOutputDesc = |
| 4389 | new SwAudioOutputDescriptor(NULL, mpClientInterface); |
| 4390 | status_t status = dupOutputDesc->openDuplicating(mPrimaryOutput, desc, |
| 4391 | &duplicatedOutput); |
| 4392 | if (status == NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4393 | // add duplicated output descriptor |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4394 | addOutput(duplicatedOutput, dupOutputDesc); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4395 | } else { |
| 4396 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4397 | mPrimaryOutput->mIoHandle, output); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4398 | desc->close(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4399 | removeOutput(output); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4400 | nextAudioPortGeneration(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4401 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4402 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4403 | } |
| 4404 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4405 | } else { |
| 4406 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4407 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4408 | if (output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4409 | ALOGW("checkOutputsForDevice() could not open output for device %x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4410 | profiles.removeAt(profile_index); |
| 4411 | profile_index--; |
| 4412 | } else { |
| 4413 | outputs.add(output); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4414 | // Load digital format info only for digital devices |
| 4415 | if (audio_device_is_digital(device)) { |
| 4416 | devDesc->importAudioPort(profile); |
| 4417 | } |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 4418 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4419 | if (device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4420 | ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)", |
| 4421 | device, address.string()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4422 | setOutputDevice(desc, device, true/*force*/, 0/*delay*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4423 | NULL/*patch handle*/, address.string()); |
| 4424 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4425 | ALOGV("checkOutputsForDevice(): adding output %d", output); |
| 4426 | } |
| 4427 | } |
| 4428 | |
| 4429 | if (profiles.isEmpty()) { |
| 4430 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 4431 | return BAD_VALUE; |
| 4432 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4433 | } else { // Disconnect |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4434 | // check if one opened output is not needed any more after disconnecting one device |
| 4435 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4436 | desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4437 | if (!desc->isDuplicated()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4438 | // exact match on device |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4439 | if (device_distinguishes_on_address(device) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4440 | (desc->supportedDevices() == device)) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4441 | findIoHandlesByAddress(desc, device, address, outputs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4442 | } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4443 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", |
| 4444 | mOutputs.keyAt(i)); |
| 4445 | outputs.add(mOutputs.keyAt(i)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4446 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4447 | } |
| 4448 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4449 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4450 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4451 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 4452 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4453 | if (profile->supportDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4454 | ALOGV("checkOutputsForDevice(): " |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4455 | "clearing direct output profile %zu on module %s", |
| 4456 | j, hwModule->getName()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4457 | profile->clearAudioProfiles(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4458 | } |
| 4459 | } |
| 4460 | } |
| 4461 | } |
| 4462 | return NO_ERROR; |
| 4463 | } |
| 4464 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4465 | status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4466 | audio_policy_dev_state_t state, |
| 4467 | SortedVector<audio_io_handle_t>& inputs, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4468 | const String8& address) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4469 | { |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4470 | audio_devices_t device = devDesc->type(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4471 | sp<AudioInputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4472 | |
| 4473 | if (audio_device_is_digital(device)) { |
| 4474 | // erase all current sample rates, formats and channel masks |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 4475 | devDesc->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4476 | } |
| 4477 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4478 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 4479 | // first list already open inputs that can be routed to this device |
| 4480 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4481 | desc = mInputs.valueAt(input_index); |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4482 | if (desc->mProfile->supportDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4483 | ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index)); |
| 4484 | inputs.add(mInputs.keyAt(input_index)); |
| 4485 | } |
| 4486 | } |
| 4487 | |
| 4488 | // then look for input profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4489 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4490 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4491 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4492 | profile_index < hwModule->getInputProfiles().size(); |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4493 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4494 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4495 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4496 | if (profile->supportDevice(device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4497 | if (!device_distinguishes_on_address(device) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4498 | profile->supportDeviceAddress(address)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4499 | profiles.add(profile); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4500 | ALOGV("checkInputsForDevice(): adding profile %zu from module %s", |
| 4501 | profile_index, hwModule->getName()); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4502 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4503 | } |
| 4504 | } |
| 4505 | } |
| 4506 | |
| 4507 | if (profiles.isEmpty() && inputs.isEmpty()) { |
| 4508 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 4509 | return BAD_VALUE; |
| 4510 | } |
| 4511 | |
| 4512 | // open inputs for matching profiles if needed. Direct inputs are also opened to |
| 4513 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 4514 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { |
| 4515 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4516 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4517 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4518 | // nothing to do if one input is already opened for this profile |
| 4519 | size_t input_index; |
| 4520 | for (input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4521 | desc = mInputs.valueAt(input_index); |
| 4522 | if (desc->mProfile == profile) { |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4523 | if (audio_device_is_digital(device)) { |
| 4524 | devDesc->importAudioPort(profile); |
| 4525 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4526 | break; |
| 4527 | } |
| 4528 | } |
| 4529 | if (input_index != mInputs.size()) { |
| 4530 | continue; |
| 4531 | } |
| 4532 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4533 | if (!profile->canOpenNewIo()) { |
| 4534 | ALOGW("Max Input number %u already opened for this profile %s", |
| 4535 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 4536 | continue; |
| 4537 | } |
| 4538 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4539 | desc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4540 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4541 | status_t status = desc->open(nullptr, |
| 4542 | device, |
| 4543 | address, |
| 4544 | AUDIO_SOURCE_MIC, |
| 4545 | AUDIO_INPUT_FLAG_NONE, |
| 4546 | &input); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4547 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4548 | if (status == NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4549 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4550 | char *param = audio_device_address_to_parameter(device, address); |
| 4551 | mpClientInterface->setParameters(input, String8(param)); |
| 4552 | free(param); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4553 | } |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 4554 | updateAudioProfiles(device, input, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4555 | if (!profile->hasValidAudioProfile()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4556 | ALOGW("checkInputsForDevice() direct input missing param"); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4557 | desc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4558 | input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4559 | } |
| 4560 | |
| 4561 | if (input != 0) { |
| 4562 | addInput(input, desc); |
| 4563 | } |
| 4564 | } // endif input != 0 |
| 4565 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4566 | if (input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4567 | ALOGW("checkInputsForDevice() could not open input for device 0x%X", device); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4568 | profiles.removeAt(profile_index); |
| 4569 | profile_index--; |
| 4570 | } else { |
| 4571 | inputs.add(input); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4572 | if (audio_device_is_digital(device)) { |
| 4573 | devDesc->importAudioPort(profile); |
| 4574 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4575 | ALOGV("checkInputsForDevice(): adding input %d", input); |
| 4576 | } |
| 4577 | } // end scan profiles |
| 4578 | |
| 4579 | if (profiles.isEmpty()) { |
| 4580 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 4581 | return BAD_VALUE; |
| 4582 | } |
| 4583 | } else { |
| 4584 | // Disconnect |
| 4585 | // check if one opened input is not needed any more after disconnecting one device |
| 4586 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4587 | desc = mInputs.valueAt(input_index); |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4588 | if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4589 | ALOGV("checkInputsForDevice(): disconnecting adding input %d", |
| 4590 | mInputs.keyAt(input_index)); |
| 4591 | inputs.add(mInputs.keyAt(input_index)); |
| 4592 | } |
| 4593 | } |
| 4594 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4595 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4596 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4597 | profile_index < hwModule->getInputProfiles().size(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4598 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4599 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4600 | if (profile->supportDevice(device)) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4601 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %s", |
| 4602 | profile_index, hwModule->getName()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4603 | profile->clearAudioProfiles(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4604 | } |
| 4605 | } |
| 4606 | } |
| 4607 | } // end disconnect |
| 4608 | |
| 4609 | return NO_ERROR; |
| 4610 | } |
| 4611 | |
| 4612 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4613 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4614 | { |
| 4615 | ALOGV("closeOutput(%d)", output); |
| 4616 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4617 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4618 | if (outputDesc == NULL) { |
| 4619 | ALOGW("closeOutput() unknown output %d", output); |
| 4620 | return; |
| 4621 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4622 | mPolicyMixes.closeOutput(outputDesc); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4623 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4624 | // look for duplicated outputs connected to the output being removed. |
| 4625 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4626 | sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4627 | if (dupOutputDesc->isDuplicated() && |
| 4628 | (dupOutputDesc->mOutput1 == outputDesc || |
| 4629 | dupOutputDesc->mOutput2 == outputDesc)) { |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4630 | sp<SwAudioOutputDescriptor> outputDesc2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4631 | if (dupOutputDesc->mOutput1 == outputDesc) { |
| 4632 | outputDesc2 = dupOutputDesc->mOutput2; |
| 4633 | } else { |
| 4634 | outputDesc2 = dupOutputDesc->mOutput1; |
| 4635 | } |
| 4636 | // As all active tracks on duplicated output will be deleted, |
| 4637 | // and as they were also referenced on the other output, the reference |
| 4638 | // count for their stream type must be adjusted accordingly on |
| 4639 | // the other output. |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4640 | bool wasActive = outputDesc2->isActive(); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4641 | for (int j = 0; j < AUDIO_STREAM_CNT; j++) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4642 | int refCount = dupOutputDesc->mRefCount[j]; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4643 | outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4644 | } |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4645 | // stop() will be a no op if the output is still active but is needed in case all |
| 4646 | // active streams refcounts where cleared above |
| 4647 | if (wasActive) { |
| 4648 | outputDesc2->stop(); |
| 4649 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4650 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); |
| 4651 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); |
| 4652 | |
| 4653 | mpClientInterface->closeOutput(duplicatedOutput); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4654 | removeOutput(duplicatedOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4655 | } |
| 4656 | } |
| 4657 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4658 | nextAudioPortGeneration(); |
| 4659 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4660 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4661 | if (index >= 0) { |
| 4662 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4663 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4664 | mAudioPatches.removeItemsAt(index); |
| 4665 | mpClientInterface->onAudioPatchListUpdate(); |
| 4666 | } |
| 4667 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4668 | outputDesc->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4669 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4670 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4671 | mPreviousOutputs = mOutputs; |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4672 | } |
| 4673 | |
| 4674 | void AudioPolicyManager::closeInput(audio_io_handle_t input) |
| 4675 | { |
| 4676 | ALOGV("closeInput(%d)", input); |
| 4677 | |
| 4678 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
| 4679 | if (inputDesc == NULL) { |
| 4680 | ALOGW("closeInput() unknown input %d", input); |
| 4681 | return; |
| 4682 | } |
| 4683 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4684 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4685 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4686 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4687 | if (index >= 0) { |
| 4688 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4689 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4690 | mAudioPatches.removeItemsAt(index); |
| 4691 | mpClientInterface->onAudioPatchListUpdate(); |
| 4692 | } |
| 4693 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4694 | inputDesc->close(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4695 | mInputs.removeItem(input); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4696 | } |
| 4697 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4698 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice( |
| 4699 | audio_devices_t device, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4700 | const SwAudioOutputCollection& openOutputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4701 | { |
| 4702 | SortedVector<audio_io_handle_t> outputs; |
| 4703 | |
| 4704 | ALOGVV("getOutputsForDevice() device %04x", device); |
| 4705 | for (size_t i = 0; i < openOutputs.size(); i++) { |
Eric Laurent | 37ddbb4 | 2016-08-10 16:19:14 -0700 | [diff] [blame] | 4706 | ALOGVV("output %zu isDuplicated=%d device=%04x", |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4707 | i, openOutputs.valueAt(i)->isDuplicated(), |
| 4708 | openOutputs.valueAt(i)->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4709 | if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) { |
| 4710 | ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i)); |
| 4711 | outputs.add(openOutputs.keyAt(i)); |
| 4712 | } |
| 4713 | } |
| 4714 | return outputs; |
| 4715 | } |
| 4716 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4717 | bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4718 | SortedVector<audio_io_handle_t>& outputs2) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4719 | { |
| 4720 | if (outputs1.size() != outputs2.size()) { |
| 4721 | return false; |
| 4722 | } |
| 4723 | for (size_t i = 0; i < outputs1.size(); i++) { |
| 4724 | if (outputs1[i] != outputs2[i]) { |
| 4725 | return false; |
| 4726 | } |
| 4727 | } |
| 4728 | return true; |
| 4729 | } |
| 4730 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4731 | void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4732 | { |
| 4733 | audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/); |
| 4734 | audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 4735 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs); |
| 4736 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs); |
| 4737 | |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4738 | // also take into account external policy-related changes: add all outputs which are |
| 4739 | // associated with policies in the "before" and "after" output vectors |
| 4740 | ALOGVV("checkOutputForStrategy(): policy related outputs"); |
| 4741 | for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4742 | const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4743 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4744 | srcOutputs.add(desc->mIoHandle); |
| 4745 | ALOGVV(" previous outputs: adding %d", desc->mIoHandle); |
| 4746 | } |
| 4747 | } |
| 4748 | for (size_t i = 0 ; i < mOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4749 | const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4750 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4751 | dstOutputs.add(desc->mIoHandle); |
| 4752 | ALOGVV(" new outputs: adding %d", desc->mIoHandle); |
| 4753 | } |
| 4754 | } |
| 4755 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4756 | if (!vectorsEqual(srcOutputs,dstOutputs)) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 4757 | // get maximum latency of all source outputs to determine the minimum mute time guaranteeing |
| 4758 | // audio from invalidated tracks will be rendered when unmuting |
| 4759 | uint32_t maxLatency = 0; |
| 4760 | for (audio_io_handle_t srcOut : srcOutputs) { |
| 4761 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); |
| 4762 | if (desc != 0 && maxLatency < desc->latency()) { |
| 4763 | maxLatency = desc->latency(); |
| 4764 | } |
| 4765 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4766 | ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d", |
| 4767 | strategy, srcOutputs[0], dstOutputs[0]); |
| 4768 | // mute strategy while moving tracks from one output to another |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4769 | for (audio_io_handle_t srcOut : srcOutputs) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 4770 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); |
| 4771 | if (desc != 0 && isStrategyActive(desc, strategy)) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4772 | setStrategyMute(strategy, true, desc); |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 4773 | setStrategyMute(strategy, false, desc, maxLatency * LATENCY_MUTE_FACTOR, newDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4774 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4775 | sp<AudioSourceDescriptor> source = |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4776 | getSourceForStrategyOnOutput(srcOut, strategy); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4777 | if (source != 0){ |
| 4778 | connectAudioSource(source); |
| 4779 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4780 | } |
| 4781 | |
| 4782 | // Move effects associated to this strategy from previous output to new output |
| 4783 | if (strategy == STRATEGY_MEDIA) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4784 | selectOutputForMusicEffects(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4785 | } |
| 4786 | // Move tracks associated to this strategy from previous output to new output |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4787 | for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4788 | if (getStrategy((audio_stream_type_t)i) == strategy) { |
| 4789 | mpClientInterface->invalidateStream((audio_stream_type_t)i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4790 | } |
| 4791 | } |
| 4792 | } |
| 4793 | } |
| 4794 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4795 | void AudioPolicyManager::checkOutputForAllStrategies() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4796 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4797 | 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] | 4798 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4799 | checkOutputForStrategy(STRATEGY_PHONE); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4800 | 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] | 4801 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4802 | checkOutputForStrategy(STRATEGY_SONIFICATION); |
| 4803 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4804 | checkOutputForStrategy(STRATEGY_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4805 | checkOutputForStrategy(STRATEGY_MEDIA); |
| 4806 | checkOutputForStrategy(STRATEGY_DTMF); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4807 | checkOutputForStrategy(STRATEGY_REROUTING); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4808 | } |
| 4809 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4810 | void AudioPolicyManager::checkA2dpSuspend() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4811 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4812 | audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput(); |
Aniket Kumar Lata | a8ee996 | 2018-01-31 20:24:23 -0800 | [diff] [blame] | 4813 | if (a2dpOutput == 0 || mOutputs.isA2dpOffloadedOnPrimary()) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4814 | mA2dpSuspended = false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4815 | return; |
| 4816 | } |
| 4817 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4818 | bool isScoConnected = |
Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 4819 | ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET & |
| 4820 | ~AUDIO_DEVICE_BIT_IN) != 0) || |
| 4821 | ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0); |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4822 | |
| 4823 | // if suspended, restore A2DP output if: |
| 4824 | // ((SCO device is NOT connected) || |
| 4825 | // ((forced usage communication is NOT SCO) && (forced usage for record is NOT SCO) && |
| 4826 | // (phone state is NOT in call) && (phone state is NOT ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4827 | // |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4828 | // if not suspended, suspend A2DP output if: |
| 4829 | // (SCO device is connected) && |
| 4830 | // ((forced usage for communication is SCO) || (forced usage for record is SCO) || |
| 4831 | // ((phone state is in call) || (phone state is ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4832 | // |
| 4833 | if (mA2dpSuspended) { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4834 | if (!isScoConnected || |
| 4835 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != |
| 4836 | AUDIO_POLICY_FORCE_BT_SCO) && |
| 4837 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != |
| 4838 | AUDIO_POLICY_FORCE_BT_SCO) && |
| 4839 | (mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4840 | (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4841 | |
| 4842 | mpClientInterface->restoreOutput(a2dpOutput); |
| 4843 | mA2dpSuspended = false; |
| 4844 | } |
| 4845 | } else { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4846 | if (isScoConnected && |
| 4847 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == |
| 4848 | AUDIO_POLICY_FORCE_BT_SCO) || |
| 4849 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == |
| 4850 | AUDIO_POLICY_FORCE_BT_SCO) || |
| 4851 | (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4852 | (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4853 | |
| 4854 | mpClientInterface->suspendOutput(a2dpOutput); |
| 4855 | mA2dpSuspended = true; |
| 4856 | } |
| 4857 | } |
| 4858 | } |
| 4859 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4860 | audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
| 4861 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4862 | { |
| 4863 | audio_devices_t device = AUDIO_DEVICE_NONE; |
| 4864 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4865 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4866 | if (index >= 0) { |
| 4867 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4868 | if (patchDesc->mUid != mUidCached) { |
| 4869 | ALOGV("getNewOutputDevice() device %08x forced by patch %d", |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4870 | outputDesc->device(), outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4871 | return outputDesc->device(); |
| 4872 | } |
| 4873 | } |
| 4874 | |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 4875 | // Check if an explicit routing request exists for an active stream on this output and |
| 4876 | // use it in priority before any other rule |
| 4877 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
| 4878 | if (outputDesc->isStreamActive((audio_stream_type_t)stream)) { |
| 4879 | audio_devices_t forcedDevice = |
| 4880 | mOutputRoutes.getActiveDeviceForStream( |
| 4881 | (audio_stream_type_t)stream, mAvailableOutputDevices); |
| 4882 | |
| 4883 | if (forcedDevice != AUDIO_DEVICE_NONE) { |
| 4884 | return forcedDevice; |
| 4885 | } |
| 4886 | } |
| 4887 | } |
| 4888 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4889 | // 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] | 4890 | // 1: the strategy enforced audible is active and enforced on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4891 | // use device for strategy enforced audible |
| 4892 | // 2: we are in call or the strategy phone is active on the output: |
| 4893 | // use device for strategy phone |
Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4894 | // 3: the strategy sonification is active on the output: |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4895 | // use device for strategy sonification |
Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4896 | // 4: the strategy for enforced audible is active but not enforced on the output: |
| 4897 | // use the device for strategy enforced audible |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4898 | // 5: the strategy accessibility is active on the output: |
| 4899 | // use device for strategy accessibility |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4900 | // 6: the strategy "respectful" sonification is active on the output: |
| 4901 | // use device for strategy "respectful" sonification |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4902 | // 7: the strategy media is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4903 | // use device for strategy media |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4904 | // 8: the strategy DTMF is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4905 | // use device for strategy DTMF |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4906 | // 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] | 4907 | // use device for strategy t-t-s |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 4908 | |
| 4909 | // FIXME: extend use of isStrategyActiveOnSameModule() to all strategies |
| 4910 | // with a refined rule considering mutually exclusive devices (using same backend) |
| 4911 | // as opposed to all streams on the same audio HAL module. |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4912 | if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4913 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4914 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
| 4915 | } else if (isInCall() || |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 4916 | isStrategyActiveOnSameModule(outputDesc, STRATEGY_PHONE)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4917 | device = getDeviceForStrategy(STRATEGY_PHONE, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4918 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4919 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache); |
Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4920 | } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) { |
| 4921 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4922 | } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) { |
| 4923 | device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4924 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4925 | device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4926 | } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4927 | device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4928 | } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4929 | device = getDeviceForStrategy(STRATEGY_DTMF, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4930 | } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4931 | device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4932 | } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4933 | device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4934 | } |
| 4935 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4936 | ALOGV("getNewOutputDevice() selected device %x", device); |
| 4937 | return device; |
| 4938 | } |
| 4939 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4940 | audio_devices_t AudioPolicyManager::getNewInputDevice(const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4941 | { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4942 | audio_devices_t device = AUDIO_DEVICE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4943 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4944 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4945 | if (index >= 0) { |
| 4946 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4947 | if (patchDesc->mUid != mUidCached) { |
| 4948 | ALOGV("getNewInputDevice() device %08x forced by patch %d", |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4949 | inputDesc->mDevice, inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4950 | return inputDesc->mDevice; |
| 4951 | } |
| 4952 | } |
| 4953 | |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 4954 | // If we are not in call and no client is active on this input, this methods returns |
| 4955 | // 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] | 4956 | audio_source_t source = inputDesc->getHighestPrioritySource(true /*activeOnly*/); |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 4957 | if (source == AUDIO_SOURCE_DEFAULT && isInCall()) { |
| 4958 | source = AUDIO_SOURCE_VOICE_COMMUNICATION; |
| 4959 | } |
| 4960 | if (source != AUDIO_SOURCE_DEFAULT) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4961 | device = getDeviceAndMixForInputSource(source); |
| 4962 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4963 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4964 | return device; |
| 4965 | } |
| 4966 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4967 | bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1, |
| 4968 | audio_stream_type_t stream2) { |
Jean-Michel Trivi | 99bb2f9 | 2016-11-23 15:52:07 -0800 | [diff] [blame] | 4969 | return (stream1 == stream2); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4970 | } |
| 4971 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4972 | uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4973 | return (uint32_t)getStrategy(stream); |
| 4974 | } |
| 4975 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4976 | audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4977 | // By checking the range of stream before calling getStrategy, we avoid |
| 4978 | // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE |
| 4979 | // 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] | 4980 | if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4981 | return AUDIO_DEVICE_NONE; |
| 4982 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4983 | audio_devices_t devices = AUDIO_DEVICE_NONE; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4984 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 4985 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4986 | continue; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4987 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4988 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4989 | audio_devices_t curDevices = |
Eric Laurent | 447a87b | 2016-07-07 17:32:10 -0700 | [diff] [blame] | 4990 | getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/); |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4991 | for (audio_io_handle_t output : getOutputsForDevice(curDevices, mOutputs)) { |
| 4992 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4993 | if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4994 | curDevices |= outputDesc->device(); |
| 4995 | } |
| 4996 | } |
| 4997 | devices |= curDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4998 | } |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4999 | |
| 5000 | /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it |
| 5001 | and doesn't really need to.*/ |
| 5002 | if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) { |
| 5003 | devices |= AUDIO_DEVICE_OUT_SPEAKER; |
| 5004 | devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
| 5005 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5006 | return devices; |
| 5007 | } |
| 5008 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5009 | routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const |
| 5010 | { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5011 | 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] | 5012 | return mEngine->getStrategyForStream(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5013 | } |
| 5014 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5015 | uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) { |
| 5016 | // flags to strategy mapping |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5017 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 5018 | return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER; |
| 5019 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5020 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 5021 | return (uint32_t) STRATEGY_ENFORCED_AUDIBLE; |
| 5022 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5023 | // usage to strategy mapping |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5024 | return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage)); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5025 | } |
| 5026 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5027 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5028 | switch(stream) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5029 | case AUDIO_STREAM_MUSIC: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5030 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
| 5031 | updateDevicesAndOutputs(); |
| 5032 | break; |
| 5033 | default: |
| 5034 | break; |
| 5035 | } |
| 5036 | } |
| 5037 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5038 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 5039 | |
| 5040 | // skip beacon mute management if a dedicated TTS output is available |
| 5041 | if (mTtsOutputAvailable) { |
| 5042 | return 0; |
| 5043 | } |
| 5044 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5045 | switch(event) { |
| 5046 | case STARTING_OUTPUT: |
| 5047 | mBeaconMuteRefCount++; |
| 5048 | break; |
| 5049 | case STOPPING_OUTPUT: |
| 5050 | if (mBeaconMuteRefCount > 0) { |
| 5051 | mBeaconMuteRefCount--; |
| 5052 | } |
| 5053 | break; |
| 5054 | case STARTING_BEACON: |
| 5055 | mBeaconPlayingRefCount++; |
| 5056 | break; |
| 5057 | case STOPPING_BEACON: |
| 5058 | if (mBeaconPlayingRefCount > 0) { |
| 5059 | mBeaconPlayingRefCount--; |
| 5060 | } |
| 5061 | break; |
| 5062 | } |
| 5063 | |
| 5064 | if (mBeaconMuteRefCount > 0) { |
| 5065 | // any playback causes beacon to be muted |
| 5066 | return setBeaconMute(true); |
| 5067 | } else { |
| 5068 | // no other playback: unmute when beacon starts playing, mute when it stops |
| 5069 | return setBeaconMute(mBeaconPlayingRefCount == 0); |
| 5070 | } |
| 5071 | } |
| 5072 | |
| 5073 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { |
| 5074 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", |
| 5075 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); |
| 5076 | // keep track of muted state to avoid repeating mute/unmute operations |
| 5077 | if (mBeaconMuted != mute) { |
| 5078 | // mute/unmute AUDIO_STREAM_TTS on all outputs |
| 5079 | ALOGV("\t muting %d", mute); |
| 5080 | uint32_t maxLatency = 0; |
| 5081 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5082 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5083 | setStreamMute(AUDIO_STREAM_TTS, mute/*on*/, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5084 | desc, |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5085 | 0 /*delay*/, AUDIO_DEVICE_NONE); |
| 5086 | const uint32_t latency = desc->latency() * 2; |
| 5087 | if (latency > maxLatency) { |
| 5088 | maxLatency = latency; |
| 5089 | } |
| 5090 | } |
| 5091 | mBeaconMuted = mute; |
| 5092 | return maxLatency; |
| 5093 | } |
| 5094 | return 0; |
| 5095 | } |
| 5096 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5097 | audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5098 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5099 | { |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 5100 | // Check if an explicit routing request exists for a stream type corresponding to the |
| 5101 | // specified strategy and use it in priority over default routing rules. |
| 5102 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
| 5103 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
| 5104 | audio_devices_t forcedDevice = |
| 5105 | mOutputRoutes.getActiveDeviceForStream( |
| 5106 | (audio_stream_type_t)stream, mAvailableOutputDevices); |
| 5107 | if (forcedDevice != AUDIO_DEVICE_NONE) { |
| 5108 | return forcedDevice; |
| 5109 | } |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 5110 | } |
| 5111 | } |
| 5112 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5113 | if (fromCache) { |
| 5114 | ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x", |
| 5115 | strategy, mDeviceForStrategy[strategy]); |
| 5116 | return mDeviceForStrategy[strategy]; |
| 5117 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5118 | return mEngine->getDeviceForStrategy(strategy); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5119 | } |
| 5120 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5121 | void AudioPolicyManager::updateDevicesAndOutputs() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5122 | { |
| 5123 | for (int i = 0; i < NUM_STRATEGIES; i++) { |
| 5124 | mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
| 5125 | } |
| 5126 | mPreviousOutputs = mOutputs; |
| 5127 | } |
| 5128 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5129 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5130 | audio_devices_t prevDevice, |
| 5131 | uint32_t delayMs) |
| 5132 | { |
| 5133 | // mute/unmute strategies using an incompatible device combination |
| 5134 | // if muting, wait for the audio in pcm buffer to be drained before proceeding |
| 5135 | // if unmuting, unmute only after the specified delay |
| 5136 | if (outputDesc->isDuplicated()) { |
| 5137 | return 0; |
| 5138 | } |
| 5139 | |
| 5140 | uint32_t muteWaitMs = 0; |
| 5141 | audio_devices_t device = outputDesc->device(); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5142 | bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5143 | |
| 5144 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
| 5145 | audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5146 | curDevice = curDevice & outputDesc->supportedDevices(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5147 | bool mute = shouldMute && (curDevice & device) && (curDevice != device); |
| 5148 | bool doMute = false; |
| 5149 | |
| 5150 | if (mute && !outputDesc->mStrategyMutedByDevice[i]) { |
| 5151 | doMute = true; |
| 5152 | outputDesc->mStrategyMutedByDevice[i] = true; |
| 5153 | } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){ |
| 5154 | doMute = true; |
| 5155 | outputDesc->mStrategyMutedByDevice[i] = false; |
| 5156 | } |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5157 | if (doMute) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5158 | for (size_t j = 0; j < mOutputs.size(); j++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5159 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5160 | // skip output if it does not share any device with current output |
| 5161 | if ((desc->supportedDevices() & outputDesc->supportedDevices()) |
| 5162 | == AUDIO_DEVICE_NONE) { |
| 5163 | continue; |
| 5164 | } |
Eric Laurent | 37ddbb4 | 2016-08-10 16:19:14 -0700 | [diff] [blame] | 5165 | ALOGVV("checkDeviceMuteStrategies() %s strategy %zu (curDevice %04x)", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5166 | mute ? "muting" : "unmuting", i, curDevice); |
| 5167 | setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5168 | if (isStrategyActive(desc, (routing_strategy)i)) { |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5169 | if (mute) { |
| 5170 | // FIXME: should not need to double latency if volume could be applied |
| 5171 | // immediately by the audioflinger mixer. We must account for the delay |
| 5172 | // between now and the next time the audioflinger thread for this output |
| 5173 | // will process a buffer (which corresponds to one buffer size, |
| 5174 | // usually 1/2 or 1/4 of the latency). |
| 5175 | if (muteWaitMs < desc->latency() * 2) { |
| 5176 | muteWaitMs = desc->latency() * 2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5177 | } |
| 5178 | } |
| 5179 | } |
| 5180 | } |
| 5181 | } |
| 5182 | } |
| 5183 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5184 | // temporary mute output if device selection changes to avoid volume bursts due to |
| 5185 | // different per device volumes |
| 5186 | if (outputDesc->isActive() && (device != prevDevice)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5187 | uint32_t tempMuteWaitMs = outputDesc->latency() * 2; |
| 5188 | // temporary mute duration is conservatively set to 4 times the reported latency |
| 5189 | uint32_t tempMuteDurationMs = outputDesc->latency() * 4; |
| 5190 | if (muteWaitMs < tempMuteWaitMs) { |
| 5191 | muteWaitMs = tempMuteWaitMs; |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5192 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5193 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5194 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5195 | if (isStrategyActive(outputDesc, (routing_strategy)i)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5196 | // make sure that we do not start the temporary mute period too early in case of |
| 5197 | // delayed device change |
| 5198 | setStrategyMute((routing_strategy)i, true, outputDesc, delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5199 | setStrategyMute((routing_strategy)i, false, outputDesc, |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5200 | delayMs + tempMuteDurationMs, device); |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5201 | } |
| 5202 | } |
| 5203 | } |
| 5204 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5205 | // wait for the PCM output buffers to empty before proceeding with the rest of the command |
| 5206 | if (muteWaitMs > delayMs) { |
| 5207 | muteWaitMs -= delayMs; |
| 5208 | usleep(muteWaitMs * 1000); |
| 5209 | return muteWaitMs; |
| 5210 | } |
| 5211 | return 0; |
| 5212 | } |
| 5213 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5214 | uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5215 | audio_devices_t device, |
| 5216 | bool force, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5217 | int delayMs, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5218 | audio_patch_handle_t *patchHandle, |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5219 | const char *address, |
| 5220 | bool requiresMuteCheck) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5221 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5222 | ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5223 | AudioParameter param; |
| 5224 | uint32_t muteWaitMs; |
| 5225 | |
| 5226 | if (outputDesc->isDuplicated()) { |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5227 | muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs, |
| 5228 | nullptr /* patchHandle */, nullptr /* address */, requiresMuteCheck); |
| 5229 | muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs, |
| 5230 | nullptr /* patchHandle */, nullptr /* address */, requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5231 | return muteWaitMs; |
| 5232 | } |
| 5233 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current |
| 5234 | // output profile |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5235 | if ((device != AUDIO_DEVICE_NONE) && |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5236 | ((device & outputDesc->supportedDevices()) == AUDIO_DEVICE_NONE)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5237 | return 0; |
| 5238 | } |
| 5239 | |
| 5240 | // filter devices according to output selected |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5241 | device = (audio_devices_t)(device & outputDesc->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5242 | |
| 5243 | audio_devices_t prevDevice = outputDesc->mDevice; |
| 5244 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 5245 | ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5246 | |
| 5247 | if (device != AUDIO_DEVICE_NONE) { |
| 5248 | outputDesc->mDevice = device; |
| 5249 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5250 | |
| 5251 | // if the outputs are not materially active, there is no need to mute. |
| 5252 | if (requiresMuteCheck) { |
| 5253 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs); |
| 5254 | } else { |
| 5255 | ALOGV("%s: suppressing checkDeviceMuteStrategies", __func__); |
| 5256 | muteWaitMs = 0; |
| 5257 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5258 | |
| 5259 | // Do not change the routing if: |
Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 5260 | // the requested device is AUDIO_DEVICE_NONE |
| 5261 | // OR the requested device is the same as current device |
| 5262 | // AND force is not specified |
| 5263 | // AND the output is connected by a valid audio patch. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5264 | // Doing this check here allows the caller to call setOutputDevice() without conditions |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 5265 | if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && |
| 5266 | !force && |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 5267 | outputDesc->getPatchHandle() != 0) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5268 | ALOGV("setOutputDevice() setting same device 0x%04x or null device", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5269 | return muteWaitMs; |
| 5270 | } |
| 5271 | |
| 5272 | ALOGV("setOutputDevice() changing device"); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5273 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5274 | // do the routing |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5275 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5276 | resetOutputDevice(outputDesc, delayMs, NULL); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5277 | } else { |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 5278 | DeviceVector deviceList; |
| 5279 | if ((address == NULL) || (strlen(address) == 0)) { |
| 5280 | deviceList = mAvailableOutputDevices.getDevicesFromType(device); |
| 5281 | } else { |
| 5282 | deviceList = mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address)); |
| 5283 | } |
| 5284 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5285 | if (!deviceList.isEmpty()) { |
| 5286 | struct audio_patch patch; |
| 5287 | outputDesc->toAudioPortConfig(&patch.sources[0]); |
| 5288 | patch.num_sources = 1; |
| 5289 | patch.num_sinks = 0; |
| 5290 | for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) { |
| 5291 | deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5292 | patch.num_sinks++; |
| 5293 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5294 | ssize_t index; |
| 5295 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { |
| 5296 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 5297 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 5298 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5299 | } |
| 5300 | sp< AudioPatch> patchDesc; |
| 5301 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 5302 | if (index >= 0) { |
| 5303 | patchDesc = mAudioPatches.valueAt(index); |
| 5304 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 5305 | } |
| 5306 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5307 | status_t status = mpClientInterface->createAudioPatch(&patch, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5308 | &afPatchHandle, |
| 5309 | delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5310 | ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d" |
| 5311 | "num_sources %d num_sinks %d", |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5312 | status, afPatchHandle, patch.num_sources, patch.num_sinks); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5313 | if (status == NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5314 | if (index < 0) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 5315 | patchDesc = new AudioPatch(&patch, mUidCached); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5316 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 5317 | } else { |
| 5318 | patchDesc->mPatch = patch; |
| 5319 | } |
| 5320 | patchDesc->mAfPatchHandle = afPatchHandle; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5321 | if (patchHandle) { |
| 5322 | *patchHandle = patchDesc->mHandle; |
| 5323 | } |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 5324 | outputDesc->setPatchHandle(patchDesc->mHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5325 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5326 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5327 | } |
| 5328 | } |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 5329 | |
| 5330 | // inform all input as well |
| 5331 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 5332 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5333 | if (!is_virtual_input_device(inputDescriptor->mDevice)) { |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 5334 | AudioParameter inputCmd = AudioParameter(); |
| 5335 | ALOGV("%s: inform input %d of device:%d", __func__, |
| 5336 | inputDescriptor->mIoHandle, device); |
| 5337 | inputCmd.addInt(String8(AudioParameter::keyRouting),device); |
| 5338 | mpClientInterface->setParameters(inputDescriptor->mIoHandle, |
| 5339 | inputCmd.toString(), |
| 5340 | delayMs); |
| 5341 | } |
| 5342 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5343 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5344 | |
| 5345 | // update stream volumes according to new device |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5346 | applyStreamVolumes(outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5347 | |
| 5348 | return muteWaitMs; |
| 5349 | } |
| 5350 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5351 | status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5352 | int delayMs, |
| 5353 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5354 | { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5355 | ssize_t index; |
| 5356 | if (patchHandle) { |
| 5357 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 5358 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 5359 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5360 | } |
| 5361 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5362 | return INVALID_OPERATION; |
| 5363 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5364 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 5365 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5366 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 5367 | outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5368 | removeAudioPatch(patchDesc->mHandle); |
| 5369 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5370 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5371 | return status; |
| 5372 | } |
| 5373 | |
| 5374 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, |
| 5375 | audio_devices_t device, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5376 | bool force, |
| 5377 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5378 | { |
| 5379 | status_t status = NO_ERROR; |
| 5380 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5381 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5382 | if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) { |
| 5383 | inputDesc->mDevice = device; |
| 5384 | |
| 5385 | DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device); |
| 5386 | if (!deviceList.isEmpty()) { |
| 5387 | struct audio_patch patch; |
| 5388 | inputDesc->toAudioPortConfig(&patch.sinks[0]); |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 5389 | // AUDIO_SOURCE_HOTWORD is for internal use only: |
| 5390 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 5391 | if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD && |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 5392 | !inputDesc->isSoundTrigger()) { |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 5393 | patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 5394 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5395 | patch.num_sinks = 1; |
| 5396 | //only one input device for now |
| 5397 | deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5398 | patch.num_sources = 1; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5399 | ssize_t index; |
| 5400 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { |
| 5401 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 5402 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 5403 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5404 | } |
| 5405 | sp< AudioPatch> patchDesc; |
| 5406 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 5407 | if (index >= 0) { |
| 5408 | patchDesc = mAudioPatches.valueAt(index); |
| 5409 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 5410 | } |
| 5411 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5412 | status_t status = mpClientInterface->createAudioPatch(&patch, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5413 | &afPatchHandle, |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5414 | 0); |
| 5415 | ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d", |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5416 | status, afPatchHandle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5417 | if (status == NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5418 | if (index < 0) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 5419 | patchDesc = new AudioPatch(&patch, mUidCached); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5420 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 5421 | } else { |
| 5422 | patchDesc->mPatch = patch; |
| 5423 | } |
| 5424 | patchDesc->mAfPatchHandle = afPatchHandle; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5425 | if (patchHandle) { |
| 5426 | *patchHandle = patchDesc->mHandle; |
| 5427 | } |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 5428 | inputDesc->setPatchHandle(patchDesc->mHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5429 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5430 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5431 | } |
| 5432 | } |
| 5433 | } |
| 5434 | return status; |
| 5435 | } |
| 5436 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5437 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, |
| 5438 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5439 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5440 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5441 | ssize_t index; |
| 5442 | if (patchHandle) { |
| 5443 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 5444 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 5445 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5446 | } |
| 5447 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5448 | return INVALID_OPERATION; |
| 5449 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5450 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 5451 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5452 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 5453 | inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5454 | removeAudioPatch(patchDesc->mHandle); |
| 5455 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5456 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5457 | return status; |
| 5458 | } |
| 5459 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 5460 | sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5461 | const String8& address, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5462 | uint32_t& samplingRate, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5463 | audio_format_t& format, |
| 5464 | audio_channel_mask_t& channelMask, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5465 | audio_input_flags_t flags) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5466 | { |
| 5467 | // Choose an input profile based on the requested capture parameters: select the first available |
| 5468 | // profile supporting all requested parameters. |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5469 | // |
| 5470 | // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return |
| 5471 | // the best matching profile, not the first one. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5472 | |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5473 | sp<IOProfile> firstInexact; |
| 5474 | uint32_t updatedSamplingRate = 0; |
| 5475 | audio_format_t updatedFormat = AUDIO_FORMAT_INVALID; |
| 5476 | audio_channel_mask_t updatedChannelMask = AUDIO_CHANNEL_INVALID; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 5477 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 5478 | for (const auto& profile : hwModule->getInputProfiles()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5479 | // profile->log(); |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5480 | //updatedFormat = format; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5481 | if (profile->isCompatibleProfile(device, address, samplingRate, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5482 | &samplingRate /*updatedSamplingRate*/, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5483 | format, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5484 | &format, /*updatedFormat*/ |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5485 | channelMask, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5486 | &channelMask /*updatedChannelMask*/, |
| 5487 | // FIXME ugly cast |
| 5488 | (audio_output_flags_t) flags, |
| 5489 | true /*exactMatchRequiredForInputFlags*/)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5490 | return profile; |
| 5491 | } |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5492 | if (firstInexact == nullptr && profile->isCompatibleProfile(device, address, |
| 5493 | samplingRate, |
| 5494 | &updatedSamplingRate, |
| 5495 | format, |
| 5496 | &updatedFormat, |
| 5497 | channelMask, |
| 5498 | &updatedChannelMask, |
| 5499 | // FIXME ugly cast |
| 5500 | (audio_output_flags_t) flags, |
| 5501 | false /*exactMatchRequiredForInputFlags*/)) { |
| 5502 | firstInexact = profile; |
| 5503 | } |
| 5504 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5505 | } |
| 5506 | } |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5507 | if (firstInexact != nullptr) { |
| 5508 | samplingRate = updatedSamplingRate; |
| 5509 | format = updatedFormat; |
| 5510 | channelMask = updatedChannelMask; |
| 5511 | return firstInexact; |
| 5512 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5513 | return NULL; |
| 5514 | } |
| 5515 | |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5516 | |
| 5517 | audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5518 | AudioMix **policyMix) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5519 | { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 5520 | audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
| 5521 | audio_devices_t selectedDeviceFromMix = |
| 5522 | mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5523 | |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 5524 | if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) { |
| 5525 | return selectedDeviceFromMix; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5526 | } |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5527 | return getDeviceForInputSource(inputSource); |
| 5528 | } |
| 5529 | |
| 5530 | audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource) |
| 5531 | { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 5532 | // Routing |
| 5533 | // Scan the whole RouteMap to see if we have an explicit route: |
| 5534 | // if the input source in the RouteMap is the same as the argument above, |
| 5535 | // and activity count is non-zero and the device in the route descriptor is available |
| 5536 | // then select this device. |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 5537 | for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) { |
| 5538 | sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex); |
Eric Laurent | 2157f5b | 2018-04-25 18:33:02 -0700 | [diff] [blame] | 5539 | if ((inputSource == route->mSource) && route->isActiveOrChanged() && |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 5540 | (mAvailableInputDevices.indexOf(route->mDeviceDescriptor) >= 0)) { |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 5541 | return route->mDeviceDescriptor->type(); |
| 5542 | } |
| 5543 | } |
| 5544 | |
| 5545 | return mEngine->getDeviceForInputSource(inputSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5546 | } |
| 5547 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5548 | float AudioPolicyManager::computeVolume(audio_stream_type_t stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5549 | int index, |
| 5550 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5551 | { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5552 | float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index); |
Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 5553 | |
| 5554 | // handle the case of accessibility active while a ringtone is playing: if the ringtone is much |
| 5555 | // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch |
| 5556 | // exploration of the dialer UI. In this situation, bring the accessibility volume closer to |
| 5557 | // the ringtone volume |
| 5558 | if ((stream == AUDIO_STREAM_ACCESSIBILITY) |
| 5559 | && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState()) |
| 5560 | && isStreamActive(AUDIO_STREAM_RING, 0)) { |
| 5561 | const float ringVolumeDB = computeVolume(AUDIO_STREAM_RING, index, device); |
| 5562 | return ringVolumeDB - 4 > volumeDB ? ringVolumeDB - 4 : volumeDB; |
| 5563 | } |
| 5564 | |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 5565 | // in-call: always cap earpiece volume by voice volume + some low headroom |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5566 | if ((stream != AUDIO_STREAM_VOICE_CALL) && (device & AUDIO_DEVICE_OUT_EARPIECE) && |
| 5567 | (isInCall() || mOutputs.isStreamActiveLocally(AUDIO_STREAM_VOICE_CALL))) { |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 5568 | switch (stream) { |
| 5569 | case AUDIO_STREAM_SYSTEM: |
| 5570 | case AUDIO_STREAM_RING: |
| 5571 | case AUDIO_STREAM_MUSIC: |
| 5572 | case AUDIO_STREAM_ALARM: |
| 5573 | case AUDIO_STREAM_NOTIFICATION: |
| 5574 | case AUDIO_STREAM_ENFORCED_AUDIBLE: |
| 5575 | case AUDIO_STREAM_DTMF: |
| 5576 | case AUDIO_STREAM_ACCESSIBILITY: { |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5577 | int voiceVolumeIndex = |
| 5578 | mVolumeCurves->getVolumeIndex(AUDIO_STREAM_VOICE_CALL, AUDIO_DEVICE_OUT_EARPIECE); |
| 5579 | const float maxVoiceVolDb = |
| 5580 | computeVolume(AUDIO_STREAM_VOICE_CALL, voiceVolumeIndex, AUDIO_DEVICE_OUT_EARPIECE) |
| 5581 | + IN_CALL_EARPIECE_HEADROOM_DB; |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 5582 | if (volumeDB > maxVoiceVolDb) { |
| 5583 | ALOGV("computeVolume() stream %d at vol=%f overriden by stream %d at vol=%f", |
| 5584 | stream, volumeDB, AUDIO_STREAM_VOICE_CALL, maxVoiceVolDb); |
| 5585 | volumeDB = maxVoiceVolDb; |
| 5586 | } |
| 5587 | } break; |
| 5588 | default: |
| 5589 | break; |
| 5590 | } |
| 5591 | } |
| 5592 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5593 | // if a headset is connected, apply the following rules to ring tones and notifications |
| 5594 | // to avoid sound level bursts in user's ears: |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5595 | // - always attenuate notifications volume by 6dB |
| 5596 | // - attenuate ring tones volume by 6dB unless music is not playing and |
| 5597 | // speaker is part of the select devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5598 | // - if music is playing, always limit the volume to current music volume, |
| 5599 | // with a minimum threshold at -36dB so that notification is always perceived. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5600 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5601 | if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 5602 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | |
| 5603 | AUDIO_DEVICE_OUT_WIRED_HEADSET | |
Eric Laurent | 904d632 | 2017-03-17 17:20:47 -0700 | [diff] [blame] | 5604 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE | |
Jakub Pawlowski | 00f928c | 2018-03-22 13:20:03 -0700 | [diff] [blame] | 5605 | AUDIO_DEVICE_OUT_USB_HEADSET | |
| 5606 | AUDIO_DEVICE_OUT_HEARING_AID)) && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5607 | ((stream_strategy == STRATEGY_SONIFICATION) |
| 5608 | || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL) |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5609 | || (stream == AUDIO_STREAM_SYSTEM) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5610 | || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5611 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) && |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5612 | mVolumeCurves->canBeMuted(stream)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5613 | // when the phone is ringing we must consider that music could have been paused just before |
| 5614 | // by the music application and behave as if music was active if the last music track was |
| 5615 | // just stopped |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5616 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5617 | mLimitRingtoneVolume) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5618 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5619 | audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5620 | float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5621 | mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC, |
| 5622 | musicDevice), |
| 5623 | musicDevice); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5624 | float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ? |
| 5625 | musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB; |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5626 | if (volumeDB > minVolDB) { |
| 5627 | volumeDB = minVolDB; |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5628 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5629 | } |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5630 | if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 5631 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) { |
| 5632 | // on A2DP, also ensure notification volume is not too low compared to media when |
| 5633 | // intended to be played |
| 5634 | if ((volumeDB > -96.0f) && |
| 5635 | (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) { |
| 5636 | ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f", |
| 5637 | stream, device, |
| 5638 | volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB); |
| 5639 | volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB; |
| 5640 | } |
| 5641 | } |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5642 | } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) || |
| 5643 | stream_strategy != STRATEGY_SONIFICATION) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5644 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5645 | } |
| 5646 | } |
| 5647 | |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5648 | return volumeDB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5649 | } |
| 5650 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5651 | status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5652 | int index, |
| 5653 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5654 | audio_devices_t device, |
| 5655 | int delayMs, |
| 5656 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5657 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5658 | // do not change actual stream volume if the stream is muted |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5659 | if (outputDesc->mMuteCount[stream] != 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5660 | ALOGVV("checkAndSetVolume() stream %d muted count %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5661 | stream, outputDesc->mMuteCount[stream]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5662 | return NO_ERROR; |
| 5663 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5664 | audio_policy_forced_cfg_t forceUseForComm = |
| 5665 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5666 | // 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] | 5667 | if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) || |
| 5668 | (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5669 | 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] | 5670 | stream, forceUseForComm); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5671 | return INVALID_OPERATION; |
| 5672 | } |
| 5673 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5674 | if (device == AUDIO_DEVICE_NONE) { |
| 5675 | device = outputDesc->device(); |
| 5676 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5677 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5678 | float volumeDb = computeVolume(stream, index, device); |
HW Lee | da7581e | 2018-05-22 18:31:34 +0800 | [diff] [blame] | 5679 | if (outputDesc->isFixedVolume(device) || |
| 5680 | // Force VoIP volume to max for bluetooth SCO |
| 5681 | ((stream == AUDIO_STREAM_VOICE_CALL || stream == AUDIO_STREAM_BLUETOOTH_SCO) && |
| 5682 | (device & AUDIO_DEVICE_OUT_ALL_SCO) != 0)) { |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5683 | volumeDb = 0.0f; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5684 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5685 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5686 | outputDesc->setVolume(volumeDb, stream, device, delayMs, force); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5687 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5688 | if (stream == AUDIO_STREAM_VOICE_CALL || |
| 5689 | stream == AUDIO_STREAM_BLUETOOTH_SCO) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5690 | float voiceVolume; |
| 5691 | // 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] | 5692 | if (stream == AUDIO_STREAM_VOICE_CALL) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5693 | voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5694 | } else { |
| 5695 | voiceVolume = 1.0; |
| 5696 | } |
| 5697 | |
Eric Laurent | 18fba84 | 2016-03-31 14:41:26 -0700 | [diff] [blame] | 5698 | if (voiceVolume != mLastVoiceVolume) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5699 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); |
| 5700 | mLastVoiceVolume = voiceVolume; |
| 5701 | } |
| 5702 | } |
| 5703 | |
| 5704 | return NO_ERROR; |
| 5705 | } |
| 5706 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5707 | void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc, |
| 5708 | audio_devices_t device, |
| 5709 | int delayMs, |
| 5710 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5711 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5712 | ALOGVV("applyStreamVolumes() for device %08x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5713 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5714 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5715 | checkAndSetVolume((audio_stream_type_t)stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5716 | mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5717 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5718 | device, |
| 5719 | delayMs, |
| 5720 | force); |
| 5721 | } |
| 5722 | } |
| 5723 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5724 | void AudioPolicyManager::setStrategyMute(routing_strategy strategy, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5725 | bool on, |
| 5726 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5727 | int delayMs, |
| 5728 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5729 | { |
Eric Laurent | 72249d5 | 2015-06-08 11:12:15 -0700 | [diff] [blame] | 5730 | ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d", |
| 5731 | strategy, on, outputDesc->getId()); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5732 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5733 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5734 | setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5735 | } |
| 5736 | } |
| 5737 | } |
| 5738 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5739 | void AudioPolicyManager::setStreamMute(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5740 | bool on, |
| 5741 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5742 | int delayMs, |
| 5743 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5744 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5745 | if (device == AUDIO_DEVICE_NONE) { |
| 5746 | device = outputDesc->device(); |
| 5747 | } |
| 5748 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5749 | ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x", |
| 5750 | stream, on, outputDesc->mMuteCount[stream], device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5751 | |
| 5752 | if (on) { |
| 5753 | if (outputDesc->mMuteCount[stream] == 0) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5754 | if (mVolumeCurves->canBeMuted(stream) && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5755 | ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5756 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5757 | checkAndSetVolume(stream, 0, outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5758 | } |
| 5759 | } |
| 5760 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored |
| 5761 | outputDesc->mMuteCount[stream]++; |
| 5762 | } else { |
| 5763 | if (outputDesc->mMuteCount[stream] == 0) { |
| 5764 | ALOGV("setStreamMute() unmuting non muted stream!"); |
| 5765 | return; |
| 5766 | } |
| 5767 | if (--outputDesc->mMuteCount[stream] == 0) { |
| 5768 | checkAndSetVolume(stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5769 | mVolumeCurves->getVolumeIndex(stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5770 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5771 | device, |
| 5772 | delayMs); |
| 5773 | } |
| 5774 | } |
| 5775 | } |
| 5776 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5777 | void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5778 | bool starting, bool stateChange) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5779 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 5780 | if(!hasPrimaryOutput()) { |
| 5781 | return; |
| 5782 | } |
| 5783 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5784 | // if the stream pertains to sonification strategy and we are in call we must |
| 5785 | // mute the stream if it is low visibility. If it is high visibility, we must play a tone |
| 5786 | // in the device used for phone strategy and play the tone if the selected device does not |
| 5787 | // interfere with the device used for phone strategy |
| 5788 | // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as |
| 5789 | // many times as there are active tracks on the output |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5790 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5791 | if ((stream_strategy == STRATEGY_SONIFICATION) || |
| 5792 | ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5793 | sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5794 | ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d", |
| 5795 | stream, starting, outputDesc->mDevice, stateChange); |
| 5796 | if (outputDesc->mRefCount[stream]) { |
| 5797 | int muteCount = 1; |
| 5798 | if (stateChange) { |
| 5799 | muteCount = outputDesc->mRefCount[stream]; |
| 5800 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5801 | if (audio_is_low_visibility(stream)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5802 | ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount); |
| 5803 | for (int i = 0; i < muteCount; i++) { |
| 5804 | setStreamMute(stream, starting, mPrimaryOutput); |
| 5805 | } |
| 5806 | } else { |
| 5807 | ALOGV("handleIncallSonification() high visibility"); |
| 5808 | if (outputDesc->device() & |
| 5809 | getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) { |
| 5810 | ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount); |
| 5811 | for (int i = 0; i < muteCount; i++) { |
| 5812 | setStreamMute(stream, starting, mPrimaryOutput); |
| 5813 | } |
| 5814 | } |
| 5815 | if (starting) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5816 | mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION, |
| 5817 | AUDIO_STREAM_VOICE_CALL); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5818 | } else { |
| 5819 | mpClientInterface->stopTone(); |
| 5820 | } |
| 5821 | } |
| 5822 | } |
| 5823 | } |
| 5824 | } |
| 5825 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5826 | audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr) |
| 5827 | { |
| 5828 | // flags to stream type mapping |
| 5829 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 5830 | return AUDIO_STREAM_ENFORCED_AUDIBLE; |
| 5831 | } |
| 5832 | if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) { |
| 5833 | return AUDIO_STREAM_BLUETOOTH_SCO; |
| 5834 | } |
Jean-Michel Trivi | 79ad438 | 2015-01-29 10:49:39 -0800 | [diff] [blame] | 5835 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 5836 | return AUDIO_STREAM_TTS; |
| 5837 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5838 | |
| 5839 | // usage to stream type mapping |
| 5840 | switch (attr->usage) { |
| 5841 | case AUDIO_USAGE_MEDIA: |
| 5842 | case AUDIO_USAGE_GAME: |
Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 5843 | case AUDIO_USAGE_ASSISTANT: |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5844 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 5845 | return AUDIO_STREAM_MUSIC; |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5846 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 5847 | return AUDIO_STREAM_ACCESSIBILITY; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5848 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 5849 | return AUDIO_STREAM_SYSTEM; |
| 5850 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 5851 | return AUDIO_STREAM_VOICE_CALL; |
| 5852 | |
| 5853 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 5854 | return AUDIO_STREAM_DTMF; |
| 5855 | |
| 5856 | case AUDIO_USAGE_ALARM: |
| 5857 | return AUDIO_STREAM_ALARM; |
| 5858 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 5859 | return AUDIO_STREAM_RING; |
| 5860 | |
| 5861 | case AUDIO_USAGE_NOTIFICATION: |
| 5862 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 5863 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 5864 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 5865 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 5866 | return AUDIO_STREAM_NOTIFICATION; |
| 5867 | |
| 5868 | case AUDIO_USAGE_UNKNOWN: |
| 5869 | default: |
| 5870 | return AUDIO_STREAM_MUSIC; |
| 5871 | } |
| 5872 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5873 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5874 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) |
| 5875 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5876 | // has flags that map to a strategy? |
| 5877 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { |
| 5878 | return true; |
| 5879 | } |
| 5880 | |
| 5881 | // has known usage? |
| 5882 | switch (paa->usage) { |
| 5883 | case AUDIO_USAGE_UNKNOWN: |
| 5884 | case AUDIO_USAGE_MEDIA: |
| 5885 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 5886 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 5887 | case AUDIO_USAGE_ALARM: |
| 5888 | case AUDIO_USAGE_NOTIFICATION: |
| 5889 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 5890 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 5891 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 5892 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 5893 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 5894 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 5895 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 5896 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 5897 | case AUDIO_USAGE_GAME: |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5898 | case AUDIO_USAGE_VIRTUAL_SOURCE: |
Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 5899 | case AUDIO_USAGE_ASSISTANT: |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5900 | break; |
| 5901 | default: |
| 5902 | return false; |
| 5903 | } |
| 5904 | return true; |
| 5905 | } |
| 5906 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5907 | bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor>& outputDesc, |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5908 | routing_strategy strategy, uint32_t inPastMs, |
| 5909 | nsecs_t sysTime) const |
| 5910 | { |
| 5911 | if ((sysTime == 0) && (inPastMs != 0)) { |
| 5912 | sysTime = systemTime(); |
| 5913 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5914 | 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] | 5915 | if (((getStrategy((audio_stream_type_t)i) == strategy) || |
| 5916 | (NUM_STRATEGIES == strategy)) && |
| 5917 | outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) { |
| 5918 | return true; |
| 5919 | } |
| 5920 | } |
| 5921 | return false; |
| 5922 | } |
| 5923 | |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 5924 | bool AudioPolicyManager::isStrategyActiveOnSameModule(const sp<AudioOutputDescriptor>& outputDesc, |
| 5925 | routing_strategy strategy, uint32_t inPastMs, |
| 5926 | nsecs_t sysTime) const |
| 5927 | { |
| 5928 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5929 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 5930 | if (outputDesc->sharesHwModuleWith(desc) |
| 5931 | && isStrategyActive(desc, strategy, inPastMs, sysTime)) { |
| 5932 | return true; |
| 5933 | } |
| 5934 | } |
| 5935 | return false; |
| 5936 | } |
| 5937 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5938 | audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage) |
| 5939 | { |
| 5940 | return mEngine->getForceUse(usage); |
| 5941 | } |
| 5942 | |
| 5943 | bool AudioPolicyManager::isInCall() |
| 5944 | { |
| 5945 | return isStateInCall(mEngine->getPhoneState()); |
| 5946 | } |
| 5947 | |
| 5948 | bool AudioPolicyManager::isStateInCall(int state) |
| 5949 | { |
| 5950 | return is_state_in_call(state); |
| 5951 | } |
| 5952 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5953 | void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc) |
| 5954 | { |
| 5955 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
| 5956 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 5957 | if (sourceDesc->mDevice->equals(deviceDesc)) { |
| 5958 | ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle()); |
| 5959 | stopAudioSource(sourceDesc->getHandle()); |
| 5960 | } |
| 5961 | } |
| 5962 | |
| 5963 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
| 5964 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 5965 | bool release = false; |
| 5966 | for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) { |
| 5967 | const struct audio_port_config *source = &patchDesc->mPatch.sources[j]; |
| 5968 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 5969 | source->ext.device.type == deviceDesc->type()) { |
| 5970 | release = true; |
| 5971 | } |
| 5972 | } |
| 5973 | for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) { |
| 5974 | const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j]; |
| 5975 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && |
| 5976 | sink->ext.device.type == deviceDesc->type()) { |
| 5977 | release = true; |
| 5978 | } |
| 5979 | } |
| 5980 | if (release) { |
| 5981 | ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle); |
| 5982 | releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid); |
| 5983 | } |
| 5984 | } |
| 5985 | } |
| 5986 | |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5987 | // Modify the list of surround sound formats supported. |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5988 | void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) { |
| 5989 | FormatVector &formats = *formatsPtr; |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5990 | // TODO Set this based on Config properties. |
| 5991 | const bool alwaysForceAC3 = true; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5992 | |
| 5993 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5994 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5995 | ALOGD("%s: forced use = %d", __FUNCTION__, forceUse); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5996 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5997 | // If MANUAL, keep the supported surround sound formats as current enabled ones. |
| 5998 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
| 5999 | formats.clear(); |
| 6000 | for (auto it = mSurroundFormats.begin(); it != mSurroundFormats.end(); it++) { |
| 6001 | formats.add(*it); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 6002 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6003 | // Always enable IEC61937 when in MANUAL mode. |
| 6004 | formats.add(AUDIO_FORMAT_IEC61937); |
| 6005 | } else { // NEVER, AUTO or ALWAYS |
| 6006 | // Analyze original support for various formats. |
| 6007 | bool supportsAC3 = false; |
| 6008 | bool supportsOtherSurround = false; |
| 6009 | bool supportsIEC61937 = false; |
| 6010 | mSurroundFormats.clear(); |
| 6011 | for (ssize_t formatIndex = 0; formatIndex < (ssize_t)formats.size(); formatIndex++) { |
| 6012 | audio_format_t format = formats[formatIndex]; |
| 6013 | switch (format) { |
| 6014 | case AUDIO_FORMAT_AC3: |
| 6015 | supportsAC3 = true; |
| 6016 | break; |
| 6017 | case AUDIO_FORMAT_E_AC3: |
| 6018 | case AUDIO_FORMAT_DTS: |
| 6019 | case AUDIO_FORMAT_DTS_HD: |
| 6020 | // If ALWAYS, remove all other surround formats here |
| 6021 | // since we will add them later. |
| 6022 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { |
| 6023 | formats.removeAt(formatIndex); |
| 6024 | formatIndex--; |
| 6025 | } |
| 6026 | supportsOtherSurround = true; |
| 6027 | break; |
| 6028 | case AUDIO_FORMAT_IEC61937: |
| 6029 | supportsIEC61937 = true; |
| 6030 | break; |
| 6031 | default: |
| 6032 | break; |
| 6033 | } |
| 6034 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 6035 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6036 | // Modify formats based on surround preferences. |
| 6037 | // If NEVER, remove support for surround formats. |
| 6038 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
| 6039 | if (supportsAC3 || supportsOtherSurround || supportsIEC61937) { |
| 6040 | // Remove surround sound related formats. |
| 6041 | for (size_t formatIndex = 0; formatIndex < formats.size(); ) { |
| 6042 | audio_format_t format = formats[formatIndex]; |
| 6043 | switch(format) { |
| 6044 | case AUDIO_FORMAT_AC3: |
| 6045 | case AUDIO_FORMAT_E_AC3: |
| 6046 | case AUDIO_FORMAT_DTS: |
| 6047 | case AUDIO_FORMAT_DTS_HD: |
| 6048 | case AUDIO_FORMAT_IEC61937: |
| 6049 | formats.removeAt(formatIndex); |
| 6050 | break; |
| 6051 | default: |
| 6052 | formatIndex++; // keep it |
| 6053 | break; |
| 6054 | } |
| 6055 | } |
| 6056 | supportsAC3 = false; |
| 6057 | supportsOtherSurround = false; |
| 6058 | supportsIEC61937 = false; |
| 6059 | } |
| 6060 | } else { // AUTO or ALWAYS |
| 6061 | // Most TVs support AC3 even if they do not report it in the EDID. |
| 6062 | if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)) |
| 6063 | && !supportsAC3) { |
| 6064 | formats.add(AUDIO_FORMAT_AC3); |
| 6065 | supportsAC3 = true; |
| 6066 | } |
| 6067 | |
| 6068 | // If ALWAYS, add support for raw surround formats if all are missing. |
| 6069 | // This assumes that if any of these formats are reported by the HAL |
| 6070 | // then the report is valid and should not be modified. |
| 6071 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { |
| 6072 | formats.add(AUDIO_FORMAT_E_AC3); |
| 6073 | formats.add(AUDIO_FORMAT_DTS); |
| 6074 | formats.add(AUDIO_FORMAT_DTS_HD); |
| 6075 | supportsOtherSurround = true; |
| 6076 | } |
| 6077 | |
| 6078 | // Add support for IEC61937 if any raw surround supported. |
| 6079 | // The HAL could do this but add it here, just in case. |
| 6080 | if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) { |
| 6081 | formats.add(AUDIO_FORMAT_IEC61937); |
| 6082 | supportsIEC61937 = true; |
| 6083 | } |
| 6084 | |
| 6085 | // Add reported surround sound formats to enabled surround formats. |
| 6086 | for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) { |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 6087 | audio_format_t format = formats[formatIndex]; |
| 6088 | switch(format) { |
| 6089 | case AUDIO_FORMAT_AC3: |
| 6090 | case AUDIO_FORMAT_E_AC3: |
| 6091 | case AUDIO_FORMAT_DTS: |
| 6092 | case AUDIO_FORMAT_DTS_HD: |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6093 | case AUDIO_FORMAT_AAC_LC: |
| 6094 | case AUDIO_FORMAT_DOLBY_TRUEHD: |
| 6095 | case AUDIO_FORMAT_E_AC3_JOC: |
| 6096 | mSurroundFormats.insert(format); |
Chih-Hung Hsieh | 3939960 | 2018-10-16 14:42:13 -0700 | [diff] [blame] | 6097 | break; |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 6098 | default: |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 6099 | break; |
| 6100 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 6101 | } |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 6102 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 6103 | } |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6104 | } |
| 6105 | |
| 6106 | // Modify the list of channel masks supported. |
| 6107 | void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) { |
| 6108 | ChannelsVector &channelMasks = *channelMasksPtr; |
| 6109 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 6110 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
| 6111 | |
| 6112 | // If NEVER, then remove support for channelMasks > stereo. |
| 6113 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
| 6114 | for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) { |
| 6115 | audio_channel_mask_t channelMask = channelMasks[maskIndex]; |
| 6116 | if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) { |
| 6117 | ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask); |
| 6118 | channelMasks.removeAt(maskIndex); |
| 6119 | } else { |
| 6120 | maskIndex++; |
| 6121 | } |
| 6122 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6123 | // If ALWAYS or MANUAL, then make sure we at least support 5.1 |
| 6124 | } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS |
| 6125 | || forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6126 | bool supports5dot1 = false; |
| 6127 | // Are there any channel masks that can be considered "surround"? |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 6128 | for (audio_channel_mask_t channelMask : channelMasks) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6129 | if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) { |
| 6130 | supports5dot1 = true; |
| 6131 | break; |
| 6132 | } |
| 6133 | } |
| 6134 | // If not then add 5.1 support. |
| 6135 | if (!supports5dot1) { |
| 6136 | channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1); |
| 6137 | ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__); |
| 6138 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 6139 | } |
| 6140 | } |
| 6141 | |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 6142 | void AudioPolicyManager::updateAudioProfiles(audio_devices_t device, |
| 6143 | audio_io_handle_t ioHandle, |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6144 | AudioProfileVector &profiles) |
| 6145 | { |
| 6146 | String8 reply; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6147 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6148 | // Format MUST be checked first to update the list of AudioProfile |
| 6149 | if (profiles.hasDynamicFormat()) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6150 | reply = mpClientInterface->getParameters( |
| 6151 | ioHandle, String8(AudioParameter::keyStreamSupportedFormats)); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6152 | ALOGV("%s: supported formats %d, %s", __FUNCTION__, ioHandle, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 6153 | AudioParameter repliedParameters(reply); |
| 6154 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6155 | String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) { |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6156 | ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__); |
| 6157 | return; |
| 6158 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 6159 | FormatVector formats = formatsFromString(reply.string()); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 6160 | if (device == AUDIO_DEVICE_OUT_HDMI) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6161 | filterSurroundFormats(&formats); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 6162 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 6163 | profiles.setFormats(formats); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6164 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6165 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 6166 | for (audio_format_t format : profiles.getSupportedFormats()) { |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 6167 | ChannelsVector channelMasks; |
| 6168 | SampleRateVector samplingRates; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6169 | AudioParameter requestedParameters; |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6170 | requestedParameters.addInt(String8(AudioParameter::keyFormat), format); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6171 | |
| 6172 | if (profiles.hasDynamicRateFor(format)) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6173 | reply = mpClientInterface->getParameters( |
| 6174 | ioHandle, |
| 6175 | requestedParameters.toString() + ";" + |
| 6176 | AudioParameter::keyStreamSupportedSamplingRates); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6177 | ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 6178 | AudioParameter repliedParameters(reply); |
| 6179 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6180 | String8(AudioParameter::keyStreamSupportedSamplingRates), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 6181 | samplingRates = samplingRatesFromString(reply.string()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6182 | } |
| 6183 | } |
| 6184 | if (profiles.hasDynamicChannelsFor(format)) { |
| 6185 | reply = mpClientInterface->getParameters(ioHandle, |
| 6186 | requestedParameters.toString() + ";" + |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6187 | AudioParameter::keyStreamSupportedChannels); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6188 | ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 6189 | AudioParameter repliedParameters(reply); |
| 6190 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6191 | String8(AudioParameter::keyStreamSupportedChannels), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 6192 | channelMasks = channelMasksFromString(reply.string()); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6193 | if (device == AUDIO_DEVICE_OUT_HDMI) { |
| 6194 | filterSurroundChannelMasks(&channelMasks); |
| 6195 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6196 | } |
| 6197 | } |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 6198 | profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates)); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6199 | } |
| 6200 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6201 | |
Mikhail Naganov | 1b2a794 | 2017-12-08 10:18:09 -0800 | [diff] [blame] | 6202 | } // namespace android |