Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 17 | #define LOG_TAG "APM_AudioPolicyManager" |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
| 19 | |
| 20 | //#define VERY_VERBOSE_LOGGING |
| 21 | #ifdef VERY_VERBOSE_LOGGING |
| 22 | #define ALOGVV ALOGV |
| 23 | #else |
| 24 | #define ALOGVV(a...) do { } while(0) |
| 25 | #endif |
| 26 | |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 27 | #define AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH 128 |
| 28 | #define AUDIO_POLICY_XML_CONFIG_FILE_NAME "audio_policy_configuration.xml" |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 29 | #define AUDIO_POLICY_A2DP_OFFLOAD_DISABLED_XML_CONFIG_FILE_NAME \ |
| 30 | "audio_policy_configuration_a2dp_offload_disabled.xml" |
François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 31 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 32 | #include <inttypes.h> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 33 | #include <math.h> |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 34 | #include <vector> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 35 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 36 | #include <AudioPolicyManagerInterface.h> |
| 37 | #include <AudioPolicyEngineInstance.h> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 38 | #include <cutils/properties.h> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 39 | #include <utils/Log.h> |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 40 | #include <media/AudioParameter.h> |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 41 | #include <media/AudioPolicyHelper.h> |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 42 | #include <private/android_filesystem_config.h> |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 43 | #include <soundtrigger/SoundTrigger.h> |
Mikhail Naganov | cbc8f61 | 2016-10-11 18:05:13 -0700 | [diff] [blame] | 44 | #include <system/audio.h> |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 45 | #include <audio_policy_conf.h> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 46 | #include "AudioPolicyManager.h" |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 47 | #ifndef USE_XML_AUDIO_POLICY_CONF |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 48 | #include <ConfigParsingUtils.h> |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 49 | #include <StreamDescriptor.h> |
François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 50 | #endif |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 51 | #include <Serializer.h> |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 52 | #include "TypeConverter.h" |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 53 | #include <policy.h> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 54 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 55 | namespace android { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 56 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 57 | //FIXME: workaround for truncated touch sounds |
| 58 | // to be removed when the problem is handled by system UI |
| 59 | #define TOUCH_SOUND_FIXED_DELAY_MS 100 |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 60 | |
| 61 | // Largest difference in dB on earpiece in call between the voice volume and another |
| 62 | // media / notification / system volume. |
| 63 | constexpr float IN_CALL_EARPIECE_HEADROOM_DB = 3.f; |
| 64 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 65 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) |
| 66 | // Array of all surround formats. |
| 67 | static const audio_format_t SURROUND_FORMATS[] = { |
| 68 | AUDIO_FORMAT_AC3, |
| 69 | AUDIO_FORMAT_E_AC3, |
| 70 | AUDIO_FORMAT_DTS, |
| 71 | AUDIO_FORMAT_DTS_HD, |
| 72 | AUDIO_FORMAT_AAC_LC, |
| 73 | AUDIO_FORMAT_DOLBY_TRUEHD, |
| 74 | AUDIO_FORMAT_E_AC3_JOC, |
| 75 | }; |
| 76 | // Array of all AAC formats. When AAC is enabled by users, all AAC formats should be enabled. |
| 77 | static const audio_format_t AAC_FORMATS[] = { |
| 78 | AUDIO_FORMAT_AAC_LC, |
| 79 | AUDIO_FORMAT_AAC_HE_V1, |
| 80 | AUDIO_FORMAT_AAC_HE_V2, |
| 81 | AUDIO_FORMAT_AAC_ELD, |
| 82 | AUDIO_FORMAT_AAC_XHE, |
| 83 | }; |
| 84 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 85 | // Compressed formats for MSD module, ordered from most preferred to least preferred. |
| 86 | static const std::vector<audio_format_t> compressedFormatsOrder = {{ |
| 87 | AUDIO_FORMAT_MAT_2_1, AUDIO_FORMAT_MAT_2_0, AUDIO_FORMAT_E_AC3, |
| 88 | AUDIO_FORMAT_AC3, AUDIO_FORMAT_PCM_16_BIT }}; |
| 89 | // Channel masks for MSD module, 3D > 2D > 1D ordering (most preferred to least preferred). |
| 90 | static const std::vector<audio_channel_mask_t> surroundChannelMasksOrder = {{ |
| 91 | AUDIO_CHANNEL_OUT_3POINT1POINT2, AUDIO_CHANNEL_OUT_3POINT0POINT2, |
| 92 | AUDIO_CHANNEL_OUT_2POINT1POINT2, AUDIO_CHANNEL_OUT_2POINT0POINT2, |
| 93 | AUDIO_CHANNEL_OUT_5POINT1, AUDIO_CHANNEL_OUT_STEREO }}; |
| 94 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 95 | // ---------------------------------------------------------------------------- |
| 96 | // AudioPolicyInterface implementation |
| 97 | // ---------------------------------------------------------------------------- |
| 98 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 99 | status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 100 | audio_policy_dev_state_t state, |
| 101 | const char *device_address, |
| 102 | const char *device_name) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 103 | { |
jiabin | a7b4379 | 2018-02-15 16:04:46 -0800 | [diff] [blame] | 104 | status_t status = setDeviceConnectionStateInt(device, state, device_address, device_name); |
| 105 | nextAudioPortGeneration(); |
| 106 | return status; |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 107 | } |
| 108 | |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 109 | void AudioPolicyManager::broadcastDeviceConnectionState(audio_devices_t device, |
| 110 | audio_policy_dev_state_t state, |
| 111 | const String8 &device_address) |
| 112 | { |
| 113 | AudioParameter param(device_address); |
| 114 | const String8 key(state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE ? |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 115 | AudioParameter::keyStreamConnect : AudioParameter::keyStreamDisconnect); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 116 | param.addInt(key, device); |
| 117 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 118 | } |
| 119 | |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 120 | status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device, |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 121 | audio_policy_dev_state_t state, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 122 | const char *device_address, |
| 123 | const char *device_name) |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 124 | { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 125 | ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s", |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 126 | device, state, device_address, device_name); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 127 | |
| 128 | // connect/disconnect only 1 device at a time |
| 129 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; |
| 130 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 131 | sp<DeviceDescriptor> devDesc = |
| 132 | mHwModules.getDeviceDescriptor(device, device_address, device_name); |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 133 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 134 | // handle output devices |
| 135 | if (audio_is_output_device(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 136 | SortedVector <audio_io_handle_t> outputs; |
| 137 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 138 | ssize_t index = mAvailableOutputDevices.indexOf(devDesc); |
| 139 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 140 | // save a copy of the opened output descriptors before any output is opened or closed |
| 141 | // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies() |
| 142 | mPreviousOutputs = mOutputs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 143 | switch (state) |
| 144 | { |
| 145 | // handle output device connection |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 146 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 147 | if (index >= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 148 | ALOGW("setDeviceConnectionState() device already connected: %x", device); |
| 149 | return INVALID_OPERATION; |
| 150 | } |
| 151 | ALOGV("setDeviceConnectionState() connecting device %x", device); |
| 152 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 153 | // register new device as available |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 154 | index = mAvailableOutputDevices.add(devDesc); |
| 155 | if (index >= 0) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 156 | sp<HwModule> module = mHwModules.getModuleForDevice(device); |
Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 157 | if (module == 0) { |
| 158 | ALOGD("setDeviceConnectionState() could not find HW module for device %08x", |
| 159 | device); |
| 160 | mAvailableOutputDevices.remove(devDesc); |
| 161 | return INVALID_OPERATION; |
| 162 | } |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 163 | mAvailableOutputDevices[index]->attach(module); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 164 | } else { |
| 165 | return NO_MEMORY; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 166 | } |
| 167 | |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 168 | // Before checking outputs, broadcast connect event to allow HAL to retrieve dynamic |
| 169 | // parameters on newly connected devices (instead of opening the outputs...) |
| 170 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); |
| 171 | |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 172 | if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 173 | mAvailableOutputDevices.remove(devDesc); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 174 | |
| 175 | broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 176 | devDesc->mAddress); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 177 | return INVALID_OPERATION; |
| 178 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 179 | // Propagate device availability to Engine |
| 180 | mEngine->setDeviceConnectionState(devDesc, state); |
| 181 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 182 | // outputs should never be empty here |
| 183 | ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():" |
| 184 | "checkOutputsForDevice() returned no outputs but status OK"); |
| 185 | ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs", |
| 186 | outputs.size()); |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 187 | |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 188 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 189 | // handle output device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 190 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 191 | if (index < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 192 | ALOGW("setDeviceConnectionState() device not connected: %x", device); |
| 193 | return INVALID_OPERATION; |
| 194 | } |
| 195 | |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 196 | ALOGV("setDeviceConnectionState() disconnecting output device %x", device); |
| 197 | |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 198 | // Send Disconnect to HALs |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 199 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 200 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 201 | // remove device from available output devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 202 | mAvailableOutputDevices.remove(devDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 203 | |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 204 | checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 205 | |
| 206 | // Propagate device availability to Engine |
| 207 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 208 | } break; |
| 209 | |
| 210 | default: |
| 211 | ALOGE("setDeviceConnectionState() invalid state: %x", state); |
| 212 | return BAD_VALUE; |
| 213 | } |
| 214 | |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 215 | checkForDeviceAndOutputChanges([&]() { |
| 216 | // outputs must be closed after checkOutputForAllStrategies() is executed |
| 217 | if (!outputs.isEmpty()) { |
| 218 | for (audio_io_handle_t output : outputs) { |
| 219 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
| 220 | // close unused outputs after device disconnection or direct outputs that have been |
| 221 | // opened by checkOutputsForDevice() to query dynamic parameters |
| 222 | if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) || |
| 223 | (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) && |
| 224 | (desc->mDirectOpenCount == 0))) { |
| 225 | closeOutput(output); |
| 226 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 227 | } |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 228 | // check A2DP again after closing A2DP output to reset mA2dpSuspended if needed |
| 229 | return true; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 230 | } |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 231 | return false; |
| 232 | }); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 233 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 234 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 235 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 236 | updateCallRouting(newDevice); |
| 237 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 238 | const audio_devices_t msdOutDevice = getMsdAudioOutDeviceTypes(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 239 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 240 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 241 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) { |
| 242 | audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 243 | // do not force device change on duplicated output because if device is 0, it will |
| 244 | // also force a device 0 for the two outputs it is duplicated to which may override |
| 245 | // a valid device selection on those outputs. |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 246 | bool force = (msdOutDevice == AUDIO_DEVICE_NONE || msdOutDevice != desc->device()) |
| 247 | && !desc->isDuplicated() |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 248 | && (!device_distinguishes_on_address(device) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 249 | // always force when disconnecting (a non-duplicated device) |
| 250 | || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 251 | setOutputDevice(desc, newDevice, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 252 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 255 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
| 256 | cleanUpForDevice(devDesc); |
| 257 | } |
| 258 | |
Eric Laurent | 72aa32f | 2014-05-30 18:51:48 -0700 | [diff] [blame] | 259 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | b71e58b | 2014-05-29 16:08:11 -0700 | [diff] [blame] | 260 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 261 | } // end if is output device |
| 262 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 263 | // handle input devices |
| 264 | if (audio_is_input_device(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 265 | SortedVector <audio_io_handle_t> inputs; |
| 266 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 267 | ssize_t index = mAvailableInputDevices.indexOf(devDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 268 | switch (state) |
| 269 | { |
| 270 | // handle input device connection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 271 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 272 | if (index >= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 273 | ALOGW("setDeviceConnectionState() device already connected: %d", device); |
| 274 | return INVALID_OPERATION; |
| 275 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 276 | sp<HwModule> module = mHwModules.getModuleForDevice(device); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 277 | if (module == NULL) { |
| 278 | ALOGW("setDeviceConnectionState(): could not find HW module for device %08x", |
| 279 | device); |
| 280 | return INVALID_OPERATION; |
| 281 | } |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 282 | |
| 283 | // Before checking intputs, broadcast connect event to allow HAL to retrieve dynamic |
| 284 | // parameters on newly connected devices (instead of opening the inputs...) |
| 285 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); |
| 286 | |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 287 | if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) { |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 288 | broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 289 | devDesc->mAddress); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 290 | return INVALID_OPERATION; |
| 291 | } |
| 292 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 293 | index = mAvailableInputDevices.add(devDesc); |
| 294 | if (index >= 0) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 295 | mAvailableInputDevices[index]->attach(module); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 296 | } else { |
| 297 | return NO_MEMORY; |
| 298 | } |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 299 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 300 | // Propagate device availability to Engine |
| 301 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 302 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 303 | |
| 304 | // handle input device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 305 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 306 | if (index < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 307 | ALOGW("setDeviceConnectionState() device not connected: %d", device); |
| 308 | return INVALID_OPERATION; |
| 309 | } |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 310 | |
| 311 | ALOGV("setDeviceConnectionState() disconnecting input device %x", device); |
| 312 | |
| 313 | // Set Disconnect to HALs |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 314 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 315 | |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 316 | checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 317 | mAvailableInputDevices.remove(devDesc); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 318 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 319 | // Propagate device availability to Engine |
| 320 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 321 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 322 | |
| 323 | default: |
| 324 | ALOGE("setDeviceConnectionState() invalid state: %x", state); |
| 325 | return BAD_VALUE; |
| 326 | } |
| 327 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 328 | closeAllInputs(); |
Eric Laurent | 5f5fca5 | 2016-08-04 11:48:57 -0700 | [diff] [blame] | 329 | // As the input device list can impact the output device selection, update |
| 330 | // getDeviceForStrategy() cache |
| 331 | updateDevicesAndOutputs(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 332 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 333 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 334 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 335 | updateCallRouting(newDevice); |
| 336 | } |
| 337 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 338 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
| 339 | cleanUpForDevice(devDesc); |
| 340 | } |
| 341 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 342 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 343 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 344 | } // end if is input device |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 345 | |
| 346 | ALOGW("setDeviceConnectionState() invalid device: %x", device); |
| 347 | return BAD_VALUE; |
| 348 | } |
| 349 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 350 | audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 351 | const char *device_address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 352 | { |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 353 | sp<DeviceDescriptor> devDesc = |
| 354 | mHwModules.getDeviceDescriptor(device, device_address, "", |
| 355 | (strlen(device_address) != 0)/*matchAddress*/); |
| 356 | |
| 357 | if (devDesc == 0) { |
| 358 | ALOGW("getDeviceConnectionState() undeclared device, type %08x, address: %s", |
| 359 | device, device_address); |
| 360 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
| 361 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 362 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 363 | DeviceVector *deviceVector; |
| 364 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 365 | if (audio_is_output_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 366 | deviceVector = &mAvailableOutputDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 367 | } else if (audio_is_input_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 368 | deviceVector = &mAvailableInputDevices; |
| 369 | } else { |
| 370 | ALOGW("getDeviceConnectionState() invalid device type %08x", device); |
| 371 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 372 | } |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 373 | |
| 374 | return (deviceVector->getDevice(device, String8(device_address)) != 0) ? |
| 375 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 376 | } |
| 377 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 378 | status_t AudioPolicyManager::handleDeviceConfigChange(audio_devices_t device, |
| 379 | const char *device_address, |
| 380 | const char *device_name) |
| 381 | { |
| 382 | status_t status; |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 383 | String8 reply; |
| 384 | AudioParameter param; |
| 385 | int isReconfigA2dpSupported = 0; |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 386 | |
| 387 | ALOGV("handleDeviceConfigChange(() device: 0x%X, address %s name %s", |
| 388 | device, device_address, device_name); |
| 389 | |
Pavlin Radoslavov | c694ff4 | 2017-01-09 23:27:29 -0800 | [diff] [blame] | 390 | // connect/disconnect only 1 device at a time |
| 391 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; |
| 392 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 393 | // Check if the device is currently connected |
| 394 | sp<DeviceDescriptor> devDesc = |
| 395 | mHwModules.getDeviceDescriptor(device, device_address, device_name); |
| 396 | ssize_t index = mAvailableOutputDevices.indexOf(devDesc); |
| 397 | if (index < 0) { |
| 398 | // Nothing to do: device is not connected |
| 399 | return NO_ERROR; |
| 400 | } |
| 401 | |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 402 | // For offloaded A2DP, Hw modules may have the capability to |
| 403 | // configure codecs. Check if any of the loaded hw modules |
| 404 | // supports this. |
| 405 | // If supported, send a set parameter to configure A2DP codecs |
| 406 | // and return. No need to toggle device state. |
| 407 | if (device & AUDIO_DEVICE_OUT_ALL_A2DP) { |
| 408 | reply = mpClientInterface->getParameters( |
| 409 | AUDIO_IO_HANDLE_NONE, |
| 410 | String8(AudioParameter::keyReconfigA2dpSupported)); |
| 411 | AudioParameter repliedParameters(reply); |
| 412 | repliedParameters.getInt( |
| 413 | String8(AudioParameter::keyReconfigA2dpSupported), isReconfigA2dpSupported); |
| 414 | if (isReconfigA2dpSupported) { |
| 415 | const String8 key(AudioParameter::keyReconfigA2dp); |
| 416 | param.add(key, String8("true")); |
| 417 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 418 | return NO_ERROR; |
| 419 | } |
| 420 | } |
| 421 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 422 | // Toggle the device state: UNAVAILABLE -> AVAILABLE |
| 423 | // This will force reading again the device configuration |
| 424 | status = setDeviceConnectionState(device, |
| 425 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 426 | device_address, device_name); |
| 427 | if (status != NO_ERROR) { |
| 428 | ALOGW("handleDeviceConfigChange() error disabling connection state: %d", |
| 429 | status); |
| 430 | return status; |
| 431 | } |
| 432 | |
| 433 | status = setDeviceConnectionState(device, |
| 434 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 435 | device_address, device_name); |
| 436 | if (status != NO_ERROR) { |
| 437 | ALOGW("handleDeviceConfigChange() error enabling connection state: %d", |
| 438 | status); |
| 439 | return status; |
| 440 | } |
| 441 | |
| 442 | return NO_ERROR; |
| 443 | } |
| 444 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 445 | uint32_t AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, uint32_t delayMs) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 446 | { |
| 447 | bool createTxPatch = false; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 448 | uint32_t muteWaitMs = 0; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 449 | |
Andy Hung | a76c7de | 2016-12-13 19:14:31 -0800 | [diff] [blame] | 450 | if(!hasPrimaryOutput() || mPrimaryOutput->device() == AUDIO_DEVICE_OUT_STUB) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 451 | return muteWaitMs; |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 452 | } |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 453 | audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 454 | ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice); |
| 455 | |
| 456 | // release existing RX patch if any |
| 457 | if (mCallRxPatch != 0) { |
| 458 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 459 | mCallRxPatch.clear(); |
| 460 | } |
| 461 | // release TX patch if any |
| 462 | if (mCallTxPatch != 0) { |
| 463 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 464 | mCallTxPatch.clear(); |
| 465 | } |
| 466 | |
| 467 | // If the RX device is on the primary HW module, then use legacy routing method for voice calls |
| 468 | // via setOutputDevice() on primary output. |
| 469 | // Otherwise, create two audio patches for TX and RX path. |
| 470 | if (availablePrimaryOutputDevices() & rxDevice) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 471 | muteWaitMs = setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 472 | // If the TX device is also on the primary HW module, setOutputDevice() will take care |
| 473 | // of it due to legacy implementation. If not, create a patch. |
| 474 | if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN) |
| 475 | == AUDIO_DEVICE_NONE) { |
| 476 | createTxPatch = true; |
| 477 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 478 | } else { // create RX path audio patch |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 479 | mCallRxPatch = createTelephonyPatch(true /*isRx*/, rxDevice, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 480 | createTxPatch = true; |
| 481 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 482 | if (createTxPatch) { // create TX path audio patch |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 483 | mCallTxPatch = createTelephonyPatch(false /*isRx*/, txDevice, delayMs); |
| 484 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 485 | |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 486 | return muteWaitMs; |
| 487 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 488 | |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 489 | sp<AudioPatch> AudioPolicyManager::createTelephonyPatch( |
| 490 | bool isRx, audio_devices_t device, uint32_t delayMs) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 491 | PatchBuilder patchBuilder; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 492 | |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 493 | sp<DeviceDescriptor> txSourceDeviceDesc; |
| 494 | if (isRx) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 495 | patchBuilder.addSink(findDevice(mAvailableOutputDevices, device)). |
| 496 | addSource(findDevice(mAvailableInputDevices, AUDIO_DEVICE_IN_TELEPHONY_RX)); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 497 | } else { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 498 | patchBuilder.addSource(txSourceDeviceDesc = findDevice(mAvailableInputDevices, device)). |
| 499 | addSink(findDevice(mAvailableOutputDevices, AUDIO_DEVICE_OUT_TELEPHONY_TX)); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 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); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 510 | patchBuilder.addSource(outputDesc, { .stream = AUDIO_STREAM_PATCH }); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | if (!isRx) { |
Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 514 | // terminate active capture if on the same HW module as the call TX source device |
| 515 | // FIXME: would be better to refine to only inputs whose profile connects to the |
| 516 | // call TX device but this information is not in the audio patch and logic here must be |
| 517 | // symmetric to the one in startInput() |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 518 | for (const auto& activeDesc : mInputs.getActiveInputs()) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 519 | if (activeDesc->hasSameHwModuleAs(txSourceDeviceDesc)) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 520 | closeActiveClients(activeDesc); |
Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 521 | } |
| 522 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 523 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 524 | |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 525 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 526 | status_t status = mpClientInterface->createAudioPatch( |
| 527 | patchBuilder.patch(), &afPatchHandle, delayMs); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 528 | ALOGW_IF(status != NO_ERROR, |
| 529 | "%s() error %d creating %s audio patch", __func__, status, isRx ? "RX" : "TX"); |
| 530 | sp<AudioPatch> audioPatch; |
| 531 | if (status == NO_ERROR) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 532 | audioPatch = new AudioPatch(patchBuilder.patch(), mUidCached); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 533 | audioPatch->mAfPatchHandle = afPatchHandle; |
| 534 | audioPatch->mUid = mUidCached; |
| 535 | } |
| 536 | return audioPatch; |
| 537 | } |
| 538 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 539 | sp<DeviceDescriptor> AudioPolicyManager::findDevice( |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 540 | const DeviceVector& devices, audio_devices_t device) const { |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 541 | DeviceVector deviceList = devices.getDevicesFromTypeMask(device); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 542 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 543 | "%s() selected device type %#x is not in devices list", __func__, device); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 544 | return deviceList.itemAt(0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 545 | } |
| 546 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 547 | void AudioPolicyManager::setPhoneState(audio_mode_t state) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 548 | { |
| 549 | ALOGV("setPhoneState() state %d", state); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 550 | // store previous phone state for management of sonification strategy below |
| 551 | int oldState = mEngine->getPhoneState(); |
| 552 | |
| 553 | if (mEngine->setPhoneState(state) != NO_ERROR) { |
| 554 | ALOGW("setPhoneState() invalid or same state %d", state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 555 | return; |
| 556 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 557 | /// Opens: can these line be executed after the switch of volume curves??? |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 558 | if (isStateInCall(oldState)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 559 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 560 | // force reevaluating accessibility routing when call stops |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 561 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 562 | } |
| 563 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 564 | /** |
| 565 | * Switching to or from incall state or switching between telephony and VoIP lead to force |
| 566 | * routing command. |
| 567 | */ |
| 568 | bool force = ((is_state_in_call(oldState) != is_state_in_call(state)) |
| 569 | || (is_state_in_call(state) && (state != oldState))); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 570 | |
| 571 | // check for device and output changes triggered by new phone state |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 572 | checkForDeviceAndOutputChanges(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 573 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 574 | int delayMs = 0; |
| 575 | if (isStateInCall(state)) { |
| 576 | nsecs_t sysTime = systemTime(); |
| 577 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 578 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 579 | // mute media and sonification strategies and delay device switch by the largest |
| 580 | // latency of any output where either strategy is active. |
| 581 | // 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] | 582 | if ((isStrategyActive(desc, STRATEGY_MEDIA, |
| 583 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 584 | sysTime) || |
| 585 | isStrategyActive(desc, STRATEGY_SONIFICATION, |
| 586 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 587 | sysTime)) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 588 | (delayMs < (int)desc->latency()*2)) { |
| 589 | delayMs = desc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 590 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 591 | setStrategyMute(STRATEGY_MEDIA, true, desc); |
| 592 | setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 593 | getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/)); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 594 | setStrategyMute(STRATEGY_SONIFICATION, true, desc); |
| 595 | setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 596 | getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/)); |
| 597 | } |
| 598 | } |
| 599 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 600 | if (hasPrimaryOutput()) { |
| 601 | // Note that despite the fact that getNewOutputDevice() is called on the primary output, |
| 602 | // the device returned is not necessarily reachable via this output |
| 603 | audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 604 | // force routing command to audio hardware when ending call |
| 605 | // even if no device change is needed |
| 606 | if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) { |
| 607 | rxDevice = mPrimaryOutput->device(); |
| 608 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 609 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 610 | if (state == AUDIO_MODE_IN_CALL) { |
| 611 | updateCallRouting(rxDevice, delayMs); |
| 612 | } else if (oldState == AUDIO_MODE_IN_CALL) { |
| 613 | if (mCallRxPatch != 0) { |
| 614 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 615 | mCallRxPatch.clear(); |
| 616 | } |
| 617 | if (mCallTxPatch != 0) { |
| 618 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 619 | mCallTxPatch.clear(); |
| 620 | } |
| 621 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
| 622 | } else { |
| 623 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 624 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 625 | } |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 626 | |
| 627 | // reevaluate routing on all outputs in case tracks have been started during the call |
| 628 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 629 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 630 | audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/); |
| 631 | if (state != AUDIO_MODE_IN_CALL || desc != mPrimaryOutput) { |
Yung Ti Su | f60c824 | 2018-05-10 18:07:26 +0800 | [diff] [blame] | 632 | setOutputDevice(desc, newDevice, (newDevice != AUDIO_DEVICE_NONE), 0 /*delayMs*/); |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 633 | } |
| 634 | } |
| 635 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 636 | if (isStateInCall(state)) { |
| 637 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 638 | // force reevaluating accessibility routing when call starts |
| 639 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | // 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] | 643 | if (state == AUDIO_MODE_RINGTONE && |
| 644 | isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 645 | mLimitRingtoneVolume = true; |
| 646 | } else { |
| 647 | mLimitRingtoneVolume = false; |
| 648 | } |
| 649 | } |
| 650 | |
Jean-Michel Trivi | 887a9ed | 2015-03-31 18:02:24 -0700 | [diff] [blame] | 651 | audio_mode_t AudioPolicyManager::getPhoneState() { |
| 652 | return mEngine->getPhoneState(); |
| 653 | } |
| 654 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 655 | void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 656 | audio_policy_forced_cfg_t config) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 657 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 658 | ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState()); |
Eric Laurent | 8dc87a6 | 2017-05-16 19:00:40 -0700 | [diff] [blame] | 659 | if (config == mEngine->getForceUse(usage)) { |
| 660 | return; |
| 661 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 662 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 663 | if (mEngine->setForceUse(usage, config) != NO_ERROR) { |
| 664 | ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage); |
| 665 | return; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 666 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 667 | bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) || |
| 668 | (usage == AUDIO_POLICY_FORCE_FOR_DOCK) || |
| 669 | (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 670 | |
| 671 | // check for device and output changes triggered by new force usage |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 672 | checkForDeviceAndOutputChanges(); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 673 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 674 | //FIXME: workaround for truncated touch sounds |
| 675 | // to be removed when the problem is handled by system UI |
| 676 | uint32_t delayMs = 0; |
| 677 | uint32_t waitMs = 0; |
| 678 | if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) { |
| 679 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 680 | } |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 681 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 682 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/); |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 683 | waitMs = updateCallRouting(newDevice, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 684 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 685 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 686 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
| 687 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/); |
| 688 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 689 | waitMs = setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE), |
| 690 | delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 691 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 692 | if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 693 | applyStreamVolumes(outputDesc, newDevice, waitMs, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 694 | } |
| 695 | } |
| 696 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 697 | for (const auto& activeDesc : mInputs.getActiveInputs()) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 698 | audio_devices_t newDevice = getNewInputDevice(activeDesc); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 699 | // 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] | 700 | if (activeDesc->mProfile->getSupportedDevices().types() & |
| 701 | (newDevice & ~AUDIO_DEVICE_BIT_IN)) { |
| 702 | setInputDevice(activeDesc->mIoHandle, newDevice); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 703 | } else { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 704 | closeInput(activeDesc->mIoHandle); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 705 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 706 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 707 | } |
| 708 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 709 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 710 | { |
| 711 | ALOGV("setSystemProperty() property %s, value %s", property, value); |
| 712 | } |
| 713 | |
| 714 | // Find a direct output profile compatible with the parameters passed, even if the input flags do |
| 715 | // not explicitly request a direct output |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 716 | sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput( |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 717 | audio_devices_t device, |
| 718 | uint32_t samplingRate, |
| 719 | audio_format_t format, |
| 720 | audio_channel_mask_t channelMask, |
| 721 | audio_output_flags_t flags) |
| 722 | { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 723 | // only retain flags that will drive the direct output profile selection |
| 724 | // if explicitly requested |
| 725 | static const uint32_t kRelevantFlags = |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 726 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | |
| 727 | AUDIO_OUTPUT_FLAG_VOIP_RX); |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 728 | flags = |
| 729 | (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); |
| 730 | |
| 731 | sp<IOProfile> profile; |
| 732 | |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 733 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 734 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 735 | if (!curProfile->isCompatibleProfile(device, String8(""), |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 736 | samplingRate, NULL /*updatedSamplingRate*/, |
| 737 | format, NULL /*updatedFormat*/, |
| 738 | channelMask, NULL /*updatedChannelMask*/, |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 739 | flags)) { |
| 740 | continue; |
| 741 | } |
| 742 | // reject profiles not corresponding to a device currently available |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 743 | if ((mAvailableOutputDevices.types() & curProfile->getSupportedDevicesType()) == 0) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 744 | continue; |
| 745 | } |
| 746 | // 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] | 747 | if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 748 | continue; |
| 749 | } |
| 750 | profile = curProfile; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 751 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 752 | break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 753 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 754 | } |
| 755 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 756 | return profile; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 757 | } |
| 758 | |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 759 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 760 | { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 761 | routing_strategy strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 762 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Andy Hung | c990152 | 2017-11-10 20:07:54 -0800 | [diff] [blame] | 763 | |
| 764 | // Note that related method getOutputForAttr() uses getOutputForDevice() not selectOutput(). |
| 765 | // We use selectOutput() here since we don't have the desired AudioTrack sample rate, |
| 766 | // format, flags, etc. This may result in some discrepancy for functions that utilize |
| 767 | // getOutput() solely on audio_stream_type such as AudioSystem::getOutputFrameCount() |
| 768 | // and AudioSystem::getOutputSamplingRate(). |
| 769 | |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 770 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 771 | 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] | 772 | |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 773 | ALOGV("getOutput() stream %d selected device %08x, output %d", stream, device, output); |
| 774 | return output; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 775 | } |
| 776 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 777 | status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, |
| 778 | audio_io_handle_t *output, |
| 779 | audio_session_t session, |
| 780 | audio_stream_type_t *stream, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 781 | uid_t uid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 782 | const audio_config_t *config, |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 783 | audio_output_flags_t *flags, |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 784 | audio_port_handle_t *selectedDeviceId, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 785 | audio_port_handle_t *portId) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 786 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 787 | audio_attributes_t attributes; |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 788 | DeviceVector outputDevices; |
| 789 | routing_strategy strategy; |
| 790 | audio_devices_t device; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 791 | const audio_port_handle_t requestedDeviceId = *selectedDeviceId; |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 792 | audio_devices_t msdDevice = getMsdAudioOutDeviceTypes(); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 793 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 794 | // The supplied portId must be AUDIO_PORT_HANDLE_NONE |
| 795 | if (*portId != AUDIO_PORT_HANDLE_NONE) { |
| 796 | return INVALID_OPERATION; |
| 797 | } |
| 798 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 799 | if (attr != NULL) { |
| 800 | if (!isValidAttributes(attr)) { |
| 801 | ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]", |
| 802 | attr->usage, attr->content_type, attr->flags, |
| 803 | attr->tags); |
| 804 | return BAD_VALUE; |
| 805 | } |
| 806 | attributes = *attr; |
| 807 | } else { |
| 808 | if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) { |
| 809 | ALOGE("getOutputForAttr(): invalid stream type"); |
| 810 | return BAD_VALUE; |
| 811 | } |
| 812 | stream_type_to_audio_attributes(*stream, &attributes); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 813 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 814 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 815 | ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x" |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 816 | " session %d selectedDeviceId %d", |
| 817 | attributes.usage, attributes.content_type, attributes.tags, attributes.flags, |
| 818 | session, requestedDeviceId); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 819 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 820 | *stream = streamTypefromAttributesInt(&attributes); |
| 821 | |
| 822 | strategy = getStrategyForAttr(&attributes); |
| 823 | |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 824 | // First check for explicit routing (eg. setPreferredDevice) |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 825 | if (requestedDeviceId != AUDIO_PORT_HANDLE_NONE) { |
| 826 | sp<DeviceDescriptor> deviceDesc = |
| 827 | mAvailableOutputDevices.getDeviceFromId(requestedDeviceId); |
| 828 | device = deviceDesc->type(); |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 829 | } else { |
| 830 | // If no explict route, is there a matching dynamic policy that applies? |
| 831 | sp<SwAudioOutputDescriptor> desc; |
| 832 | if (mPolicyMixes.getOutputForAttr(attributes, uid, desc) == NO_ERROR) { |
| 833 | ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr"); |
| 834 | if (!audio_has_proportional_frames(config->format)) { |
| 835 | return BAD_VALUE; |
| 836 | } |
| 837 | *stream = streamTypefromAttributesInt(&attributes); |
| 838 | *output = desc->mIoHandle; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 839 | AudioMix *mix = desc->mPolicyMix; |
| 840 | sp<DeviceDescriptor> deviceDesc = |
| 841 | mAvailableOutputDevices.getDevice(mix->mDeviceType, mix->mDeviceAddress); |
| 842 | *selectedDeviceId = deviceDesc != 0 ? deviceDesc->getId() : AUDIO_PORT_HANDLE_NONE; |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 843 | ALOGV("getOutputForAttr() returns output %d", *output); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 844 | goto exit; |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | // Virtual sources must always be dynamicaly or explicitly routed |
| 848 | if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 849 | ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE"); |
| 850 | return BAD_VALUE; |
| 851 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 852 | device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 853 | } |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 854 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 855 | if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 856 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 857 | } |
| 858 | |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 859 | // Set incall music only if device was explicitly set, and fallback to the device which is |
| 860 | // chosen by the engine if not. |
| 861 | // FIXME: provide a more generic approach which is not device specific and move this back |
| 862 | // to getOutputForDevice. |
| 863 | if (device == AUDIO_DEVICE_OUT_TELEPHONY_TX && |
| 864 | *stream == AUDIO_STREAM_MUSIC && |
| 865 | audio_is_linear_pcm(config->format) && |
| 866 | isInCall()) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 867 | if (requestedDeviceId != AUDIO_PORT_HANDLE_NONE) { |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 868 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INCALL_MUSIC; |
| 869 | } else { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 870 | // Get the devce type directly from the engine to bypass preferred route logic |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 871 | device = mEngine->getDeviceForStrategy(strategy); |
| 872 | } |
| 873 | } |
| 874 | |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 875 | ALOGV("getOutputForAttr() device 0x%x, sampling rate %d, format %#x, channel mask %#x, " |
| 876 | "flags %#x", |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 877 | device, config->sample_rate, config->format, config->channel_mask, *flags); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 878 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 879 | *output = AUDIO_IO_HANDLE_NONE; |
| 880 | if (msdDevice != AUDIO_DEVICE_NONE) { |
| 881 | *output = getOutputForDevice(msdDevice, session, *stream, config, flags); |
| 882 | if (*output != AUDIO_IO_HANDLE_NONE && setMsdPatch(device) == NO_ERROR) { |
| 883 | ALOGV("%s() Using MSD device 0x%x instead of device 0x%x", |
| 884 | __func__, msdDevice, device); |
| 885 | device = msdDevice; |
| 886 | } else { |
| 887 | *output = AUDIO_IO_HANDLE_NONE; |
| 888 | } |
| 889 | } |
| 890 | if (*output == AUDIO_IO_HANDLE_NONE) { |
| 891 | *output = getOutputForDevice(device, session, *stream, config, flags); |
| 892 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 893 | if (*output == AUDIO_IO_HANDLE_NONE) { |
| 894 | return INVALID_OPERATION; |
| 895 | } |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 896 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 897 | outputDevices = mAvailableOutputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 898 | *selectedDeviceId = outputDevices.size() > 0 ? outputDevices.itemAt(0)->getId() |
| 899 | : AUDIO_PORT_HANDLE_NONE; |
| 900 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 901 | exit: |
| 902 | audio_config_base_t clientConfig = {.sample_rate = config->sample_rate, |
| 903 | .format = config->format, |
| 904 | .channel_mask = config->channel_mask }; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 905 | *portId = AudioPort::getNextUniqueId(); |
| 906 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 907 | sp<TrackClientDescriptor> clientDesc = |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 908 | new TrackClientDescriptor(*portId, uid, session, attributes, clientConfig, |
| 909 | requestedDeviceId, *stream, |
| 910 | getStrategyForAttr(&attributes), |
| 911 | *flags); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 912 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(*output); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 913 | outputDesc->clientsMap().emplace(*portId, clientDesc); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 914 | |
| 915 | ALOGV(" getOutputForAttr() returns output %d selectedDeviceId %d for port ID %d", |
| 916 | *output, *selectedDeviceId, *portId); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 917 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 918 | return NO_ERROR; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | audio_io_handle_t AudioPolicyManager::getOutputForDevice( |
| 922 | audio_devices_t device, |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 923 | audio_session_t session, |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 924 | audio_stream_type_t stream, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 925 | const audio_config_t *config, |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 926 | audio_output_flags_t *flags) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 927 | { |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 928 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 929 | status_t status; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 930 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 931 | // open a direct output if required by specified parameters |
| 932 | //force direct flag if offload flag is set: offloading implies a direct output stream |
| 933 | // and all common behaviors are driven by checking only the direct flag |
| 934 | // this should normally be set appropriately in the policy configuration file |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 935 | if ((*flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 936 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 937 | } |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 938 | if ((*flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 939 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 940 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 941 | // only allow deep buffering for music stream type |
| 942 | if (stream != AUDIO_STREAM_MUSIC) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 943 | *flags = (audio_output_flags_t)(*flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 944 | } else if (/* stream == AUDIO_STREAM_MUSIC && */ |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 945 | *flags == AUDIO_OUTPUT_FLAG_NONE && |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 946 | property_get_bool("audio.deep_buffer.media", false /* default_value */)) { |
| 947 | // use DEEP_BUFFER as default output for music stream type |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 948 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER; |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 949 | } |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 950 | if (stream == AUDIO_STREAM_TTS) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 951 | *flags = AUDIO_OUTPUT_FLAG_TTS; |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 952 | } else if (stream == AUDIO_STREAM_VOICE_CALL && |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 953 | audio_is_linear_pcm(config->format)) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 954 | *flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX | |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 955 | AUDIO_OUTPUT_FLAG_DIRECT); |
| 956 | ALOGV("Set VoIP and Direct output flags for PCM format"); |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 957 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 958 | |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 959 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 960 | sp<IOProfile> profile; |
| 961 | |
| 962 | // 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] | 963 | // and not explicitly requested |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 964 | if (((*flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 965 | audio_is_linear_pcm(config->format) && config->sample_rate <= SAMPLE_RATE_HZ_MAX && |
| 966 | audio_channel_count_from_out_mask(config->channel_mask) <= 2) { |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 967 | goto non_direct_output; |
| 968 | } |
| 969 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 970 | // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled. |
| 971 | // This prevents creating an offloaded track and tearing it down immediately after start |
| 972 | // when audioflinger detects there is an active non offloadable effect. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 973 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 974 | // This may prevent offloading in rare situations where effects are left active by apps |
| 975 | // in the background. |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 976 | |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 977 | if (((*flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 978 | !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 979 | profile = getProfileForDirectOutput(device, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 980 | config->sample_rate, |
| 981 | config->format, |
| 982 | config->channel_mask, |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 983 | (audio_output_flags_t)*flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 984 | } |
| 985 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 986 | if (profile != 0) { |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 987 | // exclusive outputs for MMAP and Offload are enforced by different session ids. |
| 988 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 989 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 990 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { |
| 991 | // reuse direct output if currently open by the same client |
| 992 | // and configured with same parameters |
| 993 | if ((config->sample_rate == desc->mSamplingRate) && |
Andy Hung | 5659ed5 | 2018-04-30 10:31:26 -0700 | [diff] [blame] | 994 | (config->format == desc->mFormat) && |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 995 | (config->channel_mask == desc->mChannelMask) && |
| 996 | (session == desc->mDirectClientSession)) { |
| 997 | desc->mDirectOpenCount++; |
| 998 | ALOGI("getOutputForDevice() reusing direct output %d for session %d", |
| 999 | mOutputs.keyAt(i), session); |
| 1000 | return mOutputs.keyAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1001 | } |
| 1002 | } |
| 1003 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1004 | |
| 1005 | if (!profile->canOpenNewIo()) { |
| 1006 | goto non_direct_output; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1007 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1008 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1009 | sp<SwAudioOutputDescriptor> outputDesc = |
| 1010 | new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 1011 | |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 1012 | DeviceVector outputDevices = mAvailableOutputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 1013 | String8 address = outputDevices.size() > 0 ? outputDevices.itemAt(0)->mAddress |
| 1014 | : String8(""); |
| 1015 | |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1016 | status = outputDesc->open(config, device, address, stream, *flags, &output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1017 | |
| 1018 | // only accept an output with the requested parameters |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1019 | if (status != NO_ERROR || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1020 | (config->sample_rate != 0 && config->sample_rate != outputDesc->mSamplingRate) || |
Andy Hung | 5659ed5 | 2018-04-30 10:31:26 -0700 | [diff] [blame] | 1021 | (config->format != AUDIO_FORMAT_DEFAULT && config->format != outputDesc->mFormat) || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1022 | (config->channel_mask != 0 && config->channel_mask != outputDesc->mChannelMask)) { |
| 1023 | ALOGV("getOutputForDevice() failed opening direct output: output %d sample rate %d %d," |
| 1024 | "format %d %d, channel mask %04x %04x", output, config->sample_rate, |
| 1025 | outputDesc->mSamplingRate, config->format, outputDesc->mFormat, |
| 1026 | config->channel_mask, outputDesc->mChannelMask); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1027 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1028 | outputDesc->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1029 | } |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 1030 | // 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] | 1031 | if (audio_is_linear_pcm(config->format) && |
| 1032 | config->sample_rate <= SAMPLE_RATE_HZ_MAX) { |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 1033 | goto non_direct_output; |
| 1034 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1035 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1036 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1037 | outputDesc->mDirectOpenCount = 1; |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 1038 | outputDesc->mDirectClientSession = session; |
| 1039 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1040 | addOutput(output, outputDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1041 | mPreviousOutputs = mOutputs; |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 1042 | ALOGV("getOutputForDevice() returns new direct output %d", output); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1043 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1044 | return output; |
| 1045 | } |
| 1046 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 1047 | non_direct_output: |
Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 1048 | |
| 1049 | // A request for HW A/V sync cannot fallback to a mixed output because time |
| 1050 | // stamps are embedded in audio data |
Phil Burk | 2d05993 | 2018-02-15 15:55:11 -0800 | [diff] [blame] | 1051 | 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] | 1052 | return AUDIO_IO_HANDLE_NONE; |
| 1053 | } |
| 1054 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1055 | // ignoring channel mask due to downmix capability in mixer |
| 1056 | |
| 1057 | // open a non direct output |
| 1058 | |
| 1059 | // for non direct outputs, only PCM is supported |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1060 | if (audio_is_linear_pcm(config->format)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1061 | // get which output is suitable for the specified stream. The actual |
| 1062 | // routing change will happen when startOutput() will be called |
| 1063 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 1064 | |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1065 | // 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] | 1066 | *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_DIRECT); |
| 1067 | output = selectOutput(outputs, *flags, config->format); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1068 | } |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 1069 | ALOGW_IF((output == 0), "getOutputForDevice() could not find output for stream %d, " |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1070 | "sampling rate %d, format %#x, channels %#x, flags %#x", |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1071 | stream, config->sample_rate, config->format, config->channel_mask, *flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1072 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1073 | return output; |
| 1074 | } |
| 1075 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1076 | sp<DeviceDescriptor> AudioPolicyManager::getMsdAudioInDevice() const { |
| 1077 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 1078 | if (msdModule != 0) { |
| 1079 | DeviceVector msdInputDevices = mAvailableInputDevices.getDevicesFromHwModule( |
| 1080 | msdModule->getHandle()); |
| 1081 | if (!msdInputDevices.isEmpty()) return msdInputDevices.itemAt(0); |
| 1082 | } |
| 1083 | return 0; |
| 1084 | } |
| 1085 | |
| 1086 | audio_devices_t AudioPolicyManager::getMsdAudioOutDeviceTypes() const { |
| 1087 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 1088 | if (msdModule != 0) { |
| 1089 | return mAvailableOutputDevices.getDeviceTypesFromHwModule(msdModule->getHandle()); |
| 1090 | } |
| 1091 | return AUDIO_DEVICE_NONE; |
| 1092 | } |
| 1093 | |
| 1094 | const AudioPatchCollection AudioPolicyManager::getMsdPatches() const { |
| 1095 | AudioPatchCollection msdPatches; |
| 1096 | audio_module_handle_t msdModuleHandle = mHwModules.getModuleFromName( |
| 1097 | AUDIO_HARDWARE_MODULE_ID_MSD)->getHandle(); |
| 1098 | if (msdModuleHandle == AUDIO_MODULE_HANDLE_NONE) return msdPatches; |
| 1099 | for (size_t i = 0; i < mAudioPatches.size(); ++i) { |
| 1100 | sp<AudioPatch> patch = mAudioPatches.valueAt(i); |
| 1101 | for (size_t j = 0; j < patch->mPatch.num_sources; ++j) { |
| 1102 | const struct audio_port_config *source = &patch->mPatch.sources[j]; |
| 1103 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 1104 | source->ext.device.hw_module == msdModuleHandle) { |
| 1105 | msdPatches.addAudioPatch(patch->mHandle, patch); |
| 1106 | } |
| 1107 | } |
| 1108 | } |
| 1109 | return msdPatches; |
| 1110 | } |
| 1111 | |
| 1112 | status_t AudioPolicyManager::getBestMsdAudioProfileFor(audio_devices_t outputDevice, |
| 1113 | bool hwAvSync, audio_port_config *sourceConfig, audio_port_config *sinkConfig) const |
| 1114 | { |
| 1115 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 1116 | if (msdModule == nullptr) { |
| 1117 | ALOGE("%s() unable to get MSD module", __func__); |
| 1118 | return NO_INIT; |
| 1119 | } |
| 1120 | sp<HwModule> deviceModule = mHwModules.getModuleForDevice(outputDevice); |
| 1121 | if (deviceModule == nullptr) { |
| 1122 | ALOGE("%s() unable to get module for %#x", __func__, outputDevice); |
| 1123 | return NO_INIT; |
| 1124 | } |
| 1125 | const InputProfileCollection &inputProfiles = msdModule->getInputProfiles(); |
| 1126 | if (inputProfiles.isEmpty()) { |
| 1127 | ALOGE("%s() no input profiles for MSD module", __func__); |
| 1128 | return NO_INIT; |
| 1129 | } |
| 1130 | const OutputProfileCollection &outputProfiles = deviceModule->getOutputProfiles(); |
| 1131 | if (outputProfiles.isEmpty()) { |
| 1132 | ALOGE("%s() no output profiles for device %#x", __func__, outputDevice); |
| 1133 | return NO_INIT; |
| 1134 | } |
| 1135 | AudioProfileVector msdProfiles; |
| 1136 | // Each IOProfile represents a MixPort from audio_policy_configuration.xml |
| 1137 | for (const auto &inProfile : inputProfiles) { |
| 1138 | if (hwAvSync == ((inProfile->getFlags() & AUDIO_INPUT_FLAG_HW_AV_SYNC) != 0)) { |
| 1139 | msdProfiles.appendVector(inProfile->getAudioProfiles()); |
| 1140 | } |
| 1141 | } |
| 1142 | AudioProfileVector deviceProfiles; |
| 1143 | for (const auto &outProfile : outputProfiles) { |
| 1144 | if (hwAvSync == ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0)) { |
| 1145 | deviceProfiles.appendVector(outProfile->getAudioProfiles()); |
| 1146 | } |
| 1147 | } |
| 1148 | struct audio_config_base bestSinkConfig; |
| 1149 | status_t result = msdProfiles.findBestMatchingOutputConfig(deviceProfiles, |
| 1150 | compressedFormatsOrder, surroundChannelMasksOrder, true /*preferHigherSamplingRates*/, |
| 1151 | &bestSinkConfig); |
| 1152 | if (result != NO_ERROR) { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1153 | ALOGD("%s() no matching profiles found for device: %#x, hwAvSync: %d", |
| 1154 | __func__, outputDevice, hwAvSync); |
Greg Kaiser | 8328965 | 2018-07-30 06:13:57 -0700 | [diff] [blame] | 1155 | return result; |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1156 | } |
| 1157 | sinkConfig->sample_rate = bestSinkConfig.sample_rate; |
| 1158 | sinkConfig->channel_mask = bestSinkConfig.channel_mask; |
| 1159 | sinkConfig->format = bestSinkConfig.format; |
| 1160 | // For encoded streams force direct flag to prevent downstream mixing. |
| 1161 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1162 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_DIRECT); |
| 1163 | sourceConfig->sample_rate = bestSinkConfig.sample_rate; |
| 1164 | // Specify exact channel mask to prevent guessing by bit count in PatchPanel. |
| 1165 | sourceConfig->channel_mask = audio_channel_mask_out_to_in(bestSinkConfig.channel_mask); |
| 1166 | sourceConfig->format = bestSinkConfig.format; |
| 1167 | // Copy input stream directly without any processing (e.g. resampling). |
| 1168 | sourceConfig->flags.input = static_cast<audio_input_flags_t>( |
| 1169 | sourceConfig->flags.input | AUDIO_INPUT_FLAG_DIRECT); |
| 1170 | if (hwAvSync) { |
| 1171 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1172 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
| 1173 | sourceConfig->flags.input = static_cast<audio_input_flags_t>( |
| 1174 | sourceConfig->flags.input | AUDIO_INPUT_FLAG_HW_AV_SYNC); |
| 1175 | } |
| 1176 | const unsigned int config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE | |
| 1177 | AUDIO_PORT_CONFIG_CHANNEL_MASK | AUDIO_PORT_CONFIG_FORMAT | AUDIO_PORT_CONFIG_FLAGS; |
| 1178 | sinkConfig->config_mask |= config_mask; |
| 1179 | sourceConfig->config_mask |= config_mask; |
| 1180 | return NO_ERROR; |
| 1181 | } |
| 1182 | |
| 1183 | PatchBuilder AudioPolicyManager::buildMsdPatch(audio_devices_t outputDevice) const |
| 1184 | { |
| 1185 | PatchBuilder patchBuilder; |
| 1186 | patchBuilder.addSource(getMsdAudioInDevice()). |
| 1187 | addSink(findDevice(mAvailableOutputDevices, outputDevice)); |
| 1188 | audio_port_config sourceConfig = patchBuilder.patch()->sources[0]; |
| 1189 | audio_port_config sinkConfig = patchBuilder.patch()->sinks[0]; |
| 1190 | // TODO: Figure out whether MSD module has HW_AV_SYNC flag set in the AP config file. |
| 1191 | // For now, we just forcefully try with HwAvSync first. |
| 1192 | status_t res = getBestMsdAudioProfileFor(outputDevice, true /*hwAvSync*/, |
| 1193 | &sourceConfig, &sinkConfig) == NO_ERROR ? NO_ERROR : |
| 1194 | getBestMsdAudioProfileFor( |
| 1195 | outputDevice, false /*hwAvSync*/, &sourceConfig, &sinkConfig); |
| 1196 | if (res == NO_ERROR) { |
| 1197 | // Found a matching profile for encoded audio. Re-create PatchBuilder with this config. |
| 1198 | return (PatchBuilder()).addSource(sourceConfig).addSink(sinkConfig); |
| 1199 | } |
| 1200 | ALOGV("%s() no matching profile found. Fall through to default PCM patch" |
| 1201 | " supporting PCM format conversion.", __func__); |
| 1202 | return patchBuilder; |
| 1203 | } |
| 1204 | |
| 1205 | status_t AudioPolicyManager::setMsdPatch(audio_devices_t outputDevice) { |
| 1206 | ALOGV("%s() for outputDevice %#x", __func__, outputDevice); |
| 1207 | if (outputDevice == AUDIO_DEVICE_NONE) { |
| 1208 | // Use media strategy for unspecified output device. This should only |
| 1209 | // occur on checkForDeviceAndOutputChanges(). Device connection events may |
| 1210 | // therefore invalidate explicit routing requests. |
| 1211 | outputDevice = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/); |
| 1212 | } |
| 1213 | PatchBuilder patchBuilder = buildMsdPatch(outputDevice); |
| 1214 | const struct audio_patch* patch = patchBuilder.patch(); |
| 1215 | const AudioPatchCollection msdPatches = getMsdPatches(); |
| 1216 | if (!msdPatches.isEmpty()) { |
| 1217 | LOG_ALWAYS_FATAL_IF(msdPatches.size() > 1, |
| 1218 | "The current MSD prototype only supports one output patch"); |
| 1219 | sp<AudioPatch> currentPatch = msdPatches.valueAt(0); |
| 1220 | if (audio_patches_are_equal(¤tPatch->mPatch, patch)) { |
| 1221 | return NO_ERROR; |
| 1222 | } |
| 1223 | releaseAudioPatch(currentPatch->mHandle, mUidCached); |
| 1224 | } |
| 1225 | status_t status = installPatch(__func__, -1 /*index*/, nullptr /*patchHandle*/, |
| 1226 | patch, 0 /*delayMs*/, mUidCached, nullptr /*patchDescPtr*/); |
| 1227 | ALOGE_IF(status != NO_ERROR, "%s() error %d creating MSD audio patch", __func__, status); |
| 1228 | ALOGI_IF(status == NO_ERROR, "%s() Patch created from MSD_IN to " |
| 1229 | "device:%#x (format:%#x channels:%#x samplerate:%d)", __func__, outputDevice, |
| 1230 | patch->sources[0].format, patch->sources[0].channel_mask, patch->sources[0].sample_rate); |
| 1231 | return status; |
| 1232 | } |
| 1233 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1234 | 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] | 1235 | audio_output_flags_t flags, |
| 1236 | audio_format_t format) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1237 | { |
| 1238 | // select one output among several that provide a path to a particular device or set of |
| 1239 | // devices (the list was previously build by getOutputsForDevice()). |
| 1240 | // The priority is as follows: |
| 1241 | // 1: the output with the highest number of requested policy flags |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1242 | // 2: the output with the bit depth the closest to the requested one |
| 1243 | // 3: the primary output |
| 1244 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1245 | |
| 1246 | if (outputs.size() == 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1247 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1248 | } |
| 1249 | if (outputs.size() == 1) { |
| 1250 | return outputs[0]; |
| 1251 | } |
| 1252 | |
| 1253 | int maxCommonFlags = 0; |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1254 | audio_io_handle_t outputForFlags = AUDIO_IO_HANDLE_NONE; |
| 1255 | audio_io_handle_t outputForPrimary = AUDIO_IO_HANDLE_NONE; |
| 1256 | audio_io_handle_t outputForFormat = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1257 | audio_format_t bestFormat = AUDIO_FORMAT_INVALID; |
| 1258 | audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1259 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1260 | for (audio_io_handle_t output : outputs) { |
| 1261 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1262 | if (!outputDesc->isDuplicated()) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1263 | // if a valid format is specified, skip output if not compatible |
| 1264 | if (format != AUDIO_FORMAT_INVALID) { |
| 1265 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
Andy Hung | 5659ed5 | 2018-04-30 10:31:26 -0700 | [diff] [blame] | 1266 | if (format != outputDesc->mFormat) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1267 | continue; |
| 1268 | } |
| 1269 | } else if (!audio_is_linear_pcm(format)) { |
| 1270 | continue; |
| 1271 | } |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1272 | if (AudioPort::isBetterFormatMatch( |
| 1273 | outputDesc->mFormat, bestFormat, format)) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1274 | outputForFormat = output; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1275 | bestFormat = outputDesc->mFormat; |
| 1276 | } |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1277 | } |
| 1278 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1279 | int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags); |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1280 | if (commonFlags >= maxCommonFlags) { |
| 1281 | if (commonFlags == maxCommonFlags) { |
Andy Hung | c990152 | 2017-11-10 20:07:54 -0800 | [diff] [blame] | 1282 | if (format != AUDIO_FORMAT_INVALID |
| 1283 | && AudioPort::isBetterFormatMatch( |
| 1284 | outputDesc->mFormat, bestFormatForFlags, format)) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1285 | outputForFlags = output; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1286 | bestFormatForFlags = outputDesc->mFormat; |
| 1287 | } |
| 1288 | } else { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1289 | outputForFlags = output; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1290 | maxCommonFlags = commonFlags; |
| 1291 | bestFormatForFlags = outputDesc->mFormat; |
| 1292 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1293 | ALOGV("selectOutput() commonFlags for output %d, %04x", output, commonFlags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1294 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1295 | if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1296 | outputForPrimary = output; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1297 | } |
| 1298 | } |
| 1299 | } |
| 1300 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1301 | if (outputForFlags != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1302 | return outputForFlags; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1303 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1304 | if (outputForFormat != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1305 | return outputForFormat; |
| 1306 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1307 | if (outputForPrimary != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1308 | return outputForPrimary; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | return outputs[0]; |
| 1312 | } |
| 1313 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1314 | status_t AudioPolicyManager::startOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1315 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1316 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 1317 | |
| 1318 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 1319 | if (outputDesc == 0) { |
| 1320 | ALOGW("startOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1321 | return BAD_VALUE; |
| 1322 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1323 | sp<TrackClientDescriptor> client = outputDesc->clientsMap()[portId]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1324 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1325 | ALOGV("startOutput() output %d, stream %d, session %d", |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1326 | outputDesc->mIoHandle, client->stream(), client->session()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1327 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1328 | status_t status = outputDesc->start(); |
| 1329 | if (status != NO_ERROR) { |
| 1330 | return status; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1331 | } |
| 1332 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1333 | uint32_t delayMs; |
| 1334 | status = startSource(outputDesc, client, &delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1335 | |
| 1336 | if (status != NO_ERROR) { |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1337 | outputDesc->stop(); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1338 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1339 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1340 | if (delayMs != 0) { |
| 1341 | usleep(delayMs * 1000); |
| 1342 | } |
| 1343 | |
| 1344 | return status; |
| 1345 | } |
| 1346 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1347 | status_t AudioPolicyManager::startSource(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 1348 | const sp<TrackClientDescriptor>& client, |
| 1349 | uint32_t *delayMs) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1350 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1351 | // cannot start playback of STREAM_TTS if any other output is being used |
| 1352 | uint32_t beaconMuteLatency = 0; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1353 | |
| 1354 | *delayMs = 0; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1355 | audio_stream_type_t stream = client->stream(); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1356 | if (stream == AUDIO_STREAM_TTS) { |
| 1357 | ALOGV("\t found BEACON stream"); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 1358 | if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1359 | return INVALID_OPERATION; |
| 1360 | } else { |
| 1361 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); |
| 1362 | } |
| 1363 | } else { |
| 1364 | // some playback other than beacon starts |
| 1365 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); |
| 1366 | } |
| 1367 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1368 | // 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] | 1369 | // check active before incrementing usage count |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1370 | bool force = !outputDesc->isActive() && |
| 1371 | (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1372 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1373 | audio_devices_t device = AUDIO_DEVICE_NONE; |
| 1374 | AudioMix *policyMix = NULL; |
| 1375 | const char *address = NULL; |
| 1376 | if (outputDesc->mPolicyMix != NULL) { |
| 1377 | policyMix = outputDesc->mPolicyMix; |
| 1378 | address = policyMix->mDeviceAddress.string(); |
| 1379 | if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 1380 | device = policyMix->mDeviceType; |
| 1381 | } else { |
| 1382 | device = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
| 1383 | } |
| 1384 | } |
| 1385 | |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1386 | // requiresMuteCheck is false when we can bypass mute strategy. |
| 1387 | // It covers a common case when there is no materially active audio |
| 1388 | // and muting would result in unnecessary delay and dropped audio. |
| 1389 | const uint32_t outputLatencyMs = outputDesc->latency(); |
| 1390 | bool requiresMuteCheck = outputDesc->isActive(outputLatencyMs * 2); // account for drain |
| 1391 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1392 | // increment usage count for this stream on the requested output: |
| 1393 | // NOTE that the usage count is the same for duplicated output and hardware output which is |
| 1394 | // necessary for a correct control of hardware output routing by startOutput() and stopOutput() |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame^] | 1395 | outputDesc->setClientActive(client, true); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1396 | |
| 1397 | if (client->hasPreferredDevice(true)) { |
| 1398 | device = getNewOutputDevice(outputDesc, false /*fromCache*/); |
| 1399 | if (device != outputDesc->device()) { |
| 1400 | checkStrategyRoute(getStrategy(stream), outputDesc->mIoHandle); |
| 1401 | } |
| 1402 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1403 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1404 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1405 | selectOutputForMusicEffects(); |
| 1406 | } |
| 1407 | |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame^] | 1408 | if (outputDesc->streamActiveCount(stream) == 1 || device != AUDIO_DEVICE_NONE) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1409 | // starting an output being rerouted? |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1410 | if (device == AUDIO_DEVICE_NONE) { |
| 1411 | device = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1412 | } |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1413 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1414 | routing_strategy strategy = getStrategy(stream); |
| 1415 | bool shouldWait = (strategy == STRATEGY_SONIFICATION) || |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1416 | (strategy == STRATEGY_SONIFICATION_RESPECTFUL) || |
| 1417 | (beaconMuteLatency > 0); |
| 1418 | uint32_t waitMs = beaconMuteLatency; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1419 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1420 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1421 | if (desc != outputDesc) { |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1422 | // An output has a shared device if |
| 1423 | // - managed by the same hw module |
| 1424 | // - supports the currently selected device |
| 1425 | const bool sharedDevice = outputDesc->sharesHwModuleWith(desc) |
| 1426 | && (desc->supportedDevices() & device) != AUDIO_DEVICE_NONE; |
| 1427 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1428 | // force a device change if any other output is: |
| 1429 | // - managed by the same hw module |
Jean-Michel Trivi | 4a5b481 | 2018-02-08 17:22:32 +0000 | [diff] [blame] | 1430 | // - supports currently selected device |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1431 | // - has a current device selection that differs from selected device. |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1432 | // - has an active audio patch |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1433 | // 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] | 1434 | // change the device currently selected by the other output. |
| 1435 | if (sharedDevice && |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1436 | desc->device() != device && |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1437 | desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1438 | force = true; |
| 1439 | } |
| 1440 | // 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] | 1441 | // a notification so that audio focus effect can propagate, or that a mute/unmute |
| 1442 | // event occurred for beacon |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1443 | const uint32_t latencyMs = desc->latency(); |
| 1444 | const bool isActive = desc->isActive(latencyMs * 2); // account for drain |
| 1445 | |
| 1446 | if (shouldWait && isActive && (waitMs < latencyMs)) { |
| 1447 | waitMs = latencyMs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1448 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1449 | |
| 1450 | // Require mute check if another output is on a shared device |
| 1451 | // and currently active to have proper drain and avoid pops. |
| 1452 | // Note restoring AudioTracks onto this output needs to invoke |
| 1453 | // a volume ramp if there is no mute. |
| 1454 | requiresMuteCheck |= sharedDevice && isActive; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1455 | } |
| 1456 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1457 | |
| 1458 | const uint32_t muteWaitMs = |
| 1459 | setOutputDevice(outputDesc, device, force, 0, NULL, address, requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1460 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1461 | // apply volume rules for current stream and device if necessary |
| 1462 | checkAndSetVolume(stream, |
Shuhei Miyazaki | 6fe7033 | 2017-07-31 15:21:28 +0900 | [diff] [blame] | 1463 | mVolumeCurves->getVolumeIndex(stream, outputDesc->device()), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1464 | outputDesc, |
Shuhei Miyazaki | 6fe7033 | 2017-07-31 15:21:28 +0900 | [diff] [blame] | 1465 | outputDesc->device()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1466 | |
| 1467 | // update the outputs if starting an output with a stream that can affect notification |
| 1468 | // routing |
| 1469 | handleNotificationRoutingForStream(stream); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1470 | |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 1471 | // force reevaluating accessibility routing when ringtone or alarm starts |
| 1472 | if (strategy == STRATEGY_SONIFICATION) { |
| 1473 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
| 1474 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1475 | |
| 1476 | if (waitMs > muteWaitMs) { |
| 1477 | *delayMs = waitMs - muteWaitMs; |
| 1478 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1479 | |
| 1480 | // FIXME: A device change (muteWaitMs > 0) likely introduces a volume change. |
| 1481 | // A volume change enacted by APM with 0 delay is not synchronous, as it goes |
| 1482 | // via AudioCommandThread to AudioFlinger. Hence it is possible that the volume |
| 1483 | // change occurs after the MixerThread starts and causes a stream volume |
| 1484 | // glitch. |
| 1485 | // |
| 1486 | // We do not introduce additional delay here. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1487 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1488 | |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 1489 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
| 1490 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
| 1491 | setStrategyMute(STRATEGY_SONIFICATION, true, outputDesc); |
| 1492 | } |
| 1493 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1494 | // Automatically enable the remote submix input when output is started on a re routing mix |
| 1495 | // of type MIX_TYPE_RECORDERS |
| 1496 | if (audio_is_remote_submix_device(device) && policyMix != NULL && |
| 1497 | policyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1498 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1499 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 1500 | address, |
| 1501 | "remote-submix"); |
| 1502 | } |
| 1503 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1504 | return NO_ERROR; |
| 1505 | } |
| 1506 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1507 | status_t AudioPolicyManager::stopOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1508 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1509 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 1510 | |
| 1511 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 1512 | if (outputDesc == 0) { |
| 1513 | ALOGW("stopOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1514 | return BAD_VALUE; |
| 1515 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1516 | sp<TrackClientDescriptor> client = outputDesc->clientsMap()[portId]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1517 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1518 | ALOGV("stopOutput() output %d, stream %d, session %d", |
| 1519 | outputDesc->mIoHandle, client->stream(), client->session()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1520 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1521 | status_t status = stopSource(outputDesc, client); |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1522 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1523 | if (status == NO_ERROR ) { |
| 1524 | outputDesc->stop(); |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1525 | } |
| 1526 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1527 | } |
| 1528 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1529 | status_t AudioPolicyManager::stopSource(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 1530 | const sp<TrackClientDescriptor>& client) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1531 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1532 | // always handle stream stop, check which stream type is stopping |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1533 | audio_stream_type_t stream = client->stream(); |
| 1534 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1535 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); |
| 1536 | |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame^] | 1537 | if (outputDesc->streamActiveCount(stream) > 0) { |
| 1538 | if (outputDesc->streamActiveCount(stream) == 1) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1539 | // Automatically disable the remote submix input when output is stopped on a |
| 1540 | // re routing mix of type MIX_TYPE_RECORDERS |
| 1541 | if (audio_is_remote_submix_device(outputDesc->mDevice) && |
| 1542 | outputDesc->mPolicyMix != NULL && |
| 1543 | outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1544 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1545 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 1546 | outputDesc->mPolicyMix->mDeviceAddress, |
| 1547 | "remote-submix"); |
| 1548 | } |
| 1549 | } |
| 1550 | bool forceDeviceUpdate = false; |
| 1551 | if (client->hasPreferredDevice(true)) { |
| 1552 | checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE); |
| 1553 | forceDeviceUpdate = true; |
| 1554 | } |
| 1555 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1556 | // decrement usage count of this stream on the output |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame^] | 1557 | outputDesc->setClientActive(client, false); |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1558 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1559 | // store time at which the stream was stopped - see isStreamActive() |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame^] | 1560 | if (outputDesc->streamActiveCount(stream) == 0 || forceDeviceUpdate) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1561 | outputDesc->mStopTime[stream] = systemTime(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1562 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1563 | // delay the device switch by twice the latency because stopOutput() is executed when |
| 1564 | // the track stop() command is received and at that time the audio track buffer can |
| 1565 | // still contain data that needs to be drained. The latency only covers the audio HAL |
| 1566 | // and kernel buffers. Also the latency does not always include additional delay in the |
| 1567 | // audio path (audio DSP, CODEC ...) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1568 | setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1569 | |
| 1570 | // force restoring the device selection on other active outputs if it differs from the |
| 1571 | // one being selected for this output |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1572 | uint32_t delayMs = outputDesc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1573 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1574 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1575 | if (desc != outputDesc && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1576 | desc->isActive() && |
| 1577 | outputDesc->sharesHwModuleWith(desc) && |
| 1578 | (newDevice != desc->device())) { |
Haynes Mathew George | 11c499a | 2016-08-26 12:08:25 -0700 | [diff] [blame] | 1579 | audio_devices_t newDevice2 = getNewOutputDevice(desc, false /*fromCache*/); |
| 1580 | bool force = desc->device() != newDevice2; |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 1581 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1582 | setOutputDevice(desc, |
Haynes Mathew George | 11c499a | 2016-08-26 12:08:25 -0700 | [diff] [blame] | 1583 | newDevice2, |
| 1584 | force, |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1585 | delayMs); |
| 1586 | // re-apply device specific volume if not done by setOutputDevice() |
| 1587 | if (!force) { |
| 1588 | applyStreamVolumes(desc, newDevice2, delayMs); |
| 1589 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1590 | } |
| 1591 | } |
| 1592 | // update the outputs if stopping one with a stream that can affect notification routing |
| 1593 | handleNotificationRoutingForStream(stream); |
| 1594 | } |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 1595 | |
| 1596 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
| 1597 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
| 1598 | setStrategyMute(STRATEGY_SONIFICATION, false, outputDesc); |
| 1599 | } |
| 1600 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1601 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1602 | selectOutputForMusicEffects(); |
| 1603 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1604 | return NO_ERROR; |
| 1605 | } else { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1606 | ALOGW("stopOutput() refcount is already 0"); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1607 | return INVALID_OPERATION; |
| 1608 | } |
| 1609 | } |
| 1610 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1611 | void AudioPolicyManager::releaseOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1612 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1613 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 1614 | |
| 1615 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 1616 | if (outputDesc == 0) { |
| 1617 | ALOGW("releaseOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1618 | return; |
| 1619 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1620 | sp<TrackClientDescriptor> client = outputDesc->clientsMap()[portId]; |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1621 | |
| 1622 | ALOGV("releaseOutput() %d", outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1623 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1624 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 1625 | if (outputDesc->mDirectOpenCount <= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1626 | ALOGW("releaseOutput() invalid open count %d for output %d", |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1627 | outputDesc->mDirectOpenCount, outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1628 | return; |
| 1629 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1630 | if (--outputDesc->mDirectOpenCount == 0) { |
| 1631 | closeOutput(outputDesc->mIoHandle); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1632 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1633 | } |
| 1634 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1635 | outputDesc->clientsMap().erase(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1636 | } |
| 1637 | |
| 1638 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1639 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, |
| 1640 | audio_io_handle_t *input, |
| 1641 | audio_session_t session, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1642 | uid_t uid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1643 | const audio_config_base_t *config, |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1644 | audio_input_flags_t flags, |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1645 | audio_port_handle_t *selectedDeviceId, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1646 | input_type_t *inputType, |
| 1647 | audio_port_handle_t *portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1648 | { |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1649 | ALOGV("getInputForAttr() source %d, sampling rate %d, format %#x, channel mask %#x," |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1650 | "session %d, flags %#x", |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1651 | attr->source, config->sample_rate, config->format, config->channel_mask, session, flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1652 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1653 | status_t status = NO_ERROR; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1654 | // handle legacy remote submix case where the address was not always specified |
| 1655 | String8 address = String8(""); |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1656 | audio_source_t halInputSource; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1657 | audio_source_t inputSource = attr->source; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1658 | AudioMix *policyMix = NULL; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1659 | DeviceVector inputDevices; |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1660 | sp<AudioInputDescriptor> inputDesc; |
| 1661 | sp<RecordClientDescriptor> clientDesc; |
| 1662 | audio_port_handle_t requestedDeviceId = *selectedDeviceId; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1663 | bool isSoundTrigger; |
| 1664 | audio_devices_t device; |
| 1665 | |
| 1666 | // The supplied portId must be AUDIO_PORT_HANDLE_NONE |
| 1667 | if (*portId != AUDIO_PORT_HANDLE_NONE) { |
| 1668 | return INVALID_OPERATION; |
| 1669 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1670 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1671 | if (inputSource == AUDIO_SOURCE_DEFAULT) { |
| 1672 | inputSource = AUDIO_SOURCE_MIC; |
| 1673 | } |
| 1674 | |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1675 | // Explicit routing? |
| 1676 | sp<DeviceDescriptor> deviceDesc; |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1677 | if (*selectedDeviceId != AUDIO_PORT_HANDLE_NONE) { |
jiabin | c0c831a | 2018-01-29 17:55:15 -0800 | [diff] [blame] | 1678 | deviceDesc = mAvailableInputDevices.getDeviceFromId(*selectedDeviceId); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1679 | } |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1680 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1681 | // special case for mmap capture: if an input IO handle is specified, we reuse this input if |
| 1682 | // possible |
| 1683 | if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) == AUDIO_INPUT_FLAG_MMAP_NOIRQ && |
| 1684 | *input != AUDIO_IO_HANDLE_NONE) { |
| 1685 | ssize_t index = mInputs.indexOfKey(*input); |
| 1686 | if (index < 0) { |
| 1687 | ALOGW("getInputForAttr() unknown MMAP input %d", *input); |
| 1688 | status = BAD_VALUE; |
| 1689 | goto error; |
| 1690 | } |
| 1691 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1692 | RecordClientVector clients = inputDesc->getClientsForSession(session); |
| 1693 | if (clients.size() == 0) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1694 | ALOGW("getInputForAttr() unknown session %d on input %d", session, *input); |
| 1695 | status = BAD_VALUE; |
| 1696 | goto error; |
| 1697 | } |
| 1698 | // For MMAP mode, the first call to getInputForAttr() is made on behalf of audioflinger. |
| 1699 | // 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] | 1700 | // corresponds to a new client and is only permitted from the same UID. |
| 1701 | // If the first UID is silenced, allow a new UID connection and replace with new UID |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1702 | if (clients.size() > 1) { |
| 1703 | for (const auto& client : clients) { |
| 1704 | // The client map is ordered by key values (portId) and portIds are allocated |
| 1705 | // incrementaly. So the first client in this list is the one opened by audio flinger |
| 1706 | // when the mmap stream is created and should be ignored as it does not correspond |
| 1707 | // to an actual client |
| 1708 | if (client == *clients.cbegin()) { |
| 1709 | continue; |
| 1710 | } |
| 1711 | if (uid != client->uid() && !client->isSilenced()) { |
| 1712 | ALOGW("getInputForAttr() bad uid %d for client %d uid %d", |
| 1713 | uid, client->portId(), client->uid()); |
| 1714 | status = INVALID_OPERATION; |
| 1715 | goto error; |
| 1716 | } |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 1717 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1718 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1719 | *inputType = API_INPUT_LEGACY; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1720 | device = inputDesc->mDevice; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1721 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1722 | ALOGI("%s reusing MMAP input %d for session %d", __FUNCTION__, *input, session); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1723 | goto exit; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1724 | } |
| 1725 | |
| 1726 | *input = AUDIO_IO_HANDLE_NONE; |
| 1727 | *inputType = API_INPUT_INVALID; |
| 1728 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1729 | halInputSource = inputSource; |
| 1730 | |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1731 | if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX && |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1732 | strncmp(attr->tags, "addr=", strlen("addr=")) == 0) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1733 | status = mPolicyMixes.getInputMixForAttr(*attr, &policyMix); |
| 1734 | if (status != NO_ERROR) { |
| 1735 | goto error; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 1736 | } |
| 1737 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1738 | device = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 1739 | address = String8(attr->tags + strlen("addr=")); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1740 | } else { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1741 | if (deviceDesc != 0) { |
| 1742 | device = deviceDesc->type(); |
| 1743 | } else { |
| 1744 | device = getDeviceAndMixForInputSource(inputSource, &policyMix); |
| 1745 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1746 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1747 | ALOGW("getInputForAttr() could not find device for source %d", inputSource); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1748 | status = BAD_VALUE; |
| 1749 | goto error; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1750 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1751 | if (policyMix != NULL) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1752 | address = policyMix->mDeviceAddress; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1753 | if (policyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1754 | // there is an external policy, but this input is attached to a mix of recorders, |
| 1755 | // meaning it receives audio injected into the framework, so the recorder doesn't |
| 1756 | // know about it and is therefore considered "legacy" |
| 1757 | *inputType = API_INPUT_LEGACY; |
| 1758 | } else { |
| 1759 | // recording a mix of players defined by an external policy, we're rerouting for |
| 1760 | // an external policy |
| 1761 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
| 1762 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1763 | } else if (audio_is_remote_submix_device(device)) { |
| 1764 | address = String8("0"); |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1765 | *inputType = API_INPUT_MIX_CAPTURE; |
Eric Laurent | 82db269 | 2015-08-07 13:59:42 -0700 | [diff] [blame] | 1766 | } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) { |
| 1767 | *inputType = API_INPUT_TELEPHONY_RX; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1768 | } else { |
| 1769 | *inputType = API_INPUT_LEGACY; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1770 | } |
Ravi Kumar Alamanda | b367f5b | 2015-08-25 08:21:37 -0700 | [diff] [blame] | 1771 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1772 | } |
| 1773 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1774 | *input = getInputForDevice(device, address, session, inputSource, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1775 | config, flags, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1776 | policyMix); |
| 1777 | if (*input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1778 | status = INVALID_OPERATION; |
| 1779 | goto error; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1780 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1781 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1782 | exit: |
| 1783 | |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 1784 | inputDevices = mAvailableInputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1785 | *selectedDeviceId = inputDevices.size() > 0 ? inputDevices.itemAt(0)->getId() |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1786 | : AUDIO_PORT_HANDLE_NONE; |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1787 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1788 | isSoundTrigger = inputSource == AUDIO_SOURCE_HOTWORD && |
| 1789 | mSoundTriggerSessions.indexOfKey(session) > 0; |
| 1790 | *portId = AudioPort::getNextUniqueId(); |
| 1791 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1792 | clientDesc = new RecordClientDescriptor(*portId, uid, session, |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1793 | *attr, *config, requestedDeviceId, |
| 1794 | inputSource,flags, isSoundTrigger); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1795 | inputDesc = mInputs.valueFor(*input); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1796 | inputDesc->clientsMap().emplace(*portId, clientDesc); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1797 | |
| 1798 | ALOGV("getInputForAttr() returns input %d type %d selectedDeviceId %d for port ID %d", |
| 1799 | *input, *inputType, *selectedDeviceId, *portId); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1800 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1801 | return NO_ERROR; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1802 | |
| 1803 | error: |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1804 | return status; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1805 | } |
| 1806 | |
| 1807 | |
| 1808 | audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device, |
| 1809 | String8 address, |
| 1810 | audio_session_t session, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1811 | audio_source_t inputSource, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1812 | const audio_config_base_t *config, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1813 | audio_input_flags_t flags, |
| 1814 | AudioMix *policyMix) |
| 1815 | { |
| 1816 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 1817 | audio_source_t halInputSource = inputSource; |
| 1818 | bool isSoundTrigger = false; |
| 1819 | |
| 1820 | if (inputSource == AUDIO_SOURCE_HOTWORD) { |
| 1821 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); |
| 1822 | if (index >= 0) { |
| 1823 | input = mSoundTriggerSessions.valueFor(session); |
| 1824 | isSoundTrigger = true; |
| 1825 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); |
| 1826 | ALOGV("SoundTrigger capture on session %d input %d", session, input); |
| 1827 | } else { |
| 1828 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1829 | } |
Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame] | 1830 | } else if (inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION && |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1831 | audio_is_linear_pcm(config->format)) { |
Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame] | 1832 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_VOIP_TX); |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1833 | } |
| 1834 | |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1835 | // find a compatible input profile (not necessarily identical in parameters) |
| 1836 | sp<IOProfile> profile; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1837 | // sampling rate and flags may be updated by getInputProfile |
| 1838 | uint32_t profileSamplingRate = (config->sample_rate == 0) ? |
| 1839 | SAMPLE_RATE_HZ_DEFAULT : config->sample_rate; |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 1840 | audio_format_t profileFormat; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1841 | audio_channel_mask_t profileChannelMask = config->channel_mask; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1842 | audio_input_flags_t profileFlags = flags; |
| 1843 | for (;;) { |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 1844 | 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] | 1845 | profile = getInputProfile(device, address, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1846 | profileSamplingRate, profileFormat, profileChannelMask, |
| 1847 | profileFlags); |
| 1848 | if (profile != 0) { |
| 1849 | break; // success |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1850 | } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) { |
| 1851 | profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1852 | } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) { |
| 1853 | profileFlags = AUDIO_INPUT_FLAG_NONE; // retry |
| 1854 | } else { // fail |
Glenn Kasten | faa10a6 | 2017-05-25 15:52:05 -0700 | [diff] [blame] | 1855 | ALOGW("getInputForDevice() could not find profile for device 0x%X, " |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1856 | "sampling rate %u, format %#x, channel mask 0x%X, flags %#x", |
| 1857 | device, config->sample_rate, config->format, config->channel_mask, flags); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1858 | return input; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1859 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1860 | } |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1861 | // Pick input sampling rate if not specified by client |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1862 | uint32_t samplingRate = config->sample_rate; |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1863 | if (samplingRate == 0) { |
| 1864 | samplingRate = profileSamplingRate; |
| 1865 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1866 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1867 | if (profile->getModuleHandle() == 0) { |
| 1868 | ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName()); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1869 | return input; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1870 | } |
| 1871 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1872 | if (!profile->canOpenNewIo()) { |
| 1873 | return AUDIO_IO_HANDLE_NONE; |
| 1874 | } |
| 1875 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1876 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1877 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1878 | audio_config_t lConfig = AUDIO_CONFIG_INITIALIZER; |
| 1879 | lConfig.sample_rate = profileSamplingRate; |
| 1880 | lConfig.channel_mask = profileChannelMask; |
| 1881 | lConfig.format = profileFormat; |
Eric Laurent | e301410 | 2017-05-03 11:15:43 -0700 | [diff] [blame] | 1882 | |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 1883 | if (address == "") { |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 1884 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 1885 | // the inputs vector must be of size >= 1, but we don't want to crash here |
| 1886 | address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress : String8(""); |
| 1887 | } |
| 1888 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1889 | status_t status = inputDesc->open(&lConfig, device, address, |
| 1890 | halInputSource, profileFlags, &input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1891 | |
| 1892 | // only accept input with the exact requested set of parameters |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1893 | if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1894 | (profileSamplingRate != lConfig.sample_rate) || |
| 1895 | !audio_formats_match(profileFormat, lConfig.format) || |
| 1896 | (profileChannelMask != lConfig.channel_mask)) { |
| 1897 | ALOGW("getInputForAttr() failed opening input: sampling rate %d" |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1898 | ", format %#x, channel mask %#x", |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1899 | profileSamplingRate, profileFormat, profileChannelMask); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1900 | if (input != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1901 | inputDesc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1902 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1903 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1904 | } |
| 1905 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1906 | inputDesc->mPolicyMix = policyMix; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1907 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1908 | addInput(input, inputDesc); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1909 | mpClientInterface->onAudioPortListUpdate(); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1910 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1911 | return input; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1912 | } |
| 1913 | |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1914 | //static |
| 1915 | bool AudioPolicyManager::isConcurrentSource(audio_source_t source) |
| 1916 | { |
| 1917 | return (source == AUDIO_SOURCE_HOTWORD) || |
| 1918 | (source == AUDIO_SOURCE_VOICE_RECOGNITION) || |
| 1919 | (source == AUDIO_SOURCE_FM_TUNER); |
| 1920 | } |
| 1921 | |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 1922 | // FIXME: remove when concurrent capture is ready. This is a hack to work around bug b/63083537. |
| 1923 | bool AudioPolicyManager::soundTriggerSupportsConcurrentCapture() { |
| 1924 | if (!mHasComputedSoundTriggerSupportsConcurrentCapture) { |
| 1925 | bool soundTriggerSupportsConcurrentCapture = false; |
| 1926 | unsigned int numModules = 0; |
| 1927 | struct sound_trigger_module_descriptor* nModules = NULL; |
| 1928 | |
| 1929 | status_t status = SoundTrigger::listModules(nModules, &numModules); |
| 1930 | if (status == NO_ERROR && numModules != 0) { |
| 1931 | nModules = (struct sound_trigger_module_descriptor*) calloc( |
| 1932 | numModules, sizeof(struct sound_trigger_module_descriptor)); |
| 1933 | if (nModules == NULL) { |
| 1934 | // We failed to malloc the buffer, so just say no for now, and hope that we have more |
| 1935 | // ram the next time this function is called. |
| 1936 | ALOGE("Failed to allocate buffer for module descriptors"); |
| 1937 | return false; |
| 1938 | } |
| 1939 | |
| 1940 | status = SoundTrigger::listModules(nModules, &numModules); |
| 1941 | if (status == NO_ERROR) { |
| 1942 | soundTriggerSupportsConcurrentCapture = true; |
| 1943 | for (size_t i = 0; i < numModules; ++i) { |
| 1944 | soundTriggerSupportsConcurrentCapture &= |
| 1945 | nModules[i].properties.concurrent_capture; |
| 1946 | } |
| 1947 | } |
| 1948 | free(nModules); |
| 1949 | } |
| 1950 | mSoundTriggerSupportsConcurrentCapture = soundTriggerSupportsConcurrentCapture; |
| 1951 | mHasComputedSoundTriggerSupportsConcurrentCapture = true; |
| 1952 | } |
| 1953 | return mSoundTriggerSupportsConcurrentCapture; |
| 1954 | } |
| 1955 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1956 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1957 | status_t AudioPolicyManager::startInput(audio_port_handle_t portId, |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1958 | bool silenced, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1959 | concurrency_type__mask_t *concurrency) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1960 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1961 | *concurrency = API_INPUT_CONCURRENCY_NONE; |
| 1962 | |
| 1963 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 1964 | |
| 1965 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 1966 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1967 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1968 | return BAD_VALUE; |
| 1969 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1970 | audio_io_handle_t input = inputDesc->mIoHandle; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1971 | sp<RecordClientDescriptor> client = inputDesc->clientsMap()[portId]; |
| 1972 | if (client->active()) { |
| 1973 | ALOGW("%s input %d client %d already started", __FUNCTION__, input, client->portId()); |
| 1974 | return INVALID_OPERATION; |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1975 | } |
| 1976 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1977 | audio_session_t session = client->session(); |
| 1978 | |
| 1979 | ALOGV("%s input:%d, session:%d, silenced:%d, concurrency:%d)", |
| 1980 | __FUNCTION__, input, session, silenced, *concurrency); |
| 1981 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1982 | if (!is_virtual_input_device(inputDesc->mDevice)) { |
| 1983 | if (mCallTxPatch != 0 && |
| 1984 | inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) { |
| 1985 | ALOGW("startInput(%d) failed: call in progress", input); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 1986 | *concurrency |= API_INPUT_CONCURRENCY_CALL; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1987 | return INVALID_OPERATION; |
| 1988 | } |
| 1989 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1990 | Vector<sp<AudioInputDescriptor>> activeInputs = mInputs.getActiveInputs(); |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1991 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1992 | // If a UID is idle and records silence and another not silenced recording starts |
| 1993 | // from another UID (idle or active) we stop the current idle UID recording in |
| 1994 | // favor of the new one - "There can be only one" TM |
| 1995 | if (!silenced) { |
| 1996 | for (const auto& activeDesc : activeInputs) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1997 | if ((activeDesc->getAudioPort()->getFlags() & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0 && |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1998 | activeDesc->getId() == inputDesc->getId()) { |
| 1999 | continue; |
| 2000 | } |
| 2001 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2002 | RecordClientVector activeClients = activeDesc->clientsList(true /*activeOnly*/); |
| 2003 | for (const auto& activeClient : activeClients) { |
| 2004 | if (activeClient->isSilenced()) { |
| 2005 | closeClient(activeClient->portId()); |
| 2006 | ALOGV("%s client %d stopping silenced client %d", __FUNCTION__, |
| 2007 | portId, activeClient->portId()); |
| 2008 | activeInputs = mInputs.getActiveInputs(); |
| 2009 | } |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 2010 | } |
| 2011 | } |
| 2012 | } |
| 2013 | |
| 2014 | for (const auto& activeDesc : activeInputs) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2015 | if ((client->flags() & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0 && |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 2016 | activeDesc->getId() == inputDesc->getId()) { |
| 2017 | continue; |
| 2018 | } |
| 2019 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2020 | audio_source_t activeSource = activeDesc->inputSource(true); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2021 | if (client->source() == AUDIO_SOURCE_HOTWORD) { |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2022 | if (activeSource == AUDIO_SOURCE_HOTWORD) { |
| 2023 | if (activeDesc->hasPreemptedSession(session)) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2024 | ALOGW("%s input %d failed for HOTWORD: " |
| 2025 | "other input %d already started for HOTWORD", __FUNCTION__, |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2026 | input, activeDesc->mIoHandle); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 2027 | *concurrency |= API_INPUT_CONCURRENCY_HOTWORD; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2028 | return INVALID_OPERATION; |
| 2029 | } |
| 2030 | } else { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2031 | ALOGV("%s input %d failed for HOTWORD: other input %d already started", |
| 2032 | __FUNCTION__, input, activeDesc->mIoHandle); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 2033 | *concurrency |= API_INPUT_CONCURRENCY_CAPTURE; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2034 | return INVALID_OPERATION; |
| 2035 | } |
| 2036 | } else { |
| 2037 | if (activeSource != AUDIO_SOURCE_HOTWORD) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2038 | ALOGW("%s input %d failed: other input %d already started", __FUNCTION__, |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2039 | input, activeDesc->mIoHandle); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 2040 | *concurrency |= API_INPUT_CONCURRENCY_CAPTURE; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2041 | return INVALID_OPERATION; |
| 2042 | } |
| 2043 | } |
| 2044 | } |
| 2045 | |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 2046 | // We only need to check if the sound trigger session supports concurrent capture if the |
| 2047 | // input is also a sound trigger input. Otherwise, we should preempt any hotword stream |
| 2048 | // that's running. |
| 2049 | const bool allowConcurrentWithSoundTrigger = |
| 2050 | inputDesc->isSoundTrigger() ? soundTriggerSupportsConcurrentCapture() : false; |
| 2051 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2052 | // if capture is allowed, preempt currently active HOTWORD captures |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2053 | for (const auto& activeDesc : activeInputs) { |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 2054 | if (allowConcurrentWithSoundTrigger && activeDesc->isSoundTrigger()) { |
| 2055 | continue; |
| 2056 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2057 | RecordClientVector activeHotwordClients = |
| 2058 | activeDesc->clientsList(true, AUDIO_SOURCE_HOTWORD); |
| 2059 | if (activeHotwordClients.size() > 0) { |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2060 | SortedVector<audio_session_t> sessions = activeDesc->getPreemptedSessions(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2061 | |
| 2062 | for (const auto& activeClient : activeHotwordClients) { |
| 2063 | *concurrency |= API_INPUT_CONCURRENCY_PREEMPT; |
| 2064 | sessions.add(activeClient->session()); |
| 2065 | closeClient(activeClient->portId()); |
| 2066 | ALOGV("%s input %d for HOTWORD preempting HOTWORD input %d", __FUNCTION__, |
| 2067 | input, activeDesc->mIoHandle); |
| 2068 | } |
| 2069 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2070 | inputDesc->setPreemptedSessions(sessions); |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2071 | } |
| 2072 | } |
| 2073 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2074 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 2075 | // Make sure we start with the correct silence state |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2076 | client->setSilenced(silenced); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 2077 | |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 2078 | // increment activity count before calling getNewInputDevice() below as only active sessions |
| 2079 | // are considered for device selection |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2080 | inputDesc->setClientActive(client, true); |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 2081 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2082 | // indicate active capture to sound trigger service if starting capture from a mic on |
| 2083 | // primary HW module |
| 2084 | audio_devices_t device = getNewInputDevice(inputDesc); |
| 2085 | setInputDevice(input, device, true /* force */); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2086 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2087 | status_t status = inputDesc->start(); |
| 2088 | if (status != NO_ERROR) { |
| 2089 | inputDesc->setClientActive(client, false); |
| 2090 | return status; |
| 2091 | } |
| 2092 | |
| 2093 | if (inputDesc->activeCount() == 1) { |
| 2094 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 2095 | if ((inputDesc->mPolicyMix != NULL) |
| 2096 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 2097 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
| 2098 | MIX_STATE_MIXING); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2099 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2100 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2101 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); |
| 2102 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
| 2103 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) { |
| 2104 | SoundTrigger::setCaptureState(true); |
| 2105 | } |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2106 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2107 | // automatically enable the remote submix output when input is started if not |
| 2108 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 2109 | // For remote submix (a virtual device), we open only one input per capture request. |
| 2110 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 2111 | String8 address = String8(""); |
| 2112 | if (inputDesc->mPolicyMix == NULL) { |
| 2113 | address = String8("0"); |
| 2114 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 2115 | address = inputDesc->mPolicyMix->mDeviceAddress; |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2116 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2117 | if (address != "") { |
| 2118 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2119 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2120 | address, "remote-submix"); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2121 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 2122 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2123 | } |
| 2124 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2125 | ALOGV("%s input %d source = %d exit", __FUNCTION__, input, client->source()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2126 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2127 | return NO_ERROR; |
| 2128 | } |
| 2129 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2130 | status_t AudioPolicyManager::stopInput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2131 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2132 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2133 | |
| 2134 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 2135 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2136 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2137 | return BAD_VALUE; |
| 2138 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2139 | audio_io_handle_t input = inputDesc->mIoHandle; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2140 | sp<RecordClientDescriptor> client = inputDesc->clientsMap()[portId]; |
| 2141 | if (!client->active()) { |
| 2142 | ALOGW("%s input %d client %d already stopped", __FUNCTION__, input, client->portId()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2143 | return INVALID_OPERATION; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2144 | } |
| 2145 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2146 | inputDesc->setClientActive(client, false); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2147 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2148 | inputDesc->stop(); |
| 2149 | if (inputDesc->isActive()) { |
| 2150 | setInputDevice(input, getNewInputDevice(inputDesc), false /* force */); |
| 2151 | } else { |
| 2152 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 2153 | if ((inputDesc->mPolicyMix != NULL) |
| 2154 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 2155 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
| 2156 | MIX_STATE_IDLE); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2157 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2158 | |
| 2159 | // automatically disable the remote submix output when input is stopped if not |
| 2160 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 2161 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 2162 | String8 address = String8(""); |
| 2163 | if (inputDesc->mPolicyMix == NULL) { |
| 2164 | address = String8("0"); |
| 2165 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 2166 | address = inputDesc->mPolicyMix->mDeviceAddress; |
| 2167 | } |
| 2168 | if (address != "") { |
| 2169 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2170 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2171 | address, "remote-submix"); |
| 2172 | } |
| 2173 | } |
| 2174 | |
| 2175 | audio_devices_t device = inputDesc->mDevice; |
| 2176 | resetInputDevice(input); |
| 2177 | |
| 2178 | // indicate inactive capture to sound trigger service if stopping capture from a mic on |
| 2179 | // primary HW module |
| 2180 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); |
| 2181 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
| 2182 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
| 2183 | SoundTrigger::setCaptureState(false); |
| 2184 | } |
| 2185 | inputDesc->clearPreemptedSessions(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2186 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2187 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2188 | } |
| 2189 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2190 | void AudioPolicyManager::releaseInput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2191 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2192 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2193 | |
| 2194 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 2195 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2196 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2197 | return; |
| 2198 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2199 | sp<RecordClientDescriptor> client = inputDesc->clientsMap()[portId]; |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2200 | audio_io_handle_t input = inputDesc->mIoHandle; |
| 2201 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2202 | ALOGV("%s %d", __FUNCTION__, input); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2203 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2204 | inputDesc->clientsMap().erase(portId); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2205 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2206 | if (inputDesc->clientsMap().size() > 0) { |
| 2207 | ALOGV("%s %zu clients remaining", __FUNCTION__, inputDesc->clientsMap().size()); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2208 | return; |
| 2209 | } |
| 2210 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2211 | closeInput(input); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2212 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2213 | ALOGV("%s exit", __FUNCTION__); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2214 | } |
| 2215 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2216 | void AudioPolicyManager::closeActiveClients(const sp<AudioInputDescriptor>& input) |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2217 | { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2218 | RecordClientVector clients = input->clientsList(true); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2219 | |
| 2220 | for (const auto& client : clients) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2221 | closeClient(client->portId()); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2222 | } |
| 2223 | } |
| 2224 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2225 | void AudioPolicyManager::closeClient(audio_port_handle_t portId) |
| 2226 | { |
| 2227 | stopInput(portId); |
| 2228 | releaseInput(portId); |
| 2229 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2230 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2231 | void AudioPolicyManager::closeAllInputs() { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2232 | bool patchRemoved = false; |
| 2233 | |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 2234 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2235 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index); |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 2236 | ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2237 | if (patch_index >= 0) { |
| 2238 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 2239 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2240 | mAudioPatches.removeItemsAt(patch_index); |
| 2241 | patchRemoved = true; |
| 2242 | } |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2243 | inputDesc->close(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2244 | } |
| 2245 | mInputs.clear(); |
Chris Thornton | 52785f3 | 2016-02-09 17:28:49 -0800 | [diff] [blame] | 2246 | SoundTrigger::setCaptureState(false); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2247 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2248 | |
| 2249 | if (patchRemoved) { |
| 2250 | mpClientInterface->onAudioPatchListUpdate(); |
| 2251 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2252 | } |
| 2253 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2254 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2255 | int indexMin, |
| 2256 | int indexMax) |
| 2257 | { |
| 2258 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2259 | mVolumeCurves->initStreamVolume(stream, indexMin, indexMax); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2260 | |
| 2261 | // initialize other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2262 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2263 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2264 | continue; |
| 2265 | } |
| 2266 | mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2267 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2268 | } |
| 2269 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2270 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2271 | int index, |
| 2272 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2273 | { |
| 2274 | |
Nadav Bar | 7c605f6 | 2017-12-25 00:01:52 +0200 | [diff] [blame] | 2275 | // VOICE_CALL stream has minVolumeIndex > 0 but can be muted directly by an |
| 2276 | // app that has MODIFY_PHONE_STATE permission. |
| 2277 | if (((index < mVolumeCurves->getVolumeIndexMin(stream)) && |
| 2278 | !(stream == AUDIO_STREAM_VOICE_CALL && index == 0)) || |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2279 | (index > mVolumeCurves->getVolumeIndexMax(stream))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2280 | return BAD_VALUE; |
| 2281 | } |
| 2282 | if (!audio_is_output_device(device)) { |
| 2283 | return BAD_VALUE; |
| 2284 | } |
| 2285 | |
| 2286 | // Force max volume if stream cannot be muted |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2287 | if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2288 | |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2289 | ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2290 | stream, device, index); |
| 2291 | |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2292 | // update other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2293 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2294 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2295 | continue; |
| 2296 | } |
| 2297 | mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index); |
| 2298 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2299 | |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2300 | // update volume on all outputs and streams matching the following: |
| 2301 | // - The requested stream (or a stream matching for volume control) is active on the output |
| 2302 | // - The device (or devices) selected by the strategy corresponding to this stream includes |
| 2303 | // the requested device |
| 2304 | // - For non default requested device, currently selected device on the output is either the |
| 2305 | // requested device or one of the devices selected by the strategy |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2306 | // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if |
| 2307 | // no specific device volume value exists for currently selected device. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2308 | status_t status = NO_ERROR; |
| 2309 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2310 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 2311 | audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device()); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2312 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 2313 | if (!(streamsMatchForvolume(stream, (audio_stream_type_t)curStream))) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2314 | continue; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2315 | } |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 2316 | if (!(desc->isStreamActive((audio_stream_type_t)curStream) || isInCall())) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2317 | continue; |
| 2318 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2319 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Jean-Michel Trivi | b7fdce6 | 2017-06-27 19:38:42 -0700 | [diff] [blame] | 2320 | audio_devices_t curStreamDevice = Volume::getDeviceForVolume(getDeviceForStrategy( |
| 2321 | curStrategy, false /*fromCache*/)); |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2322 | if ((device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) && |
| 2323 | ((curStreamDevice & device) == 0)) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2324 | continue; |
| 2325 | } |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2326 | bool applyVolume; |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2327 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2328 | curStreamDevice |= device; |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2329 | applyVolume = (curDevice & curStreamDevice) != 0; |
| 2330 | } else { |
| 2331 | applyVolume = !mVolumeCurves->hasVolumeIndexForDevice( |
Jean-Michel Trivi | b7fdce6 | 2017-06-27 19:38:42 -0700 | [diff] [blame] | 2332 | stream, curStreamDevice); |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2333 | } |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 2334 | // rescale index before applying to curStream as ranges may be different for |
| 2335 | // stream and curStream |
| 2336 | int idx = rescaleVolumeIndex(index, stream, (audio_stream_type_t)curStream); |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2337 | if (applyVolume) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2338 | //FIXME: workaround for truncated touch sounds |
| 2339 | // delayed volume change for system stream to be removed when the problem is |
| 2340 | // handled by system UI |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2341 | status_t volStatus = |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 2342 | checkAndSetVolume((audio_stream_type_t)curStream, idx, desc, curDevice, |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2343 | (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2344 | if (volStatus != NO_ERROR) { |
| 2345 | status = volStatus; |
| 2346 | } |
| 2347 | } |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2348 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2349 | } |
| 2350 | return status; |
| 2351 | } |
| 2352 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2353 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2354 | int *index, |
| 2355 | audio_devices_t device) |
| 2356 | { |
| 2357 | if (index == NULL) { |
| 2358 | return BAD_VALUE; |
| 2359 | } |
| 2360 | if (!audio_is_output_device(device)) { |
| 2361 | return BAD_VALUE; |
| 2362 | } |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2363 | // 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] | 2364 | // the strategy the stream belongs to. |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2365 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2366 | device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); |
| 2367 | } |
François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 2368 | device = Volume::getDeviceForVolume(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2369 | |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2370 | *index = mVolumeCurves->getVolumeIndex(stream, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2371 | ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index); |
| 2372 | return NO_ERROR; |
| 2373 | } |
| 2374 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2375 | audio_io_handle_t AudioPolicyManager::selectOutputForMusicEffects() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2376 | { |
| 2377 | // select one output among several suitable for global effects. |
| 2378 | // The priority is as follows: |
| 2379 | // 1: An offloaded output. If the effect ends up not being offloadable, |
| 2380 | // AudioFlinger will invalidate the track and the offloaded output |
| 2381 | // will be closed causing the effect to be moved to a PCM output. |
| 2382 | // 2: A deep buffer output |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2383 | // 3: The primary output |
| 2384 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2385 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2386 | routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2387 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2388 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2389 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2390 | if (outputs.size() == 0) { |
| 2391 | return AUDIO_IO_HANDLE_NONE; |
| 2392 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2393 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2394 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 2395 | bool activeOnly = true; |
| 2396 | |
| 2397 | while (output == AUDIO_IO_HANDLE_NONE) { |
| 2398 | audio_io_handle_t outputOffloaded = AUDIO_IO_HANDLE_NONE; |
| 2399 | audio_io_handle_t outputDeepBuffer = AUDIO_IO_HANDLE_NONE; |
| 2400 | audio_io_handle_t outputPrimary = AUDIO_IO_HANDLE_NONE; |
| 2401 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2402 | for (audio_io_handle_t output : outputs) { |
| 2403 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2404 | if (activeOnly && !desc->isStreamActive(AUDIO_STREAM_MUSIC)) { |
| 2405 | continue; |
| 2406 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2407 | ALOGV("selectOutputForMusicEffects activeOnly %d output %d flags 0x%08x", |
| 2408 | activeOnly, output, desc->mFlags); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2409 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2410 | outputOffloaded = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2411 | } |
| 2412 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2413 | outputDeepBuffer = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2414 | } |
| 2415 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2416 | outputPrimary = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2417 | } |
| 2418 | } |
| 2419 | if (outputOffloaded != AUDIO_IO_HANDLE_NONE) { |
| 2420 | output = outputOffloaded; |
| 2421 | } else if (outputDeepBuffer != AUDIO_IO_HANDLE_NONE) { |
| 2422 | output = outputDeepBuffer; |
| 2423 | } else if (outputPrimary != AUDIO_IO_HANDLE_NONE) { |
| 2424 | output = outputPrimary; |
| 2425 | } else { |
| 2426 | output = outputs[0]; |
| 2427 | } |
| 2428 | activeOnly = false; |
| 2429 | } |
| 2430 | |
| 2431 | if (output != mMusicEffectOutput) { |
| 2432 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output); |
| 2433 | mMusicEffectOutput = output; |
| 2434 | } |
| 2435 | |
| 2436 | ALOGV("selectOutputForMusicEffects selected output %d", output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2437 | return output; |
| 2438 | } |
| 2439 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2440 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc __unused) |
| 2441 | { |
| 2442 | return selectOutputForMusicEffects(); |
| 2443 | } |
| 2444 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2445 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2446 | audio_io_handle_t io, |
| 2447 | uint32_t strategy, |
| 2448 | int session, |
| 2449 | int id) |
| 2450 | { |
| 2451 | ssize_t index = mOutputs.indexOfKey(io); |
| 2452 | if (index < 0) { |
| 2453 | index = mInputs.indexOfKey(io); |
| 2454 | if (index < 0) { |
| 2455 | ALOGW("registerEffect() unknown io %d", io); |
| 2456 | return INVALID_OPERATION; |
| 2457 | } |
| 2458 | } |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2459 | return mEffects.registerEffect(desc, io, strategy, session, id); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2460 | } |
| 2461 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2462 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2463 | { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2464 | bool active = false; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2465 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) { |
| 2466 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2467 | continue; |
| 2468 | } |
| 2469 | active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs); |
| 2470 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2471 | return active; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2472 | } |
| 2473 | |
| 2474 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2475 | { |
| 2476 | return mOutputs.isStreamActiveRemotely(stream, inPastMs); |
| 2477 | } |
| 2478 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2479 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2480 | { |
| 2481 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2482 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2483 | if (inputDescriptor->isSourceActive(source)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2484 | return true; |
| 2485 | } |
| 2486 | } |
| 2487 | return false; |
| 2488 | } |
| 2489 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2490 | // Register a list of custom mixes with their attributes and format. |
| 2491 | // When a mix is registered, corresponding input and output profiles are |
| 2492 | // added to the remote submix hw module. The profile contains only the |
| 2493 | // parameters (sampling rate, format...) specified by the mix. |
| 2494 | // The corresponding input remote submix device is also connected. |
| 2495 | // |
| 2496 | // When a remote submix device is connected, the address is checked to select the |
| 2497 | // appropriate profile and the corresponding input or output stream is opened. |
| 2498 | // |
| 2499 | // When capture starts, getInputForAttr() will: |
| 2500 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2501 | // - 2 if none found, getDeviceForInputSource() will: |
| 2502 | // - 2.1 look for a mix matching the attributes source |
| 2503 | // - 2.2 if none found, default to device selection by policy rules |
| 2504 | // At this time, the corresponding output remote submix device is also connected |
| 2505 | // and active playback use cases can be transferred to this mix if needed when reconnecting |
| 2506 | // after AudioTracks are invalidated |
| 2507 | // |
| 2508 | // When playback starts, getOutputForAttr() will: |
| 2509 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2510 | // - 2 if none found, look for a mix matching the attributes usage |
| 2511 | // - 3 if none found, default to device and output selection by policy rules. |
| 2512 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 2513 | status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes) |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2514 | { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2515 | ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size()); |
| 2516 | status_t res = NO_ERROR; |
| 2517 | |
| 2518 | sp<HwModule> rSubmixModule; |
| 2519 | // examine each mix's route type |
| 2520 | for (size_t i = 0; i < mixes.size(); i++) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2521 | AudioMix mix = mixes[i]; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2522 | // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2523 | if ((mix.mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2524 | res = INVALID_OPERATION; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2525 | break; |
| 2526 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2527 | if ((mix.mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2528 | 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] | 2529 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 2530 | rSubmixModule = mHwModules.getModuleFromName( |
| 2531 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 2532 | if (rSubmixModule == 0) { |
| 2533 | ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", |
| 2534 | i); |
| 2535 | res = INVALID_OPERATION; |
| 2536 | break; |
| 2537 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2538 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2539 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2540 | String8 address = mix.mDeviceAddress; |
| 2541 | if (mix.mMixType == MIX_TYPE_PLAYERS) { |
| 2542 | mix.mDeviceType = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 2543 | } else { |
| 2544 | mix.mDeviceType = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
| 2545 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2546 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2547 | if (mPolicyMixes.registerMix(address, mix, 0 /*output desc*/) != NO_ERROR) { |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2548 | ALOGE(" Error registering mix %zu for address %s", i, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2549 | res = INVALID_OPERATION; |
| 2550 | break; |
| 2551 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2552 | audio_config_t outputConfig = mix.mFormat; |
| 2553 | audio_config_t inputConfig = mix.mFormat; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2554 | // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in |
| 2555 | // stereo and let audio flinger do the channel conversion if needed. |
| 2556 | outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 2557 | inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
| 2558 | rSubmixModule->addOutputProfile(address, &outputConfig, |
| 2559 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address); |
| 2560 | rSubmixModule->addInputProfile(address, &inputConfig, |
| 2561 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, address); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2562 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2563 | if (mix.mMixType == MIX_TYPE_PLAYERS) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2564 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2565 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2566 | address.string(), "remote-submix"); |
| 2567 | } else { |
| 2568 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2569 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2570 | address.string(), "remote-submix"); |
| 2571 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2572 | } else if ((mix.mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 2573 | String8 address = mix.mDeviceAddress; |
| 2574 | audio_devices_t device = mix.mDeviceType; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2575 | ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s", |
| 2576 | i, mixes.size(), device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2577 | |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2578 | bool foundOutput = false; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2579 | for (size_t j = 0 ; j < mOutputs.size() ; j++) { |
| 2580 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j); |
| 2581 | sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle()); |
| 2582 | if ((patch != 0) && (patch->mPatch.num_sinks != 0) |
| 2583 | && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE) |
| 2584 | && (patch->mPatch.sinks[0].ext.device.type == device) |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2585 | && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(), |
| 2586 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2587 | if (mPolicyMixes.registerMix(address, mix, desc) != NO_ERROR) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2588 | res = INVALID_OPERATION; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2589 | } else { |
| 2590 | foundOutput = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2591 | } |
| 2592 | break; |
| 2593 | } |
| 2594 | } |
| 2595 | |
| 2596 | if (res != NO_ERROR) { |
| 2597 | ALOGE(" Error registering mix %zu for device 0x%X addr %s", |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2598 | i, device, address.string()); |
| 2599 | res = INVALID_OPERATION; |
| 2600 | break; |
| 2601 | } else if (!foundOutput) { |
| 2602 | ALOGE(" Output not found for mix %zu for device 0x%X addr %s", |
| 2603 | i, device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2604 | res = INVALID_OPERATION; |
| 2605 | break; |
| 2606 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2607 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2608 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2609 | if (res != NO_ERROR) { |
| 2610 | unregisterPolicyMixes(mixes); |
| 2611 | } |
| 2612 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2613 | } |
| 2614 | |
| 2615 | status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes) |
| 2616 | { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2617 | ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2618 | status_t res = NO_ERROR; |
| 2619 | sp<HwModule> rSubmixModule; |
| 2620 | // examine each mix's route type |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2621 | for (const auto& mix : mixes) { |
| 2622 | 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] | 2623 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2624 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 2625 | rSubmixModule = mHwModules.getModuleFromName( |
| 2626 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 2627 | if (rSubmixModule == 0) { |
| 2628 | res = INVALID_OPERATION; |
| 2629 | continue; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2630 | } |
| 2631 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2632 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2633 | String8 address = mix.mDeviceAddress; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2634 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2635 | if (mPolicyMixes.unregisterMix(address) != NO_ERROR) { |
| 2636 | res = INVALID_OPERATION; |
| 2637 | continue; |
| 2638 | } |
| 2639 | |
| 2640 | if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) == |
| 2641 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2642 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2643 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2644 | address.string(), "remote-submix"); |
| 2645 | } |
| 2646 | if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) == |
| 2647 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2648 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2649 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2650 | address.string(), "remote-submix"); |
| 2651 | } |
| 2652 | rSubmixModule->removeOutputProfile(address); |
| 2653 | rSubmixModule->removeInputProfile(address); |
| 2654 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2655 | } if ((mix.mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 2656 | if (mPolicyMixes.unregisterMix(mix.mDeviceAddress) != NO_ERROR) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2657 | res = INVALID_OPERATION; |
| 2658 | continue; |
| 2659 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2660 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2661 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2662 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2663 | } |
| 2664 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2665 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2666 | status_t AudioPolicyManager::dump(int fd) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2667 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2668 | String8 result; |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 2669 | result.appendFormat("\nAudioPolicyManager Dump: %p\n", this); |
| 2670 | result.appendFormat(" Primary Output: %d\n", |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 2671 | hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE); |
Mikhail Naganov | 0d6a033 | 2016-04-19 17:12:38 -0700 | [diff] [blame] | 2672 | std::string stateLiteral; |
| 2673 | AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral); |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 2674 | result.appendFormat(" Phone state: %s\n", stateLiteral.c_str()); |
| 2675 | const char* forceUses[AUDIO_POLICY_FORCE_USE_CNT] = { |
| 2676 | "communications", "media", "record", "dock", "system", |
| 2677 | "HDMI system audio", "encoded surround output", "vibrate ringing" }; |
| 2678 | for (audio_policy_force_use_t i = AUDIO_POLICY_FORCE_FOR_COMMUNICATION; |
| 2679 | i < AUDIO_POLICY_FORCE_USE_CNT; i = (audio_policy_force_use_t)((int)i + 1)) { |
| 2680 | result.appendFormat(" Force use for %s: %d\n", |
| 2681 | forceUses[i], mEngine->getForceUse(i)); |
| 2682 | } |
| 2683 | result.appendFormat(" TTS output %savailable\n", mTtsOutputAvailable ? "" : "not "); |
| 2684 | result.appendFormat(" Master mono: %s\n", mMasterMono ? "on" : "off"); |
| 2685 | result.appendFormat(" Config source: %s\n", getConfig().getSource().c_str()); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2686 | write(fd, result.string(), result.size()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2687 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 2688 | mAvailableOutputDevices.dump(fd, String8("Available output")); |
| 2689 | mAvailableInputDevices.dump(fd, String8("Available input")); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 2690 | mHwModulesAll.dump(fd); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2691 | mOutputs.dump(fd); |
| 2692 | mInputs.dump(fd); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2693 | mVolumeCurves->dump(fd); |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2694 | mEffects.dump(fd); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2695 | mAudioPatches.dump(fd); |
Mikhail Naganov | 44344b0 | 2016-12-13 11:21:02 -0800 | [diff] [blame] | 2696 | mPolicyMixes.dump(fd); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 2697 | mAudioSources.dump(fd); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2698 | |
| 2699 | return NO_ERROR; |
| 2700 | } |
| 2701 | |
| 2702 | // This function checks for the parameters which can be offloaded. |
| 2703 | // This can be enhanced depending on the capability of the DSP and policy |
| 2704 | // of the system. |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2705 | bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2706 | { |
| 2707 | ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2708 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2709 | offloadInfo.sample_rate, offloadInfo.channel_mask, |
| 2710 | offloadInfo.format, |
| 2711 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, |
| 2712 | offloadInfo.has_video); |
| 2713 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 2714 | if (mMasterMono) { |
| 2715 | return false; // no offloading if mono is set. |
| 2716 | } |
| 2717 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2718 | // Check if offload has been disabled |
Andy Hung | 0f6e640 | 2018-09-06 11:20:45 -0700 | [diff] [blame] | 2719 | if (property_get_bool("audio.offload.disable", false /* default_value */)) { |
| 2720 | ALOGV("offload disabled by audio.offload.disable"); |
| 2721 | return false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2722 | } |
| 2723 | |
| 2724 | // Check if stream type is music, then only allow offload as of now. |
| 2725 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) |
| 2726 | { |
| 2727 | ALOGV("isOffloadSupported: stream_type != MUSIC, returning false"); |
| 2728 | return false; |
| 2729 | } |
| 2730 | |
| 2731 | //TODO: enable audio offloading with video when ready |
Andy Hung | 08945c4 | 2015-05-31 21:36:46 -0700 | [diff] [blame] | 2732 | const bool allowOffloadWithVideo = |
| 2733 | property_get_bool("audio.offload.video", false /* default_value */); |
| 2734 | if (offloadInfo.has_video && !allowOffloadWithVideo) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2735 | ALOGV("isOffloadSupported: has_video == true, returning false"); |
| 2736 | return false; |
| 2737 | } |
| 2738 | |
| 2739 | //If duration is less than minimum value defined in property, return false |
Andy Hung | 0f6e640 | 2018-09-06 11:20:45 -0700 | [diff] [blame] | 2740 | const int min_duration_secs = property_get_int32( |
| 2741 | "audio.offload.min.duration.secs", -1 /* default_value */); |
| 2742 | if (min_duration_secs >= 0) { |
| 2743 | if (offloadInfo.duration_us < min_duration_secs * 1000000LL) { |
| 2744 | ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%d)", |
| 2745 | min_duration_secs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2746 | return false; |
| 2747 | } |
| 2748 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { |
| 2749 | ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS); |
| 2750 | return false; |
| 2751 | } |
| 2752 | |
| 2753 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 2754 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 2755 | // detects there is an active non offloadable effect. |
| 2756 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 2757 | // This may prevent offloading in rare situations where effects are left active by apps |
| 2758 | // in the background. |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2759 | if (mEffects.isNonOffloadableEffectEnabled()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2760 | return false; |
| 2761 | } |
| 2762 | |
| 2763 | // See if there is a profile to support this. |
| 2764 | // AUDIO_DEVICE_NONE |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2765 | sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2766 | offloadInfo.sample_rate, |
| 2767 | offloadInfo.format, |
| 2768 | offloadInfo.channel_mask, |
| 2769 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2770 | ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT "); |
| 2771 | return (profile != 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2772 | } |
| 2773 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2774 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, |
| 2775 | audio_port_type_t type, |
| 2776 | unsigned int *num_ports, |
| 2777 | struct audio_port *ports, |
| 2778 | unsigned int *generation) |
| 2779 | { |
| 2780 | if (num_ports == NULL || (*num_ports != 0 && ports == NULL) || |
| 2781 | generation == NULL) { |
| 2782 | return BAD_VALUE; |
| 2783 | } |
| 2784 | ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports); |
| 2785 | if (ports == NULL) { |
| 2786 | *num_ports = 0; |
| 2787 | } |
| 2788 | |
| 2789 | size_t portsWritten = 0; |
| 2790 | size_t portsMax = *num_ports; |
| 2791 | *num_ports = 0; |
| 2792 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2793 | // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB |
| 2794 | // as they are used by stub HALs by convention |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2795 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2796 | for (const auto& dev : mAvailableOutputDevices) { |
| 2797 | if (dev->type() == AUDIO_DEVICE_OUT_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2798 | continue; |
| 2799 | } |
| 2800 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2801 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2802 | } |
| 2803 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2804 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2805 | } |
| 2806 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2807 | for (const auto& dev : mAvailableInputDevices) { |
| 2808 | if (dev->type() == AUDIO_DEVICE_IN_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2809 | continue; |
| 2810 | } |
| 2811 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2812 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2813 | } |
| 2814 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2815 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2816 | } |
| 2817 | } |
| 2818 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { |
| 2819 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 2820 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { |
| 2821 | mInputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2822 | } |
| 2823 | *num_ports += mInputs.size(); |
| 2824 | } |
| 2825 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2826 | size_t numOutputs = 0; |
| 2827 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2828 | if (!mOutputs[i]->isDuplicated()) { |
| 2829 | numOutputs++; |
| 2830 | if (portsWritten < portsMax) { |
| 2831 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2832 | } |
| 2833 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2834 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2835 | *num_ports += numOutputs; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2836 | } |
| 2837 | } |
| 2838 | *generation = curAudioPortGeneration(); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2839 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2840 | return NO_ERROR; |
| 2841 | } |
| 2842 | |
Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 2843 | status_t AudioPolicyManager::getAudioPort(struct audio_port *port) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2844 | { |
Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 2845 | if (port == nullptr || port->id == AUDIO_PORT_HANDLE_NONE) { |
| 2846 | return BAD_VALUE; |
| 2847 | } |
| 2848 | sp<DeviceDescriptor> dev = mAvailableOutputDevices.getDeviceFromId(port->id); |
| 2849 | if (dev != 0) { |
| 2850 | dev->toAudioPort(port); |
| 2851 | return NO_ERROR; |
| 2852 | } |
| 2853 | dev = mAvailableInputDevices.getDeviceFromId(port->id); |
| 2854 | if (dev != 0) { |
| 2855 | dev->toAudioPort(port); |
| 2856 | return NO_ERROR; |
| 2857 | } |
| 2858 | sp<SwAudioOutputDescriptor> out = mOutputs.getOutputFromId(port->id); |
| 2859 | if (out != 0) { |
| 2860 | out->toAudioPort(port); |
| 2861 | return NO_ERROR; |
| 2862 | } |
| 2863 | sp<AudioInputDescriptor> in = mInputs.getInputFromId(port->id); |
| 2864 | if (in != 0) { |
| 2865 | in->toAudioPort(port); |
| 2866 | return NO_ERROR; |
| 2867 | } |
| 2868 | return BAD_VALUE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2869 | } |
| 2870 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2871 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, |
| 2872 | audio_patch_handle_t *handle, |
| 2873 | uid_t uid) |
| 2874 | { |
| 2875 | ALOGV("createAudioPatch()"); |
| 2876 | |
| 2877 | if (handle == NULL || patch == NULL) { |
| 2878 | return BAD_VALUE; |
| 2879 | } |
| 2880 | ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks); |
| 2881 | |
Mikhail Naganov | ac9858b | 2018-06-15 13:12:37 -0700 | [diff] [blame] | 2882 | if (!audio_patch_is_valid(patch)) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2883 | return BAD_VALUE; |
| 2884 | } |
| 2885 | // only one source per audio patch supported for now |
| 2886 | if (patch->num_sources > 1) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2887 | return INVALID_OPERATION; |
| 2888 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2889 | |
| 2890 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2891 | return INVALID_OPERATION; |
| 2892 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2893 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2894 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { |
| 2895 | return INVALID_OPERATION; |
| 2896 | } |
| 2897 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2898 | |
| 2899 | sp<AudioPatch> patchDesc; |
| 2900 | ssize_t index = mAudioPatches.indexOfKey(*handle); |
| 2901 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2902 | ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id, |
| 2903 | patch->sources[0].role, |
| 2904 | patch->sources[0].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2905 | #if LOG_NDEBUG == 0 |
| 2906 | for (size_t i = 0; i < patch->num_sinks; i++) { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2907 | 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] | 2908 | patch->sinks[i].role, |
| 2909 | patch->sinks[i].type); |
| 2910 | } |
| 2911 | #endif |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2912 | |
| 2913 | if (index >= 0) { |
| 2914 | patchDesc = mAudioPatches.valueAt(index); |
| 2915 | ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 2916 | mUidCached, patchDesc->mUid, uid); |
| 2917 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 2918 | return INVALID_OPERATION; |
| 2919 | } |
| 2920 | } else { |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 2921 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2922 | } |
| 2923 | |
| 2924 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2925 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2926 | if (outputDesc == NULL) { |
| 2927 | ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id); |
| 2928 | return BAD_VALUE; |
| 2929 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2930 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", |
| 2931 | outputDesc->mIoHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2932 | if (patchDesc != 0) { |
| 2933 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
| 2934 | ALOGV("createAudioPatch() source id differs for patch current id %d new id %d", |
| 2935 | patchDesc->mPatch.sources[0].id, patch->sources[0].id); |
| 2936 | return BAD_VALUE; |
| 2937 | } |
| 2938 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2939 | DeviceVector devices; |
| 2940 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2941 | // Only support mix to devices connection |
| 2942 | // TODO add support for mix to mix connection |
| 2943 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 2944 | ALOGV("createAudioPatch() source mix but sink is not a device"); |
| 2945 | return INVALID_OPERATION; |
| 2946 | } |
| 2947 | sp<DeviceDescriptor> devDesc = |
| 2948 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 2949 | if (devDesc == 0) { |
| 2950 | ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id); |
| 2951 | return BAD_VALUE; |
| 2952 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2953 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2954 | if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2955 | devDesc->mAddress, |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2956 | patch->sources[0].sample_rate, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2957 | NULL, // updatedSamplingRate |
| 2958 | patch->sources[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2959 | NULL, // updatedFormat |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2960 | patch->sources[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2961 | NULL, // updatedChannelMask |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2962 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2963 | ALOGV("createAudioPatch() profile not supported for device %08x", |
| 2964 | devDesc->type()); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2965 | return INVALID_OPERATION; |
| 2966 | } |
| 2967 | devices.add(devDesc); |
| 2968 | } |
| 2969 | if (devices.size() == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2970 | return INVALID_OPERATION; |
| 2971 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2972 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2973 | // TODO: reconfigure output format and channels here |
| 2974 | ALOGV("createAudioPatch() setting device %08x on output %d", |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2975 | devices.types(), outputDesc->mIoHandle); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2976 | setOutputDevice(outputDesc, devices.types(), true, 0, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2977 | index = mAudioPatches.indexOfKey(*handle); |
| 2978 | if (index >= 0) { |
| 2979 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 2980 | ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided"); |
| 2981 | } |
| 2982 | patchDesc = mAudioPatches.valueAt(index); |
| 2983 | patchDesc->mUid = uid; |
| 2984 | ALOGV("createAudioPatch() success"); |
| 2985 | } else { |
| 2986 | ALOGW("createAudioPatch() setOutputDevice() failed to create a patch"); |
| 2987 | return INVALID_OPERATION; |
| 2988 | } |
| 2989 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2990 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 2991 | // input device to input mix connection |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2992 | // only one sink supported when connecting an input device to a mix |
| 2993 | if (patch->num_sinks > 1) { |
| 2994 | return INVALID_OPERATION; |
| 2995 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2996 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2997 | if (inputDesc == NULL) { |
| 2998 | return BAD_VALUE; |
| 2999 | } |
| 3000 | if (patchDesc != 0) { |
| 3001 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { |
| 3002 | return BAD_VALUE; |
| 3003 | } |
| 3004 | } |
| 3005 | sp<DeviceDescriptor> devDesc = |
| 3006 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
| 3007 | if (devDesc == 0) { |
| 3008 | return BAD_VALUE; |
| 3009 | } |
| 3010 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3011 | if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3012 | devDesc->mAddress, |
| 3013 | patch->sinks[0].sample_rate, |
| 3014 | NULL, /*updatedSampleRate*/ |
| 3015 | patch->sinks[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3016 | NULL, /*updatedFormat*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3017 | patch->sinks[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3018 | NULL, /*updatedChannelMask*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3019 | // FIXME for the parameter type, |
| 3020 | // and the NONE |
| 3021 | (audio_output_flags_t) |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 3022 | AUDIO_INPUT_FLAG_NONE)) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3023 | return INVALID_OPERATION; |
| 3024 | } |
| 3025 | // TODO: reconfigure output format and channels here |
| 3026 | ALOGV("createAudioPatch() setting device %08x on output %d", |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3027 | devDesc->type(), inputDesc->mIoHandle); |
| 3028 | setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3029 | index = mAudioPatches.indexOfKey(*handle); |
| 3030 | if (index >= 0) { |
| 3031 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 3032 | ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided"); |
| 3033 | } |
| 3034 | patchDesc = mAudioPatches.valueAt(index); |
| 3035 | patchDesc->mUid = uid; |
| 3036 | ALOGV("createAudioPatch() success"); |
| 3037 | } else { |
| 3038 | ALOGW("createAudioPatch() setInputDevice() failed to create a patch"); |
| 3039 | return INVALID_OPERATION; |
| 3040 | } |
| 3041 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 3042 | // device to device connection |
| 3043 | if (patchDesc != 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3044 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3045 | return BAD_VALUE; |
| 3046 | } |
| 3047 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3048 | sp<DeviceDescriptor> srcDeviceDesc = |
| 3049 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 3050 | if (srcDeviceDesc == 0) { |
| 3051 | return BAD_VALUE; |
| 3052 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3053 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3054 | //update source and sink with our own data as the data passed in the patch may |
| 3055 | // be incomplete. |
| 3056 | struct audio_patch newPatch = *patch; |
| 3057 | srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3058 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3059 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 3060 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 3061 | ALOGV("createAudioPatch() source device but one sink is not a device"); |
| 3062 | return INVALID_OPERATION; |
| 3063 | } |
| 3064 | |
| 3065 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 3066 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 3067 | if (sinkDeviceDesc == 0) { |
| 3068 | return BAD_VALUE; |
| 3069 | } |
| 3070 | sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]); |
| 3071 | |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3072 | // create a software bridge in PatchPanel if: |
Scott Randolph | f317240 | 2018-01-23 17:06:53 -0800 | [diff] [blame] | 3073 | // - source and sink devices are on different HW modules OR |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3074 | // - audio HAL version is < 3.0 |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 3075 | if (!srcDeviceDesc->hasSameHwModuleAs(sinkDeviceDesc) || |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 3076 | (srcDeviceDesc->mModule->getHalVersionMajor() < 3)) { |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3077 | // support only one sink device for now to simplify output selection logic |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3078 | if (patch->num_sinks > 1) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3079 | return INVALID_OPERATION; |
| 3080 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3081 | SortedVector<audio_io_handle_t> outputs = |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3082 | getOutputsForDevice(sinkDeviceDesc->type(), mOutputs); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3083 | // if the sink device is reachable via an opened output stream, request to go via |
| 3084 | // this output stream by adding a second source to the patch description |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 3085 | audio_io_handle_t output = selectOutput(outputs, |
| 3086 | AUDIO_OUTPUT_FLAG_NONE, |
| 3087 | AUDIO_FORMAT_INVALID); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3088 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 3089 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 3090 | if (outputDesc->isDuplicated()) { |
| 3091 | return INVALID_OPERATION; |
| 3092 | } |
| 3093 | outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3094 | newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3095 | newPatch.num_sources = 2; |
| 3096 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3097 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3098 | } |
| 3099 | // TODO: check from routing capabilities in config file and other conflicting patches |
| 3100 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 3101 | status_t status = installPatch(__func__, index, handle, &newPatch, 0, uid, &patchDesc); |
| 3102 | if (status != NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3103 | ALOGW("createAudioPatch() patch panel could not connect device patch, error %d", |
| 3104 | status); |
| 3105 | return INVALID_OPERATION; |
| 3106 | } |
| 3107 | } else { |
| 3108 | return BAD_VALUE; |
| 3109 | } |
| 3110 | } else { |
| 3111 | return BAD_VALUE; |
| 3112 | } |
| 3113 | return NO_ERROR; |
| 3114 | } |
| 3115 | |
| 3116 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, |
| 3117 | uid_t uid) |
| 3118 | { |
| 3119 | ALOGV("releaseAudioPatch() patch %d", handle); |
| 3120 | |
| 3121 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 3122 | |
| 3123 | if (index < 0) { |
| 3124 | return BAD_VALUE; |
| 3125 | } |
| 3126 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 3127 | ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 3128 | mUidCached, patchDesc->mUid, uid); |
| 3129 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 3130 | return INVALID_OPERATION; |
| 3131 | } |
| 3132 | |
| 3133 | struct audio_patch *patch = &patchDesc->mPatch; |
| 3134 | patchDesc->mUid = mUidCached; |
| 3135 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3136 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3137 | if (outputDesc == NULL) { |
| 3138 | ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id); |
| 3139 | return BAD_VALUE; |
| 3140 | } |
| 3141 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3142 | setOutputDevice(outputDesc, |
| 3143 | getNewOutputDevice(outputDesc, true /*fromCache*/), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3144 | true, |
| 3145 | 0, |
| 3146 | NULL); |
| 3147 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 3148 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3149 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3150 | if (inputDesc == NULL) { |
| 3151 | ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id); |
| 3152 | return BAD_VALUE; |
| 3153 | } |
| 3154 | setInputDevice(inputDesc->mIoHandle, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 3155 | getNewInputDevice(inputDesc), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3156 | true, |
| 3157 | NULL); |
| 3158 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3159 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3160 | ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d", |
| 3161 | status, patchDesc->mAfPatchHandle); |
| 3162 | removeAudioPatch(patchDesc->mHandle); |
| 3163 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 3164 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3165 | } else { |
| 3166 | return BAD_VALUE; |
| 3167 | } |
| 3168 | } else { |
| 3169 | return BAD_VALUE; |
| 3170 | } |
| 3171 | return NO_ERROR; |
| 3172 | } |
| 3173 | |
| 3174 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, |
| 3175 | struct audio_patch *patches, |
| 3176 | unsigned int *generation) |
| 3177 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3178 | if (generation == NULL) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3179 | return BAD_VALUE; |
| 3180 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3181 | *generation = curAudioPortGeneration(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3182 | return mAudioPatches.listAudioPatches(num_patches, patches); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3183 | } |
| 3184 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3185 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3186 | { |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3187 | ALOGV("setAudioPortConfig()"); |
| 3188 | |
| 3189 | if (config == NULL) { |
| 3190 | return BAD_VALUE; |
| 3191 | } |
| 3192 | ALOGV("setAudioPortConfig() on port handle %d", config->id); |
| 3193 | // Only support gain configuration for now |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3194 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { |
| 3195 | return INVALID_OPERATION; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3196 | } |
| 3197 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3198 | sp<AudioPortConfig> audioPortConfig; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3199 | if (config->type == AUDIO_PORT_TYPE_MIX) { |
| 3200 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3201 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3202 | if (outputDesc == NULL) { |
| 3203 | return BAD_VALUE; |
| 3204 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 3205 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 3206 | "setAudioPortConfig() called on duplicated output %d", |
| 3207 | outputDesc->mIoHandle); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3208 | audioPortConfig = outputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3209 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3210 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3211 | if (inputDesc == NULL) { |
| 3212 | return BAD_VALUE; |
| 3213 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3214 | audioPortConfig = inputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3215 | } else { |
| 3216 | return BAD_VALUE; |
| 3217 | } |
| 3218 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { |
| 3219 | sp<DeviceDescriptor> deviceDesc; |
| 3220 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
| 3221 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); |
| 3222 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
| 3223 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); |
| 3224 | } else { |
| 3225 | return BAD_VALUE; |
| 3226 | } |
| 3227 | if (deviceDesc == NULL) { |
| 3228 | return BAD_VALUE; |
| 3229 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3230 | audioPortConfig = deviceDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3231 | } else { |
| 3232 | return BAD_VALUE; |
| 3233 | } |
| 3234 | |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 3235 | struct audio_port_config backupConfig = {}; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3236 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); |
| 3237 | if (status == NO_ERROR) { |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 3238 | struct audio_port_config newConfig = {}; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3239 | audioPortConfig->toAudioPortConfig(&newConfig, config); |
| 3240 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3241 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3242 | if (status != NO_ERROR) { |
| 3243 | audioPortConfig->applyAudioPortConfig(&backupConfig); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3244 | } |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3245 | |
| 3246 | return status; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3247 | } |
| 3248 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3249 | void AudioPolicyManager::releaseResourcesForUid(uid_t uid) |
| 3250 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3251 | clearAudioSources(uid); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3252 | clearAudioPatches(uid); |
| 3253 | clearSessionRoutes(uid); |
| 3254 | } |
| 3255 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3256 | void AudioPolicyManager::clearAudioPatches(uid_t uid) |
| 3257 | { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 3258 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3259 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 3260 | if (patchDesc->mUid == uid) { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 3261 | releaseAudioPatch(mAudioPatches.keyAt(i), uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3262 | } |
| 3263 | } |
| 3264 | } |
| 3265 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3266 | void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy, |
| 3267 | audio_io_handle_t ouptutToSkip) |
| 3268 | { |
| 3269 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 3270 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 3271 | for (size_t j = 0; j < mOutputs.size(); j++) { |
| 3272 | if (mOutputs.keyAt(j) == ouptutToSkip) { |
| 3273 | continue; |
| 3274 | } |
| 3275 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j); |
| 3276 | if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) { |
| 3277 | continue; |
| 3278 | } |
| 3279 | // If the default device for this strategy is on another output mix, |
| 3280 | // invalidate all tracks in this strategy to force re connection. |
| 3281 | // Otherwise select new device on the output mix. |
| 3282 | if (outputs.indexOf(mOutputs.keyAt(j)) < 0) { |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 3283 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3284 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
| 3285 | mpClientInterface->invalidateStream((audio_stream_type_t)stream); |
| 3286 | } |
| 3287 | } |
| 3288 | } else { |
| 3289 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
| 3290 | setOutputDevice(outputDesc, newDevice, false); |
| 3291 | } |
| 3292 | } |
| 3293 | } |
| 3294 | |
| 3295 | void AudioPolicyManager::clearSessionRoutes(uid_t uid) |
| 3296 | { |
| 3297 | // remove output routes associated with this uid |
| 3298 | SortedVector<routing_strategy> affectedStrategies; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3299 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3300 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
| 3301 | TrackClientMap clients = outputDesc->clientsMap(); |
| 3302 | for (const auto& client : clients) { |
| 3303 | if (client.second->hasPreferredDevice() && client.second->uid() == uid) { |
| 3304 | client.second->setPreferredDeviceId(AUDIO_PORT_HANDLE_NONE); |
| 3305 | affectedStrategies.add(getStrategy(client.second->stream())); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3306 | } |
| 3307 | } |
| 3308 | } |
| 3309 | // reroute outputs if necessary |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3310 | for (const auto& strategy : affectedStrategies) { |
| 3311 | checkStrategyRoute(strategy, AUDIO_IO_HANDLE_NONE); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3312 | } |
| 3313 | |
| 3314 | // remove input routes associated with this uid |
| 3315 | SortedVector<audio_source_t> affectedSources; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3316 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3317 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
| 3318 | RecordClientMap clients = inputDesc->clientsMap(); |
| 3319 | for (const auto& client : clients) { |
| 3320 | if (client.second->hasPreferredDevice() && client.second->uid() == uid) { |
| 3321 | client.second->setPreferredDeviceId(AUDIO_PORT_HANDLE_NONE); |
| 3322 | affectedSources.add(client.second->source()); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3323 | } |
| 3324 | } |
| 3325 | } |
| 3326 | // reroute inputs if necessary |
| 3327 | SortedVector<audio_io_handle_t> inputsToClose; |
| 3328 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3329 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3330 | if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3331 | inputsToClose.add(inputDesc->mIoHandle); |
| 3332 | } |
| 3333 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3334 | for (const auto& input : inputsToClose) { |
| 3335 | closeInput(input); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3336 | } |
| 3337 | } |
| 3338 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3339 | void AudioPolicyManager::clearAudioSources(uid_t uid) |
| 3340 | { |
| 3341 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3342 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 3343 | if (sourceDesc->uid() == uid) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3344 | stopAudioSource(mAudioSources.keyAt(i)); |
| 3345 | } |
| 3346 | } |
| 3347 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3348 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3349 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, |
| 3350 | audio_io_handle_t *ioHandle, |
| 3351 | audio_devices_t *device) |
| 3352 | { |
Glenn Kasten | f0c6d7d | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 3353 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); |
| 3354 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT); |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 3355 | *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3356 | |
François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 3357 | return mSoundTriggerSessions.acquireSession(*session, *ioHandle); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3358 | } |
| 3359 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3360 | status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source, |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3361 | const audio_attributes_t *attributes, |
| 3362 | audio_port_handle_t *portId, |
| 3363 | uid_t uid) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3364 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3365 | ALOGV("%s", __FUNCTION__); |
| 3366 | *portId = AUDIO_PORT_HANDLE_NONE; |
| 3367 | |
| 3368 | if (source == NULL || attributes == NULL || portId == NULL) { |
| 3369 | ALOGW("%s invalid argument: source %p attributes %p handle %p", |
| 3370 | __FUNCTION__, source, attributes, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3371 | return BAD_VALUE; |
| 3372 | } |
| 3373 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3374 | if (source->role != AUDIO_PORT_ROLE_SOURCE || |
| 3375 | source->type != AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3376 | ALOGW("%s INVALID_OPERATION source->role %d source->type %d", |
| 3377 | __FUNCTION__, source->role, source->type); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3378 | return INVALID_OPERATION; |
| 3379 | } |
| 3380 | |
| 3381 | sp<DeviceDescriptor> srcDeviceDesc = |
| 3382 | mAvailableInputDevices.getDevice(source->ext.device.type, |
| 3383 | String8(source->ext.device.address)); |
| 3384 | if (srcDeviceDesc == 0) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3385 | ALOGW("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3386 | return BAD_VALUE; |
| 3387 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3388 | |
| 3389 | *portId = AudioPort::getNextUniqueId(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3390 | |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 3391 | struct audio_patch dummyPatch = {}; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3392 | sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3393 | |
| 3394 | sp<SourceClientDescriptor> sourceDesc = |
| 3395 | new SourceClientDescriptor(*portId, uid, *attributes, patchDesc, srcDeviceDesc, |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3396 | streamTypefromAttributesInt(attributes), |
| 3397 | getStrategyForAttr(attributes)); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3398 | |
| 3399 | status_t status = connectAudioSource(sourceDesc); |
| 3400 | if (status == NO_ERROR) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3401 | mAudioSources.add(*portId, sourceDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3402 | } |
| 3403 | return status; |
| 3404 | } |
| 3405 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3406 | status_t AudioPolicyManager::connectAudioSource(const sp<SourceClientDescriptor>& sourceDesc) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3407 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3408 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->portId()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3409 | |
| 3410 | // make sure we only have one patch per source. |
| 3411 | disconnectAudioSource(sourceDesc); |
| 3412 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3413 | audio_attributes_t attributes = sourceDesc->attributes(); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3414 | routing_strategy strategy = getStrategyForAttr(&attributes); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3415 | audio_stream_type_t stream = sourceDesc->stream(); |
| 3416 | sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->srcDevice(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3417 | |
| 3418 | audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true); |
| 3419 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 3420 | mAvailableOutputDevices.getDevice(sinkDevice, String8("")); |
| 3421 | |
| 3422 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3423 | |
| 3424 | if (srcDeviceDesc->getAudioPort()->mModule->getHandle() == |
| 3425 | sinkDeviceDesc->getAudioPort()->mModule->getHandle() && |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 3426 | srcDeviceDesc->getAudioPort()->mModule->getHalVersionMajor() >= 3 && |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3427 | srcDeviceDesc->getAudioPort()->mGains.size() > 0) { |
| 3428 | ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__); |
| 3429 | // create patch between src device and output device |
| 3430 | // create Hwoutput and add to mHwOutputs |
| 3431 | } else { |
| 3432 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs); |
| 3433 | audio_io_handle_t output = |
| 3434 | selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID); |
| 3435 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 3436 | ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice); |
| 3437 | return INVALID_OPERATION; |
| 3438 | } |
| 3439 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 3440 | if (outputDesc->isDuplicated()) { |
| 3441 | ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice); |
| 3442 | return INVALID_OPERATION; |
| 3443 | } |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 3444 | status_t status = outputDesc->start(); |
| 3445 | if (status != NO_ERROR) { |
| 3446 | return status; |
| 3447 | } |
| 3448 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3449 | // create a special patch with no sink and two sources: |
| 3450 | // - the second source indicates to PatchPanel through which output mix this patch should |
| 3451 | // be connected as well as the stream type for volume control |
| 3452 | // - the sink is defined by whatever output device is currently selected for the output |
| 3453 | // though which this patch is routed. |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 3454 | PatchBuilder patchBuilder; |
| 3455 | patchBuilder.addSource(srcDeviceDesc).addSource(outputDesc, { .stream = stream }); |
| 3456 | status = mpClientInterface->createAudioPatch(patchBuilder.patch(), |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3457 | &afPatchHandle, |
| 3458 | 0); |
| 3459 | ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__, |
| 3460 | status, afPatchHandle); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3461 | sourceDesc->patchDesc()->mPatch = *patchBuilder.patch(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3462 | if (status != NO_ERROR) { |
| 3463 | ALOGW("%s patch panel could not connect device patch, error %d", |
| 3464 | __FUNCTION__, status); |
| 3465 | return INVALID_OPERATION; |
| 3466 | } |
| 3467 | uint32_t delayMs = 0; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3468 | status = startSource(outputDesc, sourceDesc, &delayMs); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3469 | |
| 3470 | if (status != NO_ERROR) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3471 | mpClientInterface->releaseAudioPatch(sourceDesc->patchDesc()->mAfPatchHandle, 0); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3472 | return status; |
| 3473 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3474 | sourceDesc->setSwOutput(outputDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3475 | if (delayMs != 0) { |
| 3476 | usleep(delayMs * 1000); |
| 3477 | } |
| 3478 | } |
| 3479 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3480 | sourceDesc->patchDesc()->mAfPatchHandle = afPatchHandle; |
| 3481 | addAudioPatch(sourceDesc->patchDesc()->mHandle, sourceDesc->patchDesc()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3482 | |
| 3483 | return NO_ERROR; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3484 | } |
| 3485 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3486 | status_t AudioPolicyManager::stopAudioSource(audio_port_handle_t portId) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3487 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3488 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueFor(portId); |
| 3489 | ALOGV("%s port ID %d", __FUNCTION__, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3490 | if (sourceDesc == 0) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3491 | ALOGW("%s unknown source for port ID %d", __FUNCTION__, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3492 | return BAD_VALUE; |
| 3493 | } |
| 3494 | status_t status = disconnectAudioSource(sourceDesc); |
| 3495 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3496 | mAudioSources.removeItem(portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3497 | return status; |
| 3498 | } |
| 3499 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3500 | status_t AudioPolicyManager::setMasterMono(bool mono) |
| 3501 | { |
| 3502 | if (mMasterMono == mono) { |
| 3503 | return NO_ERROR; |
| 3504 | } |
| 3505 | mMasterMono = mono; |
| 3506 | // if enabling mono we close all offloaded devices, which will invalidate the |
| 3507 | // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible |
| 3508 | // for recreating the new AudioTrack as non-offloaded PCM. |
| 3509 | // |
| 3510 | // If disabling mono, we leave all tracks as is: we don't know which clients |
| 3511 | // and tracks are able to be recreated as offloaded. The next "song" should |
| 3512 | // play back offloaded. |
| 3513 | if (mMasterMono) { |
| 3514 | Vector<audio_io_handle_t> offloaded; |
| 3515 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3516 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 3517 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
| 3518 | offloaded.push(desc->mIoHandle); |
| 3519 | } |
| 3520 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3521 | for (const auto& handle : offloaded) { |
| 3522 | closeOutput(handle); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3523 | } |
| 3524 | } |
| 3525 | // update master mono for all remaining outputs |
| 3526 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3527 | updateMono(mOutputs.keyAt(i)); |
| 3528 | } |
| 3529 | return NO_ERROR; |
| 3530 | } |
| 3531 | |
| 3532 | status_t AudioPolicyManager::getMasterMono(bool *mono) |
| 3533 | { |
| 3534 | *mono = mMasterMono; |
| 3535 | return NO_ERROR; |
| 3536 | } |
| 3537 | |
Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 3538 | float AudioPolicyManager::getStreamVolumeDB( |
| 3539 | audio_stream_type_t stream, int index, audio_devices_t device) |
| 3540 | { |
| 3541 | return computeVolume(stream, index, device); |
| 3542 | } |
| 3543 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3544 | status_t AudioPolicyManager::getSupportedFormats(audio_io_handle_t ioHandle, |
| 3545 | FormatVector& formats) { |
| 3546 | if (ioHandle == AUDIO_IO_HANDLE_NONE) { |
| 3547 | return BAD_VALUE; |
| 3548 | } |
| 3549 | String8 reply; |
| 3550 | reply = mpClientInterface->getParameters( |
| 3551 | ioHandle, String8(AudioParameter::keyStreamSupportedFormats)); |
| 3552 | ALOGV("%s: supported formats %s", __FUNCTION__, reply.string()); |
| 3553 | AudioParameter repliedParameters(reply); |
| 3554 | if (repliedParameters.get( |
| 3555 | String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) { |
| 3556 | ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__); |
| 3557 | return BAD_VALUE; |
| 3558 | } |
| 3559 | for (auto format : formatsFromString(reply.string())) { |
| 3560 | // Only AUDIO_FORMAT_AAC_LC will be used in Settings UI for all AAC formats. |
| 3561 | for (size_t i = 0; i < ARRAY_SIZE(AAC_FORMATS); i++) { |
| 3562 | if (format == AAC_FORMATS[i]) { |
| 3563 | format = AUDIO_FORMAT_AAC_LC; |
| 3564 | break; |
| 3565 | } |
| 3566 | } |
| 3567 | bool exist = false; |
| 3568 | for (size_t i = 0; i < formats.size(); i++) { |
| 3569 | if (format == formats[i]) { |
| 3570 | exist = true; |
| 3571 | break; |
| 3572 | } |
| 3573 | } |
| 3574 | bool isSurroundFormat = false; |
| 3575 | for (size_t i = 0; i < ARRAY_SIZE(SURROUND_FORMATS); i++) { |
| 3576 | if (SURROUND_FORMATS[i] == format) { |
| 3577 | isSurroundFormat = true; |
| 3578 | break; |
| 3579 | } |
| 3580 | } |
| 3581 | if (!exist && isSurroundFormat) { |
| 3582 | formats.add(format); |
| 3583 | } |
| 3584 | } |
| 3585 | return NO_ERROR; |
| 3586 | } |
| 3587 | |
| 3588 | status_t AudioPolicyManager::getSurroundFormats(unsigned int *numSurroundFormats, |
| 3589 | audio_format_t *surroundFormats, |
| 3590 | bool *surroundFormatsEnabled, |
| 3591 | bool reported) |
| 3592 | { |
| 3593 | if (numSurroundFormats == NULL || (*numSurroundFormats != 0 && |
| 3594 | (surroundFormats == NULL || surroundFormatsEnabled == NULL))) { |
| 3595 | return BAD_VALUE; |
| 3596 | } |
| 3597 | ALOGV("getSurroundFormats() numSurroundFormats %d surroundFormats %p surroundFormatsEnabled %p", |
| 3598 | *numSurroundFormats, surroundFormats, surroundFormatsEnabled); |
| 3599 | |
| 3600 | // Only return value if there is HDMI output. |
| 3601 | if ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_HDMI) == 0) { |
| 3602 | return INVALID_OPERATION; |
| 3603 | } |
| 3604 | |
| 3605 | size_t formatsWritten = 0; |
| 3606 | size_t formatsMax = *numSurroundFormats; |
| 3607 | *numSurroundFormats = 0; |
| 3608 | FormatVector formats; |
| 3609 | if (reported) { |
| 3610 | // Only get surround formats which are reported by device. |
| 3611 | // First list already open outputs that can be routed to this device |
| 3612 | audio_devices_t device = AUDIO_DEVICE_OUT_HDMI; |
| 3613 | SortedVector<audio_io_handle_t> outputs; |
| 3614 | bool reportedFormatFound = false; |
| 3615 | status_t status; |
| 3616 | sp<SwAudioOutputDescriptor> desc; |
| 3617 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3618 | desc = mOutputs.valueAt(i); |
| 3619 | if (!desc->isDuplicated() && (desc->supportedDevices() & device)) { |
| 3620 | outputs.add(mOutputs.keyAt(i)); |
| 3621 | } |
| 3622 | } |
| 3623 | // Open an output to query dynamic parameters. |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 3624 | DeviceVector hdmiOutputDevices = mAvailableOutputDevices.getDevicesFromTypeMask( |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3625 | AUDIO_DEVICE_OUT_HDMI); |
| 3626 | for (size_t i = 0; i < hdmiOutputDevices.size(); i++) { |
| 3627 | String8 address = hdmiOutputDevices[i]->mAddress; |
| 3628 | for (const auto& hwModule : mHwModules) { |
| 3629 | for (size_t i = 0; i < hwModule->getOutputProfiles().size(); i++) { |
| 3630 | sp<IOProfile> profile = hwModule->getOutputProfiles()[i]; |
| 3631 | if (profile->supportDevice(AUDIO_DEVICE_OUT_HDMI) && |
| 3632 | profile->supportDeviceAddress(address)) { |
| 3633 | size_t j; |
| 3634 | for (j = 0; j < outputs.size(); j++) { |
| 3635 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
| 3636 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
| 3637 | break; |
| 3638 | } |
| 3639 | } |
| 3640 | if (j != outputs.size()) { |
| 3641 | status = getSupportedFormats(outputs.itemAt(j), formats); |
| 3642 | reportedFormatFound |= (status == NO_ERROR); |
| 3643 | continue; |
| 3644 | } |
| 3645 | |
| 3646 | if (!profile->canOpenNewIo()) { |
| 3647 | ALOGW("Max Output number %u already opened for this profile %s", |
| 3648 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 3649 | continue; |
| 3650 | } |
| 3651 | |
| 3652 | ALOGV("opening output for device %08x with params %s profile %p name %s", |
| 3653 | device, address.string(), profile.get(), profile->getName().string()); |
| 3654 | desc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
| 3655 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 3656 | status_t status = desc->open(nullptr, device, address, |
| 3657 | AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, |
| 3658 | &output); |
| 3659 | |
| 3660 | if (status == NO_ERROR) { |
| 3661 | status = getSupportedFormats(output, formats); |
| 3662 | reportedFormatFound |= (status == NO_ERROR); |
| 3663 | desc->close(); |
| 3664 | output = AUDIO_IO_HANDLE_NONE; |
| 3665 | } |
| 3666 | } |
| 3667 | } |
| 3668 | } |
| 3669 | } |
| 3670 | |
| 3671 | if (!reportedFormatFound) { |
| 3672 | return UNKNOWN_ERROR; |
| 3673 | } |
| 3674 | } else { |
| 3675 | for (size_t i = 0; i < ARRAY_SIZE(SURROUND_FORMATS); i++) { |
| 3676 | formats.add(SURROUND_FORMATS[i]); |
| 3677 | } |
| 3678 | } |
| 3679 | for (size_t i = 0; i < formats.size(); i++) { |
| 3680 | if (formatsWritten < formatsMax) { |
| 3681 | surroundFormats[formatsWritten] = formats[i]; |
| 3682 | bool formatEnabled = false; |
| 3683 | if (formats[i] == AUDIO_FORMAT_AAC_LC) { |
| 3684 | for (size_t j = 0; j < ARRAY_SIZE(AAC_FORMATS); j++) { |
| 3685 | formatEnabled = |
| 3686 | mSurroundFormats.find(AAC_FORMATS[i]) != mSurroundFormats.end(); |
| 3687 | break; |
| 3688 | } |
| 3689 | } else { |
| 3690 | formatEnabled = mSurroundFormats.find(formats[i]) != mSurroundFormats.end(); |
| 3691 | } |
| 3692 | surroundFormatsEnabled[formatsWritten++] = formatEnabled; |
| 3693 | } |
| 3694 | (*numSurroundFormats)++; |
| 3695 | } |
| 3696 | return NO_ERROR; |
| 3697 | } |
| 3698 | |
| 3699 | status_t AudioPolicyManager::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) |
| 3700 | { |
| 3701 | // Check if audio format is a surround formats. |
| 3702 | bool isSurroundFormat = false; |
| 3703 | for (size_t i = 0; i < ARRAY_SIZE(SURROUND_FORMATS); i++) { |
| 3704 | if (audioFormat == SURROUND_FORMATS[i]) { |
| 3705 | isSurroundFormat = true; |
| 3706 | break; |
| 3707 | } |
| 3708 | } |
| 3709 | if (!isSurroundFormat) { |
| 3710 | return BAD_VALUE; |
| 3711 | } |
| 3712 | |
| 3713 | // Should only be called when MANUAL. |
| 3714 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 3715 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
| 3716 | if (forceUse != AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
| 3717 | return INVALID_OPERATION; |
| 3718 | } |
| 3719 | |
| 3720 | if ((mSurroundFormats.find(audioFormat) != mSurroundFormats.end() && enabled) |
| 3721 | || (mSurroundFormats.find(audioFormat) == mSurroundFormats.end() && !enabled)) { |
| 3722 | return NO_ERROR; |
| 3723 | } |
| 3724 | |
| 3725 | // The operation is valid only when there is HDMI output available. |
| 3726 | if ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_HDMI) == 0) { |
| 3727 | return INVALID_OPERATION; |
| 3728 | } |
| 3729 | |
| 3730 | if (enabled) { |
| 3731 | if (audioFormat == AUDIO_FORMAT_AAC_LC) { |
| 3732 | for (size_t i = 0; i < ARRAY_SIZE(AAC_FORMATS); i++) { |
| 3733 | mSurroundFormats.insert(AAC_FORMATS[i]); |
| 3734 | } |
| 3735 | } else { |
| 3736 | mSurroundFormats.insert(audioFormat); |
| 3737 | } |
| 3738 | } else { |
| 3739 | if (audioFormat == AUDIO_FORMAT_AAC_LC) { |
| 3740 | for (size_t i = 0; i < ARRAY_SIZE(AAC_FORMATS); i++) { |
| 3741 | mSurroundFormats.erase(AAC_FORMATS[i]); |
| 3742 | } |
| 3743 | } else { |
| 3744 | mSurroundFormats.erase(audioFormat); |
| 3745 | } |
| 3746 | } |
| 3747 | |
| 3748 | sp<SwAudioOutputDescriptor> outputDesc; |
| 3749 | bool profileUpdated = false; |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 3750 | DeviceVector hdmiOutputDevices = mAvailableOutputDevices.getDevicesFromTypeMask( |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3751 | AUDIO_DEVICE_OUT_HDMI); |
| 3752 | for (size_t i = 0; i < hdmiOutputDevices.size(); i++) { |
| 3753 | // Simulate reconnection to update enabled surround sound formats. |
| 3754 | String8 address = hdmiOutputDevices[i]->mAddress; |
| 3755 | String8 name = hdmiOutputDevices[i]->getName(); |
| 3756 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 3757 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 3758 | address.c_str(), |
| 3759 | name.c_str()); |
| 3760 | if (status != NO_ERROR) { |
| 3761 | continue; |
| 3762 | } |
| 3763 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 3764 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 3765 | address.c_str(), |
| 3766 | name.c_str()); |
| 3767 | profileUpdated |= (status == NO_ERROR); |
| 3768 | } |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 3769 | DeviceVector hdmiInputDevices = mAvailableInputDevices.getDevicesFromTypeMask( |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3770 | AUDIO_DEVICE_IN_HDMI); |
| 3771 | for (size_t i = 0; i < hdmiInputDevices.size(); i++) { |
| 3772 | // Simulate reconnection to update enabled surround sound formats. |
| 3773 | String8 address = hdmiInputDevices[i]->mAddress; |
| 3774 | String8 name = hdmiInputDevices[i]->getName(); |
| 3775 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 3776 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 3777 | address.c_str(), |
| 3778 | name.c_str()); |
| 3779 | if (status != NO_ERROR) { |
| 3780 | continue; |
| 3781 | } |
| 3782 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 3783 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 3784 | address.c_str(), |
| 3785 | name.c_str()); |
| 3786 | profileUpdated |= (status == NO_ERROR); |
| 3787 | } |
| 3788 | |
| 3789 | // Undo the surround formats change due to no audio profiles updated. |
| 3790 | if (!profileUpdated) { |
| 3791 | if (enabled) { |
| 3792 | if (audioFormat == AUDIO_FORMAT_AAC_LC) { |
| 3793 | for (size_t i = 0; i < ARRAY_SIZE(AAC_FORMATS); i++) { |
| 3794 | mSurroundFormats.erase(AAC_FORMATS[i]); |
| 3795 | } |
| 3796 | } else { |
| 3797 | mSurroundFormats.erase(audioFormat); |
| 3798 | } |
| 3799 | } else { |
| 3800 | if (audioFormat == AUDIO_FORMAT_AAC_LC) { |
| 3801 | for (size_t i = 0; i < ARRAY_SIZE(AAC_FORMATS); i++) { |
| 3802 | mSurroundFormats.insert(AAC_FORMATS[i]); |
| 3803 | } |
| 3804 | } else { |
| 3805 | mSurroundFormats.insert(audioFormat); |
| 3806 | } |
| 3807 | } |
| 3808 | } |
| 3809 | |
| 3810 | return profileUpdated ? NO_ERROR : INVALID_OPERATION; |
| 3811 | } |
| 3812 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 3813 | void AudioPolicyManager::setRecordSilenced(uid_t uid, bool silenced) |
| 3814 | { |
| 3815 | ALOGV("AudioPolicyManager:setRecordSilenced(uid:%d, silenced:%d)", uid, silenced); |
| 3816 | |
| 3817 | Vector<sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs(); |
| 3818 | for (size_t i = 0; i < activeInputs.size(); i++) { |
| 3819 | sp<AudioInputDescriptor> activeDesc = activeInputs[i]; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3820 | RecordClientVector clients = activeDesc->clientsList(true /*activeOnly*/); |
| 3821 | for (const auto& client : clients) { |
| 3822 | if (uid == client->uid()) { |
| 3823 | client->setSilenced(silenced); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 3824 | } |
| 3825 | } |
| 3826 | } |
| 3827 | } |
| 3828 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3829 | status_t AudioPolicyManager::disconnectAudioSource(const sp<SourceClientDescriptor>& sourceDesc) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3830 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3831 | ALOGV("%s port Id %d", __FUNCTION__, sourceDesc->portId()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3832 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3833 | sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->patchDesc()->mHandle); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3834 | if (patchDesc == 0) { |
| 3835 | ALOGW("%s source has no patch with handle %d", __FUNCTION__, |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3836 | sourceDesc->patchDesc()->mHandle); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3837 | return BAD_VALUE; |
| 3838 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3839 | removeAudioPatch(sourceDesc->patchDesc()->mHandle); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3840 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3841 | sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->swOutput().promote(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3842 | if (swOutputDesc != 0) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3843 | status_t status = stopSource(swOutputDesc, sourceDesc); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 3844 | if (status == NO_ERROR) { |
| 3845 | swOutputDesc->stop(); |
| 3846 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3847 | mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3848 | } else { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3849 | sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->hwOutput().promote(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3850 | if (hwOutputDesc != 0) { |
| 3851 | // release patch between src device and output device |
| 3852 | // close Hwoutput and remove from mHwOutputs |
| 3853 | } else { |
| 3854 | ALOGW("%s source has neither SW nor HW output", __FUNCTION__); |
| 3855 | } |
| 3856 | } |
| 3857 | return NO_ERROR; |
| 3858 | } |
| 3859 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3860 | sp<SourceClientDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput( |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3861 | audio_io_handle_t output, routing_strategy strategy) |
| 3862 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3863 | sp<SourceClientDescriptor> source; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3864 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3865 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 3866 | audio_attributes_t attributes = sourceDesc->attributes(); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3867 | routing_strategy sourceStrategy = getStrategyForAttr(&attributes); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3868 | sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->swOutput().promote(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3869 | if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) { |
| 3870 | source = sourceDesc; |
| 3871 | break; |
| 3872 | } |
| 3873 | } |
| 3874 | return source; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3875 | } |
| 3876 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3877 | // ---------------------------------------------------------------------------- |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3878 | // AudioPolicyManager |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3879 | // ---------------------------------------------------------------------------- |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3880 | uint32_t AudioPolicyManager::nextAudioPortGeneration() |
| 3881 | { |
Mikhail Naganov | 2773dd7 | 2017-12-08 10:12:11 -0800 | [diff] [blame] | 3882 | return mAudioPortGeneration++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3883 | } |
| 3884 | |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3885 | #ifdef USE_XML_AUDIO_POLICY_CONF |
| 3886 | // Treblized audio policy xml config will be located in /odm/etc or /vendor/etc. |
| 3887 | static const char *kConfigLocationList[] = |
| 3888 | {"/odm/etc", "/vendor/etc", "/system/etc"}; |
| 3889 | static const int kConfigLocationListSize = |
| 3890 | (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0])); |
| 3891 | |
| 3892 | static status_t deserializeAudioPolicyXmlConfig(AudioPolicyConfig &config) { |
| 3893 | char audioPolicyXmlConfigFile[AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH]; |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3894 | std::vector<const char*> fileNames; |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3895 | status_t ret; |
| 3896 | |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3897 | if (property_get_bool("ro.bluetooth.a2dp_offload.supported", false) && |
| 3898 | property_get_bool("persist.bluetooth.a2dp_offload.disabled", false)) { |
| 3899 | // A2DP offload supported but disabled: try to use special XML file |
| 3900 | fileNames.push_back(AUDIO_POLICY_A2DP_OFFLOAD_DISABLED_XML_CONFIG_FILE_NAME); |
| 3901 | } |
| 3902 | fileNames.push_back(AUDIO_POLICY_XML_CONFIG_FILE_NAME); |
| 3903 | |
| 3904 | for (const char* fileName : fileNames) { |
| 3905 | for (int i = 0; i < kConfigLocationListSize; i++) { |
| 3906 | PolicySerializer serializer; |
| 3907 | snprintf(audioPolicyXmlConfigFile, sizeof(audioPolicyXmlConfigFile), |
| 3908 | "%s/%s", kConfigLocationList[i], fileName); |
| 3909 | ret = serializer.deserialize(audioPolicyXmlConfigFile, config); |
| 3910 | if (ret == NO_ERROR) { |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 3911 | config.setSource(audioPolicyXmlConfigFile); |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3912 | return ret; |
| 3913 | } |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3914 | } |
| 3915 | } |
| 3916 | return ret; |
| 3917 | } |
| 3918 | #endif |
| 3919 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3920 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface, |
| 3921 | bool /*forTesting*/) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3922 | : |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 3923 | mUidCached(AID_AUDIOSERVER), // no need to call getuid(), there's only one of us running. |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3924 | mpClientInterface(clientInterface), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3925 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3926 | mA2dpSuspended(false), |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3927 | #ifdef USE_XML_AUDIO_POLICY_CONF |
| 3928 | mVolumeCurves(new VolumeCurvesCollection()), |
| 3929 | mConfig(mHwModulesAll, mAvailableOutputDevices, mAvailableInputDevices, |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 3930 | mDefaultOutputDevice, static_cast<VolumeCurvesCollection*>(mVolumeCurves.get())), |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3931 | #else |
| 3932 | mVolumeCurves(new StreamDescriptorCollection()), |
| 3933 | mConfig(mHwModulesAll, mAvailableOutputDevices, mAvailableInputDevices, |
| 3934 | mDefaultOutputDevice), |
| 3935 | #endif |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 3936 | mAudioPortGeneration(1), |
| 3937 | mBeaconMuteRefCount(0), |
| 3938 | mBeaconPlayingRefCount(0), |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3939 | mBeaconMuted(false), |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3940 | mTtsOutputAvailable(false), |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3941 | mMasterMono(false), |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 3942 | mMusicEffectOutput(AUDIO_IO_HANDLE_NONE), |
| 3943 | mHasComputedSoundTriggerSupportsConcurrentCapture(false) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3944 | { |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3945 | } |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3946 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3947 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) |
| 3948 | : AudioPolicyManager(clientInterface, false /*forTesting*/) |
| 3949 | { |
| 3950 | loadConfig(); |
| 3951 | initialize(); |
| 3952 | } |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3953 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3954 | void AudioPolicyManager::loadConfig() { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3955 | #ifdef USE_XML_AUDIO_POLICY_CONF |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3956 | if (deserializeAudioPolicyXmlConfig(getConfig()) != NO_ERROR) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3957 | #else |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3958 | if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, getConfig()) != NO_ERROR) |
| 3959 | && (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, getConfig()) != NO_ERROR)) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3960 | #endif |
| 3961 | ALOGE("could not load audio policy configuration file, setting defaults"); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3962 | getConfig().setDefault(); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3963 | } |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3964 | } |
| 3965 | |
| 3966 | status_t AudioPolicyManager::initialize() { |
| 3967 | mVolumeCurves->initializeVolumeCurves(getConfig().isSpeakerDrcEnabled()); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3968 | |
| 3969 | // 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] | 3970 | audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance(); |
| 3971 | if (!engineInstance) { |
| 3972 | ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3973 | return NO_INIT; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3974 | } |
| 3975 | // Retrieve the Policy Manager Interface |
| 3976 | mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>(); |
| 3977 | if (mEngine == NULL) { |
| 3978 | ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3979 | return NO_INIT; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3980 | } |
| 3981 | mEngine->setObserver(this); |
| 3982 | status_t status = mEngine->initCheck(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3983 | if (status != NO_ERROR) { |
| 3984 | LOG_FATAL("Policy engine not initialized(err=%d)", status); |
| 3985 | return status; |
| 3986 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3987 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3988 | // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3989 | // open all output streams needed to access attached devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3990 | audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types(); |
| 3991 | audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3992 | for (const auto& hwModule : mHwModulesAll) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 3993 | hwModule->setHandle(mpClientInterface->loadHwModule(hwModule->getName())); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3994 | if (hwModule->getHandle() == AUDIO_MODULE_HANDLE_NONE) { |
| 3995 | ALOGW("could not open HW module %s", hwModule->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3996 | continue; |
| 3997 | } |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3998 | mHwModules.push_back(hwModule); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3999 | // open all output streams needed to access attached devices |
| 4000 | // except for direct output streams that are only opened when they are actually |
| 4001 | // required by an app. |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4002 | // This also validates mAvailableOutputDevices list |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4003 | for (const auto& outProfile : hwModule->getOutputProfiles()) { |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4004 | if (!outProfile->canOpenNewIo()) { |
| 4005 | ALOGE("Invalid Output profile max open count %u for profile %s", |
| 4006 | outProfile->maxOpenCount, outProfile->getTagName().c_str()); |
| 4007 | continue; |
| 4008 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4009 | if (!outProfile->hasSupportedDevices()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4010 | ALOGW("Output profile contains no device on module %s", hwModule->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4011 | continue; |
| 4012 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4013 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 4014 | mTtsOutputAvailable = true; |
| 4015 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4016 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4017 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4018 | continue; |
| 4019 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4020 | audio_devices_t profileType = outProfile->getSupportedDevicesType(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4021 | if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) { |
| 4022 | profileType = mDefaultOutputDevice->type(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 4023 | } else { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4024 | // chose first device present in profile's SupportedDevices also part of |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4025 | // outputDeviceTypes |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4026 | profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4027 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4028 | if ((profileType & outputDeviceTypes) == 0) { |
| 4029 | continue; |
| 4030 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4031 | sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile, |
| 4032 | mpClientInterface); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 4033 | const DeviceVector &supportedDevices = outProfile->getSupportedDevices(); |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 4034 | const DeviceVector &devicesForType = supportedDevices.getDevicesFromTypeMask( |
| 4035 | profileType); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 4036 | String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress |
| 4037 | : String8(""); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4038 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4039 | status_t status = outputDesc->open(nullptr, profileType, address, |
| 4040 | AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, &output); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4041 | |
| 4042 | if (status != NO_ERROR) { |
| 4043 | ALOGW("Cannot open output stream for device %08x on hw module %s", |
| 4044 | outputDesc->mDevice, |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4045 | hwModule->getName()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4046 | } else { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4047 | for (const auto& dev : supportedDevices) { |
| 4048 | ssize_t index = mAvailableOutputDevices.indexOf(dev); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4049 | // 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] | 4050 | if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4051 | mAvailableOutputDevices[index]->attach(hwModule); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4052 | } |
| 4053 | } |
| 4054 | if (mPrimaryOutput == 0 && |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4055 | outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4056 | mPrimaryOutput = outputDesc; |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4057 | } |
| 4058 | addOutput(output, outputDesc); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4059 | setOutputDevice(outputDesc, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4060 | profileType, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 4061 | true, |
| 4062 | 0, |
| 4063 | NULL, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4064 | address); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4065 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4066 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4067 | // open input streams needed to access attached devices to validate |
| 4068 | // mAvailableInputDevices list |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4069 | for (const auto& inProfile : hwModule->getInputProfiles()) { |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4070 | if (!inProfile->canOpenNewIo()) { |
| 4071 | ALOGE("Invalid Input profile max open count %u for profile %s", |
| 4072 | inProfile->maxOpenCount, inProfile->getTagName().c_str()); |
| 4073 | continue; |
| 4074 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4075 | if (!inProfile->hasSupportedDevices()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4076 | ALOGW("Input profile contains no device on module %s", hwModule->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4077 | continue; |
| 4078 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4079 | // chose first device present in profile's SupportedDevices also part of |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4080 | // inputDeviceTypes |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4081 | audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes); |
| 4082 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4083 | if ((profileType & inputDeviceTypes) == 0) { |
| 4084 | continue; |
| 4085 | } |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 4086 | sp<AudioInputDescriptor> inputDesc = |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4087 | new AudioInputDescriptor(inProfile, mpClientInterface); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4088 | |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 4089 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromTypeMask(profileType); |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 4090 | // the inputs vector must be of size >= 1, but we don't want to crash here |
| 4091 | String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress |
| 4092 | : String8(""); |
| 4093 | ALOGV(" for input device 0x%x using address %s", profileType, address.string()); |
| 4094 | ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!"); |
| 4095 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4096 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4097 | status_t status = inputDesc->open(nullptr, |
| 4098 | profileType, |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 4099 | address, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4100 | AUDIO_SOURCE_MIC, |
| 4101 | AUDIO_INPUT_FLAG_NONE, |
| 4102 | &input); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4103 | |
| 4104 | if (status == NO_ERROR) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4105 | for (const auto& dev : inProfile->getSupportedDevices()) { |
| 4106 | ssize_t index = mAvailableInputDevices.indexOf(dev); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4107 | // 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] | 4108 | if (index >= 0) { |
| 4109 | sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index]; |
| 4110 | if (!devDesc->isAttached()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4111 | devDesc->attach(hwModule); |
Eric Laurent | 83efe1c | 2017-07-09 16:51:08 -0700 | [diff] [blame] | 4112 | devDesc->importAudioPort(inProfile, true); |
Eric Laurent | 45aabc3 | 2015-08-06 09:11:13 -0700 | [diff] [blame] | 4113 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4114 | } |
| 4115 | } |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4116 | inputDesc->close(); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4117 | } else { |
| 4118 | ALOGW("Cannot open input stream for device %08x on hw module %s", |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4119 | profileType, |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4120 | hwModule->getName()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4121 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4122 | } |
| 4123 | } |
| 4124 | // make sure all attached devices have been allocated a unique ID |
| 4125 | for (size_t i = 0; i < mAvailableOutputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 4126 | if (!mAvailableOutputDevices[i]->isAttached()) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4127 | ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4128 | mAvailableOutputDevices.remove(mAvailableOutputDevices[i]); |
| 4129 | continue; |
| 4130 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4131 | // The device is now validated and can be appended to the available devices of the engine |
| 4132 | mEngine->setDeviceConnectionState(mAvailableOutputDevices[i], |
| 4133 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4134 | i++; |
| 4135 | } |
| 4136 | for (size_t i = 0; i < mAvailableInputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 4137 | if (!mAvailableInputDevices[i]->isAttached()) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4138 | ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4139 | mAvailableInputDevices.remove(mAvailableInputDevices[i]); |
| 4140 | continue; |
| 4141 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4142 | // The device is now validated and can be appended to the available devices of the engine |
| 4143 | mEngine->setDeviceConnectionState(mAvailableInputDevices[i], |
| 4144 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4145 | i++; |
| 4146 | } |
| 4147 | // make sure default device is reachable |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4148 | if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4149 | ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type()); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4150 | status = NO_INIT; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4151 | } |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 4152 | // If microphones address is empty, set it according to device type |
| 4153 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { |
| 4154 | if (mAvailableInputDevices[i]->mAddress.isEmpty()) { |
| 4155 | if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BUILTIN_MIC) { |
| 4156 | mAvailableInputDevices[i]->mAddress = String8(AUDIO_BOTTOM_MICROPHONE_ADDRESS); |
| 4157 | } else if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BACK_MIC) { |
| 4158 | mAvailableInputDevices[i]->mAddress = String8(AUDIO_BACK_MICROPHONE_ADDRESS); |
| 4159 | } |
| 4160 | } |
| 4161 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4162 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4163 | if (mPrimaryOutput == 0) { |
| 4164 | ALOGE("Failed to open primary output"); |
| 4165 | status = NO_INIT; |
| 4166 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4167 | |
| 4168 | updateDevicesAndOutputs(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4169 | return status; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4170 | } |
| 4171 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4172 | AudioPolicyManager::~AudioPolicyManager() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4173 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4174 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4175 | mOutputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4176 | } |
| 4177 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4178 | mInputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4179 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4180 | mAvailableOutputDevices.clear(); |
| 4181 | mAvailableInputDevices.clear(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4182 | mOutputs.clear(); |
| 4183 | mInputs.clear(); |
| 4184 | mHwModules.clear(); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4185 | mHwModulesAll.clear(); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4186 | mSurroundFormats.clear(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4187 | } |
| 4188 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4189 | status_t AudioPolicyManager::initCheck() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4190 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 4191 | return hasPrimaryOutput() ? NO_ERROR : NO_INIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4192 | } |
| 4193 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4194 | // --- |
| 4195 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 4196 | void AudioPolicyManager::addOutput(audio_io_handle_t output, |
| 4197 | const sp<SwAudioOutputDescriptor>& outputDesc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4198 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4199 | mOutputs.add(output, outputDesc); |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 4200 | applyStreamVolumes(outputDesc, AUDIO_DEVICE_NONE, 0 /* delayMs */, true /* force */); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4201 | updateMono(output); // update mono status when adding to output list |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4202 | selectOutputForMusicEffects(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4203 | nextAudioPortGeneration(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4204 | } |
| 4205 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4206 | void AudioPolicyManager::removeOutput(audio_io_handle_t output) |
| 4207 | { |
| 4208 | mOutputs.removeItem(output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4209 | selectOutputForMusicEffects(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4210 | } |
| 4211 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 4212 | void AudioPolicyManager::addInput(audio_io_handle_t input, |
| 4213 | const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4214 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4215 | mInputs.add(input, inputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4216 | nextAudioPortGeneration(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4217 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4218 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4219 | void AudioPolicyManager::findIoHandlesByAddress(const sp<SwAudioOutputDescriptor>& desc /*in*/, |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4220 | const audio_devices_t device /*in*/, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4221 | const String8& address /*in*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4222 | SortedVector<audio_io_handle_t>& outputs /*out*/) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4223 | sp<DeviceDescriptor> devDesc = |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4224 | desc->mProfile->getSupportedDeviceByAddress(device, address); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4225 | if (devDesc != 0) { |
| 4226 | ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s", |
| 4227 | desc->mIoHandle, address.string()); |
| 4228 | outputs.add(desc->mIoHandle); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4229 | } |
| 4230 | } |
| 4231 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4232 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4233 | audio_policy_dev_state_t state, |
| 4234 | SortedVector<audio_io_handle_t>& outputs, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4235 | const String8& address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4236 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4237 | audio_devices_t device = devDesc->type(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4238 | sp<SwAudioOutputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4239 | |
| 4240 | if (audio_device_is_digital(device)) { |
| 4241 | // erase all current sample rates, formats and channel masks |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 4242 | devDesc->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4243 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4244 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4245 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4246 | // first list already open outputs that can be routed to this device |
| 4247 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4248 | desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4249 | if (!desc->isDuplicated() && (desc->supportedDevices() & device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4250 | if (!device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4251 | ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i)); |
| 4252 | outputs.add(mOutputs.keyAt(i)); |
| 4253 | } else { |
| 4254 | ALOGV(" checking address match due to device 0x%x", device); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4255 | findIoHandlesByAddress(desc, device, address, outputs); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4256 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4257 | } |
| 4258 | } |
| 4259 | // then look for output profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4260 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4261 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4262 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 4263 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4264 | if (profile->supportDevice(device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4265 | if (!device_distinguishes_on_address(device) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4266 | profile->supportDeviceAddress(address)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4267 | profiles.add(profile); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4268 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %s", |
| 4269 | j, hwModule->getName()); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4270 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4271 | } |
| 4272 | } |
| 4273 | } |
| 4274 | |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 4275 | ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size()); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4276 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4277 | if (profiles.isEmpty() && outputs.isEmpty()) { |
| 4278 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 4279 | return BAD_VALUE; |
| 4280 | } |
| 4281 | |
| 4282 | // open outputs for matching profiles if needed. Direct outputs are also opened to |
| 4283 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 4284 | 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] | 4285 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4286 | |
| 4287 | // nothing to do if one output is already opened for this profile |
| 4288 | size_t j; |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4289 | for (j = 0; j < outputs.size(); j++) { |
| 4290 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4291 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 4292 | // matching profile: save the sample rates, format and channel masks supported |
| 4293 | // by the profile in our device descriptor |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4294 | if (audio_device_is_digital(device)) { |
| 4295 | devDesc->importAudioPort(profile); |
| 4296 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4297 | break; |
| 4298 | } |
| 4299 | } |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4300 | if (j != outputs.size()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4301 | continue; |
| 4302 | } |
| 4303 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4304 | if (!profile->canOpenNewIo()) { |
| 4305 | ALOGW("Max Output number %u already opened for this profile %s", |
| 4306 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 4307 | continue; |
| 4308 | } |
| 4309 | |
Eric Laurent | 83efe1c | 2017-07-09 16:51:08 -0700 | [diff] [blame] | 4310 | ALOGV("opening output for device %08x with params %s profile %p name %s", |
| 4311 | device, address.string(), profile.get(), profile->getName().string()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4312 | desc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4313 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4314 | status_t status = desc->open(nullptr, device, address, |
| 4315 | AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, &output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4316 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4317 | if (status == NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4318 | // Here is where the out_set_parameters() for card & device gets called |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4319 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4320 | char *param = audio_device_address_to_parameter(device, address); |
| 4321 | mpClientInterface->setParameters(output, String8(param)); |
| 4322 | free(param); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4323 | } |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 4324 | updateAudioProfiles(device, output, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4325 | if (!profile->hasValidAudioProfile()) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4326 | ALOGW("checkOutputsForDevice() missing param"); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4327 | desc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4328 | output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4329 | } else if (profile->hasDynamicAudioProfile()) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4330 | desc->close(); |
Phil Burk | 702b105 | 2016-03-02 16:38:26 -0800 | [diff] [blame] | 4331 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4332 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 4333 | profile->pickAudioProfile( |
| 4334 | config.sample_rate, config.channel_mask, config.format); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4335 | config.offload_info.sample_rate = config.sample_rate; |
| 4336 | config.offload_info.channel_mask = config.channel_mask; |
| 4337 | config.offload_info.format = config.format; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4338 | |
| 4339 | status_t status = desc->open(&config, device, address, AUDIO_STREAM_DEFAULT, |
| 4340 | AUDIO_OUTPUT_FLAG_NONE, &output); |
| 4341 | if (status != NO_ERROR) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4342 | output = AUDIO_IO_HANDLE_NONE; |
| 4343 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4344 | } |
| 4345 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4346 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4347 | addOutput(output, desc); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4348 | if (device_distinguishes_on_address(device) && address != "0") { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4349 | sp<AudioPolicyMix> policyMix; |
| 4350 | if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4351 | ALOGE("checkOutputsForDevice() cannot find policy for address %s", |
| 4352 | address.string()); |
| 4353 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4354 | policyMix->setOutput(desc); |
Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 4355 | desc->mPolicyMix = policyMix->getMix(); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4356 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 4357 | } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
| 4358 | hasPrimaryOutput()) { |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 4359 | // no duplicated output for direct outputs and |
| 4360 | // outputs used by dynamic policy mixes |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4361 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4362 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4363 | //TODO: configure audio effect output stage here |
| 4364 | |
| 4365 | // 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] | 4366 | sp<SwAudioOutputDescriptor> dupOutputDesc = |
| 4367 | new SwAudioOutputDescriptor(NULL, mpClientInterface); |
| 4368 | status_t status = dupOutputDesc->openDuplicating(mPrimaryOutput, desc, |
| 4369 | &duplicatedOutput); |
| 4370 | if (status == NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4371 | // add duplicated output descriptor |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4372 | addOutput(duplicatedOutput, dupOutputDesc); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4373 | } else { |
| 4374 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4375 | mPrimaryOutput->mIoHandle, output); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4376 | desc->close(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4377 | removeOutput(output); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4378 | nextAudioPortGeneration(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4379 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4380 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4381 | } |
| 4382 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4383 | } else { |
| 4384 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4385 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4386 | if (output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4387 | ALOGW("checkOutputsForDevice() could not open output for device %x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4388 | profiles.removeAt(profile_index); |
| 4389 | profile_index--; |
| 4390 | } else { |
| 4391 | outputs.add(output); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4392 | // Load digital format info only for digital devices |
| 4393 | if (audio_device_is_digital(device)) { |
| 4394 | devDesc->importAudioPort(profile); |
| 4395 | } |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 4396 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4397 | if (device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4398 | ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)", |
| 4399 | device, address.string()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4400 | setOutputDevice(desc, device, true/*force*/, 0/*delay*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4401 | NULL/*patch handle*/, address.string()); |
| 4402 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4403 | ALOGV("checkOutputsForDevice(): adding output %d", output); |
| 4404 | } |
| 4405 | } |
| 4406 | |
| 4407 | if (profiles.isEmpty()) { |
| 4408 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 4409 | return BAD_VALUE; |
| 4410 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4411 | } else { // Disconnect |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4412 | // check if one opened output is not needed any more after disconnecting one device |
| 4413 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4414 | desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4415 | if (!desc->isDuplicated()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4416 | // exact match on device |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4417 | if (device_distinguishes_on_address(device) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4418 | (desc->supportedDevices() == device)) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4419 | findIoHandlesByAddress(desc, device, address, outputs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4420 | } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4421 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", |
| 4422 | mOutputs.keyAt(i)); |
| 4423 | outputs.add(mOutputs.keyAt(i)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4424 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4425 | } |
| 4426 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4427 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4428 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4429 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 4430 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4431 | if (profile->supportDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4432 | ALOGV("checkOutputsForDevice(): " |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4433 | "clearing direct output profile %zu on module %s", |
| 4434 | j, hwModule->getName()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4435 | profile->clearAudioProfiles(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4436 | } |
| 4437 | } |
| 4438 | } |
| 4439 | } |
| 4440 | return NO_ERROR; |
| 4441 | } |
| 4442 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4443 | status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4444 | audio_policy_dev_state_t state, |
| 4445 | SortedVector<audio_io_handle_t>& inputs, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4446 | const String8& address) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4447 | { |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4448 | audio_devices_t device = devDesc->type(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4449 | sp<AudioInputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4450 | |
| 4451 | if (audio_device_is_digital(device)) { |
| 4452 | // erase all current sample rates, formats and channel masks |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 4453 | devDesc->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4454 | } |
| 4455 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4456 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 4457 | // first list already open inputs that can be routed to this device |
| 4458 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4459 | desc = mInputs.valueAt(input_index); |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4460 | if (desc->mProfile->supportDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4461 | ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index)); |
| 4462 | inputs.add(mInputs.keyAt(input_index)); |
| 4463 | } |
| 4464 | } |
| 4465 | |
| 4466 | // then look for input profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4467 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4468 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4469 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4470 | profile_index < hwModule->getInputProfiles().size(); |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4471 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4472 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4473 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4474 | if (profile->supportDevice(device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4475 | if (!device_distinguishes_on_address(device) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4476 | profile->supportDeviceAddress(address)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4477 | profiles.add(profile); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4478 | ALOGV("checkInputsForDevice(): adding profile %zu from module %s", |
| 4479 | profile_index, hwModule->getName()); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4480 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4481 | } |
| 4482 | } |
| 4483 | } |
| 4484 | |
| 4485 | if (profiles.isEmpty() && inputs.isEmpty()) { |
| 4486 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 4487 | return BAD_VALUE; |
| 4488 | } |
| 4489 | |
| 4490 | // open inputs for matching profiles if needed. Direct inputs are also opened to |
| 4491 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 4492 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { |
| 4493 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4494 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4495 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4496 | // nothing to do if one input is already opened for this profile |
| 4497 | size_t input_index; |
| 4498 | for (input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4499 | desc = mInputs.valueAt(input_index); |
| 4500 | if (desc->mProfile == profile) { |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4501 | if (audio_device_is_digital(device)) { |
| 4502 | devDesc->importAudioPort(profile); |
| 4503 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4504 | break; |
| 4505 | } |
| 4506 | } |
| 4507 | if (input_index != mInputs.size()) { |
| 4508 | continue; |
| 4509 | } |
| 4510 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4511 | if (!profile->canOpenNewIo()) { |
| 4512 | ALOGW("Max Input number %u already opened for this profile %s", |
| 4513 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 4514 | continue; |
| 4515 | } |
| 4516 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4517 | desc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4518 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4519 | status_t status = desc->open(nullptr, |
| 4520 | device, |
| 4521 | address, |
| 4522 | AUDIO_SOURCE_MIC, |
| 4523 | AUDIO_INPUT_FLAG_NONE, |
| 4524 | &input); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4525 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4526 | if (status == NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4527 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4528 | char *param = audio_device_address_to_parameter(device, address); |
| 4529 | mpClientInterface->setParameters(input, String8(param)); |
| 4530 | free(param); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4531 | } |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 4532 | updateAudioProfiles(device, input, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4533 | if (!profile->hasValidAudioProfile()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4534 | ALOGW("checkInputsForDevice() direct input missing param"); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4535 | desc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4536 | input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4537 | } |
| 4538 | |
| 4539 | if (input != 0) { |
| 4540 | addInput(input, desc); |
| 4541 | } |
| 4542 | } // endif input != 0 |
| 4543 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4544 | if (input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4545 | ALOGW("checkInputsForDevice() could not open input for device 0x%X", device); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4546 | profiles.removeAt(profile_index); |
| 4547 | profile_index--; |
| 4548 | } else { |
| 4549 | inputs.add(input); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4550 | if (audio_device_is_digital(device)) { |
| 4551 | devDesc->importAudioPort(profile); |
| 4552 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4553 | ALOGV("checkInputsForDevice(): adding input %d", input); |
| 4554 | } |
| 4555 | } // end scan profiles |
| 4556 | |
| 4557 | if (profiles.isEmpty()) { |
| 4558 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 4559 | return BAD_VALUE; |
| 4560 | } |
| 4561 | } else { |
| 4562 | // Disconnect |
| 4563 | // check if one opened input is not needed any more after disconnecting one device |
| 4564 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4565 | desc = mInputs.valueAt(input_index); |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4566 | if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4567 | ALOGV("checkInputsForDevice(): disconnecting adding input %d", |
| 4568 | mInputs.keyAt(input_index)); |
| 4569 | inputs.add(mInputs.keyAt(input_index)); |
| 4570 | } |
| 4571 | } |
| 4572 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4573 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4574 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4575 | profile_index < hwModule->getInputProfiles().size(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4576 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4577 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4578 | if (profile->supportDevice(device)) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4579 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %s", |
| 4580 | profile_index, hwModule->getName()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4581 | profile->clearAudioProfiles(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4582 | } |
| 4583 | } |
| 4584 | } |
| 4585 | } // end disconnect |
| 4586 | |
| 4587 | return NO_ERROR; |
| 4588 | } |
| 4589 | |
| 4590 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4591 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4592 | { |
| 4593 | ALOGV("closeOutput(%d)", output); |
| 4594 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4595 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4596 | if (outputDesc == NULL) { |
| 4597 | ALOGW("closeOutput() unknown output %d", output); |
| 4598 | return; |
| 4599 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4600 | mPolicyMixes.closeOutput(outputDesc); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4601 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4602 | // look for duplicated outputs connected to the output being removed. |
| 4603 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4604 | sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4605 | if (dupOutputDesc->isDuplicated() && |
| 4606 | (dupOutputDesc->mOutput1 == outputDesc || |
| 4607 | dupOutputDesc->mOutput2 == outputDesc)) { |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4608 | sp<SwAudioOutputDescriptor> outputDesc2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4609 | if (dupOutputDesc->mOutput1 == outputDesc) { |
| 4610 | outputDesc2 = dupOutputDesc->mOutput2; |
| 4611 | } else { |
| 4612 | outputDesc2 = dupOutputDesc->mOutput1; |
| 4613 | } |
| 4614 | // As all active tracks on duplicated output will be deleted, |
| 4615 | // and as they were also referenced on the other output, the reference |
| 4616 | // count for their stream type must be adjusted accordingly on |
| 4617 | // the other output. |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4618 | bool wasActive = outputDesc2->isActive(); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4619 | for (int j = 0; j < AUDIO_STREAM_CNT; j++) { |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame^] | 4620 | int activeCount = dupOutputDesc->streamActiveCount((audio_stream_type_t)j); |
| 4621 | outputDesc2->changeStreamActiveCount((audio_stream_type_t)j,-activeCount); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4622 | } |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4623 | // stop() will be a no op if the output is still active but is needed in case all |
| 4624 | // active streams refcounts where cleared above |
| 4625 | if (wasActive) { |
| 4626 | outputDesc2->stop(); |
| 4627 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4628 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); |
| 4629 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); |
| 4630 | |
| 4631 | mpClientInterface->closeOutput(duplicatedOutput); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4632 | removeOutput(duplicatedOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4633 | } |
| 4634 | } |
| 4635 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4636 | nextAudioPortGeneration(); |
| 4637 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4638 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4639 | if (index >= 0) { |
| 4640 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4641 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4642 | mAudioPatches.removeItemsAt(index); |
| 4643 | mpClientInterface->onAudioPatchListUpdate(); |
| 4644 | } |
| 4645 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4646 | outputDesc->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4647 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4648 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4649 | mPreviousOutputs = mOutputs; |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4650 | } |
| 4651 | |
| 4652 | void AudioPolicyManager::closeInput(audio_io_handle_t input) |
| 4653 | { |
| 4654 | ALOGV("closeInput(%d)", input); |
| 4655 | |
| 4656 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
| 4657 | if (inputDesc == NULL) { |
| 4658 | ALOGW("closeInput() unknown input %d", input); |
| 4659 | return; |
| 4660 | } |
| 4661 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4662 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4663 | |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 4664 | audio_devices_t device = inputDesc->mDevice; |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4665 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4666 | if (index >= 0) { |
| 4667 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4668 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4669 | mAudioPatches.removeItemsAt(index); |
| 4670 | mpClientInterface->onAudioPatchListUpdate(); |
| 4671 | } |
| 4672 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4673 | inputDesc->close(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4674 | mInputs.removeItem(input); |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 4675 | |
| 4676 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); |
| 4677 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
| 4678 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
| 4679 | SoundTrigger::setCaptureState(false); |
| 4680 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4681 | } |
| 4682 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4683 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice( |
| 4684 | audio_devices_t device, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4685 | const SwAudioOutputCollection& openOutputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4686 | { |
| 4687 | SortedVector<audio_io_handle_t> outputs; |
| 4688 | |
| 4689 | ALOGVV("getOutputsForDevice() device %04x", device); |
| 4690 | for (size_t i = 0; i < openOutputs.size(); i++) { |
Eric Laurent | 37ddbb4 | 2016-08-10 16:19:14 -0700 | [diff] [blame] | 4691 | ALOGVV("output %zu isDuplicated=%d device=%04x", |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4692 | i, openOutputs.valueAt(i)->isDuplicated(), |
| 4693 | openOutputs.valueAt(i)->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4694 | if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) { |
| 4695 | ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i)); |
| 4696 | outputs.add(openOutputs.keyAt(i)); |
| 4697 | } |
| 4698 | } |
| 4699 | return outputs; |
| 4700 | } |
| 4701 | |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 4702 | void AudioPolicyManager::checkForDeviceAndOutputChanges(std::function<bool()> onOutputsChecked) |
| 4703 | { |
| 4704 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP |
| 4705 | // output is suspended before any tracks are moved to it |
| 4706 | checkA2dpSuspend(); |
| 4707 | checkOutputForAllStrategies(); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 4708 | if (onOutputsChecked != nullptr && onOutputsChecked()) checkA2dpSuspend(); |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 4709 | updateDevicesAndOutputs(); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 4710 | if (mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD) != 0) { |
| 4711 | setMsdPatch(); |
| 4712 | } |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 4713 | } |
| 4714 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4715 | void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4716 | { |
| 4717 | audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/); |
| 4718 | audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 4719 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs); |
| 4720 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs); |
| 4721 | |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4722 | // also take into account external policy-related changes: add all outputs which are |
| 4723 | // associated with policies in the "before" and "after" output vectors |
| 4724 | ALOGVV("checkOutputForStrategy(): policy related outputs"); |
| 4725 | for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4726 | const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4727 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4728 | srcOutputs.add(desc->mIoHandle); |
| 4729 | ALOGVV(" previous outputs: adding %d", desc->mIoHandle); |
| 4730 | } |
| 4731 | } |
| 4732 | for (size_t i = 0 ; i < mOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4733 | const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4734 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4735 | dstOutputs.add(desc->mIoHandle); |
| 4736 | ALOGVV(" new outputs: adding %d", desc->mIoHandle); |
| 4737 | } |
| 4738 | } |
| 4739 | |
Mikhail Naganov | 9de8bd1 | 2018-07-23 16:41:31 -0700 | [diff] [blame] | 4740 | if (srcOutputs != dstOutputs) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 4741 | // get maximum latency of all source outputs to determine the minimum mute time guaranteeing |
| 4742 | // audio from invalidated tracks will be rendered when unmuting |
| 4743 | uint32_t maxLatency = 0; |
| 4744 | for (audio_io_handle_t srcOut : srcOutputs) { |
| 4745 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); |
| 4746 | if (desc != 0 && maxLatency < desc->latency()) { |
| 4747 | maxLatency = desc->latency(); |
| 4748 | } |
| 4749 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4750 | ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d", |
| 4751 | strategy, srcOutputs[0], dstOutputs[0]); |
| 4752 | // mute strategy while moving tracks from one output to another |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4753 | for (audio_io_handle_t srcOut : srcOutputs) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 4754 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); |
| 4755 | if (desc != 0 && isStrategyActive(desc, strategy)) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4756 | setStrategyMute(strategy, true, desc); |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 4757 | setStrategyMute(strategy, false, desc, maxLatency * LATENCY_MUTE_FACTOR, newDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4758 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4759 | sp<SourceClientDescriptor> source = |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4760 | getSourceForStrategyOnOutput(srcOut, strategy); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4761 | if (source != 0){ |
| 4762 | connectAudioSource(source); |
| 4763 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4764 | } |
| 4765 | |
| 4766 | // Move effects associated to this strategy from previous output to new output |
| 4767 | if (strategy == STRATEGY_MEDIA) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4768 | selectOutputForMusicEffects(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4769 | } |
| 4770 | // Move tracks associated to this strategy from previous output to new output |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4771 | for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4772 | if (getStrategy((audio_stream_type_t)i) == strategy) { |
| 4773 | mpClientInterface->invalidateStream((audio_stream_type_t)i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4774 | } |
| 4775 | } |
| 4776 | } |
| 4777 | } |
| 4778 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4779 | void AudioPolicyManager::checkOutputForAllStrategies() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4780 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4781 | 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] | 4782 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4783 | checkOutputForStrategy(STRATEGY_PHONE); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4784 | 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] | 4785 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4786 | checkOutputForStrategy(STRATEGY_SONIFICATION); |
| 4787 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4788 | checkOutputForStrategy(STRATEGY_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4789 | checkOutputForStrategy(STRATEGY_MEDIA); |
| 4790 | checkOutputForStrategy(STRATEGY_DTMF); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4791 | checkOutputForStrategy(STRATEGY_REROUTING); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4792 | } |
| 4793 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4794 | void AudioPolicyManager::checkA2dpSuspend() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4795 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4796 | audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput(); |
Aniket Kumar Lata | a8ee996 | 2018-01-31 20:24:23 -0800 | [diff] [blame] | 4797 | if (a2dpOutput == 0 || mOutputs.isA2dpOffloadedOnPrimary()) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4798 | mA2dpSuspended = false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4799 | return; |
| 4800 | } |
| 4801 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4802 | bool isScoConnected = |
Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 4803 | ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET & |
| 4804 | ~AUDIO_DEVICE_BIT_IN) != 0) || |
| 4805 | ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0); |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4806 | |
| 4807 | // if suspended, restore A2DP output if: |
| 4808 | // ((SCO device is NOT connected) || |
| 4809 | // ((forced usage communication is NOT SCO) && (forced usage for record is NOT SCO) && |
| 4810 | // (phone state is NOT in call) && (phone state is NOT ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4811 | // |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4812 | // if not suspended, suspend A2DP output if: |
| 4813 | // (SCO device is connected) && |
| 4814 | // ((forced usage for communication is SCO) || (forced usage for record is SCO) || |
| 4815 | // ((phone state is in call) || (phone state is ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4816 | // |
| 4817 | if (mA2dpSuspended) { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4818 | if (!isScoConnected || |
| 4819 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != |
| 4820 | AUDIO_POLICY_FORCE_BT_SCO) && |
| 4821 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != |
| 4822 | AUDIO_POLICY_FORCE_BT_SCO) && |
| 4823 | (mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4824 | (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4825 | |
| 4826 | mpClientInterface->restoreOutput(a2dpOutput); |
| 4827 | mA2dpSuspended = false; |
| 4828 | } |
| 4829 | } else { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4830 | if (isScoConnected && |
| 4831 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == |
| 4832 | AUDIO_POLICY_FORCE_BT_SCO) || |
| 4833 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == |
| 4834 | AUDIO_POLICY_FORCE_BT_SCO) || |
| 4835 | (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4836 | (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4837 | |
| 4838 | mpClientInterface->suspendOutput(a2dpOutput); |
| 4839 | mA2dpSuspended = true; |
| 4840 | } |
| 4841 | } |
| 4842 | } |
| 4843 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4844 | template <class IoDescriptor, class Filter> |
| 4845 | sp<DeviceDescriptor> AudioPolicyManager::findPreferredDevice( |
| 4846 | IoDescriptor& desc, Filter filter, bool& active, const DeviceVector& devices) |
| 4847 | { |
| 4848 | auto activeClients = desc->clientsList(true /*activeOnly*/); |
| 4849 | auto activeClientsWithRoute = |
| 4850 | desc->clientsList(true /*activeOnly*/, filter, true /*preferredDevice*/); |
| 4851 | active = activeClients.size() > 0; |
| 4852 | if (active && activeClients.size() == activeClientsWithRoute.size()) { |
| 4853 | return devices.getDeviceFromId(activeClientsWithRoute[0]->preferredDeviceId()); |
| 4854 | } |
| 4855 | return nullptr; |
| 4856 | } |
| 4857 | |
| 4858 | template <class IoCollection, class Filter> |
| 4859 | sp<DeviceDescriptor> AudioPolicyManager::findPreferredDevice( |
| 4860 | IoCollection& ioCollection, Filter filter, const DeviceVector& devices) |
| 4861 | { |
| 4862 | sp<DeviceDescriptor> device; |
| 4863 | for (size_t i = 0; i < ioCollection.size(); i++) { |
| 4864 | auto desc = ioCollection.valueAt(i); |
| 4865 | bool active; |
| 4866 | sp<DeviceDescriptor> curDevice = findPreferredDevice(desc, filter, active, devices); |
| 4867 | if (active && curDevice == nullptr) { |
| 4868 | return nullptr; |
| 4869 | } else if (curDevice != nullptr) { |
| 4870 | device = curDevice; |
| 4871 | } |
| 4872 | } |
| 4873 | return device; |
| 4874 | } |
| 4875 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4876 | audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
| 4877 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4878 | { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4879 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4880 | if (index >= 0) { |
| 4881 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4882 | if (patchDesc->mUid != mUidCached) { |
| 4883 | ALOGV("getNewOutputDevice() device %08x forced by patch %d", |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4884 | outputDesc->device(), outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4885 | return outputDesc->device(); |
| 4886 | } |
| 4887 | } |
| 4888 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4889 | // Honor explicit routing requests only if no client using default routing is active on this |
| 4890 | // input: a specific app can not force routing for other apps by setting a preferred device. |
| 4891 | bool active; // unused |
| 4892 | sp<DeviceDescriptor> deviceDesc = |
| 4893 | findPreferredDevice(outputDesc, STRATEGY_NONE, active, mAvailableOutputDevices); |
| 4894 | if (deviceDesc != nullptr) { |
| 4895 | return deviceDesc->type(); |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 4896 | } |
| 4897 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4898 | // 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] | 4899 | // 1: the strategy enforced audible is active and enforced on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4900 | // use device for strategy enforced audible |
| 4901 | // 2: we are in call or the strategy phone is active on the output: |
| 4902 | // use device for strategy phone |
Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4903 | // 3: the strategy sonification is active on the output: |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4904 | // use device for strategy sonification |
Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4905 | // 4: the strategy for enforced audible is active but not enforced on the output: |
| 4906 | // use the device for strategy enforced audible |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4907 | // 5: the strategy accessibility is active on the output: |
| 4908 | // use device for strategy accessibility |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4909 | // 6: the strategy "respectful" sonification is active on the output: |
| 4910 | // use device for strategy "respectful" sonification |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4911 | // 7: the strategy media is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4912 | // use device for strategy media |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4913 | // 8: the strategy DTMF is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4914 | // use device for strategy DTMF |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4915 | // 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] | 4916 | // use device for strategy t-t-s |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 4917 | |
| 4918 | // FIXME: extend use of isStrategyActiveOnSameModule() to all strategies |
| 4919 | // with a refined rule considering mutually exclusive devices (using same backend) |
| 4920 | // as opposed to all streams on the same audio HAL module. |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4921 | audio_devices_t device = AUDIO_DEVICE_NONE; |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4922 | if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4923 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4924 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
| 4925 | } else if (isInCall() || |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 4926 | isStrategyActiveOnSameModule(outputDesc, STRATEGY_PHONE)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4927 | device = getDeviceForStrategy(STRATEGY_PHONE, fromCache); |
Yung Ti Su | 3e7eb5e | 2018-06-13 11:48:42 +0800 | [diff] [blame] | 4928 | } else if (isStrategyActiveOnSameModule(outputDesc, STRATEGY_SONIFICATION)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4929 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache); |
Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4930 | } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) { |
| 4931 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4932 | } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) { |
| 4933 | device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4934 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4935 | device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4936 | } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4937 | device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4938 | } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4939 | device = getDeviceForStrategy(STRATEGY_DTMF, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4940 | } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4941 | device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4942 | } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4943 | device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4944 | } |
| 4945 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4946 | ALOGV("getNewOutputDevice() selected device %x", device); |
| 4947 | return device; |
| 4948 | } |
| 4949 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4950 | audio_devices_t AudioPolicyManager::getNewInputDevice(const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4951 | { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4952 | audio_devices_t device = AUDIO_DEVICE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4953 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4954 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4955 | if (index >= 0) { |
| 4956 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4957 | if (patchDesc->mUid != mUidCached) { |
| 4958 | ALOGV("getNewInputDevice() device %08x forced by patch %d", |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4959 | inputDesc->mDevice, inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4960 | return inputDesc->mDevice; |
| 4961 | } |
| 4962 | } |
| 4963 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4964 | // Honor explicit routing requests only if no client using default routing is active on this |
| 4965 | // input: a specific app can not force routing for other apps by setting a preferred device. |
| 4966 | bool active; |
| 4967 | sp<DeviceDescriptor> deviceDesc = |
| 4968 | findPreferredDevice(inputDesc, AUDIO_SOURCE_DEFAULT, active, mAvailableInputDevices); |
| 4969 | if (deviceDesc != nullptr) { |
| 4970 | return deviceDesc->type(); |
| 4971 | } |
| 4972 | |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 4973 | // If we are not in call and no client is active on this input, this methods returns |
| 4974 | // 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] | 4975 | audio_source_t source = inputDesc->getHighestPrioritySource(true /*activeOnly*/); |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 4976 | if (source == AUDIO_SOURCE_DEFAULT && isInCall()) { |
| 4977 | source = AUDIO_SOURCE_VOICE_COMMUNICATION; |
| 4978 | } |
| 4979 | if (source != AUDIO_SOURCE_DEFAULT) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4980 | device = getDeviceAndMixForInputSource(source); |
| 4981 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4982 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4983 | return device; |
| 4984 | } |
| 4985 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4986 | bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1, |
| 4987 | audio_stream_type_t stream2) { |
Jean-Michel Trivi | 99bb2f9 | 2016-11-23 15:52:07 -0800 | [diff] [blame] | 4988 | return (stream1 == stream2); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4989 | } |
| 4990 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4991 | uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4992 | return (uint32_t)getStrategy(stream); |
| 4993 | } |
| 4994 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4995 | audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4996 | // By checking the range of stream before calling getStrategy, we avoid |
| 4997 | // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE |
| 4998 | // 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] | 4999 | if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5000 | return AUDIO_DEVICE_NONE; |
| 5001 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 5002 | audio_devices_t devices = AUDIO_DEVICE_NONE; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5003 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 5004 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 5005 | continue; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5006 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5007 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 5008 | audio_devices_t curDevices = |
Eric Laurent | 447a87b | 2016-07-07 17:32:10 -0700 | [diff] [blame] | 5009 | getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/); |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5010 | for (audio_io_handle_t output : getOutputsForDevice(curDevices, mOutputs)) { |
| 5011 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5012 | if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 5013 | curDevices |= outputDesc->device(); |
| 5014 | } |
| 5015 | } |
| 5016 | devices |= curDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5017 | } |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 5018 | |
| 5019 | /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it |
| 5020 | and doesn't really need to.*/ |
| 5021 | if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) { |
| 5022 | devices |= AUDIO_DEVICE_OUT_SPEAKER; |
| 5023 | devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
| 5024 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5025 | return devices; |
| 5026 | } |
| 5027 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5028 | routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const |
| 5029 | { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5030 | 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] | 5031 | return mEngine->getStrategyForStream(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5032 | } |
| 5033 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5034 | routing_strategy AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) { |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5035 | // flags to strategy mapping |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5036 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5037 | return STRATEGY_TRANSMITTED_THROUGH_SPEAKER; |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5038 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5039 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5040 | return STRATEGY_ENFORCED_AUDIBLE; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5041 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5042 | // usage to strategy mapping |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5043 | return mEngine->getStrategyForUsage(attr->usage); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5044 | } |
| 5045 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5046 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5047 | switch(stream) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5048 | case AUDIO_STREAM_MUSIC: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5049 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
| 5050 | updateDevicesAndOutputs(); |
| 5051 | break; |
| 5052 | default: |
| 5053 | break; |
| 5054 | } |
| 5055 | } |
| 5056 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5057 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 5058 | |
| 5059 | // skip beacon mute management if a dedicated TTS output is available |
| 5060 | if (mTtsOutputAvailable) { |
| 5061 | return 0; |
| 5062 | } |
| 5063 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5064 | switch(event) { |
| 5065 | case STARTING_OUTPUT: |
| 5066 | mBeaconMuteRefCount++; |
| 5067 | break; |
| 5068 | case STOPPING_OUTPUT: |
| 5069 | if (mBeaconMuteRefCount > 0) { |
| 5070 | mBeaconMuteRefCount--; |
| 5071 | } |
| 5072 | break; |
| 5073 | case STARTING_BEACON: |
| 5074 | mBeaconPlayingRefCount++; |
| 5075 | break; |
| 5076 | case STOPPING_BEACON: |
| 5077 | if (mBeaconPlayingRefCount > 0) { |
| 5078 | mBeaconPlayingRefCount--; |
| 5079 | } |
| 5080 | break; |
| 5081 | } |
| 5082 | |
| 5083 | if (mBeaconMuteRefCount > 0) { |
| 5084 | // any playback causes beacon to be muted |
| 5085 | return setBeaconMute(true); |
| 5086 | } else { |
| 5087 | // no other playback: unmute when beacon starts playing, mute when it stops |
| 5088 | return setBeaconMute(mBeaconPlayingRefCount == 0); |
| 5089 | } |
| 5090 | } |
| 5091 | |
| 5092 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { |
| 5093 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", |
| 5094 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); |
| 5095 | // keep track of muted state to avoid repeating mute/unmute operations |
| 5096 | if (mBeaconMuted != mute) { |
| 5097 | // mute/unmute AUDIO_STREAM_TTS on all outputs |
| 5098 | ALOGV("\t muting %d", mute); |
| 5099 | uint32_t maxLatency = 0; |
| 5100 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5101 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5102 | setStreamMute(AUDIO_STREAM_TTS, mute/*on*/, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5103 | desc, |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5104 | 0 /*delay*/, AUDIO_DEVICE_NONE); |
| 5105 | const uint32_t latency = desc->latency() * 2; |
| 5106 | if (latency > maxLatency) { |
| 5107 | maxLatency = latency; |
| 5108 | } |
| 5109 | } |
| 5110 | mBeaconMuted = mute; |
| 5111 | return maxLatency; |
| 5112 | } |
| 5113 | return 0; |
| 5114 | } |
| 5115 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5116 | audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5117 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5118 | { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5119 | // Honor explicit routing requests only if all active clients have a preferred route in which |
| 5120 | // case the last active client route is used |
| 5121 | sp<DeviceDescriptor> deviceDesc = findPreferredDevice(mOutputs, strategy, mAvailableOutputDevices); |
| 5122 | if (deviceDesc != nullptr) { |
| 5123 | return deviceDesc->type(); |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 5124 | } |
| 5125 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5126 | if (fromCache) { |
| 5127 | ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x", |
| 5128 | strategy, mDeviceForStrategy[strategy]); |
| 5129 | return mDeviceForStrategy[strategy]; |
| 5130 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5131 | return mEngine->getDeviceForStrategy(strategy); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5132 | } |
| 5133 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5134 | void AudioPolicyManager::updateDevicesAndOutputs() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5135 | { |
| 5136 | for (int i = 0; i < NUM_STRATEGIES; i++) { |
| 5137 | mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
| 5138 | } |
| 5139 | mPreviousOutputs = mOutputs; |
| 5140 | } |
| 5141 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5142 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5143 | audio_devices_t prevDevice, |
| 5144 | uint32_t delayMs) |
| 5145 | { |
| 5146 | // mute/unmute strategies using an incompatible device combination |
| 5147 | // if muting, wait for the audio in pcm buffer to be drained before proceeding |
| 5148 | // if unmuting, unmute only after the specified delay |
| 5149 | if (outputDesc->isDuplicated()) { |
| 5150 | return 0; |
| 5151 | } |
| 5152 | |
| 5153 | uint32_t muteWaitMs = 0; |
| 5154 | audio_devices_t device = outputDesc->device(); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5155 | bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5156 | |
| 5157 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
| 5158 | audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5159 | curDevice = curDevice & outputDesc->supportedDevices(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5160 | bool mute = shouldMute && (curDevice & device) && (curDevice != device); |
| 5161 | bool doMute = false; |
| 5162 | |
| 5163 | if (mute && !outputDesc->mStrategyMutedByDevice[i]) { |
| 5164 | doMute = true; |
| 5165 | outputDesc->mStrategyMutedByDevice[i] = true; |
| 5166 | } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){ |
| 5167 | doMute = true; |
| 5168 | outputDesc->mStrategyMutedByDevice[i] = false; |
| 5169 | } |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5170 | if (doMute) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5171 | for (size_t j = 0; j < mOutputs.size(); j++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5172 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5173 | // skip output if it does not share any device with current output |
| 5174 | if ((desc->supportedDevices() & outputDesc->supportedDevices()) |
| 5175 | == AUDIO_DEVICE_NONE) { |
| 5176 | continue; |
| 5177 | } |
Eric Laurent | 37ddbb4 | 2016-08-10 16:19:14 -0700 | [diff] [blame] | 5178 | ALOGVV("checkDeviceMuteStrategies() %s strategy %zu (curDevice %04x)", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5179 | mute ? "muting" : "unmuting", i, curDevice); |
| 5180 | setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5181 | if (isStrategyActive(desc, (routing_strategy)i)) { |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5182 | if (mute) { |
| 5183 | // FIXME: should not need to double latency if volume could be applied |
| 5184 | // immediately by the audioflinger mixer. We must account for the delay |
| 5185 | // between now and the next time the audioflinger thread for this output |
| 5186 | // will process a buffer (which corresponds to one buffer size, |
| 5187 | // usually 1/2 or 1/4 of the latency). |
| 5188 | if (muteWaitMs < desc->latency() * 2) { |
| 5189 | muteWaitMs = desc->latency() * 2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5190 | } |
| 5191 | } |
| 5192 | } |
| 5193 | } |
| 5194 | } |
| 5195 | } |
| 5196 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5197 | // temporary mute output if device selection changes to avoid volume bursts due to |
| 5198 | // different per device volumes |
| 5199 | if (outputDesc->isActive() && (device != prevDevice)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5200 | uint32_t tempMuteWaitMs = outputDesc->latency() * 2; |
| 5201 | // temporary mute duration is conservatively set to 4 times the reported latency |
| 5202 | uint32_t tempMuteDurationMs = outputDesc->latency() * 4; |
| 5203 | if (muteWaitMs < tempMuteWaitMs) { |
| 5204 | muteWaitMs = tempMuteWaitMs; |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5205 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5206 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5207 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5208 | if (isStrategyActive(outputDesc, (routing_strategy)i)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5209 | // make sure that we do not start the temporary mute period too early in case of |
| 5210 | // delayed device change |
| 5211 | setStrategyMute((routing_strategy)i, true, outputDesc, delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5212 | setStrategyMute((routing_strategy)i, false, outputDesc, |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5213 | delayMs + tempMuteDurationMs, device); |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5214 | } |
| 5215 | } |
| 5216 | } |
| 5217 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5218 | // wait for the PCM output buffers to empty before proceeding with the rest of the command |
| 5219 | if (muteWaitMs > delayMs) { |
| 5220 | muteWaitMs -= delayMs; |
| 5221 | usleep(muteWaitMs * 1000); |
| 5222 | return muteWaitMs; |
| 5223 | } |
| 5224 | return 0; |
| 5225 | } |
| 5226 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5227 | uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5228 | audio_devices_t device, |
| 5229 | bool force, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5230 | int delayMs, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5231 | audio_patch_handle_t *patchHandle, |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5232 | const char *address, |
| 5233 | bool requiresMuteCheck) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5234 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5235 | ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5236 | AudioParameter param; |
| 5237 | uint32_t muteWaitMs; |
| 5238 | |
| 5239 | if (outputDesc->isDuplicated()) { |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5240 | muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs, |
| 5241 | nullptr /* patchHandle */, nullptr /* address */, requiresMuteCheck); |
| 5242 | muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs, |
| 5243 | nullptr /* patchHandle */, nullptr /* address */, requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5244 | return muteWaitMs; |
| 5245 | } |
| 5246 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current |
| 5247 | // output profile |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5248 | if ((device != AUDIO_DEVICE_NONE) && |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5249 | ((device & outputDesc->supportedDevices()) == AUDIO_DEVICE_NONE)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5250 | return 0; |
| 5251 | } |
| 5252 | |
| 5253 | // filter devices according to output selected |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5254 | device = (audio_devices_t)(device & outputDesc->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5255 | |
| 5256 | audio_devices_t prevDevice = outputDesc->mDevice; |
| 5257 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 5258 | ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5259 | |
| 5260 | if (device != AUDIO_DEVICE_NONE) { |
| 5261 | outputDesc->mDevice = device; |
| 5262 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5263 | |
| 5264 | // if the outputs are not materially active, there is no need to mute. |
| 5265 | if (requiresMuteCheck) { |
| 5266 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs); |
| 5267 | } else { |
| 5268 | ALOGV("%s: suppressing checkDeviceMuteStrategies", __func__); |
| 5269 | muteWaitMs = 0; |
| 5270 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5271 | |
| 5272 | // Do not change the routing if: |
Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 5273 | // the requested device is AUDIO_DEVICE_NONE |
| 5274 | // OR the requested device is the same as current device |
| 5275 | // AND force is not specified |
| 5276 | // AND the output is connected by a valid audio patch. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5277 | // Doing this check here allows the caller to call setOutputDevice() without conditions |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 5278 | if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && |
| 5279 | !force && |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 5280 | outputDesc->getPatchHandle() != 0) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5281 | ALOGV("setOutputDevice() setting same device 0x%04x or null device", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5282 | return muteWaitMs; |
| 5283 | } |
| 5284 | |
| 5285 | ALOGV("setOutputDevice() changing device"); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5286 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5287 | // do the routing |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5288 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5289 | resetOutputDevice(outputDesc, delayMs, NULL); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5290 | } else { |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 5291 | DeviceVector deviceList; |
| 5292 | if ((address == NULL) || (strlen(address) == 0)) { |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 5293 | deviceList = mAvailableOutputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 5294 | } else { |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 5295 | sp<DeviceDescriptor> deviceDesc = mAvailableOutputDevices.getDevice( |
| 5296 | device, String8(address)); |
| 5297 | if (deviceDesc) deviceList.add(deviceDesc); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 5298 | } |
| 5299 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5300 | if (!deviceList.isEmpty()) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5301 | PatchBuilder patchBuilder; |
| 5302 | patchBuilder.addSource(outputDesc); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5303 | for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5304 | patchBuilder.addSink(deviceList.itemAt(i)); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5305 | } |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5306 | installPatch(__func__, patchHandle, outputDesc.get(), patchBuilder.patch(), delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5307 | } |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 5308 | |
| 5309 | // inform all input as well |
| 5310 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 5311 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5312 | if (!is_virtual_input_device(inputDescriptor->mDevice)) { |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 5313 | AudioParameter inputCmd = AudioParameter(); |
| 5314 | ALOGV("%s: inform input %d of device:%d", __func__, |
| 5315 | inputDescriptor->mIoHandle, device); |
| 5316 | inputCmd.addInt(String8(AudioParameter::keyRouting),device); |
| 5317 | mpClientInterface->setParameters(inputDescriptor->mIoHandle, |
| 5318 | inputCmd.toString(), |
| 5319 | delayMs); |
| 5320 | } |
| 5321 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5322 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5323 | |
| 5324 | // update stream volumes according to new device |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5325 | applyStreamVolumes(outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5326 | |
| 5327 | return muteWaitMs; |
| 5328 | } |
| 5329 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5330 | status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5331 | int delayMs, |
| 5332 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5333 | { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5334 | ssize_t index; |
| 5335 | if (patchHandle) { |
| 5336 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 5337 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 5338 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5339 | } |
| 5340 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5341 | return INVALID_OPERATION; |
| 5342 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5343 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 5344 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5345 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 5346 | outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5347 | removeAudioPatch(patchDesc->mHandle); |
| 5348 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5349 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5350 | return status; |
| 5351 | } |
| 5352 | |
| 5353 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, |
| 5354 | audio_devices_t device, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5355 | bool force, |
| 5356 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5357 | { |
| 5358 | status_t status = NO_ERROR; |
| 5359 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5360 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5361 | if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) { |
| 5362 | inputDesc->mDevice = device; |
| 5363 | |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 5364 | DeviceVector deviceList = mAvailableInputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5365 | if (!deviceList.isEmpty()) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5366 | PatchBuilder patchBuilder; |
| 5367 | patchBuilder.addSink(inputDesc, |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 5368 | // AUDIO_SOURCE_HOTWORD is for internal use only: |
| 5369 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5370 | [inputDesc](const PatchBuilder::mix_usecase_t& usecase) { |
| 5371 | auto result = usecase; |
| 5372 | if (result.source == AUDIO_SOURCE_HOTWORD && !inputDesc->isSoundTrigger()) { |
| 5373 | result.source = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 5374 | } |
| 5375 | return result; }). |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5376 | //only one input device for now |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5377 | addSource(deviceList.itemAt(0)); |
| 5378 | status = installPatch(__func__, patchHandle, inputDesc.get(), patchBuilder.patch(), 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5379 | } |
| 5380 | } |
| 5381 | return status; |
| 5382 | } |
| 5383 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5384 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, |
| 5385 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5386 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5387 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5388 | ssize_t index; |
| 5389 | if (patchHandle) { |
| 5390 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 5391 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 5392 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5393 | } |
| 5394 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5395 | return INVALID_OPERATION; |
| 5396 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5397 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 5398 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5399 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 5400 | inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5401 | removeAudioPatch(patchDesc->mHandle); |
| 5402 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5403 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5404 | return status; |
| 5405 | } |
| 5406 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 5407 | sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5408 | const String8& address, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5409 | uint32_t& samplingRate, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5410 | audio_format_t& format, |
| 5411 | audio_channel_mask_t& channelMask, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5412 | audio_input_flags_t flags) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5413 | { |
| 5414 | // Choose an input profile based on the requested capture parameters: select the first available |
| 5415 | // profile supporting all requested parameters. |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5416 | // |
| 5417 | // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return |
| 5418 | // the best matching profile, not the first one. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5419 | |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5420 | sp<IOProfile> firstInexact; |
| 5421 | uint32_t updatedSamplingRate = 0; |
| 5422 | audio_format_t updatedFormat = AUDIO_FORMAT_INVALID; |
| 5423 | audio_channel_mask_t updatedChannelMask = AUDIO_CHANNEL_INVALID; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 5424 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 5425 | for (const auto& profile : hwModule->getInputProfiles()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5426 | // profile->log(); |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5427 | //updatedFormat = format; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5428 | if (profile->isCompatibleProfile(device, address, samplingRate, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5429 | &samplingRate /*updatedSamplingRate*/, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5430 | format, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5431 | &format, /*updatedFormat*/ |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5432 | channelMask, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5433 | &channelMask /*updatedChannelMask*/, |
| 5434 | // FIXME ugly cast |
| 5435 | (audio_output_flags_t) flags, |
| 5436 | true /*exactMatchRequiredForInputFlags*/)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5437 | return profile; |
| 5438 | } |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5439 | if (firstInexact == nullptr && profile->isCompatibleProfile(device, address, |
| 5440 | samplingRate, |
| 5441 | &updatedSamplingRate, |
| 5442 | format, |
| 5443 | &updatedFormat, |
| 5444 | channelMask, |
| 5445 | &updatedChannelMask, |
| 5446 | // FIXME ugly cast |
| 5447 | (audio_output_flags_t) flags, |
| 5448 | false /*exactMatchRequiredForInputFlags*/)) { |
| 5449 | firstInexact = profile; |
| 5450 | } |
| 5451 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5452 | } |
| 5453 | } |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5454 | if (firstInexact != nullptr) { |
| 5455 | samplingRate = updatedSamplingRate; |
| 5456 | format = updatedFormat; |
| 5457 | channelMask = updatedChannelMask; |
| 5458 | return firstInexact; |
| 5459 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5460 | return NULL; |
| 5461 | } |
| 5462 | |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5463 | |
| 5464 | audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5465 | AudioMix **policyMix) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5466 | { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5467 | // Honor explicit routing requests only if all active clients have a preferred route in which |
| 5468 | // case the last active client route is used |
| 5469 | sp<DeviceDescriptor> deviceDesc = |
| 5470 | findPreferredDevice(mInputs, inputSource, mAvailableInputDevices); |
| 5471 | if (deviceDesc != nullptr) { |
| 5472 | return deviceDesc->type(); |
| 5473 | } |
| 5474 | |
| 5475 | |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 5476 | audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
| 5477 | audio_devices_t selectedDeviceFromMix = |
| 5478 | mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5479 | |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 5480 | if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) { |
| 5481 | return selectedDeviceFromMix; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5482 | } |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5483 | return getDeviceForInputSource(inputSource); |
| 5484 | } |
| 5485 | |
| 5486 | audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource) |
| 5487 | { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5488 | return mEngine->getDeviceForInputSource(inputSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5489 | } |
| 5490 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5491 | float AudioPolicyManager::computeVolume(audio_stream_type_t stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5492 | int index, |
| 5493 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5494 | { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5495 | float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index); |
Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 5496 | |
| 5497 | // handle the case of accessibility active while a ringtone is playing: if the ringtone is much |
| 5498 | // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch |
| 5499 | // exploration of the dialer UI. In this situation, bring the accessibility volume closer to |
| 5500 | // the ringtone volume |
| 5501 | if ((stream == AUDIO_STREAM_ACCESSIBILITY) |
| 5502 | && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState()) |
| 5503 | && isStreamActive(AUDIO_STREAM_RING, 0)) { |
| 5504 | const float ringVolumeDB = computeVolume(AUDIO_STREAM_RING, index, device); |
| 5505 | return ringVolumeDB - 4 > volumeDB ? ringVolumeDB - 4 : volumeDB; |
| 5506 | } |
| 5507 | |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 5508 | // in-call: always cap volume by voice volume + some low headroom |
| 5509 | if ((stream != AUDIO_STREAM_VOICE_CALL) && |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5510 | (isInCall() || mOutputs.isStreamActiveLocally(AUDIO_STREAM_VOICE_CALL))) { |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 5511 | switch (stream) { |
| 5512 | case AUDIO_STREAM_SYSTEM: |
| 5513 | case AUDIO_STREAM_RING: |
| 5514 | case AUDIO_STREAM_MUSIC: |
| 5515 | case AUDIO_STREAM_ALARM: |
| 5516 | case AUDIO_STREAM_NOTIFICATION: |
| 5517 | case AUDIO_STREAM_ENFORCED_AUDIBLE: |
| 5518 | case AUDIO_STREAM_DTMF: |
| 5519 | case AUDIO_STREAM_ACCESSIBILITY: { |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5520 | int voiceVolumeIndex = |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 5521 | mVolumeCurves->getVolumeIndex(AUDIO_STREAM_VOICE_CALL, device); |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5522 | const float maxVoiceVolDb = |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 5523 | computeVolume(AUDIO_STREAM_VOICE_CALL, voiceVolumeIndex, device) |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5524 | + IN_CALL_EARPIECE_HEADROOM_DB; |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 5525 | if (volumeDB > maxVoiceVolDb) { |
| 5526 | ALOGV("computeVolume() stream %d at vol=%f overriden by stream %d at vol=%f", |
| 5527 | stream, volumeDB, AUDIO_STREAM_VOICE_CALL, maxVoiceVolDb); |
| 5528 | volumeDB = maxVoiceVolDb; |
| 5529 | } |
| 5530 | } break; |
| 5531 | default: |
| 5532 | break; |
| 5533 | } |
| 5534 | } |
| 5535 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5536 | // if a headset is connected, apply the following rules to ring tones and notifications |
| 5537 | // to avoid sound level bursts in user's ears: |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5538 | // - always attenuate notifications volume by 6dB |
| 5539 | // - attenuate ring tones volume by 6dB unless music is not playing and |
| 5540 | // speaker is part of the select devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5541 | // - if music is playing, always limit the volume to current music volume, |
| 5542 | // with a minimum threshold at -36dB so that notification is always perceived. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5543 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5544 | if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 5545 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | |
| 5546 | AUDIO_DEVICE_OUT_WIRED_HEADSET | |
Eric Laurent | 904d632 | 2017-03-17 17:20:47 -0700 | [diff] [blame] | 5547 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE | |
Jakub Pawlowski | 00f928c | 2018-03-22 13:20:03 -0700 | [diff] [blame] | 5548 | AUDIO_DEVICE_OUT_USB_HEADSET | |
| 5549 | AUDIO_DEVICE_OUT_HEARING_AID)) && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5550 | ((stream_strategy == STRATEGY_SONIFICATION) |
| 5551 | || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL) |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5552 | || (stream == AUDIO_STREAM_SYSTEM) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5553 | || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5554 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) && |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5555 | mVolumeCurves->canBeMuted(stream)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5556 | // when the phone is ringing we must consider that music could have been paused just before |
| 5557 | // by the music application and behave as if music was active if the last music track was |
| 5558 | // just stopped |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5559 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5560 | mLimitRingtoneVolume) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5561 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5562 | audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5563 | float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5564 | mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC, |
| 5565 | musicDevice), |
| 5566 | musicDevice); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5567 | float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ? |
| 5568 | musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB; |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5569 | if (volumeDB > minVolDB) { |
| 5570 | volumeDB = minVolDB; |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5571 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5572 | } |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5573 | if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 5574 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) { |
| 5575 | // on A2DP, also ensure notification volume is not too low compared to media when |
| 5576 | // intended to be played |
| 5577 | if ((volumeDB > -96.0f) && |
| 5578 | (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) { |
| 5579 | ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f", |
| 5580 | stream, device, |
| 5581 | volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB); |
| 5582 | volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB; |
| 5583 | } |
| 5584 | } |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5585 | } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) || |
| 5586 | stream_strategy != STRATEGY_SONIFICATION) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5587 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5588 | } |
| 5589 | } |
| 5590 | |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5591 | return volumeDB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5592 | } |
| 5593 | |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 5594 | int AudioPolicyManager::rescaleVolumeIndex(int srcIndex, |
| 5595 | audio_stream_type_t srcStream, |
| 5596 | audio_stream_type_t dstStream) |
| 5597 | { |
| 5598 | if (srcStream == dstStream) { |
| 5599 | return srcIndex; |
| 5600 | } |
| 5601 | float minSrc = (float)mVolumeCurves->getVolumeIndexMin(srcStream); |
| 5602 | float maxSrc = (float)mVolumeCurves->getVolumeIndexMax(srcStream); |
| 5603 | float minDst = (float)mVolumeCurves->getVolumeIndexMin(dstStream); |
| 5604 | float maxDst = (float)mVolumeCurves->getVolumeIndexMax(dstStream); |
| 5605 | |
| 5606 | return (int)(minDst + ((srcIndex - minSrc) * (maxDst - minDst)) / (maxSrc - minSrc)); |
| 5607 | } |
| 5608 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5609 | status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5610 | int index, |
| 5611 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5612 | audio_devices_t device, |
| 5613 | int delayMs, |
| 5614 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5615 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5616 | // do not change actual stream volume if the stream is muted |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5617 | if (outputDesc->mMuteCount[stream] != 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5618 | ALOGVV("checkAndSetVolume() stream %d muted count %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5619 | stream, outputDesc->mMuteCount[stream]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5620 | return NO_ERROR; |
| 5621 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5622 | audio_policy_forced_cfg_t forceUseForComm = |
| 5623 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5624 | // 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] | 5625 | if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) || |
| 5626 | (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5627 | 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] | 5628 | stream, forceUseForComm); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5629 | return INVALID_OPERATION; |
| 5630 | } |
| 5631 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5632 | if (device == AUDIO_DEVICE_NONE) { |
| 5633 | device = outputDesc->device(); |
| 5634 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5635 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5636 | float volumeDb = computeVolume(stream, index, device); |
HW Lee | da7581e | 2018-05-22 18:31:34 +0800 | [diff] [blame] | 5637 | if (outputDesc->isFixedVolume(device) || |
| 5638 | // Force VoIP volume to max for bluetooth SCO |
| 5639 | ((stream == AUDIO_STREAM_VOICE_CALL || stream == AUDIO_STREAM_BLUETOOTH_SCO) && |
| 5640 | (device & AUDIO_DEVICE_OUT_ALL_SCO) != 0)) { |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5641 | volumeDb = 0.0f; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5642 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5643 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5644 | outputDesc->setVolume(volumeDb, stream, device, delayMs, force); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5645 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5646 | if (stream == AUDIO_STREAM_VOICE_CALL || |
| 5647 | stream == AUDIO_STREAM_BLUETOOTH_SCO) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5648 | float voiceVolume; |
| 5649 | // 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] | 5650 | if (stream == AUDIO_STREAM_VOICE_CALL) { |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 5651 | voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5652 | } else { |
| 5653 | voiceVolume = 1.0; |
| 5654 | } |
| 5655 | |
Eric Laurent | 18fba84 | 2016-03-31 14:41:26 -0700 | [diff] [blame] | 5656 | if (voiceVolume != mLastVoiceVolume) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5657 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); |
| 5658 | mLastVoiceVolume = voiceVolume; |
| 5659 | } |
| 5660 | } |
| 5661 | |
| 5662 | return NO_ERROR; |
| 5663 | } |
| 5664 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5665 | void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc, |
| 5666 | audio_devices_t device, |
| 5667 | int delayMs, |
| 5668 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5669 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5670 | ALOGVV("applyStreamVolumes() for device %08x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5671 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5672 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5673 | checkAndSetVolume((audio_stream_type_t)stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5674 | mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5675 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5676 | device, |
| 5677 | delayMs, |
| 5678 | force); |
| 5679 | } |
| 5680 | } |
| 5681 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5682 | void AudioPolicyManager::setStrategyMute(routing_strategy strategy, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5683 | bool on, |
| 5684 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5685 | int delayMs, |
| 5686 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5687 | { |
Eric Laurent | 72249d5 | 2015-06-08 11:12:15 -0700 | [diff] [blame] | 5688 | ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d", |
| 5689 | strategy, on, outputDesc->getId()); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5690 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5691 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5692 | setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5693 | } |
| 5694 | } |
| 5695 | } |
| 5696 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5697 | void AudioPolicyManager::setStreamMute(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5698 | bool on, |
| 5699 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5700 | int delayMs, |
| 5701 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5702 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5703 | if (device == AUDIO_DEVICE_NONE) { |
| 5704 | device = outputDesc->device(); |
| 5705 | } |
| 5706 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5707 | ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x", |
| 5708 | stream, on, outputDesc->mMuteCount[stream], device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5709 | |
| 5710 | if (on) { |
| 5711 | if (outputDesc->mMuteCount[stream] == 0) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5712 | if (mVolumeCurves->canBeMuted(stream) && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5713 | ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5714 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5715 | checkAndSetVolume(stream, 0, outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5716 | } |
| 5717 | } |
| 5718 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored |
| 5719 | outputDesc->mMuteCount[stream]++; |
| 5720 | } else { |
| 5721 | if (outputDesc->mMuteCount[stream] == 0) { |
| 5722 | ALOGV("setStreamMute() unmuting non muted stream!"); |
| 5723 | return; |
| 5724 | } |
| 5725 | if (--outputDesc->mMuteCount[stream] == 0) { |
| 5726 | checkAndSetVolume(stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5727 | mVolumeCurves->getVolumeIndex(stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5728 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5729 | device, |
| 5730 | delayMs); |
| 5731 | } |
| 5732 | } |
| 5733 | } |
| 5734 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5735 | audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr) |
| 5736 | { |
| 5737 | // flags to stream type mapping |
| 5738 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 5739 | return AUDIO_STREAM_ENFORCED_AUDIBLE; |
| 5740 | } |
| 5741 | if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) { |
| 5742 | return AUDIO_STREAM_BLUETOOTH_SCO; |
| 5743 | } |
Jean-Michel Trivi | 79ad438 | 2015-01-29 10:49:39 -0800 | [diff] [blame] | 5744 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 5745 | return AUDIO_STREAM_TTS; |
| 5746 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5747 | |
Ari Hausman-Cohen | 5c00d01 | 2018-06-26 17:09:11 -0700 | [diff] [blame] | 5748 | return audio_usage_to_stream_type(attr->usage); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5749 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5750 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5751 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) |
| 5752 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5753 | // has flags that map to a strategy? |
| 5754 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { |
| 5755 | return true; |
| 5756 | } |
| 5757 | |
| 5758 | // has known usage? |
| 5759 | switch (paa->usage) { |
| 5760 | case AUDIO_USAGE_UNKNOWN: |
| 5761 | case AUDIO_USAGE_MEDIA: |
| 5762 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 5763 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 5764 | case AUDIO_USAGE_ALARM: |
| 5765 | case AUDIO_USAGE_NOTIFICATION: |
| 5766 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 5767 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 5768 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 5769 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 5770 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 5771 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 5772 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 5773 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 5774 | case AUDIO_USAGE_GAME: |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5775 | case AUDIO_USAGE_VIRTUAL_SOURCE: |
Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 5776 | case AUDIO_USAGE_ASSISTANT: |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5777 | break; |
| 5778 | default: |
| 5779 | return false; |
| 5780 | } |
| 5781 | return true; |
| 5782 | } |
| 5783 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5784 | bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor>& outputDesc, |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5785 | routing_strategy strategy, uint32_t inPastMs, |
| 5786 | nsecs_t sysTime) const |
| 5787 | { |
| 5788 | if ((sysTime == 0) && (inPastMs != 0)) { |
| 5789 | sysTime = systemTime(); |
| 5790 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5791 | 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] | 5792 | if (((getStrategy((audio_stream_type_t)i) == strategy) || |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5793 | (STRATEGY_NONE == strategy)) && |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5794 | outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) { |
| 5795 | return true; |
| 5796 | } |
| 5797 | } |
| 5798 | return false; |
| 5799 | } |
| 5800 | |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 5801 | bool AudioPolicyManager::isStrategyActiveOnSameModule(const sp<AudioOutputDescriptor>& outputDesc, |
| 5802 | routing_strategy strategy, uint32_t inPastMs, |
| 5803 | nsecs_t sysTime) const |
| 5804 | { |
| 5805 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5806 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 5807 | if (outputDesc->sharesHwModuleWith(desc) |
| 5808 | && isStrategyActive(desc, strategy, inPastMs, sysTime)) { |
| 5809 | return true; |
| 5810 | } |
| 5811 | } |
| 5812 | return false; |
| 5813 | } |
| 5814 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5815 | audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage) |
| 5816 | { |
| 5817 | return mEngine->getForceUse(usage); |
| 5818 | } |
| 5819 | |
| 5820 | bool AudioPolicyManager::isInCall() |
| 5821 | { |
| 5822 | return isStateInCall(mEngine->getPhoneState()); |
| 5823 | } |
| 5824 | |
| 5825 | bool AudioPolicyManager::isStateInCall(int state) |
| 5826 | { |
| 5827 | return is_state_in_call(state); |
| 5828 | } |
| 5829 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5830 | void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc) |
| 5831 | { |
| 5832 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5833 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 5834 | if (sourceDesc->srcDevice()->equals(deviceDesc)) { |
| 5835 | ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->portId()); |
| 5836 | stopAudioSource(sourceDesc->portId()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5837 | } |
| 5838 | } |
| 5839 | |
| 5840 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
| 5841 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 5842 | bool release = false; |
| 5843 | for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) { |
| 5844 | const struct audio_port_config *source = &patchDesc->mPatch.sources[j]; |
| 5845 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 5846 | source->ext.device.type == deviceDesc->type()) { |
| 5847 | release = true; |
| 5848 | } |
| 5849 | } |
| 5850 | for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) { |
| 5851 | const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j]; |
| 5852 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && |
| 5853 | sink->ext.device.type == deviceDesc->type()) { |
| 5854 | release = true; |
| 5855 | } |
| 5856 | } |
| 5857 | if (release) { |
| 5858 | ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle); |
| 5859 | releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid); |
| 5860 | } |
| 5861 | } |
| 5862 | } |
| 5863 | |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5864 | // Modify the list of surround sound formats supported. |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5865 | void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) { |
| 5866 | FormatVector &formats = *formatsPtr; |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5867 | // TODO Set this based on Config properties. |
| 5868 | const bool alwaysForceAC3 = true; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5869 | |
| 5870 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5871 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5872 | ALOGD("%s: forced use = %d", __FUNCTION__, forceUse); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5873 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5874 | // If MANUAL, keep the supported surround sound formats as current enabled ones. |
| 5875 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
| 5876 | formats.clear(); |
| 5877 | for (auto it = mSurroundFormats.begin(); it != mSurroundFormats.end(); it++) { |
| 5878 | formats.add(*it); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5879 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5880 | // Always enable IEC61937 when in MANUAL mode. |
| 5881 | formats.add(AUDIO_FORMAT_IEC61937); |
| 5882 | } else { // NEVER, AUTO or ALWAYS |
| 5883 | // Analyze original support for various formats. |
| 5884 | bool supportsAC3 = false; |
| 5885 | bool supportsOtherSurround = false; |
| 5886 | bool supportsIEC61937 = false; |
| 5887 | mSurroundFormats.clear(); |
| 5888 | for (ssize_t formatIndex = 0; formatIndex < (ssize_t)formats.size(); formatIndex++) { |
| 5889 | audio_format_t format = formats[formatIndex]; |
| 5890 | switch (format) { |
| 5891 | case AUDIO_FORMAT_AC3: |
| 5892 | supportsAC3 = true; |
| 5893 | break; |
| 5894 | case AUDIO_FORMAT_E_AC3: |
| 5895 | case AUDIO_FORMAT_DTS: |
| 5896 | case AUDIO_FORMAT_DTS_HD: |
| 5897 | // If ALWAYS, remove all other surround formats here |
| 5898 | // since we will add them later. |
| 5899 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { |
| 5900 | formats.removeAt(formatIndex); |
| 5901 | formatIndex--; |
| 5902 | } |
| 5903 | supportsOtherSurround = true; |
| 5904 | break; |
| 5905 | case AUDIO_FORMAT_IEC61937: |
| 5906 | supportsIEC61937 = true; |
| 5907 | break; |
| 5908 | default: |
| 5909 | break; |
| 5910 | } |
| 5911 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5912 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5913 | // Modify formats based on surround preferences. |
| 5914 | // If NEVER, remove support for surround formats. |
| 5915 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
| 5916 | if (supportsAC3 || supportsOtherSurround || supportsIEC61937) { |
| 5917 | // Remove surround sound related formats. |
| 5918 | for (size_t formatIndex = 0; formatIndex < formats.size(); ) { |
| 5919 | audio_format_t format = formats[formatIndex]; |
| 5920 | switch(format) { |
| 5921 | case AUDIO_FORMAT_AC3: |
| 5922 | case AUDIO_FORMAT_E_AC3: |
| 5923 | case AUDIO_FORMAT_DTS: |
| 5924 | case AUDIO_FORMAT_DTS_HD: |
| 5925 | case AUDIO_FORMAT_IEC61937: |
| 5926 | formats.removeAt(formatIndex); |
| 5927 | break; |
| 5928 | default: |
| 5929 | formatIndex++; // keep it |
| 5930 | break; |
| 5931 | } |
| 5932 | } |
| 5933 | supportsAC3 = false; |
| 5934 | supportsOtherSurround = false; |
| 5935 | supportsIEC61937 = false; |
| 5936 | } |
| 5937 | } else { // AUTO or ALWAYS |
| 5938 | // Most TVs support AC3 even if they do not report it in the EDID. |
| 5939 | if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)) |
| 5940 | && !supportsAC3) { |
| 5941 | formats.add(AUDIO_FORMAT_AC3); |
| 5942 | supportsAC3 = true; |
| 5943 | } |
| 5944 | |
| 5945 | // If ALWAYS, add support for raw surround formats if all are missing. |
| 5946 | // This assumes that if any of these formats are reported by the HAL |
| 5947 | // then the report is valid and should not be modified. |
| 5948 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { |
| 5949 | formats.add(AUDIO_FORMAT_E_AC3); |
| 5950 | formats.add(AUDIO_FORMAT_DTS); |
| 5951 | formats.add(AUDIO_FORMAT_DTS_HD); |
| 5952 | supportsOtherSurround = true; |
| 5953 | } |
| 5954 | |
| 5955 | // Add support for IEC61937 if any raw surround supported. |
| 5956 | // The HAL could do this but add it here, just in case. |
| 5957 | if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) { |
| 5958 | formats.add(AUDIO_FORMAT_IEC61937); |
| 5959 | supportsIEC61937 = true; |
| 5960 | } |
| 5961 | |
| 5962 | // Add reported surround sound formats to enabled surround formats. |
| 5963 | for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) { |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5964 | audio_format_t format = formats[formatIndex]; |
| 5965 | switch(format) { |
| 5966 | case AUDIO_FORMAT_AC3: |
| 5967 | case AUDIO_FORMAT_E_AC3: |
| 5968 | case AUDIO_FORMAT_DTS: |
| 5969 | case AUDIO_FORMAT_DTS_HD: |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5970 | case AUDIO_FORMAT_AAC_LC: |
| 5971 | case AUDIO_FORMAT_DOLBY_TRUEHD: |
| 5972 | case AUDIO_FORMAT_E_AC3_JOC: |
| 5973 | mSurroundFormats.insert(format); |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5974 | default: |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5975 | break; |
| 5976 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5977 | } |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5978 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5979 | } |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5980 | } |
| 5981 | |
| 5982 | // Modify the list of channel masks supported. |
| 5983 | void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) { |
| 5984 | ChannelsVector &channelMasks = *channelMasksPtr; |
| 5985 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5986 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
| 5987 | |
| 5988 | // If NEVER, then remove support for channelMasks > stereo. |
| 5989 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
| 5990 | for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) { |
| 5991 | audio_channel_mask_t channelMask = channelMasks[maskIndex]; |
| 5992 | if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) { |
| 5993 | ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask); |
| 5994 | channelMasks.removeAt(maskIndex); |
| 5995 | } else { |
| 5996 | maskIndex++; |
| 5997 | } |
| 5998 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5999 | // If ALWAYS or MANUAL, then make sure we at least support 5.1 |
| 6000 | } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS |
| 6001 | || forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6002 | bool supports5dot1 = false; |
| 6003 | // Are there any channel masks that can be considered "surround"? |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 6004 | for (audio_channel_mask_t channelMask : channelMasks) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6005 | if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) { |
| 6006 | supports5dot1 = true; |
| 6007 | break; |
| 6008 | } |
| 6009 | } |
| 6010 | // If not then add 5.1 support. |
| 6011 | if (!supports5dot1) { |
| 6012 | channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1); |
| 6013 | ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__); |
| 6014 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 6015 | } |
| 6016 | } |
| 6017 | |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 6018 | void AudioPolicyManager::updateAudioProfiles(audio_devices_t device, |
| 6019 | audio_io_handle_t ioHandle, |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6020 | AudioProfileVector &profiles) |
| 6021 | { |
| 6022 | String8 reply; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6023 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6024 | // Format MUST be checked first to update the list of AudioProfile |
| 6025 | if (profiles.hasDynamicFormat()) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6026 | reply = mpClientInterface->getParameters( |
| 6027 | ioHandle, String8(AudioParameter::keyStreamSupportedFormats)); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6028 | ALOGV("%s: supported formats %d, %s", __FUNCTION__, ioHandle, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 6029 | AudioParameter repliedParameters(reply); |
| 6030 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6031 | String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) { |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6032 | ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__); |
| 6033 | return; |
| 6034 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 6035 | FormatVector formats = formatsFromString(reply.string()); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 6036 | if (device == AUDIO_DEVICE_OUT_HDMI) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6037 | filterSurroundFormats(&formats); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 6038 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 6039 | profiles.setFormats(formats); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6040 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6041 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 6042 | for (audio_format_t format : profiles.getSupportedFormats()) { |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 6043 | ChannelsVector channelMasks; |
| 6044 | SampleRateVector samplingRates; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6045 | AudioParameter requestedParameters; |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6046 | requestedParameters.addInt(String8(AudioParameter::keyFormat), format); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6047 | |
| 6048 | if (profiles.hasDynamicRateFor(format)) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6049 | reply = mpClientInterface->getParameters( |
| 6050 | ioHandle, |
| 6051 | requestedParameters.toString() + ";" + |
| 6052 | AudioParameter::keyStreamSupportedSamplingRates); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6053 | ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 6054 | AudioParameter repliedParameters(reply); |
| 6055 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6056 | String8(AudioParameter::keyStreamSupportedSamplingRates), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 6057 | samplingRates = samplingRatesFromString(reply.string()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6058 | } |
| 6059 | } |
| 6060 | if (profiles.hasDynamicChannelsFor(format)) { |
| 6061 | reply = mpClientInterface->getParameters(ioHandle, |
| 6062 | requestedParameters.toString() + ";" + |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6063 | AudioParameter::keyStreamSupportedChannels); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6064 | ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 6065 | AudioParameter repliedParameters(reply); |
| 6066 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6067 | String8(AudioParameter::keyStreamSupportedChannels), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 6068 | channelMasks = channelMasksFromString(reply.string()); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6069 | if (device == AUDIO_DEVICE_OUT_HDMI) { |
| 6070 | filterSurroundChannelMasks(&channelMasks); |
| 6071 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6072 | } |
| 6073 | } |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 6074 | profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates)); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6075 | } |
| 6076 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6077 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 6078 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 6079 | audio_patch_handle_t *patchHandle, |
| 6080 | AudioIODescriptorInterface *ioDescriptor, |
| 6081 | const struct audio_patch *patch, |
| 6082 | int delayMs) |
| 6083 | { |
| 6084 | ssize_t index = mAudioPatches.indexOfKey( |
| 6085 | patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE ? |
| 6086 | *patchHandle : ioDescriptor->getPatchHandle()); |
| 6087 | sp<AudioPatch> patchDesc; |
| 6088 | status_t status = installPatch( |
| 6089 | caller, index, patchHandle, patch, delayMs, mUidCached, &patchDesc); |
| 6090 | if (status == NO_ERROR) { |
| 6091 | ioDescriptor->setPatchHandle(patchDesc->mHandle); |
| 6092 | } |
| 6093 | return status; |
| 6094 | } |
| 6095 | |
| 6096 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 6097 | ssize_t index, |
| 6098 | audio_patch_handle_t *patchHandle, |
| 6099 | const struct audio_patch *patch, |
| 6100 | int delayMs, |
| 6101 | uid_t uid, |
| 6102 | sp<AudioPatch> *patchDescPtr) |
| 6103 | { |
| 6104 | sp<AudioPatch> patchDesc; |
| 6105 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 6106 | if (index >= 0) { |
| 6107 | patchDesc = mAudioPatches.valueAt(index); |
| 6108 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 6109 | } |
| 6110 | |
| 6111 | status_t status = mpClientInterface->createAudioPatch(patch, &afPatchHandle, delayMs); |
| 6112 | ALOGV("%s() AF::createAudioPatch returned %d patchHandle %d num_sources %d num_sinks %d", |
| 6113 | caller, status, afPatchHandle, patch->num_sources, patch->num_sinks); |
| 6114 | if (status == NO_ERROR) { |
| 6115 | if (index < 0) { |
| 6116 | patchDesc = new AudioPatch(patch, uid); |
| 6117 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 6118 | } else { |
| 6119 | patchDesc->mPatch = *patch; |
| 6120 | } |
| 6121 | patchDesc->mAfPatchHandle = afPatchHandle; |
| 6122 | if (patchHandle) { |
| 6123 | *patchHandle = patchDesc->mHandle; |
| 6124 | } |
| 6125 | nextAudioPortGeneration(); |
| 6126 | mpClientInterface->onAudioPatchListUpdate(); |
| 6127 | } |
| 6128 | if (patchDescPtr) *patchDescPtr = patchDesc; |
| 6129 | return status; |
| 6130 | } |
| 6131 | |
Mikhail Naganov | 1b2a794 | 2017-12-08 10:18:09 -0800 | [diff] [blame] | 6132 | } // namespace android |